Skip to contentSkip to conversation

arm/virt: Extract common code to wire GICC<->vCPU IRQs for reuse

PATCH In review Salil Mehta 3 participants 7 messages Nov 3, 2024 – Nov 6, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm/virt: Extract common code to wire GICC<->vCPU IRQs for reuse

Extract common GIC and CPU interrupt wiring code to improve code readability and modularity, supporting reuse in future patch sets. This refactor is benign and introduces *no* functional changes.

Note: This patch has been isolated from a larger patch set to facilitate early merging and reduce the complexity of the original set, as it operates independently. All original tags and author contributions are retained.

[!] Please note, this is a purely cosmetic change. No functional change.

[4/05/2024: Issue with total number of PPI available during create GIC]
[5/05/2024: Fix the total number of PPIs available as per ARM BSA to avoid overflow]
---
 hw/arm/virt.c | 108 ++++++++++++++++++++++++++++----------------------
 1 file changed, 60 insertions(+), 48 deletions(-)
hw/arm/virt.c +60 -48
index a0d3bef875..d6892b0266 100644
@@ -761,6 +761,65 @@ static bool gicv3_nmi_present(VirtMachineState *vms)
            (vms->gic_version != VIRT_GIC_VERSION_2);
 }
 
+/*
+ * Mapping from the output timer irq lines from the CPU to the GIC PPI inputs
+ * we use for the virt board.
+ */
+const int timer_irq[] = {
+    [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
+    [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
+    [GTIMER_HYP]  = ARCH_TIMER_NS_EL2_IRQ,
+    [GTIMER_SEC]  = ARCH_TIMER_S_EL1_IRQ,
+    [GTIMER_HYPVIRT] = ARCH_TIMER_NS_EL2_VIRT_IRQ,
+};
+
+static void wire_gic_cpu_irqs(VirtMachineState *vms, CPUState *cs)
+{
+    SysBusDevice *gicbusdev = SYS_BUS_DEVICE(vms->gic);
+    unsigned int smp_cpus = MACHINE(vms)->smp.cpus;
+    DeviceState *cpudev = DEVICE(cs);
+    int i = CPU(cs)->cpu_index;
+    int intidbase, irqn;
+
+    intidbase = NUM_IRQS + i * GIC_INTERNAL;
+
+    for (irqn = 0; irqn < ARRAY_SIZE(timer_irq); irqn++) {
+        qdev_connect_gpio_out(cpudev, irqn,
+                              qdev_get_gpio_in(vms->gic,
+                                               intidbase + timer_irq[irqn]));
+    }
+
+
+    if (vms->gic_version != VIRT_GIC_VERSION_2) {
+        qemu_irq irq = qdev_get_gpio_in(vms->gic,
+                                        intidbase + ARCH_GIC_MAINT_IRQ);
+        qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt",
+                                    0, irq);
+    } else if (vms->virt) {
+        qemu_irq irq = qdev_get_gpio_in(vms->gic,
+                                        intidbase + ARCH_GIC_MAINT_IRQ);
+        sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus, irq);
+    }
+
+    qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
+                                qdev_get_gpio_in(vms->gic, intidbase
+                                                  + VIRTUAL_PMU_IRQ));
+
+    sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
+    sysbus_connect_irq(gicbusdev, i + smp_cpus,
+                       qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
+    sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus,
+                       qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
+    sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus,
+                       qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
+    if (vms->gic_version != VIRT_GIC_VERSION_2) {
+        sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus,
+                           qdev_get_gpio_in(cpudev, ARM_CPU_NMI));
+        sysbus_connect_irq(gicbusdev, i + 5 * smp_cpus,
+                           qdev_get_gpio_in(cpudev, ARM_CPU_VINMI));
+    }
+}
+
 static void create_gic(VirtMachineState *vms, MemoryRegion *mem)
 {
     MachineState *ms = MACHINE(vms);
@@ -862,54 +921,7 @@ static void create_gic(VirtMachineState *vms, MemoryRegion *mem)
      * CPU's inputs.
      */
     for (i = 0; i < smp_cpus; i++) {
-        DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
-        int intidbase = NUM_IRQS + i * GIC_INTERNAL;
-        /* Mapping from the output timer irq lines from the CPU to the
-         * GIC PPI inputs we use for the virt board.
-         */
-        const int timer_irq[] = {
-            [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
-            [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
-            [GTIMER_HYP]  = ARCH_TIMER_NS_EL2_IRQ,
-            [GTIMER_SEC]  = ARCH_TIMER_S_EL1_IRQ,
-            [GTIMER_HYPVIRT] = ARCH_TIMER_NS_EL2_VIRT_IRQ,
-        };
-
-        for (unsigned irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
-            qdev_connect_gpio_out(cpudev, irq,
-                                  qdev_get_gpio_in(vms->gic,
-                                                   intidbase + timer_irq[irq]));
-        }
-
-        if (vms->gic_version != VIRT_GIC_VERSION_2) {
-            qemu_irq irq = qdev_get_gpio_in(vms->gic,
-                                            intidbase + ARCH_GIC_MAINT_IRQ);
-            qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt",
-                                        0, irq);
-        } else if (vms->virt) {
-            qemu_irq irq = qdev_get_gpio_in(vms->gic,
-                                            intidbase + ARCH_GIC_MAINT_IRQ);
-            sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus, irq);
-        }
-
-        qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
-                                    qdev_get_gpio_in(vms->gic, intidbase
-                                                     + VIRTUAL_PMU_IRQ));
-
-        sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
-        sysbus_connect_irq(gicbusdev, i + smp_cpus,
-                           qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
-        sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus,
-                           qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
-        sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus,
-                           qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
-
-        if (vms->gic_version != VIRT_GIC_VERSION_2) {
-            sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus,
-                               qdev_get_gpio_in(cpudev, ARM_CPU_NMI));
-            sysbus_connect_irq(gicbusdev, i + 5 * smp_cpus,
-                               qdev_get_gpio_in(cpudev, ARM_CPU_VINMI));
-        }
+        wire_gic_cpu_irqs(vms, qemu_get_cpu(i));
     }
 
     fdt_add_gic_node(vms);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 9 lines · Salil Mehta

