watchdog.h 8.2 KB

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