Dualcpu: Add general NSPE mailbox wait function

Add tfm_ns_mailbox_wait_reply() to wait for mailbox message reply
from SPE.
tfm_ns_mailbox_wait_reply() calls tfm_ns_mailbox_hal_wait_reply()
to perform platform and NS OS specific waiting mechanism
implemented based on use scenario.

Update the wait function in PSA Client APIs implementations in
dual-core system.
If the system can support multiple outstanding NS PSA Client calls,
call tfm_ns_mailbox_wait_reply() to sleep and wait for reply.
Otherwise, still call tfm_ns_mailbox_is_msg_replied() to simply
poll the reply status of the mailbox message of current thread.

Change-Id: I2a3e808b05d7644465b20f7f4160b9872f2bdb63
Signed-off-by: David Hu <david.hu@arm.com>
diff --git a/interface/src/tfm_multi_core_psa_ns_api.c b/interface/src/tfm_multi_core_psa_ns_api.c
index 25161ce..c99555e 100644
--- a/interface/src/tfm_multi_core_psa_ns_api.c
+++ b/interface/src/tfm_multi_core_psa_ns_api.c
@@ -40,8 +40,19 @@
 
 static void mailbox_wait_reply(mailbox_msg_handle_t handle)
 {
+    /*
+     * If the system can support multiple outstanding NS PSA Client calls, call
+     * tfm_ns_mailbox_wait_reply() to sleep and wait for reply. The NS side
+     * should implement tfm_ns_mailbox_hal_wait_reply() and wake-up mechanism.
+     * Otherwise, by default, call tfm_ns_mailbox_is_msg_replied() to simply
+     * poll the reply status of the mailbox message of current thread.
+     */
+#ifdef TFM_MULTI_CORE_MULTI_CLIENT_CALL
+    tfm_ns_mailbox_wait_reply(handle);
+#else
     while (!tfm_ns_mailbox_is_msg_replied(handle)) {
     }
+#endif
 }
 
 /**** API functions ****/
diff --git a/interface/src/tfm_ns_mailbox.c b/interface/src/tfm_ns_mailbox.c
index 98193e8..a623a7b 100644
--- a/interface/src/tfm_ns_mailbox.c
+++ b/interface/src/tfm_ns_mailbox.c
@@ -71,6 +71,22 @@
     }
 }
 
+static inline bool is_queue_slot_woken(uint8_t idx)
+{
+    if (idx < NUM_MAILBOX_QUEUE_SLOT) {
+        return mailbox_queue_ptr->queue[idx].is_woken;
+    }
+
+    return false;
+}
+
+static inline void clear_queue_slot_woken(uint8_t idx)
+{
+    if (idx < NUM_MAILBOX_QUEUE_SLOT) {
+        mailbox_queue_ptr->queue[idx].is_woken = false;
+    }
+}
+
 static uint8_t acquire_empty_slot(const struct ns_mailbox_queue_t *queue)
 {
     uint8_t idx;
@@ -295,3 +311,42 @@
 
     return ret;
 }
+
+#ifdef TFM_MULTI_CORE_MULTI_CLIENT_CALL
+int32_t tfm_ns_mailbox_wait_reply(mailbox_msg_handle_t handle)
+{
+    uint8_t idx;
+    int32_t ret;
+
+    if (!mailbox_queue_ptr) {
+        return MAILBOX_INVAL_PARAMS;
+    }
+
+    if (handle == MAILBOX_MSG_NULL_HANDLE) {
+        return MAILBOX_INVAL_PARAMS;
+    }
+
+    ret = get_mailbox_msg_idx(handle, &idx);
+    if (ret != MAILBOX_SUCCESS) {
+        return ret;
+    }
+
+    while (1) {
+        tfm_ns_mailbox_hal_wait_reply(handle);
+
+        /*
+         * Woken up from sleep
+         * Check the completed flag to make sure that the current thread is
+         * woken up by reply event, rather than other events.
+         */
+        tfm_ns_mailbox_hal_enter_critical();
+        if (is_queue_slot_woken(idx)) {
+            tfm_ns_mailbox_hal_exit_critical();
+            break;
+        }
+        tfm_ns_mailbox_hal_exit_critical();
+    }
+
+    return MAILBOX_SUCCESS;
+}
+#endif