1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
27 #if CONFIG_CPU == IMX31L
28 #include "avic-imx31.h"
31 /* Make this nonzero to enable more elaborate checks on objects */
33 #define KERNEL_OBJECT_CHECKS 1 /* Always 1 for DEBUG */
35 #define KERNEL_OBJECT_CHECKS 0
38 #if KERNEL_OBJECT_CHECKS
39 #define KERNEL_ASSERT(exp, msg...) \
40 ({ if (!({ exp; })) panicf(msg); })
42 #define KERNEL_ASSERT(exp, msg...) ({})
45 #if (!defined(CPU_PP) && (CONFIG_CPU != IMX31L)) || !defined(BOOTLOADER)
46 volatile long current_tick NOCACHEDATA_ATTR
= 0;
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. */
57 struct event_queue
*queues
[MAX_NUM_QUEUES
];
61 } all_queues NOCACHEBSS_ATTR
;
63 /****************************************************************************
64 * Standard kernel stuff
65 ****************************************************************************/
66 void kernel_init(void)
68 /* Init the threading API */
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
);
85 #if CONFIG_CPU == S3C2440 && defined(BOOTLOADER)
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
99 #elif defined(CPU_PP) && defined(BOOTLOADER)
100 unsigned stop
= USEC_TIMER
+ ticks
* (1000000/HZ
);
101 while (TIME_BEFORE(USEC_TIMER
, stop
))
110 #if ((CONFIG_CPU == S3C2440 || defined(ELIO_TPJ1022) || CONFIG_CPU == IMX31L) && defined(BOOTLOADER))
111 /* Some targets don't like yielding in the bootloader */
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
,
127 struct thread_entry
**spp
= &send
->senders
[i
];
131 send
->curr_sender
= *spp
;
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
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
,
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 -
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
)
165 for(i
= q
->read
; i
!= q
->write
; i
++)
167 struct thread_entry
**spp
=
168 &q
->send
->senders
[i
& QUEUE_LENGTH_MASK
];
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
);
190 memset(send
, 0, sizeof(*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
);
206 corelock_lock(&all_queues
.cl
);
209 corelock_init(&q
->cl
);
210 thread_queue_init(&q
->queue
);
213 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
214 q
->send
= NULL
; /* No message sending by default */
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
)
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 */
249 for(;i
< all_queues
.count
;i
++)
251 all_queues
.queues
[i
] = all_queues
.queues
[i
+1];
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
);
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
)
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
)
292 queue_release_sender(&q
->send
->curr_sender
, 0);
296 if (q
->read
== q
->write
)
300 #if CONFIG_CORELOCK == CORELOCK_NONE
301 #elif CONFIG_CORELOCK == SW_CORELOCK
302 const unsigned int core
= CURRENT_CORE
;
303 cores
[core
].blk_ops
.flags
= TBOP_UNLOCK_CORELOCK
;
304 cores
[core
].blk_ops
.cl_p
= &q
->cl
;
305 #elif CONFIG_CORELOCK == CORELOCK_SWAP
306 const unsigned int core
= CURRENT_CORE
;
307 cores
[core
].blk_ops
.flags
= TBOP_SET_VARu8
;
308 cores
[core
].blk_ops
.var_u8p
= &q
->cl
.locked
;
309 cores
[core
].blk_ops
.var_u8v
= 0;
310 #endif /* CONFIG_CORELOCK */
311 block_thread(&q
->queue
);
312 oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
313 corelock_lock(&q
->cl
);
315 /* A message that woke us could now be gone */
316 while (q
->read
== q
->write
);
319 rd
= q
->read
++ & QUEUE_LENGTH_MASK
;
322 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
323 if(q
->send
&& q
->send
->senders
[rd
])
325 /* Get data for a waiting thread if one */
326 queue_fetch_sender(q
->send
, rd
);
330 corelock_unlock(&q
->cl
);
331 set_irq_level(oldlevel
);
334 void queue_wait_w_tmo(struct event_queue
*q
, struct queue_event
*ev
, int ticks
)
338 oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
339 corelock_lock(&q
->cl
);
341 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
342 if (q
->send
&& q
->send
->curr_sender
)
345 queue_release_sender(&q
->send
->curr_sender
, 0);
349 if (q
->read
== q
->write
&& ticks
> 0)
351 #if CONFIG_CORELOCK == CORELOCK_NONE
352 #elif CONFIG_CORELOCK == SW_CORELOCK
353 const unsigned int core
= CURRENT_CORE
;
354 cores
[core
].blk_ops
.flags
= TBOP_UNLOCK_CORELOCK
;
355 cores
[core
].blk_ops
.cl_p
= &q
->cl
;
356 #elif CONFIG_CORELOCK == CORELOCK_SWAP
357 const unsigned int core
= CURRENT_CORE
;
358 cores
[core
].blk_ops
.flags
= TBOP_SET_VARu8
;
359 cores
[core
].blk_ops
.var_u8p
= &q
->cl
.locked
;
360 cores
[core
].blk_ops
.var_u8v
= 0;
362 block_thread_w_tmo(&q
->queue
, ticks
);
363 oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
364 corelock_lock(&q
->cl
);
367 /* no worry about a removed message here - status is checked inside
368 locks - perhaps verify if timeout or false alarm */
369 if (q
->read
!= q
->write
)
371 unsigned int rd
= q
->read
++ & QUEUE_LENGTH_MASK
;
374 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
375 if(q
->send
&& q
->send
->senders
[rd
])
377 /* Get data for a waiting thread if one */
378 queue_fetch_sender(q
->send
, rd
);
384 ev
->id
= SYS_TIMEOUT
;
387 corelock_unlock(&q
->cl
);
388 set_irq_level(oldlevel
);
391 void queue_post(struct event_queue
*q
, long id
, intptr_t data
)
396 oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
397 corelock_lock(&q
->cl
);
399 wr
= q
->write
++ & QUEUE_LENGTH_MASK
;
401 q
->events
[wr
].id
= id
;
402 q
->events
[wr
].data
= data
;
404 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
407 struct thread_entry
**spp
= &q
->send
->senders
[wr
];
411 /* overflow protect - unblock any thread waiting at this index */
412 queue_release_sender(spp
, 0);
417 /* Wakeup a waiting thread if any */
418 wakeup_thread(&q
->queue
);
420 corelock_unlock(&q
->cl
);
421 set_irq_level(oldlevel
);
424 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
425 /* IRQ handlers are not allowed use of this function - we only aim to
426 protect the queue integrity by turning them off. */
427 intptr_t queue_send(struct event_queue
*q
, long id
, intptr_t data
)
432 oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
433 corelock_lock(&q
->cl
);
435 wr
= q
->write
++ & QUEUE_LENGTH_MASK
;
437 q
->events
[wr
].id
= id
;
438 q
->events
[wr
].data
= data
;
442 const unsigned int core
= CURRENT_CORE
;
443 struct thread_entry
**spp
= &q
->send
->senders
[wr
];
447 /* overflow protect - unblock any thread waiting at this index */
448 queue_release_sender(spp
, 0);
451 /* Wakeup a waiting thread if any */
452 wakeup_thread(&q
->queue
);
454 #if CONFIG_CORELOCK == CORELOCK_NONE
455 #elif CONFIG_CORELOCK == SW_CORELOCK
456 cores
[core
].blk_ops
.flags
= TBOP_UNLOCK_CORELOCK
;
457 cores
[core
].blk_ops
.cl_p
= &q
->cl
;
458 #elif CONFIG_CORELOCK == CORELOCK_SWAP
459 cores
[core
].blk_ops
.flags
= TBOP_SET_VARu8
;
460 cores
[core
].blk_ops
.var_u8p
= &q
->cl
.locked
;
461 cores
[core
].blk_ops
.var_u8v
= 0;
463 block_thread_no_listlock(spp
);
464 return cores
[core
].running
->retval
;
467 /* Function as queue_post if sending is not enabled */
468 wakeup_thread(&q
->queue
);
470 corelock_unlock(&q
->cl
);
471 set_irq_level(oldlevel
);
476 #if 0 /* not used now but probably will be later */
477 /* Query if the last message dequeued was added by queue_send or not */
478 bool queue_in_queue_send(struct event_queue
*q
)
483 int oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
484 corelock_lock(&q
->cl
);
487 in_send
= q
->send
&& q
->send
->curr_sender
;
490 corelock_unlock(&q
->cl
);
491 set_irq_level(oldlevel
);
498 /* Replies with retval to the last dequeued message sent with queue_send */
499 void queue_reply(struct event_queue
*q
, intptr_t retval
)
501 if(q
->send
&& q
->send
->curr_sender
)
504 int oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
505 corelock_lock(&q
->cl
);
506 /* Double-check locking */
507 if(q
->send
&& q
->send
->curr_sender
)
511 queue_release_sender(&q
->send
->curr_sender
, retval
);
515 corelock_unlock(&q
->cl
);
516 set_irq_level(oldlevel
);
520 #endif /* HAVE_EXTENDED_MESSAGING_AND_NAME */
522 /* Poll queue to see if a message exists - careful in using the result if
523 * queue_remove_from_head is called when messages are posted - possibly use
524 * queue_wait_w_tmo(&q, 0) in that case or else a removed message that
525 * unsignals the queue may cause an unwanted block */
526 bool queue_empty(const struct event_queue
* q
)
528 return ( q
->read
== q
->write
);
531 bool queue_peek(struct event_queue
*q
, struct queue_event
*ev
)
533 if (q
->read
== q
->write
)
536 bool have_msg
= false;
538 int oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
539 corelock_lock(&q
->cl
);
541 if (q
->read
!= q
->write
)
543 *ev
= q
->events
[q
->read
& QUEUE_LENGTH_MASK
];
547 corelock_unlock(&q
->cl
);
548 set_irq_level(oldlevel
);
553 void queue_clear(struct event_queue
* q
)
557 oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
558 corelock_lock(&q
->cl
);
560 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
561 /* Release all threads waiting in the queue for a reply -
562 dequeued sent message will be handled by owning thread */
563 queue_release_all_senders(q
);
569 corelock_unlock(&q
->cl
);
570 set_irq_level(oldlevel
);
573 void queue_remove_from_head(struct event_queue
*q
, long id
)
577 oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
578 corelock_lock(&q
->cl
);
580 while(q
->read
!= q
->write
)
582 unsigned int rd
= q
->read
& QUEUE_LENGTH_MASK
;
584 if(q
->events
[rd
].id
!= id
)
589 #ifdef HAVE_EXTENDED_MESSAGING_AND_NAME
592 struct thread_entry
**spp
= &q
->send
->senders
[rd
];
596 /* Release any thread waiting on this message */
597 queue_release_sender(spp
, 0);
604 corelock_unlock(&q
->cl
);
605 set_irq_level(oldlevel
);
609 * The number of events waiting in the queue.
611 * @param struct of event_queue
612 * @return number of events in the queue
614 int queue_count(const struct event_queue
*q
)
616 return q
->write
- q
->read
;
619 int queue_broadcast(long id
, intptr_t data
)
624 int oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
625 corelock_lock(&all_queues
.cl
);
628 for(i
= 0;i
< all_queues
.count
;i
++)
630 queue_post(all_queues
.queues
[i
], id
, data
);
634 corelock_unlock(&all_queues
.cl
);
635 set_irq_level(oldlevel
);
641 /****************************************************************************
643 ****************************************************************************/
644 #if CONFIG_CPU == SH7034
645 void tick_start(unsigned int interval_in_ms
)
649 count
= CPU_FREQ
* interval_in_ms
/ 1000 / 8;
653 panicf("Error! The tick interval is too long (%d ms)\n",
658 /* We are using timer 0 */
660 TSTR
&= ~0x01; /* Stop the timer */
661 TSNC
&= ~0x01; /* No synchronization */
662 TMDR
&= ~0x01; /* Operate normally */
664 TCNT0
= 0; /* Start counting at 0 */
665 GRA0
= (unsigned short)(count
- 1);
666 TCR0
= 0x23; /* Clear at GRA match, sysclock/8 */
668 /* Enable interrupt on level 1 */
669 IPRC
= (IPRC
& ~0x00f0) | 0x0010;
672 TIER0
= 0xf9; /* Enable GRA match interrupt */
674 TSTR
|= 0x01; /* Start timer 1 */
677 void IMIA0(void) __attribute__ ((interrupt_handler
));
682 /* Run through the list of tick tasks */
683 for(i
= 0;i
< MAX_NUM_TICK_TASKS
;i
++)
695 #elif defined(CPU_COLDFIRE)
696 void tick_start(unsigned int interval_in_ms
)
701 count
= CPU_FREQ
/2 * interval_in_ms
/ 1000 / 16;
705 panicf("Error! The tick interval is too long (%d ms)\n",
710 prescale
= cpu_frequency
/ CPU_FREQ
;
711 /* Note: The prescaler is later adjusted on-the-fly on CPU frequency
712 changes within timer.c */
714 /* We are using timer 0 */
716 TRR0
= (unsigned short)(count
- 1); /* The reference count */
717 TCN0
= 0; /* reset the timer */
718 TMR0
= 0x001d | ((unsigned short)(prescale
- 1) << 8);
719 /* restart, CLK/16, enabled, prescaler */
721 TER0
= 0xff; /* Clear all events */
723 ICR1
= 0x8c; /* Interrupt on level 3.0 */
727 void TIMER0(void) __attribute__ ((interrupt_handler
));
732 /* Run through the list of tick tasks */
733 for(i
= 0;i
< MAX_NUM_TICK_TASKS
;i
++)
743 TER0
= 0xff; /* Clear all events */
746 #elif defined(CPU_PP)
753 /* Run through the list of tick tasks (using main core) */
754 TIMER1_VAL
; /* Read value to ack IRQ */
756 /* Run through the list of tick tasks using main CPU core -
757 wake up the COP through its control interface to provide pulse */
758 for (i
= 0;i
< MAX_NUM_TICK_TASKS
;i
++)
769 #endif /* NUM_CORES */
775 /* Must be last function called init kernel/thread initialization */
776 void tick_start(unsigned int interval_in_ms
)
782 TIMER1_CFG
= 0xc0000000 | (interval_in_ms
*1000 - 1);
783 /* unmask interrupt source */
784 CPU_INT_EN
= TIMER1_MASK
;
786 /* We don't enable interrupts in the bootloader */
787 (void)interval_in_ms
;
791 #elif CONFIG_CPU == PNX0101
793 void timer_handler(void)
797 /* Run through the list of tick tasks */
798 for(i
= 0;i
< MAX_NUM_TICK_TASKS
;i
++)
809 void tick_start(unsigned int interval_in_ms
)
811 TIMER0
.ctrl
&= ~0x80; /* Disable the counter */
812 TIMER0
.ctrl
|= 0x40; /* Reload after counting down to zero */
813 TIMER0
.load
= 3000000 * interval_in_ms
/ 1000;
814 TIMER0
.ctrl
&= ~0xc; /* No prescaler */
815 TIMER0
.clr
= 1; /* Clear the interrupt request */
817 irq_set_int_handler(IRQ_TIMER0
, timer_handler
);
818 irq_enable_int(IRQ_TIMER0
);
820 TIMER0
.ctrl
|= 0x80; /* Enable the counter */
822 #elif CONFIG_CPU == IMX31L
823 void tick_start(unsigned int interval_in_ms
)
825 EPITCR1
&= ~0x1; /* Disable the counter */
827 EPITCR1
&= ~0xE; /* Disable interrupt, count down from 0xFFFFFFFF */
828 EPITCR1
&= ~0xFFF0; /* Clear prescaler */
830 EPITCR1
|= (2700 << 2); /* Prescaler = 2700 */
832 EPITCR1
&= ~(0x3 << 24);
833 EPITCR1
|= (0x2 << 24); /* Set clock source to external clock (27mhz) */
834 EPITSR1
= 1; /* Clear the interrupt request */
836 EPITLR1
= 27000000 * interval_in_ms
/ 1000;
837 EPITCMPR1
= 27000000 * interval_in_ms
/ 1000;
839 (void)interval_in_ms
;
842 //avic_enable_int(EPIT1, IRQ, EPIT_HANDLER);
844 EPITCR1
|= 0x1; /* Enable the counter */
848 void EPIT_HANDLER(void) __attribute__((interrupt("IRQ")));
849 void EPIT_HANDLER(void) {
852 /* Run through the list of tick tasks */
853 for(i
= 0;i
< MAX_NUM_TICK_TASKS
;i
++)
861 EPITSR1
= 1; /* Clear the interrupt request */
866 int tick_add_task(void (*f
)(void))
869 int oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
871 /* Add a task if there is room */
872 for(i
= 0;i
< MAX_NUM_TICK_TASKS
;i
++)
874 if(tick_funcs
[i
] == NULL
)
877 set_irq_level(oldlevel
);
881 set_irq_level(oldlevel
);
882 panicf("Error! tick_add_task(): out of tasks");
886 int tick_remove_task(void (*f
)(void))
889 int oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
891 /* Remove a task if it is there */
892 for(i
= 0;i
< MAX_NUM_TICK_TASKS
;i
++)
894 if(tick_funcs
[i
] == f
)
896 tick_funcs
[i
] = NULL
;
897 set_irq_level(oldlevel
);
902 set_irq_level(oldlevel
);
906 /****************************************************************************
907 * Tick-based interval timers/one-shots - be mindful this is not really
908 * intended for continuous timers but for events that need to run for a short
909 * time and be cancelled without further software intervention.
910 ****************************************************************************/
911 #ifdef INCLUDE_TIMEOUT_API
912 static struct timeout
*tmo_list
= NULL
; /* list of active timeout events */
914 /* timeout tick task - calls event handlers when they expire
915 * Event handlers may alter ticks, callback and data during operation.
917 static void timeout_tick(void)
919 unsigned long tick
= current_tick
;
920 struct timeout
*curr
, *next
;
922 for (curr
= tmo_list
; curr
!= NULL
; curr
= next
)
924 next
= (struct timeout
*)curr
->next
;
926 if (TIME_BEFORE(tick
, curr
->expires
))
929 /* this event has expired - call callback */
930 if (curr
->callback(curr
))
931 *(long *)&curr
->expires
= tick
+ curr
->ticks
; /* reload */
933 timeout_cancel(curr
); /* cancel */
937 /* Cancels a timeout callback - can be called from the ISR */
938 void timeout_cancel(struct timeout
*tmo
)
940 int oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
942 if (tmo_list
!= NULL
)
944 struct timeout
*curr
= tmo_list
;
945 struct timeout
*prev
= NULL
;
947 while (curr
!= tmo
&& curr
!= NULL
)
950 curr
= (struct timeout
*)curr
->next
;
957 tmo_list
= (struct timeout
*)curr
->next
;
959 *(const struct timeout
**)&prev
->next
= curr
->next
;
961 if (tmo_list
== NULL
)
962 tick_remove_task(timeout_tick
); /* last one - remove task */
964 /* not in list or tmo == NULL */
967 set_irq_level(oldlevel
);
970 /* Adds a timeout callback - calling with an active timeout resets the
971 interval - can be called from the ISR */
972 void timeout_register(struct timeout
*tmo
, timeout_cb_type callback
,
973 int ticks
, intptr_t data
)
976 struct timeout
*curr
;
981 oldlevel
= set_irq_level(HIGHEST_IRQ_LEVEL
);
983 /* see if this one is already registered */
985 while (curr
!= tmo
&& curr
!= NULL
)
986 curr
= (struct timeout
*)curr
->next
;
990 /* not found - add it */
991 if (tmo_list
== NULL
)
992 tick_add_task(timeout_tick
); /* first one - add task */
994 *(struct timeout
**)&tmo
->next
= tmo_list
;
998 tmo
->callback
= callback
;
1001 *(long *)&tmo
->expires
= current_tick
+ ticks
;
1003 set_irq_level(oldlevel
);
1006 #endif /* INCLUDE_TIMEOUT_API */
1008 /****************************************************************************
1009 * Simple mutex functions ;)
1010 ****************************************************************************/
1011 void mutex_init(struct mutex
*m
)
1017 #if CONFIG_CORELOCK == SW_CORELOCK
1018 corelock_init(&m
->cl
);
1022 void mutex_lock(struct mutex
*m
)
1024 const unsigned int core
= CURRENT_CORE
;
1025 struct thread_entry
*const thread
= cores
[core
].running
;
1027 if(thread
== m
->thread
)
1033 /* Repeat some stuff here or else all the variation is too difficult to
1035 #if CONFIG_CORELOCK == CORELOCK_SWAP
1036 /* peek at lock until it's no longer busy */
1037 unsigned int locked
;
1038 while ((locked
= xchg8(&m
->locked
, STATE_BUSYu8
)) == STATE_BUSYu8
);
1046 /* Block until the lock is open... */
1047 cores
[core
].blk_ops
.flags
= TBOP_SET_VARu8
;
1048 cores
[core
].blk_ops
.var_u8p
= &m
->locked
;
1049 cores
[core
].blk_ops
.var_u8v
= 1;
1051 corelock_lock(&m
->cl
);
1056 corelock_unlock(&m
->cl
);
1060 /* Block until the lock is open... */
1061 #if CONFIG_CORELOCK == SW_CORELOCK
1062 cores
[core
].blk_ops
.flags
= TBOP_UNLOCK_CORELOCK
;
1063 cores
[core
].blk_ops
.cl_p
= &m
->cl
;
1065 #endif /* CONFIG_CORELOCK */
1067 block_thread_no_listlock(&m
->queue
);
1070 void mutex_unlock(struct mutex
*m
)
1072 /* unlocker not being the owner is an unlocking violation */
1073 KERNEL_ASSERT(m
->thread
== cores
[CURRENT_CORE
].running
,
1074 "mutex_unlock->wrong thread (recurse)");
1078 /* this thread still owns lock */
1083 #if CONFIG_CORELOCK == SW_CORELOCK
1084 /* lock out other cores */
1085 corelock_lock(&m
->cl
);
1086 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1087 /* wait for peeker to move on */
1088 while (xchg8(&m
->locked
, STATE_BUSYu8
) == STATE_BUSYu8
);
1091 /* transfer to next queued thread if any */
1093 /* This can become busy using SWP but is safe since only one thread
1094 will be changing things at a time. Allowing timeout waits will
1095 change that however but not now. There is also a hazard the thread
1096 could be killed before performing the wakeup but that's just
1097 irresponsible. :-) */
1098 m
->thread
= m
->queue
;
1100 if(m
->thread
== NULL
)
1102 m
->locked
= 0; /* release lock */
1103 #if CONFIG_CORELOCK == SW_CORELOCK
1104 corelock_unlock(&m
->cl
);
1107 else /* another thread is waiting - remain locked */
1109 wakeup_thread_no_listlock(&m
->queue
);
1110 #if CONFIG_CORELOCK == SW_CORELOCK
1111 corelock_unlock(&m
->cl
);
1112 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1118 /****************************************************************************
1119 * Simpl-er mutex functions ;)
1120 ****************************************************************************/
1122 void spinlock_init(struct spinlock
*l
)
1124 corelock_init(&l
->cl
);
1129 void spinlock_lock(struct spinlock
*l
)
1131 struct thread_entry
*const thread
= cores
[CURRENT_CORE
].running
;
1133 if (l
->thread
== thread
)
1139 corelock_lock(&l
->cl
);
1144 void spinlock_unlock(struct spinlock
*l
)
1146 /* unlocker not being the owner is an unlocking violation */
1147 KERNEL_ASSERT(l
->thread
== cores
[CURRENT_CORE
].running
,
1148 "spinlock_unlock->wrong thread");
1152 /* this thread still owns lock */
1161 corelock_unlock(&l
->cl
);
1163 #endif /* NUM_CORES > 1 */
1165 /****************************************************************************
1166 * Simple semaphore functions ;)
1167 ****************************************************************************/
1168 #ifdef HAVE_SEMAPHORE_OBJECTS
1169 void semaphore_init(struct semaphore
*s
, int max
, int start
)
1171 KERNEL_ASSERT(max
> 0 && start
>= 0 && start
<= max
,
1172 "semaphore_init->inv arg");
1176 #if CONFIG_CORELOCK == SW_CORELOCK
1177 corelock_init(&s
->cl
);
1181 void semaphore_wait(struct semaphore
*s
)
1183 #if CONFIG_CORELOCK == CORELOCK_NONE || CONFIG_CORELOCK == SW_CORELOCK
1184 corelock_lock(&s
->cl
);
1187 corelock_unlock(&s
->cl
);
1190 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1192 while ((count
= xchg32(&s
->count
, STATE_BUSYi
)) == STATE_BUSYi
);
1200 /* too many waits - block until dequeued */
1201 #if CONFIG_CORELOCK == SW_CORELOCK
1202 const unsigned int core
= CURRENT_CORE
;
1203 cores
[core
].blk_ops
.flags
= TBOP_UNLOCK_CORELOCK
;
1204 cores
[core
].blk_ops
.cl_p
= &s
->cl
;
1205 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1206 const unsigned int core
= CURRENT_CORE
;
1207 cores
[core
].blk_ops
.flags
= TBOP_SET_VARi
;
1208 cores
[core
].blk_ops
.var_ip
= &s
->count
;
1209 cores
[core
].blk_ops
.var_iv
= count
;
1211 block_thread_no_listlock(&s
->queue
);
1214 void semaphore_release(struct semaphore
*s
)
1216 #if CONFIG_CORELOCK == CORELOCK_NONE || CONFIG_CORELOCK == SW_CORELOCK
1217 corelock_lock(&s
->cl
);
1218 if (s
->count
< s
->max
)
1220 if (++s
->count
<= 0)
1222 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1224 while ((count
= xchg32(&s
->count
, STATE_BUSYi
)) == STATE_BUSYi
);
1229 #endif /* CONFIG_CORELOCK */
1231 /* there should be threads in this queue */
1232 KERNEL_ASSERT(s
->queue
!= NULL
, "semaphore->wakeup");
1233 /* a thread was queued - wake it up */
1234 wakeup_thread_no_listlock(&s
->queue
);
1238 #if CONFIG_CORELOCK == SW_CORELOCK
1239 corelock_unlock(&s
->cl
);
1240 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1244 #endif /* HAVE_SEMAPHORE_OBJECTS */
1246 /****************************************************************************
1247 * Simple event functions ;)
1248 ****************************************************************************/
1249 #ifdef HAVE_EVENT_OBJECTS
1250 void event_init(struct event
*e
, unsigned int flags
)
1252 e
->queues
[STATE_NONSIGNALED
] = NULL
;
1253 e
->queues
[STATE_SIGNALED
] = NULL
;
1254 e
->state
= flags
& STATE_SIGNALED
;
1255 e
->automatic
= (flags
& EVENT_AUTOMATIC
) ? 1 : 0;
1256 #if CONFIG_CORELOCK == SW_CORELOCK
1257 corelock_init(&e
->cl
);
1261 void event_wait(struct event
*e
, unsigned int for_state
)
1263 unsigned int last_state
;
1264 #if CONFIG_CORELOCK == CORELOCK_NONE || CONFIG_CORELOCK == SW_CORELOCK
1265 corelock_lock(&e
->cl
);
1266 last_state
= e
->state
;
1267 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1268 while ((last_state
= xchg8(&e
->state
, STATE_BUSYu8
)) == STATE_BUSYu8
);
1271 if(e
->automatic
!= 0)
1273 /* wait for false always satisfied by definition
1274 or if it just changed to false */
1275 if(last_state
== STATE_SIGNALED
|| for_state
== STATE_NONSIGNALED
)
1277 /* automatic - unsignal */
1278 e
->state
= STATE_NONSIGNALED
;
1279 #if CONFIG_CORELOCK == SW_CORELOCK
1280 corelock_unlock(&e
->cl
);
1284 /* block until state matches */
1286 else if(for_state
== last_state
)
1288 /* the state being waited for is the current state */
1289 #if CONFIG_CORELOCK == SW_CORELOCK
1290 corelock_unlock(&e
->cl
);
1291 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1292 e
->state
= last_state
;
1298 /* current state does not match wait-for state */
1299 #if CONFIG_CORELOCK == SW_CORELOCK
1300 const unsigned int core
= CURRENT_CORE
;
1301 cores
[core
].blk_ops
.flags
= TBOP_UNLOCK_CORELOCK
;
1302 cores
[core
].blk_ops
.cl_p
= &e
->cl
;
1303 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1304 const unsigned int core
= CURRENT_CORE
;
1305 cores
[core
].blk_ops
.flags
= TBOP_SET_VARu8
;
1306 cores
[core
].blk_ops
.var_u8p
= &e
->state
;
1307 cores
[core
].blk_ops
.var_u8v
= last_state
;
1309 block_thread_no_listlock(&e
->queues
[for_state
]);
1313 void event_set_state(struct event
*e
, unsigned int state
)
1315 unsigned int last_state
;
1316 #if CONFIG_CORELOCK == CORELOCK_NONE || CONFIG_CORELOCK == SW_CORELOCK
1317 corelock_lock(&e
->cl
);
1318 last_state
= e
->state
;
1319 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1320 while ((last_state
= xchg8(&e
->state
, STATE_BUSYu8
)) == STATE_BUSYu8
);
1323 if(last_state
== state
)
1326 #if CONFIG_CORELOCK == SW_CORELOCK
1327 corelock_unlock(&e
->cl
);
1328 #elif CONFIG_CORELOCK == CORELOCK_SWAP
1329 e
->state
= last_state
;
1334 if(state
== STATE_SIGNALED
)
1336 if(e
->automatic
!= 0)
1338 struct thread_entry
*thread
;
1339 /* no thread should have ever blocked for unsignaled */
1340 KERNEL_ASSERT(e
->queues
[STATE_NONSIGNALED
] == NULL
,
1341 "set_event_state->queue[NS]:S");
1342 /* pass to next thread and keep unsignaled - "pulse" */
1343 thread
= wakeup_thread_no_listlock(&e
->queues
[STATE_SIGNALED
]);
1344 e
->state
= thread
!= NULL
? STATE_NONSIGNALED
: STATE_SIGNALED
;
1348 /* release all threads waiting for signaled */
1349 thread_queue_wake_no_listlock(&e
->queues
[STATE_SIGNALED
]);
1350 e
->state
= STATE_SIGNALED
;
1355 /* release all threads waiting for unsignaled */
1357 /* no thread should have ever blocked if automatic */
1358 KERNEL_ASSERT(e
->queues
[STATE_NONSIGNALED
] == NULL
||
1359 e
->automatic
== 0, "set_event_state->queue[NS]:NS");
1361 thread_queue_wake_no_listlock(&e
->queues
[STATE_NONSIGNALED
]);
1362 e
->state
= STATE_NONSIGNALED
;
1365 #if CONFIG_CORELOCK == SW_CORELOCK
1366 corelock_unlock(&e
->cl
);
1369 #endif /* HAVE_EVENT_OBJECTS */