SPM: Add header files for partition static load

- Create two new header files that define the partition and service
  static data type.
- Create a header file for partition static loading definitions.

Change-Id: I73dafef7b587d3cad6fa35ff4788365a9936c062
Signed-off-by: Mingyang Sun <mingyang.sun@arm.com>
diff --git a/secure_fw/include/compile_check_defs.h b/secure_fw/include/compile_check_defs.h
new file mode 100644
index 0000000..13a8689
--- /dev/null
+++ b/secure_fw/include/compile_check_defs.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __COMPILE_CHECK_DEFS_H__
+#define __COMPILE_CHECK_DEFS_H__
+
+#include "load/partition_static_load.h"
+#include "tfm_thread.h"
+
+#if TO_THREAD_PRIORITY(PARTITION_PRI_HIGHEST) != THRD_PRIOR_HIGHEST ||  \
+    TO_THREAD_PRIORITY(PARTITION_PRI_HIGH) != THRD_PRIOR_HIGH ||        \
+    TO_THREAD_PRIORITY(PARTITION_PRI_NORMAL) != THRD_PRIOR_MEDIUM ||    \
+    TO_THREAD_PRIORITY(PARTITION_PRI_LOW) != THRD_PRIOR_LOW ||          \
+    TO_THREAD_PRIORITY(PARTITION_PRI_LOWEST) != THRD_PRIOR_LOWEST
+#error "Partition priority converting to thread priority error!"
+#endif
+
+#endif /* __COMPILE_CHECK_DEFS_H__ */
diff --git a/secure_fw/include/load/partition_defs.h b/secure_fw/include/load/partition_defs.h
new file mode 100644
index 0000000..573043e
--- /dev/null
+++ b/secure_fw/include/load/partition_defs.h
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __PARTITION_DEFS_H__
+#define __PARTITION_DEFS_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+/* Encode a magic number into version for validating partition info */
+#define PARTITION_INFO_VERSION_MASK             (0x0000FFFF)
+#define PARTITION_INFO_MAGIC_MASK               (0xFFFF0000)
+#define PARTITION_INFO_MAGIC                    (0x5F5F0000)
+
+/* Privileged definitions for partition thread mode */
+#define TFM_PARTITION_UNPRIVILEGED_MODE         (0U)
+#define TFM_PARTITION_PRIVILEGED_MODE           (1U)
+
+/*
+ * Partition static data - flags
+ * bit 7-0: priority
+ * bit 8: 1 - PSA_ROT, 0 - APP_ROT
+ * bit 9: 1 - IPC model, 0 - SFN model
+ */
+#define PARTITION_PRI_HIGHEST                   (0x0)
+#define PARTITION_PRI_HIGH                      (0xF)
+#define PARTITION_PRI_NORMAL                    (0x1F)
+#define PARTITION_PRI_LOW                       (0x7F)
+#define PARTITION_PRI_LOWEST                    (0xFF)
+#define PARTITION_PRI_MASK                      (0xFF)
+
+#define SPM_PART_FLAG_PSA_ROT                   (1U << 8)
+#define SPM_PART_FLAG_IPC                       (1U << 9)
+
+#if TFM_LVL == 3
+/**
+ * Holds isolation memory regions used by a partition. Could be extended if
+ * more isolation regions are required.
+ */
+struct private_data_t {
+    uintptr_t start;
+    uintptr_t limit;
+};
+#endif
+
+/* Common partition structure type */
+struct partition_static_info_t {
+    uint32_t        psa_ff_ver;         /* Encode the version with magic    */
+    uint32_t        pid;                /* Partition ID                     */
+    uint32_t        flags;              /* ARoT/PRoT, SFN/IPC, priority     */
+    uintptr_t       entry;              /* Entry point                      */
+    size_t          stack_size;         /* Stack size                       */
+    size_t          heap_size;          /* Heap size                        */
+    uint32_t        ndeps;              /* Dependency number                */
+    uint32_t        nservices;          /* Service number                   */
+    uintptr_t       plat_cookie;        /* A cookie for platform binding    */
+#if TFM_LVL == 3
+    struct private_data_t mems;         /* Partition isolation memory data  */
+#endif
+    uintptr_t       vars[];             /* Struct extendable indicator      */
+} __attribute__((aligned(4)));
+
+#endif /* __PARTITION_DEFS_H__ */
diff --git a/secure_fw/include/load/partition_static_load.h b/secure_fw/include/load/partition_static_load.h
new file mode 100644
index 0000000..065c6db
--- /dev/null
+++ b/secure_fw/include/load/partition_static_load.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __PARTITION_STATIC_LOAD_H__
+#define __PARTITION_STATIC_LOAD_H__
+
+#include "partition_defs.h"
+#include "service_defs.h"
+
+#define PARTITION_GET_PRIOR(flag)           ((flag) & PARTITION_PRI_MASK)
+#define TO_THREAD_PRIORITY(x)               (x)
+
+#define STRID_TO_STRING_PTR(strid)          (const char *)(strid)
+#define STRING_PTR_TO_STRID(str)            (uintptr_t)(str)
+
+#define ENTRY_TO_POSITION(x)                (uintptr_t)(x)
+#define POSITION_TO_ENTRY(x, t)             (t)(x)
+
+#define PTR_TO_POSITION(x)                  (uintptr_t)(x)
+#define POSITION_TO_PTR(x, t)               (t)(x)
+
+/* Length of extendable variables in partition static type */
+#define STATIC_INFO_EXT_LENGTH              0
+/*
+ * Argument "ps_ptr" must have be a "struct partition_static_info_t *" type and
+ * must be validated before using.
+ */
+#define STATIC_INFSZ_BYTES(ps_ptr)                                      \
+    (sizeof(*(ps_ptr)) + STATIC_INFO_EXT_LENGTH * sizeof(uintptr_t) +   \
+     (ps_ptr)->ndeps * sizeof(uint32_t) +                               \
+     (ps_ptr)->nservices * sizeof(struct service_static_info_t))
+
+#endif /* __PARTITION_STATIC_LOAD_H__ */
diff --git a/secure_fw/include/load/service_defs.h b/secure_fw/include/load/service_defs.h
new file mode 100644
index 0000000..7dda24b
--- /dev/null
+++ b/secure_fw/include/load/service_defs.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __SERVICE_DEFS_H__
+#define __SERVICE_DEFS_H__
+
+#include <stdint.h>
+#include "psa/service.h"
+
+/*
+ * Service static data - flags
+ * bit 7-0: version policy
+ * bit 8: 1 - NS accessible, 0 - NS not accessible
+ * bit 9: 1 - stateless, 0 - connection-based
+ */
+#define SERVICE_FLAG_NS_ACCESSIBLE              (1U << 8)
+#define SERVICE_FLAG_STATELESS                  (1U << 9)
+
+#define TFM_VERSION_POLICY_RELAXED              (0x0)
+#define TFM_VERSION_POLICY_STRICT               (0x1)
+#define SERVICE_VERSION_POLICY_MASK             (0xFF)
+
+#define SERVICE_IS_NS_ACCESSIBLE(flag)          \
+    ((flag) & SERVICE_FLAG_NS_ACCESSIBLE)
+#define SERVICE_IS_STATELESS(flag)              \
+    ((flag) & SERVICE_FLAG_STATELESS)
+#define SERVICE_GET_VERSION_POLICY(flag)        \
+    ((flag) & SERVICE_VERSION_POLICY_MASK)
+
+/* Common service structure type */
+struct service_static_info_t {
+    uintptr_t       name_strid;         /* String ID for name               */
+    psa_signal_t    signal;             /* Service signal                   */
+    uint32_t        sid;                /* Service ID                       */
+    uint32_t        flags;              /* Flags                            */
+    uint32_t        version;            /* Service version                  */
+} __attribute__((aligned(4)));
+
+#endif /* __SERVICE_DEFS_H__ */
diff --git a/secure_fw/spm/cmsis_psa/main.c b/secure_fw/spm/cmsis_psa/main.c
index b9dec4b..f8eb681 100644
--- a/secure_fw/spm/cmsis_psa/main.c
+++ b/secure_fw/spm/cmsis_psa/main.c
@@ -7,6 +7,7 @@
 
 #include "fih.h"
 #include "ffm/tfm_boot_data.h"
