Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / x86 / kernel / nmi_64.c
blob6389f59f9408ba41ce5b86f639a78b13867e7427
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 * Pavel Machek and
10 * Mikael Pettersson : PM converted to driver model. Disable/enable API.
13 #include <linux/nmi.h>
14 #include <linux/mm.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>
24 #include <asm/smp.h>
25 #include <asm/nmi.h>
26 #include <asm/proto.h>
27 #include <asm/mce.h>
29 int unknown_nmi_panic;
30 int nmi_watchdog_enabled;
31 int panic_on_unrecovered_nmi;
33 static cpumask_t backtrace_mask = CPU_MASK_NONE;
35 /* nmi_active:
36 * >0: the lapic NMI watchdog is active, but can be disabled
37 * <0: the lapic NMI watchdog has not been set up, and cannot
38 * be enabled
39 * 0: the lapic NMI watchdog is disabled, but can be enabled
41 atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
42 static int panic_on_timeout;
44 unsigned int nmi_watchdog = NMI_DEFAULT;
45 static unsigned int nmi_hz = HZ;
47 static DEFINE_PER_CPU(short, wd_enabled);
49 <<<<<<< HEAD:arch/x86/kernel/nmi_64.c
50 /* local prototypes */
51 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
53 =======
54 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/nmi_64.c
55 /* Run after command line and cpu_init init, but before all other checks */
56 void nmi_watchdog_default(void)
58 if (nmi_watchdog != NMI_DEFAULT)
59 return;
60 nmi_watchdog = NMI_NONE;
63 static int endflag __initdata = 0;
65 #ifdef CONFIG_SMP
66 /* The performance counters used by NMI_LOCAL_APIC don't trigger when
67 * the CPU is idle. To make sure the NMI watchdog really ticks on all
68 * CPUs during the test make them busy.
70 static __init void nmi_cpu_busy(void *data)
72 local_irq_enable_in_hardirq();
73 /* Intentionally don't use cpu_relax here. This is
74 to make sure that the performance counter really ticks,
75 even if there is a simulator or similar that catches the
76 pause instruction. On a real HT machine this is fine because
77 all other CPUs are busy with "useless" delay loops and don't
78 care if they get somewhat less cycles. */
79 while (endflag == 0)
80 mb();
82 #endif
84 int __init check_nmi_watchdog(void)
86 int *prev_nmi_count;
87 int cpu;
89 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
90 return 0;
92 if (!atomic_read(&nmi_active))
93 return 0;
95 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
96 if (!prev_nmi_count)
97 return -1;
99 printk(KERN_INFO "Testing NMI watchdog ... ");
101 #ifdef CONFIG_SMP
102 if (nmi_watchdog == NMI_LOCAL_APIC)
103 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
104 #endif
106 for (cpu = 0; cpu < NR_CPUS; cpu++)
107 prev_nmi_count[cpu] = cpu_pda(cpu)->__nmi_count;
108 local_irq_enable();
109 mdelay((20*1000)/nmi_hz); // wait 20 ticks
111 for_each_online_cpu(cpu) {
112 if (!per_cpu(wd_enabled, cpu))
113 continue;
114 if (cpu_pda(cpu)->__nmi_count - prev_nmi_count[cpu] <= 5) {
115 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
116 "appears to be stuck (%d->%d)!\n",
117 cpu,
118 prev_nmi_count[cpu],
119 cpu_pda(cpu)->__nmi_count);
120 per_cpu(wd_enabled, cpu) = 0;
121 atomic_dec(&nmi_active);
124 endflag = 1;
125 if (!atomic_read(&nmi_active)) {
126 kfree(prev_nmi_count);
127 atomic_set(&nmi_active, -1);
128 return -1;
130 printk("OK.\n");
132 /* now that we know it works we can reduce NMI frequency to
133 something more reasonable; makes a difference in some configs */
134 if (nmi_watchdog == NMI_LOCAL_APIC)
135 nmi_hz = lapic_adjust_nmi_hz(1);
137 kfree(prev_nmi_count);
138 return 0;
141 static int __init setup_nmi_watchdog(char *str)
143 int nmi;
145 if (!strncmp(str,"panic",5)) {
146 panic_on_timeout = 1;
147 str = strchr(str, ',');
148 if (!str)
149 return 1;
150 ++str;
153 get_option(&str, &nmi);
155 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
156 return 0;
158 nmi_watchdog = nmi;
159 return 1;
162 __setup("nmi_watchdog=", setup_nmi_watchdog);
164 #ifdef CONFIG_PM
166 static int nmi_pm_active; /* nmi_active before suspend */
168 static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
170 /* only CPU0 goes here, other CPUs should be offline */
171 nmi_pm_active = atomic_read(&nmi_active);
172 stop_apic_nmi_watchdog(NULL);
173 BUG_ON(atomic_read(&nmi_active) != 0);
174 return 0;
177 static int lapic_nmi_resume(struct sys_device *dev)
179 /* only CPU0 goes here, other CPUs should be offline */
180 if (nmi_pm_active > 0) {
181 setup_apic_nmi_watchdog(NULL);
182 touch_nmi_watchdog();
184 return 0;
187 static struct sysdev_class nmi_sysclass = {
188 .name = "lapic_nmi",
189 .resume = lapic_nmi_resume,
190 .suspend = lapic_nmi_suspend,
193 static struct sys_device device_lapic_nmi = {
194 .id = 0,
195 .cls = &nmi_sysclass,
198 static int __init init_lapic_nmi_sysfs(void)
200 int error;
202 /* should really be a BUG_ON but b/c this is an
203 * init call, it just doesn't work. -dcz
205 if (nmi_watchdog != NMI_LOCAL_APIC)
206 return 0;
208 if (atomic_read(&nmi_active) < 0)
209 return 0;
211 error = sysdev_class_register(&nmi_sysclass);
212 if (!error)
213 error = sysdev_register(&device_lapic_nmi);
214 return error;
216 /* must come after the local APIC's device_initcall() */
217 late_initcall(init_lapic_nmi_sysfs);
219 #endif /* CONFIG_PM */
221 static void __acpi_nmi_enable(void *__unused)
223 apic_write(APIC_LVT0, APIC_DM_NMI);
227 * Enable timer based NMIs on all CPUs:
229 void acpi_nmi_enable(void)
231 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
232 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
235 static void __acpi_nmi_disable(void *__unused)
237 apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
241 * Disable timer based NMIs on all CPUs:
243 void acpi_nmi_disable(void)
245 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
246 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
249 void setup_apic_nmi_watchdog(void *unused)
251 if (__get_cpu_var(wd_enabled))
252 return;
254 /* cheap hack to support suspend/resume */
255 /* if cpu0 is not active neither should the other cpus */
256 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
257 return;
259 switch (nmi_watchdog) {
260 case NMI_LOCAL_APIC:
261 __get_cpu_var(wd_enabled) = 1;
262 if (lapic_watchdog_init(nmi_hz) < 0) {
263 __get_cpu_var(wd_enabled) = 0;
264 return;
266 /* FALL THROUGH */
267 case NMI_IO_APIC:
268 __get_cpu_var(wd_enabled) = 1;
269 atomic_inc(&nmi_active);
273 void stop_apic_nmi_watchdog(void *unused)
275 /* only support LOCAL and IO APICs for now */
276 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
277 (nmi_watchdog != NMI_IO_APIC))
278 return;
279 if (__get_cpu_var(wd_enabled) == 0)
280 return;
281 if (nmi_watchdog == NMI_LOCAL_APIC)
282 lapic_watchdog_stop();
283 __get_cpu_var(wd_enabled) = 0;
284 atomic_dec(&nmi_active);
288 * the best way to detect whether a CPU has a 'hard lockup' problem
289 * is to check it's local APIC timer IRQ counts. If they are not
290 * changing then that CPU has some problem.
292 * as these watchdog NMI IRQs are generated on every CPU, we only
293 * have to check the current processor.
296 static DEFINE_PER_CPU(unsigned, last_irq_sum);
297 static DEFINE_PER_CPU(local_t, alert_counter);
298 static DEFINE_PER_CPU(int, nmi_touch);
300 void touch_nmi_watchdog(void)
302 if (nmi_watchdog > 0) {
303 unsigned cpu;
306 * Tell other CPUs to reset their alert counters. We cannot
307 * do it ourselves because the alert count increase is not
308 * atomic.
310 for_each_present_cpu(cpu) {
311 if (per_cpu(nmi_touch, cpu) != 1)
312 per_cpu(nmi_touch, cpu) = 1;
316 touch_softlockup_watchdog();
318 EXPORT_SYMBOL(touch_nmi_watchdog);
320 int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
322 int sum;
323 int touched = 0;
324 int cpu = smp_processor_id();
325 int rc = 0;
327 /* check for other users first */
328 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
329 == NOTIFY_STOP) {
330 rc = 1;
331 touched = 1;
334 sum = read_pda(apic_timer_irqs) + read_pda(irq0_irqs);
335 if (__get_cpu_var(nmi_touch)) {
336 __get_cpu_var(nmi_touch) = 0;
337 touched = 1;
340 if (cpu_isset(cpu, backtrace_mask)) {
341 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
343 spin_lock(&lock);
344 printk("NMI backtrace for cpu %d\n", cpu);
345 dump_stack();
346 spin_unlock(&lock);
347 cpu_clear(cpu, backtrace_mask);
350 #ifdef CONFIG_X86_MCE
351 /* Could check oops_in_progress here too, but it's safer
352 not too */
353 if (atomic_read(&mce_entry) > 0)
354 touched = 1;
355 #endif
356 /* if the apic timer isn't firing, this cpu isn't doing much */
357 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
359 * Ayiee, looks like this CPU is stuck ...
360 * wait a few IRQs (5 seconds) before doing the oops ...
362 local_inc(&__get_cpu_var(alert_counter));
363 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
364 die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs,
365 panic_on_timeout);
366 } else {
367 __get_cpu_var(last_irq_sum) = sum;
368 local_set(&__get_cpu_var(alert_counter), 0);
371 /* see if the nmi watchdog went off */
372 if (!__get_cpu_var(wd_enabled))
373 return rc;
374 switch (nmi_watchdog) {
375 case NMI_LOCAL_APIC:
376 rc |= lapic_wd_event(nmi_hz);
377 break;
378 case NMI_IO_APIC:
379 /* don't know how to accurately check for this.
380 * just assume it was a watchdog timer interrupt
381 * This matches the old behaviour.
383 rc = 1;
384 break;
386 return rc;
389 static unsigned ignore_nmis;
391 asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
393 nmi_enter();
394 add_pda(__nmi_count,1);
395 if (!ignore_nmis)
396 default_do_nmi(regs);
397 nmi_exit();
400 <<<<<<< HEAD:arch/x86/kernel/nmi_64.c
401 int do_nmi_callback(struct pt_regs * regs, int cpu)
403 #ifdef CONFIG_SYSCTL
404 if (unknown_nmi_panic)
405 return unknown_nmi_panic_callback(regs, cpu);
406 #endif
407 return 0;
410 =======
411 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/nmi_64.c
412 void stop_nmi(void)
414 acpi_nmi_disable();
415 ignore_nmis++;
418 void restart_nmi(void)
420 ignore_nmis--;
421 acpi_nmi_enable();
424 #ifdef CONFIG_SYSCTL
426 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
428 unsigned char reason = get_nmi_reason();
429 char buf[64];
431 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
432 die_nmi(buf, regs, 1); /* Always panic here */
433 return 0;
437 * proc handler for /proc/sys/kernel/nmi
439 int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
440 void __user *buffer, size_t *length, loff_t *ppos)
442 int old_state;
444 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
445 old_state = nmi_watchdog_enabled;
446 proc_dointvec(table, write, file, buffer, length, ppos);
447 if (!!old_state == !!nmi_watchdog_enabled)
448 return 0;
450 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
451 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
452 return -EIO;
455 /* if nmi_watchdog is not set yet, then set it */
456 nmi_watchdog_default();
458 if (nmi_watchdog == NMI_LOCAL_APIC) {
459 if (nmi_watchdog_enabled)
460 enable_lapic_nmi_watchdog();
461 else
462 disable_lapic_nmi_watchdog();
463 } else {
464 printk( KERN_WARNING
465 "NMI watchdog doesn't know what hardware to touch\n");
466 return -EIO;
468 return 0;
471 #endif
473 <<<<<<< HEAD:arch/x86/kernel/nmi_64.c
474 =======
475 int do_nmi_callback(struct pt_regs *regs, int cpu)
477 #ifdef CONFIG_SYSCTL
478 if (unknown_nmi_panic)
479 return unknown_nmi_panic_callback(regs, cpu);
480 #endif
481 return 0;
484 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/nmi_64.c
485 void __trigger_all_cpu_backtrace(void)
487 int i;
489 backtrace_mask = cpu_online_map;
490 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
491 for (i = 0; i < 10 * 1000; i++) {
492 if (cpus_empty(backtrace_mask))
493 break;
494 mdelay(1);
498 EXPORT_SYMBOL(nmi_active);
499 EXPORT_SYMBOL(nmi_watchdog);