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