x86, mce: remove machine check handler idle notify on 64bit
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / cpu / mcheck / mce.c
blob7562c1f674f31759e0eb00654c53971a0b072cb6
1 /*
2 * Machine check handler.
4 * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
5 * Rest from unknown author(s).
6 * 2004 Andi Kleen. Rewrote most of it.
7 * Copyright 2008 Intel Corporation
8 * Author: Andi Kleen
9 */
10 #include <linux/thread_info.h>
11 #include <linux/capability.h>
12 #include <linux/miscdevice.h>
13 #include <linux/ratelimit.h>
14 #include <linux/kallsyms.h>
15 #include <linux/rcupdate.h>
16 #include <linux/smp_lock.h>
17 #include <linux/kobject.h>
18 #include <linux/kdebug.h>
19 #include <linux/kernel.h>
20 #include <linux/percpu.h>
21 #include <linux/string.h>
22 #include <linux/sysdev.h>
23 #include <linux/ctype.h>
24 #include <linux/sched.h>
25 #include <linux/sysfs.h>
26 #include <linux/types.h>
27 #include <linux/init.h>
28 #include <linux/kmod.h>
29 #include <linux/poll.h>
30 #include <linux/cpu.h>
31 #include <linux/fs.h>
33 #include <asm/processor.h>
34 #include <asm/uaccess.h>
35 #include <asm/idle.h>
36 #include <asm/mce.h>
37 #include <asm/msr.h>
38 #include <asm/smp.h>
40 #include "mce.h"
42 /* Handle unconfigured int18 (should never happen) */
43 static void unexpected_machine_check(struct pt_regs *regs, long error_code)
45 printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n",
46 smp_processor_id());
49 /* Call the installed machine check handler for this CPU setup. */
50 void (*machine_check_vector)(struct pt_regs *, long error_code) =
51 unexpected_machine_check;
53 int mce_disabled;
55 #ifdef CONFIG_X86_64
57 #define MISC_MCELOG_MINOR 227
59 atomic_t mce_entry;
62 * Tolerant levels:
63 * 0: always panic on uncorrected errors, log corrected errors
64 * 1: panic or SIGBUS on uncorrected errors, log corrected errors
65 * 2: SIGBUS or log uncorrected errors (if possible), log corrected errors
66 * 3: never panic or SIGBUS, log all errors (for testing only)
68 static int tolerant = 1;
69 static int banks;
70 static u64 *bank;
71 static unsigned long notify_user;
72 static int rip_msr;
73 static int mce_bootlog = -1;
74 static atomic_t mce_events;
76 static char trigger[128];
77 static char *trigger_argv[2] = { trigger, NULL };
79 static unsigned long dont_init_banks;
81 static DECLARE_WAIT_QUEUE_HEAD(mce_wait);
83 /* MCA banks polled by the period polling timer for corrected events */
84 DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
85 [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
88 static inline int skip_bank_init(int i)
90 return i < BITS_PER_LONG && test_bit(i, &dont_init_banks);
93 /* Do initial initialization of a struct mce */
94 void mce_setup(struct mce *m)
96 memset(m, 0, sizeof(struct mce));
97 m->cpu = smp_processor_id();
98 rdtscll(m->tsc);
102 * Lockless MCE logging infrastructure.
103 * This avoids deadlocks on printk locks without having to break locks. Also
104 * separate MCEs from kernel messages to avoid bogus bug reports.
107 static struct mce_log mcelog = {
108 MCE_LOG_SIGNATURE,
109 MCE_LOG_LEN,
112 void mce_log(struct mce *mce)
114 unsigned next, entry;
116 atomic_inc(&mce_events);
117 mce->finished = 0;
118 wmb();
119 for (;;) {
120 entry = rcu_dereference(mcelog.next);
121 for (;;) {
123 * When the buffer fills up discard new entries.
124 * Assume that the earlier errors are the more
125 * interesting ones:
127 if (entry >= MCE_LOG_LEN) {
128 set_bit(MCE_OVERFLOW, (unsigned long *)&mcelog.flags);
129 return;
131 /* Old left over entry. Skip: */
132 if (mcelog.entry[entry].finished) {
133 entry++;
134 continue;
136 break;
138 smp_rmb();
139 next = entry + 1;
140 if (cmpxchg(&mcelog.next, entry, next) == entry)
141 break;
143 memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
144 wmb();
145 mcelog.entry[entry].finished = 1;
146 wmb();
148 set_bit(0, &notify_user);
151 static void print_mce(struct mce *m)
153 printk(KERN_EMERG "\n"
154 KERN_EMERG "HARDWARE ERROR\n"
155 KERN_EMERG
156 "CPU %d: Machine Check Exception: %16Lx Bank %d: %016Lx\n",
157 m->cpu, m->mcgstatus, m->bank, m->status);
158 if (m->ip) {
159 printk(KERN_EMERG "RIP%s %02x:<%016Lx> ",
160 !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
161 m->cs, m->ip);
162 if (m->cs == __KERNEL_CS)
163 print_symbol("{%s}", m->ip);
164 printk("\n");
166 printk(KERN_EMERG "TSC %llx ", m->tsc);
167 if (m->addr)
168 printk("ADDR %llx ", m->addr);
169 if (m->misc)
170 printk("MISC %llx ", m->misc);
171 printk("\n");
172 printk(KERN_EMERG "This is not a software problem!\n");
173 printk(KERN_EMERG "Run through mcelog --ascii to decode "
174 "and contact your hardware vendor\n");
177 static void mce_panic(char *msg, struct mce *backup, u64 start)
179 int i;
181 oops_begin();
182 for (i = 0; i < MCE_LOG_LEN; i++) {
183 u64 tsc = mcelog.entry[i].tsc;
185 if ((s64)(tsc - start) < 0)
186 continue;
187 print_mce(&mcelog.entry[i]);
188 if (backup && mcelog.entry[i].tsc == backup->tsc)
189 backup = NULL;
191 if (backup)
192 print_mce(backup);
193 panic(msg);
196 int mce_available(struct cpuinfo_x86 *c)
198 if (mce_disabled)
199 return 0;
200 return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
203 static inline void mce_get_rip(struct mce *m, struct pt_regs *regs)
205 if (regs && (m->mcgstatus & MCG_STATUS_RIPV)) {
206 m->ip = regs->ip;
207 m->cs = regs->cs;
208 } else {
209 m->ip = 0;
210 m->cs = 0;
212 if (rip_msr) {
213 /* Assume the RIP in the MSR is exact. Is this true? */
214 m->mcgstatus |= MCG_STATUS_EIPV;
215 rdmsrl(rip_msr, m->ip);
216 m->cs = 0;
221 * Poll for corrected events or events that happened before reset.
222 * Those are just logged through /dev/mcelog.
224 * This is executed in standard interrupt context.
226 void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
228 struct mce m;
229 int i;
231 mce_setup(&m);
233 rdmsrl(MSR_IA32_MCG_STATUS, m.mcgstatus);
234 for (i = 0; i < banks; i++) {
235 if (!bank[i] || !test_bit(i, *b))
236 continue;
238 m.misc = 0;
239 m.addr = 0;
240 m.bank = i;
241 m.tsc = 0;
243 barrier();
244 rdmsrl(MSR_IA32_MC0_STATUS + i*4, m.status);
245 if (!(m.status & MCI_STATUS_VAL))
246 continue;
249 * Uncorrected events are handled by the exception handler
250 * when it is enabled. But when the exception is disabled log
251 * everything.
253 * TBD do the same check for MCI_STATUS_EN here?
255 if ((m.status & MCI_STATUS_UC) && !(flags & MCP_UC))
256 continue;
258 if (m.status & MCI_STATUS_MISCV)
259 rdmsrl(MSR_IA32_MC0_MISC + i*4, m.misc);
260 if (m.status & MCI_STATUS_ADDRV)
261 rdmsrl(MSR_IA32_MC0_ADDR + i*4, m.addr);
263 if (!(flags & MCP_TIMESTAMP))
264 m.tsc = 0;
266 * Don't get the IP here because it's unlikely to
267 * have anything to do with the actual error location.
269 if (!(flags & MCP_DONTLOG)) {
270 mce_log(&m);
271 add_taint(TAINT_MACHINE_CHECK);
275 * Clear state for this bank.
277 wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
281 * Don't clear MCG_STATUS here because it's only defined for
282 * exceptions.
287 * The actual machine check handler. This only handles real
288 * exceptions when something got corrupted coming in through int 18.
290 * This is executed in NMI context not subject to normal locking rules. This
291 * implies that most kernel services cannot be safely used. Don't even
292 * think about putting a printk in there!
294 void do_machine_check(struct pt_regs *regs, long error_code)
296 struct mce m, panicm;
297 int panicm_found = 0;
298 u64 mcestart = 0;
299 int i;
301 * If no_way_out gets set, there is no safe way to recover from this
302 * MCE. If tolerant is cranked up, we'll try anyway.
304 int no_way_out = 0;
306 * If kill_it gets set, there might be a way to recover from this
307 * error.
309 int kill_it = 0;
310 DECLARE_BITMAP(toclear, MAX_NR_BANKS);
312 atomic_inc(&mce_entry);
314 if (notify_die(DIE_NMI, "machine check", regs, error_code,
315 18, SIGKILL) == NOTIFY_STOP)
316 goto out2;
317 if (!banks)
318 goto out2;
320 mce_setup(&m);
322 rdmsrl(MSR_IA32_MCG_STATUS, m.mcgstatus);
324 /* if the restart IP is not valid, we're done for */
325 if (!(m.mcgstatus & MCG_STATUS_RIPV))
326 no_way_out = 1;
328 rdtscll(mcestart);
329 barrier();
331 for (i = 0; i < banks; i++) {
332 __clear_bit(i, toclear);
333 if (!bank[i])
334 continue;
336 m.misc = 0;
337 m.addr = 0;
338 m.bank = i;
340 rdmsrl(MSR_IA32_MC0_STATUS + i*4, m.status);
341 if ((m.status & MCI_STATUS_VAL) == 0)
342 continue;
345 * Non uncorrected errors are handled by machine_check_poll
346 * Leave them alone.
348 if ((m.status & MCI_STATUS_UC) == 0)
349 continue;
352 * Set taint even when machine check was not enabled.
354 add_taint(TAINT_MACHINE_CHECK);
356 __set_bit(i, toclear);
358 if (m.status & MCI_STATUS_EN) {
359 /* if PCC was set, there's no way out */
360 no_way_out |= !!(m.status & MCI_STATUS_PCC);
362 * If this error was uncorrectable and there was
363 * an overflow, we're in trouble. If no overflow,
364 * we might get away with just killing a task.
366 if (m.status & MCI_STATUS_UC) {
367 if (tolerant < 1 || m.status & MCI_STATUS_OVER)
368 no_way_out = 1;
369 kill_it = 1;
371 } else {
373 * Machine check event was not enabled. Clear, but
374 * ignore.
376 continue;
379 if (m.status & MCI_STATUS_MISCV)
380 rdmsrl(MSR_IA32_MC0_MISC + i*4, m.misc);
381 if (m.status & MCI_STATUS_ADDRV)
382 rdmsrl(MSR_IA32_MC0_ADDR + i*4, m.addr);
384 mce_get_rip(&m, regs);
385 mce_log(&m);
388 * Did this bank cause the exception?
390 * Assume that the bank with uncorrectable errors did it,
391 * and that there is only a single one:
393 if ((m.status & MCI_STATUS_UC) &&
394 (m.status & MCI_STATUS_EN)) {
395 panicm = m;
396 panicm_found = 1;
401 * If we didn't find an uncorrectable error, pick
402 * the last one (shouldn't happen, just being safe).
404 if (!panicm_found)
405 panicm = m;
408 * If we have decided that we just CAN'T continue, and the user
409 * has not set tolerant to an insane level, give up and die.
411 if (no_way_out && tolerant < 3)
412 mce_panic("Machine check", &panicm, mcestart);
415 * If the error seems to be unrecoverable, something should be
416 * done. Try to kill as little as possible. If we can kill just
417 * one task, do that. If the user has set the tolerance very
418 * high, don't try to do anything at all.
420 if (kill_it && tolerant < 3) {
421 int user_space = 0;
424 * If the EIPV bit is set, it means the saved IP is the
425 * instruction which caused the MCE.
427 if (m.mcgstatus & MCG_STATUS_EIPV)
428 user_space = panicm.ip && (panicm.cs & 3);
431 * If we know that the error was in user space, send a
432 * SIGBUS. Otherwise, panic if tolerance is low.
434 * force_sig() takes an awful lot of locks and has a slight
435 * risk of deadlocking.
437 if (user_space) {
438 force_sig(SIGBUS, current);
439 } else if (panic_on_oops || tolerant < 2) {
440 mce_panic("Uncorrected machine check",
441 &panicm, mcestart);
445 /* notify userspace ASAP */
446 set_thread_flag(TIF_MCE_NOTIFY);
448 /* the last thing we do is clear state */
449 for (i = 0; i < banks; i++) {
450 if (test_bit(i, toclear))
451 wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
453 wrmsrl(MSR_IA32_MCG_STATUS, 0);
454 out2:
455 atomic_dec(&mce_entry);
458 #ifdef CONFIG_X86_MCE_INTEL
459 /***
460 * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
461 * @cpu: The CPU on which the event occurred.
462 * @status: Event status information
464 * This function should be called by the thermal interrupt after the
465 * event has been processed and the decision was made to log the event
466 * further.
468 * The status parameter will be saved to the 'status' field of 'struct mce'
469 * and historically has been the register value of the
470 * MSR_IA32_THERMAL_STATUS (Intel) msr.
472 void mce_log_therm_throt_event(__u64 status)
474 struct mce m;
476 mce_setup(&m);
477 m.bank = MCE_THERMAL_BANK;
478 m.status = status;
479 mce_log(&m);
481 #endif /* CONFIG_X86_MCE_INTEL */
484 * Periodic polling timer for "silent" machine check errors. If the
485 * poller finds an MCE, poll 2x faster. When the poller finds no more
486 * errors, poll 2x slower (up to check_interval seconds).
488 static int check_interval = 5 * 60; /* 5 minutes */
490 static DEFINE_PER_CPU(int, next_interval); /* in jiffies */
491 static DEFINE_PER_CPU(struct timer_list, mce_timer);
493 static void mcheck_timer(unsigned long data)
495 struct timer_list *t = &per_cpu(mce_timer, data);
496 int *n;
498 WARN_ON(smp_processor_id() != data);
500 if (mce_available(&current_cpu_data)) {
501 machine_check_poll(MCP_TIMESTAMP,
502 &__get_cpu_var(mce_poll_banks));
506 * Alert userspace if needed. If we logged an MCE, reduce the
507 * polling interval, otherwise increase the polling interval.
509 n = &__get_cpu_var(next_interval);
510 if (mce_notify_user()) {
511 *n = max(*n/2, HZ/100);
512 } else {
513 *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ));
516 t->expires = jiffies + *n;
517 add_timer(t);
520 static void mce_do_trigger(struct work_struct *work)
522 call_usermodehelper(trigger, trigger_argv, NULL, UMH_NO_WAIT);
525 static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
528 * Notify the user(s) about new machine check events.
529 * Can be called from interrupt context, but not from machine check/NMI
530 * context.
532 int mce_notify_user(void)
534 /* Not more than two messages every minute */
535 static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
537 clear_thread_flag(TIF_MCE_NOTIFY);
539 if (test_and_clear_bit(0, &notify_user)) {
540 wake_up_interruptible(&mce_wait);
543 * There is no risk of missing notifications because
544 * work_pending is always cleared before the function is
545 * executed.
547 if (trigger[0] && !work_pending(&mce_trigger_work))
548 schedule_work(&mce_trigger_work);
550 if (__ratelimit(&ratelimit))
551 printk(KERN_INFO "Machine check events logged\n");
553 return 1;
555 return 0;
559 * Initialize Machine Checks for a CPU.
561 static int mce_cap_init(void)
563 unsigned b;
564 u64 cap;
566 rdmsrl(MSR_IA32_MCG_CAP, cap);
568 b = cap & MCG_BANKCNT_MASK;
569 printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
571 if (b > MAX_NR_BANKS) {
572 printk(KERN_WARNING
573 "MCE: Using only %u machine check banks out of %u\n",
574 MAX_NR_BANKS, b);
575 b = MAX_NR_BANKS;
578 /* Don't support asymmetric configurations today */
579 WARN_ON(banks != 0 && b != banks);
580 banks = b;
581 if (!bank) {
582 bank = kmalloc(banks * sizeof(u64), GFP_KERNEL);
583 if (!bank)
584 return -ENOMEM;
585 memset(bank, 0xff, banks * sizeof(u64));
588 /* Use accurate RIP reporting if available. */
589 if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
590 rip_msr = MSR_IA32_MCG_EIP;
592 return 0;
595 static void mce_init(void *dummy)
597 mce_banks_t all_banks;
598 u64 cap;
599 int i;
602 * Log the machine checks left over from the previous reset.
604 bitmap_fill(all_banks, MAX_NR_BANKS);
605 machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
607 set_in_cr4(X86_CR4_MCE);
609 rdmsrl(MSR_IA32_MCG_CAP, cap);
610 if (cap & MCG_CTL_P)
611 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
613 for (i = 0; i < banks; i++) {
614 if (skip_bank_init(i))
615 continue;
616 wrmsrl(MSR_IA32_MC0_CTL+4*i, bank[i]);
617 wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
621 /* Add per CPU specific workarounds here */
622 static void mce_cpu_quirks(struct cpuinfo_x86 *c)
624 /* This should be disabled by the BIOS, but isn't always */
625 if (c->x86_vendor == X86_VENDOR_AMD) {
626 if (c->x86 == 15 && banks > 4) {
628 * disable GART TBL walk error reporting, which
629 * trips off incorrectly with the IOMMU & 3ware
630 * & Cerberus:
632 clear_bit(10, (unsigned long *)&bank[4]);
634 if (c->x86 <= 17 && mce_bootlog < 0) {
636 * Lots of broken BIOS around that don't clear them
637 * by default and leave crap in there. Don't log:
639 mce_bootlog = 0;
642 * Various K7s with broken bank 0 around. Always disable
643 * by default.
645 if (c->x86 == 6)
646 bank[0] = 0;
649 if (c->x86_vendor == X86_VENDOR_INTEL) {
651 * SDM documents that on family 6 bank 0 should not be written
652 * because it aliases to another special BIOS controlled
653 * register.
654 * But it's not aliased anymore on model 0x1a+
655 * Don't ignore bank 0 completely because there could be a
656 * valid event later, merely don't write CTL0.
659 if (c->x86 == 6 && c->x86_model < 0x1A)
660 __set_bit(0, &dont_init_banks);
664 static void mce_cpu_features(struct cpuinfo_x86 *c)
666 switch (c->x86_vendor) {
667 case X86_VENDOR_INTEL:
668 mce_intel_feature_init(c);
669 break;
670 case X86_VENDOR_AMD:
671 mce_amd_feature_init(c);
672 break;
673 default:
674 break;
678 static void mce_init_timer(void)
680 struct timer_list *t = &__get_cpu_var(mce_timer);
681 int *n = &__get_cpu_var(next_interval);
683 *n = check_interval * HZ;
684 if (!*n)
685 return;
686 setup_timer(t, mcheck_timer, smp_processor_id());
687 t->expires = round_jiffies(jiffies + *n);
688 add_timer(t);
692 * Called for each booted CPU to set up machine checks.
693 * Must be called with preempt off:
695 void __cpuinit mcheck_init(struct cpuinfo_x86 *c)
697 if (!mce_available(c))
698 return;
700 if (mce_cap_init() < 0) {
701 mce_disabled = 1;
702 return;
704 mce_cpu_quirks(c);
706 machine_check_vector = do_machine_check;
708 mce_init(NULL);
709 mce_cpu_features(c);
710 mce_init_timer();
714 * Character device to read and clear the MCE log.
717 static DEFINE_SPINLOCK(mce_state_lock);
718 static int open_count; /* #times opened */
719 static int open_exclu; /* already open exclusive? */
721 static int mce_open(struct inode *inode, struct file *file)
723 lock_kernel();
724 spin_lock(&mce_state_lock);
726 if (open_exclu || (open_count && (file->f_flags & O_EXCL))) {
727 spin_unlock(&mce_state_lock);
728 unlock_kernel();
730 return -EBUSY;
733 if (file->f_flags & O_EXCL)
734 open_exclu = 1;
735 open_count++;
737 spin_unlock(&mce_state_lock);
738 unlock_kernel();
740 return nonseekable_open(inode, file);
743 static int mce_release(struct inode *inode, struct file *file)
745 spin_lock(&mce_state_lock);
747 open_count--;
748 open_exclu = 0;
750 spin_unlock(&mce_state_lock);
752 return 0;
755 static void collect_tscs(void *data)
757 unsigned long *cpu_tsc = (unsigned long *)data;
759 rdtscll(cpu_tsc[smp_processor_id()]);
762 static DEFINE_MUTEX(mce_read_mutex);
764 static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize,
765 loff_t *off)
767 char __user *buf = ubuf;
768 unsigned long *cpu_tsc;
769 unsigned prev, next;
770 int i, err;
772 cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
773 if (!cpu_tsc)
774 return -ENOMEM;
776 mutex_lock(&mce_read_mutex);
777 next = rcu_dereference(mcelog.next);
779 /* Only supports full reads right now */
780 if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce)) {
781 mutex_unlock(&mce_read_mutex);
782 kfree(cpu_tsc);
784 return -EINVAL;
787 err = 0;
788 prev = 0;
789 do {
790 for (i = prev; i < next; i++) {
791 unsigned long start = jiffies;
793 while (!mcelog.entry[i].finished) {
794 if (time_after_eq(jiffies, start + 2)) {
795 memset(mcelog.entry + i, 0,
796 sizeof(struct mce));
797 goto timeout;
799 cpu_relax();
801 smp_rmb();
802 err |= copy_to_user(buf, mcelog.entry + i,
803 sizeof(struct mce));
804 buf += sizeof(struct mce);
805 timeout:
809 memset(mcelog.entry + prev, 0,
810 (next - prev) * sizeof(struct mce));
811 prev = next;
812 next = cmpxchg(&mcelog.next, prev, 0);
813 } while (next != prev);
815 synchronize_sched();
818 * Collect entries that were still getting written before the
819 * synchronize.
821 on_each_cpu(collect_tscs, cpu_tsc, 1);
823 for (i = next; i < MCE_LOG_LEN; i++) {
824 if (mcelog.entry[i].finished &&
825 mcelog.entry[i].tsc < cpu_tsc[mcelog.entry[i].cpu]) {
826 err |= copy_to_user(buf, mcelog.entry+i,
827 sizeof(struct mce));
828 smp_rmb();
829 buf += sizeof(struct mce);
830 memset(&mcelog.entry[i], 0, sizeof(struct mce));
833 mutex_unlock(&mce_read_mutex);
834 kfree(cpu_tsc);
836 return err ? -EFAULT : buf - ubuf;
839 static unsigned int mce_poll(struct file *file, poll_table *wait)
841 poll_wait(file, &mce_wait, wait);
842 if (rcu_dereference(mcelog.next))
843 return POLLIN | POLLRDNORM;
844 return 0;
847 static long mce_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
849 int __user *p = (int __user *)arg;
851 if (!capable(CAP_SYS_ADMIN))
852 return -EPERM;
854 switch (cmd) {
855 case MCE_GET_RECORD_LEN:
856 return put_user(sizeof(struct mce), p);
857 case MCE_GET_LOG_LEN:
858 return put_user(MCE_LOG_LEN, p);
859 case MCE_GETCLEAR_FLAGS: {
860 unsigned flags;
862 do {
863 flags = mcelog.flags;
864 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
866 return put_user(flags, p);
868 default:
869 return -ENOTTY;
873 static const struct file_operations mce_chrdev_ops = {
874 .open = mce_open,
875 .release = mce_release,
876 .read = mce_read,
877 .poll = mce_poll,
878 .unlocked_ioctl = mce_ioctl,
881 static struct miscdevice mce_log_device = {
882 MISC_MCELOG_MINOR,
883 "mcelog",
884 &mce_chrdev_ops,
888 * mce=off disables machine check
889 * mce=TOLERANCELEVEL (number, see above)
890 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
891 * mce=nobootlog Don't log MCEs from before booting.
893 static int __init mcheck_enable(char *str)
895 if (!strcmp(str, "off"))
896 mce_disabled = 1;
897 else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
898 mce_bootlog = (str[0] == 'b');
899 else if (isdigit(str[0]))
900 get_option(&str, &tolerant);
901 else {
902 printk(KERN_INFO "mce= argument %s ignored. Please use /sys\n",
903 str);
904 return 0;
906 return 1;
908 __setup("mce=", mcheck_enable);
911 * Sysfs support
915 * Disable machine checks on suspend and shutdown. We can't really handle
916 * them later.
918 static int mce_disable(void)
920 int i;
922 for (i = 0; i < banks; i++) {
923 if (!skip_bank_init(i))
924 wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
926 return 0;
929 static int mce_suspend(struct sys_device *dev, pm_message_t state)
931 return mce_disable();
934 static int mce_shutdown(struct sys_device *dev)
936 return mce_disable();
940 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
941 * Only one CPU is active at this time, the others get re-added later using
942 * CPU hotplug:
944 static int mce_resume(struct sys_device *dev)
946 mce_init(NULL);
947 mce_cpu_features(&current_cpu_data);
949 return 0;
952 static void mce_cpu_restart(void *data)
954 del_timer_sync(&__get_cpu_var(mce_timer));
955 if (mce_available(&current_cpu_data))
956 mce_init(NULL);
957 mce_init_timer();
960 /* Reinit MCEs after user configuration changes */
961 static void mce_restart(void)
963 on_each_cpu(mce_cpu_restart, NULL, 1);
966 static struct sysdev_class mce_sysclass = {
967 .suspend = mce_suspend,
968 .shutdown = mce_shutdown,
969 .resume = mce_resume,
970 .name = "machinecheck",
973 DEFINE_PER_CPU(struct sys_device, mce_dev);
975 __cpuinitdata
976 void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
978 /* Why are there no generic functions for this? */
979 #define ACCESSOR(name, var, start) \
980 static ssize_t show_ ## name(struct sys_device *s, \
981 struct sysdev_attribute *attr, \
982 char *buf) { \
983 return sprintf(buf, "%Lx\n", (u64)var); \
985 static ssize_t set_ ## name(struct sys_device *s, \
986 struct sysdev_attribute *attr, \
987 const char *buf, size_t siz) { \
988 char *end; \
989 u64 new = simple_strtoull(buf, &end, 0); \
991 if (end == buf) \
992 return -EINVAL; \
993 var = new; \
994 start; \
996 return end-buf; \
998 static SYSDEV_ATTR(name, 0644, show_ ## name, set_ ## name);
1000 static struct sysdev_attribute *bank_attrs;
1002 static ssize_t show_bank(struct sys_device *s, struct sysdev_attribute *attr,
1003 char *buf)
1005 u64 b = bank[attr - bank_attrs];
1007 return sprintf(buf, "%llx\n", b);
1010 static ssize_t set_bank(struct sys_device *s, struct sysdev_attribute *attr,
1011 const char *buf, size_t siz)
1013 char *end;
1014 u64 new = simple_strtoull(buf, &end, 0);
1016 if (end == buf)
1017 return -EINVAL;
1019 bank[attr - bank_attrs] = new;
1020 mce_restart();
1022 return end-buf;
1025 static ssize_t
1026 show_trigger(struct sys_device *s, struct sysdev_attribute *attr, char *buf)
1028 strcpy(buf, trigger);
1029 strcat(buf, "\n");
1030 return strlen(trigger) + 1;
1033 static ssize_t set_trigger(struct sys_device *s, struct sysdev_attribute *attr,
1034 const char *buf, size_t siz)
1036 char *p;
1037 int len;
1039 strncpy(trigger, buf, sizeof(trigger));
1040 trigger[sizeof(trigger)-1] = 0;
1041 len = strlen(trigger);
1042 p = strchr(trigger, '\n');
1044 if (*p)
1045 *p = 0;
1047 return len;
1050 static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger);
1051 static SYSDEV_INT_ATTR(tolerant, 0644, tolerant);
1053 ACCESSOR(check_interval, check_interval, mce_restart())
1055 static struct sysdev_attribute *mce_attrs[] = {
1056 &attr_tolerant.attr, &attr_check_interval, &attr_trigger,
1057 NULL
1060 static cpumask_var_t mce_dev_initialized;
1062 /* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */
1063 static __cpuinit int mce_create_device(unsigned int cpu)
1065 int err;
1066 int i;
1068 if (!mce_available(&boot_cpu_data))
1069 return -EIO;
1071 memset(&per_cpu(mce_dev, cpu).kobj, 0, sizeof(struct kobject));
1072 per_cpu(mce_dev, cpu).id = cpu;
1073 per_cpu(mce_dev, cpu).cls = &mce_sysclass;
1075 err = sysdev_register(&per_cpu(mce_dev, cpu));
1076 if (err)
1077 return err;
1079 for (i = 0; mce_attrs[i]; i++) {
1080 err = sysdev_create_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
1081 if (err)
1082 goto error;
1084 for (i = 0; i < banks; i++) {
1085 err = sysdev_create_file(&per_cpu(mce_dev, cpu),
1086 &bank_attrs[i]);
1087 if (err)
1088 goto error2;
1090 cpumask_set_cpu(cpu, mce_dev_initialized);
1092 return 0;
1093 error2:
1094 while (--i >= 0)
1095 sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
1096 error:
1097 while (--i >= 0)
1098 sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
1100 sysdev_unregister(&per_cpu(mce_dev, cpu));
1102 return err;
1105 static __cpuinit void mce_remove_device(unsigned int cpu)
1107 int i;
1109 if (!cpumask_test_cpu(cpu, mce_dev_initialized))
1110 return;
1112 for (i = 0; mce_attrs[i]; i++)
1113 sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
1115 for (i = 0; i < banks; i++)
1116 sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
1118 sysdev_unregister(&per_cpu(mce_dev, cpu));
1119 cpumask_clear_cpu(cpu, mce_dev_initialized);
1122 /* Make sure there are no machine checks on offlined CPUs. */
1123 static void mce_disable_cpu(void *h)
1125 unsigned long action = *(unsigned long *)h;
1126 int i;
1128 if (!mce_available(&current_cpu_data))
1129 return;
1130 if (!(action & CPU_TASKS_FROZEN))
1131 cmci_clear();
1132 for (i = 0; i < banks; i++) {
1133 if (!skip_bank_init(i))
1134 wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
1138 static void mce_reenable_cpu(void *h)
1140 unsigned long action = *(unsigned long *)h;
1141 int i;
1143 if (!mce_available(&current_cpu_data))
1144 return;
1146 if (!(action & CPU_TASKS_FROZEN))
1147 cmci_reenable();
1148 for (i = 0; i < banks; i++) {
1149 if (!skip_bank_init(i))
1150 wrmsrl(MSR_IA32_MC0_CTL + i*4, bank[i]);
1154 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
1155 static int __cpuinit
1156 mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
1158 unsigned int cpu = (unsigned long)hcpu;
1159 struct timer_list *t = &per_cpu(mce_timer, cpu);
1161 switch (action) {
1162 case CPU_ONLINE:
1163 case CPU_ONLINE_FROZEN:
1164 mce_create_device(cpu);
1165 if (threshold_cpu_callback)
1166 threshold_cpu_callback(action, cpu);
1167 break;
1168 case CPU_DEAD:
1169 case CPU_DEAD_FROZEN:
1170 if (threshold_cpu_callback)
1171 threshold_cpu_callback(action, cpu);
1172 mce_remove_device(cpu);
1173 break;
1174 case CPU_DOWN_PREPARE:
1175 case CPU_DOWN_PREPARE_FROZEN:
1176 del_timer_sync(t);
1177 smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
1178 break;
1179 case CPU_DOWN_FAILED:
1180 case CPU_DOWN_FAILED_FROZEN:
1181 t->expires = round_jiffies(jiffies +
1182 __get_cpu_var(next_interval));
1183 add_timer_on(t, cpu);
1184 smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
1185 break;
1186 case CPU_POST_DEAD:
1187 /* intentionally ignoring frozen here */
1188 cmci_rediscover(cpu);
1189 break;
1191 return NOTIFY_OK;
1194 static struct notifier_block mce_cpu_notifier __cpuinitdata = {
1195 .notifier_call = mce_cpu_callback,
1198 static __init int mce_init_banks(void)
1200 int i;
1202 bank_attrs = kzalloc(sizeof(struct sysdev_attribute) * banks,
1203 GFP_KERNEL);
1204 if (!bank_attrs)
1205 return -ENOMEM;
1207 for (i = 0; i < banks; i++) {
1208 struct sysdev_attribute *a = &bank_attrs[i];
1210 a->attr.name = kasprintf(GFP_KERNEL, "bank%d", i);
1211 if (!a->attr.name)
1212 goto nomem;
1214 a->attr.mode = 0644;
1215 a->show = show_bank;
1216 a->store = set_bank;
1218 return 0;
1220 nomem:
1221 while (--i >= 0)
1222 kfree(bank_attrs[i].attr.name);
1223 kfree(bank_attrs);
1224 bank_attrs = NULL;
1226 return -ENOMEM;
1229 static __init int mce_init_device(void)
1231 int err;
1232 int i = 0;
1234 if (!mce_available(&boot_cpu_data))
1235 return -EIO;
1237 alloc_cpumask_var(&mce_dev_initialized, GFP_KERNEL);
1239 err = mce_init_banks();
1240 if (err)
1241 return err;
1243 err = sysdev_class_register(&mce_sysclass);
1244 if (err)
1245 return err;
1247 for_each_online_cpu(i) {
1248 err = mce_create_device(i);
1249 if (err)
1250 return err;
1253 register_hotcpu_notifier(&mce_cpu_notifier);
1254 misc_register(&mce_log_device);
1256 return err;
1259 device_initcall(mce_init_device);
1261 #else /* CONFIG_X86_32: */
1263 int nr_mce_banks;
1264 EXPORT_SYMBOL_GPL(nr_mce_banks); /* non-fatal.o */
1266 /* This has to be run for each processor */
1267 void mcheck_init(struct cpuinfo_x86 *c)
1269 if (mce_disabled == 1)
1270 return;
1272 switch (c->x86_vendor) {
1273 case X86_VENDOR_AMD:
1274 amd_mcheck_init(c);
1275 break;
1277 case X86_VENDOR_INTEL:
1278 if (c->x86 == 5)
1279 intel_p5_mcheck_init(c);
1280 if (c->x86 == 6)
1281 intel_p6_mcheck_init(c);
1282 if (c->x86 == 15)
1283 intel_p4_mcheck_init(c);
1284 break;
1286 case X86_VENDOR_CENTAUR:
1287 if (c->x86 == 5)
1288 winchip_mcheck_init(c);
1289 break;
1291 default:
1292 break;
1294 printk(KERN_INFO "mce: CPU supports %d MCE banks\n", nr_mce_banks);
1297 static int __init mcheck_enable(char *str)
1299 mce_disabled = -1;
1300 return 1;
1303 __setup("mce", mcheck_enable);
1305 #endif /* CONFIG_X86_OLD_MCE */
1308 * Old style boot options parsing. Only for compatibility.
1310 static int __init mcheck_disable(char *str)
1312 mce_disabled = 1;
1313 return 1;
1315 __setup("nomce", mcheck_disable);