8c3edfb89c2be3d3a9a83920d0d4c9561c24539d
[firefly-linux-kernel-4.4.55.git] / arch / x86 / kernel / apic / hw_nmi.c
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
12 #include <asm/apic.h>
13 #include <linux/smp.h>
14 #include <linux/cpumask.h>
15 #include <linux/sched.h>
16 #include <linux/percpu.h>
17 #include <linux/cpumask.h>
18 #include <linux/kernel_stat.h>
19 #include <asm/mce.h>
20 #include <linux/kdebug.h>
21 #include <linux/notifier.h>
22 #include <linux/kprobes.h>
23
24
25 #include <linux/nmi.h>
26 #include <linux/module.h>
27
28 /* For reliability, we're prepared to waste bits here. */
29 static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
30
31 static DEFINE_PER_CPU(unsigned, last_irq_sum);
32
33 /*
34  * Take the local apic timer and PIT/HPET into account. We don't
35  * know which one is active, when we have highres/dyntick on
36  */
37 static inline unsigned int get_timer_irqs(int cpu)
38 {
39         unsigned int irqs = per_cpu(irq_stat, cpu).irq0_irqs;
40
41 #if defined(CONFIG_X86_LOCAL_APIC)
42         irqs += per_cpu(irq_stat, cpu).apic_timer_irqs;
43 #endif
44
45         return irqs;
46 }
47
48 static inline int mce_in_progress(void)
49 {
50 #if defined(CONFIG_X86_MCE)
51         return atomic_read(&mce_entry) > 0;
52 #endif
53         return 0;
54 }
55
56 int hw_nmi_is_cpu_stuck(struct pt_regs *regs)
57 {
58         unsigned int sum;
59         int cpu = smp_processor_id();
60
61         /* if we are doing an mce, just assume the cpu is not stuck */
62         /* Could check oops_in_progress here too, but it's safer not to */
63         if (mce_in_progress())
64                 return 0;
65
66         /* We determine if the cpu is stuck by checking whether any
67          * interrupts have happened since we last checked.  Of course
68          * an nmi storm could create false positives, but the higher
69          * level logic should account for that
70          */
71         sum = get_timer_irqs(cpu);
72         if (__get_cpu_var(last_irq_sum) == sum) {
73                 return 1;
74         } else {
75                 __get_cpu_var(last_irq_sum) = sum;
76                 return 0;
77         }
78 }
79
80 u64 hw_nmi_get_sample_period(void)
81 {
82         return (u64)(cpu_khz) * 1000 * 60;
83 }
84
85 #ifdef ARCH_HAS_NMI_WATCHDOG
86 void arch_trigger_all_cpu_backtrace(void)
87 {
88         int i;
89
90         cpumask_copy(to_cpumask(backtrace_mask), cpu_online_mask);
91
92         printk(KERN_INFO "sending NMI to all CPUs:\n");
93         apic->send_IPI_all(NMI_VECTOR);
94
95         /* Wait for up to 10 seconds for all CPUs to do the backtrace */
96         for (i = 0; i < 10 * 1000; i++) {
97                 if (cpumask_empty(to_cpumask(backtrace_mask)))
98                         break;
99                 mdelay(1);
100         }
101 }
102
103 static int __kprobes
104 arch_trigger_all_cpu_backtrace_handler(struct notifier_block *self,
105                          unsigned long cmd, void *__args)
106 {
107         struct die_args *args = __args;
108         struct pt_regs *regs;
109         int cpu = smp_processor_id();
110
111         switch (cmd) {
112         case DIE_NMI:
113         case DIE_NMI_IPI:
114                 break;
115
116         default:
117                 return NOTIFY_DONE;
118         }
119
120         regs = args->regs;
121
122         if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
123                 static arch_spinlock_t lock = __ARCH_SPIN_LOCK_UNLOCKED;
124
125                 arch_spin_lock(&lock);
126                 printk(KERN_WARNING "NMI backtrace for cpu %d\n", cpu);
127                 show_regs(regs);
128                 dump_stack();
129                 arch_spin_unlock(&lock);
130                 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
131                 return NOTIFY_STOP;
132         }
133
134         return NOTIFY_DONE;
135 }
136
137 static __read_mostly struct notifier_block backtrace_notifier = {
138         .notifier_call          = arch_trigger_all_cpu_backtrace_handler,
139         .next                   = NULL,
140         .priority               = 1
141 };
142
143 static int __init register_trigger_all_cpu_backtrace(void)
144 {
145         register_die_notifier(&backtrace_notifier);
146         return 0;
147 }
148 early_initcall(register_trigger_all_cpu_backtrace);
149 #endif
150
151 /* STUB calls to mimic old nmi_watchdog behaviour */
152 #if defined(CONFIG_X86_LOCAL_APIC)
153 unsigned int nmi_watchdog = NMI_NONE;
154 EXPORT_SYMBOL(nmi_watchdog);
155 void acpi_nmi_enable(void) { return; }
156 void acpi_nmi_disable(void) { return; }
157 #endif
158 atomic_t nmi_active = ATOMIC_INIT(0);           /* oprofile uses this */
159 EXPORT_SYMBOL(nmi_active);
160 int unknown_nmi_panic;
161 void cpu_nmi_set_wd_enabled(void) { return; }
162 void stop_apic_nmi_watchdog(void *unused) { return; }
163 void setup_apic_nmi_watchdog(void *unused) { return; }
164 int __init check_nmi_watchdog(void) { return 0; }