Build: Add configuration for doorbell APIs

The doorbell APIs are not widely used.
This patch adds a build configuration for them to save memory by
disabling the APIs.

Corresponding changes are made as well:
1. SFN_BACKEND does not support doorbell - it does not provide the
   doorbell APIs, so the config should be disabled by default.
2. The psa_wait in SFN backend can be removed as well since neither
   of the API consumer, interrupt or doorbell is enabled.

Change-Id: I7c367dc0724f915ec2932b6c29db54bdfac06fa3
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
diff --git a/config/config_default.cmake b/config/config_default.cmake
index cd1154e..a094957 100755
--- a/config/config_default.cmake
+++ b/config/config_default.cmake
@@ -55,6 +55,8 @@
 set(CONFIG_TFM_FP                       "soft"      CACHE STRING    "FP ABI type in SPE and NSPE: soft-Software ABI, hard-Hardware ABI")
 set(CONFIG_TFM_LAZY_STACKING            OFF         CACHE BOOL      "Enable/disable lazy stacking")
 
+set(CONFIG_TFM_DOORBELL_API             ON          CACHE BOOL      "Enable the doorbell APIs")
+
 ############################ Platform ##########################################
 
 set(TFM_MULTI_CORE_TOPOLOGY             OFF         CACHE BOOL      "Whether to build for a dual-cpu architecture")
diff --git a/config/tfm_sfn_config_default.cmake b/config/tfm_sfn_config_default.cmake
index e682c33..18ba9fd 100644
--- a/config/tfm_sfn_config_default.cmake
+++ b/config/tfm_sfn_config_default.cmake
@@ -8,6 +8,7 @@
 ############################ Partitions ########################################
 
 set(TFM_PSA_API                 ON          CACHE BOOL      "Use PSA API instead of secure library model")
+set(CONFIG_TFM_DOORBELL_API     OFF         CACHE BOOL      "Enable the doorbell APIs")
 set(CONFIG_TFM_SPM_BACKEND_IPC  OFF)
 set(CONFIG_TFM_SPM_BACKEND_SFN  ON)
 set(CONFIG_TFM_PARTITION_META   OFF)
diff --git a/interface/include/psa_interface_redirect.h b/interface/include/psa_interface_redirect.h
index 96dc79c..892e800 100644
--- a/interface/include/psa_interface_redirect.h
+++ b/interface/include/psa_interface_redirect.h
@@ -20,10 +20,14 @@
 #define psa_skip                 psa_skip_svc
 #define psa_write                psa_write_svc
 #define psa_reply                psa_reply_svc
-#define psa_notify               psa_notify_svc
-#define psa_clear                psa_clear_svc
 #define psa_panic                psa_panic_svc
 #define psa_rot_lifecycle_state  psa_rot_lifecycle_state_svc
+
+#if CONFIG_TFM_DOORBELL_API == 1
+#define psa_notify               psa_notify_svc
+#define psa_clear                psa_clear_svc
+#endif /* CONFIG_TFM_DOORBELL_API == 1 */
+
 /* Following PSA APIs are only needed by connection-based services */
 #if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
 #define psa_connect              psa_connect_svc
@@ -55,10 +59,14 @@
 #define psa_skip                 psa_skip_cross
 #define psa_write                psa_write_cross
 #define psa_reply                psa_reply_cross
-#define psa_notify               psa_notify_cross
-#define psa_clear                psa_clear_cross
 #define psa_panic                psa_panic_cross
 #define psa_rot_lifecycle_state  psa_rot_lifecycle_state_cross
+
+#if CONFIG_TFM_DOORBELL_API == 1
+#define psa_notify               psa_notify_cross
+#define psa_clear                psa_clear_cross
+#endif /* CONFIG_TFM_DOORBELL_API == 1 */
+
 /* Following PSA APIs are only needed by connection-based services */
 #if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
 #define psa_connect              psa_connect_cross
@@ -91,7 +99,6 @@
 #define psa_framework_version    psa_framework_version_sfn
 #define psa_version              psa_version_sfn
 #define tfm_psa_call_pack        psa_call_pack_sfn
