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