]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blobdiff - arch/x86/kernel/cpu/mcheck/mce_amd_64.c
x86, mce: trivial clean up for mce_amd_64.c
[mirror_ubuntu-jammy-kernel.git] / arch / x86 / kernel / cpu / mcheck / mce_amd_64.c
index 9817506dd4698c6578646f04100836dd6ad435c8..ddae21620bda67a8c0fecf724bf2456821659c5f 100644 (file)
  *
  *  All MC4_MISCi registers are shared between multi-cores
  */
-
-#include <linux/cpu.h>
-#include <linux/errno.h>
-#include <linux/init.h>
 #include <linux/interrupt.h>
-#include <linux/kobject.h>
 #include <linux/notifier.h>
-#include <linux/sched.h>
-#include <linux/smp.h>
+#include <linux/kobject.h>
+#include <linux/percpu.h>
 #include <linux/sysdev.h>
+#include <linux/errno.h>
+#include <linux/sched.h>
 #include <linux/sysfs.h>
+#include <linux/init.h>
+#include <linux/cpu.h>
+#include <linux/smp.h>
+
 #include <asm/apic.h>
+#include <asm/idle.h>
 #include <asm/mce.h>
 #include <asm/msr.h>
-#include <asm/percpu.h>
-#include <asm/idle.h>
 
 #define PFX               "mce_threshold: "
 #define VERSION           "version 1.1.1"
 #define MCG_XBLK_ADDR     0xC0000400
 
 struct threshold_block {
-       unsigned int block;
-       unsigned int bank;
-       unsigned int cpu;
-       u32 address;
-       u16 interrupt_enable;
-       u16 threshold_limit;
-       struct kobject kobj;
-       struct list_head miscj;
+       unsigned int            block;
+       unsigned int            bank;
+       unsigned int            cpu;
+       u32                     address;
+       u16                     interrupt_enable;
+       u16                     threshold_limit;
+       struct kobject          kobj;
+       struct list_head        miscj;
 };
 
 /* defaults used early on boot */
 static struct threshold_block threshold_defaults = {
-       .interrupt_enable = 0,
-       .threshold_limit = THRESHOLD_MAX,
+       .interrupt_enable       = 0,
+       .threshold_limit        = THRESHOLD_MAX,
 };
 
 struct threshold_bank {
-       struct kobject *kobj;
-       struct threshold_block *blocks;
-       cpumask_var_t cpus;
+       struct kobject          *kobj;
+       struct threshold_block  *blocks;
+       cpumask_var_t           cpus;
 };
 static DEFINE_PER_CPU(struct threshold_bank *, threshold_banks[NR_BANKS]);
 
@@ -79,18 +79,21 @@ static unsigned char shared_bank[NR_BANKS] = {
 
 static DEFINE_PER_CPU(unsigned char, bank_map);        /* see which banks are on */
 
+static void amd_threshold_interrupt(void);
+
 /*
  * CPU Initialization
  */
 
 struct thresh_restart {
-       struct threshold_block *b;
-       int reset;
-       u16 old_limit;
+       struct threshold_block  *b;
+       int                     reset;
+       u16                     old_limit;
 };
 
 /* must be called with correct cpu affinity */
-static long threshold_restart_bank(void *_tr)
+/* Called via smp_call_function_single() */
+static void threshold_restart_bank(void *_tr)
 {
        struct thresh_restart *tr = _tr;
        u32 mci_misc_hi, mci_misc_lo;
@@ -107,6 +110,7 @@ static long threshold_restart_bank(void *_tr)
        } else if (tr->old_limit) {     /* change limit w/o reset */
                int new_count = (mci_misc_hi & THRESHOLD_MAX) +
                    (tr->old_limit - tr->b->threshold_limit);
+
                mci_misc_hi = (mci_misc_hi & ~MASK_ERR_COUNT_HI) |
                    (new_count & THRESHOLD_MAX);
        }
@@ -117,17 +121,16 @@ static long threshold_restart_bank(void *_tr)
 
        mci_misc_hi |= MASK_COUNT_EN_HI;
        wrmsr(tr->b->address, mci_misc_lo, mci_misc_hi);
-       return 0;
 }
 
 /* cpu init entry point, called from mce.c with preempt off */
 void mce_amd_feature_init(struct cpuinfo_x86 *c)
 {
-       unsigned int bank, block;
        unsigned int cpu = smp_processor_id();
-       u8 lvt_off;
        u32 low = 0, high = 0, address = 0;
+       unsigned int bank, block;
        struct thresh_restart tr;
+       u8 lvt_off;
 
        for (bank = 0; bank < NR_BANKS; ++bank) {
                for (block = 0; block < NR_BLOCKS; ++block) {
@@ -138,8 +141,7 @@ void mce_amd_feature_init(struct cpuinfo_x86 *c)
                                if (!address)
                                        break;
                                address += MCG_XBLK_ADDR;
-                       }
-                       else
+                       } else
                                ++address;
 
                        if (rdmsr_safe(address, &low, &high))
@@ -174,6 +176,8 @@ void mce_amd_feature_init(struct cpuinfo_x86 *c)
                        tr.reset = 0;
                        tr.old_limit = 0;
                        threshold_restart_bank(&tr);
+
+                       mce_threshold_vector = amd_threshold_interrupt;
                }
        }
 }
