Ken Liu | f39d8eb | 2021-10-07 12:55:33 +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 | 30106ba | 2022-01-13 13:48:50 -0800 | [diff] [blame] | 3 | * Copyright (c) 2022, Cypress Semiconductor Corporation. All rights reserved. |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: BSD-3-Clause |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <stdint.h> |
| 10 | #include "compiler_ext_defs.h" |
| 11 | #include "current.h" |
Kevin Peng | 43160d5 | 2022-02-11 13:35:56 +0800 | [diff] [blame] | 12 | #include "runtime_defs.h" |
Kevin Peng | b288c52 | 2021-09-26 16:18:23 +0800 | [diff] [blame] | 13 | #include "tfm_hal_platform.h" |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 14 | #include "ffm/backend.h" |
Chris Brand | 30106ba | 2022-01-13 13:48:50 -0800 | [diff] [blame] | 15 | #include "ffm/stack_watermark.h" |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 16 | #include "load/partition_defs.h" |
| 17 | #include "load/service_defs.h" |
| 18 | #include "load/spm_load_api.h" |
| 19 | #include "psa/error.h" |
| 20 | #include "psa/service.h" |
| 21 | #include "spm_ipc.h" |
| 22 | |
| 23 | /* SFN Partition state */ |
| 24 | #define SFN_PARTITION_STATE_NOT_INITED 0 |
| 25 | #define SFN_PARTITION_STATE_INITED 1 |
| 26 | |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 27 | /* Declare the global component list */ |
| 28 | struct partition_head_t partition_listhead; |
| 29 | |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame^] | 30 | /* Current running partition. */ |
| 31 | struct partition_t *p_current_partition; |
| 32 | |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 33 | /* |
| 34 | * Send message and wake up the SP who is waiting on message queue, block the |
| 35 | * current component state and activate the next component. |
| 36 | */ |
| 37 | static psa_status_t sfn_messaging(struct service_t *service, |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 38 | struct conn_handle_t *handle) |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 39 | { |
| 40 | struct partition_t *p_target; |
| 41 | psa_status_t status; |
| 42 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 43 | if (!handle || !service || !service->p_ldinf || !service->partition) { |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 44 | return PSA_ERROR_PROGRAMMER_ERROR; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 45 | } |
| 46 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 47 | handle->sfn_magic = TFM_MSG_MAGIC_SFN; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 48 | p_target = service->partition; |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 49 | p_target->p_handles = handle; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 50 | |
| 51 | SET_CURRENT_COMPONENT(p_target); |
| 52 | |
| 53 | if (p_target->state == SFN_PARTITION_STATE_NOT_INITED) { |
| 54 | if (p_target->p_ldinf->entry != 0) { |
| 55 | status = ((sfn_init_fn_t)p_target->p_ldinf->entry)(); |
| 56 | /* Negative value indicates errors. */ |
| 57 | if (status < PSA_SUCCESS) { |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 58 | return PSA_ERROR_PROGRAMMER_ERROR; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | p_target->state = SFN_PARTITION_STATE_INITED; |
| 62 | } |
| 63 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 64 | status = ((service_fn_t)service->p_ldinf->sfn)(&handle->msg); |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 65 | |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame] | 66 | handle->status = TFM_HANDLE_STATUS_ACTIVE; |
| 67 | |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 68 | return status; |
| 69 | } |
| 70 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 71 | static psa_status_t sfn_replying(struct conn_handle_t *handle, int32_t status) |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 72 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 73 | SET_CURRENT_COMPONENT(handle->p_client); |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Returning a value here is necessary, because 'psa_reply' is absent |
| 77 | * for SFN clients, the 'reply' method is performed by SPM internally |
| 78 | * when SFN case, to forward the 'status' to the caller. |
| 79 | * |
| 80 | * For example: |
| 81 | * 'status' MAY contain a 'psa_handle_t' returned by SPM 'connect' and |
| 82 | * SPM needs to 'reply' it back to the caller. Treat 'psa_handle_t' value |
| 83 | * as SPM specific return value and represnent it as 'psa_status_t'. |
| 84 | */ |
| 85 | return status; |
| 86 | } |
| 87 | |
Ken Liu | ef229a2 | 2022-02-11 11:15:43 +0800 | [diff] [blame] | 88 | static void spm_thread_fn(void) |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 89 | { |
| 90 | struct partition_t *p_part, *p_curr; |
| 91 | |
| 92 | p_curr = GET_CURRENT_COMPONENT(); |
| 93 | /* Call partition initialization routine one by one. */ |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 94 | UNI_LIST_FOREACH(p_part, PARTITION_LIST_ADDR, next) { |
Kevin Peng | 56c571e | 2022-01-10 14:06:05 +0800 | [diff] [blame] | 95 | if (IS_PARTITION_IPC_MODEL(p_part->p_ldinf)) { |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 96 | continue; |
| 97 | } |
| 98 | |
| 99 | if (p_part->state == SFN_PARTITION_STATE_INITED) { |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | SET_CURRENT_COMPONENT(p_part); |
| 104 | |
| 105 | if (p_part->p_ldinf->entry != 0) { |
| 106 | if (((sfn_init_fn_t)p_part->p_ldinf->entry)() < PSA_SUCCESS) { |
| 107 | tfm_core_panic(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | p_part->state = SFN_PARTITION_STATE_INITED; |
| 112 | } |
| 113 | |
| 114 | SET_CURRENT_COMPONENT(p_curr); |
Ken Liu | ef229a2 | 2022-02-11 11:15:43 +0800 | [diff] [blame] | 115 | } |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 116 | |
Ken Liu | ef229a2 | 2022-02-11 11:15:43 +0800 | [diff] [blame] | 117 | /* Parameters are treated as assuredly */ |
| 118 | void sfn_comp_init_assuredly(struct partition_t *p_pt, uint32_t service_set) |
| 119 | { |
| 120 | const struct partition_load_info_t *p_pldi = p_pt->p_ldinf; |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame^] | 121 | struct context_ctrl_t ns_agent_ctrl; |
Ken Liu | ef229a2 | 2022-02-11 11:15:43 +0800 | [diff] [blame] | 122 | p_pt->p_handles = NULL; |
| 123 | p_pt->state = SFN_PARTITION_STATE_NOT_INITED; |
| 124 | |
Chris Brand | 30106ba | 2022-01-13 13:48:50 -0800 | [diff] [blame] | 125 | watermark_stack(p_pt); |
| 126 | |
Ken Liu | ef229a2 | 2022-02-11 11:15:43 +0800 | [diff] [blame] | 127 | /* |
| 128 | * Built-in partitions still have thread instances: NS Agent (TZ) and |
| 129 | * IDLE partition, and NS Agent (TZ) needs to be specific cared here. |
| 130 | */ |
| 131 | if (p_pldi->pid == TFM_SP_NON_SECURE_ID) { |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame^] | 132 | ARCH_CTXCTRL_INIT(&ns_agent_ctrl, |
| 133 | LOAD_ALLOCED_STACK_ADDR(p_pldi), |
| 134 | p_pldi->stack_size); |
| 135 | tfm_arch_init_context(&ns_agent_ctrl, (uintptr_t)spm_thread_fn, |
| 136 | NULL, p_pldi->entry); |
| 137 | tfm_arch_refresh_hardware_context(&ns_agent_ctrl); |
| 138 | SET_CURRENT_COMPONENT(p_pt); |
Ken Liu | ef229a2 | 2022-02-11 11:15:43 +0800 | [diff] [blame] | 139 | } |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | uint32_t sfn_system_run(void) |
| 143 | { |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame^] | 144 | return EXC_RETURN_THREAD_S_PSP; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 145 | } |
| 146 | |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 147 | static psa_signal_t sfn_wait(struct partition_t *p_pt, psa_signal_t signal_mask) |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 148 | { |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 149 | while (!(p_pt->signals_asserted & signal_mask)) |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 150 | ; |
| 151 | |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 152 | return p_pt->signals_asserted & signal_mask; |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 153 | } |
| 154 | |
Mingyang Sun | 09328bd | 2022-03-25 11:55:13 +0800 | [diff] [blame] | 155 | static void sfn_wake_up(struct partition_t *p_pt) |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 156 | { |
| 157 | (void)p_pt; |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 158 | } |
| 159 | |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 160 | const struct backend_ops_t backend_instance = { |
| 161 | .comp_init_assuredly = sfn_comp_init_assuredly, |
| 162 | .system_run = sfn_system_run, |
| 163 | .messaging = sfn_messaging, |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 164 | .replying = sfn_replying, |
| 165 | .wait = sfn_wait, |
| 166 | .wake_up = sfn_wake_up, |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 167 | }; |