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 cpu = of_cpu_node_to_id(cpu_node);
42 topology_parse_cpu_capacity(cpu_node, cpu);
44 pr_crit("Unable to find CPU node for %pOF\n", cpu_node);
46 of_node_put(cpu_node);
50 static int __init parse_core(struct device_node *core, int cluster_id,
57 struct device_node *t;
60 snprintf(name, sizeof(name), "thread%d", i);
61 t = of_get_child_by_name(core, name);
64 cpu = get_cpu_for_node(t);
66 cpu_topology[cpu].cluster_id = cluster_id;
67 cpu_topology[cpu].core_id = core_id;
68 cpu_topology[cpu].thread_id = i;
70 pr_err("%pOF: Can't get CPU for thread\n",
80 cpu = get_cpu_for_node(core);
83 pr_err("%pOF: Core has both threads and CPU\n",
88 cpu_topology[cpu].cluster_id = cluster_id;
89 cpu_topology[cpu].core_id = core_id;
91 pr_err("%pOF: Can't get CPU for leaf core\n", core);
98 static int __init parse_cluster(struct device_node *cluster, int depth)
102 bool has_cores = false;
103 struct device_node *c;
104 static int cluster_id __initdata;
109 * First check for child clusters; we currently ignore any
110 * information about the nesting of clusters and present the
111 * scheduler with a flat list of them.
115 snprintf(name, sizeof(name), "cluster%d", i);
116 c = of_get_child_by_name(cluster, name);
119 ret = parse_cluster(c, depth + 1);
127 /* Now check for cores */
130 snprintf(name, sizeof(name), "core%d", i);
131 c = of_get_child_by_name(cluster, name);
136 pr_err("%pOF: cpu-map children should be clusters\n",
143 ret = parse_core(c, cluster_id, core_id++);
145 pr_err("%pOF: Non-leaf cluster with core %s\n",
157 if (leaf && !has_cores)
158 pr_warn("%pOF: empty cluster\n", cluster);
166 static int __init parse_dt_topology(void)
168 struct device_node *cn, *map;
172 cn = of_find_node_by_path("/cpus");
174 pr_err("No CPU information found in DT\n");
179 * When topology is provided cpu-map is essentially a root
180 * cluster with restricted subnodes.
182 map = of_get_child_by_name(cn, "cpu-map");
186 ret = parse_cluster(map, 0);
190 topology_normalize_cpu_scale();
193 * Check that all cores are in the topology; the SMP code will
194 * only mark cores described in the DT as possible.
196 for_each_possible_cpu(cpu)
197 if (cpu_topology[cpu].cluster_id == -1)
210 struct cpu_topology cpu_topology[NR_CPUS];
211 EXPORT_SYMBOL_GPL(cpu_topology);
213 const struct cpumask *cpu_coregroup_mask(int cpu)
215 return &cpu_topology[cpu].core_sibling;
218 static void update_siblings_masks(unsigned int cpuid)
220 struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
223 /* update core and thread sibling masks */
224 for_each_possible_cpu(cpu) {
225 cpu_topo = &cpu_topology[cpu];
227 if (cpuid_topo->cluster_id != cpu_topo->cluster_id)
230 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
232 cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
234 if (cpuid_topo->core_id != cpu_topo->core_id)
237 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
239 cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
243 void store_cpu_topology(unsigned int cpuid)
245 struct cpu_topology *cpuid_topo = &cpu_topology[cpuid];
248 if (cpuid_topo->cluster_id != -1)
249 goto topology_populated;
251 mpidr = read_cpuid_mpidr();
253 /* Uniprocessor systems can rely on default topology values */
254 if (mpidr & MPIDR_UP_BITMASK)
257 /* Create cpu topology mapping based on MPIDR. */
258 if (mpidr & MPIDR_MT_BITMASK) {
259 /* Multiprocessor system : Multi-threads per core */
260 cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
261 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
262 cpuid_topo->cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 2) |
263 MPIDR_AFFINITY_LEVEL(mpidr, 3) << 8;
265 /* Multiprocessor system : Single-thread per core */
266 cpuid_topo->thread_id = -1;
267 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
268 cpuid_topo->cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 1) |
269 MPIDR_AFFINITY_LEVEL(mpidr, 2) << 8 |
270 MPIDR_AFFINITY_LEVEL(mpidr, 3) << 16;
273 pr_debug("CPU%u: cluster %d core %d thread %d mpidr %#016llx\n",
274 cpuid, cpuid_topo->cluster_id, cpuid_topo->core_id,
275 cpuid_topo->thread_id, mpidr);
278 update_siblings_masks(cpuid);
281 static void __init reset_cpu_topology(void)
285 for_each_possible_cpu(cpu) {
286 struct cpu_topology *cpu_topo = &cpu_topology[cpu];
288 cpu_topo->thread_id = -1;
289 cpu_topo->core_id = 0;
290 cpu_topo->cluster_id = -1;
292 cpumask_clear(&cpu_topo->core_sibling);
293 cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
294 cpumask_clear(&cpu_topo->thread_sibling);
295 cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
299 void __init init_cpu_topology(void)
301 reset_cpu_topology();
304 * Discard anything that was parsed if we hit an error so we
305 * don't use partial information.
307 if (of_have_populated_dt() && parse_dt_topology())
308 reset_cpu_topology();