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

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