xref: /optee_os/core/arch/arm/plat-stm/main.c (revision 39e661bc92dfa76623e07b35c1a223e43548bf5f)
1 /*
2  * Copyright (c) 2014-2016, STMicroelectronics International N.V.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <arm32.h>
29 #include <console.h>
30 #include <drivers/gic.h>
31 #include <drivers/stih_asc.h>
32 #include <io.h>
33 #include <kernel/generic_boot.h>
34 #include <kernel/misc.h>
35 #include <kernel/panic.h>
36 #include <kernel/pm_stubs.h>
37 #include <kernel/tz_ssvce_pl310.h>
38 #include <mm/core_mmu.h>
39 #include <mm/core_memprot.h>
40 #include <platform_config.h>
41 #include <stdint.h>
42 #include <tee/entry_std.h>
43 #include <tee/entry_fast.h>
44 #include <trace.h>
45 #include <util.h>
46 
47 register_phys_mem(MEM_AREA_IO_SEC, CPU_IOMEM_BASE, CORE_MMU_DEVICE_SIZE);
48 register_phys_mem(MEM_AREA_IO_SEC, RNG_BASE, CORE_MMU_DEVICE_SIZE);
49 register_phys_mem(MEM_AREA_IO_NSEC, UART_CONSOLE_BASE, CORE_MMU_DEVICE_SIZE);
50 
51 static struct gic_data gic_data;
52 static void main_fiq(void);
53 
54 #if defined(PLATFORM_FLAVOR_b2260)
55 #define stm_tee_entry_std	tee_entry_std
56 static bool ns_resources_ready(void)
57 {
58 	return true;
59 }
60 #else
61 /* some nonsecure resource might not be ready (uart) */
62 static int boot_is_completed __early_bss;
63 static bool ns_resources_ready(void)
64 {
65 	return !!boot_is_completed;
66 }
67 static void stm_tee_entry_std(struct thread_smc_args *smc_args)
68 {
69 	boot_is_completed = 1;
70 	tee_entry_std(smc_args);
71 }
72 #endif
73 
74 static const struct thread_handlers handlers = {
75 	.std_smc = stm_tee_entry_std,
76 	.fast_smc = tee_entry_fast,
77 	.nintr = main_fiq,
78 	.cpu_on = pm_panic,
79 	.cpu_off = pm_panic,
80 	.cpu_suspend = pm_panic,
81 	.cpu_resume = pm_panic,
82 	.system_off = pm_panic,
83 	.system_reset = pm_panic,
84 };
85 
86 const struct thread_handlers *generic_boot_get_handlers(void)
87 {
88 	return &handlers;
89 }
90 
91 static vaddr_t console_base(void)
92 {
93 	static void *va __early_bss;
94 
95 	if (cpu_mmu_enabled()) {
96 		if (!va)
97 			va = phys_to_virt(UART_CONSOLE_BASE, MEM_AREA_IO_NSEC);
98 		return (vaddr_t)va;
99 	}
100 	return UART_CONSOLE_BASE;
101 }
102 
103 void console_init(void)
104 {
105 	stih_asc_init(console_base());
106 }
107 
108 void console_putc(int ch)
109 {
110 	if (ns_resources_ready()) {
111 		vaddr_t base = console_base();
112 
113 		if (ch == '\n')
114 			stih_asc_putc('\r', base);
115 		stih_asc_putc(ch, base);
116 	}
117 }
118 
119 void console_flush(void)
120 {
121 	if (ns_resources_ready())
122 		stih_asc_flush(console_base());
123 }
124 
125 vaddr_t pl310_base(void)
126 {
127 	static void *va __early_bss;
128 
129 	if (cpu_mmu_enabled()) {
130 		if (!va)
131 			va = phys_to_virt(PL310_BASE, MEM_AREA_IO_SEC);
132 		return (vaddr_t)va;
133 	}
134 	return PL310_BASE;
135 }
136 
137 void arm_cl2_config(vaddr_t pl310)
138 {
139 	/* pl310 off */
140 	write32(0, pl310 + PL310_CTRL);
141 
142 	/* config PL310 */
143 	write32(PL310_TAG_RAM_CTRL_INIT, pl310 + PL310_TAG_RAM_CTRL);
144 	write32(PL310_DATA_RAM_CTRL_INIT, pl310 + PL310_DATA_RAM_CTRL);
145 	write32(PL310_AUX_CTRL_INIT, pl310 + PL310_AUX_CTRL);
146 	write32(PL310_PREFETCH_CTRL_INIT, pl310 + PL310_PREFETCH_CTRL);
147 	write32(PL310_POWER_CTRL_INIT, pl310 + PL310_POWER_CTRL);
148 
149 	/* invalidate all pl310 cache ways */
150 	arm_cl2_invbyway(pl310);
151 }
152 
153 void plat_cpu_reset_late(void)
154 {
155 	int i;
156 
157 	assert(!cpu_mmu_enabled());
158 
159 	/* Allow NSec to Imprecise abort */
160 	write_scr(SCR_AW);
161 
162 	if (get_core_pos())
163 		return;
164 
165 	write32(SCU_SAC_INIT, SCU_BASE + SCU_SAC);
166 	write32(SCU_NSAC_INIT, SCU_BASE + SCU_NSAC);
167 	write32(CPU_PORT_FILT_END, SCU_BASE + SCU_FILT_EA);
168 	write32(CPU_PORT_FILT_START, SCU_BASE + SCU_FILT_SA);
169 	write32(SCU_CTRL_INIT, SCU_BASE + SCU_CTRL);
170 
171 	write32(CPU_PORT_FILT_END, pl310_base() + PL310_ADDR_FILT_END);
172 	write32(CPU_PORT_FILT_START | PL310_CTRL_ENABLE_BIT,
173 				   pl310_base() + PL310_ADDR_FILT_START);
174 
175 	/* TODO: gic_init scan fails, pre-init all SPIs are nonsecure */
176 	for (i = 0; i < (31 * 4); i += 4)
177 		write32(0xFFFFFFFF, GIC_DIST_BASE + GIC_DIST_ISR1 + i);
178 }
179 
180 void main_init_gic(void)
181 {
182 	vaddr_t gicc_base;
183 	vaddr_t gicd_base;
184 
185 	gicc_base = (vaddr_t)phys_to_virt(GIC_CPU_BASE, MEM_AREA_IO_SEC);
186 	gicd_base = (vaddr_t)phys_to_virt(GIC_DIST_BASE, MEM_AREA_IO_SEC);
187 
188 	if (!gicc_base || !gicd_base)
189 		panic();
190 
191 	gic_init(&gic_data, gicc_base, gicd_base);
192 	itr_init(&gic_data.chip);
193 }
194 
195 void main_secondary_init_gic(void)
196 {
197 	gic_cpu_init(&gic_data);
198 }
199 
200 static void main_fiq(void)
201 {
202 	gic_it_handle(&gic_data);
203 }
204