ls_bl2_el3_setup.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright 2018-2022 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <assert.h>
  8. #include <common/desc_image_load.h>
  9. #include <dcfg.h>
  10. #ifdef POLICY_FUSE_PROVISION
  11. #include <fuse_io.h>
  12. #endif
  13. #include <mmu_def.h>
  14. #include <plat_common.h>
  15. #ifdef NXP_NV_SW_MAINT_LAST_EXEC_DATA
  16. #include <plat_nv_storage.h>
  17. #endif
  18. #pragma weak bl2_el3_early_platform_setup
  19. #pragma weak bl2_el3_plat_arch_setup
  20. #pragma weak bl2_el3_plat_prepare_exit
  21. static dram_regions_info_t dram_regions_info = {0};
  22. /*******************************************************************************
  23. * Return the pointer to the 'dram_regions_info structure of the DRAM.
  24. * This structure is populated after init_ddr().
  25. ******************************************************************************/
  26. dram_regions_info_t *get_dram_regions_info(void)
  27. {
  28. return &dram_regions_info;
  29. }
  30. #ifdef DDR_INIT
  31. static void populate_dram_regions_info(void)
  32. {
  33. long long dram_remain_size = dram_regions_info.total_dram_size;
  34. uint8_t reg_id = 0U;
  35. dram_regions_info.region[reg_id].addr = NXP_DRAM0_ADDR;
  36. dram_regions_info.region[reg_id].size =
  37. dram_remain_size > NXP_DRAM0_MAX_SIZE ?
  38. NXP_DRAM0_MAX_SIZE : dram_remain_size;
  39. if (dram_regions_info.region[reg_id].size != NXP_DRAM0_SIZE) {
  40. ERROR("Incorrect DRAM0 size is defined in platform_def.h\n");
  41. }
  42. dram_remain_size -= dram_regions_info.region[reg_id].size;
  43. dram_regions_info.region[reg_id].size -= (NXP_SECURE_DRAM_SIZE
  44. + NXP_SP_SHRD_DRAM_SIZE);
  45. assert(dram_regions_info.region[reg_id].size > 0);
  46. /* Reducing total dram size by 66MB */
  47. dram_regions_info.total_dram_size -= (NXP_SECURE_DRAM_SIZE
  48. + NXP_SP_SHRD_DRAM_SIZE);
  49. #if defined(NXP_DRAM1_ADDR) && defined(NXP_DRAM1_MAX_SIZE)
  50. if (dram_remain_size > 0) {
  51. reg_id++;
  52. dram_regions_info.region[reg_id].addr = NXP_DRAM1_ADDR;
  53. dram_regions_info.region[reg_id].size =
  54. dram_remain_size > NXP_DRAM1_MAX_SIZE ?
  55. NXP_DRAM1_MAX_SIZE : dram_remain_size;
  56. dram_remain_size -= dram_regions_info.region[reg_id].size;
  57. }
  58. #endif
  59. #if defined(NXP_DRAM2_ADDR) && defined(NXP_DRAM2_MAX_SIZE)
  60. if (dram_remain_size > 0) {
  61. reg_id++;
  62. dram_regions_info.region[reg_id].addr = NXP_DRAM1_ADDR;
  63. dram_regions_info.region[reg_id].size =
  64. dram_remain_size > NXP_DRAM1_MAX_SIZE ?
  65. NXP_DRAM1_MAX_SIZE : dram_remain_size;
  66. dram_remain_size -= dram_regions_info.region[reg_id].size;
  67. }
  68. #endif
  69. reg_id++;
  70. dram_regions_info.num_dram_regions = reg_id;
  71. }
  72. #endif
  73. #ifdef IMAGE_BL32
  74. /*******************************************************************************
  75. * Gets SPSR for BL32 entry
  76. ******************************************************************************/
  77. static uint32_t ls_get_spsr_for_bl32_entry(void)
  78. {
  79. /*
  80. * The Secure Payload Dispatcher service is responsible for
  81. * setting the SPSR prior to entry into the BL32 image.
  82. */
  83. return 0U;
  84. }
  85. #endif
  86. /*******************************************************************************
  87. * Gets SPSR for BL33 entry
  88. ******************************************************************************/
  89. #ifndef AARCH32
  90. static uint32_t ls_get_spsr_for_bl33_entry(void)
  91. {
  92. unsigned int mode;
  93. uint32_t spsr;
  94. /* Figure out what mode we enter the non-secure world in */
  95. mode = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1;
  96. /*
  97. * TODO: Consider the possibility of specifying the SPSR in
  98. * the FIP ToC and allowing the platform to have a say as
  99. * well.
  100. */
  101. spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  102. return spsr;
  103. }
  104. #else
  105. /*******************************************************************************
  106. * Gets SPSR for BL33 entry
  107. ******************************************************************************/
  108. static uint32_t ls_get_spsr_for_bl33_entry(void)
  109. {
  110. unsigned int hyp_status, mode, spsr;
  111. hyp_status = GET_VIRT_EXT(read_id_pfr1());
  112. mode = (hyp_status) ? MODE32_hyp : MODE32_svc;
  113. /*
  114. * TODO: Consider the possibility of specifying the SPSR in
  115. * the FIP ToC and allowing the platform to have a say as
  116. * well.
  117. */
  118. spsr = SPSR_MODE32(mode, plat_get_ns_image_entrypoint() & 0x1,
  119. SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
  120. return spsr;
  121. }
  122. #endif /* AARCH32 */
  123. void bl2_el3_early_platform_setup(u_register_t arg0 __unused,
  124. u_register_t arg1 __unused,
  125. u_register_t arg2 __unused,
  126. u_register_t arg3 __unused)
  127. {
  128. /*
  129. * SoC specific early init
  130. * Any errata handling or SoC specific early initialization can
  131. * be done here
  132. * Set Counter Base Frequency in CNTFID0 and in cntfrq_el0.
  133. * Initialize the interconnect.
  134. * Enable coherency for primary CPU cluster
  135. */
  136. soc_early_init();
  137. /* Initialise the IO layer and register platform IO devices */
  138. plat_io_setup();
  139. if (dram_regions_info.total_dram_size > 0) {
  140. populate_dram_regions_info();
  141. }
  142. #ifdef NXP_NV_SW_MAINT_LAST_EXEC_DATA
  143. read_nv_app_data();
  144. #if DEBUG
  145. const nv_app_data_t *nv_app_data = get_nv_data();
  146. INFO("Value of warm_reset flag = 0x%x\n", nv_app_data->warm_rst_flag);
  147. INFO("Value of WDT flag = 0x%x\n", nv_app_data->wdt_rst_flag);
  148. #endif
  149. #endif
  150. }
  151. /*******************************************************************************
  152. * Perform the very early platform specific architectural setup here. At the
  153. * moment this is only initializes the mmu in a quick and dirty way.
  154. ******************************************************************************/
  155. void ls_bl2_el3_plat_arch_setup(void)
  156. {
  157. unsigned int flags = 0U;
  158. /* Initialise the IO layer and register platform IO devices */
  159. ls_setup_page_tables(
  160. #if SEPARATE_BL2_NOLOAD_REGION
  161. BL2_START,
  162. BL2_LIMIT - BL2_START,
  163. #else
  164. BL2_BASE,
  165. (unsigned long)(&__BL2_END__) - BL2_BASE,
  166. #endif
  167. BL_CODE_BASE,
  168. BL_CODE_END,
  169. BL_RO_DATA_BASE,
  170. BL_RO_DATA_END
  171. #if USE_COHERENT_MEM
  172. , BL_COHERENT_RAM_BASE,
  173. BL_COHERENT_RAM_END
  174. #endif
  175. );
  176. if ((dram_regions_info.region[0].addr == 0)
  177. && (dram_regions_info.total_dram_size == 0)) {
  178. flags = XLAT_TABLE_NC;
  179. }
  180. #ifdef AARCH32
  181. enable_mmu_secure(0);
  182. #else
  183. enable_mmu_el3(flags);
  184. #endif
  185. }
  186. void bl2_el3_plat_arch_setup(void)
  187. {
  188. ls_bl2_el3_plat_arch_setup();
  189. }
  190. void bl2_platform_setup(void)
  191. {
  192. /*
  193. * Perform platform setup before loading the image.
  194. */
  195. }
  196. /* Handling image information by platform. */
  197. int ls_bl2_handle_post_image_load(unsigned int image_id)
  198. {
  199. int err = 0;
  200. bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
  201. assert(bl_mem_params);
  202. switch (image_id) {
  203. case BL31_IMAGE_ID:
  204. bl_mem_params->ep_info.args.arg3 =
  205. (u_register_t) &dram_regions_info;
  206. /* Pass the value of PORSR1 register in Argument 4 */
  207. bl_mem_params->ep_info.args.arg4 =
  208. (u_register_t)read_reg_porsr1();
  209. flush_dcache_range((uintptr_t)&dram_regions_info,
  210. sizeof(dram_regions_info));
  211. break;
  212. #if defined(AARCH64) && defined(IMAGE_BL32)
  213. case BL32_IMAGE_ID:
  214. bl_mem_params->ep_info.spsr = ls_get_spsr_for_bl32_entry();
  215. break;
  216. #endif
  217. case BL33_IMAGE_ID:
  218. /* BL33 expects to receive the primary CPU MPID (through r0) */
  219. bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
  220. bl_mem_params->ep_info.spsr = ls_get_spsr_for_bl33_entry();
  221. break;
  222. }
  223. return err;
  224. }
  225. /*******************************************************************************
  226. * This function can be used by the platforms to update/use image
  227. * information for given `image_id`.
  228. ******************************************************************************/
  229. int bl2_plat_handle_post_image_load(unsigned int image_id)
  230. {
  231. return ls_bl2_handle_post_image_load(image_id);
  232. }
  233. void bl2_el3_plat_prepare_exit(void)
  234. {
  235. return soc_bl2_prepare_exit();
  236. }
  237. /* Called to do the dynamic initialization required
  238. * before loading the next image.
  239. */
  240. void bl2_plat_preload_setup(void)
  241. {
  242. soc_preload_setup();
  243. #ifdef DDR_INIT
  244. if (dram_regions_info.total_dram_size <= 0) {
  245. ERROR("Asserting as the DDR is not initialized yet.");
  246. assert(false);
  247. }
  248. #endif
  249. if ((dram_regions_info.region[0].addr == 0)
  250. && (dram_regions_info.total_dram_size > 0)) {
  251. populate_dram_regions_info();
  252. #ifdef PLAT_XLAT_TABLES_DYNAMIC
  253. mmap_add_ddr_region_dynamically();
  254. #endif
  255. }
  256. /* setup the memory region access permissions */
  257. soc_mem_access();
  258. #ifdef POLICY_FUSE_PROVISION
  259. fip_fuse_provisioning((uintptr_t)FUSE_BUF, FUSE_SZ);
  260. #endif
  261. }