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
17 #include <linux/cpu.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/kobject.h>
22 #include <linux/notifier.h>
23 #include <linux/sched.h>
24 #include <linux/smp.h>
25 #include <linux/sysdev.h>
26 #include <linux/sysfs.h>
30 #include <asm/percpu.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 *, threshold_banks[NR_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);
113 mci_misc_hi = (mci_misc_hi & ~MASK_ERR_COUNT_HI) |
114 (new_count & THRESHOLD_MAX);
117 tr->b->interrupt_enable ?
118 (mci_misc_hi = (mci_misc_hi & ~MASK_INT_TYPE_HI) | INT_TYPE_APIC) :
119 (mci_misc_hi &= ~MASK_INT_TYPE_HI);
121 mci_misc_hi |= MASK_COUNT_EN_HI;
122 wrmsr(tr->b->address, mci_misc_lo, mci_misc_hi);
125 /* cpu init entry point, called from mce.c with preempt off */
126 void mce_amd_feature_init(struct cpuinfo_x86 *c)
128 unsigned int bank, block;
129 unsigned int cpu = smp_processor_id();
131 u32 low = 0, high = 0, address = 0;
132 struct thresh_restart tr;
134 for (bank = 0; bank < NR_BANKS; ++bank) {
135 for (block = 0; block < NR_BLOCKS; ++block) {
137 address = MSR_IA32_MC0_MISC + bank * 4;
138 else if (block == 1) {
139 address = (low & MASK_BLKPTR_LO) >> 21;
142 address += MCG_XBLK_ADDR;
147 if (rdmsr_safe(address, &low, &high))
150 if (!(high & MASK_VALID_HI)) {
157 if (!(high & MASK_CNTP_HI) ||
158 (high & MASK_LOCKED_HI))
162 per_cpu(bank_map, cpu) |= (1 << bank);
164 if (shared_bank[bank] && c->cpu_core_id)
167 lvt_off = setup_APIC_eilvt_mce(THRESHOLD_APIC_VECTOR,
168 APIC_EILVT_MSG_FIX, 0);
170 high &= ~MASK_LVTOFF_HI;
171 high |= lvt_off << 20;
172 wrmsr(address, low, high);
174 threshold_defaults.address = address;
175 tr.b = &threshold_defaults;
178 threshold_restart_bank(&tr);
180 mce_threshold_vector = amd_threshold_interrupt;
186 * APIC Interrupt Handler
190 * threshold interrupt handler will service THRESHOLD_APIC_VECTOR.
191 * the interrupt goes off when error_count reaches threshold_limit.
192 * the handler will simply log mcelog w/ software defined bank number.
194 static void amd_threshold_interrupt(void)
196 unsigned int bank, block;
198 u32 low = 0, high = 0, address = 0;
202 /* assume first bank caused it */
203 for (bank = 0; bank < NR_BANKS; ++bank) {
204 if (!(per_cpu(bank_map, m.cpu) & (1 << bank)))
206 for (block = 0; block < NR_BLOCKS; ++block) {
208 address = MSR_IA32_MC0_MISC + bank * 4;
209 else if (block == 1) {
210 address = (low & MASK_BLKPTR_LO) >> 21;
213 address += MCG_XBLK_ADDR;
218 if (rdmsr_safe(address, &low, &high))
221 if (!(high & MASK_VALID_HI)) {
228 if (!(high & MASK_CNTP_HI) ||
229 (high & MASK_LOCKED_HI))
232 /* Log the machine check that caused the threshold
234 machine_check_poll(MCP_TIMESTAMP,
235 &__get_cpu_var(mce_poll_banks));
237 if (high & MASK_OVERFLOW_HI) {
238 rdmsrl(address, m.misc);
239 rdmsrl(MSR_IA32_MC0_STATUS + bank * 4,
241 m.bank = K8_MCE_THRESHOLD_BASE
255 struct threshold_attr {
256 struct attribute attr;
257 ssize_t(*show) (struct threshold_block *, char *);
258 ssize_t(*store) (struct threshold_block *, const char *, size_t count);
261 #define SHOW_FIELDS(name) \
262 static ssize_t show_ ## name(struct threshold_block * b, char *buf) \
264 return sprintf(buf, "%lx\n", (unsigned long) b->name); \
266 SHOW_FIELDS(interrupt_enable)
267 SHOW_FIELDS(threshold_limit)
269 static ssize_t store_interrupt_enable(struct threshold_block *b,
270 const char *buf, size_t count)
273 struct thresh_restart tr;
274 unsigned long new = simple_strtoul(buf, &end, 0);
277 b->interrupt_enable = !!new;
282 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
287 static ssize_t store_threshold_limit(struct threshold_block *b,
288 const char *buf, size_t count)
291 struct thresh_restart tr;
292 unsigned long new = simple_strtoul(buf, &end, 0);
295 if (new > THRESHOLD_MAX)
299 tr.old_limit = b->threshold_limit;
300 b->threshold_limit = new;
304 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
309 struct threshold_block_cross_cpu {
310 struct threshold_block *tb;
314 static void local_error_count_handler(void *_tbcc)
316 struct threshold_block_cross_cpu *tbcc = _tbcc;
317 struct threshold_block *b = tbcc->tb;
320 rdmsr(b->address, low, high);
321 tbcc->retval = (high & 0xFFF) - (THRESHOLD_MAX - b->threshold_limit);
324 static ssize_t show_error_count(struct threshold_block *b, char *buf)
326 struct threshold_block_cross_cpu tbcc = { .tb = b, };
328 smp_call_function_single(b->cpu, local_error_count_handler, &tbcc, 1);
329 return sprintf(buf, "%lx\n", tbcc.retval);
332 static ssize_t store_error_count(struct threshold_block *b,
333 const char *buf, size_t count)
335 struct thresh_restart tr = { .b = b, .reset = 1, .old_limit = 0 };
337 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
341 #define THRESHOLD_ATTR(_name,_mode,_show,_store) { \
342 .attr = {.name = __stringify(_name), .mode = _mode }, \
347 #define RW_ATTR(name) \
348 static struct threshold_attr name = \
349 THRESHOLD_ATTR(name, 0644, show_## name, store_## name)
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);
370 ret = a->show ? a->show(b, buf) : -EIO;
374 static ssize_t store(struct kobject *kobj, struct attribute *attr,
375 const char *buf, size_t count)
377 struct threshold_block *b = to_block(kobj);
378 struct threshold_attr *a = to_attr(attr);
380 ret = a->store ? a->store(b, buf, count) : -EIO;
384 static struct sysfs_ops threshold_ops = {
389 static struct kobj_type threshold_ktype = {
390 .sysfs_ops = &threshold_ops,
391 .default_attrs = default_attrs,
394 static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
401 struct threshold_block *b = NULL;
403 if ((bank >= NR_BANKS) || (block >= NR_BLOCKS))
406 if (rdmsr_safe_on_cpu(cpu, address, &low, &high))
409 if (!(high & MASK_VALID_HI)) {
416 if (!(high & MASK_CNTP_HI) ||
417 (high & MASK_LOCKED_HI))
420 b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
427 b->address = address;
428 b->interrupt_enable = 0;
429 b->threshold_limit = THRESHOLD_MAX;
431 INIT_LIST_HEAD(&b->miscj);
433 if (per_cpu(threshold_banks, cpu)[bank]->blocks)
435 &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
437 per_cpu(threshold_banks, cpu)[bank]->blocks = b;
439 err = kobject_init_and_add(&b->kobj, &threshold_ktype,
440 per_cpu(threshold_banks, cpu)[bank]->kobj,
446 address = (low & MASK_BLKPTR_LO) >> 21;
449 address += MCG_XBLK_ADDR;
453 err = allocate_threshold_blocks(cpu, bank, ++block, address);
458 kobject_uevent(&b->kobj, KOBJ_ADD);
464 kobject_put(&b->kobj);
470 static __cpuinit long
471 local_allocate_threshold_blocks(int cpu, unsigned int bank)
473 return allocate_threshold_blocks(cpu, bank, 0,
474 MSR_IA32_MC0_MISC + bank * 4);
477 /* symlinks sibling shared banks to first core. first core owns dir/files. */
478 static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
481 struct threshold_bank *b = NULL;
484 sprintf(name, "threshold_bank%i", bank);
487 if (cpu_data(cpu).cpu_core_id && shared_bank[bank]) { /* symlink */
488 i = cpumask_first(&per_cpu(cpu_core_map, cpu));
490 /* first core not up yet */
491 if (cpu_data(i).cpu_core_id)
495 if (per_cpu(threshold_banks, cpu)[bank])
498 b = per_cpu(threshold_banks, i)[bank];
503 err = sysfs_create_link(&per_cpu(device_mce, cpu).kobj,
508 cpumask_copy(b->cpus, &per_cpu(cpu_core_map, cpu));
509 per_cpu(threshold_banks, cpu)[bank] = b;
514 b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
519 if (!alloc_cpumask_var(&b->cpus, GFP_KERNEL)) {
525 b->kobj = kobject_create_and_add(name, &per_cpu(device_mce, cpu).kobj);
530 cpumask_setall(b->cpus);
532 cpumask_copy(b->cpus, &per_cpu(cpu_core_map, cpu));
535 per_cpu(threshold_banks, cpu)[bank] = b;
537 err = local_allocate_threshold_blocks(cpu, bank);
541 for_each_cpu(i, b->cpus) {
545 err = sysfs_create_link(&per_cpu(device_mce, i).kobj,
550 per_cpu(threshold_banks, i)[bank] = b;
556 per_cpu(threshold_banks, cpu)[bank] = NULL;
557 free_cpumask_var(b->cpus);
563 /* create dir/files for all valid threshold banks */
564 static __cpuinit int threshold_create_device(unsigned int cpu)
569 for (bank = 0; bank < NR_BANKS; ++bank) {
570 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
572 err = threshold_create_bank(cpu, bank);
581 * let's be hotplug friendly.
582 * in case of multiple core processors, the first core always takes ownership
583 * of shared sysfs dir/files, and rest of the cores will be symlinked to it.
586 static void deallocate_threshold_block(unsigned int cpu,
589 struct threshold_block *pos = NULL;
590 struct threshold_block *tmp = NULL;
591 struct threshold_bank *head = per_cpu(threshold_banks, cpu)[bank];
596 list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
597 kobject_put(&pos->kobj);
598 list_del(&pos->miscj);
602 kfree(per_cpu(threshold_banks, cpu)[bank]->blocks);
603 per_cpu(threshold_banks, cpu)[bank]->blocks = NULL;
606 static void threshold_remove_bank(unsigned int cpu, int bank)
609 struct threshold_bank *b;
612 b = per_cpu(threshold_banks, cpu)[bank];
620 sprintf(name, "threshold_bank%i", bank);
623 /* sibling symlink */
624 if (shared_bank[bank] && b->blocks->cpu != cpu) {
625 sysfs_remove_link(&per_cpu(device_mce, cpu).kobj, name);
626 per_cpu(threshold_banks, cpu)[bank] = NULL;
631 /* remove all sibling symlinks before unregistering */
632 for_each_cpu(i, b->cpus) {
636 sysfs_remove_link(&per_cpu(device_mce, i).kobj, name);
637 per_cpu(threshold_banks, i)[bank] = NULL;
640 deallocate_threshold_block(cpu, bank);
643 kobject_del(b->kobj);
644 kobject_put(b->kobj);
645 free_cpumask_var(b->cpus);
647 per_cpu(threshold_banks, cpu)[bank] = NULL;
650 static void threshold_remove_device(unsigned int cpu)
654 for (bank = 0; bank < NR_BANKS; ++bank) {
655 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
657 threshold_remove_bank(cpu, bank);
661 /* get notified when a cpu comes on/off */
662 static void __cpuinit amd_64_threshold_cpu_callback(unsigned long action,
670 case CPU_ONLINE_FROZEN:
671 threshold_create_device(cpu);
674 case CPU_DEAD_FROZEN:
675 threshold_remove_device(cpu);
682 static __init int threshold_init_device(void)
686 /* to hit CPUs online before the notifier is up */
687 for_each_online_cpu(lcpu) {
688 int err = threshold_create_device(lcpu);
692 threshold_cpu_callback = amd_64_threshold_cpu_callback;
696 device_initcall(threshold_init_device);