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