xref: /linux-6.15/include/linux/hrtimer.h (revision 0cd39f46)
135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
2c0a31329SThomas Gleixner /*
3c0a31329SThomas Gleixner  *  hrtimers - High-resolution kernel timers
4c0a31329SThomas Gleixner  *
5c0a31329SThomas Gleixner  *   Copyright(C) 2005, Thomas Gleixner <[email protected]>
6c0a31329SThomas Gleixner  *   Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
7c0a31329SThomas Gleixner  *
8c0a31329SThomas Gleixner  *  data type definitions, declarations, prototypes
9c0a31329SThomas Gleixner  *
10c0a31329SThomas Gleixner  *  Started by: Thomas Gleixner and Ingo Molnar
11c0a31329SThomas Gleixner  */
12c0a31329SThomas Gleixner #ifndef _LINUX_HRTIMER_H
13c0a31329SThomas Gleixner #define _LINUX_HRTIMER_H
14c0a31329SThomas Gleixner 
1532e29396SVincenzo Frascino #include <linux/hrtimer_defs.h>
16c0a31329SThomas Gleixner #include <linux/rbtree.h>
17c0a31329SThomas Gleixner #include <linux/init.h>
18c0a31329SThomas Gleixner #include <linux/list.h>
1940b86062SStephen Rothwell #include <linux/percpu.h>
20*0cd39f46SPeter Zijlstra #include <linux/seqlock.h>
21507e1231SHeiko Carstens #include <linux/timer.h>
22998adc3dSJohn Stultz #include <linux/timerqueue.h>
23c0a31329SThomas Gleixner 
243c8aa39dSThomas Gleixner struct hrtimer_clock_base;
253c8aa39dSThomas Gleixner struct hrtimer_cpu_base;
263c8aa39dSThomas Gleixner 
27c0a31329SThomas Gleixner /*
28c0a31329SThomas Gleixner  * Mode arguments of xxx_hrtimer functions:
2919b51cb5SAnna-Maria Gleixner  *
3019b51cb5SAnna-Maria Gleixner  * HRTIMER_MODE_ABS		- Time value is absolute
3119b51cb5SAnna-Maria Gleixner  * HRTIMER_MODE_REL		- Time value is relative to now
3219b51cb5SAnna-Maria Gleixner  * HRTIMER_MODE_PINNED		- Timer is bound to CPU (is only considered
3319b51cb5SAnna-Maria Gleixner  *				  when starting the timer)
3498ecadd4SAnna-Maria Gleixner  * HRTIMER_MODE_SOFT		- Timer callback function will be executed in
3598ecadd4SAnna-Maria Gleixner  *				  soft irq context
36a67e4082SSebastian Andrzej Siewior  * HRTIMER_MODE_HARD		- Timer callback function will be executed in
37a67e4082SSebastian Andrzej Siewior  *				  hard irq context even on PREEMPT_RT.
38c0a31329SThomas Gleixner  */
39c0a31329SThomas Gleixner enum hrtimer_mode {
4019b51cb5SAnna-Maria Gleixner 	HRTIMER_MODE_ABS	= 0x00,
4119b51cb5SAnna-Maria Gleixner 	HRTIMER_MODE_REL	= 0x01,
4219b51cb5SAnna-Maria Gleixner 	HRTIMER_MODE_PINNED	= 0x02,
4398ecadd4SAnna-Maria Gleixner 	HRTIMER_MODE_SOFT	= 0x04,
44ae6683d8SSebastian Andrzej Siewior 	HRTIMER_MODE_HARD	= 0x08,
4519b51cb5SAnna-Maria Gleixner 
4619b51cb5SAnna-Maria Gleixner 	HRTIMER_MODE_ABS_PINNED = HRTIMER_MODE_ABS | HRTIMER_MODE_PINNED,
4719b51cb5SAnna-Maria Gleixner 	HRTIMER_MODE_REL_PINNED = HRTIMER_MODE_REL | HRTIMER_MODE_PINNED,
4898ecadd4SAnna-Maria Gleixner 
4998ecadd4SAnna-Maria Gleixner 	HRTIMER_MODE_ABS_SOFT	= HRTIMER_MODE_ABS | HRTIMER_MODE_SOFT,
5098ecadd4SAnna-Maria Gleixner 	HRTIMER_MODE_REL_SOFT	= HRTIMER_MODE_REL | HRTIMER_MODE_SOFT,
5198ecadd4SAnna-Maria Gleixner 
5298ecadd4SAnna-Maria Gleixner 	HRTIMER_MODE_ABS_PINNED_SOFT = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_SOFT,
5398ecadd4SAnna-Maria Gleixner 	HRTIMER_MODE_REL_PINNED_SOFT = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_SOFT,
5498ecadd4SAnna-Maria Gleixner 
55ae6683d8SSebastian Andrzej Siewior 	HRTIMER_MODE_ABS_HARD	= HRTIMER_MODE_ABS | HRTIMER_MODE_HARD,
56ae6683d8SSebastian Andrzej Siewior 	HRTIMER_MODE_REL_HARD	= HRTIMER_MODE_REL | HRTIMER_MODE_HARD,
57ae6683d8SSebastian Andrzej Siewior 
58ae6683d8SSebastian Andrzej Siewior 	HRTIMER_MODE_ABS_PINNED_HARD = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_HARD,
59ae6683d8SSebastian Andrzej Siewior 	HRTIMER_MODE_REL_PINNED_HARD = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_HARD,
60c0a31329SThomas Gleixner };
61c0a31329SThomas Gleixner 
62c9cb2e3dSThomas Gleixner /*
63c9cb2e3dSThomas Gleixner  * Return values for the callback function
64c9cb2e3dSThomas Gleixner  */
65c0a31329SThomas Gleixner enum hrtimer_restart {
66c9cb2e3dSThomas Gleixner 	HRTIMER_NORESTART,	/* Timer is not restarted */
67c9cb2e3dSThomas Gleixner 	HRTIMER_RESTART,	/* Timer must be restarted */
68c0a31329SThomas Gleixner };
69c0a31329SThomas Gleixner 
70303e967fSThomas Gleixner /*
7154cdfdb4SThomas Gleixner  * Values to track state of the timer
72303e967fSThomas Gleixner  *
73303e967fSThomas Gleixner  * Possible states:
74303e967fSThomas Gleixner  *
75303e967fSThomas Gleixner  * 0x00		inactive
76303e967fSThomas Gleixner  * 0x01		enqueued into rbtree
7754cdfdb4SThomas Gleixner  *
78887d9dc9SPeter Zijlstra  * The callback state is not part of the timer->state because clearing it would
79887d9dc9SPeter Zijlstra  * mean touching the timer after the callback, this makes it impossible to free
80887d9dc9SPeter Zijlstra  * the timer from the callback function.
8153370d2eSThomas Gleixner  *
82887d9dc9SPeter Zijlstra  * Therefore we track the callback state in:
83887d9dc9SPeter Zijlstra  *
84887d9dc9SPeter Zijlstra  *	timer->base->cpu_base->running == timer
85887d9dc9SPeter Zijlstra  *
86887d9dc9SPeter Zijlstra  * On SMP it is possible to have a "callback function running and enqueued"
87887d9dc9SPeter Zijlstra  * status. It happens for example when a posix timer expired and the callback
88303e967fSThomas Gleixner  * queued a signal. Between dropping the lock which protects the posix timer
89303e967fSThomas Gleixner  * and reacquiring the base lock of the hrtimer, another CPU can deliver the
90887d9dc9SPeter Zijlstra  * signal and rearm the timer.
91303e967fSThomas Gleixner  *
92303e967fSThomas Gleixner  * All state transitions are protected by cpu_base->lock.
93303e967fSThomas Gleixner  */
94303e967fSThomas Gleixner #define HRTIMER_STATE_INACTIVE	0x00
95303e967fSThomas Gleixner #define HRTIMER_STATE_ENQUEUED	0x01
96303e967fSThomas Gleixner 
97c0a31329SThomas Gleixner /**
98c0a31329SThomas Gleixner  * struct hrtimer - the basic hrtimer structure
99998adc3dSJohn Stultz  * @node:	timerqueue node, which also manages node.expires,
100998adc3dSJohn Stultz  *		the absolute expiry time in the hrtimers internal
101c0a31329SThomas Gleixner  *		representation. The time is related to the clock on
102592aa999SThomas Gleixner  *		which the timer is based. Is setup by adding
103592aa999SThomas Gleixner  *		slack to the _softexpires value. For non range timers
104592aa999SThomas Gleixner  *		identical to _softexpires.
105592aa999SThomas Gleixner  * @_softexpires: the absolute earliest expiry time of the hrtimer.
106592aa999SThomas Gleixner  *		The time which was given as expiry time when the timer
107592aa999SThomas Gleixner  *		was armed.
108c0a31329SThomas Gleixner  * @function:	timer expiry callback function
109c0a31329SThomas Gleixner  * @base:	pointer to the timer base (per cpu and per clock)
110303e967fSThomas Gleixner  * @state:	state information (See bit values above)
111203cbf77SThomas Gleixner  * @is_rel:	Set if the timer was armed relative
1125da70160SAnna-Maria Gleixner  * @is_soft:	Set if hrtimer will be expired in soft interrupt context.
1130ab6a3ddSThomas Gleixner  * @is_hard:	Set if hrtimer will be expired in hard interrupt context
1140ab6a3ddSThomas Gleixner  *		even on RT.
115c0a31329SThomas Gleixner  *
11654cdfdb4SThomas Gleixner  * The hrtimer structure must be initialized by hrtimer_init()
117c0a31329SThomas Gleixner  */
118c0a31329SThomas Gleixner struct hrtimer {
119998adc3dSJohn Stultz 	struct timerqueue_node		node;
120654c8e0bSArjan van de Ven 	ktime_t				_softexpires;
121c9cb2e3dSThomas Gleixner 	enum hrtimer_restart		(*function)(struct hrtimer *);
1223c8aa39dSThomas Gleixner 	struct hrtimer_clock_base	*base;
123203cbf77SThomas Gleixner 	u8				state;
124203cbf77SThomas Gleixner 	u8				is_rel;
1255da70160SAnna-Maria Gleixner 	u8				is_soft;
1260ab6a3ddSThomas Gleixner 	u8				is_hard;
127c0a31329SThomas Gleixner };
128c0a31329SThomas Gleixner 
129c0a31329SThomas Gleixner /**
13000362e33SThomas Gleixner  * struct hrtimer_sleeper - simple sleeper structure
13100362e33SThomas Gleixner  * @timer:	embedded timer structure
13200362e33SThomas Gleixner  * @task:	task to wake up
13300362e33SThomas Gleixner  *
13400362e33SThomas Gleixner  * task is set to NULL, when the timer expires.
13500362e33SThomas Gleixner  */
13600362e33SThomas Gleixner struct hrtimer_sleeper {
13700362e33SThomas Gleixner 	struct hrtimer timer;
13800362e33SThomas Gleixner 	struct task_struct *task;
13900362e33SThomas Gleixner };
14000362e33SThomas Gleixner 
141b8e38413SThomas Gleixner #ifdef CONFIG_64BIT
1423f0b9e8eSAnna-Maria Gleixner # define __hrtimer_clock_base_align	____cacheline_aligned
143b8e38413SThomas Gleixner #else
1443f0b9e8eSAnna-Maria Gleixner # define __hrtimer_clock_base_align
145b8e38413SThomas Gleixner #endif
146b8e38413SThomas Gleixner 
14700362e33SThomas Gleixner /**
148d1d67174SAndres Salomon  * struct hrtimer_clock_base - the timer base for a specific clock
14905fb6bf0SRandy Dunlap  * @cpu_base:		per cpu clock base
1503c8aa39dSThomas Gleixner  * @index:		clock type index for per_cpu support when moving a
1513c8aa39dSThomas Gleixner  *			timer to a base on another cpu.
1524d258b25SVitaliy Ivanov  * @clockid:		clock id for per_cpu support
1533f0b9e8eSAnna-Maria Gleixner  * @seq:		seqcount around __run_hrtimer
1543f0b9e8eSAnna-Maria Gleixner  * @running:		pointer to the currently running hrtimer
155c0a31329SThomas Gleixner  * @active:		red black tree root node for the active timers
156c0a31329SThomas Gleixner  * @get_time:		function to retrieve the current time of the clock
15754cdfdb4SThomas Gleixner  * @offset:		offset of this clock to the monotonic base
158c0a31329SThomas Gleixner  */
1593c8aa39dSThomas Gleixner struct hrtimer_clock_base {
1603c8aa39dSThomas Gleixner 	struct hrtimer_cpu_base	*cpu_base;
1613f0b9e8eSAnna-Maria Gleixner 	unsigned int		index;
162ab8177bcSThomas Gleixner 	clockid_t		clockid;
163af5a06b5SAhmed S. Darwish 	seqcount_raw_spinlock_t	seq;
1643f0b9e8eSAnna-Maria Gleixner 	struct hrtimer		*running;
165998adc3dSJohn Stultz 	struct timerqueue_head	active;
166c0a31329SThomas Gleixner 	ktime_t			(*get_time)(void);
16754cdfdb4SThomas Gleixner 	ktime_t			offset;
1683f0b9e8eSAnna-Maria Gleixner } __hrtimer_clock_base_align;
1693c8aa39dSThomas Gleixner 
170e06383dbSJohn Stultz enum  hrtimer_base_type {
171e06383dbSJohn Stultz 	HRTIMER_BASE_MONOTONIC,
17268fa61c0SThomas Gleixner 	HRTIMER_BASE_REALTIME,
173a3ed0e43SThomas Gleixner 	HRTIMER_BASE_BOOTTIME,
17490adda98SJohn Stultz 	HRTIMER_BASE_TAI,
17598ecadd4SAnna-Maria Gleixner 	HRTIMER_BASE_MONOTONIC_SOFT,
17698ecadd4SAnna-Maria Gleixner 	HRTIMER_BASE_REALTIME_SOFT,
177a3ed0e43SThomas Gleixner 	HRTIMER_BASE_BOOTTIME_SOFT,
17898ecadd4SAnna-Maria Gleixner 	HRTIMER_BASE_TAI_SOFT,
179e06383dbSJohn Stultz 	HRTIMER_MAX_CLOCK_BASES,
180e06383dbSJohn Stultz };
1813c8aa39dSThomas Gleixner 
1821fbc78b3SAnna-Maria Gleixner /**
1833c8aa39dSThomas Gleixner  * struct hrtimer_cpu_base - the per cpu clock bases
1843c8aa39dSThomas Gleixner  * @lock:		lock protecting the base and associated clock bases
1853c8aa39dSThomas Gleixner  *			and timers
186cddd0248SViresh Kumar  * @cpu:		cpu number
187ab8177bcSThomas Gleixner  * @active_bases:	Bitfield to mark bases with active timers
188868a3e91SThomas Gleixner  * @clock_was_set_seq:	Sequence counter of clock was set events
18954cdfdb4SThomas Gleixner  * @hres_active:	State of high resolution mode
19028bfd18bSAnna-Maria Gleixner  * @in_hrtirq:		hrtimer_interrupt() is currently executing
19141d2e494SThomas Gleixner  * @hang_detected:	The last hrtimer interrupt detected a hang
1925da70160SAnna-Maria Gleixner  * @softirq_activated:	displays, if the softirq is raised - update of softirq
1935da70160SAnna-Maria Gleixner  *			related settings is not required then.
19441d2e494SThomas Gleixner  * @nr_events:		Total number of hrtimer interrupt events
19541d2e494SThomas Gleixner  * @nr_retries:		Total number of hrtimer interrupt retries
19641d2e494SThomas Gleixner  * @nr_hangs:		Total number of hrtimer interrupt hangs
19741d2e494SThomas Gleixner  * @max_hang_time:	Maximum time spent in hrtimer_interrupt
198f61eff83SAnna-Maria Gleixner  * @softirq_expiry_lock: Lock which is taken while softirq based hrtimer are
199f61eff83SAnna-Maria Gleixner  *			 expired
200f61eff83SAnna-Maria Gleixner  * @timer_waiters:	A hrtimer_cancel() invocation waits for the timer
201f61eff83SAnna-Maria Gleixner  *			callback to finish.
20207a9a7eaSAnna-Maria Gleixner  * @expires_next:	absolute time of the next event, is required for remote
2035da70160SAnna-Maria Gleixner  *			hrtimer enqueue; it is the total first expiry time (hard
2045da70160SAnna-Maria Gleixner  *			and soft hrtimer are taken into account)
205eb27926bSAnna-Maria Gleixner  * @next_timer:		Pointer to the first expiring timer
2065da70160SAnna-Maria Gleixner  * @softirq_expires_next: Time to check, if soft queues needs also to be expired
2075da70160SAnna-Maria Gleixner  * @softirq_next_timer: Pointer to the first expiring softirq based timer
208ab8177bcSThomas Gleixner  * @clock_base:		array of clock bases for this cpu
209895bdfa7SThomas Gleixner  *
210895bdfa7SThomas Gleixner  * Note: next_timer is just an optimization for __remove_hrtimer().
211895bdfa7SThomas Gleixner  *	 Do not dereference the pointer because it is not reliable on
212895bdfa7SThomas Gleixner  *	 cross cpu removals.
2133c8aa39dSThomas Gleixner  */
2143c8aa39dSThomas Gleixner struct hrtimer_cpu_base {
215ecb49d1aSThomas Gleixner 	raw_spinlock_t			lock;
216cddd0248SViresh Kumar 	unsigned int			cpu;
217f55a6faaSJohn Stultz 	unsigned int			active_bases;
218868a3e91SThomas Gleixner 	unsigned int			clock_was_set_seq;
21911a9fe06SAnna-Maria Gleixner 	unsigned int			hres_active		: 1,
22011a9fe06SAnna-Maria Gleixner 					in_hrtirq		: 1,
2215da70160SAnna-Maria Gleixner 					hang_detected		: 1,
2225da70160SAnna-Maria Gleixner 					softirq_activated       : 1;
22311a9fe06SAnna-Maria Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
224a6ffebceSThomas Gleixner 	unsigned int			nr_events;
225da21c5a5SAnna-Maria Gleixner 	unsigned short			nr_retries;
226da21c5a5SAnna-Maria Gleixner 	unsigned short			nr_hangs;
227a6ffebceSThomas Gleixner 	unsigned int			max_hang_time;
22854cdfdb4SThomas Gleixner #endif
229f61eff83SAnna-Maria Gleixner #ifdef CONFIG_PREEMPT_RT
230f61eff83SAnna-Maria Gleixner 	spinlock_t			softirq_expiry_lock;
231f61eff83SAnna-Maria Gleixner 	atomic_t			timer_waiters;
232f61eff83SAnna-Maria Gleixner #endif
23307a9a7eaSAnna-Maria Gleixner 	ktime_t				expires_next;
234eb27926bSAnna-Maria Gleixner 	struct hrtimer			*next_timer;
2355da70160SAnna-Maria Gleixner 	ktime_t				softirq_expires_next;
2365da70160SAnna-Maria Gleixner 	struct hrtimer			*softirq_next_timer;
237f24444b0SThomas Gleixner 	struct hrtimer_clock_base	clock_base[HRTIMER_MAX_CLOCK_BASES];
2386d9a1411SThomas Gleixner } ____cacheline_aligned;
239c0a31329SThomas Gleixner 
24063ca243bSArjan van de Ven static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
24163ca243bSArjan van de Ven {
242998adc3dSJohn Stultz 	timer->node.expires = time;
243654c8e0bSArjan van de Ven 	timer->_softexpires = time;
24463ca243bSArjan van de Ven }
245654c8e0bSArjan van de Ven 
246654c8e0bSArjan van de Ven static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
247654c8e0bSArjan van de Ven {
248654c8e0bSArjan van de Ven 	timer->_softexpires = time;
249998adc3dSJohn Stultz 	timer->node.expires = ktime_add_safe(time, delta);
250654c8e0bSArjan van de Ven }
251654c8e0bSArjan van de Ven 
252da8b44d5SJohn Stultz static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, u64 delta)
253654c8e0bSArjan van de Ven {
254654c8e0bSArjan van de Ven 	timer->_softexpires = time;
255998adc3dSJohn Stultz 	timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta));
256654c8e0bSArjan van de Ven }
257654c8e0bSArjan van de Ven 
25863ca243bSArjan van de Ven static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
25963ca243bSArjan van de Ven {
2602456e855SThomas Gleixner 	timer->node.expires = tv64;
2612456e855SThomas Gleixner 	timer->_softexpires = tv64;
26263ca243bSArjan van de Ven }
26363ca243bSArjan van de Ven 
26463ca243bSArjan van de Ven static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
26563ca243bSArjan van de Ven {
266998adc3dSJohn Stultz 	timer->node.expires = ktime_add_safe(timer->node.expires, time);
267654c8e0bSArjan van de Ven 	timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
26863ca243bSArjan van de Ven }
26963ca243bSArjan van de Ven 
2707597bc94SDavid Howells static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns)
27163ca243bSArjan van de Ven {
272998adc3dSJohn Stultz 	timer->node.expires = ktime_add_ns(timer->node.expires, ns);
273654c8e0bSArjan van de Ven 	timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
27463ca243bSArjan van de Ven }
27563ca243bSArjan van de Ven 
27663ca243bSArjan van de Ven static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
27763ca243bSArjan van de Ven {
278998adc3dSJohn Stultz 	return timer->node.expires;
27963ca243bSArjan van de Ven }
28063ca243bSArjan van de Ven 
281654c8e0bSArjan van de Ven static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
282654c8e0bSArjan van de Ven {
283654c8e0bSArjan van de Ven 	return timer->_softexpires;
284654c8e0bSArjan van de Ven }
285654c8e0bSArjan van de Ven 
28663ca243bSArjan van de Ven static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
28763ca243bSArjan van de Ven {
2882456e855SThomas Gleixner 	return timer->node.expires;
28963ca243bSArjan van de Ven }
290654c8e0bSArjan van de Ven static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
291654c8e0bSArjan van de Ven {
2922456e855SThomas Gleixner 	return timer->_softexpires;
293654c8e0bSArjan van de Ven }
29463ca243bSArjan van de Ven 
29563ca243bSArjan van de Ven static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
29663ca243bSArjan van de Ven {
297998adc3dSJohn Stultz 	return ktime_to_ns(timer->node.expires);
29863ca243bSArjan van de Ven }
29963ca243bSArjan van de Ven 
30063ca243bSArjan van de Ven static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
30163ca243bSArjan van de Ven {
302998adc3dSJohn Stultz 	return ktime_sub(timer->node.expires, timer->base->get_time());
30363ca243bSArjan van de Ven }
30463ca243bSArjan van de Ven 
30554cdfdb4SThomas Gleixner static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
30654cdfdb4SThomas Gleixner {
30754cdfdb4SThomas Gleixner 	return timer->base->get_time();
30854cdfdb4SThomas Gleixner }
30954cdfdb4SThomas Gleixner 
31028bfd18bSAnna-Maria Gleixner static inline int hrtimer_is_hres_active(struct hrtimer *timer)
31128bfd18bSAnna-Maria Gleixner {
31228bfd18bSAnna-Maria Gleixner 	return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ?
31328bfd18bSAnna-Maria Gleixner 		timer->base->cpu_base->hres_active : 0;
31428bfd18bSAnna-Maria Gleixner }
31528bfd18bSAnna-Maria Gleixner 
31621d6d52aSThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS
31721d6d52aSThomas Gleixner struct clock_event_device;
31821d6d52aSThomas Gleixner 
31921d6d52aSThomas Gleixner extern void hrtimer_interrupt(struct clock_event_device *dev);
32021d6d52aSThomas Gleixner 
321f55a6faaSJohn Stultz extern void clock_was_set_delayed(void);
322f55a6faaSJohn Stultz 
323398ca17fSThomas Gleixner extern unsigned int hrtimer_resolution;
324398ca17fSThomas Gleixner 
32554cdfdb4SThomas Gleixner #else
32654cdfdb4SThomas Gleixner 
327d711b8b3SBorislav Petkov #define hrtimer_resolution	(unsigned int)LOW_RES_NSEC
328398ca17fSThomas Gleixner 
329f55a6faaSJohn Stultz static inline void clock_was_set_delayed(void) { }
330f55a6faaSJohn Stultz 
33154cdfdb4SThomas Gleixner #endif
33254cdfdb4SThomas Gleixner 
333203cbf77SThomas Gleixner static inline ktime_t
334203cbf77SThomas Gleixner __hrtimer_expires_remaining_adjusted(const struct hrtimer *timer, ktime_t now)
335203cbf77SThomas Gleixner {
336203cbf77SThomas Gleixner 	ktime_t rem = ktime_sub(timer->node.expires, now);
337203cbf77SThomas Gleixner 
338203cbf77SThomas Gleixner 	/*
339203cbf77SThomas Gleixner 	 * Adjust relative timers for the extra we added in
340203cbf77SThomas Gleixner 	 * hrtimer_start_range_ns() to prevent short timeouts.
341203cbf77SThomas Gleixner 	 */
342203cbf77SThomas Gleixner 	if (IS_ENABLED(CONFIG_TIME_LOW_RES) && timer->is_rel)
3432456e855SThomas Gleixner 		rem -= hrtimer_resolution;
344203cbf77SThomas Gleixner 	return rem;
345203cbf77SThomas Gleixner }
346203cbf77SThomas Gleixner 
347203cbf77SThomas Gleixner static inline ktime_t
348203cbf77SThomas Gleixner hrtimer_expires_remaining_adjusted(const struct hrtimer *timer)
349203cbf77SThomas Gleixner {
350203cbf77SThomas Gleixner 	return __hrtimer_expires_remaining_adjusted(timer,
351203cbf77SThomas Gleixner 						    timer->base->get_time());
352203cbf77SThomas Gleixner }
353203cbf77SThomas Gleixner 
354b12a03ceSThomas Gleixner extern void clock_was_set(void);
3559ec26907SThomas Gleixner #ifdef CONFIG_TIMERFD
3569ec26907SThomas Gleixner extern void timerfd_clock_was_set(void);
3579ec26907SThomas Gleixner #else
3589ec26907SThomas Gleixner static inline void timerfd_clock_was_set(void) { }
3599ec26907SThomas Gleixner #endif
360b12a03ceSThomas Gleixner extern void hrtimers_resume(void);
361b12a03ceSThomas Gleixner 
3622e94d1f7SArjan van de Ven DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
3632e94d1f7SArjan van de Ven 
364f61eff83SAnna-Maria Gleixner #ifdef CONFIG_PREEMPT_RT
365f61eff83SAnna-Maria Gleixner void hrtimer_cancel_wait_running(const struct hrtimer *timer);
366f61eff83SAnna-Maria Gleixner #else
367f61eff83SAnna-Maria Gleixner static inline void hrtimer_cancel_wait_running(struct hrtimer *timer)
368f61eff83SAnna-Maria Gleixner {
369f61eff83SAnna-Maria Gleixner 	cpu_relax();
370f61eff83SAnna-Maria Gleixner }
371f61eff83SAnna-Maria Gleixner #endif
3722e94d1f7SArjan van de Ven 
373c0a31329SThomas Gleixner /* Exported timer functions: */
374c0a31329SThomas Gleixner 
375c0a31329SThomas Gleixner /* Initialize timers: */
3767978672cSGeorge Anzinger extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
3777978672cSGeorge Anzinger 			 enum hrtimer_mode mode);
378dbc1625fSSebastian Andrzej Siewior extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id,
379dbc1625fSSebastian Andrzej Siewior 				 enum hrtimer_mode mode);
380c0a31329SThomas Gleixner 
381237fc6e7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
382237fc6e7SThomas Gleixner extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
383237fc6e7SThomas Gleixner 				  enum hrtimer_mode mode);
384dbc1625fSSebastian Andrzej Siewior extern void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
385dbc1625fSSebastian Andrzej Siewior 					  clockid_t clock_id,
386dbc1625fSSebastian Andrzej Siewior 					  enum hrtimer_mode mode);
387237fc6e7SThomas Gleixner 
388237fc6e7SThomas Gleixner extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
389237fc6e7SThomas Gleixner #else
390237fc6e7SThomas Gleixner static inline void hrtimer_init_on_stack(struct hrtimer *timer,
391237fc6e7SThomas Gleixner 					 clockid_t which_clock,
392237fc6e7SThomas Gleixner 					 enum hrtimer_mode mode)
393237fc6e7SThomas Gleixner {
394237fc6e7SThomas Gleixner 	hrtimer_init(timer, which_clock, mode);
395237fc6e7SThomas Gleixner }
396dbc1625fSSebastian Andrzej Siewior 
397dbc1625fSSebastian Andrzej Siewior static inline void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
398dbc1625fSSebastian Andrzej Siewior 						 clockid_t clock_id,
399dbc1625fSSebastian Andrzej Siewior 						 enum hrtimer_mode mode)
400dbc1625fSSebastian Andrzej Siewior {
401dbc1625fSSebastian Andrzej Siewior 	hrtimer_init_sleeper(sl, clock_id, mode);
402dbc1625fSSebastian Andrzej Siewior }
403dbc1625fSSebastian Andrzej Siewior 
404237fc6e7SThomas Gleixner static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
405237fc6e7SThomas Gleixner #endif
406237fc6e7SThomas Gleixner 
407c0a31329SThomas Gleixner /* Basic timer operations: */
40861699e13SThomas Gleixner extern void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
409da8b44d5SJohn Stultz 				   u64 range_ns, const enum hrtimer_mode mode);
4107f1e2ca9SPeter Zijlstra 
41102a171afSThomas Gleixner /**
4126de6250cSAnna-Maria Gleixner  * hrtimer_start - (re)start an hrtimer
41302a171afSThomas Gleixner  * @timer:	the timer to be added
41402a171afSThomas Gleixner  * @tim:	expiry time
4156de6250cSAnna-Maria Gleixner  * @mode:	timer mode: absolute (HRTIMER_MODE_ABS) or
4165da70160SAnna-Maria Gleixner  *		relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
4175da70160SAnna-Maria Gleixner  *		softirq based mode is considered for debug purpose only!
41802a171afSThomas Gleixner  */
41961699e13SThomas Gleixner static inline void hrtimer_start(struct hrtimer *timer, ktime_t tim,
42002a171afSThomas Gleixner 				 const enum hrtimer_mode mode)
42102a171afSThomas Gleixner {
42261699e13SThomas Gleixner 	hrtimer_start_range_ns(timer, tim, 0, mode);
42302a171afSThomas Gleixner }
42402a171afSThomas Gleixner 
425c0a31329SThomas Gleixner extern int hrtimer_cancel(struct hrtimer *timer);
426c0a31329SThomas Gleixner extern int hrtimer_try_to_cancel(struct hrtimer *timer);
427c0a31329SThomas Gleixner 
42861699e13SThomas Gleixner static inline void hrtimer_start_expires(struct hrtimer *timer,
42963ca243bSArjan van de Ven 					 enum hrtimer_mode mode)
43063ca243bSArjan van de Ven {
431da8b44d5SJohn Stultz 	u64 delta;
432da8f2e17SArjan van de Ven 	ktime_t soft, hard;
433da8f2e17SArjan van de Ven 	soft = hrtimer_get_softexpires(timer);
434da8f2e17SArjan van de Ven 	hard = hrtimer_get_expires(timer);
435da8f2e17SArjan van de Ven 	delta = ktime_to_ns(ktime_sub(hard, soft));
43661699e13SThomas Gleixner 	hrtimer_start_range_ns(timer, soft, delta, mode);
43763ca243bSArjan van de Ven }
43863ca243bSArjan van de Ven 
43901656464SThomas Gleixner void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl,
44001656464SThomas Gleixner 				   enum hrtimer_mode mode);
44101656464SThomas Gleixner 
44261699e13SThomas Gleixner static inline void hrtimer_restart(struct hrtimer *timer)
443c9cb2e3dSThomas Gleixner {
44461699e13SThomas Gleixner 	hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
445c9cb2e3dSThomas Gleixner }
446c0a31329SThomas Gleixner 
447c0a31329SThomas Gleixner /* Query timers: */
448203cbf77SThomas Gleixner extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
449203cbf77SThomas Gleixner 
450203cbf77SThomas Gleixner static inline ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
451203cbf77SThomas Gleixner {
452203cbf77SThomas Gleixner 	return __hrtimer_get_remaining(timer, false);
453203cbf77SThomas Gleixner }
454c0a31329SThomas Gleixner 
455c1ad348bSThomas Gleixner extern u64 hrtimer_get_next_event(void);
456a59855cdSRafael J. Wysocki extern u64 hrtimer_next_event_without(const struct hrtimer *exclude);
45769239749STony Lindgren 
458887d9dc9SPeter Zijlstra extern bool hrtimer_active(const struct hrtimer *timer);
459c0a31329SThomas Gleixner 
46056144737SEric Dumazet /**
46156144737SEric Dumazet  * hrtimer_is_queued = check, whether the timer is on one of the queues
46256144737SEric Dumazet  * @timer:	Timer to check
46356144737SEric Dumazet  *
46456144737SEric Dumazet  * Returns: True if the timer is queued, false otherwise
46556144737SEric Dumazet  *
46656144737SEric Dumazet  * The function can be used lockless, but it gives only a current snapshot.
46754cdfdb4SThomas Gleixner  */
46856144737SEric Dumazet static inline bool hrtimer_is_queued(struct hrtimer *timer)
46954cdfdb4SThomas Gleixner {
47056144737SEric Dumazet 	/* The READ_ONCE pairs with the update functions of timer->state */
47156144737SEric Dumazet 	return !!(READ_ONCE(timer->state) & HRTIMER_STATE_ENQUEUED);
47254cdfdb4SThomas Gleixner }
47354cdfdb4SThomas Gleixner 
4744346f654SOliver Hartkopp /*
4754346f654SOliver Hartkopp  * Helper function to check, whether the timer is running the callback
4764346f654SOliver Hartkopp  * function
4774346f654SOliver Hartkopp  */
4784346f654SOliver Hartkopp static inline int hrtimer_callback_running(struct hrtimer *timer)
4794346f654SOliver Hartkopp {
4803f0b9e8eSAnna-Maria Gleixner 	return timer->base->running == timer;
4814346f654SOliver Hartkopp }
4824346f654SOliver Hartkopp 
483c0a31329SThomas Gleixner /* Forward a hrtimer so it expires after now: */
4844d672e7aSDavide Libenzi extern u64
48544f21475SRoman Zippel hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
486c0a31329SThomas Gleixner 
48791e5a217SThomas Gleixner /**
48891e5a217SThomas Gleixner  * hrtimer_forward_now - forward the timer expiry so it expires after now
48991e5a217SThomas Gleixner  * @timer:	hrtimer to forward
49091e5a217SThomas Gleixner  * @interval:	the interval to forward
49191e5a217SThomas Gleixner  *
49291e5a217SThomas Gleixner  * Forward the timer expiry so it will expire after the current time
49391e5a217SThomas Gleixner  * of the hrtimer clock base. Returns the number of overruns.
49491e5a217SThomas Gleixner  *
49591e5a217SThomas Gleixner  * Can be safely called from the callback function of @timer. If
49691e5a217SThomas Gleixner  * called from other contexts @timer must neither be enqueued nor
49791e5a217SThomas Gleixner  * running the callback and the caller needs to take care of
49891e5a217SThomas Gleixner  * serialization.
49991e5a217SThomas Gleixner  *
50091e5a217SThomas Gleixner  * Note: This only updates the timer expiry value and does not requeue
50191e5a217SThomas Gleixner  * the timer.
50291e5a217SThomas Gleixner  */
5034d672e7aSDavide Libenzi static inline u64 hrtimer_forward_now(struct hrtimer *timer,
5045e05ad7dSDavide Libenzi 				      ktime_t interval)
5055e05ad7dSDavide Libenzi {
5065e05ad7dSDavide Libenzi 	return hrtimer_forward(timer, timer->base->get_time(), interval);
5075e05ad7dSDavide Libenzi }
5085e05ad7dSDavide Libenzi 
50910c94ec1SThomas Gleixner /* Precise sleep: */
510ce41aaf4SAl Viro 
511c0edd7c9SDeepa Dinamani extern int nanosleep_copyout(struct restart_block *, struct timespec64 *);
512ea2d1f7fSAndrei Vagin extern long hrtimer_nanosleep(ktime_t rqtp, const enum hrtimer_mode mode,
51310c94ec1SThomas Gleixner 			      const clockid_t clockid);
51410c94ec1SThomas Gleixner 
515da8b44d5SJohn Stultz extern int schedule_hrtimeout_range(ktime_t *expires, u64 delta,
516654c8e0bSArjan van de Ven 				    const enum hrtimer_mode mode);
517351b3f7aSCarsten Emde extern int schedule_hrtimeout_range_clock(ktime_t *expires,
518da8b44d5SJohn Stultz 					  u64 delta,
519da8b44d5SJohn Stultz 					  const enum hrtimer_mode mode,
52090777713SAnna-Maria Gleixner 					  clockid_t clock_id);
5217bb67439SArjan van de Ven extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
5227bb67439SArjan van de Ven 
523c0a31329SThomas Gleixner /* Soft interrupt function to run the hrtimer queues: */
524c0a31329SThomas Gleixner extern void hrtimer_run_queues(void);
525c0a31329SThomas Gleixner 
526c0a31329SThomas Gleixner /* Bootup initialization: */
527c0a31329SThomas Gleixner extern void __init hrtimers_init(void);
528c0a31329SThomas Gleixner 
52988ad0bf6SIngo Molnar /* Show pending timers: */
53088ad0bf6SIngo Molnar extern void sysrq_timer_list_show(void);
53188ad0bf6SIngo Molnar 
53227590dc1SThomas Gleixner int hrtimers_prepare_cpu(unsigned int cpu);
53327590dc1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
53427590dc1SThomas Gleixner int hrtimers_dead_cpu(unsigned int cpu);
53527590dc1SThomas Gleixner #else
53627590dc1SThomas Gleixner #define hrtimers_dead_cpu	NULL
53727590dc1SThomas Gleixner #endif
53827590dc1SThomas Gleixner 
539c0a31329SThomas Gleixner #endif
540