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/cacheinfo.h>
17 #include <linux/cpu.h>
18 #include <linux/cpumask.h>
19 #include <linux/init.h>
20 #include <linux/percpu.h>
21 #include <linux/node.h>
22 #include <linux/nodemask.h>
24 #include <linux/sched.h>
25 #include <linux/sched/topology.h>
26 #include <linux/slab.h>
27 #include <linux/smp.h>
28 #include <linux/string.h>
31 #include <asm/cputype.h>
32 #include <asm/topology.h>
34 static int __init get_cpu_for_node(struct device_node *node)
36 struct device_node *cpu_node;
39 cpu_node = of_parse_phandle(node, "cpu", 0);
43 cpu = of_cpu_node_to_id(cpu_node);
45 topology_parse_cpu_capacity(cpu_node, cpu);
47 pr_crit("Unable to find CPU node for %pOF\n", cpu_node);
49 of_node_put(cpu_node);
53 static int __init parse_core(struct device_node *core, int package_id,
60 struct device_node *t;
63 snprintf(name, sizeof(name), "thread%d", i);
64 t = of_get_child_by_name(core, name);
67 cpu = get_cpu_for_node(t);
69 cpu_topology[cpu].package_id = package_id;
70 cpu_topology[cpu].core_id = core_id;
71 cpu_topology[cpu].thread_id = i;
73 pr_err("%pOF: Can't get CPU for thread\n",
83 cpu = get_cpu_for_node(core);
86 pr_err("%pOF: Core has both threads and CPU\n",
91 cpu_topology[cpu].package_id = package_id;
92 cpu_topology[cpu].core_id = core_id;
94 pr_err("%pOF: Can't get CPU for leaf core\n", core);
101 static int __init parse_cluster(struct device_node *cluster, int depth)
105 bool has_cores = false;
106 struct device_node *c;
107 static int package_id __initdata;
112 * First check for child clusters; we currently ignore any
113 * information about the nesting of clusters and present the
114 * scheduler with a flat list of them.
118 snprintf(name, sizeof(name), "cluster%d", i);
119 c = of_get_child_by_name(cluster, name);
122 ret = parse_cluster(c, depth + 1);
130 /* Now check for cores */
133 snprintf(name, sizeof(name), "core%d", i);
134 c = of_get_child_by_name(cluster, name);
139 pr_err("%pOF: cpu-map children should be clusters\n",
146 ret = parse_core(c, package_id, core_id++);
148 pr_err("%pOF: Non-leaf cluster with core %s\n",
160 if (leaf && !has_cores)
161 pr_warn("%pOF: empty cluster\n", cluster);
169 static int __init parse_dt_topology(void)
171 struct device_node *cn, *map;
175 cn = of_find_node_by_path("/cpus");
177 pr_err("No CPU information found in DT\n");
182 * When topology is provided cpu-map is essentially a root
183 * cluster with restricted subnodes.
185 map = of_get_child_by_name(cn, "cpu-map");
189 ret = parse_cluster(map, 0);
193 topology_normalize_cpu_scale();
196 * Check that all cores are in the topology; the SMP code will
197 * only mark cores described in the DT as possible.
199 for_each_possible_cpu(cpu)
200 if (cpu_topology[cpu].package_id == -1)
213 struct cpu_topology cpu_topology[NR_CPUS];
214 EXPORT_SYMBOL_GPL(cpu_topology);
216 const struct cpumask *cpu_coregroup_mask(int cpu)
218 const cpumask_t *core_mask = cpumask_of_node(cpu_to_node(cpu));
220 /* Find the smaller of NUMA, core or LLC siblings */
221 if (cpumask_subset(&cpu_topology[cpu].core_sibling, core_mask)) {
222 /* not numa in package, lets use the package siblings */
223 core_mask = &cpu_topology[cpu].core_sibling;
225 if (cpu_topology[cpu].llc_id != -1) {
226 if (cpumask_subset(&cpu_topology[cpu].llc_sibling, core_mask))
227 core_mask = &cpu_topology[cpu].llc_sibling;
233 static void update_siblings_masks(unsigned int cpuid)
235 struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
238 /* update core and thread sibling masks */
239 for_each_online_cpu(cpu) {
240 cpu_topo = &cpu_topology[cpu];
242 if (cpuid_topo->llc_id == cpu_topo->llc_id) {
243 cpumask_set_cpu(cpu, &cpuid_topo->llc_sibling);
244 cpumask_set_cpu(cpuid, &cpu_topo->llc_sibling);
247 if (cpuid_topo->package_id != cpu_topo->package_id)
250 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
251 cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
253 if (cpuid_topo->core_id != cpu_topo->core_id)
256 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
257 cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
261 void store_cpu_topology(unsigned int cpuid)
263 struct cpu_topology *cpuid_topo = &cpu_topology[cpuid];
266 if (cpuid_topo->package_id != -1)
267 goto topology_populated;
269 mpidr = read_cpuid_mpidr();
271 /* Uniprocessor systems can rely on default topology values */
272 if (mpidr & MPIDR_UP_BITMASK)
276 * This would be the place to create cpu topology based on MPIDR.
278 * However, it cannot be trusted to depict the actual topology; some
279 * pieces of the architecture enforce an artificial cap on Aff0 values
280 * (e.g. GICv3's ICC_SGI1R_EL1 limits it to 15), leading to an
281 * artificial cycling of Aff1, Aff2 and Aff3 values. IOW, these end up
282 * having absolutely no relationship to the actual underlying system
283 * topology, and cannot be reasonably used as core / package ID.
285 * If the MT bit is set, Aff0 *could* be used to define a thread ID, but
286 * we still wouldn't be able to obtain a sane core ID. This means we
287 * need to entirely ignore MPIDR for any topology deduction.
289 cpuid_topo->thread_id = -1;
290 cpuid_topo->core_id = cpuid;
291 cpuid_topo->package_id = cpu_to_node(cpuid);
293 pr_debug("CPU%u: cluster %d core %d thread %d mpidr %#016llx\n",
294 cpuid, cpuid_topo->package_id, cpuid_topo->core_id,
295 cpuid_topo->thread_id, mpidr);
298 update_siblings_masks(cpuid);
301 static void clear_cpu_topology(int cpu)
303 struct cpu_topology *cpu_topo = &cpu_topology[cpu];
305 cpumask_clear(&cpu_topo->llc_sibling);
306 cpumask_set_cpu(cpu, &cpu_topo->llc_sibling);
308 cpumask_clear(&cpu_topo->core_sibling);
309 cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
310 cpumask_clear(&cpu_topo->thread_sibling);
311 cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
314 static void __init reset_cpu_topology(void)
318 for_each_possible_cpu(cpu) {
319 struct cpu_topology *cpu_topo = &cpu_topology[cpu];
321 cpu_topo->thread_id = -1;
322 cpu_topo->core_id = 0;
323 cpu_topo->package_id = -1;
324 cpu_topo->llc_id = -1;
326 clear_cpu_topology(cpu);
330 void remove_cpu_topology(unsigned int cpu)
334 for_each_cpu(sibling, topology_core_cpumask(cpu))
335 cpumask_clear_cpu(cpu, topology_core_cpumask(sibling));
336 for_each_cpu(sibling, topology_sibling_cpumask(cpu))
337 cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling));
338 for_each_cpu(sibling, topology_llc_cpumask(cpu))
339 cpumask_clear_cpu(cpu, topology_llc_cpumask(sibling));
341 clear_cpu_topology(cpu);
345 static bool __init acpi_cpu_is_threaded(int cpu)
347 int is_threaded = acpi_pptt_cpu_is_thread(cpu);
350 * if the PPTT doesn't have thread information, assume a homogeneous
351 * machine and return the current CPU's thread state.
354 is_threaded = read_cpuid_mpidr() & MPIDR_MT_BITMASK;
356 return !!is_threaded;
360 * Propagate the topology information of the processor_topology_node tree to the
361 * cpu_topology array.
363 static int __init parse_acpi_topology(void)
365 int cpu, topology_id;
367 for_each_possible_cpu(cpu) {
370 topology_id = find_acpi_cpu_topology(cpu, 0);
374 if (acpi_cpu_is_threaded(cpu)) {
375 cpu_topology[cpu].thread_id = topology_id;
376 topology_id = find_acpi_cpu_topology(cpu, 1);
377 cpu_topology[cpu].core_id = topology_id;
379 cpu_topology[cpu].thread_id = -1;
380 cpu_topology[cpu].core_id = topology_id;
382 topology_id = find_acpi_cpu_topology_package(cpu);
383 cpu_topology[cpu].package_id = topology_id;
385 i = acpi_find_last_cache_level(cpu);
389 * this is the only part of cpu_topology that has
390 * a direct relationship with the cache topology
392 cache_id = find_acpi_cpu_cache_topology(cpu, i);
394 cpu_topology[cpu].llc_id = cache_id;
402 static inline int __init parse_acpi_topology(void)
408 void __init init_cpu_topology(void)
410 reset_cpu_topology();
413 * Discard anything that was parsed if we hit an error so we
414 * don't use partial information.
416 if (!acpi_disabled && parse_acpi_topology())
417 reset_cpu_topology();
418 else if (of_have_populated_dt() && parse_dt_topology())
419 reset_cpu_topology();