SPM: Use separate source code for different models

Remove common API source 'spm_api.c', copy the common code to each
model's source files. This patch is to separate IPC and library
model source code before aligning with HAL data structure.

Change-Id: Id0d0f19f728cb27097465ae06b305831b8c26473
Signed-off-by: Mingyang Sun <Mingyang.Sun@arm.com>
diff --git a/secure_fw/spm/include/spm_api.h b/secure_fw/spm/include/spm_api.h
index 968f628..51e4f9e 100644
--- a/secure_fw/spm/include/spm_api.h
+++ b/secure_fw/spm/include/spm_api.h
@@ -173,17 +173,6 @@
 #endif /* ifdef(TFM_PSA_API) */
 
 /*********************** common definitions ***********************/
-
-/**
- * \brief Returns the index of the partition with the given partition ID.
- *
- * \param[in] partition_id     Partition id
- *
- * \return the partition idx if partition_id is valid,
- *         \ref SPM_INVALID_PARTITION_IDX othervise
- */
-uint32_t get_partition_idx(uint32_t partition_id);
-
 /**
  * \brief Get the id of the partition for its index from the db
  *
@@ -196,17 +185,6 @@
 uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx);
 
 /**
- * \brief Get the flags associated with a partition
- *
- * \param[in] partition_idx     Partition index
- *
- * \return Flags associated with the partition
- *
- * \note This function doesn't check if partition_idx is valid.
- */
-uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx);
-
-/**
  * \brief Initialize partition database
  *
  * \return Error code \ref spm_err_t
@@ -214,19 +192,6 @@
 enum spm_err_t tfm_spm_db_init(void);
 
 /**
- * \brief Change the privilege mode for partition thread mode.
- *
- * \param[in] privileged        Privileged mode,
- *                                \ref TFM_PARTITION_PRIVILEGED_MODE
- *                                and \ref TFM_PARTITION_UNPRIVILEGED_MODE
- *
- * \note Barrier instructions are not called by this function, and if
- *       it is called in thread mode, it might be necessary to call
- *       them after this function returns.
- */
-void tfm_spm_partition_change_privilege(uint32_t privileged);
-
-/**
  * \brief                   Get the current partition mode.
  *
  * \param[in] partition_flags               Flags of current partition
diff --git a/secure_fw/spm/model_func/CMakeLists.inc b/secure_fw/spm/model_func/CMakeLists.inc
index 1dca106..9720fc1 100644
--- a/secure_fw/spm/model_func/CMakeLists.inc
+++ b/secure_fw/spm/model_func/CMakeLists.inc
@@ -39,7 +39,6 @@
 		"${SFW_FUNC_SPM_DIR}/tfm_core_svcalls_func.c"
 		"${SFW_FUNC_SPM_DIR}/tfm_secure_api.c"
 		"${SFW_FUNC_SPM_DIR}/../runtime/tfm_spm_services.c"
-		"${SFW_FUNC_SPM_DIR}/../runtime/spm_api.c"
 		"${SFW_FUNC_SPM_DIR}/spm_func.c"
 		"${SFW_FUNC_SPM_DIR}/tfm_nspm_func.c"
 		"${SFW_FUNC_SPM_DIR}/../runtime/utilities.c"
diff --git a/secure_fw/spm/model_func/spm_func.c b/secure_fw/spm/model_func/spm_func.c
index 3d4491e..9cfd1a8 100644
--- a/secure_fw/spm/model_func/spm_func.c
+++ b/secure_fw/spm/model_func/spm_func.c
@@ -305,6 +305,46 @@
     return &REGION_NAME(Image$$, TFM_SECURE_STACK, $$ZI$$Limit)[-1];
 }
 
+/**
+ * \brief Returns the index of the partition with the given partition ID.
+ *
+ * \param[in] partition_id     Partition id
+ *
+ * \return the partition idx if partition_id is valid,
+ *         \ref SPM_INVALID_PARTITION_IDX othervise
+ */
+static uint32_t get_partition_idx(uint32_t partition_id)
+{
+    uint32_t i;
+
+    if (partition_id == INVALID_PARTITION_ID) {
+        return SPM_INVALID_PARTITION_IDX;
+    }
+
+    for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
+        if (g_spm_partition_db.partitions[i].static_data->partition_id ==
+            partition_id) {
+            return i;
+        }
+    }
+    return SPM_INVALID_PARTITION_IDX;
+}
+
+/**
+ * \brief Get the flags associated with a partition
+ *
+ * \param[in] partition_idx     Partition index
+ *
+ * \return Flags associated with the partition
+ *
+ * \note This function doesn't check if partition_idx is valid.
+ */
+static uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
+{
+    return g_spm_partition_db.partitions[partition_idx].static_data->
+           partition_flags;
+}
+
 static enum tfm_status_e tfm_start_partition(
                                            const struct tfm_sfn_req_s *desc_ptr,
                                            uint32_t excReturn)
