Blame SOURCES/kvm-i386-Helpers-to-encode-cache-information-consistentl.patch

383d26
From 4e90b977aecdc26df8cda57ee38e2a8159685b1f Mon Sep 17 00:00:00 2001
383d26
From: Eduardo Habkost <ehabkost@redhat.com>
383d26
Date: Tue, 3 Jul 2018 17:23:47 +0200
383d26
Subject: [PATCH 02/89] i386: Helpers to encode cache information consistently
383d26
383d26
RH-Author: Eduardo Habkost <ehabkost@redhat.com>
383d26
Message-id: <20180703172356.21038-2-ehabkost@redhat.com>
383d26
Patchwork-id: 81210
383d26
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH v3 01/10] i386: Helpers to encode cache information consistently
383d26
Bugzilla: 1481253
383d26
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
383d26
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
383d26
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
383d26
Instead of having a collection of macros that need to be used in
383d26
complex expressions to build CPUID data, define a CPUCacheInfo
383d26
struct that can hold information about a given cache.  Helper
383d26
functions will take a CPUCacheInfo struct as input to encode
383d26
CPUID leaves for a cache.
383d26
383d26
This will help us ensure consistency between cache information
383d26
CPUID leaves, and make the existing inconsistencies in CPUID info
383d26
more visible.
383d26
383d26
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
383d26
Signed-off-by: Babu Moger <babu.moger@amd.com>
383d26
Tested-by: Geoffrey McRae <geoff@hostfission.com>
383d26
Message-Id: <20180510204148.11687-2-babu.moger@amd.com>
383d26
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
383d26
(cherry picked from commit 7e3482f824809e1f6ffeb5bb8103ba27a7d1a52a)
383d26
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
383d26
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
---
383d26
 target/i386/cpu.c | 495 ++++++++++++++++++++++++++++++++++++++++--------------
383d26
 target/i386/cpu.h |  53 ++++++
383d26
 2 files changed, 424 insertions(+), 124 deletions(-)
383d26
383d26
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
383d26
index 5d60d76..9a5a164 100644
383d26
--- a/target/i386/cpu.c
383d26
+++ b/target/i386/cpu.c
383d26
@@ -56,33 +56,240 @@
383d26
 
383d26
 #include "disas/capstone.h"
383d26
 
383d26
+/* Helpers for building CPUID[2] descriptors: */
383d26
+
383d26
+struct CPUID2CacheDescriptorInfo {
383d26
+    enum CacheType type;
383d26
+    int level;
383d26
+    int size;
383d26
+    int line_size;
383d26
+    int associativity;
383d26
+};
383d26
 
383d26
-/* Cache topology CPUID constants: */
383d26
+#define KiB 1024
383d26
+#define MiB (1024 * 1024)
383d26
 
