Core: Refine tfm_core_spm_request_handler function

- Move 'tfm_core_spm_request_handler' to spm module and rename it to
  'tfm_spm_request_handler' since it is an SPM function.
- Remove un-used included headers from tfm_core.c and sort them in order.

Change-Id: I3e2aa9d247278ffc62a76cba4bf79c0e0dbd159b
Signed-off-by: Summer Qin <summer.qin@arm.com>
diff --git a/secure_fw/core/include/tfm_internal.h b/secure_fw/core/include/tfm_internal.h
index fa3cb1c..119f53f 100644
--- a/secure_fw/core/include/tfm_internal.h
+++ b/secure_fw/core/include/tfm_internal.h
@@ -49,9 +49,4 @@
  */
 void tfm_core_validate_boot_data(void);
 
-/**
- * \brief Handle an SPM request by a secure service
- */
-void tfm_core_spm_request_handler(const struct tfm_state_context_t *svc_ctx);
-
 #endif /* __TFM_INTERNAL_H__ */
diff --git a/secure_fw/core/tfm_core.c b/secure_fw/core/tfm_core.c
index e834b39..9360c01 100644
--- a/secure_fw/core/tfm_core.c
+++ b/secure_fw/core/tfm_core.c
@@ -5,29 +5,15 @@
  *
  */
 
-#include "region_defs.h"
-#include "tfm_core.h"
-#include "tfm_internal.h"
-#include "tfm_api.h"
-#include "tfm_arch.h"
-#include "tfm_core_topology.h"
-#include "tfm_nspm.h"
-#include "platform/include/tfm_spm_hal.h"
-#include "secure_fw/spm/spm_api.h"
-#include "secure_fw/include/tfm_spm_services_api.h"
-#include "tfm_irq_list.h"
-#include "tfm_utils.h"
-#include "tfm_version.h"
-#include "spm_db.h"
-#include "log/tfm_log.h"
 #include "region.h"
-#ifdef TFM_PSA_API
-#include "psa/client.h"
-#include "psa/service.h"
-#include "tfm_thread.h"
-#include "tfm_wait.h"
-#include "tfm_message_queue.h"
-#endif
+#include "spm_db.h"
+#include "tfm_core_topology.h"
+#include "tfm_internal.h"
+#include "tfm_irq_list.h"
+#include "tfm_nspm.h"
+#include "tfm_spm_hal.h"
+#include "tfm_version.h"
+#include "log/tfm_log.h"
 
 /*
  * Avoids the semihosting issue
@@ -53,7 +39,7 @@
 
 REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP,  $$ZI$$Base);
 
-int32_t tfm_core_init(void)
+static int32_t tfm_core_init(void)
 {
     size_t i;
     enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
@@ -151,51 +137,6 @@
     return TFM_SUCCESS;
 }
 
-void tfm_core_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
-{
-    uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0;
-    uint32_t running_partition_flags = 0;
-#ifdef TFM_PSA_API
-    const struct spm_partition_desc_t *partition = NULL;
-#else /* TFM_PSA_API */
-    uint32_t running_partition_idx;
-#endif /* TFM_PSA_API */
-
-    /* Check permissions on request type basis */
-
-    switch (svc_ctx->r0) {
-    case TFM_SPM_REQUEST_RESET_VOTE:
-#ifdef TFM_PSA_API
-        partition = tfm_spm_get_running_partition();
-        if (!partition) {
-            tfm_core_panic();
-        }
-        running_partition_flags = partition->static_data->partition_flags;
-#else /* TFM_PSA_API */
-        running_partition_idx =
-            tfm_spm_partition_get_running_partition_idx();
-        running_partition_flags = tfm_spm_partition_get_flags(
-                                                         running_partition_idx);
-#endif  /* TFM_PSA_API */
-
-        /* Currently only PSA Root of Trust services are allowed to make Reset
-         * vote request
-         */
-        if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) {
-            *res_ptr = (uint32_t)TFM_ERROR_GENERIC;
-        }
-
-        /* FixMe: this is a placeholder for checks to be performed before
-         * allowing execution of reset
-         */
-        *res_ptr = (uint32_t)TFM_SUCCESS;
-
-        break;
-    default:
-        *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
-    }
-}
-
 int main(void)
 {
     /* set Main Stack Pointer limit */
diff --git a/secure_fw/core/tfm_core_svcalls_func.c b/secure_fw/core/tfm_core_svcalls_func.c
index 8c6d88f..c1f9587 100644
--- a/secure_fw/core/tfm_core_svcalls_func.c
+++ b/secure_fw/core/tfm_core_svcalls_func.c
@@ -19,10 +19,11 @@
 #include "tfm_arch.h"
 #include "tfm_peripherals_def.h"
 #include "tfm_irq_list.h"
+#include "spm_api.h"
 
 #ifdef PLATFORM_SVC_HANDLERS
 extern int32_t platform_svc_handlers(tfm_svc_number_t svc_num,
-				     uint32_t *svc_args, uint32_t lr);
+                                     uint32_t *svc_args, uint32_t lr);
 #endif
 
 /* Include the definitions of the privileged IRQ handlers in case of library
@@ -64,7 +65,7 @@
         tfm_spm_get_caller_client_id_handler(svc_args);
         break;
     case TFM_SVC_SPM_REQUEST:
-        tfm_core_spm_request_handler((struct tfm_state_context_t *)svc_args);
+        tfm_spm_request_handler((struct tfm_state_context_t *)svc_args);
         break;
     case TFM_SVC_DEPRIV_REQ:
         lr = tfm_spm_depriv_req_handler(svc_args, lr);
diff --git a/secure_fw/core/tfm_core_svcalls_ipc.c b/secure_fw/core/tfm_core_svcalls_ipc.c
index 7cbf907..bd2133c 100644
--- a/secure_fw/core/tfm_core_svcalls_ipc.c
+++ b/secure_fw/core/tfm_core_svcalls_ipc.c
@@ -103,7 +103,7 @@
         tfm_spm_psa_panic();
         break;
     case TFM_SVC_SPM_REQUEST:
-        tfm_core_spm_request_handler((const struct tfm_state_context_t *)ctx);
+        tfm_spm_request_handler((const struct tfm_state_context_t *)ctx);
         break;
     case TFM_SVC_PSA_LIFECYCLE:
         return tfm_spm_get_lifecycle_state();
diff --git a/secure_fw/spm/spm_api.h b/secure_fw/spm/spm_api.h
index ea5bc9b..6dbd12f 100644
--- a/secure_fw/spm/spm_api.h
+++ b/secure_fw/spm/spm_api.h
@@ -225,6 +225,12 @@
  */
 uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags);
 
