193197a36SNathan Lynch /* 293197a36SNathan Lynch * Processor cache information made available to userspace via sysfs; 393197a36SNathan Lynch * intended to be compatible with x86 intel_cacheinfo implementation. 493197a36SNathan Lynch * 593197a36SNathan Lynch * Copyright 2008 IBM Corporation 693197a36SNathan Lynch * Author: Nathan Lynch 793197a36SNathan Lynch * 893197a36SNathan Lynch * This program is free software; you can redistribute it and/or 993197a36SNathan Lynch * modify it under the terms of the GNU General Public License version 1093197a36SNathan Lynch * 2 as published by the Free Software Foundation. 1193197a36SNathan Lynch */ 1293197a36SNathan Lynch 1393197a36SNathan Lynch #include <linux/cpu.h> 1493197a36SNathan Lynch #include <linux/cpumask.h> 1593197a36SNathan Lynch #include <linux/init.h> 1693197a36SNathan Lynch #include <linux/kernel.h> 1793197a36SNathan Lynch #include <linux/kobject.h> 1893197a36SNathan Lynch #include <linux/list.h> 1993197a36SNathan Lynch #include <linux/notifier.h> 2093197a36SNathan Lynch #include <linux/of.h> 2193197a36SNathan Lynch #include <linux/percpu.h> 2293197a36SNathan Lynch #include <asm/prom.h> 2393197a36SNathan Lynch 2493197a36SNathan Lynch #include "cacheinfo.h" 2593197a36SNathan Lynch 2693197a36SNathan Lynch /* per-cpu object for tracking: 2793197a36SNathan Lynch * - a "cache" kobject for the top-level directory 2893197a36SNathan Lynch * - a list of "index" objects representing the cpu's local cache hierarchy 2993197a36SNathan Lynch */ 3093197a36SNathan Lynch struct cache_dir { 3193197a36SNathan Lynch struct kobject *kobj; /* bare (not embedded) kobject for cache 3293197a36SNathan Lynch * directory */ 3393197a36SNathan Lynch struct cache_index_dir *index; /* list of index objects */ 3493197a36SNathan Lynch }; 3593197a36SNathan Lynch 3693197a36SNathan Lynch /* "index" object: each cpu's cache directory has an index 3793197a36SNathan Lynch * subdirectory corresponding to a cache object associated with the 3893197a36SNathan Lynch * cpu. This object's lifetime is managed via the embedded kobject. 3993197a36SNathan Lynch */ 4093197a36SNathan Lynch struct cache_index_dir { 4193197a36SNathan Lynch struct kobject kobj; 4293197a36SNathan Lynch struct cache_index_dir *next; /* next index in parent directory */ 4393197a36SNathan Lynch struct cache *cache; 4493197a36SNathan Lynch }; 4593197a36SNathan Lynch 4693197a36SNathan Lynch /* Template for determining which OF properties to query for a given 4793197a36SNathan Lynch * cache type */ 4893197a36SNathan Lynch struct cache_type_info { 4993197a36SNathan Lynch const char *name; 5093197a36SNathan Lynch const char *size_prop; 5193197a36SNathan Lynch 5293197a36SNathan Lynch /* Allow for both [di]-cache-line-size and 5393197a36SNathan Lynch * [di]-cache-block-size properties. According to the PowerPC 5493197a36SNathan Lynch * Processor binding, -line-size should be provided if it 5593197a36SNathan Lynch * differs from the cache block size (that which is operated 5693197a36SNathan Lynch * on by cache instructions), so we look for -line-size first. 5793197a36SNathan Lynch * See cache_get_line_size(). */ 5893197a36SNathan Lynch 5993197a36SNathan Lynch const char *line_size_props[2]; 6093197a36SNathan Lynch const char *nr_sets_prop; 6193197a36SNathan Lynch }; 6293197a36SNathan Lynch 6393197a36SNathan Lynch /* These are used to index the cache_type_info array. */ 6493197a36SNathan Lynch #define CACHE_TYPE_UNIFIED 0 6593197a36SNathan Lynch #define CACHE_TYPE_INSTRUCTION 1 6693197a36SNathan Lynch #define CACHE_TYPE_DATA 2 6793197a36SNathan Lynch 6893197a36SNathan Lynch static const struct cache_type_info cache_type_info[] = { 6993197a36SNathan Lynch { 7093197a36SNathan Lynch /* PowerPC Processor binding says the [di]-cache-* 7193197a36SNathan Lynch * must be equal on unified caches, so just use 7293197a36SNathan Lynch * d-cache properties. */ 7393197a36SNathan Lynch .name = "Unified", 7493197a36SNathan Lynch .size_prop = "d-cache-size", 7593197a36SNathan Lynch .line_size_props = { "d-cache-line-size", 7693197a36SNathan Lynch "d-cache-block-size", }, 7793197a36SNathan Lynch .nr_sets_prop = "d-cache-sets", 7893197a36SNathan Lynch }, 7993197a36SNathan Lynch { 8093197a36SNathan Lynch .name = "Instruction", 8193197a36SNathan Lynch .size_prop = "i-cache-size", 8293197a36SNathan Lynch .line_size_props = { "i-cache-line-size", 8393197a36SNathan Lynch "i-cache-block-size", }, 8493197a36SNathan Lynch .nr_sets_prop = "i-cache-sets", 8593197a36SNathan Lynch }, 8693197a36SNathan Lynch { 8793197a36SNathan Lynch .name = "Data", 8893197a36SNathan Lynch .size_prop = "d-cache-size", 8993197a36SNathan Lynch .line_size_props = { "d-cache-line-size", 9093197a36SNathan Lynch "d-cache-block-size", }, 9193197a36SNathan Lynch .nr_sets_prop = "d-cache-sets", 9293197a36SNathan Lynch }, 9393197a36SNathan Lynch }; 9493197a36SNathan Lynch 9593197a36SNathan Lynch /* Cache object: each instance of this corresponds to a distinct cache 9693197a36SNathan Lynch * in the system. There are separate objects for Harvard caches: one 9793197a36SNathan Lynch * each for instruction and data, and each refers to the same OF node. 9893197a36SNathan Lynch * The refcount of the OF node is elevated for the lifetime of the 9993197a36SNathan Lynch * cache object. A cache object is released when its shared_cpu_map 10093197a36SNathan Lynch * is cleared (see cache_cpu_clear). 10193197a36SNathan Lynch * 10293197a36SNathan Lynch * A cache object is on two lists: an unsorted global list 10393197a36SNathan Lynch * (cache_list) of cache objects; and a singly-linked list 10493197a36SNathan Lynch * representing the local cache hierarchy, which is ordered by level 10593197a36SNathan Lynch * (e.g. L1d -> L1i -> L2 -> L3). 10693197a36SNathan Lynch */ 10793197a36SNathan Lynch struct cache { 10893197a36SNathan Lynch struct device_node *ofnode; /* OF node for this cache, may be cpu */ 10993197a36SNathan Lynch struct cpumask shared_cpu_map; /* online CPUs using this cache */ 11093197a36SNathan Lynch int type; /* split cache disambiguation */ 11193197a36SNathan Lynch int level; /* level not explicit in device tree */ 11293197a36SNathan Lynch struct list_head list; /* global list of cache objects */ 11393197a36SNathan Lynch struct cache *next_local; /* next cache of >= level */ 11493197a36SNathan Lynch }; 11593197a36SNathan Lynch 116*fc7a9febSNathan Lynch static DEFINE_PER_CPU(struct cache_dir *, cache_dir_pcpu); 11793197a36SNathan Lynch 11893197a36SNathan Lynch /* traversal/modification of this list occurs only at cpu hotplug time; 11993197a36SNathan Lynch * access is serialized by cpu hotplug locking 12093197a36SNathan Lynch */ 12193197a36SNathan Lynch static LIST_HEAD(cache_list); 12293197a36SNathan Lynch 12393197a36SNathan Lynch static struct cache_index_dir *kobj_to_cache_index_dir(struct kobject *k) 12493197a36SNathan Lynch { 12593197a36SNathan Lynch return container_of(k, struct cache_index_dir, kobj); 12693197a36SNathan Lynch } 12793197a36SNathan Lynch 12893197a36SNathan Lynch static const char *cache_type_string(const struct cache *cache) 12993197a36SNathan Lynch { 13093197a36SNathan Lynch return cache_type_info[cache->type].name; 13193197a36SNathan Lynch } 13293197a36SNathan Lynch 13393197a36SNathan Lynch static void __cpuinit cache_init(struct cache *cache, int type, int level, struct device_node *ofnode) 13493197a36SNathan Lynch { 13593197a36SNathan Lynch cache->type = type; 13693197a36SNathan Lynch cache->level = level; 13793197a36SNathan Lynch cache->ofnode = of_node_get(ofnode); 13893197a36SNathan Lynch INIT_LIST_HEAD(&cache->list); 13993197a36SNathan Lynch list_add(&cache->list, &cache_list); 14093197a36SNathan Lynch } 14193197a36SNathan Lynch 14293197a36SNathan Lynch static struct cache *__cpuinit new_cache(int type, int level, struct device_node *ofnode) 14393197a36SNathan Lynch { 14493197a36SNathan Lynch struct cache *cache; 14593197a36SNathan Lynch 14693197a36SNathan Lynch cache = kzalloc(sizeof(*cache), GFP_KERNEL); 14793197a36SNathan Lynch if (cache) 14893197a36SNathan Lynch cache_init(cache, type, level, ofnode); 14993197a36SNathan Lynch 15093197a36SNathan Lynch return cache; 15193197a36SNathan Lynch } 15293197a36SNathan Lynch 15393197a36SNathan Lynch static void release_cache_debugcheck(struct cache *cache) 15493197a36SNathan Lynch { 15593197a36SNathan Lynch struct cache *iter; 15693197a36SNathan Lynch 15793197a36SNathan Lynch list_for_each_entry(iter, &cache_list, list) 15893197a36SNathan Lynch WARN_ONCE(iter->next_local == cache, 15993197a36SNathan Lynch "cache for %s(%s) refers to cache for %s(%s)\n", 16093197a36SNathan Lynch iter->ofnode->full_name, 16193197a36SNathan Lynch cache_type_string(iter), 16293197a36SNathan Lynch cache->ofnode->full_name, 16393197a36SNathan Lynch cache_type_string(cache)); 16493197a36SNathan Lynch } 16593197a36SNathan Lynch 16693197a36SNathan Lynch static void release_cache(struct cache *cache) 16793197a36SNathan Lynch { 16893197a36SNathan Lynch if (!cache) 16993197a36SNathan Lynch return; 17093197a36SNathan Lynch 17193197a36SNathan Lynch pr_debug("freeing L%d %s cache for %s\n", cache->level, 17293197a36SNathan Lynch cache_type_string(cache), cache->ofnode->full_name); 17393197a36SNathan Lynch 17493197a36SNathan Lynch release_cache_debugcheck(cache); 17593197a36SNathan Lynch list_del(&cache->list); 17693197a36SNathan Lynch of_node_put(cache->ofnode); 17793197a36SNathan Lynch kfree(cache); 17893197a36SNathan Lynch } 17993197a36SNathan Lynch 18093197a36SNathan Lynch static void cache_cpu_set(struct cache *cache, int cpu) 18193197a36SNathan Lynch { 18293197a36SNathan Lynch struct cache *next = cache; 18393197a36SNathan Lynch 18493197a36SNathan Lynch while (next) { 18593197a36SNathan Lynch WARN_ONCE(cpumask_test_cpu(cpu, &next->shared_cpu_map), 18693197a36SNathan Lynch "CPU %i already accounted in %s(%s)\n", 18793197a36SNathan Lynch cpu, next->ofnode->full_name, 18893197a36SNathan Lynch cache_type_string(next)); 18993197a36SNathan Lynch cpumask_set_cpu(cpu, &next->shared_cpu_map); 19093197a36SNathan Lynch next = next->next_local; 19193197a36SNathan Lynch } 19293197a36SNathan Lynch } 19393197a36SNathan Lynch 19493197a36SNathan Lynch static int cache_size(const struct cache *cache, unsigned int *ret) 19593197a36SNathan Lynch { 19693197a36SNathan Lynch const char *propname; 19793197a36SNathan Lynch const u32 *cache_size; 19893197a36SNathan Lynch 19993197a36SNathan Lynch propname = cache_type_info[cache->type].size_prop; 20093197a36SNathan Lynch 20193197a36SNathan Lynch cache_size = of_get_property(cache->ofnode, propname, NULL); 20293197a36SNathan Lynch if (!cache_size) 20393197a36SNathan Lynch return -ENODEV; 20493197a36SNathan Lynch 20593197a36SNathan Lynch *ret = *cache_size; 20693197a36SNathan Lynch return 0; 20793197a36SNathan Lynch } 20893197a36SNathan Lynch 20993197a36SNathan Lynch static int cache_size_kb(const struct cache *cache, unsigned int *ret) 21093197a36SNathan Lynch { 21193197a36SNathan Lynch unsigned int size; 21293197a36SNathan Lynch 21393197a36SNathan Lynch if (cache_size(cache, &size)) 21493197a36SNathan Lynch return -ENODEV; 21593197a36SNathan Lynch 21693197a36SNathan Lynch *ret = size / 1024; 21793197a36SNathan Lynch return 0; 21893197a36SNathan Lynch } 21993197a36SNathan Lynch 22093197a36SNathan Lynch /* not cache_line_size() because that's a macro in include/linux/cache.h */ 22193197a36SNathan Lynch static int cache_get_line_size(const struct cache *cache, unsigned int *ret) 22293197a36SNathan Lynch { 22393197a36SNathan Lynch const u32 *line_size; 22493197a36SNathan Lynch int i, lim; 22593197a36SNathan Lynch 22693197a36SNathan Lynch lim = ARRAY_SIZE(cache_type_info[cache->type].line_size_props); 22793197a36SNathan Lynch 22893197a36SNathan Lynch for (i = 0; i < lim; i++) { 22993197a36SNathan Lynch const char *propname; 23093197a36SNathan Lynch 23193197a36SNathan Lynch propname = cache_type_info[cache->type].line_size_props[i]; 23293197a36SNathan Lynch line_size = of_get_property(cache->ofnode, propname, NULL); 23393197a36SNathan Lynch if (line_size) 23493197a36SNathan Lynch break; 23593197a36SNathan Lynch } 23693197a36SNathan Lynch 23793197a36SNathan Lynch if (!line_size) 23893197a36SNathan Lynch return -ENODEV; 23993197a36SNathan Lynch 24093197a36SNathan Lynch *ret = *line_size; 24193197a36SNathan Lynch return 0; 24293197a36SNathan Lynch } 24393197a36SNathan Lynch 24493197a36SNathan Lynch static int cache_nr_sets(const struct cache *cache, unsigned int *ret) 24593197a36SNathan Lynch { 24693197a36SNathan Lynch const char *propname; 24793197a36SNathan Lynch const u32 *nr_sets; 24893197a36SNathan Lynch 24993197a36SNathan Lynch propname = cache_type_info[cache->type].nr_sets_prop; 25093197a36SNathan Lynch 25193197a36SNathan Lynch nr_sets = of_get_property(cache->ofnode, propname, NULL); 25293197a36SNathan Lynch if (!nr_sets) 25393197a36SNathan Lynch return -ENODEV; 25493197a36SNathan Lynch 25593197a36SNathan Lynch *ret = *nr_sets; 25693197a36SNathan Lynch return 0; 25793197a36SNathan Lynch } 25893197a36SNathan Lynch 25993197a36SNathan Lynch static int cache_associativity(const struct cache *cache, unsigned int *ret) 26093197a36SNathan Lynch { 26193197a36SNathan Lynch unsigned int line_size; 26293197a36SNathan Lynch unsigned int nr_sets; 26393197a36SNathan Lynch unsigned int size; 26493197a36SNathan Lynch 26593197a36SNathan Lynch if (cache_nr_sets(cache, &nr_sets)) 26693197a36SNathan Lynch goto err; 26793197a36SNathan Lynch 26893197a36SNathan Lynch /* If the cache is fully associative, there is no need to 26993197a36SNathan Lynch * check the other properties. 27093197a36SNathan Lynch */ 27193197a36SNathan Lynch if (nr_sets == 1) { 27293197a36SNathan Lynch *ret = 0; 27393197a36SNathan Lynch return 0; 27493197a36SNathan Lynch } 27593197a36SNathan Lynch 27693197a36SNathan Lynch if (cache_get_line_size(cache, &line_size)) 27793197a36SNathan Lynch goto err; 27893197a36SNathan Lynch if (cache_size(cache, &size)) 27993197a36SNathan Lynch goto err; 28093197a36SNathan Lynch 28193197a36SNathan Lynch if (!(nr_sets > 0 && size > 0 && line_size > 0)) 28293197a36SNathan Lynch goto err; 28393197a36SNathan Lynch 28493197a36SNathan Lynch *ret = (size / nr_sets) / line_size; 28593197a36SNathan Lynch return 0; 28693197a36SNathan Lynch err: 28793197a36SNathan Lynch return -ENODEV; 28893197a36SNathan Lynch } 28993197a36SNathan Lynch 29093197a36SNathan Lynch /* helper for dealing with split caches */ 29193197a36SNathan Lynch static struct cache *cache_find_first_sibling(struct cache *cache) 29293197a36SNathan Lynch { 29393197a36SNathan Lynch struct cache *iter; 29493197a36SNathan Lynch 29593197a36SNathan Lynch if (cache->type == CACHE_TYPE_UNIFIED) 29693197a36SNathan Lynch return cache; 29793197a36SNathan Lynch 29893197a36SNathan Lynch list_for_each_entry(iter, &cache_list, list) 29993197a36SNathan Lynch if (iter->ofnode == cache->ofnode && iter->next_local == cache) 30093197a36SNathan Lynch return iter; 30193197a36SNathan Lynch 30293197a36SNathan Lynch return cache; 30393197a36SNathan Lynch } 30493197a36SNathan Lynch 30593197a36SNathan Lynch /* return the first cache on a local list matching node */ 30693197a36SNathan Lynch static struct cache *cache_lookup_by_node(const struct device_node *node) 30793197a36SNathan Lynch { 30893197a36SNathan Lynch struct cache *cache = NULL; 30993197a36SNathan Lynch struct cache *iter; 31093197a36SNathan Lynch 31193197a36SNathan Lynch list_for_each_entry(iter, &cache_list, list) { 31293197a36SNathan Lynch if (iter->ofnode != node) 31393197a36SNathan Lynch continue; 31493197a36SNathan Lynch cache = cache_find_first_sibling(iter); 31593197a36SNathan Lynch break; 31693197a36SNathan Lynch } 31793197a36SNathan Lynch 31893197a36SNathan Lynch return cache; 31993197a36SNathan Lynch } 32093197a36SNathan Lynch 32193197a36SNathan Lynch static bool cache_node_is_unified(const struct device_node *np) 32293197a36SNathan Lynch { 32393197a36SNathan Lynch return of_get_property(np, "cache-unified", NULL); 32493197a36SNathan Lynch } 32593197a36SNathan Lynch 32693197a36SNathan Lynch static struct cache *__cpuinit cache_do_one_devnode_unified(struct device_node *node, int level) 32793197a36SNathan Lynch { 32893197a36SNathan Lynch struct cache *cache; 32993197a36SNathan Lynch 33093197a36SNathan Lynch pr_debug("creating L%d ucache for %s\n", level, node->full_name); 33193197a36SNathan Lynch 33293197a36SNathan Lynch cache = new_cache(CACHE_TYPE_UNIFIED, level, node); 33393197a36SNathan Lynch 33493197a36SNathan Lynch return cache; 33593197a36SNathan Lynch } 33693197a36SNathan Lynch 33793197a36SNathan Lynch static struct cache *__cpuinit cache_do_one_devnode_split(struct device_node *node, int level) 33893197a36SNathan Lynch { 33993197a36SNathan Lynch struct cache *dcache, *icache; 34093197a36SNathan Lynch 34193197a36SNathan Lynch pr_debug("creating L%d dcache and icache for %s\n", level, 34293197a36SNathan Lynch node->full_name); 34393197a36SNathan Lynch 34493197a36SNathan Lynch dcache = new_cache(CACHE_TYPE_DATA, level, node); 34593197a36SNathan Lynch icache = new_cache(CACHE_TYPE_INSTRUCTION, level, node); 34693197a36SNathan Lynch 34793197a36SNathan Lynch if (!dcache || !icache) 34893197a36SNathan Lynch goto err; 34993197a36SNathan Lynch 35093197a36SNathan Lynch dcache->next_local = icache; 35193197a36SNathan Lynch 35293197a36SNathan Lynch return dcache; 35393197a36SNathan Lynch err: 35493197a36SNathan Lynch release_cache(dcache); 35593197a36SNathan Lynch release_cache(icache); 35693197a36SNathan Lynch return NULL; 35793197a36SNathan Lynch } 35893197a36SNathan Lynch 35993197a36SNathan Lynch static struct cache *__cpuinit cache_do_one_devnode(struct device_node *node, int level) 36093197a36SNathan Lynch { 36193197a36SNathan Lynch struct cache *cache; 36293197a36SNathan Lynch 36393197a36SNathan Lynch if (cache_node_is_unified(node)) 36493197a36SNathan Lynch cache = cache_do_one_devnode_unified(node, level); 36593197a36SNathan Lynch else 36693197a36SNathan Lynch cache = cache_do_one_devnode_split(node, level); 36793197a36SNathan Lynch 36893197a36SNathan Lynch return cache; 36993197a36SNathan Lynch } 37093197a36SNathan Lynch 37193197a36SNathan Lynch static struct cache *__cpuinit cache_lookup_or_instantiate(struct device_node *node, int level) 37293197a36SNathan Lynch { 37393197a36SNathan Lynch struct cache *cache; 37493197a36SNathan Lynch 37593197a36SNathan Lynch cache = cache_lookup_by_node(node); 37693197a36SNathan Lynch 37793197a36SNathan Lynch WARN_ONCE(cache && cache->level != level, 37893197a36SNathan Lynch "cache level mismatch on lookup (got %d, expected %d)\n", 37993197a36SNathan Lynch cache->level, level); 38093197a36SNathan Lynch 38193197a36SNathan Lynch if (!cache) 38293197a36SNathan Lynch cache = cache_do_one_devnode(node, level); 38393197a36SNathan Lynch 38493197a36SNathan Lynch return cache; 38593197a36SNathan Lynch } 38693197a36SNathan Lynch 38793197a36SNathan Lynch static void __cpuinit link_cache_lists(struct cache *smaller, struct cache *bigger) 38893197a36SNathan Lynch { 38993197a36SNathan Lynch while (smaller->next_local) { 39093197a36SNathan Lynch if (smaller->next_local == bigger) 39193197a36SNathan Lynch return; /* already linked */ 39293197a36SNathan Lynch smaller = smaller->next_local; 39393197a36SNathan Lynch } 39493197a36SNathan Lynch 39593197a36SNathan Lynch smaller->next_local = bigger; 39693197a36SNathan Lynch } 39793197a36SNathan Lynch 39893197a36SNathan Lynch static void __cpuinit do_subsidiary_caches_debugcheck(struct cache *cache) 39993197a36SNathan Lynch { 40093197a36SNathan Lynch WARN_ON_ONCE(cache->level != 1); 40193197a36SNathan Lynch WARN_ON_ONCE(strcmp(cache->ofnode->type, "cpu")); 40293197a36SNathan Lynch } 40393197a36SNathan Lynch 40493197a36SNathan Lynch static void __cpuinit do_subsidiary_caches(struct cache *cache) 40593197a36SNathan Lynch { 40693197a36SNathan Lynch struct device_node *subcache_node; 40793197a36SNathan Lynch int level = cache->level; 40893197a36SNathan Lynch 40993197a36SNathan Lynch do_subsidiary_caches_debugcheck(cache); 41093197a36SNathan Lynch 41193197a36SNathan Lynch while ((subcache_node = of_find_next_cache_node(cache->ofnode))) { 41293197a36SNathan Lynch struct cache *subcache; 41393197a36SNathan Lynch 41493197a36SNathan Lynch level++; 41593197a36SNathan Lynch subcache = cache_lookup_or_instantiate(subcache_node, level); 41693197a36SNathan Lynch of_node_put(subcache_node); 41793197a36SNathan Lynch if (!subcache) 41893197a36SNathan Lynch break; 41993197a36SNathan Lynch 42093197a36SNathan Lynch link_cache_lists(cache, subcache); 42193197a36SNathan Lynch cache = subcache; 42293197a36SNathan Lynch } 42393197a36SNathan Lynch } 42493197a36SNathan Lynch 42593197a36SNathan Lynch static struct cache *__cpuinit cache_chain_instantiate(unsigned int cpu_id) 42693197a36SNathan Lynch { 42793197a36SNathan Lynch struct device_node *cpu_node; 42893197a36SNathan Lynch struct cache *cpu_cache = NULL; 42993197a36SNathan Lynch 43093197a36SNathan Lynch pr_debug("creating cache object(s) for CPU %i\n", cpu_id); 43193197a36SNathan Lynch 43293197a36SNathan Lynch cpu_node = of_get_cpu_node(cpu_id, NULL); 43393197a36SNathan Lynch WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id); 43493197a36SNathan Lynch if (!cpu_node) 43593197a36SNathan Lynch goto out; 43693197a36SNathan Lynch 43793197a36SNathan Lynch cpu_cache = cache_lookup_or_instantiate(cpu_node, 1); 43893197a36SNathan Lynch if (!cpu_cache) 43993197a36SNathan Lynch goto out; 44093197a36SNathan Lynch 44193197a36SNathan Lynch do_subsidiary_caches(cpu_cache); 44293197a36SNathan Lynch 44393197a36SNathan Lynch cache_cpu_set(cpu_cache, cpu_id); 44493197a36SNathan Lynch out: 44593197a36SNathan Lynch of_node_put(cpu_node); 44693197a36SNathan Lynch 44793197a36SNathan Lynch return cpu_cache; 44893197a36SNathan Lynch } 44993197a36SNathan Lynch 45093197a36SNathan Lynch static struct cache_dir *__cpuinit cacheinfo_create_cache_dir(unsigned int cpu_id) 45193197a36SNathan Lynch { 45293197a36SNathan Lynch struct cache_dir *cache_dir; 45393197a36SNathan Lynch struct sys_device *sysdev; 45493197a36SNathan Lynch struct kobject *kobj = NULL; 45593197a36SNathan Lynch 45693197a36SNathan Lynch sysdev = get_cpu_sysdev(cpu_id); 45793197a36SNathan Lynch WARN_ONCE(!sysdev, "no sysdev for CPU %i\n", cpu_id); 45893197a36SNathan Lynch if (!sysdev) 45993197a36SNathan Lynch goto err; 46093197a36SNathan Lynch 46193197a36SNathan Lynch kobj = kobject_create_and_add("cache", &sysdev->kobj); 46293197a36SNathan Lynch if (!kobj) 46393197a36SNathan Lynch goto err; 46493197a36SNathan Lynch 46593197a36SNathan Lynch cache_dir = kzalloc(sizeof(*cache_dir), GFP_KERNEL); 46693197a36SNathan Lynch if (!cache_dir) 46793197a36SNathan Lynch goto err; 46893197a36SNathan Lynch 46993197a36SNathan Lynch cache_dir->kobj = kobj; 47093197a36SNathan Lynch 471*fc7a9febSNathan Lynch WARN_ON_ONCE(per_cpu(cache_dir_pcpu, cpu_id) != NULL); 47293197a36SNathan Lynch 473*fc7a9febSNathan Lynch per_cpu(cache_dir_pcpu, cpu_id) = cache_dir; 47493197a36SNathan Lynch 47593197a36SNathan Lynch return cache_dir; 47693197a36SNathan Lynch err: 47793197a36SNathan Lynch kobject_put(kobj); 47893197a36SNathan Lynch return NULL; 47993197a36SNathan Lynch } 48093197a36SNathan Lynch 48193197a36SNathan Lynch static void cache_index_release(struct kobject *kobj) 48293197a36SNathan Lynch { 48393197a36SNathan Lynch struct cache_index_dir *index; 48493197a36SNathan Lynch 48593197a36SNathan Lynch index = kobj_to_cache_index_dir(kobj); 48693197a36SNathan Lynch 48793197a36SNathan Lynch pr_debug("freeing index directory for L%d %s cache\n", 48893197a36SNathan Lynch index->cache->level, cache_type_string(index->cache)); 48993197a36SNathan Lynch 49093197a36SNathan Lynch kfree(index); 49193197a36SNathan Lynch } 49293197a36SNathan Lynch 49393197a36SNathan Lynch static ssize_t cache_index_show(struct kobject *k, struct attribute *attr, char *buf) 49493197a36SNathan Lynch { 49593197a36SNathan Lynch struct kobj_attribute *kobj_attr; 49693197a36SNathan Lynch 49793197a36SNathan Lynch kobj_attr = container_of(attr, struct kobj_attribute, attr); 49893197a36SNathan Lynch 49993197a36SNathan Lynch return kobj_attr->show(k, kobj_attr, buf); 50093197a36SNathan Lynch } 50193197a36SNathan Lynch 50293197a36SNathan Lynch static struct cache *index_kobj_to_cache(struct kobject *k) 50393197a36SNathan Lynch { 50493197a36SNathan Lynch struct cache_index_dir *index; 50593197a36SNathan Lynch 50693197a36SNathan Lynch index = kobj_to_cache_index_dir(k); 50793197a36SNathan Lynch 50893197a36SNathan Lynch return index->cache; 50993197a36SNathan Lynch } 51093197a36SNathan Lynch 51193197a36SNathan Lynch static ssize_t size_show(struct kobject *k, struct kobj_attribute *attr, char *buf) 51293197a36SNathan Lynch { 51393197a36SNathan Lynch unsigned int size_kb; 51493197a36SNathan Lynch struct cache *cache; 51593197a36SNathan Lynch 51693197a36SNathan Lynch cache = index_kobj_to_cache(k); 51793197a36SNathan Lynch 51893197a36SNathan Lynch if (cache_size_kb(cache, &size_kb)) 51993197a36SNathan Lynch return -ENODEV; 52093197a36SNathan Lynch 52193197a36SNathan Lynch return sprintf(buf, "%uK\n", size_kb); 52293197a36SNathan Lynch } 52393197a36SNathan Lynch 52493197a36SNathan Lynch static struct kobj_attribute cache_size_attr = 52593197a36SNathan Lynch __ATTR(size, 0444, size_show, NULL); 52693197a36SNathan Lynch 52793197a36SNathan Lynch 52893197a36SNathan Lynch static ssize_t line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf) 52993197a36SNathan Lynch { 53093197a36SNathan Lynch unsigned int line_size; 53193197a36SNathan Lynch struct cache *cache; 53293197a36SNathan Lynch 53393197a36SNathan Lynch cache = index_kobj_to_cache(k); 53493197a36SNathan Lynch 53593197a36SNathan Lynch if (cache_get_line_size(cache, &line_size)) 53693197a36SNathan Lynch return -ENODEV; 53793197a36SNathan Lynch 53893197a36SNathan Lynch return sprintf(buf, "%u\n", line_size); 53993197a36SNathan Lynch } 54093197a36SNathan Lynch 54193197a36SNathan Lynch static struct kobj_attribute cache_line_size_attr = 54293197a36SNathan Lynch __ATTR(coherency_line_size, 0444, line_size_show, NULL); 54393197a36SNathan Lynch 54493197a36SNathan Lynch static ssize_t nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf) 54593197a36SNathan Lynch { 54693197a36SNathan Lynch unsigned int nr_sets; 54793197a36SNathan Lynch struct cache *cache; 54893197a36SNathan Lynch 54993197a36SNathan Lynch cache = index_kobj_to_cache(k); 55093197a36SNathan Lynch 55193197a36SNathan Lynch if (cache_nr_sets(cache, &nr_sets)) 55293197a36SNathan Lynch return -ENODEV; 55393197a36SNathan Lynch 55493197a36SNathan Lynch return sprintf(buf, "%u\n", nr_sets); 55593197a36SNathan Lynch } 55693197a36SNathan Lynch 55793197a36SNathan Lynch static struct kobj_attribute cache_nr_sets_attr = 55893197a36SNathan Lynch __ATTR(number_of_sets, 0444, nr_sets_show, NULL); 55993197a36SNathan Lynch 56093197a36SNathan Lynch static ssize_t associativity_show(struct kobject *k, struct kobj_attribute *attr, char *buf) 56193197a36SNathan Lynch { 56293197a36SNathan Lynch unsigned int associativity; 56393197a36SNathan Lynch struct cache *cache; 56493197a36SNathan Lynch 56593197a36SNathan Lynch cache = index_kobj_to_cache(k); 56693197a36SNathan Lynch 56793197a36SNathan Lynch if (cache_associativity(cache, &associativity)) 56893197a36SNathan Lynch return -ENODEV; 56993197a36SNathan Lynch 57093197a36SNathan Lynch return sprintf(buf, "%u\n", associativity); 57193197a36SNathan Lynch } 57293197a36SNathan Lynch 57393197a36SNathan Lynch static struct kobj_attribute cache_assoc_attr = 57493197a36SNathan Lynch __ATTR(ways_of_associativity, 0444, associativity_show, NULL); 57593197a36SNathan Lynch 57693197a36SNathan Lynch static ssize_t type_show(struct kobject *k, struct kobj_attribute *attr, char *buf) 57793197a36SNathan Lynch { 57893197a36SNathan Lynch struct cache *cache; 57993197a36SNathan Lynch 58093197a36SNathan Lynch cache = index_kobj_to_cache(k); 58193197a36SNathan Lynch 58293197a36SNathan Lynch return sprintf(buf, "%s\n", cache_type_string(cache)); 58393197a36SNathan Lynch } 58493197a36SNathan Lynch 58593197a36SNathan Lynch static struct kobj_attribute cache_type_attr = 58693197a36SNathan Lynch __ATTR(type, 0444, type_show, NULL); 58793197a36SNathan Lynch 58893197a36SNathan Lynch static ssize_t level_show(struct kobject *k, struct kobj_attribute *attr, char *buf) 58993197a36SNathan Lynch { 59093197a36SNathan Lynch struct cache_index_dir *index; 59193197a36SNathan Lynch struct cache *cache; 59293197a36SNathan Lynch 59393197a36SNathan Lynch index = kobj_to_cache_index_dir(k); 59493197a36SNathan Lynch cache = index->cache; 59593197a36SNathan Lynch 59693197a36SNathan Lynch return sprintf(buf, "%d\n", cache->level); 59793197a36SNathan Lynch } 59893197a36SNathan Lynch 59993197a36SNathan Lynch static struct kobj_attribute cache_level_attr = 60093197a36SNathan Lynch __ATTR(level, 0444, level_show, NULL); 60193197a36SNathan Lynch 60293197a36SNathan Lynch static ssize_t shared_cpu_map_show(struct kobject *k, struct kobj_attribute *attr, char *buf) 60393197a36SNathan Lynch { 60493197a36SNathan Lynch struct cache_index_dir *index; 60593197a36SNathan Lynch struct cache *cache; 60693197a36SNathan Lynch int len; 60793197a36SNathan Lynch int n = 0; 60893197a36SNathan Lynch 60993197a36SNathan Lynch index = kobj_to_cache_index_dir(k); 61093197a36SNathan Lynch cache = index->cache; 61193197a36SNathan Lynch len = PAGE_SIZE - 2; 61293197a36SNathan Lynch 61393197a36SNathan Lynch if (len > 1) { 61493197a36SNathan Lynch n = cpumask_scnprintf(buf, len, &cache->shared_cpu_map); 61593197a36SNathan Lynch buf[n++] = '\n'; 61693197a36SNathan Lynch buf[n] = '\0'; 61793197a36SNathan Lynch } 61893197a36SNathan Lynch return n; 61993197a36SNathan Lynch } 62093197a36SNathan Lynch 62193197a36SNathan Lynch static struct kobj_attribute cache_shared_cpu_map_attr = 62293197a36SNathan Lynch __ATTR(shared_cpu_map, 0444, shared_cpu_map_show, NULL); 62393197a36SNathan Lynch 62493197a36SNathan Lynch /* Attributes which should always be created -- the kobject/sysfs core 62593197a36SNathan Lynch * does this automatically via kobj_type->default_attrs. This is the 62693197a36SNathan Lynch * minimum data required to uniquely identify a cache. 62793197a36SNathan Lynch */ 62893197a36SNathan Lynch static struct attribute *cache_index_default_attrs[] = { 62993197a36SNathan Lynch &cache_type_attr.attr, 63093197a36SNathan Lynch &cache_level_attr.attr, 63193197a36SNathan Lynch &cache_shared_cpu_map_attr.attr, 63293197a36SNathan Lynch NULL, 63393197a36SNathan Lynch }; 63493197a36SNathan Lynch 63593197a36SNathan Lynch /* Attributes which should be created if the cache device node has the 63693197a36SNathan Lynch * right properties -- see cacheinfo_create_index_opt_attrs 63793197a36SNathan Lynch */ 63893197a36SNathan Lynch static struct kobj_attribute *cache_index_opt_attrs[] = { 63993197a36SNathan Lynch &cache_size_attr, 64093197a36SNathan Lynch &cache_line_size_attr, 64193197a36SNathan Lynch &cache_nr_sets_attr, 64293197a36SNathan Lynch &cache_assoc_attr, 64393197a36SNathan Lynch }; 64493197a36SNathan Lynch 64593197a36SNathan Lynch static struct sysfs_ops cache_index_ops = { 64693197a36SNathan Lynch .show = cache_index_show, 64793197a36SNathan Lynch }; 64893197a36SNathan Lynch 64993197a36SNathan Lynch static struct kobj_type cache_index_type = { 65093197a36SNathan Lynch .release = cache_index_release, 65193197a36SNathan Lynch .sysfs_ops = &cache_index_ops, 65293197a36SNathan Lynch .default_attrs = cache_index_default_attrs, 65393197a36SNathan Lynch }; 65493197a36SNathan Lynch 65593197a36SNathan Lynch static void __cpuinit cacheinfo_create_index_opt_attrs(struct cache_index_dir *dir) 65693197a36SNathan Lynch { 65793197a36SNathan Lynch const char *cache_name; 65893197a36SNathan Lynch const char *cache_type; 65993197a36SNathan Lynch struct cache *cache; 66093197a36SNathan Lynch char *buf; 66193197a36SNathan Lynch int i; 66293197a36SNathan Lynch 66393197a36SNathan Lynch buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 66493197a36SNathan Lynch if (!buf) 66593197a36SNathan Lynch return; 66693197a36SNathan Lynch 66793197a36SNathan Lynch cache = dir->cache; 66893197a36SNathan Lynch cache_name = cache->ofnode->full_name; 66993197a36SNathan Lynch cache_type = cache_type_string(cache); 67093197a36SNathan Lynch 67193197a36SNathan Lynch /* We don't want to create an attribute that can't provide a 67293197a36SNathan Lynch * meaningful value. Check the return value of each optional 67393197a36SNathan Lynch * attribute's ->show method before registering the 67493197a36SNathan Lynch * attribute. 67593197a36SNathan Lynch */ 67693197a36SNathan Lynch for (i = 0; i < ARRAY_SIZE(cache_index_opt_attrs); i++) { 67793197a36SNathan Lynch struct kobj_attribute *attr; 67893197a36SNathan Lynch ssize_t rc; 67993197a36SNathan Lynch 68093197a36SNathan Lynch attr = cache_index_opt_attrs[i]; 68193197a36SNathan Lynch 68293197a36SNathan Lynch rc = attr->show(&dir->kobj, attr, buf); 68393197a36SNathan Lynch if (rc <= 0) { 68493197a36SNathan Lynch pr_debug("not creating %s attribute for " 68593197a36SNathan Lynch "%s(%s) (rc = %zd)\n", 68693197a36SNathan Lynch attr->attr.name, cache_name, 68793197a36SNathan Lynch cache_type, rc); 68893197a36SNathan Lynch continue; 68993197a36SNathan Lynch } 69093197a36SNathan Lynch if (sysfs_create_file(&dir->kobj, &attr->attr)) 69193197a36SNathan Lynch pr_debug("could not create %s attribute for %s(%s)\n", 69293197a36SNathan Lynch attr->attr.name, cache_name, cache_type); 69393197a36SNathan Lynch } 69493197a36SNathan Lynch 69593197a36SNathan Lynch kfree(buf); 69693197a36SNathan Lynch } 69793197a36SNathan Lynch 69893197a36SNathan Lynch static void __cpuinit cacheinfo_create_index_dir(struct cache *cache, int index, struct cache_dir *cache_dir) 69993197a36SNathan Lynch { 70093197a36SNathan Lynch struct cache_index_dir *index_dir; 70193197a36SNathan Lynch int rc; 70293197a36SNathan Lynch 70393197a36SNathan Lynch index_dir = kzalloc(sizeof(*index_dir), GFP_KERNEL); 70493197a36SNathan Lynch if (!index_dir) 70593197a36SNathan Lynch goto err; 70693197a36SNathan Lynch 70793197a36SNathan Lynch index_dir->cache = cache; 70893197a36SNathan Lynch 70993197a36SNathan Lynch rc = kobject_init_and_add(&index_dir->kobj, &cache_index_type, 71093197a36SNathan Lynch cache_dir->kobj, "index%d", index); 71193197a36SNathan Lynch if (rc) 71293197a36SNathan Lynch goto err; 71393197a36SNathan Lynch 71493197a36SNathan Lynch index_dir->next = cache_dir->index; 71593197a36SNathan Lynch cache_dir->index = index_dir; 71693197a36SNathan Lynch 71793197a36SNathan Lynch cacheinfo_create_index_opt_attrs(index_dir); 71893197a36SNathan Lynch 71993197a36SNathan Lynch return; 72093197a36SNathan Lynch err: 72193197a36SNathan Lynch kfree(index_dir); 72293197a36SNathan Lynch } 72393197a36SNathan Lynch 72493197a36SNathan Lynch static void __cpuinit cacheinfo_sysfs_populate(unsigned int cpu_id, struct cache *cache_list) 72593197a36SNathan Lynch { 72693197a36SNathan Lynch struct cache_dir *cache_dir; 72793197a36SNathan Lynch struct cache *cache; 72893197a36SNathan Lynch int index = 0; 72993197a36SNathan Lynch 73093197a36SNathan Lynch cache_dir = cacheinfo_create_cache_dir(cpu_id); 73193197a36SNathan Lynch if (!cache_dir) 73293197a36SNathan Lynch return; 73393197a36SNathan Lynch 73493197a36SNathan Lynch cache = cache_list; 73593197a36SNathan Lynch while (cache) { 73693197a36SNathan Lynch cacheinfo_create_index_dir(cache, index, cache_dir); 73793197a36SNathan Lynch index++; 73893197a36SNathan Lynch cache = cache->next_local; 73993197a36SNathan Lynch } 74093197a36SNathan Lynch } 74193197a36SNathan Lynch 74293197a36SNathan Lynch void __cpuinit cacheinfo_cpu_online(unsigned int cpu_id) 74393197a36SNathan Lynch { 74493197a36SNathan Lynch struct cache *cache; 74593197a36SNathan Lynch 74693197a36SNathan Lynch cache = cache_chain_instantiate(cpu_id); 74793197a36SNathan Lynch if (!cache) 74893197a36SNathan Lynch return; 74993197a36SNathan Lynch 75093197a36SNathan Lynch cacheinfo_sysfs_populate(cpu_id, cache); 75193197a36SNathan Lynch } 75293197a36SNathan Lynch 75393197a36SNathan Lynch #ifdef CONFIG_HOTPLUG_CPU /* functions needed for cpu offline */ 75493197a36SNathan Lynch 75593197a36SNathan Lynch static struct cache *cache_lookup_by_cpu(unsigned int cpu_id) 75693197a36SNathan Lynch { 75793197a36SNathan Lynch struct device_node *cpu_node; 75893197a36SNathan Lynch struct cache *cache; 75993197a36SNathan Lynch 76093197a36SNathan Lynch cpu_node = of_get_cpu_node(cpu_id, NULL); 76193197a36SNathan Lynch WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id); 76293197a36SNathan Lynch if (!cpu_node) 76393197a36SNathan Lynch return NULL; 76493197a36SNathan Lynch 76593197a36SNathan Lynch cache = cache_lookup_by_node(cpu_node); 76693197a36SNathan Lynch of_node_put(cpu_node); 76793197a36SNathan Lynch 76893197a36SNathan Lynch return cache; 76993197a36SNathan Lynch } 77093197a36SNathan Lynch 77193197a36SNathan Lynch static void remove_index_dirs(struct cache_dir *cache_dir) 77293197a36SNathan Lynch { 77393197a36SNathan Lynch struct cache_index_dir *index; 77493197a36SNathan Lynch 77593197a36SNathan Lynch index = cache_dir->index; 77693197a36SNathan Lynch 77793197a36SNathan Lynch while (index) { 77893197a36SNathan Lynch struct cache_index_dir *next; 77993197a36SNathan Lynch 78093197a36SNathan Lynch next = index->next; 78193197a36SNathan Lynch kobject_put(&index->kobj); 78293197a36SNathan Lynch index = next; 78393197a36SNathan Lynch } 78493197a36SNathan Lynch } 78593197a36SNathan Lynch 78693197a36SNathan Lynch static void remove_cache_dir(struct cache_dir *cache_dir) 78793197a36SNathan Lynch { 78893197a36SNathan Lynch remove_index_dirs(cache_dir); 78993197a36SNathan Lynch 79093197a36SNathan Lynch kobject_put(cache_dir->kobj); 79193197a36SNathan Lynch 79293197a36SNathan Lynch kfree(cache_dir); 79393197a36SNathan Lynch } 79493197a36SNathan Lynch 79593197a36SNathan Lynch static void cache_cpu_clear(struct cache *cache, int cpu) 79693197a36SNathan Lynch { 79793197a36SNathan Lynch while (cache) { 79893197a36SNathan Lynch struct cache *next = cache->next_local; 79993197a36SNathan Lynch 80093197a36SNathan Lynch WARN_ONCE(!cpumask_test_cpu(cpu, &cache->shared_cpu_map), 80193197a36SNathan Lynch "CPU %i not accounted in %s(%s)\n", 80293197a36SNathan Lynch cpu, cache->ofnode->full_name, 80393197a36SNathan Lynch cache_type_string(cache)); 80493197a36SNathan Lynch 80593197a36SNathan Lynch cpumask_clear_cpu(cpu, &cache->shared_cpu_map); 80693197a36SNathan Lynch 80793197a36SNathan Lynch /* Release the cache object if all the cpus using it 80893197a36SNathan Lynch * are offline */ 80993197a36SNathan Lynch if (cpumask_empty(&cache->shared_cpu_map)) 81093197a36SNathan Lynch release_cache(cache); 81193197a36SNathan Lynch 81293197a36SNathan Lynch cache = next; 81393197a36SNathan Lynch } 81493197a36SNathan Lynch } 81593197a36SNathan Lynch 81693197a36SNathan Lynch void cacheinfo_cpu_offline(unsigned int cpu_id) 81793197a36SNathan Lynch { 81893197a36SNathan Lynch struct cache_dir *cache_dir; 81993197a36SNathan Lynch struct cache *cache; 82093197a36SNathan Lynch 82193197a36SNathan Lynch /* Prevent userspace from seeing inconsistent state - remove 82293197a36SNathan Lynch * the sysfs hierarchy first */ 823*fc7a9febSNathan Lynch cache_dir = per_cpu(cache_dir_pcpu, cpu_id); 82493197a36SNathan Lynch 82593197a36SNathan Lynch /* careful, sysfs population may have failed */ 82693197a36SNathan Lynch if (cache_dir) 82793197a36SNathan Lynch remove_cache_dir(cache_dir); 82893197a36SNathan Lynch 829*fc7a9febSNathan Lynch per_cpu(cache_dir_pcpu, cpu_id) = NULL; 83093197a36SNathan Lynch 83193197a36SNathan Lynch /* clear the CPU's bit in its cache chain, possibly freeing 83293197a36SNathan Lynch * cache objects */ 83393197a36SNathan Lynch cache = cache_lookup_by_cpu(cpu_id); 83493197a36SNathan Lynch if (cache) 83593197a36SNathan Lynch cache_cpu_clear(cache, cpu_id); 83693197a36SNathan Lynch } 83793197a36SNathan Lynch #endif /* CONFIG_HOTPLUG_CPU */ 838