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/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;
+    }
+}