xref: /linux-6.15/include/linux/hrtimer.h (revision 79bf2bb3)
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 /*
44303e967fSThomas Gleixner  * Bit values to track state of the timer
45303e967fSThomas Gleixner  *
46303e967fSThomas Gleixner  * Possible states:
47303e967fSThomas Gleixner  *
48303e967fSThomas Gleixner  * 0x00		inactive
49303e967fSThomas Gleixner  * 0x01		enqueued into rbtree
50303e967fSThomas Gleixner  * 0x02		callback function running
51303e967fSThomas Gleixner  * 0x03		callback function running and enqueued
52303e967fSThomas Gleixner  *		(was requeued on another CPU)
53303e967fSThomas Gleixner  *
54303e967fSThomas Gleixner  * The "callback function running and enqueued" status is only possible on
55303e967fSThomas Gleixner  * SMP. It happens for example when a posix timer expired and the callback
56303e967fSThomas Gleixner  * queued a signal. Between dropping the lock which protects the posix timer
57303e967fSThomas Gleixner  * and reacquiring the base lock of the hrtimer, another CPU can deliver the
58303e967fSThomas Gleixner  * signal and rearm the timer. We have to preserve the callback running state,
59303e967fSThomas Gleixner  * as otherwise the timer could be removed before the softirq code finishes the
60303e967fSThomas Gleixner  * the handling of the timer.
61303e967fSThomas Gleixner  *
62303e967fSThomas Gleixner  * The HRTIMER_STATE_ENQUEUE bit is always or'ed to the current state to
63303e967fSThomas Gleixner  * preserve the HRTIMER_STATE_CALLBACK bit in the above scenario.
64303e967fSThomas Gleixner  *
65303e967fSThomas Gleixner  * All state transitions are protected by cpu_base->lock.
66303e967fSThomas Gleixner  */
67303e967fSThomas Gleixner #define HRTIMER_STATE_INACTIVE	0x00
68303e967fSThomas Gleixner #define HRTIMER_STATE_ENQUEUED	0x01
69303e967fSThomas Gleixner #define HRTIMER_STATE_CALLBACK	0x02
70303e967fSThomas Gleixner 
71c0a31329SThomas Gleixner /**
72c0a31329SThomas Gleixner  * struct hrtimer - the basic hrtimer structure
73c0a31329SThomas Gleixner  * @node:	red black tree node for time ordered insertion
74c0a31329SThomas Gleixner  * @expires:	the absolute expiry time in the hrtimers internal
75c0a31329SThomas Gleixner  *		representation. The time is related to the clock on
76c0a31329SThomas Gleixner  *		which the timer is based.
77c0a31329SThomas Gleixner  * @function:	timer expiry callback function
78c0a31329SThomas Gleixner  * @base:	pointer to the timer base (per cpu and per clock)
79303e967fSThomas Gleixner  * @state:	state information (See bit values above)
80c0a31329SThomas Gleixner  *
81c0a31329SThomas Gleixner  * The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE()
82c0a31329SThomas Gleixner  */
83c0a31329SThomas Gleixner struct hrtimer {
84c0a31329SThomas Gleixner 	struct rb_node			node;
85c0a31329SThomas Gleixner 	ktime_t				expires;
86c9cb2e3dSThomas Gleixner 	enum hrtimer_restart		(*function)(struct hrtimer *);
873c8aa39dSThomas Gleixner 	struct hrtimer_clock_base	*base;
88303e967fSThomas Gleixner 	unsigned long			state;
89c0a31329SThomas Gleixner };
90c0a31329SThomas Gleixner 
91c0a31329SThomas Gleixner /**
9200362e33SThomas Gleixner  * struct hrtimer_sleeper - simple sleeper structure
9300362e33SThomas Gleixner  * @timer:	embedded timer structure
9400362e33SThomas Gleixner  * @task:	task to wake up
9500362e33SThomas Gleixner  *
9600362e33SThomas Gleixner  * task is set to NULL, when the timer expires.
9700362e33SThomas Gleixner  */
9800362e33SThomas Gleixner struct hrtimer_sleeper {
9900362e33SThomas Gleixner 	struct hrtimer timer;
10000362e33SThomas Gleixner 	struct task_struct *task;
10100362e33SThomas Gleixner };
10200362e33SThomas Gleixner 
10300362e33SThomas Gleixner /**
104c0a31329SThomas Gleixner  * struct hrtimer_base - the timer base for a specific clock
1053c8aa39dSThomas Gleixner  * @index:		clock type index for per_cpu support when moving a
1063c8aa39dSThomas Gleixner  *			timer to a base on another cpu.
107c0a31329SThomas Gleixner  * @active:		red black tree root node for the active timers
108288867ecSThomas Gleixner  * @first:		pointer to the timer node which expires first
109c0a31329SThomas Gleixner  * @resolution:		the resolution of the clock, in nanoseconds
110c0a31329SThomas Gleixner  * @get_time:		function to retrieve the current time of the clock
111a580290cSMartin Waitz  * @get_softirq_time:	function to retrieve the current time from the softirq
11292127c7aSThomas Gleixner  * @softirq_time:	the time when running the hrtimer queue in the softirq
113c0a31329SThomas Gleixner  */
1143c8aa39dSThomas Gleixner struct hrtimer_clock_base {
1153c8aa39dSThomas Gleixner 	struct hrtimer_cpu_base	*cpu_base;
116c0a31329SThomas Gleixner 	clockid_t		index;
117c0a31329SThomas Gleixner 	struct rb_root		active;
118288867ecSThomas Gleixner 	struct rb_node		*first;
119e2787630SThomas Gleixner 	ktime_t			resolution;
120c0a31329SThomas Gleixner 	ktime_t			(*get_time)(void);
12192127c7aSThomas Gleixner 	ktime_t			(*get_softirq_time)(void);
12292127c7aSThomas Gleixner 	ktime_t			softirq_time;
1233c8aa39dSThomas Gleixner };
1243c8aa39dSThomas Gleixner 
1253c8aa39dSThomas Gleixner #define HRTIMER_MAX_CLOCK_BASES 2
1263c8aa39dSThomas Gleixner 
1273c8aa39dSThomas Gleixner /*
1283c8aa39dSThomas Gleixner  * struct hrtimer_cpu_base - the per cpu clock bases
1293c8aa39dSThomas Gleixner  * @lock:		lock protecting the base and associated clock bases
1303c8aa39dSThomas Gleixner  *			and timers
1313c8aa39dSThomas Gleixner  * @lock_key:		the lock_class_key for use with lockdep
1323c8aa39dSThomas Gleixner  * @clock_base:		array of clock bases for this cpu
1333c8aa39dSThomas Gleixner  * @curr_timer:		the timer which is executing a callback right now
1343c8aa39dSThomas Gleixner  */
1353c8aa39dSThomas Gleixner struct hrtimer_cpu_base {
1363c8aa39dSThomas Gleixner 	spinlock_t			lock;
13754365524SIngo Molnar 	struct lock_class_key		lock_key;
1383c8aa39dSThomas Gleixner 	struct hrtimer_clock_base	clock_base[HRTIMER_MAX_CLOCK_BASES];
139c0a31329SThomas Gleixner };
140c0a31329SThomas Gleixner 
141becf8b5dSThomas Gleixner /*
142becf8b5dSThomas Gleixner  * clock_was_set() is a NOP for non- high-resolution systems. The
143becf8b5dSThomas Gleixner  * time-sorted order guarantees that a timer does not expire early and
144becf8b5dSThomas Gleixner  * is expired in the next softirq when the clock was advanced.
145becf8b5dSThomas Gleixner  */
146becf8b5dSThomas Gleixner #define clock_was_set()		do { } while (0)
147d316c57fSThomas Gleixner extern ktime_t ktime_get(void);
148d316c57fSThomas Gleixner extern ktime_t ktime_get_real(void);
149becf8b5dSThomas Gleixner 
150c0a31329SThomas Gleixner /* Exported timer functions: */
151c0a31329SThomas Gleixner 
152c0a31329SThomas Gleixner /* Initialize timers: */
1537978672cSGeorge Anzinger extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
1547978672cSGeorge Anzinger 			 enum hrtimer_mode mode);
155c0a31329SThomas Gleixner 
156c0a31329SThomas Gleixner /* Basic timer operations: */
157c0a31329SThomas Gleixner extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
158c0a31329SThomas Gleixner 			 const enum hrtimer_mode mode);
159c0a31329SThomas Gleixner extern int hrtimer_cancel(struct hrtimer *timer);
160c0a31329SThomas Gleixner extern int hrtimer_try_to_cancel(struct hrtimer *timer);
161c0a31329SThomas Gleixner 
162c9cb2e3dSThomas Gleixner static inline int hrtimer_restart(struct hrtimer *timer)
163c9cb2e3dSThomas Gleixner {
164c9cb2e3dSThomas Gleixner 	return hrtimer_start(timer, timer->expires, HRTIMER_MODE_ABS);
165c9cb2e3dSThomas Gleixner }
166c0a31329SThomas Gleixner 
167c0a31329SThomas Gleixner /* Query timers: */
168c0a31329SThomas Gleixner extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
169c0a31329SThomas Gleixner extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
170c0a31329SThomas Gleixner 
17169239749STony Lindgren #ifdef CONFIG_NO_IDLE_HZ
17269239749STony Lindgren extern ktime_t hrtimer_get_next_event(void);
17369239749STony Lindgren #endif
17469239749STony Lindgren 
175303e967fSThomas Gleixner /*
176303e967fSThomas Gleixner  * A timer is active, when it is enqueued into the rbtree or the callback
177303e967fSThomas Gleixner  * function is running.
178303e967fSThomas Gleixner  */
179c0a31329SThomas Gleixner static inline int hrtimer_active(const struct hrtimer *timer)
180c0a31329SThomas Gleixner {
181303e967fSThomas Gleixner 	return timer->state != HRTIMER_STATE_INACTIVE;
182c0a31329SThomas Gleixner }
183c0a31329SThomas Gleixner 
184c0a31329SThomas Gleixner /* Forward a hrtimer so it expires after now: */
18544f21475SRoman Zippel extern unsigned long
18644f21475SRoman Zippel hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
187c0a31329SThomas Gleixner 
18810c94ec1SThomas Gleixner /* Precise sleep: */
18910c94ec1SThomas Gleixner extern long hrtimer_nanosleep(struct timespec *rqtp,
19010c94ec1SThomas Gleixner 			      struct timespec __user *rmtp,
19110c94ec1SThomas Gleixner 			      const enum hrtimer_mode mode,
19210c94ec1SThomas Gleixner 			      const clockid_t clockid);
1931711ef38SToyo Abe extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
19410c94ec1SThomas Gleixner 
19500362e33SThomas Gleixner extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
19600362e33SThomas Gleixner 				 struct task_struct *tsk);
19700362e33SThomas Gleixner 
198c0a31329SThomas Gleixner /* Soft interrupt function to run the hrtimer queues: */
199c0a31329SThomas Gleixner extern void hrtimer_run_queues(void);
200c0a31329SThomas Gleixner 
201c0a31329SThomas Gleixner /* Bootup initialization: */
202c0a31329SThomas Gleixner extern void __init hrtimers_init(void);
203c0a31329SThomas Gleixner 
204*79bf2bb3SThomas Gleixner #if BITS_PER_LONG < 64
205*79bf2bb3SThomas Gleixner extern unsigned long ktime_divns(const ktime_t kt, s64 div);
206*79bf2bb3SThomas Gleixner #else /* BITS_PER_LONG < 64 */
207*79bf2bb3SThomas Gleixner # define ktime_divns(kt, div)		(unsigned long)((kt).tv64 / (div))
208*79bf2bb3SThomas Gleixner #endif
209*79bf2bb3SThomas Gleixner 
210c0a31329SThomas Gleixner #endif
211