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/errno.h>
21 #include <linux/sched.h>
22 #include <linux/sysfs.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <linux/cpu.h>
26 #include <linux/smp.h>
35 #define THRESHOLD_MAX 0xFFF
36 #define INT_TYPE_APIC 0x00020000
37 #define MASK_VALID_HI 0x80000000
38 #define MASK_CNTP_HI 0x40000000
39 #define MASK_LOCKED_HI 0x20000000
40 #define MASK_LVTOFF_HI 0x00F00000
41 #define MASK_COUNT_EN_HI 0x00080000
42 #define MASK_INT_TYPE_HI 0x00060000
43 #define MASK_OVERFLOW_HI 0x00010000
44 #define MASK_ERR_COUNT_HI 0x00000FFF
45 #define MASK_BLKPTR_LO 0xFF000000
46 #define MCG_XBLK_ADDR 0xC0000400
48 struct threshold_block {
54 bool interrupt_capable;
57 struct list_head miscj;
60 struct threshold_bank {
62 struct threshold_block *blocks;
64 static DEFINE_PER_CPU(struct threshold_bank * [NR_BANKS], threshold_banks);
66 static unsigned char shared_bank[NR_BANKS] = {
70 static DEFINE_PER_CPU(unsigned char, bank_map); /* see which banks are on */
72 static void amd_threshold_interrupt(void);
78 struct thresh_restart {
79 struct threshold_block *b;
86 static bool lvt_interrupt_supported(unsigned int bank, u32 msr_high_bits)
89 * bank 4 supports APIC LVT interrupts implicitly since forever.
95 * IntP: interrupt present; if this bit is set, the thresholding
96 * bank can generate APIC LVT interrupts
98 return msr_high_bits & BIT(28);
101 static int lvt_off_valid(struct threshold_block *b, int apic, u32 lo, u32 hi)
103 int msr = (hi & MASK_LVTOFF_HI) >> 20;
106 pr_err(FW_BUG "cpu %d, failed to setup threshold interrupt "
107 "for bank %d, block %d (MSR%08X=0x%x%08x)\n", b->cpu,
108 b->bank, b->block, b->address, hi, lo);
113 pr_err(FW_BUG "cpu %d, invalid threshold interrupt offset %d "
114 "for bank %d, block %d (MSR%08X=0x%x%08x)\n",
115 b->cpu, apic, b->bank, b->block, b->address, hi, lo);
123 * Called via smp_call_function_single(), must be called with correct
126 static void threshold_restart_bank(void *_tr)
128 struct thresh_restart *tr = _tr;
131 rdmsr(tr->b->address, lo, hi);
133 if (tr->b->threshold_limit < (hi & THRESHOLD_MAX))
134 tr->reset = 1; /* limit cannot be lower than err count */
136 if (tr->reset) { /* reset err count and overflow bit */
138 (hi & ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI)) |
139 (THRESHOLD_MAX - tr->b->threshold_limit);
140 } else if (tr->old_limit) { /* change limit w/o reset */
141 int new_count = (hi & THRESHOLD_MAX) +
142 (tr->old_limit - tr->b->threshold_limit);
144 hi = (hi & ~MASK_ERR_COUNT_HI) |
145 (new_count & THRESHOLD_MAX);
149 hi &= ~MASK_INT_TYPE_HI;
151 if (!tr->b->interrupt_capable)
154 if (tr->set_lvt_off) {
155 if (lvt_off_valid(tr->b, tr->lvt_off, lo, hi)) {
156 /* set new lvt offset */
157 hi &= ~MASK_LVTOFF_HI;
158 hi |= tr->lvt_off << 20;
162 if (tr->b->interrupt_enable)
167 hi |= MASK_COUNT_EN_HI;
168 wrmsr(tr->b->address, lo, hi);
171 static void mce_threshold_block_init(struct threshold_block *b, int offset)
173 struct thresh_restart tr = {
179 b->threshold_limit = THRESHOLD_MAX;
180 threshold_restart_bank(&tr);
183 static int setup_APIC_mce(int reserved, int new)
185 if (reserved < 0 && !setup_APIC_eilvt(new, THRESHOLD_APIC_VECTOR,
186 APIC_EILVT_MSG_FIX, 0))
192 /* cpu init entry point, called from mce.c with preempt off */
193 void mce_amd_feature_init(struct cpuinfo_x86 *c)
195 struct threshold_block b;
196 unsigned int cpu = smp_processor_id();
197 u32 low = 0, high = 0, address = 0;
198 unsigned int bank, block;
201 for (bank = 0; bank < NR_BANKS; ++bank) {
202 for (block = 0; block < NR_BLOCKS; ++block) {
204 address = MSR_IA32_MC0_MISC + bank * 4;
205 else if (block == 1) {
206 address = (low & MASK_BLKPTR_LO) >> 21;
210 address += MCG_XBLK_ADDR;
214 if (rdmsr_safe(address, &low, &high))
217 if (!(high & MASK_VALID_HI))
220 if (!(high & MASK_CNTP_HI) ||
221 (high & MASK_LOCKED_HI))
225 per_cpu(bank_map, cpu) |= (1 << bank);
227 memset(&b, 0, sizeof(b));
232 b.interrupt_capable = lvt_interrupt_supported(bank, high);
234 if (b.interrupt_capable) {
235 int new = (high & MASK_LVTOFF_HI) >> 20;
236 offset = setup_APIC_mce(offset, new);
239 mce_threshold_block_init(&b, offset);
240 mce_threshold_vector = amd_threshold_interrupt;
246 * APIC Interrupt Handler
250 * threshold interrupt handler will service THRESHOLD_APIC_VECTOR.
251 * the interrupt goes off when error_count reaches threshold_limit.
252 * the handler will simply log mcelog w/ software defined bank number.
254 static void amd_threshold_interrupt(void)
256 u32 low = 0, high = 0, address = 0;
257 unsigned int bank, block;
262 /* assume first bank caused it */
263 for (bank = 0; bank < NR_BANKS; ++bank) {
264 if (!(per_cpu(bank_map, m.cpu) & (1 << bank)))
266 for (block = 0; block < NR_BLOCKS; ++block) {
268 address = MSR_IA32_MC0_MISC + bank * 4;
269 } else if (block == 1) {
270 address = (low & MASK_BLKPTR_LO) >> 21;
273 address += MCG_XBLK_ADDR;
278 if (rdmsr_safe(address, &low, &high))
281 if (!(high & MASK_VALID_HI)) {
288 if (!(high & MASK_CNTP_HI) ||
289 (high & MASK_LOCKED_HI))
293 * Log the machine check that caused the threshold
296 machine_check_poll(MCP_TIMESTAMP,
297 &__get_cpu_var(mce_poll_banks));
299 if (high & MASK_OVERFLOW_HI) {
300 rdmsrl(address, m.misc);
301 rdmsrl(MSR_IA32_MC0_STATUS + bank * 4,
303 m.bank = K8_MCE_THRESHOLD_BASE
317 struct threshold_attr {
318 struct attribute attr;
319 ssize_t (*show) (struct threshold_block *, char *);
320 ssize_t (*store) (struct threshold_block *, const char *, size_t count);
323 #define SHOW_FIELDS(name) \
324 static ssize_t show_ ## name(struct threshold_block *b, char *buf) \
326 return sprintf(buf, "%lx\n", (unsigned long) b->name); \
328 SHOW_FIELDS(interrupt_enable)
329 SHOW_FIELDS(threshold_limit)
332 store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size)
334 struct thresh_restart tr;
337 if (!b->interrupt_capable)
340 if (strict_strtoul(buf, 0, &new) < 0)
343 b->interrupt_enable = !!new;
345 memset(&tr, 0, sizeof(tr));
348 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
354 store_threshold_limit(struct threshold_block *b, const char *buf, size_t size)
356 struct thresh_restart tr;
359 if (strict_strtoul(buf, 0, &new) < 0)
362 if (new > THRESHOLD_MAX)
367 memset(&tr, 0, sizeof(tr));
368 tr.old_limit = b->threshold_limit;
369 b->threshold_limit = new;
372 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
377 struct threshold_block_cross_cpu {
378 struct threshold_block *tb;
382 static void local_error_count_handler(void *_tbcc)
384 struct threshold_block_cross_cpu *tbcc = _tbcc;
385 struct threshold_block *b = tbcc->tb;
388 rdmsr(b->address, low, high);
389 tbcc->retval = (high & 0xFFF) - (THRESHOLD_MAX - b->threshold_limit);
392 static ssize_t show_error_count(struct threshold_block *b, char *buf)
394 struct threshold_block_cross_cpu tbcc = { .tb = b, };
396 smp_call_function_single(b->cpu, local_error_count_handler, &tbcc, 1);
397 return sprintf(buf, "%lx\n", tbcc.retval);
400 static ssize_t store_error_count(struct threshold_block *b,
401 const char *buf, size_t count)
403 struct thresh_restart tr = { .b = b, .reset = 1, .old_limit = 0 };
405 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
409 #define RW_ATTR(val) \
410 static struct threshold_attr val = { \
411 .attr = {.name = __stringify(val), .mode = 0644 }, \
412 .show = show_## val, \
413 .store = store_## val, \
416 RW_ATTR(interrupt_enable);
417 RW_ATTR(threshold_limit);
418 RW_ATTR(error_count);
420 static struct attribute *default_attrs[] = {
421 &threshold_limit.attr,
423 NULL, /* possibly interrupt_enable if supported, see below */
427 #define to_block(k) container_of(k, struct threshold_block, kobj)
428 #define to_attr(a) container_of(a, struct threshold_attr, attr)
430 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
432 struct threshold_block *b = to_block(kobj);
433 struct threshold_attr *a = to_attr(attr);
436 ret = a->show ? a->show(b, buf) : -EIO;
441 static ssize_t store(struct kobject *kobj, struct attribute *attr,
442 const char *buf, size_t count)
444 struct threshold_block *b = to_block(kobj);
445 struct threshold_attr *a = to_attr(attr);
448 ret = a->store ? a->store(b, buf, count) : -EIO;
453 static const struct sysfs_ops threshold_ops = {
458 static struct kobj_type threshold_ktype = {
459 .sysfs_ops = &threshold_ops,
460 .default_attrs = default_attrs,
463 static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
468 struct threshold_block *b = NULL;
472 if ((bank >= NR_BANKS) || (block >= NR_BLOCKS))
475 if (rdmsr_safe_on_cpu(cpu, address, &low, &high))
478 if (!(high & MASK_VALID_HI)) {
485 if (!(high & MASK_CNTP_HI) ||
486 (high & MASK_LOCKED_HI))
489 b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
496 b->address = address;
497 b->interrupt_enable = 0;
498 b->interrupt_capable = lvt_interrupt_supported(bank, high);
499 b->threshold_limit = THRESHOLD_MAX;
501 if (b->interrupt_capable)
502 threshold_ktype.default_attrs[2] = &interrupt_enable.attr;
504 threshold_ktype.default_attrs[2] = NULL;
506 INIT_LIST_HEAD(&b->miscj);
508 if (per_cpu(threshold_banks, cpu)[bank]->blocks) {
510 &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
512 per_cpu(threshold_banks, cpu)[bank]->blocks = b;
515 err = kobject_init_and_add(&b->kobj, &threshold_ktype,
516 per_cpu(threshold_banks, cpu)[bank]->kobj,
522 address = (low & MASK_BLKPTR_LO) >> 21;
525 address += MCG_XBLK_ADDR;
530 err = allocate_threshold_blocks(cpu, bank, ++block, address);
535 kobject_uevent(&b->kobj, KOBJ_ADD);
541 kobject_put(&b->kobj);
548 static __cpuinit long
549 local_allocate_threshold_blocks(int cpu, unsigned int bank)
551 return allocate_threshold_blocks(cpu, bank, 0,
552 MSR_IA32_MC0_MISC + bank * 4);
555 static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
557 struct device *dev = per_cpu(mce_device, cpu);
558 struct threshold_bank *b = NULL;
562 sprintf(name, "threshold_bank%i", bank);
564 b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
570 b->kobj = kobject_create_and_add(name, &dev->kobj);
576 per_cpu(threshold_banks, cpu)[bank] = b;
578 err = local_allocate_threshold_blocks(cpu, bank);
583 per_cpu(threshold_banks, cpu)[bank] = NULL;
589 /* create dir/files for all valid threshold banks */
590 static __cpuinit int threshold_create_device(unsigned int cpu)
595 for (bank = 0; bank < NR_BANKS; ++bank) {
596 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
598 err = threshold_create_bank(cpu, bank);
606 static void deallocate_threshold_block(unsigned int cpu,
609 struct threshold_block *pos = NULL;
610 struct threshold_block *tmp = NULL;
611 struct threshold_bank *head = per_cpu(threshold_banks, cpu)[bank];
616 list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
617 kobject_put(&pos->kobj);
618 list_del(&pos->miscj);
622 kfree(per_cpu(threshold_banks, cpu)[bank]->blocks);
623 per_cpu(threshold_banks, cpu)[bank]->blocks = NULL;
626 static void threshold_remove_bank(unsigned int cpu, int bank)
628 struct threshold_bank *b;
630 b = per_cpu(threshold_banks, cpu)[bank];
636 deallocate_threshold_block(cpu, bank);
639 kobject_del(b->kobj);
640 kobject_put(b->kobj);
642 per_cpu(threshold_banks, cpu)[bank] = NULL;
645 static void threshold_remove_device(unsigned int cpu)
649 for (bank = 0; bank < NR_BANKS; ++bank) {
650 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
652 threshold_remove_bank(cpu, bank);
656 /* get notified when a cpu comes on/off */
657 static void __cpuinit
658 amd_64_threshold_cpu_callback(unsigned long action, unsigned int cpu)
662 case CPU_ONLINE_FROZEN:
663 threshold_create_device(cpu);
666 case CPU_DEAD_FROZEN:
667 threshold_remove_device(cpu);
674 static __init int threshold_init_device(void)
678 /* to hit CPUs online before the notifier is up */
679 for_each_online_cpu(lcpu) {
680 int err = threshold_create_device(lcpu);
685 threshold_cpu_callback = amd_64_threshold_cpu_callback;
689 device_initcall(threshold_init_device);