Skip to contentSkip to conversation

watchdog: Add ARM TWD (MPCore private) watchdog driver

PATCH Applied Fil Dunsky 2 participants 2 messages Nov 21 Watchdog device drivers

lore
Share & tools
lore

Fil Dunsky

author
#0 lore raw

PATCH v9 1/1 watchdog: Add ARM TWD (MPCore private) watchdog driver

ARM11 MPCore, Cortex-A5 and Cortex-A9 MPCore systems have a watchdog next to the local timer of every CPU, banked at the same address. The binding "arm,cortex-a9-twd-wdt" exists, but there has been no driver since mpcore_wdt was removed in v3.11, among other reasons because it only ever programmed the CPU that ran the ioctl.

Use the watchdog of CPU0 as the system watchdog: start, stop, ping and get_timeleft run there through smp_call_function_single(). CPU0 cannot be taken offline on 32-bit ARM unless the platform allows it, the driver does not probe then. The watchdogs of the other CPUs are left alone. The watchdog interrupt is not needed.

Bootloaders such as the one of the Securifi Almond+ start the watchdog before booting the kernel. A running watchdog is marked WDOG_HW_RUNNING and taken over with the driver's timeout without being stopped, unless it runs with a different prescaler: the prescaler cannot be changed in watchdog mode. Once the watchdog mode is set it can only be left through the disable register sequence, which is what stop does.

The TWD clock follows the CPU clock on many systems, so rate changes are followed through a clock notifier, or a CPU frequency transition notifier when no clock is supplied. If CPU0 lost its watchdog in a deep idle state it is set up again on CPU PM exit. An atomic expiration time keeps clock rate changes and CPU idle exit from extending the timeout. Timeout changes are serialized with the frequency notifiers, and the expiration time is published before the CPU PM notifier can see the watchdog as armed.

For system sleep, stop the hardware and save the remaining timeout. Restore the expiration time from that interval at device resume:
monotonic time can advance during suspend-to-idle and device PM, so reusing the old absolute deadline could cause an immediate reset before the core post-resume keepalive. An already expired timeout is not revived. The core still stops its ping worker and refreshes the deadline normally with its explicit PM keepalives.

When no clock is supplied, calibrate PERIPHCLK against monotonic time on CPU0 and use cpufreq to track subsequent rate changes. Use a fixed calibration reference so repeated transitions do not accumulate rounding error, and invalidate measurements if a transition races with calibration. Wait for any transition that began before notifier registration, whose PRE notification could not have been observed. The fallback assumes a constant PERIPHCLK-to-CPU clock ratio; platforms with independently changing dividers need a clock provider. Calibration only observes an enabled counter and does not stop or reload a running watchdog. Bound the maximum timeout at the hardware maximum CPU frequency. Preserve actual clock provider errors, including deferred probe.

The watchdog is also registered as a restart handler with the default priority, for systems that have no other way to restart: the CPU that restarts the system, the only one left running, lets its own watchdog expire right away.

AI assistance: Codex implemented the cpufreq fallback, this PM fix and the host regression tests.

---
ARM11 MPCore, Cortex-A5 and Cortex-A9 MPCore systems have a watchdog next to the local timer of every CPU. The binding "arm,cortex-a9-twd-wdt" (watchdog/arm,twd-wdt.yaml) exists and 15 dts/dtsi files describe the device, but there has been no driver since mpcore_wdt was removed in v3.11 ("watchdog: delete mpcore_wdt driver").

As discussed on v4, this version uses the watchdog of CPU0 only, as the system watchdog. CPU0 cannot be taken offline on 32-bit ARM unless the platform allows it, and the driver does not probe then. The watchdogs of the other CPUs are not touched.

The driver was written for the Cortina Systems CS75xx / Securifi Almond+, whose boot loader arms the watchdog of CPU0: the board resets ~300 s after boot unless it is serviced. The driver also registers a restart handler with the default (last resort) priority, since the CS75xx platform has no other way to restart.

Before this PM change, the v7 driver was tested on a Securifi Almond+ (Cortex-A9, two CPUs) using its DT-supplied clock with PROVE_LOCKING:
takeover of the running watchdog; watchdog ioctls issued from CPU1; reducing the timeout from 60 to 30 seconds after 35 seconds without a ping; keepalives for 60 seconds followed by expiration and a reset; reboot(2) issued from CPU1 through the restart handler. Both runs returned to OpenWrt without lockdep warnings. GCC and Clang W=1 builds and host regression tests of deadline handling also pass. CPU hotplug, CPU PM and suspend were not tested: the platform has no support for them. The deadline regression tests simulate time and CPU PM interleaving; they do not replace testing on hardware with CPU PM.

