xref: /linux-6.15/include/linux/hrtimer.h (revision 07a9a7ea)
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>
2240b86062SStephen Rothwell #include <linux/percpu.h>
23507e1231SHeiko Carstens #include <linux/timer.h>
24998adc3dSJohn Stultz #include <linux/timerqueue.h>
25c0a31329SThomas Gleixner 
263c8aa39dSThomas Gleixner struct hrtimer_clock_base;
273c8aa39dSThomas Gleixner struct hrtimer_cpu_base;
283c8aa39dSThomas Gleixner 
29c0a31329SThomas Gleixner /*
30c0a31329SThomas Gleixner  * Mode arguments of xxx_hrtimer functions:
3119b51cb5SAnna-Maria Gleixner  *
3219b51cb5SAnna-Maria Gleixner  * HRTIMER_MODE_ABS		- Time value is absolute
3319b51cb5SAnna-Maria Gleixner  * HRTIMER_MODE_REL		- Time value is relative to now
3419b51cb5SAnna-Maria Gleixner  * HRTIMER_MODE_PINNED		- Timer is bound to CPU (is only considered
3519b51cb5SAnna-Maria Gleixner  *				  when starting the timer)
36c0a31329SThomas Gleixner  */
37c0a31329SThomas Gleixner enum hrtimer_mode {
3819b51cb5SAnna-Maria Gleixner 	HRTIMER_MODE_ABS	= 0x00,
3919b51cb5SAnna-Maria Gleixner 	HRTIMER_MODE_REL	= 0x01,
4019b51cb5SAnna-Maria Gleixner 	HRTIMER_MODE_PINNED	= 0x02,
4119b51cb5SAnna-Maria Gleixner 
4219b51cb5SAnna-Maria Gleixner 	HRTIMER_MODE_ABS_PINNED = HRTIMER_MODE_ABS | HRTIMER_MODE_PINNED,
4319b51cb5SAnna-Maria Gleixner 	HRTIMER_MODE_REL_PINNED = HRTIMER_MODE_REL | HRTIMER_MODE_PINNED,
44c0a31329SThomas Gleixner };
45c0a31329SThomas Gleixner 
46c9cb2e3dSThomas Gleixner /*
47c9cb2e3dSThomas Gleixner  * Return values for the callback function
48c9cb2e3dSThomas Gleixner  */
49c0a31329SThomas Gleixner enum hrtimer_restart {
50c9cb2e3dSThomas Gleixner 	HRTIMER_NORESTART,	/* Timer is not restarted */
51c9cb2e3dSThomas Gleixner 	HRTIMER_RESTART,	/* Timer must be restarted */
52c0a31329SThomas Gleixner };
53c0a31329SThomas Gleixner 
54303e967fSThomas Gleixner /*
5554cdfdb4SThomas Gleixner  * Values to track state of the timer
56303e967fSThomas Gleixner  *
57303e967fSThomas Gleixner  * Possible states:
58303e967fSThomas Gleixner  *
59303e967fSThomas Gleixner  * 0x00		inactive
60303e967fSThomas Gleixner  * 0x01		enqueued into rbtree
6154cdfdb4SThomas Gleixner  *
62887d9dc9SPeter Zijlstra  * The callback state is not part of the timer->state because clearing it would
63887d9dc9SPeter Zijlstra  * mean touching the timer after the callback, this makes it impossible to free
64887d9dc9SPeter Zijlstra  * the timer from the callback function.
6553370d2eSThomas Gleixner  *
66887d9dc9SPeter Zijlstra  * Therefore we track the callback state in:
67887d9dc9SPeter Zijlstra  *
68887d9dc9SPeter Zijlstra  *	timer->base->cpu_base->running == timer
69887d9dc9SPeter Zijlstra  *
70887d9dc9SPeter Zijlstra  * On SMP it is possible to have a "callback function running and enqueued"
71887d9dc9SPeter Zijlstra  * status. It happens for example when a posix timer expired and the callback
72303e967fSThomas Gleixner  * queued a signal. Between dropping the lock which protects the posix timer
73303e967fSThomas Gleixner  * and reacquiring the base lock of the hrtimer, another CPU can deliver the
74887d9dc9SPeter Zijlstra  * signal and rearm the timer.
75303e967fSThomas Gleixner  *
76303e967fSThomas Gleixner  * All state transitions are protected by cpu_base->lock.
77303e967fSThomas Gleixner  */
78303e967fSThomas Gleixner #define HRTIMER_STATE_INACTIVE	0x00
79303e967fSThomas Gleixner #define HRTIMER_STATE_ENQUEUED	0x01
80303e967fSThomas Gleixner 
81c0a31329SThomas Gleixner /**
82c0a31329SThomas Gleixner  * struct hrtimer - the basic hrtimer structure
83998adc3dSJohn Stultz  * @node:	timerqueue node, which also manages node.expires,
84998adc3dSJohn Stultz  *		the absolute expiry time in the hrtimers internal
85c0a31329SThomas Gleixner  *		representation. The time is related to the clock on
86592aa999SThomas Gleixner  *		which the timer is based. Is setup by adding
87592aa999SThomas Gleixner  *		slack to the _softexpires value. For non range timers
88592aa999SThomas Gleixner  *		identical to _softexpires.
89592aa999SThomas Gleixner  * @_softexpires: the absolute earliest expiry time of the hrtimer.
90592aa999SThomas Gleixner  *		The time which was given as expiry time when the timer
91592aa999SThomas Gleixner  *		was armed.
92c0a31329SThomas Gleixner  * @function:	timer expiry callback function
93c0a31329SThomas Gleixner  * @base:	pointer to the timer base (per cpu and per clock)
94303e967fSThomas Gleixner  * @state:	state information (See bit values above)
95203cbf77SThomas Gleixner  * @is_rel:	Set if the timer was armed relative
96c0a31329SThomas Gleixner  *
9754cdfdb4SThomas Gleixner  * The hrtimer structure must be initialized by hrtimer_init()
98c0a31329SThomas Gleixner  */
99c0a31329SThomas Gleixner struct hrtimer {
100998adc3dSJohn Stultz 	struct timerqueue_node		node;
101654c8e0bSArjan van de Ven 	ktime_t				_softexpires;
102c9cb2e3dSThomas Gleixner 	enum hrtimer_restart		(*function)(struct hrtimer *);
1033c8aa39dSThomas Gleixner 	struct hrtimer_clock_base	*base;
104203cbf77SThomas Gleixner 	u8				state;
105203cbf77SThomas Gleixner 	u8				is_rel;
106c0a31329SThomas Gleixner };
107c0a31329SThomas Gleixner 
108c0a31329SThomas Gleixner /**
10900362e33SThomas Gleixner  * struct hrtimer_sleeper - simple sleeper structure
11000362e33SThomas Gleixner  * @timer:	embedded timer structure
11100362e33SThomas Gleixner  * @task:	task to wake up
11200362e33SThomas Gleixner  *
11300362e33SThomas Gleixner  * task is set to NULL, when the timer expires.
11400362e33SThomas Gleixner  */
11500362e33SThomas Gleixner struct hrtimer_sleeper {
11600362e33SThomas Gleixner 	struct hrtimer timer;
11700362e33SThomas Gleixner 	struct task_struct *task;
11800362e33SThomas Gleixner };
11900362e33SThomas Gleixner 
120b8e38413SThomas Gleixner #ifdef CONFIG_64BIT
1213f0b9e8eSAnna-Maria Gleixner # define __hrtimer_clock_base_align	____cacheline_aligned
122b8e38413SThomas Gleixner #else
1233f0b9e8eSAnna-Maria Gleixner # define __hrtimer_clock_base_align
124b8e38413SThomas Gleixner #endif
125b8e38413SThomas Gleixner 
12600362e33SThomas Gleixner /**
127d1d67174SAndres Salomon  * struct hrtimer_clock_base - the timer base for a specific clock
12805fb6bf0SRandy Dunlap  * @cpu_base:		per cpu clock base
1293c8aa39dSThomas Gleixner  * @index:		clock type index for per_cpu support when moving a
1303c8aa39dSThomas Gleixner  *			timer to a base on another cpu.
1314d258b25SVitaliy Ivanov  * @clockid:		clock id for per_cpu support
1323f0b9e8eSAnna-Maria Gleixner  * @seq:		seqcount around __run_hrtimer
1333f0b9e8eSAnna-Maria Gleixner  * @running:		pointer to the currently running hrtimer
134c0a31329SThomas Gleixner  * @active:		red black tree root node for the active timers
135c0a31329SThomas Gleixner  * @get_time:		function to retrieve the current time of the clock
13654cdfdb4SThomas Gleixner  * @offset:		offset of this clock to the monotonic base
137c0a31329SThomas Gleixner  */
1383c8aa39dSThomas Gleixner struct hrtimer_clock_base {
1393c8aa39dSThomas Gleixner 	struct hrtimer_cpu_base	*cpu_base;
1403f0b9e8eSAnna-Maria Gleixner 	unsigned int		index;
141ab8177bcSThomas Gleixner 	clockid_t		clockid;
1423f0b9e8eSAnna-Maria Gleixner 	seqcount_t		seq;
1433f0b9e8eSAnna-Maria Gleixner 	struct hrtimer		*running;
144998adc3dSJohn Stultz 	struct timerqueue_head	active;
145c0a31329SThomas Gleixner 	ktime_t			(*get_time)(void);
14654cdfdb4SThomas Gleixner 	ktime_t			offset;
1473f0b9e8eSAnna-Maria Gleixner } __hrtimer_clock_base_align;
1483c8aa39dSThomas Gleixner 
149e06383dbSJohn Stultz enum  hrtimer_base_type {
150e06383dbSJohn Stultz 	HRTIMER_BASE_MONOTONIC,
15168fa61c0SThomas Gleixner 	HRTIMER_BASE_REALTIME,
15270a08ccaSJohn Stultz 	HRTIMER_BASE_BOOTTIME,
15390adda98SJohn Stultz 	HRTIMER_BASE_TAI,
154e06383dbSJohn Stultz 	HRTIMER_MAX_CLOCK_BASES,
155e06383dbSJohn Stultz };
1563c8aa39dSThomas Gleixner 
1571fbc78b3SAnna-Maria 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
161cddd0248SViresh Kumar  * @cpu:		cpu number
162ab8177bcSThomas Gleixner  * @active_bases:	Bitfield to mark bases with active timers
163868a3e91SThomas Gleixner  * @clock_was_set_seq:	Sequence counter of clock was set events
16454cdfdb4SThomas Gleixner  * @hres_active:	State of high resolution mode
16528bfd18bSAnna-Maria Gleixner  * @in_hrtirq:		hrtimer_interrupt() is currently executing
16641d2e494SThomas Gleixner  * @hang_detected:	The last hrtimer interrupt detected a hang
1671fbc78b3SAnna-Maria Gleixner  * @next_timer:		Pointer to the first expiring timer
16841d2e494SThomas Gleixner  * @nr_events:		Total number of hrtimer interrupt events
16941d2e494SThomas Gleixner  * @nr_retries:		Total number of hrtimer interrupt retries
17041d2e494SThomas Gleixner  * @nr_hangs:		Total number of hrtimer interrupt hangs
17141d2e494SThomas Gleixner  * @max_hang_time:	Maximum time spent in hrtimer_interrupt
172*07a9a7eaSAnna-Maria Gleixner  * @expires_next:	absolute time of the next event, is required for remote
173*07a9a7eaSAnna-Maria Gleixner  *			hrtimer enqueue
174ab8177bcSThomas Gleixner  * @clock_base:		array of clock bases for this cpu
175895bdfa7SThomas Gleixner  *
176895bdfa7SThomas Gleixner  * Note: next_timer is just an optimization for __remove_hrtimer().
177895bdfa7SThomas Gleixner  *	 Do not dereference the pointer because it is not reliable on
178895bdfa7SThomas Gleixner  *	 cross cpu removals.
1793c8aa39dSThomas Gleixner  */
1803c8aa39dSThomas Gleixner struct hrtimer_cpu_base {
181ecb49d1aSThomas Gleixner 	raw_spinlock_t			lock;
182cddd0248SViresh Kumar 	unsigned int			cpu;
183f55a6faaSJohn Stultz 	unsigned int			active_bases;
184868a3e91SThomas Gleixner 	unsigned int			clock_was_set_seq;
18528bfd18bSAnna-Maria Gleixner 	unsigned int			hres_active	: 1;
18654cdfdb4SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
187e19ffe8bSThomas Gleixner 	unsigned int			in_hrtirq	: 1,
188e19ffe8bSThomas Gleixner 					hang_detected	: 1;
189895bdfa7SThomas Gleixner 	struct hrtimer			*next_timer;
190a6ffebceSThomas Gleixner 	unsigned int			nr_events;
191da21c5a5SAnna-Maria Gleixner 	unsigned short			nr_retries;
192da21c5a5SAnna-Maria Gleixner 	unsigned short			nr_hangs;
193a6ffebceSThomas Gleixner 	unsigned int			max_hang_time;
19454cdfdb4SThomas Gleixner #endif
195*07a9a7eaSAnna-Maria Gleixner 	ktime_t				expires_next;
196f24444b0SThomas Gleixner 	struct hrtimer_clock_base	clock_base[HRTIMER_MAX_CLOCK_BASES];
1976d9a1411SThomas Gleixner } ____cacheline_aligned;
198c0a31329SThomas Gleixner 
19963ca243bSArjan van de Ven static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
20063ca243bSArjan van de Ven {
201998adc3dSJohn Stultz 	timer->node.expires = time;
202654c8e0bSArjan van de Ven 	timer->_softexpires = time;
20363ca243bSArjan van de Ven }
204654c8e0bSArjan van de Ven 
205654c8e0bSArjan van de Ven static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
206654c8e0bSArjan van de Ven {
207654c8e0bSArjan van de Ven 	timer->_softexpires = time;
208998adc3dSJohn Stultz 	timer->node.expires = ktime_add_safe(time, delta);
209654c8e0bSArjan van de Ven }
210654c8e0bSArjan van de Ven 
211da8b44d5SJohn Stultz static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, u64 delta)
212654c8e0bSArjan van de Ven {
213654c8e0bSArjan van de Ven 	timer->_softexpires = time;
214998adc3dSJohn Stultz 	timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta));
215654c8e0bSArjan van de Ven }
216654c8e0bSArjan van de Ven 
21763ca243bSArjan van de Ven static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
21863ca243bSArjan van de Ven {
2192456e855SThomas Gleixner 	timer->node.expires = tv64;
2202456e855SThomas Gleixner 	timer->_softexpires = tv64;
22163ca243bSArjan van de Ven }
22263ca243bSArjan van de Ven 
22363ca243bSArjan van de Ven static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
22463ca243bSArjan van de Ven {
225998adc3dSJohn Stultz 	timer->node.expires = ktime_add_safe(timer->node.expires, time);
226654c8e0bSArjan van de Ven 	timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
22763ca243bSArjan van de Ven }
22863ca243bSArjan van de Ven 
2297597bc94SDavid Howells static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns)
23063ca243bSArjan van de Ven {
231998adc3dSJohn Stultz 	timer->node.expires = ktime_add_ns(timer->node.expires, ns);
232654c8e0bSArjan van de Ven 	timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
23363ca243bSArjan van de Ven }
23463ca243bSArjan van de Ven 
23563ca243bSArjan van de Ven static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
23663ca243bSArjan van de Ven {
237998adc3dSJohn Stultz 	return timer->node.expires;
23863ca243bSArjan van de Ven }
23963ca243bSArjan van de Ven 
240654c8e0bSArjan van de Ven static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
241654c8e0bSArjan van de Ven {
242654c8e0bSArjan van de Ven 	return timer->_softexpires;
243654c8e0bSArjan van de Ven }
244654c8e0bSArjan van de Ven 
24563ca243bSArjan van de Ven static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
24663ca243bSArjan van de Ven {
2472456e855SThomas Gleixner 	return timer->node.expires;
24863ca243bSArjan van de Ven }
249654c8e0bSArjan van de Ven static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
250654c8e0bSArjan van de Ven {
2512456e855SThomas Gleixner 	return timer->_softexpires;
252654c8e0bSArjan van de Ven }
25363ca243bSArjan van de Ven 
25463ca243bSArjan van de Ven static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
25563ca243bSArjan van de Ven {
256998adc3dSJohn Stultz 	return ktime_to_ns(timer->node.expires);
25763ca243bSArjan van de Ven }
25863ca243bSArjan van de Ven 
25963ca243bSArjan van de Ven static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
26063ca243bSArjan van de Ven {
261998adc3dSJohn Stultz 	return ktime_sub(timer->node.expires, timer->base->get_time());
26263ca243bSArjan van de Ven }
26363ca243bSArjan van de Ven 
26454cdfdb4SThomas Gleixner static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
26554cdfdb4SThomas Gleixner {
26654cdfdb4SThomas Gleixner 	return timer->base->get_time();
26754cdfdb4SThomas Gleixner }
26854cdfdb4SThomas Gleixner 
26928bfd18bSAnna-Maria Gleixner static inline int hrtimer_is_hres_active(struct hrtimer *timer)
27028bfd18bSAnna-Maria Gleixner {
27128bfd18bSAnna-Maria Gleixner 	return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ?
27228bfd18bSAnna-Maria Gleixner 		timer->base->cpu_base->hres_active : 0;
27328bfd18bSAnna-Maria Gleixner }
27428bfd18bSAnna-Maria Gleixner 
27521d6d52aSThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
27621d6d52aSThomas Gleixner struct clock_event_device;
27721d6d52aSThomas Gleixner 
27821d6d52aSThomas Gleixner extern void hrtimer_interrupt(struct clock_event_device *dev);
27921d6d52aSThomas Gleixner 
28054cdfdb4SThomas Gleixner /*
28154cdfdb4SThomas Gleixner  * The resolution of the clocks. The resolution value is returned in
28254cdfdb4SThomas Gleixner  * the clock_getres() system call to give application programmers an
28354cdfdb4SThomas Gleixner  * idea of the (in)accuracy of timers. Timer values are rounded up to
28454cdfdb4SThomas Gleixner  * this resolution values.
28554cdfdb4SThomas Gleixner  */
286151db1fcSTony Breeds # define HIGH_RES_NSEC		1
2872456e855SThomas Gleixner # define KTIME_HIGH_RES		(HIGH_RES_NSEC)
288151db1fcSTony Breeds # define MONOTONIC_RES_NSEC	HIGH_RES_NSEC
28954cdfdb4SThomas Gleixner # define KTIME_MONOTONIC_RES	KTIME_HIGH_RES
29054cdfdb4SThomas Gleixner 
291f55a6faaSJohn Stultz extern void clock_was_set_delayed(void);
292f55a6faaSJohn Stultz 
293398ca17fSThomas Gleixner extern unsigned int hrtimer_resolution;
294398ca17fSThomas Gleixner 
29554cdfdb4SThomas Gleixner #else
29654cdfdb4SThomas Gleixner 
297151db1fcSTony Breeds # define MONOTONIC_RES_NSEC	LOW_RES_NSEC
29854cdfdb4SThomas Gleixner # define KTIME_MONOTONIC_RES	KTIME_LOW_RES
29954cdfdb4SThomas Gleixner 
300d711b8b3SBorislav Petkov #define hrtimer_resolution	(unsigned int)LOW_RES_NSEC
301398ca17fSThomas Gleixner 
302f55a6faaSJohn Stultz static inline void clock_was_set_delayed(void) { }
303f55a6faaSJohn Stultz 
30454cdfdb4SThomas Gleixner #endif
30554cdfdb4SThomas Gleixner 
306203cbf77SThomas Gleixner static inline ktime_t
307203cbf77SThomas Gleixner __hrtimer_expires_remaining_adjusted(const struct hrtimer *timer, ktime_t now)
308203cbf77SThomas Gleixner {
309203cbf77SThomas Gleixner 	ktime_t rem = ktime_sub(timer->node.expires, now);
310203cbf77SThomas Gleixner 
311203cbf77SThomas Gleixner 	/*
312203cbf77SThomas Gleixner 	 * Adjust relative timers for the extra we added in
313203cbf77SThomas Gleixner 	 * hrtimer_start_range_ns() to prevent short timeouts.
314203cbf77SThomas Gleixner 	 */
315203cbf77SThomas Gleixner 	if (IS_ENABLED(CONFIG_TIME_LOW_RES) && timer->is_rel)
3162456e855SThomas Gleixner 		rem -= hrtimer_resolution;
317203cbf77SThomas Gleixner 	return rem;
318203cbf77SThomas Gleixner }
319203cbf77SThomas Gleixner 
320203cbf77SThomas Gleixner static inline ktime_t
321203cbf77SThomas Gleixner hrtimer_expires_remaining_adjusted(const struct hrtimer *timer)
322203cbf77SThomas Gleixner {
323203cbf77SThomas Gleixner 	return __hrtimer_expires_remaining_adjusted(timer,
324203cbf77SThomas Gleixner 						    timer->base->get_time());
325203cbf77SThomas Gleixner }
326203cbf77SThomas Gleixner 
327b12a03ceSThomas Gleixner extern void clock_was_set(void);
3289ec26907SThomas Gleixner #ifdef CONFIG_TIMERFD
3299ec26907SThomas Gleixner extern void timerfd_clock_was_set(void);
3309ec26907SThomas Gleixner #else
3319ec26907SThomas Gleixner static inline void timerfd_clock_was_set(void) { }
3329ec26907SThomas Gleixner #endif
333b12a03ceSThomas Gleixner extern void hrtimers_resume(void);
334b12a03ceSThomas Gleixner 
3352e94d1f7SArjan van de Ven DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
3362e94d1f7SArjan van de Ven 
3372e94d1f7SArjan van de Ven 
338c0a31329SThomas Gleixner /* Exported timer functions: */
339c0a31329SThomas Gleixner 
340c0a31329SThomas Gleixner /* Initialize timers: */
3417978672cSGeorge Anzinger extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
3427978672cSGeorge Anzinger 			 enum hrtimer_mode mode);
343c0a31329SThomas Gleixner 
344237fc6e7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
345237fc6e7SThomas Gleixner extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
346237fc6e7SThomas Gleixner 				  enum hrtimer_mode mode);
347237fc6e7SThomas Gleixner 
348237fc6e7SThomas Gleixner extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
349237fc6e7SThomas Gleixner #else
350237fc6e7SThomas Gleixner static inline void hrtimer_init_on_stack(struct hrtimer *timer,
351237fc6e7SThomas Gleixner 					 clockid_t which_clock,
352237fc6e7SThomas Gleixner 					 enum hrtimer_mode mode)
353237fc6e7SThomas Gleixner {
354237fc6e7SThomas Gleixner 	hrtimer_init(timer, which_clock, mode);
355237fc6e7SThomas Gleixner }
356237fc6e7SThomas Gleixner static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
357237fc6e7SThomas Gleixner #endif
358237fc6e7SThomas Gleixner 
359c0a31329SThomas Gleixner /* Basic timer operations: */
36061699e13SThomas Gleixner extern void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
361da8b44d5SJohn Stultz 				   u64 range_ns, const enum hrtimer_mode mode);
3627f1e2ca9SPeter Zijlstra 
36302a171afSThomas Gleixner /**
3646de6250cSAnna-Maria Gleixner  * hrtimer_start - (re)start an hrtimer
36502a171afSThomas Gleixner  * @timer:	the timer to be added
36602a171afSThomas Gleixner  * @tim:	expiry time
3676de6250cSAnna-Maria Gleixner  * @mode:	timer mode: absolute (HRTIMER_MODE_ABS) or
3686de6250cSAnna-Maria Gleixner  *		relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED)
36902a171afSThomas Gleixner  */
37061699e13SThomas Gleixner static inline void hrtimer_start(struct hrtimer *timer, ktime_t tim,
37102a171afSThomas Gleixner 				 const enum hrtimer_mode mode)
37202a171afSThomas Gleixner {
37361699e13SThomas Gleixner 	hrtimer_start_range_ns(timer, tim, 0, mode);
37402a171afSThomas Gleixner }
37502a171afSThomas Gleixner 
376c0a31329SThomas Gleixner extern int hrtimer_cancel(struct hrtimer *timer);
377c0a31329SThomas Gleixner extern int hrtimer_try_to_cancel(struct hrtimer *timer);
378c0a31329SThomas Gleixner 
37961699e13SThomas Gleixner static inline void hrtimer_start_expires(struct hrtimer *timer,
38063ca243bSArjan van de Ven 					 enum hrtimer_mode mode)
38163ca243bSArjan van de Ven {
382da8b44d5SJohn Stultz 	u64 delta;
383da8f2e17SArjan van de Ven 	ktime_t soft, hard;
384da8f2e17SArjan van de Ven 	soft = hrtimer_get_softexpires(timer);
385da8f2e17SArjan van de Ven 	hard = hrtimer_get_expires(timer);
386da8f2e17SArjan van de Ven 	delta = ktime_to_ns(ktime_sub(hard, soft));
38761699e13SThomas Gleixner 	hrtimer_start_range_ns(timer, soft, delta, mode);
38863ca243bSArjan van de Ven }
38963ca243bSArjan van de Ven 
39061699e13SThomas Gleixner static inline void hrtimer_restart(struct hrtimer *timer)
391c9cb2e3dSThomas Gleixner {
39261699e13SThomas Gleixner 	hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
393c9cb2e3dSThomas Gleixner }
394c0a31329SThomas Gleixner 
395c0a31329SThomas Gleixner /* Query timers: */
396203cbf77SThomas Gleixner extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
397203cbf77SThomas Gleixner 
398203cbf77SThomas Gleixner static inline ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
399203cbf77SThomas Gleixner {
400203cbf77SThomas Gleixner 	return __hrtimer_get_remaining(timer, false);
401203cbf77SThomas Gleixner }
402c0a31329SThomas Gleixner 
403c1ad348bSThomas Gleixner extern u64 hrtimer_get_next_event(void);
40469239749STony Lindgren 
405887d9dc9SPeter Zijlstra extern bool hrtimer_active(const struct hrtimer *timer);
406c0a31329SThomas Gleixner 
40754cdfdb4SThomas Gleixner /*
40854cdfdb4SThomas Gleixner  * Helper function to check, whether the timer is on one of the queues
40954cdfdb4SThomas Gleixner  */
41054cdfdb4SThomas Gleixner static inline int hrtimer_is_queued(struct hrtimer *timer)
41154cdfdb4SThomas Gleixner {
412ca109491SPeter Zijlstra 	return timer->state & HRTIMER_STATE_ENQUEUED;
41354cdfdb4SThomas Gleixner }
41454cdfdb4SThomas Gleixner 
4154346f654SOliver Hartkopp /*
4164346f654SOliver Hartkopp  * Helper function to check, whether the timer is running the callback
4174346f654SOliver Hartkopp  * function
4184346f654SOliver Hartkopp  */
4194346f654SOliver Hartkopp static inline int hrtimer_callback_running(struct hrtimer *timer)
4204346f654SOliver Hartkopp {
4213f0b9e8eSAnna-Maria Gleixner 	return timer->base->running == timer;
4224346f654SOliver Hartkopp }
4234346f654SOliver Hartkopp 
424c0a31329SThomas Gleixner /* Forward a hrtimer so it expires after now: */
4254d672e7aSDavide Libenzi extern u64
42644f21475SRoman Zippel hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
427c0a31329SThomas Gleixner 
42891e5a217SThomas Gleixner /**
42991e5a217SThomas Gleixner  * hrtimer_forward_now - forward the timer expiry so it expires after now
43091e5a217SThomas Gleixner  * @timer:	hrtimer to forward
43191e5a217SThomas Gleixner  * @interval:	the interval to forward
43291e5a217SThomas Gleixner  *
43391e5a217SThomas Gleixner  * Forward the timer expiry so it will expire after the current time
43491e5a217SThomas Gleixner  * of the hrtimer clock base. Returns the number of overruns.
43591e5a217SThomas Gleixner  *
43691e5a217SThomas Gleixner  * Can be safely called from the callback function of @timer. If
43791e5a217SThomas Gleixner  * called from other contexts @timer must neither be enqueued nor
43891e5a217SThomas Gleixner  * running the callback and the caller needs to take care of
43991e5a217SThomas Gleixner  * serialization.
44091e5a217SThomas Gleixner  *
44191e5a217SThomas Gleixner  * Note: This only updates the timer expiry value and does not requeue
44291e5a217SThomas Gleixner  * the timer.
44391e5a217SThomas Gleixner  */
4444d672e7aSDavide Libenzi static inline u64 hrtimer_forward_now(struct hrtimer *timer,
4455e05ad7dSDavide Libenzi 				      ktime_t interval)
4465e05ad7dSDavide Libenzi {
4475e05ad7dSDavide Libenzi 	return hrtimer_forward(timer, timer->base->get_time(), interval);
4485e05ad7dSDavide Libenzi }
4495e05ad7dSDavide Libenzi 
45010c94ec1SThomas Gleixner /* Precise sleep: */
451ce41aaf4SAl Viro 
452c0edd7c9SDeepa Dinamani extern int nanosleep_copyout(struct restart_block *, struct timespec64 *);
453938e7cf2SThomas Gleixner extern long hrtimer_nanosleep(const struct timespec64 *rqtp,
45410c94ec1SThomas Gleixner 			      const enum hrtimer_mode mode,
45510c94ec1SThomas Gleixner 			      const clockid_t clockid);
45610c94ec1SThomas Gleixner 
45700362e33SThomas Gleixner extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
45800362e33SThomas Gleixner 				 struct task_struct *tsk);
45900362e33SThomas Gleixner 
460da8b44d5SJohn Stultz extern int schedule_hrtimeout_range(ktime_t *expires, u64 delta,
461654c8e0bSArjan van de Ven 						const enum hrtimer_mode mode);
462351b3f7aSCarsten Emde extern int schedule_hrtimeout_range_clock(ktime_t *expires,
463da8b44d5SJohn Stultz 					  u64 delta,
464da8b44d5SJohn Stultz 					  const enum hrtimer_mode mode,
46590777713SAnna-Maria Gleixner 					  clockid_t clock_id);
4667bb67439SArjan van de Ven extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
4677bb67439SArjan van de Ven 
468c0a31329SThomas Gleixner /* Soft interrupt function to run the hrtimer queues: */
469c0a31329SThomas Gleixner extern void hrtimer_run_queues(void);
470c0a31329SThomas Gleixner 
471c0a31329SThomas Gleixner /* Bootup initialization: */
472c0a31329SThomas Gleixner extern void __init hrtimers_init(void);
473c0a31329SThomas Gleixner 
47488ad0bf6SIngo Molnar /* Show pending timers: */
47588ad0bf6SIngo Molnar extern void sysrq_timer_list_show(void);
47688ad0bf6SIngo Molnar 
47727590dc1SThomas Gleixner int hrtimers_prepare_cpu(unsigned int cpu);
47827590dc1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
47927590dc1SThomas Gleixner int hrtimers_dead_cpu(unsigned int cpu);
48027590dc1SThomas Gleixner #else
48127590dc1SThomas Gleixner #define hrtimers_dead_cpu	NULL
48227590dc1SThomas Gleixner #endif
48327590dc1SThomas Gleixner 
484c0a31329SThomas Gleixner #endif
485