177c27753SZelalem Aweke /* 21975d28bSSona Mathew * Copyright (c) 2021-2025, 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> 20461c0a5dSElizabeth Ho #include <lib/el3_runtime/cpu_data.h> 2177c27753SZelalem Aweke #include <lib/el3_runtime/pubsub.h> 22d048af0dSJavier Almansa Sobrino #include <lib/extensions/mpam.h> 23c73686a1SBoyan Karatotev #include <lib/extensions/pmuv3.h> 24c73686a1SBoyan Karatotev #include <lib/extensions/sys_reg_trace.h> 25f19dc624Sjohpow01 #include <lib/gpt_rme/gpt_rme.h> 2677c27753SZelalem Aweke 2777c27753SZelalem Aweke #include <lib/spinlock.h> 2877c27753SZelalem Aweke #include <lib/utils.h> 2977c27753SZelalem Aweke #include <lib/xlat_tables/xlat_tables_v2.h> 3077c27753SZelalem Aweke #include <plat/common/common_def.h> 3177c27753SZelalem Aweke #include <plat/common/platform.h> 3277c27753SZelalem Aweke #include <platform_def.h> 3377c27753SZelalem Aweke #include <services/rmmd_svc.h> 3477c27753SZelalem Aweke #include <smccc_helpers.h> 35f92eb7e2SArunachalam Ganapathy #include <lib/extensions/sme.h> 36a4cc85c1SSubhasish Ghosh #include <lib/extensions/sve.h> 3779c0c7faSBoyan Karatotev #include <lib/extensions/spe.h> 3879c0c7faSBoyan Karatotev #include <lib/extensions/trbe.h> 3977c27753SZelalem Aweke #include "rmmd_private.h" 4077c27753SZelalem Aweke 4177c27753SZelalem Aweke /******************************************************************************* 428c980a4aSJavier Almansa Sobrino * RMM boot failure flag 438c980a4aSJavier Almansa Sobrino ******************************************************************************/ 448c980a4aSJavier Almansa Sobrino static bool rmm_boot_failed; 458c980a4aSJavier Almansa Sobrino 468c980a4aSJavier 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_el2_sysregs_context_restore(REALM); 7677c27753SZelalem Aweke cm_set_next_eret_context(REALM); 7777c27753SZelalem Aweke 7877c27753SZelalem Aweke /* Enter RMM */ 7977c27753SZelalem Aweke rc = rmmd_rmm_enter(&rmm_ctx->c_rt_ctx); 8077c27753SZelalem Aweke 818b95e848SZelalem Aweke /* 82e58daa66SJayanth Dodderi Chidanand * Save realm context. EL2 Non-secure context will be restored 83e58daa66SJayanth Dodderi Chidanand * before exiting Non-secure world, therefore there is no need 84e58daa66SJayanth Dodderi Chidanand * to clear EL2 context registers. 858b95e848SZelalem Aweke */ 8677c27753SZelalem Aweke cm_el2_sysregs_context_save(REALM); 8777c27753SZelalem Aweke 8877c27753SZelalem Aweke return rc; 8977c27753SZelalem Aweke } 9077c27753SZelalem Aweke 9177c27753SZelalem Aweke /******************************************************************************* 9277c27753SZelalem Aweke * This function returns to the place where rmmd_rmm_sync_entry() was 9377c27753SZelalem Aweke * called originally. 9477c27753SZelalem Aweke ******************************************************************************/ 9577c27753SZelalem Aweke __dead2 void rmmd_rmm_sync_exit(uint64_t rc) 9677c27753SZelalem Aweke { 9777c27753SZelalem Aweke rmmd_rmm_context_t *ctx = &rmm_context[plat_my_core_pos()]; 9877c27753SZelalem Aweke 9977c27753SZelalem Aweke /* Get context of the RMM in use by this CPU. */ 10077c27753SZelalem Aweke assert(cm_get_context(REALM) == &(ctx->cpu_ctx)); 10177c27753SZelalem Aweke 10277c27753SZelalem Aweke /* 10377c27753SZelalem Aweke * The RMMD must have initiated the original request through a 10477c27753SZelalem Aweke * synchronous entry into RMM. Jump back to the original C runtime 10577c27753SZelalem Aweke * context with the value of rc in x0; 10677c27753SZelalem Aweke */ 10777c27753SZelalem Aweke rmmd_rmm_exit(ctx->c_rt_ctx, rc); 10877c27753SZelalem Aweke 10977c27753SZelalem Aweke panic(); 11077c27753SZelalem Aweke } 11177c27753SZelalem Aweke 112a4cc85c1SSubhasish Ghosh /******************************************************************************* 11377c27753SZelalem Aweke * Jump to the RMM for the first time. 11477c27753SZelalem Aweke ******************************************************************************/ 11577c27753SZelalem Aweke static int32_t rmm_init(void) 11677c27753SZelalem Aweke { 1178c980a4aSJavier Almansa Sobrino long rc; 11877c27753SZelalem Aweke rmmd_rmm_context_t *ctx = &rmm_context[plat_my_core_pos()]; 11977c27753SZelalem Aweke 12077c27753SZelalem Aweke INFO("RMM init start.\n"); 12177c27753SZelalem Aweke 12277c27753SZelalem Aweke rc = rmmd_rmm_sync_entry(ctx); 1238c980a4aSJavier Almansa Sobrino if (rc != E_RMM_BOOT_SUCCESS) { 1248c980a4aSJavier Almansa Sobrino ERROR("RMM init failed: %ld\n", rc); 1258c980a4aSJavier Almansa Sobrino /* Mark the boot as failed for all the CPUs */ 1268c980a4aSJavier Almansa Sobrino rmm_boot_failed = true; 1278c980a4aSJavier Almansa Sobrino return 0; 12877c27753SZelalem Aweke } 12977c27753SZelalem Aweke 13077c27753SZelalem Aweke INFO("RMM init end.\n"); 13177c27753SZelalem Aweke 13277c27753SZelalem Aweke return 1; 13377c27753SZelalem Aweke } 13477c27753SZelalem Aweke 13577c27753SZelalem Aweke /******************************************************************************* 13677c27753SZelalem Aweke * Load and read RMM manifest, setup RMM. 13777c27753SZelalem Aweke ******************************************************************************/ 13877c27753SZelalem Aweke int rmmd_setup(void) 13977c27753SZelalem Aweke { 140dc65ae46SJavier Almansa Sobrino size_t shared_buf_size __unused; 141dc65ae46SJavier Almansa Sobrino uintptr_t shared_buf_base; 14277c27753SZelalem Aweke uint32_t ep_attr; 14377c27753SZelalem Aweke unsigned int linear_id = plat_my_core_pos(); 14477c27753SZelalem Aweke rmmd_rmm_context_t *rmm_ctx = &rmm_context[linear_id]; 145a97bfa5fSAlexeiFedorov struct rmm_manifest *manifest; 1461d0ca40eSJavier Almansa Sobrino int rc; 14777c27753SZelalem Aweke 14877c27753SZelalem Aweke /* Make sure RME is supported. */ 149eacbef4cSVarun Wadekar if (is_feat_rme_present() == 0U) { 150eacbef4cSVarun Wadekar /* Mark the RMM boot as failed for all the CPUs */ 151eacbef4cSVarun Wadekar rmm_boot_failed = true; 152eacbef4cSVarun Wadekar return -ENOTSUP; 153eacbef4cSVarun Wadekar } 15477c27753SZelalem Aweke 15577c27753SZelalem Aweke rmm_ep_info = bl31_plat_get_next_image_ep_info(REALM); 1568cb9c635SVarun Wadekar if ((rmm_ep_info == NULL) || (rmm_ep_info->pc == 0)) { 15777c27753SZelalem Aweke WARN("No RMM image provided by BL2 boot loader, Booting " 15877c27753SZelalem Aweke "device without RMM initialization. SMCs destined for " 15977c27753SZelalem Aweke "RMM will return SMC_UNK\n"); 160eacbef4cSVarun Wadekar 161adcd74caSVarun Wadekar /* Mark the boot as failed for all the CPUs */ 162adcd74caSVarun Wadekar rmm_boot_failed = true; 16377c27753SZelalem Aweke return -ENOENT; 16477c27753SZelalem Aweke } 16577c27753SZelalem Aweke 16677c27753SZelalem Aweke /* Initialise an entrypoint to set up the CPU context */ 16777c27753SZelalem Aweke ep_attr = EP_REALM; 16877c27753SZelalem Aweke if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0U) { 16977c27753SZelalem Aweke ep_attr |= EP_EE_BIG; 17077c27753SZelalem Aweke } 17177c27753SZelalem Aweke 17277c27753SZelalem Aweke SET_PARAM_HEAD(rmm_ep_info, PARAM_EP, VERSION_1, ep_attr); 17377c27753SZelalem Aweke rmm_ep_info->spsr = SPSR_64(MODE_EL2, 17477c27753SZelalem Aweke MODE_SP_ELX, 17577c27753SZelalem Aweke DISABLE_ALL_EXCEPTIONS); 17677c27753SZelalem Aweke 1778c980a4aSJavier Almansa Sobrino shared_buf_size = 1788c980a4aSJavier Almansa Sobrino plat_rmmd_get_el3_rmm_shared_mem(&shared_buf_base); 1798c980a4aSJavier Almansa Sobrino 1808c980a4aSJavier Almansa Sobrino assert((shared_buf_size == SZ_4K) && 1818c980a4aSJavier Almansa Sobrino ((void *)shared_buf_base != NULL)); 1828c980a4aSJavier Almansa Sobrino 18332904472SSoby Mathew /* Zero out and load the boot manifest at the beginning of the share area */ 184a97bfa5fSAlexeiFedorov manifest = (struct rmm_manifest *)shared_buf_base; 18583a4e8e0SHarry Moulton (void)memset((void *)manifest, 0, sizeof(struct rmm_manifest)); 18632904472SSoby Mathew 1871d0ca40eSJavier Almansa Sobrino rc = plat_rmmd_load_manifest(manifest); 1881d0ca40eSJavier Almansa Sobrino if (rc != 0) { 1891d0ca40eSJavier Almansa Sobrino ERROR("Error loading RMM Boot Manifest (%i)\n", rc); 1900c707813SVarun Wadekar /* Mark the boot as failed for all the CPUs */ 1910c707813SVarun Wadekar rmm_boot_failed = true; 1921d0ca40eSJavier Almansa Sobrino return rc; 1931d0ca40eSJavier Almansa Sobrino } 1941d0ca40eSJavier Almansa Sobrino flush_dcache_range((uintptr_t)shared_buf_base, shared_buf_size); 1951d0ca40eSJavier Almansa Sobrino 1968c980a4aSJavier Almansa Sobrino /* 1978c980a4aSJavier Almansa Sobrino * Prepare coldboot arguments for RMM: 1988c980a4aSJavier Almansa Sobrino * arg0: This CPUID (primary processor). 1998c980a4aSJavier Almansa Sobrino * arg1: Version for this Boot Interface. 2008c980a4aSJavier Almansa Sobrino * arg2: PLATFORM_CORE_COUNT. 2018c980a4aSJavier Almansa Sobrino * arg3: Base address for the EL3 <-> RMM shared area. The boot 2028c980a4aSJavier Almansa Sobrino * manifest will be stored at the beginning of this area. 20389d979ceSAndre Przywara * arg4: opaque activation token, as returned by previous calls 2048c980a4aSJavier Almansa Sobrino */ 2058c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg0 = linear_id; 2068c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg1 = RMM_EL3_INTERFACE_VERSION; 2078c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg2 = PLATFORM_CORE_COUNT; 2088c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg3 = shared_buf_base; 20989d979ceSAndre Przywara rmm_ep_info->args.arg4 = rmm_ctx->activation_token; 2108c980a4aSJavier Almansa Sobrino 21177c27753SZelalem Aweke /* Initialise RMM context with this entry point information */ 21277c27753SZelalem Aweke cm_setup_context(&rmm_ctx->cpu_ctx, rmm_ep_info); 21377c27753SZelalem Aweke 21477c27753SZelalem Aweke INFO("RMM setup done.\n"); 21577c27753SZelalem Aweke 21677c27753SZelalem Aweke /* Register init function for deferred init. */ 21777c27753SZelalem Aweke bl31_register_rmm_init(&rmm_init); 21877c27753SZelalem Aweke 21977c27753SZelalem Aweke return 0; 22077c27753SZelalem Aweke } 22177c27753SZelalem Aweke 22277c27753SZelalem Aweke /******************************************************************************* 22377c27753SZelalem Aweke * Forward SMC to the other security state 22477c27753SZelalem Aweke ******************************************************************************/ 22511578303SSoby Mathew static uint64_t rmmd_smc_forward(uint32_t src_sec_state, 22611578303SSoby Mathew uint32_t dst_sec_state, uint64_t x0, 22711578303SSoby Mathew uint64_t x1, uint64_t x2, uint64_t x3, 22811578303SSoby Mathew uint64_t x4, void *handle) 22977c27753SZelalem Aweke { 2308e51cccaSAlexeiFedorov cpu_context_t *ctx = cm_get_context(dst_sec_state); 2318e51cccaSAlexeiFedorov 23277c27753SZelalem Aweke /* Save incoming security state */ 23377c27753SZelalem Aweke cm_el2_sysregs_context_save(src_sec_state); 23477c27753SZelalem Aweke 23577c27753SZelalem Aweke /* Restore outgoing security state */ 23677c27753SZelalem Aweke cm_el2_sysregs_context_restore(dst_sec_state); 23777c27753SZelalem Aweke cm_set_next_eret_context(dst_sec_state); 23877c27753SZelalem Aweke 23911578303SSoby Mathew /* 2408e51cccaSAlexeiFedorov * As per SMCCCv1.2, we need to preserve x4 to x7 unless 24111578303SSoby Mathew * being used as return args. Hence we differentiate the 24211578303SSoby Mathew * onward and backward path. Support upto 8 args in the 24311578303SSoby Mathew * onward path and 4 args in return path. 2448e51cccaSAlexeiFedorov * Register x4 will be preserved by RMM in case it is not 2458e51cccaSAlexeiFedorov * used in return path. 24611578303SSoby Mathew */ 24711578303SSoby Mathew if (src_sec_state == NON_SECURE) { 2488e51cccaSAlexeiFedorov SMC_RET8(ctx, x0, x1, x2, x3, x4, 24977c27753SZelalem Aweke SMC_GET_GP(handle, CTX_GPREG_X5), 25077c27753SZelalem Aweke SMC_GET_GP(handle, CTX_GPREG_X6), 25177c27753SZelalem Aweke SMC_GET_GP(handle, CTX_GPREG_X7)); 25211578303SSoby Mathew } 2538e51cccaSAlexeiFedorov 2548e51cccaSAlexeiFedorov SMC_RET5(ctx, x0, x1, x2, x3, x4); 25577c27753SZelalem Aweke } 25677c27753SZelalem Aweke 25777c27753SZelalem Aweke /******************************************************************************* 25877c27753SZelalem Aweke * This function handles all SMCs in the range reserved for RMI. Each call is 25977c27753SZelalem Aweke * either forwarded to the other security state or handled by the RMM dispatcher 26077c27753SZelalem Aweke ******************************************************************************/ 26177c27753SZelalem Aweke uint64_t rmmd_rmi_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, 26277c27753SZelalem Aweke uint64_t x3, uint64_t x4, void *cookie, 26377c27753SZelalem Aweke void *handle, uint64_t flags) 26477c27753SZelalem Aweke { 26577c27753SZelalem Aweke uint32_t src_sec_state; 26677c27753SZelalem Aweke 2678c980a4aSJavier Almansa Sobrino /* If RMM failed to boot, treat any RMI SMC as unknown */ 2688c980a4aSJavier Almansa Sobrino if (rmm_boot_failed) { 2698c980a4aSJavier Almansa Sobrino WARN("RMMD: Failed to boot up RMM. Ignoring RMI call\n"); 2708c980a4aSJavier Almansa Sobrino SMC_RET1(handle, SMC_UNK); 2718c980a4aSJavier Almansa Sobrino } 2728c980a4aSJavier Almansa Sobrino 27377c27753SZelalem Aweke /* Determine which security state this SMC originated from */ 27477c27753SZelalem Aweke src_sec_state = caller_sec_state(flags); 27577c27753SZelalem Aweke 27677c27753SZelalem Aweke /* RMI must not be invoked by the Secure world */ 27777c27753SZelalem Aweke if (src_sec_state == SMC_FROM_SECURE) { 278319fb084SSoby Mathew WARN("RMMD: RMI invoked by secure world.\n"); 27977c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 28077c27753SZelalem Aweke } 28177c27753SZelalem Aweke 28277c27753SZelalem Aweke /* 28377c27753SZelalem Aweke * Forward an RMI call from the Normal world to the Realm world as it 28477c27753SZelalem Aweke * is. 28577c27753SZelalem Aweke */ 28677c27753SZelalem Aweke if (src_sec_state == SMC_FROM_NON_SECURE) { 28767889630SArunachalam Ganapathy /* 28867889630SArunachalam Ganapathy * If SVE hint bit is set in the flags then update the SMC 28967889630SArunachalam Ganapathy * function id and pass it on to the lower EL. 29067889630SArunachalam Ganapathy */ 29167889630SArunachalam Ganapathy if (is_sve_hint_set(flags)) { 29267889630SArunachalam Ganapathy smc_fid |= (FUNCID_SVE_HINT_MASK << 29367889630SArunachalam Ganapathy FUNCID_SVE_HINT_SHIFT); 29467889630SArunachalam Ganapathy } 295319fb084SSoby Mathew VERBOSE("RMMD: RMI call from non-secure world.\n"); 29611578303SSoby Mathew return rmmd_smc_forward(NON_SECURE, REALM, smc_fid, 29777c27753SZelalem Aweke x1, x2, x3, x4, handle); 29877c27753SZelalem Aweke } 29977c27753SZelalem Aweke 300319fb084SSoby Mathew if (src_sec_state != SMC_FROM_REALM) { 301319fb084SSoby Mathew SMC_RET1(handle, SMC_UNK); 302319fb084SSoby Mathew } 30377c27753SZelalem Aweke 30477c27753SZelalem Aweke switch (smc_fid) { 3058e51cccaSAlexeiFedorov case RMM_RMI_REQ_COMPLETE: { 3068e51cccaSAlexeiFedorov uint64_t x5 = SMC_GET_GP(handle, CTX_GPREG_X5); 30777c27753SZelalem Aweke 3088e51cccaSAlexeiFedorov return rmmd_smc_forward(REALM, NON_SECURE, x1, 3098e51cccaSAlexeiFedorov x2, x3, x4, x5, handle); 3108e51cccaSAlexeiFedorov } 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 { 3248c980a4aSJavier 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 3288c980a4aSJavier Almansa Sobrino if (rmm_boot_failed) { 3298c980a4aSJavier Almansa Sobrino /* RMM Boot failed on a previous CPU. Abort. */ 3308c980a4aSJavier Almansa Sobrino ERROR("RMM Failed to initialize. Ignoring for CPU%d\n", 3318c980a4aSJavier Almansa Sobrino linear_id); 3328c980a4aSJavier Almansa Sobrino return NULL; 3338c980a4aSJavier Almansa Sobrino } 3348c980a4aSJavier Almansa Sobrino 3358c980a4aSJavier Almansa Sobrino /* 3368c980a4aSJavier Almansa Sobrino * Prepare warmboot arguments for RMM: 3378c980a4aSJavier Almansa Sobrino * arg0: This CPUID. 33889d979ceSAndre Przywara * arg1: opaque activation token, as returned by previous calls 33989d979ceSAndre Przywara * arg2 to arg3: Not used. 3408c980a4aSJavier Almansa Sobrino */ 3418c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg0 = linear_id; 34289d979ceSAndre Przywara rmm_ep_info->args.arg1 = ctx->activation_token; 3438c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg2 = 0ULL; 3448c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg3 = 0ULL; 34577c27753SZelalem Aweke 34677c27753SZelalem Aweke /* Initialise RMM context with this entry point information */ 34777c27753SZelalem Aweke cm_setup_context(&ctx->cpu_ctx, rmm_ep_info); 34877c27753SZelalem Aweke 34977c27753SZelalem Aweke rc = rmmd_rmm_sync_entry(ctx); 3508c980a4aSJavier Almansa Sobrino 3518c980a4aSJavier Almansa Sobrino if (rc != E_RMM_BOOT_SUCCESS) { 3528c980a4aSJavier Almansa Sobrino ERROR("RMM init failed on CPU%d: %ld\n", linear_id, rc); 3538c980a4aSJavier Almansa Sobrino /* Mark the boot as failed for any other booting CPU */ 3548c980a4aSJavier Almansa Sobrino rmm_boot_failed = true; 35577c27753SZelalem Aweke } 35677c27753SZelalem Aweke 35777c27753SZelalem Aweke return NULL; 35877c27753SZelalem Aweke } 35977c27753SZelalem Aweke 36077c27753SZelalem Aweke /* Subscribe to PSCI CPU on to initialize RMM on secondary */ 36177c27753SZelalem Aweke SUBSCRIBE_TO_EVENT(psci_cpu_on_finish, rmmd_cpu_on_finish_handler); 36277c27753SZelalem Aweke 363319fb084SSoby Mathew /* Convert GPT lib error to RMMD GTS error */ 364319fb084SSoby Mathew static int gpt_to_gts_error(int error, uint32_t smc_fid, uint64_t address) 365319fb084SSoby Mathew { 366319fb084SSoby Mathew int ret; 367319fb084SSoby Mathew 368319fb084SSoby Mathew if (error == 0) { 369dc65ae46SJavier Almansa Sobrino return E_RMM_OK; 370319fb084SSoby Mathew } 371319fb084SSoby Mathew 372319fb084SSoby Mathew if (error == -EINVAL) { 373dc65ae46SJavier Almansa Sobrino ret = E_RMM_BAD_ADDR; 374319fb084SSoby Mathew } else { 375319fb084SSoby Mathew /* This is the only other error code we expect */ 376319fb084SSoby Mathew assert(error == -EPERM); 377dc65ae46SJavier Almansa Sobrino ret = E_RMM_BAD_PAS; 378319fb084SSoby Mathew } 379319fb084SSoby Mathew 380319fb084SSoby Mathew ERROR("RMMD: PAS Transition failed. GPT ret = %d, PA: 0x%"PRIx64 ", FID = 0x%x\n", 381319fb084SSoby Mathew error, address, smc_fid); 382319fb084SSoby Mathew return ret; 383319fb084SSoby Mathew } 384319fb084SSoby Mathew 3856a88ec8bSRaghu Krishnamurthy static int rmm_el3_ifc_get_feat_register(uint64_t feat_reg_idx, 3866a88ec8bSRaghu Krishnamurthy uint64_t *feat_reg) 3876a88ec8bSRaghu Krishnamurthy { 3886a88ec8bSRaghu Krishnamurthy if (feat_reg_idx != RMM_EL3_FEAT_REG_0_IDX) { 3896a88ec8bSRaghu Krishnamurthy ERROR("RMMD: Failed to get feature register %ld\n", feat_reg_idx); 3906a88ec8bSRaghu Krishnamurthy return E_RMM_INVAL; 3916a88ec8bSRaghu Krishnamurthy } 3926a88ec8bSRaghu Krishnamurthy 3936a88ec8bSRaghu Krishnamurthy *feat_reg = 0UL; 3946a88ec8bSRaghu Krishnamurthy #if RMMD_ENABLE_EL3_TOKEN_SIGN 3956a88ec8bSRaghu Krishnamurthy *feat_reg |= RMM_EL3_FEAT_REG_0_EL3_TOKEN_SIGN_MASK; 3966a88ec8bSRaghu Krishnamurthy #endif 3976a88ec8bSRaghu Krishnamurthy return E_RMM_OK; 3986a88ec8bSRaghu Krishnamurthy } 3996a88ec8bSRaghu Krishnamurthy 400f801fdc2STushar Khandelwal /* 401f801fdc2STushar Khandelwal * Update encryption key associated with @mecid. 402f801fdc2STushar Khandelwal */ 403f801fdc2STushar Khandelwal static int rmmd_mecid_key_update(uint64_t mecid) 404f801fdc2STushar Khandelwal { 405f801fdc2STushar Khandelwal uint64_t mecid_width, mecid_width_mask; 406f801fdc2STushar Khandelwal int ret; 407f801fdc2STushar Khandelwal 408f801fdc2STushar Khandelwal /* 409609ada96SJuan Pablo Conde * Check whether FEAT_MEC is supported by the hardware. If not, return 410609ada96SJuan Pablo Conde * unknown SMC. 411609ada96SJuan Pablo Conde */ 412609ada96SJuan Pablo Conde if (is_feat_mec_supported() == false) { 413609ada96SJuan Pablo Conde return E_RMM_UNK; 414609ada96SJuan Pablo Conde } 415609ada96SJuan Pablo Conde 416609ada96SJuan Pablo Conde /* 417f801fdc2STushar Khandelwal * Check whether the mecid parameter is at most MECIDR_EL2.MECIDWidthm1 + 1 418f801fdc2STushar Khandelwal * in length. 419f801fdc2STushar Khandelwal */ 420f801fdc2STushar Khandelwal mecid_width = ((read_mecidr_el2() >> MECIDR_EL2_MECIDWidthm1_SHIFT) & 421f801fdc2STushar Khandelwal MECIDR_EL2_MECIDWidthm1_MASK) + 1; 422f801fdc2STushar Khandelwal mecid_width_mask = ((1 << mecid_width) - 1); 423f801fdc2STushar Khandelwal if ((mecid & ~mecid_width_mask) != 0U) { 424f801fdc2STushar Khandelwal return E_RMM_INVAL; 425f801fdc2STushar Khandelwal } 426f801fdc2STushar Khandelwal 427f801fdc2STushar Khandelwal ret = plat_rmmd_mecid_key_update(mecid); 428f801fdc2STushar Khandelwal 429f801fdc2STushar Khandelwal if (ret != 0) { 430f801fdc2STushar Khandelwal return E_RMM_UNK; 431f801fdc2STushar Khandelwal } 432f801fdc2STushar Khandelwal return E_RMM_OK; 433f801fdc2STushar Khandelwal } 434f801fdc2STushar Khandelwal 43577c27753SZelalem Aweke /******************************************************************************* 436319fb084SSoby Mathew * This function handles RMM-EL3 interface SMCs 43777c27753SZelalem Aweke ******************************************************************************/ 438319fb084SSoby Mathew uint64_t rmmd_rmm_el3_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, 43977c27753SZelalem Aweke uint64_t x3, uint64_t x4, void *cookie, 44077c27753SZelalem Aweke void *handle, uint64_t flags) 44177c27753SZelalem Aweke { 4426a88ec8bSRaghu Krishnamurthy uint64_t remaining_len = 0UL; 44377c27753SZelalem Aweke uint32_t src_sec_state; 4446a00e9b0SRobert Wakim int ret; 44577c27753SZelalem Aweke 4468c980a4aSJavier Almansa Sobrino /* If RMM failed to boot, treat any RMM-EL3 interface SMC as unknown */ 4478c980a4aSJavier Almansa Sobrino if (rmm_boot_failed) { 4488c980a4aSJavier Almansa Sobrino WARN("RMMD: Failed to boot up RMM. Ignoring RMM-EL3 call\n"); 4498c980a4aSJavier Almansa Sobrino SMC_RET1(handle, SMC_UNK); 4508c980a4aSJavier Almansa Sobrino } 4518c980a4aSJavier Almansa Sobrino 45277c27753SZelalem Aweke /* Determine which security state this SMC originated from */ 45377c27753SZelalem Aweke src_sec_state = caller_sec_state(flags); 45477c27753SZelalem Aweke 45577c27753SZelalem Aweke if (src_sec_state != SMC_FROM_REALM) { 456319fb084SSoby Mathew WARN("RMMD: RMM-EL3 call originated from secure or normal world\n"); 45777c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 45877c27753SZelalem Aweke } 45977c27753SZelalem Aweke 46077c27753SZelalem Aweke switch (smc_fid) { 461e50fedbcSJavier Almansa Sobrino case RMM_GTSI_DELEGATE: 4626a00e9b0SRobert Wakim ret = gpt_delegate_pas(x1, PAGE_SIZE_4KB, SMC_FROM_REALM); 463319fb084SSoby Mathew SMC_RET1(handle, gpt_to_gts_error(ret, smc_fid, x1)); 464e50fedbcSJavier Almansa Sobrino case RMM_GTSI_UNDELEGATE: 4656a00e9b0SRobert Wakim ret = gpt_undelegate_pas(x1, PAGE_SIZE_4KB, SMC_FROM_REALM); 466319fb084SSoby Mathew SMC_RET1(handle, gpt_to_gts_error(ret, smc_fid, x1)); 467e50fedbcSJavier Almansa Sobrino case RMM_ATTEST_GET_REALM_KEY: 468a0435105SSoby Mathew ret = rmmd_attest_get_signing_key(x1, &x2, x3); 469a0435105SSoby Mathew SMC_RET2(handle, ret, x2); 4701975d28bSSona Mathew case RMM_ATTEST_GET_PLAT_TOKEN: 4711975d28bSSona Mathew ret = rmmd_attest_get_platform_token(x1, &x2, x3, &remaining_len); 4721975d28bSSona Mathew SMC_RET3(handle, ret, x2, remaining_len); 4736a88ec8bSRaghu Krishnamurthy case RMM_EL3_FEATURES: 4746a88ec8bSRaghu Krishnamurthy ret = rmm_el3_ifc_get_feat_register(x1, &x2); 4756a88ec8bSRaghu Krishnamurthy SMC_RET2(handle, ret, x2); 4766a88ec8bSRaghu Krishnamurthy #if RMMD_ENABLE_EL3_TOKEN_SIGN 4776a88ec8bSRaghu Krishnamurthy case RMM_EL3_TOKEN_SIGN: 4786a88ec8bSRaghu Krishnamurthy return rmmd_el3_token_sign(handle, x1, x2, x3, x4); 4796a88ec8bSRaghu Krishnamurthy #endif 4802132c707SSona Mathew 4812132c707SSona Mathew #if RMMD_ENABLE_IDE_KEY_PROG 4822132c707SSona Mathew case RMM_IDE_KEY_PROG: 4832132c707SSona Mathew { 4842132c707SSona Mathew rp_ide_key_info_t ide_key_info; 4852132c707SSona Mathew 4862132c707SSona Mathew ide_key_info.keyqw0 = x4; 4872132c707SSona Mathew ide_key_info.keyqw1 = SMC_GET_GP(handle, CTX_GPREG_X5); 4882132c707SSona Mathew ide_key_info.keyqw2 = SMC_GET_GP(handle, CTX_GPREG_X6); 4892132c707SSona Mathew ide_key_info.keyqw3 = SMC_GET_GP(handle, CTX_GPREG_X7); 4902132c707SSona Mathew ide_key_info.ifvqw0 = SMC_GET_GP(handle, CTX_GPREG_X8); 4912132c707SSona Mathew ide_key_info.ifvqw1 = SMC_GET_GP(handle, CTX_GPREG_X9); 4922132c707SSona Mathew uint64_t x10 = SMC_GET_GP(handle, CTX_GPREG_X10); 4932132c707SSona Mathew uint64_t x11 = SMC_GET_GP(handle, CTX_GPREG_X11); 4942132c707SSona Mathew 4952132c707SSona Mathew ret = rmmd_el3_ide_key_program(x1, x2, x3, &ide_key_info, x10, x11); 4962132c707SSona Mathew SMC_RET1(handle, ret); 4972132c707SSona Mathew } 4982132c707SSona Mathew case RMM_IDE_KEY_SET_GO: 4992132c707SSona Mathew ret = rmmd_el3_ide_key_set_go(x1, x2, x3, x4, SMC_GET_GP(handle, CTX_GPREG_X5)); 5002132c707SSona Mathew SMC_RET1(handle, ret); 5012132c707SSona Mathew case RMM_IDE_KEY_SET_STOP: 5022132c707SSona Mathew ret = rmmd_el3_ide_key_set_stop(x1, x2, x3, x4, SMC_GET_GP(handle, CTX_GPREG_X5)); 5032132c707SSona Mathew SMC_RET1(handle, ret); 5042132c707SSona Mathew case RMM_IDE_KM_PULL_RESPONSE: { 5052132c707SSona Mathew uint64_t req_resp = 0, req_id = 0, cookie_var = 0; 5062132c707SSona Mathew 5072132c707SSona Mathew ret = rmmd_el3_ide_km_pull_response(x1, x2, &req_resp, &req_id, &cookie_var); 5082132c707SSona Mathew SMC_RET4(handle, ret, req_resp, req_id, cookie_var); 5092132c707SSona Mathew } 5102132c707SSona Mathew #endif /* RMMD_ENABLE_IDE_KEY_PROG */ 511*745c129aSAndre Przywara case RMM_RESERVE_MEMORY: 512*745c129aSAndre Przywara ret = rmmd_reserve_memory(x1, &x2); 513*745c129aSAndre Przywara SMC_RET2(handle, ret, x2); 514*745c129aSAndre Przywara 5158c980a4aSJavier Almansa Sobrino case RMM_BOOT_COMPLETE: 51689d979ceSAndre Przywara { 51789d979ceSAndre Przywara rmmd_rmm_context_t *ctx = &rmm_context[plat_my_core_pos()]; 51889d979ceSAndre Przywara 51989d979ceSAndre Przywara ctx->activation_token = x2; 5208c980a4aSJavier Almansa Sobrino VERBOSE("RMMD: running rmmd_rmm_sync_exit\n"); 5218c980a4aSJavier Almansa Sobrino rmmd_rmm_sync_exit(x1); 52289d979ceSAndre Przywara } 523f801fdc2STushar Khandelwal case RMM_MECID_KEY_UPDATE: 524f801fdc2STushar Khandelwal ret = rmmd_mecid_key_update(x1); 525f801fdc2STushar Khandelwal SMC_RET1(handle, ret); 52677c27753SZelalem Aweke default: 527319fb084SSoby Mathew WARN("RMMD: Unsupported RMM-EL3 call 0x%08x\n", smc_fid); 52877c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 52977c27753SZelalem Aweke } 53077c27753SZelalem Aweke } 531