383d26
-/* CPUID Leaf 2 Descriptors */
383d26
+/*
383d26
+ * Known CPUID 2 cache descriptors.
383d26
+ * From Intel SDM Volume 2A, CPUID instruction
383d26
+ */
383d26
+struct CPUID2CacheDescriptorInfo cpuid2_cache_descriptors[] = {
383d26
+    [0x06] = { .level = 1, .type = ICACHE,        .size =   8 * KiB,
383d26
+               .associativity = 4,  .line_size = 32, },
383d26
+    [0x08] = { .level = 1, .type = ICACHE,        .size =  16 * KiB,
383d26
+               .associativity = 4,  .line_size = 32, },
383d26
+    [0x09] = { .level = 1, .type = ICACHE,        .size =  32 * KiB,
383d26
+               .associativity = 4,  .line_size = 64, },
383d26
+    [0x0A] = { .level = 1, .type = DCACHE,        .size =   8 * KiB,
383d26
+               .associativity = 2,  .line_size = 32, },
383d26
+    [0x0C] = { .level = 1, .type = DCACHE,        .size =  16 * KiB,
383d26
+               .associativity = 4,  .line_size = 32, },
383d26
+    [0x0D] = { .level = 1, .type = DCACHE,        .size =  16 * KiB,
383d26
+               .associativity = 4,  .line_size = 64, },
383d26
+    [0x0E] = { .level = 1, .type = DCACHE,        .size =  24 * KiB,
383d26
+               .associativity = 6,  .line_size = 64, },
383d26
+    [0x1D] = { .level = 2, .type = UNIFIED_CACHE, .size = 128 * KiB,
383d26
+               .associativity = 2,  .line_size = 64, },
383d26
+    [0x21] = { .level = 2, .type = UNIFIED_CACHE, .size = 256 * KiB,
383d26
+               .associativity = 8,  .line_size = 64, },
383d26
+    /* lines per sector is not supported cpuid2_cache_descriptor(),
383d26
+    * so descriptors 0x22, 0x23 are not included
383d26
+    */
383d26
+    [0x24] = { .level = 2, .type = UNIFIED_CACHE, .size =   1 * MiB,
383d26
+               .associativity = 16, .line_size = 64, },
383d26
+    /* lines per sector is not supported cpuid2_cache_descriptor(),
383d26
+    * so descriptors 0x25, 0x20 are not included
383d26
+    */
383d26
+    [0x2C] = { .level = 1, .type = DCACHE,        .size =  32 * KiB,
383d26
+               .associativity = 8,  .line_size = 64, },
383d26
+    [0x30] = { .level = 1, .type = ICACHE,        .size =  32 * KiB,
383d26
+               .associativity = 8,  .line_size = 64, },
383d26
+    [0x41] = { .level = 2, .type = UNIFIED_CACHE, .size = 128 * KiB,
383d26
+               .associativity = 4,  .line_size = 32, },
383d26
+    [0x42] = { .level = 2, .type = UNIFIED_CACHE, .size = 256 * KiB,
383d26
+               .associativity = 4,  .line_size = 32, },
383d26
+    [0x43] = { .level = 2, .type = UNIFIED_CACHE, .size = 512 * KiB,
383d26
+               .associativity = 4,  .line_size = 32, },
383d26
+    [0x44] = { .level = 2, .type = UNIFIED_CACHE, .size =   1 * MiB,
383d26
+               .associativity = 4,  .line_size = 32, },
383d26
+    [0x45] = { .level = 2, .type = UNIFIED_CACHE, .size =   2 * MiB,
383d26
+               .associativity = 4,  .line_size = 32, },
383d26
+    [0x46] = { .level = 3, .type = UNIFIED_CACHE, .size =   4 * MiB,
383d26
+               .associativity = 4,  .line_size = 64, },
383d26
+    [0x47] = { .level = 3, .type = UNIFIED_CACHE, .size =   8 * MiB,
383d26
+               .associativity = 8,  .line_size = 64, },
383d26
+    [0x48] = { .level = 2, .type = UNIFIED_CACHE, .size =   3 * MiB,
383d26
+               .associativity = 12, .line_size = 64, },
383d26
+    /* Descriptor 0x49 depends on CPU family/model, so it is not included */
383d26
+    [0x4A] = { .level = 3, .type = UNIFIED_CACHE, .size =   6 * MiB,
383d26
+               .associativity = 12, .line_size = 64, },
383d26
+    [0x4B] = { .level = 3, .type = UNIFIED_CACHE, .size =   8 * MiB,
383d26
+               .associativity = 16, .line_size = 64, },
383d26
+    [0x4C] = { .level = 3, .type = UNIFIED_CACHE, .size =  12 * MiB,
383d26
+               .associativity = 12, .line_size = 64, },
383d26
+    [0x4D] = { .level = 3, .type = UNIFIED_CACHE, .size =  16 * MiB,
383d26
+               .associativity = 16, .line_size = 64, },
383d26
+    [0x4E] = { .level = 2, .type = UNIFIED_CACHE, .size =   6 * MiB,
383d26
+               .associativity = 24, .line_size = 64, },
383d26
+    [0x60] = { .level = 1, .type = DCACHE,        .size =  16 * KiB,
383d26
+               .associativity = 8,  .line_size = 64, },
383d26
+    [0x66] = { .level = 1, .type = DCACHE,        .size =   8 * KiB,
383d26
+               .associativity = 4,  .line_size = 64, },
383d26
+    [0x67] = { .level = 1, .type = DCACHE,        .size =  16 * KiB,
383d26
+               .associativity = 4,  .line_size = 64, },
383d26
+    [0x68] = { .level = 1, .type = DCACHE,        .size =  32 * KiB,
383d26
+               .associativity = 4,  .line_size = 64, },
383d26
+    [0x78] = { .level = 2, .type = UNIFIED_CACHE, .size =   1 * MiB,
383d26
+               .associativity = 4,  .line_size = 64, },
383d26
+    /* lines per sector is not supported cpuid2_cache_descriptor(),
383d26
+    * so descriptors 0x79, 0x7A, 0x7B, 0x7C are not included.
383d26
+    */
383d26
+    [0x7D] = { .level = 2, .type = UNIFIED_CACHE, .size =   2 * MiB,
383d26
+               .associativity = 8,  .line_size = 64, },
383d26
+    [0x7F] = { .level = 2, .type = UNIFIED_CACHE, .size = 512 * KiB,
383d26
+               .associativity = 2,  .line_size = 64, },
383d26
+    [0x80] = { .level = 2, .type = UNIFIED_CACHE, .size = 512 * KiB,
383d26
+               .associativity = 8,  .line_size = 64, },
383d26
+    [0x82] = { .level = 2, .type = UNIFIED_CACHE, .size = 256 * KiB,
383d26
+               .associativity = 8,  .line_size = 32, },
383d26
+    [0x83] = { .level = 2, .type = UNIFIED_CACHE, .size = 512 * KiB,
383d26
+               .associativity = 8,  .line_size = 32, },
383d26
+    [0x84] = { .level = 2, .type = UNIFIED_CACHE, .size =   1 * MiB,
383d26
+               .associativity = 8,  .line_size = 32, },
383d26
+    [0x85] = { .level = 2, .type = UNIFIED_CACHE, .size =   2 * MiB,
383d26
+               .associativity = 8,  .line_size = 32, },
383d26
+    [0x86] = { .level = 2, .type = UNIFIED_CACHE, .size = 512 * KiB,
383d26
+               .associativity = 4,  .line_size = 64, },
383d26
+    [0x87] = { .level = 2, .type = UNIFIED_CACHE, .size =   1 * MiB,
383d26
+               .associativity = 8,  .line_size = 64, },
383d26
+    [0xD0] = { .level = 3, .type = UNIFIED_CACHE, .size = 512 * KiB,
383d26
+               .associativity = 4,  .line_size = 64, },
383d26
+    [0xD1] = { .level = 3, .type = UNIFIED_CACHE, .size =   1 * MiB,
383d26
+               .associativity = 4,  .line_size = 64, },
383d26
+    [0xD2] = { .level = 3, .type = UNIFIED_CACHE, .size =   2 * MiB,
383d26
+               .associativity = 4,  .line_size = 64, },
383d26
+    [0xD6] = { .level = 3, .type = UNIFIED_CACHE, .size =   1 * MiB,
383d26
+               .associativity = 8,  .line_size = 64, },
383d26
+    [0xD7] = { .level = 3, .type = UNIFIED_CACHE, .size =   2 * MiB,
383d26
+               .associativity = 8,  .line_size = 64, },
383d26
+    [0xD8] = { .level = 3, .type = UNIFIED_CACHE, .size =   4 * MiB,
383d26
+               .associativity = 8,  .line_size = 64, },
383d26
+    [0xDC] = { .level = 3, .type = UNIFIED_CACHE, .size = 1.5 * MiB,
383d26
+               .associativity = 12, .line_size = 64, },
383d26
+    [0xDD] = { .level = 3, .type = UNIFIED_CACHE, .size =   3 * MiB,
383d26
+               .associativity = 12, .line_size = 64, },
383d26
+    [0xDE] = { .level = 3, .type = UNIFIED_CACHE, .size =   6 * MiB,
383d26
+               .associativity = 12, .line_size = 64, },
383d26
+    [0xE2] = { .level = 3, .type = UNIFIED_CACHE, .size =   2 * MiB,
383d26
+               .associativity = 16, .line_size = 64, },
383d26
+    [0xE3] = { .level = 3, .type = UNIFIED_CACHE, .size =   4 * MiB,
383d26
+               .associativity = 16, .line_size = 64, },
383d26
+    [0xE4] = { .level = 3, .type = UNIFIED_CACHE, .size =   8 * MiB,
383d26
+               .associativity = 16, .line_size = 64, },
383d26
+    [0xEA] = { .level = 3, .type = UNIFIED_CACHE, .size =  12 * MiB,
383d26
+               .associativity = 24, .line_size = 64, },
383d26
+    [0xEB] = { .level = 3, .type = UNIFIED_CACHE, .size =  18 * MiB,
383d26
+               .associativity = 24, .line_size = 64, },
383d26
+    [0xEC] = { .level = 3, .type = UNIFIED_CACHE, .size =  24 * MiB,
383d26
+               .associativity = 24, .line_size = 64, },
383d26
+};
383d26
+
383d26
+/*
383d26
+ * "CPUID leaf 2 does not report cache descriptor information,
383d26
+ * use CPUID leaf 4 to query cache parameters"
383d26
+ */
383d26
+#define CACHE_DESCRIPTOR_UNAVAILABLE 0xFF
383d26
 
