David Hu | d2753b3 | 2019-09-23 18:46:15 +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 | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <string.h> |
David Hu | 675286d | 2020-08-03 14:19:09 +0800 | [diff] [blame] | 9 | |
| 10 | #include "cmsis_compiler.h" |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 11 | #include "tfm_ns_mailbox.h" |
| 12 | |
| 13 | /* The pointer to NSPE mailbox queue */ |
| 14 | static struct ns_mailbox_queue_t *mailbox_queue_ptr = NULL; |
| 15 | |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame] | 16 | static int32_t mailbox_wait_reply(uint8_t idx); |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 17 | |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 18 | static inline void clear_queue_slot_empty(uint8_t idx) |
| 19 | { |
| 20 | if (idx < NUM_MAILBOX_QUEUE_SLOT) { |
| 21 | mailbox_queue_ptr->empty_slots &= ~(1 << idx); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | static inline void set_queue_slot_empty(uint8_t idx) |
| 26 | { |
| 27 | if (idx < NUM_MAILBOX_QUEUE_SLOT) { |
| 28 | mailbox_queue_ptr->empty_slots |= (1 << idx); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | static inline void set_queue_slot_pend(uint8_t idx) |
| 33 | { |
| 34 | if (idx < NUM_MAILBOX_QUEUE_SLOT) { |
| 35 | mailbox_queue_ptr->pend_slots |= (1 << idx); |
| 36 | } |
| 37 | } |
| 38 | |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 39 | static inline void clear_queue_slot_replied(uint8_t idx) |
| 40 | { |
| 41 | if (idx < NUM_MAILBOX_QUEUE_SLOT) { |
| 42 | mailbox_queue_ptr->replied_slots &= ~(1 << idx); |
| 43 | } |
| 44 | } |
| 45 | |
David Hu | 9483037 | 2020-05-13 16:37:34 +0800 | [diff] [blame] | 46 | static inline void clear_queue_slot_all_replied(mailbox_queue_status_t status) |
| 47 | { |
| 48 | mailbox_queue_ptr->replied_slots &= ~status; |
| 49 | } |
| 50 | |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 51 | static inline bool is_queue_slot_replied(uint8_t idx) |
| 52 | { |
| 53 | if (idx < NUM_MAILBOX_QUEUE_SLOT) { |
| 54 | return mailbox_queue_ptr->replied_slots & (1UL << idx); |
| 55 | } |
| 56 | |
| 57 | return false; |
| 58 | } |
| 59 | |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 60 | static inline void set_queue_slot_woken(uint8_t idx) |
| 61 | { |
| 62 | if (idx < NUM_MAILBOX_QUEUE_SLOT) { |
| 63 | mailbox_queue_ptr->queue[idx].is_woken = true; |
| 64 | } |
| 65 | } |
| 66 | |
David Hu | f3e2047 | 2019-11-13 17:41:59 +0800 | [diff] [blame] | 67 | static inline bool is_queue_slot_woken(uint8_t idx) |
| 68 | { |
| 69 | if (idx < NUM_MAILBOX_QUEUE_SLOT) { |
| 70 | return mailbox_queue_ptr->queue[idx].is_woken; |
| 71 | } |
| 72 | |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | static inline void clear_queue_slot_woken(uint8_t idx) |
| 77 | { |
| 78 | if (idx < NUM_MAILBOX_QUEUE_SLOT) { |
| 79 | mailbox_queue_ptr->queue[idx].is_woken = false; |
| 80 | } |
| 81 | } |
| 82 | |
David Hu | 2d2a2f1 | 2020-08-09 15:27:02 +0800 | [diff] [blame^] | 83 | #ifdef TFM_MULTI_CORE_MULTI_CLIENT_CALL |
| 84 | /* |
| 85 | * When NSPE mailbox only covers a single non-secure core, spinlock only |
| 86 | * requires to disable IRQ. |
| 87 | */ |
| 88 | static inline void ns_mailbox_spin_lock(void) |
| 89 | { |
| 90 | __disable_irq(); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * It is assumed that IRQ is always enabled when spinlock is acquired. |
| 95 | * Otherwise, the waiting thread won't be woken up. |
| 96 | */ |
| 97 | static inline void ns_mailbox_spin_unlock(void) |
| 98 | { |
| 99 | __enable_irq(); |
| 100 | } |
| 101 | #else /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */ |
| 102 | /* |
| 103 | * Local spinlock is implemented as a dummy one when multiple PSA client call |
| 104 | * feature is disabled, since interrupt is not required in NS mailbox. |
| 105 | */ |
| 106 | #define ns_mailbox_spin_lock() do {} while (0) |
| 107 | |
| 108 | #define ns_mailbox_spin_unlock() do {} while (0) |
| 109 | #endif /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */ |
| 110 | |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 111 | static uint8_t acquire_empty_slot(struct ns_mailbox_queue_t *queue) |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 112 | { |
| 113 | uint8_t idx; |
| 114 | mailbox_queue_status_t status; |
| 115 | |
David Hu | 2d2a2f1 | 2020-08-09 15:27:02 +0800 | [diff] [blame^] | 116 | ns_mailbox_spin_lock(); |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 117 | status = queue->empty_slots; |
David Hu | 2d2a2f1 | 2020-08-09 15:27:02 +0800 | [diff] [blame^] | 118 | ns_mailbox_spin_unlock(); |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 119 | |
| 120 | if (!status) { |
| 121 | /* No empty slot */ |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 122 | return NUM_MAILBOX_QUEUE_SLOT; |
| 123 | } |
| 124 | |
| 125 | for (idx = 0; idx < NUM_MAILBOX_QUEUE_SLOT; idx++) { |
| 126 | if (status & (1 << idx)) { |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
David Hu | 2d2a2f1 | 2020-08-09 15:27:02 +0800 | [diff] [blame^] | 131 | ns_mailbox_spin_lock(); |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 132 | clear_queue_slot_empty(idx); |
David Hu | 2d2a2f1 | 2020-08-09 15:27:02 +0800 | [diff] [blame^] | 133 | ns_mailbox_spin_unlock(); |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 134 | |
| 135 | return idx; |
| 136 | } |
| 137 | |
David Hu | 06ebac7 | 2019-09-29 16:01:54 +0800 | [diff] [blame] | 138 | static void set_msg_owner(uint8_t idx, const void *owner) |
| 139 | { |
| 140 | if (idx < NUM_MAILBOX_QUEUE_SLOT) { |
| 141 | mailbox_queue_ptr->queue[idx].owner = owner; |
| 142 | } |
| 143 | } |
| 144 | |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 145 | #ifdef TFM_MULTI_CORE_TEST |
| 146 | void tfm_ns_mailbox_tx_stats_init(void) |
| 147 | { |
| 148 | if (!mailbox_queue_ptr) { |
| 149 | return; |
| 150 | } |
| 151 | |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 152 | mailbox_queue_ptr->nr_tx = 0; |
| 153 | mailbox_queue_ptr->nr_used_slots = 0; |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void mailbox_tx_stats_update(struct ns_mailbox_queue_t *ns_queue) |
| 157 | { |
| 158 | mailbox_queue_status_t empty_status; |
| 159 | uint8_t idx, nr_empty = 0; |
| 160 | |
| 161 | if (!ns_queue) { |
| 162 | return; |
| 163 | } |
| 164 | |
David Hu | 2d2a2f1 | 2020-08-09 15:27:02 +0800 | [diff] [blame^] | 165 | ns_mailbox_spin_lock(); |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 166 | /* Count the number of used slots when this tx arrives */ |
| 167 | empty_status = ns_queue->empty_slots; |
David Hu | 2d2a2f1 | 2020-08-09 15:27:02 +0800 | [diff] [blame^] | 168 | ns_mailbox_spin_unlock(); |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 169 | |
| 170 | if (empty_status) { |
| 171 | for (idx = 0; idx < NUM_MAILBOX_QUEUE_SLOT; idx++) { |
| 172 | if (empty_status & (0x1UL << idx)) { |
| 173 | nr_empty++; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
David Hu | 675286d | 2020-08-03 14:19:09 +0800 | [diff] [blame] | 178 | ns_mailbox_spin_lock(); |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 179 | ns_queue->nr_used_slots += (NUM_MAILBOX_QUEUE_SLOT - nr_empty); |
David Hu | 675286d | 2020-08-03 14:19:09 +0800 | [diff] [blame] | 180 | ns_queue->nr_tx++; |
| 181 | ns_mailbox_spin_unlock(); |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void tfm_ns_mailbox_stats_avg_slot(struct ns_mailbox_stats_res_t *stats_res) |
| 185 | { |
| 186 | uint32_t nr_used_slots, nr_tx; |
| 187 | |
| 188 | if (!mailbox_queue_ptr || !stats_res) { |
| 189 | return; |
| 190 | } |
| 191 | |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 192 | nr_used_slots = mailbox_queue_ptr->nr_used_slots; |
| 193 | nr_tx = mailbox_queue_ptr->nr_tx; |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 194 | |
| 195 | stats_res->avg_nr_slots = nr_used_slots / nr_tx; |
| 196 | nr_used_slots %= nr_tx; |
| 197 | stats_res->avg_nr_slots_tenths = nr_used_slots * 10 / nr_tx; |
| 198 | } |
| 199 | #endif |
| 200 | |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 201 | static int32_t mailbox_tx_client_req(uint32_t call_type, |
| 202 | const struct psa_client_params_t *params, |
| 203 | int32_t client_id, |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame] | 204 | uint8_t *slot_idx) |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 205 | { |
| 206 | uint8_t idx; |
| 207 | struct mailbox_msg_t *msg_ptr; |
David Hu | 06ebac7 | 2019-09-29 16:01:54 +0800 | [diff] [blame] | 208 | const void *task_handle; |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 209 | |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 210 | idx = acquire_empty_slot(mailbox_queue_ptr); |
| 211 | if (idx >= NUM_MAILBOX_QUEUE_SLOT) { |
| 212 | return MAILBOX_QUEUE_FULL; |
| 213 | } |
| 214 | |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 215 | #ifdef TFM_MULTI_CORE_TEST |
| 216 | mailbox_tx_stats_update(mailbox_queue_ptr); |
| 217 | #endif |
| 218 | |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 219 | /* Fill the mailbox message */ |
| 220 | msg_ptr = &mailbox_queue_ptr->queue[idx].msg; |
| 221 | |
| 222 | msg_ptr->call_type = call_type; |
| 223 | memcpy(&msg_ptr->params, params, sizeof(msg_ptr->params)); |
| 224 | msg_ptr->client_id = client_id; |
| 225 | |
David Hu | 06ebac7 | 2019-09-29 16:01:54 +0800 | [diff] [blame] | 226 | /* |
| 227 | * Fetch the current task handle. The task will be woken up according the |
| 228 | * handle value set in the owner field. |
| 229 | */ |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 230 | task_handle = tfm_ns_mailbox_os_get_task_handle(); |
David Hu | 06ebac7 | 2019-09-29 16:01:54 +0800 | [diff] [blame] | 231 | set_msg_owner(idx, task_handle); |
| 232 | |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 233 | tfm_ns_mailbox_hal_enter_critical(); |
| 234 | set_queue_slot_pend(idx); |
| 235 | tfm_ns_mailbox_hal_exit_critical(); |
| 236 | |
| 237 | tfm_ns_mailbox_hal_notify_peer(); |
| 238 | |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame] | 239 | *slot_idx = idx; |
| 240 | |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 241 | return MAILBOX_SUCCESS; |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 242 | } |
| 243 | |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame] | 244 | static int32_t mailbox_rx_client_reply(uint8_t idx, int32_t *reply) |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 245 | { |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 246 | *reply = mailbox_queue_ptr->queue[idx].reply.return_val; |
| 247 | |
David Hu | 06ebac7 | 2019-09-29 16:01:54 +0800 | [diff] [blame] | 248 | /* Clear up the owner field */ |
| 249 | set_msg_owner(idx, NULL); |
| 250 | |
David Hu | 2d2a2f1 | 2020-08-09 15:27:02 +0800 | [diff] [blame^] | 251 | ns_mailbox_spin_lock(); |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 252 | clear_queue_slot_woken(idx); |
| 253 | /* |
| 254 | * Make sure that the empty flag is set after all the other status flags are |
| 255 | * re-initialized. |
| 256 | */ |
| 257 | set_queue_slot_empty(idx); |
David Hu | 2d2a2f1 | 2020-08-09 15:27:02 +0800 | [diff] [blame^] | 258 | ns_mailbox_spin_unlock(); |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 259 | |
| 260 | return MAILBOX_SUCCESS; |
| 261 | } |
| 262 | |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 263 | int32_t tfm_ns_mailbox_client_call(uint32_t call_type, |
| 264 | const struct psa_client_params_t *params, |
| 265 | int32_t client_id, |
| 266 | int32_t *reply) |
| 267 | { |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame] | 268 | uint8_t slot_idx = NUM_MAILBOX_QUEUE_SLOT; |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 269 | int32_t reply_buf = 0x0; |
| 270 | int32_t ret; |
| 271 | |
| 272 | if (!mailbox_queue_ptr) { |
| 273 | return MAILBOX_INIT_ERROR; |
| 274 | } |
| 275 | |
| 276 | if (!params || !reply) { |
| 277 | return MAILBOX_INVAL_PARAMS; |
| 278 | } |
| 279 | |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 280 | if (tfm_ns_mailbox_os_lock_acquire() != MAILBOX_SUCCESS) { |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 281 | return MAILBOX_QUEUE_FULL; |
| 282 | } |
| 283 | |
| 284 | /* It requires SVCall if NS mailbox is put in privileged mode. */ |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame] | 285 | ret = mailbox_tx_client_req(call_type, params, client_id, &slot_idx); |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 286 | if (ret != MAILBOX_SUCCESS) { |
| 287 | goto exit; |
| 288 | } |
| 289 | |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame] | 290 | mailbox_wait_reply(slot_idx); |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 291 | |
| 292 | /* It requires SVCall if NS mailbox is put in privileged mode. */ |
David Hu | 6730af2 | 2020-05-11 19:50:08 +0800 | [diff] [blame] | 293 | ret = mailbox_rx_client_reply(slot_idx, &reply_buf); |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 294 | if (ret == MAILBOX_SUCCESS) { |
| 295 | *reply = reply_buf; |
| 296 | } |
| 297 | |
| 298 | exit: |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 299 | if (tfm_ns_mailbox_os_lock_release() != MAILBOX_SUCCESS) { |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 300 | return MAILBOX_GENERIC_ERROR; |
| 301 | } |
| 302 | |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | #ifdef TFM_MULTI_CORE_MULTI_CLIENT_CALL |
| 307 | int32_t tfm_ns_mailbox_wake_reply_owner_isr(void) |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 308 | { |
| 309 | uint8_t idx; |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 310 | mailbox_queue_status_t replied_status; |
| 311 | |
| 312 | if (!mailbox_queue_ptr) { |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 313 | return MAILBOX_INIT_ERROR; |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | tfm_ns_mailbox_hal_enter_critical_isr(); |
| 317 | replied_status = mailbox_queue_ptr->replied_slots; |
David Hu | 9483037 | 2020-05-13 16:37:34 +0800 | [diff] [blame] | 318 | clear_queue_slot_all_replied(replied_status); |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 319 | tfm_ns_mailbox_hal_exit_critical_isr(); |
| 320 | |
| 321 | if (!replied_status) { |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 322 | return MAILBOX_NO_PEND_EVENT; |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | for (idx = 0; idx < NUM_MAILBOX_QUEUE_SLOT; idx++) { |
David Hu | 9483037 | 2020-05-13 16:37:34 +0800 | [diff] [blame] | 326 | /* |
| 327 | * The reply has already received from SPE mailbox but |
| 328 | * the wake-up signal is not sent yet. |
| 329 | */ |
| 330 | if (!(replied_status & (0x1UL << idx))) { |
| 331 | continue; |
| 332 | } |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 333 | |
David Hu | 9483037 | 2020-05-13 16:37:34 +0800 | [diff] [blame] | 334 | /* Set woken-up flag */ |
| 335 | tfm_ns_mailbox_hal_enter_critical_isr(); |
| 336 | set_queue_slot_woken(idx); |
| 337 | tfm_ns_mailbox_hal_exit_critical_isr(); |
| 338 | |
| 339 | tfm_ns_mailbox_os_wake_task_isr(mailbox_queue_ptr->queue[idx].owner); |
| 340 | |
| 341 | replied_status &= ~(0x1UL << idx); |
| 342 | if (!replied_status) { |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 343 | break; |
David Hu | 9483037 | 2020-05-13 16:37:34 +0800 | [diff] [blame] | 344 | } |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 345 | } |
| 346 | |
David Hu | 1bd1c7b | 2020-05-09 14:13:20 +0800 | [diff] [blame] | 347 | return MAILBOX_SUCCESS; |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 348 | } |
David Hu | 2d2a2f1 | 2020-08-09 15:27:02 +0800 | [diff] [blame^] | 349 | |
| 350 | static inline bool mailbox_wait_reply_signal(uint8_t idx) |
| 351 | { |
| 352 | bool is_set = false; |
| 353 | |
| 354 | ns_mailbox_spin_lock(); |
| 355 | |
| 356 | if (is_queue_slot_woken(idx)) { |
| 357 | clear_queue_slot_woken(idx); |
| 358 | is_set = true; |
| 359 | } |
| 360 | |
| 361 | ns_mailbox_spin_unlock(); |
| 362 | |
| 363 | return is_set; |
| 364 | } |
| 365 | #else /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */ |
| 366 | static inline bool mailbox_wait_reply_signal(uint8_t idx) |
| 367 | { |
| 368 | bool is_set = false; |
| 369 | |
| 370 | tfm_ns_mailbox_hal_enter_critical(); |
| 371 | |
| 372 | if (is_queue_slot_replied(idx)) { |
| 373 | clear_queue_slot_replied(idx); |
| 374 | is_set = true; |
| 375 | } |
| 376 | |
| 377 | tfm_ns_mailbox_hal_exit_critical(); |
| 378 | |
| 379 | return is_set; |
| 380 | } |
| 381 | #endif /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */ |
| 382 | |
| 383 | static int32_t mailbox_wait_reply(uint8_t idx) |
| 384 | { |
| 385 | bool is_replied; |
| 386 | |
| 387 | while (1) { |
| 388 | tfm_ns_mailbox_os_wait_reply(); |
| 389 | |
| 390 | /* |
| 391 | * Woken up from sleep |
| 392 | * Check the completed flag to make sure that the current thread is |
| 393 | * woken up by reply event, rather than other events. |
| 394 | */ |
| 395 | /* |
| 396 | * It requires SVCall to access NS mailbox flags if NS mailbox is put |
| 397 | * in privileged mode. |
| 398 | * An alternative is to let NS thread allocate its own is_woken flag. |
| 399 | * But a spinlock-like mechanism is still required. |
| 400 | */ |
| 401 | is_replied = mailbox_wait_reply_signal(idx); |
| 402 | if (is_replied) { |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | return MAILBOX_SUCCESS; |
| 408 | } |
David Hu | 3684ee7 | 2019-11-12 18:43:34 +0800 | [diff] [blame] | 409 | |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 410 | int32_t tfm_ns_mailbox_init(struct ns_mailbox_queue_t *queue) |
| 411 | { |
| 412 | int32_t ret; |
| 413 | |
| 414 | if (!queue) { |
| 415 | return MAILBOX_INVAL_PARAMS; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Further verification of mailbox queue address may be required according |
| 420 | * to non-secure memory assignment. |
| 421 | */ |
| 422 | |
| 423 | memset(queue, 0, sizeof(*queue)); |
| 424 | |
| 425 | /* Initialize empty bitmask */ |
David Hu | de3f79f | 2019-11-14 16:56:51 +0800 | [diff] [blame] | 426 | queue->empty_slots = |
| 427 | (mailbox_queue_status_t)((1UL << (NUM_MAILBOX_QUEUE_SLOT - 1)) - 1); |
| 428 | queue->empty_slots += |
| 429 | (mailbox_queue_status_t)(1UL << (NUM_MAILBOX_QUEUE_SLOT - 1)); |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 430 | |
| 431 | mailbox_queue_ptr = queue; |
| 432 | |
| 433 | /* Platform specific initialization. */ |
| 434 | ret = tfm_ns_mailbox_hal_init(queue); |
David Hu | 69e590e | 2020-05-12 17:19:21 +0800 | [diff] [blame] | 435 | if (ret != MAILBOX_SUCCESS) { |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | ret = tfm_ns_mailbox_os_lock_init(); |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 440 | |
David Hu | 65cbfb8 | 2019-11-15 17:18:12 +0800 | [diff] [blame] | 441 | #ifdef TFM_MULTI_CORE_TEST |
| 442 | tfm_ns_mailbox_tx_stats_init(); |
| 443 | #endif |
| 444 | |
David Hu | d2753b3 | 2019-09-23 18:46:15 +0800 | [diff] [blame] | 445 | return ret; |
| 446 | } |