qemu_bl2_setup.c 13 KB

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