intel_scu_watchdog.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Intel_SCU 0.2: An Intel SCU IOH Based Watchdog Device
  3. * for Intel part #(s):
  4. * - AF82MP20 PCH
  5. *
  6. * Copyright (C) 2009-2010 Intel Corporation. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of version 2 of the GNU General
  10. * Public License as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be
  13. * useful, but WITHOUT ANY WARRANTY; without even the implied
  14. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU General Public License for more details.
  16. * You should have received a copy of the GNU General Public
  17. * License along with this program; if not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. * The full GNU General Public License is included in this
  21. * distribution in the file called COPYING.
  22. *
  23. */
  24. #include <linux/compiler.h>
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/types.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/watchdog.h>
  31. #include <linux/fs.h>
  32. #include <linux/notifier.h>
  33. #include <linux/reboot.h>
  34. #include <linux/init.h>
  35. #include <linux/jiffies.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/slab.h>
  38. #include <linux/io.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/delay.h>
  41. #include <linux/sched.h>
  42. #include <linux/signal.h>
  43. #include <linux/sfi.h>
  44. #include <asm/irq.h>
  45. #include <asm/atomic.h>
  46. #include <asm/intel_scu_ipc.h>
  47. #include <asm/apb_timer.h>
  48. #include <asm/mrst.h>
  49. #include "intel_scu_watchdog.h"
  50. /* Bounds number of times we will retry loading time count */
  51. /* This retry is a work around for a silicon bug. */
  52. #define MAX_RETRY 16
  53. #define IPC_SET_WATCHDOG_TIMER 0xF8
  54. static int timer_margin = DEFAULT_SOFT_TO_HARD_MARGIN;
  55. module_param(timer_margin, int, 0);
  56. MODULE_PARM_DESC(timer_margin,
  57. "Watchdog timer margin"
  58. "Time between interrupt and resetting the system"
  59. "The range is from 1 to 160"
  60. "This is the time for all keep alives to arrive");
  61. static int timer_set = DEFAULT_TIME;
  62. module_param(timer_set, int, 0);
  63. MODULE_PARM_DESC(timer_set,
  64. "Default Watchdog timer setting"
  65. "Complete cycle time"
  66. "The range is from 1 to 170"
  67. "This is the time for all keep alives to arrive");
  68. /* After watchdog device is closed, check force_boot. If:
  69. * force_boot == 0, then force boot on next watchdog interrupt after close,
  70. * force_boot == 1, then force boot immediately when device is closed.
  71. */
  72. static int force_boot;
  73. module_param(force_boot, int, 0);
  74. MODULE_PARM_DESC(force_boot,
  75. "A value of 1 means that the driver will reboot"
  76. "the system immediately if the /dev/watchdog device is closed"
  77. "A value of 0 means that when /dev/watchdog device is closed"
  78. "the watchdog timer will be refreshed for one more interval"
  79. "of length: timer_set. At the end of this interval, the"
  80. "watchdog timer will reset the system."
  81. );
  82. /* there is only one device in the system now; this can be made into
  83. * an array in the future if we have more than one device */
  84. static struct intel_scu_watchdog_dev watchdog_device;
  85. /* Forces restart, if force_reboot is set */
  86. static void watchdog_fire(void)
  87. {
  88. if (force_boot) {
  89. printk(KERN_CRIT PFX "Initiating system reboot.\n");
  90. emergency_restart();
  91. printk(KERN_CRIT PFX "Reboot didn't ?????\n");
  92. }
  93. else {
  94. printk(KERN_CRIT PFX "Immediate Reboot Disabled\n");
  95. printk(KERN_CRIT PFX
  96. "System will reset when watchdog timer times out!\n");
  97. }
  98. }
  99. static int check_timer_margin(int new_margin)
  100. {
  101. if ((new_margin < MIN_TIME_CYCLE) ||
  102. (new_margin > MAX_TIME - timer_set)) {
  103. pr_debug("Watchdog timer: value of new_margin %d is out of the range %d to %d\n",
  104. new_margin, MIN_TIME_CYCLE, MAX_TIME - timer_set);
  105. return -EINVAL;
  106. }
  107. return 0;
  108. }
  109. /*
  110. * IPC operations
  111. */
  112. static int watchdog_set_ipc(int soft_threshold, int threshold)
  113. {
  114. u32 *ipc_wbuf;
  115. u8 cbuf[16] = { '\0' };
  116. int ipc_ret = 0;
  117. ipc_wbuf = (u32 *)&cbuf;
  118. ipc_wbuf[0] = soft_threshold;
  119. ipc_wbuf[1] = threshold;
  120. ipc_ret = intel_scu_ipc_command(
  121. IPC_SET_WATCHDOG_TIMER,
  122. 0,
  123. ipc_wbuf,
  124. 2,
  125. NULL,
  126. 0);
  127. if (ipc_ret != 0)
  128. pr_err("Error setting SCU watchdog timer: %x\n", ipc_ret);
  129. return ipc_ret;
  130. };
  131. /*
  132. * Intel_SCU operations
  133. */
  134. /* timer interrupt handler */
  135. static irqreturn_t watchdog_timer_interrupt(int irq, void *dev_id)
  136. {
  137. int int_status;
  138. int_status = ioread32(watchdog_device.timer_interrupt_status_addr);
  139. pr_debug("Watchdog timer: irq, int_status: %x\n", int_status);
  140. if (int_status != 0)
  141. return IRQ_NONE;
  142. /* has the timer been started? If not, then this is spurious */
  143. if (watchdog_device.timer_started == 0) {
  144. pr_debug("Watchdog timer: spurious interrupt received\n");
  145. return IRQ_HANDLED;
  146. }
  147. /* temporarily disable the timer */
  148. iowrite32(0x00000002, watchdog_device.timer_control_addr);
  149. /* set the timer to the threshold */
  150. iowrite32(watchdog_device.threshold,
  151. watchdog_device.timer_load_count_addr);
  152. /* allow the timer to run */
  153. iowrite32(0x00000003, watchdog_device.timer_control_addr);
  154. return IRQ_HANDLED;
  155. }
  156. static int intel_scu_keepalive(void)
  157. {
  158. /* read eoi register - clears interrupt */
  159. ioread32(watchdog_device.timer_clear_interrupt_addr);
  160. /* temporarily disable the timer */
  161. iowrite32(0x00000002, watchdog_device.timer_control_addr);
  162. /* set the timer to the soft_threshold */
  163. iowrite32(watchdog_device.soft_threshold,
  164. watchdog_device.timer_load_count_addr);
  165. /* allow the timer to run */
  166. iowrite32(0x00000003, watchdog_device.timer_control_addr);
  167. return 0;
  168. }
  169. static int intel_scu_stop(void)
  170. {
  171. iowrite32(0, watchdog_device.timer_control_addr);
  172. return 0;
  173. }
  174. static int intel_scu_set_heartbeat(u32 t)
  175. {
  176. int ipc_ret;
  177. int retry_count;
  178. u32 soft_value;
  179. u32 hw_pre_value;
  180. u32 hw_value;
  181. watchdog_device.timer_set = t;
  182. watchdog_device.threshold =
  183. timer_margin * watchdog_device.timer_tbl_ptr->freq_hz;
  184. watchdog_device.soft_threshold =
  185. (watchdog_device.timer_set - timer_margin)
  186. * watchdog_device.timer_tbl_ptr->freq_hz;
  187. pr_debug("Watchdog timer: set_heartbeat: timer freq is %d\n",
  188. watchdog_device.timer_tbl_ptr->freq_hz);
  189. pr_debug("Watchdog timer: set_heartbeat: timer_set is %x (hex)\n",
  190. watchdog_device.timer_set);
  191. pr_debug("Watchdog timer: set_hearbeat: timer_margin is %x (hex)\n",
  192. timer_margin);
  193. pr_debug("Watchdog timer: set_heartbeat: threshold is %x (hex)\n",
  194. watchdog_device.threshold);
  195. pr_debug("Watchdog timer: set_heartbeat: soft_threshold is %x (hex)\n",
  196. watchdog_device.soft_threshold);
  197. /* Adjust thresholds by FREQ_ADJUSTMENT factor, to make the */
  198. /* watchdog timing come out right. */
  199. watchdog_device.threshold =
  200. watchdog_device.threshold / FREQ_ADJUSTMENT;
  201. watchdog_device.soft_threshold =
  202. watchdog_device.soft_threshold / FREQ_ADJUSTMENT;
  203. /* temporarily disable the timer */
  204. iowrite32(0x00000002, watchdog_device.timer_control_addr);
  205. /* send the threshold and soft_threshold via IPC to the processor */
  206. ipc_ret = watchdog_set_ipc(watchdog_device.soft_threshold,
  207. watchdog_device.threshold);
  208. if (ipc_ret != 0) {
  209. /* Make sure the watchdog timer is stopped */
  210. intel_scu_stop();
  211. return ipc_ret;
  212. }
  213. /* Soft Threshold set loop. Early versions of silicon did */
  214. /* not always set this count correctly. This loop checks */
  215. /* the value and retries if it was not set correctly. */
  216. retry_count = 0;
  217. soft_value = watchdog_device.soft_threshold & 0xFFFF0000;
  218. do {
  219. /* Make sure timer is stopped */
  220. intel_scu_stop();
  221. if (MAX_RETRY < retry_count++) {
  222. /* Unable to set timer value */
  223. pr_err("Watchdog timer: Unable to set timer\n");
  224. return -ENODEV;
  225. }
  226. /* set the timer to the soft threshold */
  227. iowrite32(watchdog_device.soft_threshold,
  228. watchdog_device.timer_load_count_addr);
  229. /* read count value before starting timer */
  230. hw_pre_value = ioread32(watchdog_device.timer_load_count_addr);
  231. hw_pre_value = hw_pre_value & 0xFFFF0000;
  232. /* Start the timer */
  233. iowrite32(0x00000003, watchdog_device.timer_control_addr);
  234. /* read the value the time loaded into its count reg */
  235. hw_value = ioread32(watchdog_device.timer_load_count_addr);
  236. hw_value = hw_value & 0xFFFF0000;
  237. } while (soft_value != hw_value);
  238. watchdog_device.timer_started = 1;
  239. return 0;
  240. }
  241. /*
  242. * /dev/watchdog handling
  243. */
  244. static int intel_scu_open(struct inode *inode, struct file *file)
  245. {
  246. /* Set flag to indicate that watchdog device is open */
  247. if (test_and_set_bit(0, &watchdog_device.driver_open))
  248. return -EBUSY;
  249. /* Check for reopen of driver. Reopens are not allowed */
  250. if (watchdog_device.driver_closed)
  251. return -EPERM;
  252. return nonseekable_open(inode, file);
  253. }
  254. static int intel_scu_release(struct inode *inode, struct file *file)
  255. {
  256. /*
  257. * This watchdog should not be closed, after the timer
  258. * is started with the WDIPC_SETTIMEOUT ioctl
  259. * If force_boot is set watchdog_fire() will cause an
  260. * immediate reset. If force_boot is not set, the watchdog
  261. * timer is refreshed for one more interval. At the end
  262. * of that interval, the watchdog timer will reset the system.
  263. */
  264. if (!test_and_clear_bit(0, &watchdog_device.driver_open)) {
  265. pr_debug("Watchdog timer: intel_scu_release, without open\n");
  266. return -ENOTTY;
  267. }
  268. if (!watchdog_device.timer_started) {
  269. /* Just close, since timer has not been started */
  270. pr_debug("Watchdog timer: closed, without starting timer\n");
  271. return 0;
  272. }
  273. printk(KERN_CRIT PFX
  274. "Unexpected close of /dev/watchdog!\n");
  275. /* Since the timer was started, prevent future reopens */
  276. watchdog_device.driver_closed = 1;
  277. /* Refresh the timer for one more interval */
  278. intel_scu_keepalive();
  279. /* Reboot system (if force_boot is set) */
  280. watchdog_fire();
  281. /* We should only reach this point if force_boot is not set */
  282. return 0;
  283. }
  284. static ssize_t intel_scu_write(struct file *file,
  285. char const *data,
  286. size_t len,
  287. loff_t *ppos)
  288. {
  289. if (watchdog_device.timer_started)
  290. /* Watchdog already started, keep it alive */
  291. intel_scu_keepalive();
  292. else
  293. /* Start watchdog with timer value set by init */
  294. intel_scu_set_heartbeat(watchdog_device.timer_set);
  295. return len;
  296. }
  297. static long intel_scu_ioctl(struct file *file,
  298. unsigned int cmd,
  299. unsigned long arg)
  300. {
  301. void __user *argp = (void __user *)arg;
  302. u32 __user *p = argp;
  303. u32 new_margin;
  304. static const struct watchdog_info ident = {
  305. .options = WDIOF_SETTIMEOUT
  306. | WDIOF_KEEPALIVEPING,
  307. .firmware_version = 0, /* @todo Get from SCU via
  308. ipc_get_scu_fw_version()? */
  309. .identity = "Intel_SCU IOH Watchdog" /* len < 32 */
  310. };
  311. switch (cmd) {
  312. case WDIOC_GETSUPPORT:
  313. return copy_to_user(argp,
  314. &ident,
  315. sizeof(ident)) ? -EFAULT : 0;
  316. case WDIOC_GETSTATUS:
  317. case WDIOC_GETBOOTSTATUS:
  318. return put_user(0, p);
  319. case WDIOC_KEEPALIVE:
  320. intel_scu_keepalive();
  321. return 0;
  322. case WDIOC_SETTIMEOUT:
  323. if (get_user(new_margin, p))
  324. return -EFAULT;
  325. if (check_timer_margin(new_margin))
  326. return -EINVAL;
  327. if (intel_scu_set_heartbeat(new_margin))
  328. return -EINVAL;
  329. return 0;
  330. case WDIOC_GETTIMEOUT:
  331. return put_user(watchdog_device.soft_threshold, p);
  332. default:
  333. return -ENOTTY;
  334. }
  335. }
  336. /*
  337. * Notifier for system down
  338. */
  339. static int intel_scu_notify_sys(struct notifier_block *this,
  340. unsigned long code,
  341. void *another_unused)
  342. {
  343. if (code == SYS_DOWN || code == SYS_HALT)
  344. /* Turn off the watchdog timer. */
  345. intel_scu_stop();
  346. return NOTIFY_DONE;
  347. }
  348. /*
  349. * Kernel Interfaces
  350. */
  351. static const struct file_operations intel_scu_fops = {
  352. .owner = THIS_MODULE,
  353. .llseek = no_llseek,
  354. .write = intel_scu_write,
  355. .unlocked_ioctl = intel_scu_ioctl,
  356. .open = intel_scu_open,
  357. .release = intel_scu_release,
  358. };
  359. static int __init intel_scu_watchdog_init(void)
  360. {
  361. int ret;
  362. u32 __iomem *tmp_addr;
  363. /*
  364. * We don't really need to check this as the SFI timer get will fail
  365. * but if we do so we can exit with a clearer reason and no noise.
  366. *
  367. * If it isn't an intel MID device then it doesn't have this watchdog
  368. */
  369. if (!mrst_identify_cpu())
  370. return -ENODEV;
  371. /* Check boot parameters to verify that their initial values */
  372. /* are in range. */
  373. /* Check value of timer_set boot parameter */
  374. if ((timer_set < MIN_TIME_CYCLE) ||
  375. (timer_set > MAX_TIME - MIN_TIME_CYCLE)) {
  376. pr_err("Watchdog timer: value of timer_set %x (hex) "
  377. "is out of range from %x to %x (hex)\n",
  378. timer_set, MIN_TIME_CYCLE, MAX_TIME - MIN_TIME_CYCLE);
  379. return -EINVAL;
  380. }
  381. /* Check value of timer_margin boot parameter */
  382. if (check_timer_margin(timer_margin))
  383. return -EINVAL;
  384. watchdog_device.timer_tbl_ptr = sfi_get_mtmr(sfi_mtimer_num-1);
  385. if (watchdog_device.timer_tbl_ptr == NULL) {
  386. pr_debug("Watchdog timer - Intel SCU watchdog: timer is not available\n");
  387. return -ENODEV;
  388. }
  389. /* make sure the timer exists */
  390. if (watchdog_device.timer_tbl_ptr->phys_addr == 0) {
  391. pr_debug("Watchdog timer - Intel SCU watchdog - timer %d does not have valid physical memory\n",
  392. sfi_mtimer_num);
  393. return -ENODEV;
  394. }
  395. if (watchdog_device.timer_tbl_ptr->irq == 0) {
  396. pr_debug("Watchdog timer: timer %d invalid irq\n",
  397. sfi_mtimer_num);
  398. return -ENODEV;
  399. }
  400. tmp_addr = ioremap_nocache(watchdog_device.timer_tbl_ptr->phys_addr,
  401. 20);
  402. if (tmp_addr == NULL) {
  403. pr_debug("Watchdog timer: timer unable to ioremap\n");
  404. return -ENOMEM;
  405. }
  406. watchdog_device.timer_load_count_addr = tmp_addr++;
  407. watchdog_device.timer_current_value_addr = tmp_addr++;
  408. watchdog_device.timer_control_addr = tmp_addr++;
  409. watchdog_device.timer_clear_interrupt_addr = tmp_addr++;
  410. watchdog_device.timer_interrupt_status_addr = tmp_addr++;
  411. /* Set the default time values in device structure */
  412. watchdog_device.timer_set = timer_set;
  413. watchdog_device.threshold =
  414. timer_margin * watchdog_device.timer_tbl_ptr->freq_hz;
  415. watchdog_device.soft_threshold =
  416. (watchdog_device.timer_set - timer_margin)
  417. * watchdog_device.timer_tbl_ptr->freq_hz;
  418. watchdog_device.intel_scu_notifier.notifier_call =
  419. intel_scu_notify_sys;
  420. ret = register_reboot_notifier(&watchdog_device.intel_scu_notifier);
  421. if (ret) {
  422. pr_err("Watchdog timer: cannot register notifier %d)\n", ret);
  423. goto register_reboot_error;
  424. }
  425. watchdog_device.miscdev.minor = WATCHDOG_MINOR;
  426. watchdog_device.miscdev.name = "watchdog";
  427. watchdog_device.miscdev.fops = &intel_scu_fops;
  428. ret = misc_register(&watchdog_device.miscdev);
  429. if (ret) {
  430. pr_err("Watchdog timer: cannot register miscdev %d err =%d\n",
  431. WATCHDOG_MINOR, ret);
  432. goto misc_register_error;
  433. }
  434. ret = request_irq((unsigned int)watchdog_device.timer_tbl_ptr->irq,
  435. watchdog_timer_interrupt,
  436. IRQF_SHARED, "watchdog",
  437. &watchdog_device.timer_load_count_addr);
  438. if (ret) {
  439. pr_err("Watchdog timer: error requesting irq %d\n", ret);
  440. goto request_irq_error;
  441. }
  442. /* Make sure timer is disabled before returning */
  443. intel_scu_stop();
  444. return 0;
  445. /* error cleanup */
  446. request_irq_error:
  447. misc_deregister(&watchdog_device.miscdev);
  448. misc_register_error:
  449. unregister_reboot_notifier(&watchdog_device.intel_scu_notifier);
  450. register_reboot_error:
  451. intel_scu_stop();
  452. iounmap(watchdog_device.timer_load_count_addr);
  453. return ret;
  454. }
  455. static void __exit intel_scu_watchdog_exit(void)
  456. {
  457. misc_deregister(&watchdog_device.miscdev);
  458. unregister_reboot_notifier(&watchdog_device.intel_scu_notifier);
  459. /* disable the timer */
  460. iowrite32(0x00000002, watchdog_device.timer_control_addr);
  461. iounmap(watchdog_device.timer_load_count_addr);
  462. }
  463. late_initcall(intel_scu_watchdog_init);
  464. module_exit(intel_scu_watchdog_exit);
  465. MODULE_AUTHOR("Intel Corporation");
  466. MODULE_DESCRIPTION("Intel SCU Watchdog Device Driver");
  467. MODULE_LICENSE("GPL");
  468. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  469. MODULE_VERSION(WDT_VER);