135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0 2c0a31329SThomas Gleixner /* 3c0a31329SThomas Gleixner * hrtimers - High-resolution kernel timers 4c0a31329SThomas Gleixner * 5c0a31329SThomas Gleixner * Copyright(C) 2005, Thomas Gleixner <[email protected]> 6c0a31329SThomas Gleixner * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar 7c0a31329SThomas Gleixner * 8c0a31329SThomas Gleixner * data type definitions, declarations, prototypes 9c0a31329SThomas Gleixner * 10c0a31329SThomas Gleixner * Started by: Thomas Gleixner and Ingo Molnar 11c0a31329SThomas Gleixner */ 12c0a31329SThomas Gleixner #ifndef _LINUX_HRTIMER_H 13c0a31329SThomas Gleixner #define _LINUX_HRTIMER_H 14c0a31329SThomas Gleixner 1532e29396SVincenzo Frascino #include <linux/hrtimer_defs.h> 16*50d91c76SKent Overstreet #include <linux/hrtimer_types.h> 17c0a31329SThomas Gleixner #include <linux/init.h> 18c0a31329SThomas Gleixner #include <linux/list.h> 196060ef31SKent Overstreet #include <linux/percpu-defs.h> 20*50d91c76SKent Overstreet #include <linux/rbtree.h> 210cd39f46SPeter Zijlstra #include <linux/seqlock.h> 22507e1231SHeiko Carstens #include <linux/timer.h> 23c0a31329SThomas Gleixner 243c8aa39dSThomas Gleixner struct hrtimer_clock_base; 253c8aa39dSThomas Gleixner struct hrtimer_cpu_base; 263c8aa39dSThomas Gleixner 27c0a31329SThomas Gleixner /* 28c0a31329SThomas Gleixner * Mode arguments of xxx_hrtimer functions: 2919b51cb5SAnna-Maria Gleixner * 3019b51cb5SAnna-Maria Gleixner * HRTIMER_MODE_ABS - Time value is absolute 3119b51cb5SAnna-Maria Gleixner * HRTIMER_MODE_REL - Time value is relative to now 3219b51cb5SAnna-Maria Gleixner * HRTIMER_MODE_PINNED - Timer is bound to CPU (is only considered 3319b51cb5SAnna-Maria Gleixner * when starting the timer) 3498ecadd4SAnna-Maria Gleixner * HRTIMER_MODE_SOFT - Timer callback function will be executed in 3598ecadd4SAnna-Maria Gleixner * soft irq context 36a67e4082SSebastian Andrzej Siewior * HRTIMER_MODE_HARD - Timer callback function will be executed in 37a67e4082SSebastian Andrzej Siewior * hard irq context even on PREEMPT_RT. 38c0a31329SThomas Gleixner */ 39c0a31329SThomas Gleixner enum hrtimer_mode { 4019b51cb5SAnna-Maria Gleixner HRTIMER_MODE_ABS = 0x00, 4119b51cb5SAnna-Maria Gleixner HRTIMER_MODE_REL = 0x01, 4219b51cb5SAnna-Maria Gleixner HRTIMER_MODE_PINNED = 0x02, 4398ecadd4SAnna-Maria Gleixner HRTIMER_MODE_SOFT = 0x04, 44ae6683d8SSebastian Andrzej Siewior HRTIMER_MODE_HARD = 0x08, 4519b51cb5SAnna-Maria Gleixner 4619b51cb5SAnna-Maria Gleixner HRTIMER_MODE_ABS_PINNED = HRTIMER_MODE_ABS | HRTIMER_MODE_PINNED, 4719b51cb5SAnna-Maria Gleixner HRTIMER_MODE_REL_PINNED = HRTIMER_MODE_REL | HRTIMER_MODE_PINNED, 4898ecadd4SAnna-Maria Gleixner 4998ecadd4SAnna-Maria Gleixner HRTIMER_MODE_ABS_SOFT = HRTIMER_MODE_ABS | HRTIMER_MODE_SOFT, 5098ecadd4SAnna-Maria Gleixner HRTIMER_MODE_REL_SOFT = HRTIMER_MODE_REL | HRTIMER_MODE_SOFT, 5198ecadd4SAnna-Maria Gleixner 5298ecadd4SAnna-Maria Gleixner HRTIMER_MODE_ABS_PINNED_SOFT = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_SOFT, 5398ecadd4SAnna-Maria Gleixner HRTIMER_MODE_REL_PINNED_SOFT = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_SOFT, 5498ecadd4SAnna-Maria Gleixner 55ae6683d8SSebastian Andrzej Siewior HRTIMER_MODE_ABS_HARD = HRTIMER_MODE_ABS | HRTIMER_MODE_HARD, 56ae6683d8SSebastian Andrzej Siewior HRTIMER_MODE_REL_HARD = HRTIMER_MODE_REL | HRTIMER_MODE_HARD, 57ae6683d8SSebastian Andrzej Siewior 58ae6683d8SSebastian Andrzej Siewior HRTIMER_MODE_ABS_PINNED_HARD = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_HARD, 59ae6683d8SSebastian Andrzej Siewior HRTIMER_MODE_REL_PINNED_HARD = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_HARD, 60c0a31329SThomas Gleixner }; 61c0a31329SThomas Gleixner 62c9cb2e3dSThomas Gleixner /* 6354cdfdb4SThomas Gleixner * Values to track state of the timer 64303e967fSThomas Gleixner * 65303e967fSThomas Gleixner * Possible states: 66303e967fSThomas Gleixner * 67303e967fSThomas Gleixner * 0x00 inactive 68303e967fSThomas Gleixner * 0x01 enqueued into rbtree 6954cdfdb4SThomas Gleixner * 70887d9dc9SPeter Zijlstra * The callback state is not part of the timer->state because clearing it would 71887d9dc9SPeter Zijlstra * mean touching the timer after the callback, this makes it impossible to free 72887d9dc9SPeter Zijlstra * the timer from the callback function. 7353370d2eSThomas Gleixner * 74887d9dc9SPeter Zijlstra * Therefore we track the callback state in: 75887d9dc9SPeter Zijlstra * 76887d9dc9SPeter Zijlstra * timer->base->cpu_base->running == timer 77887d9dc9SPeter Zijlstra * 78887d9dc9SPeter Zijlstra * On SMP it is possible to have a "callback function running and enqueued" 79887d9dc9SPeter Zijlstra * status. It happens for example when a posix timer expired and the callback 80303e967fSThomas Gleixner * queued a signal. Between dropping the lock which protects the posix timer 81303e967fSThomas Gleixner * and reacquiring the base lock of the hrtimer, another CPU can deliver the 82887d9dc9SPeter Zijlstra * signal and rearm the timer. 83303e967fSThomas Gleixner * 84303e967fSThomas Gleixner * All state transitions are protected by cpu_base->lock. 85303e967fSThomas Gleixner */ 86303e967fSThomas Gleixner #define HRTIMER_STATE_INACTIVE 0x00 87303e967fSThomas Gleixner #define HRTIMER_STATE_ENQUEUED 0x01 88303e967fSThomas Gleixner 89c0a31329SThomas Gleixner /** 9000362e33SThomas Gleixner * struct hrtimer_sleeper - simple sleeper structure 9100362e33SThomas Gleixner * @timer: embedded timer structure 9200362e33SThomas Gleixner * @task: task to wake up 9300362e33SThomas Gleixner * 9400362e33SThomas Gleixner * task is set to NULL, when the timer expires. 9500362e33SThomas Gleixner */ 9600362e33SThomas Gleixner struct hrtimer_sleeper { 9700362e33SThomas Gleixner struct hrtimer timer; 9800362e33SThomas Gleixner struct task_struct *task; 9900362e33SThomas Gleixner }; 10000362e33SThomas Gleixner 101b8e38413SThomas Gleixner #ifdef CONFIG_64BIT 1023f0b9e8eSAnna-Maria Gleixner # define __hrtimer_clock_base_align ____cacheline_aligned 103b8e38413SThomas Gleixner #else 1043f0b9e8eSAnna-Maria Gleixner # define __hrtimer_clock_base_align 105b8e38413SThomas Gleixner #endif 106b8e38413SThomas Gleixner 10700362e33SThomas Gleixner /** 108d1d67174SAndres Salomon * struct hrtimer_clock_base - the timer base for a specific clock 10905fb6bf0SRandy Dunlap * @cpu_base: per cpu clock base 1103c8aa39dSThomas Gleixner * @index: clock type index for per_cpu support when moving a 1113c8aa39dSThomas Gleixner * timer to a base on another cpu. 1124d258b25SVitaliy Ivanov * @clockid: clock id for per_cpu support 1133f0b9e8eSAnna-Maria Gleixner * @seq: seqcount around __run_hrtimer 1143f0b9e8eSAnna-Maria Gleixner * @running: pointer to the currently running hrtimer 115c0a31329SThomas Gleixner * @active: red black tree root node for the active timers 116c0a31329SThomas Gleixner * @get_time: function to retrieve the current time of the clock 11754cdfdb4SThomas Gleixner * @offset: offset of this clock to the monotonic base 118c0a31329SThomas Gleixner */ 1193c8aa39dSThomas Gleixner struct hrtimer_clock_base { 1203c8aa39dSThomas Gleixner struct hrtimer_cpu_base *cpu_base; 1213f0b9e8eSAnna-Maria Gleixner unsigned int index; 122ab8177bcSThomas Gleixner clockid_t clockid; 123af5a06b5SAhmed S. Darwish seqcount_raw_spinlock_t seq; 1243f0b9e8eSAnna-Maria Gleixner struct hrtimer *running; 125998adc3dSJohn Stultz struct timerqueue_head active; 126c0a31329SThomas Gleixner ktime_t (*get_time)(void); 12754cdfdb4SThomas Gleixner ktime_t offset; 1283f0b9e8eSAnna-Maria Gleixner } __hrtimer_clock_base_align; 1293c8aa39dSThomas Gleixner 130e06383dbSJohn Stultz enum hrtimer_base_type { 131e06383dbSJohn Stultz HRTIMER_BASE_MONOTONIC, 13268fa61c0SThomas Gleixner HRTIMER_BASE_REALTIME, 133a3ed0e43SThomas Gleixner HRTIMER_BASE_BOOTTIME, 13490adda98SJohn Stultz HRTIMER_BASE_TAI, 13598ecadd4SAnna-Maria Gleixner HRTIMER_BASE_MONOTONIC_SOFT, 13698ecadd4SAnna-Maria Gleixner HRTIMER_BASE_REALTIME_SOFT, 137a3ed0e43SThomas Gleixner HRTIMER_BASE_BOOTTIME_SOFT, 13898ecadd4SAnna-Maria Gleixner HRTIMER_BASE_TAI_SOFT, 139e06383dbSJohn Stultz HRTIMER_MAX_CLOCK_BASES, 140e06383dbSJohn Stultz }; 1413c8aa39dSThomas Gleixner 1421fbc78b3SAnna-Maria Gleixner /** 1433c8aa39dSThomas Gleixner * struct hrtimer_cpu_base - the per cpu clock bases 1443c8aa39dSThomas Gleixner * @lock: lock protecting the base and associated clock bases 1453c8aa39dSThomas Gleixner * and timers 146cddd0248SViresh Kumar * @cpu: cpu number 147ab8177bcSThomas Gleixner * @active_bases: Bitfield to mark bases with active timers 148868a3e91SThomas Gleixner * @clock_was_set_seq: Sequence counter of clock was set events 14954cdfdb4SThomas Gleixner * @hres_active: State of high resolution mode 15028bfd18bSAnna-Maria Gleixner * @in_hrtirq: hrtimer_interrupt() is currently executing 15141d2e494SThomas Gleixner * @hang_detected: The last hrtimer interrupt detected a hang 1525da70160SAnna-Maria Gleixner * @softirq_activated: displays, if the softirq is raised - update of softirq 1535da70160SAnna-Maria Gleixner * related settings is not required then. 15441d2e494SThomas Gleixner * @nr_events: Total number of hrtimer interrupt events 15541d2e494SThomas Gleixner * @nr_retries: Total number of hrtimer interrupt retries 15641d2e494SThomas Gleixner * @nr_hangs: Total number of hrtimer interrupt hangs 15741d2e494SThomas Gleixner * @max_hang_time: Maximum time spent in hrtimer_interrupt 158f61eff83SAnna-Maria Gleixner * @softirq_expiry_lock: Lock which is taken while softirq based hrtimer are 159f61eff83SAnna-Maria Gleixner * expired 160f61eff83SAnna-Maria Gleixner * @timer_waiters: A hrtimer_cancel() invocation waits for the timer 161f61eff83SAnna-Maria Gleixner * callback to finish. 16207a9a7eaSAnna-Maria Gleixner * @expires_next: absolute time of the next event, is required for remote 1635da70160SAnna-Maria Gleixner * hrtimer enqueue; it is the total first expiry time (hard 1645da70160SAnna-Maria Gleixner * and soft hrtimer are taken into account) 165eb27926bSAnna-Maria Gleixner * @next_timer: Pointer to the first expiring timer 1665da70160SAnna-Maria Gleixner * @softirq_expires_next: Time to check, if soft queues needs also to be expired 1675da70160SAnna-Maria Gleixner * @softirq_next_timer: Pointer to the first expiring softirq based timer 168ab8177bcSThomas Gleixner * @clock_base: array of clock bases for this cpu 169895bdfa7SThomas Gleixner * 170895bdfa7SThomas Gleixner * Note: next_timer is just an optimization for __remove_hrtimer(). 171895bdfa7SThomas Gleixner * Do not dereference the pointer because it is not reliable on 172895bdfa7SThomas Gleixner * cross cpu removals. 1733c8aa39dSThomas Gleixner */ 1743c8aa39dSThomas Gleixner struct hrtimer_cpu_base { 175ecb49d1aSThomas Gleixner raw_spinlock_t lock; 176cddd0248SViresh Kumar unsigned int cpu; 177f55a6faaSJohn Stultz unsigned int active_bases; 178868a3e91SThomas Gleixner unsigned int clock_was_set_seq; 17911a9fe06SAnna-Maria Gleixner unsigned int hres_active : 1, 18011a9fe06SAnna-Maria Gleixner in_hrtirq : 1, 1815da70160SAnna-Maria Gleixner hang_detected : 1, 1825da70160SAnna-Maria Gleixner softirq_activated : 1; 18311a9fe06SAnna-Maria Gleixner #ifdef CONFIG_HIGH_RES_TIMERS 184a6ffebceSThomas Gleixner unsigned int nr_events; 185da21c5a5SAnna-Maria Gleixner unsigned short nr_retries; 186da21c5a5SAnna-Maria Gleixner unsigned short nr_hangs; 187a6ffebceSThomas Gleixner unsigned int max_hang_time; 18854cdfdb4SThomas Gleixner #endif 189f61eff83SAnna-Maria Gleixner #ifdef CONFIG_PREEMPT_RT 190f61eff83SAnna-Maria Gleixner spinlock_t softirq_expiry_lock; 191f61eff83SAnna-Maria Gleixner atomic_t timer_waiters; 192f61eff83SAnna-Maria Gleixner #endif 19307a9a7eaSAnna-Maria Gleixner ktime_t expires_next; 194eb27926bSAnna-Maria Gleixner struct hrtimer *next_timer; 1955da70160SAnna-Maria Gleixner ktime_t softirq_expires_next; 1965da70160SAnna-Maria Gleixner struct hrtimer *softirq_next_timer; 197f24444b0SThomas Gleixner struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES]; 1986d9a1411SThomas Gleixner } ____cacheline_aligned; 199c0a31329SThomas Gleixner 20063ca243bSArjan van de Ven static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time) 20163ca243bSArjan van de Ven { 202998adc3dSJohn Stultz timer->node.expires = time; 203654c8e0bSArjan van de Ven timer->_softexpires = time; 20463ca243bSArjan van de Ven } 205654c8e0bSArjan van de Ven 206654c8e0bSArjan van de Ven static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta) 207654c8e0bSArjan van de Ven { 208654c8e0bSArjan van de Ven timer->_softexpires = time; 209998adc3dSJohn Stultz timer->node.expires = ktime_add_safe(time, delta); 210654c8e0bSArjan van de Ven } 211654c8e0bSArjan van de Ven 212da8b44d5SJohn Stultz static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, u64 delta) 213654c8e0bSArjan van de Ven { 214654c8e0bSArjan van de Ven timer->_softexpires = time; 215998adc3dSJohn Stultz timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta)); 216654c8e0bSArjan van de Ven } 217654c8e0bSArjan van de Ven 21863ca243bSArjan van de Ven static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64) 21963ca243bSArjan van de Ven { 2202456e855SThomas Gleixner timer->node.expires = tv64; 2212456e855SThomas Gleixner timer->_softexpires = tv64; 22263ca243bSArjan van de Ven } 22363ca243bSArjan van de Ven 22463ca243bSArjan van de Ven static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time) 22563ca243bSArjan van de Ven { 226998adc3dSJohn Stultz timer->node.expires = ktime_add_safe(timer->node.expires, time); 227654c8e0bSArjan van de Ven timer->_softexpires = ktime_add_safe(timer->_softexpires, time); 22863ca243bSArjan van de Ven } 22963ca243bSArjan van de Ven 2307597bc94SDavid Howells static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns) 23163ca243bSArjan van de Ven { 232998adc3dSJohn Stultz timer->node.expires = ktime_add_ns(timer->node.expires, ns); 233654c8e0bSArjan van de Ven timer->_softexpires = ktime_add_ns(timer->_softexpires, ns); 23463ca243bSArjan van de Ven } 23563ca243bSArjan van de Ven 23663ca243bSArjan van de Ven static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer) 23763ca243bSArjan van de Ven { 238998adc3dSJohn Stultz return timer->node.expires; 23963ca243bSArjan van de Ven } 24063ca243bSArjan van de Ven 241654c8e0bSArjan van de Ven static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer) 242654c8e0bSArjan van de Ven { 243654c8e0bSArjan van de Ven return timer->_softexpires; 244654c8e0bSArjan van de Ven } 245654c8e0bSArjan van de Ven 24663ca243bSArjan van de Ven static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer) 24763ca243bSArjan van de Ven { 2482456e855SThomas Gleixner return timer->node.expires; 24963ca243bSArjan van de Ven } 250654c8e0bSArjan van de Ven static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer) 251654c8e0bSArjan van de Ven { 2522456e855SThomas Gleixner return timer->_softexpires; 253654c8e0bSArjan van de Ven } 25463ca243bSArjan van de Ven 25563ca243bSArjan van de Ven static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer) 25663ca243bSArjan van de Ven { 257998adc3dSJohn Stultz return ktime_to_ns(timer->node.expires); 25863ca243bSArjan van de Ven } 25963ca243bSArjan van de Ven 26063ca243bSArjan van de Ven static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer) 26163ca243bSArjan van de Ven { 262998adc3dSJohn Stultz return ktime_sub(timer->node.expires, timer->base->get_time()); 26363ca243bSArjan van de Ven } 26463ca243bSArjan van de Ven 26554cdfdb4SThomas Gleixner static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer) 26654cdfdb4SThomas Gleixner { 26754cdfdb4SThomas Gleixner return timer->base->get_time(); 26854cdfdb4SThomas Gleixner } 26954cdfdb4SThomas Gleixner 27028bfd18bSAnna-Maria Gleixner static inline int hrtimer_is_hres_active(struct hrtimer *timer) 27128bfd18bSAnna-Maria Gleixner { 27228bfd18bSAnna-Maria Gleixner return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ? 27328bfd18bSAnna-Maria Gleixner timer->base->cpu_base->hres_active : 0; 27428bfd18bSAnna-Maria Gleixner } 27528bfd18bSAnna-Maria Gleixner 27621d6d52aSThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS 27721d6d52aSThomas Gleixner struct clock_event_device; 27821d6d52aSThomas Gleixner 27921d6d52aSThomas Gleixner extern void hrtimer_interrupt(struct clock_event_device *dev); 28021d6d52aSThomas Gleixner 281398ca17fSThomas Gleixner extern unsigned int hrtimer_resolution; 282398ca17fSThomas Gleixner 28354cdfdb4SThomas Gleixner #else 28454cdfdb4SThomas Gleixner 285d711b8b3SBorislav Petkov #define hrtimer_resolution (unsigned int)LOW_RES_NSEC 286398ca17fSThomas Gleixner 28754cdfdb4SThomas Gleixner #endif 28854cdfdb4SThomas Gleixner 289203cbf77SThomas Gleixner static inline ktime_t 290203cbf77SThomas Gleixner __hrtimer_expires_remaining_adjusted(const struct hrtimer *timer, ktime_t now) 291203cbf77SThomas Gleixner { 292203cbf77SThomas Gleixner ktime_t rem = ktime_sub(timer->node.expires, now); 293203cbf77SThomas Gleixner 294203cbf77SThomas Gleixner /* 295203cbf77SThomas Gleixner * Adjust relative timers for the extra we added in 296203cbf77SThomas Gleixner * hrtimer_start_range_ns() to prevent short timeouts. 297203cbf77SThomas Gleixner */ 298203cbf77SThomas Gleixner if (IS_ENABLED(CONFIG_TIME_LOW_RES) && timer->is_rel) 2992456e855SThomas Gleixner rem -= hrtimer_resolution; 300203cbf77SThomas Gleixner return rem; 301203cbf77SThomas Gleixner } 302203cbf77SThomas Gleixner 303203cbf77SThomas Gleixner static inline ktime_t 304203cbf77SThomas Gleixner hrtimer_expires_remaining_adjusted(const struct hrtimer *timer) 305203cbf77SThomas Gleixner { 306203cbf77SThomas Gleixner return __hrtimer_expires_remaining_adjusted(timer, 307203cbf77SThomas Gleixner timer->base->get_time()); 308203cbf77SThomas Gleixner } 309203cbf77SThomas Gleixner 3109ec26907SThomas Gleixner #ifdef CONFIG_TIMERFD 3119ec26907SThomas Gleixner extern void timerfd_clock_was_set(void); 31266f7b0c8SThomas Gleixner extern void timerfd_resume(void); 3139ec26907SThomas Gleixner #else 3149ec26907SThomas Gleixner static inline void timerfd_clock_was_set(void) { } 31566f7b0c8SThomas Gleixner static inline void timerfd_resume(void) { } 3169ec26907SThomas Gleixner #endif 317b12a03ceSThomas Gleixner 3182e94d1f7SArjan van de Ven DECLARE_PER_CPU(struct tick_device, tick_cpu_device); 3192e94d1f7SArjan van de Ven 320f61eff83SAnna-Maria Gleixner #ifdef CONFIG_PREEMPT_RT 321f61eff83SAnna-Maria Gleixner void hrtimer_cancel_wait_running(const struct hrtimer *timer); 322f61eff83SAnna-Maria Gleixner #else 323f61eff83SAnna-Maria Gleixner static inline void hrtimer_cancel_wait_running(struct hrtimer *timer) 324f61eff83SAnna-Maria Gleixner { 325f61eff83SAnna-Maria Gleixner cpu_relax(); 326f61eff83SAnna-Maria Gleixner } 327f61eff83SAnna-Maria Gleixner #endif 3282e94d1f7SArjan van de Ven 329c0a31329SThomas Gleixner /* Exported timer functions: */ 330c0a31329SThomas Gleixner 331c0a31329SThomas Gleixner /* Initialize timers: */ 3327978672cSGeorge Anzinger extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock, 3337978672cSGeorge Anzinger enum hrtimer_mode mode); 334dbc1625fSSebastian Andrzej Siewior extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id, 335dbc1625fSSebastian Andrzej Siewior enum hrtimer_mode mode); 336c0a31329SThomas Gleixner 337237fc6e7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS 338237fc6e7SThomas Gleixner extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock, 339237fc6e7SThomas Gleixner enum hrtimer_mode mode); 340dbc1625fSSebastian Andrzej Siewior extern void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl, 341dbc1625fSSebastian Andrzej Siewior clockid_t clock_id, 342dbc1625fSSebastian Andrzej Siewior enum hrtimer_mode mode); 343237fc6e7SThomas Gleixner 344237fc6e7SThomas Gleixner extern void destroy_hrtimer_on_stack(struct hrtimer *timer); 345237fc6e7SThomas Gleixner #else 346237fc6e7SThomas Gleixner static inline void hrtimer_init_on_stack(struct hrtimer *timer, 347237fc6e7SThomas Gleixner clockid_t which_clock, 348237fc6e7SThomas Gleixner enum hrtimer_mode mode) 349237fc6e7SThomas Gleixner { 350237fc6e7SThomas Gleixner hrtimer_init(timer, which_clock, mode); 351237fc6e7SThomas Gleixner } 352dbc1625fSSebastian Andrzej Siewior 353dbc1625fSSebastian Andrzej Siewior static inline void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl, 354dbc1625fSSebastian Andrzej Siewior clockid_t clock_id, 355dbc1625fSSebastian Andrzej Siewior enum hrtimer_mode mode) 356dbc1625fSSebastian Andrzej Siewior { 357dbc1625fSSebastian Andrzej Siewior hrtimer_init_sleeper(sl, clock_id, mode); 358dbc1625fSSebastian Andrzej Siewior } 359dbc1625fSSebastian Andrzej Siewior 360237fc6e7SThomas Gleixner static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { } 361237fc6e7SThomas Gleixner #endif 362237fc6e7SThomas Gleixner 363c0a31329SThomas Gleixner /* Basic timer operations: */ 36461699e13SThomas Gleixner extern void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, 365da8b44d5SJohn Stultz u64 range_ns, const enum hrtimer_mode mode); 3667f1e2ca9SPeter Zijlstra 36702a171afSThomas Gleixner /** 3686de6250cSAnna-Maria Gleixner * hrtimer_start - (re)start an hrtimer 36902a171afSThomas Gleixner * @timer: the timer to be added 37002a171afSThomas Gleixner * @tim: expiry time 3716de6250cSAnna-Maria Gleixner * @mode: timer mode: absolute (HRTIMER_MODE_ABS) or 3725da70160SAnna-Maria Gleixner * relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED); 3735da70160SAnna-Maria Gleixner * softirq based mode is considered for debug purpose only! 37402a171afSThomas Gleixner */ 37561699e13SThomas Gleixner static inline void hrtimer_start(struct hrtimer *timer, ktime_t tim, 37602a171afSThomas Gleixner const enum hrtimer_mode mode) 37702a171afSThomas Gleixner { 37861699e13SThomas Gleixner hrtimer_start_range_ns(timer, tim, 0, mode); 37902a171afSThomas Gleixner } 38002a171afSThomas Gleixner 381c0a31329SThomas Gleixner extern int hrtimer_cancel(struct hrtimer *timer); 382c0a31329SThomas Gleixner extern int hrtimer_try_to_cancel(struct hrtimer *timer); 383c0a31329SThomas Gleixner 38461699e13SThomas Gleixner static inline void hrtimer_start_expires(struct hrtimer *timer, 38563ca243bSArjan van de Ven enum hrtimer_mode mode) 38663ca243bSArjan van de Ven { 387da8b44d5SJohn Stultz u64 delta; 388da8f2e17SArjan van de Ven ktime_t soft, hard; 389da8f2e17SArjan van de Ven soft = hrtimer_get_softexpires(timer); 390da8f2e17SArjan van de Ven hard = hrtimer_get_expires(timer); 391da8f2e17SArjan van de Ven delta = ktime_to_ns(ktime_sub(hard, soft)); 39261699e13SThomas Gleixner hrtimer_start_range_ns(timer, soft, delta, mode); 39363ca243bSArjan van de Ven } 39463ca243bSArjan van de Ven 39501656464SThomas Gleixner void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl, 39601656464SThomas Gleixner enum hrtimer_mode mode); 39701656464SThomas Gleixner 39861699e13SThomas Gleixner static inline void hrtimer_restart(struct hrtimer *timer) 399c9cb2e3dSThomas Gleixner { 40061699e13SThomas Gleixner hrtimer_start_expires(timer, HRTIMER_MODE_ABS); 401c9cb2e3dSThomas Gleixner } 402c0a31329SThomas Gleixner 403c0a31329SThomas Gleixner /* Query timers: */ 404203cbf77SThomas Gleixner extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust); 405203cbf77SThomas Gleixner 40666981c37SMauro Carvalho Chehab /** 40766981c37SMauro Carvalho Chehab * hrtimer_get_remaining - get remaining time for the timer 40866981c37SMauro Carvalho Chehab * @timer: the timer to read 40966981c37SMauro Carvalho Chehab */ 410203cbf77SThomas Gleixner static inline ktime_t hrtimer_get_remaining(const struct hrtimer *timer) 411203cbf77SThomas Gleixner { 412203cbf77SThomas Gleixner return __hrtimer_get_remaining(timer, false); 413203cbf77SThomas Gleixner } 414c0a31329SThomas Gleixner 415c1ad348bSThomas Gleixner extern u64 hrtimer_get_next_event(void); 416a59855cdSRafael J. Wysocki extern u64 hrtimer_next_event_without(const struct hrtimer *exclude); 41769239749STony Lindgren 418887d9dc9SPeter Zijlstra extern bool hrtimer_active(const struct hrtimer *timer); 419c0a31329SThomas Gleixner 42056144737SEric Dumazet /** 42166981c37SMauro Carvalho Chehab * hrtimer_is_queued - check, whether the timer is on one of the queues 42256144737SEric Dumazet * @timer: Timer to check 42356144737SEric Dumazet * 42456144737SEric Dumazet * Returns: True if the timer is queued, false otherwise 42556144737SEric Dumazet * 42656144737SEric Dumazet * The function can be used lockless, but it gives only a current snapshot. 42754cdfdb4SThomas Gleixner */ 42856144737SEric Dumazet static inline bool hrtimer_is_queued(struct hrtimer *timer) 42954cdfdb4SThomas Gleixner { 43056144737SEric Dumazet /* The READ_ONCE pairs with the update functions of timer->state */ 43156144737SEric Dumazet return !!(READ_ONCE(timer->state) & HRTIMER_STATE_ENQUEUED); 43254cdfdb4SThomas Gleixner } 43354cdfdb4SThomas Gleixner 4344346f654SOliver Hartkopp /* 4354346f654SOliver Hartkopp * Helper function to check, whether the timer is running the callback 4364346f654SOliver Hartkopp * function 4374346f654SOliver Hartkopp */ 4384346f654SOliver Hartkopp static inline int hrtimer_callback_running(struct hrtimer *timer) 4394346f654SOliver Hartkopp { 4403f0b9e8eSAnna-Maria Gleixner return timer->base->running == timer; 4414346f654SOliver Hartkopp } 4424346f654SOliver Hartkopp 443c0a31329SThomas Gleixner /* Forward a hrtimer so it expires after now: */ 4444d672e7aSDavide Libenzi extern u64 44544f21475SRoman Zippel hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval); 446c0a31329SThomas Gleixner 44791e5a217SThomas Gleixner /** 44891e5a217SThomas Gleixner * hrtimer_forward_now - forward the timer expiry so it expires after now 44991e5a217SThomas Gleixner * @timer: hrtimer to forward 45091e5a217SThomas Gleixner * @interval: the interval to forward 45191e5a217SThomas Gleixner * 45291e5a217SThomas Gleixner * Forward the timer expiry so it will expire after the current time 45391e5a217SThomas Gleixner * of the hrtimer clock base. Returns the number of overruns. 45491e5a217SThomas Gleixner * 45591e5a217SThomas Gleixner * Can be safely called from the callback function of @timer. If 45691e5a217SThomas Gleixner * called from other contexts @timer must neither be enqueued nor 45791e5a217SThomas Gleixner * running the callback and the caller needs to take care of 45891e5a217SThomas Gleixner * serialization. 45991e5a217SThomas Gleixner * 46091e5a217SThomas Gleixner * Note: This only updates the timer expiry value and does not requeue 46191e5a217SThomas Gleixner * the timer. 46291e5a217SThomas Gleixner */ 4634d672e7aSDavide Libenzi static inline u64 hrtimer_forward_now(struct hrtimer *timer, 4645e05ad7dSDavide Libenzi ktime_t interval) 4655e05ad7dSDavide Libenzi { 4665e05ad7dSDavide Libenzi return hrtimer_forward(timer, timer->base->get_time(), interval); 4675e05ad7dSDavide Libenzi } 4685e05ad7dSDavide Libenzi 46910c94ec1SThomas Gleixner /* Precise sleep: */ 470ce41aaf4SAl Viro 471c0edd7c9SDeepa Dinamani extern int nanosleep_copyout(struct restart_block *, struct timespec64 *); 472ea2d1f7fSAndrei Vagin extern long hrtimer_nanosleep(ktime_t rqtp, const enum hrtimer_mode mode, 47310c94ec1SThomas Gleixner const clockid_t clockid); 47410c94ec1SThomas Gleixner 475da8b44d5SJohn Stultz extern int schedule_hrtimeout_range(ktime_t *expires, u64 delta, 476654c8e0bSArjan van de Ven const enum hrtimer_mode mode); 477351b3f7aSCarsten Emde extern int schedule_hrtimeout_range_clock(ktime_t *expires, 478da8b44d5SJohn Stultz u64 delta, 479da8b44d5SJohn Stultz const enum hrtimer_mode mode, 48090777713SAnna-Maria Gleixner clockid_t clock_id); 4817bb67439SArjan van de Ven extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode); 4827bb67439SArjan van de Ven 483c0a31329SThomas Gleixner /* Soft interrupt function to run the hrtimer queues: */ 484c0a31329SThomas Gleixner extern void hrtimer_run_queues(void); 485c0a31329SThomas Gleixner 486c0a31329SThomas Gleixner /* Bootup initialization: */ 487c0a31329SThomas Gleixner extern void __init hrtimers_init(void); 488c0a31329SThomas Gleixner 48988ad0bf6SIngo Molnar /* Show pending timers: */ 49088ad0bf6SIngo Molnar extern void sysrq_timer_list_show(void); 49188ad0bf6SIngo Molnar 49227590dc1SThomas Gleixner int hrtimers_prepare_cpu(unsigned int cpu); 49327590dc1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU 4945c0930ccSThomas Gleixner int hrtimers_cpu_dying(unsigned int cpu); 49527590dc1SThomas Gleixner #else 4965c0930ccSThomas Gleixner #define hrtimers_cpu_dying NULL 49727590dc1SThomas Gleixner #endif 49827590dc1SThomas Gleixner 499c0a31329SThomas Gleixner #endif 500