Dualcpu: Refine NS mailbox header files

- Move NS mailbox test structure and API into a dedicated header
  file tfm_ns_mailbox_test.h.
- Replace ns_mailbox_spin_lock()/unlock() with
  tfm_ns_mailbox_os_spin_lock()/unlock(), which are implemented by RTOS
  specific implementation. It decouples NS mailbox from CMSIS.

Change-Id: I22e8ce1df849529066429dda0640772a68161c60
Signed-off-by: David Hu <david.hu@arm.com>
diff --git a/cmake/install.cmake b/cmake/install.cmake
index d071b15..f7fa5bf 100644
--- a/cmake/install.cmake
+++ b/cmake/install.cmake
@@ -49,6 +49,7 @@
     install(FILES       ${INTERFACE_INC_DIR}/multi_core/tfm_multi_core_api.h
                         ${INTERFACE_INC_DIR}/multi_core/tfm_ns_mailbox.h
                         ${INTERFACE_INC_DIR}/multi_core/tfm_mailbox.h
+                        ${INTERFACE_INC_DIR}/multi_core/tfm_ns_mailbox_test.h
                         ${CMAKE_BINARY_DIR}/generated/interface/include/tfm_mailbox_config.h
             DESTINATION ${INSTALL_INTERFACE_INC_DIR})
 elseif (NOT TFM_PSA_API)
diff --git a/interface/include/multi_core/tfm_ns_mailbox.h b/interface/include/multi_core/tfm_ns_mailbox.h
index 7ba15bf..85961f7 100644
--- a/interface/include/multi_core/tfm_ns_mailbox.h
+++ b/interface/include/multi_core/tfm_ns_mailbox.h
@@ -13,7 +13,6 @@
 #include <stdbool.h>
 #include <stdint.h>
 
-#include "cmsis_compiler.h"
 #include "tfm_mailbox.h"
 
 #ifdef __cplusplus
@@ -24,22 +23,6 @@
 #error "NUM_MAILBOX_QUEUE_SLOT should be set to 1 for NS bare metal environment"
 #endif
 
-#ifdef TFM_MULTI_CORE_TEST
-/**
- * \brief The structure to hold the statistics result of NSPE mailbox
- */
-struct ns_mailbox_stats_res_t {
-    uint8_t avg_nr_slots;               /* The value before the decimal point
-                                         * in the average number of NSPE
-                                         * mailbox slots in use.
-                                         */
-    uint8_t avg_nr_slots_tenths;        /* The first digit value after the
-                                         * decimal point in the average
-                                         * number of NSPE mailbox slots in use.
-                                         */
-};
-#endif
-
 /**
  * \brief NSPE mailbox initialization
  *
@@ -70,26 +53,6 @@
                                    int32_t client_id,
                                    int32_t *reply);
 
-#ifdef TFM_MULTI_CORE_NS_OS
-/**
- * \brief Go through mailbox messages already replied by SPE mailbox and
- *        wake up the owner tasks of replied mailbox messages.
- *        This function is intended to be called inside platform specific
- *        notification IRQ handler.
- *
- * \return MAILBOX_SUCCESS       The tasks of replied mailbox messages
- *                               were found and wake-up signals were sent.
- * \return MAILBOX_NO_PEND_EVENT No replied mailbox message is found.
- * \return Other return code     Failed with an error code
- */
-int32_t tfm_ns_mailbox_wake_reply_owner_isr(void);
-#else
-static inline int32_t tfm_ns_mailbox_wake_reply_owner_isr(void)
-{
-    return MAILBOX_NO_PEND_EVENT;
-}
-#endif
-
 #ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
 /**
  * \brief Handling PSA client calls in a dedicated NS mailbox thread.
@@ -259,6 +222,31 @@
  *       queue, unless a fatal error occurs.
  */
 int32_t tfm_ns_mailbox_os_mq_receive(void *mq_handle, void *msg_ptr);
+
+/**
+ * \brief Go through mailbox messages already replied by SPE mailbox and
+ *        wake up the owner tasks of replied mailbox messages.
+ *        This function is intended to be called inside platform specific
+ *        notification IRQ handler.
+ *
+ * \return MAILBOX_SUCCESS       The tasks of replied mailbox messages
+ *                               were found and wake-up signals were sent.
+ * \return MAILBOX_NO_PEND_EVENT No replied mailbox message is found.
+ * \return Other return code     Failed with an error code
+ */
+int32_t tfm_ns_mailbox_wake_reply_owner_isr(void);
+
+/**
+ * \brief NSPE local mailbox spin lock.
+ *        Implemented by NS RTOS specific implementation.
+ */
+void tfm_ns_mailbox_os_spin_lock(void);
+
+/**
+ * \brief NSPE local mailbox spin unlock.
+ *        Implemented by NS RTOS specific implementation.
+ */
+void tfm_ns_mailbox_os_spin_unlock(void);
 #else /* TFM_MULTI_CORE_NS_OS */
 #define tfm_ns_mailbox_os_wait_reply()         do {} while (0)
 
