1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2023 Andes Technology Corporation 4 * Copyright 2022-2023 NXP 5 */ 6 7 #include <assert.h> 8 #include <compiler.h> 9 #include <config.h> 10 #include <console.h> 11 #include <keep.h> 12 #include <kernel/boot.h> 13 #include <kernel/dt.h> 14 #include <kernel/linker.h> 15 #include <kernel/misc.h> 16 #include <kernel/panic.h> 17 #include <kernel/thread.h> 18 #include <libfdt.h> 19 #include <mm/core_memprot.h> 20 #include <mm/core_mmu.h> 21 #include <mm/tee_mm.h> 22 #include <mm/tee_pager.h> 23 #include <platform_config.h> 24 #include <riscv.h> 25 #include <sbi.h> 26 #include <stdio.h> 27 #include <trace.h> 28 #include <util.h> 29 30 #define PADDR_INVALID ULONG_MAX 31 32 paddr_t start_addr; 33 34 uint32_t sem_cpu_sync[CFG_TEE_CORE_NB_CORE]; 35 uint32_t hartids[CFG_TEE_CORE_NB_CORE]; 36 37 #if defined(CFG_DT) 38 static int mark_tddram_as_reserved(struct dt_descriptor *dt) 39 { 40 return add_res_mem_dt_node(dt, "optee_core", CFG_TDDRAM_START, 41 CFG_TDDRAM_SIZE); 42 } 43 44 static void update_external_dt(void) 45 { 46 struct dt_descriptor *dt = get_external_dt_desc(); 47 48 if (!dt || !dt->blob) 49 return; 50 51 #ifdef CFG_CORE_RESERVED_SHM 52 if (mark_static_shm_as_reserved(dt)) 53 panic("Failed to config non-secure memory"); 54 #endif 55 56 if (mark_tddram_as_reserved(dt)) 57 panic("Failed to config secure memory"); 58 } 59 #else /*CFG_DT*/ 60 static void update_external_dt(void) 61 { 62 } 63 #endif /*!CFG_DT*/ 64 65 #ifdef CFG_RISCV_S_MODE 66 static void start_secondary_cores(void) 67 { 68 uint32_t curr_hartid = thread_get_core_local()->hart_id; 69 enum sbi_hsm_hart_state status = 0; 70 uint32_t hartid = 0; 71 int rc = 0; 72 int i = 0; 73 74 /* The primary CPU is always indexed by 0 */ 75 assert(get_core_pos() == 0); 76 77 for (i = 0; i < CFG_TEE_CORE_NB_CORE; i++) { 78 hartid = hartids[i]; 79 80 if (hartid == curr_hartid) 81 continue; 82 83 rc = sbi_hsm_hart_get_status(hartid, &status); 84 /* 85 * Skip if the hartid is not an assigned hart 86 * of the trusted domain, or its HSM state is 87 * not stopped. 88 */ 89 if (rc || status != SBI_HSM_STATE_STOPPED) 90 continue; 91 92 DMSG("Bringing up secondary hart%"PRIu32, hartid); 93 94 rc = sbi_hsm_hart_start(hartid, start_addr, 0 /* unused */); 95 if (rc) { 96 EMSG("Error starting secondary hart%"PRIu32, hartid); 97 panic(); 98 } 99 } 100 } 101 #endif 102 103 void init_tee_runtime(void) 104 { 105 call_preinitcalls(); 106 call_early_initcalls(); 107 call_service_initcalls(); 108 109 /* Reinitialize canaries around the stacks with crypto_rng_read(). */ 110 thread_update_canaries(); 111 } 112 113 static bool add_padding_to_pool(vaddr_t va, size_t len, void *ptr __unused) 114 { 115 malloc_add_pool((void *)va, len); 116 return true; 117 } 118 119 static void init_primary(void) 120 { 121 vaddr_t va __maybe_unused = 0; 122 123 thread_init_core_local_stacks(); 124 125 /* 126 * Mask asynchronous exceptions before switch to the thread vector 127 * as the thread handler requires those to be masked while 128 * executing with the temporary stack. The thread subsystem also 129 * asserts that the foreign interrupts are blocked when using most of 130 * its functions. 131 */ 132 thread_set_exceptions(THREAD_EXCP_ALL); 133 134 malloc_add_pool(__heap1_start, __heap1_end - __heap1_start); 135 IMSG_RAW("\n"); 136 137 core_mmu_save_mem_map(); 138 core_mmu_init_phys_mem(); 139 boot_mem_foreach_padding(add_padding_to_pool, NULL); 140 va = boot_mem_release_unused(); 141 142 thread_init_threads(CFG_NUM_THREADS); 143 thread_init_boot_thread(); 144 thread_init_primary(); 145 thread_init_per_cpu(); 146 } 147 148 /* May be overridden in plat-$(PLATFORM)/main.c */ 149 __weak void plat_primary_init_early(void) 150 { 151 } 152 153 /* May be overridden in plat-$(PLATFORM)/main.c */ 154 __weak void boot_primary_init_intc(void) 155 { 156 } 157 158 /* May be overridden in plat-$(PLATFORM)/main.c */ 159 __weak void boot_primary_init_core_ids(void) 160 { 161 #ifdef CFG_DT 162 const void *fdt = get_external_dt(); 163 const fdt32_t *reg = NULL; 164 int cpu_offset = 0; 165 int offset = 0; 166 int len = 0; 167 int i = 0; 168 169 offset = fdt_path_offset(fdt, "/cpus"); 170 if (offset < 0) 171 panic("Failed to find /cpus node in the device tree"); 172 173 fdt_for_each_subnode(cpu_offset, fdt, offset) { 174 /* 175 * Assume all TEE cores are enabled. The "reg" 176 * property in the CPU node indicates the hart ID. 177 */ 178 if (fdt_get_status(fdt, cpu_offset) == DT_STATUS_DISABLED) 179 continue; 180 181 reg = fdt_getprop(fdt, cpu_offset, "reg", &len); 182 if (!reg) { 183 EMSG("CPU node does not have 'reg' property"); 184 continue; 185 } 186 187 assert(i < CFG_TEE_CORE_NB_CORE); 188 hartids[i++] = fdt32_to_cpu(*reg); 189 } 190 191 assert(i == CFG_TEE_CORE_NB_CORE); 192 #endif 193 } 194 195 /* May be overridden in plat-$(PLATFORM)/main.c */ 196 __weak void boot_secondary_init_intc(void) 197 { 198 } 199 200 void boot_init_primary_early(void) 201 { 202 init_primary(); 203 } 204 205 void boot_init_primary_late(unsigned long fdt, 206 unsigned long tos_fw_config __unused) 207 { 208 size_t pos = get_core_pos(); 209 210 /* The primary CPU is always indexed by 0 */ 211 assert(pos == 0); 212 213 init_external_dt(fdt, CFG_DTB_MAX_SIZE); 214 discover_nsec_memory(); 215 update_external_dt(); 216 217 IMSG("OP-TEE version: %s", core_v_str); 218 if (IS_ENABLED(CFG_INSECURE)) { 219 IMSG("WARNING: This OP-TEE configuration might be insecure!"); 220 IMSG("WARNING: Please check https://optee.readthedocs.io/en/latest/architecture/porting_guidelines.html"); 221 } 222 IMSG("Primary CPU0 (hart%"PRIu32") initializing", 223 thread_get_hartid_by_hartindex(pos)); 224 boot_primary_init_intc(); 225 boot_primary_init_core_ids(); 226 init_tee_runtime(); 227 } 228 229 void __weak boot_init_primary_final(void) 230 { 231 size_t pos = get_core_pos(); 232 233 boot_mem_release_tmp_alloc(); 234 235 call_driver_initcalls(); 236 call_finalcalls(); 237 IMSG("Primary CPU0 (hart%"PRIu32") initialized", 238 thread_get_hartid_by_hartindex(pos)); 239 240 #ifdef CFG_RISCV_S_MODE 241 start_secondary_cores(); 242 #endif 243 } 244 245 static void init_secondary_helper(void) 246 { 247 size_t pos = get_core_pos(); 248 249 IMSG("Secondary CPU%zu (hart%"PRIu32") initializing", 250 pos, thread_get_hartid_by_hartindex(pos)); 251 252 /* 253 * Mask asynchronous exceptions before switch to the thread vector 254 * as the thread handler requires those to be masked while 255 * executing with the temporary stack. The thread subsystem also 256 * asserts that the foreign interrupts are blocked when using most of 257 * its functions. 258 */ 259 thread_set_exceptions(THREAD_EXCP_ALL); 260 261 thread_init_per_cpu(); 262 boot_secondary_init_intc(); 263 264 IMSG("Secondary CPU%zu (hart%"PRIu32") initialized", 265 pos, thread_get_hartid_by_hartindex(pos)); 266 } 267 268 void boot_init_secondary(unsigned long nsec_entry __unused) 269 { 270 init_secondary_helper(); 271 } 272