blob: e94b048146845a4ec1f622dc2a917852b7b2f9e1 [file] [log] [blame]
Zelalem Aweke13dc8f12021-07-09 14:20:03 -05001/*
Sona Mathew04774e72025-03-11 15:59:02 -05002 * Copyright (c) 2021-2025, 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>
Boyan Karatotev4a615bb2024-12-10 17:13:51 +000036#include <lib/extensions/spe.h>
37#include <lib/extensions/trbe.h>
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050038#include "rmmd_initial_context.h"
39#include "rmmd_private.h"
40
41/*******************************************************************************
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +000042 * RMM boot failure flag
43 ******************************************************************************/
44static bool rmm_boot_failed;
45
46/*******************************************************************************
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050047 * RMM context information.
48 ******************************************************************************/
49rmmd_rmm_context_t rmm_context[PLATFORM_CORE_COUNT];
50
51/*******************************************************************************
52 * RMM entry point information. Discovered on the primary core and reused
53 * on secondary cores.
54 ******************************************************************************/
55static entry_point_info_t *rmm_ep_info;
56
57/*******************************************************************************
58 * Static function declaration.
59 ******************************************************************************/
60static int32_t rmm_init(void);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050061
62/*******************************************************************************
63 * This function takes an RMM context pointer and performs a synchronous entry
64 * into it.
65 ******************************************************************************/
66uint64_t rmmd_rmm_sync_entry(rmmd_rmm_context_t *rmm_ctx)
67{
68 uint64_t rc;
69
70 assert(rmm_ctx != NULL);
71
72 cm_set_context(&(rmm_ctx->cpu_ctx), REALM);
73
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050074 /* Restore the realm context assigned above */
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050075 cm_el2_sysregs_context_restore(REALM);
76 cm_set_next_eret_context(REALM);
77
78 /* Enter RMM */
79 rc = rmmd_rmm_enter(&rmm_ctx->c_rt_ctx);
80
Zelalem Awekef92c0cb2022-01-31 16:59:42 -060081 /*
Jayanth Dodderi Chidanandc05031c2023-09-12 12:07:56 +010082 * Save realm context. EL2 Non-secure context will be restored
83 * before exiting Non-secure world, therefore there is no need
84 * to clear EL2 context registers.
Zelalem Awekef92c0cb2022-01-31 16:59:42 -060085 */
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050086 cm_el2_sysregs_context_save(REALM);
87
Zelalem Aweke13dc8f12021-07-09 14:20:03 -050088 return rc;
89}
90
91/*******************************************************************************
92 * This function returns to the place where rmmd_rmm_sync_entry() was
93 * called originally.
94 ******************************************************************************/
95__dead2 void rmmd_rmm_sync_exit(uint64_t rc)
96{
97 rmmd_rmm_context_t *ctx = &rmm_context[plat_my_core_pos()];
98
99 /* Get context of the RMM in use by this CPU. */
100 assert(cm_get_context(REALM) == &(ctx->cpu_ctx));
101
102 /*
103 * The RMMD must have initiated the original request through a
104 * synchronous entry into RMM. Jump back to the original C runtime
105 * context with the value of rc in x0;
106 */
107 rmmd_rmm_exit(ctx->c_rt_ctx, rc);
108
109 panic();
110}
111
112static void rmm_el2_context_init(el2_sysregs_t *regs)
113{
Jayanth Dodderi Chidanandfbbee6b2024-01-24 20:05:07 +0000114 write_el2_ctx_common(regs, spsr_el2, REALM_SPSR_EL2);
115 write_el2_ctx_common(regs, sctlr_el2, SCTLR_EL2_RES1);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500116}
117
118/*******************************************************************************
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000119 * Enable architecture extensions on first entry to Realm world.
120 ******************************************************************************/
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100121
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000122static void manage_extensions_realm(cpu_context_t *ctx)
123{
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100124 /*
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 }
Boyan Karatotev4a615bb2024-12-10 17:13:51 +0000130
131 /*
132 * SPE and TRBE cannot be fully disabled from EL3 registers alone, only
133 * sysreg access can. In case the EL1 controls leave them active on
134 * context switch, we want the owning security state to be NS so Realm
135 * can't be DOSed.
136 */
137 if (is_feat_spe_supported()) {
138 spe_disable(ctx);
139 }
140
141 if (is_feat_trbe_supported()) {
142 trbe_disable(ctx);
143 }
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100144}
145
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100146static void manage_extensions_realm_per_world(void)
147{
Jayanth Dodderi Chidanand56aa3822023-12-11 11:22:02 +0000148 cm_el3_arch_init_per_world(&per_world_context[CPU_CONTEXT_REALM]);
149
Jayanth Dodderi Chidanandd62c6812023-03-07 10:43:19 +0000150 if (is_feat_sve_supported()) {
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000151 /*
152 * Enable SVE and FPU in realm context when it is enabled for NS.
153 * Realm manager must ensure that the SVE and FPU register
154 * contexts are properly managed.
155 */
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100156 sve_enable_per_world(&per_world_context[CPU_CONTEXT_REALM]);
Jayanth Dodderi Chidanandd62c6812023-03-07 10:43:19 +0000157 }
Boyan Karatotev05504ba2023-02-15 13:21:50 +0000158
Boyan Karatotev919d3c82023-02-13 16:32:47 +0000159 /* NS can access this but Realm shouldn't */
160 if (is_feat_sys_reg_trace_supported()) {
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100161 sys_reg_trace_disable_per_world(&per_world_context[CPU_CONTEXT_REALM]);
Boyan Karatotev919d3c82023-02-13 16:32:47 +0000162 }
163
Arunachalam Ganapathya87a4092023-11-01 19:18:41 +0000164 /*
165 * If SME/SME2 is supported and enabled for NS world, then disable trapping
166 * of SME instructions for Realm world. RMM will save/restore required
167 * registers that are shared with SVE/FPU so that Realm can use FPU or SVE.
168 */
169 if (is_feat_sme_supported()) {
170 sme_enable_per_world(&per_world_context[CPU_CONTEXT_REALM]);
171 }
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000172}
173
174/*******************************************************************************
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500175 * Jump to the RMM for the first time.
176 ******************************************************************************/
177static int32_t rmm_init(void)
178{
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000179 long rc;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500180 rmmd_rmm_context_t *ctx = &rmm_context[plat_my_core_pos()];
181
182 INFO("RMM init start.\n");
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500183
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000184 /* Enable architecture extensions */
185 manage_extensions_realm(&ctx->cpu_ctx);
186
Elizabeth Ho4fc00d22023-07-18 14:10:25 +0100187 manage_extensions_realm_per_world();
188
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500189 /* Initialize RMM EL2 context. */
190 rmm_el2_context_init(&ctx->cpu_ctx.el2_sysregs_ctx);
191
192 rc = rmmd_rmm_sync_entry(ctx);
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000193 if (rc != E_RMM_BOOT_SUCCESS) {
194 ERROR("RMM init failed: %ld\n", rc);
195 /* Mark the boot as failed for all the CPUs */
196 rmm_boot_failed = true;
197 return 0;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500198 }
199
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500200 INFO("RMM init end.\n");
201
202 return 1;
203}
204
205/*******************************************************************************
206 * Load and read RMM manifest, setup RMM.
207 ******************************************************************************/
208int rmmd_setup(void)
209{
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100210 size_t shared_buf_size __unused;
211 uintptr_t shared_buf_base;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500212 uint32_t ep_attr;
213 unsigned int linear_id = plat_my_core_pos();
214 rmmd_rmm_context_t *rmm_ctx = &rmm_context[linear_id];
AlexeiFedorov8e754f92022-12-14 17:28:11 +0000215 struct rmm_manifest *manifest;
Javier Almansa Sobrino4165e842022-04-25 17:18:15 +0100216 int rc;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500217
218 /* Make sure RME is supported. */
Varun Wadekar073bcf72024-07-15 20:40:05 +0000219 if (is_feat_rme_present() == 0U) {
220 /* Mark the RMM boot as failed for all the CPUs */
221 rmm_boot_failed = true;
222 return -ENOTSUP;
223 }
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500224
225 rmm_ep_info = bl31_plat_get_next_image_ep_info(REALM);
Varun Wadekar82440992024-07-16 09:45:14 +0000226 if ((rmm_ep_info == NULL) || (rmm_ep_info->pc == 0)) {
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500227 WARN("No RMM image provided by BL2 boot loader, Booting "
228 "device without RMM initialization. SMCs destined for "
229 "RMM will return SMC_UNK\n");
Varun Wadekar073bcf72024-07-15 20:40:05 +0000230
Varun Wadekar50e7d032024-07-15 20:51:44 +0000231 /* Mark the boot as failed for all the CPUs */
232 rmm_boot_failed = true;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500233 return -ENOENT;
234 }
235
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500236 /* Initialise an entrypoint to set up the CPU context */
237 ep_attr = EP_REALM;
238 if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0U) {
239 ep_attr |= EP_EE_BIG;
240 }
241
242 SET_PARAM_HEAD(rmm_ep_info, PARAM_EP, VERSION_1, ep_attr);
243 rmm_ep_info->spsr = SPSR_64(MODE_EL2,
244 MODE_SP_ELX,
245 DISABLE_ALL_EXCEPTIONS);
246
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000247 shared_buf_size =
248 plat_rmmd_get_el3_rmm_shared_mem(&shared_buf_base);
249
250 assert((shared_buf_size == SZ_4K) &&
251 ((void *)shared_buf_base != NULL));
252
Soby Mathew414043d2024-03-26 17:16:00 +0000253 /* Zero out and load the boot manifest at the beginning of the share area */
AlexeiFedorov8e754f92022-12-14 17:28:11 +0000254 manifest = (struct rmm_manifest *)shared_buf_base;
Harry Moultone67b1272024-04-04 09:09:25 +0100255 (void)memset((void *)manifest, 0, sizeof(struct rmm_manifest));
Soby Mathew414043d2024-03-26 17:16:00 +0000256
Javier Almansa Sobrino4165e842022-04-25 17:18:15 +0100257 rc = plat_rmmd_load_manifest(manifest);
258 if (rc != 0) {
259 ERROR("Error loading RMM Boot Manifest (%i)\n", rc);
Varun Wadekarb4446412024-07-21 11:37:49 +0000260 /* Mark the boot as failed for all the CPUs */
261 rmm_boot_failed = true;
Javier Almansa Sobrino4165e842022-04-25 17:18:15 +0100262 return rc;
263 }
264 flush_dcache_range((uintptr_t)shared_buf_base, shared_buf_size);
265
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000266 /*
267 * Prepare coldboot arguments for RMM:
268 * arg0: This CPUID (primary processor).
269 * arg1: Version for this Boot Interface.
270 * arg2: PLATFORM_CORE_COUNT.
271 * arg3: Base address for the EL3 <-> RMM shared area. The boot
272 * manifest will be stored at the beginning of this area.
273 */
274 rmm_ep_info->args.arg0 = linear_id;
275 rmm_ep_info->args.arg1 = RMM_EL3_INTERFACE_VERSION;
276 rmm_ep_info->args.arg2 = PLATFORM_CORE_COUNT;
277 rmm_ep_info->args.arg3 = shared_buf_base;
278
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500279 /* Initialise RMM context with this entry point information */
280 cm_setup_context(&rmm_ctx->cpu_ctx, rmm_ep_info);
281
282 INFO("RMM setup done.\n");
283
284 /* Register init function for deferred init. */
285 bl31_register_rmm_init(&rmm_init);
286
287 return 0;
288}
289
290/*******************************************************************************
291 * Forward SMC to the other security state
292 ******************************************************************************/
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000293static uint64_t rmmd_smc_forward(uint32_t src_sec_state,
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100294 uint32_t dst_sec_state, uint64_t x0,
295 uint64_t x1, uint64_t x2, uint64_t x3,
296 uint64_t x4, void *handle)
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500297{
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100298 cpu_context_t *ctx = cm_get_context(dst_sec_state);
299
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500300 /* Save incoming security state */
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500301 cm_el2_sysregs_context_save(src_sec_state);
302
303 /* Restore outgoing security state */
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500304 cm_el2_sysregs_context_restore(dst_sec_state);
305 cm_set_next_eret_context(dst_sec_state);
306
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000307 /*
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100308 * As per SMCCCv1.2, we need to preserve x4 to x7 unless
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000309 * being used as return args. Hence we differentiate the
310 * onward and backward path. Support upto 8 args in the
311 * onward path and 4 args in return path.
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100312 * Register x4 will be preserved by RMM in case it is not
313 * used in return path.
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000314 */
315 if (src_sec_state == NON_SECURE) {
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100316 SMC_RET8(ctx, x0, x1, x2, x3, x4,
317 SMC_GET_GP(handle, CTX_GPREG_X5),
318 SMC_GET_GP(handle, CTX_GPREG_X6),
319 SMC_GET_GP(handle, CTX_GPREG_X7));
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000320 }
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100321
322 SMC_RET5(ctx, x0, x1, x2, x3, x4);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500323}
324
325/*******************************************************************************
326 * This function handles all SMCs in the range reserved for RMI. Each call is
327 * either forwarded to the other security state or handled by the RMM dispatcher
328 ******************************************************************************/
329uint64_t rmmd_rmi_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2,
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100330 uint64_t x3, uint64_t x4, void *cookie,
331 void *handle, uint64_t flags)
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500332{
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500333 uint32_t src_sec_state;
334
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000335 /* If RMM failed to boot, treat any RMI SMC as unknown */
336 if (rmm_boot_failed) {
337 WARN("RMMD: Failed to boot up RMM. Ignoring RMI call\n");
338 SMC_RET1(handle, SMC_UNK);
339 }
340
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500341 /* Determine which security state this SMC originated from */
342 src_sec_state = caller_sec_state(flags);
343
344 /* RMI must not be invoked by the Secure world */
345 if (src_sec_state == SMC_FROM_SECURE) {
Soby Mathew68ea9542022-03-22 13:58:52 +0000346 WARN("RMMD: RMI invoked by secure world.\n");
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500347 SMC_RET1(handle, SMC_UNK);
348 }
349
350 /*
351 * Forward an RMI call from the Normal world to the Realm world as it
352 * is.
353 */
354 if (src_sec_state == SMC_FROM_NON_SECURE) {
Arunachalam Ganapathy6e84add2023-08-24 15:31:01 +0100355 /*
356 * If SVE hint bit is set in the flags then update the SMC
357 * function id and pass it on to the lower EL.
358 */
359 if (is_sve_hint_set(flags)) {
360 smc_fid |= (FUNCID_SVE_HINT_MASK <<
361 FUNCID_SVE_HINT_SHIFT);
362 }
Soby Mathew68ea9542022-03-22 13:58:52 +0000363 VERBOSE("RMMD: RMI call from non-secure world.\n");
Soby Mathewfccd3ea2021-11-17 15:13:30 +0000364 return rmmd_smc_forward(NON_SECURE, REALM, smc_fid,
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500365 x1, x2, x3, x4, handle);
366 }
367
Soby Mathew68ea9542022-03-22 13:58:52 +0000368 if (src_sec_state != SMC_FROM_REALM) {
369 SMC_RET1(handle, SMC_UNK);
370 }
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500371
372 switch (smc_fid) {
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100373 case RMM_RMI_REQ_COMPLETE: {
374 uint64_t x5 = SMC_GET_GP(handle, CTX_GPREG_X5);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500375
AlexeiFedorov90ce18f2022-09-23 16:57:28 +0100376 return rmmd_smc_forward(REALM, NON_SECURE, x1,
377 x2, x3, x4, x5, handle);
378 }
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500379 default:
Soby Mathew68ea9542022-03-22 13:58:52 +0000380 WARN("RMMD: Unsupported RMM call 0x%08x\n", smc_fid);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500381 SMC_RET1(handle, SMC_UNK);
382 }
383}
384
385/*******************************************************************************
386 * This cpu has been turned on. Enter RMM to initialise R-EL2. Entry into RMM
387 * is done after initialising minimal architectural state that guarantees safe
388 * execution.
389 ******************************************************************************/
390static void *rmmd_cpu_on_finish_handler(const void *arg)
391{
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000392 long rc;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500393 uint32_t linear_id = plat_my_core_pos();
394 rmmd_rmm_context_t *ctx = &rmm_context[linear_id];
395
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000396 if (rmm_boot_failed) {
397 /* RMM Boot failed on a previous CPU. Abort. */
398 ERROR("RMM Failed to initialize. Ignoring for CPU%d\n",
399 linear_id);
400 return NULL;
401 }
402
403 /*
404 * Prepare warmboot arguments for RMM:
405 * arg0: This CPUID.
406 * arg1 to arg3: Not used.
407 */
408 rmm_ep_info->args.arg0 = linear_id;
409 rmm_ep_info->args.arg1 = 0ULL;
410 rmm_ep_info->args.arg2 = 0ULL;
411 rmm_ep_info->args.arg3 = 0ULL;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500412
413 /* Initialise RMM context with this entry point information */
414 cm_setup_context(&ctx->cpu_ctx, rmm_ep_info);
415
Subhasish Ghoshc25225a2021-12-09 15:41:37 +0000416 /* Enable architecture extensions */
417 manage_extensions_realm(&ctx->cpu_ctx);
418
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500419 /* Initialize RMM EL2 context. */
420 rmm_el2_context_init(&ctx->cpu_ctx.el2_sysregs_ctx);
421
422 rc = rmmd_rmm_sync_entry(ctx);
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000423
424 if (rc != E_RMM_BOOT_SUCCESS) {
425 ERROR("RMM init failed on CPU%d: %ld\n", linear_id, rc);
426 /* Mark the boot as failed for any other booting CPU */
427 rmm_boot_failed = true;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500428 }
429
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500430 return NULL;
431}
432
433/* Subscribe to PSCI CPU on to initialize RMM on secondary */
434SUBSCRIBE_TO_EVENT(psci_cpu_on_finish, rmmd_cpu_on_finish_handler);
435
Soby Mathew68ea9542022-03-22 13:58:52 +0000436/* Convert GPT lib error to RMMD GTS error */
437static int gpt_to_gts_error(int error, uint32_t smc_fid, uint64_t address)
438{
439 int ret;
440
441 if (error == 0) {
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100442 return E_RMM_OK;
Soby Mathew68ea9542022-03-22 13:58:52 +0000443 }
444
445 if (error == -EINVAL) {
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100446 ret = E_RMM_BAD_ADDR;
Soby Mathew68ea9542022-03-22 13:58:52 +0000447 } else {
448 /* This is the only other error code we expect */
449 assert(error == -EPERM);
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100450 ret = E_RMM_BAD_PAS;
Soby Mathew68ea9542022-03-22 13:58:52 +0000451 }
452
453 ERROR("RMMD: PAS Transition failed. GPT ret = %d, PA: 0x%"PRIx64 ", FID = 0x%x\n",
454 error, address, smc_fid);
455 return ret;
456}
457
Raghu Krishnamurthyc11b60e2024-06-03 19:02:29 -0700458static int rmm_el3_ifc_get_feat_register(uint64_t feat_reg_idx,
459 uint64_t *feat_reg)
460{
461 if (feat_reg_idx != RMM_EL3_FEAT_REG_0_IDX) {
462 ERROR("RMMD: Failed to get feature register %ld\n", feat_reg_idx);
463 return E_RMM_INVAL;
464 }
465
466 *feat_reg = 0UL;
467#if RMMD_ENABLE_EL3_TOKEN_SIGN
468 *feat_reg |= RMM_EL3_FEAT_REG_0_EL3_TOKEN_SIGN_MASK;
469#endif
470 return E_RMM_OK;
471}
472
Tushar Khandelwal01365af2024-04-22 15:35:40 +0100473/*
474 * Update encryption key associated with @mecid.
475 */
476static int rmmd_mecid_key_update(uint64_t mecid)
477{
478 uint64_t mecid_width, mecid_width_mask;
479 int ret;
480
481 /*
482 * Check whether the mecid parameter is at most MECIDR_EL2.MECIDWidthm1 + 1
483 * in length.
484 */
485 mecid_width = ((read_mecidr_el2() >> MECIDR_EL2_MECIDWidthm1_SHIFT) &
486 MECIDR_EL2_MECIDWidthm1_MASK) + 1;
487 mecid_width_mask = ((1 << mecid_width) - 1);
488 if ((mecid & ~mecid_width_mask) != 0U) {
489 return E_RMM_INVAL;
490 }
491
492 ret = plat_rmmd_mecid_key_update(mecid);
493
494 if (ret != 0) {
495 return E_RMM_UNK;
496 }
497 return E_RMM_OK;
498}
499
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500500/*******************************************************************************
Soby Mathew68ea9542022-03-22 13:58:52 +0000501 * This function handles RMM-EL3 interface SMCs
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500502 ******************************************************************************/
Soby Mathew68ea9542022-03-22 13:58:52 +0000503uint64_t rmmd_rmm_el3_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2,
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500504 uint64_t x3, uint64_t x4, void *cookie,
505 void *handle, uint64_t flags)
506{
Raghu Krishnamurthyc11b60e2024-06-03 19:02:29 -0700507 uint64_t remaining_len = 0UL;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500508 uint32_t src_sec_state;
Robert Wakim48e6b572021-10-21 15:39:56 +0100509 int ret;
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500510
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000511 /* If RMM failed to boot, treat any RMM-EL3 interface SMC as unknown */
512 if (rmm_boot_failed) {
513 WARN("RMMD: Failed to boot up RMM. Ignoring RMM-EL3 call\n");
514 SMC_RET1(handle, SMC_UNK);
515 }
516
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500517 /* Determine which security state this SMC originated from */
518 src_sec_state = caller_sec_state(flags);
519
520 if (src_sec_state != SMC_FROM_REALM) {
Soby Mathew68ea9542022-03-22 13:58:52 +0000521 WARN("RMMD: RMM-EL3 call originated from secure or normal world\n");
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500522 SMC_RET1(handle, SMC_UNK);
523 }
524
525 switch (smc_fid) {
Javier Almansa Sobrinof809b162022-07-04 17:06:36 +0100526 case RMM_GTSI_DELEGATE:
Robert Wakim48e6b572021-10-21 15:39:56 +0100527 ret = gpt_delegate_pas(x1, PAGE_SIZE_4KB, SMC_FROM_REALM);
Soby Mathew68ea9542022-03-22 13:58:52 +0000528 SMC_RET1(handle, gpt_to_gts_error(ret, smc_fid, x1));
Javier Almansa Sobrinof809b162022-07-04 17:06:36 +0100529 case RMM_GTSI_UNDELEGATE:
Robert Wakim48e6b572021-10-21 15:39:56 +0100530 ret = gpt_undelegate_pas(x1, PAGE_SIZE_4KB, SMC_FROM_REALM);
Soby Mathew68ea9542022-03-22 13:58:52 +0000531 SMC_RET1(handle, gpt_to_gts_error(ret, smc_fid, x1));
Javier Almansa Sobrinof809b162022-07-04 17:06:36 +0100532 case RMM_ATTEST_GET_REALM_KEY:
Soby Mathewf05d93a2022-03-22 16:21:19 +0000533 ret = rmmd_attest_get_signing_key(x1, &x2, x3);
534 SMC_RET2(handle, ret, x2);
Sona Mathew04774e72025-03-11 15:59:02 -0500535 case RMM_ATTEST_GET_PLAT_TOKEN:
536 ret = rmmd_attest_get_platform_token(x1, &x2, x3, &remaining_len);
537 SMC_RET3(handle, ret, x2, remaining_len);
Raghu Krishnamurthyc11b60e2024-06-03 19:02:29 -0700538 case RMM_EL3_FEATURES:
539 ret = rmm_el3_ifc_get_feat_register(x1, &x2);
540 SMC_RET2(handle, ret, x2);
541#if RMMD_ENABLE_EL3_TOKEN_SIGN
542 case RMM_EL3_TOKEN_SIGN:
543 return rmmd_el3_token_sign(handle, x1, x2, x3, x4);
544#endif
Javier Almansa Sobrino7176a772021-11-24 18:37:37 +0000545 case RMM_BOOT_COMPLETE:
546 VERBOSE("RMMD: running rmmd_rmm_sync_exit\n");
547 rmmd_rmm_sync_exit(x1);
548
Tushar Khandelwal01365af2024-04-22 15:35:40 +0100549 case RMM_MECID_KEY_UPDATE:
550 ret = rmmd_mecid_key_update(x1);
551 SMC_RET1(handle, ret);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500552 default:
Soby Mathew68ea9542022-03-22 13:58:52 +0000553 WARN("RMMD: Unsupported RMM-EL3 call 0x%08x\n", smc_fid);
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500554 SMC_RET1(handle, SMC_UNK);
555 }
Zelalem Aweke13dc8f12021-07-09 14:20:03 -0500556}