Dualcpu: Remove mailbox message handle from NS mailbox

It is unnecessary to export message handle mailbox_msg_handle_t
to applications outside NS mailbox.

- Remove message handles from NS mailbox APIs. NS applications can
  define own thread flags to manage mailbox wait/wake mechanism.
- Remove message handles from NS mailbox reference implementation.
  Remove translation between handles and NS mailbox queue slot
  index as well.
- Move mailbox_msg_handle_t definitions to SPE mailbox header file.

Change-Id: Ic4bb5e5aebc29d0424ad2332af749f2bf59e8ebd
Signed-off-by: David Hu <david.hu@arm.com>
diff --git a/interface/include/tfm_mailbox.h b/interface/include/tfm_mailbox.h
index 71f8ae7..45eb97e 100644
--- a/interface/include/tfm_mailbox.h
+++ b/interface/include/tfm_mailbox.h
@@ -119,11 +119,6 @@
                                             */
 };
 
-/* A handle to a mailbox message in use */
-typedef int32_t    mailbox_msg_handle_t;
-
-#define MAILBOX_MSG_NULL_HANDLE          ((mailbox_msg_handle_t)0)
-
 /*
  * Mailbox reply structure in non-secure memory
  * to hold the PSA client call return result from SPE
diff --git a/interface/include/tfm_ns_mailbox.h b/interface/include/tfm_ns_mailbox.h
index a2902d6..114000f 100644
--- a/interface/include/tfm_ns_mailbox.h
+++ b/interface/include/tfm_ns_mailbox.h
@@ -192,14 +192,12 @@
 
 /**
  * \brief Performs use scenario and NS OS specific waiting mechanism to wait for
- *        the reply of the specified mailbox message to be returned from SPE.
+ *        the reply to be returned from SPE.
  *
  * \note This function is implemented by NS OS specific waiting mechanism
  *       according to use scenario.
- *
- * \param[in] handle            The handle of mailbox message.
  */
-void tfm_ns_mailbox_os_wait_reply(mailbox_msg_handle_t handle);
+void tfm_ns_mailbox_os_wait_reply(void);
 
 /*
  * \brief Performs use scenario and NS OS specific mechanism in a mailbox IRQ
@@ -213,20 +211,17 @@
  *       mechanism according to use scenario.
  *
  * \param[in] task_handle       The handle to the task to be woken up.
- * \param[in] handle            The mailbox handle which can be used as thread
- *                              flag.
  */
-void tfm_ns_mailbox_os_wake_task_isr(const void *task_handle,
-                                     mailbox_msg_handle_t handle);
+void tfm_ns_mailbox_os_wake_task_isr(const void *task_handle);
 #else /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */
-#define tfm_ns_mailbox_os_wait_reply(handle)        do {} while (0)
+#define tfm_ns_mailbox_os_wait_reply()         do {} while (0)
 
 static inline const void *tfm_ns_mailbox_os_get_task_handle(void)
 {
     return NULL;
 }
 
-#define tfm_ns_mailbox_os_wake_task_isr(task, handle)    do {} while (0)
+#define tfm_ns_mailbox_os_wake_task_isr(task)  do {} while (0)
 #endif /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */
 
 #ifdef TFM_MULTI_CORE_TEST
diff --git a/interface/src/tfm_ns_mailbox.c b/interface/src/tfm_ns_mailbox.c
index b256fa2..f8f4298 100644
--- a/interface/src/tfm_ns_mailbox.c
+++ b/interface/src/tfm_ns_mailbox.c
@@ -13,7 +13,7 @@
 /* The pointer to NSPE mailbox queue */
 static struct ns_mailbox_queue_t *mailbox_queue_ptr = NULL;
 
-static int32_t mailbox_wait_reply(mailbox_msg_handle_t handle);
+static int32_t mailbox_wait_reply(uint8_t idx);
 
 static inline void clear_queue_slot_empty(uint8_t idx)
 {
@@ -36,30 +36,6 @@
     }
 }
 