383d26
-#define CPUID_2_L1D_32KB_8WAY_64B 0x2c
383d26
-#define CPUID_2_L1I_32KB_8WAY_64B 0x30
383d26
-#define CPUID_2_L2_2MB_8WAY_64B   0x7d
383d26
-#define CPUID_2_L3_16MB_16WAY_64B 0x4d
383d26
+/*
383d26
+ * Return a CPUID 2 cache descriptor for a given cache.
383d26
+ * If no known descriptor is found, return CACHE_DESCRIPTOR_UNAVAILABLE
383d26
+ */
383d26
+static uint8_t cpuid2_cache_descriptor(CPUCacheInfo *cache)
383d26
+{
383d26
+    int i;
383d26
+
383d26
+    assert(cache->size > 0);
383d26
+    assert(cache->level > 0);
383d26
+    assert(cache->line_size > 0);
383d26
+    assert(cache->associativity > 0);
383d26
+    for (i = 0; i < ARRAY_SIZE(cpuid2_cache_descriptors); i++) {
383d26
+        struct CPUID2CacheDescriptorInfo *d = &cpuid2_cache_descriptors[i];
383d26
+        if (d->level == cache->level && d->type == cache->type &&
383d26
+            d->size == cache->size && d->line_size == cache->line_size &&
383d26
+            d->associativity == cache->associativity) {
383d26
+                return i;
383d26
+            }
383d26
+    }
383d26
 
383d26
+    return CACHE_DESCRIPTOR_UNAVAILABLE;
383d26
+}
383d26
 
