blob: a07539415fd66b7c426d553588236bfd574f23a3 [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Elyes HAOUASadd76f92019-03-21 09:55:49 +01002
Raul E Rangelb29f9d42021-07-12 13:49:59 -06003#include <assert.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -05004#include <stddef.h>
5#include <stdint.h>
6#include <stdlib.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -05007#include <bootstate.h>
Raul E Rangelcc01da52021-07-12 13:43:48 -06008#include <commonlib/bsd/compiler.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -05009#include <console/console.h>
Raul E Rangelc2c38f52021-10-08 13:10:38 -060010#include <smp/node.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -050011#include <thread.h>
Elyes HAOUASadd76f92019-03-21 09:55:49 +010012#include <timer.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -050013
Raul E Rangeldb16ac952021-09-24 14:00:56 -060014static u8 thread_stacks[CONFIG_STACK_SIZE * CONFIG_NUM_THREADS] __aligned(sizeof(uint64_t));
Raul E Rangel000138e62021-07-14 11:44:51 -060015static bool initialized;
16
Aaron Durbin4409a5e2013-05-06 12:20:52 -050017static void idle_thread_init(void);
18
19/* There needs to be at least one thread to run the ramstate state machine. */
20#define TOTAL_NUM_THREADS (CONFIG_NUM_THREADS + 1)
Aaron Durbin4409a5e2013-05-06 12:20:52 -050021
22/* Storage space for the thread structs .*/
23static struct thread all_threads[TOTAL_NUM_THREADS];
24
25/* All runnable (but not running) and free threads are kept on their
26 * respective lists. */
27static struct thread *runnable_threads;
28static struct thread *free_threads;
29
Raul E Rangelc2c38f52021-10-08 13:10:38 -060030static struct thread *active_thread;
31
Aaron Durbin4409a5e2013-05-06 12:20:52 -050032static inline int thread_can_yield(const struct thread *t)
33{
Raul E Rangelbe60a0d2021-07-15 13:52:03 -060034 return (t != NULL && t->can_yield > 0);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050035}
36
Raul E Rangelc2c38f52021-10-08 13:10:38 -060037static inline void set_current_thread(struct thread *t)
38{
39 assert(boot_cpu());
40 active_thread = t;
41}
42
Aaron Durbin4409a5e2013-05-06 12:20:52 -050043static inline struct thread *current_thread(void)
44{
Raul E Rangelc2c38f52021-10-08 13:10:38 -060045 if (!initialized || !boot_cpu())
Raul E Rangel000138e62021-07-14 11:44:51 -060046 return NULL;
47
Raul E Rangelc2c38f52021-10-08 13:10:38 -060048 return active_thread;
Aaron Durbin4409a5e2013-05-06 12:20:52 -050049}
50
51static inline int thread_list_empty(struct thread **list)
52{
53 return *list == NULL;
54}
55
56static inline struct thread *pop_thread(struct thread **list)
57{
58 struct thread *t;
59
60 t = *list;
61 *list = t->next;
62 t->next = NULL;
63 return t;
64}
65
66static inline void push_thread(struct thread **list, struct thread *t)
67{
68 t->next = *list;
69 *list = t;
70}
71
72static inline void push_runnable(struct thread *t)
73{
74 push_thread(&runnable_threads, t);
75}
76
77static inline struct thread *pop_runnable(void)
78{
79 return pop_thread(&runnable_threads);
80}
81
82static inline struct thread *get_free_thread(void)
83{
84 struct thread *t;
Aaron Durbin4409a5e2013-05-06 12:20:52 -050085
86 if (thread_list_empty(&free_threads))
87 return NULL;
88
89 t = pop_thread(&free_threads);
90
Aaron Durbin4409a5e2013-05-06 12:20:52 -050091 /* Reset the current stack value to the original. */
Raul E Rangeldb16ac952021-09-24 14:00:56 -060092 if (!t->stack_orig)
93 die("%s: Invalid stack value\n", __func__);
94
Aaron Durbin4409a5e2013-05-06 12:20:52 -050095 t->stack_current = t->stack_orig;
96
97 return t;
98}
99
100static inline void free_thread(struct thread *t)
101{
102 push_thread(&free_threads, t);
103}
104
105/* The idle thread is ran whenever there isn't anything else that is runnable.
106 * It's sole responsibility is to ensure progress is made by running the timer
107 * callbacks. */
Raul E Rangelcc01da52021-07-12 13:43:48 -0600108__noreturn static enum cb_err idle_thread(void *unused)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500109{
110 /* This thread never voluntarily yields. */
Raul E Rangel9ba36ab2021-07-15 17:34:05 -0600111 thread_coop_disable();
Lee Leahy2f919ec2017-03-08 17:37:06 -0800112 while (1)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500113 timers_run();
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500114}
115
116static void schedule(struct thread *t)
117{
118 struct thread *current = current_thread();
119
120 /* If t is NULL need to find new runnable thread. */
121 if (t == NULL) {
122 if (thread_list_empty(&runnable_threads))
123 die("Runnable thread list is empty!\n");
124 t = pop_runnable();
125 } else {
126 /* current is still runnable. */
127 push_runnable(current);
128 }
Raul E Rangelcc01da52021-07-12 13:43:48 -0600129
130 if (t->handle)
131 t->handle->state = THREAD_STARTED;
132
Raul E Rangelc2c38f52021-10-08 13:10:38 -0600133 set_current_thread(t);
Raul E Rangelc842c592021-09-13 14:24:55 -0600134
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500135 switch_to_thread(t->stack_current, &current->stack_current);
136}
137
Raul E Rangelcc01da52021-07-12 13:43:48 -0600138static void terminate_thread(struct thread *t, enum cb_err error)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500139{
Raul E Rangelcc01da52021-07-12 13:43:48 -0600140 if (t->handle) {
141 t->handle->error = error;
142 t->handle->state = THREAD_DONE;
143 }
144
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500145 free_thread(t);
146 schedule(NULL);
147}
148
149static void asmlinkage call_wrapper(void *unused)
150{
151 struct thread *current = current_thread();
Raul E Rangelcc01da52021-07-12 13:43:48 -0600152 enum cb_err error;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500153
Raul E Rangelcc01da52021-07-12 13:43:48 -0600154 error = current->entry(current->entry_arg);
155
156 terminate_thread(current, error);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500157}
158
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500159struct block_boot_state {
160 boot_state_t state;
161 boot_state_sequence_t seq;
162};
163
164/* Block the provided state until thread is complete. */
165static void asmlinkage call_wrapper_block_state(void *arg)
166{
167 struct block_boot_state *bbs = arg;
168 struct thread *current = current_thread();
Raul E Rangelcc01da52021-07-12 13:43:48 -0600169 enum cb_err error;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500170
171 boot_state_block(bbs->state, bbs->seq);
Raul E Rangelcc01da52021-07-12 13:43:48 -0600172 error = current->entry(current->entry_arg);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500173 boot_state_unblock(bbs->state, bbs->seq);
Raul E Rangelcc01da52021-07-12 13:43:48 -0600174 terminate_thread(current, error);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500175}
176
177/* Prepare a thread so that it starts by executing thread_entry(thread_arg).
178 * Within thread_entry() it will call func(arg). */
Raul E Rangelcc01da52021-07-12 13:43:48 -0600179static void prepare_thread(struct thread *t, struct thread_handle *handle,
180 enum cb_err (*func)(void *), void *arg,
181 asmlinkage void (*thread_entry)(void *), void *thread_arg)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500182{
183 /* Stash the function and argument to run. */
184 t->entry = func;
185 t->entry_arg = arg;
186
187 /* All new threads can yield by default. */
188 t->can_yield = 1;
189
Raul E Rangelcc01da52021-07-12 13:43:48 -0600190 /* Pointer used to publish the state of thread */
191 t->handle = handle;
192
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500193 arch_prepare_thread(t, thread_entry, thread_arg);
194}
195
196static void thread_resume_from_timeout(struct timeout_callback *tocb)
197{
198 struct thread *to;
199
200 to = tocb->priv;
201 schedule(to);
202}
203
204static void idle_thread_init(void)
205{
206 struct thread *t;
207
208 t = get_free_thread();
209
Lee Leahy2f919ec2017-03-08 17:37:06 -0800210 if (t == NULL)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500211 die("No threads available for idle thread!\n");
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500212
213 /* Queue idle thread to run once all other threads have yielded. */
Raul E Rangelcc01da52021-07-12 13:43:48 -0600214 prepare_thread(t, NULL, idle_thread, NULL, call_wrapper, NULL);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500215 push_runnable(t);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500216}
217
218/* Don't inline this function so the timeout_callback won't have its storage
219 * space on the stack cleaned up before the call to schedule(). */
220static int __attribute__((noinline))
Lee Leahy73402172017-03-10 15:23:24 -0800221thread_yield_timed_callback(struct timeout_callback *tocb,
222 unsigned int microsecs)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500223{
224 tocb->priv = current_thread();
225 tocb->callback = thread_resume_from_timeout;
226
227 if (timer_sched_callback(tocb, microsecs))
228 return -1;
229
230 /* The timer callback will wake up the current thread. */
231 schedule(NULL);
232 return 0;
233}
234
235static void *thread_alloc_space(struct thread *t, size_t bytes)
236{
237 /* Allocate the amount of space on the stack keeping the stack
238 * aligned to the pointer size. */
239 t->stack_current -= ALIGN_UP(bytes, sizeof(uintptr_t));
240
241 return (void *)t->stack_current;
242}
243
Raul E Rangela2d83c682021-07-22 11:16:19 -0600244static void threads_initialize(void)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500245{
246 int i;
247 struct thread *t;
Ronald G. Minnich34352d12013-08-21 16:03:32 -0700248 u8 *stack_top;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500249
Raul E Rangela2d83c682021-07-22 11:16:19 -0600250 if (initialized)
251 return;
252
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500253 t = &all_threads[0];
Raul E Rangelc2c38f52021-10-08 13:10:38 -0600254
255 set_current_thread(t);
256
Raul E Rangeldb16ac952021-09-24 14:00:56 -0600257 t->stack_orig = (uintptr_t)NULL; /* We never free the main thread */
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500258 t->id = 0;
Raul E Rangelb95369c2021-07-15 17:28:13 -0600259 t->can_yield = 1;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500260
Raul E Rangelc842c592021-09-13 14:24:55 -0600261 stack_top = &thread_stacks[CONFIG_STACK_SIZE];
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500262 for (i = 1; i < TOTAL_NUM_THREADS; i++) {
263 t = &all_threads[i];
264 t->stack_orig = (uintptr_t)stack_top;
265 t->id = i;
266 stack_top += CONFIG_STACK_SIZE;
267 free_thread(t);
268 }
269
270 idle_thread_init();
Raul E Rangelb95369c2021-07-15 17:28:13 -0600271
272 initialized = 1;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500273}
274
Raul E Rangelcc01da52021-07-12 13:43:48 -0600275int thread_run(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500276{
277 struct thread *current;
278 struct thread *t;
279
Raul E Rangela2d83c682021-07-22 11:16:19 -0600280 /* Lazy initialization */
281 threads_initialize();
282
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500283 current = current_thread();
284
285 if (!thread_can_yield(current)) {
286 printk(BIOS_ERR,
Raul E Rangelba51e292021-11-02 13:42:33 -0600287 "ERROR: thread_run() called from non-yielding context!\n");
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500288 return -1;
289 }
290
291 t = get_free_thread();
292
293 if (t == NULL) {
Raul E Rangelba51e292021-11-02 13:42:33 -0600294 printk(BIOS_ERR, "ERROR: thread_run() No more threads!\n");
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500295 return -1;
296 }
297
Raul E Rangel4aec58d2021-07-15 13:20:58 -0600298 prepare_thread(t, handle, func, arg, call_wrapper, NULL);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500299 schedule(t);
300
301 return 0;
302}
303
Raul E Rangelcc01da52021-07-12 13:43:48 -0600304int thread_run_until(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg,
Lee Leahye20a3192017-03-09 16:21:34 -0800305 boot_state_t state, boot_state_sequence_t seq)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500306{
307 struct thread *current;
308 struct thread *t;
309 struct block_boot_state *bbs;
310
Raul E Rangel8c892072021-07-22 12:40:26 -0600311 /* This is a ramstage specific API */
312 if (!ENV_RAMSTAGE)
313 dead_code();
314
Raul E Rangela2d83c682021-07-22 11:16:19 -0600315 /* Lazy initialization */
316 threads_initialize();
317
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500318 current = current_thread();
319
320 if (!thread_can_yield(current)) {
321 printk(BIOS_ERR,
Raul E Rangelba51e292021-11-02 13:42:33 -0600322 "ERROR: thread_run() called from non-yielding context!\n");
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500323 return -1;
324 }
325
326 t = get_free_thread();
327
328 if (t == NULL) {
Raul E Rangelba51e292021-11-02 13:42:33 -0600329 printk(BIOS_ERR, "ERROR: thread_run() No more threads!\n");
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500330 return -1;
331 }
332
333 bbs = thread_alloc_space(t, sizeof(*bbs));
334 bbs->state = state;
335 bbs->seq = seq;
Raul E Rangelcc01da52021-07-12 13:43:48 -0600336 prepare_thread(t, handle, func, arg, call_wrapper_block_state, bbs);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500337 schedule(t);
338
339 return 0;
340}
341
Raul E Rangeld5dca212021-07-15 11:48:48 -0600342int thread_yield(void)
343{
344 return thread_yield_microseconds(0);
345}
346
Lee Leahy75b85992017-03-08 16:34:12 -0800347int thread_yield_microseconds(unsigned int microsecs)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500348{
349 struct thread *current;
350 struct timeout_callback tocb;
351
352 current = current_thread();
353
354 if (!thread_can_yield(current))
355 return -1;
356
357 if (thread_yield_timed_callback(&tocb, microsecs))
358 return -1;
359
360 return 0;
361}
362
Raul E Rangel9ba36ab2021-07-15 17:34:05 -0600363void thread_coop_enable(void)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500364{
365 struct thread *current;
366
367 current = current_thread();
368
Raul E Rangelbe60a0d2021-07-15 13:52:03 -0600369 if (current == NULL)
370 return;
371
372 assert(current->can_yield <= 0);
373
374 current->can_yield++;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500375}
376
Raul E Rangel9ba36ab2021-07-15 17:34:05 -0600377void thread_coop_disable(void)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500378{
379 struct thread *current;
380
381 current = current_thread();
382
Raul E Rangelbe60a0d2021-07-15 13:52:03 -0600383 if (current == NULL)
384 return;
385
386 current->can_yield--;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500387}
Raul E Rangelb29f9d42021-07-12 13:49:59 -0600388
Raul E Rangelcc01da52021-07-12 13:43:48 -0600389enum cb_err thread_join(struct thread_handle *handle)
390{
391 struct stopwatch sw;
392 struct thread *current = current_thread();
393
394 assert(handle);
395 assert(current);
396 assert(current->handle != handle);
397
398 if (handle->state == THREAD_UNINITIALIZED)
399 return CB_ERR_ARG;
400
401 stopwatch_init(&sw);
402
403 printk(BIOS_SPEW, "waiting for thread\n");
404
405 while (handle->state != THREAD_DONE)
406 assert(thread_yield() == 0);
407
408 printk(BIOS_SPEW, "took %lu us\n", stopwatch_duration_usecs(&sw));
409
410 return handle->error;
411}
412
Raul E Rangelb29f9d42021-07-12 13:49:59 -0600413void thread_mutex_lock(struct thread_mutex *mutex)
414{
415 struct stopwatch sw;
416
417 stopwatch_init(&sw);
418
419 while (mutex->locked)
420 assert(thread_yield() == 0);
421 mutex->locked = true;
422
423 printk(BIOS_SPEW, "took %lu us to acquire mutex\n", stopwatch_duration_usecs(&sw));
424}
425
426void thread_mutex_unlock(struct thread_mutex *mutex)
427{
428 assert(mutex->locked);
429 mutex->locked = 0;
430}