diff mbox series

[RFC,v2,15/15] i386: Support topology device tree

Message ID 20240919015533.766754-16-zhao1.liu@intel.com (mailing list archive)
State New
Headers show
Series qom-topo: Abstract CPU Topology Level to Topology Device | expand

Commit Message

Zhao Liu Sept. 19, 2024, 1:55 a.m. UTC
Support complete QOM CPu topology tree for x86 machine, and specify
bus_type for x86 CPU so that all x86 CPUs will be added in the topology
tree.

Since the CPU slot make the machine as the hotplug handler for all
topology devices, hotplug related hooks may used to handle other
topology devices besides the CPU. Thus, make microvm not assume that
the device is only a CPU when implementing the relevant hooks.

Additionally, drop code paths that are not needed by the topology tree
implementation.

Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
 hw/i386/microvm.c    | 13 +++++---
 hw/i386/x86-common.c | 78 +++++---------------------------------------
 hw/i386/x86.c        |  2 ++
 target/i386/cpu.c    |  2 ++
 4 files changed, 21 insertions(+), 74 deletions(-)
diff mbox series

Patch

diff --git a/hw/i386/microvm.c b/hw/i386/microvm.c
index 40edcee7af29..49a897db50fc 100644
--- a/hw/i386/microvm.c
+++ b/hw/i386/microvm.c
@@ -417,16 +417,21 @@  static void microvm_fix_kernel_cmdline(MachineState *machine)
 static void microvm_device_pre_plug_cb(HotplugHandler *hotplug_dev,
                                        DeviceState *dev, Error **errp)
 {
-    X86CPU *cpu = X86_CPU(dev);
+    if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
+        X86CPU *cpu;
+        cpu = X86_CPU(dev);
 
-    cpu->host_phys_bits = true; /* need reliable phys-bits */
-    x86_cpu_pre_plug(hotplug_dev, dev, errp);
+        cpu->host_phys_bits = true; /* need reliable phys-bits */
+        x86_cpu_pre_plug(hotplug_dev, dev, errp);
+    }
 }
 
 static void microvm_device_plug_cb(HotplugHandler *hotplug_dev,
                                    DeviceState *dev, Error **errp)
 {
-    x86_cpu_plug(hotplug_dev, dev, errp);
+    if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
+        x86_cpu_plug(hotplug_dev, dev, errp);
+    }
 }
 
 static void microvm_device_unplug_request_cb(HotplugHandler *hotplug_dev,
diff --git a/hw/i386/x86-common.c b/hw/i386/x86-common.c
index d837aadc9dea..75d4b2f3d43a 100644
--- a/hw/i386/x86-common.c
+++ b/hw/i386/x86-common.c
@@ -129,26 +129,18 @@  static void x86_cpu_new(X86MachineState *x86ms, int index,
                         int64_t apic_id, Error **errp)
 {
     MachineState *ms = MACHINE(x86ms);
-    MachineClass *mc = MACHINE_GET_CLASS(ms);
     Object *cpu = object_new(ms->cpu_type);
     DeviceState *dev = DEVICE(cpu);
     BusState *bus = NULL;
+    X86CPUTopoIDs topo_ids;
+    X86CPUTopoInfo topo_info;
 
-    /*
-     * Once x86 machine supports topo_tree_supported, x86 CPU would
-     * also have bus_type.
-     */
-    if (mc->smp_props.topo_tree_supported) {
-        X86CPUTopoIDs topo_ids;
-        X86CPUTopoInfo topo_info;
-
-        init_topo_info(&topo_info, x86ms);
-        x86_topo_ids_from_apicid(apic_id, &topo_info, &topo_ids);
-        bus = x86_find_topo_bus(ms, &topo_ids);
+    init_topo_info(&topo_info, x86ms);
+    x86_topo_ids_from_apicid(apic_id, &topo_info, &topo_ids);
+    bus = x86_find_topo_bus(ms, &topo_ids);
 
-        /* Only with dev->id, CPU can be inserted into topology tree. */
-        dev->id = g_strdup_printf("%s[%d]", ms->cpu_type, index);
-    }
+    /* Only with dev->id, CPU can be inserted into topology tree. */
+    dev->id = g_strdup_printf("%s[%d]", ms->cpu_type, index);
 
     if (!object_property_set_uint(cpu, "apic-id", apic_id, errp)) {
         goto out;
@@ -399,10 +391,7 @@  void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,
     X86CPU *cpu = X86_CPU(dev);
     CPUX86State *env = &cpu->env;
     MachineState *ms = MACHINE(hotplug_dev);
-    MachineClass *mc = MACHINE_GET_CLASS(ms);
     X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
-    unsigned int smp_cores = ms->smp.cores;
-    unsigned int smp_threads = ms->smp.threads;
     X86CPUTopoInfo topo_info;
 
     if (!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) {
@@ -434,58 +423,7 @@  void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,
         set_bit(CPU_TOPOLOGY_LEVEL_DIE, env->avail_cpu_topo);
     }
 
-    if (cpu->apic_id == UNASSIGNED_APIC_ID &&
-        !mc->smp_props.topo_tree_supported) {
-        x86_fixup_topo_ids(ms, cpu);
-
-        if (cpu->socket_id < 0) {
-            error_setg(errp, "CPU socket-id is not set");
-            return;
-        } else if (cpu->socket_id > ms->smp.sockets - 1) {
-            error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u",
-                       cpu->socket_id, ms->smp.sockets - 1);
-            return;
-        }
-        if (cpu->die_id < 0) {
-            error_setg(errp, "CPU die-id is not set");
-            return;
-        } else if (cpu->die_id > ms->smp.dies - 1) {
-            error_setg(errp, "Invalid CPU die-id: %u must be in range 0:%u",
-                       cpu->die_id, ms->smp.dies - 1);
-        }
-        if (cpu->module_id < 0) {
-            error_setg(errp, "CPU module-id is not set");
-            return;
-        } else if (cpu->module_id > ms->smp.modules - 1) {
-            error_setg(errp, "Invalid CPU module-id: %u must be in range 0:%u",
-                       cpu->module_id, ms->smp.modules - 1);
-            return;
-        }
-        if (cpu->core_id < 0) {
-            error_setg(errp, "CPU core-id is not set");
-            return;
-        } else if (cpu->core_id > (smp_cores - 1)) {
-            error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u",
-                       cpu->core_id, smp_cores - 1);
-            return;
-        }
-        if (cpu->thread_id < 0) {
-            error_setg(errp, "CPU thread-id is not set");
-            return;
-        } else if (cpu->thread_id > (smp_threads - 1)) {
-            error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u",
-                       cpu->thread_id, smp_threads - 1);
-            return;
-        }
-
-        topo_ids.pkg_id = cpu->socket_id;
-        topo_ids.die_id = cpu->die_id;
-        topo_ids.module_id = cpu->module_id;
-        topo_ids.core_id = cpu->core_id;
-        topo_ids.smt_id = cpu->thread_id;
-        cpu->apic_id = x86_apicid_from_topo_ids(&topo_info, &topo_ids);
-    } else if (cpu->apic_id == UNASSIGNED_APIC_ID &&
-               mc->smp_props.topo_tree_supported) {
+    if (cpu->apic_id == UNASSIGNED_APIC_ID) {
         /*
          * For this case, CPU is added by specifying the bus. Under the
          * topology tree, specifying only the bus should be feasible, but
diff --git a/hw/i386/x86.c b/hw/i386/x86.c
index 01fc5e656272..cdf7b81ad0e3 100644
--- a/hw/i386/x86.c
+++ b/hw/i386/x86.c
@@ -381,6 +381,8 @@  static void x86_machine_class_init(ObjectClass *oc, void *data)
     mc->cpu_index_to_instance_props = x86_cpu_index_to_props;
     mc->get_default_cpu_node_id = x86_get_default_cpu_node_id;
     mc->possible_cpu_arch_ids = x86_possible_cpu_arch_ids;
+    mc->smp_props.arch_id_topo_level = CPU_TOPOLOGY_LEVEL_THREAD;
+    mc->smp_props.topo_tree_supported = true;
     mc->kvm_type = x86_kvm_type;
     x86mc->save_tsc_khz = true;
     x86mc->fwcfg_dma_enabled = true;
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
index 90221ceb7313..fb54c2c100a0 100644
--- a/target/i386/cpu.c
+++ b/target/i386/cpu.c
@@ -8473,6 +8473,8 @@  static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
 #ifndef CONFIG_USER_ONLY
     BusFinderClass *bfc = BUS_FINDER_CLASS(oc);
     bfc->find_bus = x86_cpu_get_parent_bus;
+
+    dc->bus_type = TYPE_CPU_BUS;
 #endif
 
     object_class_property_add(oc, "family", "int",