blob: bace6e0c4bb4c8c6eebe3ab87904bcb291af5c4f [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +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/cpu.h>
11#include <linux/cpufreq.h>
12#include <linux/device.h>
13#include <linux/of.h>
14#include <linux/slab.h>
15#include <linux/string.h>
16#include <linux/sched/topology.h>
17#include <linux/cpuset.h>
18#include <linux/cpumask.h>
19#include <linux/init.h>
20#include <linux/percpu.h>
21#include <linux/sched.h>
22#include <linux/smp.h>
23#include <trace/hooks/topology.h>
24
25DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE;
26DEFINE_PER_CPU(unsigned long, max_cpu_freq);
27DEFINE_PER_CPU(unsigned long, max_freq_scale) = SCHED_CAPACITY_SCALE;
28
29void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
30 unsigned long max_freq)
31{
32 unsigned long scale;
33 int i;
34
35 scale = (cur_freq << SCHED_CAPACITY_SHIFT) / max_freq;
36
37 trace_android_vh_arch_set_freq_scale(cpus, cur_freq, max_freq, &scale);
38
39 for_each_cpu(i, cpus) {
40 per_cpu(freq_scale, i) = scale;
41 per_cpu(max_cpu_freq, i) = max_freq;
42 }
43}
44
45void arch_set_max_freq_scale(struct cpumask *cpus,
46 unsigned long policy_max_freq)
47{
48 unsigned long scale, max_freq;
49 int cpu = cpumask_first(cpus);
50
51 if (cpu > nr_cpu_ids)
52 return;
53
54 max_freq = per_cpu(max_cpu_freq, cpu);
55 if (!max_freq)
56 return;
57
58 scale = (policy_max_freq << SCHED_CAPACITY_SHIFT) / max_freq;
59
60 trace_android_vh_arch_set_freq_scale(cpus, policy_max_freq, max_freq, &scale);
61
62 for_each_cpu(cpu, cpus)
63 per_cpu(max_freq_scale, cpu) = scale;
64}
65
66DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
67
68void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity)
69{
70 per_cpu(cpu_scale, cpu) = capacity;
71}
72
73static ssize_t cpu_capacity_show(struct device *dev,
74 struct device_attribute *attr,
75 char *buf)
76{
77 struct cpu *cpu = container_of(dev, struct cpu, dev);
78
79 return sysfs_emit(buf, "%lu\n", topology_get_cpu_scale(cpu->dev.id));
80}
81
82static void update_topology_flags_workfn(struct work_struct *work);
83static DECLARE_WORK(update_topology_flags_work, update_topology_flags_workfn);
84
85static DEVICE_ATTR_RO(cpu_capacity);
86
87static int register_cpu_capacity_sysctl(void)
88{
89 int i;
90 struct device *cpu;
91
92 for_each_possible_cpu(i) {
93 cpu = get_cpu_device(i);
94 if (!cpu) {
95 pr_err("%s: too early to get CPU%d device!\n",
96 __func__, i);
97 continue;
98 }
99 device_create_file(cpu, &dev_attr_cpu_capacity);
100 }
101
102 return 0;
103}
104subsys_initcall(register_cpu_capacity_sysctl);
105
106static int update_topology;
107
108int topology_update_cpu_topology(void)
109{
110 return update_topology;
111}
112
113/*
114 * Updating the sched_domains can't be done directly from cpufreq callbacks
115 * due to locking, so queue the work for later.
116 */
117static void update_topology_flags_workfn(struct work_struct *work)
118{
119 update_topology = 1;
120 rebuild_sched_domains();
121 pr_debug("sched_domain hierarchy rebuilt, flags updated\n");
122 update_topology = 0;
123}
124
125static u32 capacity_scale;
126static u32 *raw_capacity;
127
128static int free_raw_capacity(void)
129{
130 kfree(raw_capacity);
131 raw_capacity = NULL;
132
133 return 0;
134}
135
136void topology_normalize_cpu_scale(void)
137{
138 u64 capacity;
139 int cpu;
140
141 if (!raw_capacity)
142 return;
143
144 pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
145 for_each_possible_cpu(cpu) {
146 pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n",
147 cpu, raw_capacity[cpu]);
148 capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT)
149 / capacity_scale;
150 topology_set_cpu_scale(cpu, capacity);
151 pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
152 cpu, topology_get_cpu_scale(cpu));
153 }
154}
155
156bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
157{
158 static bool cap_parsing_failed;
159 int ret;
160 u32 cpu_capacity;
161
162 if (cap_parsing_failed)
163 return false;
164
165 ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
166 &cpu_capacity);
167 if (!ret) {
168 if (!raw_capacity) {
169 raw_capacity = kcalloc(num_possible_cpus(),
170 sizeof(*raw_capacity),
171 GFP_KERNEL);
172 if (!raw_capacity) {
173 cap_parsing_failed = true;
174 return false;
175 }
176 }
177 capacity_scale = max(cpu_capacity, capacity_scale);
178 raw_capacity[cpu] = cpu_capacity;
179 pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n",
180 cpu_node, raw_capacity[cpu]);
181 } else {
182 if (raw_capacity) {
183 pr_err("cpu_capacity: missing %pOF raw capacity\n",
184 cpu_node);
185 pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
186 }
187 cap_parsing_failed = true;
188 free_raw_capacity();
189 }
190
191 return !ret;
192}
193
194#ifdef CONFIG_CPU_FREQ
195static cpumask_var_t cpus_to_visit;
196static void parsing_done_workfn(struct work_struct *work);
197static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
198
199static int
200init_cpu_capacity_callback(struct notifier_block *nb,
201 unsigned long val,
202 void *data)
203{
204 struct cpufreq_policy *policy = data;
205 int cpu;
206
207 if (!raw_capacity)
208 return 0;
209
210 if (val != CPUFREQ_CREATE_POLICY)
211 return 0;
212
213 pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
214 cpumask_pr_args(policy->related_cpus),
215 cpumask_pr_args(cpus_to_visit));
216
217 cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus);
218
219 for_each_cpu(cpu, policy->related_cpus) {
220 raw_capacity[cpu] = topology_get_cpu_scale(cpu) *
221 policy->cpuinfo.max_freq / 1000UL;
222 capacity_scale = max(raw_capacity[cpu], capacity_scale);
223 }
224
225 if (cpumask_empty(cpus_to_visit)) {
226 topology_normalize_cpu_scale();
227 schedule_work(&update_topology_flags_work);
228 free_raw_capacity();
229 pr_debug("cpu_capacity: parsing done\n");
230 schedule_work(&parsing_done_work);
231 }
232
233 return 0;
234}
235
236static struct notifier_block init_cpu_capacity_notifier = {
237 .notifier_call = init_cpu_capacity_callback,
238};
239
240static int __init register_cpufreq_notifier(void)
241{
242 int ret;
243
244 /*
245 * on ACPI-based systems we need to use the default cpu capacity
246 * until we have the necessary code to parse the cpu capacity, so
247 * skip registering cpufreq notifier.
248 */
249 if (!acpi_disabled || !raw_capacity)
250 return -EINVAL;
251
252 if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL))
253 return -ENOMEM;
254
255 cpumask_copy(cpus_to_visit, cpu_possible_mask);
256
257 ret = cpufreq_register_notifier(&init_cpu_capacity_notifier,
258 CPUFREQ_POLICY_NOTIFIER);
259
260 if (ret)
261 free_cpumask_var(cpus_to_visit);
262
263 return ret;
264}
265core_initcall(register_cpufreq_notifier);
266
267static void parsing_done_workfn(struct work_struct *work)
268{
269 cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
270 CPUFREQ_POLICY_NOTIFIER);
271 free_cpumask_var(cpus_to_visit);
272}
273
274#else
275core_initcall(free_raw_capacity);
276#endif
277
278#if defined(CONFIG_ARM64) || defined(CONFIG_RISCV)
279static int __init get_cpu_for_node(struct device_node *node)
280{
281 struct device_node *cpu_node;
282 int cpu;
283
284 cpu_node = of_parse_phandle(node, "cpu", 0);
285 if (!cpu_node)
286 return -1;
287
288 cpu = of_cpu_node_to_id(cpu_node);
289 if (cpu >= 0)
290 topology_parse_cpu_capacity(cpu_node, cpu);
291 else
292 pr_crit("Unable to find CPU node for %pOF\n", cpu_node);
293
294 of_node_put(cpu_node);
295 return cpu;
296}
297
298static int __init parse_core(struct device_node *core, int package_id,
299 int core_id)
300{
301 char name[20];
302 bool leaf = true;
303 int i = 0;
304 int cpu;
305 struct device_node *t;
306
307 do {
308 snprintf(name, sizeof(name), "thread%d", i);
309 t = of_get_child_by_name(core, name);
310 if (t) {
311 leaf = false;
312 cpu = get_cpu_for_node(t);
313 if (cpu >= 0) {
314 cpu_topology[cpu].package_id = package_id;
315 cpu_topology[cpu].core_id = core_id;
316 cpu_topology[cpu].thread_id = i;
317 } else {
318 pr_err("%pOF: Can't get CPU for thread\n",
319 t);
320 of_node_put(t);
321 return -EINVAL;
322 }
323 of_node_put(t);
324 }
325 i++;
326 } while (t);
327
328 cpu = get_cpu_for_node(core);
329 if (cpu >= 0) {
330 if (!leaf) {
331 pr_err("%pOF: Core has both threads and CPU\n",
332 core);
333 return -EINVAL;
334 }
335
336 cpu_topology[cpu].package_id = package_id;
337 cpu_topology[cpu].core_id = core_id;
338 } else if (leaf) {
339 pr_err("%pOF: Can't get CPU for leaf core\n", core);
340 return -EINVAL;
341 }
342
343 return 0;
344}
345
346static int __init parse_cluster(struct device_node *cluster, int depth)
347{
348 char name[20];
349 bool leaf = true;
350 bool has_cores = false;
351 struct device_node *c;
352 static int package_id __initdata;
353 int core_id = 0;
354 int i, ret;
355
356 /*
357 * First check for child clusters; we currently ignore any
358 * information about the nesting of clusters and present the
359 * scheduler with a flat list of them.
360 */
361 i = 0;
362 do {
363 snprintf(name, sizeof(name), "cluster%d", i);
364 c = of_get_child_by_name(cluster, name);
365 if (c) {
366 leaf = false;
367 ret = parse_cluster(c, depth + 1);
368 of_node_put(c);
369 if (ret != 0)
370 return ret;
371 }
372 i++;
373 } while (c);
374
375 /* Now check for cores */
376 i = 0;
377 do {
378 snprintf(name, sizeof(name), "core%d", i);
379 c = of_get_child_by_name(cluster, name);
380 if (c) {
381 has_cores = true;
382
383 if (depth == 0) {
384 pr_err("%pOF: cpu-map children should be clusters\n",
385 c);
386 of_node_put(c);
387 return -EINVAL;
388 }
389
390 if (leaf) {
391 ret = parse_core(c, package_id, core_id++);
392 } else {
393 pr_err("%pOF: Non-leaf cluster with core %s\n",
394 cluster, name);
395 ret = -EINVAL;
396 }
397
398 of_node_put(c);
399 if (ret != 0)
400 return ret;
401 }
402 i++;
403 } while (c);
404
405 if (leaf && !has_cores)
406 pr_warn("%pOF: empty cluster\n", cluster);
407
408 if (leaf)
409 package_id++;
410
411 return 0;
412}
413
414static int __init parse_dt_topology(void)
415{
416 struct device_node *cn, *map;
417 int ret = 0;
418 int cpu;
419
420 cn = of_find_node_by_path("/cpus");
421 if (!cn) {
422 pr_err("No CPU information found in DT\n");
423 return 0;
424 }
425
426 /*
427 * When topology is provided cpu-map is essentially a root
428 * cluster with restricted subnodes.
429 */
430 map = of_get_child_by_name(cn, "cpu-map");
431 if (!map)
432 goto out;
433
434 ret = parse_cluster(map, 0);
435 if (ret != 0)
436 goto out_map;
437
438 topology_normalize_cpu_scale();
439
440 /*
441 * Check that all cores are in the topology; the SMP code will
442 * only mark cores described in the DT as possible.
443 */
444 for_each_possible_cpu(cpu)
445 if (cpu_topology[cpu].package_id == -1)
446 ret = -EINVAL;
447
448out_map:
449 of_node_put(map);
450out:
451 of_node_put(cn);
452 return ret;
453}
454#endif
455
456/*
457 * cpu topology table
458 */
459struct cpu_topology cpu_topology[NR_CPUS];
460EXPORT_SYMBOL_GPL(cpu_topology);
461
462const struct cpumask *cpu_coregroup_mask(int cpu)
463{
464 const cpumask_t *core_mask = cpumask_of_node(cpu_to_node(cpu));
465
466 /* Find the smaller of NUMA, core or LLC siblings */
467 if (cpumask_subset(&cpu_topology[cpu].core_sibling, core_mask)) {
468 /* not numa in package, lets use the package siblings */
469 core_mask = &cpu_topology[cpu].core_sibling;
470 }
471 if (cpu_topology[cpu].llc_id != -1) {
472 if (cpumask_subset(&cpu_topology[cpu].llc_sibling, core_mask))
473 core_mask = &cpu_topology[cpu].llc_sibling;
474 }
475
476 return core_mask;
477}
478
479void update_siblings_masks(unsigned int cpuid)
480{
481 struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
482 int cpu;
483
484 /* update core and thread sibling masks */
485 for_each_online_cpu(cpu) {
486 cpu_topo = &cpu_topology[cpu];
487
488 if (cpu_topo->llc_id != -1 && cpuid_topo->llc_id == cpu_topo->llc_id) {
489 cpumask_set_cpu(cpu, &cpuid_topo->llc_sibling);
490 cpumask_set_cpu(cpuid, &cpu_topo->llc_sibling);
491 }
492
493 if (cpuid_topo->package_id != cpu_topo->package_id)
494 continue;
495
496 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
497 cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
498
499 if (cpuid_topo->core_id != cpu_topo->core_id)
500 continue;
501
502 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
503 cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
504 }
505}
506
507static void clear_cpu_topology(int cpu)
508{
509 struct cpu_topology *cpu_topo = &cpu_topology[cpu];
510
511 cpumask_clear(&cpu_topo->llc_sibling);
512 cpumask_set_cpu(cpu, &cpu_topo->llc_sibling);
513
514 cpumask_clear(&cpu_topo->core_sibling);
515 cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
516 cpumask_clear(&cpu_topo->thread_sibling);
517 cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
518}
519
520void __init reset_cpu_topology(void)
521{
522 unsigned int cpu;
523
524 for_each_possible_cpu(cpu) {
525 struct cpu_topology *cpu_topo = &cpu_topology[cpu];
526
527 cpu_topo->thread_id = -1;
528 cpu_topo->core_id = -1;
529 cpu_topo->package_id = -1;
530 cpu_topo->llc_id = -1;
531
532 clear_cpu_topology(cpu);
533 }
534}
535
536void remove_cpu_topology(unsigned int cpu)
537{
538 int sibling;
539
540 for_each_cpu(sibling, topology_core_cpumask(cpu))
541 cpumask_clear_cpu(cpu, topology_core_cpumask(sibling));
542 for_each_cpu(sibling, topology_sibling_cpumask(cpu))
543 cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling));
544 for_each_cpu(sibling, topology_llc_cpumask(cpu))
545 cpumask_clear_cpu(cpu, topology_llc_cpumask(sibling));
546
547 clear_cpu_topology(cpu);
548}
549
550__weak int __init parse_acpi_topology(void)
551{
552 return 0;
553}
554
555#if defined(CONFIG_ARM64) || defined(CONFIG_RISCV)
556void __init init_cpu_topology(void)
557{
558 reset_cpu_topology();
559
560 /*
561 * Discard anything that was parsed if we hit an error so we
562 * don't use partial information.
563 */
564 if (parse_acpi_topology())
565 reset_cpu_topology();
566 else if (of_have_populated_dt() && parse_dt_topology())
567 reset_cpu_topology();
568}
569
570void store_cpu_topology(unsigned int cpuid)
571{
572 struct cpu_topology *cpuid_topo = &cpu_topology[cpuid];
573
574 if (cpuid_topo->package_id != -1)
575 goto topology_populated;
576
577 cpuid_topo->thread_id = -1;
578 cpuid_topo->core_id = cpuid;
579 cpuid_topo->package_id = cpu_to_node(cpuid);
580
581 pr_debug("CPU%u: package %d core %d thread %d\n",
582 cpuid, cpuid_topo->package_id, cpuid_topo->core_id,
583 cpuid_topo->thread_id);
584
585topology_populated:
586 update_siblings_masks(cpuid);
587}
588#endif