11 #include <linux/slab.h>
12 #include <linux/device.h>
13 #include <linux/compiler.h>
15 #include <linux/sched.h>
16 #include <linux/pci.h>
18 #include <asm/processor.h>
35 #define MB(x) ((x) * 1024)
128 unsigned int level:3;
220 static const unsigned char __cpuinitconst levels[] = { 1, 1, 2, 3 };
221 static const unsigned char __cpuinitconst types[] = { 1, 2, 3, 3 };
229 unsigned line_size, lines_per_tag, assoc, size_in_kb;
239 cpuid(0x80000005, &dummy, &dummy, &l1d.val, &l1i.val);
240 cpuid(0x80000006, &dummy, &dummy, &
l2.val, &
l3.val);
248 assoc = assocs[l1->
assoc];
256 assoc = assocs[
l2.assoc];
257 line_size =
l2.line_size;
258 lines_per_tag =
l2.lines_per_tag;
265 assoc = assocs[
l3.assoc];
266 line_size =
l3.line_size;
267 lines_per_tag =
l3.lines_per_tag;
268 size_in_kb =
l3.size_encoded * 512;
270 size_in_kb = size_in_kb >> 1;
278 eax->
split.is_self_initializing = 1;
279 eax->
split.type = types[leaf];
280 eax->
split.level = levels[leaf];
281 eax->
split.num_threads_sharing = 0;
286 eax->
split.is_fully_associative = 1;
287 ebx->
split.coherency_line_size = line_size - 1;
288 ebx->
split.ways_of_associativity = assoc - 1;
289 ebx->
split.physical_line_partition = lines_per_tag - 1;
290 ecx->
split.number_of_sets = (size_in_kb * 1024) / line_size /
291 (ebx->
split.ways_of_associativity + 1) - 1;
309 unsigned int sc0, sc1, sc2, sc3;
312 pci_read_config_dword(nb->
misc, 0x1C4, &val);
339 if (this_leaf->
nb && !this_leaf->
nb->l3_cache.indices)
340 amd_calc_l3_indices(this_leaf->
nb);
352 unsigned int reg = 0;
354 pci_read_config_dword(nb->
misc, 0x1BC + slot * 4, ®);
357 if (reg & (3
UL << 30))
371 index = amd_get_l3_disable_slot(this_leaf->
base.nb, slot);
373 return sprintf(buf,
"%d\n", index);
378 #define SHOW_CACHE_DISABLE(slot) \
380 show_cache_disable_##slot(struct _cpuid4_info *this_leaf, char *buf, \
383 return show_cache_disable(this_leaf, buf, slot); \
385 SHOW_CACHE_DISABLE(0)
386 SHOW_CACHE_DISABLE(1)
389 unsigned slot,
unsigned long idx)
398 for (i = 0; i < 4; i++) {
399 u32 reg = idx | (i << 20);
401 if (!nb->l3_cache.subcaches[i])
404 pci_write_config_dword(nb->misc, 0x1BC + slot * 4, reg);
414 pci_write_config_dword(nb->misc, 0x1BC + slot * 4, reg);
428 int amd_set_l3_disable_slot(
struct amd_northbridge *nb,
int cpu,
unsigned slot,
434 ret = amd_get_l3_disable_slot(nb, slot);
442 if (index == amd_get_l3_disable_slot(nb, !slot))
445 amd_l3_disable_index(nb, cpu, slot, index);
451 const char *buf,
size_t count,
454 unsigned long val = 0;
463 cpu = cpumask_first(
to_cpumask(this_leaf->shared_cpu_map));
468 err = amd_set_l3_disable_slot(this_leaf->
base.nb, cpu, slot, val);
471 pr_warning(
"L3 slot %d in use/index already disabled!\n",
478 #define STORE_CACHE_DISABLE(slot) \
480 store_cache_disable_##slot(struct _cpuid4_info *this_leaf, \
481 const char *buf, size_t count, \
484 return store_cache_disable(this_leaf, buf, count, slot); \
486 STORE_CACHE_DISABLE(0)
487 STORE_CACHE_DISABLE(1)
490 show_cache_disable_0, store_cache_disable_0);
492 show_cache_disable_1, store_cache_disable_1);
504 store_subcaches(
struct _cpuid4_info *this_leaf,
const char *buf,
size_t count,
525 __ATTR(subcaches, 0644, show_subcaches, store_subcaches);
528 #define amd_init_l3_cache(x, y)
532 __cpuinit cpuid4_cache_lookup_regs(
int index,
541 amd_cpuid4(index, &eax, &ebx, &ecx);
544 cpuid_count(4, index, &eax.
full, &ebx.
full, &ecx.
full, &edx);
550 this_leaf->
eax = eax;
553 this_leaf->
size = (ecx.
split.number_of_sets + 1) *
554 (ebx.
split.coherency_line_size + 1) *
555 (ebx.
split.physical_line_partition + 1) *
556 (ebx.
split.ways_of_associativity + 1);
560 static int __cpuinit find_num_cache_leaves(
void)
562 unsigned int eax,
ebx,
ecx, edx;
569 cpuid_count(4, i, &eax, &ebx, &ecx, &edx);
570 cache_eax.full = eax;
578 unsigned int trace = 0, l1i = 0, l1d = 0,
l2 = 0, l3 = 0;
579 unsigned int new_l1d = 0, new_l1i = 0;
580 unsigned int new_l2 = 0, new_l3 = 0,
i;
587 static int is_initialized;
589 if (is_initialized == 0) {
603 retval = cpuid4_cache_lookup_regs(i, &this_leaf);
605 switch (this_leaf.
eax.
split.level) {
609 new_l1d = this_leaf.
size/1024;
610 else if (this_leaf.
eax.
split.type ==
612 new_l1i = this_leaf.
size/1024;
615 new_l2 = this_leaf.
size/1024;
616 num_threads_sharing = 1 + this_leaf.
eax.
split.num_threads_sharing;
617 index_msb = get_count_order(num_threads_sharing);
618 l2_id = c->
apicid & ~((1 << index_msb) - 1);
621 new_l3 = this_leaf.
size/1024;
622 num_threads_sharing = 1 + this_leaf.
eax.
split.num_threads_sharing;
623 index_msb = get_count_order(
624 num_threads_sharing);
625 l3_id = c->
apicid & ~((1 << index_msb) - 1);
640 unsigned int regs[4];
641 unsigned char *
dp = (
unsigned char *)regs;
648 n = cpuid_eax(2) & 0xFF;
650 for (i = 0 ; i <
n ; i++) {
651 cpuid(2, ®s[0], ®s[1], ®s[2], ®s[3]);
654 for (j = 0 ; j < 3 ; j++)
655 if (regs[j] & (1 << 31))
659 for (j = 1 ; j < 16 ; j++) {
660 unsigned char des = dp[
j];
670 l1i += cache_table[
k].
size;
673 l1d += cache_table[
k].
size;
679 l3 += cache_table[
k].
size;
682 trace += cache_table[
k].
size;
704 per_cpu(cpu_llc_id, cpu) = l2_id;
711 per_cpu(cpu_llc_id, cpu) = l3_id;
724 #define CPUID4_INFO_IDX(x, y) (&((per_cpu(ici_cpuid4_info, x))[y]))
728 static int __cpuinit cache_shared_amd_cpu_map_setup(
unsigned int cpu,
int index)
738 if (!
per_cpu(ici_cpuid4_info, i))
740 this_leaf = CPUID4_INFO_IDX(i, index);
744 set_bit(sibling, this_leaf->shared_cpu_map);
747 }
else if ((c->
x86 == 0x15) && ((index == 1) || (index == 2))) {
750 if (!
per_cpu(ici_cpuid4_info, i))
752 this_leaf = CPUID4_INFO_IDX(i, index);
756 set_bit(sibling, this_leaf->shared_cpu_map);
764 static void __cpuinit cache_shared_cpu_map_setup(
unsigned int cpu,
int index)
767 unsigned long num_threads_sharing;
772 if (cache_shared_amd_cpu_map_setup(cpu, index))
776 this_leaf = CPUID4_INFO_IDX(cpu, index);
777 num_threads_sharing = 1 + this_leaf->
base.eax.split.num_threads_sharing;
779 if (num_threads_sharing == 1)
780 cpumask_set_cpu(cpu,
to_cpumask(this_leaf->shared_cpu_map));
782 index_msb = get_count_order(num_threads_sharing);
789 if (i != cpu &&
per_cpu(ici_cpuid4_info, i)) {
791 CPUID4_INFO_IDX(i, index);
793 sibling_leaf->shared_cpu_map));
799 static void __cpuinit cache_remove_shared_cpu_map(
unsigned int cpu,
int index)
804 this_leaf = CPUID4_INFO_IDX(cpu, index);
806 sibling_leaf = CPUID4_INFO_IDX(sibling, index);
807 cpumask_clear_cpu(cpu,
812 static void __cpuinit cache_shared_cpu_map_setup(
unsigned int cpu,
int index)
816 static void __cpuinit cache_remove_shared_cpu_map(
unsigned int cpu,
int index)
821 static void __cpuinit free_cache_attributes(
unsigned int cpu)
826 cache_remove_shared_cpu_map(cpu, i);
832 static void __cpuinit get_cpu_leaves(
void *_retval)
838 struct _cpuid4_info *this_leaf = CPUID4_INFO_IDX(cpu, j);
840 *retval = cpuid4_cache_lookup_regs(j, &this_leaf->
base);
844 for (i = 0; i <
j; i++)
845 cache_remove_shared_cpu_map(cpu, i);
848 cache_shared_cpu_map_setup(cpu, j);
852 static int __cpuinit detect_cache_attributes(
unsigned int cpu)
856 if (num_cache_leaves == 0)
859 per_cpu(ici_cpuid4_info, cpu) = kzalloc(
880 struct _index_kobject {
883 unsigned short index;
887 static DEFINE_PER_CPU(
struct _index_kobject *, ici_index_kobject);
888 #define INDEX_KOBJECT_PTR(x, y) (&((per_cpu(ici_index_kobject, x))[y]))
890 #define show_one_plus(file_name, object, val) \
891 static ssize_t show_##file_name(struct _cpuid4_info *this_leaf, char *buf, \
894 return sprintf(buf, "%lu\n", (unsigned long)this_leaf->object + val); \
897 show_one_plus(
level,
base.eax.split.level, 0);
898 show_one_plus(coherency_line_size,
base.ebx.split.coherency_line_size, 1);
899 show_one_plus(physical_line_partition,
base.ebx.split.physical_line_partition, 1);
900 show_one_plus(ways_of_associativity,
base.ebx.split.ways_of_associativity, 1);
901 show_one_plus(number_of_sets,
base.ecx.split.number_of_sets, 1);
906 return sprintf(buf,
"%luK\n", this_leaf->
base.size / 1024);
920 cpulist_scnprintf(buf, len-2, mask) :
921 cpumask_scnprintf(buf, len-2, mask);
931 return show_shared_cpu_map_func(leaf, 0, buf);
937 return show_shared_cpu_map_func(leaf, 1, buf);
943 switch (this_leaf->
base.eax.split.type) {
947 return sprintf(buf,
"Instruction\n");
949 return sprintf(buf,
"Unified\n");
951 return sprintf(buf,
"Unknown\n");
955 #define to_object(k) container_of(k, struct _index_kobject, kobj)
956 #define to_attr(a) container_of(a, struct _cache_attr, attr)
958 #define define_one_ro(_name) \
959 static struct _cache_attr _name = \
960 __ATTR(_name, 0444, show_##_name, NULL)
972 static struct attribute *default_attrs[] = {
975 &coherency_line_size.attr,
976 &physical_line_partition.attr,
977 &ways_of_associativity.attr,
978 &number_of_sets.attr,
980 &shared_cpu_map.attr,
981 &shared_cpu_list.attr,
1004 return attrs = default_attrs;
1006 for (n = 0; default_attrs[
n]; n++)
1007 attrs[n] = default_attrs[n];
1010 attrs[n++] = &cache_disable_0.attr;
1011 attrs[n++] = &cache_disable_1.attr;
1015 attrs[n++] = &subcaches.
attr;
1024 struct _index_kobject *this_leaf =
to_object(kobj);
1028 fattr->
show(CPUID4_INFO_IDX(this_leaf->cpu, this_leaf->index),
1029 buf, this_leaf->cpu) :
1035 const char *buf,
size_t count)
1038 struct _index_kobject *this_leaf =
to_object(kobj);
1041 ret = fattr->
store ?
1042 fattr->
store(CPUID4_INFO_IDX(this_leaf->cpu, this_leaf->index),
1043 buf, count, this_leaf->cpu) :
1058 static struct kobj_type ktype_percpu_entry = {
1062 static void __cpuinit cpuid4_cache_sysfs_exit(
unsigned int cpu)
1068 free_cache_attributes(cpu);
1071 static int __cpuinit cpuid4_cache_sysfs_init(
unsigned int cpu)
1075 if (num_cache_leaves == 0)
1078 err = detect_cache_attributes(cpu);
1083 per_cpu(ici_cache_kobject, cpu) =
1088 per_cpu(ici_index_kobject, cpu) = kzalloc(
1089 sizeof(
struct _index_kobject) * num_cache_leaves,
GFP_KERNEL);
1096 cpuid4_cache_sysfs_exit(cpu);
1105 unsigned int cpu = dev->
id;
1107 struct _index_kobject *this_object;
1111 retval = cpuid4_cache_sysfs_init(cpu);
1116 &ktype_percpu_entry,
1117 &dev->
kobj,
"%s",
"cache");
1119 cpuid4_cache_sysfs_exit(cpu);
1124 this_object = INDEX_KOBJECT_PTR(cpu, i);
1125 this_object->cpu =
cpu;
1126 this_object->index =
i;
1128 this_leaf = CPUID4_INFO_IDX(cpu, i);
1131 #ifdef CONFIG_AMD_NB
1132 if (this_leaf->
base.nb)
1137 per_cpu(ici_cache_kobject, cpu),
1140 for (j = 0; j <
i; j++)
1143 cpuid4_cache_sysfs_exit(cpu);
1148 cpumask_set_cpu(cpu,
to_cpumask(cache_dev_map));
1156 unsigned int cpu = dev->
id;
1163 cpumask_clear_cpu(cpu,
to_cpumask(cache_dev_map));
1168 cpuid4_cache_sysfs_exit(cpu);
1172 unsigned long action,
void *hcpu)
1174 unsigned int cpu = (
unsigned long)hcpu;
1185 cache_remove_dev(dev);
1192 .notifier_call = cacheinfo_cpu_callback,
1195 static int __cpuinit cache_sysfs_init(
void)
1199 if (num_cache_leaves == 0)
1206 err = cache_add_dev(dev);