[PATCH] i386/x86-64: Fix NMI watchdog suspend/resume
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86_64 / kernel / nmi.c
blob5a35975e5763686a65f70eda4f858f6dd5f39608
1 /*
2 * linux/arch/x86_64/nmi.c
4 * NMI watchdog support on APIC systems
6 * Started by Ingo Molnar <mingo@redhat.com>
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.
15 #include <linux/mm.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/sysdev.h>
20 #include <linux/nmi.h>
21 #include <linux/sysctl.h>
22 #include <linux/kprobes.h>
24 #include <asm/smp.h>
25 #include <asm/nmi.h>
26 #include <asm/proto.h>
27 #include <asm/kdebug.h>
28 #include <asm/mce.h>
30 /* perfctr_nmi_owner tracks the ownership of the perfctr registers:
31 * evtsel_nmi_owner tracks the ownership of the event selection
32 * - different performance counters/ event selection may be reserved for
33 * different subsystems this reservation system just tries to coordinate
34 * things a little
36 static DEFINE_PER_CPU(unsigned, perfctr_nmi_owner);
37 static DEFINE_PER_CPU(unsigned, evntsel_nmi_owner[2]);
39 /* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's
40 * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now)
42 #define NMI_MAX_COUNTER_BITS 66
44 /* nmi_active:
45 * >0: the lapic NMI watchdog is active, but can be disabled
46 * <0: the lapic NMI watchdog has not been set up, and cannot
47 * be enabled
48 * 0: the lapic NMI watchdog is disabled, but can be enabled
50 atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
51 int panic_on_timeout;
53 unsigned int nmi_watchdog = NMI_DEFAULT;
54 static unsigned int nmi_hz = HZ;
56 struct nmi_watchdog_ctlblk {
57 int enabled;
58 u64 check_bit;
59 unsigned int cccr_msr;
60 unsigned int perfctr_msr; /* the MSR to reset in NMI handler */
61 unsigned int evntsel_msr; /* the MSR to select the events to handle */
63 static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk);
65 /* local prototypes */
66 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
68 /* converts an msr to an appropriate reservation bit */
69 static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
71 /* returns the bit offset of the performance counter register */
72 switch (boot_cpu_data.x86_vendor) {
73 case X86_VENDOR_AMD:
74 return (msr - MSR_K7_PERFCTR0);
75 case X86_VENDOR_INTEL:
76 return (msr - MSR_P4_BPU_PERFCTR0);
78 return 0;
81 /* converts an msr to an appropriate reservation bit */
82 static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr)
84 /* returns the bit offset of the event selection register */
85 switch (boot_cpu_data.x86_vendor) {
86 case X86_VENDOR_AMD:
87 return (msr - MSR_K7_EVNTSEL0);
88 case X86_VENDOR_INTEL:
89 return (msr - MSR_P4_BSU_ESCR0);
91 return 0;
94 /* checks for a bit availability (hack for oprofile) */
95 int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
97 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
99 return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
102 /* checks the an msr for availability */
103 int avail_to_resrv_perfctr_nmi(unsigned int msr)
105 unsigned int counter;
107 counter = nmi_perfctr_msr_to_bit(msr);
108 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
110 return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
113 int reserve_perfctr_nmi(unsigned int msr)
115 unsigned int counter;
117 counter = nmi_perfctr_msr_to_bit(msr);
118 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
120 if (!test_and_set_bit(counter, &__get_cpu_var(perfctr_nmi_owner)))
121 return 1;
122 return 0;
125 void release_perfctr_nmi(unsigned int msr)
127 unsigned int counter;
129 counter = nmi_perfctr_msr_to_bit(msr);
130 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
132 clear_bit(counter, &__get_cpu_var(perfctr_nmi_owner));
135 int reserve_evntsel_nmi(unsigned int msr)
137 unsigned int counter;
139 counter = nmi_evntsel_msr_to_bit(msr);
140 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
142 if (!test_and_set_bit(counter, &__get_cpu_var(evntsel_nmi_owner)))
143 return 1;
144 return 0;
147 void release_evntsel_nmi(unsigned int msr)
149 unsigned int counter;
151 counter = nmi_evntsel_msr_to_bit(msr);
152 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
154 clear_bit(counter, &__get_cpu_var(evntsel_nmi_owner));
157 static __cpuinit inline int nmi_known_cpu(void)
159 switch (boot_cpu_data.x86_vendor) {
160 case X86_VENDOR_AMD:
161 return boot_cpu_data.x86 == 15;
162 case X86_VENDOR_INTEL:
163 return boot_cpu_data.x86 == 15;
165 return 0;
168 /* Run after command line and cpu_init init, but before all other checks */
169 void nmi_watchdog_default(void)
171 if (nmi_watchdog != NMI_DEFAULT)
172 return;
173 if (nmi_known_cpu())
174 nmi_watchdog = NMI_LOCAL_APIC;
175 else
176 nmi_watchdog = NMI_IO_APIC;
179 #ifdef CONFIG_SMP
180 /* The performance counters used by NMI_LOCAL_APIC don't trigger when
181 * the CPU is idle. To make sure the NMI watchdog really ticks on all
182 * CPUs during the test make them busy.
184 static __init void nmi_cpu_busy(void *data)
186 volatile int *endflag = data;
187 local_irq_enable_in_hardirq();
188 /* Intentionally don't use cpu_relax here. This is
189 to make sure that the performance counter really ticks,
190 even if there is a simulator or similar that catches the
191 pause instruction. On a real HT machine this is fine because
192 all other CPUs are busy with "useless" delay loops and don't
193 care if they get somewhat less cycles. */
194 while (*endflag == 0)
195 barrier();
197 #endif
199 int __init check_nmi_watchdog (void)
201 volatile int endflag = 0;
202 int *counts;
203 int cpu;
205 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT))
206 return 0;
208 if (!atomic_read(&nmi_active))
209 return 0;
211 counts = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
212 if (!counts)
213 return -1;
215 printk(KERN_INFO "testing NMI watchdog ... ");
217 #ifdef CONFIG_SMP
218 if (nmi_watchdog == NMI_LOCAL_APIC)
219 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
220 #endif
222 for (cpu = 0; cpu < NR_CPUS; cpu++)
223 counts[cpu] = cpu_pda(cpu)->__nmi_count;
224 local_irq_enable();
225 mdelay((10*1000)/nmi_hz); // wait 10 ticks
227 for_each_online_cpu(cpu) {
228 if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled)
229 continue;
230 if (cpu_pda(cpu)->__nmi_count - counts[cpu] <= 5) {
231 printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
232 cpu,
233 counts[cpu],
234 cpu_pda(cpu)->__nmi_count);
235 per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0;
236 atomic_dec(&nmi_active);
239 if (!atomic_read(&nmi_active)) {
240 kfree(counts);
241 atomic_set(&nmi_active, -1);
242 return -1;
244 endflag = 1;
245 printk("OK.\n");
247 /* now that we know it works we can reduce NMI frequency to
248 something more reasonable; makes a difference in some configs */
249 if (nmi_watchdog == NMI_LOCAL_APIC)
250 nmi_hz = 1;
252 kfree(counts);
253 return 0;
256 int __init setup_nmi_watchdog(char *str)
258 int nmi;
260 if (!strncmp(str,"panic",5)) {
261 panic_on_timeout = 1;
262 str = strchr(str, ',');
263 if (!str)
264 return 1;
265 ++str;
268 get_option(&str, &nmi);
270 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
271 return 0;
273 if ((nmi == NMI_LOCAL_APIC) && (nmi_known_cpu() == 0))
274 return 0; /* no lapic support */
275 nmi_watchdog = nmi;
276 return 1;
279 __setup("nmi_watchdog=", setup_nmi_watchdog);
281 static void disable_lapic_nmi_watchdog(void)
283 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
285 if (atomic_read(&nmi_active) <= 0)
286 return;
288 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
290 BUG_ON(atomic_read(&nmi_active) != 0);
293 static void enable_lapic_nmi_watchdog(void)
295 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
297 /* are we already enabled */
298 if (atomic_read(&nmi_active) != 0)
299 return;
301 /* are we lapic aware */
302 if (nmi_known_cpu() <= 0)
303 return;
305 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
306 touch_nmi_watchdog();
309 void disable_timer_nmi_watchdog(void)
311 BUG_ON(nmi_watchdog != NMI_IO_APIC);
313 if (atomic_read(&nmi_active) <= 0)
314 return;
316 disable_irq(0);
317 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
319 BUG_ON(atomic_read(&nmi_active) != 0);
322 void enable_timer_nmi_watchdog(void)
324 BUG_ON(nmi_watchdog != NMI_IO_APIC);
326 if (atomic_read(&nmi_active) == 0) {
327 touch_nmi_watchdog();
328 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
329 enable_irq(0);
333 #ifdef CONFIG_PM
335 static int nmi_pm_active; /* nmi_active before suspend */
337 static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
339 /* only CPU0 goes here, other CPUs should be offline */
340 nmi_pm_active = atomic_read(&nmi_active);
341 stop_apic_nmi_watchdog(NULL);
342 BUG_ON(atomic_read(&nmi_active) != 0);
343 return 0;
346 static int lapic_nmi_resume(struct sys_device *dev)
348 /* only CPU0 goes here, other CPUs should be offline */
349 if (nmi_pm_active > 0) {
350 setup_apic_nmi_watchdog(NULL);
351 touch_nmi_watchdog();
353 return 0;
356 static struct sysdev_class nmi_sysclass = {
357 set_kset_name("lapic_nmi"),
358 .resume = lapic_nmi_resume,
359 .suspend = lapic_nmi_suspend,
362 static struct sys_device device_lapic_nmi = {
363 .id = 0,
364 .cls = &nmi_sysclass,
367 static int __init init_lapic_nmi_sysfs(void)
369 int error;
371 /* should really be a BUG_ON but b/c this is an
372 * init call, it just doesn't work. -dcz
374 if (nmi_watchdog != NMI_LOCAL_APIC)
375 return 0;
377 if ( atomic_read(&nmi_active) < 0 )
378 return 0;
380 error = sysdev_class_register(&nmi_sysclass);
381 if (!error)
382 error = sysdev_register(&device_lapic_nmi);
383 return error;
385 /* must come after the local APIC's device_initcall() */
386 late_initcall(init_lapic_nmi_sysfs);
388 #endif /* CONFIG_PM */
391 * Activate the NMI watchdog via the local APIC.
392 * Original code written by Keith Owens.
395 /* Note that these events don't tick when the CPU idles. This means
396 the frequency varies with CPU load. */
398 #define K7_EVNTSEL_ENABLE (1 << 22)
399 #define K7_EVNTSEL_INT (1 << 20)
400 #define K7_EVNTSEL_OS (1 << 17)
401 #define K7_EVNTSEL_USR (1 << 16)
402 #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
403 #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
405 static int setup_k7_watchdog(void)
407 unsigned int perfctr_msr, evntsel_msr;
408 unsigned int evntsel;
409 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
411 perfctr_msr = MSR_K7_PERFCTR0;
412 evntsel_msr = MSR_K7_EVNTSEL0;
413 if (!reserve_perfctr_nmi(perfctr_msr))
414 goto fail;
416 if (!reserve_evntsel_nmi(evntsel_msr))
417 goto fail1;
419 /* Simulator may not support it */
420 if (checking_wrmsrl(evntsel_msr, 0UL))
421 goto fail2;
422 wrmsrl(perfctr_msr, 0UL);
424 evntsel = K7_EVNTSEL_INT
425 | K7_EVNTSEL_OS
426 | K7_EVNTSEL_USR
427 | K7_NMI_EVENT;
429 /* setup the timer */
430 wrmsr(evntsel_msr, evntsel, 0);
431 wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
432 apic_write(APIC_LVTPC, APIC_DM_NMI);
433 evntsel |= K7_EVNTSEL_ENABLE;
434 wrmsr(evntsel_msr, evntsel, 0);
436 wd->perfctr_msr = perfctr_msr;
437 wd->evntsel_msr = evntsel_msr;
438 wd->cccr_msr = 0; //unused
439 wd->check_bit = 1ULL<<63;
440 return 1;
441 fail2:
442 release_evntsel_nmi(evntsel_msr);
443 fail1:
444 release_perfctr_nmi(perfctr_msr);
445 fail:
446 return 0;
449 static void stop_k7_watchdog(void)
451 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
453 wrmsr(wd->evntsel_msr, 0, 0);
455 release_evntsel_nmi(wd->evntsel_msr);
456 release_perfctr_nmi(wd->perfctr_msr);
459 /* Note that these events don't tick when the CPU idles. This means
460 the frequency varies with CPU load. */
462 #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
463 #define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
464 #define P4_ESCR_OS (1<<3)
465 #define P4_ESCR_USR (1<<2)
466 #define P4_CCCR_OVF_PMI0 (1<<26)
467 #define P4_CCCR_OVF_PMI1 (1<<27)
468 #define P4_CCCR_THRESHOLD(N) ((N)<<20)
469 #define P4_CCCR_COMPLEMENT (1<<19)
470 #define P4_CCCR_COMPARE (1<<18)
471 #define P4_CCCR_REQUIRED (3<<16)
472 #define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
473 #define P4_CCCR_ENABLE (1<<12)
474 #define P4_CCCR_OVF (1<<31)
475 /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
476 CRU_ESCR0 (with any non-null event selector) through a complemented
477 max threshold. [IA32-Vol3, Section 14.9.9] */
479 static int setup_p4_watchdog(void)
481 unsigned int perfctr_msr, evntsel_msr, cccr_msr;
482 unsigned int evntsel, cccr_val;
483 unsigned int misc_enable, dummy;
484 unsigned int ht_num;
485 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
487 rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy);
488 if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
489 return 0;
491 #ifdef CONFIG_SMP
492 /* detect which hyperthread we are on */
493 if (smp_num_siblings == 2) {
494 unsigned int ebx, apicid;
496 ebx = cpuid_ebx(1);
497 apicid = (ebx >> 24) & 0xff;
498 ht_num = apicid & 1;
499 } else
500 #endif
501 ht_num = 0;
503 /* performance counters are shared resources
504 * assign each hyperthread its own set
505 * (re-use the ESCR0 register, seems safe
506 * and keeps the cccr_val the same)
508 if (!ht_num) {
509 /* logical cpu 0 */
510 perfctr_msr = MSR_P4_IQ_PERFCTR0;
511 evntsel_msr = MSR_P4_CRU_ESCR0;
512 cccr_msr = MSR_P4_IQ_CCCR0;
513 cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4);
514 } else {
515 /* logical cpu 1 */
516 perfctr_msr = MSR_P4_IQ_PERFCTR1;
517 evntsel_msr = MSR_P4_CRU_ESCR0;
518 cccr_msr = MSR_P4_IQ_CCCR1;
519 cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4);
522 if (!reserve_perfctr_nmi(perfctr_msr))
523 goto fail;
525 if (!reserve_evntsel_nmi(evntsel_msr))
526 goto fail1;
528 evntsel = P4_ESCR_EVENT_SELECT(0x3F)
529 | P4_ESCR_OS
530 | P4_ESCR_USR;
532 cccr_val |= P4_CCCR_THRESHOLD(15)
533 | P4_CCCR_COMPLEMENT
534 | P4_CCCR_COMPARE
535 | P4_CCCR_REQUIRED;
537 wrmsr(evntsel_msr, evntsel, 0);
538 wrmsr(cccr_msr, cccr_val, 0);
539 wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
540 apic_write(APIC_LVTPC, APIC_DM_NMI);
541 cccr_val |= P4_CCCR_ENABLE;
542 wrmsr(cccr_msr, cccr_val, 0);
544 wd->perfctr_msr = perfctr_msr;
545 wd->evntsel_msr = evntsel_msr;
546 wd->cccr_msr = cccr_msr;
547 wd->check_bit = 1ULL<<39;
548 return 1;
549 fail1:
550 release_perfctr_nmi(perfctr_msr);
551 fail:
552 return 0;
555 static void stop_p4_watchdog(void)
557 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
559 wrmsr(wd->cccr_msr, 0, 0);
560 wrmsr(wd->evntsel_msr, 0, 0);
562 release_evntsel_nmi(wd->evntsel_msr);
563 release_perfctr_nmi(wd->perfctr_msr);
566 void setup_apic_nmi_watchdog(void *unused)
568 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
570 /* only support LOCAL and IO APICs for now */
571 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
572 (nmi_watchdog != NMI_IO_APIC))
573 return;
575 if (wd->enabled == 1)
576 return;
578 /* cheap hack to support suspend/resume */
579 /* if cpu0 is not active neither should the other cpus */
580 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
581 return;
583 if (nmi_watchdog == NMI_LOCAL_APIC) {
584 switch (boot_cpu_data.x86_vendor) {
585 case X86_VENDOR_AMD:
586 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
587 return;
588 if (!setup_k7_watchdog())
589 return;
590 break;
591 case X86_VENDOR_INTEL:
592 if (!setup_p4_watchdog())
593 return;
594 break;
595 default:
596 return;
599 wd->enabled = 1;
600 atomic_inc(&nmi_active);
603 void stop_apic_nmi_watchdog(void *unused)
605 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
607 /* only support LOCAL and IO APICs for now */
608 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
609 (nmi_watchdog != NMI_IO_APIC))
610 return;
612 if (wd->enabled == 0)
613 return;
615 if (nmi_watchdog == NMI_LOCAL_APIC) {
616 switch (boot_cpu_data.x86_vendor) {
617 case X86_VENDOR_AMD:
618 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
619 return;
620 stop_k7_watchdog();
621 break;
622 case X86_VENDOR_INTEL:
623 stop_p4_watchdog();
624 break;
625 default:
626 return;
629 wd->enabled = 0;
630 atomic_dec(&nmi_active);
634 * the best way to detect whether a CPU has a 'hard lockup' problem
635 * is to check it's local APIC timer IRQ counts. If they are not
636 * changing then that CPU has some problem.
638 * as these watchdog NMI IRQs are generated on every CPU, we only
639 * have to check the current processor.
642 static DEFINE_PER_CPU(unsigned, last_irq_sum);
643 static DEFINE_PER_CPU(local_t, alert_counter);
644 static DEFINE_PER_CPU(int, nmi_touch);
646 void touch_nmi_watchdog (void)
648 if (nmi_watchdog > 0) {
649 unsigned cpu;
652 * Tell other CPUs to reset their alert counters. We cannot
653 * do it ourselves because the alert count increase is not
654 * atomic.
656 for_each_present_cpu (cpu)
657 per_cpu(nmi_touch, cpu) = 1;
660 touch_softlockup_watchdog();
663 int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
665 int sum;
666 int touched = 0;
667 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
668 u64 dummy;
669 int rc=0;
671 /* check for other users first */
672 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
673 == NOTIFY_STOP) {
674 rc = 1;
675 touched = 1;
678 sum = read_pda(apic_timer_irqs);
679 if (__get_cpu_var(nmi_touch)) {
680 __get_cpu_var(nmi_touch) = 0;
681 touched = 1;
684 #ifdef CONFIG_X86_MCE
685 /* Could check oops_in_progress here too, but it's safer
686 not too */
687 if (atomic_read(&mce_entry) > 0)
688 touched = 1;
689 #endif
690 /* if the apic timer isn't firing, this cpu isn't doing much */
691 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
693 * Ayiee, looks like this CPU is stuck ...
694 * wait a few IRQs (5 seconds) before doing the oops ...
696 local_inc(&__get_cpu_var(alert_counter));
697 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
698 die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs);
699 } else {
700 __get_cpu_var(last_irq_sum) = sum;
701 local_set(&__get_cpu_var(alert_counter), 0);
704 /* see if the nmi watchdog went off */
705 if (wd->enabled) {
706 if (nmi_watchdog == NMI_LOCAL_APIC) {
707 rdmsrl(wd->perfctr_msr, dummy);
708 if (dummy & wd->check_bit){
709 /* this wasn't a watchdog timer interrupt */
710 goto done;
713 /* only Intel uses the cccr msr */
714 if (wd->cccr_msr != 0) {
716 * P4 quirks:
717 * - An overflown perfctr will assert its interrupt
718 * until the OVF flag in its CCCR is cleared.
719 * - LVTPC is masked on interrupt and must be
720 * unmasked by the LVTPC handler.
722 rdmsrl(wd->cccr_msr, dummy);
723 dummy &= ~P4_CCCR_OVF;
724 wrmsrl(wd->cccr_msr, dummy);
725 apic_write(APIC_LVTPC, APIC_DM_NMI);
727 /* start the cycle over again */
728 wrmsrl(wd->perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
729 rc = 1;
730 } else if (nmi_watchdog == NMI_IO_APIC) {
731 /* don't know how to accurately check for this.
732 * just assume it was a watchdog timer interrupt
733 * This matches the old behaviour.
735 rc = 1;
736 } else
737 printk(KERN_WARNING "Unknown enabled NMI hardware?!\n");
739 done:
740 return rc;
743 asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
745 nmi_enter();
746 add_pda(__nmi_count,1);
747 default_do_nmi(regs);
748 nmi_exit();
751 int do_nmi_callback(struct pt_regs * regs, int cpu)
753 #ifdef CONFIG_SYSCTL
754 if (unknown_nmi_panic)
755 return unknown_nmi_panic_callback(regs, cpu);
756 #endif
757 return 0;
760 #ifdef CONFIG_SYSCTL
762 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
764 unsigned char reason = get_nmi_reason();
765 char buf[64];
767 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
768 die_nmi(buf,regs);
769 return 0;
773 * proc handler for /proc/sys/kernel/nmi
775 int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
776 void __user *buffer, size_t *length, loff_t *ppos)
778 int old_state;
780 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
781 old_state = nmi_watchdog_enabled;
782 proc_dointvec(table, write, file, buffer, length, ppos);
783 if (!!old_state == !!nmi_watchdog_enabled)
784 return 0;
786 if (atomic_read(&nmi_active) < 0) {
787 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
788 return -EIO;
791 /* if nmi_watchdog is not set yet, then set it */
792 nmi_watchdog_default();
794 if (nmi_watchdog == NMI_LOCAL_APIC) {
795 if (nmi_watchdog_enabled)
796 enable_lapic_nmi_watchdog();
797 else
798 disable_lapic_nmi_watchdog();
799 } else {
800 printk( KERN_WARNING
801 "NMI watchdog doesn't know what hardware to touch\n");
802 return -EIO;
804 return 0;
807 #endif
809 EXPORT_SYMBOL(nmi_active);
810 EXPORT_SYMBOL(nmi_watchdog);
811 EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi);
812 EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
813 EXPORT_SYMBOL(reserve_perfctr_nmi);
814 EXPORT_SYMBOL(release_perfctr_nmi);
815 EXPORT_SYMBOL(reserve_evntsel_nmi);
816 EXPORT_SYMBOL(release_evntsel_nmi);
817 EXPORT_SYMBOL(disable_timer_nmi_watchdog);
818 EXPORT_SYMBOL(enable_timer_nmi_watchdog);
819 EXPORT_SYMBOL(touch_nmi_watchdog);