122038315SVarun Wadekar /* 2*d205cda6SVarun Wadekar * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved. 322038315SVarun Wadekar * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 522038315SVarun Wadekar */ 622038315SVarun Wadekar 722038315SVarun Wadekar /******************************************************************************* 822038315SVarun Wadekar * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a 922038315SVarun Wadekar * plug-in component to the Secure Monitor, registered as a runtime service. The 1022038315SVarun Wadekar * SPD is expected to be a functional extension of the Secure Payload (SP) that 1122038315SVarun Wadekar * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting 1222038315SVarun Wadekar * the Trusted OS/Applications range to the dispatcher. The SPD will either 1322038315SVarun Wadekar * handle the request locally or delegate it to the Secure Payload. It is also 1422038315SVarun Wadekar * responsible for initialising and maintaining communication with the SP. 1522038315SVarun Wadekar ******************************************************************************/ 1622038315SVarun Wadekar #include <assert.h> 17*d205cda6SVarun Wadekar #include <bl31/interrupt_mgmt.h> 1822038315SVarun Wadekar #include <errno.h> 1922038315SVarun Wadekar #include <stddef.h> 2009d40e0eSAntonio Nino Diaz 21b29c1b00SAntonio Nino Diaz #include <arch_helpers.h> 2209d40e0eSAntonio Nino Diaz #include <bl31/bl31.h> 23b29c1b00SAntonio Nino Diaz #include <bl32/payloads/tlk.h> 2409d40e0eSAntonio Nino Diaz #include <common/bl_common.h> 2509d40e0eSAntonio Nino Diaz #include <common/debug.h> 2609d40e0eSAntonio Nino Diaz #include <common/runtime_svc.h> 2709d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h> 2809d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 2909d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h> 3009d40e0eSAntonio Nino Diaz 3122038315SVarun Wadekar #include "tlkd_private.h" 3222038315SVarun Wadekar 3322038315SVarun Wadekar extern const spd_pm_ops_t tlkd_pm_ops; 3422038315SVarun Wadekar 3522038315SVarun Wadekar /******************************************************************************* 36cb790c5eSVarun Wadekar * Per-cpu Secure Payload state 3722038315SVarun Wadekar ******************************************************************************/ 38cb790c5eSVarun Wadekar tlk_context_t tlk_ctx; 3922038315SVarun Wadekar 4026670c82SVarun Wadekar /******************************************************************************* 4126670c82SVarun Wadekar * CPU number on which TLK booted up 4226670c82SVarun Wadekar ******************************************************************************/ 436311f63dSVarun Wadekar static uint32_t boot_cpu; 4426670c82SVarun Wadekar 4522038315SVarun Wadekar /* TLK UID: RFC-4122 compliant UUID (version-5, sha-1) */ 4603364865SRoberto Vargas DEFINE_SVC_UUID2(tlk_uuid, 4703364865SRoberto Vargas 0xc9e911bd, 0xba2b, 0xee52, 0xb1, 0x72, 4822038315SVarun Wadekar 0x46, 0x1f, 0xba, 0x97, 0x7f, 0x63); 4922038315SVarun Wadekar 50724fd958SMasahiro Yamada static int32_t tlkd_init(void); 5122038315SVarun Wadekar 5222038315SVarun Wadekar /******************************************************************************* 53*d205cda6SVarun Wadekar * Secure Payload Dispatcher's timer interrupt handler 54*d205cda6SVarun Wadekar ******************************************************************************/ 55*d205cda6SVarun Wadekar static uint64_t tlkd_interrupt_handler(uint32_t id, 56*d205cda6SVarun Wadekar uint32_t flags, 57*d205cda6SVarun Wadekar void *handle, 58*d205cda6SVarun Wadekar void *cookie) 59*d205cda6SVarun Wadekar { 60*d205cda6SVarun Wadekar cpu_context_t *s_cpu_context; 61*d205cda6SVarun Wadekar int irq = plat_ic_get_pending_interrupt_id(); 62*d205cda6SVarun Wadekar 63*d205cda6SVarun Wadekar /* acknowledge the interrupt and mark it complete */ 64*d205cda6SVarun Wadekar (void)plat_ic_acknowledge_interrupt(); 65*d205cda6SVarun Wadekar plat_ic_end_of_interrupt(irq); 66*d205cda6SVarun Wadekar 67*d205cda6SVarun Wadekar /* 68*d205cda6SVarun Wadekar * Disable the routing of NS interrupts from secure world to 69*d205cda6SVarun Wadekar * EL3 while interrupted on this core. 70*d205cda6SVarun Wadekar */ 71*d205cda6SVarun Wadekar disable_intr_rm_local(INTR_TYPE_S_EL1, SECURE); 72*d205cda6SVarun Wadekar 73*d205cda6SVarun Wadekar /* Check the security state when the exception was generated */ 74*d205cda6SVarun Wadekar assert(get_interrupt_src_ss(flags) == NON_SECURE); 75*d205cda6SVarun Wadekar assert(handle == cm_get_context(NON_SECURE)); 76*d205cda6SVarun Wadekar 77*d205cda6SVarun Wadekar /* Save non-secure state */ 78*d205cda6SVarun Wadekar cm_el1_sysregs_context_save(NON_SECURE); 79*d205cda6SVarun Wadekar 80*d205cda6SVarun Wadekar /* Get a reference to the secure context */ 81*d205cda6SVarun Wadekar s_cpu_context = cm_get_context(SECURE); 82*d205cda6SVarun Wadekar assert(s_cpu_context); 83*d205cda6SVarun Wadekar 84*d205cda6SVarun Wadekar /* 85*d205cda6SVarun Wadekar * Restore non-secure state. There is no need to save the 86*d205cda6SVarun Wadekar * secure system register context since the SP was supposed 87*d205cda6SVarun Wadekar * to preserve it during S-EL1 interrupt handling. 88*d205cda6SVarun Wadekar */ 89*d205cda6SVarun Wadekar cm_el1_sysregs_context_restore(SECURE); 90*d205cda6SVarun Wadekar cm_set_next_eret_context(SECURE); 91*d205cda6SVarun Wadekar 92*d205cda6SVarun Wadekar /* Provide the IRQ number to the SPD */ 93*d205cda6SVarun Wadekar SMC_RET4(s_cpu_context, (uint32_t)TLK_IRQ_FIRED, 0, (uint32_t)irq, 0); 94*d205cda6SVarun Wadekar } 95*d205cda6SVarun Wadekar 96*d205cda6SVarun Wadekar /******************************************************************************* 9722038315SVarun Wadekar * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type 9822038315SVarun Wadekar * (aarch32/aarch64) if not already known and initialises the context for entry 9922038315SVarun Wadekar * into the SP for its initialisation. 10022038315SVarun Wadekar ******************************************************************************/ 101724fd958SMasahiro Yamada static int32_t tlkd_setup(void) 10222038315SVarun Wadekar { 10322038315SVarun Wadekar entry_point_info_t *tlk_ep_info; 104*d205cda6SVarun Wadekar uint32_t flags; 105*d205cda6SVarun Wadekar int32_t ret; 10622038315SVarun Wadekar 10722038315SVarun Wadekar /* 10822038315SVarun Wadekar * Get information about the Secure Payload (BL32) image. Its 10922038315SVarun Wadekar * absence is a critical failure. 11022038315SVarun Wadekar */ 11122038315SVarun Wadekar tlk_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 11222038315SVarun Wadekar if (!tlk_ep_info) { 11322038315SVarun Wadekar WARN("No SP provided. Booting device without SP" 11422038315SVarun Wadekar " initialization. SMC`s destined for SP" 11522038315SVarun Wadekar " will return SMC_UNK\n"); 11622038315SVarun Wadekar return 1; 11722038315SVarun Wadekar } 11822038315SVarun Wadekar 11922038315SVarun Wadekar /* 12022038315SVarun Wadekar * If there's no valid entry point for SP, we return a non-zero value 12122038315SVarun Wadekar * signalling failure initializing the service. We bail out without 12222038315SVarun Wadekar * registering any handlers 12322038315SVarun Wadekar */ 12422038315SVarun Wadekar if (!tlk_ep_info->pc) 12522038315SVarun Wadekar return 1; 12622038315SVarun Wadekar 12722038315SVarun Wadekar /* 12822038315SVarun Wadekar * Inspect the SP image's SPSR and determine it's execution state 12922038315SVarun Wadekar * i.e whether AArch32 or AArch64. 13022038315SVarun Wadekar */ 13122038315SVarun Wadekar tlkd_init_tlk_ep_state(tlk_ep_info, 13222038315SVarun Wadekar (tlk_ep_info->spsr >> MODE_RW_SHIFT) & MODE_RW_MASK, 13322038315SVarun Wadekar tlk_ep_info->pc, 13422038315SVarun Wadekar &tlk_ctx); 13522038315SVarun Wadekar 136*d205cda6SVarun Wadekar /* get a list of all S-EL1 IRQs from the platform */ 137*d205cda6SVarun Wadekar 138*d205cda6SVarun Wadekar /* register interrupt handler */ 139*d205cda6SVarun Wadekar flags = 0; 140*d205cda6SVarun Wadekar set_interrupt_rm_flag(flags, NON_SECURE); 141*d205cda6SVarun Wadekar ret = register_interrupt_type_handler(INTR_TYPE_S_EL1, 142*d205cda6SVarun Wadekar tlkd_interrupt_handler, 143*d205cda6SVarun Wadekar flags); 144*d205cda6SVarun Wadekar if (ret != 0) { 145*d205cda6SVarun Wadekar ERROR("failed to register tlkd interrupt handler (%d)\n", ret); 146*d205cda6SVarun Wadekar } 147*d205cda6SVarun Wadekar 14822038315SVarun Wadekar /* 14922038315SVarun Wadekar * All TLK SPD initialization done. Now register our init function 15022038315SVarun Wadekar * with BL31 for deferred invocation 15122038315SVarun Wadekar */ 15222038315SVarun Wadekar bl31_register_bl32_init(&tlkd_init); 15322038315SVarun Wadekar 15422038315SVarun Wadekar return 0; 15522038315SVarun Wadekar } 15622038315SVarun Wadekar 15722038315SVarun Wadekar /******************************************************************************* 15822038315SVarun Wadekar * This function passes control to the Secure Payload image (BL32) for the first 15922038315SVarun Wadekar * time on the primary cpu after a cold boot. It assumes that a valid secure 16022038315SVarun Wadekar * context has already been created by tlkd_setup() which can be directly 16122038315SVarun Wadekar * used. This function performs a synchronous entry into the Secure payload. 16222038315SVarun Wadekar * The SP passes control back to this routine through a SMC. 16322038315SVarun Wadekar ******************************************************************************/ 164724fd958SMasahiro Yamada static int32_t tlkd_init(void) 16522038315SVarun Wadekar { 16622038315SVarun Wadekar entry_point_info_t *tlk_entry_point; 16722038315SVarun Wadekar 16822038315SVarun Wadekar /* 16922038315SVarun Wadekar * Get information about the Secure Payload (BL32) image. Its 17022038315SVarun Wadekar * absence is a critical failure. 17122038315SVarun Wadekar */ 17222038315SVarun Wadekar tlk_entry_point = bl31_plat_get_next_image_ep_info(SECURE); 17322038315SVarun Wadekar assert(tlk_entry_point); 17422038315SVarun Wadekar 175fd650ff6SSoby Mathew cm_init_my_context(tlk_entry_point); 17622038315SVarun Wadekar 17722038315SVarun Wadekar /* 17826670c82SVarun Wadekar * TLK runs only on a single CPU. Store the value of the boot 17926670c82SVarun Wadekar * CPU for sanity checking later. 18026670c82SVarun Wadekar */ 18126670c82SVarun Wadekar boot_cpu = plat_my_core_pos(); 18226670c82SVarun Wadekar 18326670c82SVarun Wadekar /* 18422038315SVarun Wadekar * Arrange for an entry into the test secure payload. 18522038315SVarun Wadekar */ 18622038315SVarun Wadekar return tlkd_synchronous_sp_entry(&tlk_ctx); 18722038315SVarun Wadekar } 18822038315SVarun Wadekar 18922038315SVarun Wadekar /******************************************************************************* 19022038315SVarun Wadekar * This function is responsible for handling all SMCs in the Trusted OS/App 19122038315SVarun Wadekar * range from the non-secure state as defined in the SMC Calling Convention 19222038315SVarun Wadekar * Document. It is also responsible for communicating with the Secure payload 19322038315SVarun Wadekar * to delegate work and return results back to the non-secure state. Lastly it 19422038315SVarun Wadekar * will also return any information that the secure payload needs to do the 19522038315SVarun Wadekar * work assigned to it. 19622038315SVarun Wadekar ******************************************************************************/ 19757d1e5faSMasahiro Yamada static uintptr_t tlkd_smc_handler(uint32_t smc_fid, 19857d1e5faSMasahiro Yamada u_register_t x1, 19957d1e5faSMasahiro Yamada u_register_t x2, 20057d1e5faSMasahiro Yamada u_register_t x3, 20157d1e5faSMasahiro Yamada u_register_t x4, 20222038315SVarun Wadekar void *cookie, 20322038315SVarun Wadekar void *handle, 20457d1e5faSMasahiro Yamada u_register_t flags) 20522038315SVarun Wadekar { 20677199df7SVarun Wadekar cpu_context_t *ns_cpu_context; 207709a3c47SVarun Wadekar gp_regs_t *gp_regs; 20822038315SVarun Wadekar uint32_t ns; 209709a3c47SVarun Wadekar uint64_t par; 21022038315SVarun Wadekar 21122038315SVarun Wadekar /* Passing a NULL context is a critical programming error */ 21222038315SVarun Wadekar assert(handle); 21322038315SVarun Wadekar 21426670c82SVarun Wadekar /* These SMCs are only supported by a single CPU */ 21526670c82SVarun Wadekar if (boot_cpu != plat_my_core_pos()) 2166693962cSVarun Wadekar SMC_RET1(handle, SMC_UNK); 2176693962cSVarun Wadekar 21822038315SVarun Wadekar /* Determine which security state this SMC originated from */ 21922038315SVarun Wadekar ns = is_caller_non_secure(flags); 22022038315SVarun Wadekar 22122038315SVarun Wadekar switch (smc_fid) { 22222038315SVarun Wadekar 22322038315SVarun Wadekar /* 224f9d25054SVarun Wadekar * This function ID is used by SP to indicate that it was 225f9d25054SVarun Wadekar * preempted by a non-secure world IRQ. 226f9d25054SVarun Wadekar */ 227f9d25054SVarun Wadekar case TLK_PREEMPTED: 228f9d25054SVarun Wadekar 229f9d25054SVarun Wadekar if (ns) 230f9d25054SVarun Wadekar SMC_RET1(handle, SMC_UNK); 231f9d25054SVarun Wadekar 232f9d25054SVarun Wadekar assert(handle == cm_get_context(SECURE)); 233f9d25054SVarun Wadekar cm_el1_sysregs_context_save(SECURE); 234f9d25054SVarun Wadekar 235f9d25054SVarun Wadekar /* Get a reference to the non-secure context */ 236f9d25054SVarun Wadekar ns_cpu_context = cm_get_context(NON_SECURE); 237f9d25054SVarun Wadekar assert(ns_cpu_context); 238f9d25054SVarun Wadekar 239f9d25054SVarun Wadekar /* 240f9d25054SVarun Wadekar * Restore non-secure state. There is no need to save the 241f9d25054SVarun Wadekar * secure system register context since the SP was supposed 242f9d25054SVarun Wadekar * to preserve it during S-EL1 interrupt handling. 243f9d25054SVarun Wadekar */ 244f9d25054SVarun Wadekar cm_el1_sysregs_context_restore(NON_SECURE); 245f9d25054SVarun Wadekar cm_set_next_eret_context(NON_SECURE); 246f9d25054SVarun Wadekar 247709a3c47SVarun Wadekar SMC_RET1(ns_cpu_context, x1); 248f9d25054SVarun Wadekar 249f9d25054SVarun Wadekar /* 25077199df7SVarun Wadekar * This is a request from the non-secure context to: 25177199df7SVarun Wadekar * 25277199df7SVarun Wadekar * a. register shared memory with the SP for storing it's 25377199df7SVarun Wadekar * activity logs. 25477199df7SVarun Wadekar * b. register shared memory with the SP for passing args 25577199df7SVarun Wadekar * required for maintaining sessions with the Trusted 25677199df7SVarun Wadekar * Applications. 2577bc05f52SMihir Joshi * c. register shared persistent buffers for secure storage 2587bc05f52SMihir Joshi * d. register NS DRAM ranges passed by Cboot 2597bc05f52SMihir Joshi * e. register Root of Trust parameters from Cboot for Verified Boot 2607bc05f52SMihir Joshi * f. open/close sessions 2617bc05f52SMihir Joshi * g. issue commands to the Trusted Apps 2627bc05f52SMihir Joshi * h. resume the preempted yielding SMC call. 26377199df7SVarun Wadekar */ 26477199df7SVarun Wadekar case TLK_REGISTER_LOGBUF: 26577199df7SVarun Wadekar case TLK_REGISTER_REQBUF: 2667bc05f52SMihir Joshi case TLK_SS_REGISTER_HANDLER: 2677bc05f52SMihir Joshi case TLK_REGISTER_NS_DRAM_RANGES: 2687bc05f52SMihir Joshi case TLK_SET_ROOT_OF_TRUST: 2696693962cSVarun Wadekar case TLK_OPEN_TA_SESSION: 2706693962cSVarun Wadekar case TLK_CLOSE_TA_SESSION: 2716693962cSVarun Wadekar case TLK_TA_LAUNCH_OP: 2726693962cSVarun Wadekar case TLK_TA_SEND_EVENT: 273ca15d9bcSVarun Wadekar case TLK_RESUME_FID: 2746693962cSVarun Wadekar 275709a3c47SVarun Wadekar if (!ns) 27677199df7SVarun Wadekar SMC_RET1(handle, SMC_UNK); 27777199df7SVarun Wadekar 27877199df7SVarun Wadekar /* 27977199df7SVarun Wadekar * This is a fresh request from the non-secure client. 28077199df7SVarun Wadekar * The parameters are in x1 and x2. Figure out which 28177199df7SVarun Wadekar * registers need to be preserved, save the non-secure 28277199df7SVarun Wadekar * state and send the request to the secure payload. 28377199df7SVarun Wadekar */ 28477199df7SVarun Wadekar assert(handle == cm_get_context(NON_SECURE)); 28577199df7SVarun Wadekar 286ca15d9bcSVarun Wadekar /* 287bbbbcdaeSDavid Cunado * Check if we are already processing a yielding SMC 288ca15d9bcSVarun Wadekar * call. Of all the supported fids, only the "resume" 289ca15d9bcSVarun Wadekar * fid expects the flag to be set. 290ca15d9bcSVarun Wadekar */ 291ca15d9bcSVarun Wadekar if (smc_fid == TLK_RESUME_FID) { 292bbbbcdaeSDavid Cunado if (!get_yield_smc_active_flag(tlk_ctx.state)) 293ca15d9bcSVarun Wadekar SMC_RET1(handle, SMC_UNK); 294ca15d9bcSVarun Wadekar } else { 295bbbbcdaeSDavid Cunado if (get_yield_smc_active_flag(tlk_ctx.state)) 29677199df7SVarun Wadekar SMC_RET1(handle, SMC_UNK); 297ca15d9bcSVarun Wadekar } 29877199df7SVarun Wadekar 29977199df7SVarun Wadekar cm_el1_sysregs_context_save(NON_SECURE); 30077199df7SVarun Wadekar 30177199df7SVarun Wadekar /* 30277199df7SVarun Wadekar * Verify if there is a valid context to use. 30377199df7SVarun Wadekar */ 30477199df7SVarun Wadekar assert(&tlk_ctx.cpu_ctx == cm_get_context(SECURE)); 30577199df7SVarun Wadekar 30677199df7SVarun Wadekar /* 30777199df7SVarun Wadekar * Mark the SP state as active. 30877199df7SVarun Wadekar */ 309bbbbcdaeSDavid Cunado set_yield_smc_active_flag(tlk_ctx.state); 31077199df7SVarun Wadekar 31177199df7SVarun Wadekar /* 31277199df7SVarun Wadekar * We are done stashing the non-secure context. Ask the 31377199df7SVarun Wadekar * secure payload to do the work now. 31477199df7SVarun Wadekar */ 31577199df7SVarun Wadekar cm_el1_sysregs_context_restore(SECURE); 31677199df7SVarun Wadekar cm_set_next_eret_context(SECURE); 31777199df7SVarun Wadekar 31877199df7SVarun Wadekar /* 319709a3c47SVarun Wadekar * TLK is a 32-bit Trusted OS and so expects the SMC 320709a3c47SVarun Wadekar * arguments via r0-r7. TLK expects the monitor frame 321709a3c47SVarun Wadekar * registers to be 64-bits long. Hence, we pass x0 in 322709a3c47SVarun Wadekar * r0-r1, x1 in r2-r3, x3 in r4-r5 and x4 in r6-r7. 323709a3c47SVarun Wadekar * 324709a3c47SVarun Wadekar * As smc_fid is a uint32 value, r1 contains 0. 325709a3c47SVarun Wadekar */ 326709a3c47SVarun Wadekar gp_regs = get_gpregs_ctx(&tlk_ctx.cpu_ctx); 327709a3c47SVarun Wadekar write_ctx_reg(gp_regs, CTX_GPREG_X4, (uint32_t)x2); 328709a3c47SVarun Wadekar write_ctx_reg(gp_regs, CTX_GPREG_X5, (uint32_t)(x2 >> 32)); 329709a3c47SVarun Wadekar write_ctx_reg(gp_regs, CTX_GPREG_X6, (uint32_t)x3); 330709a3c47SVarun Wadekar write_ctx_reg(gp_regs, CTX_GPREG_X7, (uint32_t)(x3 >> 32)); 331709a3c47SVarun Wadekar SMC_RET4(&tlk_ctx.cpu_ctx, smc_fid, 0, (uint32_t)x1, 332709a3c47SVarun Wadekar (uint32_t)(x1 >> 32)); 333709a3c47SVarun Wadekar 334709a3c47SVarun Wadekar /* 335709a3c47SVarun Wadekar * Translate NS/EL1-S virtual addresses. 336709a3c47SVarun Wadekar * 337709a3c47SVarun Wadekar * x1 = virtual address 338709a3c47SVarun Wadekar * x3 = type (NS/S) 339709a3c47SVarun Wadekar * 340709a3c47SVarun Wadekar * Returns PA:lo in r0, PA:hi in r1. 3416e159e7aSVarun Wadekar */ 3426e159e7aSVarun Wadekar case TLK_VA_TRANSLATE: 343709a3c47SVarun Wadekar 344709a3c47SVarun Wadekar /* Should be invoked only by secure world */ 345709a3c47SVarun Wadekar if (ns) 3466e159e7aSVarun Wadekar SMC_RET1(handle, SMC_UNK); 3476e159e7aSVarun Wadekar 348709a3c47SVarun Wadekar /* NS virtual addresses are 64-bit long */ 349709a3c47SVarun Wadekar if (x3 & TLK_TRANSLATE_NS_VADDR) 350709a3c47SVarun Wadekar x1 = (uint32_t)x1 | (x2 << 32); 351709a3c47SVarun Wadekar 352709a3c47SVarun Wadekar if (!x1) 353709a3c47SVarun Wadekar SMC_RET1(handle, SMC_UNK); 354709a3c47SVarun Wadekar 355709a3c47SVarun Wadekar /* 356709a3c47SVarun Wadekar * TODO: Sanity check x1. This would require platform 357709a3c47SVarun Wadekar * support. 358709a3c47SVarun Wadekar */ 359709a3c47SVarun Wadekar 3606e159e7aSVarun Wadekar /* virtual address and type: ns/s */ 361709a3c47SVarun Wadekar par = tlkd_va_translate(x1, x3); 3626e159e7aSVarun Wadekar 363709a3c47SVarun Wadekar /* return physical address in r0-r1 */ 364709a3c47SVarun Wadekar SMC_RET4(handle, (uint32_t)par, (uint32_t)(par >> 32), 0, 0); 3656e159e7aSVarun Wadekar 3666e159e7aSVarun Wadekar /* 36777199df7SVarun Wadekar * This is a request from the SP to mark completion of 368bbbbcdaeSDavid Cunado * a yielding function ID. 36977199df7SVarun Wadekar */ 37077199df7SVarun Wadekar case TLK_REQUEST_DONE: 371709a3c47SVarun Wadekar if (ns) 37277199df7SVarun Wadekar SMC_RET1(handle, SMC_UNK); 37377199df7SVarun Wadekar 37477199df7SVarun Wadekar /* 37577199df7SVarun Wadekar * Mark the SP state as inactive. 37677199df7SVarun Wadekar */ 377bbbbcdaeSDavid Cunado clr_yield_smc_active_flag(tlk_ctx.state); 37877199df7SVarun Wadekar 37977199df7SVarun Wadekar /* Get a reference to the non-secure context */ 38077199df7SVarun Wadekar ns_cpu_context = cm_get_context(NON_SECURE); 38177199df7SVarun Wadekar assert(ns_cpu_context); 38277199df7SVarun Wadekar 38377199df7SVarun Wadekar /* 38477199df7SVarun Wadekar * This is a request completion SMC and we must switch to 38577199df7SVarun Wadekar * the non-secure world to pass the result. 38677199df7SVarun Wadekar */ 38777199df7SVarun Wadekar cm_el1_sysregs_context_save(SECURE); 38877199df7SVarun Wadekar 38977199df7SVarun Wadekar /* 39077199df7SVarun Wadekar * We are done stashing the secure context. Switch to the 39177199df7SVarun Wadekar * non-secure context and return the result. 39277199df7SVarun Wadekar */ 39377199df7SVarun Wadekar cm_el1_sysregs_context_restore(NON_SECURE); 39477199df7SVarun Wadekar cm_set_next_eret_context(NON_SECURE); 395709a3c47SVarun Wadekar SMC_RET1(ns_cpu_context, x1); 39677199df7SVarun Wadekar 39777199df7SVarun Wadekar /* 39822038315SVarun Wadekar * This function ID is used only by the SP to indicate it has 39922038315SVarun Wadekar * finished initialising itself after a cold boot 40022038315SVarun Wadekar */ 40122038315SVarun Wadekar case TLK_ENTRY_DONE: 402709a3c47SVarun Wadekar if (ns) 40322038315SVarun Wadekar SMC_RET1(handle, SMC_UNK); 40422038315SVarun Wadekar 40522038315SVarun Wadekar /* 40622038315SVarun Wadekar * SP has been successfully initialized. Register power 4078aabea33SPaul Beesley * management hooks with PSCI 40822038315SVarun Wadekar */ 40922038315SVarun Wadekar psci_register_spd_pm_hook(&tlkd_pm_ops); 41022038315SVarun Wadekar 41122038315SVarun Wadekar /* 41222038315SVarun Wadekar * TLK reports completion. The SPD must have initiated 41322038315SVarun Wadekar * the original request through a synchronous entry 41422038315SVarun Wadekar * into the SP. Jump back to the original C runtime 41522038315SVarun Wadekar * context. 41622038315SVarun Wadekar */ 417709a3c47SVarun Wadekar tlkd_synchronous_sp_exit(&tlk_ctx, x1); 418185a23ffSJonathan Wright break; 41922038315SVarun Wadekar 42022038315SVarun Wadekar /* 421cb790c5eSVarun Wadekar * These function IDs are used only by TLK to indicate it has 422cb790c5eSVarun Wadekar * finished: 423cb790c5eSVarun Wadekar * 1. suspending itself after an earlier psci cpu_suspend 424cb790c5eSVarun Wadekar * request. 425cb790c5eSVarun Wadekar * 2. resuming itself after an earlier psci cpu_suspend 426cb790c5eSVarun Wadekar * request. 427cb790c5eSVarun Wadekar * 3. powering down after an earlier psci system_off/system_reset 428cb790c5eSVarun Wadekar * request. 429cb790c5eSVarun Wadekar */ 430cb790c5eSVarun Wadekar case TLK_SUSPEND_DONE: 431cb790c5eSVarun Wadekar case TLK_RESUME_DONE: 432cb790c5eSVarun Wadekar case TLK_SYSTEM_OFF_DONE: 433cb790c5eSVarun Wadekar 434cb790c5eSVarun Wadekar if (ns) 435cb790c5eSVarun Wadekar SMC_RET1(handle, SMC_UNK); 436cb790c5eSVarun Wadekar 437cb790c5eSVarun Wadekar /* 438cb790c5eSVarun Wadekar * TLK reports completion. TLKD must have initiated the 439cb790c5eSVarun Wadekar * original request through a synchronous entry into the SP. 440cb790c5eSVarun Wadekar * Jump back to the original C runtime context, and pass x1 as 441cb790c5eSVarun Wadekar * return value to the caller 442cb790c5eSVarun Wadekar */ 443cb790c5eSVarun Wadekar tlkd_synchronous_sp_exit(&tlk_ctx, x1); 444185a23ffSJonathan Wright break; 445cb790c5eSVarun Wadekar 446cb790c5eSVarun Wadekar /* 447*d205cda6SVarun Wadekar * This function ID is used by SP to indicate that it has completed 448*d205cda6SVarun Wadekar * handling the secure interrupt. 449*d205cda6SVarun Wadekar */ 450*d205cda6SVarun Wadekar case TLK_IRQ_DONE: 451*d205cda6SVarun Wadekar 452*d205cda6SVarun Wadekar if (ns) 453*d205cda6SVarun Wadekar SMC_RET1(handle, SMC_UNK); 454*d205cda6SVarun Wadekar 455*d205cda6SVarun Wadekar assert(handle == cm_get_context(SECURE)); 456*d205cda6SVarun Wadekar 457*d205cda6SVarun Wadekar /* save secure world context */ 458*d205cda6SVarun Wadekar cm_el1_sysregs_context_save(SECURE); 459*d205cda6SVarun Wadekar 460*d205cda6SVarun Wadekar /* Get a reference to the non-secure context */ 461*d205cda6SVarun Wadekar ns_cpu_context = cm_get_context(NON_SECURE); 462*d205cda6SVarun Wadekar assert(ns_cpu_context); 463*d205cda6SVarun Wadekar 464*d205cda6SVarun Wadekar /* 465*d205cda6SVarun Wadekar * Restore non-secure state. There is no need to save the 466*d205cda6SVarun Wadekar * secure system register context since the SP was supposed 467*d205cda6SVarun Wadekar * to preserve it during S-EL1 interrupt handling. 468*d205cda6SVarun Wadekar */ 469*d205cda6SVarun Wadekar cm_el1_sysregs_context_restore(NON_SECURE); 470*d205cda6SVarun Wadekar cm_set_next_eret_context(NON_SECURE); 471*d205cda6SVarun Wadekar 472*d205cda6SVarun Wadekar SMC_RET0(ns_cpu_context); 473*d205cda6SVarun Wadekar 474*d205cda6SVarun Wadekar /* 47522038315SVarun Wadekar * Return the number of service function IDs implemented to 47622038315SVarun Wadekar * provide service to non-secure 47722038315SVarun Wadekar */ 47822038315SVarun Wadekar case TOS_CALL_COUNT: 47922038315SVarun Wadekar SMC_RET1(handle, TLK_NUM_FID); 48022038315SVarun Wadekar 48122038315SVarun Wadekar /* 48222038315SVarun Wadekar * Return TLK's UID to the caller 48322038315SVarun Wadekar */ 48422038315SVarun Wadekar case TOS_UID: 48522038315SVarun Wadekar SMC_UUID_RET(handle, tlk_uuid); 48622038315SVarun Wadekar 48722038315SVarun Wadekar /* 48822038315SVarun Wadekar * Return the version of current implementation 48922038315SVarun Wadekar */ 49022038315SVarun Wadekar case TOS_CALL_VERSION: 49122038315SVarun Wadekar SMC_RET2(handle, TLK_VERSION_MAJOR, TLK_VERSION_MINOR); 49222038315SVarun Wadekar 49322038315SVarun Wadekar default: 4947bc05f52SMihir Joshi WARN("%s: Unhandled SMC: 0x%x\n", __func__, smc_fid); 49522038315SVarun Wadekar break; 49622038315SVarun Wadekar } 49722038315SVarun Wadekar 49822038315SVarun Wadekar SMC_RET1(handle, SMC_UNK); 49922038315SVarun Wadekar } 50022038315SVarun Wadekar 50122038315SVarun Wadekar /* Define a SPD runtime service descriptor for fast SMC calls */ 50222038315SVarun Wadekar DECLARE_RT_SVC( 50322038315SVarun Wadekar tlkd_tos_fast, 50422038315SVarun Wadekar 50522038315SVarun Wadekar OEN_TOS_START, 50622038315SVarun Wadekar OEN_TOS_END, 50722038315SVarun Wadekar SMC_TYPE_FAST, 50822038315SVarun Wadekar tlkd_setup, 50922038315SVarun Wadekar tlkd_smc_handler 51022038315SVarun Wadekar ); 51122038315SVarun Wadekar 512bbbbcdaeSDavid Cunado /* Define a SPD runtime service descriptor for yielding SMC calls */ 51322038315SVarun Wadekar DECLARE_RT_SVC( 51422038315SVarun Wadekar tlkd_tos_std, 51522038315SVarun Wadekar 51622038315SVarun Wadekar OEN_TOS_START, 51722038315SVarun Wadekar OEN_TOS_END, 518bbbbcdaeSDavid Cunado SMC_TYPE_YIELD, 51922038315SVarun Wadekar NULL, 52022038315SVarun Wadekar tlkd_smc_handler 52122038315SVarun Wadekar ); 5226693962cSVarun Wadekar 5236693962cSVarun Wadekar /* Define a SPD runtime service descriptor for fast SMC calls */ 5246693962cSVarun Wadekar DECLARE_RT_SVC( 5256693962cSVarun Wadekar tlkd_tap_fast, 5266693962cSVarun Wadekar 5276693962cSVarun Wadekar OEN_TAP_START, 5286693962cSVarun Wadekar OEN_TAP_END, 5296693962cSVarun Wadekar SMC_TYPE_FAST, 5306693962cSVarun Wadekar NULL, 5316693962cSVarun Wadekar tlkd_smc_handler 5326693962cSVarun Wadekar ); 5336693962cSVarun Wadekar 534bbbbcdaeSDavid Cunado /* Define a SPD runtime service descriptor for yielding SMC calls */ 5356693962cSVarun Wadekar DECLARE_RT_SVC( 5366693962cSVarun Wadekar tlkd_tap_std, 5376693962cSVarun Wadekar 5386693962cSVarun Wadekar OEN_TAP_START, 5396693962cSVarun Wadekar OEN_TAP_END, 540bbbbcdaeSDavid Cunado SMC_TYPE_YIELD, 5416693962cSVarun Wadekar NULL, 5426693962cSVarun Wadekar tlkd_smc_handler 5436693962cSVarun Wadekar ); 544