xen_wdt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Xen Watchdog Driver
  4. *
  5. * (c) Copyright 2010 Novell, Inc.
  6. */
  7. #define DRV_NAME "xen_wdt"
  8. #include <linux/bug.h>
  9. #include <linux/errno.h>
  10. #include <linux/fs.h>
  11. #include <linux/hrtimer.h>
  12. #include <linux/kernel.h>
  13. #include <linux/ktime.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/watchdog.h>
  19. #include <xen/xen.h>
  20. #include <asm/xen/hypercall.h>
  21. #include <xen/interface/sched.h>
  22. static struct platform_device *platform_device;
  23. static struct sched_watchdog wdt;
  24. static time64_t wdt_expires;
  25. #define WATCHDOG_TIMEOUT 60 /* in seconds */
  26. static unsigned int timeout;
  27. module_param(timeout, uint, S_IRUGO);
  28. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds "
  29. "(default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  30. static bool nowayout = WATCHDOG_NOWAYOUT;
  31. module_param(nowayout, bool, S_IRUGO);
  32. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  33. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  34. static inline time64_t set_timeout(struct watchdog_device *wdd)
  35. {
  36. wdt.timeout = wdd->timeout;
  37. return ktime_get_seconds() + wdd->timeout;
  38. }
  39. static int xen_wdt_start(struct watchdog_device *wdd)
  40. {
  41. time64_t expires;
  42. int err;
  43. expires = set_timeout(wdd);
  44. if (!wdt.id)
  45. err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
  46. else
  47. err = -EBUSY;
  48. if (err > 0) {
  49. wdt.id = err;
  50. wdt_expires = expires;
  51. err = 0;
  52. } else
  53. BUG_ON(!err);
  54. return err;
  55. }
  56. static int xen_wdt_stop(struct watchdog_device *wdd)
  57. {
  58. int err = 0;
  59. wdt.timeout = 0;
  60. if (wdt.id)
  61. err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
  62. if (!err)
  63. wdt.id = 0;
  64. return err;
  65. }
  66. static int xen_wdt_kick(struct watchdog_device *wdd)
  67. {
  68. time64_t expires;
  69. int err;
  70. expires = set_timeout(wdd);
  71. if (wdt.id)
  72. err = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wdt);
  73. else
  74. err = -ENXIO;
  75. if (!err)
  76. wdt_expires = expires;
  77. return err;
  78. }
  79. static unsigned int xen_wdt_get_timeleft(struct watchdog_device *wdd)
  80. {
  81. return wdt_expires - ktime_get_seconds();
  82. }
  83. static struct watchdog_info xen_wdt_info = {
  84. .identity = DRV_NAME,
  85. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  86. };
  87. static const struct watchdog_ops xen_wdt_ops = {
  88. .owner = THIS_MODULE,
  89. .start = xen_wdt_start,
  90. .stop = xen_wdt_stop,
  91. .ping = xen_wdt_kick,
  92. .get_timeleft = xen_wdt_get_timeleft,
  93. };
  94. static struct watchdog_device xen_wdt_dev = {
  95. .info = &xen_wdt_info,
  96. .ops = &xen_wdt_ops,
  97. .timeout = WATCHDOG_TIMEOUT,
  98. };
  99. static int xen_wdt_probe(struct platform_device *pdev)
  100. {
  101. struct device *dev = &pdev->dev;
  102. struct sched_watchdog wd = { .id = ~0 };
  103. int ret = HYPERVISOR_sched_op(SCHEDOP_watchdog, &wd);
  104. if (ret == -ENOSYS) {
  105. dev_err(dev, "watchdog not supported by hypervisor\n");
  106. return -ENODEV;
  107. }
  108. if (ret != -EINVAL) {
  109. dev_err(dev, "unexpected hypervisor error (%d)\n", ret);
  110. return -ENODEV;
  111. }
  112. watchdog_init_timeout(&xen_wdt_dev, timeout, NULL);
  113. watchdog_set_nowayout(&xen_wdt_dev, nowayout);
  114. watchdog_stop_on_reboot(&xen_wdt_dev);
  115. watchdog_stop_on_unregister(&xen_wdt_dev);
  116. ret = devm_watchdog_register_device(dev, &xen_wdt_dev);
  117. if (ret)
  118. return ret;
  119. dev_info(dev, "initialized (timeout=%ds, nowayout=%d)\n",
  120. xen_wdt_dev.timeout, nowayout);
  121. return 0;
  122. }
  123. static int xen_wdt_suspend(struct platform_device *dev, pm_message_t state)
  124. {
  125. typeof(wdt.id) id = wdt.id;
  126. int rc = xen_wdt_stop(&xen_wdt_dev);
  127. wdt.id = id;
  128. return rc;
  129. }
  130. static int xen_wdt_resume(struct platform_device *dev)
  131. {
  132. if (!wdt.id)
  133. return 0;
  134. wdt.id = 0;
  135. return xen_wdt_start(&xen_wdt_dev);
  136. }
  137. static struct platform_driver xen_wdt_driver = {
  138. .probe = xen_wdt_probe,
  139. .suspend = xen_wdt_suspend,
  140. .resume = xen_wdt_resume,
  141. .driver = {
  142. .name = DRV_NAME,
  143. },
  144. };
  145. static int __init xen_wdt_init_module(void)
  146. {
  147. int err;
  148. if (!xen_domain())
  149. return -ENODEV;
  150. err = platform_driver_register(&xen_wdt_driver);
  151. if (err)
  152. return err;
  153. platform_device = platform_device_register_simple(DRV_NAME,
  154. -1, NULL, 0);
  155. if (IS_ERR(platform_device)) {
  156. err = PTR_ERR(platform_device);
  157. platform_driver_unregister(&xen_wdt_driver);
  158. }
  159. return err;
  160. }
  161. static void __exit xen_wdt_cleanup_module(void)
  162. {
  163. platform_device_unregister(platform_device);
  164. platform_driver_unregister(&xen_wdt_driver);
  165. }
  166. module_init(xen_wdt_init_module);
  167. module_exit(xen_wdt_cleanup_module);
  168. MODULE_AUTHOR("Jan Beulich <jbeulich@novell.com>");
  169. MODULE_DESCRIPTION("Xen WatchDog Timer Driver");
  170. MODULE_LICENSE("GPL");