qemu_common.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. #include <plat/common/platform.h>
  16. #include "qemu_private.h"
  17. #define MAP_DEVICE0 MAP_REGION_FLAT(DEVICE0_BASE, \
  18. DEVICE0_SIZE, \
  19. MT_DEVICE | MT_RW | EL3_PAS)
  20. #ifdef DEVICE1_BASE
  21. #define MAP_DEVICE1 MAP_REGION_FLAT(DEVICE1_BASE, \
  22. DEVICE1_SIZE, \
  23. MT_DEVICE | MT_RW | EL3_PAS)
  24. #endif
  25. #ifdef DEVICE2_BASE
  26. #define MAP_DEVICE2 MAP_REGION_FLAT(DEVICE2_BASE, \
  27. DEVICE2_SIZE, \
  28. MT_DEVICE | MT_RW | EL3_PAS)
  29. #endif
  30. #define MAP_SHARED_RAM MAP_REGION_FLAT(SHARED_RAM_BASE, \
  31. SHARED_RAM_SIZE, \
  32. MT_DEVICE | MT_RW | EL3_PAS)
  33. #define MAP_BL32_MEM MAP_REGION_FLAT(BL32_MEM_BASE, BL32_MEM_SIZE, \
  34. MT_MEMORY | MT_RW | EL3_PAS)
  35. #define MAP_NS_DRAM0 MAP_REGION_FLAT(NS_DRAM0_BASE, NS_DRAM0_SIZE, \
  36. MT_MEMORY | MT_RW | MT_NS)
  37. #define MAP_FLASH0 MAP_REGION_FLAT(QEMU_FLASH0_BASE, QEMU_FLASH0_SIZE, \
  38. MT_MEMORY | MT_RO | EL3_PAS)
  39. #define MAP_FLASH1 MAP_REGION_FLAT(QEMU_FLASH1_BASE, QEMU_FLASH1_SIZE, \
  40. MT_MEMORY | MT_RO | EL3_PAS)
  41. #ifdef FW_HANDOFF_BASE
  42. #define MAP_FW_HANDOFF MAP_REGION_FLAT(FW_HANDOFF_BASE, FW_HANDOFF_SIZE, \
  43. MT_MEMORY | MT_RW | EL3_PAS)
  44. #endif
  45. #ifdef FW_NS_HANDOFF_BASE
  46. #define MAP_FW_NS_HANDOFF MAP_REGION_FLAT(FW_NS_HANDOFF_BASE, FW_HANDOFF_SIZE, \
  47. MT_MEMORY | MT_RW | MT_NS)
  48. #endif
  49. /*
  50. * Table of regions for various BL stages to map using the MMU.
  51. * This doesn't include TZRAM as the 'mem_layout' argument passed to
  52. * arm_configure_mmu_elx() will give the available subset of that,
  53. */
  54. #ifdef IMAGE_BL1
  55. static const mmap_region_t plat_qemu_mmap[] = {
  56. MAP_FLASH0,
  57. MAP_FLASH1,
  58. MAP_SHARED_RAM,
  59. MAP_DEVICE0,
  60. #ifdef MAP_DEVICE1
  61. MAP_DEVICE1,
  62. #endif
  63. #ifdef MAP_DEVICE2
  64. MAP_DEVICE2,
  65. #endif
  66. {0}
  67. };
  68. #endif
  69. #ifdef IMAGE_BL2
  70. static const mmap_region_t plat_qemu_mmap[] = {
  71. MAP_FLASH0,
  72. MAP_FLASH1,
  73. MAP_SHARED_RAM,
  74. MAP_DEVICE0,
  75. #ifdef MAP_DEVICE1
  76. MAP_DEVICE1,
  77. #endif
  78. #ifdef MAP_DEVICE2
  79. MAP_DEVICE2,
  80. #endif
  81. MAP_NS_DRAM0,
  82. #if SPM_MM
  83. QEMU_SP_IMAGE_MMAP,
  84. #else
  85. MAP_BL32_MEM,
  86. #endif
  87. #ifdef MAP_FW_HANDOFF
  88. MAP_FW_HANDOFF,
  89. #endif
  90. {0}
  91. };
  92. #endif
  93. #ifdef IMAGE_BL31
  94. static const mmap_region_t plat_qemu_mmap[] = {
  95. MAP_SHARED_RAM,
  96. MAP_DEVICE0,
  97. #ifdef MAP_DEVICE1
  98. MAP_DEVICE1,
  99. #endif
  100. #ifdef MAP_DEVICE2
  101. MAP_DEVICE2,
  102. #endif
  103. #ifdef MAP_FW_HANDOFF
  104. MAP_FW_HANDOFF,
  105. #endif
  106. #ifdef MAP_FW_NS_HANDOFF
  107. MAP_FW_NS_HANDOFF,
  108. #endif
  109. #if SPM_MM
  110. MAP_NS_DRAM0,
  111. QEMU_SPM_BUF_EL3_MMAP,
  112. #elif !SPMC_AT_EL3
  113. MAP_BL32_MEM,
  114. #endif
  115. {0}
  116. };
  117. #endif
  118. #ifdef IMAGE_BL32
  119. static const mmap_region_t plat_qemu_mmap[] = {
  120. MAP_SHARED_RAM,
  121. MAP_DEVICE0,
  122. #ifdef MAP_DEVICE1
  123. MAP_DEVICE1,
  124. #endif
  125. #ifdef MAP_DEVICE2
  126. MAP_DEVICE2,
  127. #endif
  128. {0}
  129. };
  130. #endif
  131. #ifdef IMAGE_RMM
  132. const mmap_region_t plat_qemu_mmap[] = {
  133. MAP_DEVICE0,
  134. #ifdef MAP_DEVICE1
  135. MAP_DEVICE1,
  136. #endif
  137. #ifdef MAP_DEVICE2
  138. MAP_DEVICE2,
  139. #endif
  140. {0}
  141. };
  142. #endif
  143. /*******************************************************************************
  144. * Returns QEMU platform specific memory map regions.
  145. ******************************************************************************/
  146. const mmap_region_t *plat_qemu_get_mmap(void)
  147. {
  148. return plat_qemu_mmap;
  149. }
  150. #if MEASURED_BOOT || TRUSTED_BOARD_BOOT
  151. int plat_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
  152. {
  153. return get_mbedtls_heap_helper(heap_addr, heap_size);
  154. }
  155. #endif
  156. #if SPMC_AT_EL3
  157. /*
  158. * When using the EL3 SPMC implementation allocate the datastore
  159. * for tracking shared memory descriptors in normal memory.
  160. */
  161. #define PLAT_SPMC_SHMEM_DATASTORE_SIZE 64 * 1024
  162. uint8_t plat_spmc_shmem_datastore[PLAT_SPMC_SHMEM_DATASTORE_SIZE] __aligned(2 * sizeof(long));
  163. int plat_spmc_shmem_datastore_get(uint8_t **datastore, size_t *size)
  164. {
  165. *datastore = plat_spmc_shmem_datastore;
  166. *size = PLAT_SPMC_SHMEM_DATASTORE_SIZE;
  167. return 0;
  168. }
  169. int plat_spmc_shmem_begin(struct ffa_mtd *desc)
  170. {
  171. return 0;
  172. }
  173. int plat_spmc_shmem_reclaim(struct ffa_mtd *desc)
  174. {
  175. return 0;
  176. }
  177. #endif
  178. #if defined(SPD_spmd)
  179. int plat_spmd_handle_group0_interrupt(uint32_t intid)
  180. {
  181. /*
  182. * Currently, there are no sources of Group0 secure interrupt
  183. * enabled for QEMU.
  184. */
  185. (void)intid;
  186. return -1;
  187. }
  188. #endif /*defined(SPD_spmd)*/
  189. #if ENABLE_RME
  190. /*
  191. * Get a pointer to the RMM-EL3 Shared buffer and return it
  192. * through the pointer passed as parameter.
  193. *
  194. * This function returns the size of the shared buffer.
  195. */
  196. size_t plat_rmmd_get_el3_rmm_shared_mem(uintptr_t *shared)
  197. {
  198. *shared = (uintptr_t)RMM_SHARED_BASE;
  199. return (size_t)RMM_SHARED_SIZE;
  200. }
  201. int plat_rmmd_load_manifest(struct rmm_manifest *manifest)
  202. {
  203. uint64_t checksum;
  204. uintptr_t base;
  205. uint64_t size;
  206. size_t num_banks = 1;
  207. size_t num_consoles = 1;
  208. struct ns_dram_bank *bank_ptr;
  209. struct console_info *console_ptr;
  210. assert(manifest != NULL);
  211. manifest->version = RMMD_MANIFEST_VERSION;
  212. manifest->padding = 0U; /* RES0 */
  213. manifest->plat_data = (uintptr_t)NULL;
  214. manifest->plat_dram.num_banks = num_banks;
  215. manifest->plat_console.num_consoles = num_consoles;
  216. /*
  217. * Boot manifest structure illustration:
  218. *
  219. * +----------------------------------------+
  220. * | offset | field | comment |
  221. * +----------+--------------+--------------+
  222. * | 0 | version | 0x00000003 |
  223. * +----------+--------------+--------------+
  224. * | 4 | padding | 0x00000000 |
  225. * +----------+--------------+--------------+
  226. * | 8 | plat_data | NULL |
  227. * +----------+--------------+--------------+
  228. * | 16 | num_banks | |
  229. * +----------+--------------+ |
  230. * | 24 | banks | plat_dram |
  231. * +----------+--------------+ |
  232. * | 32 | checksum | |
  233. * +----------+--------------+--------------+
  234. * | 40 | num_consoles | |
  235. * +----------+--------------+ |
  236. * | 48 | consoles | plat_console |
  237. * +----------+--------------+ |
  238. * | 56 | checksum | |
  239. * +----------+--------------+--------------+
  240. * | 64 | base 0 | |
  241. * +----------+--------------+ bank[0] |
  242. * | 72 | size 0 | |
  243. * +----------+--------------+--------------+
  244. * | 80 | base | |
  245. * +----------+--------------+ |
  246. * | 88 | map_pages | |
  247. * +----------+--------------+ |
  248. * | 96 | name | |
  249. * +----------+--------------+ consoles[0] |
  250. * | 104 | clk_in_hz | |
  251. * +----------+--------------+ |
  252. * | 112 | baud_rate | |
  253. * +----------+--------------+ |
  254. * | 120 | flags | |
  255. * +----------+--------------+--------------+
  256. */
  257. bank_ptr = (struct ns_dram_bank *)
  258. (((uintptr_t)manifest) + sizeof(*manifest));
  259. console_ptr = (struct console_info *)
  260. ((uintptr_t)bank_ptr + (num_banks * sizeof(*bank_ptr)));
  261. manifest->plat_dram.banks = bank_ptr;
  262. manifest->plat_console.consoles = console_ptr;
  263. /* Ensure the manifest is not larger than the shared buffer */
  264. assert((sizeof(struct rmm_manifest) +
  265. (sizeof(struct console_info) * num_consoles) +
  266. (sizeof(struct ns_dram_bank) * num_banks)) <= RMM_SHARED_SIZE);
  267. /* Calculate checksum of plat_dram structure */
  268. checksum = num_banks + (uint64_t)bank_ptr;
  269. base = NS_DRAM0_BASE;
  270. size = NS_DRAM0_SIZE;
  271. bank_ptr[0].base = base;
  272. bank_ptr[0].size = size;
  273. checksum += base + size;
  274. /* Checksum must be 0 */
  275. manifest->plat_dram.checksum = ~checksum + 1UL;
  276. /* Calculate the checksum of the plat_consoles structure */
  277. checksum = num_consoles + (uint64_t)console_ptr;
  278. /* Zero out the console info struct */
  279. memset((void *)console_ptr, 0, sizeof(struct console_info) * num_consoles);
  280. console_ptr[0].map_pages = 1;
  281. console_ptr[0].base = PLAT_QEMU_BOOT_UART_BASE;
  282. console_ptr[0].clk_in_hz = PLAT_QEMU_BOOT_UART_CLK_IN_HZ;
  283. console_ptr[0].baud_rate = PLAT_QEMU_CONSOLE_BAUDRATE;
  284. strlcpy(console_ptr[0].name, "pl011", sizeof(console_ptr[0].name));
  285. /* Update checksum */
  286. checksum += console_ptr[0].base + console_ptr[0].map_pages +
  287. console_ptr[0].clk_in_hz + console_ptr[0].baud_rate;
  288. /* Checksum must be 0 */
  289. manifest->plat_console.checksum = ~checksum + 1UL;
  290. return 0;
  291. }
  292. #endif /* ENABLE_RME */