72f40ca4f06b827215beaeba48e94d9898938636
[safe/jmp/linux-2.6] / arch / x86_64 / kernel / nmi.c
1 /*
2  *  linux/arch/x86_64/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  *  Pavel Machek and
12  *  Mikael Pettersson   : PM converted to driver model. Disable/enable API.
13  */
14
15 #include <linux/config.h>
16 #include <linux/mm.h>
17 #include <linux/irq.h>
18 #include <linux/delay.h>
19 #include <linux/bootmem.h>
20 #include <linux/smp_lock.h>
21 #include <linux/interrupt.h>
22 #include <linux/mc146818rtc.h>
23 #include <linux/kernel_stat.h>
24 #include <linux/module.h>
25 #include <linux/sysdev.h>
26 #include <linux/nmi.h>
27 #include <linux/sysctl.h>
28
29 #include <asm/smp.h>
30 #include <asm/mtrr.h>
31 #include <asm/mpspec.h>
32 #include <asm/nmi.h>
33 #include <asm/msr.h>
34 #include <asm/proto.h>
35 #include <asm/kdebug.h>
36
37 /*
38  * lapic_nmi_owner tracks the ownership of the lapic NMI hardware:
39  * - it may be reserved by some other driver, or not
40  * - when not reserved by some other driver, it may be used for
41  *   the NMI watchdog, or not
42  *
43  * This is maintained separately from nmi_active because the NMI
44  * watchdog may also be driven from the I/O APIC timer.
45  */
46 static DEFINE_SPINLOCK(lapic_nmi_owner_lock);
47 static unsigned int lapic_nmi_owner;
48 #define LAPIC_NMI_WATCHDOG      (1<<0)
49 #define LAPIC_NMI_RESERVED      (1<<1)
50
51 /* nmi_active:
52  * +1: the lapic NMI watchdog is active, but can be disabled
53  *  0: the lapic NMI watchdog has not been set up, and cannot
54  *     be enabled
55  * -1: the lapic NMI watchdog is disabled, but can be enabled
56  */
57 int nmi_active;         /* oprofile uses this */
58 int panic_on_timeout;
59
60 unsigned int nmi_watchdog = NMI_DEFAULT;
61 static unsigned int nmi_hz = HZ;
62 unsigned int nmi_perfctr_msr;   /* the MSR to reset in NMI handler */
63
64 /* Note that these events don't tick when the CPU idles. This means
65    the frequency varies with CPU load. */
66
67 #define K7_EVNTSEL_ENABLE       (1 << 22)
68 #define K7_EVNTSEL_INT          (1 << 20)
69 #define K7_EVNTSEL_OS           (1 << 17)
70 #define K7_EVNTSEL_USR          (1 << 16)
71 #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING    0x76
72 #define K7_NMI_EVENT            K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
73
74 #define P6_EVNTSEL0_ENABLE      (1 << 22)
75 #define P6_EVNTSEL_INT          (1 << 20)
76 #define P6_EVNTSEL_OS           (1 << 17)
77 #define P6_EVNTSEL_USR          (1 << 16)
78 #define P6_EVENT_CPU_CLOCKS_NOT_HALTED  0x79
79 #define P6_NMI_EVENT            P6_EVENT_CPU_CLOCKS_NOT_HALTED
80
81 /* Run after command line and cpu_init init, but before all other checks */
82 void __init nmi_watchdog_default(void)
83 {
84         if (nmi_watchdog != NMI_DEFAULT)
85                 return;
86
87         /* For some reason the IO APIC watchdog doesn't work on the AMD
88            8111 chipset. For now switch to local APIC mode using
89            perfctr0 there.  On Intel CPUs we don't have code to handle
90            the perfctr and the IO-APIC seems to work, so use that.  */
91
92         if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
93                 nmi_watchdog = NMI_LOCAL_APIC; 
94                 printk(KERN_INFO 
95               "Using local APIC NMI watchdog using perfctr0\n");
96         } else {
97                 printk(KERN_INFO "Using IO APIC NMI watchdog\n");
98                 nmi_watchdog = NMI_IO_APIC;
99         }
100 }
101
102 /* Why is there no CPUID flag for this? */
103 static __init int cpu_has_lapic(void)
104 {
105         switch (boot_cpu_data.x86_vendor) { 
106         case X86_VENDOR_INTEL:
107         case X86_VENDOR_AMD: 
108                 return boot_cpu_data.x86 >= 6; 
109         /* .... add more cpus here or find a different way to figure this out. */       
110         default:
111                 return 0;
112         }       
113 }
114
115 int __init check_nmi_watchdog (void)
116 {
117         int counts[NR_CPUS];
118         int cpu;
119
120         if (nmi_watchdog == NMI_LOCAL_APIC && !cpu_has_lapic())  {
121                 nmi_watchdog = NMI_NONE;
122                 return -1; 
123         }       
124
125         printk(KERN_INFO "testing NMI watchdog ... ");
126
127         for (cpu = 0; cpu < NR_CPUS; cpu++)
128                 counts[cpu] = cpu_pda[cpu].__nmi_count; 
129         local_irq_enable();
130         mdelay((10*1000)/nmi_hz); // wait 10 ticks
131
132         for (cpu = 0; cpu < NR_CPUS; cpu++) {
133                 if (cpu_pda[cpu].__nmi_count - counts[cpu] <= 5) {
134                         printk("CPU#%d: NMI appears to be stuck (%d)!\n", 
135                                cpu,
136                                cpu_pda[cpu].__nmi_count);
137                         nmi_active = 0;
138                         lapic_nmi_owner &= ~LAPIC_NMI_WATCHDOG;
139                         return -1;
140                 }
141         }
142         printk("OK.\n");
143
144         /* now that we know it works we can reduce NMI frequency to
145            something more reasonable; makes a difference in some configs */
146         if (nmi_watchdog == NMI_LOCAL_APIC)
147                 nmi_hz = 1;
148
149         return 0;
150 }
151
152 int __init setup_nmi_watchdog(char *str)
153 {
154         int nmi;
155
156         if (!strncmp(str,"panic",5)) {
157                 panic_on_timeout = 1;
158                 str = strchr(str, ',');
159                 if (!str)
160                         return 1;
161                 ++str;
162         }
163
164         get_option(&str, &nmi);
165
166         if (nmi >= NMI_INVALID)
167                 return 0;
168                 nmi_watchdog = nmi;
169         return 1;
170 }
171
172 __setup("nmi_watchdog=", setup_nmi_watchdog);
173
174 static void disable_lapic_nmi_watchdog(void)
175 {
176         if (nmi_active <= 0)
177                 return;
178         switch (boot_cpu_data.x86_vendor) {
179         case X86_VENDOR_AMD:
180                 wrmsr(MSR_K7_EVNTSEL0, 0, 0);
181                 break;
182         case X86_VENDOR_INTEL:
183                 wrmsr(MSR_IA32_EVNTSEL0, 0, 0);
184                 break;
185         }
186         nmi_active = -1;
187         /* tell do_nmi() and others that we're not active any more */
188         nmi_watchdog = 0;
189 }
190
191 static void enable_lapic_nmi_watchdog(void)
192 {
193         if (nmi_active < 0) {
194                 nmi_watchdog = NMI_LOCAL_APIC;
195                 setup_apic_nmi_watchdog();
196         }
197 }
198
199 int reserve_lapic_nmi(void)
200 {
201         unsigned int old_owner;
202
203         spin_lock(&lapic_nmi_owner_lock);
204         old_owner = lapic_nmi_owner;
205         lapic_nmi_owner |= LAPIC_NMI_RESERVED;
206         spin_unlock(&lapic_nmi_owner_lock);
207         if (old_owner & LAPIC_NMI_RESERVED)
208                 return -EBUSY;
209         if (old_owner & LAPIC_NMI_WATCHDOG)
210                 disable_lapic_nmi_watchdog();
211         return 0;
212 }
213
214 void release_lapic_nmi(void)
215 {
216         unsigned int new_owner;
217
218         spin_lock(&lapic_nmi_owner_lock);
219         new_owner = lapic_nmi_owner & ~LAPIC_NMI_RESERVED;
220         lapic_nmi_owner = new_owner;
221         spin_unlock(&lapic_nmi_owner_lock);
222         if (new_owner & LAPIC_NMI_WATCHDOG)
223                 enable_lapic_nmi_watchdog();
224 }
225
226 void disable_timer_nmi_watchdog(void)
227 {
228         if ((nmi_watchdog != NMI_IO_APIC) || (nmi_active <= 0))
229                 return;
230
231         disable_irq(0);
232         unset_nmi_callback();
233         nmi_active = -1;
234         nmi_watchdog = NMI_NONE;
235 }
236
237 void enable_timer_nmi_watchdog(void)
238 {
239         if (nmi_active < 0) {
240                 nmi_watchdog = NMI_IO_APIC;
241                 touch_nmi_watchdog();
242                 nmi_active = 1;
243                 enable_irq(0);
244         }
245 }
246
247 #ifdef CONFIG_PM
248
249 static int nmi_pm_active; /* nmi_active before suspend */
250
251 static int lapic_nmi_suspend(struct sys_device *dev, u32 state)
252 {
253         nmi_pm_active = nmi_active;
254         disable_lapic_nmi_watchdog();
255         return 0;
256 }
257
258 static int lapic_nmi_resume(struct sys_device *dev)
259 {
260         if (nmi_pm_active > 0)
261         enable_lapic_nmi_watchdog();
262         return 0;
263 }
264
265 static struct sysdev_class nmi_sysclass = {
266         set_kset_name("lapic_nmi"),
267         .resume         = lapic_nmi_resume,
268         .suspend        = lapic_nmi_suspend,
269 };
270
271 static struct sys_device device_lapic_nmi = {
272         .id             = 0,
273         .cls    = &nmi_sysclass,
274 };
275
276 static int __init init_lapic_nmi_sysfs(void)
277 {
278         int error;
279
280         if (nmi_active == 0 || nmi_watchdog != NMI_LOCAL_APIC)
281                 return 0;
282
283         error = sysdev_class_register(&nmi_sysclass);
284         if (!error)
285                 error = sysdev_register(&device_lapic_nmi);
286         return error;
287 }
288 /* must come after the local APIC's device_initcall() */
289 late_initcall(init_lapic_nmi_sysfs);
290
291 #endif  /* CONFIG_PM */
292
293 /*
294  * Activate the NMI watchdog via the local APIC.
295  * Original code written by Keith Owens.
296  */
297
298 static void setup_k7_watchdog(void)
299 {
300         int i;
301         unsigned int evntsel;
302
303         /* No check, so can start with slow frequency */
304         nmi_hz = 1; 
305
306         /* XXX should check these in EFER */
307
308         nmi_perfctr_msr = MSR_K7_PERFCTR0;
309
310         for(i = 0; i < 4; ++i) {
311                 /* Simulator may not support it */
312                 if (checking_wrmsrl(MSR_K7_EVNTSEL0+i, 0UL))
313                         return;
314                 wrmsrl(MSR_K7_PERFCTR0+i, 0UL);
315         }
316
317         evntsel = K7_EVNTSEL_INT
318                 | K7_EVNTSEL_OS
319                 | K7_EVNTSEL_USR
320                 | K7_NMI_EVENT;
321
322         wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
323         wrmsrl(MSR_K7_PERFCTR0, -((u64)cpu_khz*1000) / nmi_hz);
324         apic_write(APIC_LVTPC, APIC_DM_NMI);
325         evntsel |= K7_EVNTSEL_ENABLE;
326         wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
327 }
328
329 void setup_apic_nmi_watchdog(void)
330 {
331         switch (boot_cpu_data.x86_vendor) {
332         case X86_VENDOR_AMD:
333                 if (boot_cpu_data.x86 != 15)
334                         return;
335                 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
336                         return;
337                 setup_k7_watchdog();
338                 break;
339         default:
340                 return;
341         }
342         lapic_nmi_owner = LAPIC_NMI_WATCHDOG;
343         nmi_active = 1;
344 }
345
346 /*
347  * the best way to detect whether a CPU has a 'hard lockup' problem
348  * is to check it's local APIC timer IRQ counts. If they are not
349  * changing then that CPU has some problem.
350  *
351  * as these watchdog NMI IRQs are generated on every CPU, we only
352  * have to check the current processor.
353  *
354  * since NMIs don't listen to _any_ locks, we have to be extremely
355  * careful not to rely on unsafe variables. The printk might lock
356  * up though, so we have to break up any console locks first ...
357  * [when there will be more tty-related locks, break them up
358  *  here too!]
359  */
360
361 static unsigned int
362         last_irq_sums [NR_CPUS],
363         alert_counter [NR_CPUS];
364
365 void touch_nmi_watchdog (void)
366 {
367         int i;
368
369         /*
370          * Just reset the alert counters, (other CPUs might be
371          * spinning on locks we hold):
372          */
373         for (i = 0; i < NR_CPUS; i++)
374                 alert_counter[i] = 0;
375 }
376
377 void nmi_watchdog_tick (struct pt_regs * regs, unsigned reason)
378 {
379         int sum, cpu;
380
381         cpu = safe_smp_processor_id();
382         sum = read_pda(apic_timer_irqs);
383         if (last_irq_sums[cpu] == sum) {
384                 /*
385                  * Ayiee, looks like this CPU is stuck ...
386                  * wait a few IRQs (5 seconds) before doing the oops ...
387                  */
388                 alert_counter[cpu]++;
389                 if (alert_counter[cpu] == 5*nmi_hz) {
390                         if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
391                                                         == NOTIFY_STOP) {
392                                 alert_counter[cpu] = 0; 
393                                 return;
394                         } 
395                         die_nmi("NMI Watchdog detected LOCKUP on CPU%d", regs);
396                 }
397         } else {
398                 last_irq_sums[cpu] = sum;
399                 alert_counter[cpu] = 0;
400         }
401         if (nmi_perfctr_msr)
402                 wrmsr(nmi_perfctr_msr, -(cpu_khz/nmi_hz*1000), -1);
403 }
404
405 static int dummy_nmi_callback(struct pt_regs * regs, int cpu)
406 {
407         return 0;
408 }
409  
410 static nmi_callback_t nmi_callback = dummy_nmi_callback;
411  
412 asmlinkage void do_nmi(struct pt_regs * regs, long error_code)
413 {
414         int cpu = safe_smp_processor_id();
415
416         nmi_enter();
417         add_pda(__nmi_count,1);
418         if (!nmi_callback(regs, cpu))
419                 default_do_nmi(regs);
420         nmi_exit();
421 }
422
423 void set_nmi_callback(nmi_callback_t callback)
424 {
425         nmi_callback = callback;
426 }
427
428 void unset_nmi_callback(void)
429 {
430         nmi_callback = dummy_nmi_callback;
431 }
432
433 #ifdef CONFIG_SYSCTL
434
435 static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
436 {
437         unsigned char reason = get_nmi_reason();
438         char buf[64];
439
440         if (!(reason & 0xc0)) {
441                 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
442                 die_nmi(buf,regs);
443         }
444         return 0;
445 }
446
447 /*
448  * proc handler for /proc/sys/kernel/unknown_nmi_panic
449  */
450 int proc_unknown_nmi_panic(struct ctl_table *table, int write, struct file *file,
451                         void __user *buffer, size_t *length, loff_t *ppos)
452 {
453         int old_state;
454
455         old_state = unknown_nmi_panic;
456         proc_dointvec(table, write, file, buffer, length, ppos);
457         if (!!old_state == !!unknown_nmi_panic)
458                 return 0;
459
460         if (unknown_nmi_panic) {
461                 if (reserve_lapic_nmi() < 0) {
462                         unknown_nmi_panic = 0;
463                         return -EBUSY;
464                 } else {
465                         set_nmi_callback(unknown_nmi_panic_callback);
466                 }
467         } else {
468                 release_lapic_nmi();
469                 unset_nmi_callback();
470         }
471         return 0;
472 }
473
474 #endif
475
476 EXPORT_SYMBOL(nmi_active);
477 EXPORT_SYMBOL(nmi_watchdog);
478 EXPORT_SYMBOL(reserve_lapic_nmi);
479 EXPORT_SYMBOL(release_lapic_nmi);
480 EXPORT_SYMBOL(disable_timer_nmi_watchdog);
481 EXPORT_SYMBOL(enable_timer_nmi_watchdog);
482 EXPORT_SYMBOL(touch_nmi_watchdog);