blob: b5f61f2840d0136a55491e131cc15f74a3a1b71e [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Arch specific cpu topology information
4 *
5 * Copyright (C) 2016, ARM Ltd.
6 * Written by: Juri Lelli, ARM Ltd.
7 */
8
9#include <linux/acpi.h>
10#include <linux/arch_topology.h>
11#include <linux/cpu.h>
12#include <linux/cpufreq.h>
13#include <linux/device.h>
14#include <linux/of.h>
15#include <linux/slab.h>
16#include <linux/string.h>
17#include <linux/sched/topology.h>
18#include <linux/cpuset.h>
19
20DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE;
21DEFINE_PER_CPU(unsigned long, max_cpu_freq);
22DEFINE_PER_CPU(unsigned long, max_freq_scale) = SCHED_CAPACITY_SCALE;
23
24void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
25 unsigned long max_freq)
26{
27 unsigned long scale;
28 int i;
29
30 scale = (cur_freq << SCHED_CAPACITY_SHIFT) / max_freq;
31
32 for_each_cpu(i, cpus) {
33 per_cpu(freq_scale, i) = scale;
34 per_cpu(max_cpu_freq, i) = max_freq;
35 }
36}
37
38void arch_set_max_freq_scale(struct cpumask *cpus,
39 unsigned long policy_max_freq)
40{
41 unsigned long scale, max_freq;
42 int cpu = cpumask_first(cpus);
43
44 if (cpu > nr_cpu_ids)
45 return;
46
47 max_freq = per_cpu(max_cpu_freq, cpu);
48 if (!max_freq)
49 return;
50
51 scale = (policy_max_freq << SCHED_CAPACITY_SHIFT) / max_freq;
52
53 for_each_cpu(cpu, cpus)
54 per_cpu(max_freq_scale, cpu) = scale;
55}
56
57static DEFINE_MUTEX(cpu_scale_mutex);
58DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
59
60void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity)
61{
62 per_cpu(cpu_scale, cpu) = capacity;
63}
64
65static ssize_t cpu_capacity_show(struct device *dev,
66 struct device_attribute *attr,
67 char *buf)
68{
69 struct cpu *cpu = container_of(dev, struct cpu, dev);
70
71 return sprintf(buf, "%lu\n", topology_get_cpu_scale(NULL, cpu->dev.id));
72}
73
74static void update_topology_flags_workfn(struct work_struct *work);
75static DECLARE_WORK(update_topology_flags_work, update_topology_flags_workfn);
76
77static ssize_t cpu_capacity_store(struct device *dev,
78 struct device_attribute *attr,
79 const char *buf,
80 size_t count)
81{
82 struct cpu *cpu = container_of(dev, struct cpu, dev);
83 int this_cpu = cpu->dev.id;
84 int i;
85 unsigned long new_capacity;
86 ssize_t ret;
87
88 if (!count)
89 return 0;
90
91 ret = kstrtoul(buf, 0, &new_capacity);
92 if (ret)
93 return ret;
94 if (new_capacity > SCHED_CAPACITY_SCALE)
95 return -EINVAL;
96
97 mutex_lock(&cpu_scale_mutex);
98 for_each_cpu(i, &cpu_topology[this_cpu].core_sibling)
99 topology_set_cpu_scale(i, new_capacity);
100 mutex_unlock(&cpu_scale_mutex);
101
102 schedule_work(&update_topology_flags_work);
103
104 return count;
105}
106
107static DEVICE_ATTR_RW(cpu_capacity);
108
109static int register_cpu_capacity_sysctl(void)
110{
111 int i;
112 struct device *cpu;
113
114 for_each_possible_cpu(i) {
115 cpu = get_cpu_device(i);
116 if (!cpu) {
117 pr_err("%s: too early to get CPU%d device!\n",
118 __func__, i);
119 continue;
120 }
121 device_create_file(cpu, &dev_attr_cpu_capacity);
122 }
123
124 return 0;
125}
126subsys_initcall(register_cpu_capacity_sysctl);
127
128static int update_topology;
129
130int topology_update_cpu_topology(void)
131{
132 return update_topology;
133}
134
135/*
136 * Updating the sched_domains can't be done directly from cpufreq callbacks
137 * due to locking, so queue the work for later.
138 */
139static void update_topology_flags_workfn(struct work_struct *work)
140{
141 update_topology = 1;
142 rebuild_sched_domains();
143 pr_debug("sched_domain hierarchy rebuilt, flags updated\n");
144 update_topology = 0;
145}
146
147static u32 capacity_scale;
148static u32 *raw_capacity;
149
150static int free_raw_capacity(void)
151{
152 kfree(raw_capacity);
153 raw_capacity = NULL;
154
155 return 0;
156}
157
158void topology_normalize_cpu_scale(void)
159{
160 u64 capacity;
161 int cpu;
162
163 if (!raw_capacity)
164 return;
165
166 pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
167 mutex_lock(&cpu_scale_mutex);
168 for_each_possible_cpu(cpu) {
169 pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n",
170 cpu, raw_capacity[cpu]);
171 capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT)
172 / capacity_scale;
173 topology_set_cpu_scale(cpu, capacity);
174 pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
175 cpu, topology_get_cpu_scale(NULL, cpu));
176 }
177 mutex_unlock(&cpu_scale_mutex);
178}
179
180bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
181{
182 static bool cap_parsing_failed;
183 int ret;
184 u32 cpu_capacity;
185
186 if (cap_parsing_failed)
187 return false;
188
189 ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
190 &cpu_capacity);
191 if (!ret) {
192 if (!raw_capacity) {
193 raw_capacity = kcalloc(num_possible_cpus(),
194 sizeof(*raw_capacity),
195 GFP_KERNEL);
196 if (!raw_capacity) {
197 pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
198 cap_parsing_failed = true;
199 return false;
200 }
201 }
202 capacity_scale = max(cpu_capacity, capacity_scale);
203 raw_capacity[cpu] = cpu_capacity;
204 pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n",
205 cpu_node, raw_capacity[cpu]);
206 } else {
207 if (raw_capacity) {
208 pr_err("cpu_capacity: missing %pOF raw capacity\n",
209 cpu_node);
210 pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
211 }
212 cap_parsing_failed = true;
213 free_raw_capacity();
214 }
215
216 return !ret;
217}
218
219#ifdef CONFIG_CPU_FREQ
220static cpumask_var_t cpus_to_visit;
221static void parsing_done_workfn(struct work_struct *work);
222static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
223
224static int
225init_cpu_capacity_callback(struct notifier_block *nb,
226 unsigned long val,
227 void *data)
228{
229 struct cpufreq_policy *policy = data;
230 int cpu;
231
232 if (!raw_capacity)
233 return 0;
234
235 if (val != CPUFREQ_NOTIFY)
236 return 0;
237
238 pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
239 cpumask_pr_args(policy->related_cpus),
240 cpumask_pr_args(cpus_to_visit));
241
242 cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus);
243
244 for_each_cpu(cpu, policy->related_cpus) {
245 raw_capacity[cpu] = topology_get_cpu_scale(NULL, cpu) *
246 policy->cpuinfo.max_freq / 1000UL;
247 capacity_scale = max(raw_capacity[cpu], capacity_scale);
248 }
249
250 if (cpumask_empty(cpus_to_visit)) {
251 topology_normalize_cpu_scale();
252 schedule_work(&update_topology_flags_work);
253 free_raw_capacity();
254 pr_debug("cpu_capacity: parsing done\n");
255 schedule_work(&parsing_done_work);
256 }
257
258 return 0;
259}
260
261static struct notifier_block init_cpu_capacity_notifier = {
262 .notifier_call = init_cpu_capacity_callback,
263};
264
265static int __init register_cpufreq_notifier(void)
266{
267 int ret;
268
269 /*
270 * on ACPI-based systems we need to use the default cpu capacity
271 * until we have the necessary code to parse the cpu capacity, so
272 * skip registering cpufreq notifier.
273 */
274 if (!acpi_disabled || !raw_capacity)
275 return -EINVAL;
276
277 if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
278 pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
279 return -ENOMEM;
280 }
281
282 cpumask_copy(cpus_to_visit, cpu_possible_mask);
283
284 ret = cpufreq_register_notifier(&init_cpu_capacity_notifier,
285 CPUFREQ_POLICY_NOTIFIER);
286
287 if (ret)
288 free_cpumask_var(cpus_to_visit);
289
290 return ret;
291}
292core_initcall(register_cpufreq_notifier);
293
294static void parsing_done_workfn(struct work_struct *work)
295{
296 cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
297 CPUFREQ_POLICY_NOTIFIER);
298 free_cpumask_var(cpus_to_visit);
299}
300
301#else
302core_initcall(free_raw_capacity);
303#endif