20 #ifndef _COBALT_KERNEL_TIMER_H
21 #define _COBALT_KERNEL_TIMER_H
23 #include <cobalt/kernel/clock.h>
24 #include <cobalt/kernel/stat.h>
25 #include <cobalt/kernel/list.h>
26 #include <cobalt/kernel/ancillaries.h>
27 #include <asm/xenomai/wrappers.h>
33 #define XN_INFINITE ((xnticks_t)0)
34 #define XN_NONBLOCK ((xnticks_t)-1)
37 typedef enum xntmode {
44 #define XNTIMER_DEQUEUED 0x00000001
45 #define XNTIMER_KILLED 0x00000002
46 #define XNTIMER_PERIODIC 0x00000004
47 #define XNTIMER_REALTIME 0x00000008
48 #define XNTIMER_FIRED 0x00000010
49 #define XNTIMER_NOBLCK 0x00000020
50 #define XNTIMER_RUNNING 0x00000040
51 #define XNTIMER_KGRAVITY 0x00000080
52 #define XNTIMER_UGRAVITY 0x00000100
53 #define XNTIMER_IGRAVITY 0
55 #define XNTIMER_GRAVITY_MASK (XNTIMER_KGRAVITY|XNTIMER_UGRAVITY)
56 #define XNTIMER_INIT_MASK (XNTIMER_GRAVITY_MASK|XNTIMER_NOBLCK)
59 #define XNTIMER_SPARE0 0x01000000
60 #define XNTIMER_SPARE1 0x02000000
61 #define XNTIMER_SPARE2 0x04000000
62 #define XNTIMER_SPARE3 0x08000000
63 #define XNTIMER_SPARE4 0x10000000
64 #define XNTIMER_SPARE5 0x20000000
65 #define XNTIMER_SPARE6 0x40000000
66 #define XNTIMER_SPARE7 0x80000000
69 #define XNTIMER_LOPRIO (-999999999)
70 #define XNTIMER_STDPRIO 0
71 #define XNTIMER_HIPRIO 999999999
74 struct list_head link;
79 #define xntlholder_date(h) ((h)->key)
80 #define xntlholder_prio(h) ((h)->prio)
81 #define xntlist_init(q) INIT_LIST_HEAD(q)
82 #define xntlist_empty(q) list_empty(q)
84 static inline struct xntlholder *xntlist_head(
struct list_head *q)
89 return list_first_entry(q,
struct xntlholder, link);
92 static inline struct xntlholder *xntlist_next(
struct list_head *q,
95 if (list_is_last(&h->link, q))
98 return list_entry(h->link.next,
struct xntlholder, link);
101 static inline struct xntlholder *xntlist_second(
struct list_head *q)
103 struct xntlholder *h;
108 h = list_first_entry(q,
struct xntlholder, link);
110 return xntlist_next(q, h);
113 static inline void xntlist_insert(
struct list_head *q,
struct xntlholder *holder)
115 struct xntlholder *p;
118 list_add(&holder->link, q);
127 list_for_each_entry_reverse(p, q, link) {
128 if ((xnsticks_t) (holder->key - p->key) > 0 ||
129 (holder->key == p->key && holder->prio <= p->prio))
133 list_add(&holder->link, &p->link);
136 #define xntlist_remove(q, h) \
139 list_del(&(h)->link); \
142 #if defined(CONFIG_XENO_OPT_TIMER_HEAP)
144 #include <cobalt/kernel/bheap.h>
146 typedef bheaph_t xntimerh_t;
148 #define xntimerh_date(h) bheaph_key(h)
149 #define xntimerh_prio(h) bheaph_prio(h)
150 #define xntimerh_init(h) bheaph_init(h)
152 typedef DECLARE_BHEAP_CONTAINER(xntimerq_t, CONFIG_XENO_OPT_TIMER_HEAP_CAPACITY);
154 #define xntimerq_init(q) bheap_init((q), CONFIG_XENO_OPT_TIMER_HEAP_CAPACITY)
155 #define xntimerq_destroy(q) bheap_destroy(q)
156 #define xntimerq_empty(q) bheap_empty(q)
157 #define xntimerq_head(q) bheap_gethead(q)
158 #define xntimerq_second(q) bheap_second(q)
159 #define xntimerq_insert(q, h) bheap_insert((q),(h))
160 #define xntimerq_remove(q, h) bheap_delete((q),(h))
162 typedef struct {} xntimerq_it_t;
169 #define xntimerq_it_begin(q, i) ((void) (i), bheap_gethead(q))
170 #define xntimerq_it_next(q, i, h) ((void) (i), bheap_next((q),(h)))
174 typedef struct xntlholder xntimerh_t;
176 #define xntimerh_date(h) xntlholder_date(h)
177 #define xntimerh_prio(h) xntlholder_prio(h)
178 #define xntimerh_init(h) do { } while (0)
180 typedef struct list_head xntimerq_t;
182 #define xntimerq_init(q) xntlist_init(q)
183 #define xntimerq_destroy(q) do { } while (0)
184 #define xntimerq_empty(q) xntlist_empty(q)
185 #define xntimerq_head(q) xntlist_head(q)
186 #define xntimerq_second(q) xntlist_second(q)
187 #define xntimerq_insert(q, h) xntlist_insert((q),(h))
188 #define xntimerq_remove(q, h) xntlist_remove((q),(h))
190 typedef struct { } xntimerq_it_t;
192 #define xntimerq_it_begin(q,i) ((void) (i), xntlist_head(q))
193 #define xntimerq_it_next(q,i,h) ((void) (i), xntlist_next((q),(h)))
203 static inline struct xntimerdata *
204 xnclock_percpu_timerdata(
struct xnclock *clock,
int cpu)
206 return per_cpu_ptr(clock->timerdata, cpu);
209 static inline struct xntimerdata *
210 xnclock_this_timerdata(
struct xnclock *clock)
212 return raw_cpu_ptr(clock->timerdata);
216 #ifdef CONFIG_XENO_OPT_EXTCLOCK
217 struct xnclock *clock;
221 struct list_head adjlink;
223 unsigned long status;
227 xnticks_t interval_ns;
229 xnticks_t periodic_ticks;
231 xnticks_t start_date;
233 xnticks_t pexpect_ticks;
237 void (*handler)(
struct xntimer *timer);
238 #ifdef CONFIG_XENO_OPT_STATS
239 #ifdef CONFIG_XENO_OPT_EXTCLOCK
240 struct xnclock *tracker;
243 char name[XNOBJECT_NAME_LEN];
245 struct list_head next_stat;
247 xnstat_counter_t scheduled;
249 xnstat_counter_t fired;
253 #ifdef CONFIG_XENO_OPT_EXTCLOCK
255 static inline struct xnclock *xntimer_clock(
struct xntimer *timer)
262 static inline struct xnclock *xntimer_clock(
struct xntimer *timer)
270 static inline struct xnsched *xntimer_sched(
struct xntimer *timer)
275 #define xntimer_sched(t) xnsched_current()
278 #define xntimer_percpu_queue(__timer) \
280 struct xntimerdata *tmd; \
281 int cpu = xnsched_cpu((__timer)->sched); \
282 tmd = xnclock_percpu_timerdata(xntimer_clock(__timer), cpu); \
286 static inline xntimerq_t *xntimer_this_queue(
struct xntimer *timer)
288 struct xntimerdata *tmd;
290 tmd = xnclock_this_timerdata(xntimer_clock(timer));
295 static inline unsigned long xntimer_gravity(
struct xntimer *timer)
297 struct xnclock *clock = xntimer_clock(timer);
299 if (timer->status & XNTIMER_KGRAVITY)
300 return clock->gravity.kernel;
302 if (timer->status & XNTIMER_UGRAVITY)
303 return clock->gravity.user;
305 return clock->gravity.irq;
308 static inline void xntimer_update_date(
struct xntimer *timer)
310 xntimerh_date(&timer->aplink) = timer->start_date
311 + xnclock_ns_to_ticks(xntimer_clock(timer),
312 timer->periodic_ticks * timer->interval_ns)
313 - xntimer_gravity(timer);
316 static inline xnticks_t xntimer_pexpect(
struct xntimer *timer)
318 return timer->start_date +
319 xnclock_ns_to_ticks(xntimer_clock(timer),
320 timer->pexpect_ticks * timer->interval_ns);
323 static inline void xntimer_set_priority(
struct xntimer *timer,
326 xntimerh_prio(&timer->aplink) = prio;
329 static inline int xntimer_active_p(
struct xntimer *timer)
331 return timer->sched != NULL;
334 static inline int xntimer_running_p(
struct xntimer *timer)
336 return (timer->status & XNTIMER_RUNNING) != 0;
339 static inline int xntimer_fired_p(
struct xntimer *timer)
341 return (timer->status & XNTIMER_FIRED) != 0;
344 static inline int xntimer_periodic_p(
struct xntimer *timer)
346 return (timer->status & XNTIMER_PERIODIC) != 0;
349 void __xntimer_init(
struct xntimer *timer,
350 struct xnclock *clock,
351 void (*handler)(
struct xntimer *timer),
355 void xntimer_set_gravity(
struct xntimer *timer,
358 #ifdef CONFIG_XENO_OPT_STATS
360 #define xntimer_init(__timer, __clock, __handler, __sched, __flags) \
362 __xntimer_init(__timer, __clock, __handler, __sched, __flags); \
363 xntimer_set_name(__timer, #__handler); \
366 static inline void xntimer_reset_stats(
struct xntimer *timer)
368 xnstat_counter_set(&timer->scheduled, 0);
369 xnstat_counter_set(&timer->fired, 0);
372 static inline void xntimer_account_scheduled(
struct xntimer *timer)
374 xnstat_counter_inc(&timer->scheduled);
377 static inline void xntimer_account_fired(
struct xntimer *timer)
379 xnstat_counter_inc(&timer->fired);
382 static inline void xntimer_set_name(
struct xntimer *timer,
const char *name)
384 knamecpy(timer->name, name);
389 #define xntimer_init __xntimer_init
391 static inline void xntimer_reset_stats(
struct xntimer *timer) { }
393 static inline void xntimer_account_scheduled(
struct xntimer *timer) { }
395 static inline void xntimer_account_fired(
struct xntimer *timer) { }
397 static inline void xntimer_set_name(
struct xntimer *timer,
const char *name) { }
401 #if defined(CONFIG_XENO_OPT_EXTCLOCK) && defined(CONFIG_XENO_OPT_STATS)
402 void xntimer_switch_tracking(
struct xntimer *timer,
403 struct xnclock *newclock);
406 void xntimer_switch_tracking(
struct xntimer *timer,
407 struct xnclock *newclock) { }
429 return timer->interval_ns;
432 static inline xnticks_t xntimer_expiry(
struct xntimer *timer)
435 return xntimerh_date(&timer->aplink) + xntimer_gravity(timer);
443 void __xntimer_stop(
struct xntimer *timer);
449 xnticks_t xntimer_get_interval(
struct xntimer *timer);
451 int xntimer_heading_p(
struct xntimer *timer);
455 if (timer->status & XNTIMER_RUNNING)
456 __xntimer_stop(timer);
459 static inline xnticks_t xntimer_get_timeout_stopped(
struct xntimer *timer)
464 static inline void xntimer_enqueue(
struct xntimer *timer,
467 xntimerq_insert(q, &timer->aplink);
468 timer->status &= ~XNTIMER_DEQUEUED;
469 xntimer_account_scheduled(timer);
472 static inline void xntimer_dequeue(
struct xntimer *timer,
475 xntimerq_remove(q, &timer->aplink);
476 timer->status |= XNTIMER_DEQUEUED;
488 void xntimer_migrate(
struct xntimer *timer,
struct xnsched *sched)
490 if (timer->sched != sched)
494 int xntimer_setup_ipi(
void);
496 void xntimer_release_ipi(
void);
500 static inline void xntimer_migrate(
struct xntimer *timer,
504 static inline int xntimer_setup_ipi(
void)
509 static inline void xntimer_release_ipi(
void) { }
513 static inline void xntimer_set_sched(
struct xntimer *timer,
516 xntimer_migrate(timer, sched);
519 char *xntimer_format_time(xnticks_t ns,
520 char *buf,
size_t bufsz);
xnticks_t xntimer_get_timeout(struct xntimer *timer)
Return the relative expiration date.
Definition: timer.c:265
xnticks_t xntimer_get_date(struct xntimer *timer)
Return the absolute expiration date.
Definition: timer.c:238
void __xntimer_migrate(struct xntimer *timer, struct xnsched *sched)
Migrate a timer.
Definition: timer.c:463
Scheduling information structure.
Definition: sched.h:57
int xntimer_start(struct xntimer *timer, xnticks_t value, xnticks_t interval, xntmode_t mode)
Arm a timer.
Definition: timer.c:100
void xntimer_destroy(struct xntimer *timer)
Release a timer object.
Definition: timer.c:429
unsigned long long xntimer_get_overruns(struct xntimer *timer, xnticks_t now)
Get the count of overruns for the last tick.
Definition: timer.c:518
void xntimer_release_hardware(void)
Release hardware timers.
Definition: timer.c:821
static xnticks_t xntimer_interval(struct xntimer *timer)
Return the timer interval value.
Definition: timer.h:427
static void xntimer_stop(struct xntimer *timer)
Disarm a timer.
Definition: timer.h:453
int xntimer_grab_hardware(void)
Grab the hardware timer on all real-time CPUs.
Definition: timer.c:724