22 #include <linux/errno.h>
23 #include <linux/sched.h>
25 #include <linux/slab.h>
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 static const char *
const th_names[] = {
62 static unsigned char shared_bank[
NR_BANKS] = {
68 static void amd_threshold_interrupt(
void);
102 static bool lvt_interrupt_supported(
unsigned int bank,
u32 msr_high_bits)
114 return msr_high_bits &
BIT(28);
122 pr_err(
FW_BUG "cpu %d, failed to setup threshold interrupt "
123 "for bank %d, block %d (MSR%08X=0x%x%08x)\n", b->
cpu,
129 pr_err(
FW_BUG "cpu %d, invalid threshold interrupt offset %d "
130 "for bank %d, block %d (MSR%08X=0x%x%08x)\n",
142 static void threshold_restart_bank(
void *_tr)
147 rdmsr(tr->
b->address, lo, hi);
155 (THRESHOLD_MAX - tr->
b->threshold_limit);
161 (new_count & THRESHOLD_MAX);
167 if (!tr->
b->interrupt_capable)
171 if (lvt_off_valid(tr->
b, tr->
lvt_off, lo, hi)) {
178 if (tr->
b->interrupt_enable)
184 wrmsr(tr->
b->address, lo, hi);
196 threshold_restart_bank(&tr);
199 static int setup_APIC_mce(
int reserved,
int new)
221 else if (block == 1) {
252 offset = setup_APIC_mce(offset,
new);
255 mce_threshold_block_init(&b, offset);
270 static void amd_threshold_interrupt(
void)
280 if (!(
per_cpu(bank_map,
m.cpu) & (1 << bank)))
285 }
else if (block == 1) {
339 #define SHOW_FIELDS(name) \
340 static ssize_t show_ ## name(struct threshold_block *b, char *buf) \
342 return sprintf(buf, "%lu\n", (unsigned long) b->name); \
353 if (!b->interrupt_capable)
359 b->interrupt_enable = !!
new;
361 memset(&tr, 0,
sizeof(tr));
378 if (
new > THRESHOLD_MAX)
383 memset(&tr, 0,
sizeof(tr));
399 return sprintf(buf,
"%u\n", ((hi & THRESHOLD_MAX) -
404 .attr = {.name =
__stringify(error_count), .mode = 0444 },
405 .show = show_error_count,
408 #define RW_ATTR(val) \
409 static struct threshold_attr val = { \
410 .attr = {.name = __stringify(val), .mode = 0644 }, \
411 .show = show_## val, \
412 .store = store_## val, \
418 static struct attribute *default_attrs[] = {
419 &threshold_limit.attr,
425 #define to_block(k) container_of(k, struct threshold_block, kobj)
426 #define to_attr(a) container_of(a, struct threshold_attr, attr)
440 const char *buf,
size_t count)
451 static const struct sysfs_ops threshold_ops = {
456 static struct kobj_type threshold_ktype = {
457 .sysfs_ops = &threshold_ops,
461 static __cpuinit int allocate_threshold_blocks(
unsigned int cpu,
470 if ((bank >= NR_BANKS) || (block >= NR_BLOCKS))
476 if (!(high & MASK_VALID_HI)) {
504 INIT_LIST_HEAD(&b->
miscj);
506 if (
per_cpu(threshold_banks, cpu)[bank]->blocks) {
508 &
per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
514 per_cpu(threshold_banks, cpu)[bank]->kobj,
515 (bank == 4 ? bank4_names(b) : th_names[bank]));
528 err = allocate_threshold_blocks(cpu, bank, ++block, address);
570 static
__cpuinit int threshold_create_bank(
unsigned int cpu,
unsigned int bank)
575 const char *
name = th_names[bank];
578 if (shared_bank[bank]) {
582 if (nb && nb->
bank4) {
589 per_cpu(threshold_banks, cpu)[bank] =
b;
592 err = __threshold_add_blocks(b);
610 per_cpu(threshold_banks, cpu)[bank] =
b;
612 if (shared_bank[bank]) {
622 err = allocate_threshold_blocks(cpu, bank, 0,
635 static __cpuinit int threshold_create_device(
unsigned int cpu)
640 for (bank = 0; bank <
NR_BANKS; ++bank) {
641 if (!(
per_cpu(bank_map, cpu) & (1 << bank)))
643 err = threshold_create_bank(cpu, bank);
651 static void deallocate_threshold_block(
unsigned int cpu,
668 per_cpu(threshold_banks, cpu)[bank]->blocks =
NULL;
682 static
void threshold_remove_bank(
unsigned int cpu,
int bank)
687 b =
per_cpu(threshold_banks, cpu)[bank];
694 if (shared_bank[bank]) {
696 __threshold_remove_blocks(b);
709 deallocate_threshold_block(cpu, bank);
718 static void threshold_remove_device(
unsigned int cpu)
722 for (bank = 0; bank <
NR_BANKS; ++bank) {
723 if (!(
per_cpu(bank_map, cpu) & (1 << bank)))
725 threshold_remove_bank(cpu, bank);
731 amd_64_threshold_cpu_callback(
unsigned long action,
unsigned int cpu)
736 threshold_create_device(cpu);
740 threshold_remove_device(cpu);
747 static __init int threshold_init_device(
void)
753 int err = threshold_create_device(lcpu);