Extract common GIC and CPU interrupt wiring code to improve code readability and modularity, supporting reuse in future patch sets. This refactor is benign and introduces *no* functional changes.

Note: This patch has been isolated from a larger patch set to facilitate early merging and reduce the complexity of the original set, as it operates independently. All original tags and author contributions are retained.

I would prefer to see refactoring patches in the context of the series that makes them necessary. As it stands, there doesn't really seem to be much benefit to this change.

thanks
-- PMM

#2 lore raw
Quoted 6 lines · Peter Maydell
Quoted 5 lines · Salil Mehta

Extract common GIC and CPU interrupt wiring code to improve code readability and modularity, supporting reuse in future patch sets. This refactor is benign and introduces *no* functional changes.

Quoted 9 lines
Quoted 4 lines

Note: This patch has been isolated from a larger patch set to facilitate early merging and reduce the complexity of the original set, as it operates independently. All original tags and author contributions are retained.

I would prefer to see refactoring patches in the context of the series that makes them necessary. As it stands, there doesn't really seem to be much benefit to this change.

+1. Please include those 3 (refactoring) patches into a series where the background is given.

Thanks,
Gavin

#3 lore raw
In reply to Peter Maydell

HI Peter,

Quoted 10 lines

From: Peter Maydell <[email protected]>
Sent: Monday, November 4, 2024 1:27 PM
To: Salil Mehta <[email protected]>

Quoted 5 lines · Salil Mehta

Extract common GIC and CPU interrupt wiring code to improve code readability and modularity, supporting reuse in future patch sets. This refactor is benign and introduces *no* functional changes.

Quoted 8 lines
Quoted 4 lines

Note: This patch has been isolated from a larger patch set to facilitate early merging and reduce the complexity of the original set, as it operates independently. All original tags and author contributions are retained.

I would prefer to see refactoring patches in the context of the series that makes them necessary. As it stands, there doesn't really seem to be much benefit to this change.

Ok no issues. Just to confirm, your above comment is just specific to this patch or all the 3 individual patches I had sent for review?

Thanks
Salil.

Quoted 3 lines

thanks
-- PMM

#4 lore raw
Quoted 18 lines · Salil Mehta

HI Peter,

Quoted 15 lines

From: Peter Maydell <[email protected]>
Sent: Monday, November 4, 2024 1:27 PM
To: Salil Mehta <[email protected]>

Quoted 9 lines · Salil Mehta

Extract common GIC and CPU interrupt wiring code to improve code readability and modularity, supporting reuse in future patch sets. This refactor is benign and introduces *no* functional changes.

Note: This patch has been isolated from a larger patch set to facilitate early merging and reduce the complexity of the original set, as it operates independently. All original tags and author contributions are retained.

Quoted 7 lines
Quoted 3 lines

