2 * linux/arch/x86_64/nmi.c
4 * NMI watchdog support on APIC systems
6 * Started by Ingo Molnar <mingo@redhat.com>
9 * Mikael Pettersson : AMD K7 support for local APIC NMI watchdog.
10 * Mikael Pettersson : Power Management for local APIC NMI watchdog.
12 * Mikael Pettersson : PM converted to driver model. Disable/enable API.
15 #include <linux/config.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>
30 #include <asm/mpspec.h>
33 #include <asm/proto.h>
34 #include <asm/kdebug.h>
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
40 * -1: the lapic NMI watchdog is disabled, but can be enabled
42 static int nmi_active;
43 static int panic_on_timeout;
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;
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
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
64 /* Why is there no CPUID flag for this? */
65 static __init int cpu_has_lapic(void)
67 switch (boot_cpu_data.x86_vendor) {
68 case X86_VENDOR_INTEL:
70 return boot_cpu_data.x86 >= 6;
71 /* .... add more cpus here or find a different way to figure this out. */
77 int __init check_nmi_watchdog (void)
82 if (nmi_watchdog == NMI_LOCAL_APIC && !cpu_has_lapic()) {
83 nmi_watchdog = NMI_NONE;
87 printk(KERN_INFO "testing NMI watchdog ... ");
89 for (cpu = 0; cpu < NR_CPUS; cpu++)
90 counts[cpu] = cpu_pda[cpu].__nmi_count;
92 mdelay((10*1000)/nmi_hz); // wait 10 ticks
94 for (cpu = 0; cpu < NR_CPUS; cpu++) {
97 if (cpu_pda[cpu].__nmi_count - counts[cpu] <= 5) {
98 printk("CPU#%d: NMI appears to be stuck (%d)!\n",
100 cpu_pda[cpu].__nmi_count);
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)
115 static int __init setup_nmi_watchdog(char *str)
119 if (!strncmp(str,"panic",5)) {
120 panic_on_timeout = 1;
121 str = strchr(str, ',');
127 get_option(&str, &nmi);
129 if (nmi >= NMI_INVALID)
135 __setup("nmi_watchdog=", setup_nmi_watchdog);
137 void disable_lapic_nmi_watchdog(void)
141 switch (boot_cpu_data.x86_vendor) {
143 wrmsr(MSR_K7_EVNTSEL0, 0, 0);
145 case X86_VENDOR_INTEL:
146 wrmsr(MSR_IA32_EVNTSEL0, 0, 0);
150 /* tell do_nmi() and others that we're not active any more */
154 void enable_lapic_nmi_watchdog(void)
156 if (nmi_active < 0) {
157 nmi_watchdog = NMI_LOCAL_APIC;
158 setup_apic_nmi_watchdog();
162 void disable_timer_nmi_watchdog(void)
164 if ((nmi_watchdog != NMI_IO_APIC) || (nmi_active <= 0))
168 unset_nmi_callback();
170 nmi_watchdog = NMI_NONE;
173 void enable_timer_nmi_watchdog(void)
175 if (nmi_active < 0) {
176 nmi_watchdog = NMI_IO_APIC;
177 touch_nmi_watchdog();
185 static int nmi_pm_active; /* nmi_active before suspend */
187 static int lapic_nmi_suspend(struct sys_device *dev, u32 state)
189 nmi_pm_active = nmi_active;
190 disable_lapic_nmi_watchdog();
194 static int lapic_nmi_resume(struct sys_device *dev)
196 if (nmi_pm_active > 0)
197 enable_lapic_nmi_watchdog();
201 static struct sysdev_class nmi_sysclass = {
202 set_kset_name("lapic_nmi"),
203 .resume = lapic_nmi_resume,
204 .suspend = lapic_nmi_suspend,
207 static struct sys_device device_lapic_nmi = {
209 .cls = &nmi_sysclass,
212 static int __init init_lapic_nmi_sysfs(void)
219 error = sysdev_class_register(&nmi_sysclass);
221 error = sys_device_register(&device_lapic_nmi);
224 /* must come after the local APIC's device_initcall() */
225 late_initcall(init_lapic_nmi_sysfs);
227 #endif /* CONFIG_PM */
230 * Activate the NMI watchdog via the local APIC.
231 * Original code written by Keith Owens.
234 static void setup_k7_watchdog(void)
237 unsigned int evntsel;
239 /* XXX should check these in EFER */
241 nmi_perfctr_msr = MSR_K7_PERFCTR0;
243 for(i = 0; i < 4; ++i) {
244 /* Simulator may not support it */
245 if (checking_wrmsrl(MSR_K7_EVNTSEL0+i, 0UL))
247 wrmsrl(MSR_K7_PERFCTR0+i, 0UL);
250 evntsel = K7_EVNTSEL_INT
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);
263 void setup_apic_nmi_watchdog (void)
265 switch (boot_cpu_data.x86_vendor) {
267 if (boot_cpu_data.x86 < 6)
269 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
279 static spinlock_t nmi_print_lock = SPIN_LOCK_UNLOCKED;
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.
286 * as these watchdog NMI IRQs are generated on every CPU, we only
287 * have to check the current processor.
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
297 last_irq_sums [NR_CPUS],
298 alert_counter [NR_CPUS];
300 void touch_nmi_watchdog (void)
305 * Just reset the alert counters, (other CPUs might be
306 * spinning on locks we hold):
308 for (i = 0; i < NR_CPUS; i++)
309 alert_counter[i] = 0;
312 void nmi_watchdog_tick (struct pt_regs * regs, unsigned reason)
314 if (nmi_watchdog_disabled)
317 int sum, cpu = safe_smp_processor_id();
319 sum = read_pda(apic_timer_irqs);
320 if (last_irq_sums[cpu] == sum) {
322 * Ayiee, looks like this CPU is stuck ...
323 * wait a few IRQs (5 seconds) before doing the oops ...
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;
331 spin_lock(&nmi_print_lock);
333 * We are in trouble anyway, lets at least try
334 * to get a message out.
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");
343 spin_unlock(&nmi_print_lock);
348 last_irq_sums[cpu] = sum;
349 alert_counter[cpu] = 0;
352 wrmsr(nmi_perfctr_msr, -(cpu_khz/nmi_hz*1000), -1);
355 static int dummy_nmi_callback(struct pt_regs * regs, int cpu)
360 static nmi_callback_t nmi_callback = dummy_nmi_callback;
362 asmlinkage void do_nmi(struct pt_regs * regs, long error_code)
364 int cpu = safe_smp_processor_id();
367 add_pda(__nmi_count,1);
368 if (!nmi_callback(regs, cpu))
369 default_do_nmi(regs);
373 void set_nmi_callback(nmi_callback_t callback)
375 nmi_callback = callback;
378 void unset_nmi_callback(void)
380 nmi_callback = dummy_nmi_callback;
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);