projects/linux·blob master · refs

238 lines · 8.4 KiB

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