[PATCH] Mobil Pentium 4 HT and the NMI
[linux-2.6/x86.git] / arch / i386 / kernel / nmi.c
blob8c242bb1ef4571685cebeb2c11c0160a92bc9695
1 /*
2 * linux/arch/i386/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 * Mikael Pettersson : Pentium 4 support for local APIC NMI watchdog.
12 * Pavel Machek and
13 * Mikael Pettersson : PM converted to driver model. Disable/enable API.
16 #include <linux/config.h>
17 #include <linux/mm.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/sysctl.h>
30 #include <asm/smp.h>
31 #include <asm/div64.h>
32 #include <asm/nmi.h>
34 #include "mach_traps.h"
36 unsigned int nmi_watchdog = NMI_NONE;
37 extern int unknown_nmi_panic;
38 static unsigned int nmi_hz = HZ;
39 static unsigned int nmi_perfctr_msr; /* the MSR to reset in NMI handler */
40 static unsigned int nmi_p4_cccr_val;
41 extern void show_registers(struct pt_regs *regs);
44 * lapic_nmi_owner tracks the ownership of the lapic NMI hardware:
45 * - it may be reserved by some other driver, or not
46 * - when not reserved by some other driver, it may be used for
47 * the NMI watchdog, or not
49 * This is maintained separately from nmi_active because the NMI
50 * watchdog may also be driven from the I/O APIC timer.
52 static DEFINE_SPINLOCK(lapic_nmi_owner_lock);
53 static unsigned int lapic_nmi_owner;
54 #define LAPIC_NMI_WATCHDOG (1<<0)
55 #define LAPIC_NMI_RESERVED (1<<1)
57 /* nmi_active:
58 * +1: the lapic NMI watchdog is active, but can be disabled
59 * 0: the lapic NMI watchdog has not been set up, and cannot
60 * be enabled
61 * -1: the lapic NMI watchdog is disabled, but can be enabled
63 int nmi_active;
65 #define K7_EVNTSEL_ENABLE (1 << 22)
66 #define K7_EVNTSEL_INT (1 << 20)
67 #define K7_EVNTSEL_OS (1 << 17)
68 #define K7_EVNTSEL_USR (1 << 16)
69 #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
70 #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
72 #define P6_EVNTSEL0_ENABLE (1 << 22)
73 #define P6_EVNTSEL_INT (1 << 20)
74 #define P6_EVNTSEL_OS (1 << 17)
75 #define P6_EVNTSEL_USR (1 << 16)
76 #define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79
77 #define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED
79 #define MSR_P4_MISC_ENABLE 0x1A0
80 #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
81 #define MSR_P4_MISC_ENABLE_PEBS_UNAVAIL (1<<12)
82 #define MSR_P4_PERFCTR0 0x300
83 #define MSR_P4_CCCR0 0x360
84 #define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
85 #define P4_ESCR_OS (1<<3)
86 #define P4_ESCR_USR (1<<2)
87 #define P4_CCCR_OVF_PMI0 (1<<26)
88 #define P4_CCCR_OVF_PMI1 (1<<27)
89 #define P4_CCCR_THRESHOLD(N) ((N)<<20)
90 #define P4_CCCR_COMPLEMENT (1<<19)
91 #define P4_CCCR_COMPARE (1<<18)
92 #define P4_CCCR_REQUIRED (3<<16)
93 #define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
94 #define P4_CCCR_ENABLE (1<<12)
95 /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
96 CRU_ESCR0 (with any non-null event selector) through a complemented
97 max threshold. [IA32-Vol3, Section 14.9.9] */
98 #define MSR_P4_IQ_COUNTER0 0x30C
99 #define P4_NMI_CRU_ESCR0 (P4_ESCR_EVENT_SELECT(0x3F)|P4_ESCR_OS|P4_ESCR_USR)
100 #define P4_NMI_IQ_CCCR0 \
101 (P4_CCCR_OVF_PMI0|P4_CCCR_THRESHOLD(15)|P4_CCCR_COMPLEMENT| \
102 P4_CCCR_COMPARE|P4_CCCR_REQUIRED|P4_CCCR_ESCR_SELECT(4)|P4_CCCR_ENABLE)
104 static int __init check_nmi_watchdog(void)
106 unsigned int prev_nmi_count[NR_CPUS];
107 int cpu;
109 if (nmi_watchdog == NMI_NONE)
110 return 0;
112 printk(KERN_INFO "Testing NMI watchdog ... ");
114 for (cpu = 0; cpu < NR_CPUS; cpu++)
115 prev_nmi_count[cpu] = per_cpu(irq_stat, cpu).__nmi_count;
116 local_irq_enable();
117 mdelay((10*1000)/nmi_hz); // wait 10 ticks
119 for (cpu = 0; cpu < NR_CPUS; cpu++) {
120 #ifdef CONFIG_SMP
121 /* Check cpu_callin_map here because that is set
122 after the timer is started. */
123 if (!cpu_isset(cpu, cpu_callin_map))
124 continue;
125 #endif
126 if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
127 printk("CPU#%d: NMI appears to be stuck!\n", cpu);
128 nmi_active = 0;
129 lapic_nmi_owner &= ~LAPIC_NMI_WATCHDOG;
130 return -1;
133 printk("OK.\n");
135 /* now that we know it works we can reduce NMI frequency to
136 something more reasonable; makes a difference in some configs */
137 if (nmi_watchdog == NMI_LOCAL_APIC)
138 nmi_hz = 1;
140 return 0;
142 /* This needs to happen later in boot so counters are working */
143 late_initcall(check_nmi_watchdog);
145 static int __init setup_nmi_watchdog(char *str)
147 int nmi;
149 get_option(&str, &nmi);
151 if (nmi >= NMI_INVALID)
152 return 0;
153 if (nmi == NMI_NONE)
154 nmi_watchdog = nmi;
156 * If any other x86 CPU has a local APIC, then
157 * please test the NMI stuff there and send me the
158 * missing bits. Right now Intel P6/P4 and AMD K7 only.
160 if ((nmi == NMI_LOCAL_APIC) &&
161 (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
162 (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15))
163 nmi_watchdog = nmi;
164 if ((nmi == NMI_LOCAL_APIC) &&
165 (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) &&
166 (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15))
167 nmi_watchdog = nmi;
169 * We can enable the IO-APIC watchdog
170 * unconditionally.
172 if (nmi == NMI_IO_APIC) {
173 nmi_active = 1;
174 nmi_watchdog = nmi;
176 return 1;
179 __setup("nmi_watchdog=", setup_nmi_watchdog);
181 static void disable_lapic_nmi_watchdog(void)
183 if (nmi_active <= 0)
184 return;
185 switch (boot_cpu_data.x86_vendor) {
186 case X86_VENDOR_AMD:
187 wrmsr(MSR_K7_EVNTSEL0, 0, 0);
188 break;
189 case X86_VENDOR_INTEL:
190 switch (boot_cpu_data.x86) {
191 case 6:
192 if (boot_cpu_data.x86_model > 0xd)
193 break;
195 wrmsr(MSR_P6_EVNTSEL0, 0, 0);
196 break;
197 case 15:
198 if (boot_cpu_data.x86_model > 0x4)
199 break;
201 wrmsr(MSR_P4_IQ_CCCR0, 0, 0);
202 wrmsr(MSR_P4_CRU_ESCR0, 0, 0);
203 break;
205 break;
207 nmi_active = -1;
208 /* tell do_nmi() and others that we're not active any more */
209 nmi_watchdog = 0;
212 static void enable_lapic_nmi_watchdog(void)
214 if (nmi_active < 0) {
215 nmi_watchdog = NMI_LOCAL_APIC;
216 setup_apic_nmi_watchdog();
220 int reserve_lapic_nmi(void)
222 unsigned int old_owner;
224 spin_lock(&lapic_nmi_owner_lock);
225 old_owner = lapic_nmi_owner;
226 lapic_nmi_owner |= LAPIC_NMI_RESERVED;
227 spin_unlock(&lapic_nmi_owner_lock);
228 if (old_owner & LAPIC_NMI_RESERVED)
229 return -EBUSY;
230 if (old_owner & LAPIC_NMI_WATCHDOG)
231 disable_lapic_nmi_watchdog();
232 return 0;
235 void release_lapic_nmi(void)
237 unsigned int new_owner;
239 spin_lock(&lapic_nmi_owner_lock);
240 new_owner = lapic_nmi_owner & ~LAPIC_NMI_RESERVED;
241 lapic_nmi_owner = new_owner;
242 spin_unlock(&lapic_nmi_owner_lock);
243 if (new_owner & LAPIC_NMI_WATCHDOG)
244 enable_lapic_nmi_watchdog();
247 void disable_timer_nmi_watchdog(void)
249 if ((nmi_watchdog != NMI_IO_APIC) || (nmi_active <= 0))
250 return;
252 unset_nmi_callback();
253 nmi_active = -1;
254 nmi_watchdog = NMI_NONE;
257 void enable_timer_nmi_watchdog(void)
259 if (nmi_active < 0) {
260 nmi_watchdog = NMI_IO_APIC;
261 touch_nmi_watchdog();
262 nmi_active = 1;
266 #ifdef CONFIG_PM
268 static int nmi_pm_active; /* nmi_active before suspend */
270 static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
272 nmi_pm_active = nmi_active;
273 disable_lapic_nmi_watchdog();
274 return 0;
277 static int lapic_nmi_resume(struct sys_device *dev)
279 if (nmi_pm_active > 0)
280 enable_lapic_nmi_watchdog();
281 return 0;
285 static struct sysdev_class nmi_sysclass = {
286 set_kset_name("lapic_nmi"),
287 .resume = lapic_nmi_resume,
288 .suspend = lapic_nmi_suspend,
291 static struct sys_device device_lapic_nmi = {
292 .id = 0,
293 .cls = &nmi_sysclass,
296 static int __init init_lapic_nmi_sysfs(void)
298 int error;
300 if (nmi_active == 0 || nmi_watchdog != NMI_LOCAL_APIC)
301 return 0;
303 error = sysdev_class_register(&nmi_sysclass);
304 if (!error)
305 error = sysdev_register(&device_lapic_nmi);
306 return error;
308 /* must come after the local APIC's device_initcall() */
309 late_initcall(init_lapic_nmi_sysfs);
311 #endif /* CONFIG_PM */
314 * Activate the NMI watchdog via the local APIC.
315 * Original code written by Keith Owens.
318 static void clear_msr_range(unsigned int base, unsigned int n)
320 unsigned int i;
322 for(i = 0; i < n; ++i)
323 wrmsr(base+i, 0, 0);
326 static inline void write_watchdog_counter(const char *descr)
328 u64 count = (u64)cpu_khz * 1000;
330 do_div(count, nmi_hz);
331 if(descr)
332 Dprintk("setting %s to -0x%08Lx\n", descr, count);
333 wrmsrl(nmi_perfctr_msr, 0 - count);
336 static void setup_k7_watchdog(void)
338 unsigned int evntsel;
340 nmi_perfctr_msr = MSR_K7_PERFCTR0;
342 clear_msr_range(MSR_K7_EVNTSEL0, 4);
343 clear_msr_range(MSR_K7_PERFCTR0, 4);
345 evntsel = K7_EVNTSEL_INT
346 | K7_EVNTSEL_OS
347 | K7_EVNTSEL_USR
348 | K7_NMI_EVENT;
350 wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
351 write_watchdog_counter("K7_PERFCTR0");
352 apic_write(APIC_LVTPC, APIC_DM_NMI);
353 evntsel |= K7_EVNTSEL_ENABLE;
354 wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
357 static void setup_p6_watchdog(void)
359 unsigned int evntsel;
361 nmi_perfctr_msr = MSR_P6_PERFCTR0;
363 clear_msr_range(MSR_P6_EVNTSEL0, 2);
364 clear_msr_range(MSR_P6_PERFCTR0, 2);
366 evntsel = P6_EVNTSEL_INT
367 | P6_EVNTSEL_OS
368 | P6_EVNTSEL_USR
369 | P6_NMI_EVENT;
371 wrmsr(MSR_P6_EVNTSEL0, evntsel, 0);
372 write_watchdog_counter("P6_PERFCTR0");
373 apic_write(APIC_LVTPC, APIC_DM_NMI);
374 evntsel |= P6_EVNTSEL0_ENABLE;
375 wrmsr(MSR_P6_EVNTSEL0, evntsel, 0);
378 static int setup_p4_watchdog(void)
380 unsigned int misc_enable, dummy;
382 rdmsr(MSR_P4_MISC_ENABLE, misc_enable, dummy);
383 if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
384 return 0;
386 nmi_perfctr_msr = MSR_P4_IQ_COUNTER0;
387 nmi_p4_cccr_val = P4_NMI_IQ_CCCR0;
388 #ifdef CONFIG_SMP
389 if (smp_num_siblings == 2)
390 nmi_p4_cccr_val |= P4_CCCR_OVF_PMI1;
391 #endif
393 if (!(misc_enable & MSR_P4_MISC_ENABLE_PEBS_UNAVAIL))
394 clear_msr_range(0x3F1, 2);
395 /* MSR 0x3F0 seems to have a default value of 0xFC00, but current
396 docs doesn't fully define it, so leave it alone for now. */
397 if (boot_cpu_data.x86_model >= 0x3) {
398 /* MSR_P4_IQ_ESCR0/1 (0x3ba/0x3bb) removed */
399 clear_msr_range(0x3A0, 26);
400 clear_msr_range(0x3BC, 3);
401 } else {
402 clear_msr_range(0x3A0, 31);
404 clear_msr_range(0x3C0, 6);
405 clear_msr_range(0x3C8, 6);
406 clear_msr_range(0x3E0, 2);
407 clear_msr_range(MSR_P4_CCCR0, 18);
408 clear_msr_range(MSR_P4_PERFCTR0, 18);
410 wrmsr(MSR_P4_CRU_ESCR0, P4_NMI_CRU_ESCR0, 0);
411 wrmsr(MSR_P4_IQ_CCCR0, P4_NMI_IQ_CCCR0 & ~P4_CCCR_ENABLE, 0);
412 write_watchdog_counter("P4_IQ_COUNTER0");
413 apic_write(APIC_LVTPC, APIC_DM_NMI);
414 wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
415 return 1;
418 void setup_apic_nmi_watchdog (void)
420 switch (boot_cpu_data.x86_vendor) {
421 case X86_VENDOR_AMD:
422 if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15)
423 return;
424 setup_k7_watchdog();
425 break;
426 case X86_VENDOR_INTEL:
427 switch (boot_cpu_data.x86) {
428 case 6:
429 if (boot_cpu_data.x86_model > 0xd)
430 return;
432 setup_p6_watchdog();
433 break;
434 case 15:
435 if (boot_cpu_data.x86_model > 0x4)
436 return;
438 if (!setup_p4_watchdog())
439 return;
440 break;
441 default:
442 return;
444 break;
445 default:
446 return;
448 lapic_nmi_owner = LAPIC_NMI_WATCHDOG;
449 nmi_active = 1;
453 * the best way to detect whether a CPU has a 'hard lockup' problem
454 * is to check it's local APIC timer IRQ counts. If they are not
455 * changing then that CPU has some problem.
457 * as these watchdog NMI IRQs are generated on every CPU, we only
458 * have to check the current processor.
460 * since NMIs don't listen to _any_ locks, we have to be extremely
461 * careful not to rely on unsafe variables. The printk might lock
462 * up though, so we have to break up any console locks first ...
463 * [when there will be more tty-related locks, break them up
464 * here too!]
467 static unsigned int
468 last_irq_sums [NR_CPUS],
469 alert_counter [NR_CPUS];
471 void touch_nmi_watchdog (void)
473 int i;
476 * Just reset the alert counters, (other CPUs might be
477 * spinning on locks we hold):
479 for (i = 0; i < NR_CPUS; i++)
480 alert_counter[i] = 0;
483 extern void die_nmi(struct pt_regs *, const char *msg);
485 void nmi_watchdog_tick (struct pt_regs * regs)
489 * Since current_thread_info()-> is always on the stack, and we
490 * always switch the stack NMI-atomically, it's safe to use
491 * smp_processor_id().
493 int sum, cpu = smp_processor_id();
495 sum = per_cpu(irq_stat, cpu).apic_timer_irqs;
497 if (last_irq_sums[cpu] == sum) {
499 * Ayiee, looks like this CPU is stuck ...
500 * wait a few IRQs (5 seconds) before doing the oops ...
502 alert_counter[cpu]++;
503 if (alert_counter[cpu] == 5*nmi_hz)
504 die_nmi(regs, "NMI Watchdog detected LOCKUP");
505 } else {
506 last_irq_sums[cpu] = sum;
507 alert_counter[cpu] = 0;
509 if (nmi_perfctr_msr) {
510 if (nmi_perfctr_msr == MSR_P4_IQ_COUNTER0) {
512 * P4 quirks:
513 * - An overflown perfctr will assert its interrupt
514 * until the OVF flag in its CCCR is cleared.
515 * - LVTPC is masked on interrupt and must be
516 * unmasked by the LVTPC handler.
518 wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
519 apic_write(APIC_LVTPC, APIC_DM_NMI);
521 else if (nmi_perfctr_msr == MSR_P6_PERFCTR0) {
522 /* Only P6 based Pentium M need to re-unmask
523 * the apic vector but it doesn't hurt
524 * other P6 variant */
525 apic_write(APIC_LVTPC, APIC_DM_NMI);
527 write_watchdog_counter(NULL);
531 #ifdef CONFIG_SYSCTL
533 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
535 unsigned char reason = get_nmi_reason();
536 char buf[64];
538 if (!(reason & 0xc0)) {
539 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
540 die_nmi(regs, buf);
542 return 0;
546 * proc handler for /proc/sys/kernel/unknown_nmi_panic
548 int proc_unknown_nmi_panic(ctl_table *table, int write, struct file *file,
549 void __user *buffer, size_t *length, loff_t *ppos)
551 int old_state;
553 old_state = unknown_nmi_panic;
554 proc_dointvec(table, write, file, buffer, length, ppos);
555 if (!!old_state == !!unknown_nmi_panic)
556 return 0;
558 if (unknown_nmi_panic) {
559 if (reserve_lapic_nmi() < 0) {
560 unknown_nmi_panic = 0;
561 return -EBUSY;
562 } else {
563 set_nmi_callback(unknown_nmi_panic_callback);
565 } else {
566 release_lapic_nmi();
567 unset_nmi_callback();
569 return 0;
572 #endif
574 EXPORT_SYMBOL(nmi_active);
575 EXPORT_SYMBOL(nmi_watchdog);
576 EXPORT_SYMBOL(reserve_lapic_nmi);
577 EXPORT_SYMBOL(release_lapic_nmi);
578 EXPORT_SYMBOL(disable_timer_nmi_watchdog);
579 EXPORT_SYMBOL(enable_timer_nmi_watchdog);