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/kernel.h> 1693197a36SNathan Lynch #include <linux/kobject.h> 1793197a36SNathan Lynch #include <linux/list.h> 1893197a36SNathan Lynch #include <linux/notifier.h> 1993197a36SNathan Lynch #include <linux/of.h> 2093197a36SNathan Lynch #include <linux/percpu.h> 215a0e3ad6STejun Heo #include <linux/slab.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 116fc7a9febSNathan 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 133061d19f2SPaul Gortmaker static void cache_init(struct cache *cache, int type, int level, 134061d19f2SPaul Gortmaker struct device_node *ofnode) 13593197a36SNathan Lynch { 13693197a36SNathan Lynch cache->type = type; 13793197a36SNathan Lynch cache->level = level; 13893197a36SNathan Lynch cache->ofnode = of_node_get(ofnode); 13993197a36SNathan Lynch INIT_LIST_HEAD(&cache->list); 14093197a36SNathan Lynch list_add(&cache->list, &cache_list); 14193197a36SNathan Lynch } 14293197a36SNathan Lynch 143061d19f2SPaul Gortmaker static struct cache *new_cache(int type, int level, struct device_node *ofnode) 14493197a36SNathan Lynch { 14593197a36SNathan Lynch struct cache *cache; 14693197a36SNathan Lynch 14793197a36SNathan Lynch cache = kzalloc(sizeof(*cache), GFP_KERNEL); 14893197a36SNathan Lynch if (cache) 14993197a36SNathan Lynch cache_init(cache, type, level, ofnode); 15093197a36SNathan Lynch 15193197a36SNathan Lynch return cache; 15293197a36SNathan Lynch } 15393197a36SNathan Lynch 15493197a36SNathan Lynch static void release_cache_debugcheck(struct cache *cache) 15593197a36SNathan Lynch { 15693197a36SNathan Lynch struct cache *iter; 15793197a36SNathan Lynch 15893197a36SNathan Lynch list_for_each_entry(iter, &cache_list, list) 15993197a36SNathan Lynch WARN_ONCE(iter->next_local == cache, 16093197a36SNathan Lynch "cache for %s(%s) refers to cache for %s(%s)\n", 16193197a36SNathan Lynch iter->ofnode->full_name, 16293197a36SNathan Lynch cache_type_string(iter), 16393197a36SNathan Lynch cache->ofnode->full_name, 16493197a36SNathan Lynch cache_type_string(cache)); 16593197a36SNathan Lynch } 16693197a36SNathan Lynch 16793197a36SNathan Lynch static void release_cache(struct cache *cache) 16893197a36SNathan Lynch { 16993197a36SNathan Lynch if (!cache) 17093197a36SNathan Lynch return; 17193197a36SNathan Lynch 17293197a36SNathan Lynch pr_debug("freeing L%d %s cache for %s\n", cache->level, 17393197a36SNathan Lynch cache_type_string(cache), cache->ofnode->full_name); 17493197a36SNathan Lynch 17593197a36SNathan Lynch release_cache_debugcheck(cache); 17693197a36SNathan Lynch list_del(&cache->list); 17793197a36SNathan Lynch of_node_put(cache->ofnode); 17893197a36SNathan Lynch kfree(cache); 17993197a36SNathan Lynch } 18093197a36SNathan Lynch 18193197a36SNathan Lynch static void cache_cpu_set(struct cache *cache, int cpu) 18293197a36SNathan Lynch { 18393197a36SNathan Lynch struct cache *next = cache; 18493197a36SNathan Lynch 18593197a36SNathan Lynch while (next) { 18693197a36SNathan Lynch WARN_ONCE(cpumask_test_cpu(cpu, &next->shared_cpu_map), 18793197a36SNathan Lynch "CPU %i already accounted in %s(%s)\n", 18893197a36SNathan Lynch cpu, next->ofnode->full_name, 18993197a36SNathan Lynch cache_type_string(next)); 19093197a36SNathan Lynch cpumask_set_cpu(cpu, &next->shared_cpu_map); 19193197a36SNathan Lynch next = next->next_local; 19293197a36SNathan Lynch } 19393197a36SNathan Lynch } 19493197a36SNathan Lynch 19593197a36SNathan Lynch static int cache_size(const struct cache *cache, unsigned int *ret) 19693197a36SNathan Lynch { 19793197a36SNathan Lynch const char *propname; 198d10bd84fSAnton Blanchard const __be32 *cache_size; 19993197a36SNathan Lynch 20093197a36SNathan Lynch propname = cache_type_info[cache->type].size_prop; 20193197a36SNathan Lynch 20293197a36SNathan Lynch cache_size = of_get_property(cache->ofnode, propname, NULL); 20393197a36SNathan Lynch if (!cache_size) 20493197a36SNathan Lynch return -ENODEV; 20593197a36SNathan Lynch 206d10bd84fSAnton Blanchard *ret = of_read_number(cache_size, 1); 20793197a36SNathan Lynch return 0; 20893197a36SNathan Lynch } 20993197a36SNathan Lynch 21093197a36SNathan Lynch static int cache_size_kb(const struct cache *cache, unsigned int *ret) 21193197a36SNathan Lynch { 21293197a36SNathan Lynch unsigned int size; 21393197a36SNathan Lynch 21493197a36SNathan Lynch if (cache_size(cache, &size)) 21593197a36SNathan Lynch return -ENODEV; 21693197a36SNathan Lynch 21793197a36SNathan Lynch *ret = size / 1024; 21893197a36SNathan Lynch return 0; 21993197a36SNathan Lynch } 22093197a36SNathan Lynch 22193197a36SNathan Lynch /* not cache_line_size() because that's a macro in include/linux/cache.h */ 22293197a36SNathan Lynch static int cache_get_line_size(const struct cache *cache, unsigned int *ret) 22393197a36SNathan Lynch { 224d10bd84fSAnton Blanchard const __be32 *line_size; 22593197a36SNathan Lynch int i, lim; 22693197a36SNathan Lynch 22793197a36SNathan Lynch lim = ARRAY_SIZE(cache_type_info[cache->type].line_size_props); 22893197a36SNathan Lynch 22993197a36SNathan Lynch for (i = 0; i < lim; i++) { 23093197a36SNathan Lynch const char *propname; 23193197a36SNathan Lynch 23293197a36SNathan Lynch propname = cache_type_info[cache->type].line_size_props[i]; 23393197a36SNathan Lynch line_size = of_get_property(cache->ofnode, propname, NULL); 23493197a36SNathan Lynch if (line_size) 23593197a36SNathan Lynch break; 23693197a36SNathan Lynch } 23793197a36SNathan Lynch 23893197a36SNathan Lynch if (!line_size) 23993197a36SNathan Lynch return -ENODEV; 24093197a36SNathan Lynch 241d10bd84fSAnton Blanchard *ret = of_read_number(line_size, 1); 24293197a36SNathan Lynch return 0; 24393197a36SNathan Lynch } 24493197a36SNathan Lynch 24593197a36SNathan Lynch static int cache_nr_sets(const struct cache *cache, unsigned int *ret) 24693197a36SNathan Lynch { 24793197a36SNathan Lynch const char *propname; 248d10bd84fSAnton Blanchard const __be32 *nr_sets; 24993197a36SNathan Lynch 25093197a36SNathan Lynch propname = cache_type_info[cache->type].nr_sets_prop; 25193197a36SNathan Lynch 25293197a36SNathan Lynch nr_sets = of_get_property(cache->ofnode, propname, NULL); 25393197a36SNathan Lynch if (!nr_sets) 25493197a36SNathan Lynch return -ENODEV; 25593197a36SNathan Lynch 256d10bd84fSAnton Blanchard *ret = of_read_number(nr_sets, 1); 25793197a36SNathan Lynch return 0; 25893197a36SNathan Lynch } 25993197a36SNathan Lynch 26093197a36SNathan Lynch static int cache_associativity(const struct cache *cache, unsigned int *ret) 26193197a36SNathan Lynch { 26293197a36SNathan Lynch unsigned int line_size; 26393197a36SNathan Lynch unsigned int nr_sets; 26493197a36SNathan Lynch unsigned int size; 26593197a36SNathan Lynch 26693197a36SNathan Lynch if (cache_nr_sets(cache, &nr_sets)) 26793197a36SNathan Lynch goto err; 26893197a36SNathan Lynch 26993197a36SNathan Lynch /* If the cache is fully associative, there is no need to 27093197a36SNathan Lynch * check the other properties. 27193197a36SNathan Lynch */ 27293197a36SNathan Lynch if (nr_sets == 1) { 27393197a36SNathan Lynch *ret = 0; 27493197a36SNathan Lynch return 0; 27593197a36SNathan Lynch } 27693197a36SNathan Lynch 27793197a36SNathan Lynch if (cache_get_line_size(cache, &line_size)) 27893197a36SNathan Lynch goto err; 27993197a36SNathan Lynch if (cache_size(cache, &size)) 28093197a36SNathan Lynch goto err; 28193197a36SNathan Lynch 28293197a36SNathan Lynch if (!(nr_sets > 0 && size > 0 && line_size > 0)) 28393197a36SNathan Lynch goto err; 28493197a36SNathan Lynch 28593197a36SNathan Lynch *ret = (size / nr_sets) / line_size; 28693197a36SNathan Lynch return 0; 28793197a36SNathan Lynch err: 28893197a36SNathan Lynch return -ENODEV; 28993197a36SNathan Lynch } 29093197a36SNathan Lynch 29193197a36SNathan Lynch /* helper for dealing with split caches */ 29293197a36SNathan Lynch static struct cache *cache_find_first_sibling(struct cache *cache) 29393197a36SNathan Lynch { 29493197a36SNathan Lynch struct cache *iter; 29593197a36SNathan Lynch 29693197a36SNathan Lynch if (cache->type == CACHE_TYPE_UNIFIED) 29793197a36SNathan Lynch return cache; 29893197a36SNathan Lynch 29993197a36SNathan Lynch list_for_each_entry(iter, &cache_list, list) 30093197a36SNathan Lynch if (iter->ofnode == cache->ofnode && iter->next_local == cache) 30193197a36SNathan Lynch return iter; 30293197a36SNathan Lynch 30393197a36SNathan Lynch return cache; 30493197a36SNathan Lynch } 30593197a36SNathan Lynch 30693197a36SNathan Lynch /* return the first cache on a local list matching node */ 30793197a36SNathan Lynch static struct cache *cache_lookup_by_node(const struct device_node *node) 30893197a36SNathan Lynch { 30993197a36SNathan Lynch struct cache *cache = NULL; 31093197a36SNathan Lynch struct cache *iter; 31193197a36SNathan Lynch 31293197a36SNathan Lynch list_for_each_entry(iter, &cache_list, list) { 31393197a36SNathan Lynch if (iter->ofnode != node) 31493197a36SNathan Lynch continue; 31593197a36SNathan Lynch cache = cache_find_first_sibling(iter); 31693197a36SNathan Lynch break; 31793197a36SNathan Lynch } 31893197a36SNathan Lynch 31993197a36SNathan Lynch return cache; 32093197a36SNathan Lynch } 32193197a36SNathan Lynch 32293197a36SNathan Lynch static bool cache_node_is_unified(const struct device_node *np) 32393197a36SNathan Lynch { 32493197a36SNathan Lynch return of_get_property(np, "cache-unified", NULL); 32593197a36SNathan Lynch } 32693197a36SNathan Lynch 327061d19f2SPaul Gortmaker static struct cache *cache_do_one_devnode_unified(struct device_node *node, 328061d19f2SPaul Gortmaker int level) 32993197a36SNathan Lynch { 33093197a36SNathan Lynch struct cache *cache; 33193197a36SNathan Lynch 33293197a36SNathan Lynch pr_debug("creating L%d ucache for %s\n", level, node->full_name); 33393197a36SNathan Lynch 33493197a36SNathan Lynch cache = new_cache(CACHE_TYPE_UNIFIED, level, node); 33593197a36SNathan Lynch 33693197a36SNathan Lynch return cache; 33793197a36SNathan Lynch } 33893197a36SNathan Lynch 339061d19f2SPaul Gortmaker static struct cache *cache_do_one_devnode_split(struct device_node *node, 340061d19f2SPaul Gortmaker int level) 34193197a36SNathan Lynch { 34293197a36SNathan Lynch struct cache *dcache, *icache; 34393197a36SNathan Lynch 34493197a36SNathan Lynch pr_debug("creating L%d dcache and icache for %s\n", level, 34593197a36SNathan Lynch node->full_name); 34693197a36SNathan Lynch 34793197a36SNathan Lynch dcache = new_cache(CACHE_TYPE_DATA, level, node); 34893197a36SNathan Lynch icache = new_cache(CACHE_TYPE_INSTRUCTION, level, node); 34993197a36SNathan Lynch 35093197a36SNathan Lynch if (!dcache || !icache) 35193197a36SNathan Lynch goto err; 35293197a36SNathan Lynch 35393197a36SNathan Lynch dcache->next_local = icache; 35493197a36SNathan Lynch 35593197a36SNathan Lynch return dcache; 35693197a36SNathan Lynch err: 35793197a36SNathan Lynch release_cache(dcache); 35893197a36SNathan Lynch release_cache(icache); 35993197a36SNathan Lynch return NULL; 36093197a36SNathan Lynch } 36193197a36SNathan Lynch 362061d19f2SPaul Gortmaker static struct cache *cache_do_one_devnode(struct device_node *node, int level) 36393197a36SNathan Lynch { 36493197a36SNathan Lynch struct cache *cache; 36593197a36SNathan Lynch 36693197a36SNathan Lynch if (cache_node_is_unified(node)) 36793197a36SNathan Lynch cache = cache_do_one_devnode_unified(node, level); 36893197a36SNathan Lynch else 36993197a36SNathan Lynch cache = cache_do_one_devnode_split(node, level); 37093197a36SNathan Lynch 37193197a36SNathan Lynch return cache; 37293197a36SNathan Lynch } 37393197a36SNathan Lynch 374061d19f2SPaul Gortmaker static struct cache *cache_lookup_or_instantiate(struct device_node *node, 375061d19f2SPaul Gortmaker int level) 37693197a36SNathan Lynch { 37793197a36SNathan Lynch struct cache *cache; 37893197a36SNathan Lynch 37993197a36SNathan Lynch cache = cache_lookup_by_node(node); 38093197a36SNathan Lynch 38193197a36SNathan Lynch WARN_ONCE(cache && cache->level != level, 38293197a36SNathan Lynch "cache level mismatch on lookup (got %d, expected %d)\n", 38393197a36SNathan Lynch cache->level, level); 38493197a36SNathan Lynch 38593197a36SNathan Lynch if (!cache) 38693197a36SNathan Lynch cache = cache_do_one_devnode(node, level); 38793197a36SNathan Lynch 38893197a36SNathan Lynch return cache; 38993197a36SNathan Lynch } 39093197a36SNathan Lynch 391061d19f2SPaul Gortmaker static void link_cache_lists(struct cache *smaller, struct cache *bigger) 39293197a36SNathan Lynch { 39393197a36SNathan Lynch while (smaller->next_local) { 39493197a36SNathan Lynch if (smaller->next_local == bigger) 39593197a36SNathan Lynch return; /* already linked */ 39693197a36SNathan Lynch smaller = smaller->next_local; 39793197a36SNathan Lynch } 39893197a36SNathan Lynch 39993197a36SNathan Lynch smaller->next_local = bigger; 40093197a36SNathan Lynch } 40193197a36SNathan Lynch 402061d19f2SPaul Gortmaker static void do_subsidiary_caches_debugcheck(struct cache *cache) 40393197a36SNathan Lynch { 40493197a36SNathan Lynch WARN_ON_ONCE(cache->level != 1); 40593197a36SNathan Lynch WARN_ON_ONCE(strcmp(cache->ofnode->type, "cpu")); 40693197a36SNathan Lynch } 40793197a36SNathan Lynch 408061d19f2SPaul Gortmaker static void do_subsidiary_caches(struct cache *cache) 40993197a36SNathan Lynch { 41093197a36SNathan Lynch struct device_node *subcache_node; 41193197a36SNathan Lynch int level = cache->level; 41293197a36SNathan Lynch 41393197a36SNathan Lynch do_subsidiary_caches_debugcheck(cache); 41493197a36SNathan Lynch 41593197a36SNathan Lynch while ((subcache_node = of_find_next_cache_node(cache->ofnode))) { 41693197a36SNathan Lynch struct cache *subcache; 41793197a36SNathan Lynch 41893197a36SNathan Lynch level++; 41993197a36SNathan Lynch subcache = cache_lookup_or_instantiate(subcache_node, level); 42093197a36SNathan Lynch of_node_put(subcache_node); 42193197a36SNathan Lynch if (!subcache) 42293197a36SNathan Lynch break; 42393197a36SNathan Lynch 42493197a36SNathan Lynch link_cache_lists(cache, subcache); 42593197a36SNathan Lynch cache = subcache; 42693197a36SNathan Lynch } 42793197a36SNathan Lynch } 42893197a36SNathan Lynch 429061d19f2SPaul Gortmaker static struct cache *cache_chain_instantiate(unsigned int cpu_id) 43093197a36SNathan Lynch { 43193197a36SNathan Lynch struct device_node *cpu_node; 43293197a36SNathan Lynch struct cache *cpu_cache = NULL; 43393197a36SNathan Lynch 43493197a36SNathan Lynch pr_debug("creating cache object(s) for CPU %i\n", cpu_id); 43593197a36SNathan Lynch 43693197a36SNathan Lynch cpu_node = of_get_cpu_node(cpu_id, NULL); 43793197a36SNathan Lynch WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id); 43893197a36SNathan Lynch if (!cpu_node) 43993197a36SNathan Lynch goto out; 44093197a36SNathan Lynch 44193197a36SNathan Lynch cpu_cache = cache_lookup_or_instantiate(cpu_node, 1); 44293197a36SNathan Lynch if (!cpu_cache) 44393197a36SNathan Lynch goto out; 44493197a36SNathan Lynch 44593197a36SNathan Lynch do_subsidiary_caches(cpu_cache); 44693197a36SNathan Lynch 44793197a36SNathan Lynch cache_cpu_set(cpu_cache, cpu_id); 44893197a36SNathan Lynch out: 44993197a36SNathan Lynch of_node_put(cpu_node); 45093197a36SNathan Lynch 45193197a36SNathan Lynch return cpu_cache; 45293197a36SNathan Lynch } 45393197a36SNathan Lynch 454061d19f2SPaul Gortmaker static struct cache_dir *cacheinfo_create_cache_dir(unsigned int cpu_id) 45593197a36SNathan Lynch { 45693197a36SNathan Lynch struct cache_dir *cache_dir; 4578a25a2fdSKay Sievers struct device *dev; 45893197a36SNathan Lynch struct kobject *kobj = NULL; 45993197a36SNathan Lynch 4608a25a2fdSKay Sievers dev = get_cpu_device(cpu_id); 4618a25a2fdSKay Sievers WARN_ONCE(!dev, "no dev for CPU %i\n", cpu_id); 4628a25a2fdSKay Sievers if (!dev) 46393197a36SNathan Lynch goto err; 46493197a36SNathan Lynch 4658a25a2fdSKay Sievers kobj = kobject_create_and_add("cache", &dev->kobj); 46693197a36SNathan Lynch if (!kobj) 46793197a36SNathan Lynch goto err; 46893197a36SNathan Lynch 46993197a36SNathan Lynch cache_dir = kzalloc(sizeof(*cache_dir), GFP_KERNEL); 47093197a36SNathan Lynch if (!cache_dir) 47193197a36SNathan Lynch goto err; 47293197a36SNathan Lynch 47393197a36SNathan Lynch cache_dir->kobj = kobj; 47493197a36SNathan Lynch 475fc7a9febSNathan Lynch WARN_ON_ONCE(per_cpu(cache_dir_pcpu, cpu_id) != NULL); 47693197a36SNathan Lynch 477fc7a9febSNathan Lynch per_cpu(cache_dir_pcpu, cpu_id) = cache_dir; 47893197a36SNathan Lynch 47993197a36SNathan Lynch return cache_dir; 48093197a36SNathan Lynch err: 48193197a36SNathan Lynch kobject_put(kobj); 48293197a36SNathan Lynch return NULL; 48393197a36SNathan Lynch } 48493197a36SNathan Lynch 48593197a36SNathan Lynch static void cache_index_release(struct kobject *kobj) 48693197a36SNathan Lynch { 48793197a36SNathan Lynch struct cache_index_dir *index; 48893197a36SNathan Lynch 48993197a36SNathan Lynch index = kobj_to_cache_index_dir(kobj); 49093197a36SNathan Lynch 49193197a36SNathan Lynch pr_debug("freeing index directory for L%d %s cache\n", 49293197a36SNathan Lynch index->cache->level, cache_type_string(index->cache)); 49393197a36SNathan Lynch 49493197a36SNathan Lynch kfree(index); 49593197a36SNathan Lynch } 49693197a36SNathan Lynch 49793197a36SNathan Lynch static ssize_t cache_index_show(struct kobject *k, struct attribute *attr, char *buf) 49893197a36SNathan Lynch { 49993197a36SNathan Lynch struct kobj_attribute *kobj_attr; 50093197a36SNathan Lynch 50193197a36SNathan Lynch kobj_attr = container_of(attr, struct kobj_attribute, attr); 50293197a36SNathan Lynch 50393197a36SNathan Lynch return kobj_attr->show(k, kobj_attr, buf); 50493197a36SNathan Lynch } 50593197a36SNathan Lynch 50693197a36SNathan Lynch static struct cache *index_kobj_to_cache(struct kobject *k) 50793197a36SNathan Lynch { 50893197a36SNathan Lynch struct cache_index_dir *index; 50993197a36SNathan Lynch 51093197a36SNathan Lynch index = kobj_to_cache_index_dir(k); 51193197a36SNathan Lynch 51293197a36SNathan Lynch return index->cache; 51393197a36SNathan Lynch } 51493197a36SNathan Lynch 51593197a36SNathan Lynch static ssize_t size_show(struct kobject *k, struct kobj_attribute *attr, char *buf) 51693197a36SNathan Lynch { 51793197a36SNathan Lynch unsigned int size_kb; 51893197a36SNathan Lynch struct cache *cache; 51993197a36SNathan Lynch 52093197a36SNathan Lynch cache = index_kobj_to_cache(k); 52193197a36SNathan Lynch 52293197a36SNathan Lynch if (cache_size_kb(cache, &size_kb)) 52393197a36SNathan Lynch return -ENODEV; 52493197a36SNathan Lynch 52593197a36SNathan Lynch return sprintf(buf, "%uK\n", size_kb); 52693197a36SNathan Lynch } 52793197a36SNathan Lynch 52893197a36SNathan Lynch static struct kobj_attribute cache_size_attr = 52993197a36SNathan Lynch __ATTR(size, 0444, size_show, NULL); 53093197a36SNathan Lynch 53193197a36SNathan Lynch 53293197a36SNathan Lynch static ssize_t line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf) 53393197a36SNathan Lynch { 53493197a36SNathan Lynch unsigned int line_size; 53593197a36SNathan Lynch struct cache *cache; 53693197a36SNathan Lynch 53793197a36SNathan Lynch cache = index_kobj_to_cache(k); 53893197a36SNathan Lynch 53993197a36SNathan Lynch if (cache_get_line_size(cache, &line_size)) 54093197a36SNathan Lynch return -ENODEV; 54193197a36SNathan Lynch 54293197a36SNathan Lynch return sprintf(buf, "%u\n", line_size); 54393197a36SNathan Lynch } 54493197a36SNathan Lynch 54593197a36SNathan Lynch static struct kobj_attribute cache_line_size_attr = 54693197a36SNathan Lynch __ATTR(coherency_line_size, 0444, line_size_show, NULL); 54793197a36SNathan Lynch 54893197a36SNathan Lynch static ssize_t nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf) 54993197a36SNathan Lynch { 55093197a36SNathan Lynch unsigned int nr_sets; 55193197a36SNathan Lynch struct cache *cache; 55293197a36SNathan Lynch 55393197a36SNathan Lynch cache = index_kobj_to_cache(k); 55493197a36SNathan Lynch 55593197a36SNathan Lynch if (cache_nr_sets(cache, &nr_sets)) 55693197a36SNathan Lynch return -ENODEV; 55793197a36SNathan Lynch 55893197a36SNathan Lynch return sprintf(buf, "%u\n", nr_sets); 55993197a36SNathan Lynch } 56093197a36SNathan Lynch 56193197a36SNathan Lynch static struct kobj_attribute cache_nr_sets_attr = 56293197a36SNathan Lynch __ATTR(number_of_sets, 0444, nr_sets_show, NULL); 56393197a36SNathan Lynch 56493197a36SNathan Lynch static ssize_t associativity_show(struct kobject *k, struct kobj_attribute *attr, char *buf) 56593197a36SNathan Lynch { 56693197a36SNathan Lynch unsigned int associativity; 56793197a36SNathan Lynch struct cache *cache; 56893197a36SNathan Lynch 56993197a36SNathan Lynch cache = index_kobj_to_cache(k); 57093197a36SNathan Lynch 57193197a36SNathan Lynch if (cache_associativity(cache, &associativity)) 57293197a36SNathan Lynch return -ENODEV; 57393197a36SNathan Lynch 57493197a36SNathan Lynch return sprintf(buf, "%u\n", associativity); 57593197a36SNathan Lynch } 57693197a36SNathan Lynch 57793197a36SNathan Lynch static struct kobj_attribute cache_assoc_attr = 57893197a36SNathan Lynch __ATTR(ways_of_associativity, 0444, associativity_show, NULL); 57993197a36SNathan Lynch 58093197a36SNathan Lynch static ssize_t type_show(struct kobject *k, struct kobj_attribute *attr, char *buf) 58193197a36SNathan Lynch { 58293197a36SNathan Lynch struct cache *cache; 58393197a36SNathan Lynch 58493197a36SNathan Lynch cache = index_kobj_to_cache(k); 58593197a36SNathan Lynch 58693197a36SNathan Lynch return sprintf(buf, "%s\n", cache_type_string(cache)); 58793197a36SNathan Lynch } 58893197a36SNathan Lynch 58993197a36SNathan Lynch static struct kobj_attribute cache_type_attr = 59093197a36SNathan Lynch __ATTR(type, 0444, type_show, NULL); 59193197a36SNathan Lynch 59293197a36SNathan Lynch static ssize_t level_show(struct kobject *k, struct kobj_attribute *attr, char *buf) 59393197a36SNathan Lynch { 59493197a36SNathan Lynch struct cache_index_dir *index; 59593197a36SNathan Lynch struct cache *cache; 59693197a36SNathan Lynch 59793197a36SNathan Lynch index = kobj_to_cache_index_dir(k); 59893197a36SNathan Lynch cache = index->cache; 59993197a36SNathan Lynch 60093197a36SNathan Lynch return sprintf(buf, "%d\n", cache->level); 60193197a36SNathan Lynch } 60293197a36SNathan Lynch 60393197a36SNathan Lynch static struct kobj_attribute cache_level_attr = 60493197a36SNathan Lynch __ATTR(level, 0444, level_show, NULL); 60593197a36SNathan Lynch 60693197a36SNathan Lynch static ssize_t shared_cpu_map_show(struct kobject *k, struct kobj_attribute *attr, char *buf) 60793197a36SNathan Lynch { 60893197a36SNathan Lynch struct cache_index_dir *index; 60993197a36SNathan Lynch struct cache *cache; 610*0c118b7bSTejun Heo int ret; 61193197a36SNathan Lynch 61293197a36SNathan Lynch index = kobj_to_cache_index_dir(k); 61393197a36SNathan Lynch cache = index->cache; 61493197a36SNathan Lynch 615*0c118b7bSTejun Heo ret = scnprintf(buf, PAGE_SIZE - 1, "%*pb\n", 616*0c118b7bSTejun Heo cpumask_pr_args(&cache->shared_cpu_map)); 617*0c118b7bSTejun Heo buf[ret++] = '\n'; 618*0c118b7bSTejun Heo buf[ret] = '\0'; 619*0c118b7bSTejun Heo return ret; 62093197a36SNathan Lynch } 62193197a36SNathan Lynch 62293197a36SNathan Lynch static struct kobj_attribute cache_shared_cpu_map_attr = 62393197a36SNathan Lynch __ATTR(shared_cpu_map, 0444, shared_cpu_map_show, NULL); 62493197a36SNathan Lynch 62593197a36SNathan Lynch /* Attributes which should always be created -- the kobject/sysfs core 62693197a36SNathan Lynch * does this automatically via kobj_type->default_attrs. This is the 62793197a36SNathan Lynch * minimum data required to uniquely identify a cache. 62893197a36SNathan Lynch */ 62993197a36SNathan Lynch static struct attribute *cache_index_default_attrs[] = { 63093197a36SNathan Lynch &cache_type_attr.attr, 63193197a36SNathan Lynch &cache_level_attr.attr, 63293197a36SNathan Lynch &cache_shared_cpu_map_attr.attr, 63393197a36SNathan Lynch NULL, 63493197a36SNathan Lynch }; 63593197a36SNathan Lynch 63693197a36SNathan Lynch /* Attributes which should be created if the cache device node has the 63793197a36SNathan Lynch * right properties -- see cacheinfo_create_index_opt_attrs 63893197a36SNathan Lynch */ 63993197a36SNathan Lynch static struct kobj_attribute *cache_index_opt_attrs[] = { 64093197a36SNathan Lynch &cache_size_attr, 64193197a36SNathan Lynch &cache_line_size_attr, 64293197a36SNathan Lynch &cache_nr_sets_attr, 64393197a36SNathan Lynch &cache_assoc_attr, 64493197a36SNathan Lynch }; 64593197a36SNathan Lynch 64652cf25d0SEmese Revfy static const struct sysfs_ops cache_index_ops = { 64793197a36SNathan Lynch .show = cache_index_show, 64893197a36SNathan Lynch }; 64993197a36SNathan Lynch 65093197a36SNathan Lynch static struct kobj_type cache_index_type = { 65193197a36SNathan Lynch .release = cache_index_release, 65293197a36SNathan Lynch .sysfs_ops = &cache_index_ops, 65393197a36SNathan Lynch .default_attrs = cache_index_default_attrs, 65493197a36SNathan Lynch }; 65593197a36SNathan Lynch 656061d19f2SPaul Gortmaker static void cacheinfo_create_index_opt_attrs(struct cache_index_dir *dir) 65793197a36SNathan Lynch { 65893197a36SNathan Lynch const char *cache_name; 65993197a36SNathan Lynch const char *cache_type; 66093197a36SNathan Lynch struct cache *cache; 66193197a36SNathan Lynch char *buf; 66293197a36SNathan Lynch int i; 66393197a36SNathan Lynch 66493197a36SNathan Lynch buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 66593197a36SNathan Lynch if (!buf) 66693197a36SNathan Lynch return; 66793197a36SNathan Lynch 66893197a36SNathan Lynch cache = dir->cache; 66993197a36SNathan Lynch cache_name = cache->ofnode->full_name; 67093197a36SNathan Lynch cache_type = cache_type_string(cache); 67193197a36SNathan Lynch 67293197a36SNathan Lynch /* We don't want to create an attribute that can't provide a 67393197a36SNathan Lynch * meaningful value. Check the return value of each optional 67493197a36SNathan Lynch * attribute's ->show method before registering the 67593197a36SNathan Lynch * attribute. 67693197a36SNathan Lynch */ 67793197a36SNathan Lynch for (i = 0; i < ARRAY_SIZE(cache_index_opt_attrs); i++) { 67893197a36SNathan Lynch struct kobj_attribute *attr; 67993197a36SNathan Lynch ssize_t rc; 68093197a36SNathan Lynch 68193197a36SNathan Lynch attr = cache_index_opt_attrs[i]; 68293197a36SNathan Lynch 68393197a36SNathan Lynch rc = attr->show(&dir->kobj, attr, buf); 68493197a36SNathan Lynch if (rc <= 0) { 68593197a36SNathan Lynch pr_debug("not creating %s attribute for " 68693197a36SNathan Lynch "%s(%s) (rc = %zd)\n", 68793197a36SNathan Lynch attr->attr.name, cache_name, 68893197a36SNathan Lynch cache_type, rc); 68993197a36SNathan Lynch continue; 69093197a36SNathan Lynch } 69193197a36SNathan Lynch if (sysfs_create_file(&dir->kobj, &attr->attr)) 69293197a36SNathan Lynch pr_debug("could not create %s attribute for %s(%s)\n", 69393197a36SNathan Lynch attr->attr.name, cache_name, cache_type); 69493197a36SNathan Lynch } 69593197a36SNathan Lynch 69693197a36SNathan Lynch kfree(buf); 69793197a36SNathan Lynch } 69893197a36SNathan Lynch 699061d19f2SPaul Gortmaker static void cacheinfo_create_index_dir(struct cache *cache, int index, 700061d19f2SPaul Gortmaker struct cache_dir *cache_dir) 70193197a36SNathan Lynch { 70293197a36SNathan Lynch struct cache_index_dir *index_dir; 70393197a36SNathan Lynch int rc; 70493197a36SNathan Lynch 70593197a36SNathan Lynch index_dir = kzalloc(sizeof(*index_dir), GFP_KERNEL); 70693197a36SNathan Lynch if (!index_dir) 70793197a36SNathan Lynch goto err; 70893197a36SNathan Lynch 70993197a36SNathan Lynch index_dir->cache = cache; 71093197a36SNathan Lynch 71193197a36SNathan Lynch rc = kobject_init_and_add(&index_dir->kobj, &cache_index_type, 71293197a36SNathan Lynch cache_dir->kobj, "index%d", index); 71393197a36SNathan Lynch if (rc) 71493197a36SNathan Lynch goto err; 71593197a36SNathan Lynch 71693197a36SNathan Lynch index_dir->next = cache_dir->index; 71793197a36SNathan Lynch cache_dir->index = index_dir; 71893197a36SNathan Lynch 71993197a36SNathan Lynch cacheinfo_create_index_opt_attrs(index_dir); 72093197a36SNathan Lynch 72193197a36SNathan Lynch return; 72293197a36SNathan Lynch err: 72393197a36SNathan Lynch kfree(index_dir); 72493197a36SNathan Lynch } 72593197a36SNathan Lynch 726061d19f2SPaul Gortmaker static void cacheinfo_sysfs_populate(unsigned int cpu_id, 727061d19f2SPaul Gortmaker struct cache *cache_list) 72893197a36SNathan Lynch { 72993197a36SNathan Lynch struct cache_dir *cache_dir; 73093197a36SNathan Lynch struct cache *cache; 73193197a36SNathan Lynch int index = 0; 73293197a36SNathan Lynch 73393197a36SNathan Lynch cache_dir = cacheinfo_create_cache_dir(cpu_id); 73493197a36SNathan Lynch if (!cache_dir) 73593197a36SNathan Lynch return; 73693197a36SNathan Lynch 73793197a36SNathan Lynch cache = cache_list; 73893197a36SNathan Lynch while (cache) { 73993197a36SNathan Lynch cacheinfo_create_index_dir(cache, index, cache_dir); 74093197a36SNathan Lynch index++; 74193197a36SNathan Lynch cache = cache->next_local; 74293197a36SNathan Lynch } 74393197a36SNathan Lynch } 74493197a36SNathan Lynch 745061d19f2SPaul Gortmaker void cacheinfo_cpu_online(unsigned int cpu_id) 74693197a36SNathan Lynch { 74793197a36SNathan Lynch struct cache *cache; 74893197a36SNathan Lynch 74993197a36SNathan Lynch cache = cache_chain_instantiate(cpu_id); 75093197a36SNathan Lynch if (!cache) 75193197a36SNathan Lynch return; 75293197a36SNathan Lynch 75393197a36SNathan Lynch cacheinfo_sysfs_populate(cpu_id, cache); 75493197a36SNathan Lynch } 75593197a36SNathan Lynch 7566b36ba84SHaren Myneni /* functions needed to remove cache entry for cpu offline or suspend/resume */ 7576b36ba84SHaren Myneni 7586b36ba84SHaren Myneni #if (defined(CONFIG_PPC_PSERIES) && defined(CONFIG_SUSPEND)) || \ 7596b36ba84SHaren Myneni defined(CONFIG_HOTPLUG_CPU) 76093197a36SNathan Lynch 76193197a36SNathan Lynch static struct cache *cache_lookup_by_cpu(unsigned int cpu_id) 76293197a36SNathan Lynch { 76393197a36SNathan Lynch struct device_node *cpu_node; 76493197a36SNathan Lynch struct cache *cache; 76593197a36SNathan Lynch 76693197a36SNathan Lynch cpu_node = of_get_cpu_node(cpu_id, NULL); 76793197a36SNathan Lynch WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id); 76893197a36SNathan Lynch if (!cpu_node) 76993197a36SNathan Lynch return NULL; 77093197a36SNathan Lynch 77193197a36SNathan Lynch cache = cache_lookup_by_node(cpu_node); 77293197a36SNathan Lynch of_node_put(cpu_node); 77393197a36SNathan Lynch 77493197a36SNathan Lynch return cache; 77593197a36SNathan Lynch } 77693197a36SNathan Lynch 77793197a36SNathan Lynch static void remove_index_dirs(struct cache_dir *cache_dir) 77893197a36SNathan Lynch { 77993197a36SNathan Lynch struct cache_index_dir *index; 78093197a36SNathan Lynch 78193197a36SNathan Lynch index = cache_dir->index; 78293197a36SNathan Lynch 78393197a36SNathan Lynch while (index) { 78493197a36SNathan Lynch struct cache_index_dir *next; 78593197a36SNathan Lynch 78693197a36SNathan Lynch next = index->next; 78793197a36SNathan Lynch kobject_put(&index->kobj); 78893197a36SNathan Lynch index = next; 78993197a36SNathan Lynch } 79093197a36SNathan Lynch } 79193197a36SNathan Lynch 79293197a36SNathan Lynch static void remove_cache_dir(struct cache_dir *cache_dir) 79393197a36SNathan Lynch { 79493197a36SNathan Lynch remove_index_dirs(cache_dir); 79593197a36SNathan Lynch 79691b973f9SPaul Mackerras /* Remove cache dir from sysfs */ 79791b973f9SPaul Mackerras kobject_del(cache_dir->kobj); 79891b973f9SPaul Mackerras 79993197a36SNathan Lynch kobject_put(cache_dir->kobj); 80093197a36SNathan Lynch 80193197a36SNathan Lynch kfree(cache_dir); 80293197a36SNathan Lynch } 80393197a36SNathan Lynch 80493197a36SNathan Lynch static void cache_cpu_clear(struct cache *cache, int cpu) 80593197a36SNathan Lynch { 80693197a36SNathan Lynch while (cache) { 80793197a36SNathan Lynch struct cache *next = cache->next_local; 80893197a36SNathan Lynch 80993197a36SNathan Lynch WARN_ONCE(!cpumask_test_cpu(cpu, &cache->shared_cpu_map), 81093197a36SNathan Lynch "CPU %i not accounted in %s(%s)\n", 81193197a36SNathan Lynch cpu, cache->ofnode->full_name, 81293197a36SNathan Lynch cache_type_string(cache)); 81393197a36SNathan Lynch 81493197a36SNathan Lynch cpumask_clear_cpu(cpu, &cache->shared_cpu_map); 81593197a36SNathan Lynch 81693197a36SNathan Lynch /* Release the cache object if all the cpus using it 81793197a36SNathan Lynch * are offline */ 81893197a36SNathan Lynch if (cpumask_empty(&cache->shared_cpu_map)) 81993197a36SNathan Lynch release_cache(cache); 82093197a36SNathan Lynch 82193197a36SNathan Lynch cache = next; 82293197a36SNathan Lynch } 82393197a36SNathan Lynch } 82493197a36SNathan Lynch 82593197a36SNathan Lynch void cacheinfo_cpu_offline(unsigned int cpu_id) 82693197a36SNathan Lynch { 82793197a36SNathan Lynch struct cache_dir *cache_dir; 82893197a36SNathan Lynch struct cache *cache; 82993197a36SNathan Lynch 83093197a36SNathan Lynch /* Prevent userspace from seeing inconsistent state - remove 83193197a36SNathan Lynch * the sysfs hierarchy first */ 832fc7a9febSNathan Lynch cache_dir = per_cpu(cache_dir_pcpu, cpu_id); 83393197a36SNathan Lynch 83493197a36SNathan Lynch /* careful, sysfs population may have failed */ 83593197a36SNathan Lynch if (cache_dir) 83693197a36SNathan Lynch remove_cache_dir(cache_dir); 83793197a36SNathan Lynch 838fc7a9febSNathan Lynch per_cpu(cache_dir_pcpu, cpu_id) = NULL; 83993197a36SNathan Lynch 84093197a36SNathan Lynch /* clear the CPU's bit in its cache chain, possibly freeing 84193197a36SNathan Lynch * cache objects */ 84293197a36SNathan Lynch cache = cache_lookup_by_cpu(cpu_id); 84393197a36SNathan Lynch if (cache) 84493197a36SNathan Lynch cache_cpu_clear(cache, cpu_id); 84593197a36SNathan Lynch } 8466b36ba84SHaren Myneni #endif /* (CONFIG_PPC_PSERIES && CONFIG_SUSPEND) || CONFIG_HOTPLUG_CPU */ 847