ae23c9
From f5560b5492153802d046fab1d873970f57ebb42f Mon Sep 17 00:00:00 2001
ae23c9
From: Eduardo Habkost <ehabkost@redhat.com>
ae23c9
Date: Thu, 26 Jul 2018 17:19:00 +0100
ae23c9
Subject: [PATCH 10/14] i386: Add support for CPUID_8000_001E for AMD
ae23c9
ae23c9
RH-Author: Eduardo Habkost <ehabkost@redhat.com>
ae23c9
Message-id: <20180726171904.27418-8-ehabkost@redhat.com>
ae23c9
Patchwork-id: 81533
ae23c9
O-Subject: [qemu-kvm RHEL8/virt212 PATCH v2 07/11] i386: Add support for CPUID_8000_001E for AMD
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
From: Babu Moger <babu.moger@amd.com>
ae23c9
ae23c9
Add support for cpuid leaf CPUID_8000_001E. Build the config that closely
ae23c9
match the underlying hardware. Please refer to the Processor Programming
ae23c9
Reference (PPR) for AMD Family 17h Model for more details.
ae23c9
ae23c9
Signed-off-by: Babu Moger <babu.moger@amd.com>
ae23c9
Message-Id: <1528498581-131037-2-git-send-email-babu.moger@amd.com>
ae23c9
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
ae23c9
(cherry picked from commit ed78467a214595a63af7800a073a03ffe37cd7db)
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 | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
ae23c9
 1 file changed, 86 insertions(+)
ae23c9
ae23c9
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
ae23c9
index d2474d7..3ed1e47 100644
ae23c9
--- a/target/i386/cpu.c
ae23c9
+++ b/target/i386/cpu.c
ae23c9
@@ -429,6 +429,87 @@ static void encode_cache_cpuid8000001d(CPUCacheInfo *cache, CPUState *cs,
ae23c9
            (cache->complex_indexing ? CACHE_COMPLEX_IDX : 0);
ae23c9
 }
ae23c9
 
ae23c9
+/* Data structure to hold the configuration info for a given core index */
ae23c9
+struct core_topology {
ae23c9
+    /* core complex id of the current core index */
ae23c9
+    int ccx_id;
ae23c9
+    /*
ae23c9
+     * Adjusted core index for this core in the topology
ae23c9
+     * This can be 0,1,2,3 with max 4 cores in a core complex
ae23c9
+     */
ae23c9
+    int core_id;
ae23c9
+    /* Node id for this core index */
ae23c9
+    int node_id;
ae23c9
+    /* Number of nodes in this config */
ae23c9
+    int num_nodes;
ae23c9
+};
ae23c9
+
ae23c9
+/*
ae23c9
+ * Build the configuration closely match the EPYC hardware. Using the EPYC
ae23c9
+ * hardware configuration values (MAX_CCX, MAX_CORES_IN_CCX, MAX_CORES_IN_NODE)
ae23c9
+ * right now. This could change in future.
ae23c9
+ * nr_cores : Total number of cores in the config
ae23c9
+ * core_id  : Core index of the current CPU
ae23c9
+ * topo     : Data structure to hold all the config info for this core index
ae23c9
+ */
ae23c9
+static void build_core_topology(int nr_cores, int core_id,
ae23c9
+                                struct core_topology *topo)
ae23c9
+{
ae23c9
+    int nodes, cores_in_ccx;
ae23c9
+
ae23c9
+    /* First get the number of nodes required */
ae23c9
+    nodes = nodes_in_socket(nr_cores);
ae23c9
+
ae23c9
+    cores_in_ccx = cores_in_core_complex(nr_cores);
ae23c9
+
ae23c9
+    topo->node_id = core_id / (cores_in_ccx * MAX_CCX);
ae23c9
+    topo->ccx_id = (core_id % (cores_in_ccx * MAX_CCX)) / cores_in_ccx;
ae23c9
+    topo->core_id = core_id % cores_in_ccx;
ae23c9
+    topo->num_nodes = nodes;
ae23c9
+}
ae23c9
+
ae23c9
+/* Encode cache info for CPUID[8000001E] */
ae23c9
+static void encode_topo_cpuid8000001e(CPUState *cs, X86CPU *cpu,
ae23c9
+                                       uint32_t *eax, uint32_t *ebx,
ae23c9
+                                       uint32_t *ecx, uint32_t *edx)
ae23c9
+{
ae23c9
+    struct core_topology topo = {0};
ae23c9
+
ae23c9
+    build_core_topology(cs->nr_cores, cpu->core_id, &topo;;
ae23c9
+    *eax = cpu->apic_id;
ae23c9
+    /*
ae23c9
+     * CPUID_Fn8000001E_EBX
ae23c9
+     * 31:16 Reserved
ae23c9
+     * 15:8  Threads per core (The number of threads per core is
ae23c9
+     *       Threads per core + 1)
ae23c9
+     *  7:0  Core id (see bit decoding below)
ae23c9
+     *       SMT:
ae23c9
+     *           4:3 node id
ae23c9
+     *             2 Core complex id
ae23c9
+     *           1:0 Core id
ae23c9
+     *       Non SMT:
ae23c9
+     *           5:4 node id
ae23c9
+     *             3 Core complex id
ae23c9
+     *           1:0 Core id
ae23c9
+     */
ae23c9
+    if (cs->nr_threads - 1) {
ae23c9
+        *ebx = ((cs->nr_threads - 1) << 8) | (topo.node_id << 3) |
ae23c9
+                (topo.ccx_id << 2) | topo.core_id;
ae23c9
+    } else {
ae23c9
+        *ebx = (topo.node_id << 4) | (topo.ccx_id << 3) | topo.core_id;
ae23c9
+    }
ae23c9
+    /*
ae23c9
+     * CPUID_Fn8000001E_ECX
ae23c9
+     * 31:11 Reserved
ae23c9
+     * 10:8  Nodes per processor (Nodes per processor is number of nodes + 1)
ae23c9
+     *  7:0  Node id (see bit decoding below)
ae23c9
+     *         2  Socket id
ae23c9
+     *       1:0  Node id
ae23c9
+     */
ae23c9
+    *ecx = ((topo.num_nodes - 1) << 8) | (cpu->socket_id << 2) | topo.node_id;
ae23c9
+    *edx = 0;
ae23c9
+}
ae23c9
+
ae23c9
 /*
ae23c9
  * Definitions of the hardcoded cache entries we expose:
ae23c9
  * These are legacy cache values. If there is a need to change any
ae23c9
@@ -4105,6 +4186,11 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
ae23c9
             break;
ae23c9
         }
ae23c9
         break;
ae23c9
+    case 0x8000001E:
ae23c9
+        assert(cpu->core_id <= 255);
ae23c9
+        encode_topo_cpuid8000001e(cs, cpu,
ae23c9
+                                  eax, ebx, ecx, edx);
ae23c9
+        break;
ae23c9
     case 0xC0000000:
ae23c9
         *eax = env->cpuid_xlevel2;
ae23c9
         *ebx = 0;
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9