Core: Separate SPM_DB for library model and IPC model

- Separate the template for SPM_DB into two files, one for the library
  model and another for the IPC model.
- Update the generated files and related sources.

Change-Id: If221fc5956c7caeaa20ec21810f75773654d0d3a
Signed-off-by: Shawn Shan <shawn.shan@arm.com>
diff --git a/secure_fw/spm/tfm_spm_db_func.inc.template b/secure_fw/spm/tfm_spm_db_func.inc.template
new file mode 100644
index 0000000..8f424b8
--- /dev/null
+++ b/secure_fw/spm/tfm_spm_db_func.inc.template
@@ -0,0 +1,259 @@
+/*
+ * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+{{utilities.donotedit_warning}}
+
+#ifndef __TFM_SPM_DB_FUNC_INC__
+#define __TFM_SPM_DB_FUNC_INC__
+
+#include "spm_api.h"
+#include "psa_manifest/sid.h"
+
+{# Produce a build error if heap_size is presented in the manifest, because of the dynamic memory allocation is not supported now. #}
+{% for manifest in manifests %}
+    {% if manifest.manifest.heap_size %}
+#error "Please do not add 'heap_size' for partition '{{manifest.manifest.name}}', the dynamic memory allocation is not supported now!"
+    {% endif %}
+{% endfor %}
+/**************************************************************************/
+/** IRQ count per partition */
+/**************************************************************************/
+{% for manifest in manifests %}
+    {% if manifest.attr.conditional %}
+#ifdef {{manifest.attr.conditional}}
+    {% endif %}
+    {% if manifest.manifest.irqs %}
+#define TFM_PARTITION_{{manifest.manifest.name}}_IRQ_COUNT {{manifest.manifest.irqs | length() }}
+    {% else %}
+#define TFM_PARTITION_{{manifest.manifest.name}}_IRQ_COUNT 0
+    {% endif %}
+    {% if manifest.attr.conditional %}
+#endif /* {{manifest.attr.conditional}} */
+    {% endif %}
+
+{% endfor %}
+/**************************************************************************/
+/** Declarations of partition init functions */
+/**************************************************************************/
+{% for manifest in manifests %}
+    {% if manifest.attr.conditional %}
+#ifdef {{manifest.attr.conditional}}
+    {% endif %}
+extern void {{manifest.manifest.entry_point}}(void);
+    {% if manifest.attr.conditional %}
+#endif /* {{manifest.attr.conditional}} */
+    {% endif %}
+
+{% endfor %}
+/**************************************************************************/
+/** Context stacks for IRQ handling */
+/**************************************************************************/
+/* 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
+ */
+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}}
+    {% endif %}
+static uint32_t ctx_stack_{{manifest.manifest.name}}[
+        (sizeof(struct interrupted_ctx_stack_frame_t) +
+            (TFM_PARTITION_{{manifest.manifest.name}}_IRQ_COUNT) * (
+                sizeof(struct interrupted_ctx_stack_frame_t) +
+                sizeof(struct handler_ctx_stack_frame_t)
+        )) / sizeof(uint32_t)];
+    {% if manifest.attr.conditional %}
+#endif /* {{manifest.attr.conditional}} */
+    {% 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 %}
+};
+
+/**************************************************************************/
+/** Dependencies array for Secure Partition */
+/**************************************************************************/
+{% for manifest in manifests %}
+    {% if manifest.manifest.dependencies %}
+        {% if manifest.attr.conditional %}
+#ifdef {{manifest.attr.conditional}}
+        {% endif %}
+static int32_t dependencies_{{manifest.manifest.name}}[] =
+{
+        {% for dependence in manifest.manifest.dependencies %}
+            {% for service in manifest.manifest.services %}
+                {% if dependence == service.name %}
+#error "Please DO NOT include SP's own RoT Service '{{dependence}}', which will cause a deadlock!"
+                {% endif %}
+            {% endfor %}
+    {{dependence}}_SID,
+        {% endfor %}
+};
+        {% if manifest.attr.conditional %}
+#endif /* {{manifest.attr.conditional}} */
+        {% endif %}
+
+    {% endif %}
+{% endfor %}
+/**************************************************************************/
+/** The static data of the partition list */
+/**************************************************************************/
+const struct spm_partition_static_data_t static_data_list[] =
+{
+    {
+        .partition_id         = TFM_SP_NON_SECURE_ID,
+        .partition_flags      = 0,
+    },
+
+    {
+        .partition_id         = TFM_SP_CORE_ID,
+        .partition_flags      = SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_PSA_ROT,
+    },
+
+{% for manifest in manifests %}
+    {% if manifest.attr.conditional %}
+#ifdef {{manifest.attr.conditional}}
+    {% endif %}
+    {{'{'}}
+        .partition_id         = {{manifest.manifest.name}},
+    {% 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}},
+        .dependencies_num     = {{manifest.manifest.dependencies | length()}},
+    {% if manifest.manifest.dependencies %}
+        .p_dependencies       = dependencies_{{manifest.manifest.name}},
+    {% else %}
+        .p_dependencies       = NULL,
+    {% endif %}
+    {{'},'}}
+    {% if manifest.attr.conditional %}
+#endif /* {{manifest.attr.conditional}} */
+    {% endif %}
+
+{% endfor %}
+};
+
+/**************************************************************************/
+/** The platform data of the partition list */
+/**************************************************************************/
+{% for manifest in manifests %}
+    {% if manifest.manifest.mmio_regions %}
+        {% if manifest.attr.conditional %}
+#ifdef {{manifest.attr.conditional}}
+        {% endif %}
+const struct tfm_spm_partition_platform_data_t *
+    platform_data_list_{{manifest.manifest.name}}[] =
+{
+        {% for region in manifest.manifest.mmio_regions %}
+            {% if region.conditional %}
+#ifdef {{region.conditional}}
+            {% endif %}
+    {{region.name}},
+            {% if region.conditional %}
+#endif /* {{region.conditional}} */
+            {% endif %}
+        {% endfor %}
+    NULL
+};
+        {% if manifest.attr.conditional %}
+#endif /* {{manifest.attr.conditional}} */
+        {% endif %}
+
+    {% endif %}
+{% endfor %}
+const struct tfm_spm_partition_platform_data_t **platform_data_list_list[] =
+{
+    NULL,
+    NULL,
+
+{% for manifest in manifests %}
+    {% if manifest.attr.conditional %}
+#ifdef {{manifest.attr.conditional}}
+    {% endif %}
+    {% if manifest.manifest.mmio_regions %}
+    platform_data_list_{{manifest.manifest.name}},
+    {% else %}{# if manifest.manifest.mmio_regions #}
+    NULL,
+    {% endif %}{# if manifest.manifest.mmio_regions #}
+    {% if manifest.attr.conditional %}
+#endif /* {{manifest.attr.conditional}} */
+    {% endif %}
+
+{% endfor %}
+};
+
+/**************************************************************************/
+/** The partition list for the DB */
+/**************************************************************************/
+static struct spm_partition_desc_t partition_list [] =
+{
+    {{'{{0}}'}}, /* placeholder for Non-secure internal partition */
+    {{'{{0}}'}},   /* placeholder for TF-M Core internal partition */
+
+{% for manifest in manifests %}
+    /* -----------------------------------------------------------------------*/
+    /* - Partition DB record for {{manifest.manifest.name}} */
+    /* -----------------------------------------------------------------------*/
+    {% if manifest.attr.conditional %}
+#ifdef {{manifest.attr.conditional}}
+    {% endif %}
+    {{'{'}}
+    /* Runtime data */
+        .runtime_data             = {0},
+        .static_data              = NULL,
+        .platform_data_list       = NULL,
+    {{'},'}}
+    {% if manifest.attr.conditional %}
+#endif /* {{manifest.attr.conditional}} */
+    {% endif %}
+
+{% endfor %}
+};
+
+struct spm_partition_db_t g_spm_partition_db = {
+    .is_init = 0,
+    .partition_count = sizeof(partition_list) / sizeof(partition_list[0]),
+    .running_partition_idx = 0,
+    .partitions = partition_list,
+};
+
+#endif /* __TFM_SPM_DB_FUNC_INC__ */