qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Zhao Liu <zhao1.liu@intel.com>
To: "Daniel P . Berrangé" <berrange@redhat.com>,
	"Igor Mammedov" <imammedo@redhat.com>,
	"Eduardo Habkost" <eduardo@habkost.net>,
	"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Yanan Wang" <wangyanan55@huawei.com>,
	"Michael S . Tsirkin" <mst@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Cédric Le Goater" <clg@kaod.org>,
	"Nicholas Piggin" <npiggin@gmail.com>,
	"Frédéric Barrat" <fbarrat@linux.ibm.com>,
	"Daniel Henrique Barboza" <danielhb413@gmail.com>,
	"David Gibson" <david@gibson.dropbear.id.au>,
	"Harsh Prateek Bora" <harshpb@linux.ibm.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Marcelo Tosatti" <mtosatti@redhat.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Peter Maydell" <peter.maydell@linaro.org>
Cc: qemu-devel@nongnu.org, kvm@vger.kernel.org, qemu-ppc@nongnu.org,
	qemu-arm@nongnu.org, Zhenyu Wang <zhenyu.z.wang@intel.com>,
	Dapeng Mi <dapeng1.mi@linux.intel.com>,
	Yongwei Ma <yongwei.ma@intel.com>, Zhao Liu <zhao1.liu@intel.com>
Subject: [RFC v2 12/15] hw/i386: Allow i386 to create new CPUs in topology tree
Date: Thu, 19 Sep 2024 09:55:30 +0800	[thread overview]
Message-ID: <20240919015533.766754-13-zhao1.liu@intel.com> (raw)
In-Reply-To: <20240919015533.766754-1-zhao1.liu@intel.com>

For x86, CPU's apic ID represent its topology path and is the
combination of topology sub IDs in each leavl.

When x86 machine creates CPUs, to insert the CPU into topology tree, use
apic ID to get topology sub IDs.

Then search the topology tree for the corresponding parent topology
device and insert the CPU into the CPU bus of the parent device.

Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
 hw/i386/x86-common.c | 101 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 97 insertions(+), 4 deletions(-)

diff --git a/hw/i386/x86-common.c b/hw/i386/x86-common.c
index b21d2ab97349..a7f082b0a90b 100644
--- a/hw/i386/x86-common.c
+++ b/hw/i386/x86-common.c
@@ -53,14 +53,107 @@
 /* Physical Address of PVH entry point read from kernel ELF NOTE */
 static size_t pvh_start_addr;
 