I would prefer to see refactoring patches in the context of the series that makes them necessary. As it stands, there doesn't really seem to be much benefit to this change.

Ok no issues. Just to confirm, your above comment is just specific to this patch or all the 3 individual patches I had sent for review?

It applies to all of them (and as a general principle).

thanks
-- PMM

#5 lore raw

HI Peter,

Quoted 29 lines

From: Peter Maydell <[email protected]>
Sent: Wednesday, November 6, 2024 1:01 PM
To: Salil Mehta <[email protected]>

Quoted 8 lines · Salil Mehta

HI Peter,

Quoted 5 lines

From: Peter Maydell <[email protected]>
Sent: Monday, November 4, 2024 1:27 PM
To: Salil Mehta <[email protected]>

On Sun, 3 Nov 2024 at 15:25, Salil Mehta <[email protected]>

Quoted 15 lines · wrote:
Quoted 13 lines
Quoted 6 lines

Extract common GIC and CPU interrupt wiring code to improve code readability and modularity, supporting reuse in future patch sets. This refactor is benign and introduces *no* functional changes.

Note: This patch has been isolated from a larger patch set to >

facilitate early merging and reduce the complexity of the original

Quoted 1 line

set, as it operates independently. All original tags and author >

contributions are retained.

I would prefer to see refactoring patches in the context of the
series that makes them necessary. As it stands, there doesn't really seem to be much benefit to this change.

Quoted 4 lines
Quoted 2 lines

Ok no issues. Just to confirm, your above comment is just specific to this patch or all the 3 individual patches I had sent for review?

It applies to all of them (and as a general principle).

Got it. I thought changes in below patches were in any case useful as they were improving the code?

https://lore.kernel.org/qemu-devel/[email protected]/
https://lore.kernel.org/qemu-devel/[email protected]/

Thanks

Quoted 3 lines

thanks
-- PMM

Reviewed-by on v1 1/1
#6 lore raw
In reply to Salil Mehta
Quoted 18 lines · Salil Mehta

Extract common GIC and CPU interrupt wiring code to improve code readability and modularity, supporting reuse in future patch sets. This refactor is benign and introduces *no* functional changes.

Note: This patch has been isolated from a larger patch set to facilitate early merging and reduce the complexity of the original set, as it operates independently. All original tags and author contributions are retained.

[!] Please note, this is a purely cosmetic change. No functional change.

[4/05/2024: Issue with total number of PPI available during create GIC]
[5/05/2024: Fix the total number of PPIs available as per ARM BSA to avoid overflow]
Quoted 4 lines
---
  hw/arm/virt.c | 108 ++++++++++++++++++++++++++++----------------------
  1 file changed, 60 insertions(+), 48 deletions(-)

With the following nitpicks addressed:

Quoted 10 lines
hw/arm/virt.c +2 -0
index a0d3bef875..d6892b0266 100644
@@ -761,6 +761,65 @@ static bool gicv3_nmi_present(VirtMachineState *vms)
             (vms->gic_version != VIRT_GIC_VERSION_2);
  }
 
