blob: fee18b27252f955d5ec83e60b1d0137e30660c46 [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 Gleixner303e9672007-02-16 01:27:51 -0800153 * Helper function to check, whether the timer is on one of the queues
154 */
155static inline int hrtimer_is_queued(struct hrtimer *timer)
156{
157 return timer->state & HRTIMER_STATE_ENQUEUED;
158}
159
160/*
161 * Helper function to check, whether the timer is running the callback
162 * function
163 */
164static inline int hrtimer_callback_running(struct hrtimer *timer)
165{
166 return timer->state & HRTIMER_STATE_CALLBACK;
167}
168
169/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800170 * Functions and macros which are different for UP/SMP systems are kept in a
171 * single place
172 */
173#ifdef CONFIG_SMP
174
175#define set_curr_timer(b, t) do { (b)->curr_timer = (t); } while (0)
176
177/*
178 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
179 * means that all timers which are tied to this base via timer->base are
180 * locked, and the base itself is locked too.
181 *
182 * So __run_timers/migrate_timers can safely modify all timers which could
183 * be found on the lists/queues.
184 *
185 * When the timer's base is locked, and the timer removed from list, it is
186 * possible to set timer->base = NULL and drop the lock: the timer remains
187 * locked.
188 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800189static
190struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
191 unsigned long *flags)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800192{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800193 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800194
195 for (;;) {
196 base = timer->base;
197 if (likely(base != NULL)) {
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800198 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800199 if (likely(base == timer->base))
200 return base;
201 /* The timer has migrated to another CPU: */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800202 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800203 }
204 cpu_relax();
205 }
206}
207
208/*
209 * Switch the timer base to the current CPU when possible.
210 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800211static inline struct hrtimer_clock_base *
212switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800213{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800214 struct hrtimer_clock_base *new_base;
215 struct hrtimer_cpu_base *new_cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800216
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800217 new_cpu_base = &__get_cpu_var(hrtimer_bases);
218 new_base = &new_cpu_base->clock_base[base->index];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800219
220 if (base != new_base) {
221 /*
222 * We are trying to schedule the timer on the local CPU.
223 * However we can't change timer's base while it is running,
224 * so we keep it on the same CPU. No hassle vs. reprogramming
225 * the event source in the high resolution case. The softirq
226 * code will take care of this when the timer function has
227 * completed. There is no conflict as we hold the lock until
228 * the timer is enqueued.
229 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800230 if (unlikely(base->cpu_base->curr_timer == timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800231 return base;
232
233 /* See the comment in lock_timer_base() */
234 timer->base = NULL;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800235 spin_unlock(&base->cpu_base->lock);
236 spin_lock(&new_base->cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800237 timer->base = new_base;
238 }
239 return new_base;
240}
241
242#else /* CONFIG_SMP */
243
244#define set_curr_timer(b, t) do { } while (0)
245
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800246static inline struct hrtimer_clock_base *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800247lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
248{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800249 struct hrtimer_clock_base *base = timer->base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800250
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800251 spin_lock_irqsave(&base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800252
253 return base;
254}
255
256#define switch_hrtimer_base(t, b) (b)
257
258#endif /* !CONFIG_SMP */
259
260/*
261 * Functions for the union type storage format of ktime_t which are
262 * too large for inlining:
263 */
264#if BITS_PER_LONG < 64
265# ifndef CONFIG_KTIME_SCALAR
266/**
267 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800268 * @kt: addend
269 * @nsec: the scalar nsec value to add
270 *
271 * Returns the sum of kt and nsec in ktime_t format
272 */
273ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
274{
275 ktime_t tmp;
276
277 if (likely(nsec < NSEC_PER_SEC)) {
278 tmp.tv64 = nsec;
279 } else {
280 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
281
282 tmp = ktime_set((long)nsec, rem);
283 }
284
285 return ktime_add(kt, tmp);
286}
287
288#else /* CONFIG_KTIME_SCALAR */
289
290# endif /* !CONFIG_KTIME_SCALAR */
291
292/*
293 * Divide a ktime value by a nanosecond value
294 */
Roman Zippeldf869b62006-03-26 01:38:11 -0800295static unsigned long ktime_divns(const ktime_t kt, s64 div)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800296{
297 u64 dclc, inc, dns;
298 int sft = 0;
299
300 dclc = dns = ktime_to_ns(kt);
301 inc = div;
302 /* Make sure the divisor is less than 2^32: */
303 while (div >> 32) {
304 sft++;
305 div >>= 1;
306 }
307 dclc >>= sft;
308 do_div(dclc, (unsigned long) div);
309
310 return (unsigned long) dclc;
311}
312
313#else /* BITS_PER_LONG < 64 */
314# define ktime_divns(kt, div) (unsigned long)((kt).tv64 / (div))
315#endif /* BITS_PER_LONG >= 64 */
316
317/*
John Stultz411187f2007-02-16 01:27:30 -0800318 * Timekeeping resumed notification
319 */
320void hrtimer_notify_resume(void)
321{
322 clock_was_set();
323}
324
325/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800326 * Counterpart to lock_timer_base above:
327 */
328static inline
329void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
330{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800331 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800332}
333
334/**
335 * hrtimer_forward - forward the timer expiry
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800336 * @timer: hrtimer to forward
Roman Zippel44f21472006-03-26 01:38:06 -0800337 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800338 * @interval: the interval to forward
339 *
340 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700341 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800342 */
343unsigned long
Roman Zippel44f21472006-03-26 01:38:06 -0800344hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800345{
346 unsigned long orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800347 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800348
349 delta = ktime_sub(now, timer->expires);
350
351 if (delta.tv64 < 0)
352 return 0;
353
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100354 if (interval.tv64 < timer->base->resolution.tv64)
355 interval.tv64 = timer->base->resolution.tv64;
356
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800357 if (unlikely(delta.tv64 >= interval.tv64)) {
Roman Zippeldf869b62006-03-26 01:38:11 -0800358 s64 incr = ktime_to_ns(interval);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800359
360 orun = ktime_divns(delta, incr);
361 timer->expires = ktime_add_ns(timer->expires, incr * orun);
362 if (timer->expires.tv64 > now.tv64)
363 return orun;
364 /*
365 * This (and the ktime_add() below) is the
366 * correction for exact:
367 */
368 orun++;
369 }
370 timer->expires = ktime_add(timer->expires, interval);
371
372 return orun;
373}
374
375/*
376 * enqueue_hrtimer - internal function to (re)start a timer
377 *
378 * The timer is inserted in expiry order. Insertion into the
379 * red black tree is O(log(n)). Must hold the base lock.
380 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800381static void enqueue_hrtimer(struct hrtimer *timer,
382 struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800383{
384 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800385 struct rb_node *parent = NULL;
386 struct hrtimer *entry;
387
388 /*
389 * Find the right place in the rbtree:
390 */
391 while (*link) {
392 parent = *link;
393 entry = rb_entry(parent, struct hrtimer, node);
394 /*
395 * We dont care about collisions. Nodes with
396 * the same expiry time stay together.
397 */
398 if (timer->expires.tv64 < entry->expires.tv64)
399 link = &(*link)->rb_left;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100400 else
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800401 link = &(*link)->rb_right;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800402 }
403
404 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100405 * Insert the timer to the rbtree and check whether it
406 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800407 */
408 rb_link_node(&timer->node, parent, link);
409 rb_insert_color(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800410 /*
411 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
412 * state of a possibly running callback.
413 */
414 timer->state |= HRTIMER_STATE_ENQUEUED;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800415
Thomas Gleixner288867e2006-01-12 11:25:54 +0100416 if (!base->first || timer->expires.tv64 <
417 rb_entry(base->first, struct hrtimer, node)->expires.tv64)
418 base->first = &timer->node;
419}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800420
421/*
422 * __remove_hrtimer - internal function to remove a timer
423 *
424 * Caller must hold the base lock.
425 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800426static void __remove_hrtimer(struct hrtimer *timer,
Thomas Gleixner303e9672007-02-16 01:27:51 -0800427 struct hrtimer_clock_base *base,
428 unsigned long newstate)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800429{
430 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100431 * Remove the timer from the rbtree and replace the
432 * first entry pointer if necessary.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800433 */
Thomas Gleixner288867e2006-01-12 11:25:54 +0100434 if (base->first == &timer->node)
435 base->first = rb_next(&timer->node);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800436 rb_erase(&timer->node, &base->active);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800437 timer->state = newstate;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800438}
439
440/*
441 * remove hrtimer, called with base lock held
442 */
443static inline int
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800444remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800445{
Thomas Gleixner303e9672007-02-16 01:27:51 -0800446 if (hrtimer_is_queued(timer)) {
447 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800448 return 1;
449 }
450 return 0;
451}
452
453/**
454 * hrtimer_start - (re)start an relative timer on the current CPU
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800455 * @timer: the timer to be added
456 * @tim: expiry time
457 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
458 *
459 * Returns:
460 * 0 on success
461 * 1 when the timer was active
462 */
463int
464hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
465{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800466 struct hrtimer_clock_base *base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800467 unsigned long flags;
468 int ret;
469
470 base = lock_hrtimer_base(timer, &flags);
471
472 /* Remove an active timer from the queue: */
473 ret = remove_hrtimer(timer, base);
474
475 /* Switch the timer base, if necessary: */
476 new_base = switch_hrtimer_base(timer, base);
477
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800478 if (mode == HRTIMER_MODE_REL) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800479 tim = ktime_add(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800480 /*
481 * CONFIG_TIME_LOW_RES is a temporary way for architectures
482 * to signal that they simply return xtime in
483 * do_gettimeoffset(). In this case we want to round up by
484 * resolution when starting a relative timer, to avoid short
485 * timeouts. This will go away with the GTOD framework.
486 */
487#ifdef CONFIG_TIME_LOW_RES
488 tim = ktime_add(tim, base->resolution);
489#endif
490 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800491 timer->expires = tim;
492
493 enqueue_hrtimer(timer, new_base);
494
495 unlock_hrtimer_base(timer, &flags);
496
497 return ret;
498}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700499EXPORT_SYMBOL_GPL(hrtimer_start);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800500
501/**
502 * hrtimer_try_to_cancel - try to deactivate a timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800503 * @timer: hrtimer to stop
504 *
505 * Returns:
506 * 0 when the timer was not active
507 * 1 when the timer was active
508 * -1 when the timer is currently excuting the callback function and
Randy Dunlapfa9799e2006-06-25 05:49:15 -0700509 * cannot be stopped
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800510 */
511int hrtimer_try_to_cancel(struct hrtimer *timer)
512{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800513 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800514 unsigned long flags;
515 int ret = -1;
516
517 base = lock_hrtimer_base(timer, &flags);
518
Thomas Gleixner303e9672007-02-16 01:27:51 -0800519 if (!hrtimer_callback_running(timer))
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800520 ret = remove_hrtimer(timer, base);
521
522 unlock_hrtimer_base(timer, &flags);
523
524 return ret;
525
526}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700527EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800528
529/**
530 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800531 * @timer: the timer to be cancelled
532 *
533 * Returns:
534 * 0 when the timer was not active
535 * 1 when the timer was active
536 */
537int hrtimer_cancel(struct hrtimer *timer)
538{
539 for (;;) {
540 int ret = hrtimer_try_to_cancel(timer);
541
542 if (ret >= 0)
543 return ret;
Joe Korty5ef37b12006-04-10 22:54:13 -0700544 cpu_relax();
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800545 }
546}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700547EXPORT_SYMBOL_GPL(hrtimer_cancel);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800548
549/**
550 * hrtimer_get_remaining - get remaining time for the timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800551 * @timer: the timer to read
552 */
553ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
554{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800555 struct hrtimer_clock_base *base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800556 unsigned long flags;
557 ktime_t rem;
558
559 base = lock_hrtimer_base(timer, &flags);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800560 rem = ktime_sub(timer->expires, base->get_time());
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800561 unlock_hrtimer_base(timer, &flags);
562
563 return rem;
564}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700565EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800566
Thomas Gleixnerfd064b92007-02-16 01:27:47 -0800567#if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
Tony Lindgren69239742006-03-06 15:42:45 -0800568/**
569 * hrtimer_get_next_event - get the time until next expiry event
570 *
571 * Returns the delta to the next expiry event or KTIME_MAX if no timer
572 * is pending.
573 */
574ktime_t hrtimer_get_next_event(void)
575{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800576 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
577 struct hrtimer_clock_base *base = cpu_base->clock_base;
Tony Lindgren69239742006-03-06 15:42:45 -0800578 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
579 unsigned long flags;
580 int i;
581
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800582 spin_lock_irqsave(&cpu_base->lock, flags);
583
584 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
Tony Lindgren69239742006-03-06 15:42:45 -0800585 struct hrtimer *timer;
586
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800587 if (!base->first)
Tony Lindgren69239742006-03-06 15:42:45 -0800588 continue;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800589
Tony Lindgren69239742006-03-06 15:42:45 -0800590 timer = rb_entry(base->first, struct hrtimer, node);
591 delta.tv64 = timer->expires.tv64;
Tony Lindgren69239742006-03-06 15:42:45 -0800592 delta = ktime_sub(delta, base->get_time());
593 if (delta.tv64 < mindelta.tv64)
594 mindelta.tv64 = delta.tv64;
595 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800596
597 spin_unlock_irqrestore(&cpu_base->lock, flags);
598
Tony Lindgren69239742006-03-06 15:42:45 -0800599 if (mindelta.tv64 < 0)
600 mindelta.tv64 = 0;
601 return mindelta;
602}
603#endif
604
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800605/**
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800606 * hrtimer_init - initialize a timer to the given clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800607 * @timer: the timer to be initialized
608 * @clock_id: the clock to be used
George Anzinger7978672c2006-02-01 03:05:11 -0800609 * @mode: timer mode abs/rel
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800610 */
George Anzinger7978672c2006-02-01 03:05:11 -0800611void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
612 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800613{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800614 struct hrtimer_cpu_base *cpu_base;
George Anzinger7978672c2006-02-01 03:05:11 -0800615
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800616 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -0800617
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800618 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
George Anzinger7978672c2006-02-01 03:05:11 -0800619
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800620 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
George Anzinger7978672c2006-02-01 03:05:11 -0800621 clock_id = CLOCK_MONOTONIC;
622
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800623 timer->base = &cpu_base->clock_base[clock_id];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800624}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700625EXPORT_SYMBOL_GPL(hrtimer_init);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800626
627/**
628 * hrtimer_get_res - get the timer resolution for a clock
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800629 * @which_clock: which clock to query
630 * @tp: pointer to timespec variable to store the resolution
631 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800632 * Store the resolution of the clock selected by @which_clock in the
633 * variable pointed to by @tp.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800634 */
635int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
636{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800637 struct hrtimer_cpu_base *cpu_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800638
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800639 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
640 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800641
642 return 0;
643}
Stephen Hemminger8d16b762006-05-30 21:26:09 -0700644EXPORT_SYMBOL_GPL(hrtimer_get_res);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800645
646/*
647 * Expire the per base hrtimer-queue:
648 */
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800649static inline void run_hrtimer_queue(struct hrtimer_cpu_base *cpu_base,
650 int index)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800651{
Thomas Gleixner288867e2006-01-12 11:25:54 +0100652 struct rb_node *node;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800653 struct hrtimer_clock_base *base = &cpu_base->clock_base[index];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800654
Dimitri Sivanich3055add2006-03-31 02:31:20 -0800655 if (!base->first)
656 return;
657
Thomas Gleixner92127c72006-03-26 01:38:05 -0800658 if (base->get_softirq_time)
659 base->softirq_time = base->get_softirq_time();
660
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800661 spin_lock_irq(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800662
Thomas Gleixner288867e2006-01-12 11:25:54 +0100663 while ((node = base->first)) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800664 struct hrtimer *timer;
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800665 enum hrtimer_restart (*fn)(struct hrtimer *);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800666 int restart;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800667
Thomas Gleixner288867e2006-01-12 11:25:54 +0100668 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800669 if (base->softirq_time.tv64 <= timer->expires.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800670 break;
671
672 fn = timer->function;
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800673 set_curr_timer(cpu_base, timer);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800674 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK);
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800675 spin_unlock_irq(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800676
Roman Zippel05cfb612006-03-26 01:38:12 -0800677 restart = fn(timer);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800678
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800679 spin_lock_irq(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800680
Thomas Gleixner303e9672007-02-16 01:27:51 -0800681 timer->state &= ~HRTIMER_STATE_CALLBACK;
Roman Zippelb75f7a52006-03-26 01:38:09 -0800682 if (restart != HRTIMER_NORESTART) {
683 BUG_ON(hrtimer_active(timer));
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800684 enqueue_hrtimer(timer, base);
Roman Zippelb75f7a52006-03-26 01:38:09 -0800685 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800686 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800687 set_curr_timer(cpu_base, NULL);
688 spin_unlock_irq(&cpu_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800689}
690
691/*
692 * Called from timer softirq every jiffy, expire hrtimers:
693 */
694void hrtimer_run_queues(void)
695{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800696 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800697 int i;
698
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800699 hrtimer_get_softirq_time(cpu_base);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800700
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800701 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
702 run_hrtimer_queue(cpu_base, i);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800703}
704
705/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800706 * Sleep related functions:
707 */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800708static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
Thomas Gleixner00362e32006-03-31 02:31:17 -0800709{
710 struct hrtimer_sleeper *t =
711 container_of(timer, struct hrtimer_sleeper, timer);
712 struct task_struct *task = t->task;
713
714 t->task = NULL;
715 if (task)
716 wake_up_process(task);
717
718 return HRTIMER_NORESTART;
719}
720
Ingo Molnar36c8b582006-07-03 00:25:41 -0700721void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
Thomas Gleixner00362e32006-03-31 02:31:17 -0800722{
723 sl->timer.function = hrtimer_wakeup;
724 sl->task = task;
725}
726
Thomas Gleixner669d7862006-03-31 02:31:19 -0800727static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800728{
Thomas Gleixner669d7862006-03-31 02:31:19 -0800729 hrtimer_init_sleeper(t, current);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800730
Roman Zippel432569b2006-03-26 01:38:08 -0800731 do {
732 set_current_state(TASK_INTERRUPTIBLE);
733 hrtimer_start(&t->timer, t->timer.expires, mode);
734
735 schedule();
736
Thomas Gleixner669d7862006-03-31 02:31:19 -0800737 hrtimer_cancel(&t->timer);
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800738 mode = HRTIMER_MODE_ABS;
Roman Zippel432569b2006-03-26 01:38:08 -0800739
Thomas Gleixner669d7862006-03-31 02:31:19 -0800740 } while (t->task && !signal_pending(current));
741
742 return t->task == NULL;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800743}
744
Toyo Abe1711ef32006-09-29 02:00:28 -0700745long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800746{
Thomas Gleixner669d7862006-03-31 02:31:19 -0800747 struct hrtimer_sleeper t;
Ingo Molnarea13dbc2006-01-16 10:59:41 +0100748 struct timespec __user *rmtp;
749 struct timespec tu;
Roman Zippel432569b2006-03-26 01:38:08 -0800750 ktime_t time;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800751
752 restart->fn = do_no_restart_syscall;
753
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800754 hrtimer_init(&t.timer, restart->arg0, HRTIMER_MODE_ABS);
Toyo Abe1711ef32006-09-29 02:00:28 -0700755 t.timer.expires.tv64 = ((u64)restart->arg3 << 32) | (u64) restart->arg2;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800756
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800757 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800758 return 0;
759
Toyo Abe1711ef32006-09-29 02:00:28 -0700760 rmtp = (struct timespec __user *) restart->arg1;
Roman Zippel432569b2006-03-26 01:38:08 -0800761 if (rmtp) {
762 time = ktime_sub(t.timer.expires, t.timer.base->get_time());
763 if (time.tv64 <= 0)
764 return 0;
765 tu = ktime_to_timespec(time);
766 if (copy_to_user(rmtp, &tu, sizeof(tu)))
767 return -EFAULT;
768 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800769
Toyo Abe1711ef32006-09-29 02:00:28 -0700770 restart->fn = hrtimer_nanosleep_restart;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800771
772 /* The other values in restart are already filled in */
773 return -ERESTART_RESTARTBLOCK;
774}
775
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800776long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
777 const enum hrtimer_mode mode, const clockid_t clockid)
778{
779 struct restart_block *restart;
Thomas Gleixner669d7862006-03-31 02:31:19 -0800780 struct hrtimer_sleeper t;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800781 struct timespec tu;
782 ktime_t rem;
783
Roman Zippel432569b2006-03-26 01:38:08 -0800784 hrtimer_init(&t.timer, clockid, mode);
785 t.timer.expires = timespec_to_ktime(*rqtp);
786 if (do_nanosleep(&t, mode))
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800787 return 0;
788
George Anzinger7978672c2006-02-01 03:05:11 -0800789 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800790 if (mode == HRTIMER_MODE_ABS)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800791 return -ERESTARTNOHAND;
792
Roman Zippel432569b2006-03-26 01:38:08 -0800793 if (rmtp) {
794 rem = ktime_sub(t.timer.expires, t.timer.base->get_time());
795 if (rem.tv64 <= 0)
796 return 0;
797 tu = ktime_to_timespec(rem);
798 if (copy_to_user(rmtp, &tu, sizeof(tu)))
799 return -EFAULT;
800 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800801
802 restart = &current_thread_info()->restart_block;
Toyo Abe1711ef32006-09-29 02:00:28 -0700803 restart->fn = hrtimer_nanosleep_restart;
804 restart->arg0 = (unsigned long) t.timer.base->index;
805 restart->arg1 = (unsigned long) rmtp;
806 restart->arg2 = t.timer.expires.tv64 & 0xFFFFFFFF;
807 restart->arg3 = t.timer.expires.tv64 >> 32;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800808
809 return -ERESTART_RESTARTBLOCK;
810}
811
Thomas Gleixner6ba1b912006-01-09 20:52:36 -0800812asmlinkage long
813sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
814{
815 struct timespec tu;
816
817 if (copy_from_user(&tu, rqtp, sizeof(tu)))
818 return -EFAULT;
819
820 if (!timespec_valid(&tu))
821 return -EINVAL;
822
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800823 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
Thomas Gleixner6ba1b912006-01-09 20:52:36 -0800824}
825
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800826/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800827 * Functions related to boot-time initialization:
828 */
829static void __devinit init_hrtimers_cpu(int cpu)
830{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800831 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800832 int i;
833
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800834 spin_lock_init(&cpu_base->lock);
835 lockdep_set_class(&cpu_base->lock, &cpu_base->lock_key);
836
837 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
838 cpu_base->clock_base[i].cpu_base = cpu_base;
839
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800840}
841
842#ifdef CONFIG_HOTPLUG_CPU
843
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800844static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
845 struct hrtimer_clock_base *new_base)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800846{
847 struct hrtimer *timer;
848 struct rb_node *node;
849
850 while ((node = rb_first(&old_base->active))) {
851 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner303e9672007-02-16 01:27:51 -0800852 BUG_ON(timer->state & HRTIMER_STATE_CALLBACK);
853 __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800854 timer->base = new_base;
855 enqueue_hrtimer(timer, new_base);
856 }
857}
858
859static void migrate_hrtimers(int cpu)
860{
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800861 struct hrtimer_cpu_base *old_base, *new_base;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800862 int i;
863
864 BUG_ON(cpu_online(cpu));
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800865 old_base = &per_cpu(hrtimer_bases, cpu);
866 new_base = &get_cpu_var(hrtimer_bases);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800867
868 local_irq_disable();
869
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800870 spin_lock(&new_base->lock);
871 spin_lock(&old_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800872
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800873 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800874 BUG_ON(old_base->curr_timer);
875
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800876 migrate_hrtimer_list(&old_base->clock_base[i],
877 &new_base->clock_base[i]);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800878 }
Thomas Gleixner3c8aa392007-02-16 01:27:50 -0800879 spin_unlock(&old_base->lock);
880 spin_unlock(&new_base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800881
882 local_irq_enable();
883 put_cpu_var(hrtimer_bases);
884}
885#endif /* CONFIG_HOTPLUG_CPU */
886
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700887static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800888 unsigned long action, void *hcpu)
889{
890 long cpu = (long)hcpu;
891
892 switch (action) {
893
894 case CPU_UP_PREPARE:
895 init_hrtimers_cpu(cpu);
896 break;
897
898#ifdef CONFIG_HOTPLUG_CPU
899 case CPU_DEAD:
900 migrate_hrtimers(cpu);
901 break;
902#endif
903
904 default:
905 break;
906 }
907
908 return NOTIFY_OK;
909}
910
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700911static struct notifier_block __cpuinitdata hrtimers_nb = {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800912 .notifier_call = hrtimer_cpu_notify,
913};
914
915void __init hrtimers_init(void)
916{
917 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
918 (void *)(long)smp_processor_id());
919 register_cpu_notifier(&hrtimers_nb);
920}
921