blob: 9857bb74a723fc648ee07b895baf126eda966880 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * IndyDog 0.3 A Hardware Watchdog Device for SGI IP22
4 *
Alan Cox9b9dbcc2008-05-19 14:06:08 +01005 * (c) Copyright 2002 Guido Guenther <agx@sigxcpu.org>,
6 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Alan Cox29fa0582008-10-27 15:17:56 +00008 * based on softdog.c by Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Joe Perches27c766a2012-02-15 15:06:19 -080011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/types.h>
16#include <linux/kernel.h>
17#include <linux/fs.h>
18#include <linux/mm.h>
19#include <linux/miscdevice.h>
20#include <linux/watchdog.h>
21#include <linux/notifier.h>
22#include <linux/reboot.h>
23#include <linux/init.h>
Alan Cox9b9dbcc2008-05-19 14:06:08 +010024#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/sgi/mc.h>
26
Alan Cox9b9dbcc2008-05-19 14:06:08 +010027static unsigned long indydog_alive;
Axel Lin1334f322011-11-29 13:54:01 +080028static DEFINE_SPINLOCK(indydog_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
31
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010032static bool nowayout = WATCHDOG_NOWAYOUT;
33module_param(nowayout, bool, 0);
Alan Cox9b9dbcc2008-05-19 14:06:08 +010034MODULE_PARM_DESC(nowayout,
35 "Watchdog cannot be stopped once started (default="
36 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38static void indydog_start(void)
39{
Alan Cox9b9dbcc2008-05-19 14:06:08 +010040 spin_lock(&indydog_lock);
Alexander Shiyan0c29c2e2014-02-15 13:21:51 +040041 sgimc->cpuctrl0 |= SGIMC_CCTRL0_WDOG;
Alan Cox9b9dbcc2008-05-19 14:06:08 +010042 spin_unlock(&indydog_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
45static void indydog_stop(void)
46{
Alan Cox9b9dbcc2008-05-19 14:06:08 +010047 spin_lock(&indydog_lock);
Alexander Shiyan0c29c2e2014-02-15 13:21:51 +040048 sgimc->cpuctrl0 &= ~SGIMC_CCTRL0_WDOG;
Alan Cox9b9dbcc2008-05-19 14:06:08 +010049 spin_unlock(&indydog_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Joe Perches27c766a2012-02-15 15:06:19 -080051 pr_info("Stopped watchdog timer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
54static void indydog_ping(void)
55{
56 sgimc->watchdogt = 0;
57}
58
59/*
60 * Allow only one person to hold it open
61 */
62static int indydog_open(struct inode *inode, struct file *file)
63{
Alan Cox9b9dbcc2008-05-19 14:06:08 +010064 if (test_and_set_bit(0, &indydog_alive))
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return -EBUSY;
66
67 if (nowayout)
68 __module_get(THIS_MODULE);
69
70 /* Activate timer */
71 indydog_start();
72 indydog_ping();
73
Joe Perches27c766a2012-02-15 15:06:19 -080074 pr_info("Started watchdog timer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +030076 return stream_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79static int indydog_release(struct inode *inode, struct file *file)
80{
81 /* Shut off the timer.
82 * Lock it in if it's a module and we defined ...NOWAYOUT */
83 if (!nowayout)
84 indydog_stop(); /* Turn the WDT off */
Alan Cox9b9dbcc2008-05-19 14:06:08 +010085 clear_bit(0, &indydog_alive);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return 0;
87}
88
Alan Cox9b9dbcc2008-05-19 14:06:08 +010089static ssize_t indydog_write(struct file *file, const char *data,
90 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 /* Refresh the timer. */
Alan Cox9b9dbcc2008-05-19 14:06:08 +010093 if (len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 indydog_ping();
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return len;
96}
97
Alan Cox9b9dbcc2008-05-19 14:06:08 +010098static long indydog_ioctl(struct file *file, unsigned int cmd,
99 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 int options, retval = -EINVAL;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000102 static const struct watchdog_info ident = {
Wim Van Sebroecke73a7802009-05-11 18:33:00 +0000103 .options = WDIOF_KEEPALIVEPING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .firmware_version = 0,
105 .identity = "Hardware Watchdog for SGI IP22",
106 };
107
108 switch (cmd) {
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100109 case WDIOC_GETSUPPORT:
110 if (copy_to_user((struct watchdog_info *)arg,
111 &ident, sizeof(ident)))
112 return -EFAULT;
113 return 0;
114 case WDIOC_GETSTATUS:
115 case WDIOC_GETBOOTSTATUS:
116 return put_user(0, (int *)arg);
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100117 case WDIOC_SETOPTIONS:
118 {
119 if (get_user(options, (int *)arg))
120 return -EFAULT;
121 if (options & WDIOS_DISABLECARD) {
122 indydog_stop();
123 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100125 if (options & WDIOS_ENABLECARD) {
126 indydog_start();
127 retval = 0;
128 }
129 return retval;
130 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000131 case WDIOC_KEEPALIVE:
132 indydog_ping();
133 return 0;
134 case WDIOC_GETTIMEOUT:
135 return put_user(WATCHDOG_TIMEOUT, (int *)arg);
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100136 default:
137 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139}
140
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100141static int indydog_notify_sys(struct notifier_block *this,
142 unsigned long code, void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 if (code == SYS_DOWN || code == SYS_HALT)
145 indydog_stop(); /* Turn the WDT off */
146
147 return NOTIFY_DONE;
148}
149
Arjan van de Ven62322d22006-07-03 00:24:21 -0700150static const struct file_operations indydog_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 .owner = THIS_MODULE,
152 .llseek = no_llseek,
153 .write = indydog_write,
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100154 .unlocked_ioctl = indydog_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200155 .compat_ioctl = compat_ptr_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 .open = indydog_open,
157 .release = indydog_release,
158};
159
160static struct miscdevice indydog_miscdev = {
161 .minor = WATCHDOG_MINOR,
162 .name = "watchdog",
163 .fops = &indydog_fops,
164};
165
166static struct notifier_block indydog_notifier = {
167 .notifier_call = indydog_notify_sys,
168};
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static int __init watchdog_init(void)
171{
172 int ret;
173
174 ret = register_reboot_notifier(&indydog_notifier);
175 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800176 pr_err("cannot register reboot notifier (err=%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return ret;
178 }
179
180 ret = misc_register(&indydog_miscdev);
181 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800182 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
183 WATCHDOG_MINOR, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 unregister_reboot_notifier(&indydog_notifier);
185 return ret;
186 }
187
Joe Perches27c766a2012-02-15 15:06:19 -0800188 pr_info("Hardware Watchdog Timer for SGI IP22: 0.3\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 return 0;
191}
192
193static void __exit watchdog_exit(void)
194{
195 misc_deregister(&indydog_miscdev);
196 unregister_reboot_notifier(&indydog_notifier);
197}
198
199module_init(watchdog_init);
200module_exit(watchdog_exit);
201
202MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
203MODULE_DESCRIPTION("Hardware Watchdog Device for SGI IP22");
204MODULE_LICENSE("GPL");