3 * (c) Copyright 2016 Oracle Corporation
5 * Implement a simple watchdog driver using the built-in sun4v hypervisor
6 * watchdog support. If time expires, the hypervisor stops or bounces
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/errno.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/watchdog.h>
23 #include <asm/hypervisor.h>
24 #include <asm/mdesc.h>
26 #define WDT_TIMEOUT 60
27 #define WDT_MAX_TIMEOUT 31536000
28 #define WDT_MIN_TIMEOUT 1
29 #define WDT_DEFAULT_RESOLUTION_MS 1000 /* 1 second */
31 static unsigned int timeout;
32 module_param(timeout, uint, 0);
33 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
34 __MODULE_STRING(WDT_TIMEOUT) ")");
36 static bool nowayout = WATCHDOG_NOWAYOUT;
37 module_param(nowayout, bool, S_IRUGO);
38 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
39 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
41 static int sun4v_wdt_stop(struct watchdog_device *wdd)
43 sun4v_mach_set_watchdog(0, NULL);
48 static int sun4v_wdt_ping(struct watchdog_device *wdd)
53 * HV watchdog timer will round up the timeout
54 * passed in to the nearest multiple of the
55 * watchdog resolution in milliseconds.
57 hverr = sun4v_mach_set_watchdog(wdd->timeout * 1000, NULL);
58 if (hverr == HV_EINVAL)
64 static int sun4v_wdt_set_timeout(struct watchdog_device *wdd,
67 wdd->timeout = timeout;
72 static const struct watchdog_info sun4v_wdt_ident = {
73 .options = WDIOF_SETTIMEOUT |
76 .identity = "sun4v hypervisor watchdog",
77 .firmware_version = 0,
80 static struct watchdog_ops sun4v_wdt_ops = {
82 .start = sun4v_wdt_ping,
83 .stop = sun4v_wdt_stop,
84 .ping = sun4v_wdt_ping,
85 .set_timeout = sun4v_wdt_set_timeout,
88 static struct watchdog_device wdd = {
89 .info = &sun4v_wdt_ident,
90 .ops = &sun4v_wdt_ops,
91 .min_timeout = WDT_MIN_TIMEOUT,
92 .max_timeout = WDT_MAX_TIMEOUT,
93 .timeout = WDT_TIMEOUT,
96 static int __init sun4v_wdt_init(void)
98 struct mdesc_handle *handle;
102 unsigned long major = 1, minor = 1;
105 * There are 2 properties that can be set from the control
106 * domain for the watchdog.
107 * watchdog-resolution
108 * watchdog-max-timeout
110 * We can expect a handle to be returned otherwise something
111 * serious is wrong. Correct to return -ENODEV here.
114 handle = mdesc_grab();
118 node = mdesc_node_by_name(handle, MDESC_NODE_NULL, "platform");
120 if (node == MDESC_NODE_NULL)
124 * This is a safe way to validate if we are on the right
127 if (sun4v_hvapi_register(HV_GRP_CORE, major, &minor))
130 /* Allow value of watchdog-resolution up to 1s (default) */
131 value = mdesc_get_property(handle, node, "watchdog-resolution", NULL);
135 *value > WDT_DEFAULT_RESOLUTION_MS)
139 value = mdesc_get_property(handle, node, "watchdog-max-timeout", NULL);
142 * If the property value (in ms) is smaller than
143 * min_timeout, return -EINVAL.
145 if (*value < wdd.min_timeout * 1000)
149 * If the property value is smaller than
150 * default max_timeout then set watchdog max_timeout to
151 * the value of the property in seconds.
153 if (*value < wdd.max_timeout * 1000)
154 wdd.max_timeout = *value / 1000;
157 watchdog_init_timeout(&wdd, timeout, NULL);
159 watchdog_set_nowayout(&wdd, nowayout);
161 err = watchdog_register_device(&wdd);
165 pr_info("initialized (timeout=%ds, nowayout=%d)\n",
166 wdd.timeout, nowayout);
168 mdesc_release(handle);
173 sun4v_hvapi_unregister(HV_GRP_CORE);
176 mdesc_release(handle);
180 static void __exit sun4v_wdt_exit(void)
182 sun4v_hvapi_unregister(HV_GRP_CORE);
183 watchdog_unregister_device(&wdd);
186 module_init(sun4v_wdt_init);
187 module_exit(sun4v_wdt_exit);
189 MODULE_AUTHOR("Wim Coekaerts <wim.coekaerts@oracle.com>");
190 MODULE_DESCRIPTION("sun4v watchdog driver");
191 MODULE_LICENSE("GPL");