xref: /openbmc/linux/arch/x86/kernel/apic/hw_nmi.c (revision d2999e1b)
1 /*
2  *  HW NMI watchdog support
3  *
4  *  started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
5  *
6  *  Arch specific calls to support NMI watchdog
7  *
8  *  Bits copied from original nmi.c file
9  *
10  */
11 #include <asm/apic.h>
12 #include <asm/nmi.h>
13 
14 #include <linux/cpumask.h>
15 #include <linux/kdebug.h>
16 #include <linux/notifier.h>
17 #include <linux/kprobes.h>
18 #include <linux/nmi.h>
19 #include <linux/module.h>
20 #include <linux/delay.h>
21 
22 #ifdef CONFIG_HARDLOCKUP_DETECTOR
23 u64 hw_nmi_get_sample_period(int watchdog_thresh)
24 {
25 	return (u64)(cpu_khz) * 1000 * watchdog_thresh;
26 }
27 #endif
28 
29 #ifdef arch_trigger_all_cpu_backtrace
30 /* For reliability, we're prepared to waste bits here. */
31 static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
32 
33 /* "in progress" flag of arch_trigger_all_cpu_backtrace */
34 static unsigned long backtrace_flag;
35 
36 void arch_trigger_all_cpu_backtrace(bool include_self)
37 {
38 	int i;
39 	int cpu = get_cpu();
40 
41 	if (test_and_set_bit(0, &backtrace_flag)) {
42 		/*
43 		 * If there is already a trigger_all_cpu_backtrace() in progress
44 		 * (backtrace_flag == 1), don't output double cpu dump infos.
45 		 */
46 		put_cpu();
47 		return;
48 	}
49 
50 	cpumask_copy(to_cpumask(backtrace_mask), cpu_online_mask);
51 	if (!include_self)
52 		cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
53 
54 	if (!cpumask_empty(to_cpumask(backtrace_mask))) {
55 		pr_info("sending NMI to %s CPUs:\n",
56 			(include_self ? "all" : "other"));
57 		apic->send_IPI_mask(to_cpumask(backtrace_mask), NMI_VECTOR);
58 	}
59 
60 	/* Wait for up to 10 seconds for all CPUs to do the backtrace */
61 	for (i = 0; i < 10 * 1000; i++) {
62 		if (cpumask_empty(to_cpumask(backtrace_mask)))
63 			break;
64 		mdelay(1);
65 		touch_softlockup_watchdog();
66 	}
67 
68 	clear_bit(0, &backtrace_flag);
69 	smp_mb__after_atomic();
70 	put_cpu();
71 }
72 
73 static int
74 arch_trigger_all_cpu_backtrace_handler(unsigned int cmd, struct pt_regs *regs)
75 {
76 	int cpu;
77 
78 	cpu = smp_processor_id();
79 
80 	if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
81 		static arch_spinlock_t lock = __ARCH_SPIN_LOCK_UNLOCKED;
82 
83 		arch_spin_lock(&lock);
84 		printk(KERN_WARNING "NMI backtrace for cpu %d\n", cpu);
85 		show_regs(regs);
86 		arch_spin_unlock(&lock);
87 		cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
88 		return NMI_HANDLED;
89 	}
90 
91 	return NMI_DONE;
92 }
93 NOKPROBE_SYMBOL(arch_trigger_all_cpu_backtrace_handler);
94 
95 static int __init register_trigger_all_cpu_backtrace(void)
96 {
97 	register_nmi_handler(NMI_LOCAL, arch_trigger_all_cpu_backtrace_handler,
98 				0, "arch_bt");
99 	return 0;
100 }
101 early_initcall(register_trigger_all_cpu_backtrace);
102 #endif
103