sun4v_wdt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * sun4v watchdog timer
  3. * (c) Copyright 2016 Oracle Corporation
  4. *
  5. * Implement a simple watchdog driver using the built-in sun4v hypervisor
  6. * watchdog support. If time expires, the hypervisor stops or bounces
  7. * the guest domain.
  8. *
  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.
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/errno.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/watchdog.h>
  21. #include <asm/hypervisor.h>
  22. #include <asm/mdesc.h>
  23. #define WDT_TIMEOUT 60
  24. #define WDT_MAX_TIMEOUT 31536000
  25. #define WDT_MIN_TIMEOUT 1
  26. #define WDT_DEFAULT_RESOLUTION_MS 1000 /* 1 second */
  27. static unsigned int timeout;
  28. module_param(timeout, uint, 0);
  29. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
  30. __MODULE_STRING(WDT_TIMEOUT) ")");
  31. static bool nowayout = WATCHDOG_NOWAYOUT;
  32. module_param(nowayout, bool, S_IRUGO);
  33. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  34. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  35. static int sun4v_wdt_stop(struct watchdog_device *wdd)
  36. {
  37. sun4v_mach_set_watchdog(0, NULL);
  38. return 0;
  39. }
  40. static int sun4v_wdt_ping(struct watchdog_device *wdd)
  41. {
  42. int hverr;
  43. /*
  44. * HV watchdog timer will round up the timeout
  45. * passed in to the nearest multiple of the
  46. * watchdog resolution in milliseconds.
  47. */
  48. hverr = sun4v_mach_set_watchdog(wdd->timeout * 1000, NULL);
  49. if (hverr == HV_EINVAL)
  50. return -EINVAL;
  51. return 0;
  52. }
  53. static int sun4v_wdt_set_timeout(struct watchdog_device *wdd,
  54. unsigned int timeout)
  55. {
  56. wdd->timeout = timeout;
  57. return 0;
  58. }
  59. static const struct watchdog_info sun4v_wdt_ident = {
  60. .options = WDIOF_SETTIMEOUT |
  61. WDIOF_MAGICCLOSE |
  62. WDIOF_KEEPALIVEPING,
  63. .identity = "sun4v hypervisor watchdog",
  64. .firmware_version = 0,
  65. };
  66. static struct watchdog_ops sun4v_wdt_ops = {
  67. .owner = THIS_MODULE,
  68. .start = sun4v_wdt_ping,
  69. .stop = sun4v_wdt_stop,
  70. .ping = sun4v_wdt_ping,
  71. .set_timeout = sun4v_wdt_set_timeout,
  72. };
  73. static struct watchdog_device wdd = {
  74. .info = &sun4v_wdt_ident,
  75. .ops = &sun4v_wdt_ops,
  76. .min_timeout = WDT_MIN_TIMEOUT,
  77. .max_timeout = WDT_MAX_TIMEOUT,
  78. .timeout = WDT_TIMEOUT,
  79. };
  80. static int __init sun4v_wdt_init(void)
  81. {
  82. struct mdesc_handle *handle;
  83. u64 node;
  84. const u64 *value;
  85. int err = 0;
  86. unsigned long major = 1, minor = 1;
  87. /*
  88. * There are 2 properties that can be set from the control
  89. * domain for the watchdog.
  90. * watchdog-resolution
  91. * watchdog-max-timeout
  92. *
  93. * We can expect a handle to be returned otherwise something
  94. * serious is wrong. Correct to return -ENODEV here.
  95. */
  96. handle = mdesc_grab();
  97. if (!handle)
  98. return -ENODEV;
  99. node = mdesc_node_by_name(handle, MDESC_NODE_NULL, "platform");
  100. err = -ENODEV;
  101. if (node == MDESC_NODE_NULL)
  102. goto out_release;
  103. /*
  104. * This is a safe way to validate if we are on the right
  105. * platform.
  106. */
  107. if (sun4v_hvapi_register(HV_GRP_CORE, major, &minor))
  108. goto out_hv_unreg;
  109. /* Allow value of watchdog-resolution up to 1s (default) */
  110. value = mdesc_get_property(handle, node, "watchdog-resolution", NULL);
  111. err = -EINVAL;
  112. if (value) {
  113. if (*value == 0 ||
  114. *value > WDT_DEFAULT_RESOLUTION_MS)
  115. goto out_hv_unreg;
  116. }
  117. value = mdesc_get_property(handle, node, "watchdog-max-timeout", NULL);
  118. if (value) {
  119. /*
  120. * If the property value (in ms) is smaller than
  121. * min_timeout, return -EINVAL.
  122. */
  123. if (*value < wdd.min_timeout * 1000)
  124. goto out_hv_unreg;
  125. /*
  126. * If the property value is smaller than
  127. * default max_timeout then set watchdog max_timeout to
  128. * the value of the property in seconds.
  129. */
  130. if (*value < wdd.max_timeout * 1000)
  131. wdd.max_timeout = *value / 1000;
  132. }
  133. watchdog_init_timeout(&wdd, timeout, NULL);
  134. watchdog_set_nowayout(&wdd, nowayout);
  135. err = watchdog_register_device(&wdd);
  136. if (err)
  137. goto out_hv_unreg;
  138. pr_info("initialized (timeout=%ds, nowayout=%d)\n",
  139. wdd.timeout, nowayout);
  140. mdesc_release(handle);
  141. return 0;
  142. out_hv_unreg:
  143. sun4v_hvapi_unregister(HV_GRP_CORE);
  144. out_release:
  145. mdesc_release(handle);
  146. return err;
  147. }
  148. static void __exit sun4v_wdt_exit(void)
  149. {
  150. sun4v_hvapi_unregister(HV_GRP_CORE);
  151. watchdog_unregister_device(&wdd);
  152. }
  153. module_init(sun4v_wdt_init);
  154. module_exit(sun4v_wdt_exit);
  155. MODULE_AUTHOR("Wim Coekaerts <wim.coekaerts@oracle.com>");
  156. MODULE_DESCRIPTION("sun4v watchdog driver");
  157. MODULE_LICENSE("GPL");