blob: fc7608a89d932b0b5138ab2e2acf936dd68b174b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Machine check handler.
Ingo Molnare9eee032009-04-08 12:31:17 +02003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02005 * Rest from unknown author(s).
6 * 2004 Andi Kleen. Rewrote most of it.
Andi Kleenb79109c2009-02-12 13:43:23 +01007 * Copyright 2008 Intel Corporation
8 * Author: Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Joe Perchesc767a542012-05-21 19:50:07 -070010
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Tim Hockine02e68d2007-07-21 17:10:36 +020013#include <linux/thread_info.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020014#include <linux/capability.h>
15#include <linux/miscdevice.h>
Andi Kleen8457c842009-02-12 13:49:33 +010016#include <linux/ratelimit.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020017#include <linux/kallsyms.h>
18#include <linux/rcupdate.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020019#include <linux/kobject.h>
Hidetoshi Seto14a02532009-04-30 16:04:51 +090020#include <linux/uaccess.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020021#include <linux/kdebug.h>
22#include <linux/kernel.h>
23#include <linux/percpu.h>
24#include <linux/string.h>
Kay Sievers8a25a2f2011-12-21 14:29:42 -080025#include <linux/device.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010026#include <linux/syscore_ops.h>
Andi Kleen3c079792009-05-27 21:56:55 +020027#include <linux/delay.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020028#include <linux/ctype.h>
29#include <linux/sched.h>
30#include <linux/sysfs.h>
31#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020033#include <linux/init.h>
34#include <linux/kmod.h>
35#include <linux/poll.h>
Andi Kleen3c079792009-05-27 21:56:55 +020036#include <linux/nmi.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020037#include <linux/cpu.h>
Hidetoshi Seto14a02532009-04-30 16:04:51 +090038#include <linux/smp.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020039#include <linux/fs.h>
Andi Kleen9b1beaf2009-05-27 21:56:59 +020040#include <linux/mm.h>
Huang Ying5be9ed22009-07-31 09:41:42 +080041#include <linux/debugfs.h>
Hidetoshi Setob77e70b2011-06-08 10:56:02 +090042#include <linux/irq_work.h>
Paul Gortmaker69c60c82011-05-26 12:22:53 -040043#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Ingo Molnare9eee032009-04-08 12:31:17 +020045#include <asm/processor.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020046#include <asm/mce.h>
47#include <asm/msr.h>
Ingo Molnare9eee032009-04-08 12:31:17 +020048
Andi Kleenbd19a5e2009-05-27 21:56:55 +020049#include "mce-internal.h"
Ingo Molnar711c2e42009-04-08 12:31:26 +020050
Hidetoshi Seto93b62c32011-06-08 11:00:45 +090051static DEFINE_MUTEX(mce_chrdev_read_mutex);
Ingo Molnar2aa2b50dd2010-03-14 08:57:03 +010052
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -080053#define rcu_dereference_check_mce(p) \
Paul E. McKenneyec8c27e2010-04-30 06:45:36 -070054 rcu_dereference_index_check((p), \
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -080055 rcu_read_lock_sched_held() || \
Hidetoshi Seto93b62c32011-06-08 11:00:45 +090056 lockdep_is_held(&mce_chrdev_read_mutex))
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -080057
Hidetoshi Seto8968f9d2009-10-13 16:19:41 +090058#define CREATE_TRACE_POINTS
59#include <trace/events/mce.h>
60
Andi Kleen3c079792009-05-27 21:56:55 +020061#define SPINUNIT 100 /* 100ns */
62
Andi Kleen553f2652006-04-07 19:49:57 +020063atomic_t mce_entry;
64
Andi Kleen01ca79f2009-05-27 21:56:52 +020065DEFINE_PER_CPU(unsigned, mce_exception_count);
66
Borislav Petkov14625942012-10-17 12:05:33 +020067struct mce_bank *mce_banks __read_mostly;
Andi Kleencebe1822009-07-09 00:31:43 +020068
Borislav Petkovd203f0b2012-10-15 18:03:57 +020069struct mca_config mca_cfg __read_mostly = {
Borislav Petkov84c25592012-10-15 19:59:18 +020070 .bootlog = -1,
Borislav Petkovd203f0b2012-10-15 18:03:57 +020071 /*
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 */
Borislav Petkov84c25592012-10-15 19:59:18 +020078 .tolerant = 1,
79 .monarch_timeout = -1
Borislav Petkovd203f0b2012-10-15 18:03:57 +020080};
81
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +090082/* 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 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Hidetoshi Seto93b62c32011-06-08 11:00:45 +090087static DECLARE_WAIT_QUEUE_HEAD(mce_chrdev_wait);
88
Andi Kleen3c079792009-05-27 21:56:55 +020089static DEFINE_PER_CPU(struct mce, mces_seen);
90static int cpu_missing;
91
Andi Kleenee031c32009-02-12 13:49:34 +010092/* MCA banks polled by the period polling timer for corrected events */
93DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
94 [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
95};
96
Andi Kleen9b1beaf2009-05-27 21:56:59 +020097static DEFINE_PER_CPU(struct work_struct, mce_work);
98
Tony Luck61b0fcc2012-07-19 11:28:46 -070099static void (*quirk_no_way_out)(int bank, struct mce *m, struct pt_regs *regs);
100
Borislav Petkov3653ada2011-12-04 15:12:09 +0100101/*
102 * CPU/chipset specific EDAC code can register a notifier call here to print
103 * MCE errors in a human-readable form.
104 */
105ATOMIC_NOTIFIER_HEAD(x86_mce_decoder_chain);
106
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100107/* Do initial initialization of a struct mce */
108void mce_setup(struct mce *m)
109{
110 memset(m, 0, sizeof(struct mce));
Andi Kleend620c672009-05-27 21:56:56 +0200111 m->cpu = m->extcpu = smp_processor_id();
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100112 rdtscll(m->tsc);
Andi Kleen8ee08342009-05-27 21:56:56 +0200113 /* We hope get_seconds stays lockless */
114 m->time = get_seconds();
115 m->cpuvendor = boot_cpu_data.x86_vendor;
116 m->cpuid = cpuid_eax(1);
Andi Kleen8ee08342009-05-27 21:56:56 +0200117 m->socketid = cpu_data(m->extcpu).phys_proc_id;
Andi Kleen8ee08342009-05-27 21:56:56 +0200118 m->apicid = cpu_data(m->extcpu).initial_apicid;
119 rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
Andi Kleenb5f2fa42009-02-12 13:43:22 +0100120}
121
Andi Kleenea149b32009-04-29 19:31:00 +0200122DEFINE_PER_CPU(struct mce, injectm);
123EXPORT_PER_CPU_SYMBOL_GPL(injectm);
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
126 * Lockless MCE logging infrastructure.
127 * This avoids deadlocks on printk locks without having to break locks. Also
128 * separate MCEs from kernel messages to avoid bogus bug reports.
129 */
130
Adrian Bunk231fd902008-01-30 13:30:30 +0100131static struct mce_log mcelog = {
Andi Kleenf6fb0ac2009-05-27 21:56:55 +0200132 .signature = MCE_LOG_SIGNATURE,
133 .len = MCE_LOG_LEN,
134 .recordlen = sizeof(struct mce),
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200135};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137void mce_log(struct mce *mce)
138{
139 unsigned next, entry;
Borislav Petkovf0cb5452011-07-18 11:24:45 -0300140 int ret = 0;
Ingo Molnare9eee032009-04-08 12:31:17 +0200141
Hidetoshi Seto8968f9d2009-10-13 16:19:41 +0900142 /* Emit the trace record: */
143 trace_mce_record(mce);
144
Borislav Petkovf0cb5452011-07-18 11:24:45 -0300145 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, mce);
146 if (ret == NOTIFY_STOP)
147 return;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 mce->finished = 0;
Mike Waychison76441432005-09-30 00:01:27 +0200150 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 for (;;) {
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -0800152 entry = rcu_dereference_check_mce(mcelog.next);
Andi Kleen673242c2005-09-12 18:49:24 +0200153 for (;;) {
Mauro Carvalho Chehab696e4092009-07-23 06:57:45 -0300154
155 /*
Ingo Molnare9eee032009-04-08 12:31:17 +0200156 * When the buffer fills up discard new entries.
157 * Assume that the earlier errors are the more
158 * interesting ones:
159 */
Andi Kleen673242c2005-09-12 18:49:24 +0200160 if (entry >= MCE_LOG_LEN) {
Hidetoshi Seto14a02532009-04-30 16:04:51 +0900161 set_bit(MCE_OVERFLOW,
162 (unsigned long *)&mcelog.flags);
Andi Kleen673242c2005-09-12 18:49:24 +0200163 return;
164 }
Ingo Molnare9eee032009-04-08 12:31:17 +0200165 /* Old left over entry. Skip: */
Andi Kleen673242c2005-09-12 18:49:24 +0200166 if (mcelog.entry[entry].finished) {
167 entry++;
168 continue;
169 }
Mike Waychison76441432005-09-30 00:01:27 +0200170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 smp_rmb();
173 next = entry + 1;
174 if (cmpxchg(&mcelog.next, entry, next) == entry)
175 break;
176 }
177 memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
Mike Waychison76441432005-09-30 00:01:27 +0200178 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 mcelog.entry[entry].finished = 1;
Mike Waychison76441432005-09-30 00:01:27 +0200180 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Andi Kleena0189c72009-05-27 21:56:54 +0200182 mce->finished = 1;
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +0900183 set_bit(0, &mce_need_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Borislav Petkov09371952011-12-08 12:28:33 +0100186static void drain_mcelog_buffer(void)
187{
188 unsigned int next, i, prev = 0;
189
Srivatsa S. Bhatb11e3d72012-03-07 11:44:29 +0100190 next = ACCESS_ONCE(mcelog.next);
Borislav Petkov09371952011-12-08 12:28:33 +0100191
192 do {
193 struct mce *m;
194
195 /* drain what was logged during boot */
196 for (i = prev; i < next; i++) {
197 unsigned long start = jiffies;
198 unsigned retries = 1;
199
200 m = &mcelog.entry[i];
201
202 while (!m->finished) {
203 if (time_after_eq(jiffies, start + 2*retries))
204 retries++;
205
206 cpu_relax();
207
208 if (!m->finished && retries >= 4) {
Joe Perchesc767a542012-05-21 19:50:07 -0700209 pr_err("skipping error being logged currently!\n");
Borislav Petkov09371952011-12-08 12:28:33 +0100210 break;
211 }
212 }
213 smp_rmb();
214 atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
215 }
216
217 memset(mcelog.entry + prev, 0, (next - prev) * sizeof(*m));
218 prev = next;
219 next = cmpxchg(&mcelog.next, prev, 0);
220 } while (next != prev);
221}
222
223
Borislav Petkov3653ada2011-12-04 15:12:09 +0100224void mce_register_decode_chain(struct notifier_block *nb)
225{
226 atomic_notifier_chain_register(&x86_mce_decoder_chain, nb);
Borislav Petkov09371952011-12-08 12:28:33 +0100227 drain_mcelog_buffer();
Borislav Petkov3653ada2011-12-04 15:12:09 +0100228}
229EXPORT_SYMBOL_GPL(mce_register_decode_chain);
230
231void mce_unregister_decode_chain(struct notifier_block *nb)
232{
233 atomic_notifier_chain_unregister(&x86_mce_decoder_chain, nb);
234}
235EXPORT_SYMBOL_GPL(mce_unregister_decode_chain);
236
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900237static void print_mce(struct mce *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Borislav Petkovdffa4b22011-04-20 12:23:49 +0200239 int ret = 0;
240
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800241 pr_emerg(HW_ERR "CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
Andi Kleend620c672009-05-27 21:56:56 +0200242 m->extcpu, m->mcgstatus, m->bank, m->status);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200243
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100244 if (m->ip) {
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800245 pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200246 !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
247 m->cs, m->ip);
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (m->cs == __KERNEL_CS)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100250 print_symbol("{%s}", m->ip);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200251 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Borislav Petkov549d0422009-07-24 13:51:42 +0200253
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800254 pr_emerg(HW_ERR "TSC %llx ", m->tsc);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200255 if (m->addr)
256 pr_cont("ADDR %llx ", m->addr);
257 if (m->misc)
258 pr_cont("MISC %llx ", m->misc);
259
260 pr_cont("\n");
Andi Kleen506ed6b2011-10-12 17:46:33 -0700261 /*
262 * Note this output is parsed by external tools and old fields
263 * should not be changed.
264 */
Borislav Petkov881e23e2011-10-17 16:45:10 +0200265 pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x microcode %x\n",
Andi Kleen506ed6b2011-10-12 17:46:33 -0700266 m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid,
267 cpu_data(m->extcpu).microcode);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200268
269 /*
270 * Print out human-readable details about the MCE error,
Borislav Petkovfb253192009-10-07 13:20:38 +0200271 * (if the CPU has an implementation for that)
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200272 */
Borislav Petkovdffa4b22011-04-20 12:23:49 +0200273 ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
274 if (ret == NOTIFY_STOP)
275 return;
276
277 pr_emerg_ratelimited(HW_ERR "Run the above through 'mcelog --ascii'\n");
Andi Kleen86503562009-05-27 21:56:58 +0200278}
279
Andi Kleenf94b61c2009-05-27 21:56:55 +0200280#define PANIC_TIMEOUT 5 /* 5 seconds */
281
282static atomic_t mce_paniced;
283
Huang Yingbf783f92009-07-31 09:41:43 +0800284static int fake_panic;
285static atomic_t mce_fake_paniced;
286
Andi Kleenf94b61c2009-05-27 21:56:55 +0200287/* Panic in progress. Enable interrupts and wait for final IPI */
288static void wait_for_panic(void)
289{
290 long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200291
Andi Kleenf94b61c2009-05-27 21:56:55 +0200292 preempt_disable();
293 local_irq_enable();
294 while (timeout-- > 0)
295 udelay(1);
Andi Kleen29b0f592009-05-27 21:56:56 +0200296 if (panic_timeout == 0)
Borislav Petkov7af19e42012-10-15 20:25:17 +0200297 panic_timeout = mca_cfg.panic_timeout;
Andi Kleenf94b61c2009-05-27 21:56:55 +0200298 panic("Panicing machine check CPU died");
299}
300
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200301static void mce_panic(char *msg, struct mce *final, char *exp)
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200302{
Huang Ying482908b2010-05-18 14:35:22 +0800303 int i, apei_err = 0;
Tim Hockine02e68d2007-07-21 17:10:36 +0200304
Huang Yingbf783f92009-07-31 09:41:43 +0800305 if (!fake_panic) {
306 /*
307 * Make sure only one CPU runs in machine check panic
308 */
309 if (atomic_inc_return(&mce_paniced) > 1)
310 wait_for_panic();
311 barrier();
Andi Kleenf94b61c2009-05-27 21:56:55 +0200312
Huang Yingbf783f92009-07-31 09:41:43 +0800313 bust_spinlocks(1);
314 console_verbose();
315 } else {
316 /* Don't log too much for fake panic */
317 if (atomic_inc_return(&mce_fake_paniced) > 1)
318 return;
319 }
Andi Kleena0189c72009-05-27 21:56:54 +0200320 /* First print corrected ones that are still unlogged */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 for (i = 0; i < MCE_LOG_LEN; i++) {
Andi Kleena0189c72009-05-27 21:56:54 +0200322 struct mce *m = &mcelog.entry[i];
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900323 if (!(m->status & MCI_STATUS_VAL))
324 continue;
Huang Ying482908b2010-05-18 14:35:22 +0800325 if (!(m->status & MCI_STATUS_UC)) {
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900326 print_mce(m);
Huang Ying482908b2010-05-18 14:35:22 +0800327 if (!apei_err)
328 apei_err = apei_write_mce(m);
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Andi Kleena0189c72009-05-27 21:56:54 +0200331 /* Now print uncorrected but with the final one last */
332 for (i = 0; i < MCE_LOG_LEN; i++) {
333 struct mce *m = &mcelog.entry[i];
334 if (!(m->status & MCI_STATUS_VAL))
335 continue;
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900336 if (!(m->status & MCI_STATUS_UC))
337 continue;
Huang Ying482908b2010-05-18 14:35:22 +0800338 if (!final || memcmp(m, final, sizeof(struct mce))) {
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900339 print_mce(m);
Huang Ying482908b2010-05-18 14:35:22 +0800340 if (!apei_err)
341 apei_err = apei_write_mce(m);
342 }
Andi Kleena0189c72009-05-27 21:56:54 +0200343 }
Huang Ying482908b2010-05-18 14:35:22 +0800344 if (final) {
Hidetoshi Seto77e26cc2009-06-11 16:04:35 +0900345 print_mce(final);
Huang Ying482908b2010-05-18 14:35:22 +0800346 if (!apei_err)
347 apei_err = apei_write_mce(final);
348 }
Andi Kleen3c079792009-05-27 21:56:55 +0200349 if (cpu_missing)
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800350 pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200351 if (exp)
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800352 pr_emerg(HW_ERR "Machine check: %s\n", exp);
Huang Yingbf783f92009-07-31 09:41:43 +0800353 if (!fake_panic) {
354 if (panic_timeout == 0)
Borislav Petkov7af19e42012-10-15 20:25:17 +0200355 panic_timeout = mca_cfg.panic_timeout;
Huang Yingbf783f92009-07-31 09:41:43 +0800356 panic(msg);
357 } else
Huang Yinga2d7b0d2010-06-08 14:35:39 +0800358 pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200359}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Andi Kleenea149b32009-04-29 19:31:00 +0200361/* Support code for software error injection */
362
363static int msr_to_offset(u32 msr)
364{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100365 unsigned bank = __this_cpu_read(injectm.bank);
Ingo Molnarf436f8b2009-10-01 16:14:32 +0200366
Borislav Petkov84c25592012-10-15 19:59:18 +0200367 if (msr == mca_cfg.rip_msr)
Andi Kleenea149b32009-04-29 19:31:00 +0200368 return offsetof(struct mce, ip);
Andi Kleena2d32bc2009-07-09 00:31:44 +0200369 if (msr == MSR_IA32_MCx_STATUS(bank))
Andi Kleenea149b32009-04-29 19:31:00 +0200370 return offsetof(struct mce, status);
Andi Kleena2d32bc2009-07-09 00:31:44 +0200371 if (msr == MSR_IA32_MCx_ADDR(bank))
Andi Kleenea149b32009-04-29 19:31:00 +0200372 return offsetof(struct mce, addr);
Andi Kleena2d32bc2009-07-09 00:31:44 +0200373 if (msr == MSR_IA32_MCx_MISC(bank))
Andi Kleenea149b32009-04-29 19:31:00 +0200374 return offsetof(struct mce, misc);
375 if (msr == MSR_IA32_MCG_STATUS)
376 return offsetof(struct mce, mcgstatus);
377 return -1;
378}
379
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200380/* MSR access wrappers used for error injection */
381static u64 mce_rdmsrl(u32 msr)
382{
383 u64 v;
Ingo Molnar11868a22009-09-23 17:49:55 +0200384
Tejun Heo0a3aee02010-12-18 16:28:55 +0100385 if (__this_cpu_read(injectm.finished)) {
Andi Kleenea149b32009-04-29 19:31:00 +0200386 int offset = msr_to_offset(msr);
Ingo Molnar11868a22009-09-23 17:49:55 +0200387
Andi Kleenea149b32009-04-29 19:31:00 +0200388 if (offset < 0)
389 return 0;
390 return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
391 }
Ingo Molnar11868a22009-09-23 17:49:55 +0200392
393 if (rdmsrl_safe(msr, &v)) {
394 WARN_ONCE(1, "mce: Unable to read msr %d!\n", msr);
395 /*
396 * Return zero in case the access faulted. This should
397 * not happen normally but can happen if the CPU does
398 * something weird, or if the code is buggy.
399 */
400 v = 0;
401 }
402
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200403 return v;
404}
405
406static void mce_wrmsrl(u32 msr, u64 v)
407{
Tejun Heo0a3aee02010-12-18 16:28:55 +0100408 if (__this_cpu_read(injectm.finished)) {
Andi Kleenea149b32009-04-29 19:31:00 +0200409 int offset = msr_to_offset(msr);
Ingo Molnar11868a22009-09-23 17:49:55 +0200410
Andi Kleenea149b32009-04-29 19:31:00 +0200411 if (offset >= 0)
412 *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
413 return;
414 }
Andi Kleen5f8c1a52009-04-29 19:29:12 +0200415 wrmsrl(msr, v);
416}
417
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200418/*
Hidetoshi Setob8325c52011-06-08 10:57:46 +0900419 * Collect all global (w.r.t. this processor) status about this machine
420 * check into our "mce" struct so that we can use it later to assess
421 * the severity of the problem as we read per-bank specific details.
422 */
423static inline void mce_gather_info(struct mce *m, struct pt_regs *regs)
424{
425 mce_setup(m);
426
427 m->mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
428 if (regs) {
429 /*
430 * Get the address of the instruction at the time of
431 * the machine check error.
432 */
433 if (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) {
434 m->ip = regs->ip;
435 m->cs = regs->cs;
Andi Kleena129a7c2010-11-19 13:16:22 +0100436
437 /*
438 * When in VM86 mode make the cs look like ring 3
439 * always. This is a lie, but it's better than passing
440 * the additional vm86 bit around everywhere.
441 */
442 if (v8086_mode(regs))
443 m->cs |= 3;
Hidetoshi Setob8325c52011-06-08 10:57:46 +0900444 }
445 /* Use accurate RIP reporting if available. */
Borislav Petkov84c25592012-10-15 19:59:18 +0200446 if (mca_cfg.rip_msr)
447 m->ip = mce_rdmsrl(mca_cfg.rip_msr);
Hidetoshi Setob8325c52011-06-08 10:57:46 +0900448 }
449}
450
451/*
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200452 * Simple lockless ring to communicate PFNs from the exception handler with the
453 * process context work function. This is vastly simplified because there's
454 * only a single reader and a single writer.
455 */
456#define MCE_RING_SIZE 16 /* we use one entry less */
457
458struct mce_ring {
459 unsigned short start;
460 unsigned short end;
461 unsigned long ring[MCE_RING_SIZE];
462};
463static DEFINE_PER_CPU(struct mce_ring, mce_ring);
464
465/* Runs with CPU affinity in workqueue */
466static int mce_ring_empty(void)
467{
468 struct mce_ring *r = &__get_cpu_var(mce_ring);
469
470 return r->start == r->end;
471}
472
473static int mce_ring_get(unsigned long *pfn)
474{
475 struct mce_ring *r;
476 int ret = 0;
477
478 *pfn = 0;
479 get_cpu();
480 r = &__get_cpu_var(mce_ring);
481 if (r->start == r->end)
482 goto out;
483 *pfn = r->ring[r->start];
484 r->start = (r->start + 1) % MCE_RING_SIZE;
485 ret = 1;
486out:
487 put_cpu();
488 return ret;
489}
490
491/* Always runs in MCE context with preempt off */
492static int mce_ring_add(unsigned long pfn)
493{
494 struct mce_ring *r = &__get_cpu_var(mce_ring);
495 unsigned next;
496
497 next = (r->end + 1) % MCE_RING_SIZE;
498 if (next == r->start)
499 return -1;
500 r->ring[r->end] = pfn;
501 wmb();
502 r->end = next;
503 return 0;
504}
505
Andi Kleen88ccbed2009-02-12 13:49:36 +0100506int mce_available(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Borislav Petkov14625942012-10-17 12:05:33 +0200508 if (mca_cfg.disabled)
Andi Kleen5b4408f2009-02-12 13:39:30 +0100509 return 0;
Akinobu Mita3d1712c2006-03-24 03:15:11 -0800510 return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200513static void mce_schedule_work(void)
514{
Tejun Heo4d899be2012-12-21 17:57:05 -0800515 if (!mce_ring_empty())
516 schedule_work(&__get_cpu_var(mce_work));
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200517}
518
Hidetoshi Setob77e70b2011-06-08 10:56:02 +0900519DEFINE_PER_CPU(struct irq_work, mce_irq_work);
520
521static void mce_irq_work_cb(struct irq_work *entry)
Andi Kleenccc3c312009-05-27 21:56:54 +0200522{
Andi Kleen9ff36ee2009-05-27 21:56:58 +0200523 mce_notify_irq();
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200524 mce_schedule_work();
Andi Kleenccc3c312009-05-27 21:56:54 +0200525}
Andi Kleenccc3c312009-05-27 21:56:54 +0200526
527static void mce_report_event(struct pt_regs *regs)
528{
529 if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
Andi Kleen9ff36ee2009-05-27 21:56:58 +0200530 mce_notify_irq();
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200531 /*
532 * Triggering the work queue here is just an insurance
533 * policy in case the syscall exit notify handler
534 * doesn't run soon enough or ends up running on the
535 * wrong CPU (can happen when audit sleeps)
536 */
537 mce_schedule_work();
Andi Kleenccc3c312009-05-27 21:56:54 +0200538 return;
539 }
540
Hidetoshi Setob77e70b2011-06-08 10:56:02 +0900541 irq_work_queue(&__get_cpu_var(mce_irq_work));
Andi Kleenccc3c312009-05-27 21:56:54 +0200542}
543
Tony Luck85f926942011-12-13 09:48:13 -0800544/*
545 * Read ADDR and MISC registers.
546 */
547static void mce_read_aux(struct mce *m, int i)
548{
549 if (m->status & MCI_STATUS_MISCV)
550 m->misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
551 if (m->status & MCI_STATUS_ADDRV) {
552 m->addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
553
554 /*
555 * Mask the reported address by the reported granularity.
556 */
Borislav Petkov14625942012-10-17 12:05:33 +0200557 if (mca_cfg.ser && (m->status & MCI_STATUS_MISCV)) {
Tony Luck85f926942011-12-13 09:48:13 -0800558 u8 shift = MCI_MISC_ADDR_LSB(m->misc);
559 m->addr >>= shift;
560 m->addr <<= shift;
561 }
562 }
563}
564
Andi Kleenca84f692009-05-27 21:56:57 +0200565DEFINE_PER_CPU(unsigned, mce_poll_count);
566
Thomas Gleixnerd88203d2007-10-23 22:37:23 +0200567/*
Andi Kleenb79109c2009-02-12 13:43:23 +0100568 * Poll for corrected events or events that happened before reset.
569 * Those are just logged through /dev/mcelog.
570 *
571 * This is executed in standard interrupt context.
Andi Kleened7290d2009-05-27 21:56:57 +0200572 *
573 * Note: spec recommends to panic for fatal unsignalled
574 * errors here. However this would be quite problematic --
575 * we would need to reimplement the Monarch handling and
576 * it would mess up the exclusion between exception handler
577 * and poll hander -- * so we skip this for now.
578 * These cases should not happen anyways, or only when the CPU
579 * is already totally * confused. In this case it's likely it will
580 * not fully execute the machine check handler either.
Andi Kleenb79109c2009-02-12 13:43:23 +0100581 */
Andi Kleenee031c32009-02-12 13:49:34 +0100582void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
Andi Kleenb79109c2009-02-12 13:43:23 +0100583{
584 struct mce m;
585 int i;
586
Alex Shic6ae41e2012-05-11 15:35:27 +0800587 this_cpu_inc(mce_poll_count);
Andi Kleenca84f692009-05-27 21:56:57 +0200588
Hidetoshi Setob8325c52011-06-08 10:57:46 +0900589 mce_gather_info(&m, NULL);
Andi Kleenb79109c2009-02-12 13:43:23 +0100590
Borislav Petkovd203f0b2012-10-15 18:03:57 +0200591 for (i = 0; i < mca_cfg.banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +0200592 if (!mce_banks[i].ctl || !test_bit(i, *b))
Andi Kleenb79109c2009-02-12 13:43:23 +0100593 continue;
594
595 m.misc = 0;
596 m.addr = 0;
597 m.bank = i;
598 m.tsc = 0;
599
600 barrier();
Andi Kleena2d32bc2009-07-09 00:31:44 +0200601 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
Andi Kleenb79109c2009-02-12 13:43:23 +0100602 if (!(m.status & MCI_STATUS_VAL))
603 continue;
604
605 /*
Andi Kleened7290d2009-05-27 21:56:57 +0200606 * Uncorrected or signalled events are handled by the exception
607 * handler when it is enabled, so don't process those here.
Andi Kleenb79109c2009-02-12 13:43:23 +0100608 *
609 * TBD do the same check for MCI_STATUS_EN here?
610 */
Andi Kleened7290d2009-05-27 21:56:57 +0200611 if (!(flags & MCP_UC) &&
Borislav Petkov14625942012-10-17 12:05:33 +0200612 (m.status & (mca_cfg.ser ? MCI_STATUS_S : MCI_STATUS_UC)))
Andi Kleenb79109c2009-02-12 13:43:23 +0100613 continue;
614
Tony Luck85f926942011-12-13 09:48:13 -0800615 mce_read_aux(&m, i);
Andi Kleenb79109c2009-02-12 13:43:23 +0100616
617 if (!(flags & MCP_TIMESTAMP))
618 m.tsc = 0;
619 /*
620 * Don't get the IP here because it's unlikely to
621 * have anything to do with the actual error location.
622 */
Borislav Petkovd203f0b2012-10-15 18:03:57 +0200623 if (!(flags & MCP_DONTLOG) && !mca_cfg.dont_log_ce)
Andi Kleen5679af42009-04-07 17:06:55 +0200624 mce_log(&m);
Andi Kleenb79109c2009-02-12 13:43:23 +0100625
626 /*
627 * Clear state for this bank.
628 */
Andi Kleena2d32bc2009-07-09 00:31:44 +0200629 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
Andi Kleenb79109c2009-02-12 13:43:23 +0100630 }
631
632 /*
633 * Don't clear MCG_STATUS here because it's only defined for
634 * exceptions.
635 */
Andi Kleen88921be2009-05-27 21:56:51 +0200636
637 sync_core();
Andi Kleenb79109c2009-02-12 13:43:23 +0100638}
Andi Kleenea149b32009-04-29 19:31:00 +0200639EXPORT_SYMBOL_GPL(machine_check_poll);
Andi Kleenb79109c2009-02-12 13:43:23 +0100640
641/*
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200642 * Do a quick check if any of the events requires a panic.
643 * This decides if we keep the events around or clear them.
644 */
Tony Luck61b0fcc2012-07-19 11:28:46 -0700645static int mce_no_way_out(struct mce *m, char **msg, unsigned long *validp,
646 struct pt_regs *regs)
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200647{
Tony Luck95022b82012-04-18 15:19:40 -0700648 int i, ret = 0;
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200649
Borislav Petkovd203f0b2012-10-15 18:03:57 +0200650 for (i = 0; i < mca_cfg.banks; i++) {
Andi Kleena2d32bc2009-07-09 00:31:44 +0200651 m->status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
Tony Luck61b0fcc2012-07-19 11:28:46 -0700652 if (m->status & MCI_STATUS_VAL) {
Tony Luck95022b82012-04-18 15:19:40 -0700653 __set_bit(i, validp);
Tony Luck61b0fcc2012-07-19 11:28:46 -0700654 if (quirk_no_way_out)
655 quirk_no_way_out(i, m, regs);
656 }
Borislav Petkovd203f0b2012-10-15 18:03:57 +0200657 if (mce_severity(m, mca_cfg.tolerant, msg) >= MCE_PANIC_SEVERITY)
Tony Luck95022b82012-04-18 15:19:40 -0700658 ret = 1;
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200659 }
Tony Luck95022b82012-04-18 15:19:40 -0700660 return ret;
Andi Kleenbd19a5e2009-05-27 21:56:55 +0200661}
662
663/*
Andi Kleen3c079792009-05-27 21:56:55 +0200664 * Variable to establish order between CPUs while scanning.
665 * Each CPU spins initially until executing is equal its number.
666 */
667static atomic_t mce_executing;
668
669/*
670 * Defines order of CPUs on entry. First CPU becomes Monarch.
671 */
672static atomic_t mce_callin;
673
674/*
675 * Check if a timeout waiting for other CPUs happened.
676 */
677static int mce_timed_out(u64 *t)
678{
679 /*
680 * The others already did panic for some reason.
681 * Bail out like in a timeout.
682 * rmb() to tell the compiler that system_state
683 * might have been modified by someone else.
684 */
685 rmb();
686 if (atomic_read(&mce_paniced))
687 wait_for_panic();
Borislav Petkov84c25592012-10-15 19:59:18 +0200688 if (!mca_cfg.monarch_timeout)
Andi Kleen3c079792009-05-27 21:56:55 +0200689 goto out;
690 if ((s64)*t < SPINUNIT) {
691 /* CHECKME: Make panic default for 1 too? */
Borislav Petkovd203f0b2012-10-15 18:03:57 +0200692 if (mca_cfg.tolerant < 1)
Andi Kleen3c079792009-05-27 21:56:55 +0200693 mce_panic("Timeout synchronizing machine check over CPUs",
694 NULL, NULL);
695 cpu_missing = 1;
696 return 1;
697 }
698 *t -= SPINUNIT;
699out:
700 touch_nmi_watchdog();
701 return 0;
702}
703
704/*
705 * The Monarch's reign. The Monarch is the CPU who entered
706 * the machine check handler first. It waits for the others to
707 * raise the exception too and then grades them. When any
708 * error is fatal panic. Only then let the others continue.
709 *
710 * The other CPUs entering the MCE handler will be controlled by the
711 * Monarch. They are called Subjects.
712 *
713 * This way we prevent any potential data corruption in a unrecoverable case
714 * and also makes sure always all CPU's errors are examined.
715 *
Hidetoshi Seto680b6cf2009-08-26 16:20:36 +0900716 * Also this detects the case of a machine check event coming from outer
Andi Kleen3c079792009-05-27 21:56:55 +0200717 * space (not detected by any CPUs) In this case some external agent wants
718 * us to shut down, so panic too.
719 *
720 * The other CPUs might still decide to panic if the handler happens
721 * in a unrecoverable place, but in this case the system is in a semi-stable
722 * state and won't corrupt anything by itself. It's ok to let the others
723 * continue for a bit first.
724 *
725 * All the spin loops have timeouts; when a timeout happens a CPU
726 * typically elects itself to be Monarch.
727 */
728static void mce_reign(void)
729{
730 int cpu;
731 struct mce *m = NULL;
732 int global_worst = 0;
733 char *msg = NULL;
734 char *nmsg = NULL;
735
736 /*
737 * This CPU is the Monarch and the other CPUs have run
738 * through their handlers.
739 * Grade the severity of the errors of all the CPUs.
740 */
741 for_each_possible_cpu(cpu) {
Borislav Petkovd203f0b2012-10-15 18:03:57 +0200742 int severity = mce_severity(&per_cpu(mces_seen, cpu),
743 mca_cfg.tolerant,
Andi Kleen3c079792009-05-27 21:56:55 +0200744 &nmsg);
745 if (severity > global_worst) {
746 msg = nmsg;
747 global_worst = severity;
748 m = &per_cpu(mces_seen, cpu);
749 }
750 }
751
752 /*
753 * Cannot recover? Panic here then.
754 * This dumps all the mces in the log buffer and stops the
755 * other CPUs.
756 */
Borislav Petkovd203f0b2012-10-15 18:03:57 +0200757 if (m && global_worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3)
Andi Kleenac960372009-05-27 21:56:58 +0200758 mce_panic("Fatal Machine check", m, msg);
Andi Kleen3c079792009-05-27 21:56:55 +0200759
760 /*
761 * For UC somewhere we let the CPU who detects it handle it.
762 * Also must let continue the others, otherwise the handling
763 * CPU could deadlock on a lock.
764 */
765
766 /*
767 * No machine check event found. Must be some external
768 * source or one CPU is hung. Panic.
769 */
Borislav Petkovd203f0b2012-10-15 18:03:57 +0200770 if (global_worst <= MCE_KEEP_SEVERITY && mca_cfg.tolerant < 3)
Andi Kleen3c079792009-05-27 21:56:55 +0200771 mce_panic("Machine check from unknown source", NULL, NULL);
772
773 /*
774 * Now clear all the mces_seen so that they don't reappear on
775 * the next mce.
776 */
777 for_each_possible_cpu(cpu)
778 memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
779}
780
781static atomic_t global_nwo;
782
783/*
784 * Start of Monarch synchronization. This waits until all CPUs have
785 * entered the exception handler and then determines if any of them
786 * saw a fatal event that requires panic. Then it executes them
787 * in the entry order.
788 * TBD double check parallel CPU hotunplug
789 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900790static int mce_start(int *no_way_out)
Andi Kleen3c079792009-05-27 21:56:55 +0200791{
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900792 int order;
Andi Kleen3c079792009-05-27 21:56:55 +0200793 int cpus = num_online_cpus();
Borislav Petkov84c25592012-10-15 19:59:18 +0200794 u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
Andi Kleen3c079792009-05-27 21:56:55 +0200795
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900796 if (!timeout)
797 return -1;
Andi Kleen3c079792009-05-27 21:56:55 +0200798
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900799 atomic_add(*no_way_out, &global_nwo);
Huang Ying184e1fd2009-06-15 15:37:07 +0800800 /*
801 * global_nwo should be updated before mce_callin
802 */
803 smp_wmb();
Borislav Petkova95436e2009-06-20 23:28:22 -0700804 order = atomic_inc_return(&mce_callin);
Andi Kleen3c079792009-05-27 21:56:55 +0200805
806 /*
807 * Wait for everyone.
808 */
809 while (atomic_read(&mce_callin) != cpus) {
810 if (mce_timed_out(&timeout)) {
811 atomic_set(&global_nwo, 0);
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900812 return -1;
Andi Kleen3c079792009-05-27 21:56:55 +0200813 }
814 ndelay(SPINUNIT);
815 }
816
817 /*
Huang Ying184e1fd2009-06-15 15:37:07 +0800818 * mce_callin should be read before global_nwo
819 */
820 smp_rmb();
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900821
822 if (order == 1) {
823 /*
824 * Monarch: Starts executing now, the others wait.
825 */
826 atomic_set(&mce_executing, 1);
827 } else {
828 /*
829 * Subject: Now start the scanning loop one by one in
830 * the original callin order.
831 * This way when there are any shared banks it will be
832 * only seen by one CPU before cleared, avoiding duplicates.
833 */
834 while (atomic_read(&mce_executing) < order) {
835 if (mce_timed_out(&timeout)) {
836 atomic_set(&global_nwo, 0);
837 return -1;
838 }
839 ndelay(SPINUNIT);
840 }
841 }
842
Huang Ying184e1fd2009-06-15 15:37:07 +0800843 /*
Andi Kleen3c079792009-05-27 21:56:55 +0200844 * Cache the global no_way_out state.
845 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900846 *no_way_out = atomic_read(&global_nwo);
Andi Kleen3c079792009-05-27 21:56:55 +0200847
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +0900848 return order;
Andi Kleen3c079792009-05-27 21:56:55 +0200849}
850
851/*
852 * Synchronize between CPUs after main scanning loop.
853 * This invokes the bulk of the Monarch processing.
854 */
855static int mce_end(int order)
856{
857 int ret = -1;
Borislav Petkov84c25592012-10-15 19:59:18 +0200858 u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
Andi Kleen3c079792009-05-27 21:56:55 +0200859
860 if (!timeout)
861 goto reset;
862 if (order < 0)
863 goto reset;
864
865 /*
866 * Allow others to run.
867 */
868 atomic_inc(&mce_executing);
869
870 if (order == 1) {
871 /* CHECKME: Can this race with a parallel hotplug? */
872 int cpus = num_online_cpus();
873
874 /*
875 * Monarch: Wait for everyone to go through their scanning
876 * loops.
877 */
878 while (atomic_read(&mce_executing) <= cpus) {
879 if (mce_timed_out(&timeout))
880 goto reset;
881 ndelay(SPINUNIT);
882 }
883
884 mce_reign();
885 barrier();
886 ret = 0;
887 } else {
888 /*
889 * Subject: Wait for Monarch to finish.
890 */
891 while (atomic_read(&mce_executing) != 0) {
892 if (mce_timed_out(&timeout))
893 goto reset;
894 ndelay(SPINUNIT);
895 }
896
897 /*
898 * Don't reset anything. That's done by the Monarch.
899 */
900 return 0;
901 }
902
903 /*
904 * Reset all global state.
905 */
906reset:
907 atomic_set(&global_nwo, 0);
908 atomic_set(&mce_callin, 0);
909 barrier();
910
911 /*
912 * Let others run again.
913 */
914 atomic_set(&mce_executing, 0);
915 return ret;
916}
917
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200918/*
919 * Check if the address reported by the CPU is in a format we can parse.
920 * It would be possible to add code for most other cases, but all would
921 * be somewhat complicated (e.g. segment offset would require an instruction
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300922 * parser). So only support physical addresses up to page granuality for now.
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200923 */
924static int mce_usable_address(struct mce *m)
925{
926 if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
927 return 0;
Hidetoshi Seto2b90e772011-06-08 10:56:56 +0900928 if (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200929 return 0;
Hidetoshi Seto2b90e772011-06-08 10:56:56 +0900930 if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_ADDR_PHYS)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200931 return 0;
932 return 1;
933}
934
Andi Kleen3c079792009-05-27 21:56:55 +0200935static void mce_clear_state(unsigned long *toclear)
936{
937 int i;
938
Borislav Petkovd203f0b2012-10-15 18:03:57 +0200939 for (i = 0; i < mca_cfg.banks; i++) {
Andi Kleen3c079792009-05-27 21:56:55 +0200940 if (test_bit(i, toclear))
Andi Kleena2d32bc2009-07-09 00:31:44 +0200941 mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
Andi Kleen3c079792009-05-27 21:56:55 +0200942 }
943}
944
945/*
Tony Luckaf104e32011-12-14 15:55:20 -0800946 * Need to save faulting physical address associated with a process
947 * in the machine check handler some place where we can grab it back
948 * later in mce_notify_process()
949 */
950#define MCE_INFO_MAX 16
951
952struct mce_info {
953 atomic_t inuse;
954 struct task_struct *t;
955 __u64 paddr;
Tony Luckdad17432012-05-14 15:07:48 -0700956 int restartable;
Tony Luckaf104e32011-12-14 15:55:20 -0800957} mce_info[MCE_INFO_MAX];
958
Tony Luckdad17432012-05-14 15:07:48 -0700959static void mce_save_info(__u64 addr, int c)
Tony Luckaf104e32011-12-14 15:55:20 -0800960{
961 struct mce_info *mi;
962
963 for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++) {
964 if (atomic_cmpxchg(&mi->inuse, 0, 1) == 0) {
965 mi->t = current;
966 mi->paddr = addr;
Tony Luckdad17432012-05-14 15:07:48 -0700967 mi->restartable = c;
Tony Luckaf104e32011-12-14 15:55:20 -0800968 return;
969 }
970 }
971
972 mce_panic("Too many concurrent recoverable errors", NULL, NULL);
973}
974
975static struct mce_info *mce_find_info(void)
976{
977 struct mce_info *mi;
978
979 for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++)
980 if (atomic_read(&mi->inuse) && mi->t == current)
981 return mi;
982 return NULL;
983}
984
985static void mce_clear_info(struct mce_info *mi)
986{
987 atomic_set(&mi->inuse, 0);
988}
989
990/*
Andi Kleenb79109c2009-02-12 13:43:23 +0100991 * The actual machine check handler. This only handles real
992 * exceptions when something got corrupted coming in through int 18.
993 *
994 * This is executed in NMI context not subject to normal locking rules. This
995 * implies that most kernel services cannot be safely used. Don't even
996 * think about putting a printk in there!
Andi Kleen3c079792009-05-27 21:56:55 +0200997 *
998 * On Intel systems this is entered on all CPUs in parallel through
999 * MCE broadcast. However some CPUs might be broken beyond repair,
1000 * so be always careful when synchronizing with others.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 */
Ingo Molnare9eee032009-04-08 12:31:17 +02001002void do_machine_check(struct pt_regs *regs, long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Borislav Petkov14625942012-10-17 12:05:33 +02001004 struct mca_config *cfg = &mca_cfg;
Andi Kleen3c079792009-05-27 21:56:55 +02001005 struct mce m, *final;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 int i;
Andi Kleen3c079792009-05-27 21:56:55 +02001007 int worst = 0;
1008 int severity;
1009 /*
1010 * Establish sequential order between the CPUs entering the machine
1011 * check handler.
1012 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +09001013 int order;
Tim Hockinbd784322007-07-21 17:10:37 +02001014 /*
1015 * If no_way_out gets set, there is no safe way to recover from this
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001016 * MCE. If mca_cfg.tolerant is cranked up, we'll try anyway.
Tim Hockinbd784322007-07-21 17:10:37 +02001017 */
1018 int no_way_out = 0;
1019 /*
1020 * If kill_it gets set, there might be a way to recover from this
1021 * error.
1022 */
1023 int kill_it = 0;
Andi Kleenb79109c2009-02-12 13:43:23 +01001024 DECLARE_BITMAP(toclear, MAX_NR_BANKS);
Tony Luck95022b82012-04-18 15:19:40 -07001025 DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
Andi Kleenbd19a5e2009-05-27 21:56:55 +02001026 char *msg = "Unknown";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Andi Kleen553f2652006-04-07 19:49:57 +02001028 atomic_inc(&mce_entry);
1029
Alex Shic6ae41e2012-05-11 15:35:27 +08001030 this_cpu_inc(mce_exception_count);
Andi Kleen01ca79f2009-05-27 21:56:52 +02001031
Borislav Petkov14625942012-10-17 12:05:33 +02001032 if (!cfg->banks)
Andi Kleen32561692009-05-27 21:56:53 +02001033 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Hidetoshi Setob8325c52011-06-08 10:57:46 +09001035 mce_gather_info(&m, regs);
Andi Kleenb5f2fa42009-02-12 13:43:22 +01001036
Andi Kleen3c079792009-05-27 21:56:55 +02001037 final = &__get_cpu_var(mces_seen);
1038 *final = m;
1039
Tony Luck95022b82012-04-18 15:19:40 -07001040 memset(valid_banks, 0, sizeof(valid_banks));
Tony Luck61b0fcc2012-07-19 11:28:46 -07001041 no_way_out = mce_no_way_out(&m, &msg, valid_banks, regs);
Hidetoshi Seto680b6cf2009-08-26 16:20:36 +09001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 barrier();
1044
Andi Kleen3c079792009-05-27 21:56:55 +02001045 /*
Tony Lucka8c321f2012-01-03 11:45:45 -08001046 * When no restart IP might need to kill or panic.
1047 * Assume the worst for now, but if we find the
1048 * severity is MCE_AR_SEVERITY we have other options.
Andi Kleened7290d2009-05-27 21:56:57 +02001049 */
1050 if (!(m.mcgstatus & MCG_STATUS_RIPV))
1051 kill_it = 1;
1052
1053 /*
Andi Kleen3c079792009-05-27 21:56:55 +02001054 * Go through all the banks in exclusion of the other CPUs.
1055 * This way we don't report duplicated events on shared banks
1056 * because the first one to see it will clear it.
1057 */
Hidetoshi Seto7fb06fc2009-06-15 18:18:43 +09001058 order = mce_start(&no_way_out);
Borislav Petkov14625942012-10-17 12:05:33 +02001059 for (i = 0; i < cfg->banks; i++) {
Andi Kleenb79109c2009-02-12 13:43:23 +01001060 __clear_bit(i, toclear);
Tony Luck95022b82012-04-18 15:19:40 -07001061 if (!test_bit(i, valid_banks))
1062 continue;
Andi Kleencebe1822009-07-09 00:31:43 +02001063 if (!mce_banks[i].ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 continue;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001065
1066 m.misc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 m.addr = 0;
1068 m.bank = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Andi Kleena2d32bc2009-07-09 00:31:44 +02001070 m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if ((m.status & MCI_STATUS_VAL) == 0)
1072 continue;
1073
Andi Kleenb79109c2009-02-12 13:43:23 +01001074 /*
Andi Kleened7290d2009-05-27 21:56:57 +02001075 * Non uncorrected or non signaled errors are handled by
1076 * machine_check_poll. Leave them alone, unless this panics.
Andi Kleenb79109c2009-02-12 13:43:23 +01001077 */
Borislav Petkov14625942012-10-17 12:05:33 +02001078 if (!(m.status & (cfg->ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
Andi Kleened7290d2009-05-27 21:56:57 +02001079 !no_way_out)
Andi Kleenb79109c2009-02-12 13:43:23 +01001080 continue;
1081
1082 /*
1083 * Set taint even when machine check was not enabled.
1084 */
1085 add_taint(TAINT_MACHINE_CHECK);
1086
Borislav Petkov14625942012-10-17 12:05:33 +02001087 severity = mce_severity(&m, cfg->tolerant, NULL);
Andi Kleenb79109c2009-02-12 13:43:23 +01001088
Andi Kleened7290d2009-05-27 21:56:57 +02001089 /*
1090 * When machine check was for corrected handler don't touch,
1091 * unless we're panicing.
1092 */
1093 if (severity == MCE_KEEP_SEVERITY && !no_way_out)
1094 continue;
1095 __set_bit(i, toclear);
1096 if (severity == MCE_NO_SEVERITY) {
Andi Kleenb79109c2009-02-12 13:43:23 +01001097 /*
1098 * Machine check event was not enabled. Clear, but
1099 * ignore.
1100 */
1101 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103
Tony Luck85f926942011-12-13 09:48:13 -08001104 mce_read_aux(&m, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001106 /*
1107 * Action optional error. Queue address for later processing.
1108 * When the ring overflows we just ignore the AO error.
1109 * RED-PEN add some logging mechanism when
1110 * usable_address or mce_add_ring fails.
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001111 * RED-PEN don't ignore overflow for mca_cfg.tolerant == 0
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001112 */
1113 if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
1114 mce_ring_add(m.addr >> PAGE_SHIFT);
1115
Andi Kleenb79109c2009-02-12 13:43:23 +01001116 mce_log(&m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Andi Kleen3c079792009-05-27 21:56:55 +02001118 if (severity > worst) {
1119 *final = m;
1120 worst = severity;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123
Tony Lucka8c321f2012-01-03 11:45:45 -08001124 /* mce_clear_state will clear *final, save locally for use later */
1125 m = *final;
1126
Andi Kleen3c079792009-05-27 21:56:55 +02001127 if (!no_way_out)
1128 mce_clear_state(toclear);
1129
Ingo Molnare9eee032009-04-08 12:31:17 +02001130 /*
Andi Kleen3c079792009-05-27 21:56:55 +02001131 * Do most of the synchronization with other CPUs.
1132 * When there's any problem use only local no_way_out state.
Ingo Molnare9eee032009-04-08 12:31:17 +02001133 */
Andi Kleen3c079792009-05-27 21:56:55 +02001134 if (mce_end(order) < 0)
1135 no_way_out = worst >= MCE_PANIC_SEVERITY;
Tim Hockinbd784322007-07-21 17:10:37 +02001136
1137 /*
Tony Lucka8c321f2012-01-03 11:45:45 -08001138 * At insane "tolerant" levels we take no action. Otherwise
1139 * we only die if we have no other choice. For less serious
1140 * issues we try to recover, or limit damage to the current
1141 * process.
Tim Hockinbd784322007-07-21 17:10:37 +02001142 */
Borislav Petkov14625942012-10-17 12:05:33 +02001143 if (cfg->tolerant < 3) {
Tony Lucka8c321f2012-01-03 11:45:45 -08001144 if (no_way_out)
1145 mce_panic("Fatal machine check on current CPU", &m, msg);
1146 if (worst == MCE_AR_SEVERITY) {
1147 /* schedule action before return to userland */
Tony Luckdad17432012-05-14 15:07:48 -07001148 mce_save_info(m.addr, m.mcgstatus & MCG_STATUS_RIPV);
Tony Lucka8c321f2012-01-03 11:45:45 -08001149 set_thread_flag(TIF_MCE_NOTIFY);
1150 } else if (kill_it) {
1151 force_sig(SIGBUS, current);
1152 }
1153 }
Tim Hockine02e68d2007-07-21 17:10:36 +02001154
Andi Kleen3c079792009-05-27 21:56:55 +02001155 if (worst > 0)
1156 mce_report_event(regs);
Andi Kleen5f8c1a52009-04-29 19:29:12 +02001157 mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
Andi Kleen32561692009-05-27 21:56:53 +02001158out:
Andi Kleen553f2652006-04-07 19:49:57 +02001159 atomic_dec(&mce_entry);
Andi Kleen88921be2009-05-27 21:56:51 +02001160 sync_core();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
Andi Kleenea149b32009-04-29 19:31:00 +02001162EXPORT_SYMBOL_GPL(do_machine_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Tony Luckcd42f4a2011-12-15 10:48:12 -08001164#ifndef CONFIG_MEMORY_FAILURE
1165int memory_failure(unsigned long pfn, int vector, int flags)
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001166{
Tony Lucka8c321f2012-01-03 11:45:45 -08001167 /* mce_severity() should not hand us an ACTION_REQUIRED error */
1168 BUG_ON(flags & MF_ACTION_REQUIRED);
Joe Perchesc767a542012-05-21 19:50:07 -07001169 pr_err("Uncorrected memory error in page 0x%lx ignored\n"
1170 "Rebuild kernel with CONFIG_MEMORY_FAILURE=y for smarter handling\n",
1171 pfn);
Tony Luckcd42f4a2011-12-15 10:48:12 -08001172
1173 return 0;
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001174}
Tony Luckcd42f4a2011-12-15 10:48:12 -08001175#endif
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001176
1177/*
Tony Lucka8c321f2012-01-03 11:45:45 -08001178 * Called in process context that interrupted by MCE and marked with
1179 * TIF_MCE_NOTIFY, just before returning to erroneous userland.
1180 * This code is allowed to sleep.
1181 * Attempt possible recovery such as calling the high level VM handler to
1182 * process any corrupted pages, and kill/signal current process if required.
1183 * Action required errors are handled here.
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001184 */
1185void mce_notify_process(void)
1186{
1187 unsigned long pfn;
Tony Lucka8c321f2012-01-03 11:45:45 -08001188 struct mce_info *mi = mce_find_info();
Tony Luck6751ed62012-07-11 10:20:47 -07001189 int flags = MF_ACTION_REQUIRED;
Tony Lucka8c321f2012-01-03 11:45:45 -08001190
1191 if (!mi)
1192 mce_panic("Lost physical address for unconsumed uncorrectable error", NULL, NULL);
1193 pfn = mi->paddr >> PAGE_SHIFT;
1194
1195 clear_thread_flag(TIF_MCE_NOTIFY);
1196
1197 pr_err("Uncorrected hardware memory error in user-access at %llx",
1198 mi->paddr);
Tony Luckdad17432012-05-14 15:07:48 -07001199 /*
1200 * We must call memory_failure() here even if the current process is
1201 * doomed. We still need to mark the page as poisoned and alert any
1202 * other users of the page.
1203 */
Tony Luck6751ed62012-07-11 10:20:47 -07001204 if (!mi->restartable)
1205 flags |= MF_MUST_KILL;
1206 if (memory_failure(pfn, MCE_VECTOR, flags) < 0) {
Tony Lucka8c321f2012-01-03 11:45:45 -08001207 pr_err("Memory error not recovered");
1208 force_sig(SIGBUS, current);
1209 }
1210 mce_clear_info(mi);
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001211}
1212
Tony Lucka8c321f2012-01-03 11:45:45 -08001213/*
1214 * Action optional processing happens here (picking up
1215 * from the list of faulting pages that do_machine_check()
1216 * placed into the "ring").
1217 */
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001218static void mce_process_work(struct work_struct *dummy)
1219{
Tony Lucka8c321f2012-01-03 11:45:45 -08001220 unsigned long pfn;
1221
1222 while (mce_ring_get(&pfn))
1223 memory_failure(pfn, MCE_VECTOR, 0);
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001224}
1225
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001226#ifdef CONFIG_X86_MCE_INTEL
1227/***
1228 * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
Simon Arlott676b1852007-10-20 01:25:36 +02001229 * @cpu: The CPU on which the event occurred.
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001230 * @status: Event status information
1231 *
1232 * This function should be called by the thermal interrupt after the
1233 * event has been processed and the decision was made to log the event
1234 * further.
1235 *
1236 * The status parameter will be saved to the 'status' field of 'struct mce'
1237 * and historically has been the register value of the
1238 * MSR_IA32_THERMAL_STATUS (Intel) msr.
1239 */
Andi Kleenb5f2fa42009-02-12 13:43:22 +01001240void mce_log_therm_throt_event(__u64 status)
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001241{
1242 struct mce m;
1243
Andi Kleenb5f2fa42009-02-12 13:43:22 +01001244 mce_setup(&m);
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001245 m.bank = MCE_THERMAL_BANK;
1246 m.status = status;
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02001247 mce_log(&m);
1248}
1249#endif /* CONFIG_X86_MCE_INTEL */
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251/*
Tim Hockin8a336b02007-05-02 19:27:19 +02001252 * Periodic polling timer for "silent" machine check errors. If the
1253 * poller finds an MCE, poll 2x faster. When the poller finds no more
1254 * errors, poll 2x slower (up to check_interval seconds).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 */
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001256static unsigned long check_interval = 5 * 60; /* 5 minutes */
Ingo Molnare9eee032009-04-08 12:31:17 +02001257
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001258static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
Andi Kleen52d168e2009-02-12 13:39:29 +01001259static DEFINE_PER_CPU(struct timer_list, mce_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Chen Gong55babd82012-08-09 11:44:51 -07001261static unsigned long mce_adjust_timer_default(unsigned long interval)
1262{
1263 return interval;
1264}
1265
1266static unsigned long (*mce_adjust_timer)(unsigned long interval) =
1267 mce_adjust_timer_default;
1268
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001269static void mce_timer_fn(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001271 struct timer_list *t = &__get_cpu_var(mce_timer);
1272 unsigned long iv;
Andi Kleen52d168e2009-02-12 13:39:29 +01001273
1274 WARN_ON(smp_processor_id() != data);
1275
Tejun Heo7b543a52010-12-18 16:30:05 +01001276 if (mce_available(__this_cpu_ptr(&cpu_info))) {
Andi Kleenee031c32009-02-12 13:49:34 +01001277 machine_check_poll(MCP_TIMESTAMP,
1278 &__get_cpu_var(mce_poll_banks));
Chen Gong55babd82012-08-09 11:44:51 -07001279 mce_intel_cmci_poll();
Ingo Molnare9eee032009-04-08 12:31:17 +02001280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 /*
Tim Hockine02e68d2007-07-21 17:10:36 +02001283 * Alert userspace if needed. If we logged an MCE, reduce the
1284 * polling interval, otherwise increase the polling interval.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 */
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001286 iv = __this_cpu_read(mce_next_interval);
Chen Gong55babd82012-08-09 11:44:51 -07001287 if (mce_notify_irq()) {
Chen Gong958fb3c2012-06-05 10:35:02 +08001288 iv = max(iv / 2, (unsigned long) HZ/100);
Chen Gong55babd82012-08-09 11:44:51 -07001289 } else {
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001290 iv = min(iv * 2, round_jiffies_relative(check_interval * HZ));
Chen Gong55babd82012-08-09 11:44:51 -07001291 iv = mce_adjust_timer(iv);
1292 }
Thomas Gleixner82f7af02012-05-24 17:54:51 +00001293 __this_cpu_write(mce_next_interval, iv);
Chen Gong55babd82012-08-09 11:44:51 -07001294 /* Might have become 0 after CMCI storm subsided */
1295 if (iv) {
1296 t->expires = jiffies + iv;
1297 add_timer_on(t, smp_processor_id());
1298 }
1299}
Tim Hockin8a336b02007-05-02 19:27:19 +02001300
Chen Gong55babd82012-08-09 11:44:51 -07001301/*
1302 * Ensure that the timer is firing in @interval from now.
1303 */
1304void mce_timer_kick(unsigned long interval)
1305{
1306 struct timer_list *t = &__get_cpu_var(mce_timer);
1307 unsigned long when = jiffies + interval;
1308 unsigned long iv = __this_cpu_read(mce_next_interval);
1309
1310 if (timer_pending(t)) {
1311 if (time_before(when, t->expires))
1312 mod_timer_pinned(t, when);
1313 } else {
1314 t->expires = round_jiffies(when);
1315 add_timer_on(t, smp_processor_id());
1316 }
1317 if (interval < iv)
1318 __this_cpu_write(mce_next_interval, interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04001321/* Must not be called in IRQ context where del_timer_sync() can deadlock */
1322static void mce_timer_delete_all(void)
1323{
1324 int cpu;
1325
1326 for_each_online_cpu(cpu)
1327 del_timer_sync(&per_cpu(mce_timer, cpu));
1328}
1329
Andi Kleen9bd98402009-02-12 13:39:28 +01001330static void mce_do_trigger(struct work_struct *work)
1331{
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001332 call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
Andi Kleen9bd98402009-02-12 13:39:28 +01001333}
1334
1335static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
1336
Tim Hockine02e68d2007-07-21 17:10:36 +02001337/*
Andi Kleen9bd98402009-02-12 13:39:28 +01001338 * Notify the user(s) about new machine check events.
1339 * Can be called from interrupt context, but not from machine check/NMI
1340 * context.
Tim Hockine02e68d2007-07-21 17:10:36 +02001341 */
Andi Kleen9ff36ee2009-05-27 21:56:58 +02001342int mce_notify_irq(void)
Tim Hockine02e68d2007-07-21 17:10:36 +02001343{
Andi Kleen8457c842009-02-12 13:49:33 +01001344 /* Not more than two messages every minute */
1345 static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1346
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09001347 if (test_and_clear_bit(0, &mce_need_notify)) {
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001348 /* wake processes polling /dev/mcelog */
1349 wake_up_interruptible(&mce_chrdev_wait);
Andi Kleen9bd98402009-02-12 13:39:28 +01001350
Tejun Heo4d899be2012-12-21 17:57:05 -08001351 if (mce_helper[0])
Andi Kleen9bd98402009-02-12 13:39:28 +01001352 schedule_work(&mce_trigger_work);
Tim Hockine02e68d2007-07-21 17:10:36 +02001353
Andi Kleen8457c842009-02-12 13:49:33 +01001354 if (__ratelimit(&ratelimit))
Huang Yinga2d7b0d2010-06-08 14:35:39 +08001355 pr_info(HW_ERR "Machine check events logged\n");
Tim Hockine02e68d2007-07-21 17:10:36 +02001356
1357 return 1;
1358 }
1359 return 0;
1360}
Andi Kleen9ff36ee2009-05-27 21:56:58 +02001361EXPORT_SYMBOL_GPL(mce_notify_irq);
Tim Hockine02e68d2007-07-21 17:10:36 +02001362
Hidetoshi Setocffd3772009-11-12 15:52:40 +09001363static int __cpuinit __mcheck_cpu_mce_banks_init(void)
Andi Kleencebe1822009-07-09 00:31:43 +02001364{
1365 int i;
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001366 u8 num_banks = mca_cfg.banks;
Andi Kleencebe1822009-07-09 00:31:43 +02001367
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001368 mce_banks = kzalloc(num_banks * sizeof(struct mce_bank), GFP_KERNEL);
Andi Kleencebe1822009-07-09 00:31:43 +02001369 if (!mce_banks)
1370 return -ENOMEM;
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001371
1372 for (i = 0; i < num_banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02001373 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02001374
Andi Kleencebe1822009-07-09 00:31:43 +02001375 b->ctl = -1ULL;
1376 b->init = 1;
1377 }
1378 return 0;
1379}
1380
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001381/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 * Initialize Machine Checks for a CPU.
1383 */
Borislav Petkov5e099542009-10-16 12:31:32 +02001384static int __cpuinit __mcheck_cpu_cap_init(void)
Andi Kleen0d7482e32009-02-17 23:07:13 +01001385{
Andi Kleen0d7482e32009-02-17 23:07:13 +01001386 unsigned b;
Ingo Molnare9eee032009-04-08 12:31:17 +02001387 u64 cap;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001388
1389 rdmsrl(MSR_IA32_MCG_CAP, cap);
Thomas Gleixner01c66802009-04-08 12:31:24 +02001390
1391 b = cap & MCG_BANKCNT_MASK;
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001392 if (!mca_cfg.banks)
Joe Perchesc767a542012-05-21 19:50:07 -07001393 pr_info("CPU supports %d MCE banks\n", b);
Ingo Molnarb6592942009-04-08 12:31:27 +02001394
Andi Kleen0d7482e32009-02-17 23:07:13 +01001395 if (b > MAX_NR_BANKS) {
Joe Perchesc767a542012-05-21 19:50:07 -07001396 pr_warn("Using only %u machine check banks out of %u\n",
Andi Kleen0d7482e32009-02-17 23:07:13 +01001397 MAX_NR_BANKS, b);
1398 b = MAX_NR_BANKS;
1399 }
1400
1401 /* Don't support asymmetric configurations today */
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001402 WARN_ON(mca_cfg.banks != 0 && b != mca_cfg.banks);
1403 mca_cfg.banks = b;
1404
Andi Kleencebe1822009-07-09 00:31:43 +02001405 if (!mce_banks) {
Hidetoshi Setocffd3772009-11-12 15:52:40 +09001406 int err = __mcheck_cpu_mce_banks_init();
Ingo Molnar11868a22009-09-23 17:49:55 +02001407
Andi Kleencebe1822009-07-09 00:31:43 +02001408 if (err)
1409 return err;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001410 }
1411
1412 /* Use accurate RIP reporting if available. */
Thomas Gleixner01c66802009-04-08 12:31:24 +02001413 if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
Borislav Petkov84c25592012-10-15 19:59:18 +02001414 mca_cfg.rip_msr = MSR_IA32_MCG_EIP;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001415
Andi Kleened7290d2009-05-27 21:56:57 +02001416 if (cap & MCG_SER_P)
Borislav Petkov14625942012-10-17 12:05:33 +02001417 mca_cfg.ser = true;
Andi Kleened7290d2009-05-27 21:56:57 +02001418
Andi Kleen0d7482e32009-02-17 23:07:13 +01001419 return 0;
1420}
1421
Borislav Petkov5e099542009-10-16 12:31:32 +02001422static void __mcheck_cpu_init_generic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Borislav Petkov84c25592012-10-15 19:59:18 +02001424 enum mcp_flags m_fl = 0;
Ingo Molnare9eee032009-04-08 12:31:17 +02001425 mce_banks_t all_banks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 u64 cap;
1427 int i;
1428
Borislav Petkov84c25592012-10-15 19:59:18 +02001429 if (!mca_cfg.bootlog)
1430 m_fl = MCP_DONTLOG;
1431
Andi Kleenb79109c2009-02-12 13:43:23 +01001432 /*
1433 * Log the machine checks left over from the previous reset.
1434 */
Andi Kleenee031c32009-02-12 13:49:34 +01001435 bitmap_fill(all_banks, MAX_NR_BANKS);
Borislav Petkov84c25592012-10-15 19:59:18 +02001436 machine_check_poll(MCP_UC | m_fl, &all_banks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 set_in_cr4(X86_CR4_MCE);
1439
Andi Kleen0d7482e32009-02-17 23:07:13 +01001440 rdmsrl(MSR_IA32_MCG_CAP, cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (cap & MCG_CTL_P)
1442 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1443
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001444 for (i = 0; i < mca_cfg.banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02001445 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02001446
Andi Kleencebe1822009-07-09 00:31:43 +02001447 if (!b->init)
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001448 continue;
Andi Kleena2d32bc2009-07-09 00:31:44 +02001449 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
1450 wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
Tony Luck61b0fcc2012-07-19 11:28:46 -07001454/*
1455 * During IFU recovery Sandy Bridge -EP4S processors set the RIPV and
1456 * EIPV bits in MCG_STATUS to zero on the affected logical processor (SDM
1457 * Vol 3B Table 15-20). But this confuses both the code that determines
1458 * whether the machine check occurred in kernel or user mode, and also
1459 * the severity assessment code. Pretend that EIPV was set, and take the
1460 * ip/cs values from the pt_regs that mce_gather_info() ignored earlier.
1461 */
1462static void quirk_sandybridge_ifu(int bank, struct mce *m, struct pt_regs *regs)
1463{
1464 if (bank != 0)
1465 return;
1466 if ((m->mcgstatus & (MCG_STATUS_EIPV|MCG_STATUS_RIPV)) != 0)
1467 return;
1468 if ((m->status & (MCI_STATUS_OVER|MCI_STATUS_UC|
1469 MCI_STATUS_EN|MCI_STATUS_MISCV|MCI_STATUS_ADDRV|
1470 MCI_STATUS_PCC|MCI_STATUS_S|MCI_STATUS_AR|
1471 MCACOD)) !=
1472 (MCI_STATUS_UC|MCI_STATUS_EN|
1473 MCI_STATUS_MISCV|MCI_STATUS_ADDRV|MCI_STATUS_S|
1474 MCI_STATUS_AR|MCACOD_INSTR))
1475 return;
1476
1477 m->mcgstatus |= MCG_STATUS_EIPV;
1478 m->ip = regs->ip;
1479 m->cs = regs->cs;
1480}
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482/* Add per CPU specific workarounds here */
Borislav Petkov5e099542009-10-16 12:31:32 +02001483static int __cpuinit __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001484{
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001485 struct mca_config *cfg = &mca_cfg;
1486
Ingo Molnare412cd22009-08-17 10:19:00 +02001487 if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
Joe Perchesc767a542012-05-21 19:50:07 -07001488 pr_info("unknown CPU type - not enabling MCE support\n");
Ingo Molnare412cd22009-08-17 10:19:00 +02001489 return -EOPNOTSUPP;
1490 }
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 /* This should be disabled by the BIOS, but isn't always */
Jan Beulich911f6a72008-04-22 16:22:21 +01001493 if (c->x86_vendor == X86_VENDOR_AMD) {
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001494 if (c->x86 == 15 && cfg->banks > 4) {
Ingo Molnare9eee032009-04-08 12:31:17 +02001495 /*
1496 * disable GART TBL walk error reporting, which
1497 * trips off incorrectly with the IOMMU & 3ware
1498 * & Cerberus:
1499 */
Andi Kleencebe1822009-07-09 00:31:43 +02001500 clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
Ingo Molnare9eee032009-04-08 12:31:17 +02001501 }
Borislav Petkov84c25592012-10-15 19:59:18 +02001502 if (c->x86 <= 17 && cfg->bootlog < 0) {
Ingo Molnare9eee032009-04-08 12:31:17 +02001503 /*
1504 * Lots of broken BIOS around that don't clear them
1505 * by default and leave crap in there. Don't log:
1506 */
Borislav Petkov84c25592012-10-15 19:59:18 +02001507 cfg->bootlog = 0;
Ingo Molnare9eee032009-04-08 12:31:17 +02001508 }
Andi Kleen2e6f6942009-04-27 18:42:48 +02001509 /*
1510 * Various K7s with broken bank 0 around. Always disable
1511 * by default.
1512 */
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001513 if (c->x86 == 6 && cfg->banks > 0)
Andi Kleencebe1822009-07-09 00:31:43 +02001514 mce_banks[0].ctl = 0;
Borislav Petkov575203b2012-04-20 18:01:34 +02001515
1516 /*
1517 * Turn off MC4_MISC thresholding banks on those models since
1518 * they're not supported there.
1519 */
1520 if (c->x86 == 0x15 &&
1521 (c->x86_model >= 0x10 && c->x86_model <= 0x1f)) {
1522 int i;
1523 u64 val, hwcr;
1524 bool need_toggle;
1525 u32 msrs[] = {
1526 0x00000413, /* MC4_MISC0 */
1527 0xc0000408, /* MC4_MISC1 */
1528 };
1529
1530 rdmsrl(MSR_K7_HWCR, hwcr);
1531
1532 /* McStatusWrEn has to be set */
1533 need_toggle = !(hwcr & BIT(18));
1534
1535 if (need_toggle)
1536 wrmsrl(MSR_K7_HWCR, hwcr | BIT(18));
1537
1538 for (i = 0; i < ARRAY_SIZE(msrs); i++) {
1539 rdmsrl(msrs[i], val);
1540
1541 /* CntP bit set? */
Borislav Petkov80f0336102012-05-22 12:53:46 +02001542 if (val & BIT_64(62)) {
1543 val &= ~BIT_64(62);
1544 wrmsrl(msrs[i], val);
Borislav Petkov575203b2012-04-20 18:01:34 +02001545 }
1546 }
1547
1548 /* restore old settings */
1549 if (need_toggle)
1550 wrmsrl(MSR_K7_HWCR, hwcr);
1551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
Andi Kleene5835382005-11-05 17:25:54 +01001553
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001554 if (c->x86_vendor == X86_VENDOR_INTEL) {
1555 /*
1556 * SDM documents that on family 6 bank 0 should not be written
1557 * because it aliases to another special BIOS controlled
1558 * register.
1559 * But it's not aliased anymore on model 0x1a+
1560 * Don't ignore bank 0 completely because there could be a
1561 * valid event later, merely don't write CTL0.
1562 */
1563
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001564 if (c->x86 == 6 && c->x86_model < 0x1A && cfg->banks > 0)
Andi Kleencebe1822009-07-09 00:31:43 +02001565 mce_banks[0].init = 0;
Andi Kleen3c079792009-05-27 21:56:55 +02001566
1567 /*
1568 * All newer Intel systems support MCE broadcasting. Enable
1569 * synchronization with a one second timeout.
1570 */
1571 if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
Borislav Petkov84c25592012-10-15 19:59:18 +02001572 cfg->monarch_timeout < 0)
1573 cfg->monarch_timeout = USEC_PER_SEC;
Bartlomiej Zolnierkiewiczc7f6fa42009-07-28 23:52:54 +02001574
Ingo Molnare412cd22009-08-17 10:19:00 +02001575 /*
1576 * There are also broken BIOSes on some Pentium M and
1577 * earlier systems:
1578 */
Borislav Petkov84c25592012-10-15 19:59:18 +02001579 if (c->x86 == 6 && c->x86_model <= 13 && cfg->bootlog < 0)
1580 cfg->bootlog = 0;
Tony Luck61b0fcc2012-07-19 11:28:46 -07001581
1582 if (c->x86 == 6 && c->x86_model == 45)
1583 quirk_no_way_out = quirk_sandybridge_ifu;
Andi Kleen06b7a7a2009-04-27 18:37:43 +02001584 }
Borislav Petkov84c25592012-10-15 19:59:18 +02001585 if (cfg->monarch_timeout < 0)
1586 cfg->monarch_timeout = 0;
1587 if (cfg->bootlog != 0)
Borislav Petkov7af19e42012-10-15 20:25:17 +02001588 cfg->panic_timeout = 30;
Ingo Molnare412cd22009-08-17 10:19:00 +02001589
1590 return 0;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001591}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001593static int __cpuinit __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
Andi Kleen4efc0672009-04-28 19:07:31 +02001594{
1595 if (c->x86 != 5)
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001596 return 0;
1597
Andi Kleen4efc0672009-04-28 19:07:31 +02001598 switch (c->x86_vendor) {
1599 case X86_VENDOR_INTEL:
Hidetoshi Setoc6978362009-06-15 17:22:49 +09001600 intel_p5_mcheck_init(c);
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001601 return 1;
Andi Kleen4efc0672009-04-28 19:07:31 +02001602 break;
1603 case X86_VENDOR_CENTAUR:
1604 winchip_mcheck_init(c);
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001605 return 1;
Andi Kleen4efc0672009-04-28 19:07:31 +02001606 break;
1607 }
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001608
1609 return 0;
Andi Kleen4efc0672009-04-28 19:07:31 +02001610}
1611
Borislav Petkov5e099542009-10-16 12:31:32 +02001612static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 switch (c->x86_vendor) {
1615 case X86_VENDOR_INTEL:
1616 mce_intel_feature_init(c);
Chen Gong55babd82012-08-09 11:44:51 -07001617 mce_adjust_timer = mce_intel_adjust_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 break;
Jacob Shin89b831e2005-11-05 17:25:53 +01001619 case X86_VENDOR_AMD:
1620 mce_amd_feature_init(c);
1621 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 default:
1623 break;
1624 }
1625}
1626
Thomas Gleixner26c3c282012-07-19 13:59:39 -04001627static void mce_start_timer(unsigned int cpu, struct timer_list *t)
1628{
Chen Gong55babd82012-08-09 11:44:51 -07001629 unsigned long iv = mce_adjust_timer(check_interval * HZ);
Thomas Gleixner26c3c282012-07-19 13:59:39 -04001630
1631 __this_cpu_write(mce_next_interval, iv);
1632
Borislav Petkov7af19e42012-10-15 20:25:17 +02001633 if (mca_cfg.ignore_ce || !iv)
Thomas Gleixner26c3c282012-07-19 13:59:39 -04001634 return;
1635
1636 t->expires = round_jiffies(jiffies + iv);
1637 add_timer_on(t, smp_processor_id());
1638}
1639
Borislav Petkov5e099542009-10-16 12:31:32 +02001640static void __mcheck_cpu_init_timer(void)
Andi Kleen52d168e2009-02-12 13:39:29 +01001641{
1642 struct timer_list *t = &__get_cpu_var(mce_timer);
Thomas Gleixner26c3c282012-07-19 13:59:39 -04001643 unsigned int cpu = smp_processor_id();
Andi Kleen52d168e2009-02-12 13:39:29 +01001644
Thomas Gleixner26c3c282012-07-19 13:59:39 -04001645 setup_timer(t, mce_timer_fn, cpu);
1646 mce_start_timer(cpu, t);
Andi Kleen52d168e2009-02-12 13:39:29 +01001647}
1648
Andi Kleen9eda8cb2009-07-09 00:31:42 +02001649/* Handle unconfigured int18 (should never happen) */
1650static void unexpected_machine_check(struct pt_regs *regs, long error_code)
1651{
Joe Perchesc767a542012-05-21 19:50:07 -07001652 pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
Andi Kleen9eda8cb2009-07-09 00:31:42 +02001653 smp_processor_id());
1654}
1655
1656/* Call the installed machine check handler for this CPU setup. */
1657void (*machine_check_vector)(struct pt_regs *, long error_code) =
1658 unexpected_machine_check;
1659
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001660/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 * Called for each booted CPU to set up machine checks.
Ingo Molnare9eee032009-04-08 12:31:17 +02001662 * Must be called with preempt off:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 */
Borislav Petkov5e099542009-10-16 12:31:32 +02001664void __cpuinit mcheck_cpu_init(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Borislav Petkov14625942012-10-17 12:05:33 +02001666 if (mca_cfg.disabled)
Andi Kleen4efc0672009-04-28 19:07:31 +02001667 return;
1668
Hidetoshi Seto3a97fc32011-06-08 10:58:35 +09001669 if (__mcheck_cpu_ancient_init(c))
1670 return;
Andi Kleen4efc0672009-04-28 19:07:31 +02001671
Andi Kleen5b4408f2009-02-12 13:39:30 +01001672 if (!mce_available(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return;
1674
Borislav Petkov5e099542009-10-16 12:31:32 +02001675 if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
Borislav Petkov14625942012-10-17 12:05:33 +02001676 mca_cfg.disabled = true;
Andi Kleen0d7482e32009-02-17 23:07:13 +01001677 return;
1678 }
Andi Kleen0d7482e32009-02-17 23:07:13 +01001679
Andi Kleen5d727922009-04-27 19:25:48 +02001680 machine_check_vector = do_machine_check;
1681
Borislav Petkov5e099542009-10-16 12:31:32 +02001682 __mcheck_cpu_init_generic();
1683 __mcheck_cpu_init_vendor(c);
1684 __mcheck_cpu_init_timer();
Andi Kleen9b1beaf2009-05-27 21:56:59 +02001685 INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
Hidetoshi Setob77e70b2011-06-08 10:56:02 +09001686 init_irq_work(&__get_cpu_var(mce_irq_work), &mce_irq_work_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
1689/*
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001690 * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 */
1692
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001693static DEFINE_SPINLOCK(mce_chrdev_state_lock);
1694static int mce_chrdev_open_count; /* #times opened */
1695static int mce_chrdev_open_exclu; /* already open exclusive? */
Tim Hockinf528e7b2007-07-21 17:10:35 +02001696
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001697static int mce_chrdev_open(struct inode *inode, struct file *file)
Tim Hockinf528e7b2007-07-21 17:10:35 +02001698{
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001699 spin_lock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001700
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001701 if (mce_chrdev_open_exclu ||
1702 (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
1703 spin_unlock(&mce_chrdev_state_lock);
Ingo Molnare9eee032009-04-08 12:31:17 +02001704
Tim Hockinf528e7b2007-07-21 17:10:35 +02001705 return -EBUSY;
1706 }
1707
1708 if (file->f_flags & O_EXCL)
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001709 mce_chrdev_open_exclu = 1;
1710 mce_chrdev_open_count++;
Tim Hockinf528e7b2007-07-21 17:10:35 +02001711
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001712 spin_unlock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001713
Tim Hockinbd784322007-07-21 17:10:37 +02001714 return nonseekable_open(inode, file);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001715}
1716
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001717static int mce_chrdev_release(struct inode *inode, struct file *file)
Tim Hockinf528e7b2007-07-21 17:10:35 +02001718{
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001719 spin_lock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001720
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001721 mce_chrdev_open_count--;
1722 mce_chrdev_open_exclu = 0;
Tim Hockinf528e7b2007-07-21 17:10:35 +02001723
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001724 spin_unlock(&mce_chrdev_state_lock);
Tim Hockinf528e7b2007-07-21 17:10:35 +02001725
1726 return 0;
1727}
1728
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001729static void collect_tscs(void *data)
1730{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 unsigned long *cpu_tsc = (unsigned long *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001733 rdtscll(cpu_tsc[smp_processor_id()]);
1734}
1735
Huang Ying482908b2010-05-18 14:35:22 +08001736static int mce_apei_read_done;
1737
1738/* Collect MCE record of previous boot in persistent storage via APEI ERST. */
1739static int __mce_read_apei(char __user **ubuf, size_t usize)
1740{
1741 int rc;
1742 u64 record_id;
1743 struct mce m;
1744
1745 if (usize < sizeof(struct mce))
1746 return -EINVAL;
1747
1748 rc = apei_read_mce(&m, &record_id);
1749 /* Error or no more MCE record */
1750 if (rc <= 0) {
1751 mce_apei_read_done = 1;
Naoya Horiguchifadd85f2012-01-23 15:54:52 -05001752 /*
1753 * When ERST is disabled, mce_chrdev_read() should return
1754 * "no record" instead of "no device."
1755 */
1756 if (rc == -ENODEV)
1757 return 0;
Huang Ying482908b2010-05-18 14:35:22 +08001758 return rc;
1759 }
1760 rc = -EFAULT;
1761 if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
1762 return rc;
1763 /*
1764 * In fact, we should have cleared the record after that has
1765 * been flushed to the disk or sent to network in
1766 * /sbin/mcelog, but we have no interface to support that now,
1767 * so just clear it to avoid duplication.
1768 */
1769 rc = apei_clear_mce(record_id);
1770 if (rc) {
1771 mce_apei_read_done = 1;
1772 return rc;
1773 }
1774 *ubuf += sizeof(struct mce);
1775
1776 return 0;
1777}
1778
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001779static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
1780 size_t usize, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 char __user *buf = ubuf;
Ingo Molnare9eee032009-04-08 12:31:17 +02001783 unsigned long *cpu_tsc;
1784 unsigned prev, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 int i, err;
1786
Mike Travis6bca67f2008-07-18 18:11:27 -07001787 cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
Andi Kleenf0de53b2005-04-16 15:25:10 -07001788 if (!cpu_tsc)
1789 return -ENOMEM;
1790
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001791 mutex_lock(&mce_chrdev_read_mutex);
Huang Ying482908b2010-05-18 14:35:22 +08001792
1793 if (!mce_apei_read_done) {
1794 err = __mce_read_apei(&buf, usize);
1795 if (err || buf != ubuf)
1796 goto out;
1797 }
1798
Paul E. McKenneyf56e8a02010-03-05 15:03:27 -08001799 next = rcu_dereference_check_mce(mcelog.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 /* Only supports full reads right now */
Huang Ying482908b2010-05-18 14:35:22 +08001802 err = -EINVAL;
1803 if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
1804 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 err = 0;
Huang Yingef41df4342009-02-12 13:39:34 +01001807 prev = 0;
1808 do {
1809 for (i = prev; i < next; i++) {
1810 unsigned long start = jiffies;
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001811 struct mce *m = &mcelog.entry[i];
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001812
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001813 while (!m->finished) {
Huang Yingef41df4342009-02-12 13:39:34 +01001814 if (time_after_eq(jiffies, start + 2)) {
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001815 memset(m, 0, sizeof(*m));
Huang Yingef41df4342009-02-12 13:39:34 +01001816 goto timeout;
1817 }
1818 cpu_relax();
Andi Kleen673242c2005-09-12 18:49:24 +02001819 }
Huang Yingef41df4342009-02-12 13:39:34 +01001820 smp_rmb();
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001821 err |= copy_to_user(buf, m, sizeof(*m));
1822 buf += sizeof(*m);
Huang Yingef41df4342009-02-12 13:39:34 +01001823timeout:
1824 ;
Andi Kleen673242c2005-09-12 18:49:24 +02001825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Huang Yingef41df4342009-02-12 13:39:34 +01001827 memset(mcelog.entry + prev, 0,
1828 (next - prev) * sizeof(struct mce));
1829 prev = next;
1830 next = cmpxchg(&mcelog.next, prev, 0);
1831 } while (next != prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Paul E. McKenneyb2b18662005-06-25 14:55:38 -07001833 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001835 /*
1836 * Collect entries that were still getting written before the
1837 * synchronize.
1838 */
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001839 on_each_cpu(collect_tscs, cpu_tsc, 1);
Ingo Molnare9eee032009-04-08 12:31:17 +02001840
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001841 for (i = next; i < MCE_LOG_LEN; i++) {
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001842 struct mce *m = &mcelog.entry[i];
1843
1844 if (m->finished && m->tsc < cpu_tsc[m->cpu]) {
1845 err |= copy_to_user(buf, m, sizeof(*m));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 smp_rmb();
Hidetoshi Seto559faa62011-06-08 11:00:08 +09001847 buf += sizeof(*m);
1848 memset(m, 0, sizeof(*m));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001850 }
Huang Ying482908b2010-05-18 14:35:22 +08001851
1852 if (err)
1853 err = -EFAULT;
1854
1855out:
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001856 mutex_unlock(&mce_chrdev_read_mutex);
Andi Kleenf0de53b2005-04-16 15:25:10 -07001857 kfree(cpu_tsc);
Ingo Molnare9eee032009-04-08 12:31:17 +02001858
Huang Ying482908b2010-05-18 14:35:22 +08001859 return err ? err : buf - ubuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001862static unsigned int mce_chrdev_poll(struct file *file, poll_table *wait)
Tim Hockine02e68d2007-07-21 17:10:36 +02001863{
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001864 poll_wait(file, &mce_chrdev_wait, wait);
Paul E. McKenneya4dd9922011-04-01 07:15:14 -07001865 if (rcu_access_index(mcelog.next))
Tim Hockine02e68d2007-07-21 17:10:36 +02001866 return POLLIN | POLLRDNORM;
Huang Ying482908b2010-05-18 14:35:22 +08001867 if (!mce_apei_read_done && apei_check_mce())
1868 return POLLIN | POLLRDNORM;
Tim Hockine02e68d2007-07-21 17:10:36 +02001869 return 0;
1870}
1871
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001872static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
1873 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 int __user *p = (int __user *)arg;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (!capable(CAP_SYS_ADMIN))
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001878 return -EPERM;
Ingo Molnare9eee032009-04-08 12:31:17 +02001879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 switch (cmd) {
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001881 case MCE_GET_RECORD_LEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return put_user(sizeof(struct mce), p);
1883 case MCE_GET_LOG_LEN:
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001884 return put_user(MCE_LOG_LEN, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 case MCE_GETCLEAR_FLAGS: {
1886 unsigned flags;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001887
1888 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 flags = mcelog.flags;
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001890 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
Ingo Molnare9eee032009-04-08 12:31:17 +02001891
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001892 return put_user(flags, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894 default:
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001895 return -ENOTTY;
1896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
Luck, Tony66f5ddf2011-11-03 11:46:47 -07001899static ssize_t (*mce_write)(struct file *filp, const char __user *ubuf,
1900 size_t usize, loff_t *off);
1901
1902void register_mce_write_callback(ssize_t (*fn)(struct file *filp,
1903 const char __user *ubuf,
1904 size_t usize, loff_t *off))
1905{
1906 mce_write = fn;
1907}
1908EXPORT_SYMBOL_GPL(register_mce_write_callback);
1909
1910ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
1911 size_t usize, loff_t *off)
1912{
1913 if (mce_write)
1914 return mce_write(filp, ubuf, usize, off);
1915 else
1916 return -EINVAL;
1917}
1918
1919static const struct file_operations mce_chrdev_ops = {
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001920 .open = mce_chrdev_open,
1921 .release = mce_chrdev_release,
1922 .read = mce_chrdev_read,
Luck, Tony66f5ddf2011-11-03 11:46:47 -07001923 .write = mce_chrdev_write,
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001924 .poll = mce_chrdev_poll,
1925 .unlocked_ioctl = mce_chrdev_ioctl,
1926 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927};
1928
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09001929static struct miscdevice mce_chrdev_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 MISC_MCELOG_MINOR,
1931 "mcelog",
1932 &mce_chrdev_ops,
1933};
1934
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001935/*
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001936 * mce=off Disables machine check
1937 * mce=no_cmci Disables CMCI
1938 * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
1939 * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
Andi Kleen3c079792009-05-27 21:56:55 +02001940 * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
1941 * monarchtimeout is how long to wait for other CPUs on machine
1942 * check, or 0 to not wait
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001943 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
1944 * mce=nobootlog Don't log MCEs from before booting.
Naveen N. Rao450cc202012-09-27 10:08:00 -07001945 * mce=bios_cmci_threshold Don't program the CMCI threshold
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947static int __init mcheck_enable(char *str)
1948{
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001949 struct mca_config *cfg = &mca_cfg;
1950
Bartlomiej Zolnierkiewicze3346fc2009-07-28 23:55:09 +02001951 if (*str == 0) {
Andi Kleen4efc0672009-04-28 19:07:31 +02001952 enable_p5_mce();
Bartlomiej Zolnierkiewicze3346fc2009-07-28 23:55:09 +02001953 return 1;
1954 }
Andi Kleen4efc0672009-04-28 19:07:31 +02001955 if (*str == '=')
1956 str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (!strcmp(str, "off"))
Borislav Petkov14625942012-10-17 12:05:33 +02001958 cfg->disabled = true;
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001959 else if (!strcmp(str, "no_cmci"))
Borislav Petkov7af19e42012-10-15 20:25:17 +02001960 cfg->cmci_disabled = true;
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001961 else if (!strcmp(str, "dont_log_ce"))
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001962 cfg->dont_log_ce = true;
Hidetoshi Seto62fdac52009-06-11 16:06:07 +09001963 else if (!strcmp(str, "ignore_ce"))
Borislav Petkov7af19e42012-10-15 20:25:17 +02001964 cfg->ignore_ce = true;
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001965 else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
Borislav Petkov84c25592012-10-15 19:59:18 +02001966 cfg->bootlog = (str[0] == 'b');
Naveen N. Rao450cc202012-09-27 10:08:00 -07001967 else if (!strcmp(str, "bios_cmci_threshold"))
Borislav Petkov14625942012-10-17 12:05:33 +02001968 cfg->bios_cmci_threshold = true;
Andi Kleen3c079792009-05-27 21:56:55 +02001969 else if (isdigit(str[0])) {
Borislav Petkovd203f0b2012-10-15 18:03:57 +02001970 get_option(&str, &(cfg->tolerant));
Andi Kleen3c079792009-05-27 21:56:55 +02001971 if (*str == ',') {
1972 ++str;
Borislav Petkov84c25592012-10-15 19:59:18 +02001973 get_option(&str, &(cfg->monarch_timeout));
Andi Kleen3c079792009-05-27 21:56:55 +02001974 }
1975 } else {
Joe Perchesc767a542012-05-21 19:50:07 -07001976 pr_info("mce argument %s ignored. Please use /sys\n", str);
Hidetoshi Seto13503fa2009-03-26 17:39:20 +09001977 return 0;
1978 }
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001979 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
Andi Kleen4efc0672009-04-28 19:07:31 +02001981__setup("mce", mcheck_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Yong Wanga2202aa2009-11-10 09:38:24 +08001983int __init mcheck_init(void)
Borislav Petkovb33a6362009-10-16 12:31:33 +02001984{
Yong Wanga2202aa2009-11-10 09:38:24 +08001985 mcheck_intel_therm_init();
1986
Borislav Petkovb33a6362009-10-16 12:31:33 +02001987 return 0;
1988}
Borislav Petkovb33a6362009-10-16 12:31:33 +02001989
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001990/*
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09001991 * mce_syscore: PM support
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02001992 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Andi Kleen973a2dd2009-02-12 13:39:32 +01001994/*
1995 * Disable machine checks on suspend and shutdown. We can't really handle
1996 * them later.
1997 */
Borislav Petkov5e099542009-10-16 12:31:32 +02001998static int mce_disable_error_reporting(void)
Andi Kleen973a2dd2009-02-12 13:39:32 +01001999{
2000 int i;
2001
Borislav Petkovd203f0b2012-10-15 18:03:57 +02002002 for (i = 0; i < mca_cfg.banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02002003 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02002004
Andi Kleencebe1822009-07-09 00:31:43 +02002005 if (b->init)
Andi Kleena2d32bc2009-07-09 00:31:44 +02002006 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002007 }
Andi Kleen973a2dd2009-02-12 13:39:32 +01002008 return 0;
2009}
2010
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09002011static int mce_syscore_suspend(void)
Andi Kleen973a2dd2009-02-12 13:39:32 +01002012{
Borislav Petkov5e099542009-10-16 12:31:32 +02002013 return mce_disable_error_reporting();
Andi Kleen973a2dd2009-02-12 13:39:32 +01002014}
2015
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09002016static void mce_syscore_shutdown(void)
Andi Kleen973a2dd2009-02-12 13:39:32 +01002017{
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002018 mce_disable_error_reporting();
Andi Kleen973a2dd2009-02-12 13:39:32 +01002019}
2020
Ingo Molnare9eee032009-04-08 12:31:17 +02002021/*
2022 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
2023 * Only one CPU is active at this time, the others get re-added later using
2024 * CPU hotplug:
2025 */
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09002026static void mce_syscore_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Borislav Petkov5e099542009-10-16 12:31:32 +02002028 __mcheck_cpu_init_generic();
Tejun Heo7b543a52010-12-18 16:30:05 +01002029 __mcheck_cpu_init_vendor(__this_cpu_ptr(&cpu_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002032static struct syscore_ops mce_syscore_ops = {
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09002033 .suspend = mce_syscore_suspend,
2034 .shutdown = mce_syscore_shutdown,
2035 .resume = mce_syscore_resume,
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002036};
2037
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09002038/*
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002039 * mce_device: Sysfs support
Hidetoshi Setoc7cece82011-06-08 11:02:03 +09002040 */
2041
Andi Kleen52d168e2009-02-12 13:39:29 +01002042static void mce_cpu_restart(void *data)
2043{
Tejun Heo7b543a52010-12-18 16:30:05 +01002044 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Hidetoshi Seto33edbf02009-06-15 17:18:45 +09002045 return;
Borislav Petkov5e099542009-10-16 12:31:32 +02002046 __mcheck_cpu_init_generic();
2047 __mcheck_cpu_init_timer();
Andi Kleen52d168e2009-02-12 13:39:29 +01002048}
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050/* Reinit MCEs after user configuration changes */
Thomas Gleixnerd88203d2007-10-23 22:37:23 +02002051static void mce_restart(void)
2052{
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04002053 mce_timer_delete_all();
Andi Kleen52d168e2009-02-12 13:39:29 +01002054 on_each_cpu(mce_cpu_restart, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002057/* Toggle features for corrected errors */
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04002058static void mce_disable_cmci(void *data)
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002059{
Tejun Heo7b543a52010-12-18 16:30:05 +01002060 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002061 return;
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002062 cmci_clear();
2063}
2064
2065static void mce_enable_ce(void *all)
2066{
Tejun Heo7b543a52010-12-18 16:30:05 +01002067 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002068 return;
2069 cmci_reenable();
2070 cmci_recheck();
2071 if (all)
Borislav Petkov5e099542009-10-16 12:31:32 +02002072 __mcheck_cpu_init_timer();
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002073}
2074
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002075static struct bus_type mce_subsys = {
Ingo Molnare9eee032009-04-08 12:31:17 +02002076 .name = "machinecheck",
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002077 .dev_name = "machinecheck",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078};
2079
Greg Kroah-Hartmand6126ef2012-01-26 15:49:14 -08002080DEFINE_PER_CPU(struct device *, mce_device);
Ingo Molnare9eee032009-04-08 12:31:17 +02002081
2082__cpuinitdata
2083void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002085static inline struct mce_bank *attr_to_bank(struct device_attribute *attr)
Andi Kleencebe1822009-07-09 00:31:43 +02002086{
2087 return container_of(attr, struct mce_bank, attr);
2088}
Andi Kleen0d7482e32009-02-17 23:07:13 +01002089
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002090static ssize_t show_bank(struct device *s, struct device_attribute *attr,
Andi Kleen0d7482e32009-02-17 23:07:13 +01002091 char *buf)
2092{
Andi Kleencebe1822009-07-09 00:31:43 +02002093 return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
Andi Kleen0d7482e32009-02-17 23:07:13 +01002094}
2095
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002096static ssize_t set_bank(struct device *s, struct device_attribute *attr,
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09002097 const char *buf, size_t size)
Andi Kleen0d7482e32009-02-17 23:07:13 +01002098{
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09002099 u64 new;
Ingo Molnare9eee032009-04-08 12:31:17 +02002100
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09002101 if (strict_strtoull(buf, 0, &new) < 0)
Andi Kleen0d7482e32009-02-17 23:07:13 +01002102 return -EINVAL;
Ingo Molnare9eee032009-04-08 12:31:17 +02002103
Andi Kleencebe1822009-07-09 00:31:43 +02002104 attr_to_bank(attr)->ctl = new;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002105 mce_restart();
Ingo Molnare9eee032009-04-08 12:31:17 +02002106
Hidetoshi Seto9319cec2009-04-14 17:26:30 +09002107 return size;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002108}
Andi Kleena98f0dd2007-02-13 13:26:23 +01002109
Ingo Molnare9eee032009-04-08 12:31:17 +02002110static ssize_t
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002111show_trigger(struct device *s, struct device_attribute *attr, char *buf)
Andi Kleena98f0dd2007-02-13 13:26:23 +01002112{
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09002113 strcpy(buf, mce_helper);
Andi Kleena98f0dd2007-02-13 13:26:23 +01002114 strcat(buf, "\n");
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09002115 return strlen(mce_helper) + 1;
Andi Kleena98f0dd2007-02-13 13:26:23 +01002116}
2117
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002118static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
Ingo Molnare9eee032009-04-08 12:31:17 +02002119 const char *buf, size_t siz)
Andi Kleena98f0dd2007-02-13 13:26:23 +01002120{
2121 char *p;
Ingo Molnare9eee032009-04-08 12:31:17 +02002122
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09002123 strncpy(mce_helper, buf, sizeof(mce_helper));
2124 mce_helper[sizeof(mce_helper)-1] = 0;
Hidetoshi Seto1020bcb2009-06-15 17:20:57 +09002125 p = strchr(mce_helper, '\n');
Ingo Molnare9eee032009-04-08 12:31:17 +02002126
Jan Beuliche9084ec2009-07-16 09:45:11 +01002127 if (p)
Ingo Molnare9eee032009-04-08 12:31:17 +02002128 *p = 0;
2129
Jan Beuliche9084ec2009-07-16 09:45:11 +01002130 return strlen(mce_helper) + !!p;
Andi Kleena98f0dd2007-02-13 13:26:23 +01002131}
2132
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002133static ssize_t set_ignore_ce(struct device *s,
2134 struct device_attribute *attr,
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002135 const char *buf, size_t size)
2136{
2137 u64 new;
2138
2139 if (strict_strtoull(buf, 0, &new) < 0)
2140 return -EINVAL;
2141
Borislav Petkov7af19e42012-10-15 20:25:17 +02002142 if (mca_cfg.ignore_ce ^ !!new) {
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002143 if (new) {
2144 /* disable ce features */
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04002145 mce_timer_delete_all();
2146 on_each_cpu(mce_disable_cmci, NULL, 1);
Borislav Petkov7af19e42012-10-15 20:25:17 +02002147 mca_cfg.ignore_ce = true;
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002148 } else {
2149 /* enable ce features */
Borislav Petkov7af19e42012-10-15 20:25:17 +02002150 mca_cfg.ignore_ce = false;
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002151 on_each_cpu(mce_enable_ce, (void *)1, 1);
2152 }
2153 }
2154 return size;
2155}
2156
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002157static ssize_t set_cmci_disabled(struct device *s,
2158 struct device_attribute *attr,
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002159 const char *buf, size_t size)
2160{
2161 u64 new;
2162
2163 if (strict_strtoull(buf, 0, &new) < 0)
2164 return -EINVAL;
2165
Borislav Petkov7af19e42012-10-15 20:25:17 +02002166 if (mca_cfg.cmci_disabled ^ !!new) {
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002167 if (new) {
2168 /* disable cmci */
Hidetoshi Seto9aaef962011-06-17 04:40:36 -04002169 on_each_cpu(mce_disable_cmci, NULL, 1);
Borislav Petkov7af19e42012-10-15 20:25:17 +02002170 mca_cfg.cmci_disabled = true;
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002171 } else {
2172 /* enable cmci */
Borislav Petkov7af19e42012-10-15 20:25:17 +02002173 mca_cfg.cmci_disabled = false;
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002174 on_each_cpu(mce_enable_ce, NULL, 1);
2175 }
2176 }
2177 return size;
2178}
2179
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002180static ssize_t store_int_with_restart(struct device *s,
2181 struct device_attribute *attr,
Andi Kleenb56f6422009-05-27 21:56:52 +02002182 const char *buf, size_t size)
2183{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002184 ssize_t ret = device_store_int(s, attr, buf, size);
Andi Kleenb56f6422009-05-27 21:56:52 +02002185 mce_restart();
2186 return ret;
2187}
2188
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002189static DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
Borislav Petkovd203f0b2012-10-15 18:03:57 +02002190static DEVICE_INT_ATTR(tolerant, 0644, mca_cfg.tolerant);
Borislav Petkov84c25592012-10-15 19:59:18 +02002191static DEVICE_INT_ATTR(monarch_timeout, 0644, mca_cfg.monarch_timeout);
Borislav Petkovd203f0b2012-10-15 18:03:57 +02002192static DEVICE_BOOL_ATTR(dont_log_ce, 0644, mca_cfg.dont_log_ce);
Ingo Molnare9eee032009-04-08 12:31:17 +02002193
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002194static struct dev_ext_attribute dev_attr_check_interval = {
2195 __ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
Andi Kleenb56f6422009-05-27 21:56:52 +02002196 &check_interval
2197};
Ingo Molnare9eee032009-04-08 12:31:17 +02002198
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002199static struct dev_ext_attribute dev_attr_ignore_ce = {
Borislav Petkov7af19e42012-10-15 20:25:17 +02002200 __ATTR(ignore_ce, 0644, device_show_bool, set_ignore_ce),
2201 &mca_cfg.ignore_ce
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002202};
2203
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002204static struct dev_ext_attribute dev_attr_cmci_disabled = {
Borislav Petkov7af19e42012-10-15 20:25:17 +02002205 __ATTR(cmci_disabled, 0644, device_show_bool, set_cmci_disabled),
2206 &mca_cfg.cmci_disabled
Hidetoshi Seto9af43b52009-06-15 17:21:36 +09002207};
2208
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002209static struct device_attribute *mce_device_attrs[] = {
2210 &dev_attr_tolerant.attr,
2211 &dev_attr_check_interval.attr,
2212 &dev_attr_trigger,
2213 &dev_attr_monarch_timeout.attr,
2214 &dev_attr_dont_log_ce.attr,
2215 &dev_attr_ignore_ce.attr,
2216 &dev_attr_cmci_disabled.attr,
Andi Kleena98f0dd2007-02-13 13:26:23 +01002217 NULL
2218};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002220static cpumask_var_t mce_device_initialized;
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002221
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002222static void mce_device_release(struct device *dev)
2223{
2224 kfree(dev);
2225}
2226
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002227/* Per cpu device init. All of the cpus still share the same ctrl bank: */
2228static __cpuinit int mce_device_create(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002230 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 int err;
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09002232 int i, j;
Mike Travis92cb7612007-10-19 20:35:04 +02002233
Andreas Herrmann90367552007-11-07 02:12:58 +01002234 if (!mce_available(&boot_cpu_data))
Andi Kleen91c6d402005-07-28 21:15:39 -07002235 return -EIO;
2236
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002237 dev = kzalloc(sizeof *dev, GFP_KERNEL);
2238 if (!dev)
2239 return -ENOMEM;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002240 dev->id = cpu;
2241 dev->bus = &mce_subsys;
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -08002242 dev->release = &mce_device_release;
Andi Kleen91c6d402005-07-28 21:15:39 -07002243
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002244 err = device_register(dev);
Akinobu Mitad435d862007-10-18 03:05:15 -07002245 if (err)
2246 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07002247
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002248 for (i = 0; mce_device_attrs[i]; i++) {
2249 err = device_create_file(dev, mce_device_attrs[i]);
Akinobu Mitad435d862007-10-18 03:05:15 -07002250 if (err)
2251 goto error;
Andi Kleen91c6d402005-07-28 21:15:39 -07002252 }
Borislav Petkovd203f0b2012-10-15 18:03:57 +02002253 for (j = 0; j < mca_cfg.banks; j++) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002254 err = device_create_file(dev, &mce_banks[j].attr);
Andi Kleen0d7482e32009-02-17 23:07:13 +01002255 if (err)
2256 goto error2;
2257 }
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002258 cpumask_set_cpu(cpu, mce_device_initialized);
Greg Kroah-Hartmand6126ef2012-01-26 15:49:14 -08002259 per_cpu(mce_device, cpu) = dev;
Akinobu Mitad435d862007-10-18 03:05:15 -07002260
2261 return 0;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002262error2:
Hidetoshi Setob1f49f92009-06-18 14:53:24 +09002263 while (--j >= 0)
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002264 device_remove_file(dev, &mce_banks[j].attr);
Akinobu Mitad435d862007-10-18 03:05:15 -07002265error:
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002266 while (--i >= 0)
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002267 device_remove_file(dev, mce_device_attrs[i]);
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002268
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002269 device_unregister(dev);
Akinobu Mitad435d862007-10-18 03:05:15 -07002270
Andi Kleen91c6d402005-07-28 21:15:39 -07002271 return err;
2272}
2273
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002274static __cpuinit void mce_device_remove(unsigned int cpu)
Andi Kleen91c6d402005-07-28 21:15:39 -07002275{
Greg Kroah-Hartmand6126ef2012-01-26 15:49:14 -08002276 struct device *dev = per_cpu(mce_device, cpu);
Shaohua Li73ca5352006-01-11 22:43:06 +01002277 int i;
2278
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002279 if (!cpumask_test_cpu(cpu, mce_device_initialized))
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002280 return;
2281
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002282 for (i = 0; mce_device_attrs[i]; i++)
2283 device_remove_file(dev, mce_device_attrs[i]);
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002284
Borislav Petkovd203f0b2012-10-15 18:03:57 +02002285 for (i = 0; i < mca_cfg.banks; i++)
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002286 device_remove_file(dev, &mce_banks[i].attr);
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002287
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002288 device_unregister(dev);
2289 cpumask_clear_cpu(cpu, mce_device_initialized);
Greg Kroah-Hartmand6126ef2012-01-26 15:49:14 -08002290 per_cpu(mce_device, cpu) = NULL;
Andi Kleen91c6d402005-07-28 21:15:39 -07002291}
Andi Kleen91c6d402005-07-28 21:15:39 -07002292
Andi Kleend6b75582009-02-12 13:39:31 +01002293/* Make sure there are no machine checks on offlined CPUs. */
Hidetoshi Seto767df1b2009-11-26 17:29:02 +09002294static void __cpuinit mce_disable_cpu(void *h)
Andi Kleend6b75582009-02-12 13:39:31 +01002295{
Andi Kleen88ccbed2009-02-12 13:49:36 +01002296 unsigned long action = *(unsigned long *)h;
Ingo Molnarcb491fc2009-04-08 12:31:17 +02002297 int i;
Andi Kleend6b75582009-02-12 13:39:31 +01002298
Tejun Heo7b543a52010-12-18 16:30:05 +01002299 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Andi Kleend6b75582009-02-12 13:39:31 +01002300 return;
Hidetoshi Seto767df1b2009-11-26 17:29:02 +09002301
Andi Kleen88ccbed2009-02-12 13:49:36 +01002302 if (!(action & CPU_TASKS_FROZEN))
2303 cmci_clear();
Borislav Petkovd203f0b2012-10-15 18:03:57 +02002304 for (i = 0; i < mca_cfg.banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02002305 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02002306
Andi Kleencebe1822009-07-09 00:31:43 +02002307 if (b->init)
Andi Kleena2d32bc2009-07-09 00:31:44 +02002308 wrmsrl(MSR_IA32_MCx_CTL(i), 0);
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002309 }
Andi Kleend6b75582009-02-12 13:39:31 +01002310}
2311
Hidetoshi Seto767df1b2009-11-26 17:29:02 +09002312static void __cpuinit mce_reenable_cpu(void *h)
Andi Kleend6b75582009-02-12 13:39:31 +01002313{
Andi Kleen88ccbed2009-02-12 13:49:36 +01002314 unsigned long action = *(unsigned long *)h;
Ingo Molnare9eee032009-04-08 12:31:17 +02002315 int i;
Andi Kleend6b75582009-02-12 13:39:31 +01002316
Tejun Heo7b543a52010-12-18 16:30:05 +01002317 if (!mce_available(__this_cpu_ptr(&cpu_info)))
Andi Kleend6b75582009-02-12 13:39:31 +01002318 return;
Ingo Molnare9eee032009-04-08 12:31:17 +02002319
Andi Kleen88ccbed2009-02-12 13:49:36 +01002320 if (!(action & CPU_TASKS_FROZEN))
2321 cmci_reenable();
Borislav Petkovd203f0b2012-10-15 18:03:57 +02002322 for (i = 0; i < mca_cfg.banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02002323 struct mce_bank *b = &mce_banks[i];
Ingo Molnar11868a22009-09-23 17:49:55 +02002324
Andi Kleencebe1822009-07-09 00:31:43 +02002325 if (b->init)
Andi Kleena2d32bc2009-07-09 00:31:44 +02002326 wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
Andi Kleen06b7a7a2009-04-27 18:37:43 +02002327 }
Andi Kleend6b75582009-02-12 13:39:31 +01002328}
2329
Andi Kleen91c6d402005-07-28 21:15:39 -07002330/* Get notified when a cpu comes on/off. Be hotplug friendly. */
Ingo Molnare9eee032009-04-08 12:31:17 +02002331static int __cpuinit
2332mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
Andi Kleen91c6d402005-07-28 21:15:39 -07002333{
2334 unsigned int cpu = (unsigned long)hcpu;
Andi Kleen52d168e2009-02-12 13:39:29 +01002335 struct timer_list *t = &per_cpu(mce_timer, cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07002336
Thomas Gleixner1a65f972012-07-19 13:59:40 -04002337 switch (action & ~CPU_TASKS_FROZEN) {
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002338 case CPU_ONLINE:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002339 mce_device_create(cpu);
Rafael J. Wysocki87357282008-08-22 22:23:09 +02002340 if (threshold_cpu_callback)
2341 threshold_cpu_callback(action, cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07002342 break;
Andi Kleen91c6d402005-07-28 21:15:39 -07002343 case CPU_DEAD:
Rafael J. Wysocki87357282008-08-22 22:23:09 +02002344 if (threshold_cpu_callback)
2345 threshold_cpu_callback(action, cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002346 mce_device_remove(cpu);
Chen Gong55babd82012-08-09 11:44:51 -07002347 mce_intel_hcpu_update(cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07002348 break;
Andi Kleen52d168e2009-02-12 13:39:29 +01002349 case CPU_DOWN_PREPARE:
Andi Kleen88ccbed2009-02-12 13:49:36 +01002350 smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
Chen Gong55babd82012-08-09 11:44:51 -07002351 del_timer_sync(t);
Andi Kleen52d168e2009-02-12 13:39:29 +01002352 break;
2353 case CPU_DOWN_FAILED:
Andi Kleen88ccbed2009-02-12 13:49:36 +01002354 smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
Thomas Gleixner26c3c282012-07-19 13:59:39 -04002355 mce_start_timer(cpu, t);
Andi Kleen88ccbed2009-02-12 13:49:36 +01002356 break;
Thomas Gleixner1a65f972012-07-19 13:59:40 -04002357 }
2358
2359 if (action == CPU_POST_DEAD) {
Andi Kleen88ccbed2009-02-12 13:49:36 +01002360 /* intentionally ignoring frozen here */
2361 cmci_rediscover(cpu);
Andi Kleen91c6d402005-07-28 21:15:39 -07002362 }
Thomas Gleixner1a65f972012-07-19 13:59:40 -04002363
Andreas Herrmannbae19fe2007-11-14 17:00:44 -08002364 return NOTIFY_OK;
Andi Kleen91c6d402005-07-28 21:15:39 -07002365}
2366
Sam Ravnborg1e356692008-01-30 13:33:36 +01002367static struct notifier_block mce_cpu_notifier __cpuinitdata = {
Andi Kleen91c6d402005-07-28 21:15:39 -07002368 .notifier_call = mce_cpu_callback,
2369};
2370
Andi Kleencebe1822009-07-09 00:31:43 +02002371static __init void mce_init_banks(void)
Andi Kleen0d7482e32009-02-17 23:07:13 +01002372{
2373 int i;
2374
Borislav Petkovd203f0b2012-10-15 18:03:57 +02002375 for (i = 0; i < mca_cfg.banks; i++) {
Andi Kleencebe1822009-07-09 00:31:43 +02002376 struct mce_bank *b = &mce_banks[i];
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002377 struct device_attribute *a = &b->attr;
Ingo Molnare9eee032009-04-08 12:31:17 +02002378
Eric W. Biedermana07e4152010-02-11 15:23:05 -08002379 sysfs_attr_init(&a->attr);
Andi Kleencebe1822009-07-09 00:31:43 +02002380 a->attr.name = b->attrname;
2381 snprintf(b->attrname, ATTR_LEN, "bank%d", i);
Ingo Molnare9eee032009-04-08 12:31:17 +02002382
2383 a->attr.mode = 0644;
2384 a->show = show_bank;
2385 a->store = set_bank;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002386 }
Andi Kleen0d7482e32009-02-17 23:07:13 +01002387}
2388
Borislav Petkov5e099542009-10-16 12:31:32 +02002389static __init int mcheck_init_device(void)
Andi Kleen91c6d402005-07-28 21:15:39 -07002390{
2391 int err;
2392 int i = 0;
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (!mce_available(&boot_cpu_data))
2395 return -EIO;
Andi Kleen0d7482e32009-02-17 23:07:13 +01002396
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002397 zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL);
Rusty Russell996867d2009-03-13 14:49:51 +10302398
Andi Kleencebe1822009-07-09 00:31:43 +02002399 mce_init_banks();
Andi Kleen0d7482e32009-02-17 23:07:13 +01002400
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002401 err = subsys_system_register(&mce_subsys, NULL);
Akinobu Mitad435d862007-10-18 03:05:15 -07002402 if (err)
2403 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07002404
2405 for_each_online_cpu(i) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002406 err = mce_device_create(i);
Akinobu Mitad435d862007-10-18 03:05:15 -07002407 if (err)
2408 return err;
Andi Kleen91c6d402005-07-28 21:15:39 -07002409 }
2410
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002411 register_syscore_ops(&mce_syscore_ops);
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -07002412 register_hotcpu_notifier(&mce_cpu_notifier);
Hidetoshi Seto93b62c32011-06-08 11:00:45 +09002413
2414 /* register character device /dev/mcelog */
2415 misc_register(&mce_chrdev_device);
Ingo Molnare9eee032009-04-08 12:31:17 +02002416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
Liu, Jinsongcef12ee2012-06-07 19:56:51 +08002419device_initcall_sync(mcheck_init_device);
Ingo Molnara988d332009-04-08 12:31:25 +02002420
Andi Kleend7c3c9a2009-04-28 23:07:25 +02002421/*
2422 * Old style boot options parsing. Only for compatibility.
2423 */
2424static int __init mcheck_disable(char *str)
2425{
Borislav Petkov14625942012-10-17 12:05:33 +02002426 mca_cfg.disabled = true;
Andi Kleend7c3c9a2009-04-28 23:07:25 +02002427 return 1;
2428}
2429__setup("nomce", mcheck_disable);
Huang Ying5be9ed22009-07-31 09:41:42 +08002430
2431#ifdef CONFIG_DEBUG_FS
2432struct dentry *mce_get_debugfs_dir(void)
2433{
2434 static struct dentry *dmce;
2435
2436 if (!dmce)
2437 dmce = debugfs_create_dir("mce", NULL);
2438
2439 return dmce;
2440}
Huang Yingbf783f92009-07-31 09:41:43 +08002441
2442static void mce_reset(void)
2443{
2444 cpu_missing = 0;
2445 atomic_set(&mce_fake_paniced, 0);
2446 atomic_set(&mce_executing, 0);
2447 atomic_set(&mce_callin, 0);
2448 atomic_set(&global_nwo, 0);
2449}
2450
2451static int fake_panic_get(void *data, u64 *val)
2452{
2453 *val = fake_panic;
2454 return 0;
2455}
2456
2457static int fake_panic_set(void *data, u64 val)
2458{
2459 mce_reset();
2460 fake_panic = val;
2461 return 0;
2462}
2463
2464DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
2465 fake_panic_set, "%llu\n");
2466
Borislav Petkov5e099542009-10-16 12:31:32 +02002467static int __init mcheck_debugfs_init(void)
Huang Yingbf783f92009-07-31 09:41:43 +08002468{
2469 struct dentry *dmce, *ffake_panic;
2470
2471 dmce = mce_get_debugfs_dir();
2472 if (!dmce)
2473 return -ENOMEM;
2474 ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
2475 &fake_panic_fops);
2476 if (!ffake_panic)
2477 return -ENOMEM;
2478
2479 return 0;
2480}
Borislav Petkov5e099542009-10-16 12:31:32 +02002481late_initcall(mcheck_debugfs_init);
Huang Ying5be9ed22009-07-31 09:41:42 +08002482#endif