4 * (c) Copyright 2010 Novell, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #define DRV_NAME "xen_wdt"
14 #include <linux/bug.h>
15 #include <linux/errno.h>
17 #include <linux/hrtimer.h>
18 #include <linux/kernel.h>
19 #include <linux/ktime.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/platform_device.h>
24 #include <linux/watchdog.h>
26 #include <asm/xen/hypercall.h>
27 #include <xen/interface/sched.h>
29 static struct platform_device
*platform_device
;
30 static struct sched_watchdog wdt
;
31 static time64_t wdt_expires
;
33 #define WATCHDOG_TIMEOUT 60 /* in seconds */
34 static unsigned int timeout
;
35 module_param(timeout
, uint
, S_IRUGO
);
36 MODULE_PARM_DESC(timeout
, "Watchdog timeout in seconds "
37 "(default=" __MODULE_STRING(WATCHDOG_TIMEOUT
) ")");
39 static bool nowayout
= WATCHDOG_NOWAYOUT
;
40 module_param(nowayout
, bool, S_IRUGO
);
41 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started "
42 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
44 static inline time64_t
set_timeout(struct watchdog_device
*wdd
)
46 wdt
.timeout
= wdd
->timeout
;
47 return ktime_get_seconds() + wdd
->timeout
;
50 static int xen_wdt_start(struct watchdog_device
*wdd
)
55 expires
= set_timeout(wdd
);
57 err
= HYPERVISOR_sched_op(SCHEDOP_watchdog
, &wdt
);
62 wdt_expires
= expires
;
70 static int xen_wdt_stop(struct watchdog_device
*wdd
)
76 err
= HYPERVISOR_sched_op(SCHEDOP_watchdog
, &wdt
);
83 static int xen_wdt_kick(struct watchdog_device
*wdd
)
88 expires
= set_timeout(wdd
);
90 err
= HYPERVISOR_sched_op(SCHEDOP_watchdog
, &wdt
);
94 wdt_expires
= expires
;
99 static unsigned int xen_wdt_get_timeleft(struct watchdog_device
*wdd
)
101 return wdt_expires
- ktime_get_seconds();
104 static struct watchdog_info xen_wdt_info
= {
105 .identity
= DRV_NAME
,
106 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
109 static const struct watchdog_ops xen_wdt_ops
= {
110 .owner
= THIS_MODULE
,
111 .start
= xen_wdt_start
,
112 .stop
= xen_wdt_stop
,
113 .ping
= xen_wdt_kick
,
114 .get_timeleft
= xen_wdt_get_timeleft
,
117 static struct watchdog_device xen_wdt_dev
= {
118 .info
= &xen_wdt_info
,
120 .timeout
= WATCHDOG_TIMEOUT
,
123 static int xen_wdt_probe(struct platform_device
*pdev
)
125 struct sched_watchdog wd
= { .id
= ~0 };
126 int ret
= HYPERVISOR_sched_op(SCHEDOP_watchdog
, &wd
);
128 if (ret
== -ENOSYS
) {
129 dev_err(&pdev
->dev
, "watchdog not supported by hypervisor\n");
133 if (ret
!= -EINVAL
) {
134 dev_err(&pdev
->dev
, "unexpected hypervisor error (%d)\n", ret
);
138 if (watchdog_init_timeout(&xen_wdt_dev
, timeout
, NULL
))
139 dev_info(&pdev
->dev
, "timeout value invalid, using %d\n",
140 xen_wdt_dev
.timeout
);
141 watchdog_set_nowayout(&xen_wdt_dev
, nowayout
);
142 watchdog_stop_on_reboot(&xen_wdt_dev
);
143 watchdog_stop_on_unregister(&xen_wdt_dev
);
145 ret
= devm_watchdog_register_device(&pdev
->dev
, &xen_wdt_dev
);
147 dev_err(&pdev
->dev
, "cannot register watchdog device (%d)\n",
152 dev_info(&pdev
->dev
, "initialized (timeout=%ds, nowayout=%d)\n",
153 xen_wdt_dev
.timeout
, nowayout
);
158 static int xen_wdt_suspend(struct platform_device
*dev
, pm_message_t state
)
160 typeof(wdt
.id
) id
= wdt
.id
;
161 int rc
= xen_wdt_stop(&xen_wdt_dev
);
167 static int xen_wdt_resume(struct platform_device
*dev
)
172 return xen_wdt_start(&xen_wdt_dev
);
175 static struct platform_driver xen_wdt_driver
= {
176 .probe
= xen_wdt_probe
,
177 .suspend
= xen_wdt_suspend
,
178 .resume
= xen_wdt_resume
,
184 static int __init
xen_wdt_init_module(void)
191 err
= platform_driver_register(&xen_wdt_driver
);
195 platform_device
= platform_device_register_simple(DRV_NAME
,
197 if (IS_ERR(platform_device
)) {
198 err
= PTR_ERR(platform_device
);
199 platform_driver_unregister(&xen_wdt_driver
);
205 static void __exit
xen_wdt_cleanup_module(void)
207 platform_device_unregister(platform_device
);
208 platform_driver_unregister(&xen_wdt_driver
);
211 module_init(xen_wdt_init_module
);
212 module_exit(xen_wdt_cleanup_module
);
214 MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
215 MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
216 MODULE_LICENSE("GPL");