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