blob: d42374a952d7105804e9da8b4fe46c242ae3808f [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mm.h>
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/sysdev.h>
20#include <linux/nmi.h>
21#include <linux/sysctl.h>
Andi Kleeneddb6fb2006-02-03 21:50:41 +010022#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/proto.h>
27#include <asm/kdebug.h>
Andi Kleen553f2652006-04-07 19:49:57 +020028#include <asm/mce.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Don Zickus828f0af2006-09-26 10:52:26 +020030/* perfctr_nmi_owner tracks the ownership of the perfctr registers:
31 * evtsel_nmi_owner tracks the ownership of the event selection
32 * - different performance counters/ event selection may be reserved for
33 * different subsystems this reservation system just tries to coordinate
34 * things a little
35 */
36static DEFINE_PER_CPU(unsigned, perfctr_nmi_owner);
37static DEFINE_PER_CPU(unsigned, evntsel_nmi_owner[2]);
38
39/* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's
40 * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now)
41 */
42#define NMI_MAX_COUNTER_BITS 66
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * lapic_nmi_owner tracks the ownership of the lapic NMI hardware:
46 * - it may be reserved by some other driver, or not
47 * - when not reserved by some other driver, it may be used for
48 * the NMI watchdog, or not
49 *
50 * This is maintained separately from nmi_active because the NMI
51 * watchdog may also be driven from the I/O APIC timer.
52 */
53static DEFINE_SPINLOCK(lapic_nmi_owner_lock);
54static unsigned int lapic_nmi_owner;
55#define LAPIC_NMI_WATCHDOG (1<<0)
56#define LAPIC_NMI_RESERVED (1<<1)
57
58/* nmi_active:
Don Zickusf2802e72006-09-26 10:52:26 +020059 * >0: the lapic NMI watchdog is active, but can be disabled
60 * <0: the lapic NMI watchdog has not been set up, and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * be enabled
Don Zickusf2802e72006-09-26 10:52:26 +020062 * 0: the lapic NMI watchdog is disabled, but can be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 */
Don Zickusf2802e72006-09-26 10:52:26 +020064atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065int panic_on_timeout;
66
67unsigned int nmi_watchdog = NMI_DEFAULT;
68static unsigned int nmi_hz = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Don Zickusf2802e72006-09-26 10:52:26 +020070struct nmi_watchdog_ctlblk {
71 int enabled;
72 u64 check_bit;
73 unsigned int cccr_msr;
74 unsigned int perfctr_msr; /* the MSR to reset in NMI handler */
75 unsigned int evntsel_msr; /* the MSR to select the events to handle */
76};
77static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Don Zickusf2802e72006-09-26 10:52:26 +020079/* local prototypes */
80static void stop_apic_nmi_watchdog(void *unused);
81static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
Andi Kleen75152112005-05-16 21:53:34 -070082
Don Zickus828f0af2006-09-26 10:52:26 +020083/* converts an msr to an appropriate reservation bit */
84static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
85{
86 /* returns the bit offset of the performance counter register */
87 switch (boot_cpu_data.x86_vendor) {
88 case X86_VENDOR_AMD:
89 return (msr - MSR_K7_PERFCTR0);
90 case X86_VENDOR_INTEL:
91 return (msr - MSR_P4_BPU_PERFCTR0);
92 }
93 return 0;
94}
95
96/* converts an msr to an appropriate reservation bit */
97static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr)
98{
99 /* returns the bit offset of the event selection register */
100 switch (boot_cpu_data.x86_vendor) {
101 case X86_VENDOR_AMD:
102 return (msr - MSR_K7_EVNTSEL0);
103 case X86_VENDOR_INTEL:
104 return (msr - MSR_P4_BSU_ESCR0);
105 }
106 return 0;
107}
108
109/* checks for a bit availability (hack for oprofile) */
110int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
111{
112 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
113
114 return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
115}
116
117/* checks the an msr for availability */
118int avail_to_resrv_perfctr_nmi(unsigned int msr)
119{
120 unsigned int counter;
121
122 counter = nmi_perfctr_msr_to_bit(msr);
123 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
124
125 return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
126}
127
128int reserve_perfctr_nmi(unsigned int msr)
129{
130 unsigned int counter;
131
132 counter = nmi_perfctr_msr_to_bit(msr);
133 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
134
135 if (!test_and_set_bit(counter, &__get_cpu_var(perfctr_nmi_owner)))
136 return 1;
137 return 0;
138}
139
140void release_perfctr_nmi(unsigned int msr)
141{
142 unsigned int counter;
143
144 counter = nmi_perfctr_msr_to_bit(msr);
145 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
146
147 clear_bit(counter, &__get_cpu_var(perfctr_nmi_owner));
148}
149
150int reserve_evntsel_nmi(unsigned int msr)
151{
152 unsigned int counter;
153
154 counter = nmi_evntsel_msr_to_bit(msr);
155 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
156
157 if (!test_and_set_bit(counter, &__get_cpu_var(evntsel_nmi_owner)))
158 return 1;
159 return 0;
160}
161
162void release_evntsel_nmi(unsigned int msr)
163{
164 unsigned int counter;
165
166 counter = nmi_evntsel_msr_to_bit(msr);
167 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
168
169 clear_bit(counter, &__get_cpu_var(evntsel_nmi_owner));
170}
171
Ashok Raje6982c62005-06-25 14:54:58 -0700172static __cpuinit inline int nmi_known_cpu(void)
Andi Kleen75152112005-05-16 21:53:34 -0700173{
174 switch (boot_cpu_data.x86_vendor) {
175 case X86_VENDOR_AMD:
176 return boot_cpu_data.x86 == 15;
177 case X86_VENDOR_INTEL:
Andi Kleenb07f8912006-09-26 10:52:26 +0200178 return boot_cpu_data.x86 == 15;
Andi Kleen75152112005-05-16 21:53:34 -0700179 }
180 return 0;
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183/* Run after command line and cpu_init init, but before all other checks */
Ashok Raje6982c62005-06-25 14:54:58 -0700184void __cpuinit nmi_watchdog_default(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 if (nmi_watchdog != NMI_DEFAULT)
187 return;
Andi Kleen75152112005-05-16 21:53:34 -0700188 if (nmi_known_cpu())
189 nmi_watchdog = NMI_LOCAL_APIC;
190 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 nmi_watchdog = NMI_IO_APIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Andi Kleen75152112005-05-16 21:53:34 -0700194#ifdef CONFIG_SMP
195/* The performance counters used by NMI_LOCAL_APIC don't trigger when
196 * the CPU is idle. To make sure the NMI watchdog really ticks on all
197 * CPUs during the test make them busy.
198 */
199static __init void nmi_cpu_busy(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Andi Kleen75152112005-05-16 21:53:34 -0700201 volatile int *endflag = data;
Ingo Molnar366c7f52006-07-03 00:25:25 -0700202 local_irq_enable_in_hardirq();
Andi Kleen75152112005-05-16 21:53:34 -0700203 /* Intentionally don't use cpu_relax here. This is
204 to make sure that the performance counter really ticks,
205 even if there is a simulator or similar that catches the
206 pause instruction. On a real HT machine this is fine because
207 all other CPUs are busy with "useless" delay loops and don't
208 care if they get somewhat less cycles. */
209 while (*endflag == 0)
210 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
Andi Kleen75152112005-05-16 21:53:34 -0700212#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Andi Kleen75152112005-05-16 21:53:34 -0700214int __init check_nmi_watchdog (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Andi Kleen75152112005-05-16 21:53:34 -0700216 volatile int endflag = 0;
Andi Kleenac6b9312005-05-16 21:53:19 -0700217 int *counts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 int cpu;
219
Don Zickusf2802e72006-09-26 10:52:26 +0200220 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT))
221 return 0;
222
223 if (!atomic_read(&nmi_active))
224 return 0;
225
Andi Kleen75152112005-05-16 21:53:34 -0700226 counts = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
227 if (!counts)
228 return -1;
Jack F Vogel67701ae2005-05-01 08:58:48 -0700229
Andi Kleen75152112005-05-16 21:53:34 -0700230 printk(KERN_INFO "testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Andi Kleen7554c3f2006-01-11 22:45:45 +0100232#ifdef CONFIG_SMP
Andi Kleen75152112005-05-16 21:53:34 -0700233 if (nmi_watchdog == NMI_LOCAL_APIC)
234 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
Andi Kleen7554c3f2006-01-11 22:45:45 +0100235#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 for (cpu = 0; cpu < NR_CPUS; cpu++)
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100238 counts[cpu] = cpu_pda(cpu)->__nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 local_irq_enable();
240 mdelay((10*1000)/nmi_hz); // wait 10 ticks
241
Andrew Morton394e3902006-03-23 03:01:05 -0800242 for_each_online_cpu(cpu) {
Don Zickusf2802e72006-09-26 10:52:26 +0200243 if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled)
244 continue;
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100245 if (cpu_pda(cpu)->__nmi_count - counts[cpu] <= 5) {
Andi Kleen75152112005-05-16 21:53:34 -0700246 printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 cpu,
Andi Kleen75152112005-05-16 21:53:34 -0700248 counts[cpu],
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100249 cpu_pda(cpu)->__nmi_count);
Don Zickusf2802e72006-09-26 10:52:26 +0200250 per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0;
251 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253 }
Don Zickusf2802e72006-09-26 10:52:26 +0200254 if (!atomic_read(&nmi_active)) {
255 kfree(counts);
256 atomic_set(&nmi_active, -1);
257 return -1;
258 }
Andi Kleen75152112005-05-16 21:53:34 -0700259 endflag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 printk("OK.\n");
261
262 /* now that we know it works we can reduce NMI frequency to
263 something more reasonable; makes a difference in some configs */
264 if (nmi_watchdog == NMI_LOCAL_APIC)
265 nmi_hz = 1;
266
Andi Kleenac6b9312005-05-16 21:53:19 -0700267 kfree(counts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 0;
269}
270
271int __init setup_nmi_watchdog(char *str)
272{
273 int nmi;
274
275 if (!strncmp(str,"panic",5)) {
276 panic_on_timeout = 1;
277 str = strchr(str, ',');
278 if (!str)
279 return 1;
280 ++str;
281 }
282
283 get_option(&str, &nmi);
284
Don Zickusf2802e72006-09-26 10:52:26 +0200285 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200287
288 if ((nmi == NMI_LOCAL_APIC) && (nmi_known_cpu() == 0))
289 return 0; /* no lapic support */
Andi Kleen75152112005-05-16 21:53:34 -0700290 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return 1;
292}
293
294__setup("nmi_watchdog=", setup_nmi_watchdog);
295
296static void disable_lapic_nmi_watchdog(void)
297{
Don Zickusf2802e72006-09-26 10:52:26 +0200298 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
299
300 if (atomic_read(&nmi_active) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return;
Don Zickusf2802e72006-09-26 10:52:26 +0200302
303 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
304
305 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308static void enable_lapic_nmi_watchdog(void)
309{
Don Zickusf2802e72006-09-26 10:52:26 +0200310 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
311
312 /* are we already enabled */
313 if (atomic_read(&nmi_active) != 0)
314 return;
315
316 /* are we lapic aware */
317 if (nmi_known_cpu() <= 0)
318 return;
319
320 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
321 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324int reserve_lapic_nmi(void)
325{
326 unsigned int old_owner;
327
328 spin_lock(&lapic_nmi_owner_lock);
329 old_owner = lapic_nmi_owner;
330 lapic_nmi_owner |= LAPIC_NMI_RESERVED;
331 spin_unlock(&lapic_nmi_owner_lock);
332 if (old_owner & LAPIC_NMI_RESERVED)
333 return -EBUSY;
334 if (old_owner & LAPIC_NMI_WATCHDOG)
335 disable_lapic_nmi_watchdog();
336 return 0;
337}
338
339void release_lapic_nmi(void)
340{
341 unsigned int new_owner;
342
343 spin_lock(&lapic_nmi_owner_lock);
344 new_owner = lapic_nmi_owner & ~LAPIC_NMI_RESERVED;
345 lapic_nmi_owner = new_owner;
346 spin_unlock(&lapic_nmi_owner_lock);
347 if (new_owner & LAPIC_NMI_WATCHDOG)
348 enable_lapic_nmi_watchdog();
349}
350
351void disable_timer_nmi_watchdog(void)
352{
Don Zickusf2802e72006-09-26 10:52:26 +0200353 BUG_ON(nmi_watchdog != NMI_IO_APIC);
354
355 if (atomic_read(&nmi_active) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return;
357
358 disable_irq(0);
Don Zickusf2802e72006-09-26 10:52:26 +0200359 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
360
361 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
364void enable_timer_nmi_watchdog(void)
365{
Don Zickusf2802e72006-09-26 10:52:26 +0200366 BUG_ON(nmi_watchdog != NMI_IO_APIC);
367
368 if (atomic_read(&nmi_active) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 touch_nmi_watchdog();
Don Zickusf2802e72006-09-26 10:52:26 +0200370 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 enable_irq(0);
372 }
373}
374
375#ifdef CONFIG_PM
376
377static int nmi_pm_active; /* nmi_active before suspend */
378
Pavel Machek829ca9a2005-09-03 15:56:56 -0700379static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Don Zickusf2802e72006-09-26 10:52:26 +0200381 nmi_pm_active = atomic_read(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 disable_lapic_nmi_watchdog();
383 return 0;
384}
385
386static int lapic_nmi_resume(struct sys_device *dev)
387{
388 if (nmi_pm_active > 0)
Don Zickusf2802e72006-09-26 10:52:26 +0200389 enable_lapic_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0;
391}
392
393static struct sysdev_class nmi_sysclass = {
394 set_kset_name("lapic_nmi"),
395 .resume = lapic_nmi_resume,
396 .suspend = lapic_nmi_suspend,
397};
398
399static struct sys_device device_lapic_nmi = {
400 .id = 0,
401 .cls = &nmi_sysclass,
402};
403
404static int __init init_lapic_nmi_sysfs(void)
405{
406 int error;
407
Don Zickusf2802e72006-09-26 10:52:26 +0200408 /* should really be a BUG_ON but b/c this is an
409 * init call, it just doesn't work. -dcz
410 */
411 if (nmi_watchdog != NMI_LOCAL_APIC)
412 return 0;
413
414 if ( atomic_read(&nmi_active) < 0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 0;
416
417 error = sysdev_class_register(&nmi_sysclass);
418 if (!error)
419 error = sysdev_register(&device_lapic_nmi);
420 return error;
421}
422/* must come after the local APIC's device_initcall() */
423late_initcall(init_lapic_nmi_sysfs);
424
425#endif /* CONFIG_PM */
426
Don Zickusf2802e72006-09-26 10:52:26 +0200427/*
428 * Activate the NMI watchdog via the local APIC.
429 * Original code written by Keith Owens.
430 */
431
432/* Note that these events don't tick when the CPU idles. This means
433 the frequency varies with CPU load. */
434
435#define K7_EVNTSEL_ENABLE (1 << 22)
436#define K7_EVNTSEL_INT (1 << 20)
437#define K7_EVNTSEL_OS (1 << 17)
438#define K7_EVNTSEL_USR (1 << 16)
439#define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
440#define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
441
Don Zickus828f0af2006-09-26 10:52:26 +0200442static int setup_k7_watchdog(void)
Andi Kleen75152112005-05-16 21:53:34 -0700443{
Don Zickusf2802e72006-09-26 10:52:26 +0200444 unsigned int perfctr_msr, evntsel_msr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 unsigned int evntsel;
Don Zickusf2802e72006-09-26 10:52:26 +0200446 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Don Zickusf2802e72006-09-26 10:52:26 +0200448 perfctr_msr = MSR_K7_PERFCTR0;
449 evntsel_msr = MSR_K7_EVNTSEL0;
450 if (!reserve_perfctr_nmi(perfctr_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200451 goto fail;
452
Don Zickusf2802e72006-09-26 10:52:26 +0200453 if (!reserve_evntsel_nmi(evntsel_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200454 goto fail1;
455
456 /* Simulator may not support it */
Don Zickusf2802e72006-09-26 10:52:26 +0200457 if (checking_wrmsrl(evntsel_msr, 0UL))
Don Zickus828f0af2006-09-26 10:52:26 +0200458 goto fail2;
Don Zickusf2802e72006-09-26 10:52:26 +0200459 wrmsrl(perfctr_msr, 0UL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 evntsel = K7_EVNTSEL_INT
462 | K7_EVNTSEL_OS
463 | K7_EVNTSEL_USR
464 | K7_NMI_EVENT;
465
Don Zickusf2802e72006-09-26 10:52:26 +0200466 /* setup the timer */
467 wrmsr(evntsel_msr, evntsel, 0);
468 wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 apic_write(APIC_LVTPC, APIC_DM_NMI);
470 evntsel |= K7_EVNTSEL_ENABLE;
Don Zickusf2802e72006-09-26 10:52:26 +0200471 wrmsr(evntsel_msr, evntsel, 0);
472
473 wd->perfctr_msr = perfctr_msr;
474 wd->evntsel_msr = evntsel_msr;
475 wd->cccr_msr = 0; //unused
476 wd->check_bit = 1ULL<<63;
Don Zickus828f0af2006-09-26 10:52:26 +0200477 return 1;
478fail2:
Don Zickusf2802e72006-09-26 10:52:26 +0200479 release_evntsel_nmi(evntsel_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200480fail1:
Don Zickusf2802e72006-09-26 10:52:26 +0200481 release_perfctr_nmi(perfctr_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200482fail:
483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Don Zickusf2802e72006-09-26 10:52:26 +0200486static void stop_k7_watchdog(void)
487{
488 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
489
490 wrmsr(wd->evntsel_msr, 0, 0);
491
492 release_evntsel_nmi(wd->evntsel_msr);
493 release_perfctr_nmi(wd->perfctr_msr);
494}
495
496/* Note that these events don't tick when the CPU idles. This means
497 the frequency varies with CPU load. */
498
499#define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
500#define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
501#define P4_ESCR_OS (1<<3)
502#define P4_ESCR_USR (1<<2)
503#define P4_CCCR_OVF_PMI0 (1<<26)
504#define P4_CCCR_OVF_PMI1 (1<<27)
505#define P4_CCCR_THRESHOLD(N) ((N)<<20)
506#define P4_CCCR_COMPLEMENT (1<<19)
507#define P4_CCCR_COMPARE (1<<18)
508#define P4_CCCR_REQUIRED (3<<16)
509#define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
510#define P4_CCCR_ENABLE (1<<12)
511#define P4_CCCR_OVF (1<<31)
512/* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
513 CRU_ESCR0 (with any non-null event selector) through a complemented
514 max threshold. [IA32-Vol3, Section 14.9.9] */
Andi Kleen75152112005-05-16 21:53:34 -0700515
516static int setup_p4_watchdog(void)
517{
Don Zickusf2802e72006-09-26 10:52:26 +0200518 unsigned int perfctr_msr, evntsel_msr, cccr_msr;
519 unsigned int evntsel, cccr_val;
Andi Kleen75152112005-05-16 21:53:34 -0700520 unsigned int misc_enable, dummy;
Don Zickusf2802e72006-09-26 10:52:26 +0200521 unsigned int ht_num;
522 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Andi Kleen75152112005-05-16 21:53:34 -0700523
Don Zickusf2802e72006-09-26 10:52:26 +0200524 rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy);
Andi Kleen75152112005-05-16 21:53:34 -0700525 if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
526 return 0;
527
Andi Kleen75152112005-05-16 21:53:34 -0700528#ifdef CONFIG_SMP
Don Zickusf2802e72006-09-26 10:52:26 +0200529 /* detect which hyperthread we are on */
530 if (smp_num_siblings == 2) {
531 unsigned int ebx, apicid;
Andi Kleen75152112005-05-16 21:53:34 -0700532
Don Zickusf2802e72006-09-26 10:52:26 +0200533 ebx = cpuid_ebx(1);
534 apicid = (ebx >> 24) & 0xff;
535 ht_num = apicid & 1;
536 } else
537#endif
538 ht_num = 0;
539
540 /* performance counters are shared resources
541 * assign each hyperthread its own set
542 * (re-use the ESCR0 register, seems safe
543 * and keeps the cccr_val the same)
544 */
545 if (!ht_num) {
546 /* logical cpu 0 */
547 perfctr_msr = MSR_P4_IQ_PERFCTR0;
548 evntsel_msr = MSR_P4_CRU_ESCR0;
549 cccr_msr = MSR_P4_IQ_CCCR0;
550 cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4);
551 } else {
552 /* logical cpu 1 */
553 perfctr_msr = MSR_P4_IQ_PERFCTR1;
554 evntsel_msr = MSR_P4_CRU_ESCR0;
555 cccr_msr = MSR_P4_IQ_CCCR1;
556 cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4);
557 }
558
559 if (!reserve_perfctr_nmi(perfctr_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200560 goto fail;
561
Don Zickusf2802e72006-09-26 10:52:26 +0200562 if (!reserve_evntsel_nmi(evntsel_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200563 goto fail1;
Andi Kleen75152112005-05-16 21:53:34 -0700564
Don Zickusf2802e72006-09-26 10:52:26 +0200565 evntsel = P4_ESCR_EVENT_SELECT(0x3F)
566 | P4_ESCR_OS
567 | P4_ESCR_USR;
568
569 cccr_val |= P4_CCCR_THRESHOLD(15)
570 | P4_CCCR_COMPLEMENT
571 | P4_CCCR_COMPARE
572 | P4_CCCR_REQUIRED;
573
574 wrmsr(evntsel_msr, evntsel, 0);
575 wrmsr(cccr_msr, cccr_val, 0);
576 wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
Andi Kleen75152112005-05-16 21:53:34 -0700577 apic_write(APIC_LVTPC, APIC_DM_NMI);
Don Zickusf2802e72006-09-26 10:52:26 +0200578 cccr_val |= P4_CCCR_ENABLE;
579 wrmsr(cccr_msr, cccr_val, 0);
580
581 wd->perfctr_msr = perfctr_msr;
582 wd->evntsel_msr = evntsel_msr;
583 wd->cccr_msr = cccr_msr;
584 wd->check_bit = 1ULL<<39;
Andi Kleen75152112005-05-16 21:53:34 -0700585 return 1;
Don Zickus828f0af2006-09-26 10:52:26 +0200586fail1:
Don Zickusf2802e72006-09-26 10:52:26 +0200587 release_perfctr_nmi(perfctr_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200588fail:
589 return 0;
Andi Kleen75152112005-05-16 21:53:34 -0700590}
591
Don Zickusf2802e72006-09-26 10:52:26 +0200592static void stop_p4_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Don Zickusf2802e72006-09-26 10:52:26 +0200594 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Andi Kleen75152112005-05-16 21:53:34 -0700595
Don Zickusf2802e72006-09-26 10:52:26 +0200596 wrmsr(wd->cccr_msr, 0, 0);
597 wrmsr(wd->evntsel_msr, 0, 0);
598
599 release_evntsel_nmi(wd->evntsel_msr);
600 release_perfctr_nmi(wd->perfctr_msr);
601}
602
603void setup_apic_nmi_watchdog(void *unused)
604{
605 /* only support LOCAL and IO APICs for now */
606 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
607 (nmi_watchdog != NMI_IO_APIC))
608 return;
609
610 if (nmi_watchdog == NMI_LOCAL_APIC) {
611 switch (boot_cpu_data.x86_vendor) {
612 case X86_VENDOR_AMD:
613 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
614 return;
615 if (!setup_k7_watchdog())
616 return;
617 break;
618 case X86_VENDOR_INTEL:
619 if (!setup_p4_watchdog())
620 return;
621 break;
622 default:
623 return;
624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Don Zickusf2802e72006-09-26 10:52:26 +0200626 __get_cpu_var(nmi_watchdog_ctlblk.enabled) = 1;
627 atomic_inc(&nmi_active);
628}
629
630static void stop_apic_nmi_watchdog(void *unused)
631{
632 /* only support LOCAL and IO APICs for now */
633 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
634 (nmi_watchdog != NMI_IO_APIC))
635 return;
636
637 if (nmi_watchdog == NMI_LOCAL_APIC) {
638 switch (boot_cpu_data.x86_vendor) {
639 case X86_VENDOR_AMD:
640 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
641 return;
642 stop_k7_watchdog();
643 break;
644 case X86_VENDOR_INTEL:
645 stop_p4_watchdog();
646 break;
647 default:
648 return;
649 }
650 }
651 __get_cpu_var(nmi_watchdog_ctlblk.enabled) = 0;
652 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655/*
656 * the best way to detect whether a CPU has a 'hard lockup' problem
657 * is to check it's local APIC timer IRQ counts. If they are not
658 * changing then that CPU has some problem.
659 *
660 * as these watchdog NMI IRQs are generated on every CPU, we only
661 * have to check the current processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
663
Andi Kleen75152112005-05-16 21:53:34 -0700664static DEFINE_PER_CPU(unsigned, last_irq_sum);
665static DEFINE_PER_CPU(local_t, alert_counter);
666static DEFINE_PER_CPU(int, nmi_touch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668void touch_nmi_watchdog (void)
669{
Jan Beulich99019e92006-02-16 23:41:55 +0100670 if (nmi_watchdog > 0) {
671 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Jan Beulich99019e92006-02-16 23:41:55 +0100673 /*
674 * Tell other CPUs to reset their alert counters. We cannot
675 * do it ourselves because the alert count increase is not
676 * atomic.
677 */
678 for_each_present_cpu (cpu)
679 per_cpu(nmi_touch, cpu) = 1;
680 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700681
682 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100685void __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Andi Kleen75152112005-05-16 21:53:34 -0700687 int sum;
688 int touched = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200689 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
690 u64 dummy;
691
692 /* check for other users first */
693 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
694 == NOTIFY_STOP) {
695 touched = 1;
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 sum = read_pda(apic_timer_irqs);
Andi Kleen75152112005-05-16 21:53:34 -0700699 if (__get_cpu_var(nmi_touch)) {
700 __get_cpu_var(nmi_touch) = 0;
701 touched = 1;
702 }
Don Zickusf2802e72006-09-26 10:52:26 +0200703
Andi Kleen553f2652006-04-07 19:49:57 +0200704#ifdef CONFIG_X86_MCE
705 /* Could check oops_in_progress here too, but it's safer
706 not too */
707 if (atomic_read(&mce_entry) > 0)
708 touched = 1;
709#endif
Don Zickusf2802e72006-09-26 10:52:26 +0200710 /* if the apic timer isn't firing, this cpu isn't doing much */
Andi Kleen75152112005-05-16 21:53:34 -0700711 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /*
713 * Ayiee, looks like this CPU is stuck ...
714 * wait a few IRQs (5 seconds) before doing the oops ...
715 */
Andi Kleen75152112005-05-16 21:53:34 -0700716 local_inc(&__get_cpu_var(alert_counter));
Don Zickusf2802e72006-09-26 10:52:26 +0200717 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
Chuck Ebbert84781572005-09-12 18:49:24 +0200718 die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 } else {
Andi Kleen75152112005-05-16 21:53:34 -0700720 __get_cpu_var(last_irq_sum) = sum;
721 local_set(&__get_cpu_var(alert_counter), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Don Zickusf2802e72006-09-26 10:52:26 +0200723
724 /* see if the nmi watchdog went off */
725 if (wd->enabled) {
726 if (nmi_watchdog == NMI_LOCAL_APIC) {
727 rdmsrl(wd->perfctr_msr, dummy);
728 if (dummy & wd->check_bit){
729 /* this wasn't a watchdog timer interrupt */
730 goto done;
731 }
732
733 /* only Intel uses the cccr msr */
734 if (wd->cccr_msr != 0) {
735 /*
736 * P4 quirks:
737 * - An overflown perfctr will assert its interrupt
738 * until the OVF flag in its CCCR is cleared.
739 * - LVTPC is masked on interrupt and must be
740 * unmasked by the LVTPC handler.
741 */
742 rdmsrl(wd->cccr_msr, dummy);
743 dummy &= ~P4_CCCR_OVF;
744 wrmsrl(wd->cccr_msr, dummy);
745 apic_write(APIC_LVTPC, APIC_DM_NMI);
746 }
747 /* start the cycle over again */
748 wrmsrl(wd->perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
749 }
Andi Kleen75152112005-05-16 21:53:34 -0700750 }
Don Zickusf2802e72006-09-26 10:52:26 +0200751done:
752 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100755static __kprobes int dummy_nmi_callback(struct pt_regs * regs, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 return 0;
758}
759
760static nmi_callback_t nmi_callback = dummy_nmi_callback;
761
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100762asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 int cpu = safe_smp_processor_id();
765
766 nmi_enter();
767 add_pda(__nmi_count,1);
Paul E. McKenney19306052005-09-06 15:16:35 -0700768 if (!rcu_dereference(nmi_callback)(regs, cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 default_do_nmi(regs);
770 nmi_exit();
771}
772
773void set_nmi_callback(nmi_callback_t callback)
774{
Jan Beulich8c914cb2006-03-25 16:29:40 +0100775 vmalloc_sync_all();
Paul E. McKenney19306052005-09-06 15:16:35 -0700776 rcu_assign_pointer(nmi_callback, callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
Andrew Mortond9a56852006-06-28 04:27:04 -0700778EXPORT_SYMBOL_GPL(set_nmi_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780void unset_nmi_callback(void)
781{
782 nmi_callback = dummy_nmi_callback;
783}
Andrew Mortond9a56852006-06-28 04:27:04 -0700784EXPORT_SYMBOL_GPL(unset_nmi_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786#ifdef CONFIG_SYSCTL
787
788static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
789{
790 unsigned char reason = get_nmi_reason();
791 char buf[64];
792
793 if (!(reason & 0xc0)) {
794 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
795 die_nmi(buf,regs);
796 }
797 return 0;
798}
799
800/*
801 * proc handler for /proc/sys/kernel/unknown_nmi_panic
802 */
803int proc_unknown_nmi_panic(struct ctl_table *table, int write, struct file *file,
804 void __user *buffer, size_t *length, loff_t *ppos)
805{
806 int old_state;
807
808 old_state = unknown_nmi_panic;
809 proc_dointvec(table, write, file, buffer, length, ppos);
810 if (!!old_state == !!unknown_nmi_panic)
811 return 0;
812
813 if (unknown_nmi_panic) {
814 if (reserve_lapic_nmi() < 0) {
815 unknown_nmi_panic = 0;
816 return -EBUSY;
817 } else {
818 set_nmi_callback(unknown_nmi_panic_callback);
819 }
820 } else {
821 release_lapic_nmi();
822 unset_nmi_callback();
823 }
824 return 0;
825}
826
827#endif
828
829EXPORT_SYMBOL(nmi_active);
830EXPORT_SYMBOL(nmi_watchdog);
Don Zickus828f0af2006-09-26 10:52:26 +0200831EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi);
832EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
833EXPORT_SYMBOL(reserve_perfctr_nmi);
834EXPORT_SYMBOL(release_perfctr_nmi);
835EXPORT_SYMBOL(reserve_evntsel_nmi);
836EXPORT_SYMBOL(release_evntsel_nmi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837EXPORT_SYMBOL(reserve_lapic_nmi);
838EXPORT_SYMBOL(release_lapic_nmi);
839EXPORT_SYMBOL(disable_timer_nmi_watchdog);
840EXPORT_SYMBOL(enable_timer_nmi_watchdog);
841EXPORT_SYMBOL(touch_nmi_watchdog);