blob: 62f14c2c62bfa90bb8be0390616e0a0f1c48e68a [file] [log] [blame]
Aaron Durbin4409a5e2013-05-06 12:20:52 -05001/*
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.
Aaron Durbin4409a5e2013-05-06 12:20:52 -050014 */
15#ifndef THREAD_H_
16#define THREAD_H_
17
18#include <stddef.h>
19#include <stdint.h>
20#include <bootstate.h>
21#include <timer.h>
22#include <arch/cpu.h>
23
24#if CONFIG_COOP_MULTITASKING && !defined(__SMM__) && !defined(__PRE_RAM__)
25
26struct thread {
27 int id;
28 uintptr_t stack_current;
29 uintptr_t stack_orig;
30 struct thread *next;
31 void (*entry)(void *);
32 void *entry_arg;
33 int can_yield;
34};
35
36void threads_initialize(void);
Ronald G. Minnich34352d12013-08-21 16:03:32 -070037/* Get the base of the thread stacks.
38 * Returns pointer to CONFIG_NUM_THREADS*CONFIG_STACK_SIZE contiguous bytes
39 * aligned to CONFIG_STACK_SIZE, or NULL.
40 */
41void *arch_get_thread_stackbase(void);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050042/* Run func(arrg) on a new thread. Return 0 on successful start of thread, < 0
43 * when thread could not be started. Note that the thread will block the
44 * current state in the boot state machine until it is complete. */
45int thread_run(void (*func)(void *), void *arg);
46/* thread_run_until is the same as thread_run() except that it blocks state
Martin Roth0cb07e32013-07-09 21:46:01 -060047 * transitions from occurring in the (state, seq) pair of the boot state
Aaron Durbin4409a5e2013-05-06 12:20:52 -050048 * machine. */
49int thread_run_until(void (*func)(void *), void *arg,
50 boot_state_t state, boot_state_sequence_t seq);
51/* Return 0 on successful yield for the given amount of time, < 0 when thread
52 * did not yield. */
53int thread_yield_microseconds(unsigned microsecs);
54
55/* Allow and prevent thread cooperation on current running thread. By default
Martin Roth0cb07e32013-07-09 21:46:01 -060056 * all threads are marked to be cooperative. That means a thread can yield
Aaron Durbin4409a5e2013-05-06 12:20:52 -050057 * to another thread at a pre-determined switch point. Current there is
58 * only a single place where switching may occur: a call to udelay(). */
59void thread_cooperate(void);
60void thread_prevent_coop(void);
61
62static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci)
63{
64 ci->thread = NULL;
65}
66
67/* Architecture specific thread functions. */
68void asmlinkage switch_to_thread(uintptr_t new_stack, uintptr_t *saved_stack);
69/* Set up the stack frame for a new thread so that a switch_to_thread() call
70 * will enter the thread_entry() function with arg as a parameter. The
71 * saved_stack field in the struct thread needs to be updated accordingly. */
72void arch_prepare_thread(struct thread *t,
73 void asmlinkage (*thread_entry)(void *), void *arg);
74#else
75static inline void threads_initialize(void) {}
76static inline int thread_run(void (*func)(void *), void *arg) { return -1; }
77static inline int thread_yield_microseconds(unsigned microsecs) { return -1; }
78static inline void thread_cooperate(void) {}
79static inline void thread_prevent_coop(void) {}
80struct cpu_info;
81static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) { }
82#endif
83
84#endif /* THREAD_H_ */