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/slab.h>
25 #include <linux/init.h>
26 #include <linux/cpu.h>
27 #include <linux/smp.h>
34 #define PFX "mce_threshold: "
35 #define VERSION "version 1.1.1"
38 #define THRESHOLD_MAX 0xFFF
39 #define INT_TYPE_APIC 0x00020000
40 #define MASK_VALID_HI 0x80000000
41 #define MASK_CNTP_HI 0x40000000
42 #define MASK_LOCKED_HI 0x20000000
43 #define MASK_LVTOFF_HI 0x00F00000
44 #define MASK_COUNT_EN_HI 0x00080000
45 #define MASK_INT_TYPE_HI 0x00060000
46 #define MASK_OVERFLOW_HI 0x00010000
47 #define MASK_ERR_COUNT_HI 0x00000FFF
48 #define MASK_BLKPTR_LO 0xFF000000
49 #define MCG_XBLK_ADDR 0xC0000400
51 struct threshold_block
{
59 struct list_head miscj
;
62 /* defaults used early on boot */
63 static struct threshold_block threshold_defaults
= {
64 .interrupt_enable
= 0,
65 .threshold_limit
= THRESHOLD_MAX
,
68 struct threshold_bank
{
70 struct threshold_block
*blocks
;
73 static DEFINE_PER_CPU(struct threshold_bank
* [NR_BANKS
], threshold_banks
);
76 static unsigned char shared_bank
[NR_BANKS
] = {
81 static DEFINE_PER_CPU(unsigned char, bank_map
); /* see which banks are on */
83 static void amd_threshold_interrupt(void);
89 struct thresh_restart
{
90 struct threshold_block
*b
;
95 /* must be called with correct cpu affinity */
96 /* Called via smp_call_function_single() */
97 static void threshold_restart_bank(void *_tr
)
99 struct thresh_restart
*tr
= _tr
;
100 u32 mci_misc_hi
, mci_misc_lo
;
102 rdmsr(tr
->b
->address
, mci_misc_lo
, mci_misc_hi
);
104 if (tr
->b
->threshold_limit
< (mci_misc_hi
& THRESHOLD_MAX
))
105 tr
->reset
= 1; /* limit cannot be lower than err count */
107 if (tr
->reset
) { /* reset err count and overflow bit */
109 (mci_misc_hi
& ~(MASK_ERR_COUNT_HI
| MASK_OVERFLOW_HI
)) |
110 (THRESHOLD_MAX
- tr
->b
->threshold_limit
);
111 } else if (tr
->old_limit
) { /* change limit w/o reset */
112 int new_count
= (mci_misc_hi
& THRESHOLD_MAX
) +
113 (tr
->old_limit
- tr
->b
->threshold_limit
);
115 mci_misc_hi
= (mci_misc_hi
& ~MASK_ERR_COUNT_HI
) |
116 (new_count
& THRESHOLD_MAX
);
119 tr
->b
->interrupt_enable
?
120 (mci_misc_hi
= (mci_misc_hi
& ~MASK_INT_TYPE_HI
) | INT_TYPE_APIC
) :
121 (mci_misc_hi
&= ~MASK_INT_TYPE_HI
);
123 mci_misc_hi
|= MASK_COUNT_EN_HI
;
124 wrmsr(tr
->b
->address
, mci_misc_lo
, mci_misc_hi
);
127 /* cpu init entry point, called from mce.c with preempt off */
128 void mce_amd_feature_init(struct cpuinfo_x86
*c
)
130 unsigned int cpu
= smp_processor_id();
131 u32 low
= 0, high
= 0, address
= 0;
132 unsigned int bank
, block
;
133 struct thresh_restart tr
;
137 for (bank
= 0; bank
< NR_BANKS
; ++bank
) {
138 for (block
= 0; block
< NR_BLOCKS
; ++block
) {
140 address
= MSR_IA32_MC0_MISC
+ bank
* 4;
141 else if (block
== 1) {
142 address
= (low
& MASK_BLKPTR_LO
) >> 21;
146 address
+= MCG_XBLK_ADDR
;
150 if (rdmsr_safe(address
, &low
, &high
))
153 if (!(high
& MASK_VALID_HI
))
156 if (!(high
& MASK_CNTP_HI
) ||
157 (high
& MASK_LOCKED_HI
))
161 per_cpu(bank_map
, cpu
) |= (1 << bank
);
163 if (shared_bank
[bank
] && c
->cpu_core_id
)
166 offset
= (high
& MASK_LVTOFF_HI
) >> 20;
168 if (setup_APIC_eilvt(offset
,
169 THRESHOLD_APIC_VECTOR
,
170 APIC_EILVT_MSG_FIX
, 0)) {
171 pr_err(FW_BUG
"cpu %d, failed to "
172 "setup threshold interrupt "
173 "for bank %d, block %d "
174 "(MSR%08X=0x%x%08x)",
175 smp_processor_id(), bank
, block
,
180 } else if (lvt_off
!= offset
) {
181 pr_err(FW_BUG
"cpu %d, invalid threshold "
182 "interrupt offset %d for bank %d,"
183 "block %d (MSR%08X=0x%x%08x)",
184 smp_processor_id(), lvt_off
, bank
,
185 block
, address
, high
, low
);
189 high
&= ~MASK_LVTOFF_HI
;
190 high
|= lvt_off
<< 20;
191 wrmsr(address
, low
, high
);
193 threshold_defaults
.address
= address
;
194 tr
.b
= &threshold_defaults
;
197 threshold_restart_bank(&tr
);
199 mce_threshold_vector
= amd_threshold_interrupt
;
205 * APIC Interrupt Handler
209 * threshold interrupt handler will service THRESHOLD_APIC_VECTOR.
210 * the interrupt goes off when error_count reaches threshold_limit.
211 * the handler will simply log mcelog w/ software defined bank number.
213 static void amd_threshold_interrupt(void)
215 u32 low
= 0, high
= 0, address
= 0;
216 unsigned int bank
, block
;
221 /* assume first bank caused it */
222 for (bank
= 0; bank
< NR_BANKS
; ++bank
) {
223 if (!(per_cpu(bank_map
, m
.cpu
) & (1 << bank
)))
225 for (block
= 0; block
< NR_BLOCKS
; ++block
) {
227 address
= MSR_IA32_MC0_MISC
+ bank
* 4;
228 } else if (block
== 1) {
229 address
= (low
& MASK_BLKPTR_LO
) >> 21;
232 address
+= MCG_XBLK_ADDR
;
237 if (rdmsr_safe(address
, &low
, &high
))
240 if (!(high
& MASK_VALID_HI
)) {
247 if (!(high
& MASK_CNTP_HI
) ||
248 (high
& MASK_LOCKED_HI
))
252 * Log the machine check that caused the threshold
255 machine_check_poll(MCP_TIMESTAMP
,
256 &__get_cpu_var(mce_poll_banks
));
258 if (high
& MASK_OVERFLOW_HI
) {
259 rdmsrl(address
, m
.misc
);
260 rdmsrl(MSR_IA32_MC0_STATUS
+ bank
* 4,
262 m
.bank
= K8_MCE_THRESHOLD_BASE
276 struct threshold_attr
{
277 struct attribute attr
;
278 ssize_t (*show
) (struct threshold_block
*, char *);
279 ssize_t (*store
) (struct threshold_block
*, const char *, size_t count
);
282 #define SHOW_FIELDS(name) \
283 static ssize_t show_ ## name(struct threshold_block *b, char *buf) \
285 return sprintf(buf, "%lx\n", (unsigned long) b->name); \
287 SHOW_FIELDS(interrupt_enable
)
288 SHOW_FIELDS(threshold_limit
)
291 store_interrupt_enable(struct threshold_block
*b
, const char *buf
, size_t size
)
293 struct thresh_restart tr
;
296 if (strict_strtoul(buf
, 0, &new) < 0)
299 b
->interrupt_enable
= !!new;
305 smp_call_function_single(b
->cpu
, threshold_restart_bank
, &tr
, 1);
311 store_threshold_limit(struct threshold_block
*b
, const char *buf
, size_t size
)
313 struct thresh_restart tr
;
316 if (strict_strtoul(buf
, 0, &new) < 0)
319 if (new > THRESHOLD_MAX
)
324 tr
.old_limit
= b
->threshold_limit
;
325 b
->threshold_limit
= new;
329 smp_call_function_single(b
->cpu
, threshold_restart_bank
, &tr
, 1);
334 struct threshold_block_cross_cpu
{
335 struct threshold_block
*tb
;
339 static void local_error_count_handler(void *_tbcc
)
341 struct threshold_block_cross_cpu
*tbcc
= _tbcc
;
342 struct threshold_block
*b
= tbcc
->tb
;
345 rdmsr(b
->address
, low
, high
);
346 tbcc
->retval
= (high
& 0xFFF) - (THRESHOLD_MAX
- b
->threshold_limit
);
349 static ssize_t
show_error_count(struct threshold_block
*b
, char *buf
)
351 struct threshold_block_cross_cpu tbcc
= { .tb
= b
, };
353 smp_call_function_single(b
->cpu
, local_error_count_handler
, &tbcc
, 1);
354 return sprintf(buf
, "%lx\n", tbcc
.retval
);
357 static ssize_t
store_error_count(struct threshold_block
*b
,
358 const char *buf
, size_t count
)
360 struct thresh_restart tr
= { .b
= b
, .reset
= 1, .old_limit
= 0 };
362 smp_call_function_single(b
->cpu
, threshold_restart_bank
, &tr
, 1);
366 #define RW_ATTR(val) \
367 static struct threshold_attr val = { \
368 .attr = {.name = __stringify(val), .mode = 0644 }, \
369 .show = show_## val, \
370 .store = store_## val, \
373 RW_ATTR(interrupt_enable
);
374 RW_ATTR(threshold_limit
);
375 RW_ATTR(error_count
);
377 static struct attribute
*default_attrs
[] = {
378 &interrupt_enable
.attr
,
379 &threshold_limit
.attr
,
384 #define to_block(k) container_of(k, struct threshold_block, kobj)
385 #define to_attr(a) container_of(a, struct threshold_attr, attr)
387 static ssize_t
show(struct kobject
*kobj
, struct attribute
*attr
, char *buf
)
389 struct threshold_block
*b
= to_block(kobj
);
390 struct threshold_attr
*a
= to_attr(attr
);
393 ret
= a
->show
? a
->show(b
, buf
) : -EIO
;
398 static ssize_t
store(struct kobject
*kobj
, struct attribute
*attr
,
399 const char *buf
, size_t count
)
401 struct threshold_block
*b
= to_block(kobj
);
402 struct threshold_attr
*a
= to_attr(attr
);
405 ret
= a
->store
? a
->store(b
, buf
, count
) : -EIO
;
410 static const struct sysfs_ops threshold_ops
= {
415 static struct kobj_type threshold_ktype
= {
416 .sysfs_ops
= &threshold_ops
,
417 .default_attrs
= default_attrs
,
420 static __cpuinit
int allocate_threshold_blocks(unsigned int cpu
,
425 struct threshold_block
*b
= NULL
;
429 if ((bank
>= NR_BANKS
) || (block
>= NR_BLOCKS
))
432 if (rdmsr_safe_on_cpu(cpu
, address
, &low
, &high
))
435 if (!(high
& MASK_VALID_HI
)) {
442 if (!(high
& MASK_CNTP_HI
) ||
443 (high
& MASK_LOCKED_HI
))
446 b
= kzalloc(sizeof(struct threshold_block
), GFP_KERNEL
);
453 b
->address
= address
;
454 b
->interrupt_enable
= 0;
455 b
->threshold_limit
= THRESHOLD_MAX
;
457 INIT_LIST_HEAD(&b
->miscj
);
459 if (per_cpu(threshold_banks
, cpu
)[bank
]->blocks
) {
461 &per_cpu(threshold_banks
, cpu
)[bank
]->blocks
->miscj
);
463 per_cpu(threshold_banks
, cpu
)[bank
]->blocks
= b
;
466 err
= kobject_init_and_add(&b
->kobj
, &threshold_ktype
,
467 per_cpu(threshold_banks
, cpu
)[bank
]->kobj
,
473 address
= (low
& MASK_BLKPTR_LO
) >> 21;
476 address
+= MCG_XBLK_ADDR
;
481 err
= allocate_threshold_blocks(cpu
, bank
, ++block
, address
);
486 kobject_uevent(&b
->kobj
, KOBJ_ADD
);
492 kobject_put(&b
->kobj
);
498 static __cpuinit
long
499 local_allocate_threshold_blocks(int cpu
, unsigned int bank
)
501 return allocate_threshold_blocks(cpu
, bank
, 0,
502 MSR_IA32_MC0_MISC
+ bank
* 4);
505 /* symlinks sibling shared banks to first core. first core owns dir/files. */
506 static __cpuinit
int threshold_create_bank(unsigned int cpu
, unsigned int bank
)
509 struct threshold_bank
*b
= NULL
;
512 struct cpuinfo_x86
*c
= &cpu_data(cpu
);
515 sprintf(name
, "threshold_bank%i", bank
);
518 if (cpu_data(cpu
).cpu_core_id
&& shared_bank
[bank
]) { /* symlink */
519 i
= cpumask_first(c
->llc_shared_map
);
521 /* first core not up yet */
522 if (cpu_data(i
).cpu_core_id
)
526 if (per_cpu(threshold_banks
, cpu
)[bank
])
529 b
= per_cpu(threshold_banks
, i
)[bank
];
534 err
= sysfs_create_link(&per_cpu(mce_dev
, cpu
).kobj
,
539 cpumask_copy(b
->cpus
, c
->llc_shared_map
);
540 per_cpu(threshold_banks
, cpu
)[bank
] = b
;
546 b
= kzalloc(sizeof(struct threshold_bank
), GFP_KERNEL
);
551 if (!zalloc_cpumask_var(&b
->cpus
, GFP_KERNEL
)) {
557 b
->kobj
= kobject_create_and_add(name
, &per_cpu(mce_dev
, cpu
).kobj
);
562 cpumask_setall(b
->cpus
);
564 cpumask_set_cpu(cpu
, b
->cpus
);
567 per_cpu(threshold_banks
, cpu
)[bank
] = b
;
569 err
= local_allocate_threshold_blocks(cpu
, bank
);
573 for_each_cpu(i
, b
->cpus
) {
577 err
= sysfs_create_link(&per_cpu(mce_dev
, i
).kobj
,
582 per_cpu(threshold_banks
, i
)[bank
] = b
;
588 per_cpu(threshold_banks
, cpu
)[bank
] = NULL
;
589 free_cpumask_var(b
->cpus
);
595 /* create dir/files for all valid threshold banks */
596 static __cpuinit
int threshold_create_device(unsigned int cpu
)
601 for (bank
= 0; bank
< NR_BANKS
; ++bank
) {
602 if (!(per_cpu(bank_map
, cpu
) & (1 << bank
)))
604 err
= threshold_create_bank(cpu
, bank
);
613 * let's be hotplug friendly.
614 * in case of multiple core processors, the first core always takes ownership
615 * of shared sysfs dir/files, and rest of the cores will be symlinked to it.
618 static void deallocate_threshold_block(unsigned int cpu
,
621 struct threshold_block
*pos
= NULL
;
622 struct threshold_block
*tmp
= NULL
;
623 struct threshold_bank
*head
= per_cpu(threshold_banks
, cpu
)[bank
];
628 list_for_each_entry_safe(pos
, tmp
, &head
->blocks
->miscj
, miscj
) {
629 kobject_put(&pos
->kobj
);
630 list_del(&pos
->miscj
);
634 kfree(per_cpu(threshold_banks
, cpu
)[bank
]->blocks
);
635 per_cpu(threshold_banks
, cpu
)[bank
]->blocks
= NULL
;
638 static void threshold_remove_bank(unsigned int cpu
, int bank
)
640 struct threshold_bank
*b
;
644 b
= per_cpu(threshold_banks
, cpu
)[bank
];
650 sprintf(name
, "threshold_bank%i", bank
);
653 /* sibling symlink */
654 if (shared_bank
[bank
] && b
->blocks
->cpu
!= cpu
) {
655 sysfs_remove_link(&per_cpu(mce_dev
, cpu
).kobj
, name
);
656 per_cpu(threshold_banks
, cpu
)[bank
] = NULL
;
662 /* remove all sibling symlinks before unregistering */
663 for_each_cpu(i
, b
->cpus
) {
667 sysfs_remove_link(&per_cpu(mce_dev
, i
).kobj
, name
);
668 per_cpu(threshold_banks
, i
)[bank
] = NULL
;
671 deallocate_threshold_block(cpu
, bank
);
674 kobject_del(b
->kobj
);
675 kobject_put(b
->kobj
);
676 free_cpumask_var(b
->cpus
);
678 per_cpu(threshold_banks
, cpu
)[bank
] = NULL
;
681 static void threshold_remove_device(unsigned int cpu
)
685 for (bank
= 0; bank
< NR_BANKS
; ++bank
) {
686 if (!(per_cpu(bank_map
, cpu
) & (1 << bank
)))
688 threshold_remove_bank(cpu
, bank
);
692 /* get notified when a cpu comes on/off */
693 static void __cpuinit
694 amd_64_threshold_cpu_callback(unsigned long action
, unsigned int cpu
)
698 case CPU_ONLINE_FROZEN
:
699 threshold_create_device(cpu
);
702 case CPU_DEAD_FROZEN
:
703 threshold_remove_device(cpu
);
710 static __init
int threshold_init_device(void)
714 /* to hit CPUs online before the notifier is up */
715 for_each_online_cpu(lcpu
) {
716 int err
= threshold_create_device(lcpu
);
721 threshold_cpu_callback
= amd_64_threshold_cpu_callback
;
725 device_initcall(threshold_init_device
);