Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [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 | #include <linux/percpu.h> |
| 24 | #include <linux/timer.h> |
| 25 | #include <linux/timerqueue.h> |
| 26 | |
| 27 | struct hrtimer_clock_base; |
| 28 | struct hrtimer_cpu_base; |
| 29 | |
| 30 | enum hrtimer_mode { |
| 31 | HRTIMER_MODE_ABS = 0x0, |
| 32 | HRTIMER_MODE_REL = 0x1, |
| 33 | HRTIMER_MODE_PINNED = 0x02, |
| 34 | HRTIMER_MODE_ABS_PINNED = 0x02, |
| 35 | HRTIMER_MODE_REL_PINNED = 0x03, |
| 36 | }; |
| 37 | |
| 38 | enum hrtimer_restart { |
| 39 | HRTIMER_NORESTART, |
| 40 | HRTIMER_RESTART, |
| 41 | }; |
| 42 | |
| 43 | #define HRTIMER_STATE_INACTIVE 0x00 |
| 44 | #define HRTIMER_STATE_ENQUEUED 0x01 |
| 45 | #define HRTIMER_STATE_CALLBACK 0x02 |
| 46 | #define HRTIMER_STATE_MIGRATE 0x04 |
| 47 | |
| 48 | struct hrtimer { |
| 49 | struct timerqueue_node node; |
| 50 | ktime_t _softexpires; |
| 51 | enum hrtimer_restart (*function)(struct hrtimer *); |
| 52 | struct hrtimer_clock_base *base; |
| 53 | unsigned long state; |
| 54 | #ifdef CONFIG_TIMER_STATS |
| 55 | int start_pid; |
| 56 | void *start_site; |
| 57 | char start_comm[16]; |
| 58 | #endif |
| 59 | }; |
| 60 | |
| 61 | struct hrtimer_sleeper { |
| 62 | struct hrtimer timer; |
| 63 | struct task_struct *task; |
| 64 | }; |
| 65 | |
| 66 | struct hrtimer_clock_base { |
| 67 | struct hrtimer_cpu_base *cpu_base; |
| 68 | int index; |
| 69 | clockid_t clockid; |
| 70 | struct timerqueue_head active; |
| 71 | ktime_t resolution; |
| 72 | ktime_t (*get_time)(void); |
| 73 | ktime_t softirq_time; |
| 74 | ktime_t offset; |
| 75 | }; |
| 76 | |
| 77 | enum hrtimer_base_type { |
| 78 | HRTIMER_BASE_MONOTONIC, |
| 79 | HRTIMER_BASE_REALTIME, |
| 80 | HRTIMER_BASE_BOOTTIME, |
| 81 | HRTIMER_MAX_CLOCK_BASES, |
| 82 | }; |
| 83 | |
| 84 | struct hrtimer_cpu_base { |
| 85 | raw_spinlock_t lock; |
| 86 | unsigned int active_bases; |
| 87 | unsigned int clock_was_set; |
| 88 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 89 | ktime_t expires_next; |
| 90 | int hres_active; |
| 91 | int hang_detected; |
| 92 | unsigned long nr_events; |
| 93 | unsigned long nr_retries; |
| 94 | unsigned long nr_hangs; |
| 95 | ktime_t max_hang_time; |
| 96 | #endif |
| 97 | struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES]; |
| 98 | }; |
| 99 | |
| 100 | static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time) |
| 101 | { |
| 102 | timer->node.expires = time; |
| 103 | timer->_softexpires = time; |
| 104 | } |
| 105 | |
| 106 | static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta) |
| 107 | { |
| 108 | timer->_softexpires = time; |
| 109 | timer->node.expires = ktime_add_safe(time, delta); |
| 110 | } |
| 111 | |
| 112 | static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, unsigned long delta) |
| 113 | { |
| 114 | timer->_softexpires = time; |
| 115 | timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta)); |
| 116 | } |
| 117 | |
| 118 | static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64) |
| 119 | { |
| 120 | timer->node.expires.tv64 = tv64; |
| 121 | timer->_softexpires.tv64 = tv64; |
| 122 | } |
| 123 | |
| 124 | static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time) |
| 125 | { |
| 126 | timer->node.expires = ktime_add_safe(timer->node.expires, time); |
| 127 | timer->_softexpires = ktime_add_safe(timer->_softexpires, time); |
| 128 | } |
| 129 | |
| 130 | static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns) |
| 131 | { |
| 132 | timer->node.expires = ktime_add_ns(timer->node.expires, ns); |
| 133 | timer->_softexpires = ktime_add_ns(timer->_softexpires, ns); |
| 134 | } |
| 135 | |
| 136 | static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer) |
| 137 | { |
| 138 | return timer->node.expires; |
| 139 | } |
| 140 | |
| 141 | static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer) |
| 142 | { |
| 143 | return timer->_softexpires; |
| 144 | } |
| 145 | |
| 146 | static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer) |
| 147 | { |
| 148 | return timer->node.expires.tv64; |
| 149 | } |
| 150 | static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer) |
| 151 | { |
| 152 | return timer->_softexpires.tv64; |
| 153 | } |
| 154 | |
| 155 | static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer) |
| 156 | { |
| 157 | return ktime_to_ns(timer->node.expires); |
| 158 | } |
| 159 | |
| 160 | static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer) |
| 161 | { |
| 162 | return ktime_sub(timer->node.expires, timer->base->get_time()); |
| 163 | } |
| 164 | |
| 165 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 166 | struct clock_event_device; |
| 167 | |
| 168 | extern void hrtimer_interrupt(struct clock_event_device *dev); |
| 169 | |
| 170 | static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer) |
| 171 | { |
| 172 | return timer->base->get_time(); |
| 173 | } |
| 174 | |
| 175 | static inline int hrtimer_is_hres_active(struct hrtimer *timer) |
| 176 | { |
| 177 | return timer->base->cpu_base->hres_active; |
| 178 | } |
| 179 | |
| 180 | extern void hrtimer_peek_ahead_timers(void); |
| 181 | |
| 182 | # define HIGH_RES_NSEC 1 |
| 183 | # define KTIME_HIGH_RES (ktime_t) { .tv64 = HIGH_RES_NSEC } |
| 184 | # define MONOTONIC_RES_NSEC HIGH_RES_NSEC |
| 185 | # define KTIME_MONOTONIC_RES KTIME_HIGH_RES |
| 186 | |
| 187 | extern void clock_was_set_delayed(void); |
| 188 | |
| 189 | #else |
| 190 | |
| 191 | # define MONOTONIC_RES_NSEC LOW_RES_NSEC |
| 192 | # define KTIME_MONOTONIC_RES KTIME_LOW_RES |
| 193 | |
| 194 | static inline void hrtimer_peek_ahead_timers(void) { } |
| 195 | |
| 196 | static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer) |
| 197 | { |
| 198 | return timer->base->softirq_time; |
| 199 | } |
| 200 | |
| 201 | static inline int hrtimer_is_hres_active(struct hrtimer *timer) |
| 202 | { |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static inline void clock_was_set_delayed(void) { } |
| 207 | |
| 208 | #endif |
| 209 | |
| 210 | extern void clock_was_set(void); |
| 211 | #ifdef CONFIG_TIMERFD |
| 212 | extern void timerfd_clock_was_set(void); |
| 213 | #else |
| 214 | static inline void timerfd_clock_was_set(void) { } |
| 215 | #endif |
| 216 | extern void hrtimers_resume(void); |
| 217 | |
| 218 | extern ktime_t ktime_get(void); |
| 219 | extern ktime_t ktime_get_real(void); |
| 220 | extern ktime_t ktime_get_boottime(void); |
| 221 | extern ktime_t ktime_get_monotonic_offset(void); |
| 222 | extern ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot); |
| 223 | |
| 224 | DECLARE_PER_CPU(struct tick_device, tick_cpu_device); |
| 225 | |
| 226 | |
| 227 | |
| 228 | extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock, |
| 229 | enum hrtimer_mode mode); |
| 230 | |
| 231 | #ifdef CONFIG_DEBUG_OBJECTS_TIMERS |
| 232 | extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock, |
| 233 | enum hrtimer_mode mode); |
| 234 | |
| 235 | extern void destroy_hrtimer_on_stack(struct hrtimer *timer); |
| 236 | #else |
| 237 | static inline void hrtimer_init_on_stack(struct hrtimer *timer, |
| 238 | clockid_t which_clock, |
| 239 | enum hrtimer_mode mode) |
| 240 | { |
| 241 | hrtimer_init(timer, which_clock, mode); |
| 242 | } |
| 243 | static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { } |
| 244 | #endif |
| 245 | |
| 246 | extern int hrtimer_start(struct hrtimer *timer, ktime_t tim, |
| 247 | const enum hrtimer_mode mode); |
| 248 | extern int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, |
| 249 | unsigned long range_ns, const enum hrtimer_mode mode); |
| 250 | extern int |
| 251 | __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, |
| 252 | unsigned long delta_ns, |
| 253 | const enum hrtimer_mode mode, int wakeup); |
| 254 | |
| 255 | extern int hrtimer_cancel(struct hrtimer *timer); |
| 256 | extern int hrtimer_try_to_cancel(struct hrtimer *timer); |
| 257 | |
| 258 | static inline int hrtimer_start_expires(struct hrtimer *timer, |
| 259 | enum hrtimer_mode mode) |
| 260 | { |
| 261 | unsigned long delta; |
| 262 | ktime_t soft, hard; |
| 263 | soft = hrtimer_get_softexpires(timer); |
| 264 | hard = hrtimer_get_expires(timer); |
| 265 | delta = ktime_to_ns(ktime_sub(hard, soft)); |
| 266 | return hrtimer_start_range_ns(timer, soft, delta, mode); |
| 267 | } |
| 268 | |
| 269 | static inline int hrtimer_restart(struct hrtimer *timer) |
| 270 | { |
| 271 | return hrtimer_start_expires(timer, HRTIMER_MODE_ABS); |
| 272 | } |
| 273 | |
| 274 | extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer); |
| 275 | extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp); |
| 276 | |
| 277 | extern ktime_t hrtimer_get_next_event(void); |
| 278 | |
| 279 | static inline int hrtimer_active(const struct hrtimer *timer) |
| 280 | { |
| 281 | return timer->state != HRTIMER_STATE_INACTIVE; |
| 282 | } |
| 283 | |
| 284 | static inline int hrtimer_is_queued(struct hrtimer *timer) |
| 285 | { |
| 286 | return timer->state & HRTIMER_STATE_ENQUEUED; |
| 287 | } |
| 288 | |
| 289 | static inline int hrtimer_callback_running(struct hrtimer *timer) |
| 290 | { |
| 291 | return timer->state & HRTIMER_STATE_CALLBACK; |
| 292 | } |
| 293 | |
| 294 | extern u64 |
| 295 | hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval); |
| 296 | |
| 297 | static inline u64 hrtimer_forward_now(struct hrtimer *timer, |
| 298 | ktime_t interval) |
| 299 | { |
| 300 | return hrtimer_forward(timer, timer->base->get_time(), interval); |
| 301 | } |
| 302 | |
| 303 | extern long hrtimer_nanosleep(struct timespec *rqtp, |
| 304 | struct timespec __user *rmtp, |
| 305 | const enum hrtimer_mode mode, |
| 306 | const clockid_t clockid); |
| 307 | extern long hrtimer_nanosleep_restart(struct restart_block *restart_block); |
| 308 | |
| 309 | extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, |
| 310 | struct task_struct *tsk); |
| 311 | |
| 312 | extern int schedule_hrtimeout_range(ktime_t *expires, unsigned long delta, |
| 313 | const enum hrtimer_mode mode); |
| 314 | extern int schedule_hrtimeout_range_clock(ktime_t *expires, |
| 315 | unsigned long delta, const enum hrtimer_mode mode, int clock); |
| 316 | extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode); |
| 317 | |
| 318 | extern void hrtimer_run_queues(void); |
| 319 | extern void hrtimer_run_pending(void); |
| 320 | |
| 321 | extern void __init hrtimers_init(void); |
| 322 | |
| 323 | #if BITS_PER_LONG < 64 |
| 324 | extern u64 ktime_divns(const ktime_t kt, s64 div); |
| 325 | #else |
| 326 | # define ktime_divns(kt, div) (u64)((kt).tv64 / (div)) |
| 327 | #endif |
| 328 | |
| 329 | extern void sysrq_timer_list_show(void); |
| 330 | |
| 331 | #endif |