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 <mm/core_memprot.h> 19 #include <mm/core_mmu.h> 20 #include <mm/tee_mm.h> 21 #include <mm/tee_pager.h> 22 #include <platform_config.h> 23 #include <riscv.h> 24 #include <sbi.h> 25 #include <stdio.h> 26 #include <trace.h> 27 #include <util.h> 28 29 #define PADDR_INVALID ULONG_MAX 30 31 paddr_t start_addr; 32 unsigned long boot_args[4]; 33 34 uint32_t sem_cpu_sync[CFG_TEE_CORE_NB_CORE]; 35 36 #if defined(CFG_DT) 37 static int mark_tddram_as_reserved(struct dt_descriptor *dt) 38 { 39 return add_res_mem_dt_node(dt, "optee_core", CFG_TDDRAM_START, 40 CFG_TDDRAM_SIZE); 41 } 42 43 static void update_external_dt(void) 44 { 45 struct dt_descriptor *dt = get_external_dt_desc(); 46 47 if (!dt || !dt->blob) 48 return; 49 50 #ifdef CFG_CORE_RESERVED_SHM 51 if (mark_static_shm_as_reserved(dt)) 52 panic("Failed to config non-secure memory"); 53 #endif 54 55 if (mark_tddram_as_reserved(dt)) 56 panic("Failed to config secure memory"); 57 } 58 #else /*CFG_DT*/ 59 static void update_external_dt(void) 60 { 61 } 62 #endif /*!CFG_DT*/ 63 64 void init_sec_mon(unsigned long nsec_entry __maybe_unused) 65 { 66 assert(nsec_entry == PADDR_INVALID); 67 /* Do nothing as we don't have a secure monitor */ 68 } 69 70 #ifdef CFG_RISCV_S_MODE 71 static void start_secondary_cores(void) 72 { 73 size_t i = 0; 74 size_t pos = get_core_pos(); 75 76 for (i = 0; i < CFG_TEE_CORE_NB_CORE; i++) 77 if (i != pos && IS_ENABLED(CFG_RISCV_SBI) && 78 sbi_hsm_hart_start(i, start_addr, i)) 79 EMSG("Error starting secondary hart %zu", i); 80 } 81 #endif 82 83 void init_tee_runtime(void) 84 { 85 call_preinitcalls(); 86 call_early_initcalls(); 87 call_service_initcalls(); 88 } 89 90 static void init_primary(unsigned long nsec_entry) 91 { 92 vaddr_t va __maybe_unused = 0; 93 94 thread_init_core_local_stacks(); 95 96 /* 97 * Mask asynchronous exceptions before switch to the thread vector 98 * as the thread handler requires those to be masked while 99 * executing with the temporary stack. The thread subsystem also 100 * asserts that the foreign interrupts are blocked when using most of 101 * its functions. 102 */ 103 thread_set_exceptions(THREAD_EXCP_ALL); 104 105 malloc_add_pool(__heap1_start, __heap1_end - __heap1_start); 106 IMSG_RAW("\n"); 107 108 core_mmu_save_mem_map(); 109 core_mmu_init_phys_mem(); 110 va = boot_mem_release_unused(); 111 112 thread_init_boot_thread(); 113 thread_init_primary(); 114 thread_init_per_cpu(); 115 init_sec_mon(nsec_entry); 116 } 117 118 /* May be overridden in plat-$(PLATFORM)/main.c */ 119 __weak void plat_primary_init_early(void) 120 { 121 } 122 123 /* May be overridden in plat-$(PLATFORM)/main.c */ 124 __weak void boot_primary_init_intc(void) 125 { 126 } 127 128 /* May be overridden in plat-$(PLATFORM)/main.c */ 129 __weak void boot_secondary_init_intc(void) 130 { 131 } 132 133 void boot_init_primary_early(void) 134 { 135 unsigned long e = PADDR_INVALID; 136 137 init_primary(e); 138 } 139 140 void boot_init_primary_late(unsigned long fdt, 141 unsigned long tos_fw_config __unused) 142 { 143 init_external_dt(fdt, CFG_DTB_MAX_SIZE); 144 discover_nsec_memory(); 145 update_external_dt(); 146 147 IMSG("OP-TEE version: %s", core_v_str); 148 if (IS_ENABLED(CFG_INSECURE)) { 149 IMSG("WARNING: This OP-TEE configuration might be insecure!"); 150 IMSG("WARNING: Please check https://optee.readthedocs.io/en/latest/architecture/porting_guidelines.html"); 151 } 152 IMSG("Primary CPU initializing"); 153 boot_primary_init_intc(); 154 init_tee_runtime(); 155 } 156 157 void __weak boot_init_primary_final(void) 158 { 159 boot_mem_release_tmp_alloc(); 160 161 call_driver_initcalls(); 162 call_finalcalls(); 163 IMSG("Primary CPU initialized"); 164 165 #ifdef CFG_RISCV_S_MODE 166 start_secondary_cores(); 167 #endif 168 } 169 170 static void init_secondary_helper(unsigned long nsec_entry) 171 { 172 size_t pos = get_core_pos(); 173 174 IMSG("Secondary CPU %zu initializing", pos); 175 176 /* 177 * Mask asynchronous exceptions before switch to the thread vector 178 * as the thread handler requires those to be masked while 179 * executing with the temporary stack. The thread subsystem also 180 * asserts that the foreign interrupts are blocked when using most of 181 * its functions. 182 */ 183 thread_set_exceptions(THREAD_EXCP_ALL); 184 185 thread_init_per_cpu(); 186 init_sec_mon(nsec_entry); 187 boot_secondary_init_intc(); 188 189 IMSG("Secondary CPU %zu initialized", pos); 190 } 191 192 void boot_init_secondary(unsigned long nsec_entry __unused) 193 { 194 init_secondary_helper(PADDR_INVALID); 195 } 196