Dualcpu: Move NS mailbox thread management to RTOS specific file

The NS mailbox thread management is NS RTOS specific.

- Add a new tfm_ns_mailbox_rtos_api.c to collect all the NS mailbox
  APIs which rely on RTOS specific implementations.
- Move those API implementations from platform to
  tfm_ns_mailbox_rtos_api.c.
- Rename those APIs to replace HAL keyword with OS keyword, to
  indicate that their implementations are RTOS specific.

Change-Id: Ic2885bc1676964719d1524b39d6518444610e1aa
Signed-off-by: David Hu <david.hu@arm.com>
diff --git a/cmake/install.cmake b/cmake/install.cmake
index dd1ffc8..cb7f100 100644
--- a/cmake/install.cmake
+++ b/cmake/install.cmake
@@ -113,8 +113,9 @@
 
 if (TFM_MULTI_CORE_TOPOLOGY)
     install(FILES       ${INTERFACE_SRC_DIR}/tfm_ns_mailbox.c
-                        ${INTERFACE_SRC_DIR}/tfm_multi_core_api.c
+                        ${INTERFACE_SRC_DIR}/tfm_multi_core_ns_api.c
                         ${INTERFACE_SRC_DIR}/tfm_multi_core_psa_ns_api.c
+                        ${INTERFACE_SRC_DIR}/tfm_ns_mailbox_rtos_api.c
             DESTINATION ${INSTALL_INTERFACE_SRC_DIR})
 else()
     install(FILES       ${INTERFACE_SRC_DIR}/tfm_ns_interface.c
diff --git a/interface/include/tfm_multi_core_api.h b/interface/include/tfm_multi_core_api.h
index 908b137..752cf96 100644
--- a/interface/include/tfm_multi_core_api.h
+++ b/interface/include/tfm_multi_core_api.h
@@ -14,8 +14,6 @@
 
 #include <stdint.h>
 
-#include "os_wrapper/common.h"
-
 /**
  * \brief Called on the non-secure CPU.
  *        Flags that the non-secure side has completed its initialization.
@@ -38,36 +36,6 @@
  */
 int32_t tfm_platform_ns_wait_for_s_cpu_ready(void);
 
-#ifdef FORWARD_PROT_MSG
-static inline uint32_t tfm_ns_multi_core_lock_acquire(void)
-{
-    return OS_WRAPPER_SUCCESS;
-}
-
-static inline uint32_t tfm_ns_multi_core_lock_release(void)
-{
-    return OS_WRAPPER_SUCCESS;
-}
-#else /* FORWARD_PROT_MSG */
-/**
- * \brief Acquire the multi-core lock for synchronizing PSA client call(s)
- *        The actual implementation depends on the use scenario.
- *
- * \return \ref OS_WRAPPER_SUCCESS on success
- * \return \ref OS_WRAPPER_ERROR on error
- */
-uint32_t tfm_ns_multi_core_lock_acquire(void);
-
-/**
- * \brief Release the multi-core lock for synchronizing PSA client call(s)
- *        The actual implementation depends on the use scenario.
- *
- * \return \ref OS_WRAPPER_SUCCESS on success
- * \return \ref OS_WRAPPER_ERROR on error
- */
-uint32_t tfm_ns_multi_core_lock_release(void);
-#endif /* FORWARD_PROT_MSG */
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/interface/include/tfm_ns_mailbox.h b/interface/include/tfm_ns_mailbox.h
index bdca18c..a2902d6 100644
--- a/interface/include/tfm_ns_mailbox.h
+++ b/interface/include/tfm_ns_mailbox.h
@@ -64,26 +64,6 @@
                                    int32_t client_id,
                                    int32_t *reply);
 
-#ifdef TFM_MULTI_CORE_MULTI_CLIENT_CALL
-/**
- * \brief Get the handle of the current non-secure task executing mailbox
- *        functionalities
- *
- * \note This function should be implemented according to platform, NS OS
- *       and actual use scenario.
- *       This function can be ignored or return NULL if sleep/wake-up mechanism
- *       is not required in PSA Client API implementation.
- *
- * \return Return the handle of task.
- */
-const void *tfm_ns_mailbox_get_task_handle(void);
-#else
-static inline const void *tfm_ns_mailbox_get_task_handle(void)
-{
-    return NULL;
-}
-#endif
-
 /**
  * \brief Wake up the owner task of the first replied mailbox message in the
  *        NSPE mailbox queue.
@@ -152,38 +132,102 @@
  */
 void tfm_ns_mailbox_hal_exit_critical_isr(void);
 
