1375f538aSAchin Gupta /* 2fd650ff6SSoby Mathew * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved. 3375f538aSAchin Gupta * 4375f538aSAchin Gupta * Redistribution and use in source and binary forms, with or without 5375f538aSAchin Gupta * modification, are permitted provided that the following conditions are met: 6375f538aSAchin Gupta * 7375f538aSAchin Gupta * Redistributions of source code must retain the above copyright notice, this 8375f538aSAchin Gupta * list of conditions and the following disclaimer. 9375f538aSAchin Gupta * 10375f538aSAchin Gupta * Redistributions in binary form must reproduce the above copyright notice, 11375f538aSAchin Gupta * this list of conditions and the following disclaimer in the documentation 12375f538aSAchin Gupta * and/or other materials provided with the distribution. 13375f538aSAchin Gupta * 14375f538aSAchin Gupta * Neither the name of ARM nor the names of its contributors may be used 15375f538aSAchin Gupta * to endorse or promote products derived from this software without specific 16375f538aSAchin Gupta * prior written permission. 17375f538aSAchin Gupta * 18375f538aSAchin Gupta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19375f538aSAchin Gupta * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20375f538aSAchin Gupta * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21375f538aSAchin Gupta * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22375f538aSAchin Gupta * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23375f538aSAchin Gupta * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24375f538aSAchin Gupta * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25375f538aSAchin Gupta * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26375f538aSAchin Gupta * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27375f538aSAchin Gupta * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28375f538aSAchin Gupta * POSSIBILITY OF SUCH DAMAGE. 29375f538aSAchin Gupta */ 30375f538aSAchin Gupta 31375f538aSAchin Gupta 32375f538aSAchin Gupta /******************************************************************************* 33375f538aSAchin Gupta * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a 34375f538aSAchin Gupta * plug-in component to the Secure Monitor, registered as a runtime service. The 35375f538aSAchin Gupta * SPD is expected to be a functional extension of the Secure Payload (SP) that 36375f538aSAchin Gupta * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting 37375f538aSAchin Gupta * the Trusted OS/Applications range to the dispatcher. The SPD will either 38375f538aSAchin Gupta * handle the request locally or delegate it to the Secure Payload. It is also 39375f538aSAchin Gupta * responsible for initialising and maintaining communication with the SP. 40375f538aSAchin Gupta ******************************************************************************/ 41375f538aSAchin Gupta #include <arch_helpers.h> 4297043ac9SDan Handley #include <assert.h> 4397043ac9SDan Handley #include <bl_common.h> 4497043ac9SDan Handley #include <bl31.h> 45375f538aSAchin Gupta #include <context_mgmt.h> 46b44a4435SAchin Gupta #include <debug.h> 47b44a4435SAchin Gupta #include <errno.h> 48b44a4435SAchin Gupta #include <platform.h> 49375f538aSAchin Gupta #include <runtime_svc.h> 5097043ac9SDan Handley #include <stddef.h> 51f4f1ae77SSoby Mathew #include <string.h> 52375f538aSAchin Gupta #include <tsp.h> 5352538b9bSJeenu Viswambharan #include <uuid.h> 5435e98e55SDan Handley #include "tspd_private.h" 55375f538aSAchin Gupta 56375f538aSAchin Gupta /******************************************************************************* 57399fb08fSAndrew Thoelke * Address of the entrypoint vector table in the Secure Payload. It is 58399fb08fSAndrew Thoelke * initialised once on the primary core after a cold boot. 59375f538aSAchin Gupta ******************************************************************************/ 60399fb08fSAndrew Thoelke tsp_vectors_t *tsp_vectors; 61375f538aSAchin Gupta 62375f538aSAchin Gupta /******************************************************************************* 63375f538aSAchin Gupta * Array to keep track of per-cpu Secure Payload state 64375f538aSAchin Gupta ******************************************************************************/ 65fb037bfbSDan Handley tsp_context_t tspd_sp_context[TSPD_CORE_COUNT]; 66375f538aSAchin Gupta 677f366605SJeenu Viswambharan 6852538b9bSJeenu Viswambharan /* TSP UID */ 6952538b9bSJeenu Viswambharan DEFINE_SVC_UUID(tsp_uuid, 7052538b9bSJeenu Viswambharan 0x5b3056a0, 0x3291, 0x427b, 0x98, 0x11, 7152538b9bSJeenu Viswambharan 0x71, 0x68, 0xca, 0x50, 0xf3, 0xfa); 7252538b9bSJeenu Viswambharan 736871c5d3SVikram Kanigiri int32_t tspd_init(void); 747f366605SJeenu Viswambharan 75*404dba53SSoby Mathew /* 76*404dba53SSoby Mathew * This helper function handles Secure EL1 preemption. The preemption could be 77*404dba53SSoby Mathew * due Non Secure interrupts or EL3 interrupts. In both the cases we context 78*404dba53SSoby Mathew * switch to the normal world and in case of EL3 interrupts, it will again be 79*404dba53SSoby Mathew * routed to EL3 which will get handled at the exception vectors. 80*404dba53SSoby Mathew */ 81f4f1ae77SSoby Mathew uint64_t tspd_handle_sp_preemption(void *handle) 82f4f1ae77SSoby Mathew { 83f4f1ae77SSoby Mathew cpu_context_t *ns_cpu_context; 84*404dba53SSoby Mathew 85f4f1ae77SSoby Mathew assert(handle == cm_get_context(SECURE)); 86f4f1ae77SSoby Mathew cm_el1_sysregs_context_save(SECURE); 87f4f1ae77SSoby Mathew /* Get a reference to the non-secure context */ 88f4f1ae77SSoby Mathew ns_cpu_context = cm_get_context(NON_SECURE); 89f4f1ae77SSoby Mathew assert(ns_cpu_context); 90f4f1ae77SSoby Mathew 91f4f1ae77SSoby Mathew /* 92f4f1ae77SSoby Mathew * Restore non-secure state. The secure system 93f4f1ae77SSoby Mathew * register context will be saved when required. 94f4f1ae77SSoby Mathew */ 95f4f1ae77SSoby Mathew cm_el1_sysregs_context_restore(NON_SECURE); 96f4f1ae77SSoby Mathew cm_set_next_eret_context(NON_SECURE); 97f4f1ae77SSoby Mathew 98*404dba53SSoby Mathew /* 99*404dba53SSoby Mathew * We need to restore non secure context according to 100*404dba53SSoby Mathew * the SEL1 context which got preempted and currently 101*404dba53SSoby Mathew * TSP can only be preempted when a STD SMC is ongoing. 102*404dba53SSoby Mathew * Return SMC_PREEMPTED in x0 and restore non secure 103*404dba53SSoby Mathew * context. 104*404dba53SSoby Mathew */ 105f4f1ae77SSoby Mathew SMC_RET1(ns_cpu_context, SMC_PREEMPTED); 106f4f1ae77SSoby Mathew } 107*404dba53SSoby Mathew 108b44a4435SAchin Gupta /******************************************************************************* 109b44a4435SAchin Gupta * This function is the handler registered for S-EL1 interrupts by the TSPD. It 110b44a4435SAchin Gupta * validates the interrupt and upon success arranges entry into the TSP at 111b44a4435SAchin Gupta * 'tsp_fiq_entry()' for handling the interrupt. 112b44a4435SAchin Gupta ******************************************************************************/ 113b44a4435SAchin Gupta static uint64_t tspd_sel1_interrupt_handler(uint32_t id, 114b44a4435SAchin Gupta uint32_t flags, 115b44a4435SAchin Gupta void *handle, 116b44a4435SAchin Gupta void *cookie) 117b44a4435SAchin Gupta { 118b44a4435SAchin Gupta uint32_t linear_id; 119b44a4435SAchin Gupta tsp_context_t *tsp_ctx; 120b44a4435SAchin Gupta 121b44a4435SAchin Gupta /* Check the security state when the exception was generated */ 122b44a4435SAchin Gupta assert(get_interrupt_src_ss(flags) == NON_SECURE); 123b44a4435SAchin Gupta 124b44a4435SAchin Gupta /* Sanity check the pointer to this cpu's context */ 12508ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 126b44a4435SAchin Gupta 127b44a4435SAchin Gupta /* Save the non-secure context before entering the TSP */ 128b44a4435SAchin Gupta cm_el1_sysregs_context_save(NON_SECURE); 129b44a4435SAchin Gupta 130b44a4435SAchin Gupta /* Get a reference to this cpu's TSP context */ 131fd650ff6SSoby Mathew linear_id = plat_my_core_pos(); 132b44a4435SAchin Gupta tsp_ctx = &tspd_sp_context[linear_id]; 13308ab89d3SAndrew Thoelke assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE)); 134b44a4435SAchin Gupta 135b44a4435SAchin Gupta /* 136b44a4435SAchin Gupta * Determine if the TSP was previously preempted. Its last known 137b44a4435SAchin Gupta * context has to be preserved in this case. 138b44a4435SAchin Gupta * The TSP should return control to the TSPD after handling this 139b44a4435SAchin Gupta * FIQ. Preserve essential EL3 context to allow entry into the 140b44a4435SAchin Gupta * TSP at the FIQ entry point using the 'cpu_context' structure. 141b44a4435SAchin Gupta * There is no need to save the secure system register context 142b44a4435SAchin Gupta * since the TSP is supposed to preserve it during S-EL1 interrupt 143b44a4435SAchin Gupta * handling. 144b44a4435SAchin Gupta */ 145b44a4435SAchin Gupta if (get_std_smc_active_flag(tsp_ctx->state)) { 146b44a4435SAchin Gupta tsp_ctx->saved_spsr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx, 147b44a4435SAchin Gupta CTX_SPSR_EL3); 148b44a4435SAchin Gupta tsp_ctx->saved_elr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx, 149b44a4435SAchin Gupta CTX_ELR_EL3); 150f4f1ae77SSoby Mathew #if TSPD_ROUTE_IRQ_TO_EL3 151f4f1ae77SSoby Mathew /*Need to save the previously interrupted secure context */ 152f4f1ae77SSoby Mathew memcpy(&tsp_ctx->sp_ctx, &tsp_ctx->cpu_ctx, TSPD_SP_CTX_SIZE); 153f4f1ae77SSoby Mathew #endif 154b44a4435SAchin Gupta } 155b44a4435SAchin Gupta 156b44a4435SAchin Gupta cm_el1_sysregs_context_restore(SECURE); 157167a9357SAndrew Thoelke cm_set_elr_spsr_el3(SECURE, (uint64_t) &tsp_vectors->fiq_entry, 158167a9357SAndrew Thoelke SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS)); 159f4f1ae77SSoby Mathew 160b44a4435SAchin Gupta cm_set_next_eret_context(SECURE); 161b44a4435SAchin Gupta 162b44a4435SAchin Gupta /* 163b44a4435SAchin Gupta * Tell the TSP that it has to handle an FIQ synchronously. Also the 164b44a4435SAchin Gupta * instruction in normal world where the interrupt was generated is 165b44a4435SAchin Gupta * passed for debugging purposes. It is safe to retrieve this address 166b44a4435SAchin Gupta * from ELR_EL3 as the secure context will not take effect until 167b44a4435SAchin Gupta * el3_exit(). 168b44a4435SAchin Gupta */ 169b44a4435SAchin Gupta SMC_RET2(&tsp_ctx->cpu_ctx, TSP_HANDLE_FIQ_AND_RETURN, read_elr_el3()); 170b44a4435SAchin Gupta } 1717f366605SJeenu Viswambharan 172f4f1ae77SSoby Mathew #if TSPD_ROUTE_IRQ_TO_EL3 173f4f1ae77SSoby Mathew /******************************************************************************* 174f4f1ae77SSoby Mathew * This function is the handler registered for S-EL1 interrupts by the TSPD. It 175f4f1ae77SSoby Mathew * validates the interrupt and upon success arranges entry into the TSP at 176f4f1ae77SSoby Mathew * 'tsp_fiq_entry()' for handling the interrupt. 177f4f1ae77SSoby Mathew ******************************************************************************/ 178f4f1ae77SSoby Mathew static uint64_t tspd_ns_interrupt_handler(uint32_t id, 179f4f1ae77SSoby Mathew uint32_t flags, 180f4f1ae77SSoby Mathew void *handle, 181f4f1ae77SSoby Mathew void *cookie) 182f4f1ae77SSoby Mathew { 183f4f1ae77SSoby Mathew /* Check the security state when the exception was generated */ 184f4f1ae77SSoby Mathew assert(get_interrupt_src_ss(flags) == SECURE); 185f4f1ae77SSoby Mathew 186f4f1ae77SSoby Mathew /* 187f4f1ae77SSoby Mathew * Disable the routing of NS interrupts from secure world to EL3 while 188f4f1ae77SSoby Mathew * interrupted on this core. 189f4f1ae77SSoby Mathew */ 190f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 191f4f1ae77SSoby Mathew 192f4f1ae77SSoby Mathew return tspd_handle_sp_preemption(handle); 193f4f1ae77SSoby Mathew } 194f4f1ae77SSoby Mathew #endif 195f4f1ae77SSoby Mathew 196375f538aSAchin Gupta /******************************************************************************* 197375f538aSAchin Gupta * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type 198375f538aSAchin Gupta * (aarch32/aarch64) if not already known and initialises the context for entry 199375f538aSAchin Gupta * into the SP for its initialisation. 200375f538aSAchin Gupta ******************************************************************************/ 201375f538aSAchin Gupta int32_t tspd_setup(void) 202375f538aSAchin Gupta { 20350e27dadSVikram Kanigiri entry_point_info_t *tsp_ep_info; 204375f538aSAchin Gupta uint32_t linear_id; 205375f538aSAchin Gupta 206fd650ff6SSoby Mathew linear_id = plat_my_core_pos(); 207375f538aSAchin Gupta 208375f538aSAchin Gupta /* 209375f538aSAchin Gupta * Get information about the Secure Payload (BL32) image. Its 210375f538aSAchin Gupta * absence is a critical failure. TODO: Add support to 211375f538aSAchin Gupta * conditionally include the SPD service 212375f538aSAchin Gupta */ 21350e27dadSVikram Kanigiri tsp_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 21450e27dadSVikram Kanigiri if (!tsp_ep_info) { 21550e27dadSVikram Kanigiri WARN("No TSP provided by BL2 boot loader, Booting device" 21650e27dadSVikram Kanigiri " without TSP initialization. SMC`s destined for TSP" 21750e27dadSVikram Kanigiri " will return SMC_UNK\n"); 21850e27dadSVikram Kanigiri return 1; 21950e27dadSVikram Kanigiri } 220375f538aSAchin Gupta 221375f538aSAchin Gupta /* 2227f366605SJeenu Viswambharan * If there's no valid entry point for SP, we return a non-zero value 2237f366605SJeenu Viswambharan * signalling failure initializing the service. We bail out without 2247f366605SJeenu Viswambharan * registering any handlers 2257f366605SJeenu Viswambharan */ 22650e27dadSVikram Kanigiri if (!tsp_ep_info->pc) 2277f366605SJeenu Viswambharan return 1; 2287f366605SJeenu Viswambharan 2297f366605SJeenu Viswambharan /* 230375f538aSAchin Gupta * We could inspect the SP image and determine it's execution 231375f538aSAchin Gupta * state i.e whether AArch32 or AArch64. Assuming it's AArch64 232375f538aSAchin Gupta * for the time being. 233375f538aSAchin Gupta */ 23450e27dadSVikram Kanigiri tspd_init_tsp_ep_state(tsp_ep_info, 235375f538aSAchin Gupta TSP_AARCH64, 23650e27dadSVikram Kanigiri tsp_ep_info->pc, 237375f538aSAchin Gupta &tspd_sp_context[linear_id]); 238375f538aSAchin Gupta 239faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 240faaa2e76SVikram Kanigiri bl31_set_next_image_type(SECURE); 241faaa2e76SVikram Kanigiri #else 2427f366605SJeenu Viswambharan /* 2437f366605SJeenu Viswambharan * All TSPD initialization done. Now register our init function with 2447f366605SJeenu Viswambharan * BL31 for deferred invocation 2457f366605SJeenu Viswambharan */ 2467f366605SJeenu Viswambharan bl31_register_bl32_init(&tspd_init); 247faaa2e76SVikram Kanigiri #endif 24850e27dadSVikram Kanigiri return 0; 249375f538aSAchin Gupta } 250375f538aSAchin Gupta 251375f538aSAchin Gupta /******************************************************************************* 252375f538aSAchin Gupta * This function passes control to the Secure Payload image (BL32) for the first 253375f538aSAchin Gupta * time on the primary cpu after a cold boot. It assumes that a valid secure 254375f538aSAchin Gupta * context has already been created by tspd_setup() which can be directly used. 255375f538aSAchin Gupta * It also assumes that a valid non-secure context has been initialised by PSCI 256375f538aSAchin Gupta * so it does not need to save and restore any non-secure state. This function 257375f538aSAchin Gupta * performs a synchronous entry into the Secure payload. The SP passes control 2586871c5d3SVikram Kanigiri * back to this routine through a SMC. 259375f538aSAchin Gupta ******************************************************************************/ 2606871c5d3SVikram Kanigiri int32_t tspd_init(void) 261375f538aSAchin Gupta { 262fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(); 263fb037bfbSDan Handley tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 26450e27dadSVikram Kanigiri entry_point_info_t *tsp_entry_point; 265faaa2e76SVikram Kanigiri uint64_t rc; 26650e27dadSVikram Kanigiri 26750e27dadSVikram Kanigiri /* 26850e27dadSVikram Kanigiri * Get information about the Secure Payload (BL32) image. Its 26950e27dadSVikram Kanigiri * absence is a critical failure. 27050e27dadSVikram Kanigiri */ 27150e27dadSVikram Kanigiri tsp_entry_point = bl31_plat_get_next_image_ep_info(SECURE); 27250e27dadSVikram Kanigiri assert(tsp_entry_point); 27350e27dadSVikram Kanigiri 274fd650ff6SSoby Mathew cm_init_my_context(tsp_entry_point); 275375f538aSAchin Gupta 276375f538aSAchin Gupta /* 277faaa2e76SVikram Kanigiri * Arrange for an entry into the test secure payload. It will be 278faaa2e76SVikram Kanigiri * returned via TSP_ENTRY_DONE case 279607084eeSAchin Gupta */ 280375f538aSAchin Gupta rc = tspd_synchronous_sp_entry(tsp_ctx); 281375f538aSAchin Gupta assert(rc != 0); 282b44a4435SAchin Gupta 283375f538aSAchin Gupta return rc; 284375f538aSAchin Gupta } 285375f538aSAchin Gupta 2867f366605SJeenu Viswambharan 287375f538aSAchin Gupta /******************************************************************************* 288375f538aSAchin Gupta * This function is responsible for handling all SMCs in the Trusted OS/App 289375f538aSAchin Gupta * range from the non-secure state as defined in the SMC Calling Convention 290375f538aSAchin Gupta * Document. It is also responsible for communicating with the Secure payload 291375f538aSAchin Gupta * to delegate work and return results back to the non-secure state. Lastly it 292375f538aSAchin Gupta * will also return any information that the secure payload needs to do the 293375f538aSAchin Gupta * work assigned to it. 294375f538aSAchin Gupta ******************************************************************************/ 295375f538aSAchin Gupta uint64_t tspd_smc_handler(uint32_t smc_fid, 296375f538aSAchin Gupta uint64_t x1, 297375f538aSAchin Gupta uint64_t x2, 298375f538aSAchin Gupta uint64_t x3, 299375f538aSAchin Gupta uint64_t x4, 300375f538aSAchin Gupta void *cookie, 301375f538aSAchin Gupta void *handle, 302375f538aSAchin Gupta uint64_t flags) 303375f538aSAchin Gupta { 304fb037bfbSDan Handley cpu_context_t *ns_cpu_context; 305fd650ff6SSoby Mathew uint32_t linear_id = plat_my_core_pos(), ns; 306fb037bfbSDan Handley tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 307faaa2e76SVikram Kanigiri uint64_t rc; 308faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 309faaa2e76SVikram Kanigiri entry_point_info_t *next_image_info; 310faaa2e76SVikram Kanigiri #endif 311375f538aSAchin Gupta 312375f538aSAchin Gupta /* Determine which security state this SMC originated from */ 313375f538aSAchin Gupta ns = is_caller_non_secure(flags); 314375f538aSAchin Gupta 315375f538aSAchin Gupta switch (smc_fid) { 316375f538aSAchin Gupta 317375f538aSAchin Gupta /* 318239b04faSSoby Mathew * This function ID is used by TSP to indicate that it was 319239b04faSSoby Mathew * preempted by a normal world IRQ. 320239b04faSSoby Mathew * 321239b04faSSoby Mathew */ 322239b04faSSoby Mathew case TSP_PREEMPTED: 323239b04faSSoby Mathew if (ns) 324239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 325239b04faSSoby Mathew 326f4f1ae77SSoby Mathew return tspd_handle_sp_preemption(handle); 327239b04faSSoby Mathew 328239b04faSSoby Mathew /* 329b44a4435SAchin Gupta * This function ID is used only by the TSP to indicate that it has 330b44a4435SAchin Gupta * finished handling a S-EL1 FIQ interrupt. Execution should resume 331b44a4435SAchin Gupta * in the normal world. 332b44a4435SAchin Gupta */ 333b44a4435SAchin Gupta case TSP_HANDLED_S_EL1_FIQ: 334b44a4435SAchin Gupta if (ns) 335b44a4435SAchin Gupta SMC_RET1(handle, SMC_UNK); 336b44a4435SAchin Gupta 33708ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 338b44a4435SAchin Gupta 339b44a4435SAchin Gupta /* 340b44a4435SAchin Gupta * Restore the relevant EL3 state which saved to service 341b44a4435SAchin Gupta * this SMC. 342b44a4435SAchin Gupta */ 343b44a4435SAchin Gupta if (get_std_smc_active_flag(tsp_ctx->state)) { 344b44a4435SAchin Gupta SMC_SET_EL3(&tsp_ctx->cpu_ctx, 345b44a4435SAchin Gupta CTX_SPSR_EL3, 346b44a4435SAchin Gupta tsp_ctx->saved_spsr_el3); 347b44a4435SAchin Gupta SMC_SET_EL3(&tsp_ctx->cpu_ctx, 348b44a4435SAchin Gupta CTX_ELR_EL3, 349b44a4435SAchin Gupta tsp_ctx->saved_elr_el3); 350f4f1ae77SSoby Mathew #if TSPD_ROUTE_IRQ_TO_EL3 351f4f1ae77SSoby Mathew /* 352f4f1ae77SSoby Mathew * Need to restore the previously interrupted 353f4f1ae77SSoby Mathew * secure context. 354f4f1ae77SSoby Mathew */ 355f4f1ae77SSoby Mathew memcpy(&tsp_ctx->cpu_ctx, &tsp_ctx->sp_ctx, 356f4f1ae77SSoby Mathew TSPD_SP_CTX_SIZE); 357f4f1ae77SSoby Mathew #endif 358b44a4435SAchin Gupta } 359b44a4435SAchin Gupta 360b44a4435SAchin Gupta /* Get a reference to the non-secure context */ 36108ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 362b44a4435SAchin Gupta assert(ns_cpu_context); 363b44a4435SAchin Gupta 364b44a4435SAchin Gupta /* 365b44a4435SAchin Gupta * Restore non-secure state. There is no need to save the 366b44a4435SAchin Gupta * secure system register context since the TSP was supposed 367b44a4435SAchin Gupta * to preserve it during S-EL1 interrupt handling. 368b44a4435SAchin Gupta */ 369b44a4435SAchin Gupta cm_el1_sysregs_context_restore(NON_SECURE); 370b44a4435SAchin Gupta cm_set_next_eret_context(NON_SECURE); 371b44a4435SAchin Gupta 372b44a4435SAchin Gupta SMC_RET0((uint64_t) ns_cpu_context); 373b44a4435SAchin Gupta 374b44a4435SAchin Gupta /* 375375f538aSAchin Gupta * This function ID is used only by the SP to indicate it has 376375f538aSAchin Gupta * finished initialising itself after a cold boot 377375f538aSAchin Gupta */ 378375f538aSAchin Gupta case TSP_ENTRY_DONE: 379375f538aSAchin Gupta if (ns) 380375f538aSAchin Gupta SMC_RET1(handle, SMC_UNK); 381375f538aSAchin Gupta 382375f538aSAchin Gupta /* 383375f538aSAchin Gupta * Stash the SP entry points information. This is done 384375f538aSAchin Gupta * only once on the primary cpu 385375f538aSAchin Gupta */ 386399fb08fSAndrew Thoelke assert(tsp_vectors == NULL); 387399fb08fSAndrew Thoelke tsp_vectors = (tsp_vectors_t *) x1; 388375f538aSAchin Gupta 389faaa2e76SVikram Kanigiri if (tsp_vectors) { 390faaa2e76SVikram Kanigiri set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON); 391faaa2e76SVikram Kanigiri 392faaa2e76SVikram Kanigiri /* 393faaa2e76SVikram Kanigiri * TSP has been successfully initialized. Register power 394faaa2e76SVikram Kanigiri * managemnt hooks with PSCI 395faaa2e76SVikram Kanigiri */ 396faaa2e76SVikram Kanigiri psci_register_spd_pm_hook(&tspd_pm); 397faaa2e76SVikram Kanigiri 398faaa2e76SVikram Kanigiri /* 399faaa2e76SVikram Kanigiri * Register an interrupt handler for S-EL1 interrupts 400faaa2e76SVikram Kanigiri * when generated during code executing in the 401faaa2e76SVikram Kanigiri * non-secure state. 402faaa2e76SVikram Kanigiri */ 403faaa2e76SVikram Kanigiri flags = 0; 404faaa2e76SVikram Kanigiri set_interrupt_rm_flag(flags, NON_SECURE); 405faaa2e76SVikram Kanigiri rc = register_interrupt_type_handler(INTR_TYPE_S_EL1, 406faaa2e76SVikram Kanigiri tspd_sel1_interrupt_handler, 407faaa2e76SVikram Kanigiri flags); 408faaa2e76SVikram Kanigiri if (rc) 409faaa2e76SVikram Kanigiri panic(); 410f4f1ae77SSoby Mathew 411f4f1ae77SSoby Mathew #if TSPD_ROUTE_IRQ_TO_EL3 412f4f1ae77SSoby Mathew /* 413f4f1ae77SSoby Mathew * Register an interrupt handler for NS interrupts when 414f4f1ae77SSoby Mathew * generated during code executing in secure state are 415f4f1ae77SSoby Mathew * routed to EL3. 416f4f1ae77SSoby Mathew */ 417f4f1ae77SSoby Mathew flags = 0; 418f4f1ae77SSoby Mathew set_interrupt_rm_flag(flags, SECURE); 419f4f1ae77SSoby Mathew 420f4f1ae77SSoby Mathew rc = register_interrupt_type_handler(INTR_TYPE_NS, 421f4f1ae77SSoby Mathew tspd_ns_interrupt_handler, 422f4f1ae77SSoby Mathew flags); 423f4f1ae77SSoby Mathew if (rc) 424f4f1ae77SSoby Mathew panic(); 425f4f1ae77SSoby Mathew 426f4f1ae77SSoby Mathew /* 427*404dba53SSoby Mathew * Disable the NS interrupt locally. 428f4f1ae77SSoby Mathew */ 429f4f1ae77SSoby Mathew disable_intr_rm_local(INTR_TYPE_NS, SECURE); 430f4f1ae77SSoby Mathew #endif 431faaa2e76SVikram Kanigiri } 432faaa2e76SVikram Kanigiri 433faaa2e76SVikram Kanigiri 434faaa2e76SVikram Kanigiri #if TSP_INIT_ASYNC 435faaa2e76SVikram Kanigiri /* Save the Secure EL1 system register context */ 436faaa2e76SVikram Kanigiri assert(cm_get_context(SECURE) == &tsp_ctx->cpu_ctx); 437faaa2e76SVikram Kanigiri cm_el1_sysregs_context_save(SECURE); 438faaa2e76SVikram Kanigiri 439faaa2e76SVikram Kanigiri /* Program EL3 registers to enable entry into the next EL */ 440faaa2e76SVikram Kanigiri next_image_info = bl31_plat_get_next_image_ep_info(NON_SECURE); 441faaa2e76SVikram Kanigiri assert(next_image_info); 442faaa2e76SVikram Kanigiri assert(NON_SECURE == 443faaa2e76SVikram Kanigiri GET_SECURITY_STATE(next_image_info->h.attr)); 444faaa2e76SVikram Kanigiri 445fd650ff6SSoby Mathew cm_init_my_context(next_image_info); 446faaa2e76SVikram Kanigiri cm_prepare_el3_exit(NON_SECURE); 447faaa2e76SVikram Kanigiri SMC_RET0(cm_get_context(NON_SECURE)); 448faaa2e76SVikram Kanigiri #else 449375f538aSAchin Gupta /* 450375f538aSAchin Gupta * SP reports completion. The SPD must have initiated 451375f538aSAchin Gupta * the original request through a synchronous entry 452375f538aSAchin Gupta * into the SP. Jump back to the original C runtime 453375f538aSAchin Gupta * context. 454375f538aSAchin Gupta */ 455916a2c1eSAchin Gupta tspd_synchronous_sp_exit(tsp_ctx, x1); 456faaa2e76SVikram Kanigiri #endif 457375f538aSAchin Gupta 458607084eeSAchin Gupta /* 459607084eeSAchin Gupta * These function IDs is used only by the SP to indicate it has 460607084eeSAchin Gupta * finished: 461607084eeSAchin Gupta * 1. turning itself on in response to an earlier psci 462607084eeSAchin Gupta * cpu_on request 463607084eeSAchin Gupta * 2. resuming itself after an earlier psci cpu_suspend 464607084eeSAchin Gupta * request. 465607084eeSAchin Gupta */ 466607084eeSAchin Gupta case TSP_ON_DONE: 467607084eeSAchin Gupta case TSP_RESUME_DONE: 468607084eeSAchin Gupta 469607084eeSAchin Gupta /* 470607084eeSAchin Gupta * These function IDs is used only by the SP to indicate it has 471607084eeSAchin Gupta * finished: 472607084eeSAchin Gupta * 1. suspending itself after an earlier psci cpu_suspend 473607084eeSAchin Gupta * request. 474607084eeSAchin Gupta * 2. turning itself off in response to an earlier psci 475607084eeSAchin Gupta * cpu_off request. 476607084eeSAchin Gupta */ 477607084eeSAchin Gupta case TSP_OFF_DONE: 478607084eeSAchin Gupta case TSP_SUSPEND_DONE: 479d5f13093SJuan Castillo case TSP_SYSTEM_OFF_DONE: 480d5f13093SJuan Castillo case TSP_SYSTEM_RESET_DONE: 481607084eeSAchin Gupta if (ns) 482607084eeSAchin Gupta SMC_RET1(handle, SMC_UNK); 483607084eeSAchin Gupta 484607084eeSAchin Gupta /* 485607084eeSAchin Gupta * SP reports completion. The SPD must have initiated the 486607084eeSAchin Gupta * original request through a synchronous entry into the SP. 487607084eeSAchin Gupta * Jump back to the original C runtime context, and pass x1 as 488607084eeSAchin Gupta * return value to the caller 489607084eeSAchin Gupta */ 490916a2c1eSAchin Gupta tspd_synchronous_sp_exit(tsp_ctx, x1); 491607084eeSAchin Gupta 492916a2c1eSAchin Gupta /* 493916a2c1eSAchin Gupta * Request from non-secure client to perform an 494916a2c1eSAchin Gupta * arithmetic operation or response from secure 495916a2c1eSAchin Gupta * payload to an earlier request. 496916a2c1eSAchin Gupta */ 497239b04faSSoby Mathew case TSP_FAST_FID(TSP_ADD): 498239b04faSSoby Mathew case TSP_FAST_FID(TSP_SUB): 499239b04faSSoby Mathew case TSP_FAST_FID(TSP_MUL): 500239b04faSSoby Mathew case TSP_FAST_FID(TSP_DIV): 501239b04faSSoby Mathew 502239b04faSSoby Mathew case TSP_STD_FID(TSP_ADD): 503239b04faSSoby Mathew case TSP_STD_FID(TSP_SUB): 504239b04faSSoby Mathew case TSP_STD_FID(TSP_MUL): 505239b04faSSoby Mathew case TSP_STD_FID(TSP_DIV): 506916a2c1eSAchin Gupta if (ns) { 507916a2c1eSAchin Gupta /* 508916a2c1eSAchin Gupta * This is a fresh request from the non-secure client. 509916a2c1eSAchin Gupta * The parameters are in x1 and x2. Figure out which 510916a2c1eSAchin Gupta * registers need to be preserved, save the non-secure 511916a2c1eSAchin Gupta * state and send the request to the secure payload. 512916a2c1eSAchin Gupta */ 51308ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 514239b04faSSoby Mathew 515239b04faSSoby Mathew /* Check if we are already preempted */ 516239b04faSSoby Mathew if (get_std_smc_active_flag(tsp_ctx->state)) 517239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 518239b04faSSoby Mathew 519916a2c1eSAchin Gupta cm_el1_sysregs_context_save(NON_SECURE); 520916a2c1eSAchin Gupta 521916a2c1eSAchin Gupta /* Save x1 and x2 for use by TSP_GET_ARGS call below */ 522239b04faSSoby Mathew store_tsp_args(tsp_ctx, x1, x2); 523916a2c1eSAchin Gupta 524916a2c1eSAchin Gupta /* 525916a2c1eSAchin Gupta * We are done stashing the non-secure context. Ask the 526916a2c1eSAchin Gupta * secure payload to do the work now. 527916a2c1eSAchin Gupta */ 528916a2c1eSAchin Gupta 529916a2c1eSAchin Gupta /* 530916a2c1eSAchin Gupta * Verify if there is a valid context to use, copy the 531916a2c1eSAchin Gupta * operation type and parameters to the secure context 532916a2c1eSAchin Gupta * and jump to the fast smc entry point in the secure 533916a2c1eSAchin Gupta * payload. Entry into S-EL1 will take place upon exit 534916a2c1eSAchin Gupta * from this function. 535916a2c1eSAchin Gupta */ 53608ab89d3SAndrew Thoelke assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE)); 537239b04faSSoby Mathew 538239b04faSSoby Mathew /* Set appropriate entry for SMC. 539239b04faSSoby Mathew * We expect the TSP to manage the PSTATE.I and PSTATE.F 540239b04faSSoby Mathew * flags as appropriate. 541239b04faSSoby Mathew */ 542239b04faSSoby Mathew if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_FAST) { 543239b04faSSoby Mathew cm_set_elr_el3(SECURE, (uint64_t) 544399fb08fSAndrew Thoelke &tsp_vectors->fast_smc_entry); 545239b04faSSoby Mathew } else { 546239b04faSSoby Mathew set_std_smc_active_flag(tsp_ctx->state); 547239b04faSSoby Mathew cm_set_elr_el3(SECURE, (uint64_t) 548399fb08fSAndrew Thoelke &tsp_vectors->std_smc_entry); 549f4f1ae77SSoby Mathew #if TSPD_ROUTE_IRQ_TO_EL3 550f4f1ae77SSoby Mathew /* 551f4f1ae77SSoby Mathew * Enable the routing of NS interrupts to EL3 552f4f1ae77SSoby Mathew * during STD SMC processing on this core. 553f4f1ae77SSoby Mathew */ 554f4f1ae77SSoby Mathew enable_intr_rm_local(INTR_TYPE_NS, SECURE); 555f4f1ae77SSoby Mathew #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); 578f4f1ae77SSoby Mathew if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_STD) { 579239b04faSSoby Mathew clr_std_smc_active_flag(tsp_ctx->state); 580f4f1ae77SSoby Mathew #if TSPD_ROUTE_IRQ_TO_EL3 581f4f1ae77SSoby Mathew /* 582f4f1ae77SSoby Mathew * Disable the routing of NS interrupts to EL3 583f4f1ae77SSoby Mathew * after STD SMC processing is finished on this 584f4f1ae77SSoby Mathew * core. 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; 594916a2c1eSAchin Gupta 595916a2c1eSAchin Gupta /* 596239b04faSSoby Mathew * Request from non secure world to resume the preempted 597239b04faSSoby Mathew * Standard SMC call. 598239b04faSSoby Mathew */ 599239b04faSSoby Mathew case TSP_FID_RESUME: 600239b04faSSoby Mathew /* RESUME should be invoked only by normal world */ 601239b04faSSoby Mathew if (!ns) { 602239b04faSSoby Mathew assert(0); 603239b04faSSoby Mathew break; 604239b04faSSoby Mathew } 605239b04faSSoby Mathew 606239b04faSSoby Mathew /* 607239b04faSSoby Mathew * This is a resume request from the non-secure client. 608239b04faSSoby Mathew * save the non-secure state and send the request to 609239b04faSSoby Mathew * the secure payload. 610239b04faSSoby Mathew */ 61108ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 612239b04faSSoby Mathew 613239b04faSSoby Mathew /* Check if we are already preempted before resume */ 614239b04faSSoby Mathew if (!get_std_smc_active_flag(tsp_ctx->state)) 615239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 616239b04faSSoby Mathew 617239b04faSSoby Mathew cm_el1_sysregs_context_save(NON_SECURE); 618239b04faSSoby Mathew 619239b04faSSoby Mathew /* 620239b04faSSoby Mathew * We are done stashing the non-secure context. Ask the 621239b04faSSoby Mathew * secure payload to do the work now. 622239b04faSSoby Mathew */ 623f4f1ae77SSoby Mathew #if TSPD_ROUTE_IRQ_TO_EL3 624f4f1ae77SSoby Mathew /* 625f4f1ae77SSoby Mathew * Enable the routing of NS interrupts to EL3 during resumption 626f4f1ae77SSoby Mathew * of STD SMC call on this core. 627f4f1ae77SSoby Mathew */ 628f4f1ae77SSoby Mathew enable_intr_rm_local(INTR_TYPE_NS, SECURE); 629f4f1ae77SSoby Mathew #endif 630f4f1ae77SSoby Mathew 631f4f1ae77SSoby Mathew 632239b04faSSoby Mathew 633239b04faSSoby Mathew /* We just need to return to the preempted point in 634239b04faSSoby Mathew * TSP and the execution will resume as normal. 635239b04faSSoby Mathew */ 636239b04faSSoby Mathew cm_el1_sysregs_context_restore(SECURE); 637239b04faSSoby Mathew cm_set_next_eret_context(SECURE); 63810b65ecfSSoby Mathew SMC_RET0(&tsp_ctx->cpu_ctx); 639239b04faSSoby Mathew 640239b04faSSoby Mathew /* 641916a2c1eSAchin Gupta * This is a request from the secure payload for more arguments 642916a2c1eSAchin Gupta * for an ongoing arithmetic operation requested by the 643916a2c1eSAchin Gupta * non-secure world. Simply return the arguments from the non- 644916a2c1eSAchin Gupta * secure client in the original call. 645916a2c1eSAchin Gupta */ 646916a2c1eSAchin Gupta case TSP_GET_ARGS: 647916a2c1eSAchin Gupta if (ns) 648916a2c1eSAchin Gupta SMC_RET1(handle, SMC_UNK); 649916a2c1eSAchin Gupta 650239b04faSSoby Mathew get_tsp_args(tsp_ctx, x1, x2); 651239b04faSSoby Mathew SMC_RET2(handle, x1, x2); 652916a2c1eSAchin Gupta 65352538b9bSJeenu Viswambharan case TOS_CALL_COUNT: 65452538b9bSJeenu Viswambharan /* 65552538b9bSJeenu Viswambharan * Return the number of service function IDs implemented to 65652538b9bSJeenu Viswambharan * provide service to non-secure 65752538b9bSJeenu Viswambharan */ 65852538b9bSJeenu Viswambharan SMC_RET1(handle, TSP_NUM_FID); 65952538b9bSJeenu Viswambharan 66052538b9bSJeenu Viswambharan case TOS_UID: 66152538b9bSJeenu Viswambharan /* Return TSP UID to the caller */ 66252538b9bSJeenu Viswambharan SMC_UUID_RET(handle, tsp_uuid); 66352538b9bSJeenu Viswambharan 66452538b9bSJeenu Viswambharan case TOS_CALL_VERSION: 66552538b9bSJeenu Viswambharan /* Return the version of current implementation */ 66652538b9bSJeenu Viswambharan SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR); 66752538b9bSJeenu Viswambharan 668375f538aSAchin Gupta default: 669607084eeSAchin Gupta break; 670375f538aSAchin Gupta } 671375f538aSAchin Gupta 672607084eeSAchin Gupta SMC_RET1(handle, SMC_UNK); 673375f538aSAchin Gupta } 674375f538aSAchin Gupta 675239b04faSSoby Mathew /* Define a SPD runtime service descriptor for fast SMC calls */ 676375f538aSAchin Gupta DECLARE_RT_SVC( 677239b04faSSoby Mathew tspd_fast, 678375f538aSAchin Gupta 679375f538aSAchin Gupta OEN_TOS_START, 680375f538aSAchin Gupta OEN_TOS_END, 681375f538aSAchin Gupta SMC_TYPE_FAST, 682375f538aSAchin Gupta tspd_setup, 683375f538aSAchin Gupta tspd_smc_handler 684375f538aSAchin Gupta ); 685239b04faSSoby Mathew 686239b04faSSoby Mathew /* Define a SPD runtime service descriptor for standard SMC calls */ 687239b04faSSoby Mathew DECLARE_RT_SVC( 688239b04faSSoby Mathew tspd_std, 689239b04faSSoby Mathew 690239b04faSSoby Mathew OEN_TOS_START, 691239b04faSSoby Mathew OEN_TOS_END, 692239b04faSSoby Mathew SMC_TYPE_STD, 693239b04faSSoby Mathew NULL, 694239b04faSSoby Mathew tspd_smc_handler 695239b04faSSoby Mathew ); 696