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