xref: /optee_os/core/arch/riscv/kernel/boot.c (revision 89da7ffe58b51e694eef722a0db34b19531ef770)
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 void init_sec_mon(unsigned long nsec_entry __maybe_unused)
66 {
67 	assert(nsec_entry == PADDR_INVALID);
68 	/* Do nothing as we don't have a secure monitor */
69 }
70 
71 #ifdef CFG_RISCV_S_MODE
72 static void start_secondary_cores(void)
73 {
74 	uint32_t curr_hartid = thread_get_core_local()->hart_id;
75 	enum sbi_hsm_hart_state status = 0;
76 	uint32_t hartid = 0;
77 	int rc = 0;
78 	int i = 0;
79 
80 	/* The primary CPU is always indexed by 0 */
81 	assert(get_core_pos() == 0);
82 
83 	for (i = 0; i < CFG_TEE_CORE_NB_CORE; i++) {
84 		hartid = hartids[i];
85 
86 		if (hartid == curr_hartid)
87 			continue;
88 
89 		rc = sbi_hsm_hart_get_status(hartid, &status);
90 		/*
91 		 * Skip if the hartid is not an assigned hart
92 		 * of the trusted domain, or its HSM state is
93 		 * not stopped.
94 		 */
95 		if (rc || status != SBI_HSM_STATE_STOPPED)
96 			continue;
97 
98 		DMSG("Bringing up secondary hart%"PRIu32, hartid);
99 
100 		rc = sbi_hsm_hart_start(hartid, start_addr, 0 /* unused */);
101 		if (rc) {
102 			EMSG("Error starting secondary hart%"PRIu32, hartid);
103 			panic();
104 		}
105 	}
106 }
107 #endif
108 
109 void init_tee_runtime(void)
110 {
111 	call_preinitcalls();
112 	call_early_initcalls();
113 	call_service_initcalls();
114 
115 	/* Reinitialize canaries around the stacks with crypto_rng_read(). */
116 	thread_update_canaries();
117 }
118 
119 static bool add_padding_to_pool(vaddr_t va, size_t len, void *ptr __unused)
120 {
121 	malloc_add_pool((void *)va, len);
122 	return true;
123 }
124 
125 static void init_primary(unsigned long nsec_entry)
126 {
127 	vaddr_t va __maybe_unused = 0;
128 
129 	thread_init_core_local_stacks();
130 
131 	/*
132 	 * Mask asynchronous exceptions before switch to the thread vector
133 	 * as the thread handler requires those to be masked while
134 	 * executing with the temporary stack. The thread subsystem also
135 	 * asserts that the foreign interrupts are blocked when using most of
136 	 * its functions.
137 	 */
138 	thread_set_exceptions(THREAD_EXCP_ALL);
139 
140 	malloc_add_pool(__heap1_start, __heap1_end - __heap1_start);
141 	IMSG_RAW("\n");
142 
143 	core_mmu_save_mem_map();
144 	core_mmu_init_phys_mem();
145 	boot_mem_foreach_padding(add_padding_to_pool, NULL);
146 	va = boot_mem_release_unused();
147 
148 	thread_init_boot_thread();
149 	thread_init_primary();
150 	thread_init_per_cpu();
151 	init_sec_mon(nsec_entry);
152 }
153 
154 /* May be overridden in plat-$(PLATFORM)/main.c */
155 __weak void plat_primary_init_early(void)
156 {
157 }
158 
159 /* May be overridden in plat-$(PLATFORM)/main.c */
160 __weak void boot_primary_init_intc(void)
161 {
162 }
163 
164 /* May be overridden in plat-$(PLATFORM)/main.c */
165 __weak void boot_primary_init_core_ids(void)
166 {
167 #ifdef CFG_DT
168 	const void *fdt = get_external_dt();
169 	const fdt32_t *reg = NULL;
170 	int cpu_offset = 0;
171 	int offset = 0;
172 	int len = 0;
173 	int i = 0;
174 
175 	offset = fdt_path_offset(fdt, "/cpus");
176 	if (offset < 0)
177 		panic("Failed to find /cpus node in the device tree");
178 
179 	fdt_for_each_subnode(cpu_offset, fdt, offset) {
180 		/*
181 		 * Assume all TEE cores are enabled. The "reg"
182 		 * property in the CPU node indicates the hart ID.
183 		 */
184 		if (fdt_get_status(fdt, cpu_offset) == DT_STATUS_DISABLED)
185 			continue;
186 
187 		reg = fdt_getprop(fdt, cpu_offset, "reg", &len);
188 		if (!reg) {
189 			EMSG("CPU node does not have 'reg' property");
190 			continue;
191 		}
192 
193 		assert(i < CFG_TEE_CORE_NB_CORE);
194 		hartids[i++] = fdt32_to_cpu(*reg);
195 	}
196 
197 	assert(i == CFG_TEE_CORE_NB_CORE);
198 #endif
199 }
200 
201 /* May be overridden in plat-$(PLATFORM)/main.c */
202 __weak void boot_secondary_init_intc(void)
203 {
204 }
205 
206 void boot_init_primary_early(void)
207 {
208 	unsigned long e = PADDR_INVALID;
209 
210 	init_primary(e);
211 }
212 
213 void boot_init_primary_late(unsigned long fdt,
214 			    unsigned long tos_fw_config __unused)
215 {
216 	size_t pos = get_core_pos();
217 
218 	/* The primary CPU is always indexed by 0 */
219 	assert(pos == 0);
220 
221 	init_external_dt(fdt, CFG_DTB_MAX_SIZE);
222 	discover_nsec_memory();
223 	update_external_dt();
224 
225 	IMSG("OP-TEE version: %s", core_v_str);
226 	if (IS_ENABLED(CFG_INSECURE)) {
227 		IMSG("WARNING: This OP-TEE configuration might be insecure!");
228 		IMSG("WARNING: Please check https://optee.readthedocs.io/en/latest/architecture/porting_guidelines.html");
229 	}
230 	IMSG("Primary CPU0 (hart%"PRIu32") initializing",
231 	     thread_get_hartid_by_hartindex(pos));
232 	boot_primary_init_intc();
233 	boot_primary_init_core_ids();
234 	init_tee_runtime();
235 }
236 
237 void __weak boot_init_primary_final(void)
238 {
239 	size_t pos = get_core_pos();
240 
241 	boot_mem_release_tmp_alloc();
242 
243 	call_driver_initcalls();
244 	call_finalcalls();
245 	IMSG("Primary CPU0 (hart%"PRIu32") initialized",
246 	     thread_get_hartid_by_hartindex(pos));
247 
248 #ifdef CFG_RISCV_S_MODE
249 	start_secondary_cores();
250 #endif
251 }
252 
253 static void init_secondary_helper(unsigned long nsec_entry)
254 {
255 	size_t pos = get_core_pos();
256 
257 	IMSG("Secondary CPU%zu (hart%"PRIu32") initializing",
258 	     pos, thread_get_hartid_by_hartindex(pos));
259 
260 	/*
261 	 * Mask asynchronous exceptions before switch to the thread vector
262 	 * as the thread handler requires those to be masked while
263 	 * executing with the temporary stack. The thread subsystem also
264 	 * asserts that the foreign interrupts are blocked when using most of
265 	 * its functions.
266 	 */
267 	thread_set_exceptions(THREAD_EXCP_ALL);
268 
269 	thread_init_per_cpu();
270 	init_sec_mon(nsec_entry);
271 	boot_secondary_init_intc();
272 
273 	IMSG("Secondary CPU%zu (hart%"PRIu32") initialized",
274 	     pos, thread_get_hartid_by_hartindex(pos));
275 }
276 
277 void boot_init_secondary(unsigned long nsec_entry __unused)
278 {
279 	init_secondary_helper(PADDR_INVALID);
280 }
281