2 * Processor cache information made available to userspace via sysfs;
3 * intended to be compatible with x86 intel_cacheinfo implementation.
5 * Copyright 2008 IBM Corporation
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
13 #include <linux/cpu.h>
14 #include <linux/cpumask.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/kobject.h>
18 #include <linux/list.h>
19 #include <linux/notifier.h>
21 #include <linux/percpu.h>
22 #include <linux/slab.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 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 __cpuinit cache_init(struct cache *cache, int type, int level, struct device_node *ofnode)
147 cache->level = level;
148 cache->ofnode = of_node_get(ofnode);
149 INIT_LIST_HEAD(&cache->list);
150 list_add(&cache->list, &cache_list);
153 static struct cache *__cpuinit new_cache(int type, int level, struct device_node *ofnode)
157 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
159 cache_init(cache, type, level, ofnode);
164 static void release_cache_debugcheck(struct cache *cache)
168 list_for_each_entry(iter, &cache_list, list)
169 WARN_ONCE(iter->next_local == cache,
170 "cache for %s(%s) refers to cache for %s(%s)\n",
171 iter->ofnode->full_name,
172 cache_type_string(iter),
173 cache->ofnode->full_name,
174 cache_type_string(cache));
177 static void release_cache(struct cache *cache)
182 pr_debug("freeing L%d %s cache for %s\n", cache->level,
183 cache_type_string(cache), cache->ofnode->full_name);
185 release_cache_debugcheck(cache);
186 list_del(&cache->list);
187 of_node_put(cache->ofnode);
191 static void cache_cpu_set(struct cache *cache, int cpu)
193 struct cache *next = cache;
196 WARN_ONCE(cpumask_test_cpu(cpu, &next->shared_cpu_map),
197 "CPU %i already accounted in %s(%s)\n",
198 cpu, next->ofnode->full_name,
199 cache_type_string(next));
200 cpumask_set_cpu(cpu, &next->shared_cpu_map);
201 next = next->next_local;
205 static int cache_size(const struct cache *cache, unsigned int *ret)
207 const char *propname;
208 const u32 *cache_size;
210 propname = cache_type_info[cache->type].size_prop;
212 cache_size = of_get_property(cache->ofnode, propname, NULL);
220 static int cache_size_kb(const struct cache *cache, unsigned int *ret)
224 if (cache_size(cache, &size))
231 /* not cache_line_size() because that's a macro in include/linux/cache.h */
232 static int cache_get_line_size(const struct cache *cache, unsigned int *ret)
234 const u32 *line_size;
237 lim = ARRAY_SIZE(cache_type_info[cache->type].line_size_props);
239 for (i = 0; i < lim; i++) {
240 const char *propname;
242 propname = cache_type_info[cache->type].line_size_props[i];
243 line_size = of_get_property(cache->ofnode, propname, NULL);
255 static int cache_nr_sets(const struct cache *cache, unsigned int *ret)
257 const char *propname;
260 propname = cache_type_info[cache->type].nr_sets_prop;
262 nr_sets = of_get_property(cache->ofnode, propname, NULL);
270 static int cache_associativity(const struct cache *cache, unsigned int *ret)
272 unsigned int line_size;
273 unsigned int nr_sets;
276 if (cache_nr_sets(cache, &nr_sets))
279 /* If the cache is fully associative, there is no need to
280 * check the other properties.
287 if (cache_get_line_size(cache, &line_size))
289 if (cache_size(cache, &size))
292 if (!(nr_sets > 0 && size > 0 && line_size > 0))
295 *ret = (size / nr_sets) / line_size;
301 /* helper for dealing with split caches */
302 static struct cache *cache_find_first_sibling(struct cache *cache)
306 if (cache->type == CACHE_TYPE_UNIFIED ||
307 cache->type == CACHE_TYPE_UNIFIED_D)
310 list_for_each_entry(iter, &cache_list, list)
311 if (iter->ofnode == cache->ofnode && iter->next_local == cache)
317 /* return the first cache on a local list matching node */
318 static struct cache *cache_lookup_by_node(const struct device_node *node)
320 struct cache *cache = NULL;
323 list_for_each_entry(iter, &cache_list, list) {
324 if (iter->ofnode != node)
326 cache = cache_find_first_sibling(iter);
333 static bool cache_node_is_unified(const struct device_node *np)
335 return of_get_property(np, "cache-unified", NULL);
339 * Unified caches can have two different sets of tags. Most embedded
340 * use cache-size, etc. for the unified cache size, but open firmware systems
341 * use d-cache-size, etc. Check on initialization for which type we have, and
342 * return the appropriate structure type. Assume it's embedded if it isn't
343 * open firmware. If it's yet a 3rd type, then there will be missing entries
344 * in /sys/devices/system/cpu/cpu0/cache/index2/, and this code will need
345 * to be extended further.
347 static int cache_is_unified_d(const struct device_node *np)
349 return of_get_property(np,
350 cache_type_info[CACHE_TYPE_UNIFIED_D].size_prop, NULL) ?
351 CACHE_TYPE_UNIFIED_D : CACHE_TYPE_UNIFIED;
356 static struct cache *__cpuinit cache_do_one_devnode_unified(struct device_node *node, int level)
358 pr_debug("creating L%d ucache for %s\n", level, node->full_name);
360 return new_cache(cache_is_unified_d(node), level, node);
363 static struct cache *__cpuinit cache_do_one_devnode_split(struct device_node *node, int level)
365 struct cache *dcache, *icache;
367 pr_debug("creating L%d dcache and icache for %s\n", level,
370 dcache = new_cache(CACHE_TYPE_DATA, level, node);
371 icache = new_cache(CACHE_TYPE_INSTRUCTION, level, node);
373 if (!dcache || !icache)
376 dcache->next_local = icache;
380 release_cache(dcache);
381 release_cache(icache);
385 static struct cache *__cpuinit cache_do_one_devnode(struct device_node *node, int level)
389 if (cache_node_is_unified(node))
390 cache = cache_do_one_devnode_unified(node, level);
392 cache = cache_do_one_devnode_split(node, level);
397 static struct cache *__cpuinit cache_lookup_or_instantiate(struct device_node *node, int level)
401 cache = cache_lookup_by_node(node);
403 WARN_ONCE(cache && cache->level != level,
404 "cache level mismatch on lookup (got %d, expected %d)\n",
405 cache->level, level);
408 cache = cache_do_one_devnode(node, level);
413 static void __cpuinit link_cache_lists(struct cache *smaller, struct cache *bigger)
415 while (smaller->next_local) {
416 if (smaller->next_local == bigger)
417 return; /* already linked */
418 smaller = smaller->next_local;
421 smaller->next_local = bigger;
424 static void __cpuinit do_subsidiary_caches_debugcheck(struct cache *cache)
426 WARN_ON_ONCE(cache->level != 1);
427 WARN_ON_ONCE(strcmp(cache->ofnode->type, "cpu"));
430 static void __cpuinit do_subsidiary_caches(struct cache *cache)
432 struct device_node *subcache_node;
433 int level = cache->level;
435 do_subsidiary_caches_debugcheck(cache);
437 while ((subcache_node = of_find_next_cache_node(cache->ofnode))) {
438 struct cache *subcache;
441 subcache = cache_lookup_or_instantiate(subcache_node, level);
442 of_node_put(subcache_node);
446 link_cache_lists(cache, subcache);
451 static struct cache *__cpuinit cache_chain_instantiate(unsigned int cpu_id)
453 struct device_node *cpu_node;
454 struct cache *cpu_cache = NULL;
456 pr_debug("creating cache object(s) for CPU %i\n", cpu_id);
458 cpu_node = of_get_cpu_node(cpu_id, NULL);
459 WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
463 cpu_cache = cache_lookup_or_instantiate(cpu_node, 1);
467 do_subsidiary_caches(cpu_cache);
469 cache_cpu_set(cpu_cache, cpu_id);
471 of_node_put(cpu_node);
476 static struct cache_dir *__cpuinit cacheinfo_create_cache_dir(unsigned int cpu_id)
478 struct cache_dir *cache_dir;
479 struct sys_device *sysdev;
480 struct kobject *kobj = NULL;
482 sysdev = get_cpu_sysdev(cpu_id);
483 WARN_ONCE(!sysdev, "no sysdev for CPU %i\n", cpu_id);
487 kobj = kobject_create_and_add("cache", &sysdev->kobj);
491 cache_dir = kzalloc(sizeof(*cache_dir), GFP_KERNEL);
495 cache_dir->kobj = kobj;
497 WARN_ON_ONCE(per_cpu(cache_dir_pcpu, cpu_id) != NULL);
499 per_cpu(cache_dir_pcpu, cpu_id) = cache_dir;
507 static void cache_index_release(struct kobject *kobj)
509 struct cache_index_dir *index;
511 index = kobj_to_cache_index_dir(kobj);
513 pr_debug("freeing index directory for L%d %s cache\n",
514 index->cache->level, cache_type_string(index->cache));
519 static ssize_t cache_index_show(struct kobject *k, struct attribute *attr, char *buf)
521 struct kobj_attribute *kobj_attr;
523 kobj_attr = container_of(attr, struct kobj_attribute, attr);
525 return kobj_attr->show(k, kobj_attr, buf);
528 static struct cache *index_kobj_to_cache(struct kobject *k)
530 struct cache_index_dir *index;
532 index = kobj_to_cache_index_dir(k);
537 static ssize_t size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
539 unsigned int size_kb;
542 cache = index_kobj_to_cache(k);
544 if (cache_size_kb(cache, &size_kb))
547 return sprintf(buf, "%uK\n", size_kb);
550 static struct kobj_attribute cache_size_attr =
551 __ATTR(size, 0444, size_show, NULL);
554 static ssize_t line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
556 unsigned int line_size;
559 cache = index_kobj_to_cache(k);
561 if (cache_get_line_size(cache, &line_size))
564 return sprintf(buf, "%u\n", line_size);
567 static struct kobj_attribute cache_line_size_attr =
568 __ATTR(coherency_line_size, 0444, line_size_show, NULL);
570 static ssize_t nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
572 unsigned int nr_sets;
575 cache = index_kobj_to_cache(k);
577 if (cache_nr_sets(cache, &nr_sets))
580 return sprintf(buf, "%u\n", nr_sets);
583 static struct kobj_attribute cache_nr_sets_attr =
584 __ATTR(number_of_sets, 0444, nr_sets_show, NULL);
586 static ssize_t associativity_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
588 unsigned int associativity;
591 cache = index_kobj_to_cache(k);
593 if (cache_associativity(cache, &associativity))
596 return sprintf(buf, "%u\n", associativity);
599 static struct kobj_attribute cache_assoc_attr =
600 __ATTR(ways_of_associativity, 0444, associativity_show, NULL);
602 static ssize_t type_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
606 cache = index_kobj_to_cache(k);
608 return sprintf(buf, "%s\n", cache_type_string(cache));
611 static struct kobj_attribute cache_type_attr =
612 __ATTR(type, 0444, type_show, NULL);
614 static ssize_t level_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
616 struct cache_index_dir *index;
619 index = kobj_to_cache_index_dir(k);
620 cache = index->cache;
622 return sprintf(buf, "%d\n", cache->level);
625 static struct kobj_attribute cache_level_attr =
626 __ATTR(level, 0444, level_show, NULL);
628 static ssize_t shared_cpu_map_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
630 struct cache_index_dir *index;
635 index = kobj_to_cache_index_dir(k);
636 cache = index->cache;
640 n = cpumask_scnprintf(buf, len, &cache->shared_cpu_map);
647 static struct kobj_attribute cache_shared_cpu_map_attr =
648 __ATTR(shared_cpu_map, 0444, shared_cpu_map_show, NULL);
650 /* Attributes which should always be created -- the kobject/sysfs core
651 * does this automatically via kobj_type->default_attrs. This is the
652 * minimum data required to uniquely identify a cache.
654 static struct attribute *cache_index_default_attrs[] = {
655 &cache_type_attr.attr,
656 &cache_level_attr.attr,
657 &cache_shared_cpu_map_attr.attr,
661 /* Attributes which should be created if the cache device node has the
662 * right properties -- see cacheinfo_create_index_opt_attrs
664 static struct kobj_attribute *cache_index_opt_attrs[] = {
666 &cache_line_size_attr,
671 static const struct sysfs_ops cache_index_ops = {
672 .show = cache_index_show,
675 static struct kobj_type cache_index_type = {
676 .release = cache_index_release,
677 .sysfs_ops = &cache_index_ops,
678 .default_attrs = cache_index_default_attrs,
681 static void __cpuinit cacheinfo_create_index_opt_attrs(struct cache_index_dir *dir)
683 const char *cache_name;
684 const char *cache_type;
689 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
694 cache_name = cache->ofnode->full_name;
695 cache_type = cache_type_string(cache);
697 /* We don't want to create an attribute that can't provide a
698 * meaningful value. Check the return value of each optional
699 * attribute's ->show method before registering the
702 for (i = 0; i < ARRAY_SIZE(cache_index_opt_attrs); i++) {
703 struct kobj_attribute *attr;
706 attr = cache_index_opt_attrs[i];
708 rc = attr->show(&dir->kobj, attr, buf);
710 pr_debug("not creating %s attribute for "
711 "%s(%s) (rc = %zd)\n",
712 attr->attr.name, cache_name,
716 if (sysfs_create_file(&dir->kobj, &attr->attr))
717 pr_debug("could not create %s attribute for %s(%s)\n",
718 attr->attr.name, cache_name, cache_type);
724 static void __cpuinit cacheinfo_create_index_dir(struct cache *cache, int index, struct cache_dir *cache_dir)
726 struct cache_index_dir *index_dir;
729 index_dir = kzalloc(sizeof(*index_dir), GFP_KERNEL);
733 index_dir->cache = cache;
735 rc = kobject_init_and_add(&index_dir->kobj, &cache_index_type,
736 cache_dir->kobj, "index%d", index);
740 index_dir->next = cache_dir->index;
741 cache_dir->index = index_dir;
743 cacheinfo_create_index_opt_attrs(index_dir);
750 static void __cpuinit cacheinfo_sysfs_populate(unsigned int cpu_id, struct cache *cache_list)
752 struct cache_dir *cache_dir;
756 cache_dir = cacheinfo_create_cache_dir(cpu_id);
762 cacheinfo_create_index_dir(cache, index, cache_dir);
764 cache = cache->next_local;
768 void __cpuinit cacheinfo_cpu_online(unsigned int cpu_id)
772 cache = cache_chain_instantiate(cpu_id);
776 cacheinfo_sysfs_populate(cpu_id, cache);
779 #ifdef CONFIG_HOTPLUG_CPU /* functions needed for cpu offline */
781 static struct cache *cache_lookup_by_cpu(unsigned int cpu_id)
783 struct device_node *cpu_node;
786 cpu_node = of_get_cpu_node(cpu_id, NULL);
787 WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
791 cache = cache_lookup_by_node(cpu_node);
792 of_node_put(cpu_node);
797 static void remove_index_dirs(struct cache_dir *cache_dir)
799 struct cache_index_dir *index;
801 index = cache_dir->index;
804 struct cache_index_dir *next;
807 kobject_put(&index->kobj);
812 static void remove_cache_dir(struct cache_dir *cache_dir)
814 remove_index_dirs(cache_dir);
816 /* Remove cache dir from sysfs */
817 kobject_del(cache_dir->kobj);
819 kobject_put(cache_dir->kobj);
824 static void cache_cpu_clear(struct cache *cache, int cpu)
827 struct cache *next = cache->next_local;
829 WARN_ONCE(!cpumask_test_cpu(cpu, &cache->shared_cpu_map),
830 "CPU %i not accounted in %s(%s)\n",
831 cpu, cache->ofnode->full_name,
832 cache_type_string(cache));
834 cpumask_clear_cpu(cpu, &cache->shared_cpu_map);
836 /* Release the cache object if all the cpus using it
838 if (cpumask_empty(&cache->shared_cpu_map))
839 release_cache(cache);
845 void cacheinfo_cpu_offline(unsigned int cpu_id)
847 struct cache_dir *cache_dir;
850 /* Prevent userspace from seeing inconsistent state - remove
851 * the sysfs hierarchy first */
852 cache_dir = per_cpu(cache_dir_pcpu, cpu_id);
854 /* careful, sysfs population may have failed */
856 remove_cache_dir(cache_dir);
858 per_cpu(cache_dir_pcpu, cpu_id) = NULL;
860 /* clear the CPU's bit in its cache chain, possibly freeing
862 cache = cache_lookup_by_cpu(cpu_id);
864 cache_cpu_clear(cache, cpu_id);
866 #endif /* CONFIG_HOTPLUG_CPU */