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

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