blob: df307fa6f8b561d13feea9934d4340ffd375a446 [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
5#include <stddef.h>
6#include <stdint.h>
7#include <bootstate.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -05008#include <arch/cpu.h>
9
Kyösti Mälkkie3acc8f2019-09-13 10:49:20 +030010#if ENV_RAMSTAGE && CONFIG(COOP_MULTITASKING)
Aaron Durbin4409a5e2013-05-06 12:20:52 -050011
12struct thread {
13 int id;
14 uintptr_t stack_current;
15 uintptr_t stack_orig;
16 struct thread *next;
17 void (*entry)(void *);
18 void *entry_arg;
19 int can_yield;
20};
21
22void threads_initialize(void);
Ronald G. Minnich34352d12013-08-21 16:03:32 -070023/* Get the base of the thread stacks.
24 * Returns pointer to CONFIG_NUM_THREADS*CONFIG_STACK_SIZE contiguous bytes
25 * aligned to CONFIG_STACK_SIZE, or NULL.
26 */
27void *arch_get_thread_stackbase(void);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050028/* Run func(arrg) on a new thread. Return 0 on successful start of thread, < 0
29 * when thread could not be started. Note that the thread will block the
30 * current state in the boot state machine until it is complete. */
31int thread_run(void (*func)(void *), void *arg);
32/* thread_run_until is the same as thread_run() except that it blocks state
Martin Roth0cb07e32013-07-09 21:46:01 -060033 * transitions from occurring in the (state, seq) pair of the boot state
Aaron Durbin4409a5e2013-05-06 12:20:52 -050034 * machine. */
35int thread_run_until(void (*func)(void *), void *arg,
Lee Leahy708fc272017-03-07 12:18:53 -080036 boot_state_t state, boot_state_sequence_t seq);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050037/* Return 0 on successful yield for the given amount of time, < 0 when thread
38 * did not yield. */
Lee Leahy0ca2a062017-03-06 18:01:04 -080039int thread_yield_microseconds(unsigned int microsecs);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050040
41/* Allow and prevent thread cooperation on current running thread. By default
Martin Roth0cb07e32013-07-09 21:46:01 -060042 * all threads are marked to be cooperative. That means a thread can yield
Aaron Durbin4409a5e2013-05-06 12:20:52 -050043 * to another thread at a pre-determined switch point. Current there is
44 * only a single place where switching may occur: a call to udelay(). */
45void thread_cooperate(void);
46void thread_prevent_coop(void);
47
48static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci)
49{
50 ci->thread = NULL;
51}
52
53/* Architecture specific thread functions. */
Lee Leahy22c28e02017-03-07 15:47:44 -080054asmlinkage void switch_to_thread(uintptr_t new_stack, uintptr_t *saved_stack);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050055/* Set up the stack frame for a new thread so that a switch_to_thread() call
56 * will enter the thread_entry() function with arg as a parameter. The
57 * saved_stack field in the struct thread needs to be updated accordingly. */
58void arch_prepare_thread(struct thread *t,
Lee Leahy746d4af2017-03-07 15:31:49 -080059 asmlinkage void (*thread_entry)(void *), void *arg);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050060#else
61static inline void threads_initialize(void) {}
62static inline int thread_run(void (*func)(void *), void *arg) { return -1; }
Lee Leahy0ca2a062017-03-06 18:01:04 -080063static inline int thread_yield_microseconds(unsigned int microsecs)
64{
65 return -1;
66}
Aaron Durbin4409a5e2013-05-06 12:20:52 -050067static inline void thread_cooperate(void) {}
68static inline void thread_prevent_coop(void) {}
69struct cpu_info;
70static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) { }
71#endif
72
73#endif /* THREAD_H_ */