2 * (c) 2005, 2006 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.
9 * Support : jacob.shin@amd.com
12 * - added support for AMD Family 0x10 processors
14 * All MC4_MISCi registers are shared between multi-cores
16 #include <linux/interrupt.h>
17 #include <linux/notifier.h>
18 #include <linux/kobject.h>
19 #include <linux/percpu.h>
20 #include <linux/sysdev.h>
21 #include <linux/errno.h>
22 #include <linux/sched.h>
23 #include <linux/sysfs.h>
24 #include <linux/init.h>
25 #include <linux/cpu.h>
26 #include <linux/smp.h>
33 #define PFX "mce_threshold: "
34 #define VERSION "version 1.1.1"
37 #define THRESHOLD_MAX 0xFFF
38 #define INT_TYPE_APIC 0x00020000
39 #define MASK_VALID_HI 0x80000000
40 #define MASK_CNTP_HI 0x40000000
41 #define MASK_LOCKED_HI 0x20000000
42 #define MASK_LVTOFF_HI 0x00F00000
43 #define MASK_COUNT_EN_HI 0x00080000
44 #define MASK_INT_TYPE_HI 0x00060000
45 #define MASK_OVERFLOW_HI 0x00010000
46 #define MASK_ERR_COUNT_HI 0x00000FFF
47 #define MASK_BLKPTR_LO 0xFF000000
48 #define MCG_XBLK_ADDR 0xC0000400
50 struct threshold_block {
58 struct list_head miscj;
61 /* defaults used early on boot */
62 static struct threshold_block threshold_defaults = {
63 .interrupt_enable = 0,
64 .threshold_limit = THRESHOLD_MAX,
67 struct threshold_bank {
69 struct threshold_block *blocks;
72 static DEFINE_PER_CPU(struct threshold_bank * [NR_BANKS], threshold_banks);
75 static unsigned char shared_bank[NR_BANKS] = {
80 static DEFINE_PER_CPU(unsigned char, bank_map); /* see which banks are on */
82 static void amd_threshold_interrupt(void);
88 struct thresh_restart {
89 struct threshold_block *b;
94 /* must be called with correct cpu affinity */
95 /* Called via smp_call_function_single() */
96 static void threshold_restart_bank(void *_tr)
98 struct thresh_restart *tr = _tr;
99 u32 mci_misc_hi, mci_misc_lo;
101 rdmsr(tr->b->address, mci_misc_lo, mci_misc_hi);
103 if (tr->b->threshold_limit < (mci_misc_hi & THRESHOLD_MAX))
104 tr->reset = 1; /* limit cannot be lower than err count */
106 if (tr->reset) { /* reset err count and overflow bit */
108 (mci_misc_hi & ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI)) |
109 (THRESHOLD_MAX - tr->b->threshold_limit);
110 } else if (tr->old_limit) { /* change limit w/o reset */
111 int new_count = (mci_misc_hi & THRESHOLD_MAX) +
112 (tr->old_limit - tr->b->threshold_limit);
114 mci_misc_hi = (mci_misc_hi & ~MASK_ERR_COUNT_HI) |
115 (new_count & THRESHOLD_MAX);
118 tr->b->interrupt_enable ?
119 (mci_misc_hi = (mci_misc_hi & ~MASK_INT_TYPE_HI) | INT_TYPE_APIC) :
120 (mci_misc_hi &= ~MASK_INT_TYPE_HI);
122 mci_misc_hi |= MASK_COUNT_EN_HI;
123 wrmsr(tr->b->address, mci_misc_lo, mci_misc_hi);
126 /* cpu init entry point, called from mce.c with preempt off */
127 void mce_amd_feature_init(struct cpuinfo_x86 *c)
129 unsigned int cpu = smp_processor_id();
130 u32 low = 0, high = 0, address = 0;
131 unsigned int bank, block;
132 struct thresh_restart tr;
135 for (bank = 0; bank < NR_BANKS; ++bank) {
136 for (block = 0; block < NR_BLOCKS; ++block) {
138 address = MSR_IA32_MC0_MISC + bank * 4;
139 else if (block == 1) {
140 address = (low & MASK_BLKPTR_LO) >> 21;
144 address += MCG_XBLK_ADDR;
148 if (rdmsr_safe(address, &low, &high))
151 if (!(high & MASK_VALID_HI))
154 if (!(high & MASK_CNTP_HI) ||
155 (high & MASK_LOCKED_HI))
159 per_cpu(bank_map, cpu) |= (1 << bank);
161 if (shared_bank[bank] && c->cpu_core_id)
164 lvt_off = setup_APIC_eilvt_mce(THRESHOLD_APIC_VECTOR,
165 APIC_EILVT_MSG_FIX, 0);
167 high &= ~MASK_LVTOFF_HI;
168 high |= lvt_off << 20;
169 wrmsr(address, low, high);
171 threshold_defaults.address = address;
172 tr.b = &threshold_defaults;
175 threshold_restart_bank(&tr);
177 mce_threshold_vector = amd_threshold_interrupt;
183 * APIC Interrupt Handler
187 * threshold interrupt handler will service THRESHOLD_APIC_VECTOR.
188 * the interrupt goes off when error_count reaches threshold_limit.
189 * the handler will simply log mcelog w/ software defined bank number.
191 static void amd_threshold_interrupt(void)
193 u32 low = 0, high = 0, address = 0;
194 unsigned int bank, block;
199 /* assume first bank caused it */
200 for (bank = 0; bank < NR_BANKS; ++bank) {
201 if (!(per_cpu(bank_map, m.cpu) & (1 << bank)))
203 for (block = 0; block < NR_BLOCKS; ++block) {
205 address = MSR_IA32_MC0_MISC + bank * 4;
206 } else if (block == 1) {
207 address = (low & MASK_BLKPTR_LO) >> 21;
210 address += MCG_XBLK_ADDR;
215 if (rdmsr_safe(address, &low, &high))
218 if (!(high & MASK_VALID_HI)) {
225 if (!(high & MASK_CNTP_HI) ||
226 (high & MASK_LOCKED_HI))
230 * Log the machine check that caused the threshold
233 machine_check_poll(MCP_TIMESTAMP,
234 &__get_cpu_var(mce_poll_banks));
236 if (high & MASK_OVERFLOW_HI) {
237 rdmsrl(address, m.misc);
238 rdmsrl(MSR_IA32_MC0_STATUS + bank * 4,
240 m.bank = K8_MCE_THRESHOLD_BASE
254 struct threshold_attr {
255 struct attribute attr;
256 ssize_t (*show) (struct threshold_block *, char *);
257 ssize_t (*store) (struct threshold_block *, const char *, size_t count);
260 #define SHOW_FIELDS(name) \
261 static ssize_t show_ ## name(struct threshold_block *b, char *buf) \
263 return sprintf(buf, "%lx\n", (unsigned long) b->name); \
265 SHOW_FIELDS(interrupt_enable)
266 SHOW_FIELDS(threshold_limit)
269 store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size)
271 struct thresh_restart tr;
274 if (strict_strtoul(buf, 0, &new) < 0)
277 b->interrupt_enable = !!new;
283 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
289 store_threshold_limit(struct threshold_block *b, const char *buf, size_t size)
291 struct thresh_restart tr;
294 if (strict_strtoul(buf, 0, &new) < 0)
297 if (new > THRESHOLD_MAX)
302 tr.old_limit = b->threshold_limit;
303 b->threshold_limit = new;
307 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
312 struct threshold_block_cross_cpu {
313 struct threshold_block *tb;
317 static void local_error_count_handler(void *_tbcc)
319 struct threshold_block_cross_cpu *tbcc = _tbcc;
320 struct threshold_block *b = tbcc->tb;
323 rdmsr(b->address, low, high);
324 tbcc->retval = (high & 0xFFF) - (THRESHOLD_MAX - b->threshold_limit);
327 static ssize_t show_error_count(struct threshold_block *b, char *buf)
329 struct threshold_block_cross_cpu tbcc = { .tb = b, };
331 smp_call_function_single(b->cpu, local_error_count_handler, &tbcc, 1);
332 return sprintf(buf, "%lx\n", tbcc.retval);
335 static ssize_t store_error_count(struct threshold_block *b,
336 const char *buf, size_t count)
338 struct thresh_restart tr = { .b = b, .reset = 1, .old_limit = 0 };
340 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
344 #define RW_ATTR(val) \
345 static struct threshold_attr val = { \
346 .attr = {.name = __stringify(val), .mode = 0644 }, \
347 .show = show_## val, \
348 .store = store_## val, \
351 RW_ATTR(interrupt_enable);
352 RW_ATTR(threshold_limit);
353 RW_ATTR(error_count);
355 static struct attribute *default_attrs[] = {
356 &interrupt_enable.attr,
357 &threshold_limit.attr,
362 #define to_block(k) container_of(k, struct threshold_block, kobj)
363 #define to_attr(a) container_of(a, struct threshold_attr, attr)
365 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
367 struct threshold_block *b = to_block(kobj);
368 struct threshold_attr *a = to_attr(attr);
371 ret = a->show ? a->show(b, buf) : -EIO;
376 static ssize_t store(struct kobject *kobj, struct attribute *attr,
377 const char *buf, size_t count)
379 struct threshold_block *b = to_block(kobj);
380 struct threshold_attr *a = to_attr(attr);
383 ret = a->store ? a->store(b, buf, count) : -EIO;
388 static struct sysfs_ops threshold_ops = {
393 static struct kobj_type threshold_ktype = {
394 .sysfs_ops = &threshold_ops,
395 .default_attrs = default_attrs,
398 static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
403 struct threshold_block *b = NULL;
407 if ((bank >= NR_BANKS) || (block >= NR_BLOCKS))
410 if (rdmsr_safe_on_cpu(cpu, address, &low, &high))
413 if (!(high & MASK_VALID_HI)) {
420 if (!(high & MASK_CNTP_HI) ||
421 (high & MASK_LOCKED_HI))
424 b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
431 b->address = address;
432 b->interrupt_enable = 0;
433 b->threshold_limit = THRESHOLD_MAX;
435 INIT_LIST_HEAD(&b->miscj);
437 if (per_cpu(threshold_banks, cpu)[bank]->blocks) {
439 &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
441 per_cpu(threshold_banks, cpu)[bank]->blocks = b;
444 err = kobject_init_and_add(&b->kobj, &threshold_ktype,
445 per_cpu(threshold_banks, cpu)[bank]->kobj,
451 address = (low & MASK_BLKPTR_LO) >> 21;
454 address += MCG_XBLK_ADDR;
459 err = allocate_threshold_blocks(cpu, bank, ++block, address);
464 kobject_uevent(&b->kobj, KOBJ_ADD);
470 kobject_put(&b->kobj);
476 static __cpuinit long
477 local_allocate_threshold_blocks(int cpu, unsigned int bank)
479 return allocate_threshold_blocks(cpu, bank, 0,
480 MSR_IA32_MC0_MISC + bank * 4);
483 /* symlinks sibling shared banks to first core. first core owns dir/files. */
484 static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
487 struct threshold_bank *b = NULL;
490 struct cpuinfo_x86 *c = &cpu_data(cpu);
493 sprintf(name, "threshold_bank%i", bank);
496 if (cpu_data(cpu).cpu_core_id && shared_bank[bank]) { /* symlink */
497 i = cpumask_first(c->llc_shared_map);
499 /* first core not up yet */
500 if (cpu_data(i).cpu_core_id)
504 if (per_cpu(threshold_banks, cpu)[bank])
507 b = per_cpu(threshold_banks, i)[bank];
512 err = sysfs_create_link(&per_cpu(mce_dev, cpu).kobj,
517 cpumask_copy(b->cpus, c->llc_shared_map);
518 per_cpu(threshold_banks, cpu)[bank] = b;
524 b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
529 if (!alloc_cpumask_var(&b->cpus, GFP_KERNEL)) {
535 b->kobj = kobject_create_and_add(name, &per_cpu(mce_dev, cpu).kobj);
540 cpumask_setall(b->cpus);
542 cpumask_copy(b->cpus, c->llc_shared_map);
545 per_cpu(threshold_banks, cpu)[bank] = b;
547 err = local_allocate_threshold_blocks(cpu, bank);
551 for_each_cpu(i, b->cpus) {
555 err = sysfs_create_link(&per_cpu(mce_dev, i).kobj,
560 per_cpu(threshold_banks, i)[bank] = b;
566 per_cpu(threshold_banks, cpu)[bank] = NULL;
567 free_cpumask_var(b->cpus);
573 /* create dir/files for all valid threshold banks */
574 static __cpuinit int threshold_create_device(unsigned int cpu)
579 for (bank = 0; bank < NR_BANKS; ++bank) {
580 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
582 err = threshold_create_bank(cpu, bank);
591 * let's be hotplug friendly.
592 * in case of multiple core processors, the first core always takes ownership
593 * of shared sysfs dir/files, and rest of the cores will be symlinked to it.
596 static void deallocate_threshold_block(unsigned int cpu,
599 struct threshold_block *pos = NULL;
600 struct threshold_block *tmp = NULL;
601 struct threshold_bank *head = per_cpu(threshold_banks, cpu)[bank];
606 list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
607 kobject_put(&pos->kobj);
608 list_del(&pos->miscj);
612 kfree(per_cpu(threshold_banks, cpu)[bank]->blocks);
613 per_cpu(threshold_banks, cpu)[bank]->blocks = NULL;
616 static void threshold_remove_bank(unsigned int cpu, int bank)
618 struct threshold_bank *b;
622 b = per_cpu(threshold_banks, cpu)[bank];
628 sprintf(name, "threshold_bank%i", bank);
631 /* sibling symlink */
632 if (shared_bank[bank] && b->blocks->cpu != cpu) {
633 sysfs_remove_link(&per_cpu(mce_dev, cpu).kobj, name);
634 per_cpu(threshold_banks, cpu)[bank] = NULL;
640 /* remove all sibling symlinks before unregistering */
641 for_each_cpu(i, b->cpus) {
645 sysfs_remove_link(&per_cpu(mce_dev, i).kobj, name);
646 per_cpu(threshold_banks, i)[bank] = NULL;
649 deallocate_threshold_block(cpu, bank);
652 kobject_del(b->kobj);
653 kobject_put(b->kobj);
654 free_cpumask_var(b->cpus);
656 per_cpu(threshold_banks, cpu)[bank] = NULL;
659 static void threshold_remove_device(unsigned int cpu)
663 for (bank = 0; bank < NR_BANKS; ++bank) {
664 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
666 threshold_remove_bank(cpu, bank);
670 /* get notified when a cpu comes on/off */
671 static void __cpuinit
672 amd_64_threshold_cpu_callback(unsigned long action, unsigned int cpu)
676 case CPU_ONLINE_FROZEN:
677 threshold_create_device(cpu);
680 case CPU_DEAD_FROZEN:
681 threshold_remove_device(cpu);
688 static __init int threshold_init_device(void)
692 /* to hit CPUs online before the notifier is up */
693 for_each_online_cpu(lcpu) {
694 int err = threshold_create_device(lcpu);
699 threshold_cpu_callback = amd_64_threshold_cpu_callback;
703 device_initcall(threshold_init_device);