Blame SOURCES/kvm-i386-Add-new-property-to-control-cache-info.patch

357786
From 64b860ac7db707ef2a29d957b794c831637315a6 Mon Sep 17 00:00:00 2001
357786
From: Eduardo Habkost <ehabkost@redhat.com>
357786
Date: Tue, 3 Jul 2018 17:23:50 +0200
357786
Subject: [PATCH 05/89] i386: Add new property to control cache info
357786
357786
RH-Author: Eduardo Habkost <ehabkost@redhat.com>
357786
Message-id: <20180703172356.21038-5-ehabkost@redhat.com>
357786
Patchwork-id: 81212
357786
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH v3 04/10] i386: Add new property to control cache info
357786
Bugzilla: 1481253
357786
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
357786
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
357786
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
357786
From: Babu Moger <babu.moger@amd.com>
357786
357786
The property legacy-cache will be used to control the cache information.
357786
If user passes "-cpu legacy-cache" then older information will
357786
be displayed even if the hardware supports new information. Otherwise
357786
use the statically loaded cache definitions if available.
357786
357786
Renamed the previous cache structures to legacy_*. If there is any change in
357786
the cache information, then it needs to be initialized in builtin_x86_defs.
357786
357786
Signed-off-by: Babu Moger <babu.moger@amd.com>
357786
Tested-by: Geoffrey McRae <geoff@hostfission.com>
357786
Message-Id: <20180514164156.27034-3-babu.moger@amd.com>
357786
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
357786
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
357786
(cherry picked from commit ab8f992e3e63e91be257e4e343d386dae7be4bcb)
357786
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 include/hw/i386/pc.h |  4 +++
357786
 target/i386/cpu.c    | 97 ++++++++++++++++++++++++++++++++++++++--------------
357786
 target/i386/cpu.h    |  5 +++
357786
 3 files changed, 80 insertions(+), 26 deletions(-)
357786
357786
diff --git a/include/hw/i386/pc.h b/include/hw/i386/pc.h
357786
index d5a0827..3ff55c6 100644
357786
--- a/include/hw/i386/pc.h
357786
+++ b/include/hw/i386/pc.h
357786
@@ -979,6 +979,10 @@ extern void igd_passthrough_isa_bridge_create(PCIBus *bus, uint16_t gpu_dev_id);
357786
             .driver   = "Skylake-Server" "-" TYPE_X86_CPU,\
357786
             .property = "clflushopt",\
357786
             .value    = "off",\
357786
+        },{ /* PC_RHEL7_5_COMPAT from PC_COMPAT_2_12 */ \
357786
+            .driver   = TYPE_X86_CPU,\
357786
+            .property = "legacy-cache",\
357786
+            .value    = "on",\
357786
         },
357786
 
357786
 
357786
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
357786
index 23eb47d..3426130 100644
357786
--- a/target/i386/cpu.c
357786
+++ b/target/i386/cpu.c
357786
@@ -336,10 +336,14 @@ static void encode_cache_cpuid80000006(CPUCacheInfo *l2,
357786
     }
357786
 }
357786
 
357786
-/* Definitions of the hardcoded cache entries we expose: */
357786
+/*
357786
+ * Definitions of the hardcoded cache entries we expose:
357786
+ * These are legacy cache values. If there is a need to change any
357786
+ * of these values please use builtin_x86_defs
357786
+ */
357786
 
357786
 /* L1 data cache: */
