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 4100e62ff9SJuan Pablo Conde #define MECID_SHIFT U(32) 4200e62ff9SJuan Pablo Conde #define MECID_MASK 0xFFFFU 4300e62ff9SJuan Pablo Conde 4400e62ff9SJuan Pablo Conde #define MEC_REFRESH_REASON_SHIFT U(0) 4500e62ff9SJuan Pablo Conde #define MEC_REFRESH_REASON_MASK BIT(0) 4600e62ff9SJuan Pablo Conde 4777c27753SZelalem Aweke /******************************************************************************* 488c980a4aSJavier Almansa Sobrino * RMM boot failure flag 498c980a4aSJavier Almansa Sobrino ******************************************************************************/ 508c980a4aSJavier Almansa Sobrino static bool rmm_boot_failed; 518c980a4aSJavier Almansa Sobrino 528c980a4aSJavier Almansa Sobrino /******************************************************************************* 5377c27753SZelalem Aweke * RMM context information. 5477c27753SZelalem Aweke ******************************************************************************/ 5577c27753SZelalem Aweke rmmd_rmm_context_t rmm_context[PLATFORM_CORE_COUNT]; 5677c27753SZelalem Aweke 5777c27753SZelalem Aweke /******************************************************************************* 5877c27753SZelalem Aweke * RMM entry point information. Discovered on the primary core and reused 5977c27753SZelalem Aweke * on secondary cores. 6077c27753SZelalem Aweke ******************************************************************************/ 6177c27753SZelalem Aweke static entry_point_info_t *rmm_ep_info; 6277c27753SZelalem Aweke 6377c27753SZelalem Aweke /******************************************************************************* 6477c27753SZelalem Aweke * Static function declaration. 6577c27753SZelalem Aweke ******************************************************************************/ 6677c27753SZelalem Aweke static int32_t rmm_init(void); 6777c27753SZelalem Aweke 6877c27753SZelalem Aweke /******************************************************************************* 6977c27753SZelalem Aweke * This function takes an RMM context pointer and performs a synchronous entry 7077c27753SZelalem Aweke * into it. 7177c27753SZelalem Aweke ******************************************************************************/ 7277c27753SZelalem Aweke uint64_t rmmd_rmm_sync_entry(rmmd_rmm_context_t *rmm_ctx) 7377c27753SZelalem Aweke { 7477c27753SZelalem Aweke uint64_t rc; 7577c27753SZelalem Aweke 7677c27753SZelalem Aweke assert(rmm_ctx != NULL); 7777c27753SZelalem Aweke 7877c27753SZelalem Aweke cm_set_context(&(rmm_ctx->cpu_ctx), REALM); 7977c27753SZelalem Aweke 8077c27753SZelalem Aweke /* Restore the realm context assigned above */ 8177c27753SZelalem Aweke cm_el2_sysregs_context_restore(REALM); 8277c27753SZelalem Aweke cm_set_next_eret_context(REALM); 8377c27753SZelalem Aweke 8477c27753SZelalem Aweke /* Enter RMM */ 8577c27753SZelalem Aweke rc = rmmd_rmm_enter(&rmm_ctx->c_rt_ctx); 8677c27753SZelalem Aweke 878b95e848SZelalem Aweke /* 88e58daa66SJayanth Dodderi Chidanand * Save realm context. EL2 Non-secure context will be restored 89e58daa66SJayanth Dodderi Chidanand * before exiting Non-secure world, therefore there is no need 90e58daa66SJayanth Dodderi Chidanand * to clear EL2 context registers. 918b95e848SZelalem Aweke */ 9277c27753SZelalem Aweke cm_el2_sysregs_context_save(REALM); 9377c27753SZelalem Aweke 9477c27753SZelalem Aweke return rc; 9577c27753SZelalem Aweke } 9677c27753SZelalem Aweke 9777c27753SZelalem Aweke /******************************************************************************* 9877c27753SZelalem Aweke * This function returns to the place where rmmd_rmm_sync_entry() was 9977c27753SZelalem Aweke * called originally. 10077c27753SZelalem Aweke ******************************************************************************/ 10177c27753SZelalem Aweke __dead2 void rmmd_rmm_sync_exit(uint64_t rc) 10277c27753SZelalem Aweke { 10377c27753SZelalem Aweke rmmd_rmm_context_t *ctx = &rmm_context[plat_my_core_pos()]; 10477c27753SZelalem Aweke 10577c27753SZelalem Aweke /* Get context of the RMM in use by this CPU. */ 10677c27753SZelalem Aweke assert(cm_get_context(REALM) == &(ctx->cpu_ctx)); 10777c27753SZelalem Aweke 10877c27753SZelalem Aweke /* 10977c27753SZelalem Aweke * The RMMD must have initiated the original request through a 11077c27753SZelalem Aweke * synchronous entry into RMM. Jump back to the original C runtime 11177c27753SZelalem Aweke * context with the value of rc in x0; 11277c27753SZelalem Aweke */ 11377c27753SZelalem Aweke rmmd_rmm_exit(ctx->c_rt_ctx, rc); 11477c27753SZelalem Aweke 11577c27753SZelalem Aweke panic(); 11677c27753SZelalem Aweke } 11777c27753SZelalem Aweke 118a4cc85c1SSubhasish Ghosh /******************************************************************************* 11977c27753SZelalem Aweke * Jump to the RMM for the first time. 12077c27753SZelalem Aweke ******************************************************************************/ 12177c27753SZelalem Aweke static int32_t rmm_init(void) 12277c27753SZelalem Aweke { 1238c980a4aSJavier Almansa Sobrino long rc; 12477c27753SZelalem Aweke rmmd_rmm_context_t *ctx = &rmm_context[plat_my_core_pos()]; 12577c27753SZelalem Aweke 12677c27753SZelalem Aweke INFO("RMM init start.\n"); 12777c27753SZelalem Aweke 12877c27753SZelalem Aweke rc = rmmd_rmm_sync_entry(ctx); 1298c980a4aSJavier Almansa Sobrino if (rc != E_RMM_BOOT_SUCCESS) { 1308c980a4aSJavier Almansa Sobrino ERROR("RMM init failed: %ld\n", rc); 1318c980a4aSJavier Almansa Sobrino /* Mark the boot as failed for all the CPUs */ 1328c980a4aSJavier Almansa Sobrino rmm_boot_failed = true; 1338c980a4aSJavier Almansa Sobrino return 0; 13477c27753SZelalem Aweke } 13577c27753SZelalem Aweke 13677c27753SZelalem Aweke INFO("RMM init end.\n"); 13777c27753SZelalem Aweke 13877c27753SZelalem Aweke return 1; 13977c27753SZelalem Aweke } 14077c27753SZelalem Aweke 14177c27753SZelalem Aweke /******************************************************************************* 14277c27753SZelalem Aweke * Load and read RMM manifest, setup RMM. 14377c27753SZelalem Aweke ******************************************************************************/ 14477c27753SZelalem Aweke int rmmd_setup(void) 14577c27753SZelalem Aweke { 146dc65ae46SJavier Almansa Sobrino size_t shared_buf_size __unused; 147dc65ae46SJavier Almansa Sobrino uintptr_t shared_buf_base; 14877c27753SZelalem Aweke uint32_t ep_attr; 14977c27753SZelalem Aweke unsigned int linear_id = plat_my_core_pos(); 15077c27753SZelalem Aweke rmmd_rmm_context_t *rmm_ctx = &rmm_context[linear_id]; 151a97bfa5fSAlexeiFedorov struct rmm_manifest *manifest; 1521d0ca40eSJavier Almansa Sobrino int rc; 15377c27753SZelalem Aweke 15477c27753SZelalem Aweke /* Make sure RME is supported. */ 155eacbef4cSVarun Wadekar if (is_feat_rme_present() == 0U) { 156eacbef4cSVarun Wadekar /* Mark the RMM boot as failed for all the CPUs */ 157eacbef4cSVarun Wadekar rmm_boot_failed = true; 158eacbef4cSVarun Wadekar return -ENOTSUP; 159eacbef4cSVarun Wadekar } 16077c27753SZelalem Aweke 16177c27753SZelalem Aweke rmm_ep_info = bl31_plat_get_next_image_ep_info(REALM); 1628cb9c635SVarun Wadekar if ((rmm_ep_info == NULL) || (rmm_ep_info->pc == 0)) { 16377c27753SZelalem Aweke WARN("No RMM image provided by BL2 boot loader, Booting " 16477c27753SZelalem Aweke "device without RMM initialization. SMCs destined for " 16577c27753SZelalem Aweke "RMM will return SMC_UNK\n"); 166eacbef4cSVarun Wadekar 167adcd74caSVarun Wadekar /* Mark the boot as failed for all the CPUs */ 168adcd74caSVarun Wadekar rmm_boot_failed = true; 16977c27753SZelalem Aweke return -ENOENT; 17077c27753SZelalem Aweke } 17177c27753SZelalem Aweke 17277c27753SZelalem Aweke /* Initialise an entrypoint to set up the CPU context */ 17377c27753SZelalem Aweke ep_attr = EP_REALM; 17477c27753SZelalem Aweke if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0U) { 17577c27753SZelalem Aweke ep_attr |= EP_EE_BIG; 17677c27753SZelalem Aweke } 17777c27753SZelalem Aweke 17877c27753SZelalem Aweke SET_PARAM_HEAD(rmm_ep_info, PARAM_EP, VERSION_1, ep_attr); 17977c27753SZelalem Aweke rmm_ep_info->spsr = SPSR_64(MODE_EL2, 18077c27753SZelalem Aweke MODE_SP_ELX, 18177c27753SZelalem Aweke DISABLE_ALL_EXCEPTIONS); 18277c27753SZelalem Aweke 1838c980a4aSJavier Almansa Sobrino shared_buf_size = 1848c980a4aSJavier Almansa Sobrino plat_rmmd_get_el3_rmm_shared_mem(&shared_buf_base); 1858c980a4aSJavier Almansa Sobrino 1868c980a4aSJavier Almansa Sobrino assert((shared_buf_size == SZ_4K) && 1878c980a4aSJavier Almansa Sobrino ((void *)shared_buf_base != NULL)); 1888c980a4aSJavier Almansa Sobrino 18932904472SSoby Mathew /* Zero out and load the boot manifest at the beginning of the share area */ 190a97bfa5fSAlexeiFedorov manifest = (struct rmm_manifest *)shared_buf_base; 19183a4e8e0SHarry Moulton (void)memset((void *)manifest, 0, sizeof(struct rmm_manifest)); 19232904472SSoby Mathew 1931d0ca40eSJavier Almansa Sobrino rc = plat_rmmd_load_manifest(manifest); 1941d0ca40eSJavier Almansa Sobrino if (rc != 0) { 1951d0ca40eSJavier Almansa Sobrino ERROR("Error loading RMM Boot Manifest (%i)\n", rc); 1960c707813SVarun Wadekar /* Mark the boot as failed for all the CPUs */ 1970c707813SVarun Wadekar rmm_boot_failed = true; 1981d0ca40eSJavier Almansa Sobrino return rc; 1991d0ca40eSJavier Almansa Sobrino } 2001d0ca40eSJavier Almansa Sobrino flush_dcache_range((uintptr_t)shared_buf_base, shared_buf_size); 2011d0ca40eSJavier Almansa Sobrino 2028c980a4aSJavier Almansa Sobrino /* 2038c980a4aSJavier Almansa Sobrino * Prepare coldboot arguments for RMM: 2048c980a4aSJavier Almansa Sobrino * arg0: This CPUID (primary processor). 2058c980a4aSJavier Almansa Sobrino * arg1: Version for this Boot Interface. 2068c980a4aSJavier Almansa Sobrino * arg2: PLATFORM_CORE_COUNT. 2078c980a4aSJavier Almansa Sobrino * arg3: Base address for the EL3 <-> RMM shared area. The boot 2088c980a4aSJavier Almansa Sobrino * manifest will be stored at the beginning of this area. 20989d979ceSAndre Przywara * arg4: opaque activation token, as returned by previous calls 2108c980a4aSJavier Almansa Sobrino */ 2118c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg0 = linear_id; 2128c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg1 = RMM_EL3_INTERFACE_VERSION; 2138c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg2 = PLATFORM_CORE_COUNT; 2148c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg3 = shared_buf_base; 21589d979ceSAndre Przywara rmm_ep_info->args.arg4 = rmm_ctx->activation_token; 2168c980a4aSJavier Almansa Sobrino 21777c27753SZelalem Aweke /* Initialise RMM context with this entry point information */ 21877c27753SZelalem Aweke cm_setup_context(&rmm_ctx->cpu_ctx, rmm_ep_info); 21977c27753SZelalem Aweke 22077c27753SZelalem Aweke INFO("RMM setup done.\n"); 22177c27753SZelalem Aweke 22277c27753SZelalem Aweke /* Register init function for deferred init. */ 22377c27753SZelalem Aweke bl31_register_rmm_init(&rmm_init); 22477c27753SZelalem Aweke 22577c27753SZelalem Aweke return 0; 22677c27753SZelalem Aweke } 22777c27753SZelalem Aweke 22877c27753SZelalem Aweke /******************************************************************************* 22977c27753SZelalem Aweke * Forward SMC to the other security state 23077c27753SZelalem Aweke ******************************************************************************/ 23111578303SSoby Mathew static uint64_t rmmd_smc_forward(uint32_t src_sec_state, 23211578303SSoby Mathew uint32_t dst_sec_state, uint64_t x0, 23311578303SSoby Mathew uint64_t x1, uint64_t x2, uint64_t x3, 23411578303SSoby Mathew uint64_t x4, void *handle) 23577c27753SZelalem Aweke { 2368e51cccaSAlexeiFedorov cpu_context_t *ctx = cm_get_context(dst_sec_state); 2378e51cccaSAlexeiFedorov 23877c27753SZelalem Aweke /* Save incoming security state */ 23977c27753SZelalem Aweke cm_el2_sysregs_context_save(src_sec_state); 24077c27753SZelalem Aweke 24177c27753SZelalem Aweke /* Restore outgoing security state */ 24277c27753SZelalem Aweke cm_el2_sysregs_context_restore(dst_sec_state); 24377c27753SZelalem Aweke cm_set_next_eret_context(dst_sec_state); 24477c27753SZelalem Aweke 24511578303SSoby Mathew /* 2468e51cccaSAlexeiFedorov * As per SMCCCv1.2, we need to preserve x4 to x7 unless 24711578303SSoby Mathew * being used as return args. Hence we differentiate the 24811578303SSoby Mathew * onward and backward path. Support upto 8 args in the 24911578303SSoby Mathew * onward path and 4 args in return path. 2508e51cccaSAlexeiFedorov * Register x4 will be preserved by RMM in case it is not 2518e51cccaSAlexeiFedorov * used in return path. 25211578303SSoby Mathew */ 25311578303SSoby Mathew if (src_sec_state == NON_SECURE) { 2548e51cccaSAlexeiFedorov SMC_RET8(ctx, x0, x1, x2, x3, x4, 25577c27753SZelalem Aweke SMC_GET_GP(handle, CTX_GPREG_X5), 25677c27753SZelalem Aweke SMC_GET_GP(handle, CTX_GPREG_X6), 25777c27753SZelalem Aweke SMC_GET_GP(handle, CTX_GPREG_X7)); 25811578303SSoby Mathew } 2598e51cccaSAlexeiFedorov 2608e51cccaSAlexeiFedorov SMC_RET5(ctx, x0, x1, x2, x3, x4); 26177c27753SZelalem Aweke } 26277c27753SZelalem Aweke 26377c27753SZelalem Aweke /******************************************************************************* 26477c27753SZelalem Aweke * This function handles all SMCs in the range reserved for RMI. Each call is 26577c27753SZelalem Aweke * either forwarded to the other security state or handled by the RMM dispatcher 26677c27753SZelalem Aweke ******************************************************************************/ 26777c27753SZelalem Aweke uint64_t rmmd_rmi_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, 26877c27753SZelalem Aweke uint64_t x3, uint64_t x4, void *cookie, 26977c27753SZelalem Aweke void *handle, uint64_t flags) 27077c27753SZelalem Aweke { 27177c27753SZelalem Aweke uint32_t src_sec_state; 27277c27753SZelalem Aweke 2738c980a4aSJavier Almansa Sobrino /* If RMM failed to boot, treat any RMI SMC as unknown */ 2748c980a4aSJavier Almansa Sobrino if (rmm_boot_failed) { 2758c980a4aSJavier Almansa Sobrino WARN("RMMD: Failed to boot up RMM. Ignoring RMI call\n"); 2768c980a4aSJavier Almansa Sobrino SMC_RET1(handle, SMC_UNK); 2778c980a4aSJavier Almansa Sobrino } 2788c980a4aSJavier Almansa Sobrino 27977c27753SZelalem Aweke /* Determine which security state this SMC originated from */ 28077c27753SZelalem Aweke src_sec_state = caller_sec_state(flags); 28177c27753SZelalem Aweke 28277c27753SZelalem Aweke /* RMI must not be invoked by the Secure world */ 28377c27753SZelalem Aweke if (src_sec_state == SMC_FROM_SECURE) { 284319fb084SSoby Mathew WARN("RMMD: RMI invoked by secure world.\n"); 28577c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 28677c27753SZelalem Aweke } 28777c27753SZelalem Aweke 28877c27753SZelalem Aweke /* 28977c27753SZelalem Aweke * Forward an RMI call from the Normal world to the Realm world as it 29077c27753SZelalem Aweke * is. 29177c27753SZelalem Aweke */ 29277c27753SZelalem Aweke if (src_sec_state == SMC_FROM_NON_SECURE) { 29367889630SArunachalam Ganapathy /* 29467889630SArunachalam Ganapathy * If SVE hint bit is set in the flags then update the SMC 29567889630SArunachalam Ganapathy * function id and pass it on to the lower EL. 29667889630SArunachalam Ganapathy */ 29767889630SArunachalam Ganapathy if (is_sve_hint_set(flags)) { 29867889630SArunachalam Ganapathy smc_fid |= (FUNCID_SVE_HINT_MASK << 29967889630SArunachalam Ganapathy FUNCID_SVE_HINT_SHIFT); 30067889630SArunachalam Ganapathy } 301319fb084SSoby Mathew VERBOSE("RMMD: RMI call from non-secure world.\n"); 30211578303SSoby Mathew return rmmd_smc_forward(NON_SECURE, REALM, smc_fid, 30377c27753SZelalem Aweke x1, x2, x3, x4, handle); 30477c27753SZelalem Aweke } 30577c27753SZelalem Aweke 306319fb084SSoby Mathew if (src_sec_state != SMC_FROM_REALM) { 307319fb084SSoby Mathew SMC_RET1(handle, SMC_UNK); 308319fb084SSoby Mathew } 30977c27753SZelalem Aweke 31077c27753SZelalem Aweke switch (smc_fid) { 3118e51cccaSAlexeiFedorov case RMM_RMI_REQ_COMPLETE: { 3128e51cccaSAlexeiFedorov uint64_t x5 = SMC_GET_GP(handle, CTX_GPREG_X5); 31377c27753SZelalem Aweke 3148e51cccaSAlexeiFedorov return rmmd_smc_forward(REALM, NON_SECURE, x1, 3158e51cccaSAlexeiFedorov x2, x3, x4, x5, handle); 3168e51cccaSAlexeiFedorov } 31777c27753SZelalem Aweke default: 318319fb084SSoby Mathew WARN("RMMD: Unsupported RMM call 0x%08x\n", smc_fid); 31977c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 32077c27753SZelalem Aweke } 32177c27753SZelalem Aweke } 32277c27753SZelalem Aweke 32377c27753SZelalem Aweke /******************************************************************************* 32477c27753SZelalem Aweke * This cpu has been turned on. Enter RMM to initialise R-EL2. Entry into RMM 32577c27753SZelalem Aweke * is done after initialising minimal architectural state that guarantees safe 32677c27753SZelalem Aweke * execution. 32777c27753SZelalem Aweke ******************************************************************************/ 32877c27753SZelalem Aweke static void *rmmd_cpu_on_finish_handler(const void *arg) 32977c27753SZelalem Aweke { 3308c980a4aSJavier Almansa Sobrino long rc; 33177c27753SZelalem Aweke uint32_t linear_id = plat_my_core_pos(); 33277c27753SZelalem Aweke rmmd_rmm_context_t *ctx = &rmm_context[linear_id]; 33377c27753SZelalem Aweke 3348c980a4aSJavier Almansa Sobrino if (rmm_boot_failed) { 3358c980a4aSJavier Almansa Sobrino /* RMM Boot failed on a previous CPU. Abort. */ 3368c980a4aSJavier Almansa Sobrino ERROR("RMM Failed to initialize. Ignoring for CPU%d\n", 3378c980a4aSJavier Almansa Sobrino linear_id); 3388c980a4aSJavier Almansa Sobrino return NULL; 3398c980a4aSJavier Almansa Sobrino } 3408c980a4aSJavier Almansa Sobrino 3418c980a4aSJavier Almansa Sobrino /* 3428c980a4aSJavier Almansa Sobrino * Prepare warmboot arguments for RMM: 3438c980a4aSJavier Almansa Sobrino * arg0: This CPUID. 34489d979ceSAndre Przywara * arg1: opaque activation token, as returned by previous calls 34589d979ceSAndre Przywara * arg2 to arg3: Not used. 3468c980a4aSJavier Almansa Sobrino */ 3478c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg0 = linear_id; 34889d979ceSAndre Przywara rmm_ep_info->args.arg1 = ctx->activation_token; 3498c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg2 = 0ULL; 3508c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg3 = 0ULL; 35177c27753SZelalem Aweke 35277c27753SZelalem Aweke /* Initialise RMM context with this entry point information */ 35377c27753SZelalem Aweke cm_setup_context(&ctx->cpu_ctx, rmm_ep_info); 35477c27753SZelalem Aweke 35577c27753SZelalem Aweke rc = rmmd_rmm_sync_entry(ctx); 3568c980a4aSJavier Almansa Sobrino 3578c980a4aSJavier Almansa Sobrino if (rc != E_RMM_BOOT_SUCCESS) { 3588c980a4aSJavier Almansa Sobrino ERROR("RMM init failed on CPU%d: %ld\n", linear_id, rc); 3598c980a4aSJavier Almansa Sobrino /* Mark the boot as failed for any other booting CPU */ 3608c980a4aSJavier Almansa Sobrino rmm_boot_failed = true; 36177c27753SZelalem Aweke } 36277c27753SZelalem Aweke 36377c27753SZelalem Aweke return NULL; 36477c27753SZelalem Aweke } 36577c27753SZelalem Aweke 36677c27753SZelalem Aweke /* Subscribe to PSCI CPU on to initialize RMM on secondary */ 36777c27753SZelalem Aweke SUBSCRIBE_TO_EVENT(psci_cpu_on_finish, rmmd_cpu_on_finish_handler); 36877c27753SZelalem Aweke 369319fb084SSoby Mathew /* Convert GPT lib error to RMMD GTS error */ 370319fb084SSoby Mathew static int gpt_to_gts_error(int error, uint32_t smc_fid, uint64_t address) 371319fb084SSoby Mathew { 372319fb084SSoby Mathew int ret; 373319fb084SSoby Mathew 374319fb084SSoby Mathew if (error == 0) { 375dc65ae46SJavier Almansa Sobrino return E_RMM_OK; 376319fb084SSoby Mathew } 377319fb084SSoby Mathew 378319fb084SSoby Mathew if (error == -EINVAL) { 379dc65ae46SJavier Almansa Sobrino ret = E_RMM_BAD_ADDR; 380319fb084SSoby Mathew } else { 381319fb084SSoby Mathew /* This is the only other error code we expect */ 382319fb084SSoby Mathew assert(error == -EPERM); 383dc65ae46SJavier Almansa Sobrino ret = E_RMM_BAD_PAS; 384319fb084SSoby Mathew } 385319fb084SSoby Mathew 386319fb084SSoby Mathew ERROR("RMMD: PAS Transition failed. GPT ret = %d, PA: 0x%"PRIx64 ", FID = 0x%x\n", 387319fb084SSoby Mathew error, address, smc_fid); 388319fb084SSoby Mathew return ret; 389319fb084SSoby Mathew } 390319fb084SSoby Mathew 3916a88ec8bSRaghu Krishnamurthy static int rmm_el3_ifc_get_feat_register(uint64_t feat_reg_idx, 3926a88ec8bSRaghu Krishnamurthy uint64_t *feat_reg) 3936a88ec8bSRaghu Krishnamurthy { 3946a88ec8bSRaghu Krishnamurthy if (feat_reg_idx != RMM_EL3_FEAT_REG_0_IDX) { 3956a88ec8bSRaghu Krishnamurthy ERROR("RMMD: Failed to get feature register %ld\n", feat_reg_idx); 3966a88ec8bSRaghu Krishnamurthy return E_RMM_INVAL; 3976a88ec8bSRaghu Krishnamurthy } 3986a88ec8bSRaghu Krishnamurthy 3996a88ec8bSRaghu Krishnamurthy *feat_reg = 0UL; 4006a88ec8bSRaghu Krishnamurthy #if RMMD_ENABLE_EL3_TOKEN_SIGN 4016a88ec8bSRaghu Krishnamurthy *feat_reg |= RMM_EL3_FEAT_REG_0_EL3_TOKEN_SIGN_MASK; 4026a88ec8bSRaghu Krishnamurthy #endif 4036a88ec8bSRaghu Krishnamurthy return E_RMM_OK; 4046a88ec8bSRaghu Krishnamurthy } 4056a88ec8bSRaghu Krishnamurthy 406f801fdc2STushar Khandelwal /* 40700e62ff9SJuan Pablo Conde * Update encryption key associated with mecid included in x1. 408f801fdc2STushar Khandelwal */ 40900e62ff9SJuan Pablo Conde static int rmmd_mecid_key_update(uint64_t x1) 410f801fdc2STushar Khandelwal { 411f801fdc2STushar Khandelwal uint64_t mecid_width, mecid_width_mask; 41200e62ff9SJuan Pablo Conde uint16_t mecid; 41300e62ff9SJuan Pablo Conde unsigned int reason; 414f801fdc2STushar Khandelwal int ret; 415f801fdc2STushar Khandelwal 416f801fdc2STushar Khandelwal /* 417609ada96SJuan Pablo Conde * Check whether FEAT_MEC is supported by the hardware. If not, return 418609ada96SJuan Pablo Conde * unknown SMC. 419609ada96SJuan Pablo Conde */ 420609ada96SJuan Pablo Conde if (is_feat_mec_supported() == false) { 421609ada96SJuan Pablo Conde return E_RMM_UNK; 422609ada96SJuan Pablo Conde } 423609ada96SJuan Pablo Conde 424609ada96SJuan Pablo Conde /* 425f801fdc2STushar Khandelwal * Check whether the mecid parameter is at most MECIDR_EL2.MECIDWidthm1 + 1 426f801fdc2STushar Khandelwal * in length. 427f801fdc2STushar Khandelwal */ 428f801fdc2STushar Khandelwal mecid_width = ((read_mecidr_el2() >> MECIDR_EL2_MECIDWidthm1_SHIFT) & 42900e62ff9SJuan Pablo Conde MECIDR_EL2_MECIDWidthm1_MASK) + 1UL; 43000e62ff9SJuan Pablo Conde mecid_width_mask = ((1UL << mecid_width) - 1UL); 43100e62ff9SJuan Pablo Conde 432*c08285cfSSoby Mathew mecid = (x1 >> MECID_SHIFT) & MECID_MASK; 433f801fdc2STushar Khandelwal if ((mecid & ~mecid_width_mask) != 0U) { 434f801fdc2STushar Khandelwal return E_RMM_INVAL; 435f801fdc2STushar Khandelwal } 436f801fdc2STushar Khandelwal 43700e62ff9SJuan Pablo Conde reason = (x1 >> MEC_REFRESH_REASON_SHIFT) & MEC_REFRESH_REASON_MASK; 43800e62ff9SJuan Pablo Conde ret = plat_rmmd_mecid_key_update(mecid, reason); 439f801fdc2STushar Khandelwal 440f801fdc2STushar Khandelwal if (ret != 0) { 441f801fdc2STushar Khandelwal return E_RMM_UNK; 442f801fdc2STushar Khandelwal } 443f801fdc2STushar Khandelwal return E_RMM_OK; 444f801fdc2STushar Khandelwal } 445f801fdc2STushar Khandelwal 44677c27753SZelalem Aweke /******************************************************************************* 447319fb084SSoby Mathew * This function handles RMM-EL3 interface SMCs 44877c27753SZelalem Aweke ******************************************************************************/ 449319fb084SSoby Mathew uint64_t rmmd_rmm_el3_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, 45077c27753SZelalem Aweke uint64_t x3, uint64_t x4, void *cookie, 45177c27753SZelalem Aweke void *handle, uint64_t flags) 45277c27753SZelalem Aweke { 4536a88ec8bSRaghu Krishnamurthy uint64_t remaining_len = 0UL; 45477c27753SZelalem Aweke uint32_t src_sec_state; 4556a00e9b0SRobert Wakim int ret; 45677c27753SZelalem Aweke 4578c980a4aSJavier Almansa Sobrino /* If RMM failed to boot, treat any RMM-EL3 interface SMC as unknown */ 4588c980a4aSJavier Almansa Sobrino if (rmm_boot_failed) { 4598c980a4aSJavier Almansa Sobrino WARN("RMMD: Failed to boot up RMM. Ignoring RMM-EL3 call\n"); 4608c980a4aSJavier Almansa Sobrino SMC_RET1(handle, SMC_UNK); 4618c980a4aSJavier Almansa Sobrino } 4628c980a4aSJavier Almansa Sobrino 46377c27753SZelalem Aweke /* Determine which security state this SMC originated from */ 46477c27753SZelalem Aweke src_sec_state = caller_sec_state(flags); 46577c27753SZelalem Aweke 46677c27753SZelalem Aweke if (src_sec_state != SMC_FROM_REALM) { 467319fb084SSoby Mathew WARN("RMMD: RMM-EL3 call originated from secure or normal world\n"); 46877c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 46977c27753SZelalem Aweke } 47077c27753SZelalem Aweke 47177c27753SZelalem Aweke switch (smc_fid) { 472e50fedbcSJavier Almansa Sobrino case RMM_GTSI_DELEGATE: 4736a00e9b0SRobert Wakim ret = gpt_delegate_pas(x1, PAGE_SIZE_4KB, SMC_FROM_REALM); 474319fb084SSoby Mathew SMC_RET1(handle, gpt_to_gts_error(ret, smc_fid, x1)); 475e50fedbcSJavier Almansa Sobrino case RMM_GTSI_UNDELEGATE: 4766a00e9b0SRobert Wakim ret = gpt_undelegate_pas(x1, PAGE_SIZE_4KB, SMC_FROM_REALM); 477319fb084SSoby Mathew SMC_RET1(handle, gpt_to_gts_error(ret, smc_fid, x1)); 478e50fedbcSJavier Almansa Sobrino case RMM_ATTEST_GET_REALM_KEY: 479a0435105SSoby Mathew ret = rmmd_attest_get_signing_key(x1, &x2, x3); 480a0435105SSoby Mathew SMC_RET2(handle, ret, x2); 4811975d28bSSona Mathew case RMM_ATTEST_GET_PLAT_TOKEN: 4821975d28bSSona Mathew ret = rmmd_attest_get_platform_token(x1, &x2, x3, &remaining_len); 4831975d28bSSona Mathew SMC_RET3(handle, ret, x2, remaining_len); 4846a88ec8bSRaghu Krishnamurthy case RMM_EL3_FEATURES: 4856a88ec8bSRaghu Krishnamurthy ret = rmm_el3_ifc_get_feat_register(x1, &x2); 4866a88ec8bSRaghu Krishnamurthy SMC_RET2(handle, ret, x2); 4876a88ec8bSRaghu Krishnamurthy #if RMMD_ENABLE_EL3_TOKEN_SIGN 4886a88ec8bSRaghu Krishnamurthy case RMM_EL3_TOKEN_SIGN: 4896a88ec8bSRaghu Krishnamurthy return rmmd_el3_token_sign(handle, x1, x2, x3, x4); 4906a88ec8bSRaghu Krishnamurthy #endif 4912132c707SSona Mathew 4922132c707SSona Mathew #if RMMD_ENABLE_IDE_KEY_PROG 4932132c707SSona Mathew case RMM_IDE_KEY_PROG: 4942132c707SSona Mathew { 4952132c707SSona Mathew rp_ide_key_info_t ide_key_info; 4962132c707SSona Mathew 4972132c707SSona Mathew ide_key_info.keyqw0 = x4; 4982132c707SSona Mathew ide_key_info.keyqw1 = SMC_GET_GP(handle, CTX_GPREG_X5); 4992132c707SSona Mathew ide_key_info.keyqw2 = SMC_GET_GP(handle, CTX_GPREG_X6); 5002132c707SSona Mathew ide_key_info.keyqw3 = SMC_GET_GP(handle, CTX_GPREG_X7); 5012132c707SSona Mathew ide_key_info.ifvqw0 = SMC_GET_GP(handle, CTX_GPREG_X8); 5022132c707SSona Mathew ide_key_info.ifvqw1 = SMC_GET_GP(handle, CTX_GPREG_X9); 5032132c707SSona Mathew uint64_t x10 = SMC_GET_GP(handle, CTX_GPREG_X10); 5042132c707SSona Mathew uint64_t x11 = SMC_GET_GP(handle, CTX_GPREG_X11); 5052132c707SSona Mathew 5062132c707SSona Mathew ret = rmmd_el3_ide_key_program(x1, x2, x3, &ide_key_info, x10, x11); 5072132c707SSona Mathew SMC_RET1(handle, ret); 5082132c707SSona Mathew } 5092132c707SSona Mathew case RMM_IDE_KEY_SET_GO: 5102132c707SSona Mathew ret = rmmd_el3_ide_key_set_go(x1, x2, x3, x4, SMC_GET_GP(handle, CTX_GPREG_X5)); 5112132c707SSona Mathew SMC_RET1(handle, ret); 5122132c707SSona Mathew case RMM_IDE_KEY_SET_STOP: 5132132c707SSona Mathew ret = rmmd_el3_ide_key_set_stop(x1, x2, x3, x4, SMC_GET_GP(handle, CTX_GPREG_X5)); 5142132c707SSona Mathew SMC_RET1(handle, ret); 5152132c707SSona Mathew case RMM_IDE_KM_PULL_RESPONSE: { 5162132c707SSona Mathew uint64_t req_resp = 0, req_id = 0, cookie_var = 0; 5172132c707SSona Mathew 5182132c707SSona Mathew ret = rmmd_el3_ide_km_pull_response(x1, x2, &req_resp, &req_id, &cookie_var); 5192132c707SSona Mathew SMC_RET4(handle, ret, req_resp, req_id, cookie_var); 5202132c707SSona Mathew } 5212132c707SSona Mathew #endif /* RMMD_ENABLE_IDE_KEY_PROG */ 522745c129aSAndre Przywara case RMM_RESERVE_MEMORY: 523745c129aSAndre Przywara ret = rmmd_reserve_memory(x1, &x2); 524745c129aSAndre Przywara SMC_RET2(handle, ret, x2); 525745c129aSAndre Przywara 5268c980a4aSJavier Almansa Sobrino case RMM_BOOT_COMPLETE: 52789d979ceSAndre Przywara { 52889d979ceSAndre Przywara rmmd_rmm_context_t *ctx = &rmm_context[plat_my_core_pos()]; 52989d979ceSAndre Przywara 53089d979ceSAndre Przywara ctx->activation_token = x2; 5318c980a4aSJavier Almansa Sobrino VERBOSE("RMMD: running rmmd_rmm_sync_exit\n"); 5328c980a4aSJavier Almansa Sobrino rmmd_rmm_sync_exit(x1); 53389d979ceSAndre Przywara } 53400e62ff9SJuan Pablo Conde case RMM_MEC_REFRESH: 535f801fdc2STushar Khandelwal ret = rmmd_mecid_key_update(x1); 536f801fdc2STushar Khandelwal SMC_RET1(handle, ret); 53777c27753SZelalem Aweke default: 538319fb084SSoby Mathew WARN("RMMD: Unsupported RMM-EL3 call 0x%08x\n", smc_fid); 53977c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 54077c27753SZelalem Aweke } 54177c27753SZelalem Aweke } 542