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