Blame SOURCES/kvm-i386-Populate-AMD-Processor-Cache-Information-for-cp.patch

357786
From a24dd4151d27389947c011293d6775327268b1c2 Mon Sep 17 00:00:00 2001
357786
From: Eduardo Habkost <ehabkost@redhat.com>
357786
Date: Tue, 3 Jul 2018 17:23:52 +0200
357786
Subject: [PATCH 07/89] i386: Populate AMD Processor Cache Information for
357786
 cpuid 0x8000001D
357786
357786
RH-Author: Eduardo Habkost <ehabkost@redhat.com>
357786
Message-id: <20180703172356.21038-7-ehabkost@redhat.com>
357786
Patchwork-id: 81216
357786
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH v3 06/10] i386: Populate AMD Processor Cache Information for cpuid 0x8000001D
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
Add information for cpuid 0x8000001D leaf. Populate cache topology information
357786
for different cache types (Data Cache, Instruction Cache, L2 and L3) supported
357786
by 0x8000001D leaf. Please refer to the Processor Programming Reference (PPR)
357786
for AMD Family 17h Model for more details.
357786
357786
Signed-off-by: Babu Moger <babu.moger@amd.com>
357786
Message-Id: <1527176614-26271-3-git-send-email-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 8f4202fb1080f86958782b1fca0bf0279f67d136)
357786
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 target/i386/cpu.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
357786
 target/i386/kvm.c |  29 ++++++++++++--
357786
 2 files changed, 143 insertions(+), 3 deletions(-)
357786
357786
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
357786
index 41d0b72..57f74c6 100644
357786
--- a/target/i386/cpu.c
357786
+++ b/target/i386/cpu.c
357786
@@ -337,6 +337,99 @@ static void encode_cache_cpuid80000006(CPUCacheInfo *l2,
357786
 }
357786
 
357786
 /*
357786
+ * Definitions used for building CPUID Leaf 0x8000001D and 0x8000001E
357786
+ * Please refer to the AMD64 Architecture Programmer’s Manual Volume 3.
357786
+ * Define the constants to build the cpu topology. Right now, TOPOEXT
357786
+ * feature is enabled only on EPYC. So, these constants are based on
357786
+ * EPYC supported configurations. We may need to handle the cases if
357786
+ * these values change in future.
357786
+ */
357786
+/* Maximum core complexes in a node */
357786
+#define MAX_CCX 2
357786
+/* Maximum cores in a core complex */
357786
+#define MAX_CORES_IN_CCX 4
357786
+/* Maximum cores in a node */
357786
+#define MAX_CORES_IN_NODE 8
357786
+/* Maximum nodes in a socket */
357786
+#define MAX_NODES_PER_SOCKET 4
357786
+
357786
+/*
357786
+ * Figure out the number of nodes required to build this config.
357786
+ * Max cores in a node is 8
357786
+ */
357786
+static int nodes_in_socket(int nr_cores)
357786
+{
357786
+    int nodes;
357786
+
357786
+    nodes = DIV_ROUND_UP(nr_cores, MAX_CORES_IN_NODE);
357786
+
357786
+   /* Hardware does not support config with 3 nodes, return 4 in that case */
357786
+    return (nodes == 3) ? 4 : nodes;
357786
+}
357786
+
357786
+/*
357786
+ * Decide the number of cores in a core complex with the given nr_cores using
357786
+ * following set constants MAX_CCX, MAX_CORES_IN_CCX, MAX_CORES_IN_NODE and
357786
+ * MAX_NODES_PER_SOCKET. Maintain symmetry as much as possible
357786
+ * L3 cache is shared across all cores in a core complex. So, this will also
357786
+ * tell us how many cores are sharing the L3 cache.
357786
+ */
357786
+static int cores_in_core_complex(int nr_cores)
357786
+{
357786
+    int nodes;
357786
+
357786
+    /* Check if we can fit all the cores in one core complex */
357786
+    if (nr_cores <= MAX_CORES_IN_CCX) {
357786
+        return nr_cores;
357786
+    }
357786
+    /* Get the number of nodes required to build this config */
357786
+    nodes = nodes_in_socket(nr_cores);
357786
+
357786
+    /*
357786
+     * Divide the cores accros all the core complexes
357786
+     * Return rounded up value
357786
+     */
357786
+    return DIV_ROUND_UP(nr_cores, nodes * MAX_CCX);
357786
+}
357786
+
357786
+/* Encode cache info for CPUID[8000001D] */
357786
+static void encode_cache_cpuid8000001d(CPUCacheInfo *cache, CPUState *cs,
357786
+                                uint32_t *eax, uint32_t *ebx,
357786
+                                uint32_t *ecx, uint32_t *edx)
357786
+{
357786
+    uint32_t l3_cores;
357786
+    assert(cache->size == cache->line_size * cache->associativity *
357786
+                          cache->partitions * cache->sets);
357786
+
357786
+    *eax = CACHE_TYPE(cache->type) | CACHE_LEVEL(cache->level) |
357786
+               (cache->self_init ? CACHE_SELF_INIT_LEVEL : 0);
357786
+
357786
+    /* L3 is shared among multiple cores */
357786
+    if (cache->level == 3) {
357786
+        l3_cores = cores_in_core_complex(cs->nr_cores);
357786
+        *eax |= ((l3_cores * cs->nr_threads) - 1) << 14;
357786
+    } else {
357786
+        *eax |= ((cs->nr_threads - 1) << 14);
357786
+    }
357786
+
357786
+    assert(cache->line_size > 0);
357786
+    assert(cache->partitions > 0);
357786
+    assert(cache->associativity > 0);
357786
+    /* We don't implement fully-associative caches */
357786
+    assert(cache->associativity < cache->sets);
357786
+    *ebx = (cache->line_size - 1) |
357786
+           ((cache->partitions - 1) << 12) |
357786
+           ((cache->associativity - 1) << 22);
357786
+
357786
+    assert(cache->sets > 0);
357786
+    *ecx = cache->sets - 1;
357786
+
357786
+    *edx = (cache->no_invd_sharing ? CACHE_NO_INVD_SHARING : 0) |
357786
+           (cache->inclusive ? CACHE_INCLUSIVE : 0) |
357786
+           (cache->complex_indexing ? CACHE_COMPLEX_IDX : 0);
357786
+}
357786
+
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
@@ -3988,6 +4081,30 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
357786
             *edx = 0;
