Core: Enable non-consecutive IDs for partitions

This commit eliminates the linear mapping between the secure partition
IDs and partition data indexes. To do this, the PARTITION_ID_GET(id)
macro is removed, and find_partition_record is created to search through
the services DB for the desired service.

The commit also simplifies user_service_defines.inc. From now, calling
PARTITION_DECLARE() for a partition is enough, no need to call
PARTITION_ADD()

Change-Id: I69d064f3aa3fbca8171179571ae9cec1b92fdeb4
Signed-off-by: Mate Toth-Pal <mate.toth-pal@arm.com>
diff --git a/secure_fw/spm/spm_db.h b/secure_fw/spm/spm_db.h
index 01aed35..45b6871 100644
--- a/secure_fw/spm/spm_db.h
+++ b/secure_fw/spm/spm_db.h
@@ -5,10 +5,13 @@
  *
  */
 
-#ifndef __SPM_SPM_DB_H__
-#define __SPM_SPM_DB_H__
+#ifndef __SPM_DB_H__
+#define __SPM_DB_H__
 
 #include <stdint.h>
+#include "platform_retarget.h"
+#include "target_cfg.h"
+#include "spm_partition_defs.h"
 
 /* This limit is only used to define the size of the database reserved for
  * partitions. There's no requirement that it match the number of partitions
@@ -16,7 +19,10 @@
  */
 #define SPM_MAX_PARTITIONS (6)
 
-#define PARTITION_ID_GET(id)      (id - TFM_SP_BASE)
+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);
 
@@ -49,7 +55,7 @@
 struct spm_partition_db_t {
     uint32_t is_init;
     uint32_t partition_count;
-    uint32_t running_partition_id;
+    uint32_t running_partition_idx;
     struct spm_partition_desc_t partitions[SPM_MAX_PARTITIONS];
 };
 
@@ -64,164 +70,104 @@
     (uint32_t)&REGION_NAME(Image$$, partition, region)
 #endif
 
-#define PARTITION_DECLARE(partition)                       \
-    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); \
-
 
 #if TFM_LVL == 1
-#define PARTITION_INIT_STATIC_DATA(data, partition) \
-    data.partition_id    = partition##_ID;          \
-    data.periph_start    = 0U;                      \
-    data.periph_limit    = 0U;                      \
-    data.periph_ppc_bank = 0U;                      \
-    data.periph_ppc_loc  = 0U;                      \
-    data.partition_init  = 0U
+#define PARTITION_INIT_STATIC_DATA(data, partition)     \
+    do {                                                \
+        data.partition_id    = partition##_ID;          \
+        data.periph_start    = 0U;                      \
+        data.periph_limit    = 0U;                      \
+        data.periph_ppc_bank = 0U;                      \
+        data.periph_ppc_loc  = 0U;                      \
+        data.partition_init  = 0U;                      \
+    } while (0)
 #else
-#define PARTITION_INIT_STATIC_DATA(data, partition)                        \
-    data.partition_id    = partition##_ID;                                 \
-    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); \
-    data.periph_start    = 0U;                                             \
-    data.periph_limit    = 0U;                                             \
-    data.periph_ppc_bank = 0U;                                             \
-    data.periph_ppc_loc  = 0U;                                             \
-    data.partition_init  = 0U
+#define PARTITION_INIT_STATIC_DATA(data, partition)                            \
+    do {                                                                       \
+        data.partition_id    = partition##_ID;                                 \
+        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); \
+        data.periph_start    = 0U;                                             \
+        data.periph_limit    = 0U;                                             \
+        data.periph_ppc_bank = 0U;                                             \
+        data.periph_ppc_loc  = 0U;                                             \
+        data.partition_init  = 0U;                                             \
+    } while (0)
 #endif
 
 #if TFM_LVL == 1
-#define DUMMY_PARTITION_INIT_STATIC_DATA(data, partition)                     \
-        /* In case of TFM_LVL1 the static data is initialised the same way */ \
-        PARTITION_INIT_STATIC_DATA(data, partition)
+#define PARTITION_INIT_RUNTIME_DATA(data, partition)            \
+    do {                                                        \
+        data.partition_state      = SPM_PARTITION_STATE_UNINIT; \
+        data.caller_partition_idx = 0U;                         \
+        data.orig_psp             = 0U;                         \
+        data.orig_psplim          = 0U;                         \
+        data.orig_lr              = 0U;                         \
+        data.share                = 0U;                         \
+    } while (0)
 #else
-#define DUMMY_PARTITION_INIT_STATIC_DATA(data, partition) \
-    data.partition_id    = partition##_ID;                \
-    data.code_start      = 0U;                            \
-    data.code_limit      = 0U;                            \
-    data.ro_start        = 0U;                            \
-    data.ro_limit        = 0U;                            \
-    data.rw_start        = 0U;                            \
-    data.rw_limit        = 0U;                            \
-    data.zi_start        = 0U;                            \
-    data.zi_limit        = 0U;                            \
-    data.stack_bottom    = 0U;                            \
-    data.stack_top       = 0U;                            \
-    data.periph_start    = 0U;                            \
-    data.periph_limit    = 0U;                            \
-    data.periph_ppc_bank = 0U;                            \
-    data.periph_ppc_loc  = 0U;                            \
-    data.partition_init  = 0U
+#define PARTITION_INIT_RUNTIME_DATA(data, partition)                \
+    do {                                                            \
+        data.partition_state      = SPM_PARTITION_STATE_UNINIT;     \
+        data.caller_partition_idx = 0U;                             \
+        data.orig_psp             = 0U;                             \
+        data.orig_psplim          = 0U;                             \
+        data.orig_lr              = 0U;                             \
+        data.share                = 0U;                             \
+        data.stack_ptr            =                                 \
+                PART_REGION_ADDR(partition, _STACK$$ZI$$Limit);     \
+    } while (0)
 #endif
 
