David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 1 | /* |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, Arm Limited. All rights reserved. |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 8 | /* Data types and API definitions in NSPE mailbox library */ |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 9 | |
| 10 | #ifndef __TFM_NS_MAILBOX_H__ |
| 11 | #define __TFM_NS_MAILBOX_H__ |
| 12 | |
| 13 | #include <stdbool.h> |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 14 | #include <stdint.h> |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 15 | #include "tfm_mailbox.h" |
| 16 | |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 17 | #ifdef __cplusplus |
| 18 | extern "C" { |
| 19 | #endif |
| 20 | |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 21 | #ifdef TFM_MULTI_CORE_TEST |
| 22 | /** |
| 23 | * \brief The structure to hold the statistics result of NSPE mailbox |
| 24 | */ |
| 25 | struct ns_mailbox_stats_res_t { |
| 26 | uint8_t avg_nr_slots; /* The value before the decimal point |
| 27 | * in the average number of NSPE |
| 28 | * mailbox slots in use. |
| 29 | */ |
| 30 | uint8_t avg_nr_slots_tenths; /* The first digit value after the |
| 31 | * decimal point in the average |
| 32 | * number of NSPE mailbox slots in use. |
| 33 | */ |
| 34 | }; |
| 35 | #endif |
| 36 | |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 37 | /** |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 38 | * \brief NSPE mailbox initialization |
| 39 | * |
| 40 | * \param[in] queue The base address of NSPE mailbox queue to be |
| 41 | * initialized. |
| 42 | * |
| 43 | * \retval MAILBOX_SUCCESS Operation succeeded. |
| 44 | * \retval Other return code Operation failed with an error code. |
| 45 | */ |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 46 | int32_t tfm_ns_mailbox_init(struct ns_mailbox_queue_t *queue); |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 47 | |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 48 | /** |
| 49 | * \brief Send PSA client call to SPE via mailbox. Wait and fetch PSA client |
| 50 | * call result. |
| 51 | * |
| 52 | * \param[in] call_type PSA client call type |
| 53 | * \param[in] params Parameters used for PSA client call |
| 54 | * \param[in] client_id Optional client ID of non-secure caller. |
| 55 | * It is required to identify the non-secure caller |
| 56 | * when NSPE OS enforces non-secure task isolation. |
| 57 | * \param[out] reply The buffer written with PSA client call result. |
| 58 | * |
| 59 | * \retval MAILBOX_SUCCESS The PSA client call is completed successfully. |
| 60 | * \retval Other return code Operation failed with an error code. |
| 61 | */ |
| 62 | int32_t tfm_ns_mailbox_client_call(uint32_t call_type, |
| 63 | const struct psa_client_params_t *params, |
| 64 | int32_t client_id, |
| 65 | int32_t *reply); |
| 66 | |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 67 | /** |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 68 | * \brief Wake up the owner task of the first replied mailbox message in the |
| 69 | * NSPE mailbox queue. |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 70 | * This function is intended to be called inside platform specific |
| 71 | * notification IRQ handler. |
| 72 | * |
| 73 | * \note The replied status of the fetched mailbox message will be cleaned after |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 74 | * the message is fetched. When this function is called again, it wakes |
| 75 | * the owner task of next replied mailbox message from the NSPE mailbox |
| 76 | * queue. |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 77 | * |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 78 | * \return MAILBOX_SUCCESS The task of the first replied mailbox message |
| 79 | * is found and wake-up signal is sent. |
| 80 | * \return MAILBOX_NO_PEND_EVENT No replied mailbox message is found. |
| 81 | * \return Other return code Failed with an error code |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 82 | */ |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 83 | int32_t tfm_ns_mailbox_wake_reply_owner_isr(void); |
David Hu | f3e2047 | 2019-11-13 17:41:59 +0800 | [diff] [blame] | 84 | |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 85 | /** |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 86 | * \brief Platform specific NSPE mailbox initialization. |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 87 | * Invoked by \ref tfm_ns_mailbox_init(). |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 88 | * |
| 89 | * \param[in] queue The base address of NSPE mailbox queue to be |
| 90 | * initialized. |
| 91 | * |
| 92 | * \retval MAILBOX_SUCCESS Operation succeeded. |
| 93 | * \retval Other return code Operation failed with an error code. |
| 94 | */ |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 95 | int32_t tfm_ns_mailbox_hal_init(struct ns_mailbox_queue_t *queue); |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 96 | |
| 97 | /** |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 98 | * \brief Notify SPE to deal with the PSA client call sent via mailbox |
| 99 | * |
| 100 | * \note The implementation depends on platform specific hardware and use case. |
| 101 | * |
| 102 | * \retval MAILBOX_SUCCESS Operation succeeded. |
| 103 | * \retval Other return code Operation failed with an error code. |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 104 | */ |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 105 | int32_t tfm_ns_mailbox_hal_notify_peer(void); |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 106 | |
| 107 | /** |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 108 | * \brief Enter critical section of NSPE mailbox. |
| 109 | * |
| 110 | * \note The implementation depends on platform specific hardware and use case. |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 111 | */ |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 112 | void tfm_ns_mailbox_hal_enter_critical(void); |
| 113 | |
| 114 | /** |
| 115 | * \brief Exit critical section of NSPE mailbox. |
| 116 | * |
| 117 | * \note The implementation depends on platform specific hardware and use case. |
| 118 | */ |
| 119 | void tfm_ns_mailbox_hal_exit_critical(void); |
| 120 | |
David Hu | c617ba1 | 2019-11-14 17:06:11 +0800 | [diff] [blame] | 121 | /** |
| 122 | * \brief Enter critical section of NSPE mailbox in IRQ handler. |
| 123 | * |
| 124 | * \note The implementation depends on platform specific hardware and use case. |
| 125 | */ |
| 126 | void tfm_ns_mailbox_hal_enter_critical_isr(void); |
| 127 | |
| 128 | /** |
| 129 | * \brief Enter critical section of NSPE mailbox in IRQ handler |
| 130 | * |
| 131 | * \note The implementation depends on platform specific hardware and use case. |
| 132 | */ |
| 133 | void tfm_ns_mailbox_hal_exit_critical_isr(void); |
| 134 | |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 135 | #ifdef FORWARD_PROT_MSG |
| 136 | static inline int32_t tfm_ns_mailbox_os_lock_init(void) |
| 137 | { |
| 138 | return MAILBOX_SUCCESS; |
| 139 | } |
| 140 | |
| 141 | static inline uint32_t tfm_ns_mailbox_os_lock_acquire(void) |
| 142 | { |
| 143 | return MAILBOX_SUCCESS; |
| 144 | } |
| 145 | |
| 146 | static inline uint32_t tfm_ns_mailbox_os_lock_release(void) |
| 147 | { |
| 148 | return MAILBOX_SUCCESS; |
| 149 | } |
| 150 | #else /* FORWARD_PROT_MSG */ |
| 151 | /** |
| 152 | * \brief Initialize the multi-core lock for synchronizing PSA client call(s) |
| 153 | * The actual implementation depends on the non-secure use scenario. |
| 154 | * |
| 155 | * \return \ref MAILBOX_SUCCESS on success |
| 156 | * \return \ref MAILBOX_GENERIC_ERROR on error |
| 157 | */ |
| 158 | int32_t tfm_ns_mailbox_os_lock_init(void); |
| 159 | |
| 160 | /** |
| 161 | * \brief Acquire the multi-core lock for synchronizing PSA client call(s) |
| 162 | * The actual implementation depends on the non-secure use scenario. |
| 163 | * |
| 164 | * \return \ref MAILBOX_SUCCESS on success |
| 165 | * \return \ref MAILBOX_GENERIC_ERROR on error |
| 166 | */ |
| 167 | int32_t tfm_ns_mailbox_os_lock_acquire(void); |
| 168 | |
| 169 | /** |
| 170 | * \brief Release the multi-core lock for synchronizing PSA client call(s) |
| 171 | * The actual implementation depends on the non-secure use scenario. |
| 172 | * |
| 173 | * \return \ref MAILBOX_SUCCESS on success |
| 174 | * \return \ref MAILBOX_GENERIC_ERROR on error |
| 175 | */ |
| 176 | int32_t tfm_ns_mailbox_os_lock_release(void); |
| 177 | #endif /* FORWARD_PROT_MSG */ |
| 178 | |
David Hu | f3e2047 | 2019-11-13 17:41:59 +0800 | [diff] [blame] | 179 | #ifdef TFM_MULTI_CORE_MULTI_CLIENT_CALL |
| 180 | /** |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 181 | * \brief Get the handle of the current non-secure task executing mailbox |
| 182 | * functionalities |
| 183 | * |
| 184 | * \note This function should be implemented according to NS OS and |
| 185 | * actual use scenario. |
| 186 | * This function can be ignored or return NULL if sleep/wake-up mechanism |
| 187 | * is not required in PSA Client API implementation. |
| 188 | * |
| 189 | * \return Return the handle of task. |
| 190 | */ |
| 191 | const void *tfm_ns_mailbox_os_get_task_handle(void); |
| 192 | |
| 193 | /** |
| 194 | * \brief Performs use scenario and NS OS specific waiting mechanism to wait for |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame^] | 195 | * the reply to be returned from SPE. |
David Hu | f3e2047 | 2019-11-13 17:41:59 +0800 | [diff] [blame] | 196 | * |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 197 | * \note This function is implemented by NS OS specific waiting mechanism |
| 198 | * according to use scenario. |
David Hu | f3e2047 | 2019-11-13 17:41:59 +0800 | [diff] [blame] | 199 | */ |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame^] | 200 | void tfm_ns_mailbox_os_wait_reply(void); |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 201 | |
| 202 | /* |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 203 | * \brief Performs use scenario and NS OS specific mechanism in a mailbox IRQ |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 204 | * handler, to wake up a sleeping task which is waiting for its mailbox |
| 205 | * message reply. |
| 206 | * |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 207 | * \note The underlying NS OS specific function called inside this function |
| 208 | * should be able to work in an IRQ handler. |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 209 | * |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 210 | * \note This function is implemented by NS OS specific waiting |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 211 | * mechanism according to use scenario. |
| 212 | * |
| 213 | * \param[in] task_handle The handle to the task to be woken up. |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 214 | */ |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame^] | 215 | void tfm_ns_mailbox_os_wake_task_isr(const void *task_handle); |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 216 | #else /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */ |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame^] | 217 | #define tfm_ns_mailbox_os_wait_reply() do {} while (0) |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 218 | |
| 219 | static inline const void *tfm_ns_mailbox_os_get_task_handle(void) |
| 220 | { |
| 221 | return NULL; |
| 222 | } |
| 223 | |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame^] | 224 | #define tfm_ns_mailbox_os_wake_task_isr(task) do {} while (0) |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 225 | #endif /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */ |
David Hu | f3e2047 | 2019-11-13 17:41:59 +0800 | [diff] [blame] | 226 | |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 227 | #ifdef TFM_MULTI_CORE_TEST |
| 228 | /** |
| 229 | * \brief Initialize the statistics module in TF-M NSPE mailbox. |
| 230 | * |
| 231 | * \note This function is only available when multi-core tests are enabled. |
| 232 | */ |
| 233 | void tfm_ns_mailbox_tx_stats_init(void); |
| 234 | |
| 235 | /** |
| 236 | * \brief Calculate the average number of used NS mailbox queue slots each time |
| 237 | * NS task requires a queue slot to submit mailbox message, which is |
| 238 | * recorded in NS mailbox statisitics module. |
| 239 | * |
| 240 | * \note This function is only available when multi-core tests are enabled. |
| 241 | * |
| 242 | * \param[in] stats_res The buffer to be written with |
| 243 | * \ref ns_mailbox_stats_res_t. |
| 244 | * |
| 245 | * \return Return the calculation result. |
| 246 | */ |
| 247 | void tfm_ns_mailbox_stats_avg_slot(struct ns_mailbox_stats_res_t *stats_res); |
| 248 | #endif |
| 249 | |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 250 | #ifdef __cplusplus |
| 251 | } |
| 252 | #endif |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 253 | |
| 254 | #endif /* __TFM_NS_MAILBOX_H__ */ |