[PATCH] Only allocate percpu data for possible CPUs
[linux-block.git] / arch / x86_64 / kernel / nmi.c
CommitLineData
1da177e4
LT
1/*
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
15#include <linux/config.h>
16#include <linux/mm.h>
1da177e4
LT
17#include <linux/delay.h>
18#include <linux/bootmem.h>
19#include <linux/smp_lock.h>
20#include <linux/interrupt.h>
21#include <linux/mc146818rtc.h>
22#include <linux/kernel_stat.h>
23#include <linux/module.h>
24#include <linux/sysdev.h>
25#include <linux/nmi.h>
26#include <linux/sysctl.h>
eddb6fb9 27#include <linux/kprobes.h>
1da177e4
LT
28
29#include <asm/smp.h>
30#include <asm/mtrr.h>
31#include <asm/mpspec.h>
32#include <asm/nmi.h>
33#include <asm/msr.h>
34#include <asm/proto.h>
35#include <asm/kdebug.h>
75152114 36#include <asm/local.h>
1da177e4
LT
37
38/*
39 * lapic_nmi_owner tracks the ownership of the lapic NMI hardware:
40 * - it may be reserved by some other driver, or not
41 * - when not reserved by some other driver, it may be used for
42 * the NMI watchdog, or not
43 *
44 * This is maintained separately from nmi_active because the NMI
45 * watchdog may also be driven from the I/O APIC timer.
46 */
47static DEFINE_SPINLOCK(lapic_nmi_owner_lock);
48static unsigned int lapic_nmi_owner;
49#define LAPIC_NMI_WATCHDOG (1<<0)
50#define LAPIC_NMI_RESERVED (1<<1)
51
52/* nmi_active:
53 * +1: the lapic NMI watchdog is active, but can be disabled
54 * 0: the lapic NMI watchdog has not been set up, and cannot
55 * be enabled
56 * -1: the lapic NMI watchdog is disabled, but can be enabled
57 */
58int nmi_active; /* oprofile uses this */
59int panic_on_timeout;
60
61unsigned int nmi_watchdog = NMI_DEFAULT;
62static unsigned int nmi_hz = HZ;
75152114
AK
63static unsigned int nmi_perfctr_msr; /* the MSR to reset in NMI handler */
64static unsigned int nmi_p4_cccr_val;
1da177e4
LT
65
66/* Note that these events don't tick when the CPU idles. This means
67 the frequency varies with CPU load. */
68
69#define K7_EVNTSEL_ENABLE (1 << 22)
70#define K7_EVNTSEL_INT (1 << 20)
71#define K7_EVNTSEL_OS (1 << 17)
72#define K7_EVNTSEL_USR (1 << 16)
73#define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
74#define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
75
75152114
AK
76#define MSR_P4_MISC_ENABLE 0x1A0
77#define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
78#define MSR_P4_MISC_ENABLE_PEBS_UNAVAIL (1<<12)
79#define MSR_P4_PERFCTR0 0x300
80#define MSR_P4_CCCR0 0x360
81#define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
82#define P4_ESCR_OS (1<<3)
83#define P4_ESCR_USR (1<<2)
84#define P4_CCCR_OVF_PMI0 (1<<26)
85#define P4_CCCR_OVF_PMI1 (1<<27)
86#define P4_CCCR_THRESHOLD(N) ((N)<<20)
87#define P4_CCCR_COMPLEMENT (1<<19)
88#define P4_CCCR_COMPARE (1<<18)
89#define P4_CCCR_REQUIRED (3<<16)
90#define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
91#define P4_CCCR_ENABLE (1<<12)
92/* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
93 CRU_ESCR0 (with any non-null event selector) through a complemented
94 max threshold. [IA32-Vol3, Section 14.9.9] */
95#define MSR_P4_IQ_COUNTER0 0x30C
96#define P4_NMI_CRU_ESCR0 (P4_ESCR_EVENT_SELECT(0x3F)|P4_ESCR_OS|P4_ESCR_USR)
97#define P4_NMI_IQ_CCCR0 \
98 (P4_CCCR_OVF_PMI0|P4_CCCR_THRESHOLD(15)|P4_CCCR_COMPLEMENT| \
99 P4_CCCR_COMPARE|P4_CCCR_REQUIRED|P4_CCCR_ESCR_SELECT(4)|P4_CCCR_ENABLE)
100
e6982c67 101static __cpuinit inline int nmi_known_cpu(void)
75152114
AK
102{
103 switch (boot_cpu_data.x86_vendor) {
104 case X86_VENDOR_AMD:
105 return boot_cpu_data.x86 == 15;
106 case X86_VENDOR_INTEL:
107 return boot_cpu_data.x86 == 15;
108 }
109 return 0;
110}
1da177e4
LT
111
112/* Run after command line and cpu_init init, but before all other checks */
e6982c67 113void __cpuinit nmi_watchdog_default(void)
1da177e4
LT
114{
115 if (nmi_watchdog != NMI_DEFAULT)
116 return;
75152114
AK
117 if (nmi_known_cpu())
118 nmi_watchdog = NMI_LOCAL_APIC;
119 else
1da177e4 120 nmi_watchdog = NMI_IO_APIC;
1da177e4
LT
121}
122
75152114
AK
123#ifdef CONFIG_SMP
124/* The performance counters used by NMI_LOCAL_APIC don't trigger when
125 * the CPU is idle. To make sure the NMI watchdog really ticks on all
126 * CPUs during the test make them busy.
127 */
128static __init void nmi_cpu_busy(void *data)
1da177e4 129{
75152114
AK
130 volatile int *endflag = data;
131 local_irq_enable();
132 /* Intentionally don't use cpu_relax here. This is
133 to make sure that the performance counter really ticks,
134 even if there is a simulator or similar that catches the
135 pause instruction. On a real HT machine this is fine because
136 all other CPUs are busy with "useless" delay loops and don't
137 care if they get somewhat less cycles. */
138 while (*endflag == 0)
139 barrier();
1da177e4 140}
75152114 141#endif
1da177e4 142
75152114 143int __init check_nmi_watchdog (void)
1da177e4 144{
75152114 145 volatile int endflag = 0;
ac6b931c 146 int *counts;
1da177e4
LT
147 int cpu;
148
75152114
AK
149 counts = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
150 if (!counts)
151 return -1;
1da177e4 152
75152114 153 printk(KERN_INFO "testing NMI watchdog ... ");
ac6b931c 154
7554c3f0 155#ifdef CONFIG_SMP
75152114
AK
156 if (nmi_watchdog == NMI_LOCAL_APIC)
157 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
7554c3f0 158#endif
1da177e4
LT
159
160 for (cpu = 0; cpu < NR_CPUS; cpu++)
df79efde 161 counts[cpu] = cpu_pda(cpu)->__nmi_count;
1da177e4
LT
162 local_irq_enable();
163 mdelay((10*1000)/nmi_hz); // wait 10 ticks
164
165 for (cpu = 0; cpu < NR_CPUS; cpu++) {
75152114
AK
166 if (!cpu_online(cpu))
167 continue;
df79efde 168 if (cpu_pda(cpu)->__nmi_count - counts[cpu] <= 5) {
75152114
AK
169 endflag = 1;
170 printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
1da177e4 171 cpu,
75152114 172 counts[cpu],
df79efde 173 cpu_pda(cpu)->__nmi_count);
1da177e4
LT
174 nmi_active = 0;
175 lapic_nmi_owner &= ~LAPIC_NMI_WATCHDOG;
75152114 176 nmi_perfctr_msr = 0;
ac6b931c 177 kfree(counts);
1da177e4
LT
178 return -1;
179 }
180 }
75152114 181 endflag = 1;
1da177e4
LT
182 printk("OK.\n");
183
184 /* now that we know it works we can reduce NMI frequency to
185 something more reasonable; makes a difference in some configs */
186 if (nmi_watchdog == NMI_LOCAL_APIC)
187 nmi_hz = 1;
188
ac6b931c 189 kfree(counts);
1da177e4
LT
190 return 0;
191}
192
193int __init setup_nmi_watchdog(char *str)
194{
195 int nmi;
196
197 if (!strncmp(str,"panic",5)) {
198 panic_on_timeout = 1;
199 str = strchr(str, ',');
200 if (!str)
201 return 1;
202 ++str;
203 }
204
205 get_option(&str, &nmi);
206
207 if (nmi >= NMI_INVALID)
208 return 0;
75152114 209 nmi_watchdog = nmi;
1da177e4
LT
210 return 1;
211}
212
213__setup("nmi_watchdog=", setup_nmi_watchdog);
214
215static void disable_lapic_nmi_watchdog(void)
216{
217 if (nmi_active <= 0)
218 return;
219 switch (boot_cpu_data.x86_vendor) {
220 case X86_VENDOR_AMD:
221 wrmsr(MSR_K7_EVNTSEL0, 0, 0);
222 break;
223 case X86_VENDOR_INTEL:
75152114
AK
224 if (boot_cpu_data.x86 == 15) {
225 wrmsr(MSR_P4_IQ_CCCR0, 0, 0);
226 wrmsr(MSR_P4_CRU_ESCR0, 0, 0);
227 }
1da177e4
LT
228 break;
229 }
230 nmi_active = -1;
231 /* tell do_nmi() and others that we're not active any more */
232 nmi_watchdog = 0;
233}
234
235static void enable_lapic_nmi_watchdog(void)
236{
237 if (nmi_active < 0) {
238 nmi_watchdog = NMI_LOCAL_APIC;
99019e91 239 touch_nmi_watchdog();
1da177e4
LT
240 setup_apic_nmi_watchdog();
241 }
242}
243
244int reserve_lapic_nmi(void)
245{
246 unsigned int old_owner;
247
248 spin_lock(&lapic_nmi_owner_lock);
249 old_owner = lapic_nmi_owner;
250 lapic_nmi_owner |= LAPIC_NMI_RESERVED;
251 spin_unlock(&lapic_nmi_owner_lock);
252 if (old_owner & LAPIC_NMI_RESERVED)
253 return -EBUSY;
254 if (old_owner & LAPIC_NMI_WATCHDOG)
255 disable_lapic_nmi_watchdog();
256 return 0;
257}
258
259void release_lapic_nmi(void)
260{
261 unsigned int new_owner;
262
263 spin_lock(&lapic_nmi_owner_lock);
264 new_owner = lapic_nmi_owner & ~LAPIC_NMI_RESERVED;
265 lapic_nmi_owner = new_owner;
266 spin_unlock(&lapic_nmi_owner_lock);
267 if (new_owner & LAPIC_NMI_WATCHDOG)
268 enable_lapic_nmi_watchdog();
269}
270
271void disable_timer_nmi_watchdog(void)
272{
273 if ((nmi_watchdog != NMI_IO_APIC) || (nmi_active <= 0))
274 return;
275
276 disable_irq(0);
277 unset_nmi_callback();
278 nmi_active = -1;
279 nmi_watchdog = NMI_NONE;
280}
281
282void enable_timer_nmi_watchdog(void)
283{
284 if (nmi_active < 0) {
285 nmi_watchdog = NMI_IO_APIC;
286 touch_nmi_watchdog();
287 nmi_active = 1;
288 enable_irq(0);
289 }
290}
291
292#ifdef CONFIG_PM
293
294static int nmi_pm_active; /* nmi_active before suspend */
295
829ca9a3 296static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
1da177e4
LT
297{
298 nmi_pm_active = nmi_active;
299 disable_lapic_nmi_watchdog();
300 return 0;
301}
302
303static int lapic_nmi_resume(struct sys_device *dev)
304{
305 if (nmi_pm_active > 0)
306 enable_lapic_nmi_watchdog();
307 return 0;
308}
309
310static struct sysdev_class nmi_sysclass = {
311 set_kset_name("lapic_nmi"),
312 .resume = lapic_nmi_resume,
313 .suspend = lapic_nmi_suspend,
314};
315
316static struct sys_device device_lapic_nmi = {
317 .id = 0,
318 .cls = &nmi_sysclass,
319};
320
321static int __init init_lapic_nmi_sysfs(void)
322{
323 int error;
324
325 if (nmi_active == 0 || nmi_watchdog != NMI_LOCAL_APIC)
326 return 0;
327
328 error = sysdev_class_register(&nmi_sysclass);
329 if (!error)
330 error = sysdev_register(&device_lapic_nmi);
331 return error;
332}
333/* must come after the local APIC's device_initcall() */
334late_initcall(init_lapic_nmi_sysfs);
335
336#endif /* CONFIG_PM */
337
338/*
339 * Activate the NMI watchdog via the local APIC.
340 * Original code written by Keith Owens.
341 */
342
75152114
AK
343static void clear_msr_range(unsigned int base, unsigned int n)
344{
345 unsigned int i;
346
347 for(i = 0; i < n; ++i)
348 wrmsr(base+i, 0, 0);
349}
350
1da177e4
LT
351static void setup_k7_watchdog(void)
352{
353 int i;
354 unsigned int evntsel;
355
1da177e4
LT
356 nmi_perfctr_msr = MSR_K7_PERFCTR0;
357
358 for(i = 0; i < 4; ++i) {
359 /* Simulator may not support it */
75152114
AK
360 if (checking_wrmsrl(MSR_K7_EVNTSEL0+i, 0UL)) {
361 nmi_perfctr_msr = 0;
1da177e4 362 return;
75152114 363 }
1da177e4
LT
364 wrmsrl(MSR_K7_PERFCTR0+i, 0UL);
365 }
366
367 evntsel = K7_EVNTSEL_INT
368 | K7_EVNTSEL_OS
369 | K7_EVNTSEL_USR
370 | K7_NMI_EVENT;
371
372 wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
42ac8ff2 373 wrmsrl(MSR_K7_PERFCTR0, -((u64)cpu_khz * 1000 / nmi_hz));
1da177e4
LT
374 apic_write(APIC_LVTPC, APIC_DM_NMI);
375 evntsel |= K7_EVNTSEL_ENABLE;
376 wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
377}
378
75152114
AK
379
380static int setup_p4_watchdog(void)
381{
382 unsigned int misc_enable, dummy;
383
384 rdmsr(MSR_P4_MISC_ENABLE, misc_enable, dummy);
385 if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
386 return 0;
387
388 nmi_perfctr_msr = MSR_P4_IQ_COUNTER0;
389 nmi_p4_cccr_val = P4_NMI_IQ_CCCR0;
390#ifdef CONFIG_SMP
391 if (smp_num_siblings == 2)
392 nmi_p4_cccr_val |= P4_CCCR_OVF_PMI1;
393#endif
394
395 if (!(misc_enable & MSR_P4_MISC_ENABLE_PEBS_UNAVAIL))
396 clear_msr_range(0x3F1, 2);
397 /* MSR 0x3F0 seems to have a default value of 0xFC00, but current
398 docs doesn't fully define it, so leave it alone for now. */
399 if (boot_cpu_data.x86_model >= 0x3) {
400 /* MSR_P4_IQ_ESCR0/1 (0x3ba/0x3bb) removed */
401 clear_msr_range(0x3A0, 26);
402 clear_msr_range(0x3BC, 3);
403 } else {
404 clear_msr_range(0x3A0, 31);
405 }
406 clear_msr_range(0x3C0, 6);
407 clear_msr_range(0x3C8, 6);
408 clear_msr_range(0x3E0, 2);
409 clear_msr_range(MSR_P4_CCCR0, 18);
410 clear_msr_range(MSR_P4_PERFCTR0, 18);
411
412 wrmsr(MSR_P4_CRU_ESCR0, P4_NMI_CRU_ESCR0, 0);
413 wrmsr(MSR_P4_IQ_CCCR0, P4_NMI_IQ_CCCR0 & ~P4_CCCR_ENABLE, 0);
42ac8ff2
JB
414 Dprintk("setting P4_IQ_COUNTER0 to 0x%08lx\n", -(cpu_khz * 1000UL / nmi_hz));
415 wrmsrl(MSR_P4_IQ_COUNTER0, -((u64)cpu_khz * 1000 / nmi_hz));
75152114
AK
416 apic_write(APIC_LVTPC, APIC_DM_NMI);
417 wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
418 return 1;
419}
420
1da177e4
LT
421void setup_apic_nmi_watchdog(void)
422{
423 switch (boot_cpu_data.x86_vendor) {
424 case X86_VENDOR_AMD:
72e76be2 425 if (boot_cpu_data.x86 != 15)
1da177e4
LT
426 return;
427 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
428 return;
429 setup_k7_watchdog();
430 break;
75152114
AK
431 case X86_VENDOR_INTEL:
432 if (boot_cpu_data.x86 != 15)
433 return;
434 if (!setup_p4_watchdog())
435 return;
436 break;
437
1da177e4
LT
438 default:
439 return;
440 }
441 lapic_nmi_owner = LAPIC_NMI_WATCHDOG;
442 nmi_active = 1;
443}
444
445/*
446 * the best way to detect whether a CPU has a 'hard lockup' problem
447 * is to check it's local APIC timer IRQ counts. If they are not
448 * changing then that CPU has some problem.
449 *
450 * as these watchdog NMI IRQs are generated on every CPU, we only
451 * have to check the current processor.
1da177e4
LT
452 */
453
75152114
AK
454static DEFINE_PER_CPU(unsigned, last_irq_sum);
455static DEFINE_PER_CPU(local_t, alert_counter);
456static DEFINE_PER_CPU(int, nmi_touch);
1da177e4
LT
457
458void touch_nmi_watchdog (void)
459{
99019e91
JB
460 if (nmi_watchdog > 0) {
461 unsigned cpu;
1da177e4 462
99019e91
JB
463 /*
464 * Tell other CPUs to reset their alert counters. We cannot
465 * do it ourselves because the alert count increase is not
466 * atomic.
467 */
468 for_each_present_cpu (cpu)
469 per_cpu(nmi_touch, cpu) = 1;
470 }
8446f1d3
IM
471
472 touch_softlockup_watchdog();
1da177e4
LT
473}
474
eddb6fb9 475void __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
1da177e4 476{
75152114
AK
477 int sum;
478 int touched = 0;
1da177e4 479
1da177e4 480 sum = read_pda(apic_timer_irqs);
75152114
AK
481 if (__get_cpu_var(nmi_touch)) {
482 __get_cpu_var(nmi_touch) = 0;
483 touched = 1;
484 }
485 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
1da177e4
LT
486 /*
487 * Ayiee, looks like this CPU is stuck ...
488 * wait a few IRQs (5 seconds) before doing the oops ...
489 */
75152114
AK
490 local_inc(&__get_cpu_var(alert_counter));
491 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz) {
1da177e4
LT
492 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
493 == NOTIFY_STOP) {
75152114 494 local_set(&__get_cpu_var(alert_counter), 0);
1da177e4 495 return;
84781576
CE
496 }
497 die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs);
1da177e4
LT
498 }
499 } else {
75152114
AK
500 __get_cpu_var(last_irq_sum) = sum;
501 local_set(&__get_cpu_var(alert_counter), 0);
1da177e4 502 }
75152114
AK
503 if (nmi_perfctr_msr) {
504 if (nmi_perfctr_msr == MSR_P4_IQ_COUNTER0) {
505 /*
506 * P4 quirks:
507 * - An overflown perfctr will assert its interrupt
508 * until the OVF flag in its CCCR is cleared.
509 * - LVTPC is masked on interrupt and must be
510 * unmasked by the LVTPC handler.
511 */
512 wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
513 apic_write(APIC_LVTPC, APIC_DM_NMI);
514 }
42ac8ff2 515 wrmsrl(nmi_perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
75152114 516 }
1da177e4
LT
517}
518
eddb6fb9 519static __kprobes int dummy_nmi_callback(struct pt_regs * regs, int cpu)
1da177e4
LT
520{
521 return 0;
522}
523
524static nmi_callback_t nmi_callback = dummy_nmi_callback;
525
eddb6fb9 526asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
1da177e4
LT
527{
528 int cpu = safe_smp_processor_id();
529
530 nmi_enter();
531 add_pda(__nmi_count,1);
19306059 532 if (!rcu_dereference(nmi_callback)(regs, cpu))
1da177e4
LT
533 default_do_nmi(regs);
534 nmi_exit();
535}
536
537void set_nmi_callback(nmi_callback_t callback)
538{
19306059 539 rcu_assign_pointer(nmi_callback, callback);
1da177e4
LT
540}
541
542void unset_nmi_callback(void)
543{
544 nmi_callback = dummy_nmi_callback;
545}
546
547#ifdef CONFIG_SYSCTL
548
549static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
550{
551 unsigned char reason = get_nmi_reason();
552 char buf[64];
553
554 if (!(reason & 0xc0)) {
555 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
556 die_nmi(buf,regs);
557 }
558 return 0;
559}
560
561/*
562 * proc handler for /proc/sys/kernel/unknown_nmi_panic
563 */
564int proc_unknown_nmi_panic(struct ctl_table *table, int write, struct file *file,
565 void __user *buffer, size_t *length, loff_t *ppos)
566{
567 int old_state;
568
569 old_state = unknown_nmi_panic;
570 proc_dointvec(table, write, file, buffer, length, ppos);
571 if (!!old_state == !!unknown_nmi_panic)
572 return 0;
573
574 if (unknown_nmi_panic) {
575 if (reserve_lapic_nmi() < 0) {
576 unknown_nmi_panic = 0;
577 return -EBUSY;
578 } else {
579 set_nmi_callback(unknown_nmi_panic_callback);
580 }
581 } else {
582 release_lapic_nmi();
583 unset_nmi_callback();
584 }
585 return 0;
586}
587
588#endif
589
590EXPORT_SYMBOL(nmi_active);
591EXPORT_SYMBOL(nmi_watchdog);
592EXPORT_SYMBOL(reserve_lapic_nmi);
593EXPORT_SYMBOL(release_lapic_nmi);
594EXPORT_SYMBOL(disable_timer_nmi_watchdog);
595EXPORT_SYMBOL(enable_timer_nmi_watchdog);
596EXPORT_SYMBOL(touch_nmi_watchdog);