xref: /linux-6.15/include/linux/hrtimer.h (revision 3c8aa39d)
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 
24*3c8aa39dSThomas Gleixner struct hrtimer_clock_base;
25*3c8aa39dSThomas Gleixner struct hrtimer_cpu_base;
26*3c8aa39dSThomas 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 
43c0a31329SThomas Gleixner /**
44c0a31329SThomas Gleixner  * struct hrtimer - the basic hrtimer structure
45c0a31329SThomas Gleixner  * @node:	red black tree node for time ordered insertion
46c0a31329SThomas Gleixner  * @expires:	the absolute expiry time in the hrtimers internal
47c0a31329SThomas Gleixner  *		representation. The time is related to the clock on
48c0a31329SThomas Gleixner  *		which the timer is based.
49c0a31329SThomas Gleixner  * @function:	timer expiry callback function
50c0a31329SThomas Gleixner  * @base:	pointer to the timer base (per cpu and per clock)
51c0a31329SThomas Gleixner  *
52c0a31329SThomas Gleixner  * The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE()
53c0a31329SThomas Gleixner  */
54c0a31329SThomas Gleixner struct hrtimer {
55c0a31329SThomas Gleixner 	struct rb_node			node;
56c0a31329SThomas Gleixner 	ktime_t				expires;
57c9cb2e3dSThomas Gleixner 	enum hrtimer_restart		(*function)(struct hrtimer *);
58*3c8aa39dSThomas Gleixner 	struct hrtimer_clock_base	*base;
59c0a31329SThomas Gleixner };
60c0a31329SThomas Gleixner 
61c0a31329SThomas Gleixner /**
6200362e33SThomas Gleixner  * struct hrtimer_sleeper - simple sleeper structure
6300362e33SThomas Gleixner  * @timer:	embedded timer structure
6400362e33SThomas Gleixner  * @task:	task to wake up
6500362e33SThomas Gleixner  *
6600362e33SThomas Gleixner  * task is set to NULL, when the timer expires.
6700362e33SThomas Gleixner  */
6800362e33SThomas Gleixner struct hrtimer_sleeper {
6900362e33SThomas Gleixner 	struct hrtimer timer;
7000362e33SThomas Gleixner 	struct task_struct *task;
7100362e33SThomas Gleixner };
7200362e33SThomas Gleixner 
7300362e33SThomas Gleixner /**
74c0a31329SThomas Gleixner  * struct hrtimer_base - the timer base for a specific clock
75*3c8aa39dSThomas Gleixner  * @index:		clock type index for per_cpu support when moving a
76*3c8aa39dSThomas Gleixner  *			timer to a base on another cpu.
77c0a31329SThomas Gleixner  * @active:		red black tree root node for the active timers
78288867ecSThomas Gleixner  * @first:		pointer to the timer node which expires first
79c0a31329SThomas Gleixner  * @resolution:		the resolution of the clock, in nanoseconds
80c0a31329SThomas Gleixner  * @get_time:		function to retrieve the current time of the clock
81a580290cSMartin Waitz  * @get_softirq_time:	function to retrieve the current time from the softirq
8292127c7aSThomas Gleixner  * @softirq_time:	the time when running the hrtimer queue in the softirq
83c0a31329SThomas Gleixner  */
84*3c8aa39dSThomas Gleixner struct hrtimer_clock_base {
85*3c8aa39dSThomas Gleixner 	struct hrtimer_cpu_base	*cpu_base;
86c0a31329SThomas Gleixner 	clockid_t		index;
87c0a31329SThomas Gleixner 	struct rb_root		active;
88288867ecSThomas Gleixner 	struct rb_node		*first;
89e2787630SThomas Gleixner 	ktime_t			resolution;
90c0a31329SThomas Gleixner 	ktime_t			(*get_time)(void);
9192127c7aSThomas Gleixner 	ktime_t			(*get_softirq_time)(void);
9292127c7aSThomas Gleixner 	ktime_t			softirq_time;
93*3c8aa39dSThomas Gleixner };
94*3c8aa39dSThomas Gleixner 
95*3c8aa39dSThomas Gleixner #define HRTIMER_MAX_CLOCK_BASES 2
96*3c8aa39dSThomas Gleixner 
97*3c8aa39dSThomas Gleixner /*
98*3c8aa39dSThomas Gleixner  * struct hrtimer_cpu_base - the per cpu clock bases
99*3c8aa39dSThomas Gleixner  * @lock:		lock protecting the base and associated clock bases
100*3c8aa39dSThomas Gleixner  *			and timers
101*3c8aa39dSThomas Gleixner  * @lock_key:		the lock_class_key for use with lockdep
102*3c8aa39dSThomas Gleixner  * @clock_base:		array of clock bases for this cpu
103*3c8aa39dSThomas Gleixner  * @curr_timer:		the timer which is executing a callback right now
104*3c8aa39dSThomas Gleixner  */
105*3c8aa39dSThomas Gleixner struct hrtimer_cpu_base {
106*3c8aa39dSThomas Gleixner 	spinlock_t			lock;
10754365524SIngo Molnar 	struct lock_class_key		lock_key;
108*3c8aa39dSThomas Gleixner 	struct hrtimer_clock_base	clock_base[HRTIMER_MAX_CLOCK_BASES];
109*3c8aa39dSThomas Gleixner 	struct hrtimer			*curr_timer;
110c0a31329SThomas Gleixner };
111c0a31329SThomas Gleixner 
112becf8b5dSThomas Gleixner /*
113becf8b5dSThomas Gleixner  * clock_was_set() is a NOP for non- high-resolution systems. The
114becf8b5dSThomas Gleixner  * time-sorted order guarantees that a timer does not expire early and
115becf8b5dSThomas Gleixner  * is expired in the next softirq when the clock was advanced.
116becf8b5dSThomas Gleixner  */
117becf8b5dSThomas Gleixner #define clock_was_set()		do { } while (0)
118becf8b5dSThomas Gleixner 
119c0a31329SThomas Gleixner /* Exported timer functions: */
120c0a31329SThomas Gleixner 
121c0a31329SThomas Gleixner /* Initialize timers: */
1227978672cSGeorge Anzinger extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
1237978672cSGeorge Anzinger 			 enum hrtimer_mode mode);
124c0a31329SThomas Gleixner 
125c0a31329SThomas Gleixner /* Basic timer operations: */
126c0a31329SThomas Gleixner extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
127c0a31329SThomas Gleixner 			 const enum hrtimer_mode mode);
128c0a31329SThomas Gleixner extern int hrtimer_cancel(struct hrtimer *timer);
129c0a31329SThomas Gleixner extern int hrtimer_try_to_cancel(struct hrtimer *timer);
130c0a31329SThomas Gleixner 
131c9cb2e3dSThomas Gleixner static inline int hrtimer_restart(struct hrtimer *timer)
132c9cb2e3dSThomas Gleixner {
133c9cb2e3dSThomas Gleixner 	return hrtimer_start(timer, timer->expires, HRTIMER_MODE_ABS);
134c9cb2e3dSThomas Gleixner }
135c0a31329SThomas Gleixner 
136c0a31329SThomas Gleixner /* Query timers: */
137c0a31329SThomas Gleixner extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
138c0a31329SThomas Gleixner extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
139c0a31329SThomas Gleixner 
14069239749STony Lindgren #ifdef CONFIG_NO_IDLE_HZ
14169239749STony Lindgren extern ktime_t hrtimer_get_next_event(void);
14269239749STony Lindgren #endif
14369239749STony Lindgren 
144c0a31329SThomas Gleixner static inline int hrtimer_active(const struct hrtimer *timer)
145c0a31329SThomas Gleixner {
146ed198cb4SDavid Woodhouse 	return rb_parent(&timer->node) != &timer->node;
147c0a31329SThomas Gleixner }
148c0a31329SThomas Gleixner 
149c0a31329SThomas Gleixner /* Forward a hrtimer so it expires after now: */
15044f21475SRoman Zippel extern unsigned long
15144f21475SRoman Zippel hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
152c0a31329SThomas Gleixner 
15310c94ec1SThomas Gleixner /* Precise sleep: */
15410c94ec1SThomas Gleixner extern long hrtimer_nanosleep(struct timespec *rqtp,
15510c94ec1SThomas Gleixner 			      struct timespec __user *rmtp,
15610c94ec1SThomas Gleixner 			      const enum hrtimer_mode mode,
15710c94ec1SThomas Gleixner 			      const clockid_t clockid);
1581711ef38SToyo Abe extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
15910c94ec1SThomas Gleixner 
16000362e33SThomas Gleixner extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
16100362e33SThomas Gleixner 				 struct task_struct *tsk);
16200362e33SThomas Gleixner 
163c0a31329SThomas Gleixner /* Soft interrupt function to run the hrtimer queues: */
164c0a31329SThomas Gleixner extern void hrtimer_run_queues(void);
165c0a31329SThomas Gleixner 
166411187fbSJohn Stultz /* Resume notification */
167411187fbSJohn Stultz void hrtimer_notify_resume(void);
168411187fbSJohn Stultz 
169c0a31329SThomas Gleixner /* Bootup initialization: */
170c0a31329SThomas Gleixner extern void __init hrtimers_init(void);
171c0a31329SThomas Gleixner 
172c0a31329SThomas Gleixner #endif
173