16a0da736SJayanth Dodderi Chidanand /* 20a33adc0SGovindraj Raja * Copyright (c) 2022-2024, Arm Limited and Contributors. All rights reserved. 36a0da736SJayanth Dodderi Chidanand * 46a0da736SJayanth Dodderi Chidanand * SPDX-License-Identifier: BSD-3-Clause 56a0da736SJayanth Dodderi Chidanand */ 66a0da736SJayanth Dodderi Chidanand 769c17f52SAndre Przywara #include <arch_features.h> 869c17f52SAndre Przywara #include <common/debug.h> 96a0da736SJayanth Dodderi Chidanand #include <common/feat_detect.h> 106a0da736SJayanth Dodderi Chidanand 11b45dd74eSAndre Przywara static bool tainted; 12b45dd74eSAndre Przywara 136a0da736SJayanth Dodderi Chidanand /******************************************************************************* 146a0da736SJayanth Dodderi Chidanand * This section lists the wrapper modules for each feature to evaluate the 1569c17f52SAndre Przywara * feature states (FEAT_STATE_ALWAYS and FEAT_STATE_CHECK) and perform 1669c17f52SAndre Przywara * necessary action as below: 176a0da736SJayanth Dodderi Chidanand * 186a0da736SJayanth Dodderi Chidanand * It verifies whether the FEAT_XXX (eg: FEAT_SB) is supported by the PE or not. 196a0da736SJayanth Dodderi Chidanand * Without this check an exception would occur during context save/restore 206a0da736SJayanth Dodderi Chidanand * routines, if the feature is enabled but not supported by PE. 216a0da736SJayanth Dodderi Chidanand ******************************************************************************/ 226a0da736SJayanth Dodderi Chidanand 2369c17f52SAndre Przywara #define feat_detect_panic(a, b) ((a) ? (void)0 : feature_panic(b)) 2469c17f52SAndre Przywara 2569c17f52SAndre Przywara /******************************************************************************* 2669c17f52SAndre Przywara * Function : feature_panic 2769c17f52SAndre Przywara * Customised panic function with error logging mechanism to list the feature 2869c17f52SAndre Przywara * not supported by the PE. 2969c17f52SAndre Przywara ******************************************************************************/ 3069c17f52SAndre Przywara static inline void feature_panic(char *feat_name) 3169c17f52SAndre Przywara { 3269c17f52SAndre Przywara ERROR("FEAT_%s not supported by the PE\n", feat_name); 3369c17f52SAndre Przywara panic(); 3469c17f52SAndre Przywara } 3569c17f52SAndre Przywara 36b45dd74eSAndre Przywara /******************************************************************************* 37b45dd74eSAndre Przywara * Function : check_feature 38b45dd74eSAndre Przywara * Check for a valid combination of build time flags (ENABLE_FEAT_xxx) and 39a4cccb4fSAndre Przywara * feature availability on the hardware. <min> is the smallest feature 40a4cccb4fSAndre Przywara * ID field value that is required for that feature. 41a4cccb4fSAndre Przywara * Triggers a panic later if a feature is forcefully enabled, but not 42a4cccb4fSAndre Przywara * available on the PE. Also will panic if the hardware feature ID field 43a4cccb4fSAndre Przywara * is larger than the maximum known and supported number, specified by <max>. 44b45dd74eSAndre Przywara * 45b45dd74eSAndre Przywara * We force inlining here to let the compiler optimise away the whole check 46b45dd74eSAndre Przywara * if the feature is disabled at build time (FEAT_STATE_DISABLED). 47b45dd74eSAndre Przywara ******************************************************************************/ 48b45dd74eSAndre Przywara static inline void __attribute((__always_inline__)) 49a4cccb4fSAndre Przywara check_feature(int state, unsigned long field, const char *feat_name, 50a4cccb4fSAndre Przywara unsigned int min, unsigned int max) 51b45dd74eSAndre Przywara { 52a4cccb4fSAndre Przywara if (state == FEAT_STATE_ALWAYS && field < min) { 53b45dd74eSAndre Przywara ERROR("FEAT_%s not supported by the PE\n", feat_name); 54b45dd74eSAndre Przywara tainted = true; 55b45dd74eSAndre Przywara } 56a4cccb4fSAndre Przywara if (state >= FEAT_STATE_ALWAYS && field > max) { 57a4cccb4fSAndre Przywara ERROR("FEAT_%s is version %ld, but is only known up to version %d\n", 58a4cccb4fSAndre Przywara feat_name, field, max); 59a4cccb4fSAndre Przywara tainted = true; 60a4cccb4fSAndre Przywara } 61b45dd74eSAndre Przywara } 62b45dd74eSAndre Przywara 636a0da736SJayanth Dodderi Chidanand /************************************************ 646a0da736SJayanth Dodderi Chidanand * Feature : FEAT_PAUTH (Pointer Authentication) 656a0da736SJayanth Dodderi Chidanand ***********************************************/ 666a0da736SJayanth Dodderi Chidanand static void read_feat_pauth(void) 676a0da736SJayanth Dodderi Chidanand { 6869c17f52SAndre Przywara #if (ENABLE_PAUTH == FEAT_STATE_ALWAYS) || (CTX_INCLUDE_PAUTH_REGS == FEAT_STATE_ALWAYS) 696a0da736SJayanth Dodderi Chidanand feat_detect_panic(is_armv8_3_pauth_present(), "PAUTH"); 706a0da736SJayanth Dodderi Chidanand #endif 716a0da736SJayanth Dodderi Chidanand } 726a0da736SJayanth Dodderi Chidanand 736a0da736SJayanth Dodderi Chidanand /**************************************************** 746a0da736SJayanth Dodderi Chidanand * Feature : FEAT_BTI (Branch Target Identification) 756a0da736SJayanth Dodderi Chidanand ***************************************************/ 766a0da736SJayanth Dodderi Chidanand static void read_feat_bti(void) 776a0da736SJayanth Dodderi Chidanand { 7869c17f52SAndre Przywara #if (ENABLE_BTI == FEAT_STATE_ALWAYS) 79*aaaf2cc3SSona Mathew feat_detect_panic(is_feat_bti_present(), "BTI"); 806a0da736SJayanth Dodderi Chidanand #endif 816a0da736SJayanth Dodderi Chidanand } 826a0da736SJayanth Dodderi Chidanand 836a0da736SJayanth Dodderi Chidanand /************************************************** 846a0da736SJayanth Dodderi Chidanand * Feature : FEAT_RME (Realm Management Extension) 856a0da736SJayanth Dodderi Chidanand *************************************************/ 866a0da736SJayanth Dodderi Chidanand static void read_feat_rme(void) 876a0da736SJayanth Dodderi Chidanand { 8869c17f52SAndre Przywara #if (ENABLE_RME == FEAT_STATE_ALWAYS) 89*aaaf2cc3SSona Mathew feat_detect_panic(is_feat_rme_present(), "RME"); 906a0da736SJayanth Dodderi Chidanand #endif 916a0da736SJayanth Dodderi Chidanand } 926a0da736SJayanth Dodderi Chidanand 93ff86e0b4SJuan Pablo Conde /****************************************************************** 94ff86e0b4SJuan Pablo Conde * Feature : FEAT_RNG_TRAP (Trapping support for RNDR/RNDRRS) 95ff86e0b4SJuan Pablo Conde *****************************************************************/ 96ff86e0b4SJuan Pablo Conde static void read_feat_rng_trap(void) 97ff86e0b4SJuan Pablo Conde { 9869c17f52SAndre Przywara #if (ENABLE_FEAT_RNG_TRAP == FEAT_STATE_ALWAYS) 99ff86e0b4SJuan Pablo Conde feat_detect_panic(is_feat_rng_trap_present(), "RNG_TRAP"); 100ff86e0b4SJuan Pablo Conde #endif 101ff86e0b4SJuan Pablo Conde } 102ff86e0b4SJuan Pablo Conde 1036a0da736SJayanth Dodderi Chidanand /*********************************************************************************** 1046a0da736SJayanth Dodderi Chidanand * TF-A supports many Arm architectural features starting from arch version 1056a0da736SJayanth Dodderi Chidanand * (8.0 till 8.7+). These features are mostly enabled through build flags. This 1066a0da736SJayanth Dodderi Chidanand * mechanism helps in validating these build flags in the early boot phase 1076a0da736SJayanth Dodderi Chidanand * either in BL1 or BL31 depending on the platform and assists in identifying 1086a0da736SJayanth Dodderi Chidanand * and notifying the features which are enabled but not supported by the PE. 1096a0da736SJayanth Dodderi Chidanand * 1106a0da736SJayanth Dodderi Chidanand * It reads all the enabled features ID-registers and ensures the features 1116a0da736SJayanth Dodderi Chidanand * are supported by the PE. 1126a0da736SJayanth Dodderi Chidanand * In case if they aren't it stops booting at an early phase and logs the error 1136a0da736SJayanth Dodderi Chidanand * messages, notifying the platforms about the features that are not supported. 1146a0da736SJayanth Dodderi Chidanand * 1156a0da736SJayanth Dodderi Chidanand * Further the procedure is implemented with a tri-state approach for each feature: 1166a0da736SJayanth Dodderi Chidanand * ENABLE_FEAT_xxx = 0 : The feature is disabled statically at compile time 1176a0da736SJayanth Dodderi Chidanand * ENABLE_FEAT_xxx = 1 : The feature is enabled and must be present in hardware. 1186a0da736SJayanth Dodderi Chidanand * There will be panic if feature is not present at cold boot. 1196a0da736SJayanth Dodderi Chidanand * ENABLE_FEAT_xxx = 2 : The feature is enabled but dynamically enabled at runtime 1206a0da736SJayanth Dodderi Chidanand * depending on hardware capability. 1216a0da736SJayanth Dodderi Chidanand * 12269c17f52SAndre Przywara * For better readability, state values are defined with macros, namely: 12369c17f52SAndre Przywara * { FEAT_STATE_DISABLED, FEAT_STATE_ALWAYS, FEAT_STATE_CHECK }, taking values 12469c17f52SAndre Przywara * { 0, 1, 2 }, respectively, as their naming. 1256a0da736SJayanth Dodderi Chidanand **********************************************************************************/ 1266a0da736SJayanth Dodderi Chidanand void detect_arch_features(void) 1276a0da736SJayanth Dodderi Chidanand { 128b45dd74eSAndre Przywara tainted = false; 129b45dd74eSAndre Przywara 1306a0da736SJayanth Dodderi Chidanand /* v8.0 features */ 131*aaaf2cc3SSona Mathew check_feature(ENABLE_FEAT_SB, read_feat_sb_id_field(), "SB", 1, 1); 1327db710f0SAndre Przywara check_feature(ENABLE_FEAT_CSV2_2, read_feat_csv2_id_field(), 133*aaaf2cc3SSona Mathew "CSV2_2", 2, 3); 134c73686a1SBoyan Karatotev /* 135c73686a1SBoyan Karatotev * Even though the PMUv3 is an OPTIONAL feature, it is always 136c73686a1SBoyan Karatotev * implemented and Arm prescribes so. So assume it will be there and do 137c73686a1SBoyan Karatotev * away with a flag for it. This is used to check minor PMUv3px 138c73686a1SBoyan Karatotev * revisions so that we catch them as they come along 139c73686a1SBoyan Karatotev */ 140c73686a1SBoyan Karatotev check_feature(FEAT_STATE_ALWAYS, read_feat_pmuv3_id_field(), 141c73686a1SBoyan Karatotev "PMUv3", 1, ID_AA64DFR0_PMUVER_PMUV3P7); 1426a0da736SJayanth Dodderi Chidanand 1436a0da736SJayanth Dodderi Chidanand /* v8.1 features */ 144*aaaf2cc3SSona Mathew check_feature(ENABLE_FEAT_PAN, read_feat_pan_id_field(), "PAN", 1, 3); 145ea735bf5SAndre Przywara check_feature(ENABLE_FEAT_VHE, read_feat_vhe_id_field(), "VHE", 1, 1); 1466a0da736SJayanth Dodderi Chidanand 1476a0da736SJayanth Dodderi Chidanand /* v8.2 features */ 1482b0bc4e0SJayanth Dodderi Chidanand check_feature(ENABLE_SVE_FOR_NS, read_feat_sve_id_field(), 149*aaaf2cc3SSona Mathew "SVE", 1, 1); 1506503ff29SAndre Przywara check_feature(ENABLE_FEAT_RAS, read_feat_ras_id_field(), "RAS", 1, 2); 1516a0da736SJayanth Dodderi Chidanand 1526a0da736SJayanth Dodderi Chidanand /* v8.3 features */ 1536a0da736SJayanth Dodderi Chidanand read_feat_pauth(); 1546a0da736SJayanth Dodderi Chidanand 1556a0da736SJayanth Dodderi Chidanand /* v8.4 features */ 156*aaaf2cc3SSona Mathew check_feature(ENABLE_FEAT_DIT, read_feat_dit_id_field(), "DIT", 1, 1); 157d23acc9eSAndre Przywara check_feature(ENABLE_FEAT_AMU, read_feat_amu_id_field(), 158a4cccb4fSAndre Przywara "AMUv1", 1, 2); 159edebefbcSArvind Ram Prakash check_feature(ENABLE_FEAT_MPAM, read_feat_mpam_version(), 1601f8be7fcSAndre Przywara "MPAM", 1, 17); 161d5384b69SAndre Przywara check_feature(CTX_INCLUDE_NEVE_REGS, read_feat_nv_id_field(), 162*aaaf2cc3SSona Mathew "NV2", 2, 2); 163623f6140SAndre Przywara check_feature(ENABLE_FEAT_SEL2, read_feat_sel2_id_field(), 164623f6140SAndre Przywara "SEL2", 1, 1); 165fc8d2d39SAndre Przywara check_feature(ENABLE_TRF_FOR_NS, read_feat_trf_id_field(), 166fc8d2d39SAndre Przywara "TRF", 1, 1); 1676a0da736SJayanth Dodderi Chidanand 1686a0da736SJayanth Dodderi Chidanand /* v8.5 features */ 169c282384dSGovindraj Raja check_feature(ENABLE_FEAT_MTE2, get_armv8_5_mte_support(), "MTE2", 1708e397889SGovindraj Raja MTE_IMPLEMENTED_ELX, MTE_IMPLEMENTED_ASY); 171ac17e52cSAndre Przywara check_feature(ENABLE_FEAT_RNG, read_feat_rng_id_field(), "RNG", 1, 1); 1726a0da736SJayanth Dodderi Chidanand read_feat_bti(); 173ff86e0b4SJuan Pablo Conde read_feat_rng_trap(); 1746a0da736SJayanth Dodderi Chidanand 1756a0da736SJayanth Dodderi Chidanand /* v8.6 features */ 176b57e16a4SAndre Przywara check_feature(ENABLE_FEAT_AMUv1p1, read_feat_amu_id_field(), 177b57e16a4SAndre Przywara "AMUv1p1", 2, 2); 178a4cccb4fSAndre Przywara check_feature(ENABLE_FEAT_FGT, read_feat_fgt_id_field(), "FGT", 1, 1); 179*aaaf2cc3SSona Mathew check_feature(ENABLE_FEAT_ECV, read_feat_ecv_id_field(), "ECV", 1, 2); 1801223d2a0SAndre Przywara check_feature(ENABLE_FEAT_TWED, read_feat_twed_id_field(), 181*aaaf2cc3SSona Mathew "TWED", 1, 1); 1826a0da736SJayanth Dodderi Chidanand 18383a4dae1SBoyan Karatotev /* 18483a4dae1SBoyan Karatotev * even though this is a "DISABLE" it does confusingly perform feature 18583a4dae1SBoyan Karatotev * enablement duties like all other flags here. Check it against the HW 18683a4dae1SBoyan Karatotev * feature when we intend to diverge from the default behaviour 18783a4dae1SBoyan Karatotev */ 188*aaaf2cc3SSona Mathew check_feature(DISABLE_MTPMU, read_feat_mtpmu_id_field(), "MTPMU", 1, 1); 18983a4dae1SBoyan Karatotev 1906a0da736SJayanth Dodderi Chidanand /* v8.7 features */ 191*aaaf2cc3SSona Mathew check_feature(ENABLE_FEAT_HCX, read_feat_hcx_id_field(), "HCX", 1, 1); 1926a0da736SJayanth Dodderi Chidanand 193d3331603SMark Brown /* v8.9 features */ 194a8d5d3d5SAndre Przywara check_feature(ENABLE_FEAT_TCR2, read_feat_tcr2_id_field(), 195d3331603SMark Brown "TCR2", 1, 1); 196062b6c6bSMark Brown check_feature(ENABLE_FEAT_S2PIE, read_feat_s2pie_id_field(), 197062b6c6bSMark Brown "S2PIE", 1, 1); 198062b6c6bSMark Brown check_feature(ENABLE_FEAT_S1PIE, read_feat_s1pie_id_field(), 199062b6c6bSMark Brown "S1PIE", 1, 1); 200062b6c6bSMark Brown check_feature(ENABLE_FEAT_S2POE, read_feat_s2poe_id_field(), 201062b6c6bSMark Brown "S2POE", 1, 1); 202062b6c6bSMark Brown check_feature(ENABLE_FEAT_S1POE, read_feat_s1poe_id_field(), 203062b6c6bSMark Brown "S1POE", 1, 1); 20430019d86SSona Mathew check_feature(ENABLE_FEAT_CSV2_3, read_feat_csv2_id_field(), 20530019d86SSona Mathew "CSV2_3", 3, 3); 206d3331603SMark Brown 2071298f2f1SJayanth Dodderi Chidanand /* v9.0 features */ 208ff491036SAndre Przywara check_feature(ENABLE_BRBE_FOR_NS, read_feat_brbe_id_field(), 209*aaaf2cc3SSona Mathew "BRBE", 1, 2); 210f5360cfaSAndre Przywara check_feature(ENABLE_TRBE_FOR_NS, read_feat_trbe_id_field(), 211f5360cfaSAndre Przywara "TRBE", 1, 1); 2121298f2f1SJayanth Dodderi Chidanand 2136a0da736SJayanth Dodderi Chidanand /* v9.2 features */ 21445007acdSJayanth Dodderi Chidanand check_feature(ENABLE_SME_FOR_NS, read_feat_sme_id_field(), 215*aaaf2cc3SSona Mathew "SME", 1, 2); 21603d3c0d7SJayanth Dodderi Chidanand check_feature(ENABLE_SME2_FOR_NS, read_feat_sme_id_field(), 217*aaaf2cc3SSona Mathew "SME2", 2, 2); 218688ab57bSMark Brown 219688ab57bSMark Brown /* v9.4 features */ 220688ab57bSMark Brown check_feature(ENABLE_FEAT_GCS, read_feat_gcs_id_field(), "GCS", 1, 1); 221688ab57bSMark Brown 2226a0da736SJayanth Dodderi Chidanand read_feat_rme(); 223b45dd74eSAndre Przywara 224b45dd74eSAndre Przywara if (tainted) { 225b45dd74eSAndre Przywara panic(); 226b45dd74eSAndre Przywara } 2276a0da736SJayanth Dodderi Chidanand } 228