SPM: Trigger scheduler at the end of SPM API

Trigger scheduler immediately is unnecessary and redundant.
Scheduling can be triggered at the final step of API handling.

Change-Id: Ifea20e5c00f5dff221a8fe36deb222c0d209fe15
Signed-off-by: Mingyang Sun <mingyang.sun@arm.com>
diff --git a/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c b/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c
index a3e2cf3..d92cffe 100644
--- a/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c
+++ b/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c
@@ -241,6 +241,11 @@
             tfm_core_panic();
         }
         svc_args[0] = SVC_Handler_IPC(svc_number, svc_args, exc_return);
+
+        if (THRD_EXPECTING_SCHEDULE()) {
+            tfm_arch_trigger_pendsv();
+        }
+
         break;
     }
 
diff --git a/secure_fw/spm/cmsis_psa/thread.c b/secure_fw/spm/cmsis_psa/thread.c
index 9014619..9e9b70f 100644
--- a/secure_fw/spm/cmsis_psa/thread.c
+++ b/secure_fw/spm/cmsis_psa/thread.c
@@ -105,7 +105,6 @@
 
     p_sync_obj->owner = pth;
     thrd_set_state(pth, THRD_STATE_BLOCK);
-    tfm_arch_trigger_pendsv();
 }
 
 void thrd_wake_up(struct sync_obj_t *p_sync_obj, uint32_t ret_val)
@@ -117,6 +116,5 @@
         tfm_arch_set_context_ret_code(p_sync_obj->owner->p_context_ctrl,
                                       ret_val);
         p_sync_obj->owner = NULL;
-        tfm_arch_trigger_pendsv();
     }
 }
diff --git a/secure_fw/spm/cmsis_psa/thread.h b/secure_fw/spm/cmsis_psa/thread.h
index 86aacd0..ad1aad9 100644
--- a/secure_fw/spm/cmsis_psa/thread.h
+++ b/secure_fw/spm/cmsis_psa/thread.h
@@ -8,8 +8,8 @@
 #ifndef __M_THREAD_H__ /* Add an extra M as thread.h is common. */
 #define __M_THREAD_H__
 
-#include <stdint.h>
 #include <stddef.h>
+#include <stdint.h>
 
 /* State codes */
 #define THRD_STATE_CREATING       0
@@ -36,7 +36,7 @@
 struct thread_t {
     uint8_t         priority;           /* Priority                          */
     uint8_t         state;              /* State                             */
-    uint16_t        flags;              /* specific flags                    */
+    uint16_t        flags;              /* Specific flags                    */
     void            *p_context_ctrl;    /* Context control (sp, splimit, lr) */
     struct thread_t *next;              /* Next thread in list               */
 };
@@ -82,10 +82,18 @@
  * Parameters :
  *  x              -     Context pointer to be bound with the current thread.
  */
-#define THRD_UPDATE_CUR_CTXCTRL(x)         \
+#define THRD_UPDATE_CUR_CTXCTRL(x)          \
                                 CURRENT_THREAD->p_context_ctrl = (void *)(x)
 
 /*
+ * Check if a schedule is under expectation by measuring on a given thread.
+ *
+ * Return :
+ *  `true` if schedule is under expectation. `false` if not.
+ */
+#define THRD_EXPECTING_SCHEDULE() (!(thrd_next() == CURRENT_THREAD))
+
+/*
  * Set thread state, and updates the runnable head.
  *
  * Parameters :
@@ -131,7 +139,6 @@
  */
 uint32_t thrd_start_scheduler(struct thread_t **ppth);
 
-
 /* Sync object */
 
 /* A magic for corruption detecting */
diff --git a/secure_fw/spm/ffm/psa_api.c b/secure_fw/spm/ffm/psa_api.c
index b38f85c..52f15e6 100644
--- a/secure_fw/spm/ffm/psa_api.c
+++ b/secure_fw/spm/ffm/psa_api.c
@@ -414,9 +414,6 @@
         partition->signals_waiting = signal_mask;
         thrd_wait_on(&partition->waitobj,
                      &(tfm_spm_get_running_partition()->thrd));
-    } else if ((partition->signals_asserted & signal_mask) == 0) {
-        /* Activate scheduler to check if any higher priority thread to run */
-        tfm_arch_trigger_pendsv();
     }
 
     return partition->signals_asserted & signal_mask;