blob: a2c7ed208512b21c56b21e6ac876e86f8f893ffd [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin4409a5e2013-05-06 12:20:52 -05002#ifndef THREAD_H_
3#define THREAD_H_
4
Aaron Durbin4409a5e2013-05-06 12:20:52 -05005#include <stdint.h>
6#include <bootstate.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -05007#include <arch/cpu.h>
8
Kyösti Mälkkie3acc8f2019-09-13 10:49:20 +03009#if ENV_RAMSTAGE && CONFIG(COOP_MULTITASKING)
Aaron Durbin4409a5e2013-05-06 12:20:52 -050010
11struct 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
21void threads_initialize(void);
Ronald G. Minnich34352d12013-08-21 16:03:32 -070022/* 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 */
26void *arch_get_thread_stackbase(void);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050027/* 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. */
30int thread_run(void (*func)(void *), void *arg);
31/* thread_run_until is the same as thread_run() except that it blocks state
Martin Roth0cb07e32013-07-09 21:46:01 -060032 * transitions from occurring in the (state, seq) pair of the boot state
Aaron Durbin4409a5e2013-05-06 12:20:52 -050033 * machine. */
34int thread_run_until(void (*func)(void *), void *arg,
Lee Leahy708fc272017-03-07 12:18:53 -080035 boot_state_t state, boot_state_sequence_t seq);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050036/* Return 0 on successful yield for the given amount of time, < 0 when thread
37 * did not yield. */
Lee Leahy0ca2a062017-03-06 18:01:04 -080038int thread_yield_microseconds(unsigned int microsecs);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050039
40/* Allow and prevent thread cooperation on current running thread. By default
Martin Roth0cb07e32013-07-09 21:46:01 -060041 * all threads are marked to be cooperative. That means a thread can yield
Aaron Durbin4409a5e2013-05-06 12:20:52 -050042 * 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(). */
44void thread_cooperate(void);
45void thread_prevent_coop(void);
46
47static 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 Leahy22c28e02017-03-07 15:47:44 -080053asmlinkage void switch_to_thread(uintptr_t new_stack, uintptr_t *saved_stack);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050054/* 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. */
57void arch_prepare_thread(struct thread *t,
Lee Leahy746d4af2017-03-07 15:31:49 -080058 asmlinkage void (*thread_entry)(void *), void *arg);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050059#else
60static inline void threads_initialize(void) {}
61static inline int thread_run(void (*func)(void *), void *arg) { return -1; }
Lee Leahy0ca2a062017-03-06 18:01:04 -080062static inline int thread_yield_microseconds(unsigned int microsecs)
63{
64 return -1;
65}
Aaron Durbin4409a5e2013-05-06 12:20:52 -050066static inline void thread_cooperate(void) {}
67static inline void thread_prevent_coop(void) {}
68struct cpu_info;
69static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) { }
70#endif
71
72#endif /* THREAD_H_ */