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/pm_stubs.h> 44 #include <kernel/misc.h> 45 #include <mm/tee_pager.h> 46 #include <mm/tee_mmu.h> 47 #include <mm/core_mmu.h> 48 #include <mm/tee_mmu_defs.h> 49 #include <tee/entry_std.h> 50 #include <tee/entry_fast.h> 51 #include <tee/arch_svc.h> 52 #include <platform.h> 53 #include <util.h> 54 #include <trace.h> 55 #include <malloc.h> 56 57 /* teecore heap address/size is defined in scatter file */ 58 extern unsigned char teecore_heap_start; 59 extern unsigned char teecore_heap_end; 60 61 static void main_fiq(void); 62 static void main_tee_entry_std(struct thread_smc_args *args); 63 static void main_tee_entry_fast(struct thread_smc_args *args); 64 65 static const struct thread_handlers handlers = { 66 .std_smc = main_tee_entry_std, 67 .fast_smc = main_tee_entry_fast, 68 .fiq = main_fiq, 69 .svc = tee_svc_handler, 70 .abort = tee_pager_abort_handler, 71 .cpu_on = pm_panic, 72 .cpu_off = pm_panic, 73 .cpu_suspend = pm_panic, 74 .cpu_resume = pm_panic, 75 .system_off = pm_panic, 76 .system_reset = pm_panic, 77 }; 78 79 void main_init(uint32_t nsec_entry); /* called from assembly only */ 80 void main_init(uint32_t nsec_entry) 81 { 82 struct sm_nsec_ctx *nsec_ctx; 83 size_t pos = get_core_pos(); 84 85 /* 86 * Mask IRQ and FIQ before switch to the thread vector as the 87 * thread handler requires IRQ and FIQ to be masked while executing 88 * with the temporary stack. The thread subsystem also asserts that 89 * IRQ is blocked when using most if its functions. 90 */ 91 thread_mask_exceptions(THREAD_EXCP_FIQ | THREAD_EXCP_IRQ); 92 93 if (pos == 0) { 94 thread_init_primary(&handlers); 95 96 /* initialize platform */ 97 platform_init(); 98 } 99 100 thread_init_per_cpu(); 101 102 /* Initialize secure monitor */ 103 nsec_ctx = sm_get_nsec_ctx(); 104 nsec_ctx->mon_lr = nsec_entry; 105 nsec_ctx->mon_spsr = CPSR_MODE_SVC | CPSR_I; 106 107 if (pos == 0) { 108 unsigned long a, s; 109 /* core malloc pool init */ 110 #ifdef CFG_TEE_MALLOC_START 111 a = CFG_TEE_MALLOC_START; 112 s = CFG_TEE_MALLOC_SIZE; 113 #else 114 a = (unsigned long)&teecore_heap_start; 115 s = (unsigned long)&teecore_heap_end; 116 a = ((a + 1) & ~0x0FFFF) + 0x10000; /* 64kB aligned */ 117 s = s & ~0x0FFFF; /* 64kB aligned */ 118 s = s - a; 119 #endif 120 malloc_add_pool((void *)a, s); 121 122 teecore_init_ta_ram(); 123 124 if (init_teecore() != TEE_SUCCESS) { 125 panic(); 126 } 127 } 128 129 IMSG("optee initialize finished\n"); 130 } 131 132 static void main_fiq(void) 133 { 134 panic(); 135 } 136 137 static void main_tee_entry_fast(struct thread_smc_args *args) 138 { 139 /* TODO move to main_init() */ 140 if (init_teecore() != TEE_SUCCESS) 141 panic(); 142 143 /* SiP Service Call Count */ 144 if (args->a0 == TEESMC32_SIP_SUNXI_CALLS_COUNT) { 145 args->a0 = 1; 146 return; 147 } 148 149 /* SiP Service Call UID */ 150 if (args->a0 == TEESMC32_SIP_SUNXI_CALLS_UID) { 151 args->a0 = TEESMC_SIP_SUNXI_UID_R0; 152 args->a1 = TEESMC_SIP_SUNXI_UID_R1; 153 args->a2 = TEESMC_SIP_SUNXI_UID_R2; 154 args->a3 = TEESMC_SIP_SUNXI_UID_R3; 155 return; 156 } 157 158 /* SiP Service Calls */ 159 if (args->a0 == TEESMC32_OPTEE_FAST_CALL_SIP_SUNXI) { 160 platform_smc_handle(args); 161 return; 162 } 163 164 tee_entry_fast(args); 165 } 166 167 168 169 static void main_tee_entry_std(struct thread_smc_args *args) 170 { 171 /* TODO move to main_init() */ 172 if (init_teecore() != TEE_SUCCESS) 173 panic(); 174 175 tee_entry_std(args); 176 } 177 178 /* main_tee_entry_fast() supports 3 platform-specific functions */ 179 void tee_entry_get_api_call_count(struct thread_smc_args *args) 180 { 181 args->a0 = tee_entry_generic_get_api_call_count() + 3; 182 } 183