1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Ulf Ralberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
26 /* Priority scheduling (when enabled with HAVE_PRIORITY_SCHEDULING) works
27 * by giving high priority threads more CPU time than less priority threads
30 * If software playback codec pcm buffer is going down to critical, codec
31 * can change it own priority to REALTIME to override user interface and
32 * prevent playback skipping.
34 #define PRIORITY_REALTIME 1
35 #define PRIORITY_USER_INTERFACE 4 /* The main thread */
36 #define PRIORITY_RECORDING 4 /* Recording thread */
37 #define PRIORITY_PLAYBACK 4 /* or REALTIME when needed */
38 #define PRIORITY_BUFFERING 4 /* Codec buffering thread */
39 #define PRIORITY_SYSTEM 6 /* All other firmware threads */
40 #define PRIORITY_BACKGROUND 8 /* Normal application threads */
42 #if CONFIG_CODEC == SWCODEC
48 #define DEFAULT_STACK_SIZE 0x400 /* Bytes */
51 /* Need to keep structures inside the header file because debug_menu
56 unsigned int macsr
; /* 0 - EMAC status register */
57 unsigned int d
[6]; /* 4-24 - d2-d7 */
58 unsigned int a
[5]; /* 28-44 - a2-a6 */
59 void *sp
; /* 48 - Stack pointer (a7) */
60 void *start
; /* 52 - Thread start address, or NULL when started */
61 } __attribute__((packed
));
62 #elif CONFIG_CPU == SH7034
65 unsigned int r
[7]; /* 0-24 - Registers r8 thru r14 */
66 void *sp
; /* 28 - Stack pointer (r15) */
67 void *pr
; /* 32 - Procedure register */
68 void *start
; /* 36 - Thread start address, or NULL when started */
69 } __attribute__((packed
));
70 #elif defined(CPU_ARM)
73 unsigned int r
[8]; /* 0-28 - Registers r4-r11 */
74 void *sp
; /* 32 - Stack pointer (r13) */
75 unsigned int lr
; /* 36 - r14 (lr) */
76 void *start
; /* 40 - Thread start address, or NULL when started */
77 } __attribute__((packed
));
78 #endif /* CONFIG_CPU */
82 void *t
; /* Simulator OS thread */
83 void *c
; /* Condition for blocking and sync */
84 void (*start
)(void); /* Start function */
86 #endif /* !SIMULATOR */
88 #define STATE_RUNNING 0x00000000
89 #define STATE_BLOCKED 0x20000000
90 #define STATE_SLEEPING 0x40000000
91 #define STATE_BLOCKED_W_TMO 0x60000000
93 #define THREAD_STATE_MASK 0x60000000
94 #define STATE_ARG_MASK 0x1FFFFFFF
96 #define GET_STATE_ARG(state) (state & STATE_ARG_MASK)
97 #define GET_STATE(state) (state & THREAD_STATE_MASK)
98 #define SET_STATE(var,state,arg) (var = (state | ((arg) & STATE_ARG_MASK)))
99 #define CLEAR_STATE_ARG(var) (var &= ~STATE_ARG_MASK)
101 #define STATE_BOOSTED 0x80000000
102 #define STATE_IS_BOOSTED(var) (var & STATE_BOOSTED)
103 #define SET_BOOST_STATE(var) (var |= STATE_BOOSTED)
105 struct thread_entry
{
109 unsigned long statearg
;
110 unsigned short stack_size
;
112 unsigned char core
; /* To which core threads belongs to. */
114 #ifdef HAVE_PRIORITY_SCHEDULING
115 unsigned char priority
;
116 unsigned char priority_x
;
119 struct thread_entry
*next
, *prev
;
120 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
126 struct thread_entry
*running
;
127 struct thread_entry
*sleeping
;
128 struct thread_entry
*waking
;
129 struct thread_entry
**wakeup_list
;
130 #ifdef HAVE_PRIORITY_SCHEDULING
131 long highest_priority
;
134 volatile bool lock_issued
;
135 volatile bool kernel_running
;
138 int switch_to_irq_level
;
139 #define STAY_IRQ_LEVEL -1
142 #ifdef HAVE_PRIORITY_SCHEDULING
143 #define IF_PRIO(...) __VA_ARGS__
148 /* PortalPlayer chips have 2 cores, therefore need atomic mutexes
149 * Just use it for ARM, Coldfire and whatever else well...why not?
152 /* Macros generate better code than an inline function is this case */
153 #if (defined (CPU_PP) || defined (CPU_ARM)) && CONFIG_CPU != PP5020
154 #define test_and_set(x_, v_) \
158 "swpb %[old], %[v], [%[x]] \r\n" \
160 : [v]"r"((uint32_t)v_), [x]"r"((uint32_t *)x_) \
164 #elif defined (CPU_COLDFIRE)
165 #define test_and_set(x_, v_) \
169 "bset.l %[v], (%[x]) \r\n" \
170 "sne.b %[old] \r\n" \
172 : [v]"i,d"((uint32_t)v_), [x]"a,a"((uint32_t *)x_) \
176 #elif CONFIG_CPU == SH7034
177 #define test_and_set(x_, v_) \
182 "mov #-1, %[old] \r\n" \
183 "negc %[old], %[old] \r\n" \
185 : [v]"M"((uint32_t)v_), /* Value of v_ must be 1 */ \
186 [x]"r"((uint8_t *)x_) \
191 /* default for no asm version */
192 #define test_and_set(x_, v_) \
194 uint32_t old = *(uint32_t *)x_; \
195 *(uint32_t *)x_ = v_; \
201 create_thread(void (*function
)(void), void* stack
, int stack_size
,
202 const char *name
IF_PRIO(, int priority
)
203 IF_COP(, unsigned int core
, bool fallback
));
205 #ifdef HAVE_SCHEDULER_BOOSTCTRL
206 void trigger_cpu_boost(void);
208 #define trigger_cpu_boost()
211 void remove_thread(struct thread_entry
*thread
);
212 void switch_thread(bool save_context
, struct thread_entry
**blocked_list
);
213 void sleep_thread(int ticks
);
214 void block_thread(struct thread_entry
**thread
);
215 void block_thread_w_tmo(struct thread_entry
**thread
, int timeout
);
216 void set_irq_level_and_block_thread(struct thread_entry
**thread
, int level
);
217 void set_irq_level_and_block_thread_w_tmo(struct thread_entry
**list
,
218 int timeout
, int level
);
219 void wakeup_thread(struct thread_entry
**thread
);
220 void wakeup_thread_irq_safe(struct thread_entry
**thread
);
221 #ifdef HAVE_PRIORITY_SCHEDULING
222 int thread_set_priority(struct thread_entry
*thread
, int priority
);
223 int thread_get_priority(struct thread_entry
*thread
);
224 /* Yield that guarantees thread execution once per round regardless of
225 thread's scheduler priority - basically a transient realtime boost
226 without altering the scheduler's thread precedence. */
227 void priority_yield(void);
229 #define priority_yield yield
230 #endif /* HAVE_PRIORITY_SCHEDULING */
231 struct thread_entry
* thread_get_current(void);
232 void init_threads(void);
233 int thread_stack_usage(const struct thread_entry
*thread
);
235 int idle_stack_usage(unsigned int core
);
237 int thread_get_status(const struct thread_entry
*thread
);
238 void thread_get_name(char *buffer
, int size
,
239 struct thread_entry
*thread
);
241 void profile_thread(void);