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