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/cpu.h>
15 #include <linux/cpumask.h>
16 #include <linux/export.h>
17 #include <linux/init.h>
18 #include <linux/percpu.h>
19 #include <linux/node.h>
20 #include <linux/nodemask.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
25 #include <asm/cputype.h>
26 #include <asm/smp_plat.h>
27 #include <asm/topology.h>
30 * cpu power scale management
35 * 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
37 * and we reflect that difference in the cpu_power field so the scheduler can
38 * take this difference into account during load balance. A per cpu structure
39 * is preferred because each CPU updates its own cpu_power field during the
40 * load balance except for idle cores. One idle core is selected to run the
41 * rebalance_domains for all idle cores and the cpu_power can be updated
42 * during this sequence.
44 static DEFINE_PER_CPU(unsigned long, cpu_scale);
46 unsigned long arch_scale_freq_power(struct sched_domain *sd, int cpu)
48 return per_cpu(cpu_scale, cpu);
51 static void set_power_scale(unsigned int cpu, unsigned long power)
53 per_cpu(cpu_scale, cpu) = power;
57 struct cpu_efficiency {
58 const char *compatible;
59 unsigned long efficiency;
63 * Table of relative efficiency of each processors
64 * The efficiency value must fit in 20bit and the final
65 * cpu_scale value must be in the range
66 * 0 < cpu_scale < 3*SCHED_POWER_SCALE/2
67 * in order to return at most 1 when DIV_ROUND_CLOSEST
68 * is used to compute the capacity of a CPU.
69 * Processors that are not defined in the table,
70 * use the default SCHED_POWER_SCALE value for cpu_scale.
72 struct cpu_efficiency table_efficiency[] = {
73 {"arm,cortex-a15", 3891},
74 {"arm,cortex-a7", 2048},
80 unsigned long capacity;
83 struct cpu_capacity *cpu_capacity;
85 unsigned long middle_capacity = 1;
88 * Iterate all CPUs' descriptor in DT and compute the efficiency
89 * (as per table_efficiency). Also calculate a middle efficiency
90 * as close as possible to (max{eff_i} - min{eff_i}) / 2
91 * This is later used to scale the cpu_power field such that an
92 * 'average' CPU is of middle power. Also see the comments near
93 * table_efficiency[] and update_cpu_power().
95 static void __init parse_dt_topology(void)
97 struct cpu_efficiency *cpu_eff;
98 struct device_node *cn = NULL;
99 unsigned long min_capacity = (unsigned long)(-1);
100 unsigned long max_capacity = 0;
101 unsigned long capacity = 0;
102 int alloc_size, cpu = 0;
104 alloc_size = nr_cpu_ids * sizeof(struct cpu_capacity);
105 cpu_capacity = kzalloc(alloc_size, GFP_NOWAIT);
107 while ((cn = of_find_node_by_type(cn, "cpu"))) {
108 const u32 *rate, *reg;
111 if (cpu >= num_possible_cpus())
114 for (cpu_eff = table_efficiency; cpu_eff->compatible; cpu_eff++)
115 if (of_device_is_compatible(cn, cpu_eff->compatible))
118 if (cpu_eff->compatible == NULL)
121 rate = of_get_property(cn, "clock-frequency", &len);
122 if (!rate || len != 4) {
123 pr_err("%s missing clock-frequency property\n",
128 reg = of_get_property(cn, "reg", &len);
129 if (!reg || len != 4) {
130 pr_err("%s missing reg property\n", cn->full_name);
134 capacity = ((be32_to_cpup(rate)) >> 20) * cpu_eff->efficiency;
136 /* Save min capacity of the system */
137 if (capacity < min_capacity)
138 min_capacity = capacity;
140 /* Save max capacity of the system */
141 if (capacity > max_capacity)
142 max_capacity = capacity;
144 cpu_capacity[cpu].capacity = capacity;
145 cpu_capacity[cpu++].hwid = be32_to_cpup(reg);
148 if (cpu < num_possible_cpus())
149 cpu_capacity[cpu].hwid = (unsigned long)(-1);
151 /* If min and max capacities are equals, we bypass the update of the
152 * cpu_scale because all CPUs have the same capacity. Otherwise, we
153 * compute a middle_capacity factor that will ensure that the capacity
154 * of an 'average' CPU of the system will be as close as possible to
155 * SCHED_POWER_SCALE, which is the default value, but with the
156 * constraint explained near table_efficiency[].
158 if (min_capacity == max_capacity)
159 cpu_capacity[0].hwid = (unsigned long)(-1);
160 else if (4*max_capacity < (3*(max_capacity + min_capacity)))
161 middle_capacity = (min_capacity + max_capacity)
162 >> (SCHED_POWER_SHIFT+1);
164 middle_capacity = ((max_capacity / 3)
165 >> (SCHED_POWER_SHIFT-1)) + 1;
170 * Look for a customed capacity of a CPU in the cpu_capacity table during the
171 * boot. The update of all CPUs is in O(n^2) for heteregeneous system but the
172 * function returns directly for SMP system.
174 void update_cpu_power(unsigned int cpu, unsigned long hwid)
176 unsigned int idx = 0;
178 /* look for the cpu's hwid in the cpu capacity table */
179 for (idx = 0; idx < num_possible_cpus(); idx++) {
180 if (cpu_capacity[idx].hwid == hwid)
183 if (cpu_capacity[idx].hwid == -1)
187 if (idx == num_possible_cpus())
190 set_power_scale(cpu, cpu_capacity[idx].capacity / middle_capacity);
192 printk(KERN_INFO "CPU%u: update cpu_power %lu\n",
193 cpu, arch_scale_freq_power(NULL, cpu));
197 static inline void parse_dt_topology(void) {}
198 static inline void update_cpu_power(unsigned int cpuid, unsigned int mpidr) {}
204 struct cputopo_arm cpu_topology[NR_CPUS];
205 EXPORT_SYMBOL_GPL(cpu_topology);
207 const struct cpumask *cpu_coregroup_mask(int cpu)
209 return &cpu_topology[cpu].core_sibling;
212 void update_siblings_masks(unsigned int cpuid)
214 struct cputopo_arm *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
217 /* update core and thread sibling masks */
218 for_each_possible_cpu(cpu) {
219 cpu_topo = &cpu_topology[cpu];
221 if (cpuid_topo->socket_id != cpu_topo->socket_id)
224 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
226 cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
228 if (cpuid_topo->core_id != cpu_topo->core_id)
231 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
233 cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
239 * store_cpu_topology is called at boot when only one cpu is running
240 * and with the mutex cpu_hotplug.lock locked, when several cpus have booted,
241 * which prevents simultaneous write access to cpu_topology array
243 void store_cpu_topology(unsigned int cpuid)
245 struct cputopo_arm *cpuid_topo = &cpu_topology[cpuid];
248 /* If the cpu topology has been already set, just return */
249 if (cpuid_topo->core_id != -1)
252 mpidr = read_cpuid_mpidr();
254 /* create cpu topology mapping */
255 if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) {
257 * This is a multiprocessor system
258 * multiprocessor format & multiprocessor mode field are set
261 if (mpidr & MPIDR_MT_BITMASK) {
262 /* core performance interdependency */
263 cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
264 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
265 cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
267 /* largely independent cores */
268 cpuid_topo->thread_id = -1;
269 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
270 cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
274 * This is an uniprocessor system
275 * we are in multiprocessor format but uniprocessor system
276 * or in the old uniprocessor format
278 cpuid_topo->thread_id = -1;
279 cpuid_topo->core_id = 0;
280 cpuid_topo->socket_id = -1;
283 update_siblings_masks(cpuid);
285 update_cpu_power(cpuid, mpidr & MPIDR_HWID_BITMASK);
287 printk(KERN_INFO "CPU%u: thread %d, cpu %d, socket %d, mpidr %x\n",
288 cpuid, cpu_topology[cpuid].thread_id,
289 cpu_topology[cpuid].core_id,
290 cpu_topology[cpuid].socket_id, mpidr);
294 #ifdef CONFIG_SCHED_HMP
296 static const char * const little_cores[] = {
301 static bool is_little_cpu(struct device_node *cn)
303 const char * const *lc;
304 for (lc = little_cores; *lc; lc++)
305 if (of_device_is_compatible(cn, *lc))
310 void __init arch_get_fast_and_slow_cpus(struct cpumask *fast,
311 struct cpumask *slow)
313 struct device_node *cn = NULL;
320 * Use the config options if they are given. This helps testing
321 * HMP scheduling on systems without a big.LITTLE architecture.
323 if (strlen(CONFIG_HMP_FAST_CPU_MASK) && strlen(CONFIG_HMP_SLOW_CPU_MASK)) {
324 if (cpulist_parse(CONFIG_HMP_FAST_CPU_MASK, fast))
325 WARN(1, "Failed to parse HMP fast cpu mask!\n");
326 if (cpulist_parse(CONFIG_HMP_SLOW_CPU_MASK, slow))
327 WARN(1, "Failed to parse HMP slow cpu mask!\n");
332 * Else, parse device tree for little cores.
334 while ((cn = of_find_node_by_type(cn, "cpu"))) {
339 mpidr = of_get_property(cn, "reg", &len);
340 if (!mpidr || len != 4) {
341 pr_err("* %s missing reg property\n", cn->full_name);
345 cpu = get_logical_index(be32_to_cpup(mpidr));
346 if (cpu == -EINVAL) {
347 pr_err("couldn't get logical index for mpidr %x\n",
348 be32_to_cpup(mpidr));
352 if (is_little_cpu(cn))
353 cpumask_set_cpu(cpu, slow);
355 cpumask_set_cpu(cpu, fast);
358 if (!cpumask_empty(fast) && !cpumask_empty(slow))
362 * We didn't find both big and little cores so let's call all cores
363 * fast as this will keep the system running, with all cores being
366 cpumask_setall(fast);
370 struct cpumask hmp_slow_cpu_mask;
372 void __init arch_get_hmp_domains(struct list_head *hmp_domains_list)
374 struct cpumask hmp_fast_cpu_mask;
375 struct hmp_domain *domain;
377 arch_get_fast_and_slow_cpus(&hmp_fast_cpu_mask, &hmp_slow_cpu_mask);
380 * Initialize hmp_domains
381 * Must be ordered with respect to compute capacity.
382 * Fastest domain at head of list.
384 if(!cpumask_empty(&hmp_slow_cpu_mask)) {
385 domain = (struct hmp_domain *)
386 kmalloc(sizeof(struct hmp_domain), GFP_KERNEL);
387 cpumask_copy(&domain->possible_cpus, &hmp_slow_cpu_mask);
388 cpumask_and(&domain->cpus, cpu_online_mask, &domain->possible_cpus);
389 list_add(&domain->hmp_domains, hmp_domains_list);
391 domain = (struct hmp_domain *)
392 kmalloc(sizeof(struct hmp_domain), GFP_KERNEL);
393 cpumask_copy(&domain->possible_cpus, &hmp_fast_cpu_mask);
394 cpumask_and(&domain->cpus, cpu_online_mask, &domain->possible_cpus);
395 list_add(&domain->hmp_domains, hmp_domains_list);
397 #endif /* CONFIG_SCHED_HMP */
401 * cluster_to_logical_mask - return cpu logical mask of CPUs in a cluster
402 * @socket_id: cluster HW identifier
403 * @cluster_mask: the cpumask location to be initialized, modified by the
404 * function only if return value == 0
409 * -EINVAL if cluster_mask is NULL or there is no record matching socket_id
411 int cluster_to_logical_mask(unsigned int socket_id, cpumask_t *cluster_mask)
418 for_each_online_cpu(cpu)
419 if (socket_id == topology_physical_package_id(cpu)) {
420 cpumask_copy(cluster_mask, topology_core_cpumask(cpu));
428 * init_cpu_topology is called at boot when only one cpu is running
429 * which prevent simultaneous write access to cpu_topology array
431 void __init init_cpu_topology(void)
435 /* init core mask and power*/
436 for_each_possible_cpu(cpu) {
437 struct cputopo_arm *cpu_topo = &(cpu_topology[cpu]);
439 cpu_topo->thread_id = -1;
440 cpu_topo->core_id = -1;
441 cpu_topo->socket_id = -1;
442 cpumask_clear(&cpu_topo->core_sibling);
443 cpumask_clear(&cpu_topo->thread_sibling);
445 set_power_scale(cpu, SCHED_POWER_SCALE);