+#include "compile_check_defs.h"
 #include "region.h"
 #include "spm_ipc.h"
 #include "tfm_hal_platform.h"
diff --git a/secure_fw/spm/cmsis_psa/spm_ipc.c b/secure_fw/spm/cmsis_psa/spm_ipc.c
index f8c5b5e..15b22ef 100644
--- a/secure_fw/spm/cmsis_psa/spm_ipc.c
+++ b/secure_fw/spm/cmsis_psa/spm_ipc.c
@@ -33,6 +33,8 @@
 #include "spm_partition_defs.h"
 #include "psa_manifest/pid.h"
 #include "tfm/tfm_spm_services.h"
+#include "load/partition_defs.h"
+#include "load/service_defs.h"
 
 #include "secure_fw/partitions/tfm_service_list.inc"
 #include "tfm_spm_db_ipc.inc"
diff --git a/secure_fw/spm/cmsis_psa/spm_ipc.h b/secure_fw/spm/cmsis_psa/spm_ipc.h
index 892bad1..1ed68ff 100644
--- a/secure_fw/spm/cmsis_psa/spm_ipc.h
+++ b/secure_fw/spm/cmsis_psa/spm_ipc.h
@@ -17,16 +17,10 @@
 #include "tfm_thread.h"
 #include "psa/service.h"
 
-#define TFM_VERSION_POLICY_RELAXED      0
-#define TFM_VERSION_POLICY_STRICT       1
-
 #define TFM_HANDLE_STATUS_IDLE          0
 #define TFM_HANDLE_STATUS_ACTIVE        1
 #define TFM_HANDLE_STATUS_CONNECT_ERROR 2
 
