7a2cdb5578a30c3b3b2790b3cb5b190615a1943e
[linux-2.6.git] / arch / i386 / kernel / nmi.c
1 /*
2  *  linux/arch/i386/nmi.c
3  *
4  *  NMI watchdog support on APIC systems
5  *
6  *  Started by Ingo Molnar <mingo@redhat.com>
7  *
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.
14  */
15
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/dump.h>
29
30 #include <asm/smp.h>
31 #include <asm/mtrr.h>
32 #include <asm/mpspec.h>
33 #include <asm/nmi.h>
34
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);
40
41 /*
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
46  *
47  * This is maintained separately from nmi_active because the NMI
48  * watchdog may also be driven from the I/O APIC timer.
49  */
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)
54
55 /* nmi_active:
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
58  *     be enabled
59  * -1: the lapic NMI watchdog is disabled, but can be enabled
60  */
61 int nmi_active;
62
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
69
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
76
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)
101
102 int __init check_nmi_watchdog (void)
103 {
104         unsigned int prev_nmi_count[NR_CPUS];
105         int cpu;
106
107         printk(KERN_INFO "testing NMI watchdog ... ");
108
109         for (cpu = 0; cpu < NR_CPUS; cpu++)
110                 prev_nmi_count[cpu] = irq_stat[cpu].__nmi_count;
111         local_irq_enable();
112         mdelay((10*1000)/nmi_hz); // wait 10 ticks
113
114         /* FIXME: Only boot CPU is online at this stage.  Check CPUs
115            as they come up. */
116         for (cpu = 0; cpu < NR_CPUS; cpu++) {
117                 if (!cpu_online(cpu))
118                         continue;
119                 if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
120                         printk("CPU#%d: NMI appears to be stuck!\n", cpu);
121                         nmi_active = 0;
122                         lapic_nmi_owner &= ~LAPIC_NMI_WATCHDOG;
123                         return -1;
124                 }
125         }
126         printk("OK.\n");
127
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)
131                 nmi_hz = 1;
132
133         return 0;
134 }
135
136 static int __init setup_nmi_watchdog(char *str)
137 {
138         int nmi;
139
140         get_option(&str, &nmi);
141
142         if (nmi >= NMI_INVALID)
143                 return 0;
144         if (nmi == NMI_NONE)
145                 nmi_watchdog = nmi;
146         /*
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.
150          */
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))
154                 nmi_watchdog = nmi;
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))
158                 nmi_watchdog = nmi;
159         /*
160          * We can enable the IO-APIC watchdog
161          * unconditionally.
162          */
163         if (nmi == NMI_IO_APIC) {
164                 nmi_active = 1;
165                 nmi_watchdog = nmi;
166         }
167         return 1;
168 }
169
170 __setup("nmi_watchdog=", setup_nmi_watchdog);
171
172 static void disable_lapic_nmi_watchdog(void)
173 {
174         if (nmi_active <= 0)
175                 return;
176         switch (boot_cpu_data.x86_vendor) {
177         case X86_VENDOR_AMD:
178                 wrmsr(MSR_K7_EVNTSEL0, 0, 0);
179                 break;
180         case X86_VENDOR_INTEL:
181                 switch (boot_cpu_data.x86) {
182                 case 6:
183                         if (boot_cpu_data.x86_model > 0xd)
184                                 break;
185
186                         wrmsr(MSR_P6_EVNTSEL0, 0, 0);
187                         break;
188                 case 15:
189                         if (boot_cpu_data.x86_model > 0x3)
190                                 break;
191
192                         wrmsr(MSR_P4_IQ_CCCR0, 0, 0);
193                         wrmsr(MSR_P4_CRU_ESCR0, 0, 0);
194                         break;
195                 }
196                 break;
197         }
198         nmi_active = -1;
199         /* tell do_nmi() and others that we're not active any more */
200         nmi_watchdog = 0;
201 }
202
203 static void enable_lapic_nmi_watchdog(void)
204 {
205         if (nmi_active < 0) {
206                 nmi_watchdog = NMI_LOCAL_APIC;
207                 setup_apic_nmi_watchdog();
208         }
209 }
210
211 int reserve_lapic_nmi(void)
212 {
213         unsigned int old_owner;
214
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)
220                 return -EBUSY;
221         if (old_owner & LAPIC_NMI_WATCHDOG)
222                 disable_lapic_nmi_watchdog();
223         return 0;
224 }
225
226 void release_lapic_nmi(void)
227 {
228         unsigned int new_owner;
229
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();
236 }
237
238 void disable_timer_nmi_watchdog(void)
239 {
240         if ((nmi_watchdog != NMI_IO_APIC) || (nmi_active <= 0))
241                 return;
242
243         unset_nmi_callback();
244         nmi_active = -1;
245         nmi_watchdog = NMI_NONE;
246 }
247
248 void enable_timer_nmi_watchdog(void)
249 {
250         if (nmi_active < 0) {
251                 nmi_watchdog = NMI_IO_APIC;
252                 touch_nmi_watchdog();
253                 nmi_active = 1;
254         }
255 }
256
257 #ifdef CONFIG_PM
258
259 static int nmi_pm_active; /* nmi_active before suspend */
260
261 static int lapic_nmi_suspend(struct sys_device *dev, u32 state)
262 {
263         nmi_pm_active = nmi_active;
264         disable_lapic_nmi_watchdog();
265         return 0;
266 }
267
268 static int lapic_nmi_resume(struct sys_device *dev)
269 {
270         if (nmi_pm_active > 0)
271                 enable_lapic_nmi_watchdog();
272         return 0;
273 }
274
275
276 static struct sysdev_class nmi_sysclass = {
277         set_kset_name("lapic_nmi"),
278         .resume         = lapic_nmi_resume,
279         .suspend        = lapic_nmi_suspend,
280 };
281
282 static struct sys_device device_lapic_nmi = {
283         .id     = 0,
284         .cls    = &nmi_sysclass,
285 };
286
287 static int __init init_lapic_nmi_sysfs(void)
288 {
289         int error;
290
291         if (nmi_active == 0 || nmi_watchdog != NMI_LOCAL_APIC)
292                 return 0;
293
294         error = sysdev_class_register(&nmi_sysclass);
295         if (!error)
296                 error = sysdev_register(&device_lapic_nmi);
297         return error;
298 }
299 /* must come after the local APIC's device_initcall() */
300 late_initcall(init_lapic_nmi_sysfs);
301
302 #endif  /* CONFIG_PM */
303
304 /*
305  * Activate the NMI watchdog via the local APIC.
306  * Original code written by Keith Owens.
307  */
308
309 static void clear_msr_range(unsigned int base, unsigned int n)
310 {
311         unsigned int i;
312
313         for(i = 0; i < n; ++i)
314                 wrmsr(base+i, 0, 0);
315 }
316
317 static void setup_k7_watchdog(void)
318 {
319         unsigned int evntsel;
320
321         nmi_perfctr_msr = MSR_K7_PERFCTR0;
322
323         clear_msr_range(MSR_K7_EVNTSEL0, 4);
324         clear_msr_range(MSR_K7_PERFCTR0, 4);
325
326         evntsel = K7_EVNTSEL_INT
327                 | K7_EVNTSEL_OS
328                 | K7_EVNTSEL_USR
329                 | K7_NMI_EVENT;
330
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);
337 }
338
339 static void setup_p6_watchdog(void)
340 {
341         unsigned int evntsel;
342
343         nmi_perfctr_msr = MSR_P6_PERFCTR0;
344
345         clear_msr_range(MSR_P6_EVNTSEL0, 2);
346         clear_msr_range(MSR_P6_PERFCTR0, 2);
347
348         evntsel = P6_EVNTSEL_INT
349                 | P6_EVNTSEL_OS
350                 | P6_EVNTSEL_USR
351                 | P6_NMI_EVENT;
352
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);
359 }
360
361 static int setup_p4_watchdog(void)
362 {
363         unsigned int misc_enable, dummy;
364
365         rdmsr(MSR_P4_MISC_ENABLE, misc_enable, dummy);
366         if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
367                 return 0;
368
369         nmi_perfctr_msr = MSR_P4_IQ_COUNTER0;
370         nmi_p4_cccr_val = P4_NMI_IQ_CCCR0;
371 #ifdef CONFIG_SMP
372         if (smp_num_siblings == 2)
373                 nmi_p4_cccr_val |= P4_CCCR_OVF_PMI1;
374 #endif
375
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);
386
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);
393         return 1;
394 }
395
396 void setup_apic_nmi_watchdog (void)
397 {
398         switch (boot_cpu_data.x86_vendor) {
399         case X86_VENDOR_AMD:
400                 if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15)
401                         return;
402                 setup_k7_watchdog();
403                 break;
404         case X86_VENDOR_INTEL:
405                 switch (boot_cpu_data.x86) {
406                 case 6:
407                         if (boot_cpu_data.x86_model > 0xd)
408                                 return;
409
410                         setup_p6_watchdog();
411                         break;
412                 case 15:
413                         if (boot_cpu_data.x86_model > 0x3)
414                                 return;
415
416                         if (!setup_p4_watchdog())
417                                 return;
418                         break;
419                 default:
420                         return;
421                 }
422                 break;
423         default:
424                 return;
425         }
426         lapic_nmi_owner = LAPIC_NMI_WATCHDOG;
427         nmi_active = 1;
428 }
429
430 static spinlock_t nmi_print_lock = SPIN_LOCK_UNLOCKED;
431
432 /*
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.
436  *
437  * as these watchdog NMI IRQs are generated on every CPU, we only
438  * have to check the current processor.
439  *
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
444  *  here too!]
445  */
446
447 static unsigned int
448         last_irq_sums [NR_CPUS],
449         alert_counter [NR_CPUS];
450
451 void touch_nmi_watchdog (void)
452 {
453         int i;
454
455         /*
456          * Just reset the alert counters, (other CPUs might be
457          * spinning on locks we hold):
458          */
459         for (i = 0; i < NR_CPUS; i++)
460                 alert_counter[i] = 0;
461 }
462
463 void nmi_watchdog_tick (struct pt_regs * regs)
464 {
465
466         /*
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().
470          */
471         int sum, cpu = smp_processor_id();
472
473         sum = irq_stat[cpu].apic_timer_irqs;
474
475         if (last_irq_sums[cpu] == sum) {
476                 /*
477                  * Ayiee, looks like this CPU is stuck ...
478                  * wait a few IRQs (5 seconds) before doing the oops ...
479                  */
480                 alert_counter[cpu]++;
481                 if (alert_counter[cpu] == 60*nmi_hz) {
482                         spin_lock(&nmi_print_lock);
483                         /*
484                          * We are in trouble anyway, lets at least try
485                          * to get a message out.
486                          */
487                         bust_spinlocks(1);
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");
492                         console_silent();
493                         spin_unlock(&nmi_print_lock);
494                         bust_spinlocks(0);
495                         do_exit(SIGSEGV);
496                 }
497         } else {
498                 last_irq_sums[cpu] = sum;
499                 alert_counter[cpu] = 0;
500         }
501         if (nmi_perfctr_msr) {
502                 if (nmi_perfctr_msr == MSR_P4_IQ_COUNTER0) {
503                         /*
504                          * P4 quirks:
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.
509                          */
510                         wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
511                         apic_write(APIC_LVTPC, APIC_DM_NMI);
512                 }
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);
518                 }
519                 wrmsr(nmi_perfctr_msr, -(cpu_khz/nmi_hz*1000), -1);
520         }
521 }
522
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);