xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_main.c (revision f5dca2a95b09e9049ae95282681922c0705aac5f)
1bdd2596dSAchin Gupta /*
28f60d99fSRakshit Goyal  * Copyright (c) 2020-2025, 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>
23*f5dca2a9SRohit Mathew #include <lib/per_cpu/per_cpu.h>
24bdd2596dSAchin Gupta #include <lib/smccc.h>
25bdd2596dSAchin Gupta #include <lib/spinlock.h>
26bdd2596dSAchin Gupta #include <lib/utils.h>
270cea2ae0SManish V Badarkhe #include <lib/xlat_tables/xlat_tables_v2.h>
28bdd2596dSAchin Gupta #include <plat/common/common_def.h>
29bdd2596dSAchin Gupta #include <plat/common/platform.h>
30bdd2596dSAchin Gupta #include <platform_def.h>
31890b5088SRaghu Krishnamurthy #include <services/el3_spmd_logical_sp.h>
32662af36dSJ-Alves #include <services/ffa_svc.h>
336da76075SMarc Bonnici #include <services/spmc_svc.h>
34bdd2596dSAchin Gupta #include <services/spmd_svc.h>
35bdd2596dSAchin Gupta #include <smccc_helpers.h>
36bdd2596dSAchin Gupta #include "spmd_private.h"
3755fd56d7SYeoreum Yun #if TRANSFER_LIST
3855fd56d7SYeoreum Yun #include <transfer_list.h>
3955fd56d7SYeoreum Yun #endif
40bdd2596dSAchin Gupta 
41bdd2596dSAchin Gupta /*******************************************************************************
42bdd2596dSAchin Gupta  * SPM Core context information.
43bdd2596dSAchin Gupta  ******************************************************************************/
44*f5dca2a9SRohit Mathew static PER_CPU_DEFINE(spmd_spm_core_context_t, spm_core_context);
45bdd2596dSAchin Gupta 
46bdd2596dSAchin Gupta /*******************************************************************************
476da76075SMarc Bonnici  * SPM Core attribute information is read from its manifest if the SPMC is not
486da76075SMarc Bonnici  * at EL3. Else, it is populated from the SPMC directly.
49bdd2596dSAchin Gupta  ******************************************************************************/
5052696946SOlivier Deprez static spmc_manifest_attribute_t spmc_attrs;
510f14d02fSMax Shvetsov 
520f14d02fSMax Shvetsov /*******************************************************************************
53bb9fc8c0SJay Monkman  * FFA version used by nonsecure endpoint.
54bb9fc8c0SJay Monkman  ******************************************************************************/
55bb9fc8c0SJay Monkman static uint32_t nonsecure_ffa_version;
56bb9fc8c0SJay Monkman 
57bb9fc8c0SJay Monkman /*******************************************************************************
58bb9fc8c0SJay Monkman  * Whether the normal world finished negotiating its version.
59bb9fc8c0SJay Monkman  ******************************************************************************/
60bb9fc8c0SJay Monkman static bool nonsecure_version_negotiated;
61bb9fc8c0SJay Monkman 
62bb9fc8c0SJay Monkman /*******************************************************************************
63bb9fc8c0SJay Monkman  * FFA version used by SPMC, as seen by the normal world.
64bb9fc8c0SJay Monkman  ******************************************************************************/
65bb9fc8c0SJay Monkman static uint32_t spmc_nwd_ffa_version;
66bb9fc8c0SJay Monkman 
67bb9fc8c0SJay Monkman /*******************************************************************************
680f14d02fSMax Shvetsov  * SPM Core entry point information. Discovered on the primary core and reused
690f14d02fSMax Shvetsov  * on secondary cores.
700f14d02fSMax Shvetsov  ******************************************************************************/
710f14d02fSMax Shvetsov static entry_point_info_t *spmc_ep_info;
720f14d02fSMax Shvetsov 
730f14d02fSMax Shvetsov /*******************************************************************************
7452696946SOlivier Deprez  * SPM Core context on current CPU get helper.
7552696946SOlivier Deprez  ******************************************************************************/
7652696946SOlivier Deprez spmd_spm_core_context_t *spmd_get_context(void)
7752696946SOlivier Deprez {
78*f5dca2a9SRohit Mathew 	return PER_CPU_CUR(spm_core_context);
7952696946SOlivier Deprez }
8052696946SOlivier Deprez 
8152696946SOlivier Deprez /*******************************************************************************
82a92bc73bSOlivier Deprez  * SPM Core ID getter.
83a92bc73bSOlivier Deprez  ******************************************************************************/
84a92bc73bSOlivier Deprez uint16_t spmd_spmc_id_get(void)
85a92bc73bSOlivier Deprez {
86a92bc73bSOlivier Deprez 	return spmc_attrs.spmc_id;
87a92bc73bSOlivier Deprez }
88a92bc73bSOlivier Deprez 
89a92bc73bSOlivier Deprez /*******************************************************************************
900f14d02fSMax Shvetsov  * Static function declaration.
910f14d02fSMax Shvetsov  ******************************************************************************/
920f14d02fSMax Shvetsov static int32_t spmd_init(void);
9323d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr);
9495f7f6d8SRaghu Krishnamurthy 
9552696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid,
9652696946SOlivier Deprez 				 bool secure_origin,
9752696946SOlivier Deprez 				 uint64_t x1,
9852696946SOlivier Deprez 				 uint64_t x2,
9952696946SOlivier Deprez 				 uint64_t x3,
10052696946SOlivier Deprez 				 uint64_t x4,
101bb01a673SMarc Bonnici 				 void *cookie,
102bb01a673SMarc Bonnici 				 void *handle,
103bb9fc8c0SJay Monkman 				 uint64_t flags,
104bb9fc8c0SJay Monkman 				 uint32_t secure_ffa_version);
105bdd2596dSAchin Gupta 
1069944f557SDaniel Boulby /******************************************************************************
1079944f557SDaniel Boulby  * Builds an SPMD to SPMC direct message request.
1089944f557SDaniel Boulby  *****************************************************************************/
1099944f557SDaniel Boulby void spmd_build_spmc_message(gp_regs_t *gpregs, uint8_t target_func,
1109944f557SDaniel Boulby 			     unsigned long long message)
1119944f557SDaniel Boulby {
1129944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
1139944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X1,
1149944f557SDaniel Boulby 		(SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
1159944f557SDaniel Boulby 		 spmd_spmc_id_get());
1169944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X2, BIT(31) | target_func);
1179944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X3, message);
11876d53ee1SOlivier Deprez 
11976d53ee1SOlivier Deprez 	/* Zero out x4-x7 for the direct request emitted towards the SPMC. */
12076d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
12176d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
12276d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
12376d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
1249944f557SDaniel Boulby }
1259944f557SDaniel Boulby 
1269944f557SDaniel Boulby 
127bdd2596dSAchin Gupta /*******************************************************************************
12852696946SOlivier Deprez  * This function takes an SPMC context pointer and performs a synchronous
12952696946SOlivier Deprez  * SPMC entry.
130bdd2596dSAchin Gupta  ******************************************************************************/
131bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
132bdd2596dSAchin Gupta {
133bdd2596dSAchin Gupta 	uint64_t rc;
134bdd2596dSAchin Gupta 
135bdd2596dSAchin Gupta 	assert(spmc_ctx != NULL);
136bdd2596dSAchin Gupta 
137bdd2596dSAchin Gupta 	cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
138bdd2596dSAchin Gupta 
139bdd2596dSAchin Gupta 	/* Restore the context assigned above */
140033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
14128f39f02SMax Shvetsov 	cm_el2_sysregs_context_restore(SECURE);
142678ce223SOlivier Deprez #else
143678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(SECURE);
144033039f8SMax Shvetsov #endif
145bdd2596dSAchin Gupta 	cm_set_next_eret_context(SECURE);
146bdd2596dSAchin Gupta 
147033039f8SMax Shvetsov 	/* Enter SPMC */
148bdd2596dSAchin Gupta 	rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
149bdd2596dSAchin Gupta 
150bdd2596dSAchin Gupta 	/* Save secure state */
151033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
15228f39f02SMax Shvetsov 	cm_el2_sysregs_context_save(SECURE);
153678ce223SOlivier Deprez #else
154678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(SECURE);
155033039f8SMax Shvetsov #endif
156bdd2596dSAchin Gupta 
157bdd2596dSAchin Gupta 	return rc;
158bdd2596dSAchin Gupta }
159bdd2596dSAchin Gupta 
160bdd2596dSAchin Gupta /*******************************************************************************
16152696946SOlivier Deprez  * This function returns to the place where spmd_spm_core_sync_entry() was
162bdd2596dSAchin Gupta  * called originally.
163bdd2596dSAchin Gupta  ******************************************************************************/
164bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc)
165bdd2596dSAchin Gupta {
16652696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
167bdd2596dSAchin Gupta 
16852696946SOlivier Deprez 	/* Get current CPU context from SPMC context */
169bdd2596dSAchin Gupta 	assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
170bdd2596dSAchin Gupta 
171bdd2596dSAchin Gupta 	/*
172bdd2596dSAchin Gupta 	 * The SPMD must have initiated the original request through a
173bdd2596dSAchin Gupta 	 * synchronous entry into SPMC. Jump back to the original C runtime
174bdd2596dSAchin Gupta 	 * context with the value of rc in x0;
175bdd2596dSAchin Gupta 	 */
176bdd2596dSAchin Gupta 	spmd_spm_core_exit(ctx->c_rt_ctx, rc);
177bdd2596dSAchin Gupta 
178bdd2596dSAchin Gupta 	panic();
179bdd2596dSAchin Gupta }
180bdd2596dSAchin Gupta 
1819f3f4d87SBoyan Karatotev void spmd_setup_context(unsigned int core_id)
1829f3f4d87SBoyan Karatotev {
1839f3f4d87SBoyan Karatotev 	cpu_context_t *cpu_ctx;
1849f3f4d87SBoyan Karatotev 
185*f5dca2a9SRohit Mathew 	PER_CPU_CUR(spm_core_context)->state = SPMC_STATE_OFF;
1869f3f4d87SBoyan Karatotev 
1879f3f4d87SBoyan Karatotev 	/* Setup an initial cpu context for the SPMC. */
188*f5dca2a9SRohit Mathew 	cpu_ctx = &(PER_CPU_CUR(spm_core_context)->cpu_ctx);
1899f3f4d87SBoyan Karatotev 	cm_setup_context(cpu_ctx, spmc_ep_info);
1909f3f4d87SBoyan Karatotev 
1919f3f4d87SBoyan Karatotev 	/*
1929f3f4d87SBoyan Karatotev 	 * Pass the core linear ID to the SPMC through x4.
1939f3f4d87SBoyan Karatotev 	 * (TF-A implementation defined behavior helping
1949f3f4d87SBoyan Karatotev 	 * a legacy TOS migration to adopt FF-A).
1959f3f4d87SBoyan Karatotev 	 */
1969f3f4d87SBoyan Karatotev 	write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id);
1979f3f4d87SBoyan Karatotev }
1989f3f4d87SBoyan Karatotev 
199bdd2596dSAchin Gupta /*******************************************************************************
20052696946SOlivier Deprez  * Jump to the SPM Core for the first time.
201bdd2596dSAchin Gupta  ******************************************************************************/
202bdd2596dSAchin Gupta static int32_t spmd_init(void)
203bdd2596dSAchin Gupta {
20452696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
20552696946SOlivier Deprez 	uint64_t rc;
206bdd2596dSAchin Gupta 
20752696946SOlivier Deprez 	VERBOSE("SPM Core init start.\n");
2089dcf63ddSOlivier Deprez 
209f2dcf418SOlivier Deprez 	/* Primary boot core enters the SPMC for initialization. */
210f2dcf418SOlivier Deprez 	ctx->state = SPMC_STATE_ON_PENDING;
211bdd2596dSAchin Gupta 
212bdd2596dSAchin Gupta 	rc = spmd_spm_core_sync_entry(ctx);
21352696946SOlivier Deprez 	if (rc != 0ULL) {
2144ce3e99aSScott Branden 		ERROR("SPMC initialisation failed 0x%" PRIx64 "\n", rc);
21552696946SOlivier Deprez 		return 0;
216bdd2596dSAchin Gupta 	}
217bdd2596dSAchin Gupta 
2189dcf63ddSOlivier Deprez 	ctx->state = SPMC_STATE_ON;
2199dcf63ddSOlivier Deprez 
22052696946SOlivier Deprez 	VERBOSE("SPM Core init end.\n");
221bdd2596dSAchin Gupta 
222890b5088SRaghu Krishnamurthy 	spmd_logical_sp_set_spmc_initialized();
223890b5088SRaghu Krishnamurthy 	rc = spmd_logical_sp_init();
224890b5088SRaghu Krishnamurthy 	if (rc != 0) {
225890b5088SRaghu Krishnamurthy 		WARN("SPMD Logical partitions failed init.\n");
226890b5088SRaghu Krishnamurthy 	}
227890b5088SRaghu Krishnamurthy 
228bdd2596dSAchin Gupta 	return 1;
229bdd2596dSAchin Gupta }
230bdd2596dSAchin Gupta 
231bdd2596dSAchin Gupta /*******************************************************************************
2328cb99c3fSOlivier Deprez  * spmd_secure_interrupt_handler
2338cb99c3fSOlivier Deprez  * Enter the SPMC for further handling of the secure interrupt by the SPMC
2348cb99c3fSOlivier Deprez  * itself or a Secure Partition.
2358cb99c3fSOlivier Deprez  ******************************************************************************/
2368cb99c3fSOlivier Deprez static uint64_t spmd_secure_interrupt_handler(uint32_t id,
2378cb99c3fSOlivier Deprez 					      uint32_t flags,
2388cb99c3fSOlivier Deprez 					      void *handle,
2398cb99c3fSOlivier Deprez 					      void *cookie)
2408cb99c3fSOlivier Deprez {
2418cb99c3fSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
2428cb99c3fSOlivier Deprez 	gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
2438cb99c3fSOlivier Deprez 	int64_t rc;
2448cb99c3fSOlivier Deprez 
2458cb99c3fSOlivier Deprez 	/* Sanity check the security state when the exception was generated */
2468cb99c3fSOlivier Deprez 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
2478cb99c3fSOlivier Deprez 
2488cb99c3fSOlivier Deprez 	/* Sanity check the pointer to this cpu's context */
2498cb99c3fSOlivier Deprez 	assert(handle == cm_get_context(NON_SECURE));
2508cb99c3fSOlivier Deprez 
2518cb99c3fSOlivier Deprez 	/* Save the non-secure context before entering SPMC */
2528cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2538cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_save(NON_SECURE);
2542d960a11SMadhukar Pappireddy #else
2552d960a11SMadhukar Pappireddy 	cm_el1_sysregs_context_save(NON_SECURE);
25659bdcc58SMadhukar Pappireddy 
25759bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
25859bdcc58SMadhukar Pappireddy 	/*
25959bdcc58SMadhukar Pappireddy 	 * The hint bit denoting absence of SVE live state is effectively false
26059bdcc58SMadhukar Pappireddy 	 * in this scenario where execution was trapped to EL3 due to FIQ.
26159bdcc58SMadhukar Pappireddy 	 */
26259bdcc58SMadhukar Pappireddy 	simd_ctx_save(NON_SECURE, false);
2638f60d99fSRakshit Goyal 	simd_ctx_restore(SECURE);
26459bdcc58SMadhukar Pappireddy #endif
2658cb99c3fSOlivier Deprez #endif
2668cb99c3fSOlivier Deprez 
2678cb99c3fSOlivier Deprez 	/* Convey the event to the SPMC through the FFA_INTERRUPT interface. */
2688cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_INTERRUPT);
2698cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X1, 0);
2708cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X2, 0);
2718cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X3, 0);
2728cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
2738cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
2748cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
2758cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
2768cb99c3fSOlivier Deprez 
2778cb99c3fSOlivier Deprez 	/* Mark current core as handling a secure interrupt. */
2788cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = true;
2798cb99c3fSOlivier Deprez 
2808cb99c3fSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
28159bdcc58SMadhukar Pappireddy 
2828cb99c3fSOlivier Deprez 	if (rc != 0ULL) {
283eac8077aSOlivier Deprez 		ERROR("%s failed (%" PRId64 ") on CPU%u\n", __func__, rc, plat_my_core_pos());
2848cb99c3fSOlivier Deprez 	}
2858cb99c3fSOlivier Deprez 
2868cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = false;
2878cb99c3fSOlivier Deprez 
2888cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2898cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_restore(NON_SECURE);
2902d960a11SMadhukar Pappireddy #else
2912d960a11SMadhukar Pappireddy 	cm_el1_sysregs_context_restore(NON_SECURE);
29259bdcc58SMadhukar Pappireddy 
29359bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
2948f60d99fSRakshit Goyal 	simd_ctx_save(SECURE, false);
29559bdcc58SMadhukar Pappireddy 	simd_ctx_restore(NON_SECURE);
29659bdcc58SMadhukar Pappireddy #endif
2978cb99c3fSOlivier Deprez #endif
2988cb99c3fSOlivier Deprez 	cm_set_next_eret_context(NON_SECURE);
2998cb99c3fSOlivier Deprez 
3008cb99c3fSOlivier Deprez 	SMC_RET0(&ctx->cpu_ctx);
3018cb99c3fSOlivier Deprez }
3028cb99c3fSOlivier Deprez 
303bb6d0a17SOlivier Deprez #if (EL3_EXCEPTION_HANDLING == 0)
304a1e0e871SMadhukar Pappireddy /*******************************************************************************
305a1e0e871SMadhukar Pappireddy  * spmd_group0_interrupt_handler_nwd
306a1e0e871SMadhukar Pappireddy  * Group0 secure interrupt in the normal world are trapped to EL3. Delegate the
307a1e0e871SMadhukar Pappireddy  * handling of the interrupt to the platform handler, and return only upon
308a1e0e871SMadhukar Pappireddy  * successfully handling the Group0 interrupt.
309a1e0e871SMadhukar Pappireddy  ******************************************************************************/
310a1e0e871SMadhukar Pappireddy static uint64_t spmd_group0_interrupt_handler_nwd(uint32_t id,
311a1e0e871SMadhukar Pappireddy 						  uint32_t flags,
312a1e0e871SMadhukar Pappireddy 						  void *handle,
313a1e0e871SMadhukar Pappireddy 						  void *cookie)
314a1e0e871SMadhukar Pappireddy {
3155b10f25aSMadhukar Pappireddy 	uint32_t intid, intr_raw;
316a1e0e871SMadhukar Pappireddy 
317a1e0e871SMadhukar Pappireddy 	/* Sanity check the security state when the exception was generated. */
318a1e0e871SMadhukar Pappireddy 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
319a1e0e871SMadhukar Pappireddy 
320a1e0e871SMadhukar Pappireddy 	/* Sanity check the pointer to this cpu's context. */
321a1e0e871SMadhukar Pappireddy 	assert(handle == cm_get_context(NON_SECURE));
322a1e0e871SMadhukar Pappireddy 
323a1e0e871SMadhukar Pappireddy 	assert(id == INTR_ID_UNAVAILABLE);
324a1e0e871SMadhukar Pappireddy 
325a1e0e871SMadhukar Pappireddy 	assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
326a1e0e871SMadhukar Pappireddy 
3275b10f25aSMadhukar Pappireddy 	intr_raw = plat_ic_acknowledge_interrupt();
3285b10f25aSMadhukar Pappireddy 	intid = plat_ic_get_interrupt_id(intr_raw);
3295b10f25aSMadhukar Pappireddy 
3305b10f25aSMadhukar Pappireddy 	if (intid == INTR_ID_UNAVAILABLE) {
3315b10f25aSMadhukar Pappireddy 		return 0U;
3325b10f25aSMadhukar Pappireddy 	}
333a1e0e871SMadhukar Pappireddy 
334a1e0e871SMadhukar Pappireddy 	if (plat_spmd_handle_group0_interrupt(intid) < 0) {
335a1e0e871SMadhukar Pappireddy 		ERROR("Group0 interrupt %u not handled\n", intid);
336a1e0e871SMadhukar Pappireddy 		panic();
337a1e0e871SMadhukar Pappireddy 	}
338a1e0e871SMadhukar Pappireddy 
3396c91fc44SMadhukar Pappireddy 	/* Deactivate the corresponding Group0 interrupt. */
3406c91fc44SMadhukar Pappireddy 	plat_ic_end_of_interrupt(intid);
3416c91fc44SMadhukar Pappireddy 
342a1e0e871SMadhukar Pappireddy 	return 0U;
343a1e0e871SMadhukar Pappireddy }
344bb6d0a17SOlivier Deprez #endif
345a1e0e871SMadhukar Pappireddy 
3466671b3d8SMadhukar Pappireddy /*******************************************************************************
3476671b3d8SMadhukar Pappireddy  * spmd_handle_group0_intr_swd
3486671b3d8SMadhukar Pappireddy  * SPMC delegates handling of Group0 secure interrupt to EL3 firmware using
3496671b3d8SMadhukar Pappireddy  * FFA_EL3_INTR_HANDLE SMC call. Further, SPMD delegates the handling of the
3506671b3d8SMadhukar Pappireddy  * interrupt to the platform handler, and returns only upon successfully
3516671b3d8SMadhukar Pappireddy  * handling the Group0 interrupt.
3526671b3d8SMadhukar Pappireddy  ******************************************************************************/
3536671b3d8SMadhukar Pappireddy static uint64_t spmd_handle_group0_intr_swd(void *handle)
3546671b3d8SMadhukar Pappireddy {
3555b10f25aSMadhukar Pappireddy 	uint32_t intid, intr_raw;
3566671b3d8SMadhukar Pappireddy 
3576671b3d8SMadhukar Pappireddy 	/* Sanity check the pointer to this cpu's context */
3586671b3d8SMadhukar Pappireddy 	assert(handle == cm_get_context(SECURE));
3596671b3d8SMadhukar Pappireddy 
3606671b3d8SMadhukar Pappireddy 	assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
3616671b3d8SMadhukar Pappireddy 
3625b10f25aSMadhukar Pappireddy 	intr_raw = plat_ic_acknowledge_interrupt();
3635b10f25aSMadhukar Pappireddy 	intid = plat_ic_get_interrupt_id(intr_raw);
3645b10f25aSMadhukar Pappireddy 
3655b10f25aSMadhukar Pappireddy 	if (intid == INTR_ID_UNAVAILABLE) {
3665b10f25aSMadhukar Pappireddy 		return 0U;
3675b10f25aSMadhukar Pappireddy 	}
3686671b3d8SMadhukar Pappireddy 
3696671b3d8SMadhukar Pappireddy 	/*
3706671b3d8SMadhukar Pappireddy 	 * TODO: Currently due to a limitation in SPMD implementation, the
3716671b3d8SMadhukar Pappireddy 	 * platform handler is expected to not delegate handling to NWd while
3726671b3d8SMadhukar Pappireddy 	 * processing Group0 secure interrupt.
3736671b3d8SMadhukar Pappireddy 	 */
3746671b3d8SMadhukar Pappireddy 	if (plat_spmd_handle_group0_interrupt(intid) < 0) {
3756671b3d8SMadhukar Pappireddy 		/* Group0 interrupt was not handled by the platform. */
3766671b3d8SMadhukar Pappireddy 		ERROR("Group0 interrupt %u not handled\n", intid);
3776671b3d8SMadhukar Pappireddy 		panic();
3786671b3d8SMadhukar Pappireddy 	}
3796671b3d8SMadhukar Pappireddy 
3806c91fc44SMadhukar Pappireddy 	/* Deactivate the corresponding Group0 interrupt. */
3816c91fc44SMadhukar Pappireddy 	plat_ic_end_of_interrupt(intid);
3826c91fc44SMadhukar Pappireddy 
3836671b3d8SMadhukar Pappireddy 	/* Return success. */
3846671b3d8SMadhukar Pappireddy 	SMC_RET8(handle, FFA_SUCCESS_SMC32, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
3856671b3d8SMadhukar Pappireddy 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
3866671b3d8SMadhukar Pappireddy 		 FFA_PARAM_MBZ);
3876671b3d8SMadhukar Pappireddy }
3886671b3d8SMadhukar Pappireddy 
3890cea2ae0SManish V Badarkhe #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
3900cea2ae0SManish V Badarkhe static int spmd_dynamic_map_mem(uintptr_t base_addr, size_t size,
3910cea2ae0SManish V Badarkhe 				 unsigned int attr, uintptr_t *align_addr,
3920cea2ae0SManish V Badarkhe 				 size_t *align_size)
3930cea2ae0SManish V Badarkhe {
3940cea2ae0SManish V Badarkhe 	uintptr_t base_addr_align;
3950cea2ae0SManish V Badarkhe 	size_t mapped_size_align;
3960cea2ae0SManish V Badarkhe 	int rc;
3970cea2ae0SManish V Badarkhe 
3980cea2ae0SManish V Badarkhe 	/* Page aligned address and size if necessary */
3990cea2ae0SManish V Badarkhe 	base_addr_align = page_align(base_addr, DOWN);
4000cea2ae0SManish V Badarkhe 	mapped_size_align = page_align(size, UP);
4010cea2ae0SManish V Badarkhe 
4020cea2ae0SManish V Badarkhe 	if ((base_addr != base_addr_align) &&
4030cea2ae0SManish V Badarkhe 	    (size == mapped_size_align)) {
4040cea2ae0SManish V Badarkhe 		mapped_size_align += PAGE_SIZE;
4050cea2ae0SManish V Badarkhe 	}
4060cea2ae0SManish V Badarkhe 
4070cea2ae0SManish V Badarkhe 	/*
4080cea2ae0SManish V Badarkhe 	 * Map dynamically given region with its aligned base address and
4090cea2ae0SManish V Badarkhe 	 * size
4100cea2ae0SManish V Badarkhe 	 */
4110cea2ae0SManish V Badarkhe 	rc = mmap_add_dynamic_region((unsigned long long)base_addr_align,
4120cea2ae0SManish V Badarkhe 				     base_addr_align,
4130cea2ae0SManish V Badarkhe 				     mapped_size_align,
4140cea2ae0SManish V Badarkhe 				     attr);
4150cea2ae0SManish V Badarkhe 	if (rc == 0) {
4160cea2ae0SManish V Badarkhe 		*align_addr = base_addr_align;
4170cea2ae0SManish V Badarkhe 		*align_size = mapped_size_align;
4180cea2ae0SManish V Badarkhe 	}
4190cea2ae0SManish V Badarkhe 
4200cea2ae0SManish V Badarkhe 	return rc;
4210cea2ae0SManish V Badarkhe }
4220cea2ae0SManish V Badarkhe 
4230cea2ae0SManish V Badarkhe static void spmd_do_sec_cpy(uintptr_t root_base_addr, uintptr_t sec_base_addr,
4240cea2ae0SManish V Badarkhe 			    size_t size)
4250cea2ae0SManish V Badarkhe {
4260cea2ae0SManish V Badarkhe 	uintptr_t root_base_addr_align, sec_base_addr_align;
4270cea2ae0SManish V Badarkhe 	size_t root_mapped_size_align, sec_mapped_size_align;
4280cea2ae0SManish V Badarkhe 	int rc;
4290cea2ae0SManish V Badarkhe 
4300cea2ae0SManish V Badarkhe 	assert(root_base_addr != 0UL);
4310cea2ae0SManish V Badarkhe 	assert(sec_base_addr != 0UL);
4320cea2ae0SManish V Badarkhe 	assert(size != 0UL);
4330cea2ae0SManish V Badarkhe 
4340cea2ae0SManish V Badarkhe 	/* Map the memory with required attributes */
4350cea2ae0SManish V Badarkhe 	rc = spmd_dynamic_map_mem(root_base_addr, size, MT_RO_DATA | MT_ROOT,
4360cea2ae0SManish V Badarkhe 				  &root_base_addr_align,
4370cea2ae0SManish V Badarkhe 				  &root_mapped_size_align);
4380cea2ae0SManish V Badarkhe 	if (rc != 0) {
4390cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while mapping", "root region",
4400cea2ae0SManish V Badarkhe 		      root_base_addr, rc);
4410cea2ae0SManish V Badarkhe 		panic();
4420cea2ae0SManish V Badarkhe 	}
4430cea2ae0SManish V Badarkhe 
4440cea2ae0SManish V Badarkhe 	rc = spmd_dynamic_map_mem(sec_base_addr, size, MT_RW_DATA | MT_SECURE,
4450cea2ae0SManish V Badarkhe 				  &sec_base_addr_align, &sec_mapped_size_align);
4460cea2ae0SManish V Badarkhe 	if (rc != 0) {
4470cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while mapping",
4480cea2ae0SManish V Badarkhe 		      "secure region", sec_base_addr, rc);
4490cea2ae0SManish V Badarkhe 		panic();
4500cea2ae0SManish V Badarkhe 	}
4510cea2ae0SManish V Badarkhe 
4520cea2ae0SManish V Badarkhe 	/* Do copy operation */
4530cea2ae0SManish V Badarkhe 	(void)memcpy((void *)sec_base_addr, (void *)root_base_addr, size);
4540cea2ae0SManish V Badarkhe 
4550cea2ae0SManish V Badarkhe 	/* Unmap root memory region */
4560cea2ae0SManish V Badarkhe 	rc = mmap_remove_dynamic_region(root_base_addr_align,
4570cea2ae0SManish V Badarkhe 					root_mapped_size_align);
4580cea2ae0SManish V Badarkhe 	if (rc != 0) {
4590cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while unmapping",
4600cea2ae0SManish V Badarkhe 		      "root region", root_base_addr_align, rc);
4610cea2ae0SManish V Badarkhe 		panic();
4620cea2ae0SManish V Badarkhe 	}
4630cea2ae0SManish V Badarkhe 
4640cea2ae0SManish V Badarkhe 	/* Unmap secure memory region */
4650cea2ae0SManish V Badarkhe 	rc = mmap_remove_dynamic_region(sec_base_addr_align,
4660cea2ae0SManish V Badarkhe 					sec_mapped_size_align);
4670cea2ae0SManish V Badarkhe 	if (rc != 0) {
4680cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while unmapping",
4690cea2ae0SManish V Badarkhe 		      "secure region", sec_base_addr_align, rc);
4700cea2ae0SManish V Badarkhe 		panic();
4710cea2ae0SManish V Badarkhe 	}
4720cea2ae0SManish V Badarkhe }
4730cea2ae0SManish V Badarkhe #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
4740cea2ae0SManish V Badarkhe 
4758cb99c3fSOlivier Deprez /*******************************************************************************
47652696946SOlivier Deprez  * Loads SPMC manifest and inits SPMC.
4770f14d02fSMax Shvetsov  ******************************************************************************/
47823d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr)
4790f14d02fSMax Shvetsov {
4808cb99c3fSOlivier Deprez 	uint32_t ep_attr, flags;
48152696946SOlivier Deprez 	int rc;
4820cea2ae0SManish V Badarkhe 	const struct dyn_cfg_dtb_info_t *image_info __unused;
4830f14d02fSMax Shvetsov 
48452696946SOlivier Deprez 	/* Load the SPM Core manifest */
48523d5ba86SOlivier Deprez 	rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr);
4860f14d02fSMax Shvetsov 	if (rc != 0) {
48752696946SOlivier Deprez 		WARN("No or invalid SPM Core manifest image provided by BL2\n");
48852696946SOlivier Deprez 		return rc;
4890f14d02fSMax Shvetsov 	}
4900f14d02fSMax Shvetsov 
4910f14d02fSMax Shvetsov 	/*
49252696946SOlivier Deprez 	 * Ensure that the SPM Core version is compatible with the SPM
49352696946SOlivier Deprez 	 * Dispatcher version.
4940f14d02fSMax Shvetsov 	 */
495662af36dSJ-Alves 	if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) ||
496662af36dSJ-Alves 	    (spmc_attrs.minor_version > FFA_VERSION_MINOR)) {
497662af36dSJ-Alves 		WARN("Unsupported FFA version (%u.%u)\n",
4980f14d02fSMax Shvetsov 		     spmc_attrs.major_version, spmc_attrs.minor_version);
49952696946SOlivier Deprez 		return -EINVAL;
5000f14d02fSMax Shvetsov 	}
5010f14d02fSMax Shvetsov 
502662af36dSJ-Alves 	VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version,
5030f14d02fSMax Shvetsov 	     spmc_attrs.minor_version);
5040f14d02fSMax Shvetsov 
50552696946SOlivier Deprez 	VERBOSE("SPM Core run time EL%x.\n",
506033039f8SMax Shvetsov 	     SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
5070f14d02fSMax Shvetsov 
508ac03ac5eSMax Shvetsov 	/* Validate the SPMC ID, Ensure high bit is set */
50952696946SOlivier Deprez 	if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
51052696946SOlivier Deprez 			SPMC_SECURE_ID_MASK) == 0U) {
51152696946SOlivier Deprez 		WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id);
51252696946SOlivier Deprez 		return -EINVAL;
513ac03ac5eSMax Shvetsov 	}
514ac03ac5eSMax Shvetsov 
51552696946SOlivier Deprez 	/* Validate the SPM Core execution state */
5160f14d02fSMax Shvetsov 	if ((spmc_attrs.exec_state != MODE_RW_64) &&
5170f14d02fSMax Shvetsov 	    (spmc_attrs.exec_state != MODE_RW_32)) {
51823d5ba86SOlivier Deprez 		WARN("Unsupported %s%x.\n", "SPM Core execution state 0x",
5190f14d02fSMax Shvetsov 		     spmc_attrs.exec_state);
52052696946SOlivier Deprez 		return -EINVAL;
5210f14d02fSMax Shvetsov 	}
5220f14d02fSMax Shvetsov 
52323d5ba86SOlivier Deprez 	VERBOSE("%s%x.\n", "SPM Core execution state 0x",
52423d5ba86SOlivier Deprez 		spmc_attrs.exec_state);
5250f14d02fSMax Shvetsov 
526033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
527033039f8SMax Shvetsov 	/* Ensure manifest has not requested AArch32 state in S-EL2 */
528033039f8SMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
529033039f8SMax Shvetsov 		WARN("AArch32 state at S-EL2 is not supported.\n");
53052696946SOlivier Deprez 		return -EINVAL;
5310f14d02fSMax Shvetsov 	}
5320f14d02fSMax Shvetsov 
5330f14d02fSMax Shvetsov 	/*
5340f14d02fSMax Shvetsov 	 * Check if S-EL2 is supported on this system if S-EL2
5350f14d02fSMax Shvetsov 	 * is required for SPM
5360f14d02fSMax Shvetsov 	 */
537623f6140SAndre Przywara 	if (!is_feat_sel2_supported()) {
53852696946SOlivier Deprez 		WARN("SPM Core run time S-EL2 is not supported.\n");
53952696946SOlivier Deprez 		return -EINVAL;
5400f14d02fSMax Shvetsov 	}
541033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */
5420f14d02fSMax Shvetsov 
5430f14d02fSMax Shvetsov 	/* Initialise an entrypoint to set up the CPU context */
5440f14d02fSMax Shvetsov 	ep_attr = SECURE | EP_ST_ENABLE;
54552696946SOlivier Deprez 	if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) {
5460f14d02fSMax Shvetsov 		ep_attr |= EP_EE_BIG;
5470f14d02fSMax Shvetsov 	}
5480f14d02fSMax Shvetsov 
5490f14d02fSMax Shvetsov 	SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
5500f14d02fSMax Shvetsov 
5510f14d02fSMax Shvetsov 	/*
55252696946SOlivier Deprez 	 * Populate SPSR for SPM Core based upon validated parameters from the
55352696946SOlivier Deprez 	 * manifest.
5540f14d02fSMax Shvetsov 	 */
5550f14d02fSMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
5560f14d02fSMax Shvetsov 		spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
5570f14d02fSMax Shvetsov 						 SPSR_E_LITTLE,
5580f14d02fSMax Shvetsov 						 DAIF_FIQ_BIT |
5590f14d02fSMax Shvetsov 						 DAIF_IRQ_BIT |
5600f14d02fSMax Shvetsov 						 DAIF_ABT_BIT);
5610f14d02fSMax Shvetsov 	} else {
562033039f8SMax Shvetsov 
563033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
564033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL2;
565033039f8SMax Shvetsov #else
566033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL1;
567033039f8SMax Shvetsov #endif
568033039f8SMax Shvetsov 		spmc_ep_info->spsr = SPSR_64(runtime_el,
5690f14d02fSMax Shvetsov 					     MODE_SP_ELX,
5700f14d02fSMax Shvetsov 					     DISABLE_ALL_EXCEPTIONS);
5710f14d02fSMax Shvetsov 	}
5720f14d02fSMax Shvetsov 
5730cea2ae0SManish V Badarkhe #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
5740cea2ae0SManish V Badarkhe 	image_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TOS_FW_CONFIG_ID);
5750cea2ae0SManish V Badarkhe 	assert(image_info != NULL);
5760cea2ae0SManish V Badarkhe 
5770cea2ae0SManish V Badarkhe 	if ((image_info->config_addr == 0UL) ||
5780cea2ae0SManish V Badarkhe 	    (image_info->secondary_config_addr == 0UL) ||
5790cea2ae0SManish V Badarkhe 	    (image_info->config_max_size == 0UL)) {
5800cea2ae0SManish V Badarkhe 		return -EINVAL;
5810cea2ae0SManish V Badarkhe 	}
5820cea2ae0SManish V Badarkhe 
5830cea2ae0SManish V Badarkhe 	/* Copy manifest from root->secure region */
5840cea2ae0SManish V Badarkhe 	spmd_do_sec_cpy(image_info->config_addr,
5850cea2ae0SManish V Badarkhe 			image_info->secondary_config_addr,
5860cea2ae0SManish V Badarkhe 			image_info->config_max_size);
5870cea2ae0SManish V Badarkhe 
5880cea2ae0SManish V Badarkhe 	/* Update ep info of BL32 */
5890cea2ae0SManish V Badarkhe 	assert(spmc_ep_info != NULL);
5900cea2ae0SManish V Badarkhe 	spmc_ep_info->args.arg0 = image_info->secondary_config_addr;
5910cea2ae0SManish V Badarkhe #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
5920cea2ae0SManish V Badarkhe 
5939f3f4d87SBoyan Karatotev 	spmd_setup_context(plat_my_core_pos());
5940f14d02fSMax Shvetsov 
595a334c4e6SOlivier Deprez 	/* Register power management hooks with PSCI */
596a334c4e6SOlivier Deprez 	psci_register_spd_pm_hook(&spmd_pm);
597a334c4e6SOlivier Deprez 
5980f14d02fSMax Shvetsov 	/* Register init function for deferred init. */
5990f14d02fSMax Shvetsov 	bl31_register_bl32_init(&spmd_init);
6000f14d02fSMax Shvetsov 
601f2dcf418SOlivier Deprez 	INFO("SPM Core setup done.\n");
602f2dcf418SOlivier Deprez 
6038cb99c3fSOlivier Deprez 	/*
6048cb99c3fSOlivier Deprez 	 * Register an interrupt handler routing secure interrupts to SPMD
6058cb99c3fSOlivier Deprez 	 * while the NWd is running.
6068cb99c3fSOlivier Deprez 	 */
6078cb99c3fSOlivier Deprez 	flags = 0;
6088cb99c3fSOlivier Deprez 	set_interrupt_rm_flag(flags, NON_SECURE);
6098cb99c3fSOlivier Deprez 	rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
6108cb99c3fSOlivier Deprez 					     spmd_secure_interrupt_handler,
6118cb99c3fSOlivier Deprez 					     flags);
6128cb99c3fSOlivier Deprez 	if (rc != 0) {
6138cb99c3fSOlivier Deprez 		panic();
6148cb99c3fSOlivier Deprez 	}
6158cb99c3fSOlivier Deprez 
616a1e0e871SMadhukar Pappireddy 	/*
617bb6d0a17SOlivier Deprez 	 * Permit configurations where the SPM resides at S-EL1/2 and upon a
618bb6d0a17SOlivier Deprez 	 * Group0 interrupt triggering while the normal world runs, the
619bb6d0a17SOlivier Deprez 	 * interrupt is routed either through the EHF or directly to the SPMD:
620bb6d0a17SOlivier Deprez 	 *
621bb6d0a17SOlivier Deprez 	 * EL3_EXCEPTION_HANDLING=0: the Group0 interrupt is routed to the SPMD
622bb6d0a17SOlivier Deprez 	 *                   for handling by spmd_group0_interrupt_handler_nwd.
623bb6d0a17SOlivier Deprez 	 *
624bb6d0a17SOlivier Deprez 	 * EL3_EXCEPTION_HANDLING=1: the Group0 interrupt is routed to the EHF.
625bb6d0a17SOlivier Deprez 	 *
626bb6d0a17SOlivier Deprez 	 */
627bb6d0a17SOlivier Deprez #if (EL3_EXCEPTION_HANDLING == 0)
628bb6d0a17SOlivier Deprez 	/*
629fca5f0ebSMadhukar Pappireddy 	 * If EL3 interrupts are supported by the platform, register an
630fca5f0ebSMadhukar Pappireddy 	 * interrupt handler routing Group0 interrupts to SPMD while the NWd is
631fca5f0ebSMadhukar Pappireddy 	 * running.
632a1e0e871SMadhukar Pappireddy 	 */
633fca5f0ebSMadhukar Pappireddy 	if (plat_ic_has_interrupt_type(INTR_TYPE_EL3)) {
634a1e0e871SMadhukar Pappireddy 		rc = register_interrupt_type_handler(INTR_TYPE_EL3,
635a1e0e871SMadhukar Pappireddy 						     spmd_group0_interrupt_handler_nwd,
636a1e0e871SMadhukar Pappireddy 						     flags);
637a1e0e871SMadhukar Pappireddy 		if (rc != 0) {
638a1e0e871SMadhukar Pappireddy 			panic();
639a1e0e871SMadhukar Pappireddy 		}
640fca5f0ebSMadhukar Pappireddy 	}
641bb6d0a17SOlivier Deprez #endif
642bb6d0a17SOlivier Deprez 
6430f14d02fSMax Shvetsov 	return 0;
6440f14d02fSMax Shvetsov }
6450f14d02fSMax Shvetsov 
6460f14d02fSMax Shvetsov /*******************************************************************************
64752696946SOlivier Deprez  * Initialize context of SPM Core.
648bdd2596dSAchin Gupta  ******************************************************************************/
6490f14d02fSMax Shvetsov int spmd_setup(void)
650bdd2596dSAchin Gupta {
651bdd2596dSAchin Gupta 	int rc;
6526da76075SMarc Bonnici 	void *spmc_manifest;
65355fd56d7SYeoreum Yun 	struct transfer_list_header *tl __maybe_unused;
65455fd56d7SYeoreum Yun 	struct transfer_list_entry *te __maybe_unused;
6556da76075SMarc Bonnici 
6566da76075SMarc Bonnici 	/*
6576da76075SMarc Bonnici 	 * If the SPMC is at EL3, then just initialise it directly. The
6586da76075SMarc Bonnici 	 * shenanigans of when it is at a lower EL are not needed.
6596da76075SMarc Bonnici 	 */
6606da76075SMarc Bonnici 	if (is_spmc_at_el3()) {
6616da76075SMarc Bonnici 		/* Allow the SPMC to populate its attributes directly. */
6626da76075SMarc Bonnici 		spmc_populate_attrs(&spmc_attrs);
6636da76075SMarc Bonnici 
6646da76075SMarc Bonnici 		rc = spmc_setup();
6656da76075SMarc Bonnici 		if (rc != 0) {
6660d33649eSOlivier Deprez 			WARN("SPMC initialisation failed 0x%x.\n", rc);
6676da76075SMarc Bonnici 		}
6680d33649eSOlivier Deprez 		return 0;
6696da76075SMarc Bonnici 	}
670bdd2596dSAchin Gupta 
671bdd2596dSAchin Gupta 	spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
67252696946SOlivier Deprez 	if (spmc_ep_info == NULL) {
67352696946SOlivier Deprez 		WARN("No SPM Core image provided by BL2 boot loader.\n");
6740d33649eSOlivier Deprez 		return 0;
675bdd2596dSAchin Gupta 	}
676bdd2596dSAchin Gupta 
677bdd2596dSAchin Gupta 	/* Under no circumstances will this parameter be 0 */
67852696946SOlivier Deprez 	assert(spmc_ep_info->pc != 0ULL);
679bdd2596dSAchin Gupta 
68055fd56d7SYeoreum Yun 
68155fd56d7SYeoreum Yun #if TRANSFER_LIST && !RESET_TO_BL31
68255fd56d7SYeoreum Yun 	tl = (struct transfer_list_header *)spmc_ep_info->args.arg3;
68355fd56d7SYeoreum Yun 	te = transfer_list_find(tl, TL_TAG_DT_SPMC_MANIFEST);
68455fd56d7SYeoreum Yun 	if (te == NULL) {
68555fd56d7SYeoreum Yun 		WARN("SPM Core manifest absent in TRANSFER_LIST.\n");
68655fd56d7SYeoreum Yun 		return -ENOENT;
68755fd56d7SYeoreum Yun 	}
68855fd56d7SYeoreum Yun 
68955fd56d7SYeoreum Yun 	spmc_manifest = (void *)transfer_list_entry_data(te);
69055fd56d7SYeoreum Yun 
69155fd56d7SYeoreum Yun 	/* Change the DT in the handoff */
69255fd56d7SYeoreum Yun 	if (sizeof(spmc_ep_info->args.arg0) == sizeof(uint64_t)) {
69355fd56d7SYeoreum Yun 		spmc_ep_info->args.arg0 = (uintptr_t)spmc_manifest;
69455fd56d7SYeoreum Yun 	} else {
69555fd56d7SYeoreum Yun 		spmc_ep_info->args.arg3 = (uintptr_t)spmc_manifest;
69655fd56d7SYeoreum Yun 	}
69755fd56d7SYeoreum Yun #else
698bdd2596dSAchin Gupta 	/*
699bdd2596dSAchin Gupta 	 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
70052696946SOlivier Deprez 	 * be used as a manifest for the SPM Core at the next lower EL/mode.
701bdd2596dSAchin Gupta 	 */
70223d5ba86SOlivier Deprez 	spmc_manifest = (void *)spmc_ep_info->args.arg0;
70355fd56d7SYeoreum Yun #endif
70455fd56d7SYeoreum Yun 
70523d5ba86SOlivier Deprez 	if (spmc_manifest == NULL) {
7060d33649eSOlivier Deprez 		WARN("Invalid or absent SPM Core manifest.\n");
7070d33649eSOlivier Deprez 		return 0;
708bdd2596dSAchin Gupta 	}
709bdd2596dSAchin Gupta 
7100f14d02fSMax Shvetsov 	/* Load manifest, init SPMC */
71123d5ba86SOlivier Deprez 	rc = spmd_spmc_init(spmc_manifest);
7120f14d02fSMax Shvetsov 	if (rc != 0) {
71352696946SOlivier Deprez 		WARN("Booting device without SPM initialization.\n");
714bdd2596dSAchin Gupta 	}
715bdd2596dSAchin Gupta 
7160d33649eSOlivier Deprez 	return 0;
7170f14d02fSMax Shvetsov }
7180f14d02fSMax Shvetsov 
7190f14d02fSMax Shvetsov /*******************************************************************************
720bb01a673SMarc Bonnici  * Forward FF-A SMCs to the other security state.
7210f14d02fSMax Shvetsov  ******************************************************************************/
722bb01a673SMarc Bonnici uint64_t spmd_smc_switch_state(uint32_t smc_fid,
72352696946SOlivier Deprez 			       bool secure_origin,
72452696946SOlivier Deprez 			       uint64_t x1,
72552696946SOlivier Deprez 			       uint64_t x2,
72652696946SOlivier Deprez 			       uint64_t x3,
72752696946SOlivier Deprez 			       uint64_t x4,
728c925867eSOlivier Deprez 			       void *handle,
729bb9fc8c0SJay Monkman 			       uint64_t flags,
730bb9fc8c0SJay Monkman 			       uint32_t secure_ffa_version)
7310f14d02fSMax Shvetsov {
732c2901419SOlivier Deprez 	unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
733c2901419SOlivier Deprez 	unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
734bb9fc8c0SJay Monkman 	uint32_t version_in = (secure_origin) ? secure_ffa_version : nonsecure_ffa_version;
735bb9fc8c0SJay Monkman 	uint32_t version_out = (!secure_origin) ? secure_ffa_version : nonsecure_ffa_version;
736107e3cc0SOlivier Deprez 	void *ctx_out;
73793ff138bSOlivier Deprez 
738c925867eSOlivier Deprez #if SPMD_SPM_AT_SEL2
739c925867eSOlivier Deprez 	if ((secure_state_out == SECURE) && (is_sve_hint_set(flags) == true)) {
740c925867eSOlivier Deprez 		/*
741c925867eSOlivier Deprez 		 * Set the SVE hint bit in x0 and pass to the lower secure EL,
742c925867eSOlivier Deprez 		 * if it was set by the caller.
743c925867eSOlivier Deprez 		 */
744c925867eSOlivier Deprez 		smc_fid |= (FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT);
745c925867eSOlivier Deprez 	}
746c925867eSOlivier Deprez #endif
747c925867eSOlivier Deprez 
7480f14d02fSMax Shvetsov 	/* Save incoming security state */
749033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
75093ff138bSOlivier Deprez 	cm_el2_sysregs_context_save(secure_state_in);
751678ce223SOlivier Deprez #else
752678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(secure_state_in);
75359bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
75459bdcc58SMadhukar Pappireddy 	/* Forward the hint bit denoting the absence of SVE live state. */
75559bdcc58SMadhukar Pappireddy 	simd_ctx_save(secure_state_in, (!secure_origin && (is_sve_hint_set(flags) == true)));
75659bdcc58SMadhukar Pappireddy #endif
757033039f8SMax Shvetsov #endif
7580f14d02fSMax Shvetsov 
7590f14d02fSMax Shvetsov 	/* Restore outgoing security state */
760033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
76193ff138bSOlivier Deprez 	cm_el2_sysregs_context_restore(secure_state_out);
762678ce223SOlivier Deprez #else
763678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(secure_state_out);
76459bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
76559bdcc58SMadhukar Pappireddy 	simd_ctx_restore(secure_state_out);
76659bdcc58SMadhukar Pappireddy #endif
767033039f8SMax Shvetsov #endif
76893ff138bSOlivier Deprez 	cm_set_next_eret_context(secure_state_out);
7690f14d02fSMax Shvetsov 
770107e3cc0SOlivier Deprez 	ctx_out = cm_get_context(secure_state_out);
771a0a7f158SAndrei Homescu 	if (smc_fid == FFA_NORMAL_WORLD_RESUME) {
772a0a7f158SAndrei Homescu 		SMC_RET0(ctx_out);
773a0a7f158SAndrei Homescu 	}
774a0a7f158SAndrei Homescu 
775bb9fc8c0SJay Monkman 	if ((GET_SMC_CC(smc_fid) == SMC_64) && (version_out >= MAKE_FFA_VERSION(U(1), U(2)))) {
776bb9fc8c0SJay Monkman 		if (version_in < MAKE_FFA_VERSION(U(1), U(2))) {
777bb9fc8c0SJay Monkman 			/* FFA version mismatch, with dest >= 1.2 - set outgoing x8-x17 to zero */
778bb9fc8c0SJay Monkman 			SMC_RET18(ctx_out, smc_fid, x1, x2, x3, x4,
779bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X5),
780bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X6),
781bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X7),
782bb9fc8c0SJay Monkman 				  0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
783bb9fc8c0SJay Monkman 		} else {
784bb9fc8c0SJay Monkman 			/* Both FFA versions >= 1.2 - pass incoming x8-x17 to dest */
785107e3cc0SOlivier Deprez 			SMC_RET18(ctx_out, smc_fid, x1, x2, x3, x4,
786eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X5),
787eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X6),
788eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X7),
789eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X8),
790eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X9),
791eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X10),
792eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X11),
793eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X12),
794eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X13),
795eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X14),
796eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X15),
797eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X16),
798eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X17)
799eaaf517cSRaghu Krishnamurthy 				);
800bb9fc8c0SJay Monkman 		}
801bb9fc8c0SJay Monkman 	} else {
802bb9fc8c0SJay Monkman 		/* 32 bit call or dest has FFA version < 1.2 or unknown */
803107e3cc0SOlivier Deprez 		SMC_RET8(ctx_out, smc_fid, x1, x2, x3, x4,
8040f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X5),
8050f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X6),
8060f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X7));
807bb9fc8c0SJay Monkman 	}
8080f14d02fSMax Shvetsov }
8090f14d02fSMax Shvetsov 
8100f14d02fSMax Shvetsov /*******************************************************************************
811bb01a673SMarc Bonnici  * Forward SMCs to the other security state.
812bb01a673SMarc Bonnici  ******************************************************************************/
813bb01a673SMarc Bonnici static uint64_t spmd_smc_forward(uint32_t smc_fid,
814bb01a673SMarc Bonnici 				 bool secure_origin,
815bb01a673SMarc Bonnici 				 uint64_t x1,
816bb01a673SMarc Bonnici 				 uint64_t x2,
817bb01a673SMarc Bonnici 				 uint64_t x3,
818bb01a673SMarc Bonnici 				 uint64_t x4,
819bb01a673SMarc Bonnici 				 void *cookie,
820bb01a673SMarc Bonnici 				 void *handle,
821bb9fc8c0SJay Monkman 				 uint64_t flags,
822bb9fc8c0SJay Monkman 				 uint32_t secure_ffa_version)
823bb01a673SMarc Bonnici {
824bb01a673SMarc Bonnici 	if (is_spmc_at_el3() && !secure_origin) {
825bb01a673SMarc Bonnici 		return spmc_smc_handler(smc_fid, secure_origin, x1, x2, x3, x4,
826bb01a673SMarc Bonnici 					cookie, handle, flags);
827bb01a673SMarc Bonnici 	}
828c925867eSOlivier Deprez 
829bb01a673SMarc Bonnici 	return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2, x3, x4,
830bb9fc8c0SJay Monkman 				     handle, flags, secure_ffa_version);
831bb01a673SMarc Bonnici 
832bb01a673SMarc Bonnici }
833bb01a673SMarc Bonnici 
834bb01a673SMarc Bonnici /*******************************************************************************
835662af36dSJ-Alves  * Return FFA_ERROR with specified error code
8360f14d02fSMax Shvetsov  ******************************************************************************/
83795f7f6d8SRaghu Krishnamurthy uint64_t spmd_ffa_error_return(void *handle, int error_code)
8380f14d02fSMax Shvetsov {
839e46b2fd2SJ-Alves 	SMC_RET8(handle, (uint32_t) FFA_ERROR,
840e46b2fd2SJ-Alves 		 FFA_TARGET_INFO_MBZ, (uint32_t)error_code,
841662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
842662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
843bdd2596dSAchin Gupta }
844bdd2596dSAchin Gupta 
845f0d743dbSOlivier Deprez /*******************************************************************************
846f0d743dbSOlivier Deprez  * spmd_check_address_in_binary_image
847f0d743dbSOlivier Deprez  ******************************************************************************/
848f0d743dbSOlivier Deprez bool spmd_check_address_in_binary_image(uint64_t address)
849f0d743dbSOlivier Deprez {
850f0d743dbSOlivier Deprez 	assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size));
851f0d743dbSOlivier Deprez 
852f0d743dbSOlivier Deprez 	return ((address >= spmc_attrs.load_address) &&
853f0d743dbSOlivier Deprez 		(address < (spmc_attrs.load_address + spmc_attrs.binary_size)));
854f0d743dbSOlivier Deprez }
855f0d743dbSOlivier Deprez 
856c2901419SOlivier Deprez /******************************************************************************
857c2901419SOlivier Deprez  * spmd_is_spmc_message
858c2901419SOlivier Deprez  *****************************************************************************/
859c2901419SOlivier Deprez static bool spmd_is_spmc_message(unsigned int ep)
860c2901419SOlivier Deprez {
861bb01a673SMarc Bonnici 	if (is_spmc_at_el3()) {
862bb01a673SMarc Bonnici 		return false;
863bb01a673SMarc Bonnici 	}
864bb01a673SMarc Bonnici 
865c2901419SOlivier Deprez 	return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID)
866c2901419SOlivier Deprez 		&& (ffa_endpoint_source(ep) == spmc_attrs.spmc_id));
867c2901419SOlivier Deprez }
868c2901419SOlivier Deprez 
869bdd2596dSAchin Gupta /*******************************************************************************
870bb01a673SMarc Bonnici  * This function forwards FF-A SMCs to either the main SPMD handler or the
871bb01a673SMarc Bonnici  * SPMC at EL3, depending on the origin security state, if enabled.
872bb01a673SMarc Bonnici  ******************************************************************************/
873bb01a673SMarc Bonnici uint64_t spmd_ffa_smc_handler(uint32_t smc_fid,
874bb01a673SMarc Bonnici 			      uint64_t x1,
875bb01a673SMarc Bonnici 			      uint64_t x2,
876bb01a673SMarc Bonnici 			      uint64_t x3,
877bb01a673SMarc Bonnici 			      uint64_t x4,
878bb01a673SMarc Bonnici 			      void *cookie,
879bb01a673SMarc Bonnici 			      void *handle,
880bb01a673SMarc Bonnici 			      uint64_t flags)
881bb01a673SMarc Bonnici {
882bb01a673SMarc Bonnici 	if (is_spmc_at_el3()) {
883bb01a673SMarc Bonnici 		/*
884bb01a673SMarc Bonnici 		 * If we have an SPMC at EL3 allow handling of the SMC first.
885bb01a673SMarc Bonnici 		 * The SPMC will call back through to SPMD handler if required.
886bb01a673SMarc Bonnici 		 */
887bb01a673SMarc Bonnici 		if (is_caller_secure(flags)) {
888bb01a673SMarc Bonnici 			return spmc_smc_handler(smc_fid,
889bb01a673SMarc Bonnici 						is_caller_secure(flags),
890bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
891bb01a673SMarc Bonnici 						handle, flags);
892bb01a673SMarc Bonnici 		}
893bb01a673SMarc Bonnici 	}
894bb01a673SMarc Bonnici 	return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
895bb9fc8c0SJay Monkman 				handle, flags, spmc_nwd_ffa_version);
896bb9fc8c0SJay Monkman }
897bb9fc8c0SJay Monkman 
898bb9fc8c0SJay Monkman static uint32_t get_common_ffa_version(uint32_t secure_ffa_version)
899bb9fc8c0SJay Monkman {
900bb9fc8c0SJay Monkman 	if (secure_ffa_version <= nonsecure_ffa_version) {
901bb9fc8c0SJay Monkman 		return secure_ffa_version;
902bb9fc8c0SJay Monkman 	} else {
903bb9fc8c0SJay Monkman 		return nonsecure_ffa_version;
904bb9fc8c0SJay Monkman 	}
905bb01a673SMarc Bonnici }
906bb01a673SMarc Bonnici 
907bb01a673SMarc Bonnici /*******************************************************************************
908662af36dSJ-Alves  * This function handles all SMCs in the range reserved for FFA. Each call is
909bdd2596dSAchin Gupta  * either forwarded to the other security state or handled by the SPM dispatcher
910bdd2596dSAchin Gupta  ******************************************************************************/
91152696946SOlivier Deprez uint64_t spmd_smc_handler(uint32_t smc_fid,
91252696946SOlivier Deprez 			  uint64_t x1,
91352696946SOlivier Deprez 			  uint64_t x2,
91452696946SOlivier Deprez 			  uint64_t x3,
91552696946SOlivier Deprez 			  uint64_t x4,
91652696946SOlivier Deprez 			  void *cookie,
91752696946SOlivier Deprez 			  void *handle,
918bb9fc8c0SJay Monkman 			  uint64_t flags,
919bb9fc8c0SJay Monkman 			  uint32_t secure_ffa_version)
920bdd2596dSAchin Gupta {
92152696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
92293ff138bSOlivier Deprez 	bool secure_origin;
9236873088cSJ-Alves 	int ret;
9244388f28fSJ-Alves 	uint32_t input_version;
925bdd2596dSAchin Gupta 
926bdd2596dSAchin Gupta 	/* Determine which security state this SMC originated from */
92793ff138bSOlivier Deprez 	secure_origin = is_caller_secure(flags);
928bdd2596dSAchin Gupta 
9294ce3e99aSScott Branden 	VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64
9304ce3e99aSScott Branden 		" 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
931eac8077aSOlivier Deprez 		    plat_my_core_pos(), smc_fid, x1, x2, x3, x4,
932cdb49d47SOlivier Deprez 		    SMC_GET_GP(handle, CTX_GPREG_X5),
933bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X6),
934bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X7));
935bdd2596dSAchin Gupta 
9360b850e9eSRaghu Krishnamurthy 	/*
9370b850e9eSRaghu Krishnamurthy 	 * If there is an on-going info regs from EL3 SPMD LP, unconditionally
9380b850e9eSRaghu Krishnamurthy 	 * return, we don't expect any other FF-A ABIs to be called between
9390b850e9eSRaghu Krishnamurthy 	 * calls to FFA_PARTITION_INFO_GET_REGS.
9400b850e9eSRaghu Krishnamurthy 	 */
9410b850e9eSRaghu Krishnamurthy 	if (is_spmd_logical_sp_info_regs_req_in_progress(ctx)) {
9420b850e9eSRaghu Krishnamurthy 		assert(secure_origin);
9430b850e9eSRaghu Krishnamurthy 		spmd_spm_core_sync_exit(0ULL);
9440b850e9eSRaghu Krishnamurthy 	}
9450b850e9eSRaghu Krishnamurthy 
946bb9fc8c0SJay Monkman 	if ((!secure_origin) && (smc_fid != FFA_VERSION)) {
947bb9fc8c0SJay Monkman 		/*
948bb9fc8c0SJay Monkman 		 * Once the caller invokes any FF-A ABI other than FFA_VERSION,
949bb9fc8c0SJay Monkman 		 * the version negotiation phase is complete.
950bb9fc8c0SJay Monkman 		 */
951bb9fc8c0SJay Monkman 		nonsecure_version_negotiated = true;
952bb9fc8c0SJay Monkman 	}
953bb9fc8c0SJay Monkman 
954bdd2596dSAchin Gupta 	switch (smc_fid) {
955662af36dSJ-Alves 	case FFA_ERROR:
956bdd2596dSAchin Gupta 		/*
957bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
95852696946SOlivier Deprez 		 * this CPU. If so, then indicate that the SPM Core initialised
959bdd2596dSAchin Gupta 		 * unsuccessfully.
960bdd2596dSAchin Gupta 		 */
9619dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
962bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(x2);
9630f14d02fSMax Shvetsov 		}
964bdd2596dSAchin Gupta 
96566bdfd6eSRaghu Krishnamurthy 		/*
9668723eaf2SMadhukar Pappireddy 		 * Perform a synchronous exit:
9678723eaf2SMadhukar Pappireddy 		 * 1. If there was an SPMD logical partition direct request on-going,
96866bdfd6eSRaghu Krishnamurthy 		 * return back to the SPMD logical partition so the error can be
96966bdfd6eSRaghu Krishnamurthy 		 * consumed.
9708723eaf2SMadhukar Pappireddy 		 * 2. SPMC sent FFA_ERROR in response to a power management
9718723eaf2SMadhukar Pappireddy 		 * operation sent through direct request.
97266bdfd6eSRaghu Krishnamurthy 		 */
9738723eaf2SMadhukar Pappireddy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx) ||
9748723eaf2SMadhukar Pappireddy 		    ctx->psci_operation_ongoing) {
97566bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
97666bdfd6eSRaghu Krishnamurthy 			spmd_spm_core_sync_exit(0ULL);
97766bdfd6eSRaghu Krishnamurthy 		}
97866bdfd6eSRaghu Krishnamurthy 
97993ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
980bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
981bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
982bdd2596dSAchin Gupta 		break; /* not reached */
983bdd2596dSAchin Gupta 
984662af36dSJ-Alves 	case FFA_VERSION:
9854388f28fSJ-Alves 		input_version = (uint32_t)(0xFFFFFFFF & x1);
986bdd2596dSAchin Gupta 		/*
9874388f28fSJ-Alves 		 * If caller is secure and SPMC was initialized,
9884388f28fSJ-Alves 		 * return FFA_VERSION of SPMD.
9894388f28fSJ-Alves 		 * If caller is non secure and SPMC was initialized,
990bb9fc8c0SJay Monkman 		 * forward to the EL3 SPMC if enabled, otherwise send a
991bb9fc8c0SJay Monkman 		 * framework message to the SPMC at the lower EL to
992bb9fc8c0SJay Monkman 		 * negotiate a version that is compatible between the
993bb9fc8c0SJay Monkman 		 * normal world and the SPMC.
9944388f28fSJ-Alves 		 * Sanity check to "input_version".
995bb01a673SMarc Bonnici 		 * If the EL3 SPMC is enabled, ignore the SPMC state as
996bb01a673SMarc Bonnici 		 * this is not used.
997bdd2596dSAchin Gupta 		 */
9984388f28fSJ-Alves 		if ((input_version & FFA_VERSION_BIT31_MASK) ||
999bb01a673SMarc Bonnici 		    (!is_spmc_at_el3() && (ctx->state == SPMC_STATE_RESET))) {
10004388f28fSJ-Alves 			ret = FFA_ERROR_NOT_SUPPORTED;
10014388f28fSJ-Alves 		} else if (!secure_origin) {
1002bb9fc8c0SJay Monkman 			if (!nonsecure_version_negotiated) {
1003bb9fc8c0SJay Monkman 				/*
1004bb9fc8c0SJay Monkman 				 * Once an FF-A version has been negotiated
1005bb9fc8c0SJay Monkman 				 * between a caller and a callee, the version
1006bb9fc8c0SJay Monkman 				 * may not be changed for the lifetime of
1007bb9fc8c0SJay Monkman 				 * the calling component.
1008bb9fc8c0SJay Monkman 				 */
1009bb9fc8c0SJay Monkman 				nonsecure_ffa_version = input_version;
1010bb9fc8c0SJay Monkman 			}
1011bb9fc8c0SJay Monkman 
10129576fa93SMarc Bonnici 			if (is_spmc_at_el3()) {
10139576fa93SMarc Bonnici 				/*
10149576fa93SMarc Bonnici 				 * Forward the call directly to the EL3 SPMC, if
10159576fa93SMarc Bonnici 				 * enabled, as we don't need to wrap the call in
10169576fa93SMarc Bonnici 				 * a direct request.
10179576fa93SMarc Bonnici 				 */
1018bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version =
1019bb9fc8c0SJay Monkman 					MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR);
1020bb9fc8c0SJay Monkman 				return spmc_smc_handler(smc_fid, secure_origin,
10219576fa93SMarc Bonnici 							x1, x2, x3, x4, cookie,
10229576fa93SMarc Bonnici 							handle, flags);
10239576fa93SMarc Bonnici 			}
10249576fa93SMarc Bonnici 
10259944f557SDaniel Boulby 			gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
10269944f557SDaniel Boulby 			uint64_t rc;
10279944f557SDaniel Boulby 
10289944f557SDaniel Boulby 			if (spmc_attrs.major_version == 1 &&
10299944f557SDaniel Boulby 			    spmc_attrs.minor_version == 0) {
1030e46b2fd2SJ-Alves 				ret = MAKE_FFA_VERSION(spmc_attrs.major_version,
1031e46b2fd2SJ-Alves 						       spmc_attrs.minor_version);
1032bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version = (uint32_t)ret;
10339944f557SDaniel Boulby 				SMC_RET8(handle, (uint32_t)ret,
10349944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
10359944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
10369944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
10379944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
10389944f557SDaniel Boulby 					 FFA_PARAM_MBZ);
10399944f557SDaniel Boulby 				break;
10409944f557SDaniel Boulby 			}
10419944f557SDaniel Boulby 			/* Save non-secure system registers context */
10429944f557SDaniel Boulby #if SPMD_SPM_AT_SEL2
10439944f557SDaniel Boulby 			cm_el2_sysregs_context_save(NON_SECURE);
10442d960a11SMadhukar Pappireddy #else
10452d960a11SMadhukar Pappireddy 			cm_el1_sysregs_context_save(NON_SECURE);
10469944f557SDaniel Boulby #endif
10479944f557SDaniel Boulby 
10489944f557SDaniel Boulby 			/*
10499944f557SDaniel Boulby 			 * The incoming request has FFA_VERSION as X0 smc_fid
10509944f557SDaniel Boulby 			 * and requested version in x1. Prepare a direct request
10519944f557SDaniel Boulby 			 * from SPMD to SPMC with FFA_VERSION framework function
10529944f557SDaniel Boulby 			 * identifier in X2 and requested version in X3.
10539944f557SDaniel Boulby 			 */
10549944f557SDaniel Boulby 			spmd_build_spmc_message(gpregs,
10559944f557SDaniel Boulby 						SPMD_FWK_MSG_FFA_VERSION_REQ,
10569944f557SDaniel Boulby 						input_version);
10579944f557SDaniel Boulby 
105876d53ee1SOlivier Deprez 			/*
105976d53ee1SOlivier Deprez 			 * Ensure x8-x17 NS GP register values are untouched when returning
106076d53ee1SOlivier Deprez 			 * from the SPMC.
106176d53ee1SOlivier Deprez 			 */
106276d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X8, SMC_GET_GP(handle, CTX_GPREG_X8));
106376d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X9, SMC_GET_GP(handle, CTX_GPREG_X9));
106476d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X10, SMC_GET_GP(handle, CTX_GPREG_X10));
106576d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X11, SMC_GET_GP(handle, CTX_GPREG_X11));
106676d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X12, SMC_GET_GP(handle, CTX_GPREG_X12));
106776d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X13, SMC_GET_GP(handle, CTX_GPREG_X13));
106876d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X14, SMC_GET_GP(handle, CTX_GPREG_X14));
106976d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X15, SMC_GET_GP(handle, CTX_GPREG_X15));
107076d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X16, SMC_GET_GP(handle, CTX_GPREG_X16));
107176d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X17, SMC_GET_GP(handle, CTX_GPREG_X17));
107276d53ee1SOlivier Deprez 
10739944f557SDaniel Boulby 			rc = spmd_spm_core_sync_entry(ctx);
10749944f557SDaniel Boulby 
10759944f557SDaniel Boulby 			if ((rc != 0ULL) ||
10769944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X0) !=
10779944f557SDaniel Boulby 				FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
10789944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X2) !=
107959bd2ad8SMarc Bonnici 				(FFA_FWK_MSG_BIT |
10809944f557SDaniel Boulby 				 SPMD_FWK_MSG_FFA_VERSION_RESP))) {
10819944f557SDaniel Boulby 				ERROR("Failed to forward FFA_VERSION\n");
10829944f557SDaniel Boulby 				ret = FFA_ERROR_NOT_SUPPORTED;
10839944f557SDaniel Boulby 			} else {
10849944f557SDaniel Boulby 				ret = SMC_GET_GP(gpregs, CTX_GPREG_X3);
1085bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version = (uint32_t)ret;
10869944f557SDaniel Boulby 			}
10879944f557SDaniel Boulby 
10889944f557SDaniel Boulby 			/*
108976d53ee1SOlivier Deprez 			 * x0-x4 are updated by spmd_smc_forward below.
109076d53ee1SOlivier Deprez 			 * Zero out x5-x7 in the FFA_VERSION response.
109176d53ee1SOlivier Deprez 			 */
109276d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
109376d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
109476d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
109576d53ee1SOlivier Deprez 
109676d53ee1SOlivier Deprez 			/*
10979944f557SDaniel Boulby 			 * Return here after SPMC has handled FFA_VERSION.
10989944f557SDaniel Boulby 			 * The returned SPMC version is held in X3.
10999944f557SDaniel Boulby 			 * Forward this version in X0 to the non-secure caller.
11009944f557SDaniel Boulby 			 */
11019944f557SDaniel Boulby 			return spmd_smc_forward(ret, true, FFA_PARAM_MBZ,
11029944f557SDaniel Boulby 						FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1103bb01a673SMarc Bonnici 						FFA_PARAM_MBZ, cookie, gpregs,
1104bb9fc8c0SJay Monkman 						flags, spmc_nwd_ffa_version);
11054388f28fSJ-Alves 		} else {
1106e46b2fd2SJ-Alves 			ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR,
1107e46b2fd2SJ-Alves 					       FFA_VERSION_MINOR);
11084388f28fSJ-Alves 		}
11094388f28fSJ-Alves 
1110e46b2fd2SJ-Alves 		SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ,
1111e46b2fd2SJ-Alves 			 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1112e46b2fd2SJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
1113bdd2596dSAchin Gupta 		break; /* not reached */
1114bdd2596dSAchin Gupta 
1115662af36dSJ-Alves 	case FFA_FEATURES:
1116bdd2596dSAchin Gupta 		/*
1117bdd2596dSAchin Gupta 		 * This is an optional interface. Do the minimal checks and
111852696946SOlivier Deprez 		 * forward to SPM Core which will handle it if implemented.
1119bdd2596dSAchin Gupta 		 */
1120bdd2596dSAchin Gupta 
112152696946SOlivier Deprez 		/* Forward SMC from Normal world to the SPM Core */
112293ff138bSOlivier Deprez 		if (!secure_origin) {
112393ff138bSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
1124bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
1125bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
112652696946SOlivier Deprez 		}
112752696946SOlivier Deprez 
1128bdd2596dSAchin Gupta 		/*
1129bdd2596dSAchin Gupta 		 * Return success if call was from secure world i.e. all
1130662af36dSJ-Alves 		 * FFA functions are supported. This is essentially a
1131bdd2596dSAchin Gupta 		 * nop.
1132bdd2596dSAchin Gupta 		 */
1133662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4,
1134bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X5),
1135bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X6),
1136bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X7));
11370f14d02fSMax Shvetsov 
1138bdd2596dSAchin Gupta 		break; /* not reached */
1139bdd2596dSAchin Gupta 
1140662af36dSJ-Alves 	case FFA_ID_GET:
1141ac03ac5eSMax Shvetsov 		/*
1142662af36dSJ-Alves 		 * Returns the ID of the calling FFA component.
1143ac03ac5eSMax Shvetsov 		 */
1144ac03ac5eSMax Shvetsov 		if (!secure_origin) {
1145662af36dSJ-Alves 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
1146662af36dSJ-Alves 				 FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID,
1147662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1148662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1149662af36dSJ-Alves 				 FFA_PARAM_MBZ);
115052696946SOlivier Deprez 		}
115152696946SOlivier Deprez 
1152662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
1153662af36dSJ-Alves 			 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
1154662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1155662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1156662af36dSJ-Alves 			 FFA_PARAM_MBZ);
1157ac03ac5eSMax Shvetsov 
1158ac03ac5eSMax Shvetsov 		break; /* not reached */
1159ac03ac5eSMax Shvetsov 
1160cdb49d47SOlivier Deprez 	case FFA_SECONDARY_EP_REGISTER_SMC64:
1161cdb49d47SOlivier Deprez 		if (secure_origin) {
1162cdb49d47SOlivier Deprez 			ret = spmd_pm_secondary_ep_register(x1);
1163cdb49d47SOlivier Deprez 
1164cdb49d47SOlivier Deprez 			if (ret < 0) {
1165cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_ERROR_SMC64,
1166cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, ret,
1167cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1168cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1169cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
1170cdb49d47SOlivier Deprez 			} else {
1171cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_SUCCESS_SMC64,
1172cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ,
1173cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1174cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1175cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
1176cdb49d47SOlivier Deprez 			}
1177cdb49d47SOlivier Deprez 		}
1178cdb49d47SOlivier Deprez 
1179cdb49d47SOlivier Deprez 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1180cdb49d47SOlivier Deprez 		break; /* Not reached */
1181cdb49d47SOlivier Deprez 
118270c121a2SDaniel Boulby 	case FFA_SPM_ID_GET:
118370c121a2SDaniel Boulby 		if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) {
118470c121a2SDaniel Boulby 			return spmd_ffa_error_return(handle,
118570c121a2SDaniel Boulby 						     FFA_ERROR_NOT_SUPPORTED);
118670c121a2SDaniel Boulby 		}
118770c121a2SDaniel Boulby 		/*
118870c121a2SDaniel Boulby 		 * Returns the ID of the SPMC or SPMD depending on the FF-A
118970c121a2SDaniel Boulby 		 * instance where this function is invoked
119070c121a2SDaniel Boulby 		 */
119170c121a2SDaniel Boulby 		if (!secure_origin) {
119270c121a2SDaniel Boulby 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
119370c121a2SDaniel Boulby 				 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
119470c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
119570c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
119670c121a2SDaniel Boulby 				 FFA_PARAM_MBZ);
119770c121a2SDaniel Boulby 		}
119870c121a2SDaniel Boulby 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
119970c121a2SDaniel Boulby 			 FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID,
120070c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
120170c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
120270c121a2SDaniel Boulby 			 FFA_PARAM_MBZ);
120370c121a2SDaniel Boulby 
120470c121a2SDaniel Boulby 		break; /* not reached */
120570c121a2SDaniel Boulby 
1206bb9fc8c0SJay Monkman 	case FFA_MSG_SEND_DIRECT_REQ2_SMC64:
1207bb9fc8c0SJay Monkman 		if (get_common_ffa_version(secure_ffa_version) < MAKE_FFA_VERSION(U(1), U(2))) {
1208bb9fc8c0SJay Monkman 			/* Call not supported at this version */
1209bb9fc8c0SJay Monkman 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1210bb9fc8c0SJay Monkman 		}
1211bb9fc8c0SJay Monkman 		/* fallthrough */
1212f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_REQ_SMC32:
12135519f07cSShruti 	case FFA_MSG_SEND_DIRECT_REQ_SMC64:
121466bdfd6eSRaghu Krishnamurthy 		/*
121566bdfd6eSRaghu Krishnamurthy 		 * Regardless of secure_origin, SPMD logical partitions cannot
121666bdfd6eSRaghu Krishnamurthy 		 * handle direct messages. They can only initiate direct
121766bdfd6eSRaghu Krishnamurthy 		 * messages and consume direct responses or errors.
121866bdfd6eSRaghu Krishnamurthy 		 */
121966bdfd6eSRaghu Krishnamurthy 		if (is_spmd_lp_id(ffa_endpoint_source(x1)) ||
122066bdfd6eSRaghu Krishnamurthy 				  is_spmd_lp_id(ffa_endpoint_destination(x1))) {
122166bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
122266bdfd6eSRaghu Krishnamurthy 						     FFA_ERROR_INVALID_PARAMETER
122366bdfd6eSRaghu Krishnamurthy 						     );
122466bdfd6eSRaghu Krishnamurthy 		}
122566bdfd6eSRaghu Krishnamurthy 
122666bdfd6eSRaghu Krishnamurthy 		/*
122766bdfd6eSRaghu Krishnamurthy 		 * When there is an ongoing SPMD logical partition direct
122866bdfd6eSRaghu Krishnamurthy 		 * request, there cannot be another direct request. Return
122966bdfd6eSRaghu Krishnamurthy 		 * error in this case. Panic'ing is an option but that does
123066bdfd6eSRaghu Krishnamurthy 		 * not provide the opportunity for caller to abort based on
123166bdfd6eSRaghu Krishnamurthy 		 * error codes.
123266bdfd6eSRaghu Krishnamurthy 		 */
123366bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
123466bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
123566bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
123666bdfd6eSRaghu Krishnamurthy 						     FFA_ERROR_DENIED);
123766bdfd6eSRaghu Krishnamurthy 		}
123866bdfd6eSRaghu Krishnamurthy 
12395519f07cSShruti 		if (!secure_origin) {
12405519f07cSShruti 			/* Validate source endpoint is non-secure for non-secure caller. */
12415519f07cSShruti 			if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) {
12425519f07cSShruti 				return spmd_ffa_error_return(handle,
12435519f07cSShruti 						FFA_ERROR_INVALID_PARAMETER);
12445519f07cSShruti 			}
12455519f07cSShruti 		}
1246f0d743dbSOlivier Deprez 		if (secure_origin && spmd_is_spmc_message(x1)) {
1247cc6047b3SKathleen Capella 				return spmd_ffa_error_return(handle,
12486c378c2fSKathleen Capella 						FFA_ERROR_DENIED);
1249cc6047b3SKathleen Capella 		} else {
1250cc6047b3SKathleen Capella 			/* Forward direct message to the other world */
1251cc6047b3SKathleen Capella 			return spmd_smc_forward(smc_fid, secure_origin,
1252cc6047b3SKathleen Capella 						x1, x2, x3, x4, cookie,
1253bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
1254cc6047b3SKathleen Capella 		}
1255cc6047b3SKathleen Capella 		break; /* Not reached */
1256cc6047b3SKathleen Capella 
1257bb9fc8c0SJay Monkman 	case FFA_MSG_SEND_DIRECT_RESP2_SMC64:
1258bb9fc8c0SJay Monkman 		if (get_common_ffa_version(secure_ffa_version) < MAKE_FFA_VERSION(U(1), U(2))) {
1259bb9fc8c0SJay Monkman 			/* Call not supported at this version */
1260bb9fc8c0SJay Monkman 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1261bb9fc8c0SJay Monkman 		}
1262bb9fc8c0SJay Monkman 		/* fallthrough */
1263f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_RESP_SMC32:
126466bdfd6eSRaghu Krishnamurthy 	case FFA_MSG_SEND_DIRECT_RESP_SMC64:
126566bdfd6eSRaghu Krishnamurthy 		if (secure_origin && (spmd_is_spmc_message(x1) ||
126666bdfd6eSRaghu Krishnamurthy 		    is_spmd_logical_sp_dir_req_in_progress(ctx))) {
12678cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
1268f0d743dbSOlivier Deprez 		} else {
1269f0d743dbSOlivier Deprez 			/* Forward direct message to the other world */
1270f0d743dbSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
1271bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
1272bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
1273f0d743dbSOlivier Deprez 		}
1274f0d743dbSOlivier Deprez 		break; /* Not reached */
1275662af36dSJ-Alves 	case FFA_RX_RELEASE:
1276662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC32:
1277662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC64:
1278662af36dSJ-Alves 	case FFA_RXTX_UNMAP:
1279545b8eb3SRuari Phipps 	case FFA_PARTITION_INFO_GET:
1280fc3f4800SJ-Alves #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1281fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_CREATE:
1282fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_DESTROY:
1283fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BIND:
1284fc3f4800SJ-Alves 	case FFA_NOTIFICATION_UNBIND:
1285fc3f4800SJ-Alves 	case FFA_NOTIFICATION_SET:
1286fc3f4800SJ-Alves 	case FFA_NOTIFICATION_GET:
1287fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET:
1288fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET_SMC64:
1289c2eba07cSFederico Recanati 	case FFA_MSG_SEND2:
1290d555233fSFederico Recanati 	case FFA_RX_ACQUIRE:
129108f9ba5bSJ-Alves 	case FFA_NS_RES_INFO_GET_SMC64:
1292fc3f4800SJ-Alves #endif
1293662af36dSJ-Alves 	case FFA_MSG_RUN:
1294c2eba07cSFederico Recanati 		/*
1295c2eba07cSFederico Recanati 		 * Above calls should be invoked only by the Normal world and
1296c2eba07cSFederico Recanati 		 * must not be forwarded from Secure world to Normal world.
1297c2eba07cSFederico Recanati 		 */
129893ff138bSOlivier Deprez 		if (secure_origin) {
1299662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
1300662af36dSJ-Alves 						     FFA_ERROR_NOT_SUPPORTED);
1301bdd2596dSAchin Gupta 		}
1302bdd2596dSAchin Gupta 
1303e138400dSBoyan Karatotev 		/* Forward the call to the other world */
1304e138400dSBoyan Karatotev 		/* fallthrough */
1305662af36dSJ-Alves 	case FFA_MSG_SEND:
1306662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC32:
1307662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC64:
1308662af36dSJ-Alves 	case FFA_MEM_LEND_SMC32:
1309662af36dSJ-Alves 	case FFA_MEM_LEND_SMC64:
1310662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC32:
1311662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC64:
1312662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC32:
1313662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC64:
1314662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_RESP:
1315662af36dSJ-Alves 	case FFA_MEM_RELINQUISH:
1316662af36dSJ-Alves 	case FFA_MEM_RECLAIM:
1317642db984SMarc Bonnici 	case FFA_MEM_FRAG_TX:
1318642db984SMarc Bonnici 	case FFA_MEM_FRAG_RX:
1319662af36dSJ-Alves 	case FFA_SUCCESS_SMC32:
1320662af36dSJ-Alves 	case FFA_SUCCESS_SMC64:
1321bdd2596dSAchin Gupta 		/*
132266bdfd6eSRaghu Krishnamurthy 		 * If there is an ongoing direct request from an SPMD logical
132366bdfd6eSRaghu Krishnamurthy 		 * partition, return an error.
1324bdd2596dSAchin Gupta 		 */
132566bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
132666bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
132766bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
132866bdfd6eSRaghu Krishnamurthy 					FFA_ERROR_DENIED);
132966bdfd6eSRaghu Krishnamurthy 		}
1330bdd2596dSAchin Gupta 
133193ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
1332bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
1333bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1334bdd2596dSAchin Gupta 		break; /* not reached */
1335bdd2596dSAchin Gupta 
1336662af36dSJ-Alves 	case FFA_MSG_WAIT:
1337bdd2596dSAchin Gupta 		/*
1338bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
1339bdd2596dSAchin Gupta 		 * this CPU from the Secure world. If so, then indicate that the
134052696946SOlivier Deprez 		 * SPM Core initialised successfully.
1341bdd2596dSAchin Gupta 		 */
13429dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
13438cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
1344bdd2596dSAchin Gupta 		}
1345bdd2596dSAchin Gupta 
1346e138400dSBoyan Karatotev 		/* Forward the call to the other world */
1347e138400dSBoyan Karatotev 		/* fallthrough */
1348386dc365SOlivier Deprez 	case FFA_INTERRUPT:
1349662af36dSJ-Alves 	case FFA_MSG_YIELD:
1350bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Secure world */
135193ff138bSOlivier Deprez 		if (!secure_origin) {
1352662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
1353662af36dSJ-Alves 						      FFA_ERROR_NOT_SUPPORTED);
1354bdd2596dSAchin Gupta 		}
1355bdd2596dSAchin Gupta 
135666bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
135766bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
135866bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
135966bdfd6eSRaghu Krishnamurthy 					FFA_ERROR_DENIED);
136066bdfd6eSRaghu Krishnamurthy 		}
136166bdfd6eSRaghu Krishnamurthy 
136293ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
1363bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
1364bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1365bdd2596dSAchin Gupta 		break; /* not reached */
1366bdd2596dSAchin Gupta 
13678cb99c3fSOlivier Deprez 	case FFA_NORMAL_WORLD_RESUME:
13688cb99c3fSOlivier Deprez 		if (secure_origin && ctx->secure_interrupt_ongoing) {
13698cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
13708cb99c3fSOlivier Deprez 		} else {
13718cb99c3fSOlivier Deprez 			return spmd_ffa_error_return(handle, FFA_ERROR_DENIED);
13728cb99c3fSOlivier Deprez 		}
13738cb99c3fSOlivier Deprez 		break; /* Not reached */
1374eaaf517cSRaghu Krishnamurthy #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1375eaaf517cSRaghu Krishnamurthy 	case FFA_PARTITION_INFO_GET_REGS_SMC64:
1376eaaf517cSRaghu Krishnamurthy 		if (secure_origin) {
137795f7f6d8SRaghu Krishnamurthy 			return spmd_el3_populate_logical_partition_info(handle, x1,
137895f7f6d8SRaghu Krishnamurthy 								   x2, x3);
1379eaaf517cSRaghu Krishnamurthy 		}
13808cb99c3fSOlivier Deprez 
1381eaaf517cSRaghu Krishnamurthy 		/* Call only supported with SMCCC 1.2+ */
1382eaaf517cSRaghu Krishnamurthy 		if (MAKE_SMCCC_VERSION(SMCCC_MAJOR_VERSION, SMCCC_MINOR_VERSION) < 0x10002) {
1383eaaf517cSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1384eaaf517cSRaghu Krishnamurthy 		}
1385eaaf517cSRaghu Krishnamurthy 
1386eaaf517cSRaghu Krishnamurthy 		return spmd_smc_forward(smc_fid, secure_origin,
1387eaaf517cSRaghu Krishnamurthy 					x1, x2, x3, x4, cookie,
1388bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1389eaaf517cSRaghu Krishnamurthy 		break; /* Not reached */
1390eaaf517cSRaghu Krishnamurthy #endif
1391638a6f8eSShruti Gupta 	case FFA_CONSOLE_LOG_SMC32:
1392638a6f8eSShruti Gupta 	case FFA_CONSOLE_LOG_SMC64:
1393638a6f8eSShruti Gupta 		/* This interface must not be forwarded to other worlds. */
1394638a6f8eSShruti Gupta 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1395638a6f8eSShruti Gupta 		break; /* not reached */
1396638a6f8eSShruti Gupta 
13976671b3d8SMadhukar Pappireddy 	case FFA_EL3_INTR_HANDLE:
13986671b3d8SMadhukar Pappireddy 		if (secure_origin) {
13996671b3d8SMadhukar Pappireddy 			return spmd_handle_group0_intr_swd(handle);
14006671b3d8SMadhukar Pappireddy 		} else {
14016c91fc44SMadhukar Pappireddy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
14026671b3d8SMadhukar Pappireddy 		}
1403b3dcd505SMadhukar Pappireddy 	case FFA_ABORT_SMC32:
1404b3dcd505SMadhukar Pappireddy 	case FFA_ABORT_SMC64:
1405b3dcd505SMadhukar Pappireddy 		/* This interface must be invoked only by the Secure world */
1406b3dcd505SMadhukar Pappireddy 		if (!secure_origin) {
1407b3dcd505SMadhukar Pappireddy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1408b3dcd505SMadhukar Pappireddy 		}
1409b3dcd505SMadhukar Pappireddy 
1410b3dcd505SMadhukar Pappireddy 		ERROR("SPMC encountered a fatal error. Aborting now\n");
1411b3dcd505SMadhukar Pappireddy 		panic();
1412b3dcd505SMadhukar Pappireddy 
1413b3dcd505SMadhukar Pappireddy 		/* Not reached. */
1414b3dcd505SMadhukar Pappireddy 		SMC_RET0(handle);
1415bdd2596dSAchin Gupta 	default:
1416bdd2596dSAchin Gupta 		WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
1417662af36dSJ-Alves 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1418bdd2596dSAchin Gupta 	}
1419bdd2596dSAchin Gupta }
1420