blob: 60e375ce1ab2c4f5ad9c773951c4e068f72f2c1b [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
Juri Lelli615ffd62017-05-31 17:59:30 +010014#include <linux/arch_topology.h>
Vincent Guittotc9018aa2011-08-08 13:21:59 +010015#include <linux/cpu.h>
Juri Lelli06073ee2016-11-06 01:34:15 +010016#include <linux/cpufreq.h>
Vincent Guittotc9018aa2011-08-08 13:21:59 +010017#include <linux/cpumask.h>
Arnd Bergmann92bdd3f2013-05-31 22:49:22 +010018#include <linux/export.h>
Vincent Guittotc9018aa2011-08-08 13:21:59 +010019#include <linux/init.h>
20#include <linux/percpu.h>
21#include <linux/node.h>
22#include <linux/nodemask.h>
Vincent Guittot339ca092012-07-10 14:13:12 +010023#include <linux/of.h>
Vincent Guittotc9018aa2011-08-08 13:21:59 +010024#include <linux/sched.h>
Ingo Molnar105ab3d2017-02-01 16:36:40 +010025#include <linux/sched/topology.h>
Vincent Guittot339ca092012-07-10 14:13:12 +010026#include <linux/slab.h>
Juri Lelli7e5930a2016-11-06 01:34:31 +010027#include <linux/string.h>
Vincent Guittotc9018aa2011-08-08 13:21:59 +010028
Juri Lelli7e5930a2016-11-06 01:34:31 +010029#include <asm/cpu.h>
Vincent Guittotc9018aa2011-08-08 13:21:59 +010030#include <asm/cputype.h>
31#include <asm/topology.h>
32
Vincent Guittot130d9aa2012-07-10 14:08:40 +010033/*
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040034 * cpu capacity scale management
Vincent Guittot130d9aa2012-07-10 14:08:40 +010035 */
36
37/*
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040038 * cpu capacity table
Vincent Guittot130d9aa2012-07-10 14:08:40 +010039 * This per cpu data structure describes the relative capacity of each core.
40 * On a heteregenous system, cores don't have the same computation capacity
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040041 * and we reflect that difference in the cpu_capacity field so the scheduler
42 * can take this difference into account during load balance. A per cpu
43 * structure is preferred because each CPU updates its own cpu_capacity field
44 * during the load balance except for idle cores. One idle core is selected
45 * to run the rebalance_domains for all idle cores and the cpu_capacity can be
46 * updated during this sequence.
Vincent Guittot130d9aa2012-07-10 14:08:40 +010047 */
Vincent Guittot130d9aa2012-07-10 14:08:40 +010048
Vincent Guittot339ca092012-07-10 14:13:12 +010049#ifdef CONFIG_OF
50struct cpu_efficiency {
51 const char *compatible;
52 unsigned long efficiency;
53};
54
55/*
56 * Table of relative efficiency of each processors
57 * The efficiency value must fit in 20bit and the final
58 * cpu_scale value must be in the range
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040059 * 0 < cpu_scale < 3*SCHED_CAPACITY_SCALE/2
Vincent Guittot339ca092012-07-10 14:13:12 +010060 * in order to return at most 1 when DIV_ROUND_CLOSEST
61 * is used to compute the capacity of a CPU.
62 * Processors that are not defined in the table,
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040063 * use the default SCHED_CAPACITY_SCALE value for cpu_scale.
Vincent Guittot339ca092012-07-10 14:13:12 +010064 */
Mark Brown145bc292013-12-10 12:10:17 +010065static const struct cpu_efficiency table_efficiency[] = {
Vincent Guittot339ca092012-07-10 14:13:12 +010066 {"arm,cortex-a15", 3891},
67 {"arm,cortex-a7", 2048},
68 {NULL, },
69};
70
Mark Brown145bc292013-12-10 12:10:17 +010071static unsigned long *__cpu_capacity;
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +010072#define cpu_capacity(cpu) __cpu_capacity[cpu]
Vincent Guittot339ca092012-07-10 14:13:12 +010073
Mark Brown145bc292013-12-10 12:10:17 +010074static unsigned long middle_capacity = 1;
Juri Lelli06073ee2016-11-06 01:34:15 +010075static bool cap_from_dt = true;
Vincent Guittot339ca092012-07-10 14:13:12 +010076
77/*
78 * Iterate all CPUs' descriptor in DT and compute the efficiency
79 * (as per table_efficiency). Also calculate a middle efficiency
80 * as close as possible to (max{eff_i} - min{eff_i}) / 2
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -040081 * This is later used to scale the cpu_capacity field such that an
82 * 'average' CPU is of middle capacity. Also see the comments near
83 * table_efficiency[] and update_cpu_capacity().
Vincent Guittot339ca092012-07-10 14:13:12 +010084 */
85static void __init parse_dt_topology(void)
86{
Mark Brown145bc292013-12-10 12:10:17 +010087 const struct cpu_efficiency *cpu_eff;
Vincent Guittot339ca092012-07-10 14:13:12 +010088 struct device_node *cn = NULL;
Mark Brown44ae9032014-03-20 15:16:54 +010089 unsigned long min_capacity = ULONG_MAX;
Vincent Guittot339ca092012-07-10 14:13:12 +010090 unsigned long max_capacity = 0;
91 unsigned long capacity = 0;
Mark Brown44ae9032014-03-20 15:16:54 +010092 int cpu = 0;
Vincent Guittot339ca092012-07-10 14:13:12 +010093
Mark Brown44ae9032014-03-20 15:16:54 +010094 __cpu_capacity = kcalloc(nr_cpu_ids, sizeof(*__cpu_capacity),
95 GFP_NOWAIT);
Vincent Guittot339ca092012-07-10 14:13:12 +010096
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +010097 for_each_possible_cpu(cpu) {
98 const u32 *rate;
Vincent Guittot339ca092012-07-10 14:13:12 +010099 int len;
100
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +0100101 /* too early to use cpu->of_node */
102 cn = of_get_cpu_node(cpu, NULL);
103 if (!cn) {
104 pr_err("missing device node for CPU %d\n", cpu);
105 continue;
106 }
Vincent Guittot339ca092012-07-10 14:13:12 +0100107
Juri Lelli4ca4f262017-05-31 17:59:31 +0100108 if (topology_parse_cpu_capacity(cn, cpu)) {
Juri Lelli06073ee2016-11-06 01:34:15 +0100109 of_node_put(cn);
110 continue;
111 }
112
113 cap_from_dt = false;
114
Vincent Guittot339ca092012-07-10 14:13:12 +0100115 for (cpu_eff = table_efficiency; cpu_eff->compatible; cpu_eff++)
116 if (of_device_is_compatible(cn, cpu_eff->compatible))
117 break;
118
119 if (cpu_eff->compatible == NULL)
120 continue;
121
122 rate = of_get_property(cn, "clock-frequency", &len);
123 if (!rate || len != 4) {
Rob Herringa8e65e02017-07-21 14:28:32 -0500124 pr_err("%pOF missing clock-frequency property\n", cn);
Vincent Guittot339ca092012-07-10 14:13:12 +0100125 continue;
126 }
127
Vincent Guittot339ca092012-07-10 14:13:12 +0100128 capacity = ((be32_to_cpup(rate)) >> 20) * cpu_eff->efficiency;
129
130 /* Save min capacity of the system */
131 if (capacity < min_capacity)
132 min_capacity = capacity;
133
134 /* Save max capacity of the system */
135 if (capacity > max_capacity)
136 max_capacity = capacity;
137
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +0100138 cpu_capacity(cpu) = capacity;
Vincent Guittot339ca092012-07-10 14:13:12 +0100139 }
140
Vincent Guittot339ca092012-07-10 14:13:12 +0100141 /* If min and max capacities are equals, we bypass the update of the
142 * cpu_scale because all CPUs have the same capacity. Otherwise, we
143 * compute a middle_capacity factor that will ensure that the capacity
144 * of an 'average' CPU of the system will be as close as possible to
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400145 * SCHED_CAPACITY_SCALE, which is the default value, but with the
Vincent Guittot339ca092012-07-10 14:13:12 +0100146 * constraint explained near table_efficiency[].
147 */
Sudeep KarkadaNagesha816a8de2013-06-17 14:20:00 +0100148 if (4*max_capacity < (3*(max_capacity + min_capacity)))
Vincent Guittot339ca092012-07-10 14:13:12 +0100149 middle_capacity = (min_capacity + max_capacity)
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400150 >> (SCHED_CAPACITY_SHIFT+1);
Vincent Guittot339ca092012-07-10 14:13:12 +0100151 else
152 middle_capacity = ((max_capacity / 3)
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400153 >> (SCHED_CAPACITY_SHIFT-1)) + 1;
Vincent Guittot339ca092012-07-10 14:13:12 +0100154
Juri Lellic105aa32017-05-31 17:59:29 +0100155 if (cap_from_dt)
Juri Lelli4ca4f262017-05-31 17:59:31 +0100156 topology_normalize_cpu_scale();
Vincent Guittot339ca092012-07-10 14:13:12 +0100157}
158
159/*
160 * Look for a customed capacity of a CPU in the cpu_capacity table during the
161 * boot. The update of all CPUs is in O(n^2) for heteregeneous system but the
162 * function returns directly for SMP system.
163 */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400164static void update_cpu_capacity(unsigned int cpu)
Vincent Guittot339ca092012-07-10 14:13:12 +0100165{
Juri Lelli06073ee2016-11-06 01:34:15 +0100166 if (!cpu_capacity(cpu) || cap_from_dt)
Vincent Guittot339ca092012-07-10 14:13:12 +0100167 return;
168
Juri Lelli4ca4f262017-05-31 17:59:31 +0100169 topology_set_cpu_scale(cpu, cpu_capacity(cpu) / middle_capacity);
Vincent Guittot339ca092012-07-10 14:13:12 +0100170
Russell King4ed89f22014-10-28 11:26:42 +0000171 pr_info("CPU%u: update cpu_capacity %lu\n",
Juri Lelli4ca4f262017-05-31 17:59:31 +0100172 cpu, topology_get_cpu_scale(NULL, cpu));
Vincent Guittot339ca092012-07-10 14:13:12 +0100173}
174
175#else
176static inline void parse_dt_topology(void) {}
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400177static inline void update_cpu_capacity(unsigned int cpuid) {}
Vincent Guittot339ca092012-07-10 14:13:12 +0100178#endif
179
Lorenzo Pieralisidca463da2012-11-15 17:30:32 +0000180 /*
Vincent Guittot130d9aa2012-07-10 14:08:40 +0100181 * cpu topology table
182 */
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100183struct cputopo_arm cpu_topology[NR_CPUS];
Arnd Bergmann92bdd3f2013-05-31 22:49:22 +0100184EXPORT_SYMBOL_GPL(cpu_topology);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100185
Vincent Guittot4cbd6b12011-11-29 15:50:20 +0100186const struct cpumask *cpu_coregroup_mask(int cpu)
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100187{
188 return &cpu_topology[cpu].core_sibling;
189}
190
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200191/*
192 * The current assumption is that we can power gate each core independently.
193 * This will be superseded by DT binding once available.
194 */
195const struct cpumask *cpu_corepower_mask(int cpu)
196{
197 return &cpu_topology[cpu].thread_sibling;
198}
199
Mark Brown145bc292013-12-10 12:10:17 +0100200static void update_siblings_masks(unsigned int cpuid)
Vincent Guittotcb75dac2012-07-10 14:11:11 +0100201{
202 struct cputopo_arm *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
203 int cpu;
204
205 /* update core and thread sibling masks */
206 for_each_possible_cpu(cpu) {
207 cpu_topo = &cpu_topology[cpu];
208
209 if (cpuid_topo->socket_id != cpu_topo->socket_id)
210 continue;
211
212 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
213 if (cpu != cpuid)
214 cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
215
216 if (cpuid_topo->core_id != cpu_topo->core_id)
217 continue;
218
219 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
220 if (cpu != cpuid)
221 cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
222 }
223 smp_wmb();
224}
225
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100226/*
227 * store_cpu_topology is called at boot when only one cpu is running
228 * and with the mutex cpu_hotplug.lock locked, when several cpus have booted,
229 * which prevents simultaneous write access to cpu_topology array
230 */
231void store_cpu_topology(unsigned int cpuid)
232{
233 struct cputopo_arm *cpuid_topo = &cpu_topology[cpuid];
234 unsigned int mpidr;
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100235
236 /* If the cpu topology has been already set, just return */
237 if (cpuid_topo->core_id != -1)
238 return;
239
240 mpidr = read_cpuid_mpidr();
241
242 /* create cpu topology mapping */
243 if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) {
244 /*
245 * This is a multiprocessor system
246 * multiprocessor format & multiprocessor mode field are set
247 */
248
249 if (mpidr & MPIDR_MT_BITMASK) {
250 /* core performance interdependency */
Lorenzo Pieralisi71db5bf2012-11-16 15:24:06 +0000251 cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
252 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
253 cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100254 } else {
255 /* largely independent cores */
256 cpuid_topo->thread_id = -1;
Lorenzo Pieralisi71db5bf2012-11-16 15:24:06 +0000257 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
258 cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100259 }
260 } else {
261 /*
262 * This is an uniprocessor system
263 * we are in multiprocessor format but uniprocessor system
264 * or in the old uniprocessor format
265 */
266 cpuid_topo->thread_id = -1;
267 cpuid_topo->core_id = 0;
268 cpuid_topo->socket_id = -1;
269 }
270
Vincent Guittotcb75dac2012-07-10 14:11:11 +0100271 update_siblings_masks(cpuid);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100272
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400273 update_cpu_capacity(cpuid);
Vincent Guittot339ca092012-07-10 14:13:12 +0100274
Russell King4ed89f22014-10-28 11:26:42 +0000275 pr_info("CPU%u: thread %d, cpu %d, socket %d, mpidr %x\n",
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100276 cpuid, cpu_topology[cpuid].thread_id,
277 cpu_topology[cpuid].core_id,
278 cpu_topology[cpuid].socket_id, mpidr);
279}
280
Guenter Roeckb6220ad2014-06-24 18:05:29 -0700281static inline int cpu_corepower_flags(void)
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200282{
283 return SD_SHARE_PKG_RESOURCES | SD_SHARE_POWERDOMAIN;
284}
285
286static struct sched_domain_topology_level arm_topology[] = {
287#ifdef CONFIG_SCHED_MC
288 { cpu_corepower_mask, cpu_corepower_flags, SD_INIT_NAME(GMC) },
289 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
290#endif
291 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
292 { NULL, },
293};
294
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100295/*
296 * init_cpu_topology is called at boot when only one cpu is running
297 * which prevent simultaneous write access to cpu_topology array
298 */
Venkatraman Sathiyamoorthyf7e416e2012-08-03 07:58:33 +0100299void __init init_cpu_topology(void)
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100300{
301 unsigned int cpu;
302
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -0400303 /* init core mask and capacity */
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100304 for_each_possible_cpu(cpu) {
305 struct cputopo_arm *cpu_topo = &(cpu_topology[cpu]);
306
307 cpu_topo->thread_id = -1;
308 cpu_topo->core_id = -1;
309 cpu_topo->socket_id = -1;
310 cpumask_clear(&cpu_topo->core_sibling);
311 cpumask_clear(&cpu_topo->thread_sibling);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100312 }
313 smp_wmb();
Vincent Guittot339ca092012-07-10 14:13:12 +0100314
315 parse_dt_topology();
Vincent Guittotfb2aa852014-04-11 11:44:41 +0200316
317 /* Set scheduler topology descriptor */
318 set_sched_topology(arm_topology);
Vincent Guittotc9018aa2011-08-08 13:21:59 +0100319}