xref: /linux-6.15/include/linux/hrtimer.h (revision 799b64de)
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 
243c8aa39dSThomas Gleixner struct hrtimer_clock_base;
253c8aa39dSThomas Gleixner struct hrtimer_cpu_base;
263c8aa39dSThomas Gleixner 
27c0a31329SThomas Gleixner /*
28c0a31329SThomas Gleixner  * Mode arguments of xxx_hrtimer functions:
29c0a31329SThomas Gleixner  */
30c0a31329SThomas Gleixner enum hrtimer_mode {
31c9cb2e3dSThomas Gleixner 	HRTIMER_MODE_ABS,	/* Time value is absolute */
32c9cb2e3dSThomas Gleixner 	HRTIMER_MODE_REL,	/* Time value is relative to now */
33c0a31329SThomas Gleixner };
34c0a31329SThomas Gleixner 
35c9cb2e3dSThomas Gleixner /*
36c9cb2e3dSThomas Gleixner  * Return values for the callback function
37c9cb2e3dSThomas Gleixner  */
38c0a31329SThomas Gleixner enum hrtimer_restart {
39c9cb2e3dSThomas Gleixner 	HRTIMER_NORESTART,	/* Timer is not restarted */
40c9cb2e3dSThomas Gleixner 	HRTIMER_RESTART,	/* Timer must be restarted */
41c0a31329SThomas Gleixner };
42c0a31329SThomas Gleixner 
43303e967fSThomas Gleixner /*
4454cdfdb4SThomas Gleixner  * hrtimer callback modes:
4554cdfdb4SThomas Gleixner  *
4654cdfdb4SThomas Gleixner  *	HRTIMER_CB_SOFTIRQ:		Callback must run in softirq context
4754cdfdb4SThomas Gleixner  *	HRTIMER_CB_IRQSAFE:		Callback may run in hardirq context
4854cdfdb4SThomas Gleixner  *	HRTIMER_CB_IRQSAFE_NO_RESTART:	Callback may run in hardirq context and
4954cdfdb4SThomas Gleixner  *					does not restart the timer
508437fdc7SAndres Salomon  *	HRTIMER_CB_IRQSAFE_NO_SOFTIRQ:	Callback must run in hardirq context
5154cdfdb4SThomas Gleixner  *					Special mode for tick emultation
5254cdfdb4SThomas Gleixner  */
5354cdfdb4SThomas Gleixner enum hrtimer_cb_mode {
5454cdfdb4SThomas Gleixner 	HRTIMER_CB_SOFTIRQ,
5554cdfdb4SThomas Gleixner 	HRTIMER_CB_IRQSAFE,
5654cdfdb4SThomas Gleixner 	HRTIMER_CB_IRQSAFE_NO_RESTART,
5754cdfdb4SThomas Gleixner 	HRTIMER_CB_IRQSAFE_NO_SOFTIRQ,
5854cdfdb4SThomas Gleixner };
5954cdfdb4SThomas Gleixner 
6054cdfdb4SThomas Gleixner /*
6154cdfdb4SThomas Gleixner  * Values to track state of the timer
62303e967fSThomas Gleixner  *
63303e967fSThomas Gleixner  * Possible states:
64303e967fSThomas Gleixner  *
65303e967fSThomas Gleixner  * 0x00		inactive
66303e967fSThomas Gleixner  * 0x01		enqueued into rbtree
67303e967fSThomas Gleixner  * 0x02		callback function running
6854cdfdb4SThomas Gleixner  * 0x04		callback pending (high resolution mode)
6954cdfdb4SThomas Gleixner  *
7054cdfdb4SThomas Gleixner  * Special case:
71303e967fSThomas Gleixner  * 0x03		callback function running and enqueued
72303e967fSThomas Gleixner  *		(was requeued on another CPU)
73303e967fSThomas Gleixner  * The "callback function running and enqueued" status is only possible on
74303e967fSThomas Gleixner  * SMP. It happens for example when a posix timer expired and the callback
75303e967fSThomas Gleixner  * queued a signal. Between dropping the lock which protects the posix timer
76303e967fSThomas Gleixner  * and reacquiring the base lock of the hrtimer, another CPU can deliver the
77303e967fSThomas Gleixner  * signal and rearm the timer. We have to preserve the callback running state,
78303e967fSThomas Gleixner  * as otherwise the timer could be removed before the softirq code finishes the
79303e967fSThomas Gleixner  * the handling of the timer.
80303e967fSThomas Gleixner  *
813eb05676SLi Zefan  * The HRTIMER_STATE_ENQUEUED bit is always or'ed to the current state to
82303e967fSThomas Gleixner  * preserve the HRTIMER_STATE_CALLBACK bit in the above scenario.
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 #define HRTIMER_STATE_CALLBACK	0x02
8954cdfdb4SThomas Gleixner #define HRTIMER_STATE_PENDING	0x04
90303e967fSThomas Gleixner 
91c0a31329SThomas Gleixner /**
92c0a31329SThomas Gleixner  * struct hrtimer - the basic hrtimer structure
93c0a31329SThomas Gleixner  * @node:	red black tree node for time ordered insertion
94c0a31329SThomas Gleixner  * @expires:	the absolute expiry time in the hrtimers internal
95c0a31329SThomas Gleixner  *		representation. The time is related to the clock on
96c0a31329SThomas Gleixner  *		which the timer is based.
97c0a31329SThomas Gleixner  * @function:	timer expiry callback function
98c0a31329SThomas Gleixner  * @base:	pointer to the timer base (per cpu and per clock)
99303e967fSThomas Gleixner  * @state:	state information (See bit values above)
10054cdfdb4SThomas Gleixner  * @cb_mode:	high resolution timer feature to select the callback execution
10154cdfdb4SThomas Gleixner  *		 mode
10254cdfdb4SThomas Gleixner  * @cb_entry:	list head to enqueue an expired timer into the callback list
10354cdfdb4SThomas Gleixner  * @start_site:	timer statistics field to store the site where the timer
10454cdfdb4SThomas Gleixner  *		was started
10554cdfdb4SThomas Gleixner  * @start_comm: timer statistics field to store the name of the process which
10654cdfdb4SThomas Gleixner  *		started the timer
10754cdfdb4SThomas Gleixner  * @start_pid: timer statistics field to store the pid of the task which
10854cdfdb4SThomas Gleixner  *		started the timer
109c0a31329SThomas Gleixner  *
11054cdfdb4SThomas Gleixner  * The hrtimer structure must be initialized by hrtimer_init()
111c0a31329SThomas Gleixner  */
112c0a31329SThomas Gleixner struct hrtimer {
113c0a31329SThomas Gleixner 	struct rb_node			node;
114*799b64deSArjan van de Ven 	ktime_t				_expires;
115c9cb2e3dSThomas Gleixner 	enum hrtimer_restart		(*function)(struct hrtimer *);
1163c8aa39dSThomas Gleixner 	struct hrtimer_clock_base	*base;
117303e967fSThomas Gleixner 	unsigned long			state;
11854cdfdb4SThomas Gleixner 	enum hrtimer_cb_mode		cb_mode;
11954cdfdb4SThomas Gleixner 	struct list_head		cb_entry;
12082f67cd9SIngo Molnar #ifdef CONFIG_TIMER_STATS
12182f67cd9SIngo Molnar 	void				*start_site;
12282f67cd9SIngo Molnar 	char				start_comm[16];
12382f67cd9SIngo Molnar 	int				start_pid;
12482f67cd9SIngo Molnar #endif
125c0a31329SThomas Gleixner };
126c0a31329SThomas Gleixner 
127c0a31329SThomas Gleixner /**
12800362e33SThomas Gleixner  * struct hrtimer_sleeper - simple sleeper structure
12900362e33SThomas Gleixner  * @timer:	embedded timer structure
13000362e33SThomas Gleixner  * @task:	task to wake up
13100362e33SThomas Gleixner  *
13200362e33SThomas Gleixner  * task is set to NULL, when the timer expires.
13300362e33SThomas Gleixner  */
13400362e33SThomas Gleixner struct hrtimer_sleeper {
13500362e33SThomas Gleixner 	struct hrtimer timer;
13600362e33SThomas Gleixner 	struct task_struct *task;
13700362e33SThomas Gleixner };
13800362e33SThomas 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.
144c0a31329SThomas Gleixner  * @active:		red black tree root node for the active timers
145288867ecSThomas Gleixner  * @first:		pointer to the timer node which expires first
146c0a31329SThomas Gleixner  * @resolution:		the resolution of the clock, in nanoseconds
147c0a31329SThomas Gleixner  * @get_time:		function to retrieve the current time of the clock
148a580290cSMartin Waitz  * @get_softirq_time:	function to retrieve the current time from the softirq
14992127c7aSThomas Gleixner  * @softirq_time:	the time when running the hrtimer queue in the softirq
15054cdfdb4SThomas Gleixner  * @offset:		offset of this clock to the monotonic base
15154cdfdb4SThomas Gleixner  * @reprogram:		function to reprogram the timer event
152c0a31329SThomas Gleixner  */
1533c8aa39dSThomas Gleixner struct hrtimer_clock_base {
1543c8aa39dSThomas Gleixner 	struct hrtimer_cpu_base	*cpu_base;
155c0a31329SThomas Gleixner 	clockid_t		index;
156c0a31329SThomas Gleixner 	struct rb_root		active;
157288867ecSThomas Gleixner 	struct rb_node		*first;
158e2787630SThomas Gleixner 	ktime_t			resolution;
159c0a31329SThomas Gleixner 	ktime_t			(*get_time)(void);
16092127c7aSThomas Gleixner 	ktime_t			(*get_softirq_time)(void);
16192127c7aSThomas Gleixner 	ktime_t			softirq_time;
16254cdfdb4SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
16354cdfdb4SThomas Gleixner 	ktime_t			offset;
16454cdfdb4SThomas Gleixner 	int			(*reprogram)(struct hrtimer *t,
16554cdfdb4SThomas Gleixner 					     struct hrtimer_clock_base *b,
16654cdfdb4SThomas Gleixner 					     ktime_t n);
16754cdfdb4SThomas Gleixner #endif
1683c8aa39dSThomas Gleixner };
1693c8aa39dSThomas Gleixner 
1703c8aa39dSThomas Gleixner #define HRTIMER_MAX_CLOCK_BASES 2
1713c8aa39dSThomas Gleixner 
1723c8aa39dSThomas Gleixner /*
1733c8aa39dSThomas Gleixner  * struct hrtimer_cpu_base - the per cpu clock bases
1743c8aa39dSThomas Gleixner  * @lock:		lock protecting the base and associated clock bases
1753c8aa39dSThomas Gleixner  *			and timers
1763c8aa39dSThomas Gleixner  * @clock_base:		array of clock bases for this cpu
1773c8aa39dSThomas Gleixner  * @curr_timer:		the timer which is executing a callback right now
17854cdfdb4SThomas Gleixner  * @expires_next:	absolute time of the next event which was scheduled
17954cdfdb4SThomas Gleixner  *			via clock_set_next_event()
18054cdfdb4SThomas Gleixner  * @hres_active:	State of high resolution mode
18154cdfdb4SThomas Gleixner  * @check_clocks:	Indictator, when set evaluate time source and clock
18254cdfdb4SThomas Gleixner  *			event devices whether high resolution mode can be
18354cdfdb4SThomas Gleixner  *			activated.
18454cdfdb4SThomas Gleixner  * @cb_pending:		Expired timers are moved from the rbtree to this
18554cdfdb4SThomas Gleixner  *			list in the timer interrupt. The list is processed
18654cdfdb4SThomas Gleixner  *			in the softirq.
18754cdfdb4SThomas Gleixner  * @nr_events:		Total number of timer interrupt events
1883c8aa39dSThomas Gleixner  */
1893c8aa39dSThomas Gleixner struct hrtimer_cpu_base {
1903c8aa39dSThomas Gleixner 	spinlock_t			lock;
1913c8aa39dSThomas Gleixner 	struct hrtimer_clock_base	clock_base[HRTIMER_MAX_CLOCK_BASES];
192d3d74453SPeter Zijlstra 	struct list_head		cb_pending;
19354cdfdb4SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
19454cdfdb4SThomas Gleixner 	ktime_t				expires_next;
19554cdfdb4SThomas Gleixner 	int				hres_active;
19654cdfdb4SThomas Gleixner 	unsigned long			nr_events;
19754cdfdb4SThomas Gleixner #endif
198c0a31329SThomas Gleixner };
199c0a31329SThomas Gleixner 
20054cdfdb4SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
20154cdfdb4SThomas Gleixner struct clock_event_device;
20254cdfdb4SThomas Gleixner 
20354cdfdb4SThomas Gleixner extern void clock_was_set(void);
204995f054fSIngo Molnar extern void hres_timers_resume(void);
20554cdfdb4SThomas Gleixner extern void hrtimer_interrupt(struct clock_event_device *dev);
20654cdfdb4SThomas Gleixner 
20754cdfdb4SThomas Gleixner /*
20854cdfdb4SThomas Gleixner  * In high resolution mode the time reference must be read accurate
20954cdfdb4SThomas Gleixner  */
21054cdfdb4SThomas Gleixner static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
21154cdfdb4SThomas Gleixner {
21254cdfdb4SThomas Gleixner 	return timer->base->get_time();
21354cdfdb4SThomas Gleixner }
21454cdfdb4SThomas Gleixner 
2158f4d37ecSPeter Zijlstra static inline int hrtimer_is_hres_active(struct hrtimer *timer)
2168f4d37ecSPeter Zijlstra {
2178f4d37ecSPeter Zijlstra 	return timer->base->cpu_base->hres_active;
2188f4d37ecSPeter Zijlstra }
2198f4d37ecSPeter Zijlstra 
22063ca243bSArjan van de Ven static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
22163ca243bSArjan van de Ven {
222*799b64deSArjan van de Ven 	timer->_expires = time;
22363ca243bSArjan van de Ven }
22463ca243bSArjan van de Ven static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
22563ca243bSArjan van de Ven {
226*799b64deSArjan van de Ven 	timer->_expires.tv64 = tv64;
22763ca243bSArjan van de Ven }
22863ca243bSArjan van de Ven 
22963ca243bSArjan van de Ven static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
23063ca243bSArjan van de Ven {
231*799b64deSArjan van de Ven 	timer->_expires = ktime_add_safe(timer->_expires, time);
23263ca243bSArjan van de Ven }
23363ca243bSArjan van de Ven 
23463ca243bSArjan van de Ven static inline void hrtimer_add_expires_ns(struct hrtimer *timer, unsigned long ns)
23563ca243bSArjan van de Ven {
236*799b64deSArjan van de Ven 	timer->_expires = ktime_add_ns(timer->_expires, ns);
23763ca243bSArjan van de Ven }
23863ca243bSArjan van de Ven 
23963ca243bSArjan van de Ven static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
24063ca243bSArjan van de Ven {
241*799b64deSArjan van de Ven 	return timer->_expires;
24263ca243bSArjan van de Ven }
24363ca243bSArjan van de Ven 
24463ca243bSArjan van de Ven static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
24563ca243bSArjan van de Ven {
246*799b64deSArjan van de Ven 	return timer->_expires.tv64;
24763ca243bSArjan van de Ven }
24863ca243bSArjan van de Ven 
24963ca243bSArjan van de Ven static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
25063ca243bSArjan van de Ven {
251*799b64deSArjan van de Ven 	return ktime_to_ns(timer->_expires);
25263ca243bSArjan van de Ven }
25363ca243bSArjan van de Ven 
25463ca243bSArjan van de Ven static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
25563ca243bSArjan van de Ven {
256*799b64deSArjan van de Ven     return ktime_sub(timer->_expires, timer->base->get_time());
25763ca243bSArjan van de Ven }
25863ca243bSArjan van de Ven 
25954cdfdb4SThomas Gleixner /*
26054cdfdb4SThomas Gleixner  * The resolution of the clocks. The resolution value is returned in
26154cdfdb4SThomas Gleixner  * the clock_getres() system call to give application programmers an
26254cdfdb4SThomas Gleixner  * idea of the (in)accuracy of timers. Timer values are rounded up to
26354cdfdb4SThomas Gleixner  * this resolution values.
26454cdfdb4SThomas Gleixner  */
265151db1fcSTony Breeds # define HIGH_RES_NSEC		1
266151db1fcSTony Breeds # define KTIME_HIGH_RES		(ktime_t) { .tv64 = HIGH_RES_NSEC }
267151db1fcSTony Breeds # define MONOTONIC_RES_NSEC	HIGH_RES_NSEC
26854cdfdb4SThomas Gleixner # define KTIME_MONOTONIC_RES	KTIME_HIGH_RES
26954cdfdb4SThomas Gleixner 
27054cdfdb4SThomas Gleixner #else
27154cdfdb4SThomas Gleixner 
272151db1fcSTony Breeds # define MONOTONIC_RES_NSEC	LOW_RES_NSEC
27354cdfdb4SThomas Gleixner # define KTIME_MONOTONIC_RES	KTIME_LOW_RES
27454cdfdb4SThomas Gleixner 
275becf8b5dSThomas Gleixner /*
276becf8b5dSThomas Gleixner  * clock_was_set() is a NOP for non- high-resolution systems. The
277becf8b5dSThomas Gleixner  * time-sorted order guarantees that a timer does not expire early and
278becf8b5dSThomas Gleixner  * is expired in the next softirq when the clock was advanced.
279becf8b5dSThomas Gleixner  */
28054cdfdb4SThomas Gleixner static inline void clock_was_set(void) { }
28154cdfdb4SThomas Gleixner 
282995f054fSIngo Molnar static inline void hres_timers_resume(void) { }
283995f054fSIngo Molnar 
28454cdfdb4SThomas Gleixner /*
28554cdfdb4SThomas Gleixner  * In non high resolution mode the time reference is taken from
28654cdfdb4SThomas Gleixner  * the base softirq time variable.
28754cdfdb4SThomas Gleixner  */
28854cdfdb4SThomas Gleixner static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
28954cdfdb4SThomas Gleixner {
29054cdfdb4SThomas Gleixner 	return timer->base->softirq_time;
29154cdfdb4SThomas Gleixner }
29254cdfdb4SThomas Gleixner 
2938f4d37ecSPeter Zijlstra static inline int hrtimer_is_hres_active(struct hrtimer *timer)
2948f4d37ecSPeter Zijlstra {
2958f4d37ecSPeter Zijlstra 	return 0;
2968f4d37ecSPeter Zijlstra }
29754cdfdb4SThomas Gleixner #endif
29854cdfdb4SThomas Gleixner 
299d316c57fSThomas Gleixner extern ktime_t ktime_get(void);
300d316c57fSThomas Gleixner extern ktime_t ktime_get_real(void);
301becf8b5dSThomas Gleixner 
302c0a31329SThomas Gleixner /* Exported timer functions: */
303c0a31329SThomas Gleixner 
304c0a31329SThomas Gleixner /* Initialize timers: */
3057978672cSGeorge Anzinger extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
3067978672cSGeorge Anzinger 			 enum hrtimer_mode mode);
307c0a31329SThomas Gleixner 
308237fc6e7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
309237fc6e7SThomas Gleixner extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
310237fc6e7SThomas Gleixner 				  enum hrtimer_mode mode);
311237fc6e7SThomas Gleixner 
312237fc6e7SThomas Gleixner extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
313237fc6e7SThomas Gleixner #else
314237fc6e7SThomas Gleixner static inline void hrtimer_init_on_stack(struct hrtimer *timer,
315237fc6e7SThomas Gleixner 					 clockid_t which_clock,
316237fc6e7SThomas Gleixner 					 enum hrtimer_mode mode)
317237fc6e7SThomas Gleixner {
318237fc6e7SThomas Gleixner 	hrtimer_init(timer, which_clock, mode);
319237fc6e7SThomas Gleixner }
320237fc6e7SThomas Gleixner static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
321237fc6e7SThomas Gleixner #endif
322237fc6e7SThomas Gleixner 
323c0a31329SThomas Gleixner /* Basic timer operations: */
324c0a31329SThomas Gleixner extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
325c0a31329SThomas Gleixner 			 const enum hrtimer_mode mode);
326c0a31329SThomas Gleixner extern int hrtimer_cancel(struct hrtimer *timer);
327c0a31329SThomas Gleixner extern int hrtimer_try_to_cancel(struct hrtimer *timer);
328c0a31329SThomas Gleixner 
32963ca243bSArjan van de Ven static inline int hrtimer_start_expires(struct hrtimer *timer,
33063ca243bSArjan van de Ven 						enum hrtimer_mode mode)
33163ca243bSArjan van de Ven {
33263ca243bSArjan van de Ven 	return hrtimer_start(timer, hrtimer_get_expires(timer), mode);
33363ca243bSArjan van de Ven }
33463ca243bSArjan van de Ven 
335c9cb2e3dSThomas Gleixner static inline int hrtimer_restart(struct hrtimer *timer)
336c9cb2e3dSThomas Gleixner {
337*799b64deSArjan van de Ven 	return hrtimer_start(timer, timer->_expires, HRTIMER_MODE_ABS);
338c9cb2e3dSThomas Gleixner }
339c0a31329SThomas Gleixner 
340c0a31329SThomas Gleixner /* Query timers: */
341c0a31329SThomas Gleixner extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
342c0a31329SThomas Gleixner extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
343c0a31329SThomas Gleixner 
34469239749STony Lindgren extern ktime_t hrtimer_get_next_event(void);
34569239749STony Lindgren 
346303e967fSThomas Gleixner /*
347303e967fSThomas Gleixner  * A timer is active, when it is enqueued into the rbtree or the callback
348303e967fSThomas Gleixner  * function is running.
349303e967fSThomas Gleixner  */
350c0a31329SThomas Gleixner static inline int hrtimer_active(const struct hrtimer *timer)
351c0a31329SThomas Gleixner {
352303e967fSThomas Gleixner 	return timer->state != HRTIMER_STATE_INACTIVE;
353c0a31329SThomas Gleixner }
354c0a31329SThomas Gleixner 
35554cdfdb4SThomas Gleixner /*
35654cdfdb4SThomas Gleixner  * Helper function to check, whether the timer is on one of the queues
35754cdfdb4SThomas Gleixner  */
35854cdfdb4SThomas Gleixner static inline int hrtimer_is_queued(struct hrtimer *timer)
35954cdfdb4SThomas Gleixner {
36054cdfdb4SThomas Gleixner 	return timer->state &
36154cdfdb4SThomas Gleixner 		(HRTIMER_STATE_ENQUEUED | HRTIMER_STATE_PENDING);
36254cdfdb4SThomas Gleixner }
36354cdfdb4SThomas Gleixner 
3644346f654SOliver Hartkopp /*
3654346f654SOliver Hartkopp  * Helper function to check, whether the timer is running the callback
3664346f654SOliver Hartkopp  * function
3674346f654SOliver Hartkopp  */
3684346f654SOliver Hartkopp static inline int hrtimer_callback_running(struct hrtimer *timer)
3694346f654SOliver Hartkopp {
3704346f654SOliver Hartkopp 	return timer->state & HRTIMER_STATE_CALLBACK;
3714346f654SOliver Hartkopp }
3724346f654SOliver Hartkopp 
373c0a31329SThomas Gleixner /* Forward a hrtimer so it expires after now: */
3744d672e7aSDavide Libenzi extern u64
37544f21475SRoman Zippel hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
376c0a31329SThomas Gleixner 
3775e05ad7dSDavide Libenzi /* Forward a hrtimer so it expires after the hrtimer's current now */
3784d672e7aSDavide Libenzi static inline u64 hrtimer_forward_now(struct hrtimer *timer,
3795e05ad7dSDavide Libenzi 				      ktime_t interval)
3805e05ad7dSDavide Libenzi {
3815e05ad7dSDavide Libenzi 	return hrtimer_forward(timer, timer->base->get_time(), interval);
3825e05ad7dSDavide Libenzi }
3835e05ad7dSDavide Libenzi 
38410c94ec1SThomas Gleixner /* Precise sleep: */
38510c94ec1SThomas Gleixner extern long hrtimer_nanosleep(struct timespec *rqtp,
386080344b9SOleg Nesterov 			      struct timespec __user *rmtp,
38710c94ec1SThomas Gleixner 			      const enum hrtimer_mode mode,
38810c94ec1SThomas Gleixner 			      const clockid_t clockid);
3891711ef38SToyo Abe extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
39010c94ec1SThomas Gleixner 
39100362e33SThomas Gleixner extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
39200362e33SThomas Gleixner 				 struct task_struct *tsk);
39300362e33SThomas Gleixner 
3947bb67439SArjan van de Ven extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
3957bb67439SArjan van de Ven 
396c0a31329SThomas Gleixner /* Soft interrupt function to run the hrtimer queues: */
397c0a31329SThomas Gleixner extern void hrtimer_run_queues(void);
398d3d74453SPeter Zijlstra extern void hrtimer_run_pending(void);
399c0a31329SThomas Gleixner 
400c0a31329SThomas Gleixner /* Bootup initialization: */
401c0a31329SThomas Gleixner extern void __init hrtimers_init(void);
402c0a31329SThomas Gleixner 
40379bf2bb3SThomas Gleixner #if BITS_PER_LONG < 64
4044d672e7aSDavide Libenzi extern u64 ktime_divns(const ktime_t kt, s64 div);
40579bf2bb3SThomas Gleixner #else /* BITS_PER_LONG < 64 */
4064d672e7aSDavide Libenzi # define ktime_divns(kt, div)		(u64)((kt).tv64 / (div))
40779bf2bb3SThomas Gleixner #endif
40879bf2bb3SThomas Gleixner 
40988ad0bf6SIngo Molnar /* Show pending timers: */
41088ad0bf6SIngo Molnar extern void sysrq_timer_list_show(void);
41188ad0bf6SIngo Molnar 
41282f67cd9SIngo Molnar /*
41382f67cd9SIngo Molnar  * Timer-statistics info:
41482f67cd9SIngo Molnar  */
41582f67cd9SIngo Molnar #ifdef CONFIG_TIMER_STATS
41682f67cd9SIngo Molnar 
41782f67cd9SIngo Molnar extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
418c5c061b8SVenki Pallipadi 				     void *timerf, char *comm,
419c5c061b8SVenki Pallipadi 				     unsigned int timer_flag);
42082f67cd9SIngo Molnar 
42182f67cd9SIngo Molnar static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
42282f67cd9SIngo Molnar {
42382f67cd9SIngo Molnar 	timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
424c5c061b8SVenki Pallipadi 				 timer->function, timer->start_comm, 0);
42582f67cd9SIngo Molnar }
42682f67cd9SIngo Molnar 
42782f67cd9SIngo Molnar extern void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer,
42882f67cd9SIngo Molnar 						 void *addr);
42982f67cd9SIngo Molnar 
43082f67cd9SIngo Molnar static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
43182f67cd9SIngo Molnar {
43282f67cd9SIngo Molnar 	__timer_stats_hrtimer_set_start_info(timer, __builtin_return_address(0));
43382f67cd9SIngo Molnar }
43482f67cd9SIngo Molnar 
43582f67cd9SIngo Molnar static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
43682f67cd9SIngo Molnar {
43782f67cd9SIngo Molnar 	timer->start_site = NULL;
43882f67cd9SIngo Molnar }
43982f67cd9SIngo Molnar #else
44082f67cd9SIngo Molnar static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
44182f67cd9SIngo Molnar {
44282f67cd9SIngo Molnar }
44382f67cd9SIngo Molnar 
44482f67cd9SIngo Molnar static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
44582f67cd9SIngo Molnar {
44682f67cd9SIngo Molnar }
44782f67cd9SIngo Molnar 
44882f67cd9SIngo Molnar static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
44982f67cd9SIngo Molnar {
45082f67cd9SIngo Molnar }
45182f67cd9SIngo Molnar #endif
45282f67cd9SIngo Molnar 
453c0a31329SThomas Gleixner #endif
454