2 * Copyright (C) 2017 SiFive
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.
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.
14 #include <linux/cacheinfo.h>
15 #include <linux/cpu.h>
17 #include <linux/of_device.h>
19 static void ci_leaf_init(struct cacheinfo *this_leaf,
20 struct device_node *node,
21 enum cache_type type, unsigned int level)
23 this_leaf->level = level;
24 this_leaf->type = type;
25 /* not a sector cache */
26 this_leaf->physical_line_partition = 1;
27 /* TODO: Add to DTS */
28 this_leaf->attributes =
31 | CACHE_WRITE_ALLOCATE;
34 static int __init_cache_level(unsigned int cpu)
36 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
37 struct device_node *np = of_cpu_device_node_get(cpu);
38 int levels = 0, leaves = 0, level;
40 if (of_property_read_bool(np, "cache-size"))
42 if (of_property_read_bool(np, "i-cache-size"))
44 if (of_property_read_bool(np, "d-cache-size"))
49 while ((np = of_find_next_cache_node(np))) {
50 if (!of_device_is_compatible(np, "cache"))
52 if (of_property_read_u32(np, "cache-level", &level))
56 if (of_property_read_bool(np, "cache-size"))
58 if (of_property_read_bool(np, "i-cache-size"))
60 if (of_property_read_bool(np, "d-cache-size"))
65 this_cpu_ci->num_levels = levels;
66 this_cpu_ci->num_leaves = leaves;
70 static int __populate_cache_leaves(unsigned int cpu)
72 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
73 struct cacheinfo *this_leaf = this_cpu_ci->info_list;
74 struct device_node *np = of_cpu_device_node_get(cpu);
75 int levels = 1, level = 1;
77 if (of_property_read_bool(np, "cache-size"))
78 ci_leaf_init(this_leaf++, np, CACHE_TYPE_UNIFIED, level);
79 if (of_property_read_bool(np, "i-cache-size"))
80 ci_leaf_init(this_leaf++, np, CACHE_TYPE_INST, level);
81 if (of_property_read_bool(np, "d-cache-size"))
82 ci_leaf_init(this_leaf++, np, CACHE_TYPE_DATA, level);
84 while ((np = of_find_next_cache_node(np))) {
85 if (!of_device_is_compatible(np, "cache"))
87 if (of_property_read_u32(np, "cache-level", &level))
91 if (of_property_read_bool(np, "cache-size"))
92 ci_leaf_init(this_leaf++, np, CACHE_TYPE_UNIFIED, level);
93 if (of_property_read_bool(np, "i-cache-size"))
94 ci_leaf_init(this_leaf++, np, CACHE_TYPE_INST, level);
95 if (of_property_read_bool(np, "d-cache-size"))
96 ci_leaf_init(this_leaf++, np, CACHE_TYPE_DATA, level);
103 DEFINE_SMP_CALL_CACHE_FUNCTION(init_cache_level)
104 DEFINE_SMP_CALL_CACHE_FUNCTION(populate_cache_leaves)