2 * Arch specific cpu topology information
4 * Copyright (C) 2016, ARM Ltd.
5 * Written by: Juri Lelli, ARM Ltd.
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
11 * Released under the GPLv2 only.
12 * SPDX-License-Identifier: GPL-2.0
15 #include <linux/acpi.h>
16 #include <linux/arch_topology.h>
17 #include <linux/cpu.h>
18 #include <linux/cpufreq.h>
19 #include <linux/device.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 #include <linux/sched/topology.h>
25 static DEFINE_MUTEX(cpu_scale_mutex);
26 static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
28 unsigned long topology_get_cpu_scale(struct sched_domain *sd, int cpu)
30 return per_cpu(cpu_scale, cpu);
33 void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity)
35 per_cpu(cpu_scale, cpu) = capacity;
38 static ssize_t cpu_capacity_show(struct device *dev,
39 struct device_attribute *attr,
42 struct cpu *cpu = container_of(dev, struct cpu, dev);
44 return sprintf(buf, "%lu\n", topology_get_cpu_scale(NULL, cpu->dev.id));
47 static ssize_t cpu_capacity_store(struct device *dev,
48 struct device_attribute *attr,
52 struct cpu *cpu = container_of(dev, struct cpu, dev);
53 int this_cpu = cpu->dev.id;
55 unsigned long new_capacity;
61 ret = kstrtoul(buf, 0, &new_capacity);
64 if (new_capacity > SCHED_CAPACITY_SCALE)
67 mutex_lock(&cpu_scale_mutex);
68 for_each_cpu(i, &cpu_topology[this_cpu].core_sibling)
69 topology_set_cpu_scale(i, new_capacity);
70 mutex_unlock(&cpu_scale_mutex);
75 static DEVICE_ATTR_RW(cpu_capacity);
77 static int register_cpu_capacity_sysctl(void)
82 for_each_possible_cpu(i) {
83 cpu = get_cpu_device(i);
85 pr_err("%s: too early to get CPU%d device!\n",
89 device_create_file(cpu, &dev_attr_cpu_capacity);
94 subsys_initcall(register_cpu_capacity_sysctl);
96 static u32 capacity_scale;
97 static u32 *raw_capacity;
99 static int __init free_raw_capacity(void)
107 void topology_normalize_cpu_scale(void)
115 pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
116 mutex_lock(&cpu_scale_mutex);
117 for_each_possible_cpu(cpu) {
118 pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n",
119 cpu, raw_capacity[cpu]);
120 capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT)
122 topology_set_cpu_scale(cpu, capacity);
123 pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
124 cpu, topology_get_cpu_scale(NULL, cpu));
126 mutex_unlock(&cpu_scale_mutex);
129 bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
131 static bool cap_parsing_failed;
135 if (cap_parsing_failed)
138 ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
142 raw_capacity = kcalloc(num_possible_cpus(),
143 sizeof(*raw_capacity),
146 pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
147 cap_parsing_failed = true;
151 capacity_scale = max(cpu_capacity, capacity_scale);
152 raw_capacity[cpu] = cpu_capacity;
153 pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n",
154 cpu_node, raw_capacity[cpu]);
157 pr_err("cpu_capacity: missing %pOF raw capacity\n",
159 pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
161 cap_parsing_failed = true;
168 #ifdef CONFIG_CPU_FREQ
169 static cpumask_var_t cpus_to_visit __initdata;
170 static void __init parsing_done_workfn(struct work_struct *work);
171 static __initdata DECLARE_WORK(parsing_done_work, parsing_done_workfn);
174 init_cpu_capacity_callback(struct notifier_block *nb,
178 struct cpufreq_policy *policy = data;
184 if (val != CPUFREQ_NOTIFY)
187 pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
188 cpumask_pr_args(policy->related_cpus),
189 cpumask_pr_args(cpus_to_visit));
191 cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus);
193 for_each_cpu(cpu, policy->related_cpus) {
194 raw_capacity[cpu] = topology_get_cpu_scale(NULL, cpu) *
195 policy->cpuinfo.max_freq / 1000UL;
196 capacity_scale = max(raw_capacity[cpu], capacity_scale);
199 if (cpumask_empty(cpus_to_visit)) {
200 topology_normalize_cpu_scale();
202 pr_debug("cpu_capacity: parsing done\n");
203 schedule_work(&parsing_done_work);
209 static struct notifier_block init_cpu_capacity_notifier __initdata = {
210 .notifier_call = init_cpu_capacity_callback,
213 static int __init register_cpufreq_notifier(void)
216 * on ACPI-based systems we need to use the default cpu capacity
217 * until we have the necessary code to parse the cpu capacity, so
218 * skip registering cpufreq notifier.
220 if (!acpi_disabled || !raw_capacity)
223 if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
224 pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
228 cpumask_copy(cpus_to_visit, cpu_possible_mask);
230 return cpufreq_register_notifier(&init_cpu_capacity_notifier,
231 CPUFREQ_POLICY_NOTIFIER);
233 core_initcall(register_cpufreq_notifier);
235 static void __init parsing_done_workfn(struct work_struct *work)
237 cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
238 CPUFREQ_POLICY_NOTIFIER);
242 core_initcall(free_raw_capacity);