xref: /openbmc/linux/drivers/base/cacheinfo.c (revision 52110313)
1989d42e8SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2246246cbSSudeep Holla /*
3246246cbSSudeep Holla  * cacheinfo support - processor cache information via sysfs
4246246cbSSudeep Holla  *
5246246cbSSudeep Holla  * Based on arch/x86/kernel/cpu/intel_cacheinfo.c
6246246cbSSudeep Holla  * Author: Sudeep Holla <sudeep.holla@arm.com>
7246246cbSSudeep Holla  */
88e1073b1SSudeep Holla #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
98e1073b1SSudeep Holla 
1055877ef4SSudeep Holla #include <linux/acpi.h>
11246246cbSSudeep Holla #include <linux/bitops.h>
12246246cbSSudeep Holla #include <linux/cacheinfo.h>
13246246cbSSudeep Holla #include <linux/compiler.h>
14246246cbSSudeep Holla #include <linux/cpu.h>
15246246cbSSudeep Holla #include <linux/device.h>
16246246cbSSudeep Holla #include <linux/init.h>
17d4ec840bSSudeep Holla #include <linux/of_device.h>
18246246cbSSudeep Holla #include <linux/sched.h>
19246246cbSSudeep Holla #include <linux/slab.h>
20246246cbSSudeep Holla #include <linux/smp.h>
21246246cbSSudeep Holla #include <linux/sysfs.h>
22246246cbSSudeep Holla 
23246246cbSSudeep Holla /* pointer to per cpu cacheinfo */
24246246cbSSudeep Holla static DEFINE_PER_CPU(struct cpu_cacheinfo, ci_cpu_cacheinfo);
25246246cbSSudeep Holla #define ci_cacheinfo(cpu)	(&per_cpu(ci_cpu_cacheinfo, cpu))
26246246cbSSudeep Holla #define cache_leaves(cpu)	(ci_cacheinfo(cpu)->num_leaves)
27246246cbSSudeep Holla #define per_cpu_cacheinfo(cpu)	(ci_cacheinfo(cpu)->info_list)
28b14e8d21SSudeep Holla #define per_cpu_cacheinfo_idx(cpu, idx)		\
29b14e8d21SSudeep Holla 				(per_cpu_cacheinfo(cpu) + (idx))
30246246cbSSudeep Holla 
31246246cbSSudeep Holla struct cpu_cacheinfo *get_cpu_cacheinfo(unsigned int cpu)
32246246cbSSudeep Holla {
33246246cbSSudeep Holla 	return ci_cacheinfo(cpu);
34246246cbSSudeep Holla }
35246246cbSSudeep Holla 
36246246cbSSudeep Holla static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf,
37246246cbSSudeep Holla 					   struct cacheinfo *sib_leaf)
38246246cbSSudeep Holla {
399447eb0fSSudeep Holla 	/*
409447eb0fSSudeep Holla 	 * For non DT/ACPI systems, assume unique level 1 caches,
419447eb0fSSudeep Holla 	 * system-wide shared caches for all other levels. This will be used
429447eb0fSSudeep Holla 	 * only if arch specific code has not populated shared_cpu_map
439447eb0fSSudeep Holla 	 */
449447eb0fSSudeep Holla 	if (!(IS_ENABLED(CONFIG_OF) || IS_ENABLED(CONFIG_ACPI)))
459447eb0fSSudeep Holla 		return !(this_leaf->level == 1);
469447eb0fSSudeep Holla 
47f16d1becSSudeep Holla 	if ((sib_leaf->attributes & CACHE_ID) &&
48f16d1becSSudeep Holla 	    (this_leaf->attributes & CACHE_ID))
49f16d1becSSudeep Holla 		return sib_leaf->id == this_leaf->id;
50f16d1becSSudeep Holla 
519b97387cSJeremy Linton 	return sib_leaf->fw_token == this_leaf->fw_token;
52246246cbSSudeep Holla }
53dfea747dSSudeep Holla 
54cc1cfc47SSudeep Holla bool last_level_cache_is_valid(unsigned int cpu)
55cc1cfc47SSudeep Holla {
56cc1cfc47SSudeep Holla 	struct cacheinfo *llc;
57cc1cfc47SSudeep Holla 
58cc1cfc47SSudeep Holla 	if (!cache_leaves(cpu))
59cc1cfc47SSudeep Holla 		return false;
60cc1cfc47SSudeep Holla 
61cc1cfc47SSudeep Holla 	llc = per_cpu_cacheinfo_idx(cpu, cache_leaves(cpu) - 1);
62cc1cfc47SSudeep Holla 
63f16d1becSSudeep Holla 	return (llc->attributes & CACHE_ID) || !!llc->fw_token;
64f16d1becSSudeep Holla 
65cc1cfc47SSudeep Holla }
66cc1cfc47SSudeep Holla 
67cc1cfc47SSudeep Holla bool last_level_cache_is_shared(unsigned int cpu_x, unsigned int cpu_y)
68cc1cfc47SSudeep Holla {
69cc1cfc47SSudeep Holla 	struct cacheinfo *llc_x, *llc_y;
70cc1cfc47SSudeep Holla 
71cc1cfc47SSudeep Holla 	if (!last_level_cache_is_valid(cpu_x) ||
72cc1cfc47SSudeep Holla 	    !last_level_cache_is_valid(cpu_y))
73cc1cfc47SSudeep Holla 		return false;
74cc1cfc47SSudeep Holla 
75cc1cfc47SSudeep Holla 	llc_x = per_cpu_cacheinfo_idx(cpu_x, cache_leaves(cpu_x) - 1);
76cc1cfc47SSudeep Holla 	llc_y = per_cpu_cacheinfo_idx(cpu_y, cache_leaves(cpu_y) - 1);
77cc1cfc47SSudeep Holla 
78cc1cfc47SSudeep Holla 	return cache_leaves_are_shared(llc_x, llc_y);
79cc1cfc47SSudeep Holla }
80cc1cfc47SSudeep Holla 
819447eb0fSSudeep Holla #ifdef CONFIG_OF
82dfea747dSSudeep Holla /* OF properties to query for a given cache type */
83dfea747dSSudeep Holla struct cache_type_info {
84dfea747dSSudeep Holla 	const char *size_prop;
85dfea747dSSudeep Holla 	const char *line_size_props[2];
86dfea747dSSudeep Holla 	const char *nr_sets_prop;
87dfea747dSSudeep Holla };
88dfea747dSSudeep Holla 
89dfea747dSSudeep Holla static const struct cache_type_info cache_type_info[] = {
90dfea747dSSudeep Holla 	{
91dfea747dSSudeep Holla 		.size_prop       = "cache-size",
92dfea747dSSudeep Holla 		.line_size_props = { "cache-line-size",
93dfea747dSSudeep Holla 				     "cache-block-size", },
94dfea747dSSudeep Holla 		.nr_sets_prop    = "cache-sets",
95dfea747dSSudeep Holla 	}, {
96dfea747dSSudeep Holla 		.size_prop       = "i-cache-size",
97dfea747dSSudeep Holla 		.line_size_props = { "i-cache-line-size",
98dfea747dSSudeep Holla 				     "i-cache-block-size", },
99dfea747dSSudeep Holla 		.nr_sets_prop    = "i-cache-sets",
100dfea747dSSudeep Holla 	}, {
101dfea747dSSudeep Holla 		.size_prop       = "d-cache-size",
102dfea747dSSudeep Holla 		.line_size_props = { "d-cache-line-size",
103dfea747dSSudeep Holla 				     "d-cache-block-size", },
104dfea747dSSudeep Holla 		.nr_sets_prop    = "d-cache-sets",
105dfea747dSSudeep Holla 	},
106dfea747dSSudeep Holla };
107dfea747dSSudeep Holla 
108dfea747dSSudeep Holla static inline int get_cacheinfo_idx(enum cache_type type)
109dfea747dSSudeep Holla {
110dfea747dSSudeep Holla 	if (type == CACHE_TYPE_UNIFIED)
111dfea747dSSudeep Holla 		return 0;
112dfea747dSSudeep Holla 	return type;
113dfea747dSSudeep Holla }
114dfea747dSSudeep Holla 
1152ff075c7SJeremy Linton static void cache_size(struct cacheinfo *this_leaf, struct device_node *np)
116dfea747dSSudeep Holla {
117dfea747dSSudeep Holla 	const char *propname;
118dfea747dSSudeep Holla 	int ct_idx;
119dfea747dSSudeep Holla 
120dfea747dSSudeep Holla 	ct_idx = get_cacheinfo_idx(this_leaf->type);
121dfea747dSSudeep Holla 	propname = cache_type_info[ct_idx].size_prop;
122dfea747dSSudeep Holla 
1233a34c986SHuacai Chen 	of_property_read_u32(np, propname, &this_leaf->size);
124dfea747dSSudeep Holla }
125dfea747dSSudeep Holla 
126dfea747dSSudeep Holla /* not cache_line_size() because that's a macro in include/linux/cache.h */
1272ff075c7SJeremy Linton static void cache_get_line_size(struct cacheinfo *this_leaf,
1282ff075c7SJeremy Linton 				struct device_node *np)
129dfea747dSSudeep Holla {
130dfea747dSSudeep Holla 	int i, lim, ct_idx;
131dfea747dSSudeep Holla 
132dfea747dSSudeep Holla 	ct_idx = get_cacheinfo_idx(this_leaf->type);
133dfea747dSSudeep Holla 	lim = ARRAY_SIZE(cache_type_info[ct_idx].line_size_props);
134dfea747dSSudeep Holla 
135dfea747dSSudeep Holla 	for (i = 0; i < lim; i++) {
136448a5a55SSudeep Holla 		int ret;
137448a5a55SSudeep Holla 		u32 line_size;
138dfea747dSSudeep Holla 		const char *propname;
139dfea747dSSudeep Holla 
140dfea747dSSudeep Holla 		propname = cache_type_info[ct_idx].line_size_props[i];
141448a5a55SSudeep Holla 		ret = of_property_read_u32(np, propname, &line_size);
142448a5a55SSudeep Holla 		if (!ret) {
143448a5a55SSudeep Holla 			this_leaf->coherency_line_size = line_size;
144dfea747dSSudeep Holla 			break;
145dfea747dSSudeep Holla 		}
146448a5a55SSudeep Holla 	}
147dfea747dSSudeep Holla }
148dfea747dSSudeep Holla 
1492ff075c7SJeremy Linton static void cache_nr_sets(struct cacheinfo *this_leaf, struct device_node *np)
150dfea747dSSudeep Holla {
151dfea747dSSudeep Holla 	const char *propname;
152dfea747dSSudeep Holla 	int ct_idx;
153dfea747dSSudeep Holla 
154dfea747dSSudeep Holla 	ct_idx = get_cacheinfo_idx(this_leaf->type);
155dfea747dSSudeep Holla 	propname = cache_type_info[ct_idx].nr_sets_prop;
156dfea747dSSudeep Holla 
1573a34c986SHuacai Chen 	of_property_read_u32(np, propname, &this_leaf->number_of_sets);
158dfea747dSSudeep Holla }
159dfea747dSSudeep Holla 
160dfea747dSSudeep Holla static void cache_associativity(struct cacheinfo *this_leaf)
161dfea747dSSudeep Holla {
162dfea747dSSudeep Holla 	unsigned int line_size = this_leaf->coherency_line_size;
163dfea747dSSudeep Holla 	unsigned int nr_sets = this_leaf->number_of_sets;
164dfea747dSSudeep Holla 	unsigned int size = this_leaf->size;
165dfea747dSSudeep Holla 
166dfea747dSSudeep Holla 	/*
167dfea747dSSudeep Holla 	 * If the cache is fully associative, there is no need to
168dfea747dSSudeep Holla 	 * check the other properties.
169dfea747dSSudeep Holla 	 */
170dfea747dSSudeep Holla 	if (!(nr_sets == 1) && (nr_sets > 0 && size > 0 && line_size > 0))
171dfea747dSSudeep Holla 		this_leaf->ways_of_associativity = (size / nr_sets) / line_size;
172dfea747dSSudeep Holla }
173dfea747dSSudeep Holla 
1742ff075c7SJeremy Linton static bool cache_node_is_unified(struct cacheinfo *this_leaf,
1752ff075c7SJeremy Linton 				  struct device_node *np)
176f57ab9a0SSudeep Holla {
1772ff075c7SJeremy Linton 	return of_property_read_bool(np, "cache-unified");
178f57ab9a0SSudeep Holla }
179f57ab9a0SSudeep Holla 
1802ff075c7SJeremy Linton static void cache_of_set_props(struct cacheinfo *this_leaf,
1812ff075c7SJeremy Linton 			       struct device_node *np)
182dfea747dSSudeep Holla {
183f57ab9a0SSudeep Holla 	/*
184f57ab9a0SSudeep Holla 	 * init_cache_level must setup the cache level correctly
185f57ab9a0SSudeep Holla 	 * overriding the architecturally specified levels, so
186f57ab9a0SSudeep Holla 	 * if type is NONE at this stage, it should be unified
187f57ab9a0SSudeep Holla 	 */
188f57ab9a0SSudeep Holla 	if (this_leaf->type == CACHE_TYPE_NOCACHE &&
1892ff075c7SJeremy Linton 	    cache_node_is_unified(this_leaf, np))
190f57ab9a0SSudeep Holla 		this_leaf->type = CACHE_TYPE_UNIFIED;
1912ff075c7SJeremy Linton 	cache_size(this_leaf, np);
1922ff075c7SJeremy Linton 	cache_get_line_size(this_leaf, np);
1932ff075c7SJeremy Linton 	cache_nr_sets(this_leaf, np);
194dfea747dSSudeep Holla 	cache_associativity(this_leaf);
195dfea747dSSudeep Holla }
196d529a18aSJeremy Linton 
197d529a18aSJeremy Linton static int cache_setup_of_node(unsigned int cpu)
198d529a18aSJeremy Linton {
199d529a18aSJeremy Linton 	struct device_node *np;
200d529a18aSJeremy Linton 	struct cacheinfo *this_leaf;
201d529a18aSJeremy Linton 	unsigned int index = 0;
202d529a18aSJeremy Linton 
203d4ec840bSSudeep Holla 	np = of_cpu_device_node_get(cpu);
204d529a18aSJeremy Linton 	if (!np) {
205d529a18aSJeremy Linton 		pr_err("Failed to find cpu%d device node\n", cpu);
206d529a18aSJeremy Linton 		return -ENOENT;
207d529a18aSJeremy Linton 	}
208d529a18aSJeremy Linton 
209d529a18aSJeremy Linton 	while (index < cache_leaves(cpu)) {
210b14e8d21SSudeep Holla 		this_leaf = per_cpu_cacheinfo_idx(cpu, index);
211d529a18aSJeremy Linton 		if (this_leaf->level != 1)
212d529a18aSJeremy Linton 			np = of_find_next_cache_node(np);
213d529a18aSJeremy Linton 		else
214d529a18aSJeremy Linton 			np = of_node_get(np);/* cpu node itself */
215d529a18aSJeremy Linton 		if (!np)
216d529a18aSJeremy Linton 			break;
2172ff075c7SJeremy Linton 		cache_of_set_props(this_leaf, np);
2189b97387cSJeremy Linton 		this_leaf->fw_token = np;
219d529a18aSJeremy Linton 		index++;
220d529a18aSJeremy Linton 	}
221d529a18aSJeremy Linton 
222d529a18aSJeremy Linton 	if (index != cache_leaves(cpu)) /* not all OF nodes populated */
223d529a18aSJeremy Linton 		return -ENOENT;
224d529a18aSJeremy Linton 
225d529a18aSJeremy Linton 	return 0;
226d529a18aSJeremy Linton }
227246246cbSSudeep Holla #else
228246246cbSSudeep Holla static inline int cache_setup_of_node(unsigned int cpu) { return 0; }
229246246cbSSudeep Holla #endif
230246246cbSSudeep Holla 
231582b468bSJeremy Linton int __weak cache_setup_acpi(unsigned int cpu)
232582b468bSJeremy Linton {
233582b468bSJeremy Linton 	return -ENOTSUPP;
234582b468bSJeremy Linton }
235582b468bSJeremy Linton 
2369a83c84cSShaokun Zhang unsigned int coherency_max_size;
2379a83c84cSShaokun Zhang 
23836bbc5b4SSudeep Holla static int cache_setup_properties(unsigned int cpu)
23936bbc5b4SSudeep Holla {
24036bbc5b4SSudeep Holla 	int ret = 0;
24136bbc5b4SSudeep Holla 
24236bbc5b4SSudeep Holla 	if (of_have_populated_dt())
24336bbc5b4SSudeep Holla 		ret = cache_setup_of_node(cpu);
24436bbc5b4SSudeep Holla 	else if (!acpi_disabled)
24536bbc5b4SSudeep Holla 		ret = cache_setup_acpi(cpu);
24636bbc5b4SSudeep Holla 
24736bbc5b4SSudeep Holla 	return ret;
24836bbc5b4SSudeep Holla }
24936bbc5b4SSudeep Holla 
250246246cbSSudeep Holla static int cache_shared_cpu_map_setup(unsigned int cpu)
251246246cbSSudeep Holla {
252246246cbSSudeep Holla 	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
253246246cbSSudeep Holla 	struct cacheinfo *this_leaf, *sib_leaf;
254246246cbSSudeep Holla 	unsigned int index;
25555877ef4SSudeep Holla 	int ret = 0;
256246246cbSSudeep Holla 
257fac51482SSudeep Holla 	if (this_cpu_ci->cpu_map_populated)
258fac51482SSudeep Holla 		return 0;
259fac51482SSudeep Holla 
26036bbc5b4SSudeep Holla 	/*
26136bbc5b4SSudeep Holla 	 * skip setting up cache properties if LLC is valid, just need
26236bbc5b4SSudeep Holla 	 * to update the shared cpu_map if the cache attributes were
26336bbc5b4SSudeep Holla 	 * populated early before all the cpus are brought online
26436bbc5b4SSudeep Holla 	 */
26536bbc5b4SSudeep Holla 	if (!last_level_cache_is_valid(cpu)) {
26636bbc5b4SSudeep Holla 		ret = cache_setup_properties(cpu);
267246246cbSSudeep Holla 		if (ret)
268246246cbSSudeep Holla 			return ret;
26936bbc5b4SSudeep Holla 	}
270246246cbSSudeep Holla 
271246246cbSSudeep Holla 	for (index = 0; index < cache_leaves(cpu); index++) {
272246246cbSSudeep Holla 		unsigned int i;
273246246cbSSudeep Holla 
274b14e8d21SSudeep Holla 		this_leaf = per_cpu_cacheinfo_idx(cpu, index);
275246246cbSSudeep Holla 
276246246cbSSudeep Holla 		cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
277246246cbSSudeep Holla 		for_each_online_cpu(i) {
278246246cbSSudeep Holla 			struct cpu_cacheinfo *sib_cpu_ci = get_cpu_cacheinfo(i);
279246246cbSSudeep Holla 
280246246cbSSudeep Holla 			if (i == cpu || !sib_cpu_ci->info_list)
281246246cbSSudeep Holla 				continue;/* skip if itself or no cacheinfo */
282*52110313SSudeep Holla 
283b14e8d21SSudeep Holla 			sib_leaf = per_cpu_cacheinfo_idx(i, index);
284246246cbSSudeep Holla 			if (cache_leaves_are_shared(this_leaf, sib_leaf)) {
285246246cbSSudeep Holla 				cpumask_set_cpu(cpu, &sib_leaf->shared_cpu_map);
286246246cbSSudeep Holla 				cpumask_set_cpu(i, &this_leaf->shared_cpu_map);
287246246cbSSudeep Holla 			}
288246246cbSSudeep Holla 		}
2899a83c84cSShaokun Zhang 		/* record the maximum cache line size */
2909a83c84cSShaokun Zhang 		if (this_leaf->coherency_line_size > coherency_max_size)
2919a83c84cSShaokun Zhang 			coherency_max_size = this_leaf->coherency_line_size;
292246246cbSSudeep Holla 	}
293246246cbSSudeep Holla 
294246246cbSSudeep Holla 	return 0;
295246246cbSSudeep Holla }
296246246cbSSudeep Holla 
297246246cbSSudeep Holla static void cache_shared_cpu_map_remove(unsigned int cpu)
298246246cbSSudeep Holla {
299246246cbSSudeep Holla 	struct cacheinfo *this_leaf, *sib_leaf;
300246246cbSSudeep Holla 	unsigned int sibling, index;
301246246cbSSudeep Holla 
302246246cbSSudeep Holla 	for (index = 0; index < cache_leaves(cpu); index++) {
303b14e8d21SSudeep Holla 		this_leaf = per_cpu_cacheinfo_idx(cpu, index);
304246246cbSSudeep Holla 		for_each_cpu(sibling, &this_leaf->shared_cpu_map) {
305*52110313SSudeep Holla 			struct cpu_cacheinfo *sib_cpu_ci =
306*52110313SSudeep Holla 						get_cpu_cacheinfo(sibling);
307246246cbSSudeep Holla 
308*52110313SSudeep Holla 			if (sibling == cpu || !sib_cpu_ci->info_list)
309*52110313SSudeep Holla 				continue;/* skip if itself or no cacheinfo */
3102110d70cSBorislav Petkov 
311b14e8d21SSudeep Holla 			sib_leaf = per_cpu_cacheinfo_idx(sibling, index);
312246246cbSSudeep Holla 			cpumask_clear_cpu(cpu, &sib_leaf->shared_cpu_map);
313246246cbSSudeep Holla 			cpumask_clear_cpu(sibling, &this_leaf->shared_cpu_map);
314246246cbSSudeep Holla 		}
315582b468bSJeremy Linton 		if (of_have_populated_dt())
3169b97387cSJeremy Linton 			of_node_put(this_leaf->fw_token);
317246246cbSSudeep Holla 	}
318246246cbSSudeep Holla }
319246246cbSSudeep Holla 
320246246cbSSudeep Holla static void free_cache_attributes(unsigned int cpu)
321246246cbSSudeep Holla {
3222110d70cSBorislav Petkov 	if (!per_cpu_cacheinfo(cpu))
3232110d70cSBorislav Petkov 		return;
3242110d70cSBorislav Petkov 
325246246cbSSudeep Holla 	cache_shared_cpu_map_remove(cpu);
326246246cbSSudeep Holla 
327246246cbSSudeep Holla 	kfree(per_cpu_cacheinfo(cpu));
328246246cbSSudeep Holla 	per_cpu_cacheinfo(cpu) = NULL;
329e022eac8SXiongfeng Wang 	cache_leaves(cpu) = 0;
330246246cbSSudeep Holla }
331246246cbSSudeep Holla 
332246246cbSSudeep Holla int __weak init_cache_level(unsigned int cpu)
333246246cbSSudeep Holla {
334246246cbSSudeep Holla 	return -ENOENT;
335246246cbSSudeep Holla }
336246246cbSSudeep Holla 
337246246cbSSudeep Holla int __weak populate_cache_leaves(unsigned int cpu)
338246246cbSSudeep Holla {
339246246cbSSudeep Holla 	return -ENOENT;
340246246cbSSudeep Holla }
341246246cbSSudeep Holla 
34236bbc5b4SSudeep Holla int detect_cache_attributes(unsigned int cpu)
343246246cbSSudeep Holla {
344246246cbSSudeep Holla 	int ret;
345246246cbSSudeep Holla 
34636bbc5b4SSudeep Holla 	/* Since early detection of the cacheinfo is allowed via this
34736bbc5b4SSudeep Holla 	 * function and this also gets called as CPU hotplug callbacks via
34836bbc5b4SSudeep Holla 	 * cacheinfo_cpu_online, the initialisation can be skipped and only
34936bbc5b4SSudeep Holla 	 * CPU maps can be updated as the CPU online status would be update
35036bbc5b4SSudeep Holla 	 * if called via cacheinfo_cpu_online path.
35136bbc5b4SSudeep Holla 	 */
35236bbc5b4SSudeep Holla 	if (per_cpu_cacheinfo(cpu))
35336bbc5b4SSudeep Holla 		goto update_cpu_map;
35436bbc5b4SSudeep Holla 
3553370e13aSSudeep Holla 	if (init_cache_level(cpu) || !cache_leaves(cpu))
356246246cbSSudeep Holla 		return -ENOENT;
357246246cbSSudeep Holla 
358246246cbSSudeep Holla 	per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu),
359246246cbSSudeep Holla 					 sizeof(struct cacheinfo), GFP_KERNEL);
36036bbc5b4SSudeep Holla 	if (per_cpu_cacheinfo(cpu) == NULL) {
36136bbc5b4SSudeep Holla 		cache_leaves(cpu) = 0;
362246246cbSSudeep Holla 		return -ENOMEM;
36336bbc5b4SSudeep Holla 	}
364246246cbSSudeep Holla 
3652ff075c7SJeremy Linton 	/*
3662ff075c7SJeremy Linton 	 * populate_cache_leaves() may completely setup the cache leaves and
3672ff075c7SJeremy Linton 	 * shared_cpu_map or it may leave it partially setup.
3682ff075c7SJeremy Linton 	 */
369246246cbSSudeep Holla 	ret = populate_cache_leaves(cpu);
370246246cbSSudeep Holla 	if (ret)
371246246cbSSudeep Holla 		goto free_ci;
37236bbc5b4SSudeep Holla 
37336bbc5b4SSudeep Holla update_cpu_map:
374246246cbSSudeep Holla 	/*
3759b97387cSJeremy Linton 	 * For systems using DT for cache hierarchy, fw_token
3769b97387cSJeremy Linton 	 * and shared_cpu_map will be set up here only if they are
3779b97387cSJeremy Linton 	 * not populated already
378246246cbSSudeep Holla 	 */
379246246cbSSudeep Holla 	ret = cache_shared_cpu_map_setup(cpu);
3808a7d95f9SSudeep Holla 	if (ret) {
38155877ef4SSudeep Holla 		pr_warn("Unable to detect cache hierarchy for CPU %d\n", cpu);
382246246cbSSudeep Holla 		goto free_ci;
3838a7d95f9SSudeep Holla 	}
384dfea747dSSudeep Holla 
385246246cbSSudeep Holla 	return 0;
386246246cbSSudeep Holla 
387246246cbSSudeep Holla free_ci:
388246246cbSSudeep Holla 	free_cache_attributes(cpu);
389246246cbSSudeep Holla 	return ret;
390246246cbSSudeep Holla }
391246246cbSSudeep Holla 
392246246cbSSudeep Holla /* pointer to cpuX/cache device */
393246246cbSSudeep Holla static DEFINE_PER_CPU(struct device *, ci_cache_dev);
394246246cbSSudeep Holla #define per_cpu_cache_dev(cpu)	(per_cpu(ci_cache_dev, cpu))
395246246cbSSudeep Holla 
396246246cbSSudeep Holla static cpumask_t cache_dev_map;
397246246cbSSudeep Holla 
398246246cbSSudeep Holla /* pointer to array of devices for cpuX/cache/indexY */
399246246cbSSudeep Holla static DEFINE_PER_CPU(struct device **, ci_index_dev);
400246246cbSSudeep Holla #define per_cpu_index_dev(cpu)	(per_cpu(ci_index_dev, cpu))
401246246cbSSudeep Holla #define per_cache_index_dev(cpu, idx)	((per_cpu_index_dev(cpu))[idx])
402246246cbSSudeep Holla 
403246246cbSSudeep Holla #define show_one(file_name, object)				\
404246246cbSSudeep Holla static ssize_t file_name##_show(struct device *dev,		\
405246246cbSSudeep Holla 		struct device_attribute *attr, char *buf)	\
406246246cbSSudeep Holla {								\
407246246cbSSudeep Holla 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);	\
408948b3edbSJoe Perches 	return sysfs_emit(buf, "%u\n", this_leaf->object);	\
409246246cbSSudeep Holla }
410246246cbSSudeep Holla 
411e9a2ea5aSFenghua Yu show_one(id, id);
412246246cbSSudeep Holla show_one(level, level);
413246246cbSSudeep Holla show_one(coherency_line_size, coherency_line_size);
414246246cbSSudeep Holla show_one(number_of_sets, number_of_sets);
415246246cbSSudeep Holla show_one(physical_line_partition, physical_line_partition);
416246246cbSSudeep Holla show_one(ways_of_associativity, ways_of_associativity);
417246246cbSSudeep Holla 
418246246cbSSudeep Holla static ssize_t size_show(struct device *dev,
419246246cbSSudeep Holla 			 struct device_attribute *attr, char *buf)
420246246cbSSudeep Holla {
421246246cbSSudeep Holla 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);
422246246cbSSudeep Holla 
423aa838896SJoe Perches 	return sysfs_emit(buf, "%uK\n", this_leaf->size >> 10);
424246246cbSSudeep Holla }
425246246cbSSudeep Holla 
426e015e036SJoe Perches static ssize_t shared_cpu_map_show(struct device *dev,
427e015e036SJoe Perches 				   struct device_attribute *attr, char *buf)
428246246cbSSudeep Holla {
429246246cbSSudeep Holla 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);
430246246cbSSudeep Holla 	const struct cpumask *mask = &this_leaf->shared_cpu_map;
431246246cbSSudeep Holla 
432e015e036SJoe Perches 	return sysfs_emit(buf, "%*pb\n", nr_cpu_ids, mask);
433246246cbSSudeep Holla }
434246246cbSSudeep Holla 
435246246cbSSudeep Holla static ssize_t shared_cpu_list_show(struct device *dev,
436246246cbSSudeep Holla 				    struct device_attribute *attr, char *buf)
437246246cbSSudeep Holla {
438e015e036SJoe Perches 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);
439e015e036SJoe Perches 	const struct cpumask *mask = &this_leaf->shared_cpu_map;
440e015e036SJoe Perches 
441e015e036SJoe Perches 	return sysfs_emit(buf, "%*pbl\n", nr_cpu_ids, mask);
442246246cbSSudeep Holla }
443246246cbSSudeep Holla 
444246246cbSSudeep Holla static ssize_t type_show(struct device *dev,
445246246cbSSudeep Holla 			 struct device_attribute *attr, char *buf)
446246246cbSSudeep Holla {
447246246cbSSudeep Holla 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);
448973c3911SJoe Perches 	const char *output;
449246246cbSSudeep Holla 
450246246cbSSudeep Holla 	switch (this_leaf->type) {
451246246cbSSudeep Holla 	case CACHE_TYPE_DATA:
452973c3911SJoe Perches 		output = "Data";
453973c3911SJoe Perches 		break;
454246246cbSSudeep Holla 	case CACHE_TYPE_INST:
455973c3911SJoe Perches 		output = "Instruction";
456973c3911SJoe Perches 		break;
457246246cbSSudeep Holla 	case CACHE_TYPE_UNIFIED:
458973c3911SJoe Perches 		output = "Unified";
459973c3911SJoe Perches 		break;
460246246cbSSudeep Holla 	default:
461246246cbSSudeep Holla 		return -EINVAL;
462246246cbSSudeep Holla 	}
463973c3911SJoe Perches 
464973c3911SJoe Perches 	return sysfs_emit(buf, "%s\n", output);
465246246cbSSudeep Holla }
466246246cbSSudeep Holla 
467246246cbSSudeep Holla static ssize_t allocation_policy_show(struct device *dev,
468246246cbSSudeep Holla 				      struct device_attribute *attr, char *buf)
469246246cbSSudeep Holla {
470246246cbSSudeep Holla 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);
471246246cbSSudeep Holla 	unsigned int ci_attr = this_leaf->attributes;
472973c3911SJoe Perches 	const char *output;
473246246cbSSudeep Holla 
474246246cbSSudeep Holla 	if ((ci_attr & CACHE_READ_ALLOCATE) && (ci_attr & CACHE_WRITE_ALLOCATE))
475973c3911SJoe Perches 		output = "ReadWriteAllocate";
476246246cbSSudeep Holla 	else if (ci_attr & CACHE_READ_ALLOCATE)
477973c3911SJoe Perches 		output = "ReadAllocate";
478246246cbSSudeep Holla 	else if (ci_attr & CACHE_WRITE_ALLOCATE)
479973c3911SJoe Perches 		output = "WriteAllocate";
480973c3911SJoe Perches 	else
481973c3911SJoe Perches 		return 0;
482973c3911SJoe Perches 
483973c3911SJoe Perches 	return sysfs_emit(buf, "%s\n", output);
484246246cbSSudeep Holla }
485246246cbSSudeep Holla 
486246246cbSSudeep Holla static ssize_t write_policy_show(struct device *dev,
487246246cbSSudeep Holla 				 struct device_attribute *attr, char *buf)
488246246cbSSudeep Holla {
489246246cbSSudeep Holla 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);
490246246cbSSudeep Holla 	unsigned int ci_attr = this_leaf->attributes;
491246246cbSSudeep Holla 	int n = 0;
492246246cbSSudeep Holla 
493246246cbSSudeep Holla 	if (ci_attr & CACHE_WRITE_THROUGH)
494aa838896SJoe Perches 		n = sysfs_emit(buf, "WriteThrough\n");
495246246cbSSudeep Holla 	else if (ci_attr & CACHE_WRITE_BACK)
496aa838896SJoe Perches 		n = sysfs_emit(buf, "WriteBack\n");
497246246cbSSudeep Holla 	return n;
498246246cbSSudeep Holla }
499246246cbSSudeep Holla 
500e9a2ea5aSFenghua Yu static DEVICE_ATTR_RO(id);
501246246cbSSudeep Holla static DEVICE_ATTR_RO(level);
502246246cbSSudeep Holla static DEVICE_ATTR_RO(type);
503246246cbSSudeep Holla static DEVICE_ATTR_RO(coherency_line_size);
504246246cbSSudeep Holla static DEVICE_ATTR_RO(ways_of_associativity);
505246246cbSSudeep Holla static DEVICE_ATTR_RO(number_of_sets);
506246246cbSSudeep Holla static DEVICE_ATTR_RO(size);
507246246cbSSudeep Holla static DEVICE_ATTR_RO(allocation_policy);
508246246cbSSudeep Holla static DEVICE_ATTR_RO(write_policy);
509246246cbSSudeep Holla static DEVICE_ATTR_RO(shared_cpu_map);
510246246cbSSudeep Holla static DEVICE_ATTR_RO(shared_cpu_list);
511246246cbSSudeep Holla static DEVICE_ATTR_RO(physical_line_partition);
512246246cbSSudeep Holla 
513246246cbSSudeep Holla static struct attribute *cache_default_attrs[] = {
514e9a2ea5aSFenghua Yu 	&dev_attr_id.attr,
515246246cbSSudeep Holla 	&dev_attr_type.attr,
516246246cbSSudeep Holla 	&dev_attr_level.attr,
517246246cbSSudeep Holla 	&dev_attr_shared_cpu_map.attr,
518246246cbSSudeep Holla 	&dev_attr_shared_cpu_list.attr,
519246246cbSSudeep Holla 	&dev_attr_coherency_line_size.attr,
520246246cbSSudeep Holla 	&dev_attr_ways_of_associativity.attr,
521246246cbSSudeep Holla 	&dev_attr_number_of_sets.attr,
522246246cbSSudeep Holla 	&dev_attr_size.attr,
523246246cbSSudeep Holla 	&dev_attr_allocation_policy.attr,
524246246cbSSudeep Holla 	&dev_attr_write_policy.attr,
525246246cbSSudeep Holla 	&dev_attr_physical_line_partition.attr,
526246246cbSSudeep Holla 	NULL
527246246cbSSudeep Holla };
528246246cbSSudeep Holla 
529246246cbSSudeep Holla static umode_t
530246246cbSSudeep Holla cache_default_attrs_is_visible(struct kobject *kobj,
531246246cbSSudeep Holla 			       struct attribute *attr, int unused)
532246246cbSSudeep Holla {
533246246cbSSudeep Holla 	struct device *dev = kobj_to_dev(kobj);
534246246cbSSudeep Holla 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);
535246246cbSSudeep Holla 	const struct cpumask *mask = &this_leaf->shared_cpu_map;
536246246cbSSudeep Holla 	umode_t mode = attr->mode;
537246246cbSSudeep Holla 
538e9a2ea5aSFenghua Yu 	if ((attr == &dev_attr_id.attr) && (this_leaf->attributes & CACHE_ID))
539e9a2ea5aSFenghua Yu 		return mode;
540246246cbSSudeep Holla 	if ((attr == &dev_attr_type.attr) && this_leaf->type)
541246246cbSSudeep Holla 		return mode;
542246246cbSSudeep Holla 	if ((attr == &dev_attr_level.attr) && this_leaf->level)
543246246cbSSudeep Holla 		return mode;
544246246cbSSudeep Holla 	if ((attr == &dev_attr_shared_cpu_map.attr) && !cpumask_empty(mask))
545246246cbSSudeep Holla 		return mode;
546246246cbSSudeep Holla 	if ((attr == &dev_attr_shared_cpu_list.attr) && !cpumask_empty(mask))
547246246cbSSudeep Holla 		return mode;
548246246cbSSudeep Holla 	if ((attr == &dev_attr_coherency_line_size.attr) &&
549246246cbSSudeep Holla 	    this_leaf->coherency_line_size)
550246246cbSSudeep Holla 		return mode;
551246246cbSSudeep Holla 	if ((attr == &dev_attr_ways_of_associativity.attr) &&
552246246cbSSudeep Holla 	    this_leaf->size) /* allow 0 = full associativity */
553246246cbSSudeep Holla 		return mode;
554246246cbSSudeep Holla 	if ((attr == &dev_attr_number_of_sets.attr) &&
555246246cbSSudeep Holla 	    this_leaf->number_of_sets)
556246246cbSSudeep Holla 		return mode;
557246246cbSSudeep Holla 	if ((attr == &dev_attr_size.attr) && this_leaf->size)
558246246cbSSudeep Holla 		return mode;
559246246cbSSudeep Holla 	if ((attr == &dev_attr_write_policy.attr) &&
560246246cbSSudeep Holla 	    (this_leaf->attributes & CACHE_WRITE_POLICY_MASK))
561246246cbSSudeep Holla 		return mode;
562246246cbSSudeep Holla 	if ((attr == &dev_attr_allocation_policy.attr) &&
563246246cbSSudeep Holla 	    (this_leaf->attributes & CACHE_ALLOCATE_POLICY_MASK))
564246246cbSSudeep Holla 		return mode;
565246246cbSSudeep Holla 	if ((attr == &dev_attr_physical_line_partition.attr) &&
566246246cbSSudeep Holla 	    this_leaf->physical_line_partition)
567246246cbSSudeep Holla 		return mode;
568246246cbSSudeep Holla 
569246246cbSSudeep Holla 	return 0;
570246246cbSSudeep Holla }
571246246cbSSudeep Holla 
572246246cbSSudeep Holla static const struct attribute_group cache_default_group = {
573246246cbSSudeep Holla 	.attrs = cache_default_attrs,
574246246cbSSudeep Holla 	.is_visible = cache_default_attrs_is_visible,
575246246cbSSudeep Holla };
576246246cbSSudeep Holla 
577246246cbSSudeep Holla static const struct attribute_group *cache_default_groups[] = {
578246246cbSSudeep Holla 	&cache_default_group,
579246246cbSSudeep Holla 	NULL,
580246246cbSSudeep Holla };
581246246cbSSudeep Holla 
582246246cbSSudeep Holla static const struct attribute_group *cache_private_groups[] = {
583246246cbSSudeep Holla 	&cache_default_group,
584246246cbSSudeep Holla 	NULL, /* Place holder for private group */
585246246cbSSudeep Holla 	NULL,
586246246cbSSudeep Holla };
587246246cbSSudeep Holla 
588246246cbSSudeep Holla const struct attribute_group *
589246246cbSSudeep Holla __weak cache_get_priv_group(struct cacheinfo *this_leaf)
590246246cbSSudeep Holla {
591246246cbSSudeep Holla 	return NULL;
592246246cbSSudeep Holla }
593246246cbSSudeep Holla 
594246246cbSSudeep Holla static const struct attribute_group **
595246246cbSSudeep Holla cache_get_attribute_groups(struct cacheinfo *this_leaf)
596246246cbSSudeep Holla {
597246246cbSSudeep Holla 	const struct attribute_group *priv_group =
598246246cbSSudeep Holla 			cache_get_priv_group(this_leaf);
599246246cbSSudeep Holla 
600246246cbSSudeep Holla 	if (!priv_group)
601246246cbSSudeep Holla 		return cache_default_groups;
602246246cbSSudeep Holla 
603246246cbSSudeep Holla 	if (!cache_private_groups[1])
604246246cbSSudeep Holla 		cache_private_groups[1] = priv_group;
605246246cbSSudeep Holla 
606246246cbSSudeep Holla 	return cache_private_groups;
607246246cbSSudeep Holla }
608246246cbSSudeep Holla 
609246246cbSSudeep Holla /* Add/Remove cache interface for CPU device */
610246246cbSSudeep Holla static void cpu_cache_sysfs_exit(unsigned int cpu)
611246246cbSSudeep Holla {
612246246cbSSudeep Holla 	int i;
613246246cbSSudeep Holla 	struct device *ci_dev;
614246246cbSSudeep Holla 
615246246cbSSudeep Holla 	if (per_cpu_index_dev(cpu)) {
616246246cbSSudeep Holla 		for (i = 0; i < cache_leaves(cpu); i++) {
617246246cbSSudeep Holla 			ci_dev = per_cache_index_dev(cpu, i);
618246246cbSSudeep Holla 			if (!ci_dev)
619246246cbSSudeep Holla 				continue;
620246246cbSSudeep Holla 			device_unregister(ci_dev);
621246246cbSSudeep Holla 		}
622246246cbSSudeep Holla 		kfree(per_cpu_index_dev(cpu));
623246246cbSSudeep Holla 		per_cpu_index_dev(cpu) = NULL;
624246246cbSSudeep Holla 	}
625246246cbSSudeep Holla 	device_unregister(per_cpu_cache_dev(cpu));
626246246cbSSudeep Holla 	per_cpu_cache_dev(cpu) = NULL;
627246246cbSSudeep Holla }
628246246cbSSudeep Holla 
629246246cbSSudeep Holla static int cpu_cache_sysfs_init(unsigned int cpu)
630246246cbSSudeep Holla {
631246246cbSSudeep Holla 	struct device *dev = get_cpu_device(cpu);
632246246cbSSudeep Holla 
633246246cbSSudeep Holla 	if (per_cpu_cacheinfo(cpu) == NULL)
634246246cbSSudeep Holla 		return -ENOENT;
635246246cbSSudeep Holla 
636246246cbSSudeep Holla 	per_cpu_cache_dev(cpu) = cpu_device_create(dev, NULL, NULL, "cache");
637246246cbSSudeep Holla 	if (IS_ERR(per_cpu_cache_dev(cpu)))
638246246cbSSudeep Holla 		return PTR_ERR(per_cpu_cache_dev(cpu));
639246246cbSSudeep Holla 
640246246cbSSudeep Holla 	/* Allocate all required memory */
641246246cbSSudeep Holla 	per_cpu_index_dev(cpu) = kcalloc(cache_leaves(cpu),
642246246cbSSudeep Holla 					 sizeof(struct device *), GFP_KERNEL);
643246246cbSSudeep Holla 	if (unlikely(per_cpu_index_dev(cpu) == NULL))
644246246cbSSudeep Holla 		goto err_out;
645246246cbSSudeep Holla 
646246246cbSSudeep Holla 	return 0;
647246246cbSSudeep Holla 
648246246cbSSudeep Holla err_out:
649246246cbSSudeep Holla 	cpu_cache_sysfs_exit(cpu);
650246246cbSSudeep Holla 	return -ENOMEM;
651246246cbSSudeep Holla }
652246246cbSSudeep Holla 
653246246cbSSudeep Holla static int cache_add_dev(unsigned int cpu)
654246246cbSSudeep Holla {
655246246cbSSudeep Holla 	unsigned int i;
656246246cbSSudeep Holla 	int rc;
657246246cbSSudeep Holla 	struct device *ci_dev, *parent;
658246246cbSSudeep Holla 	struct cacheinfo *this_leaf;
659246246cbSSudeep Holla 	const struct attribute_group **cache_groups;
660246246cbSSudeep Holla 
661246246cbSSudeep Holla 	rc = cpu_cache_sysfs_init(cpu);
662246246cbSSudeep Holla 	if (unlikely(rc < 0))
663246246cbSSudeep Holla 		return rc;
664246246cbSSudeep Holla 
665246246cbSSudeep Holla 	parent = per_cpu_cache_dev(cpu);
666246246cbSSudeep Holla 	for (i = 0; i < cache_leaves(cpu); i++) {
667b14e8d21SSudeep Holla 		this_leaf = per_cpu_cacheinfo_idx(cpu, i);
668246246cbSSudeep Holla 		if (this_leaf->disable_sysfs)
669246246cbSSudeep Holla 			continue;
670ca388e43SJeffrey Hugo 		if (this_leaf->type == CACHE_TYPE_NOCACHE)
671ca388e43SJeffrey Hugo 			break;
672246246cbSSudeep Holla 		cache_groups = cache_get_attribute_groups(this_leaf);
673246246cbSSudeep Holla 		ci_dev = cpu_device_create(parent, this_leaf, cache_groups,
674246246cbSSudeep Holla 					   "index%1u", i);
675246246cbSSudeep Holla 		if (IS_ERR(ci_dev)) {
676246246cbSSudeep Holla 			rc = PTR_ERR(ci_dev);
677246246cbSSudeep Holla 			goto err;
678246246cbSSudeep Holla 		}
679246246cbSSudeep Holla 		per_cache_index_dev(cpu, i) = ci_dev;
680246246cbSSudeep Holla 	}
681246246cbSSudeep Holla 	cpumask_set_cpu(cpu, &cache_dev_map);
682246246cbSSudeep Holla 
683246246cbSSudeep Holla 	return 0;
684246246cbSSudeep Holla err:
685246246cbSSudeep Holla 	cpu_cache_sysfs_exit(cpu);
686246246cbSSudeep Holla 	return rc;
687246246cbSSudeep Holla }
688246246cbSSudeep Holla 
6897cc277b4SSebastian Andrzej Siewior static int cacheinfo_cpu_online(unsigned int cpu)
690246246cbSSudeep Holla {
6917cc277b4SSebastian Andrzej Siewior 	int rc = detect_cache_attributes(cpu);
692246246cbSSudeep Holla 
6937cc277b4SSebastian Andrzej Siewior 	if (rc)
6947cc277b4SSebastian Andrzej Siewior 		return rc;
695246246cbSSudeep Holla 	rc = cache_add_dev(cpu);
6967cc277b4SSebastian Andrzej Siewior 	if (rc)
697246246cbSSudeep Holla 		free_cache_attributes(cpu);
6987cc277b4SSebastian Andrzej Siewior 	return rc;
699246246cbSSudeep Holla }
7007cc277b4SSebastian Andrzej Siewior 
7017cc277b4SSebastian Andrzej Siewior static int cacheinfo_cpu_pre_down(unsigned int cpu)
7027cc277b4SSebastian Andrzej Siewior {
7037cc277b4SSebastian Andrzej Siewior 	if (cpumask_test_and_clear_cpu(cpu, &cache_dev_map))
7047cc277b4SSebastian Andrzej Siewior 		cpu_cache_sysfs_exit(cpu);
7057cc277b4SSebastian Andrzej Siewior 
7067cc277b4SSebastian Andrzej Siewior 	free_cache_attributes(cpu);
7077cc277b4SSebastian Andrzej Siewior 	return 0;
708246246cbSSudeep Holla }
709246246cbSSudeep Holla 
710246246cbSSudeep Holla static int __init cacheinfo_sysfs_init(void)
711246246cbSSudeep Holla {
71283b44fe3SJames Morse 	return cpuhp_setup_state(CPUHP_AP_BASE_CACHEINFO_ONLINE,
71383b44fe3SJames Morse 				 "base/cacheinfo:online",
7147cc277b4SSebastian Andrzej Siewior 				 cacheinfo_cpu_online, cacheinfo_cpu_pre_down);
715246246cbSSudeep Holla }
716246246cbSSudeep Holla device_initcall(cacheinfo_sysfs_init);
717