x86, irq: Kill io_apic_renumber_irq
[linux/fpc-iii.git] / arch / x86 / kernel / apic / nmi.c
blob1edaf15c0b8eef05b36653a0ed9087a9b3717f9e
1 /*
2 * NMI watchdog support on APIC systems
4 * Started by Ingo Molnar <mingo@redhat.com>
6 * Fixes:
7 * Mikael Pettersson : AMD K7 support for local APIC NMI watchdog.
8 * Mikael Pettersson : Power Management for local APIC NMI watchdog.
9 * Mikael Pettersson : Pentium 4 support for local APIC NMI watchdog.
10 * Pavel Machek and
11 * Mikael Pettersson : PM converted to driver model. Disable/enable API.
14 #include <asm/apic.h>
16 #include <linux/nmi.h>
17 #include <linux/mm.h>
18 #include <linux/delay.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/sysdev.h>
23 #include <linux/sysctl.h>
24 #include <linux/percpu.h>
25 #include <linux/kprobes.h>
26 #include <linux/cpumask.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/kdebug.h>
29 #include <linux/smp.h>
31 #include <asm/i8259.h>
32 #include <asm/io_apic.h>
33 #include <asm/proto.h>
34 #include <asm/timer.h>
36 #include <asm/mce.h>
38 #include <asm/mach_traps.h>
40 int unknown_nmi_panic;
41 int nmi_watchdog_enabled;
43 /* For reliability, we're prepared to waste bits here. */
44 static DECLARE_BITMAP(backtrace_mask, NR_CPUS) __read_mostly;
46 /* nmi_active:
47 * >0: the lapic NMI watchdog is active, but can be disabled
48 * <0: the lapic NMI watchdog has not been set up, and cannot
49 * be enabled
50 * 0: the lapic NMI watchdog is disabled, but can be enabled
52 atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
53 EXPORT_SYMBOL(nmi_active);
55 unsigned int nmi_watchdog = NMI_NONE;
56 EXPORT_SYMBOL(nmi_watchdog);
58 static int panic_on_timeout;
60 static unsigned int nmi_hz = HZ;
61 static DEFINE_PER_CPU(short, wd_enabled);
62 static int endflag __initdata;
64 static inline unsigned int get_nmi_count(int cpu)
66 return per_cpu(irq_stat, cpu).__nmi_count;
69 static inline int mce_in_progress(void)
71 #if defined(CONFIG_X86_MCE)
72 return atomic_read(&mce_entry) > 0;
73 #endif
74 return 0;
78 * Take the local apic timer and PIT/HPET into account. We don't
79 * know which one is active, when we have highres/dyntick on
81 static inline unsigned int get_timer_irqs(int cpu)
83 return per_cpu(irq_stat, cpu).apic_timer_irqs +
84 per_cpu(irq_stat, cpu).irq0_irqs;
87 #ifdef CONFIG_SMP
89 * The performance counters used by NMI_LOCAL_APIC don't trigger when
90 * the CPU is idle. To make sure the NMI watchdog really ticks on all
91 * CPUs during the test make them busy.
93 static __init void nmi_cpu_busy(void *data)
95 local_irq_enable_in_hardirq();
97 * Intentionally don't use cpu_relax here. This is
98 * to make sure that the performance counter really ticks,
99 * even if there is a simulator or similar that catches the
100 * pause instruction. On a real HT machine this is fine because
101 * all other CPUs are busy with "useless" delay loops and don't
102 * care if they get somewhat less cycles.
104 while (endflag == 0)
105 mb();
107 #endif
109 static void report_broken_nmi(int cpu, unsigned int *prev_nmi_count)
111 printk(KERN_CONT "\n");
113 printk(KERN_WARNING
114 "WARNING: CPU#%d: NMI appears to be stuck (%d->%d)!\n",
115 cpu, prev_nmi_count[cpu], get_nmi_count(cpu));
117 printk(KERN_WARNING
118 "Please report this to bugzilla.kernel.org,\n");
119 printk(KERN_WARNING
120 "and attach the output of the 'dmesg' command.\n");
122 per_cpu(wd_enabled, cpu) = 0;
123 atomic_dec(&nmi_active);
126 static void __acpi_nmi_disable(void *__unused)
128 apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
131 int __init check_nmi_watchdog(void)
133 unsigned int *prev_nmi_count;
134 int cpu;
136 if (!nmi_watchdog_active() || !atomic_read(&nmi_active))
137 return 0;
139 prev_nmi_count = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
140 if (!prev_nmi_count)
141 goto error;
143 printk(KERN_INFO "Testing NMI watchdog ... ");
145 #ifdef CONFIG_SMP
146 if (nmi_watchdog == NMI_LOCAL_APIC)
147 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0);
148 #endif
150 for_each_possible_cpu(cpu)
151 prev_nmi_count[cpu] = get_nmi_count(cpu);
152 local_irq_enable();
153 mdelay((20 * 1000) / nmi_hz); /* wait 20 ticks */
155 for_each_online_cpu(cpu) {
156 if (!per_cpu(wd_enabled, cpu))
157 continue;
158 if (get_nmi_count(cpu) - prev_nmi_count[cpu] <= 5)
159 report_broken_nmi(cpu, prev_nmi_count);
161 endflag = 1;
162 if (!atomic_read(&nmi_active)) {
163 kfree(prev_nmi_count);
164 atomic_set(&nmi_active, -1);
165 goto error;
167 printk("OK.\n");
170 * now that we know it works we can reduce NMI frequency to
171 * something more reasonable; makes a difference in some configs
173 if (nmi_watchdog == NMI_LOCAL_APIC)
174 nmi_hz = lapic_adjust_nmi_hz(1);
176 kfree(prev_nmi_count);
177 return 0;
178 error:
179 if (nmi_watchdog == NMI_IO_APIC) {
180 if (!timer_through_8259)
181 legacy_pic->chip->mask(0);
182 on_each_cpu(__acpi_nmi_disable, NULL, 1);
185 #ifdef CONFIG_X86_32
186 timer_ack = 0;
187 #endif
188 return -1;
191 static int __init setup_nmi_watchdog(char *str)
193 unsigned int nmi;
195 if (!strncmp(str, "panic", 5)) {
196 panic_on_timeout = 1;
197 str = strchr(str, ',');
198 if (!str)
199 return 1;
200 ++str;
203 if (!strncmp(str, "lapic", 5))
204 nmi_watchdog = NMI_LOCAL_APIC;
205 else if (!strncmp(str, "ioapic", 6))
206 nmi_watchdog = NMI_IO_APIC;
207 else {
208 get_option(&str, &nmi);
209 if (nmi >= NMI_INVALID)
210 return 0;
211 nmi_watchdog = nmi;
214 return 1;
216 __setup("nmi_watchdog=", setup_nmi_watchdog);
219 * Suspend/resume support
221 #ifdef CONFIG_PM
223 static int nmi_pm_active; /* nmi_active before suspend */
225 static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
227 /* only CPU0 goes here, other CPUs should be offline */
228 nmi_pm_active = atomic_read(&nmi_active);
229 stop_apic_nmi_watchdog(NULL);
230 BUG_ON(atomic_read(&nmi_active) != 0);
231 return 0;
234 static int lapic_nmi_resume(struct sys_device *dev)
236 /* only CPU0 goes here, other CPUs should be offline */
237 if (nmi_pm_active > 0) {
238 setup_apic_nmi_watchdog(NULL);
239 touch_nmi_watchdog();
241 return 0;
244 static struct sysdev_class nmi_sysclass = {
245 .name = "lapic_nmi",
246 .resume = lapic_nmi_resume,
247 .suspend = lapic_nmi_suspend,
250 static struct sys_device device_lapic_nmi = {
251 .id = 0,
252 .cls = &nmi_sysclass,
255 static int __init init_lapic_nmi_sysfs(void)
257 int error;
260 * should really be a BUG_ON but b/c this is an
261 * init call, it just doesn't work. -dcz
263 if (nmi_watchdog != NMI_LOCAL_APIC)
264 return 0;
266 if (atomic_read(&nmi_active) < 0)
267 return 0;
269 error = sysdev_class_register(&nmi_sysclass);
270 if (!error)
271 error = sysdev_register(&device_lapic_nmi);
272 return error;
275 /* must come after the local APIC's device_initcall() */
276 late_initcall(init_lapic_nmi_sysfs);
278 #endif /* CONFIG_PM */
280 static void __acpi_nmi_enable(void *__unused)
282 apic_write(APIC_LVT0, APIC_DM_NMI);
286 * Enable timer based NMIs on all CPUs:
288 void acpi_nmi_enable(void)
290 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
291 on_each_cpu(__acpi_nmi_enable, NULL, 1);
295 * Disable timer based NMIs on all CPUs:
297 void acpi_nmi_disable(void)
299 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
300 on_each_cpu(__acpi_nmi_disable, NULL, 1);
304 * This function is called as soon the LAPIC NMI watchdog driver has everything
305 * in place and it's ready to check if the NMIs belong to the NMI watchdog
307 void cpu_nmi_set_wd_enabled(void)
309 __get_cpu_var(wd_enabled) = 1;
312 void setup_apic_nmi_watchdog(void *unused)
314 if (__get_cpu_var(wd_enabled))
315 return;
317 /* cheap hack to support suspend/resume */
318 /* if cpu0 is not active neither should the other cpus */
319 if (smp_processor_id() != 0 && atomic_read(&nmi_active) <= 0)
320 return;
322 switch (nmi_watchdog) {
323 case NMI_LOCAL_APIC:
324 if (lapic_watchdog_init(nmi_hz) < 0) {
325 __get_cpu_var(wd_enabled) = 0;
326 return;
328 /* FALL THROUGH */
329 case NMI_IO_APIC:
330 __get_cpu_var(wd_enabled) = 1;
331 atomic_inc(&nmi_active);
335 void stop_apic_nmi_watchdog(void *unused)
337 /* only support LOCAL and IO APICs for now */
338 if (!nmi_watchdog_active())
339 return;
340 if (__get_cpu_var(wd_enabled) == 0)
341 return;
342 if (nmi_watchdog == NMI_LOCAL_APIC)
343 lapic_watchdog_stop();
344 else
345 __acpi_nmi_disable(NULL);
346 __get_cpu_var(wd_enabled) = 0;
347 atomic_dec(&nmi_active);
351 * the best way to detect whether a CPU has a 'hard lockup' problem
352 * is to check it's local APIC timer IRQ counts. If they are not
353 * changing then that CPU has some problem.
355 * as these watchdog NMI IRQs are generated on every CPU, we only
356 * have to check the current processor.
358 * since NMIs don't listen to _any_ locks, we have to be extremely
359 * careful not to rely on unsafe variables. The printk might lock
360 * up though, so we have to break up any console locks first ...
361 * [when there will be more tty-related locks, break them up here too!]
364 static DEFINE_PER_CPU(unsigned, last_irq_sum);
365 static DEFINE_PER_CPU(long, alert_counter);
366 static DEFINE_PER_CPU(int, nmi_touch);
368 void touch_nmi_watchdog(void)
370 if (nmi_watchdog_active()) {
371 unsigned cpu;
374 * Tell other CPUs to reset their alert counters. We cannot
375 * do it ourselves because the alert count increase is not
376 * atomic.
378 for_each_present_cpu(cpu) {
379 if (per_cpu(nmi_touch, cpu) != 1)
380 per_cpu(nmi_touch, cpu) = 1;
385 * Tickle the softlockup detector too:
387 touch_softlockup_watchdog();
389 EXPORT_SYMBOL(touch_nmi_watchdog);
391 notrace __kprobes int
392 nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
395 * Since current_thread_info()-> is always on the stack, and we
396 * always switch the stack NMI-atomically, it's safe to use
397 * smp_processor_id().
399 unsigned int sum;
400 int touched = 0;
401 int cpu = smp_processor_id();
402 int rc = 0;
404 /* check for other users first */
405 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
406 == NOTIFY_STOP) {
407 rc = 1;
408 touched = 1;
411 sum = get_timer_irqs(cpu);
413 if (__get_cpu_var(nmi_touch)) {
414 __get_cpu_var(nmi_touch) = 0;
415 touched = 1;
418 /* We can be called before check_nmi_watchdog, hence NULL check. */
419 if (cpumask_test_cpu(cpu, to_cpumask(backtrace_mask))) {
420 static DEFINE_RAW_SPINLOCK(lock); /* Serialise the printks */
422 raw_spin_lock(&lock);
423 printk(KERN_WARNING "NMI backtrace for cpu %d\n", cpu);
424 show_regs(regs);
425 dump_stack();
426 raw_spin_unlock(&lock);
427 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
429 rc = 1;
432 /* Could check oops_in_progress here too, but it's safer not to */
433 if (mce_in_progress())
434 touched = 1;
436 /* if the none of the timers isn't firing, this cpu isn't doing much */
437 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
439 * Ayiee, looks like this CPU is stuck ...
440 * wait a few IRQs (5 seconds) before doing the oops ...
442 __this_cpu_inc(alert_counter);
443 if (__this_cpu_read(alert_counter) == 5 * nmi_hz)
445 * die_nmi will return ONLY if NOTIFY_STOP happens..
447 die_nmi("BUG: NMI Watchdog detected LOCKUP",
448 regs, panic_on_timeout);
449 } else {
450 __get_cpu_var(last_irq_sum) = sum;
451 __this_cpu_write(alert_counter, 0);
454 /* see if the nmi watchdog went off */
455 if (!__get_cpu_var(wd_enabled))
456 return rc;
457 switch (nmi_watchdog) {
458 case NMI_LOCAL_APIC:
459 rc |= lapic_wd_event(nmi_hz);
460 break;
461 case NMI_IO_APIC:
463 * don't know how to accurately check for this.
464 * just assume it was a watchdog timer interrupt
465 * This matches the old behaviour.
467 rc = 1;
468 break;
470 return rc;
473 #ifdef CONFIG_SYSCTL
475 static void enable_ioapic_nmi_watchdog_single(void *unused)
477 __get_cpu_var(wd_enabled) = 1;
478 atomic_inc(&nmi_active);
479 __acpi_nmi_enable(NULL);
482 static void enable_ioapic_nmi_watchdog(void)
484 on_each_cpu(enable_ioapic_nmi_watchdog_single, NULL, 1);
485 touch_nmi_watchdog();
488 static void disable_ioapic_nmi_watchdog(void)
490 on_each_cpu(stop_apic_nmi_watchdog, NULL, 1);
493 static int __init setup_unknown_nmi_panic(char *str)
495 unknown_nmi_panic = 1;
496 return 1;
498 __setup("unknown_nmi_panic", setup_unknown_nmi_panic);
500 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
502 unsigned char reason = get_nmi_reason();
503 char buf[64];
505 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
506 die_nmi(buf, regs, 1); /* Always panic here */
507 return 0;
511 * proc handler for /proc/sys/kernel/nmi
513 int proc_nmi_enabled(struct ctl_table *table, int write,
514 void __user *buffer, size_t *length, loff_t *ppos)
516 int old_state;
518 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
519 old_state = nmi_watchdog_enabled;
520 proc_dointvec(table, write, buffer, length, ppos);
521 if (!!old_state == !!nmi_watchdog_enabled)
522 return 0;
524 if (atomic_read(&nmi_active) < 0 || !nmi_watchdog_active()) {
525 printk(KERN_WARNING
526 "NMI watchdog is permanently disabled\n");
527 return -EIO;
530 if (nmi_watchdog == NMI_LOCAL_APIC) {
531 if (nmi_watchdog_enabled)
532 enable_lapic_nmi_watchdog();
533 else
534 disable_lapic_nmi_watchdog();
535 } else if (nmi_watchdog == NMI_IO_APIC) {
536 if (nmi_watchdog_enabled)
537 enable_ioapic_nmi_watchdog();
538 else
539 disable_ioapic_nmi_watchdog();
540 } else {
541 printk(KERN_WARNING
542 "NMI watchdog doesn't know what hardware to touch\n");
543 return -EIO;
545 return 0;
548 #endif /* CONFIG_SYSCTL */
550 int do_nmi_callback(struct pt_regs *regs, int cpu)
552 #ifdef CONFIG_SYSCTL
553 if (unknown_nmi_panic)
554 return unknown_nmi_panic_callback(regs, cpu);
555 #endif
556 return 0;
559 void arch_trigger_all_cpu_backtrace(void)
561 int i;
563 cpumask_copy(to_cpumask(backtrace_mask), cpu_online_mask);
565 printk(KERN_INFO "sending NMI to all CPUs:\n");
566 apic->send_IPI_all(NMI_VECTOR);
568 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
569 for (i = 0; i < 10 * 1000; i++) {
570 if (cpumask_empty(to_cpumask(backtrace_mask)))
571 break;
572 mdelay(1);