1375f538aSAchin Gupta /* 2fd650ff6SSoby 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 75404dba53SSoby Mathew /* 76404dba53SSoby Mathew * This helper function handles Secure EL1 preemption. The preemption could be 77404dba53SSoby Mathew * due Non Secure interrupts or EL3 interrupts. In both the cases we context 78404dba53SSoby Mathew * switch to the normal world and in case of EL3 interrupts, it will again be 79404dba53SSoby Mathew * routed to EL3 which will get handled at the exception vectors. 80404dba53SSoby Mathew */ 81f4f1ae77SSoby Mathew uint64_t tspd_handle_sp_preemption(void *handle) 82f4f1ae77SSoby Mathew { 83f4f1ae77SSoby Mathew cpu_context_t *ns_cpu_context; 84404dba53SSoby Mathew 85f4f1ae77SSoby Mathew assert(handle == cm_get_context(SECURE)); 86f4f1ae77SSoby Mathew cm_el1_sysregs_context_save(SECURE); 87f4f1ae77SSoby Mathew /* Get a reference to the non-secure context */ 88f4f1ae77SSoby Mathew ns_cpu_context = cm_get_context(NON_SECURE); 89f4f1ae77SSoby Mathew assert(ns_cpu_context); 90f4f1ae77SSoby Mathew 91f4f1ae77SSoby Mathew /* 9263b8440fSSoby Mathew * To allow Secure EL1 interrupt handler to re-enter TSP while TSP 9363b8440fSSoby Mathew * is preempted, the secure system register context which will get 9463b8440fSSoby Mathew * overwritten must be additionally saved. This is currently done 9563b8440fSSoby Mathew * by the TSPD S-EL1 interrupt handler. 9663b8440fSSoby Mathew */ 9763b8440fSSoby Mathew 9863b8440fSSoby Mathew /* 9963b8440fSSoby Mathew * Restore non-secure state. 100f4f1ae77SSoby Mathew */ 101f4f1ae77SSoby Mathew cm_el1_sysregs_context_restore(NON_SECURE); 102f4f1ae77SSoby Mathew cm_set_next_eret_context(NON_SECURE); 103f4f1ae77SSoby Mathew 104404dba53SSoby Mathew /* 10563b8440fSSoby Mathew * The TSP was preempted during STD SMC execution. 10663b8440fSSoby Mathew * Return back to the normal world with SMC_PREEMPTED as error 10763b8440fSSoby Mathew * code in x0. 108404dba53SSoby Mathew */ 109f4f1ae77SSoby Mathew SMC_RET1(ns_cpu_context, SMC_PREEMPTED); 110f4f1ae77SSoby Mathew } 111404dba53SSoby Mathew 112b44a4435SAchin Gupta /******************************************************************************* 113b44a4435SAchin Gupta * This function is the handler registered for S-EL1 interrupts by the TSPD. It 114b44a4435SAchin Gupta * validates the interrupt and upon success arranges entry into the TSP at 11502446137SSoby Mathew * 'tsp_sel1_intr_entry()' for handling the interrupt. 116b44a4435SAchin Gupta ******************************************************************************/ 117b44a4435SAchin Gupta static uint64_t tspd_sel1_interrupt_handler(uint32_t id, 118b44a4435SAchin Gupta uint32_t flags, 119b44a4435SAchin Gupta void *handle, 120b44a4435SAchin Gupta void *cookie) 121b44a4435SAchin Gupta { 122b44a4435SAchin Gupta uint32_t linear_id; 123b44a4435SAchin Gupta tsp_context_t *tsp_ctx; 124b44a4435SAchin Gupta 125b44a4435SAchin Gupta /* Check the security state when the exception was generated */ 126b44a4435SAchin Gupta assert(get_interrupt_src_ss(flags) == NON_SECURE); 127b44a4435SAchin Gupta 128b44a4435SAchin Gupta /* Sanity check the pointer to this cpu's context */ 12908ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 130b44a4435SAchin Gupta 131b44a4435SAchin Gupta /* Save the non-secure context before entering the TSP */ 132b44a4435SAchin Gupta cm_el1_sysregs_context_save(NON_SECURE); 133b44a4435SAchin Gupta 134b44a4435SAchin Gupta /* Get a reference to this cpu's TSP context */ 135fd650ff6SSoby Mathew linear_id = plat_my_core_pos(); 136b44a4435SAchin Gupta tsp_ctx = &tspd_sp_context[linear_id]; 13708ab89d3SAndrew Thoelke assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE)); 138b44a4435SAchin Gupta 139b44a4435SAchin Gupta /* 140b44a4435SAchin Gupta * Determine if the TSP was previously preempted. Its last known 141b44a4435SAchin Gupta * context has to be preserved in this case. 142b44a4435SAchin Gupta * The TSP should return control to the TSPD after handling this 14302446137SSoby Mathew * S-EL1 interrupt. Preserve essential EL3 context to allow entry into 14402446137SSoby Mathew * the TSP at the S-EL1 interrupt entry point using the 'cpu_context' 14502446137SSoby Mathew * structure. There is no need to save the secure system register 14602446137SSoby Mathew * context since the TSP is supposed to preserve it during S-EL1 14702446137SSoby Mathew * interrupt handling. 148b44a4435SAchin Gupta */ 149b44a4435SAchin Gupta if (get_std_smc_active_flag(tsp_ctx->state)) { 150b44a4435SAchin Gupta tsp_ctx->saved_spsr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx, 151b44a4435SAchin Gupta CTX_SPSR_EL3); 152b44a4435SAchin Gupta tsp_ctx->saved_elr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx, 153b44a4435SAchin Gupta CTX_ELR_EL3); 15402446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 155f4f1ae77SSoby Mathew /*Need to save the previously interrupted secure context */ 156f4f1ae77SSoby Mathew memcpy(&tsp_ctx->sp_ctx, &tsp_ctx->cpu_ctx, TSPD_SP_CTX_SIZE); 157f4f1ae77SSoby Mathew #endif 158b44a4435SAchin Gupta } 159b44a4435SAchin Gupta 160b44a4435SAchin Gupta cm_el1_sysregs_context_restore(SECURE); 16102446137SSoby Mathew cm_set_elr_spsr_el3(SECURE, (uint64_t) &tsp_vectors->sel1_intr_entry, 162167a9357SAndrew Thoelke SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS)); 163f4f1ae77SSoby Mathew 164b44a4435SAchin Gupta cm_set_next_eret_context(SECURE); 165b44a4435SAchin Gupta 166b44a4435SAchin Gupta /* 16702446137SSoby Mathew * Tell the TSP that it has to handle a S-EL1 interrupt synchronously. 16802446137SSoby Mathew * Also the instruction in normal world where the interrupt was 16902446137SSoby Mathew * generated is passed for debugging purposes. It is safe to retrieve 17002446137SSoby Mathew * this address from ELR_EL3 as the secure context will not take effect 17102446137SSoby Mathew * until el3_exit(). 172b44a4435SAchin Gupta */ 17302446137SSoby Mathew SMC_RET2(&tsp_ctx->cpu_ctx, TSP_HANDLE_SEL1_INTR_AND_RETURN, read_elr_el3()); 174b44a4435SAchin Gupta } 1757f366605SJeenu Viswambharan 17602446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 177f4f1ae77SSoby Mathew /******************************************************************************* 17802446137SSoby Mathew * This function is the handler registered for Non secure interrupts by the 17902446137SSoby Mathew * TSPD. It validates the interrupt and upon success arranges entry into the 18002446137SSoby Mathew * normal world for handling the interrupt. 181f4f1ae77SSoby Mathew ******************************************************************************/ 182f4f1ae77SSoby Mathew static uint64_t tspd_ns_interrupt_handler(uint32_t id, 183f4f1ae77SSoby Mathew uint32_t flags, 184f4f1ae77SSoby Mathew void *handle, 185f4f1ae77SSoby Mathew void *cookie) 186f4f1ae77SSoby Mathew { 187f4f1ae77SSoby Mathew /* Check the security state when the exception was generated */ 188f4f1ae77SSoby Mathew assert(get_interrupt_src_ss(flags) == SECURE); 189f4f1ae77SSoby Mathew 190f4f1ae77SSoby Mathew /* 191f4f1ae77SSoby Mathew * Disable the routing of NS interrupts from secure world to EL3 while 192f4f1ae77SSoby Mathew * interrupted on this core. 193f4f1ae77SSoby Mathew */ 194f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 195f4f1ae77SSoby Mathew 196f4f1ae77SSoby Mathew return tspd_handle_sp_preemption(handle); 197f4f1ae77SSoby Mathew } 198f4f1ae77SSoby Mathew #endif 199f4f1ae77SSoby Mathew 200375f538aSAchin Gupta /******************************************************************************* 201375f538aSAchin Gupta * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type 202375f538aSAchin Gupta * (aarch32/aarch64) if not already known and initialises the context for entry 203375f538aSAchin Gupta * into the SP for its initialisation. 204375f538aSAchin Gupta ******************************************************************************/ 205375f538aSAchin Gupta int32_t tspd_setup(void) 206375f538aSAchin Gupta { 20750e27dadSVikram Kanigiri entry_point_info_t *tsp_ep_info; 208375f538aSAchin Gupta uint32_t linear_id; 209375f538aSAchin Gupta 210fd650ff6SSoby Mathew linear_id = plat_my_core_pos(); 211375f538aSAchin Gupta 212375f538aSAchin Gupta /* 213375f538aSAchin Gupta * Get information about the Secure Payload (BL32) image. Its 214375f538aSAchin Gupta * absence is a critical failure. TODO: Add support to 215375f538aSAchin Gupta * conditionally include the SPD service 216375f538aSAchin Gupta */ 21750e27dadSVikram Kanigiri tsp_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 21850e27dadSVikram Kanigiri if (!tsp_ep_info) { 21950e27dadSVikram Kanigiri WARN("No TSP provided by BL2 boot loader, Booting device" 22050e27dadSVikram Kanigiri " without TSP initialization. SMC`s destined for TSP" 22150e27dadSVikram Kanigiri " will return SMC_UNK\n"); 22250e27dadSVikram Kanigiri return 1; 22350e27dadSVikram Kanigiri } 224375f538aSAchin Gupta 225375f538aSAchin Gupta /* 2267f366605SJeenu Viswambharan * If there's no valid entry point for SP, we return a non-zero value 2277f366605SJeenu Viswambharan * signalling failure initializing the service. We bail out without 2287f366605SJeenu Viswambharan * registering any handlers 2297f366605SJeenu Viswambharan */ 23050e27dadSVikram Kanigiri if (!tsp_ep_info->pc) 2317f366605SJeenu Viswambharan return 1; 2327f366605SJeenu Viswambharan 2337f366605SJeenu Viswambharan /* 234*1645d3eeSSandrine Bailleux * We could inspect the SP image and determine its execution 235375f538aSAchin Gupta * state i.e whether AArch32 or AArch64. Assuming it's AArch64 236375f538aSAchin Gupta * for the time being. 237375f538aSAchin Gupta */ 23850e27dadSVikram Kanigiri tspd_init_tsp_ep_state(tsp_ep_info, 239375f538aSAchin Gupta TSP_AARCH64, 24050e27dadSVikram Kanigiri tsp_ep_info->pc, 241375f538aSAchin Gupta &tspd_sp_context[linear_id]); 242375f538aSAchin Gupta 243faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 244faaa2e76SVikram Kanigiri bl31_set_next_image_type(SECURE); 245faaa2e76SVikram Kanigiri #else 2467f366605SJeenu Viswambharan /* 2477f366605SJeenu Viswambharan * All TSPD initialization done. Now register our init function with 2487f366605SJeenu Viswambharan * BL31 for deferred invocation 2497f366605SJeenu Viswambharan */ 2507f366605SJeenu Viswambharan bl31_register_bl32_init(&tspd_init); 251faaa2e76SVikram Kanigiri #endif 25250e27dadSVikram Kanigiri return 0; 253375f538aSAchin Gupta } 254375f538aSAchin Gupta 255375f538aSAchin Gupta /******************************************************************************* 256375f538aSAchin Gupta * This function passes control to the Secure Payload image (BL32) for the first 257375f538aSAchin Gupta * time on the primary cpu after a cold boot. It assumes that a valid secure 258375f538aSAchin Gupta * context has already been created by tspd_setup() which can be directly used. 259375f538aSAchin Gupta * It also assumes that a valid non-secure context has been initialised by PSCI 260375f538aSAchin Gupta * so it does not need to save and restore any non-secure state. This function 261375f538aSAchin Gupta * performs a synchronous entry into the Secure payload. The SP passes control 2626871c5d3SVikram Kanigiri * back to this routine through a SMC. 263375f538aSAchin Gupta ******************************************************************************/ 2646871c5d3SVikram Kanigiri int32_t tspd_init(void) 265375f538aSAchin Gupta { 266fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 267fb037bfbSDan Handley tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 26850e27dadSVikram Kanigiri entry_point_info_t *tsp_entry_point; 269faaa2e76SVikram Kanigiri uint64_t rc; 27050e27dadSVikram Kanigiri 27150e27dadSVikram Kanigiri /* 27250e27dadSVikram Kanigiri * Get information about the Secure Payload (BL32) image. Its 27350e27dadSVikram Kanigiri * absence is a critical failure. 27450e27dadSVikram Kanigiri */ 27550e27dadSVikram Kanigiri tsp_entry_point = bl31_plat_get_next_image_ep_info(SECURE); 27650e27dadSVikram Kanigiri assert(tsp_entry_point); 27750e27dadSVikram Kanigiri 278fd650ff6SSoby Mathew cm_init_my_context(tsp_entry_point); 279375f538aSAchin Gupta 280375f538aSAchin Gupta /* 281faaa2e76SVikram Kanigiri * Arrange for an entry into the test secure payload. It will be 282faaa2e76SVikram Kanigiri * returned via TSP_ENTRY_DONE case 283607084eeSAchin Gupta */ 284375f538aSAchin Gupta rc = tspd_synchronous_sp_entry(tsp_ctx); 285375f538aSAchin Gupta assert(rc != 0); 286b44a4435SAchin Gupta 287375f538aSAchin Gupta return rc; 288375f538aSAchin Gupta } 289375f538aSAchin Gupta 2907f366605SJeenu Viswambharan 291375f538aSAchin Gupta /******************************************************************************* 292375f538aSAchin Gupta * This function is responsible for handling all SMCs in the Trusted OS/App 293375f538aSAchin Gupta * range from the non-secure state as defined in the SMC Calling Convention 294375f538aSAchin Gupta * Document. It is also responsible for communicating with the Secure payload 295375f538aSAchin Gupta * to delegate work and return results back to the non-secure state. Lastly it 296375f538aSAchin Gupta * will also return any information that the secure payload needs to do the 297375f538aSAchin Gupta * work assigned to it. 298375f538aSAchin Gupta ******************************************************************************/ 299375f538aSAchin Gupta uint64_t tspd_smc_handler(uint32_t smc_fid, 300375f538aSAchin Gupta uint64_t x1, 301375f538aSAchin Gupta uint64_t x2, 302375f538aSAchin Gupta uint64_t x3, 303375f538aSAchin Gupta uint64_t x4, 304375f538aSAchin Gupta void *cookie, 305375f538aSAchin Gupta void *handle, 306375f538aSAchin Gupta uint64_t flags) 307375f538aSAchin Gupta { 308fb037bfbSDan Handley cpu_context_t *ns_cpu_context; 309fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(), ns; 310fb037bfbSDan Handley tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 311faaa2e76SVikram Kanigiri uint64_t rc; 312faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 313faaa2e76SVikram Kanigiri entry_point_info_t *next_image_info; 314faaa2e76SVikram Kanigiri #endif 315375f538aSAchin Gupta 316375f538aSAchin Gupta /* Determine which security state this SMC originated from */ 317375f538aSAchin Gupta ns = is_caller_non_secure(flags); 318375f538aSAchin Gupta 319375f538aSAchin Gupta switch (smc_fid) { 320375f538aSAchin Gupta 321375f538aSAchin Gupta /* 322239b04faSSoby Mathew * This function ID is used by TSP to indicate that it was 323239b04faSSoby Mathew * preempted by a normal world IRQ. 324239b04faSSoby Mathew * 325239b04faSSoby Mathew */ 326239b04faSSoby Mathew case TSP_PREEMPTED: 327239b04faSSoby Mathew if (ns) 328239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 329239b04faSSoby Mathew 330f4f1ae77SSoby Mathew return tspd_handle_sp_preemption(handle); 331239b04faSSoby Mathew 332239b04faSSoby Mathew /* 333b44a4435SAchin Gupta * This function ID is used only by the TSP to indicate that it has 33463b8440fSSoby Mathew * finished handling a S-EL1 interrupt or was preempted by a higher 33563b8440fSSoby Mathew * priority pending EL3 interrupt. Execution should resume 336b44a4435SAchin Gupta * in the normal world. 337b44a4435SAchin Gupta */ 33802446137SSoby Mathew case TSP_HANDLED_S_EL1_INTR: 339b44a4435SAchin Gupta if (ns) 340b44a4435SAchin Gupta SMC_RET1(handle, SMC_UNK); 341b44a4435SAchin Gupta 34208ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 343b44a4435SAchin Gupta 344b44a4435SAchin Gupta /* 345b44a4435SAchin Gupta * Restore the relevant EL3 state which saved to service 346b44a4435SAchin Gupta * this SMC. 347b44a4435SAchin Gupta */ 348b44a4435SAchin Gupta if (get_std_smc_active_flag(tsp_ctx->state)) { 349b44a4435SAchin Gupta SMC_SET_EL3(&tsp_ctx->cpu_ctx, 350b44a4435SAchin Gupta CTX_SPSR_EL3, 351b44a4435SAchin Gupta tsp_ctx->saved_spsr_el3); 352b44a4435SAchin Gupta SMC_SET_EL3(&tsp_ctx->cpu_ctx, 353b44a4435SAchin Gupta CTX_ELR_EL3, 354b44a4435SAchin Gupta tsp_ctx->saved_elr_el3); 35502446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 356f4f1ae77SSoby Mathew /* 357f4f1ae77SSoby Mathew * Need to restore the previously interrupted 358f4f1ae77SSoby Mathew * secure context. 359f4f1ae77SSoby Mathew */ 360f4f1ae77SSoby Mathew memcpy(&tsp_ctx->cpu_ctx, &tsp_ctx->sp_ctx, 361f4f1ae77SSoby Mathew TSPD_SP_CTX_SIZE); 362f4f1ae77SSoby Mathew #endif 363b44a4435SAchin Gupta } 364b44a4435SAchin Gupta 365b44a4435SAchin Gupta /* Get a reference to the non-secure context */ 36608ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 367b44a4435SAchin Gupta assert(ns_cpu_context); 368b44a4435SAchin Gupta 369b44a4435SAchin Gupta /* 370b44a4435SAchin Gupta * Restore non-secure state. There is no need to save the 371b44a4435SAchin Gupta * secure system register context since the TSP was supposed 372b44a4435SAchin Gupta * to preserve it during S-EL1 interrupt handling. 373b44a4435SAchin Gupta */ 374b44a4435SAchin Gupta cm_el1_sysregs_context_restore(NON_SECURE); 375b44a4435SAchin Gupta cm_set_next_eret_context(NON_SECURE); 376b44a4435SAchin Gupta 377b44a4435SAchin Gupta SMC_RET0((uint64_t) ns_cpu_context); 378b44a4435SAchin Gupta 379b44a4435SAchin Gupta /* 380375f538aSAchin Gupta * This function ID is used only by the SP to indicate it has 381375f538aSAchin Gupta * finished initialising itself after a cold boot 382375f538aSAchin Gupta */ 383375f538aSAchin Gupta case TSP_ENTRY_DONE: 384375f538aSAchin Gupta if (ns) 385375f538aSAchin Gupta SMC_RET1(handle, SMC_UNK); 386375f538aSAchin Gupta 387375f538aSAchin Gupta /* 388375f538aSAchin Gupta * Stash the SP entry points information. This is done 389375f538aSAchin Gupta * only once on the primary cpu 390375f538aSAchin Gupta */ 391399fb08fSAndrew Thoelke assert(tsp_vectors == NULL); 392399fb08fSAndrew Thoelke tsp_vectors = (tsp_vectors_t *) x1; 393375f538aSAchin Gupta 394faaa2e76SVikram Kanigiri if (tsp_vectors) { 395faaa2e76SVikram Kanigiri set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON); 396faaa2e76SVikram Kanigiri 397faaa2e76SVikram Kanigiri /* 398faaa2e76SVikram Kanigiri * TSP has been successfully initialized. Register power 399faaa2e76SVikram Kanigiri * managemnt hooks with PSCI 400faaa2e76SVikram Kanigiri */ 401faaa2e76SVikram Kanigiri psci_register_spd_pm_hook(&tspd_pm); 402faaa2e76SVikram Kanigiri 403faaa2e76SVikram Kanigiri /* 404faaa2e76SVikram Kanigiri * Register an interrupt handler for S-EL1 interrupts 405faaa2e76SVikram Kanigiri * when generated during code executing in the 406faaa2e76SVikram Kanigiri * non-secure state. 407faaa2e76SVikram Kanigiri */ 408faaa2e76SVikram Kanigiri flags = 0; 409faaa2e76SVikram Kanigiri set_interrupt_rm_flag(flags, NON_SECURE); 410faaa2e76SVikram Kanigiri rc = register_interrupt_type_handler(INTR_TYPE_S_EL1, 411faaa2e76SVikram Kanigiri tspd_sel1_interrupt_handler, 412faaa2e76SVikram Kanigiri flags); 413faaa2e76SVikram Kanigiri if (rc) 414faaa2e76SVikram Kanigiri panic(); 415f4f1ae77SSoby Mathew 41602446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 417f4f1ae77SSoby Mathew /* 418f4f1ae77SSoby Mathew * Register an interrupt handler for NS interrupts when 419f4f1ae77SSoby Mathew * generated during code executing in secure state are 420f4f1ae77SSoby Mathew * routed to EL3. 421f4f1ae77SSoby Mathew */ 422f4f1ae77SSoby Mathew flags = 0; 423f4f1ae77SSoby Mathew set_interrupt_rm_flag(flags, SECURE); 424f4f1ae77SSoby Mathew 425f4f1ae77SSoby Mathew rc = register_interrupt_type_handler(INTR_TYPE_NS, 426f4f1ae77SSoby Mathew tspd_ns_interrupt_handler, 427f4f1ae77SSoby Mathew flags); 428f4f1ae77SSoby Mathew if (rc) 429f4f1ae77SSoby Mathew panic(); 430f4f1ae77SSoby Mathew 431f4f1ae77SSoby Mathew /* 432404dba53SSoby Mathew * Disable the NS interrupt locally. 433f4f1ae77SSoby Mathew */ 434f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 435f4f1ae77SSoby Mathew #endif 436faaa2e76SVikram Kanigiri } 437faaa2e76SVikram Kanigiri 438faaa2e76SVikram Kanigiri 439faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 440faaa2e76SVikram Kanigiri /* Save the Secure EL1 system register context */ 441faaa2e76SVikram Kanigiri assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx); 442faaa2e76SVikram Kanigiri cm_el1_sysregs_context_save(SECURE); 443faaa2e76SVikram Kanigiri 444faaa2e76SVikram Kanigiri /* Program EL3 registers to enable entry into the next EL */ 445faaa2e76SVikram Kanigiri next_image_info = bl31_plat_get_next_image_ep_info(NON_SECURE); 446faaa2e76SVikram Kanigiri assert(next_image_info); 447faaa2e76SVikram Kanigiri assert(NON_SECURE == 448faaa2e76SVikram Kanigiri GET_SECURITY_STATE(next_image_info->h.attr)); 449faaa2e76SVikram Kanigiri 450fd650ff6SSoby Mathew cm_init_my_context(next_image_info); 451faaa2e76SVikram Kanigiri cm_prepare_el3_exit(NON_SECURE); 452faaa2e76SVikram Kanigiri SMC_RET0(cm_get_context(NON_SECURE)); 453faaa2e76SVikram Kanigiri #else 454375f538aSAchin Gupta /* 455375f538aSAchin Gupta * SP reports completion. The SPD must have initiated 456375f538aSAchin Gupta * the original request through a synchronous entry 457375f538aSAchin Gupta * into the SP. Jump back to the original C runtime 458375f538aSAchin Gupta * context. 459375f538aSAchin Gupta */ 460916a2c1eSAchin Gupta tspd_synchronous_sp_exit(tsp_ctx, x1); 461faaa2e76SVikram Kanigiri #endif 462375f538aSAchin Gupta 463607084eeSAchin Gupta /* 464*1645d3eeSSandrine Bailleux * These function IDs are used only by the SP to indicate it has 465607084eeSAchin Gupta * finished: 466607084eeSAchin Gupta * 1. turning itself on in response to an earlier psci 467607084eeSAchin Gupta * cpu_on request 468607084eeSAchin Gupta * 2. resuming itself after an earlier psci cpu_suspend 469607084eeSAchin Gupta * request. 470607084eeSAchin Gupta */ 471607084eeSAchin Gupta case TSP_ON_DONE: 472607084eeSAchin Gupta case TSP_RESUME_DONE: 473607084eeSAchin Gupta 474607084eeSAchin Gupta /* 475*1645d3eeSSandrine Bailleux * These function IDs are used only by the SP to indicate it has 476607084eeSAchin Gupta * finished: 477607084eeSAchin Gupta * 1. suspending itself after an earlier psci cpu_suspend 478607084eeSAchin Gupta * request. 479607084eeSAchin Gupta * 2. turning itself off in response to an earlier psci 480607084eeSAchin Gupta * cpu_off request. 481607084eeSAchin Gupta */ 482607084eeSAchin Gupta case TSP_OFF_DONE: 483607084eeSAchin Gupta case TSP_SUSPEND_DONE: 484d5f13093SJuan Castillo case TSP_SYSTEM_OFF_DONE: 485d5f13093SJuan Castillo case TSP_SYSTEM_RESET_DONE: 486607084eeSAchin Gupta if (ns) 487607084eeSAchin Gupta SMC_RET1(handle, SMC_UNK); 488607084eeSAchin Gupta 489607084eeSAchin Gupta /* 490607084eeSAchin Gupta * SP reports completion. The SPD must have initiated the 491607084eeSAchin Gupta * original request through a synchronous entry into the SP. 492607084eeSAchin Gupta * Jump back to the original C runtime context, and pass x1 as 493607084eeSAchin Gupta * return value to the caller 494607084eeSAchin Gupta */ 495916a2c1eSAchin Gupta tspd_synchronous_sp_exit(tsp_ctx, x1); 496607084eeSAchin Gupta 497916a2c1eSAchin Gupta /* 498916a2c1eSAchin Gupta * Request from non-secure client to perform an 499916a2c1eSAchin Gupta * arithmetic operation or response from secure 500916a2c1eSAchin Gupta * payload to an earlier request. 501916a2c1eSAchin Gupta */ 502239b04faSSoby Mathew case TSP_FAST_FID(TSP_ADD): 503239b04faSSoby Mathew case TSP_FAST_FID(TSP_SUB): 504239b04faSSoby Mathew case TSP_FAST_FID(TSP_MUL): 505239b04faSSoby Mathew case TSP_FAST_FID(TSP_DIV): 506239b04faSSoby Mathew 507239b04faSSoby Mathew case TSP_STD_FID(TSP_ADD): 508239b04faSSoby Mathew case TSP_STD_FID(TSP_SUB): 509239b04faSSoby Mathew case TSP_STD_FID(TSP_MUL): 510239b04faSSoby Mathew case TSP_STD_FID(TSP_DIV): 511916a2c1eSAchin Gupta if (ns) { 512916a2c1eSAchin Gupta /* 513916a2c1eSAchin Gupta * This is a fresh request from the non-secure client. 514916a2c1eSAchin Gupta * The parameters are in x1 and x2. Figure out which 515916a2c1eSAchin Gupta * registers need to be preserved, save the non-secure 516916a2c1eSAchin Gupta * state and send the request to the secure payload. 517916a2c1eSAchin Gupta */ 51808ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 519239b04faSSoby Mathew 520239b04faSSoby Mathew /* Check if we are already preempted */ 521239b04faSSoby Mathew if (get_std_smc_active_flag(tsp_ctx->state)) 522239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 523239b04faSSoby Mathew 524916a2c1eSAchin Gupta cm_el1_sysregs_context_save(NON_SECURE); 525916a2c1eSAchin Gupta 526916a2c1eSAchin Gupta /* Save x1 and x2 for use by TSP_GET_ARGS call below */ 527239b04faSSoby Mathew store_tsp_args(tsp_ctx, x1, x2); 528916a2c1eSAchin Gupta 529916a2c1eSAchin Gupta /* 530916a2c1eSAchin Gupta * We are done stashing the non-secure context. Ask the 531916a2c1eSAchin Gupta * secure payload to do the work now. 532916a2c1eSAchin Gupta */ 533916a2c1eSAchin Gupta 534916a2c1eSAchin Gupta /* 535916a2c1eSAchin Gupta * Verify if there is a valid context to use, copy the 536916a2c1eSAchin Gupta * operation type and parameters to the secure context 537916a2c1eSAchin Gupta * and jump to the fast smc entry point in the secure 538916a2c1eSAchin Gupta * payload. Entry into S-EL1 will take place upon exit 539916a2c1eSAchin Gupta * from this function. 540916a2c1eSAchin Gupta */ 54108ab89d3SAndrew Thoelke assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE)); 542239b04faSSoby Mathew 543239b04faSSoby Mathew /* Set appropriate entry for SMC. 544239b04faSSoby Mathew * We expect the TSP to manage the PSTATE.I and PSTATE.F 545239b04faSSoby Mathew * flags as appropriate. 546239b04faSSoby Mathew */ 547239b04faSSoby Mathew if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_FAST) { 548239b04faSSoby Mathew cm_set_elr_el3(SECURE, (uint64_t) 549399fb08fSAndrew Thoelke &tsp_vectors->fast_smc_entry); 550239b04faSSoby Mathew } else { 551239b04faSSoby Mathew set_std_smc_active_flag(tsp_ctx->state); 552239b04faSSoby Mathew cm_set_elr_el3(SECURE, (uint64_t) 553399fb08fSAndrew Thoelke &tsp_vectors->std_smc_entry); 55402446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 555f4f1ae77SSoby Mathew /* 556f4f1ae77SSoby Mathew * Enable the routing of NS interrupts to EL3 557f4f1ae77SSoby Mathew * during STD SMC processing on this core. 558f4f1ae77SSoby Mathew */ 559f4f1ae77SSoby Mathew enable_intr_rm_local(INTR_TYPE_NS, SECURE); 560f4f1ae77SSoby Mathew #endif 561239b04faSSoby Mathew } 562239b04faSSoby Mathew 563916a2c1eSAchin Gupta cm_el1_sysregs_context_restore(SECURE); 564916a2c1eSAchin Gupta cm_set_next_eret_context(SECURE); 565239b04faSSoby Mathew SMC_RET3(&tsp_ctx->cpu_ctx, smc_fid, x1, x2); 566916a2c1eSAchin Gupta } else { 567916a2c1eSAchin Gupta /* 568916a2c1eSAchin Gupta * This is the result from the secure client of an 569239b04faSSoby Mathew * earlier request. The results are in x1-x3. Copy it 570916a2c1eSAchin Gupta * into the non-secure context, save the secure state 571916a2c1eSAchin Gupta * and return to the non-secure state. 572916a2c1eSAchin Gupta */ 57308ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 574916a2c1eSAchin Gupta cm_el1_sysregs_context_save(SECURE); 575916a2c1eSAchin Gupta 576916a2c1eSAchin Gupta /* Get a reference to the non-secure context */ 57708ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 578916a2c1eSAchin Gupta assert(ns_cpu_context); 579916a2c1eSAchin Gupta 580916a2c1eSAchin Gupta /* Restore non-secure state */ 581916a2c1eSAchin Gupta cm_el1_sysregs_context_restore(NON_SECURE); 582916a2c1eSAchin Gupta cm_set_next_eret_context(NON_SECURE); 583f4f1ae77SSoby Mathew if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_STD) { 584239b04faSSoby Mathew clr_std_smc_active_flag(tsp_ctx->state); 58502446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 586f4f1ae77SSoby Mathew /* 587f4f1ae77SSoby Mathew * Disable the routing of NS interrupts to EL3 588f4f1ae77SSoby Mathew * after STD SMC processing is finished on this 589f4f1ae77SSoby Mathew * core. 590f4f1ae77SSoby Mathew */ 591f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 592f4f1ae77SSoby Mathew #endif 593f4f1ae77SSoby Mathew } 594f4f1ae77SSoby Mathew 595239b04faSSoby Mathew SMC_RET3(ns_cpu_context, x1, x2, x3); 596916a2c1eSAchin Gupta } 597916a2c1eSAchin Gupta 598916a2c1eSAchin Gupta break; 599916a2c1eSAchin Gupta 600916a2c1eSAchin Gupta /* 601239b04faSSoby Mathew * Request from non secure world to resume the preempted 602239b04faSSoby Mathew * Standard SMC call. 603239b04faSSoby Mathew */ 604239b04faSSoby Mathew case TSP_FID_RESUME: 605239b04faSSoby Mathew /* RESUME should be invoked only by normal world */ 606239b04faSSoby Mathew if (!ns) { 607239b04faSSoby Mathew assert(0); 608239b04faSSoby Mathew break; 609239b04faSSoby Mathew } 610239b04faSSoby Mathew 611239b04faSSoby Mathew /* 612239b04faSSoby Mathew * This is a resume request from the non-secure client. 613239b04faSSoby Mathew * save the non-secure state and send the request to 614239b04faSSoby Mathew * the secure payload. 615239b04faSSoby Mathew */ 61608ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 617239b04faSSoby Mathew 618239b04faSSoby Mathew /* Check if we are already preempted before resume */ 619239b04faSSoby Mathew if (!get_std_smc_active_flag(tsp_ctx->state)) 620239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 621239b04faSSoby Mathew 622239b04faSSoby Mathew cm_el1_sysregs_context_save(NON_SECURE); 623239b04faSSoby Mathew 624239b04faSSoby Mathew /* 625239b04faSSoby Mathew * We are done stashing the non-secure context. Ask the 626239b04faSSoby Mathew * secure payload to do the work now. 627239b04faSSoby Mathew */ 62802446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 629f4f1ae77SSoby Mathew /* 630f4f1ae77SSoby Mathew * Enable the routing of NS interrupts to EL3 during resumption 631f4f1ae77SSoby Mathew * of STD SMC call on this core. 632f4f1ae77SSoby Mathew */ 633f4f1ae77SSoby Mathew enable_intr_rm_local(INTR_TYPE_NS, SECURE); 634f4f1ae77SSoby Mathew #endif 635f4f1ae77SSoby Mathew 636f4f1ae77SSoby Mathew 637239b04faSSoby Mathew 638239b04faSSoby Mathew /* We just need to return to the preempted point in 639239b04faSSoby Mathew * TSP and the execution will resume as normal. 640239b04faSSoby Mathew */ 641239b04faSSoby Mathew cm_el1_sysregs_context_restore(SECURE); 642239b04faSSoby Mathew cm_set_next_eret_context(SECURE); 64310b65ecfSSoby Mathew SMC_RET0(&tsp_ctx->cpu_ctx); 644239b04faSSoby Mathew 645239b04faSSoby Mathew /* 646916a2c1eSAchin Gupta * This is a request from the secure payload for more arguments 647916a2c1eSAchin Gupta * for an ongoing arithmetic operation requested by the 648916a2c1eSAchin Gupta * non-secure world. Simply return the arguments from the non- 649916a2c1eSAchin Gupta * secure client in the original call. 650916a2c1eSAchin Gupta */ 651916a2c1eSAchin Gupta case TSP_GET_ARGS: 652916a2c1eSAchin Gupta if (ns) 653916a2c1eSAchin Gupta SMC_RET1(handle, SMC_UNK); 654916a2c1eSAchin Gupta 655239b04faSSoby Mathew get_tsp_args(tsp_ctx, x1, x2); 656239b04faSSoby Mathew SMC_RET2(handle, x1, x2); 657916a2c1eSAchin Gupta 65852538b9bSJeenu Viswambharan case TOS_CALL_COUNT: 65952538b9bSJeenu Viswambharan /* 66052538b9bSJeenu Viswambharan * Return the number of service function IDs implemented to 66152538b9bSJeenu Viswambharan * provide service to non-secure 66252538b9bSJeenu Viswambharan */ 66352538b9bSJeenu Viswambharan SMC_RET1(handle, TSP_NUM_FID); 66452538b9bSJeenu Viswambharan 66552538b9bSJeenu Viswambharan case TOS_UID: 66652538b9bSJeenu Viswambharan /* Return TSP UID to the caller */ 66752538b9bSJeenu Viswambharan SMC_UUID_RET(handle, tsp_uuid); 66852538b9bSJeenu Viswambharan 66952538b9bSJeenu Viswambharan case TOS_CALL_VERSION: 67052538b9bSJeenu Viswambharan /* Return the version of current implementation */ 67152538b9bSJeenu Viswambharan SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR); 67252538b9bSJeenu Viswambharan 673375f538aSAchin Gupta default: 674607084eeSAchin Gupta break; 675375f538aSAchin Gupta } 676375f538aSAchin Gupta 677607084eeSAchin Gupta SMC_RET1(handle, SMC_UNK); 678375f538aSAchin Gupta } 679375f538aSAchin Gupta 680239b04faSSoby Mathew /* Define a SPD runtime service descriptor for fast SMC calls */ 681375f538aSAchin Gupta DECLARE_RT_SVC( 682239b04faSSoby Mathew tspd_fast, 683375f538aSAchin Gupta 684375f538aSAchin Gupta OEN_TOS_START, 685375f538aSAchin Gupta OEN_TOS_END, 686375f538aSAchin Gupta SMC_TYPE_FAST, 687375f538aSAchin Gupta tspd_setup, 688375f538aSAchin Gupta tspd_smc_handler 689375f538aSAchin Gupta ); 690239b04faSSoby Mathew 691239b04faSSoby Mathew /* Define a SPD runtime service descriptor for standard SMC calls */ 692239b04faSSoby Mathew DECLARE_RT_SVC( 693239b04faSSoby Mathew tspd_std, 694239b04faSSoby Mathew 695239b04faSSoby Mathew OEN_TOS_START, 696239b04faSSoby Mathew OEN_TOS_END, 697239b04faSSoby Mathew SMC_TYPE_STD, 698239b04faSSoby Mathew NULL, 699239b04faSSoby Mathew tspd_smc_handler 700239b04faSSoby Mathew ); 701