feat_detect.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2022-2023, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch_features.h>
  7. #include <common/debug.h>
  8. #include <common/feat_detect.h>
  9. static bool tainted;
  10. /*******************************************************************************
  11. * This section lists the wrapper modules for each feature to evaluate the
  12. * feature states (FEAT_STATE_ALWAYS and FEAT_STATE_CHECK) and perform
  13. * necessary action as below:
  14. *
  15. * It verifies whether the FEAT_XXX (eg: FEAT_SB) is supported by the PE or not.
  16. * Without this check an exception would occur during context save/restore
  17. * routines, if the feature is enabled but not supported by PE.
  18. ******************************************************************************/
  19. #define feat_detect_panic(a, b) ((a) ? (void)0 : feature_panic(b))
  20. /*******************************************************************************
  21. * Function : feature_panic
  22. * Customised panic function with error logging mechanism to list the feature
  23. * not supported by the PE.
  24. ******************************************************************************/
  25. static inline void feature_panic(char *feat_name)
  26. {
  27. ERROR("FEAT_%s not supported by the PE\n", feat_name);
  28. panic();
  29. }
  30. /*******************************************************************************
  31. * Function : check_feature
  32. * Check for a valid combination of build time flags (ENABLE_FEAT_xxx) and
  33. * feature availability on the hardware. <min> is the smallest feature
  34. * ID field value that is required for that feature.
  35. * Triggers a panic later if a feature is forcefully enabled, but not
  36. * available on the PE. Also will panic if the hardware feature ID field
  37. * is larger than the maximum known and supported number, specified by <max>.
  38. *
  39. * We force inlining here to let the compiler optimise away the whole check
  40. * if the feature is disabled at build time (FEAT_STATE_DISABLED).
  41. ******************************************************************************/
  42. static inline void __attribute((__always_inline__))
  43. check_feature(int state, unsigned long field, const char *feat_name,
  44. unsigned int min, unsigned int max)
  45. {
  46. if (state == FEAT_STATE_ALWAYS && field < min) {
  47. ERROR("FEAT_%s not supported by the PE\n", feat_name);
  48. tainted = true;
  49. }
  50. if (state >= FEAT_STATE_ALWAYS && field > max) {
  51. ERROR("FEAT_%s is version %ld, but is only known up to version %d\n",
  52. feat_name, field, max);
  53. tainted = true;
  54. }
  55. }
  56. /************************************************
  57. * Feature : FEAT_PAUTH (Pointer Authentication)
  58. ***********************************************/
  59. static void read_feat_pauth(void)
  60. {
  61. #if (ENABLE_PAUTH == FEAT_STATE_ALWAYS) || (CTX_INCLUDE_PAUTH_REGS == FEAT_STATE_ALWAYS)
  62. feat_detect_panic(is_armv8_3_pauth_present(), "PAUTH");
  63. #endif
  64. }
  65. /************************************************
  66. * Feature : FEAT_MTE (Memory Tagging Extension)
  67. ***********************************************/
  68. static void read_feat_mte(void)
  69. {
  70. #if (CTX_INCLUDE_MTE_REGS == FEAT_STATE_ALWAYS)
  71. unsigned int mte = get_armv8_5_mte_support();
  72. feat_detect_panic((mte != MTE_UNIMPLEMENTED), "MTE");
  73. #endif
  74. }
  75. /****************************************************
  76. * Feature : FEAT_BTI (Branch Target Identification)
  77. ***************************************************/
  78. static void read_feat_bti(void)
  79. {
  80. #if (ENABLE_BTI == FEAT_STATE_ALWAYS)
  81. feat_detect_panic(is_armv8_5_bti_present(), "BTI");
  82. #endif
  83. }
  84. /**************************************************
  85. * Feature : FEAT_RME (Realm Management Extension)
  86. *************************************************/
  87. static void read_feat_rme(void)
  88. {
  89. #if (ENABLE_RME == FEAT_STATE_ALWAYS)
  90. feat_detect_panic((get_armv9_2_feat_rme_support() !=
  91. ID_AA64PFR0_FEAT_RME_NOT_SUPPORTED), "RME");
  92. #endif
  93. }
  94. /******************************************************************
  95. * Feature : FEAT_RNG_TRAP (Trapping support for RNDR/RNDRRS)
  96. *****************************************************************/
  97. static void read_feat_rng_trap(void)
  98. {
  99. #if (ENABLE_FEAT_RNG_TRAP == FEAT_STATE_ALWAYS)
  100. feat_detect_panic(is_feat_rng_trap_present(), "RNG_TRAP");
  101. #endif
  102. }
  103. /***********************************************************************************
  104. * TF-A supports many Arm architectural features starting from arch version
  105. * (8.0 till 8.7+). These features are mostly enabled through build flags. This
  106. * mechanism helps in validating these build flags in the early boot phase
  107. * either in BL1 or BL31 depending on the platform and assists in identifying
  108. * and notifying the features which are enabled but not supported by the PE.
  109. *
  110. * It reads all the enabled features ID-registers and ensures the features
  111. * are supported by the PE.
  112. * In case if they aren't it stops booting at an early phase and logs the error
  113. * messages, notifying the platforms about the features that are not supported.
  114. *
  115. * Further the procedure is implemented with a tri-state approach for each feature:
  116. * ENABLE_FEAT_xxx = 0 : The feature is disabled statically at compile time
  117. * ENABLE_FEAT_xxx = 1 : The feature is enabled and must be present in hardware.
  118. * There will be panic if feature is not present at cold boot.
  119. * ENABLE_FEAT_xxx = 2 : The feature is enabled but dynamically enabled at runtime
  120. * depending on hardware capability.
  121. *
  122. * For better readability, state values are defined with macros, namely:
  123. * { FEAT_STATE_DISABLED, FEAT_STATE_ALWAYS, FEAT_STATE_CHECK }, taking values
  124. * { 0, 1, 2 }, respectively, as their naming.
  125. **********************************************************************************/
  126. void detect_arch_features(void)
  127. {
  128. tainted = false;
  129. /* v8.0 features */
  130. check_feature(ENABLE_FEAT_SB, read_feat_sb_id_field(), "SB", 1, 1);
  131. check_feature(ENABLE_FEAT_CSV2_2, read_feat_csv2_id_field(),
  132. "CSV2_2", 2, 3);
  133. /*
  134. * Even though the PMUv3 is an OPTIONAL feature, it is always
  135. * implemented and Arm prescribes so. So assume it will be there and do
  136. * away with a flag for it. This is used to check minor PMUv3px
  137. * revisions so that we catch them as they come along
  138. */
  139. check_feature(FEAT_STATE_ALWAYS, read_feat_pmuv3_id_field(),
  140. "PMUv3", 1, ID_AA64DFR0_PMUVER_PMUV3P7);
  141. /* v8.1 features */
  142. check_feature(ENABLE_FEAT_PAN, read_feat_pan_id_field(), "PAN", 1, 3);
  143. check_feature(ENABLE_FEAT_VHE, read_feat_vhe_id_field(), "VHE", 1, 1);
  144. /* v8.2 features */
  145. check_feature(ENABLE_SVE_FOR_NS, read_feat_sve_id_field(),
  146. "SVE", 1, 1);
  147. check_feature(ENABLE_FEAT_RAS, read_feat_ras_id_field(), "RAS", 1, 2);
  148. /* v8.3 features */
  149. read_feat_pauth();
  150. /* v8.4 features */
  151. check_feature(ENABLE_FEAT_DIT, read_feat_dit_id_field(), "DIT", 1, 1);
  152. check_feature(ENABLE_FEAT_AMU, read_feat_amu_id_field(),
  153. "AMUv1", 1, 2);
  154. check_feature(ENABLE_MPAM_FOR_LOWER_ELS, read_feat_mpam_version(),
  155. "MPAM", 1, 17);
  156. check_feature(CTX_INCLUDE_NEVE_REGS, read_feat_nv_id_field(),
  157. "NV2", 2, 2);
  158. check_feature(ENABLE_FEAT_SEL2, read_feat_sel2_id_field(),
  159. "SEL2", 1, 1);
  160. check_feature(ENABLE_TRF_FOR_NS, read_feat_trf_id_field(),
  161. "TRF", 1, 1);
  162. /* v8.5 features */
  163. read_feat_mte();
  164. check_feature(ENABLE_FEAT_RNG, read_feat_rng_id_field(), "RNG", 1, 1);
  165. read_feat_bti();
  166. read_feat_rng_trap();
  167. /* v8.6 features */
  168. check_feature(ENABLE_FEAT_AMUv1p1, read_feat_amu_id_field(),
  169. "AMUv1p1", 2, 2);
  170. check_feature(ENABLE_FEAT_FGT, read_feat_fgt_id_field(), "FGT", 1, 1);
  171. check_feature(ENABLE_FEAT_ECV, read_feat_ecv_id_field(), "ECV", 1, 2);
  172. check_feature(ENABLE_FEAT_TWED, read_feat_twed_id_field(),
  173. "TWED", 1, 1);
  174. /*
  175. * even though this is a "DISABLE" it does confusingly perform feature
  176. * enablement duties like all other flags here. Check it against the HW
  177. * feature when we intend to diverge from the default behaviour
  178. */
  179. check_feature(DISABLE_MTPMU, read_feat_mtpmu_id_field(), "MTPMU", 1, 1);
  180. /* v8.7 features */
  181. check_feature(ENABLE_FEAT_HCX, read_feat_hcx_id_field(), "HCX", 1, 1);
  182. /* v8.9 features */
  183. check_feature(ENABLE_FEAT_TCR2, read_feat_tcr2_id_field(),
  184. "TCR2", 1, 1);
  185. check_feature(ENABLE_FEAT_S2PIE, read_feat_s2pie_id_field(),
  186. "S2PIE", 1, 1);
  187. check_feature(ENABLE_FEAT_S1PIE, read_feat_s1pie_id_field(),
  188. "S1PIE", 1, 1);
  189. check_feature(ENABLE_FEAT_S2POE, read_feat_s2poe_id_field(),
  190. "S2POE", 1, 1);
  191. check_feature(ENABLE_FEAT_S1POE, read_feat_s1poe_id_field(),
  192. "S1POE", 1, 1);
  193. check_feature(ENABLE_FEAT_MTE_PERM, read_feat_mte_perm_id_field(),
  194. "MTE_PERM", 1, 1);
  195. /* v9.0 features */
  196. check_feature(ENABLE_BRBE_FOR_NS, read_feat_brbe_id_field(),
  197. "BRBE", 1, 2);
  198. check_feature(ENABLE_TRBE_FOR_NS, read_feat_trbe_id_field(),
  199. "TRBE", 1, 1);
  200. /* v9.2 features */
  201. check_feature(ENABLE_SME_FOR_NS, read_feat_sme_id_field(),
  202. "SME", 1, 2);
  203. check_feature(ENABLE_SME2_FOR_NS, read_feat_sme_id_field(),
  204. "SME2", 2, 2);
  205. /* v9.4 features */
  206. check_feature(ENABLE_FEAT_GCS, read_feat_gcs_id_field(), "GCS", 1, 1);
  207. read_feat_rme();
  208. if (tainted) {
  209. panic();
  210. }
  211. }