1c0a31329SThomas Gleixner /* 2c0a31329SThomas Gleixner * include/linux/hrtimer.h 3c0a31329SThomas Gleixner * 4c0a31329SThomas Gleixner * hrtimers - High-resolution kernel timers 5c0a31329SThomas Gleixner * 6c0a31329SThomas Gleixner * Copyright(C) 2005, Thomas Gleixner <[email protected]> 7c0a31329SThomas Gleixner * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar 8c0a31329SThomas Gleixner * 9c0a31329SThomas Gleixner * data type definitions, declarations, prototypes 10c0a31329SThomas Gleixner * 11c0a31329SThomas Gleixner * Started by: Thomas Gleixner and Ingo Molnar 12c0a31329SThomas Gleixner * 13c0a31329SThomas Gleixner * For licencing details see kernel-base/COPYING 14c0a31329SThomas Gleixner */ 15c0a31329SThomas Gleixner #ifndef _LINUX_HRTIMER_H 16c0a31329SThomas Gleixner #define _LINUX_HRTIMER_H 17c0a31329SThomas Gleixner 18c0a31329SThomas Gleixner #include <linux/rbtree.h> 19c0a31329SThomas Gleixner #include <linux/ktime.h> 20c0a31329SThomas Gleixner #include <linux/init.h> 21c0a31329SThomas Gleixner #include <linux/list.h> 22c0a31329SThomas Gleixner #include <linux/wait.h> 23c0a31329SThomas Gleixner 24c0a31329SThomas Gleixner /* 25c0a31329SThomas Gleixner * Mode arguments of xxx_hrtimer functions: 26c0a31329SThomas Gleixner */ 27c0a31329SThomas Gleixner enum hrtimer_mode { 28c0a31329SThomas Gleixner HRTIMER_ABS, /* Time value is absolute */ 29c0a31329SThomas Gleixner HRTIMER_REL, /* Time value is relative to now */ 30c0a31329SThomas Gleixner }; 31c0a31329SThomas Gleixner 32c0a31329SThomas Gleixner enum hrtimer_restart { 33c0a31329SThomas Gleixner HRTIMER_NORESTART, 34c0a31329SThomas Gleixner HRTIMER_RESTART, 35c0a31329SThomas Gleixner }; 36c0a31329SThomas Gleixner 37b75f7a51SRoman Zippel #define HRTIMER_INACTIVE ((void *)1UL) 38c0a31329SThomas Gleixner 39c0a31329SThomas Gleixner struct hrtimer_base; 40c0a31329SThomas Gleixner 41c0a31329SThomas Gleixner /** 42c0a31329SThomas Gleixner * struct hrtimer - the basic hrtimer structure 43c0a31329SThomas Gleixner * 44c0a31329SThomas Gleixner * @node: red black tree node for time ordered insertion 45c0a31329SThomas Gleixner * @expires: the absolute expiry time in the hrtimers internal 46c0a31329SThomas Gleixner * representation. The time is related to the clock on 47c0a31329SThomas Gleixner * which the timer is based. 48c0a31329SThomas Gleixner * @function: timer expiry callback function 49c0a31329SThomas Gleixner * @base: pointer to the timer base (per cpu and per clock) 50c0a31329SThomas Gleixner * 51c0a31329SThomas Gleixner * The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE() 52c0a31329SThomas Gleixner */ 53c0a31329SThomas Gleixner struct hrtimer { 54c0a31329SThomas Gleixner struct rb_node node; 55c0a31329SThomas Gleixner ktime_t expires; 5605cfb614SRoman Zippel int (*function)(struct hrtimer *); 57c0a31329SThomas Gleixner struct hrtimer_base *base; 58c0a31329SThomas Gleixner }; 59c0a31329SThomas Gleixner 60c0a31329SThomas Gleixner /** 6100362e33SThomas Gleixner * struct hrtimer_sleeper - simple sleeper structure 6200362e33SThomas Gleixner * 6300362e33SThomas Gleixner * @timer: embedded timer structure 6400362e33SThomas Gleixner * @task: task to wake up 6500362e33SThomas Gleixner * 6600362e33SThomas Gleixner * task is set to NULL, when the timer expires. 6700362e33SThomas Gleixner */ 6800362e33SThomas Gleixner struct hrtimer_sleeper { 6900362e33SThomas Gleixner struct hrtimer timer; 7000362e33SThomas Gleixner struct task_struct *task; 7100362e33SThomas Gleixner }; 7200362e33SThomas Gleixner 7300362e33SThomas Gleixner /** 74c0a31329SThomas Gleixner * struct hrtimer_base - the timer base for a specific clock 75c0a31329SThomas Gleixner * 76c0a31329SThomas Gleixner * @index: clock type index for per_cpu support when moving a timer 77c0a31329SThomas Gleixner * to a base on another cpu. 78c0a31329SThomas Gleixner * @lock: lock protecting the base and associated timers 79c0a31329SThomas Gleixner * @active: red black tree root node for the active timers 80288867ecSThomas Gleixner * @first: pointer to the timer node which expires first 81c0a31329SThomas Gleixner * @resolution: the resolution of the clock, in nanoseconds 82c0a31329SThomas Gleixner * @get_time: function to retrieve the current time of the clock 83a580290cSMartin Waitz * @get_softirq_time: function to retrieve the current time from the softirq 84c0a31329SThomas Gleixner * @curr_timer: the timer which is executing a callback right now 8592127c7aSThomas Gleixner * @softirq_time: the time when running the hrtimer queue in the softirq 86c0a31329SThomas Gleixner */ 87c0a31329SThomas Gleixner struct hrtimer_base { 88c0a31329SThomas Gleixner clockid_t index; 89c0a31329SThomas Gleixner spinlock_t lock; 90c0a31329SThomas Gleixner struct rb_root active; 91288867ecSThomas Gleixner struct rb_node *first; 92e2787630SThomas Gleixner ktime_t resolution; 93c0a31329SThomas Gleixner ktime_t (*get_time)(void); 9492127c7aSThomas Gleixner ktime_t (*get_softirq_time)(void); 95c0a31329SThomas Gleixner struct hrtimer *curr_timer; 9692127c7aSThomas Gleixner ktime_t softirq_time; 97c0a31329SThomas Gleixner }; 98c0a31329SThomas Gleixner 99becf8b5dSThomas Gleixner /* 100becf8b5dSThomas Gleixner * clock_was_set() is a NOP for non- high-resolution systems. The 101becf8b5dSThomas Gleixner * time-sorted order guarantees that a timer does not expire early and 102becf8b5dSThomas Gleixner * is expired in the next softirq when the clock was advanced. 103becf8b5dSThomas Gleixner */ 104becf8b5dSThomas Gleixner #define clock_was_set() do { } while (0) 105becf8b5dSThomas Gleixner 106c0a31329SThomas Gleixner /* Exported timer functions: */ 107c0a31329SThomas Gleixner 108c0a31329SThomas Gleixner /* Initialize timers: */ 1097978672cSGeorge Anzinger extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock, 1107978672cSGeorge Anzinger enum hrtimer_mode mode); 111c0a31329SThomas Gleixner 112c0a31329SThomas Gleixner /* Basic timer operations: */ 113c0a31329SThomas Gleixner extern int hrtimer_start(struct hrtimer *timer, ktime_t tim, 114c0a31329SThomas Gleixner const enum hrtimer_mode mode); 115c0a31329SThomas Gleixner extern int hrtimer_cancel(struct hrtimer *timer); 116c0a31329SThomas Gleixner extern int hrtimer_try_to_cancel(struct hrtimer *timer); 117c0a31329SThomas Gleixner 118c0a31329SThomas Gleixner #define hrtimer_restart(timer) hrtimer_start((timer), (timer)->expires, HRTIMER_ABS) 119c0a31329SThomas Gleixner 120c0a31329SThomas Gleixner /* Query timers: */ 121c0a31329SThomas Gleixner extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer); 122c0a31329SThomas Gleixner extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp); 123c0a31329SThomas Gleixner 12469239749STony Lindgren #ifdef CONFIG_NO_IDLE_HZ 12569239749STony Lindgren extern ktime_t hrtimer_get_next_event(void); 12669239749STony Lindgren #endif 12769239749STony Lindgren 128c0a31329SThomas Gleixner static inline int hrtimer_active(const struct hrtimer *timer) 129c0a31329SThomas Gleixner { 130*ed198cb4SDavid Woodhouse return rb_parent(&timer->node) != &timer->node; 131c0a31329SThomas Gleixner } 132c0a31329SThomas Gleixner 133c0a31329SThomas Gleixner /* Forward a hrtimer so it expires after now: */ 13444f21475SRoman Zippel extern unsigned long 13544f21475SRoman Zippel hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval); 136c0a31329SThomas Gleixner 13710c94ec1SThomas Gleixner /* Precise sleep: */ 13810c94ec1SThomas Gleixner extern long hrtimer_nanosleep(struct timespec *rqtp, 13910c94ec1SThomas Gleixner struct timespec __user *rmtp, 14010c94ec1SThomas Gleixner const enum hrtimer_mode mode, 14110c94ec1SThomas Gleixner const clockid_t clockid); 14210c94ec1SThomas Gleixner 14300362e33SThomas Gleixner extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, 14400362e33SThomas Gleixner struct task_struct *tsk); 14500362e33SThomas Gleixner 146c0a31329SThomas Gleixner /* Soft interrupt function to run the hrtimer queues: */ 147c0a31329SThomas Gleixner extern void hrtimer_run_queues(void); 148c0a31329SThomas Gleixner 149c0a31329SThomas Gleixner /* Bootup initialization: */ 150c0a31329SThomas Gleixner extern void __init hrtimers_init(void); 151c0a31329SThomas Gleixner 152c0a31329SThomas Gleixner #endif 153