blob: dad9fcbc38949f8de3d67800841e472890acdfc2 [file] [log] [blame]
Vincent Guittotc9018aa2011-08-08 13:21:59 +01001/*
2 * arch/arm/kernel/topology.c
3 *
4 * Copyright (C) 2011 Linaro Limited.
5 * Written by: Vincent Guittot
6 *
7 * based on arch/sh/kernel/topology.c
8 *
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
11 * for more details.
12 */
13
14#include <linux/cpu.h>
15#include <linux/cpumask.h>
Arnd Bergmann92bdd3f2013-05-31 22:49:22 +010016#include <linux/export.h>
Vincent Guittotc9018aa2011-08-08 13:21:59 +010017#include <linux/init.h>
18#include <linux/percpu.h>
19#include <linux/node.h>
20#include <linux/nodemask.h>
Vincent Guittot339ca092012-07-10 14:13:12 +010021#include <linux/of.h>
Vincent Guittotc9018aa2011-08-08 13:21:59 +010022#include <linux/sched.h>
Vincent Guittot339ca092012-07-10 14:13:12 +010023#include <linux/slab.h>
Maria Yue5a586c2018-02-13 12:59:23 +080024#include <linux/sched_energy.h>
Vincent Guittotc9018aa2011-08-08 13:21:59 +010025
26#include <asm/cputype.h>
27#include <asm/topology.h>
28
Vincent Guittot130d9aa2012-07-10 14:08:40 +010029/*
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040030 * cpu capacity scale management
Vincent Guittot130d9aa2012-07-10 14:08:40 +010031 */
32
33/*
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040034 * cpu capacity table
Vincent Guittot130d9aa2012-07-10 14:08:40 +010035 * This per cpu data structure describes the relative capacity of each core.
36 * On a heteregenous system, cores don't have the same computation capacity
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040037 * and we reflect that difference in the cpu_capacity field so the scheduler
38 * can take this difference into account during load balance. A per cpu
39 * structure is preferred because each CPU updates its own cpu_capacity field
40 * during the load balance except for idle cores. One idle core is selected
41 * to run the rebalance_domains for all idle cores and the cpu_capacity can be
42 * updated during this sequence.
Vincent Guittot130d9aa2012-07-10 14:08:40 +010043 */
Juri Lellid78e13a2016-01-07 16:27:33 +010044static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
Vincent Guittot130d9aa2012-07-10 14:08:40 +010045
Maria Yu1dc40b02017-09-26 16:50:56 +080046unsigned long arch_scale_freq_power(struct sched_domain *sd, int cpu)
47{
48 return per_cpu(cpu_scale, cpu);
49}
50
51static void set_power_scale(unsigned int cpu, unsigned long power)
52{
53 per_cpu(cpu_scale, cpu) = power;
54}
55
Morten Rasmussen25cea242015-04-14 16:25:31 +010056unsigned long scale_cpu_capacity(struct sched_domain *sd, int cpu)
Vincent Guittot130d9aa2012-07-10 14:08:40 +010057{
Jon Medhurstaa9ea842016-06-02 12:18:08 +000058#ifdef CONFIG_CPU_FREQ
Dietmar Eggemann568913e2015-09-23 17:59:55 +010059 unsigned long max_freq_scale = cpufreq_scale_max_freq_capacity(cpu);
60
61 return per_cpu(cpu_scale, cpu) * max_freq_scale >> SCHED_CAPACITY_SHIFT;
62#else
Vincent Guittot130d9aa2012-07-10 14:08:40 +010063 return per_cpu(cpu_scale, cpu);
Dietmar Eggemann568913e2015-09-23 17:59:55 +010064#endif
Vincent Guittot130d9aa2012-07-10 14:08:40 +010065}
66
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040067static void set_capacity_scale(unsigned int cpu, unsigned long capacity)
Vincent Guittot130d9aa2012-07-10 14:08:40 +010068{
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040069 per_cpu(cpu_scale, cpu) = capacity;
Vincent Guittot130d9aa2012-07-10 14:08:40 +010070}
71
Jeevan Shriram490837a2017-03-01 17:52:49 -080072static int __init get_cpu_for_node(struct device_node *node)
73{
74 struct device_node *cpu_node;
75 int cpu;
76
77 cpu_node = of_parse_phandle(node, "cpu", 0);
78 if (!cpu_node)
79 return -EINVAL;
80
81 for_each_possible_cpu(cpu) {
82 if (of_get_cpu_node(cpu, NULL) == cpu_node) {
83 of_node_put(cpu_node);
84 return cpu;
85 }
86 }
87
88 pr_crit("Unable to find CPU node for %s\n", cpu_node->full_name);
89
90 of_node_put(cpu_node);
91 return -EINVAL;
92}
93
94static int __init parse_core(struct device_node *core, int cluster_id,
95 int core_id)
96{
97 char name[10];
98 bool leaf = true;
99 int i = 0;
100 int cpu;
101 struct device_node *t;
102
103 do {
104 snprintf(name, sizeof(name), "thread%d", i);
105 t = of_get_child_by_name(core, name);
106 if (t) {
107 leaf = false;
108 cpu = get_cpu_for_node(t);
109 if (cpu >= 0) {
110 cpu_topology[cpu].socket_id = cluster_id;
111 cpu_topology[cpu].core_id = core_id;
112 cpu_topology[cpu].thread_id = i;
113 } else {
114 pr_err("%s: Can't get CPU for thread\n",
115 t->full_name);
116 of_node_put(t);
117 return -EINVAL;
118 }
119 of_node_put(t);
120 }
121 i++;
122 } while (t);
123
124 cpu = get_cpu_for_node(core);
125 if (cpu >= 0) {
126 if (!leaf) {
127 pr_err("%s: Core has both threads and CPU\n",
128 core->full_name);
129 return -EINVAL;
130 }
131
132 cpu_topology[cpu].socket_id = cluster_id;
133 cpu_topology[cpu].core_id = core_id;
134 } else if (leaf) {
135 pr_err("%s: Can't get CPU for leaf core\n", core->full_name);
136 return -EINVAL;
137 }
138
139 return 0;
140}
141
142static int __init parse_cluster(struct device_node *cluster, int depth)
143{
144 static int cluster_id __initdata;
145 char name[10];
146 bool leaf = true;
147 bool has_cores = false;
148 struct device_node *c;
149 int core_id = 0;
150 int i, ret;
151
152 /*
153 * First check for child clusters; we currently ignore any
154 * information about the nesting of clusters and present the
155 * scheduler with a flat list of them.
156 */
157 i = 0;
158 do {
159 snprintf(name, sizeof(name), "cluster%d", i);
160 c = of_get_child_by_name(cluster, name);
161 if (c) {
162 leaf = false;
163 ret = parse_cluster(c, depth + 1);
164 of_node_put(c);
165 if (ret != 0)
166 return ret;
167 }
168 i++;
169 } while (c);
170
171 /* Now check for cores */
172 i = 0;
173 do {
174 snprintf(name, sizeof(name), "core%d", i);
175 c = of_get_child_by_name(cluster, name);
176 if (c) {
177 has_cores = true;
178
179 if (depth == 0) {
180 pr_err("%s: cpu-map children should be clusters\n",
181 c->full_name);
182 of_node_put(c);
183 return -EINVAL;
184 }
185
186 if (leaf) {
187 ret = parse_core(c, cluster_id, core_id++);
188 } else {
189 pr_err("%s: Non-leaf cluster with core %s\n",
190 cluster->full_name, name);
191 ret = -EINVAL;
192 }
193
194 of_node_put(c);
195 if (ret != 0)
196 return ret;
197 }
198 i++;
199 } while (c);
200
201 if (leaf && !has_cores)
202 pr_warn("%s: empty cluster\n", cluster->full_name);
203
204 if (leaf)
205 cluster_id++;
206
207 return 0;
208}
209
Srivatsa Vaddagirifd5704a2014-03-31 19:42:27 -0700210static DEFINE_PER_CPU(unsigned long, cpu_efficiency) = SCHED_CAPACITY_SCALE;
211
212unsigned long arch_get_cpu_efficiency(int cpu)
213{
214 return per_cpu(cpu_efficiency, cpu);
215}
Trilok Sonib10a8a82016-06-10 14:49:06 -0700216EXPORT_SYMBOL(arch_get_cpu_efficiency);
Srivatsa Vaddagirifd5704a2014-03-31 19:42:27 -0700217
Vincent Guittot339ca092012-07-10 14:13:12 +0100218#ifdef CONFIG_OF
219struct cpu_efficiency {
220 const char *compatible;
221 unsigned long efficiency;
222};
223
224/*
225 * Table of relative efficiency of each processors
226 * The efficiency value must fit in 20bit and the final
227 * cpu_scale value must be in the range
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400228 * 0 < cpu_scale < 3*SCHED_CAPACITY_SCALE/2
Vincent Guittot339ca092012-07-10 14:13:12 +0100229 * in order to return at most 1 when DIV_ROUND_CLOSEST
230 * is used to compute the capacity of a CPU.
231 * Processors that are not defined in the table,
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400232 * use the default SCHED_CAPACITY_SCALE value for cpu_scale.
Vincent Guittot339ca092012-07-10 14:13:12 +0100233 */
Mark Brown145bc292013-12-10 12:10:17 +0100234static const struct cpu_efficiency table_efficiency[] = {
Vincent Guittot339ca092012-07-10 14:13:12 +0100235 {"arm,cortex-a15", 3891},
236 {"arm,cortex-a7", 2048},
237 {NULL, },
238};
239
Mark Brown145bc292013-12-10 12:10:17 +0100240static unsigned long *__cpu_capacity;
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +0100241#define cpu_capacity(cpu) __cpu_capacity[cpu]
Vincent Guittot339ca092012-07-10 14:13:12 +0100242
Mark Brown145bc292013-12-10 12:10:17 +0100243static unsigned long middle_capacity = 1;
Vincent Guittot339ca092012-07-10 14:13:12 +0100244
245/*
246 * Iterate all CPUs' descriptor in DT and compute the efficiency
247 * (as per table_efficiency). Also calculate a middle efficiency
248 * as close as possible to (max{eff_i} - min{eff_i}) / 2
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400249 * This is later used to scale the cpu_capacity field such that an
250 * 'average' CPU is of middle capacity. Also see the comments near
251 * table_efficiency[] and update_cpu_capacity().
Vincent Guittot339ca092012-07-10 14:13:12 +0100252 */
Jeevan Shriram490837a2017-03-01 17:52:49 -0800253static int __init parse_dt_topology(void)
Vincent Guittot339ca092012-07-10 14:13:12 +0100254{
Mark Brown145bc292013-12-10 12:10:17 +0100255 const struct cpu_efficiency *cpu_eff;
Jeevan Shriram490837a2017-03-01 17:52:49 -0800256 struct device_node *cn = NULL, *map;
Mark Brown44ae9032014-03-20 15:16:54 +0100257 unsigned long min_capacity = ULONG_MAX;
Vincent Guittot339ca092012-07-10 14:13:12 +0100258 unsigned long max_capacity = 0;
259 unsigned long capacity = 0;
Jeevan Shriram490837a2017-03-01 17:52:49 -0800260 int cpu = 0, ret = 0;
261
Srinivas Ramana3ddfb502016-06-28 12:02:28 +0530262 __cpu_capacity = kcalloc(nr_cpu_ids, sizeof(*__cpu_capacity),
263 GFP_NOWAIT);
264
Jeevan Shriram490837a2017-03-01 17:52:49 -0800265 cn = of_find_node_by_path("/cpus");
266 if (!cn) {
267 pr_err("No CPU information found in DT\n");
268 return 0;
269 }
270
271 /*
272 * When topology is provided cpu-map is essentially a root
273 * cluster with restricted subnodes.
274 */
275 map = of_get_child_by_name(cn, "cpu-map");
276 if (!map)
277 goto out;
278
279 ret = parse_cluster(map, 0);
280 if (ret != 0)
281 goto out_map;
282
283 /*
284 * Check that all cores are in the topology; the SMP code will
285 * only mark cores described in the DT as possible.
286 */
287 for_each_possible_cpu(cpu)
288 if (cpu_topology[cpu].socket_id == -1)
289 ret = -EINVAL;
Vincent Guittot339ca092012-07-10 14:13:12 +0100290
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +0100291 for_each_possible_cpu(cpu) {
292 const u32 *rate;
Vincent Guittot339ca092012-07-10 14:13:12 +0100293 int len;
Pavankumar Kondeti31058f82015-05-07 17:14:48 +0530294 u32 efficiency;
Vincent Guittot339ca092012-07-10 14:13:12 +0100295
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +0100296 /* too early to use cpu->of_node */
297 cn = of_get_cpu_node(cpu, NULL);
298 if (!cn) {
299 pr_err("missing device node for CPU %d\n", cpu);
300 continue;
301 }
Vincent Guittot339ca092012-07-10 14:13:12 +0100302
Pavankumar Kondeti31058f82015-05-07 17:14:48 +0530303 /*
304 * The CPU efficiency value passed from the device tree
305 * overrides the value defined in the table_efficiency[]
306 */
307 if (of_property_read_u32(cn, "efficiency", &efficiency) < 0) {
Vincent Guittot339ca092012-07-10 14:13:12 +0100308
Pavankumar Kondeti31058f82015-05-07 17:14:48 +0530309 for (cpu_eff = table_efficiency;
310 cpu_eff->compatible; cpu_eff++)
Vincent Guittot339ca092012-07-10 14:13:12 +0100311
Pavankumar Kondeti31058f82015-05-07 17:14:48 +0530312 if (of_device_is_compatible(cn,
313 cpu_eff->compatible))
314 break;
315
316 if (cpu_eff->compatible == NULL)
317 continue;
318
319 efficiency = cpu_eff->efficiency;
320 }
321
322 per_cpu(cpu_efficiency, cpu) = efficiency;
Srivatsa Vaddagirifd5704a2014-03-31 19:42:27 -0700323
Vincent Guittot339ca092012-07-10 14:13:12 +0100324 rate = of_get_property(cn, "clock-frequency", &len);
325 if (!rate || len != 4) {
326 pr_err("%s missing clock-frequency property\n",
327 cn->full_name);
328 continue;
329 }
330
Pavankumar Kondeti31058f82015-05-07 17:14:48 +0530331 capacity = ((be32_to_cpup(rate)) >> 20) * efficiency;
Vincent Guittot339ca092012-07-10 14:13:12 +0100332
333 /* Save min capacity of the system */
334 if (capacity < min_capacity)
335 min_capacity = capacity;
336
337 /* Save max capacity of the system */
338 if (capacity > max_capacity)
339 max_capacity = capacity;
340
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +0100341 cpu_capacity(cpu) = capacity;
Vincent Guittot339ca092012-07-10 14:13:12 +0100342 }
343
Vincent Guittot339ca092012-07-10 14:13:12 +0100344 /* If min and max capacities are equals, we bypass the update of the
345 * cpu_scale because all CPUs have the same capacity. Otherwise, we
346 * compute a middle_capacity factor that will ensure that the capacity
347 * of an 'average' CPU of the system will be as close as possible to
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400348 * SCHED_CAPACITY_SCALE, which is the default value, but with the
Vincent Guittot339ca092012-07-10 14:13:12 +0100349 * constraint explained near table_efficiency[].
350 */
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +0100351 if (4*max_capacity < (3*(max_capacity + min_capacity)))
Vincent Guittot339ca092012-07-10 14:13:12 +0100352 middle_capacity = (min_capacity + max_capacity)
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400353 >> (SCHED_CAPACITY_SHIFT+1);
Vincent Guittot339ca092012-07-10 14:13:12 +0100354 else
355 middle_capacity = ((max_capacity / 3)
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400356 >> (SCHED_CAPACITY_SHIFT-1)) + 1;
Jeevan Shriram490837a2017-03-01 17:52:49 -0800357out_map:
358 of_node_put(map);
359out:
360 of_node_put(cn);
361 return ret;
Vincent Guittot339ca092012-07-10 14:13:12 +0100362}
363
Dietmar Eggemannb4ca4bc2015-07-10 13:57:19 +0100364static const struct sched_group_energy * const cpu_core_energy(int cpu);
365
Vincent Guittot339ca092012-07-10 14:13:12 +0100366/*
367 * Look for a customed capacity of a CPU in the cpu_capacity table during the
368 * boot. The update of all CPUs is in O(n^2) for heteregeneous system but the
369 * function returns directly for SMP system.
370 */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400371static void update_cpu_capacity(unsigned int cpu)
Vincent Guittot339ca092012-07-10 14:13:12 +0100372{
Dietmar Eggemannb4ca4bc2015-07-10 13:57:19 +0100373 unsigned long capacity = SCHED_CAPACITY_SCALE;
Vincent Guittot339ca092012-07-10 14:13:12 +0100374
Dietmar Eggemannb4ca4bc2015-07-10 13:57:19 +0100375 if (cpu_core_energy(cpu)) {
376 int max_cap_idx = cpu_core_energy(cpu)->nr_cap_states - 1;
377 capacity = cpu_core_energy(cpu)->cap_states[max_cap_idx].cap;
378 }
379
380 set_capacity_scale(cpu, capacity);
Vincent Guittot339ca092012-07-10 14:13:12 +0100381
Russell King4ed89f22014-10-28 11:26:42 +0000382 pr_info("CPU%u: update cpu_capacity %lu\n",
Vincent Guittotd3bfca12014-08-26 13:06:48 +0200383 cpu, arch_scale_cpu_capacity(NULL, cpu));
Vincent Guittot339ca092012-07-10 14:13:12 +0100384}
385
386#else
Jeevan Shriram490837a2017-03-01 17:52:49 -0800387static inline int parse_dt_topology(void) {}
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400388static inline void update_cpu_capacity(unsigned int cpuid) {}
Vincent Guittot339ca092012-07-10 14:13:12 +0100389#endif
390
Lorenzo Pieralisidca463da2012-11-15 17:30:32 +0000391 /*
Vincent Guittot130d9aa2012-07-10 14:08:40 +0100392 * cpu topology table
393 */
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100394struct cputopo_arm cpu_topology[NR_CPUS];
Arnd Bergmann92bdd3f2013-05-31 22:49:22 +0100395EXPORT_SYMBOL_GPL(cpu_topology);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100396
Vincent Guittot4cbd6b12011-11-29 15:50:20 +0100397const struct cpumask *cpu_coregroup_mask(int cpu)
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100398{
399 return &cpu_topology[cpu].core_sibling;
400}
401
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200402/*
403 * The current assumption is that we can power gate each core independently.
404 * This will be superseded by DT binding once available.
405 */
406const struct cpumask *cpu_corepower_mask(int cpu)
407{
408 return &cpu_topology[cpu].thread_sibling;
409}
410
Maria Yu1dc40b02017-09-26 16:50:56 +0800411static void update_cpu_power(unsigned int cpu)
412{
413 if (!cpu_capacity(cpu))
414 return;
415
416 set_power_scale(cpu, cpu_capacity(cpu) / middle_capacity);
417
418 pr_info("CPU%u: update cpu_power %lu\n",
419 cpu, arch_scale_freq_power(NULL, cpu));
420}
421
422void update_cpu_power_capacity(int cpu)
423{
424 update_cpu_power(cpu);
425 update_cpu_capacity(cpu);
426}
427
Mark Brown145bc292013-12-10 12:10:17 +0100428static void update_siblings_masks(unsigned int cpuid)
Vincent Guittotcb75dac2012-07-10 14:11:11 +0100429{
430 struct cputopo_arm *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
431 int cpu;
432
433 /* update core and thread sibling masks */
434 for_each_possible_cpu(cpu) {
435 cpu_topo = &cpu_topology[cpu];
436
437 if (cpuid_topo->socket_id != cpu_topo->socket_id)
438 continue;
439
440 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
441 if (cpu != cpuid)
442 cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
443
444 if (cpuid_topo->core_id != cpu_topo->core_id)
445 continue;
446
447 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
448 if (cpu != cpuid)
449 cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
450 }
Srinivas Ramana3ddfb502016-06-28 12:02:28 +0530451
452 smp_wmb(); /* Ensure mask is updated*/
Vincent Guittotcb75dac2012-07-10 14:11:11 +0100453}
454
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100455/*
456 * store_cpu_topology is called at boot when only one cpu is running
457 * and with the mutex cpu_hotplug.lock locked, when several cpus have booted,
458 * which prevents simultaneous write access to cpu_topology array
459 */
460void store_cpu_topology(unsigned int cpuid)
461{
462 struct cputopo_arm *cpuid_topo = &cpu_topology[cpuid];
463 unsigned int mpidr;
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100464
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100465 if (cpuid_topo->core_id != -1)
Jeevan Shriram490837a2017-03-01 17:52:49 -0800466 goto topology_populated;
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100467
468 mpidr = read_cpuid_mpidr();
469
470 /* create cpu topology mapping */
471 if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) {
472 /*
473 * This is a multiprocessor system
474 * multiprocessor format & multiprocessor mode field are set
475 */
476
477 if (mpidr & MPIDR_MT_BITMASK) {
478 /* core performance interdependency */
Lorenzo Pieralisi71db5bf2012-11-16 15:24:06 +0000479 cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
480 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
481 cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100482 } else {
483 /* largely independent cores */
484 cpuid_topo->thread_id = -1;
Lorenzo Pieralisi71db5bf2012-11-16 15:24:06 +0000485 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
486 cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100487 }
488 } else {
489 /*
490 * This is an uniprocessor system
491 * we are in multiprocessor format but uniprocessor system
492 * or in the old uniprocessor format
493 */
494 cpuid_topo->thread_id = -1;
495 cpuid_topo->core_id = 0;
496 cpuid_topo->socket_id = -1;
497 }
498
Jeevan Shriram490837a2017-03-01 17:52:49 -0800499 pr_info("CPU%u: thread %d, cpu %d, cluster %d, mpidr %x\n",
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100500 cpuid, cpu_topology[cpuid].thread_id,
501 cpu_topology[cpuid].core_id,
502 cpu_topology[cpuid].socket_id, mpidr);
Jeevan Shriram490837a2017-03-01 17:52:49 -0800503
504topology_populated:
505 update_siblings_masks(cpuid);
506 update_cpu_capacity(cpuid);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100507}
508
Dietmar Eggemann61100bd2014-11-14 17:16:41 +0000509/* sd energy functions */
510static inline
511const struct sched_group_energy * const cpu_cluster_energy(int cpu)
512{
Maria Yue5a586c2018-02-13 12:59:23 +0800513 struct sched_group_energy *sge = sge_array[cpu][SD_LEVEL1];
514
515 if (sched_is_energy_aware() && !sge) {
516 pr_warn("Invalid sched_group_energy for Cluster%d\n", cpu);
517 return NULL;
518 }
519
520 return sge;
Dietmar Eggemann61100bd2014-11-14 17:16:41 +0000521}
522
523static inline
524const struct sched_group_energy * const cpu_core_energy(int cpu)
525{
Maria Yue5a586c2018-02-13 12:59:23 +0800526 struct sched_group_energy *sge = sge_array[cpu][SD_LEVEL0];
527
528 if (sched_is_energy_aware() && !sge) {
529 pr_warn("Invalid sched_group_energy for CPU%d\n", cpu);
530 return NULL;
531 }
532
533 return sge;
Dietmar Eggemann61100bd2014-11-14 17:16:41 +0000534}
535
Guenter Roeckb6220ad2014-06-24 18:05:29 -0700536static inline int cpu_corepower_flags(void)
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200537{
Morten Rasmussen858d7182015-01-13 13:50:46 +0000538 return SD_SHARE_PKG_RESOURCES | SD_SHARE_POWERDOMAIN | \
539 SD_SHARE_CAP_STATES;
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200540}
541
542static struct sched_domain_topology_level arm_topology[] = {
543#ifdef CONFIG_SCHED_MC
Dietmar Eggemann61100bd2014-11-14 17:16:41 +0000544 { cpu_coregroup_mask, cpu_corepower_flags, cpu_core_energy, SD_INIT_NAME(MC) },
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200545#endif
Dietmar Eggemann61100bd2014-11-14 17:16:41 +0000546 { cpu_cpu_mask, NULL, cpu_cluster_energy, SD_INIT_NAME(DIE) },
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200547 { NULL, },
548};
549
Srinivas Ramana3ddfb502016-06-28 12:02:28 +0530550static void __init reset_cpu_topology(void)
551{
552 unsigned int cpu;
553
554 for_each_possible_cpu(cpu) {
555 struct cputopo_arm *cpu_topo = &cpu_topology[cpu];
556
557 cpu_topo->thread_id = -1;
558 cpu_topo->core_id = -1;
559 cpu_topo->socket_id = -1;
560
561 cpumask_clear(&cpu_topo->core_sibling);
562 cpumask_clear(&cpu_topo->thread_sibling);
563 }
564 smp_wmb();
565}
566
567static void __init reset_cpu_capacity(void)
568{
569 unsigned int cpu;
570
571 for_each_possible_cpu(cpu)
572 set_capacity_scale(cpu, SCHED_CAPACITY_SCALE);
573}
574
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100575/*
576 * init_cpu_topology is called at boot when only one cpu is running
577 * which prevent simultaneous write access to cpu_topology array
578 */
Venkatraman Sathiyamoorthyf7e416e2012-08-03 07:58:33 +0100579void __init init_cpu_topology(void)
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100580{
581 unsigned int cpu;
582
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400583 /* init core mask and capacity */
Srinivas Ramana3ddfb502016-06-28 12:02:28 +0530584 reset_cpu_topology();
585 reset_cpu_capacity();
586 smp_wmb(); /* Ensure CPU topology and capacity are up to date */
Vincent Guittot339ca092012-07-10 14:13:12 +0100587
Jeevan Shriram490837a2017-03-01 17:52:49 -0800588 if (parse_dt_topology()) {
Srinivas Ramana3ddfb502016-06-28 12:02:28 +0530589 reset_cpu_topology();
590 reset_cpu_capacity();
Jeevan Shriram490837a2017-03-01 17:52:49 -0800591 }
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200592
Srivatsa Vaddagiri7cb075f2015-04-20 12:35:48 +0530593 for_each_possible_cpu(cpu)
594 update_siblings_masks(cpu);
595
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200596 /* Set scheduler topology descriptor */
597 set_sched_topology(arm_topology);
Maria Yue5a586c2018-02-13 12:59:23 +0800598 init_sched_energy_costs();
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100599}