8250: fix boot hang with serial console when using with Serial Over Lan port
[sfrench/cifs-2.6.git] / include / linux / hrtimer.h
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 #include <linux/percpu.h>
24
25
26 struct hrtimer_clock_base;
27 struct hrtimer_cpu_base;
28
29 /*
30  * Mode arguments of xxx_hrtimer functions:
31  */
32 enum hrtimer_mode {
33         HRTIMER_MODE_ABS,       /* Time value is absolute */
34         HRTIMER_MODE_REL,       /* Time value is relative to now */
35 };
36
37 /*
38  * Return values for the callback function
39  */
40 enum hrtimer_restart {
41         HRTIMER_NORESTART,      /* Timer is not restarted */
42         HRTIMER_RESTART,        /* Timer must be restarted */
43 };
44
45 /*
46  * Values to track state of the timer
47  *
48  * Possible states:
49  *
50  * 0x00         inactive
51  * 0x01         enqueued into rbtree
52  * 0x02         callback function running
53  *
54  * Special cases:
55  * 0x03         callback function running and enqueued
56  *              (was requeued on another CPU)
57  * 0x09         timer was migrated on CPU hotunplug
58  * The "callback function running and enqueued" status is only possible on
59  * SMP. It happens for example when a posix timer expired and the callback
60  * queued a signal. Between dropping the lock which protects the posix timer
61  * and reacquiring the base lock of the hrtimer, another CPU can deliver the
62  * signal and rearm the timer. We have to preserve the callback running state,
63  * as otherwise the timer could be removed before the softirq code finishes the
64  * the handling of the timer.
65  *
66  * The HRTIMER_STATE_ENQUEUED bit is always or'ed to the current state to
67  * preserve the HRTIMER_STATE_CALLBACK bit in the above scenario.
68  *
69  * All state transitions are protected by cpu_base->lock.
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  * struct hrtimer - the basic hrtimer structure
78  * @node:       red black tree node for time ordered insertion
79  * @_expires:   the absolute expiry time in the hrtimers internal
80  *              representation. The time is related to the clock on
81  *              which the timer is based. Is setup by adding
82  *              slack to the _softexpires value. For non range timers
83  *              identical to _softexpires.
84  * @_softexpires: the absolute earliest expiry time of the hrtimer.
85  *              The time which was given as expiry time when the timer
86  *              was armed.
87  * @function:   timer expiry callback function
88  * @base:       pointer to the timer base (per cpu and per clock)
89  * @state:      state information (See bit values above)
90  * @cb_entry:   list head to enqueue an expired timer into the callback list
91  * @start_site: timer statistics field to store the site where the timer
92  *              was started
93  * @start_comm: timer statistics field to store the name of the process which
94  *              started the timer
95  * @start_pid: timer statistics field to store the pid of the task which
96  *              started the timer
97  *
98  * The hrtimer structure must be initialized by hrtimer_init()
99  */
100 struct 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  * struct hrtimer_sleeper - simple sleeper structure
117  * @timer:      embedded timer structure
118  * @task:       task to wake up
119  *
120  * task is set to NULL, when the timer expires.
121  */
122 struct hrtimer_sleeper {
123         struct hrtimer timer;
124         struct task_struct *task;
125 };
126
127 /**
128  * struct hrtimer_clock_base - the timer base for a specific clock
129  * @cpu_base:           per cpu clock base
130  * @index:              clock type index for per_cpu support when moving a
131  *                      timer to a base on another cpu.
132  * @active:             red black tree root node for the active timers
133  * @first:              pointer to the timer node which expires first
134  * @resolution:         the resolution of the clock, in nanoseconds
135  * @get_time:           function to retrieve the current time of the clock
136  * @softirq_time:       the time when running the hrtimer queue in the softirq
137  * @offset:             offset of this clock to the monotonic base
138  */
139 struct 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  * struct hrtimer_cpu_base - the per cpu clock bases
156  * @lock:               lock protecting the base and associated clock bases
157  *                      and timers
158  * @clock_base:         array of clock bases for this cpu
159  * @curr_timer:         the timer which is executing a callback right now
160  * @expires_next:       absolute time of the next event which was scheduled
161  *                      via clock_set_next_event()
162  * @hres_active:        State of high resolution mode
163  * @check_clocks:       Indictator, when set evaluate time source and clock
164  *                      event devices whether high resolution mode can be
165  *                      activated.
166  * @nr_events:          Total number of timer interrupt events
167  */
168 struct 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
178 static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
179 {
180         timer->_expires = time;
181         timer->_softexpires = time;
182 }
183
184 static 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
190 static 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
196 static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
197 {
198         timer->_expires.tv64 = tv64;
199         timer->_softexpires.tv64 = tv64;
200 }
201
202 static 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
208 static 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
214 static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
215 {
216         return timer->_expires;
217 }
218
219 static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
220 {
221         return timer->_softexpires;
222 }
223
224 static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
225 {
226         return timer->_expires.tv64;
227 }
228 static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
229 {
230         return timer->_softexpires.tv64;
231 }
232
233 static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
234 {
235         return ktime_to_ns(timer->_expires);
236 }
237
238 static 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
244 struct clock_event_device;
245
246 extern void clock_was_set(void);
247 extern void hres_timers_resume(void);
248 extern void hrtimer_interrupt(struct clock_event_device *dev);
249
250 /*
251  * In high resolution mode the time reference must be read accurate
252  */
253 static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
254 {
255         return timer->base->get_time();
256 }
257
258 static inline int hrtimer_is_hres_active(struct hrtimer *timer)
259 {
260         return timer->base->cpu_base->hres_active;
261 }
262
263 extern void hrtimer_peek_ahead_timers(void);
264
265 /*
266  * The resolution of the clocks. The resolution value is returned in
267  * the clock_getres() system call to give application programmers an
268  * idea of the (in)accuracy of timers. Timer values are rounded up to
269  * this resolution values.
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  * clock_was_set() is a NOP for non- high-resolution systems. The
283  * time-sorted order guarantees that a timer does not expire early and
284  * is expired in the next softirq when the clock was advanced.
285  */
286 static inline void clock_was_set(void) { }
287 static inline void hrtimer_peek_ahead_timers(void) { }
288
289 static inline void hres_timers_resume(void) { }
290
291 /*
292  * In non high resolution mode the time reference is taken from
293  * the base softirq time variable.
294  */
295 static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
296 {
297         return timer->base->softirq_time;
298 }
299
300 static inline int hrtimer_is_hres_active(struct hrtimer *timer)
301 {
302         return 0;
303 }
304 #endif
305
306 extern ktime_t ktime_get(void);
307 extern ktime_t ktime_get_real(void);
308
309
310 DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
311
312
313 /* Exported timer functions: */
314
315 /* Initialize timers: */
316 extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
317                          enum hrtimer_mode mode);
318
319 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
320 extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
321                                   enum hrtimer_mode mode);
322
323 extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
324 #else
325 static 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 }
331 static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
332 #endif
333
334 /* Basic timer operations: */
335 extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
336                          const enum hrtimer_mode mode);
337 extern int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
338                         unsigned long range_ns, const enum hrtimer_mode mode);
339 extern int hrtimer_cancel(struct hrtimer *timer);
340 extern int hrtimer_try_to_cancel(struct hrtimer *timer);
341
342 static 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
353 static inline int hrtimer_restart(struct hrtimer *timer)
354 {
355         return hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
356 }
357
358 /* Query timers: */
359 extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
360 extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
361
362 extern ktime_t hrtimer_get_next_event(void);
363
364 /*
365  * A timer is active, when it is enqueued into the rbtree or the callback
366  * function is running.
367  */
368 static inline int hrtimer_active(const struct hrtimer *timer)
369 {
370         return timer->state != HRTIMER_STATE_INACTIVE;
371 }
372
373 /*
374  * Helper function to check, whether the timer is on one of the queues
375  */
376 static inline int hrtimer_is_queued(struct hrtimer *timer)
377 {
378         return timer->state & HRTIMER_STATE_ENQUEUED;
379 }
380
381 /*
382  * Helper function to check, whether the timer is running the callback
383  * function
384  */
385 static inline int hrtimer_callback_running(struct hrtimer *timer)
386 {
387         return timer->state & HRTIMER_STATE_CALLBACK;
388 }
389
390 /* Forward a hrtimer so it expires after now: */
391 extern u64
392 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
393
394 /* Forward a hrtimer so it expires after the hrtimer's current now */
395 static 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 /* Precise sleep: */
402 extern long hrtimer_nanosleep(struct timespec *rqtp,
403                               struct timespec __user *rmtp,
404                               const enum hrtimer_mode mode,
405                               const clockid_t clockid);
406 extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
407
408 extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
409                                  struct task_struct *tsk);
410
411 extern int schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
412                                                 const enum hrtimer_mode mode);
413 extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
414
415 /* Soft interrupt function to run the hrtimer queues: */
416 extern void hrtimer_run_queues(void);
417 extern void hrtimer_run_pending(void);
418
419 /* Bootup initialization: */
420 extern void __init hrtimers_init(void);
421
422 #if BITS_PER_LONG < 64
423 extern u64 ktime_divns(const ktime_t kt, s64 div);
424 #else /* BITS_PER_LONG < 64 */
425 # define ktime_divns(kt, div)           (u64)((kt).tv64 / (div))
426 #endif
427
428 /* Show pending timers: */
429 extern void sysrq_timer_list_show(void);
430
431 /*
432  * Timer-statistics info:
433  */
434 #ifdef CONFIG_TIMER_STATS
435
436 extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
437                                      void *timerf, char *comm,
438                                      unsigned int timer_flag);
439
440 static 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
446 extern void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer,
447                                                  void *addr);
448
449 static 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
454 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
455 {
456         timer->start_site = NULL;
457 }
458 #else
459 static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
460 {
461 }
462
463 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
464 {
465 }
466
467 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
468 {
469 }
470 #endif
471
472 #endif