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