1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Octeon Watchdog driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2007-2017 Cavium, Inc.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Converted to use WATCHDOG_CORE by Aaro Koskinen <aaro.koskinen@iki.fi>.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Some parts derived from wdt.c
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
12*4882a593Smuzhiyun * All Rights Reserved.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
15*4882a593Smuzhiyun * warranty for any of this software. This material is provided
16*4882a593Smuzhiyun * "AS-IS" and at no charge.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * The OCTEON watchdog has a maximum timeout of 2^32 * io_clock.
21*4882a593Smuzhiyun * For most systems this is less than 10 seconds, so to allow for
22*4882a593Smuzhiyun * software to request longer watchdog heartbeats, we maintain software
23*4882a593Smuzhiyun * counters to count multiples of the base rate. If the system locks
24*4882a593Smuzhiyun * up in such a manner that we can not run the software counters, the
25*4882a593Smuzhiyun * only result is a watchdog reset sooner than was requested. But
26*4882a593Smuzhiyun * that is OK, because in this case userspace would likely not be able
27*4882a593Smuzhiyun * to do anything anyhow.
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * The hardware watchdog interval we call the period. The OCTEON
30*4882a593Smuzhiyun * watchdog goes through several stages, after the first period an
31*4882a593Smuzhiyun * irq is asserted, then if it is not reset, after the next period NMI
32*4882a593Smuzhiyun * is asserted, then after an additional period a chip wide soft reset.
33*4882a593Smuzhiyun * So for the software counters, we reset watchdog after each period
34*4882a593Smuzhiyun * and decrement the counter. But for the last two periods we need to
35*4882a593Smuzhiyun * let the watchdog progress to the NMI stage so we disable the irq
36*4882a593Smuzhiyun * and let it proceed. Once in the NMI, we print the register state
37*4882a593Smuzhiyun * to the serial port and then wait for the reset.
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun * A watchdog is maintained for each CPU in the system, that way if
40*4882a593Smuzhiyun * one CPU suffers a lockup, we also get a register dump and reset.
41*4882a593Smuzhiyun * The userspace ping resets the watchdog on all CPUs.
42*4882a593Smuzhiyun *
43*4882a593Smuzhiyun * Before userspace opens the watchdog device, we still run the
44*4882a593Smuzhiyun * watchdogs to catch any lockups that may be kernel related.
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun */
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun #include <linux/interrupt.h>
51*4882a593Smuzhiyun #include <linux/watchdog.h>
52*4882a593Smuzhiyun #include <linux/cpumask.h>
53*4882a593Smuzhiyun #include <linux/module.h>
54*4882a593Smuzhiyun #include <linux/delay.h>
55*4882a593Smuzhiyun #include <linux/cpu.h>
56*4882a593Smuzhiyun #include <linux/irq.h>
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun #include <asm/mipsregs.h>
59*4882a593Smuzhiyun #include <asm/uasm.h>
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #include <asm/octeon/octeon.h>
62*4882a593Smuzhiyun #include <asm/octeon/cvmx-boot-vector.h>
63*4882a593Smuzhiyun #include <asm/octeon/cvmx-ciu2-defs.h>
64*4882a593Smuzhiyun #include <asm/octeon/cvmx-rst-defs.h>
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /* Watchdog interrupt major block number (8 MSBs of intsn) */
67*4882a593Smuzhiyun #define WD_BLOCK_NUMBER 0x01
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun static int divisor;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /* The count needed to achieve timeout_sec. */
72*4882a593Smuzhiyun static unsigned int timeout_cnt;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* The maximum period supported. */
75*4882a593Smuzhiyun static unsigned int max_timeout_sec;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /* The current period. */
78*4882a593Smuzhiyun static unsigned int timeout_sec;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /* Set to non-zero when userspace countdown mode active */
81*4882a593Smuzhiyun static bool do_countdown;
82*4882a593Smuzhiyun static unsigned int countdown_reset;
83*4882a593Smuzhiyun static unsigned int per_cpu_countdown[NR_CPUS];
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun static cpumask_t irq_enabled_cpus;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun #define WD_TIMO 60 /* Default heartbeat = 60 seconds */
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun #define CVMX_GSERX_SCRATCH(offset) (CVMX_ADD_IO_SEG(0x0001180090000020ull) + ((offset) & 15) * 0x1000000ull)
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun static int heartbeat = WD_TIMO;
92*4882a593Smuzhiyun module_param(heartbeat, int, 0444);
93*4882a593Smuzhiyun MODULE_PARM_DESC(heartbeat,
94*4882a593Smuzhiyun "Watchdog heartbeat in seconds. (0 < heartbeat, default="
95*4882a593Smuzhiyun __MODULE_STRING(WD_TIMO) ")");
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
98*4882a593Smuzhiyun module_param(nowayout, bool, 0444);
99*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout,
100*4882a593Smuzhiyun "Watchdog cannot be stopped once started (default="
101*4882a593Smuzhiyun __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun static int disable;
104*4882a593Smuzhiyun module_param(disable, int, 0444);
105*4882a593Smuzhiyun MODULE_PARM_DESC(disable,
106*4882a593Smuzhiyun "Disable the watchdog entirely (default=0)");
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun static struct cvmx_boot_vector_element *octeon_wdt_bootvector;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun void octeon_wdt_nmi_stage2(void);
111*4882a593Smuzhiyun
cpu2core(int cpu)112*4882a593Smuzhiyun static int cpu2core(int cpu)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun #ifdef CONFIG_SMP
115*4882a593Smuzhiyun return cpu_logical_map(cpu) & 0x3f;
116*4882a593Smuzhiyun #else
117*4882a593Smuzhiyun return cvmx_get_core_num();
118*4882a593Smuzhiyun #endif
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /**
122*4882a593Smuzhiyun * Poke the watchdog when an interrupt is received
123*4882a593Smuzhiyun *
124*4882a593Smuzhiyun * @cpl:
125*4882a593Smuzhiyun * @dev_id:
126*4882a593Smuzhiyun *
127*4882a593Smuzhiyun * Returns
128*4882a593Smuzhiyun */
octeon_wdt_poke_irq(int cpl,void * dev_id)129*4882a593Smuzhiyun static irqreturn_t octeon_wdt_poke_irq(int cpl, void *dev_id)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun int cpu = raw_smp_processor_id();
132*4882a593Smuzhiyun unsigned int core = cpu2core(cpu);
133*4882a593Smuzhiyun int node = cpu_to_node(cpu);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun if (do_countdown) {
136*4882a593Smuzhiyun if (per_cpu_countdown[cpu] > 0) {
137*4882a593Smuzhiyun /* We're alive, poke the watchdog */
138*4882a593Smuzhiyun cvmx_write_csr_node(node, CVMX_CIU_PP_POKEX(core), 1);
139*4882a593Smuzhiyun per_cpu_countdown[cpu]--;
140*4882a593Smuzhiyun } else {
141*4882a593Smuzhiyun /* Bad news, you are about to reboot. */
142*4882a593Smuzhiyun disable_irq_nosync(cpl);
143*4882a593Smuzhiyun cpumask_clear_cpu(cpu, &irq_enabled_cpus);
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun } else {
146*4882a593Smuzhiyun /* Not open, just ping away... */
147*4882a593Smuzhiyun cvmx_write_csr_node(node, CVMX_CIU_PP_POKEX(core), 1);
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun return IRQ_HANDLED;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /* From setup.c */
153*4882a593Smuzhiyun extern int prom_putchar(char c);
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /**
156*4882a593Smuzhiyun * Write a string to the uart
157*4882a593Smuzhiyun *
158*4882a593Smuzhiyun * @str: String to write
159*4882a593Smuzhiyun */
octeon_wdt_write_string(const char * str)160*4882a593Smuzhiyun static void octeon_wdt_write_string(const char *str)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun /* Just loop writing one byte at a time */
163*4882a593Smuzhiyun while (*str)
164*4882a593Smuzhiyun prom_putchar(*str++);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /**
168*4882a593Smuzhiyun * Write a hex number out of the uart
169*4882a593Smuzhiyun *
170*4882a593Smuzhiyun * @value: Number to display
171*4882a593Smuzhiyun * @digits: Number of digits to print (1 to 16)
172*4882a593Smuzhiyun */
octeon_wdt_write_hex(u64 value,int digits)173*4882a593Smuzhiyun static void octeon_wdt_write_hex(u64 value, int digits)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun int d;
176*4882a593Smuzhiyun int v;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun for (d = 0; d < digits; d++) {
179*4882a593Smuzhiyun v = (value >> ((digits - d - 1) * 4)) & 0xf;
180*4882a593Smuzhiyun if (v >= 10)
181*4882a593Smuzhiyun prom_putchar('a' + v - 10);
182*4882a593Smuzhiyun else
183*4882a593Smuzhiyun prom_putchar('0' + v);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun static const char reg_name[][3] = {
188*4882a593Smuzhiyun "$0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
189*4882a593Smuzhiyun "a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3",
190*4882a593Smuzhiyun "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
191*4882a593Smuzhiyun "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"
192*4882a593Smuzhiyun };
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /**
195*4882a593Smuzhiyun * NMI stage 3 handler. NMIs are handled in the following manner:
196*4882a593Smuzhiyun * 1) The first NMI handler enables CVMSEG and transfers from
197*4882a593Smuzhiyun * the bootbus region into normal memory. It is careful to not
198*4882a593Smuzhiyun * destroy any registers.
199*4882a593Smuzhiyun * 2) The second stage handler uses CVMSEG to save the registers
200*4882a593Smuzhiyun * and create a stack for C code. It then calls the third level
201*4882a593Smuzhiyun * handler with one argument, a pointer to the register values.
202*4882a593Smuzhiyun * 3) The third, and final, level handler is the following C
203*4882a593Smuzhiyun * function that prints out some useful infomration.
204*4882a593Smuzhiyun *
205*4882a593Smuzhiyun * @reg: Pointer to register state before the NMI
206*4882a593Smuzhiyun */
octeon_wdt_nmi_stage3(u64 reg[32])207*4882a593Smuzhiyun void octeon_wdt_nmi_stage3(u64 reg[32])
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun u64 i;
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun unsigned int coreid = cvmx_get_core_num();
212*4882a593Smuzhiyun /*
213*4882a593Smuzhiyun * Save status and cause early to get them before any changes
214*4882a593Smuzhiyun * might happen.
215*4882a593Smuzhiyun */
216*4882a593Smuzhiyun u64 cp0_cause = read_c0_cause();
217*4882a593Smuzhiyun u64 cp0_status = read_c0_status();
218*4882a593Smuzhiyun u64 cp0_error_epc = read_c0_errorepc();
219*4882a593Smuzhiyun u64 cp0_epc = read_c0_epc();
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /* Delay so output from all cores output is not jumbled together. */
222*4882a593Smuzhiyun udelay(85000 * coreid);
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun octeon_wdt_write_string("\r\n*** NMI Watchdog interrupt on Core 0x");
225*4882a593Smuzhiyun octeon_wdt_write_hex(coreid, 2);
226*4882a593Smuzhiyun octeon_wdt_write_string(" ***\r\n");
227*4882a593Smuzhiyun for (i = 0; i < 32; i++) {
228*4882a593Smuzhiyun octeon_wdt_write_string("\t");
229*4882a593Smuzhiyun octeon_wdt_write_string(reg_name[i]);
230*4882a593Smuzhiyun octeon_wdt_write_string("\t0x");
231*4882a593Smuzhiyun octeon_wdt_write_hex(reg[i], 16);
232*4882a593Smuzhiyun if (i & 1)
233*4882a593Smuzhiyun octeon_wdt_write_string("\r\n");
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun octeon_wdt_write_string("\terr_epc\t0x");
236*4882a593Smuzhiyun octeon_wdt_write_hex(cp0_error_epc, 16);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun octeon_wdt_write_string("\tepc\t0x");
239*4882a593Smuzhiyun octeon_wdt_write_hex(cp0_epc, 16);
240*4882a593Smuzhiyun octeon_wdt_write_string("\r\n");
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun octeon_wdt_write_string("\tstatus\t0x");
243*4882a593Smuzhiyun octeon_wdt_write_hex(cp0_status, 16);
244*4882a593Smuzhiyun octeon_wdt_write_string("\tcause\t0x");
245*4882a593Smuzhiyun octeon_wdt_write_hex(cp0_cause, 16);
246*4882a593Smuzhiyun octeon_wdt_write_string("\r\n");
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /* The CIU register is different for each Octeon model. */
249*4882a593Smuzhiyun if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
250*4882a593Smuzhiyun octeon_wdt_write_string("\tsrc_wd\t0x");
251*4882a593Smuzhiyun octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_SRC_PPX_IP2_WDOG(coreid)), 16);
252*4882a593Smuzhiyun octeon_wdt_write_string("\ten_wd\t0x");
253*4882a593Smuzhiyun octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_EN_PPX_IP2_WDOG(coreid)), 16);
254*4882a593Smuzhiyun octeon_wdt_write_string("\r\n");
255*4882a593Smuzhiyun octeon_wdt_write_string("\tsrc_rml\t0x");
256*4882a593Smuzhiyun octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_SRC_PPX_IP2_RML(coreid)), 16);
257*4882a593Smuzhiyun octeon_wdt_write_string("\ten_rml\t0x");
258*4882a593Smuzhiyun octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_EN_PPX_IP2_RML(coreid)), 16);
259*4882a593Smuzhiyun octeon_wdt_write_string("\r\n");
260*4882a593Smuzhiyun octeon_wdt_write_string("\tsum\t0x");
261*4882a593Smuzhiyun octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_SUM_PPX_IP2(coreid)), 16);
262*4882a593Smuzhiyun octeon_wdt_write_string("\r\n");
263*4882a593Smuzhiyun } else if (!octeon_has_feature(OCTEON_FEATURE_CIU3)) {
264*4882a593Smuzhiyun octeon_wdt_write_string("\tsum0\t0x");
265*4882a593Smuzhiyun octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU_INTX_SUM0(coreid * 2)), 16);
266*4882a593Smuzhiyun octeon_wdt_write_string("\ten0\t0x");
267*4882a593Smuzhiyun octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid * 2)), 16);
268*4882a593Smuzhiyun octeon_wdt_write_string("\r\n");
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun octeon_wdt_write_string("*** Chip soft reset soon ***\r\n");
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /*
274*4882a593Smuzhiyun * G-30204: We must trigger a soft reset before watchdog
275*4882a593Smuzhiyun * does an incomplete job of doing it.
276*4882a593Smuzhiyun */
277*4882a593Smuzhiyun if (OCTEON_IS_OCTEON3() && !OCTEON_IS_MODEL(OCTEON_CN70XX)) {
278*4882a593Smuzhiyun u64 scr;
279*4882a593Smuzhiyun unsigned int node = cvmx_get_node_num();
280*4882a593Smuzhiyun unsigned int lcore = cvmx_get_local_core_num();
281*4882a593Smuzhiyun union cvmx_ciu_wdogx ciu_wdog;
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun /*
284*4882a593Smuzhiyun * Wait for other cores to print out information, but
285*4882a593Smuzhiyun * not too long. Do the soft reset before watchdog
286*4882a593Smuzhiyun * can trigger it.
287*4882a593Smuzhiyun */
288*4882a593Smuzhiyun do {
289*4882a593Smuzhiyun ciu_wdog.u64 = cvmx_read_csr_node(node, CVMX_CIU_WDOGX(lcore));
290*4882a593Smuzhiyun } while (ciu_wdog.s.cnt > 0x10000);
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun scr = cvmx_read_csr_node(0, CVMX_GSERX_SCRATCH(0));
293*4882a593Smuzhiyun scr |= 1 << 11; /* Indicate watchdog in bit 11 */
294*4882a593Smuzhiyun cvmx_write_csr_node(0, CVMX_GSERX_SCRATCH(0), scr);
295*4882a593Smuzhiyun cvmx_write_csr_node(0, CVMX_RST_SOFT_RST, 1);
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun
octeon_wdt_cpu_to_irq(int cpu)299*4882a593Smuzhiyun static int octeon_wdt_cpu_to_irq(int cpu)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun unsigned int coreid;
302*4882a593Smuzhiyun int node;
303*4882a593Smuzhiyun int irq;
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun coreid = cpu2core(cpu);
306*4882a593Smuzhiyun node = cpu_to_node(cpu);
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun if (octeon_has_feature(OCTEON_FEATURE_CIU3)) {
309*4882a593Smuzhiyun struct irq_domain *domain;
310*4882a593Smuzhiyun int hwirq;
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun domain = octeon_irq_get_block_domain(node,
313*4882a593Smuzhiyun WD_BLOCK_NUMBER);
314*4882a593Smuzhiyun hwirq = WD_BLOCK_NUMBER << 12 | 0x200 | coreid;
315*4882a593Smuzhiyun irq = irq_find_mapping(domain, hwirq);
316*4882a593Smuzhiyun } else {
317*4882a593Smuzhiyun irq = OCTEON_IRQ_WDOG0 + coreid;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun return irq;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
octeon_wdt_cpu_pre_down(unsigned int cpu)322*4882a593Smuzhiyun static int octeon_wdt_cpu_pre_down(unsigned int cpu)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun unsigned int core;
325*4882a593Smuzhiyun int node;
326*4882a593Smuzhiyun union cvmx_ciu_wdogx ciu_wdog;
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun core = cpu2core(cpu);
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun node = cpu_to_node(cpu);
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun /* Poke the watchdog to clear out its state */
333*4882a593Smuzhiyun cvmx_write_csr_node(node, CVMX_CIU_PP_POKEX(core), 1);
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /* Disable the hardware. */
336*4882a593Smuzhiyun ciu_wdog.u64 = 0;
337*4882a593Smuzhiyun cvmx_write_csr_node(node, CVMX_CIU_WDOGX(core), ciu_wdog.u64);
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun free_irq(octeon_wdt_cpu_to_irq(cpu), octeon_wdt_poke_irq);
340*4882a593Smuzhiyun return 0;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun
octeon_wdt_cpu_online(unsigned int cpu)343*4882a593Smuzhiyun static int octeon_wdt_cpu_online(unsigned int cpu)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun unsigned int core;
346*4882a593Smuzhiyun unsigned int irq;
347*4882a593Smuzhiyun union cvmx_ciu_wdogx ciu_wdog;
348*4882a593Smuzhiyun int node;
349*4882a593Smuzhiyun struct irq_domain *domain;
350*4882a593Smuzhiyun int hwirq;
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun core = cpu2core(cpu);
353*4882a593Smuzhiyun node = cpu_to_node(cpu);
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun octeon_wdt_bootvector[core].target_ptr = (u64)octeon_wdt_nmi_stage2;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun /* Disable it before doing anything with the interrupts. */
358*4882a593Smuzhiyun ciu_wdog.u64 = 0;
359*4882a593Smuzhiyun cvmx_write_csr_node(node, CVMX_CIU_WDOGX(core), ciu_wdog.u64);
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun per_cpu_countdown[cpu] = countdown_reset;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun if (octeon_has_feature(OCTEON_FEATURE_CIU3)) {
364*4882a593Smuzhiyun /* Must get the domain for the watchdog block */
365*4882a593Smuzhiyun domain = octeon_irq_get_block_domain(node, WD_BLOCK_NUMBER);
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun /* Get a irq for the wd intsn (hardware interrupt) */
368*4882a593Smuzhiyun hwirq = WD_BLOCK_NUMBER << 12 | 0x200 | core;
369*4882a593Smuzhiyun irq = irq_create_mapping(domain, hwirq);
370*4882a593Smuzhiyun irqd_set_trigger_type(irq_get_irq_data(irq),
371*4882a593Smuzhiyun IRQ_TYPE_EDGE_RISING);
372*4882a593Smuzhiyun } else
373*4882a593Smuzhiyun irq = OCTEON_IRQ_WDOG0 + core;
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun if (request_irq(irq, octeon_wdt_poke_irq,
376*4882a593Smuzhiyun IRQF_NO_THREAD, "octeon_wdt", octeon_wdt_poke_irq))
377*4882a593Smuzhiyun panic("octeon_wdt: Couldn't obtain irq %d", irq);
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun /* Must set the irq affinity here */
380*4882a593Smuzhiyun if (octeon_has_feature(OCTEON_FEATURE_CIU3)) {
381*4882a593Smuzhiyun cpumask_t mask;
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun cpumask_clear(&mask);
384*4882a593Smuzhiyun cpumask_set_cpu(cpu, &mask);
385*4882a593Smuzhiyun irq_set_affinity(irq, &mask);
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun cpumask_set_cpu(cpu, &irq_enabled_cpus);
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun /* Poke the watchdog to clear out its state */
391*4882a593Smuzhiyun cvmx_write_csr_node(node, CVMX_CIU_PP_POKEX(core), 1);
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun /* Finally enable the watchdog now that all handlers are installed */
394*4882a593Smuzhiyun ciu_wdog.u64 = 0;
395*4882a593Smuzhiyun ciu_wdog.s.len = timeout_cnt;
396*4882a593Smuzhiyun ciu_wdog.s.mode = 3; /* 3 = Interrupt + NMI + Soft-Reset */
397*4882a593Smuzhiyun cvmx_write_csr_node(node, CVMX_CIU_WDOGX(core), ciu_wdog.u64);
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun return 0;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun
octeon_wdt_ping(struct watchdog_device __always_unused * wdog)402*4882a593Smuzhiyun static int octeon_wdt_ping(struct watchdog_device __always_unused *wdog)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun int cpu;
405*4882a593Smuzhiyun int coreid;
406*4882a593Smuzhiyun int node;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun if (disable)
409*4882a593Smuzhiyun return 0;
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun for_each_online_cpu(cpu) {
412*4882a593Smuzhiyun coreid = cpu2core(cpu);
413*4882a593Smuzhiyun node = cpu_to_node(cpu);
414*4882a593Smuzhiyun cvmx_write_csr_node(node, CVMX_CIU_PP_POKEX(coreid), 1);
415*4882a593Smuzhiyun per_cpu_countdown[cpu] = countdown_reset;
416*4882a593Smuzhiyun if ((countdown_reset || !do_countdown) &&
417*4882a593Smuzhiyun !cpumask_test_cpu(cpu, &irq_enabled_cpus)) {
418*4882a593Smuzhiyun /* We have to enable the irq */
419*4882a593Smuzhiyun enable_irq(octeon_wdt_cpu_to_irq(cpu));
420*4882a593Smuzhiyun cpumask_set_cpu(cpu, &irq_enabled_cpus);
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun return 0;
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun
octeon_wdt_calc_parameters(int t)426*4882a593Smuzhiyun static void octeon_wdt_calc_parameters(int t)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun unsigned int periods;
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun timeout_sec = max_timeout_sec;
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun /*
434*4882a593Smuzhiyun * Find the largest interrupt period, that can evenly divide
435*4882a593Smuzhiyun * the requested heartbeat time.
436*4882a593Smuzhiyun */
437*4882a593Smuzhiyun while ((t % timeout_sec) != 0)
438*4882a593Smuzhiyun timeout_sec--;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun periods = t / timeout_sec;
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun /*
443*4882a593Smuzhiyun * The last two periods are after the irq is disabled, and
444*4882a593Smuzhiyun * then to the nmi, so we subtract them off.
445*4882a593Smuzhiyun */
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun countdown_reset = periods > 2 ? periods - 2 : 0;
448*4882a593Smuzhiyun heartbeat = t;
449*4882a593Smuzhiyun timeout_cnt = ((octeon_get_io_clock_rate() / divisor) * timeout_sec) >> 8;
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun
octeon_wdt_set_timeout(struct watchdog_device * wdog,unsigned int t)452*4882a593Smuzhiyun static int octeon_wdt_set_timeout(struct watchdog_device *wdog,
453*4882a593Smuzhiyun unsigned int t)
454*4882a593Smuzhiyun {
455*4882a593Smuzhiyun int cpu;
456*4882a593Smuzhiyun int coreid;
457*4882a593Smuzhiyun union cvmx_ciu_wdogx ciu_wdog;
458*4882a593Smuzhiyun int node;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun if (t <= 0)
461*4882a593Smuzhiyun return -1;
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun octeon_wdt_calc_parameters(t);
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun if (disable)
466*4882a593Smuzhiyun return 0;
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun for_each_online_cpu(cpu) {
469*4882a593Smuzhiyun coreid = cpu2core(cpu);
470*4882a593Smuzhiyun node = cpu_to_node(cpu);
471*4882a593Smuzhiyun cvmx_write_csr_node(node, CVMX_CIU_PP_POKEX(coreid), 1);
472*4882a593Smuzhiyun ciu_wdog.u64 = 0;
473*4882a593Smuzhiyun ciu_wdog.s.len = timeout_cnt;
474*4882a593Smuzhiyun ciu_wdog.s.mode = 3; /* 3 = Interrupt + NMI + Soft-Reset */
475*4882a593Smuzhiyun cvmx_write_csr_node(node, CVMX_CIU_WDOGX(coreid), ciu_wdog.u64);
476*4882a593Smuzhiyun cvmx_write_csr_node(node, CVMX_CIU_PP_POKEX(coreid), 1);
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun octeon_wdt_ping(wdog); /* Get the irqs back on. */
479*4882a593Smuzhiyun return 0;
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun
octeon_wdt_start(struct watchdog_device * wdog)482*4882a593Smuzhiyun static int octeon_wdt_start(struct watchdog_device *wdog)
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun octeon_wdt_ping(wdog);
485*4882a593Smuzhiyun do_countdown = 1;
486*4882a593Smuzhiyun return 0;
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun
octeon_wdt_stop(struct watchdog_device * wdog)489*4882a593Smuzhiyun static int octeon_wdt_stop(struct watchdog_device *wdog)
490*4882a593Smuzhiyun {
491*4882a593Smuzhiyun do_countdown = 0;
492*4882a593Smuzhiyun octeon_wdt_ping(wdog);
493*4882a593Smuzhiyun return 0;
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun static const struct watchdog_info octeon_wdt_info = {
497*4882a593Smuzhiyun .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
498*4882a593Smuzhiyun .identity = "OCTEON",
499*4882a593Smuzhiyun };
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun static const struct watchdog_ops octeon_wdt_ops = {
502*4882a593Smuzhiyun .owner = THIS_MODULE,
503*4882a593Smuzhiyun .start = octeon_wdt_start,
504*4882a593Smuzhiyun .stop = octeon_wdt_stop,
505*4882a593Smuzhiyun .ping = octeon_wdt_ping,
506*4882a593Smuzhiyun .set_timeout = octeon_wdt_set_timeout,
507*4882a593Smuzhiyun };
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun static struct watchdog_device octeon_wdt = {
510*4882a593Smuzhiyun .info = &octeon_wdt_info,
511*4882a593Smuzhiyun .ops = &octeon_wdt_ops,
512*4882a593Smuzhiyun };
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun static enum cpuhp_state octeon_wdt_online;
515*4882a593Smuzhiyun /**
516*4882a593Smuzhiyun * Module/ driver initialization.
517*4882a593Smuzhiyun *
518*4882a593Smuzhiyun * Returns Zero on success
519*4882a593Smuzhiyun */
octeon_wdt_init(void)520*4882a593Smuzhiyun static int __init octeon_wdt_init(void)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun int ret;
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun octeon_wdt_bootvector = cvmx_boot_vector_get();
525*4882a593Smuzhiyun if (!octeon_wdt_bootvector) {
526*4882a593Smuzhiyun pr_err("Error: Cannot allocate boot vector.\n");
527*4882a593Smuzhiyun return -ENOMEM;
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun if (OCTEON_IS_MODEL(OCTEON_CN68XX))
531*4882a593Smuzhiyun divisor = 0x200;
532*4882a593Smuzhiyun else if (OCTEON_IS_MODEL(OCTEON_CN78XX))
533*4882a593Smuzhiyun divisor = 0x400;
534*4882a593Smuzhiyun else
535*4882a593Smuzhiyun divisor = 0x100;
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun /*
538*4882a593Smuzhiyun * Watchdog time expiration length = The 16 bits of LEN
539*4882a593Smuzhiyun * represent the most significant bits of a 24 bit decrementer
540*4882a593Smuzhiyun * that decrements every divisor cycle.
541*4882a593Smuzhiyun *
542*4882a593Smuzhiyun * Try for a timeout of 5 sec, if that fails a smaller number
543*4882a593Smuzhiyun * of even seconds,
544*4882a593Smuzhiyun */
545*4882a593Smuzhiyun max_timeout_sec = 6;
546*4882a593Smuzhiyun do {
547*4882a593Smuzhiyun max_timeout_sec--;
548*4882a593Smuzhiyun timeout_cnt = ((octeon_get_io_clock_rate() / divisor) * max_timeout_sec) >> 8;
549*4882a593Smuzhiyun } while (timeout_cnt > 65535);
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun BUG_ON(timeout_cnt == 0);
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun octeon_wdt_calc_parameters(heartbeat);
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun pr_info("Initial granularity %d Sec\n", timeout_sec);
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun octeon_wdt.timeout = timeout_sec;
558*4882a593Smuzhiyun octeon_wdt.max_timeout = UINT_MAX;
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun watchdog_set_nowayout(&octeon_wdt, nowayout);
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun ret = watchdog_register_device(&octeon_wdt);
563*4882a593Smuzhiyun if (ret) {
564*4882a593Smuzhiyun pr_err("watchdog_register_device() failed: %d\n", ret);
565*4882a593Smuzhiyun return ret;
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun if (disable) {
569*4882a593Smuzhiyun pr_notice("disabled\n");
570*4882a593Smuzhiyun return 0;
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun cpumask_clear(&irq_enabled_cpus);
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "watchdog/octeon:online",
576*4882a593Smuzhiyun octeon_wdt_cpu_online, octeon_wdt_cpu_pre_down);
577*4882a593Smuzhiyun if (ret < 0)
578*4882a593Smuzhiyun goto err;
579*4882a593Smuzhiyun octeon_wdt_online = ret;
580*4882a593Smuzhiyun return 0;
581*4882a593Smuzhiyun err:
582*4882a593Smuzhiyun cvmx_write_csr(CVMX_MIO_BOOT_LOC_CFGX(0), 0);
583*4882a593Smuzhiyun watchdog_unregister_device(&octeon_wdt);
584*4882a593Smuzhiyun return ret;
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun
587*4882a593Smuzhiyun /**
588*4882a593Smuzhiyun * Module / driver shutdown
589*4882a593Smuzhiyun */
octeon_wdt_cleanup(void)590*4882a593Smuzhiyun static void __exit octeon_wdt_cleanup(void)
591*4882a593Smuzhiyun {
592*4882a593Smuzhiyun watchdog_unregister_device(&octeon_wdt);
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun if (disable)
595*4882a593Smuzhiyun return;
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun cpuhp_remove_state(octeon_wdt_online);
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun /*
600*4882a593Smuzhiyun * Disable the boot-bus memory, the code it points to is soon
601*4882a593Smuzhiyun * to go missing.
602*4882a593Smuzhiyun */
603*4882a593Smuzhiyun cvmx_write_csr(CVMX_MIO_BOOT_LOC_CFGX(0), 0);
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun MODULE_LICENSE("GPL");
607*4882a593Smuzhiyun MODULE_AUTHOR("Cavium Inc. <support@cavium.com>");
608*4882a593Smuzhiyun MODULE_DESCRIPTION("Cavium Inc. OCTEON Watchdog driver.");
609*4882a593Smuzhiyun module_init(octeon_wdt_init);
610*4882a593Smuzhiyun module_exit(octeon_wdt_cleanup);
611