blob: 269fe585e71e36fa5e087ca181de2661715d0c41 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/x86_64/nmi.c
3 *
4 * NMI watchdog support on APIC systems
5 *
6 * Started by Ingo Molnar <mingo@redhat.com>
7 *
8 * Fixes:
9 * Mikael Pettersson : AMD K7 support for local APIC NMI watchdog.
10 * Mikael Pettersson : Power Management for local APIC NMI watchdog.
11 * Pavel Machek and
12 * Mikael Pettersson : PM converted to driver model. Disable/enable API.
13 */
14
Andrew Mortonbb81a092006-12-07 02:14:01 +010015#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/sysdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/sysctl.h>
Andi Kleeneddb6fb2006-02-03 21:50:41 +010022#include <linux/kprobes.h>
Andrew Mortonbb81a092006-12-07 02:14:01 +010023#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/proto.h>
28#include <asm/kdebug.h>
Andi Kleen553f2652006-04-07 19:49:57 +020029#include <asm/mce.h>
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +020030#include <asm/intel_arch_perfmon.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Andi Kleen29cbc782006-09-30 01:47:55 +020032int unknown_nmi_panic;
33int nmi_watchdog_enabled;
34int panic_on_unrecovered_nmi;
35
Don Zickus828f0af2006-09-26 10:52:26 +020036/* perfctr_nmi_owner tracks the ownership of the perfctr registers:
37 * evtsel_nmi_owner tracks the ownership of the event selection
38 * - different performance counters/ event selection may be reserved for
39 * different subsystems this reservation system just tries to coordinate
40 * things a little
41 */
42static DEFINE_PER_CPU(unsigned, perfctr_nmi_owner);
43static DEFINE_PER_CPU(unsigned, evntsel_nmi_owner[2]);
44
Andrew Mortonbb81a092006-12-07 02:14:01 +010045static cpumask_t backtrace_mask = CPU_MASK_NONE;
46
Don Zickus828f0af2006-09-26 10:52:26 +020047/* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's
48 * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now)
49 */
50#define NMI_MAX_COUNTER_BITS 66
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* nmi_active:
Don Zickusf2802e72006-09-26 10:52:26 +020053 * >0: the lapic NMI watchdog is active, but can be disabled
54 * <0: the lapic NMI watchdog has not been set up, and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * be enabled
Don Zickusf2802e72006-09-26 10:52:26 +020056 * 0: the lapic NMI watchdog is disabled, but can be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 */
Don Zickusf2802e72006-09-26 10:52:26 +020058atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059int panic_on_timeout;
60
61unsigned int nmi_watchdog = NMI_DEFAULT;
62static unsigned int nmi_hz = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Don Zickusf2802e72006-09-26 10:52:26 +020064struct nmi_watchdog_ctlblk {
65 int enabled;
66 u64 check_bit;
67 unsigned int cccr_msr;
68 unsigned int perfctr_msr; /* the MSR to reset in NMI handler */
69 unsigned int evntsel_msr; /* the MSR to select the events to handle */
70};
71static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Don Zickusf2802e72006-09-26 10:52:26 +020073/* local prototypes */
Don Zickusf2802e72006-09-26 10:52:26 +020074static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
Andi Kleen75152112005-05-16 21:53:34 -070075
Don Zickus828f0af2006-09-26 10:52:26 +020076/* converts an msr to an appropriate reservation bit */
77static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
78{
79 /* returns the bit offset of the performance counter register */
80 switch (boot_cpu_data.x86_vendor) {
81 case X86_VENDOR_AMD:
82 return (msr - MSR_K7_PERFCTR0);
83 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +020084 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
85 return (msr - MSR_ARCH_PERFMON_PERFCTR0);
86 else
87 return (msr - MSR_P4_BPU_PERFCTR0);
Don Zickus828f0af2006-09-26 10:52:26 +020088 }
89 return 0;
90}
91
92/* converts an msr to an appropriate reservation bit */
93static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr)
94{
95 /* returns the bit offset of the event selection register */
96 switch (boot_cpu_data.x86_vendor) {
97 case X86_VENDOR_AMD:
98 return (msr - MSR_K7_EVNTSEL0);
99 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200100 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
101 return (msr - MSR_ARCH_PERFMON_EVENTSEL0);
102 else
103 return (msr - MSR_P4_BSU_ESCR0);
Don Zickus828f0af2006-09-26 10:52:26 +0200104 }
105 return 0;
106}
107
108/* checks for a bit availability (hack for oprofile) */
109int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
110{
111 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
112
113 return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
114}
115
116/* checks the an msr for availability */
117int avail_to_resrv_perfctr_nmi(unsigned int msr)
118{
119 unsigned int counter;
120
121 counter = nmi_perfctr_msr_to_bit(msr);
122 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
123
124 return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
125}
126
127int reserve_perfctr_nmi(unsigned int msr)
128{
129 unsigned int counter;
130
131 counter = nmi_perfctr_msr_to_bit(msr);
132 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
133
134 if (!test_and_set_bit(counter, &__get_cpu_var(perfctr_nmi_owner)))
135 return 1;
136 return 0;
137}
138
139void release_perfctr_nmi(unsigned int msr)
140{
141 unsigned int counter;
142
143 counter = nmi_perfctr_msr_to_bit(msr);
144 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
145
146 clear_bit(counter, &__get_cpu_var(perfctr_nmi_owner));
147}
148
149int reserve_evntsel_nmi(unsigned int msr)
150{
151 unsigned int counter;
152
153 counter = nmi_evntsel_msr_to_bit(msr);
154 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
155
156 if (!test_and_set_bit(counter, &__get_cpu_var(evntsel_nmi_owner)))
157 return 1;
158 return 0;
159}
160
161void release_evntsel_nmi(unsigned int msr)
162{
163 unsigned int counter;
164
165 counter = nmi_evntsel_msr_to_bit(msr);
166 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
167
168 clear_bit(counter, &__get_cpu_var(evntsel_nmi_owner));
169}
170
Ashok Raje6982c62005-06-25 14:54:58 -0700171static __cpuinit inline int nmi_known_cpu(void)
Andi Kleen75152112005-05-16 21:53:34 -0700172{
173 switch (boot_cpu_data.x86_vendor) {
174 case X86_VENDOR_AMD:
175 return boot_cpu_data.x86 == 15;
176 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200177 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
178 return 1;
179 else
180 return (boot_cpu_data.x86 == 15);
Andi Kleen75152112005-05-16 21:53:34 -0700181 }
182 return 0;
183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185/* Run after command line and cpu_init init, but before all other checks */
Don Zickuse33e89a2006-09-26 10:52:27 +0200186void nmi_watchdog_default(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 if (nmi_watchdog != NMI_DEFAULT)
189 return;
Andi Kleen75152112005-05-16 21:53:34 -0700190 if (nmi_known_cpu())
191 nmi_watchdog = NMI_LOCAL_APIC;
192 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 nmi_watchdog = NMI_IO_APIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +0100196static int endflag __initdata = 0;
197
Andi Kleen75152112005-05-16 21:53:34 -0700198#ifdef CONFIG_SMP
199/* The performance counters used by NMI_LOCAL_APIC don't trigger when
200 * the CPU is idle. To make sure the NMI watchdog really ticks on all
201 * CPUs during the test make them busy.
202 */
203static __init void nmi_cpu_busy(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Ingo Molnar366c7f52006-07-03 00:25:25 -0700205 local_irq_enable_in_hardirq();
Andi Kleen75152112005-05-16 21:53:34 -0700206 /* Intentionally don't use cpu_relax here. This is
207 to make sure that the performance counter really ticks,
208 even if there is a simulator or similar that catches the
209 pause instruction. On a real HT machine this is fine because
210 all other CPUs are busy with "useless" delay loops and don't
211 care if they get somewhat less cycles. */
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +0100212 while (endflag == 0)
213 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
Andi Kleen75152112005-05-16 21:53:34 -0700215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Venkatesh Pallipadi16761932007-02-13 13:26:22 +0100217static unsigned int adjust_for_32bit_ctr(unsigned int hz)
218{
219 unsigned int retval = hz;
220
221 /*
222 * On Intel CPUs with ARCH_PERFMON only 32 bits in the counter
223 * are writable, with higher bits sign extending from bit 31.
224 * So, we can only program the counter with 31 bit values and
225 * 32nd bit should be 1, for 33.. to be 1.
226 * Find the appropriate nmi_hz
227 */
228 if ((((u64)cpu_khz * 1000) / retval) > 0x7fffffffULL) {
229 retval = ((u64)cpu_khz * 1000) / 0x7fffffffUL + 1;
230 }
231 return retval;
232}
233
Andi Kleen75152112005-05-16 21:53:34 -0700234int __init check_nmi_watchdog (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Andi Kleenac6b9312005-05-16 21:53:19 -0700236 int *counts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 int cpu;
238
Don Zickusf2802e72006-09-26 10:52:26 +0200239 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT))
240 return 0;
241
242 if (!atomic_read(&nmi_active))
243 return 0;
244
Andi Kleen75152112005-05-16 21:53:34 -0700245 counts = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
246 if (!counts)
247 return -1;
Jack F Vogel67701ae2005-05-01 08:58:48 -0700248
Andi Kleen75152112005-05-16 21:53:34 -0700249 printk(KERN_INFO "testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Andi Kleen7554c3f2006-01-11 22:45:45 +0100251#ifdef CONFIG_SMP
Andi Kleen75152112005-05-16 21:53:34 -0700252 if (nmi_watchdog == NMI_LOCAL_APIC)
253 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
Andi Kleen7554c3f2006-01-11 22:45:45 +0100254#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 for (cpu = 0; cpu < NR_CPUS; cpu++)
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100257 counts[cpu] = cpu_pda(cpu)->__nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 local_irq_enable();
259 mdelay((10*1000)/nmi_hz); // wait 10 ticks
260
Andrew Morton394e3902006-03-23 03:01:05 -0800261 for_each_online_cpu(cpu) {
Don Zickusf2802e72006-09-26 10:52:26 +0200262 if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled)
263 continue;
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100264 if (cpu_pda(cpu)->__nmi_count - counts[cpu] <= 5) {
Andi Kleen75152112005-05-16 21:53:34 -0700265 printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 cpu,
Andi Kleen75152112005-05-16 21:53:34 -0700267 counts[cpu],
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100268 cpu_pda(cpu)->__nmi_count);
Don Zickusf2802e72006-09-26 10:52:26 +0200269 per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0;
270 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 }
Don Zickusf2802e72006-09-26 10:52:26 +0200273 if (!atomic_read(&nmi_active)) {
274 kfree(counts);
275 atomic_set(&nmi_active, -1);
Ravikiran G Thirumalai92715e22006-12-09 21:33:35 +0100276 endflag = 1;
Don Zickusf2802e72006-09-26 10:52:26 +0200277 return -1;
278 }
Andi Kleen75152112005-05-16 21:53:34 -0700279 endflag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 printk("OK.\n");
281
282 /* now that we know it works we can reduce NMI frequency to
283 something more reasonable; makes a difference in some configs */
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200284 if (nmi_watchdog == NMI_LOCAL_APIC) {
285 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 nmi_hz = 1;
Venkatesh Pallipadi16761932007-02-13 13:26:22 +0100288 if (wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0)
289 nmi_hz = adjust_for_32bit_ctr(nmi_hz);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Andi Kleenac6b9312005-05-16 21:53:19 -0700292 kfree(counts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return 0;
294}
295
296int __init setup_nmi_watchdog(char *str)
297{
298 int nmi;
299
300 if (!strncmp(str,"panic",5)) {
301 panic_on_timeout = 1;
302 str = strchr(str, ',');
303 if (!str)
304 return 1;
305 ++str;
306 }
307
308 get_option(&str, &nmi);
309
Don Zickusf2802e72006-09-26 10:52:26 +0200310 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200312
Andi Kleen75152112005-05-16 21:53:34 -0700313 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 1;
315}
316
317__setup("nmi_watchdog=", setup_nmi_watchdog);
318
319static void disable_lapic_nmi_watchdog(void)
320{
Don Zickusf2802e72006-09-26 10:52:26 +0200321 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
322
323 if (atomic_read(&nmi_active) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return;
Don Zickusf2802e72006-09-26 10:52:26 +0200325
326 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
327
328 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331static void enable_lapic_nmi_watchdog(void)
332{
Don Zickusf2802e72006-09-26 10:52:26 +0200333 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
334
335 /* are we already enabled */
336 if (atomic_read(&nmi_active) != 0)
337 return;
338
339 /* are we lapic aware */
340 if (nmi_known_cpu() <= 0)
341 return;
342
343 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
344 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347void disable_timer_nmi_watchdog(void)
348{
Don Zickusf2802e72006-09-26 10:52:26 +0200349 BUG_ON(nmi_watchdog != NMI_IO_APIC);
350
351 if (atomic_read(&nmi_active) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return;
353
354 disable_irq(0);
Don Zickusf2802e72006-09-26 10:52:26 +0200355 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
356
357 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360void enable_timer_nmi_watchdog(void)
361{
Don Zickusf2802e72006-09-26 10:52:26 +0200362 BUG_ON(nmi_watchdog != NMI_IO_APIC);
363
364 if (atomic_read(&nmi_active) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 touch_nmi_watchdog();
Don Zickusf2802e72006-09-26 10:52:26 +0200366 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 enable_irq(0);
368 }
369}
370
Ingo Molnar5d0e6002007-02-13 13:26:24 +0100371static void __acpi_nmi_disable(void *__unused)
372{
373 apic_write(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED);
374}
375
376/*
377 * Disable timer based NMIs on all CPUs:
378 */
379void acpi_nmi_disable(void)
380{
381 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
382 on_each_cpu(__acpi_nmi_disable, NULL, 0, 1);
383}
384
385static void __acpi_nmi_enable(void *__unused)
386{
387 apic_write(APIC_LVT0, APIC_DM_NMI);
388}
389
390/*
391 * Enable timer based NMIs on all CPUs:
392 */
393void acpi_nmi_enable(void)
394{
395 if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC)
396 on_each_cpu(__acpi_nmi_enable, NULL, 0, 1);
397}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398#ifdef CONFIG_PM
399
400static int nmi_pm_active; /* nmi_active before suspend */
401
Pavel Machek829ca9a2005-09-03 15:56:56 -0700402static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Shaohua Li4038f902006-09-26 10:52:27 +0200404 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusf2802e72006-09-26 10:52:26 +0200405 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200406 stop_apic_nmi_watchdog(NULL);
407 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0;
409}
410
411static int lapic_nmi_resume(struct sys_device *dev)
412{
Shaohua Li4038f902006-09-26 10:52:27 +0200413 /* only CPU0 goes here, other CPUs should be offline */
414 if (nmi_pm_active > 0) {
415 setup_apic_nmi_watchdog(NULL);
416 touch_nmi_watchdog();
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return 0;
419}
420
421static struct sysdev_class nmi_sysclass = {
422 set_kset_name("lapic_nmi"),
423 .resume = lapic_nmi_resume,
424 .suspend = lapic_nmi_suspend,
425};
426
427static struct sys_device device_lapic_nmi = {
428 .id = 0,
429 .cls = &nmi_sysclass,
430};
431
432static int __init init_lapic_nmi_sysfs(void)
433{
434 int error;
435
Don Zickusf2802e72006-09-26 10:52:26 +0200436 /* should really be a BUG_ON but b/c this is an
437 * init call, it just doesn't work. -dcz
438 */
439 if (nmi_watchdog != NMI_LOCAL_APIC)
440 return 0;
441
442 if ( atomic_read(&nmi_active) < 0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return 0;
444
445 error = sysdev_class_register(&nmi_sysclass);
446 if (!error)
447 error = sysdev_register(&device_lapic_nmi);
448 return error;
449}
450/* must come after the local APIC's device_initcall() */
451late_initcall(init_lapic_nmi_sysfs);
452
453#endif /* CONFIG_PM */
454
Don Zickusf2802e72006-09-26 10:52:26 +0200455/*
456 * Activate the NMI watchdog via the local APIC.
457 * Original code written by Keith Owens.
458 */
459
460/* Note that these events don't tick when the CPU idles. This means
461 the frequency varies with CPU load. */
462
463#define K7_EVNTSEL_ENABLE (1 << 22)
464#define K7_EVNTSEL_INT (1 << 20)
465#define K7_EVNTSEL_OS (1 << 17)
466#define K7_EVNTSEL_USR (1 << 16)
467#define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
468#define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
469
Don Zickus828f0af2006-09-26 10:52:26 +0200470static int setup_k7_watchdog(void)
Andi Kleen75152112005-05-16 21:53:34 -0700471{
Don Zickusf2802e72006-09-26 10:52:26 +0200472 unsigned int perfctr_msr, evntsel_msr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 unsigned int evntsel;
Don Zickusf2802e72006-09-26 10:52:26 +0200474 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Don Zickusf2802e72006-09-26 10:52:26 +0200476 perfctr_msr = MSR_K7_PERFCTR0;
477 evntsel_msr = MSR_K7_EVNTSEL0;
478 if (!reserve_perfctr_nmi(perfctr_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200479 goto fail;
480
Don Zickusf2802e72006-09-26 10:52:26 +0200481 if (!reserve_evntsel_nmi(evntsel_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200482 goto fail1;
483
484 /* Simulator may not support it */
Don Zickusf2802e72006-09-26 10:52:26 +0200485 if (checking_wrmsrl(evntsel_msr, 0UL))
Don Zickus828f0af2006-09-26 10:52:26 +0200486 goto fail2;
Don Zickusf2802e72006-09-26 10:52:26 +0200487 wrmsrl(perfctr_msr, 0UL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 evntsel = K7_EVNTSEL_INT
490 | K7_EVNTSEL_OS
491 | K7_EVNTSEL_USR
492 | K7_NMI_EVENT;
493
Don Zickusf2802e72006-09-26 10:52:26 +0200494 /* setup the timer */
495 wrmsr(evntsel_msr, evntsel, 0);
496 wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 apic_write(APIC_LVTPC, APIC_DM_NMI);
498 evntsel |= K7_EVNTSEL_ENABLE;
Don Zickusf2802e72006-09-26 10:52:26 +0200499 wrmsr(evntsel_msr, evntsel, 0);
500
501 wd->perfctr_msr = perfctr_msr;
502 wd->evntsel_msr = evntsel_msr;
503 wd->cccr_msr = 0; //unused
504 wd->check_bit = 1ULL<<63;
Don Zickus828f0af2006-09-26 10:52:26 +0200505 return 1;
506fail2:
Don Zickusf2802e72006-09-26 10:52:26 +0200507 release_evntsel_nmi(evntsel_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200508fail1:
Don Zickusf2802e72006-09-26 10:52:26 +0200509 release_perfctr_nmi(perfctr_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200510fail:
511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Don Zickusf2802e72006-09-26 10:52:26 +0200514static void stop_k7_watchdog(void)
515{
516 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
517
518 wrmsr(wd->evntsel_msr, 0, 0);
519
520 release_evntsel_nmi(wd->evntsel_msr);
521 release_perfctr_nmi(wd->perfctr_msr);
522}
523
524/* Note that these events don't tick when the CPU idles. This means
525 the frequency varies with CPU load. */
526
527#define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
528#define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
529#define P4_ESCR_OS (1<<3)
530#define P4_ESCR_USR (1<<2)
531#define P4_CCCR_OVF_PMI0 (1<<26)
532#define P4_CCCR_OVF_PMI1 (1<<27)
533#define P4_CCCR_THRESHOLD(N) ((N)<<20)
534#define P4_CCCR_COMPLEMENT (1<<19)
535#define P4_CCCR_COMPARE (1<<18)
536#define P4_CCCR_REQUIRED (3<<16)
537#define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
538#define P4_CCCR_ENABLE (1<<12)
539#define P4_CCCR_OVF (1<<31)
540/* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
541 CRU_ESCR0 (with any non-null event selector) through a complemented
542 max threshold. [IA32-Vol3, Section 14.9.9] */
Andi Kleen75152112005-05-16 21:53:34 -0700543
544static int setup_p4_watchdog(void)
545{
Don Zickusf2802e72006-09-26 10:52:26 +0200546 unsigned int perfctr_msr, evntsel_msr, cccr_msr;
547 unsigned int evntsel, cccr_val;
Andi Kleen75152112005-05-16 21:53:34 -0700548 unsigned int misc_enable, dummy;
Don Zickusf2802e72006-09-26 10:52:26 +0200549 unsigned int ht_num;
550 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Andi Kleen75152112005-05-16 21:53:34 -0700551
Don Zickusf2802e72006-09-26 10:52:26 +0200552 rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy);
Andi Kleen75152112005-05-16 21:53:34 -0700553 if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
554 return 0;
555
Andi Kleen75152112005-05-16 21:53:34 -0700556#ifdef CONFIG_SMP
Don Zickusf2802e72006-09-26 10:52:26 +0200557 /* detect which hyperthread we are on */
558 if (smp_num_siblings == 2) {
559 unsigned int ebx, apicid;
Andi Kleen75152112005-05-16 21:53:34 -0700560
Don Zickusf2802e72006-09-26 10:52:26 +0200561 ebx = cpuid_ebx(1);
562 apicid = (ebx >> 24) & 0xff;
563 ht_num = apicid & 1;
564 } else
565#endif
566 ht_num = 0;
567
568 /* performance counters are shared resources
569 * assign each hyperthread its own set
570 * (re-use the ESCR0 register, seems safe
571 * and keeps the cccr_val the same)
572 */
573 if (!ht_num) {
574 /* logical cpu 0 */
575 perfctr_msr = MSR_P4_IQ_PERFCTR0;
576 evntsel_msr = MSR_P4_CRU_ESCR0;
577 cccr_msr = MSR_P4_IQ_CCCR0;
578 cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4);
579 } else {
580 /* logical cpu 1 */
581 perfctr_msr = MSR_P4_IQ_PERFCTR1;
582 evntsel_msr = MSR_P4_CRU_ESCR0;
583 cccr_msr = MSR_P4_IQ_CCCR1;
584 cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4);
585 }
586
587 if (!reserve_perfctr_nmi(perfctr_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200588 goto fail;
589
Don Zickusf2802e72006-09-26 10:52:26 +0200590 if (!reserve_evntsel_nmi(evntsel_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200591 goto fail1;
Andi Kleen75152112005-05-16 21:53:34 -0700592
Don Zickusf2802e72006-09-26 10:52:26 +0200593 evntsel = P4_ESCR_EVENT_SELECT(0x3F)
594 | P4_ESCR_OS
595 | P4_ESCR_USR;
596
597 cccr_val |= P4_CCCR_THRESHOLD(15)
598 | P4_CCCR_COMPLEMENT
599 | P4_CCCR_COMPARE
600 | P4_CCCR_REQUIRED;
601
602 wrmsr(evntsel_msr, evntsel, 0);
603 wrmsr(cccr_msr, cccr_val, 0);
604 wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
Andi Kleen75152112005-05-16 21:53:34 -0700605 apic_write(APIC_LVTPC, APIC_DM_NMI);
Don Zickusf2802e72006-09-26 10:52:26 +0200606 cccr_val |= P4_CCCR_ENABLE;
607 wrmsr(cccr_msr, cccr_val, 0);
608
609 wd->perfctr_msr = perfctr_msr;
610 wd->evntsel_msr = evntsel_msr;
611 wd->cccr_msr = cccr_msr;
612 wd->check_bit = 1ULL<<39;
Andi Kleen75152112005-05-16 21:53:34 -0700613 return 1;
Don Zickus828f0af2006-09-26 10:52:26 +0200614fail1:
Don Zickusf2802e72006-09-26 10:52:26 +0200615 release_perfctr_nmi(perfctr_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200616fail:
617 return 0;
Andi Kleen75152112005-05-16 21:53:34 -0700618}
619
Don Zickusf2802e72006-09-26 10:52:26 +0200620static void stop_p4_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Don Zickusf2802e72006-09-26 10:52:26 +0200622 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Andi Kleen75152112005-05-16 21:53:34 -0700623
Don Zickusf2802e72006-09-26 10:52:26 +0200624 wrmsr(wd->cccr_msr, 0, 0);
625 wrmsr(wd->evntsel_msr, 0, 0);
626
627 release_evntsel_nmi(wd->evntsel_msr);
628 release_perfctr_nmi(wd->perfctr_msr);
629}
630
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200631#define ARCH_PERFMON_NMI_EVENT_SEL ARCH_PERFMON_UNHALTED_CORE_CYCLES_SEL
632#define ARCH_PERFMON_NMI_EVENT_UMASK ARCH_PERFMON_UNHALTED_CORE_CYCLES_UMASK
633
634static int setup_intel_arch_watchdog(void)
635{
636 unsigned int ebx;
637 union cpuid10_eax eax;
638 unsigned int unused;
639 unsigned int perfctr_msr, evntsel_msr;
640 unsigned int evntsel;
641 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
642
643 /*
644 * Check whether the Architectural PerfMon supports
645 * Unhalted Core Cycles Event or not.
646 * NOTE: Corresponding bit = 0 in ebx indicates event present.
647 */
648 cpuid(10, &(eax.full), &ebx, &unused, &unused);
649 if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
650 (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
651 goto fail;
652
653 perfctr_msr = MSR_ARCH_PERFMON_PERFCTR0;
654 evntsel_msr = MSR_ARCH_PERFMON_EVENTSEL0;
655
656 if (!reserve_perfctr_nmi(perfctr_msr))
657 goto fail;
658
659 if (!reserve_evntsel_nmi(evntsel_msr))
660 goto fail1;
661
662 wrmsrl(perfctr_msr, 0UL);
663
664 evntsel = ARCH_PERFMON_EVENTSEL_INT
665 | ARCH_PERFMON_EVENTSEL_OS
666 | ARCH_PERFMON_EVENTSEL_USR
667 | ARCH_PERFMON_NMI_EVENT_SEL
668 | ARCH_PERFMON_NMI_EVENT_UMASK;
669
670 /* setup the timer */
671 wrmsr(evntsel_msr, evntsel, 0);
Venkatesh Pallipadi16761932007-02-13 13:26:22 +0100672
673 nmi_hz = adjust_for_32bit_ctr(nmi_hz);
674 wrmsr(perfctr_msr, (u32)(-((u64)cpu_khz * 1000 / nmi_hz)), 0);
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200675
676 apic_write(APIC_LVTPC, APIC_DM_NMI);
677 evntsel |= ARCH_PERFMON_EVENTSEL0_ENABLE;
678 wrmsr(evntsel_msr, evntsel, 0);
679
680 wd->perfctr_msr = perfctr_msr;
681 wd->evntsel_msr = evntsel_msr;
682 wd->cccr_msr = 0; //unused
683 wd->check_bit = 1ULL << (eax.split.bit_width - 1);
684 return 1;
685fail1:
686 release_perfctr_nmi(perfctr_msr);
687fail:
688 return 0;
689}
690
691static void stop_intel_arch_watchdog(void)
692{
693 unsigned int ebx;
694 union cpuid10_eax eax;
695 unsigned int unused;
696 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
697
698 /*
699 * Check whether the Architectural PerfMon supports
700 * Unhalted Core Cycles Event or not.
701 * NOTE: Corresponding bit = 0 in ebx indicates event present.
702 */
703 cpuid(10, &(eax.full), &ebx, &unused, &unused);
704 if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) ||
705 (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
706 return;
707
708 wrmsr(wd->evntsel_msr, 0, 0);
709
710 release_evntsel_nmi(wd->evntsel_msr);
711 release_perfctr_nmi(wd->perfctr_msr);
712}
713
Don Zickusf2802e72006-09-26 10:52:26 +0200714void setup_apic_nmi_watchdog(void *unused)
715{
Shaohua Li4038f902006-09-26 10:52:27 +0200716 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
717
Don Zickusf2802e72006-09-26 10:52:26 +0200718 /* only support LOCAL and IO APICs for now */
719 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
720 (nmi_watchdog != NMI_IO_APIC))
721 return;
722
Shaohua Li4038f902006-09-26 10:52:27 +0200723 if (wd->enabled == 1)
724 return;
725
726 /* cheap hack to support suspend/resume */
727 /* if cpu0 is not active neither should the other cpus */
728 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
729 return;
730
Don Zickusf2802e72006-09-26 10:52:26 +0200731 if (nmi_watchdog == NMI_LOCAL_APIC) {
732 switch (boot_cpu_data.x86_vendor) {
733 case X86_VENDOR_AMD:
734 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
735 return;
736 if (!setup_k7_watchdog())
737 return;
738 break;
739 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200740 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
741 if (!setup_intel_arch_watchdog())
742 return;
743 break;
744 }
Don Zickusf2802e72006-09-26 10:52:26 +0200745 if (!setup_p4_watchdog())
746 return;
747 break;
748 default:
749 return;
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Shaohua Li4038f902006-09-26 10:52:27 +0200752 wd->enabled = 1;
Don Zickusf2802e72006-09-26 10:52:26 +0200753 atomic_inc(&nmi_active);
754}
755
Shaohua Li4038f902006-09-26 10:52:27 +0200756void stop_apic_nmi_watchdog(void *unused)
Don Zickusf2802e72006-09-26 10:52:26 +0200757{
Shaohua Li4038f902006-09-26 10:52:27 +0200758 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
759
Don Zickusf2802e72006-09-26 10:52:26 +0200760 /* only support LOCAL and IO APICs for now */
761 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
762 (nmi_watchdog != NMI_IO_APIC))
763 return;
764
Shaohua Li4038f902006-09-26 10:52:27 +0200765 if (wd->enabled == 0)
766 return;
767
Don Zickusf2802e72006-09-26 10:52:26 +0200768 if (nmi_watchdog == NMI_LOCAL_APIC) {
769 switch (boot_cpu_data.x86_vendor) {
770 case X86_VENDOR_AMD:
771 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
772 return;
773 stop_k7_watchdog();
774 break;
775 case X86_VENDOR_INTEL:
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200776 if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
777 stop_intel_arch_watchdog();
778 break;
779 }
Don Zickusf2802e72006-09-26 10:52:26 +0200780 stop_p4_watchdog();
781 break;
782 default:
783 return;
784 }
785 }
Shaohua Li4038f902006-09-26 10:52:27 +0200786 wd->enabled = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200787 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790/*
791 * the best way to detect whether a CPU has a 'hard lockup' problem
792 * is to check it's local APIC timer IRQ counts. If they are not
793 * changing then that CPU has some problem.
794 *
795 * as these watchdog NMI IRQs are generated on every CPU, we only
796 * have to check the current processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
798
Andi Kleen75152112005-05-16 21:53:34 -0700799static DEFINE_PER_CPU(unsigned, last_irq_sum);
800static DEFINE_PER_CPU(local_t, alert_counter);
801static DEFINE_PER_CPU(int, nmi_touch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803void touch_nmi_watchdog (void)
804{
Jan Beulich99019e92006-02-16 23:41:55 +0100805 if (nmi_watchdog > 0) {
806 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Jan Beulich99019e92006-02-16 23:41:55 +0100808 /*
809 * Tell other CPUs to reset their alert counters. We cannot
810 * do it ourselves because the alert count increase is not
811 * atomic.
812 */
813 for_each_present_cpu (cpu)
814 per_cpu(nmi_touch, cpu) = 1;
815 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700816
817 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Don Zickus3adbbcc2006-09-26 10:52:26 +0200820int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Andi Kleen75152112005-05-16 21:53:34 -0700822 int sum;
823 int touched = 0;
Andrew Mortonbb81a092006-12-07 02:14:01 +0100824 int cpu = smp_processor_id();
Don Zickusf2802e72006-09-26 10:52:26 +0200825 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
826 u64 dummy;
Don Zickus3adbbcc2006-09-26 10:52:26 +0200827 int rc=0;
Don Zickusf2802e72006-09-26 10:52:26 +0200828
829 /* check for other users first */
830 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
831 == NOTIFY_STOP) {
Don Zickus3adbbcc2006-09-26 10:52:26 +0200832 rc = 1;
Don Zickusf2802e72006-09-26 10:52:26 +0200833 touched = 1;
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 sum = read_pda(apic_timer_irqs);
Andi Kleen75152112005-05-16 21:53:34 -0700837 if (__get_cpu_var(nmi_touch)) {
838 __get_cpu_var(nmi_touch) = 0;
839 touched = 1;
840 }
Don Zickusf2802e72006-09-26 10:52:26 +0200841
Andrew Mortonbb81a092006-12-07 02:14:01 +0100842 if (cpu_isset(cpu, backtrace_mask)) {
843 static DEFINE_SPINLOCK(lock); /* Serialise the printks */
844
845 spin_lock(&lock);
846 printk("NMI backtrace for cpu %d\n", cpu);
847 dump_stack();
848 spin_unlock(&lock);
849 cpu_clear(cpu, backtrace_mask);
850 }
851
Andi Kleen553f2652006-04-07 19:49:57 +0200852#ifdef CONFIG_X86_MCE
853 /* Could check oops_in_progress here too, but it's safer
854 not too */
855 if (atomic_read(&mce_entry) > 0)
856 touched = 1;
857#endif
Don Zickusf2802e72006-09-26 10:52:26 +0200858 /* if the apic timer isn't firing, this cpu isn't doing much */
Andi Kleen75152112005-05-16 21:53:34 -0700859 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /*
861 * Ayiee, looks like this CPU is stuck ...
862 * wait a few IRQs (5 seconds) before doing the oops ...
863 */
Andi Kleen75152112005-05-16 21:53:34 -0700864 local_inc(&__get_cpu_var(alert_counter));
Don Zickusf2802e72006-09-26 10:52:26 +0200865 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
Andi Kleenfac58552006-09-26 10:52:27 +0200866 die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs,
867 panic_on_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 } else {
Andi Kleen75152112005-05-16 21:53:34 -0700869 __get_cpu_var(last_irq_sum) = sum;
870 local_set(&__get_cpu_var(alert_counter), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Don Zickusf2802e72006-09-26 10:52:26 +0200872
873 /* see if the nmi watchdog went off */
874 if (wd->enabled) {
875 if (nmi_watchdog == NMI_LOCAL_APIC) {
876 rdmsrl(wd->perfctr_msr, dummy);
877 if (dummy & wd->check_bit){
878 /* this wasn't a watchdog timer interrupt */
879 goto done;
880 }
881
882 /* only Intel uses the cccr msr */
883 if (wd->cccr_msr != 0) {
884 /*
885 * P4 quirks:
886 * - An overflown perfctr will assert its interrupt
887 * until the OVF flag in its CCCR is cleared.
888 * - LVTPC is masked on interrupt and must be
889 * unmasked by the LVTPC handler.
890 */
891 rdmsrl(wd->cccr_msr, dummy);
892 dummy &= ~P4_CCCR_OVF;
893 wrmsrl(wd->cccr_msr, dummy);
894 apic_write(APIC_LVTPC, APIC_DM_NMI);
Venkatesh Pallipadi16761932007-02-13 13:26:22 +0100895 /* start the cycle over again */
896 wrmsrl(wd->perfctr_msr,
897 -((u64)cpu_khz * 1000 / nmi_hz));
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200898 } else if (wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0) {
899 /*
900 * ArchPerfom/Core Duo needs to re-unmask
901 * the apic vector
902 */
903 apic_write(APIC_LVTPC, APIC_DM_NMI);
Venkatesh Pallipadi16761932007-02-13 13:26:22 +0100904 /* ARCH_PERFMON has 32 bit counter writes */
905 wrmsr(wd->perfctr_msr,
906 (u32)(-((u64)cpu_khz * 1000 / nmi_hz)), 0);
907 } else {
908 /* start the cycle over again */
909 wrmsrl(wd->perfctr_msr,
910 -((u64)cpu_khz * 1000 / nmi_hz));
Venkatesh Pallipadi248dcb22006-09-26 10:52:27 +0200911 }
Don Zickus3adbbcc2006-09-26 10:52:26 +0200912 rc = 1;
913 } else if (nmi_watchdog == NMI_IO_APIC) {
914 /* don't know how to accurately check for this.
915 * just assume it was a watchdog timer interrupt
916 * This matches the old behaviour.
917 */
918 rc = 1;
919 } else
920 printk(KERN_WARNING "Unknown enabled NMI hardware?!\n");
Andi Kleen75152112005-05-16 21:53:34 -0700921 }
Don Zickusf2802e72006-09-26 10:52:26 +0200922done:
Don Zickus3adbbcc2006-09-26 10:52:26 +0200923 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100926asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 nmi_enter();
929 add_pda(__nmi_count,1);
Don Zickus3adbbcc2006-09-26 10:52:26 +0200930 default_do_nmi(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 nmi_exit();
932}
933
Don Zickus3adbbcc2006-09-26 10:52:26 +0200934int do_nmi_callback(struct pt_regs * regs, int cpu)
935{
Don Zickus2fbe7b22006-09-26 10:52:27 +0200936#ifdef CONFIG_SYSCTL
937 if (unknown_nmi_panic)
938 return unknown_nmi_panic_callback(regs, cpu);
939#endif
940 return 0;
Don Zickus3adbbcc2006-09-26 10:52:26 +0200941}
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943#ifdef CONFIG_SYSCTL
944
945static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
946{
947 unsigned char reason = get_nmi_reason();
948 char buf[64];
949
Don Zickus2fbe7b22006-09-26 10:52:27 +0200950 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
Andi Kleenfac58552006-09-26 10:52:27 +0200951 die_nmi(buf, regs, 1); /* Always panic here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 0;
953}
954
Don Zickus407984f2006-09-26 10:52:27 +0200955/*
956 * proc handler for /proc/sys/kernel/nmi
957 */
958int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
959 void __user *buffer, size_t *length, loff_t *ppos)
960{
961 int old_state;
962
963 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
964 old_state = nmi_watchdog_enabled;
965 proc_dointvec(table, write, file, buffer, length, ppos);
966 if (!!old_state == !!nmi_watchdog_enabled)
967 return 0;
968
969 if (atomic_read(&nmi_active) < 0) {
970 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
Don Zickuse33e89a2006-09-26 10:52:27 +0200971 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +0200972 }
973
974 /* if nmi_watchdog is not set yet, then set it */
975 nmi_watchdog_default();
976
Don Zickuse33e89a2006-09-26 10:52:27 +0200977 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +0200978 if (nmi_watchdog_enabled)
979 enable_lapic_nmi_watchdog();
980 else
981 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +0200982 } else {
Don Zickuse33e89a2006-09-26 10:52:27 +0200983 printk( KERN_WARNING
Don Zickus407984f2006-09-26 10:52:27 +0200984 "NMI watchdog doesn't know what hardware to touch\n");
985 return -EIO;
986 }
987 return 0;
988}
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990#endif
991
Andrew Mortonbb81a092006-12-07 02:14:01 +0100992void __trigger_all_cpu_backtrace(void)
993{
994 int i;
995
996 backtrace_mask = cpu_online_map;
997 /* Wait for up to 10 seconds for all CPUs to do the backtrace */
998 for (i = 0; i < 10 * 1000; i++) {
999 if (cpus_empty(backtrace_mask))
1000 break;
1001 mdelay(1);
1002 }
1003}
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005EXPORT_SYMBOL(nmi_active);
1006EXPORT_SYMBOL(nmi_watchdog);
Don Zickus828f0af2006-09-26 10:52:26 +02001007EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi);
1008EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
1009EXPORT_SYMBOL(reserve_perfctr_nmi);
1010EXPORT_SYMBOL(release_perfctr_nmi);
1011EXPORT_SYMBOL(reserve_evntsel_nmi);
1012EXPORT_SYMBOL(release_evntsel_nmi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013EXPORT_SYMBOL(disable_timer_nmi_watchdog);
1014EXPORT_SYMBOL(enable_timer_nmi_watchdog);
1015EXPORT_SYMBOL(touch_nmi_watchdog);