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/assert.h>
27 #include <cobalt/kernel/ancillaries.h>
28 #include <asm/xenomai/wrappers.h>
34 #define XN_INFINITE ((xnticks_t)0)
35 #define XN_NONBLOCK ((xnticks_t)-1)
38 typedef enum xntmode {
45 #define XNTIMER_DEQUEUED 0x00000001
46 #define XNTIMER_KILLED 0x00000002
47 #define XNTIMER_PERIODIC 0x00000004
48 #define XNTIMER_REALTIME 0x00000008
49 #define XNTIMER_FIRED 0x00000010
50 #define XNTIMER_NOBLCK 0x00000020
51 #define XNTIMER_RUNNING 0x00000040
52 #define XNTIMER_KGRAVITY 0x00000080
53 #define XNTIMER_UGRAVITY 0x00000100
54 #define XNTIMER_IGRAVITY 0
56 #define XNTIMER_GRAVITY_MASK (XNTIMER_KGRAVITY|XNTIMER_UGRAVITY)
57 #define XNTIMER_INIT_MASK (XNTIMER_GRAVITY_MASK|XNTIMER_NOBLCK)
59 #define __XNTIMER_CORE 0x10000000
62 #define XNTIMER_SPARE0 0x01000000
63 #define XNTIMER_SPARE1 0x02000000
64 #define XNTIMER_SPARE2 0x04000000
65 #define XNTIMER_SPARE3 0x08000000
66 #define XNTIMER_SPARE4 0x10000000
67 #define XNTIMER_SPARE5 0x20000000
68 #define XNTIMER_SPARE6 0x40000000
69 #define XNTIMER_SPARE7 0x80000000
72 #define XNTIMER_LOPRIO (-999999999)
73 #define XNTIMER_STDPRIO 0
74 #define XNTIMER_HIPRIO 999999999
77 struct list_head link;
82 #define xntlholder_date(h) ((h)->key)
83 #define xntlholder_prio(h) ((h)->prio)
84 #define xntlist_init(q) INIT_LIST_HEAD(q)
85 #define xntlist_empty(q) list_empty(q)
87 static inline struct xntlholder *xntlist_head(
struct list_head *q)
92 return list_first_entry(q,
struct xntlholder, link);
95 static inline struct xntlholder *xntlist_next(
struct list_head *q,
98 if (list_is_last(&h->link, q))
101 return list_entry(h->link.next,
struct xntlholder, link);
104 static inline struct xntlholder *xntlist_second(
struct list_head *q)
106 struct xntlholder *h;
111 h = list_first_entry(q,
struct xntlholder, link);
113 return xntlist_next(q, h);
116 static inline void xntlist_insert(
struct list_head *q,
struct xntlholder *holder)
118 struct xntlholder *p;
121 list_add(&holder->link, q);
130 list_for_each_entry_reverse(p, q, link) {
131 if ((xnsticks_t) (holder->key - p->key) > 0 ||
132 (holder->key == p->key && holder->prio <= p->prio))
136 list_add(&holder->link, &p->link);
139 #define xntlist_remove(q, h) \
142 list_del(&(h)->link); \
145 #if defined(CONFIG_XENO_OPT_TIMER_HEAP)
147 #include <cobalt/kernel/bheap.h>
149 typedef bheaph_t xntimerh_t;
151 #define xntimerh_date(h) bheaph_key(h)
152 #define xntimerh_prio(h) bheaph_prio(h)
153 #define xntimerh_init(h) bheaph_init(h)
155 typedef DECLARE_BHEAP_CONTAINER(xntimerq_t, CONFIG_XENO_OPT_TIMER_HEAP_CAPACITY);
157 #define xntimerq_init(q) bheap_init((q), CONFIG_XENO_OPT_TIMER_HEAP_CAPACITY)
158 #define xntimerq_destroy(q) bheap_destroy(q)
159 #define xntimerq_empty(q) bheap_empty(q)
160 #define xntimerq_head(q) bheap_gethead(q)
161 #define xntimerq_second(q) bheap_second(q)
162 #define xntimerq_insert(q, h) bheap_insert((q),(h))
163 #define xntimerq_remove(q, h) bheap_delete((q),(h))
165 typedef struct {} xntimerq_it_t;
172 #define xntimerq_it_begin(q, i) ((void) (i), bheap_gethead(q))
173 #define xntimerq_it_next(q, i, h) ((void) (i), bheap_next((q),(h)))
177 typedef struct xntlholder xntimerh_t;
179 #define xntimerh_date(h) xntlholder_date(h)
180 #define xntimerh_prio(h) xntlholder_prio(h)
181 #define xntimerh_init(h) do { } while (0)
183 typedef struct list_head xntimerq_t;
185 #define xntimerq_init(q) xntlist_init(q)
186 #define xntimerq_destroy(q) do { } while (0)
187 #define xntimerq_empty(q) xntlist_empty(q)
188 #define xntimerq_head(q) xntlist_head(q)
189 #define xntimerq_second(q) xntlist_second(q)
190 #define xntimerq_insert(q, h) xntlist_insert((q),(h))
191 #define xntimerq_remove(q, h) xntlist_remove((q),(h))
193 typedef struct { } xntimerq_it_t;
195 #define xntimerq_it_begin(q,i) ((void) (i), xntlist_head(q))
196 #define xntimerq_it_next(q,i,h) ((void) (i), xntlist_next((q),(h)))
206 static inline struct xntimerdata *
207 xnclock_percpu_timerdata(
struct xnclock *clock,
int cpu)
209 return per_cpu_ptr(clock->timerdata, cpu);
212 static inline struct xntimerdata *
213 xnclock_this_timerdata(
struct xnclock *clock)
215 return raw_cpu_ptr(clock->timerdata);
219 #ifdef CONFIG_XENO_OPT_EXTCLOCK
220 struct xnclock *clock;
224 struct list_head adjlink;
226 unsigned long status;
230 xnticks_t interval_ns;
232 xnticks_t periodic_ticks;
234 xnticks_t start_date;
236 xnticks_t pexpect_ticks;
240 void (*handler)(
struct xntimer *timer);
241 #ifdef CONFIG_XENO_OPT_STATS
242 #ifdef CONFIG_XENO_OPT_EXTCLOCK
243 struct xnclock *tracker;
246 char name[XNOBJECT_NAME_LEN];
248 struct list_head next_stat;
250 xnstat_counter_t scheduled;
252 xnstat_counter_t fired;
256 #ifdef CONFIG_XENO_OPT_EXTCLOCK
258 static inline struct xnclock *xntimer_clock(
struct xntimer *timer)
263 void xntimer_set_clock(
struct xntimer *timer,
264 struct xnclock *newclock);
268 static inline struct xnclock *xntimer_clock(
struct xntimer *timer)
273 static inline void xntimer_set_clock(
struct xntimer *timer,
274 struct xnclock *newclock)
276 XENO_BUG_ON(COBALT, newclock != &nkclock);
282 static inline struct xnsched *xntimer_sched(
struct xntimer *timer)
287 #define xntimer_sched(t) xnsched_current()
290 #define xntimer_percpu_queue(__timer) \
292 struct xntimerdata *tmd; \
293 int cpu = xnsched_cpu((__timer)->sched); \
294 tmd = xnclock_percpu_timerdata(xntimer_clock(__timer), cpu); \
298 static inline xntimerq_t *xntimer_this_queue(
struct xntimer *timer)
300 struct xntimerdata *tmd;
302 tmd = xnclock_this_timerdata(xntimer_clock(timer));
307 static inline unsigned long xntimer_gravity(
struct xntimer *timer)
309 struct xnclock *clock = xntimer_clock(timer);
311 if (timer->status & XNTIMER_KGRAVITY)
312 return clock->gravity.kernel;
314 if (timer->status & XNTIMER_UGRAVITY)
315 return clock->gravity.user;
317 return clock->gravity.irq;
320 static inline void xntimer_update_date(
struct xntimer *timer)
322 xntimerh_date(&timer->aplink) = timer->start_date
323 + xnclock_ns_to_ticks(xntimer_clock(timer),
324 timer->periodic_ticks * timer->interval_ns)
325 - xntimer_gravity(timer);
328 static inline xnticks_t xntimer_pexpect(
struct xntimer *timer)
330 return timer->start_date +
331 xnclock_ns_to_ticks(xntimer_clock(timer),
332 timer->pexpect_ticks * timer->interval_ns);
335 static inline void xntimer_set_priority(
struct xntimer *timer,
338 xntimerh_prio(&timer->aplink) = prio;
341 static inline int xntimer_active_p(
struct xntimer *timer)
343 return timer->sched != NULL;
346 static inline int xntimer_running_p(
struct xntimer *timer)
348 return (timer->status & XNTIMER_RUNNING) != 0;
351 static inline int xntimer_fired_p(
struct xntimer *timer)
353 return (timer->status & XNTIMER_FIRED) != 0;
356 static inline int xntimer_periodic_p(
struct xntimer *timer)
358 return (timer->status & XNTIMER_PERIODIC) != 0;
361 void __xntimer_init(
struct xntimer *timer,
362 struct xnclock *clock,
363 void (*handler)(
struct xntimer *timer),
367 void xntimer_set_gravity(
struct xntimer *timer,
370 #ifdef CONFIG_XENO_OPT_STATS
372 #define xntimer_init(__timer, __clock, __handler, __sched, __flags) \
374 __xntimer_init(__timer, __clock, __handler, __sched, __flags); \
375 xntimer_set_name(__timer, #__handler); \
378 static inline void xntimer_reset_stats(
struct xntimer *timer)
380 xnstat_counter_set(&timer->scheduled, 0);
381 xnstat_counter_set(&timer->fired, 0);
384 static inline void xntimer_account_scheduled(
struct xntimer *timer)
386 xnstat_counter_inc(&timer->scheduled);
389 static inline void xntimer_account_fired(
struct xntimer *timer)
391 xnstat_counter_inc(&timer->fired);
394 static inline void xntimer_set_name(
struct xntimer *timer,
const char *name)
396 knamecpy(timer->name, name);
401 #define xntimer_init __xntimer_init
403 static inline void xntimer_reset_stats(
struct xntimer *timer) { }
405 static inline void xntimer_account_scheduled(
struct xntimer *timer) { }
407 static inline void xntimer_account_fired(
struct xntimer *timer) { }
409 static inline void xntimer_set_name(
struct xntimer *timer,
const char *name) { }
413 #if defined(CONFIG_XENO_OPT_EXTCLOCK) && defined(CONFIG_XENO_OPT_STATS)
414 void xntimer_switch_tracking(
struct xntimer *timer,
415 struct xnclock *newclock);
418 void xntimer_switch_tracking(
struct xntimer *timer,
419 struct xnclock *newclock) { }
441 return timer->interval_ns;
444 static inline xnticks_t xntimer_expiry(
struct xntimer *timer)
447 return xntimerh_date(&timer->aplink) + xntimer_gravity(timer);
455 void __xntimer_stop(
struct xntimer *timer);
461 xnticks_t xntimer_get_interval(
struct xntimer *timer);
463 int xntimer_heading_p(
struct xntimer *timer);
467 if (timer->status & XNTIMER_RUNNING)
468 __xntimer_stop(timer);
471 static inline xnticks_t xntimer_get_timeout_stopped(
struct xntimer *timer)
476 static inline void xntimer_enqueue(
struct xntimer *timer,
479 xntimerq_insert(q, &timer->aplink);
480 timer->status &= ~XNTIMER_DEQUEUED;
481 xntimer_account_scheduled(timer);
484 static inline void xntimer_dequeue(
struct xntimer *timer,
487 xntimerq_remove(q, &timer->aplink);
488 timer->status |= XNTIMER_DEQUEUED;
500 void xntimer_migrate(
struct xntimer *timer,
struct xnsched *sched)
502 if (timer->sched != sched)
506 int xntimer_setup_ipi(
void);
508 void xntimer_release_ipi(
void);
512 static inline void xntimer_migrate(
struct xntimer *timer,
516 static inline int xntimer_setup_ipi(
void)
521 static inline void xntimer_release_ipi(
void) { }
525 static inline void xntimer_set_sched(
struct xntimer *timer,
528 xntimer_migrate(timer, sched);
531 char *xntimer_format_time(xnticks_t ns,
532 char *buf,
size_t bufsz);
xnticks_t xntimer_get_timeout(struct xntimer *timer)
Return the relative expiration date.
Definition: timer.c:269
xnticks_t xntimer_get_date(struct xntimer *timer)
Return the absolute expiration date.
Definition: timer.c:242
void __xntimer_migrate(struct xntimer *timer, struct xnsched *sched)
Migrate a timer.
Definition: timer.c:528
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:113
void xntimer_destroy(struct xntimer *timer)
Release a timer object.
Definition: timer.c:494
unsigned long long xntimer_get_overruns(struct xntimer *timer, xnticks_t now)
Get the count of overruns for the last tick.
Definition: timer.c:593
void xntimer_release_hardware(void)
Release hardware timers.
Definition: timer.c:908
static xnticks_t xntimer_interval(struct xntimer *timer)
Return the timer interval value.
Definition: timer.h:439
static void xntimer_stop(struct xntimer *timer)
Disarm a timer.
Definition: timer.h:465
int xntimer_grab_hardware(void)
Grab the hardware timer on all real-time CPUs.
Definition: timer.c:811