qemu_common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <string.h>
  7. #include <platform_def.h>
  8. #include <arch_helpers.h>
  9. #include <common/bl_common.h>
  10. #include <lib/xlat_tables/xlat_tables_v2.h>
  11. #include <services/el3_spmc_ffa_memory.h>
  12. #if ENABLE_RME
  13. #include <services/rmm_core_manifest.h>
  14. #endif
  15. #ifdef PLAT_qemu_sbsa
  16. #include <sbsa_platform.h>
  17. #endif
  18. #include <plat/common/platform.h>
  19. #include "qemu_private.h"
  20. #define MAP_DEVICE0 MAP_REGION_FLAT(DEVICE0_BASE, \
  21. DEVICE0_SIZE, \
  22. MT_DEVICE | MT_RW | EL3_PAS)
  23. #ifdef DEVICE1_BASE
  24. #define MAP_DEVICE1 MAP_REGION_FLAT(DEVICE1_BASE, \
  25. DEVICE1_SIZE, \
  26. MT_DEVICE | MT_RW | EL3_PAS)
  27. #endif
  28. #ifdef DEVICE2_BASE
  29. #define MAP_DEVICE2 MAP_REGION_FLAT(DEVICE2_BASE, \
  30. DEVICE2_SIZE, \
  31. MT_DEVICE | MT_RW | EL3_PAS)
  32. #endif
  33. #define MAP_SHARED_RAM MAP_REGION_FLAT(SHARED_RAM_BASE, \
  34. SHARED_RAM_SIZE, \
  35. MT_DEVICE | MT_RW | EL3_PAS)
  36. #define MAP_BL32_MEM MAP_REGION_FLAT(BL32_MEM_BASE, BL32_MEM_SIZE, \
  37. MT_MEMORY | MT_RW | EL3_PAS)
  38. #define MAP_NS_DRAM0 MAP_REGION_FLAT(NS_DRAM0_BASE, NS_DRAM0_SIZE, \
  39. MT_MEMORY | MT_RW | MT_NS)
  40. #define MAP_FLASH0 MAP_REGION_FLAT(QEMU_FLASH0_BASE, QEMU_FLASH0_SIZE, \
  41. MT_MEMORY | MT_RO | EL3_PAS)
  42. #define MAP_FLASH1 MAP_REGION_FLAT(QEMU_FLASH1_BASE, QEMU_FLASH1_SIZE, \
  43. MT_MEMORY | MT_RO | EL3_PAS)
  44. #ifdef FW_HANDOFF_BASE
  45. #define MAP_FW_HANDOFF MAP_REGION_FLAT(FW_HANDOFF_BASE, FW_HANDOFF_SIZE, \
  46. MT_MEMORY | MT_RW | EL3_PAS)
  47. #endif
  48. #ifdef FW_NS_HANDOFF_BASE
  49. #define MAP_FW_NS_HANDOFF MAP_REGION_FLAT(FW_NS_HANDOFF_BASE, FW_HANDOFF_SIZE, \
  50. MT_MEMORY | MT_RW | MT_NS)
  51. #endif
  52. /*
  53. * Table of regions for various BL stages to map using the MMU.
  54. * This doesn't include TZRAM as the 'mem_layout' argument passed to
  55. * arm_configure_mmu_elx() will give the available subset of that,
  56. */
  57. #ifdef IMAGE_BL1
  58. static const mmap_region_t plat_qemu_mmap[] = {
  59. MAP_FLASH0,
  60. MAP_FLASH1,
  61. MAP_SHARED_RAM,
  62. MAP_DEVICE0,
  63. #ifdef MAP_DEVICE1
  64. MAP_DEVICE1,
  65. #endif
  66. #ifdef MAP_DEVICE2
  67. MAP_DEVICE2,
  68. #endif
  69. {0}
  70. };
  71. #endif
  72. #ifdef IMAGE_BL2
  73. static const mmap_region_t plat_qemu_mmap[] = {
  74. MAP_FLASH0,
  75. MAP_FLASH1,
  76. MAP_SHARED_RAM,
  77. MAP_DEVICE0,
  78. #ifdef MAP_DEVICE1
  79. MAP_DEVICE1,
  80. #endif
  81. #ifdef MAP_DEVICE2
  82. MAP_DEVICE2,
  83. #endif
  84. MAP_NS_DRAM0,
  85. #if SPM_MM
  86. QEMU_SP_IMAGE_MMAP,
  87. #else
  88. MAP_BL32_MEM,
  89. #endif
  90. #ifdef MAP_FW_HANDOFF
  91. MAP_FW_HANDOFF,
  92. #endif
  93. {0}
  94. };
  95. #endif
  96. #ifdef IMAGE_BL31
  97. static const mmap_region_t plat_qemu_mmap[] = {
  98. MAP_SHARED_RAM,
  99. MAP_DEVICE0,
  100. #ifdef MAP_DEVICE1
  101. MAP_DEVICE1,
  102. #endif
  103. #ifdef MAP_DEVICE2
  104. MAP_DEVICE2,
  105. #endif
  106. #ifdef MAP_FW_HANDOFF
  107. MAP_FW_HANDOFF,
  108. #endif
  109. #ifdef MAP_FW_NS_HANDOFF
  110. MAP_FW_NS_HANDOFF,
  111. #endif
  112. #if SPM_MM
  113. MAP_NS_DRAM0,
  114. QEMU_SPM_BUF_EL3_MMAP,
  115. #elif !SPMC_AT_EL3
  116. MAP_BL32_MEM,
  117. #endif
  118. {0}
  119. };
  120. #endif
  121. #ifdef IMAGE_BL32
  122. static const mmap_region_t plat_qemu_mmap[] = {
  123. MAP_SHARED_RAM,
  124. MAP_DEVICE0,
  125. #ifdef MAP_DEVICE1
  126. MAP_DEVICE1,
  127. #endif
  128. #ifdef MAP_DEVICE2
  129. MAP_DEVICE2,
  130. #endif
  131. {0}
  132. };
  133. #endif
  134. #ifdef IMAGE_RMM
  135. const mmap_region_t plat_qemu_mmap[] = {
  136. MAP_DEVICE0,
  137. #ifdef MAP_DEVICE1
  138. MAP_DEVICE1,
  139. #endif
  140. #ifdef MAP_DEVICE2
  141. MAP_DEVICE2,
  142. #endif
  143. {0}
  144. };
  145. #endif
  146. /*******************************************************************************
  147. * Returns QEMU platform specific memory map regions.
  148. ******************************************************************************/
  149. const mmap_region_t *plat_qemu_get_mmap(void)
  150. {
  151. return plat_qemu_mmap;
  152. }
  153. #if MEASURED_BOOT || TRUSTED_BOARD_BOOT
  154. int plat_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
  155. {
  156. return get_mbedtls_heap_helper(heap_addr, heap_size);
  157. }
  158. #endif
  159. #if SPMC_AT_EL3
  160. /*
  161. * When using the EL3 SPMC implementation allocate the datastore
  162. * for tracking shared memory descriptors in normal memory.
  163. */
  164. #define PLAT_SPMC_SHMEM_DATASTORE_SIZE 64 * 1024
  165. uint8_t plat_spmc_shmem_datastore[PLAT_SPMC_SHMEM_DATASTORE_SIZE] __aligned(2 * sizeof(long));
  166. int plat_spmc_shmem_datastore_get(uint8_t **datastore, size_t *size)
  167. {
  168. *datastore = plat_spmc_shmem_datastore;
  169. *size = PLAT_SPMC_SHMEM_DATASTORE_SIZE;
  170. return 0;
  171. }
  172. int plat_spmc_shmem_begin(struct ffa_mtd *desc)
  173. {
  174. return 0;
  175. }
  176. int plat_spmc_shmem_reclaim(struct ffa_mtd *desc)
  177. {
  178. return 0;
  179. }
  180. #endif
  181. #if defined(SPD_spmd)
  182. int plat_spmd_handle_group0_interrupt(uint32_t intid)
  183. {
  184. /*
  185. * Currently, there are no sources of Group0 secure interrupt
  186. * enabled for QEMU.
  187. */
  188. (void)intid;
  189. return -1;
  190. }
  191. #endif /*defined(SPD_spmd)*/
  192. #if ENABLE_RME
  193. /*
  194. * Get a pointer to the RMM-EL3 Shared buffer and return it
  195. * through the pointer passed as parameter.
  196. *
  197. * This function returns the size of the shared buffer.
  198. */
  199. size_t plat_rmmd_get_el3_rmm_shared_mem(uintptr_t *shared)
  200. {
  201. *shared = (uintptr_t)RMM_SHARED_BASE;
  202. return (size_t)RMM_SHARED_SIZE;
  203. }
  204. #ifdef PLAT_qemu
  205. static uint32_t plat_get_num_memnodes(void)
  206. {
  207. return 1;
  208. }
  209. static void plat_get_memory_node(int index, struct ns_dram_bank *bank_ptr)
  210. {
  211. (void) index;
  212. bank_ptr->base = NS_DRAM0_BASE;
  213. bank_ptr->size = NS_DRAM0_SIZE;
  214. }
  215. #elif PLAT_qemu_sbsa
  216. static uint32_t plat_get_num_memnodes(void)
  217. {
  218. return sbsa_platform_num_memnodes();
  219. }
  220. static void plat_get_memory_node(int index, struct ns_dram_bank *bank_ptr)
  221. {
  222. struct platform_memory_data data = {0, 0, 0};
  223. if (index < sbsa_platform_num_memnodes()) {
  224. data = sbsa_platform_memory_node(index);
  225. }
  226. bank_ptr->base = data.addr_base;
  227. bank_ptr->size = data.addr_size;
  228. }
  229. #endif /* PLAT_qemu */
  230. int plat_rmmd_load_manifest(struct rmm_manifest *manifest)
  231. {
  232. int i, last;
  233. uint64_t checksum;
  234. size_t num_banks = plat_get_num_memnodes();
  235. size_t num_consoles = 1;
  236. struct ns_dram_bank *bank_ptr;
  237. struct console_info *console_ptr;
  238. assert(manifest != NULL);
  239. manifest->version = RMMD_MANIFEST_VERSION;
  240. manifest->padding = 0U; /* RES0 */
  241. manifest->plat_data = (uintptr_t)NULL;
  242. manifest->plat_dram.num_banks = num_banks;
  243. manifest->plat_console.num_consoles = num_consoles;
  244. /*
  245. * Boot manifest structure illustration:
  246. *
  247. * +----------------------------------------+
  248. * | offset | field | comment |
  249. * +----------+--------------+--------------+
  250. * | 0 | version | 0x00000003 |
  251. * +----------+--------------+--------------+
  252. * | 4 | padding | 0x00000000 |
  253. * +----------+--------------+--------------+
  254. * | 8 | plat_data | NULL |
  255. * +----------+--------------+--------------+
  256. * | 16 | num_banks | |
  257. * +----------+--------------+ |
  258. * | 24 | banks | plat_dram |
  259. * +----------+--------------+ |
  260. * | 32 | checksum | |
  261. * +----------+--------------+--------------+
  262. * | 40 | num_consoles | |
  263. * +----------+--------------+ |
  264. * | 48 | consoles | plat_console |
  265. * +----------+--------------+ |
  266. * | 56 | checksum | |
  267. * +----------+--------------+--------------+
  268. * | 64 | base 0 | |
  269. * +----------+--------------+ bank[0] |
  270. * | 72 | size 0 | |
  271. * +----------+--------------+--------------+
  272. * | 80 | base | |
  273. * +----------+--------------+ |
  274. * | 88 | map_pages | |
  275. * +----------+--------------+ |
  276. * | 96 | name | |
  277. * +----------+--------------+ consoles[0] |
  278. * | 104 | clk_in_hz | |
  279. * +----------+--------------+ |
  280. * | 112 | baud_rate | |
  281. * +----------+--------------+ |
  282. * | 120 | flags | |
  283. * +----------+--------------+--------------+
  284. */
  285. bank_ptr = (struct ns_dram_bank *)
  286. (((uintptr_t)manifest) + sizeof(*manifest));
  287. console_ptr = (struct console_info *)
  288. ((uintptr_t)bank_ptr + (num_banks * sizeof(*bank_ptr)));
  289. manifest->plat_dram.banks = bank_ptr;
  290. manifest->plat_console.consoles = console_ptr;
  291. /* Ensure the manifest is not larger than the shared buffer */
  292. assert((sizeof(struct rmm_manifest) +
  293. (sizeof(struct console_info) * num_consoles) +
  294. (sizeof(struct ns_dram_bank) * num_banks)) <= RMM_SHARED_SIZE);
  295. /* Calculate checksum of plat_dram structure */
  296. checksum = num_banks + (uint64_t)bank_ptr;
  297. /*
  298. * In the TF-A, NUMA nodes (if present) are stored in descending
  299. * order, i.e:
  300. *
  301. * INFO: RAM 0: node-id: 1, address: 0x10080000000 - 0x101ffffffff
  302. * INFO: RAM 1: node-id: 0, address: 0x10043000000 - 0x1007fffffff
  303. *
  304. * The RMM expects the memory banks to be presented in ascending order:
  305. *
  306. * INFO: RAM 1: node-id: 0, address: 0x10043000000 - 0x1007fffffff
  307. * INFO: RAM 0: node-id: 1, address: 0x10080000000 - 0x101ffffffff
  308. *
  309. * As such, go through the NUMA nodes one by one and fill out
  310. * @bank_ptr[] starting from the end. When NUMA nodes are not present
  311. * there is only one memory bank and none of the above matters.
  312. */
  313. last = num_banks - 1;
  314. for (i = 0; i < num_banks; i++) {
  315. plat_get_memory_node(i, &bank_ptr[last]);
  316. checksum += bank_ptr[last].base + bank_ptr[last].size;
  317. last--;
  318. }
  319. /* Checksum must be 0 */
  320. manifest->plat_dram.checksum = ~checksum + 1UL;
  321. /* Calculate the checksum of the plat_consoles structure */
  322. checksum = num_consoles + (uint64_t)console_ptr;
  323. /* Zero out the console info struct */
  324. memset((void *)console_ptr, 0, sizeof(struct console_info) * num_consoles);
  325. console_ptr[0].map_pages = 1;
  326. console_ptr[0].base = PLAT_QEMU_BOOT_UART_BASE;
  327. console_ptr[0].clk_in_hz = PLAT_QEMU_BOOT_UART_CLK_IN_HZ;
  328. console_ptr[0].baud_rate = PLAT_QEMU_CONSOLE_BAUDRATE;
  329. strlcpy(console_ptr[0].name, "pl011", sizeof(console_ptr[0].name));
  330. /* Update checksum */
  331. checksum += console_ptr[0].base + console_ptr[0].map_pages +
  332. console_ptr[0].clk_in_hz + console_ptr[0].baud_rate;
  333. /* Checksum must be 0 */
  334. manifest->plat_console.checksum = ~checksum + 1UL;
  335. return 0;
  336. }
  337. #endif /* ENABLE_RME */
  338. /**
  339. * plat_qemu_dt_runtime_address() - Get the final DT location in RAM
  340. *
  341. * When support is enabled on SBSA, the device tree is relocated from its
  342. * original place at the beginning of the NS RAM to after the RMM. This
  343. * function returns the address of the final location in RAM of the device
  344. * tree. See function update_dt() in qemu_bl2_setup.c
  345. *
  346. * Return: The address of the final location in RAM of the device tree
  347. */
  348. #if (ENABLE_RME && PLAT_qemu_sbsa)
  349. void *plat_qemu_dt_runtime_address(void)
  350. {
  351. return (void *)(uintptr_t)PLAT_QEMU_DT_BASE;
  352. }
  353. #else
  354. void *plat_qemu_dt_runtime_address(void)
  355. {
  356. return (void *)(uintptr_t)ARM_PRELOADED_DTB_BASE;
  357. }
  358. #endif /* (ENABLE_RME && PLAT_qemu_sbsa) */