Skip to contentSkip to conversation

hw/arm/bcm2838: Route system timer interrupts to GIC

PATCH In review Jayesh Agarwal 2 participants 4 messages Nov 17 – Nov 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/arm/bcm2838: Route system timer interrupts to GIC

The system timer interrupts are only routed to the legacy interrupt controller and not to the GIC, so a guest using the GIC (software that relies on the system timer interrupts through the GIC, such as bare metal programs or hobby kernels) never receives them.

A qemu_irq connection follows one output, one input. So also wiring the timer to the GIC would replace its connection to the legacy controller.

Add a splitter per system-timer interrupt line so that each interrupt reaches both the legacy controller (output 0 of the splitters) and the GIC (output 1 of the splitters). The timer channel n goes to GIC SPI 64+n, so channels 0-3 go to SPIs 64-67.

Testing:
The raspi4b machine started in the qtest mode:
qemu-system-aarch64 -M raspi4b -display none -accel qtest -qtest stdio

Ran following commands:

	irq_intercept_in /machine/soc/peripherals
	readl 0xfe003004
	writel 0xfe00300c 1000
	writel 0xfe003010 2000
	writel 0xfe003014 3000
	writel 0xfe003018 4000
	clock_step 5000000
	readl 0xfe003000

Before the fix:

    clock_step 5000000
    OK 5000000
    readl 0xfe003000
    OK 0x000000000000000f

The alarm fires correctly (status flag outputs 0xf), but no IRQ raise 64-67 reaches the GIC.

After the fix:
clock_step 5000000
IRQ raise 64
IRQ raise 65
IRQ raise 66
IRQ raise 67
OK 5000000
readl 0xfe003000
OK 0x000000000000000f
The alarm fires, and all the four channels reached GIC lines 64-67.

---
This was picked up from Sourojeet's earlier patch, here is the link:
https://lore.kernel.org/qemu-devel/[email protected]/

 hw/arm/bcm2835_peripherals.c         | 33 ++++++++++++++++++----------
 hw/arm/bcm2838.c                     |  7 ++++++
 include/hw/arm/bcm2835_peripherals.h |  1 +
 include/hw/arm/bcm2838_peripherals.h |  4 ++++
 4 files changed, 33 insertions(+), 12 deletions(-)
hw/arm/bcm2835_peripherals.c +21 -12
index 558c180df9..f619beba33 100644
@@ -95,6 +95,14 @@ static void raspi_peripherals_base_init(Object *obj)
     /* SYS Timer */
     object_initialize_child(obj, "systimer", &s->systmr,
                             TYPE_BCM2835_SYSTIMER);
+    /*
+     * One splitter per system-timer interrupt line so that each interrupt
+     * reaches both the legacy controller and the GIC.
+     */
+    for (int n = 0; n < BCM2835_SYSTIMER_COUNT; n++) {
+        object_initialize_child(obj, "systmr-irq-splitter[*]",
+                                &s->systmr_irq_splitter[n], TYPE_SPLIT_IRQ);
+    }
 
     /* UART0 */
     object_initialize_child(obj, "uart0", &s->uart0, TYPE_PL011);
@@ -302,18 +310,19 @@ void bcm_soc_peripherals_common_realize(DeviceState *dev, Error **errp)
     }
     memory_region_add_subregion(&s->peri_mr, ST_OFFSET,
                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systmr), 0));
-    sysbus_connect_irq(SYS_BUS_DEVICE(&s->systmr), 0,
-        qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ,
-                               INTERRUPT_TIMER0));
-    sysbus_connect_irq(SYS_BUS_DEVICE(&s->systmr), 1,
-        qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ,
-                               INTERRUPT_TIMER1));
-    sysbus_connect_irq(SYS_BUS_DEVICE(&s->systmr), 2,
-        qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ,
-                               INTERRUPT_TIMER2));
-    sysbus_connect_irq(SYS_BUS_DEVICE(&s->systmr), 3,
-        qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ,
-                               INTERRUPT_TIMER3));
+    for (n = 0; n < BCM2835_SYSTIMER_COUNT; n++) {
+        qdev_prop_set_uint32(DEVICE(&s->systmr_irq_splitter[n]),
+                "num-lines", 2);
+        if (!qdev_realize(DEVICE(&s->systmr_irq_splitter[n]), NULL, errp)) {
+            return;
+        }
+        sysbus_connect_irq(SYS_BUS_DEVICE(&s->systmr), n,
+                qdev_get_gpio_in(DEVICE(&s->systmr_irq_splitter[n]), 0));
+        qdev_connect_gpio_out(DEVICE(&s->systmr_irq_splitter[n]), 0,
+                qdev_get_gpio_in_named(DEVICE(&s->ic),
+                    BCM2835_IC_GPU_IRQ,
+                    INTERRUPT_TIMER0 + n));
+    }
 
     /* UART0 */
     qdev_prop_set_chr(DEVICE(&s->uart0), "chardev", serial_hd(0));
hw/arm/bcm2838.c +7 -0
index 650b1fe8f4..d151c50f1e 100644
@@ -188,6 +188,13 @@ static void bcm2838_realize(DeviceState *dev, Error **errp)
     qdev_connect_gpio_out(DEVICE(&ps_base->orgated_i2c_irq_splitter), 1,
                           qdev_get_gpio_in(gicdev, GIC_SPI_INTERRUPT_I2C));
 
