yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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