xref: /netbsd-src/sys/arch/arm/arm/arm_cpu_topology.c (revision 8c945a4bb74912ae73dd430315b4ed6359b859a3)
1 /*	$NetBSD: arm_cpu_topology.c,v 1.9 2024/12/10 11:27:29 jmcneill Exp $	*/
2 
3 /*
4  * Copyright (c) 2020 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /* CPU topology support for ARMv7 and ARMv8 systems.  */
30 
31 #include "opt_multiprocessor.h"
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: arm_cpu_topology.c,v 1.9 2024/12/10 11:27:29 jmcneill Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 
41 #include <arm/cpu.h>
42 #include <arm/cpu_topology.h>
43 #include <arm/armreg.h>
44 
45 #include <prop/proplib.h>
46 
47 void
48 arm_cpu_topology_set(struct cpu_info * const ci, mpidr_t mpidr)
49 {
50 #ifdef MULTIPROCESSOR
51 	uint pkgid, coreid, smtid, numaid;
52 	bool use_aff0 = (mpidr & MPIDR_MT) != 0 ||
53 			CPU_ID_ORYON_P(ci->ci_midr);
54 
55 	/* NUMA info comes from firmware tables (ACPI or FDT). */
56 	numaid = ci->ci_numa_id;
57 
58 	if (use_aff0) {
59 		pkgid = __SHIFTOUT(mpidr, MPIDR_AFF2);
60 		coreid = __SHIFTOUT(mpidr, MPIDR_AFF1);
61 		smtid = __SHIFTOUT(mpidr, MPIDR_AFF0);
62 	} else {
63 		pkgid = __SHIFTOUT(mpidr, MPIDR_AFF1);
64 		coreid = __SHIFTOUT(mpidr, MPIDR_AFF0);
65 		smtid = 0;
66 	}
67 	cpu_topology_set(ci, pkgid, coreid, smtid, numaid);
68 #endif /* MULTIPROCESSOR */
69 }
70 
71 void
72 arm_cpu_do_topology(struct cpu_info *const newci)
73 {
74 #ifdef MULTIPROCESSOR
75 	struct cpu_info *ci;
76 	CPU_INFO_ITERATOR cii;
77 	prop_dictionary_t dict;
78 	uint32_t capacity_dmips_mhz = 0;
79 	static uint32_t best_cap = 0;
80 
81 	dict = device_properties(newci->ci_dev);
82 	if (prop_dictionary_get_uint32(dict, "capacity_dmips_mhz",
83 	    &capacity_dmips_mhz))
84 		newci->ci_capacity_dmips_mhz = capacity_dmips_mhz;
85 
86 	if (newci->ci_capacity_dmips_mhz > best_cap)
87 		best_cap = newci->ci_capacity_dmips_mhz;
88 
89 	/*
90 	 * CPU_INFO_FOREACH() doesn't always work for this CPU until
91 	 * mi_cpu_attach() is called and ncpu is bumped, so call it
92 	 * directly here.  This also handles the not-MP case.
93 	 */
94 	cpu_topology_setspeed(newci, newci->ci_capacity_dmips_mhz < best_cap);
95 
96 	/*
97 	 * Using saved largest capacity, refresh previous topology info.
98 	 * It's supposed to be OK to re-set topology.
99 	 */
100 	for (CPU_INFO_FOREACH(cii, ci)) {
101 		if (ci == newci)
102 			continue;
103 		cpu_topology_setspeed(ci,
104 		    ci->ci_capacity_dmips_mhz < best_cap);
105 	}
106 #endif /* MULTIPROCESSOR */
107 }
108