@@ -187,35 +191,29 @@ void mce_amd_feature_init(struct cpuinfo_x86 *c)
  * the interrupt goes off when error_count reaches threshold_limit.
  * the handler will simply log mcelog w/ software defined bank number.
  */
-asmlinkage void mce_threshold_interrupt(void)
+static void amd_threshold_interrupt(void)
 {
+       u32 low = 0, high = 0, address = 0;
        unsigned int bank, block;
        struct mce m;
-       u32 low = 0, high = 0, address = 0;
 
-       ack_APIC_irq();
-       exit_idle();
-       irq_enter();
-
-       memset(&m, 0, sizeof(m));
-       rdtscll(m.tsc);
-       m.cpu = smp_processor_id();
+       mce_setup(&m);
 
        /* assume first bank caused it */
        for (bank = 0; bank < NR_BANKS; ++bank) {
                if (!(per_cpu(bank_map, m.cpu) & (1 << bank)))
                        continue;
                for (block = 0; block < NR_BLOCKS; ++block) {
-                       if (block == 0)
+                       if (block == 0) {
                                address = MSR_IA32_MC0_MISC + bank * 4;
-                       else if (block == 1) {
+                       else if (block == 1) {
                                address = (low & MASK_BLKPTR_LO) >> 21;
                                if (!address)
                                        break;
                                address += MCG_XBLK_ADDR;
-                       }
-                       else
+                       } else {
                                ++address;
+                       }
 
                        if (rdmsr_safe(address, &low, &high))
                                break;
@@ -231,9 +229,12 @@ asmlinkage void mce_threshold_interrupt(void)
                             (high & MASK_LOCKED_HI))
                                continue;
 
-                       /* Log the machine check that caused the threshold
-                          event. */
-                       do_machine_check(NULL, 0);
+                       /*
+                        * Log the machine check that caused the threshold
+                        * event.
+                        */
+                       machine_check_poll(MCP_TIMESTAMP,
+                                       &__get_cpu_var(mce_poll_banks));
 
                        if (high & MASK_OVERFLOW_HI) {
                                rdmsrl(address, m.misc);
@@ -243,13 +244,10 @@ asmlinkage void mce_threshold_interrupt(void)
                                       + bank * NR_BLOCKS
                                       + block;
                                mce_log(&m);
-                               goto out;
+                               return;
                        }
                }
        }
-out:
-       inc_irq_stat(irq_threshold_count);
-       irq_exit();
 }
 
 /*
@@ -258,70 +256,83 @@ out:
 
 struct threshold_attr {
        struct attribute attr;
-       ssize_t(*show) (struct threshold_block *, char *);
-       ssize_t(*store) (struct threshold_block *, const char *, size_t count);
+       ssize_t (*show) (struct threshold_block *, char *);
+       ssize_t (*store) (struct threshold_block *, const char *, size_t count);
 };
 
-#define SHOW_FIELDS(name)                                           \
-static ssize_t show_ ## name(struct threshold_block * b, char *buf) \
-{                                                                   \
-        return sprintf(buf, "%lx\n", (unsigned long) b->name);      \
+#define SHOW_FIELDS(name)                                              \
+static ssize_t show_ ## name(struct threshold_block *b, char *buf)     \
+{                                                                      \
+       return sprintf(buf, "%lx\n", (unsigned long) b->name);          \
 }
 SHOW_FIELDS(interrupt_enable)
 SHOW_FIELDS(threshold_limit)
 
-static ssize_t store_interrupt_enable(struct threshold_block *b,
-                                     const char *buf, size_t count)
+static ssize_t
+store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size)
 {
-       char *end;
        struct thresh_restart tr;
-       unsigned long new = simple_strtoul(buf, &end, 0);
-       if (end == buf)
+       unsigned long new;
+
+       if (strict_strtoul(buf, 0, &new) < 0)
                return -EINVAL;
+
        b->interrupt_enable = !!new;
 
-       tr.b = b;
-       tr.reset = 0;
-       tr.old_limit = 0;
-       work_on_cpu(b->cpu, threshold_restart_bank, &tr);
+       tr.b            = b;
+       tr.reset        = 0;
+       tr.old_limit    = 0;
+
+       smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
 
-       return end - buf;
+       return size;
 }
 
-static ssize_t store_threshold_limit(struct threshold_block *b,
-                                    const char *buf, size_t count)
+static ssize_t
+store_threshold_limit(struct threshold_block *b, const char *buf, size_t size)
 {
-       char *end;
        struct thresh_restart tr;
-       unsigned long new = simple_strtoul(buf, &end, 0);
-       if (end == buf)
+       unsigned long new;
+
+       if (strict_strtoul(buf, 0, &new) < 0)
                return -EINVAL;
+
        if (new > THRESHOLD_MAX)
                new = THRESHOLD_MAX;
        if (new < 1)
                new = 1;
+
        tr.old_limit = b->threshold_limit;
        b->threshold_limit = new;
        tr.b = b;
        tr.reset = 0;
 
-       work_on_cpu(b->cpu, threshold_restart_bank, &tr);
+       smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
 
-       return end - buf;
+       return size;
 }
 
-static long local_error_count(void *_b)
+struct threshold_block_cross_cpu {
+       struct threshold_block  *tb;
+       long                    retval;
+};
+
+static void local_error_count_handler(void *_tbcc)
 {
-       struct threshold_block *b = _b;
+       struct threshold_block_cross_cpu *tbcc = _tbcc;
+       struct threshold_block *b = tbcc->tb;
        u32 low, high;
 
        rdmsr(b->address, low, high);
-       return (high & 0xFFF) - (THRESHOLD_MAX - b->threshold_limit);
+       tbcc->retval = (high & 0xFFF) - (THRESHOLD_MAX - b->threshold_limit);
 }
 
 static ssize_t show_error_count(struct threshold_block *b, char *buf)
 {
-       return sprintf(buf, "%lx\n", work_on_cpu(b->cpu, local_error_count, b));
+       struct threshold_block_cross_cpu tbcc = { .tb = b, };
+
+       smp_call_function_single(b->cpu, local_error_count_handler, &tbcc, 1);
+       return sprintf(buf, "%lx\n", tbcc.retval);
 }
 
 static ssize_t store_error_count(struct threshold_block *b,
@@ -329,20 +340,17 @@ static ssize_t store_error_count(struct threshold_block *b,
 {
        struct thresh_restart tr = { .b = b, .reset = 1, .old_limit = 0 };
 
-       work_on_cpu(b->cpu, threshold_restart_bank, &tr);
+       smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
        return 1;
 }
 
-#define THRESHOLD_ATTR(_name,_mode,_show,_store) {            \
-        .attr = {.name = __stringify(_name), .mode = _mode }, \
-        .show = _show,                                        \
-        .store = _store,                                      \
+#define RW_ATTR(val)                                                   \
+static struct threshold_attr val = {                                   \
+       .attr   = {.name = __stringify(val), .mode = 0644 },            \
+       .show   = show_## val,                                          \
+       .store  = store_## val,                                         \
 };
 
-#define RW_ATTR(name)                                           \
-static struct threshold_attr name =                             \
-        THRESHOLD_ATTR(name, 0644, show_## name, store_## name)
-
 RW_ATTR(interrupt_enable);
 RW_ATTR(threshold_limit);
 RW_ATTR(error_count);
@@ -354,15 +362,17 @@ static struct attribute *default_attrs[] = {
        NULL
 };
 
-#define to_block(k) container_of(k, struct threshold_block, kobj)
-#define to_attr(a) container_of(a, struct threshold_attr, attr)
+#define to_block(k)    container_of(k, struct threshold_block, kobj)
+#define to_attr(a)     container_of(a, struct threshold_attr, attr)
 
 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
 {
        struct threshold_block *b = to_block(kobj);
        struct threshold_attr *a = to_attr(attr);
        ssize_t ret;
+
        ret = a->show ? a->show(b, buf) : -EIO;
+
        return ret;
 }
 
@@ -372,18 +382,20 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr,
        struct threshold_block *b = to_block(kobj);
        struct threshold_attr *a = to_attr(attr);
        ssize_t ret;
+
        ret = a->store ? a->store(b, buf, count) : -EIO;
+
        return ret;
 }
 
 static struct sysfs_ops threshold_ops = {
-       .show = show,
-       .store = store,
+       .show                   = show,
+       .store                  = store,
 };
 
 static struct kobj_type threshold_ktype = {
-       .sysfs_ops = &threshold_ops,
-       .default_attrs = default_attrs,
+       .sysfs_ops              = &threshold_ops,
+       .default_attrs          = default_attrs,
 };
 
 static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
@@ -391,14 +403,14 @@ static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
                                               unsigned int block,
                                               u32 address)
 {
-       int err;
-       u32 low, high;
        struct threshold_block *b = NULL;
+       u32 low, high;
+       int err;
 
        if ((bank >= NR_BANKS) || (block >= NR_BLOCKS))
                return 0;
 
-       if (rdmsr_safe(address, &low, &high))
+       if (rdmsr_safe_on_cpu(cpu, address, &low, &high))
                return 0;
 
        if (!(high & MASK_VALID_HI)) {
@@ -416,20 +428,21 @@ static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
        if (!b)
                return -ENOMEM;
 
-       b->block = block;
-       b->bank = bank;
-       b->cpu = cpu;
-       b->address = address;
-       b->interrupt_enable = 0;
-       b->threshold_limit = THRESHOLD_MAX;
+       b->block                = block;
+       b->bank                 = bank;
+       b->cpu                  = cpu;
+       b->address              = address;
+       b->interrupt_enable     = 0;
+       b->threshold_limit      = THRESHOLD_MAX;
 
        INIT_LIST_HEAD(&b->miscj);
 
-       if (per_cpu(threshold_banks, cpu)[bank]->blocks)
+       if (per_cpu(threshold_banks, cpu)[bank]->blocks) {
                list_add(&b->miscj,
                         &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
-       else
+       } else {
                per_cpu(threshold_banks, cpu)[bank]->blocks = b;
+       }
 
        err = kobject_init_and_add(&b->kobj, &threshold_ktype,
                                   per_cpu(threshold_banks, cpu)[bank]->kobj,
@@ -442,8 +455,9 @@ recurse:
                if (!address)
                        return 0;
                address += MCG_XBLK_ADDR;
-       } else
+       } else {
                ++address;
+       }
 
        err = allocate_threshold_blocks(cpu, bank, ++block, address);
        if (err)
@@ -462,12 +476,11 @@ out_free:
        return err;
 }
 
-static __cpuinit long local_allocate_threshold_blocks(void *_bank)
+static __cpuinit long
+local_allocate_threshold_blocks(int cpu, unsigned int bank)
 {
-       unsigned int *bank = _bank;
-
-       return allocate_threshold_blocks(smp_processor_id(), *bank, 0,
-                                        MSR_IA32_MC0_MISC + *bank * 4);
+       return allocate_threshold_blocks(cpu, bank, 0,
+                                        MSR_IA32_MC0_MISC + bank * 4);
 }
 
 /* symlinks sibling shared banks to first core.  first core owns dir/files. */
