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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/mm.h> |
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/sysdev.h> |
| 20 | #include <linux/nmi.h> |
| 21 | #include <linux/sysctl.h> |
Andi Kleen | eddb6fb | 2006-02-03 21:50:41 +0100 | [diff] [blame] | 22 | #include <linux/kprobes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include <asm/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/nmi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/proto.h> |
| 27 | #include <asm/kdebug.h> |
Andi Kleen | 553f265 | 2006-04-07 19:49:57 +0200 | [diff] [blame] | 28 | #include <asm/mce.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame^] | 30 | /* perfctr_nmi_owner tracks the ownership of the perfctr registers: |
| 31 | * evtsel_nmi_owner tracks the ownership of the event selection |
| 32 | * - different performance counters/ event selection may be reserved for |
| 33 | * different subsystems this reservation system just tries to coordinate |
| 34 | * things a little |
| 35 | */ |
| 36 | static DEFINE_PER_CPU(unsigned, perfctr_nmi_owner); |
| 37 | static DEFINE_PER_CPU(unsigned, evntsel_nmi_owner[2]); |
| 38 | |
| 39 | /* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's |
| 40 | * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now) |
| 41 | */ |
| 42 | #define NMI_MAX_COUNTER_BITS 66 |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | /* |
| 45 | * lapic_nmi_owner tracks the ownership of the lapic NMI hardware: |
| 46 | * - it may be reserved by some other driver, or not |
| 47 | * - when not reserved by some other driver, it may be used for |
| 48 | * the NMI watchdog, or not |
| 49 | * |
| 50 | * This is maintained separately from nmi_active because the NMI |
| 51 | * watchdog may also be driven from the I/O APIC timer. |
| 52 | */ |
| 53 | static DEFINE_SPINLOCK(lapic_nmi_owner_lock); |
| 54 | static unsigned int lapic_nmi_owner; |
| 55 | #define LAPIC_NMI_WATCHDOG (1<<0) |
| 56 | #define LAPIC_NMI_RESERVED (1<<1) |
| 57 | |
| 58 | /* nmi_active: |
| 59 | * +1: the lapic NMI watchdog is active, but can be disabled |
| 60 | * 0: the lapic NMI watchdog has not been set up, and cannot |
| 61 | * be enabled |
| 62 | * -1: the lapic NMI watchdog is disabled, but can be enabled |
| 63 | */ |
| 64 | int nmi_active; /* oprofile uses this */ |
| 65 | int panic_on_timeout; |
| 66 | |
| 67 | unsigned int nmi_watchdog = NMI_DEFAULT; |
| 68 | static unsigned int nmi_hz = HZ; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 69 | static unsigned int nmi_perfctr_msr; /* the MSR to reset in NMI handler */ |
| 70 | static unsigned int nmi_p4_cccr_val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | /* Note that these events don't tick when the CPU idles. This means |
| 73 | the frequency varies with CPU load. */ |
| 74 | |
| 75 | #define K7_EVNTSEL_ENABLE (1 << 22) |
| 76 | #define K7_EVNTSEL_INT (1 << 20) |
| 77 | #define K7_EVNTSEL_OS (1 << 17) |
| 78 | #define K7_EVNTSEL_USR (1 << 16) |
| 79 | #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76 |
| 80 | #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING |
| 81 | |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 82 | #define MSR_P4_MISC_ENABLE 0x1A0 |
| 83 | #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7) |
| 84 | #define MSR_P4_MISC_ENABLE_PEBS_UNAVAIL (1<<12) |
| 85 | #define MSR_P4_PERFCTR0 0x300 |
| 86 | #define MSR_P4_CCCR0 0x360 |
| 87 | #define P4_ESCR_EVENT_SELECT(N) ((N)<<25) |
| 88 | #define P4_ESCR_OS (1<<3) |
| 89 | #define P4_ESCR_USR (1<<2) |
| 90 | #define P4_CCCR_OVF_PMI0 (1<<26) |
| 91 | #define P4_CCCR_OVF_PMI1 (1<<27) |
| 92 | #define P4_CCCR_THRESHOLD(N) ((N)<<20) |
| 93 | #define P4_CCCR_COMPLEMENT (1<<19) |
| 94 | #define P4_CCCR_COMPARE (1<<18) |
| 95 | #define P4_CCCR_REQUIRED (3<<16) |
| 96 | #define P4_CCCR_ESCR_SELECT(N) ((N)<<13) |
| 97 | #define P4_CCCR_ENABLE (1<<12) |
| 98 | /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter |
| 99 | CRU_ESCR0 (with any non-null event selector) through a complemented |
| 100 | max threshold. [IA32-Vol3, Section 14.9.9] */ |
| 101 | #define MSR_P4_IQ_COUNTER0 0x30C |
| 102 | #define P4_NMI_CRU_ESCR0 (P4_ESCR_EVENT_SELECT(0x3F)|P4_ESCR_OS|P4_ESCR_USR) |
| 103 | #define P4_NMI_IQ_CCCR0 \ |
| 104 | (P4_CCCR_OVF_PMI0|P4_CCCR_THRESHOLD(15)|P4_CCCR_COMPLEMENT| \ |
| 105 | P4_CCCR_COMPARE|P4_CCCR_REQUIRED|P4_CCCR_ESCR_SELECT(4)|P4_CCCR_ENABLE) |
| 106 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame^] | 107 | /* converts an msr to an appropriate reservation bit */ |
| 108 | static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr) |
| 109 | { |
| 110 | /* returns the bit offset of the performance counter register */ |
| 111 | switch (boot_cpu_data.x86_vendor) { |
| 112 | case X86_VENDOR_AMD: |
| 113 | return (msr - MSR_K7_PERFCTR0); |
| 114 | case X86_VENDOR_INTEL: |
| 115 | return (msr - MSR_P4_BPU_PERFCTR0); |
| 116 | } |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /* converts an msr to an appropriate reservation bit */ |
| 121 | static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr) |
| 122 | { |
| 123 | /* returns the bit offset of the event selection register */ |
| 124 | switch (boot_cpu_data.x86_vendor) { |
| 125 | case X86_VENDOR_AMD: |
| 126 | return (msr - MSR_K7_EVNTSEL0); |
| 127 | case X86_VENDOR_INTEL: |
| 128 | return (msr - MSR_P4_BSU_ESCR0); |
| 129 | } |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /* checks for a bit availability (hack for oprofile) */ |
| 134 | int avail_to_resrv_perfctr_nmi_bit(unsigned int counter) |
| 135 | { |
| 136 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 137 | |
| 138 | return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner))); |
| 139 | } |
| 140 | |
| 141 | /* checks the an msr for availability */ |
| 142 | int avail_to_resrv_perfctr_nmi(unsigned int msr) |
| 143 | { |
| 144 | unsigned int counter; |
| 145 | |
| 146 | counter = nmi_perfctr_msr_to_bit(msr); |
| 147 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 148 | |
| 149 | return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner))); |
| 150 | } |
| 151 | |
| 152 | int reserve_perfctr_nmi(unsigned int msr) |
| 153 | { |
| 154 | unsigned int counter; |
| 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, &__get_cpu_var(perfctr_nmi_owner))) |
| 160 | return 1; |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | void release_perfctr_nmi(unsigned int msr) |
| 165 | { |
| 166 | unsigned int counter; |
| 167 | |
| 168 | counter = nmi_perfctr_msr_to_bit(msr); |
| 169 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 170 | |
| 171 | clear_bit(counter, &__get_cpu_var(perfctr_nmi_owner)); |
| 172 | } |
| 173 | |
| 174 | int reserve_evntsel_nmi(unsigned int msr) |
| 175 | { |
| 176 | unsigned int counter; |
| 177 | |
| 178 | counter = nmi_evntsel_msr_to_bit(msr); |
| 179 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 180 | |
| 181 | if (!test_and_set_bit(counter, &__get_cpu_var(evntsel_nmi_owner))) |
| 182 | return 1; |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | void release_evntsel_nmi(unsigned int msr) |
| 187 | { |
| 188 | unsigned int counter; |
| 189 | |
| 190 | counter = nmi_evntsel_msr_to_bit(msr); |
| 191 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 192 | |
| 193 | clear_bit(counter, &__get_cpu_var(evntsel_nmi_owner)); |
| 194 | } |
| 195 | |
Ashok Raj | e6982c6 | 2005-06-25 14:54:58 -0700 | [diff] [blame] | 196 | static __cpuinit inline int nmi_known_cpu(void) |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 197 | { |
| 198 | switch (boot_cpu_data.x86_vendor) { |
| 199 | case X86_VENDOR_AMD: |
| 200 | return boot_cpu_data.x86 == 15; |
| 201 | case X86_VENDOR_INTEL: |
Andi Kleen | b07f891 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 202 | return boot_cpu_data.x86 == 15; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 203 | } |
| 204 | return 0; |
| 205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
| 207 | /* Run after command line and cpu_init init, but before all other checks */ |
Ashok Raj | e6982c6 | 2005-06-25 14:54:58 -0700 | [diff] [blame] | 208 | void __cpuinit nmi_watchdog_default(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
| 210 | if (nmi_watchdog != NMI_DEFAULT) |
| 211 | return; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 212 | if (nmi_known_cpu()) |
| 213 | nmi_watchdog = NMI_LOCAL_APIC; |
| 214 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | nmi_watchdog = NMI_IO_APIC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 218 | #ifdef CONFIG_SMP |
| 219 | /* The performance counters used by NMI_LOCAL_APIC don't trigger when |
| 220 | * the CPU is idle. To make sure the NMI watchdog really ticks on all |
| 221 | * CPUs during the test make them busy. |
| 222 | */ |
| 223 | static __init void nmi_cpu_busy(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 225 | volatile int *endflag = data; |
Ingo Molnar | 366c7f5 | 2006-07-03 00:25:25 -0700 | [diff] [blame] | 226 | local_irq_enable_in_hardirq(); |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 227 | /* Intentionally don't use cpu_relax here. This is |
| 228 | to make sure that the performance counter really ticks, |
| 229 | even if there is a simulator or similar that catches the |
| 230 | pause instruction. On a real HT machine this is fine because |
| 231 | all other CPUs are busy with "useless" delay loops and don't |
| 232 | care if they get somewhat less cycles. */ |
| 233 | while (*endflag == 0) |
| 234 | barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 236 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 238 | int __init check_nmi_watchdog (void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 240 | volatile int endflag = 0; |
Andi Kleen | ac6b931 | 2005-05-16 21:53:19 -0700 | [diff] [blame] | 241 | int *counts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | int cpu; |
| 243 | |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 244 | counts = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL); |
| 245 | if (!counts) |
| 246 | return -1; |
Jack F Vogel | 67701ae | 2005-05-01 08:58:48 -0700 | [diff] [blame] | 247 | |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 248 | printk(KERN_INFO "testing NMI watchdog ... "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Andi Kleen | 7554c3f | 2006-01-11 22:45:45 +0100 | [diff] [blame] | 250 | #ifdef CONFIG_SMP |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 251 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 252 | smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0); |
Andi Kleen | 7554c3f | 2006-01-11 22:45:45 +0100 | [diff] [blame] | 253 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | for (cpu = 0; cpu < NR_CPUS; cpu++) |
Ravikiran G Thirumalai | df79efd | 2006-01-11 22:45:39 +0100 | [diff] [blame] | 256 | counts[cpu] = cpu_pda(cpu)->__nmi_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | local_irq_enable(); |
| 258 | mdelay((10*1000)/nmi_hz); // wait 10 ticks |
| 259 | |
Andrew Morton | 394e390 | 2006-03-23 03:01:05 -0800 | [diff] [blame] | 260 | for_each_online_cpu(cpu) { |
Ravikiran G Thirumalai | df79efd | 2006-01-11 22:45:39 +0100 | [diff] [blame] | 261 | if (cpu_pda(cpu)->__nmi_count - counts[cpu] <= 5) { |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 262 | endflag = 1; |
| 263 | printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | cpu, |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 265 | counts[cpu], |
Ravikiran G Thirumalai | df79efd | 2006-01-11 22:45:39 +0100 | [diff] [blame] | 266 | cpu_pda(cpu)->__nmi_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | nmi_active = 0; |
| 268 | lapic_nmi_owner &= ~LAPIC_NMI_WATCHDOG; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 269 | nmi_perfctr_msr = 0; |
Andi Kleen | ac6b931 | 2005-05-16 21:53:19 -0700 | [diff] [blame] | 270 | kfree(counts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | return -1; |
| 272 | } |
| 273 | } |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 274 | endflag = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | printk("OK.\n"); |
| 276 | |
| 277 | /* now that we know it works we can reduce NMI frequency to |
| 278 | something more reasonable; makes a difference in some configs */ |
| 279 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 280 | nmi_hz = 1; |
| 281 | |
Andi Kleen | ac6b931 | 2005-05-16 21:53:19 -0700 | [diff] [blame] | 282 | kfree(counts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | int __init setup_nmi_watchdog(char *str) |
| 287 | { |
| 288 | int nmi; |
| 289 | |
| 290 | if (!strncmp(str,"panic",5)) { |
| 291 | panic_on_timeout = 1; |
| 292 | str = strchr(str, ','); |
| 293 | if (!str) |
| 294 | return 1; |
| 295 | ++str; |
| 296 | } |
| 297 | |
| 298 | get_option(&str, &nmi); |
| 299 | |
| 300 | if (nmi >= NMI_INVALID) |
| 301 | return 0; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 302 | nmi_watchdog = nmi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | return 1; |
| 304 | } |
| 305 | |
| 306 | __setup("nmi_watchdog=", setup_nmi_watchdog); |
| 307 | |
| 308 | static void disable_lapic_nmi_watchdog(void) |
| 309 | { |
| 310 | if (nmi_active <= 0) |
| 311 | return; |
| 312 | switch (boot_cpu_data.x86_vendor) { |
| 313 | case X86_VENDOR_AMD: |
| 314 | wrmsr(MSR_K7_EVNTSEL0, 0, 0); |
| 315 | break; |
| 316 | case X86_VENDOR_INTEL: |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 317 | if (boot_cpu_data.x86 == 15) { |
| 318 | wrmsr(MSR_P4_IQ_CCCR0, 0, 0); |
| 319 | wrmsr(MSR_P4_CRU_ESCR0, 0, 0); |
| 320 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | break; |
| 322 | } |
| 323 | nmi_active = -1; |
| 324 | /* tell do_nmi() and others that we're not active any more */ |
| 325 | nmi_watchdog = 0; |
| 326 | } |
| 327 | |
| 328 | static void enable_lapic_nmi_watchdog(void) |
| 329 | { |
| 330 | if (nmi_active < 0) { |
| 331 | nmi_watchdog = NMI_LOCAL_APIC; |
Jan Beulich | 99019e9 | 2006-02-16 23:41:55 +0100 | [diff] [blame] | 332 | touch_nmi_watchdog(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | setup_apic_nmi_watchdog(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | int reserve_lapic_nmi(void) |
| 338 | { |
| 339 | unsigned int old_owner; |
| 340 | |
| 341 | spin_lock(&lapic_nmi_owner_lock); |
| 342 | old_owner = lapic_nmi_owner; |
| 343 | lapic_nmi_owner |= LAPIC_NMI_RESERVED; |
| 344 | spin_unlock(&lapic_nmi_owner_lock); |
| 345 | if (old_owner & LAPIC_NMI_RESERVED) |
| 346 | return -EBUSY; |
| 347 | if (old_owner & LAPIC_NMI_WATCHDOG) |
| 348 | disable_lapic_nmi_watchdog(); |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | void release_lapic_nmi(void) |
| 353 | { |
| 354 | unsigned int new_owner; |
| 355 | |
| 356 | spin_lock(&lapic_nmi_owner_lock); |
| 357 | new_owner = lapic_nmi_owner & ~LAPIC_NMI_RESERVED; |
| 358 | lapic_nmi_owner = new_owner; |
| 359 | spin_unlock(&lapic_nmi_owner_lock); |
| 360 | if (new_owner & LAPIC_NMI_WATCHDOG) |
| 361 | enable_lapic_nmi_watchdog(); |
| 362 | } |
| 363 | |
| 364 | void disable_timer_nmi_watchdog(void) |
| 365 | { |
| 366 | if ((nmi_watchdog != NMI_IO_APIC) || (nmi_active <= 0)) |
| 367 | return; |
| 368 | |
| 369 | disable_irq(0); |
| 370 | unset_nmi_callback(); |
| 371 | nmi_active = -1; |
| 372 | nmi_watchdog = NMI_NONE; |
| 373 | } |
| 374 | |
| 375 | void enable_timer_nmi_watchdog(void) |
| 376 | { |
| 377 | if (nmi_active < 0) { |
| 378 | nmi_watchdog = NMI_IO_APIC; |
| 379 | touch_nmi_watchdog(); |
| 380 | nmi_active = 1; |
| 381 | enable_irq(0); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | #ifdef CONFIG_PM |
| 386 | |
| 387 | static int nmi_pm_active; /* nmi_active before suspend */ |
| 388 | |
Pavel Machek | 829ca9a | 2005-09-03 15:56:56 -0700 | [diff] [blame] | 389 | 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] | 390 | { |
| 391 | nmi_pm_active = nmi_active; |
| 392 | disable_lapic_nmi_watchdog(); |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int lapic_nmi_resume(struct sys_device *dev) |
| 397 | { |
| 398 | if (nmi_pm_active > 0) |
| 399 | enable_lapic_nmi_watchdog(); |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static struct sysdev_class nmi_sysclass = { |
| 404 | set_kset_name("lapic_nmi"), |
| 405 | .resume = lapic_nmi_resume, |
| 406 | .suspend = lapic_nmi_suspend, |
| 407 | }; |
| 408 | |
| 409 | static struct sys_device device_lapic_nmi = { |
| 410 | .id = 0, |
| 411 | .cls = &nmi_sysclass, |
| 412 | }; |
| 413 | |
| 414 | static int __init init_lapic_nmi_sysfs(void) |
| 415 | { |
| 416 | int error; |
| 417 | |
| 418 | if (nmi_active == 0 || nmi_watchdog != NMI_LOCAL_APIC) |
| 419 | return 0; |
| 420 | |
| 421 | error = sysdev_class_register(&nmi_sysclass); |
| 422 | if (!error) |
| 423 | error = sysdev_register(&device_lapic_nmi); |
| 424 | return error; |
| 425 | } |
| 426 | /* must come after the local APIC's device_initcall() */ |
| 427 | late_initcall(init_lapic_nmi_sysfs); |
| 428 | |
| 429 | #endif /* CONFIG_PM */ |
| 430 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame^] | 431 | static int setup_k7_watchdog(void) |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 432 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | unsigned int evntsel; |
| 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | nmi_perfctr_msr = MSR_K7_PERFCTR0; |
| 436 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame^] | 437 | if (!reserve_perfctr_nmi(nmi_perfctr_msr)) |
| 438 | goto fail; |
| 439 | |
| 440 | if (!reserve_evntsel_nmi(MSR_K7_EVNTSEL0)) |
| 441 | goto fail1; |
| 442 | |
| 443 | /* Simulator may not support it */ |
| 444 | if (checking_wrmsrl(MSR_K7_EVNTSEL0, 0UL)) |
| 445 | goto fail2; |
| 446 | wrmsrl(MSR_K7_PERFCTR0, 0UL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
| 448 | evntsel = K7_EVNTSEL_INT |
| 449 | | K7_EVNTSEL_OS |
| 450 | | K7_EVNTSEL_USR |
| 451 | | K7_NMI_EVENT; |
| 452 | |
| 453 | wrmsr(MSR_K7_EVNTSEL0, evntsel, 0); |
Jan Beulich | 42ac8ff | 2005-09-13 01:25:51 -0700 | [diff] [blame] | 454 | wrmsrl(MSR_K7_PERFCTR0, -((u64)cpu_khz * 1000 / nmi_hz)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 456 | evntsel |= K7_EVNTSEL_ENABLE; |
| 457 | wrmsr(MSR_K7_EVNTSEL0, evntsel, 0); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame^] | 458 | return 1; |
| 459 | fail2: |
| 460 | release_evntsel_nmi(MSR_K7_EVNTSEL0); |
| 461 | fail1: |
| 462 | release_perfctr_nmi(nmi_perfctr_msr); |
| 463 | fail: |
| 464 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 467 | |
| 468 | static int setup_p4_watchdog(void) |
| 469 | { |
| 470 | unsigned int misc_enable, dummy; |
| 471 | |
| 472 | rdmsr(MSR_P4_MISC_ENABLE, misc_enable, dummy); |
| 473 | if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL)) |
| 474 | return 0; |
| 475 | |
| 476 | nmi_perfctr_msr = MSR_P4_IQ_COUNTER0; |
| 477 | nmi_p4_cccr_val = P4_NMI_IQ_CCCR0; |
| 478 | #ifdef CONFIG_SMP |
| 479 | if (smp_num_siblings == 2) |
| 480 | nmi_p4_cccr_val |= P4_CCCR_OVF_PMI1; |
| 481 | #endif |
| 482 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame^] | 483 | if (!reserve_perfctr_nmi(nmi_perfctr_msr)) |
| 484 | goto fail; |
| 485 | |
| 486 | if (!reserve_evntsel_nmi(MSR_P4_CRU_ESCR0)) |
| 487 | goto fail1; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 488 | |
| 489 | wrmsr(MSR_P4_CRU_ESCR0, P4_NMI_CRU_ESCR0, 0); |
| 490 | wrmsr(MSR_P4_IQ_CCCR0, P4_NMI_IQ_CCCR0 & ~P4_CCCR_ENABLE, 0); |
Jan Beulich | 42ac8ff | 2005-09-13 01:25:51 -0700 | [diff] [blame] | 491 | Dprintk("setting P4_IQ_COUNTER0 to 0x%08lx\n", -(cpu_khz * 1000UL / nmi_hz)); |
| 492 | wrmsrl(MSR_P4_IQ_COUNTER0, -((u64)cpu_khz * 1000 / nmi_hz)); |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 493 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 494 | wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0); |
| 495 | return 1; |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame^] | 496 | fail1: |
| 497 | release_perfctr_nmi(nmi_perfctr_msr); |
| 498 | fail: |
| 499 | return 0; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | void setup_apic_nmi_watchdog(void) |
| 503 | { |
| 504 | switch (boot_cpu_data.x86_vendor) { |
| 505 | case X86_VENDOR_AMD: |
Andi Kleen | 72e76be | 2005-04-16 15:25:07 -0700 | [diff] [blame] | 506 | if (boot_cpu_data.x86 != 15) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | return; |
| 508 | if (strstr(boot_cpu_data.x86_model_id, "Screwdriver")) |
| 509 | return; |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame^] | 510 | if (!setup_k7_watchdog()) |
| 511 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | break; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 513 | case X86_VENDOR_INTEL: |
Andi Kleen | b07f891 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 514 | if (boot_cpu_data.x86 != 15) |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 515 | return; |
Andi Kleen | b07f891 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 516 | if (!setup_p4_watchdog()) |
| 517 | return; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 518 | break; |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | default: |
| 521 | return; |
| 522 | } |
| 523 | lapic_nmi_owner = LAPIC_NMI_WATCHDOG; |
| 524 | nmi_active = 1; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * the best way to detect whether a CPU has a 'hard lockup' problem |
| 529 | * is to check it's local APIC timer IRQ counts. If they are not |
| 530 | * changing then that CPU has some problem. |
| 531 | * |
| 532 | * as these watchdog NMI IRQs are generated on every CPU, we only |
| 533 | * have to check the current processor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | */ |
| 535 | |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 536 | static DEFINE_PER_CPU(unsigned, last_irq_sum); |
| 537 | static DEFINE_PER_CPU(local_t, alert_counter); |
| 538 | static DEFINE_PER_CPU(int, nmi_touch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
| 540 | void touch_nmi_watchdog (void) |
| 541 | { |
Jan Beulich | 99019e9 | 2006-02-16 23:41:55 +0100 | [diff] [blame] | 542 | if (nmi_watchdog > 0) { |
| 543 | unsigned cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
Jan Beulich | 99019e9 | 2006-02-16 23:41:55 +0100 | [diff] [blame] | 545 | /* |
| 546 | * Tell other CPUs to reset their alert counters. We cannot |
| 547 | * do it ourselves because the alert count increase is not |
| 548 | * atomic. |
| 549 | */ |
| 550 | for_each_present_cpu (cpu) |
| 551 | per_cpu(nmi_touch, cpu) = 1; |
| 552 | } |
Ingo Molnar | 8446f1d | 2005-09-06 15:16:27 -0700 | [diff] [blame] | 553 | |
| 554 | touch_softlockup_watchdog(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Andi Kleen | eddb6fb | 2006-02-03 21:50:41 +0100 | [diff] [blame] | 557 | void __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | { |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 559 | int sum; |
| 560 | int touched = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | sum = read_pda(apic_timer_irqs); |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 563 | if (__get_cpu_var(nmi_touch)) { |
| 564 | __get_cpu_var(nmi_touch) = 0; |
| 565 | touched = 1; |
| 566 | } |
Andi Kleen | 553f265 | 2006-04-07 19:49:57 +0200 | [diff] [blame] | 567 | #ifdef CONFIG_X86_MCE |
| 568 | /* Could check oops_in_progress here too, but it's safer |
| 569 | not too */ |
| 570 | if (atomic_read(&mce_entry) > 0) |
| 571 | touched = 1; |
| 572 | #endif |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 573 | if (!touched && __get_cpu_var(last_irq_sum) == sum) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | /* |
| 575 | * Ayiee, looks like this CPU is stuck ... |
| 576 | * wait a few IRQs (5 seconds) before doing the oops ... |
| 577 | */ |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 578 | local_inc(&__get_cpu_var(alert_counter)); |
| 579 | if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) |
| 581 | == NOTIFY_STOP) { |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 582 | local_set(&__get_cpu_var(alert_counter), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | return; |
Chuck Ebbert | 8478157 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 584 | } |
| 585 | die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |
| 587 | } else { |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 588 | __get_cpu_var(last_irq_sum) = sum; |
| 589 | local_set(&__get_cpu_var(alert_counter), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 591 | if (nmi_perfctr_msr) { |
| 592 | if (nmi_perfctr_msr == MSR_P4_IQ_COUNTER0) { |
| 593 | /* |
| 594 | * P4 quirks: |
| 595 | * - An overflown perfctr will assert its interrupt |
| 596 | * until the OVF flag in its CCCR is cleared. |
| 597 | * - LVTPC is masked on interrupt and must be |
| 598 | * unmasked by the LVTPC handler. |
| 599 | */ |
| 600 | wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0); |
| 601 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Andi Kleen | b07f891 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 602 | } |
Jan Beulich | 42ac8ff | 2005-09-13 01:25:51 -0700 | [diff] [blame] | 603 | wrmsrl(nmi_perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz)); |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 604 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Andi Kleen | eddb6fb | 2006-02-03 21:50:41 +0100 | [diff] [blame] | 607 | static __kprobes int dummy_nmi_callback(struct pt_regs * regs, int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | { |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | static nmi_callback_t nmi_callback = dummy_nmi_callback; |
| 613 | |
Andi Kleen | eddb6fb | 2006-02-03 21:50:41 +0100 | [diff] [blame] | 614 | asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | { |
| 616 | int cpu = safe_smp_processor_id(); |
| 617 | |
| 618 | nmi_enter(); |
| 619 | add_pda(__nmi_count,1); |
Paul E. McKenney | 1930605 | 2005-09-06 15:16:35 -0700 | [diff] [blame] | 620 | if (!rcu_dereference(nmi_callback)(regs, cpu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | default_do_nmi(regs); |
| 622 | nmi_exit(); |
| 623 | } |
| 624 | |
| 625 | void set_nmi_callback(nmi_callback_t callback) |
| 626 | { |
Jan Beulich | 8c914cb | 2006-03-25 16:29:40 +0100 | [diff] [blame] | 627 | vmalloc_sync_all(); |
Paul E. McKenney | 1930605 | 2005-09-06 15:16:35 -0700 | [diff] [blame] | 628 | rcu_assign_pointer(nmi_callback, callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } |
Andrew Morton | d9a5685 | 2006-06-28 04:27:04 -0700 | [diff] [blame] | 630 | EXPORT_SYMBOL_GPL(set_nmi_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | void unset_nmi_callback(void) |
| 633 | { |
| 634 | nmi_callback = dummy_nmi_callback; |
| 635 | } |
Andrew Morton | d9a5685 | 2006-06-28 04:27:04 -0700 | [diff] [blame] | 636 | EXPORT_SYMBOL_GPL(unset_nmi_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
| 638 | #ifdef CONFIG_SYSCTL |
| 639 | |
| 640 | static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu) |
| 641 | { |
| 642 | unsigned char reason = get_nmi_reason(); |
| 643 | char buf[64]; |
| 644 | |
| 645 | if (!(reason & 0xc0)) { |
| 646 | sprintf(buf, "NMI received for unknown reason %02x\n", reason); |
| 647 | die_nmi(buf,regs); |
| 648 | } |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * proc handler for /proc/sys/kernel/unknown_nmi_panic |
| 654 | */ |
| 655 | int proc_unknown_nmi_panic(struct ctl_table *table, int write, struct file *file, |
| 656 | void __user *buffer, size_t *length, loff_t *ppos) |
| 657 | { |
| 658 | int old_state; |
| 659 | |
| 660 | old_state = unknown_nmi_panic; |
| 661 | proc_dointvec(table, write, file, buffer, length, ppos); |
| 662 | if (!!old_state == !!unknown_nmi_panic) |
| 663 | return 0; |
| 664 | |
| 665 | if (unknown_nmi_panic) { |
| 666 | if (reserve_lapic_nmi() < 0) { |
| 667 | unknown_nmi_panic = 0; |
| 668 | return -EBUSY; |
| 669 | } else { |
| 670 | set_nmi_callback(unknown_nmi_panic_callback); |
| 671 | } |
| 672 | } else { |
| 673 | release_lapic_nmi(); |
| 674 | unset_nmi_callback(); |
| 675 | } |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | #endif |
| 680 | |
| 681 | EXPORT_SYMBOL(nmi_active); |
| 682 | EXPORT_SYMBOL(nmi_watchdog); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame^] | 683 | EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi); |
| 684 | EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit); |
| 685 | EXPORT_SYMBOL(reserve_perfctr_nmi); |
| 686 | EXPORT_SYMBOL(release_perfctr_nmi); |
| 687 | EXPORT_SYMBOL(reserve_evntsel_nmi); |
| 688 | EXPORT_SYMBOL(release_evntsel_nmi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | EXPORT_SYMBOL(reserve_lapic_nmi); |
| 690 | EXPORT_SYMBOL(release_lapic_nmi); |
| 691 | EXPORT_SYMBOL(disable_timer_nmi_watchdog); |
| 692 | EXPORT_SYMBOL(enable_timer_nmi_watchdog); |
| 693 | EXPORT_SYMBOL(touch_nmi_watchdog); |