octeon-wdt-main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*
  2. * Octeon Watchdog driver
  3. *
  4. * Copyright (C) 2007, 2008, 2009, 2010 Cavium Networks
  5. *
  6. * Some parts derived from wdt.c
  7. *
  8. * (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  9. * All Rights Reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  17. * warranty for any of this software. This material is provided
  18. * "AS-IS" and at no charge.
  19. *
  20. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
  21. *
  22. * This file is subject to the terms and conditions of the GNU General Public
  23. * License. See the file "COPYING" in the main directory of this archive
  24. * for more details.
  25. *
  26. *
  27. * The OCTEON watchdog has a maximum timeout of 2^32 * io_clock.
  28. * For most systems this is less than 10 seconds, so to allow for
  29. * software to request longer watchdog heartbeats, we maintain software
  30. * counters to count multiples of the base rate. If the system locks
  31. * up in such a manner that we can not run the software counters, the
  32. * only result is a watchdog reset sooner than was requested. But
  33. * that is OK, because in this case userspace would likely not be able
  34. * to do anything anyhow.
  35. *
  36. * The hardware watchdog interval we call the period. The OCTEON
  37. * watchdog goes through several stages, after the first period an
  38. * irq is asserted, then if it is not reset, after the next period NMI
  39. * is asserted, then after an additional period a chip wide soft reset.
  40. * So for the software counters, we reset watchdog after each period
  41. * and decrement the counter. But for the last two periods we need to
  42. * let the watchdog progress to the NMI stage so we disable the irq
  43. * and let it proceed. Once in the NMI, we print the register state
  44. * to the serial port and then wait for the reset.
  45. *
  46. * A watchdog is maintained for each CPU in the system, that way if
  47. * one CPU suffers a lockup, we also get a register dump and reset.
  48. * The userspace ping resets the watchdog on all CPUs.
  49. *
  50. * Before userspace opens the watchdog device, we still run the
  51. * watchdogs to catch any lockups that may be kernel related.
  52. *
  53. */
  54. #include <linux/miscdevice.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/watchdog.h>
  57. #include <linux/cpumask.h>
  58. #include <linux/bitops.h>
  59. #include <linux/kernel.h>
  60. #include <linux/module.h>
  61. #include <linux/string.h>
  62. #include <linux/delay.h>
  63. #include <linux/cpu.h>
  64. #include <linux/smp.h>
  65. #include <linux/fs.h>
  66. #include <linux/irq.h>
  67. #include <asm/mipsregs.h>
  68. #include <asm/uasm.h>
  69. #include <asm/octeon/octeon.h>
  70. /* The count needed to achieve timeout_sec. */
  71. static unsigned int timeout_cnt;
  72. /* The maximum period supported. */
  73. static unsigned int max_timeout_sec;
  74. /* The current period. */
  75. static unsigned int timeout_sec;
  76. /* Set to non-zero when userspace countdown mode active */
  77. static int do_coundown;
  78. static unsigned int countdown_reset;
  79. static unsigned int per_cpu_countdown[NR_CPUS];
  80. static cpumask_t irq_enabled_cpus;
  81. #define WD_TIMO 60 /* Default heartbeat = 60 seconds */
  82. static int heartbeat = WD_TIMO;
  83. module_param(heartbeat, int, S_IRUGO);
  84. MODULE_PARM_DESC(heartbeat,
  85. "Watchdog heartbeat in seconds. (0 < heartbeat, default="
  86. __MODULE_STRING(WD_TIMO) ")");
  87. static int nowayout = WATCHDOG_NOWAYOUT;
  88. module_param(nowayout, int, S_IRUGO);
  89. MODULE_PARM_DESC(nowayout,
  90. "Watchdog cannot be stopped once started (default="
  91. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  92. static unsigned long octeon_wdt_is_open;
  93. static char expect_close;
  94. static u32 __initdata nmi_stage1_insns[64];
  95. /* We need one branch and therefore one relocation per target label. */
  96. static struct uasm_label __initdata labels[5];
  97. static struct uasm_reloc __initdata relocs[5];
  98. enum lable_id {
  99. label_enter_bootloader = 1
  100. };
  101. /* Some CP0 registers */
  102. #define K0 26
  103. #define C0_CVMMEMCTL 11, 7
  104. #define C0_STATUS 12, 0
  105. #define C0_EBASE 15, 1
  106. #define C0_DESAVE 31, 0
  107. void octeon_wdt_nmi_stage2(void);
  108. static void __init octeon_wdt_build_stage1(void)
  109. {
  110. int i;
  111. int len;
  112. u32 *p = nmi_stage1_insns;
  113. #ifdef CONFIG_HOTPLUG_CPU
  114. struct uasm_label *l = labels;
  115. struct uasm_reloc *r = relocs;
  116. #endif
  117. /*
  118. * For the next few instructions running the debugger may
  119. * cause corruption of k0 in the saved registers. Since we're
  120. * about to crash, nobody probably cares.
  121. *
  122. * Save K0 into the debug scratch register
  123. */
  124. uasm_i_dmtc0(&p, K0, C0_DESAVE);
  125. uasm_i_mfc0(&p, K0, C0_STATUS);
  126. #ifdef CONFIG_HOTPLUG_CPU
  127. uasm_il_bbit0(&p, &r, K0, ilog2(ST0_NMI), label_enter_bootloader);
  128. #endif
  129. /* Force 64-bit addressing enabled */
  130. uasm_i_ori(&p, K0, K0, ST0_UX | ST0_SX | ST0_KX);
  131. uasm_i_mtc0(&p, K0, C0_STATUS);
  132. #ifdef CONFIG_HOTPLUG_CPU
  133. uasm_i_mfc0(&p, K0, C0_EBASE);
  134. /* Coreid number in K0 */
  135. uasm_i_andi(&p, K0, K0, 0xf);
  136. /* 8 * coreid in bits 16-31 */
  137. uasm_i_dsll_safe(&p, K0, K0, 3 + 16);
  138. uasm_i_ori(&p, K0, K0, 0x8001);
  139. uasm_i_dsll_safe(&p, K0, K0, 16);
  140. uasm_i_ori(&p, K0, K0, 0x0700);
  141. uasm_i_drotr_safe(&p, K0, K0, 32);
  142. /*
  143. * Should result in: 0x8001,0700,0000,8*coreid which is
  144. * CVMX_CIU_WDOGX(coreid) - 0x0500
  145. *
  146. * Now ld K0, CVMX_CIU_WDOGX(coreid)
  147. */
  148. uasm_i_ld(&p, K0, 0x500, K0);
  149. /*
  150. * If bit one set handle the NMI as a watchdog event.
  151. * otherwise transfer control to bootloader.
  152. */
  153. uasm_il_bbit0(&p, &r, K0, 1, label_enter_bootloader);
  154. uasm_i_nop(&p);
  155. #endif
  156. /* Clear Dcache so cvmseg works right. */
  157. uasm_i_cache(&p, 1, 0, 0);
  158. /* Use K0 to do a read/modify/write of CVMMEMCTL */
  159. uasm_i_dmfc0(&p, K0, C0_CVMMEMCTL);
  160. /* Clear out the size of CVMSEG */
  161. uasm_i_dins(&p, K0, 0, 0, 6);
  162. /* Set CVMSEG to its largest value */
  163. uasm_i_ori(&p, K0, K0, 0x1c0 | 54);
  164. /* Store the CVMMEMCTL value */
  165. uasm_i_dmtc0(&p, K0, C0_CVMMEMCTL);
  166. /* Load the address of the second stage handler */
  167. UASM_i_LA(&p, K0, (long)octeon_wdt_nmi_stage2);
  168. uasm_i_jr(&p, K0);
  169. uasm_i_dmfc0(&p, K0, C0_DESAVE);
  170. #ifdef CONFIG_HOTPLUG_CPU
  171. uasm_build_label(&l, p, label_enter_bootloader);
  172. /* Jump to the bootloader and restore K0 */
  173. UASM_i_LA(&p, K0, (long)octeon_bootloader_entry_addr);
  174. uasm_i_jr(&p, K0);
  175. uasm_i_dmfc0(&p, K0, C0_DESAVE);
  176. #endif
  177. uasm_resolve_relocs(relocs, labels);
  178. len = (int)(p - nmi_stage1_insns);
  179. pr_debug("Synthesized NMI stage 1 handler (%d instructions).\n", len);
  180. pr_debug("\t.set push\n");
  181. pr_debug("\t.set noreorder\n");
  182. for (i = 0; i < len; i++)
  183. pr_debug("\t.word 0x%08x\n", nmi_stage1_insns[i]);
  184. pr_debug("\t.set pop\n");
  185. if (len > 32)
  186. panic("NMI stage 1 handler exceeds 32 instructions, was %d\n", len);
  187. }
  188. static int cpu2core(int cpu)
  189. {
  190. #ifdef CONFIG_SMP
  191. return cpu_logical_map(cpu);
  192. #else
  193. return cvmx_get_core_num();
  194. #endif
  195. }
  196. static int core2cpu(int coreid)
  197. {
  198. #ifdef CONFIG_SMP
  199. return cpu_number_map(coreid);
  200. #else
  201. return 0;
  202. #endif
  203. }
  204. /**
  205. * Poke the watchdog when an interrupt is received
  206. *
  207. * @cpl:
  208. * @dev_id:
  209. *
  210. * Returns
  211. */
  212. static irqreturn_t octeon_wdt_poke_irq(int cpl, void *dev_id)
  213. {
  214. unsigned int core = cvmx_get_core_num();
  215. int cpu = core2cpu(core);
  216. if (do_coundown) {
  217. if (per_cpu_countdown[cpu] > 0) {
  218. /* We're alive, poke the watchdog */
  219. cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
  220. per_cpu_countdown[cpu]--;
  221. } else {
  222. /* Bad news, you are about to reboot. */
  223. disable_irq_nosync(cpl);
  224. cpumask_clear_cpu(cpu, &irq_enabled_cpus);
  225. }
  226. } else {
  227. /* Not open, just ping away... */
  228. cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
  229. }
  230. return IRQ_HANDLED;
  231. }
  232. /* From setup.c */
  233. extern int prom_putchar(char c);
  234. /**
  235. * Write a string to the uart
  236. *
  237. * @str: String to write
  238. */
  239. static void octeon_wdt_write_string(const char *str)
  240. {
  241. /* Just loop writing one byte at a time */
  242. while (*str)
  243. prom_putchar(*str++);
  244. }
  245. /**
  246. * Write a hex number out of the uart
  247. *
  248. * @value: Number to display
  249. * @digits: Number of digits to print (1 to 16)
  250. */
  251. static void octeon_wdt_write_hex(u64 value, int digits)
  252. {
  253. int d;
  254. int v;
  255. for (d = 0; d < digits; d++) {
  256. v = (value >> ((digits - d - 1) * 4)) & 0xf;
  257. if (v >= 10)
  258. prom_putchar('a' + v - 10);
  259. else
  260. prom_putchar('0' + v);
  261. }
  262. }
  263. const char *reg_name[] = {
  264. "$0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
  265. "a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3",
  266. "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
  267. "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"
  268. };
  269. /**
  270. * NMI stage 3 handler. NMIs are handled in the following manner:
  271. * 1) The first NMI handler enables CVMSEG and transfers from
  272. * the bootbus region into normal memory. It is careful to not
  273. * destroy any registers.
  274. * 2) The second stage handler uses CVMSEG to save the registers
  275. * and create a stack for C code. It then calls the third level
  276. * handler with one argument, a pointer to the register values.
  277. * 3) The third, and final, level handler is the following C
  278. * function that prints out some useful infomration.
  279. *
  280. * @reg: Pointer to register state before the NMI
  281. */
  282. void octeon_wdt_nmi_stage3(u64 reg[32])
  283. {
  284. u64 i;
  285. unsigned int coreid = cvmx_get_core_num();
  286. /*
  287. * Save status and cause early to get them before any changes
  288. * might happen.
  289. */
  290. u64 cp0_cause = read_c0_cause();
  291. u64 cp0_status = read_c0_status();
  292. u64 cp0_error_epc = read_c0_errorepc();
  293. u64 cp0_epc = read_c0_epc();
  294. /* Delay so output from all cores output is not jumbled together. */
  295. __delay(100000000ull * coreid);
  296. octeon_wdt_write_string("\r\n*** NMI Watchdog interrupt on Core 0x");
  297. octeon_wdt_write_hex(coreid, 1);
  298. octeon_wdt_write_string(" ***\r\n");
  299. for (i = 0; i < 32; i++) {
  300. octeon_wdt_write_string("\t");
  301. octeon_wdt_write_string(reg_name[i]);
  302. octeon_wdt_write_string("\t0x");
  303. octeon_wdt_write_hex(reg[i], 16);
  304. if (i & 1)
  305. octeon_wdt_write_string("\r\n");
  306. }
  307. octeon_wdt_write_string("\terr_epc\t0x");
  308. octeon_wdt_write_hex(cp0_error_epc, 16);
  309. octeon_wdt_write_string("\tepc\t0x");
  310. octeon_wdt_write_hex(cp0_epc, 16);
  311. octeon_wdt_write_string("\r\n");
  312. octeon_wdt_write_string("\tstatus\t0x");
  313. octeon_wdt_write_hex(cp0_status, 16);
  314. octeon_wdt_write_string("\tcause\t0x");
  315. octeon_wdt_write_hex(cp0_cause, 16);
  316. octeon_wdt_write_string("\r\n");
  317. octeon_wdt_write_string("\tsum0\t0x");
  318. octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU_INTX_SUM0(coreid * 2)), 16);
  319. octeon_wdt_write_string("\ten0\t0x");
  320. octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid * 2)), 16);
  321. octeon_wdt_write_string("\r\n");
  322. octeon_wdt_write_string("*** Chip soft reset soon ***\r\n");
  323. }
  324. static void octeon_wdt_disable_interrupt(int cpu)
  325. {
  326. unsigned int core;
  327. unsigned int irq;
  328. union cvmx_ciu_wdogx ciu_wdog;
  329. core = cpu2core(cpu);
  330. irq = OCTEON_IRQ_WDOG0 + core;
  331. /* Poke the watchdog to clear out its state */
  332. cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
  333. /* Disable the hardware. */
  334. ciu_wdog.u64 = 0;
  335. cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
  336. free_irq(irq, octeon_wdt_poke_irq);
  337. }
  338. static void octeon_wdt_setup_interrupt(int cpu)
  339. {
  340. unsigned int core;
  341. unsigned int irq;
  342. union cvmx_ciu_wdogx ciu_wdog;
  343. core = cpu2core(cpu);
  344. /* Disable it before doing anything with the interrupts. */
  345. ciu_wdog.u64 = 0;
  346. cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
  347. per_cpu_countdown[cpu] = countdown_reset;
  348. irq = OCTEON_IRQ_WDOG0 + core;
  349. if (request_irq(irq, octeon_wdt_poke_irq,
  350. IRQF_DISABLED, "octeon_wdt", octeon_wdt_poke_irq))
  351. panic("octeon_wdt: Couldn't obtain irq %d", irq);
  352. cpumask_set_cpu(cpu, &irq_enabled_cpus);
  353. /* Poke the watchdog to clear out its state */
  354. cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
  355. /* Finally enable the watchdog now that all handlers are installed */
  356. ciu_wdog.u64 = 0;
  357. ciu_wdog.s.len = timeout_cnt;
  358. ciu_wdog.s.mode = 3; /* 3 = Interrupt + NMI + Soft-Reset */
  359. cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
  360. }
  361. static int octeon_wdt_cpu_callback(struct notifier_block *nfb,
  362. unsigned long action, void *hcpu)
  363. {
  364. unsigned int cpu = (unsigned long)hcpu;
  365. switch (action) {
  366. case CPU_DOWN_PREPARE:
  367. octeon_wdt_disable_interrupt(cpu);
  368. break;
  369. case CPU_ONLINE:
  370. case CPU_DOWN_FAILED:
  371. octeon_wdt_setup_interrupt(cpu);
  372. break;
  373. default:
  374. break;
  375. }
  376. return NOTIFY_OK;
  377. }
  378. static void octeon_wdt_ping(void)
  379. {
  380. int cpu;
  381. int coreid;
  382. for_each_online_cpu(cpu) {
  383. coreid = cpu2core(cpu);
  384. cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
  385. per_cpu_countdown[cpu] = countdown_reset;
  386. if ((countdown_reset || !do_coundown) &&
  387. !cpumask_test_cpu(cpu, &irq_enabled_cpus)) {
  388. /* We have to enable the irq */
  389. int irq = OCTEON_IRQ_WDOG0 + coreid;
  390. enable_irq(irq);
  391. cpumask_set_cpu(cpu, &irq_enabled_cpus);
  392. }
  393. }
  394. }
  395. static void octeon_wdt_calc_parameters(int t)
  396. {
  397. unsigned int periods;
  398. timeout_sec = max_timeout_sec;
  399. /*
  400. * Find the largest interrupt period, that can evenly divide
  401. * the requested heartbeat time.
  402. */
  403. while ((t % timeout_sec) != 0)
  404. timeout_sec--;
  405. periods = t / timeout_sec;
  406. /*
  407. * The last two periods are after the irq is disabled, and
  408. * then to the nmi, so we subtract them off.
  409. */
  410. countdown_reset = periods > 2 ? periods - 2 : 0;
  411. heartbeat = t;
  412. timeout_cnt = ((octeon_get_io_clock_rate() >> 8) * timeout_sec) >> 8;
  413. }
  414. static int octeon_wdt_set_heartbeat(int t)
  415. {
  416. int cpu;
  417. int coreid;
  418. union cvmx_ciu_wdogx ciu_wdog;
  419. if (t <= 0)
  420. return -1;
  421. octeon_wdt_calc_parameters(t);
  422. for_each_online_cpu(cpu) {
  423. coreid = cpu2core(cpu);
  424. cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
  425. ciu_wdog.u64 = 0;
  426. ciu_wdog.s.len = timeout_cnt;
  427. ciu_wdog.s.mode = 3; /* 3 = Interrupt + NMI + Soft-Reset */
  428. cvmx_write_csr(CVMX_CIU_WDOGX(coreid), ciu_wdog.u64);
  429. cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
  430. }
  431. octeon_wdt_ping(); /* Get the irqs back on. */
  432. return 0;
  433. }
  434. /**
  435. * octeon_wdt_write:
  436. * @file: file handle to the watchdog
  437. * @buf: buffer to write (unused as data does not matter here
  438. * @count: count of bytes
  439. * @ppos: pointer to the position to write. No seeks allowed
  440. *
  441. * A write to a watchdog device is defined as a keepalive signal. Any
  442. * write of data will do, as we we don't define content meaning.
  443. */
  444. static ssize_t octeon_wdt_write(struct file *file, const char __user *buf,
  445. size_t count, loff_t *ppos)
  446. {
  447. if (count) {
  448. if (!nowayout) {
  449. size_t i;
  450. /* In case it was set long ago */
  451. expect_close = 0;
  452. for (i = 0; i != count; i++) {
  453. char c;
  454. if (get_user(c, buf + i))
  455. return -EFAULT;
  456. if (c == 'V')
  457. expect_close = 1;
  458. }
  459. }
  460. octeon_wdt_ping();
  461. }
  462. return count;
  463. }
  464. /**
  465. * octeon_wdt_ioctl:
  466. * @file: file handle to the device
  467. * @cmd: watchdog command
  468. * @arg: argument pointer
  469. *
  470. * The watchdog API defines a common set of functions for all
  471. * watchdogs according to their available features. We only
  472. * actually usefully support querying capabilities and setting
  473. * the timeout.
  474. */
  475. static long octeon_wdt_ioctl(struct file *file, unsigned int cmd,
  476. unsigned long arg)
  477. {
  478. void __user *argp = (void __user *)arg;
  479. int __user *p = argp;
  480. int new_heartbeat;
  481. static struct watchdog_info ident = {
  482. .options = WDIOF_SETTIMEOUT|
  483. WDIOF_MAGICCLOSE|
  484. WDIOF_KEEPALIVEPING,
  485. .firmware_version = 1,
  486. .identity = "OCTEON",
  487. };
  488. switch (cmd) {
  489. case WDIOC_GETSUPPORT:
  490. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  491. case WDIOC_GETSTATUS:
  492. case WDIOC_GETBOOTSTATUS:
  493. return put_user(0, p);
  494. case WDIOC_KEEPALIVE:
  495. octeon_wdt_ping();
  496. return 0;
  497. case WDIOC_SETTIMEOUT:
  498. if (get_user(new_heartbeat, p))
  499. return -EFAULT;
  500. if (octeon_wdt_set_heartbeat(new_heartbeat))
  501. return -EINVAL;
  502. /* Fall through. */
  503. case WDIOC_GETTIMEOUT:
  504. return put_user(heartbeat, p);
  505. default:
  506. return -ENOTTY;
  507. }
  508. }
  509. /**
  510. * octeon_wdt_open:
  511. * @inode: inode of device
  512. * @file: file handle to device
  513. *
  514. * The watchdog device has been opened. The watchdog device is single
  515. * open and on opening we do a ping to reset the counters.
  516. */
  517. static int octeon_wdt_open(struct inode *inode, struct file *file)
  518. {
  519. if (test_and_set_bit(0, &octeon_wdt_is_open))
  520. return -EBUSY;
  521. /*
  522. * Activate
  523. */
  524. octeon_wdt_ping();
  525. do_coundown = 1;
  526. return nonseekable_open(inode, file);
  527. }
  528. /**
  529. * octeon_wdt_release:
  530. * @inode: inode to board
  531. * @file: file handle to board
  532. *
  533. * The watchdog has a configurable API. There is a religious dispute
  534. * between people who want their watchdog to be able to shut down and
  535. * those who want to be sure if the watchdog manager dies the machine
  536. * reboots. In the former case we disable the counters, in the latter
  537. * case you have to open it again very soon.
  538. */
  539. static int octeon_wdt_release(struct inode *inode, struct file *file)
  540. {
  541. if (expect_close) {
  542. do_coundown = 0;
  543. octeon_wdt_ping();
  544. } else {
  545. pr_crit("octeon_wdt: WDT device closed unexpectedly. WDT will not stop!\n");
  546. }
  547. clear_bit(0, &octeon_wdt_is_open);
  548. expect_close = 0;
  549. return 0;
  550. }
  551. static const struct file_operations octeon_wdt_fops = {
  552. .owner = THIS_MODULE,
  553. .llseek = no_llseek,
  554. .write = octeon_wdt_write,
  555. .unlocked_ioctl = octeon_wdt_ioctl,
  556. .open = octeon_wdt_open,
  557. .release = octeon_wdt_release,
  558. };
  559. static struct miscdevice octeon_wdt_miscdev = {
  560. .minor = WATCHDOG_MINOR,
  561. .name = "watchdog",
  562. .fops = &octeon_wdt_fops,
  563. };
  564. static struct notifier_block octeon_wdt_cpu_notifier = {
  565. .notifier_call = octeon_wdt_cpu_callback,
  566. };
  567. /**
  568. * Module/ driver initialization.
  569. *
  570. * Returns Zero on success
  571. */
  572. static int __init octeon_wdt_init(void)
  573. {
  574. int i;
  575. int ret;
  576. int cpu;
  577. u64 *ptr;
  578. /*
  579. * Watchdog time expiration length = The 16 bits of LEN
  580. * represent the most significant bits of a 24 bit decrementer
  581. * that decrements every 256 cycles.
  582. *
  583. * Try for a timeout of 5 sec, if that fails a smaller number
  584. * of even seconds,
  585. */
  586. max_timeout_sec = 6;
  587. do {
  588. max_timeout_sec--;
  589. timeout_cnt = ((octeon_get_io_clock_rate() >> 8) * max_timeout_sec) >> 8;
  590. } while (timeout_cnt > 65535);
  591. BUG_ON(timeout_cnt == 0);
  592. octeon_wdt_calc_parameters(heartbeat);
  593. pr_info("octeon_wdt: Initial granularity %d Sec.\n", timeout_sec);
  594. ret = misc_register(&octeon_wdt_miscdev);
  595. if (ret) {
  596. pr_err("octeon_wdt: cannot register miscdev on minor=%d (err=%d)\n",
  597. WATCHDOG_MINOR, ret);
  598. goto out;
  599. }
  600. /* Build the NMI handler ... */
  601. octeon_wdt_build_stage1();
  602. /* ... and install it. */
  603. ptr = (u64 *) nmi_stage1_insns;
  604. for (i = 0; i < 16; i++) {
  605. cvmx_write_csr(CVMX_MIO_BOOT_LOC_ADR, i * 8);
  606. cvmx_write_csr(CVMX_MIO_BOOT_LOC_DAT, ptr[i]);
  607. }
  608. cvmx_write_csr(CVMX_MIO_BOOT_LOC_CFGX(0), 0x81fc0000);
  609. cpumask_clear(&irq_enabled_cpus);
  610. for_each_online_cpu(cpu)
  611. octeon_wdt_setup_interrupt(cpu);
  612. register_hotcpu_notifier(&octeon_wdt_cpu_notifier);
  613. out:
  614. return ret;
  615. }
  616. /**
  617. * Module / driver shutdown
  618. */
  619. static void __exit octeon_wdt_cleanup(void)
  620. {
  621. int cpu;
  622. misc_deregister(&octeon_wdt_miscdev);
  623. unregister_hotcpu_notifier(&octeon_wdt_cpu_notifier);
  624. for_each_online_cpu(cpu) {
  625. int core = cpu2core(cpu);
  626. /* Disable the watchdog */
  627. cvmx_write_csr(CVMX_CIU_WDOGX(core), 0);
  628. /* Free the interrupt handler */
  629. free_irq(OCTEON_IRQ_WDOG0 + core, octeon_wdt_poke_irq);
  630. }
  631. /*
  632. * Disable the boot-bus memory, the code it points to is soon
  633. * to go missing.
  634. */
  635. cvmx_write_csr(CVMX_MIO_BOOT_LOC_CFGX(0), 0);
  636. }
  637. MODULE_LICENSE("GPL");
  638. MODULE_AUTHOR("Cavium Networks <support@caviumnetworks.com>");
  639. MODULE_DESCRIPTION("Cavium Networks Octeon Watchdog driver.");
  640. module_init(octeon_wdt_init);
  641. module_exit(octeon_wdt_cleanup);