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 */
86 /* must be called with correct cpu affinity */
87 static void threshold_restart_bank(struct threshold_block
*b
,
88 int reset
, u16 old_limit
)
90 u32 mci_misc_hi
, mci_misc_lo
;
92 rdmsr(b
->address
, mci_misc_lo
, mci_misc_hi
);
94 if (b
->threshold_limit
< (mci_misc_hi
& THRESHOLD_MAX
))
95 reset
= 1; /* limit cannot be lower than err count */
97 if (reset
) { /* reset err count and overflow bit */
99 (mci_misc_hi
& ~(MASK_ERR_COUNT_HI
| MASK_OVERFLOW_HI
)) |
100 (THRESHOLD_MAX
- b
->threshold_limit
);
101 } else if (old_limit
) { /* change limit w/o reset */
102 int new_count
= (mci_misc_hi
& THRESHOLD_MAX
) +
103 (old_limit
- b
->threshold_limit
);
104 mci_misc_hi
= (mci_misc_hi
& ~MASK_ERR_COUNT_HI
) |
105 (new_count
& THRESHOLD_MAX
);
108 b
->interrupt_enable
?
109 (mci_misc_hi
= (mci_misc_hi
& ~MASK_INT_TYPE_HI
) | INT_TYPE_APIC
) :
110 (mci_misc_hi
&= ~MASK_INT_TYPE_HI
);
112 mci_misc_hi
|= MASK_COUNT_EN_HI
;
113 wrmsr(b
->address
, mci_misc_lo
, mci_misc_hi
);
116 /* cpu init entry point, called from mce.c with preempt off */
117 void __cpuinit
mce_amd_feature_init(struct cpuinfo_x86
*c
)
119 unsigned int bank
, block
;
120 unsigned int cpu
= smp_processor_id();
122 u32 low
= 0, high
= 0, address
= 0;
124 for (bank
= 0; bank
< NR_BANKS
; ++bank
) {
125 for (block
= 0; block
< NR_BLOCKS
; ++block
) {
127 address
= MSR_IA32_MC0_MISC
+ bank
* 4;
128 else if (block
== 1) {
129 address
= (low
& MASK_BLKPTR_LO
) >> 21;
132 address
+= MCG_XBLK_ADDR
;
137 if (rdmsr_safe(address
, &low
, &high
))
140 if (!(high
& MASK_VALID_HI
)) {
147 if (!(high
& MASK_CNTP_HI
) ||
148 (high
& MASK_LOCKED_HI
))
152 per_cpu(bank_map
, cpu
) |= (1 << bank
);
154 if (shared_bank
[bank
] && c
->cpu_core_id
)
157 lvt_off
= setup_APIC_eilvt_mce(THRESHOLD_APIC_VECTOR
,
158 APIC_EILVT_MSG_FIX
, 0);
160 high
&= ~MASK_LVTOFF_HI
;
161 high
|= lvt_off
<< 20;
162 wrmsr(address
, low
, high
);
164 threshold_defaults
.address
= address
;
165 threshold_restart_bank(&threshold_defaults
, 0, 0);
171 * APIC Interrupt Handler
175 * threshold interrupt handler will service THRESHOLD_APIC_VECTOR.
176 * the interrupt goes off when error_count reaches threshold_limit.
177 * the handler will simply log mcelog w/ software defined bank number.
179 asmlinkage
void mce_threshold_interrupt(void)
181 unsigned int bank
, block
;
183 u32 low
= 0, high
= 0, address
= 0;
189 memset(&m
, 0, sizeof(m
));
191 m
.cpu
= smp_processor_id();
193 /* assume first bank caused it */
194 for (bank
= 0; bank
< NR_BANKS
; ++bank
) {
195 if (!(per_cpu(bank_map
, m
.cpu
) & (1 << bank
)))
197 for (block
= 0; block
< NR_BLOCKS
; ++block
) {
199 address
= MSR_IA32_MC0_MISC
+ bank
* 4;
200 else if (block
== 1) {
201 address
= (low
& MASK_BLKPTR_LO
) >> 21;
204 address
+= MCG_XBLK_ADDR
;
209 if (rdmsr_safe(address
, &low
, &high
))
212 if (!(high
& MASK_VALID_HI
)) {
219 if (!(high
& MASK_CNTP_HI
) ||
220 (high
& MASK_LOCKED_HI
))
223 /* Log the machine check that caused the threshold
225 do_machine_check(NULL
, 0);
227 if (high
& MASK_OVERFLOW_HI
) {
228 rdmsrl(address
, m
.misc
);
229 rdmsrl(MSR_IA32_MC0_STATUS
+ bank
* 4,
231 m
.bank
= K8_MCE_THRESHOLD_BASE
240 add_pda(irq_threshold_count
, 1);
248 struct threshold_attr
{
249 struct attribute attr
;
250 ssize_t(*show
) (struct threshold_block
*, char *);
251 ssize_t(*store
) (struct threshold_block
*, const char *, size_t count
);
254 static cpumask_t
affinity_set(unsigned int cpu
)
256 cpumask_t oldmask
= current
->cpus_allowed
;
257 cpumask_t newmask
= CPU_MASK_NONE
;
258 cpu_set(cpu
, newmask
);
259 set_cpus_allowed(current
, newmask
);
263 static void affinity_restore(cpumask_t oldmask
)
265 set_cpus_allowed(current
, oldmask
);
268 #define SHOW_FIELDS(name) \
269 static ssize_t show_ ## name(struct threshold_block * b, char *buf) \
271 return sprintf(buf, "%lx\n", (unsigned long) b->name); \
273 SHOW_FIELDS(interrupt_enable
)
274 SHOW_FIELDS(threshold_limit
)
276 static ssize_t
store_interrupt_enable(struct threshold_block
*b
,
277 const char *buf
, size_t count
)
281 unsigned long new = simple_strtoul(buf
, &end
, 0);
284 b
->interrupt_enable
= !!new;
286 oldmask
= affinity_set(b
->cpu
);
287 threshold_restart_bank(b
, 0, 0);
288 affinity_restore(oldmask
);
293 static ssize_t
store_threshold_limit(struct threshold_block
*b
,
294 const char *buf
, size_t count
)
299 unsigned long new = simple_strtoul(buf
, &end
, 0);
302 if (new > THRESHOLD_MAX
)
306 old
= b
->threshold_limit
;
307 b
->threshold_limit
= new;
309 oldmask
= affinity_set(b
->cpu
);
310 threshold_restart_bank(b
, 0, old
);
311 affinity_restore(oldmask
);
316 static ssize_t
show_error_count(struct threshold_block
*b
, char *buf
)
320 oldmask
= affinity_set(b
->cpu
);
321 rdmsr(b
->address
, low
, high
);
322 affinity_restore(oldmask
);
323 return sprintf(buf
, "%x\n",
324 (high
& 0xFFF) - (THRESHOLD_MAX
- b
->threshold_limit
));
327 static ssize_t
store_error_count(struct threshold_block
*b
,
328 const char *buf
, size_t count
)
331 oldmask
= affinity_set(b
->cpu
);
332 threshold_restart_bank(b
, 1, 0);
333 affinity_restore(oldmask
);
337 #define THRESHOLD_ATTR(_name,_mode,_show,_store) { \
338 .attr = {.name = __stringify(_name), .mode = _mode }, \
343 #define RW_ATTR(name) \
344 static struct threshold_attr name = \
345 THRESHOLD_ATTR(name, 0644, show_## name, store_## name)
347 RW_ATTR(interrupt_enable
);
348 RW_ATTR(threshold_limit
);
349 RW_ATTR(error_count
);
351 static struct attribute
*default_attrs
[] = {
352 &interrupt_enable
.attr
,
353 &threshold_limit
.attr
,
358 #define to_block(k) container_of(k, struct threshold_block, kobj)
359 #define to_attr(a) container_of(a, struct threshold_attr, attr)
361 static ssize_t
show(struct kobject
*kobj
, struct attribute
*attr
, char *buf
)
363 struct threshold_block
*b
= to_block(kobj
);
364 struct threshold_attr
*a
= to_attr(attr
);
366 ret
= a
->show
? a
->show(b
, buf
) : -EIO
;
370 static ssize_t
store(struct kobject
*kobj
, struct attribute
*attr
,
371 const char *buf
, size_t count
)
373 struct threshold_block
*b
= to_block(kobj
);
374 struct threshold_attr
*a
= to_attr(attr
);
376 ret
= a
->store
? a
->store(b
, buf
, count
) : -EIO
;
380 static struct sysfs_ops threshold_ops
= {
385 static struct kobj_type threshold_ktype
= {
386 .sysfs_ops
= &threshold_ops
,
387 .default_attrs
= default_attrs
,
390 static __cpuinit
int allocate_threshold_blocks(unsigned int cpu
,
397 struct threshold_block
*b
= NULL
;
399 if ((bank
>= NR_BANKS
) || (block
>= NR_BLOCKS
))
402 if (rdmsr_safe(address
, &low
, &high
))
405 if (!(high
& MASK_VALID_HI
)) {
412 if (!(high
& MASK_CNTP_HI
) ||
413 (high
& MASK_LOCKED_HI
))
416 b
= kzalloc(sizeof(struct threshold_block
), GFP_KERNEL
);
423 b
->address
= address
;
424 b
->interrupt_enable
= 0;
425 b
->threshold_limit
= THRESHOLD_MAX
;
427 INIT_LIST_HEAD(&b
->miscj
);
429 if (per_cpu(threshold_banks
, cpu
)[bank
]->blocks
)
431 &per_cpu(threshold_banks
, cpu
)[bank
]->blocks
->miscj
);
433 per_cpu(threshold_banks
, cpu
)[bank
]->blocks
= b
;
435 err
= kobject_init_and_add(&b
->kobj
, &threshold_ktype
,
436 per_cpu(threshold_banks
, cpu
)[bank
]->kobj
,
442 address
= (low
& MASK_BLKPTR_LO
) >> 21;
445 address
+= MCG_XBLK_ADDR
;
449 err
= allocate_threshold_blocks(cpu
, bank
, ++block
, address
);
454 kobject_uevent(&b
->kobj
, KOBJ_ADD
);
460 kobject_put(&b
->kobj
);
466 /* symlinks sibling shared banks to first core. first core owns dir/files. */
467 static __cpuinit
int threshold_create_bank(unsigned int cpu
, unsigned int bank
)
470 struct threshold_bank
*b
= NULL
;
471 cpumask_t oldmask
= CPU_MASK_NONE
;
474 sprintf(name
, "threshold_bank%i", bank
);
477 if (cpu_data(cpu
).cpu_core_id
&& shared_bank
[bank
]) { /* symlink */
478 i
= first_cpu(per_cpu(cpu_core_map
, cpu
));
480 /* first core not up yet */
481 if (cpu_data(i
).cpu_core_id
)
485 if (per_cpu(threshold_banks
, cpu
)[bank
])
488 b
= per_cpu(threshold_banks
, i
)[bank
];
493 err
= sysfs_create_link(&per_cpu(device_mce
, cpu
).kobj
,
498 b
->cpus
= per_cpu(cpu_core_map
, cpu
);
499 per_cpu(threshold_banks
, cpu
)[bank
] = b
;
504 b
= kzalloc(sizeof(struct threshold_bank
), GFP_KERNEL
);
510 b
->kobj
= kobject_create_and_add(name
, &per_cpu(device_mce
, cpu
).kobj
);
515 b
->cpus
= CPU_MASK_ALL
;
517 b
->cpus
= per_cpu(cpu_core_map
, cpu
);
520 per_cpu(threshold_banks
, cpu
)[bank
] = b
;
522 oldmask
= affinity_set(cpu
);
523 err
= allocate_threshold_blocks(cpu
, bank
, 0,
524 MSR_IA32_MC0_MISC
+ bank
* 4);
525 affinity_restore(oldmask
);
530 for_each_cpu_mask(i
, b
->cpus
) {
534 err
= sysfs_create_link(&per_cpu(device_mce
, i
).kobj
,
539 per_cpu(threshold_banks
, i
)[bank
] = b
;
545 per_cpu(threshold_banks
, cpu
)[bank
] = NULL
;
551 /* create dir/files for all valid threshold banks */
552 static __cpuinit
int threshold_create_device(unsigned int cpu
)
557 for (bank
= 0; bank
< NR_BANKS
; ++bank
) {
558 if (!(per_cpu(bank_map
, cpu
) & (1 << bank
)))
560 err
= threshold_create_bank(cpu
, bank
);
569 * let's be hotplug friendly.
570 * in case of multiple core processors, the first core always takes ownership
571 * of shared sysfs dir/files, and rest of the cores will be symlinked to it.
574 static void deallocate_threshold_block(unsigned int cpu
,
577 struct threshold_block
*pos
= NULL
;
578 struct threshold_block
*tmp
= NULL
;
579 struct threshold_bank
*head
= per_cpu(threshold_banks
, cpu
)[bank
];
584 list_for_each_entry_safe(pos
, tmp
, &head
->blocks
->miscj
, miscj
) {
585 kobject_put(&pos
->kobj
);
586 list_del(&pos
->miscj
);
590 kfree(per_cpu(threshold_banks
, cpu
)[bank
]->blocks
);
591 per_cpu(threshold_banks
, cpu
)[bank
]->blocks
= NULL
;
594 static void threshold_remove_bank(unsigned int cpu
, int bank
)
597 struct threshold_bank
*b
;
600 b
= per_cpu(threshold_banks
, cpu
)[bank
];
608 sprintf(name
, "threshold_bank%i", bank
);
611 /* sibling symlink */
612 if (shared_bank
[bank
] && b
->blocks
->cpu
!= cpu
) {
613 sysfs_remove_link(&per_cpu(device_mce
, cpu
).kobj
, name
);
614 per_cpu(threshold_banks
, cpu
)[bank
] = NULL
;
619 /* remove all sibling symlinks before unregistering */
620 for_each_cpu_mask(i
, b
->cpus
) {
624 sysfs_remove_link(&per_cpu(device_mce
, i
).kobj
, name
);
625 per_cpu(threshold_banks
, i
)[bank
] = NULL
;
628 deallocate_threshold_block(cpu
, bank
);
631 kobject_put(b
->kobj
);
633 per_cpu(threshold_banks
, cpu
)[bank
] = NULL
;
636 static void threshold_remove_device(unsigned int cpu
)
640 for (bank
= 0; bank
< NR_BANKS
; ++bank
) {
641 if (!(per_cpu(bank_map
, cpu
) & (1 << bank
)))
643 threshold_remove_bank(cpu
, bank
);
647 /* get notified when a cpu comes on/off */
648 static int __cpuinit
threshold_cpu_callback(struct notifier_block
*nfb
,
649 unsigned long action
, void *hcpu
)
651 /* cpu was unsigned int to begin with */
652 unsigned int cpu
= (unsigned long)hcpu
;
659 case CPU_ONLINE_FROZEN
:
660 threshold_create_device(cpu
);
663 case CPU_DEAD_FROZEN
:
664 threshold_remove_device(cpu
);
673 static struct notifier_block threshold_cpu_notifier __cpuinitdata
= {
674 .notifier_call
= threshold_cpu_callback
,
677 static __init
int threshold_init_device(void)
681 /* to hit CPUs online before the notifier is up */
682 for_each_online_cpu(lcpu
) {
683 int err
= threshold_create_device(lcpu
);
687 register_hotcpu_notifier(&threshold_cpu_notifier
);
691 device_initcall(threshold_init_device
);