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> 26*f708e9ddSRohit Mathew #include <lib/per_cpu/per_cpu.h> 2777c27753SZelalem Aweke 2877c27753SZelalem Aweke #include <lib/spinlock.h> 2977c27753SZelalem Aweke #include <lib/utils.h> 3077c27753SZelalem Aweke #include <lib/xlat_tables/xlat_tables_v2.h> 3177c27753SZelalem Aweke #include <plat/common/common_def.h> 3277c27753SZelalem Aweke #include <plat/common/platform.h> 3377c27753SZelalem Aweke #include <platform_def.h> 3477c27753SZelalem Aweke #include <services/rmmd_svc.h> 3577c27753SZelalem Aweke #include <smccc_helpers.h> 36f92eb7e2SArunachalam Ganapathy #include <lib/extensions/sme.h> 37a4cc85c1SSubhasish Ghosh #include <lib/extensions/sve.h> 3879c0c7faSBoyan Karatotev #include <lib/extensions/spe.h> 3979c0c7faSBoyan Karatotev #include <lib/extensions/trbe.h> 4077c27753SZelalem Aweke #include "rmmd_private.h" 4177c27753SZelalem Aweke 4200e62ff9SJuan Pablo Conde #define MECID_SHIFT U(32) 4300e62ff9SJuan Pablo Conde #define MECID_MASK 0xFFFFU 4400e62ff9SJuan Pablo Conde 4500e62ff9SJuan Pablo Conde #define MEC_REFRESH_REASON_SHIFT U(0) 4600e62ff9SJuan Pablo Conde #define MEC_REFRESH_REASON_MASK BIT(0) 4700e62ff9SJuan Pablo Conde 4877c27753SZelalem Aweke /******************************************************************************* 498c980a4aSJavier Almansa Sobrino * RMM boot failure flag 508c980a4aSJavier Almansa Sobrino ******************************************************************************/ 518c980a4aSJavier Almansa Sobrino static bool rmm_boot_failed; 528c980a4aSJavier Almansa Sobrino 538c980a4aSJavier Almansa Sobrino /******************************************************************************* 5477c27753SZelalem Aweke * RMM context information. 5577c27753SZelalem Aweke ******************************************************************************/ 56*f708e9ddSRohit Mathew PER_CPU_DEFINE(rmmd_rmm_context_t, rmm_context); 5777c27753SZelalem Aweke 5877c27753SZelalem Aweke /******************************************************************************* 5977c27753SZelalem Aweke * RMM entry point information. Discovered on the primary core and reused 6077c27753SZelalem Aweke * on secondary cores. 6177c27753SZelalem Aweke ******************************************************************************/ 6277c27753SZelalem Aweke static entry_point_info_t *rmm_ep_info; 6377c27753SZelalem Aweke 6477c27753SZelalem Aweke /******************************************************************************* 6577c27753SZelalem Aweke * Static function declaration. 6677c27753SZelalem Aweke ******************************************************************************/ 6777c27753SZelalem Aweke static int32_t rmm_init(void); 6877c27753SZelalem Aweke 6977c27753SZelalem Aweke /******************************************************************************* 7077c27753SZelalem Aweke * This function takes an RMM context pointer and performs a synchronous entry 7177c27753SZelalem Aweke * into it. 7277c27753SZelalem Aweke ******************************************************************************/ 7377c27753SZelalem Aweke uint64_t rmmd_rmm_sync_entry(rmmd_rmm_context_t *rmm_ctx) 7477c27753SZelalem Aweke { 7577c27753SZelalem Aweke uint64_t rc; 7677c27753SZelalem Aweke 7777c27753SZelalem Aweke assert(rmm_ctx != NULL); 7877c27753SZelalem Aweke 7977c27753SZelalem Aweke cm_set_context(&(rmm_ctx->cpu_ctx), REALM); 8077c27753SZelalem Aweke 8177c27753SZelalem Aweke /* Restore the realm context assigned above */ 8277c27753SZelalem Aweke cm_el2_sysregs_context_restore(REALM); 8377c27753SZelalem Aweke cm_set_next_eret_context(REALM); 8477c27753SZelalem Aweke 8577c27753SZelalem Aweke /* Enter RMM */ 8677c27753SZelalem Aweke rc = rmmd_rmm_enter(&rmm_ctx->c_rt_ctx); 8777c27753SZelalem Aweke 888b95e848SZelalem Aweke /* 89e58daa66SJayanth Dodderi Chidanand * Save realm context. EL2 Non-secure context will be restored 90e58daa66SJayanth Dodderi Chidanand * before exiting Non-secure world, therefore there is no need 91e58daa66SJayanth Dodderi Chidanand * to clear EL2 context registers. 928b95e848SZelalem Aweke */ 9377c27753SZelalem Aweke cm_el2_sysregs_context_save(REALM); 9477c27753SZelalem Aweke 9577c27753SZelalem Aweke return rc; 9677c27753SZelalem Aweke } 9777c27753SZelalem Aweke 9877c27753SZelalem Aweke /******************************************************************************* 9977c27753SZelalem Aweke * This function returns to the place where rmmd_rmm_sync_entry() was 10077c27753SZelalem Aweke * called originally. 10177c27753SZelalem Aweke ******************************************************************************/ 10277c27753SZelalem Aweke __dead2 void rmmd_rmm_sync_exit(uint64_t rc) 10377c27753SZelalem Aweke { 104*f708e9ddSRohit Mathew rmmd_rmm_context_t *ctx = PER_CPU_CUR(rmm_context); 10577c27753SZelalem Aweke 10677c27753SZelalem Aweke /* Get context of the RMM in use by this CPU. */ 10777c27753SZelalem Aweke assert(cm_get_context(REALM) == &(ctx->cpu_ctx)); 10877c27753SZelalem Aweke 10977c27753SZelalem Aweke /* 11077c27753SZelalem Aweke * The RMMD must have initiated the original request through a 11177c27753SZelalem Aweke * synchronous entry into RMM. Jump back to the original C runtime 11277c27753SZelalem Aweke * context with the value of rc in x0; 11377c27753SZelalem Aweke */ 11477c27753SZelalem Aweke rmmd_rmm_exit(ctx->c_rt_ctx, rc); 11577c27753SZelalem Aweke 11677c27753SZelalem Aweke panic(); 11777c27753SZelalem Aweke } 11877c27753SZelalem Aweke 119a4cc85c1SSubhasish Ghosh /******************************************************************************* 12077c27753SZelalem Aweke * Jump to the RMM for the first time. 12177c27753SZelalem Aweke ******************************************************************************/ 12277c27753SZelalem Aweke static int32_t rmm_init(void) 12377c27753SZelalem Aweke { 1248c980a4aSJavier Almansa Sobrino long rc; 125*f708e9ddSRohit Mathew rmmd_rmm_context_t *ctx = PER_CPU_CUR(rmm_context); 12677c27753SZelalem Aweke 12777c27753SZelalem Aweke INFO("RMM init start.\n"); 12877c27753SZelalem Aweke 12977c27753SZelalem Aweke rc = rmmd_rmm_sync_entry(ctx); 1308c980a4aSJavier Almansa Sobrino if (rc != E_RMM_BOOT_SUCCESS) { 1318c980a4aSJavier Almansa Sobrino ERROR("RMM init failed: %ld\n", rc); 1328c980a4aSJavier Almansa Sobrino /* Mark the boot as failed for all the CPUs */ 1338c980a4aSJavier Almansa Sobrino rmm_boot_failed = true; 1348c980a4aSJavier Almansa Sobrino return 0; 13577c27753SZelalem Aweke } 13677c27753SZelalem Aweke 13777c27753SZelalem Aweke INFO("RMM init end.\n"); 13877c27753SZelalem Aweke 13977c27753SZelalem Aweke return 1; 14077c27753SZelalem Aweke } 14177c27753SZelalem Aweke 14277c27753SZelalem Aweke /******************************************************************************* 14377c27753SZelalem Aweke * Load and read RMM manifest, setup RMM. 14477c27753SZelalem Aweke ******************************************************************************/ 14577c27753SZelalem Aweke int rmmd_setup(void) 14677c27753SZelalem Aweke { 147dc65ae46SJavier Almansa Sobrino size_t shared_buf_size __unused; 148dc65ae46SJavier Almansa Sobrino uintptr_t shared_buf_base; 14977c27753SZelalem Aweke uint32_t ep_attr; 15077c27753SZelalem Aweke unsigned int linear_id = plat_my_core_pos(); 151*f708e9ddSRohit Mathew 152*f708e9ddSRohit Mathew rmmd_rmm_context_t *rmm_ctx = PER_CPU_CUR(rmm_context); 153a97bfa5fSAlexeiFedorov struct rmm_manifest *manifest; 1541d0ca40eSJavier Almansa Sobrino int rc; 15577c27753SZelalem Aweke 15677c27753SZelalem Aweke /* Make sure RME is supported. */ 157eacbef4cSVarun Wadekar if (is_feat_rme_present() == 0U) { 158eacbef4cSVarun Wadekar /* Mark the RMM boot as failed for all the CPUs */ 159eacbef4cSVarun Wadekar rmm_boot_failed = true; 160eacbef4cSVarun Wadekar return -ENOTSUP; 161eacbef4cSVarun Wadekar } 16277c27753SZelalem Aweke 16377c27753SZelalem Aweke rmm_ep_info = bl31_plat_get_next_image_ep_info(REALM); 1648cb9c635SVarun Wadekar if ((rmm_ep_info == NULL) || (rmm_ep_info->pc == 0)) { 16577c27753SZelalem Aweke WARN("No RMM image provided by BL2 boot loader, Booting " 16677c27753SZelalem Aweke "device without RMM initialization. SMCs destined for " 16777c27753SZelalem Aweke "RMM will return SMC_UNK\n"); 168eacbef4cSVarun Wadekar 169adcd74caSVarun Wadekar /* Mark the boot as failed for all the CPUs */ 170adcd74caSVarun Wadekar rmm_boot_failed = true; 17177c27753SZelalem Aweke return -ENOENT; 17277c27753SZelalem Aweke } 17377c27753SZelalem Aweke 17477c27753SZelalem Aweke /* Initialise an entrypoint to set up the CPU context */ 17577c27753SZelalem Aweke ep_attr = EP_REALM; 17677c27753SZelalem Aweke if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0U) { 17777c27753SZelalem Aweke ep_attr |= EP_EE_BIG; 17877c27753SZelalem Aweke } 17977c27753SZelalem Aweke 18077c27753SZelalem Aweke SET_PARAM_HEAD(rmm_ep_info, PARAM_EP, VERSION_1, ep_attr); 18177c27753SZelalem Aweke rmm_ep_info->spsr = SPSR_64(MODE_EL2, 18277c27753SZelalem Aweke MODE_SP_ELX, 18377c27753SZelalem Aweke DISABLE_ALL_EXCEPTIONS); 18477c27753SZelalem Aweke 1858c980a4aSJavier Almansa Sobrino shared_buf_size = 1868c980a4aSJavier Almansa Sobrino plat_rmmd_get_el3_rmm_shared_mem(&shared_buf_base); 1878c980a4aSJavier Almansa Sobrino 1888c980a4aSJavier Almansa Sobrino assert((shared_buf_size == SZ_4K) && 1898c980a4aSJavier Almansa Sobrino ((void *)shared_buf_base != NULL)); 1908c980a4aSJavier Almansa Sobrino 19132904472SSoby Mathew /* Zero out and load the boot manifest at the beginning of the share area */ 192a97bfa5fSAlexeiFedorov manifest = (struct rmm_manifest *)shared_buf_base; 19383a4e8e0SHarry Moulton (void)memset((void *)manifest, 0, sizeof(struct rmm_manifest)); 19432904472SSoby Mathew 1951d0ca40eSJavier Almansa Sobrino rc = plat_rmmd_load_manifest(manifest); 1961d0ca40eSJavier Almansa Sobrino if (rc != 0) { 1971d0ca40eSJavier Almansa Sobrino ERROR("Error loading RMM Boot Manifest (%i)\n", rc); 1980c707813SVarun Wadekar /* Mark the boot as failed for all the CPUs */ 1990c707813SVarun Wadekar rmm_boot_failed = true; 2001d0ca40eSJavier Almansa Sobrino return rc; 2011d0ca40eSJavier Almansa Sobrino } 2021d0ca40eSJavier Almansa Sobrino flush_dcache_range((uintptr_t)shared_buf_base, shared_buf_size); 2031d0ca40eSJavier Almansa Sobrino 2048c980a4aSJavier Almansa Sobrino /* 2058c980a4aSJavier Almansa Sobrino * Prepare coldboot arguments for RMM: 2068c980a4aSJavier Almansa Sobrino * arg0: This CPUID (primary processor). 2078c980a4aSJavier Almansa Sobrino * arg1: Version for this Boot Interface. 2088c980a4aSJavier Almansa Sobrino * arg2: PLATFORM_CORE_COUNT. 2098c980a4aSJavier Almansa Sobrino * arg3: Base address for the EL3 <-> RMM shared area. The boot 2108c980a4aSJavier Almansa Sobrino * manifest will be stored at the beginning of this area. 21189d979ceSAndre Przywara * arg4: opaque activation token, as returned by previous calls 2128c980a4aSJavier Almansa Sobrino */ 2138c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg0 = linear_id; 2148c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg1 = RMM_EL3_INTERFACE_VERSION; 2158c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg2 = PLATFORM_CORE_COUNT; 2168c980a4aSJavier Almansa Sobrino rmm_ep_info->args.arg3 = shared_buf_base; 21789d979ceSAndre Przywara rmm_ep_info->args.arg4 = rmm_ctx->activation_token; 2188c980a4aSJavier Almansa Sobrino 21977c27753SZelalem Aweke /* Initialise RMM context with this entry point information */ 22077c27753SZelalem Aweke cm_setup_context(&rmm_ctx->cpu_ctx, rmm_ep_info); 22177c27753SZelalem Aweke 22277c27753SZelalem Aweke INFO("RMM setup done.\n"); 22377c27753SZelalem Aweke 22477c27753SZelalem Aweke /* Register init function for deferred init. */ 22577c27753SZelalem Aweke bl31_register_rmm_init(&rmm_init); 22677c27753SZelalem Aweke 22777c27753SZelalem Aweke return 0; 22877c27753SZelalem Aweke } 22977c27753SZelalem Aweke 23077c27753SZelalem Aweke /******************************************************************************* 23177c27753SZelalem Aweke * Forward SMC to the other security state 23277c27753SZelalem Aweke ******************************************************************************/ 23311578303SSoby Mathew static uint64_t rmmd_smc_forward(uint32_t src_sec_state, 23411578303SSoby Mathew uint32_t dst_sec_state, uint64_t x0, 23511578303SSoby Mathew uint64_t x1, uint64_t x2, uint64_t x3, 23611578303SSoby Mathew uint64_t x4, void *handle) 23777c27753SZelalem Aweke { 2388e51cccaSAlexeiFedorov cpu_context_t *ctx = cm_get_context(dst_sec_state); 2398e51cccaSAlexeiFedorov 24077c27753SZelalem Aweke /* Save incoming security state */ 24177c27753SZelalem Aweke cm_el2_sysregs_context_save(src_sec_state); 24277c27753SZelalem Aweke 24377c27753SZelalem Aweke /* Restore outgoing security state */ 24477c27753SZelalem Aweke cm_el2_sysregs_context_restore(dst_sec_state); 24577c27753SZelalem Aweke cm_set_next_eret_context(dst_sec_state); 24677c27753SZelalem Aweke 24711578303SSoby Mathew /* 2488e51cccaSAlexeiFedorov * As per SMCCCv1.2, we need to preserve x4 to x7 unless 24911578303SSoby Mathew * being used as return args. Hence we differentiate the 25011578303SSoby Mathew * onward and backward path. Support upto 8 args in the 25111578303SSoby Mathew * onward path and 4 args in return path. 2528e51cccaSAlexeiFedorov * Register x4 will be preserved by RMM in case it is not 2538e51cccaSAlexeiFedorov * used in return path. 25411578303SSoby Mathew */ 25511578303SSoby Mathew if (src_sec_state == NON_SECURE) { 2568e51cccaSAlexeiFedorov SMC_RET8(ctx, x0, x1, x2, x3, x4, 25777c27753SZelalem Aweke SMC_GET_GP(handle, CTX_GPREG_X5), 25877c27753SZelalem Aweke SMC_GET_GP(handle, CTX_GPREG_X6), 25977c27753SZelalem Aweke SMC_GET_GP(handle, CTX_GPREG_X7)); 26011578303SSoby Mathew } 2618e51cccaSAlexeiFedorov 2628e51cccaSAlexeiFedorov SMC_RET5(ctx, x0, x1, x2, x3, x4); 26377c27753SZelalem Aweke } 26477c27753SZelalem Aweke 26577c27753SZelalem Aweke /******************************************************************************* 26677c27753SZelalem Aweke * This function handles all SMCs in the range reserved for RMI. Each call is 26777c27753SZelalem Aweke * either forwarded to the other security state or handled by the RMM dispatcher 26877c27753SZelalem Aweke ******************************************************************************/ 26977c27753SZelalem Aweke uint64_t rmmd_rmi_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, 27077c27753SZelalem Aweke uint64_t x3, uint64_t x4, void *cookie, 27177c27753SZelalem Aweke void *handle, uint64_t flags) 27277c27753SZelalem Aweke { 27377c27753SZelalem Aweke uint32_t src_sec_state; 27477c27753SZelalem Aweke 2758c980a4aSJavier Almansa Sobrino /* If RMM failed to boot, treat any RMI SMC as unknown */ 2768c980a4aSJavier Almansa Sobrino if (rmm_boot_failed) { 2778c980a4aSJavier Almansa Sobrino WARN("RMMD: Failed to boot up RMM. Ignoring RMI call\n"); 2788c980a4aSJavier Almansa Sobrino SMC_RET1(handle, SMC_UNK); 2798c980a4aSJavier Almansa Sobrino } 2808c980a4aSJavier Almansa Sobrino 28177c27753SZelalem Aweke /* Determine which security state this SMC originated from */ 28277c27753SZelalem Aweke src_sec_state = caller_sec_state(flags); 28377c27753SZelalem Aweke 28477c27753SZelalem Aweke /* RMI must not be invoked by the Secure world */ 28577c27753SZelalem Aweke if (src_sec_state == SMC_FROM_SECURE) { 286319fb084SSoby Mathew WARN("RMMD: RMI invoked by secure world.\n"); 28777c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 28877c27753SZelalem Aweke } 28977c27753SZelalem Aweke 29077c27753SZelalem Aweke /* 29177c27753SZelalem Aweke * Forward an RMI call from the Normal world to the Realm world as it 29277c27753SZelalem Aweke * is. 29377c27753SZelalem Aweke */ 29477c27753SZelalem Aweke if (src_sec_state == SMC_FROM_NON_SECURE) { 29567889630SArunachalam Ganapathy /* 29667889630SArunachalam Ganapathy * If SVE hint bit is set in the flags then update the SMC 29767889630SArunachalam Ganapathy * function id and pass it on to the lower EL. 29867889630SArunachalam Ganapathy */ 29967889630SArunachalam Ganapathy if (is_sve_hint_set(flags)) { 30067889630SArunachalam Ganapathy smc_fid |= (FUNCID_SVE_HINT_MASK << 30167889630SArunachalam Ganapathy FUNCID_SVE_HINT_SHIFT); 30267889630SArunachalam Ganapathy } 303319fb084SSoby Mathew VERBOSE("RMMD: RMI call from non-secure world.\n"); 30411578303SSoby Mathew return rmmd_smc_forward(NON_SECURE, REALM, smc_fid, 30577c27753SZelalem Aweke x1, x2, x3, x4, handle); 30677c27753SZelalem Aweke } 30777c27753SZelalem Aweke 308319fb084SSoby Mathew if (src_sec_state != SMC_FROM_REALM) { 309319fb084SSoby Mathew SMC_RET1(handle, SMC_UNK); 310319fb084SSoby Mathew } 31177c27753SZelalem Aweke 31277c27753SZelalem Aweke switch (smc_fid) { 3138e51cccaSAlexeiFedorov case RMM_RMI_REQ_COMPLETE: { 3148e51cccaSAlexeiFedorov uint64_t x5 = SMC_GET_GP(handle, CTX_GPREG_X5); 31577c27753SZelalem Aweke 3168e51cccaSAlexeiFedorov return rmmd_smc_forward(REALM, NON_SECURE, x1, 3178e51cccaSAlexeiFedorov x2, x3, x4, x5, handle); 3188e51cccaSAlexeiFedorov } 31977c27753SZelalem Aweke default: 320319fb084SSoby Mathew WARN("RMMD: Unsupported RMM call 0x%08x\n", smc_fid); 32177c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 32277c27753SZelalem Aweke } 32377c27753SZelalem Aweke } 32477c27753SZelalem Aweke 32577c27753SZelalem Aweke /******************************************************************************* 32677c27753SZelalem Aweke * This cpu has been turned on. Enter RMM to initialise R-EL2. Entry into RMM 32777c27753SZelalem Aweke * is done after initialising minimal architectural state that guarantees safe 32877c27753SZelalem Aweke * execution. 32977c27753SZelalem Aweke ******************************************************************************/ 33077c27753SZelalem Aweke static void *rmmd_cpu_on_finish_handler(const void *arg) 33177c27753SZelalem Aweke { 3328c980a4aSJavier Almansa Sobrino long rc; 33377c27753SZelalem Aweke uint32_t linear_id = plat_my_core_pos(); 334*f708e9ddSRohit Mathew rmmd_rmm_context_t *ctx = PER_CPU_CUR(rmm_context); 33557824063SManish V Badarkhe /* Create a local copy of ep info to avoid race conditions */ 33657824063SManish V Badarkhe entry_point_info_t local_rmm_ep_info = *rmm_ep_info; 33777c27753SZelalem Aweke 3388c980a4aSJavier Almansa Sobrino if (rmm_boot_failed) { 3398c980a4aSJavier Almansa Sobrino /* RMM Boot failed on a previous CPU. Abort. */ 3408c980a4aSJavier Almansa Sobrino ERROR("RMM Failed to initialize. Ignoring for CPU%d\n", 3418c980a4aSJavier Almansa Sobrino linear_id); 3428c980a4aSJavier Almansa Sobrino return NULL; 3438c980a4aSJavier Almansa Sobrino } 3448c980a4aSJavier Almansa Sobrino 3458c980a4aSJavier Almansa Sobrino /* 3468c980a4aSJavier Almansa Sobrino * Prepare warmboot arguments for RMM: 3478c980a4aSJavier Almansa Sobrino * arg0: This CPUID. 34889d979ceSAndre Przywara * arg1: opaque activation token, as returned by previous calls 34989d979ceSAndre Przywara * arg2 to arg3: Not used. 3508c980a4aSJavier Almansa Sobrino */ 35157824063SManish V Badarkhe local_rmm_ep_info.args.arg0 = linear_id; 35257824063SManish V Badarkhe local_rmm_ep_info.args.arg1 = ctx->activation_token; 35357824063SManish V Badarkhe local_rmm_ep_info.args.arg2 = 0ULL; 35457824063SManish V Badarkhe local_rmm_ep_info.args.arg3 = 0ULL; 35577c27753SZelalem Aweke 35677c27753SZelalem Aweke /* Initialise RMM context with this entry point information */ 35757824063SManish V Badarkhe cm_setup_context(&ctx->cpu_ctx, &local_rmm_ep_info); 35877c27753SZelalem Aweke 35977c27753SZelalem Aweke rc = rmmd_rmm_sync_entry(ctx); 3608c980a4aSJavier Almansa Sobrino 3618c980a4aSJavier Almansa Sobrino if (rc != E_RMM_BOOT_SUCCESS) { 3628c980a4aSJavier Almansa Sobrino ERROR("RMM init failed on CPU%d: %ld\n", linear_id, rc); 36322bbb59fSAndre Przywara /* 36422bbb59fSAndre Przywara * TODO: Investigate handling of rmm_boot_failed under 36522bbb59fSAndre Przywara * concurrent access, or explore alternative approaches 36622bbb59fSAndre Przywara * to fixup the logic. 36722bbb59fSAndre Przywara */ 3688c980a4aSJavier Almansa Sobrino rmm_boot_failed = true; 36977c27753SZelalem Aweke } 37077c27753SZelalem Aweke 37177c27753SZelalem Aweke return NULL; 37277c27753SZelalem Aweke } 37377c27753SZelalem Aweke 37477c27753SZelalem Aweke /* Subscribe to PSCI CPU on to initialize RMM on secondary */ 37577c27753SZelalem Aweke SUBSCRIBE_TO_EVENT(psci_cpu_on_finish, rmmd_cpu_on_finish_handler); 37677c27753SZelalem Aweke 377319fb084SSoby Mathew /* Convert GPT lib error to RMMD GTS error */ 378319fb084SSoby Mathew static int gpt_to_gts_error(int error, uint32_t smc_fid, uint64_t address) 379319fb084SSoby Mathew { 380319fb084SSoby Mathew int ret; 381319fb084SSoby Mathew 382319fb084SSoby Mathew if (error == 0) { 383dc65ae46SJavier Almansa Sobrino return E_RMM_OK; 384319fb084SSoby Mathew } 385319fb084SSoby Mathew 386319fb084SSoby Mathew if (error == -EINVAL) { 387dc65ae46SJavier Almansa Sobrino ret = E_RMM_BAD_ADDR; 388319fb084SSoby Mathew } else { 389319fb084SSoby Mathew /* This is the only other error code we expect */ 390319fb084SSoby Mathew assert(error == -EPERM); 391dc65ae46SJavier Almansa Sobrino ret = E_RMM_BAD_PAS; 392319fb084SSoby Mathew } 393319fb084SSoby Mathew 394319fb084SSoby Mathew ERROR("RMMD: PAS Transition failed. GPT ret = %d, PA: 0x%"PRIx64 ", FID = 0x%x\n", 395319fb084SSoby Mathew error, address, smc_fid); 396319fb084SSoby Mathew return ret; 397319fb084SSoby Mathew } 398319fb084SSoby Mathew 3996a88ec8bSRaghu Krishnamurthy static int rmm_el3_ifc_get_feat_register(uint64_t feat_reg_idx, 4006a88ec8bSRaghu Krishnamurthy uint64_t *feat_reg) 4016a88ec8bSRaghu Krishnamurthy { 4026a88ec8bSRaghu Krishnamurthy if (feat_reg_idx != RMM_EL3_FEAT_REG_0_IDX) { 4036a88ec8bSRaghu Krishnamurthy ERROR("RMMD: Failed to get feature register %ld\n", feat_reg_idx); 4046a88ec8bSRaghu Krishnamurthy return E_RMM_INVAL; 4056a88ec8bSRaghu Krishnamurthy } 4066a88ec8bSRaghu Krishnamurthy 4076a88ec8bSRaghu Krishnamurthy *feat_reg = 0UL; 4086a88ec8bSRaghu Krishnamurthy #if RMMD_ENABLE_EL3_TOKEN_SIGN 4096a88ec8bSRaghu Krishnamurthy *feat_reg |= RMM_EL3_FEAT_REG_0_EL3_TOKEN_SIGN_MASK; 4106a88ec8bSRaghu Krishnamurthy #endif 4116a88ec8bSRaghu Krishnamurthy return E_RMM_OK; 4126a88ec8bSRaghu Krishnamurthy } 4136a88ec8bSRaghu Krishnamurthy 414f801fdc2STushar Khandelwal /* 41500e62ff9SJuan Pablo Conde * Update encryption key associated with mecid included in x1. 416f801fdc2STushar Khandelwal */ 41700e62ff9SJuan Pablo Conde static int rmmd_mecid_key_update(uint64_t x1) 418f801fdc2STushar Khandelwal { 419f801fdc2STushar Khandelwal uint64_t mecid_width, mecid_width_mask; 42000e62ff9SJuan Pablo Conde uint16_t mecid; 42100e62ff9SJuan Pablo Conde unsigned int reason; 422f801fdc2STushar Khandelwal int ret; 423f801fdc2STushar Khandelwal 424f801fdc2STushar Khandelwal /* 425609ada96SJuan Pablo Conde * Check whether FEAT_MEC is supported by the hardware. If not, return 426609ada96SJuan Pablo Conde * unknown SMC. 427609ada96SJuan Pablo Conde */ 428609ada96SJuan Pablo Conde if (is_feat_mec_supported() == false) { 429609ada96SJuan Pablo Conde return E_RMM_UNK; 430609ada96SJuan Pablo Conde } 431609ada96SJuan Pablo Conde 432609ada96SJuan Pablo Conde /* 433f801fdc2STushar Khandelwal * Check whether the mecid parameter is at most MECIDR_EL2.MECIDWidthm1 + 1 434f801fdc2STushar Khandelwal * in length. 435f801fdc2STushar Khandelwal */ 436f801fdc2STushar Khandelwal mecid_width = ((read_mecidr_el2() >> MECIDR_EL2_MECIDWidthm1_SHIFT) & 43700e62ff9SJuan Pablo Conde MECIDR_EL2_MECIDWidthm1_MASK) + 1UL; 43800e62ff9SJuan Pablo Conde mecid_width_mask = ((1UL << mecid_width) - 1UL); 43900e62ff9SJuan Pablo Conde 440c08285cfSSoby Mathew mecid = (x1 >> MECID_SHIFT) & MECID_MASK; 441f801fdc2STushar Khandelwal if ((mecid & ~mecid_width_mask) != 0U) { 442f801fdc2STushar Khandelwal return E_RMM_INVAL; 443f801fdc2STushar Khandelwal } 444f801fdc2STushar Khandelwal 44500e62ff9SJuan Pablo Conde reason = (x1 >> MEC_REFRESH_REASON_SHIFT) & MEC_REFRESH_REASON_MASK; 44600e62ff9SJuan Pablo Conde ret = plat_rmmd_mecid_key_update(mecid, reason); 447f801fdc2STushar Khandelwal 448f801fdc2STushar Khandelwal if (ret != 0) { 449f801fdc2STushar Khandelwal return E_RMM_UNK; 450f801fdc2STushar Khandelwal } 451f801fdc2STushar Khandelwal return E_RMM_OK; 452f801fdc2STushar Khandelwal } 453f801fdc2STushar Khandelwal 45477c27753SZelalem Aweke /******************************************************************************* 455319fb084SSoby Mathew * This function handles RMM-EL3 interface SMCs 45677c27753SZelalem Aweke ******************************************************************************/ 457319fb084SSoby Mathew uint64_t rmmd_rmm_el3_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, 45877c27753SZelalem Aweke uint64_t x3, uint64_t x4, void *cookie, 45977c27753SZelalem Aweke void *handle, uint64_t flags) 46077c27753SZelalem Aweke { 4616a88ec8bSRaghu Krishnamurthy uint64_t remaining_len = 0UL; 46277c27753SZelalem Aweke uint32_t src_sec_state; 4636a00e9b0SRobert Wakim int ret; 46477c27753SZelalem Aweke 4658c980a4aSJavier Almansa Sobrino /* If RMM failed to boot, treat any RMM-EL3 interface SMC as unknown */ 4668c980a4aSJavier Almansa Sobrino if (rmm_boot_failed) { 4678c980a4aSJavier Almansa Sobrino WARN("RMMD: Failed to boot up RMM. Ignoring RMM-EL3 call\n"); 4688c980a4aSJavier Almansa Sobrino SMC_RET1(handle, SMC_UNK); 4698c980a4aSJavier Almansa Sobrino } 4708c980a4aSJavier Almansa Sobrino 47177c27753SZelalem Aweke /* Determine which security state this SMC originated from */ 47277c27753SZelalem Aweke src_sec_state = caller_sec_state(flags); 47377c27753SZelalem Aweke 47477c27753SZelalem Aweke if (src_sec_state != SMC_FROM_REALM) { 475319fb084SSoby Mathew WARN("RMMD: RMM-EL3 call originated from secure or normal world\n"); 47677c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 47777c27753SZelalem Aweke } 47877c27753SZelalem Aweke 47977c27753SZelalem Aweke switch (smc_fid) { 480e50fedbcSJavier Almansa Sobrino case RMM_GTSI_DELEGATE: 4816a00e9b0SRobert Wakim ret = gpt_delegate_pas(x1, PAGE_SIZE_4KB, SMC_FROM_REALM); 482319fb084SSoby Mathew SMC_RET1(handle, gpt_to_gts_error(ret, smc_fid, x1)); 483e50fedbcSJavier Almansa Sobrino case RMM_GTSI_UNDELEGATE: 4846a00e9b0SRobert Wakim ret = gpt_undelegate_pas(x1, PAGE_SIZE_4KB, SMC_FROM_REALM); 485319fb084SSoby Mathew SMC_RET1(handle, gpt_to_gts_error(ret, smc_fid, x1)); 486e50fedbcSJavier Almansa Sobrino case RMM_ATTEST_GET_REALM_KEY: 487a0435105SSoby Mathew ret = rmmd_attest_get_signing_key(x1, &x2, x3); 488a0435105SSoby Mathew SMC_RET2(handle, ret, x2); 4891975d28bSSona Mathew case RMM_ATTEST_GET_PLAT_TOKEN: 4901975d28bSSona Mathew ret = rmmd_attest_get_platform_token(x1, &x2, x3, &remaining_len); 4911975d28bSSona Mathew SMC_RET3(handle, ret, x2, remaining_len); 4926a88ec8bSRaghu Krishnamurthy case RMM_EL3_FEATURES: 4936a88ec8bSRaghu Krishnamurthy ret = rmm_el3_ifc_get_feat_register(x1, &x2); 4946a88ec8bSRaghu Krishnamurthy SMC_RET2(handle, ret, x2); 4956a88ec8bSRaghu Krishnamurthy #if RMMD_ENABLE_EL3_TOKEN_SIGN 4966a88ec8bSRaghu Krishnamurthy case RMM_EL3_TOKEN_SIGN: 4976a88ec8bSRaghu Krishnamurthy return rmmd_el3_token_sign(handle, x1, x2, x3, x4); 4986a88ec8bSRaghu Krishnamurthy #endif 4992132c707SSona Mathew 5002132c707SSona Mathew #if RMMD_ENABLE_IDE_KEY_PROG 5012132c707SSona Mathew case RMM_IDE_KEY_PROG: 5022132c707SSona Mathew { 5032132c707SSona Mathew rp_ide_key_info_t ide_key_info; 5042132c707SSona Mathew 5052132c707SSona Mathew ide_key_info.keyqw0 = x4; 5062132c707SSona Mathew ide_key_info.keyqw1 = SMC_GET_GP(handle, CTX_GPREG_X5); 5072132c707SSona Mathew ide_key_info.keyqw2 = SMC_GET_GP(handle, CTX_GPREG_X6); 5082132c707SSona Mathew ide_key_info.keyqw3 = SMC_GET_GP(handle, CTX_GPREG_X7); 5092132c707SSona Mathew ide_key_info.ifvqw0 = SMC_GET_GP(handle, CTX_GPREG_X8); 5102132c707SSona Mathew ide_key_info.ifvqw1 = SMC_GET_GP(handle, CTX_GPREG_X9); 5112132c707SSona Mathew uint64_t x10 = SMC_GET_GP(handle, CTX_GPREG_X10); 5122132c707SSona Mathew uint64_t x11 = SMC_GET_GP(handle, CTX_GPREG_X11); 5132132c707SSona Mathew 5142132c707SSona Mathew ret = rmmd_el3_ide_key_program(x1, x2, x3, &ide_key_info, x10, x11); 5152132c707SSona Mathew SMC_RET1(handle, ret); 5162132c707SSona Mathew } 5172132c707SSona Mathew case RMM_IDE_KEY_SET_GO: 5182132c707SSona Mathew ret = rmmd_el3_ide_key_set_go(x1, x2, x3, x4, SMC_GET_GP(handle, CTX_GPREG_X5)); 5192132c707SSona Mathew SMC_RET1(handle, ret); 5202132c707SSona Mathew case RMM_IDE_KEY_SET_STOP: 5212132c707SSona Mathew ret = rmmd_el3_ide_key_set_stop(x1, x2, x3, x4, SMC_GET_GP(handle, CTX_GPREG_X5)); 5222132c707SSona Mathew SMC_RET1(handle, ret); 5232132c707SSona Mathew case RMM_IDE_KM_PULL_RESPONSE: { 5242132c707SSona Mathew uint64_t req_resp = 0, req_id = 0, cookie_var = 0; 5252132c707SSona Mathew 5262132c707SSona Mathew ret = rmmd_el3_ide_km_pull_response(x1, x2, &req_resp, &req_id, &cookie_var); 5272132c707SSona Mathew SMC_RET4(handle, ret, req_resp, req_id, cookie_var); 5282132c707SSona Mathew } 5292132c707SSona Mathew #endif /* RMMD_ENABLE_IDE_KEY_PROG */ 530745c129aSAndre Przywara case RMM_RESERVE_MEMORY: 531745c129aSAndre Przywara ret = rmmd_reserve_memory(x1, &x2); 532745c129aSAndre Przywara SMC_RET2(handle, ret, x2); 533745c129aSAndre Przywara 5348c980a4aSJavier Almansa Sobrino case RMM_BOOT_COMPLETE: 53589d979ceSAndre Przywara { 536*f708e9ddSRohit Mathew rmmd_rmm_context_t *ctx = PER_CPU_CUR(rmm_context); 53789d979ceSAndre Przywara 53889d979ceSAndre Przywara ctx->activation_token = x2; 5398c980a4aSJavier Almansa Sobrino VERBOSE("RMMD: running rmmd_rmm_sync_exit\n"); 5408c980a4aSJavier Almansa Sobrino rmmd_rmm_sync_exit(x1); 54189d979ceSAndre Przywara } 54200e62ff9SJuan Pablo Conde case RMM_MEC_REFRESH: 543f801fdc2STushar Khandelwal ret = rmmd_mecid_key_update(x1); 544f801fdc2STushar Khandelwal SMC_RET1(handle, ret); 54577c27753SZelalem Aweke default: 546319fb084SSoby Mathew WARN("RMMD: Unsupported RMM-EL3 call 0x%08x\n", smc_fid); 54777c27753SZelalem Aweke SMC_RET1(handle, SMC_UNK); 54877c27753SZelalem Aweke } 54977c27753SZelalem Aweke } 55022bbb59fSAndre Przywara 55122bbb59fSAndre Przywara /** 55222bbb59fSAndre Przywara * Helper to activate Primary CPU with the updated RMM, mainly used during 55322bbb59fSAndre Przywara * LFA of RMM. 55422bbb59fSAndre Przywara */ 55522bbb59fSAndre Przywara int rmmd_primary_activate(void) 55622bbb59fSAndre Przywara { 55722bbb59fSAndre Przywara int rc; 55822bbb59fSAndre Przywara 55922bbb59fSAndre Przywara rc = rmmd_setup(); 56022bbb59fSAndre Przywara if (rc != 0) { 56122bbb59fSAndre Przywara ERROR("rmmd_setup failed during LFA: %d\n", rc); 56222bbb59fSAndre Przywara return rc; 56322bbb59fSAndre Przywara } 56422bbb59fSAndre Przywara 56522bbb59fSAndre Przywara rc = rmm_init(); 5665ba2ad35SManish V Badarkhe if (rc == 0) { 56722bbb59fSAndre Przywara ERROR("rmm_init failed during LFA: %d\n", rc); 56822bbb59fSAndre Przywara return rc; 56922bbb59fSAndre Przywara } 57022bbb59fSAndre Przywara 57122bbb59fSAndre Przywara INFO("RMM warm reset done on primary during LFA. \n"); 57222bbb59fSAndre Przywara 57322bbb59fSAndre Przywara return 0; 57422bbb59fSAndre Przywara } 57522bbb59fSAndre Przywara 57622bbb59fSAndre Przywara /** 57722bbb59fSAndre Przywara * Helper to activate Primary CPU with the updated RMM, mainly used during 57822bbb59fSAndre Przywara * LFA of RMM. 57922bbb59fSAndre Przywara */ 58022bbb59fSAndre Przywara int rmmd_secondary_activate(void) 58122bbb59fSAndre Przywara { 58222bbb59fSAndre Przywara rmmd_cpu_on_finish_handler(NULL); 58322bbb59fSAndre Przywara 58422bbb59fSAndre Przywara return 0; 58522bbb59fSAndre Przywara } 586