blob: 88a34efac4005bbd7d149c8d52ccaa525e85b446 [file] [log] [blame]
Wim Van Sebroeck43316042011-07-22 18:55:18 +00001/*
2 * watchdog_core.c
3 *
4 * (c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>,
5 * All Rights Reserved.
6 *
7 * (c) Copyright 2008-2011 Wim Van Sebroeck <wim@iguana.be>.
8 *
9 * This source code is part of the generic code that can be used
10 * by all the watchdog timer drivers.
11 *
12 * Based on source code of the following authors:
13 * Matt Domsch <Matt_Domsch@dell.com>,
14 * Rob Radez <rob@osinvestor.com>,
15 * Rusty Lynch <rusty@linux.co.intel.com>
16 * Satyam Sharma <satyam@infradead.org>
17 * Randy Dunlap <randy.dunlap@oracle.com>
18 *
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version
22 * 2 of the License, or (at your option) any later version.
23 *
24 * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw.
25 * admit liability nor provide warranty for any of this software.
26 * This material is provided "AS-IS" and at no charge.
27 */
28
29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
31#include <linux/module.h> /* For EXPORT_SYMBOL/module stuff/... */
32#include <linux/types.h> /* For standard types */
33#include <linux/errno.h> /* For the -ENODEV/... values */
34#include <linux/kernel.h> /* For printk/panic/... */
Damien Riegel2165bf52015-11-16 12:27:59 -050035#include <linux/reboot.h> /* For restart handler */
Wim Van Sebroeck43316042011-07-22 18:55:18 +000036#include <linux/watchdog.h> /* For watchdog specific items */
37#include <linux/init.h> /* For __init/__exit/... */
Alan Cox45f5fed2012-05-10 21:48:59 +020038#include <linux/idr.h> /* For ida_* macros */
Alan Coxd6b469d2012-05-11 12:00:20 +020039#include <linux/err.h> /* For IS_ERR macros */
Fabio Porcedda30482532013-01-08 11:04:10 +010040#include <linux/of.h> /* For of_get_timeout_sec */
Wim Van Sebroeck43316042011-07-22 18:55:18 +000041
Wim Van Sebroeck6cfb5aa2012-05-21 15:31:06 +020042#include "watchdog_core.h" /* For watchdog_dev_register/... */
Wim Van Sebroeck43316042011-07-22 18:55:18 +000043
Alan Cox45f5fed2012-05-10 21:48:59 +020044static DEFINE_IDA(watchdog_ida);
Alan Coxd6b469d2012-05-11 12:00:20 +020045static struct class *watchdog_class;
Alan Cox45f5fed2012-05-10 21:48:59 +020046
Jean-Baptiste Theouef901742015-06-09 09:55:02 -070047/*
48 * Deferred Registration infrastructure.
49 *
50 * Sometimes watchdog drivers needs to be loaded as soon as possible,
51 * for example when it's impossible to disable it. To do so,
52 * raising the initcall level of the watchdog driver is a solution.
53 * But in such case, the miscdev is maybe not ready (subsys_initcall), and
54 * watchdog_core need miscdev to register the watchdog as a char device.
55 *
56 * The deferred registration infrastructure offer a way for the watchdog
57 * subsystem to register a watchdog properly, even before miscdev is ready.
58 */
59
60static DEFINE_MUTEX(wtd_deferred_reg_mutex);
61static LIST_HEAD(wtd_deferred_reg_list);
62static bool wtd_deferred_reg_done;
63
64static int watchdog_deferred_registration_add(struct watchdog_device *wdd)
65{
66 list_add_tail(&wdd->deferred,
67 &wtd_deferred_reg_list);
68 return 0;
69}
70
71static void watchdog_deferred_registration_del(struct watchdog_device *wdd)
72{
73 struct list_head *p, *n;
74 struct watchdog_device *wdd_tmp;
75
76 list_for_each_safe(p, n, &wtd_deferred_reg_list) {
77 wdd_tmp = list_entry(p, struct watchdog_device,
78 deferred);
79 if (wdd_tmp == wdd) {
80 list_del(&wdd_tmp->deferred);
81 break;
82 }
83 }
84}
85
Fabio Porcedda30482532013-01-08 11:04:10 +010086static void watchdog_check_min_max_timeout(struct watchdog_device *wdd)
87{
88 /*
89 * Check that we have valid min and max timeout values, if
90 * not reset them both to 0 (=not used or unknown)
91 */
92 if (wdd->min_timeout > wdd->max_timeout) {
93 pr_info("Invalid min and max timeout values, resetting to 0!\n");
94 wdd->min_timeout = 0;
95 wdd->max_timeout = 0;
96 }
97}
98
99/**
100 * watchdog_init_timeout() - initialize the timeout field
101 * @timeout_parm: timeout module parameter
102 * @dev: Device that stores the timeout-sec property
103 *
104 * Initialize the timeout field of the watchdog_device struct with either the
105 * timeout module parameter (if it is valid value) or the timeout-sec property
106 * (only if it is a valid value and the timeout_parm is out of bounds).
107 * If none of them are valid then we keep the old value (which should normally
108 * be the default timeout value.
109 *
110 * A zero is returned on success and -EINVAL for failure.
111 */
112int watchdog_init_timeout(struct watchdog_device *wdd,
113 unsigned int timeout_parm, struct device *dev)
114{
115 unsigned int t = 0;
116 int ret = 0;
117
118 watchdog_check_min_max_timeout(wdd);
119
Sachin Kamat6ffcff92013-10-28 14:17:17 +0530120 /* try to get the timeout module parameter first */
Doug Anderson2c34d592013-11-26 10:22:52 -0800121 if (!watchdog_timeout_invalid(wdd, timeout_parm) && timeout_parm) {
Fabio Porcedda30482532013-01-08 11:04:10 +0100122 wdd->timeout = timeout_parm;
123 return ret;
124 }
125 if (timeout_parm)
126 ret = -EINVAL;
127
128 /* try to get the timeout_sec property */
129 if (dev == NULL || dev->of_node == NULL)
130 return ret;
131 of_property_read_u32(dev->of_node, "timeout-sec", &t);
Doug Anderson2c34d592013-11-26 10:22:52 -0800132 if (!watchdog_timeout_invalid(wdd, t) && t)
Fabio Porcedda30482532013-01-08 11:04:10 +0100133 wdd->timeout = t;
134 else
135 ret = -EINVAL;
136
137 return ret;
138}
139EXPORT_SYMBOL_GPL(watchdog_init_timeout);
140
Damien Riegel2165bf52015-11-16 12:27:59 -0500141static int watchdog_restart_notifier(struct notifier_block *nb,
142 unsigned long action, void *data)
143{
144 struct watchdog_device *wdd = container_of(nb, struct watchdog_device,
145 restart_nb);
146
147 int ret;
148
149 ret = wdd->ops->restart(wdd);
150 if (ret)
151 return NOTIFY_BAD;
152
153 return NOTIFY_DONE;
154}
155
156/**
157 * watchdog_set_restart_priority - Change priority of restart handler
158 * @wdd: watchdog device
159 * @priority: priority of the restart handler, should follow these guidelines:
160 * 0: use watchdog's restart function as last resort, has limited restart
161 * capabilies
162 * 128: default restart handler, use if no other handler is expected to be
163 * available and/or if restart is sufficient to restart the entire system
164 * 255: preempt all other handlers
165 *
166 * If a wdd->ops->restart function is provided when watchdog_register_device is
167 * called, it will be registered as a restart handler with the priority given
168 * here.
169 */
170void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority)
171{
172 wdd->restart_nb.priority = priority;
173}
174EXPORT_SYMBOL_GPL(watchdog_set_restart_priority);
175
Jean-Baptiste Theouef901742015-06-09 09:55:02 -0700176static int __watchdog_register_device(struct watchdog_device *wdd)
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000177{
Justin Chen9dd4e172015-09-02 11:00:17 -0700178 int ret, id = -1, devno;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000179
180 if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
181 return -EINVAL;
182
183 /* Mandatory operations need to be supported */
184 if (wdd->ops->start == NULL || wdd->ops->stop == NULL)
185 return -EINVAL;
186
Fabio Porcedda30482532013-01-08 11:04:10 +0100187 watchdog_check_min_max_timeout(wdd);
Wim Van Sebroeck3f43f682011-07-22 19:00:16 +0000188
189 /*
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000190 * Note: now that all watchdog_device data has been verified, we
191 * will not check this anymore in other functions. If data gets
192 * corrupted in a later stage then we expect a kernel panic!
193 */
194
Hans de Goedef4e9c822012-05-22 11:40:26 +0200195 mutex_init(&wdd->lock);
Justin Chen9dd4e172015-09-02 11:00:17 -0700196
197 /* Use alias for watchdog id if possible */
198 if (wdd->parent) {
199 ret = of_alias_get_id(wdd->parent->of_node, "watchdog");
200 if (ret >= 0)
201 id = ida_simple_get(&watchdog_ida, ret,
202 ret + 1, GFP_KERNEL);
203 }
204
205 if (id < 0)
206 id = ida_simple_get(&watchdog_ida, 0, MAX_DOGS, GFP_KERNEL);
207
Alan Cox45f5fed2012-05-10 21:48:59 +0200208 if (id < 0)
209 return id;
210 wdd->id = id;
211
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000212 ret = watchdog_dev_register(wdd);
213 if (ret) {
Alan Cox45f5fed2012-05-10 21:48:59 +0200214 ida_simple_remove(&watchdog_ida, id);
215 if (!(id == 0 && ret == -EBUSY))
216 return ret;
217
218 /* Retry in case a legacy watchdog module exists */
219 id = ida_simple_get(&watchdog_ida, 1, MAX_DOGS, GFP_KERNEL);
220 if (id < 0)
221 return id;
222 wdd->id = id;
223
224 ret = watchdog_dev_register(wdd);
225 if (ret) {
226 ida_simple_remove(&watchdog_ida, id);
227 return ret;
228 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000229 }
230
Alan Coxd6b469d2012-05-11 12:00:20 +0200231 devno = wdd->cdev.dev;
232 wdd->dev = device_create(watchdog_class, wdd->parent, devno,
233 NULL, "watchdog%d", wdd->id);
234 if (IS_ERR(wdd->dev)) {
235 watchdog_dev_unregister(wdd);
236 ida_simple_remove(&watchdog_ida, id);
237 ret = PTR_ERR(wdd->dev);
238 return ret;
239 }
240
Damien Riegel2165bf52015-11-16 12:27:59 -0500241 if (wdd->ops->restart) {
242 wdd->restart_nb.notifier_call = watchdog_restart_notifier;
243
244 ret = register_restart_handler(&wdd->restart_nb);
245 if (ret)
246 dev_warn(wdd->dev, "Cannot register restart handler (%d)\n",
247 ret);
248 }
249
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000250 return 0;
251}
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000252
253/**
Jean-Baptiste Theouef901742015-06-09 09:55:02 -0700254 * watchdog_register_device() - register a watchdog device
255 * @wdd: watchdog device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000256 *
Jean-Baptiste Theouef901742015-06-09 09:55:02 -0700257 * Register a watchdog device with the kernel so that the
258 * watchdog timer can be accessed from userspace.
259 *
260 * A zero is returned on success and a negative errno code for
261 * failure.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000262 */
Jean-Baptiste Theouef901742015-06-09 09:55:02 -0700263
264int watchdog_register_device(struct watchdog_device *wdd)
265{
266 int ret;
267
268 mutex_lock(&wtd_deferred_reg_mutex);
269 if (wtd_deferred_reg_done)
270 ret = __watchdog_register_device(wdd);
271 else
272 ret = watchdog_deferred_registration_add(wdd);
273 mutex_unlock(&wtd_deferred_reg_mutex);
274 return ret;
275}
276EXPORT_SYMBOL_GPL(watchdog_register_device);
277
278static void __watchdog_unregister_device(struct watchdog_device *wdd)
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000279{
280 int ret;
Wei Yongjunb232a702012-09-10 12:41:15 +0800281 int devno;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000282
283 if (wdd == NULL)
284 return;
285
Damien Riegel2165bf52015-11-16 12:27:59 -0500286 if (wdd->ops->restart)
287 unregister_restart_handler(&wdd->restart_nb);
288
Wei Yongjunb232a702012-09-10 12:41:15 +0800289 devno = wdd->cdev.dev;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000290 ret = watchdog_dev_unregister(wdd);
291 if (ret)
Joe Perches27c766a2012-02-15 15:06:19 -0800292 pr_err("error unregistering /dev/watchdog (err=%d)\n", ret);
Alan Coxd6b469d2012-05-11 12:00:20 +0200293 device_destroy(watchdog_class, devno);
Alan Cox45f5fed2012-05-10 21:48:59 +0200294 ida_simple_remove(&watchdog_ida, wdd->id);
Alan Coxd6b469d2012-05-11 12:00:20 +0200295 wdd->dev = NULL;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000296}
Jean-Baptiste Theouef901742015-06-09 09:55:02 -0700297
298/**
299 * watchdog_unregister_device() - unregister a watchdog device
300 * @wdd: watchdog device to unregister
301 *
302 * Unregister a watchdog device that was previously successfully
303 * registered with watchdog_register_device().
304 */
305
306void watchdog_unregister_device(struct watchdog_device *wdd)
307{
308 mutex_lock(&wtd_deferred_reg_mutex);
309 if (wtd_deferred_reg_done)
310 __watchdog_unregister_device(wdd);
311 else
312 watchdog_deferred_registration_del(wdd);
313 mutex_unlock(&wtd_deferred_reg_mutex);
314}
315
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000316EXPORT_SYMBOL_GPL(watchdog_unregister_device);
317
Jean-Baptiste Theouef901742015-06-09 09:55:02 -0700318static int __init watchdog_deferred_registration(void)
319{
320 mutex_lock(&wtd_deferred_reg_mutex);
321 wtd_deferred_reg_done = true;
322 while (!list_empty(&wtd_deferred_reg_list)) {
323 struct watchdog_device *wdd;
324
325 wdd = list_first_entry(&wtd_deferred_reg_list,
326 struct watchdog_device, deferred);
327 list_del(&wdd->deferred);
328 __watchdog_register_device(wdd);
329 }
330 mutex_unlock(&wtd_deferred_reg_mutex);
331 return 0;
332}
333
Alan Cox45f5fed2012-05-10 21:48:59 +0200334static int __init watchdog_init(void)
335{
Alan Coxd6b469d2012-05-11 12:00:20 +0200336 int err;
337
338 watchdog_class = class_create(THIS_MODULE, "watchdog");
339 if (IS_ERR(watchdog_class)) {
340 pr_err("couldn't create class\n");
341 return PTR_ERR(watchdog_class);
342 }
343
344 err = watchdog_dev_init();
345 if (err < 0) {
346 class_destroy(watchdog_class);
347 return err;
348 }
349
Jean-Baptiste Theouef901742015-06-09 09:55:02 -0700350 watchdog_deferred_registration();
Alan Coxd6b469d2012-05-11 12:00:20 +0200351 return 0;
Alan Cox45f5fed2012-05-10 21:48:59 +0200352}
353
354static void __exit watchdog_exit(void)
355{
356 watchdog_dev_exit();
Alan Coxd6b469d2012-05-11 12:00:20 +0200357 class_destroy(watchdog_class);
Alan Cox45f5fed2012-05-10 21:48:59 +0200358 ida_destroy(&watchdog_ida);
359}
360
Jean-Baptiste Theouef901742015-06-09 09:55:02 -0700361subsys_initcall_sync(watchdog_init);
Alan Cox45f5fed2012-05-10 21:48:59 +0200362module_exit(watchdog_exit);
363
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000364MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
365MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
366MODULE_DESCRIPTION("WatchDog Timer Driver Core");
367MODULE_LICENSE("GPL");