2 * arch/arm64/kernel/topology.c
4 * Copyright (C) 2011,2013,2014 Linaro Limited.
6 * Based on the arm32 version written by Vincent Guittot in turn based on
7 * 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/acpi.h>
15 #include <linux/cpu.h>
16 #include <linux/cpumask.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>
24 #include <linux/string.h>
25 #include <linux/cpufreq.h>
28 #include <asm/cputype.h>
29 #include <asm/topology.h>
31 static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
32 static DEFINE_MUTEX(cpu_scale_mutex);
34 unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
36 return per_cpu(cpu_scale, cpu);
39 static void set_capacity_scale(unsigned int cpu, unsigned long capacity)
41 per_cpu(cpu_scale, cpu) = capacity;
44 #ifdef CONFIG_PROC_SYSCTL
45 static ssize_t cpu_capacity_show(struct device *dev,
46 struct device_attribute *attr,
49 struct cpu *cpu = container_of(dev, struct cpu, dev);
51 return sprintf(buf, "%lu\n",
52 arch_scale_cpu_capacity(NULL, cpu->dev.id));
55 static ssize_t cpu_capacity_store(struct device *dev,
56 struct device_attribute *attr,
60 struct cpu *cpu = container_of(dev, struct cpu, dev);
61 int this_cpu = cpu->dev.id, i;
62 unsigned long new_capacity;
66 ret = kstrtoul(buf, 0, &new_capacity);
69 if (new_capacity > SCHED_CAPACITY_SCALE)
72 mutex_lock(&cpu_scale_mutex);
73 for_each_cpu(i, &cpu_topology[this_cpu].core_sibling)
74 set_capacity_scale(i, new_capacity);
75 mutex_unlock(&cpu_scale_mutex);
81 static DEVICE_ATTR_RW(cpu_capacity);
83 static int register_cpu_capacity_sysctl(void)
88 for_each_possible_cpu(i) {
89 cpu = get_cpu_device(i);
91 pr_err("%s: too early to get CPU%d device!\n",
95 device_create_file(cpu, &dev_attr_cpu_capacity);
100 subsys_initcall(register_cpu_capacity_sysctl);
103 static u32 capacity_scale;
104 static u32 *raw_capacity;
105 static bool cap_parsing_failed;
107 static void __init parse_cpu_capacity(struct device_node *cpu_node, int cpu)
112 if (cap_parsing_failed)
115 ret = of_property_read_u32(cpu_node,
116 "capacity-dmips-mhz",
120 raw_capacity = kcalloc(num_possible_cpus(),
121 sizeof(*raw_capacity),
124 pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
125 cap_parsing_failed = true;
129 capacity_scale = max(cpu_capacity, capacity_scale);
130 raw_capacity[cpu] = cpu_capacity;
131 pr_debug("cpu_capacity: %s cpu_capacity=%u (raw)\n",
132 cpu_node->full_name, raw_capacity[cpu]);
135 pr_err("cpu_capacity: missing %s raw capacity\n",
136 cpu_node->full_name);
137 pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
139 cap_parsing_failed = true;
144 static void normalize_cpu_capacity(void)
149 if (!raw_capacity || cap_parsing_failed)
152 pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
153 mutex_lock(&cpu_scale_mutex);
154 for_each_possible_cpu(cpu) {
155 pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n",
156 cpu, raw_capacity[cpu]);
157 capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT)
159 set_capacity_scale(cpu, capacity);
160 pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
161 cpu, arch_scale_cpu_capacity(NULL, cpu));
163 mutex_unlock(&cpu_scale_mutex);
166 #ifdef CONFIG_CPU_FREQ
167 static cpumask_var_t cpus_to_visit;
168 static bool cap_parsing_done;
169 static void parsing_done_workfn(struct work_struct *work);
170 static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
173 init_cpu_capacity_callback(struct notifier_block *nb,
177 struct cpufreq_policy *policy = data;
180 if (cap_parsing_failed || cap_parsing_done)
185 pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
186 cpumask_pr_args(policy->related_cpus),
187 cpumask_pr_args(cpus_to_visit));
188 cpumask_andnot(cpus_to_visit,
190 policy->related_cpus);
191 for_each_cpu(cpu, policy->related_cpus) {
192 raw_capacity[cpu] = arch_scale_cpu_capacity(NULL, cpu) *
193 policy->cpuinfo.max_freq / 1000UL;
194 capacity_scale = max(raw_capacity[cpu], capacity_scale);
196 if (cpumask_empty(cpus_to_visit)) {
197 normalize_cpu_capacity();
199 pr_debug("cpu_capacity: parsing done\n");
200 cap_parsing_done = true;
201 schedule_work(&parsing_done_work);
207 static struct notifier_block init_cpu_capacity_notifier = {
208 .notifier_call = init_cpu_capacity_callback,
211 static int __init register_cpufreq_notifier(void)
214 * on ACPI-based systems we need to use the default cpu capacity
215 * until we have the necessary code to parse the cpu capacity, so
216 * skip registering cpufreq notifier.
218 if (!acpi_disabled || cap_parsing_failed)
221 if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
222 pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
225 cpumask_copy(cpus_to_visit, cpu_possible_mask);
227 return cpufreq_register_notifier(&init_cpu_capacity_notifier,
228 CPUFREQ_POLICY_NOTIFIER);
230 core_initcall(register_cpufreq_notifier);
232 static void parsing_done_workfn(struct work_struct *work)
234 cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
235 CPUFREQ_POLICY_NOTIFIER);
239 static int __init free_raw_capacity(void)
245 core_initcall(free_raw_capacity);
248 static int __init get_cpu_for_node(struct device_node *node)
250 struct device_node *cpu_node;
253 cpu_node = of_parse_phandle(node, "cpu", 0);
257 for_each_possible_cpu(cpu) {
258 if (of_get_cpu_node(cpu, NULL) == cpu_node) {
259 parse_cpu_capacity(cpu_node, cpu);
260 of_node_put(cpu_node);
265 pr_crit("Unable to find CPU node for %s\n", cpu_node->full_name);
267 of_node_put(cpu_node);
271 static int __init parse_core(struct device_node *core, int cluster_id,
278 struct device_node *t;
281 snprintf(name, sizeof(name), "thread%d", i);
282 t = of_get_child_by_name(core, name);
285 cpu = get_cpu_for_node(t);
287 cpu_topology[cpu].cluster_id = cluster_id;
288 cpu_topology[cpu].core_id = core_id;
289 cpu_topology[cpu].thread_id = i;
291 pr_err("%s: Can't get CPU for thread\n",
301 cpu = get_cpu_for_node(core);
304 pr_err("%s: Core has both threads and CPU\n",
309 cpu_topology[cpu].cluster_id = cluster_id;
310 cpu_topology[cpu].core_id = core_id;
312 pr_err("%s: Can't get CPU for leaf core\n", core->full_name);
319 static int __init parse_cluster(struct device_node *cluster, int depth)
323 bool has_cores = false;
324 struct device_node *c;
325 static int cluster_id __initdata;
330 * First check for child clusters; we currently ignore any
331 * information about the nesting of clusters and present the
332 * scheduler with a flat list of them.
336 snprintf(name, sizeof(name), "cluster%d", i);
337 c = of_get_child_by_name(cluster, name);
340 ret = parse_cluster(c, depth + 1);
348 /* Now check for cores */
351 snprintf(name, sizeof(name), "core%d", i);
352 c = of_get_child_by_name(cluster, name);
357 pr_err("%s: cpu-map children should be clusters\n",
364 ret = parse_core(c, cluster_id, core_id++);
366 pr_err("%s: Non-leaf cluster with core %s\n",
367 cluster->full_name, name);
378 if (leaf && !has_cores)
379 pr_warn("%s: empty cluster\n", cluster->full_name);
387 static int __init parse_dt_topology(void)
389 struct device_node *cn, *map;
393 cn = of_find_node_by_path("/cpus");
395 pr_err("No CPU information found in DT\n");
400 * When topology is provided cpu-map is essentially a root
401 * cluster with restricted subnodes.
403 map = of_get_child_by_name(cn, "cpu-map");
405 cap_parsing_failed = true;
409 ret = parse_cluster(map, 0);
413 normalize_cpu_capacity();
416 * Check that all cores are in the topology; the SMP code will
417 * only mark cores described in the DT as possible.
419 for_each_possible_cpu(cpu)
420 if (cpu_topology[cpu].cluster_id == -1)
433 struct cpu_topology cpu_topology[NR_CPUS];
434 EXPORT_SYMBOL_GPL(cpu_topology);
436 const struct cpumask *cpu_coregroup_mask(int cpu)
438 return &cpu_topology[cpu].core_sibling;
441 static void update_siblings_masks(unsigned int cpuid)
443 struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
446 /* update core and thread sibling masks */
447 for_each_possible_cpu(cpu) {
448 cpu_topo = &cpu_topology[cpu];
450 if (cpuid_topo->cluster_id != cpu_topo->cluster_id)
453 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
455 cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
457 if (cpuid_topo->core_id != cpu_topo->core_id)
460 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
462 cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
466 void store_cpu_topology(unsigned int cpuid)
468 struct cpu_topology *cpuid_topo = &cpu_topology[cpuid];
471 if (cpuid_topo->cluster_id != -1)
472 goto topology_populated;
474 mpidr = read_cpuid_mpidr();
476 /* Uniprocessor systems can rely on default topology values */
477 if (mpidr & MPIDR_UP_BITMASK)
480 /* Create cpu topology mapping based on MPIDR. */
481 if (mpidr & MPIDR_MT_BITMASK) {
482 /* Multiprocessor system : Multi-threads per core */
483 cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
484 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
485 cpuid_topo->cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 2) |
486 MPIDR_AFFINITY_LEVEL(mpidr, 3) << 8;
488 /* Multiprocessor system : Single-thread per core */
489 cpuid_topo->thread_id = -1;
490 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
491 cpuid_topo->cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 1) |
492 MPIDR_AFFINITY_LEVEL(mpidr, 2) << 8 |
493 MPIDR_AFFINITY_LEVEL(mpidr, 3) << 16;
496 pr_debug("CPU%u: cluster %d core %d thread %d mpidr %#016llx\n",
497 cpuid, cpuid_topo->cluster_id, cpuid_topo->core_id,
498 cpuid_topo->thread_id, mpidr);
501 update_siblings_masks(cpuid);
504 static void __init reset_cpu_topology(void)
508 for_each_possible_cpu(cpu) {
509 struct cpu_topology *cpu_topo = &cpu_topology[cpu];
511 cpu_topo->thread_id = -1;
512 cpu_topo->core_id = 0;
513 cpu_topo->cluster_id = -1;
515 cpumask_clear(&cpu_topo->core_sibling);
516 cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
517 cpumask_clear(&cpu_topo->thread_sibling);
518 cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
522 void __init init_cpu_topology(void)
524 reset_cpu_topology();
527 * Discard anything that was parsed if we hit an error so we
528 * don't use partial information.
530 if (of_have_populated_dt() && parse_dt_topology())
531 reset_cpu_topology();