383d26
 /* CPUID Leaf 4 constants: */
383d26
 
383d26
 /* EAX: */
383d26
-#define CPUID_4_TYPE_DCACHE  1
383d26
-#define CPUID_4_TYPE_ICACHE  2
383d26
-#define CPUID_4_TYPE_UNIFIED 3
383d26
+#define CACHE_TYPE_D    1
383d26
+#define CACHE_TYPE_I    2
383d26
+#define CACHE_TYPE_UNIFIED   3
383d26
 
383d26
-#define CPUID_4_LEVEL(l)          ((l) << 5)
383d26
+#define CACHE_LEVEL(l)        (l << 5)
383d26
 
383d26
-#define CPUID_4_SELF_INIT_LEVEL (1 << 8)
383d26
-#define CPUID_4_FULLY_ASSOC     (1 << 9)
383d26
+#define CACHE_SELF_INIT_LEVEL (1 << 8)
383d26
 
383d26
 /* EDX: */
383d26
-#define CPUID_4_NO_INVD_SHARING (1 << 0)
383d26
-#define CPUID_4_INCLUSIVE       (1 << 1)
383d26
-#define CPUID_4_COMPLEX_IDX     (1 << 2)
383d26
+#define CACHE_NO_INVD_SHARING   (1 << 0)
383d26
+#define CACHE_INCLUSIVE       (1 << 1)
383d26
+#define CACHE_COMPLEX_IDX     (1 << 2)
383d26
+
383d26
+/* Encode CacheType for CPUID[4].EAX */
383d26
+#define CACHE_TYPE(t) (((t) == DCACHE)  ? CACHE_TYPE_D  : \
383d26
+                         ((t) == ICACHE)  ? CACHE_TYPE_I  : \
383d26
+                         ((t) == UNIFIED_CACHE) ? CACHE_TYPE_UNIFIED : \
383d26
+                         0 /* Invalid value */)
383d26
+
383d26
+
383d26
+/* Encode cache info for CPUID[4] */
383d26
+static void encode_cache_cpuid4(CPUCacheInfo *cache,
383d26
+                                int num_apic_ids, int num_cores,
383d26
+                                uint32_t *eax, uint32_t *ebx,
383d26
+                                uint32_t *ecx, uint32_t *edx)
383d26
+{
383d26
+    assert(cache->size == cache->line_size * cache->associativity *
383d26
+                          cache->partitions * cache->sets);
383d26
+
383d26
+    assert(num_apic_ids > 0);
383d26
+    *eax = CACHE_TYPE(cache->type) |
383d26
+           CACHE_LEVEL(cache->level) |
383d26
+           (cache->self_init ? CACHE_SELF_INIT_LEVEL : 0) |
383d26
+           ((num_cores - 1) << 26) |
383d26
+           ((num_apic_ids - 1) << 14);
383d26
+
383d26
+    assert(cache->line_size > 0);
383d26
+    assert(cache->partitions > 0);
383d26
+    assert(cache->associativity > 0);
383d26
+    /* We don't implement fully-associative caches */
383d26
+    assert(cache->associativity < cache->sets);
383d26
+    *ebx = (cache->line_size - 1) |
383d26
+           ((cache->partitions - 1) << 12) |
383d26
+           ((cache->associativity - 1) << 22);
383d26
+
383d26
+    assert(cache->sets > 0);
383d26
+    *ecx = cache->sets - 1;
383d26
+
383d26
+    *edx = (cache->no_invd_sharing ? CACHE_NO_INVD_SHARING : 0) |
383d26
+           (cache->inclusive ? CACHE_INCLUSIVE : 0) |
383d26
+           (cache->complex_indexing ? CACHE_COMPLEX_IDX : 0);
383d26
+}
383d26
+
383d26
+/* Encode cache info for CPUID[0x80000005].ECX or CPUID[0x80000005].EDX */
383d26
+static uint32_t encode_cache_cpuid80000005(CPUCacheInfo *cache)
383d26
+{
383d26
+    assert(cache->size % 1024 == 0);
383d26
+    assert(cache->lines_per_tag > 0);
383d26
+    assert(cache->associativity > 0);
383d26
+    assert(cache->line_size > 0);
383d26
+    return ((cache->size / 1024) << 24) | (cache->associativity << 16) |
383d26
+           (cache->lines_per_tag << 8) | (cache->line_size);
383d26
+}
383d26
 
383d26
 #define ASSOC_FULL 0xFF
383d26
 
383d26
@@ -100,57 +307,140 @@
383d26
                           a == ASSOC_FULL ? 0xF : \
383d26
                           0 /* invalid value */)
383d26
 
