Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/i386/nmi.c |
| 3 | * |
| 4 | * NMI watchdog support on APIC systems |
| 5 | * |
| 6 | * Started by Ingo Molnar <mingo@redhat.com> |
| 7 | * |
| 8 | * Fixes: |
| 9 | * Mikael Pettersson : AMD K7 support for local APIC NMI watchdog. |
| 10 | * Mikael Pettersson : Power Management for local APIC NMI watchdog. |
| 11 | * Mikael Pettersson : Pentium 4 support for local APIC NMI watchdog. |
| 12 | * Pavel Machek and |
| 13 | * Mikael Pettersson : PM converted to driver model. Disable/enable API. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/config.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/nmi.h> |
| 21 | #include <linux/sysdev.h> |
| 22 | #include <linux/sysctl.h> |
Don Zickus | 3e4ff11 | 2006-06-26 13:57:01 +0200 | [diff] [blame] | 23 | #include <linux/percpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include <asm/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/nmi.h> |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 27 | #include <asm/kdebug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include "mach_traps.h" |
| 30 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 31 | /* perfctr_nmi_owner tracks the ownership of the perfctr registers: |
| 32 | * evtsel_nmi_owner tracks the ownership of the event selection |
| 33 | * - different performance counters/ event selection may be reserved for |
| 34 | * different subsystems this reservation system just tries to coordinate |
| 35 | * things a little |
| 36 | */ |
| 37 | static DEFINE_PER_CPU(unsigned long, perfctr_nmi_owner); |
| 38 | static DEFINE_PER_CPU(unsigned long, evntsel_nmi_owner[3]); |
| 39 | |
| 40 | /* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's |
| 41 | * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now) |
| 42 | */ |
| 43 | #define NMI_MAX_COUNTER_BITS 66 |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | /* |
| 46 | * lapic_nmi_owner tracks the ownership of the lapic NMI hardware: |
| 47 | * - it may be reserved by some other driver, or not |
| 48 | * - when not reserved by some other driver, it may be used for |
| 49 | * the NMI watchdog, or not |
| 50 | * |
| 51 | * This is maintained separately from nmi_active because the NMI |
| 52 | * watchdog may also be driven from the I/O APIC timer. |
| 53 | */ |
| 54 | static DEFINE_SPINLOCK(lapic_nmi_owner_lock); |
| 55 | static unsigned int lapic_nmi_owner; |
| 56 | #define LAPIC_NMI_WATCHDOG (1<<0) |
| 57 | #define LAPIC_NMI_RESERVED (1<<1) |
| 58 | |
| 59 | /* nmi_active: |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 60 | * >0: the lapic NMI watchdog is active, but can be disabled |
| 61 | * <0: the lapic NMI watchdog has not been set up, and cannot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | * be enabled |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 63 | * 0: the lapic NMI watchdog is disabled, but can be enabled |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | */ |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 65 | atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 67 | unsigned int nmi_watchdog = NMI_DEFAULT; |
| 68 | static unsigned int nmi_hz = HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 70 | struct 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 | }; |
| 77 | static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 79 | /* local prototypes */ |
| 80 | static void stop_apic_nmi_watchdog(void *unused); |
| 81 | static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu); |
| 82 | |
| 83 | extern void show_registers(struct pt_regs *regs); |
| 84 | extern int unknown_nmi_panic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 86 | /* converts an msr to an appropriate reservation bit */ |
| 87 | static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr) |
| 88 | { |
| 89 | /* returns the bit offset of the performance counter register */ |
| 90 | switch (boot_cpu_data.x86_vendor) { |
| 91 | case X86_VENDOR_AMD: |
| 92 | return (msr - MSR_K7_PERFCTR0); |
| 93 | case X86_VENDOR_INTEL: |
| 94 | switch (boot_cpu_data.x86) { |
| 95 | case 6: |
| 96 | return (msr - MSR_P6_PERFCTR0); |
| 97 | case 15: |
| 98 | return (msr - MSR_P4_BPU_PERFCTR0); |
| 99 | } |
| 100 | } |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | /* converts an msr to an appropriate reservation bit */ |
| 105 | static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr) |
| 106 | { |
| 107 | /* returns the bit offset of the event selection register */ |
| 108 | switch (boot_cpu_data.x86_vendor) { |
| 109 | case X86_VENDOR_AMD: |
| 110 | return (msr - MSR_K7_EVNTSEL0); |
| 111 | case X86_VENDOR_INTEL: |
| 112 | switch (boot_cpu_data.x86) { |
| 113 | case 6: |
| 114 | return (msr - MSR_P6_EVNTSEL0); |
| 115 | case 15: |
| 116 | return (msr - MSR_P4_BSU_ESCR0); |
| 117 | } |
| 118 | } |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | /* checks for a bit availability (hack for oprofile) */ |
| 123 | int avail_to_resrv_perfctr_nmi_bit(unsigned int counter) |
| 124 | { |
| 125 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 126 | |
| 127 | return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner))); |
| 128 | } |
| 129 | |
| 130 | /* checks the an msr for availability */ |
| 131 | int avail_to_resrv_perfctr_nmi(unsigned int msr) |
| 132 | { |
| 133 | unsigned int counter; |
| 134 | |
| 135 | counter = nmi_perfctr_msr_to_bit(msr); |
| 136 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 137 | |
| 138 | return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner))); |
| 139 | } |
| 140 | |
| 141 | int reserve_perfctr_nmi(unsigned int msr) |
| 142 | { |
| 143 | unsigned int counter; |
| 144 | |
| 145 | counter = nmi_perfctr_msr_to_bit(msr); |
| 146 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 147 | |
| 148 | if (!test_and_set_bit(counter, &__get_cpu_var(perfctr_nmi_owner))) |
| 149 | return 1; |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | void release_perfctr_nmi(unsigned int msr) |
| 154 | { |
| 155 | unsigned int counter; |
| 156 | |
| 157 | counter = nmi_perfctr_msr_to_bit(msr); |
| 158 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 159 | |
| 160 | clear_bit(counter, &__get_cpu_var(perfctr_nmi_owner)); |
| 161 | } |
| 162 | |
| 163 | int reserve_evntsel_nmi(unsigned int msr) |
| 164 | { |
| 165 | unsigned int counter; |
| 166 | |
| 167 | counter = nmi_evntsel_msr_to_bit(msr); |
| 168 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 169 | |
| 170 | if (!test_and_set_bit(counter, &__get_cpu_var(evntsel_nmi_owner)[0])) |
| 171 | return 1; |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | void release_evntsel_nmi(unsigned int msr) |
| 176 | { |
| 177 | unsigned int counter; |
| 178 | |
| 179 | counter = nmi_evntsel_msr_to_bit(msr); |
| 180 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 181 | |
| 182 | clear_bit(counter, &__get_cpu_var(evntsel_nmi_owner)[0]); |
| 183 | } |
| 184 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 185 | static __cpuinit inline int nmi_known_cpu(void) |
| 186 | { |
| 187 | switch (boot_cpu_data.x86_vendor) { |
| 188 | case X86_VENDOR_AMD: |
| 189 | return ((boot_cpu_data.x86 == 15) || (boot_cpu_data.x86 == 6)); |
| 190 | case X86_VENDOR_INTEL: |
| 191 | return ((boot_cpu_data.x86 == 15) || (boot_cpu_data.x86 == 6)); |
| 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 196 | #ifdef CONFIG_SMP |
| 197 | /* The performance counters used by NMI_LOCAL_APIC don't trigger when |
| 198 | * the CPU is idle. To make sure the NMI watchdog really ticks on all |
| 199 | * CPUs during the test make them busy. |
| 200 | */ |
| 201 | static __init void nmi_cpu_busy(void *data) |
| 202 | { |
| 203 | volatile int *endflag = data; |
Ingo Molnar | 366c7f5 | 2006-07-03 00:25:25 -0700 | [diff] [blame] | 204 | local_irq_enable_in_hardirq(); |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 205 | /* Intentionally don't use cpu_relax here. This is |
| 206 | to make sure that the performance counter really ticks, |
| 207 | even if there is a simulator or similar that catches the |
| 208 | pause instruction. On a real HT machine this is fine because |
| 209 | all other CPUs are busy with "useless" delay loops and don't |
| 210 | care if they get somewhat less cycles. */ |
| 211 | while (*endflag == 0) |
| 212 | barrier(); |
| 213 | } |
| 214 | #endif |
| 215 | |
Jack F Vogel | 67701ae | 2005-05-01 08:58:48 -0700 | [diff] [blame] | 216 | static int __init check_nmi_watchdog(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 218 | volatile int endflag = 0; |
| 219 | unsigned int *prev_nmi_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | int cpu; |
| 221 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 222 | if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT)) |
| 223 | return 0; |
| 224 | |
| 225 | if (!atomic_read(&nmi_active)) |
Jack F Vogel | 67701ae | 2005-05-01 08:58:48 -0700 | [diff] [blame] | 226 | return 0; |
| 227 | |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 228 | prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL); |
| 229 | if (!prev_nmi_count) |
| 230 | return -1; |
| 231 | |
Jack F Vogel | 67701ae | 2005-05-01 08:58:48 -0700 | [diff] [blame] | 232 | printk(KERN_INFO "Testing NMI watchdog ... "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 234 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 235 | smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0); |
| 236 | |
KAMEZAWA Hiroyuki | c891259 | 2006-03-28 01:56:39 -0800 | [diff] [blame] | 237 | for_each_possible_cpu(cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | prev_nmi_count[cpu] = per_cpu(irq_stat, cpu).__nmi_count; |
| 239 | local_irq_enable(); |
| 240 | mdelay((10*1000)/nmi_hz); // wait 10 ticks |
| 241 | |
KAMEZAWA Hiroyuki | c891259 | 2006-03-28 01:56:39 -0800 | [diff] [blame] | 242 | for_each_possible_cpu(cpu) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | #ifdef CONFIG_SMP |
| 244 | /* Check cpu_callin_map here because that is set |
| 245 | after the timer is started. */ |
| 246 | if (!cpu_isset(cpu, cpu_callin_map)) |
| 247 | continue; |
| 248 | #endif |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 249 | if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled) |
| 250 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) { |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 252 | printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n", |
| 253 | cpu, |
| 254 | prev_nmi_count[cpu], |
| 255 | nmi_count(cpu)); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 256 | per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0; |
| 257 | atomic_dec(&nmi_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 260 | if (!atomic_read(&nmi_active)) { |
| 261 | kfree(prev_nmi_count); |
| 262 | atomic_set(&nmi_active, -1); |
| 263 | return -1; |
| 264 | } |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 265 | endflag = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | printk("OK.\n"); |
| 267 | |
| 268 | /* now that we know it works we can reduce NMI frequency to |
| 269 | something more reasonable; makes a difference in some configs */ |
| 270 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 271 | nmi_hz = 1; |
| 272 | |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 273 | kfree(prev_nmi_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | return 0; |
| 275 | } |
Jack F Vogel | 67701ae | 2005-05-01 08:58:48 -0700 | [diff] [blame] | 276 | /* This needs to happen later in boot so counters are working */ |
| 277 | late_initcall(check_nmi_watchdog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
| 279 | static int __init setup_nmi_watchdog(char *str) |
| 280 | { |
| 281 | int nmi; |
| 282 | |
| 283 | get_option(&str, &nmi); |
| 284 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 285 | if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | /* |
| 288 | * If any other x86 CPU has a local APIC, then |
| 289 | * please test the NMI stuff there and send me the |
| 290 | * missing bits. Right now Intel P6/P4 and AMD K7 only. |
| 291 | */ |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 292 | if ((nmi == NMI_LOCAL_APIC) && (nmi_known_cpu() == 0)) |
| 293 | return 0; /* no lapic support */ |
| 294 | nmi_watchdog = nmi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | return 1; |
| 296 | } |
| 297 | |
| 298 | __setup("nmi_watchdog=", setup_nmi_watchdog); |
| 299 | |
| 300 | static void disable_lapic_nmi_watchdog(void) |
| 301 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 302 | BUG_ON(nmi_watchdog != NMI_LOCAL_APIC); |
| 303 | |
| 304 | if (atomic_read(&nmi_active) <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 307 | on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 309 | BUG_ON(atomic_read(&nmi_active) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static void enable_lapic_nmi_watchdog(void) |
| 313 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 314 | BUG_ON(nmi_watchdog != NMI_LOCAL_APIC); |
| 315 | |
| 316 | /* are we already enabled */ |
| 317 | if (atomic_read(&nmi_active) != 0) |
| 318 | return; |
| 319 | |
| 320 | /* are we lapic aware */ |
| 321 | if (nmi_known_cpu() <= 0) |
| 322 | return; |
| 323 | |
| 324 | on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1); |
| 325 | touch_nmi_watchdog(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | int reserve_lapic_nmi(void) |
| 329 | { |
| 330 | unsigned int old_owner; |
| 331 | |
| 332 | spin_lock(&lapic_nmi_owner_lock); |
| 333 | old_owner = lapic_nmi_owner; |
| 334 | lapic_nmi_owner |= LAPIC_NMI_RESERVED; |
| 335 | spin_unlock(&lapic_nmi_owner_lock); |
| 336 | if (old_owner & LAPIC_NMI_RESERVED) |
| 337 | return -EBUSY; |
| 338 | if (old_owner & LAPIC_NMI_WATCHDOG) |
| 339 | disable_lapic_nmi_watchdog(); |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | void release_lapic_nmi(void) |
| 344 | { |
| 345 | unsigned int new_owner; |
| 346 | |
| 347 | spin_lock(&lapic_nmi_owner_lock); |
| 348 | new_owner = lapic_nmi_owner & ~LAPIC_NMI_RESERVED; |
| 349 | lapic_nmi_owner = new_owner; |
| 350 | spin_unlock(&lapic_nmi_owner_lock); |
| 351 | if (new_owner & LAPIC_NMI_WATCHDOG) |
| 352 | enable_lapic_nmi_watchdog(); |
| 353 | } |
| 354 | |
| 355 | void disable_timer_nmi_watchdog(void) |
| 356 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 357 | BUG_ON(nmi_watchdog != NMI_IO_APIC); |
| 358 | |
| 359 | if (atomic_read(&nmi_active) <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | return; |
| 361 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 362 | disable_irq(0); |
| 363 | on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1); |
| 364 | |
| 365 | BUG_ON(atomic_read(&nmi_active) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void enable_timer_nmi_watchdog(void) |
| 369 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 370 | BUG_ON(nmi_watchdog != NMI_IO_APIC); |
| 371 | |
| 372 | if (atomic_read(&nmi_active) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | touch_nmi_watchdog(); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 374 | on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1); |
| 375 | enable_irq(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | #ifdef CONFIG_PM |
| 380 | |
| 381 | static int nmi_pm_active; /* nmi_active before suspend */ |
| 382 | |
Pavel Machek | 438510f | 2005-04-16 15:25:24 -0700 | [diff] [blame] | 383 | static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 385 | nmi_pm_active = atomic_read(&nmi_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | disable_lapic_nmi_watchdog(); |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int lapic_nmi_resume(struct sys_device *dev) |
| 391 | { |
| 392 | if (nmi_pm_active > 0) |
| 393 | enable_lapic_nmi_watchdog(); |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | |
| 398 | static struct sysdev_class nmi_sysclass = { |
| 399 | set_kset_name("lapic_nmi"), |
| 400 | .resume = lapic_nmi_resume, |
| 401 | .suspend = lapic_nmi_suspend, |
| 402 | }; |
| 403 | |
| 404 | static struct sys_device device_lapic_nmi = { |
| 405 | .id = 0, |
| 406 | .cls = &nmi_sysclass, |
| 407 | }; |
| 408 | |
| 409 | static int __init init_lapic_nmi_sysfs(void) |
| 410 | { |
| 411 | int error; |
| 412 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 413 | /* should really be a BUG_ON but b/c this is an |
| 414 | * init call, it just doesn't work. -dcz |
| 415 | */ |
| 416 | if (nmi_watchdog != NMI_LOCAL_APIC) |
| 417 | return 0; |
| 418 | |
| 419 | if ( atomic_read(&nmi_active) < 0 ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | return 0; |
| 421 | |
| 422 | error = sysdev_class_register(&nmi_sysclass); |
| 423 | if (!error) |
| 424 | error = sysdev_register(&device_lapic_nmi); |
| 425 | return error; |
| 426 | } |
| 427 | /* must come after the local APIC's device_initcall() */ |
| 428 | late_initcall(init_lapic_nmi_sysfs); |
| 429 | |
| 430 | #endif /* CONFIG_PM */ |
| 431 | |
| 432 | /* |
| 433 | * Activate the NMI watchdog via the local APIC. |
| 434 | * Original code written by Keith Owens. |
| 435 | */ |
| 436 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 437 | static void write_watchdog_counter(unsigned int perfctr_msr, const char *descr) |
Jan Beulich | 7fbb4f6 | 2005-06-23 00:08:23 -0700 | [diff] [blame] | 438 | { |
| 439 | u64 count = (u64)cpu_khz * 1000; |
| 440 | |
| 441 | do_div(count, nmi_hz); |
| 442 | if(descr) |
| 443 | Dprintk("setting %s to -0x%08Lx\n", descr, count); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 444 | wrmsrl(perfctr_msr, 0 - count); |
Jan Beulich | 7fbb4f6 | 2005-06-23 00:08:23 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 447 | /* Note that these events don't tick when the CPU idles. This means |
| 448 | the frequency varies with CPU load. */ |
| 449 | |
| 450 | #define K7_EVNTSEL_ENABLE (1 << 22) |
| 451 | #define K7_EVNTSEL_INT (1 << 20) |
| 452 | #define K7_EVNTSEL_OS (1 << 17) |
| 453 | #define K7_EVNTSEL_USR (1 << 16) |
| 454 | #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76 |
| 455 | #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING |
| 456 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 457 | static int setup_k7_watchdog(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 459 | unsigned int perfctr_msr, evntsel_msr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | unsigned int evntsel; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 461 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 463 | perfctr_msr = MSR_K7_PERFCTR0; |
| 464 | evntsel_msr = MSR_K7_EVNTSEL0; |
| 465 | if (!reserve_perfctr_nmi(perfctr_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 466 | goto fail; |
| 467 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 468 | if (!reserve_evntsel_nmi(evntsel_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 469 | goto fail1; |
| 470 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 471 | wrmsrl(perfctr_msr, 0UL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
| 473 | evntsel = K7_EVNTSEL_INT |
| 474 | | K7_EVNTSEL_OS |
| 475 | | K7_EVNTSEL_USR |
| 476 | | K7_NMI_EVENT; |
| 477 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 478 | /* setup the timer */ |
| 479 | wrmsr(evntsel_msr, evntsel, 0); |
| 480 | write_watchdog_counter(perfctr_msr, "K7_PERFCTR0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 482 | evntsel |= K7_EVNTSEL_ENABLE; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 483 | wrmsr(evntsel_msr, evntsel, 0); |
| 484 | |
| 485 | wd->perfctr_msr = perfctr_msr; |
| 486 | wd->evntsel_msr = evntsel_msr; |
| 487 | wd->cccr_msr = 0; //unused |
| 488 | wd->check_bit = 1ULL<<63; |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 489 | return 1; |
| 490 | fail1: |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 491 | release_perfctr_nmi(perfctr_msr); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 492 | fail: |
| 493 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 496 | static void stop_k7_watchdog(void) |
| 497 | { |
| 498 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
| 499 | |
| 500 | wrmsr(wd->evntsel_msr, 0, 0); |
| 501 | |
| 502 | release_evntsel_nmi(wd->evntsel_msr); |
| 503 | release_perfctr_nmi(wd->perfctr_msr); |
| 504 | } |
| 505 | |
| 506 | #define P6_EVNTSEL0_ENABLE (1 << 22) |
| 507 | #define P6_EVNTSEL_INT (1 << 20) |
| 508 | #define P6_EVNTSEL_OS (1 << 17) |
| 509 | #define P6_EVNTSEL_USR (1 << 16) |
| 510 | #define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79 |
| 511 | #define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED |
| 512 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 513 | static int setup_p6_watchdog(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 515 | unsigned int perfctr_msr, evntsel_msr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | unsigned int evntsel; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 517 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 519 | perfctr_msr = MSR_P6_PERFCTR0; |
| 520 | evntsel_msr = MSR_P6_EVNTSEL0; |
| 521 | if (!reserve_perfctr_nmi(perfctr_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 522 | goto fail; |
| 523 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 524 | if (!reserve_evntsel_nmi(evntsel_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 525 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 527 | wrmsrl(perfctr_msr, 0UL); |
| 528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | evntsel = P6_EVNTSEL_INT |
| 530 | | P6_EVNTSEL_OS |
| 531 | | P6_EVNTSEL_USR |
| 532 | | P6_NMI_EVENT; |
| 533 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 534 | /* setup the timer */ |
| 535 | wrmsr(evntsel_msr, evntsel, 0); |
| 536 | write_watchdog_counter(perfctr_msr, "P6_PERFCTR0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 538 | evntsel |= P6_EVNTSEL0_ENABLE; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 539 | wrmsr(evntsel_msr, evntsel, 0); |
| 540 | |
| 541 | wd->perfctr_msr = perfctr_msr; |
| 542 | wd->evntsel_msr = evntsel_msr; |
| 543 | wd->cccr_msr = 0; //unused |
| 544 | wd->check_bit = 1ULL<<39; |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 545 | return 1; |
| 546 | fail1: |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 547 | release_perfctr_nmi(perfctr_msr); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 548 | fail: |
| 549 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 552 | static void stop_p6_watchdog(void) |
| 553 | { |
| 554 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
| 555 | |
| 556 | wrmsr(wd->evntsel_msr, 0, 0); |
| 557 | |
| 558 | release_evntsel_nmi(wd->evntsel_msr); |
| 559 | release_perfctr_nmi(wd->perfctr_msr); |
| 560 | } |
| 561 | |
| 562 | /* Note that these events don't tick when the CPU idles. This means |
| 563 | the frequency varies with CPU load. */ |
| 564 | |
| 565 | #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7) |
| 566 | #define P4_ESCR_EVENT_SELECT(N) ((N)<<25) |
| 567 | #define P4_ESCR_OS (1<<3) |
| 568 | #define P4_ESCR_USR (1<<2) |
| 569 | #define P4_CCCR_OVF_PMI0 (1<<26) |
| 570 | #define P4_CCCR_OVF_PMI1 (1<<27) |
| 571 | #define P4_CCCR_THRESHOLD(N) ((N)<<20) |
| 572 | #define P4_CCCR_COMPLEMENT (1<<19) |
| 573 | #define P4_CCCR_COMPARE (1<<18) |
| 574 | #define P4_CCCR_REQUIRED (3<<16) |
| 575 | #define P4_CCCR_ESCR_SELECT(N) ((N)<<13) |
| 576 | #define P4_CCCR_ENABLE (1<<12) |
| 577 | #define P4_CCCR_OVF (1<<31) |
| 578 | /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter |
| 579 | CRU_ESCR0 (with any non-null event selector) through a complemented |
| 580 | max threshold. [IA32-Vol3, Section 14.9.9] */ |
| 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | static int setup_p4_watchdog(void) |
| 583 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 584 | unsigned int perfctr_msr, evntsel_msr, cccr_msr; |
| 585 | unsigned int evntsel, cccr_val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | unsigned int misc_enable, dummy; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 587 | unsigned int ht_num; |
| 588 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 590 | rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL)) |
| 592 | return 0; |
| 593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | #ifdef CONFIG_SMP |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 595 | /* detect which hyperthread we are on */ |
| 596 | if (smp_num_siblings == 2) { |
| 597 | unsigned int ebx, apicid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 599 | ebx = cpuid_ebx(1); |
| 600 | apicid = (ebx >> 24) & 0xff; |
| 601 | ht_num = apicid & 1; |
| 602 | } else |
| 603 | #endif |
| 604 | ht_num = 0; |
| 605 | |
| 606 | /* performance counters are shared resources |
| 607 | * assign each hyperthread its own set |
| 608 | * (re-use the ESCR0 register, seems safe |
| 609 | * and keeps the cccr_val the same) |
| 610 | */ |
| 611 | if (!ht_num) { |
| 612 | /* logical cpu 0 */ |
| 613 | perfctr_msr = MSR_P4_IQ_PERFCTR0; |
| 614 | evntsel_msr = MSR_P4_CRU_ESCR0; |
| 615 | cccr_msr = MSR_P4_IQ_CCCR0; |
| 616 | cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4); |
| 617 | } else { |
| 618 | /* logical cpu 1 */ |
| 619 | perfctr_msr = MSR_P4_IQ_PERFCTR1; |
| 620 | evntsel_msr = MSR_P4_CRU_ESCR0; |
| 621 | cccr_msr = MSR_P4_IQ_CCCR1; |
| 622 | cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4); |
| 623 | } |
| 624 | |
| 625 | if (!reserve_perfctr_nmi(perfctr_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 626 | goto fail; |
| 627 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 628 | if (!reserve_evntsel_nmi(evntsel_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 629 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 631 | evntsel = P4_ESCR_EVENT_SELECT(0x3F) |
| 632 | | P4_ESCR_OS |
| 633 | | P4_ESCR_USR; |
| 634 | |
| 635 | cccr_val |= P4_CCCR_THRESHOLD(15) |
| 636 | | P4_CCCR_COMPLEMENT |
| 637 | | P4_CCCR_COMPARE |
| 638 | | P4_CCCR_REQUIRED; |
| 639 | |
| 640 | wrmsr(evntsel_msr, evntsel, 0); |
| 641 | wrmsr(cccr_msr, cccr_val, 0); |
| 642 | write_watchdog_counter(perfctr_msr, "P4_IQ_COUNTER0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 644 | cccr_val |= P4_CCCR_ENABLE; |
| 645 | wrmsr(cccr_msr, cccr_val, 0); |
| 646 | wd->perfctr_msr = perfctr_msr; |
| 647 | wd->evntsel_msr = evntsel_msr; |
| 648 | wd->cccr_msr = cccr_msr; |
| 649 | wd->check_bit = 1ULL<<39; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | return 1; |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 651 | fail1: |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 652 | release_perfctr_nmi(perfctr_msr); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 653 | fail: |
| 654 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 657 | static void stop_p4_watchdog(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 659 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 661 | wrmsr(wd->cccr_msr, 0, 0); |
| 662 | wrmsr(wd->evntsel_msr, 0, 0); |
| 663 | |
| 664 | release_evntsel_nmi(wd->evntsel_msr); |
| 665 | release_perfctr_nmi(wd->perfctr_msr); |
| 666 | } |
| 667 | |
| 668 | void setup_apic_nmi_watchdog (void *unused) |
| 669 | { |
| 670 | /* only support LOCAL and IO APICs for now */ |
| 671 | if ((nmi_watchdog != NMI_LOCAL_APIC) && |
| 672 | (nmi_watchdog != NMI_IO_APIC)) |
| 673 | return; |
| 674 | |
| 675 | if (nmi_watchdog == NMI_LOCAL_APIC) { |
| 676 | switch (boot_cpu_data.x86_vendor) { |
| 677 | case X86_VENDOR_AMD: |
| 678 | if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15) |
| 679 | return; |
| 680 | if (!setup_k7_watchdog()) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 681 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | break; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 683 | case X86_VENDOR_INTEL: |
| 684 | switch (boot_cpu_data.x86) { |
| 685 | case 6: |
| 686 | if (boot_cpu_data.x86_model > 0xd) |
| 687 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 689 | if (!setup_p6_watchdog()) |
| 690 | return; |
| 691 | break; |
| 692 | case 15: |
| 693 | if (boot_cpu_data.x86_model > 0x4) |
| 694 | return; |
| 695 | |
| 696 | if (!setup_p4_watchdog()) |
| 697 | return; |
| 698 | break; |
| 699 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | return; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 701 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | break; |
| 703 | default: |
| 704 | return; |
| 705 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | } |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 707 | __get_cpu_var(nmi_watchdog_ctlblk.enabled) = 1; |
| 708 | atomic_inc(&nmi_active); |
| 709 | } |
| 710 | |
| 711 | static void stop_apic_nmi_watchdog(void *unused) |
| 712 | { |
| 713 | /* only support LOCAL and IO APICs for now */ |
| 714 | if ((nmi_watchdog != NMI_LOCAL_APIC) && |
| 715 | (nmi_watchdog != NMI_IO_APIC)) |
| 716 | return; |
| 717 | |
| 718 | if (nmi_watchdog == NMI_LOCAL_APIC) { |
| 719 | switch (boot_cpu_data.x86_vendor) { |
| 720 | case X86_VENDOR_AMD: |
| 721 | stop_k7_watchdog(); |
| 722 | break; |
| 723 | case X86_VENDOR_INTEL: |
| 724 | switch (boot_cpu_data.x86) { |
| 725 | case 6: |
| 726 | if (boot_cpu_data.x86_model > 0xd) |
| 727 | break; |
| 728 | stop_p6_watchdog(); |
| 729 | break; |
| 730 | case 15: |
| 731 | if (boot_cpu_data.x86_model > 0x4) |
| 732 | break; |
| 733 | stop_p4_watchdog(); |
| 734 | break; |
| 735 | } |
| 736 | break; |
| 737 | default: |
| 738 | return; |
| 739 | } |
| 740 | } |
| 741 | __get_cpu_var(nmi_watchdog_ctlblk.enabled) = 0; |
| 742 | atomic_dec(&nmi_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /* |
| 746 | * the best way to detect whether a CPU has a 'hard lockup' problem |
| 747 | * is to check it's local APIC timer IRQ counts. If they are not |
| 748 | * changing then that CPU has some problem. |
| 749 | * |
| 750 | * as these watchdog NMI IRQs are generated on every CPU, we only |
| 751 | * have to check the current processor. |
| 752 | * |
| 753 | * since NMIs don't listen to _any_ locks, we have to be extremely |
| 754 | * careful not to rely on unsafe variables. The printk might lock |
| 755 | * up though, so we have to break up any console locks first ... |
| 756 | * [when there will be more tty-related locks, break them up |
| 757 | * here too!] |
| 758 | */ |
| 759 | |
| 760 | static unsigned int |
| 761 | last_irq_sums [NR_CPUS], |
| 762 | alert_counter [NR_CPUS]; |
| 763 | |
| 764 | void touch_nmi_watchdog (void) |
| 765 | { |
| 766 | int i; |
| 767 | |
| 768 | /* |
| 769 | * Just reset the alert counters, (other CPUs might be |
| 770 | * spinning on locks we hold): |
| 771 | */ |
KAMEZAWA Hiroyuki | c891259 | 2006-03-28 01:56:39 -0800 | [diff] [blame] | 772 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | alert_counter[i] = 0; |
Ingo Molnar | 8446f1d | 2005-09-06 15:16:27 -0700 | [diff] [blame] | 774 | |
| 775 | /* |
| 776 | * Tickle the softlockup detector too: |
| 777 | */ |
| 778 | touch_softlockup_watchdog(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | } |
Michal Schmidt | 1e86240 | 2006-07-30 03:03:29 -0700 | [diff] [blame] | 780 | EXPORT_SYMBOL(touch_nmi_watchdog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
| 782 | extern void die_nmi(struct pt_regs *, const char *msg); |
| 783 | |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame^] | 784 | int nmi_watchdog_tick (struct pt_regs * regs, unsigned reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | { |
| 786 | |
| 787 | /* |
| 788 | * Since current_thread_info()-> is always on the stack, and we |
| 789 | * always switch the stack NMI-atomically, it's safe to use |
| 790 | * smp_processor_id(). |
| 791 | */ |
Jesper Juhl | b791cce | 2006-03-28 01:56:52 -0800 | [diff] [blame] | 792 | unsigned int sum; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 793 | int touched = 0; |
Jesper Juhl | b791cce | 2006-03-28 01:56:52 -0800 | [diff] [blame] | 794 | int cpu = smp_processor_id(); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 795 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
| 796 | u64 dummy; |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame^] | 797 | int rc=0; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 798 | |
| 799 | /* check for other users first */ |
| 800 | if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) |
| 801 | == NOTIFY_STOP) { |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame^] | 802 | rc = 1; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 803 | touched = 1; |
| 804 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | |
| 806 | sum = per_cpu(irq_stat, cpu).apic_timer_irqs; |
| 807 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 808 | /* if the apic timer isn't firing, this cpu isn't doing much */ |
| 809 | if (!touched && last_irq_sums[cpu] == sum) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | /* |
| 811 | * Ayiee, looks like this CPU is stuck ... |
| 812 | * wait a few IRQs (5 seconds) before doing the oops ... |
| 813 | */ |
| 814 | alert_counter[cpu]++; |
| 815 | if (alert_counter[cpu] == 5*nmi_hz) |
George Anzinger | 748f2ed | 2005-09-03 15:56:48 -0700 | [diff] [blame] | 816 | /* |
| 817 | * die_nmi will return ONLY if NOTIFY_STOP happens.. |
| 818 | */ |
Ingo Molnar | 91368d7 | 2006-03-23 03:00:54 -0800 | [diff] [blame] | 819 | die_nmi(regs, "BUG: NMI Watchdog detected LOCKUP"); |
GOTO Masanori | b884e25 | 2006-03-07 21:55:29 -0800 | [diff] [blame] | 820 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | last_irq_sums[cpu] = sum; |
| 822 | alert_counter[cpu] = 0; |
| 823 | } |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 824 | /* see if the nmi watchdog went off */ |
| 825 | if (wd->enabled) { |
| 826 | if (nmi_watchdog == NMI_LOCAL_APIC) { |
| 827 | rdmsrl(wd->perfctr_msr, dummy); |
| 828 | if (dummy & wd->check_bit){ |
| 829 | /* this wasn't a watchdog timer interrupt */ |
| 830 | goto done; |
| 831 | } |
| 832 | |
| 833 | /* only Intel P4 uses the cccr msr */ |
| 834 | if (wd->cccr_msr != 0) { |
| 835 | /* |
| 836 | * P4 quirks: |
| 837 | * - An overflown perfctr will assert its interrupt |
| 838 | * until the OVF flag in its CCCR is cleared. |
| 839 | * - LVTPC is masked on interrupt and must be |
| 840 | * unmasked by the LVTPC handler. |
| 841 | */ |
| 842 | rdmsrl(wd->cccr_msr, dummy); |
| 843 | dummy &= ~P4_CCCR_OVF; |
| 844 | wrmsrl(wd->cccr_msr, dummy); |
| 845 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 846 | } |
| 847 | else if (wd->perfctr_msr == MSR_P6_PERFCTR0) { |
| 848 | /* Only P6 based Pentium M need to re-unmask |
| 849 | * the apic vector but it doesn't hurt |
| 850 | * other P6 variant */ |
| 851 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 852 | } |
| 853 | /* start the cycle over again */ |
| 854 | write_watchdog_counter(wd->perfctr_msr, NULL); |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame^] | 855 | rc = 1; |
| 856 | } else if (nmi_watchdog == NMI_IO_APIC) { |
| 857 | /* don't know how to accurately check for this. |
| 858 | * just assume it was a watchdog timer interrupt |
| 859 | * This matches the old behaviour. |
| 860 | */ |
| 861 | rc = 1; |
| 862 | } else |
| 863 | printk(KERN_WARNING "Unknown enabled NMI hardware?!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | } |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 865 | done: |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame^] | 866 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | #ifdef CONFIG_SYSCTL |
| 870 | |
| 871 | static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu) |
| 872 | { |
| 873 | unsigned char reason = get_nmi_reason(); |
| 874 | char buf[64]; |
| 875 | |
| 876 | if (!(reason & 0xc0)) { |
| 877 | sprintf(buf, "NMI received for unknown reason %02x\n", reason); |
| 878 | die_nmi(regs, buf); |
| 879 | } |
| 880 | return 0; |
| 881 | } |
| 882 | |
| 883 | /* |
| 884 | * proc handler for /proc/sys/kernel/unknown_nmi_panic |
| 885 | */ |
| 886 | int proc_unknown_nmi_panic(ctl_table *table, int write, struct file *file, |
| 887 | void __user *buffer, size_t *length, loff_t *ppos) |
| 888 | { |
| 889 | int old_state; |
| 890 | |
| 891 | old_state = unknown_nmi_panic; |
| 892 | proc_dointvec(table, write, file, buffer, length, ppos); |
| 893 | if (!!old_state == !!unknown_nmi_panic) |
| 894 | return 0; |
| 895 | |
| 896 | if (unknown_nmi_panic) { |
| 897 | if (reserve_lapic_nmi() < 0) { |
| 898 | unknown_nmi_panic = 0; |
| 899 | return -EBUSY; |
| 900 | } else { |
| 901 | set_nmi_callback(unknown_nmi_panic_callback); |
| 902 | } |
| 903 | } else { |
| 904 | release_lapic_nmi(); |
| 905 | unset_nmi_callback(); |
| 906 | } |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | #endif |
| 911 | |
| 912 | EXPORT_SYMBOL(nmi_active); |
| 913 | EXPORT_SYMBOL(nmi_watchdog); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 914 | EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi); |
| 915 | EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit); |
| 916 | EXPORT_SYMBOL(reserve_perfctr_nmi); |
| 917 | EXPORT_SYMBOL(release_perfctr_nmi); |
| 918 | EXPORT_SYMBOL(reserve_evntsel_nmi); |
| 919 | EXPORT_SYMBOL(release_evntsel_nmi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | EXPORT_SYMBOL(reserve_lapic_nmi); |
| 921 | EXPORT_SYMBOL(release_lapic_nmi); |
| 922 | EXPORT_SYMBOL(disable_timer_nmi_watchdog); |
| 923 | EXPORT_SYMBOL(enable_timer_nmi_watchdog); |