xref: /linux-6.15/include/linux/hrtimer.h (revision abb3a4ea)
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
5754cdfdb4SThomas Gleixner  *
58b00c1a99SThomas Gleixner  * Special cases:
59303e967fSThomas Gleixner  * 0x03		callback function running and enqueued
60303e967fSThomas Gleixner  *		(was requeued on another CPU)
61b00c1a99SThomas Gleixner  * 0x09		timer was migrated on CPU hotunplug
62303e967fSThomas Gleixner  * The "callback function running and enqueued" status is only possible on
63303e967fSThomas Gleixner  * SMP. It happens for example when a posix timer expired and the callback
64303e967fSThomas Gleixner  * queued a signal. Between dropping the lock which protects the posix timer
65303e967fSThomas Gleixner  * and reacquiring the base lock of the hrtimer, another CPU can deliver the
66303e967fSThomas Gleixner  * signal and rearm the timer. We have to preserve the callback running state,
67303e967fSThomas Gleixner  * as otherwise the timer could be removed before the softirq code finishes the
68303e967fSThomas Gleixner  * the handling of the timer.
69303e967fSThomas Gleixner  *
703eb05676SLi Zefan  * The HRTIMER_STATE_ENQUEUED bit is always or'ed to the current state to
71303e967fSThomas Gleixner  * preserve the HRTIMER_STATE_CALLBACK bit in the above scenario.
72303e967fSThomas Gleixner  *
73303e967fSThomas Gleixner  * All state transitions are protected by cpu_base->lock.
74303e967fSThomas Gleixner  */
75303e967fSThomas Gleixner #define HRTIMER_STATE_INACTIVE	0x00
76303e967fSThomas Gleixner #define HRTIMER_STATE_ENQUEUED	0x01
77303e967fSThomas Gleixner #define HRTIMER_STATE_CALLBACK	0x02
78ca109491SPeter Zijlstra #define HRTIMER_STATE_MIGRATE	0x04
79303e967fSThomas Gleixner 
80c0a31329SThomas Gleixner /**
81c0a31329SThomas Gleixner  * struct hrtimer - the basic hrtimer structure
82998adc3dSJohn Stultz  * @node:	timerqueue node, which also manages node.expires,
83998adc3dSJohn Stultz  *		the absolute expiry time in the hrtimers internal
84c0a31329SThomas Gleixner  *		representation. The time is related to the clock on
85592aa999SThomas Gleixner  *		which the timer is based. Is setup by adding
86592aa999SThomas Gleixner  *		slack to the _softexpires value. For non range timers
87592aa999SThomas Gleixner  *		identical to _softexpires.
88592aa999SThomas Gleixner  * @_softexpires: the absolute earliest expiry time of the hrtimer.
89592aa999SThomas Gleixner  *		The time which was given as expiry time when the timer
90592aa999SThomas Gleixner  *		was armed.
91c0a31329SThomas Gleixner  * @function:	timer expiry callback function
92c0a31329SThomas Gleixner  * @base:	pointer to the timer base (per cpu and per clock)
93303e967fSThomas Gleixner  * @state:	state information (See bit values above)
9454cdfdb4SThomas Gleixner  * @start_site:	timer statistics field to store the site where the timer
9554cdfdb4SThomas Gleixner  *		was started
9654cdfdb4SThomas Gleixner  * @start_comm: timer statistics field to store the name of the process which
9754cdfdb4SThomas Gleixner  *		started the timer
9854cdfdb4SThomas Gleixner  * @start_pid: timer statistics field to store the pid of the task which
9954cdfdb4SThomas Gleixner  *		started the timer
100c0a31329SThomas Gleixner  *
10154cdfdb4SThomas Gleixner  * The hrtimer structure must be initialized by hrtimer_init()
102c0a31329SThomas Gleixner  */
103c0a31329SThomas Gleixner struct hrtimer {
104998adc3dSJohn Stultz 	struct timerqueue_node		node;
105654c8e0bSArjan van de Ven 	ktime_t				_softexpires;
106c9cb2e3dSThomas Gleixner 	enum hrtimer_restart		(*function)(struct hrtimer *);
1073c8aa39dSThomas Gleixner 	struct hrtimer_clock_base	*base;
108303e967fSThomas Gleixner 	unsigned long			state;
10982f67cd9SIngo Molnar #ifdef CONFIG_TIMER_STATS
1101b024690SRichard Kennedy 	int				start_pid;
11182f67cd9SIngo Molnar 	void				*start_site;
11282f67cd9SIngo Molnar 	char				start_comm[16];
11382f67cd9SIngo Molnar #endif
114c0a31329SThomas Gleixner };
115c0a31329SThomas Gleixner 
116c0a31329SThomas Gleixner /**
11700362e33SThomas Gleixner  * struct hrtimer_sleeper - simple sleeper structure
11800362e33SThomas Gleixner  * @timer:	embedded timer structure
11900362e33SThomas Gleixner  * @task:	task to wake up
12000362e33SThomas Gleixner  *
12100362e33SThomas Gleixner  * task is set to NULL, when the timer expires.
12200362e33SThomas Gleixner  */
12300362e33SThomas Gleixner struct hrtimer_sleeper {
12400362e33SThomas Gleixner 	struct hrtimer timer;
12500362e33SThomas Gleixner 	struct task_struct *task;
12600362e33SThomas Gleixner };
12700362e33SThomas Gleixner 
12800362e33SThomas Gleixner /**
129d1d67174SAndres Salomon  * struct hrtimer_clock_base - the timer base for a specific clock
13005fb6bf0SRandy Dunlap  * @cpu_base:		per cpu clock base
1313c8aa39dSThomas Gleixner  * @index:		clock type index for per_cpu support when moving a
1323c8aa39dSThomas Gleixner  *			timer to a base on another cpu.
133c0a31329SThomas Gleixner  * @active:		red black tree root node for the active timers
134c0a31329SThomas Gleixner  * @resolution:		the resolution of the clock, in nanoseconds
135c0a31329SThomas Gleixner  * @get_time:		function to retrieve the current time of the clock
13692127c7aSThomas Gleixner  * @softirq_time:	the time when running the hrtimer queue in the softirq
13754cdfdb4SThomas Gleixner  * @offset:		offset of this clock to the monotonic base
138c0a31329SThomas Gleixner  */
1393c8aa39dSThomas Gleixner struct hrtimer_clock_base {
1403c8aa39dSThomas Gleixner 	struct hrtimer_cpu_base	*cpu_base;
141c0a31329SThomas Gleixner 	clockid_t		index;
142998adc3dSJohn Stultz 	struct timerqueue_head	active;
143e2787630SThomas Gleixner 	ktime_t			resolution;
144c0a31329SThomas Gleixner 	ktime_t			(*get_time)(void);
14592127c7aSThomas Gleixner 	ktime_t			softirq_time;
14654cdfdb4SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
14754cdfdb4SThomas Gleixner 	ktime_t			offset;
14854cdfdb4SThomas Gleixner #endif
1493c8aa39dSThomas Gleixner };
1503c8aa39dSThomas Gleixner 
151e06383dbSJohn Stultz enum  hrtimer_base_type {
152e06383dbSJohn Stultz 	HRTIMER_BASE_REALTIME,
153e06383dbSJohn Stultz 	HRTIMER_BASE_MONOTONIC,
154e06383dbSJohn Stultz 	HRTIMER_MAX_CLOCK_BASES,
155e06383dbSJohn Stultz };
1563c8aa39dSThomas Gleixner 
1573c8aa39dSThomas Gleixner /*
1583c8aa39dSThomas Gleixner  * struct hrtimer_cpu_base - the per cpu clock bases
1593c8aa39dSThomas Gleixner  * @lock:		lock protecting the base and associated clock bases
1603c8aa39dSThomas Gleixner  *			and timers
1613c8aa39dSThomas Gleixner  * @clock_base:		array of clock bases for this cpu
16254cdfdb4SThomas Gleixner  * @expires_next:	absolute time of the next event which was scheduled
16354cdfdb4SThomas Gleixner  *			via clock_set_next_event()
16454cdfdb4SThomas Gleixner  * @hres_active:	State of high resolution mode
16541d2e494SThomas Gleixner  * @hang_detected:	The last hrtimer interrupt detected a hang
16641d2e494SThomas Gleixner  * @nr_events:		Total number of hrtimer interrupt events
16741d2e494SThomas Gleixner  * @nr_retries:		Total number of hrtimer interrupt retries
16841d2e494SThomas Gleixner  * @nr_hangs:		Total number of hrtimer interrupt hangs
16941d2e494SThomas Gleixner  * @max_hang_time:	Maximum time spent in hrtimer_interrupt
1703c8aa39dSThomas Gleixner  */
1713c8aa39dSThomas Gleixner struct hrtimer_cpu_base {
172ecb49d1aSThomas Gleixner 	raw_spinlock_t			lock;
1733c8aa39dSThomas Gleixner 	struct hrtimer_clock_base	clock_base[HRTIMER_MAX_CLOCK_BASES];
17454cdfdb4SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
17554cdfdb4SThomas Gleixner 	ktime_t				expires_next;
17654cdfdb4SThomas Gleixner 	int				hres_active;
17741d2e494SThomas Gleixner 	int				hang_detected;
17854cdfdb4SThomas Gleixner 	unsigned long			nr_events;
17941d2e494SThomas Gleixner 	unsigned long			nr_retries;
18041d2e494SThomas Gleixner 	unsigned long			nr_hangs;
18141d2e494SThomas Gleixner 	ktime_t				max_hang_time;
18254cdfdb4SThomas Gleixner #endif
183c0a31329SThomas Gleixner };
184c0a31329SThomas Gleixner 
18563ca243bSArjan van de Ven static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
18663ca243bSArjan van de Ven {
187998adc3dSJohn Stultz 	timer->node.expires = time;
188654c8e0bSArjan van de Ven 	timer->_softexpires = time;
18963ca243bSArjan van de Ven }
190654c8e0bSArjan van de Ven 
191654c8e0bSArjan van de Ven static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
192654c8e0bSArjan van de Ven {
193654c8e0bSArjan van de Ven 	timer->_softexpires = time;
194998adc3dSJohn Stultz 	timer->node.expires = ktime_add_safe(time, delta);
195654c8e0bSArjan van de Ven }
196654c8e0bSArjan van de Ven 
197654c8e0bSArjan van de Ven static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, unsigned long delta)
198654c8e0bSArjan van de Ven {
199654c8e0bSArjan van de Ven 	timer->_softexpires = time;
200998adc3dSJohn Stultz 	timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta));
201654c8e0bSArjan van de Ven }
202654c8e0bSArjan van de Ven 
20363ca243bSArjan van de Ven static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
20463ca243bSArjan van de Ven {
205998adc3dSJohn Stultz 	timer->node.expires.tv64 = tv64;
206654c8e0bSArjan van de Ven 	timer->_softexpires.tv64 = tv64;
20763ca243bSArjan van de Ven }
20863ca243bSArjan van de Ven 
20963ca243bSArjan van de Ven static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
21063ca243bSArjan van de Ven {
211998adc3dSJohn Stultz 	timer->node.expires = ktime_add_safe(timer->node.expires, time);
212654c8e0bSArjan van de Ven 	timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
21363ca243bSArjan van de Ven }
21463ca243bSArjan van de Ven 
2157597bc94SDavid Howells static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns)
21663ca243bSArjan van de Ven {
217998adc3dSJohn Stultz 	timer->node.expires = ktime_add_ns(timer->node.expires, ns);
218654c8e0bSArjan van de Ven 	timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
21963ca243bSArjan van de Ven }
22063ca243bSArjan van de Ven 
22163ca243bSArjan van de Ven static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
22263ca243bSArjan van de Ven {
223998adc3dSJohn Stultz 	return timer->node.expires;
22463ca243bSArjan van de Ven }
22563ca243bSArjan van de Ven 
226654c8e0bSArjan van de Ven static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
227654c8e0bSArjan van de Ven {
228654c8e0bSArjan van de Ven 	return timer->_softexpires;
229654c8e0bSArjan van de Ven }
230654c8e0bSArjan van de Ven 
23163ca243bSArjan van de Ven static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
23263ca243bSArjan van de Ven {
233998adc3dSJohn Stultz 	return timer->node.expires.tv64;
23463ca243bSArjan van de Ven }
235654c8e0bSArjan van de Ven static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
236654c8e0bSArjan van de Ven {
237654c8e0bSArjan van de Ven 	return timer->_softexpires.tv64;
238654c8e0bSArjan van de Ven }
23963ca243bSArjan van de Ven 
24063ca243bSArjan van de Ven static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
24163ca243bSArjan van de Ven {
242998adc3dSJohn Stultz 	return ktime_to_ns(timer->node.expires);
24363ca243bSArjan van de Ven }
24463ca243bSArjan van de Ven 
24563ca243bSArjan van de Ven static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
24663ca243bSArjan van de Ven {
247998adc3dSJohn Stultz 	return ktime_sub(timer->node.expires, timer->base->get_time());
24863ca243bSArjan van de Ven }
24963ca243bSArjan van de Ven 
25054cdfdb4SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
25154cdfdb4SThomas Gleixner struct clock_event_device;
25254cdfdb4SThomas Gleixner 
25354cdfdb4SThomas Gleixner extern void clock_was_set(void);
254995f054fSIngo Molnar extern void hres_timers_resume(void);
25554cdfdb4SThomas Gleixner extern void hrtimer_interrupt(struct clock_event_device *dev);
25654cdfdb4SThomas Gleixner 
25754cdfdb4SThomas Gleixner /*
25854cdfdb4SThomas Gleixner  * In high resolution mode the time reference must be read accurate
25954cdfdb4SThomas Gleixner  */
26054cdfdb4SThomas Gleixner static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
26154cdfdb4SThomas Gleixner {
26254cdfdb4SThomas Gleixner 	return timer->base->get_time();
26354cdfdb4SThomas Gleixner }
26454cdfdb4SThomas Gleixner 
2658f4d37ecSPeter Zijlstra static inline int hrtimer_is_hres_active(struct hrtimer *timer)
2668f4d37ecSPeter Zijlstra {
2678f4d37ecSPeter Zijlstra 	return timer->base->cpu_base->hres_active;
2688f4d37ecSPeter Zijlstra }
2698f4d37ecSPeter Zijlstra 
2702075eb8dSArjan van de Ven extern void hrtimer_peek_ahead_timers(void);
2712075eb8dSArjan van de Ven 
27254cdfdb4SThomas Gleixner /*
27354cdfdb4SThomas Gleixner  * The resolution of the clocks. The resolution value is returned in
27454cdfdb4SThomas Gleixner  * the clock_getres() system call to give application programmers an
27554cdfdb4SThomas Gleixner  * idea of the (in)accuracy of timers. Timer values are rounded up to
27654cdfdb4SThomas Gleixner  * this resolution values.
27754cdfdb4SThomas Gleixner  */
278151db1fcSTony Breeds # define HIGH_RES_NSEC		1
279151db1fcSTony Breeds # define KTIME_HIGH_RES		(ktime_t) { .tv64 = HIGH_RES_NSEC }
280151db1fcSTony Breeds # define MONOTONIC_RES_NSEC	HIGH_RES_NSEC
28154cdfdb4SThomas Gleixner # define KTIME_MONOTONIC_RES	KTIME_HIGH_RES
28254cdfdb4SThomas Gleixner 
28354cdfdb4SThomas Gleixner #else
28454cdfdb4SThomas Gleixner 
285151db1fcSTony Breeds # define MONOTONIC_RES_NSEC	LOW_RES_NSEC
28654cdfdb4SThomas Gleixner # define KTIME_MONOTONIC_RES	KTIME_LOW_RES
28754cdfdb4SThomas Gleixner 
288becf8b5dSThomas Gleixner /*
289becf8b5dSThomas Gleixner  * clock_was_set() is a NOP for non- high-resolution systems. The
290becf8b5dSThomas Gleixner  * time-sorted order guarantees that a timer does not expire early and
291becf8b5dSThomas Gleixner  * is expired in the next softirq when the clock was advanced.
292becf8b5dSThomas Gleixner  */
29354cdfdb4SThomas Gleixner static inline void clock_was_set(void) { }
2942075eb8dSArjan van de Ven static inline void hrtimer_peek_ahead_timers(void) { }
29554cdfdb4SThomas Gleixner 
296995f054fSIngo Molnar static inline void hres_timers_resume(void) { }
297995f054fSIngo Molnar 
29854cdfdb4SThomas Gleixner /*
29954cdfdb4SThomas Gleixner  * In non high resolution mode the time reference is taken from
30054cdfdb4SThomas Gleixner  * the base softirq time variable.
30154cdfdb4SThomas Gleixner  */
30254cdfdb4SThomas Gleixner static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
30354cdfdb4SThomas Gleixner {
30454cdfdb4SThomas Gleixner 	return timer->base->softirq_time;
30554cdfdb4SThomas Gleixner }
30654cdfdb4SThomas Gleixner 
3078f4d37ecSPeter Zijlstra static inline int hrtimer_is_hres_active(struct hrtimer *timer)
3088f4d37ecSPeter Zijlstra {
3098f4d37ecSPeter Zijlstra 	return 0;
3108f4d37ecSPeter Zijlstra }
31154cdfdb4SThomas Gleixner #endif
31254cdfdb4SThomas Gleixner 
313d316c57fSThomas Gleixner extern ktime_t ktime_get(void);
314d316c57fSThomas Gleixner extern ktime_t ktime_get_real(void);
315*abb3a4eaSJohn Stultz extern ktime_t ktime_get_boottime(void);
316becf8b5dSThomas Gleixner 
3172e94d1f7SArjan van de Ven 
3182e94d1f7SArjan van de Ven DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
3192e94d1f7SArjan van de Ven 
3202e94d1f7SArjan van de Ven 
321c0a31329SThomas Gleixner /* Exported timer functions: */
322c0a31329SThomas Gleixner 
323c0a31329SThomas Gleixner /* Initialize timers: */
3247978672cSGeorge Anzinger extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
3257978672cSGeorge Anzinger 			 enum hrtimer_mode mode);
326c0a31329SThomas Gleixner 
327237fc6e7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
328237fc6e7SThomas Gleixner extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
329237fc6e7SThomas Gleixner 				  enum hrtimer_mode mode);
330237fc6e7SThomas Gleixner 
331237fc6e7SThomas Gleixner extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
332237fc6e7SThomas Gleixner #else
333237fc6e7SThomas Gleixner static inline void hrtimer_init_on_stack(struct hrtimer *timer,
334237fc6e7SThomas Gleixner 					 clockid_t which_clock,
335237fc6e7SThomas Gleixner 					 enum hrtimer_mode mode)
336237fc6e7SThomas Gleixner {
337237fc6e7SThomas Gleixner 	hrtimer_init(timer, which_clock, mode);
338237fc6e7SThomas Gleixner }
339237fc6e7SThomas Gleixner static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
340237fc6e7SThomas Gleixner #endif
341237fc6e7SThomas Gleixner 
342c0a31329SThomas Gleixner /* Basic timer operations: */
343c0a31329SThomas Gleixner extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
344c0a31329SThomas Gleixner 			 const enum hrtimer_mode mode);
345da8f2e17SArjan van de Ven extern int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
346da8f2e17SArjan van de Ven 			unsigned long range_ns, const enum hrtimer_mode mode);
3477f1e2ca9SPeter Zijlstra extern int
3487f1e2ca9SPeter Zijlstra __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
3497f1e2ca9SPeter Zijlstra 			 unsigned long delta_ns,
3507f1e2ca9SPeter Zijlstra 			 const enum hrtimer_mode mode, int wakeup);
3517f1e2ca9SPeter Zijlstra 
352c0a31329SThomas Gleixner extern int hrtimer_cancel(struct hrtimer *timer);
353c0a31329SThomas Gleixner extern int hrtimer_try_to_cancel(struct hrtimer *timer);
354c0a31329SThomas Gleixner 
35563ca243bSArjan van de Ven static inline int hrtimer_start_expires(struct hrtimer *timer,
35663ca243bSArjan van de Ven 						enum hrtimer_mode mode)
35763ca243bSArjan van de Ven {
358da8f2e17SArjan van de Ven 	unsigned long delta;
359da8f2e17SArjan van de Ven 	ktime_t soft, hard;
360da8f2e17SArjan van de Ven 	soft = hrtimer_get_softexpires(timer);
361da8f2e17SArjan van de Ven 	hard = hrtimer_get_expires(timer);
362da8f2e17SArjan van de Ven 	delta = ktime_to_ns(ktime_sub(hard, soft));
3634ce105d3SArjan van de Ven 	return hrtimer_start_range_ns(timer, soft, delta, mode);
36463ca243bSArjan van de Ven }
36563ca243bSArjan van de Ven 
366c9cb2e3dSThomas Gleixner static inline int hrtimer_restart(struct hrtimer *timer)
367c9cb2e3dSThomas Gleixner {
368654c8e0bSArjan van de Ven 	return hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
369c9cb2e3dSThomas Gleixner }
370c0a31329SThomas Gleixner 
371c0a31329SThomas Gleixner /* Query timers: */
372c0a31329SThomas Gleixner extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
373c0a31329SThomas Gleixner extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
374c0a31329SThomas Gleixner 
37569239749STony Lindgren extern ktime_t hrtimer_get_next_event(void);
37669239749STony Lindgren 
377303e967fSThomas Gleixner /*
378303e967fSThomas Gleixner  * A timer is active, when it is enqueued into the rbtree or the callback
379303e967fSThomas Gleixner  * function is running.
380303e967fSThomas Gleixner  */
381c0a31329SThomas Gleixner static inline int hrtimer_active(const struct hrtimer *timer)
382c0a31329SThomas Gleixner {
383303e967fSThomas Gleixner 	return timer->state != HRTIMER_STATE_INACTIVE;
384c0a31329SThomas Gleixner }
385c0a31329SThomas Gleixner 
38654cdfdb4SThomas Gleixner /*
38754cdfdb4SThomas Gleixner  * Helper function to check, whether the timer is on one of the queues
38854cdfdb4SThomas Gleixner  */
38954cdfdb4SThomas Gleixner static inline int hrtimer_is_queued(struct hrtimer *timer)
39054cdfdb4SThomas Gleixner {
391ca109491SPeter Zijlstra 	return timer->state & HRTIMER_STATE_ENQUEUED;
39254cdfdb4SThomas Gleixner }
39354cdfdb4SThomas Gleixner 
3944346f654SOliver Hartkopp /*
3954346f654SOliver Hartkopp  * Helper function to check, whether the timer is running the callback
3964346f654SOliver Hartkopp  * function
3974346f654SOliver Hartkopp  */
3984346f654SOliver Hartkopp static inline int hrtimer_callback_running(struct hrtimer *timer)
3994346f654SOliver Hartkopp {
4004346f654SOliver Hartkopp 	return timer->state & HRTIMER_STATE_CALLBACK;
4014346f654SOliver Hartkopp }
4024346f654SOliver Hartkopp 
403c0a31329SThomas Gleixner /* Forward a hrtimer so it expires after now: */
4044d672e7aSDavide Libenzi extern u64
40544f21475SRoman Zippel hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
406c0a31329SThomas Gleixner 
4075e05ad7dSDavide Libenzi /* Forward a hrtimer so it expires after the hrtimer's current now */
4084d672e7aSDavide Libenzi static inline u64 hrtimer_forward_now(struct hrtimer *timer,
4095e05ad7dSDavide Libenzi 				      ktime_t interval)
4105e05ad7dSDavide Libenzi {
4115e05ad7dSDavide Libenzi 	return hrtimer_forward(timer, timer->base->get_time(), interval);
4125e05ad7dSDavide Libenzi }
4135e05ad7dSDavide Libenzi 
41410c94ec1SThomas Gleixner /* Precise sleep: */
41510c94ec1SThomas Gleixner extern long hrtimer_nanosleep(struct timespec *rqtp,
416080344b9SOleg Nesterov 			      struct timespec __user *rmtp,
41710c94ec1SThomas Gleixner 			      const enum hrtimer_mode mode,
41810c94ec1SThomas Gleixner 			      const clockid_t clockid);
4191711ef38SToyo Abe extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
42010c94ec1SThomas Gleixner 
42100362e33SThomas Gleixner extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
42200362e33SThomas Gleixner 				 struct task_struct *tsk);
42300362e33SThomas Gleixner 
424654c8e0bSArjan van de Ven extern int schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
425654c8e0bSArjan van de Ven 						const enum hrtimer_mode mode);
426351b3f7aSCarsten Emde extern int schedule_hrtimeout_range_clock(ktime_t *expires,
427351b3f7aSCarsten Emde 		unsigned long delta, const enum hrtimer_mode mode, int clock);
4287bb67439SArjan van de Ven extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
4297bb67439SArjan van de Ven 
430c0a31329SThomas Gleixner /* Soft interrupt function to run the hrtimer queues: */
431c0a31329SThomas Gleixner extern void hrtimer_run_queues(void);
432d3d74453SPeter Zijlstra extern void hrtimer_run_pending(void);
433c0a31329SThomas Gleixner 
434c0a31329SThomas Gleixner /* Bootup initialization: */
435c0a31329SThomas Gleixner extern void __init hrtimers_init(void);
436c0a31329SThomas Gleixner 
43779bf2bb3SThomas Gleixner #if BITS_PER_LONG < 64
4384d672e7aSDavide Libenzi extern u64 ktime_divns(const ktime_t kt, s64 div);
43979bf2bb3SThomas Gleixner #else /* BITS_PER_LONG < 64 */
4404d672e7aSDavide Libenzi # define ktime_divns(kt, div)		(u64)((kt).tv64 / (div))
44179bf2bb3SThomas Gleixner #endif
44279bf2bb3SThomas Gleixner 
44388ad0bf6SIngo Molnar /* Show pending timers: */
44488ad0bf6SIngo Molnar extern void sysrq_timer_list_show(void);
44588ad0bf6SIngo Molnar 
446c0a31329SThomas Gleixner #endif
447