Platform: Create files for spm hal

 - Create spm_hal.c and spm_hal.h files to later contain platform
   dependent isolation hardware code
 - Move isolation hardware specific partition database fields to
   spm_hal.h
 - Move the code from spm_db.h that have to be included only once to
   spm_db_setup.h
 - Adapt cmake system to the new file structure

Change-Id: Ib80e4b3c366b2b4038739bc28a02b165d3402832
Signed-off-by: Mate Toth-Pal <mate.toth-pal@arm.com>
diff --git a/secure_fw/spm/spm_api.c b/secure_fw/spm/spm_api.c
index 67fc42c..fd25dc3 100644
--- a/secure_fw/spm/spm_api.c
+++ b/secure_fw/spm/spm_api.c
@@ -10,12 +10,17 @@
 #include <stdio.h>
 #include <string.h>
 #include "spm_api.h"
-#include "spm_db.h"
+#include "platform/include/tfm_spm_hal.h"
+#include "spm_db_setup.h"
 #include "tfm_internal.h"
 #include "tfm_api.h"
 #include "mpu_armv8m_drv.h"
 #include "region_defs.h"
 #include "secure_fw/core/tfm_core.h"
+#include "platform_retarget.h"
+#include "target_cfg.h"
+#include "spm_partition_defs.h"
+
 
 struct spm_partition_db_t g_spm_partition_db = {0,};
 
