Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 1 | /* |
Varun Wadekar | 079e20e | 2018-08-10 09:55:25 -0700 | [diff] [blame] | 2 | * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved. |
Varun Wadekar | f09264a | 2018-11-05 15:12:55 -0800 | [diff] [blame] | 3 | * Copyright (c) 2020, NVIDIA Corporation. All rights reserved. |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 4 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 5 | * SPDX-License-Identifier: BSD-3-Clause |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /******************************************************************************* |
| 9 | * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a |
| 10 | * plug-in component to the Secure Monitor, registered as a runtime service. The |
| 11 | * SPD is expected to be a functional extension of the Secure Payload (SP) that |
| 12 | * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting |
| 13 | * the Trusted OS/Applications range to the dispatcher. The SPD will either |
| 14 | * handle the request locally or delegate it to the Secure Payload. It is also |
| 15 | * responsible for initialising and maintaining communication with the SP. |
| 16 | ******************************************************************************/ |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 17 | #include <assert.h> |
Varun Wadekar | 079e20e | 2018-08-10 09:55:25 -0700 | [diff] [blame] | 18 | #include <bl31/interrupt_mgmt.h> |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 19 | #include <errno.h> |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 20 | #include <stddef.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 21 | |
Antonio Nino Diaz | 31a157f | 2019-02-11 11:57:57 +0000 | [diff] [blame] | 22 | #include <arch_helpers.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 23 | #include <bl31/bl31.h> |
Antonio Nino Diaz | 31a157f | 2019-02-11 11:57:57 +0000 | [diff] [blame] | 24 | #include <bl32/payloads/tlk.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 25 | #include <common/bl_common.h> |
| 26 | #include <common/debug.h> |
| 27 | #include <common/runtime_svc.h> |
| 28 | #include <lib/el3_runtime/context_mgmt.h> |
| 29 | #include <plat/common/platform.h> |
| 30 | #include <tools_share/uuid.h> |
| 31 | |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 32 | #include "tlkd_private.h" |
| 33 | |
| 34 | extern const spd_pm_ops_t tlkd_pm_ops; |
| 35 | |
| 36 | /******************************************************************************* |
Varun Wadekar | a70dec3 | 2015-08-26 12:49:03 +0530 | [diff] [blame] | 37 | * Per-cpu Secure Payload state |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 38 | ******************************************************************************/ |
Varun Wadekar | a70dec3 | 2015-08-26 12:49:03 +0530 | [diff] [blame] | 39 | tlk_context_t tlk_ctx; |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 40 | |
Varun Wadekar | 0bbe8a6 | 2016-06-07 21:21:59 -0700 | [diff] [blame] | 41 | /******************************************************************************* |
| 42 | * CPU number on which TLK booted up |
| 43 | ******************************************************************************/ |
Varun Wadekar | 66231d1 | 2017-06-07 09:57:42 -0700 | [diff] [blame] | 44 | static uint32_t boot_cpu; |
Varun Wadekar | 0bbe8a6 | 2016-06-07 21:21:59 -0700 | [diff] [blame] | 45 | |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 46 | /* TLK UID: RFC-4122 compliant UUID (version-5, sha-1) */ |
Roberto Vargas | eace8f1 | 2018-04-26 13:36:53 +0100 | [diff] [blame] | 47 | DEFINE_SVC_UUID2(tlk_uuid, |
| 48 | 0xc9e911bd, 0xba2b, 0xee52, 0xb1, 0x72, |
| 49 | 0x46, 0x1f, 0xba, 0x97, 0x7f, 0x63); |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 50 | |
Masahiro Yamada | 5621275 | 2018-04-19 01:14:42 +0900 | [diff] [blame] | 51 | static int32_t tlkd_init(void); |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 52 | |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 53 | /******************************************************************************* |
Varun Wadekar | 079e20e | 2018-08-10 09:55:25 -0700 | [diff] [blame] | 54 | * Secure Payload Dispatcher's timer interrupt handler |
| 55 | ******************************************************************************/ |
| 56 | static uint64_t tlkd_interrupt_handler(uint32_t id, |
| 57 | uint32_t flags, |
| 58 | void *handle, |
| 59 | void *cookie) |
| 60 | { |
| 61 | cpu_context_t *s_cpu_context; |
| 62 | int irq = plat_ic_get_pending_interrupt_id(); |
| 63 | |
| 64 | /* acknowledge the interrupt and mark it complete */ |
| 65 | (void)plat_ic_acknowledge_interrupt(); |
| 66 | plat_ic_end_of_interrupt(irq); |
| 67 | |
| 68 | /* |
| 69 | * Disable the routing of NS interrupts from secure world to |
| 70 | * EL3 while interrupted on this core. |
| 71 | */ |
| 72 | disable_intr_rm_local(INTR_TYPE_S_EL1, SECURE); |
| 73 | |
| 74 | /* Check the security state when the exception was generated */ |
| 75 | assert(get_interrupt_src_ss(flags) == NON_SECURE); |
| 76 | assert(handle == cm_get_context(NON_SECURE)); |
| 77 | |
| 78 | /* Save non-secure state */ |
| 79 | cm_el1_sysregs_context_save(NON_SECURE); |
| 80 | |
| 81 | /* Get a reference to the secure context */ |
| 82 | s_cpu_context = cm_get_context(SECURE); |
| 83 | assert(s_cpu_context); |
| 84 | |
| 85 | /* |
| 86 | * Restore non-secure state. There is no need to save the |
| 87 | * secure system register context since the SP was supposed |
| 88 | * to preserve it during S-EL1 interrupt handling. |
| 89 | */ |
| 90 | cm_el1_sysregs_context_restore(SECURE); |
| 91 | cm_set_next_eret_context(SECURE); |
| 92 | |
| 93 | /* Provide the IRQ number to the SPD */ |
| 94 | SMC_RET4(s_cpu_context, (uint32_t)TLK_IRQ_FIRED, 0, (uint32_t)irq, 0); |
| 95 | } |
| 96 | |
| 97 | /******************************************************************************* |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 98 | * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type |
| 99 | * (aarch32/aarch64) if not already known and initialises the context for entry |
| 100 | * into the SP for its initialisation. |
| 101 | ******************************************************************************/ |
Masahiro Yamada | 5621275 | 2018-04-19 01:14:42 +0900 | [diff] [blame] | 102 | static int32_t tlkd_setup(void) |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 103 | { |
| 104 | entry_point_info_t *tlk_ep_info; |
Varun Wadekar | 079e20e | 2018-08-10 09:55:25 -0700 | [diff] [blame] | 105 | uint32_t flags; |
| 106 | int32_t ret; |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * Get information about the Secure Payload (BL32) image. Its |
| 110 | * absence is a critical failure. |
| 111 | */ |
| 112 | tlk_ep_info = bl31_plat_get_next_image_ep_info(SECURE); |
| 113 | if (!tlk_ep_info) { |
| 114 | WARN("No SP provided. Booting device without SP" |
| 115 | " initialization. SMC`s destined for SP" |
| 116 | " will return SMC_UNK\n"); |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * If there's no valid entry point for SP, we return a non-zero value |
| 122 | * signalling failure initializing the service. We bail out without |
| 123 | * registering any handlers |
| 124 | */ |
| 125 | if (!tlk_ep_info->pc) |
| 126 | return 1; |
| 127 | |
| 128 | /* |
| 129 | * Inspect the SP image's SPSR and determine it's execution state |
| 130 | * i.e whether AArch32 or AArch64. |
| 131 | */ |
| 132 | tlkd_init_tlk_ep_state(tlk_ep_info, |
| 133 | (tlk_ep_info->spsr >> MODE_RW_SHIFT) & MODE_RW_MASK, |
| 134 | tlk_ep_info->pc, |
| 135 | &tlk_ctx); |
| 136 | |
Varun Wadekar | 079e20e | 2018-08-10 09:55:25 -0700 | [diff] [blame] | 137 | /* get a list of all S-EL1 IRQs from the platform */ |
| 138 | |
| 139 | /* register interrupt handler */ |
| 140 | flags = 0; |
| 141 | set_interrupt_rm_flag(flags, NON_SECURE); |
| 142 | ret = register_interrupt_type_handler(INTR_TYPE_S_EL1, |
| 143 | tlkd_interrupt_handler, |
| 144 | flags); |
| 145 | if (ret != 0) { |
| 146 | ERROR("failed to register tlkd interrupt handler (%d)\n", ret); |
| 147 | } |
| 148 | |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 149 | /* |
| 150 | * All TLK SPD initialization done. Now register our init function |
| 151 | * with BL31 for deferred invocation |
| 152 | */ |
| 153 | bl31_register_bl32_init(&tlkd_init); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /******************************************************************************* |
| 159 | * This function passes control to the Secure Payload image (BL32) for the first |
| 160 | * time on the primary cpu after a cold boot. It assumes that a valid secure |
| 161 | * context has already been created by tlkd_setup() which can be directly |
| 162 | * used. This function performs a synchronous entry into the Secure payload. |
| 163 | * The SP passes control back to this routine through a SMC. |
| 164 | ******************************************************************************/ |
Masahiro Yamada | 5621275 | 2018-04-19 01:14:42 +0900 | [diff] [blame] | 165 | static int32_t tlkd_init(void) |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 166 | { |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 167 | entry_point_info_t *tlk_entry_point; |
| 168 | |
| 169 | /* |
| 170 | * Get information about the Secure Payload (BL32) image. Its |
| 171 | * absence is a critical failure. |
| 172 | */ |
| 173 | tlk_entry_point = bl31_plat_get_next_image_ep_info(SECURE); |
| 174 | assert(tlk_entry_point); |
| 175 | |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 176 | cm_init_my_context(tlk_entry_point); |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 177 | |
| 178 | /* |
Varun Wadekar | 0bbe8a6 | 2016-06-07 21:21:59 -0700 | [diff] [blame] | 179 | * TLK runs only on a single CPU. Store the value of the boot |
| 180 | * CPU for sanity checking later. |
| 181 | */ |
| 182 | boot_cpu = plat_my_core_pos(); |
| 183 | |
| 184 | /* |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 185 | * Arrange for an entry into the test secure payload. |
| 186 | */ |
| 187 | return tlkd_synchronous_sp_entry(&tlk_ctx); |
| 188 | } |
| 189 | |
| 190 | /******************************************************************************* |
| 191 | * This function is responsible for handling all SMCs in the Trusted OS/App |
| 192 | * range from the non-secure state as defined in the SMC Calling Convention |
| 193 | * Document. It is also responsible for communicating with the Secure payload |
| 194 | * to delegate work and return results back to the non-secure state. Lastly it |
| 195 | * will also return any information that the secure payload needs to do the |
| 196 | * work assigned to it. |
| 197 | ******************************************************************************/ |
Masahiro Yamada | 5ac9d96 | 2018-04-19 01:18:48 +0900 | [diff] [blame] | 198 | static uintptr_t tlkd_smc_handler(uint32_t smc_fid, |
| 199 | u_register_t x1, |
| 200 | u_register_t x2, |
| 201 | u_register_t x3, |
| 202 | u_register_t x4, |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 203 | void *cookie, |
| 204 | void *handle, |
Masahiro Yamada | 5ac9d96 | 2018-04-19 01:18:48 +0900 | [diff] [blame] | 205 | u_register_t flags) |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 206 | { |
Varun Wadekar | a97535f | 2015-03-13 14:19:11 +0530 | [diff] [blame] | 207 | cpu_context_t *ns_cpu_context; |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 208 | gp_regs_t *gp_regs; |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 209 | uint32_t ns; |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 210 | uint64_t par; |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 211 | |
| 212 | /* Passing a NULL context is a critical programming error */ |
| 213 | assert(handle); |
| 214 | |
Varun Wadekar | 0bbe8a6 | 2016-06-07 21:21:59 -0700 | [diff] [blame] | 215 | /* These SMCs are only supported by a single CPU */ |
| 216 | if (boot_cpu != plat_my_core_pos()) |
Varun Wadekar | b539b6c | 2015-03-13 15:18:20 +0530 | [diff] [blame] | 217 | SMC_RET1(handle, SMC_UNK); |
| 218 | |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 219 | /* Determine which security state this SMC originated from */ |
| 220 | ns = is_caller_non_secure(flags); |
| 221 | |
| 222 | switch (smc_fid) { |
| 223 | |
| 224 | /* |
Varun Wadekar | 968c029 | 2015-03-13 15:10:54 +0530 | [diff] [blame] | 225 | * This function ID is used by SP to indicate that it was |
| 226 | * preempted by a non-secure world IRQ. |
| 227 | */ |
| 228 | case TLK_PREEMPTED: |
| 229 | |
| 230 | if (ns) |
| 231 | SMC_RET1(handle, SMC_UNK); |
| 232 | |
| 233 | assert(handle == cm_get_context(SECURE)); |
| 234 | cm_el1_sysregs_context_save(SECURE); |
| 235 | |
| 236 | /* Get a reference to the non-secure context */ |
| 237 | ns_cpu_context = cm_get_context(NON_SECURE); |
| 238 | assert(ns_cpu_context); |
| 239 | |
| 240 | /* |
| 241 | * Restore non-secure state. There is no need to save the |
| 242 | * secure system register context since the SP was supposed |
| 243 | * to preserve it during S-EL1 interrupt handling. |
| 244 | */ |
| 245 | cm_el1_sysregs_context_restore(NON_SECURE); |
| 246 | cm_set_next_eret_context(NON_SECURE); |
| 247 | |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 248 | SMC_RET1(ns_cpu_context, x1); |
Varun Wadekar | 968c029 | 2015-03-13 15:10:54 +0530 | [diff] [blame] | 249 | |
| 250 | /* |
Varun Wadekar | a97535f | 2015-03-13 14:19:11 +0530 | [diff] [blame] | 251 | * This is a request from the non-secure context to: |
| 252 | * |
| 253 | * a. register shared memory with the SP for storing it's |
| 254 | * activity logs. |
| 255 | * b. register shared memory with the SP for passing args |
| 256 | * required for maintaining sessions with the Trusted |
| 257 | * Applications. |
Mihir Joshi | 518230c | 2018-01-22 14:02:16 -0800 | [diff] [blame] | 258 | * c. register shared persistent buffers for secure storage |
| 259 | * d. register NS DRAM ranges passed by Cboot |
| 260 | * e. register Root of Trust parameters from Cboot for Verified Boot |
| 261 | * f. open/close sessions |
| 262 | * g. issue commands to the Trusted Apps |
| 263 | * h. resume the preempted yielding SMC call. |
Varun Wadekar | a97535f | 2015-03-13 14:19:11 +0530 | [diff] [blame] | 264 | */ |
| 265 | case TLK_REGISTER_LOGBUF: |
| 266 | case TLK_REGISTER_REQBUF: |
Mihir Joshi | 518230c | 2018-01-22 14:02:16 -0800 | [diff] [blame] | 267 | case TLK_SS_REGISTER_HANDLER: |
| 268 | case TLK_REGISTER_NS_DRAM_RANGES: |
| 269 | case TLK_SET_ROOT_OF_TRUST: |
Varun Wadekar | b539b6c | 2015-03-13 15:18:20 +0530 | [diff] [blame] | 270 | case TLK_OPEN_TA_SESSION: |
| 271 | case TLK_CLOSE_TA_SESSION: |
| 272 | case TLK_TA_LAUNCH_OP: |
| 273 | case TLK_TA_SEND_EVENT: |
Varun Wadekar | 5e1fa05 | 2015-10-07 17:15:41 +0530 | [diff] [blame] | 274 | case TLK_RESUME_FID: |
Mustafa Yigit Bilgen | 95867b1 | 2018-12-03 15:53:38 -0800 | [diff] [blame] | 275 | case TLK_SET_BL_VERSION: |
| 276 | case TLK_LOCK_BL_INTERFACE: |
| 277 | case TLK_BL_RPMB_SERVICE: |
Varun Wadekar | b539b6c | 2015-03-13 15:18:20 +0530 | [diff] [blame] | 278 | |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 279 | if (!ns) |
Varun Wadekar | a97535f | 2015-03-13 14:19:11 +0530 | [diff] [blame] | 280 | SMC_RET1(handle, SMC_UNK); |
| 281 | |
| 282 | /* |
| 283 | * This is a fresh request from the non-secure client. |
| 284 | * The parameters are in x1 and x2. Figure out which |
| 285 | * registers need to be preserved, save the non-secure |
| 286 | * state and send the request to the secure payload. |
| 287 | */ |
| 288 | assert(handle == cm_get_context(NON_SECURE)); |
| 289 | |
Varun Wadekar | 5e1fa05 | 2015-10-07 17:15:41 +0530 | [diff] [blame] | 290 | /* |
David Cunado | c8833ea | 2017-04-16 17:15:08 +0100 | [diff] [blame] | 291 | * Check if we are already processing a yielding SMC |
Varun Wadekar | 5e1fa05 | 2015-10-07 17:15:41 +0530 | [diff] [blame] | 292 | * call. Of all the supported fids, only the "resume" |
| 293 | * fid expects the flag to be set. |
| 294 | */ |
| 295 | if (smc_fid == TLK_RESUME_FID) { |
David Cunado | c8833ea | 2017-04-16 17:15:08 +0100 | [diff] [blame] | 296 | if (!get_yield_smc_active_flag(tlk_ctx.state)) |
Varun Wadekar | 5e1fa05 | 2015-10-07 17:15:41 +0530 | [diff] [blame] | 297 | SMC_RET1(handle, SMC_UNK); |
| 298 | } else { |
David Cunado | c8833ea | 2017-04-16 17:15:08 +0100 | [diff] [blame] | 299 | if (get_yield_smc_active_flag(tlk_ctx.state)) |
Varun Wadekar | 5e1fa05 | 2015-10-07 17:15:41 +0530 | [diff] [blame] | 300 | SMC_RET1(handle, SMC_UNK); |
| 301 | } |
Varun Wadekar | a97535f | 2015-03-13 14:19:11 +0530 | [diff] [blame] | 302 | |
| 303 | cm_el1_sysregs_context_save(NON_SECURE); |
| 304 | |
| 305 | /* |
| 306 | * Verify if there is a valid context to use. |
| 307 | */ |
| 308 | assert(&tlk_ctx.cpu_ctx == cm_get_context(SECURE)); |
| 309 | |
| 310 | /* |
| 311 | * Mark the SP state as active. |
| 312 | */ |
David Cunado | c8833ea | 2017-04-16 17:15:08 +0100 | [diff] [blame] | 313 | set_yield_smc_active_flag(tlk_ctx.state); |
Varun Wadekar | a97535f | 2015-03-13 14:19:11 +0530 | [diff] [blame] | 314 | |
Varun Wadekar | a97535f | 2015-03-13 14:19:11 +0530 | [diff] [blame] | 315 | /* |
| 316 | * We are done stashing the non-secure context. Ask the |
| 317 | * secure payload to do the work now. |
| 318 | */ |
| 319 | cm_el1_sysregs_context_restore(SECURE); |
| 320 | cm_set_next_eret_context(SECURE); |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 321 | |
| 322 | /* |
| 323 | * TLK is a 32-bit Trusted OS and so expects the SMC |
| 324 | * arguments via r0-r7. TLK expects the monitor frame |
| 325 | * registers to be 64-bits long. Hence, we pass x0 in |
| 326 | * r0-r1, x1 in r2-r3, x3 in r4-r5 and x4 in r6-r7. |
| 327 | * |
| 328 | * As smc_fid is a uint32 value, r1 contains 0. |
| 329 | */ |
| 330 | gp_regs = get_gpregs_ctx(&tlk_ctx.cpu_ctx); |
| 331 | write_ctx_reg(gp_regs, CTX_GPREG_X4, (uint32_t)x2); |
| 332 | write_ctx_reg(gp_regs, CTX_GPREG_X5, (uint32_t)(x2 >> 32)); |
| 333 | write_ctx_reg(gp_regs, CTX_GPREG_X6, (uint32_t)x3); |
| 334 | write_ctx_reg(gp_regs, CTX_GPREG_X7, (uint32_t)(x3 >> 32)); |
| 335 | SMC_RET4(&tlk_ctx.cpu_ctx, smc_fid, 0, (uint32_t)x1, |
| 336 | (uint32_t)(x1 >> 32)); |
Varun Wadekar | a97535f | 2015-03-13 14:19:11 +0530 | [diff] [blame] | 337 | |
| 338 | /* |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 339 | * Translate NS/EL1-S virtual addresses. |
| 340 | * |
| 341 | * x1 = virtual address |
| 342 | * x3 = type (NS/S) |
| 343 | * |
| 344 | * Returns PA:lo in r0, PA:hi in r1. |
Varun Wadekar | 97625e3 | 2015-03-13 14:59:03 +0530 | [diff] [blame] | 345 | */ |
| 346 | case TLK_VA_TRANSLATE: |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 347 | |
| 348 | /* Should be invoked only by secure world */ |
| 349 | if (ns) |
Varun Wadekar | 97625e3 | 2015-03-13 14:59:03 +0530 | [diff] [blame] | 350 | SMC_RET1(handle, SMC_UNK); |
| 351 | |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 352 | /* NS virtual addresses are 64-bit long */ |
| 353 | if (x3 & TLK_TRANSLATE_NS_VADDR) |
| 354 | x1 = (uint32_t)x1 | (x2 << 32); |
Varun Wadekar | 97625e3 | 2015-03-13 14:59:03 +0530 | [diff] [blame] | 355 | |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 356 | if (!x1) |
| 357 | SMC_RET1(handle, SMC_UNK); |
Varun Wadekar | 97625e3 | 2015-03-13 14:59:03 +0530 | [diff] [blame] | 358 | |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 359 | /* |
| 360 | * TODO: Sanity check x1. This would require platform |
| 361 | * support. |
| 362 | */ |
Varun Wadekar | 97625e3 | 2015-03-13 14:59:03 +0530 | [diff] [blame] | 363 | |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 364 | /* virtual address and type: ns/s */ |
| 365 | par = tlkd_va_translate(x1, x3); |
| 366 | |
| 367 | /* return physical address in r0-r1 */ |
| 368 | SMC_RET4(handle, (uint32_t)par, (uint32_t)(par >> 32), 0, 0); |
Varun Wadekar | 97625e3 | 2015-03-13 14:59:03 +0530 | [diff] [blame] | 369 | |
| 370 | /* |
Varun Wadekar | a97535f | 2015-03-13 14:19:11 +0530 | [diff] [blame] | 371 | * This is a request from the SP to mark completion of |
David Cunado | c8833ea | 2017-04-16 17:15:08 +0100 | [diff] [blame] | 372 | * a yielding function ID. |
Varun Wadekar | a97535f | 2015-03-13 14:19:11 +0530 | [diff] [blame] | 373 | */ |
| 374 | case TLK_REQUEST_DONE: |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 375 | if (ns) |
Varun Wadekar | a97535f | 2015-03-13 14:19:11 +0530 | [diff] [blame] | 376 | SMC_RET1(handle, SMC_UNK); |
| 377 | |
| 378 | /* |
| 379 | * Mark the SP state as inactive. |
| 380 | */ |
David Cunado | c8833ea | 2017-04-16 17:15:08 +0100 | [diff] [blame] | 381 | clr_yield_smc_active_flag(tlk_ctx.state); |
Varun Wadekar | a97535f | 2015-03-13 14:19:11 +0530 | [diff] [blame] | 382 | |
| 383 | /* Get a reference to the non-secure context */ |
| 384 | ns_cpu_context = cm_get_context(NON_SECURE); |
| 385 | assert(ns_cpu_context); |
| 386 | |
| 387 | /* |
| 388 | * This is a request completion SMC and we must switch to |
| 389 | * the non-secure world to pass the result. |
| 390 | */ |
| 391 | cm_el1_sysregs_context_save(SECURE); |
| 392 | |
| 393 | /* |
| 394 | * We are done stashing the secure context. Switch to the |
| 395 | * non-secure context and return the result. |
| 396 | */ |
| 397 | cm_el1_sysregs_context_restore(NON_SECURE); |
| 398 | cm_set_next_eret_context(NON_SECURE); |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 399 | SMC_RET1(ns_cpu_context, x1); |
Varun Wadekar | a97535f | 2015-03-13 14:19:11 +0530 | [diff] [blame] | 400 | |
| 401 | /* |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 402 | * This function ID is used only by the SP to indicate it has |
| 403 | * finished initialising itself after a cold boot |
| 404 | */ |
| 405 | case TLK_ENTRY_DONE: |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 406 | if (ns) |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 407 | SMC_RET1(handle, SMC_UNK); |
| 408 | |
| 409 | /* |
| 410 | * SP has been successfully initialized. Register power |
Paul Beesley | 1fbc97b | 2019-01-11 18:26:51 +0000 | [diff] [blame] | 411 | * management hooks with PSCI |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 412 | */ |
| 413 | psci_register_spd_pm_hook(&tlkd_pm_ops); |
| 414 | |
| 415 | /* |
| 416 | * TLK reports completion. The SPD must have initiated |
| 417 | * the original request through a synchronous entry |
| 418 | * into the SP. Jump back to the original C runtime |
| 419 | * context. |
| 420 | */ |
Varun Wadekar | ebfeae9 | 2015-04-02 14:57:47 +0530 | [diff] [blame] | 421 | tlkd_synchronous_sp_exit(&tlk_ctx, x1); |
Jonathan Wright | 75a5d8b | 2018-03-14 15:56:21 +0000 | [diff] [blame] | 422 | break; |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 423 | |
| 424 | /* |
Varun Wadekar | a70dec3 | 2015-08-26 12:49:03 +0530 | [diff] [blame] | 425 | * These function IDs are used only by TLK to indicate it has |
| 426 | * finished: |
| 427 | * 1. suspending itself after an earlier psci cpu_suspend |
| 428 | * request. |
| 429 | * 2. resuming itself after an earlier psci cpu_suspend |
| 430 | * request. |
| 431 | * 3. powering down after an earlier psci system_off/system_reset |
| 432 | * request. |
| 433 | */ |
| 434 | case TLK_SUSPEND_DONE: |
| 435 | case TLK_RESUME_DONE: |
Varun Wadekar | a70dec3 | 2015-08-26 12:49:03 +0530 | [diff] [blame] | 436 | |
| 437 | if (ns) |
| 438 | SMC_RET1(handle, SMC_UNK); |
| 439 | |
| 440 | /* |
| 441 | * TLK reports completion. TLKD must have initiated the |
| 442 | * original request through a synchronous entry into the SP. |
| 443 | * Jump back to the original C runtime context, and pass x1 as |
| 444 | * return value to the caller |
| 445 | */ |
| 446 | tlkd_synchronous_sp_exit(&tlk_ctx, x1); |
Jonathan Wright | 75a5d8b | 2018-03-14 15:56:21 +0000 | [diff] [blame] | 447 | break; |
Varun Wadekar | a70dec3 | 2015-08-26 12:49:03 +0530 | [diff] [blame] | 448 | |
| 449 | /* |
Varun Wadekar | 079e20e | 2018-08-10 09:55:25 -0700 | [diff] [blame] | 450 | * This function ID is used by SP to indicate that it has completed |
| 451 | * handling the secure interrupt. |
| 452 | */ |
| 453 | case TLK_IRQ_DONE: |
| 454 | |
| 455 | if (ns) |
| 456 | SMC_RET1(handle, SMC_UNK); |
| 457 | |
| 458 | assert(handle == cm_get_context(SECURE)); |
| 459 | |
| 460 | /* save secure world context */ |
| 461 | cm_el1_sysregs_context_save(SECURE); |
| 462 | |
| 463 | /* Get a reference to the non-secure context */ |
| 464 | ns_cpu_context = cm_get_context(NON_SECURE); |
| 465 | assert(ns_cpu_context); |
| 466 | |
| 467 | /* |
| 468 | * Restore non-secure state. There is no need to save the |
| 469 | * secure system register context since the SP was supposed |
| 470 | * to preserve it during S-EL1 interrupt handling. |
| 471 | */ |
| 472 | cm_el1_sysregs_context_restore(NON_SECURE); |
| 473 | cm_set_next_eret_context(NON_SECURE); |
| 474 | |
| 475 | SMC_RET0(ns_cpu_context); |
| 476 | |
| 477 | /* |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 478 | * Return the number of service function IDs implemented to |
| 479 | * provide service to non-secure |
| 480 | */ |
| 481 | case TOS_CALL_COUNT: |
| 482 | SMC_RET1(handle, TLK_NUM_FID); |
| 483 | |
| 484 | /* |
| 485 | * Return TLK's UID to the caller |
| 486 | */ |
| 487 | case TOS_UID: |
| 488 | SMC_UUID_RET(handle, tlk_uuid); |
| 489 | |
| 490 | /* |
| 491 | * Return the version of current implementation |
| 492 | */ |
| 493 | case TOS_CALL_VERSION: |
| 494 | SMC_RET2(handle, TLK_VERSION_MAJOR, TLK_VERSION_MINOR); |
| 495 | |
| 496 | default: |
Mihir Joshi | 518230c | 2018-01-22 14:02:16 -0800 | [diff] [blame] | 497 | WARN("%s: Unhandled SMC: 0x%x\n", __func__, smc_fid); |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 498 | break; |
| 499 | } |
| 500 | |
| 501 | SMC_RET1(handle, SMC_UNK); |
| 502 | } |
| 503 | |
| 504 | /* Define a SPD runtime service descriptor for fast SMC calls */ |
| 505 | DECLARE_RT_SVC( |
| 506 | tlkd_tos_fast, |
| 507 | |
| 508 | OEN_TOS_START, |
| 509 | OEN_TOS_END, |
| 510 | SMC_TYPE_FAST, |
| 511 | tlkd_setup, |
| 512 | tlkd_smc_handler |
| 513 | ); |
| 514 | |
David Cunado | c8833ea | 2017-04-16 17:15:08 +0100 | [diff] [blame] | 515 | /* Define a SPD runtime service descriptor for yielding SMC calls */ |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 516 | DECLARE_RT_SVC( |
| 517 | tlkd_tos_std, |
| 518 | |
| 519 | OEN_TOS_START, |
| 520 | OEN_TOS_END, |
David Cunado | c8833ea | 2017-04-16 17:15:08 +0100 | [diff] [blame] | 521 | SMC_TYPE_YIELD, |
Varun Wadekar | 3d4e6a5 | 2015-03-13 14:01:03 +0530 | [diff] [blame] | 522 | NULL, |
| 523 | tlkd_smc_handler |
| 524 | ); |
Varun Wadekar | b539b6c | 2015-03-13 15:18:20 +0530 | [diff] [blame] | 525 | |
| 526 | /* Define a SPD runtime service descriptor for fast SMC calls */ |
| 527 | DECLARE_RT_SVC( |
| 528 | tlkd_tap_fast, |
| 529 | |
| 530 | OEN_TAP_START, |
| 531 | OEN_TAP_END, |
| 532 | SMC_TYPE_FAST, |
| 533 | NULL, |
| 534 | tlkd_smc_handler |
| 535 | ); |
| 536 | |
David Cunado | c8833ea | 2017-04-16 17:15:08 +0100 | [diff] [blame] | 537 | /* Define a SPD runtime service descriptor for yielding SMC calls */ |
Varun Wadekar | b539b6c | 2015-03-13 15:18:20 +0530 | [diff] [blame] | 538 | DECLARE_RT_SVC( |
| 539 | tlkd_tap_std, |
| 540 | |
| 541 | OEN_TAP_START, |
| 542 | OEN_TAP_END, |
David Cunado | c8833ea | 2017-04-16 17:15:08 +0100 | [diff] [blame] | 543 | SMC_TYPE_YIELD, |
Varun Wadekar | b539b6c | 2015-03-13 15:18:20 +0530 | [diff] [blame] | 544 | NULL, |
| 545 | tlkd_smc_handler |
| 546 | ); |