1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
56
57#include <linux/miscdevice.h>
58#include <linux/interrupt.h>
59#include <linux/watchdog.h>
60#include <linux/cpumask.h>
61#include <linux/bitops.h>
62#include <linux/kernel.h>
63#include <linux/module.h>
64#include <linux/string.h>
65#include <linux/delay.h>
66#include <linux/cpu.h>
67#include <linux/smp.h>
68#include <linux/fs.h>
69#include <linux/irq.h>
70
71#include <asm/mipsregs.h>
72#include <asm/uasm.h>
73
74#include <asm/octeon/octeon.h>
75
76
77static unsigned int timeout_cnt;
78
79
80static unsigned int max_timeout_sec;
81
82
83static unsigned int timeout_sec;
84
85
86static int do_coundown;
87static unsigned int countdown_reset;
88static unsigned int per_cpu_countdown[NR_CPUS];
89
90static cpumask_t irq_enabled_cpus;
91
92#define WD_TIMO 60
93
94static int heartbeat = WD_TIMO;
95module_param(heartbeat, int, S_IRUGO);
96MODULE_PARM_DESC(heartbeat,
97 "Watchdog heartbeat in seconds. (0 < heartbeat, default="
98 __MODULE_STRING(WD_TIMO) ")");
99
100static bool nowayout = WATCHDOG_NOWAYOUT;
101module_param(nowayout, bool, S_IRUGO);
102MODULE_PARM_DESC(nowayout,
103 "Watchdog cannot be stopped once started (default="
104 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
105
106static unsigned long octeon_wdt_is_open;
107static char expect_close;
108
109static u32 __initdata nmi_stage1_insns[64];
110
111static struct uasm_label __initdata labels[5];
112static struct uasm_reloc __initdata relocs[5];
113
114enum lable_id {
115 label_enter_bootloader = 1
116};
117
118
119#define K0 26
120#define C0_CVMMEMCTL 11, 7
121#define C0_STATUS 12, 0
122#define C0_EBASE 15, 1
123#define C0_DESAVE 31, 0
124
125void octeon_wdt_nmi_stage2(void);
126
127static void __init octeon_wdt_build_stage1(void)
128{
129 int i;
130 int len;
131 u32 *p = nmi_stage1_insns;
132#ifdef CONFIG_HOTPLUG_CPU
133 struct uasm_label *l = labels;
134 struct uasm_reloc *r = relocs;
135#endif
136
137
138
139
140
141
142
143
144 uasm_i_dmtc0(&p, K0, C0_DESAVE);
145
146 uasm_i_mfc0(&p, K0, C0_STATUS);
147#ifdef CONFIG_HOTPLUG_CPU
148 uasm_il_bbit0(&p, &r, K0, ilog2(ST0_NMI), label_enter_bootloader);
149#endif
150
151 uasm_i_ori(&p, K0, K0, ST0_UX | ST0_SX | ST0_KX);
152 uasm_i_mtc0(&p, K0, C0_STATUS);
153
154#ifdef CONFIG_HOTPLUG_CPU
155 uasm_i_mfc0(&p, K0, C0_EBASE);
156
157 uasm_i_andi(&p, K0, K0, 0xf);
158
159 uasm_i_dsll_safe(&p, K0, K0, 3 + 16);
160 uasm_i_ori(&p, K0, K0, 0x8001);
161 uasm_i_dsll_safe(&p, K0, K0, 16);
162 uasm_i_ori(&p, K0, K0, 0x0700);
163 uasm_i_drotr_safe(&p, K0, K0, 32);
164
165
166
167
168
169
170 uasm_i_ld(&p, K0, 0x500, K0);
171
172
173
174
175 uasm_il_bbit0(&p, &r, K0, 1, label_enter_bootloader);
176 uasm_i_nop(&p);
177#endif
178
179
180 uasm_i_cache(&p, 1, 0, 0);
181
182
183 uasm_i_dmfc0(&p, K0, C0_CVMMEMCTL);
184
185 uasm_i_dins(&p, K0, 0, 0, 6);
186
187 uasm_i_ori(&p, K0, K0, 0x1c0 | 54);
188
189 uasm_i_dmtc0(&p, K0, C0_CVMMEMCTL);
190
191
192 UASM_i_LA(&p, K0, (long)octeon_wdt_nmi_stage2);
193 uasm_i_jr(&p, K0);
194 uasm_i_dmfc0(&p, K0, C0_DESAVE);
195
196#ifdef CONFIG_HOTPLUG_CPU
197 uasm_build_label(&l, p, label_enter_bootloader);
198
199 UASM_i_LA(&p, K0, (long)octeon_bootloader_entry_addr);
200 uasm_i_jr(&p, K0);
201 uasm_i_dmfc0(&p, K0, C0_DESAVE);
202#endif
203 uasm_resolve_relocs(relocs, labels);
204
205 len = (int)(p - nmi_stage1_insns);
206 pr_debug("Synthesized NMI stage 1 handler (%d instructions)\n", len);
207
208 pr_debug("\t.set push\n");
209 pr_debug("\t.set noreorder\n");
210 for (i = 0; i < len; i++)
211 pr_debug("\t.word 0x%08x\n", nmi_stage1_insns[i]);
212 pr_debug("\t.set pop\n");
213
214 if (len > 32)
215 panic("NMI stage 1 handler exceeds 32 instructions, was %d\n", len);
216}
217
218static int cpu2core(int cpu)
219{
220#ifdef CONFIG_SMP
221 return cpu_logical_map(cpu);
222#else
223 return cvmx_get_core_num();
224#endif
225}
226
227static int core2cpu(int coreid)
228{
229#ifdef CONFIG_SMP
230 return cpu_number_map(coreid);
231#else
232 return 0;
233#endif
234}
235
236
237
238
239
240
241
242
243
244static irqreturn_t octeon_wdt_poke_irq(int cpl, void *dev_id)
245{
246 unsigned int core = cvmx_get_core_num();
247 int cpu = core2cpu(core);
248
249 if (do_coundown) {
250 if (per_cpu_countdown[cpu] > 0) {
251
252 cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
253 per_cpu_countdown[cpu]--;
254 } else {
255
256 disable_irq_nosync(cpl);
257 cpumask_clear_cpu(cpu, &irq_enabled_cpus);
258 }
259 } else {
260
261 cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
262 }
263 return IRQ_HANDLED;
264}
265
266
267extern int prom_putchar(char c);
268
269
270
271
272
273
274static void octeon_wdt_write_string(const char *str)
275{
276
277 while (*str)
278 prom_putchar(*str++);
279}
280
281
282
283
284
285
286
287static void octeon_wdt_write_hex(u64 value, int digits)
288{
289 int d;
290 int v;
291 for (d = 0; d < digits; d++) {
292 v = (value >> ((digits - d - 1) * 4)) & 0xf;
293 if (v >= 10)
294 prom_putchar('a' + v - 10);
295 else
296 prom_putchar('0' + v);
297 }
298}
299
300const char *reg_name[] = {
301 "$0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
302 "a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3",
303 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
304 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"
305};
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320void octeon_wdt_nmi_stage3(u64 reg[32])
321{
322 u64 i;
323
324 unsigned int coreid = cvmx_get_core_num();
325
326
327
328
329 u64 cp0_cause = read_c0_cause();
330 u64 cp0_status = read_c0_status();
331 u64 cp0_error_epc = read_c0_errorepc();
332 u64 cp0_epc = read_c0_epc();
333
334
335 __delay(100000000ull * coreid);
336
337 octeon_wdt_write_string("\r\n*** NMI Watchdog interrupt on Core 0x");
338 octeon_wdt_write_hex(coreid, 1);
339 octeon_wdt_write_string(" ***\r\n");
340 for (i = 0; i < 32; i++) {
341 octeon_wdt_write_string("\t");
342 octeon_wdt_write_string(reg_name[i]);
343 octeon_wdt_write_string("\t0x");
344 octeon_wdt_write_hex(reg[i], 16);
345 if (i & 1)
346 octeon_wdt_write_string("\r\n");
347 }
348 octeon_wdt_write_string("\terr_epc\t0x");
349 octeon_wdt_write_hex(cp0_error_epc, 16);
350
351 octeon_wdt_write_string("\tepc\t0x");
352 octeon_wdt_write_hex(cp0_epc, 16);
353 octeon_wdt_write_string("\r\n");
354
355 octeon_wdt_write_string("\tstatus\t0x");
356 octeon_wdt_write_hex(cp0_status, 16);
357 octeon_wdt_write_string("\tcause\t0x");
358 octeon_wdt_write_hex(cp0_cause, 16);
359 octeon_wdt_write_string("\r\n");
360
361 octeon_wdt_write_string("\tsum0\t0x");
362 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU_INTX_SUM0(coreid * 2)), 16);
363 octeon_wdt_write_string("\ten0\t0x");
364 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid * 2)), 16);
365 octeon_wdt_write_string("\r\n");
366
367 octeon_wdt_write_string("*** Chip soft reset soon ***\r\n");
368}
369
370static void octeon_wdt_disable_interrupt(int cpu)
371{
372 unsigned int core;
373 unsigned int irq;
374 union cvmx_ciu_wdogx ciu_wdog;
375
376 core = cpu2core(cpu);
377
378 irq = OCTEON_IRQ_WDOG0 + core;
379
380
381 cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
382
383
384 ciu_wdog.u64 = 0;
385 cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
386
387 free_irq(irq, octeon_wdt_poke_irq);
388}
389
390static void octeon_wdt_setup_interrupt(int cpu)
391{
392 unsigned int core;
393 unsigned int irq;
394 union cvmx_ciu_wdogx ciu_wdog;
395
396 core = cpu2core(cpu);
397
398
399 ciu_wdog.u64 = 0;
400 cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
401
402 per_cpu_countdown[cpu] = countdown_reset;
403
404 irq = OCTEON_IRQ_WDOG0 + core;
405
406 if (request_irq(irq, octeon_wdt_poke_irq,
407 IRQF_NO_THREAD, "octeon_wdt", octeon_wdt_poke_irq))
408 panic("octeon_wdt: Couldn't obtain irq %d", irq);
409
410 cpumask_set_cpu(cpu, &irq_enabled_cpus);
411
412
413 cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
414
415
416 ciu_wdog.u64 = 0;
417 ciu_wdog.s.len = timeout_cnt;
418 ciu_wdog.s.mode = 3;
419 cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
420}
421
422static int octeon_wdt_cpu_callback(struct notifier_block *nfb,
423 unsigned long action, void *hcpu)
424{
425 unsigned int cpu = (unsigned long)hcpu;
426
427 switch (action) {
428 case CPU_DOWN_PREPARE:
429 octeon_wdt_disable_interrupt(cpu);
430 break;
431 case CPU_ONLINE:
432 case CPU_DOWN_FAILED:
433 octeon_wdt_setup_interrupt(cpu);
434 break;
435 default:
436 break;
437 }
438 return NOTIFY_OK;
439}
440
441static void octeon_wdt_ping(void)
442{
443 int cpu;
444 int coreid;
445
446 for_each_online_cpu(cpu) {
447 coreid = cpu2core(cpu);
448 cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
449 per_cpu_countdown[cpu] = countdown_reset;
450 if ((countdown_reset || !do_coundown) &&
451 !cpumask_test_cpu(cpu, &irq_enabled_cpus)) {
452
453 int irq = OCTEON_IRQ_WDOG0 + coreid;
454 enable_irq(irq);
455 cpumask_set_cpu(cpu, &irq_enabled_cpus);
456 }
457 }
458}
459
460static void octeon_wdt_calc_parameters(int t)
461{
462 unsigned int periods;
463
464 timeout_sec = max_timeout_sec;
465
466
467
468
469
470
471 while ((t % timeout_sec) != 0)
472 timeout_sec--;
473
474 periods = t / timeout_sec;
475
476
477
478
479
480
481 countdown_reset = periods > 2 ? periods - 2 : 0;
482 heartbeat = t;
483 timeout_cnt = ((octeon_get_io_clock_rate() >> 8) * timeout_sec) >> 8;
484}
485
486static int octeon_wdt_set_heartbeat(int t)
487{
488 int cpu;
489 int coreid;
490 union cvmx_ciu_wdogx ciu_wdog;
491
492 if (t <= 0)
493 return -1;
494
495 octeon_wdt_calc_parameters(t);
496
497 for_each_online_cpu(cpu) {
498 coreid = cpu2core(cpu);
499 cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
500 ciu_wdog.u64 = 0;
501 ciu_wdog.s.len = timeout_cnt;
502 ciu_wdog.s.mode = 3;
503 cvmx_write_csr(CVMX_CIU_WDOGX(coreid), ciu_wdog.u64);
504 cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
505 }
506 octeon_wdt_ping();
507 return 0;
508}
509
510
511
512
513
514
515
516
517
518
519
520
521static ssize_t octeon_wdt_write(struct file *file, const char __user *buf,
522 size_t count, loff_t *ppos)
523{
524 if (count) {
525 if (!nowayout) {
526 size_t i;
527
528
529 expect_close = 0;
530
531 for (i = 0; i != count; i++) {
532 char c;
533 if (get_user(c, buf + i))
534 return -EFAULT;
535 if (c == 'V')
536 expect_close = 1;
537 }
538 }
539 octeon_wdt_ping();
540 }
541 return count;
542}
543
544
545
546
547
548
549
550
551
552
553
554
555
556static long octeon_wdt_ioctl(struct file *file, unsigned int cmd,
557 unsigned long arg)
558{
559 void __user *argp = (void __user *)arg;
560 int __user *p = argp;
561 int new_heartbeat;
562
563 static struct watchdog_info ident = {
564 .options = WDIOF_SETTIMEOUT|
565 WDIOF_MAGICCLOSE|
566 WDIOF_KEEPALIVEPING,
567 .firmware_version = 1,
568 .identity = "OCTEON",
569 };
570
571 switch (cmd) {
572 case WDIOC_GETSUPPORT:
573 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
574 case WDIOC_GETSTATUS:
575 case WDIOC_GETBOOTSTATUS:
576 return put_user(0, p);
577 case WDIOC_KEEPALIVE:
578 octeon_wdt_ping();
579 return 0;
580 case WDIOC_SETTIMEOUT:
581 if (get_user(new_heartbeat, p))
582 return -EFAULT;
583 if (octeon_wdt_set_heartbeat(new_heartbeat))
584 return -EINVAL;
585
586 case WDIOC_GETTIMEOUT:
587 return put_user(heartbeat, p);
588 default:
589 return -ENOTTY;
590 }
591}
592
593
594
595
596
597
598
599
600
601
602static int octeon_wdt_open(struct inode *inode, struct file *file)
603{
604 if (test_and_set_bit(0, &octeon_wdt_is_open))
605 return -EBUSY;
606
607
608
609 octeon_wdt_ping();
610 do_coundown = 1;
611 return nonseekable_open(inode, file);
612}
613
614
615
616
617
618
619
620
621
622
623
624
625
626static int octeon_wdt_release(struct inode *inode, struct file *file)
627{
628 if (expect_close) {
629 do_coundown = 0;
630 octeon_wdt_ping();
631 } else {
632 pr_crit("WDT device closed unexpectedly. WDT will not stop!\n");
633 }
634 clear_bit(0, &octeon_wdt_is_open);
635 expect_close = 0;
636 return 0;
637}
638
639static const struct file_operations octeon_wdt_fops = {
640 .owner = THIS_MODULE,
641 .llseek = no_llseek,
642 .write = octeon_wdt_write,
643 .unlocked_ioctl = octeon_wdt_ioctl,
644 .open = octeon_wdt_open,
645 .release = octeon_wdt_release,
646};
647
648static struct miscdevice octeon_wdt_miscdev = {
649 .minor = WATCHDOG_MINOR,
650 .name = "watchdog",
651 .fops = &octeon_wdt_fops,
652};
653
654static struct notifier_block octeon_wdt_cpu_notifier = {
655 .notifier_call = octeon_wdt_cpu_callback,
656};
657
658
659
660
661
662
663
664static int __init octeon_wdt_init(void)
665{
666 int i;
667 int ret;
668 int cpu;
669 u64 *ptr;
670
671
672
673
674
675
676
677
678
679 max_timeout_sec = 6;
680 do {
681 max_timeout_sec--;
682 timeout_cnt = ((octeon_get_io_clock_rate() >> 8) * max_timeout_sec) >> 8;
683 } while (timeout_cnt > 65535);
684
685 BUG_ON(timeout_cnt == 0);
686
687 octeon_wdt_calc_parameters(heartbeat);
688
689 pr_info("Initial granularity %d Sec\n", timeout_sec);
690
691 ret = misc_register(&octeon_wdt_miscdev);
692 if (ret) {
693 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
694 WATCHDOG_MINOR, ret);
695 goto out;
696 }
697
698
699 octeon_wdt_build_stage1();
700
701
702 ptr = (u64 *) nmi_stage1_insns;
703 for (i = 0; i < 16; i++) {
704 cvmx_write_csr(CVMX_MIO_BOOT_LOC_ADR, i * 8);
705 cvmx_write_csr(CVMX_MIO_BOOT_LOC_DAT, ptr[i]);
706 }
707 cvmx_write_csr(CVMX_MIO_BOOT_LOC_CFGX(0), 0x81fc0000);
708
709 cpumask_clear(&irq_enabled_cpus);
710
711 for_each_online_cpu(cpu)
712 octeon_wdt_setup_interrupt(cpu);
713
714 register_hotcpu_notifier(&octeon_wdt_cpu_notifier);
715out:
716 return ret;
717}
718
719
720
721
722static void __exit octeon_wdt_cleanup(void)
723{
724 int cpu;
725
726 misc_deregister(&octeon_wdt_miscdev);
727
728 unregister_hotcpu_notifier(&octeon_wdt_cpu_notifier);
729
730 for_each_online_cpu(cpu) {
731 int core = cpu2core(cpu);
732
733 cvmx_write_csr(CVMX_CIU_WDOGX(core), 0);
734
735 free_irq(OCTEON_IRQ_WDOG0 + core, octeon_wdt_poke_irq);
736 }
737
738
739
740
741 cvmx_write_csr(CVMX_MIO_BOOT_LOC_CFGX(0), 0);
742}
743
744MODULE_LICENSE("GPL");
745MODULE_AUTHOR("Cavium Networks <support@caviumnetworks.com>");
746MODULE_DESCRIPTION("Cavium Networks Octeon Watchdog driver.");
747module_init(octeon_wdt_init);
748module_exit(octeon_wdt_cleanup);
749