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 DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE;
27 void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
28 unsigned long max_freq)
33 scale = (cur_freq << SCHED_CAPACITY_SHIFT) / max_freq;
36 per_cpu(freq_scale, i) = scale;
39 static DEFINE_MUTEX(cpu_scale_mutex);
40 DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
42 void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity)
44 per_cpu(cpu_scale, cpu) = capacity;
47 static ssize_t cpu_capacity_show(struct device *dev,
48 struct device_attribute *attr,
51 struct cpu *cpu = container_of(dev, struct cpu, dev);
53 return sprintf(buf, "%lu\n", topology_get_cpu_scale(NULL, cpu->dev.id));
56 static ssize_t cpu_capacity_store(struct device *dev,
57 struct device_attribute *attr,
61 struct cpu *cpu = container_of(dev, struct cpu, dev);
62 int this_cpu = cpu->dev.id;
64 unsigned long new_capacity;
70 ret = kstrtoul(buf, 0, &new_capacity);
73 if (new_capacity > SCHED_CAPACITY_SCALE)
76 mutex_lock(&cpu_scale_mutex);
77 for_each_cpu(i, &cpu_topology[this_cpu].core_sibling)
78 topology_set_cpu_scale(i, new_capacity);
79 mutex_unlock(&cpu_scale_mutex);
84 static DEVICE_ATTR_RW(cpu_capacity);
86 static int register_cpu_capacity_sysctl(void)
91 for_each_possible_cpu(i) {
92 cpu = get_cpu_device(i);
94 pr_err("%s: too early to get CPU%d device!\n",
98 device_create_file(cpu, &dev_attr_cpu_capacity);
103 subsys_initcall(register_cpu_capacity_sysctl);
105 static u32 capacity_scale;
106 static u32 *raw_capacity;
108 static int free_raw_capacity(void)
116 void topology_normalize_cpu_scale(void)
124 pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
125 mutex_lock(&cpu_scale_mutex);
126 for_each_possible_cpu(cpu) {
127 pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n",
128 cpu, raw_capacity[cpu]);
129 capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT)
131 topology_set_cpu_scale(cpu, capacity);
132 pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
133 cpu, topology_get_cpu_scale(NULL, cpu));
135 mutex_unlock(&cpu_scale_mutex);
138 bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
140 static bool cap_parsing_failed;
144 if (cap_parsing_failed)
147 ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
151 raw_capacity = kcalloc(num_possible_cpus(),
152 sizeof(*raw_capacity),
155 pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
156 cap_parsing_failed = true;
160 capacity_scale = max(cpu_capacity, capacity_scale);
161 raw_capacity[cpu] = cpu_capacity;
162 pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n",
163 cpu_node, raw_capacity[cpu]);
166 pr_err("cpu_capacity: missing %pOF raw capacity\n",
168 pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
170 cap_parsing_failed = true;
177 #ifdef CONFIG_CPU_FREQ
178 static cpumask_var_t cpus_to_visit __initdata;
179 static void __init parsing_done_workfn(struct work_struct *work);
180 static __initdata DECLARE_WORK(parsing_done_work, parsing_done_workfn);
183 init_cpu_capacity_callback(struct notifier_block *nb,
187 struct cpufreq_policy *policy = data;
193 if (val != CPUFREQ_NOTIFY)
196 pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
197 cpumask_pr_args(policy->related_cpus),
198 cpumask_pr_args(cpus_to_visit));
200 cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus);
202 for_each_cpu(cpu, policy->related_cpus) {
203 raw_capacity[cpu] = topology_get_cpu_scale(NULL, cpu) *
204 policy->cpuinfo.max_freq / 1000UL;
205 capacity_scale = max(raw_capacity[cpu], capacity_scale);
208 if (cpumask_empty(cpus_to_visit)) {
209 topology_normalize_cpu_scale();
211 pr_debug("cpu_capacity: parsing done\n");
212 schedule_work(&parsing_done_work);
218 static struct notifier_block init_cpu_capacity_notifier __initdata = {
219 .notifier_call = init_cpu_capacity_callback,
222 static int __init register_cpufreq_notifier(void)
227 * on ACPI-based systems we need to use the default cpu capacity
228 * until we have the necessary code to parse the cpu capacity, so
229 * skip registering cpufreq notifier.
231 if (!acpi_disabled || !raw_capacity)
234 if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
235 pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
239 cpumask_copy(cpus_to_visit, cpu_possible_mask);
241 ret = cpufreq_register_notifier(&init_cpu_capacity_notifier,
242 CPUFREQ_POLICY_NOTIFIER);
245 free_cpumask_var(cpus_to_visit);
249 core_initcall(register_cpufreq_notifier);
251 static void __init parsing_done_workfn(struct work_struct *work)
253 cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
254 CPUFREQ_POLICY_NOTIFIER);
255 free_cpumask_var(cpus_to_visit);
259 core_initcall(free_raw_capacity);