1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#ifndef _LINUX_HRTIMER_H
16#define _LINUX_HRTIMER_H
17
18#include <linux/rbtree.h>
19#include <linux/ktime.h>
20#include <linux/init.h>
21#include <linux/list.h>
22#include <linux/wait.h>
23#include <linux/percpu.h>
24
25
26struct hrtimer_clock_base;
27struct hrtimer_cpu_base;
28
29
30
31
32enum hrtimer_mode {
33 HRTIMER_MODE_ABS,
34 HRTIMER_MODE_REL,
35};
36
37
38
39
40enum hrtimer_restart {
41 HRTIMER_NORESTART,
42 HRTIMER_RESTART,
43};
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71#define HRTIMER_STATE_INACTIVE 0x00
72#define HRTIMER_STATE_ENQUEUED 0x01
73#define HRTIMER_STATE_CALLBACK 0x02
74#define HRTIMER_STATE_MIGRATE 0x04
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100struct hrtimer {
101 struct rb_node node;
102 ktime_t _expires;
103 ktime_t _softexpires;
104 enum hrtimer_restart (*function)(struct hrtimer *);
105 struct hrtimer_clock_base *base;
106 unsigned long state;
107 struct list_head cb_entry;
108#ifdef CONFIG_TIMER_STATS
109 int start_pid;
110 void *start_site;
111 char start_comm[16];
112#endif
113};
114
115
116
117
118
119
120
121
122struct hrtimer_sleeper {
123 struct hrtimer timer;
124 struct task_struct *task;
125};
126
127
128
129
130
131
132
133
134
135
136
137
138
139struct hrtimer_clock_base {
140 struct hrtimer_cpu_base *cpu_base;
141 clockid_t index;
142 struct rb_root active;
143 struct rb_node *first;
144 ktime_t resolution;
145 ktime_t (*get_time)(void);
146 ktime_t softirq_time;
147#ifdef CONFIG_HIGH_RES_TIMERS
148 ktime_t offset;
149#endif
150};
151
152#define HRTIMER_MAX_CLOCK_BASES 2
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168struct hrtimer_cpu_base {
169 spinlock_t lock;
170 struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
171#ifdef CONFIG_HIGH_RES_TIMERS
172 ktime_t expires_next;
173 int hres_active;
174 unsigned long nr_events;
175#endif
176};
177
178static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
179{
180 timer->_expires = time;
181 timer->_softexpires = time;
182}
183
184static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
185{
186 timer->_softexpires = time;
187 timer->_expires = ktime_add_safe(time, delta);
188}
189
190static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, unsigned long delta)
191{
192 timer->_softexpires = time;
193 timer->_expires = ktime_add_safe(time, ns_to_ktime(delta));
194}
195
196static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
197{
198 timer->_expires.tv64 = tv64;
199 timer->_softexpires.tv64 = tv64;
200}
201
202static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
203{
204 timer->_expires = ktime_add_safe(timer->_expires, time);
205 timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
206}
207
208static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns)
209{
210 timer->_expires = ktime_add_ns(timer->_expires, ns);
211 timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
212}
213
214static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
215{
216 return timer->_expires;
217}
218
219static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
220{
221 return timer->_softexpires;
222}
223
224static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
225{
226 return timer->_expires.tv64;
227}
228static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
229{
230 return timer->_softexpires.tv64;
231}
232
233static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
234{
235 return ktime_to_ns(timer->_expires);
236}
237
238static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
239{
240 return ktime_sub(timer->_expires, timer->base->get_time());
241}
242
243#ifdef CONFIG_HIGH_RES_TIMERS
244struct clock_event_device;
245
246extern void clock_was_set(void);
247extern void hres_timers_resume(void);
248extern void hrtimer_interrupt(struct clock_event_device *dev);
249
250
251
252
253static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
254{
255 return timer->base->get_time();
256}
257
258static inline int hrtimer_is_hres_active(struct hrtimer *timer)
259{
260 return timer->base->cpu_base->hres_active;
261}
262
263extern void hrtimer_peek_ahead_timers(void);
264
265
266
267
268
269
270
271# define HIGH_RES_NSEC 1
272# define KTIME_HIGH_RES (ktime_t) { .tv64 = HIGH_RES_NSEC }
273# define MONOTONIC_RES_NSEC HIGH_RES_NSEC
274# define KTIME_MONOTONIC_RES KTIME_HIGH_RES
275
276#else
277
278# define MONOTONIC_RES_NSEC LOW_RES_NSEC
279# define KTIME_MONOTONIC_RES KTIME_LOW_RES
280
281
282
283
284
285
286static inline void clock_was_set(void) { }
287static inline void hrtimer_peek_ahead_timers(void) { }
288
289static inline void hres_timers_resume(void) { }
290
291
292
293
294
295static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
296{
297 return timer->base->softirq_time;
298}
299
300static inline int hrtimer_is_hres_active(struct hrtimer *timer)
301{
302 return 0;
303}
304#endif
305
306extern ktime_t ktime_get(void);
307extern ktime_t ktime_get_real(void);
308
309
310DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
311
312
313
314
315
316extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
317 enum hrtimer_mode mode);
318
319#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
320extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
321 enum hrtimer_mode mode);
322
323extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
324#else
325static inline void hrtimer_init_on_stack(struct hrtimer *timer,
326 clockid_t which_clock,
327 enum hrtimer_mode mode)
328{
329 hrtimer_init(timer, which_clock, mode);
330}
331static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
332#endif
333
334
335extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
336 const enum hrtimer_mode mode);
337extern int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
338 unsigned long range_ns, const enum hrtimer_mode mode);
339extern int hrtimer_cancel(struct hrtimer *timer);
340extern int hrtimer_try_to_cancel(struct hrtimer *timer);
341
342static inline int hrtimer_start_expires(struct hrtimer *timer,
343 enum hrtimer_mode mode)
344{
345 unsigned long delta;
346 ktime_t soft, hard;
347 soft = hrtimer_get_softexpires(timer);
348 hard = hrtimer_get_expires(timer);
349 delta = ktime_to_ns(ktime_sub(hard, soft));
350 return hrtimer_start_range_ns(timer, soft, delta, mode);
351}
352
353static inline int hrtimer_restart(struct hrtimer *timer)
354{
355 return hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
356}
357
358
359extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
360extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
361
362extern ktime_t hrtimer_get_next_event(void);
363
364
365
366
367
368static inline int hrtimer_active(const struct hrtimer *timer)
369{
370 return timer->state != HRTIMER_STATE_INACTIVE;
371}
372
373
374
375
376static inline int hrtimer_is_queued(struct hrtimer *timer)
377{
378 return timer->state & HRTIMER_STATE_ENQUEUED;
379}
380
381
382
383
384
385static inline int hrtimer_callback_running(struct hrtimer *timer)
386{
387 return timer->state & HRTIMER_STATE_CALLBACK;
388}
389
390
391extern u64
392hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
393
394
395static inline u64 hrtimer_forward_now(struct hrtimer *timer,
396 ktime_t interval)
397{
398 return hrtimer_forward(timer, timer->base->get_time(), interval);
399}
400
401
402extern long hrtimer_nanosleep(struct timespec *rqtp,
403 struct timespec __user *rmtp,
404 const enum hrtimer_mode mode,
405 const clockid_t clockid);
406extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
407
408extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
409 struct task_struct *tsk);
410
411extern int schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
412 const enum hrtimer_mode mode);
413extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
414
415
416extern void hrtimer_run_queues(void);
417extern void hrtimer_run_pending(void);
418
419
420extern void __init hrtimers_init(void);
421
422#if BITS_PER_LONG < 64
423extern u64 ktime_divns(const ktime_t kt, s64 div);
424#else
425# define ktime_divns(kt, div) (u64)((kt).tv64 / (div))
426#endif
427
428
429extern void sysrq_timer_list_show(void);
430
431
432
433
434#ifdef CONFIG_TIMER_STATS
435
436extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
437 void *timerf, char *comm,
438 unsigned int timer_flag);
439
440static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
441{
442 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
443 timer->function, timer->start_comm, 0);
444}
445
446extern void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer,
447 void *addr);
448
449static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
450{
451 __timer_stats_hrtimer_set_start_info(timer, __builtin_return_address(0));
452}
453
454static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
455{
456 timer->start_site = NULL;
457}
458#else
459static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
460{
461}
462
463static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
464{
465}
466
467static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
468{
469}
470#endif
471
472#endif
473