+#ifdef FORWARD_PROT_MSG
+static inline int32_t tfm_ns_mailbox_os_lock_init(void)
+{
+    return MAILBOX_SUCCESS;
+}
+
+static inline uint32_t tfm_ns_mailbox_os_lock_acquire(void)
+{
+    return MAILBOX_SUCCESS;
+}
+
+static inline uint32_t tfm_ns_mailbox_os_lock_release(void)
+{
+    return MAILBOX_SUCCESS;
+}
+#else /* FORWARD_PROT_MSG */
+/**
+ * \brief Initialize the multi-core lock for synchronizing PSA client call(s)
+ *        The actual implementation depends on the non-secure use scenario.
+ *
+ * \return \ref MAILBOX_SUCCESS on success
+ * \return \ref MAILBOX_GENERIC_ERROR on error
+ */
+int32_t tfm_ns_mailbox_os_lock_init(void);
+
+/**
+ * \brief Acquire the multi-core lock for synchronizing PSA client call(s)
+ *        The actual implementation depends on the non-secure use scenario.
+ *
+ * \return \ref MAILBOX_SUCCESS on success
+ * \return \ref MAILBOX_GENERIC_ERROR on error
+ */
+int32_t tfm_ns_mailbox_os_lock_acquire(void);
+
+/**
+ * \brief Release the multi-core lock for synchronizing PSA client call(s)
+ *        The actual implementation depends on the non-secure use scenario.
+ *
+ * \return \ref MAILBOX_SUCCESS on success
+ * \return \ref MAILBOX_GENERIC_ERROR on error
+ */
+int32_t tfm_ns_mailbox_os_lock_release(void);
+#endif /* FORWARD_PROT_MSG */
+
 #ifdef TFM_MULTI_CORE_MULTI_CLIENT_CALL
 /**
- * \brief Performs platform and NS OS specific waiting mechanism to wait for
+ * \brief Get the handle of the current non-secure task executing mailbox
+ *        functionalities
+ *
+ * \note This function should be implemented according to NS OS and
+ *       actual use scenario.
+ *       This function can be ignored or return NULL if sleep/wake-up mechanism
+ *       is not required in PSA Client API implementation.
+ *
+ * \return Return the handle of task.
+ */
+const void *tfm_ns_mailbox_os_get_task_handle(void);
+
+/**
+ * \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.
  *
- * \note This function is implemented by platform and NS OS specific waiting
- *       mechanism according to use scenario.
+ * \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_hal_wait_reply(mailbox_msg_handle_t handle);
+void tfm_ns_mailbox_os_wait_reply(mailbox_msg_handle_t handle);
 
 /*
- * \brief Performs platform and NS OS specific mechanism in a mailbox IRQ
+ * \brief Performs use scenario and NS OS specific mechanism in a mailbox IRQ
  *        handler, to wake up a sleeping task which is waiting for its mailbox
  *        message reply.
  *
- * \note The underlying platform and NS OS specific function called inside this
- *       function should be able to work in an IRQ handler.
+ * \note The underlying NS OS specific function called inside this function
+ *       should be able to work in an IRQ handler.
  *
- * \note This function is implemented by platform and NS OS specific waiting
+ * \note This function is implemented by NS OS specific waiting
  *       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_hal_wake_task_isr(const void *task_handle,
-                                      mailbox_msg_handle_t handle);
-#else
-#define tfm_ns_mailbox_hal_wait_reply(handle)        do {} while (0)
-#endif
+void tfm_ns_mailbox_os_wake_task_isr(const void *task_handle,
+                                     mailbox_msg_handle_t handle);
+#else /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */
+#define tfm_ns_mailbox_os_wait_reply(handle)        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)
+#endif /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */
 
 #ifdef TFM_MULTI_CORE_TEST
 /**
diff --git a/interface/src/tfm_multi_core_api.c b/interface/src/tfm_multi_core_api.c
deleted file mode 100644
index c3deb50..0000000
--- a/interface/src/tfm_multi_core_api.c
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2019, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#include "os_wrapper/semaphore.h"
-
-#include "tfm_api.h"
-#include "tfm_mailbox.h"
-#include "tfm_multi_core_api.h"
-
-#define MAX_SEMAPHORE_COUNT            NUM_MAILBOX_QUEUE_SLOT
-
-static void *ns_lock_handle = NULL;
-
-__attribute__((weak))
-enum tfm_status_e tfm_ns_interface_init(void)
-{
-    ns_lock_handle = os_wrapper_semaphore_create(MAX_SEMAPHORE_COUNT,
-                                                 MAX_SEMAPHORE_COUNT,
-                                                 NULL);
-    if (!ns_lock_handle) {
-        return TFM_ERROR_GENERIC;
-    }
-
-    return TFM_SUCCESS;
-}
-
-int32_t tfm_ns_wait_for_s_cpu_ready(void)
-{
-    return tfm_platform_ns_wait_for_s_cpu_ready();
-}
-
-uint32_t tfm_ns_multi_core_lock_acquire(void)
-{
-    return os_wrapper_semaphore_acquire(ns_lock_handle,
-                                        OS_WRAPPER_WAIT_FOREVER);
-}
-
-uint32_t tfm_ns_multi_core_lock_release(void)
-{
-    return os_wrapper_semaphore_release(ns_lock_handle);
-}
diff --git a/interface/src/tfm_multi_core_ns_api.c b/interface/src/tfm_multi_core_ns_api.c
new file mode 100644
index 0000000..ce9233c
--- /dev/null
+++ b/interface/src/tfm_multi_core_ns_api.c
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2019-2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "tfm_api.h"
+#include "tfm_multi_core_api.h"
+
+__attribute__((weak))
+enum tfm_status_e tfm_ns_interface_init(void)
+{
+    return TFM_SUCCESS;
+}
+
+int32_t tfm_ns_wait_for_s_cpu_ready(void)
+{
+    return tfm_platform_ns_wait_for_s_cpu_ready();
+}
diff --git a/interface/src/tfm_ns_mailbox.c b/interface/src/tfm_ns_mailbox.c
index 4e44fae..b256fa2 100644
--- a/interface/src/tfm_ns_mailbox.c
+++ b/interface/src/tfm_ns_mailbox.c
@@ -8,8 +8,6 @@
 #include <string.h>
 
 #include "cmsis_compiler.h"
-#include "os_wrapper/common.h"
-#include "tfm_multi_core_api.h"
 #include "tfm_ns_mailbox.h"
 
 /* The pointer to NSPE mailbox queue */
