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> 2340b86062SStephen Rothwell #include <linux/percpu.h> 24507e1231SHeiko Carstens #include <linux/timer.h> 25998adc3dSJohn Stultz #include <linux/timerqueue.h> 26c0a31329SThomas Gleixner 273c8aa39dSThomas Gleixner struct hrtimer_clock_base; 283c8aa39dSThomas Gleixner struct hrtimer_cpu_base; 293c8aa39dSThomas Gleixner 30c0a31329SThomas Gleixner /* 31c0a31329SThomas Gleixner * Mode arguments of xxx_hrtimer functions: 32c0a31329SThomas Gleixner */ 33c0a31329SThomas Gleixner enum hrtimer_mode { 34597d0275SArun R Bharadwaj HRTIMER_MODE_ABS = 0x0, /* Time value is absolute */ 35597d0275SArun R Bharadwaj HRTIMER_MODE_REL = 0x1, /* Time value is relative to now */ 36597d0275SArun R Bharadwaj HRTIMER_MODE_PINNED = 0x02, /* Timer is bound to CPU */ 37597d0275SArun R Bharadwaj HRTIMER_MODE_ABS_PINNED = 0x02, 38597d0275SArun R Bharadwaj HRTIMER_MODE_REL_PINNED = 0x03, 39c0a31329SThomas Gleixner }; 40c0a31329SThomas Gleixner 41c9cb2e3dSThomas Gleixner /* 42c9cb2e3dSThomas Gleixner * Return values for the callback function 43c9cb2e3dSThomas Gleixner */ 44c0a31329SThomas Gleixner enum hrtimer_restart { 45c9cb2e3dSThomas Gleixner HRTIMER_NORESTART, /* Timer is not restarted */ 46c9cb2e3dSThomas Gleixner HRTIMER_RESTART, /* Timer must be restarted */ 47c0a31329SThomas Gleixner }; 48c0a31329SThomas Gleixner 49303e967fSThomas Gleixner /* 5054cdfdb4SThomas Gleixner * Values to track state of the timer 51303e967fSThomas Gleixner * 52303e967fSThomas Gleixner * Possible states: 53303e967fSThomas Gleixner * 54303e967fSThomas Gleixner * 0x00 inactive 55303e967fSThomas Gleixner * 0x01 enqueued into rbtree 56303e967fSThomas Gleixner * 0x02 callback function running 5753370d2eSThomas Gleixner * 0x04 timer is migrated to another cpu 5854cdfdb4SThomas Gleixner * 59b00c1a99SThomas Gleixner * Special cases: 60303e967fSThomas Gleixner * 0x03 callback function running and enqueued 61303e967fSThomas Gleixner * (was requeued on another CPU) 6253370d2eSThomas Gleixner * 0x05 timer was migrated on CPU hotunplug 6353370d2eSThomas Gleixner * 64303e967fSThomas Gleixner * The "callback function running and enqueued" status is only possible on 65303e967fSThomas Gleixner * SMP. It happens for example when a posix timer expired and the callback 66303e967fSThomas Gleixner * queued a signal. Between dropping the lock which protects the posix timer 67303e967fSThomas Gleixner * and reacquiring the base lock of the hrtimer, another CPU can deliver the 68303e967fSThomas Gleixner * signal and rearm the timer. We have to preserve the callback running state, 69303e967fSThomas Gleixner * as otherwise the timer could be removed before the softirq code finishes the 70303e967fSThomas Gleixner * the handling of the timer. 71303e967fSThomas Gleixner * 7253370d2eSThomas Gleixner * The HRTIMER_STATE_ENQUEUED bit is always or'ed to the current state 7353370d2eSThomas Gleixner * to preserve the HRTIMER_STATE_CALLBACK in the above scenario. This 7453370d2eSThomas Gleixner * also affects HRTIMER_STATE_MIGRATE where the preservation is not 7553370d2eSThomas Gleixner * necessary. HRTIMER_STATE_MIGRATE is cleared after the timer is 7653370d2eSThomas Gleixner * enqueued on the new cpu. 77303e967fSThomas Gleixner * 78303e967fSThomas Gleixner * All state transitions are protected by cpu_base->lock. 79303e967fSThomas Gleixner */ 80303e967fSThomas Gleixner #define HRTIMER_STATE_INACTIVE 0x00 81303e967fSThomas Gleixner #define HRTIMER_STATE_ENQUEUED 0x01 82303e967fSThomas Gleixner #define HRTIMER_STATE_CALLBACK 0x02 83ca109491SPeter Zijlstra #define HRTIMER_STATE_MIGRATE 0x04 84303e967fSThomas Gleixner 85c0a31329SThomas Gleixner /** 86c0a31329SThomas Gleixner * struct hrtimer - the basic hrtimer structure 87998adc3dSJohn Stultz * @node: timerqueue node, which also manages node.expires, 88998adc3dSJohn Stultz * the absolute expiry time in the hrtimers internal 89c0a31329SThomas Gleixner * representation. The time is related to the clock on 90592aa999SThomas Gleixner * which the timer is based. Is setup by adding 91592aa999SThomas Gleixner * slack to the _softexpires value. For non range timers 92592aa999SThomas Gleixner * identical to _softexpires. 93592aa999SThomas Gleixner * @_softexpires: the absolute earliest expiry time of the hrtimer. 94592aa999SThomas Gleixner * The time which was given as expiry time when the timer 95592aa999SThomas Gleixner * was armed. 96c0a31329SThomas Gleixner * @function: timer expiry callback function 97c0a31329SThomas Gleixner * @base: pointer to the timer base (per cpu and per clock) 98303e967fSThomas Gleixner * @state: state information (See bit values above) 99e2e680fbSViresh Kumar * @start_pid: timer statistics field to store the pid of the task which 100e2e680fbSViresh Kumar * started the timer 10154cdfdb4SThomas Gleixner * @start_site: timer statistics field to store the site where the timer 10254cdfdb4SThomas Gleixner * was started 10354cdfdb4SThomas Gleixner * @start_comm: timer statistics field to store the name of the process which 10454cdfdb4SThomas Gleixner * started the timer 105c0a31329SThomas Gleixner * 10654cdfdb4SThomas Gleixner * The hrtimer structure must be initialized by hrtimer_init() 107c0a31329SThomas Gleixner */ 108c0a31329SThomas Gleixner struct hrtimer { 109998adc3dSJohn Stultz struct timerqueue_node node; 110654c8e0bSArjan van de Ven ktime_t _softexpires; 111c9cb2e3dSThomas Gleixner enum hrtimer_restart (*function)(struct hrtimer *); 1123c8aa39dSThomas Gleixner struct hrtimer_clock_base *base; 113303e967fSThomas Gleixner unsigned long state; 11482f67cd9SIngo Molnar #ifdef CONFIG_TIMER_STATS 1151b024690SRichard Kennedy int start_pid; 11682f67cd9SIngo Molnar void *start_site; 11782f67cd9SIngo Molnar char start_comm[16]; 11882f67cd9SIngo Molnar #endif 119c0a31329SThomas Gleixner }; 120c0a31329SThomas Gleixner 121c0a31329SThomas Gleixner /** 12200362e33SThomas Gleixner * struct hrtimer_sleeper - simple sleeper structure 12300362e33SThomas Gleixner * @timer: embedded timer structure 12400362e33SThomas Gleixner * @task: task to wake up 12500362e33SThomas Gleixner * 12600362e33SThomas Gleixner * task is set to NULL, when the timer expires. 12700362e33SThomas Gleixner */ 12800362e33SThomas Gleixner struct hrtimer_sleeper { 12900362e33SThomas Gleixner struct hrtimer timer; 13000362e33SThomas Gleixner struct task_struct *task; 13100362e33SThomas Gleixner }; 13200362e33SThomas Gleixner 133b8e38413SThomas Gleixner #ifdef CONFIG_64BIT 134b8e38413SThomas Gleixner # define HRTIMER_CLOCK_BASE_ALIGN 64 135b8e38413SThomas Gleixner #else 136b8e38413SThomas Gleixner # define HRTIMER_CLOCK_BASE_ALIGN 32 137b8e38413SThomas Gleixner #endif 138b8e38413SThomas Gleixner 13900362e33SThomas Gleixner /** 140d1d67174SAndres Salomon * struct hrtimer_clock_base - the timer base for a specific clock 14105fb6bf0SRandy Dunlap * @cpu_base: per cpu clock base 1423c8aa39dSThomas Gleixner * @index: clock type index for per_cpu support when moving a 1433c8aa39dSThomas Gleixner * timer to a base on another cpu. 1444d258b25SVitaliy Ivanov * @clockid: clock id for per_cpu support 145c0a31329SThomas Gleixner * @active: red black tree root node for the active timers 146c0a31329SThomas Gleixner * @get_time: function to retrieve the current time of the clock 14754cdfdb4SThomas Gleixner * @offset: offset of this clock to the monotonic base 148c0a31329SThomas Gleixner */ 1493c8aa39dSThomas Gleixner struct hrtimer_clock_base { 1503c8aa39dSThomas Gleixner struct hrtimer_cpu_base *cpu_base; 151ab8177bcSThomas Gleixner int index; 152ab8177bcSThomas Gleixner clockid_t clockid; 153998adc3dSJohn Stultz struct timerqueue_head active; 154c0a31329SThomas Gleixner ktime_t (*get_time)(void); 15554cdfdb4SThomas Gleixner ktime_t offset; 156b8e38413SThomas Gleixner } __attribute__((__aligned__(HRTIMER_CLOCK_BASE_ALIGN))); 1573c8aa39dSThomas Gleixner 158e06383dbSJohn Stultz enum hrtimer_base_type { 159e06383dbSJohn Stultz HRTIMER_BASE_MONOTONIC, 16068fa61c0SThomas Gleixner HRTIMER_BASE_REALTIME, 16170a08ccaSJohn Stultz HRTIMER_BASE_BOOTTIME, 16290adda98SJohn Stultz HRTIMER_BASE_TAI, 163e06383dbSJohn Stultz HRTIMER_MAX_CLOCK_BASES, 164e06383dbSJohn Stultz }; 1653c8aa39dSThomas Gleixner 1663c8aa39dSThomas Gleixner /* 1673c8aa39dSThomas Gleixner * struct hrtimer_cpu_base - the per cpu clock bases 1683c8aa39dSThomas Gleixner * @lock: lock protecting the base and associated clock bases 1693c8aa39dSThomas Gleixner * and timers 170cddd0248SViresh Kumar * @cpu: cpu number 171ab8177bcSThomas Gleixner * @active_bases: Bitfield to mark bases with active timers 172868a3e91SThomas Gleixner * @clock_was_set_seq: Sequence counter of clock was set events 17354cdfdb4SThomas Gleixner * @expires_next: absolute time of the next event which was scheduled 17454cdfdb4SThomas Gleixner * via clock_set_next_event() 175*895bdfa7SThomas Gleixner * @next_timer: Pointer to the first expiring timer 1769bc74919SThomas Gleixner * @in_hrtirq: hrtimer_interrupt() is currently executing 17754cdfdb4SThomas Gleixner * @hres_active: State of high resolution mode 17841d2e494SThomas Gleixner * @hang_detected: The last hrtimer interrupt detected a hang 17941d2e494SThomas Gleixner * @nr_events: Total number of hrtimer interrupt events 18041d2e494SThomas Gleixner * @nr_retries: Total number of hrtimer interrupt retries 18141d2e494SThomas Gleixner * @nr_hangs: Total number of hrtimer interrupt hangs 18241d2e494SThomas Gleixner * @max_hang_time: Maximum time spent in hrtimer_interrupt 183ab8177bcSThomas Gleixner * @clock_base: array of clock bases for this cpu 184*895bdfa7SThomas Gleixner * 185*895bdfa7SThomas Gleixner * Note: next_timer is just an optimization for __remove_hrtimer(). 186*895bdfa7SThomas Gleixner * Do not dereference the pointer because it is not reliable on 187*895bdfa7SThomas Gleixner * cross cpu removals. 1883c8aa39dSThomas Gleixner */ 1893c8aa39dSThomas Gleixner struct hrtimer_cpu_base { 190ecb49d1aSThomas Gleixner raw_spinlock_t lock; 191cddd0248SViresh Kumar unsigned int cpu; 192f55a6faaSJohn Stultz unsigned int active_bases; 193868a3e91SThomas Gleixner unsigned int clock_was_set_seq; 19454cdfdb4SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS 195e19ffe8bSThomas Gleixner unsigned int in_hrtirq : 1, 196e19ffe8bSThomas Gleixner hres_active : 1, 197e19ffe8bSThomas Gleixner hang_detected : 1; 19854cdfdb4SThomas Gleixner ktime_t expires_next; 199*895bdfa7SThomas Gleixner struct hrtimer *next_timer; 200a6ffebceSThomas Gleixner unsigned int nr_events; 201a6ffebceSThomas Gleixner unsigned int nr_retries; 202a6ffebceSThomas Gleixner unsigned int nr_hangs; 203a6ffebceSThomas Gleixner unsigned int max_hang_time; 20454cdfdb4SThomas Gleixner #endif 205f24444b0SThomas Gleixner struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES]; 2066d9a1411SThomas Gleixner } ____cacheline_aligned; 207c0a31329SThomas Gleixner 20863ca243bSArjan van de Ven static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time) 20963ca243bSArjan van de Ven { 210b8e38413SThomas Gleixner BUILD_BUG_ON(sizeof(struct hrtimer_clock_base) > HRTIMER_CLOCK_BASE_ALIGN); 211b8e38413SThomas Gleixner 212998adc3dSJohn Stultz timer->node.expires = time; 213654c8e0bSArjan van de Ven timer->_softexpires = time; 21463ca243bSArjan van de Ven } 215654c8e0bSArjan van de Ven 216654c8e0bSArjan van de Ven static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta) 217654c8e0bSArjan van de Ven { 218654c8e0bSArjan van de Ven timer->_softexpires = time; 219998adc3dSJohn Stultz timer->node.expires = ktime_add_safe(time, delta); 220654c8e0bSArjan van de Ven } 221654c8e0bSArjan van de Ven 222654c8e0bSArjan van de Ven static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, unsigned long delta) 223654c8e0bSArjan van de Ven { 224654c8e0bSArjan van de Ven timer->_softexpires = time; 225998adc3dSJohn Stultz timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta)); 226654c8e0bSArjan van de Ven } 227654c8e0bSArjan van de Ven 22863ca243bSArjan van de Ven static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64) 22963ca243bSArjan van de Ven { 230998adc3dSJohn Stultz timer->node.expires.tv64 = tv64; 231654c8e0bSArjan van de Ven timer->_softexpires.tv64 = tv64; 23263ca243bSArjan van de Ven } 23363ca243bSArjan van de Ven 23463ca243bSArjan van de Ven static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time) 23563ca243bSArjan van de Ven { 236998adc3dSJohn Stultz timer->node.expires = ktime_add_safe(timer->node.expires, time); 237654c8e0bSArjan van de Ven timer->_softexpires = ktime_add_safe(timer->_softexpires, time); 23863ca243bSArjan van de Ven } 23963ca243bSArjan van de Ven 2407597bc94SDavid Howells static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns) 24163ca243bSArjan van de Ven { 242998adc3dSJohn Stultz timer->node.expires = ktime_add_ns(timer->node.expires, ns); 243654c8e0bSArjan van de Ven timer->_softexpires = ktime_add_ns(timer->_softexpires, ns); 24463ca243bSArjan van de Ven } 24563ca243bSArjan van de Ven 24663ca243bSArjan van de Ven static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer) 24763ca243bSArjan van de Ven { 248998adc3dSJohn Stultz return timer->node.expires; 24963ca243bSArjan van de Ven } 25063ca243bSArjan van de Ven 251654c8e0bSArjan van de Ven static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer) 252654c8e0bSArjan van de Ven { 253654c8e0bSArjan van de Ven return timer->_softexpires; 254654c8e0bSArjan van de Ven } 255654c8e0bSArjan van de Ven 25663ca243bSArjan van de Ven static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer) 25763ca243bSArjan van de Ven { 258998adc3dSJohn Stultz return timer->node.expires.tv64; 25963ca243bSArjan van de Ven } 260654c8e0bSArjan van de Ven static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer) 261654c8e0bSArjan van de Ven { 262654c8e0bSArjan van de Ven return timer->_softexpires.tv64; 263654c8e0bSArjan van de Ven } 26463ca243bSArjan van de Ven 26563ca243bSArjan van de Ven static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer) 26663ca243bSArjan van de Ven { 267998adc3dSJohn Stultz return ktime_to_ns(timer->node.expires); 26863ca243bSArjan van de Ven } 26963ca243bSArjan van de Ven 27063ca243bSArjan van de Ven static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer) 27163ca243bSArjan van de Ven { 272998adc3dSJohn Stultz return ktime_sub(timer->node.expires, timer->base->get_time()); 27363ca243bSArjan van de Ven } 27463ca243bSArjan van de Ven 27554cdfdb4SThomas Gleixner static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer) 27654cdfdb4SThomas Gleixner { 27754cdfdb4SThomas Gleixner return timer->base->get_time(); 27854cdfdb4SThomas Gleixner } 27954cdfdb4SThomas Gleixner 28021d6d52aSThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS 28121d6d52aSThomas Gleixner struct clock_event_device; 28221d6d52aSThomas Gleixner 28321d6d52aSThomas Gleixner extern void hrtimer_interrupt(struct clock_event_device *dev); 28421d6d52aSThomas Gleixner 2858f4d37ecSPeter Zijlstra static inline int hrtimer_is_hres_active(struct hrtimer *timer) 2868f4d37ecSPeter Zijlstra { 2878f4d37ecSPeter Zijlstra return timer->base->cpu_base->hres_active; 2888f4d37ecSPeter Zijlstra } 2898f4d37ecSPeter Zijlstra 2902075eb8dSArjan van de Ven extern void hrtimer_peek_ahead_timers(void); 2912075eb8dSArjan van de Ven 29254cdfdb4SThomas Gleixner /* 29354cdfdb4SThomas Gleixner * The resolution of the clocks. The resolution value is returned in 29454cdfdb4SThomas Gleixner * the clock_getres() system call to give application programmers an 29554cdfdb4SThomas Gleixner * idea of the (in)accuracy of timers. Timer values are rounded up to 29654cdfdb4SThomas Gleixner * this resolution values. 29754cdfdb4SThomas Gleixner */ 298151db1fcSTony Breeds # define HIGH_RES_NSEC 1 299151db1fcSTony Breeds # define KTIME_HIGH_RES (ktime_t) { .tv64 = HIGH_RES_NSEC } 300151db1fcSTony Breeds # define MONOTONIC_RES_NSEC HIGH_RES_NSEC 30154cdfdb4SThomas Gleixner # define KTIME_MONOTONIC_RES KTIME_HIGH_RES 30254cdfdb4SThomas Gleixner 303f55a6faaSJohn Stultz extern void clock_was_set_delayed(void); 304f55a6faaSJohn Stultz 305398ca17fSThomas Gleixner extern unsigned int hrtimer_resolution; 306398ca17fSThomas Gleixner 30754cdfdb4SThomas Gleixner #else 30854cdfdb4SThomas Gleixner 309151db1fcSTony Breeds # define MONOTONIC_RES_NSEC LOW_RES_NSEC 31054cdfdb4SThomas Gleixner # define KTIME_MONOTONIC_RES KTIME_LOW_RES 31154cdfdb4SThomas Gleixner 312398ca17fSThomas Gleixner #define hrtimer_resolution LOW_RES_NSEC 313398ca17fSThomas Gleixner 3142075eb8dSArjan van de Ven static inline void hrtimer_peek_ahead_timers(void) { } 31554cdfdb4SThomas Gleixner 3168f4d37ecSPeter Zijlstra static inline int hrtimer_is_hres_active(struct hrtimer *timer) 3178f4d37ecSPeter Zijlstra { 3188f4d37ecSPeter Zijlstra return 0; 3198f4d37ecSPeter Zijlstra } 320f55a6faaSJohn Stultz 321f55a6faaSJohn Stultz static inline void clock_was_set_delayed(void) { } 322f55a6faaSJohn Stultz 32354cdfdb4SThomas Gleixner #endif 32454cdfdb4SThomas Gleixner 325b12a03ceSThomas Gleixner extern void clock_was_set(void); 3269ec26907SThomas Gleixner #ifdef CONFIG_TIMERFD 3279ec26907SThomas Gleixner extern void timerfd_clock_was_set(void); 3289ec26907SThomas Gleixner #else 3299ec26907SThomas Gleixner static inline void timerfd_clock_was_set(void) { } 3309ec26907SThomas Gleixner #endif 331b12a03ceSThomas Gleixner extern void hrtimers_resume(void); 332b12a03ceSThomas Gleixner 3332e94d1f7SArjan van de Ven DECLARE_PER_CPU(struct tick_device, tick_cpu_device); 3342e94d1f7SArjan van de Ven 3352e94d1f7SArjan van de Ven 336c0a31329SThomas Gleixner /* Exported timer functions: */ 337c0a31329SThomas Gleixner 338c0a31329SThomas Gleixner /* Initialize timers: */ 3397978672cSGeorge Anzinger extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock, 3407978672cSGeorge Anzinger enum hrtimer_mode mode); 341c0a31329SThomas Gleixner 342237fc6e7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS 343237fc6e7SThomas Gleixner extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock, 344237fc6e7SThomas Gleixner enum hrtimer_mode mode); 345237fc6e7SThomas Gleixner 346237fc6e7SThomas Gleixner extern void destroy_hrtimer_on_stack(struct hrtimer *timer); 347237fc6e7SThomas Gleixner #else 348237fc6e7SThomas Gleixner static inline void hrtimer_init_on_stack(struct hrtimer *timer, 349237fc6e7SThomas Gleixner clockid_t which_clock, 350237fc6e7SThomas Gleixner enum hrtimer_mode mode) 351237fc6e7SThomas Gleixner { 352237fc6e7SThomas Gleixner hrtimer_init(timer, which_clock, mode); 353237fc6e7SThomas Gleixner } 354237fc6e7SThomas Gleixner static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { } 355237fc6e7SThomas Gleixner #endif 356237fc6e7SThomas Gleixner 357c0a31329SThomas Gleixner /* Basic timer operations: */ 358c0a31329SThomas Gleixner extern int hrtimer_start(struct hrtimer *timer, ktime_t tim, 359c0a31329SThomas Gleixner const enum hrtimer_mode mode); 360da8f2e17SArjan van de Ven extern int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, 361da8f2e17SArjan van de Ven unsigned long range_ns, const enum hrtimer_mode mode); 3627f1e2ca9SPeter Zijlstra extern int 3637f1e2ca9SPeter Zijlstra __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, 3647f1e2ca9SPeter Zijlstra unsigned long delta_ns, 3657f1e2ca9SPeter Zijlstra const enum hrtimer_mode mode, int wakeup); 3667f1e2ca9SPeter Zijlstra 367c0a31329SThomas Gleixner extern int hrtimer_cancel(struct hrtimer *timer); 368c0a31329SThomas Gleixner extern int hrtimer_try_to_cancel(struct hrtimer *timer); 369c0a31329SThomas Gleixner 37063ca243bSArjan van de Ven static inline int hrtimer_start_expires(struct hrtimer *timer, 37163ca243bSArjan van de Ven enum hrtimer_mode mode) 37263ca243bSArjan van de Ven { 373da8f2e17SArjan van de Ven unsigned long delta; 374da8f2e17SArjan van de Ven ktime_t soft, hard; 375da8f2e17SArjan van de Ven soft = hrtimer_get_softexpires(timer); 376da8f2e17SArjan van de Ven hard = hrtimer_get_expires(timer); 377da8f2e17SArjan van de Ven delta = ktime_to_ns(ktime_sub(hard, soft)); 3784ce105d3SArjan van de Ven return hrtimer_start_range_ns(timer, soft, delta, mode); 37963ca243bSArjan van de Ven } 38063ca243bSArjan van de Ven 381c9cb2e3dSThomas Gleixner static inline int hrtimer_restart(struct hrtimer *timer) 382c9cb2e3dSThomas Gleixner { 383654c8e0bSArjan van de Ven return hrtimer_start_expires(timer, HRTIMER_MODE_ABS); 384c9cb2e3dSThomas Gleixner } 385c0a31329SThomas Gleixner 386c0a31329SThomas Gleixner /* Query timers: */ 387c0a31329SThomas Gleixner extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer); 388c0a31329SThomas Gleixner 38969239749STony Lindgren extern ktime_t hrtimer_get_next_event(void); 39069239749STony Lindgren 391303e967fSThomas Gleixner /* 39253370d2eSThomas Gleixner * A timer is active, when it is enqueued into the rbtree or the 39353370d2eSThomas Gleixner * callback function is running or it's in the state of being migrated 39453370d2eSThomas Gleixner * to another cpu. 395303e967fSThomas Gleixner */ 396c0a31329SThomas Gleixner static inline int hrtimer_active(const struct hrtimer *timer) 397c0a31329SThomas Gleixner { 398303e967fSThomas Gleixner return timer->state != HRTIMER_STATE_INACTIVE; 399c0a31329SThomas Gleixner } 400c0a31329SThomas Gleixner 40154cdfdb4SThomas Gleixner /* 40254cdfdb4SThomas Gleixner * Helper function to check, whether the timer is on one of the queues 40354cdfdb4SThomas Gleixner */ 40454cdfdb4SThomas Gleixner static inline int hrtimer_is_queued(struct hrtimer *timer) 40554cdfdb4SThomas Gleixner { 406ca109491SPeter Zijlstra return timer->state & HRTIMER_STATE_ENQUEUED; 40754cdfdb4SThomas Gleixner } 40854cdfdb4SThomas Gleixner 4094346f654SOliver Hartkopp /* 4104346f654SOliver Hartkopp * Helper function to check, whether the timer is running the callback 4114346f654SOliver Hartkopp * function 4124346f654SOliver Hartkopp */ 4134346f654SOliver Hartkopp static inline int hrtimer_callback_running(struct hrtimer *timer) 4144346f654SOliver Hartkopp { 4154346f654SOliver Hartkopp return timer->state & HRTIMER_STATE_CALLBACK; 4164346f654SOliver Hartkopp } 4174346f654SOliver Hartkopp 418c0a31329SThomas Gleixner /* Forward a hrtimer so it expires after now: */ 4194d672e7aSDavide Libenzi extern u64 42044f21475SRoman Zippel hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval); 421c0a31329SThomas Gleixner 42291e5a217SThomas Gleixner /** 42391e5a217SThomas Gleixner * hrtimer_forward_now - forward the timer expiry so it expires after now 42491e5a217SThomas Gleixner * @timer: hrtimer to forward 42591e5a217SThomas Gleixner * @interval: the interval to forward 42691e5a217SThomas Gleixner * 42791e5a217SThomas Gleixner * Forward the timer expiry so it will expire after the current time 42891e5a217SThomas Gleixner * of the hrtimer clock base. Returns the number of overruns. 42991e5a217SThomas Gleixner * 43091e5a217SThomas Gleixner * Can be safely called from the callback function of @timer. If 43191e5a217SThomas Gleixner * called from other contexts @timer must neither be enqueued nor 43291e5a217SThomas Gleixner * running the callback and the caller needs to take care of 43391e5a217SThomas Gleixner * serialization. 43491e5a217SThomas Gleixner * 43591e5a217SThomas Gleixner * Note: This only updates the timer expiry value and does not requeue 43691e5a217SThomas Gleixner * the timer. 43791e5a217SThomas Gleixner */ 4384d672e7aSDavide Libenzi static inline u64 hrtimer_forward_now(struct hrtimer *timer, 4395e05ad7dSDavide Libenzi ktime_t interval) 4405e05ad7dSDavide Libenzi { 4415e05ad7dSDavide Libenzi return hrtimer_forward(timer, timer->base->get_time(), interval); 4425e05ad7dSDavide Libenzi } 4435e05ad7dSDavide Libenzi 44410c94ec1SThomas Gleixner /* Precise sleep: */ 44510c94ec1SThomas Gleixner extern long hrtimer_nanosleep(struct timespec *rqtp, 446080344b9SOleg Nesterov struct timespec __user *rmtp, 44710c94ec1SThomas Gleixner const enum hrtimer_mode mode, 44810c94ec1SThomas Gleixner const clockid_t clockid); 4491711ef38SToyo Abe extern long hrtimer_nanosleep_restart(struct restart_block *restart_block); 45010c94ec1SThomas Gleixner 45100362e33SThomas Gleixner extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, 45200362e33SThomas Gleixner struct task_struct *tsk); 45300362e33SThomas Gleixner 454654c8e0bSArjan van de Ven extern int schedule_hrtimeout_range(ktime_t *expires, unsigned long delta, 455654c8e0bSArjan van de Ven const enum hrtimer_mode mode); 456351b3f7aSCarsten Emde extern int schedule_hrtimeout_range_clock(ktime_t *expires, 457351b3f7aSCarsten Emde unsigned long delta, const enum hrtimer_mode mode, int clock); 4587bb67439SArjan van de Ven extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode); 4597bb67439SArjan van de Ven 460c0a31329SThomas Gleixner /* Soft interrupt function to run the hrtimer queues: */ 461c0a31329SThomas Gleixner extern void hrtimer_run_queues(void); 462d3d74453SPeter Zijlstra extern void hrtimer_run_pending(void); 463c0a31329SThomas Gleixner 464c0a31329SThomas Gleixner /* Bootup initialization: */ 465c0a31329SThomas Gleixner extern void __init hrtimers_init(void); 466c0a31329SThomas Gleixner 46788ad0bf6SIngo Molnar /* Show pending timers: */ 46888ad0bf6SIngo Molnar extern void sysrq_timer_list_show(void); 46988ad0bf6SIngo Molnar 470c0a31329SThomas Gleixner #endif 471