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 | |
Xinyu Zhang | a7ba50b | 2021-12-27 17:32:53 +0800 | [diff] [blame] | 26 | #ifdef CONFIG_TFM_PSA_API_CROSS_CALL |
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 = { |
| 32 | .sp = (uint32_t)&spm_stack_local[CONFIG_TFM_SPM_THREAD_STACK_SIZE], |
| 33 | .sp_limit = (uint32_t)spm_stack_local, |
| 34 | .reserved = 0, |
| 35 | .exc_ret = 0, |
| 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, |
| 49 | struct tfm_msg_body_t *msg) |
| 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 | |
| 55 | if (!msg || !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); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 63 | /* Add message to partition message list tail */ |
| 64 | BI_LIST_INSERT_BEFORE(&p_owner->msg_list, &msg->msg_node); |
| 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 | |
| 81 | if (!is_tfm_rpc_msg(msg)) { |
| 82 | thrd_wait_on(&msg->ack_evnt, CURRENT_THREAD); |
| 83 | } |
| 84 | |
| 85 | return PSA_SUCCESS; |
| 86 | } |
| 87 | |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 88 | static int32_t ipc_replying(struct tfm_msg_body_t *p_msg, int32_t status) |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 89 | { |
| 90 | if (is_tfm_rpc_msg(p_msg)) { |
| 91 | tfm_rpc_client_call_reply(p_msg, status); |
| 92 | } else { |
| 93 | thrd_wake_up(&p_msg->ack_evnt, status); |
| 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); |
| 113 | BI_LIST_INIT_NODE(&p_pt->msg_list); |
| 114 | |
| 115 | THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl, |
| 116 | TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags))); |
| 117 | |
Summer Qin | 9544482 | 2022-01-27 11:22:00 +0800 | [diff] [blame^] | 118 | #if defined(CONFIG_TFM_PSA_API_CROSS_CALL) && !defined(TFM_MULTI_CORE_TOPOLOGY) |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 119 | if (p_pldi->pid == TFM_SP_NON_SECURE_ID) { |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 120 | SPM_THREAD_CONTEXT = &p_pt->ctx_ctrl; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 121 | } |
Summer Qin | 9544482 | 2022-01-27 11:22:00 +0800 | [diff] [blame^] | 122 | #endif |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 123 | |
| 124 | thrd_start(&p_pt->thrd, |
Summer Qin | 9544482 | 2022-01-27 11:22:00 +0800 | [diff] [blame^] | 125 | POSITION_TO_ENTRY(p_pldi->entry, thrd_fn_t), NULL, |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 126 | LOAD_ALLOCED_STACK_ADDR(p_pldi), |
| 127 | LOAD_ALLOCED_STACK_ADDR(p_pldi) + p_pldi->stack_size); |
| 128 | } |
| 129 | |
| 130 | static uint32_t ipc_system_run(void) |
| 131 | { |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 132 | uint32_t control; |
| 133 | struct partition_t *p_cur_pt; |
| 134 | |
Chris Brand | 3778bc1 | 2021-12-15 17:01:05 -0800 | [diff] [blame] | 135 | #ifdef CONFIG_TFM_PSA_API_THREAD_CALL |
| 136 | TFM_CORE_ASSERT(SPM_THREAD_CONTEXT); |
| 137 | #endif |
| 138 | |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 139 | control = thrd_start_scheduler(&CURRENT_THREAD); |
| 140 | |
| 141 | p_cur_pt = TO_CONTAINER(CURRENT_THREAD->p_context_ctrl, |
| 142 | struct partition_t, ctx_ctrl); |
| 143 | |
| 144 | if (tfm_hal_update_boundaries(p_cur_pt->p_ldinf, p_cur_pt->p_boundaries) |
| 145 | != TFM_HAL_SUCCESS) { |
| 146 | tfm_core_panic(); |
| 147 | } |
| 148 | |
| 149 | return control; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | const struct backend_ops_t backend_instance = { |
| 153 | .comp_init_assuredly = ipc_comp_init_assuredly, |
| 154 | .system_run = ipc_system_run, |
| 155 | .messaging = ipc_messaging, |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 156 | .replying = ipc_replying, |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 157 | }; |