-#define psa_wait                 psa_wait_sfn
 #define psa_read                 psa_read_sfn
 #define psa_skip                 psa_skip_sfn
 #define psa_write                psa_write_sfn
diff --git a/secure_fw/CMakeLists.txt b/secure_fw/CMakeLists.txt
index 9ac0406..d66118a 100644
--- a/secure_fw/CMakeLists.txt
+++ b/secure_fw/CMakeLists.txt
@@ -124,6 +124,7 @@
 target_compile_definitions(tfm_secure_api
     INTERFACE
         CONFIG_TFM_BUILDING_SPE=1
+        $<$<BOOL:${CONFIG_TFM_DOORBELL_API}>:CONFIG_TFM_DOORBELL_API=1>
 )
 
 ############################# Secure veneers ###################################
diff --git a/secure_fw/spm/CMakeLists.txt b/secure_fw/spm/CMakeLists.txt
index 7009b0a..387685d 100755
--- a/secure_fw/spm/CMakeLists.txt
+++ b/secure_fw/spm/CMakeLists.txt
@@ -95,6 +95,7 @@
         # CONFIG_TFM_FP
         $<$<STREQUAL:${CONFIG_TFM_FP},hard>:CONFIG_TFM_FP=2>
         $<$<STREQUAL:${CONFIG_TFM_FP},soft>:CONFIG_TFM_FP=0>
+        $<$<BOOL:${CONFIG_TFM_DOORBELL_API}>:CONFIG_TFM_DOORBELL_API=1>
 )
 
 target_compile_options(tfm_spm
diff --git a/secure_fw/spm/cmsis_psa/psa_interface_cross.c b/secure_fw/spm/cmsis_psa/psa_interface_cross.c
index 88c27c0..cd17de2 100644
--- a/secure_fw/spm/cmsis_psa/psa_interface_cross.c
+++ b/secure_fw/spm/cmsis_psa/psa_interface_cross.c
@@ -178,6 +178,7 @@
     );
 }
 
+#if CONFIG_TFM_DOORBELL_API == 1
 __naked
 __section(".psa_interface_cross_call")
 void psa_notify_cross(int32_t partition_id)
@@ -207,6 +208,7 @@
         "b      psa_interface_cross_unified_entry           \n"
     );
 }
+#endif /* CONFIG_TFM_DOORBELL_API == 1 */
 
 __naked
 __section(".psa_interface_cross_call")
diff --git a/secure_fw/spm/cmsis_psa/psa_interface_sfn.c b/secure_fw/spm/cmsis_psa/psa_interface_sfn.c
index d7e5179..a36e0b9 100644
--- a/secure_fw/spm/cmsis_psa/psa_interface_sfn.c
+++ b/secure_fw/spm/cmsis_psa/psa_interface_sfn.c
@@ -42,16 +42,6 @@
     return (psa_status_t)stat;
 }
 
-psa_signal_t psa_wait_sfn(psa_signal_t signal_mask, uint32_t timeout)
-{
-    /*
-     * TODO:
-     *   Need to trigger scheduler here, when 'psa_wait'
-     *   is needed (interrupt case, e.g.).
-     */
-    return tfm_spm_partition_psa_wait(signal_mask, timeout);
-}
-
 size_t psa_read_sfn(psa_handle_t msg_handle, uint32_t invec_idx,
                     void *buffer, size_t num_bytes)
 {
diff --git a/secure_fw/spm/cmsis_psa/psa_interface_svc.c b/secure_fw/spm/cmsis_psa/psa_interface_svc.c
index 3d1c62c..3674e24 100644
--- a/secure_fw/spm/cmsis_psa/psa_interface_svc.c
+++ b/secure_fw/spm/cmsis_psa/psa_interface_svc.c
@@ -74,6 +74,7 @@
                    "bx      lr                                 \n");
 }
 
