blob: 4ea074fd6efc3abe13d901a74a742feb20f8f224 [file] [log] [blame]
Zelalem Aweke13dc8f12021-07-09 14:20:03 -05001/*
Jayanth Dodderi Chidanandd62c6812023-03-07 10:43:19 +00002 * Copyright (c) 2021-2023, Arm Limited and Contributors. All rights reserved.
Zelalem Aweke13dc8f12021-07-09 14:20:03 -05003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8#include <errno.h>
Manish Pandey9174a752021-11-09 20:49:56 +00009#include <inttypes.h>
10#include <stdint.h>
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050011#include <string.h>
12
13#include <arch_helpers.h>
14#include <arch_features.h>
15#include <bl31/bl31.h>
16#include <common/debug.h>
17#include <common/runtime_svc.h>
18#include <context.h>
19#include <lib/el3_runtime/context_mgmt.h>
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010020#include <lib/el3_runtime/cpu_data.h>
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050021#include <lib/el3_runtime/pubsub.h>
Boyan Karatotev05504ba2023-02-15 13:21:50 +000022#include <lib/extensions/pmuv3.h>
23#include <lib/extensions/sys_reg_trace.h>
johpow019d134022021-06-16 17:57:28 -050024#include <lib/gpt_rme/gpt_rme.h>
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050025
26#include <lib/spinlock.h>
27#include <lib/utils.h>
28#include <lib/xlat_tables/xlat_tables_v2.h>
29#include <plat/common/common_def.h>
30#include <plat/common/platform.h>
31#include <platform_def.h>
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050032#include <services/rmmd_svc.h>
33#include <smccc_helpers.h>
Arunachalam Ganapathy337700a2023-05-18 10:57:29 +010034#include <lib/extensions/sme.h>
Subhasish Ghoshc25225a2021-12-09 15:41:37 +000035#include <lib/extensions/sve.h>
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050036#include "rmmd_initial_context.h"
37#include "rmmd_private.h"
38
39/*******************************************************************************
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +000040 * RMM boot failure flag
41 ******************************************************************************/
42static bool rmm_boot_failed;
43
44/*******************************************************************************
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050045 * RMM context information.
46 ******************************************************************************/
47rmmd_rmm_context_t rmm_context[PLATFORM_CORE_COUNT];
48
49/*******************************************************************************
50 * RMM entry point information. Discovered on the primary core and reused
51 * on secondary cores.
52 ******************************************************************************/
53static entry_point_info_t *rmm_ep_info;
54
55/*******************************************************************************
56 * Static function declaration.
57 ******************************************************************************/
58static int32_t rmm_init(void);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050059
60/*******************************************************************************
61 * This function takes an RMM context pointer and performs a synchronous entry
62 * into it.
63 ******************************************************************************/
64uint64_t rmmd_rmm_sync_entry(rmmd_rmm_context_t *rmm_ctx)
65{
66 uint64_t rc;
67
68 assert(rmm_ctx != NULL);
69
70 cm_set_context(&(rmm_ctx->cpu_ctx), REALM);
71
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050072 /* Restore the realm context assigned above */
73 cm_el1_sysregs_context_restore(REALM);
74 cm_el2_sysregs_context_restore(REALM);
75 cm_set_next_eret_context(REALM);
76
77 /* Enter RMM */
78 rc = rmmd_rmm_enter(&rmm_ctx->c_rt_ctx);
79
Zelalem Awekef92c0cb2022-01-31 16:59:42 -060080 /*
81 * Save realm context. EL1 and EL2 Non-secure
82 * contexts will be restored before exiting to
83 * Non-secure world, therefore there is no need
84 * to clear EL1 and EL2 context registers.
85 */
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050086 cm_el1_sysregs_context_save(REALM);
87 cm_el2_sysregs_context_save(REALM);
88
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050089 return rc;
90}
91
92/*******************************************************************************
93 * This function returns to the place where rmmd_rmm_sync_entry() was
94 * called originally.
95 ******************************************************************************/
96__dead2 void rmmd_rmm_sync_exit(uint64_t rc)
97{
98 rmmd_rmm_context_t *ctx = &rmm_context[plat_my_core_pos()];
99
100 /* Get context of the RMM in use by this CPU. */
101 assert(cm_get_context(REALM) == &(ctx->cpu_ctx));
102
103 /*
104 * The RMMD must have initiated the original request through a
105 * synchronous entry into RMM. Jump back to the original C runtime
106 * context with the value of rc in x0;
107 */
108 rmmd_rmm_exit(ctx->c_rt_ctx, rc);
109
110 panic();
111}
112
113static void rmm_el2_context_init(el2_sysregs_t *regs)
114{
115 regs->ctx_regs[CTX_SPSR_EL2 >> 3] = REALM_SPSR_EL2;
116 regs->ctx_regs[CTX_SCTLR_EL2 >> 3] = SCTLR_EL2_RES1;
117}
118
119/*******************************************************************************
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000120 * Enable architecture extensions on first entry to Realm world.
121 ******************************************************************************/
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100122
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000123static void manage_extensions_realm(cpu_context_t *ctx)
124{
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100125 pmuv3_enable(ctx);
126
127 /*
Arunachalam Ganapathya87a4092023-11-01 19:18:41 +0000128 * Enable access to TPIDR2_EL0 if SME/SME2 is enabled for Non Secure world.
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100129 */
130 if (is_feat_sme_supported()) {
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100131 sme_enable(ctx);
132 }
133}
134
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100135static void manage_extensions_realm_per_world(void)
136{
Jayanth Dodderi Chidanand56aa3822023-12-11 11:22:02 +0000137 cm_el3_arch_init_per_world(&per_world_context[CPU_CONTEXT_REALM]);
138
Jayanth Dodderi Chidanandd62c6812023-03-07 10:43:19 +0000139 if (is_feat_sve_supported()) {
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000140 /*
141 * Enable SVE and FPU in realm context when it is enabled for NS.
142 * Realm manager must ensure that the SVE and FPU register
143 * contexts are properly managed.
144 */
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100145 sve_enable_per_world(&per_world_context[CPU_CONTEXT_REALM]);
Jayanth Dodderi Chidanandd62c6812023-03-07 10:43:19 +0000146 }
Boyan Karatotev05504ba2023-02-15 13:21:50 +0000147
Boyan Karatotev919d3c82023-02-13 16:32:47 +0000148 /* NS can access this but Realm shouldn't */
149 if (is_feat_sys_reg_trace_supported()) {
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100150 sys_reg_trace_disable_per_world(&per_world_context[CPU_CONTEXT_REALM]);
Boyan Karatotev919d3c82023-02-13 16:32:47 +0000151 }
152
Arunachalam Ganapathya87a4092023-11-01 19:18:41 +0000153 /*
154 * If SME/SME2 is supported and enabled for NS world, then disable trapping
155 * of SME instructions for Realm world. RMM will save/restore required
156 * registers that are shared with SVE/FPU so that Realm can use FPU or SVE.
157 */
158 if (is_feat_sme_supported()) {
159 sme_enable_per_world(&per_world_context[CPU_CONTEXT_REALM]);
160 }
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000161}
162
163/*******************************************************************************
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500164 * Jump to the RMM for the first time.
165 ******************************************************************************/
166static int32_t rmm_init(void)
167{
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000168 long rc;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500169 rmmd_rmm_context_t *ctx = &rmm_context[plat_my_core_pos()];
170
171 INFO("RMM init start.\n");
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500172
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000173 /* Enable architecture extensions */
174 manage_extensions_realm(&ctx->cpu_ctx);
175
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100176 manage_extensions_realm_per_world();
177
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500178 /* Initialize RMM EL2 context. */
179 rmm_el2_context_init(&ctx->cpu_ctx.el2_sysregs_ctx);
180
181 rc = rmmd_rmm_sync_entry(ctx);
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000182 if (rc != E_RMM_BOOT_SUCCESS) {
183 ERROR("RMM init failed: %ld\n", rc);
184 /* Mark the boot as failed for all the CPUs */
185 rmm_boot_failed = true;
186 return 0;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500187 }
188
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500189 INFO("RMM init end.\n");
190
191 return 1;
192}
193
194/*******************************************************************************
195 * Load and read RMM manifest, setup RMM.
196 ******************************************************************************/
197int rmmd_setup(void)
198{
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100199 size_t shared_buf_size __unused;
200 uintptr_t shared_buf_base;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500201 uint32_t ep_attr;
202 unsigned int linear_id = plat_my_core_pos();
203 rmmd_rmm_context_t *rmm_ctx = &rmm_context[linear_id];
AlexeiFedorov8e754f92022-12-14 17:28:11 +0000204 struct rmm_manifest *manifest;
Javier Almansa Sobrino4165e842022-04-25 17:18:15 +0100205 int rc;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500206
207 /* Make sure RME is supported. */
208 assert(get_armv9_2_feat_rme_support() != 0U);
209
210 rmm_ep_info = bl31_plat_get_next_image_ep_info(REALM);
211 if (rmm_ep_info == NULL) {
212 WARN("No RMM image provided by BL2 boot loader, Booting "
213 "device without RMM initialization. SMCs destined for "
214 "RMM will return SMC_UNK\n");
215 return -ENOENT;
216 }
217
218 /* Under no circumstances will this parameter be 0 */
219 assert(rmm_ep_info->pc == RMM_BASE);
220
221 /* Initialise an entrypoint to set up the CPU context */
222 ep_attr = EP_REALM;
223 if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0U) {
224 ep_attr |= EP_EE_BIG;
225 }
226
227 SET_PARAM_HEAD(rmm_ep_info, PARAM_EP, VERSION_1, ep_attr);
228 rmm_ep_info->spsr = SPSR_64(MODE_EL2,
229 MODE_SP_ELX,
230 DISABLE_ALL_EXCEPTIONS);
231
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000232 shared_buf_size =
233 plat_rmmd_get_el3_rmm_shared_mem(&shared_buf_base);
234
235 assert((shared_buf_size == SZ_4K) &&
236 ((void *)shared_buf_base != NULL));
237
Javier Almansa Sobrino4165e842022-04-25 17:18:15 +0100238 /* Load the boot manifest at the beginning of the shared area */
AlexeiFedorov8e754f92022-12-14 17:28:11 +0000239 manifest = (struct rmm_manifest *)shared_buf_base;
Javier Almansa Sobrino4165e842022-04-25 17:18:15 +0100240 rc = plat_rmmd_load_manifest(manifest);
241 if (rc != 0) {
242 ERROR("Error loading RMM Boot Manifest (%i)\n", rc);
243 return rc;
244 }
245 flush_dcache_range((uintptr_t)shared_buf_base, shared_buf_size);
246
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000247 /*
248 * Prepare coldboot arguments for RMM:
249 * arg0: This CPUID (primary processor).
250 * arg1: Version for this Boot Interface.
251 * arg2: PLATFORM_CORE_COUNT.
252 * arg3: Base address for the EL3 <-> RMM shared area. The boot
253 * manifest will be stored at the beginning of this area.
254 */
255 rmm_ep_info->args.arg0 = linear_id;
256 rmm_ep_info->args.arg1 = RMM_EL3_INTERFACE_VERSION;
257 rmm_ep_info->args.arg2 = PLATFORM_CORE_COUNT;
258 rmm_ep_info->args.arg3 = shared_buf_base;
259
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500260 /* Initialise RMM context with this entry point information */
261 cm_setup_context(&rmm_ctx->cpu_ctx, rmm_ep_info);
262
263 INFO("RMM setup done.\n");
264
265 /* Register init function for deferred init. */
266 bl31_register_rmm_init(&rmm_init);
267
268 return 0;
269}
270
271/*******************************************************************************
272 * Forward SMC to the other security state
273 ******************************************************************************/
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000274static uint64_t rmmd_smc_forward(uint32_t src_sec_state,
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100275 uint32_t dst_sec_state, uint64_t x0,
276 uint64_t x1, uint64_t x2, uint64_t x3,
277 uint64_t x4, void *handle)
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500278{
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100279 cpu_context_t *ctx = cm_get_context(dst_sec_state);
280
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500281 /* Save incoming security state */
282 cm_el1_sysregs_context_save(src_sec_state);
283 cm_el2_sysregs_context_save(src_sec_state);
284
285 /* Restore outgoing security state */
286 cm_el1_sysregs_context_restore(dst_sec_state);
287 cm_el2_sysregs_context_restore(dst_sec_state);
288 cm_set_next_eret_context(dst_sec_state);
289
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000290 /*
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100291 * As per SMCCCv1.2, we need to preserve x4 to x7 unless
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000292 * being used as return args. Hence we differentiate the
293 * onward and backward path. Support upto 8 args in the
294 * onward path and 4 args in return path.
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100295 * Register x4 will be preserved by RMM in case it is not
296 * used in return path.
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000297 */
298 if (src_sec_state == NON_SECURE) {
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100299 SMC_RET8(ctx, x0, x1, x2, x3, x4,
300 SMC_GET_GP(handle, CTX_GPREG_X5),
301 SMC_GET_GP(handle, CTX_GPREG_X6),
302 SMC_GET_GP(handle, CTX_GPREG_X7));
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000303 }
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100304
305 SMC_RET5(ctx, x0, x1, x2, x3, x4);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500306}
307
308/*******************************************************************************
309 * This function handles all SMCs in the range reserved for RMI. Each call is
310 * either forwarded to the other security state or handled by the RMM dispatcher
311 ******************************************************************************/
312uint64_t rmmd_rmi_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2,
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100313 uint64_t x3, uint64_t x4, void *cookie,
314 void *handle, uint64_t flags)
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500315{
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500316 uint32_t src_sec_state;
317
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000318 /* If RMM failed to boot, treat any RMI SMC as unknown */
319 if (rmm_boot_failed) {
320 WARN("RMMD: Failed to boot up RMM. Ignoring RMI call\n");
321 SMC_RET1(handle, SMC_UNK);
322 }
323
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500324 /* Determine which security state this SMC originated from */
325 src_sec_state = caller_sec_state(flags);
326
327 /* RMI must not be invoked by the Secure world */
328 if (src_sec_state == SMC_FROM_SECURE) {
Soby Mathew68ea9542022-03-22 13:58:52 +0000329 WARN("RMMD: RMI invoked by secure world.\n");
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500330 SMC_RET1(handle, SMC_UNK);
331 }
332
333 /*
334 * Forward an RMI call from the Normal world to the Realm world as it
335 * is.
336 */
337 if (src_sec_state == SMC_FROM_NON_SECURE) {
Arunachalam Ganapathy6e84add2023-08-24 15:31:01 +0100338 /*
339 * If SVE hint bit is set in the flags then update the SMC
340 * function id and pass it on to the lower EL.
341 */
342 if (is_sve_hint_set(flags)) {
343 smc_fid |= (FUNCID_SVE_HINT_MASK <<
344 FUNCID_SVE_HINT_SHIFT);
345 }
Soby Mathew68ea9542022-03-22 13:58:52 +0000346 VERBOSE("RMMD: RMI call from non-secure world.\n");
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000347 return rmmd_smc_forward(NON_SECURE, REALM, smc_fid,
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500348 x1, x2, x3, x4, handle);
349 }
350
Soby Mathew68ea9542022-03-22 13:58:52 +0000351 if (src_sec_state != SMC_FROM_REALM) {
352 SMC_RET1(handle, SMC_UNK);
353 }
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500354
355 switch (smc_fid) {
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100356 case RMM_RMI_REQ_COMPLETE: {
357 uint64_t x5 = SMC_GET_GP(handle, CTX_GPREG_X5);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500358
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100359 return rmmd_smc_forward(REALM, NON_SECURE, x1,
360 x2, x3, x4, x5, handle);
361 }
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500362 default:
Soby Mathew68ea9542022-03-22 13:58:52 +0000363 WARN("RMMD: Unsupported RMM call 0x%08x\n", smc_fid);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500364 SMC_RET1(handle, SMC_UNK);
365 }
366}
367
368/*******************************************************************************
369 * This cpu has been turned on. Enter RMM to initialise R-EL2. Entry into RMM
370 * is done after initialising minimal architectural state that guarantees safe
371 * execution.
372 ******************************************************************************/
373static void *rmmd_cpu_on_finish_handler(const void *arg)
374{
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000375 long rc;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500376 uint32_t linear_id = plat_my_core_pos();
377 rmmd_rmm_context_t *ctx = &rmm_context[linear_id];
378
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000379 if (rmm_boot_failed) {
380 /* RMM Boot failed on a previous CPU. Abort. */
381 ERROR("RMM Failed to initialize. Ignoring for CPU%d\n",
382 linear_id);
383 return NULL;
384 }
385
386 /*
387 * Prepare warmboot arguments for RMM:
388 * arg0: This CPUID.
389 * arg1 to arg3: Not used.
390 */
391 rmm_ep_info->args.arg0 = linear_id;
392 rmm_ep_info->args.arg1 = 0ULL;
393 rmm_ep_info->args.arg2 = 0ULL;
394 rmm_ep_info->args.arg3 = 0ULL;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500395
396 /* Initialise RMM context with this entry point information */
397 cm_setup_context(&ctx->cpu_ctx, rmm_ep_info);
398
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000399 /* Enable architecture extensions */
400 manage_extensions_realm(&ctx->cpu_ctx);
401
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500402 /* Initialize RMM EL2 context. */
403 rmm_el2_context_init(&ctx->cpu_ctx.el2_sysregs_ctx);
404
405 rc = rmmd_rmm_sync_entry(ctx);
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000406
407 if (rc != E_RMM_BOOT_SUCCESS) {
408 ERROR("RMM init failed on CPU%d: %ld\n", linear_id, rc);
409 /* Mark the boot as failed for any other booting CPU */
410 rmm_boot_failed = true;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500411 }
412
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500413 return NULL;
414}
415
416/* Subscribe to PSCI CPU on to initialize RMM on secondary */
417SUBSCRIBE_TO_EVENT(psci_cpu_on_finish, rmmd_cpu_on_finish_handler);
418
Soby Mathew68ea9542022-03-22 13:58:52 +0000419/* Convert GPT lib error to RMMD GTS error */
420static int gpt_to_gts_error(int error, uint32_t smc_fid, uint64_t address)
421{
422 int ret;
423
424 if (error == 0) {
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100425 return E_RMM_OK;
Soby Mathew68ea9542022-03-22 13:58:52 +0000426 }
427
428 if (error == -EINVAL) {
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100429 ret = E_RMM_BAD_ADDR;
Soby Mathew68ea9542022-03-22 13:58:52 +0000430 } else {
431 /* This is the only other error code we expect */
432 assert(error == -EPERM);
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100433 ret = E_RMM_BAD_PAS;
Soby Mathew68ea9542022-03-22 13:58:52 +0000434 }
435
436 ERROR("RMMD: PAS Transition failed. GPT ret = %d, PA: 0x%"PRIx64 ", FID = 0x%x\n",
437 error, address, smc_fid);
438 return ret;
439}
440
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500441/*******************************************************************************
Soby Mathew68ea9542022-03-22 13:58:52 +0000442 * This function handles RMM-EL3 interface SMCs
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500443 ******************************************************************************/
Soby Mathew68ea9542022-03-22 13:58:52 +0000444uint64_t rmmd_rmm_el3_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2,
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500445 uint64_t x3, uint64_t x4, void *cookie,
446 void *handle, uint64_t flags)
447{
448 uint32_t src_sec_state;
Robert Wakim48e6b572021-10-21 15:39:56 +0100449 int ret;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500450
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000451 /* If RMM failed to boot, treat any RMM-EL3 interface SMC as unknown */
452 if (rmm_boot_failed) {
453 WARN("RMMD: Failed to boot up RMM. Ignoring RMM-EL3 call\n");
454 SMC_RET1(handle, SMC_UNK);
455 }
456
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500457 /* Determine which security state this SMC originated from */
458 src_sec_state = caller_sec_state(flags);
459
460 if (src_sec_state != SMC_FROM_REALM) {
Soby Mathew68ea9542022-03-22 13:58:52 +0000461 WARN("RMMD: RMM-EL3 call originated from secure or normal world\n");
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500462 SMC_RET1(handle, SMC_UNK);
463 }
464
465 switch (smc_fid) {
Javier Almansa Sobrinof809b162022-07-04 17:06:36 +0100466 case RMM_GTSI_DELEGATE:
Robert Wakim48e6b572021-10-21 15:39:56 +0100467 ret = gpt_delegate_pas(x1, PAGE_SIZE_4KB, SMC_FROM_REALM);
Soby Mathew68ea9542022-03-22 13:58:52 +0000468 SMC_RET1(handle, gpt_to_gts_error(ret, smc_fid, x1));
Javier Almansa Sobrinof809b162022-07-04 17:06:36 +0100469 case RMM_GTSI_UNDELEGATE:
Robert Wakim48e6b572021-10-21 15:39:56 +0100470 ret = gpt_undelegate_pas(x1, PAGE_SIZE_4KB, SMC_FROM_REALM);
Soby Mathew68ea9542022-03-22 13:58:52 +0000471 SMC_RET1(handle, gpt_to_gts_error(ret, smc_fid, x1));
Javier Almansa Sobrinof809b162022-07-04 17:06:36 +0100472 case RMM_ATTEST_GET_PLAT_TOKEN:
Soby Mathew294e1cf2022-03-22 16:19:39 +0000473 ret = rmmd_attest_get_platform_token(x1, &x2, x3);
474 SMC_RET2(handle, ret, x2);
Javier Almansa Sobrinof809b162022-07-04 17:06:36 +0100475 case RMM_ATTEST_GET_REALM_KEY:
Soby Mathewf05d93a2022-03-22 16:21:19 +0000476 ret = rmmd_attest_get_signing_key(x1, &x2, x3);
477 SMC_RET2(handle, ret, x2);
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000478
479 case RMM_BOOT_COMPLETE:
480 VERBOSE("RMMD: running rmmd_rmm_sync_exit\n");
481 rmmd_rmm_sync_exit(x1);
482
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500483 default:
Soby Mathew68ea9542022-03-22 13:58:52 +0000484 WARN("RMMD: Unsupported RMM-EL3 call 0x%08x\n", smc_fid);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500485 SMC_RET1(handle, SMC_UNK);
486 }
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500487}