1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
293197a36SNathan Lynch /*
393197a36SNathan Lynch * Processor cache information made available to userspace via sysfs;
493197a36SNathan Lynch * intended to be compatible with x86 intel_cacheinfo implementation.
593197a36SNathan Lynch *
693197a36SNathan Lynch * Copyright 2008 IBM Corporation
793197a36SNathan Lynch * Author: Nathan Lynch
893197a36SNathan Lynch */
993197a36SNathan Lynch
10e2b3c165SNathan Lynch #define pr_fmt(fmt) "cacheinfo: " fmt
11e2b3c165SNathan Lynch
1293197a36SNathan Lynch #include <linux/cpu.h>
1393197a36SNathan Lynch #include <linux/cpumask.h>
1493197a36SNathan Lynch #include <linux/kernel.h>
1593197a36SNathan Lynch #include <linux/kobject.h>
1693197a36SNathan Lynch #include <linux/list.h>
1793197a36SNathan Lynch #include <linux/notifier.h>
1893197a36SNathan Lynch #include <linux/of.h>
1993197a36SNathan Lynch #include <linux/percpu.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
21500fe5f5SGautham R. Shenoy #include <asm/cputhreads.h>
22500fe5f5SGautham R. Shenoy #include <asm/smp.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. */
64f7e9e358SDave Olson #define CACHE_TYPE_UNIFIED 0 /* cache-size, cache-block-size, etc. */
65f7e9e358SDave Olson #define CACHE_TYPE_UNIFIED_D 1 /* d-cache-size, d-cache-block-size, etc */
66f7e9e358SDave Olson #define CACHE_TYPE_INSTRUCTION 2
67f7e9e358SDave Olson #define CACHE_TYPE_DATA 3
6893197a36SNathan Lynch
6993197a36SNathan Lynch static const struct cache_type_info cache_type_info[] = {
7093197a36SNathan Lynch {
71f7e9e358SDave Olson /* Embedded systems that use cache-size, cache-block-size,
72f7e9e358SDave Olson * etc. for the Unified (typically L2) cache. */
73f7e9e358SDave Olson .name = "Unified",
74f7e9e358SDave Olson .size_prop = "cache-size",
75f7e9e358SDave Olson .line_size_props = { "cache-line-size",
76f7e9e358SDave Olson "cache-block-size", },
77f7e9e358SDave Olson .nr_sets_prop = "cache-sets",
78f7e9e358SDave Olson },
79f7e9e358SDave Olson {
8093197a36SNathan Lynch /* PowerPC Processor binding says the [di]-cache-*
8193197a36SNathan Lynch * must be equal on unified caches, so just use
8293197a36SNathan Lynch * d-cache properties. */
8393197a36SNathan Lynch .name = "Unified",
8493197a36SNathan Lynch .size_prop = "d-cache-size",
8593197a36SNathan Lynch .line_size_props = { "d-cache-line-size",
8693197a36SNathan Lynch "d-cache-block-size", },
8793197a36SNathan Lynch .nr_sets_prop = "d-cache-sets",
8893197a36SNathan Lynch },
8993197a36SNathan Lynch {
9093197a36SNathan Lynch .name = "Instruction",
9193197a36SNathan Lynch .size_prop = "i-cache-size",
9293197a36SNathan Lynch .line_size_props = { "i-cache-line-size",
9393197a36SNathan Lynch "i-cache-block-size", },
9493197a36SNathan Lynch .nr_sets_prop = "i-cache-sets",
9593197a36SNathan Lynch },
9693197a36SNathan Lynch {
9793197a36SNathan Lynch .name = "Data",
9893197a36SNathan Lynch .size_prop = "d-cache-size",
9993197a36SNathan Lynch .line_size_props = { "d-cache-line-size",
10093197a36SNathan Lynch "d-cache-block-size", },
10193197a36SNathan Lynch .nr_sets_prop = "d-cache-sets",
10293197a36SNathan Lynch },
10393197a36SNathan Lynch };
10493197a36SNathan Lynch
10593197a36SNathan Lynch /* Cache object: each instance of this corresponds to a distinct cache
10693197a36SNathan Lynch * in the system. There are separate objects for Harvard caches: one
10793197a36SNathan Lynch * each for instruction and data, and each refers to the same OF node.
10893197a36SNathan Lynch * The refcount of the OF node is elevated for the lifetime of the
10993197a36SNathan Lynch * cache object. A cache object is released when its shared_cpu_map
11093197a36SNathan Lynch * is cleared (see cache_cpu_clear).
11193197a36SNathan Lynch *
11293197a36SNathan Lynch * A cache object is on two lists: an unsorted global list
11393197a36SNathan Lynch * (cache_list) of cache objects; and a singly-linked list
11493197a36SNathan Lynch * representing the local cache hierarchy, which is ordered by level
11593197a36SNathan Lynch * (e.g. L1d -> L1i -> L2 -> L3).
11693197a36SNathan Lynch */
11793197a36SNathan Lynch struct cache {
11893197a36SNathan Lynch struct device_node *ofnode; /* OF node for this cache, may be cpu */
11993197a36SNathan Lynch struct cpumask shared_cpu_map; /* online CPUs using this cache */
12093197a36SNathan Lynch int type; /* split cache disambiguation */
12193197a36SNathan Lynch int level; /* level not explicit in device tree */
122a4bec516SGautham R. Shenoy int group_id; /* id of the group of threads that share this cache */
12393197a36SNathan Lynch struct list_head list; /* global list of cache objects */
12493197a36SNathan Lynch struct cache *next_local; /* next cache of >= level */
12593197a36SNathan Lynch };
12693197a36SNathan Lynch
127fc7a9febSNathan Lynch static DEFINE_PER_CPU(struct cache_dir *, cache_dir_pcpu);
12893197a36SNathan Lynch
12993197a36SNathan Lynch /* traversal/modification of this list occurs only at cpu hotplug time;
13093197a36SNathan Lynch * access is serialized by cpu hotplug locking
13193197a36SNathan Lynch */
13293197a36SNathan Lynch static LIST_HEAD(cache_list);
13393197a36SNathan Lynch
kobj_to_cache_index_dir(struct kobject * k)13493197a36SNathan Lynch static struct cache_index_dir *kobj_to_cache_index_dir(struct kobject *k)
13593197a36SNathan Lynch {
13693197a36SNathan Lynch return container_of(k, struct cache_index_dir, kobj);
13793197a36SNathan Lynch }
13893197a36SNathan Lynch
cache_type_string(const struct cache * cache)13993197a36SNathan Lynch static const char *cache_type_string(const struct cache *cache)
14093197a36SNathan Lynch {
14193197a36SNathan Lynch return cache_type_info[cache->type].name;
14293197a36SNathan Lynch }
14393197a36SNathan Lynch
cache_init(struct cache * cache,int type,int level,struct device_node * ofnode,int group_id)144061d19f2SPaul Gortmaker static void cache_init(struct cache *cache, int type, int level,
145a4bec516SGautham R. Shenoy struct device_node *ofnode, int group_id)
14693197a36SNathan Lynch {
14793197a36SNathan Lynch cache->type = type;
14893197a36SNathan Lynch cache->level = level;
14993197a36SNathan Lynch cache->ofnode = of_node_get(ofnode);
150a4bec516SGautham R. Shenoy cache->group_id = group_id;
15193197a36SNathan Lynch INIT_LIST_HEAD(&cache->list);
15293197a36SNathan Lynch list_add(&cache->list, &cache_list);
15393197a36SNathan Lynch }
15493197a36SNathan Lynch
new_cache(int type,int level,struct device_node * ofnode,int group_id)155a4bec516SGautham R. Shenoy static struct cache *new_cache(int type, int level,
156a4bec516SGautham R. Shenoy struct device_node *ofnode, int group_id)
15793197a36SNathan Lynch {
15893197a36SNathan Lynch struct cache *cache;
15993197a36SNathan Lynch
16093197a36SNathan Lynch cache = kzalloc(sizeof(*cache), GFP_KERNEL);
16193197a36SNathan Lynch if (cache)
162a4bec516SGautham R. Shenoy cache_init(cache, type, level, ofnode, group_id);
16393197a36SNathan Lynch
16493197a36SNathan Lynch return cache;
16593197a36SNathan Lynch }
16693197a36SNathan Lynch
release_cache_debugcheck(struct cache * cache)16793197a36SNathan Lynch static void release_cache_debugcheck(struct cache *cache)
16893197a36SNathan Lynch {
16993197a36SNathan Lynch struct cache *iter;
17093197a36SNathan Lynch
17193197a36SNathan Lynch list_for_each_entry(iter, &cache_list, list)
17293197a36SNathan Lynch WARN_ONCE(iter->next_local == cache,
173be6f885eSNathan Lynch "cache for %pOFP(%s) refers to cache for %pOFP(%s)\n",
174b7c670d6SRob Herring iter->ofnode,
17593197a36SNathan Lynch cache_type_string(iter),
176b7c670d6SRob Herring cache->ofnode,
17793197a36SNathan Lynch cache_type_string(cache));
17893197a36SNathan Lynch }
17993197a36SNathan Lynch
release_cache(struct cache * cache)18093197a36SNathan Lynch static void release_cache(struct cache *cache)
18193197a36SNathan Lynch {
18293197a36SNathan Lynch if (!cache)
18393197a36SNathan Lynch return;
18493197a36SNathan Lynch
185be6f885eSNathan Lynch pr_debug("freeing L%d %s cache for %pOFP\n", cache->level,
186b7c670d6SRob Herring cache_type_string(cache), cache->ofnode);
18793197a36SNathan Lynch
18893197a36SNathan Lynch release_cache_debugcheck(cache);
18993197a36SNathan Lynch list_del(&cache->list);
19093197a36SNathan Lynch of_node_put(cache->ofnode);
19193197a36SNathan Lynch kfree(cache);
19293197a36SNathan Lynch }
19393197a36SNathan Lynch
cache_cpu_set(struct cache * cache,int cpu)19493197a36SNathan Lynch static void cache_cpu_set(struct cache *cache, int cpu)
19593197a36SNathan Lynch {
19693197a36SNathan Lynch struct cache *next = cache;
19793197a36SNathan Lynch
19893197a36SNathan Lynch while (next) {
19993197a36SNathan Lynch WARN_ONCE(cpumask_test_cpu(cpu, &next->shared_cpu_map),
200be6f885eSNathan Lynch "CPU %i already accounted in %pOFP(%s)\n",
201b7c670d6SRob Herring cpu, next->ofnode,
20293197a36SNathan Lynch cache_type_string(next));
20393197a36SNathan Lynch cpumask_set_cpu(cpu, &next->shared_cpu_map);
20493197a36SNathan Lynch next = next->next_local;
20593197a36SNathan Lynch }
20693197a36SNathan Lynch }
20793197a36SNathan Lynch
cache_size(const struct cache * cache,unsigned int * ret)20893197a36SNathan Lynch static int cache_size(const struct cache *cache, unsigned int *ret)
20993197a36SNathan Lynch {
21093197a36SNathan Lynch const char *propname;
211d10bd84fSAnton Blanchard const __be32 *cache_size;
21293197a36SNathan Lynch
21393197a36SNathan Lynch propname = cache_type_info[cache->type].size_prop;
21493197a36SNathan Lynch
21593197a36SNathan Lynch cache_size = of_get_property(cache->ofnode, propname, NULL);
21693197a36SNathan Lynch if (!cache_size)
21793197a36SNathan Lynch return -ENODEV;
21893197a36SNathan Lynch
219d10bd84fSAnton Blanchard *ret = of_read_number(cache_size, 1);
22093197a36SNathan Lynch return 0;
22193197a36SNathan Lynch }
22293197a36SNathan Lynch
cache_size_kb(const struct cache * cache,unsigned int * ret)22393197a36SNathan Lynch static int cache_size_kb(const struct cache *cache, unsigned int *ret)
22493197a36SNathan Lynch {
22593197a36SNathan Lynch unsigned int size;
22693197a36SNathan Lynch
22793197a36SNathan Lynch if (cache_size(cache, &size))
22893197a36SNathan Lynch return -ENODEV;
22993197a36SNathan Lynch
23093197a36SNathan Lynch *ret = size / 1024;
23193197a36SNathan Lynch return 0;
23293197a36SNathan Lynch }
23393197a36SNathan Lynch
23493197a36SNathan Lynch /* not cache_line_size() because that's a macro in include/linux/cache.h */
cache_get_line_size(const struct cache * cache,unsigned int * ret)23593197a36SNathan Lynch static int cache_get_line_size(const struct cache *cache, unsigned int *ret)
23693197a36SNathan Lynch {
237d10bd84fSAnton Blanchard const __be32 *line_size;
23893197a36SNathan Lynch int i, lim;
23993197a36SNathan Lynch
24093197a36SNathan Lynch lim = ARRAY_SIZE(cache_type_info[cache->type].line_size_props);
24193197a36SNathan Lynch
24293197a36SNathan Lynch for (i = 0; i < lim; i++) {
24393197a36SNathan Lynch const char *propname;
24493197a36SNathan Lynch
24593197a36SNathan Lynch propname = cache_type_info[cache->type].line_size_props[i];
24693197a36SNathan Lynch line_size = of_get_property(cache->ofnode, propname, NULL);
24793197a36SNathan Lynch if (line_size)
24893197a36SNathan Lynch break;
24993197a36SNathan Lynch }
25093197a36SNathan Lynch
25193197a36SNathan Lynch if (!line_size)
25293197a36SNathan Lynch return -ENODEV;
25393197a36SNathan Lynch
254d10bd84fSAnton Blanchard *ret = of_read_number(line_size, 1);
25593197a36SNathan Lynch return 0;
25693197a36SNathan Lynch }
25793197a36SNathan Lynch
cache_nr_sets(const struct cache * cache,unsigned int * ret)25893197a36SNathan Lynch static int cache_nr_sets(const struct cache *cache, unsigned int *ret)
25993197a36SNathan Lynch {
26093197a36SNathan Lynch const char *propname;
261d10bd84fSAnton Blanchard const __be32 *nr_sets;
26293197a36SNathan Lynch
26393197a36SNathan Lynch propname = cache_type_info[cache->type].nr_sets_prop;
26493197a36SNathan Lynch
26593197a36SNathan Lynch nr_sets = of_get_property(cache->ofnode, propname, NULL);
26693197a36SNathan Lynch if (!nr_sets)
26793197a36SNathan Lynch return -ENODEV;
26893197a36SNathan Lynch
269d10bd84fSAnton Blanchard *ret = of_read_number(nr_sets, 1);
27093197a36SNathan Lynch return 0;
27193197a36SNathan Lynch }
27293197a36SNathan Lynch
cache_associativity(const struct cache * cache,unsigned int * ret)27393197a36SNathan Lynch static int cache_associativity(const struct cache *cache, unsigned int *ret)
27493197a36SNathan Lynch {
27593197a36SNathan Lynch unsigned int line_size;
27693197a36SNathan Lynch unsigned int nr_sets;
27793197a36SNathan Lynch unsigned int size;
27893197a36SNathan Lynch
27993197a36SNathan Lynch if (cache_nr_sets(cache, &nr_sets))
28093197a36SNathan Lynch goto err;
28193197a36SNathan Lynch
28293197a36SNathan Lynch /* If the cache is fully associative, there is no need to
28393197a36SNathan Lynch * check the other properties.
28493197a36SNathan Lynch */
28593197a36SNathan Lynch if (nr_sets == 1) {
28693197a36SNathan Lynch *ret = 0;
28793197a36SNathan Lynch return 0;
28893197a36SNathan Lynch }
28993197a36SNathan Lynch
29093197a36SNathan Lynch if (cache_get_line_size(cache, &line_size))
29193197a36SNathan Lynch goto err;
29293197a36SNathan Lynch if (cache_size(cache, &size))
29393197a36SNathan Lynch goto err;
29493197a36SNathan Lynch
29593197a36SNathan Lynch if (!(nr_sets > 0 && size > 0 && line_size > 0))
29693197a36SNathan Lynch goto err;
29793197a36SNathan Lynch
29893197a36SNathan Lynch *ret = (size / nr_sets) / line_size;
29993197a36SNathan Lynch return 0;
30093197a36SNathan Lynch err:
30193197a36SNathan Lynch return -ENODEV;
30293197a36SNathan Lynch }
30393197a36SNathan Lynch
30493197a36SNathan Lynch /* helper for dealing with split caches */
cache_find_first_sibling(struct cache * cache)30593197a36SNathan Lynch static struct cache *cache_find_first_sibling(struct cache *cache)
30693197a36SNathan Lynch {
30793197a36SNathan Lynch struct cache *iter;
30893197a36SNathan Lynch
309f7e9e358SDave Olson if (cache->type == CACHE_TYPE_UNIFIED ||
310f7e9e358SDave Olson cache->type == CACHE_TYPE_UNIFIED_D)
31193197a36SNathan Lynch return cache;
31293197a36SNathan Lynch
31393197a36SNathan Lynch list_for_each_entry(iter, &cache_list, list)
314a4bec516SGautham R. Shenoy if (iter->ofnode == cache->ofnode &&
315a4bec516SGautham R. Shenoy iter->group_id == cache->group_id &&
316a4bec516SGautham R. Shenoy iter->next_local == cache)
31793197a36SNathan Lynch return iter;
31893197a36SNathan Lynch
31993197a36SNathan Lynch return cache;
32093197a36SNathan Lynch }
32193197a36SNathan Lynch
322a4bec516SGautham R. Shenoy /* return the first cache on a local list matching node and thread-group id */
cache_lookup_by_node_group(const struct device_node * node,int group_id)323a4bec516SGautham R. Shenoy static struct cache *cache_lookup_by_node_group(const struct device_node *node,
324a4bec516SGautham R. Shenoy int group_id)
32593197a36SNathan Lynch {
32693197a36SNathan Lynch struct cache *cache = NULL;
32793197a36SNathan Lynch struct cache *iter;
32893197a36SNathan Lynch
32993197a36SNathan Lynch list_for_each_entry(iter, &cache_list, list) {
330a4bec516SGautham R. Shenoy if (iter->ofnode != node ||
331a4bec516SGautham R. Shenoy iter->group_id != group_id)
33293197a36SNathan Lynch continue;
33393197a36SNathan Lynch cache = cache_find_first_sibling(iter);
33493197a36SNathan Lynch break;
33593197a36SNathan Lynch }
33693197a36SNathan Lynch
33793197a36SNathan Lynch return cache;
33893197a36SNathan Lynch }
33993197a36SNathan Lynch
cache_node_is_unified(const struct device_node * np)34093197a36SNathan Lynch static bool cache_node_is_unified(const struct device_node *np)
34193197a36SNathan Lynch {
34293197a36SNathan Lynch return of_get_property(np, "cache-unified", NULL);
34393197a36SNathan Lynch }
34493197a36SNathan Lynch
345f7e9e358SDave Olson /*
346f7e9e358SDave Olson * Unified caches can have two different sets of tags. Most embedded
347f7e9e358SDave Olson * use cache-size, etc. for the unified cache size, but open firmware systems
348f7e9e358SDave Olson * use d-cache-size, etc. Check on initialization for which type we have, and
349f7e9e358SDave Olson * return the appropriate structure type. Assume it's embedded if it isn't
350f7e9e358SDave Olson * open firmware. If it's yet a 3rd type, then there will be missing entries
351f7e9e358SDave Olson * in /sys/devices/system/cpu/cpu0/cache/index2/, and this code will need
352f7e9e358SDave Olson * to be extended further.
353f7e9e358SDave Olson */
cache_is_unified_d(const struct device_node * np)354f7e9e358SDave Olson static int cache_is_unified_d(const struct device_node *np)
35593197a36SNathan Lynch {
356f7e9e358SDave Olson return of_get_property(np,
357f7e9e358SDave Olson cache_type_info[CACHE_TYPE_UNIFIED_D].size_prop, NULL) ?
358f7e9e358SDave Olson CACHE_TYPE_UNIFIED_D : CACHE_TYPE_UNIFIED;
359f7e9e358SDave Olson }
36093197a36SNathan Lynch
cache_do_one_devnode_unified(struct device_node * node,int group_id,int level)361a4bec516SGautham R. Shenoy static struct cache *cache_do_one_devnode_unified(struct device_node *node, int group_id,
362a4bec516SGautham R. Shenoy int level)
363f7e9e358SDave Olson {
364be6f885eSNathan Lynch pr_debug("creating L%d ucache for %pOFP\n", level, node);
36593197a36SNathan Lynch
366a4bec516SGautham R. Shenoy return new_cache(cache_is_unified_d(node), level, node, group_id);
36793197a36SNathan Lynch }
36893197a36SNathan Lynch
cache_do_one_devnode_split(struct device_node * node,int group_id,int level)369a4bec516SGautham R. Shenoy static struct cache *cache_do_one_devnode_split(struct device_node *node, int group_id,
370061d19f2SPaul Gortmaker int level)
37193197a36SNathan Lynch {
37293197a36SNathan Lynch struct cache *dcache, *icache;
37393197a36SNathan Lynch
374be6f885eSNathan Lynch pr_debug("creating L%d dcache and icache for %pOFP\n", level,
375b7c670d6SRob Herring node);
37693197a36SNathan Lynch
377a4bec516SGautham R. Shenoy dcache = new_cache(CACHE_TYPE_DATA, level, node, group_id);
378a4bec516SGautham R. Shenoy icache = new_cache(CACHE_TYPE_INSTRUCTION, level, node, group_id);
37993197a36SNathan Lynch
38093197a36SNathan Lynch if (!dcache || !icache)
38193197a36SNathan Lynch goto err;
38293197a36SNathan Lynch
38393197a36SNathan Lynch dcache->next_local = icache;
38493197a36SNathan Lynch
38593197a36SNathan Lynch return dcache;
38693197a36SNathan Lynch err:
38793197a36SNathan Lynch release_cache(dcache);
38893197a36SNathan Lynch release_cache(icache);
38993197a36SNathan Lynch return NULL;
39093197a36SNathan Lynch }
39193197a36SNathan Lynch
cache_do_one_devnode(struct device_node * node,int group_id,int level)392a4bec516SGautham R. Shenoy static struct cache *cache_do_one_devnode(struct device_node *node, int group_id, int level)
39393197a36SNathan Lynch {
39493197a36SNathan Lynch struct cache *cache;
39593197a36SNathan Lynch
39693197a36SNathan Lynch if (cache_node_is_unified(node))
397a4bec516SGautham R. Shenoy cache = cache_do_one_devnode_unified(node, group_id, level);
39893197a36SNathan Lynch else
399a4bec516SGautham R. Shenoy cache = cache_do_one_devnode_split(node, group_id, level);
40093197a36SNathan Lynch
40193197a36SNathan Lynch return cache;
40293197a36SNathan Lynch }
40393197a36SNathan Lynch
cache_lookup_or_instantiate(struct device_node * node,int group_id,int level)404061d19f2SPaul Gortmaker static struct cache *cache_lookup_or_instantiate(struct device_node *node,
405a4bec516SGautham R. Shenoy int group_id,
406061d19f2SPaul Gortmaker int level)
40793197a36SNathan Lynch {
40893197a36SNathan Lynch struct cache *cache;
40993197a36SNathan Lynch
410a4bec516SGautham R. Shenoy cache = cache_lookup_by_node_group(node, group_id);
41193197a36SNathan Lynch
41293197a36SNathan Lynch WARN_ONCE(cache && cache->level != level,
41393197a36SNathan Lynch "cache level mismatch on lookup (got %d, expected %d)\n",
41493197a36SNathan Lynch cache->level, level);
41593197a36SNathan Lynch
41693197a36SNathan Lynch if (!cache)
417a4bec516SGautham R. Shenoy cache = cache_do_one_devnode(node, group_id, level);
41893197a36SNathan Lynch
41993197a36SNathan Lynch return cache;
42093197a36SNathan Lynch }
42193197a36SNathan Lynch
link_cache_lists(struct cache * smaller,struct cache * bigger)422061d19f2SPaul Gortmaker static void link_cache_lists(struct cache *smaller, struct cache *bigger)
42393197a36SNathan Lynch {
42493197a36SNathan Lynch while (smaller->next_local) {
42593197a36SNathan Lynch if (smaller->next_local == bigger)
42693197a36SNathan Lynch return; /* already linked */
42793197a36SNathan Lynch smaller = smaller->next_local;
42893197a36SNathan Lynch }
42993197a36SNathan Lynch
43093197a36SNathan Lynch smaller->next_local = bigger;
4316ec54363SNathan Lynch
4326ec54363SNathan Lynch /*
4336ec54363SNathan Lynch * The cache->next_local list sorts by level ascending:
4346ec54363SNathan Lynch * L1d -> L1i -> L2 -> L3 ...
4356ec54363SNathan Lynch */
4366ec54363SNathan Lynch WARN_ONCE((smaller->level == 1 && bigger->level > 2) ||
4376ec54363SNathan Lynch (smaller->level > 1 && bigger->level != smaller->level + 1),
4386ec54363SNathan Lynch "linking L%i cache %pOFP to L%i cache %pOFP; skipped a level?\n",
4396ec54363SNathan Lynch smaller->level, smaller->ofnode, bigger->level, bigger->ofnode);
44093197a36SNathan Lynch }
44193197a36SNathan Lynch
do_subsidiary_caches_debugcheck(struct cache * cache)442061d19f2SPaul Gortmaker static void do_subsidiary_caches_debugcheck(struct cache *cache)
44393197a36SNathan Lynch {
4441b3da8ffSNathan Lynch WARN_ONCE(cache->level != 1,
4451b3da8ffSNathan Lynch "instantiating cache chain from L%d %s cache for "
4461b3da8ffSNathan Lynch "%pOFP instead of an L1\n", cache->level,
4471b3da8ffSNathan Lynch cache_type_string(cache), cache->ofnode);
4481b3da8ffSNathan Lynch WARN_ONCE(!of_node_is_type(cache->ofnode, "cpu"),
4491b3da8ffSNathan Lynch "instantiating cache chain from node %pOFP of type '%s' "
4501b3da8ffSNathan Lynch "instead of a cpu node\n", cache->ofnode,
4511b3da8ffSNathan Lynch of_node_get_device_type(cache->ofnode));
45293197a36SNathan Lynch }
45393197a36SNathan Lynch
454a4bec516SGautham R. Shenoy /*
455a4bec516SGautham R. Shenoy * If sub-groups of threads in a core containing @cpu_id share the
456a4bec516SGautham R. Shenoy * L@level-cache (information obtained via "ibm,thread-groups"
457a4bec516SGautham R. Shenoy * device-tree property), then we identify the group by the first
458a4bec516SGautham R. Shenoy * thread-sibling in the group. We define this to be the group-id.
459a4bec516SGautham R. Shenoy *
460a4bec516SGautham R. Shenoy * In the absence of any thread-group information for L@level-cache,
461a4bec516SGautham R. Shenoy * this function returns -1.
462a4bec516SGautham R. Shenoy */
get_group_id(unsigned int cpu_id,int level)463a4bec516SGautham R. Shenoy static int get_group_id(unsigned int cpu_id, int level)
464a4bec516SGautham R. Shenoy {
465a4bec516SGautham R. Shenoy if (has_big_cores && level == 1)
466a4bec516SGautham R. Shenoy return cpumask_first(per_cpu(thread_group_l1_cache_map,
467a4bec516SGautham R. Shenoy cpu_id));
468a4bec516SGautham R. Shenoy else if (thread_group_shares_l2 && level == 2)
469a4bec516SGautham R. Shenoy return cpumask_first(per_cpu(thread_group_l2_cache_map,
470a4bec516SGautham R. Shenoy cpu_id));
471e9ef81e1SParth Shah else if (thread_group_shares_l3 && level == 3)
472e9ef81e1SParth Shah return cpumask_first(per_cpu(thread_group_l3_cache_map,
473e9ef81e1SParth Shah cpu_id));
474a4bec516SGautham R. Shenoy return -1;
475a4bec516SGautham R. Shenoy }
476a4bec516SGautham R. Shenoy
do_subsidiary_caches(struct cache * cache,unsigned int cpu_id)477a4bec516SGautham R. Shenoy static void do_subsidiary_caches(struct cache *cache, unsigned int cpu_id)
47893197a36SNathan Lynch {
47993197a36SNathan Lynch struct device_node *subcache_node;
48093197a36SNathan Lynch int level = cache->level;
48193197a36SNathan Lynch
48293197a36SNathan Lynch do_subsidiary_caches_debugcheck(cache);
48393197a36SNathan Lynch
48493197a36SNathan Lynch while ((subcache_node = of_find_next_cache_node(cache->ofnode))) {
48593197a36SNathan Lynch struct cache *subcache;
486a4bec516SGautham R. Shenoy int group_id;
48793197a36SNathan Lynch
48893197a36SNathan Lynch level++;
489a4bec516SGautham R. Shenoy group_id = get_group_id(cpu_id, level);
490a4bec516SGautham R. Shenoy subcache = cache_lookup_or_instantiate(subcache_node, group_id, level);
49193197a36SNathan Lynch of_node_put(subcache_node);
49293197a36SNathan Lynch if (!subcache)
49393197a36SNathan Lynch break;
49493197a36SNathan Lynch
49593197a36SNathan Lynch link_cache_lists(cache, subcache);
49693197a36SNathan Lynch cache = subcache;
49793197a36SNathan Lynch }
49893197a36SNathan Lynch }
49993197a36SNathan Lynch
cache_chain_instantiate(unsigned int cpu_id)500061d19f2SPaul Gortmaker static struct cache *cache_chain_instantiate(unsigned int cpu_id)
50193197a36SNathan Lynch {
50293197a36SNathan Lynch struct device_node *cpu_node;
50393197a36SNathan Lynch struct cache *cpu_cache = NULL;
504a4bec516SGautham R. Shenoy int group_id;
50593197a36SNathan Lynch
50693197a36SNathan Lynch pr_debug("creating cache object(s) for CPU %i\n", cpu_id);
50793197a36SNathan Lynch
50893197a36SNathan Lynch cpu_node = of_get_cpu_node(cpu_id, NULL);
50993197a36SNathan Lynch WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
51093197a36SNathan Lynch if (!cpu_node)
51193197a36SNathan Lynch goto out;
51293197a36SNathan Lynch
513a4bec516SGautham R. Shenoy group_id = get_group_id(cpu_id, 1);
514a4bec516SGautham R. Shenoy
515a4bec516SGautham R. Shenoy cpu_cache = cache_lookup_or_instantiate(cpu_node, group_id, 1);
51693197a36SNathan Lynch if (!cpu_cache)
51793197a36SNathan Lynch goto out;
51893197a36SNathan Lynch
519a4bec516SGautham R. Shenoy do_subsidiary_caches(cpu_cache, cpu_id);
52093197a36SNathan Lynch
52193197a36SNathan Lynch cache_cpu_set(cpu_cache, cpu_id);
52293197a36SNathan Lynch out:
52393197a36SNathan Lynch of_node_put(cpu_node);
52493197a36SNathan Lynch
52593197a36SNathan Lynch return cpu_cache;
52693197a36SNathan Lynch }
52793197a36SNathan Lynch
cacheinfo_create_cache_dir(unsigned int cpu_id)528061d19f2SPaul Gortmaker static struct cache_dir *cacheinfo_create_cache_dir(unsigned int cpu_id)
52993197a36SNathan Lynch {
53093197a36SNathan Lynch struct cache_dir *cache_dir;
5318a25a2fdSKay Sievers struct device *dev;
53293197a36SNathan Lynch struct kobject *kobj = NULL;
53393197a36SNathan Lynch
5348a25a2fdSKay Sievers dev = get_cpu_device(cpu_id);
5358a25a2fdSKay Sievers WARN_ONCE(!dev, "no dev for CPU %i\n", cpu_id);
5368a25a2fdSKay Sievers if (!dev)
53793197a36SNathan Lynch goto err;
53893197a36SNathan Lynch
5398a25a2fdSKay Sievers kobj = kobject_create_and_add("cache", &dev->kobj);
54093197a36SNathan Lynch if (!kobj)
54193197a36SNathan Lynch goto err;
54293197a36SNathan Lynch
54393197a36SNathan Lynch cache_dir = kzalloc(sizeof(*cache_dir), GFP_KERNEL);
54493197a36SNathan Lynch if (!cache_dir)
54593197a36SNathan Lynch goto err;
54693197a36SNathan Lynch
54793197a36SNathan Lynch cache_dir->kobj = kobj;
54893197a36SNathan Lynch
549fc7a9febSNathan Lynch WARN_ON_ONCE(per_cpu(cache_dir_pcpu, cpu_id) != NULL);
55093197a36SNathan Lynch
551fc7a9febSNathan Lynch per_cpu(cache_dir_pcpu, cpu_id) = cache_dir;
55293197a36SNathan Lynch
55393197a36SNathan Lynch return cache_dir;
55493197a36SNathan Lynch err:
55593197a36SNathan Lynch kobject_put(kobj);
55693197a36SNathan Lynch return NULL;
55793197a36SNathan Lynch }
55893197a36SNathan Lynch
cache_index_release(struct kobject * kobj)55993197a36SNathan Lynch static void cache_index_release(struct kobject *kobj)
56093197a36SNathan Lynch {
56193197a36SNathan Lynch struct cache_index_dir *index;
56293197a36SNathan Lynch
56393197a36SNathan Lynch index = kobj_to_cache_index_dir(kobj);
56493197a36SNathan Lynch
56593197a36SNathan Lynch pr_debug("freeing index directory for L%d %s cache\n",
56693197a36SNathan Lynch index->cache->level, cache_type_string(index->cache));
56793197a36SNathan Lynch
56893197a36SNathan Lynch kfree(index);
56993197a36SNathan Lynch }
57093197a36SNathan Lynch
cache_index_show(struct kobject * k,struct attribute * attr,char * buf)57193197a36SNathan Lynch static ssize_t cache_index_show(struct kobject *k, struct attribute *attr, char *buf)
57293197a36SNathan Lynch {
57393197a36SNathan Lynch struct kobj_attribute *kobj_attr;
57493197a36SNathan Lynch
57593197a36SNathan Lynch kobj_attr = container_of(attr, struct kobj_attribute, attr);
57693197a36SNathan Lynch
57793197a36SNathan Lynch return kobj_attr->show(k, kobj_attr, buf);
57893197a36SNathan Lynch }
57993197a36SNathan Lynch
index_kobj_to_cache(struct kobject * k)58093197a36SNathan Lynch static struct cache *index_kobj_to_cache(struct kobject *k)
58193197a36SNathan Lynch {
58293197a36SNathan Lynch struct cache_index_dir *index;
58393197a36SNathan Lynch
58493197a36SNathan Lynch index = kobj_to_cache_index_dir(k);
58593197a36SNathan Lynch
58693197a36SNathan Lynch return index->cache;
58793197a36SNathan Lynch }
58893197a36SNathan Lynch
size_show(struct kobject * k,struct kobj_attribute * attr,char * buf)58993197a36SNathan Lynch static ssize_t size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
59093197a36SNathan Lynch {
59193197a36SNathan Lynch unsigned int size_kb;
59293197a36SNathan Lynch struct cache *cache;
59393197a36SNathan Lynch
59493197a36SNathan Lynch cache = index_kobj_to_cache(k);
59593197a36SNathan Lynch
59693197a36SNathan Lynch if (cache_size_kb(cache, &size_kb))
59793197a36SNathan Lynch return -ENODEV;
59893197a36SNathan Lynch
59993197a36SNathan Lynch return sprintf(buf, "%uK\n", size_kb);
60093197a36SNathan Lynch }
60193197a36SNathan Lynch
60293197a36SNathan Lynch static struct kobj_attribute cache_size_attr =
60393197a36SNathan Lynch __ATTR(size, 0444, size_show, NULL);
60493197a36SNathan Lynch
60593197a36SNathan Lynch
line_size_show(struct kobject * k,struct kobj_attribute * attr,char * buf)60693197a36SNathan Lynch static ssize_t line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
60793197a36SNathan Lynch {
60893197a36SNathan Lynch unsigned int line_size;
60993197a36SNathan Lynch struct cache *cache;
61093197a36SNathan Lynch
61193197a36SNathan Lynch cache = index_kobj_to_cache(k);
61293197a36SNathan Lynch
61393197a36SNathan Lynch if (cache_get_line_size(cache, &line_size))
61493197a36SNathan Lynch return -ENODEV;
61593197a36SNathan Lynch
61693197a36SNathan Lynch return sprintf(buf, "%u\n", line_size);
61793197a36SNathan Lynch }
61893197a36SNathan Lynch
61993197a36SNathan Lynch static struct kobj_attribute cache_line_size_attr =
62093197a36SNathan Lynch __ATTR(coherency_line_size, 0444, line_size_show, NULL);
62193197a36SNathan Lynch
nr_sets_show(struct kobject * k,struct kobj_attribute * attr,char * buf)62293197a36SNathan Lynch static ssize_t nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
62393197a36SNathan Lynch {
62493197a36SNathan Lynch unsigned int nr_sets;
62593197a36SNathan Lynch struct cache *cache;
62693197a36SNathan Lynch
62793197a36SNathan Lynch cache = index_kobj_to_cache(k);
62893197a36SNathan Lynch
62993197a36SNathan Lynch if (cache_nr_sets(cache, &nr_sets))
63093197a36SNathan Lynch return -ENODEV;
63193197a36SNathan Lynch
63293197a36SNathan Lynch return sprintf(buf, "%u\n", nr_sets);
63393197a36SNathan Lynch }
63493197a36SNathan Lynch
63593197a36SNathan Lynch static struct kobj_attribute cache_nr_sets_attr =
63693197a36SNathan Lynch __ATTR(number_of_sets, 0444, nr_sets_show, NULL);
63793197a36SNathan Lynch
associativity_show(struct kobject * k,struct kobj_attribute * attr,char * buf)63893197a36SNathan Lynch static ssize_t associativity_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
63993197a36SNathan Lynch {
64093197a36SNathan Lynch unsigned int associativity;
64193197a36SNathan Lynch struct cache *cache;
64293197a36SNathan Lynch
64393197a36SNathan Lynch cache = index_kobj_to_cache(k);
64493197a36SNathan Lynch
64593197a36SNathan Lynch if (cache_associativity(cache, &associativity))
64693197a36SNathan Lynch return -ENODEV;
64793197a36SNathan Lynch
64893197a36SNathan Lynch return sprintf(buf, "%u\n", associativity);
64993197a36SNathan Lynch }
65093197a36SNathan Lynch
65193197a36SNathan Lynch static struct kobj_attribute cache_assoc_attr =
65293197a36SNathan Lynch __ATTR(ways_of_associativity, 0444, associativity_show, NULL);
65393197a36SNathan Lynch
type_show(struct kobject * k,struct kobj_attribute * attr,char * buf)65493197a36SNathan Lynch static ssize_t type_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
65593197a36SNathan Lynch {
65693197a36SNathan Lynch struct cache *cache;
65793197a36SNathan Lynch
65893197a36SNathan Lynch cache = index_kobj_to_cache(k);
65993197a36SNathan Lynch
66093197a36SNathan Lynch return sprintf(buf, "%s\n", cache_type_string(cache));
66193197a36SNathan Lynch }
66293197a36SNathan Lynch
66393197a36SNathan Lynch static struct kobj_attribute cache_type_attr =
66493197a36SNathan Lynch __ATTR(type, 0444, type_show, NULL);
66593197a36SNathan Lynch
level_show(struct kobject * k,struct kobj_attribute * attr,char * buf)66693197a36SNathan Lynch static ssize_t level_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
66793197a36SNathan Lynch {
66893197a36SNathan Lynch struct cache_index_dir *index;
66993197a36SNathan Lynch struct cache *cache;
67093197a36SNathan Lynch
67193197a36SNathan Lynch index = kobj_to_cache_index_dir(k);
67293197a36SNathan Lynch cache = index->cache;
67393197a36SNathan Lynch
67493197a36SNathan Lynch return sprintf(buf, "%d\n", cache->level);
67593197a36SNathan Lynch }
67693197a36SNathan Lynch
67793197a36SNathan Lynch static struct kobj_attribute cache_level_attr =
67893197a36SNathan Lynch __ATTR(level, 0444, level_show, NULL);
67993197a36SNathan Lynch
68074b7492eSSrikar Dronamraju static ssize_t
show_shared_cpumap(struct kobject * k,struct kobj_attribute * attr,char * buf,bool list)68174b7492eSSrikar Dronamraju show_shared_cpumap(struct kobject *k, struct kobj_attribute *attr, char *buf, bool list)
68293197a36SNathan Lynch {
68393197a36SNathan Lynch struct cache_index_dir *index;
68493197a36SNathan Lynch struct cache *cache;
685500fe5f5SGautham R. Shenoy const struct cpumask *mask;
68693197a36SNathan Lynch
68793197a36SNathan Lynch index = kobj_to_cache_index_dir(k);
68893197a36SNathan Lynch cache = index->cache;
68993197a36SNathan Lynch
69069aa8e07SGautham R. Shenoy mask = &cache->shared_cpu_map;
691500fe5f5SGautham R. Shenoy
69274b7492eSSrikar Dronamraju return cpumap_print_to_pagebuf(list, buf, mask);
69374b7492eSSrikar Dronamraju }
69474b7492eSSrikar Dronamraju
shared_cpu_map_show(struct kobject * k,struct kobj_attribute * attr,char * buf)69574b7492eSSrikar Dronamraju static ssize_t shared_cpu_map_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
69674b7492eSSrikar Dronamraju {
697a87a77cbSSrikar Dronamraju return show_shared_cpumap(k, attr, buf, false);
698a87a77cbSSrikar Dronamraju }
699a87a77cbSSrikar Dronamraju
shared_cpu_list_show(struct kobject * k,struct kobj_attribute * attr,char * buf)700a87a77cbSSrikar Dronamraju static ssize_t shared_cpu_list_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
701a87a77cbSSrikar Dronamraju {
702a87a77cbSSrikar Dronamraju return show_shared_cpumap(k, attr, buf, true);
70393197a36SNathan Lynch }
70493197a36SNathan Lynch
70593197a36SNathan Lynch static struct kobj_attribute cache_shared_cpu_map_attr =
70693197a36SNathan Lynch __ATTR(shared_cpu_map, 0444, shared_cpu_map_show, NULL);
70793197a36SNathan Lynch
708a87a77cbSSrikar Dronamraju static struct kobj_attribute cache_shared_cpu_list_attr =
709a87a77cbSSrikar Dronamraju __ATTR(shared_cpu_list, 0444, shared_cpu_list_show, NULL);
710a87a77cbSSrikar Dronamraju
71193197a36SNathan Lynch /* Attributes which should always be created -- the kobject/sysfs core
712*2bdf3f9eSGreg Kroah-Hartman * does this automatically via kobj_type->default_groups. This is the
71393197a36SNathan Lynch * minimum data required to uniquely identify a cache.
71493197a36SNathan Lynch */
71593197a36SNathan Lynch static struct attribute *cache_index_default_attrs[] = {
71693197a36SNathan Lynch &cache_type_attr.attr,
71793197a36SNathan Lynch &cache_level_attr.attr,
71893197a36SNathan Lynch &cache_shared_cpu_map_attr.attr,
719a87a77cbSSrikar Dronamraju &cache_shared_cpu_list_attr.attr,
72093197a36SNathan Lynch NULL,
72193197a36SNathan Lynch };
722*2bdf3f9eSGreg Kroah-Hartman ATTRIBUTE_GROUPS(cache_index_default);
72393197a36SNathan Lynch
72493197a36SNathan Lynch /* Attributes which should be created if the cache device node has the
72593197a36SNathan Lynch * right properties -- see cacheinfo_create_index_opt_attrs
72693197a36SNathan Lynch */
72793197a36SNathan Lynch static struct kobj_attribute *cache_index_opt_attrs[] = {
72893197a36SNathan Lynch &cache_size_attr,
72993197a36SNathan Lynch &cache_line_size_attr,
73093197a36SNathan Lynch &cache_nr_sets_attr,
73193197a36SNathan Lynch &cache_assoc_attr,
73293197a36SNathan Lynch };
73393197a36SNathan Lynch
73452cf25d0SEmese Revfy static const struct sysfs_ops cache_index_ops = {
73593197a36SNathan Lynch .show = cache_index_show,
73693197a36SNathan Lynch };
73793197a36SNathan Lynch
73893197a36SNathan Lynch static struct kobj_type cache_index_type = {
73993197a36SNathan Lynch .release = cache_index_release,
74093197a36SNathan Lynch .sysfs_ops = &cache_index_ops,
741*2bdf3f9eSGreg Kroah-Hartman .default_groups = cache_index_default_groups,
74293197a36SNathan Lynch };
74393197a36SNathan Lynch
cacheinfo_create_index_opt_attrs(struct cache_index_dir * dir)744061d19f2SPaul Gortmaker static void cacheinfo_create_index_opt_attrs(struct cache_index_dir *dir)
74593197a36SNathan Lynch {
74693197a36SNathan Lynch const char *cache_type;
74793197a36SNathan Lynch struct cache *cache;
74893197a36SNathan Lynch char *buf;
74993197a36SNathan Lynch int i;
75093197a36SNathan Lynch
75193197a36SNathan Lynch buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
75293197a36SNathan Lynch if (!buf)
75393197a36SNathan Lynch return;
75493197a36SNathan Lynch
75593197a36SNathan Lynch cache = dir->cache;
75693197a36SNathan Lynch cache_type = cache_type_string(cache);
75793197a36SNathan Lynch
75893197a36SNathan Lynch /* We don't want to create an attribute that can't provide a
75993197a36SNathan Lynch * meaningful value. Check the return value of each optional
76093197a36SNathan Lynch * attribute's ->show method before registering the
76193197a36SNathan Lynch * attribute.
76293197a36SNathan Lynch */
76393197a36SNathan Lynch for (i = 0; i < ARRAY_SIZE(cache_index_opt_attrs); i++) {
76493197a36SNathan Lynch struct kobj_attribute *attr;
76593197a36SNathan Lynch ssize_t rc;
76693197a36SNathan Lynch
76793197a36SNathan Lynch attr = cache_index_opt_attrs[i];
76893197a36SNathan Lynch
76993197a36SNathan Lynch rc = attr->show(&dir->kobj, attr, buf);
77093197a36SNathan Lynch if (rc <= 0) {
77193197a36SNathan Lynch pr_debug("not creating %s attribute for "
772be6f885eSNathan Lynch "%pOFP(%s) (rc = %zd)\n",
773b7c670d6SRob Herring attr->attr.name, cache->ofnode,
77493197a36SNathan Lynch cache_type, rc);
77593197a36SNathan Lynch continue;
77693197a36SNathan Lynch }
77793197a36SNathan Lynch if (sysfs_create_file(&dir->kobj, &attr->attr))
778be6f885eSNathan Lynch pr_debug("could not create %s attribute for %pOFP(%s)\n",
779b7c670d6SRob Herring attr->attr.name, cache->ofnode, cache_type);
78093197a36SNathan Lynch }
78193197a36SNathan Lynch
78293197a36SNathan Lynch kfree(buf);
78393197a36SNathan Lynch }
78493197a36SNathan Lynch
cacheinfo_create_index_dir(struct cache * cache,int index,struct cache_dir * cache_dir)785061d19f2SPaul Gortmaker static void cacheinfo_create_index_dir(struct cache *cache, int index,
786061d19f2SPaul Gortmaker struct cache_dir *cache_dir)
78793197a36SNathan Lynch {
78893197a36SNathan Lynch struct cache_index_dir *index_dir;
78993197a36SNathan Lynch int rc;
79093197a36SNathan Lynch
79193197a36SNathan Lynch index_dir = kzalloc(sizeof(*index_dir), GFP_KERNEL);
79293197a36SNathan Lynch if (!index_dir)
7937e803979STobin C. Harding return;
79493197a36SNathan Lynch
79593197a36SNathan Lynch index_dir->cache = cache;
79693197a36SNathan Lynch
79793197a36SNathan Lynch rc = kobject_init_and_add(&index_dir->kobj, &cache_index_type,
79893197a36SNathan Lynch cache_dir->kobj, "index%d", index);
7997e803979STobin C. Harding if (rc) {
8007e803979STobin C. Harding kobject_put(&index_dir->kobj);
8017e803979STobin C. Harding return;
8027e803979STobin C. Harding }
80393197a36SNathan Lynch
80493197a36SNathan Lynch index_dir->next = cache_dir->index;
80593197a36SNathan Lynch cache_dir->index = index_dir;
80693197a36SNathan Lynch
80793197a36SNathan Lynch cacheinfo_create_index_opt_attrs(index_dir);
80893197a36SNathan Lynch }
80993197a36SNathan Lynch
cacheinfo_sysfs_populate(unsigned int cpu_id,struct cache * cache_list)810061d19f2SPaul Gortmaker static void cacheinfo_sysfs_populate(unsigned int cpu_id,
811061d19f2SPaul Gortmaker struct cache *cache_list)
81293197a36SNathan Lynch {
81393197a36SNathan Lynch struct cache_dir *cache_dir;
81493197a36SNathan Lynch struct cache *cache;
81593197a36SNathan Lynch int index = 0;
81693197a36SNathan Lynch
81793197a36SNathan Lynch cache_dir = cacheinfo_create_cache_dir(cpu_id);
81893197a36SNathan Lynch if (!cache_dir)
81993197a36SNathan Lynch return;
82093197a36SNathan Lynch
82193197a36SNathan Lynch cache = cache_list;
82293197a36SNathan Lynch while (cache) {
82393197a36SNathan Lynch cacheinfo_create_index_dir(cache, index, cache_dir);
82493197a36SNathan Lynch index++;
82593197a36SNathan Lynch cache = cache->next_local;
82693197a36SNathan Lynch }
82793197a36SNathan Lynch }
82893197a36SNathan Lynch
cacheinfo_cpu_online(unsigned int cpu_id)829061d19f2SPaul Gortmaker void cacheinfo_cpu_online(unsigned int cpu_id)
83093197a36SNathan Lynch {
83193197a36SNathan Lynch struct cache *cache;
83293197a36SNathan Lynch
83393197a36SNathan Lynch cache = cache_chain_instantiate(cpu_id);
83493197a36SNathan Lynch if (!cache)
83593197a36SNathan Lynch return;
83693197a36SNathan Lynch
83793197a36SNathan Lynch cacheinfo_sysfs_populate(cpu_id, cache);
83893197a36SNathan Lynch }
83993197a36SNathan Lynch
8406b36ba84SHaren Myneni /* functions needed to remove cache entry for cpu offline or suspend/resume */
8416b36ba84SHaren Myneni
8426b36ba84SHaren Myneni #if (defined(CONFIG_PPC_PSERIES) && defined(CONFIG_SUSPEND)) || \
8436b36ba84SHaren Myneni defined(CONFIG_HOTPLUG_CPU)
84493197a36SNathan Lynch
cache_lookup_by_cpu(unsigned int cpu_id)84593197a36SNathan Lynch static struct cache *cache_lookup_by_cpu(unsigned int cpu_id)
84693197a36SNathan Lynch {
84793197a36SNathan Lynch struct device_node *cpu_node;
84893197a36SNathan Lynch struct cache *cache;
849a4bec516SGautham R. Shenoy int group_id;
85093197a36SNathan Lynch
85193197a36SNathan Lynch cpu_node = of_get_cpu_node(cpu_id, NULL);
85293197a36SNathan Lynch WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
85393197a36SNathan Lynch if (!cpu_node)
85493197a36SNathan Lynch return NULL;
85593197a36SNathan Lynch
856a4bec516SGautham R. Shenoy group_id = get_group_id(cpu_id, 1);
857a4bec516SGautham R. Shenoy cache = cache_lookup_by_node_group(cpu_node, group_id);
85893197a36SNathan Lynch of_node_put(cpu_node);
85993197a36SNathan Lynch
86093197a36SNathan Lynch return cache;
86193197a36SNathan Lynch }
86293197a36SNathan Lynch
remove_index_dirs(struct cache_dir * cache_dir)86393197a36SNathan Lynch static void remove_index_dirs(struct cache_dir *cache_dir)
86493197a36SNathan Lynch {
86593197a36SNathan Lynch struct cache_index_dir *index;
86693197a36SNathan Lynch
86793197a36SNathan Lynch index = cache_dir->index;
86893197a36SNathan Lynch
86993197a36SNathan Lynch while (index) {
87093197a36SNathan Lynch struct cache_index_dir *next;
87193197a36SNathan Lynch
87293197a36SNathan Lynch next = index->next;
87393197a36SNathan Lynch kobject_put(&index->kobj);
87493197a36SNathan Lynch index = next;
87593197a36SNathan Lynch }
87693197a36SNathan Lynch }
87793197a36SNathan Lynch
remove_cache_dir(struct cache_dir * cache_dir)87893197a36SNathan Lynch static void remove_cache_dir(struct cache_dir *cache_dir)
87993197a36SNathan Lynch {
88093197a36SNathan Lynch remove_index_dirs(cache_dir);
88193197a36SNathan Lynch
88291b973f9SPaul Mackerras /* Remove cache dir from sysfs */
88391b973f9SPaul Mackerras kobject_del(cache_dir->kobj);
88491b973f9SPaul Mackerras
88593197a36SNathan Lynch kobject_put(cache_dir->kobj);
88693197a36SNathan Lynch
88793197a36SNathan Lynch kfree(cache_dir);
88893197a36SNathan Lynch }
88993197a36SNathan Lynch
cache_cpu_clear(struct cache * cache,int cpu)89093197a36SNathan Lynch static void cache_cpu_clear(struct cache *cache, int cpu)
89193197a36SNathan Lynch {
89293197a36SNathan Lynch while (cache) {
89393197a36SNathan Lynch struct cache *next = cache->next_local;
89493197a36SNathan Lynch
89593197a36SNathan Lynch WARN_ONCE(!cpumask_test_cpu(cpu, &cache->shared_cpu_map),
896be6f885eSNathan Lynch "CPU %i not accounted in %pOFP(%s)\n",
897b7c670d6SRob Herring cpu, cache->ofnode,
89893197a36SNathan Lynch cache_type_string(cache));
89993197a36SNathan Lynch
90093197a36SNathan Lynch cpumask_clear_cpu(cpu, &cache->shared_cpu_map);
90193197a36SNathan Lynch
90293197a36SNathan Lynch /* Release the cache object if all the cpus using it
90393197a36SNathan Lynch * are offline */
90493197a36SNathan Lynch if (cpumask_empty(&cache->shared_cpu_map))
90593197a36SNathan Lynch release_cache(cache);
90693197a36SNathan Lynch
90793197a36SNathan Lynch cache = next;
90893197a36SNathan Lynch }
90993197a36SNathan Lynch }
91093197a36SNathan Lynch
cacheinfo_cpu_offline(unsigned int cpu_id)91193197a36SNathan Lynch void cacheinfo_cpu_offline(unsigned int cpu_id)
91293197a36SNathan Lynch {
91393197a36SNathan Lynch struct cache_dir *cache_dir;
91493197a36SNathan Lynch struct cache *cache;
91593197a36SNathan Lynch
91693197a36SNathan Lynch /* Prevent userspace from seeing inconsistent state - remove
91793197a36SNathan Lynch * the sysfs hierarchy first */
918fc7a9febSNathan Lynch cache_dir = per_cpu(cache_dir_pcpu, cpu_id);
91993197a36SNathan Lynch
92093197a36SNathan Lynch /* careful, sysfs population may have failed */
92193197a36SNathan Lynch if (cache_dir)
92293197a36SNathan Lynch remove_cache_dir(cache_dir);
92393197a36SNathan Lynch
924fc7a9febSNathan Lynch per_cpu(cache_dir_pcpu, cpu_id) = NULL;
92593197a36SNathan Lynch
92693197a36SNathan Lynch /* clear the CPU's bit in its cache chain, possibly freeing
92793197a36SNathan Lynch * cache objects */
92893197a36SNathan Lynch cache = cache_lookup_by_cpu(cpu_id);
92993197a36SNathan Lynch if (cache)
93093197a36SNathan Lynch cache_cpu_clear(cache, cpu_id);
93193197a36SNathan Lynch }
932d4aa219aSNathan Lynch
cacheinfo_teardown(void)933d4aa219aSNathan Lynch void cacheinfo_teardown(void)
934d4aa219aSNathan Lynch {
935d4aa219aSNathan Lynch unsigned int cpu;
936d4aa219aSNathan Lynch
937d4aa219aSNathan Lynch lockdep_assert_cpus_held();
938d4aa219aSNathan Lynch
939d4aa219aSNathan Lynch for_each_online_cpu(cpu)
940d4aa219aSNathan Lynch cacheinfo_cpu_offline(cpu);
941d4aa219aSNathan Lynch }
942d4aa219aSNathan Lynch
cacheinfo_rebuild(void)943d4aa219aSNathan Lynch void cacheinfo_rebuild(void)
944d4aa219aSNathan Lynch {
945d4aa219aSNathan Lynch unsigned int cpu;
946d4aa219aSNathan Lynch
947d4aa219aSNathan Lynch lockdep_assert_cpus_held();
948d4aa219aSNathan Lynch
949d4aa219aSNathan Lynch for_each_online_cpu(cpu)
950d4aa219aSNathan Lynch cacheinfo_cpu_online(cpu);
951d4aa219aSNathan Lynch }
952d4aa219aSNathan Lynch
9536b36ba84SHaren Myneni #endif /* (CONFIG_PPC_PSERIES && CONFIG_SUSPEND) || CONFIG_HOTPLUG_CPU */
954