Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Machine check handler. | |
e9eee03e | 3 | * |
1da177e4 | 4 | * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs. |
d88203d1 TG |
5 | * Rest from unknown author(s). |
6 | * 2004 Andi Kleen. Rewrote most of it. | |
b79109c3 AK |
7 | * Copyright 2008 Intel Corporation |
8 | * Author: Andi Kleen | |
1da177e4 | 9 | */ |
e9eee03e IM |
10 | #include <linux/thread_info.h> |
11 | #include <linux/capability.h> | |
12 | #include <linux/miscdevice.h> | |
ccc3c319 | 13 | #include <linux/interrupt.h> |
e9eee03e IM |
14 | #include <linux/ratelimit.h> |
15 | #include <linux/kallsyms.h> | |
16 | #include <linux/rcupdate.h> | |
e9eee03e | 17 | #include <linux/kobject.h> |
14a02530 | 18 | #include <linux/uaccess.h> |
e9eee03e IM |
19 | #include <linux/kdebug.h> |
20 | #include <linux/kernel.h> | |
21 | #include <linux/percpu.h> | |
1da177e4 | 22 | #include <linux/string.h> |
1da177e4 | 23 | #include <linux/sysdev.h> |
3c079792 | 24 | #include <linux/delay.h> |
8c566ef5 | 25 | #include <linux/ctype.h> |
e9eee03e | 26 | #include <linux/sched.h> |
0d7482e3 | 27 | #include <linux/sysfs.h> |
e9eee03e IM |
28 | #include <linux/types.h> |
29 | #include <linux/init.h> | |
30 | #include <linux/kmod.h> | |
31 | #include <linux/poll.h> | |
3c079792 | 32 | #include <linux/nmi.h> |
e9eee03e | 33 | #include <linux/cpu.h> |
14a02530 | 34 | #include <linux/smp.h> |
e9eee03e | 35 | #include <linux/fs.h> |
9b1beaf2 | 36 | #include <linux/mm.h> |
e9eee03e | 37 | |
d88203d1 | 38 | #include <asm/processor.h> |
ccc3c319 AK |
39 | #include <asm/hw_irq.h> |
40 | #include <asm/apic.h> | |
e02e68d3 | 41 | #include <asm/idle.h> |
ccc3c319 | 42 | #include <asm/ipi.h> |
e9eee03e IM |
43 | #include <asm/mce.h> |
44 | #include <asm/msr.h> | |
1da177e4 | 45 | |
bd19a5e6 | 46 | #include "mce-internal.h" |
711c2e48 IM |
47 | #include "mce.h" |
48 | ||
5d727926 AK |
49 | /* Handle unconfigured int18 (should never happen) */ |
50 | static void unexpected_machine_check(struct pt_regs *regs, long error_code) | |
51 | { | |
52 | printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n", | |
53 | smp_processor_id()); | |
54 | } | |
55 | ||
56 | /* Call the installed machine check handler for this CPU setup. */ | |
57 | void (*machine_check_vector)(struct pt_regs *, long error_code) = | |
58 | unexpected_machine_check; | |
04b2b1a4 AK |
59 | |
60 | int mce_disabled; | |
61 | ||
4efc0670 | 62 | #ifdef CONFIG_X86_NEW_MCE |
711c2e48 | 63 | |
e9eee03e | 64 | #define MISC_MCELOG_MINOR 227 |
0d7482e3 | 65 | |
3c079792 AK |
66 | #define SPINUNIT 100 /* 100ns */ |
67 | ||
553f265f AK |
68 | atomic_t mce_entry; |
69 | ||
01ca79f1 AK |
70 | DEFINE_PER_CPU(unsigned, mce_exception_count); |
71 | ||
bd78432c TH |
72 | /* |
73 | * Tolerant levels: | |
74 | * 0: always panic on uncorrected errors, log corrected errors | |
75 | * 1: panic or SIGBUS on uncorrected errors, log corrected errors | |
76 | * 2: SIGBUS or log uncorrected errors (if possible), log corrected errors | |
77 | * 3: never panic or SIGBUS, log all errors (for testing only) | |
78 | */ | |
e9eee03e IM |
79 | static int tolerant = 1; |
80 | static int banks; | |
81 | static u64 *bank; | |
82 | static unsigned long notify_user; | |
83 | static int rip_msr; | |
84 | static int mce_bootlog = -1; | |
3c079792 | 85 | static int monarch_timeout = -1; |
29b0f591 | 86 | static int mce_panic_timeout; |
62fdac59 HS |
87 | static int mce_dont_log_ce; |
88 | int mce_cmci_disabled; | |
89 | int mce_ignore_ce; | |
ed7290d0 | 90 | int mce_ser; |
a98f0dd3 | 91 | |
e9eee03e IM |
92 | static char trigger[128]; |
93 | static char *trigger_argv[2] = { trigger, NULL }; | |
1da177e4 | 94 | |
06b7a7a5 AK |
95 | static unsigned long dont_init_banks; |
96 | ||
e02e68d3 | 97 | static DECLARE_WAIT_QUEUE_HEAD(mce_wait); |
3c079792 AK |
98 | static DEFINE_PER_CPU(struct mce, mces_seen); |
99 | static int cpu_missing; | |
100 | ||
e02e68d3 | 101 | |
ee031c31 AK |
102 | /* MCA banks polled by the period polling timer for corrected events */ |
103 | DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = { | |
104 | [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL | |
105 | }; | |
106 | ||
06b7a7a5 AK |
107 | static inline int skip_bank_init(int i) |
108 | { | |
109 | return i < BITS_PER_LONG && test_bit(i, &dont_init_banks); | |
110 | } | |
111 | ||
9b1beaf2 AK |
112 | static DEFINE_PER_CPU(struct work_struct, mce_work); |
113 | ||
b5f2fa4e AK |
114 | /* Do initial initialization of a struct mce */ |
115 | void mce_setup(struct mce *m) | |
116 | { | |
117 | memset(m, 0, sizeof(struct mce)); | |
d620c67f | 118 | m->cpu = m->extcpu = smp_processor_id(); |
b5f2fa4e | 119 | rdtscll(m->tsc); |
8ee08347 AK |
120 | /* We hope get_seconds stays lockless */ |
121 | m->time = get_seconds(); | |
122 | m->cpuvendor = boot_cpu_data.x86_vendor; | |
123 | m->cpuid = cpuid_eax(1); | |
124 | #ifdef CONFIG_SMP | |
125 | m->socketid = cpu_data(m->extcpu).phys_proc_id; | |
126 | #endif | |
127 | m->apicid = cpu_data(m->extcpu).initial_apicid; | |
128 | rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap); | |
b5f2fa4e AK |
129 | } |
130 | ||
ea149b36 AK |
131 | DEFINE_PER_CPU(struct mce, injectm); |
132 | EXPORT_PER_CPU_SYMBOL_GPL(injectm); | |
133 | ||
1da177e4 LT |
134 | /* |
135 | * Lockless MCE logging infrastructure. | |
136 | * This avoids deadlocks on printk locks without having to break locks. Also | |
137 | * separate MCEs from kernel messages to avoid bogus bug reports. | |
138 | */ | |
139 | ||
231fd906 | 140 | static struct mce_log mcelog = { |
f6fb0ac0 AK |
141 | .signature = MCE_LOG_SIGNATURE, |
142 | .len = MCE_LOG_LEN, | |
143 | .recordlen = sizeof(struct mce), | |
d88203d1 | 144 | }; |
1da177e4 LT |
145 | |
146 | void mce_log(struct mce *mce) | |
147 | { | |
148 | unsigned next, entry; | |
e9eee03e | 149 | |
1da177e4 | 150 | mce->finished = 0; |
7644143c | 151 | wmb(); |
1da177e4 LT |
152 | for (;;) { |
153 | entry = rcu_dereference(mcelog.next); | |
673242c1 | 154 | for (;;) { |
e9eee03e IM |
155 | /* |
156 | * When the buffer fills up discard new entries. | |
157 | * Assume that the earlier errors are the more | |
158 | * interesting ones: | |
159 | */ | |
673242c1 | 160 | if (entry >= MCE_LOG_LEN) { |
14a02530 HS |
161 | set_bit(MCE_OVERFLOW, |
162 | (unsigned long *)&mcelog.flags); | |
673242c1 AK |
163 | return; |
164 | } | |
e9eee03e | 165 | /* Old left over entry. Skip: */ |
673242c1 AK |
166 | if (mcelog.entry[entry].finished) { |
167 | entry++; | |
168 | continue; | |
169 | } | |
7644143c | 170 | break; |
1da177e4 | 171 | } |
1da177e4 LT |
172 | smp_rmb(); |
173 | next = entry + 1; | |
174 | if (cmpxchg(&mcelog.next, entry, next) == entry) | |
175 | break; | |
176 | } | |
177 | memcpy(mcelog.entry + entry, mce, sizeof(struct mce)); | |
7644143c | 178 | wmb(); |
1da177e4 | 179 | mcelog.entry[entry].finished = 1; |
7644143c | 180 | wmb(); |
1da177e4 | 181 | |
a0189c70 | 182 | mce->finished = 1; |
e02e68d3 | 183 | set_bit(0, ¬ify_user); |
1da177e4 LT |
184 | } |
185 | ||
77e26cca | 186 | static void print_mce(struct mce *m) |
1da177e4 | 187 | { |
86503560 | 188 | printk(KERN_EMERG |
1da177e4 | 189 | "CPU %d: Machine Check Exception: %16Lx Bank %d: %016Lx\n", |
d620c67f | 190 | m->extcpu, m->mcgstatus, m->bank, m->status); |
65ea5b03 | 191 | if (m->ip) { |
d88203d1 | 192 | printk(KERN_EMERG "RIP%s %02x:<%016Lx> ", |
1da177e4 | 193 | !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "", |
65ea5b03 | 194 | m->cs, m->ip); |
1da177e4 | 195 | if (m->cs == __KERNEL_CS) |
65ea5b03 | 196 | print_symbol("{%s}", m->ip); |
1da177e4 LT |
197 | printk("\n"); |
198 | } | |
f6d1826d | 199 | printk(KERN_EMERG "TSC %llx ", m->tsc); |
1da177e4 | 200 | if (m->addr) |
f6d1826d | 201 | printk("ADDR %llx ", m->addr); |
1da177e4 | 202 | if (m->misc) |
f6d1826d | 203 | printk("MISC %llx ", m->misc); |
1da177e4 | 204 | printk("\n"); |
8ee08347 AK |
205 | printk(KERN_EMERG "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n", |
206 | m->cpuvendor, m->cpuid, m->time, m->socketid, | |
207 | m->apicid); | |
86503560 AK |
208 | } |
209 | ||
77e26cca HS |
210 | static void print_mce_head(void) |
211 | { | |
212 | printk(KERN_EMERG "\n" KERN_EMERG "HARDWARE ERROR\n"); | |
213 | } | |
214 | ||
86503560 AK |
215 | static void print_mce_tail(void) |
216 | { | |
217 | printk(KERN_EMERG "This is not a software problem!\n" | |
218 | KERN_EMERG "Run through mcelog --ascii to decode and contact your hardware vendor\n"); | |
1da177e4 LT |
219 | } |
220 | ||
f94b61c2 AK |
221 | #define PANIC_TIMEOUT 5 /* 5 seconds */ |
222 | ||
223 | static atomic_t mce_paniced; | |
224 | ||
225 | /* Panic in progress. Enable interrupts and wait for final IPI */ | |
226 | static void wait_for_panic(void) | |
227 | { | |
228 | long timeout = PANIC_TIMEOUT*USEC_PER_SEC; | |
229 | preempt_disable(); | |
230 | local_irq_enable(); | |
231 | while (timeout-- > 0) | |
232 | udelay(1); | |
29b0f591 AK |
233 | if (panic_timeout == 0) |
234 | panic_timeout = mce_panic_timeout; | |
f94b61c2 AK |
235 | panic("Panicing machine check CPU died"); |
236 | } | |
237 | ||
bd19a5e6 | 238 | static void mce_panic(char *msg, struct mce *final, char *exp) |
d88203d1 | 239 | { |
1da177e4 | 240 | int i; |
e02e68d3 | 241 | |
f94b61c2 AK |
242 | /* |
243 | * Make sure only one CPU runs in machine check panic | |
244 | */ | |
245 | if (atomic_add_return(1, &mce_paniced) > 1) | |
246 | wait_for_panic(); | |
247 | barrier(); | |
248 | ||
d896a940 AK |
249 | bust_spinlocks(1); |
250 | console_verbose(); | |
77e26cca | 251 | print_mce_head(); |
a0189c70 | 252 | /* First print corrected ones that are still unlogged */ |
1da177e4 | 253 | for (i = 0; i < MCE_LOG_LEN; i++) { |
a0189c70 | 254 | struct mce *m = &mcelog.entry[i]; |
77e26cca HS |
255 | if (!(m->status & MCI_STATUS_VAL)) |
256 | continue; | |
257 | if (!(m->status & MCI_STATUS_UC)) | |
258 | print_mce(m); | |
a0189c70 AK |
259 | } |
260 | /* Now print uncorrected but with the final one last */ | |
261 | for (i = 0; i < MCE_LOG_LEN; i++) { | |
262 | struct mce *m = &mcelog.entry[i]; | |
263 | if (!(m->status & MCI_STATUS_VAL)) | |
1da177e4 | 264 | continue; |
77e26cca HS |
265 | if (!(m->status & MCI_STATUS_UC)) |
266 | continue; | |
a0189c70 | 267 | if (!final || memcmp(m, final, sizeof(struct mce))) |
77e26cca | 268 | print_mce(m); |
1da177e4 | 269 | } |
a0189c70 | 270 | if (final) |
77e26cca | 271 | print_mce(final); |
3c079792 AK |
272 | if (cpu_missing) |
273 | printk(KERN_EMERG "Some CPUs didn't answer in synchronization\n"); | |
86503560 | 274 | print_mce_tail(); |
bd19a5e6 AK |
275 | if (exp) |
276 | printk(KERN_EMERG "Machine check: %s\n", exp); | |
29b0f591 AK |
277 | if (panic_timeout == 0) |
278 | panic_timeout = mce_panic_timeout; | |
e02e68d3 | 279 | panic(msg); |
d88203d1 | 280 | } |
1da177e4 | 281 | |
ea149b36 AK |
282 | /* Support code for software error injection */ |
283 | ||
284 | static int msr_to_offset(u32 msr) | |
285 | { | |
286 | unsigned bank = __get_cpu_var(injectm.bank); | |
287 | if (msr == rip_msr) | |
288 | return offsetof(struct mce, ip); | |
289 | if (msr == MSR_IA32_MC0_STATUS + bank*4) | |
290 | return offsetof(struct mce, status); | |
291 | if (msr == MSR_IA32_MC0_ADDR + bank*4) | |
292 | return offsetof(struct mce, addr); | |
293 | if (msr == MSR_IA32_MC0_MISC + bank*4) | |
294 | return offsetof(struct mce, misc); | |
295 | if (msr == MSR_IA32_MCG_STATUS) | |
296 | return offsetof(struct mce, mcgstatus); | |
297 | return -1; | |
298 | } | |
299 | ||
5f8c1a54 AK |
300 | /* MSR access wrappers used for error injection */ |
301 | static u64 mce_rdmsrl(u32 msr) | |
302 | { | |
303 | u64 v; | |
ea149b36 AK |
304 | if (__get_cpu_var(injectm).finished) { |
305 | int offset = msr_to_offset(msr); | |
306 | if (offset < 0) | |
307 | return 0; | |
308 | return *(u64 *)((char *)&__get_cpu_var(injectm) + offset); | |
309 | } | |
5f8c1a54 AK |
310 | rdmsrl(msr, v); |
311 | return v; | |
312 | } | |
313 | ||
314 | static void mce_wrmsrl(u32 msr, u64 v) | |
315 | { | |
ea149b36 AK |
316 | if (__get_cpu_var(injectm).finished) { |
317 | int offset = msr_to_offset(msr); | |
318 | if (offset >= 0) | |
319 | *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v; | |
320 | return; | |
321 | } | |
5f8c1a54 AK |
322 | wrmsrl(msr, v); |
323 | } | |
324 | ||
9b1beaf2 AK |
325 | /* |
326 | * Simple lockless ring to communicate PFNs from the exception handler with the | |
327 | * process context work function. This is vastly simplified because there's | |
328 | * only a single reader and a single writer. | |
329 | */ | |
330 | #define MCE_RING_SIZE 16 /* we use one entry less */ | |
331 | ||
332 | struct mce_ring { | |
333 | unsigned short start; | |
334 | unsigned short end; | |
335 | unsigned long ring[MCE_RING_SIZE]; | |
336 | }; | |
337 | static DEFINE_PER_CPU(struct mce_ring, mce_ring); | |
338 | ||
339 | /* Runs with CPU affinity in workqueue */ | |
340 | static int mce_ring_empty(void) | |
341 | { | |
342 | struct mce_ring *r = &__get_cpu_var(mce_ring); | |
343 | ||
344 | return r->start == r->end; | |
345 | } | |
346 | ||
347 | static int mce_ring_get(unsigned long *pfn) | |
348 | { | |
349 | struct mce_ring *r; | |
350 | int ret = 0; | |
351 | ||
352 | *pfn = 0; | |
353 | get_cpu(); | |
354 | r = &__get_cpu_var(mce_ring); | |
355 | if (r->start == r->end) | |
356 | goto out; | |
357 | *pfn = r->ring[r->start]; | |
358 | r->start = (r->start + 1) % MCE_RING_SIZE; | |
359 | ret = 1; | |
360 | out: | |
361 | put_cpu(); | |
362 | return ret; | |
363 | } | |
364 | ||
365 | /* Always runs in MCE context with preempt off */ | |
366 | static int mce_ring_add(unsigned long pfn) | |
367 | { | |
368 | struct mce_ring *r = &__get_cpu_var(mce_ring); | |
369 | unsigned next; | |
370 | ||
371 | next = (r->end + 1) % MCE_RING_SIZE; | |
372 | if (next == r->start) | |
373 | return -1; | |
374 | r->ring[r->end] = pfn; | |
375 | wmb(); | |
376 | r->end = next; | |
377 | return 0; | |
378 | } | |
379 | ||
88ccbedd | 380 | int mce_available(struct cpuinfo_x86 *c) |
1da177e4 | 381 | { |
04b2b1a4 | 382 | if (mce_disabled) |
5b4408fd | 383 | return 0; |
3d1712c9 | 384 | return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA); |
1da177e4 LT |
385 | } |
386 | ||
9b1beaf2 AK |
387 | static void mce_schedule_work(void) |
388 | { | |
389 | if (!mce_ring_empty()) { | |
390 | struct work_struct *work = &__get_cpu_var(mce_work); | |
391 | if (!work_pending(work)) | |
392 | schedule_work(work); | |
393 | } | |
394 | } | |
395 | ||
1b2797dc HY |
396 | /* |
397 | * Get the address of the instruction at the time of the machine check | |
398 | * error. | |
399 | */ | |
94ad8474 AK |
400 | static inline void mce_get_rip(struct mce *m, struct pt_regs *regs) |
401 | { | |
1b2797dc HY |
402 | |
403 | if (regs && (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV))) { | |
65ea5b03 | 404 | m->ip = regs->ip; |
94ad8474 AK |
405 | m->cs = regs->cs; |
406 | } else { | |
65ea5b03 | 407 | m->ip = 0; |
94ad8474 AK |
408 | m->cs = 0; |
409 | } | |
1b2797dc | 410 | if (rip_msr) |
5f8c1a54 | 411 | m->ip = mce_rdmsrl(rip_msr); |
94ad8474 AK |
412 | } |
413 | ||
ccc3c319 AK |
414 | #ifdef CONFIG_X86_LOCAL_APIC |
415 | /* | |
416 | * Called after interrupts have been reenabled again | |
417 | * when a MCE happened during an interrupts off region | |
418 | * in the kernel. | |
419 | */ | |
420 | asmlinkage void smp_mce_self_interrupt(struct pt_regs *regs) | |
421 | { | |
422 | ack_APIC_irq(); | |
423 | exit_idle(); | |
424 | irq_enter(); | |
9ff36ee9 | 425 | mce_notify_irq(); |
9b1beaf2 | 426 | mce_schedule_work(); |
ccc3c319 AK |
427 | irq_exit(); |
428 | } | |
429 | #endif | |
430 | ||
431 | static void mce_report_event(struct pt_regs *regs) | |
432 | { | |
433 | if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) { | |
9ff36ee9 | 434 | mce_notify_irq(); |
9b1beaf2 AK |
435 | /* |
436 | * Triggering the work queue here is just an insurance | |
437 | * policy in case the syscall exit notify handler | |
438 | * doesn't run soon enough or ends up running on the | |
439 | * wrong CPU (can happen when audit sleeps) | |
440 | */ | |
441 | mce_schedule_work(); | |
ccc3c319 AK |
442 | return; |
443 | } | |
444 | ||
445 | #ifdef CONFIG_X86_LOCAL_APIC | |
446 | /* | |
447 | * Without APIC do not notify. The event will be picked | |
448 | * up eventually. | |
449 | */ | |
450 | if (!cpu_has_apic) | |
451 | return; | |
452 | ||
453 | /* | |
454 | * When interrupts are disabled we cannot use | |
455 | * kernel services safely. Trigger an self interrupt | |
456 | * through the APIC to instead do the notification | |
457 | * after interrupts are reenabled again. | |
458 | */ | |
459 | apic->send_IPI_self(MCE_SELF_VECTOR); | |
460 | ||
461 | /* | |
462 | * Wait for idle afterwards again so that we don't leave the | |
463 | * APIC in a non idle state because the normal APIC writes | |
464 | * cannot exclude us. | |
465 | */ | |
466 | apic_wait_icr_idle(); | |
467 | #endif | |
468 | } | |
469 | ||
ca84f696 AK |
470 | DEFINE_PER_CPU(unsigned, mce_poll_count); |
471 | ||
d88203d1 | 472 | /* |
b79109c3 AK |
473 | * Poll for corrected events or events that happened before reset. |
474 | * Those are just logged through /dev/mcelog. | |
475 | * | |
476 | * This is executed in standard interrupt context. | |
ed7290d0 AK |
477 | * |
478 | * Note: spec recommends to panic for fatal unsignalled | |
479 | * errors here. However this would be quite problematic -- | |
480 | * we would need to reimplement the Monarch handling and | |
481 | * it would mess up the exclusion between exception handler | |
482 | * and poll hander -- * so we skip this for now. | |
483 | * These cases should not happen anyways, or only when the CPU | |
484 | * is already totally * confused. In this case it's likely it will | |
485 | * not fully execute the machine check handler either. | |
b79109c3 | 486 | */ |
ee031c31 | 487 | void machine_check_poll(enum mcp_flags flags, mce_banks_t *b) |
b79109c3 AK |
488 | { |
489 | struct mce m; | |
490 | int i; | |
491 | ||
ca84f696 AK |
492 | __get_cpu_var(mce_poll_count)++; |
493 | ||
b79109c3 AK |
494 | mce_setup(&m); |
495 | ||
5f8c1a54 | 496 | m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS); |
b79109c3 | 497 | for (i = 0; i < banks; i++) { |
ee031c31 | 498 | if (!bank[i] || !test_bit(i, *b)) |
b79109c3 AK |
499 | continue; |
500 | ||
501 | m.misc = 0; | |
502 | m.addr = 0; | |
503 | m.bank = i; | |
504 | m.tsc = 0; | |
505 | ||
506 | barrier(); | |
5f8c1a54 | 507 | m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4); |
b79109c3 AK |
508 | if (!(m.status & MCI_STATUS_VAL)) |
509 | continue; | |
510 | ||
511 | /* | |
ed7290d0 AK |
512 | * Uncorrected or signalled events are handled by the exception |
513 | * handler when it is enabled, so don't process those here. | |
b79109c3 AK |
514 | * |
515 | * TBD do the same check for MCI_STATUS_EN here? | |
516 | */ | |
ed7290d0 AK |
517 | if (!(flags & MCP_UC) && |
518 | (m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC))) | |
b79109c3 AK |
519 | continue; |
520 | ||
521 | if (m.status & MCI_STATUS_MISCV) | |
5f8c1a54 | 522 | m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4); |
b79109c3 | 523 | if (m.status & MCI_STATUS_ADDRV) |
5f8c1a54 | 524 | m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4); |
b79109c3 AK |
525 | |
526 | if (!(flags & MCP_TIMESTAMP)) | |
527 | m.tsc = 0; | |
528 | /* | |
529 | * Don't get the IP here because it's unlikely to | |
530 | * have anything to do with the actual error location. | |
531 | */ | |
62fdac59 | 532 | if (!(flags & MCP_DONTLOG) && !mce_dont_log_ce) { |
5679af4c AK |
533 | mce_log(&m); |
534 | add_taint(TAINT_MACHINE_CHECK); | |
535 | } | |
b79109c3 AK |
536 | |
537 | /* | |
538 | * Clear state for this bank. | |
539 | */ | |
5f8c1a54 | 540 | mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0); |
b79109c3 AK |
541 | } |
542 | ||
543 | /* | |
544 | * Don't clear MCG_STATUS here because it's only defined for | |
545 | * exceptions. | |
546 | */ | |
88921be3 AK |
547 | |
548 | sync_core(); | |
b79109c3 | 549 | } |
ea149b36 | 550 | EXPORT_SYMBOL_GPL(machine_check_poll); |
b79109c3 | 551 | |
bd19a5e6 AK |
552 | /* |
553 | * Do a quick check if any of the events requires a panic. | |
554 | * This decides if we keep the events around or clear them. | |
555 | */ | |
556 | static int mce_no_way_out(struct mce *m, char **msg) | |
557 | { | |
558 | int i; | |
559 | ||
560 | for (i = 0; i < banks; i++) { | |
561 | m->status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4); | |
562 | if (mce_severity(m, tolerant, msg) >= MCE_PANIC_SEVERITY) | |
563 | return 1; | |
564 | } | |
565 | return 0; | |
566 | } | |
567 | ||
3c079792 AK |
568 | /* |
569 | * Variable to establish order between CPUs while scanning. | |
570 | * Each CPU spins initially until executing is equal its number. | |
571 | */ | |
572 | static atomic_t mce_executing; | |
573 | ||
574 | /* | |
575 | * Defines order of CPUs on entry. First CPU becomes Monarch. | |
576 | */ | |
577 | static atomic_t mce_callin; | |
578 | ||
579 | /* | |
580 | * Check if a timeout waiting for other CPUs happened. | |
581 | */ | |
582 | static int mce_timed_out(u64 *t) | |
583 | { | |
584 | /* | |
585 | * The others already did panic for some reason. | |
586 | * Bail out like in a timeout. | |
587 | * rmb() to tell the compiler that system_state | |
588 | * might have been modified by someone else. | |
589 | */ | |
590 | rmb(); | |
591 | if (atomic_read(&mce_paniced)) | |
592 | wait_for_panic(); | |
593 | if (!monarch_timeout) | |
594 | goto out; | |
595 | if ((s64)*t < SPINUNIT) { | |
596 | /* CHECKME: Make panic default for 1 too? */ | |
597 | if (tolerant < 1) | |
598 | mce_panic("Timeout synchronizing machine check over CPUs", | |
599 | NULL, NULL); | |
600 | cpu_missing = 1; | |
601 | return 1; | |
602 | } | |
603 | *t -= SPINUNIT; | |
604 | out: | |
605 | touch_nmi_watchdog(); | |
606 | return 0; | |
607 | } | |
608 | ||
609 | /* | |
610 | * The Monarch's reign. The Monarch is the CPU who entered | |
611 | * the machine check handler first. It waits for the others to | |
612 | * raise the exception too and then grades them. When any | |
613 | * error is fatal panic. Only then let the others continue. | |
614 | * | |
615 | * The other CPUs entering the MCE handler will be controlled by the | |
616 | * Monarch. They are called Subjects. | |
617 | * | |
618 | * This way we prevent any potential data corruption in a unrecoverable case | |
619 | * and also makes sure always all CPU's errors are examined. | |
620 | * | |
621 | * Also this detects the case of an machine check event coming from outer | |
622 | * space (not detected by any CPUs) In this case some external agent wants | |
623 | * us to shut down, so panic too. | |
624 | * | |
625 | * The other CPUs might still decide to panic if the handler happens | |
626 | * in a unrecoverable place, but in this case the system is in a semi-stable | |
627 | * state and won't corrupt anything by itself. It's ok to let the others | |
628 | * continue for a bit first. | |
629 | * | |
630 | * All the spin loops have timeouts; when a timeout happens a CPU | |
631 | * typically elects itself to be Monarch. | |
632 | */ | |
633 | static void mce_reign(void) | |
634 | { | |
635 | int cpu; | |
636 | struct mce *m = NULL; | |
637 | int global_worst = 0; | |
638 | char *msg = NULL; | |
639 | char *nmsg = NULL; | |
640 | ||
641 | /* | |
642 | * This CPU is the Monarch and the other CPUs have run | |
643 | * through their handlers. | |
644 | * Grade the severity of the errors of all the CPUs. | |
645 | */ | |
646 | for_each_possible_cpu(cpu) { | |
647 | int severity = mce_severity(&per_cpu(mces_seen, cpu), tolerant, | |
648 | &nmsg); | |
649 | if (severity > global_worst) { | |
650 | msg = nmsg; | |
651 | global_worst = severity; | |
652 | m = &per_cpu(mces_seen, cpu); | |
653 | } | |
654 | } | |
655 | ||
656 | /* | |
657 | * Cannot recover? Panic here then. | |
658 | * This dumps all the mces in the log buffer and stops the | |
659 | * other CPUs. | |
660 | */ | |
661 | if (m && global_worst >= MCE_PANIC_SEVERITY && tolerant < 3) | |
ac960375 | 662 | mce_panic("Fatal Machine check", m, msg); |
3c079792 AK |
663 | |
664 | /* | |
665 | * For UC somewhere we let the CPU who detects it handle it. | |
666 | * Also must let continue the others, otherwise the handling | |
667 | * CPU could deadlock on a lock. | |
668 | */ | |
669 | ||
670 | /* | |
671 | * No machine check event found. Must be some external | |
672 | * source or one CPU is hung. Panic. | |
673 | */ | |
674 | if (!m && tolerant < 3) | |
675 | mce_panic("Machine check from unknown source", NULL, NULL); | |
676 | ||
677 | /* | |
678 | * Now clear all the mces_seen so that they don't reappear on | |
679 | * the next mce. | |
680 | */ | |
681 | for_each_possible_cpu(cpu) | |
682 | memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce)); | |
683 | } | |
684 | ||
685 | static atomic_t global_nwo; | |
686 | ||
687 | /* | |
688 | * Start of Monarch synchronization. This waits until all CPUs have | |
689 | * entered the exception handler and then determines if any of them | |
690 | * saw a fatal event that requires panic. Then it executes them | |
691 | * in the entry order. | |
692 | * TBD double check parallel CPU hotunplug | |
693 | */ | |
694 | static int mce_start(int no_way_out, int *order) | |
695 | { | |
696 | int nwo; | |
697 | int cpus = num_online_cpus(); | |
698 | u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC; | |
699 | ||
700 | if (!timeout) { | |
701 | *order = -1; | |
702 | return no_way_out; | |
703 | } | |
704 | ||
705 | atomic_add(no_way_out, &global_nwo); | |
184e1fdf HY |
706 | /* |
707 | * global_nwo should be updated before mce_callin | |
708 | */ | |
709 | smp_wmb(); | |
710 | *order = atomic_add_return(1, &mce_callin); | |
3c079792 AK |
711 | |
712 | /* | |
713 | * Wait for everyone. | |
714 | */ | |
715 | while (atomic_read(&mce_callin) != cpus) { | |
716 | if (mce_timed_out(&timeout)) { | |
717 | atomic_set(&global_nwo, 0); | |
718 | *order = -1; | |
719 | return no_way_out; | |
720 | } | |
721 | ndelay(SPINUNIT); | |
722 | } | |
723 | ||
184e1fdf HY |
724 | /* |
725 | * mce_callin should be read before global_nwo | |
726 | */ | |
727 | smp_rmb(); | |
3c079792 AK |
728 | /* |
729 | * Cache the global no_way_out state. | |
730 | */ | |
731 | nwo = atomic_read(&global_nwo); | |
732 | ||
733 | /* | |
734 | * Monarch starts executing now, the others wait. | |
735 | */ | |
736 | if (*order == 1) { | |
737 | atomic_set(&mce_executing, 1); | |
738 | return nwo; | |
739 | } | |
740 | ||
741 | /* | |
742 | * Now start the scanning loop one by one | |
743 | * in the original callin order. | |
744 | * This way when there are any shared banks it will | |
745 | * be only seen by one CPU before cleared, avoiding duplicates. | |
746 | */ | |
747 | while (atomic_read(&mce_executing) < *order) { | |
748 | if (mce_timed_out(&timeout)) { | |
749 | atomic_set(&global_nwo, 0); | |
750 | *order = -1; | |
751 | return no_way_out; | |
752 | } | |
753 | ndelay(SPINUNIT); | |
754 | } | |
755 | return nwo; | |
756 | } | |
757 | ||
758 | /* | |
759 | * Synchronize between CPUs after main scanning loop. | |
760 | * This invokes the bulk of the Monarch processing. | |
761 | */ | |
762 | static int mce_end(int order) | |
763 | { | |
764 | int ret = -1; | |
765 | u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC; | |
766 | ||
767 | if (!timeout) | |
768 | goto reset; | |
769 | if (order < 0) | |
770 | goto reset; | |
771 | ||
772 | /* | |
773 | * Allow others to run. | |
774 | */ | |
775 | atomic_inc(&mce_executing); | |
776 | ||
777 | if (order == 1) { | |
778 | /* CHECKME: Can this race with a parallel hotplug? */ | |
779 | int cpus = num_online_cpus(); | |
780 | ||
781 | /* | |
782 | * Monarch: Wait for everyone to go through their scanning | |
783 | * loops. | |
784 | */ | |
785 | while (atomic_read(&mce_executing) <= cpus) { | |
786 | if (mce_timed_out(&timeout)) | |
787 | goto reset; | |
788 | ndelay(SPINUNIT); | |
789 | } | |
790 | ||
791 | mce_reign(); | |
792 | barrier(); | |
793 | ret = 0; | |
794 | } else { | |
795 | /* | |
796 | * Subject: Wait for Monarch to finish. | |
797 | */ | |
798 | while (atomic_read(&mce_executing) != 0) { | |
799 | if (mce_timed_out(&timeout)) | |
800 | goto reset; | |
801 | ndelay(SPINUNIT); | |
802 | } | |
803 | ||
804 | /* | |
805 | * Don't reset anything. That's done by the Monarch. | |
806 | */ | |
807 | return 0; | |
808 | } | |
809 | ||
810 | /* | |
811 | * Reset all global state. | |
812 | */ | |
813 | reset: | |
814 | atomic_set(&global_nwo, 0); | |
815 | atomic_set(&mce_callin, 0); | |
816 | barrier(); | |
817 | ||
818 | /* | |
819 | * Let others run again. | |
820 | */ | |
821 | atomic_set(&mce_executing, 0); | |
822 | return ret; | |
823 | } | |
824 | ||
9b1beaf2 AK |
825 | /* |
826 | * Check if the address reported by the CPU is in a format we can parse. | |
827 | * It would be possible to add code for most other cases, but all would | |
828 | * be somewhat complicated (e.g. segment offset would require an instruction | |
829 | * parser). So only support physical addresses upto page granuality for now. | |
830 | */ | |
831 | static int mce_usable_address(struct mce *m) | |
832 | { | |
833 | if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV)) | |
834 | return 0; | |
835 | if ((m->misc & 0x3f) > PAGE_SHIFT) | |
836 | return 0; | |
837 | if (((m->misc >> 6) & 7) != MCM_ADDR_PHYS) | |
838 | return 0; | |
839 | return 1; | |
840 | } | |
841 | ||
3c079792 AK |
842 | static void mce_clear_state(unsigned long *toclear) |
843 | { | |
844 | int i; | |
845 | ||
846 | for (i = 0; i < banks; i++) { | |
847 | if (test_bit(i, toclear)) | |
848 | mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0); | |
849 | } | |
850 | } | |
851 | ||
b79109c3 AK |
852 | /* |
853 | * The actual machine check handler. This only handles real | |
854 | * exceptions when something got corrupted coming in through int 18. | |
855 | * | |
856 | * This is executed in NMI context not subject to normal locking rules. This | |
857 | * implies that most kernel services cannot be safely used. Don't even | |
858 | * think about putting a printk in there! | |
3c079792 AK |
859 | * |
860 | * On Intel systems this is entered on all CPUs in parallel through | |
861 | * MCE broadcast. However some CPUs might be broken beyond repair, | |
862 | * so be always careful when synchronizing with others. | |
1da177e4 | 863 | */ |
e9eee03e | 864 | void do_machine_check(struct pt_regs *regs, long error_code) |
1da177e4 | 865 | { |
3c079792 | 866 | struct mce m, *final; |
1da177e4 | 867 | int i; |
3c079792 AK |
868 | int worst = 0; |
869 | int severity; | |
870 | /* | |
871 | * Establish sequential order between the CPUs entering the machine | |
872 | * check handler. | |
873 | */ | |
184e1fdf | 874 | int order = -1; |
3c079792 | 875 | |
bd78432c TH |
876 | /* |
877 | * If no_way_out gets set, there is no safe way to recover from this | |
878 | * MCE. If tolerant is cranked up, we'll try anyway. | |
879 | */ | |
880 | int no_way_out = 0; | |
881 | /* | |
882 | * If kill_it gets set, there might be a way to recover from this | |
883 | * error. | |
884 | */ | |
885 | int kill_it = 0; | |
b79109c3 | 886 | DECLARE_BITMAP(toclear, MAX_NR_BANKS); |
bd19a5e6 | 887 | char *msg = "Unknown"; |
1da177e4 | 888 | |
553f265f AK |
889 | atomic_inc(&mce_entry); |
890 | ||
01ca79f1 AK |
891 | __get_cpu_var(mce_exception_count)++; |
892 | ||
b79109c3 | 893 | if (notify_die(DIE_NMI, "machine check", regs, error_code, |
22f5991c | 894 | 18, SIGKILL) == NOTIFY_STOP) |
32561696 | 895 | goto out; |
b79109c3 | 896 | if (!banks) |
32561696 | 897 | goto out; |
1da177e4 | 898 | |
b5f2fa4e AK |
899 | mce_setup(&m); |
900 | ||
5f8c1a54 | 901 | m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS); |
bd19a5e6 | 902 | no_way_out = mce_no_way_out(&m, &msg); |
d88203d1 | 903 | |
3c079792 AK |
904 | final = &__get_cpu_var(mces_seen); |
905 | *final = m; | |
906 | ||
1da177e4 LT |
907 | barrier(); |
908 | ||
ed7290d0 AK |
909 | /* |
910 | * When no restart IP must always kill or panic. | |
911 | */ | |
912 | if (!(m.mcgstatus & MCG_STATUS_RIPV)) | |
913 | kill_it = 1; | |
914 | ||
3c079792 AK |
915 | /* |
916 | * Go through all the banks in exclusion of the other CPUs. | |
917 | * This way we don't report duplicated events on shared banks | |
918 | * because the first one to see it will clear it. | |
919 | */ | |
920 | no_way_out = mce_start(no_way_out, &order); | |
1da177e4 | 921 | for (i = 0; i < banks; i++) { |
b79109c3 | 922 | __clear_bit(i, toclear); |
0d7482e3 | 923 | if (!bank[i]) |
1da177e4 | 924 | continue; |
d88203d1 TG |
925 | |
926 | m.misc = 0; | |
1da177e4 LT |
927 | m.addr = 0; |
928 | m.bank = i; | |
1da177e4 | 929 | |
5f8c1a54 | 930 | m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4); |
1da177e4 LT |
931 | if ((m.status & MCI_STATUS_VAL) == 0) |
932 | continue; | |
933 | ||
b79109c3 | 934 | /* |
ed7290d0 AK |
935 | * Non uncorrected or non signaled errors are handled by |
936 | * machine_check_poll. Leave them alone, unless this panics. | |
b79109c3 | 937 | */ |
ed7290d0 AK |
938 | if (!(m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)) && |
939 | !no_way_out) | |
b79109c3 AK |
940 | continue; |
941 | ||
942 | /* | |
943 | * Set taint even when machine check was not enabled. | |
944 | */ | |
945 | add_taint(TAINT_MACHINE_CHECK); | |
946 | ||
ed7290d0 | 947 | severity = mce_severity(&m, tolerant, NULL); |
b79109c3 | 948 | |
ed7290d0 AK |
949 | /* |
950 | * When machine check was for corrected handler don't touch, | |
951 | * unless we're panicing. | |
952 | */ | |
953 | if (severity == MCE_KEEP_SEVERITY && !no_way_out) | |
954 | continue; | |
955 | __set_bit(i, toclear); | |
956 | if (severity == MCE_NO_SEVERITY) { | |
b79109c3 AK |
957 | /* |
958 | * Machine check event was not enabled. Clear, but | |
959 | * ignore. | |
960 | */ | |
961 | continue; | |
1da177e4 LT |
962 | } |
963 | ||
ed7290d0 AK |
964 | /* |
965 | * Kill on action required. | |
966 | */ | |
967 | if (severity == MCE_AR_SEVERITY) | |
968 | kill_it = 1; | |
969 | ||
1da177e4 | 970 | if (m.status & MCI_STATUS_MISCV) |
5f8c1a54 | 971 | m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4); |
1da177e4 | 972 | if (m.status & MCI_STATUS_ADDRV) |
5f8c1a54 | 973 | m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4); |
1da177e4 | 974 | |
9b1beaf2 AK |
975 | /* |
976 | * Action optional error. Queue address for later processing. | |
977 | * When the ring overflows we just ignore the AO error. | |
978 | * RED-PEN add some logging mechanism when | |
979 | * usable_address or mce_add_ring fails. | |
980 | * RED-PEN don't ignore overflow for tolerant == 0 | |
981 | */ | |
982 | if (severity == MCE_AO_SEVERITY && mce_usable_address(&m)) | |
983 | mce_ring_add(m.addr >> PAGE_SHIFT); | |
984 | ||
94ad8474 | 985 | mce_get_rip(&m, regs); |
b79109c3 | 986 | mce_log(&m); |
1da177e4 | 987 | |
3c079792 AK |
988 | if (severity > worst) { |
989 | *final = m; | |
990 | worst = severity; | |
1da177e4 | 991 | } |
1da177e4 LT |
992 | } |
993 | ||
3c079792 AK |
994 | if (!no_way_out) |
995 | mce_clear_state(toclear); | |
996 | ||
e9eee03e | 997 | /* |
3c079792 AK |
998 | * Do most of the synchronization with other CPUs. |
999 | * When there's any problem use only local no_way_out state. | |
e9eee03e | 1000 | */ |
3c079792 AK |
1001 | if (mce_end(order) < 0) |
1002 | no_way_out = worst >= MCE_PANIC_SEVERITY; | |
bd78432c TH |
1003 | |
1004 | /* | |
1005 | * If we have decided that we just CAN'T continue, and the user | |
e9eee03e | 1006 | * has not set tolerant to an insane level, give up and die. |
3c079792 AK |
1007 | * |
1008 | * This is mainly used in the case when the system doesn't | |
1009 | * support MCE broadcasting or it has been disabled. | |
bd78432c TH |
1010 | */ |
1011 | if (no_way_out && tolerant < 3) | |
ac960375 | 1012 | mce_panic("Fatal machine check on current CPU", final, msg); |
bd78432c TH |
1013 | |
1014 | /* | |
1015 | * If the error seems to be unrecoverable, something should be | |
1016 | * done. Try to kill as little as possible. If we can kill just | |
1017 | * one task, do that. If the user has set the tolerance very | |
1018 | * high, don't try to do anything at all. | |
1019 | */ | |
bd78432c | 1020 | |
ed7290d0 AK |
1021 | if (kill_it && tolerant < 3) |
1022 | force_sig(SIGBUS, current); | |
1da177e4 | 1023 | |
e02e68d3 TH |
1024 | /* notify userspace ASAP */ |
1025 | set_thread_flag(TIF_MCE_NOTIFY); | |
1026 | ||
3c079792 AK |
1027 | if (worst > 0) |
1028 | mce_report_event(regs); | |
5f8c1a54 | 1029 | mce_wrmsrl(MSR_IA32_MCG_STATUS, 0); |
32561696 | 1030 | out: |
553f265f | 1031 | atomic_dec(&mce_entry); |
88921be3 | 1032 | sync_core(); |
1da177e4 | 1033 | } |
ea149b36 | 1034 | EXPORT_SYMBOL_GPL(do_machine_check); |
1da177e4 | 1035 | |
9b1beaf2 AK |
1036 | /* dummy to break dependency. actual code is in mm/memory-failure.c */ |
1037 | void __attribute__((weak)) memory_failure(unsigned long pfn, int vector) | |
1038 | { | |
1039 | printk(KERN_ERR "Action optional memory failure at %lx ignored\n", pfn); | |
1040 | } | |
1041 | ||
1042 | /* | |
1043 | * Called after mce notification in process context. This code | |
1044 | * is allowed to sleep. Call the high level VM handler to process | |
1045 | * any corrupted pages. | |
1046 | * Assume that the work queue code only calls this one at a time | |
1047 | * per CPU. | |
1048 | * Note we don't disable preemption, so this code might run on the wrong | |
1049 | * CPU. In this case the event is picked up by the scheduled work queue. | |
1050 | * This is merely a fast path to expedite processing in some common | |
1051 | * cases. | |
1052 | */ | |
1053 | void mce_notify_process(void) | |
1054 | { | |
1055 | unsigned long pfn; | |
1056 | mce_notify_irq(); | |
1057 | while (mce_ring_get(&pfn)) | |
1058 | memory_failure(pfn, MCE_VECTOR); | |
1059 | } | |
1060 | ||
1061 | static void mce_process_work(struct work_struct *dummy) | |
1062 | { | |
1063 | mce_notify_process(); | |
1064 | } | |
1065 | ||
15d5f839 DZ |
1066 | #ifdef CONFIG_X86_MCE_INTEL |
1067 | /*** | |
1068 | * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog | |
676b1855 | 1069 | * @cpu: The CPU on which the event occurred. |
15d5f839 DZ |
1070 | * @status: Event status information |
1071 | * | |
1072 | * This function should be called by the thermal interrupt after the | |
1073 | * event has been processed and the decision was made to log the event | |
1074 | * further. | |
1075 | * | |
1076 | * The status parameter will be saved to the 'status' field of 'struct mce' | |
1077 | * and historically has been the register value of the | |
1078 | * MSR_IA32_THERMAL_STATUS (Intel) msr. | |
1079 | */ | |
b5f2fa4e | 1080 | void mce_log_therm_throt_event(__u64 status) |
15d5f839 DZ |
1081 | { |
1082 | struct mce m; | |
1083 | ||
b5f2fa4e | 1084 | mce_setup(&m); |
15d5f839 DZ |
1085 | m.bank = MCE_THERMAL_BANK; |
1086 | m.status = status; | |
15d5f839 DZ |
1087 | mce_log(&m); |
1088 | } | |
1089 | #endif /* CONFIG_X86_MCE_INTEL */ | |
1090 | ||
1da177e4 | 1091 | /* |
8a336b0a TH |
1092 | * Periodic polling timer for "silent" machine check errors. If the |
1093 | * poller finds an MCE, poll 2x faster. When the poller finds no more | |
1094 | * errors, poll 2x slower (up to check_interval seconds). | |
1da177e4 | 1095 | */ |
1da177e4 | 1096 | static int check_interval = 5 * 60; /* 5 minutes */ |
e9eee03e | 1097 | |
6298c512 | 1098 | static DEFINE_PER_CPU(int, next_interval); /* in jiffies */ |
52d168e2 | 1099 | static DEFINE_PER_CPU(struct timer_list, mce_timer); |
1da177e4 | 1100 | |
52d168e2 | 1101 | static void mcheck_timer(unsigned long data) |
1da177e4 | 1102 | { |
52d168e2 | 1103 | struct timer_list *t = &per_cpu(mce_timer, data); |
6298c512 | 1104 | int *n; |
52d168e2 AK |
1105 | |
1106 | WARN_ON(smp_processor_id() != data); | |
1107 | ||
e9eee03e | 1108 | if (mce_available(¤t_cpu_data)) { |
ee031c31 AK |
1109 | machine_check_poll(MCP_TIMESTAMP, |
1110 | &__get_cpu_var(mce_poll_banks)); | |
e9eee03e | 1111 | } |
1da177e4 LT |
1112 | |
1113 | /* | |
e02e68d3 TH |
1114 | * Alert userspace if needed. If we logged an MCE, reduce the |
1115 | * polling interval, otherwise increase the polling interval. | |
1da177e4 | 1116 | */ |
6298c512 | 1117 | n = &__get_cpu_var(next_interval); |
9ff36ee9 | 1118 | if (mce_notify_irq()) |
6298c512 | 1119 | *n = max(*n/2, HZ/100); |
14a02530 | 1120 | else |
6298c512 | 1121 | *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ)); |
e02e68d3 | 1122 | |
6298c512 | 1123 | t->expires = jiffies + *n; |
52d168e2 | 1124 | add_timer(t); |
e02e68d3 TH |
1125 | } |
1126 | ||
9bd98405 AK |
1127 | static void mce_do_trigger(struct work_struct *work) |
1128 | { | |
1129 | call_usermodehelper(trigger, trigger_argv, NULL, UMH_NO_WAIT); | |
1130 | } | |
1131 | ||
1132 | static DECLARE_WORK(mce_trigger_work, mce_do_trigger); | |
1133 | ||
e02e68d3 | 1134 | /* |
9bd98405 AK |
1135 | * Notify the user(s) about new machine check events. |
1136 | * Can be called from interrupt context, but not from machine check/NMI | |
1137 | * context. | |
e02e68d3 | 1138 | */ |
9ff36ee9 | 1139 | int mce_notify_irq(void) |
e02e68d3 | 1140 | { |
8457c84d AK |
1141 | /* Not more than two messages every minute */ |
1142 | static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2); | |
1143 | ||
e02e68d3 | 1144 | clear_thread_flag(TIF_MCE_NOTIFY); |
e9eee03e | 1145 | |
e02e68d3 | 1146 | if (test_and_clear_bit(0, ¬ify_user)) { |
e02e68d3 | 1147 | wake_up_interruptible(&mce_wait); |
9bd98405 AK |
1148 | |
1149 | /* | |
1150 | * There is no risk of missing notifications because | |
1151 | * work_pending is always cleared before the function is | |
1152 | * executed. | |
1153 | */ | |
1154 | if (trigger[0] && !work_pending(&mce_trigger_work)) | |
1155 | schedule_work(&mce_trigger_work); | |
e02e68d3 | 1156 | |
8457c84d | 1157 | if (__ratelimit(&ratelimit)) |
8a336b0a | 1158 | printk(KERN_INFO "Machine check events logged\n"); |
e02e68d3 TH |
1159 | |
1160 | return 1; | |
1da177e4 | 1161 | } |
e02e68d3 TH |
1162 | return 0; |
1163 | } | |
9ff36ee9 | 1164 | EXPORT_SYMBOL_GPL(mce_notify_irq); |
8a336b0a | 1165 | |
d88203d1 | 1166 | /* |
1da177e4 LT |
1167 | * Initialize Machine Checks for a CPU. |
1168 | */ | |
0d7482e3 | 1169 | static int mce_cap_init(void) |
1da177e4 | 1170 | { |
0d7482e3 | 1171 | unsigned b; |
e9eee03e | 1172 | u64 cap; |
1da177e4 LT |
1173 | |
1174 | rdmsrl(MSR_IA32_MCG_CAP, cap); | |
01c6680a TG |
1175 | |
1176 | b = cap & MCG_BANKCNT_MASK; | |
b659294b IM |
1177 | printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b); |
1178 | ||
0d7482e3 AK |
1179 | if (b > MAX_NR_BANKS) { |
1180 | printk(KERN_WARNING | |
1181 | "MCE: Using only %u machine check banks out of %u\n", | |
1182 | MAX_NR_BANKS, b); | |
1183 | b = MAX_NR_BANKS; | |
1184 | } | |
1185 | ||
1186 | /* Don't support asymmetric configurations today */ | |
1187 | WARN_ON(banks != 0 && b != banks); | |
1188 | banks = b; | |
1189 | if (!bank) { | |
1190 | bank = kmalloc(banks * sizeof(u64), GFP_KERNEL); | |
1191 | if (!bank) | |
1192 | return -ENOMEM; | |
1193 | memset(bank, 0xff, banks * sizeof(u64)); | |
1da177e4 | 1194 | } |
0d7482e3 | 1195 | |
94ad8474 | 1196 | /* Use accurate RIP reporting if available. */ |
01c6680a | 1197 | if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9) |
94ad8474 | 1198 | rip_msr = MSR_IA32_MCG_EIP; |
1da177e4 | 1199 | |
ed7290d0 AK |
1200 | if (cap & MCG_SER_P) |
1201 | mce_ser = 1; | |
1202 | ||
0d7482e3 AK |
1203 | return 0; |
1204 | } | |
1205 | ||
8be91105 | 1206 | static void mce_init(void) |
0d7482e3 | 1207 | { |
e9eee03e | 1208 | mce_banks_t all_banks; |
0d7482e3 AK |
1209 | u64 cap; |
1210 | int i; | |
1211 | ||
b79109c3 AK |
1212 | /* |
1213 | * Log the machine checks left over from the previous reset. | |
1214 | */ | |
ee031c31 | 1215 | bitmap_fill(all_banks, MAX_NR_BANKS); |
5679af4c | 1216 | machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks); |
1da177e4 LT |
1217 | |
1218 | set_in_cr4(X86_CR4_MCE); | |
1219 | ||
0d7482e3 | 1220 | rdmsrl(MSR_IA32_MCG_CAP, cap); |
1da177e4 LT |
1221 | if (cap & MCG_CTL_P) |
1222 | wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff); | |
1223 | ||
1224 | for (i = 0; i < banks; i++) { | |
06b7a7a5 AK |
1225 | if (skip_bank_init(i)) |
1226 | continue; | |
0d7482e3 | 1227 | wrmsrl(MSR_IA32_MC0_CTL+4*i, bank[i]); |
1da177e4 | 1228 | wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0); |
d88203d1 | 1229 | } |
1da177e4 LT |
1230 | } |
1231 | ||
1232 | /* Add per CPU specific workarounds here */ | |
ec5b3d32 | 1233 | static void mce_cpu_quirks(struct cpuinfo_x86 *c) |
d88203d1 | 1234 | { |
1da177e4 | 1235 | /* This should be disabled by the BIOS, but isn't always */ |
911f6a7b | 1236 | if (c->x86_vendor == X86_VENDOR_AMD) { |
e9eee03e IM |
1237 | if (c->x86 == 15 && banks > 4) { |
1238 | /* | |
1239 | * disable GART TBL walk error reporting, which | |
1240 | * trips off incorrectly with the IOMMU & 3ware | |
1241 | * & Cerberus: | |
1242 | */ | |
0d7482e3 | 1243 | clear_bit(10, (unsigned long *)&bank[4]); |
e9eee03e IM |
1244 | } |
1245 | if (c->x86 <= 17 && mce_bootlog < 0) { | |
1246 | /* | |
1247 | * Lots of broken BIOS around that don't clear them | |
1248 | * by default and leave crap in there. Don't log: | |
1249 | */ | |
911f6a7b | 1250 | mce_bootlog = 0; |
e9eee03e | 1251 | } |
2e6f694f AK |
1252 | /* |
1253 | * Various K7s with broken bank 0 around. Always disable | |
1254 | * by default. | |
1255 | */ | |
1256 | if (c->x86 == 6) | |
1257 | bank[0] = 0; | |
1da177e4 | 1258 | } |
e583538f | 1259 | |
06b7a7a5 AK |
1260 | if (c->x86_vendor == X86_VENDOR_INTEL) { |
1261 | /* | |
1262 | * SDM documents that on family 6 bank 0 should not be written | |
1263 | * because it aliases to another special BIOS controlled | |
1264 | * register. | |
1265 | * But it's not aliased anymore on model 0x1a+ | |
1266 | * Don't ignore bank 0 completely because there could be a | |
1267 | * valid event later, merely don't write CTL0. | |
1268 | */ | |
1269 | ||
1270 | if (c->x86 == 6 && c->x86_model < 0x1A) | |
1271 | __set_bit(0, &dont_init_banks); | |
3c079792 AK |
1272 | |
1273 | /* | |
1274 | * All newer Intel systems support MCE broadcasting. Enable | |
1275 | * synchronization with a one second timeout. | |
1276 | */ | |
1277 | if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) && | |
1278 | monarch_timeout < 0) | |
1279 | monarch_timeout = USEC_PER_SEC; | |
06b7a7a5 | 1280 | } |
3c079792 AK |
1281 | if (monarch_timeout < 0) |
1282 | monarch_timeout = 0; | |
29b0f591 AK |
1283 | if (mce_bootlog != 0) |
1284 | mce_panic_timeout = 30; | |
d88203d1 | 1285 | } |
1da177e4 | 1286 | |
4efc0670 AK |
1287 | static void __cpuinit mce_ancient_init(struct cpuinfo_x86 *c) |
1288 | { | |
1289 | if (c->x86 != 5) | |
1290 | return; | |
1291 | switch (c->x86_vendor) { | |
1292 | case X86_VENDOR_INTEL: | |
1293 | if (mce_p5_enabled()) | |
1294 | intel_p5_mcheck_init(c); | |
1295 | break; | |
1296 | case X86_VENDOR_CENTAUR: | |
1297 | winchip_mcheck_init(c); | |
1298 | break; | |
1299 | } | |
1300 | } | |
1301 | ||
cc3ca220 | 1302 | static void mce_cpu_features(struct cpuinfo_x86 *c) |
1da177e4 LT |
1303 | { |
1304 | switch (c->x86_vendor) { | |
1305 | case X86_VENDOR_INTEL: | |
1306 | mce_intel_feature_init(c); | |
1307 | break; | |
89b831ef JS |
1308 | case X86_VENDOR_AMD: |
1309 | mce_amd_feature_init(c); | |
1310 | break; | |
1da177e4 LT |
1311 | default: |
1312 | break; | |
1313 | } | |
1314 | } | |
1315 | ||
52d168e2 AK |
1316 | static void mce_init_timer(void) |
1317 | { | |
1318 | struct timer_list *t = &__get_cpu_var(mce_timer); | |
6298c512 | 1319 | int *n = &__get_cpu_var(next_interval); |
52d168e2 | 1320 | |
62fdac59 HS |
1321 | if (mce_ignore_ce) |
1322 | return; | |
1323 | ||
6298c512 AK |
1324 | *n = check_interval * HZ; |
1325 | if (!*n) | |
52d168e2 AK |
1326 | return; |
1327 | setup_timer(t, mcheck_timer, smp_processor_id()); | |
6298c512 | 1328 | t->expires = round_jiffies(jiffies + *n); |
52d168e2 AK |
1329 | add_timer(t); |
1330 | } | |
1331 | ||
d88203d1 | 1332 | /* |
1da177e4 | 1333 | * Called for each booted CPU to set up machine checks. |
e9eee03e | 1334 | * Must be called with preempt off: |
1da177e4 | 1335 | */ |
e6982c67 | 1336 | void __cpuinit mcheck_init(struct cpuinfo_x86 *c) |
1da177e4 | 1337 | { |
4efc0670 AK |
1338 | if (mce_disabled) |
1339 | return; | |
1340 | ||
1341 | mce_ancient_init(c); | |
1342 | ||
5b4408fd | 1343 | if (!mce_available(c)) |
1da177e4 LT |
1344 | return; |
1345 | ||
0d7482e3 | 1346 | if (mce_cap_init() < 0) { |
04b2b1a4 | 1347 | mce_disabled = 1; |
0d7482e3 AK |
1348 | return; |
1349 | } | |
1350 | mce_cpu_quirks(c); | |
1351 | ||
5d727926 AK |
1352 | machine_check_vector = do_machine_check; |
1353 | ||
8be91105 | 1354 | mce_init(); |
1da177e4 | 1355 | mce_cpu_features(c); |
52d168e2 | 1356 | mce_init_timer(); |
9b1beaf2 | 1357 | INIT_WORK(&__get_cpu_var(mce_work), mce_process_work); |
1da177e4 LT |
1358 | } |
1359 | ||
1360 | /* | |
1361 | * Character device to read and clear the MCE log. | |
1362 | */ | |
1363 | ||
f528e7ba | 1364 | static DEFINE_SPINLOCK(mce_state_lock); |
e9eee03e IM |
1365 | static int open_count; /* #times opened */ |
1366 | static int open_exclu; /* already open exclusive? */ | |
f528e7ba TH |
1367 | |
1368 | static int mce_open(struct inode *inode, struct file *file) | |
1369 | { | |
1370 | spin_lock(&mce_state_lock); | |
1371 | ||
1372 | if (open_exclu || (open_count && (file->f_flags & O_EXCL))) { | |
1373 | spin_unlock(&mce_state_lock); | |
e9eee03e | 1374 | |
f528e7ba TH |
1375 | return -EBUSY; |
1376 | } | |
1377 | ||
1378 | if (file->f_flags & O_EXCL) | |
1379 | open_exclu = 1; | |
1380 | open_count++; | |
1381 | ||
1382 | spin_unlock(&mce_state_lock); | |
1383 | ||
bd78432c | 1384 | return nonseekable_open(inode, file); |
f528e7ba TH |
1385 | } |
1386 | ||
1387 | static int mce_release(struct inode *inode, struct file *file) | |
1388 | { | |
1389 | spin_lock(&mce_state_lock); | |
1390 | ||
1391 | open_count--; | |
1392 | open_exclu = 0; | |
1393 | ||
1394 | spin_unlock(&mce_state_lock); | |
1395 | ||
1396 | return 0; | |
1397 | } | |
1398 | ||
d88203d1 TG |
1399 | static void collect_tscs(void *data) |
1400 | { | |
1da177e4 | 1401 | unsigned long *cpu_tsc = (unsigned long *)data; |
d88203d1 | 1402 | |
1da177e4 | 1403 | rdtscll(cpu_tsc[smp_processor_id()]); |
d88203d1 | 1404 | } |
1da177e4 | 1405 | |
e9eee03e IM |
1406 | static DEFINE_MUTEX(mce_read_mutex); |
1407 | ||
d88203d1 TG |
1408 | static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize, |
1409 | loff_t *off) | |
1da177e4 | 1410 | { |
e9eee03e | 1411 | char __user *buf = ubuf; |
f0de53bb | 1412 | unsigned long *cpu_tsc; |
ef41df43 | 1413 | unsigned prev, next; |
1da177e4 LT |
1414 | int i, err; |
1415 | ||
6bca67f9 | 1416 | cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL); |
f0de53bb AK |
1417 | if (!cpu_tsc) |
1418 | return -ENOMEM; | |
1419 | ||
8c8b8859 | 1420 | mutex_lock(&mce_read_mutex); |
1da177e4 LT |
1421 | next = rcu_dereference(mcelog.next); |
1422 | ||
1423 | /* Only supports full reads right now */ | |
d88203d1 | 1424 | if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce)) { |
8c8b8859 | 1425 | mutex_unlock(&mce_read_mutex); |
f0de53bb | 1426 | kfree(cpu_tsc); |
e9eee03e | 1427 | |
1da177e4 LT |
1428 | return -EINVAL; |
1429 | } | |
1430 | ||
1431 | err = 0; | |
ef41df43 HY |
1432 | prev = 0; |
1433 | do { | |
1434 | for (i = prev; i < next; i++) { | |
1435 | unsigned long start = jiffies; | |
1436 | ||
1437 | while (!mcelog.entry[i].finished) { | |
1438 | if (time_after_eq(jiffies, start + 2)) { | |
1439 | memset(mcelog.entry + i, 0, | |
1440 | sizeof(struct mce)); | |
1441 | goto timeout; | |
1442 | } | |
1443 | cpu_relax(); | |
673242c1 | 1444 | } |
ef41df43 HY |
1445 | smp_rmb(); |
1446 | err |= copy_to_user(buf, mcelog.entry + i, | |
1447 | sizeof(struct mce)); | |
1448 | buf += sizeof(struct mce); | |
1449 | timeout: | |
1450 | ; | |
673242c1 | 1451 | } |
1da177e4 | 1452 | |
ef41df43 HY |
1453 | memset(mcelog.entry + prev, 0, |
1454 | (next - prev) * sizeof(struct mce)); | |
1455 | prev = next; | |
1456 | next = cmpxchg(&mcelog.next, prev, 0); | |
1457 | } while (next != prev); | |
1da177e4 | 1458 | |
b2b18660 | 1459 | synchronize_sched(); |
1da177e4 | 1460 | |
d88203d1 TG |
1461 | /* |
1462 | * Collect entries that were still getting written before the | |
1463 | * synchronize. | |
1464 | */ | |
15c8b6c1 | 1465 | on_each_cpu(collect_tscs, cpu_tsc, 1); |
e9eee03e | 1466 | |
d88203d1 TG |
1467 | for (i = next; i < MCE_LOG_LEN; i++) { |
1468 | if (mcelog.entry[i].finished && | |
1469 | mcelog.entry[i].tsc < cpu_tsc[mcelog.entry[i].cpu]) { | |
1470 | err |= copy_to_user(buf, mcelog.entry+i, | |
1471 | sizeof(struct mce)); | |
1da177e4 LT |
1472 | smp_rmb(); |
1473 | buf += sizeof(struct mce); | |
1474 | memset(&mcelog.entry[i], 0, sizeof(struct mce)); | |
1475 | } | |
d88203d1 | 1476 | } |
8c8b8859 | 1477 | mutex_unlock(&mce_read_mutex); |
f0de53bb | 1478 | kfree(cpu_tsc); |
e9eee03e | 1479 | |
d88203d1 | 1480 | return err ? -EFAULT : buf - ubuf; |
1da177e4 LT |
1481 | } |
1482 | ||
e02e68d3 TH |
1483 | static unsigned int mce_poll(struct file *file, poll_table *wait) |
1484 | { | |
1485 | poll_wait(file, &mce_wait, wait); | |
1486 | if (rcu_dereference(mcelog.next)) | |
1487 | return POLLIN | POLLRDNORM; | |
1488 | return 0; | |
1489 | } | |
1490 | ||
c68461b6 | 1491 | static long mce_ioctl(struct file *f, unsigned int cmd, unsigned long arg) |
1da177e4 LT |
1492 | { |
1493 | int __user *p = (int __user *)arg; | |
d88203d1 | 1494 | |
1da177e4 | 1495 | if (!capable(CAP_SYS_ADMIN)) |
d88203d1 | 1496 | return -EPERM; |
e9eee03e | 1497 | |
1da177e4 | 1498 | switch (cmd) { |
d88203d1 | 1499 | case MCE_GET_RECORD_LEN: |
1da177e4 LT |
1500 | return put_user(sizeof(struct mce), p); |
1501 | case MCE_GET_LOG_LEN: | |
d88203d1 | 1502 | return put_user(MCE_LOG_LEN, p); |
1da177e4 LT |
1503 | case MCE_GETCLEAR_FLAGS: { |
1504 | unsigned flags; | |
d88203d1 TG |
1505 | |
1506 | do { | |
1da177e4 | 1507 | flags = mcelog.flags; |
d88203d1 | 1508 | } while (cmpxchg(&mcelog.flags, flags, 0) != flags); |
e9eee03e | 1509 | |
d88203d1 | 1510 | return put_user(flags, p); |
1da177e4 LT |
1511 | } |
1512 | default: | |
d88203d1 TG |
1513 | return -ENOTTY; |
1514 | } | |
1da177e4 LT |
1515 | } |
1516 | ||
a1ff41bf | 1517 | /* Modified in mce-inject.c, so not static or const */ |
ea149b36 | 1518 | struct file_operations mce_chrdev_ops = { |
e9eee03e IM |
1519 | .open = mce_open, |
1520 | .release = mce_release, | |
1521 | .read = mce_read, | |
1522 | .poll = mce_poll, | |
1523 | .unlocked_ioctl = mce_ioctl, | |
1da177e4 | 1524 | }; |
ea149b36 | 1525 | EXPORT_SYMBOL_GPL(mce_chrdev_ops); |
1da177e4 LT |
1526 | |
1527 | static struct miscdevice mce_log_device = { | |
1528 | MISC_MCELOG_MINOR, | |
1529 | "mcelog", | |
1530 | &mce_chrdev_ops, | |
1531 | }; | |
1532 | ||
13503fa9 | 1533 | /* |
62fdac59 HS |
1534 | * mce=off Disables machine check |
1535 | * mce=no_cmci Disables CMCI | |
1536 | * mce=dont_log_ce Clears corrected events silently, no log created for CEs. | |
1537 | * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared. | |
3c079792 AK |
1538 | * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above) |
1539 | * monarchtimeout is how long to wait for other CPUs on machine | |
1540 | * check, or 0 to not wait | |
13503fa9 HS |
1541 | * mce=bootlog Log MCEs from before booting. Disabled by default on AMD. |
1542 | * mce=nobootlog Don't log MCEs from before booting. | |
1543 | */ | |
1da177e4 LT |
1544 | static int __init mcheck_enable(char *str) |
1545 | { | |
4efc0670 AK |
1546 | if (*str == 0) |
1547 | enable_p5_mce(); | |
1548 | if (*str == '=') | |
1549 | str++; | |
1da177e4 | 1550 | if (!strcmp(str, "off")) |
04b2b1a4 | 1551 | mce_disabled = 1; |
62fdac59 HS |
1552 | else if (!strcmp(str, "no_cmci")) |
1553 | mce_cmci_disabled = 1; | |
1554 | else if (!strcmp(str, "dont_log_ce")) | |
1555 | mce_dont_log_ce = 1; | |
1556 | else if (!strcmp(str, "ignore_ce")) | |
1557 | mce_ignore_ce = 1; | |
13503fa9 HS |
1558 | else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog")) |
1559 | mce_bootlog = (str[0] == 'b'); | |
3c079792 | 1560 | else if (isdigit(str[0])) { |
8c566ef5 | 1561 | get_option(&str, &tolerant); |
3c079792 AK |
1562 | if (*str == ',') { |
1563 | ++str; | |
1564 | get_option(&str, &monarch_timeout); | |
1565 | } | |
1566 | } else { | |
4efc0670 | 1567 | printk(KERN_INFO "mce argument %s ignored. Please use /sys\n", |
13503fa9 HS |
1568 | str); |
1569 | return 0; | |
1570 | } | |
9b41046c | 1571 | return 1; |
1da177e4 | 1572 | } |
4efc0670 | 1573 | __setup("mce", mcheck_enable); |
1da177e4 | 1574 | |
d88203d1 | 1575 | /* |
1da177e4 | 1576 | * Sysfs support |
d88203d1 | 1577 | */ |
1da177e4 | 1578 | |
973a2dd1 AK |
1579 | /* |
1580 | * Disable machine checks on suspend and shutdown. We can't really handle | |
1581 | * them later. | |
1582 | */ | |
1583 | static int mce_disable(void) | |
1584 | { | |
1585 | int i; | |
1586 | ||
06b7a7a5 AK |
1587 | for (i = 0; i < banks; i++) { |
1588 | if (!skip_bank_init(i)) | |
1589 | wrmsrl(MSR_IA32_MC0_CTL + i*4, 0); | |
1590 | } | |
973a2dd1 AK |
1591 | return 0; |
1592 | } | |
1593 | ||
1594 | static int mce_suspend(struct sys_device *dev, pm_message_t state) | |
1595 | { | |
1596 | return mce_disable(); | |
1597 | } | |
1598 | ||
1599 | static int mce_shutdown(struct sys_device *dev) | |
1600 | { | |
1601 | return mce_disable(); | |
1602 | } | |
1603 | ||
e9eee03e IM |
1604 | /* |
1605 | * On resume clear all MCE state. Don't want to see leftovers from the BIOS. | |
1606 | * Only one CPU is active at this time, the others get re-added later using | |
1607 | * CPU hotplug: | |
1608 | */ | |
1da177e4 LT |
1609 | static int mce_resume(struct sys_device *dev) |
1610 | { | |
8be91105 | 1611 | mce_init(); |
6ec68bff | 1612 | mce_cpu_features(¤t_cpu_data); |
e9eee03e | 1613 | |
1da177e4 LT |
1614 | return 0; |
1615 | } | |
1616 | ||
52d168e2 AK |
1617 | static void mce_cpu_restart(void *data) |
1618 | { | |
1619 | del_timer_sync(&__get_cpu_var(mce_timer)); | |
1620 | if (mce_available(¤t_cpu_data)) | |
8be91105 | 1621 | mce_init(); |
52d168e2 AK |
1622 | mce_init_timer(); |
1623 | } | |
1624 | ||
1da177e4 | 1625 | /* Reinit MCEs after user configuration changes */ |
d88203d1 TG |
1626 | static void mce_restart(void) |
1627 | { | |
52d168e2 | 1628 | on_each_cpu(mce_cpu_restart, NULL, 1); |
1da177e4 LT |
1629 | } |
1630 | ||
1631 | static struct sysdev_class mce_sysclass = { | |
e9eee03e IM |
1632 | .suspend = mce_suspend, |
1633 | .shutdown = mce_shutdown, | |
1634 | .resume = mce_resume, | |
1635 | .name = "machinecheck", | |
1da177e4 LT |
1636 | }; |
1637 | ||
cb491fca | 1638 | DEFINE_PER_CPU(struct sys_device, mce_dev); |
e9eee03e IM |
1639 | |
1640 | __cpuinitdata | |
1641 | void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu); | |
1da177e4 | 1642 | |
0d7482e3 AK |
1643 | static struct sysdev_attribute *bank_attrs; |
1644 | ||
1645 | static ssize_t show_bank(struct sys_device *s, struct sysdev_attribute *attr, | |
1646 | char *buf) | |
1647 | { | |
1648 | u64 b = bank[attr - bank_attrs]; | |
e9eee03e | 1649 | |
f6d1826d | 1650 | return sprintf(buf, "%llx\n", b); |
0d7482e3 AK |
1651 | } |
1652 | ||
1653 | static ssize_t set_bank(struct sys_device *s, struct sysdev_attribute *attr, | |
9319cec8 | 1654 | const char *buf, size_t size) |
0d7482e3 | 1655 | { |
9319cec8 | 1656 | u64 new; |
e9eee03e | 1657 | |
9319cec8 | 1658 | if (strict_strtoull(buf, 0, &new) < 0) |
0d7482e3 | 1659 | return -EINVAL; |
e9eee03e | 1660 | |
0d7482e3 AK |
1661 | bank[attr - bank_attrs] = new; |
1662 | mce_restart(); | |
e9eee03e | 1663 | |
9319cec8 | 1664 | return size; |
0d7482e3 | 1665 | } |
a98f0dd3 | 1666 | |
e9eee03e IM |
1667 | static ssize_t |
1668 | show_trigger(struct sys_device *s, struct sysdev_attribute *attr, char *buf) | |
a98f0dd3 AK |
1669 | { |
1670 | strcpy(buf, trigger); | |
1671 | strcat(buf, "\n"); | |
1672 | return strlen(trigger) + 1; | |
1673 | } | |
1674 | ||
4a0b2b4d | 1675 | static ssize_t set_trigger(struct sys_device *s, struct sysdev_attribute *attr, |
e9eee03e | 1676 | const char *buf, size_t siz) |
a98f0dd3 AK |
1677 | { |
1678 | char *p; | |
1679 | int len; | |
e9eee03e | 1680 | |
a98f0dd3 AK |
1681 | strncpy(trigger, buf, sizeof(trigger)); |
1682 | trigger[sizeof(trigger)-1] = 0; | |
1683 | len = strlen(trigger); | |
1684 | p = strchr(trigger, '\n'); | |
e9eee03e IM |
1685 | |
1686 | if (*p) | |
1687 | *p = 0; | |
1688 | ||
a98f0dd3 AK |
1689 | return len; |
1690 | } | |
1691 | ||
b56f642d AK |
1692 | static ssize_t store_int_with_restart(struct sys_device *s, |
1693 | struct sysdev_attribute *attr, | |
1694 | const char *buf, size_t size) | |
1695 | { | |
1696 | ssize_t ret = sysdev_store_int(s, attr, buf, size); | |
1697 | mce_restart(); | |
1698 | return ret; | |
1699 | } | |
1700 | ||
a98f0dd3 | 1701 | static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger); |
d95d62c0 | 1702 | static SYSDEV_INT_ATTR(tolerant, 0644, tolerant); |
3c079792 | 1703 | static SYSDEV_INT_ATTR(monarch_timeout, 0644, monarch_timeout); |
e9eee03e | 1704 | |
b56f642d AK |
1705 | static struct sysdev_ext_attribute attr_check_interval = { |
1706 | _SYSDEV_ATTR(check_interval, 0644, sysdev_show_int, | |
1707 | store_int_with_restart), | |
1708 | &check_interval | |
1709 | }; | |
e9eee03e | 1710 | |
cb491fca | 1711 | static struct sysdev_attribute *mce_attrs[] = { |
b56f642d | 1712 | &attr_tolerant.attr, &attr_check_interval.attr, &attr_trigger, |
3c079792 | 1713 | &attr_monarch_timeout.attr, |
a98f0dd3 AK |
1714 | NULL |
1715 | }; | |
1da177e4 | 1716 | |
cb491fca | 1717 | static cpumask_var_t mce_dev_initialized; |
bae19fe0 | 1718 | |
e9eee03e | 1719 | /* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */ |
91c6d400 | 1720 | static __cpuinit int mce_create_device(unsigned int cpu) |
1da177e4 LT |
1721 | { |
1722 | int err; | |
73ca5358 | 1723 | int i; |
92cb7612 | 1724 | |
90367556 | 1725 | if (!mce_available(&boot_cpu_data)) |
91c6d400 AK |
1726 | return -EIO; |
1727 | ||
cb491fca IM |
1728 | memset(&per_cpu(mce_dev, cpu).kobj, 0, sizeof(struct kobject)); |
1729 | per_cpu(mce_dev, cpu).id = cpu; | |
1730 | per_cpu(mce_dev, cpu).cls = &mce_sysclass; | |
91c6d400 | 1731 | |
cb491fca | 1732 | err = sysdev_register(&per_cpu(mce_dev, cpu)); |
d435d862 AM |
1733 | if (err) |
1734 | return err; | |
1735 | ||
cb491fca IM |
1736 | for (i = 0; mce_attrs[i]; i++) { |
1737 | err = sysdev_create_file(&per_cpu(mce_dev, cpu), mce_attrs[i]); | |
d435d862 AM |
1738 | if (err) |
1739 | goto error; | |
1740 | } | |
0d7482e3 | 1741 | for (i = 0; i < banks; i++) { |
cb491fca | 1742 | err = sysdev_create_file(&per_cpu(mce_dev, cpu), |
0d7482e3 AK |
1743 | &bank_attrs[i]); |
1744 | if (err) | |
1745 | goto error2; | |
1746 | } | |
cb491fca | 1747 | cpumask_set_cpu(cpu, mce_dev_initialized); |
91c6d400 | 1748 | |
d435d862 | 1749 | return 0; |
0d7482e3 | 1750 | error2: |
cb491fca IM |
1751 | while (--i >= 0) |
1752 | sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]); | |
d435d862 | 1753 | error: |
cb491fca IM |
1754 | while (--i >= 0) |
1755 | sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]); | |
1756 | ||
1757 | sysdev_unregister(&per_cpu(mce_dev, cpu)); | |
d435d862 | 1758 | |
91c6d400 AK |
1759 | return err; |
1760 | } | |
1761 | ||
2d9cd6c2 | 1762 | static __cpuinit void mce_remove_device(unsigned int cpu) |
91c6d400 | 1763 | { |
73ca5358 SL |
1764 | int i; |
1765 | ||
cb491fca | 1766 | if (!cpumask_test_cpu(cpu, mce_dev_initialized)) |
bae19fe0 AH |
1767 | return; |
1768 | ||
cb491fca IM |
1769 | for (i = 0; mce_attrs[i]; i++) |
1770 | sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]); | |
1771 | ||
0d7482e3 | 1772 | for (i = 0; i < banks; i++) |
cb491fca IM |
1773 | sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]); |
1774 | ||
1775 | sysdev_unregister(&per_cpu(mce_dev, cpu)); | |
1776 | cpumask_clear_cpu(cpu, mce_dev_initialized); | |
91c6d400 | 1777 | } |
91c6d400 | 1778 | |
d6b75584 | 1779 | /* Make sure there are no machine checks on offlined CPUs. */ |
ec5b3d32 | 1780 | static void mce_disable_cpu(void *h) |
d6b75584 | 1781 | { |
88ccbedd | 1782 | unsigned long action = *(unsigned long *)h; |
cb491fca | 1783 | int i; |
d6b75584 AK |
1784 | |
1785 | if (!mce_available(¤t_cpu_data)) | |
1786 | return; | |
88ccbedd AK |
1787 | if (!(action & CPU_TASKS_FROZEN)) |
1788 | cmci_clear(); | |
06b7a7a5 AK |
1789 | for (i = 0; i < banks; i++) { |
1790 | if (!skip_bank_init(i)) | |
1791 | wrmsrl(MSR_IA32_MC0_CTL + i*4, 0); | |
1792 | } | |
d6b75584 AK |
1793 | } |
1794 | ||
ec5b3d32 | 1795 | static void mce_reenable_cpu(void *h) |
d6b75584 | 1796 | { |
88ccbedd | 1797 | unsigned long action = *(unsigned long *)h; |
e9eee03e | 1798 | int i; |
d6b75584 AK |
1799 | |
1800 | if (!mce_available(¤t_cpu_data)) | |
1801 | return; | |
e9eee03e | 1802 | |
88ccbedd AK |
1803 | if (!(action & CPU_TASKS_FROZEN)) |
1804 | cmci_reenable(); | |
06b7a7a5 AK |
1805 | for (i = 0; i < banks; i++) { |
1806 | if (!skip_bank_init(i)) | |
1807 | wrmsrl(MSR_IA32_MC0_CTL + i*4, bank[i]); | |
1808 | } | |
d6b75584 AK |
1809 | } |
1810 | ||
91c6d400 | 1811 | /* Get notified when a cpu comes on/off. Be hotplug friendly. */ |
e9eee03e IM |
1812 | static int __cpuinit |
1813 | mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu) | |
91c6d400 AK |
1814 | { |
1815 | unsigned int cpu = (unsigned long)hcpu; | |
52d168e2 | 1816 | struct timer_list *t = &per_cpu(mce_timer, cpu); |
91c6d400 AK |
1817 | |
1818 | switch (action) { | |
bae19fe0 AH |
1819 | case CPU_ONLINE: |
1820 | case CPU_ONLINE_FROZEN: | |
1821 | mce_create_device(cpu); | |
8735728e RW |
1822 | if (threshold_cpu_callback) |
1823 | threshold_cpu_callback(action, cpu); | |
91c6d400 | 1824 | break; |
91c6d400 | 1825 | case CPU_DEAD: |
8bb78442 | 1826 | case CPU_DEAD_FROZEN: |
8735728e RW |
1827 | if (threshold_cpu_callback) |
1828 | threshold_cpu_callback(action, cpu); | |
91c6d400 AK |
1829 | mce_remove_device(cpu); |
1830 | break; | |
52d168e2 AK |
1831 | case CPU_DOWN_PREPARE: |
1832 | case CPU_DOWN_PREPARE_FROZEN: | |
1833 | del_timer_sync(t); | |
88ccbedd | 1834 | smp_call_function_single(cpu, mce_disable_cpu, &action, 1); |
52d168e2 AK |
1835 | break; |
1836 | case CPU_DOWN_FAILED: | |
1837 | case CPU_DOWN_FAILED_FROZEN: | |
6298c512 AK |
1838 | t->expires = round_jiffies(jiffies + |
1839 | __get_cpu_var(next_interval)); | |
52d168e2 | 1840 | add_timer_on(t, cpu); |
88ccbedd AK |
1841 | smp_call_function_single(cpu, mce_reenable_cpu, &action, 1); |
1842 | break; | |
1843 | case CPU_POST_DEAD: | |
1844 | /* intentionally ignoring frozen here */ | |
1845 | cmci_rediscover(cpu); | |
52d168e2 | 1846 | break; |
91c6d400 | 1847 | } |
bae19fe0 | 1848 | return NOTIFY_OK; |
91c6d400 AK |
1849 | } |
1850 | ||
1e35669d | 1851 | static struct notifier_block mce_cpu_notifier __cpuinitdata = { |
91c6d400 AK |
1852 | .notifier_call = mce_cpu_callback, |
1853 | }; | |
1854 | ||
0d7482e3 AK |
1855 | static __init int mce_init_banks(void) |
1856 | { | |
1857 | int i; | |
1858 | ||
1859 | bank_attrs = kzalloc(sizeof(struct sysdev_attribute) * banks, | |
1860 | GFP_KERNEL); | |
1861 | if (!bank_attrs) | |
1862 | return -ENOMEM; | |
1863 | ||
1864 | for (i = 0; i < banks; i++) { | |
1865 | struct sysdev_attribute *a = &bank_attrs[i]; | |
e9eee03e IM |
1866 | |
1867 | a->attr.name = kasprintf(GFP_KERNEL, "bank%d", i); | |
0d7482e3 AK |
1868 | if (!a->attr.name) |
1869 | goto nomem; | |
e9eee03e IM |
1870 | |
1871 | a->attr.mode = 0644; | |
1872 | a->show = show_bank; | |
1873 | a->store = set_bank; | |
0d7482e3 AK |
1874 | } |
1875 | return 0; | |
1876 | ||
1877 | nomem: | |
1878 | while (--i >= 0) | |
1879 | kfree(bank_attrs[i].attr.name); | |
1880 | kfree(bank_attrs); | |
1881 | bank_attrs = NULL; | |
e9eee03e | 1882 | |
0d7482e3 AK |
1883 | return -ENOMEM; |
1884 | } | |
1885 | ||
91c6d400 AK |
1886 | static __init int mce_init_device(void) |
1887 | { | |
1888 | int err; | |
1889 | int i = 0; | |
1890 | ||
1da177e4 LT |
1891 | if (!mce_available(&boot_cpu_data)) |
1892 | return -EIO; | |
0d7482e3 | 1893 | |
cb491fca | 1894 | alloc_cpumask_var(&mce_dev_initialized, GFP_KERNEL); |
996867d0 | 1895 | |
0d7482e3 AK |
1896 | err = mce_init_banks(); |
1897 | if (err) | |
1898 | return err; | |
1899 | ||
1da177e4 | 1900 | err = sysdev_class_register(&mce_sysclass); |
d435d862 AM |
1901 | if (err) |
1902 | return err; | |
91c6d400 AK |
1903 | |
1904 | for_each_online_cpu(i) { | |
d435d862 AM |
1905 | err = mce_create_device(i); |
1906 | if (err) | |
1907 | return err; | |
91c6d400 AK |
1908 | } |
1909 | ||
be6b5a35 | 1910 | register_hotcpu_notifier(&mce_cpu_notifier); |
1da177e4 | 1911 | misc_register(&mce_log_device); |
e9eee03e | 1912 | |
1da177e4 | 1913 | return err; |
1da177e4 | 1914 | } |
91c6d400 | 1915 | |
1da177e4 | 1916 | device_initcall(mce_init_device); |
a988d334 | 1917 | |
4efc0670 | 1918 | #else /* CONFIG_X86_OLD_MCE: */ |
a988d334 | 1919 | |
a988d334 IM |
1920 | int nr_mce_banks; |
1921 | EXPORT_SYMBOL_GPL(nr_mce_banks); /* non-fatal.o */ | |
1922 | ||
a988d334 IM |
1923 | /* This has to be run for each processor */ |
1924 | void mcheck_init(struct cpuinfo_x86 *c) | |
1925 | { | |
1926 | if (mce_disabled == 1) | |
1927 | return; | |
1928 | ||
1929 | switch (c->x86_vendor) { | |
1930 | case X86_VENDOR_AMD: | |
1931 | amd_mcheck_init(c); | |
1932 | break; | |
1933 | ||
1934 | case X86_VENDOR_INTEL: | |
1935 | if (c->x86 == 5) | |
1936 | intel_p5_mcheck_init(c); | |
1937 | if (c->x86 == 6) | |
1938 | intel_p6_mcheck_init(c); | |
1939 | if (c->x86 == 15) | |
1940 | intel_p4_mcheck_init(c); | |
1941 | break; | |
1942 | ||
1943 | case X86_VENDOR_CENTAUR: | |
1944 | if (c->x86 == 5) | |
1945 | winchip_mcheck_init(c); | |
1946 | break; | |
1947 | ||
1948 | default: | |
1949 | break; | |
1950 | } | |
b659294b | 1951 | printk(KERN_INFO "mce: CPU supports %d MCE banks\n", nr_mce_banks); |
a988d334 IM |
1952 | } |
1953 | ||
a988d334 IM |
1954 | static int __init mcheck_enable(char *str) |
1955 | { | |
1956 | mce_disabled = -1; | |
1957 | return 1; | |
1958 | } | |
1959 | ||
a988d334 IM |
1960 | __setup("mce", mcheck_enable); |
1961 | ||
d7c3c9a6 AK |
1962 | #endif /* CONFIG_X86_OLD_MCE */ |
1963 | ||
1964 | /* | |
1965 | * Old style boot options parsing. Only for compatibility. | |
1966 | */ | |
1967 | static int __init mcheck_disable(char *str) | |
1968 | { | |
1969 | mce_disabled = 1; | |
1970 | return 1; | |
1971 | } | |
1972 | __setup("nomce", mcheck_disable); |