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 <[email protected]>
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>
17b9581552SRob Herring #include <linux/of.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
31ef9f643aSPierre Gondois /* Set if no cache information is found in DT/ACPI. */
32ef9f643aSPierre Gondois static bool use_arch_info;
33ef9f643aSPierre Gondois
get_cpu_cacheinfo(unsigned int cpu)34246246cbSSudeep Holla struct cpu_cacheinfo *get_cpu_cacheinfo(unsigned int cpu)
35246246cbSSudeep Holla {
36246246cbSSudeep Holla return ci_cacheinfo(cpu);
37246246cbSSudeep Holla }
38246246cbSSudeep Holla
cache_leaves_are_shared(struct cacheinfo * this_leaf,struct cacheinfo * sib_leaf)39246246cbSSudeep Holla static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf,
40246246cbSSudeep Holla struct cacheinfo *sib_leaf)
41246246cbSSudeep Holla {
429447eb0fSSudeep Holla /*
439447eb0fSSudeep Holla * For non DT/ACPI systems, assume unique level 1 caches,
447a306e3eSPierre Gondois * system-wide shared caches for all other levels.
459447eb0fSSudeep Holla */
46ef9f643aSPierre Gondois if (!(IS_ENABLED(CONFIG_OF) || IS_ENABLED(CONFIG_ACPI)) ||
47ef9f643aSPierre Gondois use_arch_info)
487a306e3eSPierre Gondois return (this_leaf->level != 1) && (sib_leaf->level != 1);
499447eb0fSSudeep Holla
50f16d1becSSudeep Holla if ((sib_leaf->attributes & CACHE_ID) &&
51f16d1becSSudeep Holla (this_leaf->attributes & CACHE_ID))
52f16d1becSSudeep Holla return sib_leaf->id == this_leaf->id;
53f16d1becSSudeep Holla
549b97387cSJeremy Linton return sib_leaf->fw_token == this_leaf->fw_token;
55246246cbSSudeep Holla }
56dfea747dSSudeep Holla
last_level_cache_is_valid(unsigned int cpu)57cc1cfc47SSudeep Holla bool last_level_cache_is_valid(unsigned int cpu)
58cc1cfc47SSudeep Holla {
59cc1cfc47SSudeep Holla struct cacheinfo *llc;
60cc1cfc47SSudeep Holla
61*b3fce429SRicardo Neri if (!cache_leaves(cpu) || !per_cpu_cacheinfo(cpu))
62cc1cfc47SSudeep Holla return false;
63cc1cfc47SSudeep Holla
64cc1cfc47SSudeep Holla llc = per_cpu_cacheinfo_idx(cpu, cache_leaves(cpu) - 1);
65cc1cfc47SSudeep Holla
66f16d1becSSudeep Holla return (llc->attributes & CACHE_ID) || !!llc->fw_token;
67f16d1becSSudeep Holla
68cc1cfc47SSudeep Holla }
69cc1cfc47SSudeep Holla
last_level_cache_is_shared(unsigned int cpu_x,unsigned int cpu_y)70cc1cfc47SSudeep Holla bool last_level_cache_is_shared(unsigned int cpu_x, unsigned int cpu_y)
71cc1cfc47SSudeep Holla {
72cc1cfc47SSudeep Holla struct cacheinfo *llc_x, *llc_y;
73cc1cfc47SSudeep Holla
74cc1cfc47SSudeep Holla if (!last_level_cache_is_valid(cpu_x) ||
75cc1cfc47SSudeep Holla !last_level_cache_is_valid(cpu_y))
76cc1cfc47SSudeep Holla return false;
77cc1cfc47SSudeep Holla
78cc1cfc47SSudeep Holla llc_x = per_cpu_cacheinfo_idx(cpu_x, cache_leaves(cpu_x) - 1);
79cc1cfc47SSudeep Holla llc_y = per_cpu_cacheinfo_idx(cpu_y, cache_leaves(cpu_y) - 1);
80cc1cfc47SSudeep Holla
81cc1cfc47SSudeep Holla return cache_leaves_are_shared(llc_x, llc_y);
82cc1cfc47SSudeep Holla }
83cc1cfc47SSudeep Holla
849447eb0fSSudeep Holla #ifdef CONFIG_OF
85cde0fbffSPierre Gondois
86cde0fbffSPierre Gondois static bool of_check_cache_nodes(struct device_node *np);
87cde0fbffSPierre Gondois
88dfea747dSSudeep Holla /* OF properties to query for a given cache type */
89dfea747dSSudeep Holla struct cache_type_info {
90dfea747dSSudeep Holla const char *size_prop;
91dfea747dSSudeep Holla const char *line_size_props[2];
92dfea747dSSudeep Holla const char *nr_sets_prop;
93dfea747dSSudeep Holla };
94dfea747dSSudeep Holla
95dfea747dSSudeep Holla static const struct cache_type_info cache_type_info[] = {
96dfea747dSSudeep Holla {
97dfea747dSSudeep Holla .size_prop = "cache-size",
98dfea747dSSudeep Holla .line_size_props = { "cache-line-size",
99dfea747dSSudeep Holla "cache-block-size", },
100dfea747dSSudeep Holla .nr_sets_prop = "cache-sets",
101dfea747dSSudeep Holla }, {
102dfea747dSSudeep Holla .size_prop = "i-cache-size",
103dfea747dSSudeep Holla .line_size_props = { "i-cache-line-size",
104dfea747dSSudeep Holla "i-cache-block-size", },
105dfea747dSSudeep Holla .nr_sets_prop = "i-cache-sets",
106dfea747dSSudeep Holla }, {
107dfea747dSSudeep Holla .size_prop = "d-cache-size",
108dfea747dSSudeep Holla .line_size_props = { "d-cache-line-size",
109dfea747dSSudeep Holla "d-cache-block-size", },
110dfea747dSSudeep Holla .nr_sets_prop = "d-cache-sets",
111dfea747dSSudeep Holla },
112dfea747dSSudeep Holla };
113dfea747dSSudeep Holla
get_cacheinfo_idx(enum cache_type type)114dfea747dSSudeep Holla static inline int get_cacheinfo_idx(enum cache_type type)
115dfea747dSSudeep Holla {
116dfea747dSSudeep Holla if (type == CACHE_TYPE_UNIFIED)
117dfea747dSSudeep Holla return 0;
118dfea747dSSudeep Holla return type;
119dfea747dSSudeep Holla }
120dfea747dSSudeep Holla
cache_size(struct cacheinfo * this_leaf,struct device_node * np)1212ff075c7SJeremy Linton static void cache_size(struct cacheinfo *this_leaf, struct device_node *np)
122dfea747dSSudeep Holla {
123dfea747dSSudeep Holla const char *propname;
124dfea747dSSudeep Holla int ct_idx;
125dfea747dSSudeep Holla
126dfea747dSSudeep Holla ct_idx = get_cacheinfo_idx(this_leaf->type);
127dfea747dSSudeep Holla propname = cache_type_info[ct_idx].size_prop;
128dfea747dSSudeep Holla
1293a34c986SHuacai Chen of_property_read_u32(np, propname, &this_leaf->size);
130dfea747dSSudeep Holla }
131dfea747dSSudeep Holla
132dfea747dSSudeep Holla /* 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)1332ff075c7SJeremy Linton static void cache_get_line_size(struct cacheinfo *this_leaf,
1342ff075c7SJeremy Linton struct device_node *np)
135dfea747dSSudeep Holla {
136dfea747dSSudeep Holla int i, lim, ct_idx;
137dfea747dSSudeep Holla
138dfea747dSSudeep Holla ct_idx = get_cacheinfo_idx(this_leaf->type);
139dfea747dSSudeep Holla lim = ARRAY_SIZE(cache_type_info[ct_idx].line_size_props);
140dfea747dSSudeep Holla
141dfea747dSSudeep Holla for (i = 0; i < lim; i++) {
142448a5a55SSudeep Holla int ret;
143448a5a55SSudeep Holla u32 line_size;
144dfea747dSSudeep Holla const char *propname;
145dfea747dSSudeep Holla
146dfea747dSSudeep Holla propname = cache_type_info[ct_idx].line_size_props[i];
147448a5a55SSudeep Holla ret = of_property_read_u32(np, propname, &line_size);
148448a5a55SSudeep Holla if (!ret) {
149448a5a55SSudeep Holla this_leaf->coherency_line_size = line_size;
150dfea747dSSudeep Holla break;
151dfea747dSSudeep Holla }
152448a5a55SSudeep Holla }
153dfea747dSSudeep Holla }
154dfea747dSSudeep Holla
cache_nr_sets(struct cacheinfo * this_leaf,struct device_node * np)1552ff075c7SJeremy Linton static void cache_nr_sets(struct cacheinfo *this_leaf, struct device_node *np)
156dfea747dSSudeep Holla {
157dfea747dSSudeep Holla const char *propname;
158dfea747dSSudeep Holla int ct_idx;
159dfea747dSSudeep Holla
160dfea747dSSudeep Holla ct_idx = get_cacheinfo_idx(this_leaf->type);
161dfea747dSSudeep Holla propname = cache_type_info[ct_idx].nr_sets_prop;
162dfea747dSSudeep Holla
1633a34c986SHuacai Chen of_property_read_u32(np, propname, &this_leaf->number_of_sets);
164dfea747dSSudeep Holla }
165dfea747dSSudeep Holla
cache_associativity(struct cacheinfo * this_leaf)166dfea747dSSudeep Holla static void cache_associativity(struct cacheinfo *this_leaf)
167dfea747dSSudeep Holla {
168dfea747dSSudeep Holla unsigned int line_size = this_leaf->coherency_line_size;
169dfea747dSSudeep Holla unsigned int nr_sets = this_leaf->number_of_sets;
170dfea747dSSudeep Holla unsigned int size = this_leaf->size;
171dfea747dSSudeep Holla
172dfea747dSSudeep Holla /*
173dfea747dSSudeep Holla * If the cache is fully associative, there is no need to
174dfea747dSSudeep Holla * check the other properties.
175dfea747dSSudeep Holla */
176dfea747dSSudeep Holla if (!(nr_sets == 1) && (nr_sets > 0 && size > 0 && line_size > 0))
177dfea747dSSudeep Holla this_leaf->ways_of_associativity = (size / nr_sets) / line_size;
178dfea747dSSudeep Holla }
179dfea747dSSudeep Holla
cache_node_is_unified(struct cacheinfo * this_leaf,struct device_node * np)1802ff075c7SJeremy Linton static bool cache_node_is_unified(struct cacheinfo *this_leaf,
1812ff075c7SJeremy Linton struct device_node *np)
182f57ab9a0SSudeep Holla {
1832ff075c7SJeremy Linton return of_property_read_bool(np, "cache-unified");
184f57ab9a0SSudeep Holla }
185f57ab9a0SSudeep Holla
cache_of_set_props(struct cacheinfo * this_leaf,struct device_node * np)1862ff075c7SJeremy Linton static void cache_of_set_props(struct cacheinfo *this_leaf,
1872ff075c7SJeremy Linton struct device_node *np)
188dfea747dSSudeep Holla {
189f57ab9a0SSudeep Holla /*
190f57ab9a0SSudeep Holla * init_cache_level must setup the cache level correctly
191f57ab9a0SSudeep Holla * overriding the architecturally specified levels, so
192f57ab9a0SSudeep Holla * if type is NONE at this stage, it should be unified
193f57ab9a0SSudeep Holla */
194f57ab9a0SSudeep Holla if (this_leaf->type == CACHE_TYPE_NOCACHE &&
1952ff075c7SJeremy Linton cache_node_is_unified(this_leaf, np))
196f57ab9a0SSudeep Holla this_leaf->type = CACHE_TYPE_UNIFIED;
1972ff075c7SJeremy Linton cache_size(this_leaf, np);
1982ff075c7SJeremy Linton cache_get_line_size(this_leaf, np);
1992ff075c7SJeremy Linton cache_nr_sets(this_leaf, np);
200dfea747dSSudeep Holla cache_associativity(this_leaf);
201dfea747dSSudeep Holla }
202d529a18aSJeremy Linton
cache_setup_of_node(unsigned int cpu)203d529a18aSJeremy Linton static int cache_setup_of_node(unsigned int cpu)
204d529a18aSJeremy Linton {
205d529a18aSJeremy Linton struct cacheinfo *this_leaf;
206d529a18aSJeremy Linton unsigned int index = 0;
207d529a18aSJeremy Linton
2081b48fbbcSVincenzo Mezzela struct device_node *np __free(device_node) = of_cpu_device_node_get(cpu);
209d529a18aSJeremy Linton if (!np) {
210d529a18aSJeremy Linton pr_err("Failed to find cpu%d device node\n", cpu);
211d529a18aSJeremy Linton return -ENOENT;
212d529a18aSJeremy Linton }
213d529a18aSJeremy Linton
214cde0fbffSPierre Gondois if (!of_check_cache_nodes(np)) {
215cde0fbffSPierre Gondois return -ENOENT;
216cde0fbffSPierre Gondois }
217cde0fbffSPierre Gondois
218d529a18aSJeremy Linton while (index < cache_leaves(cpu)) {
219b14e8d21SSudeep Holla this_leaf = per_cpu_cacheinfo_idx(cpu, index);
2203da72e18SPierre Gondois if (this_leaf->level != 1) {
2211b48fbbcSVincenzo Mezzela struct device_node *prev __free(device_node) = np;
222d529a18aSJeremy Linton np = of_find_next_cache_node(np);
223d529a18aSJeremy Linton if (!np)
224d529a18aSJeremy Linton break;
2253da72e18SPierre Gondois }
2262ff075c7SJeremy Linton cache_of_set_props(this_leaf, np);
2279b97387cSJeremy Linton this_leaf->fw_token = np;
228d529a18aSJeremy Linton index++;
229d529a18aSJeremy Linton }
230d529a18aSJeremy Linton
231d529a18aSJeremy Linton if (index != cache_leaves(cpu)) /* not all OF nodes populated */
232d529a18aSJeremy Linton return -ENOENT;
233d529a18aSJeremy Linton
234d529a18aSJeremy Linton return 0;
235d529a18aSJeremy Linton }
236c3719bd9SPierre Gondois
of_check_cache_nodes(struct device_node * np)237cde0fbffSPierre Gondois static bool of_check_cache_nodes(struct device_node *np)
238cde0fbffSPierre Gondois {
239cde0fbffSPierre Gondois if (of_property_present(np, "cache-size") ||
240cde0fbffSPierre Gondois of_property_present(np, "i-cache-size") ||
241cde0fbffSPierre Gondois of_property_present(np, "d-cache-size") ||
242cde0fbffSPierre Gondois of_property_present(np, "cache-unified"))
243cde0fbffSPierre Gondois return true;
244cde0fbffSPierre Gondois
2451b48fbbcSVincenzo Mezzela struct device_node *next __free(device_node) = of_find_next_cache_node(np);
246cde0fbffSPierre Gondois if (next) {
247cde0fbffSPierre Gondois return true;
248cde0fbffSPierre Gondois }
249cde0fbffSPierre Gondois
250cde0fbffSPierre Gondois return false;
251cde0fbffSPierre Gondois }
252cde0fbffSPierre Gondois
of_count_cache_leaves(struct device_node * np)253de0df442SPierre Gondois static int of_count_cache_leaves(struct device_node *np)
254c3719bd9SPierre Gondois {
255de0df442SPierre Gondois unsigned int leaves = 0;
256c3719bd9SPierre Gondois
2572d038efcSRob Herring (Arm) if (of_property_present(np, "cache-size"))
258c3719bd9SPierre Gondois ++leaves;
2592d038efcSRob Herring (Arm) if (of_property_present(np, "i-cache-size"))
260c3719bd9SPierre Gondois ++leaves;
2612d038efcSRob Herring (Arm) if (of_property_present(np, "d-cache-size"))
262c3719bd9SPierre Gondois ++leaves;
263de0df442SPierre Gondois
264de0df442SPierre Gondois if (!leaves) {
265de0df442SPierre Gondois /* The '[i-|d-|]cache-size' property is required, but
266de0df442SPierre Gondois * if absent, fallback on the 'cache-unified' property.
267de0df442SPierre Gondois */
268de0df442SPierre Gondois if (of_property_read_bool(np, "cache-unified"))
269de0df442SPierre Gondois return 1;
270de0df442SPierre Gondois else
271de0df442SPierre Gondois return 2;
272de0df442SPierre Gondois }
273de0df442SPierre Gondois
274de0df442SPierre Gondois return leaves;
275de0df442SPierre Gondois }
276de0df442SPierre Gondois
init_of_cache_level(unsigned int cpu)277de0df442SPierre Gondois int init_of_cache_level(unsigned int cpu)
278de0df442SPierre Gondois {
279de0df442SPierre Gondois struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
2801b48fbbcSVincenzo Mezzela struct device_node *np __free(device_node) = of_cpu_device_node_get(cpu);
281de0df442SPierre Gondois unsigned int levels = 0, leaves, level;
282de0df442SPierre Gondois
283cde0fbffSPierre Gondois if (!of_check_cache_nodes(np)) {
284cde0fbffSPierre Gondois return -ENOENT;
285cde0fbffSPierre Gondois }
286cde0fbffSPierre Gondois
287de0df442SPierre Gondois leaves = of_count_cache_leaves(np);
288c3719bd9SPierre Gondois if (leaves > 0)
289c3719bd9SPierre Gondois levels = 1;
290c3719bd9SPierre Gondois
2911b48fbbcSVincenzo Mezzela while (1) {
2921b48fbbcSVincenzo Mezzela struct device_node *prev __free(device_node) = np;
2931b48fbbcSVincenzo Mezzela np = of_find_next_cache_node(np);
2941b48fbbcSVincenzo Mezzela if (!np)
2951b48fbbcSVincenzo Mezzela break;
2961b48fbbcSVincenzo Mezzela
297c3719bd9SPierre Gondois if (!of_device_is_compatible(np, "cache"))
2981b48fbbcSVincenzo Mezzela return -EINVAL;
299c3719bd9SPierre Gondois if (of_property_read_u32(np, "cache-level", &level))
3001b48fbbcSVincenzo Mezzela return -EINVAL;
301c3719bd9SPierre Gondois if (level <= levels)
3021b48fbbcSVincenzo Mezzela return -EINVAL;
303de0df442SPierre Gondois
304de0df442SPierre Gondois leaves += of_count_cache_leaves(np);
305c3719bd9SPierre Gondois levels = level;
306c3719bd9SPierre Gondois }
307c3719bd9SPierre Gondois
308c3719bd9SPierre Gondois this_cpu_ci->num_levels = levels;
309c3719bd9SPierre Gondois this_cpu_ci->num_leaves = leaves;
310c3719bd9SPierre Gondois
311c3719bd9SPierre Gondois return 0;
312c3719bd9SPierre Gondois }
313c3719bd9SPierre Gondois
314246246cbSSudeep Holla #else
cache_setup_of_node(unsigned int cpu)315246246cbSSudeep Holla static inline int cache_setup_of_node(unsigned int cpu) { return 0; }
init_of_cache_level(unsigned int cpu)316c3719bd9SPierre Gondois int init_of_cache_level(unsigned int cpu) { return 0; }
317246246cbSSudeep Holla #endif
318246246cbSSudeep Holla
cache_setup_acpi(unsigned int cpu)319582b468bSJeremy Linton int __weak cache_setup_acpi(unsigned int cpu)
320582b468bSJeremy Linton {
321582b468bSJeremy Linton return -ENOTSUPP;
322582b468bSJeremy Linton }
323582b468bSJeremy Linton
3249a83c84cSShaokun Zhang unsigned int coherency_max_size;
3259a83c84cSShaokun Zhang
cache_setup_properties(unsigned int cpu)32636bbc5b4SSudeep Holla static int cache_setup_properties(unsigned int cpu)
32736bbc5b4SSudeep Holla {
32836bbc5b4SSudeep Holla int ret = 0;
32936bbc5b4SSudeep Holla
33036bbc5b4SSudeep Holla if (of_have_populated_dt())
33136bbc5b4SSudeep Holla ret = cache_setup_of_node(cpu);
33236bbc5b4SSudeep Holla else if (!acpi_disabled)
33336bbc5b4SSudeep Holla ret = cache_setup_acpi(cpu);
33436bbc5b4SSudeep Holla
335ef9f643aSPierre Gondois // Assume there is no cache information available in DT/ACPI from now.
336ef9f643aSPierre Gondois if (ret && use_arch_cache_info())
337ef9f643aSPierre Gondois use_arch_info = true;
338ef9f643aSPierre Gondois
33936bbc5b4SSudeep Holla return ret;
34036bbc5b4SSudeep Holla }
34136bbc5b4SSudeep Holla
cache_shared_cpu_map_setup(unsigned int cpu)342246246cbSSudeep Holla static int cache_shared_cpu_map_setup(unsigned int cpu)
343246246cbSSudeep Holla {
344246246cbSSudeep Holla struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
345246246cbSSudeep Holla struct cacheinfo *this_leaf, *sib_leaf;
346198102c9SYong-Xuan Wang unsigned int index, sib_index;
34755877ef4SSudeep Holla int ret = 0;
348246246cbSSudeep Holla
349fac51482SSudeep Holla if (this_cpu_ci->cpu_map_populated)
350fac51482SSudeep Holla return 0;
351fac51482SSudeep Holla
35236bbc5b4SSudeep Holla /*
35336bbc5b4SSudeep Holla * skip setting up cache properties if LLC is valid, just need
35436bbc5b4SSudeep Holla * to update the shared cpu_map if the cache attributes were
35536bbc5b4SSudeep Holla * populated early before all the cpus are brought online
35636bbc5b4SSudeep Holla */
357ef9f643aSPierre Gondois if (!last_level_cache_is_valid(cpu) && !use_arch_info) {
35836bbc5b4SSudeep Holla ret = cache_setup_properties(cpu);
359246246cbSSudeep Holla if (ret)
360246246cbSSudeep Holla return ret;
36136bbc5b4SSudeep Holla }
362246246cbSSudeep Holla
363246246cbSSudeep Holla for (index = 0; index < cache_leaves(cpu); index++) {
364246246cbSSudeep Holla unsigned int i;
365246246cbSSudeep Holla
366b14e8d21SSudeep Holla this_leaf = per_cpu_cacheinfo_idx(cpu, index);
367246246cbSSudeep Holla
368246246cbSSudeep Holla cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
369246246cbSSudeep Holla for_each_online_cpu(i) {
370f87f132cSNikolay Borisov if (i == cpu || !per_cpu_cacheinfo(i))
371246246cbSSudeep Holla continue;/* skip if itself or no cacheinfo */
372198102c9SYong-Xuan Wang for (sib_index = 0; sib_index < cache_leaves(i); sib_index++) {
373198102c9SYong-Xuan Wang sib_leaf = per_cpu_cacheinfo_idx(i, sib_index);
374126310c9SK Prateek Nayak
375126310c9SK Prateek Nayak /*
376126310c9SK Prateek Nayak * Comparing cache IDs only makes sense if the leaves
377126310c9SK Prateek Nayak * belong to the same cache level of same type. Skip
378126310c9SK Prateek Nayak * the check if level and type do not match.
379126310c9SK Prateek Nayak */
380126310c9SK Prateek Nayak if (sib_leaf->level != this_leaf->level ||
381126310c9SK Prateek Nayak sib_leaf->type != this_leaf->type)
382126310c9SK Prateek Nayak continue;
383126310c9SK Prateek Nayak
384246246cbSSudeep Holla if (cache_leaves_are_shared(this_leaf, sib_leaf)) {
385246246cbSSudeep Holla cpumask_set_cpu(cpu, &sib_leaf->shared_cpu_map);
386246246cbSSudeep Holla cpumask_set_cpu(i, &this_leaf->shared_cpu_map);
387198102c9SYong-Xuan Wang break;
388198102c9SYong-Xuan Wang }
389246246cbSSudeep Holla }
390246246cbSSudeep Holla }
3919a83c84cSShaokun Zhang /* record the maximum cache line size */
3929a83c84cSShaokun Zhang if (this_leaf->coherency_line_size > coherency_max_size)
3939a83c84cSShaokun Zhang coherency_max_size = this_leaf->coherency_line_size;
394246246cbSSudeep Holla }
395246246cbSSudeep Holla
396c26fabe7SK Prateek Nayak /* shared_cpu_map is now populated for the cpu */
397c26fabe7SK Prateek Nayak this_cpu_ci->cpu_map_populated = true;
398246246cbSSudeep Holla return 0;
399246246cbSSudeep Holla }
400246246cbSSudeep Holla
cache_shared_cpu_map_remove(unsigned int cpu)401246246cbSSudeep Holla static void cache_shared_cpu_map_remove(unsigned int cpu)
402246246cbSSudeep Holla {
403c26fabe7SK Prateek Nayak struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
404246246cbSSudeep Holla struct cacheinfo *this_leaf, *sib_leaf;
405198102c9SYong-Xuan Wang unsigned int sibling, index, sib_index;
406246246cbSSudeep Holla
407246246cbSSudeep Holla for (index = 0; index < cache_leaves(cpu); index++) {
408b14e8d21SSudeep Holla this_leaf = per_cpu_cacheinfo_idx(cpu, index);
409246246cbSSudeep Holla for_each_cpu(sibling, &this_leaf->shared_cpu_map) {
410f87f132cSNikolay Borisov if (sibling == cpu || !per_cpu_cacheinfo(sibling))
41152110313SSudeep Holla continue;/* skip if itself or no cacheinfo */
4122110d70cSBorislav Petkov
413198102c9SYong-Xuan Wang for (sib_index = 0; sib_index < cache_leaves(sibling); sib_index++) {
414198102c9SYong-Xuan Wang sib_leaf = per_cpu_cacheinfo_idx(sibling, sib_index);
415126310c9SK Prateek Nayak
416126310c9SK Prateek Nayak /*
417126310c9SK Prateek Nayak * Comparing cache IDs only makes sense if the leaves
418126310c9SK Prateek Nayak * belong to the same cache level of same type. Skip
419126310c9SK Prateek Nayak * the check if level and type do not match.
420126310c9SK Prateek Nayak */
421126310c9SK Prateek Nayak if (sib_leaf->level != this_leaf->level ||
422126310c9SK Prateek Nayak sib_leaf->type != this_leaf->type)
423126310c9SK Prateek Nayak continue;
424126310c9SK Prateek Nayak
425198102c9SYong-Xuan Wang if (cache_leaves_are_shared(this_leaf, sib_leaf)) {
426246246cbSSudeep Holla cpumask_clear_cpu(cpu, &sib_leaf->shared_cpu_map);
427246246cbSSudeep Holla cpumask_clear_cpu(sibling, &this_leaf->shared_cpu_map);
428198102c9SYong-Xuan Wang break;
429198102c9SYong-Xuan Wang }
430198102c9SYong-Xuan Wang }
431246246cbSSudeep Holla }
432246246cbSSudeep Holla }
433c26fabe7SK Prateek Nayak
434c26fabe7SK Prateek Nayak /* cpu is no longer populated in the shared map */
435c26fabe7SK Prateek Nayak this_cpu_ci->cpu_map_populated = false;
436246246cbSSudeep Holla }
437246246cbSSudeep Holla
free_cache_attributes(unsigned int cpu)438246246cbSSudeep Holla static void free_cache_attributes(unsigned int cpu)
439246246cbSSudeep Holla {
4402110d70cSBorislav Petkov if (!per_cpu_cacheinfo(cpu))
4412110d70cSBorislav Petkov return;
4422110d70cSBorislav Petkov
443246246cbSSudeep Holla cache_shared_cpu_map_remove(cpu);
444246246cbSSudeep Holla }
445246246cbSSudeep Holla
early_cache_level(unsigned int cpu)4466539cffaSRadu Rendec int __weak early_cache_level(unsigned int cpu)
4476539cffaSRadu Rendec {
4486539cffaSRadu Rendec return -ENOENT;
4496539cffaSRadu Rendec }
4506539cffaSRadu Rendec
init_cache_level(unsigned int cpu)451246246cbSSudeep Holla int __weak init_cache_level(unsigned int cpu)
452246246cbSSudeep Holla {
453246246cbSSudeep Holla return -ENOENT;
454246246cbSSudeep Holla }
455246246cbSSudeep Holla
populate_cache_leaves(unsigned int cpu)456246246cbSSudeep Holla int __weak populate_cache_leaves(unsigned int cpu)
457246246cbSSudeep Holla {
458246246cbSSudeep Holla return -ENOENT;
459246246cbSSudeep Holla }
460246246cbSSudeep Holla
allocate_cache_info(int cpu)461*b3fce429SRicardo Neri static inline int allocate_cache_info(int cpu)
462246246cbSSudeep Holla {
463*b3fce429SRicardo Neri per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu), sizeof(struct cacheinfo), GFP_ATOMIC);
4645944ce09SPierre Gondois if (!per_cpu_cacheinfo(cpu)) {
46536bbc5b4SSudeep Holla cache_leaves(cpu) = 0;
466246246cbSSudeep Holla return -ENOMEM;
46736bbc5b4SSudeep Holla }
468246246cbSSudeep Holla
4695944ce09SPierre Gondois return 0;
4705944ce09SPierre Gondois }
4715944ce09SPierre Gondois
fetch_cache_info(unsigned int cpu)4725944ce09SPierre Gondois int fetch_cache_info(unsigned int cpu)
4735944ce09SPierre Gondois {
4746539cffaSRadu Rendec struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
475ecaef469SPierre Gondois unsigned int levels = 0, split_levels = 0;
4765944ce09SPierre Gondois int ret;
4775944ce09SPierre Gondois
4785944ce09SPierre Gondois if (acpi_disabled) {
4795944ce09SPierre Gondois ret = init_of_cache_level(cpu);
4805944ce09SPierre Gondois } else {
4815944ce09SPierre Gondois ret = acpi_get_cache_info(cpu, &levels, &split_levels);
4826539cffaSRadu Rendec if (!ret) {
4835944ce09SPierre Gondois this_cpu_ci->num_levels = levels;
4845944ce09SPierre Gondois /*
4855944ce09SPierre Gondois * This assumes that:
4865944ce09SPierre Gondois * - there cannot be any split caches (data/instruction)
4875944ce09SPierre Gondois * above a unified cache
4885944ce09SPierre Gondois * - data/instruction caches come by pair
4895944ce09SPierre Gondois */
4905944ce09SPierre Gondois this_cpu_ci->num_leaves = levels + split_levels;
4915944ce09SPierre Gondois }
4926539cffaSRadu Rendec }
4936539cffaSRadu Rendec
4946539cffaSRadu Rendec if (ret || !cache_leaves(cpu)) {
4956539cffaSRadu Rendec ret = early_cache_level(cpu);
4966539cffaSRadu Rendec if (ret)
4976539cffaSRadu Rendec return ret;
4986539cffaSRadu Rendec
4995944ce09SPierre Gondois if (!cache_leaves(cpu))
5005944ce09SPierre Gondois return -ENOENT;
5015944ce09SPierre Gondois
5026539cffaSRadu Rendec this_cpu_ci->early_ci_levels = true;
5036539cffaSRadu Rendec }
5046539cffaSRadu Rendec
5056539cffaSRadu Rendec return allocate_cache_info(cpu);
5066539cffaSRadu Rendec }
5076539cffaSRadu Rendec
init_level_allocate_ci(unsigned int cpu)5086539cffaSRadu Rendec static inline int init_level_allocate_ci(unsigned int cpu)
5096539cffaSRadu Rendec {
5106539cffaSRadu Rendec unsigned int early_leaves = cache_leaves(cpu);
5116539cffaSRadu Rendec
5126539cffaSRadu Rendec /* Since early initialization/allocation of the cacheinfo is allowed
5136539cffaSRadu Rendec * via fetch_cache_info() and this also gets called as CPU hotplug
5146539cffaSRadu Rendec * callbacks via cacheinfo_cpu_online, the init/alloc can be skipped
5156539cffaSRadu Rendec * as it will happen only once (the cacheinfo memory is never freed).
5166539cffaSRadu Rendec * Just populate the cacheinfo. However, if the cacheinfo has been
5176539cffaSRadu Rendec * allocated early through the arch-specific early_cache_level() call,
5186539cffaSRadu Rendec * there is a chance the info is wrong (this can happen on arm64). In
5196539cffaSRadu Rendec * that case, call init_cache_level() anyway to give the arch-specific
5206539cffaSRadu Rendec * code a chance to make things right.
5216539cffaSRadu Rendec */
5226539cffaSRadu Rendec if (per_cpu_cacheinfo(cpu) && !ci_cacheinfo(cpu)->early_ci_levels)
5236539cffaSRadu Rendec return 0;
5246539cffaSRadu Rendec
5256539cffaSRadu Rendec if (init_cache_level(cpu) || !cache_leaves(cpu))
5266539cffaSRadu Rendec return -ENOENT;
5276539cffaSRadu Rendec
5286539cffaSRadu Rendec /*
5296539cffaSRadu Rendec * Now that we have properly initialized the cache level info, make
5306539cffaSRadu Rendec * sure we don't try to do that again the next time we are called
5316539cffaSRadu Rendec * (e.g. as CPU hotplug callbacks).
5326539cffaSRadu Rendec */
5336539cffaSRadu Rendec ci_cacheinfo(cpu)->early_ci_levels = false;
5346539cffaSRadu Rendec
535*b3fce429SRicardo Neri /*
536*b3fce429SRicardo Neri * Some architectures (e.g., x86) do not use early initialization.
537*b3fce429SRicardo Neri * Allocate memory now in such case.
538*b3fce429SRicardo Neri */
539*b3fce429SRicardo Neri if (cache_leaves(cpu) <= early_leaves && per_cpu_cacheinfo(cpu))
5406539cffaSRadu Rendec return 0;
5416539cffaSRadu Rendec
5426539cffaSRadu Rendec kfree(per_cpu_cacheinfo(cpu));
5435944ce09SPierre Gondois return allocate_cache_info(cpu);
5445944ce09SPierre Gondois }
5455944ce09SPierre Gondois
detect_cache_attributes(unsigned int cpu)5465944ce09SPierre Gondois int detect_cache_attributes(unsigned int cpu)
5475944ce09SPierre Gondois {
5485944ce09SPierre Gondois int ret;
5495944ce09SPierre Gondois
5506539cffaSRadu Rendec ret = init_level_allocate_ci(cpu);
5515944ce09SPierre Gondois if (ret)
5525944ce09SPierre Gondois return ret;
5535944ce09SPierre Gondois
5542ff075c7SJeremy Linton /*
5555c271238SYicong Yang * If LLC is valid the cache leaves were already populated so just go to
5565c271238SYicong Yang * update the cpu map.
5575c271238SYicong Yang */
5585c271238SYicong Yang if (!last_level_cache_is_valid(cpu)) {
5595c271238SYicong Yang /*
5602ff075c7SJeremy Linton * populate_cache_leaves() may completely setup the cache leaves and
5612ff075c7SJeremy Linton * shared_cpu_map or it may leave it partially setup.
5622ff075c7SJeremy Linton */
563246246cbSSudeep Holla ret = populate_cache_leaves(cpu);
564246246cbSSudeep Holla if (ret)
565246246cbSSudeep Holla goto free_ci;
5665c271238SYicong Yang }
56736bbc5b4SSudeep Holla
568246246cbSSudeep Holla /*
5699b97387cSJeremy Linton * For systems using DT for cache hierarchy, fw_token
5709b97387cSJeremy Linton * and shared_cpu_map will be set up here only if they are
5719b97387cSJeremy Linton * not populated already
572246246cbSSudeep Holla */
573246246cbSSudeep Holla ret = cache_shared_cpu_map_setup(cpu);
5748a7d95f9SSudeep Holla if (ret) {
57555877ef4SSudeep Holla pr_warn("Unable to detect cache hierarchy for CPU %d\n", cpu);
576246246cbSSudeep Holla goto free_ci;
5778a7d95f9SSudeep Holla }
578dfea747dSSudeep Holla
579246246cbSSudeep Holla return 0;
580246246cbSSudeep Holla
581246246cbSSudeep Holla free_ci:
582246246cbSSudeep Holla free_cache_attributes(cpu);
583246246cbSSudeep Holla return ret;
584246246cbSSudeep Holla }
585246246cbSSudeep Holla
586246246cbSSudeep Holla /* pointer to cpuX/cache device */
587246246cbSSudeep Holla static DEFINE_PER_CPU(struct device *, ci_cache_dev);
588246246cbSSudeep Holla #define per_cpu_cache_dev(cpu) (per_cpu(ci_cache_dev, cpu))
589246246cbSSudeep Holla
590246246cbSSudeep Holla static cpumask_t cache_dev_map;
591246246cbSSudeep Holla
592246246cbSSudeep Holla /* pointer to array of devices for cpuX/cache/indexY */
593246246cbSSudeep Holla static DEFINE_PER_CPU(struct device **, ci_index_dev);
594246246cbSSudeep Holla #define per_cpu_index_dev(cpu) (per_cpu(ci_index_dev, cpu))
595246246cbSSudeep Holla #define per_cache_index_dev(cpu, idx) ((per_cpu_index_dev(cpu))[idx])
596246246cbSSudeep Holla
597246246cbSSudeep Holla #define show_one(file_name, object) \
598246246cbSSudeep Holla static ssize_t file_name##_show(struct device *dev, \
599246246cbSSudeep Holla struct device_attribute *attr, char *buf) \
600246246cbSSudeep Holla { \
601246246cbSSudeep Holla struct cacheinfo *this_leaf = dev_get_drvdata(dev); \
602948b3edbSJoe Perches return sysfs_emit(buf, "%u\n", this_leaf->object); \
603246246cbSSudeep Holla }
604246246cbSSudeep Holla
605e9a2ea5aSFenghua Yu show_one(id, id);
606246246cbSSudeep Holla show_one(level, level);
607246246cbSSudeep Holla show_one(coherency_line_size, coherency_line_size);
608246246cbSSudeep Holla show_one(number_of_sets, number_of_sets);
609246246cbSSudeep Holla show_one(physical_line_partition, physical_line_partition);
610246246cbSSudeep Holla show_one(ways_of_associativity, ways_of_associativity);
611246246cbSSudeep Holla
size_show(struct device * dev,struct device_attribute * attr,char * buf)612246246cbSSudeep Holla static ssize_t size_show(struct device *dev,
613246246cbSSudeep Holla struct device_attribute *attr, char *buf)
614246246cbSSudeep Holla {
615246246cbSSudeep Holla struct cacheinfo *this_leaf = dev_get_drvdata(dev);
616246246cbSSudeep Holla
617aa838896SJoe Perches return sysfs_emit(buf, "%uK\n", this_leaf->size >> 10);
618246246cbSSudeep Holla }
619246246cbSSudeep Holla
shared_cpu_map_show(struct device * dev,struct device_attribute * attr,char * buf)620e015e036SJoe Perches static ssize_t shared_cpu_map_show(struct device *dev,
621e015e036SJoe Perches struct device_attribute *attr, char *buf)
622246246cbSSudeep Holla {
623246246cbSSudeep Holla struct cacheinfo *this_leaf = dev_get_drvdata(dev);
624246246cbSSudeep Holla const struct cpumask *mask = &this_leaf->shared_cpu_map;
625246246cbSSudeep Holla
626e015e036SJoe Perches return sysfs_emit(buf, "%*pb\n", nr_cpu_ids, mask);
627246246cbSSudeep Holla }
628246246cbSSudeep Holla
shared_cpu_list_show(struct device * dev,struct device_attribute * attr,char * buf)629246246cbSSudeep Holla static ssize_t shared_cpu_list_show(struct device *dev,
630246246cbSSudeep Holla struct device_attribute *attr, char *buf)
631246246cbSSudeep Holla {
632e015e036SJoe Perches struct cacheinfo *this_leaf = dev_get_drvdata(dev);
633e015e036SJoe Perches const struct cpumask *mask = &this_leaf->shared_cpu_map;
634e015e036SJoe Perches
635e015e036SJoe Perches return sysfs_emit(buf, "%*pbl\n", nr_cpu_ids, mask);
636246246cbSSudeep Holla }
637246246cbSSudeep Holla
type_show(struct device * dev,struct device_attribute * attr,char * buf)638246246cbSSudeep Holla static ssize_t type_show(struct device *dev,
639246246cbSSudeep Holla struct device_attribute *attr, char *buf)
640246246cbSSudeep Holla {
641246246cbSSudeep Holla struct cacheinfo *this_leaf = dev_get_drvdata(dev);
642973c3911SJoe Perches const char *output;
643246246cbSSudeep Holla
644246246cbSSudeep Holla switch (this_leaf->type) {
645246246cbSSudeep Holla case CACHE_TYPE_DATA:
646973c3911SJoe Perches output = "Data";
647973c3911SJoe Perches break;
648246246cbSSudeep Holla case CACHE_TYPE_INST:
649973c3911SJoe Perches output = "Instruction";
650973c3911SJoe Perches break;
651246246cbSSudeep Holla case CACHE_TYPE_UNIFIED:
652973c3911SJoe Perches output = "Unified";
653973c3911SJoe Perches break;
654246246cbSSudeep Holla default:
655246246cbSSudeep Holla return -EINVAL;
656246246cbSSudeep Holla }
657973c3911SJoe Perches
658973c3911SJoe Perches return sysfs_emit(buf, "%s\n", output);
659246246cbSSudeep Holla }
660246246cbSSudeep Holla
allocation_policy_show(struct device * dev,struct device_attribute * attr,char * buf)661246246cbSSudeep Holla static ssize_t allocation_policy_show(struct device *dev,
662246246cbSSudeep Holla struct device_attribute *attr, char *buf)
663246246cbSSudeep Holla {
664246246cbSSudeep Holla struct cacheinfo *this_leaf = dev_get_drvdata(dev);
665246246cbSSudeep Holla unsigned int ci_attr = this_leaf->attributes;
666973c3911SJoe Perches const char *output;
667246246cbSSudeep Holla
668246246cbSSudeep Holla if ((ci_attr & CACHE_READ_ALLOCATE) && (ci_attr & CACHE_WRITE_ALLOCATE))
669973c3911SJoe Perches output = "ReadWriteAllocate";
670246246cbSSudeep Holla else if (ci_attr & CACHE_READ_ALLOCATE)
671973c3911SJoe Perches output = "ReadAllocate";
672246246cbSSudeep Holla else if (ci_attr & CACHE_WRITE_ALLOCATE)
673973c3911SJoe Perches output = "WriteAllocate";
674973c3911SJoe Perches else
675973c3911SJoe Perches return 0;
676973c3911SJoe Perches
677973c3911SJoe Perches return sysfs_emit(buf, "%s\n", output);
678246246cbSSudeep Holla }
679246246cbSSudeep Holla
write_policy_show(struct device * dev,struct device_attribute * attr,char * buf)680246246cbSSudeep Holla static ssize_t write_policy_show(struct device *dev,
681246246cbSSudeep Holla struct device_attribute *attr, char *buf)
682246246cbSSudeep Holla {
683246246cbSSudeep Holla struct cacheinfo *this_leaf = dev_get_drvdata(dev);
684246246cbSSudeep Holla unsigned int ci_attr = this_leaf->attributes;
685246246cbSSudeep Holla int n = 0;
686246246cbSSudeep Holla
687246246cbSSudeep Holla if (ci_attr & CACHE_WRITE_THROUGH)
688aa838896SJoe Perches n = sysfs_emit(buf, "WriteThrough\n");
689246246cbSSudeep Holla else if (ci_attr & CACHE_WRITE_BACK)
690aa838896SJoe Perches n = sysfs_emit(buf, "WriteBack\n");
691246246cbSSudeep Holla return n;
692246246cbSSudeep Holla }
693246246cbSSudeep Holla
694e9a2ea5aSFenghua Yu static DEVICE_ATTR_RO(id);
695246246cbSSudeep Holla static DEVICE_ATTR_RO(level);
696246246cbSSudeep Holla static DEVICE_ATTR_RO(type);
697246246cbSSudeep Holla static DEVICE_ATTR_RO(coherency_line_size);
698246246cbSSudeep Holla static DEVICE_ATTR_RO(ways_of_associativity);
699246246cbSSudeep Holla static DEVICE_ATTR_RO(number_of_sets);
700246246cbSSudeep Holla static DEVICE_ATTR_RO(size);
701246246cbSSudeep Holla static DEVICE_ATTR_RO(allocation_policy);
702246246cbSSudeep Holla static DEVICE_ATTR_RO(write_policy);
703246246cbSSudeep Holla static DEVICE_ATTR_RO(shared_cpu_map);
704246246cbSSudeep Holla static DEVICE_ATTR_RO(shared_cpu_list);
705246246cbSSudeep Holla static DEVICE_ATTR_RO(physical_line_partition);
706246246cbSSudeep Holla
707246246cbSSudeep Holla static struct attribute *cache_default_attrs[] = {
708e9a2ea5aSFenghua Yu &dev_attr_id.attr,
709246246cbSSudeep Holla &dev_attr_type.attr,
710246246cbSSudeep Holla &dev_attr_level.attr,
711246246cbSSudeep Holla &dev_attr_shared_cpu_map.attr,
712246246cbSSudeep Holla &dev_attr_shared_cpu_list.attr,
713246246cbSSudeep Holla &dev_attr_coherency_line_size.attr,
714246246cbSSudeep Holla &dev_attr_ways_of_associativity.attr,
715246246cbSSudeep Holla &dev_attr_number_of_sets.attr,
716246246cbSSudeep Holla &dev_attr_size.attr,
717246246cbSSudeep Holla &dev_attr_allocation_policy.attr,
718246246cbSSudeep Holla &dev_attr_write_policy.attr,
719246246cbSSudeep Holla &dev_attr_physical_line_partition.attr,
720246246cbSSudeep Holla NULL
721246246cbSSudeep Holla };
722246246cbSSudeep Holla
723246246cbSSudeep Holla static umode_t
cache_default_attrs_is_visible(struct kobject * kobj,struct attribute * attr,int unused)724246246cbSSudeep Holla cache_default_attrs_is_visible(struct kobject *kobj,
725246246cbSSudeep Holla struct attribute *attr, int unused)
726246246cbSSudeep Holla {
727246246cbSSudeep Holla struct device *dev = kobj_to_dev(kobj);
728246246cbSSudeep Holla struct cacheinfo *this_leaf = dev_get_drvdata(dev);
729246246cbSSudeep Holla const struct cpumask *mask = &this_leaf->shared_cpu_map;
730246246cbSSudeep Holla umode_t mode = attr->mode;
731246246cbSSudeep Holla
732e9a2ea5aSFenghua Yu if ((attr == &dev_attr_id.attr) && (this_leaf->attributes & CACHE_ID))
733e9a2ea5aSFenghua Yu return mode;
734246246cbSSudeep Holla if ((attr == &dev_attr_type.attr) && this_leaf->type)
735246246cbSSudeep Holla return mode;
736246246cbSSudeep Holla if ((attr == &dev_attr_level.attr) && this_leaf->level)
737246246cbSSudeep Holla return mode;
738246246cbSSudeep Holla if ((attr == &dev_attr_shared_cpu_map.attr) && !cpumask_empty(mask))
739246246cbSSudeep Holla return mode;
740246246cbSSudeep Holla if ((attr == &dev_attr_shared_cpu_list.attr) && !cpumask_empty(mask))
741246246cbSSudeep Holla return mode;
742246246cbSSudeep Holla if ((attr == &dev_attr_coherency_line_size.attr) &&
743246246cbSSudeep Holla this_leaf->coherency_line_size)
744246246cbSSudeep Holla return mode;
745246246cbSSudeep Holla if ((attr == &dev_attr_ways_of_associativity.attr) &&
746246246cbSSudeep Holla this_leaf->size) /* allow 0 = full associativity */
747246246cbSSudeep Holla return mode;
748246246cbSSudeep Holla if ((attr == &dev_attr_number_of_sets.attr) &&
749246246cbSSudeep Holla this_leaf->number_of_sets)
750246246cbSSudeep Holla return mode;
751246246cbSSudeep Holla if ((attr == &dev_attr_size.attr) && this_leaf->size)
752246246cbSSudeep Holla return mode;
753246246cbSSudeep Holla if ((attr == &dev_attr_write_policy.attr) &&
754246246cbSSudeep Holla (this_leaf->attributes & CACHE_WRITE_POLICY_MASK))
755246246cbSSudeep Holla return mode;
756246246cbSSudeep Holla if ((attr == &dev_attr_allocation_policy.attr) &&
757246246cbSSudeep Holla (this_leaf->attributes & CACHE_ALLOCATE_POLICY_MASK))
758246246cbSSudeep Holla return mode;
759246246cbSSudeep Holla if ((attr == &dev_attr_physical_line_partition.attr) &&
760246246cbSSudeep Holla this_leaf->physical_line_partition)
761246246cbSSudeep Holla return mode;
762246246cbSSudeep Holla
763246246cbSSudeep Holla return 0;
764246246cbSSudeep Holla }
765246246cbSSudeep Holla
766246246cbSSudeep Holla static const struct attribute_group cache_default_group = {
767246246cbSSudeep Holla .attrs = cache_default_attrs,
768246246cbSSudeep Holla .is_visible = cache_default_attrs_is_visible,
769246246cbSSudeep Holla };
770246246cbSSudeep Holla
771246246cbSSudeep Holla static const struct attribute_group *cache_default_groups[] = {
772246246cbSSudeep Holla &cache_default_group,
773246246cbSSudeep Holla NULL,
774246246cbSSudeep Holla };
775246246cbSSudeep Holla
776246246cbSSudeep Holla static const struct attribute_group *cache_private_groups[] = {
777246246cbSSudeep Holla &cache_default_group,
778246246cbSSudeep Holla NULL, /* Place holder for private group */
779246246cbSSudeep Holla NULL,
780246246cbSSudeep Holla };
781246246cbSSudeep Holla
782246246cbSSudeep Holla const struct attribute_group *
cache_get_priv_group(struct cacheinfo * this_leaf)783246246cbSSudeep Holla __weak cache_get_priv_group(struct cacheinfo *this_leaf)
784246246cbSSudeep Holla {
785246246cbSSudeep Holla return NULL;
786246246cbSSudeep Holla }
787246246cbSSudeep Holla
788246246cbSSudeep Holla static const struct attribute_group **
cache_get_attribute_groups(struct cacheinfo * this_leaf)789246246cbSSudeep Holla cache_get_attribute_groups(struct cacheinfo *this_leaf)
790246246cbSSudeep Holla {
791246246cbSSudeep Holla const struct attribute_group *priv_group =
792246246cbSSudeep Holla cache_get_priv_group(this_leaf);
793246246cbSSudeep Holla
794246246cbSSudeep Holla if (!priv_group)
795246246cbSSudeep Holla return cache_default_groups;
796246246cbSSudeep Holla
797246246cbSSudeep Holla if (!cache_private_groups[1])
798246246cbSSudeep Holla cache_private_groups[1] = priv_group;
799246246cbSSudeep Holla
800246246cbSSudeep Holla return cache_private_groups;
801246246cbSSudeep Holla }
802246246cbSSudeep Holla
803246246cbSSudeep Holla /* Add/Remove cache interface for CPU device */
cpu_cache_sysfs_exit(unsigned int cpu)804246246cbSSudeep Holla static void cpu_cache_sysfs_exit(unsigned int cpu)
805246246cbSSudeep Holla {
806246246cbSSudeep Holla int i;
807246246cbSSudeep Holla struct device *ci_dev;
808246246cbSSudeep Holla
809246246cbSSudeep Holla if (per_cpu_index_dev(cpu)) {
810246246cbSSudeep Holla for (i = 0; i < cache_leaves(cpu); i++) {
811246246cbSSudeep Holla ci_dev = per_cache_index_dev(cpu, i);
812246246cbSSudeep Holla if (!ci_dev)
813246246cbSSudeep Holla continue;
814246246cbSSudeep Holla device_unregister(ci_dev);
815246246cbSSudeep Holla }
816246246cbSSudeep Holla kfree(per_cpu_index_dev(cpu));
817246246cbSSudeep Holla per_cpu_index_dev(cpu) = NULL;
818246246cbSSudeep Holla }
819246246cbSSudeep Holla device_unregister(per_cpu_cache_dev(cpu));
820246246cbSSudeep Holla per_cpu_cache_dev(cpu) = NULL;
821246246cbSSudeep Holla }
822246246cbSSudeep Holla
cpu_cache_sysfs_init(unsigned int cpu)823246246cbSSudeep Holla static int cpu_cache_sysfs_init(unsigned int cpu)
824246246cbSSudeep Holla {
825246246cbSSudeep Holla struct device *dev = get_cpu_device(cpu);
826246246cbSSudeep Holla
827246246cbSSudeep Holla if (per_cpu_cacheinfo(cpu) == NULL)
828246246cbSSudeep Holla return -ENOENT;
829246246cbSSudeep Holla
830246246cbSSudeep Holla per_cpu_cache_dev(cpu) = cpu_device_create(dev, NULL, NULL, "cache");
831246246cbSSudeep Holla if (IS_ERR(per_cpu_cache_dev(cpu)))
832246246cbSSudeep Holla return PTR_ERR(per_cpu_cache_dev(cpu));
833246246cbSSudeep Holla
834246246cbSSudeep Holla /* Allocate all required memory */
835246246cbSSudeep Holla per_cpu_index_dev(cpu) = kcalloc(cache_leaves(cpu),
836246246cbSSudeep Holla sizeof(struct device *), GFP_KERNEL);
837246246cbSSudeep Holla if (unlikely(per_cpu_index_dev(cpu) == NULL))
838246246cbSSudeep Holla goto err_out;
839246246cbSSudeep Holla
840246246cbSSudeep Holla return 0;
841246246cbSSudeep Holla
842246246cbSSudeep Holla err_out:
843246246cbSSudeep Holla cpu_cache_sysfs_exit(cpu);
844246246cbSSudeep Holla return -ENOMEM;
845246246cbSSudeep Holla }
846246246cbSSudeep Holla
cache_add_dev(unsigned int cpu)847246246cbSSudeep Holla static int cache_add_dev(unsigned int cpu)
848246246cbSSudeep Holla {
849246246cbSSudeep Holla unsigned int i;
850246246cbSSudeep Holla int rc;
851246246cbSSudeep Holla struct device *ci_dev, *parent;
852246246cbSSudeep Holla struct cacheinfo *this_leaf;
853246246cbSSudeep Holla const struct attribute_group **cache_groups;
854246246cbSSudeep Holla
855246246cbSSudeep Holla rc = cpu_cache_sysfs_init(cpu);
856246246cbSSudeep Holla if (unlikely(rc < 0))
857246246cbSSudeep Holla return rc;
858246246cbSSudeep Holla
859246246cbSSudeep Holla parent = per_cpu_cache_dev(cpu);
860246246cbSSudeep Holla for (i = 0; i < cache_leaves(cpu); i++) {
861b14e8d21SSudeep Holla this_leaf = per_cpu_cacheinfo_idx(cpu, i);
862246246cbSSudeep Holla if (this_leaf->disable_sysfs)
863246246cbSSudeep Holla continue;
864ca388e43SJeffrey Hugo if (this_leaf->type == CACHE_TYPE_NOCACHE)
865ca388e43SJeffrey Hugo break;
866246246cbSSudeep Holla cache_groups = cache_get_attribute_groups(this_leaf);
867246246cbSSudeep Holla ci_dev = cpu_device_create(parent, this_leaf, cache_groups,
868246246cbSSudeep Holla "index%1u", i);
869246246cbSSudeep Holla if (IS_ERR(ci_dev)) {
870246246cbSSudeep Holla rc = PTR_ERR(ci_dev);
871246246cbSSudeep Holla goto err;
872246246cbSSudeep Holla }
873246246cbSSudeep Holla per_cache_index_dev(cpu, i) = ci_dev;
874246246cbSSudeep Holla }
875246246cbSSudeep Holla cpumask_set_cpu(cpu, &cache_dev_map);
876246246cbSSudeep Holla
877246246cbSSudeep Holla return 0;
878246246cbSSudeep Holla err:
879246246cbSSudeep Holla cpu_cache_sysfs_exit(cpu);
880246246cbSSudeep Holla return rc;
881246246cbSSudeep Holla }
882246246cbSSudeep Holla
cpu_map_shared_cache(bool online,unsigned int cpu,cpumask_t ** map)8835cec4eb7SHuang Ying static unsigned int cpu_map_shared_cache(bool online, unsigned int cpu,
8845cec4eb7SHuang Ying cpumask_t **map)
8855cec4eb7SHuang Ying {
8865cec4eb7SHuang Ying struct cacheinfo *llc, *sib_llc;
8875cec4eb7SHuang Ying unsigned int sibling;
8885cec4eb7SHuang Ying
8895cec4eb7SHuang Ying if (!last_level_cache_is_valid(cpu))
8905cec4eb7SHuang Ying return 0;
8915cec4eb7SHuang Ying
8925cec4eb7SHuang Ying llc = per_cpu_cacheinfo_idx(cpu, cache_leaves(cpu) - 1);
8935cec4eb7SHuang Ying
8945cec4eb7SHuang Ying if (llc->type != CACHE_TYPE_DATA && llc->type != CACHE_TYPE_UNIFIED)
8955cec4eb7SHuang Ying return 0;
8965cec4eb7SHuang Ying
8975cec4eb7SHuang Ying if (online) {
8985cec4eb7SHuang Ying *map = &llc->shared_cpu_map;
8995cec4eb7SHuang Ying return cpumask_weight(*map);
9005cec4eb7SHuang Ying }
9015cec4eb7SHuang Ying
9025cec4eb7SHuang Ying /* shared_cpu_map of offlined CPU will be cleared, so use sibling map */
9035cec4eb7SHuang Ying for_each_cpu(sibling, &llc->shared_cpu_map) {
9045cec4eb7SHuang Ying if (sibling == cpu || !last_level_cache_is_valid(sibling))
9055cec4eb7SHuang Ying continue;
9065cec4eb7SHuang Ying sib_llc = per_cpu_cacheinfo_idx(sibling, cache_leaves(sibling) - 1);
9075cec4eb7SHuang Ying *map = &sib_llc->shared_cpu_map;
9085cec4eb7SHuang Ying return cpumask_weight(*map);
9095cec4eb7SHuang Ying }
9105cec4eb7SHuang Ying
9115cec4eb7SHuang Ying return 0;
9125cec4eb7SHuang Ying }
9135cec4eb7SHuang Ying
91494a3bfe4SHuang Ying /*
91594a3bfe4SHuang Ying * Calculate the size of the per-CPU data cache slice. This can be
91694a3bfe4SHuang Ying * used to estimate the size of the data cache slice that can be used
91794a3bfe4SHuang Ying * by one CPU under ideal circumstances. UNIFIED caches are counted
91894a3bfe4SHuang Ying * in addition to DATA caches. So, please consider code cache usage
91994a3bfe4SHuang Ying * when use the result.
92094a3bfe4SHuang Ying *
92194a3bfe4SHuang Ying * Because the cache inclusive/non-inclusive information isn't
92294a3bfe4SHuang Ying * available, we just use the size of the per-CPU slice of LLC to make
92394a3bfe4SHuang Ying * the result more predictable across architectures.
92494a3bfe4SHuang Ying */
update_per_cpu_data_slice_size_cpu(unsigned int cpu)92594a3bfe4SHuang Ying static void update_per_cpu_data_slice_size_cpu(unsigned int cpu)
92694a3bfe4SHuang Ying {
92794a3bfe4SHuang Ying struct cpu_cacheinfo *ci;
92894a3bfe4SHuang Ying struct cacheinfo *llc;
92994a3bfe4SHuang Ying unsigned int nr_shared;
93094a3bfe4SHuang Ying
93194a3bfe4SHuang Ying if (!last_level_cache_is_valid(cpu))
93294a3bfe4SHuang Ying return;
93394a3bfe4SHuang Ying
93494a3bfe4SHuang Ying ci = ci_cacheinfo(cpu);
93594a3bfe4SHuang Ying llc = per_cpu_cacheinfo_idx(cpu, cache_leaves(cpu) - 1);
93694a3bfe4SHuang Ying
93794a3bfe4SHuang Ying if (llc->type != CACHE_TYPE_DATA && llc->type != CACHE_TYPE_UNIFIED)
93894a3bfe4SHuang Ying return;
93994a3bfe4SHuang Ying
94094a3bfe4SHuang Ying nr_shared = cpumask_weight(&llc->shared_cpu_map);
94194a3bfe4SHuang Ying if (nr_shared)
94294a3bfe4SHuang Ying ci->per_cpu_data_slice_size = llc->size / nr_shared;
94394a3bfe4SHuang Ying }
94494a3bfe4SHuang Ying
update_per_cpu_data_slice_size(bool cpu_online,unsigned int cpu,cpumask_t * cpu_map)9455cec4eb7SHuang Ying static void update_per_cpu_data_slice_size(bool cpu_online, unsigned int cpu,
9465cec4eb7SHuang Ying cpumask_t *cpu_map)
94794a3bfe4SHuang Ying {
94894a3bfe4SHuang Ying unsigned int icpu;
94994a3bfe4SHuang Ying
9505cec4eb7SHuang Ying for_each_cpu(icpu, cpu_map) {
95194a3bfe4SHuang Ying if (!cpu_online && icpu == cpu)
95294a3bfe4SHuang Ying continue;
95394a3bfe4SHuang Ying update_per_cpu_data_slice_size_cpu(icpu);
9545cec4eb7SHuang Ying setup_pcp_cacheinfo(icpu);
95594a3bfe4SHuang Ying }
95694a3bfe4SHuang Ying }
95794a3bfe4SHuang Ying
cacheinfo_cpu_online(unsigned int cpu)9587cc277b4SSebastian Andrzej Siewior static int cacheinfo_cpu_online(unsigned int cpu)
959246246cbSSudeep Holla {
9607cc277b4SSebastian Andrzej Siewior int rc = detect_cache_attributes(cpu);
9615cec4eb7SHuang Ying cpumask_t *cpu_map;
962246246cbSSudeep Holla
9637cc277b4SSebastian Andrzej Siewior if (rc)
9647cc277b4SSebastian Andrzej Siewior return rc;
965246246cbSSudeep Holla rc = cache_add_dev(cpu);
9667cc277b4SSebastian Andrzej Siewior if (rc)
96794a3bfe4SHuang Ying goto err;
9685cec4eb7SHuang Ying if (cpu_map_shared_cache(true, cpu, &cpu_map))
9695cec4eb7SHuang Ying update_per_cpu_data_slice_size(true, cpu, cpu_map);
97094a3bfe4SHuang Ying return 0;
97194a3bfe4SHuang Ying err:
972246246cbSSudeep Holla free_cache_attributes(cpu);
9737cc277b4SSebastian Andrzej Siewior return rc;
974246246cbSSudeep Holla }
9757cc277b4SSebastian Andrzej Siewior
cacheinfo_cpu_pre_down(unsigned int cpu)9767cc277b4SSebastian Andrzej Siewior static int cacheinfo_cpu_pre_down(unsigned int cpu)
9777cc277b4SSebastian Andrzej Siewior {
9785cec4eb7SHuang Ying cpumask_t *cpu_map;
9795cec4eb7SHuang Ying unsigned int nr_shared;
9805cec4eb7SHuang Ying
9815cec4eb7SHuang Ying nr_shared = cpu_map_shared_cache(false, cpu, &cpu_map);
9827cc277b4SSebastian Andrzej Siewior if (cpumask_test_and_clear_cpu(cpu, &cache_dev_map))
9837cc277b4SSebastian Andrzej Siewior cpu_cache_sysfs_exit(cpu);
9847cc277b4SSebastian Andrzej Siewior
9857cc277b4SSebastian Andrzej Siewior free_cache_attributes(cpu);
9865cec4eb7SHuang Ying if (nr_shared > 1)
9875cec4eb7SHuang Ying update_per_cpu_data_slice_size(false, cpu, cpu_map);
9887cc277b4SSebastian Andrzej Siewior return 0;
989246246cbSSudeep Holla }
990246246cbSSudeep Holla
cacheinfo_sysfs_init(void)991246246cbSSudeep Holla static int __init cacheinfo_sysfs_init(void)
992246246cbSSudeep Holla {
99383b44fe3SJames Morse return cpuhp_setup_state(CPUHP_AP_BASE_CACHEINFO_ONLINE,
99483b44fe3SJames Morse "base/cacheinfo:online",
9957cc277b4SSebastian Andrzej Siewior cacheinfo_cpu_online, cacheinfo_cpu_pre_down);
996246246cbSSudeep Holla }
997246246cbSSudeep Holla device_initcall(cacheinfo_sysfs_init);
998