tegra_bl31_setup.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright (c) 2015-2024, ARM Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2020-2023, NVIDIA Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <errno.h>
  9. #include <inttypes.h>
  10. #include <stddef.h>
  11. #include <string.h>
  12. #include <platform_def.h>
  13. #include <arch.h>
  14. #include <arch_helpers.h>
  15. #include <bl31/bl31.h>
  16. #include <common/bl_common.h>
  17. #include <common/debug.h>
  18. #include <cortex_a57.h>
  19. #include <denver.h>
  20. #include <drivers/console.h>
  21. #include <lib/mmio.h>
  22. #include <lib/utils.h>
  23. #include <lib/utils_def.h>
  24. #include <plat/common/platform.h>
  25. #include <memctrl.h>
  26. #include <profiler.h>
  27. #include <smmu.h>
  28. #include <tegra_def.h>
  29. #include <tegra_platform.h>
  30. #include <tegra_private.h>
  31. /* length of Trusty's input parameters (in bytes) */
  32. #define TRUSTY_PARAMS_LEN_BYTES (4096*2)
  33. /*******************************************************************************
  34. * Declarations of linker defined symbols which will help us find the layout
  35. * of trusted SRAM
  36. ******************************************************************************/
  37. IMPORT_SYM(uint64_t, __RW_START__, BL31_RW_START);
  38. extern uint64_t tegra_bl31_phys_base;
  39. static entry_point_info_t bl33_image_ep_info, bl32_image_ep_info;
  40. static plat_params_from_bl2_t plat_bl31_params_from_bl2 = {
  41. .tzdram_size = TZDRAM_SIZE
  42. };
  43. #ifdef SPD_trusty
  44. static aapcs64_params_t bl32_args;
  45. #endif
  46. /*******************************************************************************
  47. * This variable holds the non-secure image entry address
  48. ******************************************************************************/
  49. extern uint64_t ns_image_entrypoint;
  50. /*******************************************************************************
  51. * Return a pointer to the 'entry_point_info' structure of the next image for
  52. * security state specified. BL33 corresponds to the non-secure image type
  53. * while BL32 corresponds to the secure image type.
  54. ******************************************************************************/
  55. entry_point_info_t *bl31_plat_get_next_image_ep_info(uint32_t type)
  56. {
  57. entry_point_info_t *ep = NULL;
  58. /* return BL32 entry point info if it is valid */
  59. if (type == NON_SECURE) {
  60. ep = &bl33_image_ep_info;
  61. } else if ((type == SECURE) && (bl32_image_ep_info.pc != 0U)) {
  62. ep = &bl32_image_ep_info;
  63. }
  64. return ep;
  65. }
  66. /*******************************************************************************
  67. * Return a pointer to the 'plat_params_from_bl2_t' structure. The BL2 image
  68. * passes this platform specific information.
  69. ******************************************************************************/
  70. plat_params_from_bl2_t *bl31_get_plat_params(void)
  71. {
  72. return &plat_bl31_params_from_bl2;
  73. }
  74. /*******************************************************************************
  75. * Perform any BL31 specific platform actions. Populate the BL33 and BL32 image
  76. * info.
  77. ******************************************************************************/
  78. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  79. u_register_t arg2, u_register_t arg3)
  80. {
  81. struct tegra_bl31_params *arg_from_bl2 = plat_get_bl31_params();
  82. plat_params_from_bl2_t *plat_params = plat_get_bl31_plat_params();
  83. int32_t ret;
  84. /*
  85. * Tegra platforms will receive boot parameters through custom
  86. * mechanisms. So, we ignore the input parameters.
  87. */
  88. (void)arg0;
  89. (void)arg1;
  90. /*
  91. * Copy BL3-3, BL3-2 entry point information.
  92. * They are stored in Secure RAM, in BL2's address space.
  93. */
  94. assert(arg_from_bl2 != NULL);
  95. assert(arg_from_bl2->bl33_ep_info != NULL);
  96. bl33_image_ep_info = *arg_from_bl2->bl33_ep_info;
  97. if (arg_from_bl2->bl32_ep_info != NULL) {
  98. bl32_image_ep_info = *arg_from_bl2->bl32_ep_info;
  99. #ifdef SPD_trusty
  100. /* save BL32 boot parameters */
  101. memcpy(&bl32_args, &arg_from_bl2->bl32_ep_info->args, sizeof(bl32_args));
  102. #endif
  103. }
  104. /*
  105. * Parse platform specific parameters
  106. */
  107. assert(plat_params != NULL);
  108. plat_bl31_params_from_bl2.tzdram_base = plat_params->tzdram_base;
  109. plat_bl31_params_from_bl2.tzdram_size = plat_params->tzdram_size;
  110. plat_bl31_params_from_bl2.uart_id = plat_params->uart_id;
  111. plat_bl31_params_from_bl2.l2_ecc_parity_prot_dis = plat_params->l2_ecc_parity_prot_dis;
  112. plat_bl31_params_from_bl2.sc7entry_fw_size = plat_params->sc7entry_fw_size;
  113. plat_bl31_params_from_bl2.sc7entry_fw_base = plat_params->sc7entry_fw_base;
  114. /*
  115. * It is very important that we run either from TZDRAM or TZSRAM base.
  116. * Add an explicit check here.
  117. */
  118. if ((plat_bl31_params_from_bl2.tzdram_base != (uint64_t)BL31_BASE) &&
  119. (TEGRA_TZRAM_BASE != BL31_BASE)) {
  120. panic();
  121. }
  122. /*
  123. * Enable console for the platform
  124. */
  125. plat_enable_console(plat_params->uart_id);
  126. /*
  127. * The previous bootloader passes the base address of the shared memory
  128. * location to store the boot profiler logs. Sanity check the
  129. * address and initialise the profiler library, if it looks ok.
  130. */
  131. ret = bl31_check_ns_address(plat_params->boot_profiler_shmem_base,
  132. PROFILER_SIZE_BYTES);
  133. if (ret == (int32_t)0) {
  134. /* store the membase for the profiler lib */
  135. plat_bl31_params_from_bl2.boot_profiler_shmem_base =
  136. plat_params->boot_profiler_shmem_base;
  137. /* initialise the profiler library */
  138. boot_profiler_init(plat_params->boot_profiler_shmem_base,
  139. TEGRA_TMRUS_BASE);
  140. }
  141. /*
  142. * Add timestamp for platform early setup entry.
  143. */
  144. boot_profiler_add_record("[TF] early setup entry");
  145. /*
  146. * Initialize delay timer
  147. */
  148. tegra_delay_timer_init();
  149. /* Early platform setup for Tegra SoCs */
  150. plat_early_platform_setup();
  151. /*
  152. * Add timestamp for platform early setup exit.
  153. */
  154. boot_profiler_add_record("[TF] early setup exit");
  155. INFO("BL3-1: Boot CPU: %s Processor [%lx]\n",
  156. (((read_midr() >> MIDR_IMPL_SHIFT) & MIDR_IMPL_MASK)
  157. == DENVER_IMPL) ? "Denver" : "ARM", read_mpidr());
  158. }
  159. #ifdef SPD_trusty
  160. void plat_trusty_set_boot_args(aapcs64_params_t *args)
  161. {
  162. /*
  163. * arg0 = TZDRAM aperture available for BL32
  164. * arg1 = BL32 boot params
  165. * arg2 = EKS Blob Length
  166. * arg3 = Boot Profiler Carveout Base
  167. */
  168. args->arg0 = bl32_args.arg0;
  169. args->arg1 = bl32_args.arg2;
  170. /* update EKS size */
  171. args->arg2 = bl32_args.arg4;
  172. /* Profiler Carveout Base */
  173. args->arg3 = bl32_args.arg5;
  174. }
  175. #endif
  176. /*******************************************************************************
  177. * Initialize the gic, configure the SCR.
  178. ******************************************************************************/
  179. void bl31_platform_setup(void)
  180. {
  181. /*
  182. * Add timestamp for platform setup entry.
  183. */
  184. boot_profiler_add_record("[TF] plat setup entry");
  185. /* Initialize the gic cpu and distributor interfaces */
  186. plat_gic_setup();
  187. /*
  188. * Setup secondary CPU POR infrastructure.
  189. */
  190. plat_secondary_setup();
  191. /*
  192. * Initial Memory Controller configuration.
  193. */
  194. tegra_memctrl_setup();
  195. /*
  196. * Late setup handler to allow platforms to performs additional
  197. * functionality.
  198. * This handler gets called with MMU enabled.
  199. */
  200. plat_late_platform_setup();
  201. /*
  202. * Add timestamp for platform setup exit.
  203. */
  204. boot_profiler_add_record("[TF] plat setup exit");
  205. INFO("BL3-1: Tegra platform setup complete\n");
  206. }
  207. /*******************************************************************************
  208. * Perform any BL3-1 platform runtime setup prior to BL3-1 cold boot exit
  209. ******************************************************************************/
  210. void bl31_plat_runtime_setup(void)
  211. {
  212. /*
  213. * Platform specific runtime setup
  214. */
  215. plat_runtime_setup();
  216. /*
  217. * Add final timestamp before exiting BL31.
  218. */
  219. boot_profiler_add_record("[TF] bl31 exit");
  220. boot_profiler_deinit();
  221. }
  222. /*******************************************************************************
  223. * Perform the very early platform specific architectural setup here. At the
  224. * moment this only initializes the mmu in a quick and dirty way.
  225. ******************************************************************************/
  226. void bl31_plat_arch_setup(void)
  227. {
  228. uint64_t rw_start = BL31_RW_START;
  229. uint64_t rw_size = BL_END - BL31_RW_START;
  230. uint64_t rodata_start = BL_RO_DATA_BASE;
  231. uint64_t rodata_size = BL_RO_DATA_END - BL_RO_DATA_BASE;
  232. uint64_t code_base = BL_CODE_BASE;
  233. uint64_t code_size = BL_CODE_END - BL_CODE_BASE;
  234. const mmap_region_t *plat_mmio_map = NULL;
  235. const plat_params_from_bl2_t *params_from_bl2 = bl31_get_plat_params();
  236. /*
  237. * Add timestamp for arch setup entry.
  238. */
  239. boot_profiler_add_record("[TF] arch setup entry");
  240. /* add MMIO space */
  241. plat_mmio_map = plat_get_mmio_map();
  242. if (plat_mmio_map != NULL) {
  243. mmap_add(plat_mmio_map);
  244. } else {
  245. WARN("MMIO map not available\n");
  246. }
  247. /* add memory regions */
  248. mmap_add_region(rw_start, rw_start,
  249. rw_size,
  250. MT_MEMORY | MT_RW | MT_SECURE);
  251. mmap_add_region(rodata_start, rodata_start,
  252. rodata_size,
  253. MT_RO_DATA | MT_SECURE);
  254. mmap_add_region(code_base, code_base,
  255. code_size,
  256. MT_CODE | MT_SECURE);
  257. /* map TZDRAM used by BL31 as coherent memory */
  258. if (TEGRA_TZRAM_BASE == tegra_bl31_phys_base) {
  259. mmap_add_region(params_from_bl2->tzdram_base,
  260. params_from_bl2->tzdram_base,
  261. BL31_SIZE,
  262. MT_DEVICE | MT_RW | MT_SECURE);
  263. }
  264. /* set up translation tables */
  265. init_xlat_tables();
  266. /* enable the MMU */
  267. enable_mmu_el3(0);
  268. /*
  269. * Add timestamp for arch setup exit.
  270. */
  271. boot_profiler_add_record("[TF] arch setup exit");
  272. INFO("BL3-1: Tegra: MMU enabled\n");
  273. }
  274. /*******************************************************************************
  275. * Check if the given NS DRAM range is valid
  276. ******************************************************************************/
  277. int32_t bl31_check_ns_address(uint64_t base, uint64_t size_in_bytes)
  278. {
  279. uint64_t end = base + size_in_bytes - U(1);
  280. /*
  281. * Sanity check the input values
  282. */
  283. if ((base == 0U) || (size_in_bytes == 0U)) {
  284. ERROR("NS address 0x%" PRIx64 " (%" PRId64 " bytes) is invalid\n",
  285. base, size_in_bytes);
  286. return -EINVAL;
  287. }
  288. /*
  289. * Check if the NS DRAM address is valid
  290. */
  291. if ((base < TEGRA_DRAM_BASE) || (base >= TEGRA_DRAM_END) ||
  292. (end > TEGRA_DRAM_END)) {
  293. ERROR("NS address 0x%" PRIx64 " is out-of-bounds!\n", base);
  294. return -EFAULT;
  295. }
  296. /*
  297. * TZDRAM aperture contains the BL31 and BL32 images, so we need
  298. * to check if the NS DRAM range overlaps the TZDRAM aperture.
  299. */
  300. if ((base < (uint64_t)TZDRAM_END) && (end > tegra_bl31_phys_base)) {
  301. ERROR("NS address 0x%" PRIx64 " overlaps TZDRAM!\n", base);
  302. return -ENOTSUP;
  303. }
  304. /* valid NS address */
  305. return 0;
  306. }