1375f538aSAchin Gupta /* 2*8aabea33SPaul Beesley * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved. 3375f538aSAchin Gupta * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5375f538aSAchin Gupta */ 6375f538aSAchin Gupta 7375f538aSAchin Gupta 8375f538aSAchin Gupta /******************************************************************************* 9375f538aSAchin Gupta * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a 10375f538aSAchin Gupta * plug-in component to the Secure Monitor, registered as a runtime service. The 11375f538aSAchin Gupta * SPD is expected to be a functional extension of the Secure Payload (SP) that 12375f538aSAchin Gupta * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting 13375f538aSAchin Gupta * the Trusted OS/Applications range to the dispatcher. The SPD will either 14375f538aSAchin Gupta * handle the request locally or delegate it to the Secure Payload. It is also 15375f538aSAchin Gupta * responsible for initialising and maintaining communication with the SP. 16375f538aSAchin Gupta ******************************************************************************/ 1797043ac9SDan Handley #include <assert.h> 18b44a4435SAchin Gupta #include <errno.h> 1997043ac9SDan Handley #include <stddef.h> 20f4f1ae77SSoby Mathew #include <string.h> 2109d40e0eSAntonio Nino Diaz 2209d40e0eSAntonio Nino Diaz #include <arch_helpers.h> 2309d40e0eSAntonio Nino Diaz #include <bl31/bl31.h> 2409d40e0eSAntonio Nino Diaz #include <bl31/ehf.h> 2509d40e0eSAntonio Nino Diaz #include <bl32/tsp/tsp.h> 2609d40e0eSAntonio Nino Diaz #include <common/bl_common.h> 2709d40e0eSAntonio Nino Diaz #include <common/debug.h> 2809d40e0eSAntonio Nino Diaz #include <common/runtime_svc.h> 2909d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h> 3009d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 3109d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h> 3209d40e0eSAntonio Nino Diaz 3335e98e55SDan Handley #include "tspd_private.h" 34375f538aSAchin Gupta 35375f538aSAchin Gupta /******************************************************************************* 36399fb08fSAndrew Thoelke * Address of the entrypoint vector table in the Secure Payload. It is 37399fb08fSAndrew Thoelke * initialised once on the primary core after a cold boot. 38375f538aSAchin Gupta ******************************************************************************/ 39399fb08fSAndrew Thoelke tsp_vectors_t *tsp_vectors; 40375f538aSAchin Gupta 41375f538aSAchin Gupta /******************************************************************************* 42375f538aSAchin Gupta * Array to keep track of per-cpu Secure Payload state 43375f538aSAchin Gupta ******************************************************************************/ 44fb037bfbSDan Handley tsp_context_t tspd_sp_context[TSPD_CORE_COUNT]; 45375f538aSAchin Gupta 467f366605SJeenu Viswambharan 4752538b9bSJeenu Viswambharan /* TSP UID */ 4803364865SRoberto Vargas DEFINE_SVC_UUID2(tsp_uuid, 4903364865SRoberto Vargas 0xa056305b, 0x9132, 0x7b42, 0x98, 0x11, 5052538b9bSJeenu Viswambharan 0x71, 0x68, 0xca, 0x50, 0xf3, 0xfa); 5152538b9bSJeenu Viswambharan 526871c5d3SVikram Kanigiri int32_t tspd_init(void); 537f366605SJeenu Viswambharan 54404dba53SSoby Mathew /* 55404dba53SSoby Mathew * This helper function handles Secure EL1 preemption. The preemption could be 56404dba53SSoby Mathew * due Non Secure interrupts or EL3 interrupts. In both the cases we context 57404dba53SSoby Mathew * switch to the normal world and in case of EL3 interrupts, it will again be 58404dba53SSoby Mathew * routed to EL3 which will get handled at the exception vectors. 59404dba53SSoby Mathew */ 60f4f1ae77SSoby Mathew uint64_t tspd_handle_sp_preemption(void *handle) 61f4f1ae77SSoby Mathew { 62f4f1ae77SSoby Mathew cpu_context_t *ns_cpu_context; 63404dba53SSoby Mathew 64f4f1ae77SSoby Mathew assert(handle == cm_get_context(SECURE)); 65f4f1ae77SSoby Mathew cm_el1_sysregs_context_save(SECURE); 66f4f1ae77SSoby Mathew /* Get a reference to the non-secure context */ 67f4f1ae77SSoby Mathew ns_cpu_context = cm_get_context(NON_SECURE); 68f4f1ae77SSoby Mathew assert(ns_cpu_context); 69f4f1ae77SSoby Mathew 70f4f1ae77SSoby Mathew /* 7163b8440fSSoby Mathew * To allow Secure EL1 interrupt handler to re-enter TSP while TSP 7263b8440fSSoby Mathew * is preempted, the secure system register context which will get 7363b8440fSSoby Mathew * overwritten must be additionally saved. This is currently done 7463b8440fSSoby Mathew * by the TSPD S-EL1 interrupt handler. 7563b8440fSSoby Mathew */ 7663b8440fSSoby Mathew 7763b8440fSSoby Mathew /* 7863b8440fSSoby Mathew * Restore non-secure state. 79f4f1ae77SSoby Mathew */ 80f4f1ae77SSoby Mathew cm_el1_sysregs_context_restore(NON_SECURE); 81f4f1ae77SSoby Mathew cm_set_next_eret_context(NON_SECURE); 82f4f1ae77SSoby Mathew 83404dba53SSoby Mathew /* 8416292f54SDavid Cunado * The TSP was preempted during execution of a Yielding SMC Call. 8563b8440fSSoby Mathew * Return back to the normal world with SMC_PREEMPTED as error 8663b8440fSSoby Mathew * code in x0. 87404dba53SSoby Mathew */ 88f4f1ae77SSoby Mathew SMC_RET1(ns_cpu_context, SMC_PREEMPTED); 89f4f1ae77SSoby Mathew } 90404dba53SSoby Mathew 91b44a4435SAchin Gupta /******************************************************************************* 92b44a4435SAchin Gupta * This function is the handler registered for S-EL1 interrupts by the TSPD. It 93b44a4435SAchin Gupta * validates the interrupt and upon success arranges entry into the TSP at 9402446137SSoby Mathew * 'tsp_sel1_intr_entry()' for handling the interrupt. 95b44a4435SAchin Gupta ******************************************************************************/ 96b44a4435SAchin Gupta static uint64_t tspd_sel1_interrupt_handler(uint32_t id, 97b44a4435SAchin Gupta uint32_t flags, 98b44a4435SAchin Gupta void *handle, 99b44a4435SAchin Gupta void *cookie) 100b44a4435SAchin Gupta { 101b44a4435SAchin Gupta uint32_t linear_id; 102b44a4435SAchin Gupta tsp_context_t *tsp_ctx; 103b44a4435SAchin Gupta 104b44a4435SAchin Gupta /* Check the security state when the exception was generated */ 105b44a4435SAchin Gupta assert(get_interrupt_src_ss(flags) == NON_SECURE); 106b44a4435SAchin Gupta 107b44a4435SAchin Gupta /* Sanity check the pointer to this cpu's context */ 10808ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 109b44a4435SAchin Gupta 110b44a4435SAchin Gupta /* Save the non-secure context before entering the TSP */ 111b44a4435SAchin Gupta cm_el1_sysregs_context_save(NON_SECURE); 112b44a4435SAchin Gupta 113b44a4435SAchin Gupta /* Get a reference to this cpu's TSP context */ 114fd650ff6SSoby Mathew linear_id = plat_my_core_pos(); 115b44a4435SAchin Gupta tsp_ctx = &tspd_sp_context[linear_id]; 11608ab89d3SAndrew Thoelke assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE)); 117b44a4435SAchin Gupta 118b44a4435SAchin Gupta /* 119b44a4435SAchin Gupta * Determine if the TSP was previously preempted. Its last known 120b44a4435SAchin Gupta * context has to be preserved in this case. 121b44a4435SAchin Gupta * The TSP should return control to the TSPD after handling this 12202446137SSoby Mathew * S-EL1 interrupt. Preserve essential EL3 context to allow entry into 12302446137SSoby Mathew * the TSP at the S-EL1 interrupt entry point using the 'cpu_context' 12402446137SSoby Mathew * structure. There is no need to save the secure system register 12502446137SSoby Mathew * context since the TSP is supposed to preserve it during S-EL1 12602446137SSoby Mathew * interrupt handling. 127b44a4435SAchin Gupta */ 12816292f54SDavid Cunado if (get_yield_smc_active_flag(tsp_ctx->state)) { 129b44a4435SAchin Gupta tsp_ctx->saved_spsr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx, 130b44a4435SAchin Gupta CTX_SPSR_EL3); 131b44a4435SAchin Gupta tsp_ctx->saved_elr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx, 132b44a4435SAchin Gupta CTX_ELR_EL3); 13302446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 134f4f1ae77SSoby Mathew /*Need to save the previously interrupted secure context */ 135f4f1ae77SSoby Mathew memcpy(&tsp_ctx->sp_ctx, &tsp_ctx->cpu_ctx, TSPD_SP_CTX_SIZE); 136f4f1ae77SSoby Mathew #endif 137b44a4435SAchin Gupta } 138b44a4435SAchin Gupta 139b44a4435SAchin Gupta cm_el1_sysregs_context_restore(SECURE); 14002446137SSoby Mathew cm_set_elr_spsr_el3(SECURE, (uint64_t) &tsp_vectors->sel1_intr_entry, 141167a9357SAndrew Thoelke SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS)); 142f4f1ae77SSoby Mathew 143b44a4435SAchin Gupta cm_set_next_eret_context(SECURE); 144b44a4435SAchin Gupta 145b44a4435SAchin Gupta /* 14602446137SSoby Mathew * Tell the TSP that it has to handle a S-EL1 interrupt synchronously. 14702446137SSoby Mathew * Also the instruction in normal world where the interrupt was 14802446137SSoby Mathew * generated is passed for debugging purposes. It is safe to retrieve 14902446137SSoby Mathew * this address from ELR_EL3 as the secure context will not take effect 15002446137SSoby Mathew * until el3_exit(). 151b44a4435SAchin Gupta */ 15202446137SSoby Mathew SMC_RET2(&tsp_ctx->cpu_ctx, TSP_HANDLE_SEL1_INTR_AND_RETURN, read_elr_el3()); 153b44a4435SAchin Gupta } 1547f366605SJeenu Viswambharan 15502446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 156f4f1ae77SSoby Mathew /******************************************************************************* 15702446137SSoby Mathew * This function is the handler registered for Non secure interrupts by the 15802446137SSoby Mathew * TSPD. It validates the interrupt and upon success arranges entry into the 15902446137SSoby Mathew * normal world for handling the interrupt. 160f4f1ae77SSoby Mathew ******************************************************************************/ 161f4f1ae77SSoby Mathew static uint64_t tspd_ns_interrupt_handler(uint32_t id, 162f4f1ae77SSoby Mathew uint32_t flags, 163f4f1ae77SSoby Mathew void *handle, 164f4f1ae77SSoby Mathew void *cookie) 165f4f1ae77SSoby Mathew { 166f4f1ae77SSoby Mathew /* Check the security state when the exception was generated */ 167f4f1ae77SSoby Mathew assert(get_interrupt_src_ss(flags) == SECURE); 168f4f1ae77SSoby Mathew 169f4f1ae77SSoby Mathew /* 170f4f1ae77SSoby Mathew * Disable the routing of NS interrupts from secure world to EL3 while 171f4f1ae77SSoby Mathew * interrupted on this core. 172f4f1ae77SSoby Mathew */ 173f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 174f4f1ae77SSoby Mathew 175f4f1ae77SSoby Mathew return tspd_handle_sp_preemption(handle); 176f4f1ae77SSoby Mathew } 177f4f1ae77SSoby Mathew #endif 178f4f1ae77SSoby Mathew 179375f538aSAchin Gupta /******************************************************************************* 180375f538aSAchin Gupta * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type 181375f538aSAchin Gupta * (aarch32/aarch64) if not already known and initialises the context for entry 182375f538aSAchin Gupta * into the SP for its initialisation. 183375f538aSAchin Gupta ******************************************************************************/ 184724fd958SMasahiro Yamada static int32_t tspd_setup(void) 185375f538aSAchin Gupta { 18650e27dadSVikram Kanigiri entry_point_info_t *tsp_ep_info; 187375f538aSAchin Gupta uint32_t linear_id; 188375f538aSAchin Gupta 189fd650ff6SSoby Mathew linear_id = plat_my_core_pos(); 190375f538aSAchin Gupta 191375f538aSAchin Gupta /* 192375f538aSAchin Gupta * Get information about the Secure Payload (BL32) image. Its 193375f538aSAchin Gupta * absence is a critical failure. TODO: Add support to 194375f538aSAchin Gupta * conditionally include the SPD service 195375f538aSAchin Gupta */ 19650e27dadSVikram Kanigiri tsp_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 19750e27dadSVikram Kanigiri if (!tsp_ep_info) { 19850e27dadSVikram Kanigiri WARN("No TSP provided by BL2 boot loader, Booting device" 19950e27dadSVikram Kanigiri " without TSP initialization. SMC`s destined for TSP" 20050e27dadSVikram Kanigiri " will return SMC_UNK\n"); 20150e27dadSVikram Kanigiri return 1; 20250e27dadSVikram Kanigiri } 203375f538aSAchin Gupta 204375f538aSAchin Gupta /* 2057f366605SJeenu Viswambharan * If there's no valid entry point for SP, we return a non-zero value 2067f366605SJeenu Viswambharan * signalling failure initializing the service. We bail out without 2077f366605SJeenu Viswambharan * registering any handlers 2087f366605SJeenu Viswambharan */ 20950e27dadSVikram Kanigiri if (!tsp_ep_info->pc) 2107f366605SJeenu Viswambharan return 1; 2117f366605SJeenu Viswambharan 2127f366605SJeenu Viswambharan /* 2131645d3eeSSandrine Bailleux * We could inspect the SP image and determine its execution 214375f538aSAchin Gupta * state i.e whether AArch32 or AArch64. Assuming it's AArch64 215375f538aSAchin Gupta * for the time being. 216375f538aSAchin Gupta */ 21750e27dadSVikram Kanigiri tspd_init_tsp_ep_state(tsp_ep_info, 218375f538aSAchin Gupta TSP_AARCH64, 21950e27dadSVikram Kanigiri tsp_ep_info->pc, 220375f538aSAchin Gupta &tspd_sp_context[linear_id]); 221375f538aSAchin Gupta 222faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 223faaa2e76SVikram Kanigiri bl31_set_next_image_type(SECURE); 224faaa2e76SVikram Kanigiri #else 2257f366605SJeenu Viswambharan /* 2267f366605SJeenu Viswambharan * All TSPD initialization done. Now register our init function with 2277f366605SJeenu Viswambharan * BL31 for deferred invocation 2287f366605SJeenu Viswambharan */ 2297f366605SJeenu Viswambharan bl31_register_bl32_init(&tspd_init); 230faaa2e76SVikram Kanigiri #endif 23150e27dadSVikram Kanigiri return 0; 232375f538aSAchin Gupta } 233375f538aSAchin Gupta 234375f538aSAchin Gupta /******************************************************************************* 235375f538aSAchin Gupta * This function passes control to the Secure Payload image (BL32) for the first 236375f538aSAchin Gupta * time on the primary cpu after a cold boot. It assumes that a valid secure 237375f538aSAchin Gupta * context has already been created by tspd_setup() which can be directly used. 238375f538aSAchin Gupta * It also assumes that a valid non-secure context has been initialised by PSCI 239375f538aSAchin Gupta * so it does not need to save and restore any non-secure state. This function 240375f538aSAchin Gupta * performs a synchronous entry into the Secure payload. The SP passes control 2416871c5d3SVikram Kanigiri * back to this routine through a SMC. 242375f538aSAchin Gupta ******************************************************************************/ 2436871c5d3SVikram Kanigiri int32_t tspd_init(void) 244375f538aSAchin Gupta { 245fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 246fb037bfbSDan Handley tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 24750e27dadSVikram Kanigiri entry_point_info_t *tsp_entry_point; 248faaa2e76SVikram Kanigiri uint64_t rc; 24950e27dadSVikram Kanigiri 25050e27dadSVikram Kanigiri /* 25150e27dadSVikram Kanigiri * Get information about the Secure Payload (BL32) image. Its 25250e27dadSVikram Kanigiri * absence is a critical failure. 25350e27dadSVikram Kanigiri */ 25450e27dadSVikram Kanigiri tsp_entry_point = bl31_plat_get_next_image_ep_info(SECURE); 25550e27dadSVikram Kanigiri assert(tsp_entry_point); 25650e27dadSVikram Kanigiri 257fd650ff6SSoby Mathew cm_init_my_context(tsp_entry_point); 258375f538aSAchin Gupta 259375f538aSAchin Gupta /* 260faaa2e76SVikram Kanigiri * Arrange for an entry into the test secure payload. It will be 261faaa2e76SVikram Kanigiri * returned via TSP_ENTRY_DONE case 262607084eeSAchin Gupta */ 263375f538aSAchin Gupta rc = tspd_synchronous_sp_entry(tsp_ctx); 264375f538aSAchin Gupta assert(rc != 0); 265b44a4435SAchin Gupta 266375f538aSAchin Gupta return rc; 267375f538aSAchin Gupta } 268375f538aSAchin Gupta 2697f366605SJeenu Viswambharan 270375f538aSAchin Gupta /******************************************************************************* 271375f538aSAchin Gupta * This function is responsible for handling all SMCs in the Trusted OS/App 272375f538aSAchin Gupta * range from the non-secure state as defined in the SMC Calling Convention 273375f538aSAchin Gupta * Document. It is also responsible for communicating with the Secure payload 274375f538aSAchin Gupta * to delegate work and return results back to the non-secure state. Lastly it 275375f538aSAchin Gupta * will also return any information that the secure payload needs to do the 276375f538aSAchin Gupta * work assigned to it. 277375f538aSAchin Gupta ******************************************************************************/ 27857d1e5faSMasahiro Yamada static uintptr_t tspd_smc_handler(uint32_t smc_fid, 27957d1e5faSMasahiro Yamada u_register_t x1, 28057d1e5faSMasahiro Yamada u_register_t x2, 28157d1e5faSMasahiro Yamada u_register_t x3, 28257d1e5faSMasahiro Yamada u_register_t x4, 283375f538aSAchin Gupta void *cookie, 284375f538aSAchin Gupta void *handle, 28557d1e5faSMasahiro Yamada u_register_t flags) 286375f538aSAchin Gupta { 287fb037bfbSDan Handley cpu_context_t *ns_cpu_context; 288fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(), ns; 289fb037bfbSDan Handley tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 290faaa2e76SVikram Kanigiri uint64_t rc; 291faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 292faaa2e76SVikram Kanigiri entry_point_info_t *next_image_info; 293faaa2e76SVikram Kanigiri #endif 294375f538aSAchin Gupta 295375f538aSAchin Gupta /* Determine which security state this SMC originated from */ 296375f538aSAchin Gupta ns = is_caller_non_secure(flags); 297375f538aSAchin Gupta 298375f538aSAchin Gupta switch (smc_fid) { 299375f538aSAchin Gupta 300375f538aSAchin Gupta /* 301239b04faSSoby Mathew * This function ID is used by TSP to indicate that it was 302239b04faSSoby Mathew * preempted by a normal world IRQ. 303239b04faSSoby Mathew * 304239b04faSSoby Mathew */ 305239b04faSSoby Mathew case TSP_PREEMPTED: 306239b04faSSoby Mathew if (ns) 307239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 308239b04faSSoby Mathew 309f4f1ae77SSoby Mathew return tspd_handle_sp_preemption(handle); 310239b04faSSoby Mathew 311239b04faSSoby Mathew /* 312b44a4435SAchin Gupta * This function ID is used only by the TSP to indicate that it has 31363b8440fSSoby Mathew * finished handling a S-EL1 interrupt or was preempted by a higher 31463b8440fSSoby Mathew * priority pending EL3 interrupt. Execution should resume 315b44a4435SAchin Gupta * in the normal world. 316b44a4435SAchin Gupta */ 31702446137SSoby Mathew case TSP_HANDLED_S_EL1_INTR: 318b44a4435SAchin Gupta if (ns) 319b44a4435SAchin Gupta SMC_RET1(handle, SMC_UNK); 320b44a4435SAchin Gupta 32108ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 322b44a4435SAchin Gupta 323b44a4435SAchin Gupta /* 324b44a4435SAchin Gupta * Restore the relevant EL3 state which saved to service 325b44a4435SAchin Gupta * this SMC. 326b44a4435SAchin Gupta */ 32716292f54SDavid Cunado if (get_yield_smc_active_flag(tsp_ctx->state)) { 328b44a4435SAchin Gupta SMC_SET_EL3(&tsp_ctx->cpu_ctx, 329b44a4435SAchin Gupta CTX_SPSR_EL3, 330b44a4435SAchin Gupta tsp_ctx->saved_spsr_el3); 331b44a4435SAchin Gupta SMC_SET_EL3(&tsp_ctx->cpu_ctx, 332b44a4435SAchin Gupta CTX_ELR_EL3, 333b44a4435SAchin Gupta tsp_ctx->saved_elr_el3); 33402446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 335f4f1ae77SSoby Mathew /* 336f4f1ae77SSoby Mathew * Need to restore the previously interrupted 337f4f1ae77SSoby Mathew * secure context. 338f4f1ae77SSoby Mathew */ 339f4f1ae77SSoby Mathew memcpy(&tsp_ctx->cpu_ctx, &tsp_ctx->sp_ctx, 340f4f1ae77SSoby Mathew TSPD_SP_CTX_SIZE); 341f4f1ae77SSoby Mathew #endif 342b44a4435SAchin Gupta } 343b44a4435SAchin Gupta 344b44a4435SAchin Gupta /* Get a reference to the non-secure context */ 34508ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 346b44a4435SAchin Gupta assert(ns_cpu_context); 347b44a4435SAchin Gupta 348b44a4435SAchin Gupta /* 349b44a4435SAchin Gupta * Restore non-secure state. There is no need to save the 350b44a4435SAchin Gupta * secure system register context since the TSP was supposed 351b44a4435SAchin Gupta * to preserve it during S-EL1 interrupt handling. 352b44a4435SAchin Gupta */ 353b44a4435SAchin Gupta cm_el1_sysregs_context_restore(NON_SECURE); 354b44a4435SAchin Gupta cm_set_next_eret_context(NON_SECURE); 355b44a4435SAchin Gupta 356b44a4435SAchin Gupta SMC_RET0((uint64_t) ns_cpu_context); 357b44a4435SAchin Gupta 358b44a4435SAchin Gupta /* 359375f538aSAchin Gupta * This function ID is used only by the SP to indicate it has 360375f538aSAchin Gupta * finished initialising itself after a cold boot 361375f538aSAchin Gupta */ 362375f538aSAchin Gupta case TSP_ENTRY_DONE: 363375f538aSAchin Gupta if (ns) 364375f538aSAchin Gupta SMC_RET1(handle, SMC_UNK); 365375f538aSAchin Gupta 366375f538aSAchin Gupta /* 367375f538aSAchin Gupta * Stash the SP entry points information. This is done 368375f538aSAchin Gupta * only once on the primary cpu 369375f538aSAchin Gupta */ 370399fb08fSAndrew Thoelke assert(tsp_vectors == NULL); 371399fb08fSAndrew Thoelke tsp_vectors = (tsp_vectors_t *) x1; 372375f538aSAchin Gupta 373faaa2e76SVikram Kanigiri if (tsp_vectors) { 374faaa2e76SVikram Kanigiri set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON); 375faaa2e76SVikram Kanigiri 376faaa2e76SVikram Kanigiri /* 377faaa2e76SVikram Kanigiri * TSP has been successfully initialized. Register power 378*8aabea33SPaul Beesley * management hooks with PSCI 379faaa2e76SVikram Kanigiri */ 380faaa2e76SVikram Kanigiri psci_register_spd_pm_hook(&tspd_pm); 381faaa2e76SVikram Kanigiri 382faaa2e76SVikram Kanigiri /* 383faaa2e76SVikram Kanigiri * Register an interrupt handler for S-EL1 interrupts 384faaa2e76SVikram Kanigiri * when generated during code executing in the 385faaa2e76SVikram Kanigiri * non-secure state. 386faaa2e76SVikram Kanigiri */ 387faaa2e76SVikram Kanigiri flags = 0; 388faaa2e76SVikram Kanigiri set_interrupt_rm_flag(flags, NON_SECURE); 389faaa2e76SVikram Kanigiri rc = register_interrupt_type_handler(INTR_TYPE_S_EL1, 390faaa2e76SVikram Kanigiri tspd_sel1_interrupt_handler, 391faaa2e76SVikram Kanigiri flags); 392faaa2e76SVikram Kanigiri if (rc) 393faaa2e76SVikram Kanigiri panic(); 394f4f1ae77SSoby Mathew 39502446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 396f4f1ae77SSoby Mathew /* 397f4f1ae77SSoby Mathew * Register an interrupt handler for NS interrupts when 398f4f1ae77SSoby Mathew * generated during code executing in secure state are 399f4f1ae77SSoby Mathew * routed to EL3. 400f4f1ae77SSoby Mathew */ 401f4f1ae77SSoby Mathew flags = 0; 402f4f1ae77SSoby Mathew set_interrupt_rm_flag(flags, SECURE); 403f4f1ae77SSoby Mathew 404f4f1ae77SSoby Mathew rc = register_interrupt_type_handler(INTR_TYPE_NS, 405f4f1ae77SSoby Mathew tspd_ns_interrupt_handler, 406f4f1ae77SSoby Mathew flags); 407f4f1ae77SSoby Mathew if (rc) 408f4f1ae77SSoby Mathew panic(); 409f4f1ae77SSoby Mathew 410f4f1ae77SSoby Mathew /* 411404dba53SSoby Mathew * Disable the NS interrupt locally. 412f4f1ae77SSoby Mathew */ 413f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 414f4f1ae77SSoby Mathew #endif 415faaa2e76SVikram Kanigiri } 416faaa2e76SVikram Kanigiri 417faaa2e76SVikram Kanigiri 418faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 419faaa2e76SVikram Kanigiri /* Save the Secure EL1 system register context */ 420faaa2e76SVikram Kanigiri assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx); 421faaa2e76SVikram Kanigiri cm_el1_sysregs_context_save(SECURE); 422faaa2e76SVikram Kanigiri 423faaa2e76SVikram Kanigiri /* Program EL3 registers to enable entry into the next EL */ 424faaa2e76SVikram Kanigiri next_image_info = bl31_plat_get_next_image_ep_info(NON_SECURE); 425faaa2e76SVikram Kanigiri assert(next_image_info); 426faaa2e76SVikram Kanigiri assert(NON_SECURE == 427faaa2e76SVikram Kanigiri GET_SECURITY_STATE(next_image_info->h.attr)); 428faaa2e76SVikram Kanigiri 429fd650ff6SSoby Mathew cm_init_my_context(next_image_info); 430faaa2e76SVikram Kanigiri cm_prepare_el3_exit(NON_SECURE); 431faaa2e76SVikram Kanigiri SMC_RET0(cm_get_context(NON_SECURE)); 432faaa2e76SVikram Kanigiri #else 433375f538aSAchin Gupta /* 434375f538aSAchin Gupta * SP reports completion. The SPD must have initiated 435375f538aSAchin Gupta * the original request through a synchronous entry 436375f538aSAchin Gupta * into the SP. Jump back to the original C runtime 437375f538aSAchin Gupta * context. 438375f538aSAchin Gupta */ 439916a2c1eSAchin Gupta tspd_synchronous_sp_exit(tsp_ctx, x1); 440185a23ffSJonathan Wright break; 441faaa2e76SVikram Kanigiri #endif 4423df6012aSDouglas Raillard /* 4433df6012aSDouglas Raillard * This function ID is used only by the SP to indicate it has finished 44416292f54SDavid Cunado * aborting a preempted Yielding SMC Call. 4453df6012aSDouglas Raillard */ 4463df6012aSDouglas Raillard case TSP_ABORT_DONE: 447375f538aSAchin Gupta 448607084eeSAchin Gupta /* 4491645d3eeSSandrine Bailleux * These function IDs are used only by the SP to indicate it has 450607084eeSAchin Gupta * finished: 451607084eeSAchin Gupta * 1. turning itself on in response to an earlier psci 452607084eeSAchin Gupta * cpu_on request 453607084eeSAchin Gupta * 2. resuming itself after an earlier psci cpu_suspend 454607084eeSAchin Gupta * request. 455607084eeSAchin Gupta */ 456607084eeSAchin Gupta case TSP_ON_DONE: 457607084eeSAchin Gupta case TSP_RESUME_DONE: 458607084eeSAchin Gupta 459607084eeSAchin Gupta /* 4601645d3eeSSandrine Bailleux * These function IDs are used only by the SP to indicate it has 461607084eeSAchin Gupta * finished: 462607084eeSAchin Gupta * 1. suspending itself after an earlier psci cpu_suspend 463607084eeSAchin Gupta * request. 464607084eeSAchin Gupta * 2. turning itself off in response to an earlier psci 465607084eeSAchin Gupta * cpu_off request. 466607084eeSAchin Gupta */ 467607084eeSAchin Gupta case TSP_OFF_DONE: 468607084eeSAchin Gupta case TSP_SUSPEND_DONE: 469d5f13093SJuan Castillo case TSP_SYSTEM_OFF_DONE: 470d5f13093SJuan Castillo case TSP_SYSTEM_RESET_DONE: 471607084eeSAchin Gupta if (ns) 472607084eeSAchin Gupta SMC_RET1(handle, SMC_UNK); 473607084eeSAchin Gupta 474607084eeSAchin Gupta /* 475607084eeSAchin Gupta * SP reports completion. The SPD must have initiated the 476607084eeSAchin Gupta * original request through a synchronous entry into the SP. 477607084eeSAchin Gupta * Jump back to the original C runtime context, and pass x1 as 478607084eeSAchin Gupta * return value to the caller 479607084eeSAchin Gupta */ 480916a2c1eSAchin Gupta tspd_synchronous_sp_exit(tsp_ctx, x1); 481185a23ffSJonathan Wright break; 482607084eeSAchin Gupta 483916a2c1eSAchin Gupta /* 484916a2c1eSAchin Gupta * Request from non-secure client to perform an 485916a2c1eSAchin Gupta * arithmetic operation or response from secure 486916a2c1eSAchin Gupta * payload to an earlier request. 487916a2c1eSAchin Gupta */ 488239b04faSSoby Mathew case TSP_FAST_FID(TSP_ADD): 489239b04faSSoby Mathew case TSP_FAST_FID(TSP_SUB): 490239b04faSSoby Mathew case TSP_FAST_FID(TSP_MUL): 491239b04faSSoby Mathew case TSP_FAST_FID(TSP_DIV): 492239b04faSSoby Mathew 49316292f54SDavid Cunado case TSP_YIELD_FID(TSP_ADD): 49416292f54SDavid Cunado case TSP_YIELD_FID(TSP_SUB): 49516292f54SDavid Cunado case TSP_YIELD_FID(TSP_MUL): 49616292f54SDavid Cunado case TSP_YIELD_FID(TSP_DIV): 497916a2c1eSAchin Gupta if (ns) { 498916a2c1eSAchin Gupta /* 499916a2c1eSAchin Gupta * This is a fresh request from the non-secure client. 500916a2c1eSAchin Gupta * The parameters are in x1 and x2. Figure out which 501916a2c1eSAchin Gupta * registers need to be preserved, save the non-secure 502916a2c1eSAchin Gupta * state and send the request to the secure payload. 503916a2c1eSAchin Gupta */ 50408ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 505239b04faSSoby Mathew 506239b04faSSoby Mathew /* Check if we are already preempted */ 50716292f54SDavid Cunado if (get_yield_smc_active_flag(tsp_ctx->state)) 508239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 509239b04faSSoby Mathew 510916a2c1eSAchin Gupta cm_el1_sysregs_context_save(NON_SECURE); 511916a2c1eSAchin Gupta 512916a2c1eSAchin Gupta /* Save x1 and x2 for use by TSP_GET_ARGS call below */ 513239b04faSSoby Mathew store_tsp_args(tsp_ctx, x1, x2); 514916a2c1eSAchin Gupta 515916a2c1eSAchin Gupta /* 516916a2c1eSAchin Gupta * We are done stashing the non-secure context. Ask the 517916a2c1eSAchin Gupta * secure payload to do the work now. 518916a2c1eSAchin Gupta */ 519916a2c1eSAchin Gupta 520916a2c1eSAchin Gupta /* 521916a2c1eSAchin Gupta * Verify if there is a valid context to use, copy the 522916a2c1eSAchin Gupta * operation type and parameters to the secure context 523916a2c1eSAchin Gupta * and jump to the fast smc entry point in the secure 524916a2c1eSAchin Gupta * payload. Entry into S-EL1 will take place upon exit 525916a2c1eSAchin Gupta * from this function. 526916a2c1eSAchin Gupta */ 52708ab89d3SAndrew Thoelke assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE)); 528239b04faSSoby Mathew 529239b04faSSoby Mathew /* Set appropriate entry for SMC. 530239b04faSSoby Mathew * We expect the TSP to manage the PSTATE.I and PSTATE.F 531239b04faSSoby Mathew * flags as appropriate. 532239b04faSSoby Mathew */ 533239b04faSSoby Mathew if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_FAST) { 534239b04faSSoby Mathew cm_set_elr_el3(SECURE, (uint64_t) 535399fb08fSAndrew Thoelke &tsp_vectors->fast_smc_entry); 536239b04faSSoby Mathew } else { 53716292f54SDavid Cunado set_yield_smc_active_flag(tsp_ctx->state); 538239b04faSSoby Mathew cm_set_elr_el3(SECURE, (uint64_t) 53916292f54SDavid Cunado &tsp_vectors->yield_smc_entry); 54002446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 541f4f1ae77SSoby Mathew /* 542f4f1ae77SSoby Mathew * Enable the routing of NS interrupts to EL3 54316292f54SDavid Cunado * during processing of a Yielding SMC Call on 54416292f54SDavid Cunado * this core. 545f4f1ae77SSoby Mathew */ 546f4f1ae77SSoby Mathew enable_intr_rm_local(INTR_TYPE_NS, SECURE); 547f4f1ae77SSoby Mathew #endif 5481dd022caSJeenu Viswambharan 5491dd022caSJeenu Viswambharan #if EL3_EXCEPTION_HANDLING 5501dd022caSJeenu Viswambharan /* 5511dd022caSJeenu Viswambharan * With EL3 exception handling, while an SMC is 5521dd022caSJeenu Viswambharan * being processed, Non-secure interrupts can't 5531dd022caSJeenu Viswambharan * preempt Secure execution. However, for 5541dd022caSJeenu Viswambharan * yielding SMCs, we want preemption to happen; 5551dd022caSJeenu Viswambharan * so explicitly allow NS preemption in this 556472be0f7SJeenu Viswambharan * case, and supply the preemption return code 557472be0f7SJeenu Viswambharan * for TSP. 5581dd022caSJeenu Viswambharan */ 559472be0f7SJeenu Viswambharan ehf_allow_ns_preemption(TSP_PREEMPTED); 5601dd022caSJeenu Viswambharan #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); 58316292f54SDavid Cunado if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_YIELD) { 58416292f54SDavid Cunado clr_yield_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 58816292f54SDavid Cunado * after processing of a Yielding SMC Call on 58916292f54SDavid Cunado * this core is finished. 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 } 597a08a2014SDaniel Boulby assert(0); /* Unreachable */ 598916a2c1eSAchin Gupta 5993df6012aSDouglas Raillard /* 60016292f54SDavid Cunado * Request from the non-secure world to abort a preempted Yielding SMC 60116292f54SDavid Cunado * Call. 6023df6012aSDouglas Raillard */ 6033df6012aSDouglas Raillard case TSP_FID_ABORT: 6043df6012aSDouglas Raillard /* ABORT should only be invoked by normal world */ 6053df6012aSDouglas Raillard if (!ns) { 6063df6012aSDouglas Raillard assert(0); 6073df6012aSDouglas Raillard break; 6083df6012aSDouglas Raillard } 6093df6012aSDouglas Raillard 61057a5a56cSDouglas Raillard assert(handle == cm_get_context(NON_SECURE)); 61157a5a56cSDouglas Raillard cm_el1_sysregs_context_save(NON_SECURE); 61257a5a56cSDouglas Raillard 6133df6012aSDouglas Raillard /* Abort the preempted SMC request */ 61457a5a56cSDouglas Raillard if (!tspd_abort_preempted_smc(tsp_ctx)) { 6153df6012aSDouglas Raillard /* 6163df6012aSDouglas Raillard * If there was no preempted SMC to abort, return 6173df6012aSDouglas Raillard * SMC_UNK. 61857a5a56cSDouglas Raillard * 61957a5a56cSDouglas Raillard * Restoring the NON_SECURE context is not necessary as 62057a5a56cSDouglas Raillard * the synchronous entry did not take place if the 62157a5a56cSDouglas Raillard * return code of tspd_abort_preempted_smc is zero. 6223df6012aSDouglas Raillard */ 62357a5a56cSDouglas Raillard cm_set_next_eret_context(NON_SECURE); 6243df6012aSDouglas Raillard break; 62557a5a56cSDouglas Raillard } 62657a5a56cSDouglas Raillard 62757a5a56cSDouglas Raillard cm_el1_sysregs_context_restore(NON_SECURE); 62857a5a56cSDouglas Raillard cm_set_next_eret_context(NON_SECURE); 6297a317a70SAntonio Nino Diaz SMC_RET1(handle, SMC_OK); 630916a2c1eSAchin Gupta 631916a2c1eSAchin Gupta /* 632239b04faSSoby Mathew * Request from non secure world to resume the preempted 63316292f54SDavid Cunado * Yielding SMC Call. 634239b04faSSoby Mathew */ 635239b04faSSoby Mathew case TSP_FID_RESUME: 636239b04faSSoby Mathew /* RESUME should be invoked only by normal world */ 637239b04faSSoby Mathew if (!ns) { 638239b04faSSoby Mathew assert(0); 639239b04faSSoby Mathew break; 640239b04faSSoby Mathew } 641239b04faSSoby Mathew 642239b04faSSoby Mathew /* 643239b04faSSoby Mathew * This is a resume request from the non-secure client. 644239b04faSSoby Mathew * save the non-secure state and send the request to 645239b04faSSoby Mathew * the secure payload. 646239b04faSSoby Mathew */ 64708ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 648239b04faSSoby Mathew 649239b04faSSoby Mathew /* Check if we are already preempted before resume */ 65016292f54SDavid Cunado if (!get_yield_smc_active_flag(tsp_ctx->state)) 651239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 652239b04faSSoby Mathew 653239b04faSSoby Mathew cm_el1_sysregs_context_save(NON_SECURE); 654239b04faSSoby Mathew 655239b04faSSoby Mathew /* 656239b04faSSoby Mathew * We are done stashing the non-secure context. Ask the 657239b04faSSoby Mathew * secure payload to do the work now. 658239b04faSSoby Mathew */ 65902446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 660f4f1ae77SSoby Mathew /* 661f4f1ae77SSoby Mathew * Enable the routing of NS interrupts to EL3 during resumption 66216292f54SDavid Cunado * of a Yielding SMC Call on this core. 663f4f1ae77SSoby Mathew */ 664f4f1ae77SSoby Mathew enable_intr_rm_local(INTR_TYPE_NS, SECURE); 665f4f1ae77SSoby Mathew #endif 666f4f1ae77SSoby Mathew 6671dd022caSJeenu Viswambharan #if EL3_EXCEPTION_HANDLING 6681dd022caSJeenu Viswambharan /* 6691dd022caSJeenu Viswambharan * Allow the resumed yielding SMC processing to be preempted by 670472be0f7SJeenu Viswambharan * Non-secure interrupts. Also, supply the preemption return 671472be0f7SJeenu Viswambharan * code for TSP. 6721dd022caSJeenu Viswambharan */ 673472be0f7SJeenu Viswambharan ehf_allow_ns_preemption(TSP_PREEMPTED); 6741dd022caSJeenu Viswambharan #endif 675239b04faSSoby Mathew 676239b04faSSoby Mathew /* We just need to return to the preempted point in 677239b04faSSoby Mathew * TSP and the execution will resume as normal. 678239b04faSSoby Mathew */ 679239b04faSSoby Mathew cm_el1_sysregs_context_restore(SECURE); 680239b04faSSoby Mathew cm_set_next_eret_context(SECURE); 68110b65ecfSSoby Mathew SMC_RET0(&tsp_ctx->cpu_ctx); 682239b04faSSoby Mathew 683239b04faSSoby Mathew /* 684916a2c1eSAchin Gupta * This is a request from the secure payload for more arguments 685916a2c1eSAchin Gupta * for an ongoing arithmetic operation requested by the 686916a2c1eSAchin Gupta * non-secure world. Simply return the arguments from the non- 687916a2c1eSAchin Gupta * secure client in the original call. 688916a2c1eSAchin Gupta */ 689916a2c1eSAchin Gupta case TSP_GET_ARGS: 690916a2c1eSAchin Gupta if (ns) 691916a2c1eSAchin Gupta SMC_RET1(handle, SMC_UNK); 692916a2c1eSAchin Gupta 693239b04faSSoby Mathew get_tsp_args(tsp_ctx, x1, x2); 694239b04faSSoby Mathew SMC_RET2(handle, x1, x2); 695916a2c1eSAchin Gupta 69652538b9bSJeenu Viswambharan case TOS_CALL_COUNT: 69752538b9bSJeenu Viswambharan /* 69852538b9bSJeenu Viswambharan * Return the number of service function IDs implemented to 69952538b9bSJeenu Viswambharan * provide service to non-secure 70052538b9bSJeenu Viswambharan */ 70152538b9bSJeenu Viswambharan SMC_RET1(handle, TSP_NUM_FID); 70252538b9bSJeenu Viswambharan 70352538b9bSJeenu Viswambharan case TOS_UID: 70452538b9bSJeenu Viswambharan /* Return TSP UID to the caller */ 70552538b9bSJeenu Viswambharan SMC_UUID_RET(handle, tsp_uuid); 70652538b9bSJeenu Viswambharan 70752538b9bSJeenu Viswambharan case TOS_CALL_VERSION: 70852538b9bSJeenu Viswambharan /* Return the version of current implementation */ 70952538b9bSJeenu Viswambharan SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR); 71052538b9bSJeenu Viswambharan 711375f538aSAchin Gupta default: 712607084eeSAchin Gupta break; 713375f538aSAchin Gupta } 714375f538aSAchin Gupta 715607084eeSAchin Gupta SMC_RET1(handle, SMC_UNK); 716375f538aSAchin Gupta } 717375f538aSAchin Gupta 718239b04faSSoby Mathew /* Define a SPD runtime service descriptor for fast SMC calls */ 719375f538aSAchin Gupta DECLARE_RT_SVC( 720239b04faSSoby Mathew tspd_fast, 721375f538aSAchin Gupta 722375f538aSAchin Gupta OEN_TOS_START, 723375f538aSAchin Gupta OEN_TOS_END, 724375f538aSAchin Gupta SMC_TYPE_FAST, 725375f538aSAchin Gupta tspd_setup, 726375f538aSAchin Gupta tspd_smc_handler 727375f538aSAchin Gupta ); 728239b04faSSoby Mathew 72916292f54SDavid Cunado /* Define a SPD runtime service descriptor for Yielding SMC Calls */ 730239b04faSSoby Mathew DECLARE_RT_SVC( 731239b04faSSoby Mathew tspd_std, 732239b04faSSoby Mathew 733239b04faSSoby Mathew OEN_TOS_START, 734239b04faSSoby Mathew OEN_TOS_END, 73516292f54SDavid Cunado SMC_TYPE_YIELD, 736239b04faSSoby Mathew NULL, 737239b04faSSoby Mathew tspd_smc_handler 738239b04faSSoby Mathew ); 739