1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 SiFive
6 #include <linux/cacheinfo.h>
9 #include <linux/of_device.h>
10 #include <asm/cacheinfo.h>
12 static struct riscv_cacheinfo_ops *rv_cache_ops;
14 void riscv_set_cacheinfo_ops(struct riscv_cacheinfo_ops *ops)
18 EXPORT_SYMBOL_GPL(riscv_set_cacheinfo_ops);
20 const struct attribute_group *
21 cache_get_priv_group(struct cacheinfo *this_leaf)
23 if (rv_cache_ops && rv_cache_ops->get_priv_group)
24 return rv_cache_ops->get_priv_group(this_leaf);
28 static void ci_leaf_init(struct cacheinfo *this_leaf,
29 struct device_node *node,
30 enum cache_type type, unsigned int level)
32 this_leaf->level = level;
33 this_leaf->type = type;
36 static int __init_cache_level(unsigned int cpu)
38 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
39 struct device_node *np = of_cpu_device_node_get(cpu);
40 struct device_node *prev = NULL;
41 int levels = 0, leaves = 0, level;
43 if (of_property_read_bool(np, "cache-size"))
45 if (of_property_read_bool(np, "i-cache-size"))
47 if (of_property_read_bool(np, "d-cache-size"))
53 while ((np = of_find_next_cache_node(np))) {
56 if (!of_device_is_compatible(np, "cache"))
58 if (of_property_read_u32(np, "cache-level", &level))
62 if (of_property_read_bool(np, "cache-size"))
64 if (of_property_read_bool(np, "i-cache-size"))
66 if (of_property_read_bool(np, "d-cache-size"))
72 this_cpu_ci->num_levels = levels;
73 this_cpu_ci->num_leaves = leaves;
78 static int __populate_cache_leaves(unsigned int cpu)
80 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
81 struct cacheinfo *this_leaf = this_cpu_ci->info_list;
82 struct device_node *np = of_cpu_device_node_get(cpu);
83 struct device_node *prev = NULL;
84 int levels = 1, level = 1;
86 if (of_property_read_bool(np, "cache-size"))
87 ci_leaf_init(this_leaf++, np, CACHE_TYPE_UNIFIED, level);
88 if (of_property_read_bool(np, "i-cache-size"))
89 ci_leaf_init(this_leaf++, np, CACHE_TYPE_INST, level);
90 if (of_property_read_bool(np, "d-cache-size"))
91 ci_leaf_init(this_leaf++, np, CACHE_TYPE_DATA, level);
94 while ((np = of_find_next_cache_node(np))) {
97 if (!of_device_is_compatible(np, "cache"))
99 if (of_property_read_u32(np, "cache-level", &level))
103 if (of_property_read_bool(np, "cache-size"))
104 ci_leaf_init(this_leaf++, np, CACHE_TYPE_UNIFIED, level);
105 if (of_property_read_bool(np, "i-cache-size"))
106 ci_leaf_init(this_leaf++, np, CACHE_TYPE_INST, level);
107 if (of_property_read_bool(np, "d-cache-size"))
108 ci_leaf_init(this_leaf++, np, CACHE_TYPE_DATA, level);
116 DEFINE_SMP_CALL_CACHE_FUNCTION(init_cache_level)
117 DEFINE_SMP_CALL_CACHE_FUNCTION(populate_cache_leaves)