177c27753SZelalem Aweke /* 2319fb084SSoby Mathew * Copyright (c) 2021-2022, ARM Limited and Contributors. All rights reserved. 377c27753SZelalem Aweke * 477c27753SZelalem Aweke * SPDX-License-Identifier: BSD-3-Clause 577c27753SZelalem Aweke */ 677c27753SZelalem Aweke 777c27753SZelalem Aweke #include <assert.h> 877c27753SZelalem Aweke #include <errno.h> 92461bd3aSManish Pandey #include <inttypes.h> 102461bd3aSManish Pandey #include <stdint.h> 1177c27753SZelalem Aweke #include <string.h> 1277c27753SZelalem Aweke 1377c27753SZelalem Aweke #include <arch_helpers.h> 1477c27753SZelalem Aweke #include <arch_features.h> 1577c27753SZelalem Aweke #include <bl31/bl31.h> 1677c27753SZelalem Aweke #include <common/debug.h> 1777c27753SZelalem Aweke #include <common/runtime_svc.h> 1877c27753SZelalem Aweke #include <context.h> 1977c27753SZelalem Aweke #include <lib/el3_runtime/context_mgmt.h> 2077c27753SZelalem Aweke #include <lib/el3_runtime/pubsub.h> 21f19dc624Sjohpow01 #include <lib/gpt_rme/gpt_rme.h> 2277c27753SZelalem Aweke 2377c27753SZelalem Aweke #include <lib/spinlock.h> 2477c27753SZelalem Aweke #include <lib/utils.h> 2577c27753SZelalem Aweke #include <lib/xlat_tables/xlat_tables_v2.h> 2677c27753SZelalem Aweke #include <plat/common/common_def.h> 2777c27753SZelalem Aweke #include <plat/common/platform.h> 2877c27753SZelalem Aweke #include <platform_def.h> 2977c27753SZelalem Aweke #include <services/rmmd_svc.h> 3077c27753SZelalem Aweke #include <smccc_helpers.h> 31a4cc85c1SSubhasish Ghosh #include <lib/extensions/sve.h> 3277c27753SZelalem Aweke #include "rmmd_initial_context.h" 3377c27753SZelalem Aweke #include "rmmd_private.h" 3477c27753SZelalem Aweke 3577c27753SZelalem Aweke /******************************************************************************* 36*8c980a4aSJavier Almansa Sobrino * RMM <-> EL3 shared buffer information. 37*8c980a4aSJavier Almansa Sobrino ******************************************************************************/ 38*8c980a4aSJavier Almansa Sobrino static size_t shared_buf_size; 39*8c980a4aSJavier Almansa Sobrino static uintptr_t shared_buf_base; 40*8c980a4aSJavier Almansa Sobrino 41*8c980a4aSJavier Almansa Sobrino /******************************************************************************* 42*8c980a4aSJavier Almansa Sobrino * RMM boot failure flag 43*8c980a4aSJavier Almansa Sobrino ******************************************************************************/ 44*8c980a4aSJavier Almansa Sobrino static bool rmm_boot_failed; 45*8c980a4aSJavier Almansa Sobrino 46*8c980a4aSJavier Almansa Sobrino /******************************************************************************* 4777c27753SZelalem Aweke * RMM context information. 4877c27753SZelalem Aweke ******************************************************************************/ 4977c27753SZelalem Aweke rmmd_rmm_context_t rmm_context[PLATFORM_CORE_COUNT]; 5077c27753SZelalem Aweke 5177c27753SZelalem Aweke /******************************************************************************* 5277c27753SZelalem Aweke * RMM entry point information. Discovered on the primary core and reused 5377c27753SZelalem Aweke * on secondary cores. 5477c27753SZelalem Aweke ******************************************************************************/ 5577c27753SZelalem Aweke static entry_point_info_t *rmm_ep_info; 5677c27753SZelalem Aweke 5777c27753SZelalem Aweke /******************************************************************************* 5877c27753SZelalem Aweke * Static function declaration. 5977c27753SZelalem Aweke ******************************************************************************/ 6077c27753SZelalem Aweke static int32_t rmm_init(void); 6177c27753SZelalem Aweke 6277c27753SZelalem Aweke /******************************************************************************* 6377c27753SZelalem Aweke * This function takes an RMM context pointer and performs a synchronous entry 6477c27753SZelalem Aweke * into it. 6577c27753SZelalem Aweke ******************************************************************************/ 6677c27753SZelalem Aweke uint64_t rmmd_rmm_sync_entry(rmmd_rmm_context_t *rmm_ctx) 6777c27753SZelalem Aweke { 6877c27753SZelalem Aweke uint64_t rc; 6977c27753SZelalem Aweke 7077c27753SZelalem Aweke assert(rmm_ctx != NULL); 7177c27753SZelalem Aweke 7277c27753SZelalem Aweke cm_set_context(&(rmm_ctx->cpu_ctx), REALM); 7377c27753SZelalem Aweke 7477c27753SZelalem Aweke /* Restore the realm context assigned above */ 7577c27753SZelalem Aweke cm_el1_sysregs_context_restore(REALM); 7677c27753SZelalem Aweke cm_el2_sysregs_context_restore(REALM); 7777c27753SZelalem Aweke cm_set_next_eret_context(REALM); 7877c27753SZelalem Aweke 7977c27753SZelalem Aweke /* Enter RMM */ 8077c27753SZelalem Aweke rc = rmmd_rmm_enter(&rmm_ctx->c_rt_ctx); 8177c27753SZelalem Aweke 828b95e848SZelalem Aweke /* 838b95e848SZelalem Aweke * Save realm context. EL1 and EL2 Non-secure 848b95e848SZelalem Aweke * contexts will be restored before exiting to 858b95e848SZelalem Aweke * Non-secure world, therefore there is no need 868b95e848SZelalem Aweke * to clear EL1 and EL2 context registers. 878b95e848SZelalem Aweke */ 8877c27753SZelalem Aweke cm_el1_sysregs_context_save(REALM); 8977c27753SZelalem Aweke cm_el2_sysregs_context_save(REALM); 9077c27753SZelalem Aweke 9177c27753SZelalem Aweke return rc; 9277c27753SZelalem Aweke } 9377c27753SZelalem Aweke 9477c27753SZelalem Aweke /******************************************************************************* 9577c27753SZelalem Aweke * This function returns to the place where rmmd_rmm_sync_entry() was 9677c27753SZelalem Aweke * called originally. 9777c27753SZelalem Aweke ******************************************************************************/ 9877c27753SZelalem Aweke __dead2 void rmmd_rmm_sync_exit(uint64_t rc) 9977c27753SZelalem Aweke { 10077c27753SZelalem Aweke rmmd_rmm_context_t *ctx = &rmm_context[plat_my_core_pos()]; 10177c27753SZelalem Aweke 10277c27753SZelalem Aweke /* Get context of the RMM in use by this CPU. */ 10377c27753SZelalem Aweke assert(cm_get_context(REALM) == &(ctx->cpu_ctx)); 10477c27753SZelalem Aweke 10577c27753SZelalem Aweke /* 10677c27753SZelalem Aweke * The RMMD must have initiated the original request through a 10777c27753SZelalem Aweke * synchronous entry into RMM. Jump back to the original C runtime 10877c27753SZelalem Aweke * context with the value of rc in x0; 10977c27753SZelalem Aweke */ 11077c27753SZelalem Aweke rmmd_rmm_exit(ctx->c_rt_ctx, rc); 11177c27753SZelalem Aweke 11277c27753SZelalem Aweke panic(); 11377c27753SZelalem Aweke } 11477c27753SZelalem Aweke 11577c27753SZelalem Aweke static void rmm_el2_context_init(el2_sysregs_t *regs) 11677c27753SZelalem Aweke { 11777c27753SZelalem Aweke regs->ctx_regs[CTX_SPSR_EL2 >> 3] = REALM_SPSR_EL2; 11877c27753SZelalem Aweke regs->ctx_regs[CTX_SCTLR_EL2 >> 3] = SCTLR_EL2_RES1; 11977c27753SZelalem Aweke } 12077c27753SZelalem Aweke 12177c27753SZelalem Aweke /******************************************************************************* 122a4cc85c1SSubhasish Ghosh * Enable architecture extensions on first entry to Realm world. 123a4cc85c1SSubhasish Ghosh ******************************************************************************/ 124a4cc85c1SSubhasish Ghosh static void manage_extensions_realm(cpu_context_t *ctx) 125a4cc85c1SSubhasish Ghosh { 126a4cc85c1SSubhasish Ghosh #if ENABLE_SVE_FOR_NS 127a4cc85c1SSubhasish Ghosh /* 128a4cc85c1SSubhasish Ghosh * Enable SVE and FPU in realm context when it is enabled for NS. 129a4cc85c1SSubhasish Ghosh * Realm manager must ensure that the SVE and FPU register 130a4cc85c1SSubhasish Ghosh * contexts are properly managed. 131a4cc85c1SSubhasish Ghosh */ 132a4cc85c1SSubhasish Ghosh sve_enable(ctx); 133a4cc85c1SSubhasish Ghosh #else 134a4cc85c1SSubhasish Ghosh /* 135a4cc85c1SSubhasish Ghosh * Disable SVE and FPU in realm context when it is disabled for NS. 136a4cc85c1SSubhasish Ghosh */ 137a4cc85c1SSubhasish Ghosh sve_disable(ctx); 138a4cc85c1SSubhasish Ghosh #endif /* ENABLE_SVE_FOR_NS */ 139a4cc85c1SSubhasish Ghosh } 140a4cc85c1SSubhasish Ghosh 141a4cc85c1SSubhasish Ghosh /******************************************************************************* 14277c27753SZelalem Aweke * Jump to the RMM for the first time. 14377c27753SZelalem Aweke ******************************************************************************/ 14477c27753SZelalem Aweke static int32_t rmm_init(void) 14577c27753SZelalem Aweke { 146*8c980a4aSJavier Almansa Sobrino long rc; 14777c27753SZelalem Aweke rmmd_rmm_context_t *ctx = &rmm_context[plat_my_core_pos()]; 14877c27753SZelalem Aweke 14977c27753SZelalem Aweke INFO("RMM init start.\n"); 15077c27753SZelalem Aweke 151a4cc85c1SSubhasish Ghosh /* Enable architecture extensions */ 152a4cc85c1SSubhasish Ghosh manage_extensions_realm(&ctx->cpu_ctx); 153a4cc85c1SSubhasish Ghosh 15477c27753SZelalem Aweke /* Initialize RMM EL2 context. */ 15577c27753SZelalem Aweke rmm_el2_context_init(&ctx->cpu_ctx.el2_sysregs_ctx); 15677c27753SZelalem Aweke 15777c27753SZelalem Aweke rc = rmmd_rmm_sync_entry(ctx); 158*8c980a4aSJavier Almansa Sobrino if (rc != E_RMM_BOOT_SUCCESS) { 159*8c980a4aSJavier Almansa Sobrino ERROR("RMM init failed: %ld\n", rc); 160*8c980a4aSJavier Almansa Sobrino /* Mark the boot as failed for all the CPUs */ 161*8c980a4aSJavier Almansa Sobrino rmm_boot_failed = true; 162*8c980a4aSJavier Almansa Sobrino return 0; 16377c27753SZelalem Aweke } 16477c27753SZelalem Aweke 16577c27753SZelalem Aweke INFO("RMM init end.\n"); 16677c27753SZelalem Aweke 16777c27753SZelalem Aweke return 1; 16877c27753SZelalem Aweke } 16977c27753SZelalem Aweke 17077c27753SZelalem Aweke /******************************************************************************* 17177c27753SZelalem Aweke * Load and read RMM manifest, setup RMM. 17277c27753SZelalem Aweke ******************************************************************************/ 17377c27753SZelalem Aweke int rmmd_setup(void) 17477c27753SZelalem Aweke { 17577c27753SZelalem Aweke uint32_t ep_attr; 17677c27753SZelalem Aweke unsigned int linear_id = plat_my_core_pos(); 17777c27753SZelalem Aweke rmmd_rmm_context_t *rmm_ctx = &rmm_context[linear_id]; 17877c27753SZelalem Aweke 17977c27753SZelalem Aweke /* Make sure RME is supported. */ 18077c27753SZelalem Aweke assert(get_armv9_2_feat_rme_support() != 0U); 18177c27753SZelalem Aweke 18277c27753SZelalem Aweke rmm_ep_info = bl31_plat_get_next_image_ep_info(REALM); 18377c27753SZelalem Aweke if (rmm_ep_info == NULL) { 18477c27753SZelalem Aweke WARN("No RMM image provided by BL2 boot loader, Booting " 18577c27753SZelalem Aweke "device without RMM initialization. SMCs destined for " 18677c27753SZelalem Aweke "RMM will return SMC_UNK\n"); 18777c27753SZelalem Aweke return -ENOENT; 18877c27753SZelalem Aweke } 18977c27753SZelalem Aweke 19077c27753SZelalem Aweke /* Under no circumstances will this parameter be 0 */ 19177c27753SZelalem Aweke assert(rmm_ep_info->pc == RMM_BASE); 19277c27753SZelalem Aweke 19377c27753SZelalem Aweke /* Initialise an entrypoint to set up the CPU context */ 19477c27753SZelalem Aweke ep_attr = EP_REALM; 19577c27753SZelalem Aweke if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0U) { 19677c27753SZelalem Aweke ep_attr |= EP_EE_BIG; 19777c27753SZelalem Aweke } 19877c27753SZelalem Aweke 19977c27753SZelalem Aweke SET_PARAM_HEAD(rmm_ep_info, PARAM_EP, VERSION_1, ep_attr); 20077c27753SZelalem Aweke rmm_ep_info->spsr = SPSR_64(MODE_EL2, 20177c27753SZelalem Aweke MODE_SP_ELX, 20277c27753SZelalem Aweke DISABLE_ALL_EXCEPTIONS); 20377c27753SZelalem Aweke 204*8c980a4aSJavier Almansa Sobrino shared_buf_size = 205*8c980a4aSJavier Almansa Sobrino plat_rmmd_get_el3_rmm_shared_mem(&shared_buf_base); 206*8c980a4aSJavier Almansa Sobrino 207*8c980a4aSJavier Almansa Sobrino assert((shared_buf_size == SZ_4K) && 208*8c980a4aSJavier Almansa Sobrino ((void *)shared_buf_base != NULL)); 209*8c980a4aSJavier Almansa Sobrino 210*8c980a4aSJavier Almansa Sobrino /* 211*8c980a4aSJavier Almansa Sobrino * Prepare coldboot arguments for RMM: 212*8c980a4aSJavier Almansa Sobrino * arg0: This CPUID (primary processor). 213*8c980a4aSJavier Almansa Sobrino * arg1: Version for this Boot Interface. 214*8c980a4aSJavier Almansa Sobrino * arg2: PLATFORM_CORE_COUNT. 215*8c980a4aSJavier Almansa Sobrino * arg3: Base address for the EL3 <-> RMM shared area. The boot 216*8c980a4aSJavier Almansa Sobrino * manifest will be stored at the beginning of this area. 217*8c980a4aSJavier Almansa Sobrino */ 218*8c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg0 = linear_id; 219*8c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg1 = RMM_EL3_INTERFACE_VERSION; 220*8c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg2 = PLATFORM_CORE_COUNT; 221*8c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg3 = shared_buf_base; 222*8c980a4aSJavier Almansa Sobrino 22377c27753SZelalem Aweke /* Initialise RMM context with this entry point information */ 22477c27753SZelalem Aweke cm_setup_context(&rmm_ctx->cpu_ctx, rmm_ep_info); 22577c27753SZelalem Aweke 22677c27753SZelalem Aweke INFO("RMM setup done.\n"); 22777c27753SZelalem Aweke 22877c27753SZelalem Aweke /* Register init function for deferred init. */ 22977c27753SZelalem Aweke bl31_register_rmm_init(&rmm_init); 23077c27753SZelalem Aweke 23177c27753SZelalem Aweke return 0; 23277c27753SZelalem Aweke } 23377c27753SZelalem Aweke 23477c27753SZelalem Aweke /******************************************************************************* 23577c27753SZelalem Aweke * Forward SMC to the other security state 23677c27753SZelalem Aweke ******************************************************************************/ 23711578303SSoby Mathew static uint64_t rmmd_smc_forward(uint32_t src_sec_state, 23811578303SSoby Mathew uint32_t dst_sec_state, uint64_t x0, 23911578303SSoby Mathew uint64_t x1, uint64_t x2, uint64_t x3, 24011578303SSoby Mathew uint64_t x4, void *handle) 24177c27753SZelalem Aweke { 24277c27753SZelalem Aweke /* Save incoming security state */ 24377c27753SZelalem Aweke cm_el1_sysregs_context_save(src_sec_state); 24477c27753SZelalem Aweke cm_el2_sysregs_context_save(src_sec_state); 24577c27753SZelalem Aweke 24677c27753SZelalem Aweke /* Restore outgoing security state */ 24777c27753SZelalem Aweke cm_el1_sysregs_context_restore(dst_sec_state); 24877c27753SZelalem Aweke cm_el2_sysregs_context_restore(dst_sec_state); 24977c27753SZelalem Aweke cm_set_next_eret_context(dst_sec_state); 25077c27753SZelalem Aweke 25111578303SSoby Mathew /* 25211578303SSoby Mathew * As per SMCCCv1.1, we need to preserve x4 to x7 unless 25311578303SSoby Mathew * being used as return args. Hence we differentiate the 25411578303SSoby Mathew * onward and backward path. Support upto 8 args in the 25511578303SSoby Mathew * onward path and 4 args in return path. 25611578303SSoby Mathew */ 25711578303SSoby Mathew if (src_sec_state == NON_SECURE) { 25811578303SSoby Mathew SMC_RET8(cm_get_context(dst_sec_state), x0, x1, x2, x3, x4, 25977c27753SZelalem Aweke SMC_GET_GP(handle, CTX_GPREG_X5), 26077c27753SZelalem Aweke SMC_GET_GP(handle, CTX_GPREG_X6), 26177c27753SZelalem Aweke SMC_GET_GP(handle, CTX_GPREG_X7)); 26211578303SSoby Mathew } else { 26311578303SSoby Mathew SMC_RET4(cm_get_context(dst_sec_state), x0, x1, x2, x3); 26411578303SSoby Mathew } 26577c27753SZelalem Aweke } 26677c27753SZelalem Aweke 26777c27753SZelalem Aweke /******************************************************************************* 26877c27753SZelalem Aweke * This function handles all SMCs in the range reserved for RMI. Each call is 26977c27753SZelalem Aweke * either forwarded to the other security state or handled by the RMM dispatcher 27077c27753SZelalem Aweke ******************************************************************************/ 27177c27753SZelalem Aweke uint64_t rmmd_rmi_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, 27277c27753SZelalem Aweke uint64_t x3, uint64_t x4, void *cookie, 27377c27753SZelalem Aweke void *handle, uint64_t flags) 27477c27753SZelalem Aweke { 27577c27753SZelalem Aweke uint32_t src_sec_state; 27677c27753SZelalem Aweke 277*8c980a4aSJavier Almansa Sobrino /* If RMM failed to boot, treat any RMI SMC as unknown */ 278*8c980a4aSJavier Almansa Sobrino if (rmm_boot_failed) { 279*8c980a4aSJavier Almansa Sobrino WARN("RMMD: Failed to boot up RMM. Ignoring RMI call\n"); 280*8c980a4aSJavier Almansa Sobrino SMC_RET1(handle, SMC_UNK); 281*8c980a4aSJavier Almansa Sobrino } 282*8c980a4aSJavier Almansa Sobrino 28377c27753SZelalem Aweke /* Determine which security state this SMC originated from */ 28477c27753SZelalem Aweke src_sec_state = caller_sec_state(flags); 28577c27753SZelalem Aweke 28677c27753SZelalem Aweke /* RMI must not be invoked by the Secure world */ 28777c27753SZelalem Aweke if (src_sec_state == SMC_FROM_SECURE) { 288319fb084SSoby Mathew WARN("RMMD: RMI invoked by secure world.\n"); 28977c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 29077c27753SZelalem Aweke } 29177c27753SZelalem Aweke 29277c27753SZelalem Aweke /* 29377c27753SZelalem Aweke * Forward an RMI call from the Normal world to the Realm world as it 29477c27753SZelalem Aweke * is. 29577c27753SZelalem Aweke */ 29677c27753SZelalem Aweke if (src_sec_state == SMC_FROM_NON_SECURE) { 297319fb084SSoby Mathew VERBOSE("RMMD: RMI call from non-secure world.\n"); 29811578303SSoby Mathew return rmmd_smc_forward(NON_SECURE, REALM, smc_fid, 29977c27753SZelalem Aweke x1, x2, x3, x4, handle); 30077c27753SZelalem Aweke } 30177c27753SZelalem Aweke 302319fb084SSoby Mathew if (src_sec_state != SMC_FROM_REALM) { 303319fb084SSoby Mathew SMC_RET1(handle, SMC_UNK); 304319fb084SSoby Mathew } 30577c27753SZelalem Aweke 30677c27753SZelalem Aweke switch (smc_fid) { 307319fb084SSoby Mathew case RMMD_RMI_REQ_COMPLETE: 30811578303SSoby Mathew return rmmd_smc_forward(REALM, NON_SECURE, x1, 30977c27753SZelalem Aweke x2, x3, x4, 0, handle); 31077c27753SZelalem Aweke 31177c27753SZelalem Aweke default: 312319fb084SSoby Mathew WARN("RMMD: Unsupported RMM call 0x%08x\n", smc_fid); 31377c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 31477c27753SZelalem Aweke } 31577c27753SZelalem Aweke } 31677c27753SZelalem Aweke 31777c27753SZelalem Aweke /******************************************************************************* 31877c27753SZelalem Aweke * This cpu has been turned on. Enter RMM to initialise R-EL2. Entry into RMM 31977c27753SZelalem Aweke * is done after initialising minimal architectural state that guarantees safe 32077c27753SZelalem Aweke * execution. 32177c27753SZelalem Aweke ******************************************************************************/ 32277c27753SZelalem Aweke static void *rmmd_cpu_on_finish_handler(const void *arg) 32377c27753SZelalem Aweke { 324*8c980a4aSJavier Almansa Sobrino long rc; 32577c27753SZelalem Aweke uint32_t linear_id = plat_my_core_pos(); 32677c27753SZelalem Aweke rmmd_rmm_context_t *ctx = &rmm_context[linear_id]; 32777c27753SZelalem Aweke 328*8c980a4aSJavier Almansa Sobrino if (rmm_boot_failed) { 329*8c980a4aSJavier Almansa Sobrino /* RMM Boot failed on a previous CPU. Abort. */ 330*8c980a4aSJavier Almansa Sobrino ERROR("RMM Failed to initialize. Ignoring for CPU%d\n", 331*8c980a4aSJavier Almansa Sobrino linear_id); 332*8c980a4aSJavier Almansa Sobrino return NULL; 333*8c980a4aSJavier Almansa Sobrino } 334*8c980a4aSJavier Almansa Sobrino 335*8c980a4aSJavier Almansa Sobrino /* 336*8c980a4aSJavier Almansa Sobrino * Prepare warmboot arguments for RMM: 337*8c980a4aSJavier Almansa Sobrino * arg0: This CPUID. 338*8c980a4aSJavier Almansa Sobrino * arg1 to arg3: Not used. 339*8c980a4aSJavier Almansa Sobrino */ 340*8c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg0 = linear_id; 341*8c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg1 = 0ULL; 342*8c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg2 = 0ULL; 343*8c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg3 = 0ULL; 34477c27753SZelalem Aweke 34577c27753SZelalem Aweke /* Initialise RMM context with this entry point information */ 34677c27753SZelalem Aweke cm_setup_context(&ctx->cpu_ctx, rmm_ep_info); 34777c27753SZelalem Aweke 348a4cc85c1SSubhasish Ghosh /* Enable architecture extensions */ 349a4cc85c1SSubhasish Ghosh manage_extensions_realm(&ctx->cpu_ctx); 350a4cc85c1SSubhasish Ghosh 35177c27753SZelalem Aweke /* Initialize RMM EL2 context. */ 35277c27753SZelalem Aweke rmm_el2_context_init(&ctx->cpu_ctx.el2_sysregs_ctx); 35377c27753SZelalem Aweke 35477c27753SZelalem Aweke rc = rmmd_rmm_sync_entry(ctx); 355*8c980a4aSJavier Almansa Sobrino 356*8c980a4aSJavier Almansa Sobrino if (rc != E_RMM_BOOT_SUCCESS) { 357*8c980a4aSJavier Almansa Sobrino ERROR("RMM init failed on CPU%d: %ld\n", linear_id, rc); 358*8c980a4aSJavier Almansa Sobrino /* Mark the boot as failed for any other booting CPU */ 359*8c980a4aSJavier Almansa Sobrino rmm_boot_failed = true; 36077c27753SZelalem Aweke } 36177c27753SZelalem Aweke 36277c27753SZelalem Aweke return NULL; 36377c27753SZelalem Aweke } 36477c27753SZelalem Aweke 36577c27753SZelalem Aweke /* Subscribe to PSCI CPU on to initialize RMM on secondary */ 36677c27753SZelalem Aweke SUBSCRIBE_TO_EVENT(psci_cpu_on_finish, rmmd_cpu_on_finish_handler); 36777c27753SZelalem Aweke 368319fb084SSoby Mathew /* Convert GPT lib error to RMMD GTS error */ 369319fb084SSoby Mathew static int gpt_to_gts_error(int error, uint32_t smc_fid, uint64_t address) 370319fb084SSoby Mathew { 371319fb084SSoby Mathew int ret; 372319fb084SSoby Mathew 373319fb084SSoby Mathew if (error == 0) { 374319fb084SSoby Mathew return RMMD_OK; 375319fb084SSoby Mathew } 376319fb084SSoby Mathew 377319fb084SSoby Mathew if (error == -EINVAL) { 378319fb084SSoby Mathew ret = RMMD_ERR_BAD_ADDR; 379319fb084SSoby Mathew } else { 380319fb084SSoby Mathew /* This is the only other error code we expect */ 381319fb084SSoby Mathew assert(error == -EPERM); 382319fb084SSoby Mathew ret = RMMD_ERR_BAD_PAS; 383319fb084SSoby Mathew } 384319fb084SSoby Mathew 385319fb084SSoby Mathew ERROR("RMMD: PAS Transition failed. GPT ret = %d, PA: 0x%"PRIx64 ", FID = 0x%x\n", 386319fb084SSoby Mathew error, address, smc_fid); 387319fb084SSoby Mathew return ret; 388319fb084SSoby Mathew } 389319fb084SSoby Mathew 39077c27753SZelalem Aweke /******************************************************************************* 391319fb084SSoby Mathew * This function handles RMM-EL3 interface SMCs 39277c27753SZelalem Aweke ******************************************************************************/ 393319fb084SSoby Mathew uint64_t rmmd_rmm_el3_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, 39477c27753SZelalem Aweke uint64_t x3, uint64_t x4, void *cookie, 39577c27753SZelalem Aweke void *handle, uint64_t flags) 39677c27753SZelalem Aweke { 39777c27753SZelalem Aweke uint32_t src_sec_state; 3986a00e9b0SRobert Wakim int ret; 39977c27753SZelalem Aweke 400*8c980a4aSJavier Almansa Sobrino /* If RMM failed to boot, treat any RMM-EL3 interface SMC as unknown */ 401*8c980a4aSJavier Almansa Sobrino if (rmm_boot_failed) { 402*8c980a4aSJavier Almansa Sobrino WARN("RMMD: Failed to boot up RMM. Ignoring RMM-EL3 call\n"); 403*8c980a4aSJavier Almansa Sobrino SMC_RET1(handle, SMC_UNK); 404*8c980a4aSJavier Almansa Sobrino } 405*8c980a4aSJavier Almansa Sobrino 40677c27753SZelalem Aweke /* Determine which security state this SMC originated from */ 40777c27753SZelalem Aweke src_sec_state = caller_sec_state(flags); 40877c27753SZelalem Aweke 40977c27753SZelalem Aweke if (src_sec_state != SMC_FROM_REALM) { 410319fb084SSoby Mathew WARN("RMMD: RMM-EL3 call originated from secure or normal world\n"); 41177c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 41277c27753SZelalem Aweke } 41377c27753SZelalem Aweke 41477c27753SZelalem Aweke switch (smc_fid) { 415319fb084SSoby Mathew case RMMD_GTSI_DELEGATE: 4166a00e9b0SRobert Wakim ret = gpt_delegate_pas(x1, PAGE_SIZE_4KB, SMC_FROM_REALM); 417319fb084SSoby Mathew SMC_RET1(handle, gpt_to_gts_error(ret, smc_fid, x1)); 418319fb084SSoby Mathew case RMMD_GTSI_UNDELEGATE: 4196a00e9b0SRobert Wakim ret = gpt_undelegate_pas(x1, PAGE_SIZE_4KB, SMC_FROM_REALM); 420319fb084SSoby Mathew SMC_RET1(handle, gpt_to_gts_error(ret, smc_fid, x1)); 4210f9159b7SSoby Mathew case RMMD_ATTEST_GET_PLAT_TOKEN: 4220f9159b7SSoby Mathew ret = rmmd_attest_get_platform_token(x1, &x2, x3); 4230f9159b7SSoby Mathew SMC_RET2(handle, ret, x2); 424a0435105SSoby Mathew case RMMD_ATTEST_GET_REALM_KEY: 425a0435105SSoby Mathew ret = rmmd_attest_get_signing_key(x1, &x2, x3); 426a0435105SSoby Mathew SMC_RET2(handle, ret, x2); 427*8c980a4aSJavier Almansa Sobrino 428*8c980a4aSJavier Almansa Sobrino case RMM_BOOT_COMPLETE: 429*8c980a4aSJavier Almansa Sobrino VERBOSE("RMMD: running rmmd_rmm_sync_exit\n"); 430*8c980a4aSJavier Almansa Sobrino rmmd_rmm_sync_exit(x1); 431*8c980a4aSJavier Almansa Sobrino 43277c27753SZelalem Aweke default: 433319fb084SSoby Mathew WARN("RMMD: Unsupported RMM-EL3 call 0x%08x\n", smc_fid); 43477c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 43577c27753SZelalem Aweke } 43677c27753SZelalem Aweke } 437