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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/nmi.h> |
| 20 | #include <linux/sysdev.h> |
| 21 | #include <linux/sysctl.h> |
Don Zickus | 3e4ff11 | 2006-06-26 13:57:01 +0200 | [diff] [blame] | 22 | #include <linux/percpu.h> |
Andi Kleen | 1de8497 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 23 | #include <linux/dmi.h> |
Fernando Luis Vázquez Cao | 0603975 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 24 | #include <linux/kprobes.h> |
Andrew Morton | bb81a09 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 25 | #include <linux/cpumask.h> |
Thomas Gleixner | f8b5035 | 2007-02-16 01:28:09 -0800 | [diff] [blame] | 26 | #include <linux/kernel_stat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | #include <asm/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/nmi.h> |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 30 | #include <asm/kdebug.h> |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 31 | #include <asm/intel_arch_perfmon.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | #include "mach_traps.h" |
| 34 | |
Andi Kleen | 29cbc78 | 2006-09-30 01:47:55 +0200 | [diff] [blame] | 35 | int unknown_nmi_panic; |
| 36 | int nmi_watchdog_enabled; |
| 37 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 38 | /* perfctr_nmi_owner tracks the ownership of the perfctr registers: |
| 39 | * evtsel_nmi_owner tracks the ownership of the event selection |
| 40 | * - different performance counters/ event selection may be reserved for |
| 41 | * different subsystems this reservation system just tries to coordinate |
| 42 | * things a little |
| 43 | */ |
| 44 | static DEFINE_PER_CPU(unsigned long, perfctr_nmi_owner); |
| 45 | static DEFINE_PER_CPU(unsigned long, evntsel_nmi_owner[3]); |
| 46 | |
Andrew Morton | bb81a09 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 47 | static cpumask_t backtrace_mask = CPU_MASK_NONE; |
| 48 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 49 | /* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's |
| 50 | * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now) |
| 51 | */ |
| 52 | #define NMI_MAX_COUNTER_BITS 66 |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* nmi_active: |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 55 | * >0: the lapic NMI watchdog is active, but can be disabled |
| 56 | * <0: the lapic NMI watchdog has not been set up, and cannot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | * be enabled |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 58 | * 0: the lapic NMI watchdog is disabled, but can be enabled |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | */ |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 60 | atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 62 | unsigned int nmi_watchdog = NMI_DEFAULT; |
| 63 | static unsigned int nmi_hz = HZ; |
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 | struct nmi_watchdog_ctlblk { |
| 66 | int enabled; |
| 67 | u64 check_bit; |
| 68 | unsigned int cccr_msr; |
| 69 | unsigned int perfctr_msr; /* the MSR to reset in NMI handler */ |
| 70 | unsigned int evntsel_msr; /* the MSR to select the events to handle */ |
| 71 | }; |
| 72 | static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 74 | /* local prototypes */ |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 75 | static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu); |
| 76 | |
| 77 | extern void show_registers(struct pt_regs *regs); |
| 78 | extern int unknown_nmi_panic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 80 | /* converts an msr to an appropriate reservation bit */ |
| 81 | static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr) |
| 82 | { |
| 83 | /* returns the bit offset of the performance counter register */ |
| 84 | switch (boot_cpu_data.x86_vendor) { |
| 85 | case X86_VENDOR_AMD: |
| 86 | return (msr - MSR_K7_PERFCTR0); |
| 87 | case X86_VENDOR_INTEL: |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 88 | if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) |
| 89 | return (msr - MSR_ARCH_PERFMON_PERFCTR0); |
| 90 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 91 | switch (boot_cpu_data.x86) { |
| 92 | case 6: |
| 93 | return (msr - MSR_P6_PERFCTR0); |
| 94 | case 15: |
| 95 | return (msr - MSR_P4_BPU_PERFCTR0); |
| 96 | } |
| 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | /* converts an msr to an appropriate reservation bit */ |
| 102 | static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr) |
| 103 | { |
| 104 | /* returns the bit offset of the event selection register */ |
| 105 | switch (boot_cpu_data.x86_vendor) { |
| 106 | case X86_VENDOR_AMD: |
| 107 | return (msr - MSR_K7_EVNTSEL0); |
| 108 | case X86_VENDOR_INTEL: |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 109 | if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) |
| 110 | return (msr - MSR_ARCH_PERFMON_EVENTSEL0); |
| 111 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 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 | { |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 125 | int cpu; |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 126 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 127 | for_each_possible_cpu (cpu) { |
| 128 | if (test_bit(counter, &per_cpu(perfctr_nmi_owner, cpu))) |
| 129 | return 0; |
| 130 | } |
| 131 | return 1; |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* checks the an msr for availability */ |
| 135 | int avail_to_resrv_perfctr_nmi(unsigned int msr) |
| 136 | { |
| 137 | unsigned int counter; |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 138 | int cpu; |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 139 | |
| 140 | counter = nmi_perfctr_msr_to_bit(msr); |
| 141 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 142 | |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 143 | for_each_possible_cpu (cpu) { |
| 144 | if (test_bit(counter, &per_cpu(perfctr_nmi_owner, cpu))) |
| 145 | return 0; |
| 146 | } |
| 147 | return 1; |
| 148 | } |
| 149 | |
| 150 | static int __reserve_perfctr_nmi(int cpu, unsigned int msr) |
| 151 | { |
| 152 | unsigned int counter; |
| 153 | if (cpu < 0) |
| 154 | cpu = smp_processor_id(); |
| 155 | |
| 156 | counter = nmi_perfctr_msr_to_bit(msr); |
| 157 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 158 | |
| 159 | if (!test_and_set_bit(counter, &per_cpu(perfctr_nmi_owner, cpu))) |
| 160 | return 1; |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static void __release_perfctr_nmi(int cpu, unsigned int msr) |
| 165 | { |
| 166 | unsigned int counter; |
| 167 | if (cpu < 0) |
| 168 | cpu = smp_processor_id(); |
| 169 | |
| 170 | counter = nmi_perfctr_msr_to_bit(msr); |
| 171 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 172 | |
| 173 | clear_bit(counter, &per_cpu(perfctr_nmi_owner, cpu)); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | int reserve_perfctr_nmi(unsigned int msr) |
| 177 | { |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 178 | int cpu, i; |
| 179 | for_each_possible_cpu (cpu) { |
| 180 | if (!__reserve_perfctr_nmi(cpu, msr)) { |
| 181 | for_each_possible_cpu (i) { |
| 182 | if (i >= cpu) |
| 183 | break; |
| 184 | __release_perfctr_nmi(i, msr); |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | } |
| 189 | return 1; |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void release_perfctr_nmi(unsigned int msr) |
| 193 | { |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 194 | int cpu; |
| 195 | for_each_possible_cpu (cpu) { |
| 196 | __release_perfctr_nmi(cpu, msr); |
| 197 | } |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 200 | int __reserve_evntsel_nmi(int cpu, unsigned int msr) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 201 | { |
| 202 | unsigned int counter; |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 203 | if (cpu < 0) |
| 204 | cpu = smp_processor_id(); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 205 | |
| 206 | counter = nmi_evntsel_msr_to_bit(msr); |
| 207 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 208 | |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 209 | if (!test_and_set_bit(counter, &per_cpu(evntsel_nmi_owner, cpu)[0])) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 210 | return 1; |
| 211 | return 0; |
| 212 | } |
| 213 | |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 214 | static void __release_evntsel_nmi(int cpu, unsigned int msr) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 215 | { |
| 216 | unsigned int counter; |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 217 | if (cpu < 0) |
| 218 | cpu = smp_processor_id(); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 219 | |
| 220 | counter = nmi_evntsel_msr_to_bit(msr); |
| 221 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 222 | |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 223 | clear_bit(counter, &per_cpu(evntsel_nmi_owner, cpu)[0]); |
| 224 | } |
| 225 | |
| 226 | int reserve_evntsel_nmi(unsigned int msr) |
| 227 | { |
| 228 | int cpu, i; |
| 229 | for_each_possible_cpu (cpu) { |
| 230 | if (!__reserve_evntsel_nmi(cpu, msr)) { |
| 231 | for_each_possible_cpu (i) { |
| 232 | if (i >= cpu) |
| 233 | break; |
| 234 | __release_evntsel_nmi(i, msr); |
| 235 | } |
| 236 | return 0; |
| 237 | } |
| 238 | } |
| 239 | return 1; |
| 240 | } |
| 241 | |
| 242 | void release_evntsel_nmi(unsigned int msr) |
| 243 | { |
| 244 | int cpu; |
| 245 | for_each_possible_cpu (cpu) { |
| 246 | __release_evntsel_nmi(cpu, msr); |
| 247 | } |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 250 | static __cpuinit inline int nmi_known_cpu(void) |
| 251 | { |
| 252 | switch (boot_cpu_data.x86_vendor) { |
| 253 | case X86_VENDOR_AMD: |
Andi Kleen | 0a4599c | 2007-02-13 13:26:25 +0100 | [diff] [blame] | 254 | return ((boot_cpu_data.x86 == 15) || (boot_cpu_data.x86 == 6) |
| 255 | || (boot_cpu_data.x86 == 16)); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 256 | case X86_VENDOR_INTEL: |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 257 | if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) |
| 258 | return 1; |
| 259 | else |
| 260 | return ((boot_cpu_data.x86 == 15) || (boot_cpu_data.x86 == 6)); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 261 | } |
| 262 | return 0; |
| 263 | } |
| 264 | |
Ravikiran G Thirumalai | 92715e2 | 2006-12-09 21:33:35 +0100 | [diff] [blame] | 265 | static int endflag __initdata = 0; |
| 266 | |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 267 | #ifdef CONFIG_SMP |
| 268 | /* The performance counters used by NMI_LOCAL_APIC don't trigger when |
| 269 | * the CPU is idle. To make sure the NMI watchdog really ticks on all |
| 270 | * CPUs during the test make them busy. |
| 271 | */ |
| 272 | static __init void nmi_cpu_busy(void *data) |
| 273 | { |
Ingo Molnar | 366c7f5 | 2006-07-03 00:25:25 -0700 | [diff] [blame] | 274 | local_irq_enable_in_hardirq(); |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 275 | /* Intentionally don't use cpu_relax here. This is |
| 276 | to make sure that the performance counter really ticks, |
| 277 | even if there is a simulator or similar that catches the |
| 278 | pause instruction. On a real HT machine this is fine because |
| 279 | all other CPUs are busy with "useless" delay loops and don't |
| 280 | care if they get somewhat less cycles. */ |
Ravikiran G Thirumalai | 92715e2 | 2006-12-09 21:33:35 +0100 | [diff] [blame] | 281 | while (endflag == 0) |
| 282 | mb(); |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 283 | } |
| 284 | #endif |
| 285 | |
Venkatesh Pallipadi | 90ce4bc | 2007-02-13 13:26:22 +0100 | [diff] [blame] | 286 | static unsigned int adjust_for_32bit_ctr(unsigned int hz) |
| 287 | { |
| 288 | u64 counter_val; |
| 289 | unsigned int retval = hz; |
| 290 | |
| 291 | /* |
| 292 | * On Intel CPUs with P6/ARCH_PERFMON only 32 bits in the counter |
| 293 | * are writable, with higher bits sign extending from bit 31. |
| 294 | * So, we can only program the counter with 31 bit values and |
| 295 | * 32nd bit should be 1, for 33.. to be 1. |
| 296 | * Find the appropriate nmi_hz |
| 297 | */ |
| 298 | counter_val = (u64)cpu_khz * 1000; |
| 299 | do_div(counter_val, retval); |
| 300 | if (counter_val > 0x7fffffffULL) { |
| 301 | u64 count = (u64)cpu_khz * 1000; |
| 302 | do_div(count, 0x7fffffffUL); |
| 303 | retval = count + 1; |
| 304 | } |
| 305 | return retval; |
| 306 | } |
| 307 | |
Jack F Vogel | 67701ae | 2005-05-01 08:58:48 -0700 | [diff] [blame] | 308 | static int __init check_nmi_watchdog(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 310 | unsigned int *prev_nmi_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | int cpu; |
| 312 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 313 | if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT)) |
| 314 | return 0; |
| 315 | |
| 316 | if (!atomic_read(&nmi_active)) |
Jack F Vogel | 67701ae | 2005-05-01 08:58:48 -0700 | [diff] [blame] | 317 | return 0; |
| 318 | |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 319 | prev_nmi_count = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL); |
| 320 | if (!prev_nmi_count) |
| 321 | return -1; |
| 322 | |
Jack F Vogel | 67701ae | 2005-05-01 08:58:48 -0700 | [diff] [blame] | 323 | printk(KERN_INFO "Testing NMI watchdog ... "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 325 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 326 | smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0); |
| 327 | |
KAMEZAWA Hiroyuki | c891259 | 2006-03-28 01:56:39 -0800 | [diff] [blame] | 328 | for_each_possible_cpu(cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | prev_nmi_count[cpu] = per_cpu(irq_stat, cpu).__nmi_count; |
| 330 | local_irq_enable(); |
Andi Kleen | 0fb2ebf | 2007-04-02 12:14:12 +0200 | [diff] [blame^] | 331 | mdelay((20*1000)/nmi_hz); // wait 20 ticks |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
KAMEZAWA Hiroyuki | c891259 | 2006-03-28 01:56:39 -0800 | [diff] [blame] | 333 | for_each_possible_cpu(cpu) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | #ifdef CONFIG_SMP |
| 335 | /* Check cpu_callin_map here because that is set |
| 336 | after the timer is started. */ |
| 337 | if (!cpu_isset(cpu, cpu_callin_map)) |
| 338 | continue; |
| 339 | #endif |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 340 | if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled) |
| 341 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | if (nmi_count(cpu) - prev_nmi_count[cpu] <= 5) { |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 343 | printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n", |
| 344 | cpu, |
| 345 | prev_nmi_count[cpu], |
| 346 | nmi_count(cpu)); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 347 | per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0; |
| 348 | atomic_dec(&nmi_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 351 | if (!atomic_read(&nmi_active)) { |
| 352 | kfree(prev_nmi_count); |
| 353 | atomic_set(&nmi_active, -1); |
| 354 | return -1; |
| 355 | } |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 356 | endflag = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | printk("OK.\n"); |
| 358 | |
| 359 | /* now that we know it works we can reduce NMI frequency to |
| 360 | something more reasonable; makes a difference in some configs */ |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 361 | if (nmi_watchdog == NMI_LOCAL_APIC) { |
| 362 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
| 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | nmi_hz = 1; |
Venkatesh Pallipadi | 90ce4bc | 2007-02-13 13:26:22 +0100 | [diff] [blame] | 365 | |
| 366 | if (wd->perfctr_msr == MSR_P6_PERFCTR0 || |
| 367 | wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0) { |
| 368 | nmi_hz = adjust_for_32bit_ctr(nmi_hz); |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 369 | } |
| 370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Eric W. Biederman | 29b7008 | 2005-10-30 14:59:40 -0800 | [diff] [blame] | 372 | kfree(prev_nmi_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | return 0; |
| 374 | } |
Jack F Vogel | 67701ae | 2005-05-01 08:58:48 -0700 | [diff] [blame] | 375 | /* This needs to happen later in boot so counters are working */ |
| 376 | late_initcall(check_nmi_watchdog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
| 378 | static int __init setup_nmi_watchdog(char *str) |
| 379 | { |
| 380 | int nmi; |
| 381 | |
| 382 | get_option(&str, &nmi); |
| 383 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 384 | if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return 0; |
Venkatesh Pallipadi | 58d9ce7d | 2007-01-22 20:40:34 -0800 | [diff] [blame] | 386 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 387 | nmi_watchdog = nmi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | return 1; |
| 389 | } |
| 390 | |
| 391 | __setup("nmi_watchdog=", setup_nmi_watchdog); |
| 392 | |
| 393 | static void disable_lapic_nmi_watchdog(void) |
| 394 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 395 | BUG_ON(nmi_watchdog != NMI_LOCAL_APIC); |
| 396 | |
| 397 | if (atomic_read(&nmi_active) <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 400 | on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 402 | BUG_ON(atomic_read(&nmi_active) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static void enable_lapic_nmi_watchdog(void) |
| 406 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 407 | BUG_ON(nmi_watchdog != NMI_LOCAL_APIC); |
| 408 | |
| 409 | /* are we already enabled */ |
| 410 | if (atomic_read(&nmi_active) != 0) |
| 411 | return; |
| 412 | |
| 413 | /* are we lapic aware */ |
| 414 | if (nmi_known_cpu() <= 0) |
| 415 | return; |
| 416 | |
| 417 | on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1); |
| 418 | touch_nmi_watchdog(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | void disable_timer_nmi_watchdog(void) |
| 422 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 423 | BUG_ON(nmi_watchdog != NMI_IO_APIC); |
| 424 | |
| 425 | if (atomic_read(&nmi_active) <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | return; |
| 427 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 428 | disable_irq(0); |
| 429 | on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1); |
| 430 | |
| 431 | BUG_ON(atomic_read(&nmi_active) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void enable_timer_nmi_watchdog(void) |
| 435 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 436 | BUG_ON(nmi_watchdog != NMI_IO_APIC); |
| 437 | |
| 438 | if (atomic_read(&nmi_active) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | touch_nmi_watchdog(); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 440 | on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1); |
| 441 | enable_irq(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
Ingo Molnar | 5d0e600 | 2007-02-13 13:26:24 +0100 | [diff] [blame] | 445 | static void __acpi_nmi_disable(void *__unused) |
| 446 | { |
| 447 | apic_write_around(APIC_LVT0, APIC_DM_NMI | APIC_LVT_MASKED); |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Disable timer based NMIs on all CPUs: |
| 452 | */ |
| 453 | void acpi_nmi_disable(void) |
| 454 | { |
| 455 | if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC) |
| 456 | on_each_cpu(__acpi_nmi_disable, NULL, 0, 1); |
| 457 | } |
| 458 | |
| 459 | static void __acpi_nmi_enable(void *__unused) |
| 460 | { |
| 461 | apic_write_around(APIC_LVT0, APIC_DM_NMI); |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * Enable timer based NMIs on all CPUs: |
| 466 | */ |
| 467 | void acpi_nmi_enable(void) |
| 468 | { |
| 469 | if (atomic_read(&nmi_active) && nmi_watchdog == NMI_IO_APIC) |
| 470 | on_each_cpu(__acpi_nmi_enable, NULL, 0, 1); |
| 471 | } |
| 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | #ifdef CONFIG_PM |
| 474 | |
| 475 | static int nmi_pm_active; /* nmi_active before suspend */ |
| 476 | |
Pavel Machek | 438510f | 2005-04-16 15:25:24 -0700 | [diff] [blame] | 477 | 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] | 478 | { |
Shaohua Li | 4038f90 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 479 | /* only CPU0 goes here, other CPUs should be offline */ |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 480 | nmi_pm_active = atomic_read(&nmi_active); |
Shaohua Li | 4038f90 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 481 | stop_apic_nmi_watchdog(NULL); |
| 482 | BUG_ON(atomic_read(&nmi_active) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static int lapic_nmi_resume(struct sys_device *dev) |
| 487 | { |
Shaohua Li | 4038f90 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 488 | /* only CPU0 goes here, other CPUs should be offline */ |
| 489 | if (nmi_pm_active > 0) { |
| 490 | setup_apic_nmi_watchdog(NULL); |
| 491 | touch_nmi_watchdog(); |
| 492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | |
| 497 | static struct sysdev_class nmi_sysclass = { |
| 498 | set_kset_name("lapic_nmi"), |
| 499 | .resume = lapic_nmi_resume, |
| 500 | .suspend = lapic_nmi_suspend, |
| 501 | }; |
| 502 | |
| 503 | static struct sys_device device_lapic_nmi = { |
| 504 | .id = 0, |
| 505 | .cls = &nmi_sysclass, |
| 506 | }; |
| 507 | |
| 508 | static int __init init_lapic_nmi_sysfs(void) |
| 509 | { |
| 510 | int error; |
| 511 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 512 | /* should really be a BUG_ON but b/c this is an |
| 513 | * init call, it just doesn't work. -dcz |
| 514 | */ |
| 515 | if (nmi_watchdog != NMI_LOCAL_APIC) |
| 516 | return 0; |
| 517 | |
| 518 | if ( atomic_read(&nmi_active) < 0 ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | return 0; |
| 520 | |
| 521 | error = sysdev_class_register(&nmi_sysclass); |
| 522 | if (!error) |
| 523 | error = sysdev_register(&device_lapic_nmi); |
| 524 | return error; |
| 525 | } |
| 526 | /* must come after the local APIC's device_initcall() */ |
| 527 | late_initcall(init_lapic_nmi_sysfs); |
| 528 | |
| 529 | #endif /* CONFIG_PM */ |
| 530 | |
| 531 | /* |
| 532 | * Activate the NMI watchdog via the local APIC. |
| 533 | * Original code written by Keith Owens. |
| 534 | */ |
| 535 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 536 | static void write_watchdog_counter(unsigned int perfctr_msr, const char *descr) |
Jan Beulich | 7fbb4f6 | 2005-06-23 00:08:23 -0700 | [diff] [blame] | 537 | { |
| 538 | u64 count = (u64)cpu_khz * 1000; |
| 539 | |
| 540 | do_div(count, nmi_hz); |
| 541 | if(descr) |
| 542 | Dprintk("setting %s to -0x%08Lx\n", descr, count); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 543 | wrmsrl(perfctr_msr, 0 - count); |
Jan Beulich | 7fbb4f6 | 2005-06-23 00:08:23 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Venkatesh Pallipadi | 90ce4bc | 2007-02-13 13:26:22 +0100 | [diff] [blame] | 546 | static void write_watchdog_counter32(unsigned int perfctr_msr, |
| 547 | const char *descr) |
| 548 | { |
| 549 | u64 count = (u64)cpu_khz * 1000; |
| 550 | |
| 551 | do_div(count, nmi_hz); |
| 552 | if(descr) |
| 553 | Dprintk("setting %s to -0x%08Lx\n", descr, count); |
| 554 | wrmsr(perfctr_msr, (u32)(-count), 0); |
| 555 | } |
| 556 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 557 | /* Note that these events don't tick when the CPU idles. This means |
| 558 | the frequency varies with CPU load. */ |
| 559 | |
| 560 | #define K7_EVNTSEL_ENABLE (1 << 22) |
| 561 | #define K7_EVNTSEL_INT (1 << 20) |
| 562 | #define K7_EVNTSEL_OS (1 << 17) |
| 563 | #define K7_EVNTSEL_USR (1 << 16) |
| 564 | #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76 |
| 565 | #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING |
| 566 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 567 | static int setup_k7_watchdog(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 569 | unsigned int perfctr_msr, evntsel_msr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | unsigned int evntsel; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 571 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 573 | perfctr_msr = MSR_K7_PERFCTR0; |
| 574 | evntsel_msr = MSR_K7_EVNTSEL0; |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 575 | if (!__reserve_perfctr_nmi(-1, perfctr_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 576 | goto fail; |
| 577 | |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 578 | if (!__reserve_evntsel_nmi(-1, evntsel_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 579 | goto fail1; |
| 580 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 581 | wrmsrl(perfctr_msr, 0UL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
| 583 | evntsel = K7_EVNTSEL_INT |
| 584 | | K7_EVNTSEL_OS |
| 585 | | K7_EVNTSEL_USR |
| 586 | | K7_NMI_EVENT; |
| 587 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 588 | /* setup the timer */ |
| 589 | wrmsr(evntsel_msr, evntsel, 0); |
| 590 | write_watchdog_counter(perfctr_msr, "K7_PERFCTR0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 592 | evntsel |= K7_EVNTSEL_ENABLE; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 593 | wrmsr(evntsel_msr, evntsel, 0); |
| 594 | |
| 595 | wd->perfctr_msr = perfctr_msr; |
| 596 | wd->evntsel_msr = evntsel_msr; |
| 597 | wd->cccr_msr = 0; //unused |
| 598 | wd->check_bit = 1ULL<<63; |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 599 | return 1; |
| 600 | fail1: |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 601 | __release_perfctr_nmi(-1, perfctr_msr); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 602 | fail: |
| 603 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 606 | static void stop_k7_watchdog(void) |
| 607 | { |
| 608 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
| 609 | |
| 610 | wrmsr(wd->evntsel_msr, 0, 0); |
| 611 | |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 612 | __release_evntsel_nmi(-1, wd->evntsel_msr); |
| 613 | __release_perfctr_nmi(-1, wd->perfctr_msr); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | #define P6_EVNTSEL0_ENABLE (1 << 22) |
| 617 | #define P6_EVNTSEL_INT (1 << 20) |
| 618 | #define P6_EVNTSEL_OS (1 << 17) |
| 619 | #define P6_EVNTSEL_USR (1 << 16) |
| 620 | #define P6_EVENT_CPU_CLOCKS_NOT_HALTED 0x79 |
| 621 | #define P6_NMI_EVENT P6_EVENT_CPU_CLOCKS_NOT_HALTED |
| 622 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 623 | static int setup_p6_watchdog(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 625 | unsigned int perfctr_msr, evntsel_msr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | unsigned int evntsel; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 627 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 629 | perfctr_msr = MSR_P6_PERFCTR0; |
| 630 | evntsel_msr = MSR_P6_EVNTSEL0; |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 631 | if (!__reserve_perfctr_nmi(-1, perfctr_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 632 | goto fail; |
| 633 | |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 634 | if (!__reserve_evntsel_nmi(-1, evntsel_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 635 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 637 | wrmsrl(perfctr_msr, 0UL); |
| 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | evntsel = P6_EVNTSEL_INT |
| 640 | | P6_EVNTSEL_OS |
| 641 | | P6_EVNTSEL_USR |
| 642 | | P6_NMI_EVENT; |
| 643 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 644 | /* setup the timer */ |
| 645 | wrmsr(evntsel_msr, evntsel, 0); |
Venkatesh Pallipadi | 90ce4bc | 2007-02-13 13:26:22 +0100 | [diff] [blame] | 646 | nmi_hz = adjust_for_32bit_ctr(nmi_hz); |
| 647 | write_watchdog_counter32(perfctr_msr, "P6_PERFCTR0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 649 | evntsel |= P6_EVNTSEL0_ENABLE; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 650 | wrmsr(evntsel_msr, evntsel, 0); |
| 651 | |
| 652 | wd->perfctr_msr = perfctr_msr; |
| 653 | wd->evntsel_msr = evntsel_msr; |
| 654 | wd->cccr_msr = 0; //unused |
| 655 | wd->check_bit = 1ULL<<39; |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 656 | return 1; |
| 657 | fail1: |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 658 | __release_perfctr_nmi(-1, perfctr_msr); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 659 | fail: |
| 660 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 663 | static void stop_p6_watchdog(void) |
| 664 | { |
| 665 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
| 666 | |
| 667 | wrmsr(wd->evntsel_msr, 0, 0); |
| 668 | |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 669 | __release_evntsel_nmi(-1, wd->evntsel_msr); |
| 670 | __release_perfctr_nmi(-1, wd->perfctr_msr); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* Note that these events don't tick when the CPU idles. This means |
| 674 | the frequency varies with CPU load. */ |
| 675 | |
| 676 | #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7) |
| 677 | #define P4_ESCR_EVENT_SELECT(N) ((N)<<25) |
| 678 | #define P4_ESCR_OS (1<<3) |
| 679 | #define P4_ESCR_USR (1<<2) |
| 680 | #define P4_CCCR_OVF_PMI0 (1<<26) |
| 681 | #define P4_CCCR_OVF_PMI1 (1<<27) |
| 682 | #define P4_CCCR_THRESHOLD(N) ((N)<<20) |
| 683 | #define P4_CCCR_COMPLEMENT (1<<19) |
| 684 | #define P4_CCCR_COMPARE (1<<18) |
| 685 | #define P4_CCCR_REQUIRED (3<<16) |
| 686 | #define P4_CCCR_ESCR_SELECT(N) ((N)<<13) |
| 687 | #define P4_CCCR_ENABLE (1<<12) |
| 688 | #define P4_CCCR_OVF (1<<31) |
| 689 | /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter |
| 690 | CRU_ESCR0 (with any non-null event selector) through a complemented |
| 691 | max threshold. [IA32-Vol3, Section 14.9.9] */ |
| 692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | static int setup_p4_watchdog(void) |
| 694 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 695 | unsigned int perfctr_msr, evntsel_msr, cccr_msr; |
| 696 | unsigned int evntsel, cccr_val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | unsigned int misc_enable, dummy; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 698 | unsigned int ht_num; |
| 699 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 701 | rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL)) |
| 703 | return 0; |
| 704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | #ifdef CONFIG_SMP |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 706 | /* detect which hyperthread we are on */ |
| 707 | if (smp_num_siblings == 2) { |
| 708 | unsigned int ebx, apicid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 710 | ebx = cpuid_ebx(1); |
| 711 | apicid = (ebx >> 24) & 0xff; |
| 712 | ht_num = apicid & 1; |
| 713 | } else |
| 714 | #endif |
| 715 | ht_num = 0; |
| 716 | |
| 717 | /* performance counters are shared resources |
| 718 | * assign each hyperthread its own set |
| 719 | * (re-use the ESCR0 register, seems safe |
| 720 | * and keeps the cccr_val the same) |
| 721 | */ |
| 722 | if (!ht_num) { |
| 723 | /* logical cpu 0 */ |
| 724 | perfctr_msr = MSR_P4_IQ_PERFCTR0; |
| 725 | evntsel_msr = MSR_P4_CRU_ESCR0; |
| 726 | cccr_msr = MSR_P4_IQ_CCCR0; |
| 727 | cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4); |
| 728 | } else { |
| 729 | /* logical cpu 1 */ |
| 730 | perfctr_msr = MSR_P4_IQ_PERFCTR1; |
| 731 | evntsel_msr = MSR_P4_CRU_ESCR0; |
| 732 | cccr_msr = MSR_P4_IQ_CCCR1; |
| 733 | cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4); |
| 734 | } |
| 735 | |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 736 | if (!__reserve_perfctr_nmi(-1, perfctr_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 737 | goto fail; |
| 738 | |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 739 | if (!__reserve_evntsel_nmi(-1, evntsel_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 740 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 742 | evntsel = P4_ESCR_EVENT_SELECT(0x3F) |
| 743 | | P4_ESCR_OS |
| 744 | | P4_ESCR_USR; |
| 745 | |
| 746 | cccr_val |= P4_CCCR_THRESHOLD(15) |
| 747 | | P4_CCCR_COMPLEMENT |
| 748 | | P4_CCCR_COMPARE |
| 749 | | P4_CCCR_REQUIRED; |
| 750 | |
| 751 | wrmsr(evntsel_msr, evntsel, 0); |
| 752 | wrmsr(cccr_msr, cccr_val, 0); |
| 753 | write_watchdog_counter(perfctr_msr, "P4_IQ_COUNTER0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 755 | cccr_val |= P4_CCCR_ENABLE; |
| 756 | wrmsr(cccr_msr, cccr_val, 0); |
| 757 | wd->perfctr_msr = perfctr_msr; |
| 758 | wd->evntsel_msr = evntsel_msr; |
| 759 | wd->cccr_msr = cccr_msr; |
| 760 | wd->check_bit = 1ULL<<39; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | return 1; |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 762 | fail1: |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 763 | __release_perfctr_nmi(-1, perfctr_msr); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 764 | fail: |
| 765 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 768 | static void stop_p4_watchdog(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | { |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 770 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 772 | wrmsr(wd->cccr_msr, 0, 0); |
| 773 | wrmsr(wd->evntsel_msr, 0, 0); |
| 774 | |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 775 | __release_evntsel_nmi(-1, wd->evntsel_msr); |
| 776 | __release_perfctr_nmi(-1, wd->perfctr_msr); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 777 | } |
| 778 | |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 779 | #define ARCH_PERFMON_NMI_EVENT_SEL ARCH_PERFMON_UNHALTED_CORE_CYCLES_SEL |
| 780 | #define ARCH_PERFMON_NMI_EVENT_UMASK ARCH_PERFMON_UNHALTED_CORE_CYCLES_UMASK |
| 781 | |
| 782 | static int setup_intel_arch_watchdog(void) |
| 783 | { |
| 784 | unsigned int ebx; |
| 785 | union cpuid10_eax eax; |
| 786 | unsigned int unused; |
| 787 | unsigned int perfctr_msr, evntsel_msr; |
| 788 | unsigned int evntsel; |
| 789 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
| 790 | |
| 791 | /* |
| 792 | * Check whether the Architectural PerfMon supports |
| 793 | * Unhalted Core Cycles Event or not. |
| 794 | * NOTE: Corresponding bit = 0 in ebx indicates event present. |
| 795 | */ |
| 796 | cpuid(10, &(eax.full), &ebx, &unused, &unused); |
| 797 | if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) || |
| 798 | (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT)) |
| 799 | goto fail; |
| 800 | |
| 801 | perfctr_msr = MSR_ARCH_PERFMON_PERFCTR0; |
| 802 | evntsel_msr = MSR_ARCH_PERFMON_EVENTSEL0; |
| 803 | |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 804 | if (!__reserve_perfctr_nmi(-1, perfctr_msr)) |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 805 | goto fail; |
| 806 | |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 807 | if (!__reserve_evntsel_nmi(-1, evntsel_msr)) |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 808 | goto fail1; |
| 809 | |
| 810 | wrmsrl(perfctr_msr, 0UL); |
| 811 | |
| 812 | evntsel = ARCH_PERFMON_EVENTSEL_INT |
| 813 | | ARCH_PERFMON_EVENTSEL_OS |
| 814 | | ARCH_PERFMON_EVENTSEL_USR |
| 815 | | ARCH_PERFMON_NMI_EVENT_SEL |
| 816 | | ARCH_PERFMON_NMI_EVENT_UMASK; |
| 817 | |
| 818 | /* setup the timer */ |
| 819 | wrmsr(evntsel_msr, evntsel, 0); |
Venkatesh Pallipadi | 90ce4bc | 2007-02-13 13:26:22 +0100 | [diff] [blame] | 820 | nmi_hz = adjust_for_32bit_ctr(nmi_hz); |
| 821 | write_watchdog_counter32(perfctr_msr, "INTEL_ARCH_PERFCTR0"); |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 822 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 823 | evntsel |= ARCH_PERFMON_EVENTSEL0_ENABLE; |
| 824 | wrmsr(evntsel_msr, evntsel, 0); |
| 825 | |
| 826 | wd->perfctr_msr = perfctr_msr; |
| 827 | wd->evntsel_msr = evntsel_msr; |
| 828 | wd->cccr_msr = 0; //unused |
| 829 | wd->check_bit = 1ULL << (eax.split.bit_width - 1); |
| 830 | return 1; |
| 831 | fail1: |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 832 | __release_perfctr_nmi(-1, perfctr_msr); |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 833 | fail: |
| 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | static void stop_intel_arch_watchdog(void) |
| 838 | { |
| 839 | unsigned int ebx; |
| 840 | union cpuid10_eax eax; |
| 841 | unsigned int unused; |
| 842 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
| 843 | |
| 844 | /* |
| 845 | * Check whether the Architectural PerfMon supports |
| 846 | * Unhalted Core Cycles Event or not. |
| 847 | * NOTE: Corresponding bit = 0 in ebx indicates event present. |
| 848 | */ |
| 849 | cpuid(10, &(eax.full), &ebx, &unused, &unused); |
| 850 | if ((eax.split.mask_length < (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX+1)) || |
| 851 | (ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT)) |
| 852 | return; |
| 853 | |
| 854 | wrmsr(wd->evntsel_msr, 0, 0); |
Andi Kleen | 89e0756 | 2007-04-02 12:14:12 +0200 | [diff] [blame] | 855 | __release_evntsel_nmi(-1, wd->evntsel_msr); |
| 856 | __release_perfctr_nmi(-1, wd->perfctr_msr); |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 857 | } |
| 858 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 859 | void setup_apic_nmi_watchdog (void *unused) |
| 860 | { |
Shaohua Li | 4038f90 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 861 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
| 862 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 863 | /* only support LOCAL and IO APICs for now */ |
| 864 | if ((nmi_watchdog != NMI_LOCAL_APIC) && |
| 865 | (nmi_watchdog != NMI_IO_APIC)) |
| 866 | return; |
| 867 | |
Shaohua Li | 4038f90 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 868 | if (wd->enabled == 1) |
| 869 | return; |
| 870 | |
| 871 | /* cheap hack to support suspend/resume */ |
| 872 | /* if cpu0 is not active neither should the other cpus */ |
| 873 | if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0)) |
| 874 | return; |
| 875 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 876 | if (nmi_watchdog == NMI_LOCAL_APIC) { |
| 877 | switch (boot_cpu_data.x86_vendor) { |
| 878 | case X86_VENDOR_AMD: |
Andi Kleen | 0a4599c | 2007-02-13 13:26:25 +0100 | [diff] [blame] | 879 | if (boot_cpu_data.x86 != 6 && boot_cpu_data.x86 != 15 && |
| 880 | boot_cpu_data.x86 != 16) |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 881 | return; |
| 882 | if (!setup_k7_watchdog()) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 883 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | break; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 885 | case X86_VENDOR_INTEL: |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 886 | if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) { |
| 887 | if (!setup_intel_arch_watchdog()) |
| 888 | return; |
| 889 | break; |
| 890 | } |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 891 | switch (boot_cpu_data.x86) { |
| 892 | case 6: |
| 893 | if (boot_cpu_data.x86_model > 0xd) |
| 894 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 896 | if (!setup_p6_watchdog()) |
| 897 | return; |
| 898 | break; |
| 899 | case 15: |
| 900 | if (boot_cpu_data.x86_model > 0x4) |
| 901 | return; |
| 902 | |
| 903 | if (!setup_p4_watchdog()) |
| 904 | return; |
| 905 | break; |
| 906 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | return; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 908 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | break; |
| 910 | default: |
| 911 | return; |
| 912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | } |
Shaohua Li | 4038f90 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 914 | wd->enabled = 1; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 915 | atomic_inc(&nmi_active); |
| 916 | } |
| 917 | |
Shaohua Li | 4038f90 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 918 | void stop_apic_nmi_watchdog(void *unused) |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 919 | { |
Shaohua Li | 4038f90 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 920 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
| 921 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 922 | /* only support LOCAL and IO APICs for now */ |
| 923 | if ((nmi_watchdog != NMI_LOCAL_APIC) && |
| 924 | (nmi_watchdog != NMI_IO_APIC)) |
| 925 | return; |
| 926 | |
Shaohua Li | 4038f90 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 927 | if (wd->enabled == 0) |
| 928 | return; |
| 929 | |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 930 | if (nmi_watchdog == NMI_LOCAL_APIC) { |
| 931 | switch (boot_cpu_data.x86_vendor) { |
| 932 | case X86_VENDOR_AMD: |
| 933 | stop_k7_watchdog(); |
| 934 | break; |
| 935 | case X86_VENDOR_INTEL: |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 936 | if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) { |
| 937 | stop_intel_arch_watchdog(); |
| 938 | break; |
| 939 | } |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 940 | switch (boot_cpu_data.x86) { |
| 941 | case 6: |
| 942 | if (boot_cpu_data.x86_model > 0xd) |
| 943 | break; |
| 944 | stop_p6_watchdog(); |
| 945 | break; |
| 946 | case 15: |
| 947 | if (boot_cpu_data.x86_model > 0x4) |
| 948 | break; |
| 949 | stop_p4_watchdog(); |
| 950 | break; |
| 951 | } |
| 952 | break; |
| 953 | default: |
| 954 | return; |
| 955 | } |
| 956 | } |
Shaohua Li | 4038f90 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 957 | wd->enabled = 0; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 958 | atomic_dec(&nmi_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | /* |
| 962 | * the best way to detect whether a CPU has a 'hard lockup' problem |
| 963 | * is to check it's local APIC timer IRQ counts. If they are not |
| 964 | * changing then that CPU has some problem. |
| 965 | * |
| 966 | * as these watchdog NMI IRQs are generated on every CPU, we only |
| 967 | * have to check the current processor. |
| 968 | * |
| 969 | * since NMIs don't listen to _any_ locks, we have to be extremely |
| 970 | * careful not to rely on unsafe variables. The printk might lock |
| 971 | * up though, so we have to break up any console locks first ... |
| 972 | * [when there will be more tty-related locks, break them up |
| 973 | * here too!] |
| 974 | */ |
| 975 | |
| 976 | static unsigned int |
| 977 | last_irq_sums [NR_CPUS], |
| 978 | alert_counter [NR_CPUS]; |
| 979 | |
| 980 | void touch_nmi_watchdog (void) |
| 981 | { |
Jan Beulich | c6ea396 | 2006-12-07 02:14:09 +0100 | [diff] [blame] | 982 | if (nmi_watchdog > 0) { |
| 983 | unsigned cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
Jan Beulich | c6ea396 | 2006-12-07 02:14:09 +0100 | [diff] [blame] | 985 | /* |
| 986 | * Just reset the alert counters, (other CPUs might be |
| 987 | * spinning on locks we hold): |
| 988 | */ |
| 989 | for_each_present_cpu (cpu) |
| 990 | alert_counter[cpu] = 0; |
| 991 | } |
Ingo Molnar | 8446f1d | 2005-09-06 15:16:27 -0700 | [diff] [blame] | 992 | |
| 993 | /* |
| 994 | * Tickle the softlockup detector too: |
| 995 | */ |
| 996 | touch_softlockup_watchdog(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | } |
Michal Schmidt | 1e86240 | 2006-07-30 03:03:29 -0700 | [diff] [blame] | 998 | EXPORT_SYMBOL(touch_nmi_watchdog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | |
| 1000 | extern void die_nmi(struct pt_regs *, const char *msg); |
| 1001 | |
Fernando Luis Vázquez Cao | 0603975 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 1002 | __kprobes int nmi_watchdog_tick(struct pt_regs * regs, unsigned reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | { |
| 1004 | |
| 1005 | /* |
| 1006 | * Since current_thread_info()-> is always on the stack, and we |
| 1007 | * always switch the stack NMI-atomically, it's safe to use |
| 1008 | * smp_processor_id(). |
| 1009 | */ |
Jesper Juhl | b791cce | 2006-03-28 01:56:52 -0800 | [diff] [blame] | 1010 | unsigned int sum; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1011 | int touched = 0; |
Jesper Juhl | b791cce | 2006-03-28 01:56:52 -0800 | [diff] [blame] | 1012 | int cpu = smp_processor_id(); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1013 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
| 1014 | u64 dummy; |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1015 | int rc=0; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1016 | |
| 1017 | /* check for other users first */ |
| 1018 | if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) |
| 1019 | == NOTIFY_STOP) { |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1020 | rc = 1; |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1021 | touched = 1; |
| 1022 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
Andrew Morton | bb81a09 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 1024 | if (cpu_isset(cpu, backtrace_mask)) { |
| 1025 | static DEFINE_SPINLOCK(lock); /* Serialise the printks */ |
| 1026 | |
| 1027 | spin_lock(&lock); |
| 1028 | printk("NMI backtrace for cpu %d\n", cpu); |
| 1029 | dump_stack(); |
| 1030 | spin_unlock(&lock); |
| 1031 | cpu_clear(cpu, backtrace_mask); |
| 1032 | } |
| 1033 | |
Thomas Gleixner | f8b5035 | 2007-02-16 01:28:09 -0800 | [diff] [blame] | 1034 | /* |
| 1035 | * Take the local apic timer and PIT/HPET into account. We don't |
| 1036 | * know which one is active, when we have highres/dyntick on |
| 1037 | */ |
| 1038 | sum = per_cpu(irq_stat, cpu).apic_timer_irqs + kstat_irqs(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
Thomas Gleixner | f8b5035 | 2007-02-16 01:28:09 -0800 | [diff] [blame] | 1040 | /* if the none of the timers isn't firing, this cpu isn't doing much */ |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1041 | if (!touched && last_irq_sums[cpu] == sum) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | /* |
| 1043 | * Ayiee, looks like this CPU is stuck ... |
| 1044 | * wait a few IRQs (5 seconds) before doing the oops ... |
| 1045 | */ |
| 1046 | alert_counter[cpu]++; |
| 1047 | if (alert_counter[cpu] == 5*nmi_hz) |
George Anzinger | 748f2ed | 2005-09-03 15:56:48 -0700 | [diff] [blame] | 1048 | /* |
| 1049 | * die_nmi will return ONLY if NOTIFY_STOP happens.. |
| 1050 | */ |
Ingo Molnar | 91368d7 | 2006-03-23 03:00:54 -0800 | [diff] [blame] | 1051 | die_nmi(regs, "BUG: NMI Watchdog detected LOCKUP"); |
GOTO Masanori | b884e25 | 2006-03-07 21:55:29 -0800 | [diff] [blame] | 1052 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | last_irq_sums[cpu] = sum; |
| 1054 | alert_counter[cpu] = 0; |
| 1055 | } |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1056 | /* see if the nmi watchdog went off */ |
| 1057 | if (wd->enabled) { |
| 1058 | if (nmi_watchdog == NMI_LOCAL_APIC) { |
| 1059 | rdmsrl(wd->perfctr_msr, dummy); |
| 1060 | if (dummy & wd->check_bit){ |
| 1061 | /* this wasn't a watchdog timer interrupt */ |
| 1062 | goto done; |
| 1063 | } |
| 1064 | |
| 1065 | /* only Intel P4 uses the cccr msr */ |
| 1066 | if (wd->cccr_msr != 0) { |
| 1067 | /* |
| 1068 | * P4 quirks: |
| 1069 | * - An overflown perfctr will assert its interrupt |
| 1070 | * until the OVF flag in its CCCR is cleared. |
| 1071 | * - LVTPC is masked on interrupt and must be |
| 1072 | * unmasked by the LVTPC handler. |
| 1073 | */ |
| 1074 | rdmsrl(wd->cccr_msr, dummy); |
| 1075 | dummy &= ~P4_CCCR_OVF; |
| 1076 | wrmsrl(wd->cccr_msr, dummy); |
| 1077 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Venkatesh Pallipadi | 90ce4bc | 2007-02-13 13:26:22 +0100 | [diff] [blame] | 1078 | /* start the cycle over again */ |
| 1079 | write_watchdog_counter(wd->perfctr_msr, NULL); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1080 | } |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 1081 | else if (wd->perfctr_msr == MSR_P6_PERFCTR0 || |
| 1082 | wd->perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0) { |
| 1083 | /* P6 based Pentium M need to re-unmask |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1084 | * the apic vector but it doesn't hurt |
Venkatesh Pallipadi | 248dcb2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 1085 | * other P6 variant. |
| 1086 | * ArchPerfom/Core Duo also needs this */ |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1087 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Venkatesh Pallipadi | 90ce4bc | 2007-02-13 13:26:22 +0100 | [diff] [blame] | 1088 | /* P6/ARCH_PERFMON has 32 bit counter write */ |
| 1089 | write_watchdog_counter32(wd->perfctr_msr, NULL); |
| 1090 | } else { |
| 1091 | /* start the cycle over again */ |
| 1092 | write_watchdog_counter(wd->perfctr_msr, NULL); |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1093 | } |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1094 | rc = 1; |
| 1095 | } else if (nmi_watchdog == NMI_IO_APIC) { |
| 1096 | /* don't know how to accurately check for this. |
| 1097 | * just assume it was a watchdog timer interrupt |
| 1098 | * This matches the old behaviour. |
| 1099 | */ |
| 1100 | rc = 1; |
Fernando Luis Vázquez Cao | 0603975 | 2006-09-26 10:52:36 +0200 | [diff] [blame] | 1101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | } |
Don Zickus | b7471c6 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1103 | done: |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1104 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 1107 | int do_nmi_callback(struct pt_regs * regs, int cpu) |
| 1108 | { |
| 1109 | #ifdef CONFIG_SYSCTL |
| 1110 | if (unknown_nmi_panic) |
| 1111 | return unknown_nmi_panic_callback(regs, cpu); |
| 1112 | #endif |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | #ifdef CONFIG_SYSCTL |
| 1117 | |
| 1118 | static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu) |
| 1119 | { |
| 1120 | unsigned char reason = get_nmi_reason(); |
| 1121 | char buf[64]; |
| 1122 | |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 1123 | sprintf(buf, "NMI received for unknown reason %02x\n", reason); |
| 1124 | die_nmi(regs, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | return 0; |
| 1126 | } |
| 1127 | |
Don Zickus | 407984f | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 1128 | /* |
Don Zickus | e33e89a | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 1129 | * proc handler for /proc/sys/kernel/nmi |
Don Zickus | 407984f | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 1130 | */ |
| 1131 | int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file, |
| 1132 | void __user *buffer, size_t *length, loff_t *ppos) |
| 1133 | { |
| 1134 | int old_state; |
| 1135 | |
| 1136 | nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0; |
| 1137 | old_state = nmi_watchdog_enabled; |
| 1138 | proc_dointvec(table, write, file, buffer, length, ppos); |
| 1139 | if (!!old_state == !!nmi_watchdog_enabled) |
| 1140 | return 0; |
| 1141 | |
| 1142 | if (atomic_read(&nmi_active) < 0) { |
Don Zickus | e33e89a | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 1143 | printk( KERN_WARNING "NMI watchdog is permanently disabled\n"); |
| 1144 | return -EIO; |
Don Zickus | 407984f | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | if (nmi_watchdog == NMI_DEFAULT) { |
| 1148 | if (nmi_known_cpu() > 0) |
| 1149 | nmi_watchdog = NMI_LOCAL_APIC; |
| 1150 | else |
| 1151 | nmi_watchdog = NMI_IO_APIC; |
| 1152 | } |
| 1153 | |
Don Zickus | e33e89a | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 1154 | if (nmi_watchdog == NMI_LOCAL_APIC) { |
Don Zickus | 407984f | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 1155 | if (nmi_watchdog_enabled) |
| 1156 | enable_lapic_nmi_watchdog(); |
| 1157 | else |
| 1158 | disable_lapic_nmi_watchdog(); |
Don Zickus | 407984f | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 1159 | } else { |
| 1160 | printk( KERN_WARNING |
| 1161 | "NMI watchdog doesn't know what hardware to touch\n"); |
| 1162 | return -EIO; |
| 1163 | } |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | #endif |
| 1168 | |
Andrew Morton | bb81a09 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 1169 | void __trigger_all_cpu_backtrace(void) |
| 1170 | { |
| 1171 | int i; |
| 1172 | |
| 1173 | backtrace_mask = cpu_online_map; |
| 1174 | /* Wait for up to 10 seconds for all CPUs to do the backtrace */ |
| 1175 | for (i = 0; i < 10 * 1000; i++) { |
| 1176 | if (cpus_empty(backtrace_mask)) |
| 1177 | break; |
| 1178 | mdelay(1); |
| 1179 | } |
| 1180 | } |
| 1181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | EXPORT_SYMBOL(nmi_active); |
| 1183 | EXPORT_SYMBOL(nmi_watchdog); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 1184 | EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi); |
| 1185 | EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit); |
| 1186 | EXPORT_SYMBOL(reserve_perfctr_nmi); |
| 1187 | EXPORT_SYMBOL(release_perfctr_nmi); |
| 1188 | EXPORT_SYMBOL(reserve_evntsel_nmi); |
| 1189 | EXPORT_SYMBOL(release_evntsel_nmi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | EXPORT_SYMBOL(disable_timer_nmi_watchdog); |
| 1191 | EXPORT_SYMBOL(enable_timer_nmi_watchdog); |