x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / arch / arm / kernel / topology.c
blobd3935dcfe24c81b39856aee4a80baffe0c5c048d
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/cpufreq.h>
16 #include <linux/cpumask.h>
17 #include <linux/export.h>
18 #include <linux/init.h>
19 #include <linux/percpu.h>
20 #include <linux/node.h>
21 #include <linux/nodemask.h>
22 #include <linux/of.h>
23 #include <linux/sched.h>
24 #include <linux/sched/topology.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
28 #include <asm/cpu.h>
29 #include <asm/cputype.h>
30 #include <asm/topology.h>
33 * cpu capacity scale management
37 * cpu capacity table
38 * This per cpu data structure describes the relative capacity of each core.
39 * On a heteregenous system, cores don't have the same computation capacity
40 * and we reflect that difference in the cpu_capacity field so the scheduler
41 * can take this difference into account during load balance. A per cpu
42 * structure is preferred because each CPU updates its own cpu_capacity field
43 * during the load balance except for idle cores. One idle core is selected
44 * to run the rebalance_domains for all idle cores and the cpu_capacity can be
45 * updated during this sequence.
47 static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
48 static DEFINE_MUTEX(cpu_scale_mutex);
50 unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
52 return per_cpu(cpu_scale, cpu);
55 static void set_capacity_scale(unsigned int cpu, unsigned long capacity)
57 per_cpu(cpu_scale, cpu) = capacity;
60 static ssize_t cpu_capacity_show(struct device *dev,
61 struct device_attribute *attr,
62 char *buf)
64 struct cpu *cpu = container_of(dev, struct cpu, dev);
66 return sprintf(buf, "%lu\n",
67 arch_scale_cpu_capacity(NULL, cpu->dev.id));
70 static ssize_t cpu_capacity_store(struct device *dev,
71 struct device_attribute *attr,
72 const char *buf,
73 size_t count)
75 struct cpu *cpu = container_of(dev, struct cpu, dev);
76 int this_cpu = cpu->dev.id, i;
77 unsigned long new_capacity;
78 ssize_t ret;
80 if (count) {
81 ret = kstrtoul(buf, 0, &new_capacity);
82 if (ret)
83 return ret;
84 if (new_capacity > SCHED_CAPACITY_SCALE)
85 return -EINVAL;
87 mutex_lock(&cpu_scale_mutex);
88 for_each_cpu(i, &cpu_topology[this_cpu].core_sibling)
89 set_capacity_scale(i, new_capacity);
90 mutex_unlock(&cpu_scale_mutex);
93 return count;
96 static DEVICE_ATTR_RW(cpu_capacity);
98 static int register_cpu_capacity_sysctl(void)
100 int i;
101 struct device *cpu;
103 for_each_possible_cpu(i) {
104 cpu = get_cpu_device(i);
105 if (!cpu) {
106 pr_err("%s: too early to get CPU%d device!\n",
107 __func__, i);
108 continue;
110 device_create_file(cpu, &dev_attr_cpu_capacity);
113 return 0;
115 subsys_initcall(register_cpu_capacity_sysctl);
117 #ifdef CONFIG_OF
118 struct cpu_efficiency {
119 const char *compatible;
120 unsigned long efficiency;
124 * Table of relative efficiency of each processors
125 * The efficiency value must fit in 20bit and the final
126 * cpu_scale value must be in the range
127 * 0 < cpu_scale < 3*SCHED_CAPACITY_SCALE/2
128 * in order to return at most 1 when DIV_ROUND_CLOSEST
129 * is used to compute the capacity of a CPU.
130 * Processors that are not defined in the table,
131 * use the default SCHED_CAPACITY_SCALE value for cpu_scale.
133 static const struct cpu_efficiency table_efficiency[] = {
134 {"arm,cortex-a15", 3891},
135 {"arm,cortex-a7", 2048},
136 {NULL, },
139 static unsigned long *__cpu_capacity;
140 #define cpu_capacity(cpu) __cpu_capacity[cpu]
142 static unsigned long middle_capacity = 1;
143 static bool cap_from_dt = true;
144 static u32 *raw_capacity;
145 static bool cap_parsing_failed;
146 static u32 capacity_scale;
148 static int __init parse_cpu_capacity(struct device_node *cpu_node, int cpu)
150 int ret = 1;
151 u32 cpu_capacity;
153 if (cap_parsing_failed)
154 return !ret;
156 ret = of_property_read_u32(cpu_node,
157 "capacity-dmips-mhz",
158 &cpu_capacity);
159 if (!ret) {
160 if (!raw_capacity) {
161 raw_capacity = kcalloc(num_possible_cpus(),
162 sizeof(*raw_capacity),
163 GFP_KERNEL);
164 if (!raw_capacity) {
165 pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
166 cap_parsing_failed = true;
167 return !ret;
170 capacity_scale = max(cpu_capacity, capacity_scale);
171 raw_capacity[cpu] = cpu_capacity;
172 pr_debug("cpu_capacity: %s cpu_capacity=%u (raw)\n",
173 cpu_node->full_name, raw_capacity[cpu]);
174 } else {
175 if (raw_capacity) {
176 pr_err("cpu_capacity: missing %s raw capacity\n",
177 cpu_node->full_name);
178 pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
180 cap_parsing_failed = true;
181 kfree(raw_capacity);
184 return !ret;
187 static void normalize_cpu_capacity(void)
189 u64 capacity;
190 int cpu;
192 if (!raw_capacity || cap_parsing_failed)
193 return;
195 pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
196 mutex_lock(&cpu_scale_mutex);
197 for_each_possible_cpu(cpu) {
198 capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT)
199 / capacity_scale;
200 set_capacity_scale(cpu, capacity);
201 pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
202 cpu, arch_scale_cpu_capacity(NULL, cpu));
204 mutex_unlock(&cpu_scale_mutex);
207 #ifdef CONFIG_CPU_FREQ
208 static cpumask_var_t cpus_to_visit;
209 static bool cap_parsing_done;
210 static void parsing_done_workfn(struct work_struct *work);
211 static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
213 static int
214 init_cpu_capacity_callback(struct notifier_block *nb,
215 unsigned long val,
216 void *data)
218 struct cpufreq_policy *policy = data;
219 int cpu;
221 if (cap_parsing_failed || cap_parsing_done)
222 return 0;
224 switch (val) {
225 case CPUFREQ_NOTIFY:
226 pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
227 cpumask_pr_args(policy->related_cpus),
228 cpumask_pr_args(cpus_to_visit));
229 cpumask_andnot(cpus_to_visit,
230 cpus_to_visit,
231 policy->related_cpus);
232 for_each_cpu(cpu, policy->related_cpus) {
233 raw_capacity[cpu] = arch_scale_cpu_capacity(NULL, cpu) *
234 policy->cpuinfo.max_freq / 1000UL;
235 capacity_scale = max(raw_capacity[cpu], capacity_scale);
237 if (cpumask_empty(cpus_to_visit)) {
238 normalize_cpu_capacity();
239 kfree(raw_capacity);
240 pr_debug("cpu_capacity: parsing done\n");
241 cap_parsing_done = true;
242 schedule_work(&parsing_done_work);
245 return 0;
248 static struct notifier_block init_cpu_capacity_notifier = {
249 .notifier_call = init_cpu_capacity_callback,
252 static int __init register_cpufreq_notifier(void)
254 if (cap_parsing_failed)
255 return -EINVAL;
257 if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
258 pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
259 return -ENOMEM;
261 cpumask_copy(cpus_to_visit, cpu_possible_mask);
263 return cpufreq_register_notifier(&init_cpu_capacity_notifier,
264 CPUFREQ_POLICY_NOTIFIER);
266 core_initcall(register_cpufreq_notifier);
268 static void parsing_done_workfn(struct work_struct *work)
270 cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
271 CPUFREQ_POLICY_NOTIFIER);
274 #else
275 static int __init free_raw_capacity(void)
277 kfree(raw_capacity);
279 return 0;
281 core_initcall(free_raw_capacity);
282 #endif
285 * Iterate all CPUs' descriptor in DT and compute the efficiency
286 * (as per table_efficiency). Also calculate a middle efficiency
287 * as close as possible to (max{eff_i} - min{eff_i}) / 2
288 * This is later used to scale the cpu_capacity field such that an
289 * 'average' CPU is of middle capacity. Also see the comments near
290 * table_efficiency[] and update_cpu_capacity().
292 static void __init parse_dt_topology(void)
294 const struct cpu_efficiency *cpu_eff;
295 struct device_node *cn = NULL;
296 unsigned long min_capacity = ULONG_MAX;
297 unsigned long max_capacity = 0;
298 unsigned long capacity = 0;
299 int cpu = 0;
301 __cpu_capacity = kcalloc(nr_cpu_ids, sizeof(*__cpu_capacity),
302 GFP_NOWAIT);
304 cn = of_find_node_by_path("/cpus");
305 if (!cn) {
306 pr_err("No CPU information found in DT\n");
307 return;
310 for_each_possible_cpu(cpu) {
311 const u32 *rate;
312 int len;
314 /* too early to use cpu->of_node */
315 cn = of_get_cpu_node(cpu, NULL);
316 if (!cn) {
317 pr_err("missing device node for CPU %d\n", cpu);
318 continue;
321 if (parse_cpu_capacity(cn, cpu)) {
322 of_node_put(cn);
323 continue;
326 cap_from_dt = false;
328 for (cpu_eff = table_efficiency; cpu_eff->compatible; cpu_eff++)
329 if (of_device_is_compatible(cn, cpu_eff->compatible))
330 break;
332 if (cpu_eff->compatible == NULL)
333 continue;
335 rate = of_get_property(cn, "clock-frequency", &len);
336 if (!rate || len != 4) {
337 pr_err("%s missing clock-frequency property\n",
338 cn->full_name);
339 continue;
342 capacity = ((be32_to_cpup(rate)) >> 20) * cpu_eff->efficiency;
344 /* Save min capacity of the system */
345 if (capacity < min_capacity)
346 min_capacity = capacity;
348 /* Save max capacity of the system */
349 if (capacity > max_capacity)
350 max_capacity = capacity;
352 cpu_capacity(cpu) = capacity;
355 /* If min and max capacities are equals, we bypass the update of the
356 * cpu_scale because all CPUs have the same capacity. Otherwise, we
357 * compute a middle_capacity factor that will ensure that the capacity
358 * of an 'average' CPU of the system will be as close as possible to
359 * SCHED_CAPACITY_SCALE, which is the default value, but with the
360 * constraint explained near table_efficiency[].
362 if (4*max_capacity < (3*(max_capacity + min_capacity)))
363 middle_capacity = (min_capacity + max_capacity)
364 >> (SCHED_CAPACITY_SHIFT+1);
365 else
366 middle_capacity = ((max_capacity / 3)
367 >> (SCHED_CAPACITY_SHIFT-1)) + 1;
369 if (cap_from_dt && !cap_parsing_failed)
370 normalize_cpu_capacity();
374 * Look for a customed capacity of a CPU in the cpu_capacity table during the
375 * boot. The update of all CPUs is in O(n^2) for heteregeneous system but the
376 * function returns directly for SMP system.
378 static void update_cpu_capacity(unsigned int cpu)
380 if (!cpu_capacity(cpu) || cap_from_dt)
381 return;
383 set_capacity_scale(cpu, cpu_capacity(cpu) / middle_capacity);
385 pr_info("CPU%u: update cpu_capacity %lu\n",
386 cpu, arch_scale_cpu_capacity(NULL, cpu));
389 #else
390 static inline void parse_dt_topology(void) {}
391 static inline void update_cpu_capacity(unsigned int cpuid) {}
392 #endif
395 * cpu topology table
397 struct cputopo_arm cpu_topology[NR_CPUS];
398 EXPORT_SYMBOL_GPL(cpu_topology);
400 const struct cpumask *cpu_coregroup_mask(int cpu)
402 return &cpu_topology[cpu].core_sibling;
406 * The current assumption is that we can power gate each core independently.
407 * This will be superseded by DT binding once available.
409 const struct cpumask *cpu_corepower_mask(int cpu)
411 return &cpu_topology[cpu].thread_sibling;
414 static void update_siblings_masks(unsigned int cpuid)
416 struct cputopo_arm *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
417 int cpu;
419 /* update core and thread sibling masks */
420 for_each_possible_cpu(cpu) {
421 cpu_topo = &cpu_topology[cpu];
423 if (cpuid_topo->socket_id != cpu_topo->socket_id)
424 continue;
426 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
427 if (cpu != cpuid)
428 cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
430 if (cpuid_topo->core_id != cpu_topo->core_id)
431 continue;
433 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
434 if (cpu != cpuid)
435 cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
437 smp_wmb();
441 * store_cpu_topology is called at boot when only one cpu is running
442 * and with the mutex cpu_hotplug.lock locked, when several cpus have booted,
443 * which prevents simultaneous write access to cpu_topology array
445 void store_cpu_topology(unsigned int cpuid)
447 struct cputopo_arm *cpuid_topo = &cpu_topology[cpuid];
448 unsigned int mpidr;
450 /* If the cpu topology has been already set, just return */
451 if (cpuid_topo->core_id != -1)
452 return;
454 mpidr = read_cpuid_mpidr();
456 /* create cpu topology mapping */
457 if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) {
459 * This is a multiprocessor system
460 * multiprocessor format & multiprocessor mode field are set
463 if (mpidr & MPIDR_MT_BITMASK) {
464 /* core performance interdependency */
465 cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
466 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
467 cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
468 } else {
469 /* largely independent cores */
470 cpuid_topo->thread_id = -1;
471 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
472 cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
474 } else {
476 * This is an uniprocessor system
477 * we are in multiprocessor format but uniprocessor system
478 * or in the old uniprocessor format
480 cpuid_topo->thread_id = -1;
481 cpuid_topo->core_id = 0;
482 cpuid_topo->socket_id = -1;
485 update_siblings_masks(cpuid);
487 update_cpu_capacity(cpuid);
489 pr_info("CPU%u: thread %d, cpu %d, socket %d, mpidr %x\n",
490 cpuid, cpu_topology[cpuid].thread_id,
491 cpu_topology[cpuid].core_id,
492 cpu_topology[cpuid].socket_id, mpidr);
495 static inline int cpu_corepower_flags(void)
497 return SD_SHARE_PKG_RESOURCES | SD_SHARE_POWERDOMAIN;
500 static struct sched_domain_topology_level arm_topology[] = {
501 #ifdef CONFIG_SCHED_MC
502 { cpu_corepower_mask, cpu_corepower_flags, SD_INIT_NAME(GMC) },
503 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
504 #endif
505 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
506 { NULL, },
510 * init_cpu_topology is called at boot when only one cpu is running
511 * which prevent simultaneous write access to cpu_topology array
513 void __init init_cpu_topology(void)
515 unsigned int cpu;
517 /* init core mask and capacity */
518 for_each_possible_cpu(cpu) {
519 struct cputopo_arm *cpu_topo = &(cpu_topology[cpu]);
521 cpu_topo->thread_id = -1;
522 cpu_topo->core_id = -1;
523 cpu_topo->socket_id = -1;
524 cpumask_clear(&cpu_topo->core_sibling);
525 cpumask_clear(&cpu_topo->thread_sibling);
527 smp_wmb();
529 parse_dt_topology();
531 /* Set scheduler topology descriptor */
532 set_sched_topology(arm_topology);