xref: /linux-6.15/include/linux/hrtimer.h (revision 2075eb8d)
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;
114799b64deSArjan van de Ven 	ktime_t				_expires;
115654c8e0bSArjan van de Ven 	ktime_t				_softexpires;
116c9cb2e3dSThomas Gleixner 	enum hrtimer_restart		(*function)(struct hrtimer *);
1173c8aa39dSThomas Gleixner 	struct hrtimer_clock_base	*base;
118303e967fSThomas Gleixner 	unsigned long			state;
11954cdfdb4SThomas Gleixner 	enum hrtimer_cb_mode		cb_mode;
12054cdfdb4SThomas Gleixner 	struct list_head		cb_entry;
12182f67cd9SIngo Molnar #ifdef CONFIG_TIMER_STATS
12282f67cd9SIngo Molnar 	void				*start_site;
12382f67cd9SIngo Molnar 	char				start_comm[16];
12482f67cd9SIngo Molnar 	int				start_pid;
12582f67cd9SIngo Molnar #endif
126c0a31329SThomas Gleixner };
127c0a31329SThomas Gleixner 
128c0a31329SThomas Gleixner /**
12900362e33SThomas Gleixner  * struct hrtimer_sleeper - simple sleeper structure
13000362e33SThomas Gleixner  * @timer:	embedded timer structure
13100362e33SThomas Gleixner  * @task:	task to wake up
13200362e33SThomas Gleixner  *
13300362e33SThomas Gleixner  * task is set to NULL, when the timer expires.
13400362e33SThomas Gleixner  */
13500362e33SThomas Gleixner struct hrtimer_sleeper {
13600362e33SThomas Gleixner 	struct hrtimer timer;
13700362e33SThomas Gleixner 	struct task_struct *task;
13800362e33SThomas Gleixner };
13900362e33SThomas Gleixner 
14000362e33SThomas Gleixner /**
141d1d67174SAndres Salomon  * struct hrtimer_clock_base - the timer base for a specific clock
14205fb6bf0SRandy Dunlap  * @cpu_base:		per cpu clock base
1433c8aa39dSThomas Gleixner  * @index:		clock type index for per_cpu support when moving a
1443c8aa39dSThomas Gleixner  *			timer to a base on another cpu.
145c0a31329SThomas Gleixner  * @active:		red black tree root node for the active timers
146288867ecSThomas Gleixner  * @first:		pointer to the timer node which expires first
147c0a31329SThomas Gleixner  * @resolution:		the resolution of the clock, in nanoseconds
148c0a31329SThomas Gleixner  * @get_time:		function to retrieve the current time of the clock
149a580290cSMartin Waitz  * @get_softirq_time:	function to retrieve the current time from the softirq
15092127c7aSThomas Gleixner  * @softirq_time:	the time when running the hrtimer queue in the softirq
15154cdfdb4SThomas Gleixner  * @offset:		offset of this clock to the monotonic base
15254cdfdb4SThomas Gleixner  * @reprogram:		function to reprogram the timer event
153c0a31329SThomas Gleixner  */
1543c8aa39dSThomas Gleixner struct hrtimer_clock_base {
1553c8aa39dSThomas Gleixner 	struct hrtimer_cpu_base	*cpu_base;
156c0a31329SThomas Gleixner 	clockid_t		index;
157c0a31329SThomas Gleixner 	struct rb_root		active;
158288867ecSThomas Gleixner 	struct rb_node		*first;
159e2787630SThomas Gleixner 	ktime_t			resolution;
160c0a31329SThomas Gleixner 	ktime_t			(*get_time)(void);
16192127c7aSThomas Gleixner 	ktime_t			(*get_softirq_time)(void);
16292127c7aSThomas Gleixner 	ktime_t			softirq_time;
16354cdfdb4SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
16454cdfdb4SThomas Gleixner 	ktime_t			offset;
16554cdfdb4SThomas Gleixner 	int			(*reprogram)(struct hrtimer *t,
16654cdfdb4SThomas Gleixner 					     struct hrtimer_clock_base *b,
16754cdfdb4SThomas Gleixner 					     ktime_t n);
16854cdfdb4SThomas Gleixner #endif
1693c8aa39dSThomas Gleixner };
1703c8aa39dSThomas Gleixner 
1713c8aa39dSThomas Gleixner #define HRTIMER_MAX_CLOCK_BASES 2
1723c8aa39dSThomas Gleixner 
1733c8aa39dSThomas Gleixner /*
1743c8aa39dSThomas Gleixner  * struct hrtimer_cpu_base - the per cpu clock bases
1753c8aa39dSThomas Gleixner  * @lock:		lock protecting the base and associated clock bases
1763c8aa39dSThomas Gleixner  *			and timers
1773c8aa39dSThomas Gleixner  * @clock_base:		array of clock bases for this cpu
1783c8aa39dSThomas Gleixner  * @curr_timer:		the timer which is executing a callback right now
17954cdfdb4SThomas Gleixner  * @expires_next:	absolute time of the next event which was scheduled
18054cdfdb4SThomas Gleixner  *			via clock_set_next_event()
18154cdfdb4SThomas Gleixner  * @hres_active:	State of high resolution mode
18254cdfdb4SThomas Gleixner  * @check_clocks:	Indictator, when set evaluate time source and clock
18354cdfdb4SThomas Gleixner  *			event devices whether high resolution mode can be
18454cdfdb4SThomas Gleixner  *			activated.
18554cdfdb4SThomas Gleixner  * @cb_pending:		Expired timers are moved from the rbtree to this
18654cdfdb4SThomas Gleixner  *			list in the timer interrupt. The list is processed
18754cdfdb4SThomas Gleixner  *			in the softirq.
18854cdfdb4SThomas Gleixner  * @nr_events:		Total number of timer interrupt events
1893c8aa39dSThomas Gleixner  */
1903c8aa39dSThomas Gleixner struct hrtimer_cpu_base {
1913c8aa39dSThomas Gleixner 	spinlock_t			lock;
1923c8aa39dSThomas Gleixner 	struct hrtimer_clock_base	clock_base[HRTIMER_MAX_CLOCK_BASES];
193d3d74453SPeter Zijlstra 	struct list_head		cb_pending;
19454cdfdb4SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
19554cdfdb4SThomas Gleixner 	ktime_t				expires_next;
19654cdfdb4SThomas Gleixner 	int				hres_active;
19754cdfdb4SThomas Gleixner 	unsigned long			nr_events;
19854cdfdb4SThomas Gleixner #endif
199c0a31329SThomas Gleixner };
200c0a31329SThomas Gleixner 
20163ca243bSArjan van de Ven static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
20263ca243bSArjan van de Ven {
203799b64deSArjan van de Ven 	timer->_expires = time;
204654c8e0bSArjan van de Ven 	timer->_softexpires = time;
20563ca243bSArjan van de Ven }
206654c8e0bSArjan van de Ven 
207654c8e0bSArjan van de Ven static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
208654c8e0bSArjan van de Ven {
209654c8e0bSArjan van de Ven 	timer->_softexpires = time;
210654c8e0bSArjan van de Ven 	timer->_expires = ktime_add_safe(time, delta);
211654c8e0bSArjan van de Ven }
212654c8e0bSArjan van de Ven 
213654c8e0bSArjan van de Ven static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, unsigned long delta)
214654c8e0bSArjan van de Ven {
215654c8e0bSArjan van de Ven 	timer->_softexpires = time;
216654c8e0bSArjan van de Ven 	timer->_expires = ktime_add_safe(time, ns_to_ktime(delta));
217654c8e0bSArjan van de Ven }
218654c8e0bSArjan van de Ven 
21963ca243bSArjan van de Ven static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
22063ca243bSArjan van de Ven {
221799b64deSArjan van de Ven 	timer->_expires.tv64 = tv64;
222654c8e0bSArjan van de Ven 	timer->_softexpires.tv64 = tv64;
22363ca243bSArjan van de Ven }
22463ca243bSArjan van de Ven 
22563ca243bSArjan van de Ven static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
22663ca243bSArjan van de Ven {
227799b64deSArjan van de Ven 	timer->_expires = ktime_add_safe(timer->_expires, time);
228654c8e0bSArjan van de Ven 	timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
22963ca243bSArjan van de Ven }
23063ca243bSArjan van de Ven 
23163ca243bSArjan van de Ven static inline void hrtimer_add_expires_ns(struct hrtimer *timer, unsigned long ns)
23263ca243bSArjan van de Ven {
233799b64deSArjan van de Ven 	timer->_expires = ktime_add_ns(timer->_expires, ns);
234654c8e0bSArjan van de Ven 	timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
23563ca243bSArjan van de Ven }
23663ca243bSArjan van de Ven 
23763ca243bSArjan van de Ven static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
23863ca243bSArjan van de Ven {
239799b64deSArjan van de Ven 	return timer->_expires;
24063ca243bSArjan van de Ven }
24163ca243bSArjan van de Ven 
242654c8e0bSArjan van de Ven static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
243654c8e0bSArjan van de Ven {
244654c8e0bSArjan van de Ven 	return timer->_softexpires;
245654c8e0bSArjan van de Ven }
246654c8e0bSArjan van de Ven 
24763ca243bSArjan van de Ven static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
24863ca243bSArjan van de Ven {
249799b64deSArjan van de Ven 	return timer->_expires.tv64;
25063ca243bSArjan van de Ven }
251654c8e0bSArjan van de Ven static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
252654c8e0bSArjan van de Ven {
253654c8e0bSArjan van de Ven 	return timer->_softexpires.tv64;
254654c8e0bSArjan van de Ven }
25563ca243bSArjan van de Ven 
25663ca243bSArjan van de Ven static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
25763ca243bSArjan van de Ven {
258799b64deSArjan van de Ven 	return ktime_to_ns(timer->_expires);
25963ca243bSArjan van de Ven }
26063ca243bSArjan van de Ven 
26163ca243bSArjan van de Ven static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
26263ca243bSArjan van de Ven {
263799b64deSArjan van de Ven     return ktime_sub(timer->_expires, timer->base->get_time());
26463ca243bSArjan van de Ven }
26563ca243bSArjan van de Ven 
266584fb4a7SArjan van de Ven #ifdef CONFIG_HIGH_RES_TIMERS
267584fb4a7SArjan van de Ven struct clock_event_device;
268584fb4a7SArjan van de Ven 
269584fb4a7SArjan van de Ven extern void clock_was_set(void);
270584fb4a7SArjan van de Ven extern void hres_timers_resume(void);
271584fb4a7SArjan van de Ven extern void hrtimer_interrupt(struct clock_event_device *dev);
272584fb4a7SArjan van de Ven 
27354cdfdb4SThomas Gleixner /*
2742ec02270SArjan van de Ven  * In high resolution mode the time reference must be read accurate
2752ec02270SArjan van de Ven  */
2762ec02270SArjan van de Ven static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
2772ec02270SArjan van de Ven {
2782ec02270SArjan van de Ven 	return timer->base->get_time();
2792ec02270SArjan van de Ven }
2802ec02270SArjan van de Ven 
2812ec02270SArjan van de Ven static inline int hrtimer_is_hres_active(struct hrtimer *timer)
2822ec02270SArjan van de Ven {
2832ec02270SArjan van de Ven 	return timer->base->cpu_base->hres_active;
2842ec02270SArjan van de Ven }
2852ec02270SArjan van de Ven 
286*2075eb8dSArjan van de Ven extern void hrtimer_peek_ahead_timers(void);
287*2075eb8dSArjan van de Ven 
2882ec02270SArjan van de Ven /*
28954cdfdb4SThomas Gleixner  * The resolution of the clocks. The resolution value is returned in
29054cdfdb4SThomas Gleixner  * the clock_getres() system call to give application programmers an
29154cdfdb4SThomas Gleixner  * idea of the (in)accuracy of timers. Timer values are rounded up to
29254cdfdb4SThomas Gleixner  * this resolution values.
29354cdfdb4SThomas Gleixner  */
294151db1fcSTony Breeds # define HIGH_RES_NSEC		1
295151db1fcSTony Breeds # define KTIME_HIGH_RES		(ktime_t) { .tv64 = HIGH_RES_NSEC }
296151db1fcSTony Breeds # define MONOTONIC_RES_NSEC	HIGH_RES_NSEC
29754cdfdb4SThomas Gleixner # define KTIME_MONOTONIC_RES	KTIME_HIGH_RES
29854cdfdb4SThomas Gleixner 
29954cdfdb4SThomas Gleixner #else
30054cdfdb4SThomas Gleixner 
301151db1fcSTony Breeds # define MONOTONIC_RES_NSEC	LOW_RES_NSEC
30254cdfdb4SThomas Gleixner # define KTIME_MONOTONIC_RES	KTIME_LOW_RES
30354cdfdb4SThomas Gleixner 
304becf8b5dSThomas Gleixner /*
305becf8b5dSThomas Gleixner  * clock_was_set() is a NOP for non- high-resolution systems. The
306becf8b5dSThomas Gleixner  * time-sorted order guarantees that a timer does not expire early and
307becf8b5dSThomas Gleixner  * is expired in the next softirq when the clock was advanced.
308becf8b5dSThomas Gleixner  */
30954cdfdb4SThomas Gleixner static inline void clock_was_set(void) { }
310*2075eb8dSArjan van de Ven static inline void hrtimer_peek_ahead_timers(void) { }
31154cdfdb4SThomas Gleixner 
312995f054fSIngo Molnar static inline void hres_timers_resume(void) { }
313995f054fSIngo Molnar 
31454cdfdb4SThomas Gleixner /*
31554cdfdb4SThomas Gleixner  * In non high resolution mode the time reference is taken from
31654cdfdb4SThomas Gleixner  * the base softirq time variable.
31754cdfdb4SThomas Gleixner  */
31854cdfdb4SThomas Gleixner static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
31954cdfdb4SThomas Gleixner {
32054cdfdb4SThomas Gleixner 	return timer->base->softirq_time;
32154cdfdb4SThomas Gleixner }
32254cdfdb4SThomas Gleixner 
3238f4d37ecSPeter Zijlstra static inline int hrtimer_is_hres_active(struct hrtimer *timer)
3248f4d37ecSPeter Zijlstra {
3258f4d37ecSPeter Zijlstra 	return 0;
3268f4d37ecSPeter Zijlstra }
32754cdfdb4SThomas Gleixner #endif
32854cdfdb4SThomas Gleixner 
329d316c57fSThomas Gleixner extern ktime_t ktime_get(void);
330d316c57fSThomas Gleixner extern ktime_t ktime_get_real(void);
331becf8b5dSThomas Gleixner 
3322e94d1f7SArjan van de Ven 
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);
362c0a31329SThomas Gleixner extern int hrtimer_cancel(struct hrtimer *timer);
363c0a31329SThomas Gleixner extern int hrtimer_try_to_cancel(struct hrtimer *timer);
364c0a31329SThomas Gleixner 
36563ca243bSArjan van de Ven static inline int hrtimer_start_expires(struct hrtimer *timer,
36663ca243bSArjan van de Ven 						enum hrtimer_mode mode)
36763ca243bSArjan van de Ven {
368da8f2e17SArjan van de Ven 	unsigned long delta;
369da8f2e17SArjan van de Ven 	ktime_t soft, hard;
370da8f2e17SArjan van de Ven 	soft = hrtimer_get_softexpires(timer);
371da8f2e17SArjan van de Ven 	hard = hrtimer_get_expires(timer);
372da8f2e17SArjan van de Ven 	delta = ktime_to_ns(ktime_sub(hard, soft));
3734ce105d3SArjan van de Ven 	return hrtimer_start_range_ns(timer, soft, delta, mode);
37463ca243bSArjan van de Ven }
37563ca243bSArjan van de Ven 
376c9cb2e3dSThomas Gleixner static inline int hrtimer_restart(struct hrtimer *timer)
377c9cb2e3dSThomas Gleixner {
378654c8e0bSArjan van de Ven 	return hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
379c9cb2e3dSThomas Gleixner }
380c0a31329SThomas Gleixner 
381c0a31329SThomas Gleixner /* Query timers: */
382c0a31329SThomas Gleixner extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
383c0a31329SThomas Gleixner extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
384c0a31329SThomas Gleixner 
38569239749STony Lindgren extern ktime_t hrtimer_get_next_event(void);
38669239749STony Lindgren 
387303e967fSThomas Gleixner /*
388303e967fSThomas Gleixner  * A timer is active, when it is enqueued into the rbtree or the callback
389303e967fSThomas Gleixner  * function is running.
390303e967fSThomas Gleixner  */
391c0a31329SThomas Gleixner static inline int hrtimer_active(const struct hrtimer *timer)
392c0a31329SThomas Gleixner {
393303e967fSThomas Gleixner 	return timer->state != HRTIMER_STATE_INACTIVE;
394c0a31329SThomas Gleixner }
395c0a31329SThomas Gleixner 
39654cdfdb4SThomas Gleixner /*
39754cdfdb4SThomas Gleixner  * Helper function to check, whether the timer is on one of the queues
39854cdfdb4SThomas Gleixner  */
39954cdfdb4SThomas Gleixner static inline int hrtimer_is_queued(struct hrtimer *timer)
40054cdfdb4SThomas Gleixner {
40154cdfdb4SThomas Gleixner 	return timer->state &
40254cdfdb4SThomas Gleixner 		(HRTIMER_STATE_ENQUEUED | HRTIMER_STATE_PENDING);
40354cdfdb4SThomas Gleixner }
40454cdfdb4SThomas Gleixner 
4054346f654SOliver Hartkopp /*
4064346f654SOliver Hartkopp  * Helper function to check, whether the timer is running the callback
4074346f654SOliver Hartkopp  * function
4084346f654SOliver Hartkopp  */
4094346f654SOliver Hartkopp static inline int hrtimer_callback_running(struct hrtimer *timer)
4104346f654SOliver Hartkopp {
4114346f654SOliver Hartkopp 	return timer->state & HRTIMER_STATE_CALLBACK;
4124346f654SOliver Hartkopp }
4134346f654SOliver Hartkopp 
414c0a31329SThomas Gleixner /* Forward a hrtimer so it expires after now: */
4154d672e7aSDavide Libenzi extern u64
41644f21475SRoman Zippel hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
417c0a31329SThomas Gleixner 
4185e05ad7dSDavide Libenzi /* Forward a hrtimer so it expires after the hrtimer's current now */
4194d672e7aSDavide Libenzi static inline u64 hrtimer_forward_now(struct hrtimer *timer,
4205e05ad7dSDavide Libenzi 				      ktime_t interval)
4215e05ad7dSDavide Libenzi {
4225e05ad7dSDavide Libenzi 	return hrtimer_forward(timer, timer->base->get_time(), interval);
4235e05ad7dSDavide Libenzi }
4245e05ad7dSDavide Libenzi 
42510c94ec1SThomas Gleixner /* Precise sleep: */
42610c94ec1SThomas Gleixner extern long hrtimer_nanosleep(struct timespec *rqtp,
427080344b9SOleg Nesterov 			      struct timespec __user *rmtp,
42810c94ec1SThomas Gleixner 			      const enum hrtimer_mode mode,
42910c94ec1SThomas Gleixner 			      const clockid_t clockid);
4301711ef38SToyo Abe extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
43110c94ec1SThomas Gleixner 
43200362e33SThomas Gleixner extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
43300362e33SThomas Gleixner 				 struct task_struct *tsk);
43400362e33SThomas Gleixner 
435654c8e0bSArjan van de Ven extern int schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
436654c8e0bSArjan van de Ven 						const enum hrtimer_mode mode);
4377bb67439SArjan van de Ven extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
4387bb67439SArjan van de Ven 
439c0a31329SThomas Gleixner /* Soft interrupt function to run the hrtimer queues: */
440c0a31329SThomas Gleixner extern void hrtimer_run_queues(void);
441d3d74453SPeter Zijlstra extern void hrtimer_run_pending(void);
442c0a31329SThomas Gleixner 
443c0a31329SThomas Gleixner /* Bootup initialization: */
444c0a31329SThomas Gleixner extern void __init hrtimers_init(void);
445c0a31329SThomas Gleixner 
44679bf2bb3SThomas Gleixner #if BITS_PER_LONG < 64
4474d672e7aSDavide Libenzi extern u64 ktime_divns(const ktime_t kt, s64 div);
44879bf2bb3SThomas Gleixner #else /* BITS_PER_LONG < 64 */
4494d672e7aSDavide Libenzi # define ktime_divns(kt, div)		(u64)((kt).tv64 / (div))
45079bf2bb3SThomas Gleixner #endif
45179bf2bb3SThomas Gleixner 
45288ad0bf6SIngo Molnar /* Show pending timers: */
45388ad0bf6SIngo Molnar extern void sysrq_timer_list_show(void);
45488ad0bf6SIngo Molnar 
45582f67cd9SIngo Molnar /*
45682f67cd9SIngo Molnar  * Timer-statistics info:
45782f67cd9SIngo Molnar  */
45882f67cd9SIngo Molnar #ifdef CONFIG_TIMER_STATS
45982f67cd9SIngo Molnar 
46082f67cd9SIngo Molnar extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
461c5c061b8SVenki Pallipadi 				     void *timerf, char *comm,
462c5c061b8SVenki Pallipadi 				     unsigned int timer_flag);
46382f67cd9SIngo Molnar 
46482f67cd9SIngo Molnar static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
46582f67cd9SIngo Molnar {
46682f67cd9SIngo Molnar 	timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
467c5c061b8SVenki Pallipadi 				 timer->function, timer->start_comm, 0);
46882f67cd9SIngo Molnar }
46982f67cd9SIngo Molnar 
47082f67cd9SIngo Molnar extern void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer,
47182f67cd9SIngo Molnar 						 void *addr);
47282f67cd9SIngo Molnar 
47382f67cd9SIngo Molnar static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
47482f67cd9SIngo Molnar {
47582f67cd9SIngo Molnar 	__timer_stats_hrtimer_set_start_info(timer, __builtin_return_address(0));
47682f67cd9SIngo Molnar }
47782f67cd9SIngo Molnar 
47882f67cd9SIngo Molnar static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
47982f67cd9SIngo Molnar {
48082f67cd9SIngo Molnar 	timer->start_site = NULL;
48182f67cd9SIngo Molnar }
48282f67cd9SIngo Molnar #else
48382f67cd9SIngo Molnar static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
48482f67cd9SIngo Molnar {
48582f67cd9SIngo Molnar }
48682f67cd9SIngo Molnar 
48782f67cd9SIngo Molnar static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
48882f67cd9SIngo Molnar {
48982f67cd9SIngo Molnar }
49082f67cd9SIngo Molnar 
49182f67cd9SIngo Molnar static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
49282f67cd9SIngo Molnar {
49382f67cd9SIngo Molnar }
49482f67cd9SIngo Molnar #endif
49582f67cd9SIngo Molnar 
496c0a31329SThomas Gleixner #endif
497