Core: Eliminate SPM DB initialising code

This commit modifies template files related to the SPM DB so that
instead of generating code that initialises the DB, a C structure
initialiser is generated directly for the DB.

Change-Id: I88c2cd077a15101d24559027a73716a2f0a5c185
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 9b3f3f2..ffca849 100644
--- a/secure_fw/spm/spm_api.c
+++ b/secure_fw/spm/spm_api.c
@@ -12,7 +12,7 @@
 #include "spm_api.h"
 #include "platform/include/tfm_spm_hal.h"
 #include "tfm_memory_utils.h"
-#include "spm_db_setup.h"
+#include "spm_db.h"
 #include "tfm_internal.h"
 #include "tfm_api.h"
 #include "tfm_nspm.h"
@@ -20,8 +20,8 @@
 #include "tfm_peripherals_def.h"
 #include "spm_partition_defs.h"
 
-
-struct spm_partition_db_t g_spm_partition_db = {0,};
+#define NON_SECURE_INTERNAL_PARTITION_DB_IDX 0
+#define TFM_CORE_INTERNAL_PARTITION_DB_IDX   1
 
 typedef enum {
     TFM_INIT_FAILURE,
@@ -45,6 +45,9 @@
     uint32_t caller_partition_idx;
 };
 
+/* Define SPM DB structure */
+#include "secure_fw/services/tfm_spm_db.inc"
+
 /*
  * This function is called when a secure partition causes an error.
  * In case of an error in the error handling, a non-zero value have to be
@@ -74,17 +77,6 @@
 }
 #endif /* !defined(TFM_PSA_API) */
 
-/*
- * This function prevents name clashes between the variable names accessibles in
- * the scope of where tfm_partition_list.inc is included and the varaible names
- * defined inside tfm_partition_list.inc file.
- */
-static inline enum spm_err_t add_user_defined_partitions(void) {
-    #include "secure_fw/services/tfm_partition_list.inc"
-
-    return SPM_ERR_OK;
-}
-
 uint32_t get_partition_idx(uint32_t partition_id)
 {
     uint32_t i;
@@ -104,18 +96,13 @@
 
 enum spm_err_t tfm_spm_db_init(void)
 {
-    struct spm_partition_desc_t *part_ptr;
-    enum spm_err_t err;
+   struct spm_partition_desc_t *part_ptr;
     static uint32_t ns_interrupt_ctx_stack[
            sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0};
     static uint32_t tfm_core_interrupt_ctx_stack[
            sizeof(struct interrupted_ctx_stack_frame_t)/sizeof(uint32_t)] = {0};
 
-    (void)tfm_memset (&g_spm_partition_db, 0, sizeof(g_spm_partition_db));
-
     /* This function initialises partition db */
-    g_spm_partition_db.running_partition_idx = SPM_INVALID_PARTITION_IDX;
-    g_spm_partition_db.partition_count = 0;
 
     /* There are a few partitions that are used by TF-M internally.
      * These are explicitly added to the partition db here.
@@ -128,11 +115,9 @@
     uint32_t psp_stack_bottom = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base;
     uint32_t psp_stack_top    = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Limit;
 #endif
-    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]);
+                                         NON_SECURE_INTERNAL_PARTITION_DB_IDX]);
     part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
 #ifdef TFM_PSA_API
     part_ptr->static_data.partition_flags = SPM_PART_FLAG_APP_ROT |
@@ -155,25 +140,15 @@
     part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
     part_ptr->runtime_data.ctx_stack_ptr = ns_interrupt_ctx_stack;
     tfm_nspm_configure_clients();
-    ++g_spm_partition_db.partition_count;
 
     /* For the TF-M core environment itself */
-    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]);
+                                           TFM_CORE_INTERNAL_PARTITION_DB_IDX]);
     part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
     part_ptr->static_data.partition_flags =
                     SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_PSA_ROT;
     part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
     part_ptr->runtime_data.ctx_stack_ptr = tfm_core_interrupt_ctx_stack;
-    ++g_spm_partition_db.partition_count;
-
-    err = add_user_defined_partitions();
-    if (err != SPM_ERR_OK) {
-        return err;
-    }
 
     g_spm_partition_db.is_init = 1;
 
diff --git a/secure_fw/spm/spm_db.h b/secure_fw/spm/spm_db.h
index a3af8be..8b1a00b 100644
--- a/secure_fw/spm/spm_db.h
+++ b/secure_fw/spm/spm_db.h
@@ -61,6 +61,13 @@
 #endif
 };
 
