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