@@ -234,7 +232,7 @@
      * Fetch the current task handle. The task will be woken up according the
      * handle value set in the owner field.
      */
-    task_handle = tfm_ns_mailbox_get_task_handle();
+    task_handle = tfm_ns_mailbox_os_get_task_handle();
     set_msg_owner(idx, task_handle);
 
     get_mailbox_msg_handle(idx, handle);
@@ -297,7 +295,7 @@
         return MAILBOX_INVAL_PARAMS;
     }
 
-    if (tfm_ns_multi_core_lock_acquire() != OS_WRAPPER_SUCCESS) {
+    if (tfm_ns_mailbox_os_lock_acquire() != MAILBOX_SUCCESS) {
         return MAILBOX_QUEUE_FULL;
     }
 
@@ -316,7 +314,7 @@
     }
 
 exit:
-    if (tfm_ns_multi_core_lock_release() != OS_WRAPPER_SUCCESS) {
+    if (tfm_ns_mailbox_os_lock_release() != MAILBOX_SUCCESS) {
         return MAILBOX_GENERIC_ERROR;
     }
 
@@ -365,8 +363,8 @@
         return ret;
     }
 
-    tfm_ns_mailbox_hal_wake_task_isr(mailbox_queue_ptr->queue[idx].owner,
-                                     handle);
+    tfm_ns_mailbox_os_wake_task_isr(mailbox_queue_ptr->queue[idx].owner,
+                                    handle);
 
     return MAILBOX_SUCCESS;
 }
@@ -397,6 +395,11 @@
 
     /* Platform specific initialization. */
     ret = tfm_ns_mailbox_hal_init(queue);
