xref: /optee_os/core/arch/arm/plat-vexpress/main.c (revision 385e3901a6f10bc2bb000fa283909db3bdbd9809)
1 /*
2  * Copyright (c) 2016, Linaro Limited
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <platform_config.h>
30 
31 #include <stdint.h>
32 #include <string.h>
33 
34 #include <drivers/gic.h>
35 #include <drivers/pl011.h>
36 
37 #include <arm.h>
38 #include <kernel/generic_boot.h>
39 #include <kernel/pm_stubs.h>
40 #include <trace.h>
41 #include <kernel/misc.h>
42 #include <kernel/tee_time.h>
43 #include <tee/entry_fast.h>
44 #include <tee/entry_std.h>
45 #include <mm/core_memprot.h>
46 #include <console.h>
47 #include <keep.h>
48 #include <initcall.h>
49 
50 static void main_fiq(void);
51 
52 static const struct thread_handlers handlers = {
53 	.std_smc = tee_entry_std,
54 	.fast_smc = tee_entry_fast,
55 	.fiq = main_fiq,
56 #if defined(CFG_WITH_ARM_TRUSTED_FW)
57 	.cpu_on = cpu_on_handler,
58 	.cpu_off = pm_do_nothing,
59 	.cpu_suspend = pm_do_nothing,
60 	.cpu_resume = pm_do_nothing,
61 	.system_off = pm_do_nothing,
62 	.system_reset = pm_do_nothing,
63 #else
64 	.cpu_on = pm_panic,
65 	.cpu_off = pm_panic,
66 	.cpu_suspend = pm_panic,
67 	.cpu_resume = pm_panic,
68 	.system_off = pm_panic,
69 	.system_reset = pm_panic,
70 #endif
71 };
72 
73 static struct gic_data gic_data;
74 
75 const struct thread_handlers *generic_boot_get_handlers(void)
76 {
77 	return &handlers;
78 }
79 
80 #ifdef GIC_BASE
81 void main_init_gic(void)
82 {
83 	vaddr_t gicc_base;
84 	vaddr_t gicd_base;
85 
86 	gicc_base = (vaddr_t)phys_to_virt(GIC_BASE + GICC_OFFSET,
87 					  MEM_AREA_IO_SEC);
88 	gicd_base = (vaddr_t)phys_to_virt(GIC_BASE + GICD_OFFSET,
89 					  MEM_AREA_IO_SEC);
90 	TEE_ASSERT(gicc_base && gicd_base);
91 
92 #if PLATFORM_FLAVOR_IS(fvp) || PLATFORM_FLAVOR_IS(juno) || \
93     PLATFORM_FLAVOR_IS(qemu_armv8a)
94 	/* On ARMv8, GIC configuration is initialized in ARM-TF */
95 	gic_init_base_addr(&gic_data, gicc_base, gicd_base);
96 #else
97 	/* Initialize GIC */
98 	gic_init(&gic_data, gicc_base, gicd_base);
99 #endif
100 	itr_init(&gic_data.chip);
101 }
102 #endif
103 
104 static void main_fiq(void)
105 {
106 	gic_it_handle(&gic_data);
107 }
108 
109 static vaddr_t console_base(void)
110 {
111 	static void *va;
112 
113 	if (cpu_mmu_enabled()) {
114 		if (!va)
115 			va = phys_to_virt(CONSOLE_UART_BASE, MEM_AREA_IO_SEC);
116 		return (vaddr_t)va;
117 	}
118 	return CONSOLE_UART_BASE;
119 }
120 
121 void console_init(void)
122 {
123 	pl011_init(console_base(), CONSOLE_UART_CLK_IN_HZ, CONSOLE_BAUDRATE);
124 }
125 
126 void console_putc(int ch)
127 {
128 	vaddr_t base = console_base();
129 
130 	pl011_putc(ch, base);
131 	if (ch == '\n')
132 		pl011_putc('\r', base);
133 }
134 
135 void console_flush(void)
136 {
137 	pl011_flush(console_base());
138 }
139 
140 #ifdef IT_CONSOLE_UART
141 static enum itr_return console_itr_cb(struct itr_handler *h __unused)
142 {
143 	paddr_t uart_base = console_base();
144 
145 	while (pl011_have_rx_data(uart_base)) {
146 		int ch __maybe_unused = pl011_getchar(uart_base);
147 
148 		DMSG("cpu %zu: got 0x%x", get_core_pos(), ch);
149 	}
150 	return ITRR_HANDLED;
151 }
152 
153 static struct itr_handler console_itr = {
154 	.it = IT_CONSOLE_UART,
155 	.flags = ITRF_TRIGGER_LEVEL,
156 	.handler = console_itr_cb,
157 };
158 KEEP_PAGER(console_itr);
159 
160 static TEE_Result init_console_itr(void)
161 {
162 	itr_add(&console_itr);
163 	itr_enable(&console_itr);
164 	return TEE_SUCCESS;
165 }
166 driver_init(init_console_itr);
167 #endif
168