App: Add NS interface code moved from TF-M

Add NS interface code moved from TF-M. It includes OS wrapper headers
and RTX specific implementation of OS wrapper.

Signed-off-by: David Hu <david.hu@arm.com>
Change-Id: I0b88535eaa720b52d2090d5ed041987093df9d75
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index a210219..2843056 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -27,19 +27,28 @@
 set(INTERFACE_SRC_DIR    ${CMAKE_SOURCE_DIR}/interface/src)
 set(INTERFACE_INC_DIR    ${CMAKE_SOURCE_DIR}/interface/include)
 
+# NS interface implemented by NSPE
+set(NS_INTERFACE_DIR     ${CMAKE_CURRENT_SOURCE_DIR}/../ns_interface)
+
 #################### TF-M NS interface (header only) ###########################
 
 add_library(tfm_ns_interface INTERFACE)
 
+# Include interface headers exported by TF-M
 target_include_directories(tfm_ns_interface
     INTERFACE
         ${INTERFACE_INC_DIR}
         ${CMAKE_BINARY_DIR}/generated/interface/include
-        ${INTERFACE_INC_DIR}/os_wrapper
         $<$<BOOL:${TFM_MULTI_CORE_TOPOLOGY}>:${INTERFACE_INC_DIR}/multi_core>
         $<$<BOOL:${TFM_MULTI_CORE_TOPOLOGY}>:${CMAKE_SOURCE_DIR}/platform/ext/cmsis>
 )
 
+# Include NS local interface headers
+target_include_directories(tfm_ns_interface
+    INTERFACE
+        ${NS_INTERFACE_DIR}
+)
+
 # PSA interface files are generated from a template
 add_dependencies(tfm_ns_interface
     tfm_generated_files
@@ -87,16 +96,24 @@
         target_sources(tfm_api_ns PRIVATE
             ${INTERFACE_SRC_DIR}/multi_core/tfm_multi_core_ns_api.c
             ${INTERFACE_SRC_DIR}/multi_core/tfm_multi_core_psa_ns_api.c
-            $<$<BOOL:${TFM_MULTI_CORE_NS_OS}>:${INTERFACE_SRC_DIR}/multi_core/tfm_ns_mailbox_rtos_api.c>
             $<$<NOT:$<BOOL:${TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD}>>:${INTERFACE_SRC_DIR}/multi_core/tfm_ns_mailbox.c>
             $<$<AND:$<BOOL:${TFM_MULTI_CORE_NS_OS}>,$<BOOL:${TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD}>>:${INTERFACE_SRC_DIR}/multi_core/tfm_ns_mailbox_thread.c>
             $<$<BOOL:${TEST_NS}>:${INTERFACE_SRC_DIR}/multi_core/tfm_ns_mailbox_test.c>
         )
+
+        # NS RTOS specific implementation of NS mailbox
+        target_sources(tfm_api_ns PRIVATE
+            $<$<BOOL:${TFM_MULTI_CORE_NS_OS}>:${NS_INTERFACE_DIR}/multi_core/tfm_ns_mailbox_rtos_api.c>
+        )
     else()
         target_sources(tfm_api_ns PRIVATE
-           ${INTERFACE_SRC_DIR}/tfm_ns_interface.c
            ${INTERFACE_SRC_DIR}/tfm_psa_ns_api.c
         )
+
+        # NS specific implementation of NS interface dispacther
+        target_sources(tfm_api_ns PRIVATE
+           ${CMAKE_CURRENT_SOURCE_DIR}/tfm_ns_interface.c
+        )
     endif()
 else()
     target_sources(tfm_api_ns PRIVATE
@@ -108,7 +125,11 @@
         $<$<BOOL:${TFM_PARTITION_INITIAL_ATTESTATION}>:${INTERFACE_SRC_DIR}/tfm_initial_attestation_func_api.c>
         $<$<BOOL:${TFM_PARTITION_FIRMWARE_UPDATE}>:${INTERFACE_SRC_DIR}/tfm_firmware_update_func_api.c>
         ${INTERFACE_SRC_DIR}/tfm_psa_ns_api.c
-        ${INTERFACE_SRC_DIR}/tfm_ns_interface.c
+    )
+
+    # NS specific implementation of NS interface dispacther
+    target_sources(tfm_api_ns PRIVATE
+        ${CMAKE_CURRENT_SOURCE_DIR}/tfm_ns_interface.c
     )
 endif()
 
diff --git a/app/main_ns.c b/app/main_ns.c
index b3a027e..aaaead9 100644
--- a/app/main_ns.c
+++ b/app/main_ns.c
@@ -111,6 +111,8 @@
         }
     }
 }
+#else
+extern uint32_t tfm_ns_interface_init(void);
 #endif
 
 /**
@@ -156,10 +158,10 @@
 
 #ifdef TFM_MULTI_CORE_TOPOLOGY
     tfm_ns_multi_core_boot();
-#endif
-
+#else
     /* Initialize the TFM NS interface */
     tfm_ns_interface_init();
+#endif
 
 #ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
     (void) osThreadNew(mailbox_thread_func, NULL, &mailbox_thread_attr);
diff --git a/app/tfm_ns_interface.c b/app/tfm_ns_interface.c
new file mode 100644
index 0000000..3bd401b
--- /dev/null
+++ b/app/tfm_ns_interface.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2017-2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+#include <stdint.h>
+
+#include "os_wrapper/mutex.h"
+
+#include "tfm_ns_interface.h"
+
+/**
+ * \brief the ns_lock ID
+ */
+static void *ns_lock_handle = NULL;
+
+int32_t tfm_ns_interface_dispatch(veneer_fn fn,
+                                  uint32_t arg0, uint32_t arg1,
+                                  uint32_t arg2, uint32_t arg3)
+{
+    int32_t result;
+
+    /* TFM request protected by NS lock */
+    while (os_wrapper_mutex_acquire(ns_lock_handle, OS_WRAPPER_WAIT_FOREVER)
+            != OS_WRAPPER_SUCCESS);
+
+    result = fn(arg0, arg1, arg2, arg3);
+
+    while (os_wrapper_mutex_release(ns_lock_handle) != OS_WRAPPER_SUCCESS);
+
+    return result;
+}
+
+uint32_t tfm_ns_interface_init(void)
+{
+    void *handle;
+
+    handle = os_wrapper_mutex_create();
+    if (!handle) {
+        return OS_WRAPPER_ERROR;
+    }
+
+    ns_lock_handle = handle;
+    return OS_WRAPPER_SUCCESS;
+}