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 #include <linux/cpu.h>
11 #include <linux/cpumask.h>
12 #include <linux/kernel.h>
13 #include <linux/kobject.h>
14 #include <linux/list.h>
15 #include <linux/notifier.h>
17 #include <linux/percpu.h>
18 #include <linux/slab.h>
20 #include <asm/cputhreads.h>
23 #include "cacheinfo.h"
25 /* per-cpu object for tracking:
26 * - a "cache" kobject for the top-level directory
27 * - a list of "index" objects representing the cpu's local cache hierarchy
30 struct kobject
*kobj
; /* bare (not embedded) kobject for cache
32 struct cache_index_dir
*index
; /* list of index objects */
35 /* "index" object: each cpu's cache directory has an index
36 * subdirectory corresponding to a cache object associated with the
37 * cpu. This object's lifetime is managed via the embedded kobject.
39 struct cache_index_dir
{
41 struct cache_index_dir
*next
; /* next index in parent directory */
45 /* Template for determining which OF properties to query for a given
47 struct cache_type_info
{
49 const char *size_prop
;
51 /* Allow for both [di]-cache-line-size and
52 * [di]-cache-block-size properties. According to the PowerPC
53 * Processor binding, -line-size should be provided if it
54 * differs from the cache block size (that which is operated
55 * on by cache instructions), so we look for -line-size first.
56 * See cache_get_line_size(). */
58 const char *line_size_props
[2];
59 const char *nr_sets_prop
;
62 /* These are used to index the cache_type_info array. */
63 #define CACHE_TYPE_UNIFIED 0 /* cache-size, cache-block-size, etc. */
64 #define CACHE_TYPE_UNIFIED_D 1 /* d-cache-size, d-cache-block-size, etc */
65 #define CACHE_TYPE_INSTRUCTION 2
66 #define CACHE_TYPE_DATA 3
68 static const struct cache_type_info cache_type_info
[] = {
70 /* Embedded systems that use cache-size, cache-block-size,
71 * etc. for the Unified (typically L2) cache. */
73 .size_prop
= "cache-size",
74 .line_size_props
= { "cache-line-size",
75 "cache-block-size", },
76 .nr_sets_prop
= "cache-sets",
79 /* PowerPC Processor binding says the [di]-cache-*
80 * must be equal on unified caches, so just use
81 * d-cache properties. */
83 .size_prop
= "d-cache-size",
84 .line_size_props
= { "d-cache-line-size",
85 "d-cache-block-size", },
86 .nr_sets_prop
= "d-cache-sets",
89 .name
= "Instruction",
90 .size_prop
= "i-cache-size",
91 .line_size_props
= { "i-cache-line-size",
92 "i-cache-block-size", },
93 .nr_sets_prop
= "i-cache-sets",
97 .size_prop
= "d-cache-size",
98 .line_size_props
= { "d-cache-line-size",
99 "d-cache-block-size", },
100 .nr_sets_prop
= "d-cache-sets",
104 /* Cache object: each instance of this corresponds to a distinct cache
105 * in the system. There are separate objects for Harvard caches: one
106 * each for instruction and data, and each refers to the same OF node.
107 * The refcount of the OF node is elevated for the lifetime of the
108 * cache object. A cache object is released when its shared_cpu_map
109 * is cleared (see cache_cpu_clear).
111 * A cache object is on two lists: an unsorted global list
112 * (cache_list) of cache objects; and a singly-linked list
113 * representing the local cache hierarchy, which is ordered by level
114 * (e.g. L1d -> L1i -> L2 -> L3).
117 struct device_node
*ofnode
; /* OF node for this cache, may be cpu */
118 struct cpumask shared_cpu_map
; /* online CPUs using this cache */
119 int type
; /* split cache disambiguation */
120 int level
; /* level not explicit in device tree */
121 struct list_head list
; /* global list of cache objects */
122 struct cache
*next_local
; /* next cache of >= level */
125 static DEFINE_PER_CPU(struct cache_dir
*, cache_dir_pcpu
);
127 /* traversal/modification of this list occurs only at cpu hotplug time;
128 * access is serialized by cpu hotplug locking
130 static LIST_HEAD(cache_list
);
132 static struct cache_index_dir
*kobj_to_cache_index_dir(struct kobject
*k
)
134 return container_of(k
, struct cache_index_dir
, kobj
);
137 static const char *cache_type_string(const struct cache
*cache
)
139 return cache_type_info
[cache
->type
].name
;
142 static void cache_init(struct cache
*cache
, int type
, int level
,
143 struct device_node
*ofnode
)
146 cache
->level
= level
;
147 cache
->ofnode
= of_node_get(ofnode
);
148 INIT_LIST_HEAD(&cache
->list
);
149 list_add(&cache
->list
, &cache_list
);
152 static struct cache
*new_cache(int type
, int level
, struct device_node
*ofnode
)
156 cache
= kzalloc(sizeof(*cache
), GFP_KERNEL
);
158 cache_init(cache
, type
, level
, ofnode
);
163 static void release_cache_debugcheck(struct cache
*cache
)
167 list_for_each_entry(iter
, &cache_list
, list
)
168 WARN_ONCE(iter
->next_local
== cache
,
169 "cache for %pOF(%s) refers to cache for %pOF(%s)\n",
171 cache_type_string(iter
),
173 cache_type_string(cache
));
176 static void release_cache(struct cache
*cache
)
181 pr_debug("freeing L%d %s cache for %pOF\n", cache
->level
,
182 cache_type_string(cache
), cache
->ofnode
);
184 release_cache_debugcheck(cache
);
185 list_del(&cache
->list
);
186 of_node_put(cache
->ofnode
);
190 static void cache_cpu_set(struct cache
*cache
, int cpu
)
192 struct cache
*next
= cache
;
195 WARN_ONCE(cpumask_test_cpu(cpu
, &next
->shared_cpu_map
),
196 "CPU %i already accounted in %pOF(%s)\n",
198 cache_type_string(next
));
199 cpumask_set_cpu(cpu
, &next
->shared_cpu_map
);
200 next
= next
->next_local
;
204 static int cache_size(const struct cache
*cache
, unsigned int *ret
)
206 const char *propname
;
207 const __be32
*cache_size
;
209 propname
= cache_type_info
[cache
->type
].size_prop
;
211 cache_size
= of_get_property(cache
->ofnode
, propname
, NULL
);
215 *ret
= of_read_number(cache_size
, 1);
219 static int cache_size_kb(const struct cache
*cache
, unsigned int *ret
)
223 if (cache_size(cache
, &size
))
230 /* not cache_line_size() because that's a macro in include/linux/cache.h */
231 static int cache_get_line_size(const struct cache
*cache
, unsigned int *ret
)
233 const __be32
*line_size
;
236 lim
= ARRAY_SIZE(cache_type_info
[cache
->type
].line_size_props
);
238 for (i
= 0; i
< lim
; i
++) {
239 const char *propname
;
241 propname
= cache_type_info
[cache
->type
].line_size_props
[i
];
242 line_size
= of_get_property(cache
->ofnode
, propname
, NULL
);
250 *ret
= of_read_number(line_size
, 1);
254 static int cache_nr_sets(const struct cache
*cache
, unsigned int *ret
)
256 const char *propname
;
257 const __be32
*nr_sets
;
259 propname
= cache_type_info
[cache
->type
].nr_sets_prop
;
261 nr_sets
= of_get_property(cache
->ofnode
, propname
, NULL
);
265 *ret
= of_read_number(nr_sets
, 1);
269 static int cache_associativity(const struct cache
*cache
, unsigned int *ret
)
271 unsigned int line_size
;
272 unsigned int nr_sets
;
275 if (cache_nr_sets(cache
, &nr_sets
))
278 /* If the cache is fully associative, there is no need to
279 * check the other properties.
286 if (cache_get_line_size(cache
, &line_size
))
288 if (cache_size(cache
, &size
))
291 if (!(nr_sets
> 0 && size
> 0 && line_size
> 0))
294 *ret
= (size
/ nr_sets
) / line_size
;
300 /* helper for dealing with split caches */
301 static struct cache
*cache_find_first_sibling(struct cache
*cache
)
305 if (cache
->type
== CACHE_TYPE_UNIFIED
||
306 cache
->type
== CACHE_TYPE_UNIFIED_D
)
309 list_for_each_entry(iter
, &cache_list
, list
)
310 if (iter
->ofnode
== cache
->ofnode
&& iter
->next_local
== cache
)
316 /* return the first cache on a local list matching node */
317 static struct cache
*cache_lookup_by_node(const struct device_node
*node
)
319 struct cache
*cache
= NULL
;
322 list_for_each_entry(iter
, &cache_list
, list
) {
323 if (iter
->ofnode
!= node
)
325 cache
= cache_find_first_sibling(iter
);
332 static bool cache_node_is_unified(const struct device_node
*np
)
334 return of_get_property(np
, "cache-unified", NULL
);
338 * Unified caches can have two different sets of tags. Most embedded
339 * use cache-size, etc. for the unified cache size, but open firmware systems
340 * use d-cache-size, etc. Check on initialization for which type we have, and
341 * return the appropriate structure type. Assume it's embedded if it isn't
342 * open firmware. If it's yet a 3rd type, then there will be missing entries
343 * in /sys/devices/system/cpu/cpu0/cache/index2/, and this code will need
344 * to be extended further.
346 static int cache_is_unified_d(const struct device_node
*np
)
348 return of_get_property(np
,
349 cache_type_info
[CACHE_TYPE_UNIFIED_D
].size_prop
, NULL
) ?
350 CACHE_TYPE_UNIFIED_D
: CACHE_TYPE_UNIFIED
;
353 static struct cache
*cache_do_one_devnode_unified(struct device_node
*node
, int level
)
355 pr_debug("creating L%d ucache for %pOF\n", level
, node
);
357 return new_cache(cache_is_unified_d(node
), level
, node
);
360 static struct cache
*cache_do_one_devnode_split(struct device_node
*node
,
363 struct cache
*dcache
, *icache
;
365 pr_debug("creating L%d dcache and icache for %pOF\n", level
,
368 dcache
= new_cache(CACHE_TYPE_DATA
, level
, node
);
369 icache
= new_cache(CACHE_TYPE_INSTRUCTION
, level
, node
);
371 if (!dcache
|| !icache
)
374 dcache
->next_local
= icache
;
378 release_cache(dcache
);
379 release_cache(icache
);
383 static struct cache
*cache_do_one_devnode(struct device_node
*node
, int level
)
387 if (cache_node_is_unified(node
))
388 cache
= cache_do_one_devnode_unified(node
, level
);
390 cache
= cache_do_one_devnode_split(node
, level
);
395 static struct cache
*cache_lookup_or_instantiate(struct device_node
*node
,
400 cache
= cache_lookup_by_node(node
);
402 WARN_ONCE(cache
&& cache
->level
!= level
,
403 "cache level mismatch on lookup (got %d, expected %d)\n",
404 cache
->level
, level
);
407 cache
= cache_do_one_devnode(node
, level
);
412 static void link_cache_lists(struct cache
*smaller
, struct cache
*bigger
)
414 while (smaller
->next_local
) {
415 if (smaller
->next_local
== bigger
)
416 return; /* already linked */
417 smaller
= smaller
->next_local
;
420 smaller
->next_local
= bigger
;
423 static void do_subsidiary_caches_debugcheck(struct cache
*cache
)
425 WARN_ON_ONCE(cache
->level
!= 1);
426 WARN_ON_ONCE(!of_node_is_type(cache
->ofnode
, "cpu"));
429 static void do_subsidiary_caches(struct cache
*cache
)
431 struct device_node
*subcache_node
;
432 int level
= cache
->level
;
434 do_subsidiary_caches_debugcheck(cache
);
436 while ((subcache_node
= of_find_next_cache_node(cache
->ofnode
))) {
437 struct cache
*subcache
;
440 subcache
= cache_lookup_or_instantiate(subcache_node
, level
);
441 of_node_put(subcache_node
);
445 link_cache_lists(cache
, subcache
);
450 static struct cache
*cache_chain_instantiate(unsigned int cpu_id
)
452 struct device_node
*cpu_node
;
453 struct cache
*cpu_cache
= NULL
;
455 pr_debug("creating cache object(s) for CPU %i\n", cpu_id
);
457 cpu_node
= of_get_cpu_node(cpu_id
, NULL
);
458 WARN_ONCE(!cpu_node
, "no OF node found for CPU %i\n", cpu_id
);
462 cpu_cache
= cache_lookup_or_instantiate(cpu_node
, 1);
466 do_subsidiary_caches(cpu_cache
);
468 cache_cpu_set(cpu_cache
, cpu_id
);
470 of_node_put(cpu_node
);
475 static struct cache_dir
*cacheinfo_create_cache_dir(unsigned int cpu_id
)
477 struct cache_dir
*cache_dir
;
479 struct kobject
*kobj
= NULL
;
481 dev
= get_cpu_device(cpu_id
);
482 WARN_ONCE(!dev
, "no dev for CPU %i\n", cpu_id
);
486 kobj
= kobject_create_and_add("cache", &dev
->kobj
);
490 cache_dir
= kzalloc(sizeof(*cache_dir
), GFP_KERNEL
);
494 cache_dir
->kobj
= kobj
;
496 WARN_ON_ONCE(per_cpu(cache_dir_pcpu
, cpu_id
) != NULL
);
498 per_cpu(cache_dir_pcpu
, cpu_id
) = cache_dir
;
506 static void cache_index_release(struct kobject
*kobj
)
508 struct cache_index_dir
*index
;
510 index
= kobj_to_cache_index_dir(kobj
);
512 pr_debug("freeing index directory for L%d %s cache\n",
513 index
->cache
->level
, cache_type_string(index
->cache
));
518 static ssize_t
cache_index_show(struct kobject
*k
, struct attribute
*attr
, char *buf
)
520 struct kobj_attribute
*kobj_attr
;
522 kobj_attr
= container_of(attr
, struct kobj_attribute
, attr
);
524 return kobj_attr
->show(k
, kobj_attr
, buf
);
527 static struct cache
*index_kobj_to_cache(struct kobject
*k
)
529 struct cache_index_dir
*index
;
531 index
= kobj_to_cache_index_dir(k
);
536 static ssize_t
size_show(struct kobject
*k
, struct kobj_attribute
*attr
, char *buf
)
538 unsigned int size_kb
;
541 cache
= index_kobj_to_cache(k
);
543 if (cache_size_kb(cache
, &size_kb
))
546 return sprintf(buf
, "%uK\n", size_kb
);
549 static struct kobj_attribute cache_size_attr
=
550 __ATTR(size
, 0444, size_show
, NULL
);
553 static ssize_t
line_size_show(struct kobject
*k
, struct kobj_attribute
*attr
, char *buf
)
555 unsigned int line_size
;
558 cache
= index_kobj_to_cache(k
);
560 if (cache_get_line_size(cache
, &line_size
))
563 return sprintf(buf
, "%u\n", line_size
);
566 static struct kobj_attribute cache_line_size_attr
=
567 __ATTR(coherency_line_size
, 0444, line_size_show
, NULL
);
569 static ssize_t
nr_sets_show(struct kobject
*k
, struct kobj_attribute
*attr
, char *buf
)
571 unsigned int nr_sets
;
574 cache
= index_kobj_to_cache(k
);
576 if (cache_nr_sets(cache
, &nr_sets
))
579 return sprintf(buf
, "%u\n", nr_sets
);
582 static struct kobj_attribute cache_nr_sets_attr
=
583 __ATTR(number_of_sets
, 0444, nr_sets_show
, NULL
);
585 static ssize_t
associativity_show(struct kobject
*k
, struct kobj_attribute
*attr
, char *buf
)
587 unsigned int associativity
;
590 cache
= index_kobj_to_cache(k
);
592 if (cache_associativity(cache
, &associativity
))
595 return sprintf(buf
, "%u\n", associativity
);
598 static struct kobj_attribute cache_assoc_attr
=
599 __ATTR(ways_of_associativity
, 0444, associativity_show
, NULL
);
601 static ssize_t
type_show(struct kobject
*k
, struct kobj_attribute
*attr
, char *buf
)
605 cache
= index_kobj_to_cache(k
);
607 return sprintf(buf
, "%s\n", cache_type_string(cache
));
610 static struct kobj_attribute cache_type_attr
=
611 __ATTR(type
, 0444, type_show
, NULL
);
613 static ssize_t
level_show(struct kobject
*k
, struct kobj_attribute
*attr
, char *buf
)
615 struct cache_index_dir
*index
;
618 index
= kobj_to_cache_index_dir(k
);
619 cache
= index
->cache
;
621 return sprintf(buf
, "%d\n", cache
->level
);
624 static struct kobj_attribute cache_level_attr
=
625 __ATTR(level
, 0444, level_show
, NULL
);
627 static unsigned int index_dir_to_cpu(struct cache_index_dir
*index
)
629 struct kobject
*index_dir_kobj
= &index
->kobj
;
630 struct kobject
*cache_dir_kobj
= index_dir_kobj
->parent
;
631 struct kobject
*cpu_dev_kobj
= cache_dir_kobj
->parent
;
632 struct device
*dev
= kobj_to_dev(cpu_dev_kobj
);
638 * On big-core systems, each core has two groups of CPUs each of which
639 * has its own L1-cache. The thread-siblings which share l1-cache with
640 * @cpu can be obtained via cpu_smallcore_mask().
642 static const struct cpumask
*get_big_core_shared_cpu_map(int cpu
, struct cache
*cache
)
644 if (cache
->level
== 1)
645 return cpu_smallcore_mask(cpu
);
647 return &cache
->shared_cpu_map
;
650 static ssize_t
shared_cpu_map_show(struct kobject
*k
, struct kobj_attribute
*attr
, char *buf
)
652 struct cache_index_dir
*index
;
654 const struct cpumask
*mask
;
657 index
= kobj_to_cache_index_dir(k
);
658 cache
= index
->cache
;
661 cpu
= index_dir_to_cpu(index
);
662 mask
= get_big_core_shared_cpu_map(cpu
, cache
);
664 mask
= &cache
->shared_cpu_map
;
667 ret
= scnprintf(buf
, PAGE_SIZE
- 1, "%*pb\n",
668 cpumask_pr_args(mask
));
674 static struct kobj_attribute cache_shared_cpu_map_attr
=
675 __ATTR(shared_cpu_map
, 0444, shared_cpu_map_show
, NULL
);
677 /* Attributes which should always be created -- the kobject/sysfs core
678 * does this automatically via kobj_type->default_attrs. This is the
679 * minimum data required to uniquely identify a cache.
681 static struct attribute
*cache_index_default_attrs
[] = {
682 &cache_type_attr
.attr
,
683 &cache_level_attr
.attr
,
684 &cache_shared_cpu_map_attr
.attr
,
688 /* Attributes which should be created if the cache device node has the
689 * right properties -- see cacheinfo_create_index_opt_attrs
691 static struct kobj_attribute
*cache_index_opt_attrs
[] = {
693 &cache_line_size_attr
,
698 static const struct sysfs_ops cache_index_ops
= {
699 .show
= cache_index_show
,
702 static struct kobj_type cache_index_type
= {
703 .release
= cache_index_release
,
704 .sysfs_ops
= &cache_index_ops
,
705 .default_attrs
= cache_index_default_attrs
,
708 static void cacheinfo_create_index_opt_attrs(struct cache_index_dir
*dir
)
710 const char *cache_type
;
715 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
720 cache_type
= cache_type_string(cache
);
722 /* We don't want to create an attribute that can't provide a
723 * meaningful value. Check the return value of each optional
724 * attribute's ->show method before registering the
727 for (i
= 0; i
< ARRAY_SIZE(cache_index_opt_attrs
); i
++) {
728 struct kobj_attribute
*attr
;
731 attr
= cache_index_opt_attrs
[i
];
733 rc
= attr
->show(&dir
->kobj
, attr
, buf
);
735 pr_debug("not creating %s attribute for "
736 "%pOF(%s) (rc = %zd)\n",
737 attr
->attr
.name
, cache
->ofnode
,
741 if (sysfs_create_file(&dir
->kobj
, &attr
->attr
))
742 pr_debug("could not create %s attribute for %pOF(%s)\n",
743 attr
->attr
.name
, cache
->ofnode
, cache_type
);
749 static void cacheinfo_create_index_dir(struct cache
*cache
, int index
,
750 struct cache_dir
*cache_dir
)
752 struct cache_index_dir
*index_dir
;
755 index_dir
= kzalloc(sizeof(*index_dir
), GFP_KERNEL
);
759 index_dir
->cache
= cache
;
761 rc
= kobject_init_and_add(&index_dir
->kobj
, &cache_index_type
,
762 cache_dir
->kobj
, "index%d", index
);
764 kobject_put(&index_dir
->kobj
);
768 index_dir
->next
= cache_dir
->index
;
769 cache_dir
->index
= index_dir
;
771 cacheinfo_create_index_opt_attrs(index_dir
);
774 static void cacheinfo_sysfs_populate(unsigned int cpu_id
,
775 struct cache
*cache_list
)
777 struct cache_dir
*cache_dir
;
781 cache_dir
= cacheinfo_create_cache_dir(cpu_id
);
787 cacheinfo_create_index_dir(cache
, index
, cache_dir
);
789 cache
= cache
->next_local
;
793 void cacheinfo_cpu_online(unsigned int cpu_id
)
797 cache
= cache_chain_instantiate(cpu_id
);
801 cacheinfo_sysfs_populate(cpu_id
, cache
);
804 /* functions needed to remove cache entry for cpu offline or suspend/resume */
806 #if (defined(CONFIG_PPC_PSERIES) && defined(CONFIG_SUSPEND)) || \
807 defined(CONFIG_HOTPLUG_CPU)
809 static struct cache
*cache_lookup_by_cpu(unsigned int cpu_id
)
811 struct device_node
*cpu_node
;
814 cpu_node
= of_get_cpu_node(cpu_id
, NULL
);
815 WARN_ONCE(!cpu_node
, "no OF node found for CPU %i\n", cpu_id
);
819 cache
= cache_lookup_by_node(cpu_node
);
820 of_node_put(cpu_node
);
825 static void remove_index_dirs(struct cache_dir
*cache_dir
)
827 struct cache_index_dir
*index
;
829 index
= cache_dir
->index
;
832 struct cache_index_dir
*next
;
835 kobject_put(&index
->kobj
);
840 static void remove_cache_dir(struct cache_dir
*cache_dir
)
842 remove_index_dirs(cache_dir
);
844 /* Remove cache dir from sysfs */
845 kobject_del(cache_dir
->kobj
);
847 kobject_put(cache_dir
->kobj
);
852 static void cache_cpu_clear(struct cache
*cache
, int cpu
)
855 struct cache
*next
= cache
->next_local
;
857 WARN_ONCE(!cpumask_test_cpu(cpu
, &cache
->shared_cpu_map
),
858 "CPU %i not accounted in %pOF(%s)\n",
860 cache_type_string(cache
));
862 cpumask_clear_cpu(cpu
, &cache
->shared_cpu_map
);
864 /* Release the cache object if all the cpus using it
866 if (cpumask_empty(&cache
->shared_cpu_map
))
867 release_cache(cache
);
873 void cacheinfo_cpu_offline(unsigned int cpu_id
)
875 struct cache_dir
*cache_dir
;
878 /* Prevent userspace from seeing inconsistent state - remove
879 * the sysfs hierarchy first */
880 cache_dir
= per_cpu(cache_dir_pcpu
, cpu_id
);
882 /* careful, sysfs population may have failed */
884 remove_cache_dir(cache_dir
);
886 per_cpu(cache_dir_pcpu
, cpu_id
) = NULL
;
888 /* clear the CPU's bit in its cache chain, possibly freeing
890 cache
= cache_lookup_by_cpu(cpu_id
);
892 cache_cpu_clear(cache
, cpu_id
);
895 void cacheinfo_teardown(void)
899 lockdep_assert_cpus_held();
901 for_each_online_cpu(cpu
)
902 cacheinfo_cpu_offline(cpu
);
905 void cacheinfo_rebuild(void)
909 lockdep_assert_cpus_held();
911 for_each_online_cpu(cpu
)
912 cacheinfo_cpu_online(cpu
);
915 #endif /* (CONFIG_PPC_PSERIES && CONFIG_SUSPEND) || CONFIG_HOTPLUG_CPU */