x86, mce: port K7 bank 0 quirk to 64bit mce code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / cpu / mcheck / mce.c
blob1336280edcc27cb40f51cc7044e6195ba5f3b616
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 #ifdef CONFIG_X86_64
44 #define MISC_MCELOG_MINOR 227
46 atomic_t mce_entry;
48 static int mce_dont_init;
51 * Tolerant levels:
52 * 0: always panic on uncorrected errors, log corrected errors
53 * 1: panic or SIGBUS on uncorrected errors, log corrected errors
54 * 2: SIGBUS or log uncorrected errors (if possible), log corrected errors
55 * 3: never panic or SIGBUS, log all errors (for testing only)
57 static int tolerant = 1;
58 static int banks;
59 static u64 *bank;
60 static unsigned long notify_user;
61 static int rip_msr;
62 static int mce_bootlog = -1;
63 static atomic_t mce_events;
65 static char trigger[128];
66 static char *trigger_argv[2] = { trigger, NULL };
68 static unsigned long dont_init_banks;
70 static DECLARE_WAIT_QUEUE_HEAD(mce_wait);
72 /* MCA banks polled by the period polling timer for corrected events */
73 DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
74 [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
77 static inline int skip_bank_init(int i)
79 return i < BITS_PER_LONG && test_bit(i, &dont_init_banks);
82 /* Do initial initialization of a struct mce */
83 void mce_setup(struct mce *m)
85 memset(m, 0, sizeof(struct mce));
86 m->cpu = smp_processor_id();
87 rdtscll(m->tsc);
91 * Lockless MCE logging infrastructure.
92 * This avoids deadlocks on printk locks without having to break locks. Also
93 * separate MCEs from kernel messages to avoid bogus bug reports.
96 static struct mce_log mcelog = {
97 MCE_LOG_SIGNATURE,
98 MCE_LOG_LEN,
101 void mce_log(struct mce *mce)
103 unsigned next, entry;
105 atomic_inc(&mce_events);
106 mce->finished = 0;
107 wmb();
108 for (;;) {
109 entry = rcu_dereference(mcelog.next);
110 for (;;) {
112 * When the buffer fills up discard new entries.
113 * Assume that the earlier errors are the more
114 * interesting ones:
116 if (entry >= MCE_LOG_LEN) {
117 set_bit(MCE_OVERFLOW, (unsigned long *)&mcelog.flags);
118 return;
120 /* Old left over entry. Skip: */
121 if (mcelog.entry[entry].finished) {
122 entry++;
123 continue;
125 break;
127 smp_rmb();
128 next = entry + 1;
129 if (cmpxchg(&mcelog.next, entry, next) == entry)
130 break;
132 memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
133 wmb();
134 mcelog.entry[entry].finished = 1;
135 wmb();
137 set_bit(0, &notify_user);
140 static void print_mce(struct mce *m)
142 printk(KERN_EMERG "\n"
143 KERN_EMERG "HARDWARE ERROR\n"
144 KERN_EMERG
145 "CPU %d: Machine Check Exception: %16Lx Bank %d: %016Lx\n",
146 m->cpu, m->mcgstatus, m->bank, m->status);
147 if (m->ip) {
148 printk(KERN_EMERG "RIP%s %02x:<%016Lx> ",
149 !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
150 m->cs, m->ip);
151 if (m->cs == __KERNEL_CS)
152 print_symbol("{%s}", m->ip);
153 printk("\n");
155 printk(KERN_EMERG "TSC %llx ", m->tsc);
156 if (m->addr)
157 printk("ADDR %llx ", m->addr);
158 if (m->misc)
159 printk("MISC %llx ", m->misc);
160 printk("\n");
161 printk(KERN_EMERG "This is not a software problem!\n");
162 printk(KERN_EMERG "Run through mcelog --ascii to decode "
163 "and contact your hardware vendor\n");
166 static void mce_panic(char *msg, struct mce *backup, u64 start)
168 int i;
170 oops_begin();
171 for (i = 0; i < MCE_LOG_LEN; i++) {
172 u64 tsc = mcelog.entry[i].tsc;
174 if ((s64)(tsc - start) < 0)
175 continue;
176 print_mce(&mcelog.entry[i]);
177 if (backup && mcelog.entry[i].tsc == backup->tsc)
178 backup = NULL;
180 if (backup)
181 print_mce(backup);
182 panic(msg);
185 int mce_available(struct cpuinfo_x86 *c)
187 if (mce_dont_init)
188 return 0;
189 return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
192 static inline void mce_get_rip(struct mce *m, struct pt_regs *regs)
194 if (regs && (m->mcgstatus & MCG_STATUS_RIPV)) {
195 m->ip = regs->ip;
196 m->cs = regs->cs;
197 } else {
198 m->ip = 0;
199 m->cs = 0;
201 if (rip_msr) {
202 /* Assume the RIP in the MSR is exact. Is this true? */
203 m->mcgstatus |= MCG_STATUS_EIPV;
204 rdmsrl(rip_msr, m->ip);
205 m->cs = 0;
210 * Poll for corrected events or events that happened before reset.
211 * Those are just logged through /dev/mcelog.
213 * This is executed in standard interrupt context.
215 void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
217 struct mce m;
218 int i;
220 mce_setup(&m);
222 rdmsrl(MSR_IA32_MCG_STATUS, m.mcgstatus);
223 for (i = 0; i < banks; i++) {
224 if (!bank[i] || !test_bit(i, *b))
225 continue;
227 m.misc = 0;
228 m.addr = 0;
229 m.bank = i;
230 m.tsc = 0;
232 barrier();
233 rdmsrl(MSR_IA32_MC0_STATUS + i*4, m.status);
234 if (!(m.status & MCI_STATUS_VAL))
235 continue;
238 * Uncorrected events are handled by the exception handler
239 * when it is enabled. But when the exception is disabled log
240 * everything.
242 * TBD do the same check for MCI_STATUS_EN here?
244 if ((m.status & MCI_STATUS_UC) && !(flags & MCP_UC))
245 continue;
247 if (m.status & MCI_STATUS_MISCV)
248 rdmsrl(MSR_IA32_MC0_MISC + i*4, m.misc);
249 if (m.status & MCI_STATUS_ADDRV)
250 rdmsrl(MSR_IA32_MC0_ADDR + i*4, m.addr);
252 if (!(flags & MCP_TIMESTAMP))
253 m.tsc = 0;
255 * Don't get the IP here because it's unlikely to
256 * have anything to do with the actual error location.
258 if (!(flags & MCP_DONTLOG)) {
259 mce_log(&m);
260 add_taint(TAINT_MACHINE_CHECK);
264 * Clear state for this bank.
266 wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
270 * Don't clear MCG_STATUS here because it's only defined for
271 * exceptions.
276 * The actual machine check handler. This only handles real
277 * exceptions when something got corrupted coming in through int 18.
279 * This is executed in NMI context not subject to normal locking rules. This
280 * implies that most kernel services cannot be safely used. Don't even
281 * think about putting a printk in there!
283 void do_machine_check(struct pt_regs *regs, long error_code)
285 struct mce m, panicm;
286 int panicm_found = 0;
287 u64 mcestart = 0;
288 int i;
290 * If no_way_out gets set, there is no safe way to recover from this
291 * MCE. If tolerant is cranked up, we'll try anyway.
293 int no_way_out = 0;
295 * If kill_it gets set, there might be a way to recover from this
296 * error.
298 int kill_it = 0;
299 DECLARE_BITMAP(toclear, MAX_NR_BANKS);
301 atomic_inc(&mce_entry);
303 if (notify_die(DIE_NMI, "machine check", regs, error_code,
304 18, SIGKILL) == NOTIFY_STOP)
305 goto out2;
306 if (!banks)
307 goto out2;
309 mce_setup(&m);
311 rdmsrl(MSR_IA32_MCG_STATUS, m.mcgstatus);
313 /* if the restart IP is not valid, we're done for */
314 if (!(m.mcgstatus & MCG_STATUS_RIPV))
315 no_way_out = 1;
317 rdtscll(mcestart);
318 barrier();
320 for (i = 0; i < banks; i++) {
321 __clear_bit(i, toclear);
322 if (!bank[i])
323 continue;
325 m.misc = 0;
326 m.addr = 0;
327 m.bank = i;
329 rdmsrl(MSR_IA32_MC0_STATUS + i*4, m.status);
330 if ((m.status & MCI_STATUS_VAL) == 0)
331 continue;
334 * Non uncorrected errors are handled by machine_check_poll
335 * Leave them alone.
337 if ((m.status & MCI_STATUS_UC) == 0)
338 continue;
341 * Set taint even when machine check was not enabled.
343 add_taint(TAINT_MACHINE_CHECK);
345 __set_bit(i, toclear);
347 if (m.status & MCI_STATUS_EN) {
348 /* if PCC was set, there's no way out */
349 no_way_out |= !!(m.status & MCI_STATUS_PCC);
351 * If this error was uncorrectable and there was
352 * an overflow, we're in trouble. If no overflow,
353 * we might get away with just killing a task.
355 if (m.status & MCI_STATUS_UC) {
356 if (tolerant < 1 || m.status & MCI_STATUS_OVER)
357 no_way_out = 1;
358 kill_it = 1;
360 } else {
362 * Machine check event was not enabled. Clear, but
363 * ignore.
365 continue;
368 if (m.status & MCI_STATUS_MISCV)
369 rdmsrl(MSR_IA32_MC0_MISC + i*4, m.misc);
370 if (m.status & MCI_STATUS_ADDRV)
371 rdmsrl(MSR_IA32_MC0_ADDR + i*4, m.addr);
373 mce_get_rip(&m, regs);
374 mce_log(&m);
377 * Did this bank cause the exception?
379 * Assume that the bank with uncorrectable errors did it,
380 * and that there is only a single one:
382 if ((m.status & MCI_STATUS_UC) &&
383 (m.status & MCI_STATUS_EN)) {
384 panicm = m;
385 panicm_found = 1;
390 * If we didn't find an uncorrectable error, pick
391 * the last one (shouldn't happen, just being safe).
393 if (!panicm_found)
394 panicm = m;
397 * If we have decided that we just CAN'T continue, and the user
398 * has not set tolerant to an insane level, give up and die.
400 if (no_way_out && tolerant < 3)
401 mce_panic("Machine check", &panicm, mcestart);
404 * If the error seems to be unrecoverable, something should be
405 * done. Try to kill as little as possible. If we can kill just
406 * one task, do that. If the user has set the tolerance very
407 * high, don't try to do anything at all.
409 if (kill_it && tolerant < 3) {
410 int user_space = 0;
413 * If the EIPV bit is set, it means the saved IP is the
414 * instruction which caused the MCE.
416 if (m.mcgstatus & MCG_STATUS_EIPV)
417 user_space = panicm.ip && (panicm.cs & 3);
420 * If we know that the error was in user space, send a
421 * SIGBUS. Otherwise, panic if tolerance is low.
423 * force_sig() takes an awful lot of locks and has a slight
424 * risk of deadlocking.
426 if (user_space) {
427 force_sig(SIGBUS, current);
428 } else if (panic_on_oops || tolerant < 2) {
429 mce_panic("Uncorrected machine check",
430 &panicm, mcestart);
434 /* notify userspace ASAP */
435 set_thread_flag(TIF_MCE_NOTIFY);
437 /* the last thing we do is clear state */
438 for (i = 0; i < banks; i++) {
439 if (test_bit(i, toclear))
440 wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
442 wrmsrl(MSR_IA32_MCG_STATUS, 0);
443 out2:
444 atomic_dec(&mce_entry);
447 #ifdef CONFIG_X86_MCE_INTEL
448 /***
449 * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
450 * @cpu: The CPU on which the event occurred.
451 * @status: Event status information
453 * This function should be called by the thermal interrupt after the
454 * event has been processed and the decision was made to log the event
455 * further.
457 * The status parameter will be saved to the 'status' field of 'struct mce'
458 * and historically has been the register value of the
459 * MSR_IA32_THERMAL_STATUS (Intel) msr.
461 void mce_log_therm_throt_event(__u64 status)
463 struct mce m;
465 mce_setup(&m);
466 m.bank = MCE_THERMAL_BANK;
467 m.status = status;
468 mce_log(&m);
470 #endif /* CONFIG_X86_MCE_INTEL */
473 * Periodic polling timer for "silent" machine check errors. If the
474 * poller finds an MCE, poll 2x faster. When the poller finds no more
475 * errors, poll 2x slower (up to check_interval seconds).
477 static int check_interval = 5 * 60; /* 5 minutes */
479 static DEFINE_PER_CPU(int, next_interval); /* in jiffies */
480 static DEFINE_PER_CPU(struct timer_list, mce_timer);
482 static void mcheck_timer(unsigned long data)
484 struct timer_list *t = &per_cpu(mce_timer, data);
485 int *n;
487 WARN_ON(smp_processor_id() != data);
489 if (mce_available(&current_cpu_data)) {
490 machine_check_poll(MCP_TIMESTAMP,
491 &__get_cpu_var(mce_poll_banks));
495 * Alert userspace if needed. If we logged an MCE, reduce the
496 * polling interval, otherwise increase the polling interval.
498 n = &__get_cpu_var(next_interval);
499 if (mce_notify_user()) {
500 *n = max(*n/2, HZ/100);
501 } else {
502 *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ));
505 t->expires = jiffies + *n;
506 add_timer(t);
509 static void mce_do_trigger(struct work_struct *work)
511 call_usermodehelper(trigger, trigger_argv, NULL, UMH_NO_WAIT);
514 static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
517 * Notify the user(s) about new machine check events.
518 * Can be called from interrupt context, but not from machine check/NMI
519 * context.
521 int mce_notify_user(void)
523 /* Not more than two messages every minute */
524 static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
526 clear_thread_flag(TIF_MCE_NOTIFY);
528 if (test_and_clear_bit(0, &notify_user)) {
529 wake_up_interruptible(&mce_wait);
532 * There is no risk of missing notifications because
533 * work_pending is always cleared before the function is
534 * executed.
536 if (trigger[0] && !work_pending(&mce_trigger_work))
537 schedule_work(&mce_trigger_work);
539 if (__ratelimit(&ratelimit))
540 printk(KERN_INFO "Machine check events logged\n");
542 return 1;
544 return 0;
547 /* see if the idle task needs to notify userspace: */
548 static int
549 mce_idle_callback(struct notifier_block *nfb, unsigned long action,
550 void *unused)
552 /* IDLE_END should be safe - interrupts are back on */
553 if (action == IDLE_END && test_thread_flag(TIF_MCE_NOTIFY))
554 mce_notify_user();
556 return NOTIFY_OK;
559 static struct notifier_block mce_idle_notifier = {
560 .notifier_call = mce_idle_callback,
563 static __init int periodic_mcheck_init(void)
565 idle_notifier_register(&mce_idle_notifier);
566 return 0;
568 __initcall(periodic_mcheck_init);
571 * Initialize Machine Checks for a CPU.
573 static int mce_cap_init(void)
575 unsigned b;
576 u64 cap;
578 rdmsrl(MSR_IA32_MCG_CAP, cap);
580 b = cap & MCG_BANKCNT_MASK;
581 printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
583 if (b > MAX_NR_BANKS) {
584 printk(KERN_WARNING
585 "MCE: Using only %u machine check banks out of %u\n",
586 MAX_NR_BANKS, b);
587 b = MAX_NR_BANKS;
590 /* Don't support asymmetric configurations today */
591 WARN_ON(banks != 0 && b != banks);
592 banks = b;
593 if (!bank) {
594 bank = kmalloc(banks * sizeof(u64), GFP_KERNEL);
595 if (!bank)
596 return -ENOMEM;
597 memset(bank, 0xff, banks * sizeof(u64));
600 /* Use accurate RIP reporting if available. */
601 if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
602 rip_msr = MSR_IA32_MCG_EIP;
604 return 0;
607 static void mce_init(void *dummy)
609 mce_banks_t all_banks;
610 u64 cap;
611 int i;
614 * Log the machine checks left over from the previous reset.
616 bitmap_fill(all_banks, MAX_NR_BANKS);
617 machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
619 set_in_cr4(X86_CR4_MCE);
621 rdmsrl(MSR_IA32_MCG_CAP, cap);
622 if (cap & MCG_CTL_P)
623 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
625 for (i = 0; i < banks; i++) {
626 if (skip_bank_init(i))
627 continue;
628 wrmsrl(MSR_IA32_MC0_CTL+4*i, bank[i]);
629 wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
633 /* Add per CPU specific workarounds here */
634 static void mce_cpu_quirks(struct cpuinfo_x86 *c)
636 /* This should be disabled by the BIOS, but isn't always */
637 if (c->x86_vendor == X86_VENDOR_AMD) {
638 if (c->x86 == 15 && banks > 4) {
640 * disable GART TBL walk error reporting, which
641 * trips off incorrectly with the IOMMU & 3ware
642 * & Cerberus:
644 clear_bit(10, (unsigned long *)&bank[4]);
646 if (c->x86 <= 17 && mce_bootlog < 0) {
648 * Lots of broken BIOS around that don't clear them
649 * by default and leave crap in there. Don't log:
651 mce_bootlog = 0;
654 * Various K7s with broken bank 0 around. Always disable
655 * by default.
657 if (c->x86 == 6)
658 bank[0] = 0;
661 if (c->x86_vendor == X86_VENDOR_INTEL) {
663 * SDM documents that on family 6 bank 0 should not be written
664 * because it aliases to another special BIOS controlled
665 * register.
666 * But it's not aliased anymore on model 0x1a+
667 * Don't ignore bank 0 completely because there could be a
668 * valid event later, merely don't write CTL0.
671 if (c->x86 == 6 && c->x86_model < 0x1A)
672 __set_bit(0, &dont_init_banks);
676 static void mce_cpu_features(struct cpuinfo_x86 *c)
678 switch (c->x86_vendor) {
679 case X86_VENDOR_INTEL:
680 mce_intel_feature_init(c);
681 break;
682 case X86_VENDOR_AMD:
683 mce_amd_feature_init(c);
684 break;
685 default:
686 break;
690 static void mce_init_timer(void)
692 struct timer_list *t = &__get_cpu_var(mce_timer);
693 int *n = &__get_cpu_var(next_interval);
695 *n = check_interval * HZ;
696 if (!*n)
697 return;
698 setup_timer(t, mcheck_timer, smp_processor_id());
699 t->expires = round_jiffies(jiffies + *n);
700 add_timer(t);
704 * Called for each booted CPU to set up machine checks.
705 * Must be called with preempt off:
707 void __cpuinit mcheck_init(struct cpuinfo_x86 *c)
709 if (!mce_available(c))
710 return;
712 if (mce_cap_init() < 0) {
713 mce_dont_init = 1;
714 return;
716 mce_cpu_quirks(c);
718 mce_init(NULL);
719 mce_cpu_features(c);
720 mce_init_timer();
724 * Character device to read and clear the MCE log.
727 static DEFINE_SPINLOCK(mce_state_lock);
728 static int open_count; /* #times opened */
729 static int open_exclu; /* already open exclusive? */
731 static int mce_open(struct inode *inode, struct file *file)
733 lock_kernel();
734 spin_lock(&mce_state_lock);
736 if (open_exclu || (open_count && (file->f_flags & O_EXCL))) {
737 spin_unlock(&mce_state_lock);
738 unlock_kernel();
740 return -EBUSY;
743 if (file->f_flags & O_EXCL)
744 open_exclu = 1;
745 open_count++;
747 spin_unlock(&mce_state_lock);
748 unlock_kernel();
750 return nonseekable_open(inode, file);
753 static int mce_release(struct inode *inode, struct file *file)
755 spin_lock(&mce_state_lock);
757 open_count--;
758 open_exclu = 0;
760 spin_unlock(&mce_state_lock);
762 return 0;
765 static void collect_tscs(void *data)
767 unsigned long *cpu_tsc = (unsigned long *)data;
769 rdtscll(cpu_tsc[smp_processor_id()]);
772 static DEFINE_MUTEX(mce_read_mutex);
774 static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize,
775 loff_t *off)
777 char __user *buf = ubuf;
778 unsigned long *cpu_tsc;
779 unsigned prev, next;
780 int i, err;
782 cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
783 if (!cpu_tsc)
784 return -ENOMEM;
786 mutex_lock(&mce_read_mutex);
787 next = rcu_dereference(mcelog.next);
789 /* Only supports full reads right now */
790 if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce)) {
791 mutex_unlock(&mce_read_mutex);
792 kfree(cpu_tsc);
794 return -EINVAL;
797 err = 0;
798 prev = 0;
799 do {
800 for (i = prev; i < next; i++) {
801 unsigned long start = jiffies;
803 while (!mcelog.entry[i].finished) {
804 if (time_after_eq(jiffies, start + 2)) {
805 memset(mcelog.entry + i, 0,
806 sizeof(struct mce));
807 goto timeout;
809 cpu_relax();
811 smp_rmb();
812 err |= copy_to_user(buf, mcelog.entry + i,
813 sizeof(struct mce));
814 buf += sizeof(struct mce);
815 timeout:
819 memset(mcelog.entry + prev, 0,
820 (next - prev) * sizeof(struct mce));
821 prev = next;
822 next = cmpxchg(&mcelog.next, prev, 0);
823 } while (next != prev);
825 synchronize_sched();
828 * Collect entries that were still getting written before the
829 * synchronize.
831 on_each_cpu(collect_tscs, cpu_tsc, 1);
833 for (i = next; i < MCE_LOG_LEN; i++) {
834 if (mcelog.entry[i].finished &&
835 mcelog.entry[i].tsc < cpu_tsc[mcelog.entry[i].cpu]) {
836 err |= copy_to_user(buf, mcelog.entry+i,
837 sizeof(struct mce));
838 smp_rmb();
839 buf += sizeof(struct mce);
840 memset(&mcelog.entry[i], 0, sizeof(struct mce));
843 mutex_unlock(&mce_read_mutex);
844 kfree(cpu_tsc);
846 return err ? -EFAULT : buf - ubuf;
849 static unsigned int mce_poll(struct file *file, poll_table *wait)
851 poll_wait(file, &mce_wait, wait);
852 if (rcu_dereference(mcelog.next))
853 return POLLIN | POLLRDNORM;
854 return 0;
857 static long mce_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
859 int __user *p = (int __user *)arg;
861 if (!capable(CAP_SYS_ADMIN))
862 return -EPERM;
864 switch (cmd) {
865 case MCE_GET_RECORD_LEN:
866 return put_user(sizeof(struct mce), p);
867 case MCE_GET_LOG_LEN:
868 return put_user(MCE_LOG_LEN, p);
869 case MCE_GETCLEAR_FLAGS: {
870 unsigned flags;
872 do {
873 flags = mcelog.flags;
874 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
876 return put_user(flags, p);
878 default:
879 return -ENOTTY;
883 static const struct file_operations mce_chrdev_ops = {
884 .open = mce_open,
885 .release = mce_release,
886 .read = mce_read,
887 .poll = mce_poll,
888 .unlocked_ioctl = mce_ioctl,
891 static struct miscdevice mce_log_device = {
892 MISC_MCELOG_MINOR,
893 "mcelog",
894 &mce_chrdev_ops,
898 * Old style boot options parsing. Only for compatibility.
900 static int __init mcheck_disable(char *str)
902 mce_dont_init = 1;
903 return 1;
905 __setup("nomce", mcheck_disable);
908 * mce=off disables machine check
909 * mce=TOLERANCELEVEL (number, see above)
910 * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
911 * mce=nobootlog Don't log MCEs from before booting.
913 static int __init mcheck_enable(char *str)
915 if (!strcmp(str, "off"))
916 mce_dont_init = 1;
917 else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
918 mce_bootlog = (str[0] == 'b');
919 else if (isdigit(str[0]))
920 get_option(&str, &tolerant);
921 else {
922 printk(KERN_INFO "mce= argument %s ignored. Please use /sys\n",
923 str);
924 return 0;
926 return 1;
928 __setup("mce=", mcheck_enable);
931 * Sysfs support
935 * Disable machine checks on suspend and shutdown. We can't really handle
936 * them later.
938 static int mce_disable(void)
940 int i;
942 for (i = 0; i < banks; i++) {
943 if (!skip_bank_init(i))
944 wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
946 return 0;
949 static int mce_suspend(struct sys_device *dev, pm_message_t state)
951 return mce_disable();
954 static int mce_shutdown(struct sys_device *dev)
956 return mce_disable();
960 * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
961 * Only one CPU is active at this time, the others get re-added later using
962 * CPU hotplug:
964 static int mce_resume(struct sys_device *dev)
966 mce_init(NULL);
967 mce_cpu_features(&current_cpu_data);
969 return 0;
972 static void mce_cpu_restart(void *data)
974 del_timer_sync(&__get_cpu_var(mce_timer));
975 if (mce_available(&current_cpu_data))
976 mce_init(NULL);
977 mce_init_timer();
980 /* Reinit MCEs after user configuration changes */
981 static void mce_restart(void)
983 on_each_cpu(mce_cpu_restart, NULL, 1);
986 static struct sysdev_class mce_sysclass = {
987 .suspend = mce_suspend,
988 .shutdown = mce_shutdown,
989 .resume = mce_resume,
990 .name = "machinecheck",
993 DEFINE_PER_CPU(struct sys_device, mce_dev);
995 __cpuinitdata
996 void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
998 /* Why are there no generic functions for this? */
999 #define ACCESSOR(name, var, start) \
1000 static ssize_t show_ ## name(struct sys_device *s, \
1001 struct sysdev_attribute *attr, \
1002 char *buf) { \
1003 return sprintf(buf, "%Lx\n", (u64)var); \
1005 static ssize_t set_ ## name(struct sys_device *s, \
1006 struct sysdev_attribute *attr, \
1007 const char *buf, size_t siz) { \
1008 char *end; \
1009 u64 new = simple_strtoull(buf, &end, 0); \
1011 if (end == buf) \
1012 return -EINVAL; \
1013 var = new; \
1014 start; \
1016 return end-buf; \
1018 static SYSDEV_ATTR(name, 0644, show_ ## name, set_ ## name);
1020 static struct sysdev_attribute *bank_attrs;
1022 static ssize_t show_bank(struct sys_device *s, struct sysdev_attribute *attr,
1023 char *buf)
1025 u64 b = bank[attr - bank_attrs];
1027 return sprintf(buf, "%llx\n", b);
1030 static ssize_t set_bank(struct sys_device *s, struct sysdev_attribute *attr,
1031 const char *buf, size_t siz)
1033 char *end;
1034 u64 new = simple_strtoull(buf, &end, 0);
1036 if (end == buf)
1037 return -EINVAL;
1039 bank[attr - bank_attrs] = new;
1040 mce_restart();
1042 return end-buf;
1045 static ssize_t
1046 show_trigger(struct sys_device *s, struct sysdev_attribute *attr, char *buf)
1048 strcpy(buf, trigger);
1049 strcat(buf, "\n");
1050 return strlen(trigger) + 1;
1053 static ssize_t set_trigger(struct sys_device *s, struct sysdev_attribute *attr,
1054 const char *buf, size_t siz)
1056 char *p;
1057 int len;
1059 strncpy(trigger, buf, sizeof(trigger));
1060 trigger[sizeof(trigger)-1] = 0;
1061 len = strlen(trigger);
1062 p = strchr(trigger, '\n');
1064 if (*p)
1065 *p = 0;
1067 return len;
1070 static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger);
1071 static SYSDEV_INT_ATTR(tolerant, 0644, tolerant);
1073 ACCESSOR(check_interval, check_interval, mce_restart())
1075 static struct sysdev_attribute *mce_attrs[] = {
1076 &attr_tolerant.attr, &attr_check_interval, &attr_trigger,
1077 NULL
1080 static cpumask_var_t mce_dev_initialized;
1082 /* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */
1083 static __cpuinit int mce_create_device(unsigned int cpu)
1085 int err;
1086 int i;
1088 if (!mce_available(&boot_cpu_data))
1089 return -EIO;
1091 memset(&per_cpu(mce_dev, cpu).kobj, 0, sizeof(struct kobject));
1092 per_cpu(mce_dev, cpu).id = cpu;
1093 per_cpu(mce_dev, cpu).cls = &mce_sysclass;
1095 err = sysdev_register(&per_cpu(mce_dev, cpu));
1096 if (err)
1097 return err;
1099 for (i = 0; mce_attrs[i]; i++) {
1100 err = sysdev_create_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
1101 if (err)
1102 goto error;
1104 for (i = 0; i < banks; i++) {
1105 err = sysdev_create_file(&per_cpu(mce_dev, cpu),
1106 &bank_attrs[i]);
1107 if (err)
1108 goto error2;
1110 cpumask_set_cpu(cpu, mce_dev_initialized);
1112 return 0;
1113 error2:
1114 while (--i >= 0)
1115 sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
1116 error:
1117 while (--i >= 0)
1118 sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
1120 sysdev_unregister(&per_cpu(mce_dev, cpu));
1122 return err;
1125 static __cpuinit void mce_remove_device(unsigned int cpu)
1127 int i;
1129 if (!cpumask_test_cpu(cpu, mce_dev_initialized))
1130 return;
1132 for (i = 0; mce_attrs[i]; i++)
1133 sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
1135 for (i = 0; i < banks; i++)
1136 sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
1138 sysdev_unregister(&per_cpu(mce_dev, cpu));
1139 cpumask_clear_cpu(cpu, mce_dev_initialized);
1142 /* Make sure there are no machine checks on offlined CPUs. */
1143 static void mce_disable_cpu(void *h)
1145 unsigned long action = *(unsigned long *)h;
1146 int i;
1148 if (!mce_available(&current_cpu_data))
1149 return;
1150 if (!(action & CPU_TASKS_FROZEN))
1151 cmci_clear();
1152 for (i = 0; i < banks; i++) {
1153 if (!skip_bank_init(i))
1154 wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
1158 static void mce_reenable_cpu(void *h)
1160 unsigned long action = *(unsigned long *)h;
1161 int i;
1163 if (!mce_available(&current_cpu_data))
1164 return;
1166 if (!(action & CPU_TASKS_FROZEN))
1167 cmci_reenable();
1168 for (i = 0; i < banks; i++) {
1169 if (!skip_bank_init(i))
1170 wrmsrl(MSR_IA32_MC0_CTL + i*4, bank[i]);
1174 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
1175 static int __cpuinit
1176 mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
1178 unsigned int cpu = (unsigned long)hcpu;
1179 struct timer_list *t = &per_cpu(mce_timer, cpu);
1181 switch (action) {
1182 case CPU_ONLINE:
1183 case CPU_ONLINE_FROZEN:
1184 mce_create_device(cpu);
1185 if (threshold_cpu_callback)
1186 threshold_cpu_callback(action, cpu);
1187 break;
1188 case CPU_DEAD:
1189 case CPU_DEAD_FROZEN:
1190 if (threshold_cpu_callback)
1191 threshold_cpu_callback(action, cpu);
1192 mce_remove_device(cpu);
1193 break;
1194 case CPU_DOWN_PREPARE:
1195 case CPU_DOWN_PREPARE_FROZEN:
1196 del_timer_sync(t);
1197 smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
1198 break;
1199 case CPU_DOWN_FAILED:
1200 case CPU_DOWN_FAILED_FROZEN:
1201 t->expires = round_jiffies(jiffies +
1202 __get_cpu_var(next_interval));
1203 add_timer_on(t, cpu);
1204 smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
1205 break;
1206 case CPU_POST_DEAD:
1207 /* intentionally ignoring frozen here */
1208 cmci_rediscover(cpu);
1209 break;
1211 return NOTIFY_OK;
1214 static struct notifier_block mce_cpu_notifier __cpuinitdata = {
1215 .notifier_call = mce_cpu_callback,
1218 static __init int mce_init_banks(void)
1220 int i;
1222 bank_attrs = kzalloc(sizeof(struct sysdev_attribute) * banks,
1223 GFP_KERNEL);
1224 if (!bank_attrs)
1225 return -ENOMEM;
1227 for (i = 0; i < banks; i++) {
1228 struct sysdev_attribute *a = &bank_attrs[i];
1230 a->attr.name = kasprintf(GFP_KERNEL, "bank%d", i);
1231 if (!a->attr.name)
1232 goto nomem;
1234 a->attr.mode = 0644;
1235 a->show = show_bank;
1236 a->store = set_bank;
1238 return 0;
1240 nomem:
1241 while (--i >= 0)
1242 kfree(bank_attrs[i].attr.name);
1243 kfree(bank_attrs);
1244 bank_attrs = NULL;
1246 return -ENOMEM;
1249 static __init int mce_init_device(void)
1251 int err;
1252 int i = 0;
1254 if (!mce_available(&boot_cpu_data))
1255 return -EIO;
1257 alloc_cpumask_var(&mce_dev_initialized, GFP_KERNEL);
1259 err = mce_init_banks();
1260 if (err)
1261 return err;
1263 err = sysdev_class_register(&mce_sysclass);
1264 if (err)
1265 return err;
1267 for_each_online_cpu(i) {
1268 err = mce_create_device(i);
1269 if (err)
1270 return err;
1273 register_hotcpu_notifier(&mce_cpu_notifier);
1274 misc_register(&mce_log_device);
1276 return err;
1279 device_initcall(mce_init_device);
1281 #else /* CONFIG_X86_32: */
1283 int mce_disabled;
1285 int nr_mce_banks;
1286 EXPORT_SYMBOL_GPL(nr_mce_banks); /* non-fatal.o */
1288 /* Handle unconfigured int18 (should never happen) */
1289 static void unexpected_machine_check(struct pt_regs *regs, long error_code)
1291 printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n",
1292 smp_processor_id());
1295 /* Call the installed machine check handler for this CPU setup. */
1296 void (*machine_check_vector)(struct pt_regs *, long error_code) =
1297 unexpected_machine_check;
1299 /* This has to be run for each processor */
1300 void mcheck_init(struct cpuinfo_x86 *c)
1302 if (mce_disabled == 1)
1303 return;
1305 switch (c->x86_vendor) {
1306 case X86_VENDOR_AMD:
1307 amd_mcheck_init(c);
1308 break;
1310 case X86_VENDOR_INTEL:
1311 if (c->x86 == 5)
1312 intel_p5_mcheck_init(c);
1313 if (c->x86 == 6)
1314 intel_p6_mcheck_init(c);
1315 if (c->x86 == 15)
1316 intel_p4_mcheck_init(c);
1317 break;
1319 case X86_VENDOR_CENTAUR:
1320 if (c->x86 == 5)
1321 winchip_mcheck_init(c);
1322 break;
1324 default:
1325 break;
1327 printk(KERN_INFO "mce: CPU supports %d MCE banks\n", nr_mce_banks);
1330 static int __init mcheck_disable(char *str)
1332 mce_disabled = 1;
1333 return 1;
1336 static int __init mcheck_enable(char *str)
1338 mce_disabled = -1;
1339 return 1;
1342 __setup("nomce", mcheck_disable);
1343 __setup("mce", mcheck_enable);
1345 #endif /* CONFIG_X86_32 */