Disable low battery shutdown for the time being on iPod 3g until the battery reading...
[Rockbox.git] / firmware / kernel.c
blob803c2246409ce73d90964b8f593ec900e05ab0d6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Björn Stenberg
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 ****************************************************************************/
19 #include <stdlib.h>
20 #include <string.h>
21 #include "config.h"
22 #include "kernel.h"
23 #include "thread.h"
24 #include "cpu.h"
25 #include "system.h"
26 #include "panic.h"
27 #if CONFIG_CPU == IMX31L
28 #include "avic-imx31.h"
29 #endif
31 /* Make this nonzero to enable more elaborate checks on objects */
32 #ifdef DEBUG
33 #define KERNEL_OBJECT_CHECKS 1 /* Always 1 for DEBUG */
34 #else
35 #define KERNEL_OBJECT_CHECKS 0
36 #endif
38 #if KERNEL_OBJECT_CHECKS
39 #define KERNEL_ASSERT(exp, msg...) \
40 ({ if (!({ exp; })) panicf(msg); })
41 #else
42 #define KERNEL_ASSERT(exp, msg...) ({})
43 #endif
45 #if (!defined(CPU_PP) && (CONFIG_CPU != IMX31L)) || !defined(BOOTLOADER)
46 volatile long current_tick NOCACHEDATA_ATTR = 0;
47 #endif
49 void (*tick_funcs[MAX_NUM_TICK_TASKS])(void);
51 extern struct core_entry cores[NUM_CORES];
53 /* This array holds all queues that are initiated. It is used for broadcast. */
54 static struct
56 int count;
57 struct event_queue *queues[MAX_NUM_QUEUES];
58 #if NUM_CORES > 1
59 struct corelock cl;
60 #endif
61 } all_queues NOCACHEBSS_ATTR;
63 /****************************************************************************
64 * Standard kernel stuff
65 ****************************************************************************/
66 void kernel_init(void)
68 /* Init the threading API */
69 init_threads();
71 /* Other processors will not reach this point in a multicore build.
72 * In a single-core build with multiple cores they fall-through and
73 * sleep in cop_main without returning. */
74 if (CURRENT_CORE == CPU)
76 memset(tick_funcs, 0, sizeof(tick_funcs));
77 memset(&all_queues, 0, sizeof(all_queues));
78 corelock_init(&all_queues.cl);
79 tick_start(1000/HZ);
83 void sleep(int ticks)
85 #if CONFIG_CPU == S3C2440 && defined(BOOTLOADER)
86 volatile int counter;
87 TCON &= ~(1 << 20); // stop timer 4
88 // TODO: this constant depends on dividers settings inherited from
89 // firmware. Set them explicitly somwhere.
90 TCNTB4 = 12193 * ticks / HZ;
91 TCON |= 1 << 21; // set manual bit
92 TCON &= ~(1 << 21); // reset manual bit
93 TCON &= ~(1 << 22); //autoreload Off
94 TCON |= (1 << 20); // start timer 4
95 do {
96 counter = TCNTO4;
97 } while(counter > 0);
99 #elif defined(CPU_PP) && defined(BOOTLOADER)
100 unsigned stop = USEC_TIMER + ticks * (1000000/HZ);
101 while (TIME_BEFORE(USEC_TIMER, stop))
102 switch_thread(NULL);
103 #else
104 sleep_thread(ticks);
105 #endif
108 void yield(void)
110 #if ((CONFIG_CPU == S3C2440 || defined(ELIO_TPJ1022) || CONFIG_CPU == IMX31L) && defined(BOOTLOADER))
111 /* Some targets don't like yielding in the bootloader */
112 #else
113 switch_thread(NULL);
114 #endif
117 /****************************************************************************
118 * Queue handling stuff
119 ****************************************************************************/
121 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
122 /* Moves waiting thread's descriptor to the current sender when a
123 message is dequeued */
124 static void queue_fetch_sender(struct queue_sender_list *send,
125 unsigned int i)
127 struct thread_entry **spp = &send->senders[i];
129 if(*spp)
131 send->curr_sender = *spp;
132 *spp = NULL;
136 /* Puts the specified return value in the waiting thread's return value
137 * and wakes the thread.
138 * 1) A sender should be confirmed to exist before calling which makes it
139 * more efficent to reject the majority of cases that don't need this
140 called.
141 * 2) Requires interrupts disabled since queue overflows can cause posts
142 * from interrupt handlers to wake threads. Not doing so could cause
143 * an attempt at multiple wakes or other problems.
145 static void queue_release_sender(struct thread_entry **sender,
146 intptr_t retval)
148 (*sender)->retval = retval;
149 wakeup_thread_no_listlock(sender);
150 /* This should _never_ happen - there must never be multiple
151 threads in this list and it is a corrupt state */
152 KERNEL_ASSERT(*sender == NULL, "queue->send slot ovf: %08X", (int)*sender);
155 /* Releases any waiting threads that are queued with queue_send -
156 * reply with 0.
157 * Disable IRQs and lock before calling since it uses
158 * queue_release_sender.
160 static void queue_release_all_senders(struct event_queue *q)
162 if(q->send)
164 unsigned int i;
165 for(i = q->read; i != q->write; i++)
167 struct thread_entry **spp =
168 &q->send->senders[i & QUEUE_LENGTH_MASK];
170 if(*spp)
172 queue_release_sender(spp, 0);
178 /* Enables queue_send on the specified queue - caller allocates the extra
179 data structure. Only queues which are taken to be owned by a thread should
180 enable this. Public waiting is not permitted. */
181 void queue_enable_queue_send(struct event_queue *q,
182 struct queue_sender_list *send)
184 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
185 corelock_lock(&q->cl);
187 q->send = NULL;
188 if(send != NULL)
190 memset(send, 0, sizeof(*send));
191 q->send = send;
194 corelock_unlock(&q->cl);
195 set_irq_level(oldlevel);
197 #endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
199 /* Queue must not be available for use during this call */
200 void queue_init(struct event_queue *q, bool register_queue)
202 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
204 if(register_queue)
206 corelock_lock(&all_queues.cl);
209 corelock_init(&q->cl);
210 thread_queue_init(&q->queue);
211 q->read = 0;
212 q->write = 0;
213 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
214 q->send = NULL; /* No message sending by default */
215 #endif
217 if(register_queue)
219 if(all_queues.count >= MAX_NUM_QUEUES)
221 panicf("queue_init->out of queues");
223 /* Add it to the all_queues array */
224 all_queues.queues[all_queues.count++] = q;
225 corelock_unlock(&all_queues.cl);
228 set_irq_level(oldlevel);
231 /* Queue must not be available for use during this call */
232 void queue_delete(struct event_queue *q)
234 int oldlevel;
235 int i;
237 oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
238 corelock_lock(&all_queues.cl);
239 corelock_lock(&q->cl);
241 /* Find the queue to be deleted */
242 for(i = 0;i < all_queues.count;i++)
244 if(all_queues.queues[i] == q)
246 /* Move the following queues up in the list */
247 all_queues.count--;
249 for(;i < all_queues.count;i++)
251 all_queues.queues[i] = all_queues.queues[i+1];
254 break;
258 corelock_unlock(&all_queues.cl);
260 /* Release threads waiting on queue head */
261 thread_queue_wake(&q->queue);
263 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
264 /* Release waiting threads for reply and reply to any dequeued
265 message waiting for one. */
266 queue_release_all_senders(q);
267 queue_reply(q, 0);
268 #endif
270 q->read = 0;
271 q->write = 0;
273 corelock_unlock(&q->cl);
274 set_irq_level(oldlevel);
277 /* NOTE: multiple threads waiting on a queue head cannot have a well-
278 defined release order if timeouts are used. If multiple threads must
279 access the queue head, use a dispatcher or queue_wait only. */
280 void queue_wait(struct event_queue *q, struct queue_event *ev)
282 int oldlevel;
283 unsigned int rd;
285 oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
286 corelock_lock(&q->cl);
288 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
289 if(q->send && q->send->curr_sender)
291 /* auto-reply */
292 queue_release_sender(&q->send->curr_sender, 0);
294 #endif
296 if (q->read == q->write)
300 #if CONFIG_CORELOCK == CORELOCK_NONE
301 cores[CURRENT_CORE].irq_level = oldlevel;
302 #elif CONFIG_CORELOCK == SW_CORELOCK
303 const unsigned int core = CURRENT_CORE;
304 cores[core].blk_ops.irq_level = oldlevel;
305 cores[core].blk_ops.flags = TBOP_UNLOCK_CORELOCK | TBOP_IRQ_LEVEL;
306 cores[core].blk_ops.cl_p = &q->cl;
307 #elif CONFIG_CORELOCK == CORELOCK_SWAP
308 const unsigned int core = CURRENT_CORE;
309 cores[core].blk_ops.irq_level = oldlevel;
310 cores[core].blk_ops.flags = TBOP_SET_VARu8 | TBOP_IRQ_LEVEL;
311 cores[core].blk_ops.var_u8p = &q->cl.locked;
312 cores[core].blk_ops.var_u8v = 0;
313 #endif /* CONFIG_CORELOCK */
314 block_thread(&q->queue);
315 oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
316 corelock_lock(&q->cl);
318 /* A message that woke us could now be gone */
319 while (q->read == q->write);
322 rd = q->read++ & QUEUE_LENGTH_MASK;
323 *ev = q->events[rd];
325 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
326 if(q->send && q->send->senders[rd])
328 /* Get data for a waiting thread if one */
329 queue_fetch_sender(q->send, rd);
331 #endif
333 corelock_unlock(&q->cl);
334 set_irq_level(oldlevel);
337 void queue_wait_w_tmo(struct event_queue *q, struct queue_event *ev, int ticks)
339 int oldlevel;
341 oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
342 corelock_lock(&q->cl);
344 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
345 if (q->send && q->send->curr_sender)
347 /* auto-reply */
348 queue_release_sender(&q->send->curr_sender, 0);
350 #endif
352 if (q->read == q->write && ticks > 0)
354 #if CONFIG_CORELOCK == CORELOCK_NONE
355 cores[CURRENT_CORE].irq_level = oldlevel;
356 #elif CONFIG_CORELOCK == SW_CORELOCK
357 const unsigned int core = CURRENT_CORE;
358 cores[core].blk_ops.irq_level = oldlevel;
359 cores[core].blk_ops.flags = TBOP_UNLOCK_CORELOCK | TBOP_IRQ_LEVEL;
360 cores[core].blk_ops.cl_p = &q->cl;
361 #elif CONFIG_CORELOCK == CORELOCK_SWAP
362 const unsigned int core = CURRENT_CORE;
363 cores[core].blk_ops.irq_level = oldlevel;
364 cores[core].blk_ops.flags = TBOP_SET_VARu8 | TBOP_IRQ_LEVEL;
365 cores[core].blk_ops.var_u8p = &q->cl.locked;
366 cores[core].blk_ops.var_u8v = 0;
367 #endif
368 block_thread_w_tmo(&q->queue, ticks);
369 oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
370 corelock_lock(&q->cl);
373 /* no worry about a removed message here - status is checked inside
374 locks - perhaps verify if timeout or false alarm */
375 if (q->read != q->write)
377 unsigned int rd = q->read++ & QUEUE_LENGTH_MASK;
378 *ev = q->events[rd];
380 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
381 if(q->send && q->send->senders[rd])
383 /* Get data for a waiting thread if one */
384 queue_fetch_sender(q->send, rd);
386 #endif
388 else
390 ev->id = SYS_TIMEOUT;
393 corelock_unlock(&q->cl);
394 set_irq_level(oldlevel);
397 void queue_post(struct event_queue *q, long id, intptr_t data)
399 int oldlevel;
400 unsigned int wr;
402 oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
403 corelock_lock(&q->cl);
405 wr = q->write++ & QUEUE_LENGTH_MASK;
407 q->events[wr].id = id;
408 q->events[wr].data = data;
410 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
411 if(q->send)
413 struct thread_entry **spp = &q->send->senders[wr];
415 if (*spp)
417 /* overflow protect - unblock any thread waiting at this index */
418 queue_release_sender(spp, 0);
421 #endif
423 /* Wakeup a waiting thread if any */
424 wakeup_thread(&q->queue);
426 corelock_unlock(&q->cl);
427 set_irq_level(oldlevel);
430 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
431 /* IRQ handlers are not allowed use of this function - we only aim to
432 protect the queue integrity by turning them off. */
433 intptr_t queue_send(struct event_queue *q, long id, intptr_t data)
435 int oldlevel;
436 unsigned int wr;
438 oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
439 corelock_lock(&q->cl);
441 wr = q->write++ & QUEUE_LENGTH_MASK;
443 q->events[wr].id = id;
444 q->events[wr].data = data;
446 if(q->send)
448 const unsigned int core = CURRENT_CORE;
449 struct thread_entry **spp = &q->send->senders[wr];
451 if(*spp)
453 /* overflow protect - unblock any thread waiting at this index */
454 queue_release_sender(spp, 0);
457 /* Wakeup a waiting thread if any */
458 wakeup_thread(&q->queue);
460 #if CONFIG_CORELOCK == CORELOCK_NONE
461 cores[core].irq_level = oldlevel;
462 #elif CONFIG_CORELOCK == SW_CORELOCK
463 cores[core].blk_ops.irq_level = oldlevel;
464 cores[core].blk_ops.flags = TBOP_UNLOCK_CORELOCK | TBOP_IRQ_LEVEL;
465 cores[core].blk_ops.cl_p = &q->cl;
466 #elif CONFIG_CORELOCK == CORELOCK_SWAP
467 cores[core].blk_ops.irq_level = oldlevel;
468 cores[core].blk_ops.flags = TBOP_SET_VARu8 | TBOP_IRQ_LEVEL;
469 cores[core].blk_ops.var_u8p = &q->cl.locked;
470 cores[core].blk_ops.var_u8v = 0;
471 #endif
472 block_thread_no_listlock(spp);
473 return cores[core].running->retval;
476 /* Function as queue_post if sending is not enabled */
477 wakeup_thread(&q->queue);
479 corelock_unlock(&q->cl);
480 set_irq_level(oldlevel);
482 return 0;
485 #if 0 /* not used now but probably will be later */
486 /* Query if the last message dequeued was added by queue_send or not */
487 bool queue_in_queue_send(struct event_queue *q)
489 bool in_send;
491 #if NUM_CORES > 1
492 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
493 corelock_lock(&q->cl);
494 #endif
496 in_send = q->send && q->send->curr_sender;
498 #if NUM_CORES > 1
499 corelock_unlock(&q->cl);
500 set_irq_level(oldlevel);
501 #endif
503 return in_send;
505 #endif
507 /* Replies with retval to the last dequeued message sent with queue_send */
508 void queue_reply(struct event_queue *q, intptr_t retval)
510 if(q->send && q->send->curr_sender)
512 #if NUM_CORES > 1
513 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
514 corelock_lock(&q->cl);
515 /* Double-check locking */
516 if(q->send && q->send->curr_sender)
518 #endif
520 queue_release_sender(&q->send->curr_sender, retval);
522 #if NUM_CORES > 1
524 corelock_unlock(&q->cl);
525 set_irq_level(oldlevel);
526 #endif
529 #endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
531 /* Poll queue to see if a message exists - careful in using the result if
532 * queue_remove_from_head is called when messages are posted - possibly use
533 * queue_wait_w_tmo(&q, 0) in that case or else a removed message that
534 * unsignals the queue may cause an unwanted block */
535 bool queue_empty(const struct event_queue* q)
537 return ( q->read == q->write );
540 bool queue_peek(struct event_queue *q, struct queue_event *ev)
542 if (q->read == q->write)
543 return false;
545 bool have_msg = false;
547 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
548 corelock_lock(&q->cl);
550 if (q->read != q->write)
552 *ev = q->events[q->read & QUEUE_LENGTH_MASK];
553 have_msg = true;
556 corelock_unlock(&q->cl);
557 set_irq_level(oldlevel);
559 return have_msg;
562 void queue_clear(struct event_queue* q)
564 int oldlevel;
566 oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
567 corelock_lock(&q->cl);
569 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
570 /* Release all threads waiting in the queue for a reply -
571 dequeued sent message will be handled by owning thread */
572 queue_release_all_senders(q);
573 #endif
575 q->read = 0;
576 q->write = 0;
578 corelock_unlock(&q->cl);
579 set_irq_level(oldlevel);
582 void queue_remove_from_head(struct event_queue *q, long id)
584 int oldlevel;
586 oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
587 corelock_lock(&q->cl);
589 while(q->read != q->write)
591 unsigned int rd = q->read & QUEUE_LENGTH_MASK;
593 if(q->events[rd].id != id)
595 break;
598 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
599 if(q->send)
601 struct thread_entry **spp = &q->send->senders[rd];
603 if (*spp)
605 /* Release any thread waiting on this message */
606 queue_release_sender(spp, 0);
609 #endif
610 q->read++;
613 corelock_unlock(&q->cl);
614 set_irq_level(oldlevel);
618 * The number of events waiting in the queue.
620 * @param struct of event_queue
621 * @return number of events in the queue
623 int queue_count(const struct event_queue *q)
625 return q->write - q->read;
628 int queue_broadcast(long id, intptr_t data)
630 int i;
632 #if NUM_CORES > 1
633 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
634 corelock_lock(&all_queues.cl);
635 #endif
637 for(i = 0;i < all_queues.count;i++)
639 queue_post(all_queues.queues[i], id, data);
642 #if NUM_CORES > 1
643 corelock_unlock(&all_queues.cl);
644 set_irq_level(oldlevel);
645 #endif
647 return i;
650 /****************************************************************************
651 * Timer tick
652 ****************************************************************************/
653 #if CONFIG_CPU == SH7034
654 void tick_start(unsigned int interval_in_ms)
656 unsigned long count;
658 count = CPU_FREQ * interval_in_ms / 1000 / 8;
660 if(count > 0x10000)
662 panicf("Error! The tick interval is too long (%d ms)\n",
663 interval_in_ms);
664 return;
667 /* We are using timer 0 */
669 TSTR &= ~0x01; /* Stop the timer */
670 TSNC &= ~0x01; /* No synchronization */
671 TMDR &= ~0x01; /* Operate normally */
673 TCNT0 = 0; /* Start counting at 0 */
674 GRA0 = (unsigned short)(count - 1);
675 TCR0 = 0x23; /* Clear at GRA match, sysclock/8 */
677 /* Enable interrupt on level 1 */
678 IPRC = (IPRC & ~0x00f0) | 0x0010;
680 TSR0 &= ~0x01;
681 TIER0 = 0xf9; /* Enable GRA match interrupt */
683 TSTR |= 0x01; /* Start timer 1 */
686 void IMIA0(void) __attribute__ ((interrupt_handler));
687 void IMIA0(void)
689 int i;
691 /* Run through the list of tick tasks */
692 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
694 if(tick_funcs[i])
696 tick_funcs[i]();
700 current_tick++;
702 TSR0 &= ~0x01;
704 #elif defined(CPU_COLDFIRE)
705 void tick_start(unsigned int interval_in_ms)
707 unsigned long count;
708 int prescale;
710 count = CPU_FREQ/2 * interval_in_ms / 1000 / 16;
712 if(count > 0x10000)
714 panicf("Error! The tick interval is too long (%d ms)\n",
715 interval_in_ms);
716 return;
719 prescale = cpu_frequency / CPU_FREQ;
720 /* Note: The prescaler is later adjusted on-the-fly on CPU frequency
721 changes within timer.c */
723 /* We are using timer 0 */
725 TRR0 = (unsigned short)(count - 1); /* The reference count */
726 TCN0 = 0; /* reset the timer */
727 TMR0 = 0x001d | ((unsigned short)(prescale - 1) << 8);
728 /* restart, CLK/16, enabled, prescaler */
730 TER0 = 0xff; /* Clear all events */
732 ICR1 = 0x8c; /* Interrupt on level 3.0 */
733 IMR &= ~0x200;
736 void TIMER0(void) __attribute__ ((interrupt_handler));
737 void TIMER0(void)
739 int i;
741 /* Run through the list of tick tasks */
742 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
744 if(tick_funcs[i])
746 tick_funcs[i]();
750 current_tick++;
752 TER0 = 0xff; /* Clear all events */
755 #elif defined(CPU_PP)
757 #ifndef BOOTLOADER
758 void TIMER1(void)
760 int i;
762 /* Run through the list of tick tasks (using main core) */
763 TIMER1_VAL; /* Read value to ack IRQ */
765 /* Run through the list of tick tasks using main CPU core -
766 wake up the COP through its control interface to provide pulse */
767 for (i = 0;i < MAX_NUM_TICK_TASKS;i++)
769 if (tick_funcs[i])
771 tick_funcs[i]();
775 #if NUM_CORES > 1
776 /* Pulse the COP */
777 core_wake(COP);
778 #endif /* NUM_CORES */
780 current_tick++;
782 #endif
784 /* Must be last function called init kernel/thread initialization */
785 void tick_start(unsigned int interval_in_ms)
787 #ifndef BOOTLOADER
788 TIMER1_CFG = 0x0;
789 TIMER1_VAL;
790 /* enable timer */
791 TIMER1_CFG = 0xc0000000 | (interval_in_ms*1000 - 1);
792 /* unmask interrupt source */
793 CPU_INT_EN = TIMER1_MASK;
794 #else
795 /* We don't enable interrupts in the bootloader */
796 (void)interval_in_ms;
797 #endif
800 #elif CONFIG_CPU == PNX0101
802 void timer_handler(void)
804 int i;
806 /* Run through the list of tick tasks */
807 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
809 if(tick_funcs[i])
810 tick_funcs[i]();
813 current_tick++;
815 TIMER0.clr = 0;
818 void tick_start(unsigned int interval_in_ms)
820 TIMER0.ctrl &= ~0x80; /* Disable the counter */
821 TIMER0.ctrl |= 0x40; /* Reload after counting down to zero */
822 TIMER0.load = 3000000 * interval_in_ms / 1000;
823 TIMER0.ctrl &= ~0xc; /* No prescaler */
824 TIMER0.clr = 1; /* Clear the interrupt request */
826 irq_set_int_handler(IRQ_TIMER0, timer_handler);
827 irq_enable_int(IRQ_TIMER0);
829 TIMER0.ctrl |= 0x80; /* Enable the counter */
831 #elif CONFIG_CPU == IMX31L
832 void tick_start(unsigned int interval_in_ms)
834 EPITCR1 &= ~0x1; /* Disable the counter */
836 EPITCR1 &= ~0xE; /* Disable interrupt, count down from 0xFFFFFFFF */
837 EPITCR1 &= ~0xFFF0; /* Clear prescaler */
838 #ifdef BOOTLOADER
839 EPITCR1 |= (2700 << 2); /* Prescaler = 2700 */
840 #endif
841 EPITCR1 &= ~(0x3 << 24);
842 EPITCR1 |= (0x2 << 24); /* Set clock source to external clock (27mhz) */
843 EPITSR1 = 1; /* Clear the interrupt request */
844 #ifndef BOOTLOADER
845 EPITLR1 = 27000000 * interval_in_ms / 1000;
846 EPITCMPR1 = 27000000 * interval_in_ms / 1000;
847 #else
848 (void)interval_in_ms;
849 #endif
851 //avic_enable_int(EPIT1, IRQ, EPIT_HANDLER);
853 EPITCR1 |= 0x1; /* Enable the counter */
856 #ifndef BOOTLOADER
857 void EPIT_HANDLER(void) __attribute__((interrupt("IRQ")));
858 void EPIT_HANDLER(void) {
859 int i;
861 /* Run through the list of tick tasks */
862 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
864 if(tick_funcs[i])
865 tick_funcs[i]();
868 current_tick++;
870 EPITSR1 = 1; /* Clear the interrupt request */
872 #endif
873 #endif
875 int tick_add_task(void (*f)(void))
877 int i;
878 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
880 /* Add a task if there is room */
881 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
883 if(tick_funcs[i] == NULL)
885 tick_funcs[i] = f;
886 set_irq_level(oldlevel);
887 return 0;
890 set_irq_level(oldlevel);
891 panicf("Error! tick_add_task(): out of tasks");
892 return -1;
895 int tick_remove_task(void (*f)(void))
897 int i;
898 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
900 /* Remove a task if it is there */
901 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
903 if(tick_funcs[i] == f)
905 tick_funcs[i] = NULL;
906 set_irq_level(oldlevel);
907 return 0;
911 set_irq_level(oldlevel);
912 return -1;
915 /****************************************************************************
916 * Tick-based interval timers/one-shots - be mindful this is not really
917 * intended for continuous timers but for events that need to run for a short
918 * time and be cancelled without further software intervention.
919 ****************************************************************************/
920 #ifdef INCLUDE_TIMEOUT_API
921 static struct timeout *tmo_list = NULL; /* list of active timeout events */
923 /* timeout tick task - calls event handlers when they expire
924 * Event handlers may alter ticks, callback and data during operation.
926 static void timeout_tick(void)
928 unsigned long tick = current_tick;
929 struct timeout *curr, *next;
931 for (curr = tmo_list; curr != NULL; curr = next)
933 next = (struct timeout *)curr->next;
935 if (TIME_BEFORE(tick, curr->expires))
936 continue;
938 /* this event has expired - call callback */
939 if (curr->callback(curr))
940 *(long *)&curr->expires = tick + curr->ticks; /* reload */
941 else
942 timeout_cancel(curr); /* cancel */
946 /* Cancels a timeout callback - can be called from the ISR */
947 void timeout_cancel(struct timeout *tmo)
949 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
951 if (tmo_list != NULL)
953 struct timeout *curr = tmo_list;
954 struct timeout *prev = NULL;
956 while (curr != tmo && curr != NULL)
958 prev = curr;
959 curr = (struct timeout *)curr->next;
962 if (curr != NULL)
964 /* in list */
965 if (prev == NULL)
966 tmo_list = (struct timeout *)curr->next;
967 else
968 *(const struct timeout **)&prev->next = curr->next;
970 if (tmo_list == NULL)
971 tick_remove_task(timeout_tick); /* last one - remove task */
973 /* not in list or tmo == NULL */
976 set_irq_level(oldlevel);
979 /* Adds a timeout callback - calling with an active timeout resets the
980 interval - can be called from the ISR */
981 void timeout_register(struct timeout *tmo, timeout_cb_type callback,
982 int ticks, intptr_t data)
984 int oldlevel;
985 struct timeout *curr;
987 if (tmo == NULL)
988 return;
990 oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
992 /* see if this one is already registered */
993 curr = tmo_list;
994 while (curr != tmo && curr != NULL)
995 curr = (struct timeout *)curr->next;
997 if (curr == NULL)
999 /* not found - add it */
1000 if (tmo_list == NULL)
1001 tick_add_task(timeout_tick); /* first one - add task */
1003 *(struct timeout **)&tmo->next = tmo_list;
1004 tmo_list = tmo;
1007 tmo->callback = callback;
1008 tmo->ticks = ticks;
1009 tmo->data = data;
1010 *(long *)&tmo->expires = current_tick + ticks;
1012 set_irq_level(oldlevel);
1015 #endif /* INCLUDE_TIMEOUT_API */
1017 /****************************************************************************
1018 * Simple mutex functions ;)
1019 ****************************************************************************/
1020 void mutex_init(struct mutex *m)
1022 m->queue = NULL;
1023 m->thread = NULL;
1024 m->count = 0;
1025 m->locked = 0;
1026 #if CONFIG_CORELOCK == SW_CORELOCK
1027 corelock_init(&m->cl);
1028 #endif
1031 void mutex_lock(struct mutex *m)
1033 const unsigned int core = CURRENT_CORE;
1034 struct thread_entry *const thread = cores[core].running;
1036 if(thread == m->thread)
1038 m->count++;
1039 return;
1042 /* Repeat some stuff here or else all the variation is too difficult to
1043 read */
1044 #if CONFIG_CORELOCK == CORELOCK_SWAP
1045 /* peek at lock until it's no longer busy */
1046 unsigned int locked;
1047 while ((locked = xchg8(&m->locked, STATE_BUSYu8)) == STATE_BUSYu8);
1048 if(locked == 0)
1050 m->thread = thread;
1051 m->locked = 1;
1052 return;
1055 /* Block until the lock is open... */
1056 cores[core].blk_ops.flags = TBOP_SET_VARu8;
1057 cores[core].blk_ops.var_u8p = &m->locked;
1058 cores[core].blk_ops.var_u8v = 1;
1059 #else
1060 corelock_lock(&m->cl);
1061 if (m->locked == 0)
1063 m->locked = 1;
1064 m->thread = thread;
1065 corelock_unlock(&m->cl);
1066 return;
1069 /* Block until the lock is open... */
1070 #if CONFIG_CORELOCK == SW_CORELOCK
1071 cores[core].blk_ops.flags = TBOP_UNLOCK_CORELOCK;
1072 cores[core].blk_ops.cl_p = &m->cl;
1073 #endif
1074 #endif /* CONFIG_CORELOCK */
1076 block_thread_no_listlock(&m->queue);
1079 void mutex_unlock(struct mutex *m)
1081 /* unlocker not being the owner is an unlocking violation */
1082 KERNEL_ASSERT(m->thread == cores[CURRENT_CORE].running,
1083 "mutex_unlock->wrong thread (recurse)");
1085 if(m->count > 0)
1087 /* this thread still owns lock */
1088 m->count--;
1089 return;
1092 #if CONFIG_CORELOCK == SW_CORELOCK
1093 /* lock out other cores */
1094 corelock_lock(&m->cl);
1095 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1096 /* wait for peeker to move on */
1097 while (xchg8(&m->locked, STATE_BUSYu8) == STATE_BUSYu8);
1098 #endif
1100 /* transfer to next queued thread if any */
1102 /* This can become busy using SWP but is safe since only one thread
1103 will be changing things at a time. Allowing timeout waits will
1104 change that however but not now. There is also a hazard the thread
1105 could be killed before performing the wakeup but that's just
1106 irresponsible. :-) */
1107 m->thread = m->queue;
1109 if(m->thread == NULL)
1111 m->locked = 0; /* release lock */
1112 #if CONFIG_CORELOCK == SW_CORELOCK
1113 corelock_unlock(&m->cl);
1114 #endif
1116 else /* another thread is waiting - remain locked */
1118 wakeup_thread_no_listlock(&m->queue);
1119 #if CONFIG_CORELOCK == SW_CORELOCK
1120 corelock_unlock(&m->cl);
1121 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1122 m->locked = 1;
1123 #endif
1127 /****************************************************************************
1128 * Simpl-er mutex functions ;)
1129 ****************************************************************************/
1130 void spinlock_init(struct spinlock *l IF_COP(, unsigned int flags))
1132 l->locked = 0;
1133 l->thread = NULL;
1134 l->count = 0;
1135 #if NUM_CORES > 1
1136 l->task_switch = flags & SPINLOCK_TASK_SWITCH;
1137 corelock_init(&l->cl);
1138 #endif
1141 void spinlock_lock(struct spinlock *l)
1143 struct thread_entry *const thread = cores[CURRENT_CORE].running;
1145 if (l->thread == thread)
1147 l->count++;
1148 return;
1151 #if NUM_CORES > 1
1152 if (l->task_switch != 0)
1153 #endif
1155 /* Let other threads run until the lock is free */
1156 while(test_and_set(&l->locked, 1, &l->cl) != 0)
1158 /* spin and switch until the lock is open... */
1159 switch_thread(NULL);
1162 #if NUM_CORES > 1
1163 else
1165 /* Use the corelock purely */
1166 corelock_lock(&l->cl);
1168 #endif
1170 l->thread = thread;
1173 void spinlock_unlock(struct spinlock *l)
1175 /* unlocker not being the owner is an unlocking violation */
1176 KERNEL_ASSERT(l->thread == cores[CURRENT_CORE].running,
1177 "spinlock_unlock->wrong thread");
1179 if (l->count > 0)
1181 /* this thread still owns lock */
1182 l->count--;
1183 return;
1186 /* clear owner */
1187 l->thread = NULL;
1189 #if NUM_CORES > 1
1190 if (l->task_switch != 0)
1191 #endif
1193 /* release lock */
1194 #if CONFIG_CORELOCK == SW_CORELOCK
1195 /* This must be done since our unlock could be missed by the
1196 test_and_set and leave the object locked permanently */
1197 corelock_lock(&l->cl);
1198 #endif
1199 l->locked = 0;
1202 #if NUM_CORES > 1
1203 corelock_unlock(&l->cl);
1204 #endif
1207 /****************************************************************************
1208 * Simple semaphore functions ;)
1209 ****************************************************************************/
1210 #ifdef HAVE_SEMAPHORE_OBJECTS
1211 void semaphore_init(struct semaphore *s, int max, int start)
1213 KERNEL_ASSERT(max > 0 && start >= 0 && start <= max,
1214 "semaphore_init->inv arg");
1215 s->queue = NULL;
1216 s->max = max;
1217 s->count = start;
1218 #if CONFIG_CORELOCK == SW_CORELOCK
1219 corelock_init(&s->cl);
1220 #endif
1223 void semaphore_wait(struct semaphore *s)
1225 #if CONFIG_CORELOCK == CORELOCK_NONE || CONFIG_CORELOCK == SW_CORELOCK
1226 corelock_lock(&s->cl);
1227 if(--s->count >= 0)
1229 corelock_unlock(&s->cl);
1230 return;
1232 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1233 int count;
1234 while ((count = xchg32(&s->count, STATE_BUSYi)) == STATE_BUSYi);
1235 if(--count >= 0)
1237 s->count = count;
1238 return;
1240 #endif
1242 /* too many waits - block until dequeued */
1243 #if CONFIG_CORELOCK == SW_CORELOCK
1244 const unsigned int core = CURRENT_CORE;
1245 cores[core].blk_ops.flags = TBOP_UNLOCK_CORELOCK;
1246 cores[core].blk_ops.cl_p = &s->cl;
1247 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1248 const unsigned int core = CURRENT_CORE;
1249 cores[core].blk_ops.flags = TBOP_SET_VARi;
1250 cores[core].blk_ops.var_ip = &s->count;
1251 cores[core].blk_ops.var_iv = count;
1252 #endif
1253 block_thread_no_listlock(&s->queue);
1256 void semaphore_release(struct semaphore *s)
1258 #if CONFIG_CORELOCK == CORELOCK_NONE || CONFIG_CORELOCK == SW_CORELOCK
1259 corelock_lock(&s->cl);
1260 if (s->count < s->max)
1262 if (++s->count <= 0)
1264 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1265 int count;
1266 while ((count = xchg32(&s->count, STATE_BUSYi)) == STATE_BUSYi);
1267 if(count < s->max)
1269 if(++count <= 0)
1271 #endif /* CONFIG_CORELOCK */
1273 /* there should be threads in this queue */
1274 KERNEL_ASSERT(s->queue != NULL, "semaphore->wakeup");
1275 /* a thread was queued - wake it up */
1276 wakeup_thread_no_listlock(&s->queue);
1280 #if CONFIG_CORELOCK == SW_CORELOCK
1281 corelock_unlock(&s->cl);
1282 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1283 s->count = count;
1284 #endif
1286 #endif /* HAVE_SEMAPHORE_OBJECTS */
1288 /****************************************************************************
1289 * Simple event functions ;)
1290 ****************************************************************************/
1291 #ifdef HAVE_EVENT_OBJECTS
1292 void event_init(struct event *e, unsigned int flags)
1294 e->queues[STATE_NONSIGNALED] = NULL;
1295 e->queues[STATE_SIGNALED] = NULL;
1296 e->state = flags & STATE_SIGNALED;
1297 e->automatic = (flags & EVENT_AUTOMATIC) ? 1 : 0;
1298 #if CONFIG_CORELOCK == SW_CORELOCK
1299 corelock_init(&e->cl);
1300 #endif
1303 void event_wait(struct event *e, unsigned int for_state)
1305 unsigned int last_state;
1306 #if CONFIG_CORELOCK == CORELOCK_NONE || CONFIG_CORELOCK == SW_CORELOCK
1307 corelock_lock(&e->cl);
1308 last_state = e->state;
1309 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1310 while ((last_state = xchg8(&e->state, STATE_BUSYu8)) == STATE_BUSYu8);
1311 #endif
1313 if(e->automatic != 0)
1315 /* wait for false always satisfied by definition
1316 or if it just changed to false */
1317 if(last_state == STATE_SIGNALED || for_state == STATE_NONSIGNALED)
1319 /* automatic - unsignal */
1320 e->state = STATE_NONSIGNALED;
1321 #if CONFIG_CORELOCK == SW_CORELOCK
1322 corelock_unlock(&e->cl);
1323 #endif
1324 return;
1326 /* block until state matches */
1328 else if(for_state == last_state)
1330 /* the state being waited for is the current state */
1331 #if CONFIG_CORELOCK == SW_CORELOCK
1332 corelock_unlock(&e->cl);
1333 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1334 e->state = last_state;
1335 #endif
1336 return;
1340 /* current state does not match wait-for state */
1341 #if CONFIG_CORELOCK == SW_CORELOCK
1342 const unsigned int core = CURRENT_CORE;
1343 cores[core].blk_ops.flags = TBOP_UNLOCK_CORELOCK;
1344 cores[core].blk_ops.cl_p = &e->cl;
1345 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1346 const unsigned int core = CURRENT_CORE;
1347 cores[core].blk_ops.flags = TBOP_SET_VARu8;
1348 cores[core].blk_ops.var_u8p = &e->state;
1349 cores[core].blk_ops.var_u8v = last_state;
1350 #endif
1351 block_thread_no_listlock(&e->queues[for_state]);
1355 void event_set_state(struct event *e, unsigned int state)
1357 unsigned int last_state;
1358 #if CONFIG_CORELOCK == CORELOCK_NONE || CONFIG_CORELOCK == SW_CORELOCK
1359 corelock_lock(&e->cl);
1360 last_state = e->state;
1361 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1362 while ((last_state = xchg8(&e->state, STATE_BUSYu8)) == STATE_BUSYu8);
1363 #endif
1365 if(last_state == state)
1367 /* no change */
1368 #if CONFIG_CORELOCK == SW_CORELOCK
1369 corelock_unlock(&e->cl);
1370 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1371 e->state = last_state;
1372 #endif
1373 return;
1376 if(state == STATE_SIGNALED)
1378 if(e->automatic != 0)
1380 struct thread_entry *thread;
1381 /* no thread should have ever blocked for unsignaled */
1382 KERNEL_ASSERT(e->queues[STATE_NONSIGNALED] == NULL,
1383 "set_event_state->queue[NS]:S");
1384 /* pass to next thread and keep unsignaled - "pulse" */
1385 thread = wakeup_thread_no_listlock(&e->queues[STATE_SIGNALED]);
1386 e->state = thread != NULL ? STATE_NONSIGNALED : STATE_SIGNALED;
1388 else
1390 /* release all threads waiting for signaled */
1391 thread_queue_wake_no_listlock(&e->queues[STATE_SIGNALED]);
1392 e->state = STATE_SIGNALED;
1395 else
1397 /* release all threads waiting for unsignaled */
1399 /* no thread should have ever blocked if automatic */
1400 KERNEL_ASSERT(e->queues[STATE_NONSIGNALED] == NULL ||
1401 e->automatic == 0, "set_event_state->queue[NS]:NS");
1403 thread_queue_wake_no_listlock(&e->queues[STATE_NONSIGNALED]);
1404 e->state = STATE_NONSIGNALED;
1407 #if CONFIG_CORELOCK == SW_CORELOCK
1408 corelock_unlock(&e->cl);
1409 #endif
1411 #endif /* HAVE_EVENT_OBJECTS */