357786
         }
357786
         break;
357786
+    case 0x8000001D:
357786
+        *eax = 0;
357786
+        switch (count) {
357786
+        case 0: /* L1 dcache info */
357786
+            encode_cache_cpuid8000001d(env->cache_info_amd.l1d_cache, cs,
357786
+                                       eax, ebx, ecx, edx);
357786
+            break;
357786
+        case 1: /* L1 icache info */
357786
+            encode_cache_cpuid8000001d(env->cache_info_amd.l1i_cache, cs,
357786
+                                       eax, ebx, ecx, edx);
357786
+            break;
357786
+        case 2: /* L2 cache info */
357786
+            encode_cache_cpuid8000001d(env->cache_info_amd.l2_cache, cs,
357786
+                                       eax, ebx, ecx, edx);
357786
+            break;
357786
+        case 3: /* L3 cache info */
357786
+            encode_cache_cpuid8000001d(env->cache_info_amd.l3_cache, cs,
357786
+                                       eax, ebx, ecx, edx);
357786
+            break;
357786
+        default: /* end of info */
357786
+            *eax = *ebx = *ecx = *edx = 0;
357786
+            break;
357786
+        }
357786
+        break;
357786
     case 0xC0000000:
357786
         *eax = env->cpuid_xlevel2;
357786
         *ebx = 0;
357786
diff --git a/target/i386/kvm.c b/target/i386/kvm.c
357786
index 19e6aa3..447215b 100644
357786
--- a/target/i386/kvm.c
357786
+++ b/target/i386/kvm.c
357786
@@ -968,9 +968,32 @@ int kvm_arch_init_vcpu(CPUState *cs)
357786
         }
357786
         c = &cpuid_data.entries[cpuid_i++];
357786
 
357786
-        c->function = i;
357786
-        c->flags = 0;
357786
-        cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
357786
+        switch (i) {
357786
+        case 0x8000001d:
357786
+            /* Query for all AMD cache information leaves */
357786
+            for (j = 0; ; j++) {
357786
+                c->function = i;
357786
+                c->flags = KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
357786
+                c->index = j;
357786
+                cpu_x86_cpuid(env, i, j, &c->eax, &c->ebx, &c->ecx, &c->edx);
357786
+
357786
+                if (c->eax == 0) {
357786
+                    break;
357786
+                }
357786
+                if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {
357786
+                    fprintf(stderr, "cpuid_data is full, no space for "
357786
+                            "cpuid(eax:0x%x,ecx:0x%x)\n", i, j);
357786
+                    abort();
357786
+                }
357786
+                c = &cpuid_data.entries[cpuid_i++];
357786
+            }
357786
+            break;
357786
+        default:
357786
+            c->function = i;
357786
+            c->flags = 0;
357786
+            cpu_x86_cpuid(env, i, 0, &c->eax, &c->ebx, &c->ecx, &c->edx);
357786
+            break;
357786
+        }
357786
     }
357786
 
357786
     /* Call Centaur's CPUID instructions they are supported. */
357786
-- 
357786
1.8.3.1
357786