@@ -481,7 +494,7 @@ static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
 
 #ifdef CONFIG_SMP
        if (cpu_data(cpu).cpu_core_id && shared_bank[bank]) {   /* symlink */
-               i = cpumask_first(&per_cpu(cpu_core_map, cpu));
+               i = cpumask_first(cpu_core_mask(cpu));
 
                /* first core not up yet */
                if (cpu_data(i).cpu_core_id)
@@ -496,13 +509,14 @@ static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
                if (!b)
                        goto out;
 
-               err = sysfs_create_link(&per_cpu(device_mce, cpu).kobj,
+               err = sysfs_create_link(&per_cpu(mce_dev, cpu).kobj,
                                        b->kobj, name);
                if (err)
                        goto out;
 
-               cpumask_copy(b->cpus, &per_cpu(cpu_core_map, cpu));
+               cpumask_copy(b->cpus, cpu_core_mask(cpu));
                per_cpu(threshold_banks, cpu)[bank] = b;
+
                goto out;
        }
 #endif
@@ -518,19 +532,19 @@ static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
                goto out;
        }
 
-       b->kobj = kobject_create_and_add(name, &per_cpu(device_mce, cpu).kobj);
+       b->kobj = kobject_create_and_add(name, &per_cpu(mce_dev, cpu).kobj);
        if (!b->kobj)
                goto out_free;
 
 #ifndef CONFIG_SMP
        cpumask_setall(b->cpus);
 #else
