Core: Minimize the memory usage for partition list

Split the rodata, data and bss of the partition list.

Change-Id: I1b7e876ceee38b65b871b6be664d46bc7bd49af3
Signed-off-by: Summer Qin <summer.qin@arm.com>
diff --git a/secure_fw/services/tfm_spm_db.inc.template b/secure_fw/services/tfm_spm_db.inc.template
index f647639..f58dd30 100644
--- a/secure_fw/services/tfm_spm_db.inc.template
+++ b/secure_fw/services/tfm_spm_db.inc.template
@@ -32,6 +32,10 @@
 /**************************************************************************/
 /** Declarations of partition init functions */
 /**************************************************************************/
+#ifdef TFM_PSA_API
+extern psa_status_t tfm_nspm_thread_entry(void);
+#endif
+
 {% for manifest in manifests %}
     {% if manifest.attr.conditional %}
 #ifdef {{manifest.attr.conditional}}
@@ -45,6 +49,10 @@
 /**************************************************************************/
 /** Memory region declarations */
 /**************************************************************************/
+#ifdef TFM_PSA_API
+REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
+REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Limit);
+
 {% for manifest in manifests %}
     {% if manifest.attr.conditional %}
 #ifdef {{manifest.attr.conditional}}
@@ -64,6 +72,8 @@
     {% endif %}
 
 {% endfor %}
+#endif /* defined(TFM_PSA_API) */
+
 #ifndef TFM_PSA_API
 /**************************************************************************/
 /** Context stacks for IRQ handling */
@@ -77,6 +87,12 @@
  *   intr_ctx: Frame pushed when the partition is interrupted
  *   hndl_ctx: Frame pushed when the partition is handling an interrupt
  */
+static uint32_t ns_interrupt_ctx_stack[
+        sizeof(struct interrupted_ctx_stack_frame_t) / sizeof(uint32_t)];
+
+static uint32_t tfm_core_interrupt_ctx_stack[
+        sizeof(struct interrupted_ctx_stack_frame_t) / sizeof(uint32_t)];
+
 {% for manifest in manifests %}
     {% if manifest.attr.conditional %}
 #ifdef {{manifest.attr.conditional}}
@@ -92,16 +108,158 @@
     {% endif %}
 
 {% endfor %}
+
+uint32_t *ctx_stack_list[] =
+{
+    ns_interrupt_ctx_stack,
+    tfm_core_interrupt_ctx_stack,
+{% for manifest in manifests %}
+    {% if manifest.attr.conditional %}
+#ifdef {{manifest.attr.conditional}}
+    {% endif %}
+    ctx_stack_{{manifest.manifest.name}},
+    {% if manifest.attr.conditional %}
+#endif /* {{manifest.attr.conditional}} */
+    {% endif %}
+{% endfor %}
+};
 #endif /* !defined(TFM_PSA_API) */
 
 /**************************************************************************/
