blob: 4a0853e703edfe7096d06c59c215818e2ef0ee53 [file] [log] [blame]
Marc Bonnici8e1a7552021-12-01 17:57:04 +00001/*
2 * Copyright (c) 2022, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8#include <errno.h>
9
10#include <arch_helpers.h>
11#include <bl31/bl31.h>
12#include <bl31/ehf.h>
13#include <common/debug.h>
14#include <common/fdt_wrappers.h>
15#include <common/runtime_svc.h>
16#include <lib/el3_runtime/context_mgmt.h>
17#include <lib/smccc.h>
18#include <lib/utils.h>
19#include <lib/xlat_tables/xlat_tables_v2.h>
20#include <libfdt.h>
21#include <plat/common/platform.h>
Marc Bonnici9a297042022-02-14 17:06:09 +000022#include <services/el3_spmc_logical_sp.h>
Marc Bonnici8e1a7552021-12-01 17:57:04 +000023#include <services/ffa_svc.h>
24#include <services/spmc_svc.h>
25#include <services/spmd_svc.h>
26#include "spmc.h"
27
28#include <platform_def.h>
29
30/*
31 * Allocate a secure partition descriptor to describe each SP in the system that
32 * does not reside at EL3.
33 */
34static struct secure_partition_desc sp_desc[SECURE_PARTITION_COUNT];
35
36/*
37 * Allocate an NS endpoint descriptor to describe each VM and the Hypervisor in
38 * the system that interacts with a SP. It is used to track the Hypervisor
39 * buffer pair, version and ID for now. It could be extended to track VM
40 * properties when the SPMC supports indirect messaging.
41 */
42static struct ns_endpoint_desc ns_ep_desc[NS_PARTITION_COUNT];
43
44/*
Marc Bonnici9a297042022-02-14 17:06:09 +000045 * Helper function to obtain the array storing the EL3
46 * Logical Partition descriptors.
47 */
48struct el3_lp_desc *get_el3_lp_array(void)
49{
50 return (struct el3_lp_desc *) EL3_LP_DESCS_START;
51}
52
53/*
Marc Bonnici8e1a7552021-12-01 17:57:04 +000054 * Helper function to obtain the descriptor of the last SP to whom control was
55 * handed to on this physical cpu. Currently, we assume there is only one SP.
56 * TODO: Expand to track multiple partitions when required.
57 */
58struct secure_partition_desc *spmc_get_current_sp_ctx(void)
59{
60 return &(sp_desc[ACTIVE_SP_DESC_INDEX]);
61}
62
63/*
64 * Helper function to obtain the execution context of an SP on the
65 * current physical cpu.
66 */
67struct sp_exec_ctx *spmc_get_sp_ec(struct secure_partition_desc *sp)
68{
69 return &(sp->ec[get_ec_index(sp)]);
70}
71
72/* Helper function to get pointer to SP context from its ID. */
73struct secure_partition_desc *spmc_get_sp_ctx(uint16_t id)
74{
Marc Bonnicie95eb7c2021-12-08 14:24:03 +000075 /* Check for Secure World Partitions. */
Marc Bonnici8e1a7552021-12-01 17:57:04 +000076 for (unsigned int i = 0U; i < SECURE_PARTITION_COUNT; i++) {
77 if (sp_desc[i].sp_id == id) {
78 return &(sp_desc[i]);
79 }
80 }
81 return NULL;
82}
83
Marc Bonnici52a9cbc2021-11-24 10:32:16 +000084/*
85 * Helper function to obtain the descriptor of the Hypervisor or OS kernel.
86 * We assume that the first descriptor is reserved for this entity.
87 */
88struct ns_endpoint_desc *spmc_get_hyp_ctx(void)
89{
90 return &(ns_ep_desc[0]);
91}
92
Marc Bonnicia2cfa612021-11-24 10:33:48 +000093/*
94 * Helper function to obtain the RX/TX buffer pair descriptor of the Hypervisor
95 * or OS kernel in the normal world or the last SP that was run.
96 */
97struct mailbox *spmc_get_mbox_desc(bool secure_origin)
98{
99 /* Obtain the RX/TX buffer pair descriptor. */
100 if (secure_origin) {
101 return &(spmc_get_current_sp_ctx()->mailbox);
102 } else {
103 return &(spmc_get_hyp_ctx()->mailbox);
104 }
105}
106
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000107/******************************************************************************
108 * This function returns to the place where spmc_sp_synchronous_entry() was
109 * called originally.
110 ******************************************************************************/
111__dead2 void spmc_sp_synchronous_exit(struct sp_exec_ctx *ec, uint64_t rc)
112{
113 /*
114 * The SPM must have initiated the original request through a
115 * synchronous entry into the secure partition. Jump back to the
116 * original C runtime context with the value of rc in x0;
117 */
118 spm_secure_partition_exit(ec->c_rt_ctx, rc);
119
120 panic();
121}
122
123/*******************************************************************************
124 * Return FFA_ERROR with specified error code.
125 ******************************************************************************/
126uint64_t spmc_ffa_error_return(void *handle, int error_code)
127{
128 SMC_RET8(handle, FFA_ERROR,
129 FFA_TARGET_INFO_MBZ, error_code,
130 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
131 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
132}
133
134/******************************************************************************
135 * Helper function to validate a secure partition ID to ensure it does not
136 * conflict with any other FF-A component and follows the convention to
137 * indicate it resides within the secure world.
138 ******************************************************************************/
139bool is_ffa_secure_id_valid(uint16_t partition_id)
140{
Marc Bonnici9a297042022-02-14 17:06:09 +0000141 struct el3_lp_desc *el3_lp_descs = get_el3_lp_array();
142
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000143 /* Ensure the ID is not the invalid partition ID. */
144 if (partition_id == INV_SP_ID) {
145 return false;
146 }
147
148 /* Ensure the ID is not the SPMD ID. */
149 if (partition_id == SPMD_DIRECT_MSG_ENDPOINT_ID) {
150 return false;
151 }
152
153 /*
154 * Ensure the ID follows the convention to indicate it resides
155 * in the secure world.
156 */
157 if (!ffa_is_secure_world_id(partition_id)) {
158 return false;
159 }
160
161 /* Ensure we don't conflict with the SPMC partition ID. */
162 if (partition_id == FFA_SPMC_ID) {
163 return false;
164 }
165
166 /* Ensure we do not already have an SP context with this ID. */
167 if (spmc_get_sp_ctx(partition_id)) {
168 return false;
169 }
170
Marc Bonnici9a297042022-02-14 17:06:09 +0000171 /* Ensure we don't clash with any Logical SP's. */
172 for (unsigned int i = 0U; i < EL3_LP_DESCS_COUNT; i++) {
173 if (el3_lp_descs[i].sp_id == partition_id) {
174 return false;
175 }
176 }
177
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000178 return true;
179}
180
181/*******************************************************************************
Marc Bonnici7e19db82021-11-29 17:17:29 +0000182 * This function either forwards the request to the other world or returns
183 * with an ERET depending on the source of the call.
Marc Bonnici9a297042022-02-14 17:06:09 +0000184 * We can assume that the destination is for an entity at a lower exception
185 * level as any messages destined for a logical SP resident in EL3 will have
186 * already been taken care of by the SPMC before entering this function.
Marc Bonnici7e19db82021-11-29 17:17:29 +0000187 ******************************************************************************/
188static uint64_t spmc_smc_return(uint32_t smc_fid,
189 bool secure_origin,
190 uint64_t x1,
191 uint64_t x2,
192 uint64_t x3,
193 uint64_t x4,
194 void *handle,
195 void *cookie,
196 uint64_t flags,
197 uint16_t dst_id)
198{
199 /* If the destination is in the normal world always go via the SPMD. */
200 if (ffa_is_normal_world_id(dst_id)) {
201 return spmd_smc_handler(smc_fid, x1, x2, x3, x4,
202 cookie, handle, flags);
203 }
204 /*
205 * If the caller is secure and we want to return to the secure world,
206 * ERET directly.
207 */
208 else if (secure_origin && ffa_is_secure_world_id(dst_id)) {
209 SMC_RET5(handle, smc_fid, x1, x2, x3, x4);
210 }
211 /* If we originated in the normal world then switch contexts. */
212 else if (!secure_origin && ffa_is_secure_world_id(dst_id)) {
213 return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2,
214 x3, x4, handle);
215 } else {
216 /* Unknown State. */
217 panic();
218 }
219
220 /* Shouldn't be Reached. */
221 return 0;
222}
223
224/*******************************************************************************
Marc Bonnici8eb15202021-11-29 17:05:33 +0000225 * FF-A ABI Handlers.
226 ******************************************************************************/
Marc Bonnici5eeacd52021-11-29 17:05:57 +0000227
228/*******************************************************************************
229 * Helper function to validate arg2 as part of a direct message.
230 ******************************************************************************/
231static inline bool direct_msg_validate_arg2(uint64_t x2)
232{
233 /*
234 * We currently only support partition messages, therefore ensure x2 is
235 * not set.
236 */
237 if (x2 != (uint64_t) 0) {
238 VERBOSE("Arg2 MBZ for partition messages (0x%lx).\n", x2);
239 return false;
240 }
241 return true;
242}
243
244/*******************************************************************************
245 * Handle direct request messages and route to the appropriate destination.
246 ******************************************************************************/
247static uint64_t direct_req_smc_handler(uint32_t smc_fid,
248 bool secure_origin,
249 uint64_t x1,
250 uint64_t x2,
251 uint64_t x3,
252 uint64_t x4,
253 void *cookie,
254 void *handle,
255 uint64_t flags)
256{
257 uint16_t dst_id = ffa_endpoint_destination(x1);
Marc Bonnici9a297042022-02-14 17:06:09 +0000258 struct el3_lp_desc *el3_lp_descs;
Marc Bonnici5eeacd52021-11-29 17:05:57 +0000259 struct secure_partition_desc *sp;
260 unsigned int idx;
261
262 /* Check if arg2 has been populated correctly based on message type. */
263 if (!direct_msg_validate_arg2(x2)) {
264 return spmc_ffa_error_return(handle,
265 FFA_ERROR_INVALID_PARAMETER);
266 }
267
Marc Bonnici9a297042022-02-14 17:06:09 +0000268 el3_lp_descs = get_el3_lp_array();
269
270 /* Check if the request is destined for a Logical Partition. */
271 for (unsigned int i = 0U; i < MAX_EL3_LP_DESCS_COUNT; i++) {
272 if (el3_lp_descs[i].sp_id == dst_id) {
273 return el3_lp_descs[i].direct_req(
274 smc_fid, secure_origin, x1, x2, x3, x4,
275 cookie, handle, flags);
276 }
277 }
278
Marc Bonnici5eeacd52021-11-29 17:05:57 +0000279 /*
Marc Bonnici9a297042022-02-14 17:06:09 +0000280 * If the request was not targeted to a LSP and from the secure world
281 * then it is invalid since a SP cannot call into the Normal world and
282 * there is no other SP to call into. If there are other SPs in future
283 * then the partition runtime model would need to be validated as well.
Marc Bonnici5eeacd52021-11-29 17:05:57 +0000284 */
285 if (secure_origin) {
286 VERBOSE("Direct request not supported to the Normal World.\n");
287 return spmc_ffa_error_return(handle,
288 FFA_ERROR_INVALID_PARAMETER);
289 }
290
291 /* Check if the SP ID is valid. */
292 sp = spmc_get_sp_ctx(dst_id);
293 if (sp == NULL) {
294 VERBOSE("Direct request to unknown partition ID (0x%x).\n",
295 dst_id);
296 return spmc_ffa_error_return(handle,
297 FFA_ERROR_INVALID_PARAMETER);
298 }
299
300 /*
301 * Check that the target execution context is in a waiting state before
302 * forwarding the direct request to it.
303 */
304 idx = get_ec_index(sp);
305 if (sp->ec[idx].rt_state != RT_STATE_WAITING) {
306 VERBOSE("SP context on core%u is not waiting (%u).\n",
307 idx, sp->ec[idx].rt_model);
308 return spmc_ffa_error_return(handle, FFA_ERROR_BUSY);
309 }
310
311 /*
312 * Everything checks out so forward the request to the SP after updating
313 * its state and runtime model.
314 */
315 sp->ec[idx].rt_state = RT_STATE_RUNNING;
316 sp->ec[idx].rt_model = RT_MODEL_DIR_REQ;
317 return spmc_smc_return(smc_fid, secure_origin, x1, x2, x3, x4,
318 handle, cookie, flags, dst_id);
319}
320
321/*******************************************************************************
322 * Handle direct response messages and route to the appropriate destination.
323 ******************************************************************************/
324static uint64_t direct_resp_smc_handler(uint32_t smc_fid,
325 bool secure_origin,
326 uint64_t x1,
327 uint64_t x2,
328 uint64_t x3,
329 uint64_t x4,
330 void *cookie,
331 void *handle,
332 uint64_t flags)
333{
334 uint16_t dst_id = ffa_endpoint_destination(x1);
335 struct secure_partition_desc *sp;
336 unsigned int idx;
337
338 /* Check if arg2 has been populated correctly based on message type. */
339 if (!direct_msg_validate_arg2(x2)) {
340 return spmc_ffa_error_return(handle,
341 FFA_ERROR_INVALID_PARAMETER);
342 }
343
344 /* Check that the response did not originate from the Normal world. */
345 if (!secure_origin) {
346 VERBOSE("Direct Response not supported from Normal World.\n");
347 return spmc_ffa_error_return(handle,
348 FFA_ERROR_INVALID_PARAMETER);
349 }
350
351 /*
352 * Check that the response is either targeted to the Normal world or the
353 * SPMC e.g. a PM response.
354 */
355 if ((dst_id != FFA_SPMC_ID) && ffa_is_secure_world_id(dst_id)) {
356 VERBOSE("Direct response to invalid partition ID (0x%x).\n",
357 dst_id);
358 return spmc_ffa_error_return(handle,
359 FFA_ERROR_INVALID_PARAMETER);
360 }
361
362 /* Obtain the SP descriptor and update its runtime state. */
363 sp = spmc_get_sp_ctx(ffa_endpoint_source(x1));
364 if (sp == NULL) {
365 VERBOSE("Direct response to unknown partition ID (0x%x).\n",
366 dst_id);
367 return spmc_ffa_error_return(handle,
368 FFA_ERROR_INVALID_PARAMETER);
369 }
370
371 /* Sanity check state is being tracked correctly in the SPMC. */
372 idx = get_ec_index(sp);
373 assert(sp->ec[idx].rt_state == RT_STATE_RUNNING);
374
375 /* Ensure SP execution context was in the right runtime model. */
376 if (sp->ec[idx].rt_model != RT_MODEL_DIR_REQ) {
377 VERBOSE("SP context on core%u not handling direct req (%u).\n",
378 idx, sp->ec[idx].rt_model);
379 return spmc_ffa_error_return(handle, FFA_ERROR_DENIED);
380 }
381
382 /* Update the state of the SP execution context. */
383 sp->ec[idx].rt_state = RT_STATE_WAITING;
384
385 /*
386 * If the receiver is not the SPMC then forward the response to the
387 * Normal world.
388 */
389 if (dst_id == FFA_SPMC_ID) {
390 spmc_sp_synchronous_exit(&sp->ec[idx], x4);
391 /* Should not get here. */
392 panic();
393 }
394
395 return spmc_smc_return(smc_fid, secure_origin, x1, x2, x3, x4,
396 handle, cookie, flags, dst_id);
397}
398
Marc Bonnici8eb15202021-11-29 17:05:33 +0000399/*******************************************************************************
400 * This function handles the FFA_MSG_WAIT SMC to allow an SP to relinquish its
401 * cycles.
402 ******************************************************************************/
403static uint64_t msg_wait_handler(uint32_t smc_fid,
404 bool secure_origin,
405 uint64_t x1,
406 uint64_t x2,
407 uint64_t x3,
408 uint64_t x4,
409 void *cookie,
410 void *handle,
411 uint64_t flags)
412{
413 struct secure_partition_desc *sp;
414 unsigned int idx;
415
416 /*
417 * Check that the response did not originate from the Normal world as
418 * only the secure world can call this ABI.
419 */
420 if (!secure_origin) {
421 VERBOSE("Normal world cannot call FFA_MSG_WAIT.\n");
422 return spmc_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
423 }
424
425 /* Get the descriptor of the SP that invoked FFA_MSG_WAIT. */
426 sp = spmc_get_current_sp_ctx();
427 if (sp == NULL) {
428 return spmc_ffa_error_return(handle,
429 FFA_ERROR_INVALID_PARAMETER);
430 }
431
432 /*
433 * Get the execution context of the SP that invoked FFA_MSG_WAIT.
434 */
435 idx = get_ec_index(sp);
436
437 /* Ensure SP execution context was in the right runtime model. */
438 if (sp->ec[idx].rt_model == RT_MODEL_DIR_REQ) {
439 return spmc_ffa_error_return(handle, FFA_ERROR_DENIED);
440 }
441
442 /* Sanity check the state is being tracked correctly in the SPMC. */
443 assert(sp->ec[idx].rt_state == RT_STATE_RUNNING);
444
445 /*
446 * Perform a synchronous exit if the partition was initialising. The
447 * state is updated after the exit.
448 */
449 if (sp->ec[idx].rt_model == RT_MODEL_INIT) {
450 spmc_sp_synchronous_exit(&sp->ec[idx], x4);
451 /* Should not get here */
452 panic();
453 }
454
455 /* Update the state of the SP execution context. */
456 sp->ec[idx].rt_state = RT_STATE_WAITING;
457
458 /* Resume normal world if a secure interrupt was handled. */
459 if (sp->ec[idx].rt_model == RT_MODEL_INTR) {
460 /* FFA_MSG_WAIT can only be called from the secure world. */
461 unsigned int secure_state_in = SECURE;
462 unsigned int secure_state_out = NON_SECURE;
463
464 cm_el1_sysregs_context_save(secure_state_in);
465 cm_el1_sysregs_context_restore(secure_state_out);
466 cm_set_next_eret_context(secure_state_out);
467 SMC_RET0(cm_get_context(secure_state_out));
468 }
469
470 /* Forward the response to the Normal world. */
471 return spmc_smc_return(smc_fid, secure_origin, x1, x2, x3, x4,
472 handle, cookie, flags, FFA_NWD_ID);
473}
474
Marc Bonnicib4e99842021-12-10 09:21:56 +0000475static uint64_t ffa_error_handler(uint32_t smc_fid,
476 bool secure_origin,
477 uint64_t x1,
478 uint64_t x2,
479 uint64_t x3,
480 uint64_t x4,
481 void *cookie,
482 void *handle,
483 uint64_t flags)
484{
485 struct secure_partition_desc *sp;
486 unsigned int idx;
487
488 /* Check that the response did not originate from the Normal world. */
489 if (!secure_origin) {
490 return spmc_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
491 }
492
493 /* Get the descriptor of the SP that invoked FFA_ERROR. */
494 sp = spmc_get_current_sp_ctx();
495 if (sp == NULL) {
496 return spmc_ffa_error_return(handle,
497 FFA_ERROR_INVALID_PARAMETER);
498 }
499
500 /* Get the execution context of the SP that invoked FFA_ERROR. */
501 idx = get_ec_index(sp);
502
503 /*
504 * We only expect FFA_ERROR to be received during SP initialisation
505 * otherwise this is an invalid call.
506 */
507 if (sp->ec[idx].rt_model == RT_MODEL_INIT) {
508 ERROR("SP 0x%x failed to initialize.\n", sp->sp_id);
509 spmc_sp_synchronous_exit(&sp->ec[idx], x2);
510 /* Should not get here. */
511 panic();
512 }
513
514 return spmc_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
515}
516
Marc Bonnicie95eb7c2021-12-08 14:24:03 +0000517static uint64_t ffa_version_handler(uint32_t smc_fid,
518 bool secure_origin,
519 uint64_t x1,
520 uint64_t x2,
521 uint64_t x3,
522 uint64_t x4,
523 void *cookie,
524 void *handle,
525 uint64_t flags)
526{
527 uint32_t requested_version = x1 & FFA_VERSION_MASK;
528
529 if (requested_version & FFA_VERSION_BIT31_MASK) {
530 /* Invalid encoding, return an error. */
531 SMC_RET1(handle, FFA_ERROR_NOT_SUPPORTED);
532 /* Execution stops here. */
533 }
534
535 /* Determine the caller to store the requested version. */
536 if (secure_origin) {
537 /*
538 * Ensure that the SP is reporting the same version as
539 * specified in its manifest. If these do not match there is
540 * something wrong with the SP.
541 * TODO: Should we abort the SP? For now assert this is not
542 * case.
543 */
544 assert(requested_version ==
545 spmc_get_current_sp_ctx()->ffa_version);
546 } else {
547 /*
548 * If this is called by the normal world, record this
549 * information in its descriptor.
550 */
551 spmc_get_hyp_ctx()->ffa_version = requested_version;
552 }
553
554 SMC_RET1(handle, MAKE_FFA_VERSION(FFA_VERSION_MAJOR,
555 FFA_VERSION_MINOR));
556}
557
Marc Bonnici8eb15202021-11-29 17:05:33 +0000558/*******************************************************************************
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000559 * This function will parse the Secure Partition Manifest. From manifest, it
560 * will fetch details for preparing Secure partition image context and secure
561 * partition image boot arguments if any.
562 ******************************************************************************/
563static int sp_manifest_parse(void *sp_manifest, int offset,
564 struct secure_partition_desc *sp,
565 entry_point_info_t *ep_info)
566{
567 int32_t ret, node;
568 uint32_t config_32;
569
570 /*
571 * Look for the mandatory fields that are expected to be present in
572 * the SP manifests.
573 */
574 node = fdt_path_offset(sp_manifest, "/");
575 if (node < 0) {
576 ERROR("Did not find root node.\n");
577 return node;
578 }
579
Marc Bonnici21e644d2021-08-24 11:31:52 +0100580 ret = fdt_read_uint32_array(sp_manifest, node, "uuid",
581 ARRAY_SIZE(sp->uuid), sp->uuid);
582 if (ret != 0) {
583 ERROR("Missing Secure Partition UUID.\n");
584 return ret;
585 }
586
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000587 ret = fdt_read_uint32(sp_manifest, node, "exception-level", &config_32);
588 if (ret != 0) {
589 ERROR("Missing SP Exception Level information.\n");
590 return ret;
591 }
592
593 sp->runtime_el = config_32;
594
595 ret = fdt_read_uint32(sp_manifest, node, "ffa-version", &config_32);
596 if (ret != 0) {
597 ERROR("Missing Secure Partition FF-A Version.\n");
598 return ret;
599 }
600
601 sp->ffa_version = config_32;
602
603 ret = fdt_read_uint32(sp_manifest, node, "execution-state", &config_32);
604 if (ret != 0) {
605 ERROR("Missing Secure Partition Execution State.\n");
606 return ret;
607 }
608
609 sp->execution_state = config_32;
610
Marc Bonnicieec0d042021-12-09 10:51:05 +0000611 ret = fdt_read_uint32(sp_manifest, node,
Marc Bonnicif5244892021-12-09 18:34:02 +0000612 "messaging-method", &config_32);
613 if (ret != 0) {
614 ERROR("Missing Secure Partition messaging method.\n");
615 return ret;
616 }
617
618 /* Validate this entry, we currently only support direct messaging. */
619 if ((config_32 & ~(FFA_PARTITION_DIRECT_REQ_RECV |
620 FFA_PARTITION_DIRECT_REQ_SEND)) != 0U) {
621 WARN("Invalid Secure Partition messaging method (0x%x)\n",
622 config_32);
623 return -EINVAL;
624 }
625
626 sp->properties = config_32;
627
628 ret = fdt_read_uint32(sp_manifest, node,
Marc Bonnicieec0d042021-12-09 10:51:05 +0000629 "execution-ctx-count", &config_32);
630
631 if (ret != 0) {
632 ERROR("Missing SP Execution Context Count.\n");
633 return ret;
634 }
635
636 /*
637 * Ensure this field is set correctly in the manifest however
638 * since this is currently a hardcoded value for S-EL1 partitions
639 * we don't need to save it here, just validate.
640 */
641 if (config_32 != PLATFORM_CORE_COUNT) {
642 ERROR("SP Execution Context Count (%u) must be %u.\n",
643 config_32, PLATFORM_CORE_COUNT);
644 return -EINVAL;
645 }
646
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000647 /*
648 * Look for the optional fields that are expected to be present in
649 * an SP manifest.
650 */
651 ret = fdt_read_uint32(sp_manifest, node, "id", &config_32);
652 if (ret != 0) {
653 WARN("Missing Secure Partition ID.\n");
654 } else {
655 if (!is_ffa_secure_id_valid(config_32)) {
656 ERROR("Invalid Secure Partition ID (0x%x).\n",
657 config_32);
658 return -EINVAL;
659 }
660 sp->sp_id = config_32;
661 }
662
663 return 0;
664}
665
666/*******************************************************************************
667 * This function gets the Secure Partition Manifest base and maps the manifest
668 * region.
669 * Currently only one Secure Partition manifest is considered which is used to
670 * prepare the context for the single Secure Partition.
671 ******************************************************************************/
672static int find_and_prepare_sp_context(void)
673{
674 void *sp_manifest;
675 uintptr_t manifest_base;
676 uintptr_t manifest_base_align;
677 entry_point_info_t *next_image_ep_info;
678 int32_t ret;
679 struct secure_partition_desc *sp;
680
681 next_image_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
682 if (next_image_ep_info == NULL) {
683 WARN("No Secure Partition image provided by BL2.\n");
684 return -ENOENT;
685 }
686
687 sp_manifest = (void *)next_image_ep_info->args.arg0;
688 if (sp_manifest == NULL) {
689 WARN("Secure Partition manifest absent.\n");
690 return -ENOENT;
691 }
692
693 manifest_base = (uintptr_t)sp_manifest;
694 manifest_base_align = page_align(manifest_base, DOWN);
695
696 /*
697 * Map the secure partition manifest region in the EL3 translation
698 * regime.
699 * Map an area equal to (2 * PAGE_SIZE) for now. During manifest base
700 * alignment the region of 1 PAGE_SIZE from manifest align base may
701 * not completely accommodate the secure partition manifest region.
702 */
703 ret = mmap_add_dynamic_region((unsigned long long)manifest_base_align,
704 manifest_base_align,
705 PAGE_SIZE * 2,
706 MT_RO_DATA);
707 if (ret != 0) {
708 ERROR("Error while mapping SP manifest (%d).\n", ret);
709 return ret;
710 }
711
712 ret = fdt_node_offset_by_compatible(sp_manifest, -1,
713 "arm,ffa-manifest-1.0");
714 if (ret < 0) {
715 ERROR("Error happened in SP manifest reading.\n");
716 return -EINVAL;
717 }
718
719 /*
720 * Store the size of the manifest so that it can be used later to pass
721 * the manifest as boot information later.
722 */
723 next_image_ep_info->args.arg1 = fdt_totalsize(sp_manifest);
724 INFO("Manifest size = %lu bytes.\n", next_image_ep_info->args.arg1);
725
726 /*
727 * Select an SP descriptor for initialising the partition's execution
728 * context on the primary CPU.
729 */
730 sp = spmc_get_current_sp_ctx();
731
732 /* Initialize entry point information for the SP */
733 SET_PARAM_HEAD(next_image_ep_info, PARAM_EP, VERSION_1,
734 SECURE | EP_ST_ENABLE);
735
736 /* Parse the SP manifest. */
737 ret = sp_manifest_parse(sp_manifest, ret, sp, next_image_ep_info);
738 if (ret != 0) {
739 ERROR("Error in Secure Partition manifest parsing.\n");
740 return ret;
741 }
742
743 /* Check that the runtime EL in the manifest was correct. */
744 if (sp->runtime_el != S_EL1) {
745 ERROR("Unexpected runtime EL: %d\n", sp->runtime_el);
746 return -EINVAL;
747 }
748
749 /* Perform any common initialisation. */
750 spmc_sp_common_setup(sp, next_image_ep_info);
751
752 /* Perform any initialisation specific to S-EL1 SPs. */
753 spmc_el1_sp_setup(sp, next_image_ep_info);
754
755 /* Initialize the SP context with the required ep info. */
756 spmc_sp_common_ep_commit(sp, next_image_ep_info);
757
758 return 0;
759}
760
761/*******************************************************************************
762 * This function takes an SP context pointer and performs a synchronous entry
763 * into it.
764 ******************************************************************************/
Marc Bonnici9a297042022-02-14 17:06:09 +0000765static int32_t logical_sp_init(void)
766{
767 int32_t rc = 0;
768 struct el3_lp_desc *el3_lp_descs;
769
770 /* Perform initial validation of the Logical Partitions. */
771 rc = el3_sp_desc_validate();
772 if (rc != 0) {
773 ERROR("Logical Partition validation failed!\n");
774 return rc;
775 }
776
777 el3_lp_descs = get_el3_lp_array();
778
779 INFO("Logical Secure Partition init start.\n");
780 for (unsigned int i = 0U; i < EL3_LP_DESCS_COUNT; i++) {
781 rc = el3_lp_descs[i].init();
782 if (rc != 0) {
783 ERROR("Logical SP (0x%x) Failed to Initialize\n",
784 el3_lp_descs[i].sp_id);
785 return rc;
786 }
787 VERBOSE("Logical SP (0x%x) Initialized\n",
788 el3_lp_descs[i].sp_id);
789 }
790
791 INFO("Logical Secure Partition init completed.\n");
792
793 return rc;
794}
795
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000796uint64_t spmc_sp_synchronous_entry(struct sp_exec_ctx *ec)
797{
798 uint64_t rc;
799
800 assert(ec != NULL);
801
802 /* Assign the context of the SP to this CPU */
803 cm_set_context(&(ec->cpu_ctx), SECURE);
804
805 /* Restore the context assigned above */
806 cm_el1_sysregs_context_restore(SECURE);
807 cm_set_next_eret_context(SECURE);
808
809 /* Invalidate TLBs at EL1. */
810 tlbivmalle1();
811 dsbish();
812
813 /* Enter Secure Partition */
814 rc = spm_secure_partition_enter(&ec->c_rt_ctx);
815
816 /* Save secure state */
817 cm_el1_sysregs_context_save(SECURE);
818
819 return rc;
820}
821
822/*******************************************************************************
823 * SPMC Helper Functions.
824 ******************************************************************************/
825static int32_t sp_init(void)
826{
827 uint64_t rc;
828 struct secure_partition_desc *sp;
829 struct sp_exec_ctx *ec;
830
831 sp = spmc_get_current_sp_ctx();
832 ec = spmc_get_sp_ec(sp);
833 ec->rt_model = RT_MODEL_INIT;
834 ec->rt_state = RT_STATE_RUNNING;
835
836 INFO("Secure Partition (0x%x) init start.\n", sp->sp_id);
837
838 rc = spmc_sp_synchronous_entry(ec);
839 if (rc != 0) {
840 /* Indicate SP init was not successful. */
841 ERROR("SP (0x%x) failed to initialize (%lu).\n",
842 sp->sp_id, rc);
843 return 0;
844 }
845
846 ec->rt_state = RT_STATE_WAITING;
847 INFO("Secure Partition initialized.\n");
848
849 return 1;
850}
851
852static void initalize_sp_descs(void)
853{
854 struct secure_partition_desc *sp;
855
856 for (unsigned int i = 0U; i < SECURE_PARTITION_COUNT; i++) {
857 sp = &sp_desc[i];
858 sp->sp_id = INV_SP_ID;
Marc Bonniciecc460a2021-09-02 13:18:41 +0100859 sp->mailbox.rx_buffer = NULL;
860 sp->mailbox.tx_buffer = NULL;
861 sp->mailbox.state = MAILBOX_STATE_EMPTY;
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000862 sp->secondary_ep = 0;
863 }
864}
865
866static void initalize_ns_ep_descs(void)
867{
868 struct ns_endpoint_desc *ns_ep;
869
870 for (unsigned int i = 0U; i < NS_PARTITION_COUNT; i++) {
871 ns_ep = &ns_ep_desc[i];
872 /*
873 * Clashes with the Hypervisor ID but will not be a
874 * problem in practice.
875 */
876 ns_ep->ns_ep_id = 0;
877 ns_ep->ffa_version = 0;
Marc Bonniciecc460a2021-09-02 13:18:41 +0100878 ns_ep->mailbox.rx_buffer = NULL;
879 ns_ep->mailbox.tx_buffer = NULL;
880 ns_ep->mailbox.state = MAILBOX_STATE_EMPTY;
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000881 }
882}
883
884/*******************************************************************************
Marc Bonnici1c33cc32021-11-29 17:57:03 +0000885 * Initialize SPMC attributes for the SPMD.
886 ******************************************************************************/
887void spmc_populate_attrs(spmc_manifest_attribute_t *spmc_attrs)
888{
889 spmc_attrs->major_version = FFA_VERSION_MAJOR;
890 spmc_attrs->minor_version = FFA_VERSION_MINOR;
891 spmc_attrs->exec_state = MODE_RW_64;
892 spmc_attrs->spmc_id = FFA_SPMC_ID;
893}
894
895/*******************************************************************************
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000896 * Initialize contexts of all Secure Partitions.
897 ******************************************************************************/
898int32_t spmc_setup(void)
899{
900 int32_t ret;
901
902 /* Initialize endpoint descriptors */
903 initalize_sp_descs();
904 initalize_ns_ep_descs();
905
Marc Bonnici9a297042022-02-14 17:06:09 +0000906 /* Setup logical SPs. */
907 ret = logical_sp_init();
908 if (ret != 0) {
909 ERROR("Failed to initialize Logical Partitions.\n");
910 return ret;
911 }
912
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000913 /* Perform physical SP setup. */
914
915 /* Disable MMU at EL1 (initialized by BL2) */
916 disable_mmu_icache_el1();
917
918 /* Initialize context of the SP */
919 INFO("Secure Partition context setup start.\n");
920
921 ret = find_and_prepare_sp_context();
922 if (ret != 0) {
923 ERROR("Error in SP finding and context preparation.\n");
924 return ret;
925 }
926
927 /* Register init function for deferred init. */
928 bl31_register_bl32_init(&sp_init);
929
930 INFO("Secure Partition setup done.\n");
931
932 return 0;
933}
934
935/*******************************************************************************
936 * Secure Partition Manager SMC handler.
937 ******************************************************************************/
938uint64_t spmc_smc_handler(uint32_t smc_fid,
939 bool secure_origin,
940 uint64_t x1,
941 uint64_t x2,
942 uint64_t x3,
943 uint64_t x4,
944 void *cookie,
945 void *handle,
946 uint64_t flags)
947{
948 switch (smc_fid) {
949
Marc Bonnicie95eb7c2021-12-08 14:24:03 +0000950 case FFA_VERSION:
951 return ffa_version_handler(smc_fid, secure_origin, x1, x2, x3,
952 x4, cookie, handle, flags);
953
Marc Bonnici5eeacd52021-11-29 17:05:57 +0000954 case FFA_MSG_SEND_DIRECT_REQ_SMC32:
955 case FFA_MSG_SEND_DIRECT_REQ_SMC64:
956 return direct_req_smc_handler(smc_fid, secure_origin, x1, x2,
957 x3, x4, cookie, handle, flags);
958
959 case FFA_MSG_SEND_DIRECT_RESP_SMC32:
960 case FFA_MSG_SEND_DIRECT_RESP_SMC64:
961 return direct_resp_smc_handler(smc_fid, secure_origin, x1, x2,
962 x3, x4, cookie, handle, flags);
963
Marc Bonnici8eb15202021-11-29 17:05:33 +0000964 case FFA_MSG_WAIT:
965 return msg_wait_handler(smc_fid, secure_origin, x1, x2, x3, x4,
966 cookie, handle, flags);
967
Marc Bonnicib4e99842021-12-10 09:21:56 +0000968 case FFA_ERROR:
969 return ffa_error_handler(smc_fid, secure_origin, x1, x2, x3, x4,
970 cookie, handle, flags);
971
Marc Bonnici8e1a7552021-12-01 17:57:04 +0000972 default:
973 WARN("Unsupported FF-A call 0x%08x.\n", smc_fid);
974 break;
975 }
976 return spmc_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
977}