Commit | Line | Data |
---|---|---|
c0a31329 TG |
1 | /* |
2 | * include/linux/hrtimer.h | |
3 | * | |
4 | * hrtimers - High-resolution kernel timers | |
5 | * | |
6 | * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de> | |
7 | * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar | |
8 | * | |
9 | * data type definitions, declarations, prototypes | |
10 | * | |
11 | * Started by: Thomas Gleixner and Ingo Molnar | |
12 | * | |
13 | * For licencing details see kernel-base/COPYING | |
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 | ||
3c8aa39d TG |
24 | struct hrtimer_clock_base; |
25 | struct hrtimer_cpu_base; | |
26 | ||
c0a31329 TG |
27 | /* |
28 | * Mode arguments of xxx_hrtimer functions: | |
29 | */ | |
30 | enum hrtimer_mode { | |
c9cb2e3d TG |
31 | HRTIMER_MODE_ABS, /* Time value is absolute */ |
32 | HRTIMER_MODE_REL, /* Time value is relative to now */ | |
c0a31329 TG |
33 | }; |
34 | ||
c9cb2e3d TG |
35 | /* |
36 | * Return values for the callback function | |
37 | */ | |
c0a31329 | 38 | enum hrtimer_restart { |
c9cb2e3d TG |
39 | HRTIMER_NORESTART, /* Timer is not restarted */ |
40 | HRTIMER_RESTART, /* Timer must be restarted */ | |
c0a31329 TG |
41 | }; |
42 | ||
303e967f TG |
43 | /* |
44 | * Bit values to track state of the timer | |
45 | * | |
46 | * Possible states: | |
47 | * | |
48 | * 0x00 inactive | |
49 | * 0x01 enqueued into rbtree | |
50 | * 0x02 callback function running | |
51 | * 0x03 callback function running and enqueued | |
52 | * (was requeued on another CPU) | |
53 | * | |
54 | * The "callback function running and enqueued" status is only possible on | |
55 | * SMP. It happens for example when a posix timer expired and the callback | |
56 | * queued a signal. Between dropping the lock which protects the posix timer | |
57 | * and reacquiring the base lock of the hrtimer, another CPU can deliver the | |
58 | * signal and rearm the timer. We have to preserve the callback running state, | |
59 | * as otherwise the timer could be removed before the softirq code finishes the | |
60 | * the handling of the timer. | |
61 | * | |
62 | * The HRTIMER_STATE_ENQUEUE bit is always or'ed to the current state to | |
63 | * preserve the HRTIMER_STATE_CALLBACK bit in the above scenario. | |
64 | * | |
65 | * All state transitions are protected by cpu_base->lock. | |
66 | */ | |
67 | #define HRTIMER_STATE_INACTIVE 0x00 | |
68 | #define HRTIMER_STATE_ENQUEUED 0x01 | |
69 | #define HRTIMER_STATE_CALLBACK 0x02 | |
70 | ||
c0a31329 TG |
71 | /** |
72 | * struct hrtimer - the basic hrtimer structure | |
c0a31329 | 73 | * @node: red black tree node for time ordered insertion |
c0a31329 TG |
74 | * @expires: the absolute expiry time in the hrtimers internal |
75 | * representation. The time is related to the clock on | |
76 | * which the timer is based. | |
c0a31329 | 77 | * @function: timer expiry callback function |
c0a31329 | 78 | * @base: pointer to the timer base (per cpu and per clock) |
303e967f | 79 | * @state: state information (See bit values above) |
c0a31329 TG |
80 | * |
81 | * The hrtimer structure must be initialized by init_hrtimer_#CLOCKTYPE() | |
82 | */ | |
83 | struct hrtimer { | |
3c8aa39d TG |
84 | struct rb_node node; |
85 | ktime_t expires; | |
86 | enum hrtimer_restart (*function)(struct hrtimer *); | |
87 | struct hrtimer_clock_base *base; | |
303e967f | 88 | unsigned long state; |
c0a31329 TG |
89 | }; |
90 | ||
00362e33 TG |
91 | /** |
92 | * struct hrtimer_sleeper - simple sleeper structure | |
00362e33 TG |
93 | * @timer: embedded timer structure |
94 | * @task: task to wake up | |
95 | * | |
96 | * task is set to NULL, when the timer expires. | |
97 | */ | |
98 | struct hrtimer_sleeper { | |
99 | struct hrtimer timer; | |
100 | struct task_struct *task; | |
101 | }; | |
102 | ||
c0a31329 TG |
103 | /** |
104 | * struct hrtimer_base - the timer base for a specific clock | |
3c8aa39d TG |
105 | * @index: clock type index for per_cpu support when moving a |
106 | * timer to a base on another cpu. | |
92127c7a TG |
107 | * @active: red black tree root node for the active timers |
108 | * @first: pointer to the timer node which expires first | |
109 | * @resolution: the resolution of the clock, in nanoseconds | |
110 | * @get_time: function to retrieve the current time of the clock | |
a580290c | 111 | * @get_softirq_time: function to retrieve the current time from the softirq |
92127c7a | 112 | * @softirq_time: the time when running the hrtimer queue in the softirq |
c0a31329 | 113 | */ |
3c8aa39d TG |
114 | struct hrtimer_clock_base { |
115 | struct hrtimer_cpu_base *cpu_base; | |
c0a31329 | 116 | clockid_t index; |
c0a31329 | 117 | struct rb_root active; |
288867ec | 118 | struct rb_node *first; |
e2787630 | 119 | ktime_t resolution; |
c0a31329 | 120 | ktime_t (*get_time)(void); |
92127c7a | 121 | ktime_t (*get_softirq_time)(void); |
92127c7a | 122 | ktime_t softirq_time; |
3c8aa39d TG |
123 | }; |
124 | ||
125 | #define HRTIMER_MAX_CLOCK_BASES 2 | |
126 | ||
127 | /* | |
128 | * struct hrtimer_cpu_base - the per cpu clock bases | |
129 | * @lock: lock protecting the base and associated clock bases | |
130 | * and timers | |
131 | * @lock_key: the lock_class_key for use with lockdep | |
132 | * @clock_base: array of clock bases for this cpu | |
133 | * @curr_timer: the timer which is executing a callback right now | |
134 | */ | |
135 | struct hrtimer_cpu_base { | |
136 | spinlock_t lock; | |
137 | struct lock_class_key lock_key; | |
138 | struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES]; | |
c0a31329 TG |
139 | }; |
140 | ||
becf8b5d TG |
141 | /* |
142 | * clock_was_set() is a NOP for non- high-resolution systems. The | |
143 | * time-sorted order guarantees that a timer does not expire early and | |
144 | * is expired in the next softirq when the clock was advanced. | |
145 | */ | |
146 | #define clock_was_set() do { } while (0) | |
147 | ||
c0a31329 TG |
148 | /* Exported timer functions: */ |
149 | ||
150 | /* Initialize timers: */ | |
7978672c GA |
151 | extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock, |
152 | enum hrtimer_mode mode); | |
c0a31329 TG |
153 | |
154 | /* Basic timer operations: */ | |
155 | extern int hrtimer_start(struct hrtimer *timer, ktime_t tim, | |
156 | const enum hrtimer_mode mode); | |
157 | extern int hrtimer_cancel(struct hrtimer *timer); | |
158 | extern int hrtimer_try_to_cancel(struct hrtimer *timer); | |
159 | ||
c9cb2e3d TG |
160 | static inline int hrtimer_restart(struct hrtimer *timer) |
161 | { | |
162 | return hrtimer_start(timer, timer->expires, HRTIMER_MODE_ABS); | |
163 | } | |
c0a31329 TG |
164 | |
165 | /* Query timers: */ | |
166 | extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer); | |
167 | extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp); | |
168 | ||
69239749 TL |
169 | #ifdef CONFIG_NO_IDLE_HZ |
170 | extern ktime_t hrtimer_get_next_event(void); | |
171 | #endif | |
172 | ||
303e967f TG |
173 | /* |
174 | * A timer is active, when it is enqueued into the rbtree or the callback | |
175 | * function is running. | |
176 | */ | |
c0a31329 TG |
177 | static inline int hrtimer_active(const struct hrtimer *timer) |
178 | { | |
303e967f | 179 | return timer->state != HRTIMER_STATE_INACTIVE; |
c0a31329 TG |
180 | } |
181 | ||
182 | /* Forward a hrtimer so it expires after now: */ | |
44f21475 RZ |
183 | extern unsigned long |
184 | hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval); | |
c0a31329 | 185 | |
10c94ec1 TG |
186 | /* Precise sleep: */ |
187 | extern long hrtimer_nanosleep(struct timespec *rqtp, | |
188 | struct timespec __user *rmtp, | |
189 | const enum hrtimer_mode mode, | |
190 | const clockid_t clockid); | |
1711ef38 | 191 | extern long hrtimer_nanosleep_restart(struct restart_block *restart_block); |
10c94ec1 | 192 | |
00362e33 TG |
193 | extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, |
194 | struct task_struct *tsk); | |
195 | ||
c0a31329 TG |
196 | /* Soft interrupt function to run the hrtimer queues: */ |
197 | extern void hrtimer_run_queues(void); | |
198 | ||
411187fb JS |
199 | /* Resume notification */ |
200 | void hrtimer_notify_resume(void); | |
201 | ||
c0a31329 TG |
202 | /* Bootup initialization: */ |
203 | extern void __init hrtimers_init(void); | |
204 | ||
205 | #endif |