qemu_bl2_setup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <libfdt.h>
  9. #include <platform_def.h>
  10. #include <arch_features.h>
  11. #include <arch_helpers.h>
  12. #include <common/bl_common.h>
  13. #include <common/debug.h>
  14. #include <common/desc_image_load.h>
  15. #include <common/fdt_fixup.h>
  16. #include <common/fdt_wrappers.h>
  17. #include <lib/optee_utils.h>
  18. #include <lib/transfer_list.h>
  19. #include <lib/utils.h>
  20. #include <plat/common/platform.h>
  21. #include "qemu_private.h"
  22. #define MAP_BL2_TOTAL MAP_REGION_FLAT( \
  23. bl2_tzram_layout.total_base, \
  24. bl2_tzram_layout.total_size, \
  25. MT_MEMORY | MT_RW | EL3_PAS)
  26. #define MAP_BL2_RO MAP_REGION_FLAT( \
  27. BL_CODE_BASE, \
  28. BL_CODE_END - BL_CODE_BASE, \
  29. MT_CODE | EL3_PAS), \
  30. MAP_REGION_FLAT( \
  31. BL_RO_DATA_BASE, \
  32. BL_RO_DATA_END \
  33. - BL_RO_DATA_BASE, \
  34. MT_RO_DATA | EL3_PAS)
  35. #if USE_COHERENT_MEM
  36. #define MAP_BL_COHERENT_RAM MAP_REGION_FLAT( \
  37. BL_COHERENT_RAM_BASE, \
  38. BL_COHERENT_RAM_END \
  39. - BL_COHERENT_RAM_BASE, \
  40. MT_DEVICE | MT_RW | EL3_PAS)
  41. #endif
  42. /* Data structure which holds the extents of the trusted SRAM for BL2 */
  43. static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
  44. static struct transfer_list_header *bl2_tl;
  45. void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  46. u_register_t arg2, u_register_t arg3)
  47. {
  48. meminfo_t *mem_layout = (void *)arg1;
  49. /* Initialize the console to provide early debug support */
  50. qemu_console_init();
  51. /* Setup the BL2 memory layout */
  52. bl2_tzram_layout = *mem_layout;
  53. plat_qemu_io_setup();
  54. }
  55. static void security_setup(void)
  56. {
  57. /*
  58. * This is where a TrustZone address space controller and other
  59. * security related peripherals, would be configured.
  60. */
  61. }
  62. static void update_dt(void)
  63. {
  64. #if TRANSFER_LIST
  65. struct transfer_list_entry *te;
  66. #endif
  67. int ret;
  68. void *fdt = (void *)(uintptr_t)ARM_PRELOADED_DTB_BASE;
  69. void *dst = plat_qemu_dt_runtime_address();
  70. ret = fdt_open_into(fdt, dst, PLAT_QEMU_DT_MAX_SIZE);
  71. if (ret < 0) {
  72. ERROR("Invalid Device Tree at %p: error %d\n", fdt, ret);
  73. return;
  74. }
  75. if (dt_add_psci_node(fdt)) {
  76. ERROR("Failed to add PSCI Device Tree node\n");
  77. return;
  78. }
  79. if (dt_add_psci_cpu_enable_methods(fdt)) {
  80. ERROR("Failed to add PSCI cpu enable methods in Device Tree\n");
  81. return;
  82. }
  83. #if ENABLE_RME
  84. if (fdt_add_reserved_memory(fdt, "rmm", REALM_DRAM_BASE,
  85. REALM_DRAM_SIZE)) {
  86. ERROR("Failed to reserve RMM memory in Device Tree\n");
  87. return;
  88. }
  89. INFO("Reserved RMM memory [0x%lx, 0x%lx] in Device tree\n",
  90. (uintptr_t)REALM_DRAM_BASE,
  91. (uintptr_t)REALM_DRAM_BASE + REALM_DRAM_SIZE - 1);
  92. #endif
  93. ret = fdt_pack(fdt);
  94. if (ret < 0)
  95. ERROR("Failed to pack Device Tree at %p: error %d\n", fdt, ret);
  96. #if TRANSFER_LIST
  97. /* create a TE */
  98. te = transfer_list_add(bl2_tl, TL_TAG_FDT, fdt_totalsize(fdt), fdt);
  99. if (!te) {
  100. ERROR("Failed to add FDT entry to Transfer List\n");
  101. return;
  102. }
  103. #endif
  104. }
  105. void bl2_platform_setup(void)
  106. {
  107. #if TRANSFER_LIST
  108. bl2_tl = transfer_list_init((void *)(uintptr_t)FW_HANDOFF_BASE,
  109. FW_HANDOFF_SIZE);
  110. if (!bl2_tl) {
  111. ERROR("Failed to initialize Transfer List at 0x%lx\n",
  112. (unsigned long)FW_HANDOFF_BASE);
  113. }
  114. #endif
  115. security_setup();
  116. update_dt();
  117. /* TODO Initialize timer */
  118. }
  119. void qemu_bl2_sync_transfer_list(void)
  120. {
  121. #if TRANSFER_LIST
  122. transfer_list_update_checksum(bl2_tl);
  123. #endif
  124. }
  125. void bl2_plat_arch_setup(void)
  126. {
  127. const mmap_region_t bl_regions[] = {
  128. MAP_BL2_TOTAL,
  129. MAP_BL2_RO,
  130. #if USE_COHERENT_MEM
  131. MAP_BL_COHERENT_RAM,
  132. #endif
  133. #if ENABLE_RME
  134. MAP_RMM_DRAM,
  135. MAP_GPT_L0_REGION,
  136. MAP_GPT_L1_REGION,
  137. #endif
  138. {0}
  139. };
  140. setup_page_tables(bl_regions, plat_qemu_get_mmap());
  141. #if ENABLE_RME
  142. /* BL2 runs in EL3 when RME enabled. */
  143. assert(is_feat_rme_present());
  144. enable_mmu_el3(0);
  145. #else /* ENABLE_RME */
  146. #ifdef __aarch64__
  147. enable_mmu_el1(0);
  148. #else
  149. enable_mmu_svc_mon(0);
  150. #endif
  151. #endif /* ENABLE_RME */
  152. }
  153. /*******************************************************************************
  154. * Gets SPSR for BL32 entry
  155. ******************************************************************************/
  156. static uint32_t qemu_get_spsr_for_bl32_entry(void)
  157. {
  158. #ifdef __aarch64__
  159. /*
  160. * The Secure Payload Dispatcher service is responsible for
  161. * setting the SPSR prior to entry into the BL3-2 image.
  162. */
  163. return 0;
  164. #else
  165. return SPSR_MODE32(MODE32_svc, SPSR_T_ARM, SPSR_E_LITTLE,
  166. DISABLE_ALL_EXCEPTIONS);
  167. #endif
  168. }
  169. /*******************************************************************************
  170. * Gets SPSR for BL33 entry
  171. ******************************************************************************/
  172. static uint32_t qemu_get_spsr_for_bl33_entry(void)
  173. {
  174. uint32_t spsr;
  175. #ifdef __aarch64__
  176. unsigned int mode;
  177. /* Figure out what mode we enter the non-secure world in */
  178. mode = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1;
  179. /*
  180. * TODO: Consider the possibility of specifying the SPSR in
  181. * the FIP ToC and allowing the platform to have a say as
  182. * well.
  183. */
  184. spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
  185. #else
  186. spsr = SPSR_MODE32(MODE32_svc,
  187. plat_get_ns_image_entrypoint() & 0x1,
  188. SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
  189. #endif
  190. return spsr;
  191. }
  192. #if defined(SPD_spmd) && SPMD_SPM_AT_SEL2
  193. static int load_sps_from_tb_fw_config(struct image_info *image_info)
  194. {
  195. void *dtb = (void *)image_info->image_base;
  196. const char *compat_str = "arm,sp";
  197. const struct fdt_property *uuid;
  198. uint32_t load_addr;
  199. const char *name;
  200. int sp_node;
  201. int node;
  202. node = fdt_node_offset_by_compatible(dtb, -1, compat_str);
  203. if (node < 0) {
  204. ERROR("Can't find %s in TB_FW_CONFIG", compat_str);
  205. return -1;
  206. }
  207. fdt_for_each_subnode(sp_node, dtb, node) {
  208. name = fdt_get_name(dtb, sp_node, NULL);
  209. if (name == NULL) {
  210. ERROR("Can't get name of node in dtb\n");
  211. return -1;
  212. }
  213. uuid = fdt_get_property(dtb, sp_node, "uuid", NULL);
  214. if (uuid == NULL) {
  215. ERROR("Can't find property uuid in node %s", name);
  216. return -1;
  217. }
  218. if (fdt_read_uint32(dtb, sp_node, "load-address",
  219. &load_addr) < 0) {
  220. ERROR("Can't read load-address in node %s", name);
  221. return -1;
  222. }
  223. if (qemu_io_register_sp_pkg(name, uuid->data, load_addr) < 0) {
  224. return -1;
  225. }
  226. }
  227. return 0;
  228. }
  229. #endif /*defined(SPD_spmd) && SPMD_SPM_AT_SEL2*/
  230. #if defined(SPD_opteed) || defined(AARCH32_SP_OPTEE) || defined(SPMC_OPTEE)
  231. static int handoff_pageable_part(uint64_t pagable_part)
  232. {
  233. #if TRANSFER_LIST
  234. struct transfer_list_entry *te;
  235. te = transfer_list_add(bl2_tl, TL_TAG_OPTEE_PAGABLE_PART,
  236. sizeof(pagable_part), &pagable_part);
  237. if (!te) {
  238. INFO("Cannot add TE for pageable part\n");
  239. return -1;
  240. }
  241. #endif
  242. return 0;
  243. }
  244. #endif
  245. static int qemu_bl2_handle_post_image_load(unsigned int image_id)
  246. {
  247. int err = 0;
  248. bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
  249. #if defined(SPD_opteed) || defined(AARCH32_SP_OPTEE) || defined(SPMC_OPTEE)
  250. bl_mem_params_node_t *pager_mem_params = NULL;
  251. bl_mem_params_node_t *paged_mem_params = NULL;
  252. #endif
  253. #if defined(SPD_spmd)
  254. bl_mem_params_node_t *bl32_mem_params = NULL;
  255. #endif
  256. #if TRANSFER_LIST
  257. struct transfer_list_header *ns_tl = NULL;
  258. #endif
  259. assert(bl_mem_params);
  260. switch (image_id) {
  261. #if TRANSFER_LIST
  262. case BL31_IMAGE_ID:
  263. /*
  264. * arg0 is a bl_params_t reserved for bl31_early_platform_setup2
  265. * we just need arg1 and arg3 for BL31 to update the TL from S
  266. * to NS memory before it exits
  267. */
  268. #ifdef __aarch64__
  269. if (GET_RW(bl_mem_params->ep_info.spsr) == MODE_RW_64) {
  270. bl_mem_params->ep_info.args.arg1 =
  271. TRANSFER_LIST_HANDOFF_X1_VALUE(REGISTER_CONVENTION_VERSION);
  272. } else
  273. #endif
  274. {
  275. bl_mem_params->ep_info.args.arg1 =
  276. TRANSFER_LIST_HANDOFF_R1_VALUE(REGISTER_CONVENTION_VERSION);
  277. }
  278. bl_mem_params->ep_info.args.arg3 = (uintptr_t)bl2_tl;
  279. break;
  280. #endif
  281. case BL32_IMAGE_ID:
  282. #if defined(SPD_opteed) || defined(AARCH32_SP_OPTEE) || defined(SPMC_OPTEE)
  283. pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
  284. assert(pager_mem_params);
  285. paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
  286. assert(paged_mem_params);
  287. err = parse_optee_header(&bl_mem_params->ep_info,
  288. &pager_mem_params->image_info,
  289. &paged_mem_params->image_info);
  290. if (err != 0) {
  291. WARN("OPTEE header parse error.\n");
  292. }
  293. /* add TL_TAG_OPTEE_PAGABLE_PART entry to the TL */
  294. if (handoff_pageable_part(bl_mem_params->ep_info.args.arg1)) {
  295. return -1;
  296. }
  297. #endif
  298. INFO("Handoff to BL32\n");
  299. bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl32_entry();
  300. if (TRANSFER_LIST &&
  301. transfer_list_set_handoff_args(bl2_tl,
  302. &bl_mem_params->ep_info))
  303. break;
  304. INFO("Using default arguments\n");
  305. #if defined(SPMC_OPTEE)
  306. /*
  307. * Explicit zeroes to unused registers since they may have
  308. * been populated by parse_optee_header() above.
  309. *
  310. * OP-TEE expects system DTB in x2 and TOS_FW_CONFIG in x0,
  311. * the latter is filled in below for TOS_FW_CONFIG_ID and
  312. * applies to any other SPMC too.
  313. */
  314. bl_mem_params->ep_info.args.arg2 = ARM_PRELOADED_DTB_BASE;
  315. #elif defined(SPD_opteed)
  316. /*
  317. * OP-TEE expect to receive DTB address in x2.
  318. * This will be copied into x2 by dispatcher.
  319. */
  320. bl_mem_params->ep_info.args.arg3 = ARM_PRELOADED_DTB_BASE;
  321. #elif defined(AARCH32_SP_OPTEE)
  322. bl_mem_params->ep_info.args.arg0 =
  323. bl_mem_params->ep_info.args.arg1;
  324. bl_mem_params->ep_info.args.arg1 = 0;
  325. bl_mem_params->ep_info.args.arg2 = ARM_PRELOADED_DTB_BASE;
  326. bl_mem_params->ep_info.args.arg3 = 0;
  327. #endif
  328. break;
  329. case BL33_IMAGE_ID:
  330. #ifdef AARCH32_SP_OPTEE
  331. /* AArch32 only core: OP-TEE expects NSec EP in register LR */
  332. pager_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
  333. assert(pager_mem_params);
  334. pager_mem_params->ep_info.lr_svc = bl_mem_params->ep_info.pc;
  335. #endif
  336. bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl33_entry();
  337. #if ARM_LINUX_KERNEL_AS_BL33
  338. /*
  339. * According to the file ``Documentation/arm64/booting.txt`` of
  340. * the Linux kernel tree, Linux expects the physical address of
  341. * the device tree blob (DTB) in x0, while x1-x3 are reserved
  342. * for future use and must be 0.
  343. */
  344. bl_mem_params->ep_info.args.arg0 =
  345. (u_register_t)ARM_PRELOADED_DTB_BASE;
  346. bl_mem_params->ep_info.args.arg1 = 0U;
  347. bl_mem_params->ep_info.args.arg2 = 0U;
  348. bl_mem_params->ep_info.args.arg3 = 0U;
  349. #elif TRANSFER_LIST
  350. if (bl2_tl) {
  351. /* relocate the tl to pre-allocate NS memory */
  352. ns_tl = transfer_list_relocate(bl2_tl,
  353. (void *)(uintptr_t)FW_NS_HANDOFF_BASE,
  354. bl2_tl->max_size);
  355. if (!ns_tl) {
  356. ERROR("Relocate TL to 0x%lx failed\n",
  357. (unsigned long)FW_NS_HANDOFF_BASE);
  358. return -1;
  359. }
  360. }
  361. INFO("Handoff to BL33\n");
  362. if (!transfer_list_set_handoff_args(ns_tl,
  363. &bl_mem_params->ep_info)) {
  364. INFO("Invalid TL, fallback to default arguments\n");
  365. bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
  366. }
  367. #else
  368. /* BL33 expects to receive the primary CPU MPID (through r0) */
  369. bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr();
  370. #endif /* ARM_LINUX_KERNEL_AS_BL33 */
  371. break;
  372. #ifdef SPD_spmd
  373. #if SPMD_SPM_AT_SEL2
  374. case TB_FW_CONFIG_ID:
  375. err = load_sps_from_tb_fw_config(&bl_mem_params->image_info);
  376. break;
  377. #endif
  378. case TOS_FW_CONFIG_ID:
  379. /* An SPMC expects TOS_FW_CONFIG in x0/r0 */
  380. bl32_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
  381. bl32_mem_params->ep_info.args.arg0 =
  382. bl_mem_params->image_info.image_base;
  383. break;
  384. #endif
  385. default:
  386. /* Do nothing in default case */
  387. break;
  388. }
  389. return err;
  390. }
  391. /*******************************************************************************
  392. * This function can be used by the platforms to update/use image
  393. * information for given `image_id`.
  394. ******************************************************************************/
  395. int bl2_plat_handle_post_image_load(unsigned int image_id)
  396. {
  397. return qemu_bl2_handle_post_image_load(image_id);
  398. }
  399. uintptr_t plat_get_ns_image_entrypoint(void)
  400. {
  401. return NS_IMAGE_OFFSET;
  402. }