@@ -41,7 +46,7 @@
  * returned.
  */
 static void tfm_spm_partition_err_handler(
-    struct spm_partition_desc_t *partition,
+    struct tfm_spm_partition_desc_t *partition,
     sp_error_type_t err_type,
     int32_t err_code)
 {
@@ -77,7 +82,7 @@
 
 enum spm_err_t tfm_spm_db_init(void)
 {
-    struct spm_partition_desc_t *part_ptr;
+    struct tfm_spm_partition_desc_t *part_ptr;
 
     memset (&g_spm_partition_db, 0, sizeof(g_spm_partition_db));
 
@@ -256,7 +261,7 @@
 
 enum spm_err_t tfm_spm_partition_init(void)
 {
-    struct spm_partition_desc_t *part;
+    struct tfm_spm_partition_desc_t *part;
     struct tfm_sfn_req_s desc, *desc_ptr = &desc;
     int32_t args[4] = {0};
     int32_t fail_cnt = 0;
@@ -265,9 +270,9 @@
     /* Call the init function for each partition */
     for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
         part = &g_spm_partition_db.partitions[idx];
-        if (part->static_data.periph_start) {
-            ppc_configure_to_secure(part->static_data.periph_ppc_bank,
-                    part->static_data.periph_ppc_loc);
+        if (part->platform_data.periph_start) {
+            ppc_configure_to_secure(part->platform_data.periph_ppc_bank,
+                    part->platform_data.periph_ppc_loc);
         }
         if (part->static_data.partition_init == NULL) {
             tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
@@ -313,7 +318,7 @@
      * SPM partition for that partition
      */
 
-    struct spm_partition_desc_t *part;
+    struct tfm_spm_partition_desc_t *part;
     struct mpu_armv8m_region_cfg_t region_cfg;
 
     if (!g_spm_partition_db.is_init) {
@@ -354,11 +359,11 @@
         return SPM_ERR_INVALID_CONFIG;
     }
 
-    if (part->static_data.periph_start) {
+    if (part->platform_data.periph_start) {
         /* Peripheral */
         region_cfg.region_nr = PARTITION_REGION_PERIPH;
-        region_cfg.region_base = part->static_data.periph_start;
-        region_cfg.region_limit = part->static_data.periph_limit;
+        region_cfg.region_base = part->platform_data.periph_start;
+        region_cfg.region_limit = part->platform_data.periph_limit;
         region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV;
         region_cfg.attr_sh = MPU_ARMV8M_SH_NONE;
         region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER;
@@ -367,8 +372,8 @@
             return SPM_ERR_INVALID_CONFIG;
         }
 
-        ppc_en_secure_unpriv(part->static_data.periph_ppc_bank,
-                             part->static_data.periph_ppc_loc);
+        ppc_en_secure_unpriv(part->platform_data.periph_ppc_bank,
+                             part->platform_data.periph_ppc_loc);
     }
 
     mpu_armv8m_enable(&dev_mpu_s, 1, 1);
@@ -382,14 +387,14 @@
      * SPM partition for that partition
      */
 
-    struct spm_partition_desc_t *part;
+    struct tfm_spm_partition_desc_t *part;
 
     part = &g_spm_partition_db.partitions[partition_idx];
 
-    if (part->static_data.periph_start) {
+    if (part->platform_data.periph_start) {
         /* Peripheral */
-        ppc_clr_secure_unpriv(part->static_data.periph_ppc_bank,
-                              part->static_data.periph_ppc_loc);
+        ppc_clr_secure_unpriv(part->platform_data.periph_ppc_bank,
+                              part->platform_data.periph_ppc_loc);
     }
 
     mpu_armv8m_disable(&dev_mpu_s);
@@ -496,7 +501,7 @@
 
 void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
 {
-    struct spm_partition_desc_t *partition =
+    struct tfm_spm_partition_desc_t *partition =
             &(g_spm_partition_db.partitions[partition_idx]);
     partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
     partition->runtime_data.orig_psp = 0;
diff --git a/secure_fw/spm/spm_db.h b/secure_fw/spm/spm_db.h
index 4b92557..d267f6d 100644
--- a/secure_fw/spm/spm_db.h
+++ b/secure_fw/spm/spm_db.h
@@ -8,18 +8,13 @@
 #ifndef __SPM_DB_H__
 #define __SPM_DB_H__
 
-#include <stdint.h>
-#include "platform_retarget.h"
-#include "target_cfg.h"
-#include "spm_partition_defs.h"
-
-struct spm_partition_desc_t;
-struct spm_partition_db_t;
-
-uint32_t get_partition_idx(uint32_t partition_id);
-
 typedef int32_t(*sp_init_function)(void);
 
+/**
+ * Holds the fields of the partition DB used by the SPM code. The values of
+ * these fields are calculated at compile time, and set during initialisation
+ * phase.
+ */
 struct spm_partition_static_data_t {
     uint32_t partition_id;
     uint32_t partition_flags;
@@ -35,116 +30,17 @@
     uint32_t stack_bottom;
     uint32_t stack_top;
 #endif
-    uint32_t periph_start;
-    uint32_t periph_limit;
-    uint16_t periph_ppc_bank;
-    uint16_t periph_ppc_loc;
     sp_init_function partition_init;
 };
 
-struct spm_partition_desc_t {
+/**
+ * Holds the fields that define a partition for SPM. The fields are further
+ * divided to structures, to keep the related fields close to each other.
+ */
+struct tfm_spm_partition_desc_t {
     struct spm_partition_static_data_t static_data;
     struct spm_partition_runtime_data_t runtime_data;
+    struct tfm_spm_partition_platform_data_t platform_data;
 };
 
-struct spm_partition_db_t {
-    uint32_t is_init;
-    uint32_t partition_count;
-    uint32_t running_partition_idx;
-    struct spm_partition_desc_t partitions[SPM_MAX_PARTITIONS];
-};
-
-/* Macros to pick linker symbols and allow to form the partition data base */
-#define REGION(a, b, c) a##b##c
-#define REGION_NAME(a, b, c) REGION(a, b, c)
-#if TFM_LVL == 1
-#define REGION_DECLARE(a, b, c)
-#else
-#define REGION_DECLARE(a, b, c) extern uint32_t REGION_NAME(a, b, c)
-#define PART_REGION_ADDR(partition, region) \
-    (uint32_t)&REGION_NAME(Image$$, partition, region)
-#endif
-
-
-#if TFM_LVL == 1
-#define PARTITION_INIT_STATIC_DATA(data, partition, flags) \
-    do {                                                   \
-        data.partition_id    = partition##_ID;             \
-        data.partition_flags = flags;                      \
-    } while (0)
-#else
-#define PARTITION_INIT_STATIC_DATA(data, partition, flags)                     \
-    do {                                                                       \
-        data.partition_id    = partition##_ID;                                 \
-        data.partition_flags = flags;                                          \
-        data.code_start      = PART_REGION_ADDR(partition, $$Base);            \
-        data.code_limit      = PART_REGION_ADDR(partition, $$Limit);           \
-        data.ro_start        = PART_REGION_ADDR(partition, $$RO$$Base);        \
-        data.ro_limit        = PART_REGION_ADDR(partition, $$RO$$Limit);       \
-        data.rw_start        = PART_REGION_ADDR(partition, _DATA$$RW$$Base);   \
-        data.rw_limit        = PART_REGION_ADDR(partition, _DATA$$RW$$Limit);  \
-        data.zi_start        = PART_REGION_ADDR(partition, _DATA$$ZI$$Base);   \
-        data.zi_limit        = PART_REGION_ADDR(partition, _DATA$$ZI$$Limit);  \
-        data.stack_bottom    = PART_REGION_ADDR(partition, _STACK$$ZI$$Base);  \
-        data.stack_top       = PART_REGION_ADDR(partition, _STACK$$ZI$$Limit); \
-    } while (0)
-#endif
-
-#if TFM_LVL == 1
-#define PARTITION_INIT_RUNTIME_DATA(data, partition)            \
-    do {                                                        \
-        data.partition_state      = SPM_PARTITION_STATE_UNINIT; \
-    } while (0)
-#else
-#define PARTITION_INIT_RUNTIME_DATA(data, partition)                \
-    do {                                                            \
-        data.partition_state      = SPM_PARTITION_STATE_UNINIT;     \
-        data.stack_ptr            =                                 \
-                PART_REGION_ADDR(partition, _STACK$$ZI$$Limit);     \
-    } while (0)
-#endif
-
-#define PARTITION_DECLARE(partition, flags)                                  \
-    do {                                                                     \
-        REGION_DECLARE(Image$$, partition, $$Base);                          \
-        REGION_DECLARE(Image$$, partition, $$Limit);                         \
-        REGION_DECLARE(Image$$, partition, $$RO$$Base);                      \
-        REGION_DECLARE(Image$$, partition, $$RO$$Limit);                     \
-        REGION_DECLARE(Image$$, partition, _DATA$$RW$$Base);                 \
-        REGION_DECLARE(Image$$, partition, _DATA$$RW$$Limit);                \
-        REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Base);                 \
-        REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Limit);                \
-        REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Base);                \
-        REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Limit);               \
-        struct spm_partition_desc_t *part_ptr;                               \
-        if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {      \
-            return SPM_ERR_INVALID_CONFIG;                                   \
-        }                                                                    \
-        part_ptr = &(g_spm_partition_db.partitions[                          \
-            g_spm_partition_db.partition_count]);                            \
-        PARTITION_INIT_STATIC_DATA(part_ptr->static_data, partition, flags); \
-        PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition);      \
-        ++g_spm_partition_db.partition_count;                                \
-    } while (0)
-
-#define PARTITION_ADD_INIT_FUNC(partition, init_func)                 \
-    do {                                                              \
-        extern int32_t init_func(void);                               \
-        uint32_t partition_idx = get_partition_idx(partition##_ID);   \
-        struct spm_partition_desc_t *part_ptr =                       \
-            &(g_spm_partition_db.partitions[partition_idx]);          \
-        part_ptr->static_data.partition_init = init_func;             \
-    } while (0)
-
-#define PARTITION_ADD_PERIPHERAL(partition, start, limit, bank, loc)   \
-    do {                                                               \
-        uint32_t partition_idx = get_partition_idx(partition##_ID);    \
-        struct spm_partition_desc_t *part_ptr =                        \
-            &(g_spm_partition_db.partitions[partition_idx]);           \
-        part_ptr->static_data.periph_start = start;                    \
-        part_ptr->static_data.periph_limit = limit;                    \
-        part_ptr->static_data.periph_ppc_bank = bank;                  \
-        part_ptr->static_data.periph_ppc_loc = loc;                    \
-    } while (0)
-
 #endif /* __SPM_DB_H__ */
diff --git a/secure_fw/spm/spm_db_setup.h b/secure_fw/spm/spm_db_setup.h
new file mode 100644
index 0000000..4862798
--- /dev/null
+++ b/secure_fw/spm/spm_db_setup.h
@@ -0,0 +1,127 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __SPM_DB_SETUP_H__
+#define __SPM_DB_SETUP_H__
+
+#include <stdint.h>
+#include "spm_db.h"
+
+/**
+ * \brief Return the index of a partition.
+ *
+ * Returns the index of a partition in the partition db based on the partition
+ * ID provided as a parameter.
+ *
+ * \param[in] partition_id    The ID of the partition
+ *
+ * \return \ref INVALID_PARTITION_IDX if the provided ID is invalid. The index
+ *         of the partition otherwise.
+ */
+uint32_t get_partition_idx(uint32_t partition_id);
+
+struct spm_partition_db_t {
+    uint32_t is_init;
+    uint32_t partition_count;
+    uint32_t running_partition_idx;
+    struct tfm_spm_partition_desc_t partitions[SPM_MAX_PARTITIONS];
+};
+
+/* Macros to pick linker symbols and allow to form the partition data base */
+#define REGION(a, b, c) a##b##c
+#define REGION_NAME(a, b, c) REGION(a, b, c)
+#if TFM_LVL == 1
+#define REGION_DECLARE(a, b, c)
+#else
+#define REGION_DECLARE(a, b, c) extern uint32_t REGION_NAME(a, b, c)
+#define PART_REGION_ADDR(partition, region) \
+    (uint32_t)&REGION_NAME(Image$$, partition, region)
+#endif
+
+
+#if TFM_LVL == 1
+#define PARTITION_INIT_STATIC_DATA(data, partition, flags) \
+    do {                                                   \
+        data.partition_id    = partition##_ID;             \
+        data.partition_flags = flags;                      \
+    } while (0)
+#else
+#define PARTITION_INIT_STATIC_DATA(data, partition, flags)                     \
+    do {                                                                       \
+        data.partition_id    = partition##_ID;                                 \
+        data.partition_flags = flags;                                          \
+        data.code_start      = PART_REGION_ADDR(partition, $$Base);            \
+        data.code_limit      = PART_REGION_ADDR(partition, $$Limit);           \
+        data.ro_start        = PART_REGION_ADDR(partition, $$RO$$Base);        \
+        data.ro_limit        = PART_REGION_ADDR(partition, $$RO$$Limit);       \
+        data.rw_start        = PART_REGION_ADDR(partition, _DATA$$RW$$Base);   \
+        data.rw_limit        = PART_REGION_ADDR(partition, _DATA$$RW$$Limit);  \
+        data.zi_start        = PART_REGION_ADDR(partition, _DATA$$ZI$$Base);   \
+        data.zi_limit        = PART_REGION_ADDR(partition, _DATA$$ZI$$Limit);  \
+        data.stack_bottom    = PART_REGION_ADDR(partition, _STACK$$ZI$$Base);  \
+        data.stack_top       = PART_REGION_ADDR(partition, _STACK$$ZI$$Limit); \
+    } while (0)
+#endif
+
+#if TFM_LVL == 1
+#define PARTITION_INIT_RUNTIME_DATA(data, partition)            \
+    do {                                                        \
+        data.partition_state      = SPM_PARTITION_STATE_UNINIT; \
+    } while (0)
+#else
+#define PARTITION_INIT_RUNTIME_DATA(data, partition)                \
+    do {                                                            \
+        data.partition_state      = SPM_PARTITION_STATE_UNINIT;     \
+        data.stack_ptr            =                                 \
+                PART_REGION_ADDR(partition, _STACK$$ZI$$Limit);     \
+    } while (0)
+#endif
+
+#define PARTITION_DECLARE(partition, flags)                                  \
+    do {                                                                     \
+        REGION_DECLARE(Image$$, partition, $$Base);                          \
+        REGION_DECLARE(Image$$, partition, $$Limit);                         \
+        REGION_DECLARE(Image$$, partition, $$RO$$Base);                      \
+        REGION_DECLARE(Image$$, partition, $$RO$$Limit);                     \
+        REGION_DECLARE(Image$$, partition, _DATA$$RW$$Base);                 \
+        REGION_DECLARE(Image$$, partition, _DATA$$RW$$Limit);                \
+        REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Base);                 \
+        REGION_DECLARE(Image$$, partition, _DATA$$ZI$$Limit);                \
+        REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Base);                \
+        REGION_DECLARE(Image$$, partition, _STACK$$ZI$$Limit);               \
+        struct tfm_spm_partition_desc_t *part_ptr;                           \
+        if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {      \
+            return SPM_ERR_INVALID_CONFIG;                                   \
+        }                                                                    \
+        part_ptr = &(g_spm_partition_db.partitions[                          \
+            g_spm_partition_db.partition_count]);                            \
+        PARTITION_INIT_STATIC_DATA(part_ptr->static_data, partition, flags); \
+        PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition);      \
+        ++g_spm_partition_db.partition_count;                                \
+    } while (0)
+
+#define PARTITION_ADD_INIT_FUNC(partition, init_func)                 \
+    do {                                                              \
+        extern int32_t init_func(void);                               \
+        uint32_t partition_idx = get_partition_idx(partition##_ID);   \
+        struct tfm_spm_partition_desc_t *part_ptr =                   \
+            &(g_spm_partition_db.partitions[partition_idx]);          \
+        part_ptr->static_data.partition_init = init_func;             \
+    } while (0)
+
+#define PARTITION_ADD_PERIPHERAL(partition, start, limit, bank, loc)   \
+    do {                                                               \
+        uint32_t partition_idx = get_partition_idx(partition##_ID);    \
+        struct tfm_spm_partition_desc_t *part_ptr =                    \
+            &(g_spm_partition_db.partitions[partition_idx]);           \
+        part_ptr->platform_data.periph_start = start;                  \
+        part_ptr->platform_data.periph_limit = limit;                  \
+        part_ptr->platform_data.periph_ppc_bank = bank;                \
+        part_ptr->platform_data.periph_ppc_loc = loc;                  \
+    } while (0)
+
+#endif /* __SPM_DB_SETUP_H__ */