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/ns_interface/multi_core/tfm_ns_mailbox_rtos_api.c b/ns_interface/multi_core/tfm_ns_mailbox_rtos_api.c
new file mode 100644
index 0000000..629e108
--- /dev/null
+++ b/ns_interface/multi_core/tfm_ns_mailbox_rtos_api.c
@@ -0,0 +1,116 @@
+/*
+ * 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.
+ */
+
+#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
+#include "os_wrapper/msg_queue.h"
+#else
+#include "os_wrapper/semaphore.h"
+#endif
+#include "os_wrapper/thread.h"
+
+#include "tfm_ns_mailbox.h"
+
+/*
+ * 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
+
+#ifndef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
+#define MAX_SEMAPHORE_COUNT            NUM_MAILBOX_QUEUE_SLOT
+
+static void *ns_lock_handle = NULL;
+#endif
+
+const void *tfm_ns_mailbox_os_get_task_handle(void)
+{
+    return os_wrapper_thread_get_handle();
+}
+
+void tfm_ns_mailbox_os_wait_reply(void)
+{
+    os_wrapper_thread_wait_flag(MAILBOX_THREAD_FLAG, OS_WRAPPER_WAIT_FOREVER);
+}
+
+void tfm_ns_mailbox_os_wake_task_isr(const void *task_handle)
+{
+    os_wrapper_thread_set_flag_isr((void *)task_handle, MAILBOX_THREAD_FLAG);
+}
+
+#ifdef TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD
+void *tfm_ns_mailbox_os_mq_create(size_t msg_size, uint8_t msg_count)
+{
+    return os_wrapper_msg_queue_create(msg_size, msg_count);
+}
+
+int32_t tfm_ns_mailbox_os_mq_send(void *mq_handle, const void *msg_ptr)
+{
+    int32_t ret;
+
+    if (!mq_handle || !msg_ptr) {
+        return MAILBOX_INVAL_PARAMS;
+    }
+
+    while (1) {
+        ret = os_wrapper_msg_queue_send(mq_handle, msg_ptr);
+        if (ret == OS_WRAPPER_SUCCESS) {
+            return MAILBOX_SUCCESS;
+        }
+    }
+
+    return MAILBOX_GENERIC_ERROR;
+}
+
+int32_t tfm_ns_mailbox_os_mq_receive(void *mq_handle, void *msg_ptr)
+{
+    int32_t ret;
+
+    if (!mq_handle || !msg_ptr) {
+        return MAILBOX_INVAL_PARAMS;
+    }
+
+    while (1) {
+        ret = os_wrapper_msg_queue_receive(mq_handle, msg_ptr);
+        if (ret == OS_WRAPPER_SUCCESS) {
+            return MAILBOX_SUCCESS;
+        }
+    }
+
+    return MAILBOX_GENERIC_ERROR;
+}
+#else /* TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD */
+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);
+}
+#endif /* TFM_MULTI_CORE_NS_OS_MAILBOX_THREAD */
diff --git a/ns_interface/os_wrapper/common.h b/ns_interface/os_wrapper/common.h
new file mode 100644
index 0000000..6494723
--- /dev/null
+++ b/ns_interface/os_wrapper/common.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2017-2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __OS_WRAPPER_COMMON_H__
+#define __OS_WRAPPER_COMMON_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+
+#define OS_WRAPPER_SUCCESS            (0x0)
+#define OS_WRAPPER_ERROR              (0xFFFFFFFFU)
+#define OS_WRAPPER_WAIT_FOREVER       (0xFFFFFFFFU)
+#define OS_WRAPPER_DEFAULT_STACK_SIZE (-1)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __OS_WRAPPER_COMMON_H__ */
diff --git a/ns_interface/os_wrapper/msg_queue.h b/ns_interface/os_wrapper/msg_queue.h
new file mode 100644
index 0000000..ac69773
--- /dev/null
+++ b/ns_interface/os_wrapper/msg_queue.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __OS_WRAPPER_MSG_QUEUE_H__
+#define __OS_WRAPPER_MSG_QUEUE_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stddef.h>
+
+#include "common.h"
+
+/**
+ * \brief Create and initialize a message queue
+ *
+ * \param[in] msg_size        The maximum message size in bytes
+ * \param[in] msg_count       The maximum number of messages in queue
+ *
+ * \return Returns handle of the message queue created, or NULL in case of error
+ */
+void *os_wrapper_msg_queue_create(size_t msg_size, uint8_t msg_count);
+
+/**
+ * \brief Send a message via message queue
+ *
+ * \param[in] mq_handle       The handle of message queue
+ * \param[in] msg_ptr         The pointer to the message to be sent
+ *
+ * \return \ref OS_WRAPPER_SUCCESS if the message is successfully sent, or
+ *         \ref OS_WRAPPER_ERROR in case of error
+ *
+ * \note The message size must be the same as the value set in
+ *       \ref os_wrapper_msg_queue_create.
+ *
+ * \note Time out value is not specified here. Whether the function is blocked
+ *       or returns instantly depends on the actual implementation and usage
+ *       scenario.
+ */
+int32_t os_wrapper_msg_queue_send(void *mq_handle,
+                                  const void *msg_ptr);
+
+/**
+ * \brief Receive a message from message queue
+ *
+ * \param[in] mq_handle       The handle of message queue
+ * \param[in] msg_ptr         The pointer to buffer for message to be received
+ *
+ * \return \ref OS_WRAPPER_SUCCESS if the message is successfully received, or
+ *         \ref OS_WRAPPER_ERROR in case of error
+ *
+ * \note The message size is the same as the value set in
+ *       \ref os_wrapper_msg_queue_create.
+ *
+ * \note The function should be blocked until a message is received from message
+ *       queue, unless an error occurs.
+ */
+int32_t os_wrapper_msg_queue_receive(void *mq_handle,
+                                     void *msg_ptr);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __OS_WRAPPER_MSG_QUEUE_H__ */
diff --git a/ns_interface/os_wrapper/mutex.h b/ns_interface/os_wrapper/mutex.h
new file mode 100644
index 0000000..e55ef70
--- /dev/null
+++ b/ns_interface/os_wrapper/mutex.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2017-2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __OS_WRAPPER_MUTEX_H__
+#define __OS_WRAPPER_MUTEX_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "common.h"
+
+/**
+ * \brief Creates a mutex for mutual exclusion of resources
+ *
+ * \return The handle of the created mutex on success or NULL on error
+ */
+void *os_wrapper_mutex_create(void);
+
+/**
+ * \brief Acquires a mutex that is created by \ref os_wrapper_mutex_create()
+ *
+ * \param[in] handle   The handle of the mutex to acquire. Should be one of the
+ *                     handles returned by \ref os_wrapper_mutex_create()
+ * \param[in] timeout  The maximum amount of time(in tick periods) for the
+ *                     thread to wait for the mutex to be available.
+ *                     If timeout is zero, the function will return immediately.
+ *                     Setting timeout to \ref OS_WRAPPER_WAIT_FOREVER will
+ *                     cause the thread to wait indefinitely
+ *
+ * \return \ref OS_WRAPPER_SUCCESS on success or \ref OS_WRAPPER_ERROR on error
+ */
+uint32_t os_wrapper_mutex_acquire(void *handle, uint32_t timeout);
+
+/**
+ * \brief Releases the mutex acquired previously
+ *
+
+ * \param[in] handle The handle of the mutex that has been acquired
+ *
+ * \return \ref OS_WRAPPER_SUCCESS on success or \ref OS_WRAPPER_ERROR on error
+ */
+uint32_t os_wrapper_mutex_release(void *handle);
+
+/**
+ * \brief Deletes a mutex that is created by \ref os_wrapper_mutex_create()
+ *
+ * \param[in] handle The handle of the mutex to be deleted
+ *
+ * \return \ref OS_WRAPPER_SUCCESS on success or \ref OS_WRAPPER_ERROR on error
+ */
+uint32_t os_wrapper_mutex_delete(void *handle);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __OS_WRAPPER_MUTEX_H__ */
diff --git a/ns_interface/os_wrapper/semaphore.h b/ns_interface/os_wrapper/semaphore.h
new file mode 100644
index 0000000..83d88ca
--- /dev/null
+++ b/ns_interface/os_wrapper/semaphore.h
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __OS_WRAPPER_SEMAPHORE_H__
+#define __OS_WRAPPER_SEMAPHORE_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "common.h"
+
+/**
+ * \brief Creates a new semaphore
+ *
+ * \param[in] max_count       Highest count of the semaphore
+ * \param[in] initial_count   Starting count of the available semaphore
+ * \param[in] name            Name of the semaphore
+ *
+ * \return Returns handle of the semaphore created, or NULL in case of error
+ */
+void *os_wrapper_semaphore_create(uint32_t max_count, uint32_t initial_count,
+                                  const char *name);
+
+/**
+ * \brief Acquires the semaphore
+ *
+ * \param[in] hanlde  Semaphore handle
+ * \param[in] timeout Timeout value
+ *
+ * \return \ref OS_WRAPPER_SUCCESS in case of successful acquision, or
+ *         \ref OS_WRAPPER_ERROR in case of error
+ */
+uint32_t os_wrapper_semaphore_acquire(void *handle, uint32_t timeout);
+
+/**
+ * \brief Releases the semaphore
+ *
+ * \param[in] hanlde Semaphore handle
+ *
+ * \return \ref OS_WRAPPER_SUCCESS in case of successful release, or
+ *         \ref OS_WRAPPER_ERROR in case of error
+ */
+uint32_t os_wrapper_semaphore_release(void *handle);
+
+/**
+ * \brief Deletes the semaphore
+ *
+ * \param[in] handle Semaphore handle
+ *
+ * \return \ref OS_WRAPPER_SUCCESS in case of successful release, or
+ *         \ref OS_WRAPPER_ERROR in case of error
+ */
+uint32_t os_wrapper_semaphore_delete(void *handle);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __OS_WRAPPER_SEMAPHORE_H__ */
diff --git a/ns_interface/os_wrapper/thread.h b/ns_interface/os_wrapper/thread.h
new file mode 100644
index 0000000..a493593
--- /dev/null
+++ b/ns_interface/os_wrapper/thread.h
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __OS_WRAPPER_THREAD_H__
+#define __OS_WRAPPER_THREAD_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "common.h"
+
+/* prototype for the thread entry function */
+typedef void (*os_wrapper_thread_func) (void *argument);
+
+/**
+ * \brief Creates a new thread
+ *
+ * \param[in] name        Name of the thread
+ * \param[in] stack_size  Size of stack to be allocated for this thread. It can
+ *                        be \ref OS_WRAPPER_DEFAULT_STACK_SIZE to use the
+ *                        default value provided by the underlying RTOS
+ * \param[in] func        Pointer to the function invoked by thread
+ * \param[in] arg         Argument to pass to the function invoked by thread
+ * \param[in] priority    Initial thread priority
+ *
+ * \return Returns the thread handle created, or NULL in case of error
+ */
+void *os_wrapper_thread_new(const char *name, int32_t stack_size,
+                            os_wrapper_thread_func func, void *arg,
+                            uint32_t priority);
+/**
+ * \brief Gets current thread handle
+ *
+ * \return Returns the thread handle, or NULL in case of error
+ */
+void *os_wrapper_thread_get_handle(void);
+
+/**
+ * \brief Gets thread priority
+ *
+ * \param[in]  handle   Thread handle
+ * \param[out] priority The priority of the thread
+ *
+ * \return Returns \ref OS_WRAPPER_SUCCESS on success, or \ref OS_WRAPPER_ERROR
+ *                 in case of error
+ */
+uint32_t os_wrapper_thread_get_priority(void *handle, uint32_t *priority);
+
+/**
+ * \brief Exits the calling thread
+ */
+void os_wrapper_thread_exit(void);
+
+/**
+ * \brief Set the event flags for synchronizing a thread specified by handle.
+ *
+ * \note This function may not be allowed to be called from Interrupt Service
+ *       Routines.
+ *
+ * \param[in] handle    Thread handle to be notified
+ * \param[in] flags     Event flags value
+ *
+ * \return Returns \ref OS_WRAPPER_SUCCESS on success, or \ref OS_WRAPPER_ERROR
+ *         in case of error
+ */
+uint32_t os_wrapper_thread_set_flag(void *handle, uint32_t flags);
+
+/**
+ * \brief Set the event flags in an interrupt handler for synchronizing a thread
+ *        specified by handle.
+ *
+ * \param[in] handle    Thread handle to be notified
+ * \param[in] flags     Event flags value
+ *
+ * \return Returns \ref OS_WRAPPER_SUCCESS on success, or \ref OS_WRAPPER_ERROR
+ *         in case of error
+ */
+uint32_t os_wrapper_thread_set_flag_isr(void *handle, uint32_t flags);
+
+/**
+ * \brief Wait for the event flags for synchronizing threads.
+ *
+ * \note This function may not be allowed to be called from Interrupt Service
+ *       Routines.
+ *
+ * \param[in] flags     Specify the flags to wait for
+ * \param[in] timeout   Timeout value
+ *
+ * \return Returns \ref OS_WRAPPER_SUCCESS on success, or \ref OS_WRAPPER_ERROR
+ *         in case of error
+ */
+uint32_t os_wrapper_thread_wait_flag(uint32_t flags, uint32_t timeout);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __OS_WRAPPER_THREAD_H__ */
diff --git a/ns_interface/os_wrapper/tick.h b/ns_interface/os_wrapper/tick.h
new file mode 100644
index 0000000..b377b0e
--- /dev/null
+++ b/ns_interface/os_wrapper/tick.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __OS_WRAPPER_TICK_H__
+#define __OS_WRAPPER_TICK_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "common.h"
+
+/**
+ * \brief Return RTOS current tick count
+ *
+ * \return The current tick count
+ */
+uint32_t os_wrapper_get_tick(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __OS_WRAPPER_TICK_H__ */