xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_main.c (revision eaaf517cd1bd8c9d5e3e6d2d202a69a0cbcb45bf)
1bdd2596dSAchin Gupta /*
20cea2ae0SManish V Badarkhe  * Copyright (c) 2020-2023, Arm Limited and Contributors. All rights reserved.
3bdd2596dSAchin Gupta  *
4bdd2596dSAchin Gupta  * SPDX-License-Identifier: BSD-3-Clause
5bdd2596dSAchin Gupta  */
6bdd2596dSAchin Gupta 
7bdd2596dSAchin Gupta #include <assert.h>
8bdd2596dSAchin Gupta #include <errno.h>
94ce3e99aSScott Branden #include <inttypes.h>
104ce3e99aSScott Branden #include <stdint.h>
11bdd2596dSAchin Gupta #include <string.h>
12bdd2596dSAchin Gupta 
13bdd2596dSAchin Gupta #include <arch_helpers.h>
1452696946SOlivier Deprez #include <arch/aarch64/arch_features.h>
15bdd2596dSAchin Gupta #include <bl31/bl31.h>
168cb99c3fSOlivier Deprez #include <bl31/interrupt_mgmt.h>
17bdd2596dSAchin Gupta #include <common/debug.h>
18bdd2596dSAchin Gupta #include <common/runtime_svc.h>
190cea2ae0SManish V Badarkhe #include <common/tbbr/tbbr_img_def.h>
20bdd2596dSAchin Gupta #include <lib/el3_runtime/context_mgmt.h>
210cea2ae0SManish V Badarkhe #include <lib/fconf/fconf.h>
220cea2ae0SManish V Badarkhe #include <lib/fconf/fconf_dyn_cfg_getter.h>
23bdd2596dSAchin Gupta #include <lib/smccc.h>
24bdd2596dSAchin Gupta #include <lib/spinlock.h>
25bdd2596dSAchin Gupta #include <lib/utils.h>
260cea2ae0SManish V Badarkhe #include <lib/xlat_tables/xlat_tables_v2.h>
27bdd2596dSAchin Gupta #include <plat/common/common_def.h>
28bdd2596dSAchin Gupta #include <plat/common/platform.h>
29bdd2596dSAchin Gupta #include <platform_def.h>
30662af36dSJ-Alves #include <services/ffa_svc.h>
316da76075SMarc Bonnici #include <services/spmc_svc.h>
32bdd2596dSAchin Gupta #include <services/spmd_svc.h>
33bdd2596dSAchin Gupta #include <smccc_helpers.h>
34bdd2596dSAchin Gupta #include "spmd_private.h"
35bdd2596dSAchin Gupta 
36bdd2596dSAchin Gupta /*******************************************************************************
37bdd2596dSAchin Gupta  * SPM Core context information.
38bdd2596dSAchin Gupta  ******************************************************************************/
3952696946SOlivier Deprez static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT];
40bdd2596dSAchin Gupta 
41bdd2596dSAchin Gupta /*******************************************************************************
426da76075SMarc Bonnici  * SPM Core attribute information is read from its manifest if the SPMC is not
436da76075SMarc Bonnici  * at EL3. Else, it is populated from the SPMC directly.
44bdd2596dSAchin Gupta  ******************************************************************************/
4552696946SOlivier Deprez static spmc_manifest_attribute_t spmc_attrs;
460f14d02fSMax Shvetsov 
470f14d02fSMax Shvetsov /*******************************************************************************
480f14d02fSMax Shvetsov  * SPM Core entry point information. Discovered on the primary core and reused
490f14d02fSMax Shvetsov  * on secondary cores.
500f14d02fSMax Shvetsov  ******************************************************************************/
510f14d02fSMax Shvetsov static entry_point_info_t *spmc_ep_info;
520f14d02fSMax Shvetsov 
530f14d02fSMax Shvetsov /*******************************************************************************
5402d50bb0SOlivier Deprez  * SPM Core context on CPU based on mpidr.
5502d50bb0SOlivier Deprez  ******************************************************************************/
5602d50bb0SOlivier Deprez spmd_spm_core_context_t *spmd_get_context_by_mpidr(uint64_t mpidr)
5702d50bb0SOlivier Deprez {
58f7fb0bf7SMax Shvetsov 	int core_idx = plat_core_pos_by_mpidr(mpidr);
59f7fb0bf7SMax Shvetsov 
60f7fb0bf7SMax Shvetsov 	if (core_idx < 0) {
614ce3e99aSScott Branden 		ERROR("Invalid mpidr: %" PRIx64 ", returned ID: %d\n", mpidr, core_idx);
62f7fb0bf7SMax Shvetsov 		panic();
63f7fb0bf7SMax Shvetsov 	}
64f7fb0bf7SMax Shvetsov 
65f7fb0bf7SMax Shvetsov 	return &spm_core_context[core_idx];
6602d50bb0SOlivier Deprez }
6702d50bb0SOlivier Deprez 
6802d50bb0SOlivier Deprez /*******************************************************************************
6952696946SOlivier Deprez  * SPM Core context on current CPU get helper.
7052696946SOlivier Deprez  ******************************************************************************/
7152696946SOlivier Deprez spmd_spm_core_context_t *spmd_get_context(void)
7252696946SOlivier Deprez {
7302d50bb0SOlivier Deprez 	return spmd_get_context_by_mpidr(read_mpidr());
7452696946SOlivier Deprez }
7552696946SOlivier Deprez 
7652696946SOlivier Deprez /*******************************************************************************
77a92bc73bSOlivier Deprez  * SPM Core ID getter.
78a92bc73bSOlivier Deprez  ******************************************************************************/
79a92bc73bSOlivier Deprez uint16_t spmd_spmc_id_get(void)
80a92bc73bSOlivier Deprez {
81a92bc73bSOlivier Deprez 	return spmc_attrs.spmc_id;
82a92bc73bSOlivier Deprez }
83a92bc73bSOlivier Deprez 
84a92bc73bSOlivier Deprez /*******************************************************************************
850f14d02fSMax Shvetsov  * Static function declaration.
860f14d02fSMax Shvetsov  ******************************************************************************/
870f14d02fSMax Shvetsov static int32_t spmd_init(void);
8823d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr);
89662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle,
9052696946SOlivier Deprez 				       int error_code);
9152696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid,
9252696946SOlivier Deprez 				 bool secure_origin,
9352696946SOlivier Deprez 				 uint64_t x1,
9452696946SOlivier Deprez 				 uint64_t x2,
9552696946SOlivier Deprez 				 uint64_t x3,
9652696946SOlivier Deprez 				 uint64_t x4,
97bb01a673SMarc Bonnici 				 void *cookie,
98bb01a673SMarc Bonnici 				 void *handle,
99bb01a673SMarc Bonnici 				 uint64_t flags);
100bdd2596dSAchin Gupta 
1019944f557SDaniel Boulby /******************************************************************************
1029944f557SDaniel Boulby  * Builds an SPMD to SPMC direct message request.
1039944f557SDaniel Boulby  *****************************************************************************/
1049944f557SDaniel Boulby void spmd_build_spmc_message(gp_regs_t *gpregs, uint8_t target_func,
1059944f557SDaniel Boulby 			     unsigned long long message)
1069944f557SDaniel Boulby {
1079944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
1089944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X1,
1099944f557SDaniel Boulby 		(SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
1109944f557SDaniel Boulby 		 spmd_spmc_id_get());
1119944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X2, BIT(31) | target_func);
1129944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X3, message);
1139944f557SDaniel Boulby }
1149944f557SDaniel Boulby 
1159944f557SDaniel Boulby 
116bdd2596dSAchin Gupta /*******************************************************************************
11752696946SOlivier Deprez  * This function takes an SPMC context pointer and performs a synchronous
11852696946SOlivier Deprez  * SPMC entry.
119bdd2596dSAchin Gupta  ******************************************************************************/
120bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
121bdd2596dSAchin Gupta {
122bdd2596dSAchin Gupta 	uint64_t rc;
123bdd2596dSAchin Gupta 
124bdd2596dSAchin Gupta 	assert(spmc_ctx != NULL);
125bdd2596dSAchin Gupta 
126bdd2596dSAchin Gupta 	cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
127bdd2596dSAchin Gupta 
128bdd2596dSAchin Gupta 	/* Restore the context assigned above */
129033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
13028f39f02SMax Shvetsov 	cm_el2_sysregs_context_restore(SECURE);
131678ce223SOlivier Deprez #else
132678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(SECURE);
133033039f8SMax Shvetsov #endif
134bdd2596dSAchin Gupta 	cm_set_next_eret_context(SECURE);
135bdd2596dSAchin Gupta 
136033039f8SMax Shvetsov 	/* Enter SPMC */
137bdd2596dSAchin Gupta 	rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
138bdd2596dSAchin Gupta 
139bdd2596dSAchin Gupta 	/* Save secure state */
140033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
14128f39f02SMax Shvetsov 	cm_el2_sysregs_context_save(SECURE);
142678ce223SOlivier Deprez #else
143678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(SECURE);
144033039f8SMax Shvetsov #endif
145bdd2596dSAchin Gupta 
146bdd2596dSAchin Gupta 	return rc;
147bdd2596dSAchin Gupta }
148bdd2596dSAchin Gupta 
149bdd2596dSAchin Gupta /*******************************************************************************
15052696946SOlivier Deprez  * This function returns to the place where spmd_spm_core_sync_entry() was
151bdd2596dSAchin Gupta  * called originally.
152bdd2596dSAchin Gupta  ******************************************************************************/
153bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc)
154bdd2596dSAchin Gupta {
15552696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
156bdd2596dSAchin Gupta 
15752696946SOlivier Deprez 	/* Get current CPU context from SPMC context */
158bdd2596dSAchin Gupta 	assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
159bdd2596dSAchin Gupta 
160bdd2596dSAchin Gupta 	/*
161bdd2596dSAchin Gupta 	 * The SPMD must have initiated the original request through a
162bdd2596dSAchin Gupta 	 * synchronous entry into SPMC. Jump back to the original C runtime
163bdd2596dSAchin Gupta 	 * context with the value of rc in x0;
164bdd2596dSAchin Gupta 	 */
165bdd2596dSAchin Gupta 	spmd_spm_core_exit(ctx->c_rt_ctx, rc);
166bdd2596dSAchin Gupta 
167bdd2596dSAchin Gupta 	panic();
168bdd2596dSAchin Gupta }
169bdd2596dSAchin Gupta 
170bdd2596dSAchin Gupta /*******************************************************************************
17152696946SOlivier Deprez  * Jump to the SPM Core for the first time.
172bdd2596dSAchin Gupta  ******************************************************************************/
173bdd2596dSAchin Gupta static int32_t spmd_init(void)
174bdd2596dSAchin Gupta {
17552696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
17652696946SOlivier Deprez 	uint64_t rc;
177bdd2596dSAchin Gupta 
17852696946SOlivier Deprez 	VERBOSE("SPM Core init start.\n");
1799dcf63ddSOlivier Deprez 
180f2dcf418SOlivier Deprez 	/* Primary boot core enters the SPMC for initialization. */
181f2dcf418SOlivier Deprez 	ctx->state = SPMC_STATE_ON_PENDING;
182bdd2596dSAchin Gupta 
183bdd2596dSAchin Gupta 	rc = spmd_spm_core_sync_entry(ctx);
18452696946SOlivier Deprez 	if (rc != 0ULL) {
1854ce3e99aSScott Branden 		ERROR("SPMC initialisation failed 0x%" PRIx64 "\n", rc);
18652696946SOlivier Deprez 		return 0;
187bdd2596dSAchin Gupta 	}
188bdd2596dSAchin Gupta 
1899dcf63ddSOlivier Deprez 	ctx->state = SPMC_STATE_ON;
1909dcf63ddSOlivier Deprez 
19152696946SOlivier Deprez 	VERBOSE("SPM Core init end.\n");
192bdd2596dSAchin Gupta 
193bdd2596dSAchin Gupta 	return 1;
194bdd2596dSAchin Gupta }
195bdd2596dSAchin Gupta 
196bdd2596dSAchin Gupta /*******************************************************************************
1978cb99c3fSOlivier Deprez  * spmd_secure_interrupt_handler
1988cb99c3fSOlivier Deprez  * Enter the SPMC for further handling of the secure interrupt by the SPMC
1998cb99c3fSOlivier Deprez  * itself or a Secure Partition.
2008cb99c3fSOlivier Deprez  ******************************************************************************/
2018cb99c3fSOlivier Deprez static uint64_t spmd_secure_interrupt_handler(uint32_t id,
2028cb99c3fSOlivier Deprez 					      uint32_t flags,
2038cb99c3fSOlivier Deprez 					      void *handle,
2048cb99c3fSOlivier Deprez 					      void *cookie)
2058cb99c3fSOlivier Deprez {
2068cb99c3fSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
2078cb99c3fSOlivier Deprez 	gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
2088cb99c3fSOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
2098cb99c3fSOlivier Deprez 	int64_t rc;
2108cb99c3fSOlivier Deprez 
2118cb99c3fSOlivier Deprez 	/* Sanity check the security state when the exception was generated */
2128cb99c3fSOlivier Deprez 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
2138cb99c3fSOlivier Deprez 
2148cb99c3fSOlivier Deprez 	/* Sanity check the pointer to this cpu's context */
2158cb99c3fSOlivier Deprez 	assert(handle == cm_get_context(NON_SECURE));
2168cb99c3fSOlivier Deprez 
2178cb99c3fSOlivier Deprez 	/* Save the non-secure context before entering SPMC */
2188cb99c3fSOlivier Deprez 	cm_el1_sysregs_context_save(NON_SECURE);
2198cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2208cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_save(NON_SECURE);
2218cb99c3fSOlivier Deprez #endif
2228cb99c3fSOlivier Deprez 
2238cb99c3fSOlivier Deprez 	/* Convey the event to the SPMC through the FFA_INTERRUPT interface. */
2248cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_INTERRUPT);
2258cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X1, 0);
2268cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X2, 0);
2278cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X3, 0);
2288cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
2298cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
2308cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
2318cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
2328cb99c3fSOlivier Deprez 
2338cb99c3fSOlivier Deprez 	/* Mark current core as handling a secure interrupt. */
2348cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = true;
2358cb99c3fSOlivier Deprez 
2368cb99c3fSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
2378cb99c3fSOlivier Deprez 	if (rc != 0ULL) {
2380c23e6f4SOlivier Deprez 		ERROR("%s failed (%" PRId64 ") on CPU%u\n", __func__, rc, linear_id);
2398cb99c3fSOlivier Deprez 	}
2408cb99c3fSOlivier Deprez 
2418cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = false;
2428cb99c3fSOlivier Deprez 
2438cb99c3fSOlivier Deprez 	cm_el1_sysregs_context_restore(NON_SECURE);
2448cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2458cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_restore(NON_SECURE);
2468cb99c3fSOlivier Deprez #endif
2478cb99c3fSOlivier Deprez 	cm_set_next_eret_context(NON_SECURE);
2488cb99c3fSOlivier Deprez 
2498cb99c3fSOlivier Deprez 	SMC_RET0(&ctx->cpu_ctx);
2508cb99c3fSOlivier Deprez }
2518cb99c3fSOlivier Deprez 
2520cea2ae0SManish V Badarkhe #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
2530cea2ae0SManish V Badarkhe static int spmd_dynamic_map_mem(uintptr_t base_addr, size_t size,
2540cea2ae0SManish V Badarkhe 				 unsigned int attr, uintptr_t *align_addr,
2550cea2ae0SManish V Badarkhe 				 size_t *align_size)
2560cea2ae0SManish V Badarkhe {
2570cea2ae0SManish V Badarkhe 	uintptr_t base_addr_align;
2580cea2ae0SManish V Badarkhe 	size_t mapped_size_align;
2590cea2ae0SManish V Badarkhe 	int rc;
2600cea2ae0SManish V Badarkhe 
2610cea2ae0SManish V Badarkhe 	/* Page aligned address and size if necessary */
2620cea2ae0SManish V Badarkhe 	base_addr_align = page_align(base_addr, DOWN);
2630cea2ae0SManish V Badarkhe 	mapped_size_align = page_align(size, UP);
2640cea2ae0SManish V Badarkhe 
2650cea2ae0SManish V Badarkhe 	if ((base_addr != base_addr_align) &&
2660cea2ae0SManish V Badarkhe 	    (size == mapped_size_align)) {
2670cea2ae0SManish V Badarkhe 		mapped_size_align += PAGE_SIZE;
2680cea2ae0SManish V Badarkhe 	}
2690cea2ae0SManish V Badarkhe 
2700cea2ae0SManish V Badarkhe 	/*
2710cea2ae0SManish V Badarkhe 	 * Map dynamically given region with its aligned base address and
2720cea2ae0SManish V Badarkhe 	 * size
2730cea2ae0SManish V Badarkhe 	 */
2740cea2ae0SManish V Badarkhe 	rc = mmap_add_dynamic_region((unsigned long long)base_addr_align,
2750cea2ae0SManish V Badarkhe 				     base_addr_align,
2760cea2ae0SManish V Badarkhe 				     mapped_size_align,
2770cea2ae0SManish V Badarkhe 				     attr);
2780cea2ae0SManish V Badarkhe 	if (rc == 0) {
2790cea2ae0SManish V Badarkhe 		*align_addr = base_addr_align;
2800cea2ae0SManish V Badarkhe 		*align_size = mapped_size_align;
2810cea2ae0SManish V Badarkhe 	}
2820cea2ae0SManish V Badarkhe 
2830cea2ae0SManish V Badarkhe 	return rc;
2840cea2ae0SManish V Badarkhe }
2850cea2ae0SManish V Badarkhe 
2860cea2ae0SManish V Badarkhe static void spmd_do_sec_cpy(uintptr_t root_base_addr, uintptr_t sec_base_addr,
2870cea2ae0SManish V Badarkhe 			    size_t size)
2880cea2ae0SManish V Badarkhe {
2890cea2ae0SManish V Badarkhe 	uintptr_t root_base_addr_align, sec_base_addr_align;
2900cea2ae0SManish V Badarkhe 	size_t root_mapped_size_align, sec_mapped_size_align;
2910cea2ae0SManish V Badarkhe 	int rc;
2920cea2ae0SManish V Badarkhe 
2930cea2ae0SManish V Badarkhe 	assert(root_base_addr != 0UL);
2940cea2ae0SManish V Badarkhe 	assert(sec_base_addr != 0UL);
2950cea2ae0SManish V Badarkhe 	assert(size != 0UL);
2960cea2ae0SManish V Badarkhe 
2970cea2ae0SManish V Badarkhe 	/* Map the memory with required attributes */
2980cea2ae0SManish V Badarkhe 	rc = spmd_dynamic_map_mem(root_base_addr, size, MT_RO_DATA | MT_ROOT,
2990cea2ae0SManish V Badarkhe 				  &root_base_addr_align,
3000cea2ae0SManish V Badarkhe 				  &root_mapped_size_align);
3010cea2ae0SManish V Badarkhe 	if (rc != 0) {
3020cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while mapping", "root region",
3030cea2ae0SManish V Badarkhe 		      root_base_addr, rc);
3040cea2ae0SManish V Badarkhe 		panic();
3050cea2ae0SManish V Badarkhe 	}
3060cea2ae0SManish V Badarkhe 
3070cea2ae0SManish V Badarkhe 	rc = spmd_dynamic_map_mem(sec_base_addr, size, MT_RW_DATA | MT_SECURE,
3080cea2ae0SManish V Badarkhe 				  &sec_base_addr_align, &sec_mapped_size_align);
3090cea2ae0SManish V Badarkhe 	if (rc != 0) {
3100cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while mapping",
3110cea2ae0SManish V Badarkhe 		      "secure region", sec_base_addr, rc);
3120cea2ae0SManish V Badarkhe 		panic();
3130cea2ae0SManish V Badarkhe 	}
3140cea2ae0SManish V Badarkhe 
3150cea2ae0SManish V Badarkhe 	/* Do copy operation */
3160cea2ae0SManish V Badarkhe 	(void)memcpy((void *)sec_base_addr, (void *)root_base_addr, size);
3170cea2ae0SManish V Badarkhe 
3180cea2ae0SManish V Badarkhe 	/* Unmap root memory region */
3190cea2ae0SManish V Badarkhe 	rc = mmap_remove_dynamic_region(root_base_addr_align,
3200cea2ae0SManish V Badarkhe 					root_mapped_size_align);
3210cea2ae0SManish V Badarkhe 	if (rc != 0) {
3220cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while unmapping",
3230cea2ae0SManish V Badarkhe 		      "root region", root_base_addr_align, rc);
3240cea2ae0SManish V Badarkhe 		panic();
3250cea2ae0SManish V Badarkhe 	}
3260cea2ae0SManish V Badarkhe 
3270cea2ae0SManish V Badarkhe 	/* Unmap secure memory region */
3280cea2ae0SManish V Badarkhe 	rc = mmap_remove_dynamic_region(sec_base_addr_align,
3290cea2ae0SManish V Badarkhe 					sec_mapped_size_align);
3300cea2ae0SManish V Badarkhe 	if (rc != 0) {
3310cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while unmapping",
3320cea2ae0SManish V Badarkhe 		      "secure region", sec_base_addr_align, rc);
3330cea2ae0SManish V Badarkhe 		panic();
3340cea2ae0SManish V Badarkhe 	}
3350cea2ae0SManish V Badarkhe }
3360cea2ae0SManish V Badarkhe #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
3370cea2ae0SManish V Badarkhe 
3388cb99c3fSOlivier Deprez /*******************************************************************************
33952696946SOlivier Deprez  * Loads SPMC manifest and inits SPMC.
3400f14d02fSMax Shvetsov  ******************************************************************************/
34123d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr)
3420f14d02fSMax Shvetsov {
343f2dcf418SOlivier Deprez 	cpu_context_t *cpu_ctx;
344f2dcf418SOlivier Deprez 	unsigned int core_id;
3458cb99c3fSOlivier Deprez 	uint32_t ep_attr, flags;
34652696946SOlivier Deprez 	int rc;
3470cea2ae0SManish V Badarkhe 	const struct dyn_cfg_dtb_info_t *image_info __unused;
3480f14d02fSMax Shvetsov 
34952696946SOlivier Deprez 	/* Load the SPM Core manifest */
35023d5ba86SOlivier Deprez 	rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr);
3510f14d02fSMax Shvetsov 	if (rc != 0) {
35252696946SOlivier Deprez 		WARN("No or invalid SPM Core manifest image provided by BL2\n");
35352696946SOlivier Deprez 		return rc;
3540f14d02fSMax Shvetsov 	}
3550f14d02fSMax Shvetsov 
3560f14d02fSMax Shvetsov 	/*
35752696946SOlivier Deprez 	 * Ensure that the SPM Core version is compatible with the SPM
35852696946SOlivier Deprez 	 * Dispatcher version.
3590f14d02fSMax Shvetsov 	 */
360662af36dSJ-Alves 	if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) ||
361662af36dSJ-Alves 	    (spmc_attrs.minor_version > FFA_VERSION_MINOR)) {
362662af36dSJ-Alves 		WARN("Unsupported FFA version (%u.%u)\n",
3630f14d02fSMax Shvetsov 		     spmc_attrs.major_version, spmc_attrs.minor_version);
36452696946SOlivier Deprez 		return -EINVAL;
3650f14d02fSMax Shvetsov 	}
3660f14d02fSMax Shvetsov 
367662af36dSJ-Alves 	VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version,
3680f14d02fSMax Shvetsov 	     spmc_attrs.minor_version);
3690f14d02fSMax Shvetsov 
37052696946SOlivier Deprez 	VERBOSE("SPM Core run time EL%x.\n",
371033039f8SMax Shvetsov 	     SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
3720f14d02fSMax Shvetsov 
373ac03ac5eSMax Shvetsov 	/* Validate the SPMC ID, Ensure high bit is set */
37452696946SOlivier Deprez 	if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
37552696946SOlivier Deprez 			SPMC_SECURE_ID_MASK) == 0U) {
37652696946SOlivier Deprez 		WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id);
37752696946SOlivier Deprez 		return -EINVAL;
378ac03ac5eSMax Shvetsov 	}
379ac03ac5eSMax Shvetsov 
38052696946SOlivier Deprez 	/* Validate the SPM Core execution state */
3810f14d02fSMax Shvetsov 	if ((spmc_attrs.exec_state != MODE_RW_64) &&
3820f14d02fSMax Shvetsov 	    (spmc_attrs.exec_state != MODE_RW_32)) {
38323d5ba86SOlivier Deprez 		WARN("Unsupported %s%x.\n", "SPM Core execution state 0x",
3840f14d02fSMax Shvetsov 		     spmc_attrs.exec_state);
38552696946SOlivier Deprez 		return -EINVAL;
3860f14d02fSMax Shvetsov 	}
3870f14d02fSMax Shvetsov 
38823d5ba86SOlivier Deprez 	VERBOSE("%s%x.\n", "SPM Core execution state 0x",
38923d5ba86SOlivier Deprez 		spmc_attrs.exec_state);
3900f14d02fSMax Shvetsov 
391033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
392033039f8SMax Shvetsov 	/* Ensure manifest has not requested AArch32 state in S-EL2 */
393033039f8SMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
394033039f8SMax Shvetsov 		WARN("AArch32 state at S-EL2 is not supported.\n");
39552696946SOlivier Deprez 		return -EINVAL;
3960f14d02fSMax Shvetsov 	}
3970f14d02fSMax Shvetsov 
3980f14d02fSMax Shvetsov 	/*
3990f14d02fSMax Shvetsov 	 * Check if S-EL2 is supported on this system if S-EL2
4000f14d02fSMax Shvetsov 	 * is required for SPM
4010f14d02fSMax Shvetsov 	 */
40252696946SOlivier Deprez 	if (!is_armv8_4_sel2_present()) {
40352696946SOlivier Deprez 		WARN("SPM Core run time S-EL2 is not supported.\n");
40452696946SOlivier Deprez 		return -EINVAL;
4050f14d02fSMax Shvetsov 	}
406033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */
4070f14d02fSMax Shvetsov 
4080f14d02fSMax Shvetsov 	/* Initialise an entrypoint to set up the CPU context */
4090f14d02fSMax Shvetsov 	ep_attr = SECURE | EP_ST_ENABLE;
41052696946SOlivier Deprez 	if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) {
4110f14d02fSMax Shvetsov 		ep_attr |= EP_EE_BIG;
4120f14d02fSMax Shvetsov 	}
4130f14d02fSMax Shvetsov 
4140f14d02fSMax Shvetsov 	SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
4150f14d02fSMax Shvetsov 
4160f14d02fSMax Shvetsov 	/*
41752696946SOlivier Deprez 	 * Populate SPSR for SPM Core based upon validated parameters from the
41852696946SOlivier Deprez 	 * manifest.
4190f14d02fSMax Shvetsov 	 */
4200f14d02fSMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
4210f14d02fSMax Shvetsov 		spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
4220f14d02fSMax Shvetsov 						 SPSR_E_LITTLE,
4230f14d02fSMax Shvetsov 						 DAIF_FIQ_BIT |
4240f14d02fSMax Shvetsov 						 DAIF_IRQ_BIT |
4250f14d02fSMax Shvetsov 						 DAIF_ABT_BIT);
4260f14d02fSMax Shvetsov 	} else {
427033039f8SMax Shvetsov 
428033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
429033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL2;
430033039f8SMax Shvetsov #else
431033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL1;
432033039f8SMax Shvetsov #endif
433033039f8SMax Shvetsov 		spmc_ep_info->spsr = SPSR_64(runtime_el,
4340f14d02fSMax Shvetsov 					     MODE_SP_ELX,
4350f14d02fSMax Shvetsov 					     DISABLE_ALL_EXCEPTIONS);
4360f14d02fSMax Shvetsov 	}
4370f14d02fSMax Shvetsov 
4380cea2ae0SManish V Badarkhe #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
4390cea2ae0SManish V Badarkhe 	image_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TOS_FW_CONFIG_ID);
4400cea2ae0SManish V Badarkhe 	assert(image_info != NULL);
4410cea2ae0SManish V Badarkhe 
4420cea2ae0SManish V Badarkhe 	if ((image_info->config_addr == 0UL) ||
4430cea2ae0SManish V Badarkhe 	    (image_info->secondary_config_addr == 0UL) ||
4440cea2ae0SManish V Badarkhe 	    (image_info->config_max_size == 0UL)) {
4450cea2ae0SManish V Badarkhe 		return -EINVAL;
4460cea2ae0SManish V Badarkhe 	}
4470cea2ae0SManish V Badarkhe 
4480cea2ae0SManish V Badarkhe 	/* Copy manifest from root->secure region */
4490cea2ae0SManish V Badarkhe 	spmd_do_sec_cpy(image_info->config_addr,
4500cea2ae0SManish V Badarkhe 			image_info->secondary_config_addr,
4510cea2ae0SManish V Badarkhe 			image_info->config_max_size);
4520cea2ae0SManish V Badarkhe 
4530cea2ae0SManish V Badarkhe 	/* Update ep info of BL32 */
4540cea2ae0SManish V Badarkhe 	assert(spmc_ep_info != NULL);
4550cea2ae0SManish V Badarkhe 	spmc_ep_info->args.arg0 = image_info->secondary_config_addr;
4560cea2ae0SManish V Badarkhe #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
4570cea2ae0SManish V Badarkhe 
458f2dcf418SOlivier Deprez 	/* Set an initial SPMC context state for all cores. */
459f2dcf418SOlivier Deprez 	for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) {
460f2dcf418SOlivier Deprez 		spm_core_context[core_id].state = SPMC_STATE_OFF;
4610f14d02fSMax Shvetsov 
462f2dcf418SOlivier Deprez 		/* Setup an initial cpu context for the SPMC. */
463f2dcf418SOlivier Deprez 		cpu_ctx = &spm_core_context[core_id].cpu_ctx;
464f2dcf418SOlivier Deprez 		cm_setup_context(cpu_ctx, spmc_ep_info);
4650f14d02fSMax Shvetsov 
466f2dcf418SOlivier Deprez 		/*
467f2dcf418SOlivier Deprez 		 * Pass the core linear ID to the SPMC through x4.
468f2dcf418SOlivier Deprez 		 * (TF-A implementation defined behavior helping
469f2dcf418SOlivier Deprez 		 * a legacy TOS migration to adopt FF-A).
470f2dcf418SOlivier Deprez 		 */
471f2dcf418SOlivier Deprez 		write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id);
472f2dcf418SOlivier Deprez 	}
4730f14d02fSMax Shvetsov 
474a334c4e6SOlivier Deprez 	/* Register power management hooks with PSCI */
475a334c4e6SOlivier Deprez 	psci_register_spd_pm_hook(&spmd_pm);
476a334c4e6SOlivier Deprez 
4770f14d02fSMax Shvetsov 	/* Register init function for deferred init. */
4780f14d02fSMax Shvetsov 	bl31_register_bl32_init(&spmd_init);
4790f14d02fSMax Shvetsov 
480f2dcf418SOlivier Deprez 	INFO("SPM Core setup done.\n");
481f2dcf418SOlivier Deprez 
4828cb99c3fSOlivier Deprez 	/*
4838cb99c3fSOlivier Deprez 	 * Register an interrupt handler routing secure interrupts to SPMD
4848cb99c3fSOlivier Deprez 	 * while the NWd is running.
4858cb99c3fSOlivier Deprez 	 */
4868cb99c3fSOlivier Deprez 	flags = 0;
4878cb99c3fSOlivier Deprez 	set_interrupt_rm_flag(flags, NON_SECURE);
4888cb99c3fSOlivier Deprez 	rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
4898cb99c3fSOlivier Deprez 					     spmd_secure_interrupt_handler,
4908cb99c3fSOlivier Deprez 					     flags);
4918cb99c3fSOlivier Deprez 	if (rc != 0) {
4928cb99c3fSOlivier Deprez 		panic();
4938cb99c3fSOlivier Deprez 	}
4948cb99c3fSOlivier Deprez 
4950f14d02fSMax Shvetsov 	return 0;
4960f14d02fSMax Shvetsov }
4970f14d02fSMax Shvetsov 
4980f14d02fSMax Shvetsov /*******************************************************************************
49952696946SOlivier Deprez  * Initialize context of SPM Core.
500bdd2596dSAchin Gupta  ******************************************************************************/
5010f14d02fSMax Shvetsov int spmd_setup(void)
502bdd2596dSAchin Gupta {
503bdd2596dSAchin Gupta 	int rc;
5046da76075SMarc Bonnici 	void *spmc_manifest;
5056da76075SMarc Bonnici 
5066da76075SMarc Bonnici 	/*
5076da76075SMarc Bonnici 	 * If the SPMC is at EL3, then just initialise it directly. The
5086da76075SMarc Bonnici 	 * shenanigans of when it is at a lower EL are not needed.
5096da76075SMarc Bonnici 	 */
5106da76075SMarc Bonnici 	if (is_spmc_at_el3()) {
5116da76075SMarc Bonnici 		/* Allow the SPMC to populate its attributes directly. */
5126da76075SMarc Bonnici 		spmc_populate_attrs(&spmc_attrs);
5136da76075SMarc Bonnici 
5146da76075SMarc Bonnici 		rc = spmc_setup();
5156da76075SMarc Bonnici 		if (rc != 0) {
5166da76075SMarc Bonnici 			ERROR("SPMC initialisation failed 0x%x.\n", rc);
5176da76075SMarc Bonnici 		}
5186da76075SMarc Bonnici 		return rc;
5196da76075SMarc Bonnici 	}
520bdd2596dSAchin Gupta 
521bdd2596dSAchin Gupta 	spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
52252696946SOlivier Deprez 	if (spmc_ep_info == NULL) {
52352696946SOlivier Deprez 		WARN("No SPM Core image provided by BL2 boot loader.\n");
52452696946SOlivier Deprez 		return -EINVAL;
525bdd2596dSAchin Gupta 	}
526bdd2596dSAchin Gupta 
527bdd2596dSAchin Gupta 	/* Under no circumstances will this parameter be 0 */
52852696946SOlivier Deprez 	assert(spmc_ep_info->pc != 0ULL);
529bdd2596dSAchin Gupta 
530bdd2596dSAchin Gupta 	/*
531bdd2596dSAchin Gupta 	 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
53252696946SOlivier Deprez 	 * be used as a manifest for the SPM Core at the next lower EL/mode.
533bdd2596dSAchin Gupta 	 */
53423d5ba86SOlivier Deprez 	spmc_manifest = (void *)spmc_ep_info->args.arg0;
53523d5ba86SOlivier Deprez 	if (spmc_manifest == NULL) {
53623d5ba86SOlivier Deprez 		ERROR("Invalid or absent SPM Core manifest.\n");
53723d5ba86SOlivier Deprez 		return -EINVAL;
538bdd2596dSAchin Gupta 	}
539bdd2596dSAchin Gupta 
5400f14d02fSMax Shvetsov 	/* Load manifest, init SPMC */
54123d5ba86SOlivier Deprez 	rc = spmd_spmc_init(spmc_manifest);
5420f14d02fSMax Shvetsov 	if (rc != 0) {
54352696946SOlivier Deprez 		WARN("Booting device without SPM initialization.\n");
544bdd2596dSAchin Gupta 	}
545bdd2596dSAchin Gupta 
5460f14d02fSMax Shvetsov 	return rc;
5470f14d02fSMax Shvetsov }
5480f14d02fSMax Shvetsov 
5490f14d02fSMax Shvetsov /*******************************************************************************
550bb01a673SMarc Bonnici  * Forward FF-A SMCs to the other security state.
5510f14d02fSMax Shvetsov  ******************************************************************************/
552bb01a673SMarc Bonnici uint64_t spmd_smc_switch_state(uint32_t smc_fid,
55352696946SOlivier Deprez 			       bool secure_origin,
55452696946SOlivier Deprez 			       uint64_t x1,
55552696946SOlivier Deprez 			       uint64_t x2,
55652696946SOlivier Deprez 			       uint64_t x3,
55752696946SOlivier Deprez 			       uint64_t x4,
55852696946SOlivier Deprez 			       void *handle)
5590f14d02fSMax Shvetsov {
560c2901419SOlivier Deprez 	unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
561c2901419SOlivier Deprez 	unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
56293ff138bSOlivier Deprez 
5630f14d02fSMax Shvetsov 	/* Save incoming security state */
564033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
565678ce223SOlivier Deprez 	if (secure_state_in == NON_SECURE) {
566678ce223SOlivier Deprez 		cm_el1_sysregs_context_save(secure_state_in);
567678ce223SOlivier Deprez 	}
56893ff138bSOlivier Deprez 	cm_el2_sysregs_context_save(secure_state_in);
569678ce223SOlivier Deprez #else
570678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(secure_state_in);
571033039f8SMax Shvetsov #endif
5720f14d02fSMax Shvetsov 
5730f14d02fSMax Shvetsov 	/* Restore outgoing security state */
574033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
575678ce223SOlivier Deprez 	if (secure_state_out == NON_SECURE) {
576678ce223SOlivier Deprez 		cm_el1_sysregs_context_restore(secure_state_out);
577678ce223SOlivier Deprez 	}
57893ff138bSOlivier Deprez 	cm_el2_sysregs_context_restore(secure_state_out);
579678ce223SOlivier Deprez #else
580678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(secure_state_out);
581033039f8SMax Shvetsov #endif
58293ff138bSOlivier Deprez 	cm_set_next_eret_context(secure_state_out);
5830f14d02fSMax Shvetsov 
584*eaaf517cSRaghu Krishnamurthy #if SPMD_SPM_AT_SEL2
585*eaaf517cSRaghu Krishnamurthy 	/*
586*eaaf517cSRaghu Krishnamurthy 	 * If SPMC is at SEL2, save additional registers x8-x17, which may
587*eaaf517cSRaghu Krishnamurthy 	 * be used in FF-A calls such as FFA_PARTITION_INFO_GET_REGS.
588*eaaf517cSRaghu Krishnamurthy 	 * Note that technically, all SPMCs can support this, but this code is
589*eaaf517cSRaghu Krishnamurthy 	 * under ifdef to minimize breakage in case other SPMCs do not save
590*eaaf517cSRaghu Krishnamurthy 	 * and restore x8-x17.
591*eaaf517cSRaghu Krishnamurthy 	 * We also need to pass through these registers since not all FF-A ABIs
592*eaaf517cSRaghu Krishnamurthy 	 * modify x8-x17, in which case, SMCCC requires that these registers be
593*eaaf517cSRaghu Krishnamurthy 	 * preserved, so the SPMD passes through these registers and expects the
594*eaaf517cSRaghu Krishnamurthy 	 * SPMC to save and restore (potentially also modify) them.
595*eaaf517cSRaghu Krishnamurthy 	 */
596*eaaf517cSRaghu Krishnamurthy 	SMC_RET18(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4,
597*eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X5),
598*eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X6),
599*eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X7),
600*eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X8),
601*eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X9),
602*eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X10),
603*eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X11),
604*eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X12),
605*eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X13),
606*eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X14),
607*eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X15),
608*eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X16),
609*eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X17)
610*eaaf517cSRaghu Krishnamurthy 			);
611*eaaf517cSRaghu Krishnamurthy 
612*eaaf517cSRaghu Krishnamurthy #else
61393ff138bSOlivier Deprez 	SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4,
6140f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X5),
6150f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X6),
6160f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X7));
617*eaaf517cSRaghu Krishnamurthy #endif
6180f14d02fSMax Shvetsov }
6190f14d02fSMax Shvetsov 
6200f14d02fSMax Shvetsov /*******************************************************************************
621bb01a673SMarc Bonnici  * Forward SMCs to the other security state.
622bb01a673SMarc Bonnici  ******************************************************************************/
623bb01a673SMarc Bonnici static uint64_t spmd_smc_forward(uint32_t smc_fid,
624bb01a673SMarc Bonnici 				 bool secure_origin,
625bb01a673SMarc Bonnici 				 uint64_t x1,
626bb01a673SMarc Bonnici 				 uint64_t x2,
627bb01a673SMarc Bonnici 				 uint64_t x3,
628bb01a673SMarc Bonnici 				 uint64_t x4,
629bb01a673SMarc Bonnici 				 void *cookie,
630bb01a673SMarc Bonnici 				 void *handle,
631bb01a673SMarc Bonnici 				 uint64_t flags)
632bb01a673SMarc Bonnici {
633bb01a673SMarc Bonnici 	if (is_spmc_at_el3() && !secure_origin) {
634bb01a673SMarc Bonnici 		return spmc_smc_handler(smc_fid, secure_origin, x1, x2, x3, x4,
635bb01a673SMarc Bonnici 					cookie, handle, flags);
636bb01a673SMarc Bonnici 	}
637bb01a673SMarc Bonnici 	return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2, x3, x4,
638bb01a673SMarc Bonnici 				     handle);
639bb01a673SMarc Bonnici 
640bb01a673SMarc Bonnici }
641bb01a673SMarc Bonnici 
642bb01a673SMarc Bonnici /*******************************************************************************
643662af36dSJ-Alves  * Return FFA_ERROR with specified error code
6440f14d02fSMax Shvetsov  ******************************************************************************/
645662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle, int error_code)
6460f14d02fSMax Shvetsov {
647e46b2fd2SJ-Alves 	SMC_RET8(handle, (uint32_t) FFA_ERROR,
648e46b2fd2SJ-Alves 		 FFA_TARGET_INFO_MBZ, (uint32_t)error_code,
649662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
650662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
651bdd2596dSAchin Gupta }
652bdd2596dSAchin Gupta 
653f0d743dbSOlivier Deprez /*******************************************************************************
654f0d743dbSOlivier Deprez  * spmd_check_address_in_binary_image
655f0d743dbSOlivier Deprez  ******************************************************************************/
656f0d743dbSOlivier Deprez bool spmd_check_address_in_binary_image(uint64_t address)
657f0d743dbSOlivier Deprez {
658f0d743dbSOlivier Deprez 	assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size));
659f0d743dbSOlivier Deprez 
660f0d743dbSOlivier Deprez 	return ((address >= spmc_attrs.load_address) &&
661f0d743dbSOlivier Deprez 		(address < (spmc_attrs.load_address + spmc_attrs.binary_size)));
662f0d743dbSOlivier Deprez }
663f0d743dbSOlivier Deprez 
664c2901419SOlivier Deprez /******************************************************************************
665c2901419SOlivier Deprez  * spmd_is_spmc_message
666c2901419SOlivier Deprez  *****************************************************************************/
667c2901419SOlivier Deprez static bool spmd_is_spmc_message(unsigned int ep)
668c2901419SOlivier Deprez {
669bb01a673SMarc Bonnici 	if (is_spmc_at_el3()) {
670bb01a673SMarc Bonnici 		return false;
671bb01a673SMarc Bonnici 	}
672bb01a673SMarc Bonnici 
673c2901419SOlivier Deprez 	return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID)
674c2901419SOlivier Deprez 		&& (ffa_endpoint_source(ep) == spmc_attrs.spmc_id));
675c2901419SOlivier Deprez }
676c2901419SOlivier Deprez 
677f0d743dbSOlivier Deprez /******************************************************************************
678f0d743dbSOlivier Deprez  * spmd_handle_spmc_message
679f0d743dbSOlivier Deprez  *****************************************************************************/
680a92bc73bSOlivier Deprez static int spmd_handle_spmc_message(unsigned long long msg,
681a92bc73bSOlivier Deprez 		unsigned long long parm1, unsigned long long parm2,
682a92bc73bSOlivier Deprez 		unsigned long long parm3, unsigned long long parm4)
683f0d743dbSOlivier Deprez {
684f0d743dbSOlivier Deprez 	VERBOSE("%s %llx %llx %llx %llx %llx\n", __func__,
685f0d743dbSOlivier Deprez 		msg, parm1, parm2, parm3, parm4);
686f0d743dbSOlivier Deprez 
687f0d743dbSOlivier Deprez 	return -EINVAL;
688f0d743dbSOlivier Deprez }
689f0d743dbSOlivier Deprez 
690bdd2596dSAchin Gupta /*******************************************************************************
691bb01a673SMarc Bonnici  * This function forwards FF-A SMCs to either the main SPMD handler or the
692bb01a673SMarc Bonnici  * SPMC at EL3, depending on the origin security state, if enabled.
693bb01a673SMarc Bonnici  ******************************************************************************/
694bb01a673SMarc Bonnici uint64_t spmd_ffa_smc_handler(uint32_t smc_fid,
695bb01a673SMarc Bonnici 			      uint64_t x1,
696bb01a673SMarc Bonnici 			      uint64_t x2,
697bb01a673SMarc Bonnici 			      uint64_t x3,
698bb01a673SMarc Bonnici 			      uint64_t x4,
699bb01a673SMarc Bonnici 			      void *cookie,
700bb01a673SMarc Bonnici 			      void *handle,
701bb01a673SMarc Bonnici 			      uint64_t flags)
702bb01a673SMarc Bonnici {
703bb01a673SMarc Bonnici 	if (is_spmc_at_el3()) {
704bb01a673SMarc Bonnici 		/*
705bb01a673SMarc Bonnici 		 * If we have an SPMC at EL3 allow handling of the SMC first.
706bb01a673SMarc Bonnici 		 * The SPMC will call back through to SPMD handler if required.
707bb01a673SMarc Bonnici 		 */
708bb01a673SMarc Bonnici 		if (is_caller_secure(flags)) {
709bb01a673SMarc Bonnici 			return spmc_smc_handler(smc_fid,
710bb01a673SMarc Bonnici 						is_caller_secure(flags),
711bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
712bb01a673SMarc Bonnici 						handle, flags);
713bb01a673SMarc Bonnici 		}
714bb01a673SMarc Bonnici 	}
715bb01a673SMarc Bonnici 	return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
716bb01a673SMarc Bonnici 				handle, flags);
717bb01a673SMarc Bonnici }
718bb01a673SMarc Bonnici 
719bb01a673SMarc Bonnici /*******************************************************************************
720662af36dSJ-Alves  * This function handles all SMCs in the range reserved for FFA. Each call is
721bdd2596dSAchin Gupta  * either forwarded to the other security state or handled by the SPM dispatcher
722bdd2596dSAchin Gupta  ******************************************************************************/
72352696946SOlivier Deprez uint64_t spmd_smc_handler(uint32_t smc_fid,
72452696946SOlivier Deprez 			  uint64_t x1,
72552696946SOlivier Deprez 			  uint64_t x2,
72652696946SOlivier Deprez 			  uint64_t x3,
72752696946SOlivier Deprez 			  uint64_t x4,
72852696946SOlivier Deprez 			  void *cookie,
72952696946SOlivier Deprez 			  void *handle,
730bdd2596dSAchin Gupta 			  uint64_t flags)
731bdd2596dSAchin Gupta {
732cdb49d47SOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
73352696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
73493ff138bSOlivier Deprez 	bool secure_origin;
73593ff138bSOlivier Deprez 	int32_t ret;
7364388f28fSJ-Alves 	uint32_t input_version;
737bdd2596dSAchin Gupta 
738bdd2596dSAchin Gupta 	/* Determine which security state this SMC originated from */
73993ff138bSOlivier Deprez 	secure_origin = is_caller_secure(flags);
740bdd2596dSAchin Gupta 
7414ce3e99aSScott Branden 	VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64
7424ce3e99aSScott Branden 		" 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
743cdb49d47SOlivier Deprez 		    linear_id, smc_fid, x1, x2, x3, x4,
744cdb49d47SOlivier Deprez 		    SMC_GET_GP(handle, CTX_GPREG_X5),
745bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X6),
746bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X7));
747bdd2596dSAchin Gupta 
748bdd2596dSAchin Gupta 	switch (smc_fid) {
749662af36dSJ-Alves 	case FFA_ERROR:
750bdd2596dSAchin Gupta 		/*
751bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
75252696946SOlivier Deprez 		 * this CPU. If so, then indicate that the SPM Core initialised
753bdd2596dSAchin Gupta 		 * unsuccessfully.
754bdd2596dSAchin Gupta 		 */
7559dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
756bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(x2);
7570f14d02fSMax Shvetsov 		}
758bdd2596dSAchin Gupta 
75993ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
760bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
761bb01a673SMarc Bonnici 					handle, flags);
762bdd2596dSAchin Gupta 		break; /* not reached */
763bdd2596dSAchin Gupta 
764662af36dSJ-Alves 	case FFA_VERSION:
7654388f28fSJ-Alves 		input_version = (uint32_t)(0xFFFFFFFF & x1);
766bdd2596dSAchin Gupta 		/*
7674388f28fSJ-Alves 		 * If caller is secure and SPMC was initialized,
7684388f28fSJ-Alves 		 * return FFA_VERSION of SPMD.
7694388f28fSJ-Alves 		 * If caller is non secure and SPMC was initialized,
7709576fa93SMarc Bonnici 		 * forward to the EL3 SPMC if enabled, otherwise return
7719576fa93SMarc Bonnici 		 * the SPMC version if implemented at a lower EL.
7724388f28fSJ-Alves 		 * Sanity check to "input_version".
773bb01a673SMarc Bonnici 		 * If the EL3 SPMC is enabled, ignore the SPMC state as
774bb01a673SMarc Bonnici 		 * this is not used.
775bdd2596dSAchin Gupta 		 */
7764388f28fSJ-Alves 		if ((input_version & FFA_VERSION_BIT31_MASK) ||
777bb01a673SMarc Bonnici 		    (!is_spmc_at_el3() && (ctx->state == SPMC_STATE_RESET))) {
7784388f28fSJ-Alves 			ret = FFA_ERROR_NOT_SUPPORTED;
7794388f28fSJ-Alves 		} else if (!secure_origin) {
7809576fa93SMarc Bonnici 			if (is_spmc_at_el3()) {
7819576fa93SMarc Bonnici 				/*
7829576fa93SMarc Bonnici 				 * Forward the call directly to the EL3 SPMC, if
7839576fa93SMarc Bonnici 				 * enabled, as we don't need to wrap the call in
7849576fa93SMarc Bonnici 				 * a direct request.
7859576fa93SMarc Bonnici 				 */
7869576fa93SMarc Bonnici 				return spmd_smc_forward(smc_fid, secure_origin,
7879576fa93SMarc Bonnici 							x1, x2, x3, x4, cookie,
7889576fa93SMarc Bonnici 							handle, flags);
7899576fa93SMarc Bonnici 			}
7909576fa93SMarc Bonnici 
7919944f557SDaniel Boulby 			gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
7929944f557SDaniel Boulby 			uint64_t rc;
7939944f557SDaniel Boulby 
7949944f557SDaniel Boulby 			if (spmc_attrs.major_version == 1 &&
7959944f557SDaniel Boulby 			    spmc_attrs.minor_version == 0) {
796e46b2fd2SJ-Alves 				ret = MAKE_FFA_VERSION(spmc_attrs.major_version,
797e46b2fd2SJ-Alves 						       spmc_attrs.minor_version);
7989944f557SDaniel Boulby 				SMC_RET8(handle, (uint32_t)ret,
7999944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
8009944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
8019944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
8029944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
8039944f557SDaniel Boulby 					 FFA_PARAM_MBZ);
8049944f557SDaniel Boulby 				break;
8059944f557SDaniel Boulby 			}
8069944f557SDaniel Boulby 			/* Save non-secure system registers context */
8079944f557SDaniel Boulby 			cm_el1_sysregs_context_save(NON_SECURE);
8089944f557SDaniel Boulby #if SPMD_SPM_AT_SEL2
8099944f557SDaniel Boulby 			cm_el2_sysregs_context_save(NON_SECURE);
8109944f557SDaniel Boulby #endif
8119944f557SDaniel Boulby 
8129944f557SDaniel Boulby 			/*
8139944f557SDaniel Boulby 			 * The incoming request has FFA_VERSION as X0 smc_fid
8149944f557SDaniel Boulby 			 * and requested version in x1. Prepare a direct request
8159944f557SDaniel Boulby 			 * from SPMD to SPMC with FFA_VERSION framework function
8169944f557SDaniel Boulby 			 * identifier in X2 and requested version in X3.
8179944f557SDaniel Boulby 			 */
8189944f557SDaniel Boulby 			spmd_build_spmc_message(gpregs,
8199944f557SDaniel Boulby 						SPMD_FWK_MSG_FFA_VERSION_REQ,
8209944f557SDaniel Boulby 						input_version);
8219944f557SDaniel Boulby 
8229944f557SDaniel Boulby 			rc = spmd_spm_core_sync_entry(ctx);
8239944f557SDaniel Boulby 
8249944f557SDaniel Boulby 			if ((rc != 0ULL) ||
8259944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X0) !=
8269944f557SDaniel Boulby 				FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
8279944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X2) !=
82859bd2ad8SMarc Bonnici 				(FFA_FWK_MSG_BIT |
8299944f557SDaniel Boulby 				 SPMD_FWK_MSG_FFA_VERSION_RESP))) {
8309944f557SDaniel Boulby 				ERROR("Failed to forward FFA_VERSION\n");
8319944f557SDaniel Boulby 				ret = FFA_ERROR_NOT_SUPPORTED;
8329944f557SDaniel Boulby 			} else {
8339944f557SDaniel Boulby 				ret = SMC_GET_GP(gpregs, CTX_GPREG_X3);
8349944f557SDaniel Boulby 			}
8359944f557SDaniel Boulby 
8369944f557SDaniel Boulby 			/*
8379944f557SDaniel Boulby 			 * Return here after SPMC has handled FFA_VERSION.
8389944f557SDaniel Boulby 			 * The returned SPMC version is held in X3.
8399944f557SDaniel Boulby 			 * Forward this version in X0 to the non-secure caller.
8409944f557SDaniel Boulby 			 */
8419944f557SDaniel Boulby 			return spmd_smc_forward(ret, true, FFA_PARAM_MBZ,
8429944f557SDaniel Boulby 						FFA_PARAM_MBZ, FFA_PARAM_MBZ,
843bb01a673SMarc Bonnici 						FFA_PARAM_MBZ, cookie, gpregs,
844bb01a673SMarc Bonnici 						flags);
8454388f28fSJ-Alves 		} else {
846e46b2fd2SJ-Alves 			ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR,
847e46b2fd2SJ-Alves 					       FFA_VERSION_MINOR);
8484388f28fSJ-Alves 		}
8494388f28fSJ-Alves 
850e46b2fd2SJ-Alves 		SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ,
851e46b2fd2SJ-Alves 			 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
852e46b2fd2SJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
853bdd2596dSAchin Gupta 		break; /* not reached */
854bdd2596dSAchin Gupta 
855662af36dSJ-Alves 	case FFA_FEATURES:
856bdd2596dSAchin Gupta 		/*
857bdd2596dSAchin Gupta 		 * This is an optional interface. Do the minimal checks and
85852696946SOlivier Deprez 		 * forward to SPM Core which will handle it if implemented.
859bdd2596dSAchin Gupta 		 */
860bdd2596dSAchin Gupta 
86152696946SOlivier Deprez 		/* Forward SMC from Normal world to the SPM Core */
86293ff138bSOlivier Deprez 		if (!secure_origin) {
86393ff138bSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
864bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
865bb01a673SMarc Bonnici 						handle, flags);
86652696946SOlivier Deprez 		}
86752696946SOlivier Deprez 
868bdd2596dSAchin Gupta 		/*
869bdd2596dSAchin Gupta 		 * Return success if call was from secure world i.e. all
870662af36dSJ-Alves 		 * FFA functions are supported. This is essentially a
871bdd2596dSAchin Gupta 		 * nop.
872bdd2596dSAchin Gupta 		 */
873662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4,
874bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X5),
875bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X6),
876bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X7));
8770f14d02fSMax Shvetsov 
878bdd2596dSAchin Gupta 		break; /* not reached */
879bdd2596dSAchin Gupta 
880662af36dSJ-Alves 	case FFA_ID_GET:
881ac03ac5eSMax Shvetsov 		/*
882662af36dSJ-Alves 		 * Returns the ID of the calling FFA component.
883ac03ac5eSMax Shvetsov 		 */
884ac03ac5eSMax Shvetsov 		if (!secure_origin) {
885662af36dSJ-Alves 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
886662af36dSJ-Alves 				 FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID,
887662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
888662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
889662af36dSJ-Alves 				 FFA_PARAM_MBZ);
89052696946SOlivier Deprez 		}
89152696946SOlivier Deprez 
892662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
893662af36dSJ-Alves 			 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
894662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
895662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
896662af36dSJ-Alves 			 FFA_PARAM_MBZ);
897ac03ac5eSMax Shvetsov 
898ac03ac5eSMax Shvetsov 		break; /* not reached */
899ac03ac5eSMax Shvetsov 
900cdb49d47SOlivier Deprez 	case FFA_SECONDARY_EP_REGISTER_SMC64:
901cdb49d47SOlivier Deprez 		if (secure_origin) {
902cdb49d47SOlivier Deprez 			ret = spmd_pm_secondary_ep_register(x1);
903cdb49d47SOlivier Deprez 
904cdb49d47SOlivier Deprez 			if (ret < 0) {
905cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_ERROR_SMC64,
906cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, ret,
907cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
908cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
909cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
910cdb49d47SOlivier Deprez 			} else {
911cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_SUCCESS_SMC64,
912cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ,
913cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
914cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
915cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
916cdb49d47SOlivier Deprez 			}
917cdb49d47SOlivier Deprez 		}
918cdb49d47SOlivier Deprez 
919cdb49d47SOlivier Deprez 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
920cdb49d47SOlivier Deprez 		break; /* Not reached */
921cdb49d47SOlivier Deprez 
92270c121a2SDaniel Boulby 	case FFA_SPM_ID_GET:
92370c121a2SDaniel Boulby 		if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) {
92470c121a2SDaniel Boulby 			return spmd_ffa_error_return(handle,
92570c121a2SDaniel Boulby 						     FFA_ERROR_NOT_SUPPORTED);
92670c121a2SDaniel Boulby 		}
92770c121a2SDaniel Boulby 		/*
92870c121a2SDaniel Boulby 		 * Returns the ID of the SPMC or SPMD depending on the FF-A
92970c121a2SDaniel Boulby 		 * instance where this function is invoked
93070c121a2SDaniel Boulby 		 */
93170c121a2SDaniel Boulby 		if (!secure_origin) {
93270c121a2SDaniel Boulby 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
93370c121a2SDaniel Boulby 				 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
93470c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
93570c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
93670c121a2SDaniel Boulby 				 FFA_PARAM_MBZ);
93770c121a2SDaniel Boulby 		}
93870c121a2SDaniel Boulby 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
93970c121a2SDaniel Boulby 			 FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID,
94070c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
94170c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
94270c121a2SDaniel Boulby 			 FFA_PARAM_MBZ);
94370c121a2SDaniel Boulby 
94470c121a2SDaniel Boulby 		break; /* not reached */
94570c121a2SDaniel Boulby 
946f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_REQ_SMC32:
9475519f07cSShruti 	case FFA_MSG_SEND_DIRECT_REQ_SMC64:
9485519f07cSShruti 		if (!secure_origin) {
9495519f07cSShruti 			/* Validate source endpoint is non-secure for non-secure caller. */
9505519f07cSShruti 			if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) {
9515519f07cSShruti 				return spmd_ffa_error_return(handle,
9525519f07cSShruti 						FFA_ERROR_INVALID_PARAMETER);
9535519f07cSShruti 			}
9545519f07cSShruti 		}
955f0d743dbSOlivier Deprez 		if (secure_origin && spmd_is_spmc_message(x1)) {
956f0d743dbSOlivier Deprez 			ret = spmd_handle_spmc_message(x3, x4,
957f0d743dbSOlivier Deprez 				SMC_GET_GP(handle, CTX_GPREG_X5),
958f0d743dbSOlivier Deprez 				SMC_GET_GP(handle, CTX_GPREG_X6),
959f0d743dbSOlivier Deprez 				SMC_GET_GP(handle, CTX_GPREG_X7));
960f0d743dbSOlivier Deprez 
961f0d743dbSOlivier Deprez 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
962f0d743dbSOlivier Deprez 				FFA_TARGET_INFO_MBZ, ret,
963f0d743dbSOlivier Deprez 				FFA_PARAM_MBZ, FFA_PARAM_MBZ,
964f0d743dbSOlivier Deprez 				FFA_PARAM_MBZ, FFA_PARAM_MBZ,
965f0d743dbSOlivier Deprez 				FFA_PARAM_MBZ);
966f0d743dbSOlivier Deprez 		} else {
967f0d743dbSOlivier Deprez 			/* Forward direct message to the other world */
968f0d743dbSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
969bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
970bb01a673SMarc Bonnici 						handle, flags);
971f0d743dbSOlivier Deprez 		}
972f0d743dbSOlivier Deprez 		break; /* Not reached */
973f0d743dbSOlivier Deprez 
974f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_RESP_SMC32:
975f0d743dbSOlivier Deprez 		if (secure_origin && spmd_is_spmc_message(x1)) {
9768cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
977f0d743dbSOlivier Deprez 		} else {
978f0d743dbSOlivier Deprez 			/* Forward direct message to the other world */
979f0d743dbSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
980bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
981bb01a673SMarc Bonnici 						handle, flags);
982f0d743dbSOlivier Deprez 		}
983f0d743dbSOlivier Deprez 		break; /* Not reached */
984f0d743dbSOlivier Deprez 
985662af36dSJ-Alves 	case FFA_RX_RELEASE:
986662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC32:
987662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC64:
988662af36dSJ-Alves 	case FFA_RXTX_UNMAP:
989545b8eb3SRuari Phipps 	case FFA_PARTITION_INFO_GET:
990fc3f4800SJ-Alves #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
991fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_CREATE:
992fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_DESTROY:
993fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BIND:
994fc3f4800SJ-Alves 	case FFA_NOTIFICATION_UNBIND:
995fc3f4800SJ-Alves 	case FFA_NOTIFICATION_SET:
996fc3f4800SJ-Alves 	case FFA_NOTIFICATION_GET:
997fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET:
998fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET_SMC64:
999c2eba07cSFederico Recanati 	case FFA_MSG_SEND2:
1000d555233fSFederico Recanati 	case FFA_RX_ACQUIRE:
1001fc3f4800SJ-Alves #endif
1002662af36dSJ-Alves 	case FFA_MSG_RUN:
1003c2eba07cSFederico Recanati 		/*
1004c2eba07cSFederico Recanati 		 * Above calls should be invoked only by the Normal world and
1005c2eba07cSFederico Recanati 		 * must not be forwarded from Secure world to Normal world.
1006c2eba07cSFederico Recanati 		 */
100793ff138bSOlivier Deprez 		if (secure_origin) {
1008662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
1009662af36dSJ-Alves 						     FFA_ERROR_NOT_SUPPORTED);
1010bdd2596dSAchin Gupta 		}
1011bdd2596dSAchin Gupta 
1012e138400dSBoyan Karatotev 		/* Forward the call to the other world */
1013e138400dSBoyan Karatotev 		/* fallthrough */
1014662af36dSJ-Alves 	case FFA_MSG_SEND:
1015662af36dSJ-Alves 	case FFA_MSG_SEND_DIRECT_RESP_SMC64:
1016662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC32:
1017662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC64:
1018662af36dSJ-Alves 	case FFA_MEM_LEND_SMC32:
1019662af36dSJ-Alves 	case FFA_MEM_LEND_SMC64:
1020662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC32:
1021662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC64:
1022662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC32:
1023662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC64:
1024662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_RESP:
1025662af36dSJ-Alves 	case FFA_MEM_RELINQUISH:
1026662af36dSJ-Alves 	case FFA_MEM_RECLAIM:
1027642db984SMarc Bonnici 	case FFA_MEM_FRAG_TX:
1028642db984SMarc Bonnici 	case FFA_MEM_FRAG_RX:
1029662af36dSJ-Alves 	case FFA_SUCCESS_SMC32:
1030662af36dSJ-Alves 	case FFA_SUCCESS_SMC64:
1031bdd2596dSAchin Gupta 		/*
1032bdd2596dSAchin Gupta 		 * TODO: Assume that no requests originate from EL3 at the
1033bdd2596dSAchin Gupta 		 * moment. This will change if a SP service is required in
1034bdd2596dSAchin Gupta 		 * response to secure interrupts targeted to EL3. Until then
1035bdd2596dSAchin Gupta 		 * simply forward the call to the Normal world.
1036bdd2596dSAchin Gupta 		 */
1037bdd2596dSAchin Gupta 
103893ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
1039bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
1040bb01a673SMarc Bonnici 					handle, flags);
1041bdd2596dSAchin Gupta 		break; /* not reached */
1042bdd2596dSAchin Gupta 
1043662af36dSJ-Alves 	case FFA_MSG_WAIT:
1044bdd2596dSAchin Gupta 		/*
1045bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
1046bdd2596dSAchin Gupta 		 * this CPU from the Secure world. If so, then indicate that the
104752696946SOlivier Deprez 		 * SPM Core initialised successfully.
1048bdd2596dSAchin Gupta 		 */
10499dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
10508cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
1051bdd2596dSAchin Gupta 		}
1052bdd2596dSAchin Gupta 
1053e138400dSBoyan Karatotev 		/* Forward the call to the other world */
1054e138400dSBoyan Karatotev 		/* fallthrough */
1055386dc365SOlivier Deprez 	case FFA_INTERRUPT:
1056662af36dSJ-Alves 	case FFA_MSG_YIELD:
1057bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Secure world */
105893ff138bSOlivier Deprez 		if (!secure_origin) {
1059662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
1060662af36dSJ-Alves 						      FFA_ERROR_NOT_SUPPORTED);
1061bdd2596dSAchin Gupta 		}
1062bdd2596dSAchin Gupta 
106393ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
1064bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
1065bb01a673SMarc Bonnici 					handle, flags);
1066bdd2596dSAchin Gupta 		break; /* not reached */
1067bdd2596dSAchin Gupta 
10688cb99c3fSOlivier Deprez 	case FFA_NORMAL_WORLD_RESUME:
10698cb99c3fSOlivier Deprez 		if (secure_origin && ctx->secure_interrupt_ongoing) {
10708cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
10718cb99c3fSOlivier Deprez 		} else {
10728cb99c3fSOlivier Deprez 			return spmd_ffa_error_return(handle, FFA_ERROR_DENIED);
10738cb99c3fSOlivier Deprez 		}
10748cb99c3fSOlivier Deprez 		break; /* Not reached */
1075*eaaf517cSRaghu Krishnamurthy #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1076*eaaf517cSRaghu Krishnamurthy 	case FFA_PARTITION_INFO_GET_REGS_SMC64:
1077*eaaf517cSRaghu Krishnamurthy 		if (secure_origin) {
1078*eaaf517cSRaghu Krishnamurthy 			/* TODO: Future patches to enable support for this */
1079*eaaf517cSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1080*eaaf517cSRaghu Krishnamurthy 		}
10818cb99c3fSOlivier Deprez 
1082*eaaf517cSRaghu Krishnamurthy 		/* Call only supported with SMCCC 1.2+ */
1083*eaaf517cSRaghu Krishnamurthy 		if (MAKE_SMCCC_VERSION(SMCCC_MAJOR_VERSION, SMCCC_MINOR_VERSION) < 0x10002) {
1084*eaaf517cSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1085*eaaf517cSRaghu Krishnamurthy 		}
1086*eaaf517cSRaghu Krishnamurthy 
1087*eaaf517cSRaghu Krishnamurthy 		return spmd_smc_forward(smc_fid, secure_origin,
1088*eaaf517cSRaghu Krishnamurthy 					x1, x2, x3, x4, cookie,
1089*eaaf517cSRaghu Krishnamurthy 					handle, flags);
1090*eaaf517cSRaghu Krishnamurthy 		break; /* Not reached */
1091*eaaf517cSRaghu Krishnamurthy #endif
1092bdd2596dSAchin Gupta 	default:
1093bdd2596dSAchin Gupta 		WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
1094662af36dSJ-Alves 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1095bdd2596dSAchin Gupta 	}
1096bdd2596dSAchin Gupta }
1097