plat_startup.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (c) 2014-2020, Arm Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2023, Advanced Micro Devices, Inc. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <inttypes.h>
  9. #include <stdint.h>
  10. #include <arch_helpers.h>
  11. #include <common/debug.h>
  12. #include <plat_startup.h>
  13. /*
  14. * HandoffParams
  15. * Parameter bitfield encoding
  16. * -----------------------------------------------------------------------------
  17. * Exec State 0 0 -> Aarch64, 1-> Aarch32
  18. * endianness 1 0 -> LE, 1 -> BE
  19. * secure (TZ) 2 0 -> Non secure, 1 -> secure
  20. * EL 3:4 00 -> EL0, 01 -> EL1, 10 -> EL2, 11 -> EL3
  21. * CPU# 5:6 00 -> A53_0, 01 -> A53_1, 10 -> A53_2, 11 -> A53_3
  22. * Reserved 7:10 Reserved
  23. * Cluster# 11:12 00 -> Cluster 0, 01 -> Cluster 1, 10 -> Cluster 2,
  24. * 11 -> Cluster (Applicable for Versal NET only).
  25. * Reserved 13:16 Reserved
  26. */
  27. #define XBL_FLAGS_ESTATE_SHIFT 0U
  28. #define XBL_FLAGS_ESTATE_MASK (1U << XBL_FLAGS_ESTATE_SHIFT)
  29. #define XBL_FLAGS_ESTATE_A64 0U
  30. #define XBL_FLAGS_ESTATE_A32 1U
  31. #define XBL_FLAGS_ENDIAN_SHIFT 1U
  32. #define XBL_FLAGS_ENDIAN_MASK (1U << XBL_FLAGS_ENDIAN_SHIFT)
  33. #define XBL_FLAGS_ENDIAN_LE 0U
  34. #define XBL_FLAGS_ENDIAN_BE 1U
  35. #define XBL_FLAGS_TZ_SHIFT 2U
  36. #define XBL_FLAGS_TZ_MASK (1U << XBL_FLAGS_TZ_SHIFT)
  37. #define XBL_FLAGS_NON_SECURE 0U
  38. #define XBL_FLAGS_SECURE 1U
  39. #define XBL_FLAGS_EL_SHIFT 3U
  40. #define XBL_FLAGS_EL_MASK (3U << XBL_FLAGS_EL_SHIFT)
  41. #define XBL_FLAGS_EL0 0U
  42. #define XBL_FLAGS_EL1 1U
  43. #define XBL_FLAGS_EL2 2U
  44. #define XBL_FLAGS_EL3 3U
  45. #define XBL_FLAGS_CPU_SHIFT 5U
  46. #define XBL_FLAGS_CPU_MASK (3U << XBL_FLAGS_CPU_SHIFT)
  47. #define XBL_FLAGS_A53_0 0U
  48. #define XBL_FLAGS_A53_1 1U
  49. #define XBL_FLAGS_A53_2 2U
  50. #define XBL_FLAGS_A53_3 3U
  51. #if defined(PLAT_versal_net)
  52. #define XBL_FLAGS_CLUSTER_SHIFT 11U
  53. #define XBL_FLAGS_CLUSTER_MASK GENMASK(11, 12)
  54. #define XBL_FLAGS_CLUSTER_0 0U
  55. #endif /* PLAT_versal_net */
  56. /**
  57. * get_xbl_cpu() - Get the target CPU for partition.
  58. * @partition: Pointer to partition struct.
  59. *
  60. * Return: XBL_FLAGS_A53_0, XBL_FLAGS_A53_1, XBL_FLAGS_A53_2 or XBL_FLAGS_A53_3.
  61. *
  62. */
  63. static int32_t get_xbl_cpu(const struct xbl_partition *partition)
  64. {
  65. uint64_t flags = partition->flags & XBL_FLAGS_CPU_MASK;
  66. return flags >> XBL_FLAGS_CPU_SHIFT;
  67. }
  68. /**
  69. * get_xbl_el() - Get the target exception level for partition.
  70. * @partition: Pointer to partition struct.
  71. *
  72. * Return: XBL_FLAGS_EL0, XBL_FLAGS_EL1, XBL_FLAGS_EL2 or XBL_FLAGS_EL3.
  73. *
  74. */
  75. static int32_t get_xbl_el(const struct xbl_partition *partition)
  76. {
  77. uint64_t flags = partition->flags & XBL_FLAGS_EL_MASK;
  78. return flags >> XBL_FLAGS_EL_SHIFT;
  79. }
  80. /**
  81. * get_xbl_ss() - Get the target security state for partition.
  82. * @partition: Pointer to partition struct.
  83. *
  84. * Return: XBL_FLAGS_NON_SECURE or XBL_FLAGS_SECURE.
  85. *
  86. */
  87. static int32_t get_xbl_ss(const struct xbl_partition *partition)
  88. {
  89. uint64_t flags = partition->flags & XBL_FLAGS_TZ_MASK;
  90. return flags >> XBL_FLAGS_TZ_SHIFT;
  91. }
  92. /**
  93. * get_xbl_endian() - Get the target endianness for partition.
  94. * @partition: Pointer to partition struct.
  95. *
  96. * Return: SPSR_E_LITTLE or SPSR_E_BIG.
  97. *
  98. */
  99. static int32_t get_xbl_endian(const struct xbl_partition *partition)
  100. {
  101. uint64_t flags = partition->flags & XBL_FLAGS_ENDIAN_MASK;
  102. flags >>= XBL_FLAGS_ENDIAN_SHIFT;
  103. if (flags == XBL_FLAGS_ENDIAN_BE) {
  104. return SPSR_E_BIG;
  105. } else {
  106. return SPSR_E_LITTLE;
  107. }
  108. }
  109. /**
  110. * get_xbl_estate() - Get the target execution state for partition.
  111. * @partition: Pointer to partition struct.
  112. *
  113. * Return: XBL_FLAGS_ESTATE_A32 or XBL_FLAGS_ESTATE_A64.
  114. *
  115. */
  116. static int32_t get_xbl_estate(const struct xbl_partition *partition)
  117. {
  118. uint64_t flags = partition->flags & XBL_FLAGS_ESTATE_MASK;
  119. return flags >> XBL_FLAGS_ESTATE_SHIFT;
  120. }
  121. #if defined(PLAT_versal_net)
  122. /**
  123. * get_xbl_cluster - Get the cluster number
  124. * @partition: pointer to the partition structure.
  125. *
  126. * Return: cluster number for the partition.
  127. */
  128. static int32_t get_xbl_cluster(const struct xbl_partition *partition)
  129. {
  130. uint64_t flags = partition->flags & XBL_FLAGS_CLUSTER_MASK;
  131. return (int32_t)(flags >> XBL_FLAGS_CLUSTER_SHIFT);
  132. }
  133. #endif /* PLAT_versal_net */
  134. /**
  135. * xbl_handover() - Populates the bl32 and bl33 image info structures.
  136. * @bl32: BL32 image info structure.
  137. * @bl33: BL33 image info structure.
  138. * @handoff_addr: TF-A handoff address.
  139. *
  140. * Process the handoff parameters from the XBL and populate the BL32 and BL33
  141. * image info structures accordingly.
  142. *
  143. * Return: Return the status of the handoff. The value will be from the
  144. * xbl_handoff enum.
  145. *
  146. */
  147. enum xbl_handoff xbl_handover(entry_point_info_t *bl32,
  148. entry_point_info_t *bl33,
  149. uint64_t handoff_addr)
  150. {
  151. const struct xbl_handoff_params *HandoffParams;
  152. if (!handoff_addr) {
  153. WARN("BL31: No handoff structure passed\n");
  154. return XBL_HANDOFF_NO_STRUCT;
  155. }
  156. HandoffParams = (struct xbl_handoff_params *)handoff_addr;
  157. if ((HandoffParams->magic[0] != 'X') ||
  158. (HandoffParams->magic[1] != 'L') ||
  159. (HandoffParams->magic[2] != 'N') ||
  160. (HandoffParams->magic[3] != 'X')) {
  161. ERROR("BL31: invalid handoff structure at %" PRIx64 "\n", handoff_addr);
  162. return XBL_HANDOFF_INVAL_STRUCT;
  163. }
  164. VERBOSE("BL31: TF-A handoff params at:0x%" PRIx64 ", entries:%u\n",
  165. handoff_addr, HandoffParams->num_entries);
  166. if (HandoffParams->num_entries > XBL_MAX_PARTITIONS) {
  167. ERROR("BL31: TF-A handoff params: too many partitions (%u/%u)\n",
  168. HandoffParams->num_entries, XBL_MAX_PARTITIONS);
  169. return XBL_HANDOFF_TOO_MANY_PARTS;
  170. }
  171. /*
  172. * we loop over all passed entries but only populate two image structs
  173. * (bl32, bl33). I.e. the last applicable images in the handoff
  174. * structure will be used for the hand off
  175. */
  176. for (size_t i = 0; i < HandoffParams->num_entries; i++) {
  177. entry_point_info_t *image;
  178. int32_t target_estate, target_secure, target_cpu;
  179. uint32_t target_endianness, target_el;
  180. VERBOSE("BL31: %zd: entry:0x%" PRIx64 ", flags:0x%" PRIx64 "\n", i,
  181. HandoffParams->partition[i].entry_point,
  182. HandoffParams->partition[i].flags);
  183. #if defined(PLAT_versal_net)
  184. uint32_t target_cluster;
  185. target_cluster = get_xbl_cluster(&HandoffParams->partition[i]);
  186. if (target_cluster != XBL_FLAGS_CLUSTER_0) {
  187. WARN("BL31: invalid target Cluster (%i)\n",
  188. target_cluster);
  189. continue;
  190. }
  191. #endif /* PLAT_versal_net */
  192. target_cpu = get_xbl_cpu(&HandoffParams->partition[i]);
  193. if (target_cpu != XBL_FLAGS_A53_0) {
  194. WARN("BL31: invalid target CPU (%i)\n", target_cpu);
  195. continue;
  196. }
  197. target_el = get_xbl_el(&HandoffParams->partition[i]);
  198. if ((target_el == XBL_FLAGS_EL3) ||
  199. (target_el == XBL_FLAGS_EL0)) {
  200. WARN("BL31: invalid target exception level(%i)\n",
  201. target_el);
  202. continue;
  203. }
  204. target_secure = get_xbl_ss(&HandoffParams->partition[i]);
  205. if ((target_secure == XBL_FLAGS_SECURE) &&
  206. (target_el == XBL_FLAGS_EL2)) {
  207. WARN("BL31: invalid security state (%i) for exception level (%i)\n",
  208. target_secure, target_el);
  209. continue;
  210. }
  211. target_estate = get_xbl_estate(&HandoffParams->partition[i]);
  212. target_endianness = get_xbl_endian(&HandoffParams->partition[i]);
  213. if (target_secure == XBL_FLAGS_SECURE) {
  214. image = bl32;
  215. if (target_estate == XBL_FLAGS_ESTATE_A32) {
  216. bl32->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
  217. target_endianness,
  218. DISABLE_ALL_EXCEPTIONS);
  219. } else {
  220. bl32->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
  221. DISABLE_ALL_EXCEPTIONS);
  222. }
  223. } else {
  224. image = bl33;
  225. if (target_estate == XBL_FLAGS_ESTATE_A32) {
  226. if (target_el == XBL_FLAGS_EL2) {
  227. target_el = MODE32_hyp;
  228. } else {
  229. target_el = MODE32_sys;
  230. }
  231. bl33->spsr = SPSR_MODE32(target_el, SPSR_T_ARM,
  232. target_endianness,
  233. DISABLE_ALL_EXCEPTIONS);
  234. } else {
  235. if (target_el == XBL_FLAGS_EL2) {
  236. target_el = MODE_EL2;
  237. } else {
  238. target_el = MODE_EL1;
  239. }
  240. bl33->spsr = SPSR_64(target_el, MODE_SP_ELX,
  241. DISABLE_ALL_EXCEPTIONS);
  242. }
  243. }
  244. VERBOSE("Setting up %s entry point to:%" PRIx64 ", el:%x\n",
  245. (target_secure == XBL_FLAGS_SECURE) ? "BL32" : "BL33",
  246. HandoffParams->partition[i].entry_point,
  247. target_el);
  248. image->pc = HandoffParams->partition[i].entry_point;
  249. if (target_endianness == SPSR_E_BIG) {
  250. EP_SET_EE(image->h.attr, EP_EE_BIG);
  251. } else {
  252. EP_SET_EE(image->h.attr, EP_EE_LITTLE);
  253. }
  254. }
  255. return XBL_HANDOFF_SUCCESS;
  256. }