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/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/nmi.h>
20 #include <linux/sysdev.h>
21 #include <linux/sysctl.h>
22 #include <linux/percpu.h>
23 #include <linux/dmi.h>
24 #include <linux/kprobes.h>
25 #include <linux/cpumask.h>
26 #include <linux/kernel_stat.h>
30 #include <asm/kdebug.h>
31 #include <asm/intel_arch_perfmon.h>
33 #include "mach_traps.h"
35 int unknown_nmi_panic;
36 int nmi_watchdog_enabled;
38 /* perfctr_nmi_owner tracks the ownership of the perfctr registers:
39 * evtsel_nmi_owner tracks the ownership of the event selection
40 * - different performance counters/ event selection may be reserved for
41 * different subsystems this reservation system just tries to coordinate
45 /* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's
46 * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now)
48 #define NMI_MAX_COUNTER_BITS 66
49 #define NMI_MAX_COUNTER_LONGS BITS_TO_LONGS(NMI_MAX_COUNTER_BITS)
51 static DEFINE_PER_CPU(unsigned long, perfctr_nmi_owner[NMI_MAX_COUNTER_LONGS]);
52 static DEFINE_PER_CPU(unsigned long, evntsel_nmi_owner[NMI_MAX_COUNTER_LONGS]);
54 static cpumask_t backtrace_mask = CPU_MASK_NONE;
56 * >0: the lapic NMI watchdog is active, but can be disabled
57 * <0: the lapic NMI watchdog has not been set up, and cannot
59 * 0: the lapic NMI watchdog is disabled, but can be enabled
61 atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
63 unsigned int nmi_watchdog = NMI_DEFAULT;
64 static unsigned int nmi_hz = HZ;
66 struct nmi_watchdog_ctlblk {
69 unsigned int cccr_msr;
70 unsigned int perfctr_msr; /* the MSR to reset in NMI handler */
71 unsigned int evntsel_msr; /* the MSR to select the events to handle */
73 static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk);
75 /* local prototypes */
76 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
78 extern void show_registers(struct pt_regs *regs);
79 extern int unknown_nmi_panic;
81 /* converts an msr to an appropriate reservation bit */
82 static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
84 /* returns the bit offset of the performance counter register */
85 switch (boot_cpu_data.x86_vendor) {
87 return (msr - MSR_K7_PERFCTR0);
88 case X86_VENDOR_INTEL:
89 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
90 return (msr - MSR_ARCH_PERFMON_PERFCTR0);
92 switch (boot_cpu_data.x86) {
94 return (msr - MSR_P6_PERFCTR0);
96 return (msr - MSR_P4_BPU_PERFCTR0);
102 /* converts an msr to an appropriate reservation bit */
103 static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr)
105 /* returns the bit offset of the event selection register */
106 switch (boot_cpu_data.x86_vendor) {
108 return (msr - MSR_K7_EVNTSEL0);
109 case X86_VENDOR_INTEL:
110 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
111 return (msr - MSR_ARCH_PERFMON_EVENTSEL0);
113 switch (boot_cpu_data.x86) {
115 return (msr - MSR_P6_EVNTSEL0);
117 return (msr - MSR_P4_BSU_ESCR0);
123 /* checks for a bit availability (hack for oprofile) */
124 int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
127 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
128 for_each_possible_cpu (cpu) {
129 if (test_bit(counter, &per_cpu(perfctr_nmi_owner, cpu)))
135 /* checks the an msr for availability */
136 int avail_to_resrv_perfctr_nmi(unsigned int msr)
138 unsigned int counter;
141 counter = nmi_perfctr_msr_to_bit(msr);
142 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
144 for_each_possible_cpu (cpu) {
145 if (test_bit(counter, &per_cpu(perfctr_nmi_owner, cpu)))
151 static int __reserve_perfctr_nmi(int cpu, unsigned int msr)
153 unsigned int counter;
155 cpu = smp_processor_id();
157 counter = nmi_perfctr_msr_to_bit(msr);
158 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
160 if (!test_and_set_bit(counter, &per_cpu(perfctr_nmi_owner, cpu)))
165 static void __release_perfctr_nmi(int cpu, unsigned int msr)
167 unsigned int counter;
169 cpu = smp_processor_id();
171 counter = nmi_perfctr_msr_to_bit(msr);
172 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
174 clear_bit(counter, &per_cpu(perfctr_nmi_owner, cpu));
177 int reserve_perfctr_nmi(unsigned int msr)
180 for_each_possible_cpu (cpu) {
181 if (!__reserve_perfctr_nmi(cpu, msr)) {
182 for_each_possible_cpu (i) {
185 __release_perfctr_nmi(i, msr);
193 void release_perfctr_nmi(unsigned int msr)
196 for_each_possible_cpu (cpu) {
197 __release_perfctr_nmi(cpu, msr);
201 int __reserve_evntsel_nmi(int cpu, unsigned int msr)
203 unsigned int counter;
205 cpu = smp_processor_id();
207 counter = nmi_evntsel_msr_to_bit(msr);
208 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
210 if (!test_and_set_bit(counter, &per_cpu(evntsel_nmi_owner, cpu)[0]))
215 static void __release_evntsel_nmi(int cpu, unsigned int msr)
217 unsigned int counter;
219 cpu = smp_processor_id();
221 counter = nmi_evntsel_msr_to_bit(msr);
222 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
224 clear_bit(counter, &per_cpu(evntsel_nmi_owner, cpu)[0]);
227 int reserve_evntsel_nmi(unsigned int msr)
230 for_each_possible_cpu (cpu) {
231 if (!__reserve_evntsel_nmi(cpu, msr)) {
232 for_each_possible_cpu (i) {
235 __release_evntsel_nmi(i, msr);
243 void release_evntsel_nmi(unsigned int msr)
246 for_each_possible_cpu (cpu) {
247 __release_evntsel_nmi(cpu, msr);
251 static __cpuinit inline int nmi_known_cpu(void)
253 switch (boot_cpu_data.x86_vendor) {
255 return ((boot_cpu_data.x86 == 15) || (boot_cpu_data.x86 == 6)
256 || (boot_cpu_data.x86 == 16));
257 case X86_VENDOR_INTEL:
258 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
261 return ((boot_cpu_data.x86 == 15) || (boot_cpu_data.x86 == 6));
266 static int endflag __initdata = 0;
269 /* The performance counters used by NMI_LOCAL_APIC don't trigger when
270 * the CPU is idle. To make sure the NMI watchdog really ticks on all
271 * CPUs during the test make them busy.
273 static __init void nmi_cpu_busy(void *data)
275 local_irq_enable_in_hardirq();
276 /* Intentionally don't use cpu_relax here. This is
277 to make sure that the performance counter really ticks,
278 even if there is a simulator or similar that catches the
279 pause instruction. On a real HT machine this is fine because
280 all other CPUs are busy with "useless" delay loops and don't
281 care if they get somewhat less cycles. */
287 static unsigned int adjust_for_32bit_ctr(unsigned int hz)
290 unsigned int retval = hz;
293 * On Intel CPUs with P6/ARCH_PERFMON only 32 bits in the counter
294 * are writable, with higher bits sign extending from bit 31.
295 * So, we can only program the counter with 31 bit values and
296 * 32nd bit should be 1, for 33.. to be 1.
297 * Find the appropriate nmi_hz
299 counter_val = (u64)cpu_khz * 1000;
300 do_div(counter_val, retval);
301 if (counter_val > 0x7fffffffULL) {
302 u64 count = (u64)cpu_khz * 1000;
303 do_div(count, 0x7fffffffUL);
309 static int __init check_nmi_watchdog(void)
311 unsigned int *prev_nmi_count;
314 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT))
317 if (!atomic_read(&nmi_active))
320 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
324 printk(KERN_INFO "Testing NMI watchdog ... ");
326 if (nmi_watchdog == NMI_LOCAL_APIC)
327 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
329 for_each_possible_cpu(cpu)
330 prev_nmi_count[cpu] = per_cpu(irq_stat, cpu).__nmi_count;
332 mdelay((20*1000)/nmi_hz); // wait 20 ticks
334 for_each_possible_cpu(cpu) {
336 /* Check cpu_callin_map here because that is set
337 after the timer is started. */
338 if (!cpu_isset(cpu, cpu_callin_map))
341 if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled)
343 if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
344 printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
348 per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0;
349 atomic_dec(&nmi_active);
352 if (!atomic_read(&nmi_active)) {
353 kfree(prev_nmi_count);
354 atomic_set(&nmi_active, -1);
360 /* now that we know it works we can reduce NMI frequency to
361 something more reasonable; makes a difference in some configs */
362 if (nmi_watchdog == NMI_LOCAL_APIC) {
363 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
367 if (wd->perfctr_msr == MSR_P6_PERFCTR0 ||
368 wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0) {
369 nmi_hz = adjust_for_32bit_ctr(nmi_hz);
373 kfree(prev_nmi_count);
376 /* This needs to happen later in boot so counters are working */
377 late_initcall(check_nmi_watchdog);
379 static int __init setup_nmi_watchdog(char *str)
383 get_option(&str, &nmi);
385 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
392 __setup("nmi_watchdog=", setup_nmi_watchdog);
394 static void disable_lapic_nmi_watchdog(void)
396 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
398 if (atomic_read(&nmi_active) <= 0)
401 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
403 BUG_ON(atomic_read(&nmi_active) != 0);
406 static void enable_lapic_nmi_watchdog(void)
408 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
410 /* are we already enabled */
411 if (atomic_read(&nmi_active) != 0)
414 /* are we lapic aware */
415 if (nmi_known_cpu() <= 0)
418 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
419 touch_nmi_watchdog();
422 void disable_timer_nmi_watchdog(void)
424 BUG_ON(nmi_watchdog != NMI_IO_APIC);
426 if (atomic_read(&nmi_active) <= 0)
430 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
432 BUG_ON(atomic_read(&nmi_active) != 0);
435 void enable_timer_nmi_watchdog(void)
437 BUG_ON(nmi_watchdog != NMI_IO_APIC);
439 if (atomic_read(&nmi_active) == 0) {
440 touch_nmi_watchdog();
441 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
446 static void __acpi_nmi_disable(void *__unused)
448 apic_write_around(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
452 * Disable timer based NMIs on all CPUs:
454 void acpi_nmi_disable(void)
456 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
457 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
460 static void __acpi_nmi_enable(void *__unused)
462 apic_write_around(APIC_LVT0, APIC_DM_NMI);
466 * Enable timer based NMIs on all CPUs:
468 void acpi_nmi_enable(void)
470 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
471 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
476 static int nmi_pm_active; /* nmi_active before suspend */
478 static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
480 /* only CPU0 goes here, other CPUs should be offline */
481 nmi_pm_active = atomic_read(&nmi_active);
482 stop_apic_nmi_watchdog(NULL);
483 BUG_ON(atomic_read(&nmi_active) != 0);
487 static int lapic_nmi_resume(struct sys_device *dev)
489 /* only CPU0 goes here, other CPUs should be offline */
490 if (nmi_pm_active > 0) {
491 setup_apic_nmi_watchdog(NULL);
492 touch_nmi_watchdog();
498 static struct sysdev_class nmi_sysclass = {
499 set_kset_name("lapic_nmi"),
500 .resume = lapic_nmi_resume,
501 .suspend = lapic_nmi_suspend,
504 static struct sys_device device_lapic_nmi = {
506 .cls = &nmi_sysclass,
509 static int __init init_lapic_nmi_sysfs(void)
513 /* should really be a BUG_ON but b/c this is an
514 * init call, it just doesn't work. -dcz
516 if (nmi_watchdog != NMI_LOCAL_APIC)
519 if ( atomic_read(&nmi_active) < 0 )
522 error = sysdev_class_register(&nmi_sysclass);
524 error = sysdev_register(&device_lapic_nmi);
527 /* must come after the local APIC's device_initcall() */
528 late_initcall(init_lapic_nmi_sysfs);
530 #endif /* CONFIG_PM */
533 * Activate the NMI watchdog via the local APIC.
534 * Original code written by Keith Owens.
537 static void write_watchdog_counter(unsigned int perfctr_msr, const char *descr)
539 u64 count = (u64)cpu_khz * 1000;
541 do_div(count, nmi_hz);
543 Dprintk("setting %s to -0x%08Lx\n", descr, count);
544 wrmsrl(perfctr_msr, 0 - count);
547 static void write_watchdog_counter32(unsigned int perfctr_msr,
550 u64 count = (u64)cpu_khz * 1000;
552 do_div(count, nmi_hz);
554 Dprintk("setting %s to -0x%08Lx\n", descr, count);
555 wrmsr(perfctr_msr, (u32)(-count), 0);
558 /* Note that these events don't tick when the CPU idles. This means
559 the frequency varies with CPU load. */
561 #define K7_EVNTSEL_ENABLE (1 << 22)
562 #define K7_EVNTSEL_INT (1 << 20)
563 #define K7_EVNTSEL_OS (1 << 17)
564 #define K7_EVNTSEL_USR (1 << 16)
565 #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
566 #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
568 static int setup_k7_watchdog(void)
570 unsigned int perfctr_msr, evntsel_msr;
571 unsigned int evntsel;
572 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
574 perfctr_msr = MSR_K7_PERFCTR0;
575 evntsel_msr = MSR_K7_EVNTSEL0;
576 if (!__reserve_perfctr_nmi(-1, perfctr_msr))
579 if (!__reserve_evntsel_nmi(-1, evntsel_msr))
582 wrmsrl(perfctr_msr, 0UL);
584 evntsel = K7_EVNTSEL_INT
589 /* setup the timer */
590 wrmsr(evntsel_msr, evntsel, 0);
591 write_watchdog_counter(perfctr_msr, "K7_PERFCTR0");
592 apic_write(APIC_LVTPC, APIC_DM_NMI);
593 evntsel |= K7_EVNTSEL_ENABLE;
594 wrmsr(evntsel_msr, evntsel, 0);
596 wd->perfctr_msr = perfctr_msr;
597 wd->evntsel_msr = evntsel_msr;
598 wd->cccr_msr = 0; //unused
599 wd->check_bit = 1ULL<<63;
602 __release_perfctr_nmi(-1, perfctr_msr);
607 static void stop_k7_watchdog(void)
609 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
611 wrmsr(wd->evntsel_msr, 0, 0);
613 __release_evntsel_nmi(-1, wd->evntsel_msr);
614 __release_perfctr_nmi(-1, wd->perfctr_msr);
617 #define P6_EVNTSEL0_ENABLE (1 << 22)
618 #define P6_EVNTSEL_INT (1 << 20)
619 #define P6_EVNTSEL_OS (1 << 17)
620 #define P6_EVNTSEL_USR (1 << 16)
621 #define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79
622 #define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED
624 static int setup_p6_watchdog(void)
626 unsigned int perfctr_msr, evntsel_msr;
627 unsigned int evntsel;
628 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
630 perfctr_msr = MSR_P6_PERFCTR0;
631 evntsel_msr = MSR_P6_EVNTSEL0;
632 if (!__reserve_perfctr_nmi(-1, perfctr_msr))
635 if (!__reserve_evntsel_nmi(-1, evntsel_msr))
638 wrmsrl(perfctr_msr, 0UL);
640 evntsel = P6_EVNTSEL_INT
645 /* setup the timer */
646 wrmsr(evntsel_msr, evntsel, 0);
647 nmi_hz = adjust_for_32bit_ctr(nmi_hz);
648 write_watchdog_counter32(perfctr_msr, "P6_PERFCTR0");
649 apic_write(APIC_LVTPC, APIC_DM_NMI);
650 evntsel |= P6_EVNTSEL0_ENABLE;
651 wrmsr(evntsel_msr, evntsel, 0);
653 wd->perfctr_msr = perfctr_msr;
654 wd->evntsel_msr = evntsel_msr;
655 wd->cccr_msr = 0; //unused
656 wd->check_bit = 1ULL<<39;
659 __release_perfctr_nmi(-1, perfctr_msr);
664 static void stop_p6_watchdog(void)
666 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
668 wrmsr(wd->evntsel_msr, 0, 0);
670 __release_evntsel_nmi(-1, wd->evntsel_msr);
671 __release_perfctr_nmi(-1, wd->perfctr_msr);
674 /* Note that these events don't tick when the CPU idles. This means
675 the frequency varies with CPU load. */
677 #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
678 #define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
679 #define P4_ESCR_OS (1<<3)
680 #define P4_ESCR_USR (1<<2)
681 #define P4_CCCR_OVF_PMI0 (1<<26)
682 #define P4_CCCR_OVF_PMI1 (1<<27)
683 #define P4_CCCR_THRESHOLD(N) ((N)<<20)
684 #define P4_CCCR_COMPLEMENT (1<<19)
685 #define P4_CCCR_COMPARE (1<<18)
686 #define P4_CCCR_REQUIRED (3<<16)
687 #define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
688 #define P4_CCCR_ENABLE (1<<12)
689 #define P4_CCCR_OVF (1<<31)
690 /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
691 CRU_ESCR0 (with any non-null event selector) through a complemented
692 max threshold. [IA32-Vol3, Section 14.9.9] */
694 static int setup_p4_watchdog(void)
696 unsigned int perfctr_msr, evntsel_msr, cccr_msr;
697 unsigned int evntsel, cccr_val;
698 unsigned int misc_enable, dummy;
700 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
702 rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy);
703 if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
707 /* detect which hyperthread we are on */
708 if (smp_num_siblings == 2) {
709 unsigned int ebx, apicid;
712 apicid = (ebx >> 24) & 0xff;
718 /* performance counters are shared resources
719 * assign each hyperthread its own set
720 * (re-use the ESCR0 register, seems safe
721 * and keeps the cccr_val the same)
725 perfctr_msr = MSR_P4_IQ_PERFCTR0;
726 evntsel_msr = MSR_P4_CRU_ESCR0;
727 cccr_msr = MSR_P4_IQ_CCCR0;
728 cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4);
731 perfctr_msr = MSR_P4_IQ_PERFCTR1;
732 evntsel_msr = MSR_P4_CRU_ESCR0;
733 cccr_msr = MSR_P4_IQ_CCCR1;
734 cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4);
737 if (!__reserve_perfctr_nmi(-1, perfctr_msr))
740 if (!__reserve_evntsel_nmi(-1, evntsel_msr))
743 evntsel = P4_ESCR_EVENT_SELECT(0x3F)
747 cccr_val |= P4_CCCR_THRESHOLD(15)
752 wrmsr(evntsel_msr, evntsel, 0);
753 wrmsr(cccr_msr, cccr_val, 0);
754 write_watchdog_counter(perfctr_msr, "P4_IQ_COUNTER0");
755 apic_write(APIC_LVTPC, APIC_DM_NMI);
756 cccr_val |= P4_CCCR_ENABLE;
757 wrmsr(cccr_msr, cccr_val, 0);
758 wd->perfctr_msr = perfctr_msr;
759 wd->evntsel_msr = evntsel_msr;
760 wd->cccr_msr = cccr_msr;
761 wd->check_bit = 1ULL<<39;
764 __release_perfctr_nmi(-1, perfctr_msr);
769 static void stop_p4_watchdog(void)
771 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
773 wrmsr(wd->cccr_msr, 0, 0);
774 wrmsr(wd->evntsel_msr, 0, 0);
776 __release_evntsel_nmi(-1, wd->evntsel_msr);
777 __release_perfctr_nmi(-1, wd->perfctr_msr);
780 #define ARCH_PERFMON_NMI_EVENT_SEL ARCH_PERFMON_UNHALTED_CORE_CYCLES_SEL
781 #define ARCH_PERFMON_NMI_EVENT_UMASK ARCH_PERFMON_UNHALTED_CORE_CYCLES_UMASK
783 static int setup_intel_arch_watchdog(void)
786 union cpuid10_eax eax;
788 unsigned int perfctr_msr, evntsel_msr;
789 unsigned int evntsel;
790 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
793 * Check whether the Architectural PerfMon supports
794 * Unhalted Core Cycles Event or not.
795 * NOTE: Corresponding bit = 0 in ebx indicates event present.
797 cpuid(10, &(eax.full), &ebx, &unused, &unused);
798 if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
799 (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
802 perfctr_msr = MSR_ARCH_PERFMON_PERFCTR0;
803 evntsel_msr = MSR_ARCH_PERFMON_EVENTSEL0;
805 if (!__reserve_perfctr_nmi(-1, perfctr_msr))
808 if (!__reserve_evntsel_nmi(-1, evntsel_msr))
811 wrmsrl(perfctr_msr, 0UL);
813 evntsel = ARCH_PERFMON_EVENTSEL_INT
814 | ARCH_PERFMON_EVENTSEL_OS
815 | ARCH_PERFMON_EVENTSEL_USR
816 | ARCH_PERFMON_NMI_EVENT_SEL
817 | ARCH_PERFMON_NMI_EVENT_UMASK;
819 /* setup the timer */
820 wrmsr(evntsel_msr, evntsel, 0);
821 nmi_hz = adjust_for_32bit_ctr(nmi_hz);
822 write_watchdog_counter32(perfctr_msr, "INTEL_ARCH_PERFCTR0");
823 apic_write(APIC_LVTPC, APIC_DM_NMI);
824 evntsel |= ARCH_PERFMON_EVENTSEL0_ENABLE;
825 wrmsr(evntsel_msr, evntsel, 0);
827 wd->perfctr_msr = perfctr_msr;
828 wd->evntsel_msr = evntsel_msr;
829 wd->cccr_msr = 0; //unused
830 wd->check_bit = 1ULL << (eax.split.bit_width - 1);
833 __release_perfctr_nmi(-1, perfctr_msr);
838 static void stop_intel_arch_watchdog(void)
841 union cpuid10_eax eax;
843 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
846 * Check whether the Architectural PerfMon supports
847 * Unhalted Core Cycles Event or not.
848 * NOTE: Corresponding bit = 0 in ebx indicates event present.
850 cpuid(10, &(eax.full), &ebx, &unused, &unused);
851 if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
852 (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
855 wrmsr(wd->evntsel_msr, 0, 0);
856 __release_evntsel_nmi(-1, wd->evntsel_msr);
857 __release_perfctr_nmi(-1, wd->perfctr_msr);
860 void setup_apic_nmi_watchdog (void *unused)
862 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
864 /* only support LOCAL and IO APICs for now */
865 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
866 (nmi_watchdog != NMI_IO_APIC))
869 if (wd->enabled == 1)
872 /* cheap hack to support suspend/resume */
873 /* if cpu0 is not active neither should the other cpus */
874 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
877 if (nmi_watchdog == NMI_LOCAL_APIC) {
878 switch (boot_cpu_data.x86_vendor) {
880 if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15 &&
881 boot_cpu_data.x86 != 16)
883 if (!setup_k7_watchdog())
886 case X86_VENDOR_INTEL:
887 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
888 if (!setup_intel_arch_watchdog())
892 switch (boot_cpu_data.x86) {
894 if (boot_cpu_data.x86_model > 0xd)
897 if (!setup_p6_watchdog())
901 if (boot_cpu_data.x86_model > 0x4)
904 if (!setup_p4_watchdog())
916 atomic_inc(&nmi_active);
919 void stop_apic_nmi_watchdog(void *unused)
921 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
923 /* only support LOCAL and IO APICs for now */
924 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
925 (nmi_watchdog != NMI_IO_APIC))
928 if (wd->enabled == 0)
931 if (nmi_watchdog == NMI_LOCAL_APIC) {
932 switch (boot_cpu_data.x86_vendor) {
936 case X86_VENDOR_INTEL:
937 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
938 stop_intel_arch_watchdog();
941 switch (boot_cpu_data.x86) {
943 if (boot_cpu_data.x86_model > 0xd)
948 if (boot_cpu_data.x86_model > 0x4)
959 atomic_dec(&nmi_active);
963 * the best way to detect whether a CPU has a 'hard lockup' problem
964 * is to check it's local APIC timer IRQ counts. If they are not
965 * changing then that CPU has some problem.
967 * as these watchdog NMI IRQs are generated on every CPU, we only
968 * have to check the current processor.
970 * since NMIs don't listen to _any_ locks, we have to be extremely
971 * careful not to rely on unsafe variables. The printk might lock
972 * up though, so we have to break up any console locks first ...
973 * [when there will be more tty-related locks, break them up
978 last_irq_sums [NR_CPUS],
979 alert_counter [NR_CPUS];
981 void touch_nmi_watchdog (void)
983 if (nmi_watchdog > 0) {
987 * Just reset the alert counters, (other CPUs might be
988 * spinning on locks we hold):
990 for_each_present_cpu (cpu)
991 alert_counter[cpu] = 0;
995 * Tickle the softlockup detector too:
997 touch_softlockup_watchdog();
999 EXPORT_SYMBOL(touch_nmi_watchdog);
1001 extern void die_nmi(struct pt_regs *, const char *msg);
1003 __kprobes int nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
1007 * Since current_thread_info()-> is always on the stack, and we
1008 * always switch the stack NMI-atomically, it's safe to use
1009 * smp_processor_id().
1013 int cpu = smp_processor_id();
1014 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
1018 /* check for other users first */
1019 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
1025 if (cpu_isset(cpu, backtrace_mask)) {
1026 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
1029 printk("NMI backtrace for cpu %d\n", cpu);
1032 cpu_clear(cpu, backtrace_mask);
1036 * Take the local apic timer and PIT/HPET into account. We don't
1037 * know which one is active, when we have highres/dyntick on
1039 sum = per_cpu(irq_stat, cpu).apic_timer_irqs + kstat_irqs(0);
1041 /* if the none of the timers isn't firing, this cpu isn't doing much */
1042 if (!touched && last_irq_sums[cpu] == sum) {
1044 * Ayiee, looks like this CPU is stuck ...
1045 * wait a few IRQs (5 seconds) before doing the oops ...
1047 alert_counter[cpu]++;
1048 if (alert_counter[cpu] == 5*nmi_hz)
1050 * die_nmi will return ONLY if NOTIFY_STOP happens..
1052 die_nmi(regs, "BUG: NMI Watchdog detected LOCKUP");
1054 last_irq_sums[cpu] = sum;
1055 alert_counter[cpu] = 0;
1057 /* see if the nmi watchdog went off */
1059 if (nmi_watchdog == NMI_LOCAL_APIC) {
1060 rdmsrl(wd->perfctr_msr, dummy);
1061 if (dummy & wd->check_bit){
1062 /* this wasn't a watchdog timer interrupt */
1066 /* only Intel P4 uses the cccr msr */
1067 if (wd->cccr_msr != 0) {
1070 * - An overflown perfctr will assert its interrupt
1071 * until the OVF flag in its CCCR is cleared.
1072 * - LVTPC is masked on interrupt and must be
1073 * unmasked by the LVTPC handler.
1075 rdmsrl(wd->cccr_msr, dummy);
1076 dummy &= ~P4_CCCR_OVF;
1077 wrmsrl(wd->cccr_msr, dummy);
1078 apic_write(APIC_LVTPC, APIC_DM_NMI);
1079 /* start the cycle over again */
1080 write_watchdog_counter(wd->perfctr_msr, NULL);
1082 else if (wd->perfctr_msr == MSR_P6_PERFCTR0 ||
1083 wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0) {
1084 /* P6 based Pentium M need to re-unmask
1085 * the apic vector but it doesn't hurt
1087 * ArchPerfom/Core Duo also needs this */
1088 apic_write(APIC_LVTPC, APIC_DM_NMI);
1089 /* P6/ARCH_PERFMON has 32 bit counter write */
1090 write_watchdog_counter32(wd->perfctr_msr, NULL);
1092 /* start the cycle over again */
1093 write_watchdog_counter(wd->perfctr_msr, NULL);
1096 } else if (nmi_watchdog == NMI_IO_APIC) {
1097 /* don't know how to accurately check for this.
1098 * just assume it was a watchdog timer interrupt
1099 * This matches the old behaviour.
1108 int do_nmi_callback(struct pt_regs * regs, int cpu)
1110 #ifdef CONFIG_SYSCTL
1111 if (unknown_nmi_panic)
1112 return unknown_nmi_panic_callback(regs, cpu);
1117 #ifdef CONFIG_SYSCTL
1119 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
1121 unsigned char reason = get_nmi_reason();
1124 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
1130 * proc handler for /proc/sys/kernel/nmi
1132 int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
1133 void __user *buffer, size_t *length, loff_t *ppos)
1137 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
1138 old_state = nmi_watchdog_enabled;
1139 proc_dointvec(table, write, file, buffer, length, ppos);
1140 if (!!old_state == !!nmi_watchdog_enabled)
1143 if (atomic_read(&nmi_active) < 0) {
1144 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
1148 if (nmi_watchdog == NMI_DEFAULT) {
1149 if (nmi_known_cpu() > 0)
1150 nmi_watchdog = NMI_LOCAL_APIC;
1152 nmi_watchdog = NMI_IO_APIC;
1155 if (nmi_watchdog == NMI_LOCAL_APIC) {
1156 if (nmi_watchdog_enabled)
1157 enable_lapic_nmi_watchdog();
1159 disable_lapic_nmi_watchdog();
1161 printk( KERN_WARNING
1162 "NMI watchdog doesn't know what hardware to touch\n");
1170 void __trigger_all_cpu_backtrace(void)
1174 backtrace_mask = cpu_online_map;
1175 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
1176 for (i = 0; i < 10 * 1000; i++) {
1177 if (cpus_empty(backtrace_mask))
1183 EXPORT_SYMBOL(nmi_active);
1184 EXPORT_SYMBOL(nmi_watchdog);
1185 EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi);
1186 EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
1187 EXPORT_SYMBOL(reserve_perfctr_nmi);
1188 EXPORT_SYMBOL(release_perfctr_nmi);
1189 EXPORT_SYMBOL(reserve_evntsel_nmi);
1190 EXPORT_SYMBOL(release_evntsel_nmi);
1191 EXPORT_SYMBOL(disable_timer_nmi_watchdog);
1192 EXPORT_SYMBOL(enable_timer_nmi_watchdog);