qemu_spm.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* SPDX-License-Identifier: BSD-3-Clause
  2. *
  3. * Copyright (c) 2020, Linaro Limited and Contributors. All rights reserved.
  4. */
  5. #include <libfdt.h>
  6. #include <bl31/ehf.h>
  7. #include <common/debug.h>
  8. #include <common/fdt_fixup.h>
  9. #include <common/fdt_wrappers.h>
  10. #include <lib/xlat_tables/xlat_tables_compat.h>
  11. #include <services/spm_mm_partition.h>
  12. #include <platform_def.h>
  13. /* Region equivalent to MAP_DEVICE1 suitable for mapping at EL0 */
  14. #define MAP_DEVICE1_EL0 MAP_REGION_FLAT(DEVICE1_BASE, \
  15. DEVICE1_SIZE, \
  16. MT_DEVICE | MT_RW | MT_SECURE | MT_USER)
  17. mmap_region_t plat_qemu_secure_partition_mmap[] = {
  18. QEMU_SP_IMAGE_NS_BUF_MMAP, /* must be placed at first entry */
  19. MAP_DEVICE1_EL0, /* for the UART */
  20. QEMU_SP_IMAGE_MMAP,
  21. QEMU_SPM_BUF_EL0_MMAP,
  22. QEMU_SP_IMAGE_RW_MMAP,
  23. MAP_SECURE_VARSTORE,
  24. {0}
  25. };
  26. /* Boot information passed to a secure partition during initialisation. */
  27. static spm_mm_mp_info_t sp_mp_info[PLATFORM_CORE_COUNT];
  28. spm_mm_boot_info_t plat_qemu_secure_partition_boot_info = {
  29. .h.type = PARAM_SP_IMAGE_BOOT_INFO,
  30. .h.version = VERSION_1,
  31. .h.size = sizeof(spm_mm_boot_info_t),
  32. .h.attr = 0,
  33. .sp_mem_base = PLAT_QEMU_SP_IMAGE_BASE,
  34. .sp_mem_limit = BL32_LIMIT,
  35. .sp_image_base = PLAT_QEMU_SP_IMAGE_BASE,
  36. .sp_stack_base = PLAT_SP_IMAGE_STACK_BASE,
  37. .sp_heap_base = PLAT_QEMU_SP_IMAGE_HEAP_BASE,
  38. .sp_ns_comm_buf_base = PLAT_QEMU_SP_IMAGE_NS_BUF_BASE,
  39. .sp_shared_buf_base = PLAT_SPM_BUF_BASE,
  40. .sp_image_size = PLAT_QEMU_SP_IMAGE_SIZE,
  41. .sp_pcpu_stack_size = PLAT_SP_IMAGE_STACK_PCPU_SIZE,
  42. .sp_heap_size = PLAT_QEMU_SP_IMAGE_HEAP_SIZE,
  43. .sp_ns_comm_buf_size = PLAT_QEMU_SP_IMAGE_NS_BUF_SIZE,
  44. .sp_shared_buf_size = PLAT_SPM_BUF_SIZE,
  45. .num_sp_mem_regions = PLAT_QEMU_SP_IMAGE_NUM_MEM_REGIONS,
  46. .num_cpus = PLATFORM_CORE_COUNT,
  47. .mp_info = sp_mp_info
  48. };
  49. /* Enumeration of priority levels on QEMU platforms. */
  50. ehf_pri_desc_t qemu_exceptions[] = {
  51. EHF_PRI_DESC(QEMU_PRI_BITS, PLAT_SP_PRI)
  52. };
  53. static void qemu_initialize_mp_info(spm_mm_mp_info_t *mp_info)
  54. {
  55. unsigned int i, j;
  56. spm_mm_mp_info_t *tmp = mp_info;
  57. for (i = 0; i < PLATFORM_CLUSTER_COUNT; i++) {
  58. for (j = 0; j < PLATFORM_MAX_CPUS_PER_CLUSTER; j++) {
  59. tmp->mpidr = (0x80000000 | (i << MPIDR_AFF1_SHIFT)) + j;
  60. /*
  61. * Linear indices and flags will be filled
  62. * in the spm_mm service.
  63. */
  64. tmp->linear_id = 0;
  65. tmp->flags = 0;
  66. tmp++;
  67. }
  68. }
  69. }
  70. int dt_add_ns_buf_node(uintptr_t *base)
  71. {
  72. uintptr_t addr;
  73. size_t size;
  74. uintptr_t ns_buf_addr;
  75. int node;
  76. int err;
  77. void *fdt = (void *)ARM_PRELOADED_DTB_BASE;
  78. err = fdt_open_into(fdt, fdt, PLAT_QEMU_DT_MAX_SIZE);
  79. if (err < 0) {
  80. ERROR("Invalid Device Tree at %p: error %d\n", fdt, err);
  81. return err;
  82. }
  83. /*
  84. * reserved-memory for standaloneMM non-secure buffer
  85. * is allocated at the top of the first system memory region.
  86. */
  87. node = fdt_path_offset(fdt, "/memory");
  88. err = fdt_get_reg_props_by_index(fdt, node, 0, &addr, &size);
  89. if (err < 0) {
  90. ERROR("Failed to get the memory node information\n");
  91. return err;
  92. }
  93. INFO("System RAM @ 0x%lx - 0x%lx\n", addr, addr + size - 1);
  94. ns_buf_addr = addr + (size - PLAT_QEMU_SP_IMAGE_NS_BUF_SIZE);
  95. INFO("reserved-memory for spm-mm @ 0x%lx - 0x%llx\n", ns_buf_addr,
  96. ns_buf_addr + PLAT_QEMU_SP_IMAGE_NS_BUF_SIZE - 1);
  97. err = fdt_add_reserved_memory(fdt, "ns-buf-spm-mm", ns_buf_addr,
  98. PLAT_QEMU_SP_IMAGE_NS_BUF_SIZE);
  99. if (err < 0) {
  100. ERROR("Failed to add the reserved-memory node\n");
  101. return err;
  102. }
  103. *base = ns_buf_addr;
  104. return 0;
  105. }
  106. /* Plug in QEMU exceptions to Exception Handling Framework. */
  107. EHF_REGISTER_PRIORITIES(qemu_exceptions, ARRAY_SIZE(qemu_exceptions),
  108. QEMU_PRI_BITS);
  109. const mmap_region_t *plat_get_secure_partition_mmap(void *cookie)
  110. {
  111. uintptr_t ns_buf_base;
  112. dt_add_ns_buf_node(&ns_buf_base);
  113. plat_qemu_secure_partition_mmap[0].base_pa = ns_buf_base;
  114. plat_qemu_secure_partition_mmap[0].base_va = ns_buf_base;
  115. plat_qemu_secure_partition_boot_info.sp_ns_comm_buf_base = ns_buf_base;
  116. return plat_qemu_secure_partition_mmap;
  117. }
  118. const spm_mm_boot_info_t *
  119. plat_get_secure_partition_boot_info(void *cookie)
  120. {
  121. qemu_initialize_mp_info(sp_mp_info);
  122. return &plat_qemu_secure_partition_boot_info;
  123. }