1375f538aSAchin Gupta /* 2*1dd022caSJeenu Viswambharan * Copyright (c) 2013-2018, 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 ******************************************************************************/ 17375f538aSAchin Gupta #include <arch_helpers.h> 1897043ac9SDan Handley #include <assert.h> 1997043ac9SDan Handley #include <bl31.h> 202a4b4b71SIsla Mitchell #include <bl_common.h> 21375f538aSAchin Gupta #include <context_mgmt.h> 22b44a4435SAchin Gupta #include <debug.h> 23*1dd022caSJeenu Viswambharan #include <ehf.h> 24b44a4435SAchin Gupta #include <errno.h> 25b44a4435SAchin Gupta #include <platform.h> 26375f538aSAchin Gupta #include <runtime_svc.h> 2797043ac9SDan Handley #include <stddef.h> 28f4f1ae77SSoby Mathew #include <string.h> 29375f538aSAchin Gupta #include <tsp.h> 3052538b9bSJeenu Viswambharan #include <uuid.h> 3135e98e55SDan Handley #include "tspd_private.h" 32375f538aSAchin Gupta 33375f538aSAchin Gupta /******************************************************************************* 34399fb08fSAndrew Thoelke * Address of the entrypoint vector table in the Secure Payload. It is 35399fb08fSAndrew Thoelke * initialised once on the primary core after a cold boot. 36375f538aSAchin Gupta ******************************************************************************/ 37399fb08fSAndrew Thoelke tsp_vectors_t *tsp_vectors; 38375f538aSAchin Gupta 39375f538aSAchin Gupta /******************************************************************************* 40375f538aSAchin Gupta * Array to keep track of per-cpu Secure Payload state 41375f538aSAchin Gupta ******************************************************************************/ 42fb037bfbSDan Handley tsp_context_t tspd_sp_context[TSPD_CORE_COUNT]; 43375f538aSAchin Gupta 447f366605SJeenu Viswambharan 4552538b9bSJeenu Viswambharan /* TSP UID */ 4652538b9bSJeenu Viswambharan DEFINE_SVC_UUID(tsp_uuid, 4752538b9bSJeenu Viswambharan 0x5b3056a0, 0x3291, 0x427b, 0x98, 0x11, 4852538b9bSJeenu Viswambharan 0x71, 0x68, 0xca, 0x50, 0xf3, 0xfa); 4952538b9bSJeenu Viswambharan 506871c5d3SVikram Kanigiri int32_t tspd_init(void); 517f366605SJeenu Viswambharan 52404dba53SSoby Mathew /* 53404dba53SSoby Mathew * This helper function handles Secure EL1 preemption. The preemption could be 54404dba53SSoby Mathew * due Non Secure interrupts or EL3 interrupts. In both the cases we context 55404dba53SSoby Mathew * switch to the normal world and in case of EL3 interrupts, it will again be 56404dba53SSoby Mathew * routed to EL3 which will get handled at the exception vectors. 57404dba53SSoby Mathew */ 58f4f1ae77SSoby Mathew uint64_t tspd_handle_sp_preemption(void *handle) 59f4f1ae77SSoby Mathew { 60f4f1ae77SSoby Mathew cpu_context_t *ns_cpu_context; 61404dba53SSoby Mathew 62f4f1ae77SSoby Mathew assert(handle == cm_get_context(SECURE)); 63f4f1ae77SSoby Mathew cm_el1_sysregs_context_save(SECURE); 64f4f1ae77SSoby Mathew /* Get a reference to the non-secure context */ 65f4f1ae77SSoby Mathew ns_cpu_context = cm_get_context(NON_SECURE); 66f4f1ae77SSoby Mathew assert(ns_cpu_context); 67f4f1ae77SSoby Mathew 68f4f1ae77SSoby Mathew /* 6963b8440fSSoby Mathew * To allow Secure EL1 interrupt handler to re-enter TSP while TSP 7063b8440fSSoby Mathew * is preempted, the secure system register context which will get 7163b8440fSSoby Mathew * overwritten must be additionally saved. This is currently done 7263b8440fSSoby Mathew * by the TSPD S-EL1 interrupt handler. 7363b8440fSSoby Mathew */ 7463b8440fSSoby Mathew 7563b8440fSSoby Mathew /* 7663b8440fSSoby Mathew * Restore non-secure state. 77f4f1ae77SSoby Mathew */ 78f4f1ae77SSoby Mathew cm_el1_sysregs_context_restore(NON_SECURE); 79f4f1ae77SSoby Mathew cm_set_next_eret_context(NON_SECURE); 80f4f1ae77SSoby Mathew 81404dba53SSoby Mathew /* 8216292f54SDavid Cunado * The TSP was preempted during execution of a Yielding SMC Call. 8363b8440fSSoby Mathew * Return back to the normal world with SMC_PREEMPTED as error 8463b8440fSSoby Mathew * code in x0. 85404dba53SSoby Mathew */ 86f4f1ae77SSoby Mathew SMC_RET1(ns_cpu_context, SMC_PREEMPTED); 87f4f1ae77SSoby Mathew } 88404dba53SSoby Mathew 89b44a4435SAchin Gupta /******************************************************************************* 90b44a4435SAchin Gupta * This function is the handler registered for S-EL1 interrupts by the TSPD. It 91b44a4435SAchin Gupta * validates the interrupt and upon success arranges entry into the TSP at 9202446137SSoby Mathew * 'tsp_sel1_intr_entry()' for handling the interrupt. 93b44a4435SAchin Gupta ******************************************************************************/ 94b44a4435SAchin Gupta static uint64_t tspd_sel1_interrupt_handler(uint32_t id, 95b44a4435SAchin Gupta uint32_t flags, 96b44a4435SAchin Gupta void *handle, 97b44a4435SAchin Gupta void *cookie) 98b44a4435SAchin Gupta { 99b44a4435SAchin Gupta uint32_t linear_id; 100b44a4435SAchin Gupta tsp_context_t *tsp_ctx; 101b44a4435SAchin Gupta 102b44a4435SAchin Gupta /* Check the security state when the exception was generated */ 103b44a4435SAchin Gupta assert(get_interrupt_src_ss(flags) == NON_SECURE); 104b44a4435SAchin Gupta 105b44a4435SAchin Gupta /* Sanity check the pointer to this cpu's context */ 10608ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 107b44a4435SAchin Gupta 108b44a4435SAchin Gupta /* Save the non-secure context before entering the TSP */ 109b44a4435SAchin Gupta cm_el1_sysregs_context_save(NON_SECURE); 110b44a4435SAchin Gupta 111b44a4435SAchin Gupta /* Get a reference to this cpu's TSP context */ 112fd650ff6SSoby Mathew linear_id = plat_my_core_pos(); 113b44a4435SAchin Gupta tsp_ctx = &tspd_sp_context[linear_id]; 11408ab89d3SAndrew Thoelke assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE)); 115b44a4435SAchin Gupta 116b44a4435SAchin Gupta /* 117b44a4435SAchin Gupta * Determine if the TSP was previously preempted. Its last known 118b44a4435SAchin Gupta * context has to be preserved in this case. 119b44a4435SAchin Gupta * The TSP should return control to the TSPD after handling this 12002446137SSoby Mathew * S-EL1 interrupt. Preserve essential EL3 context to allow entry into 12102446137SSoby Mathew * the TSP at the S-EL1 interrupt entry point using the 'cpu_context' 12202446137SSoby Mathew * structure. There is no need to save the secure system register 12302446137SSoby Mathew * context since the TSP is supposed to preserve it during S-EL1 12402446137SSoby Mathew * interrupt handling. 125b44a4435SAchin Gupta */ 12616292f54SDavid Cunado if (get_yield_smc_active_flag(tsp_ctx->state)) { 127b44a4435SAchin Gupta tsp_ctx->saved_spsr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx, 128b44a4435SAchin Gupta CTX_SPSR_EL3); 129b44a4435SAchin Gupta tsp_ctx->saved_elr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx, 130b44a4435SAchin Gupta CTX_ELR_EL3); 13102446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 132f4f1ae77SSoby Mathew /*Need to save the previously interrupted secure context */ 133f4f1ae77SSoby Mathew memcpy(&tsp_ctx->sp_ctx, &tsp_ctx->cpu_ctx, TSPD_SP_CTX_SIZE); 134f4f1ae77SSoby Mathew #endif 135b44a4435SAchin Gupta } 136b44a4435SAchin Gupta 137b44a4435SAchin Gupta cm_el1_sysregs_context_restore(SECURE); 13802446137SSoby Mathew cm_set_elr_spsr_el3(SECURE, (uint64_t) &tsp_vectors->sel1_intr_entry, 139167a9357SAndrew Thoelke SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS)); 140f4f1ae77SSoby Mathew 141b44a4435SAchin Gupta cm_set_next_eret_context(SECURE); 142b44a4435SAchin Gupta 143b44a4435SAchin Gupta /* 14402446137SSoby Mathew * Tell the TSP that it has to handle a S-EL1 interrupt synchronously. 14502446137SSoby Mathew * Also the instruction in normal world where the interrupt was 14602446137SSoby Mathew * generated is passed for debugging purposes. It is safe to retrieve 14702446137SSoby Mathew * this address from ELR_EL3 as the secure context will not take effect 14802446137SSoby Mathew * until el3_exit(). 149b44a4435SAchin Gupta */ 15002446137SSoby Mathew SMC_RET2(&tsp_ctx->cpu_ctx, TSP_HANDLE_SEL1_INTR_AND_RETURN, read_elr_el3()); 151b44a4435SAchin Gupta } 1527f366605SJeenu Viswambharan 15302446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 154f4f1ae77SSoby Mathew /******************************************************************************* 15502446137SSoby Mathew * This function is the handler registered for Non secure interrupts by the 15602446137SSoby Mathew * TSPD. It validates the interrupt and upon success arranges entry into the 15702446137SSoby Mathew * normal world for handling the interrupt. 158f4f1ae77SSoby Mathew ******************************************************************************/ 159f4f1ae77SSoby Mathew static uint64_t tspd_ns_interrupt_handler(uint32_t id, 160f4f1ae77SSoby Mathew uint32_t flags, 161f4f1ae77SSoby Mathew void *handle, 162f4f1ae77SSoby Mathew void *cookie) 163f4f1ae77SSoby Mathew { 164f4f1ae77SSoby Mathew /* Check the security state when the exception was generated */ 165f4f1ae77SSoby Mathew assert(get_interrupt_src_ss(flags) == SECURE); 166f4f1ae77SSoby Mathew 167f4f1ae77SSoby Mathew /* 168f4f1ae77SSoby Mathew * Disable the routing of NS interrupts from secure world to EL3 while 169f4f1ae77SSoby Mathew * interrupted on this core. 170f4f1ae77SSoby Mathew */ 171f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 172f4f1ae77SSoby Mathew 173f4f1ae77SSoby Mathew return tspd_handle_sp_preemption(handle); 174f4f1ae77SSoby Mathew } 175f4f1ae77SSoby Mathew #endif 176f4f1ae77SSoby Mathew 177375f538aSAchin Gupta /******************************************************************************* 178375f538aSAchin Gupta * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type 179375f538aSAchin Gupta * (aarch32/aarch64) if not already known and initialises the context for entry 180375f538aSAchin Gupta * into the SP for its initialisation. 181375f538aSAchin Gupta ******************************************************************************/ 182375f538aSAchin Gupta int32_t tspd_setup(void) 183375f538aSAchin Gupta { 18450e27dadSVikram Kanigiri entry_point_info_t *tsp_ep_info; 185375f538aSAchin Gupta uint32_t linear_id; 186375f538aSAchin Gupta 187fd650ff6SSoby Mathew linear_id = plat_my_core_pos(); 188375f538aSAchin Gupta 189375f538aSAchin Gupta /* 190375f538aSAchin Gupta * Get information about the Secure Payload (BL32) image. Its 191375f538aSAchin Gupta * absence is a critical failure. TODO: Add support to 192375f538aSAchin Gupta * conditionally include the SPD service 193375f538aSAchin Gupta */ 19450e27dadSVikram Kanigiri tsp_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 19550e27dadSVikram Kanigiri if (!tsp_ep_info) { 19650e27dadSVikram Kanigiri WARN("No TSP provided by BL2 boot loader, Booting device" 19750e27dadSVikram Kanigiri " without TSP initialization. SMC`s destined for TSP" 19850e27dadSVikram Kanigiri " will return SMC_UNK\n"); 19950e27dadSVikram Kanigiri return 1; 20050e27dadSVikram Kanigiri } 201375f538aSAchin Gupta 202375f538aSAchin Gupta /* 2037f366605SJeenu Viswambharan * If there's no valid entry point for SP, we return a non-zero value 2047f366605SJeenu Viswambharan * signalling failure initializing the service. We bail out without 2057f366605SJeenu Viswambharan * registering any handlers 2067f366605SJeenu Viswambharan */ 20750e27dadSVikram Kanigiri if (!tsp_ep_info->pc) 2087f366605SJeenu Viswambharan return 1; 2097f366605SJeenu Viswambharan 2107f366605SJeenu Viswambharan /* 2111645d3eeSSandrine Bailleux * We could inspect the SP image and determine its execution 212375f538aSAchin Gupta * state i.e whether AArch32 or AArch64. Assuming it's AArch64 213375f538aSAchin Gupta * for the time being. 214375f538aSAchin Gupta */ 21550e27dadSVikram Kanigiri tspd_init_tsp_ep_state(tsp_ep_info, 216375f538aSAchin Gupta TSP_AARCH64, 21750e27dadSVikram Kanigiri tsp_ep_info->pc, 218375f538aSAchin Gupta &tspd_sp_context[linear_id]); 219375f538aSAchin Gupta 220faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 221faaa2e76SVikram Kanigiri bl31_set_next_image_type(SECURE); 222faaa2e76SVikram Kanigiri #else 2237f366605SJeenu Viswambharan /* 2247f366605SJeenu Viswambharan * All TSPD initialization done. Now register our init function with 2257f366605SJeenu Viswambharan * BL31 for deferred invocation 2267f366605SJeenu Viswambharan */ 2277f366605SJeenu Viswambharan bl31_register_bl32_init(&tspd_init); 228faaa2e76SVikram Kanigiri #endif 22950e27dadSVikram Kanigiri return 0; 230375f538aSAchin Gupta } 231375f538aSAchin Gupta 232375f538aSAchin Gupta /******************************************************************************* 233375f538aSAchin Gupta * This function passes control to the Secure Payload image (BL32) for the first 234375f538aSAchin Gupta * time on the primary cpu after a cold boot. It assumes that a valid secure 235375f538aSAchin Gupta * context has already been created by tspd_setup() which can be directly used. 236375f538aSAchin Gupta * It also assumes that a valid non-secure context has been initialised by PSCI 237375f538aSAchin Gupta * so it does not need to save and restore any non-secure state. This function 238375f538aSAchin Gupta * performs a synchronous entry into the Secure payload. The SP passes control 2396871c5d3SVikram Kanigiri * back to this routine through a SMC. 240375f538aSAchin Gupta ******************************************************************************/ 2416871c5d3SVikram Kanigiri int32_t tspd_init(void) 242375f538aSAchin Gupta { 243fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 244fb037bfbSDan Handley tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 24550e27dadSVikram Kanigiri entry_point_info_t *tsp_entry_point; 246faaa2e76SVikram Kanigiri uint64_t rc; 24750e27dadSVikram Kanigiri 24850e27dadSVikram Kanigiri /* 24950e27dadSVikram Kanigiri * Get information about the Secure Payload (BL32) image. Its 25050e27dadSVikram Kanigiri * absence is a critical failure. 25150e27dadSVikram Kanigiri */ 25250e27dadSVikram Kanigiri tsp_entry_point = bl31_plat_get_next_image_ep_info(SECURE); 25350e27dadSVikram Kanigiri assert(tsp_entry_point); 25450e27dadSVikram Kanigiri 255fd650ff6SSoby Mathew cm_init_my_context(tsp_entry_point); 256375f538aSAchin Gupta 257375f538aSAchin Gupta /* 258faaa2e76SVikram Kanigiri * Arrange for an entry into the test secure payload. It will be 259faaa2e76SVikram Kanigiri * returned via TSP_ENTRY_DONE case 260607084eeSAchin Gupta */ 261375f538aSAchin Gupta rc = tspd_synchronous_sp_entry(tsp_ctx); 262375f538aSAchin Gupta assert(rc != 0); 263b44a4435SAchin Gupta 264375f538aSAchin Gupta return rc; 265375f538aSAchin Gupta } 266375f538aSAchin Gupta 2677f366605SJeenu Viswambharan 268375f538aSAchin Gupta /******************************************************************************* 269375f538aSAchin Gupta * This function is responsible for handling all SMCs in the Trusted OS/App 270375f538aSAchin Gupta * range from the non-secure state as defined in the SMC Calling Convention 271375f538aSAchin Gupta * Document. It is also responsible for communicating with the Secure payload 272375f538aSAchin Gupta * to delegate work and return results back to the non-secure state. Lastly it 273375f538aSAchin Gupta * will also return any information that the secure payload needs to do the 274375f538aSAchin Gupta * work assigned to it. 275375f538aSAchin Gupta ******************************************************************************/ 276375f538aSAchin Gupta uint64_t tspd_smc_handler(uint32_t smc_fid, 277375f538aSAchin Gupta uint64_t x1, 278375f538aSAchin Gupta uint64_t x2, 279375f538aSAchin Gupta uint64_t x3, 280375f538aSAchin Gupta uint64_t x4, 281375f538aSAchin Gupta void *cookie, 282375f538aSAchin Gupta void *handle, 283375f538aSAchin Gupta uint64_t flags) 284375f538aSAchin Gupta { 285fb037bfbSDan Handley cpu_context_t *ns_cpu_context; 286fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(), ns; 287fb037bfbSDan Handley tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 288faaa2e76SVikram Kanigiri uint64_t rc; 289faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 290faaa2e76SVikram Kanigiri entry_point_info_t *next_image_info; 291faaa2e76SVikram Kanigiri #endif 292375f538aSAchin Gupta 293375f538aSAchin Gupta /* Determine which security state this SMC originated from */ 294375f538aSAchin Gupta ns = is_caller_non_secure(flags); 295375f538aSAchin Gupta 296375f538aSAchin Gupta switch (smc_fid) { 297375f538aSAchin Gupta 298375f538aSAchin Gupta /* 299239b04faSSoby Mathew * This function ID is used by TSP to indicate that it was 300239b04faSSoby Mathew * preempted by a normal world IRQ. 301239b04faSSoby Mathew * 302239b04faSSoby Mathew */ 303239b04faSSoby Mathew case TSP_PREEMPTED: 304239b04faSSoby Mathew if (ns) 305239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 306239b04faSSoby Mathew 307f4f1ae77SSoby Mathew return tspd_handle_sp_preemption(handle); 308239b04faSSoby Mathew 309239b04faSSoby Mathew /* 310b44a4435SAchin Gupta * This function ID is used only by the TSP to indicate that it has 31163b8440fSSoby Mathew * finished handling a S-EL1 interrupt or was preempted by a higher 31263b8440fSSoby Mathew * priority pending EL3 interrupt. Execution should resume 313b44a4435SAchin Gupta * in the normal world. 314b44a4435SAchin Gupta */ 31502446137SSoby Mathew case TSP_HANDLED_S_EL1_INTR: 316b44a4435SAchin Gupta if (ns) 317b44a4435SAchin Gupta SMC_RET1(handle, SMC_UNK); 318b44a4435SAchin Gupta 31908ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 320b44a4435SAchin Gupta 321b44a4435SAchin Gupta /* 322b44a4435SAchin Gupta * Restore the relevant EL3 state which saved to service 323b44a4435SAchin Gupta * this SMC. 324b44a4435SAchin Gupta */ 32516292f54SDavid Cunado if (get_yield_smc_active_flag(tsp_ctx->state)) { 326b44a4435SAchin Gupta SMC_SET_EL3(&tsp_ctx->cpu_ctx, 327b44a4435SAchin Gupta CTX_SPSR_EL3, 328b44a4435SAchin Gupta tsp_ctx->saved_spsr_el3); 329b44a4435SAchin Gupta SMC_SET_EL3(&tsp_ctx->cpu_ctx, 330b44a4435SAchin Gupta CTX_ELR_EL3, 331b44a4435SAchin Gupta tsp_ctx->saved_elr_el3); 33202446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 333f4f1ae77SSoby Mathew /* 334f4f1ae77SSoby Mathew * Need to restore the previously interrupted 335f4f1ae77SSoby Mathew * secure context. 336f4f1ae77SSoby Mathew */ 337f4f1ae77SSoby Mathew memcpy(&tsp_ctx->cpu_ctx, &tsp_ctx->sp_ctx, 338f4f1ae77SSoby Mathew TSPD_SP_CTX_SIZE); 339f4f1ae77SSoby Mathew #endif 340b44a4435SAchin Gupta } 341b44a4435SAchin Gupta 342b44a4435SAchin Gupta /* Get a reference to the non-secure context */ 34308ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 344b44a4435SAchin Gupta assert(ns_cpu_context); 345b44a4435SAchin Gupta 346b44a4435SAchin Gupta /* 347b44a4435SAchin Gupta * Restore non-secure state. There is no need to save the 348b44a4435SAchin Gupta * secure system register context since the TSP was supposed 349b44a4435SAchin Gupta * to preserve it during S-EL1 interrupt handling. 350b44a4435SAchin Gupta */ 351b44a4435SAchin Gupta cm_el1_sysregs_context_restore(NON_SECURE); 352b44a4435SAchin Gupta cm_set_next_eret_context(NON_SECURE); 353b44a4435SAchin Gupta 354b44a4435SAchin Gupta SMC_RET0((uint64_t) ns_cpu_context); 355b44a4435SAchin Gupta 356b44a4435SAchin Gupta /* 357375f538aSAchin Gupta * This function ID is used only by the SP to indicate it has 358375f538aSAchin Gupta * finished initialising itself after a cold boot 359375f538aSAchin Gupta */ 360375f538aSAchin Gupta case TSP_ENTRY_DONE: 361375f538aSAchin Gupta if (ns) 362375f538aSAchin Gupta SMC_RET1(handle, SMC_UNK); 363375f538aSAchin Gupta 364375f538aSAchin Gupta /* 365375f538aSAchin Gupta * Stash the SP entry points information. This is done 366375f538aSAchin Gupta * only once on the primary cpu 367375f538aSAchin Gupta */ 368399fb08fSAndrew Thoelke assert(tsp_vectors == NULL); 369399fb08fSAndrew Thoelke tsp_vectors = (tsp_vectors_t *) x1; 370375f538aSAchin Gupta 371faaa2e76SVikram Kanigiri if (tsp_vectors) { 372faaa2e76SVikram Kanigiri set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON); 373faaa2e76SVikram Kanigiri 374faaa2e76SVikram Kanigiri /* 375faaa2e76SVikram Kanigiri * TSP has been successfully initialized. Register power 376faaa2e76SVikram Kanigiri * managemnt hooks with PSCI 377faaa2e76SVikram Kanigiri */ 378faaa2e76SVikram Kanigiri psci_register_spd_pm_hook(&tspd_pm); 379faaa2e76SVikram Kanigiri 380faaa2e76SVikram Kanigiri /* 381faaa2e76SVikram Kanigiri * Register an interrupt handler for S-EL1 interrupts 382faaa2e76SVikram Kanigiri * when generated during code executing in the 383faaa2e76SVikram Kanigiri * non-secure state. 384faaa2e76SVikram Kanigiri */ 385faaa2e76SVikram Kanigiri flags = 0; 386faaa2e76SVikram Kanigiri set_interrupt_rm_flag(flags, NON_SECURE); 387faaa2e76SVikram Kanigiri rc = register_interrupt_type_handler(INTR_TYPE_S_EL1, 388faaa2e76SVikram Kanigiri tspd_sel1_interrupt_handler, 389faaa2e76SVikram Kanigiri flags); 390faaa2e76SVikram Kanigiri if (rc) 391faaa2e76SVikram Kanigiri panic(); 392f4f1ae77SSoby Mathew 39302446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 394f4f1ae77SSoby Mathew /* 395f4f1ae77SSoby Mathew * Register an interrupt handler for NS interrupts when 396f4f1ae77SSoby Mathew * generated during code executing in secure state are 397f4f1ae77SSoby Mathew * routed to EL3. 398f4f1ae77SSoby Mathew */ 399f4f1ae77SSoby Mathew flags = 0; 400f4f1ae77SSoby Mathew set_interrupt_rm_flag(flags, SECURE); 401f4f1ae77SSoby Mathew 402f4f1ae77SSoby Mathew rc = register_interrupt_type_handler(INTR_TYPE_NS, 403f4f1ae77SSoby Mathew tspd_ns_interrupt_handler, 404f4f1ae77SSoby Mathew flags); 405f4f1ae77SSoby Mathew if (rc) 406f4f1ae77SSoby Mathew panic(); 407f4f1ae77SSoby Mathew 408f4f1ae77SSoby Mathew /* 409404dba53SSoby Mathew * Disable the NS interrupt locally. 410f4f1ae77SSoby Mathew */ 411f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 412f4f1ae77SSoby Mathew #endif 413faaa2e76SVikram Kanigiri } 414faaa2e76SVikram Kanigiri 415faaa2e76SVikram Kanigiri 416faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 417faaa2e76SVikram Kanigiri /* Save the Secure EL1 system register context */ 418faaa2e76SVikram Kanigiri assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx); 419faaa2e76SVikram Kanigiri cm_el1_sysregs_context_save(SECURE); 420faaa2e76SVikram Kanigiri 421faaa2e76SVikram Kanigiri /* Program EL3 registers to enable entry into the next EL */ 422faaa2e76SVikram Kanigiri next_image_info = bl31_plat_get_next_image_ep_info(NON_SECURE); 423faaa2e76SVikram Kanigiri assert(next_image_info); 424faaa2e76SVikram Kanigiri assert(NON_SECURE == 425faaa2e76SVikram Kanigiri GET_SECURITY_STATE(next_image_info->h.attr)); 426faaa2e76SVikram Kanigiri 427fd650ff6SSoby Mathew cm_init_my_context(next_image_info); 428faaa2e76SVikram Kanigiri cm_prepare_el3_exit(NON_SECURE); 429faaa2e76SVikram Kanigiri SMC_RET0(cm_get_context(NON_SECURE)); 430faaa2e76SVikram Kanigiri #else 431375f538aSAchin Gupta /* 432375f538aSAchin Gupta * SP reports completion. The SPD must have initiated 433375f538aSAchin Gupta * the original request through a synchronous entry 434375f538aSAchin Gupta * into the SP. Jump back to the original C runtime 435375f538aSAchin Gupta * context. 436375f538aSAchin Gupta */ 437916a2c1eSAchin Gupta tspd_synchronous_sp_exit(tsp_ctx, x1); 438faaa2e76SVikram Kanigiri #endif 4393df6012aSDouglas Raillard /* 4403df6012aSDouglas Raillard * This function ID is used only by the SP to indicate it has finished 44116292f54SDavid Cunado * aborting a preempted Yielding SMC Call. 4423df6012aSDouglas Raillard */ 4433df6012aSDouglas Raillard case TSP_ABORT_DONE: 444375f538aSAchin Gupta 445607084eeSAchin Gupta /* 4461645d3eeSSandrine Bailleux * These function IDs are used only by the SP to indicate it has 447607084eeSAchin Gupta * finished: 448607084eeSAchin Gupta * 1. turning itself on in response to an earlier psci 449607084eeSAchin Gupta * cpu_on request 450607084eeSAchin Gupta * 2. resuming itself after an earlier psci cpu_suspend 451607084eeSAchin Gupta * request. 452607084eeSAchin Gupta */ 453607084eeSAchin Gupta case TSP_ON_DONE: 454607084eeSAchin Gupta case TSP_RESUME_DONE: 455607084eeSAchin Gupta 456607084eeSAchin Gupta /* 4571645d3eeSSandrine Bailleux * These function IDs are used only by the SP to indicate it has 458607084eeSAchin Gupta * finished: 459607084eeSAchin Gupta * 1. suspending itself after an earlier psci cpu_suspend 460607084eeSAchin Gupta * request. 461607084eeSAchin Gupta * 2. turning itself off in response to an earlier psci 462607084eeSAchin Gupta * cpu_off request. 463607084eeSAchin Gupta */ 464607084eeSAchin Gupta case TSP_OFF_DONE: 465607084eeSAchin Gupta case TSP_SUSPEND_DONE: 466d5f13093SJuan Castillo case TSP_SYSTEM_OFF_DONE: 467d5f13093SJuan Castillo case TSP_SYSTEM_RESET_DONE: 468607084eeSAchin Gupta if (ns) 469607084eeSAchin Gupta SMC_RET1(handle, SMC_UNK); 470607084eeSAchin Gupta 471607084eeSAchin Gupta /* 472607084eeSAchin Gupta * SP reports completion. The SPD must have initiated the 473607084eeSAchin Gupta * original request through a synchronous entry into the SP. 474607084eeSAchin Gupta * Jump back to the original C runtime context, and pass x1 as 475607084eeSAchin Gupta * return value to the caller 476607084eeSAchin Gupta */ 477916a2c1eSAchin Gupta tspd_synchronous_sp_exit(tsp_ctx, x1); 478607084eeSAchin Gupta 479916a2c1eSAchin Gupta /* 480916a2c1eSAchin Gupta * Request from non-secure client to perform an 481916a2c1eSAchin Gupta * arithmetic operation or response from secure 482916a2c1eSAchin Gupta * payload to an earlier request. 483916a2c1eSAchin Gupta */ 484239b04faSSoby Mathew case TSP_FAST_FID(TSP_ADD): 485239b04faSSoby Mathew case TSP_FAST_FID(TSP_SUB): 486239b04faSSoby Mathew case TSP_FAST_FID(TSP_MUL): 487239b04faSSoby Mathew case TSP_FAST_FID(TSP_DIV): 488239b04faSSoby Mathew 48916292f54SDavid Cunado case TSP_YIELD_FID(TSP_ADD): 49016292f54SDavid Cunado case TSP_YIELD_FID(TSP_SUB): 49116292f54SDavid Cunado case TSP_YIELD_FID(TSP_MUL): 49216292f54SDavid Cunado case TSP_YIELD_FID(TSP_DIV): 493916a2c1eSAchin Gupta if (ns) { 494916a2c1eSAchin Gupta /* 495916a2c1eSAchin Gupta * This is a fresh request from the non-secure client. 496916a2c1eSAchin Gupta * The parameters are in x1 and x2. Figure out which 497916a2c1eSAchin Gupta * registers need to be preserved, save the non-secure 498916a2c1eSAchin Gupta * state and send the request to the secure payload. 499916a2c1eSAchin Gupta */ 50008ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 501239b04faSSoby Mathew 502239b04faSSoby Mathew /* Check if we are already preempted */ 50316292f54SDavid Cunado if (get_yield_smc_active_flag(tsp_ctx->state)) 504239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 505239b04faSSoby Mathew 506916a2c1eSAchin Gupta cm_el1_sysregs_context_save(NON_SECURE); 507916a2c1eSAchin Gupta 508916a2c1eSAchin Gupta /* Save x1 and x2 for use by TSP_GET_ARGS call below */ 509239b04faSSoby Mathew store_tsp_args(tsp_ctx, x1, x2); 510916a2c1eSAchin Gupta 511916a2c1eSAchin Gupta /* 512916a2c1eSAchin Gupta * We are done stashing the non-secure context. Ask the 513916a2c1eSAchin Gupta * secure payload to do the work now. 514916a2c1eSAchin Gupta */ 515916a2c1eSAchin Gupta 516916a2c1eSAchin Gupta /* 517916a2c1eSAchin Gupta * Verify if there is a valid context to use, copy the 518916a2c1eSAchin Gupta * operation type and parameters to the secure context 519916a2c1eSAchin Gupta * and jump to the fast smc entry point in the secure 520916a2c1eSAchin Gupta * payload. Entry into S-EL1 will take place upon exit 521916a2c1eSAchin Gupta * from this function. 522916a2c1eSAchin Gupta */ 52308ab89d3SAndrew Thoelke assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE)); 524239b04faSSoby Mathew 525239b04faSSoby Mathew /* Set appropriate entry for SMC. 526239b04faSSoby Mathew * We expect the TSP to manage the PSTATE.I and PSTATE.F 527239b04faSSoby Mathew * flags as appropriate. 528239b04faSSoby Mathew */ 529239b04faSSoby Mathew if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_FAST) { 530239b04faSSoby Mathew cm_set_elr_el3(SECURE, (uint64_t) 531399fb08fSAndrew Thoelke &tsp_vectors->fast_smc_entry); 532239b04faSSoby Mathew } else { 53316292f54SDavid Cunado set_yield_smc_active_flag(tsp_ctx->state); 534239b04faSSoby Mathew cm_set_elr_el3(SECURE, (uint64_t) 53516292f54SDavid Cunado &tsp_vectors->yield_smc_entry); 53602446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 537f4f1ae77SSoby Mathew /* 538f4f1ae77SSoby Mathew * Enable the routing of NS interrupts to EL3 53916292f54SDavid Cunado * during processing of a Yielding SMC Call on 54016292f54SDavid Cunado * this core. 541f4f1ae77SSoby Mathew */ 542f4f1ae77SSoby Mathew enable_intr_rm_local(INTR_TYPE_NS, SECURE); 543f4f1ae77SSoby Mathew #endif 544*1dd022caSJeenu Viswambharan 545*1dd022caSJeenu Viswambharan #if EL3_EXCEPTION_HANDLING 546*1dd022caSJeenu Viswambharan /* 547*1dd022caSJeenu Viswambharan * With EL3 exception handling, while an SMC is 548*1dd022caSJeenu Viswambharan * being processed, Non-secure interrupts can't 549*1dd022caSJeenu Viswambharan * preempt Secure execution. However, for 550*1dd022caSJeenu Viswambharan * yielding SMCs, we want preemption to happen; 551*1dd022caSJeenu Viswambharan * so explicitly allow NS preemption in this 552*1dd022caSJeenu Viswambharan * case. 553*1dd022caSJeenu Viswambharan */ 554*1dd022caSJeenu Viswambharan ehf_allow_ns_preemption(); 555*1dd022caSJeenu Viswambharan #endif 556239b04faSSoby Mathew } 557239b04faSSoby Mathew 558916a2c1eSAchin Gupta cm_el1_sysregs_context_restore(SECURE); 559916a2c1eSAchin Gupta cm_set_next_eret_context(SECURE); 560239b04faSSoby Mathew SMC_RET3(&tsp_ctx->cpu_ctx, smc_fid, x1, x2); 561916a2c1eSAchin Gupta } else { 562916a2c1eSAchin Gupta /* 563916a2c1eSAchin Gupta * This is the result from the secure client of an 564239b04faSSoby Mathew * earlier request. The results are in x1-x3. Copy it 565916a2c1eSAchin Gupta * into the non-secure context, save the secure state 566916a2c1eSAchin Gupta * and return to the non-secure state. 567916a2c1eSAchin Gupta */ 56808ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 569916a2c1eSAchin Gupta cm_el1_sysregs_context_save(SECURE); 570916a2c1eSAchin Gupta 571916a2c1eSAchin Gupta /* Get a reference to the non-secure context */ 57208ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 573916a2c1eSAchin Gupta assert(ns_cpu_context); 574916a2c1eSAchin Gupta 575916a2c1eSAchin Gupta /* Restore non-secure state */ 576916a2c1eSAchin Gupta cm_el1_sysregs_context_restore(NON_SECURE); 577916a2c1eSAchin Gupta cm_set_next_eret_context(NON_SECURE); 57816292f54SDavid Cunado if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_YIELD) { 57916292f54SDavid Cunado clr_yield_smc_active_flag(tsp_ctx->state); 58002446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 581f4f1ae77SSoby Mathew /* 582f4f1ae77SSoby Mathew * Disable the routing of NS interrupts to EL3 58316292f54SDavid Cunado * after processing of a Yielding SMC Call on 58416292f54SDavid Cunado * this core is finished. 585f4f1ae77SSoby Mathew */ 586f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 587f4f1ae77SSoby Mathew #endif 588f4f1ae77SSoby Mathew } 589f4f1ae77SSoby Mathew 590239b04faSSoby Mathew SMC_RET3(ns_cpu_context, x1, x2, x3); 591916a2c1eSAchin Gupta } 592916a2c1eSAchin Gupta 593916a2c1eSAchin Gupta break; 5943df6012aSDouglas Raillard /* 59516292f54SDavid Cunado * Request from the non-secure world to abort a preempted Yielding SMC 59616292f54SDavid Cunado * Call. 5973df6012aSDouglas Raillard */ 5983df6012aSDouglas Raillard case TSP_FID_ABORT: 5993df6012aSDouglas Raillard /* ABORT should only be invoked by normal world */ 6003df6012aSDouglas Raillard if (!ns) { 6013df6012aSDouglas Raillard assert(0); 6023df6012aSDouglas Raillard break; 6033df6012aSDouglas Raillard } 6043df6012aSDouglas Raillard 60557a5a56cSDouglas Raillard assert(handle == cm_get_context(NON_SECURE)); 60657a5a56cSDouglas Raillard cm_el1_sysregs_context_save(NON_SECURE); 60757a5a56cSDouglas Raillard 6083df6012aSDouglas Raillard /* Abort the preempted SMC request */ 60957a5a56cSDouglas Raillard if (!tspd_abort_preempted_smc(tsp_ctx)) { 6103df6012aSDouglas Raillard /* 6113df6012aSDouglas Raillard * If there was no preempted SMC to abort, return 6123df6012aSDouglas Raillard * SMC_UNK. 61357a5a56cSDouglas Raillard * 61457a5a56cSDouglas Raillard * Restoring the NON_SECURE context is not necessary as 61557a5a56cSDouglas Raillard * the synchronous entry did not take place if the 61657a5a56cSDouglas Raillard * return code of tspd_abort_preempted_smc is zero. 6173df6012aSDouglas Raillard */ 61857a5a56cSDouglas Raillard cm_set_next_eret_context(NON_SECURE); 6193df6012aSDouglas Raillard break; 62057a5a56cSDouglas Raillard } 62157a5a56cSDouglas Raillard 62257a5a56cSDouglas Raillard cm_el1_sysregs_context_restore(NON_SECURE); 62357a5a56cSDouglas Raillard cm_set_next_eret_context(NON_SECURE); 6247a317a70SAntonio Nino Diaz SMC_RET1(handle, SMC_OK); 625916a2c1eSAchin Gupta 626916a2c1eSAchin Gupta /* 627239b04faSSoby Mathew * Request from non secure world to resume the preempted 62816292f54SDavid Cunado * Yielding SMC Call. 629239b04faSSoby Mathew */ 630239b04faSSoby Mathew case TSP_FID_RESUME: 631239b04faSSoby Mathew /* RESUME should be invoked only by normal world */ 632239b04faSSoby Mathew if (!ns) { 633239b04faSSoby Mathew assert(0); 634239b04faSSoby Mathew break; 635239b04faSSoby Mathew } 636239b04faSSoby Mathew 637239b04faSSoby Mathew /* 638239b04faSSoby Mathew * This is a resume request from the non-secure client. 639239b04faSSoby Mathew * save the non-secure state and send the request to 640239b04faSSoby Mathew * the secure payload. 641239b04faSSoby Mathew */ 64208ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 643239b04faSSoby Mathew 644239b04faSSoby Mathew /* Check if we are already preempted before resume */ 64516292f54SDavid Cunado if (!get_yield_smc_active_flag(tsp_ctx->state)) 646239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 647239b04faSSoby Mathew 648239b04faSSoby Mathew cm_el1_sysregs_context_save(NON_SECURE); 649239b04faSSoby Mathew 650239b04faSSoby Mathew /* 651239b04faSSoby Mathew * We are done stashing the non-secure context. Ask the 652239b04faSSoby Mathew * secure payload to do the work now. 653239b04faSSoby Mathew */ 65402446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 655f4f1ae77SSoby Mathew /* 656f4f1ae77SSoby Mathew * Enable the routing of NS interrupts to EL3 during resumption 65716292f54SDavid Cunado * of a Yielding SMC Call on this core. 658f4f1ae77SSoby Mathew */ 659f4f1ae77SSoby Mathew enable_intr_rm_local(INTR_TYPE_NS, SECURE); 660f4f1ae77SSoby Mathew #endif 661f4f1ae77SSoby Mathew 662*1dd022caSJeenu Viswambharan #if EL3_EXCEPTION_HANDLING 663*1dd022caSJeenu Viswambharan /* 664*1dd022caSJeenu Viswambharan * Allow the resumed yielding SMC processing to be preempted by 665*1dd022caSJeenu Viswambharan * Non-secure interrupts. 666*1dd022caSJeenu Viswambharan */ 667*1dd022caSJeenu Viswambharan ehf_allow_ns_preemption(); 668*1dd022caSJeenu Viswambharan #endif 669239b04faSSoby Mathew 670239b04faSSoby Mathew /* We just need to return to the preempted point in 671239b04faSSoby Mathew * TSP and the execution will resume as normal. 672239b04faSSoby Mathew */ 673239b04faSSoby Mathew cm_el1_sysregs_context_restore(SECURE); 674239b04faSSoby Mathew cm_set_next_eret_context(SECURE); 67510b65ecfSSoby Mathew SMC_RET0(&tsp_ctx->cpu_ctx); 676239b04faSSoby Mathew 677239b04faSSoby Mathew /* 678916a2c1eSAchin Gupta * This is a request from the secure payload for more arguments 679916a2c1eSAchin Gupta * for an ongoing arithmetic operation requested by the 680916a2c1eSAchin Gupta * non-secure world. Simply return the arguments from the non- 681916a2c1eSAchin Gupta * secure client in the original call. 682916a2c1eSAchin Gupta */ 683916a2c1eSAchin Gupta case TSP_GET_ARGS: 684916a2c1eSAchin Gupta if (ns) 685916a2c1eSAchin Gupta SMC_RET1(handle, SMC_UNK); 686916a2c1eSAchin Gupta 687239b04faSSoby Mathew get_tsp_args(tsp_ctx, x1, x2); 688239b04faSSoby Mathew SMC_RET2(handle, x1, x2); 689916a2c1eSAchin Gupta 69052538b9bSJeenu Viswambharan case TOS_CALL_COUNT: 69152538b9bSJeenu Viswambharan /* 69252538b9bSJeenu Viswambharan * Return the number of service function IDs implemented to 69352538b9bSJeenu Viswambharan * provide service to non-secure 69452538b9bSJeenu Viswambharan */ 69552538b9bSJeenu Viswambharan SMC_RET1(handle, TSP_NUM_FID); 69652538b9bSJeenu Viswambharan 69752538b9bSJeenu Viswambharan case TOS_UID: 69852538b9bSJeenu Viswambharan /* Return TSP UID to the caller */ 69952538b9bSJeenu Viswambharan SMC_UUID_RET(handle, tsp_uuid); 70052538b9bSJeenu Viswambharan 70152538b9bSJeenu Viswambharan case TOS_CALL_VERSION: 70252538b9bSJeenu Viswambharan /* Return the version of current implementation */ 70352538b9bSJeenu Viswambharan SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR); 70452538b9bSJeenu Viswambharan 705375f538aSAchin Gupta default: 706607084eeSAchin Gupta break; 707375f538aSAchin Gupta } 708375f538aSAchin Gupta 709607084eeSAchin Gupta SMC_RET1(handle, SMC_UNK); 710375f538aSAchin Gupta } 711375f538aSAchin Gupta 712239b04faSSoby Mathew /* Define a SPD runtime service descriptor for fast SMC calls */ 713375f538aSAchin Gupta DECLARE_RT_SVC( 714239b04faSSoby Mathew tspd_fast, 715375f538aSAchin Gupta 716375f538aSAchin Gupta OEN_TOS_START, 717375f538aSAchin Gupta OEN_TOS_END, 718375f538aSAchin Gupta SMC_TYPE_FAST, 719375f538aSAchin Gupta tspd_setup, 720375f538aSAchin Gupta tspd_smc_handler 721375f538aSAchin Gupta ); 722239b04faSSoby Mathew 72316292f54SDavid Cunado /* Define a SPD runtime service descriptor for Yielding SMC Calls */ 724239b04faSSoby Mathew DECLARE_RT_SVC( 725239b04faSSoby Mathew tspd_std, 726239b04faSSoby Mathew 727239b04faSSoby Mathew OEN_TOS_START, 728239b04faSSoby Mathew OEN_TOS_END, 72916292f54SDavid Cunado SMC_TYPE_YIELD, 730239b04faSSoby Mathew NULL, 731239b04faSSoby Mathew tspd_smc_handler 732239b04faSSoby Mathew ); 733