@@ -605,6 +645,29 @@
     return TFM_SUCCESS;
 }
 
+uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
+{
+    return g_spm_partition_db.partitions[partition_idx].static_data->
+           partition_id;
+}
+
+uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
+{
+    if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
+        return TFM_PARTITION_PRIVILEGED_MODE;
+    } else {
+        return TFM_PARTITION_UNPRIVILEGED_MODE;
+    }
+}
+
+bool tfm_is_partition_privileged(uint32_t partition_idx)
+{
+    uint32_t flags = tfm_spm_partition_get_flags(partition_idx);
+
+    return tfm_spm_partition_get_privileged_mode(flags) ==
+           TFM_PARTITION_PRIVILEGED_MODE;
+}
+
 void tfm_spm_secure_api_init_done(void)
 {
     tfm_secure_api_initializing = 0;
diff --git a/secure_fw/spm/model_ipc/CMakeLists.inc b/secure_fw/spm/model_ipc/CMakeLists.inc
index 957bdae..a0736b8 100644
--- a/secure_fw/spm/model_ipc/CMakeLists.inc
+++ b/secure_fw/spm/model_ipc/CMakeLists.inc
@@ -42,7 +42,6 @@
 		"${SFW_IPC_SPM_DIR}/tfm_message_queue.c"
 		"${SFW_IPC_SPM_DIR}/../runtime/utilities.c"
 		"${SFW_IPC_SPM_DIR}/../runtime/tfm_core_utils.c"
-		"${SFW_IPC_SPM_DIR}/../runtime/spm_api.c"
 		"${SFW_IPC_SPM_DIR}/../runtime/tfm_spm_services.c"
 		"${SFW_IPC_SPM_DIR}/tfm_pools.c"
 		"${SFW_IPC_SPM_DIR}/tfm_thread.c"
diff --git a/secure_fw/spm/model_ipc/spm_ipc.c b/secure_fw/spm/model_ipc/spm_ipc.c
index 7cb3fc0..f15344c 100644
--- a/secure_fw/spm/model_ipc/spm_ipc.c
+++ b/secure_fw/spm/model_ipc/spm_ipc.c
@@ -290,6 +290,97 @@
     return NULL;
 }
 
