Skip to contentSkip to conversation

clocksource/drivers/timer-mediatek: Enable the GPT irq before registering

PATCH In review Ryan Brue 3 participants 3 messages Nov 26

Subsystems (2)
lore
Share & tools
lore

Ryan Brue

author
#0 lore raw

PATCH v2 1/1 clocksource/drivers/timer-mediatek: Enable the GPT irq before registering

Since the driver probes as a platform device, the GPT clock event is registered late in boot, when PSCI CPU idle may already have put CPUs into a state that stops their local timer. Those CPUs wait in the oneshot broadcast mask on the hrtimer broadcast, and tick_install_broadcast_device() arms the new broadcast device to expire at once so that it wakes them.

mtk_gpt_init() sets the timer's bit in GPT_IRQ_EN only after clockevents_config_and_register(). The forced event fires while the bit is clear, the status register stays clear, and the waiting CPUs are never woken. On an MT8173 tablet with the CPU_SLEEP_0 idle state in use, this hung the boot in 20 of 40 boots; with this change it booted 16 of 16.

Enable the interrupt before registering the clock event. The IRQ line stays disabled until enable_irq(), so the handler still can't run before the clock event is set up. With the bit set, the timer that mtk_gpt_setup() leaves running could latch an interrupt that registration doesn't acknowledge, the kind of pending interrupt that commit fc686d0037d7 ("clockevents/drivers/mtk: Fix spurious interrupt leading to crash") acknowledges in mtk_gpt_enable_irq(). So stop the timer before enabling the interrupt, until the core programs an event.

Fixes 7a097aba7255 ("clocksource/drivers/timer-mediatek: Convert to OF probing")

---
Since v7.4-rc1 the MediaTek timer probes as a platform device, and on MT8173 with PSCI CPU idle the boot then hangs about half the time: the broadcast device is armed at once to wake CPUs that are already idle, and that event is lost because the GPT's interrupt is enabled only after registration. Traced on the board with prints in tick_install_broadcast_device() and mtk_gpt_init().

v2 answers Sashiko's review of the resend: enabling the interrupt early also enabled it while the clock event timer was still running from mtk_gpt_setup(), so it could latch an interrupt that registration does not acknowledge. v2 stops the timer before enabling the interrupt. On the MT8173 tablet, with CPU_SLEEP_0 in use and prints around registration, v2 booted 16 times out of 16. The status register read 0 before registration in every boot, with v1 (3 boots) as well, so the stale interrupt never showed up on this board. In 3 of the 16 v2 boots it read 1 just before enable_irq(): the event registration armed, then delivered once the line was enabled. The tags from v1 are dropped, since the code changed.

This patch was developed with the help of an LLM.
---
Changes in v2:
- Stop the clock event timer before enabling its interrupt, so nothing is
pending when the IRQ line is enabled (Sashiko, via Daniel).
- Dropped the Reviewed-by and Tested-by tags. The code changed in a way
that I think merits that, but let me know if not.
- Link to v1: https://lore.kernel.org/r/20261103-timer-mediatek-gpt-irq-order-v1-1-6d1ddf977224@gmail.com
- Link to the resend: https://lore.kernel.org/r/20261125-timer-mediatek-gpt-irq-order-v1-1-7e24640e82d4@gmail.com

---
 drivers/clocksource/timer-mediatek.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/clocksource/timer-mediatek.c +3 -1
index 842d6ee4259e..14723c6f5f5e 100644
@@ -339,10 +339,12 @@ static int mtk_gpt_init(struct platform_device *pdev)
 
 	/* Configure clock event */
 	mtk_gpt_setup(&to, TIMER_CLK_EVT, GPT_CTRL_OP_REPEAT);
+	/* Stop it so that nothing is pending when its irq is enabled */
+	mtk_gpt_clkevt_time_stop(&to, TIMER_CLK_EVT);
+	mtk_gpt_enable_irq(&to, TIMER_CLK_EVT);
 	clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
 					TIMER_SYNC_TICKS, 0xffffffff);
 
-	mtk_gpt_enable_irq(&to, TIMER_CLK_EVT);
 	enable_irq(timer_of_irq(&to));
 
 	return 0;

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261103-timer-mediatek-gpt-irq-order-a878ed869f84

Best regards,

Signature · 1 line
Ryan Brue <[email protected]>
Reviewed-by Tested-by on v2 1/1
#1 lore raw
Quoted 24 lines · Ryan Brue

Since the driver probes as a platform device, the GPT clock event is registered late in boot, when PSCI CPU idle may already have put CPUs into a state that stops their local timer. Those CPUs wait in the oneshot broadcast mask on the hrtimer broadcast, and tick_install_broadcast_device() arms the new broadcast device to expire at once so that it wakes them.

mtk_gpt_init() sets the timer's bit in GPT_IRQ_EN only after clockevents_config_and_register(). The forced event fires while the bit is clear, the status register stays clear, and the waiting CPUs are never woken. On an MT8173 tablet with the CPU_SLEEP_0 idle state in use, this hung the boot in 20 of 40 boots; with this change it booted 16 of 16.

Enable the interrupt before registering the clock event. The IRQ line stays disabled until enable_irq(), so the handler still can't run before the clock event is set up. With the bit set, the timer that mtk_gpt_setup() leaves running could latch an interrupt that registration doesn't acknowledge, the kind of pending interrupt that commit fc686d0037d7 ("clockevents/drivers/mtk: Fix spurious interrupt leading to crash") acknowledges in mtk_gpt_enable_irq(). So stop the timer before enabling the interrupt, until the core programs an event.

Quoted 2 lines
Fixes 7a097aba7255 ("clocksource/drivers/timer-mediatek: Convert to OF probing")
Tested-by on v2 1/1
#2 lore raw
In reply to Ryan Brue
Quoted 8 lines

Enable the interrupt before registering the clock event. The IRQ line stays disabled until enable_irq(), so the handler still can't run before the clock event is set up. With the bit set, the timer that mtk_gpt_setup() leaves running could latch an interrupt that registration doesn't acknowledge, the kind of pending interrupt that commit fc686d0037d7 ("clockevents/drivers/mtk: Fix spurious interrupt leading to crash") acknowledges in mtk_gpt_enable_irq(). So stop the timer before enabling the interrupt, until the core programs an event.

--
Luca Leonardo Scorcia
[email protected]