blob: 54d95ff34f437b142adfa146879914e718ce8c00 [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Aaron Durbin4409a5e2013-05-06 12:20:52 -05003#ifndef THREAD_H_
4#define THREAD_H_
5
6#include <stddef.h>
7#include <stdint.h>
8#include <bootstate.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -05009#include <arch/cpu.h>
10
Kyösti Mälkkie3acc8f2019-09-13 10:49:20 +030011#if ENV_RAMSTAGE && CONFIG(COOP_MULTITASKING)
Aaron Durbin4409a5e2013-05-06 12:20:52 -050012
13struct thread {
14 int id;
15 uintptr_t stack_current;
16 uintptr_t stack_orig;
17 struct thread *next;
18 void (*entry)(void *);
19 void *entry_arg;
20 int can_yield;
21};
22
23void threads_initialize(void);
Ronald G. Minnich34352d12013-08-21 16:03:32 -070024/* Get the base of the thread stacks.
25 * Returns pointer to CONFIG_NUM_THREADS*CONFIG_STACK_SIZE contiguous bytes
26 * aligned to CONFIG_STACK_SIZE, or NULL.
27 */
28void *arch_get_thread_stackbase(void);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050029/* Run func(arrg) on a new thread. Return 0 on successful start of thread, < 0
30 * when thread could not be started. Note that the thread will block the
31 * current state in the boot state machine until it is complete. */
32int thread_run(void (*func)(void *), void *arg);
33/* thread_run_until is the same as thread_run() except that it blocks state
Martin Roth0cb07e32013-07-09 21:46:01 -060034 * transitions from occurring in the (state, seq) pair of the boot state
Aaron Durbin4409a5e2013-05-06 12:20:52 -050035 * machine. */
36int thread_run_until(void (*func)(void *), void *arg,
Lee Leahy708fc272017-03-07 12:18:53 -080037 boot_state_t state, boot_state_sequence_t seq);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050038/* Return 0 on successful yield for the given amount of time, < 0 when thread
39 * did not yield. */
Lee Leahy0ca2a062017-03-06 18:01:04 -080040int thread_yield_microseconds(unsigned int microsecs);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050041
42/* Allow and prevent thread cooperation on current running thread. By default
Martin Roth0cb07e32013-07-09 21:46:01 -060043 * all threads are marked to be cooperative. That means a thread can yield
Aaron Durbin4409a5e2013-05-06 12:20:52 -050044 * to another thread at a pre-determined switch point. Current there is
45 * only a single place where switching may occur: a call to udelay(). */
46void thread_cooperate(void);
47void thread_prevent_coop(void);
48
49static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci)
50{
51 ci->thread = NULL;
52}
53
54/* Architecture specific thread functions. */
Lee Leahy22c28e02017-03-07 15:47:44 -080055asmlinkage void switch_to_thread(uintptr_t new_stack, uintptr_t *saved_stack);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050056/* Set up the stack frame for a new thread so that a switch_to_thread() call
57 * will enter the thread_entry() function with arg as a parameter. The
58 * saved_stack field in the struct thread needs to be updated accordingly. */
59void arch_prepare_thread(struct thread *t,
Lee Leahy746d4af2017-03-07 15:31:49 -080060 asmlinkage void (*thread_entry)(void *), void *arg);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050061#else
62static inline void threads_initialize(void) {}
63static inline int thread_run(void (*func)(void *), void *arg) { return -1; }
Lee Leahy0ca2a062017-03-06 18:01:04 -080064static inline int thread_yield_microseconds(unsigned int microsecs)
65{
66 return -1;
67}
Aaron Durbin4409a5e2013-05-06 12:20:52 -050068static inline void thread_cooperate(void) {}
69static inline void thread_prevent_coop(void) {}
70struct cpu_info;
71static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) { }
72#endif
73
74#endif /* THREAD_H_ */