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.
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/slab.h>
25 #include <linux/init.h>
26 #include <linux/cpu.h>
27 #include <linux/smp.h>
36 #define THRESHOLD_MAX 0xFFF
37 #define INT_TYPE_APIC 0x00020000
38 #define MASK_VALID_HI 0x80000000
39 #define MASK_CNTP_HI 0x40000000
40 #define MASK_LOCKED_HI 0x20000000
41 #define MASK_LVTOFF_HI 0x00F00000
42 #define MASK_COUNT_EN_HI 0x00080000
43 #define MASK_INT_TYPE_HI 0x00060000
44 #define MASK_OVERFLOW_HI 0x00010000
45 #define MASK_ERR_COUNT_HI 0x00000FFF
46 #define MASK_BLKPTR_LO 0xFF000000
47 #define MCG_XBLK_ADDR 0xC0000400
49 struct threshold_block {
57 struct list_head miscj;
60 struct threshold_bank {
62 struct threshold_block *blocks;
65 static DEFINE_PER_CPU(struct threshold_bank * [NR_BANKS], threshold_banks);
68 static unsigned char shared_bank[NR_BANKS] = {
73 static DEFINE_PER_CPU(unsigned char, bank_map); /* see which banks are on */
75 static void amd_threshold_interrupt(void);
81 struct thresh_restart {
82 struct threshold_block *b;
89 static int lvt_off_valid(struct threshold_block *b, int apic, u32 lo, u32 hi)
91 int msr = (hi & MASK_LVTOFF_HI) >> 20;
94 pr_err(FW_BUG "cpu %d, failed to setup threshold interrupt "
95 "for bank %d, block %d (MSR%08X=0x%x%08x)\n", b->cpu,
96 b->bank, b->block, b->address, hi, lo);
101 pr_err(FW_BUG "cpu %d, invalid threshold interrupt offset %d "
102 "for bank %d, block %d (MSR%08X=0x%x%08x)\n",
103 b->cpu, apic, b->bank, b->block, b->address, hi, lo);
110 /* must be called with correct cpu affinity */
111 /* Called via smp_call_function_single() */
112 static void threshold_restart_bank(void *_tr)
114 struct thresh_restart *tr = _tr;
117 rdmsr(tr->b->address, lo, hi);
119 if (tr->b->threshold_limit < (hi & THRESHOLD_MAX))
120 tr->reset = 1; /* limit cannot be lower than err count */
122 if (tr->reset) { /* reset err count and overflow bit */
124 (hi & ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI)) |
125 (THRESHOLD_MAX - tr->b->threshold_limit);
126 } else if (tr->old_limit) { /* change limit w/o reset */
127 int new_count = (hi & THRESHOLD_MAX) +
128 (tr->old_limit - tr->b->threshold_limit);
130 hi = (hi & ~MASK_ERR_COUNT_HI) |
131 (new_count & THRESHOLD_MAX);
134 if (tr->set_lvt_off) {
135 if (lvt_off_valid(tr->b, tr->lvt_off, lo, hi)) {
136 /* set new lvt offset */
137 hi &= ~MASK_LVTOFF_HI;
138 hi |= tr->lvt_off << 20;
142 tr->b->interrupt_enable ?
143 (hi = (hi & ~MASK_INT_TYPE_HI) | INT_TYPE_APIC) :
144 (hi &= ~MASK_INT_TYPE_HI);
146 hi |= MASK_COUNT_EN_HI;
147 wrmsr(tr->b->address, lo, hi);
150 static void mce_threshold_block_init(struct threshold_block *b, int offset)
152 struct thresh_restart tr = {
158 b->threshold_limit = THRESHOLD_MAX;
159 threshold_restart_bank(&tr);
162 static int setup_APIC_mce(int reserved, int new)
164 if (reserved < 0 && !setup_APIC_eilvt(new, THRESHOLD_APIC_VECTOR,
165 APIC_EILVT_MSG_FIX, 0))
171 /* cpu init entry point, called from mce.c with preempt off */
172 void mce_amd_feature_init(struct cpuinfo_x86 *c)
174 struct threshold_block b;
175 unsigned int cpu = smp_processor_id();
176 u32 low = 0, high = 0, address = 0;
177 unsigned int bank, block;
180 for (bank = 0; bank < NR_BANKS; ++bank) {
181 for (block = 0; block < NR_BLOCKS; ++block) {
183 address = MSR_IA32_MC0_MISC + bank * 4;
184 else if (block == 1) {
185 address = (low & MASK_BLKPTR_LO) >> 21;
189 address += MCG_XBLK_ADDR;
193 if (rdmsr_safe(address, &low, &high))
196 if (!(high & MASK_VALID_HI))
199 if (!(high & MASK_CNTP_HI) ||
200 (high & MASK_LOCKED_HI))
204 per_cpu(bank_map, cpu) |= (1 << bank);
206 if (shared_bank[bank] && c->cpu_core_id)
209 offset = setup_APIC_mce(offset,
210 (high & MASK_LVTOFF_HI) >> 20);
212 memset(&b, 0, sizeof(b));
218 mce_threshold_block_init(&b, offset);
219 mce_threshold_vector = amd_threshold_interrupt;
225 * APIC Interrupt Handler
229 * threshold interrupt handler will service THRESHOLD_APIC_VECTOR.
230 * the interrupt goes off when error_count reaches threshold_limit.
231 * the handler will simply log mcelog w/ software defined bank number.
233 static void amd_threshold_interrupt(void)
235 u32 low = 0, high = 0, address = 0;
236 unsigned int bank, block;
241 /* assume first bank caused it */
242 for (bank = 0; bank < NR_BANKS; ++bank) {
243 if (!(per_cpu(bank_map, m.cpu) & (1 << bank)))
245 for (block = 0; block < NR_BLOCKS; ++block) {
247 address = MSR_IA32_MC0_MISC + bank * 4;
248 } else if (block == 1) {
249 address = (low & MASK_BLKPTR_LO) >> 21;
252 address += MCG_XBLK_ADDR;
257 if (rdmsr_safe(address, &low, &high))
260 if (!(high & MASK_VALID_HI)) {
267 if (!(high & MASK_CNTP_HI) ||
268 (high & MASK_LOCKED_HI))
272 * Log the machine check that caused the threshold
275 machine_check_poll(MCP_TIMESTAMP,
276 &__get_cpu_var(mce_poll_banks));
278 if (high & MASK_OVERFLOW_HI) {
279 rdmsrl(address, m.misc);
280 rdmsrl(MSR_IA32_MC0_STATUS + bank * 4,
282 m.bank = K8_MCE_THRESHOLD_BASE
296 struct threshold_attr {
297 struct attribute attr;
298 ssize_t (*show) (struct threshold_block *, char *);
299 ssize_t (*store) (struct threshold_block *, const char *, size_t count);
302 #define SHOW_FIELDS(name) \
303 static ssize_t show_ ## name(struct threshold_block *b, char *buf) \
305 return sprintf(buf, "%lx\n", (unsigned long) b->name); \
307 SHOW_FIELDS(interrupt_enable)
308 SHOW_FIELDS(threshold_limit)
311 store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size)
313 struct thresh_restart tr;
316 if (strict_strtoul(buf, 0, &new) < 0)
319 b->interrupt_enable = !!new;
321 memset(&tr, 0, sizeof(tr));
324 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
330 store_threshold_limit(struct threshold_block *b, const char *buf, size_t size)
332 struct thresh_restart tr;
335 if (strict_strtoul(buf, 0, &new) < 0)
338 if (new > THRESHOLD_MAX)
343 memset(&tr, 0, sizeof(tr));
344 tr.old_limit = b->threshold_limit;
345 b->threshold_limit = new;
348 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
353 struct threshold_block_cross_cpu {
354 struct threshold_block *tb;
358 static void local_error_count_handler(void *_tbcc)
360 struct threshold_block_cross_cpu *tbcc = _tbcc;
361 struct threshold_block *b = tbcc->tb;
364 rdmsr(b->address, low, high);
365 tbcc->retval = (high & 0xFFF) - (THRESHOLD_MAX - b->threshold_limit);
368 static ssize_t show_error_count(struct threshold_block *b, char *buf)
370 struct threshold_block_cross_cpu tbcc = { .tb = b, };
372 smp_call_function_single(b->cpu, local_error_count_handler, &tbcc, 1);
373 return sprintf(buf, "%lx\n", tbcc.retval);
376 static ssize_t store_error_count(struct threshold_block *b,
377 const char *buf, size_t count)
379 struct thresh_restart tr = { .b = b, .reset = 1, .old_limit = 0 };
381 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
385 #define RW_ATTR(val) \
386 static struct threshold_attr val = { \
387 .attr = {.name = __stringify(val), .mode = 0644 }, \
388 .show = show_## val, \
389 .store = store_## val, \
392 RW_ATTR(interrupt_enable);
393 RW_ATTR(threshold_limit);
394 RW_ATTR(error_count);
396 static struct attribute *default_attrs[] = {
397 &interrupt_enable.attr,
398 &threshold_limit.attr,
403 #define to_block(k) container_of(k, struct threshold_block, kobj)
404 #define to_attr(a) container_of(a, struct threshold_attr, attr)
406 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
408 struct threshold_block *b = to_block(kobj);
409 struct threshold_attr *a = to_attr(attr);
412 ret = a->show ? a->show(b, buf) : -EIO;
417 static ssize_t store(struct kobject *kobj, struct attribute *attr,
418 const char *buf, size_t count)
420 struct threshold_block *b = to_block(kobj);
421 struct threshold_attr *a = to_attr(attr);
424 ret = a->store ? a->store(b, buf, count) : -EIO;
429 static const struct sysfs_ops threshold_ops = {
434 static struct kobj_type threshold_ktype = {
435 .sysfs_ops = &threshold_ops,
436 .default_attrs = default_attrs,
439 static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
444 struct threshold_block *b = NULL;
448 if ((bank >= NR_BANKS) || (block >= NR_BLOCKS))
451 if (rdmsr_safe_on_cpu(cpu, address, &low, &high))
454 if (!(high & MASK_VALID_HI)) {
461 if (!(high & MASK_CNTP_HI) ||
462 (high & MASK_LOCKED_HI))
465 b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
472 b->address = address;
473 b->interrupt_enable = 0;
474 b->threshold_limit = THRESHOLD_MAX;
476 INIT_LIST_HEAD(&b->miscj);
478 if (per_cpu(threshold_banks, cpu)[bank]->blocks) {
480 &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
482 per_cpu(threshold_banks, cpu)[bank]->blocks = b;
485 err = kobject_init_and_add(&b->kobj, &threshold_ktype,
486 per_cpu(threshold_banks, cpu)[bank]->kobj,
492 address = (low & MASK_BLKPTR_LO) >> 21;
495 address += MCG_XBLK_ADDR;
500 err = allocate_threshold_blocks(cpu, bank, ++block, address);
505 kobject_uevent(&b->kobj, KOBJ_ADD);
511 kobject_put(&b->kobj);
517 static __cpuinit long
518 local_allocate_threshold_blocks(int cpu, unsigned int bank)
520 return allocate_threshold_blocks(cpu, bank, 0,
521 MSR_IA32_MC0_MISC + bank * 4);
524 /* symlinks sibling shared banks to first core. first core owns dir/files. */
525 static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
528 struct threshold_bank *b = NULL;
531 struct cpuinfo_x86 *c = &cpu_data(cpu);
534 sprintf(name, "threshold_bank%i", bank);
537 if (cpu_data(cpu).cpu_core_id && shared_bank[bank]) { /* symlink */
538 i = cpumask_first(c->llc_shared_map);
540 /* first core not up yet */
541 if (cpu_data(i).cpu_core_id)
545 if (per_cpu(threshold_banks, cpu)[bank])
548 b = per_cpu(threshold_banks, i)[bank];
553 err = sysfs_create_link(&per_cpu(mce_dev, cpu).kobj,
558 cpumask_copy(b->cpus, c->llc_shared_map);
559 per_cpu(threshold_banks, cpu)[bank] = b;
565 b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
570 if (!zalloc_cpumask_var(&b->cpus, GFP_KERNEL)) {
576 b->kobj = kobject_create_and_add(name, &per_cpu(mce_dev, cpu).kobj);
581 cpumask_setall(b->cpus);
583 cpumask_set_cpu(cpu, b->cpus);
586 per_cpu(threshold_banks, cpu)[bank] = b;
588 err = local_allocate_threshold_blocks(cpu, bank);
592 for_each_cpu(i, b->cpus) {
596 err = sysfs_create_link(&per_cpu(mce_dev, i).kobj,
601 per_cpu(threshold_banks, i)[bank] = b;
607 per_cpu(threshold_banks, cpu)[bank] = NULL;
608 free_cpumask_var(b->cpus);
614 /* create dir/files for all valid threshold banks */
615 static __cpuinit int threshold_create_device(unsigned int cpu)
620 for (bank = 0; bank < NR_BANKS; ++bank) {
621 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
623 err = threshold_create_bank(cpu, bank);
632 * let's be hotplug friendly.
633 * in case of multiple core processors, the first core always takes ownership
634 * of shared sysfs dir/files, and rest of the cores will be symlinked to it.
637 static void deallocate_threshold_block(unsigned int cpu,
640 struct threshold_block *pos = NULL;
641 struct threshold_block *tmp = NULL;
642 struct threshold_bank *head = per_cpu(threshold_banks, cpu)[bank];
647 list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
648 kobject_put(&pos->kobj);
649 list_del(&pos->miscj);
653 kfree(per_cpu(threshold_banks, cpu)[bank]->blocks);
654 per_cpu(threshold_banks, cpu)[bank]->blocks = NULL;
657 static void threshold_remove_bank(unsigned int cpu, int bank)
659 struct threshold_bank *b;
663 b = per_cpu(threshold_banks, cpu)[bank];
669 sprintf(name, "threshold_bank%i", bank);
672 /* sibling symlink */
673 if (shared_bank[bank] && b->blocks->cpu != cpu) {
674 sysfs_remove_link(&per_cpu(mce_dev, cpu).kobj, name);
675 per_cpu(threshold_banks, cpu)[bank] = NULL;
681 /* remove all sibling symlinks before unregistering */
682 for_each_cpu(i, b->cpus) {
686 sysfs_remove_link(&per_cpu(mce_dev, i).kobj, name);
687 per_cpu(threshold_banks, i)[bank] = NULL;
690 deallocate_threshold_block(cpu, bank);
693 kobject_del(b->kobj);
694 kobject_put(b->kobj);
695 free_cpumask_var(b->cpus);
697 per_cpu(threshold_banks, cpu)[bank] = NULL;
700 static void threshold_remove_device(unsigned int cpu)
704 for (bank = 0; bank < NR_BANKS; ++bank) {
705 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
707 threshold_remove_bank(cpu, bank);
711 /* get notified when a cpu comes on/off */
712 static void __cpuinit
713 amd_64_threshold_cpu_callback(unsigned long action, unsigned int cpu)
717 case CPU_ONLINE_FROZEN:
718 threshold_create_device(cpu);
721 case CPU_DEAD_FROZEN:
722 threshold_remove_device(cpu);
729 static __init int threshold_init_device(void)
733 /* to hit CPUs online before the notifier is up */
734 for_each_online_cpu(lcpu) {
735 int err = threshold_create_device(lcpu);
740 threshold_cpu_callback = amd_64_threshold_cpu_callback;
744 device_initcall(threshold_init_device);