Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | */ |
| 5 | |
Mate Toth-Pal | 0da5811 | 2024-01-10 11:49:58 +0100 | [diff] [blame^] | 6 | #ifndef CBMC |
| 7 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 8 | #include <arch_helpers.h> |
| 9 | #include <assert.h> |
| 10 | #include <buffer.h> |
| 11 | #include <cpuid.h> |
| 12 | #include <debug.h> |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 13 | #include <errno.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 14 | #include <gic.h> |
Soby Mathew | 26727ea | 2023-03-06 13:26:50 +0000 | [diff] [blame] | 15 | #include <plat_cmn_arch.h> |
AlexeiFedorov | ee2fc82 | 2023-10-31 14:54:39 +0000 | [diff] [blame] | 16 | #include <plat_common.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 17 | #include <rmm_el3_ifc.h> |
| 18 | #include <sizes.h> |
| 19 | #include <stdint.h> |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 20 | #include <string.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 21 | #include <xlat_contexts.h> |
Mate Toth-Pal | 7f5b27d | 2023-08-08 13:49:19 +0200 | [diff] [blame] | 22 | #include <xlat_high_va.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 23 | #include <xlat_tables.h> |
| 24 | |
Soby Mathew | 26727ea | 2023-03-06 13:26:50 +0000 | [diff] [blame] | 25 | |
| 26 | IMPORT_SYM(uintptr_t, rmm_text_start, RMM_CODE_START); |
| 27 | IMPORT_SYM(uintptr_t, rmm_text_end, RMM_CODE_END); |
| 28 | IMPORT_SYM(uintptr_t, rmm_ro_start, RMM_RO_START); |
| 29 | IMPORT_SYM(uintptr_t, rmm_ro_end, RMM_RO_END); |
| 30 | IMPORT_SYM(uintptr_t, rmm_rw_start, RMM_RW_START); |
| 31 | IMPORT_SYM(uintptr_t, rmm_rw_end, RMM_RW_END); |
| 32 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 33 | /* |
| 34 | * Memory map REGIONS used for the RMM runtime (static mappings) |
| 35 | */ |
| 36 | #define RMM_CODE_SIZE (RMM_CODE_END - RMM_CODE_START) |
| 37 | #define RMM_RO_SIZE (RMM_RO_END - RMM_RO_START) |
| 38 | #define RMM_RW_SIZE (RMM_RW_END - RMM_RW_START) |
| 39 | |
| 40 | #define RMM_CODE MAP_REGION_FLAT( \ |
| 41 | RMM_CODE_START, \ |
| 42 | RMM_CODE_SIZE, \ |
AlexeiFedorov | 3f5d627 | 2023-10-23 16:27:37 +0100 | [diff] [blame] | 43 | (MT_CODE | MT_REALM)) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 44 | |
| 45 | #define RMM_RO MAP_REGION_FLAT( \ |
| 46 | RMM_RO_START, \ |
| 47 | RMM_RO_SIZE, \ |
AlexeiFedorov | 3f5d627 | 2023-10-23 16:27:37 +0100 | [diff] [blame] | 48 | (MT_RO_DATA | MT_REALM)) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 49 | |
| 50 | #define RMM_RW MAP_REGION_FLAT( \ |
| 51 | RMM_RW_START, \ |
| 52 | RMM_RW_SIZE, \ |
AlexeiFedorov | 3f5d627 | 2023-10-23 16:27:37 +0100 | [diff] [blame] | 53 | (MT_RW_DATA | MT_REALM)) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 54 | |
| 55 | /* |
Soby Mathew | 26727ea | 2023-03-06 13:26:50 +0000 | [diff] [blame] | 56 | * Leave an invalid page between the end of RMM memory and the beginning |
| 57 | * of the shared buffer VA. This will help to detect any memory access |
| 58 | * underflow by RMM. |
| 59 | */ |
| 60 | #define RMM_SHARED_BUFFER_START (RMM_RW_END + SZ_4K) |
| 61 | |
| 62 | /* |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 63 | * Some of the fields for the RMM_SHARED region will be populated |
| 64 | * at runtime. |
| 65 | */ |
| 66 | #define RMM_SHARED MAP_REGION( \ |
| 67 | 0U, \ |
| 68 | RMM_SHARED_BUFFER_START, \ |
| 69 | 0U, \ |
AlexeiFedorov | 3f5d627 | 2023-10-23 16:27:37 +0100 | [diff] [blame] | 70 | (MT_RW_DATA | MT_REALM)) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 71 | |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 72 | /* Number of common memory mapping regions */ |
| 73 | #define COMMON_REGIONS (4U) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 74 | |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 75 | /* Total number of memory mapping regions */ |
| 76 | #define TOTAL_MMAP_REGIONS (COMMON_REGIONS + PLAT_CMN_EXTRA_MMAP_REGIONS) |
| 77 | |
| 78 | /* Memory mapping regions for the system runtime */ |
| 79 | static struct xlat_mmap_region static_regions[TOTAL_MMAP_REGIONS]; |
| 80 | |
Javier Almansa Sobrino | 765a316 | 2023-04-27 17:42:58 +0100 | [diff] [blame] | 81 | /* |
| 82 | * Allocate the runtime translation tables. |
| 83 | * Although a base table at level -1 when FEAT_LPA2 is enabled only has |
| 84 | * 16 entries, all tables are allocated a page for simplicity. |
| 85 | */ |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 86 | static uint64_t static_s1tt[XLAT_TABLE_ENTRIES * PLAT_CMN_CTX_MAX_XLAT_TABLES] |
| 87 | __aligned(XLAT_TABLES_ALIGNMENT) |
| 88 | __section("xlat_static_tables"); |
| 89 | |
| 90 | /* Structures to hold the runtime translation context information */ |
| 91 | static struct xlat_ctx_tbls runtime_tbls; |
| 92 | static struct xlat_ctx_cfg runtime_xlat_ctx_cfg; |
| 93 | static struct xlat_ctx runtime_xlat_ctx; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * Platform common cold boot setup for RMM. |
| 97 | * |
| 98 | * This function should only be invoked once during cold boot |
| 99 | * and is expected to setup architecture and platform components |
Soby Mathew | 26727ea | 2023-03-06 13:26:50 +0000 | [diff] [blame] | 100 | * common for all PEs executing RMM. The rmm_el3_ifc, the xlat tables |
| 101 | * and GIC driver are initialized by this function. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 102 | */ |
| 103 | int plat_cmn_setup(unsigned long x0, unsigned long x1, |
| 104 | unsigned long x2, unsigned long x3, |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 105 | struct xlat_mmap_region *plat_regions, |
| 106 | unsigned int nregions) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 107 | { |
| 108 | int ret; |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 109 | unsigned int plat_offset, cmn_offset; |
| 110 | |
| 111 | /* Common regions sorted by ascending VA */ |
| 112 | struct xlat_mmap_region regions[COMMON_REGIONS] = { |
| 113 | RMM_CODE, |
| 114 | RMM_RO, |
| 115 | RMM_RW, |
| 116 | RMM_SHARED |
| 117 | }; |
| 118 | |
| 119 | if (nregions > PLAT_CMN_EXTRA_MMAP_REGIONS) { |
| 120 | return -ERANGE; |
| 121 | } |
| 122 | |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 123 | if ((nregions != 0U) && (plat_regions == NULL)) { |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 124 | return -EINVAL; |
| 125 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 126 | |
Soby Mathew | 26727ea | 2023-03-06 13:26:50 +0000 | [diff] [blame] | 127 | /* Initialize the RMM <-> EL3 interface */ |
| 128 | ret = rmm_el3_ifc_init(x0, x1, x2, x3, get_shared_buf_va(x3)); |
| 129 | if (ret != 0) { |
| 130 | ERROR("%s (%u): Failed to initialize the RMM EL3 Interface\n", |
| 131 | __func__, __LINE__); |
| 132 | return ret; |
| 133 | } |
| 134 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 135 | /* Setup the parameters of the shared area */ |
Soby Mathew | 26727ea | 2023-03-06 13:26:50 +0000 | [diff] [blame] | 136 | regions[3].base_pa = get_shared_buf_pa(); |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 137 | regions[3].size = rmm_el3_ifc_get_shared_buf_size(); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 138 | |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 139 | plat_offset = COMMON_REGIONS; |
| 140 | cmn_offset = 0U; |
| 141 | if (nregions > 0U) { |
| 142 | /* |
| 143 | * Combine the common memory regions with the platform ones |
| 144 | * in an array where they are sorted as per VA. |
| 145 | */ |
| 146 | if (plat_regions[0].base_va < RMM_CODE_START) { |
| 147 | plat_offset = 0U; |
| 148 | cmn_offset = nregions; |
| 149 | } |
| 150 | (void)memcpy((void *)&static_regions[plat_offset], |
| 151 | (void *)&plat_regions[0U], |
| 152 | sizeof(struct xlat_mmap_region) * nregions); |
| 153 | } |
| 154 | |
| 155 | (void)memcpy((void *)&static_regions[cmn_offset], (void *)®ions[0U], |
| 156 | sizeof(struct xlat_mmap_region) * COMMON_REGIONS); |
| 157 | |
| 158 | ret = xlat_ctx_cfg_init(&runtime_xlat_ctx_cfg, VA_LOW_REGION, |
| 159 | &static_regions[0], nregions + COMMON_REGIONS, |
| 160 | VIRT_ADDR_SPACE_SIZE); |
| 161 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 162 | if (ret != 0) { |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 163 | ERROR("%s (%u): %s (%i)\n", |
| 164 | __func__, __LINE__, |
| 165 | "Failed to initialize the xlat ctx within the xlat library ", |
| 166 | ret); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 167 | return ret; |
| 168 | } |
| 169 | |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 170 | ret = xlat_ctx_init(&runtime_xlat_ctx, &runtime_xlat_ctx_cfg, |
| 171 | &runtime_tbls, |
| 172 | &static_s1tt[0], |
| 173 | PLAT_CMN_CTX_MAX_XLAT_TABLES); |
| 174 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 175 | if (ret != 0) { |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame] | 176 | ERROR("%s (%u): %s (%i)\n", |
| 177 | __func__, __LINE__, |
| 178 | "Failed to create the xlat ctx within the xlat library ", |
| 179 | ret); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | /* Read supported GIC virtualization features and init GIC variables */ |
| 184 | gic_get_virt_features(); |
| 185 | |
Mate Toth-Pal | 7f5b27d | 2023-08-08 13:49:19 +0200 | [diff] [blame] | 186 | return 0; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Local PE common platform setup for RMM. |
| 191 | * |
| 192 | * This function will only be invoked during |
| 193 | * warm boot and is expected to setup architecture and platform |
| 194 | * components local to a PE executing RMM. |
| 195 | */ |
| 196 | int plat_cmn_warmboot_setup(void) |
| 197 | { |
| 198 | int ret; |
| 199 | |
AlexeiFedorov | 7c5001a | 2022-12-14 13:22:33 +0000 | [diff] [blame] | 200 | /* Setup the MMU cfg for the low region (runtime context) */ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 201 | ret = xlat_arch_setup_mmu_cfg(&runtime_xlat_ctx); |
| 202 | if (ret != 0) { |
| 203 | ERROR("%s (%u): Failed to setup xlat tables for CPU[%u]\n", |
AlexeiFedorov | 7c5001a | 2022-12-14 13:22:33 +0000 | [diff] [blame] | 204 | __func__, __LINE__, my_cpuid()); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 205 | return ret; |
| 206 | } |
| 207 | |
Mate Toth-Pal | 7f5b27d | 2023-08-08 13:49:19 +0200 | [diff] [blame] | 208 | /* Perform warm boot initialization of the high VA region */ |
| 209 | ret = xlat_high_va_setup(); |
| 210 | if (ret != 0) { |
| 211 | ERROR("%s (%u): Failed to setup high VA for CPU[%u]\n", |
| 212 | __func__, __LINE__, my_cpuid()); |
| 213 | return ret; |
| 214 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 215 | |
| 216 | VERBOSE("xlat tables configured for CPU[%u]\n", my_cpuid()); |
| 217 | return 0; |
| 218 | } |
Mate Toth-Pal | 0da5811 | 2024-01-10 11:49:58 +0100 | [diff] [blame^] | 219 | |
| 220 | #endif /* CBMC */ |