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/cpu.h>
15 #include <linux/cpumask.h>
16 #include <linux/init.h>
17 #include <linux/percpu.h>
18 #include <linux/node.h>
19 #include <linux/nodemask.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
24 #include <asm/topology.h>
28 * This per cpu data structure describes the relative capacity of each core.
29 * On a heteregenous system, cores don't have the same computation capacity
30 * and we reflect that difference in the cpu_power field so the scheduler can
31 * take this difference into account during load balance. A per cpu structure
32 * is preferred because each CPU updates its own cpu_power field during the
33 * load balance except for idle cores. One idle core is selected to run the
34 * rebalance_domains for all idle cores and the cpu_power can be updated
35 * during this sequence.
37 static DEFINE_PER_CPU(unsigned long, cpu_scale);
39 unsigned long arch_scale_freq_power(struct sched_domain *sd, int cpu)
41 return per_cpu(cpu_scale, cpu);
44 static void set_power_scale(unsigned int cpu, unsigned long power)
46 per_cpu(cpu_scale, cpu) = power;
49 static int __init get_cpu_for_node(struct device_node *node)
51 struct device_node *cpu_node;
54 cpu_node = of_parse_phandle(node, "cpu", 0);
58 for_each_possible_cpu(cpu) {
59 if (of_get_cpu_node(cpu, NULL) == cpu_node) {
60 of_node_put(cpu_node);
65 pr_crit("Unable to find CPU node for %s\n", cpu_node->full_name);
67 of_node_put(cpu_node);
71 static int __init parse_core(struct device_node *core, int cluster_id,
78 struct device_node *t;
81 snprintf(name, sizeof(name), "thread%d", i);
82 t = of_get_child_by_name(core, name);
85 cpu = get_cpu_for_node(t);
87 cpu_topology[cpu].cluster_id = cluster_id;
88 cpu_topology[cpu].core_id = core_id;
89 cpu_topology[cpu].thread_id = i;
91 pr_err("%s: Can't get CPU for thread\n",
101 cpu = get_cpu_for_node(core);
104 pr_err("%s: Core has both threads and CPU\n",
109 cpu_topology[cpu].cluster_id = cluster_id;
110 cpu_topology[cpu].core_id = core_id;
112 pr_err("%s: Can't get CPU for leaf core\n", core->full_name);
119 static int __init parse_cluster(struct device_node *cluster, int depth)
123 bool has_cores = false;
124 struct device_node *c;
125 static int cluster_id __initdata;
130 * First check for child clusters; we currently ignore any
131 * information about the nesting of clusters and present the
132 * scheduler with a flat list of them.
136 snprintf(name, sizeof(name), "cluster%d", i);
137 c = of_get_child_by_name(cluster, name);
140 ret = parse_cluster(c, depth + 1);
148 /* Now check for cores */
151 snprintf(name, sizeof(name), "core%d", i);
152 c = of_get_child_by_name(cluster, name);
157 pr_err("%s: cpu-map children should be clusters\n",
164 ret = parse_core(c, cluster_id, core_id++);
166 pr_err("%s: Non-leaf cluster with core %s\n",
167 cluster->full_name, name);
178 if (leaf && !has_cores)
179 pr_warn("%s: empty cluster\n", cluster->full_name);
187 struct cpu_efficiency {
188 const char *compatible;
189 unsigned long efficiency;
193 * Table of relative efficiency of each processors
194 * The efficiency value must fit in 20bit and the final
195 * cpu_scale value must be in the range
196 * 0 < cpu_scale < 3*SCHED_POWER_SCALE/2
197 * in order to return at most 1 when DIV_ROUND_CLOSEST
198 * is used to compute the capacity of a CPU.
199 * Processors that are not defined in the table,
200 * use the default SCHED_POWER_SCALE value for cpu_scale.
202 static const struct cpu_efficiency table_efficiency[] = {
203 { "arm,cortex-a57", 3891 },
204 { "arm,cortex-a53", 2048 },
208 static unsigned long *__cpu_capacity;
209 #define cpu_capacity(cpu) __cpu_capacity[cpu]
211 static unsigned long middle_capacity = 1;
214 * Iterate all CPUs' descriptor in DT and compute the efficiency
215 * (as per table_efficiency). Also calculate a middle efficiency
216 * as close as possible to (max{eff_i} - min{eff_i}) / 2
217 * This is later used to scale the cpu_power field such that an
218 * 'average' CPU is of middle power. Also see the comments near
219 * table_efficiency[] and update_cpu_power().
221 static int __init parse_dt_topology(void)
223 struct device_node *cn, *map;
227 cn = of_find_node_by_path("/cpus");
229 pr_err("No CPU information found in DT\n");
234 * When topology is provided cpu-map is essentially a root
235 * cluster with restricted subnodes.
237 map = of_get_child_by_name(cn, "cpu-map");
241 ret = parse_cluster(map, 0);
246 * Check that all cores are in the topology; the SMP code will
247 * only mark cores described in the DT as possible.
249 for_each_possible_cpu(cpu) {
250 if (cpu_topology[cpu].cluster_id == -1) {
251 pr_err("CPU%d: No topology information specified\n",
264 static void __init parse_dt_cpu_power(void)
266 const struct cpu_efficiency *cpu_eff;
267 struct device_node *cn;
268 unsigned long min_capacity = ULONG_MAX;
269 unsigned long max_capacity = 0;
270 unsigned long capacity = 0;
273 __cpu_capacity = kcalloc(nr_cpu_ids, sizeof(*__cpu_capacity),
276 for_each_possible_cpu(cpu) {
280 /* Too early to use cpu->of_node */
281 cn = of_get_cpu_node(cpu, NULL);
283 pr_err("Missing device node for CPU %d\n", cpu);
287 for (cpu_eff = table_efficiency; cpu_eff->compatible; cpu_eff++)
288 if (of_device_is_compatible(cn, cpu_eff->compatible))
291 if (cpu_eff->compatible == NULL) {
292 pr_warn("%s: Unknown CPU type\n", cn->full_name);
296 rate = of_get_property(cn, "clock-frequency", &len);
297 if (!rate || len != 4) {
298 pr_err("%s: Missing clock-frequency property\n",
303 capacity = ((be32_to_cpup(rate)) >> 20) * cpu_eff->efficiency;
305 /* Save min capacity of the system */
306 if (capacity < min_capacity)
307 min_capacity = capacity;
309 /* Save max capacity of the system */
310 if (capacity > max_capacity)
311 max_capacity = capacity;
313 cpu_capacity(cpu) = capacity;
316 /* If min and max capacities are equal we bypass the update of the
317 * cpu_scale because all CPUs have the same capacity. Otherwise, we
318 * compute a middle_capacity factor that will ensure that the capacity
319 * of an 'average' CPU of the system will be as close as possible to
320 * SCHED_POWER_SCALE, which is the default value, but with the
321 * constraint explained near table_efficiency[].
323 if (min_capacity == max_capacity)
325 else if (4 * max_capacity < (3 * (max_capacity + min_capacity)))
326 middle_capacity = (min_capacity + max_capacity)
327 >> (SCHED_POWER_SHIFT+1);
329 middle_capacity = ((max_capacity / 3)
330 >> (SCHED_POWER_SHIFT-1)) + 1;
334 * Look for a customed capacity of a CPU in the cpu_topo_data table during the
335 * boot. The update of all CPUs is in O(n^2) for heteregeneous system but the
336 * function returns directly for SMP system.
338 static void update_cpu_power(unsigned int cpu)
340 if (!cpu_capacity(cpu))
343 set_power_scale(cpu, cpu_capacity(cpu) / middle_capacity);
345 pr_info("CPU%u: update cpu_power %lu\n",
346 cpu, arch_scale_freq_power(NULL, cpu));
352 struct cpu_topology cpu_topology[NR_CPUS];
353 EXPORT_SYMBOL_GPL(cpu_topology);
355 const struct cpumask *cpu_coregroup_mask(int cpu)
357 return &cpu_topology[cpu].core_sibling;
360 static void update_siblings_masks(unsigned int cpuid)
362 struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
365 if (cpuid_topo->cluster_id == -1) {
367 * DT does not contain topology information for this cpu.
369 pr_debug("CPU%u: No topology information configured\n", cpuid);
373 /* update core and thread sibling masks */
374 for_each_possible_cpu(cpu) {
375 cpu_topo = &cpu_topology[cpu];
377 if (cpuid_topo->cluster_id != cpu_topo->cluster_id)
380 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
382 cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
384 if (cpuid_topo->core_id != cpu_topo->core_id)
387 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
389 cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
393 void store_cpu_topology(unsigned int cpuid)
395 update_siblings_masks(cpuid);
396 update_cpu_power(cpuid);
399 static void __init reset_cpu_topology(void)
403 for_each_possible_cpu(cpu) {
404 struct cpu_topology *cpu_topo = &cpu_topology[cpu];
406 cpu_topo->thread_id = -1;
407 cpu_topo->core_id = 0;
408 cpu_topo->cluster_id = -1;
410 cpumask_clear(&cpu_topo->core_sibling);
411 cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
412 cpumask_clear(&cpu_topo->thread_sibling);
413 cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
417 static void __init reset_cpu_power(void)
421 for_each_possible_cpu(cpu)
422 set_power_scale(cpu, SCHED_POWER_SCALE);
425 void __init init_cpu_topology(void)
427 reset_cpu_topology();
430 * Discard anything that was parsed if we hit an error so we
431 * don't use partial information.
433 if (parse_dt_topology())
434 reset_cpu_topology();
437 parse_dt_cpu_power();