commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / 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
28 #include <asm/smp.h>
29 #include <asm/mtrr.h>
30 #include <asm/mpspec.h>
31 #include <asm/nmi.h>
32 #include <asm/msr.h>
33 #include <asm/proto.h>
34 #include <asm/kdebug.h>
35
36 /* nmi_active:
37  * +1: the lapic NMI watchdog is active, but can be disabled
38  *  0: the lapic NMI watchdog has not been set up, and cannot
39  *     be enabled
40  * -1: the lapic NMI watchdog is disabled, but can be enabled
41  */
42 static int nmi_active;
43 static int panic_on_timeout;
44
45 unsigned int nmi_watchdog = NMI_IO_APIC;
46 static unsigned int nmi_hz = HZ;
47 unsigned int nmi_perfctr_msr;   /* the MSR to reset in NMI handler */
48 int nmi_watchdog_disabled;
49
50 #define K7_EVNTSEL_ENABLE       (1 << 22)
51 #define K7_EVNTSEL_INT          (1 << 20)
52 #define K7_EVNTSEL_OS           (1 << 17)
53 #define K7_EVNTSEL_USR          (1 << 16)
54 #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING    0x76
55 #define K7_NMI_EVENT            K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
56
57 #define P6_EVNTSEL0_ENABLE      (1 << 22)
58 #define P6_EVNTSEL_INT          (1 << 20)
59 #define P6_EVNTSEL_OS           (1 << 17)
60 #define P6_EVNTSEL_USR          (1 << 16)
61 #define P6_EVENT_CPU_CLOCKS_NOT_HALTED  0x79
62 #define P6_NMI_EVENT            P6_EVENT_CPU_CLOCKS_NOT_HALTED
63
64 /* Why is there no CPUID flag for this? */
65 static __init int cpu_has_lapic(void)
66 {
67         switch (boot_cpu_data.x86_vendor) { 
68         case X86_VENDOR_INTEL:
69         case X86_VENDOR_AMD: 
70                 return boot_cpu_data.x86 >= 6; 
71         /* .... add more cpus here or find a different way to figure this out. */       
72         default:
73                 return 0;
74         }       
75 }
76
77 int __init check_nmi_watchdog (void)
78 {
79         int counts[NR_CPUS];
80         int cpu;
81
82         if (nmi_watchdog == NMI_LOCAL_APIC && !cpu_has_lapic())  {
83                 nmi_watchdog = NMI_NONE;
84                 return -1; 
85         }       
86
87         printk(KERN_INFO "testing NMI watchdog ... ");
88
89         for (cpu = 0; cpu < NR_CPUS; cpu++)
90                 counts[cpu] = cpu_pda[cpu].__nmi_count; 
91         local_irq_enable();
92         mdelay((10*1000)/nmi_hz); // wait 10 ticks
93
94         for (cpu = 0; cpu < NR_CPUS; cpu++) {
95                 if (!cpu_online(cpu))
96                         continue;
97                 if (cpu_pda[cpu].__nmi_count - counts[cpu] <= 5) {
98                         printk("CPU#%d: NMI appears to be stuck (%d)!\n", 
99                                cpu,
100                                cpu_pda[cpu].__nmi_count);
101                         nmi_active = 0;
102                         return -1;
103                 }
104         }
105         printk("OK.\n");
106
107         /* now that we know it works we can reduce NMI frequency to
108            something more reasonable; makes a difference in some configs */
109         if (nmi_watchdog == NMI_LOCAL_APIC)
110                 nmi_hz = 1;
111
112         return 0;
113 }
114
115 static int __init setup_nmi_watchdog(char *str)
116 {
117         int nmi;
118
119         if (!strncmp(str,"panic",5)) {
120                 panic_on_timeout = 1;
121                 str = strchr(str, ',');
122                 if (!str)
123                         return 1;
124                 ++str;
125         }
126
127         get_option(&str, &nmi);
128
129         if (nmi >= NMI_INVALID)
130                 return 0;
131                 nmi_watchdog = nmi;
132         return 1;
133 }
134
135 __setup("nmi_watchdog=", setup_nmi_watchdog);
136
137 void disable_lapic_nmi_watchdog(void)
138 {
139         if (nmi_active <= 0)
140                 return;
141         switch (boot_cpu_data.x86_vendor) {
142         case X86_VENDOR_AMD:
143                 wrmsr(MSR_K7_EVNTSEL0, 0, 0);
144                 break;
145         case X86_VENDOR_INTEL:
146                 wrmsr(MSR_IA32_EVNTSEL0, 0, 0);
147                 break;
148         }
149         nmi_active = -1;
150         /* tell do_nmi() and others that we're not active any more */
151         nmi_watchdog = 0;
152 }
153
154 void enable_lapic_nmi_watchdog(void)
155 {
156         if (nmi_active < 0) {
157                 nmi_watchdog = NMI_LOCAL_APIC;
158                 setup_apic_nmi_watchdog();
159         }
160 }
161
162 void disable_timer_nmi_watchdog(void)
163 {
164         if ((nmi_watchdog != NMI_IO_APIC) || (nmi_active <= 0))
165                 return;
166
167         disable_irq(0);
168         unset_nmi_callback();
169         nmi_active = -1;
170         nmi_watchdog = NMI_NONE;
171 }
172
173 void enable_timer_nmi_watchdog(void)
174 {
175         if (nmi_active < 0) {
176                 nmi_watchdog = NMI_IO_APIC;
177                 touch_nmi_watchdog();
178                 nmi_active = 1;
179                 enable_irq(0);
180         }
181 }
182
183 #ifdef CONFIG_PM
184
185 static int nmi_pm_active; /* nmi_active before suspend */
186
187 static int lapic_nmi_suspend(struct sys_device *dev, u32 state)
188 {
189         nmi_pm_active = nmi_active;
190         disable_lapic_nmi_watchdog();
191         return 0;
192 }
193
194 static int lapic_nmi_resume(struct sys_device *dev)
195 {
196         if (nmi_pm_active > 0)
197         enable_lapic_nmi_watchdog();
198         return 0;
199 }
200
201 static struct sysdev_class nmi_sysclass = {
202         set_kset_name("lapic_nmi"),
203         .resume         = lapic_nmi_resume,
204         .suspend        = lapic_nmi_suspend,
205 };
206
207 static struct sys_device device_lapic_nmi = {
208         .id             = 0,
209         .cls    = &nmi_sysclass,
210 };
211
212 static int __init init_lapic_nmi_sysfs(void)
213 {
214         int error;
215
216         if (nmi_active == 0)
217                 return 0;
218
219         error = sysdev_class_register(&nmi_sysclass);
220         if (!error)
221                 error = sys_device_register(&device_lapic_nmi);
222         return error;
223 }
224 /* must come after the local APIC's device_initcall() */
225 late_initcall(init_lapic_nmi_sysfs);
226
227 #endif  /* CONFIG_PM */
228
229 /*
230  * Activate the NMI watchdog via the local APIC.
231  * Original code written by Keith Owens.
232  */
233
234 static void setup_k7_watchdog(void)
235 {
236         int i;
237         unsigned int evntsel;
238
239         /* XXX should check these in EFER */
240
241         nmi_perfctr_msr = MSR_K7_PERFCTR0;
242
243         for(i = 0; i < 4; ++i) {
244                 /* Simulator may not support it */
245                 if (checking_wrmsrl(MSR_K7_EVNTSEL0+i, 0UL))
246                         return;
247                 wrmsrl(MSR_K7_PERFCTR0+i, 0UL);
248         }
249
250         evntsel = K7_EVNTSEL_INT
251                 | K7_EVNTSEL_OS
252                 | K7_EVNTSEL_USR
253                 | K7_NMI_EVENT;
254
255         wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
256         printk(KERN_INFO "watchdog: setting K7_PERFCTR0 to %08x\n", -(cpu_khz/nmi_hz*1000));
257         wrmsr(MSR_K7_PERFCTR0, -(cpu_khz/nmi_hz*1000), -1);
258         apic_write(APIC_LVTPC, APIC_DM_NMI);
259         evntsel |= K7_EVNTSEL_ENABLE;
260         wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
261 }
262
263 void setup_apic_nmi_watchdog (void)
264 {
265         switch (boot_cpu_data.x86_vendor) {
266         case X86_VENDOR_AMD:
267                 if (boot_cpu_data.x86 < 6)
268                         return;
269                 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
270                         return;
271                 setup_k7_watchdog();
272                 break;
273         default:
274                 return;
275         }
276         nmi_active = 1;
277 }
278
279 static spinlock_t nmi_print_lock = SPIN_LOCK_UNLOCKED;
280
281 /*
282  * the best way to detect whether a CPU has a 'hard lockup' problem
283  * is to check it's local APIC timer IRQ counts. If they are not
284  * changing then that CPU has some problem.
285  *
286  * as these watchdog NMI IRQs are generated on every CPU, we only
287  * have to check the current processor.
288  *
289  * since NMIs don't listen to _any_ locks, we have to be extremely
290  * careful not to rely on unsafe variables. The printk might lock
291  * up though, so we have to break up any console locks first ...
292  * [when there will be more tty-related locks, break them up
293  *  here too!]
294  */
295
296 static unsigned int
297         last_irq_sums [NR_CPUS],
298         alert_counter [NR_CPUS];
299
300 void touch_nmi_watchdog (void)
301 {
302         int i;
303
304         /*
305          * Just reset the alert counters, (other CPUs might be
306          * spinning on locks we hold):
307          */
308         for (i = 0; i < NR_CPUS; i++)
309                 alert_counter[i] = 0;
310 }
311
312 void nmi_watchdog_tick (struct pt_regs * regs, unsigned reason)
313 {
314         if (nmi_watchdog_disabled)
315                 return;
316
317         int sum, cpu = safe_smp_processor_id();
318
319         sum = read_pda(apic_timer_irqs);
320         if (last_irq_sums[cpu] == sum) {
321                 /*
322                  * Ayiee, looks like this CPU is stuck ...
323                  * wait a few IRQs (5 seconds) before doing the oops ...
324                  */
325                 alert_counter[cpu]++;
326                 if (alert_counter[cpu] == 5*nmi_hz) {
327                         if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_BAD) { 
328                                 alert_counter[cpu] = 0; 
329                                 return;
330                         } 
331                         spin_lock(&nmi_print_lock);
332                         /*
333                          * We are in trouble anyway, lets at least try
334                          * to get a message out.
335                          */
336                         bust_spinlocks(1);
337                         printk("NMI Watchdog detected LOCKUP on CPU%d, registers:\n", cpu);
338                         show_registers(regs);
339                         if (panic_on_timeout)
340                                 panic("nmi watchdog");
341                         printk("console shuts up ...\n");
342                         console_silent();
343                         spin_unlock(&nmi_print_lock);
344                         bust_spinlocks(0);
345                         do_exit(SIGSEGV);
346                 }
347         } else {
348                 last_irq_sums[cpu] = sum;
349                 alert_counter[cpu] = 0;
350         }
351         if (nmi_perfctr_msr)
352                 wrmsr(nmi_perfctr_msr, -(cpu_khz/nmi_hz*1000), -1);
353 }
354
355 static int dummy_nmi_callback(struct pt_regs * regs, int cpu)
356 {
357         return 0;
358 }
359  
360 static nmi_callback_t nmi_callback = dummy_nmi_callback;
361  
362 asmlinkage void do_nmi(struct pt_regs * regs, long error_code)
363 {
364         int cpu = safe_smp_processor_id();
365
366         nmi_enter();
367         add_pda(__nmi_count,1);
368         if (!nmi_callback(regs, cpu))
369                 default_do_nmi(regs);
370         nmi_exit();
371 }
372
373 void set_nmi_callback(nmi_callback_t callback)
374 {
375         nmi_callback = callback;
376 }
377
378 void unset_nmi_callback(void)
379 {
380         nmi_callback = dummy_nmi_callback;
381 }
382
383 EXPORT_SYMBOL(nmi_watchdog);
384 EXPORT_SYMBOL(disable_lapic_nmi_watchdog);
385 EXPORT_SYMBOL(enable_lapic_nmi_watchdog);
386 EXPORT_SYMBOL(disable_timer_nmi_watchdog);
387 EXPORT_SYMBOL(enable_timer_nmi_watchdog);
388 EXPORT_SYMBOL(touch_nmi_watchdog);