+#if CONFIG_TFM_DOORBELL_API == 1
 __naked void psa_notify_svc(int32_t partition_id)
 {
     __asm volatile("svc     "M2S(TFM_SVC_PSA_NOTIFY)"          \n"
@@ -85,6 +86,7 @@
     __asm volatile("svc     "M2S(TFM_SVC_PSA_CLEAR)"           \n"
                    "bx      lr                                 \n");
 }
+#endif /* CONFIG_TFM_DOORBELL_API == 1 */
 
 __naked void psa_panic_svc(void)
 {
diff --git a/secure_fw/spm/cmsis_psa/spm_ipc.c b/secure_fw/spm/cmsis_psa/spm_ipc.c
index 7a3fc55..feee282 100755
--- a/secure_fw/spm/cmsis_psa/spm_ipc.c
+++ b/secure_fw/spm/cmsis_psa/spm_ipc.c
@@ -249,6 +249,7 @@
     return NULL;
 }
 
+#if CONFIG_TFM_DOORBELL_API == 1
 /**
  * \brief                   Get the partition context by partition ID.
  *
@@ -270,6 +271,7 @@
 
     return NULL;
 }
+#endif /* CONFIG_TFM_DOORBELL_API == 1 */
 
 int32_t tfm_spm_check_client_version(struct service_t *service,
                                      uint32_t version)
diff --git a/secure_fw/spm/cmsis_psa/spm_ipc.h b/secure_fw/spm/cmsis_psa/spm_ipc.h
index bf4b498..b9d54d5 100644
--- a/secure_fw/spm/cmsis_psa/spm_ipc.h
+++ b/secure_fw/spm/cmsis_psa/spm_ipc.h
@@ -215,6 +215,7 @@
 struct conn_handle_t *spm_get_handle_by_signal(struct partition_t *p_ptn,
                                                psa_signal_t signal);
 
+#if CONFIG_TFM_DOORBELL_API == 1
 /**
  * \brief                   Get partition by Partition ID.
  *
@@ -225,6 +226,7 @@
  *                          \ref partition_t structures
  */
 struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id);
+#endif /* CONFIG_TFM_DOORBELL_API == 1 */
 
 /**
  * \brief                   Get the service context by service ID.
diff --git a/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c b/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c
index 1ff754e..4f4356c 100644
--- a/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c
+++ b/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c
@@ -47,8 +47,6 @@
                                          (const psa_invec *)ctx[2],
                                          (psa_outvec *)ctx[3]);
         break;
-    case TFM_SVC_PSA_WAIT:
-        return tfm_spm_partition_psa_wait((psa_signal_t)ctx[0], ctx[1]);
     case TFM_SVC_PSA_READ:
         return tfm_spm_partition_psa_read((psa_handle_t)ctx[0], ctx[1],
                                           (void *)ctx[2], (size_t)ctx[3]);
@@ -59,12 +57,14 @@
         tfm_spm_partition_psa_write((psa_handle_t)ctx[0], ctx[1],
                                     (void *)ctx[2], (size_t)ctx[3]);
         break;
+#if CONFIG_TFM_DOORBELL_API == 1
     case TFM_SVC_PSA_NOTIFY:
         tfm_spm_partition_psa_notify((int32_t)ctx[0]);
         break;
     case TFM_SVC_PSA_CLEAR:
         tfm_spm_partition_psa_clear();
         break;
+#endif /* CONFIG_TFM_DOORBELL_API == 1 */
     case TFM_SVC_PSA_PANIC:
         tfm_spm_partition_psa_panic();
         break;
@@ -90,6 +90,8 @@
     case TFM_SVC_PSA_REPLY:
         tfm_spm_partition_psa_reply((psa_handle_t)ctx[0], (psa_status_t)ctx[1]);
         break;
+    case TFM_SVC_PSA_WAIT:
+        return tfm_spm_partition_psa_wait((psa_signal_t)ctx[0], ctx[1]);
 #endif /* CONFIG_TFM_SPM_BACKEND_IPC == 1 */
 
 #if CONFIG_TFM_FLIH_API == 1 || CONFIG_TFM_SLIH_API == 1
