xref: /optee_os/core/arch/arm/plat-vexpress/main.c (revision 19a31ec40245ae01a9adcd206eec2a4bb4479fc9)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2016-2023, Linaro Limited
4  * Copyright (c) 2014, STMicroelectronics International N.V.
5  */
6 
7 #include <arm.h>
8 #include <config.h>
9 #include <console.h>
10 #include <drivers/gic.h>
11 #include <drivers/hfic.h>
12 #include <drivers/pl011.h>
13 #include <drivers/tzc400.h>
14 #include <initcall.h>
15 #include <keep.h>
16 #include <kernel/boot.h>
17 #include <kernel/interrupt.h>
18 #include <kernel/misc.h>
19 #include <kernel/notif.h>
20 #include <kernel/panic.h>
21 #include <kernel/thread_spmc.h>
22 #include <kernel/timer.h>
23 #include <mm/core_memprot.h>
24 #include <mm/core_mmu.h>
25 #include <platform_config.h>
26 #include <sm/psci.h>
27 #include <stdint.h>
28 #include <trace.h>
29 
30 static struct pl011_data console_data __nex_bss;
31 
32 register_phys_mem_pgdir(MEM_AREA_IO_SEC, CONSOLE_UART_BASE, PL011_REG_SIZE);
33 #if defined(PLATFORM_FLAVOR_fvp)
34 register_phys_mem(MEM_AREA_RAM_SEC, TZCDRAM_BASE, TZCDRAM_SIZE);
35 #endif
36 #if defined(PLATFORM_FLAVOR_qemu_virt)
37 register_phys_mem_pgdir(MEM_AREA_IO_SEC, SECRAM_BASE, SECRAM_COHERENT_SIZE);
38 #endif
39 #ifdef DRAM0_BASE
40 register_ddr(DRAM0_BASE, DRAM0_SIZE);
41 #endif
42 #ifdef DRAM1_BASE
43 register_ddr(DRAM1_BASE, DRAM1_SIZE);
44 #endif
45 
46 #ifdef CFG_GIC
47 register_phys_mem_pgdir(MEM_AREA_IO_SEC, GICC_BASE, GIC_CPU_REG_SIZE);
48 register_phys_mem_pgdir(MEM_AREA_IO_SEC, GICD_BASE, GIC_DIST_REG_SIZE);
49 #ifdef GIC_REDIST_BASE
50 register_phys_mem_pgdir(MEM_AREA_IO_SEC, GIC_REDIST_BASE, GIC_REDIST_SIZE);
51 #endif
52 
53 void boot_primary_init_intc(void)
54 {
55 #ifdef GIC_REDIST_BASE
56 	gic_init_v3(GIC_BASE + GICC_OFFSET, GIC_BASE + GICD_OFFSET,
57 		    GIC_REDIST_BASE);
58 #else
59 	gic_init(GIC_BASE + GICC_OFFSET, GIC_BASE + GICD_OFFSET);
60 #endif
61 	if (IS_ENABLED(CFG_CORE_SEL1_SPMC) &&
62 	    IS_ENABLED(CFG_CORE_ASYNC_NOTIF)) {
63 		size_t it = CFG_CORE_ASYNC_NOTIF_GIC_INTID;
64 
65 		if (it >= GIC_SGI_SEC_BASE && it <= GIC_SGI_SEC_MAX)
66 			gic_init_donate_sgi_to_ns(it);
67 		thread_spmc_set_async_notif_intid(it);
68 	}
69 }
70 
71 void boot_secondary_init_intc(void)
72 {
73 	gic_init_per_cpu();
74 }
75 #endif /*CFG_GIC*/
76 
77 #ifdef CFG_CORE_HAFNIUM_INTC
78 void boot_primary_init_intc(void)
79 {
80 	hfic_init();
81 }
82 #endif
83 
84 void plat_console_init(void)
85 {
86 	pl011_init(&console_data, CONSOLE_UART_BASE, CONSOLE_UART_CLK_IN_HZ,
87 		   CONSOLE_BAUDRATE);
88 	register_serial_console(&console_data.chip);
89 }
90 
91 #if (defined(CFG_GIC) || defined(CFG_CORE_HAFNIUM_INTC)) && \
92 	defined(IT_CONSOLE_UART) && \
93 	!defined(CFG_NS_VIRTUALIZATION) && \
94 	!(defined(CFG_WITH_ARM_TRUSTED_FW) && defined(CFG_ARM_GICV2)) && \
95 	!defined(CFG_SEMIHOSTING_CONSOLE)
96 /*
97  * This cannot be enabled with TF-A and GICv3 because TF-A then need to
98  * assign the interrupt number of the UART to OP-TEE (S-EL1). Currently
99  * there's no way of TF-A to know which interrupts that OP-TEE will serve.
100  * If TF-A doesn't assign the interrupt we're enabling below to OP-TEE it
101  * will hang in EL3 since the interrupt will just be delivered again and
102  * again.
103  */
104 
105 static void read_console(void)
106 {
107 	struct serial_chip *cons = &console_data.chip;
108 
109 	if (!cons->ops->getchar || !cons->ops->have_rx_data)
110 		return;
111 
112 	while (cons->ops->have_rx_data(cons)) {
113 		int ch __maybe_unused = cons->ops->getchar(cons);
114 
115 		DMSG("got 0x%x", ch);
116 	}
117 }
118 
119 static enum itr_return console_itr_cb(struct itr_handler *hdl __unused)
120 {
121 	if (notif_async_is_started()) {
122 		/*
123 		 * Asynchronous notifications are enabled, lets read from
124 		 * uart in the bottom half instead.
125 		 */
126 		console_data.chip.ops->rx_intr_disable(&console_data.chip);
127 		notif_send_async(NOTIF_VALUE_DO_BOTTOM_HALF);
128 	} else {
129 		read_console();
130 	}
131 	return ITRR_HANDLED;
132 }
133 
134 static struct itr_handler console_itr = {
135 	.it = IT_CONSOLE_UART,
136 	.flags = ITRF_TRIGGER_LEVEL,
137 	.handler = console_itr_cb,
138 };
139 DECLARE_KEEP_PAGER(console_itr);
140 
141 static void atomic_console_notif(struct notif_driver *ndrv __unused,
142 				 enum notif_event ev __maybe_unused)
143 {
144 	DMSG("Asynchronous notifications started, event %d", (int)ev);
145 }
146 DECLARE_KEEP_PAGER(atomic_console_notif);
147 
148 static void yielding_console_notif(struct notif_driver *ndrv __unused,
149 				   enum notif_event ev)
150 {
151 	switch (ev) {
152 	case NOTIF_EVENT_DO_BOTTOM_HALF:
153 		read_console();
154 		console_data.chip.ops->rx_intr_enable(&console_data.chip);
155 		break;
156 	case NOTIF_EVENT_STOPPED:
157 		DMSG("Asynchronous notifications stopped");
158 		console_data.chip.ops->rx_intr_enable(&console_data.chip);
159 		break;
160 	default:
161 		EMSG("Unknown event %d", (int)ev);
162 	}
163 }
164 
165 struct notif_driver console_notif = {
166 	.atomic_cb = atomic_console_notif,
167 	.yielding_cb = yielding_console_notif,
168 };
169 
170 static TEE_Result init_console_itr(void)
171 {
172 	TEE_Result res = TEE_ERROR_GENERIC;
173 	bool have_itr_ctrl = console_data.chip.ops->rx_intr_enable &&
174 			     console_data.chip.ops->rx_intr_disable;
175 
176 	res = interrupt_add_handler_with_chip(interrupt_get_main_chip(),
177 					      &console_itr);
178 	if (res)
179 		return res;
180 
181 	interrupt_enable(console_itr.chip, console_itr.it);
182 
183 	if (IS_ENABLED(CFG_CORE_ASYNC_NOTIF) && have_itr_ctrl)
184 		notif_register_driver(&console_notif);
185 	return TEE_SUCCESS;
186 }
187 driver_init(init_console_itr);
188 #endif
189 
190 #ifdef CFG_TZC400
191 register_phys_mem_pgdir(MEM_AREA_IO_SEC, TZC400_BASE, TZC400_REG_SIZE);
192 
193 static TEE_Result init_tzc400(void)
194 {
195 	void *va;
196 
197 	DMSG("Initializing TZC400");
198 
199 	va = phys_to_virt(TZC400_BASE, MEM_AREA_IO_SEC, TZC400_REG_SIZE);
200 	if (!va) {
201 		EMSG("TZC400 not mapped");
202 		panic();
203 	}
204 
205 	tzc_init((vaddr_t)va);
206 	tzc_dump_state();
207 
208 	return TEE_SUCCESS;
209 }
210 
211 service_init(init_tzc400);
212 #endif /*CFG_TZC400*/
213 
214 #if defined(PLATFORM_FLAVOR_qemu_virt)
215 static void release_secondary_early_hpen(size_t pos)
216 {
217 	struct mailbox {
218 		uint64_t ep;
219 		uint64_t hpen[];
220 	} *mailbox;
221 
222 	if (cpu_mmu_enabled())
223 		mailbox = phys_to_virt(SECRAM_BASE, MEM_AREA_IO_SEC,
224 				       SECRAM_COHERENT_SIZE);
225 	else
226 		mailbox = (void *)SECRAM_BASE;
227 
228 	if (!mailbox)
229 		panic();
230 
231 	mailbox->ep = TEE_LOAD_ADDR;
232 	dsb_ishst();
233 	mailbox->hpen[pos] = 1;
234 	dsb_ishst();
235 	sev();
236 }
237 
238 int psci_cpu_on(uint32_t core_id, uint32_t entry, uint32_t context_id)
239 {
240 	size_t pos = get_core_pos_mpidr(core_id);
241 	static bool core_is_released[CFG_TEE_CORE_NB_CORE];
242 
243 	if (!pos || pos >= CFG_TEE_CORE_NB_CORE)
244 		return PSCI_RET_INVALID_PARAMETERS;
245 
246 	DMSG("core pos: %zu: ns_entry %#" PRIx32, pos, entry);
247 
248 	if (core_is_released[pos]) {
249 		EMSG("core %zu already released", pos);
250 		return PSCI_RET_DENIED;
251 	}
252 	core_is_released[pos] = true;
253 
254 	boot_set_core_ns_entry(pos, entry, context_id);
255 	release_secondary_early_hpen(pos);
256 
257 	return PSCI_RET_SUCCESS;
258 }
259 #endif /*PLATFORM_FLAVOR_qemu_virt*/
260 
261 #if defined(CFG_CALLOUT) && defined(IT_SEC_PHY_TIMER) && \
262 	!defined(CFG_CORE_SEL2_SPMC)
263 static TEE_Result init_callout_service(void)
264 {
265 	timer_init_callout_service(interrupt_get_main_chip(), IT_SEC_PHY_TIMER);
266 
267 	return TEE_SUCCESS;
268 }
269 
270 nex_early_init(init_callout_service);
271 #endif
272