Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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