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; 56*05cfb614SRoman Zippel int (*function)(struct hrtimer *); 57c0a31329SThomas Gleixner struct hrtimer_base *base; 58c0a31329SThomas Gleixner }; 59c0a31329SThomas Gleixner 60c0a31329SThomas Gleixner /** 61c0a31329SThomas Gleixner * struct hrtimer_base - the timer base for a specific clock 62c0a31329SThomas Gleixner * 63c0a31329SThomas Gleixner * @index: clock type index for per_cpu support when moving a timer 64c0a31329SThomas Gleixner * to a base on another cpu. 65c0a31329SThomas Gleixner * @lock: lock protecting the base and associated timers 66c0a31329SThomas Gleixner * @active: red black tree root node for the active timers 67288867ecSThomas Gleixner * @first: pointer to the timer node which expires first 68c0a31329SThomas Gleixner * @resolution: the resolution of the clock, in nanoseconds 69c0a31329SThomas Gleixner * @get_time: function to retrieve the current time of the clock 7092127c7aSThomas Gleixner * @get_sofirq_time: function to retrieve the current time from the softirq 71c0a31329SThomas Gleixner * @curr_timer: the timer which is executing a callback right now 7292127c7aSThomas Gleixner * @softirq_time: the time when running the hrtimer queue in the softirq 73c0a31329SThomas Gleixner */ 74c0a31329SThomas Gleixner struct hrtimer_base { 75c0a31329SThomas Gleixner clockid_t index; 76c0a31329SThomas Gleixner spinlock_t lock; 77c0a31329SThomas Gleixner struct rb_root active; 78288867ecSThomas Gleixner struct rb_node *first; 79e2787630SThomas Gleixner ktime_t resolution; 80c0a31329SThomas Gleixner ktime_t (*get_time)(void); 8192127c7aSThomas Gleixner ktime_t (*get_softirq_time)(void); 82c0a31329SThomas Gleixner struct hrtimer *curr_timer; 8392127c7aSThomas Gleixner ktime_t softirq_time; 84c0a31329SThomas Gleixner }; 85c0a31329SThomas Gleixner 86becf8b5dSThomas Gleixner /* 87becf8b5dSThomas Gleixner * clock_was_set() is a NOP for non- high-resolution systems. The 88becf8b5dSThomas Gleixner * time-sorted order guarantees that a timer does not expire early and 89becf8b5dSThomas Gleixner * is expired in the next softirq when the clock was advanced. 90becf8b5dSThomas Gleixner */ 91becf8b5dSThomas Gleixner #define clock_was_set() do { } while (0) 92becf8b5dSThomas Gleixner 93c0a31329SThomas Gleixner /* Exported timer functions: */ 94c0a31329SThomas Gleixner 95c0a31329SThomas Gleixner /* Initialize timers: */ 967978672cSGeorge Anzinger extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock, 977978672cSGeorge Anzinger enum hrtimer_mode mode); 98c0a31329SThomas Gleixner 99c0a31329SThomas Gleixner /* Basic timer operations: */ 100c0a31329SThomas Gleixner extern int hrtimer_start(struct hrtimer *timer, ktime_t tim, 101c0a31329SThomas Gleixner const enum hrtimer_mode mode); 102c0a31329SThomas Gleixner extern int hrtimer_cancel(struct hrtimer *timer); 103c0a31329SThomas Gleixner extern int hrtimer_try_to_cancel(struct hrtimer *timer); 104c0a31329SThomas Gleixner 105c0a31329SThomas Gleixner #define hrtimer_restart(timer) hrtimer_start((timer), (timer)->expires, HRTIMER_ABS) 106c0a31329SThomas Gleixner 107c0a31329SThomas Gleixner /* Query timers: */ 108c0a31329SThomas Gleixner extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer); 109c0a31329SThomas Gleixner extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp); 110c0a31329SThomas Gleixner 11169239749STony Lindgren #ifdef CONFIG_NO_IDLE_HZ 11269239749STony Lindgren extern ktime_t hrtimer_get_next_event(void); 11369239749STony Lindgren #endif 11469239749STony Lindgren 115c0a31329SThomas Gleixner static inline int hrtimer_active(const struct hrtimer *timer) 116c0a31329SThomas Gleixner { 117b75f7a51SRoman Zippel return timer->node.rb_parent != HRTIMER_INACTIVE; 118c0a31329SThomas Gleixner } 119c0a31329SThomas Gleixner 120c0a31329SThomas Gleixner /* Forward a hrtimer so it expires after now: */ 12144f21475SRoman Zippel extern unsigned long 12244f21475SRoman Zippel hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval); 123c0a31329SThomas Gleixner 12410c94ec1SThomas Gleixner /* Precise sleep: */ 12510c94ec1SThomas Gleixner extern long hrtimer_nanosleep(struct timespec *rqtp, 12610c94ec1SThomas Gleixner struct timespec __user *rmtp, 12710c94ec1SThomas Gleixner const enum hrtimer_mode mode, 12810c94ec1SThomas Gleixner const clockid_t clockid); 12910c94ec1SThomas Gleixner 130c0a31329SThomas Gleixner /* Soft interrupt function to run the hrtimer queues: */ 131c0a31329SThomas Gleixner extern void hrtimer_run_queues(void); 132c0a31329SThomas Gleixner 133c0a31329SThomas Gleixner /* Bootup initialization: */ 134c0a31329SThomas Gleixner extern void __init hrtimers_init(void); 135c0a31329SThomas Gleixner 136c0a31329SThomas Gleixner #endif 137