1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Generic watchdog defines. Derived from..
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Berkshire PC Watchdog Defines
61da177e4SLinus Torvalds * by Ken Hollis <khollis@bitgate.com>
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds */
91da177e4SLinus Torvalds #ifndef _LINUX_WATCHDOG_H
101da177e4SLinus Torvalds #define _LINUX_WATCHDOG_H
111da177e4SLinus Torvalds
124bfdf378SAndrey Panin
13ff0b3cd4SWim Van Sebroeck #include <linux/bitops.h>
1445f5fed3SAlan Cox #include <linux/cdev.h>
15664a3923SGuenter Roeck #include <linux/device.h>
16664a3923SGuenter Roeck #include <linux/kernel.h>
172165bf52SDamien Riegel #include <linux/notifier.h>
18607ca46eSDavid Howells #include <uapi/linux/watchdog.h>
194bfdf378SAndrey Panin
2043316044SWim Van Sebroeck struct watchdog_ops;
2143316044SWim Van Sebroeck struct watchdog_device;
22b4ffb190SGuenter Roeck struct watchdog_core_data;
23ff84136cSVladimir Zapolskiy struct watchdog_governor;
2443316044SWim Van Sebroeck
2543316044SWim Van Sebroeck /** struct watchdog_ops - The watchdog-devices operations
2643316044SWim Van Sebroeck *
2743316044SWim Van Sebroeck * @owner: The module owner.
2843316044SWim Van Sebroeck * @start: The routine for starting the watchdog device.
2943316044SWim Van Sebroeck * @stop: The routine for stopping the watchdog device.
3043316044SWim Van Sebroeck * @ping: The routine that sends a keepalive ping to the watchdog device.
312fa03560SWim Van Sebroeck * @status: The routine that shows the status of the watchdog device.
32760d2800SWolfram Sang * @set_timeout:The routine for setting the watchdog devices timeout value (in seconds).
33df044e02SWolfram Sang * @set_pretimeout:The routine for setting the watchdog devices pretimeout.
34760d2800SWolfram Sang * @get_timeleft:The routine that gets the time left before a reset (in seconds).
352165bf52SDamien Riegel * @restart: The routine for restarting the machine.
3678d88fc0SWim Van Sebroeck * @ioctl: The routines that handles extra ioctl calls.
3743316044SWim Van Sebroeck *
3843316044SWim Van Sebroeck * The watchdog_ops structure contains a list of low-level operations
3943316044SWim Van Sebroeck * that control a watchdog device. It also contains the module that owns
404951d27bSBumsik Kim * these operations. The start function is mandatory, all other
4180220fa7SWolfram Sang * functions are optional.
4243316044SWim Van Sebroeck */
4343316044SWim Van Sebroeck struct watchdog_ops {
4443316044SWim Van Sebroeck struct module *owner;
4543316044SWim Van Sebroeck /* mandatory operations */
4643316044SWim Van Sebroeck int (*start)(struct watchdog_device *);
4743316044SWim Van Sebroeck /* optional operations */
484951d27bSBumsik Kim int (*stop)(struct watchdog_device *);
4943316044SWim Van Sebroeck int (*ping)(struct watchdog_device *);
502fa03560SWim Van Sebroeck unsigned int (*status)(struct watchdog_device *);
51014d694eSWim Van Sebroeck int (*set_timeout)(struct watchdog_device *, unsigned int);
52df044e02SWolfram Sang int (*set_pretimeout)(struct watchdog_device *, unsigned int);
53fd7b673cSViresh Kumar unsigned int (*get_timeleft)(struct watchdog_device *);
544d8b229dSGuenter Roeck int (*restart)(struct watchdog_device *, unsigned long, void *);
5578d88fc0SWim Van Sebroeck long (*ioctl)(struct watchdog_device *, unsigned int, unsigned long);
5643316044SWim Van Sebroeck };
5743316044SWim Van Sebroeck
5843316044SWim Van Sebroeck /** struct watchdog_device - The structure that defines a watchdog device
5943316044SWim Van Sebroeck *
6045f5fed3SAlan Cox * @id: The watchdog's ID. (Allocated by watchdog_register_device)
61d6b469d9SAlan Cox * @parent: The parent bus device
62faa58475SGuenter Roeck * @groups: List of sysfs attribute groups to create when creating the
63faa58475SGuenter Roeck * watchdog device.
6443316044SWim Van Sebroeck * @info: Pointer to a watchdog_info structure.
6543316044SWim Van Sebroeck * @ops: Pointer to the list of watchdog operations.
66ff84136cSVladimir Zapolskiy * @gov: Pointer to watchdog pretimeout governor.
672fa03560SWim Van Sebroeck * @bootstatus: Status of the watchdog device at boot.
68760d2800SWolfram Sang * @timeout: The watchdog devices timeout value (in seconds).
69df044e02SWolfram Sang * @pretimeout: The watchdog devices pre_timeout value.
70760d2800SWolfram Sang * @min_timeout:The watchdog devices minimum timeout value (in seconds).
71664a3923SGuenter Roeck * @max_timeout:The watchdog devices maximum timeout value (in seconds)
72664a3923SGuenter Roeck * as configurable from user space. Only relevant if
73664a3923SGuenter Roeck * max_hw_heartbeat_ms is not provided.
7415013ad8SGuenter Roeck * @min_hw_heartbeat_ms:
75f9f535c1SGuenter Roeck * Hardware limit for minimum time between heartbeats,
76f9f535c1SGuenter Roeck * in milli-seconds.
77664a3923SGuenter Roeck * @max_hw_heartbeat_ms:
78664a3923SGuenter Roeck * Hardware limit for maximum timeout, in milli-seconds.
79664a3923SGuenter Roeck * Replaces max_timeout if specified.
80e1313196SDamien Riegel * @reboot_nb: The notifier block to stop watchdog on reboot.
812165bf52SDamien Riegel * @restart_nb: The notifier block to register a restart function.
82b4ffb190SGuenter Roeck * @driver_data:Pointer to the drivers private data.
83b4ffb190SGuenter Roeck * @wd_data: Pointer to watchdog core internal data.
8443316044SWim Van Sebroeck * @status: Field that contains the devices internal status bits.
85664a3923SGuenter Roeck * @deferred: Entry in wtd_deferred_reg_list which is used to
86ef90174fSJean-Baptiste Theou * register early initialized watchdogs.
8743316044SWim Van Sebroeck *
8843316044SWim Van Sebroeck * The watchdog_device structure contains all information about a
8943316044SWim Van Sebroeck * watchdog timer device.
9043316044SWim Van Sebroeck *
9143316044SWim Van Sebroeck * The driver-data field may not be accessed directly. It must be accessed
9243316044SWim Van Sebroeck * via the watchdog_set_drvdata and watchdog_get_drvdata helpers.
9343316044SWim Van Sebroeck */
9443316044SWim Van Sebroeck struct watchdog_device {
9545f5fed3SAlan Cox int id;
96d6b469d9SAlan Cox struct device *parent;
97faa58475SGuenter Roeck const struct attribute_group **groups;
9843316044SWim Van Sebroeck const struct watchdog_info *info;
9943316044SWim Van Sebroeck const struct watchdog_ops *ops;
100ff84136cSVladimir Zapolskiy const struct watchdog_governor *gov;
1012fa03560SWim Van Sebroeck unsigned int bootstatus;
102014d694eSWim Van Sebroeck unsigned int timeout;
103df044e02SWolfram Sang unsigned int pretimeout;
1043f43f68eSWim Van Sebroeck unsigned int min_timeout;
1053f43f68eSWim Van Sebroeck unsigned int max_timeout;
10615013ad8SGuenter Roeck unsigned int min_hw_heartbeat_ms;
107664a3923SGuenter Roeck unsigned int max_hw_heartbeat_ms;
108e1313196SDamien Riegel struct notifier_block reboot_nb;
1092165bf52SDamien Riegel struct notifier_block restart_nb;
110*60bcd91aSGrzegorz Jaszczyk struct notifier_block pm_nb;
11143316044SWim Van Sebroeck void *driver_data;
112b4ffb190SGuenter Roeck struct watchdog_core_data *wd_data;
11343316044SWim Van Sebroeck unsigned long status;
11443316044SWim Van Sebroeck /* Bit numbers for status flags */
115234445b4SWim Van Sebroeck #define WDOG_ACTIVE 0 /* Is the watchdog running/active */
116b4ffb190SGuenter Roeck #define WDOG_NO_WAY_OUT 1 /* Is 'nowayout' feature set ? */
117b4ffb190SGuenter Roeck #define WDOG_STOP_ON_REBOOT 2 /* Should be stopped on reboot */
118ee142889SGuenter Roeck #define WDOG_HW_RUNNING 3 /* True if HW watchdog running */
119bb292ac1SGuenter Roeck #define WDOG_STOP_ON_UNREGISTER 4 /* Should be stopped on unregister */
120*60bcd91aSGrzegorz Jaszczyk #define WDOG_NO_PING_ON_SUSPEND 5 /* Ping worker should be stopped on suspend */
121ef90174fSJean-Baptiste Theou struct list_head deferred;
12243316044SWim Van Sebroeck };
12343316044SWim Van Sebroeck
1244846e378SUwe Kleine-König #define WATCHDOG_NOWAYOUT IS_BUILTIN(CONFIG_WATCHDOG_NOWAYOUT)
1254846e378SUwe Kleine-König #define WATCHDOG_NOWAYOUT_INIT_STATUS (WATCHDOG_NOWAYOUT << WDOG_NO_WAY_OUT)
126ff0b3cd4SWim Van Sebroeck
12748fc7f7eSAdam Buchbinder /* Use the following function to check whether or not the watchdog is active */
watchdog_active(struct watchdog_device * wdd)128257f8c4aSViresh Kumar static inline bool watchdog_active(struct watchdog_device *wdd)
129257f8c4aSViresh Kumar {
130257f8c4aSViresh Kumar return test_bit(WDOG_ACTIVE, &wdd->status);
131257f8c4aSViresh Kumar }
132257f8c4aSViresh Kumar
133ee142889SGuenter Roeck /*
134ee142889SGuenter Roeck * Use the following function to check whether or not the hardware watchdog
135ee142889SGuenter Roeck * is running
136ee142889SGuenter Roeck */
watchdog_hw_running(struct watchdog_device * wdd)137ee142889SGuenter Roeck static inline bool watchdog_hw_running(struct watchdog_device *wdd)
138ee142889SGuenter Roeck {
139ee142889SGuenter Roeck return test_bit(WDOG_HW_RUNNING, &wdd->status);
140ee142889SGuenter Roeck }
141ee142889SGuenter Roeck
142ff0b3cd4SWim Van Sebroeck /* Use the following function to set the nowayout feature */
watchdog_set_nowayout(struct watchdog_device * wdd,bool nowayout)14386a1e189SWim Van Sebroeck static inline void watchdog_set_nowayout(struct watchdog_device *wdd, bool nowayout)
144ff0b3cd4SWim Van Sebroeck {
145ff0b3cd4SWim Van Sebroeck if (nowayout)
146ff0b3cd4SWim Van Sebroeck set_bit(WDOG_NO_WAY_OUT, &wdd->status);
147ff0b3cd4SWim Van Sebroeck }
148ff0b3cd4SWim Van Sebroeck
149e1313196SDamien Riegel /* Use the following function to stop the watchdog on reboot */
watchdog_stop_on_reboot(struct watchdog_device * wdd)150e1313196SDamien Riegel static inline void watchdog_stop_on_reboot(struct watchdog_device *wdd)
151e1313196SDamien Riegel {
152e1313196SDamien Riegel set_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
153e1313196SDamien Riegel }
154e1313196SDamien Riegel
155bb292ac1SGuenter Roeck /* Use the following function to stop the watchdog when unregistering it */
watchdog_stop_on_unregister(struct watchdog_device * wdd)156bb292ac1SGuenter Roeck static inline void watchdog_stop_on_unregister(struct watchdog_device *wdd)
157bb292ac1SGuenter Roeck {
158bb292ac1SGuenter Roeck set_bit(WDOG_STOP_ON_UNREGISTER, &wdd->status);
159bb292ac1SGuenter Roeck }
160bb292ac1SGuenter Roeck
161*60bcd91aSGrzegorz Jaszczyk /* Use the following function to stop the wdog ping worker when suspending */
watchdog_stop_ping_on_suspend(struct watchdog_device * wdd)162*60bcd91aSGrzegorz Jaszczyk static inline void watchdog_stop_ping_on_suspend(struct watchdog_device *wdd)
163*60bcd91aSGrzegorz Jaszczyk {
164*60bcd91aSGrzegorz Jaszczyk set_bit(WDOG_NO_PING_ON_SUSPEND, &wdd->status);
165*60bcd91aSGrzegorz Jaszczyk }
166*60bcd91aSGrzegorz Jaszczyk
1673048253eSFabio Porcedda /* Use the following function to check if a timeout value is invalid */
watchdog_timeout_invalid(struct watchdog_device * wdd,unsigned int t)1683048253eSFabio Porcedda static inline bool watchdog_timeout_invalid(struct watchdog_device *wdd, unsigned int t)
1693048253eSFabio Porcedda {
1701e935949SGuenter Roeck /*
1711e935949SGuenter Roeck * The timeout is invalid if
172664a3923SGuenter Roeck * - the requested value is larger than UINT_MAX / 1000
173664a3923SGuenter Roeck * (since internal calculations are done in milli-seconds),
174664a3923SGuenter Roeck * or
1751e935949SGuenter Roeck * - the requested value is smaller than the configured minimum timeout,
1761e935949SGuenter Roeck * or
177664a3923SGuenter Roeck * - a maximum hardware timeout is not configured, a maximum timeout
178664a3923SGuenter Roeck * is configured, and the requested value is larger than the
179664a3923SGuenter Roeck * configured maximum timeout.
1801e935949SGuenter Roeck */
181664a3923SGuenter Roeck return t > UINT_MAX / 1000 || t < wdd->min_timeout ||
182664a3923SGuenter Roeck (!wdd->max_hw_heartbeat_ms && wdd->max_timeout &&
183664a3923SGuenter Roeck t > wdd->max_timeout);
1843048253eSFabio Porcedda }
1853048253eSFabio Porcedda
186df044e02SWolfram Sang /* Use the following function to check if a pretimeout value is invalid */
watchdog_pretimeout_invalid(struct watchdog_device * wdd,unsigned int t)187df044e02SWolfram Sang static inline bool watchdog_pretimeout_invalid(struct watchdog_device *wdd,
188df044e02SWolfram Sang unsigned int t)
189df044e02SWolfram Sang {
190df044e02SWolfram Sang return t && wdd->timeout && t >= wdd->timeout;
191df044e02SWolfram Sang }
192df044e02SWolfram Sang
19343316044SWim Van Sebroeck /* Use the following functions to manipulate watchdog driver specific data */
watchdog_set_drvdata(struct watchdog_device * wdd,void * data)19443316044SWim Van Sebroeck static inline void watchdog_set_drvdata(struct watchdog_device *wdd, void *data)
19543316044SWim Van Sebroeck {
19643316044SWim Van Sebroeck wdd->driver_data = data;
19743316044SWim Van Sebroeck }
19843316044SWim Van Sebroeck
watchdog_get_drvdata(struct watchdog_device * wdd)19943316044SWim Van Sebroeck static inline void *watchdog_get_drvdata(struct watchdog_device *wdd)
20043316044SWim Van Sebroeck {
20143316044SWim Van Sebroeck return wdd->driver_data;
20243316044SWim Van Sebroeck }
20343316044SWim Van Sebroeck
204ff84136cSVladimir Zapolskiy /* Use the following functions to report watchdog pretimeout event */
205ff84136cSVladimir Zapolskiy #if IS_ENABLED(CONFIG_WATCHDOG_PRETIMEOUT_GOV)
206ff84136cSVladimir Zapolskiy void watchdog_notify_pretimeout(struct watchdog_device *wdd);
207ff84136cSVladimir Zapolskiy #else
watchdog_notify_pretimeout(struct watchdog_device * wdd)208ff84136cSVladimir Zapolskiy static inline void watchdog_notify_pretimeout(struct watchdog_device *wdd)
209ff84136cSVladimir Zapolskiy {
210ff84136cSVladimir Zapolskiy pr_alert("watchdog%d: pretimeout event\n", wdd->id);
211ff84136cSVladimir Zapolskiy }
212ff84136cSVladimir Zapolskiy #endif
213ff84136cSVladimir Zapolskiy
214cf13a84dSFabio Porcedda /* drivers/watchdog/watchdog_core.c */
2152165bf52SDamien Riegel void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority);
2163048253eSFabio Porcedda extern int watchdog_init_timeout(struct watchdog_device *wdd,
2173048253eSFabio Porcedda unsigned int timeout_parm, struct device *dev);
21843316044SWim Van Sebroeck extern int watchdog_register_device(struct watchdog_device *);
21943316044SWim Van Sebroeck extern void watchdog_unregister_device(struct watchdog_device *);
220*60bcd91aSGrzegorz Jaszczyk int watchdog_dev_suspend(struct watchdog_device *wdd);
221*60bcd91aSGrzegorz Jaszczyk int watchdog_dev_resume(struct watchdog_device *wdd);
22243316044SWim Van Sebroeck
223cef9572eSTero Kristo int watchdog_set_last_hw_keepalive(struct watchdog_device *, unsigned int);
224cef9572eSTero Kristo
22583fbae5aSNeil Armstrong /* devres register variant */
22683fbae5aSNeil Armstrong int devm_watchdog_register_device(struct device *dev, struct watchdog_device *);
22783fbae5aSNeil Armstrong
2281da177e4SLinus Torvalds #endif /* ifndef _LINUX_WATCHDOG_H */
229