Add RISC-V SBI watchdog driver
Subsystems (2)
v4 cover Add RISC-V SBI watchdog driver
This series adds a driver for RISC-V SBI watchdog extension.
The SBI watchdog extension exposes watchdog including start, stop, pat, read and write attributes to the S-mode software. The kernel driver uses the standard SBI calls to interact and configure the attributes. The driver integrates with the kernel watchdog framework (/dev/watchdog) to ease user-space implementations.
The driver currently subscribes to the Message Signaled Interrupts (MSI) for watchdog pretimeout events.
The series consists of following changes:
- Patch 1 adds the definition for SBI watchdog extension ID, function IDs, and various attributes that are supported by the extension
- Patch 2 implements the kernel driver that utilizes standard SBI calls to interact and configure the attributes.
This is based on the OpenSBI watchdog extension implementation patch series available at:
Steps to test the SBI watchdog extension
1) Compile the kernel along with watchdog kselftest for RISC-V.
2) Compile OpenSBI patch series mentioned earlier to generate fw_dynamic.bin
3) Launch QEMU virt machine with IMSIC support enabled.
Note: The following command requires a rootfs.cpio packed in Image. It can be modified to use rootfs.ext2
$ qemu-system-riscv64 -M virt,aia=aplic-imsic -m 2G -smp 4 -nographic \
-bios fw_dynamic.bin -kernel Image -append "root=/dev/vda rw console=ttyS0"
4) On kernel boot-up, the /dev/watchdog entry should be available and the
kselftest binary (watchdog-test) can be used to test the functionality.
Following are some sample test cases, - Dumps all the supported options.
./watchdog-test -h
- Starts the watchdog, configures the timeout to 8s, ping interval to 2s, and disables the pretimeout.
./watchdog-test -t 8 -p 2 -n 0
- Starts the watchdog, configures the timeout to 8s, ping interval to 9s, and disables the pretimeout.
./watchdog-test -t 8 -p 9 -n 0
- Starts the watchdog, configures the timeout to 8s, ping interval to 2s, and configures the pretimeout to 6s before the timeout. ./watchdog-test -t 8 -p 3 -n 6
Changes in v4:
- Address review comment in the commit message
- Improve the comments in code to address Sashiko reviews
Changes in v3:
- Address Sashiko review comments
- Select WATCHDOG_CORE in the RISCV_SBI_WATCHDOG config section
- Appropriately return from error conditions when using dev_err_probe()
Changes in v2:
- Address Sashiko review comments
- Fix silent overflow in sec_to_usec() by setting max_timeout to U32_MAX / USEC_PER_SEC in probe, preventing timeouts greater than 4294 seconds from wrapping around silently.
- Add pretimeout validation in sbiwdt_set_timeout() to return -EINVAL if the new timeout is less than or equal to the current pretimeout.
- Advertise WDIOF_PRETIMEOUT dynamically at runtime by assigning sbiwdt_pretimeout_info to wdd.info only after MSI detection succeeds, rather than statically in watchdog_info, since CONFIG_RISCV_IMSIC=y does not guarantee MSI availability at runtime.
- Fix MSI interrupt leak on probe failure by registering platform_device_msi_free_irqs_all() via devm_add_action_or_reset() immediately after platform_device_msi_init_and_alloc_irqs() succeeds. This ensures all subsequent probe failure paths are automatically covered without explicit goto labels.
- Add module_exit() routine.
- Replace dev_err() with dev_err_probe() in probe() error paths for cleaner error reporting.
- Link to v1:
https://lore.kernel.org/linux-riscv/[email protected]/
- Sashiko review of v1
https://sashiko.dev/#/patchset/20261002163758.2919372-1-prajwal.n%40oss.qualcomm.com
Changes in v1:
- Initial RFC submission
Prajwal N (2):
RISC-V: Add defines for the RISC-V SBI watchdog extension drivers/watchdog: Add RISC-V SBI watchdog driver
arch/riscv/include/asm/sbi.h | 26 ++ drivers/watchdog/Kconfig | 10 + drivers/watchdog/Makefile | 1 + drivers/watchdog/riscv_sbi_wdt.c | 442 +++++++++++++++++++++++++++++++ 4 files changed, 479 insertions(+) create mode 100644 drivers/watchdog/riscv_sbi_wdt.c
Signature · 1 line
2.43.0
RFC PATCH v4 1/2 RISC-V: Add defines for the RISC-V SBI watchdog extension
Add the SBI watchdog extension ID and function IDs for supported operations, including start, stop, pat, and attribute read and write.
Define the attribute IDs exposed by the SBI watchdog extension, including watchdog capabilities such as MSI support and configurable watchdog parameters.
--- arch/riscv/include/asm/sbi.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
arch/riscv/include/asm/sbi.h +26 -0
@@ -40,6 +40,7 @@ enum sbi_ext_id {SBI_EXT_FWFT =0x46574654 ,SBI_EXT_MPXY =0x4D505859 ,SBI_EXT_DBTR =0x44425452 , +SBI_EXT_WDT =0x574454 ,/* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START = 0x08000000,
@@ -536,6 +537,31 @@ union sbi_dbtr_shmem_entry {struct sbi_dbtr_id_msg id; }; +/* SBI watchdog function IDs */ +enum sbi_ext_wdt_fid { +SBI_EXT_WATCHDOG_READ_ATTRIBUTE =0 , +SBI_EXT_WATCHDOG_WRITE_ATTRIBUTE , +SBI_EXT_WATCHDOG_WRITE_NOTIF_MSI_MESSAGE , +SBI_EXT_WATCHDOG_START , +SBI_EXT_WATCHDOG_STOP , +SBI_EXT_WATCHDOG_PAT , +}; + +/* SBI watchdog attributes */ +enum sbi_ext_wdt_attribute_id { +SBI_WATCHDOG_CAPABILITY =0x00000000 , +SBI_WATCHDOG_STATE =0x00000001 , +SBI_WATCHDOG_MIN_PERIOD =0x00000002 , +SBI_WATCHDOG_PERIOD =0x00000003 , +SBI_WATCHDOG_TIME_LEFT =0x00000004 , +SBI_WATCHDOG_NOTIF_TIME =0x00000005 , +SBI_WATCHDOG_NOTIF_MSI_ADDR_LOW =0x00000006 , +SBI_WATCHDOG_NOTIF_MSI_ADDR_HIGH =0x00000007 , +SBI_WATCHDOG_NOTIF_MSI_DATA =0x00000008 , +SBI_WATCHDOG_ATTR_MAX =0x00000009 , +SBI_WATCHDOG_ATTR_RESERVED_MAX =0xFFFFFFFF +}; +/* SBI spec version fields */ #define SBI_SPEC_VERSION_DEFAULT 0x1#define SBI_SPEC_VERSION_MAJOR_SHIFT 24
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [RFC PATCH v4 1/2] RISC-V: Add defines for the RISC-V SBI watchdog extension
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] riscv: sbi: inconsistent WDT vs WATCHDOG naming in asm/sbi.h --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] riscv: sbi: inconsistent WDT vs WATCHDOG naming in asm/sbi.h --
commit 0a75cf9d3428dfcabb80b08ac219176e7b70a095
Author: Prajwal N <[email protected]>
RISC-V: Add defines for the RISC-V SBI watchdog extension
This commit adds the extension ID, function IDs, and attribute IDs for the RISC-V SBI watchdog extension to arch/riscv/include/asm/sbi.h.
Quoted 30 lines
arch/riscv/include/asm/sbi.h +15
-0@@ -40,6 +40,7 @@ enum sbi_ext_id {SBI_EXT_FWFT =0x46574654 ,SBI_EXT_MPXY =0x4D505859 ,SBI_EXT_DBTR =0x44425452 , +SBI_EXT_WDT =0x574454 ,/* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START = 0x08000000,@@ -536,6 +537,31 @@ union sbi_dbtr_shmem_entry {struct sbi_dbtr_id_msg id; }; +/* SBI watchdog function IDs */ +enum sbi_ext_wdt_fid { +SBI_EXT_WATCHDOG_READ_ATTRIBUTE =0 , +SBI_EXT_WATCHDOG_WRITE_ATTRIBUTE , +SBI_EXT_WATCHDOG_WRITE_NOTIF_MSI_MESSAGE , +SBI_EXT_WATCHDOG_START , +SBI_EXT_WATCHDOG_STOP , +SBI_EXT_WATCHDOG_PAT , +}; + +/* SBI watchdog attributes */ +enum sbi_ext_wdt_attribute_id { +SBI_WATCHDOG_CAPABILITY =0x00000000 , +SBI_WATCHDOG_STATE =0x00000001 ,
arch/riscv/include/asm/sbi.h union sbi_dbtr_shmem_entry { · line 563
Quoted 10 lines
arch/riscv/include/asm/sbi.h +10
-0+SBI_WATCHDOG_MIN_PERIOD =0x00000002 , +SBI_WATCHDOG_PERIOD =0x00000003 , +SBI_WATCHDOG_TIME_LEFT =0x00000004 , +SBI_WATCHDOG_NOTIF_TIME =0x00000005 , +SBI_WATCHDOG_NOTIF_MSI_ADDR_LOW =0x00000006 , +SBI_WATCHDOG_NOTIF_MSI_ADDR_HIGH =0x00000007 , +SBI_WATCHDOG_NOTIF_MSI_DATA =0x00000008 , +SBI_WATCHDOG_ATTR_MAX =0x00000009 , +SBI_WATCHDOG_ATTR_RESERVED_MAX =0xFFFFFFFF +};
[Severity: Low]
This isn't a bug, but the other extensions in asm/sbi.h use the same acronym across SBI_EXT_<EXT>, enum sbi_ext_<ext>_fid, SBI_EXT_<EXT>_*, and SBI_<EXT>_*. Here the extension ID and enum names use WDT / wdt while the enumerators use SBI_EXT_WATCHDOG_* and SBI_WATCHDOG_* instead of SBI_EXT_WDT_* and SBI_WDT_*.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RFC PATCH v4 2/2 drivers/watchdog: Add RISC-V SBI watchdog driver
In reply to Prajwal NAdd a watchdog driver for the RISC-V SBI watchdog extension. The driver integrates with the Linux watchdog framework to configure and control the watchdog. The driver invokes the SBI watchdog using the standard SBI calls.
Support pretimeout event notifications using Message Signaled Interrupts (MSIs).
--- drivers/watchdog/Kconfig | 10 + drivers/watchdog/Makefile | 1 + drivers/watchdog/riscv_sbi_wdt.c | 442 +++++++++++++++++++++++++++++++ 3 files changed, 453 insertions(+) create mode 100644 drivers/watchdog/riscv_sbi_wdt.c
drivers/watchdog/Kconfig +10 -0
@@ -2164,6 +2164,16 @@ config STARFIVE_WATCHDOG Say Y here to support the watchdog of StarFive JH7100 and JH7110 SoC. This driver can also be built as a module if choose M. +config RISCV_SBI_WATCHDOG + tristate "RISC-V SBI based Watchdog support" + depends on RISCV_SBI + default RISCV + select WATCHDOG_CORE + help + Say Y here to include support for a watchdog timer + implemented by the Supervisor Binary Interface on RISCV platforms. + Requires firmware support. + # S390 Architecture config DIAG288_WATCHDOG
drivers/watchdog/Makefile +1 -0
@@ -202,6 +202,7 @@ obj-$(CONFIG_WATCHDOG_RTAS) += wdrtas.o # RISC-V Architecture obj-$(CONFIG_ATCWDT200_WATCHDOG) += atcwdt200_wdt.o obj-$(CONFIG_STARFIVE_WATCHDOG) += starfive-wdt.o +obj-$(CONFIG_RISCV_SBI_WATCHDOG) += riscv_sbi_wdt.o # S390 Architecture obj-$(CONFIG_DIAG288_WATCHDOG) += diag288_wdt.o
drivers/watchdog/riscv_sbi_wdt.c new file +442 -0
442 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [RFC PATCH v4 2/2] drivers/watchdog: Add RISC-V SBI watchdog driver
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] watchdog: riscv_sbi_wdt: unclamped timeout bounds in sbiwdt_init_attributes() - [Medium] watchdog…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] watchdog: riscv_sbi_wdt: unclamped timeout bounds in sbiwdt_init_attributes()
- [Medium] watchdog: riscv_sbi_wdt: stale firmware pretimeout in sbiwdt_init_attributes()
- [Medium] watchdog: riscv_sbi_wdt: missing running state check in sbiwdt_probe()
- [Medium] watchdog: riscv_sbi_wdt: probe fails in sbiwdt_probe() without S-mode IMSIC
--
commit 7154e44eab330baa1ded9a131b702c04ae8a2530
Author: Prajwal N <[email protected]>
drivers/watchdog: Add RISC-V SBI watchdog driver
This commit adds a Linux watchdog driver for the RISC-V SBI watchdog extension, including MSI-based pretimeout notification support.
Quoted 5 lines
drivers/watchdog/riscv_sbi_wdt.c new file +0
-0
[ ... ]
drivers/watchdog/riscv_sbi_wdt.c line 279
Quoted 13 lines
drivers/watchdog/riscv_sbi_wdt.c new file +13
-0+/* Initialize the watchdog attributes */ +static int sbiwdt_init_attributes (struct sbiwdt *wdt) +{ +int ret; +u32 attr_val; +u32 timeout_usec; + +/* Read watchdog capability from firmware */ + ret =sbiwdt_read_attribute (SBI_WATCHDOG_CAPABILITY , &attr_val); +if (ret) +return ret; + + wdt->attrs.wdt_capability = attr_val;
[Severity: Medium]
As reported by Guenter Roeck on RFC v3 [1], should sbiwdt_init_attributes() check SBI_WATCHDOG_STATE and either stop an active watchdog or set WDOG_HW_RUNNING on wdt->wdd.status?
wdt_state is declared in struct sbiwdt_attributes, but SBI_WATCHDOG_STATE is never read. If the bootloader, firmware, or a pre-kexec kernel leaves the watchdog running, writing SBI_WATCHDOG_PERIOD below can fail in firmware, or watchdog_register_device() will not start the kernel keepalive worker before userspace opens /dev/watchdog.
Quoted 14 lines
drivers/watchdog/riscv_sbi_wdt.c new file +14
-0+ +/* +* Read watchdog min period supported in firmware +* The firmware ensures the MIN_PERIOD is in valid range +*/ + ret =sbiwdt_read_attribute (SBI_WATCHDOG_MIN_PERIOD , &attr_val); +if (ret) +return ret; + +if (!attr_val) + attr_val =1 ; + + wdt->attrs.wdt_min_period = attr_val; + wdt->wdd.min_timeout =usec_to_sec_ceil (wdt->attrs.wdt_min_period);
drivers/watchdog/riscv_sbi_wdt.c line 303
Quoted 10 lines
drivers/watchdog/riscv_sbi_wdt.c new file +10
-0+ +/* Initialize the watchdog timeout to default value */ + timeout_usec =sec_to_usec (SBI_WATCHDOG_DEFAULT_TIMEOUT ); + ret =sbiwdt_write_attribute (SBI_WATCHDOG_PERIOD , timeout_usec); +if (ret) +return ret; + + wdt->attrs.wdt_period = timeout_usec; + wdt->wdd.timeout =SBI_WATCHDOG_DEFAULT_TIMEOUT ; + wdt->wdd.max_timeout =U32_MAX /USEC_PER_SEC ;
[Severity: Medium]
As reported by Guenter Roeck on RFC v3 [2], what happens if firmware reports SBI_WATCHDOG_MIN_PERIOD greater than 60 seconds, or near U32_MAX?
Writing an unclamped 60-second SBI_WATCHDOG_DEFAULT_TIMEOUT to SBI_WATCHDOG_PERIOD will be rejected by firmware when wdt_min_period exceeds 60,000,000 us, failing probe. Also, if wdt_min_period is between 4,294,000,001 and U32_MAX, usec_to_sec_ceil() rounds min_timeout up to 4295 seconds while max_timeout is 4294 seconds, causing watchdog_check_min_max_timeout() to reset both bounds to 0 and allowing userspace timeouts that overflow u32 in sec_to_usec().
drivers/watchdog/riscv_sbi_wdt.c line 313
Quoted 10 lines
drivers/watchdog/riscv_sbi_wdt.c new file +10
-0+ +/* +* Initialize the pretimeout to default value +* The firmware ensures the pretimeout init value is 0 +*/ + wdt->attrs.wdt_notif_time =0 ; + wdt->wdd.pretimeout =0 ; + +return 0 ; +}
[Severity: Medium]
Does sbiwdt_init_attributes() leave a stale SBI_WATCHDOG_NOTIF_TIME programmed in firmware across module reload, driver rebind, or kexec?
Neither sbiwdt_stop() nor driver teardown clears SBI_WATCHDOG_NOTIF_TIME in firmware, and sbiwdt_init_attributes() only zeroes wdt->wdd.pretimeout in memory after writing SBI_WATCHDOG_PERIOD. If a previous instance configured a pretimeout of 60 seconds or more, writing the 60-second default period fails probe; if it configured 1 to 59 seconds, firmware keeps firing unexpected pretimeout MSIs and rejects later sbiwdt_set_timeout() calls at or below the stale firmware pretimeout. Writing 0 to SBI_WATCHDOG_NOTIF_TIME before programming SBI_WATCHDOG_PERIOD would keep software and firmware in sync.
[ ... ]
Quoted 2 lines
drivers/watchdog/riscv_sbi_wdt.c new file +2
-0+static int sbiwdt_probe (struct platform_device *pdev) +{
[ ... ]
drivers/watchdog/riscv_sbi_wdt.c line 360
Quoted 12 lines
drivers/watchdog/riscv_sbi_wdt.c new file +12
-0+/* Setup MSI if the capability is available */ +if (wdt->attrs.wdt_capability &SBI_WATCHDOG_CAP_MSI_SUPPORT_MASK ) { + imsic_global =imsic_get_global_config (); +if (!imsic_global) +return dev_err_probe (dev, -ENODEV ,"IMSIC firmware node is unavailable\n" ); + + msi_domain =irq_find_matching_fwnode (imsic_global->fwnode, +DOMAIN_BUS_PLATFORM_MSI ); + +if (!msi_domain) +return dev_err_probe (dev, -ENODEV , +"IMSIC platform MSI domain is unavailable\n" );
[Severity: Medium]
Should a missing S-mode IMSIC or platform MSI domain abort the entire watchdog probe when firmware sets SBI_WATCHDOG_CAP_MSI_SUPPORT_MASK?
On platforms or guests without an S-mode IMSIC platform MSI domain (such as systems using PLIC or wired APLIC), returning -ENODEV here prevents basic watchdog registration instead of falling back to non-pretimeout operation with sbiwdt_info.
[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Guenter Roeck
maintainerRe: [RFC PATCH v4 2/2] drivers/watchdog: Add RISC-V SBI watchdog driver
Quoted 45 lines · On 11/24/26 05:37, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] watchdog:riscv_sbi_wdt: unclamped timeout bounds insbiwdt_init_attributes()
- [Medium] watchdog:riscv_sbi_wdt: stale firmware pretimeout insbiwdt_init_attributes()
- [Medium] watchdog:riscv_sbi_wdt: missing running state check insbiwdt_probe()
- [Medium] watchdog:riscv_sbi_wdt: probe fails insbiwdt_probe()without S-mode IMSIC
--commit
7154e44eab330baa1ded9a131b702c04ae8a2530
Author: Prajwal N <[email protected]>
drivers/watchdog: Add RISC-V SBI watchdog driverThis commit adds a Linux watchdog driver for the RISC-V SBI watchdog extension, including MSI-based pretimeout notification support.
Quoted 5 lines
drivers/watchdog/riscv_sbi_wdt.c new file +0
-0[ ... ]
drivers/watchdog/riscv_sbi_wdt.c line 279
Quoted 13 lines
drivers/watchdog/riscv_sbi_wdt.c new file +13
-0+/* Initialize the watchdog attributes */ +static int sbiwdt_init_attributes (struct sbiwdt *wdt) +{ +int ret; +u32 attr_val; +u32 timeout_usec; + +/* Read watchdog capability from firmware */ + ret =sbiwdt_read_attribute (SBI_WATCHDOG_CAPABILITY , &attr_val); +if (ret) +return ret; + + wdt->attrs.wdt_capability = attr_val;[Severity: Medium]
As reported by Guenter Roeck on RFC v3 [1], shouldsbiwdt_init_attributes()checkSBI_WATCHDOG_STATEand either stop an active watchdog or setWDOG_HW_RUNNINGonwdt->wdd.status?
Quoted 6 lines
wdt_stateis declared in structsbiwdt_attributes, butSBI_WATCHDOG_STATEis never read. If the bootloader, firmware, or a pre-kexec kernel leaves the watchdog running, writingSBI_WATCHDOG_PERIODbelow can fail in firmware, orwatchdog_register_device()will not start the kernel keepalive worker before userspace opens/dev/watchdog.
If you don't think that is necessary, please add a comment into the code explaining the reason.
Quoted 29 lines
Quoted 14 lines
drivers/watchdog/riscv_sbi_wdt.c new file +14
-0+ +/* +* Read watchdog min period supported in firmware +* The firmware ensures the MIN_PERIOD is in valid range +*/ + ret =sbiwdt_read_attribute (SBI_WATCHDOG_MIN_PERIOD , &attr_val); +if (ret) +return ret; + +if (!attr_val) + attr_val =1 ; + + wdt->attrs.wdt_min_period = attr_val; + wdt->wdd.min_timeout =usec_to_sec_ceil (wdt->attrs.wdt_min_period);drivers/watchdog/riscv_sbi_wdt.c line 303
Quoted 10 lines
drivers/watchdog/riscv_sbi_wdt.c new file +10
-0+ +/* Initialize the watchdog timeout to default value */ + timeout_usec =sec_to_usec (SBI_WATCHDOG_DEFAULT_TIMEOUT ); + ret =sbiwdt_write_attribute (SBI_WATCHDOG_PERIOD , timeout_usec); +if (ret) +return ret; + + wdt->attrs.wdt_period = timeout_usec; + wdt->wdd.timeout =SBI_WATCHDOG_DEFAULT_TIMEOUT ; + wdt->wdd.max_timeout =U32_MAX /USEC_PER_SEC ;[Severity: Medium]
As reported by Guenter Roeck on RFC v3 [2], what happens if firmware reportsSBI_WATCHDOG_MIN_PERIODgreater than 60 seconds, or nearU32_MAX?
Quoted 8 lines
Writing an unclamped 60-second
SBI_WATCHDOG_DEFAULT_TIMEOUTtoSBI_WATCHDOG_PERIODwill be rejected by firmware whenwdt_min_periodexceeds 60,000,000 us, failing probe. Also, ifwdt_min_periodis between 4,294,000,001 andU32_MAX,usec_to_sec_ceil()roundsmin_timeoutup to 4295 seconds whilemax_timeoutis 4294 seconds, causingwatchdog_check_min_max_timeout()to reset both bounds to 0 and allowing userspace timeouts that overflow u32 insec_to_usec().
If you don't think it is necessary to clamp wdt_min_period, please at least add a comment explaining the reason. Note though that "it won't happen" is not an argument or reason.
Thanks,
Guenter