blob: 57a0f8c1858d0cd2702cd886192884dd7c115fb1 [file] [log] [blame]
Aaron Durbin4409a5e2013-05-06 12:20:52 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Aaron Durbin4409a5e2013-05-06 12:20:52 -050014 */
15#include <stddef.h>
16#include <stdint.h>
17#include <stdlib.h>
18#include <arch/cpu.h>
19#include <bootstate.h>
20#include <console/console.h>
21#include <thread.h>
22
23static void idle_thread_init(void);
24
25/* There needs to be at least one thread to run the ramstate state machine. */
26#define TOTAL_NUM_THREADS (CONFIG_NUM_THREADS + 1)
Aaron Durbin4409a5e2013-05-06 12:20:52 -050027
28/* Storage space for the thread structs .*/
29static struct thread all_threads[TOTAL_NUM_THREADS];
30
31/* All runnable (but not running) and free threads are kept on their
32 * respective lists. */
33static struct thread *runnable_threads;
34static struct thread *free_threads;
35
36static inline struct cpu_info *thread_cpu_info(const struct thread *t)
37{
38 return (void *)(t->stack_orig);
39}
40
41static inline int thread_can_yield(const struct thread *t)
42{
43 return (t != NULL && t->can_yield);
44}
45
Elyes HAOUAS91e0e3c2016-07-30 15:51:13 +020046/* Assumes current CPU info can switch. */
Aaron Durbin4409a5e2013-05-06 12:20:52 -050047static inline struct thread *cpu_info_to_thread(const struct cpu_info *ci)
48{
49 return ci->thread;
50}
51
52static inline struct thread *current_thread(void)
53{
54 return cpu_info_to_thread(cpu_info());
55}
56
57static inline int thread_list_empty(struct thread **list)
58{
59 return *list == NULL;
60}
61
62static inline struct thread *pop_thread(struct thread **list)
63{
64 struct thread *t;
65
66 t = *list;
67 *list = t->next;
68 t->next = NULL;
69 return t;
70}
71
72static inline void push_thread(struct thread **list, struct thread *t)
73{
74 t->next = *list;
75 *list = t;
76}
77
78static inline void push_runnable(struct thread *t)
79{
80 push_thread(&runnable_threads, t);
81}
82
83static inline struct thread *pop_runnable(void)
84{
85 return pop_thread(&runnable_threads);
86}
87
88static inline struct thread *get_free_thread(void)
89{
90 struct thread *t;
91 struct cpu_info *ci;
92 struct cpu_info *new_ci;
93
94 if (thread_list_empty(&free_threads))
95 return NULL;
96
97 t = pop_thread(&free_threads);
98
99 ci = cpu_info();
100
101 /* Initialize the cpu_info structure on the new stack. */
102 new_ci = thread_cpu_info(t);
103 *new_ci = *ci;
104 new_ci->thread = t;
105
106 /* Reset the current stack value to the original. */
107 t->stack_current = t->stack_orig;
108
109 return t;
110}
111
112static inline void free_thread(struct thread *t)
113{
114 push_thread(&free_threads, t);
115}
116
117/* The idle thread is ran whenever there isn't anything else that is runnable.
118 * It's sole responsibility is to ensure progress is made by running the timer
119 * callbacks. */
120static void idle_thread(void *unused)
121{
122 /* This thread never voluntarily yields. */
123 thread_prevent_coop();
Lee Leahy2f919ec2017-03-08 17:37:06 -0800124 while (1)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500125 timers_run();
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500126}
127
128static void schedule(struct thread *t)
129{
130 struct thread *current = current_thread();
131
132 /* If t is NULL need to find new runnable thread. */
133 if (t == NULL) {
134 if (thread_list_empty(&runnable_threads))
135 die("Runnable thread list is empty!\n");
136 t = pop_runnable();
137 } else {
138 /* current is still runnable. */
139 push_runnable(current);
140 }
141 switch_to_thread(t->stack_current, &current->stack_current);
142}
143
144static void terminate_thread(struct thread *t)
145{
146 free_thread(t);
147 schedule(NULL);
148}
149
150static void asmlinkage call_wrapper(void *unused)
151{
152 struct thread *current = current_thread();
153
154 current->entry(current->entry_arg);
155 terminate_thread(current);
156}
157
158/* Block the current state transitions until thread is complete. */
159static void asmlinkage call_wrapper_block_current(void *unused)
160{
161 struct thread *current = current_thread();
162
163 boot_state_current_block();
164 current->entry(current->entry_arg);
165 boot_state_current_unblock();
166 terminate_thread(current);
167}
168
169struct block_boot_state {
170 boot_state_t state;
171 boot_state_sequence_t seq;
172};
173
174/* Block the provided state until thread is complete. */
175static void asmlinkage call_wrapper_block_state(void *arg)
176{
177 struct block_boot_state *bbs = arg;
178 struct thread *current = current_thread();
179
180 boot_state_block(bbs->state, bbs->seq);
181 current->entry(current->entry_arg);
182 boot_state_unblock(bbs->state, bbs->seq);
183 terminate_thread(current);
184}
185
186/* Prepare a thread so that it starts by executing thread_entry(thread_arg).
187 * Within thread_entry() it will call func(arg). */
188static void prepare_thread(struct thread *t, void *func, void *arg,
Lee Leahy35af5c42017-03-09 17:35:28 -0800189 asmlinkage void (*thread_entry)(void *),
Lee Leahye20a3192017-03-09 16:21:34 -0800190 void *thread_arg)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500191{
192 /* Stash the function and argument to run. */
193 t->entry = func;
194 t->entry_arg = arg;
195
196 /* All new threads can yield by default. */
197 t->can_yield = 1;
198
199 arch_prepare_thread(t, thread_entry, thread_arg);
200}
201
202static void thread_resume_from_timeout(struct timeout_callback *tocb)
203{
204 struct thread *to;
205
206 to = tocb->priv;
207 schedule(to);
208}
209
210static void idle_thread_init(void)
211{
212 struct thread *t;
213
214 t = get_free_thread();
215
Lee Leahy2f919ec2017-03-08 17:37:06 -0800216 if (t == NULL)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500217 die("No threads available for idle thread!\n");
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500218
219 /* Queue idle thread to run once all other threads have yielded. */
220 prepare_thread(t, idle_thread, NULL, call_wrapper, NULL);
221 push_runnable(t);
222 /* Mark the currently executing thread to cooperate. */
223 thread_cooperate();
224}
225
226/* Don't inline this function so the timeout_callback won't have its storage
227 * space on the stack cleaned up before the call to schedule(). */
228static int __attribute__((noinline))
Lee Leahy73402172017-03-10 15:23:24 -0800229thread_yield_timed_callback(struct timeout_callback *tocb,
230 unsigned int microsecs)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500231{
232 tocb->priv = current_thread();
233 tocb->callback = thread_resume_from_timeout;
234
235 if (timer_sched_callback(tocb, microsecs))
236 return -1;
237
238 /* The timer callback will wake up the current thread. */
239 schedule(NULL);
240 return 0;
241}
242
243static void *thread_alloc_space(struct thread *t, size_t bytes)
244{
245 /* Allocate the amount of space on the stack keeping the stack
246 * aligned to the pointer size. */
247 t->stack_current -= ALIGN_UP(bytes, sizeof(uintptr_t));
248
249 return (void *)t->stack_current;
250}
251
252void threads_initialize(void)
253{
254 int i;
255 struct thread *t;
Ronald G. Minnich34352d12013-08-21 16:03:32 -0700256 u8 *stack_top;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500257 struct cpu_info *ci;
Ronald G. Minnich34352d12013-08-21 16:03:32 -0700258 u8 *thread_stacks;
259
260 thread_stacks = arch_get_thread_stackbase();
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500261
262 /* Initialize the BSP thread first. The cpu_info structure is assumed
263 * to be just under the top of the stack. */
264 t = &all_threads[0];
265 ci = cpu_info();
266 ci->thread = t;
267 t->stack_orig = (uintptr_t)ci;
268 t->id = 0;
269
270 stack_top = &thread_stacks[CONFIG_STACK_SIZE] - sizeof(struct cpu_info);
271 for (i = 1; i < TOTAL_NUM_THREADS; i++) {
272 t = &all_threads[i];
273 t->stack_orig = (uintptr_t)stack_top;
274 t->id = i;
275 stack_top += CONFIG_STACK_SIZE;
276 free_thread(t);
277 }
278
279 idle_thread_init();
280}
281
282int thread_run(void (*func)(void *), void *arg)
283{
284 struct thread *current;
285 struct thread *t;
286
287 current = current_thread();
288
289 if (!thread_can_yield(current)) {
290 printk(BIOS_ERR,
291 "thread_run() called from non-yielding context!\n");
292 return -1;
293 }
294
295 t = get_free_thread();
296
297 if (t == NULL) {
298 printk(BIOS_ERR, "thread_run() No more threads!\n");
299 return -1;
300 }
301
302 prepare_thread(t, func, arg, call_wrapper_block_current, NULL);
303 schedule(t);
304
305 return 0;
306}
307
308int thread_run_until(void (*func)(void *), void *arg,
Lee Leahye20a3192017-03-09 16:21:34 -0800309 boot_state_t state, boot_state_sequence_t seq)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500310{
311 struct thread *current;
312 struct thread *t;
313 struct block_boot_state *bbs;
314
315 current = current_thread();
316
317 if (!thread_can_yield(current)) {
318 printk(BIOS_ERR,
319 "thread_run() called from non-yielding context!\n");
320 return -1;
321 }
322
323 t = get_free_thread();
324
325 if (t == NULL) {
326 printk(BIOS_ERR, "thread_run() No more threads!\n");
327 return -1;
328 }
329
330 bbs = thread_alloc_space(t, sizeof(*bbs));
331 bbs->state = state;
332 bbs->seq = seq;
333 prepare_thread(t, func, arg, call_wrapper_block_state, bbs);
334 schedule(t);
335
336 return 0;
337}
338
Lee Leahy75b85992017-03-08 16:34:12 -0800339int thread_yield_microseconds(unsigned int microsecs)
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500340{
341 struct thread *current;
342 struct timeout_callback tocb;
343
344 current = current_thread();
345
346 if (!thread_can_yield(current))
347 return -1;
348
349 if (thread_yield_timed_callback(&tocb, microsecs))
350 return -1;
351
352 return 0;
353}
354
355void thread_cooperate(void)
356{
357 struct thread *current;
358
359 current = current_thread();
360
361 if (current != NULL)
362 current->can_yield = 1;
363}
364
365void thread_prevent_coop(void)
366{
367 struct thread *current;
368
369 current = current_thread();
370
371 if (current != NULL)
372 current->can_yield = 0;
373}