igb: Update igb Tx flags to improve code efficiency
[linux-2.6/cjktty.git] / arch / arm / kernel / topology.c
blob26c12c6440fcde02a3829f1ed3e1035f16ed6338
1 /*
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
11 * for more details.
14 #include <linux/cpu.h>
15 #include <linux/cpumask.h>
16 #include <linux/init.h>
17 #include <linux/percpu.h>
18 #include <linux/node.h>
19 #include <linux/nodemask.h>
20 #include <linux/of.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
24 #include <asm/cputype.h>
25 #include <asm/topology.h>
28 * cpu power scale management
32 * cpu power table
33 * This per cpu data structure describes the relative capacity of each core.
34 * On a heteregenous system, cores don't have the same computation capacity
35 * and we reflect that difference in the cpu_power field so the scheduler can
36 * take this difference into account during load balance. A per cpu structure
37 * is preferred because each CPU updates its own cpu_power field during the
38 * load balance except for idle cores. One idle core is selected to run the
39 * rebalance_domains for all idle cores and the cpu_power can be updated
40 * during this sequence.
42 static DEFINE_PER_CPU(unsigned long, cpu_scale);
44 unsigned long arch_scale_freq_power(struct sched_domain *sd, int cpu)
46 return per_cpu(cpu_scale, cpu);
49 static void set_power_scale(unsigned int cpu, unsigned long power)
51 per_cpu(cpu_scale, cpu) = power;
54 #ifdef CONFIG_OF
55 struct cpu_efficiency {
56 const char *compatible;
57 unsigned long efficiency;
61 * Table of relative efficiency of each processors
62 * The efficiency value must fit in 20bit and the final
63 * cpu_scale value must be in the range
64 * 0 < cpu_scale < 3*SCHED_POWER_SCALE/2
65 * in order to return at most 1 when DIV_ROUND_CLOSEST
66 * is used to compute the capacity of a CPU.
67 * Processors that are not defined in the table,
68 * use the default SCHED_POWER_SCALE value for cpu_scale.
70 struct cpu_efficiency table_efficiency[] = {
71 {"arm,cortex-a15", 3891},
72 {"arm,cortex-a7", 2048},
73 {NULL, },
76 struct cpu_capacity {
77 unsigned long hwid;
78 unsigned long capacity;
81 struct cpu_capacity *cpu_capacity;
83 unsigned long middle_capacity = 1;
86 * Iterate all CPUs' descriptor in DT and compute the efficiency
87 * (as per table_efficiency). Also calculate a middle efficiency
88 * as close as possible to (max{eff_i} - min{eff_i}) / 2
89 * This is later used to scale the cpu_power field such that an
90 * 'average' CPU is of middle power. Also see the comments near
91 * table_efficiency[] and update_cpu_power().
93 static void __init parse_dt_topology(void)
95 struct cpu_efficiency *cpu_eff;
96 struct device_node *cn = NULL;
97 unsigned long min_capacity = (unsigned long)(-1);
98 unsigned long max_capacity = 0;
99 unsigned long capacity = 0;
100 int alloc_size, cpu = 0;
102 alloc_size = nr_cpu_ids * sizeof(struct cpu_capacity);
103 cpu_capacity = (struct cpu_capacity *)kzalloc(alloc_size, GFP_NOWAIT);
105 while ((cn = of_find_node_by_type(cn, "cpu"))) {
106 const u32 *rate, *reg;
107 int len;
109 if (cpu >= num_possible_cpus())
110 break;
112 for (cpu_eff = table_efficiency; cpu_eff->compatible; cpu_eff++)
113 if (of_device_is_compatible(cn, cpu_eff->compatible))
114 break;
116 if (cpu_eff->compatible == NULL)
117 continue;
119 rate = of_get_property(cn, "clock-frequency", &len);
120 if (!rate || len != 4) {
121 pr_err("%s missing clock-frequency property\n",
122 cn->full_name);
123 continue;
126 reg = of_get_property(cn, "reg", &len);
127 if (!reg || len != 4) {
128 pr_err("%s missing reg property\n", cn->full_name);
129 continue;
132 capacity = ((be32_to_cpup(rate)) >> 20) * cpu_eff->efficiency;
134 /* Save min capacity of the system */
135 if (capacity < min_capacity)
136 min_capacity = capacity;
138 /* Save max capacity of the system */
139 if (capacity > max_capacity)
140 max_capacity = capacity;
142 cpu_capacity[cpu].capacity = capacity;
143 cpu_capacity[cpu++].hwid = be32_to_cpup(reg);
146 if (cpu < num_possible_cpus())
147 cpu_capacity[cpu].hwid = (unsigned long)(-1);
149 /* If min and max capacities are equals, we bypass the update of the
150 * cpu_scale because all CPUs have the same capacity. Otherwise, we
151 * compute a middle_capacity factor that will ensure that the capacity
152 * of an 'average' CPU of the system will be as close as possible to
153 * SCHED_POWER_SCALE, which is the default value, but with the
154 * constraint explained near table_efficiency[].
156 if (min_capacity == max_capacity)
157 cpu_capacity[0].hwid = (unsigned long)(-1);
158 else if (4*max_capacity < (3*(max_capacity + min_capacity)))
159 middle_capacity = (min_capacity + max_capacity)
160 >> (SCHED_POWER_SHIFT+1);
161 else
162 middle_capacity = ((max_capacity / 3)
163 >> (SCHED_POWER_SHIFT-1)) + 1;
168 * Look for a customed capacity of a CPU in the cpu_capacity table during the
169 * boot. The update of all CPUs is in O(n^2) for heteregeneous system but the
170 * function returns directly for SMP system.
172 void update_cpu_power(unsigned int cpu, unsigned long hwid)
174 unsigned int idx = 0;
176 /* look for the cpu's hwid in the cpu capacity table */
177 for (idx = 0; idx < num_possible_cpus(); idx++) {
178 if (cpu_capacity[idx].hwid == hwid)
179 break;
181 if (cpu_capacity[idx].hwid == -1)
182 return;
185 if (idx == num_possible_cpus())
186 return;
188 set_power_scale(cpu, cpu_capacity[idx].capacity / middle_capacity);
190 printk(KERN_INFO "CPU%u: update cpu_power %lu\n",
191 cpu, arch_scale_freq_power(NULL, cpu));
194 #else
195 static inline void parse_dt_topology(void) {}
196 static inline void update_cpu_power(unsigned int cpuid, unsigned int mpidr) {}
197 #endif
201 * cpu topology management
204 #define MPIDR_SMP_BITMASK (0x3 << 30)
205 #define MPIDR_SMP_VALUE (0x2 << 30)
207 #define MPIDR_MT_BITMASK (0x1 << 24)
210 * These masks reflect the current use of the affinity levels.
211 * The affinity level can be up to 16 bits according to ARM ARM
213 #define MPIDR_HWID_BITMASK 0xFFFFFF
215 #define MPIDR_LEVEL0_MASK 0x3
216 #define MPIDR_LEVEL0_SHIFT 0
218 #define MPIDR_LEVEL1_MASK 0xF
219 #define MPIDR_LEVEL1_SHIFT 8
221 #define MPIDR_LEVEL2_MASK 0xFF
222 #define MPIDR_LEVEL2_SHIFT 16
225 * cpu topology table
227 struct cputopo_arm cpu_topology[NR_CPUS];
229 const struct cpumask *cpu_coregroup_mask(int cpu)
231 return &cpu_topology[cpu].core_sibling;
234 void update_siblings_masks(unsigned int cpuid)
236 struct cputopo_arm *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
237 int cpu;
239 /* update core and thread sibling masks */
240 for_each_possible_cpu(cpu) {
241 cpu_topo = &cpu_topology[cpu];
243 if (cpuid_topo->socket_id != cpu_topo->socket_id)
244 continue;
246 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
247 if (cpu != cpuid)
248 cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
250 if (cpuid_topo->core_id != cpu_topo->core_id)
251 continue;
253 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
254 if (cpu != cpuid)
255 cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
257 smp_wmb();
261 * store_cpu_topology is called at boot when only one cpu is running
262 * and with the mutex cpu_hotplug.lock locked, when several cpus have booted,
263 * which prevents simultaneous write access to cpu_topology array
265 void store_cpu_topology(unsigned int cpuid)
267 struct cputopo_arm *cpuid_topo = &cpu_topology[cpuid];
268 unsigned int mpidr;
270 /* If the cpu topology has been already set, just return */
271 if (cpuid_topo->core_id != -1)
272 return;
274 mpidr = read_cpuid_mpidr();
276 /* create cpu topology mapping */
277 if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) {
279 * This is a multiprocessor system
280 * multiprocessor format & multiprocessor mode field are set
283 if (mpidr & MPIDR_MT_BITMASK) {
284 /* core performance interdependency */
285 cpuid_topo->thread_id = (mpidr >> MPIDR_LEVEL0_SHIFT)
286 & MPIDR_LEVEL0_MASK;
287 cpuid_topo->core_id = (mpidr >> MPIDR_LEVEL1_SHIFT)
288 & MPIDR_LEVEL1_MASK;
289 cpuid_topo->socket_id = (mpidr >> MPIDR_LEVEL2_SHIFT)
290 & MPIDR_LEVEL2_MASK;
291 } else {
292 /* largely independent cores */
293 cpuid_topo->thread_id = -1;
294 cpuid_topo->core_id = (mpidr >> MPIDR_LEVEL0_SHIFT)
295 & MPIDR_LEVEL0_MASK;
296 cpuid_topo->socket_id = (mpidr >> MPIDR_LEVEL1_SHIFT)
297 & MPIDR_LEVEL1_MASK;
299 } else {
301 * This is an uniprocessor system
302 * we are in multiprocessor format but uniprocessor system
303 * or in the old uniprocessor format
305 cpuid_topo->thread_id = -1;
306 cpuid_topo->core_id = 0;
307 cpuid_topo->socket_id = -1;
310 update_siblings_masks(cpuid);
312 update_cpu_power(cpuid, mpidr & MPIDR_HWID_BITMASK);
314 printk(KERN_INFO "CPU%u: thread %d, cpu %d, socket %d, mpidr %x\n",
315 cpuid, cpu_topology[cpuid].thread_id,
316 cpu_topology[cpuid].core_id,
317 cpu_topology[cpuid].socket_id, mpidr);
321 * init_cpu_topology is called at boot when only one cpu is running
322 * which prevent simultaneous write access to cpu_topology array
324 void __init init_cpu_topology(void)
326 unsigned int cpu;
328 /* init core mask and power*/
329 for_each_possible_cpu(cpu) {
330 struct cputopo_arm *cpu_topo = &(cpu_topology[cpu]);
332 cpu_topo->thread_id = -1;
333 cpu_topo->core_id = -1;
334 cpu_topo->socket_id = -1;
335 cpumask_clear(&cpu_topo->core_sibling);
336 cpumask_clear(&cpu_topo->thread_sibling);
338 set_power_scale(cpu, SCHED_POWER_SCALE);
340 smp_wmb();
342 parse_dt_topology();