383d26
+/*
383d26
+ * Encode cache info for CPUID[0x80000006].ECX and CPUID[0x80000006].EDX
383d26
+ * @l3 can be NULL.
383d26
+ */
383d26
+static void encode_cache_cpuid80000006(CPUCacheInfo *l2,
383d26
+                                       CPUCacheInfo *l3,
383d26
+                                       uint32_t *ecx, uint32_t *edx)
383d26
+{
383d26
+    assert(l2->size % 1024 == 0);
383d26
+    assert(l2->associativity > 0);
383d26
+    assert(l2->lines_per_tag > 0);
383d26
+    assert(l2->line_size > 0);
383d26
+    *ecx = ((l2->size / 1024) << 16) |
383d26
+           (AMD_ENC_ASSOC(l2->associativity) << 12) |
383d26
+           (l2->lines_per_tag << 8) | (l2->line_size);
383d26
+
383d26
+    if (l3) {
383d26
+        assert(l3->size % (512 * 1024) == 0);
383d26
+        assert(l3->associativity > 0);
383d26
+        assert(l3->lines_per_tag > 0);
383d26
+        assert(l3->line_size > 0);
383d26
+        *edx = ((l3->size / (512 * 1024)) << 18) |
383d26
+               (AMD_ENC_ASSOC(l3->associativity) << 12) |
383d26
+               (l3->lines_per_tag << 8) | (l3->line_size);
383d26
+    } else {
383d26
+        *edx = 0;
383d26
+    }
383d26
+}
383d26
 
383d26
 /* Definitions of the hardcoded cache entries we expose: */
383d26
 
383d26
 /* L1 data cache: */
383d26
-#define L1D_LINE_SIZE         64
383d26
-#define L1D_ASSOCIATIVITY      8
383d26
-#define L1D_SETS              64
383d26
-#define L1D_PARTITIONS         1
383d26
-/* Size = LINE_SIZE*ASSOCIATIVITY*SETS*PARTITIONS = 32KiB */
383d26
-#define L1D_DESCRIPTOR CPUID_2_L1D_32KB_8WAY_64B
383d26
+static CPUCacheInfo l1d_cache = {
383d26
+    .type = DCACHE,
383d26
+    .level = 1,
383d26
+    .size = 32 * KiB,
383d26
+    .self_init = 1,
383d26
+    .line_size = 64,
383d26
+    .associativity = 8,
383d26
+    .sets = 64,
383d26
+    .partitions = 1,
383d26
+    .no_invd_sharing = true,
383d26
+};
383d26
+
383d26
 /*FIXME: CPUID leaf 0x80000005 is inconsistent with leaves 2 & 4 */
383d26
-#define L1D_LINES_PER_TAG      1
383d26
-#define L1D_SIZE_KB_AMD       64
383d26
-#define L1D_ASSOCIATIVITY_AMD  2
383d26
+static CPUCacheInfo l1d_cache_amd = {
383d26
+    .type = DCACHE,
383d26
+    .level = 1,
383d26
+    .size = 64 * KiB,
383d26
+    .self_init = 1,
383d26
+    .line_size = 64,
383d26
+    .associativity = 2,
383d26
+    .sets = 512,
383d26
+    .partitions = 1,
383d26
+    .lines_per_tag = 1,
383d26
+    .no_invd_sharing = true,
383d26
+};
383d26
 
383d26
 /* L1 instruction cache: */
383d26
-#define L1I_LINE_SIZE         64
383d26
-#define L1I_ASSOCIATIVITY      8
383d26
-#define L1I_SETS              64
383d26
-#define L1I_PARTITIONS         1
383d26
-/* Size = LINE_SIZE*ASSOCIATIVITY*SETS*PARTITIONS = 32KiB */
383d26
-#define L1I_DESCRIPTOR CPUID_2_L1I_32KB_8WAY_64B
383d26
+static CPUCacheInfo l1i_cache = {
383d26
+    .type = ICACHE,
383d26
+    .level = 1,
383d26
+    .size = 32 * KiB,
383d26
+    .self_init = 1,
383d26
+    .line_size = 64,
383d26
+    .associativity = 8,
383d26
+    .sets = 64,
383d26
+    .partitions = 1,
383d26
+    .no_invd_sharing = true,
383d26
+};
383d26
+
383d26
 /*FIXME: CPUID leaf 0x80000005 is inconsistent with leaves 2 & 4 */
383d26
-#define L1I_LINES_PER_TAG      1
383d26
-#define L1I_SIZE_KB_AMD       64
383d26
-#define L1I_ASSOCIATIVITY_AMD  2
383d26
+static CPUCacheInfo l1i_cache_amd = {
383d26
+    .type = ICACHE,
383d26
+    .level = 1,
383d26
+    .size = 64 * KiB,
383d26
+    .self_init = 1,
383d26
+    .line_size = 64,
383d26
+    .associativity = 2,
383d26
+    .sets = 512,
383d26
+    .partitions = 1,
383d26
+    .lines_per_tag = 1,
383d26
+    .no_invd_sharing = true,
383d26
+};
383d26
 
383d26
 /* Level 2 unified cache: */
