/home/lenb/src/to-linus-stable branch 'acpi-2.6.12'
[powerpc.git] / arch / x86_64 / kernel / mce.c
index 6ca0664..3b267c9 100644 (file)
@@ -15,6 +15,8 @@
 #include <linux/sysdev.h>
 #include <linux/miscdevice.h>
 #include <linux/fs.h>
+#include <linux/cpu.h>
+#include <linux/percpu.h>
 #include <asm/processor.h> 
 #include <asm/msr.h>
 #include <asm/mce.h>
@@ -327,7 +329,7 @@ static void mce_init(void *dummy)
 }
 
 /* Add per CPU specific workarounds here */
-static void __init mce_cpu_quirks(struct cpuinfo_x86 *c) 
+static void __cpuinit mce_cpu_quirks(struct cpuinfo_x86 *c)
 { 
        /* This should be disabled by the BIOS, but isn't always */
        if (c->x86_vendor == X86_VENDOR_AMD && c->x86 == 15) {
@@ -337,7 +339,7 @@ static void __init mce_cpu_quirks(struct cpuinfo_x86 *c)
        }
 }                      
 
-static void __init mce_cpu_features(struct cpuinfo_x86 *c)
+static void __cpuinit mce_cpu_features(struct cpuinfo_x86 *c)
 {
        switch (c->x86_vendor) {
        case X86_VENDOR_INTEL:
@@ -352,7 +354,7 @@ static void __init mce_cpu_features(struct cpuinfo_x86 *c)
  * Called for each booted CPU to set up machine checks.
  * Must be called with preempt off. 
  */
-void __init mcheck_init(struct cpuinfo_x86 *c)
+void __cpuinit mcheck_init(struct cpuinfo_x86 *c)
 {
        static cpumask_t mce_cpus __initdata = CPU_MASK_NONE;
 
@@ -379,18 +381,23 @@ static void collect_tscs(void *data)
 
 static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize, loff_t *off)
 {
-       unsigned long cpu_tsc[NR_CPUS];
+       unsigned long *cpu_tsc;
        static DECLARE_MUTEX(mce_read_sem);
        unsigned next;
        char __user *buf = ubuf;
        int i, err;
 
+       cpu_tsc = kmalloc(NR_CPUS * sizeof(long), GFP_KERNEL);
+       if (!cpu_tsc)
+               return -ENOMEM;
+
        down(&mce_read_sem); 
        next = rcu_dereference(mcelog.next);
 
        /* Only supports full reads right now */
        if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce)) { 
                up(&mce_read_sem);
+               kfree(cpu_tsc);
                return -EINVAL;
        }
 
@@ -406,7 +413,7 @@ static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize, loff
        memset(mcelog.entry, 0, next * sizeof(struct mce));
        mcelog.next = 0;
 
-       synchronize_kernel();   
+       synchronize_sched();
 
        /* Collect entries that were still getting written before the synchronize. */
 
@@ -421,6 +428,7 @@ static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize, loff
                }
        }       
        up(&mce_read_sem);
+       kfree(cpu_tsc);
        return err ? -EFAULT : buf - ubuf; 
 }
 
@@ -508,10 +516,7 @@ static struct sysdev_class mce_sysclass = {
        set_kset_name("machinecheck"),
 };
 
-static struct sys_device device_mce = {
-       .id     = 0,
-       .cls    = &mce_sysclass,
-};
+static DEFINE_PER_CPU(struct sys_device, device_mce);
 
 /* Why are there no generic functions for this? */
 #define ACCESSOR(name, var, start) \
@@ -536,27 +541,83 @@ ACCESSOR(bank4ctl,bank[4],mce_restart())
 ACCESSOR(tolerant,tolerant,)
 ACCESSOR(check_interval,check_interval,mce_restart())
 
+/* Per cpu sysdev init.  All of the cpus still share the same ctl bank */
+static __cpuinit int mce_create_device(unsigned int cpu)
+{
+       int err;
+       if (!mce_available(&cpu_data[cpu]))
+               return -EIO;
+
+       per_cpu(device_mce,cpu).id = cpu;
+       per_cpu(device_mce,cpu).cls = &mce_sysclass;
+
+       err = sysdev_register(&per_cpu(device_mce,cpu));
+
+       if (!err) {
+               sysdev_create_file(&per_cpu(device_mce,cpu), &attr_bank0ctl);
+               sysdev_create_file(&per_cpu(device_mce,cpu), &attr_bank1ctl);
+               sysdev_create_file(&per_cpu(device_mce,cpu), &attr_bank2ctl);
+               sysdev_create_file(&per_cpu(device_mce,cpu), &attr_bank3ctl);
+               sysdev_create_file(&per_cpu(device_mce,cpu), &attr_bank4ctl);
+               sysdev_create_file(&per_cpu(device_mce,cpu), &attr_tolerant);
+               sysdev_create_file(&per_cpu(device_mce,cpu), &attr_check_interval);
+       }
+       return err;
+}
+
+#ifdef CONFIG_HOTPLUG_CPU
+static __cpuinit void mce_remove_device(unsigned int cpu)
+{
+       sysdev_remove_file(&per_cpu(device_mce,cpu), &attr_bank0ctl);
+       sysdev_remove_file(&per_cpu(device_mce,cpu), &attr_bank1ctl);
+       sysdev_remove_file(&per_cpu(device_mce,cpu), &attr_bank2ctl);
+       sysdev_remove_file(&per_cpu(device_mce,cpu), &attr_bank3ctl);
+       sysdev_remove_file(&per_cpu(device_mce,cpu), &attr_bank4ctl);
+       sysdev_remove_file(&per_cpu(device_mce,cpu), &attr_tolerant);
+       sysdev_remove_file(&per_cpu(device_mce,cpu), &attr_check_interval);
+       sysdev_unregister(&per_cpu(device_mce,cpu));
+}
+#endif
+
+/* Get notified when a cpu comes on/off. Be hotplug friendly. */
+static __cpuinit int
+mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
+{
+       unsigned int cpu = (unsigned long)hcpu;
+
+       switch (action) {
+       case CPU_ONLINE:
+               mce_create_device(cpu);
+               break;
+#ifdef CONFIG_HOTPLUG_CPU
+       case CPU_DEAD:
+               mce_remove_device(cpu);
+               break;
+#endif
+       }
+       return NOTIFY_OK;
+}
+
+static struct notifier_block mce_cpu_notifier = {
+       .notifier_call = mce_cpu_callback,
+};
+
 static __init int mce_init_device(void)
 {
        int err;
+       int i = 0;
+
        if (!mce_available(&boot_cpu_data))
                return -EIO;
        err = sysdev_class_register(&mce_sysclass);
-       if (!err)
-               err = sysdev_register(&device_mce);
-       if (!err) { 
-               /* could create per CPU objects, but it is not worth it. */
-               sysdev_create_file(&device_mce, &attr_bank0ctl); 
-               sysdev_create_file(&device_mce, &attr_bank1ctl); 
-               sysdev_create_file(&device_mce, &attr_bank2ctl); 
-               sysdev_create_file(&device_mce, &attr_bank3ctl); 
-               sysdev_create_file(&device_mce, &attr_bank4ctl); 
-               sysdev_create_file(&device_mce, &attr_tolerant); 
-               sysdev_create_file(&device_mce, &attr_check_interval);
-       } 
-       
+
+       for_each_online_cpu(i) {
+               mce_create_device(i);
+       }
+
+       register_cpu_notifier(&mce_cpu_notifier);
        misc_register(&mce_log_device);
        return err;
-
 }
+
 device_initcall(mce_init_device);