Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  *  watchdog_core.c
0004  *
0005  *  (c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>,
0006  *                      All Rights Reserved.
0007  *
0008  *  (c) Copyright 2008-2011 Wim Van Sebroeck <wim@iguana.be>.
0009  *
0010  *  This source code is part of the generic code that can be used
0011  *  by all the watchdog timer drivers.
0012  *
0013  *  Based on source code of the following authors:
0014  *    Matt Domsch <Matt_Domsch@dell.com>,
0015  *    Rob Radez <rob@osinvestor.com>,
0016  *    Rusty Lynch <rusty@linux.co.intel.com>
0017  *    Satyam Sharma <satyam@infradead.org>
0018  *    Randy Dunlap <randy.dunlap@oracle.com>
0019  *
0020  *  Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw.
0021  *  admit liability nor provide warranty for any of this software.
0022  *  This material is provided "AS-IS" and at no charge.
0023  */
0024 
0025 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0026 
0027 #include <linux/module.h>   /* For EXPORT_SYMBOL/module stuff/... */
0028 #include <linux/types.h>    /* For standard types */
0029 #include <linux/errno.h>    /* For the -ENODEV/... values */
0030 #include <linux/kernel.h>   /* For printk/panic/... */
0031 #include <linux/reboot.h>   /* For restart handler */
0032 #include <linux/watchdog.h> /* For watchdog specific items */
0033 #include <linux/init.h>     /* For __init/__exit/... */
0034 #include <linux/idr.h>      /* For ida_* macros */
0035 #include <linux/err.h>      /* For IS_ERR macros */
0036 #include <linux/of.h>       /* For of_get_timeout_sec */
0037 #include <linux/suspend.h>
0038 
0039 #include "watchdog_core.h"  /* For watchdog_dev_register/... */
0040 
0041 static DEFINE_IDA(watchdog_ida);
0042 
0043 static int stop_on_reboot = -1;
0044 module_param(stop_on_reboot, int, 0444);
0045 MODULE_PARM_DESC(stop_on_reboot, "Stop watchdogs on reboot (0=keep watching, 1=stop)");
0046 
0047 /*
0048  * Deferred Registration infrastructure.
0049  *
0050  * Sometimes watchdog drivers needs to be loaded as soon as possible,
0051  * for example when it's impossible to disable it. To do so,
0052  * raising the initcall level of the watchdog driver is a solution.
0053  * But in such case, the miscdev is maybe not ready (subsys_initcall), and
0054  * watchdog_core need miscdev to register the watchdog as a char device.
0055  *
0056  * The deferred registration infrastructure offer a way for the watchdog
0057  * subsystem to register a watchdog properly, even before miscdev is ready.
0058  */
0059 
0060 static DEFINE_MUTEX(wtd_deferred_reg_mutex);
0061 static LIST_HEAD(wtd_deferred_reg_list);
0062 static bool wtd_deferred_reg_done;
0063 
0064 static void watchdog_deferred_registration_add(struct watchdog_device *wdd)
0065 {
0066     list_add_tail(&wdd->deferred,
0067               &wtd_deferred_reg_list);
0068 }
0069 
0070 static void watchdog_deferred_registration_del(struct watchdog_device *wdd)
0071 {
0072     struct list_head *p, *n;
0073     struct watchdog_device *wdd_tmp;
0074 
0075     list_for_each_safe(p, n, &wtd_deferred_reg_list) {
0076         wdd_tmp = list_entry(p, struct watchdog_device,
0077                      deferred);
0078         if (wdd_tmp == wdd) {
0079             list_del(&wdd_tmp->deferred);
0080             break;
0081         }
0082     }
0083 }
0084 
0085 static void watchdog_check_min_max_timeout(struct watchdog_device *wdd)
0086 {
0087     /*
0088      * Check that we have valid min and max timeout values, if
0089      * not reset them both to 0 (=not used or unknown)
0090      */
0091     if (!wdd->max_hw_heartbeat_ms && wdd->min_timeout > wdd->max_timeout) {
0092         pr_info("Invalid min and max timeout values, resetting to 0!\n");
0093         wdd->min_timeout = 0;
0094         wdd->max_timeout = 0;
0095     }
0096 }
0097 
0098 /**
0099  * watchdog_init_timeout() - initialize the timeout field
0100  * @wdd: watchdog device
0101  * @timeout_parm: timeout module parameter
0102  * @dev: Device that stores the timeout-sec property
0103  *
0104  * Initialize the timeout field of the watchdog_device struct with either the
0105  * timeout module parameter (if it is valid value) or the timeout-sec property
0106  * (only if it is a valid value and the timeout_parm is out of bounds).
0107  * If none of them are valid then we keep the old value (which should normally
0108  * be the default timeout value). Note that for the module parameter, '0' means
0109  * 'use default' while it is an invalid value for the timeout-sec property.
0110  * It should simply be dropped if you want to use the default value then.
0111  *
0112  * A zero is returned on success or -EINVAL if all provided values are out of
0113  * bounds.
0114  */
0115 int watchdog_init_timeout(struct watchdog_device *wdd,
0116                 unsigned int timeout_parm, struct device *dev)
0117 {
0118     const char *dev_str = wdd->parent ? dev_name(wdd->parent) :
0119                   (const char *)wdd->info->identity;
0120     unsigned int t = 0;
0121     int ret = 0;
0122 
0123     watchdog_check_min_max_timeout(wdd);
0124 
0125     /* check the driver supplied value (likely a module parameter) first */
0126     if (timeout_parm) {
0127         if (!watchdog_timeout_invalid(wdd, timeout_parm)) {
0128             wdd->timeout = timeout_parm;
0129             return 0;
0130         }
0131         pr_err("%s: driver supplied timeout (%u) out of range\n",
0132             dev_str, timeout_parm);
0133         ret = -EINVAL;
0134     }
0135 
0136     /* try to get the timeout_sec property */
0137     if (dev && dev->of_node &&
0138         of_property_read_u32(dev->of_node, "timeout-sec", &t) == 0) {
0139         if (t && !watchdog_timeout_invalid(wdd, t)) {
0140             wdd->timeout = t;
0141             return 0;
0142         }
0143         pr_err("%s: DT supplied timeout (%u) out of range\n", dev_str, t);
0144         ret = -EINVAL;
0145     }
0146 
0147     if (ret < 0 && wdd->timeout)
0148         pr_warn("%s: falling back to default timeout (%u)\n", dev_str,
0149             wdd->timeout);
0150 
0151     return ret;
0152 }
0153 EXPORT_SYMBOL_GPL(watchdog_init_timeout);
0154 
0155 static int watchdog_reboot_notifier(struct notifier_block *nb,
0156                     unsigned long code, void *data)
0157 {
0158     struct watchdog_device *wdd;
0159 
0160     wdd = container_of(nb, struct watchdog_device, reboot_nb);
0161     if (code == SYS_DOWN || code == SYS_HALT) {
0162         if (watchdog_active(wdd) || watchdog_hw_running(wdd)) {
0163             int ret;
0164 
0165             ret = wdd->ops->stop(wdd);
0166             if (ret)
0167                 return NOTIFY_BAD;
0168         }
0169     }
0170 
0171     return NOTIFY_DONE;
0172 }
0173 
0174 static int watchdog_restart_notifier(struct notifier_block *nb,
0175                      unsigned long action, void *data)
0176 {
0177     struct watchdog_device *wdd = container_of(nb, struct watchdog_device,
0178                            restart_nb);
0179 
0180     int ret;
0181 
0182     ret = wdd->ops->restart(wdd, action, data);
0183     if (ret)
0184         return NOTIFY_BAD;
0185 
0186     return NOTIFY_DONE;
0187 }
0188 
0189 static int watchdog_pm_notifier(struct notifier_block *nb, unsigned long mode,
0190                 void *data)
0191 {
0192     struct watchdog_device *wdd;
0193     int ret = 0;
0194 
0195     wdd = container_of(nb, struct watchdog_device, pm_nb);
0196 
0197     switch (mode) {
0198     case PM_HIBERNATION_PREPARE:
0199     case PM_RESTORE_PREPARE:
0200     case PM_SUSPEND_PREPARE:
0201         ret = watchdog_dev_suspend(wdd);
0202         break;
0203     case PM_POST_HIBERNATION:
0204     case PM_POST_RESTORE:
0205     case PM_POST_SUSPEND:
0206         ret = watchdog_dev_resume(wdd);
0207         break;
0208     }
0209 
0210     if (ret)
0211         return NOTIFY_BAD;
0212 
0213     return NOTIFY_DONE;
0214 }
0215 
0216 /**
0217  * watchdog_set_restart_priority - Change priority of restart handler
0218  * @wdd: watchdog device
0219  * @priority: priority of the restart handler, should follow these guidelines:
0220  *   0:   use watchdog's restart function as last resort, has limited restart
0221  *        capabilies
0222  *   128: default restart handler, use if no other handler is expected to be
0223  *        available and/or if restart is sufficient to restart the entire system
0224  *   255: preempt all other handlers
0225  *
0226  * If a wdd->ops->restart function is provided when watchdog_register_device is
0227  * called, it will be registered as a restart handler with the priority given
0228  * here.
0229  */
0230 void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority)
0231 {
0232     wdd->restart_nb.priority = priority;
0233 }
0234 EXPORT_SYMBOL_GPL(watchdog_set_restart_priority);
0235 
0236 static int __watchdog_register_device(struct watchdog_device *wdd)
0237 {
0238     int ret, id = -1;
0239 
0240     if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
0241         return -EINVAL;
0242 
0243     /* Mandatory operations need to be supported */
0244     if (!wdd->ops->start || (!wdd->ops->stop && !wdd->max_hw_heartbeat_ms))
0245         return -EINVAL;
0246 
0247     watchdog_check_min_max_timeout(wdd);
0248 
0249     /*
0250      * Note: now that all watchdog_device data has been verified, we
0251      * will not check this anymore in other functions. If data gets
0252      * corrupted in a later stage then we expect a kernel panic!
0253      */
0254 
0255     /* Use alias for watchdog id if possible */
0256     if (wdd->parent) {
0257         ret = of_alias_get_id(wdd->parent->of_node, "watchdog");
0258         if (ret >= 0)
0259             id = ida_simple_get(&watchdog_ida, ret,
0260                         ret + 1, GFP_KERNEL);
0261     }
0262 
0263     if (id < 0)
0264         id = ida_simple_get(&watchdog_ida, 0, MAX_DOGS, GFP_KERNEL);
0265 
0266     if (id < 0)
0267         return id;
0268     wdd->id = id;
0269 
0270     ret = watchdog_dev_register(wdd);
0271     if (ret) {
0272         ida_simple_remove(&watchdog_ida, id);
0273         if (!(id == 0 && ret == -EBUSY))
0274             return ret;
0275 
0276         /* Retry in case a legacy watchdog module exists */
0277         id = ida_simple_get(&watchdog_ida, 1, MAX_DOGS, GFP_KERNEL);
0278         if (id < 0)
0279             return id;
0280         wdd->id = id;
0281 
0282         ret = watchdog_dev_register(wdd);
0283         if (ret) {
0284             ida_simple_remove(&watchdog_ida, id);
0285             return ret;
0286         }
0287     }
0288 
0289     /* Module parameter to force watchdog policy on reboot. */
0290     if (stop_on_reboot != -1) {
0291         if (stop_on_reboot)
0292             set_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
0293         else
0294             clear_bit(WDOG_STOP_ON_REBOOT, &wdd->status);
0295     }
0296 
0297     if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status)) {
0298         if (!wdd->ops->stop)
0299             pr_warn("watchdog%d: stop_on_reboot not supported\n", wdd->id);
0300         else {
0301             wdd->reboot_nb.notifier_call = watchdog_reboot_notifier;
0302 
0303             ret = register_reboot_notifier(&wdd->reboot_nb);
0304             if (ret) {
0305                 pr_err("watchdog%d: Cannot register reboot notifier (%d)\n",
0306                     wdd->id, ret);
0307                 watchdog_dev_unregister(wdd);
0308                 ida_simple_remove(&watchdog_ida, id);
0309                 return ret;
0310             }
0311         }
0312     }
0313 
0314     if (wdd->ops->restart) {
0315         wdd->restart_nb.notifier_call = watchdog_restart_notifier;
0316 
0317         ret = register_restart_handler(&wdd->restart_nb);
0318         if (ret)
0319             pr_warn("watchdog%d: Cannot register restart handler (%d)\n",
0320                 wdd->id, ret);
0321     }
0322 
0323     if (test_bit(WDOG_NO_PING_ON_SUSPEND, &wdd->status)) {
0324         wdd->pm_nb.notifier_call = watchdog_pm_notifier;
0325 
0326         ret = register_pm_notifier(&wdd->pm_nb);
0327         if (ret)
0328             pr_warn("watchdog%d: Cannot register pm handler (%d)\n",
0329                 wdd->id, ret);
0330     }
0331 
0332     return 0;
0333 }
0334 
0335 /**
0336  * watchdog_register_device() - register a watchdog device
0337  * @wdd: watchdog device
0338  *
0339  * Register a watchdog device with the kernel so that the
0340  * watchdog timer can be accessed from userspace.
0341  *
0342  * A zero is returned on success and a negative errno code for
0343  * failure.
0344  */
0345 
0346 int watchdog_register_device(struct watchdog_device *wdd)
0347 {
0348     const char *dev_str;
0349     int ret = 0;
0350 
0351     mutex_lock(&wtd_deferred_reg_mutex);
0352     if (wtd_deferred_reg_done)
0353         ret = __watchdog_register_device(wdd);
0354     else
0355         watchdog_deferred_registration_add(wdd);
0356     mutex_unlock(&wtd_deferred_reg_mutex);
0357 
0358     if (ret) {
0359         dev_str = wdd->parent ? dev_name(wdd->parent) :
0360               (const char *)wdd->info->identity;
0361         pr_err("%s: failed to register watchdog device (err = %d)\n",
0362             dev_str, ret);
0363     }
0364 
0365     return ret;
0366 }
0367 EXPORT_SYMBOL_GPL(watchdog_register_device);
0368 
0369 static void __watchdog_unregister_device(struct watchdog_device *wdd)
0370 {
0371     if (wdd == NULL)
0372         return;
0373 
0374     if (wdd->ops->restart)
0375         unregister_restart_handler(&wdd->restart_nb);
0376 
0377     if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status))
0378         unregister_reboot_notifier(&wdd->reboot_nb);
0379 
0380     watchdog_dev_unregister(wdd);
0381     ida_simple_remove(&watchdog_ida, wdd->id);
0382 }
0383 
0384 /**
0385  * watchdog_unregister_device() - unregister a watchdog device
0386  * @wdd: watchdog device to unregister
0387  *
0388  * Unregister a watchdog device that was previously successfully
0389  * registered with watchdog_register_device().
0390  */
0391 
0392 void watchdog_unregister_device(struct watchdog_device *wdd)
0393 {
0394     mutex_lock(&wtd_deferred_reg_mutex);
0395     if (wtd_deferred_reg_done)
0396         __watchdog_unregister_device(wdd);
0397     else
0398         watchdog_deferred_registration_del(wdd);
0399     mutex_unlock(&wtd_deferred_reg_mutex);
0400 }
0401 
0402 EXPORT_SYMBOL_GPL(watchdog_unregister_device);
0403 
0404 static void devm_watchdog_unregister_device(struct device *dev, void *res)
0405 {
0406     watchdog_unregister_device(*(struct watchdog_device **)res);
0407 }
0408 
0409 /**
0410  * devm_watchdog_register_device() - resource managed watchdog_register_device()
0411  * @dev: device that is registering this watchdog device
0412  * @wdd: watchdog device
0413  *
0414  * Managed watchdog_register_device(). For watchdog device registered by this
0415  * function,  watchdog_unregister_device() is automatically called on driver
0416  * detach. See watchdog_register_device() for more information.
0417  */
0418 int devm_watchdog_register_device(struct device *dev,
0419                 struct watchdog_device *wdd)
0420 {
0421     struct watchdog_device **rcwdd;
0422     int ret;
0423 
0424     rcwdd = devres_alloc(devm_watchdog_unregister_device, sizeof(*rcwdd),
0425                  GFP_KERNEL);
0426     if (!rcwdd)
0427         return -ENOMEM;
0428 
0429     ret = watchdog_register_device(wdd);
0430     if (!ret) {
0431         *rcwdd = wdd;
0432         devres_add(dev, rcwdd);
0433     } else {
0434         devres_free(rcwdd);
0435     }
0436 
0437     return ret;
0438 }
0439 EXPORT_SYMBOL_GPL(devm_watchdog_register_device);
0440 
0441 static int __init watchdog_deferred_registration(void)
0442 {
0443     mutex_lock(&wtd_deferred_reg_mutex);
0444     wtd_deferred_reg_done = true;
0445     while (!list_empty(&wtd_deferred_reg_list)) {
0446         struct watchdog_device *wdd;
0447 
0448         wdd = list_first_entry(&wtd_deferred_reg_list,
0449                        struct watchdog_device, deferred);
0450         list_del(&wdd->deferred);
0451         __watchdog_register_device(wdd);
0452     }
0453     mutex_unlock(&wtd_deferred_reg_mutex);
0454     return 0;
0455 }
0456 
0457 static int __init watchdog_init(void)
0458 {
0459     int err;
0460 
0461     err = watchdog_dev_init();
0462     if (err < 0)
0463         return err;
0464 
0465     watchdog_deferred_registration();
0466     return 0;
0467 }
0468 
0469 static void __exit watchdog_exit(void)
0470 {
0471     watchdog_dev_exit();
0472     ida_destroy(&watchdog_ida);
0473 }
0474 
0475 subsys_initcall_sync(watchdog_init);
0476 module_exit(watchdog_exit);
0477 
0478 MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
0479 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
0480 MODULE_DESCRIPTION("WatchDog Timer Driver Core");
0481 MODULE_LICENSE("GPL");