File Index
Files and directories
Sign in
FILE

arch_topology.c

File preview with syntax highlighting.

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