xref: /optee_os/core/arch/arm/plat-ls/main.c (revision 5b25c76ac40f830867e3d60800120ffd7874e8dc)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright 2018 NXP
4  * Copyright (C) 2015 Freescale Semiconductor, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <platform_config.h>
31 
32 #include <arm.h>
33 #include <console.h>
34 #include <drivers/gic.h>
35 #ifdef CFG_PL011
36 #include <drivers/pl011.h>
37 #else
38 #include <drivers/ns16550.h>
39 #endif
40 #include <io.h>
41 #include <kernel/generic_boot.h>
42 #include <kernel/misc.h>
43 #include <kernel/panic.h>
44 #include <kernel/pm_stubs.h>
45 #include <kernel/thread.h>
46 #include <kernel/tz_ssvce_def.h>
47 #include <mm/core_memprot.h>
48 #include <sm/optee_smc.h>
49 #include <tee/entry_fast.h>
50 #include <tee/entry_std.h>
51 #include <kernel/tee_common_otp.h>
52 #include <mm/core_mmu.h>
53 
54 static const struct thread_handlers handlers = {
55 #if defined(CFG_WITH_ARM_TRUSTED_FW)
56 	.cpu_on = cpu_on_handler,
57 	.cpu_off = pm_do_nothing,
58 	.cpu_suspend = pm_do_nothing,
59 	.cpu_resume = pm_do_nothing,
60 	.system_off = pm_do_nothing,
61 	.system_reset = pm_do_nothing,
62 #else
63 	.cpu_on = pm_panic,
64 	.cpu_off = pm_panic,
65 	.cpu_suspend = pm_panic,
66 	.cpu_resume = pm_panic,
67 	.system_off = pm_panic,
68 	.system_reset = pm_panic,
69 #endif
70 };
71 
72 static struct gic_data gic_data;
73 #ifdef CFG_PL011
74 static struct pl011_data console_data;
75 #else
76 static struct ns16550_data console_data;
77 #endif
78 
79 register_phys_mem_pgdir(MEM_AREA_IO_NSEC, CONSOLE_UART_BASE,
80 			CORE_MMU_PGDIR_SIZE);
81 register_phys_mem_pgdir(MEM_AREA_IO_SEC, GIC_BASE, CORE_MMU_PGDIR_SIZE);
82 
83 const struct thread_handlers *generic_boot_get_handlers(void)
84 {
85 	return &handlers;
86 }
87 
88 #ifdef CFG_ARM32_core
89 void plat_primary_init_early(void)
90 {
91 	vaddr_t addr;
92 
93 #if defined(CFG_BOOT_SECONDARY_REQUEST)
94 	/* set secondary entry address */
95 	io_write32(DCFG_BASE + DCFG_SCRATCHRW1,
96 		   __compiler_bswap32(TEE_LOAD_ADDR));
97 
98 	/* release secondary cores */
99 	io_write32(DCFG_BASE + DCFG_CCSR_BRR /* cpu1 */,
100 		   __compiler_bswap32(0x1 << 1));
101 	dsb();
102 	sev();
103 #endif
104 
105 	/* configure CSU */
106 
107 	/* first grant all peripherals */
108 	for (addr = CSU_BASE + CSU_CSL_START;
109 		 addr != CSU_BASE + CSU_CSL_END;
110 		 addr += 4)
111 		io_write32(addr, __compiler_bswap32(CSU_ACCESS_ALL));
112 
113 	/* restrict key preipherals from NS */
114 	io_write32(CSU_BASE + CSU_CSL30,
115 		   __compiler_bswap32(CSU_ACCESS_SEC_ONLY));
116 	io_write32(CSU_BASE + CSU_CSL37,
117 		   __compiler_bswap32(CSU_ACCESS_SEC_ONLY));
118 
119 	/* lock the settings */
120 	for (addr = CSU_BASE + CSU_CSL_START;
121 	     addr != CSU_BASE + CSU_CSL_END;
122 	     addr += 4)
123 		io_setbits32(addr,
124 			     __compiler_bswap32(CSU_SETTING_LOCK));
125 }
126 #endif
127 
128 void console_init(void)
129 {
130 #ifdef CFG_PL011
131 	pl011_init(&console_data, CONSOLE_UART_BASE, CONSOLE_UART_CLK_IN_HZ,
132 		   CONSOLE_BAUDRATE);
133 #else
134 	ns16550_init(&console_data, CONSOLE_UART_BASE);
135 #endif
136 	register_serial_console(&console_data.chip);
137 }
138 
139 void main_init_gic(void)
140 {
141 	vaddr_t gicc_base;
142 	vaddr_t gicd_base;
143 
144 	gicc_base = (vaddr_t)phys_to_virt(GIC_BASE + GICC_OFFSET,
145 					  MEM_AREA_IO_SEC);
146 	gicd_base = (vaddr_t)phys_to_virt(GIC_BASE + GICD_OFFSET,
147 					  MEM_AREA_IO_SEC);
148 
149 	if (!gicc_base || !gicd_base)
150 		panic();
151 
152 	/* Initialize GIC */
153 	gic_init(&gic_data, gicc_base, gicd_base);
154 	itr_init(&gic_data.chip);
155 }
156 
157 void main_secondary_init_gic(void)
158 {
159 	gic_cpu_init(&gic_data);
160 }
161 
162 #ifdef CFG_HW_UNQ_KEY_REQUEST
163 
164 #include <types_ext.h>
165 int get_hw_unique_key(uint64_t smc_func_id, uint64_t in_key, uint64_t size);
166 
167 /*
168  * Issued when requesting to Secure Storage Key for secure storage.
169  *
170  * SiP Service Calls
171  *
172  * Register usage:
173  * r0/x0	SMC Function ID, OPTEE_SMC_FUNCID_SIP_LS_HW_UNQ_KEY
174  */
175 #define OPTEE_SMC_FUNCID_SIP_LS_HW_UNQ_KEY			0xFF14
176 #define OPTEE_SMC_FAST_CALL_SIP_LS_HW_UNQ_KEY \
177 	OPTEE_SMC_CALL_VAL(OPTEE_SMC_32, OPTEE_SMC_FAST_CALL, \
178 			   OPTEE_SMC_OWNER_SIP, \
179 			   OPTEE_SMC_FUNCID_SIP_LS_HW_UNQ_KEY)
180 
181 TEE_Result tee_otp_get_hw_unique_key(struct tee_hw_unique_key *hwkey)
182 {
183 	TEE_Result res;
184 	int ret = 0;
185 	uint8_t hw_unq_key[sizeof(hwkey->data)] __aligned(64);
186 
187 	ret = get_hw_unique_key(OPTEE_SMC_FAST_CALL_SIP_LS_HW_UNQ_KEY,
188 			virt_to_phys(hw_unq_key), sizeof(hwkey->data));
189 
190 	if (ret < 0) {
191 		EMSG("\nH/W Unique key is not fetched from the platform.");
192 		res = TEE_ERROR_SECURITY;
193 	} else {
194 		memcpy(&hwkey->data[0], hw_unq_key, sizeof(hwkey->data));
195 		res = TEE_SUCCESS;
196 	}
197 
198 	return res;
199 }
200 #endif
201