Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 1 | /* |
| 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 Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 14 | */ |
| 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 | |
Martin Roth | 9634547 | 2017-06-24 14:13:53 -0600 | [diff] [blame] | 24 | #if IS_ENABLED(CONFIG_COOP_MULTITASKING) && !defined(__SMM__) && !defined(__PRE_RAM__) |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 25 | |
| 26 | struct 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 | |
| 36 | void threads_initialize(void); |
Ronald G. Minnich | 34352d1 | 2013-08-21 16:03:32 -0700 | [diff] [blame] | 37 | /* 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 | */ |
| 41 | void *arch_get_thread_stackbase(void); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 42 | /* 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. */ |
| 45 | int thread_run(void (*func)(void *), void *arg); |
| 46 | /* 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] | 47 | * transitions from occurring in the (state, seq) pair of the boot state |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 48 | * machine. */ |
| 49 | int thread_run_until(void (*func)(void *), void *arg, |
Lee Leahy | 708fc27 | 2017-03-07 12:18:53 -0800 | [diff] [blame] | 50 | boot_state_t state, boot_state_sequence_t seq); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 51 | /* Return 0 on successful yield for the given amount of time, < 0 when thread |
| 52 | * did not yield. */ |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 53 | int thread_yield_microseconds(unsigned int microsecs); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 54 | |
| 55 | /* Allow and prevent thread cooperation on current running thread. By default |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 56 | * 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] | 57 | * 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(). */ |
| 59 | void thread_cooperate(void); |
| 60 | void thread_prevent_coop(void); |
| 61 | |
| 62 | static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) |
| 63 | { |
| 64 | ci->thread = NULL; |
| 65 | } |
| 66 | |
| 67 | /* Architecture specific thread functions. */ |
Lee Leahy | 22c28e0 | 2017-03-07 15:47:44 -0800 | [diff] [blame] | 68 | 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] | 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. */ |
| 72 | void arch_prepare_thread(struct thread *t, |
Lee Leahy | 746d4af | 2017-03-07 15:31:49 -0800 | [diff] [blame] | 73 | asmlinkage void (*thread_entry)(void *), void *arg); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 74 | #else |
| 75 | static inline void threads_initialize(void) {} |
| 76 | static inline int thread_run(void (*func)(void *), void *arg) { return -1; } |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 77 | static inline int thread_yield_microseconds(unsigned int microsecs) |
| 78 | { |
| 79 | return -1; |
| 80 | } |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 81 | static inline void thread_cooperate(void) {} |
| 82 | static inline void thread_prevent_coop(void) {} |
| 83 | struct cpu_info; |
| 84 | static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) { } |
| 85 | #endif |
| 86 | |
| 87 | #endif /* THREAD_H_ */ |