The v6 changes address the five issues in the review confirmed by Guenter:
https://lore.kernel.org/all/[email protected]/

For system sleep, stop the hardware and save the remaining timeout. Restore the expiration time from that remaining interval at device resume, before CPU PM can rearm the watchdog. Monotonic time can advance while the counter is stopped, so retaining the old absolute deadline could restart it with just one tick before the core post-resume keepalive. Core PM keepalives still refresh the timeout normally.

When no clock is supplied, calibrate the counter against monotonic time on CPU0 and use cpufreq transition notifications to track subsequent rate changes. The fallback requires a cpufreq policy; without CPU_FREQ a clock is required, and without an available policy probe is deferred. The fallback assumes PERIPHCLK maintains its calibrated ratio to the CPU clock; platforms with an independently changing divider need a clock provider. Calibration does not stop or reload an active watchdog. The maximum timeout is bounded using the maximum hardware CPU frequency. Actual clock provider errors, including deferred probe, are preserved.

The fallback is implemented following Guenter's suggestion:
https://lore.kernel.org/all/[email protected]/
The separate MAINTAINERS entry is retained as preferred in that reply.

For v8's PM change, full ARM GCC vmlinux checks with CPU_FREQ=n and the driver built in, and Clang vmlinux/module/modpost checks with CPU_FREQ=y pass. Seventeen deadline/PM regressions and nineteen calibration/cpufreq regressions exercise actual driver helpers with mocked time, CPU0 calls and MMIO. The long-suspend regression fails on v7 and passes with the fix. Hardware system sleep is not available on Almond+, so the changed suspend/resume path has not been tested on hardware.

For the scoped_guard conversion, the ARM GCC W=1 driver object and Clang vmlinux/module/modpost checks pass with CPU_FREQ=y. The 23 calibration and cpufreq host checks use the actual scoped_guard macro from this kernel with modeled mutex cleanup; the v8 and v9 helpers pass the same cases.

AI assistance: Codex implemented the cpufreq fallback and this PM fix,
and created the host regression tests.
---
Changes in v9:
- use scoped_guard() for the two mutex-protected blocks in cpufreq setup (Guenter); keep the first retry check outside the scoped guard and break out of the final guard to retry the outer calibration loop
- exercise the actual scoped_guard macro and mutex cleanup in the host regression tests, including both validation retry paths
- Review: https://lore.kernel.org/all/[email protected]/
- Link to v8: https://patch.msgid.link/[email protected]

Changes in v8:
- save the remaining timeout at device suspend and restore the deadline at device resume, avoiding a one-tick reset before the core PM keepalive if monotonic time advanced while the hardware counter was stopped (sashiko-bot)
- retain core PM keepalive behavior and update the system sleep description
- cover long suspend, frozen timekeeping, expired deadlines, inactive watchdogs, repeated resume, subsecond precision and delayed stop completion with host regressions
- Report: https://lore.kernel.org/all/[email protected]/
- Link to v7: https://patch.msgid.link/[email protected]

Changes in v7:
- use counter calibration plus cpufreq transition notifications when no clock is supplied; retain provider errors and deferred probe
- keep a fixed calibration reference to avoid rounding drift; retry calibration if frequency changes while the reference is being measured; wait for transitions that began before notifier registration
- bound the fallback maximum timeout at the maximum hardware CPU frequency
- clarify that watchdog core PM keepalives refresh the deadline before system suspend and after resume; only the device callbacks retain it
- extend the host regression test to cover these core PM keepalives
- Link to v6: https://patch.msgid.link/[email protected]

Changes in v6:
- keep an atomic expiration time so CPU PM cannot combine an old keepalive with a new timeout; serialize timeout changes with the clock notifier and let the watchdog core perform the timeout-change ping
- publish the expiration time before the armed state for CPU PM
- register cleanup and the CPU PM notifier before watchdog takeover
- preserve the expiration time across suspend/resume, stop the core ping worker for suspend and ignore pings while the hardware is suspended
- replace obsolete __MODULE_STRING() with __stringify()
- Link to v5: https://patch.msgid.link/[email protected]

