Commit | Line | Data |
---|---|---|
c0a31329 TG |
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> | |
40b86062 SR |
23 | #include <linux/percpu.h> |
24 | ||
c0a31329 | 25 | |
3c8aa39d TG |
26 | struct hrtimer_clock_base; |
27 | struct hrtimer_cpu_base; | |
28 | ||
c0a31329 TG |
29 | /* |
30 | * Mode arguments of xxx_hrtimer functions: | |
31 | */ | |
32 | enum hrtimer_mode { | |
c9cb2e3d TG |
33 | HRTIMER_MODE_ABS, /* Time value is absolute */ |
34 | HRTIMER_MODE_REL, /* Time value is relative to now */ | |
c0a31329 TG |
35 | }; |
36 | ||
c9cb2e3d TG |
37 | /* |
38 | * Return values for the callback function | |
39 | */ | |
c0a31329 | 40 | enum hrtimer_restart { |
c9cb2e3d TG |
41 | HRTIMER_NORESTART, /* Timer is not restarted */ |
42 | HRTIMER_RESTART, /* Timer must be restarted */ | |
c0a31329 TG |
43 | }; |
44 | ||
54cdfdb4 TG |
45 | /* |
46 | * Values to track state of the timer | |
303e967f TG |
47 | * |
48 | * Possible states: | |
49 | * | |
50 | * 0x00 inactive | |
51 | * 0x01 enqueued into rbtree | |
52 | * 0x02 callback function running | |
54cdfdb4 | 53 | * |
b00c1a99 | 54 | * Special cases: |
303e967f TG |
55 | * 0x03 callback function running and enqueued |
56 | * (was requeued on another CPU) | |
b00c1a99 | 57 | * 0x09 timer was migrated on CPU hotunplug |
303e967f TG |
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. | |
65 | * | |
3eb05676 | 66 | * The HRTIMER_STATE_ENQUEUED bit is always or'ed to the current state to |
303e967f TG |
67 | * preserve the HRTIMER_STATE_CALLBACK bit in the above scenario. |
68 | * | |
69 | * All state transitions are protected by cpu_base->lock. | |
70 | */ | |
71 | #define HRTIMER_STATE_INACTIVE 0x00 | |
72 | #define HRTIMER_STATE_ENQUEUED 0x01 | |
73 | #define HRTIMER_STATE_CALLBACK 0x02 | |
ca109491 | 74 | #define HRTIMER_STATE_MIGRATE 0x04 |
303e967f | 75 | |
c0a31329 TG |
76 | /** |
77 | * struct hrtimer - the basic hrtimer structure | |
c0a31329 | 78 | * @node: red black tree node for time ordered insertion |
592aa999 | 79 | * @_expires: the absolute expiry time in the hrtimers internal |
c0a31329 | 80 | * representation. The time is related to the clock on |
592aa999 TG |
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. | |
c0a31329 | 87 | * @function: timer expiry callback function |
c0a31329 | 88 | * @base: pointer to the timer base (per cpu and per clock) |
303e967f | 89 | * @state: state information (See bit values above) |
54cdfdb4 TG |
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 | |
c0a31329 | 97 | * |
54cdfdb4 | 98 | * The hrtimer structure must be initialized by hrtimer_init() |
c0a31329 TG |
99 | */ |
100 | struct hrtimer { | |
3c8aa39d | 101 | struct rb_node node; |
799b64de | 102 | ktime_t _expires; |
654c8e0b | 103 | ktime_t _softexpires; |
3c8aa39d TG |
104 | enum hrtimer_restart (*function)(struct hrtimer *); |
105 | struct hrtimer_clock_base *base; | |
303e967f | 106 | unsigned long state; |
54cdfdb4 | 107 | struct list_head cb_entry; |
82f67cd9 | 108 | #ifdef CONFIG_TIMER_STATS |
1b024690 | 109 | int start_pid; |
82f67cd9 IM |
110 | void *start_site; |
111 | char start_comm[16]; | |
82f67cd9 | 112 | #endif |
c0a31329 TG |
113 | }; |
114 | ||
00362e33 TG |
115 | /** |
116 | * struct hrtimer_sleeper - simple sleeper structure | |
00362e33 TG |
117 | * @timer: embedded timer structure |
118 | * @task: task to wake up | |
119 | * | |
120 | * task is set to NULL, when the timer expires. | |
121 | */ | |
122 | struct hrtimer_sleeper { | |
123 | struct hrtimer timer; | |
124 | struct task_struct *task; | |
125 | }; | |
126 | ||
c0a31329 | 127 | /** |
d1d67174 | 128 | * struct hrtimer_clock_base - the timer base for a specific clock |
05fb6bf0 | 129 | * @cpu_base: per cpu clock base |
3c8aa39d TG |
130 | * @index: clock type index for per_cpu support when moving a |
131 | * timer to a base on another cpu. | |
92127c7a TG |
132 | * @active: red black tree root node for the active timers |
133 | * @first: pointer to the timer node which expires first | |
134 | * @resolution: the resolution of the clock, in nanoseconds | |
135 | * @get_time: function to retrieve the current time of the clock | |
92127c7a | 136 | * @softirq_time: the time when running the hrtimer queue in the softirq |
54cdfdb4 | 137 | * @offset: offset of this clock to the monotonic base |
c0a31329 | 138 | */ |
3c8aa39d TG |
139 | struct hrtimer_clock_base { |
140 | struct hrtimer_cpu_base *cpu_base; | |
c0a31329 | 141 | clockid_t index; |
c0a31329 | 142 | struct rb_root active; |
288867ec | 143 | struct rb_node *first; |
e2787630 | 144 | ktime_t resolution; |
c0a31329 | 145 | ktime_t (*get_time)(void); |
92127c7a | 146 | ktime_t softirq_time; |
54cdfdb4 TG |
147 | #ifdef CONFIG_HIGH_RES_TIMERS |
148 | ktime_t offset; | |
54cdfdb4 | 149 | #endif |
3c8aa39d TG |
150 | }; |
151 | ||
152 | #define HRTIMER_MAX_CLOCK_BASES 2 | |
153 | ||
154 | /* | |
155 | * struct hrtimer_cpu_base - the per cpu clock bases | |
156 | * @lock: lock protecting the base and associated clock bases | |
157 | * and timers | |
3c8aa39d TG |
158 | * @clock_base: array of clock bases for this cpu |
159 | * @curr_timer: the timer which is executing a callback right now | |
54cdfdb4 TG |
160 | * @expires_next: absolute time of the next event which was scheduled |
161 | * via clock_set_next_event() | |
162 | * @hres_active: State of high resolution mode | |
163 | * @check_clocks: Indictator, when set evaluate time source and clock | |
164 | * event devices whether high resolution mode can be | |
165 | * activated. | |
54cdfdb4 | 166 | * @nr_events: Total number of timer interrupt events |
3c8aa39d TG |
167 | */ |
168 | struct hrtimer_cpu_base { | |
169 | spinlock_t lock; | |
3c8aa39d | 170 | struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES]; |
54cdfdb4 TG |
171 | #ifdef CONFIG_HIGH_RES_TIMERS |
172 | ktime_t expires_next; | |
173 | int hres_active; | |
54cdfdb4 TG |
174 | unsigned long nr_events; |
175 | #endif | |
c0a31329 TG |
176 | }; |
177 | ||
63ca243b AV |
178 | static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time) |
179 | { | |
799b64de | 180 | timer->_expires = time; |
654c8e0b | 181 | timer->_softexpires = time; |
63ca243b | 182 | } |
654c8e0b AV |
183 | |
184 | static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta) | |
185 | { | |
186 | timer->_softexpires = time; | |
187 | timer->_expires = ktime_add_safe(time, delta); | |
188 | } | |
189 | ||
190 | static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, unsigned long delta) | |
191 | { | |
192 | timer->_softexpires = time; | |
193 | timer->_expires = ktime_add_safe(time, ns_to_ktime(delta)); | |
194 | } | |
195 | ||
63ca243b AV |
196 | static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64) |
197 | { | |
799b64de | 198 | timer->_expires.tv64 = tv64; |
654c8e0b | 199 | timer->_softexpires.tv64 = tv64; |
63ca243b AV |
200 | } |
201 | ||
202 | static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time) | |
203 | { | |
799b64de | 204 | timer->_expires = ktime_add_safe(timer->_expires, time); |
654c8e0b | 205 | timer->_softexpires = ktime_add_safe(timer->_softexpires, time); |
63ca243b AV |
206 | } |
207 | ||
7597bc94 | 208 | static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns) |
63ca243b | 209 | { |
799b64de | 210 | timer->_expires = ktime_add_ns(timer->_expires, ns); |
654c8e0b | 211 | timer->_softexpires = ktime_add_ns(timer->_softexpires, ns); |
63ca243b AV |
212 | } |
213 | ||
214 | static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer) | |
215 | { | |
799b64de | 216 | return timer->_expires; |
63ca243b AV |
217 | } |
218 | ||
654c8e0b AV |
219 | static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer) |
220 | { | |
221 | return timer->_softexpires; | |
222 | } | |
223 | ||
63ca243b AV |
224 | static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer) |
225 | { | |
799b64de | 226 | return timer->_expires.tv64; |
63ca243b | 227 | } |
654c8e0b AV |
228 | static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer) |
229 | { | |
230 | return timer->_softexpires.tv64; | |
231 | } | |
63ca243b AV |
232 | |
233 | static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer) | |
234 | { | |
799b64de | 235 | return ktime_to_ns(timer->_expires); |
63ca243b AV |
236 | } |
237 | ||
238 | static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer) | |
239 | { | |
799b64de | 240 | return ktime_sub(timer->_expires, timer->base->get_time()); |
63ca243b AV |
241 | } |
242 | ||
54cdfdb4 TG |
243 | #ifdef CONFIG_HIGH_RES_TIMERS |
244 | struct clock_event_device; | |
245 | ||
246 | extern void clock_was_set(void); | |
995f054f | 247 | extern void hres_timers_resume(void); |
54cdfdb4 TG |
248 | extern void hrtimer_interrupt(struct clock_event_device *dev); |
249 | ||
250 | /* | |
251 | * In high resolution mode the time reference must be read accurate | |
252 | */ | |
253 | static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer) | |
254 | { | |
255 | return timer->base->get_time(); | |
256 | } | |
257 | ||
8f4d37ec PZ |
258 | static inline int hrtimer_is_hres_active(struct hrtimer *timer) |
259 | { | |
260 | return timer->base->cpu_base->hres_active; | |
261 | } | |
262 | ||
2075eb8d AV |
263 | extern void hrtimer_peek_ahead_timers(void); |
264 | ||
54cdfdb4 TG |
265 | /* |
266 | * The resolution of the clocks. The resolution value is returned in | |
267 | * the clock_getres() system call to give application programmers an | |
268 | * idea of the (in)accuracy of timers. Timer values are rounded up to | |
269 | * this resolution values. | |
270 | */ | |
151db1fc TB |
271 | # define HIGH_RES_NSEC 1 |
272 | # define KTIME_HIGH_RES (ktime_t) { .tv64 = HIGH_RES_NSEC } | |
273 | # define MONOTONIC_RES_NSEC HIGH_RES_NSEC | |
54cdfdb4 TG |
274 | # define KTIME_MONOTONIC_RES KTIME_HIGH_RES |
275 | ||
276 | #else | |
277 | ||
151db1fc | 278 | # define MONOTONIC_RES_NSEC LOW_RES_NSEC |
54cdfdb4 TG |
279 | # define KTIME_MONOTONIC_RES KTIME_LOW_RES |
280 | ||
becf8b5d TG |
281 | /* |
282 | * clock_was_set() is a NOP for non- high-resolution systems. The | |
283 | * time-sorted order guarantees that a timer does not expire early and | |
284 | * is expired in the next softirq when the clock was advanced. | |
285 | */ | |
54cdfdb4 | 286 | static inline void clock_was_set(void) { } |
2075eb8d | 287 | static inline void hrtimer_peek_ahead_timers(void) { } |
54cdfdb4 | 288 | |
995f054f IM |
289 | static inline void hres_timers_resume(void) { } |
290 | ||
54cdfdb4 TG |
291 | /* |
292 | * In non high resolution mode the time reference is taken from | |
293 | * the base softirq time variable. | |
294 | */ | |
295 | static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer) | |
296 | { | |
297 | return timer->base->softirq_time; | |
298 | } | |
299 | ||
8f4d37ec PZ |
300 | static inline int hrtimer_is_hres_active(struct hrtimer *timer) |
301 | { | |
302 | return 0; | |
303 | } | |
54cdfdb4 TG |
304 | #endif |
305 | ||
d316c57f TG |
306 | extern ktime_t ktime_get(void); |
307 | extern ktime_t ktime_get_real(void); | |
becf8b5d | 308 | |
2e94d1f7 AV |
309 | |
310 | DECLARE_PER_CPU(struct tick_device, tick_cpu_device); | |
2e94d1f7 AV |
311 | |
312 | ||
c0a31329 TG |
313 | /* Exported timer functions: */ |
314 | ||
315 | /* Initialize timers: */ | |
7978672c GA |
316 | extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock, |
317 | enum hrtimer_mode mode); | |
c0a31329 | 318 | |
237fc6e7 TG |
319 | #ifdef CONFIG_DEBUG_OBJECTS_TIMERS |
320 | extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock, | |
321 | enum hrtimer_mode mode); | |
322 | ||
323 | extern void destroy_hrtimer_on_stack(struct hrtimer *timer); | |
324 | #else | |
325 | static inline void hrtimer_init_on_stack(struct hrtimer *timer, | |
326 | clockid_t which_clock, | |
327 | enum hrtimer_mode mode) | |
328 | { | |
329 | hrtimer_init(timer, which_clock, mode); | |
330 | } | |
331 | static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { } | |
332 | #endif | |
333 | ||
c0a31329 TG |
334 | /* Basic timer operations: */ |
335 | extern int hrtimer_start(struct hrtimer *timer, ktime_t tim, | |
336 | const enum hrtimer_mode mode); | |
da8f2e17 AV |
337 | extern int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, |
338 | unsigned long range_ns, const enum hrtimer_mode mode); | |
7f1e2ca9 PZ |
339 | extern int |
340 | __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, | |
341 | unsigned long delta_ns, | |
342 | const enum hrtimer_mode mode, int wakeup); | |
343 | ||
c0a31329 TG |
344 | extern int hrtimer_cancel(struct hrtimer *timer); |
345 | extern int hrtimer_try_to_cancel(struct hrtimer *timer); | |
346 | ||
63ca243b AV |
347 | static inline int hrtimer_start_expires(struct hrtimer *timer, |
348 | enum hrtimer_mode mode) | |
349 | { | |
da8f2e17 AV |
350 | unsigned long delta; |
351 | ktime_t soft, hard; | |
352 | soft = hrtimer_get_softexpires(timer); | |
353 | hard = hrtimer_get_expires(timer); | |
354 | delta = ktime_to_ns(ktime_sub(hard, soft)); | |
4ce105d3 | 355 | return hrtimer_start_range_ns(timer, soft, delta, mode); |
63ca243b AV |
356 | } |
357 | ||
c9cb2e3d TG |
358 | static inline int hrtimer_restart(struct hrtimer *timer) |
359 | { | |
654c8e0b | 360 | return hrtimer_start_expires(timer, HRTIMER_MODE_ABS); |
c9cb2e3d | 361 | } |
c0a31329 TG |
362 | |
363 | /* Query timers: */ | |
364 | extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer); | |
365 | extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp); | |
366 | ||
69239749 | 367 | extern ktime_t hrtimer_get_next_event(void); |
69239749 | 368 | |
303e967f TG |
369 | /* |
370 | * A timer is active, when it is enqueued into the rbtree or the callback | |
371 | * function is running. | |
372 | */ | |
c0a31329 TG |
373 | static inline int hrtimer_active(const struct hrtimer *timer) |
374 | { | |
303e967f | 375 | return timer->state != HRTIMER_STATE_INACTIVE; |
c0a31329 TG |
376 | } |
377 | ||
54cdfdb4 TG |
378 | /* |
379 | * Helper function to check, whether the timer is on one of the queues | |
380 | */ | |
381 | static inline int hrtimer_is_queued(struct hrtimer *timer) | |
382 | { | |
ca109491 | 383 | return timer->state & HRTIMER_STATE_ENQUEUED; |
54cdfdb4 TG |
384 | } |
385 | ||
4346f654 OH |
386 | /* |
387 | * Helper function to check, whether the timer is running the callback | |
388 | * function | |
389 | */ | |
390 | static inline int hrtimer_callback_running(struct hrtimer *timer) | |
391 | { | |
392 | return timer->state & HRTIMER_STATE_CALLBACK; | |
393 | } | |
394 | ||
c0a31329 | 395 | /* Forward a hrtimer so it expires after now: */ |
4d672e7a | 396 | extern u64 |
44f21475 | 397 | hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval); |
c0a31329 | 398 | |
5e05ad7d | 399 | /* Forward a hrtimer so it expires after the hrtimer's current now */ |
4d672e7a DL |
400 | static inline u64 hrtimer_forward_now(struct hrtimer *timer, |
401 | ktime_t interval) | |
5e05ad7d DL |
402 | { |
403 | return hrtimer_forward(timer, timer->base->get_time(), interval); | |
404 | } | |
405 | ||
10c94ec1 TG |
406 | /* Precise sleep: */ |
407 | extern long hrtimer_nanosleep(struct timespec *rqtp, | |
080344b9 | 408 | struct timespec __user *rmtp, |
10c94ec1 TG |
409 | const enum hrtimer_mode mode, |
410 | const clockid_t clockid); | |
1711ef38 | 411 | extern long hrtimer_nanosleep_restart(struct restart_block *restart_block); |
10c94ec1 | 412 | |
00362e33 TG |
413 | extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, |
414 | struct task_struct *tsk); | |
415 | ||
654c8e0b AV |
416 | extern int schedule_hrtimeout_range(ktime_t *expires, unsigned long delta, |
417 | const enum hrtimer_mode mode); | |
7bb67439 AV |
418 | extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode); |
419 | ||
c0a31329 TG |
420 | /* Soft interrupt function to run the hrtimer queues: */ |
421 | extern void hrtimer_run_queues(void); | |
d3d74453 | 422 | extern void hrtimer_run_pending(void); |
c0a31329 TG |
423 | |
424 | /* Bootup initialization: */ | |
425 | extern void __init hrtimers_init(void); | |
426 | ||
79bf2bb3 | 427 | #if BITS_PER_LONG < 64 |
4d672e7a | 428 | extern u64 ktime_divns(const ktime_t kt, s64 div); |
79bf2bb3 | 429 | #else /* BITS_PER_LONG < 64 */ |
4d672e7a | 430 | # define ktime_divns(kt, div) (u64)((kt).tv64 / (div)) |
79bf2bb3 TG |
431 | #endif |
432 | ||
88ad0bf6 IM |
433 | /* Show pending timers: */ |
434 | extern void sysrq_timer_list_show(void); | |
435 | ||
82f67cd9 IM |
436 | /* |
437 | * Timer-statistics info: | |
438 | */ | |
439 | #ifdef CONFIG_TIMER_STATS | |
440 | ||
441 | extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf, | |
c5c061b8 VP |
442 | void *timerf, char *comm, |
443 | unsigned int timer_flag); | |
82f67cd9 IM |
444 | |
445 | static inline void timer_stats_account_hrtimer(struct hrtimer *timer) | |
446 | { | |
447 | timer_stats_update_stats(timer, timer->start_pid, timer->start_site, | |
c5c061b8 | 448 | timer->function, timer->start_comm, 0); |
82f67cd9 IM |
449 | } |
450 | ||
451 | extern void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, | |
452 | void *addr); | |
453 | ||
454 | static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer) | |
455 | { | |
456 | __timer_stats_hrtimer_set_start_info(timer, __builtin_return_address(0)); | |
457 | } | |
458 | ||
459 | static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer) | |
460 | { | |
461 | timer->start_site = NULL; | |
462 | } | |
463 | #else | |
464 | static inline void timer_stats_account_hrtimer(struct hrtimer *timer) | |
465 | { | |
466 | } | |
467 | ||
468 | static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer) | |
469 | { | |
470 | } | |
471 | ||
472 | static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer) | |
473 | { | |
474 | } | |
475 | #endif | |
476 | ||
c0a31329 | 477 | #endif |