1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <common.h>
15#include <stdarg.h>
16
17#include <asm/arch/sys_proto.h>
18#include <asm/omap_common.h>
19#include <asm/omap_sec_common.h>
20#include <asm/spl.h>
21#include <spl.h>
22#include <asm/cache.h>
23#include <mapmem.h>
24#include <tee/optee.h>
25
26
27#define PPA_HAL_SERVICES_START_INDEX (0x200)
28#define PPA_SERV_HAL_TEE_LOAD_MASTER (PPA_HAL_SERVICES_START_INDEX + 23)
29#define PPA_SERV_HAL_TEE_LOAD_SLAVE (PPA_HAL_SERVICES_START_INDEX + 24)
30#define PPA_SERV_HAL_SETUP_SEC_RESVD_REGION (PPA_HAL_SERVICES_START_INDEX + 25)
31#define PPA_SERV_HAL_SETUP_EMIF_FW_REGION (PPA_HAL_SERVICES_START_INDEX + 26)
32#define PPA_SERV_HAL_LOCK_EMIF_FW (PPA_HAL_SERVICES_START_INDEX + 27)
33
34int tee_loaded = 0;
35
36
37struct ppa_tee_load_info {
38 u32 tee_sec_mem_start;
39 u32 tee_sec_mem_size;
40 u32 tee_cert_start;
41 u32 tee_cert_size;
42 u32 tee_jump_addr;
43 u32 tee_arg0;
44};
45
46static u32 get_sec_mem_start(void)
47{
48 u32 sec_mem_start = CONFIG_TI_SECURE_EMIF_REGION_START;
49 u32 sec_mem_size = CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE;
50
51
52
53
54
55
56 if (sec_mem_start == 0)
57 sec_mem_start =
58 (CONFIG_SYS_SDRAM_BASE +
59 (omap_sdram_size() - sec_mem_size));
60 return sec_mem_start;
61}
62
63int secure_emif_firewall_setup(uint8_t region_num, uint32_t start_addr,
64 uint32_t size, uint32_t access_perm,
65 uint32_t initiator_perm)
66{
67 int result = 1;
68
69
70
71
72
73 debug("%s: regionNum = %x, startAddr = %x, size = %x", __func__,
74 region_num, start_addr, size);
75
76 result = secure_rom_call(
77 PPA_SERV_HAL_SETUP_EMIF_FW_REGION, 0, 0, 4,
78 (start_addr & 0xFFFFFFF0) | (region_num & 0x0F),
79 size, access_perm, initiator_perm);
80
81 if (result != 0) {
82 puts("Secure EMIF Firewall Setup failed!\n");
83 debug("Return Value = %x\n", result);
84 }
85
86 return result;
87}
88
89#if (CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE < \
90 CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE)
91#error "TI Secure EMIF: Protected size cannot be larger than total size."
92#endif
93int secure_emif_reserve(void)
94{
95 int result = 1;
96 u32 sec_mem_start = get_sec_mem_start();
97 u32 sec_prot_size = CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE;
98
99
100 if (sec_prot_size == 0)
101 return 0;
102
103
104
105
106
107
108
109 result = secure_rom_call(
110 PPA_SERV_HAL_SETUP_SEC_RESVD_REGION,
111 0, 0, 2, sec_mem_start, sec_prot_size);
112
113 if (result != 0) {
114 puts("SDRAM Firewall: Secure memory reservation failed!\n");
115 debug("Return Value = %x\n", result);
116 }
117
118 return result;
119}
120
121int secure_emif_firewall_lock(void)
122{
123 int result = 1;
124
125
126
127
128
129
130
131
132
133 result = secure_rom_call(
134 PPA_SERV_HAL_LOCK_EMIF_FW,
135 0, 0, 0);
136
137 if (result != 0) {
138 puts("Secure EMIF Firewall Lock failed!\n");
139 debug("Return Value = %x\n", result);
140 }
141
142 return result;
143}
144
145static struct ppa_tee_load_info tee_info __aligned(ARCH_DMA_MINALIGN);
146
147int secure_tee_install(u32 addr)
148{
149 struct optee_header *hdr;
150 void *loadptr;
151 u32 tee_file_size;
152 u32 sec_mem_start = get_sec_mem_start();
153 const u32 size = CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE;
154 u32 *smc_cpu1_params;
155 u32 ret;
156
157
158 if (size == 0) {
159 printf("Error loading TEE, no protected memory region available\n");
160 return -ENOBUFS;
161 }
162
163 hdr = (struct optee_header *)map_sysmem(addr, sizeof(struct optee_header));
164
165 tee_file_size = hdr->init_size + hdr->paged_size +
166 sizeof(struct optee_header) + 280;
167
168 if ((hdr->magic != OPTEE_MAGIC) ||
169 (hdr->version != OPTEE_VERSION) ||
170 (hdr->init_load_addr_hi != 0) ||
171 (hdr->init_load_addr_lo < (sec_mem_start + sizeof(struct optee_header))) ||
172 (tee_file_size > size) ||
173 ((hdr->init_load_addr_lo + tee_file_size - 1) >
174 (sec_mem_start + size - 1))) {
175 printf("Error in TEE header. Check load address and sizes\n");
176 unmap_sysmem(hdr);
177 return CMD_RET_FAILURE;
178 }
179
180 tee_info.tee_sec_mem_start = sec_mem_start;
181 tee_info.tee_sec_mem_size = size;
182 tee_info.tee_jump_addr = hdr->init_load_addr_lo;
183 tee_info.tee_cert_start = addr;
184 tee_info.tee_cert_size = tee_file_size;
185 tee_info.tee_arg0 = hdr->init_size + tee_info.tee_jump_addr;
186 unmap_sysmem(hdr);
187 loadptr = map_sysmem(addr, tee_file_size);
188
189 debug("tee_info.tee_sec_mem_start= %08X\n", tee_info.tee_sec_mem_start);
190 debug("tee_info.tee_sec_mem_size = %08X\n", tee_info.tee_sec_mem_size);
191 debug("tee_info.tee_jump_addr = %08X\n", tee_info.tee_jump_addr);
192 debug("tee_info.tee_cert_start = %08X\n", tee_info.tee_cert_start);
193 debug("tee_info.tee_cert_size = %08X\n", tee_info.tee_cert_size);
194 debug("tee_info.tee_arg0 = %08X\n", tee_info.tee_arg0);
195 debug("tee_file_size = %d\n", tee_file_size);
196
197#if !defined(CONFIG_SYS_DCACHE_OFF)
198 flush_dcache_range(
199 rounddown((u32)loadptr, ARCH_DMA_MINALIGN),
200 roundup((u32)loadptr + tee_file_size, ARCH_DMA_MINALIGN));
201
202 flush_dcache_range((u32)&tee_info, (u32)&tee_info +
203 roundup(sizeof(tee_info), ARCH_DMA_MINALIGN));
204#endif
205 unmap_sysmem(loadptr);
206
207 ret = secure_rom_call(PPA_SERV_HAL_TEE_LOAD_MASTER, 0, 0, 1, &tee_info);
208 if (ret) {
209 printf("TEE_LOAD_MASTER Failed\n");
210 return ret;
211 }
212 printf("TEE_LOAD_MASTER Done\n");
213
214 if (!is_dra72x()) {
215
216 smc_cpu1_params = (u32 *)&tee_info;
217 smc_cpu1_params[0] = 0;
218#if !defined(CONFIG_SYS_DCACHE_OFF)
219 flush_dcache_range((u32)smc_cpu1_params, (u32)smc_cpu1_params +
220 roundup(sizeof(u32), ARCH_DMA_MINALIGN));
221#endif
222 ret = omap_smc_sec_cpu1(PPA_SERV_HAL_TEE_LOAD_SLAVE, 0, 0,
223 smc_cpu1_params);
224 if (ret) {
225 printf("TEE_LOAD_SLAVE Failed\n");
226 return ret;
227 }
228 printf("TEE_LOAD_SLAVE Done\n");
229 }
230
231 tee_loaded = 1;
232
233 return 0;
234}
235