• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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/cacheinfo.h>
11 #include <linux/cleanup.h>
12 #include <linux/cpu.h>
13 #include <linux/cpufreq.h>
14 #include <linux/device.h>
15 #include <linux/of.h>
16 #include <linux/slab.h>
17 #include <linux/sched/topology.h>
18 #include <linux/cpuset.h>
19 #include <linux/cpumask.h>
20 #include <linux/init.h>
21 #include <linux/rcupdate.h>
22 #include <linux/sched.h>
23 #include <linux/units.h>
24 
25 #define CREATE_TRACE_POINTS
26 #include <trace/events/hw_pressure.h>
27 
28 #undef CREATE_TRACE_POINTS
29 #include <trace/hooks/sched.h>
30 #include <trace/hooks/topology.h>
31 
32 static DEFINE_PER_CPU(struct scale_freq_data __rcu *, sft_data);
33 static struct cpumask scale_freq_counters_mask;
34 static bool scale_freq_invariant;
35 DEFINE_PER_CPU(unsigned long, capacity_freq_ref) = 1;
36 EXPORT_PER_CPU_SYMBOL_GPL(capacity_freq_ref);
37 
supports_scale_freq_counters(const struct cpumask * cpus)38 static bool supports_scale_freq_counters(const struct cpumask *cpus)
39 {
40 	bool use_amu_fie = true;
41 
42 	trace_android_vh_use_amu_fie(&use_amu_fie);
43 	if (!use_amu_fie)
44 		return false;
45 
46 	return cpumask_subset(cpus, &scale_freq_counters_mask);
47 }
48 
topology_scale_freq_invariant(void)49 bool topology_scale_freq_invariant(void)
50 {
51 	return cpufreq_supports_freq_invariance() ||
52 	       supports_scale_freq_counters(cpu_online_mask);
53 }
54 
update_scale_freq_invariant(bool status)55 static void update_scale_freq_invariant(bool status)
56 {
57 	if (scale_freq_invariant == status)
58 		return;
59 
60 	/*
61 	 * Task scheduler behavior depends on frequency invariance support,
62 	 * either cpufreq or counter driven. If the support status changes as
63 	 * a result of counter initialisation and use, retrigger the build of
64 	 * scheduling domains to ensure the information is propagated properly.
65 	 */
66 	if (topology_scale_freq_invariant() == status) {
67 		scale_freq_invariant = status;
68 		rebuild_sched_domains_energy();
69 	}
70 }
71 
topology_set_scale_freq_source(struct scale_freq_data * data,const struct cpumask * cpus)72 void topology_set_scale_freq_source(struct scale_freq_data *data,
73 				    const struct cpumask *cpus)
74 {
75 	struct scale_freq_data *sfd;
76 	int cpu;
77 
78 	/*
79 	 * Avoid calling rebuild_sched_domains() unnecessarily if FIE is
80 	 * supported by cpufreq.
81 	 */
82 	if (cpumask_empty(&scale_freq_counters_mask))
83 		scale_freq_invariant = topology_scale_freq_invariant();
84 
85 	rcu_read_lock();
86 
87 	for_each_cpu(cpu, cpus) {
88 		sfd = rcu_dereference(*per_cpu_ptr(&sft_data, cpu));
89 
90 		/* Use ARCH provided counters whenever possible */
91 		if (!sfd || sfd->source != SCALE_FREQ_SOURCE_ARCH) {
92 			rcu_assign_pointer(per_cpu(sft_data, cpu), data);
93 			cpumask_set_cpu(cpu, &scale_freq_counters_mask);
94 		}
95 	}
96 
97 	rcu_read_unlock();
98 
99 	update_scale_freq_invariant(true);
100 }
101 EXPORT_SYMBOL_GPL(topology_set_scale_freq_source);
102 
topology_clear_scale_freq_source(enum scale_freq_source source,const struct cpumask * cpus)103 void topology_clear_scale_freq_source(enum scale_freq_source source,
104 				      const struct cpumask *cpus)
105 {
106 	struct scale_freq_data *sfd;
107 	int cpu;
108 
109 	rcu_read_lock();
110 
111 	for_each_cpu(cpu, cpus) {
112 		sfd = rcu_dereference(*per_cpu_ptr(&sft_data, cpu));
113 
114 		if (sfd && sfd->source == source) {
115 			rcu_assign_pointer(per_cpu(sft_data, cpu), NULL);
116 			cpumask_clear_cpu(cpu, &scale_freq_counters_mask);
117 		}
118 	}
119 
120 	rcu_read_unlock();
121 
122 	/*
123 	 * Make sure all references to previous sft_data are dropped to avoid
124 	 * use-after-free races.
125 	 */
126 	synchronize_rcu();
127 
128 	update_scale_freq_invariant(false);
129 }
130 EXPORT_SYMBOL_GPL(topology_clear_scale_freq_source);
131 
topology_scale_freq_tick(void)132 void topology_scale_freq_tick(void)
133 {
134 	struct scale_freq_data *sfd = rcu_dereference_sched(*this_cpu_ptr(&sft_data));
135 
136 	if (sfd)
137 		sfd->set_freq_scale();
138 }
139 
140 DEFINE_PER_CPU(unsigned long, arch_freq_scale) = SCHED_CAPACITY_SCALE;
141 EXPORT_PER_CPU_SYMBOL_GPL(arch_freq_scale);
142 
topology_set_freq_scale(const struct cpumask * cpus,unsigned long cur_freq,unsigned long max_freq)143 void topology_set_freq_scale(const struct cpumask *cpus, unsigned long cur_freq,
144 			     unsigned long max_freq)
145 {
146 	unsigned long scale;
147 	int i;
148 
149 	if (WARN_ON_ONCE(!cur_freq || !max_freq))
150 		return;
151 
152 	/*
153 	 * If the use of counters for FIE is enabled, just return as we don't
154 	 * want to update the scale factor with information from CPUFREQ.
155 	 * Instead the scale factor will be updated from arch_scale_freq_tick.
156 	 */
157 	if (supports_scale_freq_counters(cpus))
158 		return;
159 
160 	scale = (cur_freq << SCHED_CAPACITY_SHIFT) / max_freq;
161 
162 	trace_android_vh_arch_set_freq_scale(cpus, cur_freq, max_freq, &scale);
163 
164 	for_each_cpu(i, cpus)
165 		per_cpu(arch_freq_scale, i) = scale;
166 }
167 
168 DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
169 EXPORT_PER_CPU_SYMBOL_GPL(cpu_scale);
170 
topology_set_cpu_scale(unsigned int cpu,unsigned long capacity)171 void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity)
172 {
173 	per_cpu(cpu_scale, cpu) = capacity;
174 }
175 
176 DEFINE_PER_CPU(unsigned long, hw_pressure);
177 EXPORT_PER_CPU_SYMBOL_GPL(hw_pressure);
178 
179 /**
180  * topology_update_hw_pressure() - Update HW pressure for CPUs
181  * @cpus        : The related CPUs for which capacity has been reduced
182  * @capped_freq : The maximum allowed frequency that CPUs can run at
183  *
184  * Update the value of HW pressure for all @cpus in the mask. The
185  * cpumask should include all (online+offline) affected CPUs, to avoid
186  * operating on stale data when hot-plug is used for some CPUs. The
187  * @capped_freq reflects the currently allowed max CPUs frequency due to
188  * HW capping. It might be also a boost frequency value, which is bigger
189  * than the internal 'capacity_freq_ref' max frequency. In such case the
190  * pressure value should simply be removed, since this is an indication that
191  * there is no HW throttling. The @capped_freq must be provided in kHz.
192  */
topology_update_hw_pressure(const struct cpumask * cpus,unsigned long capped_freq)193 void topology_update_hw_pressure(const struct cpumask *cpus,
194 				      unsigned long capped_freq)
195 {
196 	unsigned long max_capacity, capacity, pressure;
197 	u32 max_freq;
198 	int cpu;
199 
200 	cpu = cpumask_first(cpus);
201 	max_capacity = arch_scale_cpu_capacity(cpu);
202 	max_freq = arch_scale_freq_ref(cpu);
203 
204 	/*
205 	 * Handle properly the boost frequencies, which should simply clean
206 	 * the HW pressure value.
207 	 */
208 	if (max_freq <= capped_freq)
209 		capacity = max_capacity;
210 	else
211 		capacity = mult_frac(max_capacity, capped_freq, max_freq);
212 
213 	pressure = max_capacity - capacity;
214 
215 	trace_hw_pressure_update(cpu, pressure);
216 
217 	for_each_cpu(cpu, cpus) {
218 		WRITE_ONCE(per_cpu(hw_pressure, cpu), pressure);
219 		trace_android_rvh_update_thermal_stats(cpu);
220 	}
221 }
222 EXPORT_SYMBOL_GPL(topology_update_hw_pressure);
223 
cpu_capacity_show(struct device * dev,struct device_attribute * attr,char * buf)224 static ssize_t cpu_capacity_show(struct device *dev,
225 				 struct device_attribute *attr,
226 				 char *buf)
227 {
228 	struct cpu *cpu = container_of(dev, struct cpu, dev);
229 	unsigned long capacity = topology_get_cpu_scale(cpu->dev.id);
230 
231 	trace_android_rvh_cpu_capacity_show(&capacity, cpu->dev.id);
232 	return sysfs_emit(buf, "%lu\n", capacity);
233 }
234 
235 static void update_topology_flags_workfn(struct work_struct *work);
236 static DECLARE_WORK(update_topology_flags_work, update_topology_flags_workfn);
237 
238 static DEVICE_ATTR_RO(cpu_capacity);
239 
cpu_capacity_sysctl_add(unsigned int cpu)240 static int cpu_capacity_sysctl_add(unsigned int cpu)
241 {
242 	struct device *cpu_dev = get_cpu_device(cpu);
243 
244 	if (!cpu_dev)
245 		return -ENOENT;
246 
247 	device_create_file(cpu_dev, &dev_attr_cpu_capacity);
248 
249 	return 0;
250 }
251 
cpu_capacity_sysctl_remove(unsigned int cpu)252 static int cpu_capacity_sysctl_remove(unsigned int cpu)
253 {
254 	struct device *cpu_dev = get_cpu_device(cpu);
255 
256 	if (!cpu_dev)
257 		return -ENOENT;
258 
259 	device_remove_file(cpu_dev, &dev_attr_cpu_capacity);
260 
261 	return 0;
262 }
263 
register_cpu_capacity_sysctl(void)264 static int register_cpu_capacity_sysctl(void)
265 {
266 	cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "topology/cpu-capacity",
267 			  cpu_capacity_sysctl_add, cpu_capacity_sysctl_remove);
268 
269 	return 0;
270 }
271 subsys_initcall(register_cpu_capacity_sysctl);
272 
273 static int update_topology;
274 bool topology_update_done;
275 EXPORT_SYMBOL_GPL(topology_update_done);
276 
topology_update_cpu_topology(void)277 int topology_update_cpu_topology(void)
278 {
279 	return update_topology;
280 }
281 
282 /*
283  * Updating the sched_domains can't be done directly from cpufreq callbacks
284  * due to locking, so queue the work for later.
285  */
update_topology_flags_workfn(struct work_struct * work)286 static void update_topology_flags_workfn(struct work_struct *work)
287 {
288 	update_topology = 1;
289 	rebuild_sched_domains();
290 	topology_update_done = true;
291 	trace_android_vh_update_topology_flags_workfn(NULL);
292 	pr_debug("sched_domain hierarchy rebuilt, flags updated\n");
293 	update_topology = 0;
294 }
295 
296 static u32 *raw_capacity;
297 
free_raw_capacity(void)298 static int free_raw_capacity(void)
299 {
300 	kfree(raw_capacity);
301 	raw_capacity = NULL;
302 
303 	return 0;
304 }
305 
topology_normalize_cpu_scale(void)306 void topology_normalize_cpu_scale(void)
307 {
308 	u64 capacity;
309 	u64 capacity_scale;
310 	int cpu;
311 
312 	if (!raw_capacity)
313 		return;
314 
315 	capacity_scale = 1;
316 	for_each_possible_cpu(cpu) {
317 		capacity = raw_capacity[cpu] * per_cpu(capacity_freq_ref, cpu);
318 		capacity_scale = max(capacity, capacity_scale);
319 	}
320 
321 	pr_debug("cpu_capacity: capacity_scale=%llu\n", capacity_scale);
322 	for_each_possible_cpu(cpu) {
323 		capacity = raw_capacity[cpu] * per_cpu(capacity_freq_ref, cpu);
324 		capacity = div64_u64(capacity << SCHED_CAPACITY_SHIFT,
325 			capacity_scale);
326 		topology_set_cpu_scale(cpu, capacity);
327 		pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
328 			cpu, topology_get_cpu_scale(cpu));
329 	}
330 }
331 
topology_parse_cpu_capacity(struct device_node * cpu_node,int cpu)332 bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
333 {
334 	struct clk *cpu_clk;
335 	static bool cap_parsing_failed;
336 	int ret;
337 	u32 cpu_capacity;
338 
339 	if (cap_parsing_failed)
340 		return false;
341 
342 	ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
343 				   &cpu_capacity);
344 	if (!ret) {
345 		if (!raw_capacity) {
346 			raw_capacity = kcalloc(num_possible_cpus(),
347 					       sizeof(*raw_capacity),
348 					       GFP_KERNEL);
349 			if (!raw_capacity) {
350 				cap_parsing_failed = true;
351 				return false;
352 			}
353 		}
354 		raw_capacity[cpu] = cpu_capacity;
355 		pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n",
356 			cpu_node, raw_capacity[cpu]);
357 
358 		/*
359 		 * Update capacity_freq_ref for calculating early boot CPU capacities.
360 		 * For non-clk CPU DVFS mechanism, there's no way to get the
361 		 * frequency value now, assuming they are running at the same
362 		 * frequency (by keeping the initial capacity_freq_ref value).
363 		 */
364 		cpu_clk = of_clk_get(cpu_node, 0);
365 		if (!PTR_ERR_OR_ZERO(cpu_clk)) {
366 			per_cpu(capacity_freq_ref, cpu) =
367 				clk_get_rate(cpu_clk) / HZ_PER_KHZ;
368 			clk_put(cpu_clk);
369 		}
370 	} else {
371 		if (raw_capacity) {
372 			pr_err("cpu_capacity: missing %pOF raw capacity\n",
373 				cpu_node);
374 			pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
375 		}
376 		cap_parsing_failed = true;
377 		free_raw_capacity();
378 	}
379 
380 	return !ret;
381 }
382 
freq_inv_set_max_ratio(int cpu,u64 max_rate)383 void __weak freq_inv_set_max_ratio(int cpu, u64 max_rate)
384 {
385 }
386 
387 #ifdef CONFIG_ACPI_CPPC_LIB
388 #include <acpi/cppc_acpi.h>
389 
topology_init_cpu_capacity_cppc(void)390 static inline void topology_init_cpu_capacity_cppc(void)
391 {
392 	u64 capacity, capacity_scale = 0;
393 	struct cppc_perf_caps perf_caps;
394 	int cpu;
395 
396 	if (likely(!acpi_cpc_valid()))
397 		return;
398 
399 	raw_capacity = kcalloc(num_possible_cpus(), sizeof(*raw_capacity),
400 			       GFP_KERNEL);
401 	if (!raw_capacity)
402 		return;
403 
404 	for_each_possible_cpu(cpu) {
405 		if (!cppc_get_perf_caps(cpu, &perf_caps) &&
406 		    (perf_caps.highest_perf >= perf_caps.nominal_perf) &&
407 		    (perf_caps.highest_perf >= perf_caps.lowest_perf)) {
408 			raw_capacity[cpu] = perf_caps.highest_perf;
409 			capacity_scale = max_t(u64, capacity_scale, raw_capacity[cpu]);
410 
411 			per_cpu(capacity_freq_ref, cpu) = cppc_perf_to_khz(&perf_caps, raw_capacity[cpu]);
412 
413 			pr_debug("cpu_capacity: CPU%d cpu_capacity=%u (raw).\n",
414 				 cpu, raw_capacity[cpu]);
415 			continue;
416 		}
417 
418 		pr_err("cpu_capacity: CPU%d missing/invalid highest performance.\n", cpu);
419 		pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
420 		goto exit;
421 	}
422 
423 	for_each_possible_cpu(cpu) {
424 		freq_inv_set_max_ratio(cpu,
425 				       per_cpu(capacity_freq_ref, cpu) * HZ_PER_KHZ);
426 
427 		capacity = raw_capacity[cpu];
428 		capacity = div64_u64(capacity << SCHED_CAPACITY_SHIFT,
429 				     capacity_scale);
430 		topology_set_cpu_scale(cpu, capacity);
431 		pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
432 			cpu, topology_get_cpu_scale(cpu));
433 	}
434 
435 	schedule_work(&update_topology_flags_work);
436 	pr_debug("cpu_capacity: cpu_capacity initialization done\n");
437 
438 exit:
439 	free_raw_capacity();
440 }
acpi_processor_init_invariance_cppc(void)441 void acpi_processor_init_invariance_cppc(void)
442 {
443 	topology_init_cpu_capacity_cppc();
444 }
445 #endif
446 
447 #ifdef CONFIG_CPU_FREQ
448 static cpumask_var_t cpus_to_visit;
449 static void parsing_done_workfn(struct work_struct *work);
450 static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
451 
452 static int
init_cpu_capacity_callback(struct notifier_block * nb,unsigned long val,void * data)453 init_cpu_capacity_callback(struct notifier_block *nb,
454 			   unsigned long val,
455 			   void *data)
456 {
457 	struct cpufreq_policy *policy = data;
458 	int cpu;
459 
460 	if (val != CPUFREQ_CREATE_POLICY)
461 		return 0;
462 
463 	pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
464 		 cpumask_pr_args(policy->related_cpus),
465 		 cpumask_pr_args(cpus_to_visit));
466 
467 	cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus);
468 
469 	for_each_cpu(cpu, policy->related_cpus) {
470 		per_cpu(capacity_freq_ref, cpu) = policy->cpuinfo.max_freq;
471 		freq_inv_set_max_ratio(cpu,
472 				       per_cpu(capacity_freq_ref, cpu) * HZ_PER_KHZ);
473 	}
474 
475 	if (cpumask_empty(cpus_to_visit)) {
476 		if (raw_capacity) {
477 			topology_normalize_cpu_scale();
478 			schedule_work(&update_topology_flags_work);
479 			free_raw_capacity();
480 		}
481 		pr_debug("cpu_capacity: parsing done\n");
482 		schedule_work(&parsing_done_work);
483 	}
484 
485 	return 0;
486 }
487 
488 static struct notifier_block init_cpu_capacity_notifier = {
489 	.notifier_call = init_cpu_capacity_callback,
490 };
491 
register_cpufreq_notifier(void)492 static int __init register_cpufreq_notifier(void)
493 {
494 	int ret;
495 
496 	/*
497 	 * On ACPI-based systems skip registering cpufreq notifier as cpufreq
498 	 * information is not needed for cpu capacity initialization.
499 	 */
500 	if (!acpi_disabled)
501 		return -EINVAL;
502 
503 	if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL))
504 		return -ENOMEM;
505 
506 	cpumask_copy(cpus_to_visit, cpu_possible_mask);
507 
508 	ret = cpufreq_register_notifier(&init_cpu_capacity_notifier,
509 					CPUFREQ_POLICY_NOTIFIER);
510 
511 	if (ret)
512 		free_cpumask_var(cpus_to_visit);
513 
514 	return ret;
515 }
516 core_initcall(register_cpufreq_notifier);
517 
parsing_done_workfn(struct work_struct * work)518 static void parsing_done_workfn(struct work_struct *work)
519 {
520 	cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
521 					 CPUFREQ_POLICY_NOTIFIER);
522 	free_cpumask_var(cpus_to_visit);
523 }
524 
525 #else
526 core_initcall(free_raw_capacity);
527 #endif
528 
529 #if defined(CONFIG_ARM64) || defined(CONFIG_RISCV)
530 /*
531  * This function returns the logic cpu number of the node.
532  * There are basically three kinds of return values:
533  * (1) logic cpu number which is > 0.
534  * (2) -ENODEV when the device tree(DT) node is valid and found in the DT but
535  * there is no possible logical CPU in the kernel to match. This happens
536  * when CONFIG_NR_CPUS is configure to be smaller than the number of
537  * CPU nodes in DT. We need to just ignore this case.
538  * (3) -1 if the node does not exist in the device tree
539  */
get_cpu_for_node(struct device_node * node)540 static int __init get_cpu_for_node(struct device_node *node)
541 {
542 	int cpu;
543 	struct device_node *cpu_node __free(device_node) =
544 		of_parse_phandle(node, "cpu", 0);
545 
546 	if (!cpu_node)
547 		return -1;
548 
549 	cpu = of_cpu_node_to_id(cpu_node);
550 	if (cpu >= 0)
551 		topology_parse_cpu_capacity(cpu_node, cpu);
552 	else
553 		pr_info("CPU node for %pOF exist but the possible cpu range is :%*pbl\n",
554 			cpu_node, cpumask_pr_args(cpu_possible_mask));
555 
556 	return cpu;
557 }
558 
parse_core(struct device_node * core,int package_id,int cluster_id,int core_id)559 static int __init parse_core(struct device_node *core, int package_id,
560 			     int cluster_id, int core_id)
561 {
562 	char name[20];
563 	bool leaf = true;
564 	int i = 0;
565 	int cpu;
566 
567 	do {
568 		snprintf(name, sizeof(name), "thread%d", i);
569 		struct device_node *t __free(device_node) =
570 			of_get_child_by_name(core, name);
571 
572 		if (!t)
573 			break;
574 
575 		leaf = false;
576 		cpu = get_cpu_for_node(t);
577 		if (cpu >= 0) {
578 			cpu_topology[cpu].package_id = package_id;
579 			cpu_topology[cpu].cluster_id = cluster_id;
580 			cpu_topology[cpu].core_id = core_id;
581 			cpu_topology[cpu].thread_id = i;
582 		} else if (cpu != -ENODEV) {
583 			pr_err("%pOF: Can't get CPU for thread\n", t);
584 			return -EINVAL;
585 		}
586 		i++;
587 	} while (1);
588 
589 	cpu = get_cpu_for_node(core);
590 	if (cpu >= 0) {
591 		if (!leaf) {
592 			pr_err("%pOF: Core has both threads and CPU\n",
593 			       core);
594 			return -EINVAL;
595 		}
596 
597 		cpu_topology[cpu].package_id = package_id;
598 		cpu_topology[cpu].cluster_id = cluster_id;
599 		cpu_topology[cpu].core_id = core_id;
600 	} else if (leaf && cpu != -ENODEV) {
601 		pr_err("%pOF: Can't get CPU for leaf core\n", core);
602 		return -EINVAL;
603 	}
604 
605 	return 0;
606 }
607 
parse_cluster(struct device_node * cluster,int package_id,int cluster_id,int depth)608 static int __init parse_cluster(struct device_node *cluster, int package_id,
609 				int cluster_id, int depth)
610 {
611 	char name[20];
612 	bool leaf = true;
613 	bool has_cores = false;
614 	int core_id = 0;
615 	int i, ret;
616 
617 	/*
618 	 * First check for child clusters; we currently ignore any
619 	 * information about the nesting of clusters and present the
620 	 * scheduler with a flat list of them.
621 	 */
622 	i = 0;
623 	do {
624 		snprintf(name, sizeof(name), "cluster%d", i);
625 		struct device_node *c __free(device_node) =
626 			of_get_child_by_name(cluster, name);
627 
628 		if (!c)
629 			break;
630 
631 		leaf = false;
632 		ret = parse_cluster(c, package_id, i, depth + 1);
633 		if (depth > 0)
634 			pr_warn("Topology for clusters of clusters not yet supported\n");
635 		if (ret != 0)
636 			return ret;
637 		i++;
638 	} while (1);
639 
640 	/* Now check for cores */
641 	i = 0;
642 	do {
643 		snprintf(name, sizeof(name), "core%d", i);
644 		struct device_node *c __free(device_node) =
645 			of_get_child_by_name(cluster, name);
646 
647 		if (!c)
648 			break;
649 
650 		has_cores = true;
651 
652 		if (depth == 0) {
653 			pr_err("%pOF: cpu-map children should be clusters\n", c);
654 			return -EINVAL;
655 		}
656 
657 		if (leaf) {
658 			ret = parse_core(c, package_id, cluster_id, core_id++);
659 			if (ret != 0)
660 				return ret;
661 		} else {
662 			pr_err("%pOF: Non-leaf cluster with core %s\n",
663 			       cluster, name);
664 			return -EINVAL;
665 		}
666 
667 		i++;
668 	} while (1);
669 
670 	if (leaf && !has_cores)
671 		pr_warn("%pOF: empty cluster\n", cluster);
672 
673 	return 0;
674 }
675 
parse_socket(struct device_node * socket)676 static int __init parse_socket(struct device_node *socket)
677 {
678 	char name[20];
679 	bool has_socket = false;
680 	int package_id = 0, ret;
681 
682 	do {
683 		snprintf(name, sizeof(name), "socket%d", package_id);
684 		struct device_node *c __free(device_node) =
685 			of_get_child_by_name(socket, name);
686 
687 		if (!c)
688 			break;
689 
690 		has_socket = true;
691 		ret = parse_cluster(c, package_id, -1, 0);
692 		if (ret != 0)
693 			return ret;
694 
695 		package_id++;
696 	} while (1);
697 
698 	if (!has_socket)
699 		ret = parse_cluster(socket, 0, -1, 0);
700 
701 	return ret;
702 }
703 
parse_dt_topology(void)704 static int __init parse_dt_topology(void)
705 {
706 	int ret = 0;
707 	int cpu;
708 	struct device_node *cn __free(device_node) =
709 		of_find_node_by_path("/cpus");
710 
711 	if (!cn) {
712 		pr_err("No CPU information found in DT\n");
713 		return 0;
714 	}
715 
716 	/*
717 	 * When topology is provided cpu-map is essentially a root
718 	 * cluster with restricted subnodes.
719 	 */
720 	struct device_node *map __free(device_node) =
721 		of_get_child_by_name(cn, "cpu-map");
722 
723 	if (!map)
724 		return ret;
725 
726 	ret = parse_socket(map);
727 	if (ret != 0)
728 		return ret;
729 
730 	topology_normalize_cpu_scale();
731 
732 	/*
733 	 * Check that all cores are in the topology; the SMP code will
734 	 * only mark cores described in the DT as possible.
735 	 */
736 	for_each_possible_cpu(cpu)
737 		if (cpu_topology[cpu].package_id < 0) {
738 			return -EINVAL;
739 		}
740 
741 	return ret;
742 }
743 #endif
744 
745 /*
746  * cpu topology table
747  */
748 struct cpu_topology cpu_topology[NR_CPUS];
749 EXPORT_SYMBOL_GPL(cpu_topology);
750 
cpu_coregroup_mask(int cpu)751 const struct cpumask *cpu_coregroup_mask(int cpu)
752 {
753 	const cpumask_t *core_mask = cpumask_of_node(cpu_to_node(cpu));
754 
755 	/* Find the smaller of NUMA, core or LLC siblings */
756 	if (cpumask_subset(&cpu_topology[cpu].core_sibling, core_mask)) {
757 		/* not numa in package, lets use the package siblings */
758 		core_mask = &cpu_topology[cpu].core_sibling;
759 	}
760 
761 	if (last_level_cache_is_valid(cpu)) {
762 		if (cpumask_subset(&cpu_topology[cpu].llc_sibling, core_mask))
763 			core_mask = &cpu_topology[cpu].llc_sibling;
764 	}
765 
766 	/*
767 	 * For systems with no shared cpu-side LLC but with clusters defined,
768 	 * extend core_mask to cluster_siblings. The sched domain builder will
769 	 * then remove MC as redundant with CLS if SCHED_CLUSTER is enabled.
770 	 */
771 	if (IS_ENABLED(CONFIG_SCHED_CLUSTER) &&
772 	    cpumask_subset(core_mask, &cpu_topology[cpu].cluster_sibling))
773 		core_mask = &cpu_topology[cpu].cluster_sibling;
774 
775 	return core_mask;
776 }
777 
cpu_clustergroup_mask(int cpu)778 const struct cpumask *cpu_clustergroup_mask(int cpu)
779 {
780 	/*
781 	 * Forbid cpu_clustergroup_mask() to span more or the same CPUs as
782 	 * cpu_coregroup_mask().
783 	 */
784 	if (cpumask_subset(cpu_coregroup_mask(cpu),
785 			   &cpu_topology[cpu].cluster_sibling))
786 		return topology_sibling_cpumask(cpu);
787 
788 	return &cpu_topology[cpu].cluster_sibling;
789 }
790 
update_siblings_masks(unsigned int cpuid)791 void update_siblings_masks(unsigned int cpuid)
792 {
793 	struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
794 	int cpu, ret;
795 
796 	ret = detect_cache_attributes(cpuid);
797 	if (ret && ret != -ENOENT)
798 		pr_info("Early cacheinfo allocation failed, ret = %d\n", ret);
799 
800 	/* update core and thread sibling masks */
801 	for_each_online_cpu(cpu) {
802 		cpu_topo = &cpu_topology[cpu];
803 
804 		if (last_level_cache_is_shared(cpu, cpuid)) {
805 			cpumask_set_cpu(cpu, &cpuid_topo->llc_sibling);
806 			cpumask_set_cpu(cpuid, &cpu_topo->llc_sibling);
807 		}
808 
809 		if (cpuid_topo->package_id != cpu_topo->package_id)
810 			continue;
811 
812 		cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
813 		cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
814 
815 		if (cpuid_topo->cluster_id != cpu_topo->cluster_id)
816 			continue;
817 
818 		if (cpuid_topo->cluster_id >= 0) {
819 			cpumask_set_cpu(cpu, &cpuid_topo->cluster_sibling);
820 			cpumask_set_cpu(cpuid, &cpu_topo->cluster_sibling);
821 		}
822 
823 		if (cpuid_topo->core_id != cpu_topo->core_id)
824 			continue;
825 
826 		cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
827 		cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
828 	}
829 }
830 
clear_cpu_topology(int cpu)831 static void clear_cpu_topology(int cpu)
832 {
833 	struct cpu_topology *cpu_topo = &cpu_topology[cpu];
834 
835 	cpumask_clear(&cpu_topo->llc_sibling);
836 	cpumask_set_cpu(cpu, &cpu_topo->llc_sibling);
837 
838 	cpumask_clear(&cpu_topo->cluster_sibling);
839 	cpumask_set_cpu(cpu, &cpu_topo->cluster_sibling);
840 
841 	cpumask_clear(&cpu_topo->core_sibling);
842 	cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
843 	cpumask_clear(&cpu_topo->thread_sibling);
844 	cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
845 }
846 
reset_cpu_topology(void)847 void __init reset_cpu_topology(void)
848 {
849 	unsigned int cpu;
850 
851 	for_each_possible_cpu(cpu) {
852 		struct cpu_topology *cpu_topo = &cpu_topology[cpu];
853 
854 		cpu_topo->thread_id = -1;
855 		cpu_topo->core_id = -1;
856 		cpu_topo->cluster_id = -1;
857 		cpu_topo->package_id = -1;
858 
859 		clear_cpu_topology(cpu);
860 	}
861 }
862 
remove_cpu_topology(unsigned int cpu)863 void remove_cpu_topology(unsigned int cpu)
864 {
865 	int sibling;
866 
867 	for_each_cpu(sibling, topology_core_cpumask(cpu))
868 		cpumask_clear_cpu(cpu, topology_core_cpumask(sibling));
869 	for_each_cpu(sibling, topology_sibling_cpumask(cpu))
870 		cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling));
871 	for_each_cpu(sibling, topology_cluster_cpumask(cpu))
872 		cpumask_clear_cpu(cpu, topology_cluster_cpumask(sibling));
873 	for_each_cpu(sibling, topology_llc_cpumask(cpu))
874 		cpumask_clear_cpu(cpu, topology_llc_cpumask(sibling));
875 
876 	clear_cpu_topology(cpu);
877 }
878 
parse_acpi_topology(void)879 __weak int __init parse_acpi_topology(void)
880 {
881 	return 0;
882 }
883 
884 #if defined(CONFIG_ARM64) || defined(CONFIG_RISCV)
init_cpu_topology(void)885 void __init init_cpu_topology(void)
886 {
887 	int cpu, ret;
888 
889 	reset_cpu_topology();
890 	ret = parse_acpi_topology();
891 	if (!ret)
892 		ret = of_have_populated_dt() && parse_dt_topology();
893 
894 	if (ret) {
895 		/*
896 		 * Discard anything that was parsed if we hit an error so we
897 		 * don't use partial information. But do not return yet to give
898 		 * arch-specific early cache level detection a chance to run.
899 		 */
900 		reset_cpu_topology();
901 	}
902 
903 	for_each_possible_cpu(cpu) {
904 		ret = fetch_cache_info(cpu);
905 		if (!ret)
906 			continue;
907 		else if (ret != -ENOENT)
908 			pr_err("Early cacheinfo failed, ret = %d\n", ret);
909 		return;
910 	}
911 }
912 
store_cpu_topology(unsigned int cpuid)913 void store_cpu_topology(unsigned int cpuid)
914 {
915 	struct cpu_topology *cpuid_topo = &cpu_topology[cpuid];
916 
917 	if (cpuid_topo->package_id != -1)
918 		goto topology_populated;
919 
920 	cpuid_topo->thread_id = -1;
921 	cpuid_topo->core_id = cpuid;
922 	cpuid_topo->package_id = cpu_to_node(cpuid);
923 
924 	pr_debug("CPU%u: package %d core %d thread %d\n",
925 		 cpuid, cpuid_topo->package_id, cpuid_topo->core_id,
926 		 cpuid_topo->thread_id);
927 
928 topology_populated:
929 	update_siblings_masks(cpuid);
930 }
931 #endif
932