blob: b192428ce50f3402e71615fb51df48fe8553e9ed [file] [log] [blame]
Zelalem Aweke13dc8f12021-07-09 14:20:03 -05001/*
Jayanth Dodderi Chidanandc05031c2023-09-12 12:07:56 +01002 * Copyright (c) 2021-2024, 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 */
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050073 cm_el2_sysregs_context_restore(REALM);
74 cm_set_next_eret_context(REALM);
75
76 /* Enter RMM */
77 rc = rmmd_rmm_enter(&rmm_ctx->c_rt_ctx);
78
Zelalem Awekef92c0cb2022-01-31 16:59:42 -060079 /*
Jayanth Dodderi Chidanandc05031c2023-09-12 12:07:56 +010080 * Save realm context. EL2 Non-secure context will be restored
81 * before exiting Non-secure world, therefore there is no need
82 * to clear EL2 context registers.
Zelalem Awekef92c0cb2022-01-31 16:59:42 -060083 */
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050084 cm_el2_sysregs_context_save(REALM);
85
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050086 return rc;
87}
88
89/*******************************************************************************
90 * This function returns to the place where rmmd_rmm_sync_entry() was
91 * called originally.
92 ******************************************************************************/
93__dead2 void rmmd_rmm_sync_exit(uint64_t rc)
94{
95 rmmd_rmm_context_t *ctx = &rmm_context[plat_my_core_pos()];
96
97 /* Get context of the RMM in use by this CPU. */
98 assert(cm_get_context(REALM) == &(ctx->cpu_ctx));
99
100 /*
101 * The RMMD must have initiated the original request through a
102 * synchronous entry into RMM. Jump back to the original C runtime
103 * context with the value of rc in x0;
104 */
105 rmmd_rmm_exit(ctx->c_rt_ctx, rc);
106
107 panic();
108}
109
110static void rmm_el2_context_init(el2_sysregs_t *regs)
111{
Jayanth Dodderi Chidanandfbbee6b2024-01-24 20:05:07 +0000112 write_el2_ctx_common(regs, spsr_el2, REALM_SPSR_EL2);
113 write_el2_ctx_common(regs, sctlr_el2, SCTLR_EL2_RES1);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500114}
115
116/*******************************************************************************
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000117 * Enable architecture extensions on first entry to Realm world.
118 ******************************************************************************/
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100119
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000120static void manage_extensions_realm(cpu_context_t *ctx)
121{
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100122 pmuv3_enable(ctx);
123
124 /*
Arunachalam Ganapathya87a4092023-11-01 19:18:41 +0000125 * Enable access to TPIDR2_EL0 if SME/SME2 is enabled for Non Secure world.
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100126 */
127 if (is_feat_sme_supported()) {
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100128 sme_enable(ctx);
129 }
130}
131
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100132static void manage_extensions_realm_per_world(void)
133{
Jayanth Dodderi Chidanand56aa3822023-12-11 11:22:02 +0000134 cm_el3_arch_init_per_world(&per_world_context[CPU_CONTEXT_REALM]);
135
Jayanth Dodderi Chidanandd62c6812023-03-07 10:43:19 +0000136 if (is_feat_sve_supported()) {
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000137 /*
138 * Enable SVE and FPU in realm context when it is enabled for NS.
139 * Realm manager must ensure that the SVE and FPU register
140 * contexts are properly managed.
141 */
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100142 sve_enable_per_world(&per_world_context[CPU_CONTEXT_REALM]);
Jayanth Dodderi Chidanandd62c6812023-03-07 10:43:19 +0000143 }
Boyan Karatotev05504ba2023-02-15 13:21:50 +0000144
Boyan Karatotev919d3c82023-02-13 16:32:47 +0000145 /* NS can access this but Realm shouldn't */
146 if (is_feat_sys_reg_trace_supported()) {
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100147 sys_reg_trace_disable_per_world(&per_world_context[CPU_CONTEXT_REALM]);
Boyan Karatotev919d3c82023-02-13 16:32:47 +0000148 }
149
Arunachalam Ganapathya87a4092023-11-01 19:18:41 +0000150 /*
151 * If SME/SME2 is supported and enabled for NS world, then disable trapping
152 * of SME instructions for Realm world. RMM will save/restore required
153 * registers that are shared with SVE/FPU so that Realm can use FPU or SVE.
154 */
155 if (is_feat_sme_supported()) {
156 sme_enable_per_world(&per_world_context[CPU_CONTEXT_REALM]);
157 }
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000158}
159
160/*******************************************************************************
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500161 * Jump to the RMM for the first time.
162 ******************************************************************************/
163static int32_t rmm_init(void)
164{
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000165 long rc;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500166 rmmd_rmm_context_t *ctx = &rmm_context[plat_my_core_pos()];
167
168 INFO("RMM init start.\n");
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500169
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000170 /* Enable architecture extensions */
171 manage_extensions_realm(&ctx->cpu_ctx);
172
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100173 manage_extensions_realm_per_world();
174
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500175 /* Initialize RMM EL2 context. */
176 rmm_el2_context_init(&ctx->cpu_ctx.el2_sysregs_ctx);
177
178 rc = rmmd_rmm_sync_entry(ctx);
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000179 if (rc != E_RMM_BOOT_SUCCESS) {
180 ERROR("RMM init failed: %ld\n", rc);
181 /* Mark the boot as failed for all the CPUs */
182 rmm_boot_failed = true;
183 return 0;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500184 }
185
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500186 INFO("RMM init end.\n");
187
188 return 1;
189}
190
191/*******************************************************************************
192 * Load and read RMM manifest, setup RMM.
193 ******************************************************************************/
194int rmmd_setup(void)
195{
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100196 size_t shared_buf_size __unused;
197 uintptr_t shared_buf_base;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500198 uint32_t ep_attr;
199 unsigned int linear_id = plat_my_core_pos();
200 rmmd_rmm_context_t *rmm_ctx = &rmm_context[linear_id];
AlexeiFedorov8e754f92022-12-14 17:28:11 +0000201 struct rmm_manifest *manifest;
Javier Almansa Sobrino4165e842022-04-25 17:18:15 +0100202 int rc;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500203
204 /* Make sure RME is supported. */
Varun Wadekar073bcf72024-07-15 20:40:05 +0000205 if (is_feat_rme_present() == 0U) {
206 /* Mark the RMM boot as failed for all the CPUs */
207 rmm_boot_failed = true;
208 return -ENOTSUP;
209 }
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500210
211 rmm_ep_info = bl31_plat_get_next_image_ep_info(REALM);
212 if (rmm_ep_info == NULL) {
213 WARN("No RMM image provided by BL2 boot loader, Booting "
214 "device without RMM initialization. SMCs destined for "
215 "RMM will return SMC_UNK\n");
Varun Wadekar073bcf72024-07-15 20:40:05 +0000216
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500217 return -ENOENT;
218 }
219
220 /* Under no circumstances will this parameter be 0 */
221 assert(rmm_ep_info->pc == RMM_BASE);
222
223 /* Initialise an entrypoint to set up the CPU context */
224 ep_attr = EP_REALM;
225 if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0U) {
226 ep_attr |= EP_EE_BIG;
227 }
228
229 SET_PARAM_HEAD(rmm_ep_info, PARAM_EP, VERSION_1, ep_attr);
230 rmm_ep_info->spsr = SPSR_64(MODE_EL2,
231 MODE_SP_ELX,
232 DISABLE_ALL_EXCEPTIONS);
233
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000234 shared_buf_size =
235 plat_rmmd_get_el3_rmm_shared_mem(&shared_buf_base);
236
237 assert((shared_buf_size == SZ_4K) &&
238 ((void *)shared_buf_base != NULL));
239
Soby Mathew414043d2024-03-26 17:16:00 +0000240 /* Zero out and load the boot manifest at the beginning of the share area */
AlexeiFedorov8e754f92022-12-14 17:28:11 +0000241 manifest = (struct rmm_manifest *)shared_buf_base;
Harry Moultone67b1272024-04-04 09:09:25 +0100242 (void)memset((void *)manifest, 0, sizeof(struct rmm_manifest));
Soby Mathew414043d2024-03-26 17:16:00 +0000243
Javier Almansa Sobrino4165e842022-04-25 17:18:15 +0100244 rc = plat_rmmd_load_manifest(manifest);
245 if (rc != 0) {
246 ERROR("Error loading RMM Boot Manifest (%i)\n", rc);
Varun Wadekarb4446412024-07-21 11:37:49 +0000247 /* Mark the boot as failed for all the CPUs */
248 rmm_boot_failed = true;
Javier Almansa Sobrino4165e842022-04-25 17:18:15 +0100249 return rc;
250 }
251 flush_dcache_range((uintptr_t)shared_buf_base, shared_buf_size);
252
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000253 /*
254 * Prepare coldboot arguments for RMM:
255 * arg0: This CPUID (primary processor).
256 * arg1: Version for this Boot Interface.
257 * arg2: PLATFORM_CORE_COUNT.
258 * arg3: Base address for the EL3 <-> RMM shared area. The boot
259 * manifest will be stored at the beginning of this area.
260 */
261 rmm_ep_info->args.arg0 = linear_id;
262 rmm_ep_info->args.arg1 = RMM_EL3_INTERFACE_VERSION;
263 rmm_ep_info->args.arg2 = PLATFORM_CORE_COUNT;
264 rmm_ep_info->args.arg3 = shared_buf_base;
265
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500266 /* Initialise RMM context with this entry point information */
267 cm_setup_context(&rmm_ctx->cpu_ctx, rmm_ep_info);
268
269 INFO("RMM setup done.\n");
270
271 /* Register init function for deferred init. */
272 bl31_register_rmm_init(&rmm_init);
273
274 return 0;
275}
276
277/*******************************************************************************
278 * Forward SMC to the other security state
279 ******************************************************************************/
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000280static uint64_t rmmd_smc_forward(uint32_t src_sec_state,
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100281 uint32_t dst_sec_state, uint64_t x0,
282 uint64_t x1, uint64_t x2, uint64_t x3,
283 uint64_t x4, void *handle)
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500284{
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100285 cpu_context_t *ctx = cm_get_context(dst_sec_state);
286
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500287 /* Save incoming security state */
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500288 cm_el2_sysregs_context_save(src_sec_state);
289
290 /* Restore outgoing security state */
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500291 cm_el2_sysregs_context_restore(dst_sec_state);
292 cm_set_next_eret_context(dst_sec_state);
293
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000294 /*
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100295 * As per SMCCCv1.2, we need to preserve x4 to x7 unless
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000296 * being used as return args. Hence we differentiate the
297 * onward and backward path. Support upto 8 args in the
298 * onward path and 4 args in return path.
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100299 * Register x4 will be preserved by RMM in case it is not
300 * used in return path.
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000301 */
302 if (src_sec_state == NON_SECURE) {
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100303 SMC_RET8(ctx, x0, x1, x2, x3, x4,
304 SMC_GET_GP(handle, CTX_GPREG_X5),
305 SMC_GET_GP(handle, CTX_GPREG_X6),
306 SMC_GET_GP(handle, CTX_GPREG_X7));
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000307 }
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100308
309 SMC_RET5(ctx, x0, x1, x2, x3, x4);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500310}
311
312/*******************************************************************************
313 * This function handles all SMCs in the range reserved for RMI. Each call is
314 * either forwarded to the other security state or handled by the RMM dispatcher
315 ******************************************************************************/
316uint64_t rmmd_rmi_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2,
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100317 uint64_t x3, uint64_t x4, void *cookie,
318 void *handle, uint64_t flags)
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500319{
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500320 uint32_t src_sec_state;
321
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000322 /* If RMM failed to boot, treat any RMI SMC as unknown */
323 if (rmm_boot_failed) {
324 WARN("RMMD: Failed to boot up RMM. Ignoring RMI call\n");
325 SMC_RET1(handle, SMC_UNK);
326 }
327
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500328 /* Determine which security state this SMC originated from */
329 src_sec_state = caller_sec_state(flags);
330
331 /* RMI must not be invoked by the Secure world */
332 if (src_sec_state == SMC_FROM_SECURE) {
Soby Mathew68ea9542022-03-22 13:58:52 +0000333 WARN("RMMD: RMI invoked by secure world.\n");
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500334 SMC_RET1(handle, SMC_UNK);
335 }
336
337 /*
338 * Forward an RMI call from the Normal world to the Realm world as it
339 * is.
340 */
341 if (src_sec_state == SMC_FROM_NON_SECURE) {
Arunachalam Ganapathy6e84add2023-08-24 15:31:01 +0100342 /*
343 * If SVE hint bit is set in the flags then update the SMC
344 * function id and pass it on to the lower EL.
345 */
346 if (is_sve_hint_set(flags)) {
347 smc_fid |= (FUNCID_SVE_HINT_MASK <<
348 FUNCID_SVE_HINT_SHIFT);
349 }
Soby Mathew68ea9542022-03-22 13:58:52 +0000350 VERBOSE("RMMD: RMI call from non-secure world.\n");
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000351 return rmmd_smc_forward(NON_SECURE, REALM, smc_fid,
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500352 x1, x2, x3, x4, handle);
353 }
354
Soby Mathew68ea9542022-03-22 13:58:52 +0000355 if (src_sec_state != SMC_FROM_REALM) {
356 SMC_RET1(handle, SMC_UNK);
357 }
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500358
359 switch (smc_fid) {
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100360 case RMM_RMI_REQ_COMPLETE: {
361 uint64_t x5 = SMC_GET_GP(handle, CTX_GPREG_X5);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500362
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100363 return rmmd_smc_forward(REALM, NON_SECURE, x1,
364 x2, x3, x4, x5, handle);
365 }
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500366 default:
Soby Mathew68ea9542022-03-22 13:58:52 +0000367 WARN("RMMD: Unsupported RMM call 0x%08x\n", smc_fid);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500368 SMC_RET1(handle, SMC_UNK);
369 }
370}
371
372/*******************************************************************************
373 * This cpu has been turned on. Enter RMM to initialise R-EL2. Entry into RMM
374 * is done after initialising minimal architectural state that guarantees safe
375 * execution.
376 ******************************************************************************/
377static void *rmmd_cpu_on_finish_handler(const void *arg)
378{
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000379 long rc;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500380 uint32_t linear_id = plat_my_core_pos();
381 rmmd_rmm_context_t *ctx = &rmm_context[linear_id];
382
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000383 if (rmm_boot_failed) {
384 /* RMM Boot failed on a previous CPU. Abort. */
385 ERROR("RMM Failed to initialize. Ignoring for CPU%d\n",
386 linear_id);
387 return NULL;
388 }
389
390 /*
391 * Prepare warmboot arguments for RMM:
392 * arg0: This CPUID.
393 * arg1 to arg3: Not used.
394 */
395 rmm_ep_info->args.arg0 = linear_id;
396 rmm_ep_info->args.arg1 = 0ULL;
397 rmm_ep_info->args.arg2 = 0ULL;
398 rmm_ep_info->args.arg3 = 0ULL;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500399
400 /* Initialise RMM context with this entry point information */
401 cm_setup_context(&ctx->cpu_ctx, rmm_ep_info);
402
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000403 /* Enable architecture extensions */
404 manage_extensions_realm(&ctx->cpu_ctx);
405
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500406 /* Initialize RMM EL2 context. */
407 rmm_el2_context_init(&ctx->cpu_ctx.el2_sysregs_ctx);
408
409 rc = rmmd_rmm_sync_entry(ctx);
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000410
411 if (rc != E_RMM_BOOT_SUCCESS) {
412 ERROR("RMM init failed on CPU%d: %ld\n", linear_id, rc);
413 /* Mark the boot as failed for any other booting CPU */
414 rmm_boot_failed = true;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500415 }
416
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500417 return NULL;
418}
419
420/* Subscribe to PSCI CPU on to initialize RMM on secondary */
421SUBSCRIBE_TO_EVENT(psci_cpu_on_finish, rmmd_cpu_on_finish_handler);
422
Soby Mathew68ea9542022-03-22 13:58:52 +0000423/* Convert GPT lib error to RMMD GTS error */
424static int gpt_to_gts_error(int error, uint32_t smc_fid, uint64_t address)
425{
426 int ret;
427
428 if (error == 0) {
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100429 return E_RMM_OK;
Soby Mathew68ea9542022-03-22 13:58:52 +0000430 }
431
432 if (error == -EINVAL) {
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100433 ret = E_RMM_BAD_ADDR;
Soby Mathew68ea9542022-03-22 13:58:52 +0000434 } else {
435 /* This is the only other error code we expect */
436 assert(error == -EPERM);
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100437 ret = E_RMM_BAD_PAS;
Soby Mathew68ea9542022-03-22 13:58:52 +0000438 }
439
440 ERROR("RMMD: PAS Transition failed. GPT ret = %d, PA: 0x%"PRIx64 ", FID = 0x%x\n",
441 error, address, smc_fid);
442 return ret;
443}
444
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500445/*******************************************************************************
Soby Mathew68ea9542022-03-22 13:58:52 +0000446 * This function handles RMM-EL3 interface SMCs
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500447 ******************************************************************************/
Soby Mathew68ea9542022-03-22 13:58:52 +0000448uint64_t rmmd_rmm_el3_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2,
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500449 uint64_t x3, uint64_t x4, void *cookie,
450 void *handle, uint64_t flags)
451{
452 uint32_t src_sec_state;
Robert Wakim48e6b572021-10-21 15:39:56 +0100453 int ret;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500454
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000455 /* If RMM failed to boot, treat any RMM-EL3 interface SMC as unknown */
456 if (rmm_boot_failed) {
457 WARN("RMMD: Failed to boot up RMM. Ignoring RMM-EL3 call\n");
458 SMC_RET1(handle, SMC_UNK);
459 }
460
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500461 /* Determine which security state this SMC originated from */
462 src_sec_state = caller_sec_state(flags);
463
464 if (src_sec_state != SMC_FROM_REALM) {
Soby Mathew68ea9542022-03-22 13:58:52 +0000465 WARN("RMMD: RMM-EL3 call originated from secure or normal world\n");
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500466 SMC_RET1(handle, SMC_UNK);
467 }
468
469 switch (smc_fid) {
Javier Almansa Sobrinof809b162022-07-04 17:06:36 +0100470 case RMM_GTSI_DELEGATE:
Robert Wakim48e6b572021-10-21 15:39:56 +0100471 ret = gpt_delegate_pas(x1, PAGE_SIZE_4KB, SMC_FROM_REALM);
Soby Mathew68ea9542022-03-22 13:58:52 +0000472 SMC_RET1(handle, gpt_to_gts_error(ret, smc_fid, x1));
Javier Almansa Sobrinof809b162022-07-04 17:06:36 +0100473 case RMM_GTSI_UNDELEGATE:
Robert Wakim48e6b572021-10-21 15:39:56 +0100474 ret = gpt_undelegate_pas(x1, PAGE_SIZE_4KB, SMC_FROM_REALM);
Soby Mathew68ea9542022-03-22 13:58:52 +0000475 SMC_RET1(handle, gpt_to_gts_error(ret, smc_fid, x1));
Javier Almansa Sobrinof809b162022-07-04 17:06:36 +0100476 case RMM_ATTEST_GET_PLAT_TOKEN:
Soby Mathew294e1cf2022-03-22 16:19:39 +0000477 ret = rmmd_attest_get_platform_token(x1, &x2, x3);
478 SMC_RET2(handle, ret, x2);
Javier Almansa Sobrinof809b162022-07-04 17:06:36 +0100479 case RMM_ATTEST_GET_REALM_KEY:
Soby Mathewf05d93a2022-03-22 16:21:19 +0000480 ret = rmmd_attest_get_signing_key(x1, &x2, x3);
481 SMC_RET2(handle, ret, x2);
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000482
483 case RMM_BOOT_COMPLETE:
484 VERBOSE("RMMD: running rmmd_rmm_sync_exit\n");
485 rmmd_rmm_sync_exit(x1);
486
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500487 default:
Soby Mathew68ea9542022-03-22 13:58:52 +0000488 WARN("RMMD: Unsupported RMM-EL3 call 0x%08x\n", smc_fid);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500489 SMC_RET1(handle, SMC_UNK);
490 }
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500491}