Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 2 | #ifndef THREAD_H_ |
| 3 | #define THREAD_H_ |
| 4 | |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | #include <bootstate.h> |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 7 | #include <arch/cpu.h> |
| 8 | |
Kyösti Mälkki | e3acc8f | 2019-09-13 10:49:20 +0300 | [diff] [blame] | 9 | #if ENV_RAMSTAGE && CONFIG(COOP_MULTITASKING) |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 10 | |
| 11 | struct thread { |
| 12 | int id; |
| 13 | uintptr_t stack_current; |
| 14 | uintptr_t stack_orig; |
| 15 | struct thread *next; |
| 16 | void (*entry)(void *); |
| 17 | void *entry_arg; |
| 18 | int can_yield; |
| 19 | }; |
| 20 | |
| 21 | void threads_initialize(void); |
Ronald G. Minnich | 34352d1 | 2013-08-21 16:03:32 -0700 | [diff] [blame] | 22 | /* Get the base of the thread stacks. |
| 23 | * Returns pointer to CONFIG_NUM_THREADS*CONFIG_STACK_SIZE contiguous bytes |
| 24 | * aligned to CONFIG_STACK_SIZE, or NULL. |
| 25 | */ |
| 26 | void *arch_get_thread_stackbase(void); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 27 | /* Run func(arrg) on a new thread. Return 0 on successful start of thread, < 0 |
| 28 | * when thread could not be started. Note that the thread will block the |
| 29 | * current state in the boot state machine until it is complete. */ |
| 30 | int thread_run(void (*func)(void *), void *arg); |
| 31 | /* thread_run_until is the same as thread_run() except that it blocks state |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 32 | * transitions from occurring in the (state, seq) pair of the boot state |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 33 | * machine. */ |
| 34 | int thread_run_until(void (*func)(void *), void *arg, |
Lee Leahy | 708fc27 | 2017-03-07 12:18:53 -0800 | [diff] [blame] | 35 | boot_state_t state, boot_state_sequence_t seq); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 36 | /* Return 0 on successful yield for the given amount of time, < 0 when thread |
| 37 | * did not yield. */ |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 38 | int thread_yield_microseconds(unsigned int microsecs); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 39 | |
| 40 | /* Allow and prevent thread cooperation on current running thread. By default |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 41 | * all threads are marked to be cooperative. That means a thread can yield |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 42 | * to another thread at a pre-determined switch point. Current there is |
| 43 | * only a single place where switching may occur: a call to udelay(). */ |
| 44 | void thread_cooperate(void); |
| 45 | void thread_prevent_coop(void); |
| 46 | |
| 47 | static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) |
| 48 | { |
| 49 | ci->thread = NULL; |
| 50 | } |
| 51 | |
| 52 | /* Architecture specific thread functions. */ |
Lee Leahy | 22c28e0 | 2017-03-07 15:47:44 -0800 | [diff] [blame] | 53 | asmlinkage void switch_to_thread(uintptr_t new_stack, uintptr_t *saved_stack); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 54 | /* Set up the stack frame for a new thread so that a switch_to_thread() call |
| 55 | * will enter the thread_entry() function with arg as a parameter. The |
| 56 | * saved_stack field in the struct thread needs to be updated accordingly. */ |
| 57 | void arch_prepare_thread(struct thread *t, |
Lee Leahy | 746d4af | 2017-03-07 15:31:49 -0800 | [diff] [blame] | 58 | asmlinkage void (*thread_entry)(void *), void *arg); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 59 | #else |
| 60 | static inline void threads_initialize(void) {} |
| 61 | static inline int thread_run(void (*func)(void *), void *arg) { return -1; } |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 62 | static inline int thread_yield_microseconds(unsigned int microsecs) |
| 63 | { |
| 64 | return -1; |
| 65 | } |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 66 | static inline void thread_cooperate(void) {} |
| 67 | static inline void thread_prevent_coop(void) {} |
| 68 | struct cpu_info; |
| 69 | static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) { } |
| 70 | #endif |
| 71 | |
| 72 | #endif /* THREAD_H_ */ |