1 /* 2 * Copyright (c) 2014, Allwinner Technology Co., Ltd. 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 <platform_config.h> 29 30 #include <stdint.h> 31 #include <string.h> 32 #include <assert.h> 33 34 #include <sm/sm.h> 35 #include <sm/tee_mon.h> 36 #include <sm/teesmc.h> 37 #include <sm/teesmc_optee.h> 38 39 #include <arm.h> 40 #include <kernel/thread.h> 41 #include <kernel/time_source.h> 42 #include <kernel/panic.h> 43 #include <kernel/misc.h> 44 #include <mm/tee_pager.h> 45 #include <mm/tee_mmu.h> 46 #include <mm/core_mmu.h> 47 #include <mm/tee_mmu_defs.h> 48 #include <tee/entry.h> 49 #include <tee/arch_svc.h> 50 #include <platform.h> 51 #include <util.h> 52 #include <trace.h> 53 #include <malloc.h> 54 55 /* teecore heap address/size is defined in scatter file */ 56 extern unsigned char teecore_heap_start; 57 extern unsigned char teecore_heap_end; 58 59 static void main_fiq(void); 60 static void main_tee_entry(struct thread_smc_args *args); 61 static uint32_t main_default_pm_handler(uint32_t a0, uint32_t a1); 62 63 static const struct thread_handlers handlers = { 64 .std_smc = main_tee_entry, 65 .fast_smc = main_tee_entry, 66 .fiq = main_fiq, 67 .svc = tee_svc_handler, 68 .abort = tee_pager_abort_handler, 69 .cpu_on = main_default_pm_handler, 70 .cpu_off = main_default_pm_handler, 71 .cpu_suspend = main_default_pm_handler, 72 .cpu_resume = main_default_pm_handler, 73 .system_off = main_default_pm_handler, 74 .system_reset = main_default_pm_handler, 75 }; 76 77 void main_init(uint32_t nsec_entry); /* called from assembly only */ 78 void main_init(uint32_t nsec_entry) 79 { 80 struct sm_nsec_ctx *nsec_ctx; 81 size_t pos = get_core_pos(); 82 83 /* 84 * Mask IRQ and FIQ before switch to the thread vector as the 85 * thread handler requires IRQ and FIQ to be masked while executing 86 * with the temporary stack. The thread subsystem also asserts that 87 * IRQ is blocked when using most if its functions. 88 */ 89 write_cpsr(read_cpsr() | CPSR_F | CPSR_I); 90 91 if (pos == 0) { 92 thread_init_primary(&handlers); 93 94 /* initialize platform */ 95 platform_init(); 96 } 97 98 thread_init_per_cpu(); 99 100 /* Initialize secure monitor */ 101 nsec_ctx = sm_get_nsec_ctx(); 102 nsec_ctx->mon_lr = nsec_entry; 103 nsec_ctx->mon_spsr = CPSR_MODE_SVC | CPSR_I; 104 105 if (pos == 0) { 106 unsigned long a, s; 107 /* core malloc pool init */ 108 #ifdef CFG_TEE_MALLOC_START 109 a = CFG_TEE_MALLOC_START; 110 s = CFG_TEE_MALLOC_SIZE; 111 #else 112 a = (unsigned long)&teecore_heap_start; 113 s = (unsigned long)&teecore_heap_end; 114 a = ((a + 1) & ~0x0FFFF) + 0x10000; /* 64kB aligned */ 115 s = s & ~0x0FFFF; /* 64kB aligned */ 116 s = s - a; 117 #endif 118 malloc_init((void *)a, s); 119 120 teecore_init_ta_ram(); 121 122 if (init_teecore() != TEE_SUCCESS) { 123 panic(); 124 } 125 } 126 127 IMSG("optee initialize finished\n"); 128 } 129 130 static void main_fiq(void) 131 { 132 panic(); 133 } 134 135 static uint32_t main_default_pm_handler(uint32_t a0, uint32_t a1) 136 { 137 /* 138 * This function is not supported in this configuration, and 139 * should never be called. Panic to catch unintended calls. 140 */ 141 (void)&a0; 142 (void)&a1; 143 panic(); 144 return 1; 145 } 146 147 static void main_tee_entry(struct thread_smc_args *args) 148 { 149 /* TODO move to main_init() */ 150 if (init_teecore() != TEE_SUCCESS) 151 panic(); 152 153 /* 154 * This function first catches platform specific SMC functions 155 * if none matches, the generic tee_entry is called. 156 */ 157 if (args->a0 == TEESMC32_OPTEE_FASTCALL_GET_SHM_CONFIG) { 158 args->a0 = TEESMC_RETURN_OK; 159 args->a1 = default_nsec_shm_paddr; 160 args->a2 = default_nsec_shm_size; 161 /* Should this be TEESMC cache attributes instead? */ 162 args->a3 = core_mmu_is_shm_cached(); 163 return; 164 } 165 166 if (args->a0 == TEESMC32_OPTEE_FASTCALL_L2CC_MUTEX) { 167 switch (args->a1) { 168 case TEESMC_OPTEE_L2CC_MUTEX_GET_ADDR: 169 case TEESMC_OPTEE_L2CC_MUTEX_SET_ADDR: 170 case TEESMC_OPTEE_L2CC_MUTEX_ENABLE: 171 case TEESMC_OPTEE_L2CC_MUTEX_DISABLE: 172 /* A80 platform not support L2CC_MUTEX */ 173 args->a0 = TEESMC_RETURN_UNKNOWN_FUNCTION; 174 return; 175 default: 176 args->a0 = TEESMC_RETURN_EBADCMD; 177 return; 178 } 179 } 180 181 /* SiP Service Call Count */ 182 if (args->a0 == TEESMC32_SIP_SUNXI_CALLS_COUNT) { 183 args->a0 = 1; 184 return; 185 } 186 187 /* SiP Service Call UID */ 188 if (args->a0 == TEESMC32_SIP_SUNXI_CALLS_UID) { 189 args->a0 = TEESMC_SIP_SUNXI_UID_R0; 190 args->a1 = TEESMC_SIP_SUNXI_UID_R1; 191 args->a2 = TEESMC_SIP_SUNXI_UID_R2; 192 args->a3 = TEESMC_SIP_SUNXI_UID_R3; 193 return; 194 } 195 196 /* SiP Service Calls */ 197 if (args->a0 == TEESMC32_OPTEE_FAST_CALL_SIP_SUNXI) { 198 platform_smc_handle(args); 199 return; 200 } 201 202 tee_entry(args); 203 } 204 205 206 /* Override weak function in tee/entry.c */ 207 void tee_entry_get_api_call_count(struct thread_smc_args *args) 208 { 209 args->a0 = tee_entry_generic_get_api_call_count() + 2; 210 } 211 212 /* Override weak function in tee/entry.c */ 213 void tee_entry_get_api_uuid(struct thread_smc_args *args) 214 { 215 args->a0 = TEESMC_OPTEE_UID_R0; 216 args->a1 = TEESMC_OPTEE_UID_R1; 217 args->a2 = TEESMC_OPTEE_UID_R2; 218 args->a3 = TEESMC_OPTEE_UID32_R3; 219 } 220 221 /* Override weak function in tee/entry.c */ 222 void tee_entry_get_api_revision(struct thread_smc_args *args) 223 { 224 args->a0 = TEESMC_OPTEE_REVISION_MAJOR; 225 args->a1 = TEESMC_OPTEE_REVISION_MINOR; 226 } 227 228 /* Override weak function in tee/entry.c */ 229 void tee_entry_get_os_uuid(struct thread_smc_args *args) 230 { 231 args->a0 = TEESMC_OS_OPTEE_UUID_R0; 232 args->a1 = TEESMC_OS_OPTEE_UUID_R1; 233 args->a2 = TEESMC_OS_OPTEE_UUID_R2; 234 args->a3 = TEESMC_OS_OPTEE_UUID_R3; 235 } 236 237 /* Override weak function in tee/entry.c */ 238 void tee_entry_get_os_revision(struct thread_smc_args *args) 239 { 240 args->a0 = TEESMC_OS_OPTEE_REVISION_MAJOR; 241 args->a1 = TEESMC_OS_OPTEE_REVISION_MINOR; 242 } 243