Core: Merge IPC model SP data into library model

Merges the necessary secure partition data for IPC model into library
model. Prepare a common partition data structure for optimization.

Change-Id: I88939faa337588f14f351c284e5a9d625af37fa5
Signed-off-by: Edison Ai <edison.ai@arm.com>
diff --git a/secure_fw/services/tfm_spm_db.inc b/secure_fw/services/tfm_spm_db.inc
index 35b0774..2ab3cbb4 100644
--- a/secure_fw/services/tfm_spm_db.inc
+++ b/secure_fw/services/tfm_spm_db.inc
@@ -234,6 +234,7 @@
 REGION_DECLARE(Image$$, TFM_IRQ_TEST_1, _STACK$$ZI$$Limit);
 #endif /* TFM_PARTITION_TEST_CORE */
 
+#ifndef TFM_PSA_API
 /**************************************************************************/
 /** Context stacks for IRQ handling */
 /**************************************************************************/
@@ -339,6 +340,8 @@
         )) / sizeof(uint32_t)];
 #endif /* TFM_PARTITION_TEST_CORE */
 
+#endif /* !defined(TFM_PSA_API) */
+
 /**************************************************************************/
 /** The partition list for the DB */
 /**************************************************************************/
@@ -363,6 +366,7 @@
 
     /* 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,
@@ -384,6 +388,19 @@
                 },
             .orig_outvec          = NULL,
             .ctx_stack_ptr        = ctx_stack_TFM_SP_STORAGE,
+#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,
         },
 
@@ -440,6 +457,7 @@
 
     /* 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,
@@ -461,6 +479,19 @@
                 },
             .orig_outvec          = NULL,
             .ctx_stack_ptr        = ctx_stack_TFM_SP_AUDIT_LOG,
+#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,
         },
 
@@ -523,6 +554,7 @@
 
     /* 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,
@@ -544,6 +576,19 @@
                 },
             .orig_outvec          = NULL,
             .ctx_stack_ptr        = ctx_stack_TFM_SP_CRYPTO,
+#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,
         },
 
@@ -600,6 +645,7 @@
 
     /* 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,
@@ -621,6 +667,19 @@
                 },
             .orig_outvec          = NULL,
             .ctx_stack_ptr        = ctx_stack_TFM_SP_PLATFORM,
+#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,
         },
 
@@ -677,6 +736,7 @@
 
     /* 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,
@@ -698,6 +758,19 @@
                 },
             .orig_outvec          = NULL,
             .ctx_stack_ptr        = ctx_stack_TFM_SP_INITIAL_ATTESTATION,
+#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,
         },
 
@@ -754,6 +827,7 @@
 
     /* 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,
@@ -775,6 +849,19 @@
                 },
             .orig_outvec          = NULL,
             .ctx_stack_ptr        = ctx_stack_TFM_SP_CORE_TEST,
+#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,
         },
 
@@ -834,6 +921,7 @@
 
     /* 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,
@@ -855,6 +943,19 @@
                 },
             .orig_outvec          = NULL,
             .ctx_stack_ptr        = ctx_stack_TFM_SP_CORE_TEST_2,
+#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,
         },
 
@@ -912,6 +1013,7 @@
 
     /* 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,
@@ -933,6 +1035,19 @@
                 },
             .orig_outvec          = NULL,
             .ctx_stack_ptr        = ctx_stack_TFM_SP_SECURE_TEST_PARTITION,
+#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,
         },
 
@@ -992,6 +1107,7 @@
 
     /* 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,
@@ -1013,6 +1129,19 @@
                 },
             .orig_outvec          = NULL,
             .ctx_stack_ptr        = ctx_stack_TFM_SP_IPC_SERVICE_TEST,
+#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,
         },
 
@@ -1070,6 +1199,7 @@
 
     /* 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,
@@ -1091,6 +1221,19 @@
                 },
             .orig_outvec          = NULL,
             .ctx_stack_ptr        = ctx_stack_TFM_SP_IPC_CLIENT_TEST,
+#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,
         },
 
@@ -1148,6 +1291,7 @@
 
     /* 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,
@@ -1169,6 +1313,19 @@
                 },
             .orig_outvec          = NULL,
             .ctx_stack_ptr        = ctx_stack_TFM_IRQ_TEST_1,
+#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,
         },
 
@@ -1216,7 +1373,9 @@
 struct spm_partition_db_t g_spm_partition_db = {
     .is_init = 0,
     .partition_count = sizeof(partition_list) / sizeof(partition_list[0]),
+#ifndef TFM_PSA_API
     .running_partition_idx = 0,
+#endif
     .partitions = partition_list,
 };
 
diff --git a/secure_fw/services/tfm_spm_db.inc.template b/secure_fw/services/tfm_spm_db.inc.template
index 2b52b4d..27afe40 100644
--- a/secure_fw/services/tfm_spm_db.inc.template
+++ b/secure_fw/services/tfm_spm_db.inc.template
@@ -64,6 +64,7 @@
     {% endif %}
 
 {% endfor %}
+#ifndef TFM_PSA_API
 /**************************************************************************/
 /** Context stacks for IRQ handling */
 /**************************************************************************/
@@ -91,6 +92,8 @@
     {% endif %}
 
 {% endfor %}
+#endif /* !defined(TFM_PSA_API) */
+
 /**************************************************************************/
 /** The partition list for the DB */
 /**************************************************************************/