-static int32_t get_mailbox_msg_handle(uint8_t idx,
-                                      mailbox_msg_handle_t *handle)
-{
-    if ((idx >= NUM_MAILBOX_QUEUE_SLOT) || !handle) {
-        return MAILBOX_INVAL_PARAMS;
-    }
-
-    *handle = (mailbox_msg_handle_t)(idx + 1);
-
-    return MAILBOX_SUCCESS;
-}
-
-static inline int32_t get_mailbox_msg_idx(mailbox_msg_handle_t handle,
-                                          uint8_t *idx)
-{
-    if ((handle == MAILBOX_MSG_NULL_HANDLE) || !idx) {
-        return MAILBOX_INVAL_PARAMS;
-    }
-
-    *idx = (uint8_t)(handle - 1);
-
-    return MAILBOX_SUCCESS;
-}
-
 static inline void clear_queue_slot_replied(uint8_t idx)
 {
     if (idx < NUM_MAILBOX_QUEUE_SLOT) {
@@ -206,7 +182,7 @@
 static int32_t mailbox_tx_client_req(uint32_t call_type,
                                      const struct psa_client_params_t *params,
                                      int32_t client_id,
-                                     mailbox_msg_handle_t *handle)
+                                     uint8_t *slot_idx)
 {
     uint8_t idx;
     struct mailbox_msg_t *msg_ptr;
@@ -235,32 +211,19 @@
     task_handle = tfm_ns_mailbox_os_get_task_handle();
     set_msg_owner(idx, task_handle);
 
-    get_mailbox_msg_handle(idx, handle);
-
     tfm_ns_mailbox_hal_enter_critical();
     set_queue_slot_pend(idx);
     tfm_ns_mailbox_hal_exit_critical();
 
     tfm_ns_mailbox_hal_notify_peer();
 
+    *slot_idx = idx;
+
     return MAILBOX_SUCCESS;
 }
 
-static int32_t mailbox_rx_client_reply(mailbox_msg_handle_t handle,
-                                       int32_t *reply)
+static int32_t mailbox_rx_client_reply(uint8_t idx, int32_t *reply)
 {
-    uint8_t idx;
-    int32_t ret;
-
-    if ((handle == MAILBOX_MSG_NULL_HANDLE) || (!reply)) {
-        return MAILBOX_INVAL_PARAMS;
-    }
-
-    ret = get_mailbox_msg_idx(handle, &idx);
-    if (ret != MAILBOX_SUCCESS) {
-        return ret;
-    }
-
     *reply = mailbox_queue_ptr->queue[idx].reply.return_val;
 
     /* Clear up the owner field */
@@ -283,7 +246,7 @@
                                    int32_t client_id,
                                    int32_t *reply)
 {
-    mailbox_msg_handle_t handle = MAILBOX_MSG_NULL_HANDLE;
+    uint8_t slot_idx = NUM_MAILBOX_QUEUE_SLOT;
     int32_t reply_buf = 0x0;
     int32_t ret;
 
@@ -300,15 +263,15 @@
     }
 
     /* It requires SVCall if NS mailbox is put in privileged mode. */
-    ret = mailbox_tx_client_req(call_type, params, client_id, &handle);
+    ret = mailbox_tx_client_req(call_type, params, client_id, &slot_idx);
     if (ret != MAILBOX_SUCCESS) {
         goto exit;
     }
 
-    mailbox_wait_reply(handle);
+    mailbox_wait_reply(slot_idx);
 
     /* It requires SVCall if NS mailbox is put in privileged mode. */
-    ret = mailbox_rx_client_reply(handle, &reply_buf);
+    ret = mailbox_rx_client_reply(slot_idx, &reply_buf);
     if (ret == MAILBOX_SUCCESS) {
         *reply = reply_buf;
     }
@@ -325,8 +288,6 @@
 int32_t tfm_ns_mailbox_wake_reply_owner_isr(void)
 {
     uint8_t idx;
-    int32_t ret;
-    mailbox_msg_handle_t handle;
     mailbox_queue_status_t replied_status;
 
     if (!mailbox_queue_ptr) {
@@ -354,17 +315,11 @@
     }
 
     /* In theory, it won't occur. Just in case */
-    if (idx == NUM_MAILBOX_QUEUE_SLOT) {
+    if (idx >= NUM_MAILBOX_QUEUE_SLOT) {
         return MAILBOX_NO_PEND_EVENT;
     }
 
-    ret = get_mailbox_msg_handle(idx, &handle);
-    if (ret != MAILBOX_SUCCESS) {
-        return ret;
-    }
-
-    tfm_ns_mailbox_os_wake_task_isr(mailbox_queue_ptr->queue[idx].owner,
-                                    handle);
+    tfm_ns_mailbox_os_wake_task_isr(mailbox_queue_ptr->queue[idx].owner);
 
     return MAILBOX_SUCCESS;
 }
@@ -408,22 +363,10 @@
     return ret;
 }
 
-static int32_t mailbox_wait_reply(mailbox_msg_handle_t handle)
+static int32_t mailbox_wait_reply(uint8_t idx)
 {
-    uint8_t idx;
-    int32_t ret;
-
-    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_os_wait_reply(handle);
+        tfm_ns_mailbox_os_wait_reply();
 
         /*
          * Woken up from sleep
diff --git a/interface/src/tfm_ns_mailbox_rtos_api.c b/interface/src/tfm_ns_mailbox_rtos_api.c
index 1e9e8d2..f7d23d2 100644
--- a/interface/src/tfm_ns_mailbox_rtos_api.c
+++ b/interface/src/tfm_ns_mailbox_rtos_api.c
@@ -18,6 +18,14 @@
 
 #define MAX_SEMAPHORE_COUNT            NUM_MAILBOX_QUEUE_SLOT
 
+/*
+ * Thread flag to manage wait/wake mechanism in mailbox.、
+ * Thread flag can be RTOS specific.
+ * The following example definition also covers the rule of CMSIS-RTOS2, which
+ * requires the MSB of thread flags must be 0b0.
+ */
+#define MAILBOX_THREAD_FLAG            0x5FCA0000
+
 static void *ns_lock_handle = NULL;
 
 #ifdef TFM_MULTI_CORE_MULTI_CLIENT_CALL
@@ -26,15 +34,14 @@
     return os_wrapper_thread_get_handle();
 }
 
-void tfm_ns_mailbox_os_wait_reply(mailbox_msg_handle_t handle)
+void tfm_ns_mailbox_os_wait_reply(void)
 {
-    os_wrapper_thread_wait_flag((uint32_t)handle, OS_WRAPPER_WAIT_FOREVER);
+    os_wrapper_thread_wait_flag(MAILBOX_THREAD_FLAG, OS_WRAPPER_WAIT_FOREVER);
 }
 
-void tfm_ns_mailbox_os_wake_task_isr(const void *task_handle,
-                                     mailbox_msg_handle_t handle)
+void tfm_ns_mailbox_os_wake_task_isr(const void *task_handle)
 {
-    os_wrapper_thread_set_flag_isr((void *)task_handle, (uint32_t)handle);
+    os_wrapper_thread_set_flag_isr((void *)task_handle, MAILBOX_THREAD_FLAG);
 }
 #endif /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */