blob: b343f421dc72c597e818315767ccb65d908639a3 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jan Beulich066d6c72010-10-04 10:37:26 +01002/*
3 * Xen Watchdog Driver
4 *
5 * (c) Copyright 2010 Novell, Inc.
Jan Beulich066d6c72010-10-04 10:37:26 +01006 */
7
Radu Rendec18cffd62017-11-15 19:34:41 +00008#define DRV_NAME "xen_wdt"
Jan Beulich066d6c72010-10-04 10:37:26 +01009
10#include <linux/bug.h>
11#include <linux/errno.h>
12#include <linux/fs.h>
13#include <linux/hrtimer.h>
14#include <linux/kernel.h>
15#include <linux/ktime.h>
16#include <linux/init.h>
Jan Beulich066d6c72010-10-04 10:37:26 +010017#include <linux/module.h>
18#include <linux/moduleparam.h>
19#include <linux/platform_device.h>
Jan Beulich066d6c72010-10-04 10:37:26 +010020#include <linux/watchdog.h>
21#include <xen/xen.h>
22#include <asm/xen/hypercall.h>
23#include <xen/interface/sched.h>
24
25static struct platform_device *platform_device;
Jan Beulich066d6c72010-10-04 10:37:26 +010026static struct sched_watchdog wdt;
Arnd Bergmannb6c84c92017-10-19 17:05:48 +020027static time64_t wdt_expires;
Jan Beulich066d6c72010-10-04 10:37:26 +010028
29#define WATCHDOG_TIMEOUT 60 /* in seconds */
Radu Rendec18cffd62017-11-15 19:34:41 +000030static unsigned int timeout;
Jan Beulich066d6c72010-10-04 10:37:26 +010031module_param(timeout, uint, S_IRUGO);
32MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
33 "(default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
34
35static bool nowayout = WATCHDOG_NOWAYOUT;
36module_param(nowayout, bool, S_IRUGO);
37MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
38 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
39
Radu Rendec18cffd62017-11-15 19:34:41 +000040static inline time64_t set_timeout(struct watchdog_device *wdd)
Jan Beulich066d6c72010-10-04 10:37:26 +010041{
Radu Rendec18cffd62017-11-15 19:34:41 +000042 wdt.timeout = wdd->timeout;
43 return ktime_get_seconds() + wdd->timeout;
Jan Beulich066d6c72010-10-04 10:37:26 +010044}
45
Radu Rendec18cffd62017-11-15 19:34:41 +000046static int xen_wdt_start(struct watchdog_device *wdd)
Jan Beulich066d6c72010-10-04 10:37:26 +010047{
Arnd Bergmannb6c84c92017-10-19 17:05:48 +020048 time64_t expires;
Jan Beulich066d6c72010-10-04 10:37:26 +010049 int err;
50
Radu Rendec18cffd62017-11-15 19:34:41 +000051 expires = set_timeout(wdd);
Jan Beulich066d6c72010-10-04 10:37:26 +010052 if (!wdt.id)
53 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
54 else
55 err = -EBUSY;
56 if (err > 0) {
57 wdt.id = err;
58 wdt_expires = expires;
59 err = 0;
60 } else
61 BUG_ON(!err);
62
Jan Beulich066d6c72010-10-04 10:37:26 +010063 return err;
64}
65
Radu Rendec18cffd62017-11-15 19:34:41 +000066static int xen_wdt_stop(struct watchdog_device *wdd)
Jan Beulich066d6c72010-10-04 10:37:26 +010067{
68 int err = 0;
69
Jan Beulich066d6c72010-10-04 10:37:26 +010070 wdt.timeout = 0;
71 if (wdt.id)
72 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
73 if (!err)
74 wdt.id = 0;
75
Jan Beulich066d6c72010-10-04 10:37:26 +010076 return err;
77}
78
Radu Rendec18cffd62017-11-15 19:34:41 +000079static int xen_wdt_kick(struct watchdog_device *wdd)
Jan Beulich066d6c72010-10-04 10:37:26 +010080{
Arnd Bergmannb6c84c92017-10-19 17:05:48 +020081 time64_t expires;
Jan Beulich066d6c72010-10-04 10:37:26 +010082 int err;
83
Radu Rendec18cffd62017-11-15 19:34:41 +000084 expires = set_timeout(wdd);
Jan Beulich066d6c72010-10-04 10:37:26 +010085 if (wdt.id)
86 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
87 else
88 err = -ENXIO;
89 if (!err)
90 wdt_expires = expires;
91
Jan Beulich066d6c72010-10-04 10:37:26 +010092 return err;
93}
94
Radu Rendec18cffd62017-11-15 19:34:41 +000095static unsigned int xen_wdt_get_timeleft(struct watchdog_device *wdd)
Jan Beulich066d6c72010-10-04 10:37:26 +010096{
Radu Rendec18cffd62017-11-15 19:34:41 +000097 return wdt_expires - ktime_get_seconds();
Jan Beulich066d6c72010-10-04 10:37:26 +010098}
99
Radu Rendec18cffd62017-11-15 19:34:41 +0000100static struct watchdog_info xen_wdt_info = {
101 .identity = DRV_NAME,
102 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
Jan Beulich066d6c72010-10-04 10:37:26 +0100103};
104
Radu Rendec18cffd62017-11-15 19:34:41 +0000105static const struct watchdog_ops xen_wdt_ops = {
106 .owner = THIS_MODULE,
107 .start = xen_wdt_start,
108 .stop = xen_wdt_stop,
109 .ping = xen_wdt_kick,
110 .get_timeleft = xen_wdt_get_timeleft,
Jan Beulich066d6c72010-10-04 10:37:26 +0100111};
112
Radu Rendec18cffd62017-11-15 19:34:41 +0000113static struct watchdog_device xen_wdt_dev = {
114 .info = &xen_wdt_info,
115 .ops = &xen_wdt_ops,
116 .timeout = WATCHDOG_TIMEOUT,
117};
118
119static int xen_wdt_probe(struct platform_device *pdev)
Jan Beulich066d6c72010-10-04 10:37:26 +0100120{
Guenter Roeckb90abaa2019-04-10 09:27:50 -0700121 struct device *dev = &pdev->dev;
Jan Beulich066d6c72010-10-04 10:37:26 +0100122 struct sched_watchdog wd = { .id = ~0 };
123 int ret = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wd);
124
Radu Rendec18cffd62017-11-15 19:34:41 +0000125 if (ret == -ENOSYS) {
Guenter Roeckb90abaa2019-04-10 09:27:50 -0700126 dev_err(dev, "watchdog not supported by hypervisor\n");
Radu Rendec18cffd62017-11-15 19:34:41 +0000127 return -ENODEV;
Jan Beulich066d6c72010-10-04 10:37:26 +0100128 }
129
Radu Rendec18cffd62017-11-15 19:34:41 +0000130 if (ret != -EINVAL) {
Guenter Roeckb90abaa2019-04-10 09:27:50 -0700131 dev_err(dev, "unexpected hypervisor error (%d)\n", ret);
Radu Rendec18cffd62017-11-15 19:34:41 +0000132 return -ENODEV;
133 }
Jan Beulich066d6c72010-10-04 10:37:26 +0100134
Wolfram Sangb74d6462019-04-19 20:16:00 +0200135 watchdog_init_timeout(&xen_wdt_dev, timeout, NULL);
Radu Rendec18cffd62017-11-15 19:34:41 +0000136 watchdog_set_nowayout(&xen_wdt_dev, nowayout);
137 watchdog_stop_on_reboot(&xen_wdt_dev);
138 watchdog_stop_on_unregister(&xen_wdt_dev);
Jan Beulich066d6c72010-10-04 10:37:26 +0100139
Guenter Roeckb90abaa2019-04-10 09:27:50 -0700140 ret = devm_watchdog_register_device(dev, &xen_wdt_dev);
Wolfram Sange1465132019-05-18 23:28:01 +0200141 if (ret)
Radu Rendec18cffd62017-11-15 19:34:41 +0000142 return ret;
Radu Rendec18cffd62017-11-15 19:34:41 +0000143
Guenter Roeckb90abaa2019-04-10 09:27:50 -0700144 dev_info(dev, "initialized (timeout=%ds, nowayout=%d)\n",
145 xen_wdt_dev.timeout, nowayout);
Jan Beulich066d6c72010-10-04 10:37:26 +0100146
147 return 0;
148}
149
Jan Beulich066d6c72010-10-04 10:37:26 +0100150static int xen_wdt_suspend(struct platform_device *dev, pm_message_t state)
151{
Jan Beulich83448bf2012-03-19 09:30:33 +0000152 typeof(wdt.id) id = wdt.id;
Radu Rendec18cffd62017-11-15 19:34:41 +0000153 int rc = xen_wdt_stop(&xen_wdt_dev);
Jan Beulich83448bf2012-03-19 09:30:33 +0000154
155 wdt.id = id;
156 return rc;
Jan Beulich066d6c72010-10-04 10:37:26 +0100157}
158
159static int xen_wdt_resume(struct platform_device *dev)
160{
Jan Beulich83448bf2012-03-19 09:30:33 +0000161 if (!wdt.id)
162 return 0;
163 wdt.id = 0;
Radu Rendec18cffd62017-11-15 19:34:41 +0000164 return xen_wdt_start(&xen_wdt_dev);
Jan Beulich066d6c72010-10-04 10:37:26 +0100165}
166
167static struct platform_driver xen_wdt_driver = {
168 .probe = xen_wdt_probe,
Jan Beulich066d6c72010-10-04 10:37:26 +0100169 .suspend = xen_wdt_suspend,
170 .resume = xen_wdt_resume,
171 .driver = {
Jan Beulich066d6c72010-10-04 10:37:26 +0100172 .name = DRV_NAME,
173 },
174};
175
176static int __init xen_wdt_init_module(void)
177{
178 int err;
179
180 if (!xen_domain())
181 return -ENODEV;
182
Jan Beulich066d6c72010-10-04 10:37:26 +0100183 err = platform_driver_register(&xen_wdt_driver);
184 if (err)
185 return err;
186
187 platform_device = platform_device_register_simple(DRV_NAME,
188 -1, NULL, 0);
189 if (IS_ERR(platform_device)) {
190 err = PTR_ERR(platform_device);
191 platform_driver_unregister(&xen_wdt_driver);
192 }
193
194 return err;
195}
196
197static void __exit xen_wdt_cleanup_module(void)
198{
199 platform_device_unregister(platform_device);
200 platform_driver_unregister(&xen_wdt_driver);
Jan Beulich066d6c72010-10-04 10:37:26 +0100201}
202
203module_init(xen_wdt_init_module);
204module_exit(xen_wdt_cleanup_module);
205
206MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
207MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
Jan Beulich066d6c72010-10-04 10:37:26 +0100208MODULE_LICENSE("GPL");