3 * Security related functions for OMAP5 class devices
6 * Texas Instruments, <www.ti.com>
8 * Daniel Allred <d-allred@ti.com>
9 * Harinarayan Bhatta <harinarayan@ti.com>
11 * SPDX-License-Identifier: GPL-2.0+
17 #include <asm/arch/sys_proto.h>
18 #include <asm/omap_common.h>
19 #include <asm/omap_sec_common.h>
22 #include <asm/cache.h>
24 #include <tee/optee.h>
26 /* Index for signature PPA-based TI HAL APIs */
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)
36 /* Argument for PPA_SERV_HAL_TEE_LOAD_MASTER */
37 struct ppa_tee_load_info {
38 u32 tee_sec_mem_start; /* Physical start address reserved for TEE */
39 u32 tee_sec_mem_size; /* Size of the memory reserved for TEE */
40 u32 tee_cert_start; /* Address where signed TEE binary is loaded */
41 u32 tee_cert_size; /* Size of TEE certificate (signed binary) */
42 u32 tee_jump_addr; /* Address to jump to start TEE execution */
43 u32 tee_arg0; /* argument to TEE jump function, in r0 */
46 static u32 get_sec_mem_start(void)
48 u32 sec_mem_start = CONFIG_TI_SECURE_EMIF_REGION_START;
49 u32 sec_mem_size = CONFIG_TI_SECURE_EMIF_TOTAL_REGION_SIZE;
51 * Total reserved region is all contiguous with protected
52 * region coming first, followed by the non-secure region.
53 * If 0x0 start address is given, we simply put the reserved
54 * region at the end of the external DRAM.
56 if (sec_mem_start == 0)
58 (CONFIG_SYS_SDRAM_BASE +
59 (omap_sdram_size() - sec_mem_size));
63 int 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)
70 * Call PPA HAL API to do any other general firewall
71 * configuration for regions 1-6 of the EMIF firewall.
73 debug("%s: regionNum = %x, startAddr = %x, size = %x", __func__,
74 region_num, start_addr, size);
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);
82 puts("Secure EMIF Firewall Setup failed!\n");
83 debug("Return Value = %x\n", result);
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."
93 int secure_emif_reserve(void)
96 u32 sec_mem_start = get_sec_mem_start();
97 u32 sec_prot_size = CONFIG_TI_SECURE_EMIF_PROTECTED_REGION_SIZE;
99 /* If there is no protected region, there is no reservation to make */
100 if (sec_prot_size == 0)
104 * Call PPA HAL API to reserve a chunk of EMIF SDRAM
105 * for secure world use. This region should be carved out
106 * from use by any public code. EMIF firewall region 7
107 * will be used to protect this block of memory.
109 result = secure_rom_call(
110 PPA_SERV_HAL_SETUP_SEC_RESVD_REGION,
111 0, 0, 2, sec_mem_start, sec_prot_size);
114 puts("SDRAM Firewall: Secure memory reservation failed!\n");
115 debug("Return Value = %x\n", result);
121 int secure_emif_firewall_lock(void)
126 * Call PPA HAL API to lock the EMIF firewall configurations.
127 * After this API is called, none of the PPA HAL APIs for
128 * configuring the EMIF firewalls will be usable again (that
129 * is, calls to those APIs will return failure and have no
133 result = secure_rom_call(
134 PPA_SERV_HAL_LOCK_EMIF_FW,
138 puts("Secure EMIF Firewall Lock failed!\n");
139 debug("Return Value = %x\n", result);
145 static struct ppa_tee_load_info tee_info __aligned(ARCH_DMA_MINALIGN);
147 int secure_tee_install(u32 addr)
149 struct optee_header *hdr;
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;
157 /* If there is no protected region, there is no place to put the TEE */
159 printf("Error loading TEE, no protected memory region available\n");
163 hdr = (struct optee_header *)map_sysmem(addr, sizeof(struct optee_header));
164 /* 280 bytes = size of signature */
165 tee_file_size = hdr->init_size + hdr->paged_size +
166 sizeof(struct optee_header) + 280;
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");
177 return CMD_RET_FAILURE;
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;
187 loadptr = map_sysmem(addr, tee_file_size);
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);
197 #if !defined(CONFIG_SYS_DCACHE_OFF)
199 rounddown((u32)loadptr, ARCH_DMA_MINALIGN),
200 roundup((u32)loadptr + tee_file_size, ARCH_DMA_MINALIGN));
202 flush_dcache_range((u32)&tee_info, (u32)&tee_info +
203 roundup(sizeof(tee_info), ARCH_DMA_MINALIGN));
205 unmap_sysmem(loadptr);
207 ret = secure_rom_call(PPA_SERV_HAL_TEE_LOAD_MASTER, 0, 0, 1, &tee_info);
209 printf("TEE_LOAD_MASTER Failed\n");
212 printf("TEE_LOAD_MASTER Done\n");
215 /* Reuse the tee_info buffer for SMC params */
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));
222 ret = omap_smc_sec_cpu1(PPA_SERV_HAL_TEE_LOAD_SLAVE, 0, 0,
225 printf("TEE_LOAD_SLAVE Failed\n");
228 printf("TEE_LOAD_SLAVE Done\n");