blob: a116d5bb6b12152274cd58a55bc612551896b497 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Nathan Lynch93197a32008-12-23 18:55:54 +00002/*
3 * Processor cache information made available to userspace via sysfs;
4 * intended to be compatible with x86 intel_cacheinfo implementation.
5 *
6 * Copyright 2008 IBM Corporation
7 * Author: Nathan Lynch
Nathan Lynch93197a32008-12-23 18:55:54 +00008 */
9
Nathan Lynche2b3c162019-06-27 00:15:34 -050010#define pr_fmt(fmt) "cacheinfo: " fmt
11
Nathan Lynch93197a32008-12-23 18:55:54 +000012#include <linux/cpu.h>
13#include <linux/cpumask.h>
Nathan Lynch93197a32008-12-23 18:55:54 +000014#include <linux/kernel.h>
15#include <linux/kobject.h>
16#include <linux/list.h>
17#include <linux/notifier.h>
18#include <linux/of.h>
19#include <linux/percpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Nathan Lynch93197a32008-12-23 18:55:54 +000021#include <asm/prom.h>
Gautham R. Shenoy500fe5f2018-10-11 11:03:03 +053022#include <asm/cputhreads.h>
23#include <asm/smp.h>
Nathan Lynch93197a32008-12-23 18:55:54 +000024
25#include "cacheinfo.h"
26
27/* per-cpu object for tracking:
28 * - a "cache" kobject for the top-level directory
29 * - a list of "index" objects representing the cpu's local cache hierarchy
30 */
31struct cache_dir {
32 struct kobject *kobj; /* bare (not embedded) kobject for cache
33 * directory */
34 struct cache_index_dir *index; /* list of index objects */
35};
36
37/* "index" object: each cpu's cache directory has an index
38 * subdirectory corresponding to a cache object associated with the
39 * cpu. This object's lifetime is managed via the embedded kobject.
40 */
41struct cache_index_dir {
42 struct kobject kobj;
43 struct cache_index_dir *next; /* next index in parent directory */
44 struct cache *cache;
45};
46
47/* Template for determining which OF properties to query for a given
48 * cache type */
49struct cache_type_info {
50 const char *name;
51 const char *size_prop;
52
53 /* Allow for both [di]-cache-line-size and
54 * [di]-cache-block-size properties. According to the PowerPC
55 * Processor binding, -line-size should be provided if it
56 * differs from the cache block size (that which is operated
57 * on by cache instructions), so we look for -line-size first.
58 * See cache_get_line_size(). */
59
60 const char *line_size_props[2];
61 const char *nr_sets_prop;
62};
63
64/* These are used to index the cache_type_info array. */
Dave Olsonf7e9e352015-04-02 21:28:45 -070065#define CACHE_TYPE_UNIFIED 0 /* cache-size, cache-block-size, etc. */
66#define CACHE_TYPE_UNIFIED_D 1 /* d-cache-size, d-cache-block-size, etc */
67#define CACHE_TYPE_INSTRUCTION 2
68#define CACHE_TYPE_DATA 3
Nathan Lynch93197a32008-12-23 18:55:54 +000069
70static const struct cache_type_info cache_type_info[] = {
71 {
Dave Olsonf7e9e352015-04-02 21:28:45 -070072 /* Embedded systems that use cache-size, cache-block-size,
73 * etc. for the Unified (typically L2) cache. */
74 .name = "Unified",
75 .size_prop = "cache-size",
76 .line_size_props = { "cache-line-size",
77 "cache-block-size", },
78 .nr_sets_prop = "cache-sets",
79 },
80 {
Nathan Lynch93197a32008-12-23 18:55:54 +000081 /* PowerPC Processor binding says the [di]-cache-*
82 * must be equal on unified caches, so just use
83 * d-cache properties. */
84 .name = "Unified",
85 .size_prop = "d-cache-size",
86 .line_size_props = { "d-cache-line-size",
87 "d-cache-block-size", },
88 .nr_sets_prop = "d-cache-sets",
89 },
90 {
91 .name = "Instruction",
92 .size_prop = "i-cache-size",
93 .line_size_props = { "i-cache-line-size",
94 "i-cache-block-size", },
95 .nr_sets_prop = "i-cache-sets",
96 },
97 {
98 .name = "Data",
99 .size_prop = "d-cache-size",
100 .line_size_props = { "d-cache-line-size",
101 "d-cache-block-size", },
102 .nr_sets_prop = "d-cache-sets",
103 },
104};
105
106/* Cache object: each instance of this corresponds to a distinct cache
107 * in the system. There are separate objects for Harvard caches: one
108 * each for instruction and data, and each refers to the same OF node.
109 * The refcount of the OF node is elevated for the lifetime of the
110 * cache object. A cache object is released when its shared_cpu_map
111 * is cleared (see cache_cpu_clear).
112 *
113 * A cache object is on two lists: an unsorted global list
114 * (cache_list) of cache objects; and a singly-linked list
115 * representing the local cache hierarchy, which is ordered by level
116 * (e.g. L1d -> L1i -> L2 -> L3).
117 */
118struct cache {
119 struct device_node *ofnode; /* OF node for this cache, may be cpu */
120 struct cpumask shared_cpu_map; /* online CPUs using this cache */
121 int type; /* split cache disambiguation */
122 int level; /* level not explicit in device tree */
123 struct list_head list; /* global list of cache objects */
124 struct cache *next_local; /* next cache of >= level */
125};
126
Nathan Lynchfc7a9fe2009-01-09 13:12:44 +0000127static DEFINE_PER_CPU(struct cache_dir *, cache_dir_pcpu);
Nathan Lynch93197a32008-12-23 18:55:54 +0000128
129/* traversal/modification of this list occurs only at cpu hotplug time;
130 * access is serialized by cpu hotplug locking
131 */
132static LIST_HEAD(cache_list);
133
134static struct cache_index_dir *kobj_to_cache_index_dir(struct kobject *k)
135{
136 return container_of(k, struct cache_index_dir, kobj);
137}
138
139static const char *cache_type_string(const struct cache *cache)
140{
141 return cache_type_info[cache->type].name;
142}
143
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400144static void cache_init(struct cache *cache, int type, int level,
145 struct device_node *ofnode)
Nathan Lynch93197a32008-12-23 18:55:54 +0000146{
147 cache->type = type;
148 cache->level = level;
149 cache->ofnode = of_node_get(ofnode);
150 INIT_LIST_HEAD(&cache->list);
151 list_add(&cache->list, &cache_list);
152}
153
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400154static struct cache *new_cache(int type, int level, struct device_node *ofnode)
Nathan Lynch93197a32008-12-23 18:55:54 +0000155{
156 struct cache *cache;
157
158 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
159 if (cache)
160 cache_init(cache, type, level, ofnode);
161
162 return cache;
163}
164
165static void release_cache_debugcheck(struct cache *cache)
166{
167 struct cache *iter;
168
169 list_for_each_entry(iter, &cache_list, list)
170 WARN_ONCE(iter->next_local == cache,
Nathan Lynchbe6f885e2019-06-27 00:15:35 -0500171 "cache for %pOFP(%s) refers to cache for %pOFP(%s)\n",
Rob Herringb7c670d2017-08-21 10:16:47 -0500172 iter->ofnode,
Nathan Lynch93197a32008-12-23 18:55:54 +0000173 cache_type_string(iter),
Rob Herringb7c670d2017-08-21 10:16:47 -0500174 cache->ofnode,
Nathan Lynch93197a32008-12-23 18:55:54 +0000175 cache_type_string(cache));
176}
177
178static void release_cache(struct cache *cache)
179{
180 if (!cache)
181 return;
182
Nathan Lynchbe6f885e2019-06-27 00:15:35 -0500183 pr_debug("freeing L%d %s cache for %pOFP\n", cache->level,
Rob Herringb7c670d2017-08-21 10:16:47 -0500184 cache_type_string(cache), cache->ofnode);
Nathan Lynch93197a32008-12-23 18:55:54 +0000185
186 release_cache_debugcheck(cache);
187 list_del(&cache->list);
188 of_node_put(cache->ofnode);
189 kfree(cache);
190}
191
192static void cache_cpu_set(struct cache *cache, int cpu)
193{
194 struct cache *next = cache;
195
196 while (next) {
197 WARN_ONCE(cpumask_test_cpu(cpu, &next->shared_cpu_map),
Nathan Lynchbe6f885e2019-06-27 00:15:35 -0500198 "CPU %i already accounted in %pOFP(%s)\n",
Rob Herringb7c670d2017-08-21 10:16:47 -0500199 cpu, next->ofnode,
Nathan Lynch93197a32008-12-23 18:55:54 +0000200 cache_type_string(next));
201 cpumask_set_cpu(cpu, &next->shared_cpu_map);
202 next = next->next_local;
203 }
204}
205
206static int cache_size(const struct cache *cache, unsigned int *ret)
207{
208 const char *propname;
Anton Blanchardd10bd842013-08-07 02:01:37 +1000209 const __be32 *cache_size;
Nathan Lynch93197a32008-12-23 18:55:54 +0000210
211 propname = cache_type_info[cache->type].size_prop;
212
213 cache_size = of_get_property(cache->ofnode, propname, NULL);
214 if (!cache_size)
215 return -ENODEV;
216
Anton Blanchardd10bd842013-08-07 02:01:37 +1000217 *ret = of_read_number(cache_size, 1);
Nathan Lynch93197a32008-12-23 18:55:54 +0000218 return 0;
219}
220
221static int cache_size_kb(const struct cache *cache, unsigned int *ret)
222{
223 unsigned int size;
224
225 if (cache_size(cache, &size))
226 return -ENODEV;
227
228 *ret = size / 1024;
229 return 0;
230}
231
232/* not cache_line_size() because that's a macro in include/linux/cache.h */
233static int cache_get_line_size(const struct cache *cache, unsigned int *ret)
234{
Anton Blanchardd10bd842013-08-07 02:01:37 +1000235 const __be32 *line_size;
Nathan Lynch93197a32008-12-23 18:55:54 +0000236 int i, lim;
237
238 lim = ARRAY_SIZE(cache_type_info[cache->type].line_size_props);
239
240 for (i = 0; i < lim; i++) {
241 const char *propname;
242
243 propname = cache_type_info[cache->type].line_size_props[i];
244 line_size = of_get_property(cache->ofnode, propname, NULL);
245 if (line_size)
246 break;
247 }
248
249 if (!line_size)
250 return -ENODEV;
251
Anton Blanchardd10bd842013-08-07 02:01:37 +1000252 *ret = of_read_number(line_size, 1);
Nathan Lynch93197a32008-12-23 18:55:54 +0000253 return 0;
254}
255
256static int cache_nr_sets(const struct cache *cache, unsigned int *ret)
257{
258 const char *propname;
Anton Blanchardd10bd842013-08-07 02:01:37 +1000259 const __be32 *nr_sets;
Nathan Lynch93197a32008-12-23 18:55:54 +0000260
261 propname = cache_type_info[cache->type].nr_sets_prop;
262
263 nr_sets = of_get_property(cache->ofnode, propname, NULL);
264 if (!nr_sets)
265 return -ENODEV;
266
Anton Blanchardd10bd842013-08-07 02:01:37 +1000267 *ret = of_read_number(nr_sets, 1);
Nathan Lynch93197a32008-12-23 18:55:54 +0000268 return 0;
269}
270
271static int cache_associativity(const struct cache *cache, unsigned int *ret)
272{
273 unsigned int line_size;
274 unsigned int nr_sets;
275 unsigned int size;
276
277 if (cache_nr_sets(cache, &nr_sets))
278 goto err;
279
280 /* If the cache is fully associative, there is no need to
281 * check the other properties.
282 */
283 if (nr_sets == 1) {
284 *ret = 0;
285 return 0;
286 }
287
288 if (cache_get_line_size(cache, &line_size))
289 goto err;
290 if (cache_size(cache, &size))
291 goto err;
292
293 if (!(nr_sets > 0 && size > 0 && line_size > 0))
294 goto err;
295
296 *ret = (size / nr_sets) / line_size;
297 return 0;
298err:
299 return -ENODEV;
300}
301
302/* helper for dealing with split caches */
303static struct cache *cache_find_first_sibling(struct cache *cache)
304{
305 struct cache *iter;
306
Dave Olsonf7e9e352015-04-02 21:28:45 -0700307 if (cache->type == CACHE_TYPE_UNIFIED ||
308 cache->type == CACHE_TYPE_UNIFIED_D)
Nathan Lynch93197a32008-12-23 18:55:54 +0000309 return cache;
310
311 list_for_each_entry(iter, &cache_list, list)
312 if (iter->ofnode == cache->ofnode && iter->next_local == cache)
313 return iter;
314
315 return cache;
316}
317
318/* return the first cache on a local list matching node */
319static struct cache *cache_lookup_by_node(const struct device_node *node)
320{
321 struct cache *cache = NULL;
322 struct cache *iter;
323
324 list_for_each_entry(iter, &cache_list, list) {
325 if (iter->ofnode != node)
326 continue;
327 cache = cache_find_first_sibling(iter);
328 break;
329 }
330
331 return cache;
332}
333
334static bool cache_node_is_unified(const struct device_node *np)
335{
336 return of_get_property(np, "cache-unified", NULL);
337}
338
Dave Olsonf7e9e352015-04-02 21:28:45 -0700339/*
340 * Unified caches can have two different sets of tags. Most embedded
341 * use cache-size, etc. for the unified cache size, but open firmware systems
342 * use d-cache-size, etc. Check on initialization for which type we have, and
343 * return the appropriate structure type. Assume it's embedded if it isn't
344 * open firmware. If it's yet a 3rd type, then there will be missing entries
345 * in /sys/devices/system/cpu/cpu0/cache/index2/, and this code will need
346 * to be extended further.
347 */
348static int cache_is_unified_d(const struct device_node *np)
Nathan Lynch93197a32008-12-23 18:55:54 +0000349{
Dave Olsonf7e9e352015-04-02 21:28:45 -0700350 return of_get_property(np,
351 cache_type_info[CACHE_TYPE_UNIFIED_D].size_prop, NULL) ?
352 CACHE_TYPE_UNIFIED_D : CACHE_TYPE_UNIFIED;
353}
Nathan Lynch93197a32008-12-23 18:55:54 +0000354
Dave Olsonf7e9e352015-04-02 21:28:45 -0700355static struct cache *cache_do_one_devnode_unified(struct device_node *node, int level)
356{
Nathan Lynchbe6f885e2019-06-27 00:15:35 -0500357 pr_debug("creating L%d ucache for %pOFP\n", level, node);
Nathan Lynch93197a32008-12-23 18:55:54 +0000358
Dave Olsonf7e9e352015-04-02 21:28:45 -0700359 return new_cache(cache_is_unified_d(node), level, node);
Nathan Lynch93197a32008-12-23 18:55:54 +0000360}
361
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400362static struct cache *cache_do_one_devnode_split(struct device_node *node,
363 int level)
Nathan Lynch93197a32008-12-23 18:55:54 +0000364{
365 struct cache *dcache, *icache;
366
Nathan Lynchbe6f885e2019-06-27 00:15:35 -0500367 pr_debug("creating L%d dcache and icache for %pOFP\n", level,
Rob Herringb7c670d2017-08-21 10:16:47 -0500368 node);
Nathan Lynch93197a32008-12-23 18:55:54 +0000369
370 dcache = new_cache(CACHE_TYPE_DATA, level, node);
371 icache = new_cache(CACHE_TYPE_INSTRUCTION, level, node);
372
373 if (!dcache || !icache)
374 goto err;
375
376 dcache->next_local = icache;
377
378 return dcache;
379err:
380 release_cache(dcache);
381 release_cache(icache);
382 return NULL;
383}
384
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400385static struct cache *cache_do_one_devnode(struct device_node *node, int level)
Nathan Lynch93197a32008-12-23 18:55:54 +0000386{
387 struct cache *cache;
388
389 if (cache_node_is_unified(node))
390 cache = cache_do_one_devnode_unified(node, level);
391 else
392 cache = cache_do_one_devnode_split(node, level);
393
394 return cache;
395}
396
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400397static struct cache *cache_lookup_or_instantiate(struct device_node *node,
398 int level)
Nathan Lynch93197a32008-12-23 18:55:54 +0000399{
400 struct cache *cache;
401
402 cache = cache_lookup_by_node(node);
403
404 WARN_ONCE(cache && cache->level != level,
405 "cache level mismatch on lookup (got %d, expected %d)\n",
406 cache->level, level);
407
408 if (!cache)
409 cache = cache_do_one_devnode(node, level);
410
411 return cache;
412}
413
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400414static void link_cache_lists(struct cache *smaller, struct cache *bigger)
Nathan Lynch93197a32008-12-23 18:55:54 +0000415{
416 while (smaller->next_local) {
417 if (smaller->next_local == bigger)
418 return; /* already linked */
419 smaller = smaller->next_local;
420 }
421
422 smaller->next_local = bigger;
423}
424
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400425static void do_subsidiary_caches_debugcheck(struct cache *cache)
Nathan Lynch93197a32008-12-23 18:55:54 +0000426{
Nathan Lynch1b3da8f2019-06-27 00:15:36 -0500427 WARN_ONCE(cache->level != 1,
428 "instantiating cache chain from L%d %s cache for "
429 "%pOFP instead of an L1\n", cache->level,
430 cache_type_string(cache), cache->ofnode);
431 WARN_ONCE(!of_node_is_type(cache->ofnode, "cpu"),
432 "instantiating cache chain from node %pOFP of type '%s' "
433 "instead of a cpu node\n", cache->ofnode,
434 of_node_get_device_type(cache->ofnode));
Nathan Lynch93197a32008-12-23 18:55:54 +0000435}
436
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400437static void do_subsidiary_caches(struct cache *cache)
Nathan Lynch93197a32008-12-23 18:55:54 +0000438{
439 struct device_node *subcache_node;
440 int level = cache->level;
441
442 do_subsidiary_caches_debugcheck(cache);
443
444 while ((subcache_node = of_find_next_cache_node(cache->ofnode))) {
445 struct cache *subcache;
446
447 level++;
448 subcache = cache_lookup_or_instantiate(subcache_node, level);
449 of_node_put(subcache_node);
450 if (!subcache)
451 break;
452
453 link_cache_lists(cache, subcache);
454 cache = subcache;
455 }
456}
457
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400458static struct cache *cache_chain_instantiate(unsigned int cpu_id)
Nathan Lynch93197a32008-12-23 18:55:54 +0000459{
460 struct device_node *cpu_node;
461 struct cache *cpu_cache = NULL;
462
463 pr_debug("creating cache object(s) for CPU %i\n", cpu_id);
464
465 cpu_node = of_get_cpu_node(cpu_id, NULL);
466 WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
467 if (!cpu_node)
468 goto out;
469
470 cpu_cache = cache_lookup_or_instantiate(cpu_node, 1);
471 if (!cpu_cache)
472 goto out;
473
474 do_subsidiary_caches(cpu_cache);
475
476 cache_cpu_set(cpu_cache, cpu_id);
477out:
478 of_node_put(cpu_node);
479
480 return cpu_cache;
481}
482
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400483static struct cache_dir *cacheinfo_create_cache_dir(unsigned int cpu_id)
Nathan Lynch93197a32008-12-23 18:55:54 +0000484{
485 struct cache_dir *cache_dir;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800486 struct device *dev;
Nathan Lynch93197a32008-12-23 18:55:54 +0000487 struct kobject *kobj = NULL;
488
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800489 dev = get_cpu_device(cpu_id);
490 WARN_ONCE(!dev, "no dev for CPU %i\n", cpu_id);
491 if (!dev)
Nathan Lynch93197a32008-12-23 18:55:54 +0000492 goto err;
493
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800494 kobj = kobject_create_and_add("cache", &dev->kobj);
Nathan Lynch93197a32008-12-23 18:55:54 +0000495 if (!kobj)
496 goto err;
497
498 cache_dir = kzalloc(sizeof(*cache_dir), GFP_KERNEL);
499 if (!cache_dir)
500 goto err;
501
502 cache_dir->kobj = kobj;
503
Nathan Lynchfc7a9fe2009-01-09 13:12:44 +0000504 WARN_ON_ONCE(per_cpu(cache_dir_pcpu, cpu_id) != NULL);
Nathan Lynch93197a32008-12-23 18:55:54 +0000505
Nathan Lynchfc7a9fe2009-01-09 13:12:44 +0000506 per_cpu(cache_dir_pcpu, cpu_id) = cache_dir;
Nathan Lynch93197a32008-12-23 18:55:54 +0000507
508 return cache_dir;
509err:
510 kobject_put(kobj);
511 return NULL;
512}
513
514static void cache_index_release(struct kobject *kobj)
515{
516 struct cache_index_dir *index;
517
518 index = kobj_to_cache_index_dir(kobj);
519
520 pr_debug("freeing index directory for L%d %s cache\n",
521 index->cache->level, cache_type_string(index->cache));
522
523 kfree(index);
524}
525
526static ssize_t cache_index_show(struct kobject *k, struct attribute *attr, char *buf)
527{
528 struct kobj_attribute *kobj_attr;
529
530 kobj_attr = container_of(attr, struct kobj_attribute, attr);
531
532 return kobj_attr->show(k, kobj_attr, buf);
533}
534
535static struct cache *index_kobj_to_cache(struct kobject *k)
536{
537 struct cache_index_dir *index;
538
539 index = kobj_to_cache_index_dir(k);
540
541 return index->cache;
542}
543
544static ssize_t size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
545{
546 unsigned int size_kb;
547 struct cache *cache;
548
549 cache = index_kobj_to_cache(k);
550
551 if (cache_size_kb(cache, &size_kb))
552 return -ENODEV;
553
554 return sprintf(buf, "%uK\n", size_kb);
555}
556
557static struct kobj_attribute cache_size_attr =
558 __ATTR(size, 0444, size_show, NULL);
559
560
561static ssize_t line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
562{
563 unsigned int line_size;
564 struct cache *cache;
565
566 cache = index_kobj_to_cache(k);
567
568 if (cache_get_line_size(cache, &line_size))
569 return -ENODEV;
570
571 return sprintf(buf, "%u\n", line_size);
572}
573
574static struct kobj_attribute cache_line_size_attr =
575 __ATTR(coherency_line_size, 0444, line_size_show, NULL);
576
577static ssize_t nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
578{
579 unsigned int nr_sets;
580 struct cache *cache;
581
582 cache = index_kobj_to_cache(k);
583
584 if (cache_nr_sets(cache, &nr_sets))
585 return -ENODEV;
586
587 return sprintf(buf, "%u\n", nr_sets);
588}
589
590static struct kobj_attribute cache_nr_sets_attr =
591 __ATTR(number_of_sets, 0444, nr_sets_show, NULL);
592
593static ssize_t associativity_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
594{
595 unsigned int associativity;
596 struct cache *cache;
597
598 cache = index_kobj_to_cache(k);
599
600 if (cache_associativity(cache, &associativity))
601 return -ENODEV;
602
603 return sprintf(buf, "%u\n", associativity);
604}
605
606static struct kobj_attribute cache_assoc_attr =
607 __ATTR(ways_of_associativity, 0444, associativity_show, NULL);
608
609static ssize_t type_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
610{
611 struct cache *cache;
612
613 cache = index_kobj_to_cache(k);
614
615 return sprintf(buf, "%s\n", cache_type_string(cache));
616}
617
618static struct kobj_attribute cache_type_attr =
619 __ATTR(type, 0444, type_show, NULL);
620
621static ssize_t level_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
622{
623 struct cache_index_dir *index;
624 struct cache *cache;
625
626 index = kobj_to_cache_index_dir(k);
627 cache = index->cache;
628
629 return sprintf(buf, "%d\n", cache->level);
630}
631
632static struct kobj_attribute cache_level_attr =
633 __ATTR(level, 0444, level_show, NULL);
634
Gautham R. Shenoy500fe5f2018-10-11 11:03:03 +0530635static unsigned int index_dir_to_cpu(struct cache_index_dir *index)
636{
637 struct kobject *index_dir_kobj = &index->kobj;
638 struct kobject *cache_dir_kobj = index_dir_kobj->parent;
639 struct kobject *cpu_dev_kobj = cache_dir_kobj->parent;
640 struct device *dev = kobj_to_dev(cpu_dev_kobj);
641
642 return dev->id;
643}
644
645/*
646 * On big-core systems, each core has two groups of CPUs each of which
647 * has its own L1-cache. The thread-siblings which share l1-cache with
648 * @cpu can be obtained via cpu_smallcore_mask().
649 */
650static const struct cpumask *get_big_core_shared_cpu_map(int cpu, struct cache *cache)
651{
652 if (cache->level == 1)
653 return cpu_smallcore_mask(cpu);
654
655 return &cache->shared_cpu_map;
656}
657
Srikar Dronamraju74b74922020-06-29 16:07:02 +0530658static ssize_t
659show_shared_cpumap(struct kobject *k, struct kobj_attribute *attr, char *buf, bool list)
Nathan Lynch93197a32008-12-23 18:55:54 +0000660{
661 struct cache_index_dir *index;
662 struct cache *cache;
Gautham R. Shenoy500fe5f2018-10-11 11:03:03 +0530663 const struct cpumask *mask;
Srikar Dronamraju5658cf02020-06-29 16:07:01 +0530664 int cpu;
Nathan Lynch93197a32008-12-23 18:55:54 +0000665
666 index = kobj_to_cache_index_dir(k);
667 cache = index->cache;
Nathan Lynch93197a32008-12-23 18:55:54 +0000668
Gautham R. Shenoy500fe5f2018-10-11 11:03:03 +0530669 if (has_big_cores) {
670 cpu = index_dir_to_cpu(index);
671 mask = get_big_core_shared_cpu_map(cpu, cache);
672 } else {
673 mask = &cache->shared_cpu_map;
674 }
675
Srikar Dronamraju74b74922020-06-29 16:07:02 +0530676 return cpumap_print_to_pagebuf(list, buf, mask);
677}
678
679static ssize_t shared_cpu_map_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
680{
Srikar Dronamrajua87a77c2020-06-29 16:07:03 +0530681 return show_shared_cpumap(k, attr, buf, false);
682}
683
684static ssize_t shared_cpu_list_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
685{
686 return show_shared_cpumap(k, attr, buf, true);
Nathan Lynch93197a32008-12-23 18:55:54 +0000687}
688
689static struct kobj_attribute cache_shared_cpu_map_attr =
690 __ATTR(shared_cpu_map, 0444, shared_cpu_map_show, NULL);
691
Srikar Dronamrajua87a77c2020-06-29 16:07:03 +0530692static struct kobj_attribute cache_shared_cpu_list_attr =
693 __ATTR(shared_cpu_list, 0444, shared_cpu_list_show, NULL);
694
Nathan Lynch93197a32008-12-23 18:55:54 +0000695/* Attributes which should always be created -- the kobject/sysfs core
696 * does this automatically via kobj_type->default_attrs. This is the
697 * minimum data required to uniquely identify a cache.
698 */
699static struct attribute *cache_index_default_attrs[] = {
700 &cache_type_attr.attr,
701 &cache_level_attr.attr,
702 &cache_shared_cpu_map_attr.attr,
Srikar Dronamrajua87a77c2020-06-29 16:07:03 +0530703 &cache_shared_cpu_list_attr.attr,
Nathan Lynch93197a32008-12-23 18:55:54 +0000704 NULL,
705};
706
707/* Attributes which should be created if the cache device node has the
708 * right properties -- see cacheinfo_create_index_opt_attrs
709 */
710static struct kobj_attribute *cache_index_opt_attrs[] = {
711 &cache_size_attr,
712 &cache_line_size_attr,
713 &cache_nr_sets_attr,
714 &cache_assoc_attr,
715};
716
Emese Revfy52cf25d2010-01-19 02:58:23 +0100717static const struct sysfs_ops cache_index_ops = {
Nathan Lynch93197a32008-12-23 18:55:54 +0000718 .show = cache_index_show,
719};
720
721static struct kobj_type cache_index_type = {
722 .release = cache_index_release,
723 .sysfs_ops = &cache_index_ops,
724 .default_attrs = cache_index_default_attrs,
725};
726
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400727static void cacheinfo_create_index_opt_attrs(struct cache_index_dir *dir)
Nathan Lynch93197a32008-12-23 18:55:54 +0000728{
Nathan Lynch93197a32008-12-23 18:55:54 +0000729 const char *cache_type;
730 struct cache *cache;
731 char *buf;
732 int i;
733
734 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
735 if (!buf)
736 return;
737
738 cache = dir->cache;
Nathan Lynch93197a32008-12-23 18:55:54 +0000739 cache_type = cache_type_string(cache);
740
741 /* We don't want to create an attribute that can't provide a
742 * meaningful value. Check the return value of each optional
743 * attribute's ->show method before registering the
744 * attribute.
745 */
746 for (i = 0; i < ARRAY_SIZE(cache_index_opt_attrs); i++) {
747 struct kobj_attribute *attr;
748 ssize_t rc;
749
750 attr = cache_index_opt_attrs[i];
751
752 rc = attr->show(&dir->kobj, attr, buf);
753 if (rc <= 0) {
754 pr_debug("not creating %s attribute for "
Nathan Lynchbe6f885e2019-06-27 00:15:35 -0500755 "%pOFP(%s) (rc = %zd)\n",
Rob Herringb7c670d2017-08-21 10:16:47 -0500756 attr->attr.name, cache->ofnode,
Nathan Lynch93197a32008-12-23 18:55:54 +0000757 cache_type, rc);
758 continue;
759 }
760 if (sysfs_create_file(&dir->kobj, &attr->attr))
Nathan Lynchbe6f885e2019-06-27 00:15:35 -0500761 pr_debug("could not create %s attribute for %pOFP(%s)\n",
Rob Herringb7c670d2017-08-21 10:16:47 -0500762 attr->attr.name, cache->ofnode, cache_type);
Nathan Lynch93197a32008-12-23 18:55:54 +0000763 }
764
765 kfree(buf);
766}
767
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400768static void cacheinfo_create_index_dir(struct cache *cache, int index,
769 struct cache_dir *cache_dir)
Nathan Lynch93197a32008-12-23 18:55:54 +0000770{
771 struct cache_index_dir *index_dir;
772 int rc;
773
774 index_dir = kzalloc(sizeof(*index_dir), GFP_KERNEL);
775 if (!index_dir)
Tobin C. Harding7e803972019-04-30 11:09:23 +1000776 return;
Nathan Lynch93197a32008-12-23 18:55:54 +0000777
778 index_dir->cache = cache;
779
780 rc = kobject_init_and_add(&index_dir->kobj, &cache_index_type,
781 cache_dir->kobj, "index%d", index);
Tobin C. Harding7e803972019-04-30 11:09:23 +1000782 if (rc) {
783 kobject_put(&index_dir->kobj);
Tobin C. Harding7e803972019-04-30 11:09:23 +1000784 return;
785 }
Nathan Lynch93197a32008-12-23 18:55:54 +0000786
787 index_dir->next = cache_dir->index;
788 cache_dir->index = index_dir;
789
790 cacheinfo_create_index_opt_attrs(index_dir);
Nathan Lynch93197a32008-12-23 18:55:54 +0000791}
792
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400793static void cacheinfo_sysfs_populate(unsigned int cpu_id,
794 struct cache *cache_list)
Nathan Lynch93197a32008-12-23 18:55:54 +0000795{
796 struct cache_dir *cache_dir;
797 struct cache *cache;
798 int index = 0;
799
800 cache_dir = cacheinfo_create_cache_dir(cpu_id);
801 if (!cache_dir)
802 return;
803
804 cache = cache_list;
805 while (cache) {
806 cacheinfo_create_index_dir(cache, index, cache_dir);
807 index++;
808 cache = cache->next_local;
809 }
810}
811
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400812void cacheinfo_cpu_online(unsigned int cpu_id)
Nathan Lynch93197a32008-12-23 18:55:54 +0000813{
814 struct cache *cache;
815
816 cache = cache_chain_instantiate(cpu_id);
817 if (!cache)
818 return;
819
820 cacheinfo_sysfs_populate(cpu_id, cache);
821}
822
Haren Myneni6b36ba82014-02-25 20:02:18 -0800823/* functions needed to remove cache entry for cpu offline or suspend/resume */
824
825#if (defined(CONFIG_PPC_PSERIES) && defined(CONFIG_SUSPEND)) || \
826 defined(CONFIG_HOTPLUG_CPU)
Nathan Lynch93197a32008-12-23 18:55:54 +0000827
828static struct cache *cache_lookup_by_cpu(unsigned int cpu_id)
829{
830 struct device_node *cpu_node;
831 struct cache *cache;
832
833 cpu_node = of_get_cpu_node(cpu_id, NULL);
834 WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
835 if (!cpu_node)
836 return NULL;
837
838 cache = cache_lookup_by_node(cpu_node);
839 of_node_put(cpu_node);
840
841 return cache;
842}
843
844static void remove_index_dirs(struct cache_dir *cache_dir)
845{
846 struct cache_index_dir *index;
847
848 index = cache_dir->index;
849
850 while (index) {
851 struct cache_index_dir *next;
852
853 next = index->next;
854 kobject_put(&index->kobj);
855 index = next;
856 }
857}
858
859static void remove_cache_dir(struct cache_dir *cache_dir)
860{
861 remove_index_dirs(cache_dir);
862
Paul Mackerras91b973f2014-01-18 21:14:47 +1100863 /* Remove cache dir from sysfs */
864 kobject_del(cache_dir->kobj);
865
Nathan Lynch93197a32008-12-23 18:55:54 +0000866 kobject_put(cache_dir->kobj);
867
868 kfree(cache_dir);
869}
870
871static void cache_cpu_clear(struct cache *cache, int cpu)
872{
873 while (cache) {
874 struct cache *next = cache->next_local;
875
876 WARN_ONCE(!cpumask_test_cpu(cpu, &cache->shared_cpu_map),
Nathan Lynchbe6f885e2019-06-27 00:15:35 -0500877 "CPU %i not accounted in %pOFP(%s)\n",
Rob Herringb7c670d2017-08-21 10:16:47 -0500878 cpu, cache->ofnode,
Nathan Lynch93197a32008-12-23 18:55:54 +0000879 cache_type_string(cache));
880
881 cpumask_clear_cpu(cpu, &cache->shared_cpu_map);
882
883 /* Release the cache object if all the cpus using it
884 * are offline */
885 if (cpumask_empty(&cache->shared_cpu_map))
886 release_cache(cache);
887
888 cache = next;
889 }
890}
891
892void cacheinfo_cpu_offline(unsigned int cpu_id)
893{
894 struct cache_dir *cache_dir;
895 struct cache *cache;
896
897 /* Prevent userspace from seeing inconsistent state - remove
898 * the sysfs hierarchy first */
Nathan Lynchfc7a9fe2009-01-09 13:12:44 +0000899 cache_dir = per_cpu(cache_dir_pcpu, cpu_id);
Nathan Lynch93197a32008-12-23 18:55:54 +0000900
901 /* careful, sysfs population may have failed */
902 if (cache_dir)
903 remove_cache_dir(cache_dir);
904
Nathan Lynchfc7a9fe2009-01-09 13:12:44 +0000905 per_cpu(cache_dir_pcpu, cpu_id) = NULL;
Nathan Lynch93197a32008-12-23 18:55:54 +0000906
907 /* clear the CPU's bit in its cache chain, possibly freeing
908 * cache objects */
909 cache = cache_lookup_by_cpu(cpu_id);
910 if (cache)
911 cache_cpu_clear(cache, cpu_id);
912}
Nathan Lynchd4aa2192019-06-11 23:45:04 -0500913
914void cacheinfo_teardown(void)
915{
916 unsigned int cpu;
917
918 lockdep_assert_cpus_held();
919
920 for_each_online_cpu(cpu)
921 cacheinfo_cpu_offline(cpu);
922}
923
924void cacheinfo_rebuild(void)
925{
926 unsigned int cpu;
927
928 lockdep_assert_cpus_held();
929
930 for_each_online_cpu(cpu)
931 cacheinfo_cpu_online(cpu);
932}
933
Haren Myneni6b36ba82014-02-25 20:02:18 -0800934#endif /* (CONFIG_PPC_PSERIES && CONFIG_SUSPEND) || CONFIG_HOTPLUG_CPU */