xref: /openbmc/linux/drivers/watchdog/pseries-wdt.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1*69472ffaSScott Cheloha // SPDX-License-Identifier: GPL-2.0-or-later
2*69472ffaSScott Cheloha /*
3*69472ffaSScott Cheloha  * Copyright (c) 2022 International Business Machines, Inc.
4*69472ffaSScott Cheloha  */
5*69472ffaSScott Cheloha 
6*69472ffaSScott Cheloha #include <linux/bitops.h>
7*69472ffaSScott Cheloha #include <linux/kernel.h>
8*69472ffaSScott Cheloha #include <linux/limits.h>
9*69472ffaSScott Cheloha #include <linux/math.h>
10*69472ffaSScott Cheloha #include <linux/mod_devicetable.h>
11*69472ffaSScott Cheloha #include <linux/module.h>
12*69472ffaSScott Cheloha #include <linux/moduleparam.h>
13*69472ffaSScott Cheloha #include <linux/platform_device.h>
14*69472ffaSScott Cheloha #include <linux/time64.h>
15*69472ffaSScott Cheloha #include <linux/watchdog.h>
16*69472ffaSScott Cheloha 
17*69472ffaSScott Cheloha #define DRV_NAME "pseries-wdt"
18*69472ffaSScott Cheloha 
19*69472ffaSScott Cheloha /*
20*69472ffaSScott Cheloha  * H_WATCHDOG Input
21*69472ffaSScott Cheloha  *
22*69472ffaSScott Cheloha  * R4: "flags":
23*69472ffaSScott Cheloha  *
24*69472ffaSScott Cheloha  *         Bits 48-55: "operation"
25*69472ffaSScott Cheloha  */
26*69472ffaSScott Cheloha #define PSERIES_WDTF_OP_START	0x100UL		/* start timer */
27*69472ffaSScott Cheloha #define PSERIES_WDTF_OP_STOP	0x200UL		/* stop timer */
28*69472ffaSScott Cheloha #define PSERIES_WDTF_OP_QUERY	0x300UL		/* query timer capabilities */
29*69472ffaSScott Cheloha 
30*69472ffaSScott Cheloha /*
31*69472ffaSScott Cheloha  *         Bits 56-63: "timeoutAction" (for "Start Watchdog" only)
32*69472ffaSScott Cheloha  */
33*69472ffaSScott Cheloha #define PSERIES_WDTF_ACTION_HARD_POWEROFF	0x1UL	/* poweroff */
34*69472ffaSScott Cheloha #define PSERIES_WDTF_ACTION_HARD_RESTART	0x2UL	/* restart */
35*69472ffaSScott Cheloha #define PSERIES_WDTF_ACTION_DUMP_RESTART	0x3UL	/* dump + restart */
36*69472ffaSScott Cheloha 
37*69472ffaSScott Cheloha /*
38*69472ffaSScott Cheloha  * H_WATCHDOG Output
39*69472ffaSScott Cheloha  *
40*69472ffaSScott Cheloha  * R3: Return code
41*69472ffaSScott Cheloha  *
42*69472ffaSScott Cheloha  *     H_SUCCESS    The operation completed.
43*69472ffaSScott Cheloha  *
44*69472ffaSScott Cheloha  *     H_BUSY	    The hypervisor is too busy; retry the operation.
45*69472ffaSScott Cheloha  *
46*69472ffaSScott Cheloha  *     H_PARAMETER  The given "flags" are somehow invalid.  Either the
47*69472ffaSScott Cheloha  *                  "operation" or "timeoutAction" is invalid, or a
48*69472ffaSScott Cheloha  *                  reserved bit is set.
49*69472ffaSScott Cheloha  *
50*69472ffaSScott Cheloha  *     H_P2         The given "watchdogNumber" is zero or exceeds the
51*69472ffaSScott Cheloha  *                  supported maximum value.
52*69472ffaSScott Cheloha  *
53*69472ffaSScott Cheloha  *     H_P3         The given "timeoutInMs" is below the supported
54*69472ffaSScott Cheloha  *                  minimum value.
55*69472ffaSScott Cheloha  *
56*69472ffaSScott Cheloha  *     H_NOOP       The given "watchdogNumber" is already stopped.
57*69472ffaSScott Cheloha  *
58*69472ffaSScott Cheloha  *     H_HARDWARE   The operation failed for ineffable reasons.
59*69472ffaSScott Cheloha  *
60*69472ffaSScott Cheloha  *     H_FUNCTION   The H_WATCHDOG hypercall is not supported by this
61*69472ffaSScott Cheloha  *                  hypervisor.
62*69472ffaSScott Cheloha  *
63*69472ffaSScott Cheloha  * R4:
64*69472ffaSScott Cheloha  *
65*69472ffaSScott Cheloha  * - For the "Query Watchdog Capabilities" operation, a 64-bit
66*69472ffaSScott Cheloha  *   structure:
67*69472ffaSScott Cheloha  */
68*69472ffaSScott Cheloha #define PSERIES_WDTQ_MIN_TIMEOUT(cap)	(((cap) >> 48) & 0xffff)
69*69472ffaSScott Cheloha #define PSERIES_WDTQ_MAX_NUMBER(cap)	(((cap) >> 32) & 0xffff)
70*69472ffaSScott Cheloha 
71*69472ffaSScott Cheloha static const unsigned long pseries_wdt_action[] = {
72*69472ffaSScott Cheloha 	[0] = PSERIES_WDTF_ACTION_HARD_POWEROFF,
73*69472ffaSScott Cheloha 	[1] = PSERIES_WDTF_ACTION_HARD_RESTART,
74*69472ffaSScott Cheloha 	[2] = PSERIES_WDTF_ACTION_DUMP_RESTART,
75*69472ffaSScott Cheloha };
76*69472ffaSScott Cheloha 
77*69472ffaSScott Cheloha #define WATCHDOG_ACTION 1
78*69472ffaSScott Cheloha static unsigned int action = WATCHDOG_ACTION;
79*69472ffaSScott Cheloha module_param(action, uint, 0444);
80*69472ffaSScott Cheloha MODULE_PARM_DESC(action, "Action taken when watchdog expires (default="
81*69472ffaSScott Cheloha 		 __MODULE_STRING(WATCHDOG_ACTION) ")");
82*69472ffaSScott Cheloha 
83*69472ffaSScott Cheloha static bool nowayout = WATCHDOG_NOWAYOUT;
84*69472ffaSScott Cheloha module_param(nowayout, bool, 0444);
85*69472ffaSScott Cheloha MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
86*69472ffaSScott Cheloha 		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
87*69472ffaSScott Cheloha 
88*69472ffaSScott Cheloha #define WATCHDOG_TIMEOUT 60
89*69472ffaSScott Cheloha static unsigned int timeout = WATCHDOG_TIMEOUT;
90*69472ffaSScott Cheloha module_param(timeout, uint, 0444);
91*69472ffaSScott Cheloha MODULE_PARM_DESC(timeout, "Initial watchdog timeout in seconds (default="
92*69472ffaSScott Cheloha 		 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
93*69472ffaSScott Cheloha 
94*69472ffaSScott Cheloha struct pseries_wdt {
95*69472ffaSScott Cheloha 	struct watchdog_device wd;
96*69472ffaSScott Cheloha 	unsigned long action;
97*69472ffaSScott Cheloha 	unsigned long num;		/* Watchdog numbers are 1-based */
98*69472ffaSScott Cheloha };
99*69472ffaSScott Cheloha 
pseries_wdt_start(struct watchdog_device * wdd)100*69472ffaSScott Cheloha static int pseries_wdt_start(struct watchdog_device *wdd)
101*69472ffaSScott Cheloha {
102*69472ffaSScott Cheloha 	struct pseries_wdt *pw = watchdog_get_drvdata(wdd);
103*69472ffaSScott Cheloha 	struct device *dev = wdd->parent;
104*69472ffaSScott Cheloha 	unsigned long flags, msecs;
105*69472ffaSScott Cheloha 	long rc;
106*69472ffaSScott Cheloha 
107*69472ffaSScott Cheloha 	flags = pw->action | PSERIES_WDTF_OP_START;
108*69472ffaSScott Cheloha 	msecs = wdd->timeout * MSEC_PER_SEC;
109*69472ffaSScott Cheloha 	rc = plpar_hcall_norets(H_WATCHDOG, flags, pw->num, msecs);
110*69472ffaSScott Cheloha 	if (rc != H_SUCCESS) {
111*69472ffaSScott Cheloha 		dev_crit(dev, "H_WATCHDOG: %ld: failed to start timer %lu",
112*69472ffaSScott Cheloha 			 rc, pw->num);
113*69472ffaSScott Cheloha 		return -EIO;
114*69472ffaSScott Cheloha 	}
115*69472ffaSScott Cheloha 	return 0;
116*69472ffaSScott Cheloha }
117*69472ffaSScott Cheloha 
pseries_wdt_stop(struct watchdog_device * wdd)118*69472ffaSScott Cheloha static int pseries_wdt_stop(struct watchdog_device *wdd)
119*69472ffaSScott Cheloha {
120*69472ffaSScott Cheloha 	struct pseries_wdt *pw = watchdog_get_drvdata(wdd);
121*69472ffaSScott Cheloha 	struct device *dev = wdd->parent;
122*69472ffaSScott Cheloha 	long rc;
123*69472ffaSScott Cheloha 
124*69472ffaSScott Cheloha 	rc = plpar_hcall_norets(H_WATCHDOG, PSERIES_WDTF_OP_STOP, pw->num);
125*69472ffaSScott Cheloha 	if (rc != H_SUCCESS && rc != H_NOOP) {
126*69472ffaSScott Cheloha 		dev_crit(dev, "H_WATCHDOG: %ld: failed to stop timer %lu",
127*69472ffaSScott Cheloha 			 rc, pw->num);
128*69472ffaSScott Cheloha 		return -EIO;
129*69472ffaSScott Cheloha 	}
130*69472ffaSScott Cheloha 	return 0;
131*69472ffaSScott Cheloha }
132*69472ffaSScott Cheloha 
133*69472ffaSScott Cheloha static struct watchdog_info pseries_wdt_info = {
134*69472ffaSScott Cheloha 	.identity = DRV_NAME,
135*69472ffaSScott Cheloha 	.options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT
136*69472ffaSScott Cheloha 	    | WDIOF_PRETIMEOUT,
137*69472ffaSScott Cheloha };
138*69472ffaSScott Cheloha 
139*69472ffaSScott Cheloha static const struct watchdog_ops pseries_wdt_ops = {
140*69472ffaSScott Cheloha 	.owner = THIS_MODULE,
141*69472ffaSScott Cheloha 	.start = pseries_wdt_start,
142*69472ffaSScott Cheloha 	.stop = pseries_wdt_stop,
143*69472ffaSScott Cheloha };
144*69472ffaSScott Cheloha 
pseries_wdt_probe(struct platform_device * pdev)145*69472ffaSScott Cheloha static int pseries_wdt_probe(struct platform_device *pdev)
146*69472ffaSScott Cheloha {
147*69472ffaSScott Cheloha 	unsigned long ret[PLPAR_HCALL_BUFSIZE] = { 0 };
148*69472ffaSScott Cheloha 	struct pseries_wdt *pw;
149*69472ffaSScott Cheloha 	unsigned long cap;
150*69472ffaSScott Cheloha 	long msecs, rc;
151*69472ffaSScott Cheloha 	int err;
152*69472ffaSScott Cheloha 
153*69472ffaSScott Cheloha 	rc = plpar_hcall(H_WATCHDOG, ret, PSERIES_WDTF_OP_QUERY);
154*69472ffaSScott Cheloha 	if (rc == H_FUNCTION)
155*69472ffaSScott Cheloha 		return -ENODEV;
156*69472ffaSScott Cheloha 	if (rc != H_SUCCESS)
157*69472ffaSScott Cheloha 		return -EIO;
158*69472ffaSScott Cheloha 	cap = ret[0];
159*69472ffaSScott Cheloha 
160*69472ffaSScott Cheloha 	pw = devm_kzalloc(&pdev->dev, sizeof(*pw), GFP_KERNEL);
161*69472ffaSScott Cheloha 	if (!pw)
162*69472ffaSScott Cheloha 		return -ENOMEM;
163*69472ffaSScott Cheloha 
164*69472ffaSScott Cheloha 	/*
165*69472ffaSScott Cheloha 	 * Assume watchdogNumber 1 for now.  If we ever support
166*69472ffaSScott Cheloha 	 * multiple timers we will need to devise a way to choose a
167*69472ffaSScott Cheloha 	 * distinct watchdogNumber for each platform device at device
168*69472ffaSScott Cheloha 	 * registration time.
169*69472ffaSScott Cheloha 	 */
170*69472ffaSScott Cheloha 	pw->num = 1;
171*69472ffaSScott Cheloha 	if (PSERIES_WDTQ_MAX_NUMBER(cap) < pw->num)
172*69472ffaSScott Cheloha 		return -ENODEV;
173*69472ffaSScott Cheloha 
174*69472ffaSScott Cheloha 	if (action >= ARRAY_SIZE(pseries_wdt_action))
175*69472ffaSScott Cheloha 		return -EINVAL;
176*69472ffaSScott Cheloha 	pw->action = pseries_wdt_action[action];
177*69472ffaSScott Cheloha 
178*69472ffaSScott Cheloha 	pw->wd.parent = &pdev->dev;
179*69472ffaSScott Cheloha 	pw->wd.info = &pseries_wdt_info;
180*69472ffaSScott Cheloha 	pw->wd.ops = &pseries_wdt_ops;
181*69472ffaSScott Cheloha 	msecs = PSERIES_WDTQ_MIN_TIMEOUT(cap);
182*69472ffaSScott Cheloha 	pw->wd.min_timeout = DIV_ROUND_UP(msecs, MSEC_PER_SEC);
183*69472ffaSScott Cheloha 	pw->wd.max_timeout = UINT_MAX / 1000;	/* from linux/watchdog.h */
184*69472ffaSScott Cheloha 	pw->wd.timeout = timeout;
185*69472ffaSScott Cheloha 	if (watchdog_init_timeout(&pw->wd, 0, NULL))
186*69472ffaSScott Cheloha 		return -EINVAL;
187*69472ffaSScott Cheloha 	watchdog_set_nowayout(&pw->wd, nowayout);
188*69472ffaSScott Cheloha 	watchdog_stop_on_reboot(&pw->wd);
189*69472ffaSScott Cheloha 	watchdog_stop_on_unregister(&pw->wd);
190*69472ffaSScott Cheloha 	watchdog_set_drvdata(&pw->wd, pw);
191*69472ffaSScott Cheloha 
192*69472ffaSScott Cheloha 	err = devm_watchdog_register_device(&pdev->dev, &pw->wd);
193*69472ffaSScott Cheloha 	if (err)
194*69472ffaSScott Cheloha 		return err;
195*69472ffaSScott Cheloha 
196*69472ffaSScott Cheloha 	platform_set_drvdata(pdev, &pw->wd);
197*69472ffaSScott Cheloha 
198*69472ffaSScott Cheloha 	return 0;
199*69472ffaSScott Cheloha }
200*69472ffaSScott Cheloha 
pseries_wdt_suspend(struct platform_device * pdev,pm_message_t state)201*69472ffaSScott Cheloha static int pseries_wdt_suspend(struct platform_device *pdev, pm_message_t state)
202*69472ffaSScott Cheloha {
203*69472ffaSScott Cheloha 	struct watchdog_device *wd = platform_get_drvdata(pdev);
204*69472ffaSScott Cheloha 
205*69472ffaSScott Cheloha 	if (watchdog_active(wd))
206*69472ffaSScott Cheloha 		return pseries_wdt_stop(wd);
207*69472ffaSScott Cheloha 	return 0;
208*69472ffaSScott Cheloha }
209*69472ffaSScott Cheloha 
pseries_wdt_resume(struct platform_device * pdev)210*69472ffaSScott Cheloha static int pseries_wdt_resume(struct platform_device *pdev)
211*69472ffaSScott Cheloha {
212*69472ffaSScott Cheloha 	struct watchdog_device *wd = platform_get_drvdata(pdev);
213*69472ffaSScott Cheloha 
214*69472ffaSScott Cheloha 	if (watchdog_active(wd))
215*69472ffaSScott Cheloha 		return pseries_wdt_start(wd);
216*69472ffaSScott Cheloha 	return 0;
217*69472ffaSScott Cheloha }
218*69472ffaSScott Cheloha 
219*69472ffaSScott Cheloha static const struct platform_device_id pseries_wdt_id[] = {
220*69472ffaSScott Cheloha 	{ .name = "pseries-wdt" },
221*69472ffaSScott Cheloha 	{}
222*69472ffaSScott Cheloha };
223*69472ffaSScott Cheloha MODULE_DEVICE_TABLE(platform, pseries_wdt_id);
224*69472ffaSScott Cheloha 
225*69472ffaSScott Cheloha static struct platform_driver pseries_wdt_driver = {
226*69472ffaSScott Cheloha 	.driver = {
227*69472ffaSScott Cheloha 		.name = DRV_NAME,
228*69472ffaSScott Cheloha 	},
229*69472ffaSScott Cheloha 	.id_table = pseries_wdt_id,
230*69472ffaSScott Cheloha 	.probe = pseries_wdt_probe,
231*69472ffaSScott Cheloha 	.resume = pseries_wdt_resume,
232*69472ffaSScott Cheloha 	.suspend = pseries_wdt_suspend,
233*69472ffaSScott Cheloha };
234*69472ffaSScott Cheloha module_platform_driver(pseries_wdt_driver);
235*69472ffaSScott Cheloha 
236*69472ffaSScott Cheloha MODULE_AUTHOR("Alexey Kardashevskiy");
237*69472ffaSScott Cheloha MODULE_AUTHOR("Scott Cheloha");
238*69472ffaSScott Cheloha MODULE_DESCRIPTION("POWER Architecture Platform Watchdog Driver");
239*69472ffaSScott Cheloha MODULE_LICENSE("GPL");
240