-       cpumask_copy(b->cpus, &per_cpu(cpu_core_map, cpu));
+       cpumask_copy(b->cpus, cpu_core_mask(cpu));
 #endif
 
        per_cpu(threshold_banks, cpu)[bank] = b;
 
-       err = work_on_cpu(cpu, local_allocate_threshold_blocks, &bank);
+       err = local_allocate_threshold_blocks(cpu, bank);
        if (err)
                goto out_free;
 
@@ -538,7 +552,7 @@ static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
                if (i == cpu)
                        continue;
 
-               err = sysfs_create_link(&per_cpu(device_mce, i).kobj,
+               err = sysfs_create_link(&per_cpu(mce_dev, i).kobj,
                                        b->kobj, name);
                if (err)
                        goto out;
@@ -601,15 +615,13 @@ static void deallocate_threshold_block(unsigned int cpu,
 
 static void threshold_remove_bank(unsigned int cpu, int bank)
 {
-       int i = 0;
        struct threshold_bank *b;
        char name[32];
+       int i = 0;
 
        b = per_cpu(threshold_banks, cpu)[bank];
-
        if (!b)
                return;
-
        if (!b->blocks)
                goto free_out;
 
@@ -618,8 +630,9 @@ static void threshold_remove_bank(unsigned int cpu, int bank)
 #ifdef CONFIG_SMP
        /* sibling symlink */
        if (shared_bank[bank] && b->blocks->cpu != cpu) {
-               sysfs_remove_link(&per_cpu(device_mce, cpu).kobj, name);
+               sysfs_remove_link(&per_cpu(mce_dev, cpu).kobj, name);
                per_cpu(threshold_banks, cpu)[bank] = NULL;
+
                return;
        }
 #endif
@@ -629,7 +642,7 @@ static void threshold_remove_bank(unsigned int cpu, int bank)
                if (i == cpu)
                        continue;
 
-               sysfs_remove_link(&per_cpu(device_mce, i).kobj, name);
+               sysfs_remove_link(&per_cpu(mce_dev, i).kobj, name);
                per_cpu(threshold_banks, i)[bank] = NULL;
        }
 
@@ -655,12 +668,9 @@ static void threshold_remove_device(unsigned int cpu)
 }
 
 /* get notified when a cpu comes on/off */
-static void __cpuinit amd_64_threshold_cpu_callback(unsigned long action,
-                                                    unsigned int cpu)
+static void __cpuinit
+amd_64_threshold_cpu_callback(unsigned long action, unsigned int cpu)
 {
-       if (cpu >= NR_CPUS)
-               return;
-
        switch (action) {
        case CPU_ONLINE:
        case CPU_ONLINE_FROZEN:
@@ -682,11 +692,12 @@ static __init int threshold_init_device(void)
        /* to hit CPUs online before the notifier is up */
        for_each_online_cpu(lcpu) {
                int err = threshold_create_device(lcpu);
+
                if (err)
                        return err;
        }
        threshold_cpu_callback = amd_64_threshold_cpu_callback;
+
        return 0;
 }
-
 device_initcall(threshold_init_device);