xref: /optee_os/core/arch/riscv/kernel/boot.c (revision fe1244f1057b4e81ed3c8fe4067d6473bdc37da1)
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 static void init_runtime(void)
84 {
85 	malloc_add_pool(__heap1_start, __heap1_end - __heap1_start);
86 
87 	IMSG_RAW("\n");
88 }
89 
90 void init_tee_runtime(void)
91 {
92 	core_mmu_init_phys_mem();
93 	call_preinitcalls();
94 	call_early_initcalls();
95 	call_service_initcalls();
96 }
97 
98 static void init_primary(unsigned long nsec_entry)
99 {
100 	thread_init_core_local_stacks();
101 
102 	/*
103 	 * Mask asynchronous exceptions before switch to the thread vector
104 	 * as the thread handler requires those to be masked while
105 	 * executing with the temporary stack. The thread subsystem also
106 	 * asserts that the foreign interrupts are blocked when using most of
107 	 * its functions.
108 	 */
109 	thread_set_exceptions(THREAD_EXCP_ALL);
110 
111 	init_runtime();
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 	call_driver_initcalls();
160 	call_finalcalls();
161 	IMSG("Primary CPU initialized");
162 
163 #ifdef CFG_RISCV_S_MODE
164 	start_secondary_cores();
165 #endif
166 }
167 
168 static void init_secondary_helper(unsigned long nsec_entry)
169 {
170 	size_t pos = get_core_pos();
171 
172 	IMSG("Secondary CPU %zu initializing", pos);
173 
174 	/*
175 	 * Mask asynchronous exceptions before switch to the thread vector
176 	 * as the thread handler requires those to be masked while
177 	 * executing with the temporary stack. The thread subsystem also
178 	 * asserts that the foreign interrupts are blocked when using most of
179 	 * its functions.
180 	 */
181 	thread_set_exceptions(THREAD_EXCP_ALL);
182 
183 	thread_init_per_cpu();
184 	init_sec_mon(nsec_entry);
185 	boot_secondary_init_intc();
186 
187 	IMSG("Secondary CPU %zu initialized", pos);
188 }
189 
190 void boot_init_secondary(unsigned long nsec_entry __unused)
191 {
192 	init_secondary_helper(PADDR_INVALID);
193 }
194