SPM: Update thread state naming

The thread state THRD_STATE_RUNNING is actually used as runnable.
For example, in the tfm_event_wake() function, a thread woke up by
an event is only becoming runnable. Whether to make it as the current
running thread is decided by scheduler with other condition checks
such as priority - only the thread has highest priority in the runnable
list can run.
The current running thread is indicated by CURR_THRD.

This patch updates the naming to reflect the actual implementation.
Also moves some static functions to caller to simply the code.

Change-Id: Ib8c843c85feff35d6d6287bf82093801e3168e80
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
diff --git a/secure_fw/spm/cmsis_psa/spm_ipc.c b/secure_fw/spm/cmsis_psa/spm_ipc.c
index 782d9a8..c86be41 100644
--- a/secure_fw/spm/cmsis_psa/spm_ipc.c
+++ b/secure_fw/spm/cmsis_psa/spm_ipc.c
@@ -377,7 +377,7 @@
 
 struct partition_t *tfm_spm_get_running_partition(void)
 {
-    struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr_thread();
+    struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr();
     struct partition_t *partition;
 
     partition = TFM_GET_CONTAINER_PTR(pth, struct partition_t, sp_thread);
@@ -754,8 +754,8 @@
     struct partition_t *p_next_partition;
     uint32_t is_privileged;
 #endif
-    struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next_thread();
-    struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr_thread();
+    struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next();
+    struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr();
 
     if (pth_next != NULL && pth_curr != pth_next) {
 #if TFM_LVL != 1
diff --git a/secure_fw/spm/cmsis_psa/tfm_thread.c b/secure_fw/spm/cmsis_psa/tfm_thread.c
index 3b815cb..4c4ef16 100644
--- a/secure_fw/spm/cmsis_psa/tfm_thread.c
+++ b/secure_fw/spm/cmsis_psa/tfm_thread.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -13,37 +13,33 @@
 #include "tfm_core_utils.h"
 
 /* Force ZERO in case ZI(bss) clear is missing */
-static struct tfm_core_thread_t *p_thrd_head = NULL;
-static struct tfm_core_thread_t *p_runn_head = NULL;
-static struct tfm_core_thread_t *p_curr_thrd = NULL;
+static struct tfm_core_thread_t *p_thrd_head = NULL; /* Head of all threads */
+static struct tfm_core_thread_t *p_rnbl_head = NULL; /* Head of runnable */
+static struct tfm_core_thread_t *p_curr_thrd = NULL; /* Current running */
 
 /* Define Macro to fetch global to support future expansion (PERCPU e.g.) */
 #define LIST_HEAD   p_thrd_head
-#define RUNN_HEAD   p_runn_head
+#define RNBL_HEAD   p_rnbl_head
 #define CURR_THRD   p_curr_thrd
 
-static struct tfm_core_thread_t *find_next_running_thread(
-                                                struct tfm_core_thread_t *pth)
+/* Get next thread to run for scheduler */
+struct tfm_core_thread_t *tfm_core_thrd_get_next(void)
 {
-    while (pth && pth->state != THRD_STATE_RUNNING) {
+    struct tfm_core_thread_t *pth = RNBL_HEAD;
+
+    /*
+     * First runnable thread has highest priority since threads are sorted with
+     * priority.
+     */
+    while (pth && pth->state != THRD_STATE_RUNNABLE) {
         pth = pth->next;
     }
 
     return pth;
 }
 
-/* To get next running thread for scheduler */
-struct tfm_core_thread_t *tfm_core_thrd_get_next_thread(void)
-{
-    /*
-     * First RUNNING thread has highest priority since threads are sorted with
-     * priority.
-     */
-    return find_next_running_thread(RUNN_HEAD);
-}
-
-/* To get current thread for caller */
-struct tfm_core_thread_t *tfm_core_thrd_get_curr_thread(void)
+/* To get current running thread for caller */
+struct tfm_core_thread_t *tfm_core_thrd_get_curr(void)
 {
     return CURR_THRD;
 }
@@ -66,21 +62,6 @@
     }
 }
 
-/*
- * Set first running thread as head to reduce enumerate
- * depth while searching for a first running thread.
- */
-static void update_running_head(struct tfm_core_thread_t **runn,
-                                struct tfm_core_thread_t *node)
-{
-    if ((node->state == THRD_STATE_RUNNING) &&
-        (*runn == NULL || (node->prior < (*runn)->prior))) {
-        *runn = node;
-    } else {
-        *runn = LIST_HEAD;
-    }
-}
-
 /* Set context members only. No validation here */
 void tfm_core_thrd_init(struct tfm_core_thread_t *pth,
                         tfm_core_thrd_entry_t pfn, void *param,
@@ -111,8 +92,8 @@
     /* Insert a new thread with priority */
     insert_by_prior(&LIST_HEAD, pth);
 
-    /* Mark it as RUNNING after insertion */
-    tfm_core_thrd_set_state(pth, THRD_STATE_RUNNING);
+    /* Mark it as RUNNABLE after insertion */
+    tfm_core_thrd_set_state(pth, THRD_STATE_RUNNABLE);
 
     return THRD_SUCCESS;
 }
@@ -122,7 +103,17 @@
     TFM_CORE_ASSERT(pth != NULL && new_state < THRD_STATE_INVALID);
 
     pth->state = new_state;
-    update_running_head(&RUNN_HEAD, pth);
+
+    /*
+     * Set first runnable thread as head to reduce enumerate
+     * depth while searching for a first runnable thread.
+     */
+    if ((pth->state == THRD_STATE_RUNNABLE) &&
+        (RNBL_HEAD == NULL || (pth->prior < RNBL_HEAD->prior))) {
+        RNBL_HEAD = pth;
+    } else {
+        RNBL_HEAD = LIST_HEAD;
+    }
 }
 
 /* Scheduling won't happen immediately but after the exception returns */