+/**
+ * \brief                   Handle an SPM request by a secure service
+ * \param[in] svc_ctx       The stacked SVC context
+ */
+void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx);
+
 /*********************** library definitions ***********************/
 
 #ifndef TFM_PSA_API
diff --git a/secure_fw/spm/spm_func.c b/secure_fw/spm/spm_func.c
index afb0479..696fdcf 100644
--- a/secure_fw/spm/spm_func.c
+++ b/secure_fw/spm/spm_func.c
@@ -21,6 +21,7 @@
 #include "spm_db.h"
 #include "region_defs.h"
 #include "region.h"
+#include "tfm_spm_services_api.h"
 
 #define EXC_RETURN_SECURE_FUNCTION 0xFFFFFFFD
 #define EXC_RETURN_SECURE_HANDLER  0xFFFFFFF1
@@ -1284,3 +1285,36 @@
     }
     partition->runtime_data.orig_outvec = 0;
 }
+
+void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
+{
+    uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0;
+    uint32_t running_partition_flags = 0;
+    uint32_t running_partition_idx;
+
+    /* Check permissions on request type basis */
+
+    switch (svc_ctx->r0) {
+    case TFM_SPM_REQUEST_RESET_VOTE:
+        running_partition_idx =
+            tfm_spm_partition_get_running_partition_idx();
+        running_partition_flags = tfm_spm_partition_get_flags(
+                                                         running_partition_idx);
+
+        /* Currently only PSA Root of Trust services are allowed to make Reset
+         * vote request
+         */
+        if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) {
+            *res_ptr = (uint32_t)TFM_ERROR_GENERIC;
+        }
+
+        /* FixMe: this is a placeholder for checks to be performed before
+         * allowing execution of reset
+         */
+        *res_ptr = (uint32_t)TFM_SUCCESS;
+
+        break;
+    default:
+        *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
+    }
+}
diff --git a/secure_fw/spm/spm_ipc.c b/secure_fw/spm/spm_ipc.c
index d6e707d..ea1f735 100644
--- a/secure_fw/spm/spm_ipc.c
+++ b/secure_fw/spm/spm_ipc.c
@@ -32,6 +32,7 @@
 #include "tfm_list.h"
 #include "tfm_pools.h"
 #include "region_defs.h"
+#include "tfm_spm_services_api.h"
 
 #include "secure_fw/services/tfm_service_list.inc"
 
@@ -1590,3 +1591,37 @@
         tfm_core_panic();
     }
 }
+
+void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
+{
+    uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0;
+    uint32_t running_partition_flags = 0;
+    const struct spm_partition_desc_t *partition = NULL;
+
+    /* Check permissions on request type basis */
+
+    switch (svc_ctx->r0) {
+    case TFM_SPM_REQUEST_RESET_VOTE:
+        partition = tfm_spm_get_running_partition();
+        if (!partition) {
+            tfm_core_panic();
+        }
+        running_partition_flags = partition->static_data->partition_flags;
+
+        /* Currently only PSA Root of Trust services are allowed to make Reset
+         * vote request
+         */
+        if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) {
+            *res_ptr = (uint32_t)TFM_ERROR_GENERIC;
+        }
+
+        /* FixMe: this is a placeholder for checks to be performed before
+         * allowing execution of reset
+         */
+        *res_ptr = (uint32_t)TFM_SUCCESS;
+
+        break;
+    default:
+        *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
+    }
+}