-#define PART_REGION_ADDR(partition, region) \
-    (uint32_t)&REGION_NAME(Image$$, partition, region)
-
 #define TFM_CONN_HANDLE_MAX_NUM         16
 
 /*
@@ -60,19 +54,6 @@
 
 #define SPM_INVALID_PARTITION_IDX     (~0U)
 
-/* Privileged definitions for partition thread mode */
-#define TFM_PARTITION_UNPRIVILEGED_MODE 0
-#define TFM_PARTITION_PRIVILEGED_MODE   1
-
-#define SPM_PART_FLAG_APP_ROT           0x01
-#define SPM_PART_FLAG_PSA_ROT           0x02
-#define SPM_PART_FLAG_IPC               0x04
-
-#define TFM_PRIORITY_HIGH               THRD_PRIOR_HIGHEST
-#define TFM_PRIORITY_NORMAL             THRD_PRIOR_MEDIUM
-#define TFM_PRIORITY_LOW                THRD_PRIOR_LOWEST
-#define TFM_PRIORITY(LEVEL)             TFM_PRIORITY_##LEVEL
-
 #define TFM_MSG_MAGIC                   0x15154343
 
 /* Message struct to collect parameter from client */
@@ -103,9 +84,7 @@
 };
 
 /**
- * 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.
+ * Partition storage common type.
  */
 struct partition_static_t {
     uint32_t psa_ff_ver;                /* PSA-FF version                   */
diff --git a/secure_fw/spm/cmsis_psa/tfm_rpc.c b/secure_fw/spm/cmsis_psa/tfm_rpc.c
index ac2de38..5872649 100644
--- a/secure_fw/spm/cmsis_psa/tfm_rpc.c
+++ b/secure_fw/spm/cmsis_psa/tfm_rpc.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2019-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -9,6 +9,7 @@
 #include "ffm/spm_psa_client_call.h"
 #include "tfm_rpc.h"
 #include "utilities.h"
+#include "load/partition_defs.h"
 
 static void default_handle_req(void)
 {
diff --git a/secure_fw/spm/cmsis_psa/tfm_spm_db_ipc.inc.template b/secure_fw/spm/cmsis_psa/tfm_spm_db_ipc.inc.template
index 45a1f71..503288f 100644
--- a/secure_fw/spm/cmsis_psa/tfm_spm_db_ipc.inc.template
+++ b/secure_fw/spm/cmsis_psa/tfm_spm_db_ipc.inc.template
@@ -137,9 +137,9 @@
 #if TFM_MULTI_CORE_TOPOLOGY
         .flags                = SPM_PART_FLAG_PSA_ROT | SPM_PART_FLAG_IPC,
 #else
-        .flags                = SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_IPC,
+        .flags                = SPM_PART_FLAG_IPC,
 #endif
-        .priority             = TFM_PRIORITY_LOW,
+        .priority             = PARTITION_PRI_LOWEST,
         .entry                = tfm_nspm_thread_entry,
         .stack_base_addr      = PART_REGION_ADDR(ARM_LIB_STACK, $$ZI$$Base),
         .stack_size           = S_PSP_STACK_SIZE,
@@ -170,14 +170,13 @@
         .flags                 = 0
     {% endif %}
     {% if partition.manifest.type == "APPLICATION-ROT" %}
-                               | SPM_PART_FLAG_APP_ROT
+                               | 0,
     {% elif partition.manifest.type == "PSA-ROT" %}
-                               | SPM_PART_FLAG_PSA_ROT | SPM_PART_FLAG_APP_ROT
+                               | SPM_PART_FLAG_PSA_ROT,
     {% else %}
 #error "Unsupported type '{{partition.manifest.type}}' for partition '{{partition.manifest.name}}'!"
     {% endif %}
-                              ,
-        .priority             = TFM_PRIORITY({{partition.manifest.priority}}),
+        .priority             = PARTITION_PRI_{{partition.manifest.priority}},
         .entry                = {{partition.manifest.entry_point}},
         .stack_base_addr      = (uint32_t){{partition.manifest.name.lower()}}_stack,
         .stack_size           = (uint32_t){{partition.manifest.stack_size}},
diff --git a/secure_fw/spm/cmsis_psa/tfm_thread.h b/secure_fw/spm/cmsis_psa/tfm_thread.h
index 8e96e3f..159344b 100644
--- a/secure_fw/spm/cmsis_psa/tfm_thread.h
+++ b/secure_fw/spm/cmsis_psa/tfm_thread.h
@@ -27,7 +27,9 @@
 /* Lower value has higher priority */
 #define THRD_PRIOR_MASK           0xFF
 #define THRD_PRIOR_HIGHEST        0x0
-#define THRD_PRIOR_MEDIUM         0x7F
+#define THRD_PRIOR_HIGH           0xF
+#define THRD_PRIOR_MEDIUM         0x1F
+#define THRD_PRIOR_LOW            0x7F
 #define THRD_PRIOR_LOWEST         0xFF
 
 /* Error code */