Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 1 | /* |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 2 | * Copyright (c) 2021-2022, Arm Limited. All rights reserved. |
Chris Brand | 3778bc1 | 2021-12-15 17:01:05 -0800 | [diff] [blame] | 3 | * Copyright (c) 2021, Cypress Semiconductor Corporation. All rights reserved. |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: BSD-3-Clause |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <stdint.h> |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 10 | #include "critical_section.h" |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 11 | #include "compiler_ext_defs.h" |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 12 | #include "runtime_defs.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 13 | #include "spm_ipc.h" |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 14 | #include "tfm_hal_isolation.h" |
Kevin Peng | b288c52 | 2021-09-26 16:18:23 +0800 | [diff] [blame] | 15 | #include "tfm_hal_platform.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 16 | #include "tfm_rpc.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 17 | #include "ffm/backend.h" |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 18 | #include "utilities.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 19 | #include "load/partition_defs.h" |
| 20 | #include "load/service_defs.h" |
| 21 | #include "load/spm_load_api.h" |
| 22 | #include "psa/error.h" |
| 23 | |
| 24 | /* Declare the global component list */ |
| 25 | struct partition_head_t partition_listhead; |
| 26 | |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 27 | #if CONFIG_TFM_PSA_API_CROSS_CALL == 1 |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 28 | |
| 29 | #ifdef TFM_MULTI_CORE_TOPOLOGY |
| 30 | /* TODO: To be checked when RPC design updates. */ |
| 31 | static uint8_t spm_stack_local[CONFIG_TFM_SPM_THREAD_STACK_SIZE] __aligned(8); |
| 32 | struct context_ctrl_t spm_thread_context = { |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 33 | .sp = (uint32_t)&spm_stack_local[CONFIG_TFM_SPM_THREAD_STACK_SIZE], |
| 34 | .sp_limit = (uint32_t)spm_stack_local, |
| 35 | .allocated = 0, |
| 36 | .exc_ret = 0, |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 37 | }; |
| 38 | struct context_ctrl_t *p_spm_thread_context = &spm_thread_context; |
| 39 | #else |
| 40 | struct context_ctrl_t *p_spm_thread_context; |
| 41 | #endif |
| 42 | |
| 43 | #endif |
| 44 | |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 45 | static void prv_process_metadata(struct partition_t *p_pt) |
| 46 | { |
Kevin Peng | 43160d5 | 2022-02-11 13:35:56 +0800 | [diff] [blame] | 47 | const struct partition_load_info_t *p_pt_ldi; |
| 48 | const struct service_load_info_t *p_srv_ldi; |
| 49 | struct context_ctrl_t *ctx_ctrl; |
| 50 | struct runtime_metadata_t *p_rt_meta; |
| 51 | service_fn_t *p_sfn_table; |
| 52 | uint32_t allocate_size; |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 53 | |
Kevin Peng | 43160d5 | 2022-02-11 13:35:56 +0800 | [diff] [blame] | 54 | p_pt_ldi = p_pt->p_ldinf; |
| 55 | p_srv_ldi = (struct service_load_info_t *)LOAD_INFO_SERVICE(p_pt_ldi); |
| 56 | ctx_ctrl = &p_pt->ctx_ctrl; |
| 57 | |
| 58 | /* common runtime metadata */ |
| 59 | allocate_size = sizeof(*p_rt_meta); |
| 60 | |
| 61 | if (!IS_PARTITION_IPC_MODEL(p_pt_ldi)) { |
| 62 | /* SFN specific metadata - SFN function table */ |
| 63 | allocate_size += sizeof(service_fn_t) * p_pt_ldi->nservices; |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 64 | } |
| 65 | |
Kevin Peng | 43160d5 | 2022-02-11 13:35:56 +0800 | [diff] [blame] | 66 | ARCH_CTXCTRL_ALLOCATE_STACK(ctx_ctrl, allocate_size); |
| 67 | p_rt_meta = (struct runtime_metadata_t *) |
| 68 | ARCH_CTXCTRL_ALLOCATED_PTR(ctx_ctrl); |
| 69 | |
| 70 | p_rt_meta->entry = p_pt_ldi->entry; |
| 71 | p_rt_meta->n_sfn = 0; |
| 72 | p_sfn_table = p_rt_meta->sfn_table; |
| 73 | |
| 74 | if (!IS_PARTITION_IPC_MODEL(p_pt_ldi)) { |
| 75 | /* SFN table. The signal bit of the service is the same index of SFN. */ |
| 76 | for (int i = 0; i < p_pt_ldi->nservices; i++) { |
| 77 | p_sfn_table[i] = (service_fn_t)p_srv_ldi[i].sfn; |
| 78 | } |
| 79 | |
| 80 | p_rt_meta->n_sfn = p_pt_ldi->nservices; |
| 81 | } |
| 82 | |
| 83 | p_pt->p_metadata = (void *)p_rt_meta; |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 84 | } |
| 85 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 86 | /* |
| 87 | * Send message and wake up the SP who is waiting on message queue, block the |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 88 | * current thread and trigger scheduler. |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 89 | */ |
| 90 | static psa_status_t ipc_messaging(struct service_t *service, |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 91 | struct conn_handle_t *handle) |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 92 | { |
| 93 | struct partition_t *p_owner = NULL; |
| 94 | psa_signal_t signal = 0; |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 95 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 96 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 97 | if (!handle || !service || !service->p_ldinf || !service->partition) { |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 98 | return PSA_ERROR_PROGRAMMER_ERROR; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | p_owner = service->partition; |
| 102 | signal = service->p_ldinf->signal; |
| 103 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 104 | CRITICAL_SECTION_ENTER(cs_assert); |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 105 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 106 | UNI_LIST_INSERT_AFTER(p_owner, handle, p_handles); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 107 | |
| 108 | /* Messages put. Update signals */ |
| 109 | p_owner->signals_asserted |= signal; |
| 110 | |
| 111 | if (p_owner->signals_waiting & signal) { |
| 112 | thrd_wake_up(&p_owner->waitobj, |
| 113 | (p_owner->signals_asserted & p_owner->signals_waiting)); |
| 114 | p_owner->signals_waiting &= ~signal; |
| 115 | } |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 116 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * If it is a NS request via RPC, it is unnecessary to block current |
| 120 | * thread. |
| 121 | */ |
| 122 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 123 | if (!is_tfm_rpc_msg(handle)) { |
Mingyang Sun | ac1114e | 2022-03-23 17:32:07 +0800 | [diff] [blame] | 124 | thrd_set_wait(&handle->ack_evnt, CURRENT_THREAD); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 125 | } |
| 126 | |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame^] | 127 | handle->status = TFM_HANDLE_STATUS_ACTIVE; |
| 128 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 129 | return PSA_SUCCESS; |
| 130 | } |
| 131 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 132 | static psa_status_t ipc_replying(struct conn_handle_t *handle, int32_t status) |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 133 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 134 | if (is_tfm_rpc_msg(handle)) { |
| 135 | tfm_rpc_client_call_reply(handle, status); |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 136 | } else { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 137 | thrd_wake_up(&handle->ack_evnt, status); |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 138 | } |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * 'psa_reply' exists in IPC model only and returns 'void'. Return |
| 142 | * 'PSA_SUCCESS' here always since SPM does not forward the status |
| 143 | * to the caller. |
| 144 | */ |
| 145 | return PSA_SUCCESS; |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 146 | } |
| 147 | |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 148 | extern void sprt_main(void); |
| 149 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 150 | /* Parameters are treated as assuredly */ |
| 151 | static void ipc_comp_init_assuredly(struct partition_t *p_pt, |
| 152 | uint32_t service_setting) |
| 153 | { |
| 154 | const struct partition_load_info_t *p_pldi = p_pt->p_ldinf; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 155 | |
Kevin Peng | 613b417 | 2022-02-15 14:41:44 +0800 | [diff] [blame] | 156 | #if CONFIG_TFM_DOORBELL_API == 1 |
| 157 | p_pt->signals_allowed |= PSA_DOORBELL; |
| 158 | #endif /* CONFIG_TFM_DOORBELL_API == 1 */ |
| 159 | |
| 160 | p_pt->signals_allowed |= service_setting; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 161 | |
| 162 | THRD_SYNC_INIT(&p_pt->waitobj); |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 163 | UNI_LISI_INIT_NODE(p_pt, p_handles); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 164 | |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 165 | ARCH_CTXCTRL_INIT(&p_pt->ctx_ctrl, |
| 166 | LOAD_ALLOCED_STACK_ADDR(p_pldi), |
| 167 | p_pldi->stack_size); |
| 168 | |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 169 | prv_process_metadata(p_pt); |
| 170 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 171 | THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl, |
| 172 | TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags))); |
| 173 | |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 174 | #if (CONFIG_TFM_PSA_API_CROSS_CALL == 1) && !defined(TFM_MULTI_CORE_TOPOLOGY) |
Ken Liu | 897e8f1 | 2022-02-10 03:21:17 +0100 | [diff] [blame] | 175 | if (p_pldi->pid == TFM_SP_NON_SECURE_ID) { |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 176 | SPM_THREAD_CONTEXT = &p_pt->ctx_ctrl; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 177 | } |
Summer Qin | 9544482 | 2022-01-27 11:22:00 +0800 | [diff] [blame] | 178 | #endif |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 179 | |
| 180 | thrd_start(&p_pt->thrd, |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 181 | POSITION_TO_ENTRY(sprt_main, thrd_fn_t), |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 182 | THRD_GENERAL_EXIT); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static uint32_t ipc_system_run(void) |
| 186 | { |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 187 | uint32_t control; |
| 188 | struct partition_t *p_cur_pt; |
| 189 | |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 190 | #if CONFIG_TFM_PSA_API_THREAD_CALL == 1 |
Chris Brand | 3778bc1 | 2021-12-15 17:01:05 -0800 | [diff] [blame] | 191 | TFM_CORE_ASSERT(SPM_THREAD_CONTEXT); |
| 192 | #endif |
| 193 | |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 194 | control = thrd_start_scheduler(&CURRENT_THREAD); |
| 195 | |
| 196 | p_cur_pt = TO_CONTAINER(CURRENT_THREAD->p_context_ctrl, |
| 197 | struct partition_t, ctx_ctrl); |
| 198 | |
| 199 | if (tfm_hal_update_boundaries(p_cur_pt->p_ldinf, p_cur_pt->p_boundaries) |
| 200 | != TFM_HAL_SUCCESS) { |
| 201 | tfm_core_panic(); |
| 202 | } |
| 203 | |
| 204 | return control; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 205 | } |
| 206 | |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 207 | static psa_signal_t ipc_wait(struct partition_t *p_pt, psa_signal_t signal_mask) |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 208 | { |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 209 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
| 210 | psa_signal_t ret_signal; |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 211 | |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 212 | /* |
| 213 | * 'ipc_wait()' sets the waiting signal mask for partition, and |
| 214 | * blocks the partition thread state to wait for signals. |
| 215 | * These changes should be inside the ciritical section to avoid |
| 216 | * 'signal_waiting' or the thread state to be changed by interrupts |
| 217 | * while this function is reading or writing values. |
| 218 | */ |
| 219 | CRITICAL_SECTION_ENTER(cs_assert); |
| 220 | |
| 221 | ret_signal = p_pt->signals_asserted & signal_mask; |
| 222 | if (ret_signal == 0) { |
| 223 | p_pt->signals_waiting = signal_mask; |
Mingyang Sun | ac1114e | 2022-03-23 17:32:07 +0800 | [diff] [blame] | 224 | thrd_set_wait(&p_pt->waitobj, CURRENT_THREAD); |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 225 | } |
| 226 | CRITICAL_SECTION_LEAVE(cs_assert); |
| 227 | |
| 228 | return ret_signal; |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 229 | } |
| 230 | |
Mingyang Sun | 09328bd | 2022-03-25 11:55:13 +0800 | [diff] [blame] | 231 | static void ipc_wake_up(struct partition_t *p_pt) |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 232 | { |
| 233 | thrd_wake_up(&p_pt->waitobj, |
| 234 | p_pt->signals_asserted & p_pt->signals_waiting); |
Mingyang Sun | 09328bd | 2022-03-25 11:55:13 +0800 | [diff] [blame] | 235 | p_pt->signals_waiting = 0; |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 236 | } |
| 237 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 238 | const struct backend_ops_t backend_instance = { |
| 239 | .comp_init_assuredly = ipc_comp_init_assuredly, |
| 240 | .system_run = ipc_system_run, |
| 241 | .messaging = ipc_messaging, |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 242 | .replying = ipc_replying, |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 243 | .wait = ipc_wait, |
| 244 | .wake_up = ipc_wake_up, |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 245 | }; |