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