@@ -283,77 +271,19 @@
 {
     return NULL;
 }
-#endif /* TFM_MULTI_CORE_NS_OS */
 
-#ifdef TFM_MULTI_CORE_TEST
-/**
- * \brief Initialize the statistics module in TF-M NSPE mailbox.
- *
- * \note This function is only available when multi-core tests are enabled.
- *
- * \param[in] ns_queue          The NSPE mailbox queue to be tracked.
- */
-void tfm_ns_mailbox_tx_stats_init(struct ns_mailbox_queue_t *ns_queue);
-
-/**
- * \brief Re-initialize the statistics module in TF-M NSPE mailbox.
- *        Clean up statistics data.
- *
- * \note This function is only available when multi-core tests are enabled.
- *
- * \return \ref MAILBOX_SUCCESS if the operation succeeded, or other return code
-           in case of error
- */
-int32_t tfm_ns_mailbox_tx_stats_reinit(void);
-
-/**
- * \brief Update the statistics result of NSPE mailbox message transmission.
- *
- * \note This function is only available when multi-core tests are enabled.
- */
-void tfm_ns_mailbox_tx_stats_update(void);
-
-/**
- * \brief Calculate the average number of used NS mailbox queue slots each time
- *        NS task requires a queue slot to submit mailbox message, which is
- *        recorded in NS mailbox statisitics module.
- *
- * \note This function is only available when multi-core tests are enabled.
- *
- * \param[in] stats_res         The buffer to be written with
- *                              \ref ns_mailbox_stats_res_t.
- *
- * \return Return the calculation result.
- */
-void tfm_ns_mailbox_stats_avg_slot(struct ns_mailbox_stats_res_t *stats_res);
-#endif
-
-#ifdef TFM_MULTI_CORE_NS_OS
-/*
- * When NSPE mailbox only covers a single non-secure core, spinlock only
- * requires to disable IRQ.
- */
-static inline void ns_mailbox_spin_lock(void)
+static inline int32_t tfm_ns_mailbox_wake_reply_owner_isr(void)
 {
-    __disable_irq();
+    return MAILBOX_NO_PEND_EVENT;
 }
 
 /*
- * It is assumed that IRQ is always enabled when spinlock is acquired.
- * Otherwise, the waiting thread won't be woken up.
- */
-static inline void ns_mailbox_spin_unlock(void)
-{
-    __enable_irq();
-}
-#else /* TFM_MULTI_CORE_NS_OS */
-/*
  * Local spinlock is implemented as a dummy one when integrating with NS bare
  * metal environment since interrupt is not required in NS mailbox.
  */
-#define ns_mailbox_spin_lock()   do {} while (0)
+#define tfm_ns_mailbox_os_spin_lock()   do {} while (0)
 
-#define ns_mailbox_spin_unlock() do {} while (0)
+#define tfm_ns_mailbox_os_spin_unlock() do {} while (0)
 #endif /* TFM_MULTI_CORE_NS_OS */
 
 /* The following inline functions configure non-secure mailbox queue status */
