2 * Octeon Watchdog driver
4 * Copyright (C) 2007-2017 Cavium, Inc.
6 * Converted to use WATCHDOG_CORE by Aaro Koskinen <aaro.koskinen@iki.fi>.
8 * Some parts derived from wdt.c
10 * (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
11 * All Rights Reserved.
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
18 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
19 * warranty for any of this software. This material is provided
20 * "AS-IS" and at no charge.
22 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file "COPYING" in the main directory of this archive
29 * The OCTEON watchdog has a maximum timeout of 2^32 * io_clock.
30 * For most systems this is less than 10 seconds, so to allow for
31 * software to request longer watchdog heartbeats, we maintain software
32 * counters to count multiples of the base rate. If the system locks
33 * up in such a manner that we can not run the software counters, the
34 * only result is a watchdog reset sooner than was requested. But
35 * that is OK, because in this case userspace would likely not be able
36 * to do anything anyhow.
38 * The hardware watchdog interval we call the period. The OCTEON
39 * watchdog goes through several stages, after the first period an
40 * irq is asserted, then if it is not reset, after the next period NMI
41 * is asserted, then after an additional period a chip wide soft reset.
42 * So for the software counters, we reset watchdog after each period
43 * and decrement the counter. But for the last two periods we need to
44 * let the watchdog progress to the NMI stage so we disable the irq
45 * and let it proceed. Once in the NMI, we print the register state
46 * to the serial port and then wait for the reset.
48 * A watchdog is maintained for each CPU in the system, that way if
49 * one CPU suffers a lockup, we also get a register dump and reset.
50 * The userspace ping resets the watchdog on all CPUs.
52 * Before userspace opens the watchdog device, we still run the
53 * watchdogs to catch any lockups that may be kernel related.
57 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
59 #include <linux/interrupt.h>
60 #include <linux/watchdog.h>
61 #include <linux/cpumask.h>
62 #include <linux/module.h>
63 #include <linux/delay.h>
64 #include <linux/cpu.h>
65 #include <linux/irq.h>
67 #include <asm/mipsregs.h>
70 #include <asm/octeon/octeon.h>
71 #include <asm/octeon/cvmx-boot-vector.h>
72 #include <asm/octeon/cvmx-ciu2-defs.h>
76 /* The count needed to achieve timeout_sec. */
77 static unsigned int timeout_cnt;
79 /* The maximum period supported. */
80 static unsigned int max_timeout_sec;
82 /* The current period. */
83 static unsigned int timeout_sec;
85 /* Set to non-zero when userspace countdown mode active */
86 static bool do_countdown;
87 static unsigned int countdown_reset;
88 static unsigned int per_cpu_countdown[NR_CPUS];
90 static cpumask_t irq_enabled_cpus;
92 #define WD_TIMO 60 /* Default heartbeat = 60 seconds */
94 static int heartbeat = WD_TIMO;
95 module_param(heartbeat, int, 0444);
96 MODULE_PARM_DESC(heartbeat,
97 "Watchdog heartbeat in seconds. (0 < heartbeat, default="
98 __MODULE_STRING(WD_TIMO) ")");
100 static bool nowayout = WATCHDOG_NOWAYOUT;
101 module_param(nowayout, bool, 0444);
102 MODULE_PARM_DESC(nowayout,
103 "Watchdog cannot be stopped once started (default="
104 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
107 module_param(disable, int, 0444);
108 MODULE_PARM_DESC(disable,
109 "Disable the watchdog entirely (default=0)");
111 static struct cvmx_boot_vector_element *octeon_wdt_bootvector;
113 void octeon_wdt_nmi_stage2(void);
115 static int cpu2core(int cpu)
118 return cpu_logical_map(cpu);
120 return cvmx_get_core_num();
124 static int core2cpu(int coreid)
127 return cpu_number_map(coreid);
134 * Poke the watchdog when an interrupt is received
141 static irqreturn_t octeon_wdt_poke_irq(int cpl, void *dev_id)
143 unsigned int core = cvmx_get_core_num();
144 int cpu = core2cpu(core);
147 if (per_cpu_countdown[cpu] > 0) {
148 /* We're alive, poke the watchdog */
149 cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
150 per_cpu_countdown[cpu]--;
152 /* Bad news, you are about to reboot. */
153 disable_irq_nosync(cpl);
154 cpumask_clear_cpu(cpu, &irq_enabled_cpus);
157 /* Not open, just ping away... */
158 cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
164 extern int prom_putchar(char c);
167 * Write a string to the uart
169 * @str: String to write
171 static void octeon_wdt_write_string(const char *str)
173 /* Just loop writing one byte at a time */
175 prom_putchar(*str++);
179 * Write a hex number out of the uart
181 * @value: Number to display
182 * @digits: Number of digits to print (1 to 16)
184 static void octeon_wdt_write_hex(u64 value, int digits)
189 for (d = 0; d < digits; d++) {
190 v = (value >> ((digits - d - 1) * 4)) & 0xf;
192 prom_putchar('a' + v - 10);
194 prom_putchar('0' + v);
198 static const char reg_name[][3] = {
199 "$0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
200 "a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3",
201 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
202 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"
206 * NMI stage 3 handler. NMIs are handled in the following manner:
207 * 1) The first NMI handler enables CVMSEG and transfers from
208 * the bootbus region into normal memory. It is careful to not
209 * destroy any registers.
210 * 2) The second stage handler uses CVMSEG to save the registers
211 * and create a stack for C code. It then calls the third level
212 * handler with one argument, a pointer to the register values.
213 * 3) The third, and final, level handler is the following C
214 * function that prints out some useful infomration.
216 * @reg: Pointer to register state before the NMI
218 void octeon_wdt_nmi_stage3(u64 reg[32])
222 unsigned int coreid = cvmx_get_core_num();
224 * Save status and cause early to get them before any changes
227 u64 cp0_cause = read_c0_cause();
228 u64 cp0_status = read_c0_status();
229 u64 cp0_error_epc = read_c0_errorepc();
230 u64 cp0_epc = read_c0_epc();
232 /* Delay so output from all cores output is not jumbled together. */
233 udelay(85000 * coreid);
235 octeon_wdt_write_string("\r\n*** NMI Watchdog interrupt on Core 0x");
236 octeon_wdt_write_hex(coreid, 2);
237 octeon_wdt_write_string(" ***\r\n");
238 for (i = 0; i < 32; i++) {
239 octeon_wdt_write_string("\t");
240 octeon_wdt_write_string(reg_name[i]);
241 octeon_wdt_write_string("\t0x");
242 octeon_wdt_write_hex(reg[i], 16);
244 octeon_wdt_write_string("\r\n");
246 octeon_wdt_write_string("\terr_epc\t0x");
247 octeon_wdt_write_hex(cp0_error_epc, 16);
249 octeon_wdt_write_string("\tepc\t0x");
250 octeon_wdt_write_hex(cp0_epc, 16);
251 octeon_wdt_write_string("\r\n");
253 octeon_wdt_write_string("\tstatus\t0x");
254 octeon_wdt_write_hex(cp0_status, 16);
255 octeon_wdt_write_string("\tcause\t0x");
256 octeon_wdt_write_hex(cp0_cause, 16);
257 octeon_wdt_write_string("\r\n");
259 /* The CIU register is different for each Octeon model. */
260 if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
261 octeon_wdt_write_string("\tsrc_wd\t0x");
262 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_SRC_PPX_IP2_WDOG(coreid)), 16);
263 octeon_wdt_write_string("\ten_wd\t0x");
264 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_EN_PPX_IP2_WDOG(coreid)), 16);
265 octeon_wdt_write_string("\r\n");
266 octeon_wdt_write_string("\tsrc_rml\t0x");
267 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_SRC_PPX_IP2_RML(coreid)), 16);
268 octeon_wdt_write_string("\ten_rml\t0x");
269 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_EN_PPX_IP2_RML(coreid)), 16);
270 octeon_wdt_write_string("\r\n");
271 octeon_wdt_write_string("\tsum\t0x");
272 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_SUM_PPX_IP2(coreid)), 16);
273 octeon_wdt_write_string("\r\n");
274 } else if (!octeon_has_feature(OCTEON_FEATURE_CIU3)) {
275 octeon_wdt_write_string("\tsum0\t0x");
276 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU_INTX_SUM0(coreid * 2)), 16);
277 octeon_wdt_write_string("\ten0\t0x");
278 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid * 2)), 16);
279 octeon_wdt_write_string("\r\n");
282 octeon_wdt_write_string("*** Chip soft reset soon ***\r\n");
285 static int octeon_wdt_cpu_pre_down(unsigned int cpu)
289 union cvmx_ciu_wdogx ciu_wdog;
291 core = cpu2core(cpu);
293 irq = OCTEON_IRQ_WDOG0 + core;
295 /* Poke the watchdog to clear out its state */
296 cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
298 /* Disable the hardware. */
300 cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
302 free_irq(irq, octeon_wdt_poke_irq);
306 static int octeon_wdt_cpu_online(unsigned int cpu)
310 union cvmx_ciu_wdogx ciu_wdog;
312 core = cpu2core(cpu);
314 octeon_wdt_bootvector[core].target_ptr = (u64)octeon_wdt_nmi_stage2;
316 /* Disable it before doing anything with the interrupts. */
318 cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
320 per_cpu_countdown[cpu] = countdown_reset;
322 irq = OCTEON_IRQ_WDOG0 + core;
324 if (request_irq(irq, octeon_wdt_poke_irq,
325 IRQF_NO_THREAD, "octeon_wdt", octeon_wdt_poke_irq))
326 panic("octeon_wdt: Couldn't obtain irq %d", irq);
328 cpumask_set_cpu(cpu, &irq_enabled_cpus);
330 /* Poke the watchdog to clear out its state */
331 cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
333 /* Finally enable the watchdog now that all handlers are installed */
335 ciu_wdog.s.len = timeout_cnt;
336 ciu_wdog.s.mode = 3; /* 3 = Interrupt + NMI + Soft-Reset */
337 cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
342 static int octeon_wdt_ping(struct watchdog_device __always_unused *wdog)
350 for_each_online_cpu(cpu) {
351 coreid = cpu2core(cpu);
352 cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
353 per_cpu_countdown[cpu] = countdown_reset;
354 if ((countdown_reset || !do_countdown) &&
355 !cpumask_test_cpu(cpu, &irq_enabled_cpus)) {
356 /* We have to enable the irq */
357 int irq = OCTEON_IRQ_WDOG0 + coreid;
360 cpumask_set_cpu(cpu, &irq_enabled_cpus);
366 static void octeon_wdt_calc_parameters(int t)
368 unsigned int periods;
370 timeout_sec = max_timeout_sec;
374 * Find the largest interrupt period, that can evenly divide
375 * the requested heartbeat time.
377 while ((t % timeout_sec) != 0)
380 periods = t / timeout_sec;
383 * The last two periods are after the irq is disabled, and
384 * then to the nmi, so we subtract them off.
387 countdown_reset = periods > 2 ? periods - 2 : 0;
389 timeout_cnt = ((octeon_get_io_clock_rate() / divisor) * timeout_sec) >> 8;
392 static int octeon_wdt_set_timeout(struct watchdog_device *wdog,
397 union cvmx_ciu_wdogx ciu_wdog;
402 octeon_wdt_calc_parameters(t);
407 for_each_online_cpu(cpu) {
408 coreid = cpu2core(cpu);
409 cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
411 ciu_wdog.s.len = timeout_cnt;
412 ciu_wdog.s.mode = 3; /* 3 = Interrupt + NMI + Soft-Reset */
413 cvmx_write_csr(CVMX_CIU_WDOGX(coreid), ciu_wdog.u64);
414 cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
416 octeon_wdt_ping(wdog); /* Get the irqs back on. */
420 static int octeon_wdt_start(struct watchdog_device *wdog)
422 octeon_wdt_ping(wdog);
427 static int octeon_wdt_stop(struct watchdog_device *wdog)
430 octeon_wdt_ping(wdog);
434 static const struct watchdog_info octeon_wdt_info = {
435 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
436 .identity = "OCTEON",
439 static const struct watchdog_ops octeon_wdt_ops = {
440 .owner = THIS_MODULE,
441 .start = octeon_wdt_start,
442 .stop = octeon_wdt_stop,
443 .ping = octeon_wdt_ping,
444 .set_timeout = octeon_wdt_set_timeout,
447 static struct watchdog_device octeon_wdt = {
448 .info = &octeon_wdt_info,
449 .ops = &octeon_wdt_ops,
452 static enum cpuhp_state octeon_wdt_online;
454 * Module/ driver initialization.
456 * Returns Zero on success
458 static int __init octeon_wdt_init(void)
462 octeon_wdt_bootvector = cvmx_boot_vector_get();
463 if (!octeon_wdt_bootvector) {
464 pr_err("Error: Cannot allocate boot vector.\n");
468 if (OCTEON_IS_MODEL(OCTEON_CN68XX))
474 * Watchdog time expiration length = The 16 bits of LEN
475 * represent the most significant bits of a 24 bit decrementer
476 * that decrements every divisor cycle.
478 * Try for a timeout of 5 sec, if that fails a smaller number
484 timeout_cnt = ((octeon_get_io_clock_rate() / divisor) * max_timeout_sec) >> 8;
485 } while (timeout_cnt > 65535);
487 BUG_ON(timeout_cnt == 0);
489 octeon_wdt_calc_parameters(heartbeat);
491 pr_info("Initial granularity %d Sec\n", timeout_sec);
493 octeon_wdt.timeout = timeout_sec;
494 octeon_wdt.max_timeout = UINT_MAX;
496 watchdog_set_nowayout(&octeon_wdt, nowayout);
498 ret = watchdog_register_device(&octeon_wdt);
500 pr_err("watchdog_register_device() failed: %d\n", ret);
505 pr_notice("disabled\n");
509 cpumask_clear(&irq_enabled_cpus);
511 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "watchdog/octeon:online",
512 octeon_wdt_cpu_online, octeon_wdt_cpu_pre_down);
515 octeon_wdt_online = ret;
518 cvmx_write_csr(CVMX_MIO_BOOT_LOC_CFGX(0), 0);
519 watchdog_unregister_device(&octeon_wdt);
524 * Module / driver shutdown
526 static void __exit octeon_wdt_cleanup(void)
528 watchdog_unregister_device(&octeon_wdt);
533 cpuhp_remove_state(octeon_wdt_online);
536 * Disable the boot-bus memory, the code it points to is soon
539 cvmx_write_csr(CVMX_MIO_BOOT_LOC_CFGX(0), 0);
542 MODULE_LICENSE("GPL");
543 MODULE_AUTHOR("Cavium Inc. <support@cavium.com>");
544 MODULE_DESCRIPTION("Cavium Inc. OCTEON Watchdog driver.");
545 module_init(octeon_wdt_init);
546 module_exit(octeon_wdt_cleanup);