357786
-static CPUCacheInfo l1d_cache = {
357786
+static CPUCacheInfo legacy_l1d_cache = {
357786
     .type = DCACHE,
357786
     .level = 1,
357786
     .size = 32 * KiB,
357786
@@ -352,7 +356,7 @@ static CPUCacheInfo l1d_cache = {
357786
 };
357786
 
357786
 /*FIXME: CPUID leaf 0x80000005 is inconsistent with leaves 2 & 4 */
357786
-static CPUCacheInfo l1d_cache_amd = {
357786
+static CPUCacheInfo legacy_l1d_cache_amd = {
357786
     .type = DCACHE,
357786
     .level = 1,
357786
     .size = 64 * KiB,
357786
@@ -366,7 +370,7 @@ static CPUCacheInfo l1d_cache_amd = {
357786
 };
357786
 
357786
 /* L1 instruction cache: */
357786
-static CPUCacheInfo l1i_cache = {
357786
+static CPUCacheInfo legacy_l1i_cache = {
357786
     .type = ICACHE,
357786
     .level = 1,
357786
     .size = 32 * KiB,
357786
@@ -379,7 +383,7 @@ static CPUCacheInfo l1i_cache = {
357786
 };
357786
 
357786
 /*FIXME: CPUID leaf 0x80000005 is inconsistent with leaves 2 & 4 */
357786
-static CPUCacheInfo l1i_cache_amd = {
357786
+static CPUCacheInfo legacy_l1i_cache_amd = {
357786
     .type = ICACHE,
357786
     .level = 1,
357786
     .size = 64 * KiB,
357786
@@ -393,7 +397,7 @@ static CPUCacheInfo l1i_cache_amd = {
357786
 };
357786
 
357786
 /* Level 2 unified cache: */
357786
-static CPUCacheInfo l2_cache = {
357786
+static CPUCacheInfo legacy_l2_cache = {
357786
     .type = UNIFIED_CACHE,
357786
     .level = 2,
357786
     .size = 4 * MiB,
357786
@@ -406,7 +410,7 @@ static CPUCacheInfo l2_cache = {
357786
 };
357786
 
357786
 /*FIXME: CPUID leaf 2 descriptor is inconsistent with CPUID leaf 4 */
357786
-static CPUCacheInfo l2_cache_cpuid2 = {
357786
+static CPUCacheInfo legacy_l2_cache_cpuid2 = {
357786
     .type = UNIFIED_CACHE,
357786
     .level = 2,
357786
     .size = 2 * MiB,
357786
@@ -416,7 +420,7 @@ static CPUCacheInfo l2_cache_cpuid2 = {
357786
 
357786
 
357786
 /*FIXME: CPUID leaf 0x80000006 is inconsistent with leaves 2 & 4 */
357786
-static CPUCacheInfo l2_cache_amd = {
357786
+static CPUCacheInfo legacy_l2_cache_amd = {
357786
     .type = UNIFIED_CACHE,
357786
     .level = 2,
357786
     .size = 512 * KiB,
357786
@@ -428,7 +432,7 @@ static CPUCacheInfo l2_cache_amd = {
357786
 };
357786
 
357786
 /* Level 3 unified cache: */
357786
-static CPUCacheInfo l3_cache = {
357786
+static CPUCacheInfo legacy_l3_cache = {
357786
     .type = UNIFIED_CACHE,
357786
     .level = 3,
357786
     .size = 16 * MiB,
357786
@@ -3321,6 +3325,10 @@ static void x86_cpu_load_def(X86CPU *cpu, X86CPUDefinition *def, Error **errp)
357786
         env->features[w] = def->features[w];
357786
     }
357786
 
357786
+    /* Store Cache information from the X86CPUDefinition if available */
357786
+    env->cache_info = def->cache_info;
357786
+    cpu->legacy_cache = def->cache_info ? 0 : 1;
357786
+
357786
     /* Special cases not set in the X86CPUDefinition structs: */
357786
     /* TODO: in-kernel irqchip for hvf */
357786
     if (kvm_enabled()) {
357786
@@ -3670,11 +3678,21 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
357786
         if (!cpu->enable_l3_cache) {
357786
             *ecx = 0;
357786
         } else {
357786
-            *ecx = cpuid2_cache_descriptor(&l3_cache);
357786
+            if (env->cache_info && !cpu->legacy_cache) {
357786
+                *ecx = cpuid2_cache_descriptor(&env->cache_info->l3_cache);
357786
+            } else {
357786
+                *ecx = cpuid2_cache_descriptor(&legacy_l3_cache);
357786
+            }
357786
+        }
357786
+        if (env->cache_info && !cpu->legacy_cache) {
357786
+            *edx = (cpuid2_cache_descriptor(&env->cache_info->l1d_cache) << 16) |
357786
+                   (cpuid2_cache_descriptor(&env->cache_info->l1i_cache) <<  8) |
357786
+                   (cpuid2_cache_descriptor(&env->cache_info->l2_cache));
357786
+        } else {
357786
+            *edx = (cpuid2_cache_descriptor(&legacy_l1d_cache) << 16) |
357786
+                   (cpuid2_cache_descriptor(&legacy_l1i_cache) <<  8) |
357786
+                   (cpuid2_cache_descriptor(&legacy_l2_cache_cpuid2));
357786
         }
357786
-        *edx = (cpuid2_cache_descriptor(&l1d_cache) << 16) |
357786
-               (cpuid2_cache_descriptor(&l1i_cache) <<  8) |
357786
-               (cpuid2_cache_descriptor(&l2_cache_cpuid2));
357786
         break;
357786
     case 4:
357786
         /* cache info: needed for Core compatibility */
357786
@@ -3687,27 +3705,35 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
357786
             }
357786
         } else {
357786
             *eax = 0;
357786
+            CPUCacheInfo *l1d, *l1i, *l2, *l3;
357786
+            if (env->cache_info && !cpu->legacy_cache) {
357786
+                l1d = &env->cache_info->l1d_cache;
357786
+                l1i = &env->cache_info->l1i_cache;
357786
+                l2 = &env->cache_info->l2_cache;
357786
+                l3 = &env->cache_info->l3_cache;
357786
+            } else {
357786
+                l1d = &legacy_l1d_cache;
357786
+                l1i = &legacy_l1i_cache;
357786
+                l2 = &legacy_l2_cache;
357786
+                l3 = &legacy_l3_cache;
357786
+            }
357786
             switch (count) {
357786
             case 0: /* L1 dcache info */
357786
-                encode_cache_cpuid4(&l1d_cache,
357786
-                                    1, cs->nr_cores,
357786
+                encode_cache_cpuid4(l1d, 1, cs->nr_cores,
357786
                                     eax, ebx, ecx, edx);
357786
                 break;
357786
             case 1: /* L1 icache info */
357786
-                encode_cache_cpuid4(&l1i_cache,
357786
-                                    1, cs->nr_cores,
357786
+                encode_cache_cpuid4(l1i, 1, cs->nr_cores,
357786
                                     eax, ebx, ecx, edx);
357786
                 break;
357786
             case 2: /* L2 cache info */
357786
-                encode_cache_cpuid4(&l2_cache,
357786
-                                    cs->nr_threads, cs->nr_cores,
357786
+                encode_cache_cpuid4(l2, cs->nr_threads, cs->nr_cores,
357786
                                     eax, ebx, ecx, edx);
357786
                 break;
357786
             case 3: /* L3 cache info */
357786
                 pkg_offset = apicid_pkg_offset(cs->nr_cores, cs->nr_threads);
357786
                 if (cpu->enable_l3_cache) {
357786
-                    encode_cache_cpuid4(&l3_cache,
357786
-                                        (1 << pkg_offset), cs->nr_cores,
357786
+                    encode_cache_cpuid4(l3, (1 << pkg_offset), cs->nr_cores,
357786
                                         eax, ebx, ecx, edx);
357786
                     break;
357786
                 }
357786
@@ -3920,8 +3946,13 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
357786
                (L1_ITLB_2M_ASSOC <<  8) | (L1_ITLB_2M_ENTRIES);
357786
         *ebx = (L1_DTLB_4K_ASSOC << 24) | (L1_DTLB_4K_ENTRIES << 16) | \
357786
                (L1_ITLB_4K_ASSOC <<  8) | (L1_ITLB_4K_ENTRIES);
357786
-        *ecx = encode_cache_cpuid80000005(&l1d_cache_amd);
357786
-        *edx = encode_cache_cpuid80000005(&l1i_cache_amd);
357786
+        if (env->cache_info && !cpu->legacy_cache) {
357786
+            *ecx = encode_cache_cpuid80000005(&env->cache_info->l1d_cache);
357786
+            *edx = encode_cache_cpuid80000005(&env->cache_info->l1i_cache);
357786
+        } else {
357786
+            *ecx = encode_cache_cpuid80000005(&legacy_l1d_cache_amd);
357786
+            *edx = encode_cache_cpuid80000005(&legacy_l1i_cache_amd);
357786
+        }
357786
         break;
357786
     case 0x80000006:
357786
         /* cache info (L2 cache) */
357786
@@ -3937,9 +3968,17 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
357786
                (L2_DTLB_4K_ENTRIES << 16) | \
357786
                (AMD_ENC_ASSOC(L2_ITLB_4K_ASSOC) << 12) | \
357786
                (L2_ITLB_4K_ENTRIES);
357786
-        encode_cache_cpuid80000006(&l2_cache_amd,
357786
-                                   cpu->enable_l3_cache ? &l3_cache : NULL,
357786
-                                   ecx, edx);
357786
+        if (env->cache_info && !cpu->legacy_cache) {
357786
+            encode_cache_cpuid80000006(&env->cache_info->l2_cache,
357786
+                                       cpu->enable_l3_cache ?
357786
+                                       &env->cache_info->l3_cache : NULL,
357786
+                                       ecx, edx);
357786
+        } else {
357786
+            encode_cache_cpuid80000006(&legacy_l2_cache_amd,
357786
+                                       cpu->enable_l3_cache ?
357786
+                                       &legacy_l3_cache : NULL,
357786
+                                       ecx, edx);
357786
+        }
357786
         break;
357786
     case 0x80000007:
357786
         *eax = 0;
357786
@@ -5119,6 +5158,12 @@ static Property x86_cpu_properties[] = {
357786
                      false),
357786
     DEFINE_PROP_BOOL("vmware-cpuid-freq", X86CPU, vmware_cpuid_freq, true),
357786
     DEFINE_PROP_BOOL("tcg-cpuid", X86CPU, expose_tcg, true),
357786
+    /*
357786
+     * lecacy_cache defaults to CPU model being chosen. This is set in
357786
+     * x86_cpu_load_def based on cache_info which is initialized in
357786
+     * builtin_x86_defs
357786
+     */
357786
+    DEFINE_PROP_BOOL("legacy-cache", X86CPU, legacy_cache, false),
357786
 
357786
     /*
357786
      * From "Requirements for Implementing the Microsoft
357786
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
357786
index 912aa34..b01b0c1 100644
357786
--- a/target/i386/cpu.h
357786
+++ b/target/i386/cpu.h
357786
@@ -1397,6 +1397,11 @@ struct X86CPU {
357786
      */
357786
     bool enable_l3_cache;
357786
 
357786
+    /* Compatibility bits for old machine types.
357786
+     * If true present the old cache topology information
357786
+     */
357786
+    bool legacy_cache;
357786
+
357786
     /* Compatibility bits for old machine types: */
357786
     bool enable_cpuid_0xb;
357786
 
357786
-- 
357786
1.8.3.1
357786