projects/linux·blob v7.2 · refs

1071 lines · 26.8 KiB

1// SPDX-License-Identifier: GPL-2.0
2/*
3 * cacheinfo support - processor cache information via sysfs
4 *
5 * Based on arch/x86/kernel/cpu/intel_cacheinfo.c
6 * Author: Sudeep Holla <[email protected]>
7 */
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/acpi.h>
11#include <linux/bitfield.h>
12#include <linux/bitops.h>
13#include <linux/cacheinfo.h>
14#include <linux/compiler.h>
15#include <linux/cpu.h>
16#include <linux/device.h>
17#include <linux/init.h>
18#include <linux/of.h>
19#include <linux/sched.h>
20#include <linux/sched/topology.h>
21#include <linux/slab.h>
22#include <linux/smp.h>
23#include <linux/sysfs.h>
24
25/* pointer to per cpu cacheinfo */
26static DEFINE_PER_CPU(struct cpu_cacheinfo, ci_cpu_cacheinfo);
27#define ci_cacheinfo(cpu) (&per_cpu(ci_cpu_cacheinfo, cpu))
28#define cache_leaves(cpu) (ci_cacheinfo(cpu)->num_leaves)
29#define per_cpu_cacheinfo(cpu) (ci_cacheinfo(cpu)->info_list)
30#define per_cpu_cacheinfo_idx(cpu, idx) \
31 (per_cpu_cacheinfo(cpu) + (idx))
32
33/* Set if no cache information is found in DT/ACPI. */
34static bool use_arch_info;
35
36struct cpu_cacheinfo *get_cpu_cacheinfo(unsigned int cpu)
37{
38 return ci_cacheinfo(cpu);
39}
40
41static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf,
42 struct cacheinfo *sib_leaf)
43{
44 /*
45 * For non DT/ACPI systems, assume unique level 1 caches,
46 * system-wide shared caches for all other levels.
47 */
48 if (!(IS_ENABLED(CONFIG_OF) || IS_ENABLED(CONFIG_ACPI)) ||
49 use_arch_info)
50 return (this_leaf->level != 1) && (sib_leaf->level != 1);
51
52 if ((sib_leaf->attributes & CACHE_ID) &&
53 (this_leaf->attributes & CACHE_ID))
54 return sib_leaf->id == this_leaf->id;
55
56 return sib_leaf->fw_token == this_leaf->fw_token;
57}
58
59bool last_level_cache_is_valid(unsigned int cpu)
60{
61 struct cacheinfo *llc;
62
63 if (!cache_leaves(cpu) || !per_cpu_cacheinfo(cpu))
64 return false;
65
66 llc = per_cpu_cacheinfo_idx(cpu, cache_leaves(cpu) - 1);
67
68 return (llc->attributes & CACHE_ID) || !!llc->fw_token;
69
70}
71
72/*
73 * Get the cacheinfo of the LLC associated with @cpu.
74 * Derived from update_per_cpu_data_slice_size_cpu().
75 */
76struct cacheinfo *get_cpu_cacheinfo_llc(unsigned int cpu)
77{
78 struct cacheinfo *llc;
79
80 if (!last_level_cache_is_valid(cpu))
81 return NULL;
82
83 llc = per_cpu_cacheinfo_idx(cpu, cache_leaves(cpu) - 1);
84 if (llc->type != CACHE_TYPE_DATA && llc->type != CACHE_TYPE_UNIFIED)
85 return NULL;
86
87 return llc;
88}
89
90bool last_level_cache_is_shared(unsigned int cpu_x, unsigned int cpu_y)
91{
92 struct cacheinfo *llc_x, *llc_y;
93
94 if (!last_level_cache_is_valid(cpu_x) ||
95 !last_level_cache_is_valid(cpu_y))
96 return false;
97
98 llc_x = per_cpu_cacheinfo_idx(cpu_x, cache_leaves(cpu_x) - 1);
99 llc_y = per_cpu_cacheinfo_idx(cpu_y, cache_leaves(cpu_y) - 1);
100
101 return cache_leaves_are_shared(llc_x, llc_y);
102}
103
104#ifdef CONFIG_OF
105
106static bool of_check_cache_nodes(struct device_node *np);
107
108/* OF properties to query for a given cache type */
109struct cache_type_info {
110 const char *size_prop;
111 const char *line_size_props[2];
112 const char *nr_sets_prop;
113};
114
115static const struct cache_type_info cache_type_info[] = {
116 {
117 .size_prop = "cache-size",
118 .line_size_props = { "cache-line-size",
119 "cache-block-size", },
120 .nr_sets_prop = "cache-sets",
121 }, {
122 .size_prop = "i-cache-size",
123 .line_size_props = { "i-cache-line-size",
124 "i-cache-block-size", },
125 .nr_sets_prop = "i-cache-sets",
126 }, {
127 .size_prop = "d-cache-size",
128 .line_size_props = { "d-cache-line-size",
129 "d-cache-block-size", },
130 .nr_sets_prop = "d-cache-sets",
131 },
132};
133
134static inline int get_cacheinfo_idx(enum cache_type type)
135{
136 if (type == CACHE_TYPE_UNIFIED)
137 return 0;
138 return type;
139}
140
141static void cache_size(struct cacheinfo *this_leaf, struct device_node *np)
142{
143 const char *propname;
144 int ct_idx;
145
146 ct_idx = get_cacheinfo_idx(this_leaf->type);
147 propname = cache_type_info[ct_idx].size_prop;
148
149 of_property_read_u32(np, propname, &this_leaf->size);
150}
151
152/* not cache_line_size() because that's a macro in include/linux/cache.h */
153static void cache_get_line_size(struct cacheinfo *this_leaf,
154 struct device_node *np)
155{
156 int i, lim, ct_idx;
157
158 ct_idx = get_cacheinfo_idx(this_leaf->type);
159 lim = ARRAY_SIZE(cache_type_info[ct_idx].line_size_props);
160
161 for (i = 0; i < lim; i++) {
162 int ret;
163 u32 line_size;
164 const char *propname;
165
166 propname = cache_type_info[ct_idx].line_size_props[i];
167 ret = of_property_read_u32(np, propname, &line_size);
168 if (!ret) {
169 this_leaf->coherency_line_size = line_size;
170 break;
171 }
172 }
173}
174
175static void cache_nr_sets(struct cacheinfo *this_leaf, struct device_node *np)
176{
177 const char *propname;
178 int ct_idx;
179
180 ct_idx = get_cacheinfo_idx(this_leaf->type);
181 propname = cache_type_info[ct_idx].nr_sets_prop;
182
183 of_property_read_u32(np, propname, &this_leaf->number_of_sets);
184}
185
186static void cache_associativity(struct cacheinfo *this_leaf)
187{
188 unsigned int line_size = this_leaf->coherency_line_size;
189 unsigned int nr_sets = this_leaf->number_of_sets;
190 unsigned int size = this_leaf->size;
191
192 /*
193 * If the cache is fully associative, there is no need to
194 * check the other properties.
195 */
196 if (!(nr_sets == 1) && (nr_sets > 0 && size > 0 && line_size > 0))
197 this_leaf->ways_of_associativity = (size / nr_sets) / line_size;
198}
199
200static bool cache_node_is_unified(struct cacheinfo *this_leaf,
201 struct device_node *np)
202{
203 return of_property_read_bool(np, "cache-unified");
204}
205
206static bool match_cache_node(struct device_node *cpu,
207 const struct device_node *cache_node)
208{
209 struct device_node *prev, *cache = of_find_next_cache_node(cpu);
210
211 while (cache) {
212 if (cache == cache_node) {
213 of_node_put(cache);
214 return true;
215 }
216
217 prev = cache;
218 cache = of_find_next_cache_node(cache);
219 of_node_put(prev);
220 }
221
222 return false;
223}
224
225#ifndef arch_compact_of_hwid
226#define arch_compact_of_hwid(_x) (_x)
227#endif
228
229static void cache_of_set_id(struct cacheinfo *this_leaf,
230 struct device_node *cache_node)
231{
232 struct device_node *cpu;
233 u32 min_id = ~0;
234
235 for_each_of_cpu_node(cpu) {
236 u64 id = of_get_cpu_hwid(cpu, 0);
237
238 id = arch_compact_of_hwid(id);
239 if (FIELD_GET(GENMASK_ULL(63, 32), id)) {
240 of_node_put(cpu);
241 return;
242 }
243
244 if (match_cache_node(cpu, cache_node))
245 min_id = min(min_id, id);
246 }
247
248 if (min_id != ~0) {
249 this_leaf->id = min_id;
250 this_leaf->attributes |= CACHE_ID;
251 }
252}
253
254static void cache_of_set_props(struct cacheinfo *this_leaf,
255 struct device_node *np)
256{
257 /*
258 * init_cache_level must setup the cache level correctly
259 * overriding the architecturally specified levels, so
260 * if type is NONE at this stage, it should be unified
261 */
262 if (this_leaf->type == CACHE_TYPE_NOCACHE &&
263 cache_node_is_unified(this_leaf, np))
264 this_leaf->type = CACHE_TYPE_UNIFIED;
265 cache_size(this_leaf, np);
266 cache_get_line_size(this_leaf, np);
267 cache_nr_sets(this_leaf, np);
268 cache_associativity(this_leaf);
269 cache_of_set_id(this_leaf, np);
270}
271
272static int cache_setup_of_node(unsigned int cpu)
273{
274 struct cacheinfo *this_leaf;
275 unsigned int index = 0;
276
277 struct device_node *np __free(device_node) = of_cpu_device_node_get(cpu);
278 if (!np) {
279 pr_err("Failed to find cpu%d device node\n", cpu);
280 return -ENOENT;
281 }
282
283 if (!of_check_cache_nodes(np)) {
284 return -ENOENT;
285 }
286
287 while (index < cache_leaves(cpu)) {
288 this_leaf = per_cpu_cacheinfo_idx(cpu, index);
289 if (this_leaf->level != 1) {
290 struct device_node *prev __free(device_node) = np;
291 np = of_find_next_cache_node(np);
292 if (!np)
293 break;
294 }
295 cache_of_set_props(this_leaf, np);
296 this_leaf->fw_token = np;
297 index++;
298 }
299
300 if (index != cache_leaves(cpu)) /* not all OF nodes populated */
301 return -ENOENT;
302
303 return 0;
304}
305
306static bool of_check_cache_nodes(struct device_node *np)
307{
308 if (of_property_present(np, "cache-size") ||
309 of_property_present(np, "i-cache-size") ||
310 of_property_present(np, "d-cache-size") ||
311 of_property_present(np, "cache-unified"))
312 return true;
313
314 struct device_node *next __free(device_node) = of_find_next_cache_node(np);
315 if (next) {
316 return true;
317 }
318
319 return false;
320}
321
322static int of_count_cache_leaves(struct device_node *np)
323{
324 unsigned int leaves = 0;
325
326 if (of_property_present(np, "cache-size"))
327 ++leaves;
328 if (of_property_present(np, "i-cache-size"))
329 ++leaves;
330 if (of_property_present(np, "d-cache-size"))
331 ++leaves;
332
333 if (!leaves) {
334 /* The '[i-|d-|]cache-size' property is required, but
335 * if absent, fallback on the 'cache-unified' property.
336 */
337 if (of_property_read_bool(np, "cache-unified"))
338 return 1;
339 else
340 return 2;
341 }
342
343 return leaves;
344}
345
346int init_of_cache_level(unsigned int cpu)
347{
348 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
349 struct device_node *np __free(device_node) = of_cpu_device_node_get(cpu);
350 unsigned int levels = 0, leaves, level;
351
352 if (!of_check_cache_nodes(np)) {
353 return -ENOENT;
354 }
355
356 leaves = of_count_cache_leaves(np);
357 if (leaves > 0)
358 levels = 1;
359
360 while (1) {
361 struct device_node *prev __free(device_node) = np;
362 np = of_find_next_cache_node(np);
363 if (!np)
364 break;
365
366 if (!of_device_is_compatible(np, "cache"))
367 return -EINVAL;
368 if (of_property_read_u32(np, "cache-level", &level))
369 return -EINVAL;
370 if (level <= levels)
371 return -EINVAL;
372
373 leaves += of_count_cache_leaves(np);
374 levels = level;
375 }
376
377 this_cpu_ci->num_levels = levels;
378 this_cpu_ci->num_leaves = leaves;
379
380 return 0;
381}
382
383#else
384static inline int cache_setup_of_node(unsigned int cpu) { return 0; }
385int init_of_cache_level(unsigned int cpu) { return 0; }
386#endif
387
388int __weak cache_setup_acpi(unsigned int cpu)
389{
390 return -ENOTSUPP;
391}
392
393unsigned int coherency_max_size;
394
395static int cache_setup_properties(unsigned int cpu)
396{
397 int ret = 0;
398
399 if (of_have_populated_dt())
400 ret = cache_setup_of_node(cpu);
401 else if (!acpi_disabled)
402 ret = cache_setup_acpi(cpu);
403
404 // Assume there is no cache information available in DT/ACPI from now.
405 if (ret && use_arch_cache_info())
406 use_arch_info = true;
407
408 return ret;
409}
410
411static int cache_shared_cpu_map_setup(unsigned int cpu)
412{
413 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
414 struct cacheinfo *this_leaf, *sib_leaf;
415 unsigned int index, sib_index;
416 int ret = 0;
417
418 if (this_cpu_ci->cpu_map_populated)
419 return 0;
420
421 /*
422 * skip setting up cache properties if LLC is valid, just need
423 * to update the shared cpu_map if the cache attributes were
424 * populated early before all the cpus are brought online
425 */
426 if (!last_level_cache_is_valid(cpu) && !use_arch_info) {
427 ret = cache_setup_properties(cpu);
428 if (ret)
429 return ret;
430 }
431
432 for (index = 0; index < cache_leaves(cpu); index++) {
433 unsigned int i;
434
435 this_leaf = per_cpu_cacheinfo_idx(cpu, index);
436
437 cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
438 for_each_online_cpu(i) {
439 if (i == cpu || !per_cpu_cacheinfo(i))
440 continue;/* skip if itself or no cacheinfo */
441 for (sib_index = 0; sib_index < cache_leaves(i); sib_index++) {
442 sib_leaf = per_cpu_cacheinfo_idx(i, sib_index);
443
444 /*
445 * Comparing cache IDs only makes sense if the leaves
446 * belong to the same cache level of same type. Skip
447 * the check if level and type do not match.
448 */
449 if (sib_leaf->level != this_leaf->level ||
450 sib_leaf->type != this_leaf->type)
451 continue;
452
453 if (cache_leaves_are_shared(this_leaf, sib_leaf)) {
454 cpumask_set_cpu(cpu, &sib_leaf->shared_cpu_map);
455 cpumask_set_cpu(i, &this_leaf->shared_cpu_map);
456 break;
457 }
458 }
459 }
460 /* record the maximum cache line size */
461 if (this_leaf->coherency_line_size > coherency_max_size)
462 coherency_max_size = this_leaf->coherency_line_size;
463 }
464
465 /* shared_cpu_map is now populated for the cpu */
466 this_cpu_ci->cpu_map_populated = true;
467 return 0;
468}
469
470static void cache_shared_cpu_map_remove(unsigned int cpu)
471{
472 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
473 struct cacheinfo *this_leaf, *sib_leaf;
474 unsigned int sibling, index, sib_index;
475
476 for (index = 0; index < cache_leaves(cpu); index++) {
477 this_leaf = per_cpu_cacheinfo_idx(cpu, index);
478 for_each_cpu(sibling, &this_leaf->shared_cpu_map) {
479 if (sibling == cpu || !per_cpu_cacheinfo(sibling))
480 continue;/* skip if itself or no cacheinfo */
481
482 for (sib_index = 0; sib_index < cache_leaves(sibling); sib_index++) {
483 sib_leaf = per_cpu_cacheinfo_idx(sibling, sib_index);
484
485 /*
486 * Comparing cache IDs only makes sense if the leaves
487 * belong to the same cache level of same type. Skip
488 * the check if level and type do not match.
489 */
490 if (sib_leaf->level != this_leaf->level ||
491 sib_leaf->type != this_leaf->type)
492 continue;
493
494 if (cache_leaves_are_shared(this_leaf, sib_leaf)) {
495 cpumask_clear_cpu(cpu, &sib_leaf->shared_cpu_map);
496 cpumask_clear_cpu(sibling, &this_leaf->shared_cpu_map);
497 break;
498 }
499 }
500 }
501 }
502
503 /* cpu is no longer populated in the shared map */
504 this_cpu_ci->cpu_map_populated = false;
505}
506
507static void free_cache_attributes(unsigned int cpu)
508{
509 if (!per_cpu_cacheinfo(cpu))
510 return;
511
512 cache_shared_cpu_map_remove(cpu);
513}
514
515int __weak early_cache_level(unsigned int cpu)
516{
517 return -ENOENT;
518}
519
520int __weak init_cache_level(unsigned int cpu)
521{
522 return -ENOENT;
523}
524
525int __weak populate_cache_leaves(unsigned int cpu)
526{
527 return -ENOENT;
528}
529
530static inline int allocate_cache_info(int cpu)
531{
532 per_cpu_cacheinfo(cpu) = kzalloc_objs(struct cacheinfo,
533 cache_leaves(cpu), GFP_ATOMIC);
534 if (!per_cpu_cacheinfo(cpu)) {
535 cache_leaves(cpu) = 0;
536 return -ENOMEM;
537 }
538
539 return 0;
540}
541
542int fetch_cache_info(unsigned int cpu)
543{
544 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
545 unsigned int levels = 0, split_levels = 0;
546 int ret;
547
548 if (acpi_disabled) {
549 ret = init_of_cache_level(cpu);
550 } else {
551 ret = acpi_get_cache_info(cpu, &levels, &split_levels);
552 if (!ret) {
553 this_cpu_ci->num_levels = levels;
554 /*
555 * This assumes that:
556 * - there cannot be any split caches (data/instruction)
557 * above a unified cache
558 * - data/instruction caches come by pair
559 */
560 this_cpu_ci->num_leaves = levels + split_levels;
561 }
562 }
563
564 if (ret || !cache_leaves(cpu)) {
565 ret = early_cache_level(cpu);
566 if (ret)
567 return ret;
568
569 if (!cache_leaves(cpu))
570 return -ENOENT;
571
572 this_cpu_ci->early_ci_levels = true;
573 }
574
575 return allocate_cache_info(cpu);
576}
577
578static inline int init_level_allocate_ci(unsigned int cpu)
579{
580 unsigned int early_leaves = cache_leaves(cpu);
581
582 /* Since early initialization/allocation of the cacheinfo is allowed
583 * via fetch_cache_info() and this also gets called as CPU hotplug
584 * callbacks via cacheinfo_cpu_online, the init/alloc can be skipped
585 * as it will happen only once (the cacheinfo memory is never freed).
586 * Just populate the cacheinfo. However, if the cacheinfo has been
587 * allocated early through the arch-specific early_cache_level() call,
588 * there is a chance the info is wrong (this can happen on arm64). In
589 * that case, call init_cache_level() anyway to give the arch-specific
590 * code a chance to make things right.
591 */
592 if (per_cpu_cacheinfo(cpu) && !ci_cacheinfo(cpu)->early_ci_levels)
593 return 0;
594
595 if (init_cache_level(cpu) || !cache_leaves(cpu))
596 return -ENOENT;
597
598 /*
599 * Now that we have properly initialized the cache level info, make
600 * sure we don't try to do that again the next time we are called
601 * (e.g. as CPU hotplug callbacks).
602 */
603 ci_cacheinfo(cpu)->early_ci_levels = false;
604
605 /*
606 * Some architectures (e.g., x86) do not use early initialization.
607 * Allocate memory now in such case.
608 */
609 if (cache_leaves(cpu) <= early_leaves && per_cpu_cacheinfo(cpu))
610 return 0;
611
612 kfree(per_cpu_cacheinfo(cpu));
613 return allocate_cache_info(cpu);
614}
615
616int detect_cache_attributes(unsigned int cpu)
617{
618 int ret;
619
620 ret = init_level_allocate_ci(cpu);
621 if (ret)
622 return ret;
623
624 /*
625 * If LLC is valid the cache leaves were already populated so just go to
626 * update the cpu map.
627 */
628 if (!last_level_cache_is_valid(cpu)) {
629 /*
630 * populate_cache_leaves() may completely setup the cache leaves and
631 * shared_cpu_map or it may leave it partially setup.
632 */
633 ret = populate_cache_leaves(cpu);
634 if (ret)
635 goto free_ci;
636 }
637
638 /*
639 * For systems using DT for cache hierarchy, fw_token
640 * and shared_cpu_map will be set up here only if they are
641 * not populated already
642 */
643 ret = cache_shared_cpu_map_setup(cpu);
644 if (ret) {
645 pr_warn("Unable to detect cache hierarchy for CPU %d\n", cpu);
646 goto free_ci;
647 }
648
649 return 0;
650
651free_ci:
652 free_cache_attributes(cpu);
653 return ret;
654}
655
656/* pointer to cpuX/cache device */
657static DEFINE_PER_CPU(struct device *, ci_cache_dev);
658#define per_cpu_cache_dev(cpu) (per_cpu(ci_cache_dev, cpu))
659
660static cpumask_t cache_dev_map;
661
662/* pointer to array of devices for cpuX/cache/indexY */
663static DEFINE_PER_CPU(struct device **, ci_index_dev);
664#define per_cpu_index_dev(cpu) (per_cpu(ci_index_dev, cpu))
665#define per_cache_index_dev(cpu, idx) ((per_cpu_index_dev(cpu))[idx])
666
667#define show_one(file_name, object) \
668static ssize_t file_name##_show(struct device *dev, \
669 struct device_attribute *attr, char *buf) \
670{ \
671 struct cacheinfo *this_leaf = dev_get_drvdata(dev); \
672 return sysfs_emit(buf, "%u\n", this_leaf->object); \
673}
674
675show_one(id, id);
676show_one(level, level);
677show_one(coherency_line_size, coherency_line_size);
678show_one(number_of_sets, number_of_sets);
679show_one(physical_line_partition, physical_line_partition);
680show_one(ways_of_associativity, ways_of_associativity);
681
682static ssize_t size_show(struct device *dev,
683 struct device_attribute *attr, char *buf)
684{
685 struct cacheinfo *this_leaf = dev_get_drvdata(dev);
686
687 return sysfs_emit(buf, "%uK\n", this_leaf->size >> 10);
688}
689
690static ssize_t shared_cpu_map_show(struct device *dev,
691 struct device_attribute *attr, char *buf)
692{
693 struct cacheinfo *this_leaf = dev_get_drvdata(dev);
694 const struct cpumask *mask = &this_leaf->shared_cpu_map;
695
696 return sysfs_emit(buf, "%*pb\n", nr_cpu_ids, mask);
697}
698
699static ssize_t shared_cpu_list_show(struct device *dev,
700 struct device_attribute *attr, char *buf)
701{
702 struct cacheinfo *this_leaf = dev_get_drvdata(dev);
703 const struct cpumask *mask = &this_leaf->shared_cpu_map;
704
705 return sysfs_emit(buf, "%*pbl\n", nr_cpu_ids, mask);
706}
707
708static ssize_t type_show(struct device *dev,
709 struct device_attribute *attr, char *buf)
710{
711 struct cacheinfo *this_leaf = dev_get_drvdata(dev);
712 const char *output;
713
714 switch (this_leaf->type) {
715 case CACHE_TYPE_DATA:
716 output = "Data";
717 break;
718 case CACHE_TYPE_INST:
719 output = "Instruction";
720 break;
721 case CACHE_TYPE_UNIFIED:
722 output = "Unified";
723 break;
724 default:
725 return -EINVAL;
726 }
727
728 return sysfs_emit(buf, "%s\n", output);
729}
730
731static ssize_t allocation_policy_show(struct device *dev,
732 struct device_attribute *attr, char *buf)
733{
734 struct cacheinfo *this_leaf = dev_get_drvdata(dev);
735 unsigned int ci_attr = this_leaf->attributes;
736 const char *output;
737
738 if ((ci_attr & CACHE_READ_ALLOCATE) && (ci_attr & CACHE_WRITE_ALLOCATE))
739 output = "ReadWriteAllocate";
740 else if (ci_attr & CACHE_READ_ALLOCATE)
741 output = "ReadAllocate";
742 else if (ci_attr & CACHE_WRITE_ALLOCATE)
743 output = "WriteAllocate";
744 else
745 return 0;
746
747 return sysfs_emit(buf, "%s\n", output);
748}
749
750static ssize_t write_policy_show(struct device *dev,
751 struct device_attribute *attr, char *buf)
752{
753 struct cacheinfo *this_leaf = dev_get_drvdata(dev);
754 unsigned int ci_attr = this_leaf->attributes;
755 int n = 0;
756
757 if (ci_attr & CACHE_WRITE_THROUGH)
758 n = sysfs_emit(buf, "WriteThrough\n");
759 else if (ci_attr & CACHE_WRITE_BACK)
760 n = sysfs_emit(buf, "WriteBack\n");
761 return n;
762}
763
764static DEVICE_ATTR_RO(id);
765static DEVICE_ATTR_RO(level);
766static DEVICE_ATTR_RO(type);
767static DEVICE_ATTR_RO(coherency_line_size);
768static DEVICE_ATTR_RO(ways_of_associativity);
769static DEVICE_ATTR_RO(number_of_sets);
770static DEVICE_ATTR_RO(size);
771static DEVICE_ATTR_RO(allocation_policy);
772static DEVICE_ATTR_RO(write_policy);
773static DEVICE_ATTR_RO(shared_cpu_map);
774static DEVICE_ATTR_RO(shared_cpu_list);
775static DEVICE_ATTR_RO(physical_line_partition);
776
777static struct attribute *cache_default_attrs[] = {
778 &dev_attr_id.attr,
779 &dev_attr_type.attr,
780 &dev_attr_level.attr,
781 &dev_attr_shared_cpu_map.attr,
782 &dev_attr_shared_cpu_list.attr,
783 &dev_attr_coherency_line_size.attr,
784 &dev_attr_ways_of_associativity.attr,
785 &dev_attr_number_of_sets.attr,
786 &dev_attr_size.attr,
787 &dev_attr_allocation_policy.attr,
788 &dev_attr_write_policy.attr,
789 &dev_attr_physical_line_partition.attr,
790 NULL
791};
792
793static umode_t
794cache_default_attrs_is_visible(struct kobject *kobj,
795 struct attribute *attr, int unused)
796{
797 struct device *dev = kobj_to_dev(kobj);
798 struct cacheinfo *this_leaf = dev_get_drvdata(dev);
799 const struct cpumask *mask = &this_leaf->shared_cpu_map;
800 umode_t mode = attr->mode;
801
802 if ((attr == &dev_attr_id.attr) && (this_leaf->attributes & CACHE_ID))
803 return mode;
804 if ((attr == &dev_attr_type.attr) && this_leaf->type)
805 return mode;
806 if ((attr == &dev_attr_level.attr) && this_leaf->level)
807 return mode;
808 if ((attr == &dev_attr_shared_cpu_map.attr) && !cpumask_empty(mask))
809 return mode;
810 if ((attr == &dev_attr_shared_cpu_list.attr) && !cpumask_empty(mask))
811 return mode;
812 if ((attr == &dev_attr_coherency_line_size.attr) &&
813 this_leaf->coherency_line_size)
814 return mode;
815 if ((attr == &dev_attr_ways_of_associativity.attr) &&
816 this_leaf->size) /* allow 0 = full associativity */
817 return mode;
818 if ((attr == &dev_attr_number_of_sets.attr) &&
819 this_leaf->number_of_sets)
820 return mode;
821 if ((attr == &dev_attr_size.attr) && this_leaf->size)
822 return mode;
823 if ((attr == &dev_attr_write_policy.attr) &&
824 (this_leaf->attributes & CACHE_WRITE_POLICY_MASK))
825 return mode;
826 if ((attr == &dev_attr_allocation_policy.attr) &&
827 (this_leaf->attributes & CACHE_ALLOCATE_POLICY_MASK))
828 return mode;
829 if ((attr == &dev_attr_physical_line_partition.attr) &&
830 this_leaf->physical_line_partition)
831 return mode;
832
833 return 0;
834}
835
836static const struct attribute_group cache_default_group = {
837 .attrs = cache_default_attrs,
838 .is_visible = cache_default_attrs_is_visible,
839};
840
841static const struct attribute_group *cache_default_groups[] = {
842 &cache_default_group,
843 NULL,
844};
845
846static const struct attribute_group *cache_private_groups[] = {
847 &cache_default_group,
848 NULL, /* Place holder for private group */
849 NULL,
850};
851
852const struct attribute_group *
853__weak cache_get_priv_group(struct cacheinfo *this_leaf)
854{
855 return NULL;
856}
857
858static const struct attribute_group **
859cache_get_attribute_groups(struct cacheinfo *this_leaf)
860{
861 const struct attribute_group *priv_group =
862 cache_get_priv_group(this_leaf);
863
864 if (!priv_group)
865 return cache_default_groups;
866
867 if (!cache_private_groups[1])
868 cache_private_groups[1] = priv_group;
869
870 return cache_private_groups;
871}
872
873/* Add/Remove cache interface for CPU device */
874static void cpu_cache_sysfs_exit(unsigned int cpu)
875{
876 int i;
877 struct device *ci_dev;
878
879 if (per_cpu_index_dev(cpu)) {
880 for (i = 0; i < cache_leaves(cpu); i++) {
881 ci_dev = per_cache_index_dev(cpu, i);
882 if (!ci_dev)
883 continue;
884 device_unregister(ci_dev);
885 }
886 kfree(per_cpu_index_dev(cpu));
887 per_cpu_index_dev(cpu) = NULL;
888 }
889 device_unregister(per_cpu_cache_dev(cpu));
890 per_cpu_cache_dev(cpu) = NULL;
891}
892
893static int cpu_cache_sysfs_init(unsigned int cpu)
894{
895 struct device *dev = get_cpu_device(cpu);
896
897 if (per_cpu_cacheinfo(cpu) == NULL)
898 return -ENOENT;
899
900 per_cpu_cache_dev(cpu) = cpu_device_create(dev, NULL, NULL, "cache");
901 if (IS_ERR(per_cpu_cache_dev(cpu)))
902 return PTR_ERR(per_cpu_cache_dev(cpu));
903
904 /* Allocate all required memory */
905 per_cpu_index_dev(cpu) = kzalloc_objs(struct device *,
906 cache_leaves(cpu));
907 if (unlikely(per_cpu_index_dev(cpu) == NULL))
908 goto err_out;
909
910 return 0;
911
912err_out:
913 cpu_cache_sysfs_exit(cpu);
914 return -ENOMEM;
915}
916
917static int cache_add_dev(unsigned int cpu)
918{
919 unsigned int i;
920 int rc;
921 struct device *ci_dev, *parent;
922 struct cacheinfo *this_leaf;
923 const struct attribute_group **cache_groups;
924
925 rc = cpu_cache_sysfs_init(cpu);
926 if (unlikely(rc < 0))
927 return rc;
928
929 parent = per_cpu_cache_dev(cpu);
930 for (i = 0; i < cache_leaves(cpu); i++) {
931 this_leaf = per_cpu_cacheinfo_idx(cpu, i);
932 if (this_leaf->disable_sysfs)
933 continue;
934 if (this_leaf->type == CACHE_TYPE_NOCACHE)
935 break;
936 cache_groups = cache_get_attribute_groups(this_leaf);
937 ci_dev = cpu_device_create(parent, this_leaf, cache_groups,
938 "index%1u", i);
939 if (IS_ERR(ci_dev)) {
940 rc = PTR_ERR(ci_dev);
941 goto err;
942 }
943 per_cache_index_dev(cpu, i) = ci_dev;
944 }
945 cpumask_set_cpu(cpu, &cache_dev_map);
946
947 return 0;
948err:
949 cpu_cache_sysfs_exit(cpu);
950 return rc;
951}
952
953static unsigned int cpu_map_shared_cache(bool online, unsigned int cpu,
954 cpumask_t **map)
955{
956 struct cacheinfo *llc, *sib_llc;
957 unsigned int sibling;
958
959 if (!last_level_cache_is_valid(cpu))
960 return 0;
961
962 llc = per_cpu_cacheinfo_idx(cpu, cache_leaves(cpu) - 1);
963
964 if (llc->type != CACHE_TYPE_DATA && llc->type != CACHE_TYPE_UNIFIED)
965 return 0;
966
967 if (online) {
968 *map = &llc->shared_cpu_map;
969 return cpumask_weight(*map);
970 }
971
972 /* shared_cpu_map of offlined CPU will be cleared, so use sibling map */
973 for_each_cpu(sibling, &llc->shared_cpu_map) {
974 if (sibling == cpu || !last_level_cache_is_valid(sibling))
975 continue;
976 sib_llc = per_cpu_cacheinfo_idx(sibling, cache_leaves(sibling) - 1);
977 *map = &sib_llc->shared_cpu_map;
978 return cpumask_weight(*map);
979 }
980
981 return 0;
982}
983
984/*
985 * Calculate the size of the per-CPU data cache slice. This can be
986 * used to estimate the size of the data cache slice that can be used
987 * by one CPU under ideal circumstances. UNIFIED caches are counted
988 * in addition to DATA caches. So, please consider code cache usage
989 * when use the result.
990 *
991 * Because the cache inclusive/non-inclusive information isn't
992 * available, we just use the size of the per-CPU slice of LLC to make
993 * the result more predictable across architectures.
994 */
995static void update_per_cpu_data_slice_size_cpu(unsigned int cpu)
996{
997 struct cpu_cacheinfo *ci;
998 struct cacheinfo *llc;
999 unsigned int nr_shared;
1000
1001 if (!last_level_cache_is_valid(cpu))
1002 return;
1003
1004 ci = ci_cacheinfo(cpu);
1005 llc = per_cpu_cacheinfo_idx(cpu, cache_leaves(cpu) - 1);
1006
1007 if (llc->type != CACHE_TYPE_DATA && llc->type != CACHE_TYPE_UNIFIED)
1008 return;
1009
1010 nr_shared = cpumask_weight(&llc->shared_cpu_map);
1011 if (nr_shared)
1012 ci->per_cpu_data_slice_size = llc->size / nr_shared;
1013}
1014
1015static void update_per_cpu_data_slice_size(bool cpu_online, unsigned int cpu,
1016 cpumask_t *cpu_map)
1017{
1018 unsigned int icpu;
1019
1020 for_each_cpu(icpu, cpu_map) {
1021 if (!cpu_online && icpu == cpu)
1022 continue;
1023 update_per_cpu_data_slice_size_cpu(icpu);
1024 setup_pcp_cacheinfo(icpu);
1025 }
1026}
1027
1028static int cacheinfo_cpu_online(unsigned int cpu)
1029{
1030 int rc = detect_cache_attributes(cpu);
1031 cpumask_t *cpu_map;
1032
1033 if (rc)
1034 return rc;
1035 rc = cache_add_dev(cpu);
1036 if (rc)
1037 goto err;
1038 if (cpu_map_shared_cache(true, cpu, &cpu_map))
1039 update_per_cpu_data_slice_size(true, cpu, cpu_map);
1040 sched_update_llc_bytes(cpu);
1041 return 0;
1042err:
1043 free_cache_attributes(cpu);
1044 return rc;
1045}
1046
1047static int cacheinfo_cpu_pre_down(unsigned int cpu)
1048{
1049 cpumask_t *cpu_map;
1050 unsigned int nr_shared;
1051
1052 nr_shared = cpu_map_shared_cache(false, cpu, &cpu_map);
1053 if (cpumask_test_and_clear_cpu(cpu, &cache_dev_map))
1054 cpu_cache_sysfs_exit(cpu);
1055
1056 free_cache_attributes(cpu);
1057 if (nr_shared > 1)
1058 update_per_cpu_data_slice_size(false, cpu, cpu_map);
1059
1060 sched_update_llc_bytes(cpu);
1061
1062 return 0;
1063}
1064
1065static int __init cacheinfo_sysfs_init(void)
1066{
1067 return cpuhp_setup_state(CPUHP_AP_BASE_CACHEINFO_ONLINE,
1068 "base/cacheinfo:online",
1069 cacheinfo_cpu_online, cacheinfo_cpu_pre_down);
1070}
1071device_initcall(cacheinfo_sysfs_init);