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/irq.h>
19 #include <linux/delay.h>
20 #include <linux/bootmem.h>
21 #include <linux/smp_lock.h>
22 #include <linux/interrupt.h>
23 #include <linux/mc146818rtc.h>
24 #include <linux/kernel_stat.h>
25 #include <linux/module.h>
26 #include <linux/nmi.h>
27 #include <linux/sysdev.h>
28 #include <linux/dump.h>
32 #include <asm/mpspec.h>
35 unsigned int nmi_watchdog = NMI_NONE;
36 static unsigned int nmi_hz = HZ;
37 static unsigned int nmi_perfctr_msr; /* the MSR to reset in NMI handler */
38 static unsigned int nmi_p4_cccr_val;
39 extern void show_registers(struct pt_regs *regs);
42 * lapic_nmi_owner tracks the ownership of the lapic NMI hardware:
43 * - it may be reserved by some other driver, or not
44 * - when not reserved by some other driver, it may be used for
45 * the NMI watchdog, or not
47 * This is maintained separately from nmi_active because the NMI
48 * watchdog may also be driven from the I/O APIC timer.
50 static spinlock_t lapic_nmi_owner_lock = SPIN_LOCK_UNLOCKED;
51 static unsigned int lapic_nmi_owner;
52 #define LAPIC_NMI_WATCHDOG (1<<0)
53 #define LAPIC_NMI_RESERVED (1<<1)
56 * +1: the lapic NMI watchdog is active, but can be disabled
57 * 0: the lapic NMI watchdog has not been set up, and cannot
59 * -1: the lapic NMI watchdog is disabled, but can be enabled
63 #define K7_EVNTSEL_ENABLE (1 << 22)
64 #define K7_EVNTSEL_INT (1 << 20)
65 #define K7_EVNTSEL_OS (1 << 17)
66 #define K7_EVNTSEL_USR (1 << 16)
67 #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
68 #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
70 #define P6_EVNTSEL0_ENABLE (1 << 22)
71 #define P6_EVNTSEL_INT (1 << 20)
72 #define P6_EVNTSEL_OS (1 << 17)
73 #define P6_EVNTSEL_USR (1 << 16)
74 #define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79
75 #define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED
77 #define MSR_P4_MISC_ENABLE 0x1A0
78 #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
79 #define MSR_P4_MISC_ENABLE_PEBS_UNAVAIL (1<<12)
80 #define MSR_P4_PERFCTR0 0x300
81 #define MSR_P4_CCCR0 0x360
82 #define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
83 #define P4_ESCR_OS (1<<3)
84 #define P4_ESCR_USR (1<<2)
85 #define P4_CCCR_OVF_PMI0 (1<<26)
86 #define P4_CCCR_OVF_PMI1 (1<<27)
87 #define P4_CCCR_THRESHOLD(N) ((N)<<20)
88 #define P4_CCCR_COMPLEMENT (1<<19)
89 #define P4_CCCR_COMPARE (1<<18)
90 #define P4_CCCR_REQUIRED (3<<16)
91 #define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
92 #define P4_CCCR_ENABLE (1<<12)
93 /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
94 CRU_ESCR0 (with any non-null event selector) through a complemented
95 max threshold. [IA32-Vol3, Section 14.9.9] */
96 #define MSR_P4_IQ_COUNTER0 0x30C
97 #define P4_NMI_CRU_ESCR0 (P4_ESCR_EVENT_SELECT(0x3F)|P4_ESCR_OS|P4_ESCR_USR)
98 #define P4_NMI_IQ_CCCR0 \
99 (P4_CCCR_OVF_PMI0|P4_CCCR_THRESHOLD(15)|P4_CCCR_COMPLEMENT| \
100 P4_CCCR_COMPARE|P4_CCCR_REQUIRED|P4_CCCR_ESCR_SELECT(4)|P4_CCCR_ENABLE)
102 int __init check_nmi_watchdog (void)
104 unsigned int prev_nmi_count[NR_CPUS];
107 printk(KERN_INFO "testing NMI watchdog ... ");
109 for (cpu = 0; cpu < NR_CPUS; cpu++)
110 prev_nmi_count[cpu] = irq_stat[cpu].__nmi_count;
112 mdelay((10*1000)/nmi_hz); // wait 10 ticks
114 /* FIXME: Only boot CPU is online at this stage. Check CPUs
116 for (cpu = 0; cpu < NR_CPUS; cpu++) {
117 if (!cpu_online(cpu))
119 if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
120 printk("CPU#%d: NMI appears to be stuck!\n", cpu);
122 lapic_nmi_owner &= ~LAPIC_NMI_WATCHDOG;
128 /* now that we know it works we can reduce NMI frequency to
129 something more reasonable; makes a difference in some configs */
130 if (nmi_watchdog == NMI_LOCAL_APIC)
136 static int __init setup_nmi_watchdog(char *str)
140 get_option(&str, &nmi);
142 if (nmi >= NMI_INVALID)
147 * If any other x86 CPU has a local APIC, then
148 * please test the NMI stuff there and send me the
149 * missing bits. Right now Intel P6/P4 and AMD K7 only.
151 if ((nmi == NMI_LOCAL_APIC) &&
152 (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
153 (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15))
155 if ((nmi == NMI_LOCAL_APIC) &&
156 (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) &&
157 (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15))
160 * We can enable the IO-APIC watchdog
163 if (nmi == NMI_IO_APIC) {
170 __setup("nmi_watchdog=", setup_nmi_watchdog);
172 static void disable_lapic_nmi_watchdog(void)
176 switch (boot_cpu_data.x86_vendor) {
178 wrmsr(MSR_K7_EVNTSEL0, 0, 0);
180 case X86_VENDOR_INTEL:
181 switch (boot_cpu_data.x86) {
183 if (boot_cpu_data.x86_model > 0xd)
186 wrmsr(MSR_P6_EVNTSEL0, 0, 0);
189 if (boot_cpu_data.x86_model > 0x3)
192 wrmsr(MSR_P4_IQ_CCCR0, 0, 0);
193 wrmsr(MSR_P4_CRU_ESCR0, 0, 0);
199 /* tell do_nmi() and others that we're not active any more */
203 static void enable_lapic_nmi_watchdog(void)
205 if (nmi_active < 0) {
206 nmi_watchdog = NMI_LOCAL_APIC;
207 setup_apic_nmi_watchdog();
211 int reserve_lapic_nmi(void)
213 unsigned int old_owner;
215 spin_lock(&lapic_nmi_owner_lock);
216 old_owner = lapic_nmi_owner;
217 lapic_nmi_owner |= LAPIC_NMI_RESERVED;
218 spin_unlock(&lapic_nmi_owner_lock);
219 if (old_owner & LAPIC_NMI_RESERVED)
221 if (old_owner & LAPIC_NMI_WATCHDOG)
222 disable_lapic_nmi_watchdog();
226 void release_lapic_nmi(void)
228 unsigned int new_owner;
230 spin_lock(&lapic_nmi_owner_lock);
231 new_owner = lapic_nmi_owner & ~LAPIC_NMI_RESERVED;
232 lapic_nmi_owner = new_owner;
233 spin_unlock(&lapic_nmi_owner_lock);
234 if (new_owner & LAPIC_NMI_WATCHDOG)
235 enable_lapic_nmi_watchdog();
238 void disable_timer_nmi_watchdog(void)
240 if ((nmi_watchdog != NMI_IO_APIC) || (nmi_active <= 0))
243 unset_nmi_callback();
245 nmi_watchdog = NMI_NONE;
248 void enable_timer_nmi_watchdog(void)
250 if (nmi_active < 0) {
251 nmi_watchdog = NMI_IO_APIC;
252 touch_nmi_watchdog();
259 static int nmi_pm_active; /* nmi_active before suspend */
261 static int lapic_nmi_suspend(struct sys_device *dev, u32 state)
263 nmi_pm_active = nmi_active;
264 disable_lapic_nmi_watchdog();
268 static int lapic_nmi_resume(struct sys_device *dev)
270 if (nmi_pm_active > 0)
271 enable_lapic_nmi_watchdog();
276 static struct sysdev_class nmi_sysclass = {
277 set_kset_name("lapic_nmi"),
278 .resume = lapic_nmi_resume,
279 .suspend = lapic_nmi_suspend,
282 static struct sys_device device_lapic_nmi = {
284 .cls = &nmi_sysclass,
287 static int __init init_lapic_nmi_sysfs(void)
291 if (nmi_active == 0 || nmi_watchdog != NMI_LOCAL_APIC)
294 error = sysdev_class_register(&nmi_sysclass);
296 error = sysdev_register(&device_lapic_nmi);
299 /* must come after the local APIC's device_initcall() */
300 late_initcall(init_lapic_nmi_sysfs);
302 #endif /* CONFIG_PM */
305 * Activate the NMI watchdog via the local APIC.
306 * Original code written by Keith Owens.
309 static void clear_msr_range(unsigned int base, unsigned int n)
313 for(i = 0; i < n; ++i)
317 static void setup_k7_watchdog(void)
319 unsigned int evntsel;
321 nmi_perfctr_msr = MSR_K7_PERFCTR0;
323 clear_msr_range(MSR_K7_EVNTSEL0, 4);
324 clear_msr_range(MSR_K7_PERFCTR0, 4);
326 evntsel = K7_EVNTSEL_INT
331 wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
332 Dprintk("setting K7_PERFCTR0 to %08lx\n", -(cpu_khz/nmi_hz*1000));
333 wrmsr(MSR_K7_PERFCTR0, -(cpu_khz/nmi_hz*1000), -1);
334 apic_write(APIC_LVTPC, APIC_DM_NMI);
335 evntsel |= K7_EVNTSEL_ENABLE;
336 wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
339 static void setup_p6_watchdog(void)
341 unsigned int evntsel;
343 nmi_perfctr_msr = MSR_P6_PERFCTR0;
345 clear_msr_range(MSR_P6_EVNTSEL0, 2);
346 clear_msr_range(MSR_P6_PERFCTR0, 2);
348 evntsel = P6_EVNTSEL_INT
353 wrmsr(MSR_P6_EVNTSEL0, evntsel, 0);
354 Dprintk("setting P6_PERFCTR0 to %08lx\n", -(cpu_khz/nmi_hz*1000));
355 wrmsr(MSR_P6_PERFCTR0, -(cpu_khz/nmi_hz*1000), 0);
356 apic_write(APIC_LVTPC, APIC_DM_NMI);
357 evntsel |= P6_EVNTSEL0_ENABLE;
358 wrmsr(MSR_P6_EVNTSEL0, evntsel, 0);
361 static int setup_p4_watchdog(void)
363 unsigned int misc_enable, dummy;
365 rdmsr(MSR_P4_MISC_ENABLE, misc_enable, dummy);
366 if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
369 nmi_perfctr_msr = MSR_P4_IQ_COUNTER0;
370 nmi_p4_cccr_val = P4_NMI_IQ_CCCR0;
372 if (smp_num_siblings == 2)
373 nmi_p4_cccr_val |= P4_CCCR_OVF_PMI1;
376 if (!(misc_enable & MSR_P4_MISC_ENABLE_PEBS_UNAVAIL))
377 clear_msr_range(0x3F1, 2);
378 /* MSR 0x3F0 seems to have a default value of 0xFC00, but current
379 docs doesn't fully define it, so leave it alone for now. */
380 clear_msr_range(0x3A0, 31);
381 clear_msr_range(0x3C0, 6);
382 clear_msr_range(0x3C8, 6);
383 clear_msr_range(0x3E0, 2);
384 clear_msr_range(MSR_P4_CCCR0, 18);
385 clear_msr_range(MSR_P4_PERFCTR0, 18);
387 wrmsr(MSR_P4_CRU_ESCR0, P4_NMI_CRU_ESCR0, 0);
388 wrmsr(MSR_P4_IQ_CCCR0, P4_NMI_IQ_CCCR0 & ~P4_CCCR_ENABLE, 0);
389 Dprintk("setting P4_IQ_COUNTER0 to 0x%08lx\n", -(cpu_khz/nmi_hz*1000));
390 wrmsr(MSR_P4_IQ_COUNTER0, -(cpu_khz/nmi_hz*1000), -1);
391 apic_write(APIC_LVTPC, APIC_DM_NMI);
392 wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
396 void setup_apic_nmi_watchdog (void)
398 switch (boot_cpu_data.x86_vendor) {
400 if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15)
404 case X86_VENDOR_INTEL:
405 switch (boot_cpu_data.x86) {
407 if (boot_cpu_data.x86_model > 0xd)
413 if (boot_cpu_data.x86_model > 0x3)
416 if (!setup_p4_watchdog())
426 lapic_nmi_owner = LAPIC_NMI_WATCHDOG;
430 static spinlock_t nmi_print_lock = SPIN_LOCK_UNLOCKED;
433 * the best way to detect whether a CPU has a 'hard lockup' problem
434 * is to check it's local APIC timer IRQ counts. If they are not
435 * changing then that CPU has some problem.
437 * as these watchdog NMI IRQs are generated on every CPU, we only
438 * have to check the current processor.
440 * since NMIs don't listen to _any_ locks, we have to be extremely
441 * careful not to rely on unsafe variables. The printk might lock
442 * up though, so we have to break up any console locks first ...
443 * [when there will be more tty-related locks, break them up
448 last_irq_sums [NR_CPUS],
449 alert_counter [NR_CPUS];
451 void touch_nmi_watchdog (void)
456 * Just reset the alert counters, (other CPUs might be
457 * spinning on locks we hold):
459 for (i = 0; i < NR_CPUS; i++)
460 alert_counter[i] = 0;
463 void nmi_watchdog_tick (struct pt_regs * regs)
467 * Since current_thread_info()-> is always on the stack, and we
468 * always switch the stack NMI-atomically, it's safe to use
469 * smp_processor_id().
471 int sum, cpu = smp_processor_id();
473 sum = irq_stat[cpu].apic_timer_irqs;
475 if (last_irq_sums[cpu] == sum) {
477 * Ayiee, looks like this CPU is stuck ...
478 * wait a few IRQs (5 seconds) before doing the oops ...
480 alert_counter[cpu]++;
481 if (alert_counter[cpu] == 60*nmi_hz) {
482 spin_lock(&nmi_print_lock);
484 * We are in trouble anyway, lets at least try
485 * to get a message out.
488 printk("NMI Watchdog detected LOCKUP on CPU%d, eip %08lx, registers:\n", cpu, regs->eip);
489 show_registers(regs);
490 dump("NMI Watchdog detected LOCKUP", regs);
491 printk("console shuts up ...\n");
493 spin_unlock(&nmi_print_lock);
498 last_irq_sums[cpu] = sum;
499 alert_counter[cpu] = 0;
501 if (nmi_perfctr_msr) {
502 if (nmi_perfctr_msr == MSR_P4_IQ_COUNTER0) {
505 * - An overflown perfctr will assert its interrupt
506 * until the OVF flag in its CCCR is cleared.
507 * - LVTPC is masked on interrupt and must be
508 * unmasked by the LVTPC handler.
510 wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
511 apic_write(APIC_LVTPC, APIC_DM_NMI);
513 else if (nmi_perfctr_msr == MSR_P6_PERFCTR0) {
514 /* Only P6 based Pentium M need to re-unmask
515 * the apic vector but it doesn't hurt
516 * other P6 variant */
517 apic_write(APIC_LVTPC, APIC_DM_NMI);
519 wrmsr(nmi_perfctr_msr, -(cpu_khz/nmi_hz*1000), -1);
523 EXPORT_SYMBOL(nmi_active);
524 EXPORT_SYMBOL(nmi_watchdog);
525 EXPORT_SYMBOL(reserve_lapic_nmi);
526 EXPORT_SYMBOL(release_lapic_nmi);
527 EXPORT_SYMBOL(disable_timer_nmi_watchdog);
528 EXPORT_SYMBOL(enable_timer_nmi_watchdog);
529 EXPORT_SYMBOL_GPL(touch_nmi_watchdog);