blob: 148c448bb2953c18e1058e8aff3ff5801a12bcf9 [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#ifndef THREAD_H_
20#define THREAD_H_
21
22#include <stddef.h>
23#include <stdint.h>
24#include <bootstate.h>
25#include <timer.h>
26#include <arch/cpu.h>
27
28#if CONFIG_COOP_MULTITASKING && !defined(__SMM__) && !defined(__PRE_RAM__)
29
30struct thread {
31 int id;
32 uintptr_t stack_current;
33 uintptr_t stack_orig;
34 struct thread *next;
35 void (*entry)(void *);
36 void *entry_arg;
37 int can_yield;
38};
39
40void threads_initialize(void);
41/* Run func(arrg) on a new thread. Return 0 on successful start of thread, < 0
42 * when thread could not be started. Note that the thread will block the
43 * current state in the boot state machine until it is complete. */
44int thread_run(void (*func)(void *), void *arg);
45/* thread_run_until is the same as thread_run() except that it blocks state
46 * transitions from occuring in the (state, seq) pair of the boot state
47 * machine. */
48int thread_run_until(void (*func)(void *), void *arg,
49 boot_state_t state, boot_state_sequence_t seq);
50/* Return 0 on successful yield for the given amount of time, < 0 when thread
51 * did not yield. */
52int thread_yield_microseconds(unsigned microsecs);
53
54/* Allow and prevent thread cooperation on current running thread. By default
55 * all threads are marked to be cooperative. That means a thread can yeild
56 * to another thread at a pre-determined switch point. Current there is
57 * only a single place where switching may occur: a call to udelay(). */
58void thread_cooperate(void);
59void thread_prevent_coop(void);
60
61static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci)
62{
63 ci->thread = NULL;
64}
65
66/* Architecture specific thread functions. */
67void asmlinkage switch_to_thread(uintptr_t new_stack, uintptr_t *saved_stack);
68/* Set up the stack frame for a new thread so that a switch_to_thread() call
69 * will enter the thread_entry() function with arg as a parameter. The
70 * saved_stack field in the struct thread needs to be updated accordingly. */
71void arch_prepare_thread(struct thread *t,
72 void asmlinkage (*thread_entry)(void *), void *arg);
73#else
74static inline void threads_initialize(void) {}
75static inline int thread_run(void (*func)(void *), void *arg) { return -1; }
76static inline int thread_yield_microseconds(unsigned microsecs) { return -1; }
77static inline void thread_cooperate(void) {}
78static inline void thread_prevent_coop(void) {}
79struct cpu_info;
80static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) { }
81#endif
82
83#endif /* THREAD_H_ */