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" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 12 | #include "spm_ipc.h" |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 13 | #include "tfm_hal_isolation.h" |
Kevin Peng | b288c52 | 2021-09-26 16:18:23 +0800 | [diff] [blame] | 14 | #include "tfm_hal_platform.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 15 | #include "tfm_rpc.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 16 | #include "ffm/backend.h" |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 17 | #include "utilities.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 18 | #include "load/partition_defs.h" |
| 19 | #include "load/service_defs.h" |
| 20 | #include "load/spm_load_api.h" |
| 21 | #include "psa/error.h" |
| 22 | |
| 23 | /* Declare the global component list */ |
| 24 | struct partition_head_t partition_listhead; |
| 25 | |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 26 | #if CONFIG_TFM_PSA_API_CROSS_CALL == 1 |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 27 | |
| 28 | #ifdef TFM_MULTI_CORE_TOPOLOGY |
| 29 | /* TODO: To be checked when RPC design updates. */ |
| 30 | static uint8_t spm_stack_local[CONFIG_TFM_SPM_THREAD_STACK_SIZE] __aligned(8); |
| 31 | struct context_ctrl_t spm_thread_context = { |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame^] | 32 | .sp = (uint32_t)&spm_stack_local[CONFIG_TFM_SPM_THREAD_STACK_SIZE], |
| 33 | .sp_limit = (uint32_t)spm_stack_local, |
| 34 | .allocated = 0, |
| 35 | .exc_ret = 0, |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 36 | }; |
| 37 | struct context_ctrl_t *p_spm_thread_context = &spm_thread_context; |
| 38 | #else |
| 39 | struct context_ctrl_t *p_spm_thread_context; |
| 40 | #endif |
| 41 | |
| 42 | #endif |
| 43 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 44 | /* |
| 45 | * 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] | 46 | * current thread and trigger scheduler. |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 47 | */ |
| 48 | static psa_status_t ipc_messaging(struct service_t *service, |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 49 | struct conn_handle_t *hdl) |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 50 | { |
| 51 | struct partition_t *p_owner = NULL; |
| 52 | psa_signal_t signal = 0; |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 53 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 54 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 55 | if (!hdl || !service || !service->p_ldinf || !service->partition) { |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 56 | return PSA_ERROR_PROGRAMMER_ERROR; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | p_owner = service->partition; |
| 60 | signal = service->p_ldinf->signal; |
| 61 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 62 | CRITICAL_SECTION_ENTER(cs_assert); |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 63 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 64 | UNI_LIST_INSERT_AFTER(p_owner, hdl, p_handles); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 65 | |
| 66 | /* Messages put. Update signals */ |
| 67 | p_owner->signals_asserted |= signal; |
| 68 | |
| 69 | if (p_owner->signals_waiting & signal) { |
| 70 | thrd_wake_up(&p_owner->waitobj, |
| 71 | (p_owner->signals_asserted & p_owner->signals_waiting)); |
| 72 | p_owner->signals_waiting &= ~signal; |
| 73 | } |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 74 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * If it is a NS request via RPC, it is unnecessary to block current |
| 78 | * thread. |
| 79 | */ |
| 80 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 81 | if (!is_tfm_rpc_msg(hdl)) { |
| 82 | thrd_wait_on(&hdl->ack_evnt, CURRENT_THREAD); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return PSA_SUCCESS; |
| 86 | } |
| 87 | |
Ken Liu | f8c7e53 | 2022-02-10 15:03:04 +0800 | [diff] [blame] | 88 | static psa_status_t ipc_replying(struct conn_handle_t *hdl, int32_t status) |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 89 | { |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 90 | if (is_tfm_rpc_msg(hdl)) { |
| 91 | tfm_rpc_client_call_reply(hdl, status); |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 92 | } else { |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 93 | thrd_wake_up(&hdl->ack_evnt, status); |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 94 | } |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * 'psa_reply' exists in IPC model only and returns 'void'. Return |
| 98 | * 'PSA_SUCCESS' here always since SPM does not forward the status |
| 99 | * to the caller. |
| 100 | */ |
| 101 | return PSA_SUCCESS; |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 102 | } |
| 103 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 104 | /* Parameters are treated as assuredly */ |
| 105 | static void ipc_comp_init_assuredly(struct partition_t *p_pt, |
| 106 | uint32_t service_setting) |
| 107 | { |
| 108 | const struct partition_load_info_t *p_pldi = p_pt->p_ldinf; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 109 | |
| 110 | p_pt->signals_allowed |= PSA_DOORBELL | service_setting; |
| 111 | |
| 112 | THRD_SYNC_INIT(&p_pt->waitobj); |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 113 | UNI_LISI_INIT_NODE(p_pt, p_handles); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 114 | |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame^] | 115 | ARCH_CTXCTRL_INIT(&p_pt->ctx_ctrl, |
| 116 | LOAD_ALLOCED_STACK_ADDR(p_pldi), |
| 117 | p_pldi->stack_size); |
| 118 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 119 | THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl, |
| 120 | TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags))); |
| 121 | |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 122 | #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] | 123 | if (p_pldi->pid == TFM_SP_NON_SECURE_ID) { |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 124 | SPM_THREAD_CONTEXT = &p_pt->ctx_ctrl; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 125 | } |
Summer Qin | 9544482 | 2022-01-27 11:22:00 +0800 | [diff] [blame] | 126 | #endif |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 127 | |
| 128 | thrd_start(&p_pt->thrd, |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame^] | 129 | POSITION_TO_ENTRY(p_pldi->entry, thrd_fn_t), |
| 130 | THRD_GENERAL_EXIT); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static uint32_t ipc_system_run(void) |
| 134 | { |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 135 | uint32_t control; |
| 136 | struct partition_t *p_cur_pt; |
| 137 | |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 138 | #if CONFIG_TFM_PSA_API_THREAD_CALL == 1 |
Chris Brand | 3778bc1 | 2021-12-15 17:01:05 -0800 | [diff] [blame] | 139 | TFM_CORE_ASSERT(SPM_THREAD_CONTEXT); |
| 140 | #endif |
| 141 | |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 142 | control = thrd_start_scheduler(&CURRENT_THREAD); |
| 143 | |
| 144 | p_cur_pt = TO_CONTAINER(CURRENT_THREAD->p_context_ctrl, |
| 145 | struct partition_t, ctx_ctrl); |
| 146 | |
| 147 | if (tfm_hal_update_boundaries(p_cur_pt->p_ldinf, p_cur_pt->p_boundaries) |
| 148 | != TFM_HAL_SUCCESS) { |
| 149 | tfm_core_panic(); |
| 150 | } |
| 151 | |
| 152 | return control; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | const struct backend_ops_t backend_instance = { |
| 156 | .comp_init_assuredly = ipc_comp_init_assuredly, |
| 157 | .system_run = ipc_system_run, |
| 158 | .messaging = ipc_messaging, |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 159 | .replying = ipc_replying, |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 160 | }; |