x86, mce, severity: Extend the the mce_severity mechanism to handle UCNA/DEFERRED...
[linux-2.6-block.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
01ca79f1
AK
63DEFINE_PER_CPU(unsigned, mce_exception_count);
64
1462594b 65struct mce_bank *mce_banks __read_mostly;
cebe1820 66
d203f0b8 67struct mca_config mca_cfg __read_mostly = {
84c2559d 68 .bootlog = -1,
d203f0b8
BP
69 /*
70 * Tolerant levels:
71 * 0: always panic on uncorrected errors, log corrected errors
72 * 1: panic or SIGBUS on uncorrected errors, log corrected errors
73 * 2: SIGBUS or log uncorrected errors (if possible), log corr. errors
74 * 3: never panic or SIGBUS, log all errors (for testing only)
75 */
84c2559d
BP
76 .tolerant = 1,
77 .monarch_timeout = -1
d203f0b8
BP
78};
79
1020bcbc
HS
80/* User mode helper program triggered by machine check event */
81static unsigned long mce_need_notify;
82static char mce_helper[128];
83static char *mce_helper_argv[2] = { mce_helper, NULL };
1da177e4 84
93b62c3c
HS
85static DECLARE_WAIT_QUEUE_HEAD(mce_chrdev_wait);
86
3c079792
AK
87static DEFINE_PER_CPU(struct mce, mces_seen);
88static int cpu_missing;
89
27f6c573
CG
90/* CMCI storm detection filter */
91static DEFINE_PER_CPU(unsigned long, mce_polled_error);
92
0644414e
NR
93/*
94 * MCA banks polled by the period polling timer for corrected events.
95 * With Intel CMCI, this only has MCA banks which do not support CMCI (if any).
96 */
ee031c31
AK
97DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
98 [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
99};
100
c3d1fb56
NR
101/*
102 * MCA banks controlled through firmware first for corrected errors.
103 * This is a global list of banks for which we won't enable CMCI and we
104 * won't poll. Firmware controls these banks and is responsible for
105 * reporting corrected errors through GHES. Uncorrected/recoverable
106 * errors are still notified through a machine check.
107 */
108mce_banks_t mce_banks_ce_disabled;
109
9b1beaf2
AK
110static DEFINE_PER_CPU(struct work_struct, mce_work);
111
61b0fccd
TL
112static void (*quirk_no_way_out)(int bank, struct mce *m, struct pt_regs *regs);
113
3653ada5
BP
114/*
115 * CPU/chipset specific EDAC code can register a notifier call here to print
116 * MCE errors in a human-readable form.
117 */
118ATOMIC_NOTIFIER_HEAD(x86_mce_decoder_chain);
119
b5f2fa4e
AK
120/* Do initial initialization of a struct mce */
121void mce_setup(struct mce *m)
122{
123 memset(m, 0, sizeof(struct mce));
d620c67f 124 m->cpu = m->extcpu = smp_processor_id();
b5f2fa4e 125 rdtscll(m->tsc);
8ee08347
AK
126 /* We hope get_seconds stays lockless */
127 m->time = get_seconds();
128 m->cpuvendor = boot_cpu_data.x86_vendor;
129 m->cpuid = cpuid_eax(1);
8ee08347 130 m->socketid = cpu_data(m->extcpu).phys_proc_id;
8ee08347
AK
131 m->apicid = cpu_data(m->extcpu).initial_apicid;
132 rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
b5f2fa4e
AK
133}
134
ea149b36
AK
135DEFINE_PER_CPU(struct mce, injectm);
136EXPORT_PER_CPU_SYMBOL_GPL(injectm);
137
1da177e4
LT
138/*
139 * Lockless MCE logging infrastructure.
140 * This avoids deadlocks on printk locks without having to break locks. Also
141 * separate MCEs from kernel messages to avoid bogus bug reports.
142 */
143
231fd906 144static struct mce_log mcelog = {
f6fb0ac0
AK
145 .signature = MCE_LOG_SIGNATURE,
146 .len = MCE_LOG_LEN,
147 .recordlen = sizeof(struct mce),
d88203d1 148};
1da177e4
LT
149
150void mce_log(struct mce *mce)
151{
152 unsigned next, entry;
f0cb5452 153 int ret = 0;
e9eee03e 154
8968f9d3
HS
155 /* Emit the trace record: */
156 trace_mce_record(mce);
157
f0cb5452
BP
158 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, mce);
159 if (ret == NOTIFY_STOP)
160 return;
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
295static atomic_t mce_paniced;
296
bf783f9f
HY
297static int fake_panic;
298static atomic_t mce_fake_paniced;
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
bd19a5e6 314static void mce_panic(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 */
322 if (atomic_inc_return(&mce_paniced) > 1)
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 */
330 if (atomic_inc_return(&mce_fake_paniced) > 1)
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
b77e70bf
HS
532DEFINE_PER_CPU(struct irq_work, mce_irq_work);
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
ca84f696
AK
578DEFINE_PER_CPU(unsigned, mce_poll_count);
579
d88203d1 580/*
b79109c3
AK
581 * Poll for corrected events or events that happened before reset.
582 * Those are just logged through /dev/mcelog.
583 *
584 * This is executed in standard interrupt context.
ed7290d0
AK
585 *
586 * Note: spec recommends to panic for fatal unsignalled
587 * errors here. However this would be quite problematic --
588 * we would need to reimplement the Monarch handling and
589 * it would mess up the exclusion between exception handler
590 * and poll hander -- * so we skip this for now.
591 * These cases should not happen anyways, or only when the CPU
592 * is already totally * confused. In this case it's likely it will
593 * not fully execute the machine check handler either.
b79109c3 594 */
ee031c31 595void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
b79109c3
AK
596{
597 struct mce m;
598 int i;
599
c6ae41e7 600 this_cpu_inc(mce_poll_count);
ca84f696 601
b8325c5b 602 mce_gather_info(&m, NULL);
b79109c3 603
d203f0b8 604 for (i = 0; i < mca_cfg.banks; i++) {
cebe1820 605 if (!mce_banks[i].ctl || !test_bit(i, *b))
b79109c3
AK
606 continue;
607
608 m.misc = 0;
609 m.addr = 0;
610 m.bank = i;
611 m.tsc = 0;
612
613 barrier();
a2d32bcb 614 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
b79109c3
AK
615 if (!(m.status & MCI_STATUS_VAL))
616 continue;
617
ea431643 618 this_cpu_write(mce_polled_error, 1);
b79109c3 619 /*
ed7290d0
AK
620 * Uncorrected or signalled events are handled by the exception
621 * handler when it is enabled, so don't process those here.
b79109c3
AK
622 *
623 * TBD do the same check for MCI_STATUS_EN here?
624 */
ed7290d0 625 if (!(flags & MCP_UC) &&
1462594b 626 (m.status & (mca_cfg.ser ? MCI_STATUS_S : MCI_STATUS_UC)))
b79109c3
AK
627 continue;
628
85f92694 629 mce_read_aux(&m, i);
b79109c3
AK
630
631 if (!(flags & MCP_TIMESTAMP))
632 m.tsc = 0;
633 /*
634 * Don't get the IP here because it's unlikely to
635 * have anything to do with the actual error location.
636 */
d203f0b8 637 if (!(flags & MCP_DONTLOG) && !mca_cfg.dont_log_ce)
5679af4c 638 mce_log(&m);
b79109c3
AK
639
640 /*
641 * Clear state for this bank.
642 */
a2d32bcb 643 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
b79109c3
AK
644 }
645
646 /*
647 * Don't clear MCG_STATUS here because it's only defined for
648 * exceptions.
649 */
88921be3
AK
650
651 sync_core();
b79109c3 652}
ea149b36 653EXPORT_SYMBOL_GPL(machine_check_poll);
b79109c3 654
bd19a5e6
AK
655/*
656 * Do a quick check if any of the events requires a panic.
657 * This decides if we keep the events around or clear them.
658 */
61b0fccd
TL
659static int mce_no_way_out(struct mce *m, char **msg, unsigned long *validp,
660 struct pt_regs *regs)
bd19a5e6 661{
95022b8c 662 int i, ret = 0;
bd19a5e6 663
d203f0b8 664 for (i = 0; i < mca_cfg.banks; i++) {
a2d32bcb 665 m->status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
61b0fccd 666 if (m->status & MCI_STATUS_VAL) {
95022b8c 667 __set_bit(i, validp);
61b0fccd
TL
668 if (quirk_no_way_out)
669 quirk_no_way_out(i, m, regs);
670 }
e3480271
CY
671 if (mce_severity(m, mca_cfg.tolerant, msg, true) >=
672 MCE_PANIC_SEVERITY)
95022b8c 673 ret = 1;
bd19a5e6 674 }
95022b8c 675 return ret;
bd19a5e6
AK
676}
677
3c079792
AK
678/*
679 * Variable to establish order between CPUs while scanning.
680 * Each CPU spins initially until executing is equal its number.
681 */
682static atomic_t mce_executing;
683
684/*
685 * Defines order of CPUs on entry. First CPU becomes Monarch.
686 */
687static atomic_t mce_callin;
688
689/*
690 * Check if a timeout waiting for other CPUs happened.
691 */
692static int mce_timed_out(u64 *t)
693{
694 /*
695 * The others already did panic for some reason.
696 * Bail out like in a timeout.
697 * rmb() to tell the compiler that system_state
698 * might have been modified by someone else.
699 */
700 rmb();
701 if (atomic_read(&mce_paniced))
702 wait_for_panic();
84c2559d 703 if (!mca_cfg.monarch_timeout)
3c079792
AK
704 goto out;
705 if ((s64)*t < SPINUNIT) {
716079f6 706 if (mca_cfg.tolerant <= 1)
3c079792
AK
707 mce_panic("Timeout synchronizing machine check over CPUs",
708 NULL, NULL);
709 cpu_missing = 1;
710 return 1;
711 }
712 *t -= SPINUNIT;
713out:
714 touch_nmi_watchdog();
715 return 0;
716}
717
718/*
719 * The Monarch's reign. The Monarch is the CPU who entered
720 * the machine check handler first. It waits for the others to
721 * raise the exception too and then grades them. When any
722 * error is fatal panic. Only then let the others continue.
723 *
724 * The other CPUs entering the MCE handler will be controlled by the
725 * Monarch. They are called Subjects.
726 *
727 * This way we prevent any potential data corruption in a unrecoverable case
728 * and also makes sure always all CPU's errors are examined.
729 *
680b6cfd 730 * Also this detects the case of a machine check event coming from outer
3c079792
AK
731 * space (not detected by any CPUs) In this case some external agent wants
732 * us to shut down, so panic too.
733 *
734 * The other CPUs might still decide to panic if the handler happens
735 * in a unrecoverable place, but in this case the system is in a semi-stable
736 * state and won't corrupt anything by itself. It's ok to let the others
737 * continue for a bit first.
738 *
739 * All the spin loops have timeouts; when a timeout happens a CPU
740 * typically elects itself to be Monarch.
741 */
742static void mce_reign(void)
743{
744 int cpu;
745 struct mce *m = NULL;
746 int global_worst = 0;
747 char *msg = NULL;
748 char *nmsg = NULL;
749
750 /*
751 * This CPU is the Monarch and the other CPUs have run
752 * through their handlers.
753 * Grade the severity of the errors of all the CPUs.
754 */
755 for_each_possible_cpu(cpu) {
d203f0b8
BP
756 int severity = mce_severity(&per_cpu(mces_seen, cpu),
757 mca_cfg.tolerant,
e3480271 758 &nmsg, true);
3c079792
AK
759 if (severity > global_worst) {
760 msg = nmsg;
761 global_worst = severity;
762 m = &per_cpu(mces_seen, cpu);
763 }
764 }
765
766 /*
767 * Cannot recover? Panic here then.
768 * This dumps all the mces in the log buffer and stops the
769 * other CPUs.
770 */
d203f0b8 771 if (m && global_worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3)
ac960375 772 mce_panic("Fatal Machine check", m, msg);
3c079792
AK
773
774 /*
775 * For UC somewhere we let the CPU who detects it handle it.
776 * Also must let continue the others, otherwise the handling
777 * CPU could deadlock on a lock.
778 */
779
780 /*
781 * No machine check event found. Must be some external
782 * source or one CPU is hung. Panic.
783 */
d203f0b8 784 if (global_worst <= MCE_KEEP_SEVERITY && mca_cfg.tolerant < 3)
3c079792
AK
785 mce_panic("Machine check from unknown source", NULL, NULL);
786
787 /*
788 * Now clear all the mces_seen so that they don't reappear on
789 * the next mce.
790 */
791 for_each_possible_cpu(cpu)
792 memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
793}
794
795static atomic_t global_nwo;
796
797/*
798 * Start of Monarch synchronization. This waits until all CPUs have
799 * entered the exception handler and then determines if any of them
800 * saw a fatal event that requires panic. Then it executes them
801 * in the entry order.
802 * TBD double check parallel CPU hotunplug
803 */
7fb06fc9 804static int mce_start(int *no_way_out)
3c079792 805{
7fb06fc9 806 int order;
3c079792 807 int cpus = num_online_cpus();
84c2559d 808 u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
3c079792 809
7fb06fc9
HS
810 if (!timeout)
811 return -1;
3c079792 812
7fb06fc9 813 atomic_add(*no_way_out, &global_nwo);
184e1fdf
HY
814 /*
815 * global_nwo should be updated before mce_callin
816 */
817 smp_wmb();
a95436e4 818 order = atomic_inc_return(&mce_callin);
3c079792
AK
819
820 /*
821 * Wait for everyone.
822 */
823 while (atomic_read(&mce_callin) != cpus) {
824 if (mce_timed_out(&timeout)) {
825 atomic_set(&global_nwo, 0);
7fb06fc9 826 return -1;
3c079792
AK
827 }
828 ndelay(SPINUNIT);
829 }
830
184e1fdf
HY
831 /*
832 * mce_callin should be read before global_nwo
833 */
834 smp_rmb();
3c079792 835
7fb06fc9
HS
836 if (order == 1) {
837 /*
838 * Monarch: Starts executing now, the others wait.
839 */
3c079792 840 atomic_set(&mce_executing, 1);
7fb06fc9
HS
841 } else {
842 /*
843 * Subject: Now start the scanning loop one by one in
844 * the original callin order.
845 * This way when there are any shared banks it will be
846 * only seen by one CPU before cleared, avoiding duplicates.
847 */
848 while (atomic_read(&mce_executing) < order) {
849 if (mce_timed_out(&timeout)) {
850 atomic_set(&global_nwo, 0);
851 return -1;
852 }
853 ndelay(SPINUNIT);
854 }
3c079792
AK
855 }
856
857 /*
7fb06fc9 858 * Cache the global no_way_out state.
3c079792 859 */
7fb06fc9
HS
860 *no_way_out = atomic_read(&global_nwo);
861
862 return order;
3c079792
AK
863}
864
865/*
866 * Synchronize between CPUs after main scanning loop.
867 * This invokes the bulk of the Monarch processing.
868 */
869static int mce_end(int order)
870{
871 int ret = -1;
84c2559d 872 u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
3c079792
AK
873
874 if (!timeout)
875 goto reset;
876 if (order < 0)
877 goto reset;
878
879 /*
880 * Allow others to run.
881 */
882 atomic_inc(&mce_executing);
883
884 if (order == 1) {
885 /* CHECKME: Can this race with a parallel hotplug? */
886 int cpus = num_online_cpus();
887
888 /*
889 * Monarch: Wait for everyone to go through their scanning
890 * loops.
891 */
892 while (atomic_read(&mce_executing) <= cpus) {
893 if (mce_timed_out(&timeout))
894 goto reset;
895 ndelay(SPINUNIT);
896 }
897
898 mce_reign();
899 barrier();
900 ret = 0;
901 } else {
902 /*
903 * Subject: Wait for Monarch to finish.
904 */
905 while (atomic_read(&mce_executing) != 0) {
906 if (mce_timed_out(&timeout))
907 goto reset;
908 ndelay(SPINUNIT);
909 }
910
911 /*
912 * Don't reset anything. That's done by the Monarch.
913 */
914 return 0;
915 }
916
917 /*
918 * Reset all global state.
919 */
920reset:
921 atomic_set(&global_nwo, 0);
922 atomic_set(&mce_callin, 0);
923 barrier();
924
925 /*
926 * Let others run again.
927 */
928 atomic_set(&mce_executing, 0);
929 return ret;
930}
931
9b1beaf2
AK
932/*
933 * Check if the address reported by the CPU is in a format we can parse.
934 * It would be possible to add code for most other cases, but all would
935 * be somewhat complicated (e.g. segment offset would require an instruction
0d2eb44f 936 * parser). So only support physical addresses up to page granuality for now.
9b1beaf2
AK
937 */
938static int mce_usable_address(struct mce *m)
939{
940 if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
941 return 0;
2b90e77e 942 if (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
9b1beaf2 943 return 0;
2b90e77e 944 if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_ADDR_PHYS)
9b1beaf2
AK
945 return 0;
946 return 1;
947}
948
3c079792
AK
949static void mce_clear_state(unsigned long *toclear)
950{
951 int i;
952
d203f0b8 953 for (i = 0; i < mca_cfg.banks; i++) {
3c079792 954 if (test_bit(i, toclear))
a2d32bcb 955 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
3c079792
AK
956 }
957}
958
af104e39
TL
959/*
960 * Need to save faulting physical address associated with a process
961 * in the machine check handler some place where we can grab it back
962 * later in mce_notify_process()
963 */
964#define MCE_INFO_MAX 16
965
966struct mce_info {
967 atomic_t inuse;
968 struct task_struct *t;
969 __u64 paddr;
dad1743e 970 int restartable;
af104e39
TL
971} mce_info[MCE_INFO_MAX];
972
dad1743e 973static void mce_save_info(__u64 addr, int c)
af104e39
TL
974{
975 struct mce_info *mi;
976
977 for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++) {
978 if (atomic_cmpxchg(&mi->inuse, 0, 1) == 0) {
979 mi->t = current;
980 mi->paddr = addr;
dad1743e 981 mi->restartable = c;
af104e39
TL
982 return;
983 }
984 }
985
986 mce_panic("Too many concurrent recoverable errors", NULL, NULL);
987}
988
989static struct mce_info *mce_find_info(void)
990{
991 struct mce_info *mi;
992
993 for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++)
994 if (atomic_read(&mi->inuse) && mi->t == current)
995 return mi;
996 return NULL;
997}
998
999static void mce_clear_info(struct mce_info *mi)
1000{
1001 atomic_set(&mi->inuse, 0);
1002}
1003
b79109c3
AK
1004/*
1005 * The actual machine check handler. This only handles real
1006 * exceptions when something got corrupted coming in through int 18.
1007 *
1008 * This is executed in NMI context not subject to normal locking rules. This
1009 * implies that most kernel services cannot be safely used. Don't even
1010 * think about putting a printk in there!
3c079792
AK
1011 *
1012 * On Intel systems this is entered on all CPUs in parallel through
1013 * MCE broadcast. However some CPUs might be broken beyond repair,
1014 * so be always careful when synchronizing with others.
1da177e4 1015 */
e9eee03e 1016void do_machine_check(struct pt_regs *regs, long error_code)
1da177e4 1017{
1462594b 1018 struct mca_config *cfg = &mca_cfg;
3c079792 1019 struct mce m, *final;
1da177e4 1020 int i;
3c079792
AK
1021 int worst = 0;
1022 int severity;
1023 /*
1024 * Establish sequential order between the CPUs entering the machine
1025 * check handler.
1026 */
7fb06fc9 1027 int order;
bd78432c
TH
1028 /*
1029 * If no_way_out gets set, there is no safe way to recover from this
d203f0b8 1030 * MCE. If mca_cfg.tolerant is cranked up, we'll try anyway.
bd78432c
TH
1031 */
1032 int no_way_out = 0;
1033 /*
1034 * If kill_it gets set, there might be a way to recover from this
1035 * error.
1036 */
1037 int kill_it = 0;
b79109c3 1038 DECLARE_BITMAP(toclear, MAX_NR_BANKS);
95022b8c 1039 DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
bd19a5e6 1040 char *msg = "Unknown";
1da177e4 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
89cbc767 1049 final = this_cpu_ptr(&mces_seen);
3c079792
AK
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
e3480271 1099 severity = mce_severity(&m, cfg->tolerant, NULL, true);
b79109c3 1100
ed7290d0 1101 /*
e3480271
CY
1102 * When machine check was for corrected/deferred handler don't
1103 * touch, unless we're panicing.
ed7290d0 1104 */
e3480271
CY
1105 if ((severity == MCE_KEEP_SEVERITY ||
1106 severity == MCE_UCNA_SEVERITY) && !no_way_out)
ed7290d0
AK
1107 continue;
1108 __set_bit(i, toclear);
1109 if (severity == MCE_NO_SEVERITY) {
b79109c3
AK
1110 /*
1111 * Machine check event was not enabled. Clear, but
1112 * ignore.
1113 */
1114 continue;
1da177e4
LT
1115 }
1116
85f92694 1117 mce_read_aux(&m, i);
1da177e4 1118
9b1beaf2
AK
1119 /*
1120 * Action optional error. Queue address for later processing.
1121 * When the ring overflows we just ignore the AO error.
1122 * RED-PEN add some logging mechanism when
1123 * usable_address or mce_add_ring fails.
d203f0b8 1124 * RED-PEN don't ignore overflow for mca_cfg.tolerant == 0
9b1beaf2
AK
1125 */
1126 if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
1127 mce_ring_add(m.addr >> PAGE_SHIFT);
1128
b79109c3 1129 mce_log(&m);
1da177e4 1130
3c079792
AK
1131 if (severity > worst) {
1132 *final = m;
1133 worst = severity;
1da177e4 1134 }
1da177e4
LT
1135 }
1136
a8c321fb
TL
1137 /* mce_clear_state will clear *final, save locally for use later */
1138 m = *final;
1139
3c079792
AK
1140 if (!no_way_out)
1141 mce_clear_state(toclear);
1142
e9eee03e 1143 /*
3c079792
AK
1144 * Do most of the synchronization with other CPUs.
1145 * When there's any problem use only local no_way_out state.
e9eee03e 1146 */
3c079792
AK
1147 if (mce_end(order) < 0)
1148 no_way_out = worst >= MCE_PANIC_SEVERITY;
bd78432c
TH
1149
1150 /*
a8c321fb
TL
1151 * At insane "tolerant" levels we take no action. Otherwise
1152 * we only die if we have no other choice. For less serious
1153 * issues we try to recover, or limit damage to the current
1154 * process.
bd78432c 1155 */
1462594b 1156 if (cfg->tolerant < 3) {
a8c321fb
TL
1157 if (no_way_out)
1158 mce_panic("Fatal machine check on current CPU", &m, msg);
1159 if (worst == MCE_AR_SEVERITY) {
1160 /* schedule action before return to userland */
dad1743e 1161 mce_save_info(m.addr, m.mcgstatus & MCG_STATUS_RIPV);
a8c321fb
TL
1162 set_thread_flag(TIF_MCE_NOTIFY);
1163 } else if (kill_it) {
1164 force_sig(SIGBUS, current);
1165 }
1166 }
e02e68d3 1167
3c079792
AK
1168 if (worst > 0)
1169 mce_report_event(regs);
5f8c1a54 1170 mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
32561696 1171out:
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
27f6c573
CG
1281static int cmc_error_seen(void)
1282{
89cbc767 1283 unsigned long *v = this_cpu_ptr(&mce_polled_error);
27f6c573
CG
1284
1285 return test_and_clear_bit(0, v);
1286}
1287
82f7af09 1288static void mce_timer_fn(unsigned long data)
1da177e4 1289{
89cbc767 1290 struct timer_list *t = this_cpu_ptr(&mce_timer);
82f7af09 1291 unsigned long iv;
27f6c573 1292 int notify;
52d168e2
AK
1293
1294 WARN_ON(smp_processor_id() != data);
1295
89cbc767 1296 if (mce_available(this_cpu_ptr(&cpu_info))) {
ee031c31 1297 machine_check_poll(MCP_TIMESTAMP,
89cbc767 1298 this_cpu_ptr(&mce_poll_banks));
55babd8f 1299 mce_intel_cmci_poll();
e9eee03e 1300 }
1da177e4
LT
1301
1302 /*
e02e68d3
TH
1303 * Alert userspace if needed. If we logged an MCE, reduce the
1304 * polling interval, otherwise increase the polling interval.
1da177e4 1305 */
82f7af09 1306 iv = __this_cpu_read(mce_next_interval);
27f6c573
CG
1307 notify = mce_notify_irq();
1308 notify |= cmc_error_seen();
1309 if (notify) {
958fb3c5 1310 iv = max(iv / 2, (unsigned long) HZ/100);
55babd8f 1311 } else {
82f7af09 1312 iv = min(iv * 2, round_jiffies_relative(check_interval * HZ));
55babd8f
CG
1313 iv = mce_adjust_timer(iv);
1314 }
82f7af09 1315 __this_cpu_write(mce_next_interval, iv);
55babd8f
CG
1316 /* Might have become 0 after CMCI storm subsided */
1317 if (iv) {
1318 t->expires = jiffies + iv;
1319 add_timer_on(t, smp_processor_id());
1320 }
1321}
e02e68d3 1322
55babd8f
CG
1323/*
1324 * Ensure that the timer is firing in @interval from now.
1325 */
1326void mce_timer_kick(unsigned long interval)
1327{
89cbc767 1328 struct timer_list *t = this_cpu_ptr(&mce_timer);
55babd8f
CG
1329 unsigned long when = jiffies + interval;
1330 unsigned long iv = __this_cpu_read(mce_next_interval);
1331
1332 if (timer_pending(t)) {
1333 if (time_before(when, t->expires))
1334 mod_timer_pinned(t, when);
1335 } else {
1336 t->expires = round_jiffies(when);
1337 add_timer_on(t, smp_processor_id());
1338 }
1339 if (interval < iv)
1340 __this_cpu_write(mce_next_interval, interval);
e02e68d3
TH
1341}
1342
9aaef96f
HS
1343/* Must not be called in IRQ context where del_timer_sync() can deadlock */
1344static void mce_timer_delete_all(void)
1345{
1346 int cpu;
1347
1348 for_each_online_cpu(cpu)
1349 del_timer_sync(&per_cpu(mce_timer, cpu));
1350}
1351
9bd98405
AK
1352static void mce_do_trigger(struct work_struct *work)
1353{
1020bcbc 1354 call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
9bd98405
AK
1355}
1356
1357static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
1358
e02e68d3 1359/*
9bd98405
AK
1360 * Notify the user(s) about new machine check events.
1361 * Can be called from interrupt context, but not from machine check/NMI
1362 * context.
e02e68d3 1363 */
9ff36ee9 1364int mce_notify_irq(void)
e02e68d3 1365{
8457c84d
AK
1366 /* Not more than two messages every minute */
1367 static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1368
1020bcbc 1369 if (test_and_clear_bit(0, &mce_need_notify)) {
93b62c3c
HS
1370 /* wake processes polling /dev/mcelog */
1371 wake_up_interruptible(&mce_chrdev_wait);
9bd98405 1372
4d899be5 1373 if (mce_helper[0])
9bd98405 1374 schedule_work(&mce_trigger_work);
e02e68d3 1375
8457c84d 1376 if (__ratelimit(&ratelimit))
a2d7b0d4 1377 pr_info(HW_ERR "Machine check events logged\n");
e02e68d3
TH
1378
1379 return 1;
1da177e4 1380 }
e02e68d3
TH
1381 return 0;
1382}
9ff36ee9 1383EXPORT_SYMBOL_GPL(mce_notify_irq);
8a336b0a 1384
148f9bb8 1385static int __mcheck_cpu_mce_banks_init(void)
cebe1820
AK
1386{
1387 int i;
d203f0b8 1388 u8 num_banks = mca_cfg.banks;
cebe1820 1389
d203f0b8 1390 mce_banks = kzalloc(num_banks * sizeof(struct mce_bank), GFP_KERNEL);
cebe1820
AK
1391 if (!mce_banks)
1392 return -ENOMEM;
d203f0b8
BP
1393
1394 for (i = 0; i < num_banks; i++) {
cebe1820 1395 struct mce_bank *b = &mce_banks[i];
11868a2d 1396
cebe1820
AK
1397 b->ctl = -1ULL;
1398 b->init = 1;
1399 }
1400 return 0;
1401}
1402
d88203d1 1403/*
1da177e4
LT
1404 * Initialize Machine Checks for a CPU.
1405 */
148f9bb8 1406static int __mcheck_cpu_cap_init(void)
1da177e4 1407{
0d7482e3 1408 unsigned b;
e9eee03e 1409 u64 cap;
1da177e4
LT
1410
1411 rdmsrl(MSR_IA32_MCG_CAP, cap);
01c6680a
TG
1412
1413 b = cap & MCG_BANKCNT_MASK;
d203f0b8 1414 if (!mca_cfg.banks)
c767a54b 1415 pr_info("CPU supports %d MCE banks\n", b);
b659294b 1416
0d7482e3 1417 if (b > MAX_NR_BANKS) {
c767a54b 1418 pr_warn("Using only %u machine check banks out of %u\n",
0d7482e3
AK
1419 MAX_NR_BANKS, b);
1420 b = MAX_NR_BANKS;
1421 }
1422
1423 /* Don't support asymmetric configurations today */
d203f0b8
BP
1424 WARN_ON(mca_cfg.banks != 0 && b != mca_cfg.banks);
1425 mca_cfg.banks = b;
1426
cebe1820 1427 if (!mce_banks) {
cffd377e 1428 int err = __mcheck_cpu_mce_banks_init();
11868a2d 1429
cebe1820
AK
1430 if (err)
1431 return err;
1da177e4 1432 }
0d7482e3 1433
94ad8474 1434 /* Use accurate RIP reporting if available. */
01c6680a 1435 if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
84c2559d 1436 mca_cfg.rip_msr = MSR_IA32_MCG_EIP;
1da177e4 1437
ed7290d0 1438 if (cap & MCG_SER_P)
1462594b 1439 mca_cfg.ser = true;
ed7290d0 1440
0d7482e3
AK
1441 return 0;
1442}
1443
5e09954a 1444static void __mcheck_cpu_init_generic(void)
0d7482e3 1445{
84c2559d 1446 enum mcp_flags m_fl = 0;
e9eee03e 1447 mce_banks_t all_banks;
0d7482e3
AK
1448 u64 cap;
1449 int i;
1450
84c2559d
BP
1451 if (!mca_cfg.bootlog)
1452 m_fl = MCP_DONTLOG;
1453
b79109c3
AK
1454 /*
1455 * Log the machine checks left over from the previous reset.
1456 */
ee031c31 1457 bitmap_fill(all_banks, MAX_NR_BANKS);
84c2559d 1458 machine_check_poll(MCP_UC | m_fl, &all_banks);
1da177e4
LT
1459
1460 set_in_cr4(X86_CR4_MCE);
1461
0d7482e3 1462 rdmsrl(MSR_IA32_MCG_CAP, cap);
1da177e4
LT
1463 if (cap & MCG_CTL_P)
1464 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1465
d203f0b8 1466 for (i = 0; i < mca_cfg.banks; i++) {
cebe1820 1467 struct mce_bank *b = &mce_banks[i];
11868a2d 1468
cebe1820 1469 if (!b->init)
06b7a7a5 1470 continue;
a2d32bcb
AK
1471 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
1472 wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
d88203d1 1473 }
1da177e4
LT
1474}
1475
61b0fccd
TL
1476/*
1477 * During IFU recovery Sandy Bridge -EP4S processors set the RIPV and
1478 * EIPV bits in MCG_STATUS to zero on the affected logical processor (SDM
1479 * Vol 3B Table 15-20). But this confuses both the code that determines
1480 * whether the machine check occurred in kernel or user mode, and also
1481 * the severity assessment code. Pretend that EIPV was set, and take the
1482 * ip/cs values from the pt_regs that mce_gather_info() ignored earlier.
1483 */
1484static void quirk_sandybridge_ifu(int bank, struct mce *m, struct pt_regs *regs)
1485{
1486 if (bank != 0)
1487 return;
1488 if ((m->mcgstatus & (MCG_STATUS_EIPV|MCG_STATUS_RIPV)) != 0)
1489 return;
1490 if ((m->status & (MCI_STATUS_OVER|MCI_STATUS_UC|
1491 MCI_STATUS_EN|MCI_STATUS_MISCV|MCI_STATUS_ADDRV|
1492 MCI_STATUS_PCC|MCI_STATUS_S|MCI_STATUS_AR|
1493 MCACOD)) !=
1494 (MCI_STATUS_UC|MCI_STATUS_EN|
1495 MCI_STATUS_MISCV|MCI_STATUS_ADDRV|MCI_STATUS_S|
1496 MCI_STATUS_AR|MCACOD_INSTR))
1497 return;
1498
1499 m->mcgstatus |= MCG_STATUS_EIPV;
1500 m->ip = regs->ip;
1501 m->cs = regs->cs;
1502}
1503
1da177e4 1504/* Add per CPU specific workarounds here */
148f9bb8 1505static int __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
d88203d1 1506{
d203f0b8
BP
1507 struct mca_config *cfg = &mca_cfg;
1508
e412cd25 1509 if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
c767a54b 1510 pr_info("unknown CPU type - not enabling MCE support\n");
e412cd25
IM
1511 return -EOPNOTSUPP;
1512 }
1513
1da177e4 1514 /* This should be disabled by the BIOS, but isn't always */
911f6a7b 1515 if (c->x86_vendor == X86_VENDOR_AMD) {
d203f0b8 1516 if (c->x86 == 15 && cfg->banks > 4) {
e9eee03e
IM
1517 /*
1518 * disable GART TBL walk error reporting, which
1519 * trips off incorrectly with the IOMMU & 3ware
1520 * & Cerberus:
1521 */
cebe1820 1522 clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
e9eee03e 1523 }
84c2559d 1524 if (c->x86 <= 17 && cfg->bootlog < 0) {
e9eee03e
IM
1525 /*
1526 * Lots of broken BIOS around that don't clear them
1527 * by default and leave crap in there. Don't log:
1528 */
84c2559d 1529 cfg->bootlog = 0;
e9eee03e 1530 }
2e6f694f
AK
1531 /*
1532 * Various K7s with broken bank 0 around. Always disable
1533 * by default.
1534 */
d203f0b8 1535 if (c->x86 == 6 && cfg->banks > 0)
cebe1820 1536 mce_banks[0].ctl = 0;
575203b4
BP
1537
1538 /*
1539 * Turn off MC4_MISC thresholding banks on those models since
1540 * they're not supported there.
1541 */
1542 if (c->x86 == 0x15 &&
1543 (c->x86_model >= 0x10 && c->x86_model <= 0x1f)) {
1544 int i;
1545 u64 val, hwcr;
1546 bool need_toggle;
1547 u32 msrs[] = {
1548 0x00000413, /* MC4_MISC0 */
1549 0xc0000408, /* MC4_MISC1 */
1550 };
1551
1552 rdmsrl(MSR_K7_HWCR, hwcr);
1553
1554 /* McStatusWrEn has to be set */
1555 need_toggle = !(hwcr & BIT(18));
1556
1557 if (need_toggle)
1558 wrmsrl(MSR_K7_HWCR, hwcr | BIT(18));
1559
1560 for (i = 0; i < ARRAY_SIZE(msrs); i++) {
1561 rdmsrl(msrs[i], val);
1562
1563 /* CntP bit set? */
80f03361
BP
1564 if (val & BIT_64(62)) {
1565 val &= ~BIT_64(62);
1566 wrmsrl(msrs[i], val);
575203b4
BP
1567 }
1568 }
1569
1570 /* restore old settings */
1571 if (need_toggle)
1572 wrmsrl(MSR_K7_HWCR, hwcr);
1573 }
1da177e4 1574 }
e583538f 1575
06b7a7a5
AK
1576 if (c->x86_vendor == X86_VENDOR_INTEL) {
1577 /*
1578 * SDM documents that on family 6 bank 0 should not be written
1579 * because it aliases to another special BIOS controlled
1580 * register.
1581 * But it's not aliased anymore on model 0x1a+
1582 * Don't ignore bank 0 completely because there could be a
1583 * valid event later, merely don't write CTL0.
1584 */
1585
d203f0b8 1586 if (c->x86 == 6 && c->x86_model < 0x1A && cfg->banks > 0)
cebe1820 1587 mce_banks[0].init = 0;
3c079792
AK
1588
1589 /*
1590 * All newer Intel systems support MCE broadcasting. Enable
1591 * synchronization with a one second timeout.
1592 */
1593 if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
84c2559d
BP
1594 cfg->monarch_timeout < 0)
1595 cfg->monarch_timeout = USEC_PER_SEC;
c7f6fa44 1596
e412cd25
IM
1597 /*
1598 * There are also broken BIOSes on some Pentium M and
1599 * earlier systems:
1600 */
84c2559d
BP
1601 if (c->x86 == 6 && c->x86_model <= 13 && cfg->bootlog < 0)
1602 cfg->bootlog = 0;
61b0fccd
TL
1603
1604 if (c->x86 == 6 && c->x86_model == 45)
1605 quirk_no_way_out = quirk_sandybridge_ifu;
06b7a7a5 1606 }
84c2559d
BP
1607 if (cfg->monarch_timeout < 0)
1608 cfg->monarch_timeout = 0;
1609 if (cfg->bootlog != 0)
7af19e4a 1610 cfg->panic_timeout = 30;
e412cd25
IM
1611
1612 return 0;
d88203d1 1613}
1da177e4 1614
148f9bb8 1615static int __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
4efc0670
AK
1616{
1617 if (c->x86 != 5)
3a97fc34
HS
1618 return 0;
1619
4efc0670
AK
1620 switch (c->x86_vendor) {
1621 case X86_VENDOR_INTEL:
c6978369 1622 intel_p5_mcheck_init(c);
3a97fc34 1623 return 1;
4efc0670
AK
1624 break;
1625 case X86_VENDOR_CENTAUR:
1626 winchip_mcheck_init(c);
3a97fc34 1627 return 1;
4efc0670
AK
1628 break;
1629 }
3a97fc34
HS
1630
1631 return 0;
4efc0670
AK
1632}
1633
5e09954a 1634static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
1da177e4
LT
1635{
1636 switch (c->x86_vendor) {
1637 case X86_VENDOR_INTEL:
1638 mce_intel_feature_init(c);
55babd8f 1639 mce_adjust_timer = mce_intel_adjust_timer;
1da177e4 1640 break;
89b831ef
JS
1641 case X86_VENDOR_AMD:
1642 mce_amd_feature_init(c);
1643 break;
1da177e4
LT
1644 default:
1645 break;
1646 }
1647}
1648
26c3c283 1649static void mce_start_timer(unsigned int cpu, struct timer_list *t)
52d168e2 1650{
4f75d841 1651 unsigned long iv = check_interval * HZ;
bc09effa 1652
7af19e4a 1653 if (mca_cfg.ignore_ce || !iv)
62fdac59
HS
1654 return;
1655
4f75d841
BP
1656 per_cpu(mce_next_interval, cpu) = iv;
1657
82f7af09 1658 t->expires = round_jiffies(jiffies + iv);
4f75d841 1659 add_timer_on(t, cpu);
52d168e2
AK
1660}
1661
26c3c283
TG
1662static void __mcheck_cpu_init_timer(void)
1663{
89cbc767 1664 struct timer_list *t = this_cpu_ptr(&mce_timer);
26c3c283
TG
1665 unsigned int cpu = smp_processor_id();
1666
1667 setup_timer(t, mce_timer_fn, cpu);
1668 mce_start_timer(cpu, t);
1669}
1670
9eda8cb3
AK
1671/* Handle unconfigured int18 (should never happen) */
1672static void unexpected_machine_check(struct pt_regs *regs, long error_code)
1673{
c767a54b 1674 pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
9eda8cb3
AK
1675 smp_processor_id());
1676}
1677
1678/* Call the installed machine check handler for this CPU setup. */
1679void (*machine_check_vector)(struct pt_regs *, long error_code) =
1680 unexpected_machine_check;
1681
d88203d1 1682/*
1da177e4 1683 * Called for each booted CPU to set up machine checks.
e9eee03e 1684 * Must be called with preempt off:
1da177e4 1685 */
148f9bb8 1686void mcheck_cpu_init(struct cpuinfo_x86 *c)
1da177e4 1687{
1462594b 1688 if (mca_cfg.disabled)
4efc0670
AK
1689 return;
1690
3a97fc34
HS
1691 if (__mcheck_cpu_ancient_init(c))
1692 return;
4efc0670 1693
5b4408fd 1694 if (!mce_available(c))
1da177e4
LT
1695 return;
1696
5e09954a 1697 if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
1462594b 1698 mca_cfg.disabled = true;
0d7482e3
AK
1699 return;
1700 }
0d7482e3 1701
5d727926
AK
1702 machine_check_vector = do_machine_check;
1703
5e09954a
BP
1704 __mcheck_cpu_init_generic();
1705 __mcheck_cpu_init_vendor(c);
1706 __mcheck_cpu_init_timer();
89cbc767
CL
1707 INIT_WORK(this_cpu_ptr(&mce_work), mce_process_work);
1708 init_irq_work(this_cpu_ptr(&mce_irq_work), &mce_irq_work_cb);
1da177e4
LT
1709}
1710
1711/*
93b62c3c 1712 * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
1da177e4
LT
1713 */
1714
93b62c3c
HS
1715static DEFINE_SPINLOCK(mce_chrdev_state_lock);
1716static int mce_chrdev_open_count; /* #times opened */
1717static int mce_chrdev_open_exclu; /* already open exclusive? */
f528e7ba 1718
93b62c3c 1719static int mce_chrdev_open(struct inode *inode, struct file *file)
f528e7ba 1720{
93b62c3c 1721 spin_lock(&mce_chrdev_state_lock);
f528e7ba 1722
93b62c3c
HS
1723 if (mce_chrdev_open_exclu ||
1724 (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
1725 spin_unlock(&mce_chrdev_state_lock);
e9eee03e 1726
f528e7ba
TH
1727 return -EBUSY;
1728 }
1729
1730 if (file->f_flags & O_EXCL)
93b62c3c
HS
1731 mce_chrdev_open_exclu = 1;
1732 mce_chrdev_open_count++;
f528e7ba 1733
93b62c3c 1734 spin_unlock(&mce_chrdev_state_lock);
f528e7ba 1735
bd78432c 1736 return nonseekable_open(inode, file);
f528e7ba
TH
1737}
1738
93b62c3c 1739static int mce_chrdev_release(struct inode *inode, struct file *file)
f528e7ba 1740{
93b62c3c 1741 spin_lock(&mce_chrdev_state_lock);
f528e7ba 1742
93b62c3c
HS
1743 mce_chrdev_open_count--;
1744 mce_chrdev_open_exclu = 0;
f528e7ba 1745
93b62c3c 1746 spin_unlock(&mce_chrdev_state_lock);
f528e7ba
TH
1747
1748 return 0;
1749}
1750
d88203d1
TG
1751static void collect_tscs(void *data)
1752{
1da177e4 1753 unsigned long *cpu_tsc = (unsigned long *)data;
d88203d1 1754
1da177e4 1755 rdtscll(cpu_tsc[smp_processor_id()]);
d88203d1 1756}
1da177e4 1757
482908b4
HY
1758static int mce_apei_read_done;
1759
1760/* Collect MCE record of previous boot in persistent storage via APEI ERST. */
1761static int __mce_read_apei(char __user **ubuf, size_t usize)
1762{
1763 int rc;
1764 u64 record_id;
1765 struct mce m;
1766
1767 if (usize < sizeof(struct mce))
1768 return -EINVAL;
1769
1770 rc = apei_read_mce(&m, &record_id);
1771 /* Error or no more MCE record */
1772 if (rc <= 0) {
1773 mce_apei_read_done = 1;
fadd85f1
NH
1774 /*
1775 * When ERST is disabled, mce_chrdev_read() should return
1776 * "no record" instead of "no device."
1777 */
1778 if (rc == -ENODEV)
1779 return 0;
482908b4
HY
1780 return rc;
1781 }
1782 rc = -EFAULT;
1783 if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
1784 return rc;
1785 /*
1786 * In fact, we should have cleared the record after that has
1787 * been flushed to the disk or sent to network in
1788 * /sbin/mcelog, but we have no interface to support that now,
1789 * so just clear it to avoid duplication.
1790 */
1791 rc = apei_clear_mce(record_id);
1792 if (rc) {
1793 mce_apei_read_done = 1;
1794 return rc;
1795 }
1796 *ubuf += sizeof(struct mce);
1797
1798 return 0;
1799}
1800
93b62c3c
HS
1801static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
1802 size_t usize, loff_t *off)
1da177e4 1803{
e9eee03e 1804 char __user *buf = ubuf;
f0de53bb 1805 unsigned long *cpu_tsc;
ef41df43 1806 unsigned prev, next;
1da177e4
LT
1807 int i, err;
1808
6bca67f9 1809 cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
f0de53bb
AK
1810 if (!cpu_tsc)
1811 return -ENOMEM;
1812
93b62c3c 1813 mutex_lock(&mce_chrdev_read_mutex);
482908b4
HY
1814
1815 if (!mce_apei_read_done) {
1816 err = __mce_read_apei(&buf, usize);
1817 if (err || buf != ubuf)
1818 goto out;
1819 }
1820
f56e8a07 1821 next = rcu_dereference_check_mce(mcelog.next);
1da177e4
LT
1822
1823 /* Only supports full reads right now */
482908b4
HY
1824 err = -EINVAL;
1825 if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
1826 goto out;
1da177e4
LT
1827
1828 err = 0;
ef41df43
HY
1829 prev = 0;
1830 do {
1831 for (i = prev; i < next; i++) {
1832 unsigned long start = jiffies;
559faa6b 1833 struct mce *m = &mcelog.entry[i];
ef41df43 1834
559faa6b 1835 while (!m->finished) {
ef41df43 1836 if (time_after_eq(jiffies, start + 2)) {
559faa6b 1837 memset(m, 0, sizeof(*m));
ef41df43
HY
1838 goto timeout;
1839 }
1840 cpu_relax();
673242c1 1841 }
ef41df43 1842 smp_rmb();
559faa6b
HS
1843 err |= copy_to_user(buf, m, sizeof(*m));
1844 buf += sizeof(*m);
ef41df43
HY
1845timeout:
1846 ;
673242c1 1847 }
1da177e4 1848
ef41df43
HY
1849 memset(mcelog.entry + prev, 0,
1850 (next - prev) * sizeof(struct mce));
1851 prev = next;
1852 next = cmpxchg(&mcelog.next, prev, 0);
1853 } while (next != prev);
1da177e4 1854
b2b18660 1855 synchronize_sched();
1da177e4 1856
d88203d1
TG
1857 /*
1858 * Collect entries that were still getting written before the
1859 * synchronize.
1860 */
15c8b6c1 1861 on_each_cpu(collect_tscs, cpu_tsc, 1);
e9eee03e 1862
d88203d1 1863 for (i = next; i < MCE_LOG_LEN; i++) {
559faa6b
HS
1864 struct mce *m = &mcelog.entry[i];
1865
1866 if (m->finished && m->tsc < cpu_tsc[m->cpu]) {
1867 err |= copy_to_user(buf, m, sizeof(*m));
1da177e4 1868 smp_rmb();
559faa6b
HS
1869 buf += sizeof(*m);
1870 memset(m, 0, sizeof(*m));
1da177e4 1871 }
d88203d1 1872 }
482908b4
HY
1873
1874 if (err)
1875 err = -EFAULT;
1876
1877out:
93b62c3c 1878 mutex_unlock(&mce_chrdev_read_mutex);
f0de53bb 1879 kfree(cpu_tsc);
e9eee03e 1880
482908b4 1881 return err ? err : buf - ubuf;
1da177e4
LT
1882}
1883
93b62c3c 1884static unsigned int mce_chrdev_poll(struct file *file, poll_table *wait)
e02e68d3 1885{
93b62c3c 1886 poll_wait(file, &mce_chrdev_wait, wait);
a4dd9925 1887 if (rcu_access_index(mcelog.next))
e02e68d3 1888 return POLLIN | POLLRDNORM;
482908b4
HY
1889 if (!mce_apei_read_done && apei_check_mce())
1890 return POLLIN | POLLRDNORM;
e02e68d3
TH
1891 return 0;
1892}
1893
93b62c3c
HS
1894static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
1895 unsigned long arg)
1da177e4
LT
1896{
1897 int __user *p = (int __user *)arg;
d88203d1 1898
1da177e4 1899 if (!capable(CAP_SYS_ADMIN))
d88203d1 1900 return -EPERM;
e9eee03e 1901
1da177e4 1902 switch (cmd) {
d88203d1 1903 case MCE_GET_RECORD_LEN:
1da177e4
LT
1904 return put_user(sizeof(struct mce), p);
1905 case MCE_GET_LOG_LEN:
d88203d1 1906 return put_user(MCE_LOG_LEN, p);
1da177e4
LT
1907 case MCE_GETCLEAR_FLAGS: {
1908 unsigned flags;
d88203d1
TG
1909
1910 do {
1da177e4 1911 flags = mcelog.flags;
d88203d1 1912 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
e9eee03e 1913
d88203d1 1914 return put_user(flags, p);
1da177e4
LT
1915 }
1916 default:
d88203d1
TG
1917 return -ENOTTY;
1918 }
1da177e4
LT
1919}
1920
66f5ddf3
LT
1921static ssize_t (*mce_write)(struct file *filp, const char __user *ubuf,
1922 size_t usize, loff_t *off);
1923
1924void register_mce_write_callback(ssize_t (*fn)(struct file *filp,
1925 const char __user *ubuf,
1926 size_t usize, loff_t *off))
1927{
1928 mce_write = fn;
1929}
1930EXPORT_SYMBOL_GPL(register_mce_write_callback);
1931
1932ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
1933 size_t usize, loff_t *off)
1934{
1935 if (mce_write)
1936 return mce_write(filp, ubuf, usize, off);
1937 else
1938 return -EINVAL;
1939}
1940
1941static const struct file_operations mce_chrdev_ops = {
93b62c3c
HS
1942 .open = mce_chrdev_open,
1943 .release = mce_chrdev_release,
1944 .read = mce_chrdev_read,
66f5ddf3 1945 .write = mce_chrdev_write,
93b62c3c
HS
1946 .poll = mce_chrdev_poll,
1947 .unlocked_ioctl = mce_chrdev_ioctl,
1948 .llseek = no_llseek,
1da177e4
LT
1949};
1950
93b62c3c 1951static struct miscdevice mce_chrdev_device = {
1da177e4
LT
1952 MISC_MCELOG_MINOR,
1953 "mcelog",
1954 &mce_chrdev_ops,
1955};
1956
c3d1fb56
NR
1957static void __mce_disable_bank(void *arg)
1958{
1959 int bank = *((int *)arg);
89cbc767 1960 __clear_bit(bank, this_cpu_ptr(mce_poll_banks));
c3d1fb56
NR
1961 cmci_disable_bank(bank);
1962}
1963
1964void mce_disable_bank(int bank)
1965{
1966 if (bank >= mca_cfg.banks) {
1967 pr_warn(FW_BUG
1968 "Ignoring request to disable invalid MCA bank %d.\n",
1969 bank);
1970 return;
1971 }
1972 set_bit(bank, mce_banks_ce_disabled);
1973 on_each_cpu(__mce_disable_bank, &bank, 1);
1974}
1975
13503fa9 1976/*
62fdac59
HS
1977 * mce=off Disables machine check
1978 * mce=no_cmci Disables CMCI
1979 * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
1980 * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
3c079792
AK
1981 * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
1982 * monarchtimeout is how long to wait for other CPUs on machine
1983 * check, or 0 to not wait
13503fa9
HS
1984 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
1985 * mce=nobootlog Don't log MCEs from before booting.
450cc201 1986 * mce=bios_cmci_threshold Don't program the CMCI threshold
13503fa9 1987 */
1da177e4
LT
1988static int __init mcheck_enable(char *str)
1989{
d203f0b8
BP
1990 struct mca_config *cfg = &mca_cfg;
1991
e3346fc4 1992 if (*str == 0) {
4efc0670 1993 enable_p5_mce();
e3346fc4
BZ
1994 return 1;
1995 }
4efc0670
AK
1996 if (*str == '=')
1997 str++;
1da177e4 1998 if (!strcmp(str, "off"))
1462594b 1999 cfg->disabled = true;
62fdac59 2000 else if (!strcmp(str, "no_cmci"))
7af19e4a 2001 cfg->cmci_disabled = true;
62fdac59 2002 else if (!strcmp(str, "dont_log_ce"))
d203f0b8 2003 cfg->dont_log_ce = true;
62fdac59 2004 else if (!strcmp(str, "ignore_ce"))
7af19e4a 2005 cfg->ignore_ce = true;
13503fa9 2006 else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
84c2559d 2007 cfg->bootlog = (str[0] == 'b');
450cc201 2008 else if (!strcmp(str, "bios_cmci_threshold"))
1462594b 2009 cfg->bios_cmci_threshold = true;
3c079792 2010 else if (isdigit(str[0])) {
d203f0b8 2011 get_option(&str, &(cfg->tolerant));
3c079792
AK
2012 if (*str == ',') {
2013 ++str;
84c2559d 2014 get_option(&str, &(cfg->monarch_timeout));
3c079792
AK
2015 }
2016 } else {
c767a54b 2017 pr_info("mce argument %s ignored. Please use /sys\n", str);
13503fa9
HS
2018 return 0;
2019 }
9b41046c 2020 return 1;
1da177e4 2021}
4efc0670 2022__setup("mce", mcheck_enable);
1da177e4 2023
a2202aa2 2024int __init mcheck_init(void)
b33a6363 2025{
a2202aa2
YW
2026 mcheck_intel_therm_init();
2027
b33a6363
BP
2028 return 0;
2029}
b33a6363 2030
d88203d1 2031/*
c7cece89 2032 * mce_syscore: PM support
d88203d1 2033 */
1da177e4 2034
973a2dd1
AK
2035/*
2036 * Disable machine checks on suspend and shutdown. We can't really handle
2037 * them later.
2038 */
5e09954a 2039static int mce_disable_error_reporting(void)
973a2dd1
AK
2040{
2041 int i;
2042
d203f0b8 2043 for (i = 0; i < mca_cfg.banks; i++) {
cebe1820 2044 struct mce_bank *b = &mce_banks[i];
11868a2d 2045
cebe1820 2046 if (b->init)
a2d32bcb 2047 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
06b7a7a5 2048 }
973a2dd1
AK
2049 return 0;
2050}
2051
c7cece89 2052static int mce_syscore_suspend(void)
973a2dd1 2053{
5e09954a 2054 return mce_disable_error_reporting();
973a2dd1
AK
2055}
2056
c7cece89 2057static void mce_syscore_shutdown(void)
973a2dd1 2058{
f3c6ea1b 2059 mce_disable_error_reporting();
973a2dd1
AK
2060}
2061
e9eee03e
IM
2062/*
2063 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
2064 * Only one CPU is active at this time, the others get re-added later using
2065 * CPU hotplug:
2066 */
c7cece89 2067static void mce_syscore_resume(void)
1da177e4 2068{
5e09954a 2069 __mcheck_cpu_init_generic();
89cbc767 2070 __mcheck_cpu_init_vendor(raw_cpu_ptr(&cpu_info));
1da177e4
LT
2071}
2072
f3c6ea1b 2073static struct syscore_ops mce_syscore_ops = {
c7cece89
HS
2074 .suspend = mce_syscore_suspend,
2075 .shutdown = mce_syscore_shutdown,
2076 .resume = mce_syscore_resume,
f3c6ea1b
RW
2077};
2078
c7cece89 2079/*
8a25a2fd 2080 * mce_device: Sysfs support
c7cece89
HS
2081 */
2082
52d168e2
AK
2083static void mce_cpu_restart(void *data)
2084{
89cbc767 2085 if (!mce_available(raw_cpu_ptr(&cpu_info)))
33edbf02 2086 return;
5e09954a
BP
2087 __mcheck_cpu_init_generic();
2088 __mcheck_cpu_init_timer();
52d168e2
AK
2089}
2090
1da177e4 2091/* Reinit MCEs after user configuration changes */
d88203d1
TG
2092static void mce_restart(void)
2093{
9aaef96f 2094 mce_timer_delete_all();
52d168e2 2095 on_each_cpu(mce_cpu_restart, NULL, 1);
1da177e4
LT
2096}
2097
9af43b54 2098/* Toggle features for corrected errors */
9aaef96f 2099static void mce_disable_cmci(void *data)
9af43b54 2100{
89cbc767 2101 if (!mce_available(raw_cpu_ptr(&cpu_info)))
9af43b54 2102 return;
9af43b54
HS
2103 cmci_clear();
2104}
2105
2106static void mce_enable_ce(void *all)
2107{
89cbc767 2108 if (!mce_available(raw_cpu_ptr(&cpu_info)))
9af43b54
HS
2109 return;
2110 cmci_reenable();
2111 cmci_recheck();
2112 if (all)
5e09954a 2113 __mcheck_cpu_init_timer();
9af43b54
HS
2114}
2115
8a25a2fd 2116static struct bus_type mce_subsys = {
e9eee03e 2117 .name = "machinecheck",
8a25a2fd 2118 .dev_name = "machinecheck",
1da177e4
LT
2119};
2120
d6126ef5 2121DEFINE_PER_CPU(struct device *, mce_device);
e9eee03e 2122
e9eee03e 2123void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
1da177e4 2124
8a25a2fd 2125static inline struct mce_bank *attr_to_bank(struct device_attribute *attr)
cebe1820
AK
2126{
2127 return container_of(attr, struct mce_bank, attr);
2128}
0d7482e3 2129
8a25a2fd 2130static ssize_t show_bank(struct device *s, struct device_attribute *attr,
0d7482e3
AK
2131 char *buf)
2132{
cebe1820 2133 return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
0d7482e3
AK
2134}
2135
8a25a2fd 2136static ssize_t set_bank(struct device *s, struct device_attribute *attr,
9319cec8 2137 const char *buf, size_t size)
0d7482e3 2138{
9319cec8 2139 u64 new;
e9eee03e 2140
164109e3 2141 if (kstrtou64(buf, 0, &new) < 0)
0d7482e3 2142 return -EINVAL;
e9eee03e 2143
cebe1820 2144 attr_to_bank(attr)->ctl = new;
0d7482e3 2145 mce_restart();
e9eee03e 2146
9319cec8 2147 return size;
0d7482e3 2148}
a98f0dd3 2149
e9eee03e 2150static ssize_t
8a25a2fd 2151show_trigger(struct device *s, struct device_attribute *attr, char *buf)
a98f0dd3 2152{
1020bcbc 2153 strcpy(buf, mce_helper);
a98f0dd3 2154 strcat(buf, "\n");
1020bcbc 2155 return strlen(mce_helper) + 1;
a98f0dd3
AK
2156}
2157
8a25a2fd 2158static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
e9eee03e 2159 const char *buf, size_t siz)
a98f0dd3
AK
2160{
2161 char *p;
e9eee03e 2162
1020bcbc
HS
2163 strncpy(mce_helper, buf, sizeof(mce_helper));
2164 mce_helper[sizeof(mce_helper)-1] = 0;
1020bcbc 2165 p = strchr(mce_helper, '\n');
e9eee03e 2166
e9084ec9 2167 if (p)
e9eee03e
IM
2168 *p = 0;
2169
e9084ec9 2170 return strlen(mce_helper) + !!p;
a98f0dd3
AK
2171}
2172
8a25a2fd
KS
2173static ssize_t set_ignore_ce(struct device *s,
2174 struct device_attribute *attr,
9af43b54
HS
2175 const char *buf, size_t size)
2176{
2177 u64 new;
2178
164109e3 2179 if (kstrtou64(buf, 0, &new) < 0)
9af43b54
HS
2180 return -EINVAL;
2181
7af19e4a 2182 if (mca_cfg.ignore_ce ^ !!new) {
9af43b54
HS
2183 if (new) {
2184 /* disable ce features */
9aaef96f
HS
2185 mce_timer_delete_all();
2186 on_each_cpu(mce_disable_cmci, NULL, 1);
7af19e4a 2187 mca_cfg.ignore_ce = true;
9af43b54
HS
2188 } else {
2189 /* enable ce features */
7af19e4a 2190 mca_cfg.ignore_ce = false;
9af43b54
HS
2191 on_each_cpu(mce_enable_ce, (void *)1, 1);
2192 }
2193 }
2194 return size;
2195}
2196
8a25a2fd
KS
2197static ssize_t set_cmci_disabled(struct device *s,
2198 struct device_attribute *attr,
9af43b54
HS
2199 const char *buf, size_t size)
2200{
2201 u64 new;
2202
164109e3 2203 if (kstrtou64(buf, 0, &new) < 0)
9af43b54
HS
2204 return -EINVAL;
2205
7af19e4a 2206 if (mca_cfg.cmci_disabled ^ !!new) {
9af43b54
HS
2207 if (new) {
2208 /* disable cmci */
9aaef96f 2209 on_each_cpu(mce_disable_cmci, NULL, 1);
7af19e4a 2210 mca_cfg.cmci_disabled = true;
9af43b54
HS
2211 } else {
2212 /* enable cmci */
7af19e4a 2213 mca_cfg.cmci_disabled = false;
9af43b54
HS
2214 on_each_cpu(mce_enable_ce, NULL, 1);
2215 }
2216 }
2217 return size;
2218}
2219
8a25a2fd
KS
2220static ssize_t store_int_with_restart(struct device *s,
2221 struct device_attribute *attr,
b56f642d
AK
2222 const char *buf, size_t size)
2223{
8a25a2fd 2224 ssize_t ret = device_store_int(s, attr, buf, size);
b56f642d
AK
2225 mce_restart();
2226 return ret;
2227}
2228
8a25a2fd 2229static DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
d203f0b8 2230static DEVICE_INT_ATTR(tolerant, 0644, mca_cfg.tolerant);
84c2559d 2231static DEVICE_INT_ATTR(monarch_timeout, 0644, mca_cfg.monarch_timeout);
d203f0b8 2232static DEVICE_BOOL_ATTR(dont_log_ce, 0644, mca_cfg.dont_log_ce);
e9eee03e 2233
8a25a2fd
KS
2234static struct dev_ext_attribute dev_attr_check_interval = {
2235 __ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
b56f642d
AK
2236 &check_interval
2237};
e9eee03e 2238
8a25a2fd 2239static struct dev_ext_attribute dev_attr_ignore_ce = {
7af19e4a
BP
2240 __ATTR(ignore_ce, 0644, device_show_bool, set_ignore_ce),
2241 &mca_cfg.ignore_ce
9af43b54
HS
2242};
2243
8a25a2fd 2244static struct dev_ext_attribute dev_attr_cmci_disabled = {
7af19e4a
BP
2245 __ATTR(cmci_disabled, 0644, device_show_bool, set_cmci_disabled),
2246 &mca_cfg.cmci_disabled
9af43b54
HS
2247};
2248
8a25a2fd
KS
2249static struct device_attribute *mce_device_attrs[] = {
2250 &dev_attr_tolerant.attr,
2251 &dev_attr_check_interval.attr,
2252 &dev_attr_trigger,
2253 &dev_attr_monarch_timeout.attr,
2254 &dev_attr_dont_log_ce.attr,
2255 &dev_attr_ignore_ce.attr,
2256 &dev_attr_cmci_disabled.attr,
a98f0dd3
AK
2257 NULL
2258};
1da177e4 2259
8a25a2fd 2260static cpumask_var_t mce_device_initialized;
bae19fe0 2261
e032d807
GKH
2262static void mce_device_release(struct device *dev)
2263{
2264 kfree(dev);
2265}
2266
8a25a2fd 2267/* Per cpu device init. All of the cpus still share the same ctrl bank: */
148f9bb8 2268static int mce_device_create(unsigned int cpu)
1da177e4 2269{
e032d807 2270 struct device *dev;
1da177e4 2271 int err;
b1f49f95 2272 int i, j;
92cb7612 2273
90367556 2274 if (!mce_available(&boot_cpu_data))
91c6d400
AK
2275 return -EIO;
2276
e032d807
GKH
2277 dev = kzalloc(sizeof *dev, GFP_KERNEL);
2278 if (!dev)
2279 return -ENOMEM;
8a25a2fd
KS
2280 dev->id = cpu;
2281 dev->bus = &mce_subsys;
e032d807 2282 dev->release = &mce_device_release;
91c6d400 2283
8a25a2fd 2284 err = device_register(dev);
853d9b18
LK
2285 if (err) {
2286 put_device(dev);
d435d862 2287 return err;
853d9b18 2288 }
d435d862 2289
8a25a2fd
KS
2290 for (i = 0; mce_device_attrs[i]; i++) {
2291 err = device_create_file(dev, mce_device_attrs[i]);
d435d862
AM
2292 if (err)
2293 goto error;
2294 }
d203f0b8 2295 for (j = 0; j < mca_cfg.banks; j++) {
8a25a2fd 2296 err = device_create_file(dev, &mce_banks[j].attr);
0d7482e3
AK
2297 if (err)
2298 goto error2;
2299 }
8a25a2fd 2300 cpumask_set_cpu(cpu, mce_device_initialized);
d6126ef5 2301 per_cpu(mce_device, cpu) = dev;
91c6d400 2302
d435d862 2303 return 0;
0d7482e3 2304error2:
b1f49f95 2305 while (--j >= 0)
8a25a2fd 2306 device_remove_file(dev, &mce_banks[j].attr);
d435d862 2307error:
cb491fca 2308 while (--i >= 0)
8a25a2fd 2309 device_remove_file(dev, mce_device_attrs[i]);
cb491fca 2310
8a25a2fd 2311 device_unregister(dev);
d435d862 2312
91c6d400
AK
2313 return err;
2314}
2315
148f9bb8 2316static void mce_device_remove(unsigned int cpu)
91c6d400 2317{
d6126ef5 2318 struct device *dev = per_cpu(mce_device, cpu);
73ca5358
SL
2319 int i;
2320
8a25a2fd 2321 if (!cpumask_test_cpu(cpu, mce_device_initialized))
bae19fe0
AH
2322 return;
2323
8a25a2fd
KS
2324 for (i = 0; mce_device_attrs[i]; i++)
2325 device_remove_file(dev, mce_device_attrs[i]);
cb491fca 2326
d203f0b8 2327 for (i = 0; i < mca_cfg.banks; i++)
8a25a2fd 2328 device_remove_file(dev, &mce_banks[i].attr);
cb491fca 2329
8a25a2fd
KS
2330 device_unregister(dev);
2331 cpumask_clear_cpu(cpu, mce_device_initialized);
d6126ef5 2332 per_cpu(mce_device, cpu) = NULL;
91c6d400 2333}
91c6d400 2334
d6b75584 2335/* Make sure there are no machine checks on offlined CPUs. */
148f9bb8 2336static void mce_disable_cpu(void *h)
d6b75584 2337{
88ccbedd 2338 unsigned long action = *(unsigned long *)h;
cb491fca 2339 int i;
d6b75584 2340
89cbc767 2341 if (!mce_available(raw_cpu_ptr(&cpu_info)))
d6b75584 2342 return;
767df1bd 2343
88ccbedd
AK
2344 if (!(action & CPU_TASKS_FROZEN))
2345 cmci_clear();
d203f0b8 2346 for (i = 0; i < mca_cfg.banks; i++) {
cebe1820 2347 struct mce_bank *b = &mce_banks[i];
11868a2d 2348
cebe1820 2349 if (b->init)
a2d32bcb 2350 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
06b7a7a5 2351 }
d6b75584
AK
2352}
2353
148f9bb8 2354static void mce_reenable_cpu(void *h)
d6b75584 2355{
88ccbedd 2356 unsigned long action = *(unsigned long *)h;
e9eee03e 2357 int i;
d6b75584 2358
89cbc767 2359 if (!mce_available(raw_cpu_ptr(&cpu_info)))
d6b75584 2360 return;
e9eee03e 2361
88ccbedd
AK
2362 if (!(action & CPU_TASKS_FROZEN))
2363 cmci_reenable();
d203f0b8 2364 for (i = 0; i < mca_cfg.banks; i++) {
cebe1820 2365 struct mce_bank *b = &mce_banks[i];
11868a2d 2366
cebe1820 2367 if (b->init)
a2d32bcb 2368 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
06b7a7a5 2369 }
d6b75584
AK
2370}
2371
91c6d400 2372/* Get notified when a cpu comes on/off. Be hotplug friendly. */
148f9bb8 2373static int
e9eee03e 2374mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
91c6d400
AK
2375{
2376 unsigned int cpu = (unsigned long)hcpu;
52d168e2 2377 struct timer_list *t = &per_cpu(mce_timer, cpu);
91c6d400 2378
1a65f970 2379 switch (action & ~CPU_TASKS_FROZEN) {
bae19fe0 2380 case CPU_ONLINE:
8a25a2fd 2381 mce_device_create(cpu);
8735728e
RW
2382 if (threshold_cpu_callback)
2383 threshold_cpu_callback(action, cpu);
91c6d400 2384 break;
91c6d400 2385 case CPU_DEAD:
8735728e
RW
2386 if (threshold_cpu_callback)
2387 threshold_cpu_callback(action, cpu);
8a25a2fd 2388 mce_device_remove(cpu);
55babd8f 2389 mce_intel_hcpu_update(cpu);
38356c1f
BP
2390
2391 /* intentionally ignoring frozen here */
2392 if (!(action & CPU_TASKS_FROZEN))
2393 cmci_rediscover();
91c6d400 2394 break;
52d168e2 2395 case CPU_DOWN_PREPARE:
88ccbedd 2396 smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
55babd8f 2397 del_timer_sync(t);
52d168e2
AK
2398 break;
2399 case CPU_DOWN_FAILED:
88ccbedd 2400 smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
26c3c283 2401 mce_start_timer(cpu, t);
88ccbedd 2402 break;
1a65f970
TG
2403 }
2404
bae19fe0 2405 return NOTIFY_OK;
91c6d400
AK
2406}
2407
148f9bb8 2408static struct notifier_block mce_cpu_notifier = {
91c6d400
AK
2409 .notifier_call = mce_cpu_callback,
2410};
2411
cebe1820 2412static __init void mce_init_banks(void)
0d7482e3
AK
2413{
2414 int i;
2415
d203f0b8 2416 for (i = 0; i < mca_cfg.banks; i++) {
cebe1820 2417 struct mce_bank *b = &mce_banks[i];
8a25a2fd 2418 struct device_attribute *a = &b->attr;
e9eee03e 2419
a07e4156 2420 sysfs_attr_init(&a->attr);
cebe1820
AK
2421 a->attr.name = b->attrname;
2422 snprintf(b->attrname, ATTR_LEN, "bank%d", i);
e9eee03e
IM
2423
2424 a->attr.mode = 0644;
2425 a->show = show_bank;
2426 a->store = set_bank;
0d7482e3 2427 }
0d7482e3
AK
2428}
2429
5e09954a 2430static __init int mcheck_init_device(void)
91c6d400
AK
2431{
2432 int err;
2433 int i = 0;
2434
9c15a24b
MS
2435 if (!mce_available(&boot_cpu_data)) {
2436 err = -EIO;
2437 goto err_out;
2438 }
0d7482e3 2439
9c15a24b
MS
2440 if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
2441 err = -ENOMEM;
2442 goto err_out;
2443 }
996867d0 2444
cebe1820 2445 mce_init_banks();
0d7482e3 2446
8a25a2fd 2447 err = subsys_system_register(&mce_subsys, NULL);
d435d862 2448 if (err)
9c15a24b 2449 goto err_out_mem;
91c6d400 2450
82a8f131 2451 cpu_notifier_register_begin();
91c6d400 2452 for_each_online_cpu(i) {
8a25a2fd 2453 err = mce_device_create(i);
82a8f131 2454 if (err) {
27c93415
BP
2455 /*
2456 * Register notifier anyway (and do not unreg it) so
2457 * that we don't leave undeleted timers, see notifier
2458 * callback above.
2459 */
2460 __register_hotcpu_notifier(&mce_cpu_notifier);
82a8f131 2461 cpu_notifier_register_done();
9c15a24b 2462 goto err_device_create;
82a8f131 2463 }
91c6d400
AK
2464 }
2465
82a8f131
SB
2466 __register_hotcpu_notifier(&mce_cpu_notifier);
2467 cpu_notifier_register_done();
93b62c3c 2468
9c15a24b
MS
2469 register_syscore_ops(&mce_syscore_ops);
2470
93b62c3c 2471 /* register character device /dev/mcelog */
9c15a24b
MS
2472 err = misc_register(&mce_chrdev_device);
2473 if (err)
2474 goto err_register;
2475
2476 return 0;
2477
2478err_register:
2479 unregister_syscore_ops(&mce_syscore_ops);
2480
9c15a24b
MS
2481err_device_create:
2482 /*
2483 * We didn't keep track of which devices were created above, but
2484 * even if we had, the set of online cpus might have changed.
2485 * Play safe and remove for every possible cpu, since
2486 * mce_device_remove() will do the right thing.
2487 */
2488 for_each_possible_cpu(i)
2489 mce_device_remove(i);
2490
2491err_out_mem:
2492 free_cpumask_var(mce_device_initialized);
2493
2494err_out:
2495 pr_err("Unable to init device /dev/mcelog (rc: %d)\n", err);
e9eee03e 2496
1da177e4 2497 return err;
1da177e4 2498}
cef12ee5 2499device_initcall_sync(mcheck_init_device);
a988d334 2500
d7c3c9a6
AK
2501/*
2502 * Old style boot options parsing. Only for compatibility.
2503 */
2504static int __init mcheck_disable(char *str)
2505{
1462594b 2506 mca_cfg.disabled = true;
d7c3c9a6
AK
2507 return 1;
2508}
2509__setup("nomce", mcheck_disable);
a988d334 2510
5be9ed25
HY
2511#ifdef CONFIG_DEBUG_FS
2512struct dentry *mce_get_debugfs_dir(void)
a988d334 2513{
5be9ed25 2514 static struct dentry *dmce;
a988d334 2515
5be9ed25
HY
2516 if (!dmce)
2517 dmce = debugfs_create_dir("mce", NULL);
a988d334 2518
5be9ed25
HY
2519 return dmce;
2520}
a988d334 2521
bf783f9f
HY
2522static void mce_reset(void)
2523{
2524 cpu_missing = 0;
2525 atomic_set(&mce_fake_paniced, 0);
2526 atomic_set(&mce_executing, 0);
2527 atomic_set(&mce_callin, 0);
2528 atomic_set(&global_nwo, 0);
2529}
a988d334 2530
bf783f9f
HY
2531static int fake_panic_get(void *data, u64 *val)
2532{
2533 *val = fake_panic;
2534 return 0;
a988d334
IM
2535}
2536
bf783f9f 2537static int fake_panic_set(void *data, u64 val)
a988d334 2538{
bf783f9f
HY
2539 mce_reset();
2540 fake_panic = val;
2541 return 0;
a988d334 2542}
a988d334 2543
bf783f9f
HY
2544DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
2545 fake_panic_set, "%llu\n");
d7c3c9a6 2546
5e09954a 2547static int __init mcheck_debugfs_init(void)
d7c3c9a6 2548{
bf783f9f
HY
2549 struct dentry *dmce, *ffake_panic;
2550
2551 dmce = mce_get_debugfs_dir();
2552 if (!dmce)
2553 return -ENOMEM;
2554 ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
2555 &fake_panic_fops);
2556 if (!ffake_panic)
2557 return -ENOMEM;
2558
2559 return 0;
d7c3c9a6 2560}
5e09954a 2561late_initcall(mcheck_debugfs_init);
5be9ed25 2562#endif