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:
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 * hrtimer callback modes:
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
55 enum hrtimer_cb_mode
{
58 HRTIMER_CB_IRQSAFE_NO_RESTART
,
59 HRTIMER_CB_IRQSAFE_NO_SOFTIRQ
,
63 * Values to track state of the timer
68 * 0x01 enqueued into rbtree
69 * 0x02 callback function running
70 * 0x04 callback pending (high resolution mode)
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.
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.
86 * All state transitions are protected by cpu_base->lock.
88 #define HRTIMER_STATE_INACTIVE 0x00
89 #define HRTIMER_STATE_ENQUEUED 0x01
90 #define HRTIMER_STATE_CALLBACK 0x02
91 #define HRTIMER_STATE_PENDING 0x04
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
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
107 * @start_comm: timer statistics field to store the name of the process which
109 * @start_pid: timer statistics field to store the pid of the task which
112 * The hrtimer structure must be initialized by hrtimer_init()
117 ktime_t _softexpires
;
118 enum hrtimer_restart (*function
)(struct hrtimer
*);
119 struct hrtimer_clock_base
*base
;
121 enum hrtimer_cb_mode cb_mode
;
122 struct list_head cb_entry
;
123 #ifdef CONFIG_TIMER_STATS
131 * struct hrtimer_sleeper - simple sleeper structure
132 * @timer: embedded timer structure
133 * @task: task to wake up
135 * task is set to NULL, when the timer expires.
137 struct hrtimer_sleeper
{
138 struct hrtimer timer
;
139 struct task_struct
*task
;
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
156 struct hrtimer_clock_base
{
157 struct hrtimer_cpu_base
*cpu_base
;
159 struct rb_root active
;
160 struct rb_node
*first
;
162 ktime_t (*get_time
)(void);
163 ktime_t (*get_softirq_time
)(void);
164 ktime_t softirq_time
;
165 #ifdef CONFIG_HIGH_RES_TIMERS
167 int (*reprogram
)(struct hrtimer
*t
,
168 struct hrtimer_clock_base
*b
,
173 #define HRTIMER_MAX_CLOCK_BASES 2
176 * struct hrtimer_cpu_base - the per cpu clock bases
177 * @lock: lock protecting the base and associated clock bases
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
187 * @cb_pending: Expired timers are moved from the rbtree to this
188 * list in the timer interrupt. The list is processed
190 * @nr_events: Total number of timer interrupt events
192 struct hrtimer_cpu_base
{
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
;
199 unsigned long nr_events
;
203 static inline void hrtimer_set_expires(struct hrtimer
*timer
, ktime_t time
)
205 timer
->_expires
= time
;
206 timer
->_softexpires
= time
;
209 static inline void hrtimer_set_expires_range(struct hrtimer
*timer
, ktime_t time
, ktime_t delta
)
211 timer
->_softexpires
= time
;
212 timer
->_expires
= ktime_add_safe(time
, delta
);
215 static inline void hrtimer_set_expires_range_ns(struct hrtimer
*timer
, ktime_t time
, unsigned long delta
)
217 timer
->_softexpires
= time
;
218 timer
->_expires
= ktime_add_safe(time
, ns_to_ktime(delta
));
221 static inline void hrtimer_set_expires_tv64(struct hrtimer
*timer
, s64 tv64
)
223 timer
->_expires
.tv64
= tv64
;
224 timer
->_softexpires
.tv64
= tv64
;
227 static inline void hrtimer_add_expires(struct hrtimer
*timer
, ktime_t time
)
229 timer
->_expires
= ktime_add_safe(timer
->_expires
, time
);
230 timer
->_softexpires
= ktime_add_safe(timer
->_softexpires
, time
);
233 static inline void hrtimer_add_expires_ns(struct hrtimer
*timer
, unsigned long ns
)
235 timer
->_expires
= ktime_add_ns(timer
->_expires
, ns
);
236 timer
->_softexpires
= ktime_add_ns(timer
->_softexpires
, ns
);
239 static inline ktime_t
hrtimer_get_expires(const struct hrtimer
*timer
)
241 return timer
->_expires
;
244 static inline ktime_t
hrtimer_get_softexpires(const struct hrtimer
*timer
)
246 return timer
->_softexpires
;
249 static inline s64
hrtimer_get_expires_tv64(const struct hrtimer
*timer
)
251 return timer
->_expires
.tv64
;
253 static inline s64
hrtimer_get_softexpires_tv64(const struct hrtimer
*timer
)
255 return timer
->_softexpires
.tv64
;
258 static inline s64
hrtimer_get_expires_ns(const struct hrtimer
*timer
)
260 return ktime_to_ns(timer
->_expires
);
263 static inline ktime_t
hrtimer_expires_remaining(const struct hrtimer
*timer
)
265 return ktime_sub(timer
->_expires
, timer
->base
->get_time());
268 #ifdef CONFIG_HIGH_RES_TIMERS
269 struct clock_event_device
;
271 extern void clock_was_set(void);
272 extern void hres_timers_resume(void);
273 extern void hrtimer_interrupt(struct clock_event_device
*dev
);
276 * In high resolution mode the time reference must be read accurate
278 static inline ktime_t
hrtimer_cb_get_time(struct hrtimer
*timer
)
280 return timer
->base
->get_time();
283 static inline int hrtimer_is_hres_active(struct hrtimer
*timer
)
285 return timer
->base
->cpu_base
->hres_active
;
288 extern void hrtimer_peek_ahead_timers(void);
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.
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
303 # define MONOTONIC_RES_NSEC LOW_RES_NSEC
304 # define KTIME_MONOTONIC_RES KTIME_LOW_RES
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.
311 static inline void clock_was_set(void) { }
312 static inline void hrtimer_peek_ahead_timers(void) { }
314 static inline void hres_timers_resume(void) { }
317 * In non high resolution mode the time reference is taken from
318 * the base softirq time variable.
320 static inline ktime_t
hrtimer_cb_get_time(struct hrtimer
*timer
)
322 return timer
->base
->softirq_time
;
325 static inline int hrtimer_is_hres_active(struct hrtimer
*timer
)
331 extern ktime_t
ktime_get(void);
332 extern ktime_t
ktime_get_real(void);
335 DECLARE_PER_CPU(struct tick_device
, tick_cpu_device
);
338 /* Exported timer functions: */
340 /* Initialize timers: */
341 extern void hrtimer_init(struct hrtimer
*timer
, clockid_t which_clock
,
342 enum hrtimer_mode mode
);
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
);
348 extern void destroy_hrtimer_on_stack(struct hrtimer
*timer
);
350 static inline void hrtimer_init_on_stack(struct hrtimer
*timer
,
351 clockid_t which_clock
,
352 enum hrtimer_mode mode
)
354 hrtimer_init(timer
, which_clock
, mode
);
356 static inline void destroy_hrtimer_on_stack(struct hrtimer
*timer
) { }
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
);
367 static inline int hrtimer_start_expires(struct hrtimer
*timer
,
368 enum hrtimer_mode mode
)
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
);
378 static inline int hrtimer_restart(struct hrtimer
*timer
)
380 return hrtimer_start_expires(timer
, HRTIMER_MODE_ABS
);
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
);
387 extern ktime_t
hrtimer_get_next_event(void);
390 * A timer is active, when it is enqueued into the rbtree or the callback
391 * function is running.
393 static inline int hrtimer_active(const struct hrtimer
*timer
)
395 return timer
->state
!= HRTIMER_STATE_INACTIVE
;
399 * Helper function to check, whether the timer is on one of the queues
401 static inline int hrtimer_is_queued(struct hrtimer
*timer
)
403 return timer
->state
&
404 (HRTIMER_STATE_ENQUEUED
| HRTIMER_STATE_PENDING
);
408 * Helper function to check, whether the timer is running the callback
411 static inline int hrtimer_callback_running(struct hrtimer
*timer
)
413 return timer
->state
& HRTIMER_STATE_CALLBACK
;
416 /* Forward a hrtimer so it expires after now: */
418 hrtimer_forward(struct hrtimer
*timer
, ktime_t now
, ktime_t interval
);
420 /* Forward a hrtimer so it expires after the hrtimer's current now */
421 static inline u64
hrtimer_forward_now(struct hrtimer
*timer
,
424 return hrtimer_forward(timer
, timer
->base
->get_time(), interval
);
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
);
434 extern void hrtimer_init_sleeper(struct hrtimer_sleeper
*sl
,
435 struct task_struct
*tsk
);
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
);
441 /* Soft interrupt function to run the hrtimer queues: */
442 extern void hrtimer_run_queues(void);
443 extern void hrtimer_run_pending(void);
445 /* Bootup initialization: */
446 extern void __init
hrtimers_init(void);
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))
454 /* Show pending timers: */
455 extern void sysrq_timer_list_show(void);
458 * Timer-statistics info:
460 #ifdef CONFIG_TIMER_STATS
462 extern void timer_stats_update_stats(void *timer
, pid_t pid
, void *startf
,
463 void *timerf
, char *comm
,
464 unsigned int timer_flag
);
466 static inline void timer_stats_account_hrtimer(struct hrtimer
*timer
)
468 timer_stats_update_stats(timer
, timer
->start_pid
, timer
->start_site
,
469 timer
->function
, timer
->start_comm
, 0);
472 extern void __timer_stats_hrtimer_set_start_info(struct hrtimer
*timer
,
475 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer
*timer
)
477 __timer_stats_hrtimer_set_start_info(timer
, __builtin_return_address(0));
480 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer
*timer
)
482 timer
->start_site
= NULL
;
485 static inline void timer_stats_account_hrtimer(struct hrtimer
*timer
)
489 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer
*timer
)
493 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer
*timer
)