Merge branch 'linus' into x86/apic
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / cpu / mcheck / mce_amd_64.c
blob9817506dd4698c6578646f04100836dd6ad435c8
1 /*
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
11 * April 2006
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>
27 #include <asm/apic.h>
28 #include <asm/mce.h>
29 #include <asm/msr.h>
30 #include <asm/percpu.h>
31 #include <asm/idle.h>
33 #define PFX "mce_threshold: "
34 #define VERSION "version 1.1.1"
35 #define NR_BANKS 6
36 #define NR_BLOCKS 9
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 {
51 unsigned int block;
52 unsigned int bank;
53 unsigned int cpu;
54 u32 address;
55 u16 interrupt_enable;
56 u16 threshold_limit;
57 struct kobject kobj;
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 {
68 struct kobject *kobj;
69 struct threshold_block *blocks;
70 cpumask_var_t cpus;
72 static DEFINE_PER_CPU(struct threshold_bank *, threshold_banks[NR_BANKS]);
74 #ifdef CONFIG_SMP
75 static unsigned char shared_bank[NR_BANKS] = {
76 0, 0, 0, 0, 1
78 #endif
80 static DEFINE_PER_CPU(unsigned char, bank_map); /* see which banks are on */
83 * CPU Initialization
86 struct thresh_restart {
87 struct threshold_block *b;
88 int reset;
89 u16 old_limit;
92 /* must be called with correct cpu affinity */
93 static long threshold_restart_bank(void *_tr)
95 struct thresh_restart *tr = _tr;
96 u32 mci_misc_hi, mci_misc_lo;
98 rdmsr(tr->b->address, mci_misc_lo, mci_misc_hi);
100 if (tr->b->threshold_limit < (mci_misc_hi & THRESHOLD_MAX))
101 tr->reset = 1; /* limit cannot be lower than err count */
103 if (tr->reset) { /* reset err count and overflow bit */
104 mci_misc_hi =
105 (mci_misc_hi & ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI)) |
106 (THRESHOLD_MAX - tr->b->threshold_limit);
107 } else if (tr->old_limit) { /* change limit w/o reset */
108 int new_count = (mci_misc_hi & THRESHOLD_MAX) +
109 (tr->old_limit - tr->b->threshold_limit);
110 mci_misc_hi = (mci_misc_hi & ~MASK_ERR_COUNT_HI) |
111 (new_count & THRESHOLD_MAX);
114 tr->b->interrupt_enable ?
115 (mci_misc_hi = (mci_misc_hi & ~MASK_INT_TYPE_HI) | INT_TYPE_APIC) :
116 (mci_misc_hi &= ~MASK_INT_TYPE_HI);
118 mci_misc_hi |= MASK_COUNT_EN_HI;
119 wrmsr(tr->b->address, mci_misc_lo, mci_misc_hi);
120 return 0;
123 /* cpu init entry point, called from mce.c with preempt off */
124 void mce_amd_feature_init(struct cpuinfo_x86 *c)
126 unsigned int bank, block;
127 unsigned int cpu = smp_processor_id();
128 u8 lvt_off;
129 u32 low = 0, high = 0, address = 0;
130 struct thresh_restart tr;
132 for (bank = 0; bank < NR_BANKS; ++bank) {
133 for (block = 0; block < NR_BLOCKS; ++block) {
134 if (block == 0)
135 address = MSR_IA32_MC0_MISC + bank * 4;
136 else if (block == 1) {
137 address = (low & MASK_BLKPTR_LO) >> 21;
138 if (!address)
139 break;
140 address += MCG_XBLK_ADDR;
142 else
143 ++address;
145 if (rdmsr_safe(address, &low, &high))
146 break;
148 if (!(high & MASK_VALID_HI)) {
149 if (block)
150 continue;
151 else
152 break;
155 if (!(high & MASK_CNTP_HI) ||
156 (high & MASK_LOCKED_HI))
157 continue;
159 if (!block)
160 per_cpu(bank_map, cpu) |= (1 << bank);
161 #ifdef CONFIG_SMP
162 if (shared_bank[bank] && c->cpu_core_id)
163 break;
164 #endif
165 lvt_off = setup_APIC_eilvt_mce(THRESHOLD_APIC_VECTOR,
166 APIC_EILVT_MSG_FIX, 0);
168 high &= ~MASK_LVTOFF_HI;
169 high |= lvt_off << 20;
170 wrmsr(address, low, high);
172 threshold_defaults.address = address;
173 tr.b = &threshold_defaults;
174 tr.reset = 0;
175 tr.old_limit = 0;
176 threshold_restart_bank(&tr);
182 * APIC Interrupt Handler
186 * threshold interrupt handler will service THRESHOLD_APIC_VECTOR.
187 * the interrupt goes off when error_count reaches threshold_limit.
188 * the handler will simply log mcelog w/ software defined bank number.
190 asmlinkage void mce_threshold_interrupt(void)
192 unsigned int bank, block;
193 struct mce m;
194 u32 low = 0, high = 0, address = 0;
196 ack_APIC_irq();
197 exit_idle();
198 irq_enter();
200 memset(&m, 0, sizeof(m));
201 rdtscll(m.tsc);
202 m.cpu = smp_processor_id();
204 /* assume first bank caused it */
205 for (bank = 0; bank < NR_BANKS; ++bank) {
206 if (!(per_cpu(bank_map, m.cpu) & (1 << bank)))
207 continue;
208 for (block = 0; block < NR_BLOCKS; ++block) {
209 if (block == 0)
210 address = MSR_IA32_MC0_MISC + bank * 4;
211 else if (block == 1) {
212 address = (low & MASK_BLKPTR_LO) >> 21;
213 if (!address)
214 break;
215 address += MCG_XBLK_ADDR;
217 else
218 ++address;
220 if (rdmsr_safe(address, &low, &high))
221 break;
223 if (!(high & MASK_VALID_HI)) {
224 if (block)
225 continue;
226 else
227 break;
230 if (!(high & MASK_CNTP_HI) ||
231 (high & MASK_LOCKED_HI))
232 continue;
234 /* Log the machine check that caused the threshold
235 event. */
236 do_machine_check(NULL, 0);
238 if (high & MASK_OVERFLOW_HI) {
239 rdmsrl(address, m.misc);
240 rdmsrl(MSR_IA32_MC0_STATUS + bank * 4,
241 m.status);
242 m.bank = K8_MCE_THRESHOLD_BASE
243 + bank * NR_BLOCKS
244 + block;
245 mce_log(&m);
246 goto out;
250 out:
251 inc_irq_stat(irq_threshold_count);
252 irq_exit();
256 * Sysfs Interface
259 struct threshold_attr {
260 struct attribute attr;
261 ssize_t(*show) (struct threshold_block *, char *);
262 ssize_t(*store) (struct threshold_block *, const char *, size_t count);
265 #define SHOW_FIELDS(name) \
266 static ssize_t show_ ## name(struct threshold_block * b, char *buf) \
268 return sprintf(buf, "%lx\n", (unsigned long) b->name); \
270 SHOW_FIELDS(interrupt_enable)
271 SHOW_FIELDS(threshold_limit)
273 static ssize_t store_interrupt_enable(struct threshold_block *b,
274 const char *buf, size_t count)
276 char *end;
277 struct thresh_restart tr;
278 unsigned long new = simple_strtoul(buf, &end, 0);
279 if (end == buf)
280 return -EINVAL;
281 b->interrupt_enable = !!new;
283 tr.b = b;
284 tr.reset = 0;
285 tr.old_limit = 0;
286 work_on_cpu(b->cpu, threshold_restart_bank, &tr);
288 return end - buf;
291 static ssize_t store_threshold_limit(struct threshold_block *b,
292 const char *buf, size_t count)
294 char *end;
295 struct thresh_restart tr;
296 unsigned long new = simple_strtoul(buf, &end, 0);
297 if (end == buf)
298 return -EINVAL;
299 if (new > THRESHOLD_MAX)
300 new = THRESHOLD_MAX;
301 if (new < 1)
302 new = 1;
303 tr.old_limit = b->threshold_limit;
304 b->threshold_limit = new;
305 tr.b = b;
306 tr.reset = 0;
308 work_on_cpu(b->cpu, threshold_restart_bank, &tr);
310 return end - buf;
313 static long local_error_count(void *_b)
315 struct threshold_block *b = _b;
316 u32 low, high;
318 rdmsr(b->address, low, high);
319 return (high & 0xFFF) - (THRESHOLD_MAX - b->threshold_limit);
322 static ssize_t show_error_count(struct threshold_block *b, char *buf)
324 return sprintf(buf, "%lx\n", work_on_cpu(b->cpu, local_error_count, b));
327 static ssize_t store_error_count(struct threshold_block *b,
328 const char *buf, size_t count)
330 struct thresh_restart tr = { .b = b, .reset = 1, .old_limit = 0 };
332 work_on_cpu(b->cpu, threshold_restart_bank, &tr);
333 return 1;
336 #define THRESHOLD_ATTR(_name,_mode,_show,_store) { \
337 .attr = {.name = __stringify(_name), .mode = _mode }, \
338 .show = _show, \
339 .store = _store, \
342 #define RW_ATTR(name) \
343 static struct threshold_attr name = \
344 THRESHOLD_ATTR(name, 0644, show_## name, store_## name)
346 RW_ATTR(interrupt_enable);
347 RW_ATTR(threshold_limit);
348 RW_ATTR(error_count);
350 static struct attribute *default_attrs[] = {
351 &interrupt_enable.attr,
352 &threshold_limit.attr,
353 &error_count.attr,
354 NULL
357 #define to_block(k) container_of(k, struct threshold_block, kobj)
358 #define to_attr(a) container_of(a, struct threshold_attr, attr)
360 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
362 struct threshold_block *b = to_block(kobj);
363 struct threshold_attr *a = to_attr(attr);
364 ssize_t ret;
365 ret = a->show ? a->show(b, buf) : -EIO;
366 return ret;
369 static ssize_t store(struct kobject *kobj, struct attribute *attr,
370 const char *buf, size_t count)
372 struct threshold_block *b = to_block(kobj);
373 struct threshold_attr *a = to_attr(attr);
374 ssize_t ret;
375 ret = a->store ? a->store(b, buf, count) : -EIO;
376 return ret;
379 static struct sysfs_ops threshold_ops = {
380 .show = show,
381 .store = store,
384 static struct kobj_type threshold_ktype = {
385 .sysfs_ops = &threshold_ops,
386 .default_attrs = default_attrs,
389 static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
390 unsigned int bank,
391 unsigned int block,
392 u32 address)
394 int err;
395 u32 low, high;
396 struct threshold_block *b = NULL;
398 if ((bank >= NR_BANKS) || (block >= NR_BLOCKS))
399 return 0;
401 if (rdmsr_safe(address, &low, &high))
402 return 0;
404 if (!(high & MASK_VALID_HI)) {
405 if (block)
406 goto recurse;
407 else
408 return 0;
411 if (!(high & MASK_CNTP_HI) ||
412 (high & MASK_LOCKED_HI))
413 goto recurse;
415 b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
416 if (!b)
417 return -ENOMEM;
419 b->block = block;
420 b->bank = bank;
421 b->cpu = cpu;
422 b->address = address;
423 b->interrupt_enable = 0;
424 b->threshold_limit = THRESHOLD_MAX;
426 INIT_LIST_HEAD(&b->miscj);
428 if (per_cpu(threshold_banks, cpu)[bank]->blocks)
429 list_add(&b->miscj,
430 &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
431 else
432 per_cpu(threshold_banks, cpu)[bank]->blocks = b;
434 err = kobject_init_and_add(&b->kobj, &threshold_ktype,
435 per_cpu(threshold_banks, cpu)[bank]->kobj,
436 "misc%i", block);
437 if (err)
438 goto out_free;
439 recurse:
440 if (!block) {
441 address = (low & MASK_BLKPTR_LO) >> 21;
442 if (!address)
443 return 0;
444 address += MCG_XBLK_ADDR;
445 } else
446 ++address;
448 err = allocate_threshold_blocks(cpu, bank, ++block, address);
449 if (err)
450 goto out_free;
452 if (b)
453 kobject_uevent(&b->kobj, KOBJ_ADD);
455 return err;
457 out_free:
458 if (b) {
459 kobject_put(&b->kobj);
460 kfree(b);
462 return err;
465 static __cpuinit long local_allocate_threshold_blocks(void *_bank)
467 unsigned int *bank = _bank;
469 return allocate_threshold_blocks(smp_processor_id(), *bank, 0,
470 MSR_IA32_MC0_MISC + *bank * 4);
473 /* symlinks sibling shared banks to first core. first core owns dir/files. */
474 static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
476 int i, err = 0;
477 struct threshold_bank *b = NULL;
478 char name[32];
480 sprintf(name, "threshold_bank%i", bank);
482 #ifdef CONFIG_SMP
483 if (cpu_data(cpu).cpu_core_id && shared_bank[bank]) { /* symlink */
484 i = cpumask_first(&per_cpu(cpu_core_map, cpu));
486 /* first core not up yet */
487 if (cpu_data(i).cpu_core_id)
488 goto out;
490 /* already linked */
491 if (per_cpu(threshold_banks, cpu)[bank])
492 goto out;
494 b = per_cpu(threshold_banks, i)[bank];
496 if (!b)
497 goto out;
499 err = sysfs_create_link(&per_cpu(device_mce, cpu).kobj,
500 b->kobj, name);
501 if (err)
502 goto out;
504 cpumask_copy(b->cpus, &per_cpu(cpu_core_map, cpu));
505 per_cpu(threshold_banks, cpu)[bank] = b;
506 goto out;
508 #endif
510 b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
511 if (!b) {
512 err = -ENOMEM;
513 goto out;
515 if (!alloc_cpumask_var(&b->cpus, GFP_KERNEL)) {
516 kfree(b);
517 err = -ENOMEM;
518 goto out;
521 b->kobj = kobject_create_and_add(name, &per_cpu(device_mce, cpu).kobj);
522 if (!b->kobj)
523 goto out_free;
525 #ifndef CONFIG_SMP
526 cpumask_setall(b->cpus);
527 #else
528 cpumask_copy(b->cpus, &per_cpu(cpu_core_map, cpu));
529 #endif
531 per_cpu(threshold_banks, cpu)[bank] = b;
533 err = work_on_cpu(cpu, local_allocate_threshold_blocks, &bank);
534 if (err)
535 goto out_free;
537 for_each_cpu(i, b->cpus) {
538 if (i == cpu)
539 continue;
541 err = sysfs_create_link(&per_cpu(device_mce, i).kobj,
542 b->kobj, name);
543 if (err)
544 goto out;
546 per_cpu(threshold_banks, i)[bank] = b;
549 goto out;
551 out_free:
552 per_cpu(threshold_banks, cpu)[bank] = NULL;
553 free_cpumask_var(b->cpus);
554 kfree(b);
555 out:
556 return err;
559 /* create dir/files for all valid threshold banks */
560 static __cpuinit int threshold_create_device(unsigned int cpu)
562 unsigned int bank;
563 int err = 0;
565 for (bank = 0; bank < NR_BANKS; ++bank) {
566 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
567 continue;
568 err = threshold_create_bank(cpu, bank);
569 if (err)
570 goto out;
572 out:
573 return err;
577 * let's be hotplug friendly.
578 * in case of multiple core processors, the first core always takes ownership
579 * of shared sysfs dir/files, and rest of the cores will be symlinked to it.
582 static void deallocate_threshold_block(unsigned int cpu,
583 unsigned int bank)
585 struct threshold_block *pos = NULL;
586 struct threshold_block *tmp = NULL;
587 struct threshold_bank *head = per_cpu(threshold_banks, cpu)[bank];
589 if (!head)
590 return;
592 list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
593 kobject_put(&pos->kobj);
594 list_del(&pos->miscj);
595 kfree(pos);
598 kfree(per_cpu(threshold_banks, cpu)[bank]->blocks);
599 per_cpu(threshold_banks, cpu)[bank]->blocks = NULL;
602 static void threshold_remove_bank(unsigned int cpu, int bank)
604 int i = 0;
605 struct threshold_bank *b;
606 char name[32];
608 b = per_cpu(threshold_banks, cpu)[bank];
610 if (!b)
611 return;
613 if (!b->blocks)
614 goto free_out;
616 sprintf(name, "threshold_bank%i", bank);
618 #ifdef CONFIG_SMP
619 /* sibling symlink */
620 if (shared_bank[bank] && b->blocks->cpu != cpu) {
621 sysfs_remove_link(&per_cpu(device_mce, cpu).kobj, name);
622 per_cpu(threshold_banks, cpu)[bank] = NULL;
623 return;
625 #endif
627 /* remove all sibling symlinks before unregistering */
628 for_each_cpu(i, b->cpus) {
629 if (i == cpu)
630 continue;
632 sysfs_remove_link(&per_cpu(device_mce, i).kobj, name);
633 per_cpu(threshold_banks, i)[bank] = NULL;
636 deallocate_threshold_block(cpu, bank);
638 free_out:
639 kobject_del(b->kobj);
640 kobject_put(b->kobj);
641 free_cpumask_var(b->cpus);
642 kfree(b);
643 per_cpu(threshold_banks, cpu)[bank] = NULL;
646 static void threshold_remove_device(unsigned int cpu)
648 unsigned int bank;
650 for (bank = 0; bank < NR_BANKS; ++bank) {
651 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
652 continue;
653 threshold_remove_bank(cpu, bank);
657 /* get notified when a cpu comes on/off */
658 static void __cpuinit amd_64_threshold_cpu_callback(unsigned long action,
659 unsigned int cpu)
661 if (cpu >= NR_CPUS)
662 return;
664 switch (action) {
665 case CPU_ONLINE:
666 case CPU_ONLINE_FROZEN:
667 threshold_create_device(cpu);
668 break;
669 case CPU_DEAD:
670 case CPU_DEAD_FROZEN:
671 threshold_remove_device(cpu);
672 break;
673 default:
674 break;
678 static __init int threshold_init_device(void)
680 unsigned lcpu = 0;
682 /* to hit CPUs online before the notifier is up */
683 for_each_online_cpu(lcpu) {
684 int err = threshold_create_device(lcpu);
685 if (err)
686 return err;
688 threshold_cpu_callback = amd_64_threshold_cpu_callback;
689 return 0;
692 device_initcall(threshold_init_device);