ACPI, APEI, GHES: Cleanup ghes memory error handling
[deliverable/linux.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 */
c767a54b
JP
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
e9eee03e
IM
13#include <linux/thread_info.h>
14#include <linux/capability.h>
15#include <linux/miscdevice.h>
16#include <linux/ratelimit.h>
17#include <linux/kallsyms.h>
18#include <linux/rcupdate.h>
e9eee03e 19#include <linux/kobject.h>
14a02530 20#include <linux/uaccess.h>
e9eee03e
IM
21#include <linux/kdebug.h>
22#include <linux/kernel.h>
23#include <linux/percpu.h>
1da177e4 24#include <linux/string.h>
8a25a2fd 25#include <linux/device.h>
f3c6ea1b 26#include <linux/syscore_ops.h>
3c079792 27#include <linux/delay.h>
8c566ef5 28#include <linux/ctype.h>
e9eee03e 29#include <linux/sched.h>
0d7482e3 30#include <linux/sysfs.h>
e9eee03e 31#include <linux/types.h>
5a0e3ad6 32#include <linux/slab.h>
e9eee03e
IM
33#include <linux/init.h>
34#include <linux/kmod.h>
35#include <linux/poll.h>
3c079792 36#include <linux/nmi.h>
e9eee03e 37#include <linux/cpu.h>
14a02530 38#include <linux/smp.h>
e9eee03e 39#include <linux/fs.h>
9b1beaf2 40#include <linux/mm.h>
5be9ed25 41#include <linux/debugfs.h>
b77e70bf 42#include <linux/irq_work.h>
69c60c88 43#include <linux/export.h>
e9eee03e 44
d88203d1 45#include <asm/processor.h>
e9eee03e
IM
46#include <asm/mce.h>
47#include <asm/msr.h>
1da177e4 48
bd19a5e6 49#include "mce-internal.h"
711c2e48 50
93b62c3c 51static DEFINE_MUTEX(mce_chrdev_read_mutex);
2aa2b50d 52
f56e8a07 53#define rcu_dereference_check_mce(p) \
ec8c27e0 54 rcu_dereference_index_check((p), \
f56e8a07 55 rcu_read_lock_sched_held() || \
93b62c3c 56 lockdep_is_held(&mce_chrdev_read_mutex))
f56e8a07 57
8968f9d3
HS
58#define CREATE_TRACE_POINTS
59#include <trace/events/mce.h>
60
3c079792
AK
61#define SPINUNIT 100 /* 100ns */
62
553f265f
AK
63atomic_t mce_entry;
64
01ca79f1
AK
65DEFINE_PER_CPU(unsigned, mce_exception_count);
66
1462594b 67struct mce_bank *mce_banks __read_mostly;
cebe1820 68
d203f0b8 69struct mca_config mca_cfg __read_mostly = {
84c2559d 70 .bootlog = -1,
d203f0b8
BP
71 /*
72 * Tolerant levels:
73 * 0: always panic on uncorrected errors, log corrected errors
74 * 1: panic or SIGBUS on uncorrected errors, log corrected errors
75 * 2: SIGBUS or log uncorrected errors (if possible), log corr. errors
76 * 3: never panic or SIGBUS, log all errors (for testing only)
77 */
84c2559d
BP
78 .tolerant = 1,
79 .monarch_timeout = -1
d203f0b8
BP
80};
81
1020bcbc
HS
82/* User mode helper program triggered by machine check event */
83static unsigned long mce_need_notify;
84static char mce_helper[128];
85static char *mce_helper_argv[2] = { mce_helper, NULL };
1da177e4 86
93b62c3c
HS
87static DECLARE_WAIT_QUEUE_HEAD(mce_chrdev_wait);
88
3c079792
AK
89static DEFINE_PER_CPU(struct mce, mces_seen);
90static int cpu_missing;
91
0644414e
NR
92/*
93 * MCA banks polled by the period polling timer for corrected events.
94 * With Intel CMCI, this only has MCA banks which do not support CMCI (if any).
95 */
ee031c31
AK
96DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
97 [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
98};
99
c3d1fb56
NR
100/*
101 * MCA banks controlled through firmware first for corrected errors.
102 * This is a global list of banks for which we won't enable CMCI and we
103 * won't poll. Firmware controls these banks and is responsible for
104 * reporting corrected errors through GHES. Uncorrected/recoverable
105 * errors are still notified through a machine check.
106 */
107mce_banks_t mce_banks_ce_disabled;
108
9b1beaf2
AK
109static DEFINE_PER_CPU(struct work_struct, mce_work);
110
61b0fccd
TL
111static void (*quirk_no_way_out)(int bank, struct mce *m, struct pt_regs *regs);
112
3653ada5
BP
113/*
114 * CPU/chipset specific EDAC code can register a notifier call here to print
115 * MCE errors in a human-readable form.
116 */
117ATOMIC_NOTIFIER_HEAD(x86_mce_decoder_chain);
118
b5f2fa4e
AK
119/* Do initial initialization of a struct mce */
120void mce_setup(struct mce *m)
121{
122 memset(m, 0, sizeof(struct mce));
d620c67f 123 m->cpu = m->extcpu = smp_processor_id();
b5f2fa4e 124 rdtscll(m->tsc);
8ee08347
AK
125 /* We hope get_seconds stays lockless */
126 m->time = get_seconds();
127 m->cpuvendor = boot_cpu_data.x86_vendor;
128 m->cpuid = cpuid_eax(1);
8ee08347 129 m->socketid = cpu_data(m->extcpu).phys_proc_id;
8ee08347
AK
130 m->apicid = cpu_data(m->extcpu).initial_apicid;
131 rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
b5f2fa4e
AK
132}
133
ea149b36
AK
134DEFINE_PER_CPU(struct mce, injectm);
135EXPORT_PER_CPU_SYMBOL_GPL(injectm);
136
1da177e4
LT
137/*
138 * Lockless MCE logging infrastructure.
139 * This avoids deadlocks on printk locks without having to break locks. Also
140 * separate MCEs from kernel messages to avoid bogus bug reports.
141 */
142
231fd906 143static struct mce_log mcelog = {
f6fb0ac0
AK
144 .signature = MCE_LOG_SIGNATURE,
145 .len = MCE_LOG_LEN,
146 .recordlen = sizeof(struct mce),
d88203d1 147};
1da177e4
LT
148
149void mce_log(struct mce *mce)
150{
151 unsigned next, entry;
f0cb5452 152 int ret = 0;
e9eee03e 153
8968f9d3
HS
154 /* Emit the trace record: */
155 trace_mce_record(mce);
156
f0cb5452
BP
157 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, mce);
158 if (ret == NOTIFY_STOP)
159 return;
160
1da177e4 161 mce->finished = 0;
7644143c 162 wmb();
1da177e4 163 for (;;) {
f56e8a07 164 entry = rcu_dereference_check_mce(mcelog.next);
673242c1 165 for (;;) {
696e409d 166
e9eee03e
IM
167 /*
168 * When the buffer fills up discard new entries.
169 * Assume that the earlier errors are the more
170 * interesting ones:
171 */
673242c1 172 if (entry >= MCE_LOG_LEN) {
14a02530
HS
173 set_bit(MCE_OVERFLOW,
174 (unsigned long *)&mcelog.flags);
673242c1
AK
175 return;
176 }
e9eee03e 177 /* Old left over entry. Skip: */
673242c1
AK
178 if (mcelog.entry[entry].finished) {
179 entry++;
180 continue;
181 }
7644143c 182 break;
1da177e4 183 }
1da177e4
LT
184 smp_rmb();
185 next = entry + 1;
186 if (cmpxchg(&mcelog.next, entry, next) == entry)
187 break;
188 }
189 memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
7644143c 190 wmb();
1da177e4 191 mcelog.entry[entry].finished = 1;
7644143c 192 wmb();
1da177e4 193
a0189c70 194 mce->finished = 1;
1020bcbc 195 set_bit(0, &mce_need_notify);
1da177e4
LT
196}
197
09371957
BP
198static void drain_mcelog_buffer(void)
199{
200 unsigned int next, i, prev = 0;
201
b11e3d78 202 next = ACCESS_ONCE(mcelog.next);
09371957
BP
203
204 do {
205 struct mce *m;
206
207 /* drain what was logged during boot */
208 for (i = prev; i < next; i++) {
209 unsigned long start = jiffies;
210 unsigned retries = 1;
211
212 m = &mcelog.entry[i];
213
214 while (!m->finished) {
215 if (time_after_eq(jiffies, start + 2*retries))
216 retries++;
217
218 cpu_relax();
219
220 if (!m->finished && retries >= 4) {
c767a54b 221 pr_err("skipping error being logged currently!\n");
09371957
BP
222 break;
223 }
224 }
225 smp_rmb();
226 atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
227 }
228
229 memset(mcelog.entry + prev, 0, (next - prev) * sizeof(*m));
230 prev = next;
231 next = cmpxchg(&mcelog.next, prev, 0);
232 } while (next != prev);
233}
234
235
3653ada5
BP
236void mce_register_decode_chain(struct notifier_block *nb)
237{
238 atomic_notifier_chain_register(&x86_mce_decoder_chain, nb);
09371957 239 drain_mcelog_buffer();
3653ada5
BP
240}
241EXPORT_SYMBOL_GPL(mce_register_decode_chain);
242
243void mce_unregister_decode_chain(struct notifier_block *nb)
244{
245 atomic_notifier_chain_unregister(&x86_mce_decoder_chain, nb);
246}
247EXPORT_SYMBOL_GPL(mce_unregister_decode_chain);
248
77e26cca 249static void print_mce(struct mce *m)
1da177e4 250{
dffa4b2f
BP
251 int ret = 0;
252
a2d7b0d4 253 pr_emerg(HW_ERR "CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
d620c67f 254 m->extcpu, m->mcgstatus, m->bank, m->status);
f436f8bb 255
65ea5b03 256 if (m->ip) {
a2d7b0d4 257 pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
f436f8bb
IM
258 !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
259 m->cs, m->ip);
260
1da177e4 261 if (m->cs == __KERNEL_CS)
65ea5b03 262 print_symbol("{%s}", m->ip);
f436f8bb 263 pr_cont("\n");
1da177e4 264 }
f436f8bb 265
a2d7b0d4 266 pr_emerg(HW_ERR "TSC %llx ", m->tsc);
1da177e4 267 if (m->addr)
f436f8bb 268 pr_cont("ADDR %llx ", m->addr);
1da177e4 269 if (m->misc)
f436f8bb 270 pr_cont("MISC %llx ", m->misc);
549d042d 271
f436f8bb 272 pr_cont("\n");
506ed6b5
AK
273 /*
274 * Note this output is parsed by external tools and old fields
275 * should not be changed.
276 */
881e23e5 277 pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x microcode %x\n",
506ed6b5
AK
278 m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid,
279 cpu_data(m->extcpu).microcode);
f436f8bb
IM
280
281 /*
282 * Print out human-readable details about the MCE error,
fb253195 283 * (if the CPU has an implementation for that)
f436f8bb 284 */
dffa4b2f
BP
285 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
286 if (ret == NOTIFY_STOP)
287 return;
288
289 pr_emerg_ratelimited(HW_ERR "Run the above through 'mcelog --ascii'\n");
86503560
AK
290}
291
f94b61c2
AK
292#define PANIC_TIMEOUT 5 /* 5 seconds */
293
294static atomic_t mce_paniced;
295
bf783f9f
HY
296static int fake_panic;
297static atomic_t mce_fake_paniced;
298
f94b61c2
AK
299/* Panic in progress. Enable interrupts and wait for final IPI */
300static void wait_for_panic(void)
301{
302 long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
f436f8bb 303
f94b61c2
AK
304 preempt_disable();
305 local_irq_enable();
306 while (timeout-- > 0)
307 udelay(1);
29b0f591 308 if (panic_timeout == 0)
7af19e4a 309 panic_timeout = mca_cfg.panic_timeout;
f94b61c2
AK
310 panic("Panicing machine check CPU died");
311}
312
bd19a5e6 313static void mce_panic(char *msg, struct mce *final, char *exp)
d88203d1 314{
482908b4 315 int i, apei_err = 0;
e02e68d3 316
bf783f9f
HY
317 if (!fake_panic) {
318 /*
319 * Make sure only one CPU runs in machine check panic
320 */
321 if (atomic_inc_return(&mce_paniced) > 1)
322 wait_for_panic();
323 barrier();
f94b61c2 324
bf783f9f
HY
325 bust_spinlocks(1);
326 console_verbose();
327 } else {
328 /* Don't log too much for fake panic */
329 if (atomic_inc_return(&mce_fake_paniced) > 1)
330 return;
331 }
a0189c70 332 /* First print corrected ones that are still unlogged */
1da177e4 333 for (i = 0; i < MCE_LOG_LEN; i++) {
a0189c70 334 struct mce *m = &mcelog.entry[i];
77e26cca
HS
335 if (!(m->status & MCI_STATUS_VAL))
336 continue;
482908b4 337 if (!(m->status & MCI_STATUS_UC)) {
77e26cca 338 print_mce(m);
482908b4
HY
339 if (!apei_err)
340 apei_err = apei_write_mce(m);
341 }
a0189c70
AK
342 }
343 /* Now print uncorrected but with the final one last */
344 for (i = 0; i < MCE_LOG_LEN; i++) {
345 struct mce *m = &mcelog.entry[i];
346 if (!(m->status & MCI_STATUS_VAL))
1da177e4 347 continue;
77e26cca
HS
348 if (!(m->status & MCI_STATUS_UC))
349 continue;
482908b4 350 if (!final || memcmp(m, final, sizeof(struct mce))) {
77e26cca 351 print_mce(m);
482908b4
HY
352 if (!apei_err)
353 apei_err = apei_write_mce(m);
354 }
1da177e4 355 }
482908b4 356 if (final) {
77e26cca 357 print_mce(final);
482908b4
HY
358 if (!apei_err)
359 apei_err = apei_write_mce(final);
360 }
3c079792 361 if (cpu_missing)
a2d7b0d4 362 pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
bd19a5e6 363 if (exp)
a2d7b0d4 364 pr_emerg(HW_ERR "Machine check: %s\n", exp);
bf783f9f
HY
365 if (!fake_panic) {
366 if (panic_timeout == 0)
7af19e4a 367 panic_timeout = mca_cfg.panic_timeout;
bf783f9f
HY
368 panic(msg);
369 } else
a2d7b0d4 370 pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
d88203d1 371}
1da177e4 372
ea149b36
AK
373/* Support code for software error injection */
374
375static int msr_to_offset(u32 msr)
376{
0a3aee0d 377 unsigned bank = __this_cpu_read(injectm.bank);
f436f8bb 378
84c2559d 379 if (msr == mca_cfg.rip_msr)
ea149b36 380 return offsetof(struct mce, ip);
a2d32bcb 381 if (msr == MSR_IA32_MCx_STATUS(bank))
ea149b36 382 return offsetof(struct mce, status);
a2d32bcb 383 if (msr == MSR_IA32_MCx_ADDR(bank))
ea149b36 384 return offsetof(struct mce, addr);
a2d32bcb 385 if (msr == MSR_IA32_MCx_MISC(bank))
ea149b36
AK
386 return offsetof(struct mce, misc);
387 if (msr == MSR_IA32_MCG_STATUS)
388 return offsetof(struct mce, mcgstatus);
389 return -1;
390}
391
5f8c1a54
AK
392/* MSR access wrappers used for error injection */
393static u64 mce_rdmsrl(u32 msr)
394{
395 u64 v;
11868a2d 396
0a3aee0d 397 if (__this_cpu_read(injectm.finished)) {
ea149b36 398 int offset = msr_to_offset(msr);
11868a2d 399
ea149b36
AK
400 if (offset < 0)
401 return 0;
402 return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
403 }
11868a2d
IM
404
405 if (rdmsrl_safe(msr, &v)) {
406 WARN_ONCE(1, "mce: Unable to read msr %d!\n", msr);
407 /*
408 * Return zero in case the access faulted. This should
409 * not happen normally but can happen if the CPU does
410 * something weird, or if the code is buggy.
411 */
412 v = 0;
413 }
414
5f8c1a54
AK
415 return v;
416}
417
418static void mce_wrmsrl(u32 msr, u64 v)
419{
0a3aee0d 420 if (__this_cpu_read(injectm.finished)) {
ea149b36 421 int offset = msr_to_offset(msr);
11868a2d 422
ea149b36
AK
423 if (offset >= 0)
424 *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
425 return;
426 }
5f8c1a54
AK
427 wrmsrl(msr, v);
428}
429
b8325c5b
HS
430/*
431 * Collect all global (w.r.t. this processor) status about this machine
432 * check into our "mce" struct so that we can use it later to assess
433 * the severity of the problem as we read per-bank specific details.
434 */
435static inline void mce_gather_info(struct mce *m, struct pt_regs *regs)
436{
437 mce_setup(m);
438
439 m->mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
440 if (regs) {
441 /*
442 * Get the address of the instruction at the time of
443 * the machine check error.
444 */
445 if (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) {
446 m->ip = regs->ip;
447 m->cs = regs->cs;
a129a7c8
AK
448
449 /*
450 * When in VM86 mode make the cs look like ring 3
451 * always. This is a lie, but it's better than passing
452 * the additional vm86 bit around everywhere.
453 */
454 if (v8086_mode(regs))
455 m->cs |= 3;
b8325c5b
HS
456 }
457 /* Use accurate RIP reporting if available. */
84c2559d
BP
458 if (mca_cfg.rip_msr)
459 m->ip = mce_rdmsrl(mca_cfg.rip_msr);
b8325c5b
HS
460 }
461}
462
9b1beaf2
AK
463/*
464 * Simple lockless ring to communicate PFNs from the exception handler with the
465 * process context work function. This is vastly simplified because there's
466 * only a single reader and a single writer.
467 */
468#define MCE_RING_SIZE 16 /* we use one entry less */
469
470struct mce_ring {
471 unsigned short start;
472 unsigned short end;
473 unsigned long ring[MCE_RING_SIZE];
474};
475static DEFINE_PER_CPU(struct mce_ring, mce_ring);
476
477/* Runs with CPU affinity in workqueue */
478static int mce_ring_empty(void)
479{
480 struct mce_ring *r = &__get_cpu_var(mce_ring);
481
482 return r->start == r->end;
483}
484
485static int mce_ring_get(unsigned long *pfn)
486{
487 struct mce_ring *r;
488 int ret = 0;
489
490 *pfn = 0;
491 get_cpu();
492 r = &__get_cpu_var(mce_ring);
493 if (r->start == r->end)
494 goto out;
495 *pfn = r->ring[r->start];
496 r->start = (r->start + 1) % MCE_RING_SIZE;
497 ret = 1;
498out:
499 put_cpu();
500 return ret;
501}
502
503/* Always runs in MCE context with preempt off */
504static int mce_ring_add(unsigned long pfn)
505{
506 struct mce_ring *r = &__get_cpu_var(mce_ring);
507 unsigned next;
508
509 next = (r->end + 1) % MCE_RING_SIZE;
510 if (next == r->start)
511 return -1;
512 r->ring[r->end] = pfn;
513 wmb();
514 r->end = next;
515 return 0;
516}
517
88ccbedd 518int mce_available(struct cpuinfo_x86 *c)
1da177e4 519{
1462594b 520 if (mca_cfg.disabled)
5b4408fd 521 return 0;
3d1712c9 522 return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
1da177e4
LT
523}
524
9b1beaf2
AK
525static void mce_schedule_work(void)
526{
4d899be5
TH
527 if (!mce_ring_empty())
528 schedule_work(&__get_cpu_var(mce_work));
9b1beaf2
AK
529}
530
b77e70bf
HS
531DEFINE_PER_CPU(struct irq_work, mce_irq_work);
532
533static void mce_irq_work_cb(struct irq_work *entry)
ccc3c319 534{
9ff36ee9 535 mce_notify_irq();
9b1beaf2 536 mce_schedule_work();
ccc3c319 537}
ccc3c319
AK
538
539static void mce_report_event(struct pt_regs *regs)
540{
541 if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
9ff36ee9 542 mce_notify_irq();
9b1beaf2
AK
543 /*
544 * Triggering the work queue here is just an insurance
545 * policy in case the syscall exit notify handler
546 * doesn't run soon enough or ends up running on the
547 * wrong CPU (can happen when audit sleeps)
548 */
549 mce_schedule_work();
ccc3c319
AK
550 return;
551 }
552
b77e70bf 553 irq_work_queue(&__get_cpu_var(mce_irq_work));
ccc3c319
AK
554}
555
85f92694
TL
556/*
557 * Read ADDR and MISC registers.
558 */
559static void mce_read_aux(struct mce *m, int i)
560{
561 if (m->status & MCI_STATUS_MISCV)
562 m->misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
563 if (m->status & MCI_STATUS_ADDRV) {
564 m->addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
565
566 /*
567 * Mask the reported address by the reported granularity.
568 */
1462594b 569 if (mca_cfg.ser && (m->status & MCI_STATUS_MISCV)) {
85f92694
TL
570 u8 shift = MCI_MISC_ADDR_LSB(m->misc);
571 m->addr >>= shift;
572 m->addr <<= shift;
573 }
574 }
575}
576
ca84f696
AK
577DEFINE_PER_CPU(unsigned, mce_poll_count);
578
d88203d1 579/*
b79109c3
AK
580 * Poll for corrected events or events that happened before reset.
581 * Those are just logged through /dev/mcelog.
582 *
583 * This is executed in standard interrupt context.
ed7290d0
AK
584 *
585 * Note: spec recommends to panic for fatal unsignalled
586 * errors here. However this would be quite problematic --
587 * we would need to reimplement the Monarch handling and
588 * it would mess up the exclusion between exception handler
589 * and poll hander -- * so we skip this for now.
590 * These cases should not happen anyways, or only when the CPU
591 * is already totally * confused. In this case it's likely it will
592 * not fully execute the machine check handler either.
b79109c3 593 */
ee031c31 594void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
b79109c3
AK
595{
596 struct mce m;
597 int i;
598
c6ae41e7 599 this_cpu_inc(mce_poll_count);
ca84f696 600
b8325c5b 601 mce_gather_info(&m, NULL);
b79109c3 602
d203f0b8 603 for (i = 0; i < mca_cfg.banks; i++) {
cebe1820 604 if (!mce_banks[i].ctl || !test_bit(i, *b))
b79109c3
AK
605 continue;
606
607 m.misc = 0;
608 m.addr = 0;
609 m.bank = i;
610 m.tsc = 0;
611
612 barrier();
a2d32bcb 613 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
b79109c3
AK
614 if (!(m.status & MCI_STATUS_VAL))
615 continue;
616
617 /*
ed7290d0
AK
618 * Uncorrected or signalled events are handled by the exception
619 * handler when it is enabled, so don't process those here.
b79109c3
AK
620 *
621 * TBD do the same check for MCI_STATUS_EN here?
622 */
ed7290d0 623 if (!(flags & MCP_UC) &&
1462594b 624 (m.status & (mca_cfg.ser ? MCI_STATUS_S : MCI_STATUS_UC)))
b79109c3
AK
625 continue;
626
85f92694 627 mce_read_aux(&m, i);
b79109c3
AK
628
629 if (!(flags & MCP_TIMESTAMP))
630 m.tsc = 0;
631 /*
632 * Don't get the IP here because it's unlikely to
633 * have anything to do with the actual error location.
634 */
d203f0b8 635 if (!(flags & MCP_DONTLOG) && !mca_cfg.dont_log_ce)
5679af4c 636 mce_log(&m);
b79109c3
AK
637
638 /*
639 * Clear state for this bank.
640 */
a2d32bcb 641 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
b79109c3
AK
642 }
643
644 /*
645 * Don't clear MCG_STATUS here because it's only defined for
646 * exceptions.
647 */
88921be3
AK
648
649 sync_core();
b79109c3 650}
ea149b36 651EXPORT_SYMBOL_GPL(machine_check_poll);
b79109c3 652
bd19a5e6
AK
653/*
654 * Do a quick check if any of the events requires a panic.
655 * This decides if we keep the events around or clear them.
656 */
61b0fccd
TL
657static int mce_no_way_out(struct mce *m, char **msg, unsigned long *validp,
658 struct pt_regs *regs)
bd19a5e6 659{
95022b8c 660 int i, ret = 0;
bd19a5e6 661
d203f0b8 662 for (i = 0; i < mca_cfg.banks; i++) {
a2d32bcb 663 m->status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
61b0fccd 664 if (m->status & MCI_STATUS_VAL) {
95022b8c 665 __set_bit(i, validp);
61b0fccd
TL
666 if (quirk_no_way_out)
667 quirk_no_way_out(i, m, regs);
668 }
d203f0b8 669 if (mce_severity(m, mca_cfg.tolerant, msg) >= MCE_PANIC_SEVERITY)
95022b8c 670 ret = 1;
bd19a5e6 671 }
95022b8c 672 return ret;
bd19a5e6
AK
673}
674
3c079792
AK
675/*
676 * Variable to establish order between CPUs while scanning.
677 * Each CPU spins initially until executing is equal its number.
678 */
679static atomic_t mce_executing;
680
681/*
682 * Defines order of CPUs on entry. First CPU becomes Monarch.
683 */
684static atomic_t mce_callin;
685
686/*
687 * Check if a timeout waiting for other CPUs happened.
688 */
689static int mce_timed_out(u64 *t)
690{
691 /*
692 * The others already did panic for some reason.
693 * Bail out like in a timeout.
694 * rmb() to tell the compiler that system_state
695 * might have been modified by someone else.
696 */
697 rmb();
698 if (atomic_read(&mce_paniced))
699 wait_for_panic();
84c2559d 700 if (!mca_cfg.monarch_timeout)
3c079792
AK
701 goto out;
702 if ((s64)*t < SPINUNIT) {
703 /* CHECKME: Make panic default for 1 too? */
d203f0b8 704 if (mca_cfg.tolerant < 1)
3c079792
AK
705 mce_panic("Timeout synchronizing machine check over CPUs",
706 NULL, NULL);
707 cpu_missing = 1;
708 return 1;
709 }
710 *t -= SPINUNIT;
711out:
712 touch_nmi_watchdog();
713 return 0;
714}
715
716/*
717 * The Monarch's reign. The Monarch is the CPU who entered
718 * the machine check handler first. It waits for the others to
719 * raise the exception too and then grades them. When any
720 * error is fatal panic. Only then let the others continue.
721 *
722 * The other CPUs entering the MCE handler will be controlled by the
723 * Monarch. They are called Subjects.
724 *
725 * This way we prevent any potential data corruption in a unrecoverable case
726 * and also makes sure always all CPU's errors are examined.
727 *
680b6cfd 728 * Also this detects the case of a machine check event coming from outer
3c079792
AK
729 * space (not detected by any CPUs) In this case some external agent wants
730 * us to shut down, so panic too.
731 *
732 * The other CPUs might still decide to panic if the handler happens
733 * in a unrecoverable place, but in this case the system is in a semi-stable
734 * state and won't corrupt anything by itself. It's ok to let the others
735 * continue for a bit first.
736 *
737 * All the spin loops have timeouts; when a timeout happens a CPU
738 * typically elects itself to be Monarch.
739 */
740static void mce_reign(void)
741{
742 int cpu;
743 struct mce *m = NULL;
744 int global_worst = 0;
745 char *msg = NULL;
746 char *nmsg = NULL;
747
748 /*
749 * This CPU is the Monarch and the other CPUs have run
750 * through their handlers.
751 * Grade the severity of the errors of all the CPUs.
752 */
753 for_each_possible_cpu(cpu) {
d203f0b8
BP
754 int severity = mce_severity(&per_cpu(mces_seen, cpu),
755 mca_cfg.tolerant,
3c079792
AK
756 &nmsg);
757 if (severity > global_worst) {
758 msg = nmsg;
759 global_worst = severity;
760 m = &per_cpu(mces_seen, cpu);
761 }
762 }
763
764 /*
765 * Cannot recover? Panic here then.
766 * This dumps all the mces in the log buffer and stops the
767 * other CPUs.
768 */
d203f0b8 769 if (m && global_worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3)
ac960375 770 mce_panic("Fatal Machine check", m, msg);
3c079792
AK
771
772 /*
773 * For UC somewhere we let the CPU who detects it handle it.
774 * Also must let continue the others, otherwise the handling
775 * CPU could deadlock on a lock.
776 */
777
778 /*
779 * No machine check event found. Must be some external
780 * source or one CPU is hung. Panic.
781 */
d203f0b8 782 if (global_worst <= MCE_KEEP_SEVERITY && mca_cfg.tolerant < 3)
3c079792
AK
783 mce_panic("Machine check from unknown source", NULL, NULL);
784
785 /*
786 * Now clear all the mces_seen so that they don't reappear on
787 * the next mce.
788 */
789 for_each_possible_cpu(cpu)
790 memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
791}
792
793static atomic_t global_nwo;
794
795/*
796 * Start of Monarch synchronization. This waits until all CPUs have
797 * entered the exception handler and then determines if any of them
798 * saw a fatal event that requires panic. Then it executes them
799 * in the entry order.
800 * TBD double check parallel CPU hotunplug
801 */
7fb06fc9 802static int mce_start(int *no_way_out)
3c079792 803{
7fb06fc9 804 int order;
3c079792 805 int cpus = num_online_cpus();
84c2559d 806 u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
3c079792 807
7fb06fc9
HS
808 if (!timeout)
809 return -1;
3c079792 810
7fb06fc9 811 atomic_add(*no_way_out, &global_nwo);
184e1fdf
HY
812 /*
813 * global_nwo should be updated before mce_callin
814 */
815 smp_wmb();
a95436e4 816 order = atomic_inc_return(&mce_callin);
3c079792
AK
817
818 /*
819 * Wait for everyone.
820 */
821 while (atomic_read(&mce_callin) != cpus) {
822 if (mce_timed_out(&timeout)) {
823 atomic_set(&global_nwo, 0);
7fb06fc9 824 return -1;
3c079792
AK
825 }
826 ndelay(SPINUNIT);
827 }
828
184e1fdf
HY
829 /*
830 * mce_callin should be read before global_nwo
831 */
832 smp_rmb();
3c079792 833
7fb06fc9
HS
834 if (order == 1) {
835 /*
836 * Monarch: Starts executing now, the others wait.
837 */
3c079792 838 atomic_set(&mce_executing, 1);
7fb06fc9
HS
839 } else {
840 /*
841 * Subject: Now start the scanning loop one by one in
842 * the original callin order.
843 * This way when there are any shared banks it will be
844 * only seen by one CPU before cleared, avoiding duplicates.
845 */
846 while (atomic_read(&mce_executing) < order) {
847 if (mce_timed_out(&timeout)) {
848 atomic_set(&global_nwo, 0);
849 return -1;
850 }
851 ndelay(SPINUNIT);
852 }
3c079792
AK
853 }
854
855 /*
7fb06fc9 856 * Cache the global no_way_out state.
3c079792 857 */
7fb06fc9
HS
858 *no_way_out = atomic_read(&global_nwo);
859
860 return order;
3c079792
AK
861}
862
863/*
864 * Synchronize between CPUs after main scanning loop.
865 * This invokes the bulk of the Monarch processing.
866 */
867static int mce_end(int order)
868{
869 int ret = -1;
84c2559d 870 u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
3c079792
AK
871
872 if (!timeout)
873 goto reset;
874 if (order < 0)
875 goto reset;
876
877 /*
878 * Allow others to run.
879 */
880 atomic_inc(&mce_executing);
881
882 if (order == 1) {
883 /* CHECKME: Can this race with a parallel hotplug? */
884 int cpus = num_online_cpus();
885
886 /*
887 * Monarch: Wait for everyone to go through their scanning
888 * loops.
889 */
890 while (atomic_read(&mce_executing) <= cpus) {
891 if (mce_timed_out(&timeout))
892 goto reset;
893 ndelay(SPINUNIT);
894 }
895
896 mce_reign();
897 barrier();
898 ret = 0;
899 } else {
900 /*
901 * Subject: Wait for Monarch to finish.
902 */
903 while (atomic_read(&mce_executing) != 0) {
904 if (mce_timed_out(&timeout))
905 goto reset;
906 ndelay(SPINUNIT);
907 }
908
909 /*
910 * Don't reset anything. That's done by the Monarch.
911 */
912 return 0;
913 }
914
915 /*
916 * Reset all global state.
917 */
918reset:
919 atomic_set(&global_nwo, 0);
920 atomic_set(&mce_callin, 0);
921 barrier();
922
923 /*
924 * Let others run again.
925 */
926 atomic_set(&mce_executing, 0);
927 return ret;
928}
929
9b1beaf2
AK
930/*
931 * Check if the address reported by the CPU is in a format we can parse.
932 * It would be possible to add code for most other cases, but all would
933 * be somewhat complicated (e.g. segment offset would require an instruction
0d2eb44f 934 * parser). So only support physical addresses up to page granuality for now.
9b1beaf2
AK
935 */
936static int mce_usable_address(struct mce *m)
937{
938 if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
939 return 0;
2b90e77e 940 if (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
9b1beaf2 941 return 0;
2b90e77e 942 if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_ADDR_PHYS)
9b1beaf2
AK
943 return 0;
944 return 1;
945}
946
3c079792
AK
947static void mce_clear_state(unsigned long *toclear)
948{
949 int i;
950
d203f0b8 951 for (i = 0; i < mca_cfg.banks; i++) {
3c079792 952 if (test_bit(i, toclear))
a2d32bcb 953 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
3c079792
AK
954 }
955}
956
af104e39
TL
957/*
958 * Need to save faulting physical address associated with a process
959 * in the machine check handler some place where we can grab it back
960 * later in mce_notify_process()
961 */
962#define MCE_INFO_MAX 16
963
964struct mce_info {
965 atomic_t inuse;
966 struct task_struct *t;
967 __u64 paddr;
dad1743e 968 int restartable;
af104e39
TL
969} mce_info[MCE_INFO_MAX];
970
dad1743e 971static void mce_save_info(__u64 addr, int c)
af104e39
TL
972{
973 struct mce_info *mi;
974
975 for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++) {
976 if (atomic_cmpxchg(&mi->inuse, 0, 1) == 0) {
977 mi->t = current;
978 mi->paddr = addr;
dad1743e 979 mi->restartable = c;
af104e39
TL
980 return;
981 }
982 }
983
984 mce_panic("Too many concurrent recoverable errors", NULL, NULL);
985}
986
987static struct mce_info *mce_find_info(void)
988{
989 struct mce_info *mi;
990
991 for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++)
992 if (atomic_read(&mi->inuse) && mi->t == current)
993 return mi;
994 return NULL;
995}
996
997static void mce_clear_info(struct mce_info *mi)
998{
999 atomic_set(&mi->inuse, 0);
1000}
1001
b79109c3
AK
1002/*
1003 * The actual machine check handler. This only handles real
1004 * exceptions when something got corrupted coming in through int 18.
1005 *
1006 * This is executed in NMI context not subject to normal locking rules. This
1007 * implies that most kernel services cannot be safely used. Don't even
1008 * think about putting a printk in there!
3c079792
AK
1009 *
1010 * On Intel systems this is entered on all CPUs in parallel through
1011 * MCE broadcast. However some CPUs might be broken beyond repair,
1012 * so be always careful when synchronizing with others.
1da177e4 1013 */
e9eee03e 1014void do_machine_check(struct pt_regs *regs, long error_code)
1da177e4 1015{
1462594b 1016 struct mca_config *cfg = &mca_cfg;
3c079792 1017 struct mce m, *final;
1da177e4 1018 int i;
3c079792
AK
1019 int worst = 0;
1020 int severity;
1021 /*
1022 * Establish sequential order between the CPUs entering the machine
1023 * check handler.
1024 */
7fb06fc9 1025 int order;
bd78432c
TH
1026 /*
1027 * If no_way_out gets set, there is no safe way to recover from this
d203f0b8 1028 * MCE. If mca_cfg.tolerant is cranked up, we'll try anyway.
bd78432c
TH
1029 */
1030 int no_way_out = 0;
1031 /*
1032 * If kill_it gets set, there might be a way to recover from this
1033 * error.
1034 */
1035 int kill_it = 0;
b79109c3 1036 DECLARE_BITMAP(toclear, MAX_NR_BANKS);
95022b8c 1037 DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
bd19a5e6 1038 char *msg = "Unknown";
1da177e4 1039
553f265f
AK
1040 atomic_inc(&mce_entry);
1041
c6ae41e7 1042 this_cpu_inc(mce_exception_count);
01ca79f1 1043
1462594b 1044 if (!cfg->banks)
32561696 1045 goto out;
1da177e4 1046
b8325c5b 1047 mce_gather_info(&m, regs);
b5f2fa4e 1048
3c079792
AK
1049 final = &__get_cpu_var(mces_seen);
1050 *final = m;
1051
95022b8c 1052 memset(valid_banks, 0, sizeof(valid_banks));
61b0fccd 1053 no_way_out = mce_no_way_out(&m, &msg, valid_banks, regs);
680b6cfd 1054
1da177e4
LT
1055 barrier();
1056
ed7290d0 1057 /*
a8c321fb
TL
1058 * When no restart IP might need to kill or panic.
1059 * Assume the worst for now, but if we find the
1060 * severity is MCE_AR_SEVERITY we have other options.
ed7290d0
AK
1061 */
1062 if (!(m.mcgstatus & MCG_STATUS_RIPV))
1063 kill_it = 1;
1064
3c079792
AK
1065 /*
1066 * Go through all the banks in exclusion of the other CPUs.
1067 * This way we don't report duplicated events on shared banks
1068 * because the first one to see it will clear it.
1069 */
7fb06fc9 1070 order = mce_start(&no_way_out);
1462594b 1071 for (i = 0; i < cfg->banks; i++) {
b79109c3 1072 __clear_bit(i, toclear);
95022b8c
TL
1073 if (!test_bit(i, valid_banks))
1074 continue;
cebe1820 1075 if (!mce_banks[i].ctl)
1da177e4 1076 continue;
d88203d1
TG
1077
1078 m.misc = 0;
1da177e4
LT
1079 m.addr = 0;
1080 m.bank = i;
1da177e4 1081
a2d32bcb 1082 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
1da177e4
LT
1083 if ((m.status & MCI_STATUS_VAL) == 0)
1084 continue;
1085
b79109c3 1086 /*
ed7290d0
AK
1087 * Non uncorrected or non signaled errors are handled by
1088 * machine_check_poll. Leave them alone, unless this panics.
b79109c3 1089 */
1462594b 1090 if (!(m.status & (cfg->ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
ed7290d0 1091 !no_way_out)
b79109c3
AK
1092 continue;
1093
1094 /*
1095 * Set taint even when machine check was not enabled.
1096 */
373d4d09 1097 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
b79109c3 1098
1462594b 1099 severity = mce_severity(&m, cfg->tolerant, NULL);
b79109c3 1100
ed7290d0
AK
1101 /*
1102 * When machine check was for corrected handler don't touch,
1103 * unless we're panicing.
1104 */
1105 if (severity == MCE_KEEP_SEVERITY && !no_way_out)
1106 continue;
1107 __set_bit(i, toclear);
1108 if (severity == MCE_NO_SEVERITY) {
b79109c3
AK
1109 /*
1110 * Machine check event was not enabled. Clear, but
1111 * ignore.
1112 */
1113 continue;
1da177e4
LT
1114 }
1115
85f92694 1116 mce_read_aux(&m, i);
1da177e4 1117
9b1beaf2
AK
1118 /*
1119 * Action optional error. Queue address for later processing.
1120 * When the ring overflows we just ignore the AO error.
1121 * RED-PEN add some logging mechanism when
1122 * usable_address or mce_add_ring fails.
d203f0b8 1123 * RED-PEN don't ignore overflow for mca_cfg.tolerant == 0
9b1beaf2
AK
1124 */
1125 if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
1126 mce_ring_add(m.addr >> PAGE_SHIFT);
1127
b79109c3 1128 mce_log(&m);
1da177e4 1129
3c079792
AK
1130 if (severity > worst) {
1131 *final = m;
1132 worst = severity;
1da177e4 1133 }
1da177e4
LT
1134 }
1135
a8c321fb
TL
1136 /* mce_clear_state will clear *final, save locally for use later */
1137 m = *final;
1138
3c079792
AK
1139 if (!no_way_out)
1140 mce_clear_state(toclear);
1141
e9eee03e 1142 /*
3c079792
AK
1143 * Do most of the synchronization with other CPUs.
1144 * When there's any problem use only local no_way_out state.
e9eee03e 1145 */
3c079792
AK
1146 if (mce_end(order) < 0)
1147 no_way_out = worst >= MCE_PANIC_SEVERITY;
bd78432c
TH
1148
1149 /*
a8c321fb
TL
1150 * At insane "tolerant" levels we take no action. Otherwise
1151 * we only die if we have no other choice. For less serious
1152 * issues we try to recover, or limit damage to the current
1153 * process.
bd78432c 1154 */
1462594b 1155 if (cfg->tolerant < 3) {
a8c321fb
TL
1156 if (no_way_out)
1157 mce_panic("Fatal machine check on current CPU", &m, msg);
1158 if (worst == MCE_AR_SEVERITY) {
1159 /* schedule action before return to userland */
dad1743e 1160 mce_save_info(m.addr, m.mcgstatus & MCG_STATUS_RIPV);
a8c321fb
TL
1161 set_thread_flag(TIF_MCE_NOTIFY);
1162 } else if (kill_it) {
1163 force_sig(SIGBUS, current);
1164 }
1165 }
e02e68d3 1166
3c079792
AK
1167 if (worst > 0)
1168 mce_report_event(regs);
5f8c1a54 1169 mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
32561696 1170out:
553f265f 1171 atomic_dec(&mce_entry);
88921be3 1172 sync_core();
1da177e4 1173}
ea149b36 1174EXPORT_SYMBOL_GPL(do_machine_check);
1da177e4 1175
cd42f4a3
TL
1176#ifndef CONFIG_MEMORY_FAILURE
1177int memory_failure(unsigned long pfn, int vector, int flags)
9b1beaf2 1178{
a8c321fb
TL
1179 /* mce_severity() should not hand us an ACTION_REQUIRED error */
1180 BUG_ON(flags & MF_ACTION_REQUIRED);
c767a54b
JP
1181 pr_err("Uncorrected memory error in page 0x%lx ignored\n"
1182 "Rebuild kernel with CONFIG_MEMORY_FAILURE=y for smarter handling\n",
1183 pfn);
cd42f4a3
TL
1184
1185 return 0;
9b1beaf2 1186}
cd42f4a3 1187#endif
9b1beaf2
AK
1188
1189/*
a8c321fb
TL
1190 * Called in process context that interrupted by MCE and marked with
1191 * TIF_MCE_NOTIFY, just before returning to erroneous userland.
1192 * This code is allowed to sleep.
1193 * Attempt possible recovery such as calling the high level VM handler to
1194 * process any corrupted pages, and kill/signal current process if required.
1195 * Action required errors are handled here.
9b1beaf2
AK
1196 */
1197void mce_notify_process(void)
1198{
1199 unsigned long pfn;
a8c321fb 1200 struct mce_info *mi = mce_find_info();
6751ed65 1201 int flags = MF_ACTION_REQUIRED;
a8c321fb
TL
1202
1203 if (!mi)
1204 mce_panic("Lost physical address for unconsumed uncorrectable error", NULL, NULL);
1205 pfn = mi->paddr >> PAGE_SHIFT;
1206
1207 clear_thread_flag(TIF_MCE_NOTIFY);
1208
1209 pr_err("Uncorrected hardware memory error in user-access at %llx",
1210 mi->paddr);
dad1743e
TL
1211 /*
1212 * We must call memory_failure() here even if the current process is
1213 * doomed. We still need to mark the page as poisoned and alert any
1214 * other users of the page.
1215 */
6751ed65
TL
1216 if (!mi->restartable)
1217 flags |= MF_MUST_KILL;
1218 if (memory_failure(pfn, MCE_VECTOR, flags) < 0) {
a8c321fb
TL
1219 pr_err("Memory error not recovered");
1220 force_sig(SIGBUS, current);
1221 }
1222 mce_clear_info(mi);
9b1beaf2
AK
1223}
1224
a8c321fb
TL
1225/*
1226 * Action optional processing happens here (picking up
1227 * from the list of faulting pages that do_machine_check()
1228 * placed into the "ring").
1229 */
9b1beaf2
AK
1230static void mce_process_work(struct work_struct *dummy)
1231{
a8c321fb
TL
1232 unsigned long pfn;
1233
1234 while (mce_ring_get(&pfn))
1235 memory_failure(pfn, MCE_VECTOR, 0);
9b1beaf2
AK
1236}
1237
15d5f839
DZ
1238#ifdef CONFIG_X86_MCE_INTEL
1239/***
1240 * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
676b1855 1241 * @cpu: The CPU on which the event occurred.
15d5f839
DZ
1242 * @status: Event status information
1243 *
1244 * This function should be called by the thermal interrupt after the
1245 * event has been processed and the decision was made to log the event
1246 * further.
1247 *
1248 * The status parameter will be saved to the 'status' field of 'struct mce'
1249 * and historically has been the register value of the
1250 * MSR_IA32_THERMAL_STATUS (Intel) msr.
1251 */
b5f2fa4e 1252void mce_log_therm_throt_event(__u64 status)
15d5f839
DZ
1253{
1254 struct mce m;
1255
b5f2fa4e 1256 mce_setup(&m);
15d5f839
DZ
1257 m.bank = MCE_THERMAL_BANK;
1258 m.status = status;
15d5f839
DZ
1259 mce_log(&m);
1260}
1261#endif /* CONFIG_X86_MCE_INTEL */
1262
1da177e4 1263/*
8a336b0a
TH
1264 * Periodic polling timer for "silent" machine check errors. If the
1265 * poller finds an MCE, poll 2x faster. When the poller finds no more
1266 * errors, poll 2x slower (up to check_interval seconds).
1da177e4 1267 */
82f7af09 1268static unsigned long check_interval = 5 * 60; /* 5 minutes */
e9eee03e 1269
82f7af09 1270static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
52d168e2 1271static DEFINE_PER_CPU(struct timer_list, mce_timer);
1da177e4 1272
55babd8f
CG
1273static unsigned long mce_adjust_timer_default(unsigned long interval)
1274{
1275 return interval;
1276}
1277
1278static unsigned long (*mce_adjust_timer)(unsigned long interval) =
1279 mce_adjust_timer_default;
1280
82f7af09 1281static void mce_timer_fn(unsigned long data)
1da177e4 1282{
82f7af09
TG
1283 struct timer_list *t = &__get_cpu_var(mce_timer);
1284 unsigned long iv;
52d168e2
AK
1285
1286 WARN_ON(smp_processor_id() != data);
1287
7b543a53 1288 if (mce_available(__this_cpu_ptr(&cpu_info))) {
ee031c31
AK
1289 machine_check_poll(MCP_TIMESTAMP,
1290 &__get_cpu_var(mce_poll_banks));
55babd8f 1291 mce_intel_cmci_poll();
e9eee03e 1292 }
1da177e4
LT
1293
1294 /*
e02e68d3
TH
1295 * Alert userspace if needed. If we logged an MCE, reduce the
1296 * polling interval, otherwise increase the polling interval.
1da177e4 1297 */
82f7af09 1298 iv = __this_cpu_read(mce_next_interval);
55babd8f 1299 if (mce_notify_irq()) {
958fb3c5 1300 iv = max(iv / 2, (unsigned long) HZ/100);
55babd8f 1301 } else {
82f7af09 1302 iv = min(iv * 2, round_jiffies_relative(check_interval * HZ));
55babd8f
CG
1303 iv = mce_adjust_timer(iv);
1304 }
82f7af09 1305 __this_cpu_write(mce_next_interval, iv);
55babd8f
CG
1306 /* Might have become 0 after CMCI storm subsided */
1307 if (iv) {
1308 t->expires = jiffies + iv;
1309 add_timer_on(t, smp_processor_id());
1310 }
1311}
e02e68d3 1312
55babd8f
CG
1313/*
1314 * Ensure that the timer is firing in @interval from now.
1315 */
1316void mce_timer_kick(unsigned long interval)
1317{
1318 struct timer_list *t = &__get_cpu_var(mce_timer);
1319 unsigned long when = jiffies + interval;
1320 unsigned long iv = __this_cpu_read(mce_next_interval);
1321
1322 if (timer_pending(t)) {
1323 if (time_before(when, t->expires))
1324 mod_timer_pinned(t, when);
1325 } else {
1326 t->expires = round_jiffies(when);
1327 add_timer_on(t, smp_processor_id());
1328 }
1329 if (interval < iv)
1330 __this_cpu_write(mce_next_interval, interval);
e02e68d3
TH
1331}
1332
9aaef96f
HS
1333/* Must not be called in IRQ context where del_timer_sync() can deadlock */
1334static void mce_timer_delete_all(void)
1335{
1336 int cpu;
1337
1338 for_each_online_cpu(cpu)
1339 del_timer_sync(&per_cpu(mce_timer, cpu));
1340}
1341
9bd98405
AK
1342static void mce_do_trigger(struct work_struct *work)
1343{
1020bcbc 1344 call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
9bd98405
AK
1345}
1346
1347static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
1348
e02e68d3 1349/*
9bd98405
AK
1350 * Notify the user(s) about new machine check events.
1351 * Can be called from interrupt context, but not from machine check/NMI
1352 * context.
e02e68d3 1353 */
9ff36ee9 1354int mce_notify_irq(void)
e02e68d3 1355{
8457c84d
AK
1356 /* Not more than two messages every minute */
1357 static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1358
1020bcbc 1359 if (test_and_clear_bit(0, &mce_need_notify)) {
93b62c3c
HS
1360 /* wake processes polling /dev/mcelog */
1361 wake_up_interruptible(&mce_chrdev_wait);
9bd98405 1362
4d899be5 1363 if (mce_helper[0])
9bd98405 1364 schedule_work(&mce_trigger_work);
e02e68d3 1365
8457c84d 1366 if (__ratelimit(&ratelimit))
a2d7b0d4 1367 pr_info(HW_ERR "Machine check events logged\n");
e02e68d3
TH
1368
1369 return 1;
1da177e4 1370 }
e02e68d3
TH
1371 return 0;
1372}
9ff36ee9 1373EXPORT_SYMBOL_GPL(mce_notify_irq);
8a336b0a 1374
148f9bb8 1375static int __mcheck_cpu_mce_banks_init(void)
cebe1820
AK
1376{
1377 int i;
d203f0b8 1378 u8 num_banks = mca_cfg.banks;
cebe1820 1379
d203f0b8 1380 mce_banks = kzalloc(num_banks * sizeof(struct mce_bank), GFP_KERNEL);
cebe1820
AK
1381 if (!mce_banks)
1382 return -ENOMEM;
d203f0b8
BP
1383
1384 for (i = 0; i < num_banks; i++) {
cebe1820 1385 struct mce_bank *b = &mce_banks[i];
11868a2d 1386
cebe1820
AK
1387 b->ctl = -1ULL;
1388 b->init = 1;
1389 }
1390 return 0;
1391}
1392
d88203d1 1393/*
1da177e4
LT
1394 * Initialize Machine Checks for a CPU.
1395 */
148f9bb8 1396static int __mcheck_cpu_cap_init(void)
1da177e4 1397{
0d7482e3 1398 unsigned b;
e9eee03e 1399 u64 cap;
1da177e4
LT
1400
1401 rdmsrl(MSR_IA32_MCG_CAP, cap);
01c6680a
TG
1402
1403 b = cap & MCG_BANKCNT_MASK;
d203f0b8 1404 if (!mca_cfg.banks)
c767a54b 1405 pr_info("CPU supports %d MCE banks\n", b);
b659294b 1406
0d7482e3 1407 if (b > MAX_NR_BANKS) {
c767a54b 1408 pr_warn("Using only %u machine check banks out of %u\n",
0d7482e3
AK
1409 MAX_NR_BANKS, b);
1410 b = MAX_NR_BANKS;
1411 }
1412
1413 /* Don't support asymmetric configurations today */
d203f0b8
BP
1414 WARN_ON(mca_cfg.banks != 0 && b != mca_cfg.banks);
1415 mca_cfg.banks = b;
1416
cebe1820 1417 if (!mce_banks) {
cffd377e 1418 int err = __mcheck_cpu_mce_banks_init();
11868a2d 1419
cebe1820
AK
1420 if (err)
1421 return err;
1da177e4 1422 }
0d7482e3 1423
94ad8474 1424 /* Use accurate RIP reporting if available. */
01c6680a 1425 if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
84c2559d 1426 mca_cfg.rip_msr = MSR_IA32_MCG_EIP;
1da177e4 1427
ed7290d0 1428 if (cap & MCG_SER_P)
1462594b 1429 mca_cfg.ser = true;
ed7290d0 1430
0d7482e3
AK
1431 return 0;
1432}
1433
5e09954a 1434static void __mcheck_cpu_init_generic(void)
0d7482e3 1435{
84c2559d 1436 enum mcp_flags m_fl = 0;
e9eee03e 1437 mce_banks_t all_banks;
0d7482e3
AK
1438 u64 cap;
1439 int i;
1440
84c2559d
BP
1441 if (!mca_cfg.bootlog)
1442 m_fl = MCP_DONTLOG;
1443
b79109c3
AK
1444 /*
1445 * Log the machine checks left over from the previous reset.
1446 */
ee031c31 1447 bitmap_fill(all_banks, MAX_NR_BANKS);
84c2559d 1448 machine_check_poll(MCP_UC | m_fl, &all_banks);
1da177e4
LT
1449
1450 set_in_cr4(X86_CR4_MCE);
1451
0d7482e3 1452 rdmsrl(MSR_IA32_MCG_CAP, cap);
1da177e4
LT
1453 if (cap & MCG_CTL_P)
1454 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1455
d203f0b8 1456 for (i = 0; i < mca_cfg.banks; i++) {
cebe1820 1457 struct mce_bank *b = &mce_banks[i];
11868a2d 1458
cebe1820 1459 if (!b->init)
06b7a7a5 1460 continue;
a2d32bcb
AK
1461 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
1462 wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
d88203d1 1463 }
1da177e4
LT
1464}
1465
61b0fccd
TL
1466/*
1467 * During IFU recovery Sandy Bridge -EP4S processors set the RIPV and
1468 * EIPV bits in MCG_STATUS to zero on the affected logical processor (SDM
1469 * Vol 3B Table 15-20). But this confuses both the code that determines
1470 * whether the machine check occurred in kernel or user mode, and also
1471 * the severity assessment code. Pretend that EIPV was set, and take the
1472 * ip/cs values from the pt_regs that mce_gather_info() ignored earlier.
1473 */
1474static void quirk_sandybridge_ifu(int bank, struct mce *m, struct pt_regs *regs)
1475{
1476 if (bank != 0)
1477 return;
1478 if ((m->mcgstatus & (MCG_STATUS_EIPV|MCG_STATUS_RIPV)) != 0)
1479 return;
1480 if ((m->status & (MCI_STATUS_OVER|MCI_STATUS_UC|
1481 MCI_STATUS_EN|MCI_STATUS_MISCV|MCI_STATUS_ADDRV|
1482 MCI_STATUS_PCC|MCI_STATUS_S|MCI_STATUS_AR|
1483 MCACOD)) !=
1484 (MCI_STATUS_UC|MCI_STATUS_EN|
1485 MCI_STATUS_MISCV|MCI_STATUS_ADDRV|MCI_STATUS_S|
1486 MCI_STATUS_AR|MCACOD_INSTR))
1487 return;
1488
1489 m->mcgstatus |= MCG_STATUS_EIPV;
1490 m->ip = regs->ip;
1491 m->cs = regs->cs;
1492}
1493
1da177e4 1494/* Add per CPU specific workarounds here */
148f9bb8 1495static int __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
d88203d1 1496{
d203f0b8
BP
1497 struct mca_config *cfg = &mca_cfg;
1498
e412cd25 1499 if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
c767a54b 1500 pr_info("unknown CPU type - not enabling MCE support\n");
e412cd25
IM
1501 return -EOPNOTSUPP;
1502 }
1503
1da177e4 1504 /* This should be disabled by the BIOS, but isn't always */
911f6a7b 1505 if (c->x86_vendor == X86_VENDOR_AMD) {
d203f0b8 1506 if (c->x86 == 15 && cfg->banks > 4) {
e9eee03e
IM
1507 /*
1508 * disable GART TBL walk error reporting, which
1509 * trips off incorrectly with the IOMMU & 3ware
1510 * & Cerberus:
1511 */
cebe1820 1512 clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
e9eee03e 1513 }
84c2559d 1514 if (c->x86 <= 17 && cfg->bootlog < 0) {
e9eee03e
IM
1515 /*
1516 * Lots of broken BIOS around that don't clear them
1517 * by default and leave crap in there. Don't log:
1518 */
84c2559d 1519 cfg->bootlog = 0;
e9eee03e 1520 }
2e6f694f
AK
1521 /*
1522 * Various K7s with broken bank 0 around. Always disable
1523 * by default.
1524 */
d203f0b8 1525 if (c->x86 == 6 && cfg->banks > 0)
cebe1820 1526 mce_banks[0].ctl = 0;
575203b4
BP
1527
1528 /*
1529 * Turn off MC4_MISC thresholding banks on those models since
1530 * they're not supported there.
1531 */
1532 if (c->x86 == 0x15 &&
1533 (c->x86_model >= 0x10 && c->x86_model <= 0x1f)) {
1534 int i;
1535 u64 val, hwcr;
1536 bool need_toggle;
1537 u32 msrs[] = {
1538 0x00000413, /* MC4_MISC0 */
1539 0xc0000408, /* MC4_MISC1 */
1540 };
1541
1542 rdmsrl(MSR_K7_HWCR, hwcr);
1543
1544 /* McStatusWrEn has to be set */
1545 need_toggle = !(hwcr & BIT(18));
1546
1547 if (need_toggle)
1548 wrmsrl(MSR_K7_HWCR, hwcr | BIT(18));
1549
1550 for (i = 0; i < ARRAY_SIZE(msrs); i++) {
1551 rdmsrl(msrs[i], val);
1552
1553 /* CntP bit set? */
80f03361
BP
1554 if (val & BIT_64(62)) {
1555 val &= ~BIT_64(62);
1556 wrmsrl(msrs[i], val);
575203b4
BP
1557 }
1558 }
1559
1560 /* restore old settings */
1561 if (need_toggle)
1562 wrmsrl(MSR_K7_HWCR, hwcr);
1563 }
1da177e4 1564 }
e583538f 1565
06b7a7a5
AK
1566 if (c->x86_vendor == X86_VENDOR_INTEL) {
1567 /*
1568 * SDM documents that on family 6 bank 0 should not be written
1569 * because it aliases to another special BIOS controlled
1570 * register.
1571 * But it's not aliased anymore on model 0x1a+
1572 * Don't ignore bank 0 completely because there could be a
1573 * valid event later, merely don't write CTL0.
1574 */
1575
d203f0b8 1576 if (c->x86 == 6 && c->x86_model < 0x1A && cfg->banks > 0)
cebe1820 1577 mce_banks[0].init = 0;
3c079792
AK
1578
1579 /*
1580 * All newer Intel systems support MCE broadcasting. Enable
1581 * synchronization with a one second timeout.
1582 */
1583 if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
84c2559d
BP
1584 cfg->monarch_timeout < 0)
1585 cfg->monarch_timeout = USEC_PER_SEC;
c7f6fa44 1586
e412cd25
IM
1587 /*
1588 * There are also broken BIOSes on some Pentium M and
1589 * earlier systems:
1590 */
84c2559d
BP
1591 if (c->x86 == 6 && c->x86_model <= 13 && cfg->bootlog < 0)
1592 cfg->bootlog = 0;
61b0fccd
TL
1593
1594 if (c->x86 == 6 && c->x86_model == 45)
1595 quirk_no_way_out = quirk_sandybridge_ifu;
06b7a7a5 1596 }
84c2559d
BP
1597 if (cfg->monarch_timeout < 0)
1598 cfg->monarch_timeout = 0;
1599 if (cfg->bootlog != 0)
7af19e4a 1600 cfg->panic_timeout = 30;
e412cd25
IM
1601
1602 return 0;
d88203d1 1603}
1da177e4 1604
148f9bb8 1605static int __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
4efc0670
AK
1606{
1607 if (c->x86 != 5)
3a97fc34
HS
1608 return 0;
1609
4efc0670
AK
1610 switch (c->x86_vendor) {
1611 case X86_VENDOR_INTEL:
c6978369 1612 intel_p5_mcheck_init(c);
3a97fc34 1613 return 1;
4efc0670
AK
1614 break;
1615 case X86_VENDOR_CENTAUR:
1616 winchip_mcheck_init(c);
3a97fc34 1617 return 1;
4efc0670
AK
1618 break;
1619 }
3a97fc34
HS
1620
1621 return 0;
4efc0670
AK
1622}
1623
5e09954a 1624static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
1da177e4
LT
1625{
1626 switch (c->x86_vendor) {
1627 case X86_VENDOR_INTEL:
1628 mce_intel_feature_init(c);
55babd8f 1629 mce_adjust_timer = mce_intel_adjust_timer;
1da177e4 1630 break;
89b831ef
JS
1631 case X86_VENDOR_AMD:
1632 mce_amd_feature_init(c);
1633 break;
1da177e4
LT
1634 default:
1635 break;
1636 }
1637}
1638
26c3c283 1639static void mce_start_timer(unsigned int cpu, struct timer_list *t)
52d168e2 1640{
55babd8f 1641 unsigned long iv = mce_adjust_timer(check_interval * HZ);
52d168e2 1642
26c3c283 1643 __this_cpu_write(mce_next_interval, iv);
bc09effa 1644
7af19e4a 1645 if (mca_cfg.ignore_ce || !iv)
62fdac59
HS
1646 return;
1647
82f7af09 1648 t->expires = round_jiffies(jiffies + iv);
5be6066a 1649 add_timer_on(t, smp_processor_id());
52d168e2
AK
1650}
1651
26c3c283
TG
1652static void __mcheck_cpu_init_timer(void)
1653{
1654 struct timer_list *t = &__get_cpu_var(mce_timer);
1655 unsigned int cpu = smp_processor_id();
1656
1657 setup_timer(t, mce_timer_fn, cpu);
1658 mce_start_timer(cpu, t);
1659}
1660
9eda8cb3
AK
1661/* Handle unconfigured int18 (should never happen) */
1662static void unexpected_machine_check(struct pt_regs *regs, long error_code)
1663{
c767a54b 1664 pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
9eda8cb3
AK
1665 smp_processor_id());
1666}
1667
1668/* Call the installed machine check handler for this CPU setup. */
1669void (*machine_check_vector)(struct pt_regs *, long error_code) =
1670 unexpected_machine_check;
1671
d88203d1 1672/*
1da177e4 1673 * Called for each booted CPU to set up machine checks.
e9eee03e 1674 * Must be called with preempt off:
1da177e4 1675 */
148f9bb8 1676void mcheck_cpu_init(struct cpuinfo_x86 *c)
1da177e4 1677{
1462594b 1678 if (mca_cfg.disabled)
4efc0670
AK
1679 return;
1680
3a97fc34
HS
1681 if (__mcheck_cpu_ancient_init(c))
1682 return;
4efc0670 1683
5b4408fd 1684 if (!mce_available(c))
1da177e4
LT
1685 return;
1686
5e09954a 1687 if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
1462594b 1688 mca_cfg.disabled = true;
0d7482e3
AK
1689 return;
1690 }
0d7482e3 1691
5d727926
AK
1692 machine_check_vector = do_machine_check;
1693
5e09954a
BP
1694 __mcheck_cpu_init_generic();
1695 __mcheck_cpu_init_vendor(c);
1696 __mcheck_cpu_init_timer();
9b1beaf2 1697 INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
b77e70bf 1698 init_irq_work(&__get_cpu_var(mce_irq_work), &mce_irq_work_cb);
1da177e4
LT
1699}
1700
1701/*
93b62c3c 1702 * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
1da177e4
LT
1703 */
1704
93b62c3c
HS
1705static DEFINE_SPINLOCK(mce_chrdev_state_lock);
1706static int mce_chrdev_open_count; /* #times opened */
1707static int mce_chrdev_open_exclu; /* already open exclusive? */
f528e7ba 1708
93b62c3c 1709static int mce_chrdev_open(struct inode *inode, struct file *file)
f528e7ba 1710{
93b62c3c 1711 spin_lock(&mce_chrdev_state_lock);
f528e7ba 1712
93b62c3c
HS
1713 if (mce_chrdev_open_exclu ||
1714 (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
1715 spin_unlock(&mce_chrdev_state_lock);
e9eee03e 1716
f528e7ba
TH
1717 return -EBUSY;
1718 }
1719
1720 if (file->f_flags & O_EXCL)
93b62c3c
HS
1721 mce_chrdev_open_exclu = 1;
1722 mce_chrdev_open_count++;
f528e7ba 1723
93b62c3c 1724 spin_unlock(&mce_chrdev_state_lock);
f528e7ba 1725
bd78432c 1726 return nonseekable_open(inode, file);
f528e7ba
TH
1727}
1728
93b62c3c 1729static int mce_chrdev_release(struct inode *inode, struct file *file)
f528e7ba 1730{
93b62c3c 1731 spin_lock(&mce_chrdev_state_lock);
f528e7ba 1732
93b62c3c
HS
1733 mce_chrdev_open_count--;
1734 mce_chrdev_open_exclu = 0;
f528e7ba 1735
93b62c3c 1736 spin_unlock(&mce_chrdev_state_lock);
f528e7ba
TH
1737
1738 return 0;
1739}
1740
d88203d1
TG
1741static void collect_tscs(void *data)
1742{
1da177e4 1743 unsigned long *cpu_tsc = (unsigned long *)data;
d88203d1 1744
1da177e4 1745 rdtscll(cpu_tsc[smp_processor_id()]);
d88203d1 1746}
1da177e4 1747
482908b4
HY
1748static int mce_apei_read_done;
1749
1750/* Collect MCE record of previous boot in persistent storage via APEI ERST. */
1751static int __mce_read_apei(char __user **ubuf, size_t usize)
1752{
1753 int rc;
1754 u64 record_id;
1755 struct mce m;
1756
1757 if (usize < sizeof(struct mce))
1758 return -EINVAL;
1759
1760 rc = apei_read_mce(&m, &record_id);
1761 /* Error or no more MCE record */
1762 if (rc <= 0) {
1763 mce_apei_read_done = 1;
fadd85f1
NH
1764 /*
1765 * When ERST is disabled, mce_chrdev_read() should return
1766 * "no record" instead of "no device."
1767 */
1768 if (rc == -ENODEV)
1769 return 0;
482908b4
HY
1770 return rc;
1771 }
1772 rc = -EFAULT;
1773 if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
1774 return rc;
1775 /*
1776 * In fact, we should have cleared the record after that has
1777 * been flushed to the disk or sent to network in
1778 * /sbin/mcelog, but we have no interface to support that now,
1779 * so just clear it to avoid duplication.
1780 */
1781 rc = apei_clear_mce(record_id);
1782 if (rc) {
1783 mce_apei_read_done = 1;
1784 return rc;
1785 }
1786 *ubuf += sizeof(struct mce);
1787
1788 return 0;
1789}
1790
93b62c3c
HS
1791static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
1792 size_t usize, loff_t *off)
1da177e4 1793{
e9eee03e 1794 char __user *buf = ubuf;
f0de53bb 1795 unsigned long *cpu_tsc;
ef41df43 1796 unsigned prev, next;
1da177e4
LT
1797 int i, err;
1798
6bca67f9 1799 cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
f0de53bb
AK
1800 if (!cpu_tsc)
1801 return -ENOMEM;
1802
93b62c3c 1803 mutex_lock(&mce_chrdev_read_mutex);
482908b4
HY
1804
1805 if (!mce_apei_read_done) {
1806 err = __mce_read_apei(&buf, usize);
1807 if (err || buf != ubuf)
1808 goto out;
1809 }
1810
f56e8a07 1811 next = rcu_dereference_check_mce(mcelog.next);
1da177e4
LT
1812
1813 /* Only supports full reads right now */
482908b4
HY
1814 err = -EINVAL;
1815 if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
1816 goto out;
1da177e4
LT
1817
1818 err = 0;
ef41df43
HY
1819 prev = 0;
1820 do {
1821 for (i = prev; i < next; i++) {
1822 unsigned long start = jiffies;
559faa6b 1823 struct mce *m = &mcelog.entry[i];
ef41df43 1824
559faa6b 1825 while (!m->finished) {
ef41df43 1826 if (time_after_eq(jiffies, start + 2)) {
559faa6b 1827 memset(m, 0, sizeof(*m));
ef41df43
HY
1828 goto timeout;
1829 }
1830 cpu_relax();
673242c1 1831 }
ef41df43 1832 smp_rmb();
559faa6b
HS
1833 err |= copy_to_user(buf, m, sizeof(*m));
1834 buf += sizeof(*m);
ef41df43
HY
1835timeout:
1836 ;
673242c1 1837 }
1da177e4 1838
ef41df43
HY
1839 memset(mcelog.entry + prev, 0,
1840 (next - prev) * sizeof(struct mce));
1841 prev = next;
1842 next = cmpxchg(&mcelog.next, prev, 0);
1843 } while (next != prev);
1da177e4 1844
b2b18660 1845 synchronize_sched();
1da177e4 1846
d88203d1
TG
1847 /*
1848 * Collect entries that were still getting written before the
1849 * synchronize.
1850 */
15c8b6c1 1851 on_each_cpu(collect_tscs, cpu_tsc, 1);
e9eee03e 1852
d88203d1 1853 for (i = next; i < MCE_LOG_LEN; i++) {
559faa6b
HS
1854 struct mce *m = &mcelog.entry[i];
1855
1856 if (m->finished && m->tsc < cpu_tsc[m->cpu]) {
1857 err |= copy_to_user(buf, m, sizeof(*m));
1da177e4 1858 smp_rmb();
559faa6b
HS
1859 buf += sizeof(*m);
1860 memset(m, 0, sizeof(*m));
1da177e4 1861 }
d88203d1 1862 }
482908b4
HY
1863
1864 if (err)
1865 err = -EFAULT;
1866
1867out:
93b62c3c 1868 mutex_unlock(&mce_chrdev_read_mutex);
f0de53bb 1869 kfree(cpu_tsc);
e9eee03e 1870
482908b4 1871 return err ? err : buf - ubuf;
1da177e4
LT
1872}
1873
93b62c3c 1874static unsigned int mce_chrdev_poll(struct file *file, poll_table *wait)
e02e68d3 1875{
93b62c3c 1876 poll_wait(file, &mce_chrdev_wait, wait);
a4dd9925 1877 if (rcu_access_index(mcelog.next))
e02e68d3 1878 return POLLIN | POLLRDNORM;
482908b4
HY
1879 if (!mce_apei_read_done && apei_check_mce())
1880 return POLLIN | POLLRDNORM;
e02e68d3
TH
1881 return 0;
1882}
1883
93b62c3c
HS
1884static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
1885 unsigned long arg)
1da177e4
LT
1886{
1887 int __user *p = (int __user *)arg;
d88203d1 1888
1da177e4 1889 if (!capable(CAP_SYS_ADMIN))
d88203d1 1890 return -EPERM;
e9eee03e 1891
1da177e4 1892 switch (cmd) {
d88203d1 1893 case MCE_GET_RECORD_LEN:
1da177e4
LT
1894 return put_user(sizeof(struct mce), p);
1895 case MCE_GET_LOG_LEN:
d88203d1 1896 return put_user(MCE_LOG_LEN, p);
1da177e4
LT
1897 case MCE_GETCLEAR_FLAGS: {
1898 unsigned flags;
d88203d1
TG
1899
1900 do {
1da177e4 1901 flags = mcelog.flags;
d88203d1 1902 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
e9eee03e 1903
d88203d1 1904 return put_user(flags, p);
1da177e4
LT
1905 }
1906 default:
d88203d1
TG
1907 return -ENOTTY;
1908 }
1da177e4
LT
1909}
1910
66f5ddf3
LT
1911static ssize_t (*mce_write)(struct file *filp, const char __user *ubuf,
1912 size_t usize, loff_t *off);
1913
1914void register_mce_write_callback(ssize_t (*fn)(struct file *filp,
1915 const char __user *ubuf,
1916 size_t usize, loff_t *off))
1917{
1918 mce_write = fn;
1919}
1920EXPORT_SYMBOL_GPL(register_mce_write_callback);
1921
1922ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
1923 size_t usize, loff_t *off)
1924{
1925 if (mce_write)
1926 return mce_write(filp, ubuf, usize, off);
1927 else
1928 return -EINVAL;
1929}
1930
1931static const struct file_operations mce_chrdev_ops = {
93b62c3c
HS
1932 .open = mce_chrdev_open,
1933 .release = mce_chrdev_release,
1934 .read = mce_chrdev_read,
66f5ddf3 1935 .write = mce_chrdev_write,
93b62c3c
HS
1936 .poll = mce_chrdev_poll,
1937 .unlocked_ioctl = mce_chrdev_ioctl,
1938 .llseek = no_llseek,
1da177e4
LT
1939};
1940
93b62c3c 1941static struct miscdevice mce_chrdev_device = {
1da177e4
LT
1942 MISC_MCELOG_MINOR,
1943 "mcelog",
1944 &mce_chrdev_ops,
1945};
1946
c3d1fb56
NR
1947static void __mce_disable_bank(void *arg)
1948{
1949 int bank = *((int *)arg);
1950 __clear_bit(bank, __get_cpu_var(mce_poll_banks));
1951 cmci_disable_bank(bank);
1952}
1953
1954void mce_disable_bank(int bank)
1955{
1956 if (bank >= mca_cfg.banks) {
1957 pr_warn(FW_BUG
1958 "Ignoring request to disable invalid MCA bank %d.\n",
1959 bank);
1960 return;
1961 }
1962 set_bit(bank, mce_banks_ce_disabled);
1963 on_each_cpu(__mce_disable_bank, &bank, 1);
1964}
1965
13503fa9 1966/*
62fdac59
HS
1967 * mce=off Disables machine check
1968 * mce=no_cmci Disables CMCI
1969 * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
1970 * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
3c079792
AK
1971 * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
1972 * monarchtimeout is how long to wait for other CPUs on machine
1973 * check, or 0 to not wait
13503fa9
HS
1974 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
1975 * mce=nobootlog Don't log MCEs from before booting.
450cc201 1976 * mce=bios_cmci_threshold Don't program the CMCI threshold
13503fa9 1977 */
1da177e4
LT
1978static int __init mcheck_enable(char *str)
1979{
d203f0b8
BP
1980 struct mca_config *cfg = &mca_cfg;
1981
e3346fc4 1982 if (*str == 0) {
4efc0670 1983 enable_p5_mce();
e3346fc4
BZ
1984 return 1;
1985 }
4efc0670
AK
1986 if (*str == '=')
1987 str++;
1da177e4 1988 if (!strcmp(str, "off"))
1462594b 1989 cfg->disabled = true;
62fdac59 1990 else if (!strcmp(str, "no_cmci"))
7af19e4a 1991 cfg->cmci_disabled = true;
62fdac59 1992 else if (!strcmp(str, "dont_log_ce"))
d203f0b8 1993 cfg->dont_log_ce = true;
62fdac59 1994 else if (!strcmp(str, "ignore_ce"))
7af19e4a 1995 cfg->ignore_ce = true;
13503fa9 1996 else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
84c2559d 1997 cfg->bootlog = (str[0] == 'b');
450cc201 1998 else if (!strcmp(str, "bios_cmci_threshold"))
1462594b 1999 cfg->bios_cmci_threshold = true;
3c079792 2000 else if (isdigit(str[0])) {
d203f0b8 2001 get_option(&str, &(cfg->tolerant));
3c079792
AK
2002 if (*str == ',') {
2003 ++str;
84c2559d 2004 get_option(&str, &(cfg->monarch_timeout));
3c079792
AK
2005 }
2006 } else {
c767a54b 2007 pr_info("mce argument %s ignored. Please use /sys\n", str);
13503fa9
HS
2008 return 0;
2009 }
9b41046c 2010 return 1;
1da177e4 2011}
4efc0670 2012__setup("mce", mcheck_enable);
1da177e4 2013
a2202aa2 2014int __init mcheck_init(void)
b33a6363 2015{
a2202aa2
YW
2016 mcheck_intel_therm_init();
2017
b33a6363
BP
2018 return 0;
2019}
b33a6363 2020
d88203d1 2021/*
c7cece89 2022 * mce_syscore: PM support
d88203d1 2023 */
1da177e4 2024
973a2dd1
AK
2025/*
2026 * Disable machine checks on suspend and shutdown. We can't really handle
2027 * them later.
2028 */
5e09954a 2029static int mce_disable_error_reporting(void)
973a2dd1
AK
2030{
2031 int i;
2032
d203f0b8 2033 for (i = 0; i < mca_cfg.banks; i++) {
cebe1820 2034 struct mce_bank *b = &mce_banks[i];
11868a2d 2035
cebe1820 2036 if (b->init)
a2d32bcb 2037 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
06b7a7a5 2038 }
973a2dd1
AK
2039 return 0;
2040}
2041
c7cece89 2042static int mce_syscore_suspend(void)
973a2dd1 2043{
5e09954a 2044 return mce_disable_error_reporting();
973a2dd1
AK
2045}
2046
c7cece89 2047static void mce_syscore_shutdown(void)
973a2dd1 2048{
f3c6ea1b 2049 mce_disable_error_reporting();
973a2dd1
AK
2050}
2051
e9eee03e
IM
2052/*
2053 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
2054 * Only one CPU is active at this time, the others get re-added later using
2055 * CPU hotplug:
2056 */
c7cece89 2057static void mce_syscore_resume(void)
1da177e4 2058{
5e09954a 2059 __mcheck_cpu_init_generic();
7b543a53 2060 __mcheck_cpu_init_vendor(__this_cpu_ptr(&cpu_info));
1da177e4
LT
2061}
2062
f3c6ea1b 2063static struct syscore_ops mce_syscore_ops = {
c7cece89
HS
2064 .suspend = mce_syscore_suspend,
2065 .shutdown = mce_syscore_shutdown,
2066 .resume = mce_syscore_resume,
f3c6ea1b
RW
2067};
2068
c7cece89 2069/*
8a25a2fd 2070 * mce_device: Sysfs support
c7cece89
HS
2071 */
2072
52d168e2
AK
2073static void mce_cpu_restart(void *data)
2074{
7b543a53 2075 if (!mce_available(__this_cpu_ptr(&cpu_info)))
33edbf02 2076 return;
5e09954a
BP
2077 __mcheck_cpu_init_generic();
2078 __mcheck_cpu_init_timer();
52d168e2
AK
2079}
2080
1da177e4 2081/* Reinit MCEs after user configuration changes */
d88203d1
TG
2082static void mce_restart(void)
2083{
9aaef96f 2084 mce_timer_delete_all();
52d168e2 2085 on_each_cpu(mce_cpu_restart, NULL, 1);
1da177e4
LT
2086}
2087
9af43b54 2088/* Toggle features for corrected errors */
9aaef96f 2089static void mce_disable_cmci(void *data)
9af43b54 2090{
7b543a53 2091 if (!mce_available(__this_cpu_ptr(&cpu_info)))
9af43b54 2092 return;
9af43b54
HS
2093 cmci_clear();
2094}
2095
2096static void mce_enable_ce(void *all)
2097{
7b543a53 2098 if (!mce_available(__this_cpu_ptr(&cpu_info)))
9af43b54
HS
2099 return;
2100 cmci_reenable();
2101 cmci_recheck();
2102 if (all)
5e09954a 2103 __mcheck_cpu_init_timer();
9af43b54
HS
2104}
2105
8a25a2fd 2106static struct bus_type mce_subsys = {
e9eee03e 2107 .name = "machinecheck",
8a25a2fd 2108 .dev_name = "machinecheck",
1da177e4
LT
2109};
2110
d6126ef5 2111DEFINE_PER_CPU(struct device *, mce_device);
e9eee03e 2112
e9eee03e 2113void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
1da177e4 2114
8a25a2fd 2115static inline struct mce_bank *attr_to_bank(struct device_attribute *attr)
cebe1820
AK
2116{
2117 return container_of(attr, struct mce_bank, attr);
2118}
0d7482e3 2119
8a25a2fd 2120static ssize_t show_bank(struct device *s, struct device_attribute *attr,
0d7482e3
AK
2121 char *buf)
2122{
cebe1820 2123 return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
0d7482e3
AK
2124}
2125
8a25a2fd 2126static ssize_t set_bank(struct device *s, struct device_attribute *attr,
9319cec8 2127 const char *buf, size_t size)
0d7482e3 2128{
9319cec8 2129 u64 new;
e9eee03e 2130
9319cec8 2131 if (strict_strtoull(buf, 0, &new) < 0)
0d7482e3 2132 return -EINVAL;
e9eee03e 2133
cebe1820 2134 attr_to_bank(attr)->ctl = new;
0d7482e3 2135 mce_restart();
e9eee03e 2136
9319cec8 2137 return size;
0d7482e3 2138}
a98f0dd3 2139
e9eee03e 2140static ssize_t
8a25a2fd 2141show_trigger(struct device *s, struct device_attribute *attr, char *buf)
a98f0dd3 2142{
1020bcbc 2143 strcpy(buf, mce_helper);
a98f0dd3 2144 strcat(buf, "\n");
1020bcbc 2145 return strlen(mce_helper) + 1;
a98f0dd3
AK
2146}
2147
8a25a2fd 2148static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
e9eee03e 2149 const char *buf, size_t siz)
a98f0dd3
AK
2150{
2151 char *p;
e9eee03e 2152
1020bcbc
HS
2153 strncpy(mce_helper, buf, sizeof(mce_helper));
2154 mce_helper[sizeof(mce_helper)-1] = 0;
1020bcbc 2155 p = strchr(mce_helper, '\n');
e9eee03e 2156
e9084ec9 2157 if (p)
e9eee03e
IM
2158 *p = 0;
2159
e9084ec9 2160 return strlen(mce_helper) + !!p;
a98f0dd3
AK
2161}
2162
8a25a2fd
KS
2163static ssize_t set_ignore_ce(struct device *s,
2164 struct device_attribute *attr,
9af43b54
HS
2165 const char *buf, size_t size)
2166{
2167 u64 new;
2168
2169 if (strict_strtoull(buf, 0, &new) < 0)
2170 return -EINVAL;
2171
7af19e4a 2172 if (mca_cfg.ignore_ce ^ !!new) {
9af43b54
HS
2173 if (new) {
2174 /* disable ce features */
9aaef96f
HS
2175 mce_timer_delete_all();
2176 on_each_cpu(mce_disable_cmci, NULL, 1);
7af19e4a 2177 mca_cfg.ignore_ce = true;
9af43b54
HS
2178 } else {
2179 /* enable ce features */
7af19e4a 2180 mca_cfg.ignore_ce = false;
9af43b54
HS
2181 on_each_cpu(mce_enable_ce, (void *)1, 1);
2182 }
2183 }
2184 return size;
2185}
2186
8a25a2fd
KS
2187static ssize_t set_cmci_disabled(struct device *s,
2188 struct device_attribute *attr,
9af43b54
HS
2189 const char *buf, size_t size)
2190{
2191 u64 new;
2192
2193 if (strict_strtoull(buf, 0, &new) < 0)
2194 return -EINVAL;
2195
7af19e4a 2196 if (mca_cfg.cmci_disabled ^ !!new) {
9af43b54
HS
2197 if (new) {
2198 /* disable cmci */
9aaef96f 2199 on_each_cpu(mce_disable_cmci, NULL, 1);
7af19e4a 2200 mca_cfg.cmci_disabled = true;
9af43b54
HS
2201 } else {
2202 /* enable cmci */
7af19e4a 2203 mca_cfg.cmci_disabled = false;
9af43b54
HS
2204 on_each_cpu(mce_enable_ce, NULL, 1);
2205 }
2206 }
2207 return size;
2208}
2209
8a25a2fd
KS
2210static ssize_t store_int_with_restart(struct device *s,
2211 struct device_attribute *attr,
b56f642d
AK
2212 const char *buf, size_t size)
2213{
8a25a2fd 2214 ssize_t ret = device_store_int(s, attr, buf, size);
b56f642d
AK
2215 mce_restart();
2216 return ret;
2217}
2218
8a25a2fd 2219static DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
d203f0b8 2220static DEVICE_INT_ATTR(tolerant, 0644, mca_cfg.tolerant);
84c2559d 2221static DEVICE_INT_ATTR(monarch_timeout, 0644, mca_cfg.monarch_timeout);
d203f0b8 2222static DEVICE_BOOL_ATTR(dont_log_ce, 0644, mca_cfg.dont_log_ce);
e9eee03e 2223
8a25a2fd
KS
2224static struct dev_ext_attribute dev_attr_check_interval = {
2225 __ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
b56f642d
AK
2226 &check_interval
2227};
e9eee03e 2228
8a25a2fd 2229static struct dev_ext_attribute dev_attr_ignore_ce = {
7af19e4a
BP
2230 __ATTR(ignore_ce, 0644, device_show_bool, set_ignore_ce),
2231 &mca_cfg.ignore_ce
9af43b54
HS
2232};
2233
8a25a2fd 2234static struct dev_ext_attribute dev_attr_cmci_disabled = {
7af19e4a
BP
2235 __ATTR(cmci_disabled, 0644, device_show_bool, set_cmci_disabled),
2236 &mca_cfg.cmci_disabled
9af43b54
HS
2237};
2238
8a25a2fd
KS
2239static struct device_attribute *mce_device_attrs[] = {
2240 &dev_attr_tolerant.attr,
2241 &dev_attr_check_interval.attr,
2242 &dev_attr_trigger,
2243 &dev_attr_monarch_timeout.attr,
2244 &dev_attr_dont_log_ce.attr,
2245 &dev_attr_ignore_ce.attr,
2246 &dev_attr_cmci_disabled.attr,
a98f0dd3
AK
2247 NULL
2248};
1da177e4 2249
8a25a2fd 2250static cpumask_var_t mce_device_initialized;
bae19fe0 2251
e032d807
GKH
2252static void mce_device_release(struct device *dev)
2253{
2254 kfree(dev);
2255}
2256
8a25a2fd 2257/* Per cpu device init. All of the cpus still share the same ctrl bank: */
148f9bb8 2258static int mce_device_create(unsigned int cpu)
1da177e4 2259{
e032d807 2260 struct device *dev;
1da177e4 2261 int err;
b1f49f95 2262 int i, j;
92cb7612 2263
90367556 2264 if (!mce_available(&boot_cpu_data))
91c6d400
AK
2265 return -EIO;
2266
e032d807
GKH
2267 dev = kzalloc(sizeof *dev, GFP_KERNEL);
2268 if (!dev)
2269 return -ENOMEM;
8a25a2fd
KS
2270 dev->id = cpu;
2271 dev->bus = &mce_subsys;
e032d807 2272 dev->release = &mce_device_release;
91c6d400 2273
8a25a2fd 2274 err = device_register(dev);
853d9b18
LK
2275 if (err) {
2276 put_device(dev);
d435d862 2277 return err;
853d9b18 2278 }
d435d862 2279
8a25a2fd
KS
2280 for (i = 0; mce_device_attrs[i]; i++) {
2281 err = device_create_file(dev, mce_device_attrs[i]);
d435d862
AM
2282 if (err)
2283 goto error;
2284 }
d203f0b8 2285 for (j = 0; j < mca_cfg.banks; j++) {
8a25a2fd 2286 err = device_create_file(dev, &mce_banks[j].attr);
0d7482e3
AK
2287 if (err)
2288 goto error2;
2289 }
8a25a2fd 2290 cpumask_set_cpu(cpu, mce_device_initialized);
d6126ef5 2291 per_cpu(mce_device, cpu) = dev;
91c6d400 2292
d435d862 2293 return 0;
0d7482e3 2294error2:
b1f49f95 2295 while (--j >= 0)
8a25a2fd 2296 device_remove_file(dev, &mce_banks[j].attr);
d435d862 2297error:
cb491fca 2298 while (--i >= 0)
8a25a2fd 2299 device_remove_file(dev, mce_device_attrs[i]);
cb491fca 2300
8a25a2fd 2301 device_unregister(dev);
d435d862 2302
91c6d400
AK
2303 return err;
2304}
2305
148f9bb8 2306static void mce_device_remove(unsigned int cpu)
91c6d400 2307{
d6126ef5 2308 struct device *dev = per_cpu(mce_device, cpu);
73ca5358
SL
2309 int i;
2310
8a25a2fd 2311 if (!cpumask_test_cpu(cpu, mce_device_initialized))
bae19fe0
AH
2312 return;
2313
8a25a2fd
KS
2314 for (i = 0; mce_device_attrs[i]; i++)
2315 device_remove_file(dev, mce_device_attrs[i]);
cb491fca 2316
d203f0b8 2317 for (i = 0; i < mca_cfg.banks; i++)
8a25a2fd 2318 device_remove_file(dev, &mce_banks[i].attr);
cb491fca 2319
8a25a2fd
KS
2320 device_unregister(dev);
2321 cpumask_clear_cpu(cpu, mce_device_initialized);
d6126ef5 2322 per_cpu(mce_device, cpu) = NULL;
91c6d400 2323}
91c6d400 2324
d6b75584 2325/* Make sure there are no machine checks on offlined CPUs. */
148f9bb8 2326static void mce_disable_cpu(void *h)
d6b75584 2327{
88ccbedd 2328 unsigned long action = *(unsigned long *)h;
cb491fca 2329 int i;
d6b75584 2330
7b543a53 2331 if (!mce_available(__this_cpu_ptr(&cpu_info)))
d6b75584 2332 return;
767df1bd 2333
88ccbedd
AK
2334 if (!(action & CPU_TASKS_FROZEN))
2335 cmci_clear();
d203f0b8 2336 for (i = 0; i < mca_cfg.banks; i++) {
cebe1820 2337 struct mce_bank *b = &mce_banks[i];
11868a2d 2338
cebe1820 2339 if (b->init)
a2d32bcb 2340 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
06b7a7a5 2341 }
d6b75584
AK
2342}
2343
148f9bb8 2344static void mce_reenable_cpu(void *h)
d6b75584 2345{
88ccbedd 2346 unsigned long action = *(unsigned long *)h;
e9eee03e 2347 int i;
d6b75584 2348
7b543a53 2349 if (!mce_available(__this_cpu_ptr(&cpu_info)))
d6b75584 2350 return;
e9eee03e 2351
88ccbedd
AK
2352 if (!(action & CPU_TASKS_FROZEN))
2353 cmci_reenable();
d203f0b8 2354 for (i = 0; i < mca_cfg.banks; i++) {
cebe1820 2355 struct mce_bank *b = &mce_banks[i];
11868a2d 2356
cebe1820 2357 if (b->init)
a2d32bcb 2358 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
06b7a7a5 2359 }
d6b75584
AK
2360}
2361
91c6d400 2362/* Get notified when a cpu comes on/off. Be hotplug friendly. */
148f9bb8 2363static int
e9eee03e 2364mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
91c6d400
AK
2365{
2366 unsigned int cpu = (unsigned long)hcpu;
52d168e2 2367 struct timer_list *t = &per_cpu(mce_timer, cpu);
91c6d400 2368
1a65f970 2369 switch (action & ~CPU_TASKS_FROZEN) {
bae19fe0 2370 case CPU_ONLINE:
8a25a2fd 2371 mce_device_create(cpu);
8735728e
RW
2372 if (threshold_cpu_callback)
2373 threshold_cpu_callback(action, cpu);
91c6d400 2374 break;
91c6d400 2375 case CPU_DEAD:
8735728e
RW
2376 if (threshold_cpu_callback)
2377 threshold_cpu_callback(action, cpu);
8a25a2fd 2378 mce_device_remove(cpu);
55babd8f 2379 mce_intel_hcpu_update(cpu);
91c6d400 2380 break;
52d168e2 2381 case CPU_DOWN_PREPARE:
88ccbedd 2382 smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
55babd8f 2383 del_timer_sync(t);
52d168e2
AK
2384 break;
2385 case CPU_DOWN_FAILED:
88ccbedd 2386 smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
26c3c283 2387 mce_start_timer(cpu, t);
88ccbedd 2388 break;
1a65f970
TG
2389 }
2390
2391 if (action == CPU_POST_DEAD) {
88ccbedd 2392 /* intentionally ignoring frozen here */
7a0c819d 2393 cmci_rediscover();
91c6d400 2394 }
1a65f970 2395
bae19fe0 2396 return NOTIFY_OK;
91c6d400
AK
2397}
2398
148f9bb8 2399static struct notifier_block mce_cpu_notifier = {
91c6d400
AK
2400 .notifier_call = mce_cpu_callback,
2401};
2402
cebe1820 2403static __init void mce_init_banks(void)
0d7482e3
AK
2404{
2405 int i;
2406
d203f0b8 2407 for (i = 0; i < mca_cfg.banks; i++) {
cebe1820 2408 struct mce_bank *b = &mce_banks[i];
8a25a2fd 2409 struct device_attribute *a = &b->attr;
e9eee03e 2410
a07e4156 2411 sysfs_attr_init(&a->attr);
cebe1820
AK
2412 a->attr.name = b->attrname;
2413 snprintf(b->attrname, ATTR_LEN, "bank%d", i);
e9eee03e
IM
2414
2415 a->attr.mode = 0644;
2416 a->show = show_bank;
2417 a->store = set_bank;
0d7482e3 2418 }
0d7482e3
AK
2419}
2420
5e09954a 2421static __init int mcheck_init_device(void)
91c6d400
AK
2422{
2423 int err;
2424 int i = 0;
2425
1da177e4
LT
2426 if (!mce_available(&boot_cpu_data))
2427 return -EIO;
0d7482e3 2428
8a25a2fd 2429 zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL);
996867d0 2430
cebe1820 2431 mce_init_banks();
0d7482e3 2432
8a25a2fd 2433 err = subsys_system_register(&mce_subsys, NULL);
d435d862
AM
2434 if (err)
2435 return err;
91c6d400
AK
2436
2437 for_each_online_cpu(i) {
8a25a2fd 2438 err = mce_device_create(i);
d435d862
AM
2439 if (err)
2440 return err;
91c6d400
AK
2441 }
2442
f3c6ea1b 2443 register_syscore_ops(&mce_syscore_ops);
be6b5a35 2444 register_hotcpu_notifier(&mce_cpu_notifier);
93b62c3c
HS
2445
2446 /* register character device /dev/mcelog */
2447 misc_register(&mce_chrdev_device);
e9eee03e 2448
1da177e4 2449 return err;
1da177e4 2450}
cef12ee5 2451device_initcall_sync(mcheck_init_device);
a988d334 2452
d7c3c9a6
AK
2453/*
2454 * Old style boot options parsing. Only for compatibility.
2455 */
2456static int __init mcheck_disable(char *str)
2457{
1462594b 2458 mca_cfg.disabled = true;
d7c3c9a6
AK
2459 return 1;
2460}
2461__setup("nomce", mcheck_disable);
a988d334 2462
5be9ed25
HY
2463#ifdef CONFIG_DEBUG_FS
2464struct dentry *mce_get_debugfs_dir(void)
a988d334 2465{
5be9ed25 2466 static struct dentry *dmce;
a988d334 2467
5be9ed25
HY
2468 if (!dmce)
2469 dmce = debugfs_create_dir("mce", NULL);
a988d334 2470
5be9ed25
HY
2471 return dmce;
2472}
a988d334 2473
bf783f9f
HY
2474static void mce_reset(void)
2475{
2476 cpu_missing = 0;
2477 atomic_set(&mce_fake_paniced, 0);
2478 atomic_set(&mce_executing, 0);
2479 atomic_set(&mce_callin, 0);
2480 atomic_set(&global_nwo, 0);
2481}
a988d334 2482
bf783f9f
HY
2483static int fake_panic_get(void *data, u64 *val)
2484{
2485 *val = fake_panic;
2486 return 0;
a988d334
IM
2487}
2488
bf783f9f 2489static int fake_panic_set(void *data, u64 val)
a988d334 2490{
bf783f9f
HY
2491 mce_reset();
2492 fake_panic = val;
2493 return 0;
a988d334 2494}
a988d334 2495
bf783f9f
HY
2496DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
2497 fake_panic_set, "%llu\n");
d7c3c9a6 2498
5e09954a 2499static int __init mcheck_debugfs_init(void)
d7c3c9a6 2500{
bf783f9f
HY
2501 struct dentry *dmce, *ffake_panic;
2502
2503 dmce = mce_get_debugfs_dir();
2504 if (!dmce)
2505 return -ENOMEM;
2506 ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
2507 &fake_panic_fops);
2508 if (!ffake_panic)
2509 return -ENOMEM;
2510
2511 return 0;
d7c3c9a6 2512}
5e09954a 2513late_initcall(mcheck_debugfs_init);
5be9ed25 2514#endif
This page took 0.916108 seconds and 5 git commands to generate.