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