blob: b27bef5fc99234accee1f3381a172f7edf992345 [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>
Vincent Guittotc9018aa2011-08-08 13:21:59 +010024
25#include <asm/cputype.h>
26#include <asm/topology.h>
27
Vincent Guittot130d9aa2012-07-10 14:08:40 +010028/*
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040029 * cpu capacity scale management
Vincent Guittot130d9aa2012-07-10 14:08:40 +010030 */
31
32/*
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040033 * cpu capacity table
Vincent Guittot130d9aa2012-07-10 14:08:40 +010034 * This per cpu data structure describes the relative capacity of each core.
35 * On a heteregenous system, cores don't have the same computation capacity
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040036 * and we reflect that difference in the cpu_capacity field so the scheduler
37 * can take this difference into account during load balance. A per cpu
38 * structure is preferred because each CPU updates its own cpu_capacity field
39 * during the load balance except for idle cores. One idle core is selected
40 * to run the rebalance_domains for all idle cores and the cpu_capacity can be
41 * updated during this sequence.
Vincent Guittot130d9aa2012-07-10 14:08:40 +010042 */
Juri Lellid78e13a2016-01-07 16:27:33 +010043static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
Vincent Guittot130d9aa2012-07-10 14:08:40 +010044
Morten Rasmussen25cea242015-04-14 16:25:31 +010045unsigned long scale_cpu_capacity(struct sched_domain *sd, int cpu)
Vincent Guittot130d9aa2012-07-10 14:08:40 +010046{
Jon Medhurstaa9ea842016-06-02 12:18:08 +000047#ifdef CONFIG_CPU_FREQ
Dietmar Eggemann568913e2015-09-23 17:59:55 +010048 unsigned long max_freq_scale = cpufreq_scale_max_freq_capacity(cpu);
49
50 return per_cpu(cpu_scale, cpu) * max_freq_scale >> SCHED_CAPACITY_SHIFT;
51#else
Vincent Guittot130d9aa2012-07-10 14:08:40 +010052 return per_cpu(cpu_scale, cpu);
Dietmar Eggemann568913e2015-09-23 17:59:55 +010053#endif
Vincent Guittot130d9aa2012-07-10 14:08:40 +010054}
55
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040056static void set_capacity_scale(unsigned int cpu, unsigned long capacity)
Vincent Guittot130d9aa2012-07-10 14:08:40 +010057{
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040058 per_cpu(cpu_scale, cpu) = capacity;
Vincent Guittot130d9aa2012-07-10 14:08:40 +010059}
60
Jeevan Shriram490837a2017-03-01 17:52:49 -080061static int __init get_cpu_for_node(struct device_node *node)
62{
63 struct device_node *cpu_node;
64 int cpu;
65
66 cpu_node = of_parse_phandle(node, "cpu", 0);
67 if (!cpu_node)
68 return -EINVAL;
69
70 for_each_possible_cpu(cpu) {
71 if (of_get_cpu_node(cpu, NULL) == cpu_node) {
72 of_node_put(cpu_node);
73 return cpu;
74 }
75 }
76
77 pr_crit("Unable to find CPU node for %s\n", cpu_node->full_name);
78
79 of_node_put(cpu_node);
80 return -EINVAL;
81}
82
83static int __init parse_core(struct device_node *core, int cluster_id,
84 int core_id)
85{
86 char name[10];
87 bool leaf = true;
88 int i = 0;
89 int cpu;
90 struct device_node *t;
91
92 do {
93 snprintf(name, sizeof(name), "thread%d", i);
94 t = of_get_child_by_name(core, name);
95 if (t) {
96 leaf = false;
97 cpu = get_cpu_for_node(t);
98 if (cpu >= 0) {
99 cpu_topology[cpu].socket_id = cluster_id;
100 cpu_topology[cpu].core_id = core_id;
101 cpu_topology[cpu].thread_id = i;
102 } else {
103 pr_err("%s: Can't get CPU for thread\n",
104 t->full_name);
105 of_node_put(t);
106 return -EINVAL;
107 }
108 of_node_put(t);
109 }
110 i++;
111 } while (t);
112
113 cpu = get_cpu_for_node(core);
114 if (cpu >= 0) {
115 if (!leaf) {
116 pr_err("%s: Core has both threads and CPU\n",
117 core->full_name);
118 return -EINVAL;
119 }
120
121 cpu_topology[cpu].socket_id = cluster_id;
122 cpu_topology[cpu].core_id = core_id;
123 } else if (leaf) {
124 pr_err("%s: Can't get CPU for leaf core\n", core->full_name);
125 return -EINVAL;
126 }
127
128 return 0;
129}
130
131static int __init parse_cluster(struct device_node *cluster, int depth)
132{
133 static int cluster_id __initdata;
134 char name[10];
135 bool leaf = true;
136 bool has_cores = false;
137 struct device_node *c;
138 int core_id = 0;
139 int i, ret;
140
141 /*
142 * First check for child clusters; we currently ignore any
143 * information about the nesting of clusters and present the
144 * scheduler with a flat list of them.
145 */
146 i = 0;
147 do {
148 snprintf(name, sizeof(name), "cluster%d", i);
149 c = of_get_child_by_name(cluster, name);
150 if (c) {
151 leaf = false;
152 ret = parse_cluster(c, depth + 1);
153 of_node_put(c);
154 if (ret != 0)
155 return ret;
156 }
157 i++;
158 } while (c);
159
160 /* Now check for cores */
161 i = 0;
162 do {
163 snprintf(name, sizeof(name), "core%d", i);
164 c = of_get_child_by_name(cluster, name);
165 if (c) {
166 has_cores = true;
167
168 if (depth == 0) {
169 pr_err("%s: cpu-map children should be clusters\n",
170 c->full_name);
171 of_node_put(c);
172 return -EINVAL;
173 }
174
175 if (leaf) {
176 ret = parse_core(c, cluster_id, core_id++);
177 } else {
178 pr_err("%s: Non-leaf cluster with core %s\n",
179 cluster->full_name, name);
180 ret = -EINVAL;
181 }
182
183 of_node_put(c);
184 if (ret != 0)
185 return ret;
186 }
187 i++;
188 } while (c);
189
190 if (leaf && !has_cores)
191 pr_warn("%s: empty cluster\n", cluster->full_name);
192
193 if (leaf)
194 cluster_id++;
195
196 return 0;
197}
198
Vincent Guittot339ca092012-07-10 14:13:12 +0100199#ifdef CONFIG_OF
200struct cpu_efficiency {
201 const char *compatible;
202 unsigned long efficiency;
203};
204
205/*
206 * Table of relative efficiency of each processors
207 * The efficiency value must fit in 20bit and the final
208 * cpu_scale value must be in the range
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400209 * 0 < cpu_scale < 3*SCHED_CAPACITY_SCALE/2
Vincent Guittot339ca092012-07-10 14:13:12 +0100210 * in order to return at most 1 when DIV_ROUND_CLOSEST
211 * is used to compute the capacity of a CPU.
212 * Processors that are not defined in the table,
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400213 * use the default SCHED_CAPACITY_SCALE value for cpu_scale.
Vincent Guittot339ca092012-07-10 14:13:12 +0100214 */
Mark Brown145bc292013-12-10 12:10:17 +0100215static const struct cpu_efficiency table_efficiency[] = {
Vincent Guittot339ca092012-07-10 14:13:12 +0100216 {"arm,cortex-a15", 3891},
217 {"arm,cortex-a7", 2048},
218 {NULL, },
219};
220
Mark Brown145bc292013-12-10 12:10:17 +0100221static unsigned long *__cpu_capacity;
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +0100222#define cpu_capacity(cpu) __cpu_capacity[cpu]
Vincent Guittot339ca092012-07-10 14:13:12 +0100223
Mark Brown145bc292013-12-10 12:10:17 +0100224static unsigned long middle_capacity = 1;
Vincent Guittot339ca092012-07-10 14:13:12 +0100225
226/*
227 * Iterate all CPUs' descriptor in DT and compute the efficiency
228 * (as per table_efficiency). Also calculate a middle efficiency
229 * as close as possible to (max{eff_i} - min{eff_i}) / 2
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400230 * This is later used to scale the cpu_capacity field such that an
231 * 'average' CPU is of middle capacity. Also see the comments near
232 * table_efficiency[] and update_cpu_capacity().
Vincent Guittot339ca092012-07-10 14:13:12 +0100233 */
Jeevan Shriram490837a2017-03-01 17:52:49 -0800234static int __init parse_dt_topology(void)
Vincent Guittot339ca092012-07-10 14:13:12 +0100235{
Mark Brown145bc292013-12-10 12:10:17 +0100236 const struct cpu_efficiency *cpu_eff;
Jeevan Shriram490837a2017-03-01 17:52:49 -0800237 struct device_node *cn = NULL, *map;
Mark Brown44ae9032014-03-20 15:16:54 +0100238 unsigned long min_capacity = ULONG_MAX;
Vincent Guittot339ca092012-07-10 14:13:12 +0100239 unsigned long max_capacity = 0;
240 unsigned long capacity = 0;
Jeevan Shriram490837a2017-03-01 17:52:49 -0800241 int cpu = 0, ret = 0;
242
243 cn = of_find_node_by_path("/cpus");
244 if (!cn) {
245 pr_err("No CPU information found in DT\n");
246 return 0;
247 }
248
249 /*
250 * When topology is provided cpu-map is essentially a root
251 * cluster with restricted subnodes.
252 */
253 map = of_get_child_by_name(cn, "cpu-map");
254 if (!map)
255 goto out;
256
257 ret = parse_cluster(map, 0);
258 if (ret != 0)
259 goto out_map;
260
261 /*
262 * Check that all cores are in the topology; the SMP code will
263 * only mark cores described in the DT as possible.
264 */
265 for_each_possible_cpu(cpu)
266 if (cpu_topology[cpu].socket_id == -1)
267 ret = -EINVAL;
Vincent Guittot339ca092012-07-10 14:13:12 +0100268
Mark Brown44ae9032014-03-20 15:16:54 +0100269 __cpu_capacity = kcalloc(nr_cpu_ids, sizeof(*__cpu_capacity),
270 GFP_NOWAIT);
Vincent Guittot339ca092012-07-10 14:13:12 +0100271
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +0100272 for_each_possible_cpu(cpu) {
273 const u32 *rate;
Vincent Guittot339ca092012-07-10 14:13:12 +0100274 int len;
275
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +0100276 /* too early to use cpu->of_node */
277 cn = of_get_cpu_node(cpu, NULL);
278 if (!cn) {
279 pr_err("missing device node for CPU %d\n", cpu);
280 continue;
281 }
Vincent Guittot339ca092012-07-10 14:13:12 +0100282
283 for (cpu_eff = table_efficiency; cpu_eff->compatible; cpu_eff++)
284 if (of_device_is_compatible(cn, cpu_eff->compatible))
285 break;
286
287 if (cpu_eff->compatible == NULL)
288 continue;
289
290 rate = of_get_property(cn, "clock-frequency", &len);
291 if (!rate || len != 4) {
292 pr_err("%s missing clock-frequency property\n",
293 cn->full_name);
294 continue;
295 }
296
Vincent Guittot339ca092012-07-10 14:13:12 +0100297 capacity = ((be32_to_cpup(rate)) >> 20) * cpu_eff->efficiency;
298
299 /* Save min capacity of the system */
300 if (capacity < min_capacity)
301 min_capacity = capacity;
302
303 /* Save max capacity of the system */
304 if (capacity > max_capacity)
305 max_capacity = capacity;
306
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +0100307 cpu_capacity(cpu) = capacity;
Vincent Guittot339ca092012-07-10 14:13:12 +0100308 }
309
Vincent Guittot339ca092012-07-10 14:13:12 +0100310 /* If min and max capacities are equals, we bypass the update of the
311 * cpu_scale because all CPUs have the same capacity. Otherwise, we
312 * compute a middle_capacity factor that will ensure that the capacity
313 * of an 'average' CPU of the system will be as close as possible to
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400314 * SCHED_CAPACITY_SCALE, which is the default value, but with the
Vincent Guittot339ca092012-07-10 14:13:12 +0100315 * constraint explained near table_efficiency[].
316 */
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +0100317 if (4*max_capacity < (3*(max_capacity + min_capacity)))
Vincent Guittot339ca092012-07-10 14:13:12 +0100318 middle_capacity = (min_capacity + max_capacity)
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400319 >> (SCHED_CAPACITY_SHIFT+1);
Vincent Guittot339ca092012-07-10 14:13:12 +0100320 else
321 middle_capacity = ((max_capacity / 3)
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400322 >> (SCHED_CAPACITY_SHIFT-1)) + 1;
Jeevan Shriram490837a2017-03-01 17:52:49 -0800323out_map:
324 of_node_put(map);
325out:
326 of_node_put(cn);
327 return ret;
Vincent Guittot339ca092012-07-10 14:13:12 +0100328}
329
Dietmar Eggemannb4ca4bc2015-07-10 13:57:19 +0100330static const struct sched_group_energy * const cpu_core_energy(int cpu);
331
Vincent Guittot339ca092012-07-10 14:13:12 +0100332/*
333 * Look for a customed capacity of a CPU in the cpu_capacity table during the
334 * boot. The update of all CPUs is in O(n^2) for heteregeneous system but the
335 * function returns directly for SMP system.
336 */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400337static void update_cpu_capacity(unsigned int cpu)
Vincent Guittot339ca092012-07-10 14:13:12 +0100338{
Dietmar Eggemannb4ca4bc2015-07-10 13:57:19 +0100339 unsigned long capacity = SCHED_CAPACITY_SCALE;
Vincent Guittot339ca092012-07-10 14:13:12 +0100340
Dietmar Eggemannb4ca4bc2015-07-10 13:57:19 +0100341 if (cpu_core_energy(cpu)) {
342 int max_cap_idx = cpu_core_energy(cpu)->nr_cap_states - 1;
343 capacity = cpu_core_energy(cpu)->cap_states[max_cap_idx].cap;
344 }
345
346 set_capacity_scale(cpu, capacity);
Vincent Guittot339ca092012-07-10 14:13:12 +0100347
Russell King4ed89f22014-10-28 11:26:42 +0000348 pr_info("CPU%u: update cpu_capacity %lu\n",
Vincent Guittotd3bfca12014-08-26 13:06:48 +0200349 cpu, arch_scale_cpu_capacity(NULL, cpu));
Vincent Guittot339ca092012-07-10 14:13:12 +0100350}
351
352#else
Jeevan Shriram490837a2017-03-01 17:52:49 -0800353static inline int parse_dt_topology(void) {}
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400354static inline void update_cpu_capacity(unsigned int cpuid) {}
Vincent Guittot339ca092012-07-10 14:13:12 +0100355#endif
356
Lorenzo Pieralisidca463d2012-11-15 17:30:32 +0000357 /*
Vincent Guittot130d9aa2012-07-10 14:08:40 +0100358 * cpu topology table
359 */
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100360struct cputopo_arm cpu_topology[NR_CPUS];
Arnd Bergmann92bdd3f2013-05-31 22:49:22 +0100361EXPORT_SYMBOL_GPL(cpu_topology);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100362
Vincent Guittot4cbd6b12011-11-29 15:50:20 +0100363const struct cpumask *cpu_coregroup_mask(int cpu)
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100364{
365 return &cpu_topology[cpu].core_sibling;
366}
367
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200368/*
369 * The current assumption is that we can power gate each core independently.
370 * This will be superseded by DT binding once available.
371 */
372const struct cpumask *cpu_corepower_mask(int cpu)
373{
374 return &cpu_topology[cpu].thread_sibling;
375}
376
Mark Brown145bc292013-12-10 12:10:17 +0100377static void update_siblings_masks(unsigned int cpuid)
Vincent Guittotcb75dac2012-07-10 14:11:11 +0100378{
379 struct cputopo_arm *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
380 int cpu;
381
382 /* update core and thread sibling masks */
383 for_each_possible_cpu(cpu) {
384 cpu_topo = &cpu_topology[cpu];
385
386 if (cpuid_topo->socket_id != cpu_topo->socket_id)
387 continue;
388
389 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
390 if (cpu != cpuid)
391 cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
392
393 if (cpuid_topo->core_id != cpu_topo->core_id)
394 continue;
395
396 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
397 if (cpu != cpuid)
398 cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
399 }
400 smp_wmb();
401}
402
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100403/*
404 * store_cpu_topology is called at boot when only one cpu is running
405 * and with the mutex cpu_hotplug.lock locked, when several cpus have booted,
406 * which prevents simultaneous write access to cpu_topology array
407 */
408void store_cpu_topology(unsigned int cpuid)
409{
410 struct cputopo_arm *cpuid_topo = &cpu_topology[cpuid];
411 unsigned int mpidr;
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100412
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100413 if (cpuid_topo->core_id != -1)
Jeevan Shriram490837a2017-03-01 17:52:49 -0800414 goto topology_populated;
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100415
416 mpidr = read_cpuid_mpidr();
417
418 /* create cpu topology mapping */
419 if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) {
420 /*
421 * This is a multiprocessor system
422 * multiprocessor format & multiprocessor mode field are set
423 */
424
425 if (mpidr & MPIDR_MT_BITMASK) {
426 /* core performance interdependency */
Lorenzo Pieralisi71db5bf2012-11-16 15:24:06 +0000427 cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
428 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
429 cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100430 } else {
431 /* largely independent cores */
432 cpuid_topo->thread_id = -1;
Lorenzo Pieralisi71db5bf2012-11-16 15:24:06 +0000433 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
434 cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100435 }
436 } else {
437 /*
438 * This is an uniprocessor system
439 * we are in multiprocessor format but uniprocessor system
440 * or in the old uniprocessor format
441 */
442 cpuid_topo->thread_id = -1;
443 cpuid_topo->core_id = 0;
444 cpuid_topo->socket_id = -1;
445 }
446
Jeevan Shriram490837a2017-03-01 17:52:49 -0800447 pr_info("CPU%u: thread %d, cpu %d, cluster %d, mpidr %x\n",
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100448 cpuid, cpu_topology[cpuid].thread_id,
449 cpu_topology[cpuid].core_id,
450 cpu_topology[cpuid].socket_id, mpidr);
Jeevan Shriram490837a2017-03-01 17:52:49 -0800451
452topology_populated:
453 update_siblings_masks(cpuid);
454 update_cpu_capacity(cpuid);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100455}
456
Dietmar Eggemann61100bd2014-11-14 17:16:41 +0000457/*
458 * ARM TC2 specific energy cost model data. There are no unit requirements for
459 * the data. Data can be normalized to any reference point, but the
460 * normalization must be consistent. That is, one bogo-joule/watt must be the
461 * same quantity for all data, but we don't care what it is.
462 */
463static struct idle_state idle_states_cluster_a7[] = {
464 { .power = 25 }, /* arch_cpu_idle() (active idle) = WFI */
465 { .power = 25 }, /* WFI */
466 { .power = 10 }, /* cluster-sleep-l */
467 };
468
469static struct idle_state idle_states_cluster_a15[] = {
470 { .power = 70 }, /* arch_cpu_idle() (active idle) = WFI */
471 { .power = 70 }, /* WFI */
472 { .power = 25 }, /* cluster-sleep-b */
473 };
474
475static struct capacity_state cap_states_cluster_a7[] = {
476 /* Cluster only power */
477 { .cap = 150, .power = 2967, }, /* 350 MHz */
478 { .cap = 172, .power = 2792, }, /* 400 MHz */
479 { .cap = 215, .power = 2810, }, /* 500 MHz */
480 { .cap = 258, .power = 2815, }, /* 600 MHz */
481 { .cap = 301, .power = 2919, }, /* 700 MHz */
482 { .cap = 344, .power = 2847, }, /* 800 MHz */
483 { .cap = 387, .power = 3917, }, /* 900 MHz */
484 { .cap = 430, .power = 4905, }, /* 1000 MHz */
485 };
486
487static struct capacity_state cap_states_cluster_a15[] = {
488 /* Cluster only power */
489 { .cap = 426, .power = 7920, }, /* 500 MHz */
490 { .cap = 512, .power = 8165, }, /* 600 MHz */
491 { .cap = 597, .power = 8172, }, /* 700 MHz */
492 { .cap = 682, .power = 8195, }, /* 800 MHz */
493 { .cap = 768, .power = 8265, }, /* 900 MHz */
494 { .cap = 853, .power = 8446, }, /* 1000 MHz */
495 { .cap = 938, .power = 11426, }, /* 1100 MHz */
496 { .cap = 1024, .power = 15200, }, /* 1200 MHz */
497 };
498
499static struct sched_group_energy energy_cluster_a7 = {
500 .nr_idle_states = ARRAY_SIZE(idle_states_cluster_a7),
501 .idle_states = idle_states_cluster_a7,
502 .nr_cap_states = ARRAY_SIZE(cap_states_cluster_a7),
503 .cap_states = cap_states_cluster_a7,
504};
505
506static struct sched_group_energy energy_cluster_a15 = {
507 .nr_idle_states = ARRAY_SIZE(idle_states_cluster_a15),
508 .idle_states = idle_states_cluster_a15,
509 .nr_cap_states = ARRAY_SIZE(cap_states_cluster_a15),
510 .cap_states = cap_states_cluster_a15,
511};
512
513static struct idle_state idle_states_core_a7[] = {
514 { .power = 0 }, /* arch_cpu_idle (active idle) = WFI */
515 { .power = 0 }, /* WFI */
516 { .power = 0 }, /* cluster-sleep-l */
517 };
518
519static struct idle_state idle_states_core_a15[] = {
520 { .power = 0 }, /* arch_cpu_idle (active idle) = WFI */
521 { .power = 0 }, /* WFI */
522 { .power = 0 }, /* cluster-sleep-b */
523 };
524
525static struct capacity_state cap_states_core_a7[] = {
526 /* Power per cpu */
527 { .cap = 150, .power = 187, }, /* 350 MHz */
528 { .cap = 172, .power = 275, }, /* 400 MHz */
529 { .cap = 215, .power = 334, }, /* 500 MHz */
530 { .cap = 258, .power = 407, }, /* 600 MHz */
531 { .cap = 301, .power = 447, }, /* 700 MHz */
532 { .cap = 344, .power = 549, }, /* 800 MHz */
533 { .cap = 387, .power = 761, }, /* 900 MHz */
534 { .cap = 430, .power = 1024, }, /* 1000 MHz */
535 };
536
537static struct capacity_state cap_states_core_a15[] = {
538 /* Power per cpu */
539 { .cap = 426, .power = 2021, }, /* 500 MHz */
540 { .cap = 512, .power = 2312, }, /* 600 MHz */
541 { .cap = 597, .power = 2756, }, /* 700 MHz */
542 { .cap = 682, .power = 3125, }, /* 800 MHz */
543 { .cap = 768, .power = 3524, }, /* 900 MHz */
544 { .cap = 853, .power = 3846, }, /* 1000 MHz */
545 { .cap = 938, .power = 5177, }, /* 1100 MHz */
546 { .cap = 1024, .power = 6997, }, /* 1200 MHz */
547 };
548
549static struct sched_group_energy energy_core_a7 = {
550 .nr_idle_states = ARRAY_SIZE(idle_states_core_a7),
551 .idle_states = idle_states_core_a7,
552 .nr_cap_states = ARRAY_SIZE(cap_states_core_a7),
553 .cap_states = cap_states_core_a7,
554};
555
556static struct sched_group_energy energy_core_a15 = {
557 .nr_idle_states = ARRAY_SIZE(idle_states_core_a15),
558 .idle_states = idle_states_core_a15,
559 .nr_cap_states = ARRAY_SIZE(cap_states_core_a15),
560 .cap_states = cap_states_core_a15,
561};
562
563/* sd energy functions */
564static inline
565const struct sched_group_energy * const cpu_cluster_energy(int cpu)
566{
567 return cpu_topology[cpu].socket_id ? &energy_cluster_a7 :
568 &energy_cluster_a15;
569}
570
571static inline
572const struct sched_group_energy * const cpu_core_energy(int cpu)
573{
574 return cpu_topology[cpu].socket_id ? &energy_core_a7 :
575 &energy_core_a15;
576}
577
Guenter Roeckb6220ad2014-06-24 18:05:29 -0700578static inline int cpu_corepower_flags(void)
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200579{
Morten Rasmussen858d7182015-01-13 13:50:46 +0000580 return SD_SHARE_PKG_RESOURCES | SD_SHARE_POWERDOMAIN | \
581 SD_SHARE_CAP_STATES;
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200582}
583
584static struct sched_domain_topology_level arm_topology[] = {
585#ifdef CONFIG_SCHED_MC
Dietmar Eggemann61100bd2014-11-14 17:16:41 +0000586 { cpu_coregroup_mask, cpu_corepower_flags, cpu_core_energy, SD_INIT_NAME(MC) },
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200587#endif
Dietmar Eggemann61100bd2014-11-14 17:16:41 +0000588 { cpu_cpu_mask, NULL, cpu_cluster_energy, SD_INIT_NAME(DIE) },
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200589 { NULL, },
590};
591
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100592/*
593 * init_cpu_topology is called at boot when only one cpu is running
594 * which prevent simultaneous write access to cpu_topology array
595 */
Venkatraman Sathiyamoorthyf7e416e2012-08-03 07:58:33 +0100596void __init init_cpu_topology(void)
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100597{
598 unsigned int cpu;
599
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400600 /* init core mask and capacity */
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100601 for_each_possible_cpu(cpu) {
602 struct cputopo_arm *cpu_topo = &(cpu_topology[cpu]);
603
604 cpu_topo->thread_id = -1;
605 cpu_topo->core_id = -1;
606 cpu_topo->socket_id = -1;
607 cpumask_clear(&cpu_topo->core_sibling);
608 cpumask_clear(&cpu_topo->thread_sibling);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100609 }
610 smp_wmb();
Vincent Guittot339ca092012-07-10 14:13:12 +0100611
Jeevan Shriram490837a2017-03-01 17:52:49 -0800612 if (parse_dt_topology()) {
613 struct cputopo_arm *cpu_topo = &(cpu_topology[cpu]);
614
615 cpu_topo->thread_id = -1;
616 cpu_topo->core_id = -1;
617 cpu_topo->socket_id = -1;
618 cpumask_clear(&cpu_topo->core_sibling);
619 cpumask_clear(&cpu_topo->thread_sibling);
620
621 set_capacity_scale(cpu, SCHED_CAPACITY_SCALE);
622 }
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200623
Srivatsa Vaddagiri7cb075f2015-04-20 12:35:48 +0530624 for_each_possible_cpu(cpu)
625 update_siblings_masks(cpu);
626
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200627 /* Set scheduler topology descriptor */
628 set_sched_topology(arm_topology);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100629}