Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 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 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 24 | struct hrtimer_clock_base; |
| 25 | struct hrtimer_cpu_base; |
| 26 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 27 | /* |
| 28 | * Mode arguments of xxx_hrtimer functions: |
| 29 | */ |
| 30 | enum hrtimer_mode { |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 31 | HRTIMER_MODE_ABS, /* Time value is absolute */ |
| 32 | HRTIMER_MODE_REL, /* Time value is relative to now */ |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 33 | }; |
| 34 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 35 | /* |
| 36 | * Return values for the callback function |
| 37 | */ |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 38 | enum hrtimer_restart { |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 39 | HRTIMER_NORESTART, /* Timer is not restarted */ |
| 40 | HRTIMER_RESTART, /* Timer must be restarted */ |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame^] | 43 | /* |
| 44 | * Bit values to track state of the timer |
| 45 | * |
| 46 | * Possible states: |
| 47 | * |
| 48 | * 0x00 inactive |
| 49 | * 0x01 enqueued into rbtree |
| 50 | * 0x02 callback function running |
| 51 | * 0x03 callback function running and enqueued |
| 52 | * (was requeued on another CPU) |
| 53 | * |
| 54 | * The "callback function running and enqueued" status is only possible on |
| 55 | * SMP. It happens for example when a posix timer expired and the callback |
| 56 | * queued a signal. Between dropping the lock which protects the posix timer |
| 57 | * and reacquiring the base lock of the hrtimer, another CPU can deliver the |
| 58 | * signal and rearm the timer. We have to preserve the callback running state, |
| 59 | * as otherwise the timer could be removed before the softirq code finishes the |
| 60 | * the handling of the timer. |
| 61 | * |
| 62 | * The HRTIMER_STATE_ENQUEUE bit is always or'ed to the current state to |
| 63 | * preserve the HRTIMER_STATE_CALLBACK bit in the above scenario. |
| 64 | * |
| 65 | * All state transitions are protected by cpu_base->lock. |
| 66 | */ |
| 67 | #define HRTIMER_STATE_INACTIVE 0x00 |
| 68 | #define HRTIMER_STATE_ENQUEUED 0x01 |
| 69 | #define HRTIMER_STATE_CALLBACK 0x02 |
| 70 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 71 | /** |
| 72 | * struct hrtimer - the basic hrtimer structure |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 73 | * @node: red black tree node for time ordered insertion |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 74 | * @expires: the absolute expiry time in the hrtimers internal |
| 75 | * representation. The time is related to the clock on |
| 76 | * which the timer is based. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 77 | * @function: timer expiry callback function |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 78 | * @base: pointer to the timer base (per cpu and per clock) |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame^] | 79 | * @state: state information (See bit values above) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 80 | * |
| 81 | * The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE() |
| 82 | */ |
| 83 | struct hrtimer { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 84 | struct rb_node node; |
| 85 | ktime_t expires; |
| 86 | enum hrtimer_restart (*function)(struct hrtimer *); |
| 87 | struct hrtimer_clock_base *base; |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame^] | 88 | unsigned long state; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /** |
Thomas Gleixner | 00362e3 | 2006-03-31 02:31:17 -0800 | [diff] [blame] | 92 | * struct hrtimer_sleeper - simple sleeper structure |
Thomas Gleixner | 00362e3 | 2006-03-31 02:31:17 -0800 | [diff] [blame] | 93 | * @timer: embedded timer structure |
| 94 | * @task: task to wake up |
| 95 | * |
| 96 | * task is set to NULL, when the timer expires. |
| 97 | */ |
| 98 | struct hrtimer_sleeper { |
| 99 | struct hrtimer timer; |
| 100 | struct task_struct *task; |
| 101 | }; |
| 102 | |
| 103 | /** |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 104 | * struct hrtimer_base - the timer base for a specific clock |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 105 | * @index: clock type index for per_cpu support when moving a |
| 106 | * timer to a base on another cpu. |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 107 | * @active: red black tree root node for the active timers |
| 108 | * @first: pointer to the timer node which expires first |
| 109 | * @resolution: the resolution of the clock, in nanoseconds |
| 110 | * @get_time: function to retrieve the current time of the clock |
Martin Waitz | a580290 | 2006-04-02 13:59:55 +0200 | [diff] [blame] | 111 | * @get_softirq_time: function to retrieve the current time from the softirq |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 112 | * @softirq_time: the time when running the hrtimer queue in the softirq |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 113 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 114 | struct hrtimer_clock_base { |
| 115 | struct hrtimer_cpu_base *cpu_base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 116 | clockid_t index; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 117 | struct rb_root active; |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 118 | struct rb_node *first; |
Thomas Gleixner | e278763 | 2006-01-12 11:36:14 +0100 | [diff] [blame] | 119 | ktime_t resolution; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 120 | ktime_t (*get_time)(void); |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 121 | ktime_t (*get_softirq_time)(void); |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 122 | ktime_t softirq_time; |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | #define HRTIMER_MAX_CLOCK_BASES 2 |
| 126 | |
| 127 | /* |
| 128 | * struct hrtimer_cpu_base - the per cpu clock bases |
| 129 | * @lock: lock protecting the base and associated clock bases |
| 130 | * and timers |
| 131 | * @lock_key: the lock_class_key for use with lockdep |
| 132 | * @clock_base: array of clock bases for this cpu |
| 133 | * @curr_timer: the timer which is executing a callback right now |
| 134 | */ |
| 135 | struct hrtimer_cpu_base { |
| 136 | spinlock_t lock; |
| 137 | struct lock_class_key lock_key; |
| 138 | struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES]; |
| 139 | struct hrtimer *curr_timer; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
Thomas Gleixner | becf8b5 | 2006-01-09 20:52:38 -0800 | [diff] [blame] | 142 | /* |
| 143 | * clock_was_set() is a NOP for non- high-resolution systems. The |
| 144 | * time-sorted order guarantees that a timer does not expire early and |
| 145 | * is expired in the next softirq when the clock was advanced. |
| 146 | */ |
| 147 | #define clock_was_set() do { } while (0) |
| 148 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 149 | /* Exported timer functions: */ |
| 150 | |
| 151 | /* Initialize timers: */ |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 152 | extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock, |
| 153 | enum hrtimer_mode mode); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 154 | |
| 155 | /* Basic timer operations: */ |
| 156 | extern int hrtimer_start(struct hrtimer *timer, ktime_t tim, |
| 157 | const enum hrtimer_mode mode); |
| 158 | extern int hrtimer_cancel(struct hrtimer *timer); |
| 159 | extern int hrtimer_try_to_cancel(struct hrtimer *timer); |
| 160 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 161 | static inline int hrtimer_restart(struct hrtimer *timer) |
| 162 | { |
| 163 | return hrtimer_start(timer, timer->expires, HRTIMER_MODE_ABS); |
| 164 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 165 | |
| 166 | /* Query timers: */ |
| 167 | extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer); |
| 168 | extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp); |
| 169 | |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 170 | #ifdef CONFIG_NO_IDLE_HZ |
| 171 | extern ktime_t hrtimer_get_next_event(void); |
| 172 | #endif |
| 173 | |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame^] | 174 | /* |
| 175 | * A timer is active, when it is enqueued into the rbtree or the callback |
| 176 | * function is running. |
| 177 | */ |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 178 | static inline int hrtimer_active(const struct hrtimer *timer) |
| 179 | { |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame^] | 180 | return timer->state != HRTIMER_STATE_INACTIVE; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* Forward a hrtimer so it expires after now: */ |
Roman Zippel | 44f2147 | 2006-03-26 01:38:06 -0800 | [diff] [blame] | 184 | extern unsigned long |
| 185 | hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 186 | |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 187 | /* Precise sleep: */ |
| 188 | extern long hrtimer_nanosleep(struct timespec *rqtp, |
| 189 | struct timespec __user *rmtp, |
| 190 | const enum hrtimer_mode mode, |
| 191 | const clockid_t clockid); |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 192 | extern long hrtimer_nanosleep_restart(struct restart_block *restart_block); |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 193 | |
Thomas Gleixner | 00362e3 | 2006-03-31 02:31:17 -0800 | [diff] [blame] | 194 | extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, |
| 195 | struct task_struct *tsk); |
| 196 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 197 | /* Soft interrupt function to run the hrtimer queues: */ |
| 198 | extern void hrtimer_run_queues(void); |
| 199 | |
John Stultz | 411187f | 2007-02-16 01:27:30 -0800 | [diff] [blame] | 200 | /* Resume notification */ |
| 201 | void hrtimer_notify_resume(void); |
| 202 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 203 | /* Bootup initialization: */ |
| 204 | extern void __init hrtimers_init(void); |
| 205 | |
| 206 | #endif |