xref: /openbmc/linux/drivers/base/cacheinfo.c (revision 989d42e8)
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  *
8246246cbSSudeep Holla  * This program is free software; you can redistribute it and/or modify
9246246cbSSudeep Holla  * it under the terms of the GNU General Public License version 2 as
10246246cbSSudeep Holla  * published by the Free Software Foundation.
11246246cbSSudeep Holla  *
12246246cbSSudeep Holla  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13246246cbSSudeep Holla  * kind, whether express or implied; without even the implied warranty
14246246cbSSudeep Holla  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15246246cbSSudeep Holla  * GNU General Public License for more details.
16246246cbSSudeep Holla  *
17246246cbSSudeep Holla  * You should have received a copy of the GNU General Public License
18246246cbSSudeep Holla  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19246246cbSSudeep Holla  */
208e1073b1SSudeep Holla #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
218e1073b1SSudeep Holla 
2255877ef4SSudeep Holla #include <linux/acpi.h>
23246246cbSSudeep Holla #include <linux/bitops.h>
24246246cbSSudeep Holla #include <linux/cacheinfo.h>
25246246cbSSudeep Holla #include <linux/compiler.h>
26246246cbSSudeep Holla #include <linux/cpu.h>
27246246cbSSudeep Holla #include <linux/device.h>
28246246cbSSudeep Holla #include <linux/init.h>
29246246cbSSudeep Holla #include <linux/of.h>
30246246cbSSudeep Holla #include <linux/sched.h>
31246246cbSSudeep Holla #include <linux/slab.h>
32246246cbSSudeep Holla #include <linux/smp.h>
33246246cbSSudeep Holla #include <linux/sysfs.h>
34246246cbSSudeep Holla 
35246246cbSSudeep Holla /* pointer to per cpu cacheinfo */
36246246cbSSudeep Holla static DEFINE_PER_CPU(struct cpu_cacheinfo, ci_cpu_cacheinfo);
37246246cbSSudeep Holla #define ci_cacheinfo(cpu)	(&per_cpu(ci_cpu_cacheinfo, cpu))
38246246cbSSudeep Holla #define cache_leaves(cpu)	(ci_cacheinfo(cpu)->num_leaves)
39246246cbSSudeep Holla #define per_cpu_cacheinfo(cpu)	(ci_cacheinfo(cpu)->info_list)
40246246cbSSudeep Holla 
41246246cbSSudeep Holla struct cpu_cacheinfo *get_cpu_cacheinfo(unsigned int cpu)
42246246cbSSudeep Holla {
43246246cbSSudeep Holla 	return ci_cacheinfo(cpu);
44246246cbSSudeep Holla }
45246246cbSSudeep Holla 
46246246cbSSudeep Holla #ifdef CONFIG_OF
47246246cbSSudeep Holla static int cache_setup_of_node(unsigned int cpu)
48246246cbSSudeep Holla {
49246246cbSSudeep Holla 	struct device_node *np;
50246246cbSSudeep Holla 	struct cacheinfo *this_leaf;
51246246cbSSudeep Holla 	struct device *cpu_dev = get_cpu_device(cpu);
52246246cbSSudeep Holla 	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
53246246cbSSudeep Holla 	unsigned int index = 0;
54246246cbSSudeep Holla 
55246246cbSSudeep Holla 	/* skip if of_node is already populated */
56246246cbSSudeep Holla 	if (this_cpu_ci->info_list->of_node)
57246246cbSSudeep Holla 		return 0;
58246246cbSSudeep Holla 
59246246cbSSudeep Holla 	if (!cpu_dev) {
60246246cbSSudeep Holla 		pr_err("No cpu device for CPU %d\n", cpu);
61246246cbSSudeep Holla 		return -ENODEV;
62246246cbSSudeep Holla 	}
63246246cbSSudeep Holla 	np = cpu_dev->of_node;
64246246cbSSudeep Holla 	if (!np) {
65246246cbSSudeep Holla 		pr_err("Failed to find cpu%d device node\n", cpu);
66246246cbSSudeep Holla 		return -ENOENT;
67246246cbSSudeep Holla 	}
68246246cbSSudeep Holla 
698a7d95f9SSudeep Holla 	while (index < cache_leaves(cpu)) {
70246246cbSSudeep Holla 		this_leaf = this_cpu_ci->info_list + index;
71246246cbSSudeep Holla 		if (this_leaf->level != 1)
72246246cbSSudeep Holla 			np = of_find_next_cache_node(np);
73246246cbSSudeep Holla 		else
74246246cbSSudeep Holla 			np = of_node_get(np);/* cpu node itself */
758a7d95f9SSudeep Holla 		if (!np)
768a7d95f9SSudeep Holla 			break;
77246246cbSSudeep Holla 		this_leaf->of_node = np;
78246246cbSSudeep Holla 		index++;
79246246cbSSudeep Holla 	}
808a7d95f9SSudeep Holla 
818a7d95f9SSudeep Holla 	if (index != cache_leaves(cpu)) /* not all OF nodes populated */
828a7d95f9SSudeep Holla 		return -ENOENT;
838a7d95f9SSudeep Holla 
84246246cbSSudeep Holla 	return 0;
85246246cbSSudeep Holla }
86246246cbSSudeep Holla 
87246246cbSSudeep Holla static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf,
88246246cbSSudeep Holla 					   struct cacheinfo *sib_leaf)
89246246cbSSudeep Holla {
90246246cbSSudeep Holla 	return sib_leaf->of_node == this_leaf->of_node;
91246246cbSSudeep Holla }
92dfea747dSSudeep Holla 
93dfea747dSSudeep Holla /* OF properties to query for a given cache type */
94dfea747dSSudeep Holla struct cache_type_info {
95dfea747dSSudeep Holla 	const char *size_prop;
96dfea747dSSudeep Holla 	const char *line_size_props[2];
97dfea747dSSudeep Holla 	const char *nr_sets_prop;
98dfea747dSSudeep Holla };
99dfea747dSSudeep Holla 
100dfea747dSSudeep Holla static const struct cache_type_info cache_type_info[] = {
101dfea747dSSudeep Holla 	{
102dfea747dSSudeep Holla 		.size_prop       = "cache-size",
103dfea747dSSudeep Holla 		.line_size_props = { "cache-line-size",
104dfea747dSSudeep Holla 				     "cache-block-size", },
105dfea747dSSudeep Holla 		.nr_sets_prop    = "cache-sets",
106dfea747dSSudeep Holla 	}, {
107dfea747dSSudeep Holla 		.size_prop       = "i-cache-size",
108dfea747dSSudeep Holla 		.line_size_props = { "i-cache-line-size",
109dfea747dSSudeep Holla 				     "i-cache-block-size", },
110dfea747dSSudeep Holla 		.nr_sets_prop    = "i-cache-sets",
111dfea747dSSudeep Holla 	}, {
112dfea747dSSudeep Holla 		.size_prop       = "d-cache-size",
113dfea747dSSudeep Holla 		.line_size_props = { "d-cache-line-size",
114dfea747dSSudeep Holla 				     "d-cache-block-size", },
115dfea747dSSudeep Holla 		.nr_sets_prop    = "d-cache-sets",
116dfea747dSSudeep Holla 	},
117dfea747dSSudeep Holla };
118dfea747dSSudeep Holla 
119dfea747dSSudeep Holla static inline int get_cacheinfo_idx(enum cache_type type)
120dfea747dSSudeep Holla {
121dfea747dSSudeep Holla 	if (type == CACHE_TYPE_UNIFIED)
122dfea747dSSudeep Holla 		return 0;
123dfea747dSSudeep Holla 	return type;
124dfea747dSSudeep Holla }
125dfea747dSSudeep Holla 
126dfea747dSSudeep Holla static void cache_size(struct cacheinfo *this_leaf)
127dfea747dSSudeep Holla {
128dfea747dSSudeep Holla 	const char *propname;
129dfea747dSSudeep Holla 	const __be32 *cache_size;
130dfea747dSSudeep Holla 	int ct_idx;
131dfea747dSSudeep Holla 
132dfea747dSSudeep Holla 	ct_idx = get_cacheinfo_idx(this_leaf->type);
133dfea747dSSudeep Holla 	propname = cache_type_info[ct_idx].size_prop;
134dfea747dSSudeep Holla 
135dfea747dSSudeep Holla 	cache_size = of_get_property(this_leaf->of_node, propname, NULL);
136dfea747dSSudeep Holla 	if (cache_size)
137dfea747dSSudeep Holla 		this_leaf->size = of_read_number(cache_size, 1);
138dfea747dSSudeep Holla }
139dfea747dSSudeep Holla 
140dfea747dSSudeep Holla /* not cache_line_size() because that's a macro in include/linux/cache.h */
141dfea747dSSudeep Holla static void cache_get_line_size(struct cacheinfo *this_leaf)
142dfea747dSSudeep Holla {
143dfea747dSSudeep Holla 	const __be32 *line_size;
144dfea747dSSudeep Holla 	int i, lim, ct_idx;
145dfea747dSSudeep Holla 
146dfea747dSSudeep Holla 	ct_idx = get_cacheinfo_idx(this_leaf->type);
147dfea747dSSudeep Holla 	lim = ARRAY_SIZE(cache_type_info[ct_idx].line_size_props);
148dfea747dSSudeep Holla 
149dfea747dSSudeep Holla 	for (i = 0; i < lim; i++) {
150dfea747dSSudeep Holla 		const char *propname;
151dfea747dSSudeep Holla 
152dfea747dSSudeep Holla 		propname = cache_type_info[ct_idx].line_size_props[i];
153dfea747dSSudeep Holla 		line_size = of_get_property(this_leaf->of_node, propname, NULL);
154dfea747dSSudeep Holla 		if (line_size)
155dfea747dSSudeep Holla 			break;
156dfea747dSSudeep Holla 	}
157dfea747dSSudeep Holla 
158dfea747dSSudeep Holla 	if (line_size)
159dfea747dSSudeep Holla 		this_leaf->coherency_line_size = of_read_number(line_size, 1);
160dfea747dSSudeep Holla }
161dfea747dSSudeep Holla 
162dfea747dSSudeep Holla static void cache_nr_sets(struct cacheinfo *this_leaf)
163dfea747dSSudeep Holla {
164dfea747dSSudeep Holla 	const char *propname;
165dfea747dSSudeep Holla 	const __be32 *nr_sets;
166dfea747dSSudeep Holla 	int ct_idx;
167dfea747dSSudeep Holla 
168dfea747dSSudeep Holla 	ct_idx = get_cacheinfo_idx(this_leaf->type);
169dfea747dSSudeep Holla 	propname = cache_type_info[ct_idx].nr_sets_prop;
170dfea747dSSudeep Holla 
171dfea747dSSudeep Holla 	nr_sets = of_get_property(this_leaf->of_node, propname, NULL);
172dfea747dSSudeep Holla 	if (nr_sets)
173dfea747dSSudeep Holla 		this_leaf->number_of_sets = of_read_number(nr_sets, 1);
174dfea747dSSudeep Holla }
175dfea747dSSudeep Holla 
176dfea747dSSudeep Holla static void cache_associativity(struct cacheinfo *this_leaf)
177dfea747dSSudeep Holla {
178dfea747dSSudeep Holla 	unsigned int line_size = this_leaf->coherency_line_size;
179dfea747dSSudeep Holla 	unsigned int nr_sets = this_leaf->number_of_sets;
180dfea747dSSudeep Holla 	unsigned int size = this_leaf->size;
181dfea747dSSudeep Holla 
182dfea747dSSudeep Holla 	/*
183dfea747dSSudeep Holla 	 * If the cache is fully associative, there is no need to
184dfea747dSSudeep Holla 	 * check the other properties.
185dfea747dSSudeep Holla 	 */
186dfea747dSSudeep Holla 	if (!(nr_sets == 1) && (nr_sets > 0 && size > 0 && line_size > 0))
187dfea747dSSudeep Holla 		this_leaf->ways_of_associativity = (size / nr_sets) / line_size;
188dfea747dSSudeep Holla }
189dfea747dSSudeep Holla 
190dfea747dSSudeep Holla static void cache_of_override_properties(unsigned int cpu)
191dfea747dSSudeep Holla {
192dfea747dSSudeep Holla 	int index;
193dfea747dSSudeep Holla 	struct cacheinfo *this_leaf;
194dfea747dSSudeep Holla 	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
195dfea747dSSudeep Holla 
196dfea747dSSudeep Holla 	for (index = 0; index < cache_leaves(cpu); index++) {
197dfea747dSSudeep Holla 		this_leaf = this_cpu_ci->info_list + index;
198dfea747dSSudeep Holla 		cache_size(this_leaf);
199dfea747dSSudeep Holla 		cache_get_line_size(this_leaf);
200dfea747dSSudeep Holla 		cache_nr_sets(this_leaf);
201dfea747dSSudeep Holla 		cache_associativity(this_leaf);
202dfea747dSSudeep Holla 	}
203dfea747dSSudeep Holla }
204246246cbSSudeep Holla #else
205dfea747dSSudeep Holla static void cache_of_override_properties(unsigned int cpu) { }
206246246cbSSudeep Holla static inline int cache_setup_of_node(unsigned int cpu) { return 0; }
207246246cbSSudeep Holla static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf,
208246246cbSSudeep Holla 					   struct cacheinfo *sib_leaf)
209246246cbSSudeep Holla {
210246246cbSSudeep Holla 	/*
211246246cbSSudeep Holla 	 * For non-DT systems, assume unique level 1 cache, system-wide
212246246cbSSudeep Holla 	 * shared caches for all other levels. This will be used only if
213246246cbSSudeep Holla 	 * arch specific code has not populated shared_cpu_map
214246246cbSSudeep Holla 	 */
215246246cbSSudeep Holla 	return !(this_leaf->level == 1);
216246246cbSSudeep Holla }
217246246cbSSudeep Holla #endif
218246246cbSSudeep Holla 
219246246cbSSudeep Holla static int cache_shared_cpu_map_setup(unsigned int cpu)
220246246cbSSudeep Holla {
221246246cbSSudeep Holla 	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
222246246cbSSudeep Holla 	struct cacheinfo *this_leaf, *sib_leaf;
223246246cbSSudeep Holla 	unsigned int index;
22455877ef4SSudeep Holla 	int ret = 0;
225246246cbSSudeep Holla 
226fac51482SSudeep Holla 	if (this_cpu_ci->cpu_map_populated)
227fac51482SSudeep Holla 		return 0;
228fac51482SSudeep Holla 
22955877ef4SSudeep Holla 	if (of_have_populated_dt())
230246246cbSSudeep Holla 		ret = cache_setup_of_node(cpu);
23155877ef4SSudeep Holla 	else if (!acpi_disabled)
23255877ef4SSudeep Holla 		/* No cache property/hierarchy support yet in ACPI */
23355877ef4SSudeep Holla 		ret = -ENOTSUPP;
234246246cbSSudeep Holla 	if (ret)
235246246cbSSudeep Holla 		return ret;
236246246cbSSudeep Holla 
237246246cbSSudeep Holla 	for (index = 0; index < cache_leaves(cpu); index++) {
238246246cbSSudeep Holla 		unsigned int i;
239246246cbSSudeep Holla 
240246246cbSSudeep Holla 		this_leaf = this_cpu_ci->info_list + index;
241246246cbSSudeep Holla 		/* skip if shared_cpu_map is already populated */
242246246cbSSudeep Holla 		if (!cpumask_empty(&this_leaf->shared_cpu_map))
243246246cbSSudeep Holla 			continue;
244246246cbSSudeep Holla 
245246246cbSSudeep Holla 		cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
246246246cbSSudeep Holla 		for_each_online_cpu(i) {
247246246cbSSudeep Holla 			struct cpu_cacheinfo *sib_cpu_ci = get_cpu_cacheinfo(i);
248246246cbSSudeep Holla 
249246246cbSSudeep Holla 			if (i == cpu || !sib_cpu_ci->info_list)
250246246cbSSudeep Holla 				continue;/* skip if itself or no cacheinfo */
251246246cbSSudeep Holla 			sib_leaf = sib_cpu_ci->info_list + index;
252246246cbSSudeep Holla 			if (cache_leaves_are_shared(this_leaf, sib_leaf)) {
253246246cbSSudeep Holla 				cpumask_set_cpu(cpu, &sib_leaf->shared_cpu_map);
254246246cbSSudeep Holla 				cpumask_set_cpu(i, &this_leaf->shared_cpu_map);
255246246cbSSudeep Holla 			}
256246246cbSSudeep Holla 		}
257246246cbSSudeep Holla 	}
258246246cbSSudeep Holla 
259246246cbSSudeep Holla 	return 0;
260246246cbSSudeep Holla }
261246246cbSSudeep Holla 
262246246cbSSudeep Holla static void cache_shared_cpu_map_remove(unsigned int cpu)
263246246cbSSudeep Holla {
264246246cbSSudeep Holla 	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
265246246cbSSudeep Holla 	struct cacheinfo *this_leaf, *sib_leaf;
266246246cbSSudeep Holla 	unsigned int sibling, index;
267246246cbSSudeep Holla 
268246246cbSSudeep Holla 	for (index = 0; index < cache_leaves(cpu); index++) {
269246246cbSSudeep Holla 		this_leaf = this_cpu_ci->info_list + index;
270246246cbSSudeep Holla 		for_each_cpu(sibling, &this_leaf->shared_cpu_map) {
271246246cbSSudeep Holla 			struct cpu_cacheinfo *sib_cpu_ci;
272246246cbSSudeep Holla 
273246246cbSSudeep Holla 			if (sibling == cpu) /* skip itself */
274246246cbSSudeep Holla 				continue;
2752110d70cSBorislav Petkov 
276246246cbSSudeep Holla 			sib_cpu_ci = get_cpu_cacheinfo(sibling);
2772110d70cSBorislav Petkov 			if (!sib_cpu_ci->info_list)
2782110d70cSBorislav Petkov 				continue;
2792110d70cSBorislav Petkov 
280246246cbSSudeep Holla 			sib_leaf = sib_cpu_ci->info_list + index;
281246246cbSSudeep Holla 			cpumask_clear_cpu(cpu, &sib_leaf->shared_cpu_map);
282246246cbSSudeep Holla 			cpumask_clear_cpu(sibling, &this_leaf->shared_cpu_map);
283246246cbSSudeep Holla 		}
284246246cbSSudeep Holla 		of_node_put(this_leaf->of_node);
285246246cbSSudeep Holla 	}
286246246cbSSudeep Holla }
287246246cbSSudeep Holla 
288dfea747dSSudeep Holla static void cache_override_properties(unsigned int cpu)
289dfea747dSSudeep Holla {
290dfea747dSSudeep Holla 	if (of_have_populated_dt())
291dfea747dSSudeep Holla 		return cache_of_override_properties(cpu);
292dfea747dSSudeep Holla }
293dfea747dSSudeep Holla 
294246246cbSSudeep Holla static void free_cache_attributes(unsigned int cpu)
295246246cbSSudeep Holla {
2962110d70cSBorislav Petkov 	if (!per_cpu_cacheinfo(cpu))
2972110d70cSBorislav Petkov 		return;
2982110d70cSBorislav Petkov 
299246246cbSSudeep Holla 	cache_shared_cpu_map_remove(cpu);
300246246cbSSudeep Holla 
301246246cbSSudeep Holla 	kfree(per_cpu_cacheinfo(cpu));
302246246cbSSudeep Holla 	per_cpu_cacheinfo(cpu) = NULL;
303246246cbSSudeep Holla }
304246246cbSSudeep Holla 
305246246cbSSudeep Holla int __weak init_cache_level(unsigned int cpu)
306246246cbSSudeep Holla {
307246246cbSSudeep Holla 	return -ENOENT;
308246246cbSSudeep Holla }
309246246cbSSudeep Holla 
310246246cbSSudeep Holla int __weak populate_cache_leaves(unsigned int cpu)
311246246cbSSudeep Holla {
312246246cbSSudeep Holla 	return -ENOENT;
313246246cbSSudeep Holla }
314246246cbSSudeep Holla 
315246246cbSSudeep Holla static int detect_cache_attributes(unsigned int cpu)
316246246cbSSudeep Holla {
317246246cbSSudeep Holla 	int ret;
318246246cbSSudeep Holla 
3193370e13aSSudeep Holla 	if (init_cache_level(cpu) || !cache_leaves(cpu))
320246246cbSSudeep Holla 		return -ENOENT;
321246246cbSSudeep Holla 
322246246cbSSudeep Holla 	per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu),
323246246cbSSudeep Holla 					 sizeof(struct cacheinfo), GFP_KERNEL);
324246246cbSSudeep Holla 	if (per_cpu_cacheinfo(cpu) == NULL)
325246246cbSSudeep Holla 		return -ENOMEM;
326246246cbSSudeep Holla 
327246246cbSSudeep Holla 	ret = populate_cache_leaves(cpu);
328246246cbSSudeep Holla 	if (ret)
329246246cbSSudeep Holla 		goto free_ci;
330246246cbSSudeep Holla 	/*
3312539b258SWill Deacon 	 * For systems using DT for cache hierarchy, of_node and shared_cpu_map
332246246cbSSudeep Holla 	 * will be set up here only if they are not populated already
333246246cbSSudeep Holla 	 */
334246246cbSSudeep Holla 	ret = cache_shared_cpu_map_setup(cpu);
3358a7d95f9SSudeep Holla 	if (ret) {
33655877ef4SSudeep Holla 		pr_warn("Unable to detect cache hierarchy for CPU %d\n", cpu);
337246246cbSSudeep Holla 		goto free_ci;
3388a7d95f9SSudeep Holla 	}
339dfea747dSSudeep Holla 
340dfea747dSSudeep Holla 	cache_override_properties(cpu);
341246246cbSSudeep Holla 	return 0;
342246246cbSSudeep Holla 
343246246cbSSudeep Holla free_ci:
344246246cbSSudeep Holla 	free_cache_attributes(cpu);
345246246cbSSudeep Holla 	return ret;
346246246cbSSudeep Holla }
347246246cbSSudeep Holla 
348246246cbSSudeep Holla /* pointer to cpuX/cache device */
349246246cbSSudeep Holla static DEFINE_PER_CPU(struct device *, ci_cache_dev);
350246246cbSSudeep Holla #define per_cpu_cache_dev(cpu)	(per_cpu(ci_cache_dev, cpu))
351246246cbSSudeep Holla 
352246246cbSSudeep Holla static cpumask_t cache_dev_map;
353246246cbSSudeep Holla 
354246246cbSSudeep Holla /* pointer to array of devices for cpuX/cache/indexY */
355246246cbSSudeep Holla static DEFINE_PER_CPU(struct device **, ci_index_dev);
356246246cbSSudeep Holla #define per_cpu_index_dev(cpu)	(per_cpu(ci_index_dev, cpu))
357246246cbSSudeep Holla #define per_cache_index_dev(cpu, idx)	((per_cpu_index_dev(cpu))[idx])
358246246cbSSudeep Holla 
359246246cbSSudeep Holla #define show_one(file_name, object)				\
360246246cbSSudeep Holla static ssize_t file_name##_show(struct device *dev,		\
361246246cbSSudeep Holla 		struct device_attribute *attr, char *buf)	\
362246246cbSSudeep Holla {								\
363246246cbSSudeep Holla 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);	\
364246246cbSSudeep Holla 	return sprintf(buf, "%u\n", this_leaf->object);		\
365246246cbSSudeep Holla }
366246246cbSSudeep Holla 
367e9a2ea5aSFenghua Yu show_one(id, id);
368246246cbSSudeep Holla show_one(level, level);
369246246cbSSudeep Holla show_one(coherency_line_size, coherency_line_size);
370246246cbSSudeep Holla show_one(number_of_sets, number_of_sets);
371246246cbSSudeep Holla show_one(physical_line_partition, physical_line_partition);
372246246cbSSudeep Holla show_one(ways_of_associativity, ways_of_associativity);
373246246cbSSudeep Holla 
374246246cbSSudeep Holla static ssize_t size_show(struct device *dev,
375246246cbSSudeep Holla 			 struct device_attribute *attr, char *buf)
376246246cbSSudeep Holla {
377246246cbSSudeep Holla 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);
378246246cbSSudeep Holla 
379246246cbSSudeep Holla 	return sprintf(buf, "%uK\n", this_leaf->size >> 10);
380246246cbSSudeep Holla }
381246246cbSSudeep Holla 
382246246cbSSudeep Holla static ssize_t shared_cpumap_show_func(struct device *dev, bool list, char *buf)
383246246cbSSudeep Holla {
384246246cbSSudeep Holla 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);
385246246cbSSudeep Holla 	const struct cpumask *mask = &this_leaf->shared_cpu_map;
386246246cbSSudeep Holla 
387246246cbSSudeep Holla 	return cpumap_print_to_pagebuf(list, buf, mask);
388246246cbSSudeep Holla }
389246246cbSSudeep Holla 
390246246cbSSudeep Holla static ssize_t shared_cpu_map_show(struct device *dev,
391246246cbSSudeep Holla 				   struct device_attribute *attr, char *buf)
392246246cbSSudeep Holla {
393246246cbSSudeep Holla 	return shared_cpumap_show_func(dev, false, buf);
394246246cbSSudeep Holla }
395246246cbSSudeep Holla 
396246246cbSSudeep Holla static ssize_t shared_cpu_list_show(struct device *dev,
397246246cbSSudeep Holla 				    struct device_attribute *attr, char *buf)
398246246cbSSudeep Holla {
399246246cbSSudeep Holla 	return shared_cpumap_show_func(dev, true, buf);
400246246cbSSudeep Holla }
401246246cbSSudeep Holla 
402246246cbSSudeep Holla static ssize_t type_show(struct device *dev,
403246246cbSSudeep Holla 			 struct device_attribute *attr, char *buf)
404246246cbSSudeep Holla {
405246246cbSSudeep Holla 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);
406246246cbSSudeep Holla 
407246246cbSSudeep Holla 	switch (this_leaf->type) {
408246246cbSSudeep Holla 	case CACHE_TYPE_DATA:
409246246cbSSudeep Holla 		return sprintf(buf, "Data\n");
410246246cbSSudeep Holla 	case CACHE_TYPE_INST:
411246246cbSSudeep Holla 		return sprintf(buf, "Instruction\n");
412246246cbSSudeep Holla 	case CACHE_TYPE_UNIFIED:
413246246cbSSudeep Holla 		return sprintf(buf, "Unified\n");
414246246cbSSudeep Holla 	default:
415246246cbSSudeep Holla 		return -EINVAL;
416246246cbSSudeep Holla 	}
417246246cbSSudeep Holla }
418246246cbSSudeep Holla 
419246246cbSSudeep Holla static ssize_t allocation_policy_show(struct device *dev,
420246246cbSSudeep Holla 				      struct device_attribute *attr, char *buf)
421246246cbSSudeep Holla {
422246246cbSSudeep Holla 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);
423246246cbSSudeep Holla 	unsigned int ci_attr = this_leaf->attributes;
424246246cbSSudeep Holla 	int n = 0;
425246246cbSSudeep Holla 
426246246cbSSudeep Holla 	if ((ci_attr & CACHE_READ_ALLOCATE) && (ci_attr & CACHE_WRITE_ALLOCATE))
427246246cbSSudeep Holla 		n = sprintf(buf, "ReadWriteAllocate\n");
428246246cbSSudeep Holla 	else if (ci_attr & CACHE_READ_ALLOCATE)
429246246cbSSudeep Holla 		n = sprintf(buf, "ReadAllocate\n");
430246246cbSSudeep Holla 	else if (ci_attr & CACHE_WRITE_ALLOCATE)
431246246cbSSudeep Holla 		n = sprintf(buf, "WriteAllocate\n");
432246246cbSSudeep Holla 	return n;
433246246cbSSudeep Holla }
434246246cbSSudeep Holla 
435246246cbSSudeep Holla static ssize_t write_policy_show(struct device *dev,
436246246cbSSudeep Holla 				 struct device_attribute *attr, char *buf)
437246246cbSSudeep Holla {
438246246cbSSudeep Holla 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);
439246246cbSSudeep Holla 	unsigned int ci_attr = this_leaf->attributes;
440246246cbSSudeep Holla 	int n = 0;
441246246cbSSudeep Holla 
442246246cbSSudeep Holla 	if (ci_attr & CACHE_WRITE_THROUGH)
443246246cbSSudeep Holla 		n = sprintf(buf, "WriteThrough\n");
444246246cbSSudeep Holla 	else if (ci_attr & CACHE_WRITE_BACK)
445246246cbSSudeep Holla 		n = sprintf(buf, "WriteBack\n");
446246246cbSSudeep Holla 	return n;
447246246cbSSudeep Holla }
448246246cbSSudeep Holla 
449e9a2ea5aSFenghua Yu static DEVICE_ATTR_RO(id);
450246246cbSSudeep Holla static DEVICE_ATTR_RO(level);
451246246cbSSudeep Holla static DEVICE_ATTR_RO(type);
452246246cbSSudeep Holla static DEVICE_ATTR_RO(coherency_line_size);
453246246cbSSudeep Holla static DEVICE_ATTR_RO(ways_of_associativity);
454246246cbSSudeep Holla static DEVICE_ATTR_RO(number_of_sets);
455246246cbSSudeep Holla static DEVICE_ATTR_RO(size);
456246246cbSSudeep Holla static DEVICE_ATTR_RO(allocation_policy);
457246246cbSSudeep Holla static DEVICE_ATTR_RO(write_policy);
458246246cbSSudeep Holla static DEVICE_ATTR_RO(shared_cpu_map);
459246246cbSSudeep Holla static DEVICE_ATTR_RO(shared_cpu_list);
460246246cbSSudeep Holla static DEVICE_ATTR_RO(physical_line_partition);
461246246cbSSudeep Holla 
462246246cbSSudeep Holla static struct attribute *cache_default_attrs[] = {
463e9a2ea5aSFenghua Yu 	&dev_attr_id.attr,
464246246cbSSudeep Holla 	&dev_attr_type.attr,
465246246cbSSudeep Holla 	&dev_attr_level.attr,
466246246cbSSudeep Holla 	&dev_attr_shared_cpu_map.attr,
467246246cbSSudeep Holla 	&dev_attr_shared_cpu_list.attr,
468246246cbSSudeep Holla 	&dev_attr_coherency_line_size.attr,
469246246cbSSudeep Holla 	&dev_attr_ways_of_associativity.attr,
470246246cbSSudeep Holla 	&dev_attr_number_of_sets.attr,
471246246cbSSudeep Holla 	&dev_attr_size.attr,
472246246cbSSudeep Holla 	&dev_attr_allocation_policy.attr,
473246246cbSSudeep Holla 	&dev_attr_write_policy.attr,
474246246cbSSudeep Holla 	&dev_attr_physical_line_partition.attr,
475246246cbSSudeep Holla 	NULL
476246246cbSSudeep Holla };
477246246cbSSudeep Holla 
478246246cbSSudeep Holla static umode_t
479246246cbSSudeep Holla cache_default_attrs_is_visible(struct kobject *kobj,
480246246cbSSudeep Holla 			       struct attribute *attr, int unused)
481246246cbSSudeep Holla {
482246246cbSSudeep Holla 	struct device *dev = kobj_to_dev(kobj);
483246246cbSSudeep Holla 	struct cacheinfo *this_leaf = dev_get_drvdata(dev);
484246246cbSSudeep Holla 	const struct cpumask *mask = &this_leaf->shared_cpu_map;
485246246cbSSudeep Holla 	umode_t mode = attr->mode;
486246246cbSSudeep Holla 
487e9a2ea5aSFenghua Yu 	if ((attr == &dev_attr_id.attr) && (this_leaf->attributes & CACHE_ID))
488e9a2ea5aSFenghua Yu 		return mode;
489246246cbSSudeep Holla 	if ((attr == &dev_attr_type.attr) && this_leaf->type)
490246246cbSSudeep Holla 		return mode;
491246246cbSSudeep Holla 	if ((attr == &dev_attr_level.attr) && this_leaf->level)
492246246cbSSudeep Holla 		return mode;
493246246cbSSudeep Holla 	if ((attr == &dev_attr_shared_cpu_map.attr) && !cpumask_empty(mask))
494246246cbSSudeep Holla 		return mode;
495246246cbSSudeep Holla 	if ((attr == &dev_attr_shared_cpu_list.attr) && !cpumask_empty(mask))
496246246cbSSudeep Holla 		return mode;
497246246cbSSudeep Holla 	if ((attr == &dev_attr_coherency_line_size.attr) &&
498246246cbSSudeep Holla 	    this_leaf->coherency_line_size)
499246246cbSSudeep Holla 		return mode;
500246246cbSSudeep Holla 	if ((attr == &dev_attr_ways_of_associativity.attr) &&
501246246cbSSudeep Holla 	    this_leaf->size) /* allow 0 = full associativity */
502246246cbSSudeep Holla 		return mode;
503246246cbSSudeep Holla 	if ((attr == &dev_attr_number_of_sets.attr) &&
504246246cbSSudeep Holla 	    this_leaf->number_of_sets)
505246246cbSSudeep Holla 		return mode;
506246246cbSSudeep Holla 	if ((attr == &dev_attr_size.attr) && this_leaf->size)
507246246cbSSudeep Holla 		return mode;
508246246cbSSudeep Holla 	if ((attr == &dev_attr_write_policy.attr) &&
509246246cbSSudeep Holla 	    (this_leaf->attributes & CACHE_WRITE_POLICY_MASK))
510246246cbSSudeep Holla 		return mode;
511246246cbSSudeep Holla 	if ((attr == &dev_attr_allocation_policy.attr) &&
512246246cbSSudeep Holla 	    (this_leaf->attributes & CACHE_ALLOCATE_POLICY_MASK))
513246246cbSSudeep Holla 		return mode;
514246246cbSSudeep Holla 	if ((attr == &dev_attr_physical_line_partition.attr) &&
515246246cbSSudeep Holla 	    this_leaf->physical_line_partition)
516246246cbSSudeep Holla 		return mode;
517246246cbSSudeep Holla 
518246246cbSSudeep Holla 	return 0;
519246246cbSSudeep Holla }
520246246cbSSudeep Holla 
521246246cbSSudeep Holla static const struct attribute_group cache_default_group = {
522246246cbSSudeep Holla 	.attrs = cache_default_attrs,
523246246cbSSudeep Holla 	.is_visible = cache_default_attrs_is_visible,
524246246cbSSudeep Holla };
525246246cbSSudeep Holla 
526246246cbSSudeep Holla static const struct attribute_group *cache_default_groups[] = {
527246246cbSSudeep Holla 	&cache_default_group,
528246246cbSSudeep Holla 	NULL,
529246246cbSSudeep Holla };
530246246cbSSudeep Holla 
531246246cbSSudeep Holla static const struct attribute_group *cache_private_groups[] = {
532246246cbSSudeep Holla 	&cache_default_group,
533246246cbSSudeep Holla 	NULL, /* Place holder for private group */
534246246cbSSudeep Holla 	NULL,
535246246cbSSudeep Holla };
536246246cbSSudeep Holla 
537246246cbSSudeep Holla const struct attribute_group *
538246246cbSSudeep Holla __weak cache_get_priv_group(struct cacheinfo *this_leaf)
539246246cbSSudeep Holla {
540246246cbSSudeep Holla 	return NULL;
541246246cbSSudeep Holla }
542246246cbSSudeep Holla 
543246246cbSSudeep Holla static const struct attribute_group **
544246246cbSSudeep Holla cache_get_attribute_groups(struct cacheinfo *this_leaf)
545246246cbSSudeep Holla {
546246246cbSSudeep Holla 	const struct attribute_group *priv_group =
547246246cbSSudeep Holla 			cache_get_priv_group(this_leaf);
548246246cbSSudeep Holla 
549246246cbSSudeep Holla 	if (!priv_group)
550246246cbSSudeep Holla 		return cache_default_groups;
551246246cbSSudeep Holla 
552246246cbSSudeep Holla 	if (!cache_private_groups[1])
553246246cbSSudeep Holla 		cache_private_groups[1] = priv_group;
554246246cbSSudeep Holla 
555246246cbSSudeep Holla 	return cache_private_groups;
556246246cbSSudeep Holla }
557246246cbSSudeep Holla 
558246246cbSSudeep Holla /* Add/Remove cache interface for CPU device */
559246246cbSSudeep Holla static void cpu_cache_sysfs_exit(unsigned int cpu)
560246246cbSSudeep Holla {
561246246cbSSudeep Holla 	int i;
562246246cbSSudeep Holla 	struct device *ci_dev;
563246246cbSSudeep Holla 
564246246cbSSudeep Holla 	if (per_cpu_index_dev(cpu)) {
565246246cbSSudeep Holla 		for (i = 0; i < cache_leaves(cpu); i++) {
566246246cbSSudeep Holla 			ci_dev = per_cache_index_dev(cpu, i);
567246246cbSSudeep Holla 			if (!ci_dev)
568246246cbSSudeep Holla 				continue;
569246246cbSSudeep Holla 			device_unregister(ci_dev);
570246246cbSSudeep Holla 		}
571246246cbSSudeep Holla 		kfree(per_cpu_index_dev(cpu));
572246246cbSSudeep Holla 		per_cpu_index_dev(cpu) = NULL;
573246246cbSSudeep Holla 	}
574246246cbSSudeep Holla 	device_unregister(per_cpu_cache_dev(cpu));
575246246cbSSudeep Holla 	per_cpu_cache_dev(cpu) = NULL;
576246246cbSSudeep Holla }
577246246cbSSudeep Holla 
578246246cbSSudeep Holla static int cpu_cache_sysfs_init(unsigned int cpu)
579246246cbSSudeep Holla {
580246246cbSSudeep Holla 	struct device *dev = get_cpu_device(cpu);
581246246cbSSudeep Holla 
582246246cbSSudeep Holla 	if (per_cpu_cacheinfo(cpu) == NULL)
583246246cbSSudeep Holla 		return -ENOENT;
584246246cbSSudeep Holla 
585246246cbSSudeep Holla 	per_cpu_cache_dev(cpu) = cpu_device_create(dev, NULL, NULL, "cache");
586246246cbSSudeep Holla 	if (IS_ERR(per_cpu_cache_dev(cpu)))
587246246cbSSudeep Holla 		return PTR_ERR(per_cpu_cache_dev(cpu));
588246246cbSSudeep Holla 
589246246cbSSudeep Holla 	/* Allocate all required memory */
590246246cbSSudeep Holla 	per_cpu_index_dev(cpu) = kcalloc(cache_leaves(cpu),
591246246cbSSudeep Holla 					 sizeof(struct device *), GFP_KERNEL);
592246246cbSSudeep Holla 	if (unlikely(per_cpu_index_dev(cpu) == NULL))
593246246cbSSudeep Holla 		goto err_out;
594246246cbSSudeep Holla 
595246246cbSSudeep Holla 	return 0;
596246246cbSSudeep Holla 
597246246cbSSudeep Holla err_out:
598246246cbSSudeep Holla 	cpu_cache_sysfs_exit(cpu);
599246246cbSSudeep Holla 	return -ENOMEM;
600246246cbSSudeep Holla }
601246246cbSSudeep Holla 
602246246cbSSudeep Holla static int cache_add_dev(unsigned int cpu)
603246246cbSSudeep Holla {
604246246cbSSudeep Holla 	unsigned int i;
605246246cbSSudeep Holla 	int rc;
606246246cbSSudeep Holla 	struct device *ci_dev, *parent;
607246246cbSSudeep Holla 	struct cacheinfo *this_leaf;
608246246cbSSudeep Holla 	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
609246246cbSSudeep Holla 	const struct attribute_group **cache_groups;
610246246cbSSudeep Holla 
611246246cbSSudeep Holla 	rc = cpu_cache_sysfs_init(cpu);
612246246cbSSudeep Holla 	if (unlikely(rc < 0))
613246246cbSSudeep Holla 		return rc;
614246246cbSSudeep Holla 
615246246cbSSudeep Holla 	parent = per_cpu_cache_dev(cpu);
616246246cbSSudeep Holla 	for (i = 0; i < cache_leaves(cpu); i++) {
617246246cbSSudeep Holla 		this_leaf = this_cpu_ci->info_list + i;
618246246cbSSudeep Holla 		if (this_leaf->disable_sysfs)
619246246cbSSudeep Holla 			continue;
620246246cbSSudeep Holla 		cache_groups = cache_get_attribute_groups(this_leaf);
621246246cbSSudeep Holla 		ci_dev = cpu_device_create(parent, this_leaf, cache_groups,
622246246cbSSudeep Holla 					   "index%1u", i);
623246246cbSSudeep Holla 		if (IS_ERR(ci_dev)) {
624246246cbSSudeep Holla 			rc = PTR_ERR(ci_dev);
625246246cbSSudeep Holla 			goto err;
626246246cbSSudeep Holla 		}
627246246cbSSudeep Holla 		per_cache_index_dev(cpu, i) = ci_dev;
628246246cbSSudeep Holla 	}
629246246cbSSudeep Holla 	cpumask_set_cpu(cpu, &cache_dev_map);
630246246cbSSudeep Holla 
631246246cbSSudeep Holla 	return 0;
632246246cbSSudeep Holla err:
633246246cbSSudeep Holla 	cpu_cache_sysfs_exit(cpu);
634246246cbSSudeep Holla 	return rc;
635246246cbSSudeep Holla }
636246246cbSSudeep Holla 
6377cc277b4SSebastian Andrzej Siewior static int cacheinfo_cpu_online(unsigned int cpu)
638246246cbSSudeep Holla {
6397cc277b4SSebastian Andrzej Siewior 	int rc = detect_cache_attributes(cpu);
640246246cbSSudeep Holla 
6417cc277b4SSebastian Andrzej Siewior 	if (rc)
6427cc277b4SSebastian Andrzej Siewior 		return rc;
643246246cbSSudeep Holla 	rc = cache_add_dev(cpu);
6447cc277b4SSebastian Andrzej Siewior 	if (rc)
645246246cbSSudeep Holla 		free_cache_attributes(cpu);
6467cc277b4SSebastian Andrzej Siewior 	return rc;
647246246cbSSudeep Holla }
6487cc277b4SSebastian Andrzej Siewior 
6497cc277b4SSebastian Andrzej Siewior static int cacheinfo_cpu_pre_down(unsigned int cpu)
6507cc277b4SSebastian Andrzej Siewior {
6517cc277b4SSebastian Andrzej Siewior 	if (cpumask_test_and_clear_cpu(cpu, &cache_dev_map))
6527cc277b4SSebastian Andrzej Siewior 		cpu_cache_sysfs_exit(cpu);
6537cc277b4SSebastian Andrzej Siewior 
6547cc277b4SSebastian Andrzej Siewior 	free_cache_attributes(cpu);
6557cc277b4SSebastian Andrzej Siewior 	return 0;
656246246cbSSudeep Holla }
657246246cbSSudeep Holla 
658246246cbSSudeep Holla static int __init cacheinfo_sysfs_init(void)
659246246cbSSudeep Holla {
6607cc277b4SSebastian Andrzej Siewior 	return cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "base/cacheinfo:online",
6617cc277b4SSebastian Andrzej Siewior 				 cacheinfo_cpu_online, cacheinfo_cpu_pre_down);
662246246cbSSudeep Holla }
663246246cbSSudeep Holla device_initcall(cacheinfo_sysfs_init);
664