2 * NMI watchdog support on APIC systems
4 * Started by Ingo Molnar <mingo@redhat.com>
7 * Mikael Pettersson : AMD K7 support for local APIC NMI watchdog.
8 * Mikael Pettersson : Power Management for local APIC NMI watchdog.
10 * Mikael Pettersson : PM converted to driver model. Disable/enable API.
13 #include <linux/nmi.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/sysdev.h>
19 #include <linux/sysctl.h>
20 #include <linux/kprobes.h>
21 #include <linux/cpumask.h>
22 #include <linux/kdebug.h>
26 #include <asm/proto.h>
29 #include <mach_traps.h>
31 int unknown_nmi_panic;
32 int nmi_watchdog_enabled;
34 static cpumask_t backtrace_mask = CPU_MASK_NONE;
37 * >0: the lapic NMI watchdog is active, but can be disabled
38 * <0: the lapic NMI watchdog has not been set up, and cannot
40 * 0: the lapic NMI watchdog is disabled, but can be enabled
42 atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
43 static int panic_on_timeout;
45 unsigned int nmi_watchdog = NMI_DEFAULT;
46 static unsigned int nmi_hz = HZ;
48 static DEFINE_PER_CPU(short, wd_enabled);
50 static int endflag __initdata = 0;
52 static inline unsigned int get_nmi_count(int cpu)
54 return cpu_pda(cpu)->__nmi_count;
57 static inline int mce_in_progress(void)
60 return atomic_read(&mce_entry) > 0;
66 * Take the local apic timer and PIT/HPET into account. We don't
67 * know which one is active, when we have highres/dyntick on
69 static inline unsigned int get_timer_irqs(int cpu)
71 return read_pda(apic_timer_irqs) + read_pda(irq0_irqs);
74 /* Run after command line and cpu_init init, but before all other checks */
75 void nmi_watchdog_default(void)
77 if (nmi_watchdog != NMI_DEFAULT)
79 nmi_watchdog = NMI_NONE;
83 /* The performance counters used by NMI_LOCAL_APIC don't trigger when
84 * the CPU is idle. To make sure the NMI watchdog really ticks on all
85 * CPUs during the test make them busy.
87 static __init void nmi_cpu_busy(void *data)
89 local_irq_enable_in_hardirq();
90 /* Intentionally don't use cpu_relax here. This is
91 to make sure that the performance counter really ticks,
92 even if there is a simulator or similar that catches the
93 pause instruction. On a real HT machine this is fine because
94 all other CPUs are busy with "useless" delay loops and don't
95 care if they get somewhat less cycles. */
101 int __init check_nmi_watchdog(void)
103 unsigned int *prev_nmi_count;
106 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
109 if (!atomic_read(&nmi_active))
112 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
116 printk(KERN_INFO "Testing NMI watchdog ... ");
119 if (nmi_watchdog == NMI_LOCAL_APIC)
120 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
123 for_each_possible_cpu(cpu)
124 prev_nmi_count[cpu] = get_nmi_count(cpu);
126 mdelay((20*1000)/nmi_hz); // wait 20 ticks
128 for_each_online_cpu(cpu) {
129 if (!per_cpu(wd_enabled, cpu))
131 if (get_nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
132 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
133 "appears to be stuck (%d->%d)!\n",
137 per_cpu(wd_enabled, cpu) = 0;
138 atomic_dec(&nmi_active);
142 if (!atomic_read(&nmi_active)) {
143 kfree(prev_nmi_count);
144 atomic_set(&nmi_active, -1);
149 /* now that we know it works we can reduce NMI frequency to
150 something more reasonable; makes a difference in some configs */
151 if (nmi_watchdog == NMI_LOCAL_APIC)
152 nmi_hz = lapic_adjust_nmi_hz(1);
154 kfree(prev_nmi_count);
158 static int __init setup_nmi_watchdog(char *str)
162 if (!strncmp(str,"panic",5)) {
163 panic_on_timeout = 1;
164 str = strchr(str, ',');
170 get_option(&str, &nmi);
172 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
179 __setup("nmi_watchdog=", setup_nmi_watchdog);
183 static int nmi_pm_active; /* nmi_active before suspend */
185 static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
187 /* only CPU0 goes here, other CPUs should be offline */
188 nmi_pm_active = atomic_read(&nmi_active);
189 stop_apic_nmi_watchdog(NULL);
190 BUG_ON(atomic_read(&nmi_active) != 0);
194 static int lapic_nmi_resume(struct sys_device *dev)
196 /* only CPU0 goes here, other CPUs should be offline */
197 if (nmi_pm_active > 0) {
198 setup_apic_nmi_watchdog(NULL);
199 touch_nmi_watchdog();
204 static struct sysdev_class nmi_sysclass = {
206 .resume = lapic_nmi_resume,
207 .suspend = lapic_nmi_suspend,
210 static struct sys_device device_lapic_nmi = {
212 .cls = &nmi_sysclass,
215 static int __init init_lapic_nmi_sysfs(void)
219 /* should really be a BUG_ON but b/c this is an
220 * init call, it just doesn't work. -dcz
222 if (nmi_watchdog != NMI_LOCAL_APIC)
225 if (atomic_read(&nmi_active) < 0)
228 error = sysdev_class_register(&nmi_sysclass);
230 error = sysdev_register(&device_lapic_nmi);
233 /* must come after the local APIC's device_initcall() */
234 late_initcall(init_lapic_nmi_sysfs);
236 #endif /* CONFIG_PM */
238 static void __acpi_nmi_enable(void *__unused)
240 apic_write_around(APIC_LVT0, APIC_DM_NMI);
244 * Enable timer based NMIs on all CPUs:
246 void acpi_nmi_enable(void)
248 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
249 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
252 static void __acpi_nmi_disable(void *__unused)
254 apic_write_around(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
258 * Disable timer based NMIs on all CPUs:
260 void acpi_nmi_disable(void)
262 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
263 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
266 void setup_apic_nmi_watchdog(void *unused)
268 if (__get_cpu_var(wd_enabled))
271 /* cheap hack to support suspend/resume */
272 /* if cpu0 is not active neither should the other cpus */
273 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
276 switch (nmi_watchdog) {
278 __get_cpu_var(wd_enabled) = 1;
279 if (lapic_watchdog_init(nmi_hz) < 0) {
280 __get_cpu_var(wd_enabled) = 0;
285 __get_cpu_var(wd_enabled) = 1;
286 atomic_inc(&nmi_active);
290 void stop_apic_nmi_watchdog(void *unused)
292 /* only support LOCAL and IO APICs for now */
293 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
294 (nmi_watchdog != NMI_IO_APIC))
296 if (__get_cpu_var(wd_enabled) == 0)
298 if (nmi_watchdog == NMI_LOCAL_APIC)
299 lapic_watchdog_stop();
300 __get_cpu_var(wd_enabled) = 0;
301 atomic_dec(&nmi_active);
305 * the best way to detect whether a CPU has a 'hard lockup' problem
306 * is to check it's local APIC timer IRQ counts. If they are not
307 * changing then that CPU has some problem.
309 * as these watchdog NMI IRQs are generated on every CPU, we only
310 * have to check the current processor.
313 static DEFINE_PER_CPU(unsigned, last_irq_sum);
314 static DEFINE_PER_CPU(local_t, alert_counter);
315 static DEFINE_PER_CPU(int, nmi_touch);
317 void touch_nmi_watchdog(void)
319 if (nmi_watchdog > 0) {
323 * Tell other CPUs to reset their alert counters. We cannot
324 * do it ourselves because the alert count increase is not
327 for_each_present_cpu(cpu) {
328 if (per_cpu(nmi_touch, cpu) != 1)
329 per_cpu(nmi_touch, cpu) = 1;
333 touch_softlockup_watchdog();
335 EXPORT_SYMBOL(touch_nmi_watchdog);
337 notrace __kprobes int
338 nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
342 int cpu = smp_processor_id();
345 /* check for other users first */
346 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
352 sum = get_timer_irqs(cpu);
354 if (__get_cpu_var(nmi_touch)) {
355 __get_cpu_var(nmi_touch) = 0;
359 if (cpu_isset(cpu, backtrace_mask)) {
360 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
363 printk("NMI backtrace for cpu %d\n", cpu);
366 cpu_clear(cpu, backtrace_mask);
369 if (mce_in_progress())
372 /* if the apic timer isn't firing, this cpu isn't doing much */
373 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
375 * Ayiee, looks like this CPU is stuck ...
376 * wait a few IRQs (5 seconds) before doing the oops ...
378 local_inc(&__get_cpu_var(alert_counter));
379 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
380 die_nmi("NMI Watchdog detected LOCKUP",
381 regs, panic_on_timeout);
383 __get_cpu_var(last_irq_sum) = sum;
384 local_set(&__get_cpu_var(alert_counter), 0);
387 /* see if the nmi watchdog went off */
388 if (!__get_cpu_var(wd_enabled))
390 switch (nmi_watchdog) {
392 rc |= lapic_wd_event(nmi_hz);
395 /* don't know how to accurately check for this.
396 * just assume it was a watchdog timer interrupt
397 * This matches the old behaviour.
407 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
409 unsigned char reason = get_nmi_reason();
412 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
413 die_nmi(buf, regs, 1); /* Always panic here */
418 * proc handler for /proc/sys/kernel/nmi
420 int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
421 void __user *buffer, size_t *length, loff_t *ppos)
425 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
426 old_state = nmi_watchdog_enabled;
427 proc_dointvec(table, write, file, buffer, length, ppos);
428 if (!!old_state == !!nmi_watchdog_enabled)
431 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
432 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
436 /* if nmi_watchdog is not set yet, then set it */
437 nmi_watchdog_default();
439 if (nmi_watchdog == NMI_LOCAL_APIC) {
440 if (nmi_watchdog_enabled)
441 enable_lapic_nmi_watchdog();
443 disable_lapic_nmi_watchdog();
446 "NMI watchdog doesn't know what hardware to touch\n");
454 int do_nmi_callback(struct pt_regs *regs, int cpu)
457 if (unknown_nmi_panic)
458 return unknown_nmi_panic_callback(regs, cpu);
463 void __trigger_all_cpu_backtrace(void)
467 backtrace_mask = cpu_online_map;
468 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
469 for (i = 0; i < 10 * 1000; i++) {
470 if (cpus_empty(backtrace_mask))
476 EXPORT_SYMBOL(nmi_active);
477 EXPORT_SYMBOL(nmi_watchdog);