1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Generic watchdog defines. Derived from..
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Berkshire PC Watchdog Defines
6*4882a593Smuzhiyun * by Ken Hollis <khollis@bitgate.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun #ifndef _LINUX_WATCHDOG_H
10*4882a593Smuzhiyun #define _LINUX_WATCHDOG_H
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/bitops.h>
14*4882a593Smuzhiyun #include <linux/cdev.h>
15*4882a593Smuzhiyun #include <linux/device.h>
16*4882a593Smuzhiyun #include <linux/kernel.h>
17*4882a593Smuzhiyun #include <linux/notifier.h>
18*4882a593Smuzhiyun #include <uapi/linux/watchdog.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun struct watchdog_ops;
21*4882a593Smuzhiyun struct watchdog_device;
22*4882a593Smuzhiyun struct watchdog_core_data;
23*4882a593Smuzhiyun struct watchdog_governor;
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /** struct watchdog_ops - The watchdog-devices operations
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * @owner: The module owner.
28*4882a593Smuzhiyun * @start: The routine for starting the watchdog device.
29*4882a593Smuzhiyun * @stop: The routine for stopping the watchdog device.
30*4882a593Smuzhiyun * @ping: The routine that sends a keepalive ping to the watchdog device.
31*4882a593Smuzhiyun * @status: The routine that shows the status of the watchdog device.
32*4882a593Smuzhiyun * @set_timeout:The routine for setting the watchdog devices timeout value (in seconds).
33*4882a593Smuzhiyun * @set_pretimeout:The routine for setting the watchdog devices pretimeout.
34*4882a593Smuzhiyun * @get_timeleft:The routine that gets the time left before a reset (in seconds).
35*4882a593Smuzhiyun * @restart: The routine for restarting the machine.
36*4882a593Smuzhiyun * @ioctl: The routines that handles extra ioctl calls.
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * The watchdog_ops structure contains a list of low-level operations
39*4882a593Smuzhiyun * that control a watchdog device. It also contains the module that owns
40*4882a593Smuzhiyun * these operations. The start function is mandatory, all other
41*4882a593Smuzhiyun * functions are optional.
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun struct watchdog_ops {
44*4882a593Smuzhiyun struct module *owner;
45*4882a593Smuzhiyun /* mandatory operations */
46*4882a593Smuzhiyun int (*start)(struct watchdog_device *);
47*4882a593Smuzhiyun /* optional operations */
48*4882a593Smuzhiyun int (*stop)(struct watchdog_device *);
49*4882a593Smuzhiyun int (*ping)(struct watchdog_device *);
50*4882a593Smuzhiyun unsigned int (*status)(struct watchdog_device *);
51*4882a593Smuzhiyun int (*set_timeout)(struct watchdog_device *, unsigned int);
52*4882a593Smuzhiyun int (*set_pretimeout)(struct watchdog_device *, unsigned int);
53*4882a593Smuzhiyun unsigned int (*get_timeleft)(struct watchdog_device *);
54*4882a593Smuzhiyun int (*restart)(struct watchdog_device *, unsigned long, void *);
55*4882a593Smuzhiyun long (*ioctl)(struct watchdog_device *, unsigned int, unsigned long);
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /** struct watchdog_device - The structure that defines a watchdog device
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * @id: The watchdog's ID. (Allocated by watchdog_register_device)
61*4882a593Smuzhiyun * @parent: The parent bus device
62*4882a593Smuzhiyun * @groups: List of sysfs attribute groups to create when creating the
63*4882a593Smuzhiyun * watchdog device.
64*4882a593Smuzhiyun * @info: Pointer to a watchdog_info structure.
65*4882a593Smuzhiyun * @ops: Pointer to the list of watchdog operations.
66*4882a593Smuzhiyun * @gov: Pointer to watchdog pretimeout governor.
67*4882a593Smuzhiyun * @bootstatus: Status of the watchdog device at boot.
68*4882a593Smuzhiyun * @timeout: The watchdog devices timeout value (in seconds).
69*4882a593Smuzhiyun * @pretimeout: The watchdog devices pre_timeout value.
70*4882a593Smuzhiyun * @min_timeout:The watchdog devices minimum timeout value (in seconds).
71*4882a593Smuzhiyun * @max_timeout:The watchdog devices maximum timeout value (in seconds)
72*4882a593Smuzhiyun * as configurable from user space. Only relevant if
73*4882a593Smuzhiyun * max_hw_heartbeat_ms is not provided.
74*4882a593Smuzhiyun * @min_hw_heartbeat_ms:
75*4882a593Smuzhiyun * Hardware limit for minimum time between heartbeats,
76*4882a593Smuzhiyun * in milli-seconds.
77*4882a593Smuzhiyun * @max_hw_heartbeat_ms:
78*4882a593Smuzhiyun * Hardware limit for maximum timeout, in milli-seconds.
79*4882a593Smuzhiyun * Replaces max_timeout if specified.
80*4882a593Smuzhiyun * @reboot_nb: The notifier block to stop watchdog on reboot.
81*4882a593Smuzhiyun * @restart_nb: The notifier block to register a restart function.
82*4882a593Smuzhiyun * @driver_data:Pointer to the drivers private data.
83*4882a593Smuzhiyun * @wd_data: Pointer to watchdog core internal data.
84*4882a593Smuzhiyun * @status: Field that contains the devices internal status bits.
85*4882a593Smuzhiyun * @deferred: Entry in wtd_deferred_reg_list which is used to
86*4882a593Smuzhiyun * register early initialized watchdogs.
87*4882a593Smuzhiyun *
88*4882a593Smuzhiyun * The watchdog_device structure contains all information about a
89*4882a593Smuzhiyun * watchdog timer device.
90*4882a593Smuzhiyun *
91*4882a593Smuzhiyun * The driver-data field may not be accessed directly. It must be accessed
92*4882a593Smuzhiyun * via the watchdog_set_drvdata and watchdog_get_drvdata helpers.
93*4882a593Smuzhiyun */
94*4882a593Smuzhiyun struct watchdog_device {
95*4882a593Smuzhiyun int id;
96*4882a593Smuzhiyun struct device *parent;
97*4882a593Smuzhiyun const struct attribute_group **groups;
98*4882a593Smuzhiyun const struct watchdog_info *info;
99*4882a593Smuzhiyun const struct watchdog_ops *ops;
100*4882a593Smuzhiyun const struct watchdog_governor *gov;
101*4882a593Smuzhiyun unsigned int bootstatus;
102*4882a593Smuzhiyun unsigned int timeout;
103*4882a593Smuzhiyun unsigned int pretimeout;
104*4882a593Smuzhiyun unsigned int min_timeout;
105*4882a593Smuzhiyun unsigned int max_timeout;
106*4882a593Smuzhiyun unsigned int min_hw_heartbeat_ms;
107*4882a593Smuzhiyun unsigned int max_hw_heartbeat_ms;
108*4882a593Smuzhiyun struct notifier_block reboot_nb;
109*4882a593Smuzhiyun struct notifier_block restart_nb;
110*4882a593Smuzhiyun void *driver_data;
111*4882a593Smuzhiyun struct watchdog_core_data *wd_data;
112*4882a593Smuzhiyun unsigned long status;
113*4882a593Smuzhiyun /* Bit numbers for status flags */
114*4882a593Smuzhiyun #define WDOG_ACTIVE 0 /* Is the watchdog running/active */
115*4882a593Smuzhiyun #define WDOG_NO_WAY_OUT 1 /* Is 'nowayout' feature set ? */
116*4882a593Smuzhiyun #define WDOG_STOP_ON_REBOOT 2 /* Should be stopped on reboot */
117*4882a593Smuzhiyun #define WDOG_HW_RUNNING 3 /* True if HW watchdog running */
118*4882a593Smuzhiyun #define WDOG_STOP_ON_UNREGISTER 4 /* Should be stopped on unregister */
119*4882a593Smuzhiyun struct list_head deferred;
120*4882a593Smuzhiyun };
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun #define WATCHDOG_NOWAYOUT IS_BUILTIN(CONFIG_WATCHDOG_NOWAYOUT)
123*4882a593Smuzhiyun #define WATCHDOG_NOWAYOUT_INIT_STATUS (WATCHDOG_NOWAYOUT << WDOG_NO_WAY_OUT)
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun /* Use the following function to check whether or not the watchdog is active */
watchdog_active(struct watchdog_device * wdd)126*4882a593Smuzhiyun static inline bool watchdog_active(struct watchdog_device *wdd)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun return test_bit(WDOG_ACTIVE, &wdd->status);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /*
132*4882a593Smuzhiyun * Use the following function to check whether or not the hardware watchdog
133*4882a593Smuzhiyun * is running
134*4882a593Smuzhiyun */
watchdog_hw_running(struct watchdog_device * wdd)135*4882a593Smuzhiyun static inline bool watchdog_hw_running(struct watchdog_device *wdd)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun return test_bit(WDOG_HW_RUNNING, &wdd->status);
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /* Use the following function to set the nowayout feature */
watchdog_set_nowayout(struct watchdog_device * wdd,bool nowayout)141*4882a593Smuzhiyun static inline void watchdog_set_nowayout(struct watchdog_device *wdd, bool nowayout)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun if (nowayout)
144*4882a593Smuzhiyun set_bit(WDOG_NO_WAY_OUT, &wdd->status);
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /* Use the following function to stop the watchdog on reboot */
watchdog_stop_on_reboot(struct watchdog_device * wdd)148*4882a593Smuzhiyun static inline void watchdog_stop_on_reboot(struct watchdog_device *wdd)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun set_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /* Use the following function to stop the watchdog when unregistering it */
watchdog_stop_on_unregister(struct watchdog_device * wdd)154*4882a593Smuzhiyun static inline void watchdog_stop_on_unregister(struct watchdog_device *wdd)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun set_bit(WDOG_STOP_ON_UNREGISTER, &wdd->status);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /* Use the following function to check if a timeout value is invalid */
watchdog_timeout_invalid(struct watchdog_device * wdd,unsigned int t)160*4882a593Smuzhiyun static inline bool watchdog_timeout_invalid(struct watchdog_device *wdd, unsigned int t)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun /*
163*4882a593Smuzhiyun * The timeout is invalid if
164*4882a593Smuzhiyun * - the requested value is larger than UINT_MAX / 1000
165*4882a593Smuzhiyun * (since internal calculations are done in milli-seconds),
166*4882a593Smuzhiyun * or
167*4882a593Smuzhiyun * - the requested value is smaller than the configured minimum timeout,
168*4882a593Smuzhiyun * or
169*4882a593Smuzhiyun * - a maximum hardware timeout is not configured, a maximum timeout
170*4882a593Smuzhiyun * is configured, and the requested value is larger than the
171*4882a593Smuzhiyun * configured maximum timeout.
172*4882a593Smuzhiyun */
173*4882a593Smuzhiyun return t > UINT_MAX / 1000 || t < wdd->min_timeout ||
174*4882a593Smuzhiyun (!wdd->max_hw_heartbeat_ms && wdd->max_timeout &&
175*4882a593Smuzhiyun t > wdd->max_timeout);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun /* Use the following function to check if a pretimeout value is invalid */
watchdog_pretimeout_invalid(struct watchdog_device * wdd,unsigned int t)179*4882a593Smuzhiyun static inline bool watchdog_pretimeout_invalid(struct watchdog_device *wdd,
180*4882a593Smuzhiyun unsigned int t)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun return t && wdd->timeout && t >= wdd->timeout;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /* Use the following functions to manipulate watchdog driver specific data */
watchdog_set_drvdata(struct watchdog_device * wdd,void * data)186*4882a593Smuzhiyun static inline void watchdog_set_drvdata(struct watchdog_device *wdd, void *data)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun wdd->driver_data = data;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
watchdog_get_drvdata(struct watchdog_device * wdd)191*4882a593Smuzhiyun static inline void *watchdog_get_drvdata(struct watchdog_device *wdd)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun return wdd->driver_data;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun /* Use the following functions to report watchdog pretimeout event */
197*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_WATCHDOG_PRETIMEOUT_GOV)
198*4882a593Smuzhiyun void watchdog_notify_pretimeout(struct watchdog_device *wdd);
199*4882a593Smuzhiyun #else
watchdog_notify_pretimeout(struct watchdog_device * wdd)200*4882a593Smuzhiyun static inline void watchdog_notify_pretimeout(struct watchdog_device *wdd)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun pr_alert("watchdog%d: pretimeout event\n", wdd->id);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun #endif
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /* drivers/watchdog/watchdog_core.c */
207*4882a593Smuzhiyun void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority);
208*4882a593Smuzhiyun extern int watchdog_init_timeout(struct watchdog_device *wdd,
209*4882a593Smuzhiyun unsigned int timeout_parm, struct device *dev);
210*4882a593Smuzhiyun extern int watchdog_register_device(struct watchdog_device *);
211*4882a593Smuzhiyun extern void watchdog_unregister_device(struct watchdog_device *);
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun int watchdog_set_last_hw_keepalive(struct watchdog_device *, unsigned int);
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /* devres register variant */
216*4882a593Smuzhiyun int devm_watchdog_register_device(struct device *dev, struct watchdog_device *);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun #endif /* ifndef _LINUX_WATCHDOG_H */
219