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/interface/include/config_impl.h.template b/interface/include/config_impl.h.template
index 4210055..6ef1a2b 100644
--- a/interface/include/config_impl.h.template
+++ b/interface/include/config_impl.h.template
@@ -12,10 +12,16 @@
 
 {% if ipc_partition_num > 0 and sfn_partition_num == 0 %}
 #define {{"%-56s"|format("CONFIG_TFM_SPM_BACKEND_IPC")}} 1
+
+#if TFM_LVL > 1
 #define {{"%-56s"|format("CONFIG_TFM_PSA_API_SUPERVISOR_CALL")}} 1
+#else
+#define {{"%-56s"|format("CONFIG_TFM_PSA_API_THREAD_CALL")}} 1
+#define {{"%-56s"|format("CONFIG_TFM_SPM_THREAD_STACK_SIZE")}} 1024
+#endif
 
 {% elif sfn_partition_num > 0 and ipc_partition_num == 0 %}
-#define CONFIG_TFM_SPM_BACKEND_SFN                 1
+#define {{"%-56s"|format("CONFIG_TFM_SPM_BACKEND_SFN")}} 1
 
 #if TFM_LVL > 1
 #error "High isolation level SFN model is not supported."
diff --git a/interface/include/psa_interface_redirect.h b/interface/include/psa_interface_redirect.h
index 2f2bc8c..ac16008 100644
--- a/interface/include/psa_interface_redirect.h
+++ b/interface/include/psa_interface_redirect.h
@@ -7,7 +7,7 @@
 #ifndef __PSA_INTERFACE_REDIRECT_H__
 #define __PSA_INTERFACE_REDIRECT_H__
 
-#ifdef CONFIG_TFM_PSA_API_SUPERVISOR_CALL
+#if defined(CONFIG_TFM_PSA_API_SUPERVISOR_CALL)
 
 #define psa_framework_version    psa_framework_version_svc
 #define psa_version              psa_version_svc
@@ -30,6 +30,29 @@
 #define psa_reset_signal         psa_reset_signal_svc
 #define psa_rot_lifecycle_state  psa_rot_lifecycle_state_svc
 
+#elif defined(CONFIG_TFM_PSA_API_THREAD_CALL)
+
+#define psa_framework_version    psa_framework_version_thread
+#define psa_version              psa_version_thread
+#define psa_connect              psa_connect_thread
+#define tfm_psa_call_pack        tfm_psa_call_pack_thread
+#define psa_close                psa_close_thread
+#define psa_wait                 psa_wait_thread
+#define psa_get                  psa_get_thread
+#define psa_set_rhandle          psa_set_rhandle_thread
+#define psa_read                 psa_read_thread
+#define psa_skip                 psa_skip_thread
+#define psa_write                psa_write_thread
+#define psa_reply                psa_reply_thread
+#define psa_notify               psa_notify_thread
+#define psa_clear                psa_clear_thread
+#define psa_eoi                  psa_eoi_thread
+#define psa_panic                psa_panic_thread
+#define psa_irq_enable           psa_irq_enable_thread
+#define psa_irq_disable          psa_irq_disable_thread
+#define psa_reset_signal         psa_reset_signal_thread
+#define psa_rot_lifecycle_state  psa_rot_lifecycle_state_thread
+
 #endif
 
 #endif /* __PSA_INTERFACE_REDIRECT_H__ */