diff --git a/secure_fw/spm/cmsis_psa/tfm_thread.h b/secure_fw/spm/cmsis_psa/tfm_thread.h
index 925967b..8e96e3f 100644
--- a/secure_fw/spm/cmsis_psa/tfm_thread.h
+++ b/secure_fw/spm/cmsis_psa/tfm_thread.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -14,7 +14,7 @@
 
 /* State code */
 #define THRD_STATE_CREATING       0
-#define THRD_STATE_RUNNING        1
+#define THRD_STATE_RUNNABLE       1
 #define THRD_STATE_BLOCK          2
 #define THRD_STATE_DETACH         3
 #define THRD_STATE_INVALID        4
@@ -170,15 +170,15 @@
  * Return :
  *  Current running thread context pointer.
  */
-struct tfm_core_thread_t *tfm_core_thrd_get_curr_thread(void);
+struct tfm_core_thread_t *tfm_core_thrd_get_curr(void);
 
 /*
- * Get next running thread in list.
+ * Get next thread to run in list.
  *
  * Return :
- *  Pointer of next thread to be run.
+ *  Pointer of next thread to run.
  */
-struct tfm_core_thread_t *tfm_core_thrd_get_next_thread(void);
+struct tfm_core_thread_t *tfm_core_thrd_get_next(void);
 
 /*
  * Start scheduler for existing threads
diff --git a/secure_fw/spm/cmsis_psa/tfm_wait.c b/secure_fw/spm/cmsis_psa/tfm_wait.c
index c199c88..a8f11fa 100644
--- a/secure_fw/spm/cmsis_psa/tfm_wait.c
+++ b/secure_fw/spm/cmsis_psa/tfm_wait.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -12,7 +12,7 @@
 {
     TFM_CORE_ASSERT(pevnt && pevnt->magic == TFM_EVENT_MAGIC);
 
-    pevnt->owner = tfm_core_thrd_get_curr_thread();
+    pevnt->owner = tfm_core_thrd_get_curr();
     tfm_core_thrd_set_state(pevnt->owner, THRD_STATE_BLOCK);
     tfm_core_thrd_activate_schedule();
 }
@@ -22,7 +22,7 @@
     TFM_CORE_ASSERT(pevnt && pevnt->magic == TFM_EVENT_MAGIC);
 
     if (pevnt->owner && pevnt->owner->state == THRD_STATE_BLOCK) {
-        tfm_core_thrd_set_state(pevnt->owner, THRD_STATE_RUNNING);
+        tfm_core_thrd_set_state(pevnt->owner, THRD_STATE_RUNNABLE);
         tfm_core_thrd_set_retval(pevnt->owner, retval);
         pevnt->owner = NULL;
         tfm_core_thrd_activate_schedule();