2 * (c) 2005-2015 Advanced Micro Devices, Inc.
3 * Your use of this code is subject to the terms and conditions of the
4 * GNU general public license version 2. See "COPYING" or
5 * http://www.gnu.org/licenses/gpl.html
7 * Written by Jacob Shin - AMD, Inc.
8 * Maintained by: Borislav Petkov <bp@alien8.de>
10 * All MC4_MISCi registers are shared between cores on a node.
12 #include <linux/interrupt.h>
13 #include <linux/notifier.h>
14 #include <linux/kobject.h>
15 #include <linux/percpu.h>
16 #include <linux/errno.h>
17 #include <linux/sched.h>
18 #include <linux/sysfs.h>
19 #include <linux/slab.h>
20 #include <linux/init.h>
21 #include <linux/cpu.h>
22 #include <linux/smp.h>
24 #include <asm/amd_nb.h>
29 #include <asm/trace/irq_vectors.h>
32 #define THRESHOLD_MAX 0xFFF
33 #define INT_TYPE_APIC 0x00020000
34 #define MASK_VALID_HI 0x80000000
35 #define MASK_CNTP_HI 0x40000000
36 #define MASK_LOCKED_HI 0x20000000
37 #define MASK_LVTOFF_HI 0x00F00000
38 #define MASK_COUNT_EN_HI 0x00080000
39 #define MASK_INT_TYPE_HI 0x00060000
40 #define MASK_OVERFLOW_HI 0x00010000
41 #define MASK_ERR_COUNT_HI 0x00000FFF
42 #define MASK_BLKPTR_LO 0xFF000000
43 #define MCG_XBLK_ADDR 0xC0000400
45 /* Deferred error settings */
46 #define MSR_CU_DEF_ERR 0xC0000410
47 #define MASK_DEF_LVTOFF 0x000000F0
48 #define MASK_DEF_INT_TYPE 0x00000006
49 #define DEF_LVT_OFF 0x2
50 #define DEF_INT_TYPE_APIC 0x2
54 /* Threshold LVT offset is at MSR0xC0000410[15:12] */
55 #define SMCA_THR_LVT_OFF 0xF000
58 * OS is required to set the MCAX bit to acknowledge that it is now using the
59 * new MSR ranges and new registers under each bank. It also means that the OS
60 * will configure deferred errors in the new MCx_CONFIG register. If the bit is
61 * not set, uncorrectable errors will cause a system panic.
63 #define SMCA_MCAX_EN_OFF 0x1
65 static const char * const th_names[] = {
74 static DEFINE_PER_CPU(struct threshold_bank **, threshold_banks);
75 static DEFINE_PER_CPU(unsigned char, bank_map); /* see which banks are on */
77 static void amd_threshold_interrupt(void);
78 static void amd_deferred_error_interrupt(void);
80 static void default_deferred_error_interrupt(void)
82 pr_err("Unexpected deferred interrupt at vector %x\n", DEFERRED_ERROR_VECTOR);
84 void (*deferred_error_int_vector)(void) = default_deferred_error_interrupt;
90 struct thresh_restart {
91 struct threshold_block *b;
98 static inline bool is_shared_bank(int bank)
101 * Scalable MCA provides for only one core to have access to the MSRs of
107 /* Bank 4 is for northbridge reporting and is thus shared */
111 static const char *bank4_names(const struct threshold_block *b)
113 switch (b->address) {
125 WARN(1, "Funny MSR: 0x%08x\n", b->address);
131 static bool lvt_interrupt_supported(unsigned int bank, u32 msr_high_bits)
134 * bank 4 supports APIC LVT interrupts implicitly since forever.
140 * IntP: interrupt present; if this bit is set, the thresholding
141 * bank can generate APIC LVT interrupts
143 return msr_high_bits & BIT(28);
146 static int lvt_off_valid(struct threshold_block *b, int apic, u32 lo, u32 hi)
148 int msr = (hi & MASK_LVTOFF_HI) >> 20;
151 pr_err(FW_BUG "cpu %d, failed to setup threshold interrupt "
152 "for bank %d, block %d (MSR%08X=0x%x%08x)\n", b->cpu,
153 b->bank, b->block, b->address, hi, lo);
159 * On SMCA CPUs, LVT offset is programmed at a different MSR, and
160 * the BIOS provides the value. The original field where LVT offset
161 * was set is reserved. Return early here:
166 pr_err(FW_BUG "cpu %d, invalid threshold interrupt offset %d "
167 "for bank %d, block %d (MSR%08X=0x%x%08x)\n",
168 b->cpu, apic, b->bank, b->block, b->address, hi, lo);
176 * Called via smp_call_function_single(), must be called with correct
179 static void threshold_restart_bank(void *_tr)
181 struct thresh_restart *tr = _tr;
184 rdmsr(tr->b->address, lo, hi);
186 if (tr->b->threshold_limit < (hi & THRESHOLD_MAX))
187 tr->reset = 1; /* limit cannot be lower than err count */
189 if (tr->reset) { /* reset err count and overflow bit */
191 (hi & ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI)) |
192 (THRESHOLD_MAX - tr->b->threshold_limit);
193 } else if (tr->old_limit) { /* change limit w/o reset */
194 int new_count = (hi & THRESHOLD_MAX) +
195 (tr->old_limit - tr->b->threshold_limit);
197 hi = (hi & ~MASK_ERR_COUNT_HI) |
198 (new_count & THRESHOLD_MAX);
202 hi &= ~MASK_INT_TYPE_HI;
204 if (!tr->b->interrupt_capable)
207 if (tr->set_lvt_off) {
208 if (lvt_off_valid(tr->b, tr->lvt_off, lo, hi)) {
209 /* set new lvt offset */
210 hi &= ~MASK_LVTOFF_HI;
211 hi |= tr->lvt_off << 20;
215 if (tr->b->interrupt_enable)
220 hi |= MASK_COUNT_EN_HI;
221 wrmsr(tr->b->address, lo, hi);
224 static void mce_threshold_block_init(struct threshold_block *b, int offset)
226 struct thresh_restart tr = {
232 b->threshold_limit = THRESHOLD_MAX;
233 threshold_restart_bank(&tr);
236 static int setup_APIC_mce_threshold(int reserved, int new)
238 if (reserved < 0 && !setup_APIC_eilvt(new, THRESHOLD_APIC_VECTOR,
239 APIC_EILVT_MSG_FIX, 0))
245 static int setup_APIC_deferred_error(int reserved, int new)
247 if (reserved < 0 && !setup_APIC_eilvt(new, DEFERRED_ERROR_VECTOR,
248 APIC_EILVT_MSG_FIX, 0))
254 static void deferred_error_interrupt_enable(struct cpuinfo_x86 *c)
256 u32 low = 0, high = 0;
257 int def_offset = -1, def_new;
259 if (rdmsr_safe(MSR_CU_DEF_ERR, &low, &high))
262 def_new = (low & MASK_DEF_LVTOFF) >> 4;
263 if (!(low & MASK_DEF_LVTOFF)) {
264 pr_err(FW_BUG "Your BIOS is not setting up LVT offset 0x2 for deferred error IRQs correctly.\n");
265 def_new = DEF_LVT_OFF;
266 low = (low & ~MASK_DEF_LVTOFF) | (DEF_LVT_OFF << 4);
269 def_offset = setup_APIC_deferred_error(def_offset, def_new);
270 if ((def_offset == def_new) &&
271 (deferred_error_int_vector != amd_deferred_error_interrupt))
272 deferred_error_int_vector = amd_deferred_error_interrupt;
274 low = (low & ~MASK_DEF_INT_TYPE) | DEF_INT_TYPE_APIC;
275 wrmsr(MSR_CU_DEF_ERR, low, high);
279 prepare_threshold_block(unsigned int bank, unsigned int block, u32 addr,
280 int offset, u32 misc_high)
282 unsigned int cpu = smp_processor_id();
283 struct threshold_block b;
287 per_cpu(bank_map, cpu) |= (1 << bank);
289 memset(&b, 0, sizeof(b));
294 b.interrupt_capable = lvt_interrupt_supported(bank, misc_high);
296 if (!b.interrupt_capable)
299 b.interrupt_enable = 1;
301 if (mce_flags.smca) {
302 u32 smca_low, smca_high;
303 u32 smca_addr = MSR_AMD64_SMCA_MCx_CONFIG(bank);
305 if (!rdmsr_safe(smca_addr, &smca_low, &smca_high)) {
306 smca_high |= SMCA_MCAX_EN_OFF;
307 wrmsr(smca_addr, smca_low, smca_high);
310 /* Gather LVT offset for thresholding: */
311 if (rdmsr_safe(MSR_CU_DEF_ERR, &smca_low, &smca_high))
314 new = (smca_low & SMCA_THR_LVT_OFF) >> 12;
316 new = (misc_high & MASK_LVTOFF_HI) >> 20;
319 offset = setup_APIC_mce_threshold(offset, new);
321 if ((offset == new) && (mce_threshold_vector != amd_threshold_interrupt))
322 mce_threshold_vector = amd_threshold_interrupt;
325 mce_threshold_block_init(&b, offset);
331 /* cpu init entry point, called from mce.c with preempt off */
332 void mce_amd_feature_init(struct cpuinfo_x86 *c)
334 u32 low = 0, high = 0, address = 0;
335 unsigned int bank, block;
338 for (bank = 0; bank < mca_cfg.banks; ++bank) {
339 for (block = 0; block < NR_BLOCKS; ++block) {
341 address = MSR_IA32_MCx_MISC(bank);
342 else if (block == 1) {
343 address = (low & MASK_BLKPTR_LO) >> 21;
347 address += MCG_XBLK_ADDR;
351 if (rdmsr_safe(address, &low, &high))
354 if (!(high & MASK_VALID_HI))
357 if (!(high & MASK_CNTP_HI) ||
358 (high & MASK_LOCKED_HI))
361 offset = prepare_threshold_block(bank, block, address, offset, high);
365 if (mce_flags.succor)
366 deferred_error_interrupt_enable(c);
369 static void __log_error(unsigned int bank, bool threshold_err, u64 misc)
374 rdmsrl(MSR_IA32_MCx_STATUS(bank), status);
375 if (!(status & MCI_STATUS_VAL))
386 if (m.status & MCI_STATUS_ADDRV)
387 rdmsrl(MSR_IA32_MCx_ADDR(bank), m.addr);
390 wrmsrl(MSR_IA32_MCx_STATUS(bank), 0);
393 static inline void __smp_deferred_error_interrupt(void)
395 inc_irq_stat(irq_deferred_error_count);
396 deferred_error_int_vector();
399 asmlinkage __visible void smp_deferred_error_interrupt(void)
402 __smp_deferred_error_interrupt();
406 asmlinkage __visible void smp_trace_deferred_error_interrupt(void)
409 trace_deferred_error_apic_entry(DEFERRED_ERROR_VECTOR);
410 __smp_deferred_error_interrupt();
411 trace_deferred_error_apic_exit(DEFERRED_ERROR_VECTOR);
415 /* APIC interrupt handler for deferred errors */
416 static void amd_deferred_error_interrupt(void)
421 for (bank = 0; bank < mca_cfg.banks; ++bank) {
422 rdmsrl(MSR_IA32_MCx_STATUS(bank), status);
424 if (!(status & MCI_STATUS_VAL) ||
425 !(status & MCI_STATUS_DEFERRED))
428 __log_error(bank, false, 0);
434 * APIC Interrupt Handler
438 * threshold interrupt handler will service THRESHOLD_APIC_VECTOR.
439 * the interrupt goes off when error_count reaches threshold_limit.
440 * the handler will simply log mcelog w/ software defined bank number.
443 static void amd_threshold_interrupt(void)
445 u32 low = 0, high = 0, address = 0;
446 int cpu = smp_processor_id();
447 unsigned int bank, block;
449 /* assume first bank caused it */
450 for (bank = 0; bank < mca_cfg.banks; ++bank) {
451 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
453 for (block = 0; block < NR_BLOCKS; ++block) {
455 address = MSR_IA32_MCx_MISC(bank);
456 } else if (block == 1) {
457 address = (low & MASK_BLKPTR_LO) >> 21;
460 address += MCG_XBLK_ADDR;
465 if (rdmsr_safe(address, &low, &high))
468 if (!(high & MASK_VALID_HI)) {
475 if (!(high & MASK_CNTP_HI) ||
476 (high & MASK_LOCKED_HI))
480 * Log the machine check that caused the threshold
483 if (high & MASK_OVERFLOW_HI)
490 __log_error(bank, true, ((u64)high << 32) | low);
497 struct threshold_attr {
498 struct attribute attr;
499 ssize_t (*show) (struct threshold_block *, char *);
500 ssize_t (*store) (struct threshold_block *, const char *, size_t count);
503 #define SHOW_FIELDS(name) \
504 static ssize_t show_ ## name(struct threshold_block *b, char *buf) \
506 return sprintf(buf, "%lu\n", (unsigned long) b->name); \
508 SHOW_FIELDS(interrupt_enable)
509 SHOW_FIELDS(threshold_limit)
512 store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size)
514 struct thresh_restart tr;
517 if (!b->interrupt_capable)
520 if (kstrtoul(buf, 0, &new) < 0)
523 b->interrupt_enable = !!new;
525 memset(&tr, 0, sizeof(tr));
528 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
534 store_threshold_limit(struct threshold_block *b, const char *buf, size_t size)
536 struct thresh_restart tr;
539 if (kstrtoul(buf, 0, &new) < 0)
542 if (new > THRESHOLD_MAX)
547 memset(&tr, 0, sizeof(tr));
548 tr.old_limit = b->threshold_limit;
549 b->threshold_limit = new;
552 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
557 static ssize_t show_error_count(struct threshold_block *b, char *buf)
561 rdmsr_on_cpu(b->cpu, b->address, &lo, &hi);
563 return sprintf(buf, "%u\n", ((hi & THRESHOLD_MAX) -
564 (THRESHOLD_MAX - b->threshold_limit)));
567 static struct threshold_attr error_count = {
568 .attr = {.name = __stringify(error_count), .mode = 0444 },
569 .show = show_error_count,
572 #define RW_ATTR(val) \
573 static struct threshold_attr val = { \
574 .attr = {.name = __stringify(val), .mode = 0644 }, \
575 .show = show_## val, \
576 .store = store_## val, \
579 RW_ATTR(interrupt_enable);
580 RW_ATTR(threshold_limit);
582 static struct attribute *default_attrs[] = {
583 &threshold_limit.attr,
585 NULL, /* possibly interrupt_enable if supported, see below */
589 #define to_block(k) container_of(k, struct threshold_block, kobj)
590 #define to_attr(a) container_of(a, struct threshold_attr, attr)
592 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
594 struct threshold_block *b = to_block(kobj);
595 struct threshold_attr *a = to_attr(attr);
598 ret = a->show ? a->show(b, buf) : -EIO;
603 static ssize_t store(struct kobject *kobj, struct attribute *attr,
604 const char *buf, size_t count)
606 struct threshold_block *b = to_block(kobj);
607 struct threshold_attr *a = to_attr(attr);
610 ret = a->store ? a->store(b, buf, count) : -EIO;
615 static const struct sysfs_ops threshold_ops = {
620 static struct kobj_type threshold_ktype = {
621 .sysfs_ops = &threshold_ops,
622 .default_attrs = default_attrs,
625 static int allocate_threshold_blocks(unsigned int cpu, unsigned int bank,
626 unsigned int block, u32 address)
628 struct threshold_block *b = NULL;
632 if ((bank >= mca_cfg.banks) || (block >= NR_BLOCKS))
635 if (rdmsr_safe_on_cpu(cpu, address, &low, &high))
638 if (!(high & MASK_VALID_HI)) {
645 if (!(high & MASK_CNTP_HI) ||
646 (high & MASK_LOCKED_HI))
649 b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
656 b->address = address;
657 b->interrupt_enable = 0;
658 b->interrupt_capable = lvt_interrupt_supported(bank, high);
659 b->threshold_limit = THRESHOLD_MAX;
661 if (b->interrupt_capable) {
662 threshold_ktype.default_attrs[2] = &interrupt_enable.attr;
663 b->interrupt_enable = 1;
665 threshold_ktype.default_attrs[2] = NULL;
668 INIT_LIST_HEAD(&b->miscj);
670 if (per_cpu(threshold_banks, cpu)[bank]->blocks) {
672 &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
674 per_cpu(threshold_banks, cpu)[bank]->blocks = b;
677 err = kobject_init_and_add(&b->kobj, &threshold_ktype,
678 per_cpu(threshold_banks, cpu)[bank]->kobj,
679 (bank == 4 ? bank4_names(b) : th_names[bank]));
684 address = (low & MASK_BLKPTR_LO) >> 21;
687 address += MCG_XBLK_ADDR;
692 err = allocate_threshold_blocks(cpu, bank, ++block, address);
697 kobject_uevent(&b->kobj, KOBJ_ADD);
703 kobject_put(&b->kobj);
710 static int __threshold_add_blocks(struct threshold_bank *b)
712 struct list_head *head = &b->blocks->miscj;
713 struct threshold_block *pos = NULL;
714 struct threshold_block *tmp = NULL;
717 err = kobject_add(&b->blocks->kobj, b->kobj, b->blocks->kobj.name);
721 list_for_each_entry_safe(pos, tmp, head, miscj) {
723 err = kobject_add(&pos->kobj, b->kobj, pos->kobj.name);
725 list_for_each_entry_safe_reverse(pos, tmp, head, miscj)
726 kobject_del(&pos->kobj);
734 static int threshold_create_bank(unsigned int cpu, unsigned int bank)
736 struct device *dev = per_cpu(mce_device, cpu);
737 struct amd_northbridge *nb = NULL;
738 struct threshold_bank *b = NULL;
739 const char *name = th_names[bank];
742 if (is_shared_bank(bank)) {
743 nb = node_to_amd_nb(amd_get_nb_id(cpu));
745 /* threshold descriptor already initialized on this node? */
746 if (nb && nb->bank4) {
749 err = kobject_add(b->kobj, &dev->kobj, name);
753 per_cpu(threshold_banks, cpu)[bank] = b;
754 atomic_inc(&b->cpus);
756 err = __threshold_add_blocks(b);
762 b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
768 b->kobj = kobject_create_and_add(name, &dev->kobj);
774 per_cpu(threshold_banks, cpu)[bank] = b;
776 if (is_shared_bank(bank)) {
777 atomic_set(&b->cpus, 1);
779 /* nb is already initialized, see above */
786 err = allocate_threshold_blocks(cpu, bank, 0, MSR_IA32_MCx_MISC(bank));
797 /* create dir/files for all valid threshold banks */
798 static int threshold_create_device(unsigned int cpu)
801 struct threshold_bank **bp;
804 bp = kzalloc(sizeof(struct threshold_bank *) * mca_cfg.banks,
809 per_cpu(threshold_banks, cpu) = bp;
811 for (bank = 0; bank < mca_cfg.banks; ++bank) {
812 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
814 err = threshold_create_bank(cpu, bank);
822 static void deallocate_threshold_block(unsigned int cpu,
825 struct threshold_block *pos = NULL;
826 struct threshold_block *tmp = NULL;
827 struct threshold_bank *head = per_cpu(threshold_banks, cpu)[bank];
832 list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
833 kobject_put(&pos->kobj);
834 list_del(&pos->miscj);
838 kfree(per_cpu(threshold_banks, cpu)[bank]->blocks);
839 per_cpu(threshold_banks, cpu)[bank]->blocks = NULL;
842 static void __threshold_remove_blocks(struct threshold_bank *b)
844 struct threshold_block *pos = NULL;
845 struct threshold_block *tmp = NULL;
847 kobject_del(b->kobj);
849 list_for_each_entry_safe(pos, tmp, &b->blocks->miscj, miscj)
850 kobject_del(&pos->kobj);
853 static void threshold_remove_bank(unsigned int cpu, int bank)
855 struct amd_northbridge *nb;
856 struct threshold_bank *b;
858 b = per_cpu(threshold_banks, cpu)[bank];
865 if (is_shared_bank(bank)) {
866 if (!atomic_dec_and_test(&b->cpus)) {
867 __threshold_remove_blocks(b);
868 per_cpu(threshold_banks, cpu)[bank] = NULL;
872 * the last CPU on this node using the shared bank is
873 * going away, remove that bank now.
875 nb = node_to_amd_nb(amd_get_nb_id(cpu));
880 deallocate_threshold_block(cpu, bank);
883 kobject_del(b->kobj);
884 kobject_put(b->kobj);
886 per_cpu(threshold_banks, cpu)[bank] = NULL;
889 static void threshold_remove_device(unsigned int cpu)
893 for (bank = 0; bank < mca_cfg.banks; ++bank) {
894 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
896 threshold_remove_bank(cpu, bank);
898 kfree(per_cpu(threshold_banks, cpu));
901 /* get notified when a cpu comes on/off */
903 amd_64_threshold_cpu_callback(unsigned long action, unsigned int cpu)
907 case CPU_ONLINE_FROZEN:
908 threshold_create_device(cpu);
911 case CPU_DEAD_FROZEN:
912 threshold_remove_device(cpu);
919 static __init int threshold_init_device(void)
923 /* to hit CPUs online before the notifier is up */
924 for_each_online_cpu(lcpu) {
925 int err = threshold_create_device(lcpu);
930 threshold_cpu_callback = amd_64_threshold_cpu_callback;
935 * there are 3 funcs which need to be _initcalled in a logic sequence:
936 * 1. xen_late_init_mcelog
937 * 2. mcheck_init_device
938 * 3. threshold_init_device
940 * xen_late_init_mcelog must register xen_mce_chrdev_device before
941 * native mce_chrdev_device registration if running under xen platform;
943 * mcheck_init_device should be inited before threshold_init_device to
944 * initialize mce_device, otherwise a NULL ptr dereference will cause panic.
946 * so we use following _initcalls
947 * 1. device_initcall(xen_late_init_mcelog);
948 * 2. device_initcall_sync(mcheck_init_device);
949 * 3. late_initcall(threshold_init_device);
951 * when running under xen, the initcall order is 1,2,3;
952 * on baremetal, we skip 1 and we do only 2 and 3.
954 late_initcall(threshold_init_device);