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