blob: fbdd48404b54d8ba9babc279487e657edc91ae4a [file] [log] [blame]
David Daney4c076fb2010-07-24 10:16:05 -07001/*
2 * Octeon Watchdog driver
3 *
4 * Copyright (C) 2007, 2008, 2009, 2010 Cavium Networks
5 *
Aaro Koskinen3d588c92015-03-28 20:05:38 +02006 * Converted to use WATCHDOG_CORE by Aaro Koskinen <aaro.koskinen@iki.fi>.
7 *
David Daney4c076fb2010-07-24 10:16:05 -07008 * Some parts derived from wdt.c
9 *
10 * (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
11 * All Rights Reserved.
12 *
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.
17 *
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.
21 *
22 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
23 *
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
26 * for more details.
27 *
28 *
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.
37 *
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.
47 *
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.
51 *
52 * Before userspace opens the watchdog device, we still run the
53 * watchdogs to catch any lockups that may be kernel related.
54 *
55 */
56
Joe Perches27c766a2012-02-15 15:06:19 -080057#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
58
David Daney4c076fb2010-07-24 10:16:05 -070059#include <linux/interrupt.h>
60#include <linux/watchdog.h>
61#include <linux/cpumask.h>
62#include <linux/bitops.h>
63#include <linux/kernel.h>
64#include <linux/module.h>
65#include <linux/string.h>
66#include <linux/delay.h>
67#include <linux/cpu.h>
68#include <linux/smp.h>
69#include <linux/fs.h>
David Howellsca4d3e672010-10-07 14:08:54 +010070#include <linux/irq.h>
David Daney4c076fb2010-07-24 10:16:05 -070071
72#include <asm/mipsregs.h>
73#include <asm/uasm.h>
74
75#include <asm/octeon/octeon.h>
Steven J. Hill49d148b2017-08-29 10:40:32 -050076#include <asm/octeon/cvmx-boot-vector.h>
David Daney4c076fb2010-07-24 10:16:05 -070077
78/* The count needed to achieve timeout_sec. */
79static unsigned int timeout_cnt;
80
81/* The maximum period supported. */
82static unsigned int max_timeout_sec;
83
84/* The current period. */
85static unsigned int timeout_sec;
86
87/* Set to non-zero when userspace countdown mode active */
88static int do_coundown;
89static unsigned int countdown_reset;
90static unsigned int per_cpu_countdown[NR_CPUS];
91
92static cpumask_t irq_enabled_cpus;
93
94#define WD_TIMO 60 /* Default heartbeat = 60 seconds */
95
96static int heartbeat = WD_TIMO;
97module_param(heartbeat, int, S_IRUGO);
98MODULE_PARM_DESC(heartbeat,
99 "Watchdog heartbeat in seconds. (0 < heartbeat, default="
100 __MODULE_STRING(WD_TIMO) ")");
101
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100102static bool nowayout = WATCHDOG_NOWAYOUT;
103module_param(nowayout, bool, S_IRUGO);
David Daney4c076fb2010-07-24 10:16:05 -0700104MODULE_PARM_DESC(nowayout,
105 "Watchdog cannot be stopped once started (default="
106 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
107
Steven J. Hill49d148b2017-08-29 10:40:32 -0500108static struct cvmx_boot_vector_element *octeon_wdt_bootvector;
David Daney4c076fb2010-07-24 10:16:05 -0700109
110void octeon_wdt_nmi_stage2(void);
111
David Daney4c076fb2010-07-24 10:16:05 -0700112static int cpu2core(int cpu)
113{
114#ifdef CONFIG_SMP
115 return cpu_logical_map(cpu);
116#else
117 return cvmx_get_core_num();
118#endif
119}
120
121static int core2cpu(int coreid)
122{
123#ifdef CONFIG_SMP
124 return cpu_number_map(coreid);
125#else
126 return 0;
127#endif
128}
129
130/**
131 * Poke the watchdog when an interrupt is received
132 *
133 * @cpl:
134 * @dev_id:
135 *
136 * Returns
137 */
138static irqreturn_t octeon_wdt_poke_irq(int cpl, void *dev_id)
139{
140 unsigned int core = cvmx_get_core_num();
141 int cpu = core2cpu(core);
142
143 if (do_coundown) {
144 if (per_cpu_countdown[cpu] > 0) {
145 /* We're alive, poke the watchdog */
146 cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
147 per_cpu_countdown[cpu]--;
148 } else {
149 /* Bad news, you are about to reboot. */
150 disable_irq_nosync(cpl);
151 cpumask_clear_cpu(cpu, &irq_enabled_cpus);
152 }
153 } else {
154 /* Not open, just ping away... */
155 cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
156 }
157 return IRQ_HANDLED;
158}
159
160/* From setup.c */
161extern int prom_putchar(char c);
162
163/**
164 * Write a string to the uart
165 *
166 * @str: String to write
167 */
168static void octeon_wdt_write_string(const char *str)
169{
170 /* Just loop writing one byte at a time */
171 while (*str)
172 prom_putchar(*str++);
173}
174
175/**
176 * Write a hex number out of the uart
177 *
178 * @value: Number to display
179 * @digits: Number of digits to print (1 to 16)
180 */
181static void octeon_wdt_write_hex(u64 value, int digits)
182{
183 int d;
184 int v;
Aaro Koskinen8692cf02015-03-28 20:05:39 +0200185
David Daney4c076fb2010-07-24 10:16:05 -0700186 for (d = 0; d < digits; d++) {
187 v = (value >> ((digits - d - 1) * 4)) & 0xf;
188 if (v >= 10)
189 prom_putchar('a' + v - 10);
190 else
191 prom_putchar('0' + v);
192 }
193}
194
Aaro Koskinen3a30c072015-03-28 20:05:40 +0200195static const char reg_name[][3] = {
David Daney4c076fb2010-07-24 10:16:05 -0700196 "$0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
197 "a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3",
198 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
199 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"
200};
201
202/**
203 * NMI stage 3 handler. NMIs are handled in the following manner:
204 * 1) The first NMI handler enables CVMSEG and transfers from
205 * the bootbus region into normal memory. It is careful to not
206 * destroy any registers.
207 * 2) The second stage handler uses CVMSEG to save the registers
208 * and create a stack for C code. It then calls the third level
209 * handler with one argument, a pointer to the register values.
210 * 3) The third, and final, level handler is the following C
211 * function that prints out some useful infomration.
212 *
213 * @reg: Pointer to register state before the NMI
214 */
215void octeon_wdt_nmi_stage3(u64 reg[32])
216{
217 u64 i;
218
219 unsigned int coreid = cvmx_get_core_num();
220 /*
221 * Save status and cause early to get them before any changes
222 * might happen.
223 */
224 u64 cp0_cause = read_c0_cause();
225 u64 cp0_status = read_c0_status();
226 u64 cp0_error_epc = read_c0_errorepc();
227 u64 cp0_epc = read_c0_epc();
228
229 /* Delay so output from all cores output is not jumbled together. */
230 __delay(100000000ull * coreid);
231
232 octeon_wdt_write_string("\r\n*** NMI Watchdog interrupt on Core 0x");
233 octeon_wdt_write_hex(coreid, 1);
234 octeon_wdt_write_string(" ***\r\n");
235 for (i = 0; i < 32; i++) {
236 octeon_wdt_write_string("\t");
237 octeon_wdt_write_string(reg_name[i]);
238 octeon_wdt_write_string("\t0x");
239 octeon_wdt_write_hex(reg[i], 16);
240 if (i & 1)
241 octeon_wdt_write_string("\r\n");
242 }
243 octeon_wdt_write_string("\terr_epc\t0x");
244 octeon_wdt_write_hex(cp0_error_epc, 16);
245
246 octeon_wdt_write_string("\tepc\t0x");
247 octeon_wdt_write_hex(cp0_epc, 16);
248 octeon_wdt_write_string("\r\n");
249
250 octeon_wdt_write_string("\tstatus\t0x");
251 octeon_wdt_write_hex(cp0_status, 16);
252 octeon_wdt_write_string("\tcause\t0x");
253 octeon_wdt_write_hex(cp0_cause, 16);
254 octeon_wdt_write_string("\r\n");
255
256 octeon_wdt_write_string("\tsum0\t0x");
257 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU_INTX_SUM0(coreid * 2)), 16);
258 octeon_wdt_write_string("\ten0\t0x");
259 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid * 2)), 16);
260 octeon_wdt_write_string("\r\n");
261
262 octeon_wdt_write_string("*** Chip soft reset soon ***\r\n");
263}
264
Sebastian Andrzej Siewior948b9c62016-11-17 19:35:32 +0100265static int octeon_wdt_cpu_pre_down(unsigned int cpu)
David Daney4c076fb2010-07-24 10:16:05 -0700266{
267 unsigned int core;
268 unsigned int irq;
269 union cvmx_ciu_wdogx ciu_wdog;
270
271 core = cpu2core(cpu);
272
273 irq = OCTEON_IRQ_WDOG0 + core;
274
275 /* Poke the watchdog to clear out its state */
276 cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
277
278 /* Disable the hardware. */
279 ciu_wdog.u64 = 0;
280 cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
281
282 free_irq(irq, octeon_wdt_poke_irq);
Sebastian Andrzej Siewior948b9c62016-11-17 19:35:32 +0100283 return 0;
David Daney4c076fb2010-07-24 10:16:05 -0700284}
285
Sebastian Andrzej Siewior948b9c62016-11-17 19:35:32 +0100286static int octeon_wdt_cpu_online(unsigned int cpu)
David Daney4c076fb2010-07-24 10:16:05 -0700287{
288 unsigned int core;
289 unsigned int irq;
290 union cvmx_ciu_wdogx ciu_wdog;
291
292 core = cpu2core(cpu);
293
Steven J. Hill49d148b2017-08-29 10:40:32 -0500294 octeon_wdt_bootvector[core].target_ptr = (u64)octeon_wdt_nmi_stage2;
295
David Daney4c076fb2010-07-24 10:16:05 -0700296 /* Disable it before doing anything with the interrupts. */
297 ciu_wdog.u64 = 0;
298 cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
299
300 per_cpu_countdown[cpu] = countdown_reset;
301
302 irq = OCTEON_IRQ_WDOG0 + core;
303
304 if (request_irq(irq, octeon_wdt_poke_irq,
Venkat Subbiah47bfd052011-10-03 17:22:04 -0700305 IRQF_NO_THREAD, "octeon_wdt", octeon_wdt_poke_irq))
David Daney4c076fb2010-07-24 10:16:05 -0700306 panic("octeon_wdt: Couldn't obtain irq %d", irq);
307
308 cpumask_set_cpu(cpu, &irq_enabled_cpus);
309
310 /* Poke the watchdog to clear out its state */
311 cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
312
313 /* Finally enable the watchdog now that all handlers are installed */
314 ciu_wdog.u64 = 0;
315 ciu_wdog.s.len = timeout_cnt;
316 ciu_wdog.s.mode = 3; /* 3 = Interrupt + NMI + Soft-Reset */
317 cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
David Daney4c076fb2010-07-24 10:16:05 -0700318
Sebastian Andrzej Siewior948b9c62016-11-17 19:35:32 +0100319 return 0;
David Daney4c076fb2010-07-24 10:16:05 -0700320}
321
Aaro Koskinen3d588c92015-03-28 20:05:38 +0200322static int octeon_wdt_ping(struct watchdog_device __always_unused *wdog)
David Daney4c076fb2010-07-24 10:16:05 -0700323{
324 int cpu;
325 int coreid;
326
327 for_each_online_cpu(cpu) {
328 coreid = cpu2core(cpu);
329 cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
330 per_cpu_countdown[cpu] = countdown_reset;
331 if ((countdown_reset || !do_coundown) &&
332 !cpumask_test_cpu(cpu, &irq_enabled_cpus)) {
333 /* We have to enable the irq */
334 int irq = OCTEON_IRQ_WDOG0 + coreid;
Aaro Koskinen8692cf02015-03-28 20:05:39 +0200335
David Daney4c076fb2010-07-24 10:16:05 -0700336 enable_irq(irq);
337 cpumask_set_cpu(cpu, &irq_enabled_cpus);
338 }
339 }
Aaro Koskinen3d588c92015-03-28 20:05:38 +0200340 return 0;
David Daney4c076fb2010-07-24 10:16:05 -0700341}
342
343static void octeon_wdt_calc_parameters(int t)
344{
345 unsigned int periods;
346
347 timeout_sec = max_timeout_sec;
348
349
350 /*
351 * Find the largest interrupt period, that can evenly divide
352 * the requested heartbeat time.
353 */
354 while ((t % timeout_sec) != 0)
355 timeout_sec--;
356
357 periods = t / timeout_sec;
358
359 /*
360 * The last two periods are after the irq is disabled, and
361 * then to the nmi, so we subtract them off.
362 */
363
364 countdown_reset = periods > 2 ? periods - 2 : 0;
365 heartbeat = t;
David Daney468ffde2010-10-07 16:03:52 -0700366 timeout_cnt = ((octeon_get_io_clock_rate() >> 8) * timeout_sec) >> 8;
David Daney4c076fb2010-07-24 10:16:05 -0700367}
368
Aaro Koskinen3d588c92015-03-28 20:05:38 +0200369static int octeon_wdt_set_timeout(struct watchdog_device *wdog,
370 unsigned int t)
David Daney4c076fb2010-07-24 10:16:05 -0700371{
372 int cpu;
373 int coreid;
374 union cvmx_ciu_wdogx ciu_wdog;
375
376 if (t <= 0)
377 return -1;
378
379 octeon_wdt_calc_parameters(t);
380
381 for_each_online_cpu(cpu) {
382 coreid = cpu2core(cpu);
383 cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
384 ciu_wdog.u64 = 0;
385 ciu_wdog.s.len = timeout_cnt;
386 ciu_wdog.s.mode = 3; /* 3 = Interrupt + NMI + Soft-Reset */
387 cvmx_write_csr(CVMX_CIU_WDOGX(coreid), ciu_wdog.u64);
388 cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
389 }
Aaro Koskinen3d588c92015-03-28 20:05:38 +0200390 octeon_wdt_ping(wdog); /* Get the irqs back on. */
David Daney4c076fb2010-07-24 10:16:05 -0700391 return 0;
392}
393
Aaro Koskinen3d588c92015-03-28 20:05:38 +0200394static int octeon_wdt_start(struct watchdog_device *wdog)
David Daney4c076fb2010-07-24 10:16:05 -0700395{
Aaro Koskinen3d588c92015-03-28 20:05:38 +0200396 octeon_wdt_ping(wdog);
David Daney4c076fb2010-07-24 10:16:05 -0700397 do_coundown = 1;
David Daney4c076fb2010-07-24 10:16:05 -0700398 return 0;
399}
400
Aaro Koskinen3d588c92015-03-28 20:05:38 +0200401static int octeon_wdt_stop(struct watchdog_device *wdog)
402{
403 do_coundown = 0;
404 octeon_wdt_ping(wdog);
405 return 0;
406}
David Daney4c076fb2010-07-24 10:16:05 -0700407
Aaro Koskinen3d588c92015-03-28 20:05:38 +0200408static const struct watchdog_info octeon_wdt_info = {
409 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
410 .identity = "OCTEON",
411};
412
413static const struct watchdog_ops octeon_wdt_ops = {
414 .owner = THIS_MODULE,
415 .start = octeon_wdt_start,
416 .stop = octeon_wdt_stop,
417 .ping = octeon_wdt_ping,
418 .set_timeout = octeon_wdt_set_timeout,
419};
420
421static struct watchdog_device octeon_wdt = {
422 .info = &octeon_wdt_info,
423 .ops = &octeon_wdt_ops,
424};
David Daney4c076fb2010-07-24 10:16:05 -0700425
Sebastian Andrzej Siewior948b9c62016-11-17 19:35:32 +0100426static enum cpuhp_state octeon_wdt_online;
David Daney4c076fb2010-07-24 10:16:05 -0700427/**
428 * Module/ driver initialization.
429 *
430 * Returns Zero on success
431 */
432static int __init octeon_wdt_init(void)
433{
434 int i;
435 int ret;
David Daney4c076fb2010-07-24 10:16:05 -0700436 u64 *ptr;
437
Steven J. Hill49d148b2017-08-29 10:40:32 -0500438 octeon_wdt_bootvector = cvmx_boot_vector_get();
439 if (!octeon_wdt_bootvector) {
440 pr_err("Error: Cannot allocate boot vector.\n");
441 return -ENOMEM;
442 }
443
David Daney4c076fb2010-07-24 10:16:05 -0700444 /*
445 * Watchdog time expiration length = The 16 bits of LEN
446 * represent the most significant bits of a 24 bit decrementer
447 * that decrements every 256 cycles.
448 *
449 * Try for a timeout of 5 sec, if that fails a smaller number
450 * of even seconds,
451 */
452 max_timeout_sec = 6;
453 do {
454 max_timeout_sec--;
Aaro Koskinen8692cf02015-03-28 20:05:39 +0200455 timeout_cnt = ((octeon_get_io_clock_rate() >> 8) *
456 max_timeout_sec) >> 8;
David Daney4c076fb2010-07-24 10:16:05 -0700457 } while (timeout_cnt > 65535);
458
459 BUG_ON(timeout_cnt == 0);
460
461 octeon_wdt_calc_parameters(heartbeat);
462
Joe Perches27c766a2012-02-15 15:06:19 -0800463 pr_info("Initial granularity %d Sec\n", timeout_sec);
David Daney4c076fb2010-07-24 10:16:05 -0700464
Aaro Koskinen3d588c92015-03-28 20:05:38 +0200465 octeon_wdt.timeout = timeout_sec;
466 octeon_wdt.max_timeout = UINT_MAX;
467
468 watchdog_set_nowayout(&octeon_wdt, nowayout);
469
470 ret = watchdog_register_device(&octeon_wdt);
David Daney4c076fb2010-07-24 10:16:05 -0700471 if (ret) {
Aaro Koskinen3d588c92015-03-28 20:05:38 +0200472 pr_err("watchdog_register_device() failed: %d\n", ret);
473 return ret;
David Daney4c076fb2010-07-24 10:16:05 -0700474 }
475
David Daney4c076fb2010-07-24 10:16:05 -0700476 cpumask_clear(&irq_enabled_cpus);
477
Sebastian Andrzej Siewior948b9c62016-11-17 19:35:32 +0100478 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "watchdog/octeon:online",
479 octeon_wdt_cpu_online, octeon_wdt_cpu_pre_down);
480 if (ret < 0)
481 goto err;
482 octeon_wdt_online = ret;
Aaro Koskinen3d588c92015-03-28 20:05:38 +0200483 return 0;
Sebastian Andrzej Siewior948b9c62016-11-17 19:35:32 +0100484err:
485 cvmx_write_csr(CVMX_MIO_BOOT_LOC_CFGX(0), 0);
486 watchdog_unregister_device(&octeon_wdt);
487 return ret;
David Daney4c076fb2010-07-24 10:16:05 -0700488}
489
490/**
491 * Module / driver shutdown
492 */
493static void __exit octeon_wdt_cleanup(void)
494{
Aaro Koskinen3d588c92015-03-28 20:05:38 +0200495 watchdog_unregister_device(&octeon_wdt);
Sebastian Andrzej Siewior948b9c62016-11-17 19:35:32 +0100496 cpuhp_remove_state(octeon_wdt_online);
Srivatsa S. Bhat99c3bf32014-03-11 02:10:43 +0530497
David Daney4c076fb2010-07-24 10:16:05 -0700498 /*
499 * Disable the boot-bus memory, the code it points to is soon
500 * to go missing.
501 */
502 cvmx_write_csr(CVMX_MIO_BOOT_LOC_CFGX(0), 0);
503}
504
505MODULE_LICENSE("GPL");
506MODULE_AUTHOR("Cavium Networks <support@caviumnetworks.com>");
507MODULE_DESCRIPTION("Cavium Networks Octeon Watchdog driver.");
508module_init(octeon_wdt_init);
509module_exit(octeon_wdt_cleanup);