blob: 9f1e8c1afab75e26daa0ad1ebe6fec79a6a26aec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/nmi.h>
20#include <linux/sysdev.h>
21#include <linux/sysctl.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020022#include <linux/percpu.h>
Andi Kleen1de84972006-09-26 10:52:27 +020023#include <linux/dmi.h>
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +020024#include <linux/kprobes.h>
Andrew Mortonbb81a092006-12-07 02:14:01 +010025#include <linux/cpumask.h>
Thomas Gleixnerf8b50352007-02-16 01:28:09 -080026#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/nmi.h>
Don Zickusb7471c62006-09-26 10:52:26 +020030#include <asm/kdebug.h>
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +020031#include <asm/intel_arch_perfmon.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include "mach_traps.h"
34
Andi Kleen29cbc782006-09-30 01:47:55 +020035int unknown_nmi_panic;
36int nmi_watchdog_enabled;
37
Don Zickus828f0af2006-09-26 10:52:26 +020038/* perfctr_nmi_owner tracks the ownership of the perfctr registers:
39 * evtsel_nmi_owner tracks the ownership of the event selection
40 * - different performance counters/ event selection may be reserved for
41 * different subsystems this reservation system just tries to coordinate
42 * things a little
43 */
Andrew Mortonbb81a092006-12-07 02:14:01 +010044
Don Zickus828f0af2006-09-26 10:52:26 +020045/* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's
46 * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now)
47 */
48#define NMI_MAX_COUNTER_BITS 66
Andi Kleen1714f9b2007-04-16 10:30:27 +020049#define NMI_MAX_COUNTER_LONGS BITS_TO_LONGS(NMI_MAX_COUNTER_BITS)
Don Zickus828f0af2006-09-26 10:52:26 +020050
Andi Kleen1714f9b2007-04-16 10:30:27 +020051static DEFINE_PER_CPU(unsigned long, perfctr_nmi_owner[NMI_MAX_COUNTER_LONGS]);
52static DEFINE_PER_CPU(unsigned long, evntsel_nmi_owner[NMI_MAX_COUNTER_LONGS]);
53
54static cpumask_t backtrace_mask = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/* nmi_active:
Don Zickusb7471c62006-09-26 10:52:26 +020056 * >0: the lapic NMI watchdog is active, but can be disabled
57 * <0: the lapic NMI watchdog has not been set up, and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * be enabled
Don Zickusb7471c62006-09-26 10:52:26 +020059 * 0: the lapic NMI watchdog is disabled, but can be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
Don Zickusb7471c62006-09-26 10:52:26 +020061atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Don Zickusb7471c62006-09-26 10:52:26 +020063unsigned int nmi_watchdog = NMI_DEFAULT;
64static unsigned int nmi_hz = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Don Zickusb7471c62006-09-26 10:52:26 +020066struct nmi_watchdog_ctlblk {
67 int enabled;
68 u64 check_bit;
69 unsigned int cccr_msr;
70 unsigned int perfctr_msr; /* the MSR to reset in NMI handler */
71 unsigned int evntsel_msr; /* the MSR to select the events to handle */
72};
73static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Don Zickusb7471c62006-09-26 10:52:26 +020075/* local prototypes */
Don Zickusb7471c62006-09-26 10:52:26 +020076static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
77
78extern void show_registers(struct pt_regs *regs);
79extern int unknown_nmi_panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Don Zickus828f0af2006-09-26 10:52:26 +020081/* converts an msr to an appropriate reservation bit */
82static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
83{
84 /* returns the bit offset of the performance counter register */
85 switch (boot_cpu_data.x86_vendor) {
86 case X86_VENDOR_AMD:
87 return (msr - MSR_K7_PERFCTR0);
88 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +020089 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
90 return (msr - MSR_ARCH_PERFMON_PERFCTR0);
91
Don Zickus828f0af2006-09-26 10:52:26 +020092 switch (boot_cpu_data.x86) {
93 case 6:
94 return (msr - MSR_P6_PERFCTR0);
95 case 15:
96 return (msr - MSR_P4_BPU_PERFCTR0);
97 }
98 }
99 return 0;
100}
101
102/* converts an msr to an appropriate reservation bit */
103static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr)
104{
105 /* returns the bit offset of the event selection register */
106 switch (boot_cpu_data.x86_vendor) {
107 case X86_VENDOR_AMD:
108 return (msr - MSR_K7_EVNTSEL0);
109 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200110 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
111 return (msr - MSR_ARCH_PERFMON_EVENTSEL0);
112
Don Zickus828f0af2006-09-26 10:52:26 +0200113 switch (boot_cpu_data.x86) {
114 case 6:
115 return (msr - MSR_P6_EVNTSEL0);
116 case 15:
117 return (msr - MSR_P4_BSU_ESCR0);
118 }
119 }
120 return 0;
121}
122
123/* checks for a bit availability (hack for oprofile) */
124int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
125{
Andi Kleen89e07562007-04-02 12:14:12 +0200126 int cpu;
Don Zickus828f0af2006-09-26 10:52:26 +0200127 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
Andi Kleen89e07562007-04-02 12:14:12 +0200128 for_each_possible_cpu (cpu) {
129 if (test_bit(counter, &per_cpu(perfctr_nmi_owner, cpu)))
130 return 0;
131 }
132 return 1;
Don Zickus828f0af2006-09-26 10:52:26 +0200133}
134
135/* checks the an msr for availability */
136int avail_to_resrv_perfctr_nmi(unsigned int msr)
137{
138 unsigned int counter;
Andi Kleen89e07562007-04-02 12:14:12 +0200139 int cpu;
Don Zickus828f0af2006-09-26 10:52:26 +0200140
141 counter = nmi_perfctr_msr_to_bit(msr);
142 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
143
Andi Kleen89e07562007-04-02 12:14:12 +0200144 for_each_possible_cpu (cpu) {
145 if (test_bit(counter, &per_cpu(perfctr_nmi_owner, cpu)))
146 return 0;
147 }
148 return 1;
149}
150
151static int __reserve_perfctr_nmi(int cpu, unsigned int msr)
152{
153 unsigned int counter;
154 if (cpu < 0)
155 cpu = smp_processor_id();
156
157 counter = nmi_perfctr_msr_to_bit(msr);
158 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
159
160 if (!test_and_set_bit(counter, &per_cpu(perfctr_nmi_owner, cpu)))
161 return 1;
162 return 0;
163}
164
165static void __release_perfctr_nmi(int cpu, unsigned int msr)
166{
167 unsigned int counter;
168 if (cpu < 0)
169 cpu = smp_processor_id();
170
171 counter = nmi_perfctr_msr_to_bit(msr);
172 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
173
174 clear_bit(counter, &per_cpu(perfctr_nmi_owner, cpu));
Don Zickus828f0af2006-09-26 10:52:26 +0200175}
176
177int reserve_perfctr_nmi(unsigned int msr)
178{
Andi Kleen89e07562007-04-02 12:14:12 +0200179 int cpu, i;
180 for_each_possible_cpu (cpu) {
181 if (!__reserve_perfctr_nmi(cpu, msr)) {
182 for_each_possible_cpu (i) {
183 if (i >= cpu)
184 break;
185 __release_perfctr_nmi(i, msr);
186 }
187 return 0;
188 }
189 }
190 return 1;
Don Zickus828f0af2006-09-26 10:52:26 +0200191}
192
193void release_perfctr_nmi(unsigned int msr)
194{
Andi Kleen89e07562007-04-02 12:14:12 +0200195 int cpu;
196 for_each_possible_cpu (cpu) {
197 __release_perfctr_nmi(cpu, msr);
198 }
Don Zickus828f0af2006-09-26 10:52:26 +0200199}
200
Andi Kleen89e07562007-04-02 12:14:12 +0200201int __reserve_evntsel_nmi(int cpu, unsigned int msr)
Don Zickus828f0af2006-09-26 10:52:26 +0200202{
203 unsigned int counter;
Andi Kleen89e07562007-04-02 12:14:12 +0200204 if (cpu < 0)
205 cpu = smp_processor_id();
Don Zickus828f0af2006-09-26 10:52:26 +0200206
207 counter = nmi_evntsel_msr_to_bit(msr);
208 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
209
Andi Kleen89e07562007-04-02 12:14:12 +0200210 if (!test_and_set_bit(counter, &per_cpu(evntsel_nmi_owner, cpu)[0]))
Don Zickus828f0af2006-09-26 10:52:26 +0200211 return 1;
212 return 0;
213}
214
Andi Kleen89e07562007-04-02 12:14:12 +0200215static void __release_evntsel_nmi(int cpu, unsigned int msr)
Don Zickus828f0af2006-09-26 10:52:26 +0200216{
217 unsigned int counter;
Andi Kleen89e07562007-04-02 12:14:12 +0200218 if (cpu < 0)
219 cpu = smp_processor_id();
Don Zickus828f0af2006-09-26 10:52:26 +0200220
221 counter = nmi_evntsel_msr_to_bit(msr);
222 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
223
Andi Kleen89e07562007-04-02 12:14:12 +0200224 clear_bit(counter, &per_cpu(evntsel_nmi_owner, cpu)[0]);
225}
226
227int reserve_evntsel_nmi(unsigned int msr)
228{
229 int cpu, i;
230 for_each_possible_cpu (cpu) {
231 if (!__reserve_evntsel_nmi(cpu, msr)) {
232 for_each_possible_cpu (i) {
233 if (i >= cpu)
234 break;
235 __release_evntsel_nmi(i, msr);
236 }
237 return 0;
238 }
239 }
240 return 1;
241}
242
243void release_evntsel_nmi(unsigned int msr)
244{
245 int cpu;
246 for_each_possible_cpu (cpu) {
247 __release_evntsel_nmi(cpu, msr);
248 }
Don Zickus828f0af2006-09-26 10:52:26 +0200249}
250
Don Zickusb7471c62006-09-26 10:52:26 +0200251static __cpuinit inline int nmi_known_cpu(void)
252{
253 switch (boot_cpu_data.x86_vendor) {
254 case X86_VENDOR_AMD:
Andi Kleen0a4599c2007-02-13 13:26:25 +0100255 return ((boot_cpu_data.x86 == 15) || (boot_cpu_data.x86 == 6)
256 || (boot_cpu_data.x86 == 16));
Don Zickusb7471c62006-09-26 10:52:26 +0200257 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200258 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
259 return 1;
260 else
261 return ((boot_cpu_data.x86 == 15) || (boot_cpu_data.x86 == 6));
Don Zickusb7471c62006-09-26 10:52:26 +0200262 }
263 return 0;
264}
265
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +0100266static int endflag __initdata = 0;
267
Eric W. Biederman29b70082005-10-30 14:59:40 -0800268#ifdef CONFIG_SMP
269/* The performance counters used by NMI_LOCAL_APIC don't trigger when
270 * the CPU is idle. To make sure the NMI watchdog really ticks on all
271 * CPUs during the test make them busy.
272 */
273static __init void nmi_cpu_busy(void *data)
274{
Ingo Molnar366c7f52006-07-03 00:25:25 -0700275 local_irq_enable_in_hardirq();
Eric W. Biederman29b70082005-10-30 14:59:40 -0800276 /* Intentionally don't use cpu_relax here. This is
277 to make sure that the performance counter really ticks,
278 even if there is a simulator or similar that catches the
279 pause instruction. On a real HT machine this is fine because
280 all other CPUs are busy with "useless" delay loops and don't
281 care if they get somewhat less cycles. */
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +0100282 while (endflag == 0)
283 mb();
Eric W. Biederman29b70082005-10-30 14:59:40 -0800284}
285#endif
286
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100287static unsigned int adjust_for_32bit_ctr(unsigned int hz)
288{
289 u64 counter_val;
290 unsigned int retval = hz;
291
292 /*
293 * On Intel CPUs with P6/ARCH_PERFMON only 32 bits in the counter
294 * are writable, with higher bits sign extending from bit 31.
295 * So, we can only program the counter with 31 bit values and
296 * 32nd bit should be 1, for 33.. to be 1.
297 * Find the appropriate nmi_hz
298 */
299 counter_val = (u64)cpu_khz * 1000;
300 do_div(counter_val, retval);
301 if (counter_val > 0x7fffffffULL) {
302 u64 count = (u64)cpu_khz * 1000;
303 do_div(count, 0x7fffffffUL);
304 retval = count + 1;
305 }
306 return retval;
307}
308
Jack F Vogel67701ae2005-05-01 08:58:48 -0700309static int __init check_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Eric W. Biederman29b70082005-10-30 14:59:40 -0800311 unsigned int *prev_nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 int cpu;
313
Don Zickusb7471c62006-09-26 10:52:26 +0200314 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT))
315 return 0;
316
317 if (!atomic_read(&nmi_active))
Jack F Vogel67701ae2005-05-01 08:58:48 -0700318 return 0;
319
Eric W. Biederman29b70082005-10-30 14:59:40 -0800320 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
321 if (!prev_nmi_count)
322 return -1;
323
Jack F Vogel67701ae2005-05-01 08:58:48 -0700324 printk(KERN_INFO "Testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Eric W. Biederman29b70082005-10-30 14:59:40 -0800326 if (nmi_watchdog == NMI_LOCAL_APIC)
327 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
328
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800329 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 prev_nmi_count[cpu] = per_cpu(irq_stat, cpu).__nmi_count;
331 local_irq_enable();
Andi Kleen0fb2ebf2007-04-02 12:14:12 +0200332 mdelay((20*1000)/nmi_hz); // wait 20 ticks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800334 for_each_possible_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#ifdef CONFIG_SMP
336 /* Check cpu_callin_map here because that is set
337 after the timer is started. */
338 if (!cpu_isset(cpu, cpu_callin_map))
339 continue;
340#endif
Don Zickusb7471c62006-09-26 10:52:26 +0200341 if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled)
342 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
Eric W. Biederman29b70082005-10-30 14:59:40 -0800344 printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
345 cpu,
346 prev_nmi_count[cpu],
347 nmi_count(cpu));
Don Zickusb7471c62006-09-26 10:52:26 +0200348 per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0;
349 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351 }
Don Zickusb7471c62006-09-26 10:52:26 +0200352 if (!atomic_read(&nmi_active)) {
353 kfree(prev_nmi_count);
354 atomic_set(&nmi_active, -1);
355 return -1;
356 }
Eric W. Biederman29b70082005-10-30 14:59:40 -0800357 endflag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 printk("OK.\n");
359
360 /* now that we know it works we can reduce NMI frequency to
361 something more reasonable; makes a difference in some configs */
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200362 if (nmi_watchdog == NMI_LOCAL_APIC) {
363 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 nmi_hz = 1;
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100366
367 if (wd->perfctr_msr == MSR_P6_PERFCTR0 ||
368 wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0) {
369 nmi_hz = adjust_for_32bit_ctr(nmi_hz);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200370 }
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Eric W. Biederman29b70082005-10-30 14:59:40 -0800373 kfree(prev_nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375}
Jack F Vogel67701ae2005-05-01 08:58:48 -0700376/* This needs to happen later in boot so counters are working */
377late_initcall(check_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379static int __init setup_nmi_watchdog(char *str)
380{
381 int nmi;
382
383 get_option(&str, &nmi);
384
Don Zickusb7471c62006-09-26 10:52:26 +0200385 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 0;
Venkatesh Pallipadi58d9ce7d2007-01-22 20:40:34 -0800387
Don Zickusb7471c62006-09-26 10:52:26 +0200388 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 1;
390}
391
392__setup("nmi_watchdog=", setup_nmi_watchdog);
393
394static void disable_lapic_nmi_watchdog(void)
395{
Don Zickusb7471c62006-09-26 10:52:26 +0200396 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
397
398 if (atomic_read(&nmi_active) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Don Zickusb7471c62006-09-26 10:52:26 +0200401 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Don Zickusb7471c62006-09-26 10:52:26 +0200403 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406static void enable_lapic_nmi_watchdog(void)
407{
Don Zickusb7471c62006-09-26 10:52:26 +0200408 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
409
410 /* are we already enabled */
411 if (atomic_read(&nmi_active) != 0)
412 return;
413
414 /* are we lapic aware */
415 if (nmi_known_cpu() <= 0)
416 return;
417
418 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
419 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422void disable_timer_nmi_watchdog(void)
423{
Don Zickusb7471c62006-09-26 10:52:26 +0200424 BUG_ON(nmi_watchdog != NMI_IO_APIC);
425
426 if (atomic_read(&nmi_active) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return;
428
Don Zickusb7471c62006-09-26 10:52:26 +0200429 disable_irq(0);
430 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
431
432 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435void enable_timer_nmi_watchdog(void)
436{
Don Zickusb7471c62006-09-26 10:52:26 +0200437 BUG_ON(nmi_watchdog != NMI_IO_APIC);
438
439 if (atomic_read(&nmi_active) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 touch_nmi_watchdog();
Don Zickusb7471c62006-09-26 10:52:26 +0200441 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
442 enable_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444}
445
Ingo Molnar5d0e6002007-02-13 13:26:24 +0100446static void __acpi_nmi_disable(void *__unused)
447{
448 apic_write_around(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
449}
450
451/*
452 * Disable timer based NMIs on all CPUs:
453 */
454void acpi_nmi_disable(void)
455{
456 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
457 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
458}
459
460static void __acpi_nmi_enable(void *__unused)
461{
462 apic_write_around(APIC_LVT0, APIC_DM_NMI);
463}
464
465/*
466 * Enable timer based NMIs on all CPUs:
467 */
468void acpi_nmi_enable(void)
469{
470 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
471 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474#ifdef CONFIG_PM
475
476static int nmi_pm_active; /* nmi_active before suspend */
477
Pavel Machek438510f2005-04-16 15:25:24 -0700478static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Shaohua Li4038f902006-09-26 10:52:27 +0200480 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusb7471c62006-09-26 10:52:26 +0200481 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200482 stop_apic_nmi_watchdog(NULL);
483 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 0;
485}
486
487static int lapic_nmi_resume(struct sys_device *dev)
488{
Shaohua Li4038f902006-09-26 10:52:27 +0200489 /* only CPU0 goes here, other CPUs should be offline */
490 if (nmi_pm_active > 0) {
491 setup_apic_nmi_watchdog(NULL);
492 touch_nmi_watchdog();
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0;
495}
496
497
498static struct sysdev_class nmi_sysclass = {
499 set_kset_name("lapic_nmi"),
500 .resume = lapic_nmi_resume,
501 .suspend = lapic_nmi_suspend,
502};
503
504static struct sys_device device_lapic_nmi = {
505 .id = 0,
506 .cls = &nmi_sysclass,
507};
508
509static int __init init_lapic_nmi_sysfs(void)
510{
511 int error;
512
Don Zickusb7471c62006-09-26 10:52:26 +0200513 /* should really be a BUG_ON but b/c this is an
514 * init call, it just doesn't work. -dcz
515 */
516 if (nmi_watchdog != NMI_LOCAL_APIC)
517 return 0;
518
519 if ( atomic_read(&nmi_active) < 0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return 0;
521
522 error = sysdev_class_register(&nmi_sysclass);
523 if (!error)
524 error = sysdev_register(&device_lapic_nmi);
525 return error;
526}
527/* must come after the local APIC's device_initcall() */
528late_initcall(init_lapic_nmi_sysfs);
529
530#endif /* CONFIG_PM */
531
532/*
533 * Activate the NMI watchdog via the local APIC.
534 * Original code written by Keith Owens.
535 */
536
Don Zickusb7471c62006-09-26 10:52:26 +0200537static void write_watchdog_counter(unsigned int perfctr_msr, const char *descr)
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700538{
539 u64 count = (u64)cpu_khz * 1000;
540
541 do_div(count, nmi_hz);
542 if(descr)
543 Dprintk("setting %s to -0x%08Lx\n", descr, count);
Don Zickusb7471c62006-09-26 10:52:26 +0200544 wrmsrl(perfctr_msr, 0 - count);
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700545}
546
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100547static void write_watchdog_counter32(unsigned int perfctr_msr,
548 const char *descr)
549{
550 u64 count = (u64)cpu_khz * 1000;
551
552 do_div(count, nmi_hz);
553 if(descr)
554 Dprintk("setting %s to -0x%08Lx\n", descr, count);
555 wrmsr(perfctr_msr, (u32)(-count), 0);
556}
557
Don Zickusb7471c62006-09-26 10:52:26 +0200558/* Note that these events don't tick when the CPU idles. This means
559 the frequency varies with CPU load. */
560
561#define K7_EVNTSEL_ENABLE (1 << 22)
562#define K7_EVNTSEL_INT (1 << 20)
563#define K7_EVNTSEL_OS (1 << 17)
564#define K7_EVNTSEL_USR (1 << 16)
565#define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
566#define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
567
Don Zickus828f0af2006-09-26 10:52:26 +0200568static int setup_k7_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Don Zickusb7471c62006-09-26 10:52:26 +0200570 unsigned int perfctr_msr, evntsel_msr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 unsigned int evntsel;
Don Zickusb7471c62006-09-26 10:52:26 +0200572 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Don Zickusb7471c62006-09-26 10:52:26 +0200574 perfctr_msr = MSR_K7_PERFCTR0;
575 evntsel_msr = MSR_K7_EVNTSEL0;
Andi Kleen89e07562007-04-02 12:14:12 +0200576 if (!__reserve_perfctr_nmi(-1, perfctr_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200577 goto fail;
578
Andi Kleen89e07562007-04-02 12:14:12 +0200579 if (!__reserve_evntsel_nmi(-1, evntsel_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200580 goto fail1;
581
Don Zickusb7471c62006-09-26 10:52:26 +0200582 wrmsrl(perfctr_msr, 0UL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 evntsel = K7_EVNTSEL_INT
585 | K7_EVNTSEL_OS
586 | K7_EVNTSEL_USR
587 | K7_NMI_EVENT;
588
Don Zickusb7471c62006-09-26 10:52:26 +0200589 /* setup the timer */
590 wrmsr(evntsel_msr, evntsel, 0);
591 write_watchdog_counter(perfctr_msr, "K7_PERFCTR0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 apic_write(APIC_LVTPC, APIC_DM_NMI);
593 evntsel |= K7_EVNTSEL_ENABLE;
Don Zickusb7471c62006-09-26 10:52:26 +0200594 wrmsr(evntsel_msr, evntsel, 0);
595
596 wd->perfctr_msr = perfctr_msr;
597 wd->evntsel_msr = evntsel_msr;
598 wd->cccr_msr = 0; //unused
599 wd->check_bit = 1ULL<<63;
Don Zickus828f0af2006-09-26 10:52:26 +0200600 return 1;
601fail1:
Andi Kleen89e07562007-04-02 12:14:12 +0200602 __release_perfctr_nmi(-1, perfctr_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200603fail:
604 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Don Zickusb7471c62006-09-26 10:52:26 +0200607static void stop_k7_watchdog(void)
608{
609 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
610
611 wrmsr(wd->evntsel_msr, 0, 0);
612
Andi Kleen89e07562007-04-02 12:14:12 +0200613 __release_evntsel_nmi(-1, wd->evntsel_msr);
614 __release_perfctr_nmi(-1, wd->perfctr_msr);
Don Zickusb7471c62006-09-26 10:52:26 +0200615}
616
617#define P6_EVNTSEL0_ENABLE (1 << 22)
618#define P6_EVNTSEL_INT (1 << 20)
619#define P6_EVNTSEL_OS (1 << 17)
620#define P6_EVNTSEL_USR (1 << 16)
621#define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79
622#define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED
623
Don Zickus828f0af2006-09-26 10:52:26 +0200624static int setup_p6_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Don Zickusb7471c62006-09-26 10:52:26 +0200626 unsigned int perfctr_msr, evntsel_msr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 unsigned int evntsel;
Don Zickusb7471c62006-09-26 10:52:26 +0200628 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Don Zickusb7471c62006-09-26 10:52:26 +0200630 perfctr_msr = MSR_P6_PERFCTR0;
631 evntsel_msr = MSR_P6_EVNTSEL0;
Andi Kleen89e07562007-04-02 12:14:12 +0200632 if (!__reserve_perfctr_nmi(-1, perfctr_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200633 goto fail;
634
Andi Kleen89e07562007-04-02 12:14:12 +0200635 if (!__reserve_evntsel_nmi(-1, evntsel_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200636 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Don Zickusb7471c62006-09-26 10:52:26 +0200638 wrmsrl(perfctr_msr, 0UL);
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 evntsel = P6_EVNTSEL_INT
641 | P6_EVNTSEL_OS
642 | P6_EVNTSEL_USR
643 | P6_NMI_EVENT;
644
Don Zickusb7471c62006-09-26 10:52:26 +0200645 /* setup the timer */
646 wrmsr(evntsel_msr, evntsel, 0);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100647 nmi_hz = adjust_for_32bit_ctr(nmi_hz);
648 write_watchdog_counter32(perfctr_msr, "P6_PERFCTR0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 apic_write(APIC_LVTPC, APIC_DM_NMI);
650 evntsel |= P6_EVNTSEL0_ENABLE;
Don Zickusb7471c62006-09-26 10:52:26 +0200651 wrmsr(evntsel_msr, evntsel, 0);
652
653 wd->perfctr_msr = perfctr_msr;
654 wd->evntsel_msr = evntsel_msr;
655 wd->cccr_msr = 0; //unused
656 wd->check_bit = 1ULL<<39;
Don Zickus828f0af2006-09-26 10:52:26 +0200657 return 1;
658fail1:
Andi Kleen89e07562007-04-02 12:14:12 +0200659 __release_perfctr_nmi(-1, perfctr_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200660fail:
661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Don Zickusb7471c62006-09-26 10:52:26 +0200664static void stop_p6_watchdog(void)
665{
666 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
667
668 wrmsr(wd->evntsel_msr, 0, 0);
669
Andi Kleen89e07562007-04-02 12:14:12 +0200670 __release_evntsel_nmi(-1, wd->evntsel_msr);
671 __release_perfctr_nmi(-1, wd->perfctr_msr);
Don Zickusb7471c62006-09-26 10:52:26 +0200672}
673
674/* Note that these events don't tick when the CPU idles. This means
675 the frequency varies with CPU load. */
676
677#define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
678#define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
679#define P4_ESCR_OS (1<<3)
680#define P4_ESCR_USR (1<<2)
681#define P4_CCCR_OVF_PMI0 (1<<26)
682#define P4_CCCR_OVF_PMI1 (1<<27)
683#define P4_CCCR_THRESHOLD(N) ((N)<<20)
684#define P4_CCCR_COMPLEMENT (1<<19)
685#define P4_CCCR_COMPARE (1<<18)
686#define P4_CCCR_REQUIRED (3<<16)
687#define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
688#define P4_CCCR_ENABLE (1<<12)
689#define P4_CCCR_OVF (1<<31)
690/* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
691 CRU_ESCR0 (with any non-null event selector) through a complemented
692 max threshold. [IA32-Vol3, Section 14.9.9] */
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static int setup_p4_watchdog(void)
695{
Don Zickusb7471c62006-09-26 10:52:26 +0200696 unsigned int perfctr_msr, evntsel_msr, cccr_msr;
697 unsigned int evntsel, cccr_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 unsigned int misc_enable, dummy;
Don Zickusb7471c62006-09-26 10:52:26 +0200699 unsigned int ht_num;
700 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Don Zickusb7471c62006-09-26 10:52:26 +0200702 rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
704 return 0;
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706#ifdef CONFIG_SMP
Don Zickusb7471c62006-09-26 10:52:26 +0200707 /* detect which hyperthread we are on */
708 if (smp_num_siblings == 2) {
709 unsigned int ebx, apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Don Zickusb7471c62006-09-26 10:52:26 +0200711 ebx = cpuid_ebx(1);
712 apicid = (ebx >> 24) & 0xff;
713 ht_num = apicid & 1;
714 } else
715#endif
716 ht_num = 0;
717
718 /* performance counters are shared resources
719 * assign each hyperthread its own set
720 * (re-use the ESCR0 register, seems safe
721 * and keeps the cccr_val the same)
722 */
723 if (!ht_num) {
724 /* logical cpu 0 */
725 perfctr_msr = MSR_P4_IQ_PERFCTR0;
726 evntsel_msr = MSR_P4_CRU_ESCR0;
727 cccr_msr = MSR_P4_IQ_CCCR0;
728 cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4);
729 } else {
730 /* logical cpu 1 */
731 perfctr_msr = MSR_P4_IQ_PERFCTR1;
732 evntsel_msr = MSR_P4_CRU_ESCR0;
733 cccr_msr = MSR_P4_IQ_CCCR1;
734 cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4);
735 }
736
Andi Kleen89e07562007-04-02 12:14:12 +0200737 if (!__reserve_perfctr_nmi(-1, perfctr_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200738 goto fail;
739
Andi Kleen89e07562007-04-02 12:14:12 +0200740 if (!__reserve_evntsel_nmi(-1, evntsel_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200741 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Don Zickusb7471c62006-09-26 10:52:26 +0200743 evntsel = P4_ESCR_EVENT_SELECT(0x3F)
744 | P4_ESCR_OS
745 | P4_ESCR_USR;
746
747 cccr_val |= P4_CCCR_THRESHOLD(15)
748 | P4_CCCR_COMPLEMENT
749 | P4_CCCR_COMPARE
750 | P4_CCCR_REQUIRED;
751
752 wrmsr(evntsel_msr, evntsel, 0);
753 wrmsr(cccr_msr, cccr_val, 0);
754 write_watchdog_counter(perfctr_msr, "P4_IQ_COUNTER0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 apic_write(APIC_LVTPC, APIC_DM_NMI);
Don Zickusb7471c62006-09-26 10:52:26 +0200756 cccr_val |= P4_CCCR_ENABLE;
757 wrmsr(cccr_msr, cccr_val, 0);
758 wd->perfctr_msr = perfctr_msr;
759 wd->evntsel_msr = evntsel_msr;
760 wd->cccr_msr = cccr_msr;
761 wd->check_bit = 1ULL<<39;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return 1;
Don Zickus828f0af2006-09-26 10:52:26 +0200763fail1:
Andi Kleen89e07562007-04-02 12:14:12 +0200764 __release_perfctr_nmi(-1, perfctr_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200765fail:
766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Don Zickusb7471c62006-09-26 10:52:26 +0200769static void stop_p4_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Don Zickusb7471c62006-09-26 10:52:26 +0200771 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Don Zickusb7471c62006-09-26 10:52:26 +0200773 wrmsr(wd->cccr_msr, 0, 0);
774 wrmsr(wd->evntsel_msr, 0, 0);
775
Andi Kleen89e07562007-04-02 12:14:12 +0200776 __release_evntsel_nmi(-1, wd->evntsel_msr);
777 __release_perfctr_nmi(-1, wd->perfctr_msr);
Don Zickusb7471c62006-09-26 10:52:26 +0200778}
779
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200780#define ARCH_PERFMON_NMI_EVENT_SEL ARCH_PERFMON_UNHALTED_CORE_CYCLES_SEL
781#define ARCH_PERFMON_NMI_EVENT_UMASK ARCH_PERFMON_UNHALTED_CORE_CYCLES_UMASK
782
783static int setup_intel_arch_watchdog(void)
784{
785 unsigned int ebx;
786 union cpuid10_eax eax;
787 unsigned int unused;
788 unsigned int perfctr_msr, evntsel_msr;
789 unsigned int evntsel;
790 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
791
792 /*
793 * Check whether the Architectural PerfMon supports
794 * Unhalted Core Cycles Event or not.
795 * NOTE: Corresponding bit = 0 in ebx indicates event present.
796 */
797 cpuid(10, &(eax.full), &ebx, &unused, &unused);
798 if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
799 (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
800 goto fail;
801
802 perfctr_msr = MSR_ARCH_PERFMON_PERFCTR0;
803 evntsel_msr = MSR_ARCH_PERFMON_EVENTSEL0;
804
Andi Kleen89e07562007-04-02 12:14:12 +0200805 if (!__reserve_perfctr_nmi(-1, perfctr_msr))
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200806 goto fail;
807
Andi Kleen89e07562007-04-02 12:14:12 +0200808 if (!__reserve_evntsel_nmi(-1, evntsel_msr))
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200809 goto fail1;
810
811 wrmsrl(perfctr_msr, 0UL);
812
813 evntsel = ARCH_PERFMON_EVENTSEL_INT
814 | ARCH_PERFMON_EVENTSEL_OS
815 | ARCH_PERFMON_EVENTSEL_USR
816 | ARCH_PERFMON_NMI_EVENT_SEL
817 | ARCH_PERFMON_NMI_EVENT_UMASK;
818
819 /* setup the timer */
820 wrmsr(evntsel_msr, evntsel, 0);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100821 nmi_hz = adjust_for_32bit_ctr(nmi_hz);
822 write_watchdog_counter32(perfctr_msr, "INTEL_ARCH_PERFCTR0");
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200823 apic_write(APIC_LVTPC, APIC_DM_NMI);
824 evntsel |= ARCH_PERFMON_EVENTSEL0_ENABLE;
825 wrmsr(evntsel_msr, evntsel, 0);
826
827 wd->perfctr_msr = perfctr_msr;
828 wd->evntsel_msr = evntsel_msr;
829 wd->cccr_msr = 0; //unused
830 wd->check_bit = 1ULL << (eax.split.bit_width - 1);
831 return 1;
832fail1:
Andi Kleen89e07562007-04-02 12:14:12 +0200833 __release_perfctr_nmi(-1, perfctr_msr);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200834fail:
835 return 0;
836}
837
838static void stop_intel_arch_watchdog(void)
839{
840 unsigned int ebx;
841 union cpuid10_eax eax;
842 unsigned int unused;
843 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
844
845 /*
846 * Check whether the Architectural PerfMon supports
847 * Unhalted Core Cycles Event or not.
848 * NOTE: Corresponding bit = 0 in ebx indicates event present.
849 */
850 cpuid(10, &(eax.full), &ebx, &unused, &unused);
851 if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
852 (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
853 return;
854
855 wrmsr(wd->evntsel_msr, 0, 0);
Andi Kleen89e07562007-04-02 12:14:12 +0200856 __release_evntsel_nmi(-1, wd->evntsel_msr);
857 __release_perfctr_nmi(-1, wd->perfctr_msr);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200858}
859
Don Zickusb7471c62006-09-26 10:52:26 +0200860void setup_apic_nmi_watchdog (void *unused)
861{
Shaohua Li4038f902006-09-26 10:52:27 +0200862 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
863
Don Zickusb7471c62006-09-26 10:52:26 +0200864 /* only support LOCAL and IO APICs for now */
865 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
866 (nmi_watchdog != NMI_IO_APIC))
867 return;
868
Shaohua Li4038f902006-09-26 10:52:27 +0200869 if (wd->enabled == 1)
870 return;
871
872 /* cheap hack to support suspend/resume */
873 /* if cpu0 is not active neither should the other cpus */
874 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
875 return;
876
Don Zickusb7471c62006-09-26 10:52:26 +0200877 if (nmi_watchdog == NMI_LOCAL_APIC) {
878 switch (boot_cpu_data.x86_vendor) {
879 case X86_VENDOR_AMD:
Andi Kleen0a4599c2007-02-13 13:26:25 +0100880 if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15 &&
881 boot_cpu_data.x86 != 16)
Don Zickusb7471c62006-09-26 10:52:26 +0200882 return;
883 if (!setup_k7_watchdog())
Don Zickus828f0af2006-09-26 10:52:26 +0200884 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 break;
Don Zickusb7471c62006-09-26 10:52:26 +0200886 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200887 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
888 if (!setup_intel_arch_watchdog())
889 return;
890 break;
891 }
Don Zickusb7471c62006-09-26 10:52:26 +0200892 switch (boot_cpu_data.x86) {
893 case 6:
894 if (boot_cpu_data.x86_model > 0xd)
895 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Don Zickusb7471c62006-09-26 10:52:26 +0200897 if (!setup_p6_watchdog())
898 return;
899 break;
900 case 15:
901 if (boot_cpu_data.x86_model > 0x4)
902 return;
903
904 if (!setup_p4_watchdog())
905 return;
906 break;
907 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return;
Don Zickusb7471c62006-09-26 10:52:26 +0200909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 break;
911 default:
912 return;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
Shaohua Li4038f902006-09-26 10:52:27 +0200915 wd->enabled = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200916 atomic_inc(&nmi_active);
917}
918
Shaohua Li4038f902006-09-26 10:52:27 +0200919void stop_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200920{
Shaohua Li4038f902006-09-26 10:52:27 +0200921 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
922
Don Zickusb7471c62006-09-26 10:52:26 +0200923 /* only support LOCAL and IO APICs for now */
924 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
925 (nmi_watchdog != NMI_IO_APIC))
926 return;
927
Shaohua Li4038f902006-09-26 10:52:27 +0200928 if (wd->enabled == 0)
929 return;
930
Don Zickusb7471c62006-09-26 10:52:26 +0200931 if (nmi_watchdog == NMI_LOCAL_APIC) {
932 switch (boot_cpu_data.x86_vendor) {
933 case X86_VENDOR_AMD:
934 stop_k7_watchdog();
935 break;
936 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200937 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
938 stop_intel_arch_watchdog();
939 break;
940 }
Don Zickusb7471c62006-09-26 10:52:26 +0200941 switch (boot_cpu_data.x86) {
942 case 6:
943 if (boot_cpu_data.x86_model > 0xd)
944 break;
945 stop_p6_watchdog();
946 break;
947 case 15:
948 if (boot_cpu_data.x86_model > 0x4)
949 break;
950 stop_p4_watchdog();
951 break;
952 }
953 break;
954 default:
955 return;
956 }
957 }
Shaohua Li4038f902006-09-26 10:52:27 +0200958 wd->enabled = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200959 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
962/*
963 * the best way to detect whether a CPU has a 'hard lockup' problem
964 * is to check it's local APIC timer IRQ counts. If they are not
965 * changing then that CPU has some problem.
966 *
967 * as these watchdog NMI IRQs are generated on every CPU, we only
968 * have to check the current processor.
969 *
970 * since NMIs don't listen to _any_ locks, we have to be extremely
971 * careful not to rely on unsafe variables. The printk might lock
972 * up though, so we have to break up any console locks first ...
973 * [when there will be more tty-related locks, break them up
974 * here too!]
975 */
976
977static unsigned int
978 last_irq_sums [NR_CPUS],
979 alert_counter [NR_CPUS];
980
981void touch_nmi_watchdog (void)
982{
Jan Beulichc6ea3962006-12-07 02:14:09 +0100983 if (nmi_watchdog > 0) {
984 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Jan Beulichc6ea3962006-12-07 02:14:09 +0100986 /*
987 * Just reset the alert counters, (other CPUs might be
988 * spinning on locks we hold):
989 */
990 for_each_present_cpu (cpu)
991 alert_counter[cpu] = 0;
992 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700993
994 /*
995 * Tickle the softlockup detector too:
996 */
997 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
Michal Schmidt1e862402006-07-30 03:03:29 -0700999EXPORT_SYMBOL(touch_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001extern void die_nmi(struct pt_regs *, const char *msg);
1002
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +02001003__kprobes int nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005
1006 /*
1007 * Since current_thread_info()-> is always on the stack, and we
1008 * always switch the stack NMI-atomically, it's safe to use
1009 * smp_processor_id().
1010 */
Jesper Juhlb791cce2006-03-28 01:56:52 -08001011 unsigned int sum;
Don Zickusb7471c62006-09-26 10:52:26 +02001012 int touched = 0;
Jesper Juhlb791cce2006-03-28 01:56:52 -08001013 int cpu = smp_processor_id();
Don Zickusb7471c62006-09-26 10:52:26 +02001014 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
1015 u64 dummy;
Don Zickus3adbbcc2006-09-26 10:52:26 +02001016 int rc=0;
Don Zickusb7471c62006-09-26 10:52:26 +02001017
1018 /* check for other users first */
1019 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
1020 == NOTIFY_STOP) {
Don Zickus3adbbcc2006-09-26 10:52:26 +02001021 rc = 1;
Don Zickusb7471c62006-09-26 10:52:26 +02001022 touched = 1;
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Andrew Mortonbb81a092006-12-07 02:14:01 +01001025 if (cpu_isset(cpu, backtrace_mask)) {
1026 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
1027
1028 spin_lock(&lock);
1029 printk("NMI backtrace for cpu %d\n", cpu);
1030 dump_stack();
1031 spin_unlock(&lock);
1032 cpu_clear(cpu, backtrace_mask);
1033 }
1034
Thomas Gleixnerf8b50352007-02-16 01:28:09 -08001035 /*
1036 * Take the local apic timer and PIT/HPET into account. We don't
1037 * know which one is active, when we have highres/dyntick on
1038 */
1039 sum = per_cpu(irq_stat, cpu).apic_timer_irqs + kstat_irqs(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Thomas Gleixnerf8b50352007-02-16 01:28:09 -08001041 /* if the none of the timers isn't firing, this cpu isn't doing much */
Don Zickusb7471c62006-09-26 10:52:26 +02001042 if (!touched && last_irq_sums[cpu] == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /*
1044 * Ayiee, looks like this CPU is stuck ...
1045 * wait a few IRQs (5 seconds) before doing the oops ...
1046 */
1047 alert_counter[cpu]++;
1048 if (alert_counter[cpu] == 5*nmi_hz)
George Anzinger748f2ed2005-09-03 15:56:48 -07001049 /*
1050 * die_nmi will return ONLY if NOTIFY_STOP happens..
1051 */
Ingo Molnar91368d72006-03-23 03:00:54 -08001052 die_nmi(regs, "BUG: NMI Watchdog detected LOCKUP");
GOTO Masanorib884e252006-03-07 21:55:29 -08001053 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 last_irq_sums[cpu] = sum;
1055 alert_counter[cpu] = 0;
1056 }
Don Zickusb7471c62006-09-26 10:52:26 +02001057 /* see if the nmi watchdog went off */
1058 if (wd->enabled) {
1059 if (nmi_watchdog == NMI_LOCAL_APIC) {
1060 rdmsrl(wd->perfctr_msr, dummy);
1061 if (dummy & wd->check_bit){
1062 /* this wasn't a watchdog timer interrupt */
1063 goto done;
1064 }
1065
1066 /* only Intel P4 uses the cccr msr */
1067 if (wd->cccr_msr != 0) {
1068 /*
1069 * P4 quirks:
1070 * - An overflown perfctr will assert its interrupt
1071 * until the OVF flag in its CCCR is cleared.
1072 * - LVTPC is masked on interrupt and must be
1073 * unmasked by the LVTPC handler.
1074 */
1075 rdmsrl(wd->cccr_msr, dummy);
1076 dummy &= ~P4_CCCR_OVF;
1077 wrmsrl(wd->cccr_msr, dummy);
1078 apic_write(APIC_LVTPC, APIC_DM_NMI);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +01001079 /* start the cycle over again */
1080 write_watchdog_counter(wd->perfctr_msr, NULL);
Don Zickusb7471c62006-09-26 10:52:26 +02001081 }
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +02001082 else if (wd->perfctr_msr == MSR_P6_PERFCTR0 ||
1083 wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0) {
1084 /* P6 based Pentium M need to re-unmask
Don Zickusb7471c62006-09-26 10:52:26 +02001085 * the apic vector but it doesn't hurt
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +02001086 * other P6 variant.
1087 * ArchPerfom/Core Duo also needs this */
Don Zickusb7471c62006-09-26 10:52:26 +02001088 apic_write(APIC_LVTPC, APIC_DM_NMI);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +01001089 /* P6/ARCH_PERFMON has 32 bit counter write */
1090 write_watchdog_counter32(wd->perfctr_msr, NULL);
1091 } else {
1092 /* start the cycle over again */
1093 write_watchdog_counter(wd->perfctr_msr, NULL);
Don Zickusb7471c62006-09-26 10:52:26 +02001094 }
Don Zickus3adbbcc2006-09-26 10:52:26 +02001095 rc = 1;
1096 } else if (nmi_watchdog == NMI_IO_APIC) {
1097 /* don't know how to accurately check for this.
1098 * just assume it was a watchdog timer interrupt
1099 * This matches the old behaviour.
1100 */
1101 rc = 1;
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +02001102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Don Zickusb7471c62006-09-26 10:52:26 +02001104done:
Don Zickus3adbbcc2006-09-26 10:52:26 +02001105 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Don Zickus2fbe7b22006-09-26 10:52:27 +02001108int do_nmi_callback(struct pt_regs * regs, int cpu)
1109{
1110#ifdef CONFIG_SYSCTL
1111 if (unknown_nmi_panic)
1112 return unknown_nmi_panic_callback(regs, cpu);
1113#endif
1114 return 0;
1115}
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117#ifdef CONFIG_SYSCTL
1118
1119static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
1120{
1121 unsigned char reason = get_nmi_reason();
1122 char buf[64];
1123
Don Zickus2fbe7b22006-09-26 10:52:27 +02001124 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
1125 die_nmi(regs, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return 0;
1127}
1128
Don Zickus407984f2006-09-26 10:52:27 +02001129/*
Don Zickuse33e89a2006-09-26 10:52:27 +02001130 * proc handler for /proc/sys/kernel/nmi
Don Zickus407984f2006-09-26 10:52:27 +02001131 */
1132int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
1133 void __user *buffer, size_t *length, loff_t *ppos)
1134{
1135 int old_state;
1136
1137 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
1138 old_state = nmi_watchdog_enabled;
1139 proc_dointvec(table, write, file, buffer, length, ppos);
1140 if (!!old_state == !!nmi_watchdog_enabled)
1141 return 0;
1142
1143 if (atomic_read(&nmi_active) < 0) {
Don Zickuse33e89a2006-09-26 10:52:27 +02001144 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
1145 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +02001146 }
1147
1148 if (nmi_watchdog == NMI_DEFAULT) {
1149 if (nmi_known_cpu() > 0)
1150 nmi_watchdog = NMI_LOCAL_APIC;
1151 else
1152 nmi_watchdog = NMI_IO_APIC;
1153 }
1154
Don Zickuse33e89a2006-09-26 10:52:27 +02001155 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +02001156 if (nmi_watchdog_enabled)
1157 enable_lapic_nmi_watchdog();
1158 else
1159 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +02001160 } else {
1161 printk( KERN_WARNING
1162 "NMI watchdog doesn't know what hardware to touch\n");
1163 return -EIO;
1164 }
1165 return 0;
1166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168#endif
1169
Andrew Mortonbb81a092006-12-07 02:14:01 +01001170void __trigger_all_cpu_backtrace(void)
1171{
1172 int i;
1173
1174 backtrace_mask = cpu_online_map;
1175 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
1176 for (i = 0; i < 10 * 1000; i++) {
1177 if (cpus_empty(backtrace_mask))
1178 break;
1179 mdelay(1);
1180 }
1181}
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183EXPORT_SYMBOL(nmi_active);
1184EXPORT_SYMBOL(nmi_watchdog);
Don Zickus828f0af2006-09-26 10:52:26 +02001185EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi);
1186EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
1187EXPORT_SYMBOL(reserve_perfctr_nmi);
1188EXPORT_SYMBOL(release_perfctr_nmi);
1189EXPORT_SYMBOL(reserve_evntsel_nmi);
1190EXPORT_SYMBOL(release_evntsel_nmi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191EXPORT_SYMBOL(disable_timer_nmi_watchdog);
1192EXPORT_SYMBOL(enable_timer_nmi_watchdog);