@@ -129,6 +132,7 @@
 
     /* 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,
@@ -150,6 +154,19 @@
                 },
             .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,
         {{'},'}}
 
@@ -216,7 +233,9 @@
 struct spm_partition_db_t g_spm_partition_db = {
     .is_init = 0,
     .partition_count = sizeof(partition_list) / sizeof(partition_list[0]),
+#ifndef TFM_PSA_API
     .running_partition_idx = 0,
+#endif
     .partitions = partition_list,
 };
 
diff --git a/secure_fw/spm/spm_api.c b/secure_fw/spm/spm_api.c
index ffca849..3a3b2f8 100644
--- a/secure_fw/spm/spm_api.c
+++ b/secure_fw/spm/spm_api.c
@@ -23,6 +23,7 @@
 #define NON_SECURE_INTERNAL_PARTITION_DB_IDX 0
 #define TFM_CORE_INTERNAL_PARTITION_DB_IDX   1
 
+#ifndef TFM_PSA_API
 typedef enum {
     TFM_INIT_FAILURE,
 } sp_error_type_t;
@@ -44,6 +45,7 @@
     uint32_t partition_state;
     uint32_t caller_partition_idx;
 };
+#endif /* !defined(TFM_PSA_API) */
 
 /* Define SPM DB structure */
 #include "secure_fw/services/tfm_spm_db.inc"
@@ -96,11 +98,13 @@
 
 enum spm_err_t tfm_spm_db_init(void)
 {
-   struct spm_partition_desc_t *part_ptr;
+    struct spm_partition_desc_t *part_ptr;
+#ifndef TFM_PSA_API
     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};
+#endif /* !defined(TFM_PSA_API) */
 
     /* This function initialises partition db */
 
@@ -137,8 +141,11 @@
     part_ptr->memory_data.rw_start     = psp_stack_bottom;
 #endif
 
+#ifndef TFM_PSA_API
     part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
     part_ptr->runtime_data.ctx_stack_ptr = ns_interrupt_ctx_stack;
+#endif /* !defined(TFM_PSA_API) */
+
     tfm_nspm_configure_clients();
 
     /* For the TF-M core environment itself */
@@ -147,8 +154,10 @@
     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;
+#ifndef TFM_PSA_API
     part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
     part_ptr->runtime_data.ctx_stack_ptr = tfm_core_interrupt_ctx_stack;
+#endif /* !defined(TFM_PSA_API) */
 
     g_spm_partition_db.is_init = 1;
 
diff --git a/secure_fw/spm/spm_api.h b/secure_fw/spm/spm_api.h
index 565e708..adc57f2 100644
--- a/secure_fw/spm/spm_api.h
+++ b/secure_fw/spm/spm_api.h
@@ -12,6 +12,10 @@
 #include "tfm_api.h"
 #include "spm_partition_defs.h"
 #include "secure_fw/core/tfm_secure_api.h"
+#ifdef TFM_PSA_API
+#include "tfm_list.h"
+#include "tfm_wait.h"
+#endif
 
 #define SPM_INVALID_PARTITION_IDX     (~0U)
 
@@ -40,6 +44,7 @@
 #define SPM_PART_FLAG_PSA_ROT 0x02
 #define SPM_PART_FLAG_IPC     0x04
 
+#ifndef TFM_PSA_API
 /**
  * \brief Holds the iovec parameters that are passed to a service
  *
@@ -53,11 +58,17 @@
     size_t out_len;                    /*!< Number psa_outvec objects in out_vec
                                         */
 };
+#endif /* !define(TFM_PSA_API) */
 
 /**
  * \brief Runtime context information of a partition
  */
 struct spm_partition_runtime_data_t {
+#ifdef TFM_PSA_API
+    struct tfm_event_t signal_evnt;     /* Event signal                      */
+    uint32_t signals;                   /* Service signals had been triggered*/
+    struct tfm_list_node_t service_list;/* Service list                      */
+#else /* TFM_PSA_API */
     uint32_t partition_state;
     uint32_t caller_partition_idx;
     int32_t caller_client_id;
@@ -72,15 +83,11 @@
     struct iovec_args_t iovec_args;
     psa_outvec *orig_outvec;
     uint32_t *ctx_stack_ptr;
-    /*
-     * FIXME: There is a 'signal_mask' defined in the structure
-     * 'tfm_spm_ipc_partition_t'. It should be eliminated, and the IPC
-     * implementation should use the 'signal_mask' define in this structure.
-     * However currently the content of 'spm_partition_runtime_data_t' structure
-     * is not maintained by the IPC implementation. This is to be fixed with the
-     * effort of restructuring common code among library and IPC model.
-     */
-    uint32_t signal_mask;
+#endif /* TFM_PSA_API */
+    uint32_t signal_mask;               /*
+                                         * Service signal mask passed by
+                                         * psa_wait()
+                                         */
 };
 
 
diff --git a/secure_fw/spm/spm_db.h b/secure_fw/spm/spm_db.h
index 8b1a00b..7b74aba 100644
--- a/secure_fw/spm/spm_db.h
+++ b/secure_fw/spm/spm_db.h
@@ -39,6 +39,7 @@
  * phase.
  */
 struct spm_partition_static_data_t {
+    int32_t index;                      /* Partition index */
     uint32_t partition_id;
     uint32_t partition_flags;
     uint32_t partition_priority;
@@ -64,7 +65,9 @@
 struct spm_partition_db_t {
     uint32_t is_init;
     uint32_t partition_count;
+#ifndef TFM_PSA_API
     uint32_t running_partition_idx;
+#endif /* !defined(TFM_PSA_API) */
     struct spm_partition_desc_t *partitions;
 };