+    if (ret != MAILBOX_SUCCESS) {
+        return ret;
+    }
+
+    ret = tfm_ns_mailbox_os_lock_init();
 
 #ifdef TFM_MULTI_CORE_TEST
     tfm_ns_mailbox_tx_stats_init();
@@ -420,7 +423,7 @@
     }
 
     while (1) {
-        tfm_ns_mailbox_hal_wait_reply(handle);
+        tfm_ns_mailbox_os_wait_reply(handle);
 
         /*
          * Woken up from sleep
diff --git a/interface/src/tfm_ns_mailbox_rtos_api.c b/interface/src/tfm_ns_mailbox_rtos_api.c
new file mode 100644
index 0000000..1e9e8d2
--- /dev/null
+++ b/interface/src/tfm_ns_mailbox_rtos_api.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+/*
+ * This file is a reference implementation of Non-secure mailbox RTOS API.
+ * This reference implementation is based on TF-M ROTS wrapper API.
+ * It can be replaced by RTOS specific implementation.
+ */
+
+#include "os_wrapper/semaphore.h"
+#include "os_wrapper/thread.h"
+
+#include "tfm_ns_mailbox.h"
+
+#define MAX_SEMAPHORE_COUNT            NUM_MAILBOX_QUEUE_SLOT
+
+static void *ns_lock_handle = NULL;
+
+#ifdef TFM_MULTI_CORE_MULTI_CLIENT_CALL
+const void *tfm_ns_mailbox_os_get_task_handle(void)
+{
+    return os_wrapper_thread_get_handle();
+}
+
+void tfm_ns_mailbox_os_wait_reply(mailbox_msg_handle_t handle)
+{
+    os_wrapper_thread_wait_flag((uint32_t)handle, OS_WRAPPER_WAIT_FOREVER);
+}
+
+void tfm_ns_mailbox_os_wake_task_isr(const void *task_handle,
+                                     mailbox_msg_handle_t handle)
+{
+    os_wrapper_thread_set_flag_isr((void *)task_handle, (uint32_t)handle);
+}
+#endif /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */
+
+int32_t tfm_ns_mailbox_os_lock_init(void)
+{
+    ns_lock_handle = os_wrapper_semaphore_create(MAX_SEMAPHORE_COUNT,
+                                                 MAX_SEMAPHORE_COUNT,
+                                                 NULL);
+    if (!ns_lock_handle) {
+        return MAILBOX_GENERIC_ERROR;
+    }
+
+    return MAILBOX_SUCCESS;
+}
+
+int32_t tfm_ns_mailbox_os_lock_acquire(void)
+{
+    return os_wrapper_semaphore_acquire(ns_lock_handle,
+                                        OS_WRAPPER_WAIT_FOREVER);
+}
+
+int32_t tfm_ns_mailbox_os_lock_release(void)
+{
+    return os_wrapper_semaphore_release(ns_lock_handle);
+}
diff --git a/platform/ext/target/cypress/psoc64/mailbox/platform_ns_mailbox.c b/platform/ext/target/cypress/psoc64/mailbox/platform_ns_mailbox.c
index 6a616e0..23b1b37 100644
--- a/platform/ext/target/cypress/psoc64/mailbox/platform_ns_mailbox.c
+++ b/platform/ext/target/cypress/psoc64/mailbox/platform_ns_mailbox.c
@@ -98,22 +98,6 @@
     return MAILBOX_SUCCESS;
 }
 
-const void *tfm_ns_mailbox_get_task_handle(void)
-{
-    return os_wrapper_thread_get_handle();
-}
-
-void tfm_ns_mailbox_hal_wait_reply(mailbox_msg_handle_t handle)
-{
-    os_wrapper_thread_wait_flag((uint32_t)handle, OS_WRAPPER_WAIT_FOREVER);
-}
-
-void tfm_ns_mailbox_hal_wake_task_isr(const void *task_handle,
-                                      mailbox_msg_handle_t handle)
-{
-    os_wrapper_thread_set_flag_isr((void *)task_handle, (uint32_t)handle);
-}
-
 void tfm_ns_mailbox_hal_enter_critical(void)
 {
     saved_irq_state = Cy_SysLib_EnterCriticalSection();