+/**
+ * \brief Returns the index of the partition with the given partition ID.
+ *
+ * \param[in] partition_id     Partition id
+ *
+ * \return the partition idx if partition_id is valid,
+ *         \ref SPM_INVALID_PARTITION_IDX othervise
+ */
+static uint32_t get_partition_idx(uint32_t partition_id)
+{
+    uint32_t i;
+
+    if (partition_id == INVALID_PARTITION_ID) {
+        return SPM_INVALID_PARTITION_IDX;
+    }
+
+    for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
+        if (g_spm_partition_db.partitions[i].static_data->partition_id ==
+            partition_id) {
+            return i;
+        }
+    }
+    return SPM_INVALID_PARTITION_IDX;
+}
+
+/**
+ * \brief Get the flags associated with a partition
+ *
+ * \param[in] partition_idx     Partition index
+ *
+ * \return Flags associated with the partition
+ *
+ * \note This function doesn't check if partition_idx is valid.
+ */
+static uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
+{
+    return g_spm_partition_db.partitions[partition_idx].static_data->
+           partition_flags;
+}
+
+#if TFM_LVL != 1
+/**
+ * \brief Change the privilege mode for partition thread mode.
+ *
+ * \param[in] privileged        Privileged mode,
+ *                                \ref TFM_PARTITION_PRIVILEGED_MODE
+ *                                and \ref TFM_PARTITION_UNPRIVILEGED_MODE
+ *
+ * \note Barrier instructions are not called by this function, and if
+ *       it is called in thread mode, it might be necessary to call
+ *       them after this function returns.
+ */
+static void tfm_spm_partition_change_privilege(uint32_t privileged)
+{
+    CONTROL_Type ctrl;
+
+    ctrl.w = __get_CONTROL();
+
+    if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
+        ctrl.b.nPRIV = 0;
+    } else {
+        ctrl.b.nPRIV = 1;
+    }
+
+    __set_CONTROL(ctrl.w);
+}
+#endif /* if(TFM_LVL != 1) */
+
+uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
+{
+    return g_spm_partition_db.partitions[partition_idx].static_data->
+           partition_id;
+}
+
+uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
+{
+    if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
+        return TFM_PARTITION_PRIVILEGED_MODE;
+    } else {
+        return TFM_PARTITION_UNPRIVILEGED_MODE;
+    }
+}
+
+bool tfm_is_partition_privileged(uint32_t partition_idx)
+{
+    uint32_t flags = tfm_spm_partition_get_flags(partition_idx);
+
+    return tfm_spm_partition_get_privileged_mode(flags) ==
+           TFM_PARTITION_PRIVILEGED_MODE;
+}
+
 struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
 {
     uint32_t i, num;
diff --git a/secure_fw/spm/runtime/spm_api.c b/secure_fw/spm/runtime/spm_api.c
deleted file mode 100644
index 22db2ec..0000000
--- a/secure_fw/spm/runtime/spm_api.c
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-/* All the APIs defined in this file are common for library and IPC model. */
-
-#include "spm_api.h"
-#include "spm_db.h"
-
-/* Extern SPM variable */
-extern struct spm_partition_db_t g_spm_partition_db;
-
-uint32_t get_partition_idx(uint32_t partition_id)
-{
-    uint32_t i;
-
-    if (partition_id == INVALID_PARTITION_ID) {
-        return SPM_INVALID_PARTITION_IDX;
-    }
-
-    for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
-        if (g_spm_partition_db.partitions[i].static_data->partition_id ==
-                partition_id) {
-            return i;
-        }
-    }
-    return SPM_INVALID_PARTITION_IDX;
-}
-
-uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
-{
-    return g_spm_partition_db.partitions[partition_idx].static_data->
-            partition_id;
-}
-
-uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
-{
-    return g_spm_partition_db.partitions[partition_idx].static_data->
-            partition_flags;
-}
-
-uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
-{
-    if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
-        return TFM_PARTITION_PRIVILEGED_MODE;
-    } else {
-        return TFM_PARTITION_UNPRIVILEGED_MODE;
-    }
-}
-
-bool tfm_is_partition_privileged(uint32_t partition_idx)
-{
-    uint32_t flags = tfm_spm_partition_get_flags(partition_idx);
-
-    return tfm_spm_partition_get_privileged_mode(flags) ==
-        TFM_PARTITION_PRIVILEGED_MODE;
-}
-
-__attribute__((section("SFN")))
-void tfm_spm_partition_change_privilege(uint32_t privileged)
-{
-    CONTROL_Type ctrl;
-
-    ctrl.w = __get_CONTROL();
-
-    if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
-        ctrl.b.nPRIV = 0;
-    } else {
-        ctrl.b.nPRIV = 1;
-    }
-
-    __set_CONTROL(ctrl.w);
-}