intel_scu_watchdog.c 15 KB

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