1375f538aSAchin Gupta /* 2*fd650ff6SSoby Mathew * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved. 3375f538aSAchin Gupta * 4375f538aSAchin Gupta * Redistribution and use in source and binary forms, with or without 5375f538aSAchin Gupta * modification, are permitted provided that the following conditions are met: 6375f538aSAchin Gupta * 7375f538aSAchin Gupta * Redistributions of source code must retain the above copyright notice, this 8375f538aSAchin Gupta * list of conditions and the following disclaimer. 9375f538aSAchin Gupta * 10375f538aSAchin Gupta * Redistributions in binary form must reproduce the above copyright notice, 11375f538aSAchin Gupta * this list of conditions and the following disclaimer in the documentation 12375f538aSAchin Gupta * and/or other materials provided with the distribution. 13375f538aSAchin Gupta * 14375f538aSAchin Gupta * Neither the name of ARM nor the names of its contributors may be used 15375f538aSAchin Gupta * to endorse or promote products derived from this software without specific 16375f538aSAchin Gupta * prior written permission. 17375f538aSAchin Gupta * 18375f538aSAchin Gupta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19375f538aSAchin Gupta * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20375f538aSAchin Gupta * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21375f538aSAchin Gupta * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22375f538aSAchin Gupta * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23375f538aSAchin Gupta * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24375f538aSAchin Gupta * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25375f538aSAchin Gupta * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26375f538aSAchin Gupta * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27375f538aSAchin Gupta * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28375f538aSAchin Gupta * POSSIBILITY OF SUCH DAMAGE. 29375f538aSAchin Gupta */ 30375f538aSAchin Gupta 31375f538aSAchin Gupta 32375f538aSAchin Gupta /******************************************************************************* 33375f538aSAchin Gupta * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a 34375f538aSAchin Gupta * plug-in component to the Secure Monitor, registered as a runtime service. The 35375f538aSAchin Gupta * SPD is expected to be a functional extension of the Secure Payload (SP) that 36375f538aSAchin Gupta * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting 37375f538aSAchin Gupta * the Trusted OS/Applications range to the dispatcher. The SPD will either 38375f538aSAchin Gupta * handle the request locally or delegate it to the Secure Payload. It is also 39375f538aSAchin Gupta * responsible for initialising and maintaining communication with the SP. 40375f538aSAchin Gupta ******************************************************************************/ 41375f538aSAchin Gupta #include <arch_helpers.h> 4297043ac9SDan Handley #include <assert.h> 4397043ac9SDan Handley #include <bl_common.h> 4497043ac9SDan Handley #include <bl31.h> 45375f538aSAchin Gupta #include <context_mgmt.h> 46b44a4435SAchin Gupta #include <debug.h> 47b44a4435SAchin Gupta #include <errno.h> 48b44a4435SAchin Gupta #include <platform.h> 49375f538aSAchin Gupta #include <runtime_svc.h> 5097043ac9SDan Handley #include <stddef.h> 51f4f1ae77SSoby Mathew #include <string.h> 52375f538aSAchin Gupta #include <tsp.h> 5352538b9bSJeenu Viswambharan #include <uuid.h> 5435e98e55SDan Handley #include "tspd_private.h" 55375f538aSAchin Gupta 56375f538aSAchin Gupta /******************************************************************************* 57399fb08fSAndrew Thoelke * Address of the entrypoint vector table in the Secure Payload. It is 58399fb08fSAndrew Thoelke * initialised once on the primary core after a cold boot. 59375f538aSAchin Gupta ******************************************************************************/ 60399fb08fSAndrew Thoelke tsp_vectors_t *tsp_vectors; 61375f538aSAchin Gupta 62375f538aSAchin Gupta /******************************************************************************* 63375f538aSAchin Gupta * Array to keep track of per-cpu Secure Payload state 64375f538aSAchin Gupta ******************************************************************************/ 65fb037bfbSDan Handley tsp_context_t tspd_sp_context[TSPD_CORE_COUNT]; 66375f538aSAchin Gupta 677f366605SJeenu Viswambharan 6852538b9bSJeenu Viswambharan /* TSP UID */ 6952538b9bSJeenu Viswambharan DEFINE_SVC_UUID(tsp_uuid, 7052538b9bSJeenu Viswambharan 0x5b3056a0, 0x3291, 0x427b, 0x98, 0x11, 7152538b9bSJeenu Viswambharan 0x71, 0x68, 0xca, 0x50, 0xf3, 0xfa); 7252538b9bSJeenu Viswambharan 736871c5d3SVikram Kanigiri int32_t tspd_init(void); 747f366605SJeenu Viswambharan 75f4f1ae77SSoby Mathew uint64_t tspd_handle_sp_preemption(void *handle) 76f4f1ae77SSoby Mathew { 77f4f1ae77SSoby Mathew cpu_context_t *ns_cpu_context; 78f4f1ae77SSoby Mathew assert(handle == cm_get_context(SECURE)); 79f4f1ae77SSoby Mathew cm_el1_sysregs_context_save(SECURE); 80f4f1ae77SSoby Mathew /* Get a reference to the non-secure context */ 81f4f1ae77SSoby Mathew ns_cpu_context = cm_get_context(NON_SECURE); 82f4f1ae77SSoby Mathew assert(ns_cpu_context); 83f4f1ae77SSoby Mathew 84f4f1ae77SSoby Mathew /* 85f4f1ae77SSoby Mathew * Restore non-secure state. The secure system 86f4f1ae77SSoby Mathew * register context will be saved when required. 87f4f1ae77SSoby Mathew */ 88f4f1ae77SSoby Mathew cm_el1_sysregs_context_restore(NON_SECURE); 89f4f1ae77SSoby Mathew cm_set_next_eret_context(NON_SECURE); 90f4f1ae77SSoby Mathew 91f4f1ae77SSoby Mathew SMC_RET1(ns_cpu_context, SMC_PREEMPTED); 92f4f1ae77SSoby Mathew } 93b44a4435SAchin Gupta /******************************************************************************* 94b44a4435SAchin Gupta * This function is the handler registered for S-EL1 interrupts by the TSPD. It 95b44a4435SAchin Gupta * validates the interrupt and upon success arranges entry into the TSP at 96b44a4435SAchin Gupta * 'tsp_fiq_entry()' for handling the interrupt. 97b44a4435SAchin Gupta ******************************************************************************/ 98b44a4435SAchin Gupta static uint64_t tspd_sel1_interrupt_handler(uint32_t id, 99b44a4435SAchin Gupta uint32_t flags, 100b44a4435SAchin Gupta void *handle, 101b44a4435SAchin Gupta void *cookie) 102b44a4435SAchin Gupta { 103b44a4435SAchin Gupta uint32_t linear_id; 104b44a4435SAchin Gupta tsp_context_t *tsp_ctx; 105b44a4435SAchin Gupta 106b44a4435SAchin Gupta /* Check the security state when the exception was generated */ 107b44a4435SAchin Gupta assert(get_interrupt_src_ss(flags) == NON_SECURE); 108b44a4435SAchin Gupta 109b44a4435SAchin Gupta #if IMF_READ_INTERRUPT_ID 110b44a4435SAchin Gupta /* Check the security status of the interrupt */ 111a3781085SSoby Mathew assert(plat_ic_get_interrupt_type(id) == INTR_TYPE_S_EL1); 112b44a4435SAchin Gupta #endif 113b44a4435SAchin Gupta 114b44a4435SAchin Gupta /* Sanity check the pointer to this cpu's context */ 11508ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 116b44a4435SAchin Gupta 117b44a4435SAchin Gupta /* Save the non-secure context before entering the TSP */ 118b44a4435SAchin Gupta cm_el1_sysregs_context_save(NON_SECURE); 119b44a4435SAchin Gupta 120b44a4435SAchin Gupta /* Get a reference to this cpu's TSP context */ 121*fd650ff6SSoby Mathew linear_id = plat_my_core_pos(); 122b44a4435SAchin Gupta tsp_ctx = &tspd_sp_context[linear_id]; 12308ab89d3SAndrew Thoelke assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE)); 124b44a4435SAchin Gupta 125b44a4435SAchin Gupta /* 126b44a4435SAchin Gupta * Determine if the TSP was previously preempted. Its last known 127b44a4435SAchin Gupta * context has to be preserved in this case. 128b44a4435SAchin Gupta * The TSP should return control to the TSPD after handling this 129b44a4435SAchin Gupta * FIQ. Preserve essential EL3 context to allow entry into the 130b44a4435SAchin Gupta * TSP at the FIQ entry point using the 'cpu_context' structure. 131b44a4435SAchin Gupta * There is no need to save the secure system register context 132b44a4435SAchin Gupta * since the TSP is supposed to preserve it during S-EL1 interrupt 133b44a4435SAchin Gupta * handling. 134b44a4435SAchin Gupta */ 135b44a4435SAchin Gupta if (get_std_smc_active_flag(tsp_ctx->state)) { 136b44a4435SAchin Gupta tsp_ctx->saved_spsr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx, 137b44a4435SAchin Gupta CTX_SPSR_EL3); 138b44a4435SAchin Gupta tsp_ctx->saved_elr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx, 139b44a4435SAchin Gupta CTX_ELR_EL3); 140f4f1ae77SSoby Mathew #if TSPD_ROUTE_IRQ_TO_EL3 141f4f1ae77SSoby Mathew /*Need to save the previously interrupted secure context */ 142f4f1ae77SSoby Mathew memcpy(&tsp_ctx->sp_ctx, &tsp_ctx->cpu_ctx, TSPD_SP_CTX_SIZE); 143f4f1ae77SSoby Mathew #endif 144b44a4435SAchin Gupta } 145b44a4435SAchin Gupta 146b44a4435SAchin Gupta cm_el1_sysregs_context_restore(SECURE); 147167a9357SAndrew Thoelke cm_set_elr_spsr_el3(SECURE, (uint64_t) &tsp_vectors->fiq_entry, 148167a9357SAndrew Thoelke SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS)); 149f4f1ae77SSoby Mathew 150b44a4435SAchin Gupta cm_set_next_eret_context(SECURE); 151b44a4435SAchin Gupta 152b44a4435SAchin Gupta /* 153b44a4435SAchin Gupta * Tell the TSP that it has to handle an FIQ synchronously. Also the 154b44a4435SAchin Gupta * instruction in normal world where the interrupt was generated is 155b44a4435SAchin Gupta * passed for debugging purposes. It is safe to retrieve this address 156b44a4435SAchin Gupta * from ELR_EL3 as the secure context will not take effect until 157b44a4435SAchin Gupta * el3_exit(). 158b44a4435SAchin Gupta */ 159b44a4435SAchin Gupta SMC_RET2(&tsp_ctx->cpu_ctx, TSP_HANDLE_FIQ_AND_RETURN, read_elr_el3()); 160b44a4435SAchin Gupta } 1617f366605SJeenu Viswambharan 162f4f1ae77SSoby Mathew #if TSPD_ROUTE_IRQ_TO_EL3 163f4f1ae77SSoby Mathew /******************************************************************************* 164f4f1ae77SSoby Mathew * This function is the handler registered for S-EL1 interrupts by the TSPD. It 165f4f1ae77SSoby Mathew * validates the interrupt and upon success arranges entry into the TSP at 166f4f1ae77SSoby Mathew * 'tsp_fiq_entry()' for handling the interrupt. 167f4f1ae77SSoby Mathew ******************************************************************************/ 168f4f1ae77SSoby Mathew static uint64_t tspd_ns_interrupt_handler(uint32_t id, 169f4f1ae77SSoby Mathew uint32_t flags, 170f4f1ae77SSoby Mathew void *handle, 171f4f1ae77SSoby Mathew void *cookie) 172f4f1ae77SSoby Mathew { 173f4f1ae77SSoby Mathew /* Check the security state when the exception was generated */ 174f4f1ae77SSoby Mathew assert(get_interrupt_src_ss(flags) == SECURE); 175f4f1ae77SSoby Mathew 176f4f1ae77SSoby Mathew #if IMF_READ_INTERRUPT_ID 177f4f1ae77SSoby Mathew /* Check the security status of the interrupt */ 178f4f1ae77SSoby Mathew assert(plat_ic_get_interrupt_type(id) == INTR_TYPE_NS); 179f4f1ae77SSoby Mathew #endif 180f4f1ae77SSoby Mathew /* 181f4f1ae77SSoby Mathew * Disable the routing of NS interrupts from secure world to EL3 while 182f4f1ae77SSoby Mathew * interrupted on this core. 183f4f1ae77SSoby Mathew */ 184f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 185f4f1ae77SSoby Mathew 186f4f1ae77SSoby Mathew return tspd_handle_sp_preemption(handle); 187f4f1ae77SSoby Mathew } 188f4f1ae77SSoby Mathew #endif 189f4f1ae77SSoby Mathew 190375f538aSAchin Gupta /******************************************************************************* 191375f538aSAchin Gupta * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type 192375f538aSAchin Gupta * (aarch32/aarch64) if not already known and initialises the context for entry 193375f538aSAchin Gupta * into the SP for its initialisation. 194375f538aSAchin Gupta ******************************************************************************/ 195375f538aSAchin Gupta int32_t tspd_setup(void) 196375f538aSAchin Gupta { 19750e27dadSVikram Kanigiri entry_point_info_t *tsp_ep_info; 198375f538aSAchin Gupta uint32_t linear_id; 199375f538aSAchin Gupta 200*fd650ff6SSoby Mathew linear_id = plat_my_core_pos(); 201375f538aSAchin Gupta 202375f538aSAchin Gupta /* 203375f538aSAchin Gupta * Get information about the Secure Payload (BL32) image. Its 204375f538aSAchin Gupta * absence is a critical failure. TODO: Add support to 205375f538aSAchin Gupta * conditionally include the SPD service 206375f538aSAchin Gupta */ 20750e27dadSVikram Kanigiri tsp_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 20850e27dadSVikram Kanigiri if (!tsp_ep_info) { 20950e27dadSVikram Kanigiri WARN("No TSP provided by BL2 boot loader, Booting device" 21050e27dadSVikram Kanigiri " without TSP initialization. SMC`s destined for TSP" 21150e27dadSVikram Kanigiri " will return SMC_UNK\n"); 21250e27dadSVikram Kanigiri return 1; 21350e27dadSVikram Kanigiri } 214375f538aSAchin Gupta 215375f538aSAchin Gupta /* 2167f366605SJeenu Viswambharan * If there's no valid entry point for SP, we return a non-zero value 2177f366605SJeenu Viswambharan * signalling failure initializing the service. We bail out without 2187f366605SJeenu Viswambharan * registering any handlers 2197f366605SJeenu Viswambharan */ 22050e27dadSVikram Kanigiri if (!tsp_ep_info->pc) 2217f366605SJeenu Viswambharan return 1; 2227f366605SJeenu Viswambharan 2237f366605SJeenu Viswambharan /* 224375f538aSAchin Gupta * We could inspect the SP image and determine it's execution 225375f538aSAchin Gupta * state i.e whether AArch32 or AArch64. Assuming it's AArch64 226375f538aSAchin Gupta * for the time being. 227375f538aSAchin Gupta */ 22850e27dadSVikram Kanigiri tspd_init_tsp_ep_state(tsp_ep_info, 229375f538aSAchin Gupta TSP_AARCH64, 23050e27dadSVikram Kanigiri tsp_ep_info->pc, 231375f538aSAchin Gupta &tspd_sp_context[linear_id]); 232375f538aSAchin Gupta 233faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 234faaa2e76SVikram Kanigiri bl31_set_next_image_type(SECURE); 235faaa2e76SVikram Kanigiri #else 2367f366605SJeenu Viswambharan /* 2377f366605SJeenu Viswambharan * All TSPD initialization done. Now register our init function with 2387f366605SJeenu Viswambharan * BL31 for deferred invocation 2397f366605SJeenu Viswambharan */ 2407f366605SJeenu Viswambharan bl31_register_bl32_init(&tspd_init); 241faaa2e76SVikram Kanigiri #endif 24250e27dadSVikram Kanigiri return 0; 243375f538aSAchin Gupta } 244375f538aSAchin Gupta 245375f538aSAchin Gupta /******************************************************************************* 246375f538aSAchin Gupta * This function passes control to the Secure Payload image (BL32) for the first 247375f538aSAchin Gupta * time on the primary cpu after a cold boot. It assumes that a valid secure 248375f538aSAchin Gupta * context has already been created by tspd_setup() which can be directly used. 249375f538aSAchin Gupta * It also assumes that a valid non-secure context has been initialised by PSCI 250375f538aSAchin Gupta * so it does not need to save and restore any non-secure state. This function 251375f538aSAchin Gupta * performs a synchronous entry into the Secure payload. The SP passes control 2526871c5d3SVikram Kanigiri * back to this routine through a SMC. 253375f538aSAchin Gupta ******************************************************************************/ 2546871c5d3SVikram Kanigiri int32_t tspd_init(void) 255375f538aSAchin Gupta { 256*fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 257fb037bfbSDan Handley tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 25850e27dadSVikram Kanigiri entry_point_info_t *tsp_entry_point; 259faaa2e76SVikram Kanigiri uint64_t rc; 26050e27dadSVikram Kanigiri 26150e27dadSVikram Kanigiri /* 26250e27dadSVikram Kanigiri * Get information about the Secure Payload (BL32) image. Its 26350e27dadSVikram Kanigiri * absence is a critical failure. 26450e27dadSVikram Kanigiri */ 26550e27dadSVikram Kanigiri tsp_entry_point = bl31_plat_get_next_image_ep_info(SECURE); 26650e27dadSVikram Kanigiri assert(tsp_entry_point); 26750e27dadSVikram Kanigiri 268*fd650ff6SSoby Mathew cm_init_my_context(tsp_entry_point); 269375f538aSAchin Gupta 270375f538aSAchin Gupta /* 271faaa2e76SVikram Kanigiri * Arrange for an entry into the test secure payload. It will be 272faaa2e76SVikram Kanigiri * returned via TSP_ENTRY_DONE case 273607084eeSAchin Gupta */ 274375f538aSAchin Gupta rc = tspd_synchronous_sp_entry(tsp_ctx); 275375f538aSAchin Gupta assert(rc != 0); 276b44a4435SAchin Gupta 277375f538aSAchin Gupta return rc; 278375f538aSAchin Gupta } 279375f538aSAchin Gupta 2807f366605SJeenu Viswambharan 281375f538aSAchin Gupta /******************************************************************************* 282375f538aSAchin Gupta * This function is responsible for handling all SMCs in the Trusted OS/App 283375f538aSAchin Gupta * range from the non-secure state as defined in the SMC Calling Convention 284375f538aSAchin Gupta * Document. It is also responsible for communicating with the Secure payload 285375f538aSAchin Gupta * to delegate work and return results back to the non-secure state. Lastly it 286375f538aSAchin Gupta * will also return any information that the secure payload needs to do the 287375f538aSAchin Gupta * work assigned to it. 288375f538aSAchin Gupta ******************************************************************************/ 289375f538aSAchin Gupta uint64_t tspd_smc_handler(uint32_t smc_fid, 290375f538aSAchin Gupta uint64_t x1, 291375f538aSAchin Gupta uint64_t x2, 292375f538aSAchin Gupta uint64_t x3, 293375f538aSAchin Gupta uint64_t x4, 294375f538aSAchin Gupta void *cookie, 295375f538aSAchin Gupta void *handle, 296375f538aSAchin Gupta uint64_t flags) 297375f538aSAchin Gupta { 298fb037bfbSDan Handley cpu_context_t *ns_cpu_context; 299*fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(), ns; 300fb037bfbSDan Handley tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 301faaa2e76SVikram Kanigiri uint64_t rc; 302faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 303faaa2e76SVikram Kanigiri entry_point_info_t *next_image_info; 304faaa2e76SVikram Kanigiri #endif 305375f538aSAchin Gupta 306375f538aSAchin Gupta /* Determine which security state this SMC originated from */ 307375f538aSAchin Gupta ns = is_caller_non_secure(flags); 308375f538aSAchin Gupta 309375f538aSAchin Gupta switch (smc_fid) { 310375f538aSAchin Gupta 311375f538aSAchin Gupta /* 312239b04faSSoby Mathew * This function ID is used by TSP to indicate that it was 313239b04faSSoby Mathew * preempted by a normal world IRQ. 314239b04faSSoby Mathew * 315239b04faSSoby Mathew */ 316239b04faSSoby Mathew case TSP_PREEMPTED: 317239b04faSSoby Mathew if (ns) 318239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 319239b04faSSoby Mathew 320f4f1ae77SSoby Mathew return tspd_handle_sp_preemption(handle); 321239b04faSSoby Mathew 322239b04faSSoby Mathew /* 323b44a4435SAchin Gupta * This function ID is used only by the TSP to indicate that it has 324b44a4435SAchin Gupta * finished handling a S-EL1 FIQ interrupt. Execution should resume 325b44a4435SAchin Gupta * in the normal world. 326b44a4435SAchin Gupta */ 327b44a4435SAchin Gupta case TSP_HANDLED_S_EL1_FIQ: 328b44a4435SAchin Gupta if (ns) 329b44a4435SAchin Gupta SMC_RET1(handle, SMC_UNK); 330b44a4435SAchin Gupta 33108ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 332b44a4435SAchin Gupta 333b44a4435SAchin Gupta /* 334b44a4435SAchin Gupta * Restore the relevant EL3 state which saved to service 335b44a4435SAchin Gupta * this SMC. 336b44a4435SAchin Gupta */ 337b44a4435SAchin Gupta if (get_std_smc_active_flag(tsp_ctx->state)) { 338b44a4435SAchin Gupta SMC_SET_EL3(&tsp_ctx->cpu_ctx, 339b44a4435SAchin Gupta CTX_SPSR_EL3, 340b44a4435SAchin Gupta tsp_ctx->saved_spsr_el3); 341b44a4435SAchin Gupta SMC_SET_EL3(&tsp_ctx->cpu_ctx, 342b44a4435SAchin Gupta CTX_ELR_EL3, 343b44a4435SAchin Gupta tsp_ctx->saved_elr_el3); 344f4f1ae77SSoby Mathew #if TSPD_ROUTE_IRQ_TO_EL3 345f4f1ae77SSoby Mathew /* 346f4f1ae77SSoby Mathew * Need to restore the previously interrupted 347f4f1ae77SSoby Mathew * secure context. 348f4f1ae77SSoby Mathew */ 349f4f1ae77SSoby Mathew memcpy(&tsp_ctx->cpu_ctx, &tsp_ctx->sp_ctx, 350f4f1ae77SSoby Mathew TSPD_SP_CTX_SIZE); 351f4f1ae77SSoby Mathew #endif 352b44a4435SAchin Gupta } 353b44a4435SAchin Gupta 354b44a4435SAchin Gupta /* Get a reference to the non-secure context */ 35508ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 356b44a4435SAchin Gupta assert(ns_cpu_context); 357b44a4435SAchin Gupta 358b44a4435SAchin Gupta /* 359b44a4435SAchin Gupta * Restore non-secure state. There is no need to save the 360b44a4435SAchin Gupta * secure system register context since the TSP was supposed 361b44a4435SAchin Gupta * to preserve it during S-EL1 interrupt handling. 362b44a4435SAchin Gupta */ 363b44a4435SAchin Gupta cm_el1_sysregs_context_restore(NON_SECURE); 364b44a4435SAchin Gupta cm_set_next_eret_context(NON_SECURE); 365b44a4435SAchin Gupta 366b44a4435SAchin Gupta SMC_RET0((uint64_t) ns_cpu_context); 367b44a4435SAchin Gupta 368b44a4435SAchin Gupta 369b44a4435SAchin Gupta /* 370b44a4435SAchin Gupta * This function ID is used only by the TSP to indicate that it was 371b44a4435SAchin Gupta * interrupted due to a EL3 FIQ interrupt. Execution should resume 372b44a4435SAchin Gupta * in the normal world. 373b44a4435SAchin Gupta */ 374b44a4435SAchin Gupta case TSP_EL3_FIQ: 375b44a4435SAchin Gupta if (ns) 376b44a4435SAchin Gupta SMC_RET1(handle, SMC_UNK); 377b44a4435SAchin Gupta 37808ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 379b44a4435SAchin Gupta 380b44a4435SAchin Gupta /* Assert that standard SMC execution has been preempted */ 381b44a4435SAchin Gupta assert(get_std_smc_active_flag(tsp_ctx->state)); 382b44a4435SAchin Gupta 383b44a4435SAchin Gupta /* Save the secure system register state */ 384b44a4435SAchin Gupta cm_el1_sysregs_context_save(SECURE); 385b44a4435SAchin Gupta 386b44a4435SAchin Gupta /* Get a reference to the non-secure context */ 38708ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 388b44a4435SAchin Gupta assert(ns_cpu_context); 389b44a4435SAchin Gupta 390b44a4435SAchin Gupta /* Restore non-secure state */ 391b44a4435SAchin Gupta cm_el1_sysregs_context_restore(NON_SECURE); 392b44a4435SAchin Gupta cm_set_next_eret_context(NON_SECURE); 393b44a4435SAchin Gupta 394b44a4435SAchin Gupta SMC_RET1(ns_cpu_context, TSP_EL3_FIQ); 395b44a4435SAchin Gupta 396b44a4435SAchin Gupta 397b44a4435SAchin Gupta /* 398375f538aSAchin Gupta * This function ID is used only by the SP to indicate it has 399375f538aSAchin Gupta * finished initialising itself after a cold boot 400375f538aSAchin Gupta */ 401375f538aSAchin Gupta case TSP_ENTRY_DONE: 402375f538aSAchin Gupta if (ns) 403375f538aSAchin Gupta SMC_RET1(handle, SMC_UNK); 404375f538aSAchin Gupta 405375f538aSAchin Gupta /* 406375f538aSAchin Gupta * Stash the SP entry points information. This is done 407375f538aSAchin Gupta * only once on the primary cpu 408375f538aSAchin Gupta */ 409399fb08fSAndrew Thoelke assert(tsp_vectors == NULL); 410399fb08fSAndrew Thoelke tsp_vectors = (tsp_vectors_t *) x1; 411375f538aSAchin Gupta 412faaa2e76SVikram Kanigiri if (tsp_vectors) { 413faaa2e76SVikram Kanigiri set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON); 414faaa2e76SVikram Kanigiri 415faaa2e76SVikram Kanigiri /* 416faaa2e76SVikram Kanigiri * TSP has been successfully initialized. Register power 417faaa2e76SVikram Kanigiri * managemnt hooks with PSCI 418faaa2e76SVikram Kanigiri */ 419faaa2e76SVikram Kanigiri psci_register_spd_pm_hook(&tspd_pm); 420faaa2e76SVikram Kanigiri 421faaa2e76SVikram Kanigiri /* 422faaa2e76SVikram Kanigiri * Register an interrupt handler for S-EL1 interrupts 423faaa2e76SVikram Kanigiri * when generated during code executing in the 424faaa2e76SVikram Kanigiri * non-secure state. 425faaa2e76SVikram Kanigiri */ 426faaa2e76SVikram Kanigiri flags = 0; 427faaa2e76SVikram Kanigiri set_interrupt_rm_flag(flags, NON_SECURE); 428faaa2e76SVikram Kanigiri rc = register_interrupt_type_handler(INTR_TYPE_S_EL1, 429faaa2e76SVikram Kanigiri tspd_sel1_interrupt_handler, 430faaa2e76SVikram Kanigiri flags); 431faaa2e76SVikram Kanigiri if (rc) 432faaa2e76SVikram Kanigiri panic(); 433f4f1ae77SSoby Mathew 434f4f1ae77SSoby Mathew #if TSPD_ROUTE_IRQ_TO_EL3 435f4f1ae77SSoby Mathew /* 436f4f1ae77SSoby Mathew * Register an interrupt handler for NS interrupts when 437f4f1ae77SSoby Mathew * generated during code executing in secure state are 438f4f1ae77SSoby Mathew * routed to EL3. 439f4f1ae77SSoby Mathew */ 440f4f1ae77SSoby Mathew flags = 0; 441f4f1ae77SSoby Mathew set_interrupt_rm_flag(flags, SECURE); 442f4f1ae77SSoby Mathew 443f4f1ae77SSoby Mathew rc = register_interrupt_type_handler(INTR_TYPE_NS, 444f4f1ae77SSoby Mathew tspd_ns_interrupt_handler, 445f4f1ae77SSoby Mathew flags); 446f4f1ae77SSoby Mathew if (rc) 447f4f1ae77SSoby Mathew panic(); 448f4f1ae77SSoby Mathew 449f4f1ae77SSoby Mathew /* 450f4f1ae77SSoby Mathew * Disable the interrupt NS locally since it will be enabled globally 451*fd650ff6SSoby Mathew * within cm_init_my_context. 452f4f1ae77SSoby Mathew */ 453f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 454f4f1ae77SSoby Mathew #endif 455faaa2e76SVikram Kanigiri } 456faaa2e76SVikram Kanigiri 457faaa2e76SVikram Kanigiri 458faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 459faaa2e76SVikram Kanigiri /* Save the Secure EL1 system register context */ 460faaa2e76SVikram Kanigiri assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx); 461faaa2e76SVikram Kanigiri cm_el1_sysregs_context_save(SECURE); 462faaa2e76SVikram Kanigiri 463faaa2e76SVikram Kanigiri /* Program EL3 registers to enable entry into the next EL */ 464faaa2e76SVikram Kanigiri next_image_info = bl31_plat_get_next_image_ep_info(NON_SECURE); 465faaa2e76SVikram Kanigiri assert(next_image_info); 466faaa2e76SVikram Kanigiri assert(NON_SECURE == 467faaa2e76SVikram Kanigiri GET_SECURITY_STATE(next_image_info->h.attr)); 468faaa2e76SVikram Kanigiri 469*fd650ff6SSoby Mathew cm_init_my_context(next_image_info); 470faaa2e76SVikram Kanigiri cm_prepare_el3_exit(NON_SECURE); 471faaa2e76SVikram Kanigiri SMC_RET0(cm_get_context(NON_SECURE)); 472faaa2e76SVikram Kanigiri #else 473375f538aSAchin Gupta /* 474375f538aSAchin Gupta * SP reports completion. The SPD must have initiated 475375f538aSAchin Gupta * the original request through a synchronous entry 476375f538aSAchin Gupta * into the SP. Jump back to the original C runtime 477375f538aSAchin Gupta * context. 478375f538aSAchin Gupta */ 479916a2c1eSAchin Gupta tspd_synchronous_sp_exit(tsp_ctx, x1); 480faaa2e76SVikram Kanigiri #endif 481375f538aSAchin Gupta 482607084eeSAchin Gupta /* 483607084eeSAchin Gupta * These function IDs is used only by the SP to indicate it has 484607084eeSAchin Gupta * finished: 485607084eeSAchin Gupta * 1. turning itself on in response to an earlier psci 486607084eeSAchin Gupta * cpu_on request 487607084eeSAchin Gupta * 2. resuming itself after an earlier psci cpu_suspend 488607084eeSAchin Gupta * request. 489607084eeSAchin Gupta */ 490607084eeSAchin Gupta case TSP_ON_DONE: 491607084eeSAchin Gupta case TSP_RESUME_DONE: 492607084eeSAchin Gupta 493607084eeSAchin Gupta /* 494607084eeSAchin Gupta * These function IDs is used only by the SP to indicate it has 495607084eeSAchin Gupta * finished: 496607084eeSAchin Gupta * 1. suspending itself after an earlier psci cpu_suspend 497607084eeSAchin Gupta * request. 498607084eeSAchin Gupta * 2. turning itself off in response to an earlier psci 499607084eeSAchin Gupta * cpu_off request. 500607084eeSAchin Gupta */ 501607084eeSAchin Gupta case TSP_OFF_DONE: 502607084eeSAchin Gupta case TSP_SUSPEND_DONE: 503d5f13093SJuan Castillo case TSP_SYSTEM_OFF_DONE: 504d5f13093SJuan Castillo case TSP_SYSTEM_RESET_DONE: 505607084eeSAchin Gupta if (ns) 506607084eeSAchin Gupta SMC_RET1(handle, SMC_UNK); 507607084eeSAchin Gupta 508607084eeSAchin Gupta /* 509607084eeSAchin Gupta * SP reports completion. The SPD must have initiated the 510607084eeSAchin Gupta * original request through a synchronous entry into the SP. 511607084eeSAchin Gupta * Jump back to the original C runtime context, and pass x1 as 512607084eeSAchin Gupta * return value to the caller 513607084eeSAchin Gupta */ 514916a2c1eSAchin Gupta tspd_synchronous_sp_exit(tsp_ctx, x1); 515607084eeSAchin Gupta 516916a2c1eSAchin Gupta /* 517916a2c1eSAchin Gupta * Request from non-secure client to perform an 518916a2c1eSAchin Gupta * arithmetic operation or response from secure 519916a2c1eSAchin Gupta * payload to an earlier request. 520916a2c1eSAchin Gupta */ 521239b04faSSoby Mathew case TSP_FAST_FID(TSP_ADD): 522239b04faSSoby Mathew case TSP_FAST_FID(TSP_SUB): 523239b04faSSoby Mathew case TSP_FAST_FID(TSP_MUL): 524239b04faSSoby Mathew case TSP_FAST_FID(TSP_DIV): 525239b04faSSoby Mathew 526239b04faSSoby Mathew case TSP_STD_FID(TSP_ADD): 527239b04faSSoby Mathew case TSP_STD_FID(TSP_SUB): 528239b04faSSoby Mathew case TSP_STD_FID(TSP_MUL): 529239b04faSSoby Mathew case TSP_STD_FID(TSP_DIV): 530916a2c1eSAchin Gupta if (ns) { 531916a2c1eSAchin Gupta /* 532916a2c1eSAchin Gupta * This is a fresh request from the non-secure client. 533916a2c1eSAchin Gupta * The parameters are in x1 and x2. Figure out which 534916a2c1eSAchin Gupta * registers need to be preserved, save the non-secure 535916a2c1eSAchin Gupta * state and send the request to the secure payload. 536916a2c1eSAchin Gupta */ 53708ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 538239b04faSSoby Mathew 539239b04faSSoby Mathew /* Check if we are already preempted */ 540239b04faSSoby Mathew if (get_std_smc_active_flag(tsp_ctx->state)) 541239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 542239b04faSSoby Mathew 543916a2c1eSAchin Gupta cm_el1_sysregs_context_save(NON_SECURE); 544916a2c1eSAchin Gupta 545916a2c1eSAchin Gupta /* Save x1 and x2 for use by TSP_GET_ARGS call below */ 546239b04faSSoby Mathew store_tsp_args(tsp_ctx, x1, x2); 547916a2c1eSAchin Gupta 548916a2c1eSAchin Gupta /* 549916a2c1eSAchin Gupta * We are done stashing the non-secure context. Ask the 550916a2c1eSAchin Gupta * secure payload to do the work now. 551916a2c1eSAchin Gupta */ 552916a2c1eSAchin Gupta 553916a2c1eSAchin Gupta /* 554916a2c1eSAchin Gupta * Verify if there is a valid context to use, copy the 555916a2c1eSAchin Gupta * operation type and parameters to the secure context 556916a2c1eSAchin Gupta * and jump to the fast smc entry point in the secure 557916a2c1eSAchin Gupta * payload. Entry into S-EL1 will take place upon exit 558916a2c1eSAchin Gupta * from this function. 559916a2c1eSAchin Gupta */ 56008ab89d3SAndrew Thoelke assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE)); 561239b04faSSoby Mathew 562239b04faSSoby Mathew /* Set appropriate entry for SMC. 563239b04faSSoby Mathew * We expect the TSP to manage the PSTATE.I and PSTATE.F 564239b04faSSoby Mathew * flags as appropriate. 565239b04faSSoby Mathew */ 566239b04faSSoby Mathew if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_FAST) { 567239b04faSSoby Mathew cm_set_elr_el3(SECURE, (uint64_t) 568399fb08fSAndrew Thoelke &tsp_vectors->fast_smc_entry); 569239b04faSSoby Mathew } else { 570239b04faSSoby Mathew set_std_smc_active_flag(tsp_ctx->state); 571239b04faSSoby Mathew cm_set_elr_el3(SECURE, (uint64_t) 572399fb08fSAndrew Thoelke &tsp_vectors->std_smc_entry); 573f4f1ae77SSoby Mathew #if TSPD_ROUTE_IRQ_TO_EL3 574f4f1ae77SSoby Mathew /* 575f4f1ae77SSoby Mathew * Enable the routing of NS interrupts to EL3 576f4f1ae77SSoby Mathew * during STD SMC processing on this core. 577f4f1ae77SSoby Mathew */ 578f4f1ae77SSoby Mathew enable_intr_rm_local(INTR_TYPE_NS, SECURE); 579f4f1ae77SSoby Mathew #endif 580239b04faSSoby Mathew } 581239b04faSSoby Mathew 582916a2c1eSAchin Gupta cm_el1_sysregs_context_restore(SECURE); 583916a2c1eSAchin Gupta cm_set_next_eret_context(SECURE); 584239b04faSSoby Mathew SMC_RET3(&tsp_ctx->cpu_ctx, smc_fid, x1, x2); 585916a2c1eSAchin Gupta } else { 586916a2c1eSAchin Gupta /* 587916a2c1eSAchin Gupta * This is the result from the secure client of an 588239b04faSSoby Mathew * earlier request. The results are in x1-x3. Copy it 589916a2c1eSAchin Gupta * into the non-secure context, save the secure state 590916a2c1eSAchin Gupta * and return to the non-secure state. 591916a2c1eSAchin Gupta */ 59208ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 593916a2c1eSAchin Gupta cm_el1_sysregs_context_save(SECURE); 594916a2c1eSAchin Gupta 595916a2c1eSAchin Gupta /* Get a reference to the non-secure context */ 59608ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 597916a2c1eSAchin Gupta assert(ns_cpu_context); 598916a2c1eSAchin Gupta 599916a2c1eSAchin Gupta /* Restore non-secure state */ 600916a2c1eSAchin Gupta cm_el1_sysregs_context_restore(NON_SECURE); 601916a2c1eSAchin Gupta cm_set_next_eret_context(NON_SECURE); 602f4f1ae77SSoby Mathew if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_STD) { 603239b04faSSoby Mathew clr_std_smc_active_flag(tsp_ctx->state); 604f4f1ae77SSoby Mathew #if TSPD_ROUTE_IRQ_TO_EL3 605f4f1ae77SSoby Mathew /* 606f4f1ae77SSoby Mathew * Disable the routing of NS interrupts to EL3 607f4f1ae77SSoby Mathew * after STD SMC processing is finished on this 608f4f1ae77SSoby Mathew * core. 609f4f1ae77SSoby Mathew */ 610f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 611f4f1ae77SSoby Mathew #endif 612f4f1ae77SSoby Mathew } 613f4f1ae77SSoby Mathew 614239b04faSSoby Mathew SMC_RET3(ns_cpu_context, x1, x2, x3); 615916a2c1eSAchin Gupta } 616916a2c1eSAchin Gupta 617916a2c1eSAchin Gupta break; 618916a2c1eSAchin Gupta 619916a2c1eSAchin Gupta /* 620239b04faSSoby Mathew * Request from non secure world to resume the preempted 621239b04faSSoby Mathew * Standard SMC call. 622239b04faSSoby Mathew */ 623239b04faSSoby Mathew case TSP_FID_RESUME: 624239b04faSSoby Mathew /* RESUME should be invoked only by normal world */ 625239b04faSSoby Mathew if (!ns) { 626239b04faSSoby Mathew assert(0); 627239b04faSSoby Mathew break; 628239b04faSSoby Mathew } 629239b04faSSoby Mathew 630239b04faSSoby Mathew /* 631239b04faSSoby Mathew * This is a resume request from the non-secure client. 632239b04faSSoby Mathew * save the non-secure state and send the request to 633239b04faSSoby Mathew * the secure payload. 634239b04faSSoby Mathew */ 63508ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 636239b04faSSoby Mathew 637239b04faSSoby Mathew /* Check if we are already preempted before resume */ 638239b04faSSoby Mathew if (!get_std_smc_active_flag(tsp_ctx->state)) 639239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 640239b04faSSoby Mathew 641239b04faSSoby Mathew cm_el1_sysregs_context_save(NON_SECURE); 642239b04faSSoby Mathew 643239b04faSSoby Mathew /* 644239b04faSSoby Mathew * We are done stashing the non-secure context. Ask the 645239b04faSSoby Mathew * secure payload to do the work now. 646239b04faSSoby Mathew */ 647f4f1ae77SSoby Mathew #if TSPD_ROUTE_IRQ_TO_EL3 648f4f1ae77SSoby Mathew /* 649f4f1ae77SSoby Mathew * Enable the routing of NS interrupts to EL3 during resumption 650f4f1ae77SSoby Mathew * of STD SMC call on this core. 651f4f1ae77SSoby Mathew */ 652f4f1ae77SSoby Mathew enable_intr_rm_local(INTR_TYPE_NS, SECURE); 653f4f1ae77SSoby Mathew #endif 654f4f1ae77SSoby Mathew 655f4f1ae77SSoby Mathew 656239b04faSSoby Mathew 657239b04faSSoby Mathew /* We just need to return to the preempted point in 658239b04faSSoby Mathew * TSP and the execution will resume as normal. 659239b04faSSoby Mathew */ 660239b04faSSoby Mathew cm_el1_sysregs_context_restore(SECURE); 661239b04faSSoby Mathew cm_set_next_eret_context(SECURE); 66210b65ecfSSoby Mathew SMC_RET0(&tsp_ctx->cpu_ctx); 663239b04faSSoby Mathew 664239b04faSSoby Mathew /* 665916a2c1eSAchin Gupta * This is a request from the secure payload for more arguments 666916a2c1eSAchin Gupta * for an ongoing arithmetic operation requested by the 667916a2c1eSAchin Gupta * non-secure world. Simply return the arguments from the non- 668916a2c1eSAchin Gupta * secure client in the original call. 669916a2c1eSAchin Gupta */ 670916a2c1eSAchin Gupta case TSP_GET_ARGS: 671916a2c1eSAchin Gupta if (ns) 672916a2c1eSAchin Gupta SMC_RET1(handle, SMC_UNK); 673916a2c1eSAchin Gupta 674239b04faSSoby Mathew get_tsp_args(tsp_ctx, x1, x2); 675239b04faSSoby Mathew SMC_RET2(handle, x1, x2); 676916a2c1eSAchin Gupta 67752538b9bSJeenu Viswambharan case TOS_CALL_COUNT: 67852538b9bSJeenu Viswambharan /* 67952538b9bSJeenu Viswambharan * Return the number of service function IDs implemented to 68052538b9bSJeenu Viswambharan * provide service to non-secure 68152538b9bSJeenu Viswambharan */ 68252538b9bSJeenu Viswambharan SMC_RET1(handle, TSP_NUM_FID); 68352538b9bSJeenu Viswambharan 68452538b9bSJeenu Viswambharan case TOS_UID: 68552538b9bSJeenu Viswambharan /* Return TSP UID to the caller */ 68652538b9bSJeenu Viswambharan SMC_UUID_RET(handle, tsp_uuid); 68752538b9bSJeenu Viswambharan 68852538b9bSJeenu Viswambharan case TOS_CALL_VERSION: 68952538b9bSJeenu Viswambharan /* Return the version of current implementation */ 69052538b9bSJeenu Viswambharan SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR); 69152538b9bSJeenu Viswambharan 692375f538aSAchin Gupta default: 693607084eeSAchin Gupta break; 694375f538aSAchin Gupta } 695375f538aSAchin Gupta 696607084eeSAchin Gupta SMC_RET1(handle, SMC_UNK); 697375f538aSAchin Gupta } 698375f538aSAchin Gupta 699239b04faSSoby Mathew /* Define a SPD runtime service descriptor for fast SMC calls */ 700375f538aSAchin Gupta DECLARE_RT_SVC( 701239b04faSSoby Mathew tspd_fast, 702375f538aSAchin Gupta 703375f538aSAchin Gupta OEN_TOS_START, 704375f538aSAchin Gupta OEN_TOS_END, 705375f538aSAchin Gupta SMC_TYPE_FAST, 706375f538aSAchin Gupta tspd_setup, 707375f538aSAchin Gupta tspd_smc_handler 708375f538aSAchin Gupta ); 709239b04faSSoby Mathew 710239b04faSSoby Mathew /* Define a SPD runtime service descriptor for standard SMC calls */ 711239b04faSSoby Mathew DECLARE_RT_SVC( 712239b04faSSoby Mathew tspd_std, 713239b04faSSoby Mathew 714239b04faSSoby Mathew OEN_TOS_START, 715239b04faSSoby Mathew OEN_TOS_END, 716239b04faSSoby Mathew SMC_TYPE_STD, 717239b04faSSoby Mathew NULL, 718239b04faSSoby Mathew tspd_smc_handler 719239b04faSSoby Mathew ); 720