2 * linux/arch/i386/nmi.c
4 * NMI watchdog support on APIC systems
6 * Started by Ingo Molnar <mingo@redhat.com>
9 * Mikael Pettersson : AMD K7 support for local APIC NMI watchdog.
10 * Mikael Pettersson : Power Management for local APIC NMI watchdog.
11 * Mikael Pettersson : Pentium 4 support for local APIC NMI watchdog.
13 * Mikael Pettersson : PM converted to driver model. Disable/enable API.
16 #include <linux/config.h>
18 #include <linux/delay.h>
19 #include <linux/bootmem.h>
20 #include <linux/smp_lock.h>
21 #include <linux/interrupt.h>
22 #include <linux/mc146818rtc.h>
23 #include <linux/kernel_stat.h>
24 #include <linux/module.h>
25 #include <linux/nmi.h>
26 #include <linux/sysdev.h>
27 #include <linux/sysctl.h>
30 #include <asm/div64.h>
33 #include "mach_traps.h"
35 unsigned int nmi_watchdog = NMI_NONE;
36 extern int unknown_nmi_panic;
37 static unsigned int nmi_hz = HZ;
38 static unsigned int nmi_perfctr_msr; /* the MSR to reset in NMI handler */
39 static unsigned int nmi_p4_cccr_val;
40 extern void show_registers(struct pt_regs *regs);
43 * lapic_nmi_owner tracks the ownership of the lapic NMI hardware:
44 * - it may be reserved by some other driver, or not
45 * - when not reserved by some other driver, it may be used for
46 * the NMI watchdog, or not
48 * This is maintained separately from nmi_active because the NMI
49 * watchdog may also be driven from the I/O APIC timer.
51 static DEFINE_SPINLOCK(lapic_nmi_owner_lock);
52 static unsigned int lapic_nmi_owner;
53 #define LAPIC_NMI_WATCHDOG (1<<0)
54 #define LAPIC_NMI_RESERVED (1<<1)
57 * +1: the lapic NMI watchdog is active, but can be disabled
58 * 0: the lapic NMI watchdog has not been set up, and cannot
60 * -1: the lapic NMI watchdog is disabled, but can be enabled
64 #define K7_EVNTSEL_ENABLE (1 << 22)
65 #define K7_EVNTSEL_INT (1 << 20)
66 #define K7_EVNTSEL_OS (1 << 17)
67 #define K7_EVNTSEL_USR (1 << 16)
68 #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
69 #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
71 #define P6_EVNTSEL0_ENABLE (1 << 22)
72 #define P6_EVNTSEL_INT (1 << 20)
73 #define P6_EVNTSEL_OS (1 << 17)
74 #define P6_EVNTSEL_USR (1 << 16)
75 #define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79
76 #define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED
78 #define MSR_P4_MISC_ENABLE 0x1A0
79 #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
80 #define MSR_P4_MISC_ENABLE_PEBS_UNAVAIL (1<<12)
81 #define MSR_P4_PERFCTR0 0x300
82 #define MSR_P4_CCCR0 0x360
83 #define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
84 #define P4_ESCR_OS (1<<3)
85 #define P4_ESCR_USR (1<<2)
86 #define P4_CCCR_OVF_PMI0 (1<<26)
87 #define P4_CCCR_OVF_PMI1 (1<<27)
88 #define P4_CCCR_THRESHOLD(N) ((N)<<20)
89 #define P4_CCCR_COMPLEMENT (1<<19)
90 #define P4_CCCR_COMPARE (1<<18)
91 #define P4_CCCR_REQUIRED (3<<16)
92 #define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
93 #define P4_CCCR_ENABLE (1<<12)
94 /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
95 CRU_ESCR0 (with any non-null event selector) through a complemented
96 max threshold. [IA32-Vol3, Section 14.9.9] */
97 #define MSR_P4_IQ_COUNTER0 0x30C
98 #define P4_NMI_CRU_ESCR0 (P4_ESCR_EVENT_SELECT(0x3F)|P4_ESCR_OS|P4_ESCR_USR)
99 #define P4_NMI_IQ_CCCR0 \
100 (P4_CCCR_OVF_PMI0|P4_CCCR_THRESHOLD(15)|P4_CCCR_COMPLEMENT| \
101 P4_CCCR_COMPARE|P4_CCCR_REQUIRED|P4_CCCR_ESCR_SELECT(4)|P4_CCCR_ENABLE)
104 /* The performance counters used by NMI_LOCAL_APIC don't trigger when
105 * the CPU is idle. To make sure the NMI watchdog really ticks on all
106 * CPUs during the test make them busy.
108 static __init void nmi_cpu_busy(void *data)
110 volatile int *endflag = data;
112 /* Intentionally don't use cpu_relax here. This is
113 to make sure that the performance counter really ticks,
114 even if there is a simulator or similar that catches the
115 pause instruction. On a real HT machine this is fine because
116 all other CPUs are busy with "useless" delay loops and don't
117 care if they get somewhat less cycles. */
118 while (*endflag == 0)
123 static int __init check_nmi_watchdog(void)
125 volatile int endflag = 0;
126 unsigned int *prev_nmi_count;
129 if (nmi_watchdog == NMI_NONE)
132 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
136 printk(KERN_INFO "Testing NMI watchdog ... ");
138 if (nmi_watchdog == NMI_LOCAL_APIC)
139 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
141 for_each_possible_cpu(cpu)
142 prev_nmi_count[cpu] = per_cpu(irq_stat, cpu).__nmi_count;
144 mdelay((10*1000)/nmi_hz); // wait 10 ticks
146 for_each_possible_cpu(cpu) {
148 /* Check cpu_callin_map here because that is set
149 after the timer is started. */
150 if (!cpu_isset(cpu, cpu_callin_map))
153 if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
155 printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
160 lapic_nmi_owner &= ~LAPIC_NMI_WATCHDOG;
161 kfree(prev_nmi_count);
168 /* now that we know it works we can reduce NMI frequency to
169 something more reasonable; makes a difference in some configs */
170 if (nmi_watchdog == NMI_LOCAL_APIC)
173 kfree(prev_nmi_count);
176 /* This needs to happen later in boot so counters are working */
177 late_initcall(check_nmi_watchdog);
179 static int __init setup_nmi_watchdog(char *str)
183 get_option(&str, &nmi);
185 if (nmi >= NMI_INVALID)
190 * If any other x86 CPU has a local APIC, then
191 * please test the NMI stuff there and send me the
192 * missing bits. Right now Intel P6/P4 and AMD K7 only.
194 if ((nmi == NMI_LOCAL_APIC) &&
195 (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
196 (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15))
198 if ((nmi == NMI_LOCAL_APIC) &&
199 (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) &&
200 (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15))
203 * We can enable the IO-APIC watchdog
206 if (nmi == NMI_IO_APIC) {
213 __setup("nmi_watchdog=", setup_nmi_watchdog);
215 static void disable_lapic_nmi_watchdog(void)
219 switch (boot_cpu_data.x86_vendor) {
221 wrmsr(MSR_K7_EVNTSEL0, 0, 0);
223 case X86_VENDOR_INTEL:
224 switch (boot_cpu_data.x86) {
226 if (boot_cpu_data.x86_model > 0xd)
229 wrmsr(MSR_P6_EVNTSEL0, 0, 0);
232 if (boot_cpu_data.x86_model > 0x4)
235 wrmsr(MSR_P4_IQ_CCCR0, 0, 0);
236 wrmsr(MSR_P4_CRU_ESCR0, 0, 0);
242 /* tell do_nmi() and others that we're not active any more */
246 static void enable_lapic_nmi_watchdog(void)
248 if (nmi_active < 0) {
249 nmi_watchdog = NMI_LOCAL_APIC;
250 setup_apic_nmi_watchdog();
254 int reserve_lapic_nmi(void)
256 unsigned int old_owner;
258 spin_lock(&lapic_nmi_owner_lock);
259 old_owner = lapic_nmi_owner;
260 lapic_nmi_owner |= LAPIC_NMI_RESERVED;
261 spin_unlock(&lapic_nmi_owner_lock);
262 if (old_owner & LAPIC_NMI_RESERVED)
264 if (old_owner & LAPIC_NMI_WATCHDOG)
265 disable_lapic_nmi_watchdog();
269 void release_lapic_nmi(void)
271 unsigned int new_owner;
273 spin_lock(&lapic_nmi_owner_lock);
274 new_owner = lapic_nmi_owner & ~LAPIC_NMI_RESERVED;
275 lapic_nmi_owner = new_owner;
276 spin_unlock(&lapic_nmi_owner_lock);
277 if (new_owner & LAPIC_NMI_WATCHDOG)
278 enable_lapic_nmi_watchdog();
281 void disable_timer_nmi_watchdog(void)
283 if ((nmi_watchdog != NMI_IO_APIC) || (nmi_active <= 0))
286 unset_nmi_callback();
288 nmi_watchdog = NMI_NONE;
291 void enable_timer_nmi_watchdog(void)
293 if (nmi_active < 0) {
294 nmi_watchdog = NMI_IO_APIC;
295 touch_nmi_watchdog();
302 static int nmi_pm_active; /* nmi_active before suspend */
304 static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
306 nmi_pm_active = nmi_active;
307 disable_lapic_nmi_watchdog();
311 static int lapic_nmi_resume(struct sys_device *dev)
313 if (nmi_pm_active > 0)
314 enable_lapic_nmi_watchdog();
319 static struct sysdev_class nmi_sysclass = {
320 set_kset_name("lapic_nmi"),
321 .resume = lapic_nmi_resume,
322 .suspend = lapic_nmi_suspend,
325 static struct sys_device device_lapic_nmi = {
327 .cls = &nmi_sysclass,
330 static int __init init_lapic_nmi_sysfs(void)
334 if (nmi_active == 0 || nmi_watchdog != NMI_LOCAL_APIC)
337 error = sysdev_class_register(&nmi_sysclass);
339 error = sysdev_register(&device_lapic_nmi);
342 /* must come after the local APIC's device_initcall() */
343 late_initcall(init_lapic_nmi_sysfs);
345 #endif /* CONFIG_PM */
348 * Activate the NMI watchdog via the local APIC.
349 * Original code written by Keith Owens.
352 static void clear_msr_range(unsigned int base, unsigned int n)
356 for(i = 0; i < n; ++i)
360 static void write_watchdog_counter(const char *descr)
362 u64 count = (u64)cpu_khz * 1000;
364 do_div(count, nmi_hz);
366 Dprintk("setting %s to -0x%08Lx\n", descr, count);
367 wrmsrl(nmi_perfctr_msr, 0 - count);
370 static void setup_k7_watchdog(void)
372 unsigned int evntsel;
374 nmi_perfctr_msr = MSR_K7_PERFCTR0;
376 clear_msr_range(MSR_K7_EVNTSEL0, 4);
377 clear_msr_range(MSR_K7_PERFCTR0, 4);
379 evntsel = K7_EVNTSEL_INT
384 wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
385 write_watchdog_counter("K7_PERFCTR0");
386 apic_write(APIC_LVTPC, APIC_DM_NMI);
387 evntsel |= K7_EVNTSEL_ENABLE;
388 wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
391 static void setup_p6_watchdog(void)
393 unsigned int evntsel;
395 nmi_perfctr_msr = MSR_P6_PERFCTR0;
397 clear_msr_range(MSR_P6_EVNTSEL0, 2);
398 clear_msr_range(MSR_P6_PERFCTR0, 2);
400 evntsel = P6_EVNTSEL_INT
405 wrmsr(MSR_P6_EVNTSEL0, evntsel, 0);
406 write_watchdog_counter("P6_PERFCTR0");
407 apic_write(APIC_LVTPC, APIC_DM_NMI);
408 evntsel |= P6_EVNTSEL0_ENABLE;
409 wrmsr(MSR_P6_EVNTSEL0, evntsel, 0);
412 static int setup_p4_watchdog(void)
414 unsigned int misc_enable, dummy;
416 rdmsr(MSR_P4_MISC_ENABLE, misc_enable, dummy);
417 if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
420 nmi_perfctr_msr = MSR_P4_IQ_COUNTER0;
421 nmi_p4_cccr_val = P4_NMI_IQ_CCCR0;
423 if (smp_num_siblings == 2)
424 nmi_p4_cccr_val |= P4_CCCR_OVF_PMI1;
427 if (!(misc_enable & MSR_P4_MISC_ENABLE_PEBS_UNAVAIL))
428 clear_msr_range(0x3F1, 2);
429 /* MSR 0x3F0 seems to have a default value of 0xFC00, but current
430 docs doesn't fully define it, so leave it alone for now. */
431 if (boot_cpu_data.x86_model >= 0x3) {
432 /* MSR_P4_IQ_ESCR0/1 (0x3ba/0x3bb) removed */
433 clear_msr_range(0x3A0, 26);
434 clear_msr_range(0x3BC, 3);
436 clear_msr_range(0x3A0, 31);
438 clear_msr_range(0x3C0, 6);
439 clear_msr_range(0x3C8, 6);
440 clear_msr_range(0x3E0, 2);
441 clear_msr_range(MSR_P4_CCCR0, 18);
442 clear_msr_range(MSR_P4_PERFCTR0, 18);
444 wrmsr(MSR_P4_CRU_ESCR0, P4_NMI_CRU_ESCR0, 0);
445 wrmsr(MSR_P4_IQ_CCCR0, P4_NMI_IQ_CCCR0 & ~P4_CCCR_ENABLE, 0);
446 write_watchdog_counter("P4_IQ_COUNTER0");
447 apic_write(APIC_LVTPC, APIC_DM_NMI);
448 wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
452 void setup_apic_nmi_watchdog (void)
454 switch (boot_cpu_data.x86_vendor) {
456 if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15)
460 case X86_VENDOR_INTEL:
461 switch (boot_cpu_data.x86) {
463 if (boot_cpu_data.x86_model > 0xd)
469 if (boot_cpu_data.x86_model > 0x4)
472 if (!setup_p4_watchdog())
482 lapic_nmi_owner = LAPIC_NMI_WATCHDOG;
487 * the best way to detect whether a CPU has a 'hard lockup' problem
488 * is to check it's local APIC timer IRQ counts. If they are not
489 * changing then that CPU has some problem.
491 * as these watchdog NMI IRQs are generated on every CPU, we only
492 * have to check the current processor.
494 * since NMIs don't listen to _any_ locks, we have to be extremely
495 * careful not to rely on unsafe variables. The printk might lock
496 * up though, so we have to break up any console locks first ...
497 * [when there will be more tty-related locks, break them up
502 last_irq_sums [NR_CPUS],
503 alert_counter [NR_CPUS];
505 void touch_nmi_watchdog (void)
510 * Just reset the alert counters, (other CPUs might be
511 * spinning on locks we hold):
513 for_each_possible_cpu(i)
514 alert_counter[i] = 0;
517 * Tickle the softlockup detector too:
519 touch_softlockup_watchdog();
522 extern void die_nmi(struct pt_regs *, const char *msg);
524 void nmi_watchdog_tick (struct pt_regs * regs)
528 * Since current_thread_info()-> is always on the stack, and we
529 * always switch the stack NMI-atomically, it's safe to use
530 * smp_processor_id().
533 int cpu = smp_processor_id();
535 sum = per_cpu(irq_stat, cpu).apic_timer_irqs;
537 if (last_irq_sums[cpu] == sum) {
539 * Ayiee, looks like this CPU is stuck ...
540 * wait a few IRQs (5 seconds) before doing the oops ...
542 alert_counter[cpu]++;
543 if (alert_counter[cpu] == 5*nmi_hz)
545 * die_nmi will return ONLY if NOTIFY_STOP happens..
547 die_nmi(regs, "BUG: NMI Watchdog detected LOCKUP");
549 last_irq_sums[cpu] = sum;
550 alert_counter[cpu] = 0;
552 if (nmi_perfctr_msr) {
553 if (nmi_perfctr_msr == MSR_P4_IQ_COUNTER0) {
556 * - An overflown perfctr will assert its interrupt
557 * until the OVF flag in its CCCR is cleared.
558 * - LVTPC is masked on interrupt and must be
559 * unmasked by the LVTPC handler.
561 wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
562 apic_write(APIC_LVTPC, APIC_DM_NMI);
564 else if (nmi_perfctr_msr == MSR_P6_PERFCTR0) {
565 /* Only P6 based Pentium M need to re-unmask
566 * the apic vector but it doesn't hurt
567 * other P6 variant */
568 apic_write(APIC_LVTPC, APIC_DM_NMI);
570 write_watchdog_counter(NULL);
576 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
578 unsigned char reason = get_nmi_reason();
581 if (!(reason & 0xc0)) {
582 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
589 * proc handler for /proc/sys/kernel/unknown_nmi_panic
591 int proc_unknown_nmi_panic(ctl_table *table, int write, struct file *file,
592 void __user *buffer, size_t *length, loff_t *ppos)
596 old_state = unknown_nmi_panic;
597 proc_dointvec(table, write, file, buffer, length, ppos);
598 if (!!old_state == !!unknown_nmi_panic)
601 if (unknown_nmi_panic) {
602 if (reserve_lapic_nmi() < 0) {
603 unknown_nmi_panic = 0;
606 set_nmi_callback(unknown_nmi_panic_callback);
610 unset_nmi_callback();
617 EXPORT_SYMBOL(nmi_active);
618 EXPORT_SYMBOL(nmi_watchdog);
619 EXPORT_SYMBOL(reserve_lapic_nmi);
620 EXPORT_SYMBOL(release_lapic_nmi);
621 EXPORT_SYMBOL(disable_timer_nmi_watchdog);
622 EXPORT_SYMBOL(enable_timer_nmi_watchdog);