blob: 89a9f535b4ceaa845722eeb6b6e588fc31f09653 [file] [log] [blame]
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001/*
2 * linux/kernel/hrtimer.c
3 *
Thomas Gleixner3c8aa392007-02-16 01:27:50 -08004 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2005-2006, Red Hat, Inc., Ingo Molnar
6 * Copyright(C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08007 *
8 * High-resolution kernel timers
9 *
10 * In contrast to the low-resolution timeout API implemented in
11 * kernel/timer.c, hrtimers provide finer resolution and accuracy
12 * depending on system configuration and capabilities.
13 *
14 * These timers are currently used for:
15 * - itimers
16 * - POSIX timers
17 * - nanosleep
18 * - precise in-kernel timing
19 *
20 * Started by: Thomas Gleixner and Ingo Molnar
21 *
22 * Credits:
23 * based on kernel/timer.c
24 *
Thomas Gleixner66188fa2006-02-01 03:05:13 -080025 * Help, testing, suggestions, bugfixes, improvements were
26 * provided by:
27 *
28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
29 * et. al.
30 *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080031 * For licencing details see kernel-base/COPYING
32 */
33
34#include <linux/cpu.h>
35#include <linux/module.h>
36#include <linux/percpu.h>
37#include <linux/hrtimer.h>
38#include <linux/notifier.h>
39#include <linux/syscalls.h>
40#include <linux/interrupt.h>
41
42#include <asm/uaccess.h>
43
44/**
45 * ktime_get - get the monotonic time in ktime_t format
46 *
47 * returns the time in ktime_t format
48 */
49static ktime_t ktime_get(void)
50{
51 struct timespec now;
52
53 ktime_get_ts(&now);
54
55 return timespec_to_ktime(now);
56}
57
58/**
59 * ktime_get_real - get the real (wall-) time in ktime_t format
60 *
61 * returns the time in ktime_t format
62 */
63static ktime_t ktime_get_real(void)
64{
65 struct timespec now;
66
67 getnstimeofday(&now);
68
69 return timespec_to_ktime(now);
70}
71
72EXPORT_SYMBOL_GPL(ktime_get_real);
73
74/*
75 * The timer bases:
George Anzinger7978672c2006-02-01 03:05:11 -080076 *
77 * Note: If we want to add new timer bases, we have to skip the two
78 * clock ids captured by the cpu-timers. We do this by holding empty
79 * entries rather than doing math adjustment of the clock ids.
80 * This ensures that we capture erroneous accesses to these clock ids
81 * rather than moving them into the range of valid clock id's.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080082 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080083static DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080084{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080085
86 .clock_base =
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080087 {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -080088 {
89 .index = CLOCK_REALTIME,
90 .get_time = &ktime_get_real,
91 .resolution = KTIME_REALTIME_RES,
92 },
93 {
94 .index = CLOCK_MONOTONIC,
95 .get_time = &ktime_get,
96 .resolution = KTIME_MONOTONIC_RES,
97 },
98 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080099};
100
101/**
102 * ktime_get_ts - get the monotonic clock in timespec format
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800103 * @ts: pointer to timespec variable
104 *
105 * The function calculates the monotonic clock from the realtime
106 * clock and the wall_to_monotonic offset and stores the result
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800107 * in normalized timespec format in the variable pointed to by @ts.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800108 */
109void ktime_get_ts(struct timespec *ts)
110{
111 struct timespec tomono;
112 unsigned long seq;
113
114 do {
115 seq = read_seqbegin(&xtime_lock);
116 getnstimeofday(ts);
117 tomono = wall_to_monotonic;
118
119 } while (read_seqretry(&xtime_lock, seq));
120
121 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
122 ts->tv_nsec + tomono.tv_nsec);
123}
Matt Helsley69778e32006-01-09 20:52:39 -0800124EXPORT_SYMBOL_GPL(ktime_get_ts);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800125
126/*
Thomas Gleixner92127c72006-03-26 01:38:05 -0800127 * Get the coarse grained time at the softirq based on xtime and
128 * wall_to_monotonic.
129 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800130static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
Thomas Gleixner92127c72006-03-26 01:38:05 -0800131{
132 ktime_t xtim, tomono;
john stultzf4304ab2007-02-16 01:27:26 -0800133 struct timespec xts;
Thomas Gleixner92127c72006-03-26 01:38:05 -0800134 unsigned long seq;
135
136 do {
137 seq = read_seqbegin(&xtime_lock);
john stultzf4304ab2007-02-16 01:27:26 -0800138#ifdef CONFIG_NO_HZ
139 getnstimeofday(&xts);
140#else
141 xts = xtime;
142#endif
Thomas Gleixner92127c72006-03-26 01:38:05 -0800143 } while (read_seqretry(&xtime_lock, seq));
144
john stultzf4304ab2007-02-16 01:27:26 -0800145 xtim = timespec_to_ktime(xts);
146 tomono = timespec_to_ktime(wall_to_monotonic);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800147 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
148 base->clock_base[CLOCK_MONOTONIC].softirq_time =
149 ktime_add(xtim, tomono);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800150}
151
152/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800153 * Functions and macros which are different for UP/SMP systems are kept in a
154 * single place
155 */
156#ifdef CONFIG_SMP
157
158#define set_curr_timer(b, t) do { (b)->curr_timer = (t); } while (0)
159
160/*
161 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
162 * means that all timers which are tied to this base via timer->base are
163 * locked, and the base itself is locked too.
164 *
165 * So __run_timers/migrate_timers can safely modify all timers which could
166 * be found on the lists/queues.
167 *
168 * When the timer's base is locked, and the timer removed from list, it is
169 * possible to set timer->base = NULL and drop the lock: the timer remains
170 * locked.
171 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800172static
173struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
174 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800175{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800176 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800177
178 for (;;) {
179 base = timer->base;
180 if (likely(base != NULL)) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800181 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800182 if (likely(base == timer->base))
183 return base;
184 /* The timer has migrated to another CPU: */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800185 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800186 }
187 cpu_relax();
188 }
189}
190
191/*
192 * Switch the timer base to the current CPU when possible.
193 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800194static inline struct hrtimer_clock_base *
195switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800196{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800197 struct hrtimer_clock_base *new_base;
198 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800199
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800200 new_cpu_base = &__get_cpu_var(hrtimer_bases);
201 new_base = &new_cpu_base->clock_base[base->index];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800202
203 if (base != new_base) {
204 /*
205 * We are trying to schedule the timer on the local CPU.
206 * However we can't change timer's base while it is running,
207 * so we keep it on the same CPU. No hassle vs. reprogramming
208 * the event source in the high resolution case. The softirq
209 * code will take care of this when the timer function has
210 * completed. There is no conflict as we hold the lock until
211 * the timer is enqueued.
212 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800213 if (unlikely(base->cpu_base->curr_timer == timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800214 return base;
215
216 /* See the comment in lock_timer_base() */
217 timer->base = NULL;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800218 spin_unlock(&base->cpu_base->lock);
219 spin_lock(&new_base->cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800220 timer->base = new_base;
221 }
222 return new_base;
223}
224
225#else /* CONFIG_SMP */
226
227#define set_curr_timer(b, t) do { } while (0)
228
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800229static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800230lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
231{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800232 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800233
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800234 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800235
236 return base;
237}
238
239#define switch_hrtimer_base(t, b) (b)
240
241#endif /* !CONFIG_SMP */
242
243/*
244 * Functions for the union type storage format of ktime_t which are
245 * too large for inlining:
246 */
247#if BITS_PER_LONG < 64
248# ifndef CONFIG_KTIME_SCALAR
249/**
250 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800251 * @kt: addend
252 * @nsec: the scalar nsec value to add
253 *
254 * Returns the sum of kt and nsec in ktime_t format
255 */
256ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
257{
258 ktime_t tmp;
259
260 if (likely(nsec < NSEC_PER_SEC)) {
261 tmp.tv64 = nsec;
262 } else {
263 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
264
265 tmp = ktime_set((long)nsec, rem);
266 }
267
268 return ktime_add(kt, tmp);
269}
270
271#else /* CONFIG_KTIME_SCALAR */
272
273# endif /* !CONFIG_KTIME_SCALAR */
274
275/*
276 * Divide a ktime value by a nanosecond value
277 */
Roman Zippeldf869b62006-03-26 01:38:11 -0800278static unsigned long ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800279{
280 u64 dclc, inc, dns;
281 int sft = 0;
282
283 dclc = dns = ktime_to_ns(kt);
284 inc = div;
285 /* Make sure the divisor is less than 2^32: */
286 while (div >> 32) {
287 sft++;
288 div >>= 1;
289 }
290 dclc >>= sft;
291 do_div(dclc, (unsigned long) div);
292
293 return (unsigned long) dclc;
294}
295
296#else /* BITS_PER_LONG < 64 */
297# define ktime_divns(kt, div) (unsigned long)((kt).tv64 / (div))
298#endif /* BITS_PER_LONG >= 64 */
299
300/*
John Stultz411187f2007-02-16 01:27:30 -0800301 * Timekeeping resumed notification
302 */
303void hrtimer_notify_resume(void)
304{
305 clock_was_set();
306}
307
308/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800309 * Counterpart to lock_timer_base above:
310 */
311static inline
312void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
313{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800314 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800315}
316
317/**
318 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800319 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800320 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800321 * @interval: the interval to forward
322 *
323 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700324 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800325 */
326unsigned long
Roman Zippel44f21472006-03-26 01:38:06 -0800327hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800328{
329 unsigned long orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800330 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800331
332 delta = ktime_sub(now, timer->expires);
333
334 if (delta.tv64 < 0)
335 return 0;
336
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100337 if (interval.tv64 < timer->base->resolution.tv64)
338 interval.tv64 = timer->base->resolution.tv64;
339
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800340 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800341 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800342
343 orun = ktime_divns(delta, incr);
344 timer->expires = ktime_add_ns(timer->expires, incr * orun);
345 if (timer->expires.tv64 > now.tv64)
346 return orun;
347 /*
348 * This (and the ktime_add() below) is the
349 * correction for exact:
350 */
351 orun++;
352 }
353 timer->expires = ktime_add(timer->expires, interval);
354
355 return orun;
356}
357
358/*
359 * enqueue_hrtimer - internal function to (re)start a timer
360 *
361 * The timer is inserted in expiry order. Insertion into the
362 * red black tree is O(log(n)). Must hold the base lock.
363 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800364static void enqueue_hrtimer(struct hrtimer *timer,
365 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800366{
367 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800368 struct rb_node *parent = NULL;
369 struct hrtimer *entry;
370
371 /*
372 * Find the right place in the rbtree:
373 */
374 while (*link) {
375 parent = *link;
376 entry = rb_entry(parent, struct hrtimer, node);
377 /*
378 * We dont care about collisions. Nodes with
379 * the same expiry time stay together.
380 */
381 if (timer->expires.tv64 < entry->expires.tv64)
382 link = &(*link)->rb_left;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100383 else
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800384 link = &(*link)->rb_right;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800385 }
386
387 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100388 * Insert the timer to the rbtree and check whether it
389 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800390 */
391 rb_link_node(&timer->node, parent, link);
392 rb_insert_color(&timer->node, &base->active);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800393
Thomas Gleixner288867e2006-01-12 11:25:54 +0100394 if (!base->first || timer->expires.tv64 <
395 rb_entry(base->first, struct hrtimer, node)->expires.tv64)
396 base->first = &timer->node;
397}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800398
399/*
400 * __remove_hrtimer - internal function to remove a timer
401 *
402 * Caller must hold the base lock.
403 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800404static void __remove_hrtimer(struct hrtimer *timer,
405 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800406{
407 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100408 * Remove the timer from the rbtree and replace the
409 * first entry pointer if necessary.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800410 */
Thomas Gleixner288867e2006-01-12 11:25:54 +0100411 if (base->first == &timer->node)
412 base->first = rb_next(&timer->node);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800413 rb_erase(&timer->node, &base->active);
David Woodhouseed198cb2006-04-22 02:38:50 +0100414 rb_set_parent(&timer->node, &timer->node);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800415}
416
417/*
418 * remove hrtimer, called with base lock held
419 */
420static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800421remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800422{
423 if (hrtimer_active(timer)) {
424 __remove_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800425 return 1;
426 }
427 return 0;
428}
429
430/**
431 * hrtimer_start - (re)start an relative timer on the current CPU
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800432 * @timer: the timer to be added
433 * @tim: expiry time
434 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
435 *
436 * Returns:
437 * 0 on success
438 * 1 when the timer was active
439 */
440int
441hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
442{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800443 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800444 unsigned long flags;
445 int ret;
446
447 base = lock_hrtimer_base(timer, &flags);
448
449 /* Remove an active timer from the queue: */
450 ret = remove_hrtimer(timer, base);
451
452 /* Switch the timer base, if necessary: */
453 new_base = switch_hrtimer_base(timer, base);
454
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800455 if (mode == HRTIMER_MODE_REL) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800456 tim = ktime_add(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800457 /*
458 * CONFIG_TIME_LOW_RES is a temporary way for architectures
459 * to signal that they simply return xtime in
460 * do_gettimeoffset(). In this case we want to round up by
461 * resolution when starting a relative timer, to avoid short
462 * timeouts. This will go away with the GTOD framework.
463 */
464#ifdef CONFIG_TIME_LOW_RES
465 tim = ktime_add(tim, base->resolution);
466#endif
467 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800468 timer->expires = tim;
469
470 enqueue_hrtimer(timer, new_base);
471
472 unlock_hrtimer_base(timer, &flags);
473
474 return ret;
475}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700476EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800477
478/**
479 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800480 * @timer: hrtimer to stop
481 *
482 * Returns:
483 * 0 when the timer was not active
484 * 1 when the timer was active
485 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -0700486 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800487 */
488int hrtimer_try_to_cancel(struct hrtimer *timer)
489{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800490 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800491 unsigned long flags;
492 int ret = -1;
493
494 base = lock_hrtimer_base(timer, &flags);
495
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800496 if (base->cpu_base->curr_timer != timer)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800497 ret = remove_hrtimer(timer, base);
498
499 unlock_hrtimer_base(timer, &flags);
500
501 return ret;
502
503}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700504EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800505
506/**
507 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800508 * @timer: the timer to be cancelled
509 *
510 * Returns:
511 * 0 when the timer was not active
512 * 1 when the timer was active
513 */
514int hrtimer_cancel(struct hrtimer *timer)
515{
516 for (;;) {
517 int ret = hrtimer_try_to_cancel(timer);
518
519 if (ret >= 0)
520 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -0700521 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800522 }
523}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700524EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800525
526/**
527 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800528 * @timer: the timer to read
529 */
530ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
531{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800532 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800533 unsigned long flags;
534 ktime_t rem;
535
536 base = lock_hrtimer_base(timer, &flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800537 rem = ktime_sub(timer->expires, base->get_time());
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800538 unlock_hrtimer_base(timer, &flags);
539
540 return rem;
541}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700542EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800543
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800544#if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
Tony Lindgren69239742006-03-06 15:42:45 -0800545/**
546 * hrtimer_get_next_event - get the time until next expiry event
547 *
548 * Returns the delta to the next expiry event or KTIME_MAX if no timer
549 * is pending.
550 */
551ktime_t hrtimer_get_next_event(void)
552{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800553 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
554 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -0800555 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
556 unsigned long flags;
557 int i;
558
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800559 spin_lock_irqsave(&cpu_base->lock, flags);
560
561 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
Tony Lindgren69239742006-03-06 15:42:45 -0800562 struct hrtimer *timer;
563
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800564 if (!base->first)
Tony Lindgren69239742006-03-06 15:42:45 -0800565 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800566
Tony Lindgren69239742006-03-06 15:42:45 -0800567 timer = rb_entry(base->first, struct hrtimer, node);
568 delta.tv64 = timer->expires.tv64;
Tony Lindgren69239742006-03-06 15:42:45 -0800569 delta = ktime_sub(delta, base->get_time());
570 if (delta.tv64 < mindelta.tv64)
571 mindelta.tv64 = delta.tv64;
572 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800573
574 spin_unlock_irqrestore(&cpu_base->lock, flags);
575
Tony Lindgren69239742006-03-06 15:42:45 -0800576 if (mindelta.tv64 < 0)
577 mindelta.tv64 = 0;
578 return mindelta;
579}
580#endif
581
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800582/**
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800583 * hrtimer_init - initialize a timer to the given clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800584 * @timer: the timer to be initialized
585 * @clock_id: the clock to be used
George Anzinger7978672c2006-02-01 03:05:11 -0800586 * @mode: timer mode abs/rel
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800587 */
George Anzinger7978672c2006-02-01 03:05:11 -0800588void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
589 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800590{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800591 struct hrtimer_cpu_base *cpu_base;
George Anzinger7978672c2006-02-01 03:05:11 -0800592
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800593 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -0800594
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800595 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -0800596
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800597 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -0800598 clock_id = CLOCK_MONOTONIC;
599
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800600 timer->base = &cpu_base->clock_base[clock_id];
David Woodhouseed198cb2006-04-22 02:38:50 +0100601 rb_set_parent(&timer->node, &timer->node);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800602}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700603EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800604
605/**
606 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800607 * @which_clock: which clock to query
608 * @tp: pointer to timespec variable to store the resolution
609 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800610 * Store the resolution of the clock selected by @which_clock in the
611 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800612 */
613int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
614{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800615 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800616
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800617 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
618 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800619
620 return 0;
621}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700622EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800623
624/*
625 * Expire the per base hrtimer-queue:
626 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800627static inline void run_hrtimer_queue(struct hrtimer_cpu_base *cpu_base,
628 int index)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800629{
Thomas Gleixner288867e2006-01-12 11:25:54 +0100630 struct rb_node *node;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800631 struct hrtimer_clock_base *base = &cpu_base->clock_base[index];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800632
Dimitri Sivanich3055add2006-03-31 02:31:20 -0800633 if (!base->first)
634 return;
635
Thomas Gleixner92127c72006-03-26 01:38:05 -0800636 if (base->get_softirq_time)
637 base->softirq_time = base->get_softirq_time();
638
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800639 spin_lock_irq(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800640
Thomas Gleixner288867e2006-01-12 11:25:54 +0100641 while ((node = base->first)) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800642 struct hrtimer *timer;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800643 enum hrtimer_restart (*fn)(struct hrtimer *);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800644 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800645
Thomas Gleixner288867e2006-01-12 11:25:54 +0100646 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800647 if (base->softirq_time.tv64 <= timer->expires.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800648 break;
649
650 fn = timer->function;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800651 set_curr_timer(cpu_base, timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800652 __remove_hrtimer(timer, base);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800653 spin_unlock_irq(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800654
Roman Zippel05cfb612006-03-26 01:38:12 -0800655 restart = fn(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800656
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800657 spin_lock_irq(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800658
Roman Zippelb75f7a52006-03-26 01:38:09 -0800659 if (restart != HRTIMER_NORESTART) {
660 BUG_ON(hrtimer_active(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800661 enqueue_hrtimer(timer, base);
Roman Zippelb75f7a52006-03-26 01:38:09 -0800662 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800663 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800664 set_curr_timer(cpu_base, NULL);
665 spin_unlock_irq(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800666}
667
668/*
669 * Called from timer softirq every jiffy, expire hrtimers:
670 */
671void hrtimer_run_queues(void)
672{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800673 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800674 int i;
675
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800676 hrtimer_get_softirq_time(cpu_base);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800677
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800678 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
679 run_hrtimer_queue(cpu_base, i);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800680}
681
682/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800683 * Sleep related functions:
684 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800685static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -0800686{
687 struct hrtimer_sleeper *t =
688 container_of(timer, struct hrtimer_sleeper, timer);
689 struct task_struct *task = t->task;
690
691 t->task = NULL;
692 if (task)
693 wake_up_process(task);
694
695 return HRTIMER_NORESTART;
696}
697
Ingo Molnar36c8b582006-07-03 00:25:41 -0700698void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -0800699{
700 sl->timer.function = hrtimer_wakeup;
701 sl->task = task;
702}
703
Thomas Gleixner669d7862006-03-31 02:31:19 -0800704static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800705{
Thomas Gleixner669d7862006-03-31 02:31:19 -0800706 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800707
Roman Zippel432569b2006-03-26 01:38:08 -0800708 do {
709 set_current_state(TASK_INTERRUPTIBLE);
710 hrtimer_start(&t->timer, t->timer.expires, mode);
711
712 schedule();
713
Thomas Gleixner669d7862006-03-31 02:31:19 -0800714 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800715 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -0800716
Thomas Gleixner669d7862006-03-31 02:31:19 -0800717 } while (t->task && !signal_pending(current));
718
719 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800720}
721
Toyo Abe1711ef32006-09-29 02:00:28 -0700722long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800723{
Thomas Gleixner669d7862006-03-31 02:31:19 -0800724 struct hrtimer_sleeper t;
Ingo Molnarea13dbc2006-01-16 10:59:41 +0100725 struct timespec __user *rmtp;
726 struct timespec tu;
Roman Zippel432569b2006-03-26 01:38:08 -0800727 ktime_t time;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800728
729 restart->fn = do_no_restart_syscall;
730
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800731 hrtimer_init(&t.timer, restart->arg0, HRTIMER_MODE_ABS);
Toyo Abe1711ef32006-09-29 02:00:28 -0700732 t.timer.expires.tv64 = ((u64)restart->arg3 << 32) | (u64) restart->arg2;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800733
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800734 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800735 return 0;
736
Toyo Abe1711ef32006-09-29 02:00:28 -0700737 rmtp = (struct timespec __user *) restart->arg1;
Roman Zippel432569b2006-03-26 01:38:08 -0800738 if (rmtp) {
739 time = ktime_sub(t.timer.expires, t.timer.base->get_time());
740 if (time.tv64 <= 0)
741 return 0;
742 tu = ktime_to_timespec(time);
743 if (copy_to_user(rmtp, &tu, sizeof(tu)))
744 return -EFAULT;
745 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800746
Toyo Abe1711ef32006-09-29 02:00:28 -0700747 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800748
749 /* The other values in restart are already filled in */
750 return -ERESTART_RESTARTBLOCK;
751}
752
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800753long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
754 const enum hrtimer_mode mode, const clockid_t clockid)
755{
756 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -0800757 struct hrtimer_sleeper t;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800758 struct timespec tu;
759 ktime_t rem;
760
Roman Zippel432569b2006-03-26 01:38:08 -0800761 hrtimer_init(&t.timer, clockid, mode);
762 t.timer.expires = timespec_to_ktime(*rqtp);
763 if (do_nanosleep(&t, mode))
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800764 return 0;
765
George Anzinger7978672c2006-02-01 03:05:11 -0800766 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800767 if (mode == HRTIMER_MODE_ABS)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800768 return -ERESTARTNOHAND;
769
Roman Zippel432569b2006-03-26 01:38:08 -0800770 if (rmtp) {
771 rem = ktime_sub(t.timer.expires, t.timer.base->get_time());
772 if (rem.tv64 <= 0)
773 return 0;
774 tu = ktime_to_timespec(rem);
775 if (copy_to_user(rmtp, &tu, sizeof(tu)))
776 return -EFAULT;
777 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800778
779 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -0700780 restart->fn = hrtimer_nanosleep_restart;
781 restart->arg0 = (unsigned long) t.timer.base->index;
782 restart->arg1 = (unsigned long) rmtp;
783 restart->arg2 = t.timer.expires.tv64 & 0xFFFFFFFF;
784 restart->arg3 = t.timer.expires.tv64 >> 32;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800785
786 return -ERESTART_RESTARTBLOCK;
787}
788
Thomas Gleixner6ba1b912006-01-09 20:52:36 -0800789asmlinkage long
790sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
791{
792 struct timespec tu;
793
794 if (copy_from_user(&tu, rqtp, sizeof(tu)))
795 return -EFAULT;
796
797 if (!timespec_valid(&tu))
798 return -EINVAL;
799
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800800 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -0800801}
802
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800803/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800804 * Functions related to boot-time initialization:
805 */
806static void __devinit init_hrtimers_cpu(int cpu)
807{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800808 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800809 int i;
810
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800811 spin_lock_init(&cpu_base->lock);
812 lockdep_set_class(&cpu_base->lock, &cpu_base->lock_key);
813
814 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
815 cpu_base->clock_base[i].cpu_base = cpu_base;
816
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800817}
818
819#ifdef CONFIG_HOTPLUG_CPU
820
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800821static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
822 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800823{
824 struct hrtimer *timer;
825 struct rb_node *node;
826
827 while ((node = rb_first(&old_base->active))) {
828 timer = rb_entry(node, struct hrtimer, node);
829 __remove_hrtimer(timer, old_base);
830 timer->base = new_base;
831 enqueue_hrtimer(timer, new_base);
832 }
833}
834
835static void migrate_hrtimers(int cpu)
836{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800837 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800838 int i;
839
840 BUG_ON(cpu_online(cpu));
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800841 old_base = &per_cpu(hrtimer_bases, cpu);
842 new_base = &get_cpu_var(hrtimer_bases);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800843
844 local_irq_disable();
845
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800846 spin_lock(&new_base->lock);
847 spin_lock(&old_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800848
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800849 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800850 BUG_ON(old_base->curr_timer);
851
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800852 migrate_hrtimer_list(&old_base->clock_base[i],
853 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800854 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800855 spin_unlock(&old_base->lock);
856 spin_unlock(&new_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800857
858 local_irq_enable();
859 put_cpu_var(hrtimer_bases);
860}
861#endif /* CONFIG_HOTPLUG_CPU */
862
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700863static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800864 unsigned long action, void *hcpu)
865{
866 long cpu = (long)hcpu;
867
868 switch (action) {
869
870 case CPU_UP_PREPARE:
871 init_hrtimers_cpu(cpu);
872 break;
873
874#ifdef CONFIG_HOTPLUG_CPU
875 case CPU_DEAD:
876 migrate_hrtimers(cpu);
877 break;
878#endif
879
880 default:
881 break;
882 }
883
884 return NOTIFY_OK;
885}
886
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700887static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800888 .notifier_call = hrtimer_cpu_notify,
889};
890
891void __init hrtimers_init(void)
892{
893 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
894 (void *)(long)smp_processor_id());
895 register_cpu_notifier(&hrtimers_nb);
896}
897