Changes in v5:
- use the watchdog of CPU0 only instead of the watchdogs of all CPUs, which overlapped with the lockup detector (Guenter): no CPU hotplug state, start, stop, ping and get_timeleft run on CPU0 through smp_call_function_single(); do not probe if CPU0 is hotpluggable
- keep the time of the last keepalive and load the watchdog with the time left from it, so a clock rate change or CPU PM exit no longer extends the timeout (sashiko-bot)
- serialize the watchdog operations and the clock notifier with a mutex
- stop the watchdog when the driver is removed (sashiko-bot)
- fix the comment on suspend (sashiko-bot)
- ignore clock notifications that do not change the rate
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
- rebase on v7.4-rc1
- sent on its own, split out of "Cortina Systems CS75xx: interrupt controller, GPIO, I2C and TWD watchdog"
- do not take cpus_read_lock() in the clock notifier, which can be
called from CPU hotplug through cpufreq: publish the armed state and
the rate with WRITE_ONCE() and a full barrier first, then apply them with on_each_cpu(), so that a rate change racing with start is never missed (sashiko-bot)
- restore the watchdog of a CPU on CPU PM exit when its power domain
went down in idle (sashiko-bot)
- register the restart handler with the default priority instead of
128, so that it only runs when nothing else can restart the system
- print the timeout and the clock rate only as debug messages
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- follow rate changes of the clock, which on many MPCore systems scales with the CPU clock, and restore the watchdog of the boot CPU on resume (sashiko-bot)
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- do not include <linux/mod_devicetable.h> (Uwe)
- the prescaler cannot be changed in watchdog mode (verified on the board), so a watchdog left running by the boot loader with a different prescaler is stopped before it is reprogrammed (sashiko-bot)
- Link to v1: https://patch.msgid.link/[email protected]

To: Wim Van Sebroeck <[email protected]>
To: Guenter Roeck <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: Russell King <[email protected]>
Cc: [email protected]

---
 MAINTAINERS                    |   6 +
 drivers/watchdog/Kconfig       |  15 +
 drivers/watchdog/Makefile      |   1 +
 drivers/watchdog/arm_twd_wdt.c | 684 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 706 insertions(+)
MAINTAINERS +6 -0
index 93abbb315..245d80c13 100644
@@ -2544,6 +2544,12 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc.git
 F:	arch/arm/mach-*/
 F:	arch/arm/plat-*/
 
+ARM TWD WATCHDOG DRIVER
+M:	Fil Dunsky <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/watchdog/arm_twd_wdt.c
+
 ARM/ACTIONS SEMI ARCHITECTURE
 M:	Andreas Färber <[email protected]>
 M:	Manivannan Sadhasivam <[email protected]>
drivers/watchdog/Kconfig +15 -0
index f7d0a0c2c..326546e24 100644
@@ -465,6 +465,21 @@ config ARM_SP805_WATCHDOG
 	  ARM Primecell SP805 Watchdog timer. This will reboot your system when
 	  the timeout is reached.
 
+config ARM_TWD_WATCHDOG
+	tristate "ARM TWD (MPCore private) Watchdog"
+	depends on HAVE_ARM_TWD || COMPILE_TEST
+	depends on HAS_IOMEM
+	select WATCHDOG_CORE
+	help
+	  Say Y here to include support for the private watchdog found next
+	  to the local (TWD) timer of every CPU in ARM11 MPCore, Cortex-A5
+	  and Cortex-A9 MPCore systems. The watchdog of CPU0 is used as the
+	  system watchdog. A watchdog left running by the bootloader is
+	  taken over.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called arm_twd_wdt.
+
 config ARM_SBSA_WATCHDOG
 	tristate "ARM SBSA Generic Watchdog"
 	depends on ARM64
drivers/watchdog/Makefile +1 -0
index 9ac406677..4ff0bced5 100644
@@ -40,6 +40,7 @@ obj-$(CONFIG_USBPCWATCHDOG) += pcwd_usb.o
 # ARM Architecture
 obj-$(CONFIG_ARM_SP805_WATCHDOG) += sp805_wdt.o
 obj-$(CONFIG_ARM_SBSA_WATCHDOG) += sbsa_gwdt.o
+obj-$(CONFIG_ARM_TWD_WATCHDOG) += arm_twd_wdt.o
 obj-$(CONFIG_ARMADA_37XX_WATCHDOG) += armada_37xx_wdt.o
 obj-$(CONFIG_AIROHA_WATCHDOG) += airoha_wdt.o
 obj-$(CONFIG_ASM9260_WATCHDOG) += asm9260_wdt.o
drivers/watchdog/arm_twd_wdt.c new file +684 -0

684 lines not on this page · raw message

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261015-arm-twd-wdt-71ff5cef355b

Best regards,

Signature · 1 line
Fil Dunsky <[email protected]>
maintainer Applied
#1 lore raw
Quoted 8 lines · Fil Dunsky

From: Fil Dunsky <[email protected]>

ARM11 MPCore, Cortex-A5 and Cortex-A9 MPCore systems have a watchdog next to the local timer of every CPU, banked at the same address. The binding "arm,cortex-a9-twd-wdt" exists, but there has been no driver since mpcore_wdt was removed in v3.11, among other reasons because it only ever programmed the CPU that ran the ioctl.

...

Quoted 2 lines

Applied.

Thanks,
Guenter