xref: /openbmc/linux/arch/riscv/kernel/cacheinfo.c (revision 6d99a79c)
1 /*
2  * Copyright (C) 2017 SiFive
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation, version 2.
7  *
8  *   This program is distributed in the hope that it will be useful,
9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *   GNU General Public License for more details.
12  */
13 
14 #include <linux/cacheinfo.h>
15 #include <linux/cpu.h>
16 #include <linux/of.h>
17 #include <linux/of_device.h>
18 
19 static void ci_leaf_init(struct cacheinfo *this_leaf,
20 			 struct device_node *node,
21 			 enum cache_type type, unsigned int level)
22 {
23 	this_leaf->level = level;
24 	this_leaf->type = type;
25 }
26 
27 static int __init_cache_level(unsigned int cpu)
28 {
29 	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
30 	struct device_node *np = of_cpu_device_node_get(cpu);
31 	int levels = 0, leaves = 0, level;
32 
33 	if (of_property_read_bool(np, "cache-size"))
34 		++leaves;
35 	if (of_property_read_bool(np, "i-cache-size"))
36 		++leaves;
37 	if (of_property_read_bool(np, "d-cache-size"))
38 		++leaves;
39 	if (leaves > 0)
40 		levels = 1;
41 
42 	while ((np = of_find_next_cache_node(np))) {
43 		if (!of_device_is_compatible(np, "cache"))
44 			break;
45 		if (of_property_read_u32(np, "cache-level", &level))
46 			break;
47 		if (level <= levels)
48 			break;
49 		if (of_property_read_bool(np, "cache-size"))
50 			++leaves;
51 		if (of_property_read_bool(np, "i-cache-size"))
52 			++leaves;
53 		if (of_property_read_bool(np, "d-cache-size"))
54 			++leaves;
55 		levels = level;
56 	}
57 
58 	this_cpu_ci->num_levels = levels;
59 	this_cpu_ci->num_leaves = leaves;
60 	return 0;
61 }
62 
63 static int __populate_cache_leaves(unsigned int cpu)
64 {
65 	struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
66 	struct cacheinfo *this_leaf = this_cpu_ci->info_list;
67 	struct device_node *np = of_cpu_device_node_get(cpu);
68 	int levels = 1, level = 1;
69 
70 	if (of_property_read_bool(np, "cache-size"))
71 		ci_leaf_init(this_leaf++, np, CACHE_TYPE_UNIFIED, level);
72 	if (of_property_read_bool(np, "i-cache-size"))
73 		ci_leaf_init(this_leaf++, np, CACHE_TYPE_INST, level);
74 	if (of_property_read_bool(np, "d-cache-size"))
75 		ci_leaf_init(this_leaf++, np, CACHE_TYPE_DATA, level);
76 
77 	while ((np = of_find_next_cache_node(np))) {
78 		if (!of_device_is_compatible(np, "cache"))
79 			break;
80 		if (of_property_read_u32(np, "cache-level", &level))
81 			break;
82 		if (level <= levels)
83 			break;
84 		if (of_property_read_bool(np, "cache-size"))
85 			ci_leaf_init(this_leaf++, np, CACHE_TYPE_UNIFIED, level);
86 		if (of_property_read_bool(np, "i-cache-size"))
87 			ci_leaf_init(this_leaf++, np, CACHE_TYPE_INST, level);
88 		if (of_property_read_bool(np, "d-cache-size"))
89 			ci_leaf_init(this_leaf++, np, CACHE_TYPE_DATA, level);
90 		levels = level;
91 	}
92 
93 	return 0;
94 }
95 
96 DEFINE_SMP_CALL_CACHE_FUNCTION(init_cache_level)
97 DEFINE_SMP_CALL_CACHE_FUNCTION(populate_cache_leaves)
98