1 // SPDX-License-Identifier: GPL-2.0-only
3 * Processor cache information made available to userspace via sysfs;
4 * intended to be compatible with x86 intel_cacheinfo implementation.
6 * Copyright 2008 IBM Corporation
10 #define pr_fmt(fmt) "cacheinfo: " fmt
12 #include <linux/cpu.h>
13 #include <linux/cpumask.h>
14 #include <linux/kernel.h>
15 #include <linux/kobject.h>
16 #include <linux/list.h>
17 #include <linux/notifier.h>
19 #include <linux/percpu.h>
20 #include <linux/slab.h>
21 #include <asm/cputhreads.h>
24 #include "cacheinfo.h"
26 /* per-cpu object for tracking:
27 * - a "cache" kobject for the top-level directory
28 * - a list of "index" objects representing the cpu's local cache hierarchy
31 struct kobject *kobj; /* bare (not embedded) kobject for cache
33 struct cache_index_dir *index; /* list of index objects */
36 /* "index" object: each cpu's cache directory has an index
37 * subdirectory corresponding to a cache object associated with the
38 * cpu. This object's lifetime is managed via the embedded kobject.
40 struct cache_index_dir {
42 struct cache_index_dir *next; /* next index in parent directory */
46 /* Template for determining which OF properties to query for a given
48 struct cache_type_info {
50 const char *size_prop;
52 /* Allow for both [di]-cache-line-size and
53 * [di]-cache-block-size properties. According to the PowerPC
54 * Processor binding, -line-size should be provided if it
55 * differs from the cache block size (that which is operated
56 * on by cache instructions), so we look for -line-size first.
57 * See cache_get_line_size(). */
59 const char *line_size_props[2];
60 const char *nr_sets_prop;
63 /* These are used to index the cache_type_info array. */
64 #define CACHE_TYPE_UNIFIED 0 /* cache-size, cache-block-size, etc. */
65 #define CACHE_TYPE_UNIFIED_D 1 /* d-cache-size, d-cache-block-size, etc */
66 #define CACHE_TYPE_INSTRUCTION 2
67 #define CACHE_TYPE_DATA 3
69 static const struct cache_type_info cache_type_info[] = {
71 /* Embedded systems that use cache-size, cache-block-size,
72 * etc. for the Unified (typically L2) cache. */
74 .size_prop = "cache-size",
75 .line_size_props = { "cache-line-size",
76 "cache-block-size", },
77 .nr_sets_prop = "cache-sets",
80 /* PowerPC Processor binding says the [di]-cache-*
81 * must be equal on unified caches, so just use
82 * d-cache properties. */
84 .size_prop = "d-cache-size",
85 .line_size_props = { "d-cache-line-size",
86 "d-cache-block-size", },
87 .nr_sets_prop = "d-cache-sets",
90 .name = "Instruction",
91 .size_prop = "i-cache-size",
92 .line_size_props = { "i-cache-line-size",
93 "i-cache-block-size", },
94 .nr_sets_prop = "i-cache-sets",
98 .size_prop = "d-cache-size",
99 .line_size_props = { "d-cache-line-size",
100 "d-cache-block-size", },
101 .nr_sets_prop = "d-cache-sets",
105 /* Cache object: each instance of this corresponds to a distinct cache
106 * in the system. There are separate objects for Harvard caches: one
107 * each for instruction and data, and each refers to the same OF node.
108 * The refcount of the OF node is elevated for the lifetime of the
109 * cache object. A cache object is released when its shared_cpu_map
110 * is cleared (see cache_cpu_clear).
112 * A cache object is on two lists: an unsorted global list
113 * (cache_list) of cache objects; and a singly-linked list
114 * representing the local cache hierarchy, which is ordered by level
115 * (e.g. L1d -> L1i -> L2 -> L3).
118 struct device_node *ofnode; /* OF node for this cache, may be cpu */
119 struct cpumask shared_cpu_map; /* online CPUs using this cache */
120 int type; /* split cache disambiguation */
121 int level; /* level not explicit in device tree */
122 int group_id; /* id of the group of threads that share this cache */
123 struct list_head list; /* global list of cache objects */
124 struct cache *next_local; /* next cache of >= level */
127 static DEFINE_PER_CPU(struct cache_dir *, cache_dir_pcpu);
129 /* traversal/modification of this list occurs only at cpu hotplug time;
130 * access is serialized by cpu hotplug locking
132 static LIST_HEAD(cache_list);
134 static struct cache_index_dir *kobj_to_cache_index_dir(struct kobject *k)
136 return container_of(k, struct cache_index_dir, kobj);
139 static const char *cache_type_string(const struct cache *cache)
141 return cache_type_info[cache->type].name;
144 static void cache_init(struct cache *cache, int type, int level,
145 struct device_node *ofnode, int group_id)
148 cache->level = level;
149 cache->ofnode = of_node_get(ofnode);
150 cache->group_id = group_id;
151 INIT_LIST_HEAD(&cache->list);
152 list_add(&cache->list, &cache_list);
155 static struct cache *new_cache(int type, int level,
156 struct device_node *ofnode, int group_id)
160 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
162 cache_init(cache, type, level, ofnode, group_id);
167 static void release_cache_debugcheck(struct cache *cache)
171 list_for_each_entry(iter, &cache_list, list)
172 WARN_ONCE(iter->next_local == cache,
173 "cache for %pOFP(%s) refers to cache for %pOFP(%s)\n",
175 cache_type_string(iter),
177 cache_type_string(cache));
180 static void release_cache(struct cache *cache)
185 pr_debug("freeing L%d %s cache for %pOFP\n", cache->level,
186 cache_type_string(cache), cache->ofnode);
188 release_cache_debugcheck(cache);
189 list_del(&cache->list);
190 of_node_put(cache->ofnode);
194 static void cache_cpu_set(struct cache *cache, int cpu)
196 struct cache *next = cache;
199 WARN_ONCE(cpumask_test_cpu(cpu, &next->shared_cpu_map),
200 "CPU %i already accounted in %pOFP(%s)\n",
202 cache_type_string(next));
203 cpumask_set_cpu(cpu, &next->shared_cpu_map);
204 next = next->next_local;
208 static int cache_size(const struct cache *cache, unsigned int *ret)
210 const char *propname;
211 const __be32 *cache_size;
213 propname = cache_type_info[cache->type].size_prop;
215 cache_size = of_get_property(cache->ofnode, propname, NULL);
219 *ret = of_read_number(cache_size, 1);
223 static int cache_size_kb(const struct cache *cache, unsigned int *ret)
227 if (cache_size(cache, &size))
234 /* not cache_line_size() because that's a macro in include/linux/cache.h */
235 static int cache_get_line_size(const struct cache *cache, unsigned int *ret)
237 const __be32 *line_size;
240 lim = ARRAY_SIZE(cache_type_info[cache->type].line_size_props);
242 for (i = 0; i < lim; i++) {
243 const char *propname;
245 propname = cache_type_info[cache->type].line_size_props[i];
246 line_size = of_get_property(cache->ofnode, propname, NULL);
254 *ret = of_read_number(line_size, 1);
258 static int cache_nr_sets(const struct cache *cache, unsigned int *ret)
260 const char *propname;
261 const __be32 *nr_sets;
263 propname = cache_type_info[cache->type].nr_sets_prop;
265 nr_sets = of_get_property(cache->ofnode, propname, NULL);
269 *ret = of_read_number(nr_sets, 1);
273 static int cache_associativity(const struct cache *cache, unsigned int *ret)
275 unsigned int line_size;
276 unsigned int nr_sets;
279 if (cache_nr_sets(cache, &nr_sets))
282 /* If the cache is fully associative, there is no need to
283 * check the other properties.
290 if (cache_get_line_size(cache, &line_size))
292 if (cache_size(cache, &size))
295 if (!(nr_sets > 0 && size > 0 && line_size > 0))
298 *ret = (size / nr_sets) / line_size;
304 /* helper for dealing with split caches */
305 static struct cache *cache_find_first_sibling(struct cache *cache)
309 if (cache->type == CACHE_TYPE_UNIFIED ||
310 cache->type == CACHE_TYPE_UNIFIED_D)
313 list_for_each_entry(iter, &cache_list, list)
314 if (iter->ofnode == cache->ofnode &&
315 iter->group_id == cache->group_id &&
316 iter->next_local == cache)
322 /* return the first cache on a local list matching node and thread-group id */
323 static struct cache *cache_lookup_by_node_group(const struct device_node *node,
326 struct cache *cache = NULL;
329 list_for_each_entry(iter, &cache_list, list) {
330 if (iter->ofnode != node ||
331 iter->group_id != group_id)
333 cache = cache_find_first_sibling(iter);
340 static bool cache_node_is_unified(const struct device_node *np)
342 return of_get_property(np, "cache-unified", NULL);
346 * Unified caches can have two different sets of tags. Most embedded
347 * use cache-size, etc. for the unified cache size, but open firmware systems
348 * use d-cache-size, etc. Check on initialization for which type we have, and
349 * return the appropriate structure type. Assume it's embedded if it isn't
350 * open firmware. If it's yet a 3rd type, then there will be missing entries
351 * in /sys/devices/system/cpu/cpu0/cache/index2/, and this code will need
352 * to be extended further.
354 static int cache_is_unified_d(const struct device_node *np)
356 return of_get_property(np,
357 cache_type_info[CACHE_TYPE_UNIFIED_D].size_prop, NULL) ?
358 CACHE_TYPE_UNIFIED_D : CACHE_TYPE_UNIFIED;
361 static struct cache *cache_do_one_devnode_unified(struct device_node *node, int group_id,
364 pr_debug("creating L%d ucache for %pOFP\n", level, node);
366 return new_cache(cache_is_unified_d(node), level, node, group_id);
369 static struct cache *cache_do_one_devnode_split(struct device_node *node, int group_id,
372 struct cache *dcache, *icache;
374 pr_debug("creating L%d dcache and icache for %pOFP\n", level,
377 dcache = new_cache(CACHE_TYPE_DATA, level, node, group_id);
378 icache = new_cache(CACHE_TYPE_INSTRUCTION, level, node, group_id);
380 if (!dcache || !icache)
383 dcache->next_local = icache;
387 release_cache(dcache);
388 release_cache(icache);
392 static struct cache *cache_do_one_devnode(struct device_node *node, int group_id, int level)
396 if (cache_node_is_unified(node))
397 cache = cache_do_one_devnode_unified(node, group_id, level);
399 cache = cache_do_one_devnode_split(node, group_id, level);
404 static struct cache *cache_lookup_or_instantiate(struct device_node *node,
410 cache = cache_lookup_by_node_group(node, group_id);
412 WARN_ONCE(cache && cache->level != level,
413 "cache level mismatch on lookup (got %d, expected %d)\n",
414 cache->level, level);
417 cache = cache_do_one_devnode(node, group_id, level);
422 static void link_cache_lists(struct cache *smaller, struct cache *bigger)
424 while (smaller->next_local) {
425 if (smaller->next_local == bigger)
426 return; /* already linked */
427 smaller = smaller->next_local;
430 smaller->next_local = bigger;
433 * The cache->next_local list sorts by level ascending:
434 * L1d -> L1i -> L2 -> L3 ...
436 WARN_ONCE((smaller->level == 1 && bigger->level > 2) ||
437 (smaller->level > 1 && bigger->level != smaller->level + 1),
438 "linking L%i cache %pOFP to L%i cache %pOFP; skipped a level?\n",
439 smaller->level, smaller->ofnode, bigger->level, bigger->ofnode);
442 static void do_subsidiary_caches_debugcheck(struct cache *cache)
444 WARN_ONCE(cache->level != 1,
445 "instantiating cache chain from L%d %s cache for "
446 "%pOFP instead of an L1\n", cache->level,
447 cache_type_string(cache), cache->ofnode);
448 WARN_ONCE(!of_node_is_type(cache->ofnode, "cpu"),
449 "instantiating cache chain from node %pOFP of type '%s' "
450 "instead of a cpu node\n", cache->ofnode,
451 of_node_get_device_type(cache->ofnode));
455 * If sub-groups of threads in a core containing @cpu_id share the
456 * L@level-cache (information obtained via "ibm,thread-groups"
457 * device-tree property), then we identify the group by the first
458 * thread-sibling in the group. We define this to be the group-id.
460 * In the absence of any thread-group information for L@level-cache,
461 * this function returns -1.
463 static int get_group_id(unsigned int cpu_id, int level)
465 if (has_big_cores && level == 1)
466 return cpumask_first(per_cpu(thread_group_l1_cache_map,
468 else if (thread_group_shares_l2 && level == 2)
469 return cpumask_first(per_cpu(thread_group_l2_cache_map,
471 else if (thread_group_shares_l3 && level == 3)
472 return cpumask_first(per_cpu(thread_group_l3_cache_map,
477 static void do_subsidiary_caches(struct cache *cache, unsigned int cpu_id)
479 struct device_node *subcache_node;
480 int level = cache->level;
482 do_subsidiary_caches_debugcheck(cache);
484 while ((subcache_node = of_find_next_cache_node(cache->ofnode))) {
485 struct cache *subcache;
489 group_id = get_group_id(cpu_id, level);
490 subcache = cache_lookup_or_instantiate(subcache_node, group_id, level);
491 of_node_put(subcache_node);
495 link_cache_lists(cache, subcache);
500 static struct cache *cache_chain_instantiate(unsigned int cpu_id)
502 struct device_node *cpu_node;
503 struct cache *cpu_cache = NULL;
506 pr_debug("creating cache object(s) for CPU %i\n", cpu_id);
508 cpu_node = of_get_cpu_node(cpu_id, NULL);
509 WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
513 group_id = get_group_id(cpu_id, 1);
515 cpu_cache = cache_lookup_or_instantiate(cpu_node, group_id, 1);
519 do_subsidiary_caches(cpu_cache, cpu_id);
521 cache_cpu_set(cpu_cache, cpu_id);
523 of_node_put(cpu_node);
528 static struct cache_dir *cacheinfo_create_cache_dir(unsigned int cpu_id)
530 struct cache_dir *cache_dir;
532 struct kobject *kobj = NULL;
534 dev = get_cpu_device(cpu_id);
535 WARN_ONCE(!dev, "no dev for CPU %i\n", cpu_id);
539 kobj = kobject_create_and_add("cache", &dev->kobj);
543 cache_dir = kzalloc(sizeof(*cache_dir), GFP_KERNEL);
547 cache_dir->kobj = kobj;
549 WARN_ON_ONCE(per_cpu(cache_dir_pcpu, cpu_id) != NULL);
551 per_cpu(cache_dir_pcpu, cpu_id) = cache_dir;
559 static void cache_index_release(struct kobject *kobj)
561 struct cache_index_dir *index;
563 index = kobj_to_cache_index_dir(kobj);
565 pr_debug("freeing index directory for L%d %s cache\n",
566 index->cache->level, cache_type_string(index->cache));
571 static ssize_t cache_index_show(struct kobject *k, struct attribute *attr, char *buf)
573 struct kobj_attribute *kobj_attr;
575 kobj_attr = container_of(attr, struct kobj_attribute, attr);
577 return kobj_attr->show(k, kobj_attr, buf);
580 static struct cache *index_kobj_to_cache(struct kobject *k)
582 struct cache_index_dir *index;
584 index = kobj_to_cache_index_dir(k);
589 static ssize_t size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
591 unsigned int size_kb;
594 cache = index_kobj_to_cache(k);
596 if (cache_size_kb(cache, &size_kb))
599 return sprintf(buf, "%uK\n", size_kb);
602 static struct kobj_attribute cache_size_attr =
603 __ATTR(size, 0444, size_show, NULL);
606 static ssize_t line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
608 unsigned int line_size;
611 cache = index_kobj_to_cache(k);
613 if (cache_get_line_size(cache, &line_size))
616 return sprintf(buf, "%u\n", line_size);
619 static struct kobj_attribute cache_line_size_attr =
620 __ATTR(coherency_line_size, 0444, line_size_show, NULL);
622 static ssize_t nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
624 unsigned int nr_sets;
627 cache = index_kobj_to_cache(k);
629 if (cache_nr_sets(cache, &nr_sets))
632 return sprintf(buf, "%u\n", nr_sets);
635 static struct kobj_attribute cache_nr_sets_attr =
636 __ATTR(number_of_sets, 0444, nr_sets_show, NULL);
638 static ssize_t associativity_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
640 unsigned int associativity;
643 cache = index_kobj_to_cache(k);
645 if (cache_associativity(cache, &associativity))
648 return sprintf(buf, "%u\n", associativity);
651 static struct kobj_attribute cache_assoc_attr =
652 __ATTR(ways_of_associativity, 0444, associativity_show, NULL);
654 static ssize_t type_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
658 cache = index_kobj_to_cache(k);
660 return sprintf(buf, "%s\n", cache_type_string(cache));
663 static struct kobj_attribute cache_type_attr =
664 __ATTR(type, 0444, type_show, NULL);
666 static ssize_t level_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
668 struct cache_index_dir *index;
671 index = kobj_to_cache_index_dir(k);
672 cache = index->cache;
674 return sprintf(buf, "%d\n", cache->level);
677 static struct kobj_attribute cache_level_attr =
678 __ATTR(level, 0444, level_show, NULL);
681 show_shared_cpumap(struct kobject *k, struct kobj_attribute *attr, char *buf, bool list)
683 struct cache_index_dir *index;
685 const struct cpumask *mask;
687 index = kobj_to_cache_index_dir(k);
688 cache = index->cache;
690 mask = &cache->shared_cpu_map;
692 return cpumap_print_to_pagebuf(list, buf, mask);
695 static ssize_t shared_cpu_map_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
697 return show_shared_cpumap(k, attr, buf, false);
700 static ssize_t shared_cpu_list_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
702 return show_shared_cpumap(k, attr, buf, true);
705 static struct kobj_attribute cache_shared_cpu_map_attr =
706 __ATTR(shared_cpu_map, 0444, shared_cpu_map_show, NULL);
708 static struct kobj_attribute cache_shared_cpu_list_attr =
709 __ATTR(shared_cpu_list, 0444, shared_cpu_list_show, NULL);
711 /* Attributes which should always be created -- the kobject/sysfs core
712 * does this automatically via kobj_type->default_groups. This is the
713 * minimum data required to uniquely identify a cache.
715 static struct attribute *cache_index_default_attrs[] = {
716 &cache_type_attr.attr,
717 &cache_level_attr.attr,
718 &cache_shared_cpu_map_attr.attr,
719 &cache_shared_cpu_list_attr.attr,
722 ATTRIBUTE_GROUPS(cache_index_default);
724 /* Attributes which should be created if the cache device node has the
725 * right properties -- see cacheinfo_create_index_opt_attrs
727 static struct kobj_attribute *cache_index_opt_attrs[] = {
729 &cache_line_size_attr,
734 static const struct sysfs_ops cache_index_ops = {
735 .show = cache_index_show,
738 static struct kobj_type cache_index_type = {
739 .release = cache_index_release,
740 .sysfs_ops = &cache_index_ops,
741 .default_groups = cache_index_default_groups,
744 static void cacheinfo_create_index_opt_attrs(struct cache_index_dir *dir)
746 const char *cache_type;
751 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
756 cache_type = cache_type_string(cache);
758 /* We don't want to create an attribute that can't provide a
759 * meaningful value. Check the return value of each optional
760 * attribute's ->show method before registering the
763 for (i = 0; i < ARRAY_SIZE(cache_index_opt_attrs); i++) {
764 struct kobj_attribute *attr;
767 attr = cache_index_opt_attrs[i];
769 rc = attr->show(&dir->kobj, attr, buf);
771 pr_debug("not creating %s attribute for "
772 "%pOFP(%s) (rc = %zd)\n",
773 attr->attr.name, cache->ofnode,
777 if (sysfs_create_file(&dir->kobj, &attr->attr))
778 pr_debug("could not create %s attribute for %pOFP(%s)\n",
779 attr->attr.name, cache->ofnode, cache_type);
785 static void cacheinfo_create_index_dir(struct cache *cache, int index,
786 struct cache_dir *cache_dir)
788 struct cache_index_dir *index_dir;
791 index_dir = kzalloc(sizeof(*index_dir), GFP_KERNEL);
795 index_dir->cache = cache;
797 rc = kobject_init_and_add(&index_dir->kobj, &cache_index_type,
798 cache_dir->kobj, "index%d", index);
800 kobject_put(&index_dir->kobj);
804 index_dir->next = cache_dir->index;
805 cache_dir->index = index_dir;
807 cacheinfo_create_index_opt_attrs(index_dir);
810 static void cacheinfo_sysfs_populate(unsigned int cpu_id,
811 struct cache *cache_list)
813 struct cache_dir *cache_dir;
817 cache_dir = cacheinfo_create_cache_dir(cpu_id);
823 cacheinfo_create_index_dir(cache, index, cache_dir);
825 cache = cache->next_local;
829 void cacheinfo_cpu_online(unsigned int cpu_id)
833 cache = cache_chain_instantiate(cpu_id);
837 cacheinfo_sysfs_populate(cpu_id, cache);
840 /* functions needed to remove cache entry for cpu offline or suspend/resume */
842 #if (defined(CONFIG_PPC_PSERIES) && defined(CONFIG_SUSPEND)) || \
843 defined(CONFIG_HOTPLUG_CPU)
845 static struct cache *cache_lookup_by_cpu(unsigned int cpu_id)
847 struct device_node *cpu_node;
851 cpu_node = of_get_cpu_node(cpu_id, NULL);
852 WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
856 group_id = get_group_id(cpu_id, 1);
857 cache = cache_lookup_by_node_group(cpu_node, group_id);
858 of_node_put(cpu_node);
863 static void remove_index_dirs(struct cache_dir *cache_dir)
865 struct cache_index_dir *index;
867 index = cache_dir->index;
870 struct cache_index_dir *next;
873 kobject_put(&index->kobj);
878 static void remove_cache_dir(struct cache_dir *cache_dir)
880 remove_index_dirs(cache_dir);
882 /* Remove cache dir from sysfs */
883 kobject_del(cache_dir->kobj);
885 kobject_put(cache_dir->kobj);
890 static void cache_cpu_clear(struct cache *cache, int cpu)
893 struct cache *next = cache->next_local;
895 WARN_ONCE(!cpumask_test_cpu(cpu, &cache->shared_cpu_map),
896 "CPU %i not accounted in %pOFP(%s)\n",
898 cache_type_string(cache));
900 cpumask_clear_cpu(cpu, &cache->shared_cpu_map);
902 /* Release the cache object if all the cpus using it
904 if (cpumask_empty(&cache->shared_cpu_map))
905 release_cache(cache);
911 void cacheinfo_cpu_offline(unsigned int cpu_id)
913 struct cache_dir *cache_dir;
916 /* Prevent userspace from seeing inconsistent state - remove
917 * the sysfs hierarchy first */
918 cache_dir = per_cpu(cache_dir_pcpu, cpu_id);
920 /* careful, sysfs population may have failed */
922 remove_cache_dir(cache_dir);
924 per_cpu(cache_dir_pcpu, cpu_id) = NULL;
926 /* clear the CPU's bit in its cache chain, possibly freeing
928 cache = cache_lookup_by_cpu(cpu_id);
930 cache_cpu_clear(cache, cpu_id);
933 void cacheinfo_teardown(void)
937 lockdep_assert_cpus_held();
939 for_each_online_cpu(cpu)
940 cacheinfo_cpu_offline(cpu);
943 void cacheinfo_rebuild(void)
947 lockdep_assert_cpus_held();
949 for_each_online_cpu(cpu)
950 cacheinfo_cpu_online(cpu);
953 #endif /* (CONFIG_PPC_PSERIES && CONFIG_SUSPEND) || CONFIG_HOTPLUG_CPU */