watchdog: Add reboot API
Subsystems (5)
Guenter Roeck
maintainerv2 cover watchdog: Add reboot API
Some hardware implements reboot through its watchdog hardware, for example by triggering a watchdog timeout or by writing into its watchdog register set. Platform specific code starts to spread into watchdog drivers, typically by setting pointers to a callback function which is then called from the architecture's reset handler.
While global and exported callback function pointers (such as arm_pm_restart) may be acceptable as long as they are used from platform and/or architecture code, using such a mechanism across subsystems and drivers is less than desirable. Ultimately, we'll need a better solution.
This patch series provides such a solution. It extends the watchdog subsystem to support reboot functionality, provides an API function call to trigger reboots, adds support for the new API to arm and arm64, and converts the drivers providing reboot functionality to use the new infrastructure.
The first patch in the series implements the new API. The second and third patch modify the arm and arm64 architecture reset handlers to call the added API function. The final two patches register the reboot handlers in the sunxi and moxart watchdog drivers with the watchdog subsystem.
The sunxi patch depends on the most recent patch series sumitted by Maxime Ripard.
Note that I did not address the comments asking for support of multiple watchdogs with reset handlers, nor the request to add a flag to provide 'preferential' treatment for one of those watchdogs. This will require additional discussion and needs to be addressed with a later patch if needed. Key questions are how to add such support for non-DT systems, and if the scope of restart handling is a function of the driver or of the system. Also, if one of the watchdogs does not result in a complete system reset but another one does, it is not clear to me why the less-than-perfect watchdog would be configured in the first place (or how this situation would be handled today).
Compile tested only for arm64. Tested on arm/moxart by Jonas Jensen.
Guenter Roeck
maintainerPATCH v2 1/5 watchdog: Add API to trigger reboots
Some hardware implements reboot through its watchdog hardware, for example by triggering a watchdog timeout. Platform specific code starts to spread into watchdog drivers, typically by setting pointers to a callback functions which is then called from the platform reset handler.
To simplify code and provide a unified API to trigger reboots by watchdog drivers, provide a single API to trigger such reboots through the watchdog subsystem.
---
v2: Remove unnecessary check for ops in reboot function
drivers/watchdog/watchdog_core.c | 15 +++++++++++++++ include/linux/watchdog.h | 11 +++++++++++ 2 files changed, 26 insertions(+)
drivers/watchdog/watchdog_core.c +15 -0
@@ -43,6 +43,15 @@static DEFINE_IDA (watchdog_ida);static struct class *watchdog_class; +static struct watchdog_device *wdd_reboot_dev; + +void watchdog_do_reboot (enum reboot_mode mode,const char *cmd) +{ +if (wdd_reboot_dev) + wdd_reboot_dev->ops->reboot (wdd_reboot_dev, mode, cmd); +} +EXPORT_SYMBOL (watchdog_do_reboot); +static void watchdog_check_min_max_timeout (struct watchdog_device *wdd) {/*
@@ -162,6 +171,9 @@ int watchdog_register_device(struct watchdog_device *wdd)return ret; } +if (wdd->ops->reboot) + wdd_reboot_dev = wdd; +return 0 ; }EXPORT_SYMBOL_GPL (watchdog_register_device);
@@ -181,6 +193,9 @@ void watchdog_unregister_device(struct watchdog_device *wdd)if (wdd ==NULL )return ; +if (wdd == wdd_reboot_dev) + wdd_reboot_dev =NULL ; + devno = wdd->cdev.dev; ret =watchdog_dev_unregister (wdd);if (ret)
include/linux/watchdog.h +11 -0
@@ -12,6 +12,7 @@#include <linux/bitops.h> #include <linux/device.h> #include <linux/cdev.h> +#include <linux/reboot.h> #include <uapi/linux/watchdog.h> struct watchdog_ops ;
@@ -23,6 +24,7 @@ struct watchdog_device;* @start: The routine for starting the watchdog device. * @stop: The routine for stopping the watchdog device. * @ping: The routine that sends a keepalive ping to the watchdog device. +* @reboot: The routine for rebooting the system * @status: The routine that shows the status of the watchdog device. * @set_timeout:The routine for setting the watchdog devices timeout value. * @get_timeleft:The routine that get's the time that's left before a reset.
@@ -42,6 +44,8 @@ struct watchdog_ops {int (*stop)(struct watchdog_device *);/* optional operations */ int (*ping)(struct watchdog_device *); +void (*reboot)(struct watchdog_device *,enum reboot_mode , +const char *);unsigned int (*status)(struct watchdog_device *);int (*set_timeout)(struct watchdog_device *,unsigned int );unsigned int (*get_timeleft)(struct watchdog_device *);
@@ -142,4 +146,11 @@ extern int watchdog_init_timeout(struct watchdog_device *wdd,extern int watchdog_register_device (struct watchdog_device *);extern void watchdog_unregister_device (struct watchdog_device *); +#ifdef CONFIG_WATCHDOG_CORE +extern void watchdog_do_reboot (enum reboot_mode mode,const char *cmd); +#else +static inline void watchdog_do_reboot (enum reboot_mode mode, +const char *cmd) { } +#endif +#endif /* ifndef _LINUX_WATCHDOG_H */
Signature · 1 line
1.9.1
Guenter Roeck
maintainerPATCH v2 2/5 arm64: Support reboot through watchdog subsystem
In reply to Guenter RoeckThe watchdog subsystem provides an API to perform a system reboot. Use it.
---
v2: No change
arch/arm64/kernel/process.c | 3 +++ 1 file changed, 3 insertions(+)
arch/arm64/kernel/process.c +3 -0
@@ -42,6 +42,7 @@#include <linux/hw_breakpoint.h> #include <linux/personality.h> #include <linux/notifier.h> +#include <linux/watchdog.h> #include <asm/compat.h> #include <asm/cacheflush.h>
@@ -144,6 +145,8 @@ void machine_restart(char *cmd)if (arm_pm_restart)arm_pm_restart (reboot_mode, cmd); +watchdog_do_reboot (reboot_mode, cmd); +/* * Whoops - the architecture was unable to reboot. */
Signature · 1 line
1.9.1
Guenter Roeck
maintainerRe: [PATCH v2 2/5] arm64: Support reboot through watchdog subsystem
Quoted 4 lines · Guenter Roeck
The watchdog subsystem provides an API to perform a system reboot. Use it.
Signed-off-by Guenter Roeck <[email protected]>
Any comments / Ack from the arm maintainers ?
Thanks,
Guenter
Quoted 28 lines
---
v2: No changearch/arm64/kernel/process.c | 3 +++ 1 file changed, 3 insertions(+)arch/arm64/kernel/process.c +3
-0@@ -42,6 +42,7 @@#include <linux/hw_breakpoint.h> #include <linux/personality.h> #include <linux/notifier.h> +#include <linux/watchdog.h> #include <asm/compat.h> #include <asm/cacheflush.h> @@ -144,6 +145,8 @@ void machine_restart(char *cmd)if (arm_pm_restart)arm_pm_restart (reboot_mode, cmd); +watchdog_do_reboot (reboot_mode, cmd); +/* * Whoops - the architecture was unable to reboot. */
Re: [PATCH v2 2/5] arm64: Support reboot through watchdog subsystem
Quoted 7 lines · Guenter Roeck
Quoted 4 lines · Guenter Roeck
The watchdog subsystem provides an API to perform a system reboot. Use it.
Signed-off-by Guenter Roeck <[email protected]>Any comments / Ack from the arm maintainers ?
I'm fine with the arm/arm64 changes, but I still reckon the parameters should be dropped unless you have a use for them.
Will
Guenter Roeck
maintainerRe: [PATCH v2 2/5] arm64: Support reboot through watchdog subsystem
Quoted 11 lines · Will Deacon
Quoted 7 lines · Guenter Roeck
Quoted 4 lines · Guenter Roeck
The watchdog subsystem provides an API to perform a system reboot. Use it.
Signed-off-by Guenter Roeck <[email protected]>Any comments / Ack from the arm maintainers ?
I'm fine with the arm/arm64 changes, but I still reckon the parameters should be dropped unless you have a use for them.
Ok, I'll drop the parameters and send a new version.
Guenter
Guenter Roeck
maintainerPATCH v2 3/5 arm: Support reboot through watchdog subsystem
In reply to Guenter RoeckThe watchdog subsystem provides an API to perform a system reboot. Use it.
With this change, the arm_pm_restart callback is now optional, so check if it is set before calling it.
---
v2: No change
arch/arm/kernel/process.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/arm/kernel/process.c +5 -1
@@ -32,6 +32,7 @@#include <linux/hw_breakpoint.h> #include <linux/leds.h> #include <linux/reboot.h> +#include <linux/watchdog.h> #include <asm/cacheflush.h> #include <asm/idmap.h>
@@ -230,7 +231,10 @@ void machine_restart(char *cmd)local_irq_disable ();smp_send_stop (); -arm_pm_restart (reboot_mode, cmd); +if (arm_pm_restart) +arm_pm_restart (reboot_mode, cmd); + +watchdog_do_reboot (reboot_mode, cmd);/* Give a grace period for failure to restart of 1s */ mdelay (1000 );
Signature · 1 line
1.9.1
Guenter Roeck
maintainerRe: [PATCH v2 3/5] arm: Support reboot through watchdog subsystem
Quoted 8 lines · Guenter Roeck
The watchdog subsystem provides an API to perform a system reboot. Use it.
With this change, the
arm_pm_restartcallback is now optional, so check if it is set before calling it.Tested-by Jonas Jensen <[email protected]>Signed-off-by Guenter Roeck <[email protected]>
Any comments / ack from the arm maintainers ?
Thanks,
Guenter
Quoted 31 lines
---
v2: No changearch/arm/kernel/process.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)arch/arm/kernel/process.c +5
-1@@ -32,6 +32,7 @@#include <linux/hw_breakpoint.h> #include <linux/leds.h> #include <linux/reboot.h> +#include <linux/watchdog.h> #include <asm/cacheflush.h> #include <asm/idmap.h> @@ -230,7 +231,10 @@ void machine_restart(char *cmd)local_irq_disable ();smp_send_stop (); -arm_pm_restart (reboot_mode, cmd); +if (arm_pm_restart) +arm_pm_restart (reboot_mode, cmd); + +watchdog_do_reboot (reboot_mode, cmd);/* Give a grace period for failure to restart of 1s */ mdelay (1000 );
Guenter Roeck
maintainerPATCH v2 4/5 watchdog: moxart: Register reboot handler with watchdog subsystem
In reply to Guenter RoeckThe watchdog subsystem now provides an API to trigger a system reboot. Register with it.
---
v2: No change
drivers/watchdog/moxart_wdt.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-)
drivers/watchdog/moxart_wdt.c +8 -12
@@ -19,8 +19,6 @@#include <linux/watchdog.h> #include <linux/moduleparam.h> -#include <asm/system_misc.h> -#define REG_COUNT 0x4#define REG_MODE 0x8#define REG_ENABLE 0xC
@@ -31,15 +29,16 @@ struct moxart_wdt_dev {unsigned int clock_frequency; }; -static struct moxart_wdt_dev *moxart_restart_ctx; -static int heartbeat; -static void moxart_wdt_restart (enum reboot_mode reboot_mode,const char *cmd) +static void moxart_wdt_reboot (struct watchdog_device *wdt_dev, +enum reboot_mode mode,const char *cmd) { -writel (1 , moxart_restart_ctx->base +REG_COUNT ); -writel (0x5ab9 , moxart_restart_ctx->base +REG_MODE ); -writel (0x03 , moxart_restart_ctx->base +REG_ENABLE ); +struct moxart_wdt_dev *moxart_wdt =watchdog_get_drvdata (wdt_dev); + +writel (1 , moxart_wdt->base +REG_COUNT ); +writel (0x5ab9 , moxart_wdt->base +REG_MODE ); +writel (0x03 , moxart_wdt->base +REG_ENABLE ); }static int moxart_wdt_stop (struct watchdog_device *wdt_dev)
@@ -81,6 +80,7 @@ static const struct watchdog_ops moxart_wdt_ops = { .owner = THIS_MODULE, .start = moxart_wdt_start, .stop = moxart_wdt_stop, + .reboot = moxart_wdt_reboot, .set_timeout = moxart_wdt_set_timeout, };
@@ -136,9 +136,6 @@ static int moxart_wdt_probe(struct platform_device *pdev)if (err)return err; - moxart_restart_ctx = moxart_wdt; - arm_pm_restart = moxart_wdt_restart; -dev_dbg (dev,"Watchdog enabled (heartbeat=%d sec, nowayout=%d)\n" , moxart_wdt->dev.timeout, nowayout);
@@ -149,7 +146,6 @@ static int moxart_wdt_remove(struct platform_device *pdev) {struct moxart_wdt_dev *moxart_wdt =platform_get_drvdata (pdev); - arm_pm_restart =NULL ;moxart_wdt_stop (&moxart_wdt->dev);watchdog_unregister_device (&moxart_wdt->dev);
Signature · 1 line
1.9.1
Guenter Roeck
maintainerPATCH v2 5/5 watchdog: sunxi: Register reboot handler with watchdog subsystem
In reply to Guenter RoeckThe watchdog subsystem now provides an API to trigger a system reboot. Register with it.
---
v2: Rebased to v2 of Maxime's patch adding reboot support
to the sunxi watchdog driver.
drivers/watchdog/sunxi_wdt.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-)
drivers/watchdog/sunxi_wdt.c +9 -13
@@ -27,8 +27,6 @@#include <linux/types.h> #include <linux/watchdog.h> -#include <asm/system_misc.h> -#define WDT_MAX_TIMEOUT 16#define WDT_MIN_TIMEOUT 1#define WDT_MODE_TIMEOUT (n) ((n) << 3)
@@ -74,23 +72,25 @@ static const int wdt_timeout_map[] = { [16] = 0xB, /* 16s */ }; -static void __iomem *reboot_wdt_base; - -static void sun4i_wdt_restart (enum reboot_mode mode,const char *cmd) +static void sunxi_wdt_reboot (struct watchdog_device *wdt_dev, +enum reboot_mode mode,const char *cmd) { +struct sunxi_wdt_dev *sunxi_wdt =watchdog_get_drvdata (wdt_dev); +/* Enable timer and set reset bit in the watchdog */ -writel (WDT_MODE_EN |WDT_MODE_RST_EN , reboot_wdt_base +WDT_MODE ); +writel (WDT_MODE_EN |WDT_MODE_RST_EN , + sunxi_wdt->wdt_base +WDT_MODE );/* * Restart the watchdog. The default (and lowest) interval * value for the watchdog is 0.5s. */ -writel (WDT_CTRL_RELOAD , reboot_wdt_base +WDT_CTRL ); +writel (WDT_CTRL_RELOAD , sunxi_wdt->wdt_base +WDT_CTRL );while (1 ) {mdelay (5 );writel (WDT_MODE_EN |WDT_MODE_RST_EN , - reboot_wdt_base +WDT_MODE ); + sunxi_wdt->wdt_base +WDT_MODE ); } }
@@ -167,6 +167,7 @@ static const struct watchdog_ops sunxi_wdt_ops = { .start = sunxi_wdt_start, .stop = sunxi_wdt_stop, .ping = sunxi_wdt_ping, + .reboot = sunxi_wdt_reboot, .set_timeout = sunxi_wdt_set_timeout, };
@@ -205,9 +206,6 @@ static int sunxi_wdt_probe(struct platform_device *pdev)if (unlikely (err))return err; - reboot_wdt_base = sunxi_wdt->wdt_base; - arm_pm_restart = sun4i_wdt_restart; -dev_info (&pdev->dev,"Watchdog enabled (timeout=%d sec, nowayout=%d)" , sunxi_wdt->wdt_dev.timeout, nowayout);
@@ -218,8 +216,6 @@ static int sunxi_wdt_remove(struct platform_device *pdev) {struct sunxi_wdt_dev *sunxi_wdt =platform_get_drvdata (pdev); - arm_pm_restart =NULL ; -watchdog_unregister_device (&sunxi_wdt->wdt_dev);watchdog_set_drvdata (&sunxi_wdt->wdt_dev,NULL );
Signature · 1 line
1.9.1
Randy Dunlap
reviewerQuoted 5 lines · Guenter Roeck
Some hardware implements reboot through its watchdog hardware, for example by triggering a watchdog timeout or by writing into its watchdog register set. Platform specific code starts to spread into watchdog drivers, typically by setting pointers to a callback function which is then called from the architecture's reset handler.
No updates to Documentation/watchdog/ ?
Signature · 1 line
~Randy
Guenter Roeck
maintainerQuoted 9 lines · Randy Dunlap
Quoted 5 lines · Guenter Roeck
Some hardware implements reboot through its watchdog hardware, for example by triggering a watchdog timeout or by writing into its watchdog register set. Platform specific code starts to spread into watchdog drivers, typically by setting pointers to a callback function which is then called from the architecture's reset handler.
No updates to
Documentation/watchdog/?
You are absolutely right, I missed that completely. Will follow in v3.
Thanks,
Guenter
Guenter Roeck
maintainer[PATCH 6/5] watchdog: Document reboot API
In reply to Randy DunlapDocument the new reboot API functionality.
--- Documentation/watchdog/watchdog-kernel-api.txt | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
Documentation/watchdog/watchdog-kernel-api.txt +14 -0
@@ -90,6 +90,8 @@ struct watchdog_ops { int (*stop)(struct watchdog_device *); /* optional operations */ int (*ping)(struct watchdog_device *); + void (*reboot)(struct watchdog_device *, enum reboot_mode, + const char *); unsigned int (*status)(struct watchdog_device *); int (*set_timeout)(struct watchdog_device *, unsigned int); unsigned int (*get_timeleft)(struct watchdog_device *);
@@ -148,6 +150,10 @@ they are supported. These optional routines/operations are: info structure). * status: this routine checks the status of the watchdog timer device. The status of the device is reported with watchdog WDIOF_* status flags/bits. +* reboot: if this routine is present, it may be called to reboot the system. + Parameters are the pointer to the watchdog device, the reboot mode, and a + command string. The latter two parameters are the system reboot mode + and the cmd parameter passed to machine_restart(). * set_timeout: this routine checks and changes the timeout of the watchdog timer device. It returns 0 on success, -EINVAL for "parameter out of range" and -EIO for "could not write value to the watchdog". On success this
@@ -224,3 +230,11 @@ the device tree (if the module timeout parameter is invalid). Best practice is to set the default timeout value as timeout value in the watchdog_device and then use this function to set the user "preferred" timeout value. This routine returns zero on success and a negative errno code for failure. + +The watchdog_do_reboot function can be used reboot the system. This is intended +to be used in systems which do not have an explicit reboot capability, but +implement reboot by programming the watchdog to expire immediately. If the +function is called, and a watchdog driver with reboot functionality was +previously registered, the reboot function of that driver will be called. +Architecture code should call watchdog_do_reboot from its machine_reboot +function after other means to reboot the system failed.
Signature · 1 line
1.9.1
Randy Dunlap
reviewerRe: [PATCH 6/5] watchdog: Document reboot API
Quoted 32 lines · Guenter Roeck
Document the new reboot API functionality.
Signed-off-by Guenter Roeck <[email protected]>--- Documentation/watchdog/watchdog-kernel-api.txt | 14 ++++++++++++++ 1 file changed, 14 insertions(+)Documentation/watchdog/watchdog-kernel-api.txt +6
-0@@ -90,6 +90,8 @@ struct watchdog_ops { int (*stop)(struct watchdog_device *); /* optional operations */ int (*ping)(struct watchdog_device *); + void (*reboot)(struct watchdog_device *, enum reboot_mode, + const char *); unsigned int (*status)(struct watchdog_device *); int (*set_timeout)(struct watchdog_device *, unsigned int); unsigned int (*get_timeleft)(struct watchdog_device *);@@ -148,6 +150,10 @@ they are supported. These optional routines/operations are: info structure). * status: this routine checks the status of the watchdog timer device. The status of the device is reported with watchdog WDIOF_* status flags/bits. +* reboot: if this routine is present, it may be called to reboot the system. + Parameters are the pointer to the watchdog device, the reboot mode, and a + command string. The latter two parameters are the system reboot mode + and the cmd parameter passed to machine_restart(). * set_timeout: this routine checks and changes the timeout of the watchdog timer device. It returns 0 on success, -EINVAL for "parameter out of range" and -EIO for "could not write value to the watchdog". On success this
Documentation/watchdog/watchdog-kernel-api.txt the device tree (if the module timeout parameter is invalid). Best practice is · line 234
Quoted 5 lines
Documentation/watchdog/watchdog-kernel-api.txt +2
-0@@ -224,3 +230,11 @@ the device tree (if the module timeout parameter is invalid). Best practice is to set the default timeout value as timeout value in the watchdog_device and then use this function to set the user "preferred" timeout value. This routine returns zero on success and a negative errno code for failure. + +The watchdog_do_reboot function can be used reboot the system. This is intended
to reboot
Documentation/watchdog/watchdog-kernel-api.txt the device tree (if the module timeout parameter is invalid). Best practice is · line 240
Quoted 7 lines
Documentation/watchdog/watchdog-kernel-api.txt +6
-0+to be used in systems which do not have an explicit reboot capability, but +implement reboot by programming the watchdog to expire immediately. If the +function is called, and a watchdog driver with reboot functionality was +previously registered, the reboot function of that driver will be called. +Architecture code should call watchdog_do_reboot from its machine_reboot +function after other means to reboot the system failed.
Thanks for the Doc patch.
Signature · 1 line
~Randy