Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 30 | struct 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 | |
| 40 | void 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. */ |
| 44 | int 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. */ |
| 48 | int 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. */ |
| 52 | int 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(). */ |
| 58 | void thread_cooperate(void); |
| 59 | void thread_prevent_coop(void); |
| 60 | |
| 61 | static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) |
| 62 | { |
| 63 | ci->thread = NULL; |
| 64 | } |
| 65 | |
| 66 | /* Architecture specific thread functions. */ |
| 67 | void 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. */ |
| 71 | void arch_prepare_thread(struct thread *t, |
| 72 | void asmlinkage (*thread_entry)(void *), void *arg); |
| 73 | #else |
| 74 | static inline void threads_initialize(void) {} |
| 75 | static inline int thread_run(void (*func)(void *), void *arg) { return -1; } |
| 76 | static inline int thread_yield_microseconds(unsigned microsecs) { return -1; } |
| 77 | static inline void thread_cooperate(void) {} |
| 78 | static inline void thread_prevent_coop(void) {} |
| 79 | struct cpu_info; |
| 80 | static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) { } |
| 81 | #endif |
| 82 | |
| 83 | #endif /* THREAD_H_ */ |