blob: a98ba88a8c0ca04dc2385306b8697dea4a10b681 [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 */
44static DEFINE_PER_CPU(unsigned long, perfctr_nmi_owner);
45static DEFINE_PER_CPU(unsigned long, evntsel_nmi_owner[3]);
46
Andrew Mortonbb81a092006-12-07 02:14:01 +010047static cpumask_t backtrace_mask = CPU_MASK_NONE;
48
Don Zickus828f0af2006-09-26 10:52:26 +020049/* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's
50 * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now)
51 */
52#define NMI_MAX_COUNTER_BITS 66
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* nmi_active:
Don Zickusb7471c62006-09-26 10:52:26 +020055 * >0: the lapic NMI watchdog is active, but can be disabled
56 * <0: the lapic NMI watchdog has not been set up, and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * be enabled
Don Zickusb7471c62006-09-26 10:52:26 +020058 * 0: the lapic NMI watchdog is disabled, but can be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
Don Zickusb7471c62006-09-26 10:52:26 +020060atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Don Zickusb7471c62006-09-26 10:52:26 +020062unsigned int nmi_watchdog = NMI_DEFAULT;
63static unsigned int nmi_hz = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Don Zickusb7471c62006-09-26 10:52:26 +020065struct nmi_watchdog_ctlblk {
66 int enabled;
67 u64 check_bit;
68 unsigned int cccr_msr;
69 unsigned int perfctr_msr; /* the MSR to reset in NMI handler */
70 unsigned int evntsel_msr; /* the MSR to select the events to handle */
71};
72static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Don Zickusb7471c62006-09-26 10:52:26 +020074/* local prototypes */
Don Zickusb7471c62006-09-26 10:52:26 +020075static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
76
77extern void show_registers(struct pt_regs *regs);
78extern int unknown_nmi_panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Don Zickus828f0af2006-09-26 10:52:26 +020080/* converts an msr to an appropriate reservation bit */
81static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
82{
83 /* returns the bit offset of the performance counter register */
84 switch (boot_cpu_data.x86_vendor) {
85 case X86_VENDOR_AMD:
86 return (msr - MSR_K7_PERFCTR0);
87 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +020088 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
89 return (msr - MSR_ARCH_PERFMON_PERFCTR0);
90
Don Zickus828f0af2006-09-26 10:52:26 +020091 switch (boot_cpu_data.x86) {
92 case 6:
93 return (msr - MSR_P6_PERFCTR0);
94 case 15:
95 return (msr - MSR_P4_BPU_PERFCTR0);
96 }
97 }
98 return 0;
99}
100
101/* converts an msr to an appropriate reservation bit */
102static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr)
103{
104 /* returns the bit offset of the event selection register */
105 switch (boot_cpu_data.x86_vendor) {
106 case X86_VENDOR_AMD:
107 return (msr - MSR_K7_EVNTSEL0);
108 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200109 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
110 return (msr - MSR_ARCH_PERFMON_EVENTSEL0);
111
Don Zickus828f0af2006-09-26 10:52:26 +0200112 switch (boot_cpu_data.x86) {
113 case 6:
114 return (msr - MSR_P6_EVNTSEL0);
115 case 15:
116 return (msr - MSR_P4_BSU_ESCR0);
117 }
118 }
119 return 0;
120}
121
122/* checks for a bit availability (hack for oprofile) */
123int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
124{
Andi Kleen89e07562007-04-02 12:14:12 +0200125 int cpu;
Don Zickus828f0af2006-09-26 10:52:26 +0200126 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
Andi Kleen89e07562007-04-02 12:14:12 +0200127 for_each_possible_cpu (cpu) {
128 if (test_bit(counter, &per_cpu(perfctr_nmi_owner, cpu)))
129 return 0;
130 }
131 return 1;
Don Zickus828f0af2006-09-26 10:52:26 +0200132}
133
134/* checks the an msr for availability */
135int avail_to_resrv_perfctr_nmi(unsigned int msr)
136{
137 unsigned int counter;
Andi Kleen89e07562007-04-02 12:14:12 +0200138 int cpu;
Don Zickus828f0af2006-09-26 10:52:26 +0200139
140 counter = nmi_perfctr_msr_to_bit(msr);
141 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
142
Andi Kleen89e07562007-04-02 12:14:12 +0200143 for_each_possible_cpu (cpu) {
144 if (test_bit(counter, &per_cpu(perfctr_nmi_owner, cpu)))
145 return 0;
146 }
147 return 1;
148}
149
150static int __reserve_perfctr_nmi(int cpu, unsigned int msr)
151{
152 unsigned int counter;
153 if (cpu < 0)
154 cpu = smp_processor_id();
155
156 counter = nmi_perfctr_msr_to_bit(msr);
157 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
158
159 if (!test_and_set_bit(counter, &per_cpu(perfctr_nmi_owner, cpu)))
160 return 1;
161 return 0;
162}
163
164static void __release_perfctr_nmi(int cpu, unsigned int msr)
165{
166 unsigned int counter;
167 if (cpu < 0)
168 cpu = smp_processor_id();
169
170 counter = nmi_perfctr_msr_to_bit(msr);
171 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
172
173 clear_bit(counter, &per_cpu(perfctr_nmi_owner, cpu));
Don Zickus828f0af2006-09-26 10:52:26 +0200174}
175
176int reserve_perfctr_nmi(unsigned int msr)
177{
Andi Kleen89e07562007-04-02 12:14:12 +0200178 int cpu, i;
179 for_each_possible_cpu (cpu) {
180 if (!__reserve_perfctr_nmi(cpu, msr)) {
181 for_each_possible_cpu (i) {
182 if (i >= cpu)
183 break;
184 __release_perfctr_nmi(i, msr);
185 }
186 return 0;
187 }
188 }
189 return 1;
Don Zickus828f0af2006-09-26 10:52:26 +0200190}
191
192void release_perfctr_nmi(unsigned int msr)
193{
Andi Kleen89e07562007-04-02 12:14:12 +0200194 int cpu;
195 for_each_possible_cpu (cpu) {
196 __release_perfctr_nmi(cpu, msr);
197 }
Don Zickus828f0af2006-09-26 10:52:26 +0200198}
199
Andi Kleen89e07562007-04-02 12:14:12 +0200200int __reserve_evntsel_nmi(int cpu, unsigned int msr)
Don Zickus828f0af2006-09-26 10:52:26 +0200201{
202 unsigned int counter;
Andi Kleen89e07562007-04-02 12:14:12 +0200203 if (cpu < 0)
204 cpu = smp_processor_id();
Don Zickus828f0af2006-09-26 10:52:26 +0200205
206 counter = nmi_evntsel_msr_to_bit(msr);
207 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
208
Andi Kleen89e07562007-04-02 12:14:12 +0200209 if (!test_and_set_bit(counter, &per_cpu(evntsel_nmi_owner, cpu)[0]))
Don Zickus828f0af2006-09-26 10:52:26 +0200210 return 1;
211 return 0;
212}
213
Andi Kleen89e07562007-04-02 12:14:12 +0200214static void __release_evntsel_nmi(int cpu, unsigned int msr)
Don Zickus828f0af2006-09-26 10:52:26 +0200215{
216 unsigned int counter;
Andi Kleen89e07562007-04-02 12:14:12 +0200217 if (cpu < 0)
218 cpu = smp_processor_id();
Don Zickus828f0af2006-09-26 10:52:26 +0200219
220 counter = nmi_evntsel_msr_to_bit(msr);
221 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
222
Andi Kleen89e07562007-04-02 12:14:12 +0200223 clear_bit(counter, &per_cpu(evntsel_nmi_owner, cpu)[0]);
224}
225
226int reserve_evntsel_nmi(unsigned int msr)
227{
228 int cpu, i;
229 for_each_possible_cpu (cpu) {
230 if (!__reserve_evntsel_nmi(cpu, msr)) {
231 for_each_possible_cpu (i) {
232 if (i >= cpu)
233 break;
234 __release_evntsel_nmi(i, msr);
235 }
236 return 0;
237 }
238 }
239 return 1;
240}
241
242void release_evntsel_nmi(unsigned int msr)
243{
244 int cpu;
245 for_each_possible_cpu (cpu) {
246 __release_evntsel_nmi(cpu, msr);
247 }
Don Zickus828f0af2006-09-26 10:52:26 +0200248}
249
Don Zickusb7471c62006-09-26 10:52:26 +0200250static __cpuinit inline int nmi_known_cpu(void)
251{
252 switch (boot_cpu_data.x86_vendor) {
253 case X86_VENDOR_AMD:
Andi Kleen0a4599c2007-02-13 13:26:25 +0100254 return ((boot_cpu_data.x86 == 15) || (boot_cpu_data.x86 == 6)
255 || (boot_cpu_data.x86 == 16));
Don Zickusb7471c62006-09-26 10:52:26 +0200256 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200257 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
258 return 1;
259 else
260 return ((boot_cpu_data.x86 == 15) || (boot_cpu_data.x86 == 6));
Don Zickusb7471c62006-09-26 10:52:26 +0200261 }
262 return 0;
263}
264
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +0100265static int endflag __initdata = 0;
266
Eric W. Biederman29b70082005-10-30 14:59:40 -0800267#ifdef CONFIG_SMP
268/* The performance counters used by NMI_LOCAL_APIC don't trigger when
269 * the CPU is idle. To make sure the NMI watchdog really ticks on all
270 * CPUs during the test make them busy.
271 */
272static __init void nmi_cpu_busy(void *data)
273{
Ingo Molnar366c7f52006-07-03 00:25:25 -0700274 local_irq_enable_in_hardirq();
Eric W. Biederman29b70082005-10-30 14:59:40 -0800275 /* Intentionally don't use cpu_relax here. This is
276 to make sure that the performance counter really ticks,
277 even if there is a simulator or similar that catches the
278 pause instruction. On a real HT machine this is fine because
279 all other CPUs are busy with "useless" delay loops and don't
280 care if they get somewhat less cycles. */
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +0100281 while (endflag == 0)
282 mb();
Eric W. Biederman29b70082005-10-30 14:59:40 -0800283}
284#endif
285
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100286static unsigned int adjust_for_32bit_ctr(unsigned int hz)
287{
288 u64 counter_val;
289 unsigned int retval = hz;
290
291 /*
292 * On Intel CPUs with P6/ARCH_PERFMON only 32 bits in the counter
293 * are writable, with higher bits sign extending from bit 31.
294 * So, we can only program the counter with 31 bit values and
295 * 32nd bit should be 1, for 33.. to be 1.
296 * Find the appropriate nmi_hz
297 */
298 counter_val = (u64)cpu_khz * 1000;
299 do_div(counter_val, retval);
300 if (counter_val > 0x7fffffffULL) {
301 u64 count = (u64)cpu_khz * 1000;
302 do_div(count, 0x7fffffffUL);
303 retval = count + 1;
304 }
305 return retval;
306}
307
Jack F Vogel67701ae2005-05-01 08:58:48 -0700308static int __init check_nmi_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Eric W. Biederman29b70082005-10-30 14:59:40 -0800310 unsigned int *prev_nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 int cpu;
312
Don Zickusb7471c62006-09-26 10:52:26 +0200313 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT))
314 return 0;
315
316 if (!atomic_read(&nmi_active))
Jack F Vogel67701ae2005-05-01 08:58:48 -0700317 return 0;
318
Eric W. Biederman29b70082005-10-30 14:59:40 -0800319 prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
320 if (!prev_nmi_count)
321 return -1;
322
Jack F Vogel67701ae2005-05-01 08:58:48 -0700323 printk(KERN_INFO "Testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Eric W. Biederman29b70082005-10-30 14:59:40 -0800325 if (nmi_watchdog == NMI_LOCAL_APIC)
326 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
327
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800328 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 prev_nmi_count[cpu] = per_cpu(irq_stat, cpu).__nmi_count;
330 local_irq_enable();
Andi Kleen0fb2ebf2007-04-02 12:14:12 +0200331 mdelay((20*1000)/nmi_hz); // wait 20 ticks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800333 for_each_possible_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#ifdef CONFIG_SMP
335 /* Check cpu_callin_map here because that is set
336 after the timer is started. */
337 if (!cpu_isset(cpu, cpu_callin_map))
338 continue;
339#endif
Don Zickusb7471c62006-09-26 10:52:26 +0200340 if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled)
341 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) {
Eric W. Biederman29b70082005-10-30 14:59:40 -0800343 printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
344 cpu,
345 prev_nmi_count[cpu],
346 nmi_count(cpu));
Don Zickusb7471c62006-09-26 10:52:26 +0200347 per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0;
348 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 }
Don Zickusb7471c62006-09-26 10:52:26 +0200351 if (!atomic_read(&nmi_active)) {
352 kfree(prev_nmi_count);
353 atomic_set(&nmi_active, -1);
354 return -1;
355 }
Eric W. Biederman29b70082005-10-30 14:59:40 -0800356 endflag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 printk("OK.\n");
358
359 /* now that we know it works we can reduce NMI frequency to
360 something more reasonable; makes a difference in some configs */
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200361 if (nmi_watchdog == NMI_LOCAL_APIC) {
362 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 nmi_hz = 1;
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100365
366 if (wd->perfctr_msr == MSR_P6_PERFCTR0 ||
367 wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0) {
368 nmi_hz = adjust_for_32bit_ctr(nmi_hz);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200369 }
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Eric W. Biederman29b70082005-10-30 14:59:40 -0800372 kfree(prev_nmi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374}
Jack F Vogel67701ae2005-05-01 08:58:48 -0700375/* This needs to happen later in boot so counters are working */
376late_initcall(check_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378static int __init setup_nmi_watchdog(char *str)
379{
380 int nmi;
381
382 get_option(&str, &nmi);
383
Don Zickusb7471c62006-09-26 10:52:26 +0200384 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 0;
Venkatesh Pallipadi58d9ce7d2007-01-22 20:40:34 -0800386
Don Zickusb7471c62006-09-26 10:52:26 +0200387 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 1;
389}
390
391__setup("nmi_watchdog=", setup_nmi_watchdog);
392
393static void disable_lapic_nmi_watchdog(void)
394{
Don Zickusb7471c62006-09-26 10:52:26 +0200395 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
396
397 if (atomic_read(&nmi_active) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Don Zickusb7471c62006-09-26 10:52:26 +0200400 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Don Zickusb7471c62006-09-26 10:52:26 +0200402 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405static void enable_lapic_nmi_watchdog(void)
406{
Don Zickusb7471c62006-09-26 10:52:26 +0200407 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
408
409 /* are we already enabled */
410 if (atomic_read(&nmi_active) != 0)
411 return;
412
413 /* are we lapic aware */
414 if (nmi_known_cpu() <= 0)
415 return;
416
417 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
418 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421void disable_timer_nmi_watchdog(void)
422{
Don Zickusb7471c62006-09-26 10:52:26 +0200423 BUG_ON(nmi_watchdog != NMI_IO_APIC);
424
425 if (atomic_read(&nmi_active) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return;
427
Don Zickusb7471c62006-09-26 10:52:26 +0200428 disable_irq(0);
429 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
430
431 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434void enable_timer_nmi_watchdog(void)
435{
Don Zickusb7471c62006-09-26 10:52:26 +0200436 BUG_ON(nmi_watchdog != NMI_IO_APIC);
437
438 if (atomic_read(&nmi_active) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 touch_nmi_watchdog();
Don Zickusb7471c62006-09-26 10:52:26 +0200440 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
441 enable_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443}
444
Ingo Molnar5d0e6002007-02-13 13:26:24 +0100445static void __acpi_nmi_disable(void *__unused)
446{
447 apic_write_around(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
448}
449
450/*
451 * Disable timer based NMIs on all CPUs:
452 */
453void acpi_nmi_disable(void)
454{
455 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
456 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
457}
458
459static void __acpi_nmi_enable(void *__unused)
460{
461 apic_write_around(APIC_LVT0, APIC_DM_NMI);
462}
463
464/*
465 * Enable timer based NMIs on all CPUs:
466 */
467void acpi_nmi_enable(void)
468{
469 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
470 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473#ifdef CONFIG_PM
474
475static int nmi_pm_active; /* nmi_active before suspend */
476
Pavel Machek438510f2005-04-16 15:25:24 -0700477static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Shaohua Li4038f902006-09-26 10:52:27 +0200479 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusb7471c62006-09-26 10:52:26 +0200480 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200481 stop_apic_nmi_watchdog(NULL);
482 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 0;
484}
485
486static int lapic_nmi_resume(struct sys_device *dev)
487{
Shaohua Li4038f902006-09-26 10:52:27 +0200488 /* only CPU0 goes here, other CPUs should be offline */
489 if (nmi_pm_active > 0) {
490 setup_apic_nmi_watchdog(NULL);
491 touch_nmi_watchdog();
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return 0;
494}
495
496
497static struct sysdev_class nmi_sysclass = {
498 set_kset_name("lapic_nmi"),
499 .resume = lapic_nmi_resume,
500 .suspend = lapic_nmi_suspend,
501};
502
503static struct sys_device device_lapic_nmi = {
504 .id = 0,
505 .cls = &nmi_sysclass,
506};
507
508static int __init init_lapic_nmi_sysfs(void)
509{
510 int error;
511
Don Zickusb7471c62006-09-26 10:52:26 +0200512 /* should really be a BUG_ON but b/c this is an
513 * init call, it just doesn't work. -dcz
514 */
515 if (nmi_watchdog != NMI_LOCAL_APIC)
516 return 0;
517
518 if ( atomic_read(&nmi_active) < 0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return 0;
520
521 error = sysdev_class_register(&nmi_sysclass);
522 if (!error)
523 error = sysdev_register(&device_lapic_nmi);
524 return error;
525}
526/* must come after the local APIC's device_initcall() */
527late_initcall(init_lapic_nmi_sysfs);
528
529#endif /* CONFIG_PM */
530
531/*
532 * Activate the NMI watchdog via the local APIC.
533 * Original code written by Keith Owens.
534 */
535
Don Zickusb7471c62006-09-26 10:52:26 +0200536static void write_watchdog_counter(unsigned int perfctr_msr, const char *descr)
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700537{
538 u64 count = (u64)cpu_khz * 1000;
539
540 do_div(count, nmi_hz);
541 if(descr)
542 Dprintk("setting %s to -0x%08Lx\n", descr, count);
Don Zickusb7471c62006-09-26 10:52:26 +0200543 wrmsrl(perfctr_msr, 0 - count);
Jan Beulich7fbb4f62005-06-23 00:08:23 -0700544}
545
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100546static void write_watchdog_counter32(unsigned int perfctr_msr,
547 const char *descr)
548{
549 u64 count = (u64)cpu_khz * 1000;
550
551 do_div(count, nmi_hz);
552 if(descr)
553 Dprintk("setting %s to -0x%08Lx\n", descr, count);
554 wrmsr(perfctr_msr, (u32)(-count), 0);
555}
556
Don Zickusb7471c62006-09-26 10:52:26 +0200557/* Note that these events don't tick when the CPU idles. This means
558 the frequency varies with CPU load. */
559
560#define K7_EVNTSEL_ENABLE (1 << 22)
561#define K7_EVNTSEL_INT (1 << 20)
562#define K7_EVNTSEL_OS (1 << 17)
563#define K7_EVNTSEL_USR (1 << 16)
564#define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
565#define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
566
Don Zickus828f0af2006-09-26 10:52:26 +0200567static int setup_k7_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Don Zickusb7471c62006-09-26 10:52:26 +0200569 unsigned int perfctr_msr, evntsel_msr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 unsigned int evntsel;
Don Zickusb7471c62006-09-26 10:52:26 +0200571 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Don Zickusb7471c62006-09-26 10:52:26 +0200573 perfctr_msr = MSR_K7_PERFCTR0;
574 evntsel_msr = MSR_K7_EVNTSEL0;
Andi Kleen89e07562007-04-02 12:14:12 +0200575 if (!__reserve_perfctr_nmi(-1, perfctr_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200576 goto fail;
577
Andi Kleen89e07562007-04-02 12:14:12 +0200578 if (!__reserve_evntsel_nmi(-1, evntsel_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200579 goto fail1;
580
Don Zickusb7471c62006-09-26 10:52:26 +0200581 wrmsrl(perfctr_msr, 0UL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 evntsel = K7_EVNTSEL_INT
584 | K7_EVNTSEL_OS
585 | K7_EVNTSEL_USR
586 | K7_NMI_EVENT;
587
Don Zickusb7471c62006-09-26 10:52:26 +0200588 /* setup the timer */
589 wrmsr(evntsel_msr, evntsel, 0);
590 write_watchdog_counter(perfctr_msr, "K7_PERFCTR0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 apic_write(APIC_LVTPC, APIC_DM_NMI);
592 evntsel |= K7_EVNTSEL_ENABLE;
Don Zickusb7471c62006-09-26 10:52:26 +0200593 wrmsr(evntsel_msr, evntsel, 0);
594
595 wd->perfctr_msr = perfctr_msr;
596 wd->evntsel_msr = evntsel_msr;
597 wd->cccr_msr = 0; //unused
598 wd->check_bit = 1ULL<<63;
Don Zickus828f0af2006-09-26 10:52:26 +0200599 return 1;
600fail1:
Andi Kleen89e07562007-04-02 12:14:12 +0200601 __release_perfctr_nmi(-1, perfctr_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200602fail:
603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Don Zickusb7471c62006-09-26 10:52:26 +0200606static void stop_k7_watchdog(void)
607{
608 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
609
610 wrmsr(wd->evntsel_msr, 0, 0);
611
Andi Kleen89e07562007-04-02 12:14:12 +0200612 __release_evntsel_nmi(-1, wd->evntsel_msr);
613 __release_perfctr_nmi(-1, wd->perfctr_msr);
Don Zickusb7471c62006-09-26 10:52:26 +0200614}
615
616#define P6_EVNTSEL0_ENABLE (1 << 22)
617#define P6_EVNTSEL_INT (1 << 20)
618#define P6_EVNTSEL_OS (1 << 17)
619#define P6_EVNTSEL_USR (1 << 16)
620#define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79
621#define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED
622
Don Zickus828f0af2006-09-26 10:52:26 +0200623static int setup_p6_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Don Zickusb7471c62006-09-26 10:52:26 +0200625 unsigned int perfctr_msr, evntsel_msr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 unsigned int evntsel;
Don Zickusb7471c62006-09-26 10:52:26 +0200627 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Don Zickusb7471c62006-09-26 10:52:26 +0200629 perfctr_msr = MSR_P6_PERFCTR0;
630 evntsel_msr = MSR_P6_EVNTSEL0;
Andi Kleen89e07562007-04-02 12:14:12 +0200631 if (!__reserve_perfctr_nmi(-1, perfctr_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200632 goto fail;
633
Andi Kleen89e07562007-04-02 12:14:12 +0200634 if (!__reserve_evntsel_nmi(-1, evntsel_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200635 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Don Zickusb7471c62006-09-26 10:52:26 +0200637 wrmsrl(perfctr_msr, 0UL);
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 evntsel = P6_EVNTSEL_INT
640 | P6_EVNTSEL_OS
641 | P6_EVNTSEL_USR
642 | P6_NMI_EVENT;
643
Don Zickusb7471c62006-09-26 10:52:26 +0200644 /* setup the timer */
645 wrmsr(evntsel_msr, evntsel, 0);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100646 nmi_hz = adjust_for_32bit_ctr(nmi_hz);
647 write_watchdog_counter32(perfctr_msr, "P6_PERFCTR0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 apic_write(APIC_LVTPC, APIC_DM_NMI);
649 evntsel |= P6_EVNTSEL0_ENABLE;
Don Zickusb7471c62006-09-26 10:52:26 +0200650 wrmsr(evntsel_msr, evntsel, 0);
651
652 wd->perfctr_msr = perfctr_msr;
653 wd->evntsel_msr = evntsel_msr;
654 wd->cccr_msr = 0; //unused
655 wd->check_bit = 1ULL<<39;
Don Zickus828f0af2006-09-26 10:52:26 +0200656 return 1;
657fail1:
Andi Kleen89e07562007-04-02 12:14:12 +0200658 __release_perfctr_nmi(-1, perfctr_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200659fail:
660 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Don Zickusb7471c62006-09-26 10:52:26 +0200663static void stop_p6_watchdog(void)
664{
665 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
666
667 wrmsr(wd->evntsel_msr, 0, 0);
668
Andi Kleen89e07562007-04-02 12:14:12 +0200669 __release_evntsel_nmi(-1, wd->evntsel_msr);
670 __release_perfctr_nmi(-1, wd->perfctr_msr);
Don Zickusb7471c62006-09-26 10:52:26 +0200671}
672
673/* Note that these events don't tick when the CPU idles. This means
674 the frequency varies with CPU load. */
675
676#define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
677#define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
678#define P4_ESCR_OS (1<<3)
679#define P4_ESCR_USR (1<<2)
680#define P4_CCCR_OVF_PMI0 (1<<26)
681#define P4_CCCR_OVF_PMI1 (1<<27)
682#define P4_CCCR_THRESHOLD(N) ((N)<<20)
683#define P4_CCCR_COMPLEMENT (1<<19)
684#define P4_CCCR_COMPARE (1<<18)
685#define P4_CCCR_REQUIRED (3<<16)
686#define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
687#define P4_CCCR_ENABLE (1<<12)
688#define P4_CCCR_OVF (1<<31)
689/* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
690 CRU_ESCR0 (with any non-null event selector) through a complemented
691 max threshold. [IA32-Vol3, Section 14.9.9] */
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693static int setup_p4_watchdog(void)
694{
Don Zickusb7471c62006-09-26 10:52:26 +0200695 unsigned int perfctr_msr, evntsel_msr, cccr_msr;
696 unsigned int evntsel, cccr_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 unsigned int misc_enable, dummy;
Don Zickusb7471c62006-09-26 10:52:26 +0200698 unsigned int ht_num;
699 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Don Zickusb7471c62006-09-26 10:52:26 +0200701 rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
703 return 0;
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705#ifdef CONFIG_SMP
Don Zickusb7471c62006-09-26 10:52:26 +0200706 /* detect which hyperthread we are on */
707 if (smp_num_siblings == 2) {
708 unsigned int ebx, apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Don Zickusb7471c62006-09-26 10:52:26 +0200710 ebx = cpuid_ebx(1);
711 apicid = (ebx >> 24) & 0xff;
712 ht_num = apicid & 1;
713 } else
714#endif
715 ht_num = 0;
716
717 /* performance counters are shared resources
718 * assign each hyperthread its own set
719 * (re-use the ESCR0 register, seems safe
720 * and keeps the cccr_val the same)
721 */
722 if (!ht_num) {
723 /* logical cpu 0 */
724 perfctr_msr = MSR_P4_IQ_PERFCTR0;
725 evntsel_msr = MSR_P4_CRU_ESCR0;
726 cccr_msr = MSR_P4_IQ_CCCR0;
727 cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4);
728 } else {
729 /* logical cpu 1 */
730 perfctr_msr = MSR_P4_IQ_PERFCTR1;
731 evntsel_msr = MSR_P4_CRU_ESCR0;
732 cccr_msr = MSR_P4_IQ_CCCR1;
733 cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4);
734 }
735
Andi Kleen89e07562007-04-02 12:14:12 +0200736 if (!__reserve_perfctr_nmi(-1, perfctr_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200737 goto fail;
738
Andi Kleen89e07562007-04-02 12:14:12 +0200739 if (!__reserve_evntsel_nmi(-1, evntsel_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200740 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Don Zickusb7471c62006-09-26 10:52:26 +0200742 evntsel = P4_ESCR_EVENT_SELECT(0x3F)
743 | P4_ESCR_OS
744 | P4_ESCR_USR;
745
746 cccr_val |= P4_CCCR_THRESHOLD(15)
747 | P4_CCCR_COMPLEMENT
748 | P4_CCCR_COMPARE
749 | P4_CCCR_REQUIRED;
750
751 wrmsr(evntsel_msr, evntsel, 0);
752 wrmsr(cccr_msr, cccr_val, 0);
753 write_watchdog_counter(perfctr_msr, "P4_IQ_COUNTER0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 apic_write(APIC_LVTPC, APIC_DM_NMI);
Don Zickusb7471c62006-09-26 10:52:26 +0200755 cccr_val |= P4_CCCR_ENABLE;
756 wrmsr(cccr_msr, cccr_val, 0);
757 wd->perfctr_msr = perfctr_msr;
758 wd->evntsel_msr = evntsel_msr;
759 wd->cccr_msr = cccr_msr;
760 wd->check_bit = 1ULL<<39;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return 1;
Don Zickus828f0af2006-09-26 10:52:26 +0200762fail1:
Andi Kleen89e07562007-04-02 12:14:12 +0200763 __release_perfctr_nmi(-1, perfctr_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200764fail:
765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Don Zickusb7471c62006-09-26 10:52:26 +0200768static void stop_p4_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Don Zickusb7471c62006-09-26 10:52:26 +0200770 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Don Zickusb7471c62006-09-26 10:52:26 +0200772 wrmsr(wd->cccr_msr, 0, 0);
773 wrmsr(wd->evntsel_msr, 0, 0);
774
Andi Kleen89e07562007-04-02 12:14:12 +0200775 __release_evntsel_nmi(-1, wd->evntsel_msr);
776 __release_perfctr_nmi(-1, wd->perfctr_msr);
Don Zickusb7471c62006-09-26 10:52:26 +0200777}
778
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200779#define ARCH_PERFMON_NMI_EVENT_SEL ARCH_PERFMON_UNHALTED_CORE_CYCLES_SEL
780#define ARCH_PERFMON_NMI_EVENT_UMASK ARCH_PERFMON_UNHALTED_CORE_CYCLES_UMASK
781
782static int setup_intel_arch_watchdog(void)
783{
784 unsigned int ebx;
785 union cpuid10_eax eax;
786 unsigned int unused;
787 unsigned int perfctr_msr, evntsel_msr;
788 unsigned int evntsel;
789 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
790
791 /*
792 * Check whether the Architectural PerfMon supports
793 * Unhalted Core Cycles Event or not.
794 * NOTE: Corresponding bit = 0 in ebx indicates event present.
795 */
796 cpuid(10, &(eax.full), &ebx, &unused, &unused);
797 if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
798 (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
799 goto fail;
800
801 perfctr_msr = MSR_ARCH_PERFMON_PERFCTR0;
802 evntsel_msr = MSR_ARCH_PERFMON_EVENTSEL0;
803
Andi Kleen89e07562007-04-02 12:14:12 +0200804 if (!__reserve_perfctr_nmi(-1, perfctr_msr))
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200805 goto fail;
806
Andi Kleen89e07562007-04-02 12:14:12 +0200807 if (!__reserve_evntsel_nmi(-1, evntsel_msr))
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200808 goto fail1;
809
810 wrmsrl(perfctr_msr, 0UL);
811
812 evntsel = ARCH_PERFMON_EVENTSEL_INT
813 | ARCH_PERFMON_EVENTSEL_OS
814 | ARCH_PERFMON_EVENTSEL_USR
815 | ARCH_PERFMON_NMI_EVENT_SEL
816 | ARCH_PERFMON_NMI_EVENT_UMASK;
817
818 /* setup the timer */
819 wrmsr(evntsel_msr, evntsel, 0);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +0100820 nmi_hz = adjust_for_32bit_ctr(nmi_hz);
821 write_watchdog_counter32(perfctr_msr, "INTEL_ARCH_PERFCTR0");
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200822 apic_write(APIC_LVTPC, APIC_DM_NMI);
823 evntsel |= ARCH_PERFMON_EVENTSEL0_ENABLE;
824 wrmsr(evntsel_msr, evntsel, 0);
825
826 wd->perfctr_msr = perfctr_msr;
827 wd->evntsel_msr = evntsel_msr;
828 wd->cccr_msr = 0; //unused
829 wd->check_bit = 1ULL << (eax.split.bit_width - 1);
830 return 1;
831fail1:
Andi Kleen89e07562007-04-02 12:14:12 +0200832 __release_perfctr_nmi(-1, perfctr_msr);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200833fail:
834 return 0;
835}
836
837static void stop_intel_arch_watchdog(void)
838{
839 unsigned int ebx;
840 union cpuid10_eax eax;
841 unsigned int unused;
842 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
843
844 /*
845 * Check whether the Architectural PerfMon supports
846 * Unhalted Core Cycles Event or not.
847 * NOTE: Corresponding bit = 0 in ebx indicates event present.
848 */
849 cpuid(10, &(eax.full), &ebx, &unused, &unused);
850 if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
851 (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
852 return;
853
854 wrmsr(wd->evntsel_msr, 0, 0);
Andi Kleen89e07562007-04-02 12:14:12 +0200855 __release_evntsel_nmi(-1, wd->evntsel_msr);
856 __release_perfctr_nmi(-1, wd->perfctr_msr);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200857}
858
Don Zickusb7471c62006-09-26 10:52:26 +0200859void setup_apic_nmi_watchdog (void *unused)
860{
Shaohua Li4038f902006-09-26 10:52:27 +0200861 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
862
Don Zickusb7471c62006-09-26 10:52:26 +0200863 /* only support LOCAL and IO APICs for now */
864 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
865 (nmi_watchdog != NMI_IO_APIC))
866 return;
867
Shaohua Li4038f902006-09-26 10:52:27 +0200868 if (wd->enabled == 1)
869 return;
870
871 /* cheap hack to support suspend/resume */
872 /* if cpu0 is not active neither should the other cpus */
873 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
874 return;
875
Don Zickusb7471c62006-09-26 10:52:26 +0200876 if (nmi_watchdog == NMI_LOCAL_APIC) {
877 switch (boot_cpu_data.x86_vendor) {
878 case X86_VENDOR_AMD:
Andi Kleen0a4599c2007-02-13 13:26:25 +0100879 if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15 &&
880 boot_cpu_data.x86 != 16)
Don Zickusb7471c62006-09-26 10:52:26 +0200881 return;
882 if (!setup_k7_watchdog())
Don Zickus828f0af2006-09-26 10:52:26 +0200883 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 break;
Don Zickusb7471c62006-09-26 10:52:26 +0200885 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200886 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
887 if (!setup_intel_arch_watchdog())
888 return;
889 break;
890 }
Don Zickusb7471c62006-09-26 10:52:26 +0200891 switch (boot_cpu_data.x86) {
892 case 6:
893 if (boot_cpu_data.x86_model > 0xd)
894 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Don Zickusb7471c62006-09-26 10:52:26 +0200896 if (!setup_p6_watchdog())
897 return;
898 break;
899 case 15:
900 if (boot_cpu_data.x86_model > 0x4)
901 return;
902
903 if (!setup_p4_watchdog())
904 return;
905 break;
906 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return;
Don Zickusb7471c62006-09-26 10:52:26 +0200908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 break;
910 default:
911 return;
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Shaohua Li4038f902006-09-26 10:52:27 +0200914 wd->enabled = 1;
Don Zickusb7471c62006-09-26 10:52:26 +0200915 atomic_inc(&nmi_active);
916}
917
Shaohua Li4038f902006-09-26 10:52:27 +0200918void stop_apic_nmi_watchdog(void *unused)
Don Zickusb7471c62006-09-26 10:52:26 +0200919{
Shaohua Li4038f902006-09-26 10:52:27 +0200920 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
921
Don Zickusb7471c62006-09-26 10:52:26 +0200922 /* only support LOCAL and IO APICs for now */
923 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
924 (nmi_watchdog != NMI_IO_APIC))
925 return;
926
Shaohua Li4038f902006-09-26 10:52:27 +0200927 if (wd->enabled == 0)
928 return;
929
Don Zickusb7471c62006-09-26 10:52:26 +0200930 if (nmi_watchdog == NMI_LOCAL_APIC) {
931 switch (boot_cpu_data.x86_vendor) {
932 case X86_VENDOR_AMD:
933 stop_k7_watchdog();
934 break;
935 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200936 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
937 stop_intel_arch_watchdog();
938 break;
939 }
Don Zickusb7471c62006-09-26 10:52:26 +0200940 switch (boot_cpu_data.x86) {
941 case 6:
942 if (boot_cpu_data.x86_model > 0xd)
943 break;
944 stop_p6_watchdog();
945 break;
946 case 15:
947 if (boot_cpu_data.x86_model > 0x4)
948 break;
949 stop_p4_watchdog();
950 break;
951 }
952 break;
953 default:
954 return;
955 }
956 }
Shaohua Li4038f902006-09-26 10:52:27 +0200957 wd->enabled = 0;
Don Zickusb7471c62006-09-26 10:52:26 +0200958 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961/*
962 * the best way to detect whether a CPU has a 'hard lockup' problem
963 * is to check it's local APIC timer IRQ counts. If they are not
964 * changing then that CPU has some problem.
965 *
966 * as these watchdog NMI IRQs are generated on every CPU, we only
967 * have to check the current processor.
968 *
969 * since NMIs don't listen to _any_ locks, we have to be extremely
970 * careful not to rely on unsafe variables. The printk might lock
971 * up though, so we have to break up any console locks first ...
972 * [when there will be more tty-related locks, break them up
973 * here too!]
974 */
975
976static unsigned int
977 last_irq_sums [NR_CPUS],
978 alert_counter [NR_CPUS];
979
980void touch_nmi_watchdog (void)
981{
Jan Beulichc6ea3962006-12-07 02:14:09 +0100982 if (nmi_watchdog > 0) {
983 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Jan Beulichc6ea3962006-12-07 02:14:09 +0100985 /*
986 * Just reset the alert counters, (other CPUs might be
987 * spinning on locks we hold):
988 */
989 for_each_present_cpu (cpu)
990 alert_counter[cpu] = 0;
991 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700992
993 /*
994 * Tickle the softlockup detector too:
995 */
996 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
Michal Schmidt1e862402006-07-30 03:03:29 -0700998EXPORT_SYMBOL(touch_nmi_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000extern void die_nmi(struct pt_regs *, const char *msg);
1001
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +02001002__kprobes int nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004
1005 /*
1006 * Since current_thread_info()-> is always on the stack, and we
1007 * always switch the stack NMI-atomically, it's safe to use
1008 * smp_processor_id().
1009 */
Jesper Juhlb791cce2006-03-28 01:56:52 -08001010 unsigned int sum;
Don Zickusb7471c62006-09-26 10:52:26 +02001011 int touched = 0;
Jesper Juhlb791cce2006-03-28 01:56:52 -08001012 int cpu = smp_processor_id();
Don Zickusb7471c62006-09-26 10:52:26 +02001013 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
1014 u64 dummy;
Don Zickus3adbbcce2006-09-26 10:52:26 +02001015 int rc=0;
Don Zickusb7471c62006-09-26 10:52:26 +02001016
1017 /* check for other users first */
1018 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
1019 == NOTIFY_STOP) {
Don Zickus3adbbcce2006-09-26 10:52:26 +02001020 rc = 1;
Don Zickusb7471c62006-09-26 10:52:26 +02001021 touched = 1;
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Andrew Mortonbb81a092006-12-07 02:14:01 +01001024 if (cpu_isset(cpu, backtrace_mask)) {
1025 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
1026
1027 spin_lock(&lock);
1028 printk("NMI backtrace for cpu %d\n", cpu);
1029 dump_stack();
1030 spin_unlock(&lock);
1031 cpu_clear(cpu, backtrace_mask);
1032 }
1033
Thomas Gleixnerf8b50352007-02-16 01:28:09 -08001034 /*
1035 * Take the local apic timer and PIT/HPET into account. We don't
1036 * know which one is active, when we have highres/dyntick on
1037 */
1038 sum = per_cpu(irq_stat, cpu).apic_timer_irqs + kstat_irqs(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Thomas Gleixnerf8b50352007-02-16 01:28:09 -08001040 /* if the none of the timers isn't firing, this cpu isn't doing much */
Don Zickusb7471c62006-09-26 10:52:26 +02001041 if (!touched && last_irq_sums[cpu] == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /*
1043 * Ayiee, looks like this CPU is stuck ...
1044 * wait a few IRQs (5 seconds) before doing the oops ...
1045 */
1046 alert_counter[cpu]++;
1047 if (alert_counter[cpu] == 5*nmi_hz)
George Anzinger748f2ed2005-09-03 15:56:48 -07001048 /*
1049 * die_nmi will return ONLY if NOTIFY_STOP happens..
1050 */
Ingo Molnar91368d72006-03-23 03:00:54 -08001051 die_nmi(regs, "BUG: NMI Watchdog detected LOCKUP");
GOTO Masanorib884e252006-03-07 21:55:29 -08001052 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 last_irq_sums[cpu] = sum;
1054 alert_counter[cpu] = 0;
1055 }
Don Zickusb7471c62006-09-26 10:52:26 +02001056 /* see if the nmi watchdog went off */
1057 if (wd->enabled) {
1058 if (nmi_watchdog == NMI_LOCAL_APIC) {
1059 rdmsrl(wd->perfctr_msr, dummy);
1060 if (dummy & wd->check_bit){
1061 /* this wasn't a watchdog timer interrupt */
1062 goto done;
1063 }
1064
1065 /* only Intel P4 uses the cccr msr */
1066 if (wd->cccr_msr != 0) {
1067 /*
1068 * P4 quirks:
1069 * - An overflown perfctr will assert its interrupt
1070 * until the OVF flag in its CCCR is cleared.
1071 * - LVTPC is masked on interrupt and must be
1072 * unmasked by the LVTPC handler.
1073 */
1074 rdmsrl(wd->cccr_msr, dummy);
1075 dummy &= ~P4_CCCR_OVF;
1076 wrmsrl(wd->cccr_msr, dummy);
1077 apic_write(APIC_LVTPC, APIC_DM_NMI);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +01001078 /* start the cycle over again */
1079 write_watchdog_counter(wd->perfctr_msr, NULL);
Don Zickusb7471c62006-09-26 10:52:26 +02001080 }
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +02001081 else if (wd->perfctr_msr == MSR_P6_PERFCTR0 ||
1082 wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0) {
1083 /* P6 based Pentium M need to re-unmask
Don Zickusb7471c62006-09-26 10:52:26 +02001084 * the apic vector but it doesn't hurt
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +02001085 * other P6 variant.
1086 * ArchPerfom/Core Duo also needs this */
Don Zickusb7471c62006-09-26 10:52:26 +02001087 apic_write(APIC_LVTPC, APIC_DM_NMI);
Venkatesh Pallipadi90ce4bc2007-02-13 13:26:22 +01001088 /* P6/ARCH_PERFMON has 32 bit counter write */
1089 write_watchdog_counter32(wd->perfctr_msr, NULL);
1090 } else {
1091 /* start the cycle over again */
1092 write_watchdog_counter(wd->perfctr_msr, NULL);
Don Zickusb7471c62006-09-26 10:52:26 +02001093 }
Don Zickus3adbbcce2006-09-26 10:52:26 +02001094 rc = 1;
1095 } else if (nmi_watchdog == NMI_IO_APIC) {
1096 /* don't know how to accurately check for this.
1097 * just assume it was a watchdog timer interrupt
1098 * This matches the old behaviour.
1099 */
1100 rc = 1;
Fernando Luis Vázquez Cao06039752006-09-26 10:52:36 +02001101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
Don Zickusb7471c62006-09-26 10:52:26 +02001103done:
Don Zickus3adbbcce2006-09-26 10:52:26 +02001104 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Don Zickus2fbe7b22006-09-26 10:52:27 +02001107int do_nmi_callback(struct pt_regs * regs, int cpu)
1108{
1109#ifdef CONFIG_SYSCTL
1110 if (unknown_nmi_panic)
1111 return unknown_nmi_panic_callback(regs, cpu);
1112#endif
1113 return 0;
1114}
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116#ifdef CONFIG_SYSCTL
1117
1118static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
1119{
1120 unsigned char reason = get_nmi_reason();
1121 char buf[64];
1122
Don Zickus2fbe7b22006-09-26 10:52:27 +02001123 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
1124 die_nmi(regs, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return 0;
1126}
1127
Don Zickus407984f2006-09-26 10:52:27 +02001128/*
Don Zickuse33e89a2006-09-26 10:52:27 +02001129 * proc handler for /proc/sys/kernel/nmi
Don Zickus407984f2006-09-26 10:52:27 +02001130 */
1131int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
1132 void __user *buffer, size_t *length, loff_t *ppos)
1133{
1134 int old_state;
1135
1136 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
1137 old_state = nmi_watchdog_enabled;
1138 proc_dointvec(table, write, file, buffer, length, ppos);
1139 if (!!old_state == !!nmi_watchdog_enabled)
1140 return 0;
1141
1142 if (atomic_read(&nmi_active) < 0) {
Don Zickuse33e89a2006-09-26 10:52:27 +02001143 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
1144 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +02001145 }
1146
1147 if (nmi_watchdog == NMI_DEFAULT) {
1148 if (nmi_known_cpu() > 0)
1149 nmi_watchdog = NMI_LOCAL_APIC;
1150 else
1151 nmi_watchdog = NMI_IO_APIC;
1152 }
1153
Don Zickuse33e89a2006-09-26 10:52:27 +02001154 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +02001155 if (nmi_watchdog_enabled)
1156 enable_lapic_nmi_watchdog();
1157 else
1158 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +02001159 } else {
1160 printk( KERN_WARNING
1161 "NMI watchdog doesn't know what hardware to touch\n");
1162 return -EIO;
1163 }
1164 return 0;
1165}
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167#endif
1168
Andrew Mortonbb81a092006-12-07 02:14:01 +01001169void __trigger_all_cpu_backtrace(void)
1170{
1171 int i;
1172
1173 backtrace_mask = cpu_online_map;
1174 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
1175 for (i = 0; i < 10 * 1000; i++) {
1176 if (cpus_empty(backtrace_mask))
1177 break;
1178 mdelay(1);
1179 }
1180}
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182EXPORT_SYMBOL(nmi_active);
1183EXPORT_SYMBOL(nmi_watchdog);
Don Zickus828f0af2006-09-26 10:52:26 +02001184EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi);
1185EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
1186EXPORT_SYMBOL(reserve_perfctr_nmi);
1187EXPORT_SYMBOL(release_perfctr_nmi);
1188EXPORT_SYMBOL(reserve_evntsel_nmi);
1189EXPORT_SYMBOL(release_evntsel_nmi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190EXPORT_SYMBOL(disable_timer_nmi_watchdog);
1191EXPORT_SYMBOL(enable_timer_nmi_watchdog);