+/** The static data of the partition list */
+/**************************************************************************/
+const struct spm_partition_static_data_t static_data_list[] =
+{
+    {
+        .partition_id         = TFM_SP_NON_SECURE_ID,
+#ifdef TFM_PSA_API
+        .partition_flags      = SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_IPC,
+        .partition_priority   = TFM_PRIORITY_LOW,
+        .partition_init       = tfm_nspm_thread_entry,
+#else
+        .partition_flags      = 0,
+#endif
+    },
+
+#ifndef TFM_PSA_API
+    {
+        .partition_id         = TFM_SP_CORE_ID,
+        .partition_flags      = SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_PSA_ROT,
+    },
+#endif
+
+{% for manifest in manifests %}
+    {% if manifest.attr.conditional %}
+#ifdef {{manifest.attr.conditional}}
+    {% endif %}
+    {{'{'}}
+        .partition_id         = {{manifest.manifest.name}}_ID,
+    {% if manifest.attr.tfm_partition_ipc %}
+        .partition_flags      = SPM_PART_FLAG_IPC
+    {% else %}
+        .partition_flags      = 0
+    {% endif %}
+    {% if manifest.manifest.type == "APPLICATION-ROT" %}
+                              | SPM_PART_FLAG_APP_ROT
+    {% elif manifest.manifest.type == "PSA-ROT" %}
+                              | SPM_PART_FLAG_PSA_ROT | SPM_PART_FLAG_APP_ROT
+    {% else %}
+#error "Unsupported type '{{manifest.manifest.type}}' for partition '{{manifest.manifest.name}}'!"
+    {% endif %}
+                              ,
+        .partition_priority   = TFM_PRIORITY({{manifest.manifest.priority}}),
+        .partition_init       = {{manifest.manifest.entry_point}},
+    {{'},'}}
+    {% if manifest.attr.conditional %}
+#endif /* {{manifest.attr.conditional}} */
+    {% endif %}
+
+{% endfor %}
+};
+
+/**************************************************************************/
+/** The platform data of the partition list */
+/**************************************************************************/
+const struct tfm_spm_partition_platform_data_t *platform_data_list[] =
+{
+    NULL,
+
+#ifndef TFM_PSA_API
+    NULL,
+#endif
+
+{% for manifest in manifests %}
+    {% if manifest.attr.conditional %}
+#ifdef {{manifest.attr.conditional}}
+    {% endif %}
+    {% if manifest.manifest.mmio_regions %}
+    /* FIXME: Only adding the first mmio region */
+        {% for region in manifest.manifest.mmio_regions %}
+            {% if loop.first %}
+                {% if region.conditional %}
+#ifdef {{region.conditional}}
+                {% endif %}
+    {{region.name}},
+                {% if region.conditional %}
+#else /* {{region.conditional}} */
+    NULL,
+#endif /* {{region.conditional}} */
+                {% endif %}
+            {% else %} {# print nothing #} {% endif %}
+        {% endfor %}
+    {% else %}
+    NULL,
+    {% endif %}
+    {% if manifest.attr.conditional %}
+#endif /* {{manifest.attr.conditional}} */
+    {% endif %}
+
+{% endfor %}
+};
+
+/**************************************************************************/
+/** The memory data of the partition list */
+/**************************************************************************/
+#ifdef TFM_PSA_API
+const struct tfm_spm_partition_memory_data_t memory_data_list[] =
+{
+    {
+        .stack_bottom         = PART_REGION_ADDR(ARM_LIB_STACK, $$ZI$$Base),
+        .stack_top            = PART_REGION_ADDR(ARM_LIB_STACK, $$ZI$$Limit),
+        .rw_start             = PART_REGION_ADDR(ARM_LIB_STACK, $$ZI$$Base),
+    },
+{% for manifest in manifests %}
+    {% if manifest.attr.conditional %}
+#ifdef {{manifest.attr.conditional}}
+    {% endif %}
+    {{'{'}}
+        .code_start           = PART_REGION_ADDR({{manifest.manifest.name}}, $$Base),
+        .code_limit           = PART_REGION_ADDR({{manifest.manifest.name}}, $$Limit),
+        .ro_start             = PART_REGION_ADDR({{manifest.manifest.name}}, $$RO$$Base),
+        .ro_limit             = PART_REGION_ADDR({{manifest.manifest.name}}, $$RO$$Limit),
+        .rw_start             = PART_REGION_ADDR({{manifest.manifest.name}}, _DATA$$RW$$Base),
+        .rw_limit             = PART_REGION_ADDR({{manifest.manifest.name}}, _DATA$$RW$$Limit),
+        .zi_start             = PART_REGION_ADDR({{manifest.manifest.name}}, _DATA$$ZI$$Base),
+        .zi_limit             = PART_REGION_ADDR({{manifest.manifest.name}}, _DATA$$ZI$$Limit),
+        .stack_bottom         = PART_REGION_ADDR({{manifest.manifest.name}}, _STACK$$ZI$$Base),
+        .stack_top            = PART_REGION_ADDR({{manifest.manifest.name}}, _STACK$$ZI$$Limit),
+    {{'},'}}
+    {% if manifest.attr.conditional %}
+#endif /* {{manifest.attr.conditional}} */
+    {% endif %}
+
+{% endfor %}
+};
+#endif /* defined(TFM_PSA_API) */
+
+/**************************************************************************/
 /** The partition list for the DB */
 /**************************************************************************/
-struct spm_partition_desc_t partition_list [] =
+static struct spm_partition_desc_t partition_list [] =
 {
-    {{'{{0}}'}}, /* placeholder for Non-secure internal partition */
+    {{'{{}}'}}, /* placeholder for Non-secure internal partition */
 #ifndef TFM_PSA_API
-    {{'{{0}}'}}, /* placeholder for TF-M Core internal partition */
+    {{'{{}}'}}, /* placeholder for TF-M Core internal partition */
 #endif /* !ifndefined(TFM_PSA_API) */
 
 {% for manifest in manifests %}
@@ -112,111 +270,11 @@
 #ifdef {{manifest.attr.conditional}}
     {% endif %}
     {{'{'}}
-    /* Static data */
-        {{'{'}}
-            .partition_id         = {{manifest.manifest.name}}_ID,
-        {% if manifest.attr.tfm_partition_ipc %}
-            .partition_flags      = SPM_PART_FLAG_IPC
-        {% else %}
-            .partition_flags      = 0
-        {% endif %}
-        {% if manifest.manifest.type == "APPLICATION-ROT" %}
-                                  | SPM_PART_FLAG_APP_ROT
-        {% elif manifest.manifest.type == "PSA-ROT" %}
-                                  | SPM_PART_FLAG_PSA_ROT | SPM_PART_FLAG_APP_ROT
-        {% else %}
-#error "Unsupported type '{{manifest.manifest.type}}' for partition '{{manifest.manifest.name}}'!"
-        {% endif %}
-                                  ,
-            .partition_priority   = TFM_PRIORITY({{manifest.manifest.priority}}),
-            .partition_init       = {{manifest.manifest.entry_point}},
-        {{'},'}}
-
     /* Runtime data */
-        {{'{'}}
-#ifndef TFM_PSA_API
-            .partition_state      = SPM_PARTITION_STATE_UNINIT,
-            .caller_partition_idx = SPM_INVALID_PARTITION_IDX,
-            .caller_client_id     = TFM_INVALID_CLIENT_ID,
-            .share                = TFM_BUFFER_SHARE_DISABLE,
-            .stack_ptr            = 0,
-            .lr                   = 0,
-            .iovec_api            = TFM_SFN_API_IOVEC,
-            .iovec_args           =
-                {
-                    .in_vec       = {{'{{0}}'}},
-                    .in_len       = 0,
-                    .out_vec      = {{'{{0}}'}},
-                    .out_len      = 0,
-                },
-            .orig_outvec          = NULL,
-            .ctx_stack_ptr        = ctx_stack_{{manifest.manifest.name}},
-#else /* !defined(TFM_PSA_API) */
-            .signal_evnt          =
-                {
-                    .magic        = 0,
-                    .owner        = NULL,
-                },
-            .signals              = 0,
-            .service_list         =
-                {
-                    .prev         = NULL,
-                    .next         = NULL,
-                },
-#endif /* !defined(TFM_PSA_API) */
-            .signal_mask          = 0,
-        {{'},'}}
-
-        {% if manifest.manifest.mmio_regions %}
-    /* platform data */
-        /* FIXME: Only adding the first mmio region */
-            {% for region in manifest.manifest.mmio_regions %}
-                {% if loop.first %}
-                    {% if region.conditional %}
-#ifdef {{region.conditional}}
-                    {% endif %}
-        .platform_data            = {{region.name}},
-                    {% if region.conditional %}
-#else /* {{region.conditional}} */
+        .runtime_data             = {},
+        .static_data              = NULL,
         .platform_data            = NULL,
-#endif /* {{region.conditional}} */
-                    {% endif %}
-                {% else %} {# print nothing #} {% endif %}
-            {% endfor %}
-        {% else %}
-        .platform_data            = NULL,
-        {% endif %}
 
-#ifdef TFM_PSA_API
-    /* memory_data */
-        .memory_data = {{'{'}}
-            .code_start           = PART_REGION_ADDR({{manifest.manifest.name}}, $$Base),
-            .code_limit           = PART_REGION_ADDR({{manifest.manifest.name}}, $$Limit),
-            .ro_start             = PART_REGION_ADDR({{manifest.manifest.name}}, $$RO$$Base),
-            .ro_limit             = PART_REGION_ADDR({{manifest.manifest.name}}, $$RO$$Limit),
-            .rw_start             = PART_REGION_ADDR({{manifest.manifest.name}}, _DATA$$RW$$Base),
-            .rw_limit             = PART_REGION_ADDR({{manifest.manifest.name}}, _DATA$$RW$$Limit),
-            .zi_start             = PART_REGION_ADDR({{manifest.manifest.name}}, _DATA$$ZI$$Base),
-            .zi_limit             = PART_REGION_ADDR({{manifest.manifest.name}}, _DATA$$ZI$$Limit),
-            .stack_bottom         = PART_REGION_ADDR({{manifest.manifest.name}}, _STACK$$ZI$$Base),
-            .stack_top            = PART_REGION_ADDR({{manifest.manifest.name}}, _STACK$$ZI$$Limit),
-        {{'},'}}
-
-    /* sp_thrd */
-        .sp_thrd = {
-                .pfn              = NULL,
-                .param            = NULL,
-                .sp_btm           = 0,
-                .sp_top           = 0,
-                .prior            = THRD_PRIOR_LOWEST,
-                .status           = THRD_STAT_INVALID,
-                .state_ctx        =
-                {
-                    .ctxb         = {0}, /* Fields are not detailed as they are architecture dependent*/
-                },
-                .next             = NULL,
-        },
-#endif /* defined(TFM_PSA_API) */
     {{'},'}}
     {% if manifest.attr.conditional %}
 #endif /* {{manifest.attr.conditional}} */