xref: /aosp_15_r20/external/cpuinfo/src/arm/linux/aarch32-isa.c (revision 2b54f0db79fd8303838913b20ff3780cddaa909f)
1 #include <stdint.h>
2 
3 #if CPUINFO_MOCK
4 	#include <cpuinfo-mock.h>
5 #endif
6 #include <arm/linux/api.h>
7 #include <arm/linux/cp.h>
8 #include <arm/midr.h>
9 #include <cpuinfo/log.h>
10 
11 
12 #if CPUINFO_MOCK
13 	uint32_t cpuinfo_arm_fpsid = 0;
14 	uint32_t cpuinfo_arm_mvfr0 = 0;
15 	uint32_t cpuinfo_arm_wcid = 0;
16 
cpuinfo_set_fpsid(uint32_t fpsid)17 	void cpuinfo_set_fpsid(uint32_t fpsid) {
18 		cpuinfo_arm_fpsid = fpsid;
19 	}
20 
cpuinfo_set_wcid(uint32_t wcid)21 	void cpuinfo_set_wcid(uint32_t wcid) {
22 		cpuinfo_arm_wcid = wcid;
23 	}
24 #endif
25 
26 
cpuinfo_arm_linux_decode_isa_from_proc_cpuinfo(uint32_t features,uint32_t features2,uint32_t midr,uint32_t architecture_version,uint32_t architecture_flags,const struct cpuinfo_arm_chipset chipset[restrict static1],struct cpuinfo_arm_isa isa[restrict static1])27 void cpuinfo_arm_linux_decode_isa_from_proc_cpuinfo(
28 	uint32_t features,
29 	uint32_t features2,
30 	uint32_t midr,
31 	uint32_t architecture_version,
32 	uint32_t architecture_flags,
33 	const struct cpuinfo_arm_chipset chipset[restrict static 1],
34 	struct cpuinfo_arm_isa isa[restrict static 1])
35 {
36 	if (architecture_version >= 8) {
37 		/*
38 		 * ARMv7 code running on ARMv8: IDIV, VFP, NEON are always supported,
39 		 * but may be not reported in /proc/cpuinfo features.
40 		 */
41 		isa->armv5e  = true;
42 		isa->armv6   = true;
43 		isa->armv6k  = true;
44 		isa->armv7   = true;
45 		isa->armv7mp = true;
46 		isa->armv8   = true;
47 		isa->thumb  = true;
48 		isa->thumb2 = true;
49 		isa->idiv = true;
50 		isa->vfpv3 = true;
51 		isa->d32 = true;
52 		isa->fp16 = true;
53 		isa->fma = true;
54 		isa->neon = true;
55 
56 		/*
57 		 * NEON FP16 compute extension and VQRDMLAH/VQRDMLSH instructions are not indicated in /proc/cpuinfo.
58 		 * Use a MIDR-based heuristic to whitelist processors known to support it:
59 		 * - Processors with Cortex-A55 cores
60 		 * - Processors with Cortex-A65 cores
61 		 * - Processors with Cortex-A75 cores
62 		 * - Processors with Cortex-A76 cores
63 		 * - Processors with Cortex-A77 cores
64 		 * - Processors with Exynos M4 cores
65 		 * - Processors with Exynos M5 cores
66 		 * - Neoverse N1 cores
67 		 * - Neoverse V1 cores
68 		 * - Neoverse N2 cores
69 		 */
70 		if (chipset->series == cpuinfo_arm_chipset_series_samsung_exynos && chipset->model == 9810) {
71 			/* Only little cores of Exynos 9810 support FP16 & RDM */
72 			cpuinfo_log_warning("FP16 arithmetics and RDM disabled: only little cores in Exynos 9810 support these extensions");
73 		} else {
74 			switch (midr & (CPUINFO_ARM_MIDR_IMPLEMENTER_MASK | CPUINFO_ARM_MIDR_PART_MASK)) {
75 				case UINT32_C(0x4100D050): /* Cortex-A55 */
76 				case UINT32_C(0x4100D060): /* Cortex-A65 */
77 				case UINT32_C(0x4100D0B0): /* Cortex-A76 */
78 				case UINT32_C(0x4100D0D0): /* Cortex-A77 */
79 				case UINT32_C(0x4100D0E0): /* Cortex-A76AE */
80 				case UINT32_C(0x4100D460): /* Cortex-A510 */
81 				case UINT32_C(0x4100D470): /* Cortex-A710 */
82 				case UINT32_C(0x4100D480): /* Cortex-X2 */
83 				case UINT32_C(0x4800D400): /* Cortex-A76 (HiSilicon) */
84 				case UINT32_C(0x51008020): /* Kryo 385 Gold (Cortex-A75) */
85 				case UINT32_C(0x51008030): /* Kryo 385 Silver (Cortex-A55) */
86 				case UINT32_C(0x51008040): /* Kryo 485 Gold (Cortex-A76) */
87 				case UINT32_C(0x51008050): /* Kryo 485 Silver (Cortex-A55) */
88 				case UINT32_C(0x53000030): /* Exynos M4 */
89 				case UINT32_C(0x53000040): /* Exynos M5 */
90 					isa->fp16arith = true;
91 					isa->rdm = true;
92 					break;
93 			}
94 		}
95 
96 		/*
97 		 * NEON VDOT instructions are not indicated in /proc/cpuinfo.
98 		 * Use a MIDR-based heuristic to whitelist processors known to support it.
99 		 */
100 		switch (midr & (CPUINFO_ARM_MIDR_IMPLEMENTER_MASK | CPUINFO_ARM_MIDR_PART_MASK)) {
101 			case UINT32_C(0x4100D0B0): /* Cortex-A76 */
102 			case UINT32_C(0x4100D0D0): /* Cortex-A77 */
103 			case UINT32_C(0x4100D0E0): /* Cortex-A76AE */
104 			case UINT32_C(0x4800D400): /* Cortex-A76 (HiSilicon) */
105 			case UINT32_C(0x4100D460): /* Cortex-A510 */
106 			case UINT32_C(0x4100D470): /* Cortex-A710 */
107 			case UINT32_C(0x4100D480): /* Cortex-X2 */
108 			case UINT32_C(0x51008040): /* Kryo 485 Gold (Cortex-A76) */
109 			case UINT32_C(0x51008050): /* Kryo 485 Silver (Cortex-A55) */
110 			case UINT32_C(0x53000030): /* Exynos-M4 */
111 			case UINT32_C(0x53000040): /* Exynos-M5 */
112 				isa->dot = true;
113 				break;
114 			case UINT32_C(0x4100D050): /* Cortex A55: revision 1 or later only */
115 				isa->dot = !!(midr_get_variant(midr) >= 1);
116 				break;
117 			case UINT32_C(0x4100D0A0): /* Cortex A75: revision 2 or later only */
118 				isa->dot = !!(midr_get_variant(midr) >= 2);
119 				break;
120 		}
121 	} else {
122 		/* ARMv7 or lower: use feature flags to detect optional features */
123 
124 		/*
125 		 * ARM11 (ARM 1136/1156/1176/11 MPCore) processors can report v7 architecture
126 		 * even though they support only ARMv6 instruction set.
127 		 */
128 		if (architecture_version == 7 && midr_is_arm11(midr)) {
129 			cpuinfo_log_warning("kernel-reported architecture ARMv7 ignored due to mismatch with processor microarchitecture (ARM11)");
130 			architecture_version = 6;
131 		}
132 
133 		if (architecture_version < 7) {
134 			const uint32_t armv7_features_mask = CPUINFO_ARM_LINUX_FEATURE_VFPV3 | CPUINFO_ARM_LINUX_FEATURE_VFPV3D16 | CPUINFO_ARM_LINUX_FEATURE_VFPD32 |
135 				CPUINFO_ARM_LINUX_FEATURE_VFPV4 | CPUINFO_ARM_LINUX_FEATURE_NEON | CPUINFO_ARM_LINUX_FEATURE_IDIVT | CPUINFO_ARM_LINUX_FEATURE_IDIVA;
136 			if (features & armv7_features_mask) {
137 				architecture_version = 7;
138 			}
139 		}
140 		if ((architecture_version >= 6) || (features & CPUINFO_ARM_LINUX_FEATURE_EDSP) || (architecture_flags & CPUINFO_ARM_LINUX_ARCH_E)) {
141 			isa->armv5e = true;
142 		}
143 		if (architecture_version >= 6) {
144 			isa->armv6 = true;
145 		}
146 		if (architecture_version >= 7) {
147 			isa->armv6k = true;
148 			isa->armv7 = true;
149 
150 			/*
151 			 * ARMv7 MP extension (PLDW instruction) is not indicated in /proc/cpuinfo.
152 			 * Use heuristic list of supporting processors:
153 			 * - Processors supporting UDIV/SDIV instructions ("idiva" + "idivt" features in /proc/cpuinfo)
154 			 * - Cortex-A5
155 			 * - Cortex-A9
156 			 * - Dual-Core Scorpion
157 			 * - Krait (supports UDIV/SDIV, but kernels may not report it in /proc/cpuinfo)
158 			 *
159 			 * TODO: check single-core Qualcomm Scorpion.
160 			 */
161 			switch (midr & (CPUINFO_ARM_MIDR_IMPLEMENTER_MASK | CPUINFO_ARM_MIDR_PART_MASK)) {
162 				case UINT32_C(0x4100C050): /* Cortex-A5 */
163 				case UINT32_C(0x4100C090): /* Cortex-A9 */
164 				case UINT32_C(0x510002D0): /* Scorpion (dual-core) */
165 				case UINT32_C(0x510004D0): /* Krait (dual-core) */
166 				case UINT32_C(0x510006F0): /* Krait (quad-core) */
167 					isa->armv7mp = true;
168 					break;
169 				default:
170 					/* In practice IDIV instruction implies ARMv7+MP ISA */
171 					isa->armv7mp = (features & CPUINFO_ARM_LINUX_FEATURE_IDIV) == CPUINFO_ARM_LINUX_FEATURE_IDIV;
172 					break;
173 			}
174 		}
175 
176 		if (features & CPUINFO_ARM_LINUX_FEATURE_IWMMXT) {
177 			const uint32_t wcid = read_wcid();
178 			cpuinfo_log_debug("WCID = 0x%08"PRIx32, wcid);
179 			const uint32_t coprocessor_type = (wcid >> 8) & UINT32_C(0xFF);
180 			if (coprocessor_type >= 0x10) {
181 				isa->wmmx = true;
182 				if (coprocessor_type >= 0x20) {
183 					isa->wmmx2 = true;
184 				}
185 			} else {
186 				cpuinfo_log_warning("WMMX ISA disabled: OS reported iwmmxt feature, "
187 					"but WCID coprocessor type 0x%"PRIx32" indicates no WMMX support",
188 					coprocessor_type);
189 			}
190 		}
191 
192 		if ((features & CPUINFO_ARM_LINUX_FEATURE_THUMB) || (architecture_flags & CPUINFO_ARM_LINUX_ARCH_T)) {
193 			isa->thumb = true;
194 
195 			/*
196 			 * There is no separate feature flag for Thumb 2.
197 			 * All ARMv7 processors and ARM 1156 support Thumb 2.
198 			 */
199 			if (architecture_version >= 7 || midr_is_arm1156(midr)) {
200 				isa->thumb2 = true;
201 			}
202 		}
203 		if (features & CPUINFO_ARM_LINUX_FEATURE_THUMBEE) {
204 			isa->thumbee = true;
205 		}
206 		if ((features & CPUINFO_ARM_LINUX_FEATURE_JAVA) || (architecture_flags & CPUINFO_ARM_LINUX_ARCH_J)) {
207 			isa->jazelle = true;
208 		}
209 
210 		/* Qualcomm Krait may have buggy kernel configuration that doesn't report IDIV */
211 		if ((features & CPUINFO_ARM_LINUX_FEATURE_IDIV) == CPUINFO_ARM_LINUX_FEATURE_IDIV || midr_is_krait(midr)) {
212 			isa->idiv = true;
213 		}
214 
215 		const uint32_t vfp_mask = \
216 			CPUINFO_ARM_LINUX_FEATURE_VFP | CPUINFO_ARM_LINUX_FEATURE_VFPV3 | CPUINFO_ARM_LINUX_FEATURE_VFPV3D16 | \
217 			CPUINFO_ARM_LINUX_FEATURE_VFPD32 | CPUINFO_ARM_LINUX_FEATURE_VFPV4 | CPUINFO_ARM_LINUX_FEATURE_NEON;
218 		if (features & vfp_mask) {
219 			const uint32_t vfpv3_mask = CPUINFO_ARM_LINUX_FEATURE_VFPV3 | CPUINFO_ARM_LINUX_FEATURE_VFPV3D16 | \
220 				CPUINFO_ARM_LINUX_FEATURE_VFPD32 | CPUINFO_ARM_LINUX_FEATURE_VFPV4 | CPUINFO_ARM_LINUX_FEATURE_NEON;
221 			if ((architecture_version >= 7) || (features & vfpv3_mask)) {
222 				isa->vfpv3 = true;
223 
224 				const uint32_t d32_mask = CPUINFO_ARM_LINUX_FEATURE_VFPD32 | CPUINFO_ARM_LINUX_FEATURE_NEON;
225 				if (features & d32_mask) {
226 					isa->d32 = true;
227 				}
228 			} else {
229 				#if defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_8A__) || defined(__ARM_ARCH) && (__ARM_ARCH >= 7)
230 					isa->vfpv3 = true;
231 				#else
232 					const uint32_t fpsid = read_fpsid();
233 					cpuinfo_log_debug("FPSID = 0x%08"PRIx32, fpsid);
234 					const uint32_t subarchitecture = (fpsid >> 16) & UINT32_C(0x7F);
235 					if (subarchitecture >= 0x01) {
236 						isa->vfpv2 = true;
237 					}
238 				#endif
239 			}
240 		}
241 		if (features & CPUINFO_ARM_LINUX_FEATURE_NEON) {
242 			isa->neon = true;
243 		}
244 
245 		/*
246 		 * There is no separate feature flag for FP16 support.
247 		 * VFPv4 implies VFPv3-FP16 support (and in practice, NEON-HP as well).
248 		 * Additionally, ARM Cortex-A9 and Qualcomm Scorpion support FP16.
249 		 */
250 		if ((features & CPUINFO_ARM_LINUX_FEATURE_VFPV4) || midr_is_cortex_a9(midr) || midr_is_scorpion(midr)) {
251 			isa->fp16 = true;
252 		}
253 
254 		if (features & CPUINFO_ARM_LINUX_FEATURE_VFPV4) {
255 			isa->fma = true;
256 		}
257 	}
258 
259 	if (features2 & CPUINFO_ARM_LINUX_FEATURE2_AES) {
260 		isa->aes = true;
261 	}
262 	if (features2 & CPUINFO_ARM_LINUX_FEATURE2_PMULL) {
263 		isa->pmull = true;
264 	}
265 	if (features2 & CPUINFO_ARM_LINUX_FEATURE2_SHA1) {
266 		isa->sha1 = true;
267 	}
268 	if (features2 & CPUINFO_ARM_LINUX_FEATURE2_SHA2) {
269 		isa->sha2 = true;
270 	}
271 	if (features2 & CPUINFO_ARM_LINUX_FEATURE2_CRC32) {
272 		isa->crc32 = true;
273 	}
274 }
275