Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  *  Generic watchdog defines. Derived from..
0004  *
0005  * Berkshire PC Watchdog Defines
0006  * by Ken Hollis <khollis@bitgate.com>
0007  *
0008  */
0009 #ifndef _LINUX_WATCHDOG_H
0010 #define _LINUX_WATCHDOG_H
0011 
0012 
0013 #include <linux/bitops.h>
0014 #include <linux/cdev.h>
0015 #include <linux/device.h>
0016 #include <linux/kernel.h>
0017 #include <linux/notifier.h>
0018 #include <uapi/linux/watchdog.h>
0019 
0020 struct watchdog_ops;
0021 struct watchdog_device;
0022 struct watchdog_core_data;
0023 struct watchdog_governor;
0024 
0025 /** struct watchdog_ops - The watchdog-devices operations
0026  *
0027  * @owner:  The module owner.
0028  * @start:  The routine for starting the watchdog device.
0029  * @stop:   The routine for stopping the watchdog device.
0030  * @ping:   The routine that sends a keepalive ping to the watchdog device.
0031  * @status: The routine that shows the status of the watchdog device.
0032  * @set_timeout:The routine for setting the watchdog devices timeout value (in seconds).
0033  * @set_pretimeout:The routine for setting the watchdog devices pretimeout.
0034  * @get_timeleft:The routine that gets the time left before a reset (in seconds).
0035  * @restart:    The routine for restarting the machine.
0036  * @ioctl:  The routines that handles extra ioctl calls.
0037  *
0038  * The watchdog_ops structure contains a list of low-level operations
0039  * that control a watchdog device. It also contains the module that owns
0040  * these operations. The start function is mandatory, all other
0041  * functions are optional.
0042  */
0043 struct watchdog_ops {
0044     struct module *owner;
0045     /* mandatory operations */
0046     int (*start)(struct watchdog_device *);
0047     /* optional operations */
0048     int (*stop)(struct watchdog_device *);
0049     int (*ping)(struct watchdog_device *);
0050     unsigned int (*status)(struct watchdog_device *);
0051     int (*set_timeout)(struct watchdog_device *, unsigned int);
0052     int (*set_pretimeout)(struct watchdog_device *, unsigned int);
0053     unsigned int (*get_timeleft)(struct watchdog_device *);
0054     int (*restart)(struct watchdog_device *, unsigned long, void *);
0055     long (*ioctl)(struct watchdog_device *, unsigned int, unsigned long);
0056 };
0057 
0058 /** struct watchdog_device - The structure that defines a watchdog device
0059  *
0060  * @id:     The watchdog's ID. (Allocated by watchdog_register_device)
0061  * @parent: The parent bus device
0062  * @groups: List of sysfs attribute groups to create when creating the
0063  *      watchdog device.
0064  * @info:   Pointer to a watchdog_info structure.
0065  * @ops:    Pointer to the list of watchdog operations.
0066  * @gov:    Pointer to watchdog pretimeout governor.
0067  * @bootstatus: Status of the watchdog device at boot.
0068  * @timeout:    The watchdog devices timeout value (in seconds).
0069  * @pretimeout: The watchdog devices pre_timeout value.
0070  * @min_timeout:The watchdog devices minimum timeout value (in seconds).
0071  * @max_timeout:The watchdog devices maximum timeout value (in seconds)
0072  *      as configurable from user space. Only relevant if
0073  *      max_hw_heartbeat_ms is not provided.
0074  * @min_hw_heartbeat_ms:
0075  *      Hardware limit for minimum time between heartbeats,
0076  *      in milli-seconds.
0077  * @max_hw_heartbeat_ms:
0078  *      Hardware limit for maximum timeout, in milli-seconds.
0079  *      Replaces max_timeout if specified.
0080  * @reboot_nb:  The notifier block to stop watchdog on reboot.
0081  * @restart_nb: The notifier block to register a restart function.
0082  * @driver_data:Pointer to the drivers private data.
0083  * @wd_data:    Pointer to watchdog core internal data.
0084  * @status: Field that contains the devices internal status bits.
0085  * @deferred:   Entry in wtd_deferred_reg_list which is used to
0086  *      register early initialized watchdogs.
0087  *
0088  * The watchdog_device structure contains all information about a
0089  * watchdog timer device.
0090  *
0091  * The driver-data field may not be accessed directly. It must be accessed
0092  * via the watchdog_set_drvdata and watchdog_get_drvdata helpers.
0093  */
0094 struct watchdog_device {
0095     int id;
0096     struct device *parent;
0097     const struct attribute_group **groups;
0098     const struct watchdog_info *info;
0099     const struct watchdog_ops *ops;
0100     const struct watchdog_governor *gov;
0101     unsigned int bootstatus;
0102     unsigned int timeout;
0103     unsigned int pretimeout;
0104     unsigned int min_timeout;
0105     unsigned int max_timeout;
0106     unsigned int min_hw_heartbeat_ms;
0107     unsigned int max_hw_heartbeat_ms;
0108     struct notifier_block reboot_nb;
0109     struct notifier_block restart_nb;
0110     struct notifier_block pm_nb;
0111     void *driver_data;
0112     struct watchdog_core_data *wd_data;
0113     unsigned long status;
0114 /* Bit numbers for status flags */
0115 #define WDOG_ACTIVE     0   /* Is the watchdog running/active */
0116 #define WDOG_NO_WAY_OUT     1   /* Is 'nowayout' feature set ? */
0117 #define WDOG_STOP_ON_REBOOT 2   /* Should be stopped on reboot */
0118 #define WDOG_HW_RUNNING     3   /* True if HW watchdog running */
0119 #define WDOG_STOP_ON_UNREGISTER 4   /* Should be stopped on unregister */
0120 #define WDOG_NO_PING_ON_SUSPEND 5   /* Ping worker should be stopped on suspend */
0121     struct list_head deferred;
0122 };
0123 
0124 #define WATCHDOG_NOWAYOUT       IS_BUILTIN(CONFIG_WATCHDOG_NOWAYOUT)
0125 #define WATCHDOG_NOWAYOUT_INIT_STATUS   (WATCHDOG_NOWAYOUT << WDOG_NO_WAY_OUT)
0126 
0127 /* Use the following function to check whether or not the watchdog is active */
0128 static inline bool watchdog_active(struct watchdog_device *wdd)
0129 {
0130     return test_bit(WDOG_ACTIVE, &wdd->status);
0131 }
0132 
0133 /*
0134  * Use the following function to check whether or not the hardware watchdog
0135  * is running
0136  */
0137 static inline bool watchdog_hw_running(struct watchdog_device *wdd)
0138 {
0139     return test_bit(WDOG_HW_RUNNING, &wdd->status);
0140 }
0141 
0142 /* Use the following function to set the nowayout feature */
0143 static inline void watchdog_set_nowayout(struct watchdog_device *wdd, bool nowayout)
0144 {
0145     if (nowayout)
0146         set_bit(WDOG_NO_WAY_OUT, &wdd->status);
0147 }
0148 
0149 /* Use the following function to stop the watchdog on reboot */
0150 static inline void watchdog_stop_on_reboot(struct watchdog_device *wdd)
0151 {
0152     set_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
0153 }
0154 
0155 /* Use the following function to stop the watchdog when unregistering it */
0156 static inline void watchdog_stop_on_unregister(struct watchdog_device *wdd)
0157 {
0158     set_bit(WDOG_STOP_ON_UNREGISTER, &wdd->status);
0159 }
0160 
0161 /* Use the following function to stop the wdog ping worker when suspending */
0162 static inline void watchdog_stop_ping_on_suspend(struct watchdog_device *wdd)
0163 {
0164     set_bit(WDOG_NO_PING_ON_SUSPEND, &wdd->status);
0165 }
0166 
0167 /* Use the following function to check if a timeout value is invalid */
0168 static inline bool watchdog_timeout_invalid(struct watchdog_device *wdd, unsigned int t)
0169 {
0170     /*
0171      * The timeout is invalid if
0172      * - the requested value is larger than UINT_MAX / 1000
0173      *   (since internal calculations are done in milli-seconds),
0174      * or
0175      * - the requested value is smaller than the configured minimum timeout,
0176      * or
0177      * - a maximum hardware timeout is not configured, a maximum timeout
0178      *   is configured, and the requested value is larger than the
0179      *   configured maximum timeout.
0180      */
0181     return t > UINT_MAX / 1000 || t < wdd->min_timeout ||
0182         (!wdd->max_hw_heartbeat_ms && wdd->max_timeout &&
0183          t > wdd->max_timeout);
0184 }
0185 
0186 /* Use the following function to check if a pretimeout value is invalid */
0187 static inline bool watchdog_pretimeout_invalid(struct watchdog_device *wdd,
0188                            unsigned int t)
0189 {
0190     return t && wdd->timeout && t >= wdd->timeout;
0191 }
0192 
0193 /* Use the following functions to manipulate watchdog driver specific data */
0194 static inline void watchdog_set_drvdata(struct watchdog_device *wdd, void *data)
0195 {
0196     wdd->driver_data = data;
0197 }
0198 
0199 static inline void *watchdog_get_drvdata(struct watchdog_device *wdd)
0200 {
0201     return wdd->driver_data;
0202 }
0203 
0204 /* Use the following functions to report watchdog pretimeout event */
0205 #if IS_ENABLED(CONFIG_WATCHDOG_PRETIMEOUT_GOV)
0206 void watchdog_notify_pretimeout(struct watchdog_device *wdd);
0207 #else
0208 static inline void watchdog_notify_pretimeout(struct watchdog_device *wdd)
0209 {
0210     pr_alert("watchdog%d: pretimeout event\n", wdd->id);
0211 }
0212 #endif
0213 
0214 /* drivers/watchdog/watchdog_core.c */
0215 void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority);
0216 extern int watchdog_init_timeout(struct watchdog_device *wdd,
0217                   unsigned int timeout_parm, struct device *dev);
0218 extern int watchdog_register_device(struct watchdog_device *);
0219 extern void watchdog_unregister_device(struct watchdog_device *);
0220 int watchdog_dev_suspend(struct watchdog_device *wdd);
0221 int watchdog_dev_resume(struct watchdog_device *wdd);
0222 
0223 int watchdog_set_last_hw_keepalive(struct watchdog_device *, unsigned int);
0224 
0225 /* devres register variant */
0226 int devm_watchdog_register_device(struct device *dev, struct watchdog_device *);
0227 
0228 #endif  /* ifndef _LINUX_WATCHDOG_H */