blob: cd35e36e9cc26f27fb0a8edcbfc073f527e764b3 [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>
Ken Liue07c3b72021-10-14 16:19:13 +08009#include "compiler_ext_defs.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080010#include "spm_ipc.h"
Ken Liu62bae592021-10-19 22:15:43 +080011#include "tfm_hal_isolation.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080012#include "tfm_rpc.h"
13#include "tfm_spm_hal.h" /* To be checked */
14#include "ffm/backend.h"
Ken Liu62bae592021-10-19 22:15:43 +080015#include "utilities.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080016#include "load/partition_defs.h"
17#include "load/service_defs.h"
18#include "load/spm_load_api.h"
19#include "psa/error.h"
20
21/* Declare the global component list */
22struct partition_head_t partition_listhead;
23
Ken Liue07c3b72021-10-14 16:19:13 +080024#ifdef CONFIG_TFM_PSA_API_THREAD_CALL
25
26#ifdef TFM_MULTI_CORE_TOPOLOGY
27/* TODO: To be checked when RPC design updates. */
28static uint8_t spm_stack_local[CONFIG_TFM_SPM_THREAD_STACK_SIZE] __aligned(8);
29struct context_ctrl_t spm_thread_context = {
30 .sp = (uint32_t)&spm_stack_local[CONFIG_TFM_SPM_THREAD_STACK_SIZE],
31 .sp_limit = (uint32_t)spm_stack_local,
32 .reserved = 0,
33 .exc_ret = 0,
34};
35struct context_ctrl_t *p_spm_thread_context = &spm_thread_context;
36#else
37struct context_ctrl_t *p_spm_thread_context;
38#endif
39
40#endif
41
Mingyang Sundeae45d2021-09-06 15:31:07 +080042/*
43 * Send message and wake up the SP who is waiting on message queue, block the
44 * current thread and triggere scheduler.
45 */
46static psa_status_t ipc_messaging(struct service_t *service,
47 struct tfm_msg_body_t *msg)
48{
49 struct partition_t *p_owner = NULL;
50 psa_signal_t signal = 0;
51
52 if (!msg || !service || !service->p_ldinf || !service->partition) {
53 tfm_core_panic();
54 }
55
56 p_owner = service->partition;
57 signal = service->p_ldinf->signal;
58
59 /* Add message to partition message list tail */
60 BI_LIST_INSERT_BEFORE(&p_owner->msg_list, &msg->msg_node);
61
62 /* Messages put. Update signals */
63 p_owner->signals_asserted |= signal;
64
65 if (p_owner->signals_waiting & signal) {
66 thrd_wake_up(&p_owner->waitobj,
67 (p_owner->signals_asserted & p_owner->signals_waiting));
68 p_owner->signals_waiting &= ~signal;
69 }
70
71 /*
72 * If it is a NS request via RPC, it is unnecessary to block current
73 * thread.
74 */
75
76 if (!is_tfm_rpc_msg(msg)) {
77 thrd_wait_on(&msg->ack_evnt, CURRENT_THREAD);
78 }
79
80 return PSA_SUCCESS;
81}
82
83/* Parameters are treated as assuredly */
84static void ipc_comp_init_assuredly(struct partition_t *p_pt,
85 uint32_t service_setting)
86{
87 const struct partition_load_info_t *p_pldi = p_pt->p_ldinf;
88 void *p_param = NULL;
89
90 p_pt->signals_allowed |= PSA_DOORBELL | service_setting;
91
92 THRD_SYNC_INIT(&p_pt->waitobj);
93 BI_LIST_INIT_NODE(&p_pt->msg_list);
94
95 THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl,
96 TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags)));
97
98 if (p_pldi->pid == TFM_SP_NON_SECURE_ID) {
99 p_param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liue07c3b72021-10-14 16:19:13 +0800100
101#ifdef CONFIG_TFM_PSA_API_THREAD_CALL
102 SPM_THREAD_CONTEXT = &p_pt->ctx_ctrl;
103#endif
104
Mingyang Sundeae45d2021-09-06 15:31:07 +0800105 }
106
107 thrd_start(&p_pt->thrd,
108 POSITION_TO_ENTRY(p_pldi->entry, thrd_fn_t), p_param,
109 LOAD_ALLOCED_STACK_ADDR(p_pldi),
110 LOAD_ALLOCED_STACK_ADDR(p_pldi) + p_pldi->stack_size);
111}
112
113static uint32_t ipc_system_run(void)
114{
Ken Liu62bae592021-10-19 22:15:43 +0800115 uint32_t control;
116 struct partition_t *p_cur_pt;
117
118 control = thrd_start_scheduler(&CURRENT_THREAD);
119
120 p_cur_pt = TO_CONTAINER(CURRENT_THREAD->p_context_ctrl,
121 struct partition_t, ctx_ctrl);
122
123 if (tfm_hal_update_boundaries(p_cur_pt->p_ldinf, p_cur_pt->p_boundaries)
124 != TFM_HAL_SUCCESS) {
125 tfm_core_panic();
126 }
127
128 return control;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800129}
130
131const struct backend_ops_t backend_instance = {
132 .comp_init_assuredly = ipc_comp_init_assuredly,
133 .system_run = ipc_system_run,
134 .messaging = ipc_messaging,
135};