[PATCH 4/5] hw/intc/loongarch_ipi: Implment cpu hotplug interface

Bibo Mao posted 5 patches 4 weeks ago
[PATCH 4/5] hw/intc/loongarch_ipi: Implment cpu hotplug interface
Posted by Bibo Mao 4 weeks ago
Add logic cpu allocation and cpu mapping with cpu hotplug interface.
When cpu is added, connect ipi gpio irq to CPU IRQ_IPI irq pin.

Also use hotplug_handler_plug() to nofity ipi object when cold-plug
is created.

Signed-off-by: Bibo Mao <maobibo@loongson.cn>
---
 hw/intc/loongarch_ipi.c | 46 +++++++++++++++++++++++++++++++++++++++++
 hw/loongarch/virt.c     |  5 ++---
 2 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/hw/intc/loongarch_ipi.c b/hw/intc/loongarch_ipi.c
index 9296006cf6..579c9c830b 100644
--- a/hw/intc/loongarch_ipi.c
+++ b/hw/intc/loongarch_ipi.c
@@ -65,25 +65,71 @@ static int loongarch_cpu_by_arch_id(LoongsonIPICommonState *lics,
 static void loongarch_cpu_plug(HotplugHandler *hotplug_dev,
                                DeviceState *dev, Error **errp)
 {
+    LoongarchIPIState *lis = LOONGARCH_IPI(hotplug_dev);
     Object *obj = OBJECT(dev);
+    LoongArchCPU *cpu;
+    int phy_id, index;
 
     if (!object_dynamic_cast(obj, TYPE_LOONGARCH_CPU)) {
         warn_report("LoongArch IPI: Invalid %s device type",
                                        object_get_typename(obj));
         return;
     }
+
+    cpu = LOONGARCH_CPU(dev);
+    phy_id = cpu->phy_id;
+    if ((phy_id >= MAX_PHY_ID) || (phy_id < 0)) {
+        warn_report("LoongArch IPI: Invalid phy id %d", phy_id);
+        return;
+    }
+
+    if (lis->present_cpu[phy_id] >= 0) {
+        warn_report("LoongArch IPI: phy id %d is added already", phy_id);
+        return;
+    }
+
+    index = find_first_zero_bit(lis->present_cpu_map, LOONGARCH_MAX_CPUS);
+    if (index == LOONGARCH_MAX_CPUS) {
+        error_setg(errp, "no free cpu slots available");
+        return;
+    }
+
+    /* connect ipi irq to cpu irq */
+    set_bit(index, lis->present_cpu_map);
+    lis->present_cpu[phy_id] = index;
+    lis->cs[phy_id] = CPU(dev);
+    qdev_connect_gpio_out(DEVICE(lis), index, qdev_get_gpio_in(dev, IRQ_IPI));
 }
 
 static void loongarch_cpu_unplug(HotplugHandler *hotplug_dev,
                                  DeviceState *dev, Error **errp)
 {
+    LoongarchIPIState *lis = LOONGARCH_IPI(hotplug_dev);
     Object *obj = OBJECT(dev);
+    LoongArchCPU *cpu;
+    int phy_id;
 
     if (!object_dynamic_cast(obj, TYPE_LOONGARCH_CPU)) {
         warn_report("LoongArch IPI: Invalid %s device type",
                                        object_get_typename(obj));
         return;
     }
+
+    cpu = LOONGARCH_CPU(dev);
+    phy_id = cpu->phy_id;
+    if ((phy_id >= MAX_PHY_ID) || (phy_id < 0)) {
+        warn_report("LoongArch IPI: Invalid phy id %d", phy_id);
+        return;
+    }
+
+    if (lis->present_cpu[phy_id] < 0) {
+        warn_report("LoongArch IPI: phy id %d is not added", phy_id);
+        return;
+    }
+
+    clear_bit(lis->present_cpu[phy_id], lis->present_cpu_map);
+    lis->present_cpu[phy_id] = INVALID_CPU;
+    lis->cs[phy_id] = NULL;
 }
 
 static void loongarch_ipi_realize(DeviceState *dev, Error **errp)
diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c
index 9a635d1d3d..8147897cc7 100644
--- a/hw/loongarch/virt.c
+++ b/hw/loongarch/virt.c
@@ -794,6 +794,7 @@ static void virt_irq_init(LoongArchVirtMachineState *lvms)
     CPUState *cpu_state;
     int cpu, pin, i, start, num;
     uint32_t cpuintc_phandle, eiointc_phandle, pch_pic_phandle, pch_msi_phandle;
+    Error *local_err = NULL;
 
     /*
      * Extended IRQ model.
@@ -859,9 +860,7 @@ static void virt_irq_init(LoongArchVirtMachineState *lvms)
         lacpu = LOONGARCH_CPU(cpu_state);
         env = &(lacpu->env);
         env->address_space_iocsr = &lvms->as_iocsr;
-
-        /* connect ipi irq to cpu irq */
-        qdev_connect_gpio_out(ipi, cpu, qdev_get_gpio_in(cpudev, IRQ_IPI));
+        hotplug_handler_plug(HOTPLUG_HANDLER(ipi), cpudev, &local_err);
         env->ipistate = ipi;
     }
 
-- 
2.39.3