1375f538aSAchin Gupta /* 2375f538aSAchin Gupta * Copyright (c) 2013-2014, 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> 51375f538aSAchin Gupta #include <tsp.h> 5252538b9bSJeenu Viswambharan #include <uuid.h> 5335e98e55SDan Handley #include "tspd_private.h" 54375f538aSAchin Gupta 55375f538aSAchin Gupta /******************************************************************************* 56399fb08fSAndrew Thoelke * Address of the entrypoint vector table in the Secure Payload. It is 57399fb08fSAndrew Thoelke * initialised once on the primary core after a cold boot. 58375f538aSAchin Gupta ******************************************************************************/ 59399fb08fSAndrew Thoelke tsp_vectors_t *tsp_vectors; 60375f538aSAchin Gupta 61375f538aSAchin Gupta /******************************************************************************* 62375f538aSAchin Gupta * Array to keep track of per-cpu Secure Payload state 63375f538aSAchin Gupta ******************************************************************************/ 64fb037bfbSDan Handley tsp_context_t tspd_sp_context[TSPD_CORE_COUNT]; 65375f538aSAchin Gupta 667f366605SJeenu Viswambharan 6752538b9bSJeenu Viswambharan /* TSP UID */ 6852538b9bSJeenu Viswambharan DEFINE_SVC_UUID(tsp_uuid, 6952538b9bSJeenu Viswambharan 0x5b3056a0, 0x3291, 0x427b, 0x98, 0x11, 7052538b9bSJeenu Viswambharan 0x71, 0x68, 0xca, 0x50, 0xf3, 0xfa); 7152538b9bSJeenu Viswambharan 726871c5d3SVikram Kanigiri int32_t tspd_init(void); 737f366605SJeenu Viswambharan 74b44a4435SAchin Gupta /******************************************************************************* 75b44a4435SAchin Gupta * This function is the handler registered for S-EL1 interrupts by the TSPD. It 76b44a4435SAchin Gupta * validates the interrupt and upon success arranges entry into the TSP at 77b44a4435SAchin Gupta * 'tsp_fiq_entry()' for handling the interrupt. 78b44a4435SAchin Gupta ******************************************************************************/ 79b44a4435SAchin Gupta static uint64_t tspd_sel1_interrupt_handler(uint32_t id, 80b44a4435SAchin Gupta uint32_t flags, 81b44a4435SAchin Gupta void *handle, 82b44a4435SAchin Gupta void *cookie) 83b44a4435SAchin Gupta { 84b44a4435SAchin Gupta uint32_t linear_id; 85b44a4435SAchin Gupta uint64_t mpidr; 86b44a4435SAchin Gupta tsp_context_t *tsp_ctx; 87b44a4435SAchin Gupta 88b44a4435SAchin Gupta /* Check the security state when the exception was generated */ 89b44a4435SAchin Gupta assert(get_interrupt_src_ss(flags) == NON_SECURE); 90b44a4435SAchin Gupta 91b44a4435SAchin Gupta #if IMF_READ_INTERRUPT_ID 92b44a4435SAchin Gupta /* Check the security status of the interrupt */ 93a3781085SSoby Mathew assert(plat_ic_get_interrupt_type(id) == INTR_TYPE_S_EL1); 94b44a4435SAchin Gupta #endif 95b44a4435SAchin Gupta 96b44a4435SAchin Gupta /* Sanity check the pointer to this cpu's context */ 97b44a4435SAchin Gupta mpidr = read_mpidr(); 9808ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 99b44a4435SAchin Gupta 100b44a4435SAchin Gupta /* Save the non-secure context before entering the TSP */ 101b44a4435SAchin Gupta cm_el1_sysregs_context_save(NON_SECURE); 102b44a4435SAchin Gupta 103b44a4435SAchin Gupta /* Get a reference to this cpu's TSP context */ 104b44a4435SAchin Gupta linear_id = platform_get_core_pos(mpidr); 105b44a4435SAchin Gupta tsp_ctx = &tspd_sp_context[linear_id]; 10608ab89d3SAndrew Thoelke assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE)); 107b44a4435SAchin Gupta 108b44a4435SAchin Gupta /* 109b44a4435SAchin Gupta * Determine if the TSP was previously preempted. Its last known 110b44a4435SAchin Gupta * context has to be preserved in this case. 111b44a4435SAchin Gupta * The TSP should return control to the TSPD after handling this 112b44a4435SAchin Gupta * FIQ. Preserve essential EL3 context to allow entry into the 113b44a4435SAchin Gupta * TSP at the FIQ entry point using the 'cpu_context' structure. 114b44a4435SAchin Gupta * There is no need to save the secure system register context 115b44a4435SAchin Gupta * since the TSP is supposed to preserve it during S-EL1 interrupt 116b44a4435SAchin Gupta * handling. 117b44a4435SAchin Gupta */ 118b44a4435SAchin Gupta if (get_std_smc_active_flag(tsp_ctx->state)) { 119b44a4435SAchin Gupta tsp_ctx->saved_spsr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx, 120b44a4435SAchin Gupta CTX_SPSR_EL3); 121b44a4435SAchin Gupta tsp_ctx->saved_elr_el3 = SMC_GET_EL3(&tsp_ctx->cpu_ctx, 122b44a4435SAchin Gupta CTX_ELR_EL3); 123b44a4435SAchin Gupta } 124b44a4435SAchin Gupta 125b44a4435SAchin Gupta cm_el1_sysregs_context_restore(SECURE); 126167a9357SAndrew Thoelke cm_set_elr_spsr_el3(SECURE, (uint64_t) &tsp_vectors->fiq_entry, 127167a9357SAndrew Thoelke SPSR_64(MODE_EL1, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS)); 128b44a4435SAchin Gupta cm_set_next_eret_context(SECURE); 129b44a4435SAchin Gupta 130b44a4435SAchin Gupta /* 131b44a4435SAchin Gupta * Tell the TSP that it has to handle an FIQ synchronously. Also the 132b44a4435SAchin Gupta * instruction in normal world where the interrupt was generated is 133b44a4435SAchin Gupta * passed for debugging purposes. It is safe to retrieve this address 134b44a4435SAchin Gupta * from ELR_EL3 as the secure context will not take effect until 135b44a4435SAchin Gupta * el3_exit(). 136b44a4435SAchin Gupta */ 137b44a4435SAchin Gupta SMC_RET2(&tsp_ctx->cpu_ctx, TSP_HANDLE_FIQ_AND_RETURN, read_elr_el3()); 138b44a4435SAchin Gupta } 1397f366605SJeenu Viswambharan 140375f538aSAchin Gupta /******************************************************************************* 141375f538aSAchin Gupta * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type 142375f538aSAchin Gupta * (aarch32/aarch64) if not already known and initialises the context for entry 143375f538aSAchin Gupta * into the SP for its initialisation. 144375f538aSAchin Gupta ******************************************************************************/ 145375f538aSAchin Gupta int32_t tspd_setup(void) 146375f538aSAchin Gupta { 147*50e27dadSVikram Kanigiri entry_point_info_t *tsp_ep_info; 148375f538aSAchin Gupta uint64_t mpidr = read_mpidr(); 149375f538aSAchin Gupta uint32_t linear_id; 150375f538aSAchin Gupta 151375f538aSAchin Gupta linear_id = platform_get_core_pos(mpidr); 152375f538aSAchin Gupta 153375f538aSAchin Gupta /* 154375f538aSAchin Gupta * Get information about the Secure Payload (BL32) image. Its 155375f538aSAchin Gupta * absence is a critical failure. TODO: Add support to 156375f538aSAchin Gupta * conditionally include the SPD service 157375f538aSAchin Gupta */ 158*50e27dadSVikram Kanigiri tsp_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 159*50e27dadSVikram Kanigiri if (!tsp_ep_info) { 160*50e27dadSVikram Kanigiri WARN("No TSP provided by BL2 boot loader, Booting device" 161*50e27dadSVikram Kanigiri " without TSP initialization. SMC`s destined for TSP" 162*50e27dadSVikram Kanigiri " will return SMC_UNK\n"); 163*50e27dadSVikram Kanigiri return 1; 164*50e27dadSVikram Kanigiri } 165375f538aSAchin Gupta 166375f538aSAchin Gupta /* 1677f366605SJeenu Viswambharan * If there's no valid entry point for SP, we return a non-zero value 1687f366605SJeenu Viswambharan * signalling failure initializing the service. We bail out without 1697f366605SJeenu Viswambharan * registering any handlers 1707f366605SJeenu Viswambharan */ 171*50e27dadSVikram Kanigiri if (!tsp_ep_info->pc) 1727f366605SJeenu Viswambharan return 1; 1737f366605SJeenu Viswambharan 1747f366605SJeenu Viswambharan /* 175375f538aSAchin Gupta * We could inspect the SP image and determine it's execution 176375f538aSAchin Gupta * state i.e whether AArch32 or AArch64. Assuming it's AArch64 177375f538aSAchin Gupta * for the time being. 178375f538aSAchin Gupta */ 179*50e27dadSVikram Kanigiri tspd_init_tsp_ep_state(tsp_ep_info, 180375f538aSAchin Gupta TSP_AARCH64, 181*50e27dadSVikram Kanigiri tsp_ep_info->pc, 182375f538aSAchin Gupta &tspd_sp_context[linear_id]); 183375f538aSAchin Gupta 1847f366605SJeenu Viswambharan /* 1857f366605SJeenu Viswambharan * All TSPD initialization done. Now register our init function with 1867f366605SJeenu Viswambharan * BL31 for deferred invocation 1877f366605SJeenu Viswambharan */ 1887f366605SJeenu Viswambharan bl31_register_bl32_init(&tspd_init); 1897f366605SJeenu Viswambharan 190*50e27dadSVikram Kanigiri return 0; 191375f538aSAchin Gupta } 192375f538aSAchin Gupta 193375f538aSAchin Gupta /******************************************************************************* 194375f538aSAchin Gupta * This function passes control to the Secure Payload image (BL32) for the first 195375f538aSAchin Gupta * time on the primary cpu after a cold boot. It assumes that a valid secure 196375f538aSAchin Gupta * context has already been created by tspd_setup() which can be directly used. 197375f538aSAchin Gupta * It also assumes that a valid non-secure context has been initialised by PSCI 198375f538aSAchin Gupta * so it does not need to save and restore any non-secure state. This function 199375f538aSAchin Gupta * performs a synchronous entry into the Secure payload. The SP passes control 2006871c5d3SVikram Kanigiri * back to this routine through a SMC. 201375f538aSAchin Gupta ******************************************************************************/ 2026871c5d3SVikram Kanigiri int32_t tspd_init(void) 203375f538aSAchin Gupta { 204375f538aSAchin Gupta uint64_t mpidr = read_mpidr(); 205b44a4435SAchin Gupta uint32_t linear_id = platform_get_core_pos(mpidr), flags; 206375f538aSAchin Gupta uint64_t rc; 207fb037bfbSDan Handley tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 208*50e27dadSVikram Kanigiri entry_point_info_t *tsp_entry_point; 209*50e27dadSVikram Kanigiri 210*50e27dadSVikram Kanigiri /* 211*50e27dadSVikram Kanigiri * Get information about the Secure Payload (BL32) image. Its 212*50e27dadSVikram Kanigiri * absence is a critical failure. 213*50e27dadSVikram Kanigiri */ 214*50e27dadSVikram Kanigiri tsp_entry_point = bl31_plat_get_next_image_ep_info(SECURE); 215*50e27dadSVikram Kanigiri assert(tsp_entry_point); 216*50e27dadSVikram Kanigiri 217*50e27dadSVikram Kanigiri cm_init_context(mpidr, tsp_entry_point); 218375f538aSAchin Gupta 219375f538aSAchin Gupta /* 220607084eeSAchin Gupta * Arrange for an entry into the test secure payload. We expect an array 221607084eeSAchin Gupta * of vectors in return 222607084eeSAchin Gupta */ 223375f538aSAchin Gupta rc = tspd_synchronous_sp_entry(tsp_ctx); 224375f538aSAchin Gupta assert(rc != 0); 2257f366605SJeenu Viswambharan if (rc) { 2263ee8a164SAchin Gupta set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON); 227375f538aSAchin Gupta 2287f366605SJeenu Viswambharan /* 2297f366605SJeenu Viswambharan * TSP has been successfully initialized. Register power 2307f366605SJeenu Viswambharan * managemnt hooks with PSCI 2317f366605SJeenu Viswambharan */ 2327f366605SJeenu Viswambharan psci_register_spd_pm_hook(&tspd_pm); 2337f366605SJeenu Viswambharan } 2347f366605SJeenu Viswambharan 235b44a4435SAchin Gupta /* 236b44a4435SAchin Gupta * Register an interrupt handler for S-EL1 interrupts when generated 237b44a4435SAchin Gupta * during code executing in the non-secure state. 238b44a4435SAchin Gupta */ 239b44a4435SAchin Gupta flags = 0; 240b44a4435SAchin Gupta set_interrupt_rm_flag(flags, NON_SECURE); 241b44a4435SAchin Gupta rc = register_interrupt_type_handler(INTR_TYPE_S_EL1, 242b44a4435SAchin Gupta tspd_sel1_interrupt_handler, 243b44a4435SAchin Gupta flags); 244b44a4435SAchin Gupta if (rc) 245b44a4435SAchin Gupta panic(); 246b44a4435SAchin Gupta 247375f538aSAchin Gupta return rc; 248375f538aSAchin Gupta } 249375f538aSAchin Gupta 2507f366605SJeenu Viswambharan 251375f538aSAchin Gupta /******************************************************************************* 252375f538aSAchin Gupta * This function is responsible for handling all SMCs in the Trusted OS/App 253375f538aSAchin Gupta * range from the non-secure state as defined in the SMC Calling Convention 254375f538aSAchin Gupta * Document. It is also responsible for communicating with the Secure payload 255375f538aSAchin Gupta * to delegate work and return results back to the non-secure state. Lastly it 256375f538aSAchin Gupta * will also return any information that the secure payload needs to do the 257375f538aSAchin Gupta * work assigned to it. 258375f538aSAchin Gupta ******************************************************************************/ 259375f538aSAchin Gupta uint64_t tspd_smc_handler(uint32_t smc_fid, 260375f538aSAchin Gupta uint64_t x1, 261375f538aSAchin Gupta uint64_t x2, 262375f538aSAchin Gupta uint64_t x3, 263375f538aSAchin Gupta uint64_t x4, 264375f538aSAchin Gupta void *cookie, 265375f538aSAchin Gupta void *handle, 266375f538aSAchin Gupta uint64_t flags) 267375f538aSAchin Gupta { 268fb037bfbSDan Handley cpu_context_t *ns_cpu_context; 269375f538aSAchin Gupta unsigned long mpidr = read_mpidr(); 270375f538aSAchin Gupta uint32_t linear_id = platform_get_core_pos(mpidr), ns; 271fb037bfbSDan Handley tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id]; 272375f538aSAchin Gupta 273375f538aSAchin Gupta /* Determine which security state this SMC originated from */ 274375f538aSAchin Gupta ns = is_caller_non_secure(flags); 275375f538aSAchin Gupta 276375f538aSAchin Gupta switch (smc_fid) { 277375f538aSAchin Gupta 278375f538aSAchin Gupta /* 279239b04faSSoby Mathew * This function ID is used by TSP to indicate that it was 280239b04faSSoby Mathew * preempted by a normal world IRQ. 281239b04faSSoby Mathew * 282239b04faSSoby Mathew */ 283239b04faSSoby Mathew case TSP_PREEMPTED: 284239b04faSSoby Mathew if (ns) 285239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 286239b04faSSoby Mathew 28708ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 288239b04faSSoby Mathew cm_el1_sysregs_context_save(SECURE); 289239b04faSSoby Mathew /* Get a reference to the non-secure context */ 29008ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 291239b04faSSoby Mathew assert(ns_cpu_context); 292239b04faSSoby Mathew 293239b04faSSoby Mathew /* 294239b04faSSoby Mathew * Restore non-secure state. There is no need to save the 295239b04faSSoby Mathew * secure system register context since the TSP was supposed 296239b04faSSoby Mathew * to preserve it during S-EL1 interrupt handling. 297239b04faSSoby Mathew */ 298239b04faSSoby Mathew cm_el1_sysregs_context_restore(NON_SECURE); 299239b04faSSoby Mathew cm_set_next_eret_context(NON_SECURE); 300239b04faSSoby Mathew 301239b04faSSoby Mathew SMC_RET1(ns_cpu_context, SMC_PREEMPTED); 302239b04faSSoby Mathew 303239b04faSSoby Mathew /* 304b44a4435SAchin Gupta * This function ID is used only by the TSP to indicate that it has 305b44a4435SAchin Gupta * finished handling a S-EL1 FIQ interrupt. Execution should resume 306b44a4435SAchin Gupta * in the normal world. 307b44a4435SAchin Gupta */ 308b44a4435SAchin Gupta case TSP_HANDLED_S_EL1_FIQ: 309b44a4435SAchin Gupta if (ns) 310b44a4435SAchin Gupta SMC_RET1(handle, SMC_UNK); 311b44a4435SAchin Gupta 31208ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 313b44a4435SAchin Gupta 314b44a4435SAchin Gupta /* 315b44a4435SAchin Gupta * Restore the relevant EL3 state which saved to service 316b44a4435SAchin Gupta * this SMC. 317b44a4435SAchin Gupta */ 318b44a4435SAchin Gupta if (get_std_smc_active_flag(tsp_ctx->state)) { 319b44a4435SAchin Gupta SMC_SET_EL3(&tsp_ctx->cpu_ctx, 320b44a4435SAchin Gupta CTX_SPSR_EL3, 321b44a4435SAchin Gupta tsp_ctx->saved_spsr_el3); 322b44a4435SAchin Gupta SMC_SET_EL3(&tsp_ctx->cpu_ctx, 323b44a4435SAchin Gupta CTX_ELR_EL3, 324b44a4435SAchin Gupta tsp_ctx->saved_elr_el3); 325b44a4435SAchin Gupta } 326b44a4435SAchin Gupta 327b44a4435SAchin Gupta /* Get a reference to the non-secure context */ 32808ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 329b44a4435SAchin Gupta assert(ns_cpu_context); 330b44a4435SAchin Gupta 331b44a4435SAchin Gupta /* 332b44a4435SAchin Gupta * Restore non-secure state. There is no need to save the 333b44a4435SAchin Gupta * secure system register context since the TSP was supposed 334b44a4435SAchin Gupta * to preserve it during S-EL1 interrupt handling. 335b44a4435SAchin Gupta */ 336b44a4435SAchin Gupta cm_el1_sysregs_context_restore(NON_SECURE); 337b44a4435SAchin Gupta cm_set_next_eret_context(NON_SECURE); 338b44a4435SAchin Gupta 339b44a4435SAchin Gupta SMC_RET0((uint64_t) ns_cpu_context); 340b44a4435SAchin Gupta 341b44a4435SAchin Gupta 342b44a4435SAchin Gupta /* 343b44a4435SAchin Gupta * This function ID is used only by the TSP to indicate that it was 344b44a4435SAchin Gupta * interrupted due to a EL3 FIQ interrupt. Execution should resume 345b44a4435SAchin Gupta * in the normal world. 346b44a4435SAchin Gupta */ 347b44a4435SAchin Gupta case TSP_EL3_FIQ: 348b44a4435SAchin Gupta if (ns) 349b44a4435SAchin Gupta SMC_RET1(handle, SMC_UNK); 350b44a4435SAchin Gupta 35108ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 352b44a4435SAchin Gupta 353b44a4435SAchin Gupta /* Assert that standard SMC execution has been preempted */ 354b44a4435SAchin Gupta assert(get_std_smc_active_flag(tsp_ctx->state)); 355b44a4435SAchin Gupta 356b44a4435SAchin Gupta /* Save the secure system register state */ 357b44a4435SAchin Gupta cm_el1_sysregs_context_save(SECURE); 358b44a4435SAchin Gupta 359b44a4435SAchin Gupta /* Get a reference to the non-secure context */ 36008ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 361b44a4435SAchin Gupta assert(ns_cpu_context); 362b44a4435SAchin Gupta 363b44a4435SAchin Gupta /* Restore non-secure state */ 364b44a4435SAchin Gupta cm_el1_sysregs_context_restore(NON_SECURE); 365b44a4435SAchin Gupta cm_set_next_eret_context(NON_SECURE); 366b44a4435SAchin Gupta 367b44a4435SAchin Gupta SMC_RET1(ns_cpu_context, TSP_EL3_FIQ); 368b44a4435SAchin Gupta 369b44a4435SAchin Gupta 370b44a4435SAchin Gupta /* 371375f538aSAchin Gupta * This function ID is used only by the SP to indicate it has 372375f538aSAchin Gupta * finished initialising itself after a cold boot 373375f538aSAchin Gupta */ 374375f538aSAchin Gupta case TSP_ENTRY_DONE: 375375f538aSAchin Gupta if (ns) 376375f538aSAchin Gupta SMC_RET1(handle, SMC_UNK); 377375f538aSAchin Gupta 378375f538aSAchin Gupta /* 379375f538aSAchin Gupta * Stash the SP entry points information. This is done 380375f538aSAchin Gupta * only once on the primary cpu 381375f538aSAchin Gupta */ 382399fb08fSAndrew Thoelke assert(tsp_vectors == NULL); 383399fb08fSAndrew Thoelke tsp_vectors = (tsp_vectors_t *) x1; 384375f538aSAchin Gupta 385375f538aSAchin Gupta /* 386375f538aSAchin Gupta * SP reports completion. The SPD must have initiated 387375f538aSAchin Gupta * the original request through a synchronous entry 388375f538aSAchin Gupta * into the SP. Jump back to the original C runtime 389375f538aSAchin Gupta * context. 390375f538aSAchin Gupta */ 391916a2c1eSAchin Gupta tspd_synchronous_sp_exit(tsp_ctx, x1); 392375f538aSAchin Gupta 393607084eeSAchin Gupta /* 394607084eeSAchin Gupta * These function IDs is used only by the SP to indicate it has 395607084eeSAchin Gupta * finished: 396607084eeSAchin Gupta * 1. turning itself on in response to an earlier psci 397607084eeSAchin Gupta * cpu_on request 398607084eeSAchin Gupta * 2. resuming itself after an earlier psci cpu_suspend 399607084eeSAchin Gupta * request. 400607084eeSAchin Gupta */ 401607084eeSAchin Gupta case TSP_ON_DONE: 402607084eeSAchin Gupta case TSP_RESUME_DONE: 403607084eeSAchin Gupta 404607084eeSAchin Gupta /* 405607084eeSAchin Gupta * These function IDs is used only by the SP to indicate it has 406607084eeSAchin Gupta * finished: 407607084eeSAchin Gupta * 1. suspending itself after an earlier psci cpu_suspend 408607084eeSAchin Gupta * request. 409607084eeSAchin Gupta * 2. turning itself off in response to an earlier psci 410607084eeSAchin Gupta * cpu_off request. 411607084eeSAchin Gupta */ 412607084eeSAchin Gupta case TSP_OFF_DONE: 413607084eeSAchin Gupta case TSP_SUSPEND_DONE: 414607084eeSAchin Gupta if (ns) 415607084eeSAchin Gupta SMC_RET1(handle, SMC_UNK); 416607084eeSAchin Gupta 417607084eeSAchin Gupta /* 418607084eeSAchin Gupta * SP reports completion. The SPD must have initiated the 419607084eeSAchin Gupta * original request through a synchronous entry into the SP. 420607084eeSAchin Gupta * Jump back to the original C runtime context, and pass x1 as 421607084eeSAchin Gupta * return value to the caller 422607084eeSAchin Gupta */ 423916a2c1eSAchin Gupta tspd_synchronous_sp_exit(tsp_ctx, x1); 424607084eeSAchin Gupta 425916a2c1eSAchin Gupta /* 426916a2c1eSAchin Gupta * Request from non-secure client to perform an 427916a2c1eSAchin Gupta * arithmetic operation or response from secure 428916a2c1eSAchin Gupta * payload to an earlier request. 429916a2c1eSAchin Gupta */ 430239b04faSSoby Mathew case TSP_FAST_FID(TSP_ADD): 431239b04faSSoby Mathew case TSP_FAST_FID(TSP_SUB): 432239b04faSSoby Mathew case TSP_FAST_FID(TSP_MUL): 433239b04faSSoby Mathew case TSP_FAST_FID(TSP_DIV): 434239b04faSSoby Mathew 435239b04faSSoby Mathew case TSP_STD_FID(TSP_ADD): 436239b04faSSoby Mathew case TSP_STD_FID(TSP_SUB): 437239b04faSSoby Mathew case TSP_STD_FID(TSP_MUL): 438239b04faSSoby Mathew case TSP_STD_FID(TSP_DIV): 439916a2c1eSAchin Gupta if (ns) { 440916a2c1eSAchin Gupta /* 441916a2c1eSAchin Gupta * This is a fresh request from the non-secure client. 442916a2c1eSAchin Gupta * The parameters are in x1 and x2. Figure out which 443916a2c1eSAchin Gupta * registers need to be preserved, save the non-secure 444916a2c1eSAchin Gupta * state and send the request to the secure payload. 445916a2c1eSAchin Gupta */ 44608ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 447239b04faSSoby Mathew 448239b04faSSoby Mathew /* Check if we are already preempted */ 449239b04faSSoby Mathew if (get_std_smc_active_flag(tsp_ctx->state)) 450239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 451239b04faSSoby Mathew 452916a2c1eSAchin Gupta cm_el1_sysregs_context_save(NON_SECURE); 453916a2c1eSAchin Gupta 454916a2c1eSAchin Gupta /* Save x1 and x2 for use by TSP_GET_ARGS call below */ 455239b04faSSoby Mathew store_tsp_args(tsp_ctx, x1, x2); 456916a2c1eSAchin Gupta 457916a2c1eSAchin Gupta /* 458916a2c1eSAchin Gupta * We are done stashing the non-secure context. Ask the 459916a2c1eSAchin Gupta * secure payload to do the work now. 460916a2c1eSAchin Gupta */ 461916a2c1eSAchin Gupta 462916a2c1eSAchin Gupta /* 463916a2c1eSAchin Gupta * Verify if there is a valid context to use, copy the 464916a2c1eSAchin Gupta * operation type and parameters to the secure context 465916a2c1eSAchin Gupta * and jump to the fast smc entry point in the secure 466916a2c1eSAchin Gupta * payload. Entry into S-EL1 will take place upon exit 467916a2c1eSAchin Gupta * from this function. 468916a2c1eSAchin Gupta */ 46908ab89d3SAndrew Thoelke assert(&tsp_ctx->cpu_ctx == cm_get_context(SECURE)); 470239b04faSSoby Mathew 471239b04faSSoby Mathew /* Set appropriate entry for SMC. 472239b04faSSoby Mathew * We expect the TSP to manage the PSTATE.I and PSTATE.F 473239b04faSSoby Mathew * flags as appropriate. 474239b04faSSoby Mathew */ 475239b04faSSoby Mathew if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_FAST) { 476239b04faSSoby Mathew cm_set_elr_el3(SECURE, (uint64_t) 477399fb08fSAndrew Thoelke &tsp_vectors->fast_smc_entry); 478239b04faSSoby Mathew } else { 479239b04faSSoby Mathew set_std_smc_active_flag(tsp_ctx->state); 480239b04faSSoby Mathew cm_set_elr_el3(SECURE, (uint64_t) 481399fb08fSAndrew Thoelke &tsp_vectors->std_smc_entry); 482239b04faSSoby Mathew } 483239b04faSSoby Mathew 484916a2c1eSAchin Gupta cm_el1_sysregs_context_restore(SECURE); 485916a2c1eSAchin Gupta cm_set_next_eret_context(SECURE); 486239b04faSSoby Mathew SMC_RET3(&tsp_ctx->cpu_ctx, smc_fid, x1, x2); 487916a2c1eSAchin Gupta } else { 488916a2c1eSAchin Gupta /* 489916a2c1eSAchin Gupta * This is the result from the secure client of an 490239b04faSSoby Mathew * earlier request. The results are in x1-x3. Copy it 491916a2c1eSAchin Gupta * into the non-secure context, save the secure state 492916a2c1eSAchin Gupta * and return to the non-secure state. 493916a2c1eSAchin Gupta */ 49408ab89d3SAndrew Thoelke assert(handle == cm_get_context(SECURE)); 495916a2c1eSAchin Gupta cm_el1_sysregs_context_save(SECURE); 496916a2c1eSAchin Gupta 497916a2c1eSAchin Gupta /* Get a reference to the non-secure context */ 49808ab89d3SAndrew Thoelke ns_cpu_context = cm_get_context(NON_SECURE); 499916a2c1eSAchin Gupta assert(ns_cpu_context); 500916a2c1eSAchin Gupta 501916a2c1eSAchin Gupta /* Restore non-secure state */ 502916a2c1eSAchin Gupta cm_el1_sysregs_context_restore(NON_SECURE); 503916a2c1eSAchin Gupta cm_set_next_eret_context(NON_SECURE); 504239b04faSSoby Mathew if (GET_SMC_TYPE(smc_fid) == SMC_TYPE_STD) 505239b04faSSoby Mathew clr_std_smc_active_flag(tsp_ctx->state); 506239b04faSSoby Mathew SMC_RET3(ns_cpu_context, x1, x2, x3); 507916a2c1eSAchin Gupta } 508916a2c1eSAchin Gupta 509916a2c1eSAchin Gupta break; 510916a2c1eSAchin Gupta 511916a2c1eSAchin Gupta /* 512239b04faSSoby Mathew * Request from non secure world to resume the preempted 513239b04faSSoby Mathew * Standard SMC call. 514239b04faSSoby Mathew */ 515239b04faSSoby Mathew case TSP_FID_RESUME: 516239b04faSSoby Mathew /* RESUME should be invoked only by normal world */ 517239b04faSSoby Mathew if (!ns) { 518239b04faSSoby Mathew assert(0); 519239b04faSSoby Mathew break; 520239b04faSSoby Mathew } 521239b04faSSoby Mathew 522239b04faSSoby Mathew /* 523239b04faSSoby Mathew * This is a resume request from the non-secure client. 524239b04faSSoby Mathew * save the non-secure state and send the request to 525239b04faSSoby Mathew * the secure payload. 526239b04faSSoby Mathew */ 52708ab89d3SAndrew Thoelke assert(handle == cm_get_context(NON_SECURE)); 528239b04faSSoby Mathew 529239b04faSSoby Mathew /* Check if we are already preempted before resume */ 530239b04faSSoby Mathew if (!get_std_smc_active_flag(tsp_ctx->state)) 531239b04faSSoby Mathew SMC_RET1(handle, SMC_UNK); 532239b04faSSoby Mathew 533239b04faSSoby Mathew cm_el1_sysregs_context_save(NON_SECURE); 534239b04faSSoby Mathew 535239b04faSSoby Mathew /* 536239b04faSSoby Mathew * We are done stashing the non-secure context. Ask the 537239b04faSSoby Mathew * secure payload to do the work now. 538239b04faSSoby Mathew */ 539239b04faSSoby Mathew 540239b04faSSoby Mathew /* We just need to return to the preempted point in 541239b04faSSoby Mathew * TSP and the execution will resume as normal. 542239b04faSSoby Mathew */ 543239b04faSSoby Mathew cm_el1_sysregs_context_restore(SECURE); 544239b04faSSoby Mathew cm_set_next_eret_context(SECURE); 54510b65ecfSSoby Mathew SMC_RET0(&tsp_ctx->cpu_ctx); 546239b04faSSoby Mathew 547239b04faSSoby Mathew /* 548916a2c1eSAchin Gupta * This is a request from the secure payload for more arguments 549916a2c1eSAchin Gupta * for an ongoing arithmetic operation requested by the 550916a2c1eSAchin Gupta * non-secure world. Simply return the arguments from the non- 551916a2c1eSAchin Gupta * secure client in the original call. 552916a2c1eSAchin Gupta */ 553916a2c1eSAchin Gupta case TSP_GET_ARGS: 554916a2c1eSAchin Gupta if (ns) 555916a2c1eSAchin Gupta SMC_RET1(handle, SMC_UNK); 556916a2c1eSAchin Gupta 557239b04faSSoby Mathew get_tsp_args(tsp_ctx, x1, x2); 558239b04faSSoby Mathew SMC_RET2(handle, x1, x2); 559916a2c1eSAchin Gupta 56052538b9bSJeenu Viswambharan case TOS_CALL_COUNT: 56152538b9bSJeenu Viswambharan /* 56252538b9bSJeenu Viswambharan * Return the number of service function IDs implemented to 56352538b9bSJeenu Viswambharan * provide service to non-secure 56452538b9bSJeenu Viswambharan */ 56552538b9bSJeenu Viswambharan SMC_RET1(handle, TSP_NUM_FID); 56652538b9bSJeenu Viswambharan 56752538b9bSJeenu Viswambharan case TOS_UID: 56852538b9bSJeenu Viswambharan /* Return TSP UID to the caller */ 56952538b9bSJeenu Viswambharan SMC_UUID_RET(handle, tsp_uuid); 57052538b9bSJeenu Viswambharan 57152538b9bSJeenu Viswambharan case TOS_CALL_VERSION: 57252538b9bSJeenu Viswambharan /* Return the version of current implementation */ 57352538b9bSJeenu Viswambharan SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR); 57452538b9bSJeenu Viswambharan 575375f538aSAchin Gupta default: 576607084eeSAchin Gupta break; 577375f538aSAchin Gupta } 578375f538aSAchin Gupta 579607084eeSAchin Gupta SMC_RET1(handle, SMC_UNK); 580375f538aSAchin Gupta } 581375f538aSAchin Gupta 582239b04faSSoby Mathew /* Define a SPD runtime service descriptor for fast SMC calls */ 583375f538aSAchin Gupta DECLARE_RT_SVC( 584239b04faSSoby Mathew tspd_fast, 585375f538aSAchin Gupta 586375f538aSAchin Gupta OEN_TOS_START, 587375f538aSAchin Gupta OEN_TOS_END, 588375f538aSAchin Gupta SMC_TYPE_FAST, 589375f538aSAchin Gupta tspd_setup, 590375f538aSAchin Gupta tspd_smc_handler 591375f538aSAchin Gupta ); 592239b04faSSoby Mathew 593239b04faSSoby Mathew /* Define a SPD runtime service descriptor for standard SMC calls */ 594239b04faSSoby Mathew DECLARE_RT_SVC( 595239b04faSSoby Mathew tspd_std, 596239b04faSSoby Mathew 597239b04faSSoby Mathew OEN_TOS_START, 598239b04faSSoby Mathew OEN_TOS_END, 599239b04faSSoby Mathew SMC_TYPE_STD, 600239b04faSSoby Mathew NULL, 601239b04faSSoby Mathew tspd_smc_handler 602239b04faSSoby Mathew ); 603