2 * arch/arm64/kernel/topology.c
4 * Copyright (C) 2011,2013,2014 Linaro Limited.
6 * Based on the arm32 version written by Vincent Guittot in turn based on
7 * arch/sh/kernel/topology.c
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
14 #include <linux/arch_topology.h>
15 #include <linux/cpu.h>
16 #include <linux/cpumask.h>
17 #include <linux/init.h>
18 #include <linux/percpu.h>
19 #include <linux/node.h>
20 #include <linux/nodemask.h>
22 #include <linux/sched.h>
23 #include <linux/sched/topology.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
28 #include <asm/cputype.h>
29 #include <asm/topology.h>
31 static int __init get_cpu_for_node(struct device_node *node)
33 struct device_node *cpu_node;
36 cpu_node = of_parse_phandle(node, "cpu", 0);
40 for_each_possible_cpu(cpu) {
41 if (of_get_cpu_node(cpu, NULL) == cpu_node) {
42 topology_parse_cpu_capacity(cpu_node, cpu);
43 of_node_put(cpu_node);
48 pr_crit("Unable to find CPU node for %s\n", cpu_node->full_name);
50 of_node_put(cpu_node);
54 static int __init parse_core(struct device_node *core, int cluster_id,
61 struct device_node *t;
64 snprintf(name, sizeof(name), "thread%d", i);
65 t = of_get_child_by_name(core, name);
68 cpu = get_cpu_for_node(t);
70 cpu_topology[cpu].cluster_id = cluster_id;
71 cpu_topology[cpu].core_id = core_id;
72 cpu_topology[cpu].thread_id = i;
74 pr_err("%s: Can't get CPU for thread\n",
84 cpu = get_cpu_for_node(core);
87 pr_err("%s: Core has both threads and CPU\n",
92 cpu_topology[cpu].cluster_id = cluster_id;
93 cpu_topology[cpu].core_id = core_id;
95 pr_err("%s: Can't get CPU for leaf core\n", core->full_name);
102 static int __init parse_cluster(struct device_node *cluster, int depth)
106 bool has_cores = false;
107 struct device_node *c;
108 static int cluster_id __initdata;
113 * First check for child clusters; we currently ignore any
114 * information about the nesting of clusters and present the
115 * scheduler with a flat list of them.
119 snprintf(name, sizeof(name), "cluster%d", i);
120 c = of_get_child_by_name(cluster, name);
123 ret = parse_cluster(c, depth + 1);
131 /* Now check for cores */
134 snprintf(name, sizeof(name), "core%d", i);
135 c = of_get_child_by_name(cluster, name);
140 pr_err("%s: cpu-map children should be clusters\n",
147 ret = parse_core(c, cluster_id, core_id++);
149 pr_err("%s: Non-leaf cluster with core %s\n",
150 cluster->full_name, name);
161 if (leaf && !has_cores)
162 pr_warn("%s: empty cluster\n", cluster->full_name);
170 static int __init parse_dt_topology(void)
172 struct device_node *cn, *map;
176 cn = of_find_node_by_path("/cpus");
178 pr_err("No CPU information found in DT\n");
183 * When topology is provided cpu-map is essentially a root
184 * cluster with restricted subnodes.
186 map = of_get_child_by_name(cn, "cpu-map");
190 ret = parse_cluster(map, 0);
194 topology_normalize_cpu_scale();
197 * Check that all cores are in the topology; the SMP code will
198 * only mark cores described in the DT as possible.
200 for_each_possible_cpu(cpu)
201 if (cpu_topology[cpu].cluster_id == -1)
214 struct cpu_topology cpu_topology[NR_CPUS];
215 EXPORT_SYMBOL_GPL(cpu_topology);
217 const struct cpumask *cpu_coregroup_mask(int cpu)
219 return &cpu_topology[cpu].core_sibling;
222 static void update_siblings_masks(unsigned int cpuid)
224 struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
227 /* update core and thread sibling masks */
228 for_each_possible_cpu(cpu) {
229 cpu_topo = &cpu_topology[cpu];
231 if (cpuid_topo->cluster_id != cpu_topo->cluster_id)
234 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
236 cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
238 if (cpuid_topo->core_id != cpu_topo->core_id)
241 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
243 cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
247 void store_cpu_topology(unsigned int cpuid)
249 struct cpu_topology *cpuid_topo = &cpu_topology[cpuid];
252 if (cpuid_topo->cluster_id != -1)
253 goto topology_populated;
255 mpidr = read_cpuid_mpidr();
257 /* Uniprocessor systems can rely on default topology values */
258 if (mpidr & MPIDR_UP_BITMASK)
261 /* Create cpu topology mapping based on MPIDR. */
262 if (mpidr & MPIDR_MT_BITMASK) {
263 /* Multiprocessor system : Multi-threads per core */
264 cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
265 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
266 cpuid_topo->cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 2) |
267 MPIDR_AFFINITY_LEVEL(mpidr, 3) << 8;
269 /* Multiprocessor system : Single-thread per core */
270 cpuid_topo->thread_id = -1;
271 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
272 cpuid_topo->cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 1) |
273 MPIDR_AFFINITY_LEVEL(mpidr, 2) << 8 |
274 MPIDR_AFFINITY_LEVEL(mpidr, 3) << 16;
277 pr_debug("CPU%u: cluster %d core %d thread %d mpidr %#016llx\n",
278 cpuid, cpuid_topo->cluster_id, cpuid_topo->core_id,
279 cpuid_topo->thread_id, mpidr);
282 update_siblings_masks(cpuid);
285 static void __init reset_cpu_topology(void)
289 for_each_possible_cpu(cpu) {
290 struct cpu_topology *cpu_topo = &cpu_topology[cpu];
292 cpu_topo->thread_id = -1;
293 cpu_topo->core_id = 0;
294 cpu_topo->cluster_id = -1;
296 cpumask_clear(&cpu_topo->core_sibling);
297 cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
298 cpumask_clear(&cpu_topo->thread_sibling);
299 cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
303 void __init init_cpu_topology(void)
305 reset_cpu_topology();
308 * Discard anything that was parsed if we hit an error so we
309 * don't use partial information.
311 if (of_have_populated_dt() && parse_dt_topology())
312 reset_cpu_topology();