x86: correctly report NR_BANKS in mce_64.c
[pandora-kernel.git] / arch / x86 / kernel / cpu / mcheck / mce_64.c
1 /*
2  * Machine check handler.
3  * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
4  * Rest from unknown author(s).
5  * 2004 Andi Kleen. Rewrote most of it.
6  */
7
8 #include <linux/init.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
13 #include <linux/rcupdate.h>
14 #include <linux/kallsyms.h>
15 #include <linux/sysdev.h>
16 #include <linux/miscdevice.h>
17 #include <linux/fs.h>
18 #include <linux/capability.h>
19 #include <linux/cpu.h>
20 #include <linux/percpu.h>
21 #include <linux/poll.h>
22 #include <linux/thread_info.h>
23 #include <linux/ctype.h>
24 #include <linux/kmod.h>
25 #include <linux/kdebug.h>
26 #include <asm/processor.h>
27 #include <asm/msr.h>
28 #include <asm/mce.h>
29 #include <asm/uaccess.h>
30 #include <asm/smp.h>
31 #include <asm/idle.h>
32
33 #define MISC_MCELOG_MINOR 227
34 #define NR_SYSFS_BANKS 6
35
36 atomic_t mce_entry;
37
38 static int mce_dont_init;
39
40 /*
41  * Tolerant levels:
42  *   0: always panic on uncorrected errors, log corrected errors
43  *   1: panic or SIGBUS on uncorrected errors, log corrected errors
44  *   2: SIGBUS or log uncorrected errors (if possible), log corrected errors
45  *   3: never panic or SIGBUS, log all errors (for testing only)
46  */
47 static int tolerant = 1;
48 static int banks;
49 static unsigned long bank[NR_SYSFS_BANKS] = { [0 ... NR_SYSFS_BANKS-1] = ~0UL };
50 static unsigned long notify_user;
51 static int rip_msr;
52 static int mce_bootlog = -1;
53 static atomic_t mce_events;
54
55 static char trigger[128];
56 static char *trigger_argv[2] = { trigger, NULL };
57
58 static DECLARE_WAIT_QUEUE_HEAD(mce_wait);
59
60 /*
61  * Lockless MCE logging infrastructure.
62  * This avoids deadlocks on printk locks without having to break locks. Also
63  * separate MCEs from kernel messages to avoid bogus bug reports.
64  */
65
66 static struct mce_log mcelog = {
67         MCE_LOG_SIGNATURE,
68         MCE_LOG_LEN,
69 };
70
71 void mce_log(struct mce *mce)
72 {
73         unsigned next, entry;
74         atomic_inc(&mce_events);
75         mce->finished = 0;
76         wmb();
77         for (;;) {
78                 entry = rcu_dereference(mcelog.next);
79                 for (;;) {
80                         /* When the buffer fills up discard new entries. Assume
81                            that the earlier errors are the more interesting. */
82                         if (entry >= MCE_LOG_LEN) {
83                                 set_bit(MCE_OVERFLOW, (unsigned long *)&mcelog.flags);
84                                 return;
85                         }
86                         /* Old left over entry. Skip. */
87                         if (mcelog.entry[entry].finished) {
88                                 entry++;
89                                 continue;
90                         }
91                         break;
92                 }
93                 smp_rmb();
94                 next = entry + 1;
95                 if (cmpxchg(&mcelog.next, entry, next) == entry)
96                         break;
97         }
98         memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
99         wmb();
100         mcelog.entry[entry].finished = 1;
101         wmb();
102
103         set_bit(0, &notify_user);
104 }
105
106 static void print_mce(struct mce *m)
107 {
108         printk(KERN_EMERG "\n"
109                KERN_EMERG "HARDWARE ERROR\n"
110                KERN_EMERG
111                "CPU %d: Machine Check Exception: %16Lx Bank %d: %016Lx\n",
112                m->cpu, m->mcgstatus, m->bank, m->status);
113         if (m->ip) {
114                 printk(KERN_EMERG "RIP%s %02x:<%016Lx> ",
115                        !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
116                        m->cs, m->ip);
117                 if (m->cs == __KERNEL_CS)
118                         print_symbol("{%s}", m->ip);
119                 printk("\n");
120         }
121         printk(KERN_EMERG "TSC %Lx ", m->tsc);
122         if (m->addr)
123                 printk("ADDR %Lx ", m->addr);
124         if (m->misc)
125                 printk("MISC %Lx ", m->misc);
126         printk("\n");
127         printk(KERN_EMERG "This is not a software problem!\n");
128         printk(KERN_EMERG "Run through mcelog --ascii to decode "
129                "and contact your hardware vendor\n");
130 }
131
132 static void mce_panic(char *msg, struct mce *backup, unsigned long start)
133 {
134         int i;
135
136         oops_begin();
137         for (i = 0; i < MCE_LOG_LEN; i++) {
138                 unsigned long tsc = mcelog.entry[i].tsc;
139
140                 if (time_before(tsc, start))
141                         continue;
142                 print_mce(&mcelog.entry[i]);
143                 if (backup && mcelog.entry[i].tsc == backup->tsc)
144                         backup = NULL;
145         }
146         if (backup)
147                 print_mce(backup);
148         panic(msg);
149 }
150
151 static int mce_available(struct cpuinfo_x86 *c)
152 {
153         return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
154 }
155
156 static inline void mce_get_rip(struct mce *m, struct pt_regs *regs)
157 {
158         if (regs && (m->mcgstatus & MCG_STATUS_RIPV)) {
159                 m->ip = regs->ip;
160                 m->cs = regs->cs;
161         } else {
162                 m->ip = 0;
163                 m->cs = 0;
164         }
165         if (rip_msr) {
166                 /* Assume the RIP in the MSR is exact. Is this true? */
167                 m->mcgstatus |= MCG_STATUS_EIPV;
168                 rdmsrl(rip_msr, m->ip);
169                 m->cs = 0;
170         }
171 }
172
173 /*
174  * The actual machine check handler
175  */
176 void do_machine_check(struct pt_regs * regs, long error_code)
177 {
178         struct mce m, panicm;
179         u64 mcestart = 0;
180         int i;
181         int panicm_found = 0;
182         /*
183          * If no_way_out gets set, there is no safe way to recover from this
184          * MCE.  If tolerant is cranked up, we'll try anyway.
185          */
186         int no_way_out = 0;
187         /*
188          * If kill_it gets set, there might be a way to recover from this
189          * error.
190          */
191         int kill_it = 0;
192
193         atomic_inc(&mce_entry);
194
195         if ((regs
196              && notify_die(DIE_NMI, "machine check", regs, error_code,
197                            18, SIGKILL) == NOTIFY_STOP)
198             || !banks)
199                 goto out2;
200
201         memset(&m, 0, sizeof(struct mce));
202         m.cpu = smp_processor_id();
203         rdmsrl(MSR_IA32_MCG_STATUS, m.mcgstatus);
204         /* if the restart IP is not valid, we're done for */
205         if (!(m.mcgstatus & MCG_STATUS_RIPV))
206                 no_way_out = 1;
207
208         rdtscll(mcestart);
209         barrier();
210
211         for (i = 0; i < banks; i++) {
212                 if (i < NR_SYSFS_BANKS && !bank[i])
213                         continue;
214
215                 m.misc = 0;
216                 m.addr = 0;
217                 m.bank = i;
218                 m.tsc = 0;
219
220                 rdmsrl(MSR_IA32_MC0_STATUS + i*4, m.status);
221                 if ((m.status & MCI_STATUS_VAL) == 0)
222                         continue;
223
224                 if (m.status & MCI_STATUS_EN) {
225                         /* if PCC was set, there's no way out */
226                         no_way_out |= !!(m.status & MCI_STATUS_PCC);
227                         /*
228                          * If this error was uncorrectable and there was
229                          * an overflow, we're in trouble.  If no overflow,
230                          * we might get away with just killing a task.
231                          */
232                         if (m.status & MCI_STATUS_UC) {
233                                 if (tolerant < 1 || m.status & MCI_STATUS_OVER)
234                                         no_way_out = 1;
235                                 kill_it = 1;
236                         }
237                 }
238
239                 if (m.status & MCI_STATUS_MISCV)
240                         rdmsrl(MSR_IA32_MC0_MISC + i*4, m.misc);
241                 if (m.status & MCI_STATUS_ADDRV)
242                         rdmsrl(MSR_IA32_MC0_ADDR + i*4, m.addr);
243
244                 mce_get_rip(&m, regs);
245                 if (error_code >= 0)
246                         rdtscll(m.tsc);
247                 if (error_code != -2)
248                         mce_log(&m);
249
250                 /* Did this bank cause the exception? */
251                 /* Assume that the bank with uncorrectable errors did it,
252                    and that there is only a single one. */
253                 if ((m.status & MCI_STATUS_UC) && (m.status & MCI_STATUS_EN)) {
254                         panicm = m;
255                         panicm_found = 1;
256                 }
257
258                 add_taint(TAINT_MACHINE_CHECK);
259         }
260
261         /* Never do anything final in the polling timer */
262         if (!regs)
263                 goto out;
264
265         /* If we didn't find an uncorrectable error, pick
266            the last one (shouldn't happen, just being safe). */
267         if (!panicm_found)
268                 panicm = m;
269
270         /*
271          * If we have decided that we just CAN'T continue, and the user
272          *  has not set tolerant to an insane level, give up and die.
273          */
274         if (no_way_out && tolerant < 3)
275                 mce_panic("Machine check", &panicm, mcestart);
276
277         /*
278          * If the error seems to be unrecoverable, something should be
279          * done.  Try to kill as little as possible.  If we can kill just
280          * one task, do that.  If the user has set the tolerance very
281          * high, don't try to do anything at all.
282          */
283         if (kill_it && tolerant < 3) {
284                 int user_space = 0;
285
286                 /*
287                  * If the EIPV bit is set, it means the saved IP is the
288                  * instruction which caused the MCE.
289                  */
290                 if (m.mcgstatus & MCG_STATUS_EIPV)
291                         user_space = panicm.ip && (panicm.cs & 3);
292
293                 /*
294                  * If we know that the error was in user space, send a
295                  * SIGBUS.  Otherwise, panic if tolerance is low.
296                  *
297                  * do_exit() takes an awful lot of locks and has a slight
298                  * risk of deadlocking.
299                  */
300                 if (user_space) {
301                         do_exit(SIGBUS);
302                 } else if (panic_on_oops || tolerant < 2) {
303                         mce_panic("Uncorrected machine check",
304                                 &panicm, mcestart);
305                 }
306         }
307
308         /* notify userspace ASAP */
309         set_thread_flag(TIF_MCE_NOTIFY);
310
311  out:
312         /* the last thing we do is clear state */
313         for (i = 0; i < banks; i++)
314                 wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
315         wrmsrl(MSR_IA32_MCG_STATUS, 0);
316  out2:
317         atomic_dec(&mce_entry);
318 }
319
320 #ifdef CONFIG_X86_MCE_INTEL
321 /***
322  * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
323  * @cpu: The CPU on which the event occurred.
324  * @status: Event status information
325  *
326  * This function should be called by the thermal interrupt after the
327  * event has been processed and the decision was made to log the event
328  * further.
329  *
330  * The status parameter will be saved to the 'status' field of 'struct mce'
331  * and historically has been the register value of the
332  * MSR_IA32_THERMAL_STATUS (Intel) msr.
333  */
334 void mce_log_therm_throt_event(unsigned int cpu, __u64 status)
335 {
336         struct mce m;
337
338         memset(&m, 0, sizeof(m));
339         m.cpu = cpu;
340         m.bank = MCE_THERMAL_BANK;
341         m.status = status;
342         rdtscll(m.tsc);
343         mce_log(&m);
344 }
345 #endif /* CONFIG_X86_MCE_INTEL */
346
347 /*
348  * Periodic polling timer for "silent" machine check errors.  If the
349  * poller finds an MCE, poll 2x faster.  When the poller finds no more
350  * errors, poll 2x slower (up to check_interval seconds).
351  */
352
353 static int check_interval = 5 * 60; /* 5 minutes */
354 static int next_interval; /* in jiffies */
355 static void mcheck_timer(struct work_struct *work);
356 static DECLARE_DELAYED_WORK(mcheck_work, mcheck_timer);
357
358 static void mcheck_check_cpu(void *info)
359 {
360         if (mce_available(&current_cpu_data))
361                 do_machine_check(NULL, 0);
362 }
363
364 static void mcheck_timer(struct work_struct *work)
365 {
366         on_each_cpu(mcheck_check_cpu, NULL, 1, 1);
367
368         /*
369          * Alert userspace if needed.  If we logged an MCE, reduce the
370          * polling interval, otherwise increase the polling interval.
371          */
372         if (mce_notify_user()) {
373                 next_interval = max(next_interval/2, HZ/100);
374         } else {
375                 next_interval = min(next_interval * 2,
376                                 (int)round_jiffies_relative(check_interval*HZ));
377         }
378
379         schedule_delayed_work(&mcheck_work, next_interval);
380 }
381
382 /*
383  * This is only called from process context.  This is where we do
384  * anything we need to alert userspace about new MCEs.  This is called
385  * directly from the poller and also from entry.S and idle, thanks to
386  * TIF_MCE_NOTIFY.
387  */
388 int mce_notify_user(void)
389 {
390         clear_thread_flag(TIF_MCE_NOTIFY);
391         if (test_and_clear_bit(0, &notify_user)) {
392                 static unsigned long last_print;
393                 unsigned long now = jiffies;
394
395                 wake_up_interruptible(&mce_wait);
396                 if (trigger[0])
397                         call_usermodehelper(trigger, trigger_argv, NULL,
398                                                 UMH_NO_WAIT);
399
400                 if (time_after_eq(now, last_print + (check_interval*HZ))) {
401                         last_print = now;
402                         printk(KERN_INFO "Machine check events logged\n");
403                 }
404
405                 return 1;
406         }
407         return 0;
408 }
409
410 /* see if the idle task needs to notify userspace */
411 static int
412 mce_idle_callback(struct notifier_block *nfb, unsigned long action, void *junk)
413 {
414         /* IDLE_END should be safe - interrupts are back on */
415         if (action == IDLE_END && test_thread_flag(TIF_MCE_NOTIFY))
416                 mce_notify_user();
417
418         return NOTIFY_OK;
419 }
420
421 static struct notifier_block mce_idle_notifier = {
422         .notifier_call = mce_idle_callback,
423 };
424
425 static __init int periodic_mcheck_init(void)
426 {
427         next_interval = check_interval * HZ;
428         if (next_interval)
429                 schedule_delayed_work(&mcheck_work,
430                                       round_jiffies_relative(next_interval));
431         idle_notifier_register(&mce_idle_notifier);
432         return 0;
433 }
434 __initcall(periodic_mcheck_init);
435
436
437 /*
438  * Initialize Machine Checks for a CPU.
439  */
440 static void mce_init(void *dummy)
441 {
442         u64 cap;
443         int i;
444
445         rdmsrl(MSR_IA32_MCG_CAP, cap);
446         banks = cap & 0xff;
447         if (banks > MCE_EXTENDED_BANK) {
448                 banks = MCE_EXTENDED_BANK;
449                 printk(KERN_INFO "MCE: warning: using only %d banks\n",
450                        MCE_EXTENDED_BANK);
451         }
452         /* Use accurate RIP reporting if available. */
453         if ((cap & (1<<9)) && ((cap >> 16) & 0xff) >= 9)
454                 rip_msr = MSR_IA32_MCG_EIP;
455
456         /* Log the machine checks left over from the previous reset.
457            This also clears all registers */
458         do_machine_check(NULL, mce_bootlog ? -1 : -2);
459
460         set_in_cr4(X86_CR4_MCE);
461
462         if (cap & MCG_CTL_P)
463                 wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
464
465         for (i = 0; i < banks; i++) {
466                 wrmsrl(MSR_IA32_MC0_CTL+4*i, ~0UL);
467                 wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
468         }
469 }
470
471 /* Add per CPU specific workarounds here */
472 static void __cpuinit mce_cpu_quirks(struct cpuinfo_x86 *c)
473 {
474         /* This should be disabled by the BIOS, but isn't always */
475         if (c->x86_vendor == X86_VENDOR_AMD) {
476                 if(c->x86 == 15)
477                         /* disable GART TBL walk error reporting, which trips off
478                            incorrectly with the IOMMU & 3ware & Cerberus. */
479                         clear_bit(10, &bank[4]);
480                 if(c->x86 <= 17 && mce_bootlog < 0)
481                         /* Lots of broken BIOS around that don't clear them
482                            by default and leave crap in there. Don't log. */
483                         mce_bootlog = 0;
484         }
485
486 }
487
488 static void __cpuinit mce_cpu_features(struct cpuinfo_x86 *c)
489 {
490         switch (c->x86_vendor) {
491         case X86_VENDOR_INTEL:
492                 mce_intel_feature_init(c);
493                 break;
494         case X86_VENDOR_AMD:
495                 mce_amd_feature_init(c);
496                 break;
497         default:
498                 break;
499         }
500 }
501
502 /*
503  * Called for each booted CPU to set up machine checks.
504  * Must be called with preempt off.
505  */
506 void __cpuinit mcheck_init(struct cpuinfo_x86 *c)
507 {
508         static cpumask_t mce_cpus = CPU_MASK_NONE;
509
510         mce_cpu_quirks(c);
511
512         if (mce_dont_init ||
513             cpu_test_and_set(smp_processor_id(), mce_cpus) ||
514             !mce_available(c))
515                 return;
516
517         mce_init(NULL);
518         mce_cpu_features(c);
519 }
520
521 /*
522  * Character device to read and clear the MCE log.
523  */
524
525 static DEFINE_SPINLOCK(mce_state_lock);
526 static int open_count;  /* #times opened */
527 static int open_exclu;  /* already open exclusive? */
528
529 static int mce_open(struct inode *inode, struct file *file)
530 {
531         spin_lock(&mce_state_lock);
532
533         if (open_exclu || (open_count && (file->f_flags & O_EXCL))) {
534                 spin_unlock(&mce_state_lock);
535                 return -EBUSY;
536         }
537
538         if (file->f_flags & O_EXCL)
539                 open_exclu = 1;
540         open_count++;
541
542         spin_unlock(&mce_state_lock);
543
544         return nonseekable_open(inode, file);
545 }
546
547 static int mce_release(struct inode *inode, struct file *file)
548 {
549         spin_lock(&mce_state_lock);
550
551         open_count--;
552         open_exclu = 0;
553
554         spin_unlock(&mce_state_lock);
555
556         return 0;
557 }
558
559 static void collect_tscs(void *data)
560 {
561         unsigned long *cpu_tsc = (unsigned long *)data;
562
563         rdtscll(cpu_tsc[smp_processor_id()]);
564 }
565
566 static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize,
567                         loff_t *off)
568 {
569         unsigned long *cpu_tsc;
570         static DEFINE_MUTEX(mce_read_mutex);
571         unsigned next;
572         char __user *buf = ubuf;
573         int i, err;
574
575         cpu_tsc = kmalloc(NR_CPUS * sizeof(long), GFP_KERNEL);
576         if (!cpu_tsc)
577                 return -ENOMEM;
578
579         mutex_lock(&mce_read_mutex);
580         next = rcu_dereference(mcelog.next);
581
582         /* Only supports full reads right now */
583         if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce)) {
584                 mutex_unlock(&mce_read_mutex);
585                 kfree(cpu_tsc);
586                 return -EINVAL;
587         }
588
589         err = 0;
590         for (i = 0; i < next; i++) {
591                 unsigned long start = jiffies;
592
593                 while (!mcelog.entry[i].finished) {
594                         if (time_after_eq(jiffies, start + 2)) {
595                                 memset(mcelog.entry + i,0, sizeof(struct mce));
596                                 goto timeout;
597                         }
598                         cpu_relax();
599                 }
600                 smp_rmb();
601                 err |= copy_to_user(buf, mcelog.entry + i, sizeof(struct mce));
602                 buf += sizeof(struct mce);
603  timeout:
604                 ;
605         }
606
607         memset(mcelog.entry, 0, next * sizeof(struct mce));
608         mcelog.next = 0;
609
610         synchronize_sched();
611
612         /*
613          * Collect entries that were still getting written before the
614          * synchronize.
615          */
616         on_each_cpu(collect_tscs, cpu_tsc, 1, 1);
617         for (i = next; i < MCE_LOG_LEN; i++) {
618                 if (mcelog.entry[i].finished &&
619                     mcelog.entry[i].tsc < cpu_tsc[mcelog.entry[i].cpu]) {
620                         err |= copy_to_user(buf, mcelog.entry+i,
621                                             sizeof(struct mce));
622                         smp_rmb();
623                         buf += sizeof(struct mce);
624                         memset(&mcelog.entry[i], 0, sizeof(struct mce));
625                 }
626         }
627         mutex_unlock(&mce_read_mutex);
628         kfree(cpu_tsc);
629         return err ? -EFAULT : buf - ubuf;
630 }
631
632 static unsigned int mce_poll(struct file *file, poll_table *wait)
633 {
634         poll_wait(file, &mce_wait, wait);
635         if (rcu_dereference(mcelog.next))
636                 return POLLIN | POLLRDNORM;
637         return 0;
638 }
639
640 static long mce_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
641 {
642         int __user *p = (int __user *)arg;
643
644         if (!capable(CAP_SYS_ADMIN))
645                 return -EPERM;
646         switch (cmd) {
647         case MCE_GET_RECORD_LEN:
648                 return put_user(sizeof(struct mce), p);
649         case MCE_GET_LOG_LEN:
650                 return put_user(MCE_LOG_LEN, p);
651         case MCE_GETCLEAR_FLAGS: {
652                 unsigned flags;
653
654                 do {
655                         flags = mcelog.flags;
656                 } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
657                 return put_user(flags, p);
658         }
659         default:
660                 return -ENOTTY;
661         }
662 }
663
664 static const struct file_operations mce_chrdev_ops = {
665         .open = mce_open,
666         .release = mce_release,
667         .read = mce_read,
668         .poll = mce_poll,
669         .unlocked_ioctl = mce_ioctl,
670 };
671
672 static struct miscdevice mce_log_device = {
673         MISC_MCELOG_MINOR,
674         "mcelog",
675         &mce_chrdev_ops,
676 };
677
678 static unsigned long old_cr4 __initdata;
679
680 void __init stop_mce(void)
681 {
682         old_cr4 = read_cr4();
683         clear_in_cr4(X86_CR4_MCE);
684 }
685
686 void __init restart_mce(void)
687 {
688         if (old_cr4 & X86_CR4_MCE)
689                 set_in_cr4(X86_CR4_MCE);
690 }
691
692 /*
693  * Old style boot options parsing. Only for compatibility.
694  */
695 static int __init mcheck_disable(char *str)
696 {
697         mce_dont_init = 1;
698         return 1;
699 }
700
701 /* mce=off disables machine check. Note you can re-enable it later
702    using sysfs.
703    mce=TOLERANCELEVEL (number, see above)
704    mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
705    mce=nobootlog Don't log MCEs from before booting. */
706 static int __init mcheck_enable(char *str)
707 {
708         if (!strcmp(str, "off"))
709                 mce_dont_init = 1;
710         else if (!strcmp(str, "bootlog") || !strcmp(str,"nobootlog"))
711                 mce_bootlog = str[0] == 'b';
712         else if (isdigit(str[0]))
713                 get_option(&str, &tolerant);
714         else
715                 printk("mce= argument %s ignored. Please use /sys", str);
716         return 1;
717 }
718
719 __setup("nomce", mcheck_disable);
720 __setup("mce=", mcheck_enable);
721
722 /*
723  * Sysfs support
724  */
725
726 /* On resume clear all MCE state. Don't want to see leftovers from the BIOS.
727    Only one CPU is active at this time, the others get readded later using
728    CPU hotplug. */
729 static int mce_resume(struct sys_device *dev)
730 {
731         mce_init(NULL);
732         return 0;
733 }
734
735 /* Reinit MCEs after user configuration changes */
736 static void mce_restart(void)
737 {
738         if (next_interval)
739                 cancel_delayed_work(&mcheck_work);
740         /* Timer race is harmless here */
741         on_each_cpu(mce_init, NULL, 1, 1);
742         next_interval = check_interval * HZ;
743         if (next_interval)
744                 schedule_delayed_work(&mcheck_work,
745                                       round_jiffies_relative(next_interval));
746 }
747
748 static struct sysdev_class mce_sysclass = {
749         .resume = mce_resume,
750         .name = "machinecheck",
751 };
752
753 DEFINE_PER_CPU(struct sys_device, device_mce);
754
755 /* Why are there no generic functions for this? */
756 #define ACCESSOR(name, var, start) \
757         static ssize_t show_ ## name(struct sys_device *s, char *buf) { \
758                 return sprintf(buf, "%lx\n", (unsigned long)var);       \
759         }                                                               \
760         static ssize_t set_ ## name(struct sys_device *s,const char *buf,size_t siz) { \
761                 char *end;                                              \
762                 unsigned long new = simple_strtoul(buf, &end, 0);       \
763                 if (end == buf) return -EINVAL;                         \
764                 var = new;                                              \
765                 start;                                                  \
766                 return end-buf;                                         \
767         }                                                               \
768         static SYSDEV_ATTR(name, 0644, show_ ## name, set_ ## name);
769
770 /*
771  * TBD should generate these dynamically based on number of available banks.
772  * Have only 6 contol banks in /sysfs until then.
773  */
774 ACCESSOR(bank0ctl,bank[0],mce_restart())
775 ACCESSOR(bank1ctl,bank[1],mce_restart())
776 ACCESSOR(bank2ctl,bank[2],mce_restart())
777 ACCESSOR(bank3ctl,bank[3],mce_restart())
778 ACCESSOR(bank4ctl,bank[4],mce_restart())
779 ACCESSOR(bank5ctl,bank[5],mce_restart())
780
781 static ssize_t show_trigger(struct sys_device *s, char *buf)
782 {
783         strcpy(buf, trigger);
784         strcat(buf, "\n");
785         return strlen(trigger) + 1;
786 }
787
788 static ssize_t set_trigger(struct sys_device *s,const char *buf,size_t siz)
789 {
790         char *p;
791         int len;
792         strncpy(trigger, buf, sizeof(trigger));
793         trigger[sizeof(trigger)-1] = 0;
794         len = strlen(trigger);
795         p = strchr(trigger, '\n');
796         if (*p) *p = 0;
797         return len;
798 }
799
800 static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger);
801 ACCESSOR(tolerant,tolerant,)
802 ACCESSOR(check_interval,check_interval,mce_restart())
803 static struct sysdev_attribute *mce_attributes[] = {
804         &attr_bank0ctl, &attr_bank1ctl, &attr_bank2ctl,
805         &attr_bank3ctl, &attr_bank4ctl, &attr_bank5ctl,
806         &attr_tolerant, &attr_check_interval, &attr_trigger,
807         NULL
808 };
809
810 static cpumask_t mce_device_initialized = CPU_MASK_NONE;
811
812 /* Per cpu sysdev init.  All of the cpus still share the same ctl bank */
813 static __cpuinit int mce_create_device(unsigned int cpu)
814 {
815         int err;
816         int i;
817
818         if (!mce_available(&boot_cpu_data))
819                 return -EIO;
820
821         memset(&per_cpu(device_mce, cpu).kobj, 0, sizeof(struct kobject));
822         per_cpu(device_mce,cpu).id = cpu;
823         per_cpu(device_mce,cpu).cls = &mce_sysclass;
824
825         err = sysdev_register(&per_cpu(device_mce,cpu));
826         if (err)
827                 return err;
828
829         for (i = 0; mce_attributes[i]; i++) {
830                 err = sysdev_create_file(&per_cpu(device_mce,cpu),
831                                          mce_attributes[i]);
832                 if (err)
833                         goto error;
834         }
835         cpu_set(cpu, mce_device_initialized);
836
837         return 0;
838 error:
839         while (i--) {
840                 sysdev_remove_file(&per_cpu(device_mce,cpu),
841                                    mce_attributes[i]);
842         }
843         sysdev_unregister(&per_cpu(device_mce,cpu));
844
845         return err;
846 }
847
848 static void mce_remove_device(unsigned int cpu)
849 {
850         int i;
851
852         if (!cpu_isset(cpu, mce_device_initialized))
853                 return;
854
855         for (i = 0; mce_attributes[i]; i++)
856                 sysdev_remove_file(&per_cpu(device_mce,cpu),
857                         mce_attributes[i]);
858         sysdev_unregister(&per_cpu(device_mce,cpu));
859         cpu_clear(cpu, mce_device_initialized);
860 }
861
862 /* Get notified when a cpu comes on/off. Be hotplug friendly. */
863 static int __cpuinit mce_cpu_callback(struct notifier_block *nfb,
864                                       unsigned long action, void *hcpu)
865 {
866         unsigned int cpu = (unsigned long)hcpu;
867
868         switch (action) {
869         case CPU_ONLINE:
870         case CPU_ONLINE_FROZEN:
871                 mce_create_device(cpu);
872                 break;
873         case CPU_DEAD:
874         case CPU_DEAD_FROZEN:
875                 mce_remove_device(cpu);
876                 break;
877         }
878         return NOTIFY_OK;
879 }
880
881 static struct notifier_block mce_cpu_notifier __cpuinitdata = {
882         .notifier_call = mce_cpu_callback,
883 };
884
885 static __init int mce_init_device(void)
886 {
887         int err;
888         int i = 0;
889
890         if (!mce_available(&boot_cpu_data))
891                 return -EIO;
892         err = sysdev_class_register(&mce_sysclass);
893         if (err)
894                 return err;
895
896         for_each_online_cpu(i) {
897                 err = mce_create_device(i);
898                 if (err)
899                         return err;
900         }
901
902         register_hotcpu_notifier(&mce_cpu_notifier);
903         misc_register(&mce_log_device);
904         return err;
905 }
906
907 device_initcall(mce_init_device);