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/acpi.h>
15 #include <linux/arch_topology.h>
16 #include <linux/cpu.h>
17 #include <linux/cpumask.h>
18 #include <linux/init.h>
19 #include <linux/percpu.h>
20 #include <linux/node.h>
21 #include <linux/nodemask.h>
23 #include <linux/sched.h>
24 #include <linux/sched/topology.h>
25 #include <linux/slab.h>
26 #include <linux/smp.h>
27 #include <linux/string.h>
30 #include <asm/cputype.h>
31 #include <asm/topology.h>
33 static int __init get_cpu_for_node(struct device_node *node)
35 struct device_node *cpu_node;
38 cpu_node = of_parse_phandle(node, "cpu", 0);
42 cpu = of_cpu_node_to_id(cpu_node);
44 topology_parse_cpu_capacity(cpu_node, cpu);
46 pr_crit("Unable to find CPU node for %pOF\n", cpu_node);
48 of_node_put(cpu_node);
52 static int __init parse_core(struct device_node *core, int package_id,
59 struct device_node *t;
62 snprintf(name, sizeof(name), "thread%d", i);
63 t = of_get_child_by_name(core, name);
66 cpu = get_cpu_for_node(t);
68 cpu_topology[cpu].package_id = package_id;
69 cpu_topology[cpu].core_id = core_id;
70 cpu_topology[cpu].thread_id = i;
72 pr_err("%pOF: Can't get CPU for thread\n",
82 cpu = get_cpu_for_node(core);
85 pr_err("%pOF: Core has both threads and CPU\n",
90 cpu_topology[cpu].package_id = package_id;
91 cpu_topology[cpu].core_id = core_id;
93 pr_err("%pOF: Can't get CPU for leaf core\n", core);
100 static int __init parse_cluster(struct device_node *cluster, int depth)
104 bool has_cores = false;
105 struct device_node *c;
106 static int package_id __initdata;
111 * First check for child clusters; we currently ignore any
112 * information about the nesting of clusters and present the
113 * scheduler with a flat list of them.
117 snprintf(name, sizeof(name), "cluster%d", i);
118 c = of_get_child_by_name(cluster, name);
121 ret = parse_cluster(c, depth + 1);
129 /* Now check for cores */
132 snprintf(name, sizeof(name), "core%d", i);
133 c = of_get_child_by_name(cluster, name);
138 pr_err("%pOF: cpu-map children should be clusters\n",
145 ret = parse_core(c, package_id, core_id++);
147 pr_err("%pOF: Non-leaf cluster with core %s\n",
159 if (leaf && !has_cores)
160 pr_warn("%pOF: empty cluster\n", cluster);
168 static int __init parse_dt_topology(void)
170 struct device_node *cn, *map;
174 cn = of_find_node_by_path("/cpus");
176 pr_err("No CPU information found in DT\n");
181 * When topology is provided cpu-map is essentially a root
182 * cluster with restricted subnodes.
184 map = of_get_child_by_name(cn, "cpu-map");
188 ret = parse_cluster(map, 0);
192 topology_normalize_cpu_scale();
195 * Check that all cores are in the topology; the SMP code will
196 * only mark cores described in the DT as possible.
198 for_each_possible_cpu(cpu)
199 if (cpu_topology[cpu].package_id == -1)
212 struct cpu_topology cpu_topology[NR_CPUS];
213 EXPORT_SYMBOL_GPL(cpu_topology);
215 const struct cpumask *cpu_coregroup_mask(int cpu)
217 return &cpu_topology[cpu].core_sibling;
220 static void update_siblings_masks(unsigned int cpuid)
222 struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
225 /* update core and thread sibling masks */
226 for_each_possible_cpu(cpu) {
227 cpu_topo = &cpu_topology[cpu];
229 if (cpuid_topo->package_id != cpu_topo->package_id)
232 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
234 cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
236 if (cpuid_topo->core_id != cpu_topo->core_id)
239 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
241 cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
245 void store_cpu_topology(unsigned int cpuid)
247 struct cpu_topology *cpuid_topo = &cpu_topology[cpuid];
250 if (cpuid_topo->package_id != -1)
251 goto topology_populated;
253 mpidr = read_cpuid_mpidr();
255 /* Uniprocessor systems can rely on default topology values */
256 if (mpidr & MPIDR_UP_BITMASK)
259 /* Create cpu topology mapping based on MPIDR. */
260 if (mpidr & MPIDR_MT_BITMASK) {
261 /* Multiprocessor system : Multi-threads per core */
262 cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
263 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
264 cpuid_topo->package_id = MPIDR_AFFINITY_LEVEL(mpidr, 2) |
265 MPIDR_AFFINITY_LEVEL(mpidr, 3) << 8;
267 /* Multiprocessor system : Single-thread per core */
268 cpuid_topo->thread_id = -1;
269 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
270 cpuid_topo->package_id = MPIDR_AFFINITY_LEVEL(mpidr, 1) |
271 MPIDR_AFFINITY_LEVEL(mpidr, 2) << 8 |
272 MPIDR_AFFINITY_LEVEL(mpidr, 3) << 16;
275 pr_debug("CPU%u: cluster %d core %d thread %d mpidr %#016llx\n",
276 cpuid, cpuid_topo->package_id, cpuid_topo->core_id,
277 cpuid_topo->thread_id, mpidr);
280 update_siblings_masks(cpuid);
283 static void __init reset_cpu_topology(void)
287 for_each_possible_cpu(cpu) {
288 struct cpu_topology *cpu_topo = &cpu_topology[cpu];
290 cpu_topo->thread_id = -1;
291 cpu_topo->core_id = 0;
292 cpu_topo->package_id = -1;
294 cpumask_clear(&cpu_topo->core_sibling);
295 cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
296 cpumask_clear(&cpu_topo->thread_sibling);
297 cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
303 * Propagate the topology information of the processor_topology_node tree to the
304 * cpu_topology array.
306 static int __init parse_acpi_topology(void)
309 int cpu, topology_id;
311 is_threaded = read_cpuid_mpidr() & MPIDR_MT_BITMASK;
313 for_each_possible_cpu(cpu) {
314 topology_id = find_acpi_cpu_topology(cpu, 0);
319 cpu_topology[cpu].thread_id = topology_id;
320 topology_id = find_acpi_cpu_topology(cpu, 1);
321 cpu_topology[cpu].core_id = topology_id;
323 cpu_topology[cpu].thread_id = -1;
324 cpu_topology[cpu].core_id = topology_id;
326 topology_id = find_acpi_cpu_topology_package(cpu);
327 cpu_topology[cpu].package_id = topology_id;
334 static inline int __init parse_acpi_topology(void)
340 void __init init_cpu_topology(void)
342 reset_cpu_topology();
345 * Discard anything that was parsed if we hit an error so we
346 * don't use partial information.
348 if (!acpi_disabled && parse_acpi_topology())
349 reset_cpu_topology();
350 else if (of_have_populated_dt() && parse_dt_topology())
351 reset_cpu_topology();