SPM: PSA API working under Thread mode when level 1

This patch redirects PSA API into a customized 'thread call' ABI to
make it execute in thread mode, when:

- Runtime backend IPC and Isolation Level 1.

This 'thread call' ABI happens in 'Thread mode', and:

- Switch to SPM stack after PSA APIs are called. The working stack
  re-uses the NS Agent stack to save runtime memory or allocated a
  dedicated stack for it if no Trustzone NS Agent is available. No
  need to make SPM working under 'Handler mode' in this case.

- Lock scheduler to ensure PSA API context is not nested because of
  scheduling. The locking is performed by setting a flag, and the
  scheduler entry checks this flag to decide schedule or not.

- When PSA API returns, switch back to the caller stack. And trigger
  scheduler in the caller stack if a scheduling is expected. This
  helps the scheduler context management.

- SPM functionalities other than PSA API are still using SVC-based
  implementation, such as SPM initialization and log info flushing.

The content of this patch:

- Define interfaces, ABI dispatcher and architecture modifications
  to support handling PSA API under thread mode.

- Related header file and source path adjustment to help the build pass.

- Adjust IDLE partition stack size to make runtime execution work, the
  original size is too compact to run under debug build of specific
  platforms.

Change-Id: I52add3f57ef0f958d25725efb4eb702617586907
Signed-off-by: Ken Liu <Ken.Liu@arm.com>
Co-authored-by: Mingyang Sun <Mingyang.Sun@arm.com>
diff --git a/secure_fw/spm/ffm/backend_ipc.c b/secure_fw/spm/ffm/backend_ipc.c
index 3b23e1e..cd35e36 100644
--- a/secure_fw/spm/ffm/backend_ipc.c
+++ b/secure_fw/spm/ffm/backend_ipc.c
@@ -6,6 +6,7 @@
  */
 
 #include <stdint.h>
+#include "compiler_ext_defs.h"
 #include "spm_ipc.h"
 #include "tfm_hal_isolation.h"
 #include "tfm_rpc.h"
@@ -20,6 +21,24 @@
 /* Declare the global component list */
 struct partition_head_t partition_listhead;
 
+#ifdef CONFIG_TFM_PSA_API_THREAD_CALL
+
+#ifdef TFM_MULTI_CORE_TOPOLOGY
+/* TODO: To be checked when RPC design updates. */
+static uint8_t spm_stack_local[CONFIG_TFM_SPM_THREAD_STACK_SIZE] __aligned(8);
+struct context_ctrl_t spm_thread_context = {
+    .sp       = (uint32_t)&spm_stack_local[CONFIG_TFM_SPM_THREAD_STACK_SIZE],
+    .sp_limit = (uint32_t)spm_stack_local,
+    .reserved = 0,
+    .exc_ret  = 0,
+};
+struct context_ctrl_t *p_spm_thread_context = &spm_thread_context;
+#else
+struct context_ctrl_t *p_spm_thread_context;
+#endif
+
+#endif
+
 /*
  * Send message and wake up the SP who is waiting on message queue, block the
  * current thread and triggere scheduler.
@@ -78,6 +97,11 @@
 
     if (p_pldi->pid == TFM_SP_NON_SECURE_ID) {
         p_param = (void *)tfm_spm_hal_get_ns_entry_point();
+
+#ifdef CONFIG_TFM_PSA_API_THREAD_CALL
+        SPM_THREAD_CONTEXT = &p_pt->ctx_ctrl;
+#endif
+
     }
 
     thrd_start(&p_pt->thrd,