+#define PARTITION_DECLARE(partition)                                    \
+    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);   \
+        PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition); \
+        ++g_spm_partition_db.partition_count;                           \
+    } while (0)
 
-#if TFM_LVL == 1
-#define PARTITION_INIT_RUNTIME_DATA(data, partition)                           \
-    data.partition_state     = SPM_PARTITION_STATE_UNINIT;                     \
-    data.caller_partition_id = 0U;                                             \
-    data.orig_psp            = 0U;                                             \
-    data.orig_psplim         = 0U;                                             \
-    data.orig_lr             = 0U;                                             \
-    data.share               = 0U
-#else
-#define PARTITION_INIT_RUNTIME_DATA(data, partition)                           \
-    data.partition_state     = SPM_PARTITION_STATE_UNINIT;                     \
-    data.caller_partition_id = 0U;                                             \
-    data.orig_psp            = 0U;                                             \
-    data.orig_psplim         = 0U;                                             \
-    data.orig_lr             = 0U;                                             \
-    data.share               = 0U;                                             \
-    data.stack_ptr           = PART_REGION_ADDR(partition, _STACK$$ZI$$Limit)
-#endif
+#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)
 
-#if TFM_LVL == 1
-#define DUMMY_PARTITION_INIT_RUNTIME_DATA(data, partition) \
-    data.partition_state     = SPM_PARTITION_STATE_UNINIT; \
-    data.caller_partition_id = 0U;                         \
-    data.orig_psp            = 0U;                         \
-    data.orig_psplim         = 0U;                         \
-    data.orig_lr             = 0U;                         \
-    data.share               = 0U
-#else
-#define DUMMY_PARTITION_INIT_RUNTIME_DATA(data, partition) \
-    data.partition_state     = SPM_PARTITION_STATE_UNINIT; \
-    data.caller_partition_id = 0U;                         \
-    data.orig_psp            = 0U;                         \
-    data.orig_psplim         = 0U;                         \
-    data.orig_lr             = 0U;                         \
-    data.share               = 0U;                         \
-    data.stack_ptr           = 0U
-#endif
+#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)
 
-#define PARTITION_ADD(partition) {                                      \
-    if (index >= max_partitions) {                                      \
-        return max_partitions;                                          \
-    }                                                                   \
-    part_ptr = (struct spm_partition_desc_t *)&(db->partitions[index]); \
-    PARTITION_INIT_STATIC_DATA(part_ptr->static_data, partition);       \
-    PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition);     \
-    index++;                                                            \
-    }
-
-#define DUMMY_PARTITION_ADD(partition) {                                  \
-    if (index >= max_partitions) {                                        \
-        return max_partitions;                                            \
-    }                                                                     \
-    part_ptr = (struct spm_partition_desc_t *)&(db->partitions[index]);   \
-    DUMMY_PARTITION_INIT_STATIC_DATA(part_ptr->static_data, partition);   \
-    /* The runtime data is the same for the dummy partitions*/            \
-    DUMMY_PARTITION_INIT_RUNTIME_DATA(part_ptr->runtime_data, partition); \
-    index++;                                                              \
-    }
-
-#define PARTITION_ADD_PERIPHERAL(partition, start, limit, bank, loc) {  \
-        part_ptr = &(db->partitions[PARTITION_ID_GET(partition##_ID)]); \
-        ((struct spm_partition_desc_t *)part_ptr)->                     \
-               static_data.periph_start = start;                        \
-        ((struct spm_partition_desc_t *)part_ptr)->                     \
-               static_data.periph_limit = limit;                        \
-        ((struct spm_partition_desc_t *)part_ptr)->                     \
-               static_data.periph_ppc_bank = bank;                      \
-        ((struct spm_partition_desc_t *)part_ptr)->                     \
-               static_data.periph_ppc_loc = loc;                        \
-    }
-
-#define PARTITION_ADD_INIT_FUNC(partition, init_func) {                 \
-        extern int32_t init_func(void);                                 \
-        part_ptr = &(db->partitions[PARTITION_ID_GET(partition##_ID)]); \
-        ((struct spm_partition_desc_t *)part_ptr)->                     \
-               static_data.partition_init = init_func;                  \
-    }
-
-/*This file is meant to be included twice*/
-#include "user_service_defines.inc"
-
-struct spm_partition_db_t;
-
-uint32_t create_user_partition_db(struct spm_partition_db_t *db,
-                                uint32_t max_partitions)
-{
-    uint32_t index = 0;
-    struct spm_partition_desc_t *part_ptr;
-
-#include "user_service_defines.inc"
-
-    return index;
-}
-
-#endif /* __SPM_SPM_DB_H__ */
+#endif /* __SPM_DB_H__ */