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>
22 #include <asm/cputhreads.h>
25 #include "cacheinfo.h"
27 /* per-cpu object for tracking:
28 * - a "cache" kobject for the top-level directory
29 * - a list of "index" objects representing the cpu's local cache hierarchy
32 struct kobject *kobj; /* bare (not embedded) kobject for cache
34 struct cache_index_dir *index; /* list of index objects */
37 /* "index" object: each cpu's cache directory has an index
38 * subdirectory corresponding to a cache object associated with the
39 * cpu. This object's lifetime is managed via the embedded kobject.
41 struct cache_index_dir {
43 struct cache_index_dir *next; /* next index in parent directory */
47 /* Template for determining which OF properties to query for a given
49 struct cache_type_info {
51 const char *size_prop;
53 /* Allow for both [di]-cache-line-size and
54 * [di]-cache-block-size properties. According to the PowerPC
55 * Processor binding, -line-size should be provided if it
56 * differs from the cache block size (that which is operated
57 * on by cache instructions), so we look for -line-size first.
58 * See cache_get_line_size(). */
60 const char *line_size_props[2];
61 const char *nr_sets_prop;
64 /* These are used to index the cache_type_info array. */
65 #define CACHE_TYPE_UNIFIED 0 /* cache-size, cache-block-size, etc. */
66 #define CACHE_TYPE_UNIFIED_D 1 /* d-cache-size, d-cache-block-size, etc */
67 #define CACHE_TYPE_INSTRUCTION 2
68 #define CACHE_TYPE_DATA 3
70 static const struct cache_type_info cache_type_info[] = {
72 /* Embedded systems that use cache-size, cache-block-size,
73 * etc. for the Unified (typically L2) cache. */
75 .size_prop = "cache-size",
76 .line_size_props = { "cache-line-size",
77 "cache-block-size", },
78 .nr_sets_prop = "cache-sets",
81 /* PowerPC Processor binding says the [di]-cache-*
82 * must be equal on unified caches, so just use
83 * d-cache properties. */
85 .size_prop = "d-cache-size",
86 .line_size_props = { "d-cache-line-size",
87 "d-cache-block-size", },
88 .nr_sets_prop = "d-cache-sets",
91 .name = "Instruction",
92 .size_prop = "i-cache-size",
93 .line_size_props = { "i-cache-line-size",
94 "i-cache-block-size", },
95 .nr_sets_prop = "i-cache-sets",
99 .size_prop = "d-cache-size",
100 .line_size_props = { "d-cache-line-size",
101 "d-cache-block-size", },
102 .nr_sets_prop = "d-cache-sets",
106 /* Cache object: each instance of this corresponds to a distinct cache
107 * in the system. There are separate objects for Harvard caches: one
108 * each for instruction and data, and each refers to the same OF node.
109 * The refcount of the OF node is elevated for the lifetime of the
110 * cache object. A cache object is released when its shared_cpu_map
111 * is cleared (see cache_cpu_clear).
113 * A cache object is on two lists: an unsorted global list
114 * (cache_list) of cache objects; and a singly-linked list
115 * representing the local cache hierarchy, which is ordered by level
116 * (e.g. L1d -> L1i -> L2 -> L3).
119 struct device_node *ofnode; /* OF node for this cache, may be cpu */
120 struct cpumask shared_cpu_map; /* online CPUs using this cache */
121 int type; /* split cache disambiguation */
122 int level; /* level not explicit in device tree */
123 int group_id; /* id of the group of threads that share this cache */
124 struct list_head list; /* global list of cache objects */
125 struct cache *next_local; /* next cache of >= level */
128 static DEFINE_PER_CPU(struct cache_dir *, cache_dir_pcpu);
130 /* traversal/modification of this list occurs only at cpu hotplug time;
131 * access is serialized by cpu hotplug locking
133 static LIST_HEAD(cache_list);
135 static struct cache_index_dir *kobj_to_cache_index_dir(struct kobject *k)
137 return container_of(k, struct cache_index_dir, kobj);
140 static const char *cache_type_string(const struct cache *cache)
142 return cache_type_info[cache->type].name;
145 static void cache_init(struct cache *cache, int type, int level,
146 struct device_node *ofnode, int group_id)
149 cache->level = level;
150 cache->ofnode = of_node_get(ofnode);
151 cache->group_id = group_id;
152 INIT_LIST_HEAD(&cache->list);
153 list_add(&cache->list, &cache_list);
156 static struct cache *new_cache(int type, int level,
157 struct device_node *ofnode, int group_id)
161 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
163 cache_init(cache, type, level, ofnode, group_id);
168 static void release_cache_debugcheck(struct cache *cache)
172 list_for_each_entry(iter, &cache_list, list)
173 WARN_ONCE(iter->next_local == cache,
174 "cache for %pOFP(%s) refers to cache for %pOFP(%s)\n",
176 cache_type_string(iter),
178 cache_type_string(cache));
181 static void release_cache(struct cache *cache)
186 pr_debug("freeing L%d %s cache for %pOFP\n", cache->level,
187 cache_type_string(cache), cache->ofnode);
189 release_cache_debugcheck(cache);
190 list_del(&cache->list);
191 of_node_put(cache->ofnode);
195 static void cache_cpu_set(struct cache *cache, int cpu)
197 struct cache *next = cache;
200 WARN_ONCE(cpumask_test_cpu(cpu, &next->shared_cpu_map),
201 "CPU %i already accounted in %pOFP(%s)\n",
203 cache_type_string(next));
204 cpumask_set_cpu(cpu, &next->shared_cpu_map);
205 next = next->next_local;
209 static int cache_size(const struct cache *cache, unsigned int *ret)
211 const char *propname;
212 const __be32 *cache_size;
214 propname = cache_type_info[cache->type].size_prop;
216 cache_size = of_get_property(cache->ofnode, propname, NULL);
220 *ret = of_read_number(cache_size, 1);
224 static int cache_size_kb(const struct cache *cache, unsigned int *ret)
228 if (cache_size(cache, &size))
235 /* not cache_line_size() because that's a macro in include/linux/cache.h */
236 static int cache_get_line_size(const struct cache *cache, unsigned int *ret)
238 const __be32 *line_size;
241 lim = ARRAY_SIZE(cache_type_info[cache->type].line_size_props);
243 for (i = 0; i < lim; i++) {
244 const char *propname;
246 propname = cache_type_info[cache->type].line_size_props[i];
247 line_size = of_get_property(cache->ofnode, propname, NULL);
255 *ret = of_read_number(line_size, 1);
259 static int cache_nr_sets(const struct cache *cache, unsigned int *ret)
261 const char *propname;
262 const __be32 *nr_sets;
264 propname = cache_type_info[cache->type].nr_sets_prop;
266 nr_sets = of_get_property(cache->ofnode, propname, NULL);
270 *ret = of_read_number(nr_sets, 1);
274 static int cache_associativity(const struct cache *cache, unsigned int *ret)
276 unsigned int line_size;
277 unsigned int nr_sets;
280 if (cache_nr_sets(cache, &nr_sets))
283 /* If the cache is fully associative, there is no need to
284 * check the other properties.
291 if (cache_get_line_size(cache, &line_size))
293 if (cache_size(cache, &size))
296 if (!(nr_sets > 0 && size > 0 && line_size > 0))
299 *ret = (size / nr_sets) / line_size;
305 /* helper for dealing with split caches */
306 static struct cache *cache_find_first_sibling(struct cache *cache)
310 if (cache->type == CACHE_TYPE_UNIFIED ||
311 cache->type == CACHE_TYPE_UNIFIED_D)
314 list_for_each_entry(iter, &cache_list, list)
315 if (iter->ofnode == cache->ofnode &&
316 iter->group_id == cache->group_id &&
317 iter->next_local == cache)
323 /* return the first cache on a local list matching node and thread-group id */
324 static struct cache *cache_lookup_by_node_group(const struct device_node *node,
327 struct cache *cache = NULL;
330 list_for_each_entry(iter, &cache_list, list) {
331 if (iter->ofnode != node ||
332 iter->group_id != group_id)
334 cache = cache_find_first_sibling(iter);
341 static bool cache_node_is_unified(const struct device_node *np)
343 return of_get_property(np, "cache-unified", NULL);
347 * Unified caches can have two different sets of tags. Most embedded
348 * use cache-size, etc. for the unified cache size, but open firmware systems
349 * use d-cache-size, etc. Check on initialization for which type we have, and
350 * return the appropriate structure type. Assume it's embedded if it isn't
351 * open firmware. If it's yet a 3rd type, then there will be missing entries
352 * in /sys/devices/system/cpu/cpu0/cache/index2/, and this code will need
353 * to be extended further.
355 static int cache_is_unified_d(const struct device_node *np)
357 return of_get_property(np,
358 cache_type_info[CACHE_TYPE_UNIFIED_D].size_prop, NULL) ?
359 CACHE_TYPE_UNIFIED_D : CACHE_TYPE_UNIFIED;
362 static struct cache *cache_do_one_devnode_unified(struct device_node *node, int group_id,
365 pr_debug("creating L%d ucache for %pOFP\n", level, node);
367 return new_cache(cache_is_unified_d(node), level, node, group_id);
370 static struct cache *cache_do_one_devnode_split(struct device_node *node, int group_id,
373 struct cache *dcache, *icache;
375 pr_debug("creating L%d dcache and icache for %pOFP\n", level,
378 dcache = new_cache(CACHE_TYPE_DATA, level, node, group_id);
379 icache = new_cache(CACHE_TYPE_INSTRUCTION, level, node, group_id);
381 if (!dcache || !icache)
384 dcache->next_local = icache;
388 release_cache(dcache);
389 release_cache(icache);
393 static struct cache *cache_do_one_devnode(struct device_node *node, int group_id, int level)
397 if (cache_node_is_unified(node))
398 cache = cache_do_one_devnode_unified(node, group_id, level);
400 cache = cache_do_one_devnode_split(node, group_id, level);
405 static struct cache *cache_lookup_or_instantiate(struct device_node *node,
411 cache = cache_lookup_by_node_group(node, group_id);
413 WARN_ONCE(cache && cache->level != level,
414 "cache level mismatch on lookup (got %d, expected %d)\n",
415 cache->level, level);
418 cache = cache_do_one_devnode(node, group_id, level);
423 static void link_cache_lists(struct cache *smaller, struct cache *bigger)
425 while (smaller->next_local) {
426 if (smaller->next_local == bigger)
427 return; /* already linked */
428 smaller = smaller->next_local;
431 smaller->next_local = bigger;
434 * The cache->next_local list sorts by level ascending:
435 * L1d -> L1i -> L2 -> L3 ...
437 WARN_ONCE((smaller->level == 1 && bigger->level > 2) ||
438 (smaller->level > 1 && bigger->level != smaller->level + 1),
439 "linking L%i cache %pOFP to L%i cache %pOFP; skipped a level?\n",
440 smaller->level, smaller->ofnode, bigger->level, bigger->ofnode);
443 static void do_subsidiary_caches_debugcheck(struct cache *cache)
445 WARN_ONCE(cache->level != 1,
446 "instantiating cache chain from L%d %s cache for "
447 "%pOFP instead of an L1\n", cache->level,
448 cache_type_string(cache), cache->ofnode);
449 WARN_ONCE(!of_node_is_type(cache->ofnode, "cpu"),
450 "instantiating cache chain from node %pOFP of type '%s' "
451 "instead of a cpu node\n", cache->ofnode,
452 of_node_get_device_type(cache->ofnode));
456 * If sub-groups of threads in a core containing @cpu_id share the
457 * L@level-cache (information obtained via "ibm,thread-groups"
458 * device-tree property), then we identify the group by the first
459 * thread-sibling in the group. We define this to be the group-id.
461 * In the absence of any thread-group information for L@level-cache,
462 * this function returns -1.
464 static int get_group_id(unsigned int cpu_id, int level)
466 if (has_big_cores && level == 1)
467 return cpumask_first(per_cpu(thread_group_l1_cache_map,
469 else if (thread_group_shares_l2 && level == 2)
470 return cpumask_first(per_cpu(thread_group_l2_cache_map,
472 else if (thread_group_shares_l3 && level == 3)
473 return cpumask_first(per_cpu(thread_group_l3_cache_map,
478 static void do_subsidiary_caches(struct cache *cache, unsigned int cpu_id)
480 struct device_node *subcache_node;
481 int level = cache->level;
483 do_subsidiary_caches_debugcheck(cache);
485 while ((subcache_node = of_find_next_cache_node(cache->ofnode))) {
486 struct cache *subcache;
490 group_id = get_group_id(cpu_id, level);
491 subcache = cache_lookup_or_instantiate(subcache_node, group_id, level);
492 of_node_put(subcache_node);
496 link_cache_lists(cache, subcache);
501 static struct cache *cache_chain_instantiate(unsigned int cpu_id)
503 struct device_node *cpu_node;
504 struct cache *cpu_cache = NULL;
507 pr_debug("creating cache object(s) for CPU %i\n", cpu_id);
509 cpu_node = of_get_cpu_node(cpu_id, NULL);
510 WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
514 group_id = get_group_id(cpu_id, 1);
516 cpu_cache = cache_lookup_or_instantiate(cpu_node, group_id, 1);
520 do_subsidiary_caches(cpu_cache, cpu_id);
522 cache_cpu_set(cpu_cache, cpu_id);
524 of_node_put(cpu_node);
529 static struct cache_dir *cacheinfo_create_cache_dir(unsigned int cpu_id)
531 struct cache_dir *cache_dir;
533 struct kobject *kobj = NULL;
535 dev = get_cpu_device(cpu_id);
536 WARN_ONCE(!dev, "no dev for CPU %i\n", cpu_id);
540 kobj = kobject_create_and_add("cache", &dev->kobj);
544 cache_dir = kzalloc(sizeof(*cache_dir), GFP_KERNEL);
548 cache_dir->kobj = kobj;
550 WARN_ON_ONCE(per_cpu(cache_dir_pcpu, cpu_id) != NULL);
552 per_cpu(cache_dir_pcpu, cpu_id) = cache_dir;
560 static void cache_index_release(struct kobject *kobj)
562 struct cache_index_dir *index;
564 index = kobj_to_cache_index_dir(kobj);
566 pr_debug("freeing index directory for L%d %s cache\n",
567 index->cache->level, cache_type_string(index->cache));
572 static ssize_t cache_index_show(struct kobject *k, struct attribute *attr, char *buf)
574 struct kobj_attribute *kobj_attr;
576 kobj_attr = container_of(attr, struct kobj_attribute, attr);
578 return kobj_attr->show(k, kobj_attr, buf);
581 static struct cache *index_kobj_to_cache(struct kobject *k)
583 struct cache_index_dir *index;
585 index = kobj_to_cache_index_dir(k);
590 static ssize_t size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
592 unsigned int size_kb;
595 cache = index_kobj_to_cache(k);
597 if (cache_size_kb(cache, &size_kb))
600 return sprintf(buf, "%uK\n", size_kb);
603 static struct kobj_attribute cache_size_attr =
604 __ATTR(size, 0444, size_show, NULL);
607 static ssize_t line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
609 unsigned int line_size;
612 cache = index_kobj_to_cache(k);
614 if (cache_get_line_size(cache, &line_size))
617 return sprintf(buf, "%u\n", line_size);
620 static struct kobj_attribute cache_line_size_attr =
621 __ATTR(coherency_line_size, 0444, line_size_show, NULL);
623 static ssize_t nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
625 unsigned int nr_sets;
628 cache = index_kobj_to_cache(k);
630 if (cache_nr_sets(cache, &nr_sets))
633 return sprintf(buf, "%u\n", nr_sets);
636 static struct kobj_attribute cache_nr_sets_attr =
637 __ATTR(number_of_sets, 0444, nr_sets_show, NULL);
639 static ssize_t associativity_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
641 unsigned int associativity;
644 cache = index_kobj_to_cache(k);
646 if (cache_associativity(cache, &associativity))
649 return sprintf(buf, "%u\n", associativity);
652 static struct kobj_attribute cache_assoc_attr =
653 __ATTR(ways_of_associativity, 0444, associativity_show, NULL);
655 static ssize_t type_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
659 cache = index_kobj_to_cache(k);
661 return sprintf(buf, "%s\n", cache_type_string(cache));
664 static struct kobj_attribute cache_type_attr =
665 __ATTR(type, 0444, type_show, NULL);
667 static ssize_t level_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
669 struct cache_index_dir *index;
672 index = kobj_to_cache_index_dir(k);
673 cache = index->cache;
675 return sprintf(buf, "%d\n", cache->level);
678 static struct kobj_attribute cache_level_attr =
679 __ATTR(level, 0444, level_show, NULL);
682 show_shared_cpumap(struct kobject *k, struct kobj_attribute *attr, char *buf, bool list)
684 struct cache_index_dir *index;
686 const struct cpumask *mask;
688 index = kobj_to_cache_index_dir(k);
689 cache = index->cache;
691 mask = &cache->shared_cpu_map;
693 return cpumap_print_to_pagebuf(list, buf, mask);
696 static ssize_t shared_cpu_map_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
698 return show_shared_cpumap(k, attr, buf, false);
701 static ssize_t shared_cpu_list_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
703 return show_shared_cpumap(k, attr, buf, true);
706 static struct kobj_attribute cache_shared_cpu_map_attr =
707 __ATTR(shared_cpu_map, 0444, shared_cpu_map_show, NULL);
709 static struct kobj_attribute cache_shared_cpu_list_attr =
710 __ATTR(shared_cpu_list, 0444, shared_cpu_list_show, NULL);
712 /* Attributes which should always be created -- the kobject/sysfs core
713 * does this automatically via kobj_type->default_attrs. This is the
714 * minimum data required to uniquely identify a cache.
716 static struct attribute *cache_index_default_attrs[] = {
717 &cache_type_attr.attr,
718 &cache_level_attr.attr,
719 &cache_shared_cpu_map_attr.attr,
720 &cache_shared_cpu_list_attr.attr,
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_attrs = cache_index_default_attrs,
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 */