+struct spm_partition_db_t {
+    uint32_t is_init;
+    uint32_t partition_count;
+    uint32_t running_partition_idx;
+    struct spm_partition_desc_t *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)
diff --git a/secure_fw/spm/spm_db_setup.h b/secure_fw/spm/spm_db_setup.h
deleted file mode 100644
index 31696dc..0000000
--- a/secure_fw/spm/spm_db_setup.h
+++ /dev/null
@@ -1,151 +0,0 @@
-/*
- * Copyright (c) 2018-2019, 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 Get the index of a partition.
- *
- * Gets the index of a partition in the partition db based on the partition ID
- * provided as a parameter.
- *
- * \param[in] partition_idx    The index of the partition
- *
- * \return \ref INVALID_PARTITION_IDX if the provided index 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 spm_partition_desc_t partitions[SPM_MAX_PARTITIONS];
-};
-
-#define PARTITION_INIT_STATIC_DATA(data, partition, flags, id, priority)      \
-    do {                                                                      \
-        data.partition_id    = partition##_ID;                                \
-        data.partition_flags = flags;                                         \
-        data.partition_priority = TFM_PRIORITY(priority);                     \
-    } while (0)
-
-#if (TFM_LVL == 1) && !defined(TFM_PSA_API)
-#define PARTITION_INIT_MEMORY_DATA(data, partition)
-#else
-#define PARTITION_INIT_MEMORY_DATA(data, partition)                            \
-    do {                                                                       \
-        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
-
-/* The max size of the context stack can be calculated as a function of the IRQ
- * count of the secure partition:
- *
- * max_stack_size = intr_ctx_size + (IRQ_CNT * (intr_ctx_size + hndl_ctx_size))
- *
- * where:
- *   intr_ctx: Frame pushed when the partition is interrupted
- *   hndl_ctx: Frame pushed when the partition is handling an interrupt
- */
-#define DECLARE_CONTEXT_STACK(partition)                            \
-        static uint32_t ctx_stack_ptr_##partition[                  \
-            (sizeof(struct interrupted_ctx_stack_frame_t) +         \
-                (TFM_PARTITION_##partition##_IRQ_COUNT) * (         \
-                    sizeof(struct interrupted_ctx_stack_frame_t) +  \
-                    sizeof(struct handler_ctx_stack_frame_t)        \
-            )) / sizeof(uint32_t)]
-
-#if TFM_LVL == 1
-#define PARTITION_INIT_RUNTIME_DATA(data, partition)                \
-    do {                                                            \
-        DECLARE_CONTEXT_STACK(partition);                           \
-        data.partition_state = SPM_PARTITION_STATE_UNINIT;          \
-        data.ctx_stack_ptr = ctx_stack_ptr_##partition;             \
-    } while (0)
-#else
-#define PARTITION_INIT_RUNTIME_DATA(data, partition)                \
-    do {                                                            \
-        DECLARE_CONTEXT_STACK(partition);                           \
-        data.partition_state = SPM_PARTITION_STATE_UNINIT;          \
-        /* The top of the stack is reserved for the iovec        */ \
-        /* parameters of the service called. That's why in       */ \
-        /* data.stack_ptr we extract sizeof(struct iovec_args_t) */ \
-        /* from the limit.                                       */ \
-        data.stack_ptr =                                            \
-                PART_REGION_ADDR(partition, _STACK$$ZI$$Limit -     \
-                                  sizeof(struct iovec_args_t));     \
-        data.ctx_stack_ptr = ctx_stack_ptr_##partition;             \
-    } while (0)
-#endif
-
-#define PARTITION_DECLARE(partition, flag, type, id, priority)               \
-    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);               \
-        int32_t flags = flag;                                                \
-        if (tfm_memcmp(type, TFM_PARTITION_TYPE_APP,                         \
-            strlen(TFM_PARTITION_TYPE_APP)) == 0) {                          \
-            flags |= SPM_PART_FLAG_APP_ROT;                                  \
-        } else if (tfm_memcmp(type, TFM_PARTITION_TYPE_PSA,                  \
-                   strlen(TFM_PARTITION_TYPE_PSA)) == 0) {                   \
-            flags |= SPM_PART_FLAG_PSA_ROT | SPM_PART_FLAG_APP_ROT;          \
-        } else {                                                             \
-            return SPM_ERR_INVALID_CONFIG;                                   \
-        }                                                                    \
-        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,  \
-                                   id, priority);                            \
-        PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition);      \
-        PARTITION_INIT_MEMORY_DATA(part_ptr->memory_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, peripheral)                    \
-    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->platform_data = peripheral;                              \
-    } while (0)
-
-#endif /* __SPM_DB_SETUP_H__ */