blob: 1b76d1574529df782a19ade51bcd5d04e337aabb [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/* nmi_active:
Don Zickusf2802e72006-09-26 10:52:26 +020045 * >0: the lapic NMI watchdog is active, but can be disabled
46 * <0: the lapic NMI watchdog has not been set up, and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * be enabled
Don Zickusf2802e72006-09-26 10:52:26 +020048 * 0: the lapic NMI watchdog is disabled, but can be enabled
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 */
Don Zickusf2802e72006-09-26 10:52:26 +020050atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051int panic_on_timeout;
52
53unsigned int nmi_watchdog = NMI_DEFAULT;
54static unsigned int nmi_hz = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Don Zickusf2802e72006-09-26 10:52:26 +020056struct nmi_watchdog_ctlblk {
57 int enabled;
58 u64 check_bit;
59 unsigned int cccr_msr;
60 unsigned int perfctr_msr; /* the MSR to reset in NMI handler */
61 unsigned int evntsel_msr; /* the MSR to select the events to handle */
62};
63static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Don Zickusf2802e72006-09-26 10:52:26 +020065/* local prototypes */
Don Zickusf2802e72006-09-26 10:52:26 +020066static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
Andi Kleen75152112005-05-16 21:53:34 -070067
Don Zickus828f0af2006-09-26 10:52:26 +020068/* converts an msr to an appropriate reservation bit */
69static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
70{
71 /* returns the bit offset of the performance counter register */
72 switch (boot_cpu_data.x86_vendor) {
73 case X86_VENDOR_AMD:
74 return (msr - MSR_K7_PERFCTR0);
75 case X86_VENDOR_INTEL:
76 return (msr - MSR_P4_BPU_PERFCTR0);
77 }
78 return 0;
79}
80
81/* converts an msr to an appropriate reservation bit */
82static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr)
83{
84 /* returns the bit offset of the event selection register */
85 switch (boot_cpu_data.x86_vendor) {
86 case X86_VENDOR_AMD:
87 return (msr - MSR_K7_EVNTSEL0);
88 case X86_VENDOR_INTEL:
89 return (msr - MSR_P4_BSU_ESCR0);
90 }
91 return 0;
92}
93
94/* checks for a bit availability (hack for oprofile) */
95int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
96{
97 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
98
99 return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
100}
101
102/* checks the an msr for availability */
103int avail_to_resrv_perfctr_nmi(unsigned int msr)
104{
105 unsigned int counter;
106
107 counter = nmi_perfctr_msr_to_bit(msr);
108 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
109
110 return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
111}
112
113int reserve_perfctr_nmi(unsigned int msr)
114{
115 unsigned int counter;
116
117 counter = nmi_perfctr_msr_to_bit(msr);
118 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
119
120 if (!test_and_set_bit(counter, &__get_cpu_var(perfctr_nmi_owner)))
121 return 1;
122 return 0;
123}
124
125void release_perfctr_nmi(unsigned int msr)
126{
127 unsigned int counter;
128
129 counter = nmi_perfctr_msr_to_bit(msr);
130 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
131
132 clear_bit(counter, &__get_cpu_var(perfctr_nmi_owner));
133}
134
135int reserve_evntsel_nmi(unsigned int msr)
136{
137 unsigned int counter;
138
139 counter = nmi_evntsel_msr_to_bit(msr);
140 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
141
142 if (!test_and_set_bit(counter, &__get_cpu_var(evntsel_nmi_owner)))
143 return 1;
144 return 0;
145}
146
147void release_evntsel_nmi(unsigned int msr)
148{
149 unsigned int counter;
150
151 counter = nmi_evntsel_msr_to_bit(msr);
152 BUG_ON(counter > NMI_MAX_COUNTER_BITS);
153
154 clear_bit(counter, &__get_cpu_var(evntsel_nmi_owner));
155}
156
Ashok Raje6982c62005-06-25 14:54:58 -0700157static __cpuinit inline int nmi_known_cpu(void)
Andi Kleen75152112005-05-16 21:53:34 -0700158{
159 switch (boot_cpu_data.x86_vendor) {
160 case X86_VENDOR_AMD:
161 return boot_cpu_data.x86 == 15;
162 case X86_VENDOR_INTEL:
Andi Kleenb07f8912006-09-26 10:52:26 +0200163 return boot_cpu_data.x86 == 15;
Andi Kleen75152112005-05-16 21:53:34 -0700164 }
165 return 0;
166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168/* Run after command line and cpu_init init, but before all other checks */
Don Zickuse33e89a2006-09-26 10:52:27 +0200169void nmi_watchdog_default(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 if (nmi_watchdog != NMI_DEFAULT)
172 return;
Andi Kleen75152112005-05-16 21:53:34 -0700173 if (nmi_known_cpu())
174 nmi_watchdog = NMI_LOCAL_APIC;
175 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 nmi_watchdog = NMI_IO_APIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Andi Kleen75152112005-05-16 21:53:34 -0700179#ifdef CONFIG_SMP
180/* The performance counters used by NMI_LOCAL_APIC don't trigger when
181 * the CPU is idle. To make sure the NMI watchdog really ticks on all
182 * CPUs during the test make them busy.
183 */
184static __init void nmi_cpu_busy(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Andi Kleen75152112005-05-16 21:53:34 -0700186 volatile int *endflag = data;
Ingo Molnar366c7f52006-07-03 00:25:25 -0700187 local_irq_enable_in_hardirq();
Andi Kleen75152112005-05-16 21:53:34 -0700188 /* Intentionally don't use cpu_relax here. This is
189 to make sure that the performance counter really ticks,
190 even if there is a simulator or similar that catches the
191 pause instruction. On a real HT machine this is fine because
192 all other CPUs are busy with "useless" delay loops and don't
193 care if they get somewhat less cycles. */
194 while (*endflag == 0)
195 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
Andi Kleen75152112005-05-16 21:53:34 -0700197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Andi Kleen75152112005-05-16 21:53:34 -0700199int __init check_nmi_watchdog (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Andi Kleen75152112005-05-16 21:53:34 -0700201 volatile int endflag = 0;
Andi Kleenac6b9312005-05-16 21:53:19 -0700202 int *counts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int cpu;
204
Don Zickusf2802e72006-09-26 10:52:26 +0200205 if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT))
206 return 0;
207
208 if (!atomic_read(&nmi_active))
209 return 0;
210
Andi Kleen75152112005-05-16 21:53:34 -0700211 counts = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
212 if (!counts)
213 return -1;
Jack F Vogel67701ae2005-05-01 08:58:48 -0700214
Andi Kleen75152112005-05-16 21:53:34 -0700215 printk(KERN_INFO "testing NMI watchdog ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Andi Kleen7554c3f2006-01-11 22:45:45 +0100217#ifdef CONFIG_SMP
Andi Kleen75152112005-05-16 21:53:34 -0700218 if (nmi_watchdog == NMI_LOCAL_APIC)
219 smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
Andi Kleen7554c3f2006-01-11 22:45:45 +0100220#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 for (cpu = 0; cpu < NR_CPUS; cpu++)
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100223 counts[cpu] = cpu_pda(cpu)->__nmi_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 local_irq_enable();
225 mdelay((10*1000)/nmi_hz); // wait 10 ticks
226
Andrew Morton394e3902006-03-23 03:01:05 -0800227 for_each_online_cpu(cpu) {
Don Zickusf2802e72006-09-26 10:52:26 +0200228 if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled)
229 continue;
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100230 if (cpu_pda(cpu)->__nmi_count - counts[cpu] <= 5) {
Andi Kleen75152112005-05-16 21:53:34 -0700231 printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 cpu,
Andi Kleen75152112005-05-16 21:53:34 -0700233 counts[cpu],
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100234 cpu_pda(cpu)->__nmi_count);
Don Zickusf2802e72006-09-26 10:52:26 +0200235 per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0;
236 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238 }
Don Zickusf2802e72006-09-26 10:52:26 +0200239 if (!atomic_read(&nmi_active)) {
240 kfree(counts);
241 atomic_set(&nmi_active, -1);
242 return -1;
243 }
Andi Kleen75152112005-05-16 21:53:34 -0700244 endflag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 printk("OK.\n");
246
247 /* now that we know it works we can reduce NMI frequency to
248 something more reasonable; makes a difference in some configs */
249 if (nmi_watchdog == NMI_LOCAL_APIC)
250 nmi_hz = 1;
251
Andi Kleenac6b9312005-05-16 21:53:19 -0700252 kfree(counts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 0;
254}
255
256int __init setup_nmi_watchdog(char *str)
257{
258 int nmi;
259
260 if (!strncmp(str,"panic",5)) {
261 panic_on_timeout = 1;
262 str = strchr(str, ',');
263 if (!str)
264 return 1;
265 ++str;
266 }
267
268 get_option(&str, &nmi);
269
Don Zickusf2802e72006-09-26 10:52:26 +0200270 if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200272
273 if ((nmi == NMI_LOCAL_APIC) && (nmi_known_cpu() == 0))
274 return 0; /* no lapic support */
Andi Kleen75152112005-05-16 21:53:34 -0700275 nmi_watchdog = nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return 1;
277}
278
279__setup("nmi_watchdog=", setup_nmi_watchdog);
280
281static void disable_lapic_nmi_watchdog(void)
282{
Don Zickusf2802e72006-09-26 10:52:26 +0200283 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
284
285 if (atomic_read(&nmi_active) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return;
Don Zickusf2802e72006-09-26 10:52:26 +0200287
288 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
289
290 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293static void enable_lapic_nmi_watchdog(void)
294{
Don Zickusf2802e72006-09-26 10:52:26 +0200295 BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
296
297 /* are we already enabled */
298 if (atomic_read(&nmi_active) != 0)
299 return;
300
301 /* are we lapic aware */
302 if (nmi_known_cpu() <= 0)
303 return;
304
305 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
306 touch_nmi_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309void disable_timer_nmi_watchdog(void)
310{
Don Zickusf2802e72006-09-26 10:52:26 +0200311 BUG_ON(nmi_watchdog != NMI_IO_APIC);
312
313 if (atomic_read(&nmi_active) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return;
315
316 disable_irq(0);
Don Zickusf2802e72006-09-26 10:52:26 +0200317 on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
318
319 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322void enable_timer_nmi_watchdog(void)
323{
Don Zickusf2802e72006-09-26 10:52:26 +0200324 BUG_ON(nmi_watchdog != NMI_IO_APIC);
325
326 if (atomic_read(&nmi_active) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 touch_nmi_watchdog();
Don Zickusf2802e72006-09-26 10:52:26 +0200328 on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 enable_irq(0);
330 }
331}
332
333#ifdef CONFIG_PM
334
335static int nmi_pm_active; /* nmi_active before suspend */
336
Pavel Machek829ca9a2005-09-03 15:56:56 -0700337static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Shaohua Li4038f902006-09-26 10:52:27 +0200339 /* only CPU0 goes here, other CPUs should be offline */
Don Zickusf2802e72006-09-26 10:52:26 +0200340 nmi_pm_active = atomic_read(&nmi_active);
Shaohua Li4038f902006-09-26 10:52:27 +0200341 stop_apic_nmi_watchdog(NULL);
342 BUG_ON(atomic_read(&nmi_active) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344}
345
346static int lapic_nmi_resume(struct sys_device *dev)
347{
Shaohua Li4038f902006-09-26 10:52:27 +0200348 /* only CPU0 goes here, other CPUs should be offline */
349 if (nmi_pm_active > 0) {
350 setup_apic_nmi_watchdog(NULL);
351 touch_nmi_watchdog();
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354}
355
356static struct sysdev_class nmi_sysclass = {
357 set_kset_name("lapic_nmi"),
358 .resume = lapic_nmi_resume,
359 .suspend = lapic_nmi_suspend,
360};
361
362static struct sys_device device_lapic_nmi = {
363 .id = 0,
364 .cls = &nmi_sysclass,
365};
366
367static int __init init_lapic_nmi_sysfs(void)
368{
369 int error;
370
Don Zickusf2802e72006-09-26 10:52:26 +0200371 /* should really be a BUG_ON but b/c this is an
372 * init call, it just doesn't work. -dcz
373 */
374 if (nmi_watchdog != NMI_LOCAL_APIC)
375 return 0;
376
377 if ( atomic_read(&nmi_active) < 0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
379
380 error = sysdev_class_register(&nmi_sysclass);
381 if (!error)
382 error = sysdev_register(&device_lapic_nmi);
383 return error;
384}
385/* must come after the local APIC's device_initcall() */
386late_initcall(init_lapic_nmi_sysfs);
387
388#endif /* CONFIG_PM */
389
Don Zickusf2802e72006-09-26 10:52:26 +0200390/*
391 * Activate the NMI watchdog via the local APIC.
392 * Original code written by Keith Owens.
393 */
394
395/* Note that these events don't tick when the CPU idles. This means
396 the frequency varies with CPU load. */
397
398#define K7_EVNTSEL_ENABLE (1 << 22)
399#define K7_EVNTSEL_INT (1 << 20)
400#define K7_EVNTSEL_OS (1 << 17)
401#define K7_EVNTSEL_USR (1 << 16)
402#define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
403#define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
404
Don Zickus828f0af2006-09-26 10:52:26 +0200405static int setup_k7_watchdog(void)
Andi Kleen75152112005-05-16 21:53:34 -0700406{
Don Zickusf2802e72006-09-26 10:52:26 +0200407 unsigned int perfctr_msr, evntsel_msr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 unsigned int evntsel;
Don Zickusf2802e72006-09-26 10:52:26 +0200409 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Don Zickusf2802e72006-09-26 10:52:26 +0200411 perfctr_msr = MSR_K7_PERFCTR0;
412 evntsel_msr = MSR_K7_EVNTSEL0;
413 if (!reserve_perfctr_nmi(perfctr_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200414 goto fail;
415
Don Zickusf2802e72006-09-26 10:52:26 +0200416 if (!reserve_evntsel_nmi(evntsel_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200417 goto fail1;
418
419 /* Simulator may not support it */
Don Zickusf2802e72006-09-26 10:52:26 +0200420 if (checking_wrmsrl(evntsel_msr, 0UL))
Don Zickus828f0af2006-09-26 10:52:26 +0200421 goto fail2;
Don Zickusf2802e72006-09-26 10:52:26 +0200422 wrmsrl(perfctr_msr, 0UL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 evntsel = K7_EVNTSEL_INT
425 | K7_EVNTSEL_OS
426 | K7_EVNTSEL_USR
427 | K7_NMI_EVENT;
428
Don Zickusf2802e72006-09-26 10:52:26 +0200429 /* setup the timer */
430 wrmsr(evntsel_msr, evntsel, 0);
431 wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 apic_write(APIC_LVTPC, APIC_DM_NMI);
433 evntsel |= K7_EVNTSEL_ENABLE;
Don Zickusf2802e72006-09-26 10:52:26 +0200434 wrmsr(evntsel_msr, evntsel, 0);
435
436 wd->perfctr_msr = perfctr_msr;
437 wd->evntsel_msr = evntsel_msr;
438 wd->cccr_msr = 0; //unused
439 wd->check_bit = 1ULL<<63;
Don Zickus828f0af2006-09-26 10:52:26 +0200440 return 1;
441fail2:
Don Zickusf2802e72006-09-26 10:52:26 +0200442 release_evntsel_nmi(evntsel_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200443fail1:
Don Zickusf2802e72006-09-26 10:52:26 +0200444 release_perfctr_nmi(perfctr_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200445fail:
446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Don Zickusf2802e72006-09-26 10:52:26 +0200449static void stop_k7_watchdog(void)
450{
451 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
452
453 wrmsr(wd->evntsel_msr, 0, 0);
454
455 release_evntsel_nmi(wd->evntsel_msr);
456 release_perfctr_nmi(wd->perfctr_msr);
457}
458
459/* Note that these events don't tick when the CPU idles. This means
460 the frequency varies with CPU load. */
461
462#define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
463#define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
464#define P4_ESCR_OS (1<<3)
465#define P4_ESCR_USR (1<<2)
466#define P4_CCCR_OVF_PMI0 (1<<26)
467#define P4_CCCR_OVF_PMI1 (1<<27)
468#define P4_CCCR_THRESHOLD(N) ((N)<<20)
469#define P4_CCCR_COMPLEMENT (1<<19)
470#define P4_CCCR_COMPARE (1<<18)
471#define P4_CCCR_REQUIRED (3<<16)
472#define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
473#define P4_CCCR_ENABLE (1<<12)
474#define P4_CCCR_OVF (1<<31)
475/* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
476 CRU_ESCR0 (with any non-null event selector) through a complemented
477 max threshold. [IA32-Vol3, Section 14.9.9] */
Andi Kleen75152112005-05-16 21:53:34 -0700478
479static int setup_p4_watchdog(void)
480{
Don Zickusf2802e72006-09-26 10:52:26 +0200481 unsigned int perfctr_msr, evntsel_msr, cccr_msr;
482 unsigned int evntsel, cccr_val;
Andi Kleen75152112005-05-16 21:53:34 -0700483 unsigned int misc_enable, dummy;
Don Zickusf2802e72006-09-26 10:52:26 +0200484 unsigned int ht_num;
485 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Andi Kleen75152112005-05-16 21:53:34 -0700486
Don Zickusf2802e72006-09-26 10:52:26 +0200487 rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy);
Andi Kleen75152112005-05-16 21:53:34 -0700488 if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
489 return 0;
490
Andi Kleen75152112005-05-16 21:53:34 -0700491#ifdef CONFIG_SMP
Don Zickusf2802e72006-09-26 10:52:26 +0200492 /* detect which hyperthread we are on */
493 if (smp_num_siblings == 2) {
494 unsigned int ebx, apicid;
Andi Kleen75152112005-05-16 21:53:34 -0700495
Don Zickusf2802e72006-09-26 10:52:26 +0200496 ebx = cpuid_ebx(1);
497 apicid = (ebx >> 24) & 0xff;
498 ht_num = apicid & 1;
499 } else
500#endif
501 ht_num = 0;
502
503 /* performance counters are shared resources
504 * assign each hyperthread its own set
505 * (re-use the ESCR0 register, seems safe
506 * and keeps the cccr_val the same)
507 */
508 if (!ht_num) {
509 /* logical cpu 0 */
510 perfctr_msr = MSR_P4_IQ_PERFCTR0;
511 evntsel_msr = MSR_P4_CRU_ESCR0;
512 cccr_msr = MSR_P4_IQ_CCCR0;
513 cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4);
514 } else {
515 /* logical cpu 1 */
516 perfctr_msr = MSR_P4_IQ_PERFCTR1;
517 evntsel_msr = MSR_P4_CRU_ESCR0;
518 cccr_msr = MSR_P4_IQ_CCCR1;
519 cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4);
520 }
521
522 if (!reserve_perfctr_nmi(perfctr_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200523 goto fail;
524
Don Zickusf2802e72006-09-26 10:52:26 +0200525 if (!reserve_evntsel_nmi(evntsel_msr))
Don Zickus828f0af2006-09-26 10:52:26 +0200526 goto fail1;
Andi Kleen75152112005-05-16 21:53:34 -0700527
Don Zickusf2802e72006-09-26 10:52:26 +0200528 evntsel = P4_ESCR_EVENT_SELECT(0x3F)
529 | P4_ESCR_OS
530 | P4_ESCR_USR;
531
532 cccr_val |= P4_CCCR_THRESHOLD(15)
533 | P4_CCCR_COMPLEMENT
534 | P4_CCCR_COMPARE
535 | P4_CCCR_REQUIRED;
536
537 wrmsr(evntsel_msr, evntsel, 0);
538 wrmsr(cccr_msr, cccr_val, 0);
539 wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
Andi Kleen75152112005-05-16 21:53:34 -0700540 apic_write(APIC_LVTPC, APIC_DM_NMI);
Don Zickusf2802e72006-09-26 10:52:26 +0200541 cccr_val |= P4_CCCR_ENABLE;
542 wrmsr(cccr_msr, cccr_val, 0);
543
544 wd->perfctr_msr = perfctr_msr;
545 wd->evntsel_msr = evntsel_msr;
546 wd->cccr_msr = cccr_msr;
547 wd->check_bit = 1ULL<<39;
Andi Kleen75152112005-05-16 21:53:34 -0700548 return 1;
Don Zickus828f0af2006-09-26 10:52:26 +0200549fail1:
Don Zickusf2802e72006-09-26 10:52:26 +0200550 release_perfctr_nmi(perfctr_msr);
Don Zickus828f0af2006-09-26 10:52:26 +0200551fail:
552 return 0;
Andi Kleen75152112005-05-16 21:53:34 -0700553}
554
Don Zickusf2802e72006-09-26 10:52:26 +0200555static void stop_p4_watchdog(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Don Zickusf2802e72006-09-26 10:52:26 +0200557 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
Andi Kleen75152112005-05-16 21:53:34 -0700558
Don Zickusf2802e72006-09-26 10:52:26 +0200559 wrmsr(wd->cccr_msr, 0, 0);
560 wrmsr(wd->evntsel_msr, 0, 0);
561
562 release_evntsel_nmi(wd->evntsel_msr);
563 release_perfctr_nmi(wd->perfctr_msr);
564}
565
566void setup_apic_nmi_watchdog(void *unused)
567{
Shaohua Li4038f902006-09-26 10:52:27 +0200568 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
569
Don Zickusf2802e72006-09-26 10:52:26 +0200570 /* only support LOCAL and IO APICs for now */
571 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
572 (nmi_watchdog != NMI_IO_APIC))
573 return;
574
Shaohua Li4038f902006-09-26 10:52:27 +0200575 if (wd->enabled == 1)
576 return;
577
578 /* cheap hack to support suspend/resume */
579 /* if cpu0 is not active neither should the other cpus */
580 if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
581 return;
582
Don Zickusf2802e72006-09-26 10:52:26 +0200583 if (nmi_watchdog == NMI_LOCAL_APIC) {
584 switch (boot_cpu_data.x86_vendor) {
585 case X86_VENDOR_AMD:
586 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
587 return;
588 if (!setup_k7_watchdog())
589 return;
590 break;
591 case X86_VENDOR_INTEL:
592 if (!setup_p4_watchdog())
593 return;
594 break;
595 default:
596 return;
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Shaohua Li4038f902006-09-26 10:52:27 +0200599 wd->enabled = 1;
Don Zickusf2802e72006-09-26 10:52:26 +0200600 atomic_inc(&nmi_active);
601}
602
Shaohua Li4038f902006-09-26 10:52:27 +0200603void stop_apic_nmi_watchdog(void *unused)
Don Zickusf2802e72006-09-26 10:52:26 +0200604{
Shaohua Li4038f902006-09-26 10:52:27 +0200605 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
606
Don Zickusf2802e72006-09-26 10:52:26 +0200607 /* only support LOCAL and IO APICs for now */
608 if ((nmi_watchdog != NMI_LOCAL_APIC) &&
609 (nmi_watchdog != NMI_IO_APIC))
610 return;
611
Shaohua Li4038f902006-09-26 10:52:27 +0200612 if (wd->enabled == 0)
613 return;
614
Don Zickusf2802e72006-09-26 10:52:26 +0200615 if (nmi_watchdog == NMI_LOCAL_APIC) {
616 switch (boot_cpu_data.x86_vendor) {
617 case X86_VENDOR_AMD:
618 if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
619 return;
620 stop_k7_watchdog();
621 break;
622 case X86_VENDOR_INTEL:
623 stop_p4_watchdog();
624 break;
625 default:
626 return;
627 }
628 }
Shaohua Li4038f902006-09-26 10:52:27 +0200629 wd->enabled = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200630 atomic_dec(&nmi_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633/*
634 * the best way to detect whether a CPU has a 'hard lockup' problem
635 * is to check it's local APIC timer IRQ counts. If they are not
636 * changing then that CPU has some problem.
637 *
638 * as these watchdog NMI IRQs are generated on every CPU, we only
639 * have to check the current processor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
641
Andi Kleen75152112005-05-16 21:53:34 -0700642static DEFINE_PER_CPU(unsigned, last_irq_sum);
643static DEFINE_PER_CPU(local_t, alert_counter);
644static DEFINE_PER_CPU(int, nmi_touch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646void touch_nmi_watchdog (void)
647{
Jan Beulich99019e92006-02-16 23:41:55 +0100648 if (nmi_watchdog > 0) {
649 unsigned cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Jan Beulich99019e92006-02-16 23:41:55 +0100651 /*
652 * Tell other CPUs to reset their alert counters. We cannot
653 * do it ourselves because the alert count increase is not
654 * atomic.
655 */
656 for_each_present_cpu (cpu)
657 per_cpu(nmi_touch, cpu) = 1;
658 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700659
660 touch_softlockup_watchdog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Don Zickus3adbbcc2006-09-26 10:52:26 +0200663int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Andi Kleen75152112005-05-16 21:53:34 -0700665 int sum;
666 int touched = 0;
Don Zickusf2802e72006-09-26 10:52:26 +0200667 struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
668 u64 dummy;
Don Zickus3adbbcc2006-09-26 10:52:26 +0200669 int rc=0;
Don Zickusf2802e72006-09-26 10:52:26 +0200670
671 /* check for other users first */
672 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
673 == NOTIFY_STOP) {
Don Zickus3adbbcc2006-09-26 10:52:26 +0200674 rc = 1;
Don Zickusf2802e72006-09-26 10:52:26 +0200675 touched = 1;
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 sum = read_pda(apic_timer_irqs);
Andi Kleen75152112005-05-16 21:53:34 -0700679 if (__get_cpu_var(nmi_touch)) {
680 __get_cpu_var(nmi_touch) = 0;
681 touched = 1;
682 }
Don Zickusf2802e72006-09-26 10:52:26 +0200683
Andi Kleen553f2652006-04-07 19:49:57 +0200684#ifdef CONFIG_X86_MCE
685 /* Could check oops_in_progress here too, but it's safer
686 not too */
687 if (atomic_read(&mce_entry) > 0)
688 touched = 1;
689#endif
Don Zickusf2802e72006-09-26 10:52:26 +0200690 /* if the apic timer isn't firing, this cpu isn't doing much */
Andi Kleen75152112005-05-16 21:53:34 -0700691 if (!touched && __get_cpu_var(last_irq_sum) == sum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /*
693 * Ayiee, looks like this CPU is stuck ...
694 * wait a few IRQs (5 seconds) before doing the oops ...
695 */
Andi Kleen75152112005-05-16 21:53:34 -0700696 local_inc(&__get_cpu_var(alert_counter));
Don Zickusf2802e72006-09-26 10:52:26 +0200697 if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
Andi Kleenfac58552006-09-26 10:52:27 +0200698 die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs,
699 panic_on_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 } else {
Andi Kleen75152112005-05-16 21:53:34 -0700701 __get_cpu_var(last_irq_sum) = sum;
702 local_set(&__get_cpu_var(alert_counter), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Don Zickusf2802e72006-09-26 10:52:26 +0200704
705 /* see if the nmi watchdog went off */
706 if (wd->enabled) {
707 if (nmi_watchdog == NMI_LOCAL_APIC) {
708 rdmsrl(wd->perfctr_msr, dummy);
709 if (dummy & wd->check_bit){
710 /* this wasn't a watchdog timer interrupt */
711 goto done;
712 }
713
714 /* only Intel uses the cccr msr */
715 if (wd->cccr_msr != 0) {
716 /*
717 * P4 quirks:
718 * - An overflown perfctr will assert its interrupt
719 * until the OVF flag in its CCCR is cleared.
720 * - LVTPC is masked on interrupt and must be
721 * unmasked by the LVTPC handler.
722 */
723 rdmsrl(wd->cccr_msr, dummy);
724 dummy &= ~P4_CCCR_OVF;
725 wrmsrl(wd->cccr_msr, dummy);
726 apic_write(APIC_LVTPC, APIC_DM_NMI);
727 }
728 /* start the cycle over again */
729 wrmsrl(wd->perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
Don Zickus3adbbcc2006-09-26 10:52:26 +0200730 rc = 1;
731 } else if (nmi_watchdog == NMI_IO_APIC) {
732 /* don't know how to accurately check for this.
733 * just assume it was a watchdog timer interrupt
734 * This matches the old behaviour.
735 */
736 rc = 1;
737 } else
738 printk(KERN_WARNING "Unknown enabled NMI hardware?!\n");
Andi Kleen75152112005-05-16 21:53:34 -0700739 }
Don Zickusf2802e72006-09-26 10:52:26 +0200740done:
Don Zickus3adbbcc2006-09-26 10:52:26 +0200741 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Andi Kleeneddb6fb2006-02-03 21:50:41 +0100744asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 nmi_enter();
747 add_pda(__nmi_count,1);
Don Zickus3adbbcc2006-09-26 10:52:26 +0200748 default_do_nmi(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 nmi_exit();
750}
751
Don Zickus3adbbcc2006-09-26 10:52:26 +0200752int do_nmi_callback(struct pt_regs * regs, int cpu)
753{
Don Zickus2fbe7b22006-09-26 10:52:27 +0200754#ifdef CONFIG_SYSCTL
755 if (unknown_nmi_panic)
756 return unknown_nmi_panic_callback(regs, cpu);
757#endif
758 return 0;
Don Zickus3adbbcc2006-09-26 10:52:26 +0200759}
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761#ifdef CONFIG_SYSCTL
762
763static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
764{
765 unsigned char reason = get_nmi_reason();
766 char buf[64];
767
Don Zickus2fbe7b22006-09-26 10:52:27 +0200768 sprintf(buf, "NMI received for unknown reason %02x\n", reason);
Andi Kleenfac58552006-09-26 10:52:27 +0200769 die_nmi(buf, regs, 1); /* Always panic here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return 0;
771}
772
Don Zickus407984f2006-09-26 10:52:27 +0200773/*
774 * proc handler for /proc/sys/kernel/nmi
775 */
776int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
777 void __user *buffer, size_t *length, loff_t *ppos)
778{
779 int old_state;
780
781 nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
782 old_state = nmi_watchdog_enabled;
783 proc_dointvec(table, write, file, buffer, length, ppos);
784 if (!!old_state == !!nmi_watchdog_enabled)
785 return 0;
786
787 if (atomic_read(&nmi_active) < 0) {
788 printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
Don Zickuse33e89a2006-09-26 10:52:27 +0200789 return -EIO;
Don Zickus407984f2006-09-26 10:52:27 +0200790 }
791
792 /* if nmi_watchdog is not set yet, then set it */
793 nmi_watchdog_default();
794
Don Zickuse33e89a2006-09-26 10:52:27 +0200795 if (nmi_watchdog == NMI_LOCAL_APIC) {
Don Zickus407984f2006-09-26 10:52:27 +0200796 if (nmi_watchdog_enabled)
797 enable_lapic_nmi_watchdog();
798 else
799 disable_lapic_nmi_watchdog();
Don Zickus407984f2006-09-26 10:52:27 +0200800 } else {
Don Zickuse33e89a2006-09-26 10:52:27 +0200801 printk( KERN_WARNING
Don Zickus407984f2006-09-26 10:52:27 +0200802 "NMI watchdog doesn't know what hardware to touch\n");
803 return -EIO;
804 }
805 return 0;
806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808#endif
809
810EXPORT_SYMBOL(nmi_active);
811EXPORT_SYMBOL(nmi_watchdog);
Don Zickus828f0af2006-09-26 10:52:26 +0200812EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi);
813EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
814EXPORT_SYMBOL(reserve_perfctr_nmi);
815EXPORT_SYMBOL(release_perfctr_nmi);
816EXPORT_SYMBOL(reserve_evntsel_nmi);
817EXPORT_SYMBOL(release_evntsel_nmi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818EXPORT_SYMBOL(disable_timer_nmi_watchdog);
819EXPORT_SYMBOL(enable_timer_nmi_watchdog);
820EXPORT_SYMBOL(touch_nmi_watchdog);