diff --git a/interface/include/multi_core/tfm_ns_mailbox_test.h b/interface/include/multi_core/tfm_ns_mailbox_test.h
new file mode 100644
index 0000000..c3c2db9
--- /dev/null
+++ b/interface/include/multi_core/tfm_ns_mailbox_test.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+/* Data types and API definitions in test of NSPE mailbox */
+
+#ifndef __TFM_NS_MAILBOX_TEST_H__
+#define __TFM_NS_MAILBOX_TEST_H__
+
+#include <stdint.h>
+
+#include "tfm_ns_mailbox.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief The structure to hold the statistics result of NSPE mailbox
+ */
+struct ns_mailbox_stats_res_t {
+    uint8_t avg_nr_slots;               /* The value before the decimal point
+                                         * in the average number of NSPE
+                                         * mailbox slots in use.
+                                         */
+    uint8_t avg_nr_slots_tenths;        /* The first digit value after the
+                                         * decimal point in the average
+                                         * number of NSPE mailbox slots in use.
+                                         */
+};
+
+/**
+ * \brief Initialize the statistics module in TF-M NSPE mailbox.
+ *
+ * \note This function is only available when multi-core tests are enabled.
+ *
+ * \param[in] ns_queue          The NSPE mailbox queue to be tracked.
+ */
+void tfm_ns_mailbox_tx_stats_init(struct ns_mailbox_queue_t *ns_queue);
+
+/**
+ * \brief Re-initialize the statistics module in TF-M NSPE mailbox.
+ *        Clean up statistics data.
+ *
+ * \note This function is only available when multi-core tests are enabled.
+ *
+ * \return \ref MAILBOX_SUCCESS if the operation succeeded, or other return code
+           in case of error
+ */
+int32_t tfm_ns_mailbox_tx_stats_reinit(void);
+
+/**
+ * \brief Update the statistics result of NSPE mailbox message transmission.
+ *
+ * \note This function is only available when multi-core tests are enabled.
+ */
+void tfm_ns_mailbox_tx_stats_update(void);
+
+/**
+ * \brief Calculate the average number of used NS mailbox queue slots each time
+ *        NS task requires a queue slot to submit mailbox message, which is
+ *        recorded in NS mailbox statisitics module.
+ *
+ * \note This function is only available when multi-core tests are enabled.
+ *
+ * \param[in] stats_res         The buffer to be written with
+ *                              \ref ns_mailbox_stats_res_t.
+ *
+ * \return Return the calculation result.
+ */
+void tfm_ns_mailbox_stats_avg_slot(struct ns_mailbox_stats_res_t *stats_res);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __TFM_NS_MAILBOX_TEST_H__ */
diff --git a/interface/src/multi_core/tfm_ns_mailbox.c b/interface/src/multi_core/tfm_ns_mailbox.c
index 056f34a..2eece31 100644
--- a/interface/src/multi_core/tfm_ns_mailbox.c
+++ b/interface/src/multi_core/tfm_ns_mailbox.c
@@ -8,6 +8,9 @@
 #include <string.h>
 
 #include "tfm_ns_mailbox.h"
+#ifdef TFM_MULTI_CORE_TEST
+#include "tfm_ns_mailbox_test.h"
+#endif
 
 /* The pointer to NSPE mailbox queue */
 static struct ns_mailbox_queue_t *mailbox_queue_ptr = NULL;
@@ -67,9 +70,9 @@
     uint8_t idx;
     mailbox_queue_status_t status;
 
-    ns_mailbox_spin_lock();
+    tfm_ns_mailbox_os_spin_lock();
     status = queue->empty_slots;
-    ns_mailbox_spin_unlock();
+    tfm_ns_mailbox_os_spin_unlock();
 
     if (!status) {
         /* No empty slot */
@@ -82,9 +85,9 @@
         }
     }
 
-    ns_mailbox_spin_lock();
+    tfm_ns_mailbox_os_spin_lock();
     clear_queue_slot_empty(queue, idx);
-    ns_mailbox_spin_unlock();
+    tfm_ns_mailbox_os_spin_unlock();
 
     return idx;
 }
@@ -146,14 +149,14 @@
     /* Clear up the owner field */
     set_msg_owner(idx, NULL);
 
-    ns_mailbox_spin_lock();
+    tfm_ns_mailbox_os_spin_lock();
     clear_queue_slot_woken(idx);
     /*
      * Make sure that the empty flag is set after all the other status flags are
      * re-initialized.
      */
     set_queue_slot_empty(idx);
-    ns_mailbox_spin_unlock();
+    tfm_ns_mailbox_os_spin_unlock();
 
     return MAILBOX_SUCCESS;
 }
@@ -250,14 +253,14 @@
 {
     bool is_set = false;
 
-    ns_mailbox_spin_lock();
+    tfm_ns_mailbox_os_spin_lock();
 
     if (is_queue_slot_woken(idx)) {
         clear_queue_slot_woken(idx);
         is_set = true;
     }
 
-    ns_mailbox_spin_unlock();
+    tfm_ns_mailbox_os_spin_unlock();
 
     return is_set;
 }
diff --git a/interface/src/multi_core/tfm_ns_mailbox_thread.c b/interface/src/multi_core/tfm_ns_mailbox_thread.c
index 6a77b1b..a3a46b1 100644
--- a/interface/src/multi_core/tfm_ns_mailbox_thread.c
+++ b/interface/src/multi_core/tfm_ns_mailbox_thread.c
@@ -68,9 +68,9 @@
     mailbox_queue_status_t status;
 
     while (1) {
-        ns_mailbox_spin_lock();
+        tfm_ns_mailbox_os_spin_lock();
         status = queue->empty_slots;
-        ns_mailbox_spin_unlock();
+        tfm_ns_mailbox_os_spin_unlock();
 
         if (status) {
             break;
@@ -92,9 +92,9 @@
         }
     }
 
-    ns_mailbox_spin_lock();
+    tfm_ns_mailbox_os_spin_lock();
     clear_queue_slot_empty(queue, idx);
-    ns_mailbox_spin_unlock();
+    tfm_ns_mailbox_os_spin_unlock();
 
     return idx;
 }