1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * hrtimers - High-resolution kernel timers
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
6*4882a593Smuzhiyun * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * data type definitions, declarations, prototypes
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Started by: Thomas Gleixner and Ingo Molnar
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun #ifndef _LINUX_HRTIMER_H
13*4882a593Smuzhiyun #define _LINUX_HRTIMER_H
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include <linux/hrtimer_defs.h>
16*4882a593Smuzhiyun #include <linux/rbtree.h>
17*4882a593Smuzhiyun #include <linux/init.h>
18*4882a593Smuzhiyun #include <linux/list.h>
19*4882a593Smuzhiyun #include <linux/percpu.h>
20*4882a593Smuzhiyun #include <linux/seqlock.h>
21*4882a593Smuzhiyun #include <linux/timer.h>
22*4882a593Smuzhiyun #include <linux/timerqueue.h>
23*4882a593Smuzhiyun #include <linux/android_kabi.h>
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun struct hrtimer_clock_base;
26*4882a593Smuzhiyun struct hrtimer_cpu_base;
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun * Mode arguments of xxx_hrtimer functions:
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * HRTIMER_MODE_ABS - Time value is absolute
32*4882a593Smuzhiyun * HRTIMER_MODE_REL - Time value is relative to now
33*4882a593Smuzhiyun * HRTIMER_MODE_PINNED - Timer is bound to CPU (is only considered
34*4882a593Smuzhiyun * when starting the timer)
35*4882a593Smuzhiyun * HRTIMER_MODE_SOFT - Timer callback function will be executed in
36*4882a593Smuzhiyun * soft irq context
37*4882a593Smuzhiyun * HRTIMER_MODE_HARD - Timer callback function will be executed in
38*4882a593Smuzhiyun * hard irq context even on PREEMPT_RT.
39*4882a593Smuzhiyun */
40*4882a593Smuzhiyun enum hrtimer_mode {
41*4882a593Smuzhiyun HRTIMER_MODE_ABS = 0x00,
42*4882a593Smuzhiyun HRTIMER_MODE_REL = 0x01,
43*4882a593Smuzhiyun HRTIMER_MODE_PINNED = 0x02,
44*4882a593Smuzhiyun HRTIMER_MODE_SOFT = 0x04,
45*4882a593Smuzhiyun HRTIMER_MODE_HARD = 0x08,
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun HRTIMER_MODE_ABS_PINNED = HRTIMER_MODE_ABS | HRTIMER_MODE_PINNED,
48*4882a593Smuzhiyun HRTIMER_MODE_REL_PINNED = HRTIMER_MODE_REL | HRTIMER_MODE_PINNED,
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun HRTIMER_MODE_ABS_SOFT = HRTIMER_MODE_ABS | HRTIMER_MODE_SOFT,
51*4882a593Smuzhiyun HRTIMER_MODE_REL_SOFT = HRTIMER_MODE_REL | HRTIMER_MODE_SOFT,
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun HRTIMER_MODE_ABS_PINNED_SOFT = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_SOFT,
54*4882a593Smuzhiyun HRTIMER_MODE_REL_PINNED_SOFT = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_SOFT,
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun HRTIMER_MODE_ABS_HARD = HRTIMER_MODE_ABS | HRTIMER_MODE_HARD,
57*4882a593Smuzhiyun HRTIMER_MODE_REL_HARD = HRTIMER_MODE_REL | HRTIMER_MODE_HARD,
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun HRTIMER_MODE_ABS_PINNED_HARD = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_HARD,
60*4882a593Smuzhiyun HRTIMER_MODE_REL_PINNED_HARD = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_HARD,
61*4882a593Smuzhiyun };
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /*
64*4882a593Smuzhiyun * Return values for the callback function
65*4882a593Smuzhiyun */
66*4882a593Smuzhiyun enum hrtimer_restart {
67*4882a593Smuzhiyun HRTIMER_NORESTART, /* Timer is not restarted */
68*4882a593Smuzhiyun HRTIMER_RESTART, /* Timer must be restarted */
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun * Values to track state of the timer
73*4882a593Smuzhiyun *
74*4882a593Smuzhiyun * Possible states:
75*4882a593Smuzhiyun *
76*4882a593Smuzhiyun * 0x00 inactive
77*4882a593Smuzhiyun * 0x01 enqueued into rbtree
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun * The callback state is not part of the timer->state because clearing it would
80*4882a593Smuzhiyun * mean touching the timer after the callback, this makes it impossible to free
81*4882a593Smuzhiyun * the timer from the callback function.
82*4882a593Smuzhiyun *
83*4882a593Smuzhiyun * Therefore we track the callback state in:
84*4882a593Smuzhiyun *
85*4882a593Smuzhiyun * timer->base->cpu_base->running == timer
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * On SMP it is possible to have a "callback function running and enqueued"
88*4882a593Smuzhiyun * status. It happens for example when a posix timer expired and the callback
89*4882a593Smuzhiyun * queued a signal. Between dropping the lock which protects the posix timer
90*4882a593Smuzhiyun * and reacquiring the base lock of the hrtimer, another CPU can deliver the
91*4882a593Smuzhiyun * signal and rearm the timer.
92*4882a593Smuzhiyun *
93*4882a593Smuzhiyun * All state transitions are protected by cpu_base->lock.
94*4882a593Smuzhiyun */
95*4882a593Smuzhiyun #define HRTIMER_STATE_INACTIVE 0x00
96*4882a593Smuzhiyun #define HRTIMER_STATE_ENQUEUED 0x01
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /**
99*4882a593Smuzhiyun * struct hrtimer - the basic hrtimer structure
100*4882a593Smuzhiyun * @node: timerqueue node, which also manages node.expires,
101*4882a593Smuzhiyun * the absolute expiry time in the hrtimers internal
102*4882a593Smuzhiyun * representation. The time is related to the clock on
103*4882a593Smuzhiyun * which the timer is based. Is setup by adding
104*4882a593Smuzhiyun * slack to the _softexpires value. For non range timers
105*4882a593Smuzhiyun * identical to _softexpires.
106*4882a593Smuzhiyun * @_softexpires: the absolute earliest expiry time of the hrtimer.
107*4882a593Smuzhiyun * The time which was given as expiry time when the timer
108*4882a593Smuzhiyun * was armed.
109*4882a593Smuzhiyun * @function: timer expiry callback function
110*4882a593Smuzhiyun * @base: pointer to the timer base (per cpu and per clock)
111*4882a593Smuzhiyun * @state: state information (See bit values above)
112*4882a593Smuzhiyun * @is_rel: Set if the timer was armed relative
113*4882a593Smuzhiyun * @is_soft: Set if hrtimer will be expired in soft interrupt context.
114*4882a593Smuzhiyun * @is_hard: Set if hrtimer will be expired in hard interrupt context
115*4882a593Smuzhiyun * even on RT.
116*4882a593Smuzhiyun *
117*4882a593Smuzhiyun * The hrtimer structure must be initialized by hrtimer_init()
118*4882a593Smuzhiyun */
119*4882a593Smuzhiyun struct hrtimer {
120*4882a593Smuzhiyun struct timerqueue_node node;
121*4882a593Smuzhiyun ktime_t _softexpires;
122*4882a593Smuzhiyun enum hrtimer_restart (*function)(struct hrtimer *);
123*4882a593Smuzhiyun struct hrtimer_clock_base *base;
124*4882a593Smuzhiyun u8 state;
125*4882a593Smuzhiyun u8 is_rel;
126*4882a593Smuzhiyun u8 is_soft;
127*4882a593Smuzhiyun u8 is_hard;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun ANDROID_KABI_RESERVE(1);
130*4882a593Smuzhiyun };
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /**
133*4882a593Smuzhiyun * struct hrtimer_sleeper - simple sleeper structure
134*4882a593Smuzhiyun * @timer: embedded timer structure
135*4882a593Smuzhiyun * @task: task to wake up
136*4882a593Smuzhiyun *
137*4882a593Smuzhiyun * task is set to NULL, when the timer expires.
138*4882a593Smuzhiyun */
139*4882a593Smuzhiyun struct hrtimer_sleeper {
140*4882a593Smuzhiyun struct hrtimer timer;
141*4882a593Smuzhiyun struct task_struct *task;
142*4882a593Smuzhiyun };
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun #ifdef CONFIG_64BIT
145*4882a593Smuzhiyun # define __hrtimer_clock_base_align ____cacheline_aligned
146*4882a593Smuzhiyun #else
147*4882a593Smuzhiyun # define __hrtimer_clock_base_align
148*4882a593Smuzhiyun #endif
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /**
151*4882a593Smuzhiyun * struct hrtimer_clock_base - the timer base for a specific clock
152*4882a593Smuzhiyun * @cpu_base: per cpu clock base
153*4882a593Smuzhiyun * @index: clock type index for per_cpu support when moving a
154*4882a593Smuzhiyun * timer to a base on another cpu.
155*4882a593Smuzhiyun * @clockid: clock id for per_cpu support
156*4882a593Smuzhiyun * @seq: seqcount around __run_hrtimer
157*4882a593Smuzhiyun * @running: pointer to the currently running hrtimer
158*4882a593Smuzhiyun * @active: red black tree root node for the active timers
159*4882a593Smuzhiyun * @get_time: function to retrieve the current time of the clock
160*4882a593Smuzhiyun * @offset: offset of this clock to the monotonic base
161*4882a593Smuzhiyun */
162*4882a593Smuzhiyun struct hrtimer_clock_base {
163*4882a593Smuzhiyun struct hrtimer_cpu_base *cpu_base;
164*4882a593Smuzhiyun unsigned int index;
165*4882a593Smuzhiyun clockid_t clockid;
166*4882a593Smuzhiyun seqcount_raw_spinlock_t seq;
167*4882a593Smuzhiyun struct hrtimer *running;
168*4882a593Smuzhiyun struct timerqueue_head active;
169*4882a593Smuzhiyun ktime_t (*get_time)(void);
170*4882a593Smuzhiyun ktime_t offset;
171*4882a593Smuzhiyun } __hrtimer_clock_base_align;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun enum hrtimer_base_type {
174*4882a593Smuzhiyun HRTIMER_BASE_MONOTONIC,
175*4882a593Smuzhiyun HRTIMER_BASE_REALTIME,
176*4882a593Smuzhiyun HRTIMER_BASE_BOOTTIME,
177*4882a593Smuzhiyun HRTIMER_BASE_TAI,
178*4882a593Smuzhiyun HRTIMER_BASE_MONOTONIC_SOFT,
179*4882a593Smuzhiyun HRTIMER_BASE_REALTIME_SOFT,
180*4882a593Smuzhiyun HRTIMER_BASE_BOOTTIME_SOFT,
181*4882a593Smuzhiyun HRTIMER_BASE_TAI_SOFT,
182*4882a593Smuzhiyun HRTIMER_MAX_CLOCK_BASES,
183*4882a593Smuzhiyun };
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /**
186*4882a593Smuzhiyun * struct hrtimer_cpu_base - the per cpu clock bases
187*4882a593Smuzhiyun * @lock: lock protecting the base and associated clock bases
188*4882a593Smuzhiyun * and timers
189*4882a593Smuzhiyun * @cpu: cpu number
190*4882a593Smuzhiyun * @active_bases: Bitfield to mark bases with active timers
191*4882a593Smuzhiyun * @clock_was_set_seq: Sequence counter of clock was set events
192*4882a593Smuzhiyun * @hres_active: State of high resolution mode
193*4882a593Smuzhiyun * @in_hrtirq: hrtimer_interrupt() is currently executing
194*4882a593Smuzhiyun * @hang_detected: The last hrtimer interrupt detected a hang
195*4882a593Smuzhiyun * @softirq_activated: displays, if the softirq is raised - update of softirq
196*4882a593Smuzhiyun * related settings is not required then.
197*4882a593Smuzhiyun * @nr_events: Total number of hrtimer interrupt events
198*4882a593Smuzhiyun * @nr_retries: Total number of hrtimer interrupt retries
199*4882a593Smuzhiyun * @nr_hangs: Total number of hrtimer interrupt hangs
200*4882a593Smuzhiyun * @max_hang_time: Maximum time spent in hrtimer_interrupt
201*4882a593Smuzhiyun * @softirq_expiry_lock: Lock which is taken while softirq based hrtimer are
202*4882a593Smuzhiyun * expired
203*4882a593Smuzhiyun * @timer_waiters: A hrtimer_cancel() invocation waits for the timer
204*4882a593Smuzhiyun * callback to finish.
205*4882a593Smuzhiyun * @expires_next: absolute time of the next event, is required for remote
206*4882a593Smuzhiyun * hrtimer enqueue; it is the total first expiry time (hard
207*4882a593Smuzhiyun * and soft hrtimer are taken into account)
208*4882a593Smuzhiyun * @next_timer: Pointer to the first expiring timer
209*4882a593Smuzhiyun * @softirq_expires_next: Time to check, if soft queues needs also to be expired
210*4882a593Smuzhiyun * @softirq_next_timer: Pointer to the first expiring softirq based timer
211*4882a593Smuzhiyun * @clock_base: array of clock bases for this cpu
212*4882a593Smuzhiyun *
213*4882a593Smuzhiyun * Note: next_timer is just an optimization for __remove_hrtimer().
214*4882a593Smuzhiyun * Do not dereference the pointer because it is not reliable on
215*4882a593Smuzhiyun * cross cpu removals.
216*4882a593Smuzhiyun */
217*4882a593Smuzhiyun struct hrtimer_cpu_base {
218*4882a593Smuzhiyun raw_spinlock_t lock;
219*4882a593Smuzhiyun unsigned int cpu;
220*4882a593Smuzhiyun unsigned int active_bases;
221*4882a593Smuzhiyun unsigned int clock_was_set_seq;
222*4882a593Smuzhiyun unsigned int hres_active : 1,
223*4882a593Smuzhiyun in_hrtirq : 1,
224*4882a593Smuzhiyun hang_detected : 1,
225*4882a593Smuzhiyun softirq_activated : 1;
226*4882a593Smuzhiyun #ifdef CONFIG_HIGH_RES_TIMERS
227*4882a593Smuzhiyun unsigned int nr_events;
228*4882a593Smuzhiyun unsigned short nr_retries;
229*4882a593Smuzhiyun unsigned short nr_hangs;
230*4882a593Smuzhiyun unsigned int max_hang_time;
231*4882a593Smuzhiyun #endif
232*4882a593Smuzhiyun #ifdef CONFIG_PREEMPT_RT
233*4882a593Smuzhiyun spinlock_t softirq_expiry_lock;
234*4882a593Smuzhiyun atomic_t timer_waiters;
235*4882a593Smuzhiyun #endif
236*4882a593Smuzhiyun ktime_t expires_next;
237*4882a593Smuzhiyun struct hrtimer *next_timer;
238*4882a593Smuzhiyun ktime_t softirq_expires_next;
239*4882a593Smuzhiyun struct hrtimer *softirq_next_timer;
240*4882a593Smuzhiyun struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
241*4882a593Smuzhiyun } ____cacheline_aligned;
242*4882a593Smuzhiyun
hrtimer_set_expires(struct hrtimer * timer,ktime_t time)243*4882a593Smuzhiyun static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun timer->node.expires = time;
246*4882a593Smuzhiyun timer->_softexpires = time;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun
hrtimer_set_expires_range(struct hrtimer * timer,ktime_t time,ktime_t delta)249*4882a593Smuzhiyun static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun timer->_softexpires = time;
252*4882a593Smuzhiyun timer->node.expires = ktime_add_safe(time, delta);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
hrtimer_set_expires_range_ns(struct hrtimer * timer,ktime_t time,u64 delta)255*4882a593Smuzhiyun static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, u64 delta)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun timer->_softexpires = time;
258*4882a593Smuzhiyun timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta));
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
hrtimer_set_expires_tv64(struct hrtimer * timer,s64 tv64)261*4882a593Smuzhiyun static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun timer->node.expires = tv64;
264*4882a593Smuzhiyun timer->_softexpires = tv64;
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun
hrtimer_add_expires(struct hrtimer * timer,ktime_t time)267*4882a593Smuzhiyun static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun timer->node.expires = ktime_add_safe(timer->node.expires, time);
270*4882a593Smuzhiyun timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun
hrtimer_add_expires_ns(struct hrtimer * timer,u64 ns)273*4882a593Smuzhiyun static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun timer->node.expires = ktime_add_ns(timer->node.expires, ns);
276*4882a593Smuzhiyun timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun
hrtimer_get_expires(const struct hrtimer * timer)279*4882a593Smuzhiyun static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun return timer->node.expires;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
hrtimer_get_softexpires(const struct hrtimer * timer)284*4882a593Smuzhiyun static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun return timer->_softexpires;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun
hrtimer_get_expires_tv64(const struct hrtimer * timer)289*4882a593Smuzhiyun static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun return timer->node.expires;
292*4882a593Smuzhiyun }
hrtimer_get_softexpires_tv64(const struct hrtimer * timer)293*4882a593Smuzhiyun static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun return timer->_softexpires;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun
hrtimer_get_expires_ns(const struct hrtimer * timer)298*4882a593Smuzhiyun static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
299*4882a593Smuzhiyun {
300*4882a593Smuzhiyun return ktime_to_ns(timer->node.expires);
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun
hrtimer_expires_remaining(const struct hrtimer * timer)303*4882a593Smuzhiyun static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun return ktime_sub(timer->node.expires, timer->base->get_time());
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun
hrtimer_cb_get_time(struct hrtimer * timer)308*4882a593Smuzhiyun static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun return timer->base->get_time();
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun
hrtimer_is_hres_active(struct hrtimer * timer)313*4882a593Smuzhiyun static inline int hrtimer_is_hres_active(struct hrtimer *timer)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ?
316*4882a593Smuzhiyun timer->base->cpu_base->hres_active : 0;
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun #ifdef CONFIG_HIGH_RES_TIMERS
320*4882a593Smuzhiyun struct clock_event_device;
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun extern void hrtimer_interrupt(struct clock_event_device *dev);
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun extern unsigned int hrtimer_resolution;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun #else
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun #define hrtimer_resolution (unsigned int)LOW_RES_NSEC
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun #endif
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun static inline ktime_t
__hrtimer_expires_remaining_adjusted(const struct hrtimer * timer,ktime_t now)333*4882a593Smuzhiyun __hrtimer_expires_remaining_adjusted(const struct hrtimer *timer, ktime_t now)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun ktime_t rem = ktime_sub(timer->node.expires, now);
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun /*
338*4882a593Smuzhiyun * Adjust relative timers for the extra we added in
339*4882a593Smuzhiyun * hrtimer_start_range_ns() to prevent short timeouts.
340*4882a593Smuzhiyun */
341*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_TIME_LOW_RES) && timer->is_rel)
342*4882a593Smuzhiyun rem -= hrtimer_resolution;
343*4882a593Smuzhiyun return rem;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun static inline ktime_t
hrtimer_expires_remaining_adjusted(const struct hrtimer * timer)347*4882a593Smuzhiyun hrtimer_expires_remaining_adjusted(const struct hrtimer *timer)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun return __hrtimer_expires_remaining_adjusted(timer,
350*4882a593Smuzhiyun timer->base->get_time());
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun #ifdef CONFIG_TIMERFD
354*4882a593Smuzhiyun extern void timerfd_clock_was_set(void);
355*4882a593Smuzhiyun #else
timerfd_clock_was_set(void)356*4882a593Smuzhiyun static inline void timerfd_clock_was_set(void) { }
357*4882a593Smuzhiyun #endif
358*4882a593Smuzhiyun extern void hrtimers_resume(void);
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun #ifdef CONFIG_PREEMPT_RT
363*4882a593Smuzhiyun void hrtimer_cancel_wait_running(const struct hrtimer *timer);
364*4882a593Smuzhiyun #else
hrtimer_cancel_wait_running(struct hrtimer * timer)365*4882a593Smuzhiyun static inline void hrtimer_cancel_wait_running(struct hrtimer *timer)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun cpu_relax();
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun #endif
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun /* Exported timer functions: */
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun /* Initialize timers: */
374*4882a593Smuzhiyun extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
375*4882a593Smuzhiyun enum hrtimer_mode mode);
376*4882a593Smuzhiyun extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id,
377*4882a593Smuzhiyun enum hrtimer_mode mode);
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
380*4882a593Smuzhiyun extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
381*4882a593Smuzhiyun enum hrtimer_mode mode);
382*4882a593Smuzhiyun extern void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
383*4882a593Smuzhiyun clockid_t clock_id,
384*4882a593Smuzhiyun enum hrtimer_mode mode);
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
387*4882a593Smuzhiyun #else
hrtimer_init_on_stack(struct hrtimer * timer,clockid_t which_clock,enum hrtimer_mode mode)388*4882a593Smuzhiyun static inline void hrtimer_init_on_stack(struct hrtimer *timer,
389*4882a593Smuzhiyun clockid_t which_clock,
390*4882a593Smuzhiyun enum hrtimer_mode mode)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun hrtimer_init(timer, which_clock, mode);
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun
hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper * sl,clockid_t clock_id,enum hrtimer_mode mode)395*4882a593Smuzhiyun static inline void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl,
396*4882a593Smuzhiyun clockid_t clock_id,
397*4882a593Smuzhiyun enum hrtimer_mode mode)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun hrtimer_init_sleeper(sl, clock_id, mode);
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun
destroy_hrtimer_on_stack(struct hrtimer * timer)402*4882a593Smuzhiyun static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
403*4882a593Smuzhiyun #endif
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun /* Basic timer operations: */
406*4882a593Smuzhiyun extern void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
407*4882a593Smuzhiyun u64 range_ns, const enum hrtimer_mode mode);
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun /**
410*4882a593Smuzhiyun * hrtimer_start - (re)start an hrtimer
411*4882a593Smuzhiyun * @timer: the timer to be added
412*4882a593Smuzhiyun * @tim: expiry time
413*4882a593Smuzhiyun * @mode: timer mode: absolute (HRTIMER_MODE_ABS) or
414*4882a593Smuzhiyun * relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED);
415*4882a593Smuzhiyun * softirq based mode is considered for debug purpose only!
416*4882a593Smuzhiyun */
hrtimer_start(struct hrtimer * timer,ktime_t tim,const enum hrtimer_mode mode)417*4882a593Smuzhiyun static inline void hrtimer_start(struct hrtimer *timer, ktime_t tim,
418*4882a593Smuzhiyun const enum hrtimer_mode mode)
419*4882a593Smuzhiyun {
420*4882a593Smuzhiyun hrtimer_start_range_ns(timer, tim, 0, mode);
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun extern int hrtimer_cancel(struct hrtimer *timer);
424*4882a593Smuzhiyun extern int hrtimer_try_to_cancel(struct hrtimer *timer);
425*4882a593Smuzhiyun
hrtimer_start_expires(struct hrtimer * timer,enum hrtimer_mode mode)426*4882a593Smuzhiyun static inline void hrtimer_start_expires(struct hrtimer *timer,
427*4882a593Smuzhiyun enum hrtimer_mode mode)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun u64 delta;
430*4882a593Smuzhiyun ktime_t soft, hard;
431*4882a593Smuzhiyun soft = hrtimer_get_softexpires(timer);
432*4882a593Smuzhiyun hard = hrtimer_get_expires(timer);
433*4882a593Smuzhiyun delta = ktime_to_ns(ktime_sub(hard, soft));
434*4882a593Smuzhiyun hrtimer_start_range_ns(timer, soft, delta, mode);
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl,
438*4882a593Smuzhiyun enum hrtimer_mode mode);
439*4882a593Smuzhiyun
hrtimer_restart(struct hrtimer * timer)440*4882a593Smuzhiyun static inline void hrtimer_restart(struct hrtimer *timer)
441*4882a593Smuzhiyun {
442*4882a593Smuzhiyun hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun /* Query timers: */
446*4882a593Smuzhiyun extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
447*4882a593Smuzhiyun
hrtimer_get_remaining(const struct hrtimer * timer)448*4882a593Smuzhiyun static inline ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun return __hrtimer_get_remaining(timer, false);
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun extern u64 hrtimer_get_next_event(void);
454*4882a593Smuzhiyun extern u64 hrtimer_next_event_without(const struct hrtimer *exclude);
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun extern bool hrtimer_active(const struct hrtimer *timer);
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun /**
459*4882a593Smuzhiyun * hrtimer_is_queued = check, whether the timer is on one of the queues
460*4882a593Smuzhiyun * @timer: Timer to check
461*4882a593Smuzhiyun *
462*4882a593Smuzhiyun * Returns: True if the timer is queued, false otherwise
463*4882a593Smuzhiyun *
464*4882a593Smuzhiyun * The function can be used lockless, but it gives only a current snapshot.
465*4882a593Smuzhiyun */
hrtimer_is_queued(struct hrtimer * timer)466*4882a593Smuzhiyun static inline bool hrtimer_is_queued(struct hrtimer *timer)
467*4882a593Smuzhiyun {
468*4882a593Smuzhiyun /* The READ_ONCE pairs with the update functions of timer->state */
469*4882a593Smuzhiyun return !!(READ_ONCE(timer->state) & HRTIMER_STATE_ENQUEUED);
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun /*
473*4882a593Smuzhiyun * Helper function to check, whether the timer is running the callback
474*4882a593Smuzhiyun * function
475*4882a593Smuzhiyun */
hrtimer_callback_running(struct hrtimer * timer)476*4882a593Smuzhiyun static inline int hrtimer_callback_running(struct hrtimer *timer)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun return timer->base->running == timer;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun /* Forward a hrtimer so it expires after now: */
482*4882a593Smuzhiyun extern u64
483*4882a593Smuzhiyun hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun /**
486*4882a593Smuzhiyun * hrtimer_forward_now - forward the timer expiry so it expires after now
487*4882a593Smuzhiyun * @timer: hrtimer to forward
488*4882a593Smuzhiyun * @interval: the interval to forward
489*4882a593Smuzhiyun *
490*4882a593Smuzhiyun * Forward the timer expiry so it will expire after the current time
491*4882a593Smuzhiyun * of the hrtimer clock base. Returns the number of overruns.
492*4882a593Smuzhiyun *
493*4882a593Smuzhiyun * Can be safely called from the callback function of @timer. If
494*4882a593Smuzhiyun * called from other contexts @timer must neither be enqueued nor
495*4882a593Smuzhiyun * running the callback and the caller needs to take care of
496*4882a593Smuzhiyun * serialization.
497*4882a593Smuzhiyun *
498*4882a593Smuzhiyun * Note: This only updates the timer expiry value and does not requeue
499*4882a593Smuzhiyun * the timer.
500*4882a593Smuzhiyun */
hrtimer_forward_now(struct hrtimer * timer,ktime_t interval)501*4882a593Smuzhiyun static inline u64 hrtimer_forward_now(struct hrtimer *timer,
502*4882a593Smuzhiyun ktime_t interval)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun return hrtimer_forward(timer, timer->base->get_time(), interval);
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun /* Precise sleep: */
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun extern int nanosleep_copyout(struct restart_block *, struct timespec64 *);
510*4882a593Smuzhiyun extern long hrtimer_nanosleep(ktime_t rqtp, const enum hrtimer_mode mode,
511*4882a593Smuzhiyun const clockid_t clockid);
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun extern int schedule_hrtimeout_range(ktime_t *expires, u64 delta,
514*4882a593Smuzhiyun const enum hrtimer_mode mode);
515*4882a593Smuzhiyun extern int schedule_hrtimeout_range_clock(ktime_t *expires,
516*4882a593Smuzhiyun u64 delta,
517*4882a593Smuzhiyun const enum hrtimer_mode mode,
518*4882a593Smuzhiyun clockid_t clock_id);
519*4882a593Smuzhiyun extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun /* Soft interrupt function to run the hrtimer queues: */
522*4882a593Smuzhiyun extern void hrtimer_run_queues(void);
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun /* Bootup initialization: */
525*4882a593Smuzhiyun extern void __init hrtimers_init(void);
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun /* Show pending timers: */
528*4882a593Smuzhiyun extern void sysrq_timer_list_show(void);
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun int hrtimers_prepare_cpu(unsigned int cpu);
531*4882a593Smuzhiyun #ifdef CONFIG_HOTPLUG_CPU
532*4882a593Smuzhiyun int hrtimers_dead_cpu(unsigned int cpu);
533*4882a593Smuzhiyun #else
534*4882a593Smuzhiyun #define hrtimers_dead_cpu NULL
535*4882a593Smuzhiyun #endif
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun #endif
538