]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - include/linux/hrtimer.h
DECLARE_PER_CPU needs linux/percpu.h
[linux-2.6-omap-h63xx.git] / include / linux / hrtimer.h
1 /*
2  *  include/linux/hrtimer.h
3  *
4  *  hrtimers - High-resolution kernel timers
5  *
6  *   Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
7  *   Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
8  *
9  *  data type definitions, declarations, prototypes
10  *
11  *  Started by: Thomas Gleixner and Ingo Molnar
12  *
13  *  For licencing details see kernel-base/COPYING
14  */
15 #ifndef _LINUX_HRTIMER_H
16 #define _LINUX_HRTIMER_H
17
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>
24
25
26 struct hrtimer_clock_base;
27 struct hrtimer_cpu_base;
28
29 /*
30  * Mode arguments of xxx_hrtimer functions:
31  */
32 enum hrtimer_mode {
33         HRTIMER_MODE_ABS,       /* Time value is absolute */
34         HRTIMER_MODE_REL,       /* Time value is relative to now */
35 };
36
37 /*
38  * Return values for the callback function
39  */
40 enum hrtimer_restart {
41         HRTIMER_NORESTART,      /* Timer is not restarted */
42         HRTIMER_RESTART,        /* Timer must be restarted */
43 };
44
45 /*
46  * hrtimer callback modes:
47  *
48  *      HRTIMER_CB_SOFTIRQ:             Callback must run in softirq context
49  *      HRTIMER_CB_IRQSAFE:             Callback may run in hardirq context
50  *      HRTIMER_CB_IRQSAFE_NO_RESTART:  Callback may run in hardirq context and
51  *                                      does not restart the timer
52  *      HRTIMER_CB_IRQSAFE_NO_SOFTIRQ:  Callback must run in hardirq context
53  *                                      Special mode for tick emultation
54  */
55 enum hrtimer_cb_mode {
56         HRTIMER_CB_SOFTIRQ,
57         HRTIMER_CB_IRQSAFE,
58         HRTIMER_CB_IRQSAFE_NO_RESTART,
59         HRTIMER_CB_IRQSAFE_NO_SOFTIRQ,
60 };
61
62 /*
63  * Values to track state of the timer
64  *
65  * Possible states:
66  *
67  * 0x00         inactive
68  * 0x01         enqueued into rbtree
69  * 0x02         callback function running
70  * 0x04         callback pending (high resolution mode)
71  *
72  * Special case:
73  * 0x03         callback function running and enqueued
74  *              (was requeued on another CPU)
75  * The "callback function running and enqueued" status is only possible on
76  * SMP. It happens for example when a posix timer expired and the callback
77  * queued a signal. Between dropping the lock which protects the posix timer
78  * and reacquiring the base lock of the hrtimer, another CPU can deliver the
79  * signal and rearm the timer. We have to preserve the callback running state,
80  * as otherwise the timer could be removed before the softirq code finishes the
81  * the handling of the timer.
82  *
83  * The HRTIMER_STATE_ENQUEUED bit is always or'ed to the current state to
84  * preserve the HRTIMER_STATE_CALLBACK bit in the above scenario.
85  *
86  * All state transitions are protected by cpu_base->lock.
87  */
88 #define HRTIMER_STATE_INACTIVE  0x00
89 #define HRTIMER_STATE_ENQUEUED  0x01
90 #define HRTIMER_STATE_CALLBACK  0x02
91 #define HRTIMER_STATE_PENDING   0x04
92
93 /**
94  * struct hrtimer - the basic hrtimer structure
95  * @node:       red black tree node for time ordered insertion
96  * @expires:    the absolute expiry time in the hrtimers internal
97  *              representation. The time is related to the clock on
98  *              which the timer is based.
99  * @function:   timer expiry callback function
100  * @base:       pointer to the timer base (per cpu and per clock)
101  * @state:      state information (See bit values above)
102  * @cb_mode:    high resolution timer feature to select the callback execution
103  *               mode
104  * @cb_entry:   list head to enqueue an expired timer into the callback list
105  * @start_site: timer statistics field to store the site where the timer
106  *              was started
107  * @start_comm: timer statistics field to store the name of the process which
108  *              started the timer
109  * @start_pid: timer statistics field to store the pid of the task which
110  *              started the timer
111  *
112  * The hrtimer structure must be initialized by hrtimer_init()
113  */
114 struct hrtimer {
115         struct rb_node                  node;
116         ktime_t                         _expires;
117         ktime_t                         _softexpires;
118         enum hrtimer_restart            (*function)(struct hrtimer *);
119         struct hrtimer_clock_base       *base;
120         unsigned long                   state;
121         enum hrtimer_cb_mode            cb_mode;
122         struct list_head                cb_entry;
123 #ifdef CONFIG_TIMER_STATS
124         void                            *start_site;
125         char                            start_comm[16];
126         int                             start_pid;
127 #endif
128 };
129
130 /**
131  * struct hrtimer_sleeper - simple sleeper structure
132  * @timer:      embedded timer structure
133  * @task:       task to wake up
134  *
135  * task is set to NULL, when the timer expires.
136  */
137 struct hrtimer_sleeper {
138         struct hrtimer timer;
139         struct task_struct *task;
140 };
141
142 /**
143  * struct hrtimer_clock_base - the timer base for a specific clock
144  * @cpu_base:           per cpu clock base
145  * @index:              clock type index for per_cpu support when moving a
146  *                      timer to a base on another cpu.
147  * @active:             red black tree root node for the active timers
148  * @first:              pointer to the timer node which expires first
149  * @resolution:         the resolution of the clock, in nanoseconds
150  * @get_time:           function to retrieve the current time of the clock
151  * @get_softirq_time:   function to retrieve the current time from the softirq
152  * @softirq_time:       the time when running the hrtimer queue in the softirq
153  * @offset:             offset of this clock to the monotonic base
154  * @reprogram:          function to reprogram the timer event
155  */
156 struct hrtimer_clock_base {
157         struct hrtimer_cpu_base *cpu_base;
158         clockid_t               index;
159         struct rb_root          active;
160         struct rb_node          *first;
161         ktime_t                 resolution;
162         ktime_t                 (*get_time)(void);
163         ktime_t                 (*get_softirq_time)(void);
164         ktime_t                 softirq_time;
165 #ifdef CONFIG_HIGH_RES_TIMERS
166         ktime_t                 offset;
167         int                     (*reprogram)(struct hrtimer *t,
168                                              struct hrtimer_clock_base *b,
169                                              ktime_t n);
170 #endif
171 };
172
173 #define HRTIMER_MAX_CLOCK_BASES 2
174
175 /*
176  * struct hrtimer_cpu_base - the per cpu clock bases
177  * @lock:               lock protecting the base and associated clock bases
178  *                      and timers
179  * @clock_base:         array of clock bases for this cpu
180  * @curr_timer:         the timer which is executing a callback right now
181  * @expires_next:       absolute time of the next event which was scheduled
182  *                      via clock_set_next_event()
183  * @hres_active:        State of high resolution mode
184  * @check_clocks:       Indictator, when set evaluate time source and clock
185  *                      event devices whether high resolution mode can be
186  *                      activated.
187  * @cb_pending:         Expired timers are moved from the rbtree to this
188  *                      list in the timer interrupt. The list is processed
189  *                      in the softirq.
190  * @nr_events:          Total number of timer interrupt events
191  */
192 struct hrtimer_cpu_base {
193         spinlock_t                      lock;
194         struct hrtimer_clock_base       clock_base[HRTIMER_MAX_CLOCK_BASES];
195         struct list_head                cb_pending;
196 #ifdef CONFIG_HIGH_RES_TIMERS
197         ktime_t                         expires_next;
198         int                             hres_active;
199         unsigned long                   nr_events;
200 #endif
201 };
202
203 static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
204 {
205         timer->_expires = time;
206         timer->_softexpires = time;
207 }
208
209 static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
210 {
211         timer->_softexpires = time;
212         timer->_expires = ktime_add_safe(time, delta);
213 }
214
215 static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, unsigned long delta)
216 {
217         timer->_softexpires = time;
218         timer->_expires = ktime_add_safe(time, ns_to_ktime(delta));
219 }
220
221 static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
222 {
223         timer->_expires.tv64 = tv64;
224         timer->_softexpires.tv64 = tv64;
225 }
226
227 static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
228 {
229         timer->_expires = ktime_add_safe(timer->_expires, time);
230         timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
231 }
232
233 static inline void hrtimer_add_expires_ns(struct hrtimer *timer, unsigned long ns)
234 {
235         timer->_expires = ktime_add_ns(timer->_expires, ns);
236         timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
237 }
238
239 static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
240 {
241         return timer->_expires;
242 }
243
244 static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
245 {
246         return timer->_softexpires;
247 }
248
249 static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
250 {
251         return timer->_expires.tv64;
252 }
253 static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
254 {
255         return timer->_softexpires.tv64;
256 }
257
258 static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
259 {
260         return ktime_to_ns(timer->_expires);
261 }
262
263 static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
264 {
265     return ktime_sub(timer->_expires, timer->base->get_time());
266 }
267
268 #ifdef CONFIG_HIGH_RES_TIMERS
269 struct clock_event_device;
270
271 extern void clock_was_set(void);
272 extern void hres_timers_resume(void);
273 extern void hrtimer_interrupt(struct clock_event_device *dev);
274
275 /*
276  * In high resolution mode the time reference must be read accurate
277  */
278 static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
279 {
280         return timer->base->get_time();
281 }
282
283 static inline int hrtimer_is_hres_active(struct hrtimer *timer)
284 {
285         return timer->base->cpu_base->hres_active;
286 }
287
288 extern void hrtimer_peek_ahead_timers(void);
289
290 /*
291  * The resolution of the clocks. The resolution value is returned in
292  * the clock_getres() system call to give application programmers an
293  * idea of the (in)accuracy of timers. Timer values are rounded up to
294  * this resolution values.
295  */
296 # define HIGH_RES_NSEC          1
297 # define KTIME_HIGH_RES         (ktime_t) { .tv64 = HIGH_RES_NSEC }
298 # define MONOTONIC_RES_NSEC     HIGH_RES_NSEC
299 # define KTIME_MONOTONIC_RES    KTIME_HIGH_RES
300
301 #else
302
303 # define MONOTONIC_RES_NSEC     LOW_RES_NSEC
304 # define KTIME_MONOTONIC_RES    KTIME_LOW_RES
305
306 /*
307  * clock_was_set() is a NOP for non- high-resolution systems. The
308  * time-sorted order guarantees that a timer does not expire early and
309  * is expired in the next softirq when the clock was advanced.
310  */
311 static inline void clock_was_set(void) { }
312 static inline void hrtimer_peek_ahead_timers(void) { }
313
314 static inline void hres_timers_resume(void) { }
315
316 /*
317  * In non high resolution mode the time reference is taken from
318  * the base softirq time variable.
319  */
320 static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
321 {
322         return timer->base->softirq_time;
323 }
324
325 static inline int hrtimer_is_hres_active(struct hrtimer *timer)
326 {
327         return 0;
328 }
329 #endif
330
331 extern ktime_t ktime_get(void);
332 extern ktime_t ktime_get_real(void);
333
334
335 DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
336
337
338 /* Exported timer functions: */
339
340 /* Initialize timers: */
341 extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
342                          enum hrtimer_mode mode);
343
344 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
345 extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
346                                   enum hrtimer_mode mode);
347
348 extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
349 #else
350 static inline void hrtimer_init_on_stack(struct hrtimer *timer,
351                                          clockid_t which_clock,
352                                          enum hrtimer_mode mode)
353 {
354         hrtimer_init(timer, which_clock, mode);
355 }
356 static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
357 #endif
358
359 /* Basic timer operations: */
360 extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
361                          const enum hrtimer_mode mode);
362 extern int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
363                         unsigned long range_ns, const enum hrtimer_mode mode);
364 extern int hrtimer_cancel(struct hrtimer *timer);
365 extern int hrtimer_try_to_cancel(struct hrtimer *timer);
366
367 static inline int hrtimer_start_expires(struct hrtimer *timer,
368                                                 enum hrtimer_mode mode)
369 {
370         unsigned long delta;
371         ktime_t soft, hard;
372         soft = hrtimer_get_softexpires(timer);
373         hard = hrtimer_get_expires(timer);
374         delta = ktime_to_ns(ktime_sub(hard, soft));
375         return hrtimer_start_range_ns(timer, soft, delta, mode);
376 }
377
378 static inline int hrtimer_restart(struct hrtimer *timer)
379 {
380         return hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
381 }
382
383 /* Query timers: */
384 extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
385 extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
386
387 extern ktime_t hrtimer_get_next_event(void);
388
389 /*
390  * A timer is active, when it is enqueued into the rbtree or the callback
391  * function is running.
392  */
393 static inline int hrtimer_active(const struct hrtimer *timer)
394 {
395         return timer->state != HRTIMER_STATE_INACTIVE;
396 }
397
398 /*
399  * Helper function to check, whether the timer is on one of the queues
400  */
401 static inline int hrtimer_is_queued(struct hrtimer *timer)
402 {
403         return timer->state &
404                 (HRTIMER_STATE_ENQUEUED | HRTIMER_STATE_PENDING);
405 }
406
407 /*
408  * Helper function to check, whether the timer is running the callback
409  * function
410  */
411 static inline int hrtimer_callback_running(struct hrtimer *timer)
412 {
413         return timer->state & HRTIMER_STATE_CALLBACK;
414 }
415
416 /* Forward a hrtimer so it expires after now: */
417 extern u64
418 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
419
420 /* Forward a hrtimer so it expires after the hrtimer's current now */
421 static inline u64 hrtimer_forward_now(struct hrtimer *timer,
422                                       ktime_t interval)
423 {
424         return hrtimer_forward(timer, timer->base->get_time(), interval);
425 }
426
427 /* Precise sleep: */
428 extern long hrtimer_nanosleep(struct timespec *rqtp,
429                               struct timespec __user *rmtp,
430                               const enum hrtimer_mode mode,
431                               const clockid_t clockid);
432 extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
433
434 extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
435                                  struct task_struct *tsk);
436
437 extern int schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
438                                                 const enum hrtimer_mode mode);
439 extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
440
441 /* Soft interrupt function to run the hrtimer queues: */
442 extern void hrtimer_run_queues(void);
443 extern void hrtimer_run_pending(void);
444
445 /* Bootup initialization: */
446 extern void __init hrtimers_init(void);
447
448 #if BITS_PER_LONG < 64
449 extern u64 ktime_divns(const ktime_t kt, s64 div);
450 #else /* BITS_PER_LONG < 64 */
451 # define ktime_divns(kt, div)           (u64)((kt).tv64 / (div))
452 #endif
453
454 /* Show pending timers: */
455 extern void sysrq_timer_list_show(void);
456
457 /*
458  * Timer-statistics info:
459  */
460 #ifdef CONFIG_TIMER_STATS
461
462 extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
463                                      void *timerf, char *comm,
464                                      unsigned int timer_flag);
465
466 static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
467 {
468         timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
469                                  timer->function, timer->start_comm, 0);
470 }
471
472 extern void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer,
473                                                  void *addr);
474
475 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
476 {
477         __timer_stats_hrtimer_set_start_info(timer, __builtin_return_address(0));
478 }
479
480 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
481 {
482         timer->start_site = NULL;
483 }
484 #else
485 static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
486 {
487 }
488
489 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
490 {
491 }
492
493 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
494 {
495 }
496 #endif
497
498 #endif