blob: 9cb42ecb7f8966bb90bf3426f8b82e0cad9b55cf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/x86_64/nmi.c
3 *
4 * NMI watchdog support on APIC systems
5 *
6 * Started by Ingo Molnar <mingo@redhat.com>
7 *
8 * Fixes:
9 * Mikael Pettersson : AMD K7 support for local APIC NMI watchdog.
10 * Mikael Pettersson : Power Management for local APIC NMI watchdog.
11 * Pavel Machek and
12 * Mikael Pettersson : PM converted to driver model. Disable/enable API.
13 */
14
Andrew Mortonbb81a092006-12-07 02:14:01 +010015#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/sysdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/sysctl.h>
Andi Kleeneddb6fb2006-02-03 21:50:41 +010022#include <linux/kprobes.h>
Andrew Mortonbb81a092006-12-07 02:14:01 +010023#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/proto.h>
28#include <asm/kdebug.h>
Andi Kleen553f2652006-04-07 19:49:57 +020029#include <asm/mce.h>
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +020030#include <asm/intel_arch_perfmon.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Andi Kleen29cbc782006-09-30 01:47:55 +020032int unknown_nmi_panic;
33int nmi_watchdog_enabled;
34int panic_on_unrecovered_nmi;
35
Don Zickus828f0af2006-09-26 10:52:26 +020036/* perfctr_nmi_owner tracks the ownership of the perfctr registers:
37 * evtsel_nmi_owner tracks the ownership of the event selection
38 * - different performance counters/ event selection may be reserved for
39 * different subsystems this reservation system just tries to coordinate
40 * things a little
41 */
42static DEFINE_PER_CPU(unsigned, perfctr_nmi_owner);
43static DEFINE_PER_CPU(unsigned, evntsel_nmi_owner[2]);
44
Andrew Mortonbb81a092006-12-07 02:14:01 +010045static cpumask_t backtrace_mask = CPU_MASK_NONE;
46
Don Zickus828f0af2006-09-26 10:52:26 +020047/* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's
48 * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now)
49 */
50#define NMI_MAX_COUNTER_BITS 66
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* nmi_active:
Don Zickusf2802e72006-09-26 10:52:26 +020053 * >0: the lapic NMI watchdog is active, but can be disabled
54 * <0: the lapic NMI watchdog has not been set up, and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * be enabled
Don Zickusf2802e72006-09-26 10:52:26 +020056 * 0: the lapic NMI watchdog is disabled, but can be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 */
Don Zickusf2802e72006-09-26 10:52:26 +020058atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059int panic_on_timeout;
60
61unsigned int nmi_watchdog = NMI_DEFAULT;
62static unsigned int nmi_hz = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Don Zickusf2802e72006-09-26 10:52:26 +020064struct nmi_watchdog_ctlblk {
65 int enabled;
66 u64 check_bit;
67 unsigned int cccr_msr;
68 unsigned int perfctr_msr; /* the MSR to reset in NMI handler */
69 unsigned int evntsel_msr; /* the MSR to select the events to handle */
70};
71static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Don Zickusf2802e72006-09-26 10:52:26 +020073/* local prototypes */
Don Zickusf2802e72006-09-26 10:52:26 +020074static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
Andi Kleen75152112005-05-16 21:53:34 -070075
Don Zickus828f0af2006-09-26 10:52:26 +020076/* converts an msr to an appropriate reservation bit */
77static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
78{
79 /* returns the bit offset of the performance counter register */
80 switch (boot_cpu_data.x86_vendor) {
81 case X86_VENDOR_AMD:
82 return (msr - MSR_K7_PERFCTR0);
83 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +020084 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
85 return (msr - MSR_ARCH_PERFMON_PERFCTR0);
86 else
87 return (msr - MSR_P4_BPU_PERFCTR0);
Don Zickus828f0af2006-09-26 10:52:26 +020088 }
89 return 0;
90}
91
92/* converts an msr to an appropriate reservation bit */
93static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr)
94{
95 /* returns the bit offset of the event selection register */
96 switch (boot_cpu_data.x86_vendor) {
97 case X86_VENDOR_AMD:
98 return (msr - MSR_K7_EVNTSEL0);
99 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200100 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
101 return (msr - MSR_ARCH_PERFMON_EVENTSEL0);
102 else
103 return (msr - MSR_P4_BSU_ESCR0);
Don Zickus828f0af2006-09-26 10:52:26 +0200104 }
105 return 0;
106}
107
108/* checks for a bit availability (hack for oprofile) */
109int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
110{
111 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
112
113 return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
114}
115
116/* checks the an msr for availability */
117int avail_to_resrv_perfctr_nmi(unsigned int msr)
118{
119 unsigned int counter;
120
121 counter = nmi_perfctr_msr_to_bit(msr);
122 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
123
124 return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
125}
126
127int reserve_perfctr_nmi(unsigned int msr)
128{
129 unsigned int counter;
130
131 counter = nmi_perfctr_msr_to_bit(msr);
132 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
133
134 if (!test_and_set_bit(counter, &__get_cpu_var(perfctr_nmi_owner)))
135 return 1;
136 return 0;
137}
138
139void release_perfctr_nmi(unsigned int msr)
140{
141 unsigned int counter;
142
143 counter = nmi_perfctr_msr_to_bit(msr);
144 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
145
146 clear_bit(counter, &__get_cpu_var(perfctr_nmi_owner));
147}
148
149int reserve_evntsel_nmi(unsigned int msr)
150{
151 unsigned int counter;
152
153 counter = nmi_evntsel_msr_to_bit(msr);
154 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
155
156 if (!test_and_set_bit(counter, &__get_cpu_var(evntsel_nmi_owner)))
157 return 1;
158 return 0;
159}
160
161void release_evntsel_nmi(unsigned int msr)
162{
163 unsigned int counter;
164
165 counter = nmi_evntsel_msr_to_bit(msr);
166 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
167
168 clear_bit(counter, &__get_cpu_var(evntsel_nmi_owner));
169}
170
Ashok Raje6982c62005-06-25 14:54:58 -0700171static __cpuinit inline int nmi_known_cpu(void)
Andi Kleen75152112005-05-16 21:53:34 -0700172{
173 switch (boot_cpu_data.x86_vendor) {
174 case X86_VENDOR_AMD:
175 return boot_cpu_data.x86 == 15;
176 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200177 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
178 return 1;
179 else
180 return (boot_cpu_data.x86 == 15);
Andi Kleen75152112005-05-16 21:53:34 -0700181 }
182 return 0;
183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185/* Run after command line and cpu_init init, but before all other checks */
Don Zickuse33e89a2006-09-26 10:52:27 +0200186void nmi_watchdog_default(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 if (nmi_watchdog != NMI_DEFAULT)
189 return;
Andi Kleen75152112005-05-16 21:53:34 -0700190 if (nmi_known_cpu())
191 nmi_watchdog = NMI_LOCAL_APIC;
192 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 nmi_watchdog = NMI_IO_APIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +0100196static int endflag __initdata = 0;
197
Andi Kleen75152112005-05-16 21:53:34 -0700198#ifdef CONFIG_SMP
199/* The performance counters used by NMI_LOCAL_APIC don't trigger when
200 * the CPU is idle. To make sure the NMI watchdog really ticks on all
201 * CPUs during the test make them busy.
202 */
203static __init void nmi_cpu_busy(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Ingo Molnar366c7f52006-07-03 00:25:25 -0700205 local_irq_enable_in_hardirq();
Andi Kleen75152112005-05-16 21:53:34 -0700206 /* Intentionally don't use cpu_relax here. This is
207 to make sure that the performance counter really ticks,
208 even if there is a simulator or similar that catches the
209 pause instruction. On a real HT machine this is fine because
210 all other CPUs are busy with "useless" delay loops and don't
211 care if they get somewhat less cycles. */
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +0100212 while (endflag == 0)
213 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
Andi Kleen75152112005-05-16 21:53:34 -0700215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Andi Kleen75152112005-05-16 21:53:34 -0700217int __init check_nmi_watchdog (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Andi Kleenac6b9312005-05-16 21:53:19 -0700219 int *counts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 int cpu;
221
Don Zickusf2802e72006-09-26 10:52:26 +0200222 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT))
223 return 0;
224
225 if (!atomic_read(&nmi_active))
226 return 0;
227
Andi Kleen75152112005-05-16 21:53:34 -0700228 counts = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
229 if (!counts)
230 return -1;
Jack F Vogel67701ae2005-05-01 08:58:48 -0700231
Andi Kleen75152112005-05-16 21:53:34 -0700232 printk(KERN_INFO "testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Andi Kleen7554c3f2006-01-11 22:45:45 +0100234#ifdef CONFIG_SMP
Andi Kleen75152112005-05-16 21:53:34 -0700235 if (nmi_watchdog == NMI_LOCAL_APIC)
236 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
Andi Kleen7554c3f2006-01-11 22:45:45 +0100237#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 for (cpu = 0; cpu < NR_CPUS; cpu++)
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100240 counts[cpu] = cpu_pda(cpu)->__nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 local_irq_enable();
242 mdelay((10*1000)/nmi_hz); // wait 10 ticks
243
Andrew Morton394e3902006-03-23 03:01:05 -0800244 for_each_online_cpu(cpu) {
Don Zickusf2802e72006-09-26 10:52:26 +0200245 if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled)
246 continue;
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100247 if (cpu_pda(cpu)->__nmi_count - counts[cpu] <= 5) {
Andi Kleen75152112005-05-16 21:53:34 -0700248 printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 cpu,
Andi Kleen75152112005-05-16 21:53:34 -0700250 counts[cpu],
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100251 cpu_pda(cpu)->__nmi_count);
Don Zickusf2802e72006-09-26 10:52:26 +0200252 per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0;
253 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255 }
Don Zickusf2802e72006-09-26 10:52:26 +0200256 if (!atomic_read(&nmi_active)) {
257 kfree(counts);
258 atomic_set(&nmi_active, -1);
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +0100259 endflag = 1;
Don Zickusf2802e72006-09-26 10:52:26 +0200260 return -1;
261 }
Andi Kleen75152112005-05-16 21:53:34 -0700262 endflag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 printk("OK.\n");
264
265 /* now that we know it works we can reduce NMI frequency to
266 something more reasonable; makes a difference in some configs */
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200267 if (nmi_watchdog == NMI_LOCAL_APIC) {
268 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 nmi_hz = 1;
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200271 /*
272 * On Intel CPUs with ARCH_PERFMON only 32 bits in the counter
273 * are writable, with higher bits sign extending from bit 31.
274 * So, we can only program the counter with 31 bit values and
275 * 32nd bit should be 1, for 33.. to be 1.
276 * Find the appropriate nmi_hz
277 */
278 if (wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0 &&
279 ((u64)cpu_khz * 1000) > 0x7fffffffULL) {
280 nmi_hz = ((u64)cpu_khz * 1000) / 0x7fffffffUL + 1;
281 }
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Andi Kleenac6b9312005-05-16 21:53:19 -0700284 kfree(counts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return 0;
286}
287
288int __init setup_nmi_watchdog(char *str)
289{
290 int nmi;
291
292 if (!strncmp(str,"panic",5)) {
293 panic_on_timeout = 1;
294 str = strchr(str, ',');
295 if (!str)
296 return 1;
297 ++str;
298 }
299
300 get_option(&str, &nmi);
301
Don Zickusf2802e72006-09-26 10:52:26 +0200302 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200304
Andi Kleen75152112005-05-16 21:53:34 -0700305 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 1;
307}
308
309__setup("nmi_watchdog=", setup_nmi_watchdog);
310
311static void disable_lapic_nmi_watchdog(void)
312{
Don Zickusf2802e72006-09-26 10:52:26 +0200313 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
314
315 if (atomic_read(&nmi_active) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return;
Don Zickusf2802e72006-09-26 10:52:26 +0200317
318 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
319
320 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323static void enable_lapic_nmi_watchdog(void)
324{
Don Zickusf2802e72006-09-26 10:52:26 +0200325 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
326
327 /* are we already enabled */
328 if (atomic_read(&nmi_active) != 0)
329 return;
330
331 /* are we lapic aware */
332 if (nmi_known_cpu() <= 0)
333 return;
334
335 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
336 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339void disable_timer_nmi_watchdog(void)
340{
Don Zickusf2802e72006-09-26 10:52:26 +0200341 BUG_ON(nmi_watchdog != NMI_IO_APIC);
342
343 if (atomic_read(&nmi_active) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return;
345
346 disable_irq(0);
Don Zickusf2802e72006-09-26 10:52:26 +0200347 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
348
349 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352void enable_timer_nmi_watchdog(void)
353{
Don Zickusf2802e72006-09-26 10:52:26 +0200354 BUG_ON(nmi_watchdog != NMI_IO_APIC);
355
356 if (atomic_read(&nmi_active) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 touch_nmi_watchdog();
Don Zickusf2802e72006-09-26 10:52:26 +0200358 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 enable_irq(0);
360 }
361}
362
363#ifdef CONFIG_PM
364
365static int nmi_pm_active; /* nmi_active before suspend */
366
Pavel Machek829ca9a2005-09-03 15:56:56 -0700367static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Shaohua Li4038f902006-09-26 10:52:27 +0200369 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusf2802e72006-09-26 10:52:26 +0200370 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200371 stop_apic_nmi_watchdog(NULL);
372 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374}
375
376static int lapic_nmi_resume(struct sys_device *dev)
377{
Shaohua Li4038f902006-09-26 10:52:27 +0200378 /* only CPU0 goes here, other CPUs should be offline */
379 if (nmi_pm_active > 0) {
380 setup_apic_nmi_watchdog(NULL);
381 touch_nmi_watchdog();
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return 0;
384}
385
386static struct sysdev_class nmi_sysclass = {
387 set_kset_name("lapic_nmi"),
388 .resume = lapic_nmi_resume,
389 .suspend = lapic_nmi_suspend,
390};
391
392static struct sys_device device_lapic_nmi = {
393 .id = 0,
394 .cls = &nmi_sysclass,
395};
396
397static int __init init_lapic_nmi_sysfs(void)
398{
399 int error;
400
Don Zickusf2802e72006-09-26 10:52:26 +0200401 /* should really be a BUG_ON but b/c this is an
402 * init call, it just doesn't work. -dcz
403 */
404 if (nmi_watchdog != NMI_LOCAL_APIC)
405 return 0;
406
407 if ( atomic_read(&nmi_active) < 0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0;
409
410 error = sysdev_class_register(&nmi_sysclass);
411 if (!error)
412 error = sysdev_register(&device_lapic_nmi);
413 return error;
414}
415/* must come after the local APIC's device_initcall() */
416late_initcall(init_lapic_nmi_sysfs);
417
418#endif /* CONFIG_PM */
419
Don Zickusf2802e72006-09-26 10:52:26 +0200420/*
421 * Activate the NMI watchdog via the local APIC.
422 * Original code written by Keith Owens.
423 */
424
425/* Note that these events don't tick when the CPU idles. This means
426 the frequency varies with CPU load. */
427
428#define K7_EVNTSEL_ENABLE (1 << 22)
429#define K7_EVNTSEL_INT (1 << 20)
430#define K7_EVNTSEL_OS (1 << 17)
431#define K7_EVNTSEL_USR (1 << 16)
432#define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
433#define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
434
Don Zickus828f0af2006-09-26 10:52:26 +0200435static int setup_k7_watchdog(void)
Andi Kleen75152112005-05-16 21:53:34 -0700436{
Don Zickusf2802e72006-09-26 10:52:26 +0200437 unsigned int perfctr_msr, evntsel_msr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 unsigned int evntsel;
Don Zickusf2802e72006-09-26 10:52:26 +0200439 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Don Zickusf2802e72006-09-26 10:52:26 +0200441 perfctr_msr = MSR_K7_PERFCTR0;
442 evntsel_msr = MSR_K7_EVNTSEL0;
443 if (!reserve_perfctr_nmi(perfctr_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200444 goto fail;
445
Don Zickusf2802e72006-09-26 10:52:26 +0200446 if (!reserve_evntsel_nmi(evntsel_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200447 goto fail1;
448
449 /* Simulator may not support it */
Don Zickusf2802e72006-09-26 10:52:26 +0200450 if (checking_wrmsrl(evntsel_msr, 0UL))
Don Zickus828f0af2006-09-26 10:52:26 +0200451 goto fail2;
Don Zickusf2802e72006-09-26 10:52:26 +0200452 wrmsrl(perfctr_msr, 0UL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 evntsel = K7_EVNTSEL_INT
455 | K7_EVNTSEL_OS
456 | K7_EVNTSEL_USR
457 | K7_NMI_EVENT;
458
Don Zickusf2802e72006-09-26 10:52:26 +0200459 /* setup the timer */
460 wrmsr(evntsel_msr, evntsel, 0);
461 wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 apic_write(APIC_LVTPC, APIC_DM_NMI);
463 evntsel |= K7_EVNTSEL_ENABLE;
Don Zickusf2802e72006-09-26 10:52:26 +0200464 wrmsr(evntsel_msr, evntsel, 0);
465
466 wd->perfctr_msr = perfctr_msr;
467 wd->evntsel_msr = evntsel_msr;
468 wd->cccr_msr = 0; //unused
469 wd->check_bit = 1ULL<<63;
Don Zickus828f0af2006-09-26 10:52:26 +0200470 return 1;
471fail2:
Don Zickusf2802e72006-09-26 10:52:26 +0200472 release_evntsel_nmi(evntsel_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200473fail1:
Don Zickusf2802e72006-09-26 10:52:26 +0200474 release_perfctr_nmi(perfctr_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200475fail:
476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Don Zickusf2802e72006-09-26 10:52:26 +0200479static void stop_k7_watchdog(void)
480{
481 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
482
483 wrmsr(wd->evntsel_msr, 0, 0);
484
485 release_evntsel_nmi(wd->evntsel_msr);
486 release_perfctr_nmi(wd->perfctr_msr);
487}
488
489/* Note that these events don't tick when the CPU idles. This means
490 the frequency varies with CPU load. */
491
492#define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
493#define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
494#define P4_ESCR_OS (1<<3)
495#define P4_ESCR_USR (1<<2)
496#define P4_CCCR_OVF_PMI0 (1<<26)
497#define P4_CCCR_OVF_PMI1 (1<<27)
498#define P4_CCCR_THRESHOLD(N) ((N)<<20)
499#define P4_CCCR_COMPLEMENT (1<<19)
500#define P4_CCCR_COMPARE (1<<18)
501#define P4_CCCR_REQUIRED (3<<16)
502#define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
503#define P4_CCCR_ENABLE (1<<12)
504#define P4_CCCR_OVF (1<<31)
505/* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
506 CRU_ESCR0 (with any non-null event selector) through a complemented
507 max threshold. [IA32-Vol3, Section 14.9.9] */
Andi Kleen75152112005-05-16 21:53:34 -0700508
509static int setup_p4_watchdog(void)
510{
Don Zickusf2802e72006-09-26 10:52:26 +0200511 unsigned int perfctr_msr, evntsel_msr, cccr_msr;
512 unsigned int evntsel, cccr_val;
Andi Kleen75152112005-05-16 21:53:34 -0700513 unsigned int misc_enable, dummy;
Don Zickusf2802e72006-09-26 10:52:26 +0200514 unsigned int ht_num;
515 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Andi Kleen75152112005-05-16 21:53:34 -0700516
Don Zickusf2802e72006-09-26 10:52:26 +0200517 rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy);
Andi Kleen75152112005-05-16 21:53:34 -0700518 if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
519 return 0;
520
Andi Kleen75152112005-05-16 21:53:34 -0700521#ifdef CONFIG_SMP
Don Zickusf2802e72006-09-26 10:52:26 +0200522 /* detect which hyperthread we are on */
523 if (smp_num_siblings == 2) {
524 unsigned int ebx, apicid;
Andi Kleen75152112005-05-16 21:53:34 -0700525
Don Zickusf2802e72006-09-26 10:52:26 +0200526 ebx = cpuid_ebx(1);
527 apicid = (ebx >> 24) & 0xff;
528 ht_num = apicid & 1;
529 } else
530#endif
531 ht_num = 0;
532
533 /* performance counters are shared resources
534 * assign each hyperthread its own set
535 * (re-use the ESCR0 register, seems safe
536 * and keeps the cccr_val the same)
537 */
538 if (!ht_num) {
539 /* logical cpu 0 */
540 perfctr_msr = MSR_P4_IQ_PERFCTR0;
541 evntsel_msr = MSR_P4_CRU_ESCR0;
542 cccr_msr = MSR_P4_IQ_CCCR0;
543 cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4);
544 } else {
545 /* logical cpu 1 */
546 perfctr_msr = MSR_P4_IQ_PERFCTR1;
547 evntsel_msr = MSR_P4_CRU_ESCR0;
548 cccr_msr = MSR_P4_IQ_CCCR1;
549 cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4);
550 }
551
552 if (!reserve_perfctr_nmi(perfctr_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200553 goto fail;
554
Don Zickusf2802e72006-09-26 10:52:26 +0200555 if (!reserve_evntsel_nmi(evntsel_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200556 goto fail1;
Andi Kleen75152112005-05-16 21:53:34 -0700557
Don Zickusf2802e72006-09-26 10:52:26 +0200558 evntsel = P4_ESCR_EVENT_SELECT(0x3F)
559 | P4_ESCR_OS
560 | P4_ESCR_USR;
561
562 cccr_val |= P4_CCCR_THRESHOLD(15)
563 | P4_CCCR_COMPLEMENT
564 | P4_CCCR_COMPARE
565 | P4_CCCR_REQUIRED;
566
567 wrmsr(evntsel_msr, evntsel, 0);
568 wrmsr(cccr_msr, cccr_val, 0);
569 wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
Andi Kleen75152112005-05-16 21:53:34 -0700570 apic_write(APIC_LVTPC, APIC_DM_NMI);
Don Zickusf2802e72006-09-26 10:52:26 +0200571 cccr_val |= P4_CCCR_ENABLE;
572 wrmsr(cccr_msr, cccr_val, 0);
573
574 wd->perfctr_msr = perfctr_msr;
575 wd->evntsel_msr = evntsel_msr;
576 wd->cccr_msr = cccr_msr;
577 wd->check_bit = 1ULL<<39;
Andi Kleen75152112005-05-16 21:53:34 -0700578 return 1;
Don Zickus828f0af2006-09-26 10:52:26 +0200579fail1:
Don Zickusf2802e72006-09-26 10:52:26 +0200580 release_perfctr_nmi(perfctr_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200581fail:
582 return 0;
Andi Kleen75152112005-05-16 21:53:34 -0700583}
584
Don Zickusf2802e72006-09-26 10:52:26 +0200585static void stop_p4_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Don Zickusf2802e72006-09-26 10:52:26 +0200587 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Andi Kleen75152112005-05-16 21:53:34 -0700588
Don Zickusf2802e72006-09-26 10:52:26 +0200589 wrmsr(wd->cccr_msr, 0, 0);
590 wrmsr(wd->evntsel_msr, 0, 0);
591
592 release_evntsel_nmi(wd->evntsel_msr);
593 release_perfctr_nmi(wd->perfctr_msr);
594}
595
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200596#define ARCH_PERFMON_NMI_EVENT_SEL ARCH_PERFMON_UNHALTED_CORE_CYCLES_SEL
597#define ARCH_PERFMON_NMI_EVENT_UMASK ARCH_PERFMON_UNHALTED_CORE_CYCLES_UMASK
598
599static int setup_intel_arch_watchdog(void)
600{
601 unsigned int ebx;
602 union cpuid10_eax eax;
603 unsigned int unused;
604 unsigned int perfctr_msr, evntsel_msr;
605 unsigned int evntsel;
606 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
607
608 /*
609 * Check whether the Architectural PerfMon supports
610 * Unhalted Core Cycles Event or not.
611 * NOTE: Corresponding bit = 0 in ebx indicates event present.
612 */
613 cpuid(10, &(eax.full), &ebx, &unused, &unused);
614 if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
615 (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
616 goto fail;
617
618 perfctr_msr = MSR_ARCH_PERFMON_PERFCTR0;
619 evntsel_msr = MSR_ARCH_PERFMON_EVENTSEL0;
620
621 if (!reserve_perfctr_nmi(perfctr_msr))
622 goto fail;
623
624 if (!reserve_evntsel_nmi(evntsel_msr))
625 goto fail1;
626
627 wrmsrl(perfctr_msr, 0UL);
628
629 evntsel = ARCH_PERFMON_EVENTSEL_INT
630 | ARCH_PERFMON_EVENTSEL_OS
631 | ARCH_PERFMON_EVENTSEL_USR
632 | ARCH_PERFMON_NMI_EVENT_SEL
633 | ARCH_PERFMON_NMI_EVENT_UMASK;
634
635 /* setup the timer */
636 wrmsr(evntsel_msr, evntsel, 0);
637 wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
638
639 apic_write(APIC_LVTPC, APIC_DM_NMI);
640 evntsel |= ARCH_PERFMON_EVENTSEL0_ENABLE;
641 wrmsr(evntsel_msr, evntsel, 0);
642
643 wd->perfctr_msr = perfctr_msr;
644 wd->evntsel_msr = evntsel_msr;
645 wd->cccr_msr = 0; //unused
646 wd->check_bit = 1ULL << (eax.split.bit_width - 1);
647 return 1;
648fail1:
649 release_perfctr_nmi(perfctr_msr);
650fail:
651 return 0;
652}
653
654static void stop_intel_arch_watchdog(void)
655{
656 unsigned int ebx;
657 union cpuid10_eax eax;
658 unsigned int unused;
659 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
660
661 /*
662 * Check whether the Architectural PerfMon supports
663 * Unhalted Core Cycles Event or not.
664 * NOTE: Corresponding bit = 0 in ebx indicates event present.
665 */
666 cpuid(10, &(eax.full), &ebx, &unused, &unused);
667 if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
668 (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
669 return;
670
671 wrmsr(wd->evntsel_msr, 0, 0);
672
673 release_evntsel_nmi(wd->evntsel_msr);
674 release_perfctr_nmi(wd->perfctr_msr);
675}
676
Don Zickusf2802e72006-09-26 10:52:26 +0200677void setup_apic_nmi_watchdog(void *unused)
678{
Shaohua Li4038f902006-09-26 10:52:27 +0200679 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
680
Don Zickusf2802e72006-09-26 10:52:26 +0200681 /* only support LOCAL and IO APICs for now */
682 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
683 (nmi_watchdog != NMI_IO_APIC))
684 return;
685
Shaohua Li4038f902006-09-26 10:52:27 +0200686 if (wd->enabled == 1)
687 return;
688
689 /* cheap hack to support suspend/resume */
690 /* if cpu0 is not active neither should the other cpus */
691 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
692 return;
693
Don Zickusf2802e72006-09-26 10:52:26 +0200694 if (nmi_watchdog == NMI_LOCAL_APIC) {
695 switch (boot_cpu_data.x86_vendor) {
696 case X86_VENDOR_AMD:
697 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
698 return;
699 if (!setup_k7_watchdog())
700 return;
701 break;
702 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200703 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
704 if (!setup_intel_arch_watchdog())
705 return;
706 break;
707 }
Don Zickusf2802e72006-09-26 10:52:26 +0200708 if (!setup_p4_watchdog())
709 return;
710 break;
711 default:
712 return;
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Shaohua Li4038f902006-09-26 10:52:27 +0200715 wd->enabled = 1;
Don Zickusf2802e72006-09-26 10:52:26 +0200716 atomic_inc(&nmi_active);
717}
718
Shaohua Li4038f902006-09-26 10:52:27 +0200719void stop_apic_nmi_watchdog(void *unused)
Don Zickusf2802e72006-09-26 10:52:26 +0200720{
Shaohua Li4038f902006-09-26 10:52:27 +0200721 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
722
Don Zickusf2802e72006-09-26 10:52:26 +0200723 /* only support LOCAL and IO APICs for now */
724 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
725 (nmi_watchdog != NMI_IO_APIC))
726 return;
727
Shaohua Li4038f902006-09-26 10:52:27 +0200728 if (wd->enabled == 0)
729 return;
730
Don Zickusf2802e72006-09-26 10:52:26 +0200731 if (nmi_watchdog == NMI_LOCAL_APIC) {
732 switch (boot_cpu_data.x86_vendor) {
733 case X86_VENDOR_AMD:
734 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
735 return;
736 stop_k7_watchdog();
737 break;
738 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200739 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
740 stop_intel_arch_watchdog();
741 break;
742 }
Don Zickusf2802e72006-09-26 10:52:26 +0200743 stop_p4_watchdog();
744 break;
745 default:
746 return;
747 }
748 }
Shaohua Li4038f902006-09-26 10:52:27 +0200749 wd->enabled = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200750 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753/*
754 * the best way to detect whether a CPU has a 'hard lockup' problem
755 * is to check it's local APIC timer IRQ counts. If they are not
756 * changing then that CPU has some problem.
757 *
758 * as these watchdog NMI IRQs are generated on every CPU, we only
759 * have to check the current processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 */
761
Andi Kleen75152112005-05-16 21:53:34 -0700762static DEFINE_PER_CPU(unsigned, last_irq_sum);
763static DEFINE_PER_CPU(local_t, alert_counter);
764static DEFINE_PER_CPU(int, nmi_touch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766void touch_nmi_watchdog (void)
767{
Jan Beulich99019e92006-02-16 23:41:55 +0100768 if (nmi_watchdog > 0) {
769 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Jan Beulich99019e92006-02-16 23:41:55 +0100771 /*
772 * Tell other CPUs to reset their alert counters. We cannot
773 * do it ourselves because the alert count increase is not
774 * atomic.
775 */
776 for_each_present_cpu (cpu)
777 per_cpu(nmi_touch, cpu) = 1;
778 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700779
780 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Don Zickus3adbbcce2006-09-26 10:52:26 +0200783int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Andi Kleen75152112005-05-16 21:53:34 -0700785 int sum;
786 int touched = 0;
Andrew Mortonbb81a092006-12-07 02:14:01 +0100787 int cpu = smp_processor_id();
Don Zickusf2802e72006-09-26 10:52:26 +0200788 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
789 u64 dummy;
Don Zickus3adbbcce2006-09-26 10:52:26 +0200790 int rc=0;
Don Zickusf2802e72006-09-26 10:52:26 +0200791
792 /* check for other users first */
793 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
794 == NOTIFY_STOP) {
Don Zickus3adbbcce2006-09-26 10:52:26 +0200795 rc = 1;
Don Zickusf2802e72006-09-26 10:52:26 +0200796 touched = 1;
797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 sum = read_pda(apic_timer_irqs);
Andi Kleen75152112005-05-16 21:53:34 -0700800 if (__get_cpu_var(nmi_touch)) {
801 __get_cpu_var(nmi_touch) = 0;
802 touched = 1;
803 }
Don Zickusf2802e72006-09-26 10:52:26 +0200804
Andrew Mortonbb81a092006-12-07 02:14:01 +0100805 if (cpu_isset(cpu, backtrace_mask)) {
806 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
807
808 spin_lock(&lock);
809 printk("NMI backtrace for cpu %d\n", cpu);
810 dump_stack();
811 spin_unlock(&lock);
812 cpu_clear(cpu, backtrace_mask);
813 }
814
Andi Kleen553f2652006-04-07 19:49:57 +0200815#ifdef CONFIG_X86_MCE
816 /* Could check oops_in_progress here too, but it's safer
817 not too */
818 if (atomic_read(&mce_entry) > 0)
819 touched = 1;
820#endif
Don Zickusf2802e72006-09-26 10:52:26 +0200821 /* if the apic timer isn't firing, this cpu isn't doing much */
Andi Kleen75152112005-05-16 21:53:34 -0700822 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /*
824 * Ayiee, looks like this CPU is stuck ...
825 * wait a few IRQs (5 seconds) before doing the oops ...
826 */
Andi Kleen75152112005-05-16 21:53:34 -0700827 local_inc(&__get_cpu_var(alert_counter));
Don Zickusf2802e72006-09-26 10:52:26 +0200828 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
Andi Kleenfac58552006-09-26 10:52:27 +0200829 die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs,
830 panic_on_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 } else {
Andi Kleen75152112005-05-16 21:53:34 -0700832 __get_cpu_var(last_irq_sum) = sum;
833 local_set(&__get_cpu_var(alert_counter), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
Don Zickusf2802e72006-09-26 10:52:26 +0200835
836 /* see if the nmi watchdog went off */
837 if (wd->enabled) {
838 if (nmi_watchdog == NMI_LOCAL_APIC) {
839 rdmsrl(wd->perfctr_msr, dummy);
840 if (dummy & wd->check_bit){
841 /* this wasn't a watchdog timer interrupt */
842 goto done;
843 }
844
845 /* only Intel uses the cccr msr */
846 if (wd->cccr_msr != 0) {
847 /*
848 * P4 quirks:
849 * - An overflown perfctr will assert its interrupt
850 * until the OVF flag in its CCCR is cleared.
851 * - LVTPC is masked on interrupt and must be
852 * unmasked by the LVTPC handler.
853 */
854 rdmsrl(wd->cccr_msr, dummy);
855 dummy &= ~P4_CCCR_OVF;
856 wrmsrl(wd->cccr_msr, dummy);
857 apic_write(APIC_LVTPC, APIC_DM_NMI);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200858 } else if (wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0) {
859 /*
860 * ArchPerfom/Core Duo needs to re-unmask
861 * the apic vector
862 */
863 apic_write(APIC_LVTPC, APIC_DM_NMI);
864 }
Don Zickusf2802e72006-09-26 10:52:26 +0200865 /* start the cycle over again */
866 wrmsrl(wd->perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
Don Zickus3adbbcce2006-09-26 10:52:26 +0200867 rc = 1;
868 } else if (nmi_watchdog == NMI_IO_APIC) {
869 /* don't know how to accurately check for this.
870 * just assume it was a watchdog timer interrupt
871 * This matches the old behaviour.
872 */
873 rc = 1;
874 } else
875 printk(KERN_WARNING "Unknown enabled NMI hardware?!\n");
Andi Kleen75152112005-05-16 21:53:34 -0700876 }
Don Zickusf2802e72006-09-26 10:52:26 +0200877done:
Don Zickus3adbbcce2006-09-26 10:52:26 +0200878 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100881asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 nmi_enter();
884 add_pda(__nmi_count,1);
Don Zickus3adbbcce2006-09-26 10:52:26 +0200885 default_do_nmi(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 nmi_exit();
887}
888
Don Zickus3adbbcce2006-09-26 10:52:26 +0200889int do_nmi_callback(struct pt_regs * regs, int cpu)
890{
Don Zickus2fbe7b22006-09-26 10:52:27 +0200891#ifdef CONFIG_SYSCTL
892 if (unknown_nmi_panic)
893 return unknown_nmi_panic_callback(regs, cpu);
894#endif
895 return 0;
Don Zickus3adbbcce2006-09-26 10:52:26 +0200896}
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898#ifdef CONFIG_SYSCTL
899
900static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
901{
902 unsigned char reason = get_nmi_reason();
903 char buf[64];
904
Don Zickus2fbe7b22006-09-26 10:52:27 +0200905 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
Andi Kleenfac58552006-09-26 10:52:27 +0200906 die_nmi(buf, regs, 1); /* Always panic here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return 0;
908}
909
Don Zickus407984f2006-09-26 10:52:27 +0200910/*
911 * proc handler for /proc/sys/kernel/nmi
912 */
913int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
914 void __user *buffer, size_t *length, loff_t *ppos)
915{
916 int old_state;
917
918 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
919 old_state = nmi_watchdog_enabled;
920 proc_dointvec(table, write, file, buffer, length, ppos);
921 if (!!old_state == !!nmi_watchdog_enabled)
922 return 0;
923
924 if (atomic_read(&nmi_active) < 0) {
925 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
Don Zickuse33e89a2006-09-26 10:52:27 +0200926 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +0200927 }
928
929 /* if nmi_watchdog is not set yet, then set it */
930 nmi_watchdog_default();
931
Don Zickuse33e89a2006-09-26 10:52:27 +0200932 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +0200933 if (nmi_watchdog_enabled)
934 enable_lapic_nmi_watchdog();
935 else
936 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +0200937 } else {
Don Zickuse33e89a2006-09-26 10:52:27 +0200938 printk( KERN_WARNING
Don Zickus407984f2006-09-26 10:52:27 +0200939 "NMI watchdog doesn't know what hardware to touch\n");
940 return -EIO;
941 }
942 return 0;
943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945#endif
946
Andrew Mortonbb81a092006-12-07 02:14:01 +0100947void __trigger_all_cpu_backtrace(void)
948{
949 int i;
950
951 backtrace_mask = cpu_online_map;
952 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
953 for (i = 0; i < 10 * 1000; i++) {
954 if (cpus_empty(backtrace_mask))
955 break;
956 mdelay(1);
957 }
958}
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960EXPORT_SYMBOL(nmi_active);
961EXPORT_SYMBOL(nmi_watchdog);
Don Zickus828f0af2006-09-26 10:52:26 +0200962EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi);
963EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
964EXPORT_SYMBOL(reserve_perfctr_nmi);
965EXPORT_SYMBOL(release_perfctr_nmi);
966EXPORT_SYMBOL(reserve_evntsel_nmi);
967EXPORT_SYMBOL(release_evntsel_nmi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968EXPORT_SYMBOL(disable_timer_nmi_watchdog);
969EXPORT_SYMBOL(enable_timer_nmi_watchdog);
970EXPORT_SYMBOL(touch_nmi_watchdog);