intel_scu_watchdog.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <linux/compiler.h>
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/types.h>
  30. #include <linux/miscdevice.h>
  31. #include <linux/watchdog.h>
  32. #include <linux/fs.h>
  33. #include <linux/notifier.h>
  34. #include <linux/reboot.h>
  35. #include <linux/init.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/slab.h>
  39. #include <linux/io.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/delay.h>
  42. #include <linux/sched.h>
  43. #include <linux/signal.h>
  44. #include <linux/sfi.h>
  45. #include <asm/irq.h>
  46. #include <linux/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. pr_crit("Initiating system reboot\n");
  91. emergency_restart();
  92. pr_crit("Reboot didn't ?????\n");
  93. }
  94. else {
  95. pr_crit("Immediate Reboot Disabled\n");
  96. pr_crit("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("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("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("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("set_heartbeat: timer freq is %d\n",
  188. watchdog_device.timer_tbl_ptr->freq_hz);
  189. pr_debug("set_heartbeat: timer_set is %x (hex)\n",
  190. watchdog_device.timer_set);
  191. pr_debug("set_hearbeat: timer_margin is %x (hex)\n", timer_margin);
  192. pr_debug("set_heartbeat: threshold is %x (hex)\n",
  193. watchdog_device.threshold);
  194. pr_debug("set_heartbeat: soft_threshold is %x (hex)\n",
  195. watchdog_device.soft_threshold);
  196. /* Adjust thresholds by FREQ_ADJUSTMENT factor, to make the */
  197. /* watchdog timing come out right. */
  198. watchdog_device.threshold =
  199. watchdog_device.threshold / FREQ_ADJUSTMENT;
  200. watchdog_device.soft_threshold =
  201. watchdog_device.soft_threshold / FREQ_ADJUSTMENT;
  202. /* temporarily disable the timer */
  203. iowrite32(0x00000002, watchdog_device.timer_control_addr);
  204. /* send the threshold and soft_threshold via IPC to the processor */
  205. ipc_ret = watchdog_set_ipc(watchdog_device.soft_threshold,
  206. watchdog_device.threshold);
  207. if (ipc_ret != 0) {
  208. /* Make sure the watchdog timer is stopped */
  209. intel_scu_stop();
  210. return ipc_ret;
  211. }
  212. /* Soft Threshold set loop. Early versions of silicon did */
  213. /* not always set this count correctly. This loop checks */
  214. /* the value and retries if it was not set correctly. */
  215. retry_count = 0;
  216. soft_value = watchdog_device.soft_threshold & 0xFFFF0000;
  217. do {
  218. /* Make sure timer is stopped */
  219. intel_scu_stop();
  220. if (MAX_RETRY < retry_count++) {
  221. /* Unable to set timer value */
  222. pr_err("Unable to set timer\n");
  223. return -ENODEV;
  224. }
  225. /* set the timer to the soft threshold */
  226. iowrite32(watchdog_device.soft_threshold,
  227. watchdog_device.timer_load_count_addr);
  228. /* read count value before starting timer */
  229. hw_pre_value = ioread32(watchdog_device.timer_load_count_addr);
  230. hw_pre_value = hw_pre_value & 0xFFFF0000;
  231. /* Start the timer */
  232. iowrite32(0x00000003, watchdog_device.timer_control_addr);
  233. /* read the value the time loaded into its count reg */
  234. hw_value = ioread32(watchdog_device.timer_load_count_addr);
  235. hw_value = hw_value & 0xFFFF0000;
  236. } while (soft_value != hw_value);
  237. watchdog_device.timer_started = 1;
  238. return 0;
  239. }
  240. /*
  241. * /dev/watchdog handling
  242. */
  243. static int intel_scu_open(struct inode *inode, struct file *file)
  244. {
  245. /* Set flag to indicate that watchdog device is open */
  246. if (test_and_set_bit(0, &watchdog_device.driver_open))
  247. return -EBUSY;
  248. /* Check for reopen of driver. Reopens are not allowed */
  249. if (watchdog_device.driver_closed)
  250. return -EPERM;
  251. return nonseekable_open(inode, file);
  252. }
  253. static int intel_scu_release(struct inode *inode, struct file *file)
  254. {
  255. /*
  256. * This watchdog should not be closed, after the timer
  257. * is started with the WDIPC_SETTIMEOUT ioctl
  258. * If force_boot is set watchdog_fire() will cause an
  259. * immediate reset. If force_boot is not set, the watchdog
  260. * timer is refreshed for one more interval. At the end
  261. * of that interval, the watchdog timer will reset the system.
  262. */
  263. if (!test_and_clear_bit(0, &watchdog_device.driver_open)) {
  264. pr_debug("intel_scu_release, without open\n");
  265. return -ENOTTY;
  266. }
  267. if (!watchdog_device.timer_started) {
  268. /* Just close, since timer has not been started */
  269. pr_debug("closed, without starting timer\n");
  270. return 0;
  271. }
  272. pr_crit("Unexpected close of /dev/watchdog!\n");
  273. /* Since the timer was started, prevent future reopens */
  274. watchdog_device.driver_closed = 1;
  275. /* Refresh the timer for one more interval */
  276. intel_scu_keepalive();
  277. /* Reboot system (if force_boot is set) */
  278. watchdog_fire();
  279. /* We should only reach this point if force_boot is not set */
  280. return 0;
  281. }
  282. static ssize_t intel_scu_write(struct file *file,
  283. char const *data,
  284. size_t len,
  285. loff_t *ppos)
  286. {
  287. if (watchdog_device.timer_started)
  288. /* Watchdog already started, keep it alive */
  289. intel_scu_keepalive();
  290. else
  291. /* Start watchdog with timer value set by init */
  292. intel_scu_set_heartbeat(watchdog_device.timer_set);
  293. return len;
  294. }
  295. static long intel_scu_ioctl(struct file *file,
  296. unsigned int cmd,
  297. unsigned long arg)
  298. {
  299. void __user *argp = (void __user *)arg;
  300. u32 __user *p = argp;
  301. u32 new_margin;
  302. static const struct watchdog_info ident = {
  303. .options = WDIOF_SETTIMEOUT
  304. | WDIOF_KEEPALIVEPING,
  305. .firmware_version = 0, /* @todo Get from SCU via
  306. ipc_get_scu_fw_version()? */
  307. .identity = "Intel_SCU IOH Watchdog" /* len < 32 */
  308. };
  309. switch (cmd) {
  310. case WDIOC_GETSUPPORT:
  311. return copy_to_user(argp,
  312. &ident,
  313. sizeof(ident)) ? -EFAULT : 0;
  314. case WDIOC_GETSTATUS:
  315. case WDIOC_GETBOOTSTATUS:
  316. return put_user(0, p);
  317. case WDIOC_KEEPALIVE:
  318. intel_scu_keepalive();
  319. return 0;
  320. case WDIOC_SETTIMEOUT:
  321. if (get_user(new_margin, p))
  322. return -EFAULT;
  323. if (check_timer_margin(new_margin))
  324. return -EINVAL;
  325. if (intel_scu_set_heartbeat(new_margin))
  326. return -EINVAL;
  327. return 0;
  328. case WDIOC_GETTIMEOUT:
  329. return put_user(watchdog_device.soft_threshold, p);
  330. default:
  331. return -ENOTTY;
  332. }
  333. }
  334. /*
  335. * Notifier for system down
  336. */
  337. static int intel_scu_notify_sys(struct notifier_block *this,
  338. unsigned long code,
  339. void *another_unused)
  340. {
  341. if (code == SYS_DOWN || code == SYS_HALT)
  342. /* Turn off the watchdog timer. */
  343. intel_scu_stop();
  344. return NOTIFY_DONE;
  345. }
  346. /*
  347. * Kernel Interfaces
  348. */
  349. static const struct file_operations intel_scu_fops = {
  350. .owner = THIS_MODULE,
  351. .llseek = no_llseek,
  352. .write = intel_scu_write,
  353. .unlocked_ioctl = intel_scu_ioctl,
  354. .open = intel_scu_open,
  355. .release = intel_scu_release,
  356. };
  357. static int __init intel_scu_watchdog_init(void)
  358. {
  359. int ret;
  360. u32 __iomem *tmp_addr;
  361. /*
  362. * We don't really need to check this as the SFI timer get will fail
  363. * but if we do so we can exit with a clearer reason and no noise.
  364. *
  365. * If it isn't an intel MID device then it doesn't have this watchdog
  366. */
  367. if (!mrst_identify_cpu())
  368. return -ENODEV;
  369. /* Check boot parameters to verify that their initial values */
  370. /* are in range. */
  371. /* Check value of timer_set boot parameter */
  372. if ((timer_set < MIN_TIME_CYCLE) ||
  373. (timer_set > MAX_TIME - MIN_TIME_CYCLE)) {
  374. pr_err("value of timer_set %x (hex) is out of range from %x to %x (hex)\n",
  375. timer_set, MIN_TIME_CYCLE, MAX_TIME - MIN_TIME_CYCLE);
  376. return -EINVAL;
  377. }
  378. /* Check value of timer_margin boot parameter */
  379. if (check_timer_margin(timer_margin))
  380. return -EINVAL;
  381. watchdog_device.timer_tbl_ptr = sfi_get_mtmr(sfi_mtimer_num-1);
  382. if (watchdog_device.timer_tbl_ptr == NULL) {
  383. pr_debug("timer is not available\n");
  384. return -ENODEV;
  385. }
  386. /* make sure the timer exists */
  387. if (watchdog_device.timer_tbl_ptr->phys_addr == 0) {
  388. pr_debug("timer %d does not have valid physical memory\n",
  389. sfi_mtimer_num);
  390. return -ENODEV;
  391. }
  392. if (watchdog_device.timer_tbl_ptr->irq == 0) {
  393. pr_debug("timer %d invalid irq\n", sfi_mtimer_num);
  394. return -ENODEV;
  395. }
  396. tmp_addr = ioremap_nocache(watchdog_device.timer_tbl_ptr->phys_addr,
  397. 20);
  398. if (tmp_addr == NULL) {
  399. pr_debug("timer unable to ioremap\n");
  400. return -ENOMEM;
  401. }
  402. watchdog_device.timer_load_count_addr = tmp_addr++;
  403. watchdog_device.timer_current_value_addr = tmp_addr++;
  404. watchdog_device.timer_control_addr = tmp_addr++;
  405. watchdog_device.timer_clear_interrupt_addr = tmp_addr++;
  406. watchdog_device.timer_interrupt_status_addr = tmp_addr++;
  407. /* Set the default time values in device structure */
  408. watchdog_device.timer_set = timer_set;
  409. watchdog_device.threshold =
  410. timer_margin * watchdog_device.timer_tbl_ptr->freq_hz;
  411. watchdog_device.soft_threshold =
  412. (watchdog_device.timer_set - timer_margin)
  413. * watchdog_device.timer_tbl_ptr->freq_hz;
  414. watchdog_device.intel_scu_notifier.notifier_call =
  415. intel_scu_notify_sys;
  416. ret = register_reboot_notifier(&watchdog_device.intel_scu_notifier);
  417. if (ret) {
  418. pr_err("cannot register notifier %d)\n", ret);
  419. goto register_reboot_error;
  420. }
  421. watchdog_device.miscdev.minor = WATCHDOG_MINOR;
  422. watchdog_device.miscdev.name = "watchdog";
  423. watchdog_device.miscdev.fops = &intel_scu_fops;
  424. ret = misc_register(&watchdog_device.miscdev);
  425. if (ret) {
  426. pr_err("cannot register miscdev %d err =%d\n",
  427. WATCHDOG_MINOR, ret);
  428. goto misc_register_error;
  429. }
  430. ret = request_irq((unsigned int)watchdog_device.timer_tbl_ptr->irq,
  431. watchdog_timer_interrupt,
  432. IRQF_SHARED, "watchdog",
  433. &watchdog_device.timer_load_count_addr);
  434. if (ret) {
  435. pr_err("error requesting irq %d\n", ret);
  436. goto request_irq_error;
  437. }
  438. /* Make sure timer is disabled before returning */
  439. intel_scu_stop();
  440. return 0;
  441. /* error cleanup */
  442. request_irq_error:
  443. misc_deregister(&watchdog_device.miscdev);
  444. misc_register_error:
  445. unregister_reboot_notifier(&watchdog_device.intel_scu_notifier);
  446. register_reboot_error:
  447. intel_scu_stop();
  448. iounmap(watchdog_device.timer_load_count_addr);
  449. return ret;
  450. }
  451. static void __exit intel_scu_watchdog_exit(void)
  452. {
  453. misc_deregister(&watchdog_device.miscdev);
  454. unregister_reboot_notifier(&watchdog_device.intel_scu_notifier);
  455. /* disable the timer */
  456. iowrite32(0x00000002, watchdog_device.timer_control_addr);
  457. iounmap(watchdog_device.timer_load_count_addr);
  458. }
  459. late_initcall(intel_scu_watchdog_init);
  460. module_exit(intel_scu_watchdog_exit);
  461. MODULE_AUTHOR("Intel Corporation");
  462. MODULE_DESCRIPTION("Intel SCU Watchdog Device Driver");
  463. MODULE_LICENSE("GPL");
  464. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  465. MODULE_VERSION(WDT_VER);