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 | /* nmi_active: |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 45 | * >0: the lapic NMI watchdog is active, but can be disabled |
| 46 | * <0: the lapic NMI watchdog has not been set up, and cannot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | * be enabled |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 48 | * 0: the lapic NMI watchdog is disabled, but can be enabled |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | */ |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 50 | atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | int panic_on_timeout; |
| 52 | |
| 53 | unsigned int nmi_watchdog = NMI_DEFAULT; |
| 54 | static unsigned int nmi_hz = HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 56 | struct nmi_watchdog_ctlblk { |
| 57 | int enabled; |
| 58 | u64 check_bit; |
| 59 | unsigned int cccr_msr; |
| 60 | unsigned int perfctr_msr; /* the MSR to reset in NMI handler */ |
| 61 | unsigned int evntsel_msr; /* the MSR to select the events to handle */ |
| 62 | }; |
| 63 | static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 65 | /* local prototypes */ |
| 66 | static void stop_apic_nmi_watchdog(void *unused); |
| 67 | static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu); |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 68 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 69 | /* converts an msr to an appropriate reservation bit */ |
| 70 | static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr) |
| 71 | { |
| 72 | /* returns the bit offset of the performance counter register */ |
| 73 | switch (boot_cpu_data.x86_vendor) { |
| 74 | case X86_VENDOR_AMD: |
| 75 | return (msr - MSR_K7_PERFCTR0); |
| 76 | case X86_VENDOR_INTEL: |
| 77 | return (msr - MSR_P4_BPU_PERFCTR0); |
| 78 | } |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | /* converts an msr to an appropriate reservation bit */ |
| 83 | static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr) |
| 84 | { |
| 85 | /* returns the bit offset of the event selection register */ |
| 86 | switch (boot_cpu_data.x86_vendor) { |
| 87 | case X86_VENDOR_AMD: |
| 88 | return (msr - MSR_K7_EVNTSEL0); |
| 89 | case X86_VENDOR_INTEL: |
| 90 | return (msr - MSR_P4_BSU_ESCR0); |
| 91 | } |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /* checks for a bit availability (hack for oprofile) */ |
| 96 | int avail_to_resrv_perfctr_nmi_bit(unsigned int counter) |
| 97 | { |
| 98 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 99 | |
| 100 | return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner))); |
| 101 | } |
| 102 | |
| 103 | /* checks the an msr for availability */ |
| 104 | int avail_to_resrv_perfctr_nmi(unsigned int msr) |
| 105 | { |
| 106 | unsigned int counter; |
| 107 | |
| 108 | counter = nmi_perfctr_msr_to_bit(msr); |
| 109 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 110 | |
| 111 | return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner))); |
| 112 | } |
| 113 | |
| 114 | int reserve_perfctr_nmi(unsigned int msr) |
| 115 | { |
| 116 | unsigned int counter; |
| 117 | |
| 118 | counter = nmi_perfctr_msr_to_bit(msr); |
| 119 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 120 | |
| 121 | if (!test_and_set_bit(counter, &__get_cpu_var(perfctr_nmi_owner))) |
| 122 | return 1; |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | void release_perfctr_nmi(unsigned int msr) |
| 127 | { |
| 128 | unsigned int counter; |
| 129 | |
| 130 | counter = nmi_perfctr_msr_to_bit(msr); |
| 131 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 132 | |
| 133 | clear_bit(counter, &__get_cpu_var(perfctr_nmi_owner)); |
| 134 | } |
| 135 | |
| 136 | int reserve_evntsel_nmi(unsigned int msr) |
| 137 | { |
| 138 | unsigned int counter; |
| 139 | |
| 140 | counter = nmi_evntsel_msr_to_bit(msr); |
| 141 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 142 | |
| 143 | if (!test_and_set_bit(counter, &__get_cpu_var(evntsel_nmi_owner))) |
| 144 | return 1; |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | void release_evntsel_nmi(unsigned int msr) |
| 149 | { |
| 150 | unsigned int counter; |
| 151 | |
| 152 | counter = nmi_evntsel_msr_to_bit(msr); |
| 153 | BUG_ON(counter > NMI_MAX_COUNTER_BITS); |
| 154 | |
| 155 | clear_bit(counter, &__get_cpu_var(evntsel_nmi_owner)); |
| 156 | } |
| 157 | |
Ashok Raj | e6982c6 | 2005-06-25 14:54:58 -0700 | [diff] [blame] | 158 | static __cpuinit inline int nmi_known_cpu(void) |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 159 | { |
| 160 | switch (boot_cpu_data.x86_vendor) { |
| 161 | case X86_VENDOR_AMD: |
| 162 | return boot_cpu_data.x86 == 15; |
| 163 | case X86_VENDOR_INTEL: |
Andi Kleen | b07f891 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 164 | return boot_cpu_data.x86 == 15; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 165 | } |
| 166 | return 0; |
| 167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | /* 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] | 170 | void __cpuinit nmi_watchdog_default(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
| 172 | if (nmi_watchdog != NMI_DEFAULT) |
| 173 | return; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 174 | if (nmi_known_cpu()) |
| 175 | nmi_watchdog = NMI_LOCAL_APIC; |
| 176 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | nmi_watchdog = NMI_IO_APIC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 180 | #ifdef CONFIG_SMP |
| 181 | /* The performance counters used by NMI_LOCAL_APIC don't trigger when |
| 182 | * the CPU is idle. To make sure the NMI watchdog really ticks on all |
| 183 | * CPUs during the test make them busy. |
| 184 | */ |
| 185 | static __init void nmi_cpu_busy(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 187 | volatile int *endflag = data; |
Ingo Molnar | 366c7f5 | 2006-07-03 00:25:25 -0700 | [diff] [blame] | 188 | local_irq_enable_in_hardirq(); |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 189 | /* Intentionally don't use cpu_relax here. This is |
| 190 | to make sure that the performance counter really ticks, |
| 191 | even if there is a simulator or similar that catches the |
| 192 | pause instruction. On a real HT machine this is fine because |
| 193 | all other CPUs are busy with "useless" delay loops and don't |
| 194 | care if they get somewhat less cycles. */ |
| 195 | while (*endflag == 0) |
| 196 | barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 198 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 200 | int __init check_nmi_watchdog (void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 202 | volatile int endflag = 0; |
Andi Kleen | ac6b931 | 2005-05-16 21:53:19 -0700 | [diff] [blame] | 203 | int *counts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | int cpu; |
| 205 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 206 | if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT)) |
| 207 | return 0; |
| 208 | |
| 209 | if (!atomic_read(&nmi_active)) |
| 210 | return 0; |
| 211 | |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 212 | counts = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL); |
| 213 | if (!counts) |
| 214 | return -1; |
Jack F Vogel | 67701ae | 2005-05-01 08:58:48 -0700 | [diff] [blame] | 215 | |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 216 | printk(KERN_INFO "testing NMI watchdog ... "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Andi Kleen | 7554c3f | 2006-01-11 22:45:45 +0100 | [diff] [blame] | 218 | #ifdef CONFIG_SMP |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 219 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 220 | smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0); |
Andi Kleen | 7554c3f | 2006-01-11 22:45:45 +0100 | [diff] [blame] | 221 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | for (cpu = 0; cpu < NR_CPUS; cpu++) |
Ravikiran G Thirumalai | df79efd | 2006-01-11 22:45:39 +0100 | [diff] [blame] | 224 | counts[cpu] = cpu_pda(cpu)->__nmi_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | local_irq_enable(); |
| 226 | mdelay((10*1000)/nmi_hz); // wait 10 ticks |
| 227 | |
Andrew Morton | 394e390 | 2006-03-23 03:01:05 -0800 | [diff] [blame] | 228 | for_each_online_cpu(cpu) { |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 229 | if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled) |
| 230 | continue; |
Ravikiran G Thirumalai | df79efd | 2006-01-11 22:45:39 +0100 | [diff] [blame] | 231 | if (cpu_pda(cpu)->__nmi_count - counts[cpu] <= 5) { |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 232 | printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | cpu, |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 234 | counts[cpu], |
Ravikiran G Thirumalai | df79efd | 2006-01-11 22:45:39 +0100 | [diff] [blame] | 235 | cpu_pda(cpu)->__nmi_count); |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 236 | per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0; |
| 237 | atomic_dec(&nmi_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 240 | if (!atomic_read(&nmi_active)) { |
| 241 | kfree(counts); |
| 242 | atomic_set(&nmi_active, -1); |
| 243 | return -1; |
| 244 | } |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 245 | endflag = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | printk("OK.\n"); |
| 247 | |
| 248 | /* now that we know it works we can reduce NMI frequency to |
| 249 | something more reasonable; makes a difference in some configs */ |
| 250 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 251 | nmi_hz = 1; |
| 252 | |
Andi Kleen | ac6b931 | 2005-05-16 21:53:19 -0700 | [diff] [blame] | 253 | kfree(counts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | int __init setup_nmi_watchdog(char *str) |
| 258 | { |
| 259 | int nmi; |
| 260 | |
| 261 | if (!strncmp(str,"panic",5)) { |
| 262 | panic_on_timeout = 1; |
| 263 | str = strchr(str, ','); |
| 264 | if (!str) |
| 265 | return 1; |
| 266 | ++str; |
| 267 | } |
| 268 | |
| 269 | get_option(&str, &nmi); |
| 270 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 271 | if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | return 0; |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 273 | |
| 274 | if ((nmi == NMI_LOCAL_APIC) && (nmi_known_cpu() == 0)) |
| 275 | return 0; /* no lapic support */ |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 276 | nmi_watchdog = nmi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | __setup("nmi_watchdog=", setup_nmi_watchdog); |
| 281 | |
| 282 | static void disable_lapic_nmi_watchdog(void) |
| 283 | { |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 284 | BUG_ON(nmi_watchdog != NMI_LOCAL_APIC); |
| 285 | |
| 286 | if (atomic_read(&nmi_active) <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | return; |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 288 | |
| 289 | on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1); |
| 290 | |
| 291 | BUG_ON(atomic_read(&nmi_active) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static void enable_lapic_nmi_watchdog(void) |
| 295 | { |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 296 | BUG_ON(nmi_watchdog != NMI_LOCAL_APIC); |
| 297 | |
| 298 | /* are we already enabled */ |
| 299 | if (atomic_read(&nmi_active) != 0) |
| 300 | return; |
| 301 | |
| 302 | /* are we lapic aware */ |
| 303 | if (nmi_known_cpu() <= 0) |
| 304 | return; |
| 305 | |
| 306 | on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1); |
| 307 | touch_nmi_watchdog(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | void disable_timer_nmi_watchdog(void) |
| 311 | { |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 312 | BUG_ON(nmi_watchdog != NMI_IO_APIC); |
| 313 | |
| 314 | if (atomic_read(&nmi_active) <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | return; |
| 316 | |
| 317 | disable_irq(0); |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 318 | on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1); |
| 319 | |
| 320 | BUG_ON(atomic_read(&nmi_active) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void enable_timer_nmi_watchdog(void) |
| 324 | { |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 325 | BUG_ON(nmi_watchdog != NMI_IO_APIC); |
| 326 | |
| 327 | if (atomic_read(&nmi_active) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | touch_nmi_watchdog(); |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 329 | on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | enable_irq(0); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | #ifdef CONFIG_PM |
| 335 | |
| 336 | static int nmi_pm_active; /* nmi_active before suspend */ |
| 337 | |
Pavel Machek | 829ca9a | 2005-09-03 15:56:56 -0700 | [diff] [blame] | 338 | 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] | 339 | { |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 340 | nmi_pm_active = atomic_read(&nmi_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | disable_lapic_nmi_watchdog(); |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int lapic_nmi_resume(struct sys_device *dev) |
| 346 | { |
| 347 | if (nmi_pm_active > 0) |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 348 | enable_lapic_nmi_watchdog(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static struct sysdev_class nmi_sysclass = { |
| 353 | set_kset_name("lapic_nmi"), |
| 354 | .resume = lapic_nmi_resume, |
| 355 | .suspend = lapic_nmi_suspend, |
| 356 | }; |
| 357 | |
| 358 | static struct sys_device device_lapic_nmi = { |
| 359 | .id = 0, |
| 360 | .cls = &nmi_sysclass, |
| 361 | }; |
| 362 | |
| 363 | static int __init init_lapic_nmi_sysfs(void) |
| 364 | { |
| 365 | int error; |
| 366 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 367 | /* should really be a BUG_ON but b/c this is an |
| 368 | * init call, it just doesn't work. -dcz |
| 369 | */ |
| 370 | if (nmi_watchdog != NMI_LOCAL_APIC) |
| 371 | return 0; |
| 372 | |
| 373 | if ( atomic_read(&nmi_active) < 0 ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | return 0; |
| 375 | |
| 376 | error = sysdev_class_register(&nmi_sysclass); |
| 377 | if (!error) |
| 378 | error = sysdev_register(&device_lapic_nmi); |
| 379 | return error; |
| 380 | } |
| 381 | /* must come after the local APIC's device_initcall() */ |
| 382 | late_initcall(init_lapic_nmi_sysfs); |
| 383 | |
| 384 | #endif /* CONFIG_PM */ |
| 385 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 386 | /* |
| 387 | * Activate the NMI watchdog via the local APIC. |
| 388 | * Original code written by Keith Owens. |
| 389 | */ |
| 390 | |
| 391 | /* Note that these events don't tick when the CPU idles. This means |
| 392 | the frequency varies with CPU load. */ |
| 393 | |
| 394 | #define K7_EVNTSEL_ENABLE (1 << 22) |
| 395 | #define K7_EVNTSEL_INT (1 << 20) |
| 396 | #define K7_EVNTSEL_OS (1 << 17) |
| 397 | #define K7_EVNTSEL_USR (1 << 16) |
| 398 | #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76 |
| 399 | #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING |
| 400 | |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 401 | static int setup_k7_watchdog(void) |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 402 | { |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 403 | unsigned int perfctr_msr, evntsel_msr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | unsigned int evntsel; |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 405 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 407 | perfctr_msr = MSR_K7_PERFCTR0; |
| 408 | evntsel_msr = MSR_K7_EVNTSEL0; |
| 409 | if (!reserve_perfctr_nmi(perfctr_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 410 | goto fail; |
| 411 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 412 | if (!reserve_evntsel_nmi(evntsel_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 413 | goto fail1; |
| 414 | |
| 415 | /* Simulator may not support it */ |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 416 | if (checking_wrmsrl(evntsel_msr, 0UL)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 417 | goto fail2; |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 418 | wrmsrl(perfctr_msr, 0UL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | evntsel = K7_EVNTSEL_INT |
| 421 | | K7_EVNTSEL_OS |
| 422 | | K7_EVNTSEL_USR |
| 423 | | K7_NMI_EVENT; |
| 424 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 425 | /* setup the timer */ |
| 426 | wrmsr(evntsel_msr, evntsel, 0); |
| 427 | wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 429 | evntsel |= K7_EVNTSEL_ENABLE; |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 430 | wrmsr(evntsel_msr, evntsel, 0); |
| 431 | |
| 432 | wd->perfctr_msr = perfctr_msr; |
| 433 | wd->evntsel_msr = evntsel_msr; |
| 434 | wd->cccr_msr = 0; //unused |
| 435 | wd->check_bit = 1ULL<<63; |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 436 | return 1; |
| 437 | fail2: |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 438 | release_evntsel_nmi(evntsel_msr); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 439 | fail1: |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 440 | release_perfctr_nmi(perfctr_msr); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 441 | fail: |
| 442 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 445 | static void stop_k7_watchdog(void) |
| 446 | { |
| 447 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
| 448 | |
| 449 | wrmsr(wd->evntsel_msr, 0, 0); |
| 450 | |
| 451 | release_evntsel_nmi(wd->evntsel_msr); |
| 452 | release_perfctr_nmi(wd->perfctr_msr); |
| 453 | } |
| 454 | |
| 455 | /* Note that these events don't tick when the CPU idles. This means |
| 456 | the frequency varies with CPU load. */ |
| 457 | |
| 458 | #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7) |
| 459 | #define P4_ESCR_EVENT_SELECT(N) ((N)<<25) |
| 460 | #define P4_ESCR_OS (1<<3) |
| 461 | #define P4_ESCR_USR (1<<2) |
| 462 | #define P4_CCCR_OVF_PMI0 (1<<26) |
| 463 | #define P4_CCCR_OVF_PMI1 (1<<27) |
| 464 | #define P4_CCCR_THRESHOLD(N) ((N)<<20) |
| 465 | #define P4_CCCR_COMPLEMENT (1<<19) |
| 466 | #define P4_CCCR_COMPARE (1<<18) |
| 467 | #define P4_CCCR_REQUIRED (3<<16) |
| 468 | #define P4_CCCR_ESCR_SELECT(N) ((N)<<13) |
| 469 | #define P4_CCCR_ENABLE (1<<12) |
| 470 | #define P4_CCCR_OVF (1<<31) |
| 471 | /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter |
| 472 | CRU_ESCR0 (with any non-null event selector) through a complemented |
| 473 | max threshold. [IA32-Vol3, Section 14.9.9] */ |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 474 | |
| 475 | static int setup_p4_watchdog(void) |
| 476 | { |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 477 | unsigned int perfctr_msr, evntsel_msr, cccr_msr; |
| 478 | unsigned int evntsel, cccr_val; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 479 | unsigned int misc_enable, dummy; |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 480 | unsigned int ht_num; |
| 481 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 482 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 483 | rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy); |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 484 | if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL)) |
| 485 | return 0; |
| 486 | |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 487 | #ifdef CONFIG_SMP |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 488 | /* detect which hyperthread we are on */ |
| 489 | if (smp_num_siblings == 2) { |
| 490 | unsigned int ebx, apicid; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 491 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 492 | ebx = cpuid_ebx(1); |
| 493 | apicid = (ebx >> 24) & 0xff; |
| 494 | ht_num = apicid & 1; |
| 495 | } else |
| 496 | #endif |
| 497 | ht_num = 0; |
| 498 | |
| 499 | /* performance counters are shared resources |
| 500 | * assign each hyperthread its own set |
| 501 | * (re-use the ESCR0 register, seems safe |
| 502 | * and keeps the cccr_val the same) |
| 503 | */ |
| 504 | if (!ht_num) { |
| 505 | /* logical cpu 0 */ |
| 506 | perfctr_msr = MSR_P4_IQ_PERFCTR0; |
| 507 | evntsel_msr = MSR_P4_CRU_ESCR0; |
| 508 | cccr_msr = MSR_P4_IQ_CCCR0; |
| 509 | cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4); |
| 510 | } else { |
| 511 | /* logical cpu 1 */ |
| 512 | perfctr_msr = MSR_P4_IQ_PERFCTR1; |
| 513 | evntsel_msr = MSR_P4_CRU_ESCR0; |
| 514 | cccr_msr = MSR_P4_IQ_CCCR1; |
| 515 | cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4); |
| 516 | } |
| 517 | |
| 518 | if (!reserve_perfctr_nmi(perfctr_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 519 | goto fail; |
| 520 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 521 | if (!reserve_evntsel_nmi(evntsel_msr)) |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 522 | goto fail1; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 523 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 524 | evntsel = P4_ESCR_EVENT_SELECT(0x3F) |
| 525 | | P4_ESCR_OS |
| 526 | | P4_ESCR_USR; |
| 527 | |
| 528 | cccr_val |= P4_CCCR_THRESHOLD(15) |
| 529 | | P4_CCCR_COMPLEMENT |
| 530 | | P4_CCCR_COMPARE |
| 531 | | P4_CCCR_REQUIRED; |
| 532 | |
| 533 | wrmsr(evntsel_msr, evntsel, 0); |
| 534 | wrmsr(cccr_msr, cccr_val, 0); |
| 535 | wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz)); |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 536 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 537 | cccr_val |= P4_CCCR_ENABLE; |
| 538 | wrmsr(cccr_msr, cccr_val, 0); |
| 539 | |
| 540 | wd->perfctr_msr = perfctr_msr; |
| 541 | wd->evntsel_msr = evntsel_msr; |
| 542 | wd->cccr_msr = cccr_msr; |
| 543 | wd->check_bit = 1ULL<<39; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 544 | return 1; |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 545 | fail1: |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 546 | release_perfctr_nmi(perfctr_msr); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 547 | fail: |
| 548 | return 0; |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 551 | static void stop_p4_watchdog(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 553 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 554 | |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 555 | wrmsr(wd->cccr_msr, 0, 0); |
| 556 | wrmsr(wd->evntsel_msr, 0, 0); |
| 557 | |
| 558 | release_evntsel_nmi(wd->evntsel_msr); |
| 559 | release_perfctr_nmi(wd->perfctr_msr); |
| 560 | } |
| 561 | |
| 562 | void setup_apic_nmi_watchdog(void *unused) |
| 563 | { |
| 564 | /* only support LOCAL and IO APICs for now */ |
| 565 | if ((nmi_watchdog != NMI_LOCAL_APIC) && |
| 566 | (nmi_watchdog != NMI_IO_APIC)) |
| 567 | return; |
| 568 | |
| 569 | if (nmi_watchdog == NMI_LOCAL_APIC) { |
| 570 | switch (boot_cpu_data.x86_vendor) { |
| 571 | case X86_VENDOR_AMD: |
| 572 | if (strstr(boot_cpu_data.x86_model_id, "Screwdriver")) |
| 573 | return; |
| 574 | if (!setup_k7_watchdog()) |
| 575 | return; |
| 576 | break; |
| 577 | case X86_VENDOR_INTEL: |
| 578 | if (!setup_p4_watchdog()) |
| 579 | return; |
| 580 | break; |
| 581 | default: |
| 582 | return; |
| 583 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 585 | __get_cpu_var(nmi_watchdog_ctlblk.enabled) = 1; |
| 586 | atomic_inc(&nmi_active); |
| 587 | } |
| 588 | |
| 589 | static void stop_apic_nmi_watchdog(void *unused) |
| 590 | { |
| 591 | /* only support LOCAL and IO APICs for now */ |
| 592 | if ((nmi_watchdog != NMI_LOCAL_APIC) && |
| 593 | (nmi_watchdog != NMI_IO_APIC)) |
| 594 | return; |
| 595 | |
| 596 | if (nmi_watchdog == NMI_LOCAL_APIC) { |
| 597 | switch (boot_cpu_data.x86_vendor) { |
| 598 | case X86_VENDOR_AMD: |
| 599 | if (strstr(boot_cpu_data.x86_model_id, "Screwdriver")) |
| 600 | return; |
| 601 | stop_k7_watchdog(); |
| 602 | break; |
| 603 | case X86_VENDOR_INTEL: |
| 604 | stop_p4_watchdog(); |
| 605 | break; |
| 606 | default: |
| 607 | return; |
| 608 | } |
| 609 | } |
| 610 | __get_cpu_var(nmi_watchdog_ctlblk.enabled) = 0; |
| 611 | atomic_dec(&nmi_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* |
| 615 | * the best way to detect whether a CPU has a 'hard lockup' problem |
| 616 | * is to check it's local APIC timer IRQ counts. If they are not |
| 617 | * changing then that CPU has some problem. |
| 618 | * |
| 619 | * as these watchdog NMI IRQs are generated on every CPU, we only |
| 620 | * have to check the current processor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | */ |
| 622 | |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 623 | static DEFINE_PER_CPU(unsigned, last_irq_sum); |
| 624 | static DEFINE_PER_CPU(local_t, alert_counter); |
| 625 | static DEFINE_PER_CPU(int, nmi_touch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
| 627 | void touch_nmi_watchdog (void) |
| 628 | { |
Jan Beulich | 99019e9 | 2006-02-16 23:41:55 +0100 | [diff] [blame] | 629 | if (nmi_watchdog > 0) { |
| 630 | unsigned cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
Jan Beulich | 99019e9 | 2006-02-16 23:41:55 +0100 | [diff] [blame] | 632 | /* |
| 633 | * Tell other CPUs to reset their alert counters. We cannot |
| 634 | * do it ourselves because the alert count increase is not |
| 635 | * atomic. |
| 636 | */ |
| 637 | for_each_present_cpu (cpu) |
| 638 | per_cpu(nmi_touch, cpu) = 1; |
| 639 | } |
Ingo Molnar | 8446f1d | 2005-09-06 15:16:27 -0700 | [diff] [blame] | 640 | |
| 641 | touch_softlockup_watchdog(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 644 | int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | { |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 646 | int sum; |
| 647 | int touched = 0; |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 648 | struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk); |
| 649 | u64 dummy; |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 650 | int rc=0; |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 651 | |
| 652 | /* check for other users first */ |
| 653 | if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) |
| 654 | == NOTIFY_STOP) { |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 655 | rc = 1; |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 656 | touched = 1; |
| 657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | sum = read_pda(apic_timer_irqs); |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 660 | if (__get_cpu_var(nmi_touch)) { |
| 661 | __get_cpu_var(nmi_touch) = 0; |
| 662 | touched = 1; |
| 663 | } |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 664 | |
Andi Kleen | 553f265 | 2006-04-07 19:49:57 +0200 | [diff] [blame] | 665 | #ifdef CONFIG_X86_MCE |
| 666 | /* Could check oops_in_progress here too, but it's safer |
| 667 | not too */ |
| 668 | if (atomic_read(&mce_entry) > 0) |
| 669 | touched = 1; |
| 670 | #endif |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 671 | /* 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] | 672 | if (!touched && __get_cpu_var(last_irq_sum) == sum) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | /* |
| 674 | * Ayiee, looks like this CPU is stuck ... |
| 675 | * wait a few IRQs (5 seconds) before doing the oops ... |
| 676 | */ |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 677 | local_inc(&__get_cpu_var(alert_counter)); |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 678 | if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz) |
Chuck Ebbert | 8478157 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 679 | die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | } else { |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 681 | __get_cpu_var(last_irq_sum) = sum; |
| 682 | local_set(&__get_cpu_var(alert_counter), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 684 | |
| 685 | /* see if the nmi watchdog went off */ |
| 686 | if (wd->enabled) { |
| 687 | if (nmi_watchdog == NMI_LOCAL_APIC) { |
| 688 | rdmsrl(wd->perfctr_msr, dummy); |
| 689 | if (dummy & wd->check_bit){ |
| 690 | /* this wasn't a watchdog timer interrupt */ |
| 691 | goto done; |
| 692 | } |
| 693 | |
| 694 | /* only Intel uses the cccr msr */ |
| 695 | if (wd->cccr_msr != 0) { |
| 696 | /* |
| 697 | * P4 quirks: |
| 698 | * - An overflown perfctr will assert its interrupt |
| 699 | * until the OVF flag in its CCCR is cleared. |
| 700 | * - LVTPC is masked on interrupt and must be |
| 701 | * unmasked by the LVTPC handler. |
| 702 | */ |
| 703 | rdmsrl(wd->cccr_msr, dummy); |
| 704 | dummy &= ~P4_CCCR_OVF; |
| 705 | wrmsrl(wd->cccr_msr, dummy); |
| 706 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 707 | } |
| 708 | /* start the cycle over again */ |
| 709 | wrmsrl(wd->perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz)); |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 710 | rc = 1; |
| 711 | } else if (nmi_watchdog == NMI_IO_APIC) { |
| 712 | /* don't know how to accurately check for this. |
| 713 | * just assume it was a watchdog timer interrupt |
| 714 | * This matches the old behaviour. |
| 715 | */ |
| 716 | rc = 1; |
| 717 | } else |
| 718 | printk(KERN_WARNING "Unknown enabled NMI hardware?!\n"); |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 719 | } |
Don Zickus | f2802e7 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 720 | done: |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 721 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Andi Kleen | eddb6fb | 2006-02-03 21:50:41 +0100 | [diff] [blame] | 724 | asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | nmi_enter(); |
| 727 | add_pda(__nmi_count,1); |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 728 | default_do_nmi(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | nmi_exit(); |
| 730 | } |
| 731 | |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 732 | int do_nmi_callback(struct pt_regs * regs, int cpu) |
| 733 | { |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 734 | #ifdef CONFIG_SYSCTL |
| 735 | if (unknown_nmi_panic) |
| 736 | return unknown_nmi_panic_callback(regs, cpu); |
| 737 | #endif |
| 738 | return 0; |
Don Zickus | 3adbbcce | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | #ifdef CONFIG_SYSCTL |
| 742 | |
| 743 | static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu) |
| 744 | { |
| 745 | unsigned char reason = get_nmi_reason(); |
| 746 | char buf[64]; |
| 747 | |
Don Zickus | 2fbe7b2 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 748 | sprintf(buf, "NMI received for unknown reason %02x\n", reason); |
| 749 | die_nmi(buf,regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | return 0; |
| 751 | } |
| 752 | |
Don Zickus | 407984f | 2006-09-26 10:52:27 +0200 | [diff] [blame^] | 753 | /* |
| 754 | * proc handler for /proc/sys/kernel/nmi |
| 755 | */ |
| 756 | int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file, |
| 757 | void __user *buffer, size_t *length, loff_t *ppos) |
| 758 | { |
| 759 | int old_state; |
| 760 | |
| 761 | nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0; |
| 762 | old_state = nmi_watchdog_enabled; |
| 763 | proc_dointvec(table, write, file, buffer, length, ppos); |
| 764 | if (!!old_state == !!nmi_watchdog_enabled) |
| 765 | return 0; |
| 766 | |
| 767 | if (atomic_read(&nmi_active) < 0) { |
| 768 | printk( KERN_WARNING "NMI watchdog is permanently disabled\n"); |
| 769 | return -EINVAL; |
| 770 | } |
| 771 | |
| 772 | /* if nmi_watchdog is not set yet, then set it */ |
| 773 | nmi_watchdog_default(); |
| 774 | |
| 775 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 776 | { |
| 777 | if (nmi_watchdog_enabled) |
| 778 | enable_lapic_nmi_watchdog(); |
| 779 | else |
| 780 | disable_lapic_nmi_watchdog(); |
| 781 | } else if (nmi_watchdog == NMI_IO_APIC) { |
| 782 | /* FIXME |
| 783 | * for some reason these functions don't work |
| 784 | */ |
| 785 | printk("Can not enable/disable NMI on IO APIC\n"); |
| 786 | return -EIO; |
| 787 | #if 0 |
| 788 | if (nmi_watchdog_enabled) |
| 789 | enable_timer_nmi_watchdog(); |
| 790 | else |
| 791 | disable_timer_nmi_watchdog(); |
| 792 | #endif |
| 793 | } else { |
| 794 | printk(KERN_WARNING |
| 795 | "NMI watchdog doesn't know what hardware to touch\n"); |
| 796 | return -EIO; |
| 797 | } |
| 798 | return 0; |
| 799 | } |
| 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | #endif |
| 802 | |
| 803 | EXPORT_SYMBOL(nmi_active); |
| 804 | EXPORT_SYMBOL(nmi_watchdog); |
Don Zickus | 828f0af | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 805 | EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi); |
| 806 | EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit); |
| 807 | EXPORT_SYMBOL(reserve_perfctr_nmi); |
| 808 | EXPORT_SYMBOL(release_perfctr_nmi); |
| 809 | EXPORT_SYMBOL(reserve_evntsel_nmi); |
| 810 | EXPORT_SYMBOL(release_evntsel_nmi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | EXPORT_SYMBOL(disable_timer_nmi_watchdog); |
| 812 | EXPORT_SYMBOL(enable_timer_nmi_watchdog); |
| 813 | EXPORT_SYMBOL(touch_nmi_watchdog); |