blob: 944c75e19c09d95ff02987821cf2e90b94c996e8 [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 <bootstate.h>
5#include <console/console.h>
Raul E Rangelc2c38f52021-10-08 13:10:38 -06006#include <smp/node.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -05007#include <thread.h>
Elyes HAOUASadd76f92019-03-21 09:55:49 +01008#include <timer.h>
Elyes HAOUAS93a195c2021-12-31 18:46:13 +01009#include <types.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -050010
Raul E Rangeldb16ac952021-09-24 14:00:56 -060011static u8 thread_stacks[CONFIG_STACK_SIZE * CONFIG_NUM_THREADS] __aligned(sizeof(uint64_t));
Raul E Rangel000138e62021-07-14 11:44:51 -060012static bool initialized;
13
Aaron Durbin4409a5e2013-05-06 12:20:52 -050014static void idle_thread_init(void);
15
16/* There needs to be at least one thread to run the ramstate state machine. */
17#define TOTAL_NUM_THREADS (CONFIG_NUM_THREADS + 1)
Aaron Durbin4409a5e2013-05-06 12:20:52 -050018
19/* Storage space for the thread structs .*/
20static struct thread all_threads[TOTAL_NUM_THREADS];
21
22/* All runnable (but not running) and free threads are kept on their
23 * respective lists. */
24static struct thread *runnable_threads;
25static struct thread *free_threads;
26
Raul E Rangelc2c38f52021-10-08 13:10:38 -060027static struct thread *active_thread;
28
Aaron Durbin4409a5e2013-05-06 12:20:52 -050029static inline int thread_can_yield(const struct thread *t)
30{
Raul E Rangelbe60a0d2021-07-15 13:52:03 -060031 return (t != NULL && t->can_yield > 0);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050032}
33
Raul E Rangelc2c38f52021-10-08 13:10:38 -060034static inline void set_current_thread(struct thread *t)
35{
36 assert(boot_cpu());
37 active_thread = t;
38}
39
Aaron Durbin4409a5e2013-05-06 12:20:52 -050040static inline struct thread *current_thread(void)
41{
Raul E Rangelc2c38f52021-10-08 13:10:38 -060042 if (!initialized || !boot_cpu())
Raul E Rangel000138e62021-07-14 11:44:51 -060043 return NULL;
44
Raul E Rangelc2c38f52021-10-08 13:10:38 -060045 return active_thread;
Aaron Durbin4409a5e2013-05-06 12:20:52 -050046}
47
48static inline int thread_list_empty(struct thread **list)
49{
50 return *list == NULL;
51}
52
53static inline struct thread *pop_thread(struct thread **list)
54{
55 struct thread *t;
56
57 t = *list;
58 *list = t->next;
59 t->next = NULL;
60 return t;
61}
62
63static inline void push_thread(struct thread **list, struct thread *t)
64{
65 t->next = *list;
66 *list = t;
67}
68
69static inline void push_runnable(struct thread *t)
70{
71 push_thread(&runnable_threads, t);
72}
73
74static inline struct thread *pop_runnable(void)
75{
76 return pop_thread(&runnable_threads);
77}
78
79static inline struct thread *get_free_thread(void)
80{
81 struct thread *t;
Aaron Durbin4409a5e2013-05-06 12:20:52 -050082
83 if (thread_list_empty(&free_threads))
84 return NULL;
85
86 t = pop_thread(&free_threads);
87
Aaron Durbin4409a5e2013-05-06 12:20:52 -050088 /* Reset the current stack value to the original. */
Raul E Rangeldb16ac952021-09-24 14:00:56 -060089 if (!t->stack_orig)
90 die("%s: Invalid stack value\n", __func__);
91
Aaron Durbin4409a5e2013-05-06 12:20:52 -050092 t->stack_current = t->stack_orig;
93
94 return t;
95}
96
97static inline void free_thread(struct thread *t)
98{
99 push_thread(&free_threads, t);
100}
101
102/* The idle thread is ran whenever there isn't anything else that is runnable.
103 * It's sole responsibility is to ensure progress is made by running the timer
104 * callbacks. */
Raul E Rangelcc01da52021-07-12 13:43:48 -0600105__noreturn static enum cb_err idle_thread(void *unused)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500106{
107 /* This thread never voluntarily yields. */
Raul E Rangel9ba36ab2021-07-15 17:34:05 -0600108 thread_coop_disable();
Lee Leahy2f919ec2017-03-08 17:37:06 -0800109 while (1)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500110 timers_run();
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500111}
112
113static void schedule(struct thread *t)
114{
115 struct thread *current = current_thread();
116
117 /* If t is NULL need to find new runnable thread. */
118 if (t == NULL) {
119 if (thread_list_empty(&runnable_threads))
120 die("Runnable thread list is empty!\n");
121 t = pop_runnable();
122 } else {
123 /* current is still runnable. */
124 push_runnable(current);
125 }
Raul E Rangelcc01da52021-07-12 13:43:48 -0600126
127 if (t->handle)
128 t->handle->state = THREAD_STARTED;
129
Raul E Rangelc2c38f52021-10-08 13:10:38 -0600130 set_current_thread(t);
Raul E Rangelc842c592021-09-13 14:24:55 -0600131
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500132 switch_to_thread(t->stack_current, &current->stack_current);
133}
134
Raul E Rangelcc01da52021-07-12 13:43:48 -0600135static void terminate_thread(struct thread *t, enum cb_err error)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500136{
Raul E Rangelcc01da52021-07-12 13:43:48 -0600137 if (t->handle) {
138 t->handle->error = error;
139 t->handle->state = THREAD_DONE;
140 }
141
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500142 free_thread(t);
143 schedule(NULL);
144}
145
Elyes Haouase6940c02024-03-30 09:51:05 +0100146static asmlinkage void call_wrapper(void *unused)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500147{
148 struct thread *current = current_thread();
Raul E Rangelcc01da52021-07-12 13:43:48 -0600149 enum cb_err error;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500150
Raul E Rangelcc01da52021-07-12 13:43:48 -0600151 error = current->entry(current->entry_arg);
152
153 terminate_thread(current, error);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500154}
155
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500156struct block_boot_state {
157 boot_state_t state;
158 boot_state_sequence_t seq;
159};
160
161/* Block the provided state until thread is complete. */
Elyes Haouase6940c02024-03-30 09:51:05 +0100162static asmlinkage void call_wrapper_block_state(void *arg)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500163{
164 struct block_boot_state *bbs = arg;
165 struct thread *current = current_thread();
Raul E Rangelcc01da52021-07-12 13:43:48 -0600166 enum cb_err error;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500167
168 boot_state_block(bbs->state, bbs->seq);
Raul E Rangelcc01da52021-07-12 13:43:48 -0600169 error = current->entry(current->entry_arg);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500170 boot_state_unblock(bbs->state, bbs->seq);
Raul E Rangelcc01da52021-07-12 13:43:48 -0600171 terminate_thread(current, error);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500172}
173
174/* Prepare a thread so that it starts by executing thread_entry(thread_arg).
175 * Within thread_entry() it will call func(arg). */
Raul E Rangelcc01da52021-07-12 13:43:48 -0600176static void prepare_thread(struct thread *t, struct thread_handle *handle,
177 enum cb_err (*func)(void *), void *arg,
178 asmlinkage void (*thread_entry)(void *), void *thread_arg)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500179{
180 /* Stash the function and argument to run. */
181 t->entry = func;
182 t->entry_arg = arg;
183
184 /* All new threads can yield by default. */
185 t->can_yield = 1;
186
Raul E Rangelcc01da52021-07-12 13:43:48 -0600187 /* Pointer used to publish the state of thread */
188 t->handle = handle;
189
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500190 arch_prepare_thread(t, thread_entry, thread_arg);
191}
192
193static void thread_resume_from_timeout(struct timeout_callback *tocb)
194{
195 struct thread *to;
196
197 to = tocb->priv;
198 schedule(to);
199}
200
201static void idle_thread_init(void)
202{
203 struct thread *t;
204
205 t = get_free_thread();
206
Lee Leahy2f919ec2017-03-08 17:37:06 -0800207 if (t == NULL)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500208 die("No threads available for idle thread!\n");
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500209
210 /* Queue idle thread to run once all other threads have yielded. */
Raul E Rangelcc01da52021-07-12 13:43:48 -0600211 prepare_thread(t, NULL, idle_thread, NULL, call_wrapper, NULL);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500212 push_runnable(t);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500213}
214
215/* Don't inline this function so the timeout_callback won't have its storage
216 * space on the stack cleaned up before the call to schedule(). */
217static int __attribute__((noinline))
Lee Leahy73402172017-03-10 15:23:24 -0800218thread_yield_timed_callback(struct timeout_callback *tocb,
219 unsigned int microsecs)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500220{
221 tocb->priv = current_thread();
222 tocb->callback = thread_resume_from_timeout;
223
224 if (timer_sched_callback(tocb, microsecs))
225 return -1;
226
227 /* The timer callback will wake up the current thread. */
228 schedule(NULL);
229 return 0;
230}
231
232static void *thread_alloc_space(struct thread *t, size_t bytes)
233{
234 /* Allocate the amount of space on the stack keeping the stack
235 * aligned to the pointer size. */
236 t->stack_current -= ALIGN_UP(bytes, sizeof(uintptr_t));
237
238 return (void *)t->stack_current;
239}
240
Raul E Rangela2d83c682021-07-22 11:16:19 -0600241static void threads_initialize(void)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500242{
243 int i;
244 struct thread *t;
Ronald G. Minnich34352d12013-08-21 16:03:32 -0700245 u8 *stack_top;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500246
Raul E Rangela2d83c682021-07-22 11:16:19 -0600247 if (initialized)
248 return;
249
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500250 t = &all_threads[0];
Raul E Rangelc2c38f52021-10-08 13:10:38 -0600251
252 set_current_thread(t);
253
Raul E Rangeldb16ac952021-09-24 14:00:56 -0600254 t->stack_orig = (uintptr_t)NULL; /* We never free the main thread */
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500255 t->id = 0;
Raul E Rangelb95369c2021-07-15 17:28:13 -0600256 t->can_yield = 1;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500257
Raul E Rangelc842c592021-09-13 14:24:55 -0600258 stack_top = &thread_stacks[CONFIG_STACK_SIZE];
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500259 for (i = 1; i < TOTAL_NUM_THREADS; i++) {
260 t = &all_threads[i];
261 t->stack_orig = (uintptr_t)stack_top;
262 t->id = i;
263 stack_top += CONFIG_STACK_SIZE;
264 free_thread(t);
265 }
266
267 idle_thread_init();
Raul E Rangelb95369c2021-07-15 17:28:13 -0600268
269 initialized = 1;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500270}
271
Raul E Rangelcc01da52021-07-12 13:43:48 -0600272int thread_run(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500273{
274 struct thread *current;
275 struct thread *t;
276
Raul E Rangela2d83c682021-07-22 11:16:19 -0600277 /* Lazy initialization */
278 threads_initialize();
279
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500280 current = current_thread();
281
282 if (!thread_can_yield(current)) {
Julius Wernere9665952022-01-21 17:06:20 -0800283 printk(BIOS_ERR, "%s() called from non-yielding context!\n", __func__);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500284 return -1;
285 }
286
287 t = get_free_thread();
288
289 if (t == NULL) {
Julius Wernere9665952022-01-21 17:06:20 -0800290 printk(BIOS_ERR, "%s: No more threads!\n", __func__);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500291 return -1;
292 }
293
Raul E Rangel4aec58d2021-07-15 13:20:58 -0600294 prepare_thread(t, handle, func, arg, call_wrapper, NULL);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500295 schedule(t);
296
297 return 0;
298}
299
Raul E Rangelcc01da52021-07-12 13:43:48 -0600300int thread_run_until(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg,
Lee Leahye20a3192017-03-09 16:21:34 -0800301 boot_state_t state, boot_state_sequence_t seq)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500302{
303 struct thread *current;
304 struct thread *t;
305 struct block_boot_state *bbs;
306
Raul E Rangel8c892072021-07-22 12:40:26 -0600307 /* This is a ramstage specific API */
308 if (!ENV_RAMSTAGE)
309 dead_code();
310
Raul E Rangela2d83c682021-07-22 11:16:19 -0600311 /* Lazy initialization */
312 threads_initialize();
313
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500314 current = current_thread();
315
316 if (!thread_can_yield(current)) {
Julius Wernere9665952022-01-21 17:06:20 -0800317 printk(BIOS_ERR, "%s() called from non-yielding context!\n", __func__);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500318 return -1;
319 }
320
321 t = get_free_thread();
322
323 if (t == NULL) {
Julius Wernere9665952022-01-21 17:06:20 -0800324 printk(BIOS_ERR, "%s: No more threads!\n", __func__);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500325 return -1;
326 }
327
328 bbs = thread_alloc_space(t, sizeof(*bbs));
329 bbs->state = state;
330 bbs->seq = seq;
Raul E Rangelcc01da52021-07-12 13:43:48 -0600331 prepare_thread(t, handle, func, arg, call_wrapper_block_state, bbs);
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500332 schedule(t);
333
334 return 0;
335}
336
Raul E Rangeld5dca212021-07-15 11:48:48 -0600337int thread_yield(void)
338{
339 return thread_yield_microseconds(0);
340}
341
Lee Leahy75b85992017-03-08 16:34:12 -0800342int thread_yield_microseconds(unsigned int microsecs)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500343{
344 struct thread *current;
345 struct timeout_callback tocb;
346
347 current = current_thread();
348
349 if (!thread_can_yield(current))
350 return -1;
351
352 if (thread_yield_timed_callback(&tocb, microsecs))
353 return -1;
354
355 return 0;
356}
357
Raul E Rangel9ba36ab2021-07-15 17:34:05 -0600358void thread_coop_enable(void)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500359{
360 struct thread *current;
361
362 current = current_thread();
363
Raul E Rangelbe60a0d2021-07-15 13:52:03 -0600364 if (current == NULL)
365 return;
366
367 assert(current->can_yield <= 0);
368
369 current->can_yield++;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500370}
371
Raul E Rangel9ba36ab2021-07-15 17:34:05 -0600372void thread_coop_disable(void)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500373{
374 struct thread *current;
375
376 current = current_thread();
377
Raul E Rangelbe60a0d2021-07-15 13:52:03 -0600378 if (current == NULL)
379 return;
380
381 current->can_yield--;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500382}
Raul E Rangelb29f9d42021-07-12 13:49:59 -0600383
Raul E Rangelcc01da52021-07-12 13:43:48 -0600384enum cb_err thread_join(struct thread_handle *handle)
385{
386 struct stopwatch sw;
387 struct thread *current = current_thread();
388
389 assert(handle);
390 assert(current);
391 assert(current->handle != handle);
392
393 if (handle->state == THREAD_UNINITIALIZED)
394 return CB_ERR_ARG;
395
Raul E Rangelcc01da52021-07-12 13:43:48 -0600396 printk(BIOS_SPEW, "waiting for thread\n");
397
Raul E Rangelfae525f2021-11-04 15:57:00 -0600398 stopwatch_init(&sw);
399
Raul E Rangelcc01da52021-07-12 13:43:48 -0600400 while (handle->state != THREAD_DONE)
401 assert(thread_yield() == 0);
402
Rob Barnesd522f382022-09-12 06:31:47 -0600403 printk(BIOS_SPEW, "took %lld us\n", stopwatch_duration_usecs(&sw));
Raul E Rangelcc01da52021-07-12 13:43:48 -0600404
405 return handle->error;
406}
407
Raul E Rangelb29f9d42021-07-12 13:49:59 -0600408void thread_mutex_lock(struct thread_mutex *mutex)
409{
410 struct stopwatch sw;
411
412 stopwatch_init(&sw);
413
414 while (mutex->locked)
415 assert(thread_yield() == 0);
416 mutex->locked = true;
417
Rob Barnesd522f382022-09-12 06:31:47 -0600418 printk(BIOS_SPEW, "took %lld us to acquire mutex\n", stopwatch_duration_usecs(&sw));
Raul E Rangelb29f9d42021-07-12 13:49:59 -0600419}
420
421void thread_mutex_unlock(struct thread_mutex *mutex)
422{
423 assert(mutex->locked);
424 mutex->locked = 0;
425}