added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / include / linux / hrtimer.h
blob8bafa3570ca6724e5d997a6edf5ea55c12f13596
1 /*
2 * include/linux/hrtimer.h
4 * hrtimers - High-resolution kernel timers
6 * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
7 * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
9 * data type definitions, declarations, prototypes
11 * Started by: Thomas Gleixner and Ingo Molnar
13 * For licencing details see kernel-base/COPYING
15 #ifndef _LINUX_HRTIMER_H
16 #define _LINUX_HRTIMER_H
18 #include <linux/rbtree.h>
19 #include <linux/ktime.h>
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/wait.h>
23 #include <linux/percpu.h>
26 struct hrtimer_clock_base;
27 struct hrtimer_cpu_base;
30 * Mode arguments of xxx_hrtimer functions:
32 enum hrtimer_mode {
33 HRTIMER_MODE_ABS, /* Time value is absolute */
34 HRTIMER_MODE_REL, /* Time value is relative to now */
38 * Return values for the callback function
40 enum hrtimer_restart {
41 HRTIMER_NORESTART, /* Timer is not restarted */
42 HRTIMER_RESTART, /* Timer must be restarted */
46 * Values to track state of the timer
48 * Possible states:
50 * 0x00 inactive
51 * 0x01 enqueued into rbtree
52 * 0x02 callback function running
54 * Special cases:
55 * 0x03 callback function running and enqueued
56 * (was requeued on another CPU)
57 * 0x09 timer was migrated on CPU hotunplug
58 * The "callback function running and enqueued" status is only possible on
59 * SMP. It happens for example when a posix timer expired and the callback
60 * queued a signal. Between dropping the lock which protects the posix timer
61 * and reacquiring the base lock of the hrtimer, another CPU can deliver the
62 * signal and rearm the timer. We have to preserve the callback running state,
63 * as otherwise the timer could be removed before the softirq code finishes the
64 * the handling of the timer.
66 * The HRTIMER_STATE_ENQUEUED bit is always or'ed to the current state to
67 * preserve the HRTIMER_STATE_CALLBACK bit in the above scenario.
69 * All state transitions are protected by cpu_base->lock.
71 #define HRTIMER_STATE_INACTIVE 0x00
72 #define HRTIMER_STATE_ENQUEUED 0x01
73 #define HRTIMER_STATE_CALLBACK 0x02
74 #define HRTIMER_STATE_MIGRATE 0x04
76 /**
77 * struct hrtimer - the basic hrtimer structure
78 * @node: red black tree node for time ordered insertion
79 * @_expires: the absolute expiry time in the hrtimers internal
80 * representation. The time is related to the clock on
81 * which the timer is based. Is setup by adding
82 * slack to the _softexpires value. For non range timers
83 * identical to _softexpires.
84 * @_softexpires: the absolute earliest expiry time of the hrtimer.
85 * The time which was given as expiry time when the timer
86 * was armed.
87 * @function: timer expiry callback function
88 * @base: pointer to the timer base (per cpu and per clock)
89 * @state: state information (See bit values above)
90 * @cb_entry: list head to enqueue an expired timer into the callback list
91 * @start_site: timer statistics field to store the site where the timer
92 * was started
93 * @start_comm: timer statistics field to store the name of the process which
94 * started the timer
95 * @start_pid: timer statistics field to store the pid of the task which
96 * started the timer
98 * The hrtimer structure must be initialized by hrtimer_init()
100 struct hrtimer {
101 struct rb_node node;
102 ktime_t _expires;
103 ktime_t _softexpires;
104 enum hrtimer_restart (*function)(struct hrtimer *);
105 struct hrtimer_clock_base *base;
106 unsigned long state;
107 struct list_head cb_entry;
108 int irqsafe;
109 #ifdef CONFIG_TIMER_STATS
110 int start_pid;
111 void *start_site;
112 char start_comm[16];
113 #endif
117 * struct hrtimer_sleeper - simple sleeper structure
118 * @timer: embedded timer structure
119 * @task: task to wake up
121 * task is set to NULL, when the timer expires.
123 struct hrtimer_sleeper {
124 struct hrtimer timer;
125 struct task_struct *task;
129 * struct hrtimer_clock_base - the timer base for a specific clock
130 * @cpu_base: per cpu clock base
131 * @index: clock type index for per_cpu support when moving a
132 * timer to a base on another cpu.
133 * @active: red black tree root node for the active timers
134 * @first: pointer to the timer node which expires first
135 * @resolution: the resolution of the clock, in nanoseconds
136 * @get_time: function to retrieve the current time of the clock
137 * @softirq_time: the time when running the hrtimer queue in the softirq
138 * @offset: offset of this clock to the monotonic base
140 struct hrtimer_clock_base {
141 struct hrtimer_cpu_base *cpu_base;
142 clockid_t index;
143 struct rb_root active;
144 struct list_head expired;
145 struct rb_node *first;
146 ktime_t resolution;
147 ktime_t (*get_time)(void);
148 ktime_t softirq_time;
149 #ifdef CONFIG_HIGH_RES_TIMERS
150 ktime_t offset;
151 #endif
154 #define HRTIMER_MAX_CLOCK_BASES 2
157 * struct hrtimer_cpu_base - the per cpu clock bases
158 * @lock: lock protecting the base and associated clock bases
159 * and timers
160 * @clock_base: array of clock bases for this cpu
161 * @curr_timer: the timer which is executing a callback right now
162 * @expires_next: absolute time of the next event which was scheduled
163 * via clock_set_next_event()
164 * @hres_active: State of high resolution mode
165 * @check_clocks: Indictator, when set evaluate time source and clock
166 * event devices whether high resolution mode can be
167 * activated.
168 * @nr_events: Total number of timer interrupt events
170 struct hrtimer_cpu_base {
171 raw_spinlock_t lock;
172 struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
173 #ifdef CONFIG_HIGH_RES_TIMERS
174 ktime_t expires_next;
175 int hres_active;
176 unsigned long nr_events;
177 #endif
178 #ifdef CONFIG_PREEMPT_SOFTIRQS
179 wait_queue_head_t wait;
180 #endif
183 static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
185 timer->_expires = time;
186 timer->_softexpires = time;
189 static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
191 timer->_softexpires = time;
192 timer->_expires = ktime_add_safe(time, delta);
195 static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, unsigned long delta)
197 timer->_softexpires = time;
198 timer->_expires = ktime_add_safe(time, ns_to_ktime(delta));
201 static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
203 timer->_expires.tv64 = tv64;
204 timer->_softexpires.tv64 = tv64;
207 static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
209 timer->_expires = ktime_add_safe(timer->_expires, time);
210 timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
213 static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns)
215 timer->_expires = ktime_add_ns(timer->_expires, ns);
216 timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
219 static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
221 return timer->_expires;
224 static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
226 return timer->_softexpires;
229 static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
231 return timer->_expires.tv64;
233 static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
235 return timer->_softexpires.tv64;
238 static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
240 return ktime_to_ns(timer->_expires);
243 static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
245 return ktime_sub(timer->_expires, timer->base->get_time());
248 #ifdef CONFIG_HIGH_RES_TIMERS
249 struct clock_event_device;
251 extern void clock_was_set(void);
252 extern void hres_timers_resume(void);
253 extern void hrtimer_interrupt(struct clock_event_device *dev);
256 * In high resolution mode the time reference must be read accurate
258 static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
260 return timer->base->get_time();
263 static inline int hrtimer_is_hres_active(struct hrtimer *timer)
265 return timer->base->cpu_base->hres_active;
268 extern void hrtimer_peek_ahead_timers(void);
271 * The resolution of the clocks. The resolution value is returned in
272 * the clock_getres() system call to give application programmers an
273 * idea of the (in)accuracy of timers. Timer values are rounded up to
274 * this resolution values.
276 # define HIGH_RES_NSEC 1
277 # define KTIME_HIGH_RES (ktime_t) { .tv64 = HIGH_RES_NSEC }
278 # define MONOTONIC_RES_NSEC HIGH_RES_NSEC
279 # define KTIME_MONOTONIC_RES KTIME_HIGH_RES
281 #else
283 # define MONOTONIC_RES_NSEC LOW_RES_NSEC
284 # define KTIME_MONOTONIC_RES KTIME_LOW_RES
287 * clock_was_set() is a NOP for non- high-resolution systems. The
288 * time-sorted order guarantees that a timer does not expire early and
289 * is expired in the next softirq when the clock was advanced.
291 static inline void clock_was_set(void) { }
292 static inline void hrtimer_peek_ahead_timers(void) { }
294 static inline void hres_timers_resume(void) { }
297 * In non high resolution mode the time reference is taken from
298 * the base softirq time variable.
300 static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
302 return timer->base->softirq_time;
305 static inline int hrtimer_is_hres_active(struct hrtimer *timer)
307 return 0;
309 #endif
311 extern ktime_t ktime_get(void);
312 extern ktime_t ktime_get_real(void);
315 DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
318 /* Exported timer functions: */
320 /* Initialize timers: */
321 extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
322 enum hrtimer_mode mode);
324 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
325 extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
326 enum hrtimer_mode mode);
328 extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
329 #else
330 static inline void hrtimer_init_on_stack(struct hrtimer *timer,
331 clockid_t which_clock,
332 enum hrtimer_mode mode)
334 hrtimer_init(timer, which_clock, mode);
336 static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
337 #endif
339 /* Basic timer operations: */
340 extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
341 const enum hrtimer_mode mode);
342 extern int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
343 unsigned long range_ns, const enum hrtimer_mode mode);
344 extern int
345 __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
346 unsigned long delta_ns,
347 const enum hrtimer_mode mode, int wakeup);
349 extern int hrtimer_cancel(struct hrtimer *timer);
350 extern int hrtimer_try_to_cancel(struct hrtimer *timer);
352 static inline int hrtimer_start_expires(struct hrtimer *timer,
353 enum hrtimer_mode mode)
355 unsigned long delta;
356 ktime_t soft, hard;
357 soft = hrtimer_get_softexpires(timer);
358 hard = hrtimer_get_expires(timer);
359 delta = ktime_to_ns(ktime_sub(hard, soft));
360 return hrtimer_start_range_ns(timer, soft, delta, mode);
363 static inline int hrtimer_restart(struct hrtimer *timer)
365 return hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
368 /* Softirq preemption could deadlock timer removal */
369 #ifdef CONFIG_PREEMPT_SOFTIRQS
370 extern void hrtimer_wait_for_timer(const struct hrtimer *timer);
371 #else
372 # define hrtimer_wait_for_timer(timer) do { cpu_relax(); } while (0)
373 #endif
375 /* Query timers: */
376 extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
377 extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
379 extern ktime_t hrtimer_get_next_event(void);
382 * A timer is active, when it is enqueued into the rbtree or the callback
383 * function is running.
385 static inline int hrtimer_active(const struct hrtimer *timer)
387 return timer->state != HRTIMER_STATE_INACTIVE;
391 * Helper function to check, whether the timer is on one of the queues
393 static inline int hrtimer_is_queued(struct hrtimer *timer)
395 return timer->state & HRTIMER_STATE_ENQUEUED;
399 * Helper function to check, whether the timer is running the callback
400 * function
402 static inline int hrtimer_callback_running(struct hrtimer *timer)
404 return timer->state & HRTIMER_STATE_CALLBACK;
407 /* Forward a hrtimer so it expires after now: */
408 extern u64
409 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
411 /* Forward a hrtimer so it expires after the hrtimer's current now */
412 static inline u64 hrtimer_forward_now(struct hrtimer *timer,
413 ktime_t interval)
415 return hrtimer_forward(timer, timer->base->get_time(), interval);
418 /* Precise sleep: */
419 extern long hrtimer_nanosleep(struct timespec *rqtp,
420 struct timespec __user *rmtp,
421 const enum hrtimer_mode mode,
422 const clockid_t clockid);
423 extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
425 extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
426 struct task_struct *tsk);
428 extern int schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
429 const enum hrtimer_mode mode);
430 extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
432 /* Soft interrupt function to run the hrtimer queues: */
433 extern void hrtimer_run_queues(void);
434 extern void hrtimer_run_pending(void);
436 /* Bootup initialization: */
437 extern void __init hrtimers_init(void);
439 #if BITS_PER_LONG < 64
440 extern u64 ktime_divns(const ktime_t kt, s64 div);
441 #else /* BITS_PER_LONG < 64 */
442 # define ktime_divns(kt, div) (u64)((kt).tv64 / (div))
443 #endif
445 /* Show pending timers: */
446 extern void sysrq_timer_list_show(void);
449 * Timer-statistics info:
451 #ifdef CONFIG_TIMER_STATS
453 extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
454 void *timerf, char *comm,
455 unsigned int timer_flag);
457 static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
459 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
460 timer->function, timer->start_comm, 0);
463 extern void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer,
464 void *addr);
466 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
468 __timer_stats_hrtimer_set_start_info(timer, __builtin_return_address(0));
471 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
473 timer->start_site = NULL;
475 #else
476 static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
480 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
484 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
487 #endif
489 #endif