1375f538aSAchin Gupta /* 216292f54SDavid Cunado * Copyright (c) 2013-2017, 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> 20*2a4b4b71SIsla Mitchell #include <bl_common.h> 21375f538aSAchin Gupta #include <context_mgmt.h> 22b44a4435SAchin Gupta #include <debug.h> 23b44a4435SAchin Gupta #include <errno.h> 24b44a4435SAchin Gupta #include <platform.h> 25375f538aSAchin Gupta #include <runtime_svc.h> 2697043ac9SDan Handley #include <stddef.h> 27f4f1ae77SSoby Mathew #include <string.h> 28375f538aSAchin Gupta #include <tsp.h> 2952538b9bSJeenu Viswambharan #include <uuid.h> 3035e98e55SDan Handley #include "tspd_private.h" 31375f538aSAchin Gupta 32375f538aSAchin Gupta /******************************************************************************* 33399fb08fSAndrew Thoelke * Address of the entrypoint vector table in the Secure Payload. It is 34399fb08fSAndrew Thoelke * initialised once on the primary core after a cold boot. 35375f538aSAchin Gupta ******************************************************************************/ 36399fb08fSAndrew Thoelke tsp_vectors_t *tsp_vectors; 37375f538aSAchin Gupta 38375f538aSAchin Gupta /******************************************************************************* 39375f538aSAchin Gupta * Array to keep track of per-cpu Secure Payload state 40375f538aSAchin Gupta ******************************************************************************/ 41fb037bfbSDan Handley tsp_context_t tspd_sp_context[TSPD_CORE_COUNT]; 42375f538aSAchin Gupta 437f366605SJeenu Viswambharan 4452538b9bSJeenu Viswambharan /* TSP UID */ 4552538b9bSJeenu Viswambharan DEFINE_SVC_UUID(tsp_uuid, 4652538b9bSJeenu Viswambharan 0x5b3056a0, 0x3291, 0x427b, 0x98, 0x11, 4752538b9bSJeenu Viswambharan 0x71, 0x68, 0xca, 0x50, 0xf3, 0xfa); 4852538b9bSJeenu Viswambharan 496871c5d3SVikram Kanigiri int32_t tspd_init(void); 507f366605SJeenu Viswambharan 51404dba53SSoby Mathew /* 52404dba53SSoby Mathew * This helper function handles Secure EL1 preemption. The preemption could be 53404dba53SSoby Mathew * due Non Secure interrupts or EL3 interrupts. In both the cases we context 54404dba53SSoby Mathew * switch to the normal world and in case of EL3 interrupts, it will again be 55404dba53SSoby Mathew * routed to EL3 which will get handled at the exception vectors. 56404dba53SSoby Mathew */ 57f4f1ae77SSoby Mathew uint64_t tspd_handle_sp_preemption(void *handle) 58f4f1ae77SSoby Mathew { 59f4f1ae77SSoby Mathew cpu_context_t *ns_cpu_context; 60404dba53SSoby Mathew 61f4f1ae77SSoby Mathew assert(handle == cm_get_context(SECURE)); 62f4f1ae77SSoby Mathew cm_el1_sysregs_context_save(SECURE); 63f4f1ae77SSoby Mathew /* Get a reference to the non-secure context */ 64f4f1ae77SSoby Mathew ns_cpu_context = cm_get_context(NON_SECURE); 65f4f1ae77SSoby Mathew assert(ns_cpu_context); 66f4f1ae77SSoby Mathew 67f4f1ae77SSoby Mathew /* 6863b8440fSSoby Mathew * To allow Secure EL1 interrupt handler to re-enter TSP while TSP 6963b8440fSSoby Mathew * is preempted, the secure system register context which will get 7063b8440fSSoby Mathew * overwritten must be additionally saved. This is currently done 7163b8440fSSoby Mathew * by the TSPD S-EL1 interrupt handler. 7263b8440fSSoby Mathew */ 7363b8440fSSoby Mathew 7463b8440fSSoby Mathew /* 7563b8440fSSoby Mathew * Restore non-secure state. 76f4f1ae77SSoby Mathew */ 77f4f1ae77SSoby Mathew cm_el1_sysregs_context_restore(NON_SECURE); 78f4f1ae77SSoby Mathew cm_set_next_eret_context(NON_SECURE); 79f4f1ae77SSoby Mathew 80404dba53SSoby Mathew /* 8116292f54SDavid Cunado * The TSP was preempted during execution of a Yielding SMC Call. 8263b8440fSSoby Mathew * Return back to the normal world with SMC_PREEMPTED as error 8363b8440fSSoby Mathew * code in x0. 84404dba53SSoby Mathew */ 85f4f1ae77SSoby Mathew SMC_RET1(ns_cpu_context, SMC_PREEMPTED); 86f4f1ae77SSoby Mathew } 87404dba53SSoby Mathew 88b44a4435SAchin Gupta /******************************************************************************* 89b44a4435SAchin Gupta * This function is the handler registered for S-EL1 interrupts by the TSPD. It 90b44a4435SAchin Gupta * validates the interrupt and upon success arranges entry into the TSP at 9102446137SSoby Mathew * 'tsp_sel1_intr_entry()' for handling the interrupt. 92b44a4435SAchin Gupta ******************************************************************************/ 93b44a4435SAchin Gupta static uint64_t tspd_sel1_interrupt_handler(uint32_t id, 94b44a4435SAchin Gupta uint32_t flags, 95b44a4435SAchin Gupta void *handle, 96b44a4435SAchin Gupta void *cookie) 97b44a4435SAchin Gupta { 98b44a4435SAchin Gupta uint32_t linear_id; 99b44a4435SAchin Gupta tsp_context_t *tsp_ctx; 100b44a4435SAchin Gupta 101b44a4435SAchin Gupta /* Check the security state when the exception was generated */ 102b44a4435SAchin Gupta assert(get_interrupt_src_ss(flags) == NON_SECURE); 103b44a4435SAchin Gupta 104b44a4435SAchin Gupta /* Sanity check the pointer to this cpu's context */ 10508ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 106b44a4435SAchin Gupta 107b44a4435SAchin Gupta /* Save the non-secure context before entering the TSP */ 108b44a4435SAchin Gupta cm_el1_sysregs_context_save(NON_SECURE); 109b44a4435SAchin Gupta 110b44a4435SAchin Gupta /* Get a reference to this cpu's TSP context */ 111fd650ff6SSoby Mathew linear_id = plat_my_core_pos(); 112b44a4435SAchin Gupta tsp_ctx = &tspd_sp_context[linear_id]; 11308ab89d3SAndrew Thoelke assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE)); 114b44a4435SAchin Gupta 115b44a4435SAchin Gupta /* 116b44a4435SAchin Gupta * Determine if the TSP was previously preempted. Its last known 117b44a4435SAchin Gupta * context has to be preserved in this case. 118b44a4435SAchin Gupta * The TSP should return control to the TSPD after handling this 11902446137SSoby Mathew * S-EL1 interrupt. Preserve essential EL3 context to allow entry into 12002446137SSoby Mathew * the TSP at the S-EL1 interrupt entry point using the 'cpu_context' 12102446137SSoby Mathew * structure. There is no need to save the secure system register 12202446137SSoby Mathew * context since the TSP is supposed to preserve it during S-EL1 12302446137SSoby Mathew * interrupt handling. 124b44a4435SAchin Gupta */ 12516292f54SDavid Cunado if (get_yield_smc_active_flag(tsp_ctx->state)) { 126b44a4435SAchin Gupta tsp_ctx->saved_spsr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx, 127b44a4435SAchin Gupta CTX_SPSR_EL3); 128b44a4435SAchin Gupta tsp_ctx->saved_elr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx, 129b44a4435SAchin Gupta CTX_ELR_EL3); 13002446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 131f4f1ae77SSoby Mathew /*Need to save the previously interrupted secure context */ 132f4f1ae77SSoby Mathew memcpy(&tsp_ctx->sp_ctx, &tsp_ctx->cpu_ctx, TSPD_SP_CTX_SIZE); 133f4f1ae77SSoby Mathew #endif 134b44a4435SAchin Gupta } 135b44a4435SAchin Gupta 136b44a4435SAchin Gupta cm_el1_sysregs_context_restore(SECURE); 13702446137SSoby Mathew cm_set_elr_spsr_el3(SECURE, (uint64_t) &tsp_vectors->sel1_intr_entry, 138167a9357SAndrew Thoelke SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS)); 139f4f1ae77SSoby Mathew 140b44a4435SAchin Gupta cm_set_next_eret_context(SECURE); 141b44a4435SAchin Gupta 142b44a4435SAchin Gupta /* 14302446137SSoby Mathew * Tell the TSP that it has to handle a S-EL1 interrupt synchronously. 14402446137SSoby Mathew * Also the instruction in normal world where the interrupt was 14502446137SSoby Mathew * generated is passed for debugging purposes. It is safe to retrieve 14602446137SSoby Mathew * this address from ELR_EL3 as the secure context will not take effect 14702446137SSoby Mathew * until el3_exit(). 148b44a4435SAchin Gupta */ 14902446137SSoby Mathew SMC_RET2(&tsp_ctx->cpu_ctx, TSP_HANDLE_SEL1_INTR_AND_RETURN, read_elr_el3()); 150b44a4435SAchin Gupta } 1517f366605SJeenu Viswambharan 15202446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 153f4f1ae77SSoby Mathew /******************************************************************************* 15402446137SSoby Mathew * This function is the handler registered for Non secure interrupts by the 15502446137SSoby Mathew * TSPD. It validates the interrupt and upon success arranges entry into the 15602446137SSoby Mathew * normal world for handling the interrupt. 157f4f1ae77SSoby Mathew ******************************************************************************/ 158f4f1ae77SSoby Mathew static uint64_t tspd_ns_interrupt_handler(uint32_t id, 159f4f1ae77SSoby Mathew uint32_t flags, 160f4f1ae77SSoby Mathew void *handle, 161f4f1ae77SSoby Mathew void *cookie) 162f4f1ae77SSoby Mathew { 163f4f1ae77SSoby Mathew /* Check the security state when the exception was generated */ 164f4f1ae77SSoby Mathew assert(get_interrupt_src_ss(flags) == SECURE); 165f4f1ae77SSoby Mathew 166f4f1ae77SSoby Mathew /* 167f4f1ae77SSoby Mathew * Disable the routing of NS interrupts from secure world to EL3 while 168f4f1ae77SSoby Mathew * interrupted on this core. 169f4f1ae77SSoby Mathew */ 170f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 171f4f1ae77SSoby Mathew 172f4f1ae77SSoby Mathew return tspd_handle_sp_preemption(handle); 173f4f1ae77SSoby Mathew } 174f4f1ae77SSoby Mathew #endif 175f4f1ae77SSoby Mathew 176375f538aSAchin Gupta /******************************************************************************* 177375f538aSAchin Gupta * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type 178375f538aSAchin Gupta * (aarch32/aarch64) if not already known and initialises the context for entry 179375f538aSAchin Gupta * into the SP for its initialisation. 180375f538aSAchin Gupta ******************************************************************************/ 181375f538aSAchin Gupta int32_t tspd_setup(void) 182375f538aSAchin Gupta { 18350e27dadSVikram Kanigiri entry_point_info_t *tsp_ep_info; 184375f538aSAchin Gupta uint32_t linear_id; 185375f538aSAchin Gupta 186fd650ff6SSoby Mathew linear_id = plat_my_core_pos(); 187375f538aSAchin Gupta 188375f538aSAchin Gupta /* 189375f538aSAchin Gupta * Get information about the Secure Payload (BL32) image. Its 190375f538aSAchin Gupta * absence is a critical failure. TODO: Add support to 191375f538aSAchin Gupta * conditionally include the SPD service 192375f538aSAchin Gupta */ 19350e27dadSVikram Kanigiri tsp_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 19450e27dadSVikram Kanigiri if (!tsp_ep_info) { 19550e27dadSVikram Kanigiri WARN("No TSP provided by BL2 boot loader, Booting device" 19650e27dadSVikram Kanigiri " without TSP initialization. SMC`s destined for TSP" 19750e27dadSVikram Kanigiri " will return SMC_UNK\n"); 19850e27dadSVikram Kanigiri return 1; 19950e27dadSVikram Kanigiri } 200375f538aSAchin Gupta 201375f538aSAchin Gupta /* 2027f366605SJeenu Viswambharan * If there's no valid entry point for SP, we return a non-zero value 2037f366605SJeenu Viswambharan * signalling failure initializing the service. We bail out without 2047f366605SJeenu Viswambharan * registering any handlers 2057f366605SJeenu Viswambharan */ 20650e27dadSVikram Kanigiri if (!tsp_ep_info->pc) 2077f366605SJeenu Viswambharan return 1; 2087f366605SJeenu Viswambharan 2097f366605SJeenu Viswambharan /* 2101645d3eeSSandrine Bailleux * We could inspect the SP image and determine its execution 211375f538aSAchin Gupta * state i.e whether AArch32 or AArch64. Assuming it's AArch64 212375f538aSAchin Gupta * for the time being. 213375f538aSAchin Gupta */ 21450e27dadSVikram Kanigiri tspd_init_tsp_ep_state(tsp_ep_info, 215375f538aSAchin Gupta TSP_AARCH64, 21650e27dadSVikram Kanigiri tsp_ep_info->pc, 217375f538aSAchin Gupta &tspd_sp_context[linear_id]); 218375f538aSAchin Gupta 219faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 220faaa2e76SVikram Kanigiri bl31_set_next_image_type(SECURE); 221faaa2e76SVikram Kanigiri #else 2227f366605SJeenu Viswambharan /* 2237f366605SJeenu Viswambharan * All TSPD initialization done. Now register our init function with 2247f366605SJeenu Viswambharan * BL31 for deferred invocation 2257f366605SJeenu Viswambharan */ 2267f366605SJeenu Viswambharan bl31_register_bl32_init(&tspd_init); 227faaa2e76SVikram Kanigiri #endif 22850e27dadSVikram Kanigiri return 0; 229375f538aSAchin Gupta } 230375f538aSAchin Gupta 231375f538aSAchin Gupta /******************************************************************************* 232375f538aSAchin Gupta * This function passes control to the Secure Payload image (BL32) for the first 233375f538aSAchin Gupta * time on the primary cpu after a cold boot. It assumes that a valid secure 234375f538aSAchin Gupta * context has already been created by tspd_setup() which can be directly used. 235375f538aSAchin Gupta * It also assumes that a valid non-secure context has been initialised by PSCI 236375f538aSAchin Gupta * so it does not need to save and restore any non-secure state. This function 237375f538aSAchin Gupta * performs a synchronous entry into the Secure payload. The SP passes control 2386871c5d3SVikram Kanigiri * back to this routine through a SMC. 239375f538aSAchin Gupta ******************************************************************************/ 2406871c5d3SVikram Kanigiri int32_t tspd_init(void) 241375f538aSAchin Gupta { 242fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 243fb037bfbSDan Handley tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 24450e27dadSVikram Kanigiri entry_point_info_t *tsp_entry_point; 245faaa2e76SVikram Kanigiri uint64_t rc; 24650e27dadSVikram Kanigiri 24750e27dadSVikram Kanigiri /* 24850e27dadSVikram Kanigiri * Get information about the Secure Payload (BL32) image. Its 24950e27dadSVikram Kanigiri * absence is a critical failure. 25050e27dadSVikram Kanigiri */ 25150e27dadSVikram Kanigiri tsp_entry_point = bl31_plat_get_next_image_ep_info(SECURE); 25250e27dadSVikram Kanigiri assert(tsp_entry_point); 25350e27dadSVikram Kanigiri 254fd650ff6SSoby Mathew cm_init_my_context(tsp_entry_point); 255375f538aSAchin Gupta 256375f538aSAchin Gupta /* 257faaa2e76SVikram Kanigiri * Arrange for an entry into the test secure payload. It will be 258faaa2e76SVikram Kanigiri * returned via TSP_ENTRY_DONE case 259607084eeSAchin Gupta */ 260375f538aSAchin Gupta rc = tspd_synchronous_sp_entry(tsp_ctx); 261375f538aSAchin Gupta assert(rc != 0); 262b44a4435SAchin Gupta 263375f538aSAchin Gupta return rc; 264375f538aSAchin Gupta } 265375f538aSAchin Gupta 2667f366605SJeenu Viswambharan 267375f538aSAchin Gupta /******************************************************************************* 268375f538aSAchin Gupta * This function is responsible for handling all SMCs in the Trusted OS/App 269375f538aSAchin Gupta * range from the non-secure state as defined in the SMC Calling Convention 270375f538aSAchin Gupta * Document. It is also responsible for communicating with the Secure payload 271375f538aSAchin Gupta * to delegate work and return results back to the non-secure state. Lastly it 272375f538aSAchin Gupta * will also return any information that the secure payload needs to do the 273375f538aSAchin Gupta * work assigned to it. 274375f538aSAchin Gupta ******************************************************************************/ 275375f538aSAchin Gupta uint64_t tspd_smc_handler(uint32_t smc_fid, 276375f538aSAchin Gupta uint64_t x1, 277375f538aSAchin Gupta uint64_t x2, 278375f538aSAchin Gupta uint64_t x3, 279375f538aSAchin Gupta uint64_t x4, 280375f538aSAchin Gupta void *cookie, 281375f538aSAchin Gupta void *handle, 282375f538aSAchin Gupta uint64_t flags) 283375f538aSAchin Gupta { 284fb037bfbSDan Handley cpu_context_t *ns_cpu_context; 285fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(), ns; 286fb037bfbSDan Handley tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 287faaa2e76SVikram Kanigiri uint64_t rc; 288faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 289faaa2e76SVikram Kanigiri entry_point_info_t *next_image_info; 290faaa2e76SVikram Kanigiri #endif 291375f538aSAchin Gupta 292375f538aSAchin Gupta /* Determine which security state this SMC originated from */ 293375f538aSAchin Gupta ns = is_caller_non_secure(flags); 294375f538aSAchin Gupta 295375f538aSAchin Gupta switch (smc_fid) { 296375f538aSAchin Gupta 297375f538aSAchin Gupta /* 298239b04faSSoby Mathew * This function ID is used by TSP to indicate that it was 299239b04faSSoby Mathew * preempted by a normal world IRQ. 300239b04faSSoby Mathew * 301239b04faSSoby Mathew */ 302239b04faSSoby Mathew case TSP_PREEMPTED: 303239b04faSSoby Mathew if (ns) 304239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 305239b04faSSoby Mathew 306f4f1ae77SSoby Mathew return tspd_handle_sp_preemption(handle); 307239b04faSSoby Mathew 308239b04faSSoby Mathew /* 309b44a4435SAchin Gupta * This function ID is used only by the TSP to indicate that it has 31063b8440fSSoby Mathew * finished handling a S-EL1 interrupt or was preempted by a higher 31163b8440fSSoby Mathew * priority pending EL3 interrupt. Execution should resume 312b44a4435SAchin Gupta * in the normal world. 313b44a4435SAchin Gupta */ 31402446137SSoby Mathew case TSP_HANDLED_S_EL1_INTR: 315b44a4435SAchin Gupta if (ns) 316b44a4435SAchin Gupta SMC_RET1(handle, SMC_UNK); 317b44a4435SAchin Gupta 31808ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 319b44a4435SAchin Gupta 320b44a4435SAchin Gupta /* 321b44a4435SAchin Gupta * Restore the relevant EL3 state which saved to service 322b44a4435SAchin Gupta * this SMC. 323b44a4435SAchin Gupta */ 32416292f54SDavid Cunado if (get_yield_smc_active_flag(tsp_ctx->state)) { 325b44a4435SAchin Gupta SMC_SET_EL3(&tsp_ctx->cpu_ctx, 326b44a4435SAchin Gupta CTX_SPSR_EL3, 327b44a4435SAchin Gupta tsp_ctx->saved_spsr_el3); 328b44a4435SAchin Gupta SMC_SET_EL3(&tsp_ctx->cpu_ctx, 329b44a4435SAchin Gupta CTX_ELR_EL3, 330b44a4435SAchin Gupta tsp_ctx->saved_elr_el3); 33102446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 332f4f1ae77SSoby Mathew /* 333f4f1ae77SSoby Mathew * Need to restore the previously interrupted 334f4f1ae77SSoby Mathew * secure context. 335f4f1ae77SSoby Mathew */ 336f4f1ae77SSoby Mathew memcpy(&tsp_ctx->cpu_ctx, &tsp_ctx->sp_ctx, 337f4f1ae77SSoby Mathew TSPD_SP_CTX_SIZE); 338f4f1ae77SSoby Mathew #endif 339b44a4435SAchin Gupta } 340b44a4435SAchin Gupta 341b44a4435SAchin Gupta /* Get a reference to the non-secure context */ 34208ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 343b44a4435SAchin Gupta assert(ns_cpu_context); 344b44a4435SAchin Gupta 345b44a4435SAchin Gupta /* 346b44a4435SAchin Gupta * Restore non-secure state. There is no need to save the 347b44a4435SAchin Gupta * secure system register context since the TSP was supposed 348b44a4435SAchin Gupta * to preserve it during S-EL1 interrupt handling. 349b44a4435SAchin Gupta */ 350b44a4435SAchin Gupta cm_el1_sysregs_context_restore(NON_SECURE); 351b44a4435SAchin Gupta cm_set_next_eret_context(NON_SECURE); 352b44a4435SAchin Gupta 353b44a4435SAchin Gupta SMC_RET0((uint64_t) ns_cpu_context); 354b44a4435SAchin Gupta 355b44a4435SAchin Gupta /* 356375f538aSAchin Gupta * This function ID is used only by the SP to indicate it has 357375f538aSAchin Gupta * finished initialising itself after a cold boot 358375f538aSAchin Gupta */ 359375f538aSAchin Gupta case TSP_ENTRY_DONE: 360375f538aSAchin Gupta if (ns) 361375f538aSAchin Gupta SMC_RET1(handle, SMC_UNK); 362375f538aSAchin Gupta 363375f538aSAchin Gupta /* 364375f538aSAchin Gupta * Stash the SP entry points information. This is done 365375f538aSAchin Gupta * only once on the primary cpu 366375f538aSAchin Gupta */ 367399fb08fSAndrew Thoelke assert(tsp_vectors == NULL); 368399fb08fSAndrew Thoelke tsp_vectors = (tsp_vectors_t *) x1; 369375f538aSAchin Gupta 370faaa2e76SVikram Kanigiri if (tsp_vectors) { 371faaa2e76SVikram Kanigiri set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON); 372faaa2e76SVikram Kanigiri 373faaa2e76SVikram Kanigiri /* 374faaa2e76SVikram Kanigiri * TSP has been successfully initialized. Register power 375faaa2e76SVikram Kanigiri * managemnt hooks with PSCI 376faaa2e76SVikram Kanigiri */ 377faaa2e76SVikram Kanigiri psci_register_spd_pm_hook(&tspd_pm); 378faaa2e76SVikram Kanigiri 379faaa2e76SVikram Kanigiri /* 380faaa2e76SVikram Kanigiri * Register an interrupt handler for S-EL1 interrupts 381faaa2e76SVikram Kanigiri * when generated during code executing in the 382faaa2e76SVikram Kanigiri * non-secure state. 383faaa2e76SVikram Kanigiri */ 384faaa2e76SVikram Kanigiri flags = 0; 385faaa2e76SVikram Kanigiri set_interrupt_rm_flag(flags, NON_SECURE); 386faaa2e76SVikram Kanigiri rc = register_interrupt_type_handler(INTR_TYPE_S_EL1, 387faaa2e76SVikram Kanigiri tspd_sel1_interrupt_handler, 388faaa2e76SVikram Kanigiri flags); 389faaa2e76SVikram Kanigiri if (rc) 390faaa2e76SVikram Kanigiri panic(); 391f4f1ae77SSoby Mathew 39202446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 393f4f1ae77SSoby Mathew /* 394f4f1ae77SSoby Mathew * Register an interrupt handler for NS interrupts when 395f4f1ae77SSoby Mathew * generated during code executing in secure state are 396f4f1ae77SSoby Mathew * routed to EL3. 397f4f1ae77SSoby Mathew */ 398f4f1ae77SSoby Mathew flags = 0; 399f4f1ae77SSoby Mathew set_interrupt_rm_flag(flags, SECURE); 400f4f1ae77SSoby Mathew 401f4f1ae77SSoby Mathew rc = register_interrupt_type_handler(INTR_TYPE_NS, 402f4f1ae77SSoby Mathew tspd_ns_interrupt_handler, 403f4f1ae77SSoby Mathew flags); 404f4f1ae77SSoby Mathew if (rc) 405f4f1ae77SSoby Mathew panic(); 406f4f1ae77SSoby Mathew 407f4f1ae77SSoby Mathew /* 408404dba53SSoby Mathew * Disable the NS interrupt locally. 409f4f1ae77SSoby Mathew */ 410f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 411f4f1ae77SSoby Mathew #endif 412faaa2e76SVikram Kanigiri } 413faaa2e76SVikram Kanigiri 414faaa2e76SVikram Kanigiri 415faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 416faaa2e76SVikram Kanigiri /* Save the Secure EL1 system register context */ 417faaa2e76SVikram Kanigiri assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx); 418faaa2e76SVikram Kanigiri cm_el1_sysregs_context_save(SECURE); 419faaa2e76SVikram Kanigiri 420faaa2e76SVikram Kanigiri /* Program EL3 registers to enable entry into the next EL */ 421faaa2e76SVikram Kanigiri next_image_info = bl31_plat_get_next_image_ep_info(NON_SECURE); 422faaa2e76SVikram Kanigiri assert(next_image_info); 423faaa2e76SVikram Kanigiri assert(NON_SECURE == 424faaa2e76SVikram Kanigiri GET_SECURITY_STATE(next_image_info->h.attr)); 425faaa2e76SVikram Kanigiri 426fd650ff6SSoby Mathew cm_init_my_context(next_image_info); 427faaa2e76SVikram Kanigiri cm_prepare_el3_exit(NON_SECURE); 428faaa2e76SVikram Kanigiri SMC_RET0(cm_get_context(NON_SECURE)); 429faaa2e76SVikram Kanigiri #else 430375f538aSAchin Gupta /* 431375f538aSAchin Gupta * SP reports completion. The SPD must have initiated 432375f538aSAchin Gupta * the original request through a synchronous entry 433375f538aSAchin Gupta * into the SP. Jump back to the original C runtime 434375f538aSAchin Gupta * context. 435375f538aSAchin Gupta */ 436916a2c1eSAchin Gupta tspd_synchronous_sp_exit(tsp_ctx, x1); 437faaa2e76SVikram Kanigiri #endif 4383df6012aSDouglas Raillard /* 4393df6012aSDouglas Raillard * This function ID is used only by the SP to indicate it has finished 44016292f54SDavid Cunado * aborting a preempted Yielding SMC Call. 4413df6012aSDouglas Raillard */ 4423df6012aSDouglas Raillard case TSP_ABORT_DONE: 443375f538aSAchin Gupta 444607084eeSAchin Gupta /* 4451645d3eeSSandrine Bailleux * These function IDs are used only by the SP to indicate it has 446607084eeSAchin Gupta * finished: 447607084eeSAchin Gupta * 1. turning itself on in response to an earlier psci 448607084eeSAchin Gupta * cpu_on request 449607084eeSAchin Gupta * 2. resuming itself after an earlier psci cpu_suspend 450607084eeSAchin Gupta * request. 451607084eeSAchin Gupta */ 452607084eeSAchin Gupta case TSP_ON_DONE: 453607084eeSAchin Gupta case TSP_RESUME_DONE: 454607084eeSAchin Gupta 455607084eeSAchin Gupta /* 4561645d3eeSSandrine Bailleux * These function IDs are used only by the SP to indicate it has 457607084eeSAchin Gupta * finished: 458607084eeSAchin Gupta * 1. suspending itself after an earlier psci cpu_suspend 459607084eeSAchin Gupta * request. 460607084eeSAchin Gupta * 2. turning itself off in response to an earlier psci 461607084eeSAchin Gupta * cpu_off request. 462607084eeSAchin Gupta */ 463607084eeSAchin Gupta case TSP_OFF_DONE: 464607084eeSAchin Gupta case TSP_SUSPEND_DONE: 465d5f13093SJuan Castillo case TSP_SYSTEM_OFF_DONE: 466d5f13093SJuan Castillo case TSP_SYSTEM_RESET_DONE: 467607084eeSAchin Gupta if (ns) 468607084eeSAchin Gupta SMC_RET1(handle, SMC_UNK); 469607084eeSAchin Gupta 470607084eeSAchin Gupta /* 471607084eeSAchin Gupta * SP reports completion. The SPD must have initiated the 472607084eeSAchin Gupta * original request through a synchronous entry into the SP. 473607084eeSAchin Gupta * Jump back to the original C runtime context, and pass x1 as 474607084eeSAchin Gupta * return value to the caller 475607084eeSAchin Gupta */ 476916a2c1eSAchin Gupta tspd_synchronous_sp_exit(tsp_ctx, x1); 477607084eeSAchin Gupta 478916a2c1eSAchin Gupta /* 479916a2c1eSAchin Gupta * Request from non-secure client to perform an 480916a2c1eSAchin Gupta * arithmetic operation or response from secure 481916a2c1eSAchin Gupta * payload to an earlier request. 482916a2c1eSAchin Gupta */ 483239b04faSSoby Mathew case TSP_FAST_FID(TSP_ADD): 484239b04faSSoby Mathew case TSP_FAST_FID(TSP_SUB): 485239b04faSSoby Mathew case TSP_FAST_FID(TSP_MUL): 486239b04faSSoby Mathew case TSP_FAST_FID(TSP_DIV): 487239b04faSSoby Mathew 48816292f54SDavid Cunado case TSP_YIELD_FID(TSP_ADD): 48916292f54SDavid Cunado case TSP_YIELD_FID(TSP_SUB): 49016292f54SDavid Cunado case TSP_YIELD_FID(TSP_MUL): 49116292f54SDavid Cunado case TSP_YIELD_FID(TSP_DIV): 492916a2c1eSAchin Gupta if (ns) { 493916a2c1eSAchin Gupta /* 494916a2c1eSAchin Gupta * This is a fresh request from the non-secure client. 495916a2c1eSAchin Gupta * The parameters are in x1 and x2. Figure out which 496916a2c1eSAchin Gupta * registers need to be preserved, save the non-secure 497916a2c1eSAchin Gupta * state and send the request to the secure payload. 498916a2c1eSAchin Gupta */ 49908ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 500239b04faSSoby Mathew 501239b04faSSoby Mathew /* Check if we are already preempted */ 50216292f54SDavid Cunado if (get_yield_smc_active_flag(tsp_ctx->state)) 503239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 504239b04faSSoby Mathew 505916a2c1eSAchin Gupta cm_el1_sysregs_context_save(NON_SECURE); 506916a2c1eSAchin Gupta 507916a2c1eSAchin Gupta /* Save x1 and x2 for use by TSP_GET_ARGS call below */ 508239b04faSSoby Mathew store_tsp_args(tsp_ctx, x1, x2); 509916a2c1eSAchin Gupta 510916a2c1eSAchin Gupta /* 511916a2c1eSAchin Gupta * We are done stashing the non-secure context. Ask the 512916a2c1eSAchin Gupta * secure payload to do the work now. 513916a2c1eSAchin Gupta */ 514916a2c1eSAchin Gupta 515916a2c1eSAchin Gupta /* 516916a2c1eSAchin Gupta * Verify if there is a valid context to use, copy the 517916a2c1eSAchin Gupta * operation type and parameters to the secure context 518916a2c1eSAchin Gupta * and jump to the fast smc entry point in the secure 519916a2c1eSAchin Gupta * payload. Entry into S-EL1 will take place upon exit 520916a2c1eSAchin Gupta * from this function. 521916a2c1eSAchin Gupta */ 52208ab89d3SAndrew Thoelke assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE)); 523239b04faSSoby Mathew 524239b04faSSoby Mathew /* Set appropriate entry for SMC. 525239b04faSSoby Mathew * We expect the TSP to manage the PSTATE.I and PSTATE.F 526239b04faSSoby Mathew * flags as appropriate. 527239b04faSSoby Mathew */ 528239b04faSSoby Mathew if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_FAST) { 529239b04faSSoby Mathew cm_set_elr_el3(SECURE, (uint64_t) 530399fb08fSAndrew Thoelke &tsp_vectors->fast_smc_entry); 531239b04faSSoby Mathew } else { 53216292f54SDavid Cunado set_yield_smc_active_flag(tsp_ctx->state); 533239b04faSSoby Mathew cm_set_elr_el3(SECURE, (uint64_t) 53416292f54SDavid Cunado &tsp_vectors->yield_smc_entry); 53502446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 536f4f1ae77SSoby Mathew /* 537f4f1ae77SSoby Mathew * Enable the routing of NS interrupts to EL3 53816292f54SDavid Cunado * during processing of a Yielding SMC Call on 53916292f54SDavid Cunado * this core. 540f4f1ae77SSoby Mathew */ 541f4f1ae77SSoby Mathew enable_intr_rm_local(INTR_TYPE_NS, SECURE); 542f4f1ae77SSoby Mathew #endif 543239b04faSSoby Mathew } 544239b04faSSoby Mathew 545916a2c1eSAchin Gupta cm_el1_sysregs_context_restore(SECURE); 546916a2c1eSAchin Gupta cm_set_next_eret_context(SECURE); 547239b04faSSoby Mathew SMC_RET3(&tsp_ctx->cpu_ctx, smc_fid, x1, x2); 548916a2c1eSAchin Gupta } else { 549916a2c1eSAchin Gupta /* 550916a2c1eSAchin Gupta * This is the result from the secure client of an 551239b04faSSoby Mathew * earlier request. The results are in x1-x3. Copy it 552916a2c1eSAchin Gupta * into the non-secure context, save the secure state 553916a2c1eSAchin Gupta * and return to the non-secure state. 554916a2c1eSAchin Gupta */ 55508ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 556916a2c1eSAchin Gupta cm_el1_sysregs_context_save(SECURE); 557916a2c1eSAchin Gupta 558916a2c1eSAchin Gupta /* Get a reference to the non-secure context */ 55908ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 560916a2c1eSAchin Gupta assert(ns_cpu_context); 561916a2c1eSAchin Gupta 562916a2c1eSAchin Gupta /* Restore non-secure state */ 563916a2c1eSAchin Gupta cm_el1_sysregs_context_restore(NON_SECURE); 564916a2c1eSAchin Gupta cm_set_next_eret_context(NON_SECURE); 56516292f54SDavid Cunado if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_YIELD) { 56616292f54SDavid Cunado clr_yield_smc_active_flag(tsp_ctx->state); 56702446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 568f4f1ae77SSoby Mathew /* 569f4f1ae77SSoby Mathew * Disable the routing of NS interrupts to EL3 57016292f54SDavid Cunado * after processing of a Yielding SMC Call on 57116292f54SDavid Cunado * this core is finished. 572f4f1ae77SSoby Mathew */ 573f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 574f4f1ae77SSoby Mathew #endif 575f4f1ae77SSoby Mathew } 576f4f1ae77SSoby Mathew 577239b04faSSoby Mathew SMC_RET3(ns_cpu_context, x1, x2, x3); 578916a2c1eSAchin Gupta } 579916a2c1eSAchin Gupta 580916a2c1eSAchin Gupta break; 5813df6012aSDouglas Raillard /* 58216292f54SDavid Cunado * Request from the non-secure world to abort a preempted Yielding SMC 58316292f54SDavid Cunado * Call. 5843df6012aSDouglas Raillard */ 5853df6012aSDouglas Raillard case TSP_FID_ABORT: 5863df6012aSDouglas Raillard /* ABORT should only be invoked by normal world */ 5873df6012aSDouglas Raillard if (!ns) { 5883df6012aSDouglas Raillard assert(0); 5893df6012aSDouglas Raillard break; 5903df6012aSDouglas Raillard } 5913df6012aSDouglas Raillard 59257a5a56cSDouglas Raillard assert(handle == cm_get_context(NON_SECURE)); 59357a5a56cSDouglas Raillard cm_el1_sysregs_context_save(NON_SECURE); 59457a5a56cSDouglas Raillard 5953df6012aSDouglas Raillard /* Abort the preempted SMC request */ 59657a5a56cSDouglas Raillard if (!tspd_abort_preempted_smc(tsp_ctx)) { 5973df6012aSDouglas Raillard /* 5983df6012aSDouglas Raillard * If there was no preempted SMC to abort, return 5993df6012aSDouglas Raillard * SMC_UNK. 60057a5a56cSDouglas Raillard * 60157a5a56cSDouglas Raillard * Restoring the NON_SECURE context is not necessary as 60257a5a56cSDouglas Raillard * the synchronous entry did not take place if the 60357a5a56cSDouglas Raillard * return code of tspd_abort_preempted_smc is zero. 6043df6012aSDouglas Raillard */ 60557a5a56cSDouglas Raillard cm_set_next_eret_context(NON_SECURE); 6063df6012aSDouglas Raillard break; 60757a5a56cSDouglas Raillard } 60857a5a56cSDouglas Raillard 60957a5a56cSDouglas Raillard cm_el1_sysregs_context_restore(NON_SECURE); 61057a5a56cSDouglas Raillard cm_set_next_eret_context(NON_SECURE); 6117a317a70SAntonio Nino Diaz SMC_RET1(handle, SMC_OK); 612916a2c1eSAchin Gupta 613916a2c1eSAchin Gupta /* 614239b04faSSoby Mathew * Request from non secure world to resume the preempted 61516292f54SDavid Cunado * Yielding SMC Call. 616239b04faSSoby Mathew */ 617239b04faSSoby Mathew case TSP_FID_RESUME: 618239b04faSSoby Mathew /* RESUME should be invoked only by normal world */ 619239b04faSSoby Mathew if (!ns) { 620239b04faSSoby Mathew assert(0); 621239b04faSSoby Mathew break; 622239b04faSSoby Mathew } 623239b04faSSoby Mathew 624239b04faSSoby Mathew /* 625239b04faSSoby Mathew * This is a resume request from the non-secure client. 626239b04faSSoby Mathew * save the non-secure state and send the request to 627239b04faSSoby Mathew * the secure payload. 628239b04faSSoby Mathew */ 62908ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 630239b04faSSoby Mathew 631239b04faSSoby Mathew /* Check if we are already preempted before resume */ 63216292f54SDavid Cunado if (!get_yield_smc_active_flag(tsp_ctx->state)) 633239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 634239b04faSSoby Mathew 635239b04faSSoby Mathew cm_el1_sysregs_context_save(NON_SECURE); 636239b04faSSoby Mathew 637239b04faSSoby Mathew /* 638239b04faSSoby Mathew * We are done stashing the non-secure context. Ask the 639239b04faSSoby Mathew * secure payload to do the work now. 640239b04faSSoby Mathew */ 64102446137SSoby Mathew #if TSP_NS_INTR_ASYNC_PREEMPT 642f4f1ae77SSoby Mathew /* 643f4f1ae77SSoby Mathew * Enable the routing of NS interrupts to EL3 during resumption 64416292f54SDavid Cunado * of a Yielding SMC Call on this core. 645f4f1ae77SSoby Mathew */ 646f4f1ae77SSoby Mathew enable_intr_rm_local(INTR_TYPE_NS, SECURE); 647f4f1ae77SSoby Mathew #endif 648f4f1ae77SSoby Mathew 649f4f1ae77SSoby Mathew 650239b04faSSoby Mathew 651239b04faSSoby Mathew /* We just need to return to the preempted point in 652239b04faSSoby Mathew * TSP and the execution will resume as normal. 653239b04faSSoby Mathew */ 654239b04faSSoby Mathew cm_el1_sysregs_context_restore(SECURE); 655239b04faSSoby Mathew cm_set_next_eret_context(SECURE); 65610b65ecfSSoby Mathew SMC_RET0(&tsp_ctx->cpu_ctx); 657239b04faSSoby Mathew 658239b04faSSoby Mathew /* 659916a2c1eSAchin Gupta * This is a request from the secure payload for more arguments 660916a2c1eSAchin Gupta * for an ongoing arithmetic operation requested by the 661916a2c1eSAchin Gupta * non-secure world. Simply return the arguments from the non- 662916a2c1eSAchin Gupta * secure client in the original call. 663916a2c1eSAchin Gupta */ 664916a2c1eSAchin Gupta case TSP_GET_ARGS: 665916a2c1eSAchin Gupta if (ns) 666916a2c1eSAchin Gupta SMC_RET1(handle, SMC_UNK); 667916a2c1eSAchin Gupta 668239b04faSSoby Mathew get_tsp_args(tsp_ctx, x1, x2); 669239b04faSSoby Mathew SMC_RET2(handle, x1, x2); 670916a2c1eSAchin Gupta 67152538b9bSJeenu Viswambharan case TOS_CALL_COUNT: 67252538b9bSJeenu Viswambharan /* 67352538b9bSJeenu Viswambharan * Return the number of service function IDs implemented to 67452538b9bSJeenu Viswambharan * provide service to non-secure 67552538b9bSJeenu Viswambharan */ 67652538b9bSJeenu Viswambharan SMC_RET1(handle, TSP_NUM_FID); 67752538b9bSJeenu Viswambharan 67852538b9bSJeenu Viswambharan case TOS_UID: 67952538b9bSJeenu Viswambharan /* Return TSP UID to the caller */ 68052538b9bSJeenu Viswambharan SMC_UUID_RET(handle, tsp_uuid); 68152538b9bSJeenu Viswambharan 68252538b9bSJeenu Viswambharan case TOS_CALL_VERSION: 68352538b9bSJeenu Viswambharan /* Return the version of current implementation */ 68452538b9bSJeenu Viswambharan SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR); 68552538b9bSJeenu Viswambharan 686375f538aSAchin Gupta default: 687607084eeSAchin Gupta break; 688375f538aSAchin Gupta } 689375f538aSAchin Gupta 690607084eeSAchin Gupta SMC_RET1(handle, SMC_UNK); 691375f538aSAchin Gupta } 692375f538aSAchin Gupta 693239b04faSSoby Mathew /* Define a SPD runtime service descriptor for fast SMC calls */ 694375f538aSAchin Gupta DECLARE_RT_SVC( 695239b04faSSoby Mathew tspd_fast, 696375f538aSAchin Gupta 697375f538aSAchin Gupta OEN_TOS_START, 698375f538aSAchin Gupta OEN_TOS_END, 699375f538aSAchin Gupta SMC_TYPE_FAST, 700375f538aSAchin Gupta tspd_setup, 701375f538aSAchin Gupta tspd_smc_handler 702375f538aSAchin Gupta ); 703239b04faSSoby Mathew 70416292f54SDavid Cunado /* Define a SPD runtime service descriptor for Yielding SMC Calls */ 705239b04faSSoby Mathew DECLARE_RT_SVC( 706239b04faSSoby Mathew tspd_std, 707239b04faSSoby Mathew 708239b04faSSoby Mathew OEN_TOS_START, 709239b04faSSoby Mathew OEN_TOS_END, 71016292f54SDavid Cunado SMC_TYPE_YIELD, 711239b04faSSoby Mathew NULL, 712239b04faSSoby Mathew tspd_smc_handler 713239b04faSSoby Mathew ); 714