diff mbox series

[RFC,v2,08/12] hw/i386: Use get_max_topo_by_level() to get topology information

Message ID 20240919061128.769139-9-zhao1.liu@intel.com (mailing list archive)
State New
Headers show
Series Introduce Hybrid CPU Topology via Custom Topology Tree | expand

Commit Message

Zhao Liu Sept. 19, 2024, 6:11 a.m. UTC
To honor the custom topology case and generate correct APIC ID for
hybrid CPU topology, Use get_max_topo_by_level() to get topology
information instead of accessing MachineState.smp directly.

Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
 hw/i386/x86-common.c | 19 +++++++++++++------
 hw/i386/x86.c        | 20 +++++++++++++-------
 2 files changed, 26 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/hw/i386/x86-common.c b/hw/i386/x86-common.c
index 75d4b2f3d43a..58591e015569 100644
--- a/hw/i386/x86-common.c
+++ b/hw/i386/x86-common.c
@@ -202,11 +202,15 @@  void x86_cpus_init(X86MachineState *x86ms, int default_cpu_version)
 
 static void x86_fixup_topo_ids(MachineState *ms, X86CPU *cpu)
 {
+    int max_modules, max_dies;
+
+    max_modules = get_max_topo_by_level(ms, CPU_TOPOLOGY_LEVEL_MODULE);
+    max_dies = get_max_topo_by_level(ms, CPU_TOPOLOGY_LEVEL_DIE);
     /*
      * die-id was optional in QEMU 4.0 and older, so keep it optional
      * if there's only one die per socket.
      */
-    if (cpu->module_id < 0 && ms->smp.modules == 1) {
+    if (cpu->module_id < 0 && max_modules == 1) {
         cpu->module_id = 0;
     }
 
@@ -214,7 +218,7 @@  static void x86_fixup_topo_ids(MachineState *ms, X86CPU *cpu)
      * module-id was optional in QEMU 9.0 and older, so keep it optional
      * if there's only one module per die.
      */
-    if (cpu->die_id < 0 && ms->smp.dies == 1) {
+    if (cpu->die_id < 0 && max_dies == 1) {
         cpu->die_id = 0;
     }
 }
@@ -393,6 +397,7 @@  void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,
     MachineState *ms = MACHINE(hotplug_dev);
     X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
     X86CPUTopoInfo topo_info;
+    int max_modules, max_dies;
 
     if (!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) {
         error_setg(errp, "Invalid CPU type, expected cpu type: '%s'",
@@ -413,13 +418,15 @@  void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,
 
     init_topo_info(&topo_info, x86ms);
 
-    if (ms->smp.modules > 1) {
-        env->nr_modules = ms->smp.modules;
+    max_modules = get_max_topo_by_level(ms, CPU_TOPOLOGY_LEVEL_MODULE);
+    if (max_modules > 1) {
+        env->nr_modules = max_modules;
         set_bit(CPU_TOPOLOGY_LEVEL_MODULE, env->avail_cpu_topo);
     }
 
-    if (ms->smp.dies > 1) {
-        env->nr_dies = ms->smp.dies;
+    max_dies = get_max_topo_by_level(ms, CPU_TOPOLOGY_LEVEL_DIE);
+    if (max_dies > 1) {
+        env->nr_dies = max_dies;
         set_bit(CPU_TOPOLOGY_LEVEL_DIE, env->avail_cpu_topo);
     }
 
diff --git a/hw/i386/x86.c b/hw/i386/x86.c
index cdf7b81ad0e3..55904b545d84 100644
--- a/hw/i386/x86.c
+++ b/hw/i386/x86.c
@@ -44,16 +44,20 @@  void init_topo_info(X86CPUTopoInfo *topo_info,
 {
     MachineState *ms = MACHINE(x86ms);
 
-    topo_info->dies_per_pkg = ms->smp.dies;
+    topo_info->dies_per_pkg =
+        get_max_topo_by_level(ms, CPU_TOPOLOGY_LEVEL_DIE);
     /*
      * Though smp.modules means the number of modules in one cluster,
      * i386 doesn't support cluster level so that the smp.clusters
      * always defaults to 1, therefore using smp.modules directly is
      * fine here.
      */
-    topo_info->modules_per_die = ms->smp.modules;
-    topo_info->cores_per_module = ms->smp.cores;
-    topo_info->threads_per_core = ms->smp.threads;
+    topo_info->modules_per_die =
+        get_max_topo_by_level(ms, CPU_TOPOLOGY_LEVEL_MODULE);
+    topo_info->cores_per_module =
+        get_max_topo_by_level(ms, CPU_TOPOLOGY_LEVEL_CORE);
+    topo_info->threads_per_core =
+        get_max_topo_by_level(ms, CPU_TOPOLOGY_LEVEL_THREAD);
 }
 
 /*
@@ -103,7 +107,7 @@  static const CPUArchIdList *x86_possible_cpu_arch_ids(MachineState *ms)
     X86MachineState *x86ms = X86_MACHINE(ms);
     unsigned int max_cpus = ms->smp.max_cpus;
     X86CPUTopoInfo topo_info;
-    int i;
+    int i, max_dies, max_modules;
 
     if (ms->possible_cpus) {
         /*
@@ -120,6 +124,8 @@  static const CPUArchIdList *x86_possible_cpu_arch_ids(MachineState *ms)
 
     init_topo_info(&topo_info, x86ms);
 
+    max_dies = get_max_topo_by_level(ms, CPU_TOPOLOGY_LEVEL_DIE);
+    max_modules = get_max_topo_by_level(ms, CPU_TOPOLOGY_LEVEL_MODULE);
     for (i = 0; i < ms->possible_cpus->len; i++) {
         X86CPUTopoIDs topo_ids;
 
@@ -131,11 +137,11 @@  static const CPUArchIdList *x86_possible_cpu_arch_ids(MachineState *ms)
                                  &topo_info, &topo_ids);
         ms->possible_cpus->cpus[i].props.has_socket_id = true;
         ms->possible_cpus->cpus[i].props.socket_id = topo_ids.pkg_id;
-        if (ms->smp.dies > 1) {
+        if (max_dies > 1) {
             ms->possible_cpus->cpus[i].props.has_die_id = true;
             ms->possible_cpus->cpus[i].props.die_id = topo_ids.die_id;
         }
-        if (ms->smp.modules > 1) {
+        if (max_modules > 1) {
             ms->possible_cpus->cpus[i].props.has_module_id = true;
             ms->possible_cpus->cpus[i].props.module_id = topo_ids.module_id;
         }