This repository has been archived by the owner on Mar 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libnus.c
361 lines (274 loc) · 7.37 KB
/
libnus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <libnus.h>
#include <regsinternal.h>
#include <stdbool.h>
nus_scheduler_t nus_scheduler;
extern void _nus_context_switch(nus_thread_t*, nus_thread_t*);
#define IN_INTERRUPT() (nus_c0_get_status() & 0x06)
/* Internal thread functions */
nus_thread_t *_nus_tlist_pop(nus_thread_t **start){
nus_thread_t *tp = *start;
if(tp != NULL) *start = tp->next;
return tp;
}
void _nus_tlist_insert(nus_thread_t *tp, nus_thread_t **start){
nus_thread_t **cp = start;
while(*cp != NULL && tp->prio <= (*cp)->prio){
cp = &((*cp)->next);
}
tp->next = *cp;
*cp = tp;
}
void _nus_thread_ready(nus_thread_t *tp){
tp->status = READY;
_nus_tlist_insert(tp,&nus_scheduler.ready);
}
void _nus_thread_block() {
nus_thread_t *otp = nus_scheduler.current;
nus_thread_t *ntp = _nus_tlist_pop(&nus_scheduler.ready);
ntp->status = RUNNING;
nus_scheduler.current = ntp;
_nus_context_switch(ntp, otp);
}
void _nus_thread_switch(){
nus_thread_t *otp = nus_scheduler.current;
nus_thread_t *ntp = _nus_tlist_pop(&nus_scheduler.ready);
otp->status = READY;
ntp->status = RUNNING;
nus_scheduler.current = ntp;
_nus_tlist_insert(otp,&nus_scheduler.ready);
_nus_context_switch(ntp, otp);
}
void _nus_thread_reschedule(){
if(nus_scheduler.ready && nus_scheduler.ready->prio > nus_scheduler.current->prio){
_nus_thread_switch();
}
}
/* Scheduler functions */
void nus_scheduler_init(void *irq_area, size_t irq_size){
static nus_thread_t mainthread;
nus_scheduler.current = &mainthread;
nus_scheduler.ready = NULL;
nus_scheduler.irq_sp = (intptr_t) irq_area + irq_size;
mainthread.next = NULL;
mainthread.prio = 128;
mainthread.name = "main";
mainthread.status = RUNNING;
}
void nus_scheduler_start(){
nus_scheduler.started = true;
if(nus_scheduler.current->status != SUSPENDED){
_nus_thread_reschedule();
}
else {
_nus_thread_block();
}
nus_c0_set_status(nus_c0_get_status() | 1);
}
/* Thread functions */
void nus_thread_init(nus_thread_t *tp, const char *name, nus_prio_t prio, void (*entry)(void), void *area, size_t size){
tp->next = NULL;
tp->name = name;
tp->prio = prio;
tp->status = SUSPENDED;
tp->critical_nesting = 0;
tp->sp = (nus_context_t*) (area + size - sizeof (nus_context_t));
tp->sp->ra = (intptr_t) entry;
tp->sp->status = nus_c0_get_status() | 1;
}
nus_result_t nus_thread_wakeup(nus_thread_t *tp){
nus_critical_enter();
if(tp->status == SUSPENDED){
_nus_thread_ready(tp);
if(!IN_INTERRUPT() && nus_scheduler.started){
_nus_thread_reschedule();
}
}
else {
nus_critical_exit();
return NUS_ERR_THREAD_STATUS;
}
nus_critical_exit();
return NUS_OK;
}
nus_result_t nus_thread_suspend(){
if(IN_INTERRUPT()) return NUS_ERR_CONTEXT; //nonsense in exception context
nus_critical_enter();
nus_scheduler.current->status = SUSPENDED;
if(nus_scheduler.started){
_nus_thread_block();
}
nus_critical_exit();
return NUS_OK;
}
nus_result_t nus_thread_yield(){
if(IN_INTERRUPT()) return NUS_ERR_CONTEXT; //nonsense in exception context
nus_critical_enter();
if(nus_scheduler.ready && nus_scheduler.ready->prio >= nus_scheduler.current->prio){
_nus_thread_switch();
}
nus_critical_exit();
return NUS_OK;
}
nus_prio_t nus_thread_getprio(nus_thread_t *tp){
return tp->prio;
}
nus_result_t nus_thread_setprio(nus_prio_t newprio){
if(IN_INTERRUPT()) return NUS_ERR_CONTEXT; //nonsense in exception context
nus_critical_enter();
nus_scheduler.current->prio = newprio;
if(nus_scheduler.started){
_nus_thread_reschedule();
}
nus_critical_exit();
return NUS_OK;
}
/* Queue functions */
void nus_queue_init(nus_queue_t *mq, intptr_t *array, int size){
mq->array = array;
mq->size = size;
mq->count = 0;
mq->front = 0;
mq->back = 0;
mq->blockrecv = NULL;
mq->blocksend = NULL;
}
nus_result_t nus_queue_send(nus_queue_t *mq, intptr_t msg){
nus_critical_enter();
if(mq->count == mq->size){
if(IN_INTERRUPT()){
nus_critical_exit();
return NUS_ERR_CONTEXT;
}
else {
_nus_tlist_insert(nus_scheduler.current,&(mq->blocksend));
nus_scheduler.current->status = BLOCKED;
_nus_thread_block();
}
}
nus_thread_t *tp = _nus_tlist_pop(&(mq->blockrecv));
if(tp != NULL) {
_nus_thread_ready(tp);
}
mq->array[mq->back++] = msg;
if(mq->back == mq->size) mq->back = 0;
mq->count++;
if(!IN_INTERRUPT()){
_nus_thread_reschedule();
}
nus_critical_exit();
return NUS_OK;
}
nus_result_t nus_queue_recv(nus_queue_t *mq, intptr_t *msg){
nus_critical_enter();
if(mq->count == 0){
if(IN_INTERRUPT()){
nus_critical_exit();
return NUS_ERR_CONTEXT;
}
else {
_nus_tlist_insert(nus_scheduler.current,&(mq->blockrecv));
nus_scheduler.current->status = BLOCKED;
_nus_thread_block();
}
}
nus_thread_t *tp = _nus_tlist_pop(&(mq->blocksend));
if(tp != NULL) {
_nus_thread_ready(tp);
}
*msg = mq->array[mq->front++];
if(mq->front == mq->size) mq->front = 0;
mq->count--;
if(!IN_INTERRUPT()){
_nus_thread_reschedule();
}
nus_critical_exit();
return NUS_OK;
}
/* Critical section functions */
void nus_critical_enter(){
__asm__ __volatile__(
"mfc0 $t0, $12;"
"srl $t0, $t0, 1;"
"sll $t0, $t0, 1;"
"mtc0 $t0, $12;" ::: "$t0");
nus_scheduler.current->critical_nesting++;
}
void nus_critical_exit(){
if(--nus_scheduler.current->critical_nesting == 0){
__asm__ __volatile__(
"mfc0 $t0, $12;"
"ori $t0, $t0, 1;"
"mtc0 $t0, $12;" ::: "$t0");
}
}
/* RCP interfaces */
static volatile AI_regs_t* const AI_regs = (AI_regs_t*)0xa4500000;
static volatile MI_regs_t* const MI_regs = (MI_regs_t*)0xa4300000;
static volatile VI_regs_t* const VI_regs = (VI_regs_t*)0xa4400000;
static volatile PI_regs_t* const PI_regs = (PI_regs_t*)0xa4600000;
static volatile SI_regs_t* const SI_regs = (SI_regs_t*)0xa4800000;
static volatile SP_regs_t* const SP_regs = (SP_regs_t*)0xa4040000;
/* Interrupt handling */
void NUS_ISR_sw1(void) __attribute__((weak));
void NUS_ISR_sw2(void) __attribute__((weak));
void NUS_ISR_cart(void) __attribute__((weak));
void NUS_ISR_prenmi(void) __attribute__((weak));
void NUS_ISR_count(void) __attribute__((weak));
void NUS_ISR_sp(void) __attribute__((weak));
void NUS_ISR_si(void) __attribute__((weak));
void NUS_ISR_ai(void) __attribute__((weak));
void NUS_ISR_vi(void) __attribute__((weak));
void NUS_ISR_pi(void) __attribute__((weak));
void NUS_ISR_dp(void) __attribute__((weak));
static void (*nus_isr_table[])(void);
void _nus_handle_interrupt(){
uint32_t cause = nus_c0_get_cause();
uint32_t exc_code = (cause & 0x7c) >> 2;
if(exc_code == 0){
uint32_t ip = (cause & 0xff00) >> 8;
int irq = __builtin_ctz(ip);
if(nus_isr_table[irq]) {
nus_isr_table[irq]();
}
}
else {
//TODO
}
}
void nus_handle_rcp_interrupt(){
uint32_t status = MI_regs->intr;
if(status & MI_INTR_SP) {
SP_regs->status = SP_CLEAR_INTERRUPT;
if(NUS_ISR_sp) NUS_ISR_sp();
}
if(status & MI_INTR_SI) {
SI_regs->status = SI_CLEAR_INTERRUPT;
if(NUS_ISR_si) NUS_ISR_si();
}
if(status & MI_INTR_AI) {
AI_regs->status = AI_CLEAR_INTERRUPT;
if(NUS_ISR_ai) NUS_ISR_ai();
}
if(status & MI_INTR_VI) {
VI_regs->cur_line = 0;
if(NUS_ISR_vi) NUS_ISR_vi();
}
if(status & MI_INTR_PI) {
PI_regs->status = PI_CLEAR_INTERRUPT;
if(NUS_ISR_pi) NUS_ISR_pi();
}
if(status & MI_INTR_DP) {
MI_regs->mode = DP_CLEAR_INTERRUPT;
if(NUS_ISR_dp) NUS_ISR_dp();
}
}
static void (*nus_isr_table[])(void) = {
NUS_ISR_sw1,
NUS_ISR_sw2,
nus_handle_rcp_interrupt,
NUS_ISR_cart,
NUS_ISR_prenmi,
NULL,
NULL,
NUS_ISR_count
};