383d26
-#define L2_LINE_SIZE          64
383d26
-#define L2_ASSOCIATIVITY      16
383d26
-#define L2_SETS             4096
383d26
-#define L2_PARTITIONS          1
383d26
-/* Size = LINE_SIZE*ASSOCIATIVITY*SETS*PARTITIONS = 4MiB */
383d26
+static CPUCacheInfo l2_cache = {
383d26
+    .type = UNIFIED_CACHE,
383d26
+    .level = 2,
383d26
+    .size = 4 * MiB,
383d26
+    .self_init = 1,
383d26
+    .line_size = 64,
383d26
+    .associativity = 16,
383d26
+    .sets = 4096,
383d26
+    .partitions = 1,
383d26
+    .no_invd_sharing = true,
383d26
+};
383d26
+
383d26
 /*FIXME: CPUID leaf 2 descriptor is inconsistent with CPUID leaf 4 */
383d26
-#define L2_DESCRIPTOR CPUID_2_L2_2MB_8WAY_64B
383d26
+static CPUCacheInfo l2_cache_cpuid2 = {
383d26
+    .type = UNIFIED_CACHE,
383d26
+    .level = 2,
383d26
+    .size = 2 * MiB,
383d26
+    .line_size = 64,
383d26
+    .associativity = 8,
383d26
+};
383d26
+
383d26
+
383d26
 /*FIXME: CPUID leaf 0x80000006 is inconsistent with leaves 2 & 4 */
383d26
-#define L2_LINES_PER_TAG       1
383d26
-#define L2_SIZE_KB_AMD       512
383d26
+static CPUCacheInfo l2_cache_amd = {
383d26
+    .type = UNIFIED_CACHE,
383d26
+    .level = 2,
383d26
+    .size = 512 * KiB,
383d26
+    .line_size = 64,
383d26
+    .lines_per_tag = 1,
383d26
+    .associativity = 16,
383d26
+    .sets = 512,
383d26
+    .partitions = 1,
383d26
+};
383d26
 
383d26
 /* Level 3 unified cache: */
383d26
-#define L3_SIZE_KB             0 /* disabled */
383d26
-#define L3_ASSOCIATIVITY       0 /* disabled */
383d26
-#define L3_LINES_PER_TAG       0 /* disabled */
383d26
-#define L3_LINE_SIZE           0 /* disabled */
383d26
-#define L3_N_LINE_SIZE         64
383d26
-#define L3_N_ASSOCIATIVITY     16
383d26
-#define L3_N_SETS           16384
383d26
-#define L3_N_PARTITIONS         1
383d26
-#define L3_N_DESCRIPTOR CPUID_2_L3_16MB_16WAY_64B
383d26
-#define L3_N_LINES_PER_TAG      1
383d26
-#define L3_N_SIZE_KB_AMD    16384
383d26
+static CPUCacheInfo l3_cache = {
383d26
+    .type = UNIFIED_CACHE,
383d26
+    .level = 3,
383d26
+    .size = 16 * MiB,
383d26
+    .line_size = 64,
383d26
+    .associativity = 16,
383d26
+    .sets = 16384,
383d26
+    .partitions = 1,
383d26
+    .lines_per_tag = 1,
383d26
+    .self_init = true,
383d26
+    .inclusive = true,
383d26
+    .complex_indexing = true,
383d26
+};
383d26
 
383d26
 /* TLB definitions: */
383d26
 
383d26
@@ -3327,85 +3617,53 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
383d26
         if (!cpu->enable_l3_cache) {
383d26
             *ecx = 0;
383d26
         } else {
383d26
-            *ecx = L3_N_DESCRIPTOR;
383d26
+            *ecx = cpuid2_cache_descriptor(&l3_cache);
383d26
         }
383d26
-        *edx = (L1D_DESCRIPTOR << 16) | \
383d26
-               (L1I_DESCRIPTOR <<  8) | \
383d26
-               (L2_DESCRIPTOR);
383d26
+        *edx = (cpuid2_cache_descriptor(&l1d_cache) << 16) |
383d26
+               (cpuid2_cache_descriptor(&l1i_cache) <<  8) |
383d26
+               (cpuid2_cache_descriptor(&l2_cache_cpuid2));
383d26
         break;
383d26
     case 4:
383d26
         /* cache info: needed for Core compatibility */