+    /* Connect the system timer interrupts to the interrupt controller */
+    for (int n = 0; n < BCM2835_SYSTIMER_COUNT; n++) {
+        int int_n = GIC_SPI_INTERRUPT_SYS_TIMER0 + n;
+        qdev_connect_gpio_out(DEVICE(&ps_base->systmr_irq_splitter[n]), 1,
+                              qdev_get_gpio_in(gicdev, int_n));
+    }
+
     /* Connect VC mailbox to the interrupt controller */
     sysbus_connect_irq(SYS_BUS_DEVICE(&ps_base->mboxes), 0,
                        qdev_get_gpio_in(gicdev, GIC_SPI_INTERRUPT_MBOX));
include/hw/arm/bcm2835_peripherals.h +1 -0
index 4f356f4643..bd41e0de92 100644
@@ -54,6 +54,7 @@ struct BCMSocPeripheralBaseState {
     qemu_irq irq, fiq;
 
     BCM2835SystemTimerState systmr;
+    SplitIRQ systmr_irq_splitter[BCM2835_SYSTIMER_COUNT];
     BCM2835MphiState mphi;
     UnimplementedDeviceState txp;
     UnimplementedDeviceState armtmr;
include/hw/arm/bcm2838_peripherals.h +4 -0
index 60bfacdce4..af2d3b2030 100644
@@ -20,6 +20,10 @@
 /* SPI */
 #define GIC_SPI_INTERRUPT_MBOX         33
 #define GIC_SPI_INTERRUPT_MPHI         40
+#define GIC_SPI_INTERRUPT_SYS_TIMER0   64
+#define GIC_SPI_INTERRUPT_SYS_TIMER1   65
+#define GIC_SPI_INTERRUPT_SYS_TIMER2   66
+#define GIC_SPI_INTERRUPT_SYS_TIMER3   67
 #define GIC_SPI_INTERRUPT_DWC2         73
 #define GIC_SPI_INTERRUPT_DMA_0        80
 #define GIC_SPI_INTERRUPT_DMA_6        86
Signature · 1 line
2.43.0
#1 lore raw
Quoted 47 lines · Jayesh Agarwal

The system timer interrupts are only routed to the legacy interrupt controller and not to the GIC, so a guest using the GIC (software that relies on the system timer interrupts through the GIC, such as bare metal programs or hobby kernels) never receives them.

A qemu_irq connection follows one output, one input. So also wiring the timer to the GIC would replace its connection to the legacy controller.

Add a splitter per system-timer interrupt line so that each interrupt reaches both the legacy controller (output 0 of the splitters) and the GIC (output 1 of the splitters). The timer channel n goes to GIC SPI 64+n, so channels 0-3 go to SPIs 64-67.

Testing:
The raspi4b machine started in the qtest mode:
qemu-system-aarch64 -M raspi4b -display none -accel qtest -qtest stdio

Ran following commands:

        irq_intercept_in /machine/soc/peripherals
        readl 0xfe003004
        writel 0xfe00300c 1000
        writel 0xfe003010 2000
        writel 0xfe003014 3000
        writel 0xfe003018 4000
        clock_step 5000000
        readl 0xfe003000

Before the fix:

    clock_step 5000000
    OK 5000000
    readl 0xfe003000
    OK 0x000000000000000f

The alarm fires correctly (status flag outputs 0xf), but no IRQ raise 64-67 reaches the GIC.

After the fix:
clock_step 5000000
IRQ raise 64
IRQ raise 65
IRQ raise 66
IRQ raise 67
OK 5000000
readl 0xfe003000
OK 0x000000000000000f
The alarm fires, and all the four channels reached GIC lines 64-67.

Quoted 6 lines

---
This was picked up from Sourojeet's earlier patch, here is the link:
https://lore.kernel.org/qemu-devel/[email protected]/

Thanks; applied to target-arm.next.

-- PMM

#2 lore raw
Quoted 1 line · Peter Maydell

Thanks; applied to target-arm.next.

Thank you. The problem that you raised in the 2025 thread on Sourojeet's patch also affects the following devices:
uart0, aux, mbox, sdhost, mphi, dwc2, rng200, the eMMC OR gate, DMA channels 0-6 and the two DMA OR gates.

bcm_soc_peripherals_common_realize() and bcm2838_peripherals_realize(), for the Pi4 only peripherals, wire these devices to the legacy controller, and later in bcm2838_realize(), the GIC wiring overwrites that.

Should I fix them the same way, one patch per device? I will send them in December.

Thanks
Jayesh Agarwal

#3 lore raw
Quoted 9 lines · Jayesh Agarwal
Quoted 1 line · Peter Maydell

Thanks; applied to target-arm.next.

Thank you. The problem that you raised in the 2025 thread on Sourojeet's patch also affects the following devices:
uart0, aux, mbox, sdhost, mphi, dwc2, rng200, the eMMC OR gate, DMA channels 0-6 and the two DMA OR gates.

Quoted 7 lines

bcm_soc_peripherals_common_realize() and bcm2838_peripherals_realize(), for the Pi4 only peripherals, wire these devices to the legacy controller, and later in bcm2838_realize(), the GIC wiring overwrites that.

Should I fix them the same way, one patch per device? I will send them in December.

Hmm, that's quite a lot of devices. We should fix them, but I suggest you have a look at whether there's a useful way to limit the amount of code-duplication involved (e.g. use an array of splitter objects and a data-driven loop rather than open-coding all the creation and wiring individually).

December is fine; as we're in freeze for the 11.2 release this wouldn't be going into the tree until we reopen at the end of December anyway.

thanks
-- PMM