347 lines
12 KiB
Diff
347 lines
12 KiB
Diff
|
|
From 212ea93178ad1e65e625ec6942ee9aff93dd5321 Mon Sep 17 00:00:00 2001
|
||
|
|
From: Bibo Mao <maobibo@loongson.cn>
|
||
|
|
Date: Wed, 23 Oct 2024 15:13:11 +0800
|
||
|
|
Subject: [PATCH 70/78] hw/loongarch/virt: Add basic CPU plug support
|
||
|
|
|
||
|
|
Implement interface for cpu hotplug function, and enable cpu hotplug
|
||
|
|
feature on virt machine.
|
||
|
|
|
||
|
|
Co-developed-by: Xianglai Li <lixianglai@loongson.cn>
|
||
|
|
Signed-off-by: Bibo Mao <maobibo@loongson.cn>
|
||
|
|
Message-ID: <20241023071312.881866-3-maobibo@loongson.cn>
|
||
|
|
Signed-off-by: Xianglai Li <lixianglai@loongson.cn>
|
||
|
|
---
|
||
|
|
hw/loongarch/Kconfig | 1 +
|
||
|
|
hw/loongarch/virt.c | 193 +++++++++++++++++++++++++++++++++++-
|
||
|
|
include/hw/loongarch/virt.h | 1 +
|
||
|
|
target/loongarch/cpu.c | 13 +++
|
||
|
|
4 files changed, 206 insertions(+), 2 deletions(-)
|
||
|
|
|
||
|
|
diff --git a/hw/loongarch/Kconfig b/hw/loongarch/Kconfig
|
||
|
|
index 40944a8365..b42a8573d4 100644
|
||
|
|
--- a/hw/loongarch/Kconfig
|
||
|
|
+++ b/hw/loongarch/Kconfig
|
||
|
|
@@ -16,6 +16,7 @@ config LOONGARCH_VIRT
|
||
|
|
select LOONGARCH_EXTIOI
|
||
|
|
select LS7A_RTC
|
||
|
|
select SMBIOS
|
||
|
|
+ select ACPI_CPU_HOTPLUG
|
||
|
|
select ACPI_PCI
|
||
|
|
select ACPI_HW_REDUCED
|
||
|
|
select FW_CFG_DMA
|
||
|
|
diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c
|
||
|
|
index 8d1e53ff62..e7734ed3c0 100644
|
||
|
|
--- a/hw/loongarch/virt.c
|
||
|
|
+++ b/hw/loongarch/virt.c
|
||
|
|
@@ -821,7 +821,7 @@ static void virt_irq_init(LoongArchVirtMachineState *lvms)
|
||
|
|
|
||
|
|
/* Create IPI device */
|
||
|
|
ipi = qdev_new(TYPE_LOONGARCH_IPI);
|
||
|
|
- qdev_prop_set_uint32(ipi, "num-cpu", ms->smp.cpus);
|
||
|
|
+ qdev_prop_set_uint32(ipi, "num-cpu", ms->smp.max_cpus);
|
||
|
|
sysbus_realize_and_unref(SYS_BUS_DEVICE(ipi), &error_fatal);
|
||
|
|
|
||
|
|
/* IPI iocsr memory region */
|
||
|
|
@@ -845,9 +845,11 @@ static void virt_irq_init(LoongArchVirtMachineState *lvms)
|
||
|
|
env->ipistate = ipi;
|
||
|
|
}
|
||
|
|
|
||
|
|
+ lvms->ipi = ipi;
|
||
|
|
+
|
||
|
|
/* Create EXTIOI device */
|
||
|
|
extioi = qdev_new(TYPE_LOONGARCH_EXTIOI);
|
||
|
|
- qdev_prop_set_uint32(extioi, "num-cpu", ms->smp.cpus);
|
||
|
|
+ qdev_prop_set_uint32(extioi, "num-cpu", ms->smp.max_cpus);
|
||
|
|
if (virt_is_veiointc_enabled(lvms)) {
|
||
|
|
qdev_prop_set_bit(extioi, "has-virtualization-extension", true);
|
||
|
|
}
|
||
|
|
@@ -873,6 +875,8 @@ static void virt_irq_init(LoongArchVirtMachineState *lvms)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
+ lvms->extioi = extioi;
|
||
|
|
+
|
||
|
|
/* Add Extend I/O Interrupt Controller node */
|
||
|
|
fdt_add_eiointc_node(lvms, &cpuintc_phandle, &eiointc_phandle);
|
||
|
|
|
||
|
|
@@ -1310,6 +1314,181 @@ static int virt_get_arch_id_from_topo(MachineState *ms, LoongArchCPUTopo *topo)
|
||
|
|
return arch_id;
|
||
|
|
}
|
||
|
|
|
||
|
|
+/* find cpu slot in machine->possible_cpus by arch_id */
|
||
|
|
+static CPUArchId *virt_find_cpu_slot(MachineState *ms, int arch_id, int *index)
|
||
|
|
+{
|
||
|
|
+ int n;
|
||
|
|
+ for (n = 0; n < ms->possible_cpus->len; n++) {
|
||
|
|
+ if (ms->possible_cpus->cpus[n].arch_id == arch_id) {
|
||
|
|
+ if (index) {
|
||
|
|
+ *index = n;
|
||
|
|
+ }
|
||
|
|
+ return &ms->possible_cpus->cpus[n];
|
||
|
|
+ }
|
||
|
|
+ }
|
||
|
|
+
|
||
|
|
+ return NULL;
|
||
|
|
+}
|
||
|
|
+
|
||
|
|
+static void virt_cpu_pre_plug(HotplugHandler *hotplug_dev,
|
||
|
|
+ DeviceState *dev, Error **errp)
|
||
|
|
+{
|
||
|
|
+ MachineState *ms = MACHINE(OBJECT(hotplug_dev));
|
||
|
|
+ MachineClass *mc = MACHINE_GET_CLASS(hotplug_dev);
|
||
|
|
+ LoongArchCPU *cpu = LOONGARCH_CPU(dev);
|
||
|
|
+ CPUState *cs = CPU(dev);
|
||
|
|
+ CPUArchId *cpu_slot;
|
||
|
|
+ Error *local_err = NULL;
|
||
|
|
+ LoongArchCPUTopo topo;
|
||
|
|
+ int arch_id, index;
|
||
|
|
+
|
||
|
|
+ if (dev->hotplugged && !mc->has_hotpluggable_cpus) {
|
||
|
|
+ error_setg(&local_err, "CPU hotplug not supported for this machine");
|
||
|
|
+ goto out;
|
||
|
|
+ }
|
||
|
|
+
|
||
|
|
+ /* sanity check the cpu */
|
||
|
|
+ if (!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) {
|
||
|
|
+ error_setg(&local_err, "Invalid CPU type, expected cpu type: '%s'",
|
||
|
|
+ ms->cpu_type);
|
||
|
|
+ goto out;
|
||
|
|
+ }
|
||
|
|
+
|
||
|
|
+ if ((cpu->thread_id < 0) || (cpu->thread_id >= ms->smp.threads)) {
|
||
|
|
+ error_setg(&local_err,
|
||
|
|
+ "Invalid thread-id %u specified, must be in range 1:%u",
|
||
|
|
+ cpu->thread_id, ms->smp.threads - 1);
|
||
|
|
+ goto out;
|
||
|
|
+ }
|
||
|
|
+
|
||
|
|
+ if ((cpu->core_id < 0) || (cpu->core_id >= ms->smp.cores)) {
|
||
|
|
+ error_setg(&local_err,
|
||
|
|
+ "Invalid core-id %u specified, must be in range 1:%u",
|
||
|
|
+ cpu->core_id, ms->smp.cores - 1);
|
||
|
|
+ goto out;
|
||
|
|
+ }
|
||
|
|
+
|
||
|
|
+ if ((cpu->socket_id < 0) || (cpu->socket_id >= ms->smp.sockets)) {
|
||
|
|
+ error_setg(&local_err,
|
||
|
|
+ "Invalid socket-id %u specified, must be in range 1:%u",
|
||
|
|
+ cpu->socket_id, ms->smp.sockets - 1);
|
||
|
|
+ goto out;
|
||
|
|
+ }
|
||
|
|
+
|
||
|
|
+ topo.socket_id = cpu->socket_id;
|
||
|
|
+ topo.core_id = cpu->core_id;
|
||
|
|
+ topo.thread_id = cpu->thread_id;
|
||
|
|
+ arch_id = virt_get_arch_id_from_topo(ms, &topo);
|
||
|
|
+ cpu_slot = virt_find_cpu_slot(ms, arch_id, &index);
|
||
|
|
+ if (CPU(cpu_slot->cpu)) {
|
||
|
|
+ error_setg(&local_err,
|
||
|
|
+ "cpu(id%d=%d:%d:%d) with arch-id %" PRIu64 " exists",
|
||
|
|
+ cs->cpu_index, cpu->socket_id, cpu->core_id,
|
||
|
|
+ cpu->thread_id, cpu_slot->arch_id);
|
||
|
|
+ goto out;
|
||
|
|
+ }
|
||
|
|
+ cpu->phy_id = arch_id;
|
||
|
|
+ /*
|
||
|
|
+ * update cpu_index calculation method since it is easily used as index
|
||
|
|
+ * with possible_cpus array by function virt_cpu_index_to_props
|
||
|
|
+ */
|
||
|
|
+ cs->cpu_index = index;
|
||
|
|
+ numa_cpu_pre_plug(cpu_slot, dev, &local_err);
|
||
|
|
+ return ;
|
||
|
|
+
|
||
|
|
+out:
|
||
|
|
+ error_propagate(errp, local_err);
|
||
|
|
+}
|
||
|
|
+
|
||
|
|
+static void virt_cpu_unplug_request(HotplugHandler *hotplug_dev,
|
||
|
|
+ DeviceState *dev, Error **errp)
|
||
|
|
+{
|
||
|
|
+ LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(hotplug_dev);
|
||
|
|
+ Error *local_err = NULL;
|
||
|
|
+ HotplugHandlerClass *hhc;
|
||
|
|
+ LoongArchCPU *cpu = LOONGARCH_CPU(dev);
|
||
|
|
+ CPUState *cs = CPU(dev);
|
||
|
|
+
|
||
|
|
+ if (!lvms->acpi_ged) {
|
||
|
|
+ error_setg(&local_err, "CPU hot unplug not supported without ACPI");
|
||
|
|
+ error_propagate(errp, local_err);
|
||
|
|
+ return;
|
||
|
|
+ }
|
||
|
|
+
|
||
|
|
+ if (cs->cpu_index == 0) {
|
||
|
|
+ error_setg(&local_err,
|
||
|
|
+ "hot-unplug of boot cpu(id%d=%d:%d:%d) not supported",
|
||
|
|
+ cs->cpu_index, cpu->socket_id,
|
||
|
|
+ cpu->core_id, cpu->thread_id);
|
||
|
|
+ error_propagate(errp, local_err);
|
||
|
|
+ return;
|
||
|
|
+ }
|
||
|
|
+
|
||
|
|
+ hhc = HOTPLUG_HANDLER_GET_CLASS(lvms->acpi_ged);
|
||
|
|
+ hhc->unplug_request(HOTPLUG_HANDLER(lvms->acpi_ged), dev, &local_err);
|
||
|
|
+}
|
||
|
|
+
|
||
|
|
+static void virt_cpu_unplug(HotplugHandler *hotplug_dev,
|
||
|
|
+ DeviceState *dev, Error **errp)
|
||
|
|
+{
|
||
|
|
+ CPUArchId *cpu_slot;
|
||
|
|
+ HotplugHandlerClass *hhc;
|
||
|
|
+ Error *local_err = NULL;
|
||
|
|
+ LoongArchCPU *cpu = LOONGARCH_CPU(dev);
|
||
|
|
+ LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(hotplug_dev);
|
||
|
|
+
|
||
|
|
+ hhc = HOTPLUG_HANDLER_GET_CLASS(lvms->acpi_ged);
|
||
|
|
+ hhc->unplug(HOTPLUG_HANDLER(lvms->acpi_ged), dev, &local_err);
|
||
|
|
+ if (local_err) {
|
||
|
|
+ error_propagate(errp, local_err);
|
||
|
|
+ return;
|
||
|
|
+ }
|
||
|
|
+
|
||
|
|
+ cpu_slot = virt_find_cpu_slot(MACHINE(lvms), cpu->phy_id, NULL);
|
||
|
|
+ cpu_slot->cpu = NULL;
|
||
|
|
+ return;
|
||
|
|
+}
|
||
|
|
+
|
||
|
|
+static void virt_cpu_plug(HotplugHandler *hotplug_dev,
|
||
|
|
+ DeviceState *dev, Error **errp)
|
||
|
|
+{
|
||
|
|
+ CPUArchId *cpu_slot;
|
||
|
|
+ HotplugHandlerClass *hhc;
|
||
|
|
+ Error *local_err = NULL;
|
||
|
|
+ LoongArchCPU *cpu = LOONGARCH_CPU(dev);
|
||
|
|
+ CPUState *cs = CPU(cpu);
|
||
|
|
+ CPULoongArchState *env;
|
||
|
|
+ LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(hotplug_dev);
|
||
|
|
+ int pin;
|
||
|
|
+
|
||
|
|
+ if (lvms->acpi_ged) {
|
||
|
|
+ env = &(cpu->env);
|
||
|
|
+ env->address_space_iocsr = &lvms->as_iocsr;
|
||
|
|
+
|
||
|
|
+ env->ipistate = lvms->ipi;
|
||
|
|
+ if (!(kvm_enabled() && kvm_irqchip_in_kernel())) {
|
||
|
|
+ /* connect ipi irq to cpu irq, logic cpu index used here */
|
||
|
|
+ qdev_connect_gpio_out(lvms->ipi, cs->cpu_index,
|
||
|
|
+ qdev_get_gpio_in(dev, IRQ_IPI));
|
||
|
|
+
|
||
|
|
+ for (pin = 0; pin < LS3A_INTC_IP; pin++) {
|
||
|
|
+ qdev_connect_gpio_out(lvms->extioi, (cs->cpu_index * 8 + pin),
|
||
|
|
+ qdev_get_gpio_in(dev, pin + 2));
|
||
|
|
+ }
|
||
|
|
+ }
|
||
|
|
+ hhc = HOTPLUG_HANDLER_GET_CLASS(lvms->acpi_ged);
|
||
|
|
+ hhc->plug(HOTPLUG_HANDLER(lvms->acpi_ged), dev, &local_err);
|
||
|
|
+ if (local_err) {
|
||
|
|
+ error_propagate(errp, local_err);
|
||
|
|
+ return;
|
||
|
|
+ }
|
||
|
|
+ }
|
||
|
|
+
|
||
|
|
+ cpu_slot = virt_find_cpu_slot(MACHINE(lvms), cpu->phy_id, NULL);
|
||
|
|
+ cpu_slot->cpu = OBJECT(dev);
|
||
|
|
+ return;
|
||
|
|
+}
|
||
|
|
+
|
||
|
|
static bool memhp_type_supported(DeviceState *dev)
|
||
|
|
{
|
||
|
|
/* we only support pc dimm now */
|
||
|
|
@@ -1328,6 +1507,8 @@ static void virt_device_pre_plug(HotplugHandler *hotplug_dev,
|
||
|
|
{
|
||
|
|
if (memhp_type_supported(dev)) {
|
||
|
|
virt_mem_pre_plug(hotplug_dev, dev, errp);
|
||
|
|
+ } else if (object_dynamic_cast(OBJECT(dev), TYPE_LOONGARCH_CPU)) {
|
||
|
|
+ virt_cpu_pre_plug(hotplug_dev, dev, errp);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@@ -1346,6 +1527,8 @@ static void virt_device_unplug_request(HotplugHandler *hotplug_dev,
|
||
|
|
{
|
||
|
|
if (memhp_type_supported(dev)) {
|
||
|
|
virt_mem_unplug_request(hotplug_dev, dev, errp);
|
||
|
|
+ } else if (object_dynamic_cast(OBJECT(dev), TYPE_LOONGARCH_CPU)) {
|
||
|
|
+ virt_cpu_unplug_request(hotplug_dev, dev, errp);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@@ -1364,6 +1547,8 @@ static void virt_device_unplug(HotplugHandler *hotplug_dev,
|
||
|
|
{
|
||
|
|
if (memhp_type_supported(dev)) {
|
||
|
|
virt_mem_unplug(hotplug_dev, dev, errp);
|
||
|
|
+ } else if (object_dynamic_cast(OBJECT(dev), TYPE_LOONGARCH_CPU)) {
|
||
|
|
+ virt_cpu_unplug(hotplug_dev, dev, errp);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@@ -1391,6 +1576,8 @@ static void virt_device_plug_cb(HotplugHandler *hotplug_dev,
|
||
|
|
}
|
||
|
|
} else if (memhp_type_supported(dev)) {
|
||
|
|
virt_mem_plug(hotplug_dev, dev, errp);
|
||
|
|
+ } else if (object_dynamic_cast(OBJECT(dev), TYPE_LOONGARCH_CPU)) {
|
||
|
|
+ virt_cpu_plug(hotplug_dev, dev, errp);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@@ -1400,6 +1587,7 @@ static HotplugHandler *virt_get_hotplug_handler(MachineState *machine,
|
||
|
|
MachineClass *mc = MACHINE_GET_CLASS(machine);
|
||
|
|
|
||
|
|
if (device_is_dynamic_sysbus(mc, dev) ||
|
||
|
|
+ object_dynamic_cast(OBJECT(dev), TYPE_LOONGARCH_CPU) ||
|
||
|
|
object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI) ||
|
||
|
|
memhp_type_supported(dev)) {
|
||
|
|
return HOTPLUG_HANDLER(machine);
|
||
|
|
@@ -1489,6 +1677,7 @@ static void virt_class_init(ObjectClass *oc, void *data)
|
||
|
|
mc->numa_mem_supported = true;
|
||
|
|
mc->auto_enable_numa_with_memhp = true;
|
||
|
|
mc->auto_enable_numa_with_memdev = true;
|
||
|
|
+ mc->has_hotpluggable_cpus = true;
|
||
|
|
mc->get_hotplug_handler = virt_get_hotplug_handler;
|
||
|
|
mc->default_nic = "virtio-net-pci";
|
||
|
|
hc->plug = virt_device_plug_cb;
|
||
|
|
diff --git a/include/hw/loongarch/virt.h b/include/hw/loongarch/virt.h
|
||
|
|
index 0a4d9a25f0..27c52af9f3 100644
|
||
|
|
--- a/include/hw/loongarch/virt.h
|
||
|
|
+++ b/include/hw/loongarch/virt.h
|
||
|
|
@@ -64,6 +64,7 @@ struct LoongArchVirtMachineState {
|
||
|
|
AddressSpace as_iocsr;
|
||
|
|
int features;
|
||
|
|
struct loongarch_boot_info bootinfo;
|
||
|
|
+ DeviceState *ipi;
|
||
|
|
};
|
||
|
|
|
||
|
|
#define TYPE_LOONGARCH_VIRT_MACHINE MACHINE_TYPE_NAME("virt")
|
||
|
|
diff --git a/target/loongarch/cpu.c b/target/loongarch/cpu.c
|
||
|
|
index 673ed8ea18..ee764f0bc7 100644
|
||
|
|
--- a/target/loongarch/cpu.c
|
||
|
|
+++ b/target/loongarch/cpu.c
|
||
|
|
@@ -644,6 +644,17 @@ static void loongarch_cpu_realizefn(DeviceState *dev, Error **errp)
|
||
|
|
lacc->parent_realize(dev, errp);
|
||
|
|
}
|
||
|
|
|
||
|
|
+static void loongarch_cpu_unrealizefn(DeviceState *dev)
|
||
|
|
+{
|
||
|
|
+ LoongArchCPUClass *mcc = LOONGARCH_CPU_GET_CLASS(dev);
|
||
|
|
+
|
||
|
|
+#ifndef CONFIG_USER_ONLY
|
||
|
|
+ cpu_remove_sync(CPU(dev));
|
||
|
|
+#endif
|
||
|
|
+
|
||
|
|
+ mcc->parent_unrealize(dev);
|
||
|
|
+}
|
||
|
|
+
|
||
|
|
static bool loongarch_get_lsx(Object *obj, Error **errp)
|
||
|
|
{
|
||
|
|
LoongArchCPU *cpu = LOONGARCH_CPU(obj);
|
||
|
|
@@ -880,6 +891,8 @@ static void loongarch_cpu_class_init(ObjectClass *c, void *data)
|
||
|
|
device_class_set_props(dc, loongarch_cpu_properties);
|
||
|
|
device_class_set_parent_realize(dc, loongarch_cpu_realizefn,
|
||
|
|
&lacc->parent_realize);
|
||
|
|
+ device_class_set_parent_unrealize(dc, loongarch_cpu_unrealizefn,
|
||
|
|
+ &lacc->parent_unrealize);
|
||
|
|
resettable_class_set_parent_phases(rc, NULL, loongarch_cpu_reset_hold, NULL,
|
||
|
|
&lacc->parent_phases);
|
||
|
|
|
||
|
|
--
|
||
|
|
2.39.1
|
||
|
|
|