diff --git a/secure_fw/spm/ffm/backend_ipc.c b/secure_fw/spm/ffm/backend_ipc.c
index 9870d21..9507b03 100644
--- a/secure_fw/spm/ffm/backend_ipc.c
+++ b/secure_fw/spm/ffm/backend_ipc.c
@@ -107,7 +107,11 @@
 {
     const struct partition_load_info_t *p_pldi = p_pt->p_ldinf;
 
-    p_pt->signals_allowed |= PSA_DOORBELL | service_setting;
+#if CONFIG_TFM_DOORBELL_API == 1
+    p_pt->signals_allowed |= PSA_DOORBELL;
+#endif /* CONFIG_TFM_DOORBELL_API == 1 */
+
+    p_pt->signals_allowed |= service_setting;
 
     THRD_SYNC_INIT(&p_pt->waitobj);
     UNI_LISI_INIT_NODE(p_pt, p_handles);
diff --git a/secure_fw/spm/ffm/psa_api.c b/secure_fw/spm/ffm/psa_api.c
index c8ede67..62715db 100644
--- a/secure_fw/spm/ffm/psa_api.c
+++ b/secure_fw/spm/ffm/psa_api.c
@@ -435,6 +435,7 @@
 
 /* PSA Partition API function body */
 
+#if CONFIG_TFM_SPM_BACKEND_IPC == 1
 psa_signal_t tfm_spm_partition_psa_wait(psa_signal_t signal_mask,
                                         uint32_t timeout)
 {
@@ -471,8 +472,6 @@
     return partition->signals_asserted & signal_mask;
 }
 
-/* This API is only used in IPC backend. */
-#if CONFIG_TFM_SPM_BACKEND_IPC == 1
 psa_status_t tfm_spm_partition_psa_get(psa_signal_t signal, psa_msg_t *msg)
 {
     struct conn_handle_t *tmp_msg = NULL;
@@ -850,6 +849,7 @@
     return ret;
 }
 
+#if CONFIG_TFM_DOORBELL_API == 1
 void tfm_spm_partition_psa_notify(int32_t partition_id)
 {
     struct partition_t *p_pt = tfm_spm_get_partition_by_id(partition_id);
@@ -876,6 +876,7 @@
     partition->signals_asserted &= ~PSA_DOORBELL;
     CRITICAL_SECTION_LEAVE(cs_assert);
 }
+#endif /* CONFIG_TFM_DOORBELL_API == 1 */
 
 void tfm_spm_partition_psa_panic(void)
 {
diff --git a/secure_fw/spm/include/ffm/psa_api.h b/secure_fw/spm/include/ffm/psa_api.h
index 0322fa9..970ad2d 100644
--- a/secure_fw/spm/include/ffm/psa_api.h
+++ b/secure_fw/spm/include/ffm/psa_api.h
@@ -125,6 +125,8 @@
 
 /* PSA Partition API function body, for privileged use only. */
 
+/* This API is only used in IPC backend. */
+#if CONFIG_TFM_SPM_BACKEND_IPC == 1
 /**
  * \brief Function body of \ref psa_wait.
  *
@@ -140,8 +142,6 @@
 psa_signal_t tfm_spm_partition_psa_wait(psa_signal_t signal_mask,
                                         uint32_t timeout);
 
-/* This API is only used in IPC backend. */
-#if CONFIG_TFM_SPM_BACKEND_IPC == 1
 /**
  * \brief Function body of \ref psa_get.
  *
@@ -162,7 +162,7 @@
  *                                reference.
  */
 psa_status_t tfm_spm_partition_psa_get(psa_signal_t signal, psa_msg_t *msg);
-#endif
+#endif /* CONFIG_TFM_SPM_BACKEND_IPC == 1 */
 
 /**
  * \brief Function body of \ref psa_read.
@@ -257,6 +257,7 @@
 int32_t tfm_spm_partition_psa_reply(psa_handle_t msg_handle,
                                     psa_status_t status);
 
+#if CONFIG_TFM_DOORBELL_API == 1
 /**
  * \brief Function body of \ref psa_norify.
  *
@@ -276,6 +277,7 @@
  *                              currently asserted.
  */
 void tfm_spm_partition_psa_clear(void);
+#endif /* CONFIG_TFM_DOORBELL_API == 1 */
 
 /**
  * \brief Function body of \ref psa_panic.