xref: /optee_os/core/arch/arm/plat-vexpress/main.c (revision 817466cb476de705a8e3dabe1ef165fe27a18c2f)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2016, Linaro Limited
4  * Copyright (c) 2014, STMicroelectronics International N.V.
5  */
6 
7 #include <arm.h>
8 #include <console.h>
9 #include <drivers/gic.h>
10 #include <drivers/pl011.h>
11 #include <drivers/tzc400.h>
12 #include <initcall.h>
13 #include <keep.h>
14 #include <kernel/generic_boot.h>
15 #include <kernel/misc.h>
16 #include <kernel/panic.h>
17 #include <kernel/pm_stubs.h>
18 #include <kernel/tee_time.h>
19 #include <mm/core_memprot.h>
20 #include <mm/core_mmu.h>
21 #include <platform_config.h>
22 #include <sm/psci.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <tee/entry_fast.h>
26 #include <tee/entry_std.h>
27 #include <trace.h>
28 
29 static void main_fiq(void);
30 
31 static const struct thread_handlers handlers = {
32 	.std_smc = tee_entry_std,
33 	.fast_smc = tee_entry_fast,
34 	.nintr = main_fiq,
35 #if defined(CFG_WITH_ARM_TRUSTED_FW)
36 	.cpu_on = cpu_on_handler,
37 	.cpu_off = pm_do_nothing,
38 	.cpu_suspend = pm_do_nothing,
39 	.cpu_resume = pm_do_nothing,
40 	.system_off = pm_do_nothing,
41 	.system_reset = pm_do_nothing,
42 #else
43 	.cpu_on = pm_panic,
44 	.cpu_off = pm_panic,
45 	.cpu_suspend = pm_panic,
46 	.cpu_resume = pm_panic,
47 	.system_off = pm_panic,
48 	.system_reset = pm_panic,
49 #endif
50 };
51 
52 static struct gic_data gic_data;
53 static struct pl011_data console_data;
54 
55 register_phys_mem(MEM_AREA_IO_SEC, CONSOLE_UART_BASE, PL011_REG_SIZE);
56 #if defined(PLATFORM_FLAVOR_fvp)
57 register_phys_mem(MEM_AREA_RAM_SEC, TZCDRAM_BASE, TZCDRAM_SIZE);
58 #endif
59 #if defined(PLATFORM_FLAVOR_qemu_virt)
60 register_phys_mem(MEM_AREA_IO_SEC, SECRAM_BASE, SECRAM_COHERENT_SIZE);
61 #endif
62 #ifdef DRAM0_BASE
63 register_ddr(DRAM0_BASE, DRAM0_SIZE);
64 #endif
65 #ifdef DRAM1_BASE
66 register_ddr(DRAM1_BASE, DRAM1_SIZE);
67 #endif
68 
69 const struct thread_handlers *generic_boot_get_handlers(void)
70 {
71 	return &handlers;
72 }
73 
74 #ifdef GIC_BASE
75 
76 register_phys_mem(MEM_AREA_IO_SEC, GICD_BASE, GIC_DIST_REG_SIZE);
77 register_phys_mem(MEM_AREA_IO_SEC, GICC_BASE, GIC_DIST_REG_SIZE);
78 
79 void main_init_gic(void)
80 {
81 	vaddr_t gicc_base;
82 	vaddr_t gicd_base;
83 
84 	gicc_base = (vaddr_t)phys_to_virt(GIC_BASE + GICC_OFFSET,
85 					  MEM_AREA_IO_SEC);
86 	gicd_base = (vaddr_t)phys_to_virt(GIC_BASE + GICD_OFFSET,
87 					  MEM_AREA_IO_SEC);
88 	if (!gicc_base || !gicd_base)
89 		panic();
90 
91 #if defined(CFG_WITH_ARM_TRUSTED_FW)
92 	/* On ARMv8, GIC configuration is initialized in ARM-TF */
93 	gic_init_base_addr(&gic_data, gicc_base, gicd_base);
94 #else
95 	/* Initialize GIC */
96 	gic_init(&gic_data, gicc_base, gicd_base);
97 #endif
98 	itr_init(&gic_data.chip);
99 }
100 
101 #if !defined(CFG_WITH_ARM_TRUSTED_FW)
102 void main_secondary_init_gic(void)
103 {
104 	gic_cpu_init(&gic_data);
105 }
106 #endif
107 
108 #endif
109 
110 static void main_fiq(void)
111 {
112 	gic_it_handle(&gic_data);
113 }
114 
115 void console_init(void)
116 {
117 	pl011_init(&console_data, CONSOLE_UART_BASE, CONSOLE_UART_CLK_IN_HZ,
118 		   CONSOLE_BAUDRATE);
119 	register_serial_console(&console_data.chip);
120 }
121 
122 #ifdef IT_CONSOLE_UART
123 static enum itr_return console_itr_cb(struct itr_handler *h __unused)
124 {
125 	struct serial_chip *cons = &console_data.chip;
126 
127 	while (cons->ops->have_rx_data(cons)) {
128 		int ch __maybe_unused = cons->ops->getchar(cons);
129 
130 		DMSG("cpu %zu: got 0x%x", get_core_pos(), ch);
131 	}
132 	return ITRR_HANDLED;
133 }
134 
135 static struct itr_handler console_itr = {
136 	.it = IT_CONSOLE_UART,
137 	.flags = ITRF_TRIGGER_LEVEL,
138 	.handler = console_itr_cb,
139 };
140 KEEP_PAGER(console_itr);
141 
142 static TEE_Result init_console_itr(void)
143 {
144 	itr_add(&console_itr);
145 	itr_enable(IT_CONSOLE_UART);
146 	return TEE_SUCCESS;
147 }
148 driver_init(init_console_itr);
149 #endif
150 
151 #ifdef CFG_TZC400
152 register_phys_mem(MEM_AREA_IO_SEC, TZC400_BASE, TZC400_REG_SIZE);
153 
154 static TEE_Result init_tzc400(void)
155 {
156 	void *va;
157 
158 	DMSG("Initializing TZC400");
159 
160 	va = phys_to_virt(TZC400_BASE, MEM_AREA_IO_SEC);
161 	if (!va) {
162 		EMSG("TZC400 not mapped");
163 		panic();
164 	}
165 
166 	tzc_init((vaddr_t)va);
167 	tzc_dump_state();
168 
169 	return TEE_SUCCESS;
170 }
171 
172 service_init(init_tzc400);
173 #endif /*CFG_TZC400*/
174 
175 #if defined(PLATFORM_FLAVOR_qemu_virt)
176 static void release_secondary_early_hpen(size_t pos)
177 {
178 	struct mailbox {
179 		uint64_t ep;
180 		uint64_t hpen[];
181 	} *mailbox;
182 
183 	if (cpu_mmu_enabled())
184 		mailbox = phys_to_virt(SECRAM_BASE, MEM_AREA_IO_SEC);
185 	else
186 		mailbox = (void *)SECRAM_BASE;
187 
188 	if (!mailbox)
189 		panic();
190 
191 	mailbox->ep = TEE_LOAD_ADDR;
192 	dsb_ishst();
193 	mailbox->hpen[pos] = 1;
194 	dsb_ishst();
195 	sev();
196 }
197 
198 int psci_cpu_on(uint32_t core_id, uint32_t entry, uint32_t context_id)
199 {
200 	size_t pos = get_core_pos_mpidr(core_id);
201 	static bool core_is_released[CFG_TEE_CORE_NB_CORE];
202 
203 	if (!pos || pos >= CFG_TEE_CORE_NB_CORE)
204 		return PSCI_RET_INVALID_PARAMETERS;
205 
206 	DMSG("core pos: %zu: ns_entry %#" PRIx32, pos, entry);
207 
208 	if (core_is_released[pos]) {
209 		EMSG("core %zu already released", pos);
210 		return PSCI_RET_DENIED;
211 	}
212 	core_is_released[pos] = true;
213 
214 	generic_boot_set_core_ns_entry(pos, entry, context_id);
215 	release_secondary_early_hpen(pos);
216 
217 	return PSCI_RET_SUCCESS;
218 }
219 #endif /*PLATFORM_FLAVOR_qemu_virt*/
220