blob: 159344b53a1a4c1a6685e50137cf2c584fbf2eda [file] [log] [blame]
Ken Liu91d44da2018-09-20 22:42:31 +08001/*
Kevin Peng82fbca52021-03-09 13:48:48 +08002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
Ken Liu91d44da2018-09-20 22:42:31 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7#ifndef __TFM_THREAD_H__
8#define __TFM_THREAD_H__
9
Ken Liu5a2b9052019-08-15 19:03:29 +080010#include <stdint.h>
11#include <stddef.h>
David Hu50711e32019-06-12 18:32:30 +080012#include "tfm_arch.h"
Ken Liu91d44da2018-09-20 22:42:31 +080013#include "cmsis_compiler.h"
14
Summer Qin66f1e032020-01-06 15:40:03 +080015/* State code */
16#define THRD_STATE_CREATING 0
Kevin Peng82fbca52021-03-09 13:48:48 +080017#define THRD_STATE_RUNNABLE 1
Summer Qin66f1e032020-01-06 15:40:03 +080018#define THRD_STATE_BLOCK 2
19#define THRD_STATE_DETACH 3
20#define THRD_STATE_INVALID 4
Ken Liu91d44da2018-09-20 22:42:31 +080021
22/* Security attribute - default as security */
23#define THRD_ATTR_SECURE_OFFSET 16
24#define THRD_ATTR_SECURE (0)
25#define THRD_ATTR_NON_SECURE (1 << THRD_ATTR_SECURE_OFFSET)
26
27/* Lower value has higher priority */
28#define THRD_PRIOR_MASK 0xFF
29#define THRD_PRIOR_HIGHEST 0x0
Mingyang Sun00df2352021-04-15 15:46:08 +080030#define THRD_PRIOR_HIGH 0xF
31#define THRD_PRIOR_MEDIUM 0x1F
32#define THRD_PRIOR_LOW 0x7F
Ken Liu91d44da2018-09-20 22:42:31 +080033#define THRD_PRIOR_LOWEST 0xFF
34
35/* Error code */
36#define THRD_SUCCESS 0
37#define THRD_ERR_INVALID_PARAM 1
38
39/* Thread entry function type */
Summer Qin66f1e032020-01-06 15:40:03 +080040typedef void *(*tfm_core_thrd_entry_t)(void *);
Ken Liu91d44da2018-09-20 22:42:31 +080041
42/* Thread context */
Summer Qin66f1e032020-01-06 15:40:03 +080043struct tfm_core_thread_t {
44 tfm_core_thrd_entry_t pfn; /* entry function */
Ken Liu5a2b9052019-08-15 19:03:29 +080045 void *param; /* entry parameter */
Summer Qincb190872020-01-06 16:08:26 +080046 uintptr_t stk_btm; /* stack bottom (lower address) */
47 uintptr_t stk_top; /* stack top (higher address)*/
Ken Liu5a2b9052019-08-15 19:03:29 +080048 uint32_t prior; /* priority */
Summer Qin66f1e032020-01-06 15:40:03 +080049 uint32_t state; /* state */
Ken Liu91d44da2018-09-20 22:42:31 +080050
Summer Qind2ad7e72020-01-06 18:16:35 +080051 struct tfm_arch_ctx_t arch_ctx; /* State context */
Summer Qin66f1e032020-01-06 15:40:03 +080052 struct tfm_core_thread_t *next; /* next thread in list */
Ken Liu91d44da2018-09-20 22:42:31 +080053};
54
55/*
56 * Initialize a thread context with the necessary info.
57 *
58 * Parameters :
59 * pth - pointer of caller provided thread context
60 * pfn - thread entry function
61 * param - thread entry function parameter
Summer Qincb190872020-01-06 16:08:26 +080062 * stk_top - stack pointer top (higher address)
63 * stk_btm - stack pointer bottom (lower address)
Ken Liu91d44da2018-09-20 22:42:31 +080064 *
65 * Notes :
66 * Thread contex rely on caller allocated memory; initialize members in
67 * context. This function does not insert thread into schedulable list.
68 */
Summer Qin66f1e032020-01-06 15:40:03 +080069void tfm_core_thrd_init(struct tfm_core_thread_t *pth,
70 tfm_core_thrd_entry_t pfn, void *param,
Summer Qincb190872020-01-06 16:08:26 +080071 uintptr_t stk_top, uintptr_t stk_btm);
Ken Liu91d44da2018-09-20 22:42:31 +080072
Summer Qin66f1e032020-01-06 15:40:03 +080073/*
74 * Set thread priority.
Ken Liu91d44da2018-09-20 22:42:31 +080075 *
76 * Parameters :
77 * pth - pointer of thread context
78 * prior - priority value (0~255)
79 *
80 * Notes :
81 * Set thread priority. Priority is set to THRD_PRIOR_MEDIUM in
Summer Qin66f1e032020-01-06 15:40:03 +080082 * tfm_core_thrd_init().
Ken Liu91d44da2018-09-20 22:42:31 +080083 */
Summer Qin66f1e032020-01-06 15:40:03 +080084void __STATIC_INLINE tfm_core_thrd_set_priority(struct tfm_core_thread_t *pth,
85 uint32_t prior)
Ken Liu91d44da2018-09-20 22:42:31 +080086{
87 pth->prior &= ~THRD_PRIOR_MASK;
88 pth->prior |= prior & THRD_PRIOR_MASK;
89}
90
91/*
92 * Set thread security attribute.
93 *
94 * Parameters :
95 * pth - pointer of thread context
96 * attr_secure - THRD_ATTR_SECURE or THRD_ATTR_NON_SECURE
97 *
98 * Notes
99 * Reuse prior of thread context to shift down non-secure thread priority.
100 */
Summer Qin66f1e032020-01-06 15:40:03 +0800101void __STATIC_INLINE tfm_core_thrd_set_secure(struct tfm_core_thread_t *pth,
102 uint32_t attr_secure)
Ken Liu91d44da2018-09-20 22:42:31 +0800103{
104 pth->prior &= ~THRD_ATTR_NON_SECURE;
105 pth->prior |= attr_secure;
106}
107
108/*
Summer Qin66f1e032020-01-06 15:40:03 +0800109 * Set thread state.
Ken Liu91d44da2018-09-20 22:42:31 +0800110 *
111 * Parameters :
112 * pth - pointer of thread context
Summer Qin66f1e032020-01-06 15:40:03 +0800113 * new_state - new state of thread
Ken Liu91d44da2018-09-20 22:42:31 +0800114 *
115 * Return :
116 * None
117 *
118 * Notes :
Summer Qin66f1e032020-01-06 15:40:03 +0800119 * Thread state is not changed if invalid state value inputed.
Ken Liu91d44da2018-09-20 22:42:31 +0800120 */
Summer Qin66f1e032020-01-06 15:40:03 +0800121void tfm_core_thrd_set_state(struct tfm_core_thread_t *pth, uint32_t new_state);
Ken Liu91d44da2018-09-20 22:42:31 +0800122
123/*
Summer Qin66f1e032020-01-06 15:40:03 +0800124 * Get thread state.
Ken Liu91d44da2018-09-20 22:42:31 +0800125 *
126 * Parameters :
127 * pth - pointer of thread context
128 *
129 * Return :
Summer Qin66f1e032020-01-06 15:40:03 +0800130 * State of thread
Ken Liu91d44da2018-09-20 22:42:31 +0800131 */
Summer Qin66f1e032020-01-06 15:40:03 +0800132uint32_t __STATIC_INLINE tfm_core_thrd_get_state(struct tfm_core_thread_t *pth)
Ken Liu91d44da2018-09-20 22:42:31 +0800133{
Summer Qin66f1e032020-01-06 15:40:03 +0800134 return pth->state;
Ken Liu91d44da2018-09-20 22:42:31 +0800135}
136
137/*
138 * Set thread state return value.
139 *
140 * Parameters :
141 * pth - pointer of thread context
142 * retval - return value to be set for thread state
143 *
144 * Notes :
145 * This API is useful for blocked syscall blocking thread. Syscall
146 * could set its return value to the caller before caller goes.
147 */
Summer Qin66f1e032020-01-06 15:40:03 +0800148void __STATIC_INLINE tfm_core_thrd_set_retval(struct tfm_core_thread_t *pth,
149 uint32_t retval)
Ken Liu91d44da2018-09-20 22:42:31 +0800150{
Summer Qind2ad7e72020-01-06 18:16:35 +0800151 TFM_STATE_RET_VAL(&pth->arch_ctx) = retval;
Ken Liu91d44da2018-09-20 22:42:31 +0800152}
153
154/*
155 * Validate thread context and insert it into schedulable list.
156 *
157 * Parameters :
158 * pth - pointer of thread context
159 *
160 * Return :
161 * THRD_SUCCESS for success. Or an error is returned.
162 *
163 * Notes :
164 * This function validates thread info. It returns error if thread info
Summer Qin66f1e032020-01-06 15:40:03 +0800165 * is not correct. Thread is avaliable after successful tfm_core_thrd_start().
Ken Liu91d44da2018-09-20 22:42:31 +0800166 */
Summer Qin66f1e032020-01-06 15:40:03 +0800167uint32_t tfm_core_thrd_start(struct tfm_core_thread_t *pth);
Ken Liu91d44da2018-09-20 22:42:31 +0800168
169/*
170 * Get current running thread.
171 *
172 * Return :
173 * Current running thread context pointer.
174 */
Kevin Peng82fbca52021-03-09 13:48:48 +0800175struct tfm_core_thread_t *tfm_core_thrd_get_curr(void);
Ken Liu91d44da2018-09-20 22:42:31 +0800176
177/*
Kevin Peng82fbca52021-03-09 13:48:48 +0800178 * Get next thread to run in list.
Ken Liu91d44da2018-09-20 22:42:31 +0800179 *
180 * Return :
Kevin Peng82fbca52021-03-09 13:48:48 +0800181 * Pointer of next thread to run.
Ken Liu91d44da2018-09-20 22:42:31 +0800182 */
Kevin Peng82fbca52021-03-09 13:48:48 +0800183struct tfm_core_thread_t *tfm_core_thrd_get_next(void);
Ken Liu91d44da2018-09-20 22:42:31 +0800184
185/*
Ken Liu483f5da2019-04-24 10:45:21 +0800186 * Start scheduler for existing threads
187 *
188 * Parameters:
189 * pth - pointer of the caller context collecting thread
190 *
191 * Notes :
192 * This function should be called only ONCE to start the scheduler.
193 * Caller needs to provide a thread object to collect current context.
194 * The usage of the collected context is caller defined.
195 */
Summer Qin66f1e032020-01-06 15:40:03 +0800196void tfm_core_thrd_start_scheduler(struct tfm_core_thread_t *pth);
Ken Liu483f5da2019-04-24 10:45:21 +0800197
198/*
Ken Liu91d44da2018-09-20 22:42:31 +0800199 * Activate a scheduling action after exception.
200 *
201 * Notes :
202 * This function could be called multiple times before scheduling.
203 */
Summer Qin66f1e032020-01-06 15:40:03 +0800204void tfm_core_thrd_activate_schedule(void);
Ken Liu91d44da2018-09-20 22:42:31 +0800205
206/*
Summer Qind2ad7e72020-01-06 18:16:35 +0800207 * Save current architecture context into 'prev' thread and switch to 'next'.
Ken Liu91d44da2018-09-20 22:42:31 +0800208 *
209 * Parameters :
Summer Qind2ad7e72020-01-06 18:16:35 +0800210 * p_actx - latest caller context
Ken Liu91d44da2018-09-20 22:42:31 +0800211 * prev - previous thread to be switched out
212 * next - thread to be run
213 *
214 * Notes :
215 * This function could be called multiple times before scheduling.
216 */
Summer Qind2ad7e72020-01-06 18:16:35 +0800217void tfm_core_thrd_switch_context(struct tfm_arch_ctx_t *p_actx,
Summer Qin66f1e032020-01-06 15:40:03 +0800218 struct tfm_core_thread_t *prev,
219 struct tfm_core_thread_t *next);
Ken Liu91d44da2018-09-20 22:42:31 +0800220
Ken Liu91d44da2018-09-20 22:42:31 +0800221#endif