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