aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCyrill Gorcunov <gorcunov@gmail.com>2008-05-24 11:36:41 -0400
committerThomas Gleixner <tglx@linutronix.de>2008-05-25 16:32:51 -0400
commit1798bc22b2790bf2a956588e6b17c36ef79ceff7 (patch)
tree68dd3275aa52221fa4edeca4982f05be8e21a437
parentfd5cea02de100197a4c26d9e103508cf09b50a82 (diff)
x86: nmi_32/64.c - merge down nmi_32.c and nmi_64.c to nmi.c
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com> Cc: hpa@zytor.com Cc: mingo@redhat.com Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
-rw-r--r--arch/x86/kernel/Makefile2
-rw-r--r--arch/x86/kernel/nmi.c (renamed from arch/x86/kernel/nmi_32.c)101
-rw-r--r--arch/x86/kernel/nmi_64.c477
3 files changed, 65 insertions, 515 deletions
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index 5e618c3b4720..d5dd6668225f 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -53,7 +53,7 @@ obj-$(CONFIG_X86_32_SMP) += smpcommon.o
53obj-$(CONFIG_X86_64_SMP) += tsc_sync.o smpcommon.o 53obj-$(CONFIG_X86_64_SMP) += tsc_sync.o smpcommon.o
54obj-$(CONFIG_X86_TRAMPOLINE) += trampoline_$(BITS).o 54obj-$(CONFIG_X86_TRAMPOLINE) += trampoline_$(BITS).o
55obj-$(CONFIG_X86_MPPARSE) += mpparse.o 55obj-$(CONFIG_X86_MPPARSE) += mpparse.o
56obj-$(CONFIG_X86_LOCAL_APIC) += apic_$(BITS).o nmi_$(BITS).o 56obj-$(CONFIG_X86_LOCAL_APIC) += apic_$(BITS).o nmi.o
57obj-$(CONFIG_X86_IO_APIC) += io_apic_$(BITS).o 57obj-$(CONFIG_X86_IO_APIC) += io_apic_$(BITS).o
58obj-$(CONFIG_X86_REBOOTFIXUPS) += reboot_fixups_32.o 58obj-$(CONFIG_X86_REBOOTFIXUPS) += reboot_fixups_32.o
59obj-$(CONFIG_KEXEC) += machine_kexec_$(BITS).o 59obj-$(CONFIG_KEXEC) += machine_kexec_$(BITS).o
diff --git a/arch/x86/kernel/nmi_32.c b/arch/x86/kernel/nmi.c
index a94dbedf2b74..69a839fc1eb0 100644
--- a/arch/x86/kernel/nmi_32.c
+++ b/arch/x86/kernel/nmi.c
@@ -11,10 +11,11 @@
11 * Mikael Pettersson : PM converted to driver model. Disable/enable API. 11 * Mikael Pettersson : PM converted to driver model. Disable/enable API.
12 */ 12 */
13 13
14#include <linux/nmi.h>
15#include <linux/mm.h>
14#include <linux/delay.h> 16#include <linux/delay.h>
15#include <linux/interrupt.h> 17#include <linux/interrupt.h>
16#include <linux/module.h> 18#include <linux/module.h>
17#include <linux/nmi.h>
18#include <linux/sysdev.h> 19#include <linux/sysdev.h>
19#include <linux/sysctl.h> 20#include <linux/sysctl.h>
20#include <linux/percpu.h> 21#include <linux/percpu.h>
@@ -22,13 +23,15 @@
22#include <linux/cpumask.h> 23#include <linux/cpumask.h>
23#include <linux/kernel_stat.h> 24#include <linux/kernel_stat.h>
24#include <linux/kdebug.h> 25#include <linux/kdebug.h>
25#include <linux/slab.h>
26 26
27#include <asm/smp.h> 27#include <asm/smp.h>
28#include <asm/nmi.h> 28#include <asm/nmi.h>
29#include <asm/proto.h>
29#include <asm/timer.h> 30#include <asm/timer.h>
30 31
31#include "mach_traps.h" 32#include <asm/mce.h>
33
34#include <mach_traps.h>
32 35
33int unknown_nmi_panic; 36int unknown_nmi_panic;
34int nmi_watchdog_enabled; 37int nmi_watchdog_enabled;
@@ -45,19 +48,25 @@ atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
45static int panic_on_timeout; 48static int panic_on_timeout;
46 49
47unsigned int nmi_watchdog = NMI_DEFAULT; 50unsigned int nmi_watchdog = NMI_DEFAULT;
48static unsigned int nmi_hz = HZ;
49 51
52static unsigned int nmi_hz = HZ;
50static DEFINE_PER_CPU(short, wd_enabled); 53static DEFINE_PER_CPU(short, wd_enabled);
51
52static int endflag __initdata = 0; 54static int endflag __initdata = 0;
53 55
54static inline unsigned int get_nmi_count(int cpu) 56static inline unsigned int get_nmi_count(int cpu)
55{ 57{
58#ifdef CONFIG_X86_64
59 return cpu_pda(cpu)->__nmi_count;
60#else
56 return nmi_count(cpu); 61 return nmi_count(cpu);
62#endif
57} 63}
58 64
59static inline int mce_in_progress(void) 65static inline int mce_in_progress(void)
60{ 66{
67#if defined(CONFIX_X86_64) && defined(CONFIG_X86_MCE)
68 return atomic_read(&mce_entry) > 0;
69#endif
61 return 0; 70 return 0;
62} 71}
63 72
@@ -67,8 +76,12 @@ static inline int mce_in_progress(void)
67 */ 76 */
68static inline unsigned int get_timer_irqs(int cpu) 77static inline unsigned int get_timer_irqs(int cpu)
69{ 78{
79#ifdef CONFIG_X86_64
80 return read_pda(apic_timer_irqs) + read_pda(irq0_irqs);
81#else
70 return per_cpu(irq_stat, cpu).apic_timer_irqs + 82 return per_cpu(irq_stat, cpu).apic_timer_irqs +
71 per_cpu(irq_stat, cpu).irq0_irqs; 83 per_cpu(irq_stat, cpu).irq0_irqs;
84#endif
72} 85}
73 86
74/* Run after command line and cpu_init init, but before all other checks */ 87/* Run after command line and cpu_init init, but before all other checks */
@@ -76,26 +89,33 @@ void nmi_watchdog_default(void)
76{ 89{
77 if (nmi_watchdog != NMI_DEFAULT) 90 if (nmi_watchdog != NMI_DEFAULT)
78 return; 91 return;
92#ifdef CONFIG_X86_64
93 nmi_watchdog = NMI_NONE;
94#else
79 if (lapic_watchdog_ok()) 95 if (lapic_watchdog_ok())
80 nmi_watchdog = NMI_LOCAL_APIC; 96 nmi_watchdog = NMI_LOCAL_APIC;
81 else 97 else
82 nmi_watchdog = NMI_IO_APIC; 98 nmi_watchdog = NMI_IO_APIC;
99#endif
83} 100}
84 101
85#ifdef CONFIG_SMP 102#ifdef CONFIG_SMP
86/* The performance counters used by NMI_LOCAL_APIC don't trigger when 103/*
104 * The performance counters used by NMI_LOCAL_APIC don't trigger when
87 * the CPU is idle. To make sure the NMI watchdog really ticks on all 105 * the CPU is idle. To make sure the NMI watchdog really ticks on all
88 * CPUs during the test make them busy. 106 * CPUs during the test make them busy.
89 */ 107 */
90static __init void nmi_cpu_busy(void *data) 108static __init void nmi_cpu_busy(void *data)
91{ 109{
92 local_irq_enable_in_hardirq(); 110 local_irq_enable_in_hardirq();
93 /* Intentionally don't use cpu_relax here. This is 111 /*
94 to make sure that the performance counter really ticks, 112 * Intentionally don't use cpu_relax here. This is
95 even if there is a simulator or similar that catches the 113 * to make sure that the performance counter really ticks,
96 pause instruction. On a real HT machine this is fine because 114 * even if there is a simulator or similar that catches the
97 all other CPUs are busy with "useless" delay loops and don't 115 * pause instruction. On a real HT machine this is fine because
98 care if they get somewhat less cycles. */ 116 * all other CPUs are busy with "useless" delay loops and don't
117 * care if they get somewhat less cycles.
118 */
99 while (endflag == 0) 119 while (endflag == 0)
100 mb(); 120 mb();
101} 121}
@@ -106,7 +126,7 @@ int __init check_nmi_watchdog(void)
106 unsigned int *prev_nmi_count; 126 unsigned int *prev_nmi_count;
107 int cpu; 127 int cpu;
108 128
109 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED)) 129 if (nmi_watchdog == NMI_NONE || nmi_watchdog == NMI_DISABLED)
110 return 0; 130 return 0;
111 131
112 if (!atomic_read(&nmi_active)) 132 if (!atomic_read(&nmi_active))
@@ -126,7 +146,7 @@ int __init check_nmi_watchdog(void)
126 for_each_possible_cpu(cpu) 146 for_each_possible_cpu(cpu)
127 prev_nmi_count[cpu] = get_nmi_count(cpu); 147 prev_nmi_count[cpu] = get_nmi_count(cpu);
128 local_irq_enable(); 148 local_irq_enable();
129 mdelay((20*1000)/nmi_hz); // wait 20 ticks 149 mdelay((20 * 1000) / nmi_hz); /* wait 20 ticks */
130 150
131 for_each_online_cpu(cpu) { 151 for_each_online_cpu(cpu) {
132 if (!per_cpu(wd_enabled, cpu)) 152 if (!per_cpu(wd_enabled, cpu))
@@ -149,16 +169,20 @@ int __init check_nmi_watchdog(void)
149 } 169 }
150 printk("OK.\n"); 170 printk("OK.\n");
151 171
152 /* now that we know it works we can reduce NMI frequency to 172 /*
153 something more reasonable; makes a difference in some configs */ 173 * now that we know it works we can reduce NMI frequency to
174 * something more reasonable; makes a difference in some configs
175 */
154 if (nmi_watchdog == NMI_LOCAL_APIC) 176 if (nmi_watchdog == NMI_LOCAL_APIC)
155 nmi_hz = lapic_adjust_nmi_hz(1); 177 nmi_hz = lapic_adjust_nmi_hz(1);
156 178
157 kfree(prev_nmi_count); 179 kfree(prev_nmi_count);
158 return 0; 180 return 0;
181
159error: 182error:
183#ifdef CONFIG_X86_32
160 timer_ack = !cpu_has_tsc; 184 timer_ack = !cpu_has_tsc;
161 185#endif
162 return -1; 186 return -1;
163} 187}
164 188
@@ -176,18 +200,17 @@ static int __init setup_nmi_watchdog(char *str)
176 200
177 get_option(&str, &nmi); 201 get_option(&str, &nmi);
178 202
179 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE)) 203 if (nmi >= NMI_INVALID || nmi < NMI_NONE)
180 return 0; 204 return 0;
181 205
182 nmi_watchdog = nmi; 206 nmi_watchdog = nmi;
183 return 1; 207 return 1;
184} 208}
185
186__setup("nmi_watchdog=", setup_nmi_watchdog); 209__setup("nmi_watchdog=", setup_nmi_watchdog);
187 210
188 211/*
189/* Suspend/resume support */ 212 * Suspend/resume support
190 213 */
191#ifdef CONFIG_PM 214#ifdef CONFIG_PM
192 215
193static int nmi_pm_active; /* nmi_active before suspend */ 216static int nmi_pm_active; /* nmi_active before suspend */
@@ -211,7 +234,6 @@ static int lapic_nmi_resume(struct sys_device *dev)
211 return 0; 234 return 0;
212} 235}
213 236
214
215static struct sysdev_class nmi_sysclass = { 237static struct sysdev_class nmi_sysclass = {
216 .name = "lapic_nmi", 238 .name = "lapic_nmi",
217 .resume = lapic_nmi_resume, 239 .resume = lapic_nmi_resume,
@@ -227,7 +249,8 @@ static int __init init_lapic_nmi_sysfs(void)
227{ 249{
228 int error; 250 int error;
229 251
230 /* should really be a BUG_ON but b/c this is an 252 /*
253 * should really be a BUG_ON but b/c this is an
231 * init call, it just doesn't work. -dcz 254 * init call, it just doesn't work. -dcz
232 */ 255 */
233 if (nmi_watchdog != NMI_LOCAL_APIC) 256 if (nmi_watchdog != NMI_LOCAL_APIC)
@@ -241,6 +264,7 @@ static int __init init_lapic_nmi_sysfs(void)
241 error = sysdev_register(&device_lapic_nmi); 264 error = sysdev_register(&device_lapic_nmi);
242 return error; 265 return error;
243} 266}
267
244/* must come after the local APIC's device_initcall() */ 268/* must come after the local APIC's device_initcall() */
245late_initcall(init_lapic_nmi_sysfs); 269late_initcall(init_lapic_nmi_sysfs);
246 270
@@ -281,12 +305,13 @@ void setup_apic_nmi_watchdog(void *unused)
281 305
282 /* cheap hack to support suspend/resume */ 306 /* cheap hack to support suspend/resume */
283 /* if cpu0 is not active neither should the other cpus */ 307 /* if cpu0 is not active neither should the other cpus */
284 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0)) 308 if (smp_processor_id() != 0 && atomic_read(&nmi_active) <= 0)
285 return; 309 return;
286 310
287 switch (nmi_watchdog) { 311 switch (nmi_watchdog) {
288 case NMI_LOCAL_APIC: 312 case NMI_LOCAL_APIC:
289 __get_cpu_var(wd_enabled) = 1; /* enable it before to avoid race with handler */ 313 /* enable it before to avoid race with handler */
314 __get_cpu_var(wd_enabled) = 1;
290 if (lapic_watchdog_init(nmi_hz) < 0) { 315 if (lapic_watchdog_init(nmi_hz) < 0) {
291 __get_cpu_var(wd_enabled) = 0; 316 __get_cpu_var(wd_enabled) = 0;
292 return; 317 return;
@@ -301,9 +326,9 @@ void setup_apic_nmi_watchdog(void *unused)
301void stop_apic_nmi_watchdog(void *unused) 326void stop_apic_nmi_watchdog(void *unused)
302{ 327{
303 /* only support LOCAL and IO APICs for now */ 328 /* only support LOCAL and IO APICs for now */
304 if ((nmi_watchdog != NMI_LOCAL_APIC) && 329 if (nmi_watchdog != NMI_LOCAL_APIC &&
305 (nmi_watchdog != NMI_IO_APIC)) 330 nmi_watchdog != NMI_IO_APIC)
306 return; 331 return;
307 if (__get_cpu_var(wd_enabled) == 0) 332 if (__get_cpu_var(wd_enabled) == 0)
308 return; 333 return;
309 if (nmi_watchdog == NMI_LOCAL_APIC) 334 if (nmi_watchdog == NMI_LOCAL_APIC)
@@ -323,8 +348,7 @@ void stop_apic_nmi_watchdog(void *unused)
323 * since NMIs don't listen to _any_ locks, we have to be extremely 348 * since NMIs don't listen to _any_ locks, we have to be extremely
324 * careful not to rely on unsafe variables. The printk might lock 349 * careful not to rely on unsafe variables. The printk might lock
325 * up though, so we have to break up any console locks first ... 350 * up though, so we have to break up any console locks first ...
326 * [when there will be more tty-related locks, break them up 351 * [when there will be more tty-related locks, break them up here too!]
327 * here too!]
328 */ 352 */
329 353
330static DEFINE_PER_CPU(unsigned, last_irq_sum); 354static DEFINE_PER_CPU(unsigned, last_irq_sum);
@@ -357,7 +381,6 @@ EXPORT_SYMBOL(touch_nmi_watchdog);
357notrace __kprobes int 381notrace __kprobes int
358nmi_watchdog_tick(struct pt_regs *regs, unsigned reason) 382nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
359{ 383{
360
361 /* 384 /*
362 * Since current_thread_info()-> is always on the stack, and we 385 * Since current_thread_info()-> is always on the stack, and we
363 * always switch the stack NMI-atomically, it's safe to use 386 * always switch the stack NMI-atomically, it's safe to use
@@ -403,7 +426,7 @@ nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
403 * wait a few IRQs (5 seconds) before doing the oops ... 426 * wait a few IRQs (5 seconds) before doing the oops ...
404 */ 427 */
405 local_inc(&__get_cpu_var(alert_counter)); 428 local_inc(&__get_cpu_var(alert_counter));
406 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz) 429 if (local_read(&__get_cpu_var(alert_counter)) == 5 * nmi_hz)
407 /* 430 /*
408 * die_nmi will return ONLY if NOTIFY_STOP happens.. 431 * die_nmi will return ONLY if NOTIFY_STOP happens..
409 */ 432 */
@@ -413,6 +436,7 @@ nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
413 __get_cpu_var(last_irq_sum) = sum; 436 __get_cpu_var(last_irq_sum) = sum;
414 local_set(&__get_cpu_var(alert_counter), 0); 437 local_set(&__get_cpu_var(alert_counter), 0);
415 } 438 }
439
416 /* see if the nmi watchdog went off */ 440 /* see if the nmi watchdog went off */
417 if (!__get_cpu_var(wd_enabled)) 441 if (!__get_cpu_var(wd_enabled))
418 return rc; 442 return rc;
@@ -421,7 +445,8 @@ nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
421 rc |= lapic_wd_event(nmi_hz); 445 rc |= lapic_wd_event(nmi_hz);
422 break; 446 break;
423 case NMI_IO_APIC: 447 case NMI_IO_APIC:
424 /* don't know how to accurately check for this. 448 /*
449 * don't know how to accurately check for this.
425 * just assume it was a watchdog timer interrupt 450 * just assume it was a watchdog timer interrupt
426 * This matches the old behaviour. 451 * This matches the old behaviour.
427 */ 452 */
@@ -458,7 +483,8 @@ int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
458 return 0; 483 return 0;
459 484
460 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) { 485 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
461 printk( KERN_WARNING "NMI watchdog is permanently disabled\n"); 486 printk(KERN_WARNING
487 "NMI watchdog is permanently disabled\n");
462 return -EIO; 488 return -EIO;
463 } 489 }
464 490
@@ -471,14 +497,14 @@ int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
471 else 497 else
472 disable_lapic_nmi_watchdog(); 498 disable_lapic_nmi_watchdog();
473 } else { 499 } else {
474 printk( KERN_WARNING 500 printk(KERN_WARNING
475 "NMI watchdog doesn't know what hardware to touch\n"); 501 "NMI watchdog doesn't know what hardware to touch\n");
476 return -EIO; 502 return -EIO;
477 } 503 }
478 return 0; 504 return 0;
479} 505}
480 506
481#endif 507#endif /* CONFIG_SYSCTL */
482 508
483int do_nmi_callback(struct pt_regs *regs, int cpu) 509int do_nmi_callback(struct pt_regs *regs, int cpu)
484{ 510{
@@ -504,3 +530,4 @@ void __trigger_all_cpu_backtrace(void)
504 530
505EXPORT_SYMBOL(nmi_active); 531EXPORT_SYMBOL(nmi_active);
506EXPORT_SYMBOL(nmi_watchdog); 532EXPORT_SYMBOL(nmi_watchdog);
533
diff --git a/arch/x86/kernel/nmi_64.c b/arch/x86/kernel/nmi_64.c
deleted file mode 100644
index c6802447262c..000000000000
--- a/arch/x86/kernel/nmi_64.c
+++ /dev/null
@@ -1,477 +0,0 @@
1/*
2 * NMI watchdog support on APIC systems
3 *
4 * Started by Ingo Molnar <mingo@redhat.com>
5 *
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.
11 */
12
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>
23
24#include <asm/smp.h>
25#include <asm/nmi.h>
26#include <asm/proto.h>
27#include <asm/mce.h>
28
29#include <mach_traps.h>
30
31int unknown_nmi_panic;
32int nmi_watchdog_enabled;
33
34static cpumask_t backtrace_mask = CPU_MASK_NONE;
35
36/* nmi_active:
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
39 * be enabled
40 * 0: the lapic NMI watchdog is disabled, but can be enabled
41 */
42atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
43static int panic_on_timeout;
44
45unsigned int nmi_watchdog = NMI_DEFAULT;
46static unsigned int nmi_hz = HZ;
47
48static DEFINE_PER_CPU(short, wd_enabled);
49
50static int endflag __initdata = 0;
51
52static inline unsigned int get_nmi_count(int cpu)
53{
54 return cpu_pda(cpu)->__nmi_count;
55}
56
57static inline int mce_in_progress(void)
58{
59#ifdef CONFIG_X86_MCE
60 return atomic_read(&mce_entry) > 0;
61#endif
62 return 0;
63}
64
65/*
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
68 */
69static inline unsigned int get_timer_irqs(int cpu)
70{
71 return read_pda(apic_timer_irqs) + read_pda(irq0_irqs);
72}
73
74/* Run after command line and cpu_init init, but before all other checks */
75void nmi_watchdog_default(void)
76{
77 if (nmi_watchdog != NMI_DEFAULT)
78 return;
79 nmi_watchdog = NMI_NONE;
80}
81
82#ifdef CONFIG_SMP
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.
86 */
87static __init void nmi_cpu_busy(void *data)
88{
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. */
96 while (endflag == 0)
97 mb();
98}
99#endif
100
101int __init check_nmi_watchdog(void)
102{
103 unsigned int *prev_nmi_count;
104 int cpu;
105
106 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DISABLED))
107 return 0;
108
109 if (!atomic_read(&nmi_active))
110 return 0;
111
112 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
113 if (!prev_nmi_count)
114 return -1;
115
116 printk(KERN_INFO "Testing NMI watchdog ... ");
117
118#ifdef CONFIG_SMP
119 if (nmi_watchdog == NMI_LOCAL_APIC)
120 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
121#endif
122
123 for_each_possible_cpu(cpu)
124 prev_nmi_count[cpu] = get_nmi_count(cpu);
125 local_irq_enable();
126 mdelay((20*1000)/nmi_hz); // wait 20 ticks
127
128 for_each_online_cpu(cpu) {
129 if (!per_cpu(wd_enabled, cpu))
130 continue;
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",
134 cpu,
135 prev_nmi_count[cpu],
136 get_nmi_count(cpu));
137 per_cpu(wd_enabled, cpu) = 0;
138 atomic_dec(&nmi_active);
139 }
140 }
141 endflag = 1;
142 if (!atomic_read(&nmi_active)) {
143 kfree(prev_nmi_count);
144 atomic_set(&nmi_active, -1);
145 return -1;
146 }
147 printk("OK.\n");
148
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);
153
154 kfree(prev_nmi_count);
155 return 0;
156}
157
158static int __init setup_nmi_watchdog(char *str)
159{
160 int nmi;
161
162 if (!strncmp(str,"panic",5)) {
163 panic_on_timeout = 1;
164 str = strchr(str, ',');
165 if (!str)
166 return 1;
167 ++str;
168 }
169
170 get_option(&str, &nmi);
171
172 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
173 return 0;
174
175 nmi_watchdog = nmi;
176 return 1;
177}
178
179__setup("nmi_watchdog=", setup_nmi_watchdog);
180
181#ifdef CONFIG_PM
182
183static int nmi_pm_active; /* nmi_active before suspend */
184
185static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
186{
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);
191 return 0;
192}
193
194static int lapic_nmi_resume(struct sys_device *dev)
195{
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();
200 }
201 return 0;
202}
203
204static struct sysdev_class nmi_sysclass = {
205 .name = "lapic_nmi",
206 .resume = lapic_nmi_resume,
207 .suspend = lapic_nmi_suspend,
208};
209
210static struct sys_device device_lapic_nmi = {
211 .id = 0,
212 .cls = &nmi_sysclass,
213};
214
215static int __init init_lapic_nmi_sysfs(void)
216{
217 int error;
218
219 /* should really be a BUG_ON but b/c this is an
220 * init call, it just doesn't work. -dcz
221 */
222 if (nmi_watchdog != NMI_LOCAL_APIC)
223 return 0;
224
225 if (atomic_read(&nmi_active) < 0)
226 return 0;
227
228 error = sysdev_class_register(&nmi_sysclass);
229 if (!error)
230 error = sysdev_register(&device_lapic_nmi);
231 return error;
232}
233/* must come after the local APIC's device_initcall() */
234late_initcall(init_lapic_nmi_sysfs);
235
236#endif /* CONFIG_PM */
237
238static void __acpi_nmi_enable(void *__unused)
239{
240 apic_write_around(APIC_LVT0, APIC_DM_NMI);
241}
242
243/*
244 * Enable timer based NMIs on all CPUs:
245 */
246void acpi_nmi_enable(void)
247{
248 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
249 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
250}
251
252static void __acpi_nmi_disable(void *__unused)
253{
254 apic_write_around(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
255}
256
257/*
258 * Disable timer based NMIs on all CPUs:
259 */
260void acpi_nmi_disable(void)
261{
262 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
263 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
264}
265
266void setup_apic_nmi_watchdog(void *unused)
267{
268 if (__get_cpu_var(wd_enabled))
269 return;
270
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))
274 return;
275
276 switch (nmi_watchdog) {
277 case NMI_LOCAL_APIC:
278 __get_cpu_var(wd_enabled) = 1;
279 if (lapic_watchdog_init(nmi_hz) < 0) {
280 __get_cpu_var(wd_enabled) = 0;
281 return;
282 }
283 /* FALL THROUGH */
284 case NMI_IO_APIC:
285 __get_cpu_var(wd_enabled) = 1;
286 atomic_inc(&nmi_active);
287 }
288}
289
290void stop_apic_nmi_watchdog(void *unused)
291{
292 /* only support LOCAL and IO APICs for now */
293 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
294 (nmi_watchdog != NMI_IO_APIC))
295 return;
296 if (__get_cpu_var(wd_enabled) == 0)
297 return;
298 if (nmi_watchdog == NMI_LOCAL_APIC)
299 lapic_watchdog_stop();
300 __get_cpu_var(wd_enabled) = 0;
301 atomic_dec(&nmi_active);
302}
303
304/*
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.
308 *
309 * as these watchdog NMI IRQs are generated on every CPU, we only
310 * have to check the current processor.
311 */
312
313static DEFINE_PER_CPU(unsigned, last_irq_sum);
314static DEFINE_PER_CPU(local_t, alert_counter);
315static DEFINE_PER_CPU(int, nmi_touch);
316
317void touch_nmi_watchdog(void)
318{
319 if (nmi_watchdog > 0) {
320 unsigned cpu;
321
322 /*
323 * Tell other CPUs to reset their alert counters. We cannot
324 * do it ourselves because the alert count increase is not
325 * atomic.
326 */
327 for_each_present_cpu(cpu) {
328 if (per_cpu(nmi_touch, cpu) != 1)
329 per_cpu(nmi_touch, cpu) = 1;
330 }
331 }
332
333 touch_softlockup_watchdog();
334}
335EXPORT_SYMBOL(touch_nmi_watchdog);
336
337notrace __kprobes int
338nmi_watchdog_tick(struct pt_regs *regs, unsigned reason)
339{
340 unsigned int sum;
341 int touched = 0;
342 int cpu = smp_processor_id();
343 int rc = 0;
344
345 /* check for other users first */
346 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
347 == NOTIFY_STOP) {
348 rc = 1;
349 touched = 1;
350 }
351
352 sum = get_timer_irqs(cpu);
353
354 if (__get_cpu_var(nmi_touch)) {
355 __get_cpu_var(nmi_touch) = 0;
356 touched = 1;
357 }
358
359 if (cpu_isset(cpu, backtrace_mask)) {
360 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
361
362 spin_lock(&lock);
363 printk("NMI backtrace for cpu %d\n", cpu);
364 dump_stack();
365 spin_unlock(&lock);
366 cpu_clear(cpu, backtrace_mask);
367 }
368
369 if (mce_in_progress())
370 touched = 1;
371
372 /* if the apic timer isn't firing, this cpu isn't doing much */
373 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
374 /*
375 * Ayiee, looks like this CPU is stuck ...
376 * wait a few IRQs (5 seconds) before doing the oops ...
377 */
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);
382 } else {
383 __get_cpu_var(last_irq_sum) = sum;
384 local_set(&__get_cpu_var(alert_counter), 0);
385 }
386
387 /* see if the nmi watchdog went off */
388 if (!__get_cpu_var(wd_enabled))
389 return rc;
390 switch (nmi_watchdog) {
391 case NMI_LOCAL_APIC:
392 rc |= lapic_wd_event(nmi_hz);
393 break;
394 case NMI_IO_APIC:
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.
398 */
399 rc = 1;
400 break;
401 }
402 return rc;
403}
404
405#ifdef CONFIG_SYSCTL
406
407static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
408{
409 unsigned char reason = get_nmi_reason();
410 char buf[64];
411
412 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
413 die_nmi(buf, regs, 1); /* Always panic here */
414 return 0;
415}
416
417/*
418 * proc handler for /proc/sys/kernel/nmi
419 */
420int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
421 void __user *buffer, size_t *length, loff_t *ppos)
422{
423 int old_state;
424
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)
429 return 0;
430
431 if (atomic_read(&nmi_active) < 0 || nmi_watchdog == NMI_DISABLED) {
432 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
433 return -EIO;
434 }
435
436 /* if nmi_watchdog is not set yet, then set it */
437 nmi_watchdog_default();
438
439 if (nmi_watchdog == NMI_LOCAL_APIC) {
440 if (nmi_watchdog_enabled)
441 enable_lapic_nmi_watchdog();
442 else
443 disable_lapic_nmi_watchdog();
444 } else {
445 printk( KERN_WARNING
446 "NMI watchdog doesn't know what hardware to touch\n");
447 return -EIO;
448 }
449 return 0;
450}
451
452#endif
453
454int do_nmi_callback(struct pt_regs *regs, int cpu)
455{
456#ifdef CONFIG_SYSCTL
457 if (unknown_nmi_panic)
458 return unknown_nmi_panic_callback(regs, cpu);
459#endif
460 return 0;
461}
462
463void __trigger_all_cpu_backtrace(void)
464{
465 int i;
466
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))
471 break;
472 mdelay(1);
473 }
474}
475
476EXPORT_SYMBOL(nmi_active);
477EXPORT_SYMBOL(nmi_watchdog);