2 * arch/arm/kernel/topology.c
4 * Copyright (C) 2011 Linaro Limited.
5 * Written by: Vincent Guittot
7 * based on 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/arch_topology.h>
15 #include <linux/cpu.h>
16 #include <linux/cpufreq.h>
17 #include <linux/cpumask.h>
18 #include <linux/export.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/string.h>
30 #include <asm/cputype.h>
31 #include <asm/topology.h>
34 * cpu capacity scale management
39 * 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
41 * 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.
50 struct cpu_efficiency
{
51 const char *compatible
;
52 unsigned long efficiency
;
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
59 * 0 < cpu_scale < 3*SCHED_CAPACITY_SCALE/2
60 * 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,
63 * use the default SCHED_CAPACITY_SCALE value for cpu_scale.
65 static const struct cpu_efficiency table_efficiency
[] = {
66 {"arm,cortex-a15", 3891},
67 {"arm,cortex-a7", 2048},
71 static unsigned long *__cpu_capacity
;
72 #define cpu_capacity(cpu) __cpu_capacity[cpu]
74 static unsigned long middle_capacity
= 1;
75 static bool cap_from_dt
= true;
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
81 * 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().
85 static void __init
parse_dt_topology(void)
87 const struct cpu_efficiency
*cpu_eff
;
88 struct device_node
*cn
= NULL
;
89 unsigned long min_capacity
= ULONG_MAX
;
90 unsigned long max_capacity
= 0;
91 unsigned long capacity
= 0;
94 __cpu_capacity
= kcalloc(nr_cpu_ids
, sizeof(*__cpu_capacity
),
97 for_each_possible_cpu(cpu
) {
101 /* too early to use cpu->of_node */
102 cn
= of_get_cpu_node(cpu
, NULL
);
104 pr_err("missing device node for CPU %d\n", cpu
);
108 if (topology_parse_cpu_capacity(cn
, cpu
)) {
115 for (cpu_eff
= table_efficiency
; cpu_eff
->compatible
; cpu_eff
++)
116 if (of_device_is_compatible(cn
, cpu_eff
->compatible
))
119 if (cpu_eff
->compatible
== NULL
)
122 rate
= of_get_property(cn
, "clock-frequency", &len
);
123 if (!rate
|| len
!= 4) {
124 pr_err("%pOF missing clock-frequency property\n", cn
);
128 capacity
= ((be32_to_cpup(rate
)) >> 20) * cpu_eff
->efficiency
;
130 /* Save min capacity of the system */
131 if (capacity
< min_capacity
)
132 min_capacity
= capacity
;
134 /* Save max capacity of the system */
135 if (capacity
> max_capacity
)
136 max_capacity
= capacity
;
138 cpu_capacity(cpu
) = capacity
;
141 /* 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
145 * SCHED_CAPACITY_SCALE, which is the default value, but with the
146 * constraint explained near table_efficiency[].
148 if (4*max_capacity
< (3*(max_capacity
+ min_capacity
)))
149 middle_capacity
= (min_capacity
+ max_capacity
)
150 >> (SCHED_CAPACITY_SHIFT
+1);
152 middle_capacity
= ((max_capacity
/ 3)
153 >> (SCHED_CAPACITY_SHIFT
-1)) + 1;
156 topology_normalize_cpu_scale();
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.
164 static void update_cpu_capacity(unsigned int cpu
)
166 if (!cpu_capacity(cpu
) || cap_from_dt
)
169 topology_set_cpu_scale(cpu
, cpu_capacity(cpu
) / middle_capacity
);
171 pr_info("CPU%u: update cpu_capacity %lu\n",
172 cpu
, topology_get_cpu_scale(cpu
));
176 static inline void parse_dt_topology(void) {}
177 static inline void update_cpu_capacity(unsigned int cpuid
) {}
183 struct cputopo_arm cpu_topology
[NR_CPUS
];
184 EXPORT_SYMBOL_GPL(cpu_topology
);
186 const struct cpumask
*cpu_coregroup_mask(int cpu
)
188 return &cpu_topology
[cpu
].core_sibling
;
192 * The current assumption is that we can power gate each core independently.
193 * This will be superseded by DT binding once available.
195 const struct cpumask
*cpu_corepower_mask(int cpu
)
197 return &cpu_topology
[cpu
].thread_sibling
;
200 static void update_siblings_masks(unsigned int cpuid
)
202 struct cputopo_arm
*cpu_topo
, *cpuid_topo
= &cpu_topology
[cpuid
];
205 /* update core and thread sibling masks */
206 for_each_possible_cpu(cpu
) {
207 cpu_topo
= &cpu_topology
[cpu
];
209 if (cpuid_topo
->socket_id
!= cpu_topo
->socket_id
)
212 cpumask_set_cpu(cpuid
, &cpu_topo
->core_sibling
);
214 cpumask_set_cpu(cpu
, &cpuid_topo
->core_sibling
);
216 if (cpuid_topo
->core_id
!= cpu_topo
->core_id
)
219 cpumask_set_cpu(cpuid
, &cpu_topo
->thread_sibling
);
221 cpumask_set_cpu(cpu
, &cpuid_topo
->thread_sibling
);
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
231 void store_cpu_topology(unsigned int cpuid
)
233 struct cputopo_arm
*cpuid_topo
= &cpu_topology
[cpuid
];
236 /* If the cpu topology has been already set, just return */
237 if (cpuid_topo
->core_id
!= -1)
240 mpidr
= read_cpuid_mpidr();
242 /* create cpu topology mapping */
243 if ((mpidr
& MPIDR_SMP_BITMASK
) == MPIDR_SMP_VALUE
) {
245 * This is a multiprocessor system
246 * multiprocessor format & multiprocessor mode field are set
249 if (mpidr
& MPIDR_MT_BITMASK
) {
250 /* core performance interdependency */
251 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);
255 /* largely independent cores */
256 cpuid_topo
->thread_id
= -1;
257 cpuid_topo
->core_id
= MPIDR_AFFINITY_LEVEL(mpidr
, 0);
258 cpuid_topo
->socket_id
= MPIDR_AFFINITY_LEVEL(mpidr
, 1);
262 * This is an uniprocessor system
263 * we are in multiprocessor format but uniprocessor system
264 * or in the old uniprocessor format
266 cpuid_topo
->thread_id
= -1;
267 cpuid_topo
->core_id
= 0;
268 cpuid_topo
->socket_id
= -1;
271 update_siblings_masks(cpuid
);
273 update_cpu_capacity(cpuid
);
275 pr_info("CPU%u: thread %d, cpu %d, socket %d, mpidr %x\n",
276 cpuid
, cpu_topology
[cpuid
].thread_id
,
277 cpu_topology
[cpuid
].core_id
,
278 cpu_topology
[cpuid
].socket_id
, mpidr
);
281 static inline int cpu_corepower_flags(void)
283 return SD_SHARE_PKG_RESOURCES
| SD_SHARE_POWERDOMAIN
;
286 static 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
) },
291 { cpu_cpu_mask
, SD_INIT_NAME(DIE
) },
296 * init_cpu_topology is called at boot when only one cpu is running
297 * which prevent simultaneous write access to cpu_topology array
299 void __init
init_cpu_topology(void)
303 /* init core mask and capacity */
304 for_each_possible_cpu(cpu
) {
305 struct cputopo_arm
*cpu_topo
= &(cpu_topology
[cpu
]);
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
);
317 /* Set scheduler topology descriptor */
318 set_sched_topology(arm_topology
);