blob: 3b23e1e8180f83a1767f0b27e993fa5ee609c003 [file] [log] [blame]
Mingyang Sundeae45d2021-09-06 15:31:07 +08001/*
2 * Copyright (c) 2021, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include <stdint.h>
9#include "spm_ipc.h"
Ken Liu62bae592021-10-19 22:15:43 +080010#include "tfm_hal_isolation.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080011#include "tfm_rpc.h"
12#include "tfm_spm_hal.h" /* To be checked */
13#include "ffm/backend.h"
Ken Liu62bae592021-10-19 22:15:43 +080014#include "utilities.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080015#include "load/partition_defs.h"
16#include "load/service_defs.h"
17#include "load/spm_load_api.h"
18#include "psa/error.h"
19
20/* Declare the global component list */
21struct partition_head_t partition_listhead;
22
23/*
24 * Send message and wake up the SP who is waiting on message queue, block the
25 * current thread and triggere scheduler.
26 */
27static psa_status_t ipc_messaging(struct service_t *service,
28 struct tfm_msg_body_t *msg)
29{
30 struct partition_t *p_owner = NULL;
31 psa_signal_t signal = 0;
32
33 if (!msg || !service || !service->p_ldinf || !service->partition) {
34 tfm_core_panic();
35 }
36
37 p_owner = service->partition;
38 signal = service->p_ldinf->signal;
39
40 /* Add message to partition message list tail */
41 BI_LIST_INSERT_BEFORE(&p_owner->msg_list, &msg->msg_node);
42
43 /* Messages put. Update signals */
44 p_owner->signals_asserted |= signal;
45
46 if (p_owner->signals_waiting & signal) {
47 thrd_wake_up(&p_owner->waitobj,
48 (p_owner->signals_asserted & p_owner->signals_waiting));
49 p_owner->signals_waiting &= ~signal;
50 }
51
52 /*
53 * If it is a NS request via RPC, it is unnecessary to block current
54 * thread.
55 */
56
57 if (!is_tfm_rpc_msg(msg)) {
58 thrd_wait_on(&msg->ack_evnt, CURRENT_THREAD);
59 }
60
61 return PSA_SUCCESS;
62}
63
64/* Parameters are treated as assuredly */
65static void ipc_comp_init_assuredly(struct partition_t *p_pt,
66 uint32_t service_setting)
67{
68 const struct partition_load_info_t *p_pldi = p_pt->p_ldinf;
69 void *p_param = NULL;
70
71 p_pt->signals_allowed |= PSA_DOORBELL | service_setting;
72
73 THRD_SYNC_INIT(&p_pt->waitobj);
74 BI_LIST_INIT_NODE(&p_pt->msg_list);
75
76 THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl,
77 TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags)));
78
79 if (p_pldi->pid == TFM_SP_NON_SECURE_ID) {
80 p_param = (void *)tfm_spm_hal_get_ns_entry_point();
81 }
82
83 thrd_start(&p_pt->thrd,
84 POSITION_TO_ENTRY(p_pldi->entry, thrd_fn_t), p_param,
85 LOAD_ALLOCED_STACK_ADDR(p_pldi),
86 LOAD_ALLOCED_STACK_ADDR(p_pldi) + p_pldi->stack_size);
87}
88
89static uint32_t ipc_system_run(void)
90{
Ken Liu62bae592021-10-19 22:15:43 +080091 uint32_t control;
92 struct partition_t *p_cur_pt;
93
94 control = thrd_start_scheduler(&CURRENT_THREAD);
95
96 p_cur_pt = TO_CONTAINER(CURRENT_THREAD->p_context_ctrl,
97 struct partition_t, ctx_ctrl);
98
99 if (tfm_hal_update_boundaries(p_cur_pt->p_ldinf, p_cur_pt->p_boundaries)
100 != TFM_HAL_SUCCESS) {
101 tfm_core_panic();
102 }
103
104 return control;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800105}
106
107const struct backend_ops_t backend_instance = {
108 .comp_init_assuredly = ipc_comp_init_assuredly,
109 .system_run = ipc_system_run,
110 .messaging = ipc_messaging,
111};