]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/watchdog/xen_wdt.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[mirror_ubuntu-focal-kernel.git] / drivers / watchdog / xen_wdt.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
066d6c7f
JB
2/*
3 * Xen Watchdog Driver
4 *
5 * (c) Copyright 2010 Novell, Inc.
066d6c7f
JB
6 */
7
18cffd68 8#define DRV_NAME "xen_wdt"
066d6c7f
JB
9
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>
066d6c7f
JB
17#include <linux/module.h>
18#include <linux/moduleparam.h>
19#include <linux/platform_device.h>
066d6c7f
JB
20#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;
066d6c7f 26static struct sched_watchdog wdt;
b6c84c9f 27static time64_t wdt_expires;
066d6c7f
JB
28
29#define WATCHDOG_TIMEOUT 60 /* in seconds */
18cffd68 30static unsigned int timeout;
066d6c7f
JB
31module_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
18cffd68 40static inline time64_t set_timeout(struct watchdog_device *wdd)
066d6c7f 41{
18cffd68
RR
42 wdt.timeout = wdd->timeout;
43 return ktime_get_seconds() + wdd->timeout;
066d6c7f
JB
44}
45
18cffd68 46static int xen_wdt_start(struct watchdog_device *wdd)
066d6c7f 47{
b6c84c9f 48 time64_t expires;
066d6c7f
JB
49 int err;
50
18cffd68 51 expires = set_timeout(wdd);
066d6c7f
JB
52 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
066d6c7f
JB
63 return err;
64}
65
18cffd68 66static int xen_wdt_stop(struct watchdog_device *wdd)
066d6c7f
JB
67{
68 int err = 0;
69
066d6c7f
JB
70 wdt.timeout = 0;
71 if (wdt.id)
72 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
73 if (!err)
74 wdt.id = 0;
75
066d6c7f
JB
76 return err;
77}
78
18cffd68 79static int xen_wdt_kick(struct watchdog_device *wdd)
066d6c7f 80{
b6c84c9f 81 time64_t expires;
066d6c7f
JB
82 int err;
83
18cffd68 84 expires = set_timeout(wdd);
066d6c7f
JB
85 if (wdt.id)
86 err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
87 else
88 err = -ENXIO;
89 if (!err)
90 wdt_expires = expires;
91
066d6c7f
JB
92 return err;
93}
94
18cffd68 95static unsigned int xen_wdt_get_timeleft(struct watchdog_device *wdd)
066d6c7f 96{
18cffd68 97 return wdt_expires - ktime_get_seconds();
066d6c7f
JB
98}
99
18cffd68
RR
100static struct watchdog_info xen_wdt_info = {
101 .identity = DRV_NAME,
102 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
103};
066d6c7f 104
18cffd68
RR
105static 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,
066d6c7f
JB
111};
112
18cffd68
RR
113static struct watchdog_device xen_wdt_dev = {
114 .info = &xen_wdt_info,
115 .ops = &xen_wdt_ops,
116 .timeout = WATCHDOG_TIMEOUT,
066d6c7f
JB
117};
118
18cffd68 119static int xen_wdt_probe(struct platform_device *pdev)
066d6c7f 120{
b90abaac 121 struct device *dev = &pdev->dev;
066d6c7f
JB
122 struct sched_watchdog wd = { .id = ~0 };
123 int ret = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wd);
124
18cffd68 125 if (ret == -ENOSYS) {
b90abaac 126 dev_err(dev, "watchdog not supported by hypervisor\n");
18cffd68 127 return -ENODEV;
066d6c7f
JB
128 }
129
18cffd68 130 if (ret != -EINVAL) {
b90abaac 131 dev_err(dev, "unexpected hypervisor error (%d)\n", ret);
18cffd68
RR
132 return -ENODEV;
133 }
066d6c7f 134
b74d6461 135 watchdog_init_timeout(&xen_wdt_dev, timeout, NULL);
18cffd68
RR
136 watchdog_set_nowayout(&xen_wdt_dev, nowayout);
137 watchdog_stop_on_reboot(&xen_wdt_dev);
138 watchdog_stop_on_unregister(&xen_wdt_dev);
139
b90abaac 140 ret = devm_watchdog_register_device(dev, &xen_wdt_dev);
18cffd68 141 if (ret) {
b90abaac 142 dev_err(dev, "cannot register watchdog device (%d)\n", ret);
18cffd68
RR
143 return ret;
144 }
066d6c7f 145
b90abaac
GR
146 dev_info(dev, "initialized (timeout=%ds, nowayout=%d)\n",
147 xen_wdt_dev.timeout, nowayout);
066d6c7f
JB
148
149 return 0;
150}
151
066d6c7f
JB
152static int xen_wdt_suspend(struct platform_device *dev, pm_message_t state)
153{
83448bf7 154 typeof(wdt.id) id = wdt.id;
18cffd68 155 int rc = xen_wdt_stop(&xen_wdt_dev);
83448bf7
JB
156
157 wdt.id = id;
158 return rc;
066d6c7f
JB
159}
160
161static int xen_wdt_resume(struct platform_device *dev)
162{
83448bf7
JB
163 if (!wdt.id)
164 return 0;
165 wdt.id = 0;
18cffd68 166 return xen_wdt_start(&xen_wdt_dev);
066d6c7f
JB
167}
168
169static struct platform_driver xen_wdt_driver = {
170 .probe = xen_wdt_probe,
066d6c7f
JB
171 .suspend = xen_wdt_suspend,
172 .resume = xen_wdt_resume,
173 .driver = {
066d6c7f
JB
174 .name = DRV_NAME,
175 },
176};
177
178static int __init xen_wdt_init_module(void)
179{
180 int err;
181
182 if (!xen_domain())
183 return -ENODEV;
184
066d6c7f
JB
185 err = platform_driver_register(&xen_wdt_driver);
186 if (err)
187 return err;
188
189 platform_device = platform_device_register_simple(DRV_NAME,
190 -1, NULL, 0);
191 if (IS_ERR(platform_device)) {
192 err = PTR_ERR(platform_device);
193 platform_driver_unregister(&xen_wdt_driver);
194 }
195
196 return err;
197}
198
199static void __exit xen_wdt_cleanup_module(void)
200{
201 platform_device_unregister(platform_device);
202 platform_driver_unregister(&xen_wdt_driver);
066d6c7f
JB
203}
204
205module_init(xen_wdt_init_module);
206module_exit(xen_wdt_cleanup_module);
207
208MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
209MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
066d6c7f 210MODULE_LICENSE("GPL");