+/*
+ * Mapping from the output timer irq lines from the CPU to the GIC PPI inputs

hw/arm/virt.c static bool gicv3_nmi_present(VirtMachineState *vms) · line 775

Quoted 10 lines
hw/arm/virt.c +10 -0
+ * we use for the virt board.
+ */
+const int timer_irq[] = {
+    [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
+    [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
+    [GTIMER_HYP]  = ARCH_TIMER_NS_EL2_IRQ,
+    [GTIMER_SEC]  = ARCH_TIMER_S_EL1_IRQ,
+    [GTIMER_HYPVIRT] = ARCH_TIMER_NS_EL2_VIRT_IRQ,
+};
+

'static' is needed at least since it's a file-scoped array.

hw/arm/virt.c static bool gicv3_nmi_present(VirtMachineState *vms) · line 785

Quoted 10 lines
hw/arm/virt.c +10 -0
+static void wire_gic_cpu_irqs(VirtMachineState *vms, CPUState *cs)
+{
+    SysBusDevice *gicbusdev = SYS_BUS_DEVICE(vms->gic);
+    unsigned int smp_cpus = MACHINE(vms)->smp.cpus;
+    DeviceState *cpudev = DEVICE(cs);
+    int i = CPU(cs)->cpu_index;
+    int intidbase, irqn;
+
+    intidbase = NUM_IRQS + i * GIC_INTERNAL;
+

The function name wire_gic_cpu_irqs() looks not standard enough. How about to rename it to virt_set_cpu_irqs(), or virt_connect_cpu_irqs() since we already had virt_set_cpu_properties()? The subject and changelog need to be adjusted accordingly.

Lets make some of the variant's names a bit meaningful, and CPU() isn't needed since @cs is already CPUState?

    int index = cs->cpu_index;
    int n, intidbase = NUM_IRQS + i * GIC_INTERNAL;
Quoted 86 lines
hw/arm/virt.c +37 -42
+    for (irqn = 0; irqn < ARRAY_SIZE(timer_irq); irqn++) {
+        qdev_connect_gpio_out(cpudev, irqn,
+                              qdev_get_gpio_in(vms->gic,
+                                               intidbase + timer_irq[irqn]));
+    }
+
+
+    if (vms->gic_version != VIRT_GIC_VERSION_2) {
+        qemu_irq irq = qdev_get_gpio_in(vms->gic,
+                                        intidbase + ARCH_GIC_MAINT_IRQ);
+        qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt",
+                                    0, irq);
+    } else if (vms->virt) {
+        qemu_irq irq = qdev_get_gpio_in(vms->gic,
+                                        intidbase + ARCH_GIC_MAINT_IRQ);
+        sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus, irq);
+    }
+
+    qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
+                                qdev_get_gpio_in(vms->gic, intidbase
+                                                  + VIRTUAL_PMU_IRQ));
+
+    sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
+    sysbus_connect_irq(gicbusdev, i + smp_cpus,
+                       qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
+    sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus,
+                       qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
+    sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus,
+                       qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
+    if (vms->gic_version != VIRT_GIC_VERSION_2) {
+        sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus,
+                           qdev_get_gpio_in(cpudev, ARM_CPU_NMI));
+        sysbus_connect_irq(gicbusdev, i + 5 * smp_cpus,
+                           qdev_get_gpio_in(cpudev, ARM_CPU_VINMI));
+    }
+}
+
  static void create_gic(VirtMachineState *vms, MemoryRegion *mem)
  {
      MachineState *ms = MACHINE(vms);
@@ -862,54 +921,7 @@ static void create_gic(VirtMachineState *vms, MemoryRegion *mem)
       * CPU's inputs.
       */
      for (i = 0; i < smp_cpus; i++) {
-        DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
-        int intidbase = NUM_IRQS + i * GIC_INTERNAL;
-        /* Mapping from the output timer irq lines from the CPU to the
-         * GIC PPI inputs we use for the virt board.
-         */
-        const int timer_irq[] = {
-            [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
-            [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
-            [GTIMER_HYP]  = ARCH_TIMER_NS_EL2_IRQ,
-            [GTIMER_SEC]  = ARCH_TIMER_S_EL1_IRQ,
-            [GTIMER_HYPVIRT] = ARCH_TIMER_NS_EL2_VIRT_IRQ,
-        };
-
-        for (unsigned irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
-            qdev_connect_gpio_out(cpudev, irq,
-                                  qdev_get_gpio_in(vms->gic,
-                                                   intidbase + timer_irq[irq]));
-        }
-
-        if (vms->gic_version != VIRT_GIC_VERSION_2) {
-            qemu_irq irq = qdev_get_gpio_in(vms->gic,
-                                            intidbase + ARCH_GIC_MAINT_IRQ);
-            qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt",
-                                        0, irq);
-        } else if (vms->virt) {
-            qemu_irq irq = qdev_get_gpio_in(vms->gic,
-                                            intidbase + ARCH_GIC_MAINT_IRQ);
-            sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus, irq);
-        }
-
-        qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
-                                    qdev_get_gpio_in(vms->gic, intidbase
-                                                     + VIRTUAL_PMU_IRQ));
-
-        sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
-        sysbus_connect_irq(gicbusdev, i + smp_cpus,
-                           qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
-        sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus,
-                           qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
-        sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus,
-                           qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
-

hw/arm/virt.c static void create_gic(VirtMachineState *vms, MemoryRegion *mem) · line 927

Quoted 10 lines
hw/arm/virt.c +1 -6
-        if (vms->gic_version != VIRT_GIC_VERSION_2) {
-            sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus,
-                               qdev_get_gpio_in(cpudev, ARM_CPU_NMI));
-            sysbus_connect_irq(gicbusdev, i + 5 * smp_cpus,
-                               qdev_get_gpio_in(cpudev, ARM_CPU_VINMI));
-        }
+        wire_gic_cpu_irqs(vms, qemu_get_cpu(i));
      }
 
      fdt_add_gic_node(vms);

Thanks,
Gavin