383d26
         if (cpu->cache_info_passthrough) {
383d26
             host_cpuid(index, count, eax, ebx, ecx, edx);
383d26
+            /* QEMU gives out its own APIC IDs, never pass down bits 31..26.  */
383d26
             *eax &= ~0xFC000000;
383d26
+            if ((*eax & 31) && cs->nr_cores > 1) {
383d26
+                *eax |= (cs->nr_cores - 1) << 26;
383d26
+            }
383d26
         } else {
383d26
             *eax = 0;
383d26
             switch (count) {
383d26
             case 0: /* L1 dcache info */
383d26
-                *eax |= CPUID_4_TYPE_DCACHE | \
383d26
-                        CPUID_4_LEVEL(1) | \
383d26
-                        CPUID_4_SELF_INIT_LEVEL;
383d26
-                *ebx = (L1D_LINE_SIZE - 1) | \
383d26
-                       ((L1D_PARTITIONS - 1) << 12) | \
383d26
-                       ((L1D_ASSOCIATIVITY - 1) << 22);
383d26
-                *ecx = L1D_SETS - 1;
383d26
-                *edx = CPUID_4_NO_INVD_SHARING;
383d26
+                encode_cache_cpuid4(&l1d_cache,
383d26
+                                    1, cs->nr_cores,
383d26
+                                    eax, ebx, ecx, edx);
383d26
                 break;
383d26
             case 1: /* L1 icache info */
383d26
-                *eax |= CPUID_4_TYPE_ICACHE | \
383d26
-                        CPUID_4_LEVEL(1) | \
383d26
-                        CPUID_4_SELF_INIT_LEVEL;
383d26
-                *ebx = (L1I_LINE_SIZE - 1) | \
383d26
-                       ((L1I_PARTITIONS - 1) << 12) | \
383d26
-                       ((L1I_ASSOCIATIVITY - 1) << 22);
383d26
-                *ecx = L1I_SETS - 1;
383d26
-                *edx = CPUID_4_NO_INVD_SHARING;
383d26
+                encode_cache_cpuid4(&l1i_cache,
383d26
+                                    1, cs->nr_cores,
383d26
+                                    eax, ebx, ecx, edx);
383d26
                 break;
383d26
             case 2: /* L2 cache info */
383d26
-                *eax |= CPUID_4_TYPE_UNIFIED | \
383d26
-                        CPUID_4_LEVEL(2) | \
383d26
-                        CPUID_4_SELF_INIT_LEVEL;
383d26
-                if (cs->nr_threads > 1) {
383d26
-                    *eax |= (cs->nr_threads - 1) << 14;
383d26
-                }
383d26
-                *ebx = (L2_LINE_SIZE - 1) | \
383d26
-                       ((L2_PARTITIONS - 1) << 12) | \
383d26
-                       ((L2_ASSOCIATIVITY - 1) << 22);
383d26
-                *ecx = L2_SETS - 1;
383d26
-                *edx = CPUID_4_NO_INVD_SHARING;
383d26
+                encode_cache_cpuid4(&l2_cache,
383d26
+                                    cs->nr_threads, cs->nr_cores,
383d26
+                                    eax, ebx, ecx, edx);
383d26
                 break;
383d26
             case 3: /* L3 cache info */
383d26
-                if (!cpu->enable_l3_cache) {
383d26
-                    *eax = 0;
383d26
-                    *ebx = 0;
383d26
-                    *ecx = 0;
383d26
-                    *edx = 0;
383d26
+                pkg_offset = apicid_pkg_offset(cs->nr_cores, cs->nr_threads);
383d26
+                if (cpu->enable_l3_cache) {
383d26
+                    encode_cache_cpuid4(&l3_cache,
383d26
+                                        (1 << pkg_offset), cs->nr_cores,
383d26
+                                        eax, ebx, ecx, edx);
383d26
                     break;
383d26
                 }
383d26
-                *eax |= CPUID_4_TYPE_UNIFIED | \
383d26
-                        CPUID_4_LEVEL(3) | \
383d26
-                        CPUID_4_SELF_INIT_LEVEL;
383d26
-                pkg_offset = apicid_pkg_offset(cs->nr_cores, cs->nr_threads);
383d26
-                *eax |= ((1 << pkg_offset) - 1) << 14;
383d26
-                *ebx = (L3_N_LINE_SIZE - 1) | \
383d26
-                       ((L3_N_PARTITIONS - 1) << 12) | \
383d26
-                       ((L3_N_ASSOCIATIVITY - 1) << 22);
383d26
-                *ecx = L3_N_SETS - 1;
383d26
-                *edx = CPUID_4_INCLUSIVE | CPUID_4_COMPLEX_IDX;
383d26
-                break;
383d26
+                /* fall through */
383d26
             default: /* end of info */
383d26
-                *eax = 0;
383d26
-                *ebx = 0;
383d26
-                *ecx = 0;
383d26
-                *edx = 0;
383d26
+                *eax = *ebx = *ecx = *edx = 0;
383d26
                 break;
383d26
             }
383d26
         }
383d26
-
383d26
-        /* QEMU gives out its own APIC IDs, never pass down bits 31..26.  */
383d26
-        if ((*eax & 31) && cs->nr_cores > 1) {
383d26
-            *eax |= (cs->nr_cores - 1) << 26;
383d26
-        }
383d26
         break;
383d26
     case 5:
383d26
         /* mwait info: needed for Core compatibility */
383d26
@@ -3609,10 +3867,8 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
383d26
                (L1_ITLB_2M_ASSOC <<  8) | (L1_ITLB_2M_ENTRIES);
383d26
         *ebx = (L1_DTLB_4K_ASSOC << 24) | (L1_DTLB_4K_ENTRIES << 16) | \
383d26
                (L1_ITLB_4K_ASSOC <<  8) | (L1_ITLB_4K_ENTRIES);
383d26
-        *ecx = (L1D_SIZE_KB_AMD << 24) | (L1D_ASSOCIATIVITY_AMD << 16) | \
383d26
-               (L1D_LINES_PER_TAG << 8) | (L1D_LINE_SIZE);
383d26
-        *edx = (L1I_SIZE_KB_AMD << 24) | (L1I_ASSOCIATIVITY_AMD << 16) | \
383d26
-               (L1I_LINES_PER_TAG << 8) | (L1I_LINE_SIZE);
383d26
+        *ecx = encode_cache_cpuid80000005(&l1d_cache_amd);
383d26
+        *edx = encode_cache_cpuid80000005(&l1i_cache_amd);
383d26
         break;
383d26
     case 0x80000006:
383d26
         /* cache info (L2 cache) */
383d26
@@ -3628,18 +3884,9 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
383d26
                (L2_DTLB_4K_ENTRIES << 16) | \
383d26
                (AMD_ENC_ASSOC(L2_ITLB_4K_ASSOC) << 12) | \
383d26
                (L2_ITLB_4K_ENTRIES);
383d26
-        *ecx = (L2_SIZE_KB_AMD << 16) | \
383d26
-               (AMD_ENC_ASSOC(L2_ASSOCIATIVITY) << 12) | \
383d26
-               (L2_LINES_PER_TAG << 8) | (L2_LINE_SIZE);
383d26
-        if (!cpu->enable_l3_cache) {
383d26
-            *edx = ((L3_SIZE_KB / 512) << 18) | \
383d26
-                   (AMD_ENC_ASSOC(L3_ASSOCIATIVITY) << 12) | \
383d26
-                   (L3_LINES_PER_TAG << 8) | (L3_LINE_SIZE);
383d26
-        } else {
383d26
-            *edx = ((L3_N_SIZE_KB_AMD / 512) << 18) | \
383d26
-                   (AMD_ENC_ASSOC(L3_N_ASSOCIATIVITY) << 12) | \
383d26
-                   (L3_N_LINES_PER_TAG << 8) | (L3_N_LINE_SIZE);
383d26
-        }
383d26
+        encode_cache_cpuid80000006(&l2_cache_amd,
383d26
+                                   cpu->enable_l3_cache ? &l3_cache : NULL,
383d26
+                                   ecx, edx);
383d26
         break;
383d26
     case 0x80000007:
383d26
         *eax = 0;
383d26
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
383d26
index 9aebe64..a0e4eeb 100644
383d26
--- a/target/i386/cpu.h
383d26
+++ b/target/i386/cpu.h
383d26
@@ -1046,6 +1046,59 @@ typedef enum TPRAccess {
383d26
     TPR_ACCESS_WRITE,
383d26
 } TPRAccess;
383d26
 
383d26
+/* Cache information data structures: */
383d26
+
383d26
+enum CacheType {
383d26
+    DCACHE,
383d26
+    ICACHE,
383d26
+    UNIFIED_CACHE
383d26
+};
383d26
+
383d26
+typedef struct CPUCacheInfo {
383d26
+    enum CacheType type;
383d26
+    uint8_t level;
383d26
+    /* Size in bytes */
383d26
+    uint32_t size;
383d26
+    /* Line size, in bytes */
383d26
+    uint16_t line_size;
383d26
+    /*
383d26
+     * Associativity.
383d26
+     * Note: representation of fully-associative caches is not implemented
383d26
+     */
383d26
+    uint8_t associativity;
383d26
+    /* Physical line partitions. CPUID[0x8000001D].EBX, CPUID[4].EBX */
383d26
+    uint8_t partitions;
383d26
+    /* Number of sets. CPUID[0x8000001D].ECX, CPUID[4].ECX */
383d26
+    uint32_t sets;
383d26
+    /*
383d26
+     * Lines per tag.
383d26
+     * AMD-specific: CPUID[0x80000005], CPUID[0x80000006].
383d26
+     * (Is this synonym to @partitions?)
383d26
+     */
383d26
+    uint8_t lines_per_tag;
383d26
+
383d26
+    /* Self-initializing cache */
383d26
+    bool self_init;
383d26
+    /*
383d26
+     * WBINVD/INVD is not guaranteed to act upon lower level caches of
383d26
+     * non-originating threads sharing this cache.
383d26
+     * CPUID[4].EDX[bit 0], CPUID[0x8000001D].EDX[bit 0]
383d26
+     */
383d26
+    bool no_invd_sharing;
383d26
+    /*
383d26
+     * Cache is inclusive of lower cache levels.
383d26
+     * CPUID[4].EDX[bit 1], CPUID[0x8000001D].EDX[bit 1].
383d26
+     */
383d26
+    bool inclusive;
383d26
+    /*
383d26
+     * A complex function is used to index the cache, potentially using all
383d26
+     * address bits.  CPUID[4].EDX[bit 2].
383d26
+     */
383d26
+    bool complex_indexing;
383d26
+} CPUCacheInfo;
383d26
+
383d26
+
383d26
+
383d26
 typedef struct CPUX86State {
383d26
     /* standard registers */
383d26
     target_ulong regs[CPU_NB_REGS];
383d26
-- 
383d26
1.8.3.1
383d26