1 // SPDX-License-Identifier: GPL-2.0-only
3 * LoongArch cacheinfo support
5 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
7 #include <linux/cacheinfo.h>
8 #include <linux/topology.h>
9 #include <asm/bootinfo.h>
10 #include <asm/cpu-info.h>
12 int init_cache_level(unsigned int cpu)
14 int cache_present = current_cpu_data.cache_leaves_present;
15 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
17 this_cpu_ci->num_levels =
18 current_cpu_data.cache_leaves[cache_present - 1].level;
19 this_cpu_ci->num_leaves = cache_present;
24 static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf,
25 struct cacheinfo *sib_leaf)
27 return (!(*(unsigned char *)(this_leaf->priv) & CACHE_PRIVATE)
28 && !(*(unsigned char *)(sib_leaf->priv) & CACHE_PRIVATE));
31 static void cache_cpumap_setup(unsigned int cpu)
34 struct cacheinfo *this_leaf, *sib_leaf;
35 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
37 for (index = 0; index < this_cpu_ci->num_leaves; index++) {
40 this_leaf = this_cpu_ci->info_list + index;
41 /* skip if shared_cpu_map is already populated */
42 if (!cpumask_empty(&this_leaf->shared_cpu_map))
45 cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
46 for_each_online_cpu(i) {
47 struct cpu_cacheinfo *sib_cpu_ci = get_cpu_cacheinfo(i);
49 if (i == cpu || !sib_cpu_ci->info_list ||
50 (cpu_to_node(i) != cpu_to_node(cpu)))
53 sib_leaf = sib_cpu_ci->info_list + index;
54 if (cache_leaves_are_shared(this_leaf, sib_leaf)) {
55 cpumask_set_cpu(cpu, &sib_leaf->shared_cpu_map);
56 cpumask_set_cpu(i, &this_leaf->shared_cpu_map);
62 int populate_cache_leaves(unsigned int cpu)
64 int i, cache_present = current_cpu_data.cache_leaves_present;
65 struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
66 struct cacheinfo *this_leaf = this_cpu_ci->info_list;
67 struct cache_desc *cd, *cdesc = current_cpu_data.cache_leaves;
69 for (i = 0; i < cache_present; i++) {
72 this_leaf->type = cd->type;
73 this_leaf->level = cd->level;
74 this_leaf->coherency_line_size = cd->linesz;
75 this_leaf->number_of_sets = cd->sets;
76 this_leaf->ways_of_associativity = cd->ways;
77 this_leaf->size = cd->linesz * cd->sets * cd->ways;
78 this_leaf->priv = &cd->flags;
82 cache_cpumap_setup(cpu);
83 this_cpu_ci->cpu_map_populated = true;