sched: make early bootup sched_clock() use safer
[wrt350n-kernel.git] / arch / x86 / kernel / nmi_64.c
blob9a4fde74bee119f393a6d05c7207f9a23531a4d1
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 /* Run after command line and cpu_init init, but before all other checks */
50 void nmi_watchdog_default(void)
52 if (nmi_watchdog != NMI_DEFAULT)
53 return;
54 nmi_watchdog = NMI_NONE;
57 static int endflag __initdata = 0;
59 #ifdef CONFIG_SMP
60 /* The performance counters used by NMI_LOCAL_APIC don't trigger when
61 * the CPU is idle. To make sure the NMI watchdog really ticks on all
62 * CPUs during the test make them busy.
64 static __init void nmi_cpu_busy(void *data)
66 local_irq_enable_in_hardirq();
67 /* Intentionally don't use cpu_relax here. This is
68 to make sure that the performance counter really ticks,
69 even if there is a simulator or similar that catches the
70 pause instruction. On a real HT machine this is fine because
71 all other CPUs are busy with "useless" delay loops and don't
72 care if they get somewhat less cycles. */
73 while (endflag == 0)
74 mb();
76 #endif
78 int __init check_nmi_watchdog(void)
80 int *prev_nmi_count;
81 int cpu;
83 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
84 return 0;
86 if (!atomic_read(&nmi_active))
87 return 0;
89 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
90 if (!prev_nmi_count)
91 return -1;
93 printk(KERN_INFO "Testing NMI watchdog ... ");
95 #ifdef CONFIG_SMP
96 if (nmi_watchdog == NMI_LOCAL_APIC)
97 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
98 #endif
100 for (cpu = 0; cpu < NR_CPUS; cpu++)
101 prev_nmi_count[cpu] = cpu_pda(cpu)->__nmi_count;
102 local_irq_enable();
103 mdelay((20*1000)/nmi_hz); // wait 20 ticks
105 for_each_online_cpu(cpu) {
106 if (!per_cpu(wd_enabled, cpu))
107 continue;
108 if (cpu_pda(cpu)->__nmi_count - prev_nmi_count[cpu] <= 5) {
109 printk(KERN_WARNING "WARNING: CPU#%d: NMI "
110 "appears to be stuck (%d->%d)!\n",
111 cpu,
112 prev_nmi_count[cpu],
113 cpu_pda(cpu)->__nmi_count);
114 per_cpu(wd_enabled, cpu) = 0;
115 atomic_dec(&nmi_active);
118 endflag = 1;
119 if (!atomic_read(&nmi_active)) {
120 kfree(prev_nmi_count);
121 atomic_set(&nmi_active, -1);
122 return -1;
124 printk("OK.\n");
126 /* now that we know it works we can reduce NMI frequency to
127 something more reasonable; makes a difference in some configs */
128 if (nmi_watchdog == NMI_LOCAL_APIC)
129 nmi_hz = lapic_adjust_nmi_hz(1);
131 kfree(prev_nmi_count);
132 return 0;
135 static int __init setup_nmi_watchdog(char *str)
137 int nmi;
139 if (!strncmp(str,"panic",5)) {
140 panic_on_timeout = 1;
141 str = strchr(str, ',');
142 if (!str)
143 return 1;
144 ++str;
147 get_option(&str, &nmi);
149 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
150 return 0;
152 nmi_watchdog = nmi;
153 return 1;
156 __setup("nmi_watchdog=", setup_nmi_watchdog);
158 #ifdef CONFIG_PM
160 static int nmi_pm_active; /* nmi_active before suspend */
162 static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
164 /* only CPU0 goes here, other CPUs should be offline */
165 nmi_pm_active = atomic_read(&nmi_active);
166 stop_apic_nmi_watchdog(NULL);
167 BUG_ON(atomic_read(&nmi_active) != 0);
168 return 0;
171 static int lapic_nmi_resume(struct sys_device *dev)
173 /* only CPU0 goes here, other CPUs should be offline */
174 if (nmi_pm_active > 0) {
175 setup_apic_nmi_watchdog(NULL);
176 touch_nmi_watchdog();
178 return 0;
181 static struct sysdev_class nmi_sysclass = {
182 .name = "lapic_nmi",
183 .resume = lapic_nmi_resume,
184 .suspend = lapic_nmi_suspend,
187 static struct sys_device device_lapic_nmi = {
188 .id = 0,
189 .cls = &nmi_sysclass,
192 static int __init init_lapic_nmi_sysfs(void)
194 int error;
196 /* should really be a BUG_ON but b/c this is an
197 * init call, it just doesn't work. -dcz
199 if (nmi_watchdog != NMI_LOCAL_APIC)
200 return 0;
202 if (atomic_read(&nmi_active) < 0)
203 return 0;
205 error = sysdev_class_register(&nmi_sysclass);
206 if (!error)
207 error = sysdev_register(&device_lapic_nmi);
208 return error;
210 /* must come after the local APIC's device_initcall() */
211 late_initcall(init_lapic_nmi_sysfs);
213 #endif /* CONFIG_PM */
215 static void __acpi_nmi_enable(void *__unused)
217 apic_write(APIC_LVT0, APIC_DM_NMI);
221 * Enable timer based NMIs on all CPUs:
223 void acpi_nmi_enable(void)
225 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
226 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
229 static void __acpi_nmi_disable(void *__unused)
231 apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
235 * Disable timer based NMIs on all CPUs:
237 void acpi_nmi_disable(void)
239 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
240 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
243 void setup_apic_nmi_watchdog(void *unused)
245 if (__get_cpu_var(wd_enabled))
246 return;
248 /* cheap hack to support suspend/resume */
249 /* if cpu0 is not active neither should the other cpus */
250 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
251 return;
253 switch (nmi_watchdog) {
254 case NMI_LOCAL_APIC:
255 __get_cpu_var(wd_enabled) = 1;
256 if (lapic_watchdog_init(nmi_hz) < 0) {
257 __get_cpu_var(wd_enabled) = 0;
258 return;
260 /* FALL THROUGH */
261 case NMI_IO_APIC:
262 __get_cpu_var(wd_enabled) = 1;
263 atomic_inc(&nmi_active);
267 void stop_apic_nmi_watchdog(void *unused)
269 /* only support LOCAL and IO APICs for now */
270 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
271 (nmi_watchdog != NMI_IO_APIC))
272 return;
273 if (__get_cpu_var(wd_enabled) == 0)
274 return;
275 if (nmi_watchdog == NMI_LOCAL_APIC)
276 lapic_watchdog_stop();
277 __get_cpu_var(wd_enabled) = 0;
278 atomic_dec(&nmi_active);
282 * the best way to detect whether a CPU has a 'hard lockup' problem
283 * is to check it's local APIC timer IRQ counts. If they are not
284 * changing then that CPU has some problem.
286 * as these watchdog NMI IRQs are generated on every CPU, we only
287 * have to check the current processor.
290 static DEFINE_PER_CPU(unsigned, last_irq_sum);
291 static DEFINE_PER_CPU(local_t, alert_counter);
292 static DEFINE_PER_CPU(int, nmi_touch);
294 void touch_nmi_watchdog(void)
296 if (nmi_watchdog > 0) {
297 unsigned cpu;
300 * Tell other CPUs to reset their alert counters. We cannot
301 * do it ourselves because the alert count increase is not
302 * atomic.
304 for_each_present_cpu(cpu) {
305 if (per_cpu(nmi_touch, cpu) != 1)
306 per_cpu(nmi_touch, cpu) = 1;
310 touch_softlockup_watchdog();
312 EXPORT_SYMBOL(touch_nmi_watchdog);
314 int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
316 int sum;
317 int touched = 0;
318 int cpu = smp_processor_id();
319 int rc = 0;
321 /* check for other users first */
322 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
323 == NOTIFY_STOP) {
324 rc = 1;
325 touched = 1;
328 sum = read_pda(apic_timer_irqs) + read_pda(irq0_irqs);
329 if (__get_cpu_var(nmi_touch)) {
330 __get_cpu_var(nmi_touch) = 0;
331 touched = 1;
334 if (cpu_isset(cpu, backtrace_mask)) {
335 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
337 spin_lock(&lock);
338 printk("NMI backtrace for cpu %d\n", cpu);
339 dump_stack();
340 spin_unlock(&lock);
341 cpu_clear(cpu, backtrace_mask);
344 #ifdef CONFIG_X86_MCE
345 /* Could check oops_in_progress here too, but it's safer
346 not too */
347 if (atomic_read(&mce_entry) > 0)
348 touched = 1;
349 #endif
350 /* if the apic timer isn't firing, this cpu isn't doing much */
351 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
353 * Ayiee, looks like this CPU is stuck ...
354 * wait a few IRQs (5 seconds) before doing the oops ...
356 local_inc(&__get_cpu_var(alert_counter));
357 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
358 die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs,
359 panic_on_timeout);
360 } else {
361 __get_cpu_var(last_irq_sum) = sum;
362 local_set(&__get_cpu_var(alert_counter), 0);
365 /* see if the nmi watchdog went off */
366 if (!__get_cpu_var(wd_enabled))
367 return rc;
368 switch (nmi_watchdog) {
369 case NMI_LOCAL_APIC:
370 rc |= lapic_wd_event(nmi_hz);
371 break;
372 case NMI_IO_APIC:
373 /* don't know how to accurately check for this.
374 * just assume it was a watchdog timer interrupt
375 * This matches the old behaviour.
377 rc = 1;
378 break;
380 return rc;
383 static unsigned ignore_nmis;
385 asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
387 nmi_enter();
388 add_pda(__nmi_count,1);
389 if (!ignore_nmis)
390 default_do_nmi(regs);
391 nmi_exit();
394 void stop_nmi(void)
396 acpi_nmi_disable();
397 ignore_nmis++;
400 void restart_nmi(void)
402 ignore_nmis--;
403 acpi_nmi_enable();
406 #ifdef CONFIG_SYSCTL
408 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
410 unsigned char reason = get_nmi_reason();
411 char buf[64];
413 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
414 die_nmi(buf, regs, 1); /* Always panic here */
415 return 0;
419 * proc handler for /proc/sys/kernel/nmi
421 int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
422 void __user *buffer, size_t *length, loff_t *ppos)
424 int old_state;
426 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
427 old_state = nmi_watchdog_enabled;
428 proc_dointvec(table, write, file, buffer, length, ppos);
429 if (!!old_state == !!nmi_watchdog_enabled)
430 return 0;
432 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
433 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
434 return -EIO;
437 /* if nmi_watchdog is not set yet, then set it */
438 nmi_watchdog_default();
440 if (nmi_watchdog == NMI_LOCAL_APIC) {
441 if (nmi_watchdog_enabled)
442 enable_lapic_nmi_watchdog();
443 else
444 disable_lapic_nmi_watchdog();
445 } else {
446 printk( KERN_WARNING
447 "NMI watchdog doesn't know what hardware to touch\n");
448 return -EIO;
450 return 0;
453 #endif
455 int do_nmi_callback(struct pt_regs *regs, int cpu)
457 #ifdef CONFIG_SYSCTL
458 if (unknown_nmi_panic)
459 return unknown_nmi_panic_callback(regs, cpu);
460 #endif
461 return 0;
464 void __trigger_all_cpu_backtrace(void)
466 int i;
468 backtrace_mask = cpu_online_map;
469 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
470 for (i = 0; i < 10 * 1000; i++) {
471 if (cpus_empty(backtrace_mask))
472 break;
473 mdelay(1);
477 EXPORT_SYMBOL(nmi_active);
478 EXPORT_SYMBOL(nmi_watchdog);