-static void x86_cpu_new(X86MachineState *x86ms, int64_t apic_id, Error **errp)
+static int x86_cpu_get_topo_id(const X86CPUTopoIDs *topo_ids,
+                               CpuTopologyLevel level)
 {
-    Object *cpu = object_new(MACHINE(x86ms)->cpu_type);
+    switch (level) {
+    case CPU_TOPOLOGY_LEVEL_THREAD:
+        return topo_ids->smt_id;
+    case CPU_TOPOLOGY_LEVEL_CORE:
+        return topo_ids->core_id;
+    case CPU_TOPOLOGY_LEVEL_MODULE:
+        return topo_ids->module_id;
+    case CPU_TOPOLOGY_LEVEL_DIE:
+        return topo_ids->die_id;
+    case CPU_TOPOLOGY_LEVEL_SOCKET:
+        return topo_ids->pkg_id;
+    default:
+        g_assert_not_reached();
+    }
+
+    return -1;
+}
+
+typedef struct SearchCoreCb {
+    const X86CPUTopoIDs *topo_ids;
+    const CPUTopoState *parent;
+} SearchCoreCb;
+
+static int x86_search_topo_parent(DeviceState *dev, void *opaque)
+{
+    CPUTopoState *topo = CPU_TOPO(dev);
+    CpuTopologyLevel level = GET_CPU_TOPO_LEVEL(topo);
+    SearchCoreCb *cb = opaque;
+    int topo_id, index;
+
+    topo_id = x86_cpu_get_topo_id(cb->topo_ids, level);
+    index = cpu_topo_get_index(topo);
+
+    if (topo_id < 0) {
+        error_report("Invalid %s-id: %d",
+                     CpuTopologyLevel_str(level), topo_id);
+        error_printf("Try to set the %s-id in [0-%d].\n",
+                     CpuTopologyLevel_str(level),
+                     cpu_topo_get_instances_num(topo) - 1);
+        return TOPO_FOREACH_ERR;
+    }
+
+    if (topo_id == index) {
+        if (level == CPU_TOPOLOGY_LEVEL_CORE) {
+            cb->parent = topo;
+            /* The error result could exit directly. */
+            return TOPO_FOREACH_ERR;
+        }
+        return TOPO_FOREACH_CONTINUE;
+    }
+    return TOPO_FOREACH_END;
+}
+
+static BusState *x86_find_topo_bus(MachineState *ms, X86CPUTopoIDs *topo_ids)
+{
+    SearchCoreCb cb;
+
+    cb.topo_ids = topo_ids;
+    cb.parent = NULL;
+    qbus_walk_children(BUS(&ms->topo->bus), x86_search_topo_parent,
+                       NULL, NULL, NULL, &cb);
+
+    if (!cb.parent) {
+        return NULL;
+    }
+
+    return BUS(cb.parent->bus);
+}
+
+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;
+
+    /*
+     * 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);
+
+        /* 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;
     }
-    qdev_realize(DEVICE(cpu), NULL, errp);
+    qdev_realize(dev, bus, errp);
 
 out:
     object_unref(cpu);
@@ -111,7 +204,7 @@ void x86_cpus_init(X86MachineState *x86ms, int default_cpu_version)
 
     possible_cpus = mc->possible_cpu_arch_ids(ms);
     for (i = 0; i < ms->smp.cpus; i++) {
-        x86_cpu_new(x86ms, possible_cpus->cpus[i].arch_id, &error_fatal);
+        x86_cpu_new(x86ms, i, possible_cpus->cpus[i].arch_id, &error_fatal);
     }
 }
 
-- 
2.34.1



  parent reply	other threads:[~2024-09-19  1:41 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-19  1:55 [RFC v2 00/15] qom-topo: Abstract CPU Topology Level to Topology Device Zhao Liu
2024-09-19  1:55 ` [RFC v2 01/15] qdev: Add pointer to BusChild in DeviceState Zhao Liu
2024-09-19  1:55 ` [RFC v2 02/15] qdev: Add the interface to reparent the device Zhao Liu
2024-09-19  1:55 ` [RFC v2 03/15] hw/cpu: Introduce CPU topology device and CPU bus Zhao Liu
2024-09-19  1:55 ` [RFC v2 04/15] hw/cpu: Introduce CPU slot to manage CPU topology Zhao Liu
2024-09-19  1:55 ` [RFC v2 05/15] qdev: Add method in BusClass to customize device index Zhao Liu
2024-09-19  1:55 ` [RFC v2 06/15] hw/core: Create CPU slot in MachineState to manage CPU topology tree Zhao Liu
2024-09-19  1:55 ` [RFC v2 07/15] hw/core/cpu: Convert CPU from general device to topology device Zhao Liu
2024-09-19  1:55 ` [RFC v2 08/15] hw/cpu/core: Convert cpu-core " Zhao Liu
2024-09-19  1:55 ` [RFC v2 09/15] hw/cpu: Abstract module/die/socket levels as topology devices Zhao Liu
2024-09-19  1:55 ` [RFC v2 10/15] hw/machine: Build smp topology tree from -smp Zhao Liu
2024-09-19  1:55 ` [RFC v2 11/15] hw/core: Support topology tree in none machine for compatibility Zhao Liu
2024-09-19  1:55 ` Zhao Liu [this message]
2024-09-19  1:55 ` [RFC v2 13/15] system/qdev-monitor: Introduce bus-finder interface for compatibility with bus-less plug behavior Zhao Liu
2024-09-19  1:55 ` [RFC v2 14/15] i386/cpu: Support CPU plugged in topology tree via bus-finder Zhao Liu
2024-09-19  1:55 ` [RFC v2 15/15] i386: Support topology device tree Zhao Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240919015533.766754-13-zhao1.liu@intel.com \
    --to=zhao1.liu@intel.com \
    --cc=alex.bennee@linaro.org \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=clg@kaod.org \
    --cc=danielhb413@gmail.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=eduardo@habkost.net \
    --cc=fbarrat@linux.ibm.com \
    --cc=harshpb@linux.ibm.com \
    --cc=imammedo@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=mtosatti@redhat.com \
    --cc=npiggin@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=wangyanan55@huawei.com \
    --cc=yongwei.ma@intel.com \
    --cc=zhenyu.z.wang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).