1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * (c) Copyright 2016 Oracle Corporation
6 * Implement a simple watchdog driver using the built-in sun4v hypervisor
7 * watchdog support. If time expires, the hypervisor stops or bounces
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/watchdog.h>
19 #include <asm/hypervisor.h>
20 #include <asm/mdesc.h>
22 #define WDT_TIMEOUT 60
23 #define WDT_MAX_TIMEOUT 31536000
24 #define WDT_MIN_TIMEOUT 1
25 #define WDT_DEFAULT_RESOLUTION_MS 1000 /* 1 second */
27 static unsigned int timeout;
28 module_param(timeout, uint, 0);
29 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
30 __MODULE_STRING(WDT_TIMEOUT) ")");
32 static bool nowayout = WATCHDOG_NOWAYOUT;
33 module_param(nowayout, bool, S_IRUGO);
34 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
35 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
37 static int sun4v_wdt_stop(struct watchdog_device *wdd)
39 sun4v_mach_set_watchdog(0, NULL);
44 static int sun4v_wdt_ping(struct watchdog_device *wdd)
49 * HV watchdog timer will round up the timeout
50 * passed in to the nearest multiple of the
51 * watchdog resolution in milliseconds.
53 hverr = sun4v_mach_set_watchdog(wdd->timeout * 1000, NULL);
54 if (hverr == HV_EINVAL)
60 static int sun4v_wdt_set_timeout(struct watchdog_device *wdd,
63 wdd->timeout = timeout;
68 static const struct watchdog_info sun4v_wdt_ident = {
69 .options = WDIOF_SETTIMEOUT |
72 .identity = "sun4v hypervisor watchdog",
73 .firmware_version = 0,
76 static const struct watchdog_ops sun4v_wdt_ops = {
78 .start = sun4v_wdt_ping,
79 .stop = sun4v_wdt_stop,
80 .ping = sun4v_wdt_ping,
81 .set_timeout = sun4v_wdt_set_timeout,
84 static struct watchdog_device wdd = {
85 .info = &sun4v_wdt_ident,
86 .ops = &sun4v_wdt_ops,
87 .min_timeout = WDT_MIN_TIMEOUT,
88 .max_timeout = WDT_MAX_TIMEOUT,
89 .timeout = WDT_TIMEOUT,
92 static int __init sun4v_wdt_init(void)
94 struct mdesc_handle *handle;
98 unsigned long major = 1, minor = 1;
101 * There are 2 properties that can be set from the control
102 * domain for the watchdog.
103 * watchdog-resolution
104 * watchdog-max-timeout
106 * We can expect a handle to be returned otherwise something
107 * serious is wrong. Correct to return -ENODEV here.
110 handle = mdesc_grab();
114 node = mdesc_node_by_name(handle, MDESC_NODE_NULL, "platform");
116 if (node == MDESC_NODE_NULL)
120 * This is a safe way to validate if we are on the right
123 if (sun4v_hvapi_register(HV_GRP_CORE, major, &minor))
126 /* Allow value of watchdog-resolution up to 1s (default) */
127 value = mdesc_get_property(handle, node, "watchdog-resolution", NULL);
131 *value > WDT_DEFAULT_RESOLUTION_MS)
135 value = mdesc_get_property(handle, node, "watchdog-max-timeout", NULL);
138 * If the property value (in ms) is smaller than
139 * min_timeout, return -EINVAL.
141 if (*value < wdd.min_timeout * 1000)
145 * If the property value is smaller than
146 * default max_timeout then set watchdog max_timeout to
147 * the value of the property in seconds.
149 if (*value < wdd.max_timeout * 1000)
150 wdd.max_timeout = *value / 1000;
153 watchdog_init_timeout(&wdd, timeout, NULL);
155 watchdog_set_nowayout(&wdd, nowayout);
157 err = watchdog_register_device(&wdd);
161 pr_info("initialized (timeout=%ds, nowayout=%d)\n",
162 wdd.timeout, nowayout);
164 mdesc_release(handle);
169 sun4v_hvapi_unregister(HV_GRP_CORE);
172 mdesc_release(handle);
176 static void __exit sun4v_wdt_exit(void)
178 sun4v_hvapi_unregister(HV_GRP_CORE);
179 watchdog_unregister_device(&wdd);
182 module_init(sun4v_wdt_init);
183 module_exit(sun4v_wdt_exit);
185 MODULE_AUTHOR("Wim Coekaerts <wim.coekaerts@oracle.com>");
186 MODULE_DESCRIPTION("sun4v watchdog driver");
187 MODULE_LICENSE("GPL");