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 | 9483037 | 2020-05-13 16:37:34 +0800 | [diff] [blame] | 68 | * \brief Go through mailbox messages already replied by SPE mailbox and |
| 69 | * wake up the owner tasks of replied mailbox messages. |
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 | * |
David Hu | 9483037 | 2020-05-13 16:37:34 +0800 | [diff] [blame] | 73 | * \return MAILBOX_SUCCESS The tasks of replied mailbox messages |
| 74 | * were found and wake-up signals were sent. |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 75 | * \return MAILBOX_NO_PEND_EVENT No replied mailbox message is found. |
| 76 | * \return Other return code Failed with an error code |
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 | int32_t tfm_ns_mailbox_wake_reply_owner_isr(void); |
David Hu | f3e2047 | 2019-11-13 17:41:59 +0800 | [diff] [blame] | 79 | |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 80 | /** |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 81 | * \brief Platform specific NSPE mailbox initialization. |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 82 | * Invoked by \ref tfm_ns_mailbox_init(). |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 83 | * |
| 84 | * \param[in] queue The base address of NSPE mailbox queue to be |
| 85 | * initialized. |
| 86 | * |
| 87 | * \retval MAILBOX_SUCCESS Operation succeeded. |
| 88 | * \retval Other return code Operation failed with an error code. |
| 89 | */ |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 90 | 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] | 91 | |
| 92 | /** |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 93 | * \brief Notify SPE to deal with the PSA client call sent via mailbox |
| 94 | * |
| 95 | * \note The implementation depends on platform specific hardware and use case. |
| 96 | * |
| 97 | * \retval MAILBOX_SUCCESS Operation succeeded. |
| 98 | * \retval Other return code Operation failed with an error code. |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 99 | */ |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 100 | int32_t tfm_ns_mailbox_hal_notify_peer(void); |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 101 | |
| 102 | /** |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 103 | * \brief Enter critical section of NSPE mailbox. |
| 104 | * |
| 105 | * \note The implementation depends on platform specific hardware and use case. |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 106 | */ |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 107 | void tfm_ns_mailbox_hal_enter_critical(void); |
| 108 | |
| 109 | /** |
| 110 | * \brief Exit critical section of NSPE mailbox. |
| 111 | * |
| 112 | * \note The implementation depends on platform specific hardware and use case. |
| 113 | */ |
| 114 | void tfm_ns_mailbox_hal_exit_critical(void); |
| 115 | |
David Hu | c617ba1 | 2019-11-14 17:06:11 +0800 | [diff] [blame] | 116 | /** |
| 117 | * \brief Enter critical section of NSPE mailbox in IRQ handler. |
| 118 | * |
| 119 | * \note The implementation depends on platform specific hardware and use case. |
| 120 | */ |
| 121 | void tfm_ns_mailbox_hal_enter_critical_isr(void); |
| 122 | |
| 123 | /** |
| 124 | * \brief Enter critical section of NSPE mailbox in IRQ handler |
| 125 | * |
| 126 | * \note The implementation depends on platform specific hardware and use case. |
| 127 | */ |
| 128 | void tfm_ns_mailbox_hal_exit_critical_isr(void); |
| 129 | |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 130 | #ifdef FORWARD_PROT_MSG |
| 131 | static inline int32_t tfm_ns_mailbox_os_lock_init(void) |
| 132 | { |
| 133 | return MAILBOX_SUCCESS; |
| 134 | } |
| 135 | |
| 136 | static inline uint32_t tfm_ns_mailbox_os_lock_acquire(void) |
| 137 | { |
| 138 | return MAILBOX_SUCCESS; |
| 139 | } |
| 140 | |
| 141 | static inline uint32_t tfm_ns_mailbox_os_lock_release(void) |
| 142 | { |
| 143 | return MAILBOX_SUCCESS; |
| 144 | } |
| 145 | #else /* FORWARD_PROT_MSG */ |
| 146 | /** |
| 147 | * \brief Initialize the multi-core lock for synchronizing PSA client call(s) |
| 148 | * The actual implementation depends on the non-secure use scenario. |
| 149 | * |
| 150 | * \return \ref MAILBOX_SUCCESS on success |
| 151 | * \return \ref MAILBOX_GENERIC_ERROR on error |
| 152 | */ |
| 153 | int32_t tfm_ns_mailbox_os_lock_init(void); |
| 154 | |
| 155 | /** |
| 156 | * \brief Acquire the multi-core lock for synchronizing PSA client call(s) |
| 157 | * The actual implementation depends on the non-secure use scenario. |
| 158 | * |
| 159 | * \return \ref MAILBOX_SUCCESS on success |
| 160 | * \return \ref MAILBOX_GENERIC_ERROR on error |
| 161 | */ |
| 162 | int32_t tfm_ns_mailbox_os_lock_acquire(void); |
| 163 | |
| 164 | /** |
| 165 | * \brief Release the multi-core lock for synchronizing PSA client call(s) |
| 166 | * The actual implementation depends on the non-secure use scenario. |
| 167 | * |
| 168 | * \return \ref MAILBOX_SUCCESS on success |
| 169 | * \return \ref MAILBOX_GENERIC_ERROR on error |
| 170 | */ |
| 171 | int32_t tfm_ns_mailbox_os_lock_release(void); |
| 172 | #endif /* FORWARD_PROT_MSG */ |
| 173 | |
David Hu | f3e2047 | 2019-11-13 17:41:59 +0800 | [diff] [blame] | 174 | #ifdef TFM_MULTI_CORE_MULTI_CLIENT_CALL |
| 175 | /** |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 176 | * \brief Get the handle of the current non-secure task executing mailbox |
| 177 | * functionalities |
| 178 | * |
| 179 | * \note This function should be implemented according to NS OS and |
| 180 | * actual use scenario. |
| 181 | * This function can be ignored or return NULL if sleep/wake-up mechanism |
| 182 | * is not required in PSA Client API implementation. |
| 183 | * |
| 184 | * \return Return the handle of task. |
| 185 | */ |
| 186 | const void *tfm_ns_mailbox_os_get_task_handle(void); |
| 187 | |
| 188 | /** |
| 189 | * \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] | 190 | * the reply to be returned from SPE. |
David Hu | f3e2047 | 2019-11-13 17:41:59 +0800 | [diff] [blame] | 191 | * |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 192 | * \note This function is implemented by NS OS specific waiting mechanism |
| 193 | * according to use scenario. |
David Hu | f3e2047 | 2019-11-13 17:41:59 +0800 | [diff] [blame] | 194 | */ |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame] | 195 | void tfm_ns_mailbox_os_wait_reply(void); |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 196 | |
| 197 | /* |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 198 | * \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] | 199 | * handler, to wake up a sleeping task which is waiting for its mailbox |
| 200 | * message reply. |
| 201 | * |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 202 | * \note The underlying NS OS specific function called inside this function |
| 203 | * should be able to work in an IRQ handler. |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 204 | * |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 205 | * \note This function is implemented by NS OS specific waiting |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 206 | * mechanism according to use scenario. |
| 207 | * |
| 208 | * \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] | 209 | */ |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame] | 210 | void tfm_ns_mailbox_os_wake_task_isr(const void *task_handle); |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 211 | #else /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */ |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame] | 212 | #define tfm_ns_mailbox_os_wait_reply() do {} while (0) |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 213 | |
| 214 | static inline const void *tfm_ns_mailbox_os_get_task_handle(void) |
| 215 | { |
| 216 | return NULL; |
| 217 | } |
| 218 | |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame] | 219 | #define tfm_ns_mailbox_os_wake_task_isr(task) do {} while (0) |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 220 | #endif /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */ |
David Hu | f3e2047 | 2019-11-13 17:41:59 +0800 | [diff] [blame] | 221 | |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 222 | #ifdef TFM_MULTI_CORE_TEST |
| 223 | /** |
| 224 | * \brief Initialize the statistics module in TF-M NSPE mailbox. |
| 225 | * |
| 226 | * \note This function is only available when multi-core tests are enabled. |
| 227 | */ |
| 228 | void tfm_ns_mailbox_tx_stats_init(void); |
| 229 | |
| 230 | /** |
| 231 | * \brief Calculate the average number of used NS mailbox queue slots each time |
| 232 | * NS task requires a queue slot to submit mailbox message, which is |
| 233 | * recorded in NS mailbox statisitics module. |
| 234 | * |
| 235 | * \note This function is only available when multi-core tests are enabled. |
| 236 | * |
| 237 | * \param[in] stats_res The buffer to be written with |
| 238 | * \ref ns_mailbox_stats_res_t. |
| 239 | * |
| 240 | * \return Return the calculation result. |
| 241 | */ |
| 242 | void tfm_ns_mailbox_stats_avg_slot(struct ns_mailbox_stats_res_t *stats_res); |
| 243 | #endif |
| 244 | |
David Hu | dbafa3e | 2019-10-22 11:26:37 +0800 | [diff] [blame] | 245 | #ifdef __cplusplus |
| 246 | } |
| 247 | #endif |
David Hu | 49ec08a | 2019-09-23 16:13:41 +0800 | [diff] [blame] | 248 | |
| 249 | #endif /* __TFM_NS_MAILBOX_H__ */ |