blob: 3ddf82f05b0cc82ed68f81f387a3c465b40c0353 [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
46/* Assumes current cpu info can switch. */
47static 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();
124 while (1) {
125 timers_run();
126 }
127}
128
129static void schedule(struct thread *t)
130{
131 struct thread *current = current_thread();
132
133 /* If t is NULL need to find new runnable thread. */
134 if (t == NULL) {
135 if (thread_list_empty(&runnable_threads))
136 die("Runnable thread list is empty!\n");
137 t = pop_runnable();
138 } else {
139 /* current is still runnable. */
140 push_runnable(current);
141 }
142 switch_to_thread(t->stack_current, &current->stack_current);
143}
144
145static void terminate_thread(struct thread *t)
146{
147 free_thread(t);
148 schedule(NULL);
149}
150
151static void asmlinkage call_wrapper(void *unused)
152{
153 struct thread *current = current_thread();
154
155 current->entry(current->entry_arg);
156 terminate_thread(current);
157}
158
159/* Block the current state transitions until thread is complete. */
160static void asmlinkage call_wrapper_block_current(void *unused)
161{
162 struct thread *current = current_thread();
163
164 boot_state_current_block();
165 current->entry(current->entry_arg);
166 boot_state_current_unblock();
167 terminate_thread(current);
168}
169
170struct block_boot_state {
171 boot_state_t state;
172 boot_state_sequence_t seq;
173};
174
175/* Block the provided state until thread is complete. */
176static void asmlinkage call_wrapper_block_state(void *arg)
177{
178 struct block_boot_state *bbs = arg;
179 struct thread *current = current_thread();
180
181 boot_state_block(bbs->state, bbs->seq);
182 current->entry(current->entry_arg);
183 boot_state_unblock(bbs->state, bbs->seq);
184 terminate_thread(current);
185}
186
187/* Prepare a thread so that it starts by executing thread_entry(thread_arg).
188 * Within thread_entry() it will call func(arg). */
189static void prepare_thread(struct thread *t, void *func, void *arg,
190 void asmlinkage (*thread_entry)(void *),
191 void *thread_arg)
192{
193 /* Stash the function and argument to run. */
194 t->entry = func;
195 t->entry_arg = arg;
196
197 /* All new threads can yield by default. */
198 t->can_yield = 1;
199
200 arch_prepare_thread(t, thread_entry, thread_arg);
201}
202
203static void thread_resume_from_timeout(struct timeout_callback *tocb)
204{
205 struct thread *to;
206
207 to = tocb->priv;
208 schedule(to);
209}
210
211static void idle_thread_init(void)
212{
213 struct thread *t;
214
215 t = get_free_thread();
216
217 if (t == NULL) {
218 die("No threads available for idle thread!\n");
219 }
220
221 /* Queue idle thread to run once all other threads have yielded. */
222 prepare_thread(t, idle_thread, NULL, call_wrapper, NULL);
223 push_runnable(t);
224 /* Mark the currently executing thread to cooperate. */
225 thread_cooperate();
226}
227
228/* Don't inline this function so the timeout_callback won't have its storage
229 * space on the stack cleaned up before the call to schedule(). */
230static int __attribute__((noinline))
231thread_yield_timed_callback(struct timeout_callback *tocb, unsigned microsecs)
232{
233 tocb->priv = current_thread();
234 tocb->callback = thread_resume_from_timeout;
235
236 if (timer_sched_callback(tocb, microsecs))
237 return -1;
238
239 /* The timer callback will wake up the current thread. */
240 schedule(NULL);
241 return 0;
242}
243
244static void *thread_alloc_space(struct thread *t, size_t bytes)
245{
246 /* Allocate the amount of space on the stack keeping the stack
247 * aligned to the pointer size. */
248 t->stack_current -= ALIGN_UP(bytes, sizeof(uintptr_t));
249
250 return (void *)t->stack_current;
251}
252
253void threads_initialize(void)
254{
255 int i;
256 struct thread *t;
Ronald G. Minnich34352d12013-08-21 16:03:32 -0700257 u8 *stack_top;
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500258 struct cpu_info *ci;
Ronald G. Minnich34352d12013-08-21 16:03:32 -0700259 u8 *thread_stacks;
260
261 thread_stacks = arch_get_thread_stackbase();
Aaron Durbin4409a5e2013-05-06 12:20:52 -0500262
263 /* Initialize the BSP thread first. The cpu_info structure is assumed
264 * to be just under the top of the stack. */
265 t = &all_threads[0];
266 ci = cpu_info();
267 ci->thread = t;
268 t->stack_orig = (uintptr_t)ci;
269 t->id = 0;
270
271 stack_top = &thread_stacks[CONFIG_STACK_SIZE] - sizeof(struct cpu_info);
272 for (i = 1; i < TOTAL_NUM_THREADS; i++) {
273 t = &all_threads[i];
274 t->stack_orig = (uintptr_t)stack_top;
275 t->id = i;
276 stack_top += CONFIG_STACK_SIZE;
277 free_thread(t);
278 }
279
280 idle_thread_init();
281}
282
283int thread_run(void (*func)(void *), void *arg)
284{
285 struct thread *current;
286 struct thread *t;
287
288 current = current_thread();
289
290 if (!thread_can_yield(current)) {
291 printk(BIOS_ERR,
292 "thread_run() called from non-yielding context!\n");
293 return -1;
294 }
295
296 t = get_free_thread();
297
298 if (t == NULL) {
299 printk(BIOS_ERR, "thread_run() No more threads!\n");
300 return -1;
301 }
302
303 prepare_thread(t, func, arg, call_wrapper_block_current, NULL);
304 schedule(t);
305
306 return 0;
307}
308
309int thread_run_until(void (*func)(void *), void *arg,
310 boot_state_t state, boot_state_sequence_t seq)
311{
312 struct thread *current;
313 struct thread *t;
314 struct block_boot_state *bbs;
315
316 current = current_thread();
317
318 if (!thread_can_yield(current)) {
319 printk(BIOS_ERR,
320 "thread_run() called from non-yielding context!\n");
321 return -1;
322 }
323
324 t = get_free_thread();
325
326 if (t == NULL) {
327 printk(BIOS_ERR, "thread_run() No more threads!\n");
328 return -1;
329 }
330
331 bbs = thread_alloc_space(t, sizeof(*bbs));
332 bbs->state = state;
333 bbs->seq = seq;
334 prepare_thread(t, func, arg, call_wrapper_block_state, bbs);
335 schedule(t);
336
337 return 0;
338}
339
340int thread_yield_microseconds(unsigned microsecs)
341{
342 struct thread *current;
343 struct timeout_callback tocb;
344
345 current = current_thread();
346
347 if (!thread_can_yield(current))
348 return -1;
349
350 if (thread_yield_timed_callback(&tocb, microsecs))
351 return -1;
352
353 return 0;
354}
355
356void thread_cooperate(void)
357{
358 struct thread *current;
359
360 current = current_thread();
361
362 if (current != NULL)
363 current->can_yield = 1;
364}
365
366void thread_prevent_coop(void)
367{
368 struct thread *current;
369
370 current = current_thread();
371
372 if (current != NULL)
373 current->can_yield = 0;
374}