xref: /rk3399_ARM-atf/common/feat_detect.c (revision d8fdff38b544b79c4f0b757e3b3c82ce9c8a2f9e)
1 /*
2  * Copyright (c) 2022-2025, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <arch_features.h>
8 #include <common/debug.h>
9 #include <common/feat_detect.h>
10 
11 static bool tainted;
12 
13 /*******************************************************************************
14  * This section lists the wrapper modules for each feature to evaluate the
15  * feature states (FEAT_STATE_ALWAYS and FEAT_STATE_CHECK) and perform
16  * necessary action as below:
17  *
18  * It verifies whether the FEAT_XXX (eg: FEAT_SB) is supported by the PE or not.
19  * Without this check an exception would occur during context save/restore
20  * routines, if the feature is enabled but not supported by PE.
21  ******************************************************************************/
22 
23 #define feat_detect_panic(a, b)		((a) ? (void)0 : feature_panic(b))
24 
25 /*******************************************************************************
26  * Function : feature_panic
27  * Customised panic function with error logging mechanism to list the feature
28  * not supported by the PE.
29  ******************************************************************************/
30 static inline void feature_panic(char *feat_name)
31 {
32 	ERROR("FEAT_%s not supported by the PE\n", feat_name);
33 	panic();
34 }
35 
36 /*******************************************************************************
37  * Function : check_feature
38  * Check for a valid combination of build time flags (ENABLE_FEAT_xxx) and
39  * feature availability on the hardware. <min> is the smallest feature
40  * ID field value that is required for that feature.
41  * Triggers a panic later if a feature is forcefully enabled, but not
42  * available on the PE. Also will panic if the hardware feature ID field
43  * is larger than the maximum known and supported number, specified by <max>.
44  *
45  * We force inlining here to let the compiler optimise away the whole check
46  * if the feature is disabled at build time (FEAT_STATE_DISABLED).
47  ******************************************************************************/
48 static inline void __attribute((__always_inline__))
49 check_feature(int state, unsigned long field, const char *feat_name,
50 	      unsigned int min, unsigned int max)
51 {
52 	if (state == FEAT_STATE_ALWAYS && field < min) {
53 		ERROR("FEAT_%s not supported by the PE\n", feat_name);
54 		tainted = true;
55 	}
56 	if (state >= FEAT_STATE_ALWAYS && field > max) {
57 		ERROR("FEAT_%s is version %ld, but is only known up to version %d\n",
58 		      feat_name, field, max);
59 		tainted = true;
60 	}
61 }
62 
63 /************************************************
64  * Feature : FEAT_PAUTH (Pointer Authentication)
65  ***********************************************/
66 static void read_feat_pauth(void)
67 {
68 #if (ENABLE_PAUTH == FEAT_STATE_ALWAYS) || (CTX_INCLUDE_PAUTH_REGS == FEAT_STATE_ALWAYS)
69 	feat_detect_panic(is_feat_pauth_present(), "PAUTH");
70 #endif
71 }
72 
73 static unsigned int read_feat_rng_trap_id_field(void)
74 {
75 	return ISOLATE_FIELD(read_id_aa64pfr1_el1(), ID_AA64PFR1_EL1_RNDR_TRAP_SHIFT,
76 			     ID_AA64PFR1_EL1_RNDR_TRAP_MASK);
77 }
78 
79 static unsigned int read_feat_bti_id_field(void)
80 {
81 	return ISOLATE_FIELD(read_id_aa64pfr1_el1(), ID_AA64PFR1_EL1_BT_SHIFT,
82 			     ID_AA64PFR1_EL1_BT_MASK);
83 }
84 
85 static unsigned int read_feat_sb_id_field(void)
86 {
87 	return ISOLATE_FIELD(read_id_aa64isar1_el1(), ID_AA64ISAR1_SB_SHIFT,
88 			     ID_AA64ISAR1_SB_MASK);
89 }
90 
91 static unsigned int read_feat_csv2_id_field(void)
92 {
93 	return ISOLATE_FIELD(read_id_aa64pfr0_el1(), ID_AA64PFR0_CSV2_SHIFT,
94 			     ID_AA64PFR0_CSV2_MASK);
95 }
96 
97 static unsigned int read_feat_debugv8p9_id_field(void)
98 {
99 	return ISOLATE_FIELD(read_id_aa64dfr0_el1(), ID_AA64DFR0_DEBUGVER_SHIFT,
100 			     ID_AA64DFR0_DEBUGVER_MASK);
101 }
102 
103 static unsigned int read_feat_pmuv3_id_field(void)
104 {
105 	return ISOLATE_FIELD(read_id_aa64dfr0_el1(), ID_AA64DFR0_PMUVER_SHIFT,
106 			     ID_AA64DFR0_PMUVER_MASK);
107 }
108 
109 static unsigned int read_feat_vhe_id_field(void)
110 {
111 	return ISOLATE_FIELD(read_id_aa64mmfr1_el1(), ID_AA64MMFR1_EL1_VHE_SHIFT,
112 			     ID_AA64MMFR1_EL1_VHE_MASK);
113 }
114 
115 static unsigned int read_feat_sve_id_field(void)
116 {
117 	return ISOLATE_FIELD(read_id_aa64pfr0_el1(), ID_AA64PFR0_SVE_SHIFT,
118 			     ID_AA64PFR0_SVE_MASK);
119 }
120 
121 static unsigned int read_feat_ras_id_field(void)
122 {
123 	return ISOLATE_FIELD(read_id_aa64pfr0_el1(), ID_AA64PFR0_RAS_SHIFT,
124 			     ID_AA64PFR0_RAS_MASK);
125 }
126 
127 static unsigned int read_feat_dit_id_field(void)
128 {
129 	return ISOLATE_FIELD(read_id_aa64pfr0_el1(), ID_AA64PFR0_DIT_SHIFT,
130 			     ID_AA64PFR0_DIT_MASK);
131 }
132 
133 static unsigned int  read_feat_amu_id_field(void)
134 {
135 	return ISOLATE_FIELD(read_id_aa64pfr0_el1(), ID_AA64PFR0_AMU_SHIFT,
136 			     ID_AA64PFR0_AMU_MASK);
137 }
138 
139 static unsigned int read_feat_mpam_version(void)
140 {
141 	return (unsigned int)((((read_id_aa64pfr0_el1() >>
142 		ID_AA64PFR0_MPAM_SHIFT) & ID_AA64PFR0_MPAM_MASK) << 4) |
143 			((read_id_aa64pfr1_el1() >>
144 		ID_AA64PFR1_MPAM_FRAC_SHIFT) & ID_AA64PFR1_MPAM_FRAC_MASK));
145 }
146 
147 static unsigned int read_feat_nv_id_field(void)
148 {
149 	return ISOLATE_FIELD(read_id_aa64mmfr2_el1(), ID_AA64MMFR2_EL1_NV_SHIFT,
150 			     ID_AA64MMFR2_EL1_NV_MASK);
151 }
152 
153 static unsigned int read_feat_sel2_id_field(void)
154 {
155 	return ISOLATE_FIELD(read_id_aa64pfr0_el1(), ID_AA64PFR0_SEL2_SHIFT,
156 			     ID_AA64PFR0_SEL2_MASK);
157 }
158 
159 static unsigned int read_feat_trf_id_field(void)
160 {
161 	return ISOLATE_FIELD(read_id_aa64dfr0_el1(), ID_AA64DFR0_TRACEFILT_SHIFT,
162 			     ID_AA64DFR0_TRACEFILT_MASK);
163 }
164 static unsigned int get_armv8_5_mte_support(void)
165 {
166 	return ISOLATE_FIELD(read_id_aa64pfr1_el1(), ID_AA64PFR1_EL1_MTE_SHIFT,
167 			     ID_AA64PFR1_EL1_MTE_MASK);
168 }
169 static unsigned int read_feat_rng_id_field(void)
170 {
171 	return ISOLATE_FIELD(read_id_aa64isar0_el1(), ID_AA64ISAR0_RNDR_SHIFT,
172 			     ID_AA64ISAR0_RNDR_MASK);
173 }
174 static unsigned int read_feat_fgt_id_field(void)
175 {
176 	return ISOLATE_FIELD(read_id_aa64mmfr0_el1(), ID_AA64MMFR0_EL1_FGT_SHIFT,
177 			     ID_AA64MMFR0_EL1_FGT_MASK);
178 }
179 static unsigned int read_feat_ecv_id_field(void)
180 {
181 	return ISOLATE_FIELD(read_id_aa64mmfr0_el1(), ID_AA64MMFR0_EL1_ECV_SHIFT,
182 			     ID_AA64MMFR0_EL1_ECV_MASK);
183 }
184 static unsigned int read_feat_twed_id_field(void)
185 {
186 	return ISOLATE_FIELD(read_id_aa64mmfr1_el1(), ID_AA64MMFR1_EL1_TWED_SHIFT,
187 			     ID_AA64MMFR1_EL1_TWED_MASK);
188 }
189 
190 static unsigned int read_feat_hcx_id_field(void)
191 {
192 	return ISOLATE_FIELD(read_id_aa64mmfr1_el1(), ID_AA64MMFR1_EL1_HCX_SHIFT,
193 			     ID_AA64MMFR1_EL1_HCX_MASK);
194 }
195 static unsigned int read_feat_ls64_id_field(void)
196 {
197 	return ISOLATE_FIELD(read_id_aa64isar1_el1(), ID_AA64ISAR1_LS64_SHIFT,
198 			     ID_AA64ISAR1_LS64_MASK);
199 }
200 static unsigned int read_feat_tcr2_id_field(void)
201 {
202 	return ISOLATE_FIELD(read_id_aa64mmfr3_el1(), ID_AA64MMFR3_EL1_TCRX_SHIFT,
203 			     ID_AA64MMFR3_EL1_TCRX_MASK);
204 }
205 static unsigned int read_feat_s2pie_id_field(void)
206 {
207 	return ISOLATE_FIELD(read_id_aa64mmfr3_el1(), ID_AA64MMFR3_EL1_S2PIE_SHIFT,
208 			     ID_AA64MMFR3_EL1_S2PIE_MASK);
209 }
210 static unsigned int read_feat_s1pie_id_field(void)
211 {
212 	return ISOLATE_FIELD(read_id_aa64mmfr3_el1(), ID_AA64MMFR3_EL1_S1PIE_SHIFT,
213 			     ID_AA64MMFR3_EL1_S1PIE_MASK);
214 }
215 static unsigned int read_feat_s2poe_id_field(void)
216 {
217 	return ISOLATE_FIELD(read_id_aa64mmfr3_el1(), ID_AA64MMFR3_EL1_S2POE_SHIFT,
218 			     ID_AA64MMFR3_EL1_S2POE_MASK);
219 }
220 static unsigned int read_feat_s1poe_id_field(void)
221 {
222 	return ISOLATE_FIELD(read_id_aa64mmfr3_el1(), ID_AA64MMFR3_EL1_S1POE_SHIFT,
223 			     ID_AA64MMFR3_EL1_S1POE_MASK);
224 }
225 static unsigned int read_feat_brbe_id_field(void)
226 {
227 	return ISOLATE_FIELD(read_id_aa64dfr0_el1(), ID_AA64DFR0_BRBE_SHIFT,
228 			     ID_AA64DFR0_BRBE_MASK);
229 }
230 static unsigned int read_feat_trbe_id_field(void)
231 {
232 	return ISOLATE_FIELD(read_id_aa64dfr0_el1(), ID_AA64DFR0_TRACEBUFFER_SHIFT,
233 			     ID_AA64DFR0_TRACEBUFFER_MASK);
234 }
235 static unsigned int read_feat_sme_id_field(void)
236 {
237 	return ISOLATE_FIELD(read_id_aa64pfr1_el1(), ID_AA64PFR1_EL1_SME_SHIFT,
238 			     ID_AA64PFR1_EL1_SME_MASK);
239 }
240 static unsigned int read_feat_gcs_id_field(void)
241 {
242 	return ISOLATE_FIELD(read_id_aa64pfr1_el1(), ID_AA64PFR1_EL1_GCS_SHIFT,
243 			     ID_AA64PFR1_EL1_GCS_MASK);
244 }
245 
246 static unsigned int read_feat_rme_id_field(void)
247 {
248 	return ISOLATE_FIELD(read_id_aa64pfr0_el1(), ID_AA64PFR0_FEAT_RME_SHIFT,
249 			     ID_AA64PFR0_FEAT_RME_MASK);
250 }
251 
252 static unsigned int read_feat_pan_id_field(void)
253 {
254 	return ISOLATE_FIELD(read_id_aa64mmfr1_el1(), ID_AA64MMFR1_EL1_PAN_SHIFT,
255 			     ID_AA64MMFR1_EL1_PAN_MASK);
256 }
257 
258 static unsigned int read_feat_mtpmu_id_field(void)
259 {
260 	return ISOLATE_FIELD(read_id_aa64dfr0_el1(), ID_AA64DFR0_MTPMU_SHIFT,
261 			     ID_AA64DFR0_MTPMU_MASK);
262 
263 }
264 
265 static unsigned int read_feat_the_id_field(void)
266 {
267 	return ISOLATE_FIELD(read_id_aa64pfr1_el1(), ID_AA64PFR1_EL1_THE_SHIFT,
268 			     ID_AA64PFR1_EL1_THE_MASK);
269 }
270 
271 static unsigned int read_feat_sctlr2_id_field(void)
272 {
273 	return ISOLATE_FIELD(read_id_aa64mmfr3_el1(), ID_AA64MMFR3_EL1_SCTLR2_SHIFT,
274 			     ID_AA64MMFR3_EL1_SCTLR2_MASK);
275 }
276 
277 static unsigned int read_feat_d128_id_field(void)
278 {
279 	return ISOLATE_FIELD(read_id_aa64mmfr3_el1(), ID_AA64MMFR3_EL1_D128_SHIFT,
280 			     ID_AA64MMFR3_EL1_D128_MASK);
281 }
282 static unsigned int read_feat_gcie_id_field(void)
283 {
284 	return ISOLATE_FIELD(read_id_aa64pfr2_el1(), ID_AA64PFR2_EL1_GCIE_SHIFT,
285 			     ID_AA64PFR2_EL1_GCIE_MASK);
286 }
287 
288 static unsigned int read_feat_fpmr_id_field(void)
289 {
290 	return ISOLATE_FIELD(read_id_aa64pfr2_el1(), ID_AA64PFR2_EL1_FPMR_SHIFT,
291 			     ID_AA64PFR2_EL1_FPMR_MASK);
292 }
293 
294 static unsigned int read_feat_mops_id_field(void)
295 {
296 	return ISOLATE_FIELD(read_id_aa64isar2_el1(), ID_AA64ISAR2_EL1_MOPS_SHIFT,
297 			     ID_AA64ISAR2_EL1_MOPS_MASK);
298 }
299 
300 /***********************************************************************************
301  * TF-A supports many Arm architectural features starting from arch version
302  * (8.0 till 8.7+). These features are mostly enabled through build flags. This
303  * mechanism helps in validating these build flags in the early boot phase
304  * either in BL1 or BL31 depending on the platform and assists in identifying
305  * and notifying the features which are enabled but not supported by the PE.
306  *
307  * It reads all the enabled features ID-registers and ensures the features
308  * are supported by the PE.
309  * In case if they aren't it stops booting at an early phase and logs the error
310  * messages, notifying the platforms about the features that are not supported.
311  *
312  * Further the procedure is implemented with a tri-state approach for each feature:
313  * ENABLE_FEAT_xxx = 0 : The feature is disabled statically at compile time
314  * ENABLE_FEAT_xxx = 1 : The feature is enabled and must be present in hardware.
315  *                       There will be panic if feature is not present at cold boot.
316  * ENABLE_FEAT_xxx = 2 : The feature is enabled but dynamically enabled at runtime
317  *                       depending on hardware capability.
318  *
319  * For better readability, state values are defined with macros, namely:
320  * { FEAT_STATE_DISABLED, FEAT_STATE_ALWAYS, FEAT_STATE_CHECK }, taking values
321  * { 0, 1, 2 }, respectively, as their naming.
322  **********************************************************************************/
323 void detect_arch_features(void)
324 {
325 	tainted = false;
326 
327 	/* v8.0 features */
328 	check_feature(ENABLE_FEAT_SB, read_feat_sb_id_field(), "SB", 1, 1);
329 	check_feature(ENABLE_FEAT_CSV2_2, read_feat_csv2_id_field(),
330 		      "CSV2_2", 2, 3);
331 	/*
332 	 * Even though the PMUv3 is an OPTIONAL feature, it is always
333 	 * implemented and Arm prescribes so. So assume it will be there and do
334 	 * away with a flag for it. This is used to check minor PMUv3px
335 	 * revisions so that we catch them as they come along
336 	 */
337 	check_feature(FEAT_STATE_ALWAYS, read_feat_pmuv3_id_field(),
338 		      "PMUv3", 1, ID_AA64DFR0_PMUVER_PMUV3P9);
339 
340 	/* v8.1 features */
341 	check_feature(ENABLE_FEAT_PAN, read_feat_pan_id_field(), "PAN", 1, 3);
342 	check_feature(ENABLE_FEAT_VHE, read_feat_vhe_id_field(), "VHE", 1, 1);
343 
344 	/* v8.2 features */
345 	check_feature(ENABLE_SVE_FOR_NS, read_feat_sve_id_field(),
346 		      "SVE", 1, 1);
347 	check_feature(ENABLE_FEAT_RAS, read_feat_ras_id_field(), "RAS", 1, 2);
348 
349 	/* v8.3 features */
350 	/* TODO: Pauth yet to convert to tri-state feat detect logic */
351 	read_feat_pauth();
352 
353 	/* v8.4 features */
354 	check_feature(ENABLE_FEAT_DIT, read_feat_dit_id_field(), "DIT", 1, 1);
355 	check_feature(ENABLE_FEAT_AMU, read_feat_amu_id_field(),
356 		      "AMUv1", 1, 2);
357 	check_feature(ENABLE_FEAT_MOPS, read_feat_mops_id_field(),
358 		      "MOPS", 1, 1);
359 	check_feature(ENABLE_FEAT_MPAM, read_feat_mpam_version(),
360 		      "MPAM", 1, 17);
361 	check_feature(CTX_INCLUDE_NEVE_REGS, read_feat_nv_id_field(),
362 		      "NV2", 2, 2);
363 	check_feature(ENABLE_FEAT_SEL2, read_feat_sel2_id_field(),
364 		      "SEL2", 1, 1);
365 	check_feature(ENABLE_TRF_FOR_NS, read_feat_trf_id_field(),
366 		      "TRF", 1, 1);
367 
368 	/* v8.5 features */
369 	check_feature(ENABLE_FEAT_MTE2, get_armv8_5_mte_support(), "MTE2",
370 		      MTE_IMPLEMENTED_ELX, MTE_IMPLEMENTED_ASY);
371 	check_feature(ENABLE_FEAT_RNG, read_feat_rng_id_field(), "RNG", 1, 1);
372 	check_feature(ENABLE_BTI, read_feat_bti_id_field(), "BTI", 1, 1);
373 	check_feature(ENABLE_FEAT_RNG_TRAP, read_feat_rng_trap_id_field(),
374 		      "RNG_TRAP", 1, 1);
375 
376 	/* v8.6 features */
377 	check_feature(ENABLE_FEAT_AMUv1p1, read_feat_amu_id_field(),
378 		      "AMUv1p1", 2, 2);
379 	check_feature(ENABLE_FEAT_FGT, read_feat_fgt_id_field(), "FGT", 1, 2);
380 	check_feature(ENABLE_FEAT_FGT2, read_feat_fgt_id_field(), "FGT2", 2, 2);
381 	check_feature(ENABLE_FEAT_ECV, read_feat_ecv_id_field(), "ECV", 1, 2);
382 	check_feature(ENABLE_FEAT_TWED, read_feat_twed_id_field(),
383 		      "TWED", 1, 1);
384 
385 	/*
386 	 * even though this is a "DISABLE" it does confusingly perform feature
387 	 * enablement duties like all other flags here. Check it against the HW
388 	 * feature when we intend to diverge from the default behaviour
389 	 */
390 	check_feature(DISABLE_MTPMU, read_feat_mtpmu_id_field(), "MTPMU", 1, 1);
391 
392 	/* v8.7 features */
393 	check_feature(ENABLE_FEAT_HCX, read_feat_hcx_id_field(), "HCX", 1, 1);
394 	check_feature(ENABLE_FEAT_LS64_ACCDATA, read_feat_ls64_id_field(), "LS64", 1, 3);
395 
396 	/* v8.9 features */
397 	check_feature(ENABLE_FEAT_TCR2, read_feat_tcr2_id_field(),
398 		      "TCR2", 1, 1);
399 	check_feature(ENABLE_FEAT_S2PIE, read_feat_s2pie_id_field(),
400 		      "S2PIE", 1, 1);
401 	check_feature(ENABLE_FEAT_S1PIE, read_feat_s1pie_id_field(),
402 		      "S1PIE", 1, 1);
403 	check_feature(ENABLE_FEAT_S2POE, read_feat_s2poe_id_field(),
404 		      "S2POE", 1, 1);
405 	check_feature(ENABLE_FEAT_S1POE, read_feat_s1poe_id_field(),
406 		      "S1POE", 1, 1);
407 	check_feature(ENABLE_FEAT_CSV2_3, read_feat_csv2_id_field(),
408 		      "CSV2_3", 3, 3);
409 	check_feature(ENABLE_FEAT_DEBUGV8P9, read_feat_debugv8p9_id_field(),
410 			"DEBUGV8P9", 11, 11);
411 	check_feature(ENABLE_FEAT_THE, read_feat_the_id_field(),
412 			"THE", 1, 1);
413 	check_feature(ENABLE_FEAT_SCTLR2, read_feat_sctlr2_id_field(),
414 			"SCTLR2", 1, 1);
415 
416 	/* v9.0 features */
417 	check_feature(ENABLE_BRBE_FOR_NS, read_feat_brbe_id_field(),
418 		      "BRBE", 1, 2);
419 	check_feature(ENABLE_TRBE_FOR_NS, read_feat_trbe_id_field(),
420 		      "TRBE", 1, 1);
421 
422 	/* v9.2 features */
423 	check_feature(ENABLE_SME_FOR_NS, read_feat_sme_id_field(),
424 		      "SME", 1, 2);
425 	check_feature(ENABLE_SME2_FOR_NS, read_feat_sme_id_field(),
426 		      "SME2", 2, 2);
427 	check_feature(ENABLE_FEAT_FPMR, read_feat_fpmr_id_field(),
428 		      "FPMR", 1, 1);
429 
430 	/* v9.3 features */
431 	check_feature(ENABLE_FEAT_D128, read_feat_d128_id_field(),
432 		      "D128", 1, 1);
433 	check_feature(ENABLE_FEAT_GCIE, read_feat_gcie_id_field(),
434 		      "GCIE", 1, 1);
435 
436 	/* v9.4 features */
437 	check_feature(ENABLE_FEAT_GCS, read_feat_gcs_id_field(), "GCS", 1, 1);
438 	check_feature(ENABLE_RME, read_feat_rme_id_field(), "RME", 1, 1);
439 	check_feature(ENABLE_FEAT_PAUTH_LR, is_feat_pauth_lr_present(), "PAUTH_LR", 1, 1);
440 
441 	if (tainted) {
442 		panic();
443 	}
444 }
445