ASoC: Blackfin I2S: fix resuming when device hasn't been used
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / hrtimer.c
blob9002958a96e70ef0f8acc70ceeccac5327efae9e
1 /*
2 * linux/kernel/hrtimer.c
4 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
6 * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
8 * High-resolution kernel timers
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.
14 * These timers are currently used for:
15 * - itimers
16 * - POSIX timers
17 * - nanosleep
18 * - precise in-kernel timing
20 * Started by: Thomas Gleixner and Ingo Molnar
22 * Credits:
23 * based on kernel/timer.c
25 * Help, testing, suggestions, bugfixes, improvements were
26 * provided by:
28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
29 * et. al.
31 * For licencing details see kernel-base/COPYING
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/kallsyms.h>
41 #include <linux/interrupt.h>
42 #include <linux/tick.h>
43 #include <linux/seq_file.h>
44 #include <linux/err.h>
45 #include <linux/debugobjects.h>
46 #include <linux/sched.h>
47 #include <linux/timer.h>
49 #include <asm/uaccess.h>
51 /**
52 * ktime_get - get the monotonic time in ktime_t format
54 * returns the time in ktime_t format
56 ktime_t ktime_get(void)
58 struct timespec now;
60 ktime_get_ts(&now);
62 return timespec_to_ktime(now);
64 EXPORT_SYMBOL_GPL(ktime_get);
66 /**
67 * ktime_get_real - get the real (wall-) time in ktime_t format
69 * returns the time in ktime_t format
71 ktime_t ktime_get_real(void)
73 struct timespec now;
75 getnstimeofday(&now);
77 return timespec_to_ktime(now);
80 EXPORT_SYMBOL_GPL(ktime_get_real);
83 * The timer bases:
85 * Note: If we want to add new timer bases, we have to skip the two
86 * clock ids captured by the cpu-timers. We do this by holding empty
87 * entries rather than doing math adjustment of the clock ids.
88 * This ensures that we capture erroneous accesses to these clock ids
89 * rather than moving them into the range of valid clock id's.
91 DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
94 .clock_base =
97 .index = CLOCK_REALTIME,
98 .get_time = &ktime_get_real,
99 .resolution = KTIME_LOW_RES,
102 .index = CLOCK_MONOTONIC,
103 .get_time = &ktime_get,
104 .resolution = KTIME_LOW_RES,
110 * ktime_get_ts - get the monotonic clock in timespec format
111 * @ts: pointer to timespec variable
113 * The function calculates the monotonic clock from the realtime
114 * clock and the wall_to_monotonic offset and stores the result
115 * in normalized timespec format in the variable pointed to by @ts.
117 void ktime_get_ts(struct timespec *ts)
119 struct timespec tomono;
120 unsigned long seq;
122 do {
123 seq = read_seqbegin(&xtime_lock);
124 getnstimeofday(ts);
125 tomono = wall_to_monotonic;
127 } while (read_seqretry(&xtime_lock, seq));
129 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
130 ts->tv_nsec + tomono.tv_nsec);
132 EXPORT_SYMBOL_GPL(ktime_get_ts);
135 * Get the coarse grained time at the softirq based on xtime and
136 * wall_to_monotonic.
138 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
140 ktime_t xtim, tomono;
141 struct timespec xts, tom;
142 unsigned long seq;
144 do {
145 seq = read_seqbegin(&xtime_lock);
146 xts = current_kernel_time();
147 tom = wall_to_monotonic;
148 } while (read_seqretry(&xtime_lock, seq));
150 xtim = timespec_to_ktime(xts);
151 tomono = timespec_to_ktime(tom);
152 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
153 base->clock_base[CLOCK_MONOTONIC].softirq_time =
154 ktime_add(xtim, tomono);
158 * Functions and macros which are different for UP/SMP systems are kept in a
159 * single place
161 #ifdef CONFIG_SMP
164 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
165 * means that all timers which are tied to this base via timer->base are
166 * locked, and the base itself is locked too.
168 * So __run_timers/migrate_timers can safely modify all timers which could
169 * be found on the lists/queues.
171 * When the timer's base is locked, and the timer removed from list, it is
172 * possible to set timer->base = NULL and drop the lock: the timer remains
173 * locked.
175 static
176 struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
177 unsigned long *flags)
179 struct hrtimer_clock_base *base;
181 for (;;) {
182 base = timer->base;
183 if (likely(base != NULL)) {
184 spin_lock_irqsave(&base->cpu_base->lock, *flags);
185 if (likely(base == timer->base))
186 return base;
187 /* The timer has migrated to another CPU: */
188 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
190 cpu_relax();
195 * Switch the timer base to the current CPU when possible.
197 static inline struct hrtimer_clock_base *
198 switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
199 int pinned)
201 struct hrtimer_clock_base *new_base;
202 struct hrtimer_cpu_base *new_cpu_base;
203 int cpu, preferred_cpu = -1;
205 cpu = smp_processor_id();
206 #if defined(CONFIG_NO_HZ) && defined(CONFIG_SMP)
207 if (!pinned && get_sysctl_timer_migration() && idle_cpu(cpu)) {
208 preferred_cpu = get_nohz_load_balancer();
209 if (preferred_cpu >= 0)
210 cpu = preferred_cpu;
212 #endif
214 again:
215 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
216 new_base = &new_cpu_base->clock_base[base->index];
218 if (base != new_base) {
220 * We are trying to schedule the timer on the local CPU.
221 * However we can't change timer's base while it is running,
222 * so we keep it on the same CPU. No hassle vs. reprogramming
223 * the event source in the high resolution case. The softirq
224 * code will take care of this when the timer function has
225 * completed. There is no conflict as we hold the lock until
226 * the timer is enqueued.
228 if (unlikely(hrtimer_callback_running(timer)))
229 return base;
231 /* See the comment in lock_timer_base() */
232 timer->base = NULL;
233 spin_unlock(&base->cpu_base->lock);
234 spin_lock(&new_base->cpu_base->lock);
236 /* Optimized away for NOHZ=n SMP=n */
237 if (cpu == preferred_cpu) {
238 /* Calculate clock monotonic expiry time */
239 #ifdef CONFIG_HIGH_RES_TIMERS
240 ktime_t expires = ktime_sub(hrtimer_get_expires(timer),
241 new_base->offset);
242 #else
243 ktime_t expires = hrtimer_get_expires(timer);
244 #endif
247 * Get the next event on target cpu from the
248 * clock events layer.
249 * This covers the highres=off nohz=on case as well.
251 ktime_t next = clockevents_get_next_event(cpu);
253 ktime_t delta = ktime_sub(expires, next);
256 * We do not migrate the timer when it is expiring
257 * before the next event on the target cpu because
258 * we cannot reprogram the target cpu hardware and
259 * we would cause it to fire late.
261 if (delta.tv64 < 0) {
262 cpu = smp_processor_id();
263 spin_unlock(&new_base->cpu_base->lock);
264 spin_lock(&base->cpu_base->lock);
265 timer->base = base;
266 goto again;
269 timer->base = new_base;
271 return new_base;
274 #else /* CONFIG_SMP */
276 static inline struct hrtimer_clock_base *
277 lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
279 struct hrtimer_clock_base *base = timer->base;
281 spin_lock_irqsave(&base->cpu_base->lock, *flags);
283 return base;
286 # define switch_hrtimer_base(t, b, p) (b)
288 #endif /* !CONFIG_SMP */
291 * Functions for the union type storage format of ktime_t which are
292 * too large for inlining:
294 #if BITS_PER_LONG < 64
295 # ifndef CONFIG_KTIME_SCALAR
297 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
298 * @kt: addend
299 * @nsec: the scalar nsec value to add
301 * Returns the sum of kt and nsec in ktime_t format
303 ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
305 ktime_t tmp;
307 if (likely(nsec < NSEC_PER_SEC)) {
308 tmp.tv64 = nsec;
309 } else {
310 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
312 tmp = ktime_set((long)nsec, rem);
315 return ktime_add(kt, tmp);
318 EXPORT_SYMBOL_GPL(ktime_add_ns);
321 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
322 * @kt: minuend
323 * @nsec: the scalar nsec value to subtract
325 * Returns the subtraction of @nsec from @kt in ktime_t format
327 ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
329 ktime_t tmp;
331 if (likely(nsec < NSEC_PER_SEC)) {
332 tmp.tv64 = nsec;
333 } else {
334 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
336 tmp = ktime_set((long)nsec, rem);
339 return ktime_sub(kt, tmp);
342 EXPORT_SYMBOL_GPL(ktime_sub_ns);
343 # endif /* !CONFIG_KTIME_SCALAR */
346 * Divide a ktime value by a nanosecond value
348 u64 ktime_divns(const ktime_t kt, s64 div)
350 u64 dclc;
351 int sft = 0;
353 dclc = ktime_to_ns(kt);
354 /* Make sure the divisor is less than 2^32: */
355 while (div >> 32) {
356 sft++;
357 div >>= 1;
359 dclc >>= sft;
360 do_div(dclc, (unsigned long) div);
362 return dclc;
364 #endif /* BITS_PER_LONG >= 64 */
367 * Add two ktime values and do a safety check for overflow:
369 ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
371 ktime_t res = ktime_add(lhs, rhs);
374 * We use KTIME_SEC_MAX here, the maximum timeout which we can
375 * return to user space in a timespec:
377 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
378 res = ktime_set(KTIME_SEC_MAX, 0);
380 return res;
383 EXPORT_SYMBOL_GPL(ktime_add_safe);
385 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
387 static struct debug_obj_descr hrtimer_debug_descr;
390 * fixup_init is called when:
391 * - an active object is initialized
393 static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
395 struct hrtimer *timer = addr;
397 switch (state) {
398 case ODEBUG_STATE_ACTIVE:
399 hrtimer_cancel(timer);
400 debug_object_init(timer, &hrtimer_debug_descr);
401 return 1;
402 default:
403 return 0;
408 * fixup_activate is called when:
409 * - an active object is activated
410 * - an unknown object is activated (might be a statically initialized object)
412 static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
414 switch (state) {
416 case ODEBUG_STATE_NOTAVAILABLE:
417 WARN_ON_ONCE(1);
418 return 0;
420 case ODEBUG_STATE_ACTIVE:
421 WARN_ON(1);
423 default:
424 return 0;
429 * fixup_free is called when:
430 * - an active object is freed
432 static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
434 struct hrtimer *timer = addr;
436 switch (state) {
437 case ODEBUG_STATE_ACTIVE:
438 hrtimer_cancel(timer);
439 debug_object_free(timer, &hrtimer_debug_descr);
440 return 1;
441 default:
442 return 0;
446 static struct debug_obj_descr hrtimer_debug_descr = {
447 .name = "hrtimer",
448 .fixup_init = hrtimer_fixup_init,
449 .fixup_activate = hrtimer_fixup_activate,
450 .fixup_free = hrtimer_fixup_free,
453 static inline void debug_hrtimer_init(struct hrtimer *timer)
455 debug_object_init(timer, &hrtimer_debug_descr);
458 static inline void debug_hrtimer_activate(struct hrtimer *timer)
460 debug_object_activate(timer, &hrtimer_debug_descr);
463 static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
465 debug_object_deactivate(timer, &hrtimer_debug_descr);
468 static inline void debug_hrtimer_free(struct hrtimer *timer)
470 debug_object_free(timer, &hrtimer_debug_descr);
473 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
474 enum hrtimer_mode mode);
476 void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
477 enum hrtimer_mode mode)
479 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
480 __hrtimer_init(timer, clock_id, mode);
483 void destroy_hrtimer_on_stack(struct hrtimer *timer)
485 debug_object_free(timer, &hrtimer_debug_descr);
488 #else
489 static inline void debug_hrtimer_init(struct hrtimer *timer) { }
490 static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
491 static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
492 #endif
494 /* High resolution timer related functions */
495 #ifdef CONFIG_HIGH_RES_TIMERS
498 * High resolution timer enabled ?
500 static int hrtimer_hres_enabled __read_mostly = 1;
503 * Enable / Disable high resolution mode
505 static int __init setup_hrtimer_hres(char *str)
507 if (!strcmp(str, "off"))
508 hrtimer_hres_enabled = 0;
509 else if (!strcmp(str, "on"))
510 hrtimer_hres_enabled = 1;
511 else
512 return 0;
513 return 1;
516 __setup("highres=", setup_hrtimer_hres);
519 * hrtimer_high_res_enabled - query, if the highres mode is enabled
521 static inline int hrtimer_is_hres_enabled(void)
523 return hrtimer_hres_enabled;
527 * Is the high resolution mode active ?
529 static inline int hrtimer_hres_active(void)
531 return __get_cpu_var(hrtimer_bases).hres_active;
535 * Reprogram the event source with checking both queues for the
536 * next event
537 * Called with interrupts disabled and base->lock held
539 static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
541 int i;
542 struct hrtimer_clock_base *base = cpu_base->clock_base;
543 ktime_t expires;
545 cpu_base->expires_next.tv64 = KTIME_MAX;
547 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
548 struct hrtimer *timer;
550 if (!base->first)
551 continue;
552 timer = rb_entry(base->first, struct hrtimer, node);
553 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
555 * clock_was_set() has changed base->offset so the
556 * result might be negative. Fix it up to prevent a
557 * false positive in clockevents_program_event()
559 if (expires.tv64 < 0)
560 expires.tv64 = 0;
561 if (expires.tv64 < cpu_base->expires_next.tv64)
562 cpu_base->expires_next = expires;
565 if (cpu_base->expires_next.tv64 != KTIME_MAX)
566 tick_program_event(cpu_base->expires_next, 1);
570 * Shared reprogramming for clock_realtime and clock_monotonic
572 * When a timer is enqueued and expires earlier than the already enqueued
573 * timers, we have to check, whether it expires earlier than the timer for
574 * which the clock event device was armed.
576 * Called with interrupts disabled and base->cpu_base.lock held
578 static int hrtimer_reprogram(struct hrtimer *timer,
579 struct hrtimer_clock_base *base)
581 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
582 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
583 int res;
585 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
588 * When the callback is running, we do not reprogram the clock event
589 * device. The timer callback is either running on a different CPU or
590 * the callback is executed in the hrtimer_interrupt context. The
591 * reprogramming is handled either by the softirq, which called the
592 * callback or at the end of the hrtimer_interrupt.
594 if (hrtimer_callback_running(timer))
595 return 0;
598 * CLOCK_REALTIME timer might be requested with an absolute
599 * expiry time which is less than base->offset. Nothing wrong
600 * about that, just avoid to call into the tick code, which
601 * has now objections against negative expiry values.
603 if (expires.tv64 < 0)
604 return -ETIME;
606 if (expires.tv64 >= expires_next->tv64)
607 return 0;
610 * Clockevents returns -ETIME, when the event was in the past.
612 res = tick_program_event(expires, 0);
613 if (!IS_ERR_VALUE(res))
614 *expires_next = expires;
615 return res;
620 * Retrigger next event is called after clock was set
622 * Called with interrupts disabled via on_each_cpu()
624 static void retrigger_next_event(void *arg)
626 struct hrtimer_cpu_base *base;
627 struct timespec realtime_offset;
628 unsigned long seq;
630 if (!hrtimer_hres_active())
631 return;
633 do {
634 seq = read_seqbegin(&xtime_lock);
635 set_normalized_timespec(&realtime_offset,
636 -wall_to_monotonic.tv_sec,
637 -wall_to_monotonic.tv_nsec);
638 } while (read_seqretry(&xtime_lock, seq));
640 base = &__get_cpu_var(hrtimer_bases);
642 /* Adjust CLOCK_REALTIME offset */
643 spin_lock(&base->lock);
644 base->clock_base[CLOCK_REALTIME].offset =
645 timespec_to_ktime(realtime_offset);
647 hrtimer_force_reprogram(base);
648 spin_unlock(&base->lock);
652 * Clock realtime was set
654 * Change the offset of the realtime clock vs. the monotonic
655 * clock.
657 * We might have to reprogram the high resolution timer interrupt. On
658 * SMP we call the architecture specific code to retrigger _all_ high
659 * resolution timer interrupts. On UP we just disable interrupts and
660 * call the high resolution interrupt code.
662 void clock_was_set(void)
664 /* Retrigger the CPU local events everywhere */
665 on_each_cpu(retrigger_next_event, NULL, 1);
669 * During resume we might have to reprogram the high resolution timer
670 * interrupt (on the local CPU):
672 void hres_timers_resume(void)
674 WARN_ONCE(!irqs_disabled(),
675 KERN_INFO "hres_timers_resume() called with IRQs enabled!");
677 retrigger_next_event(NULL);
681 * Initialize the high resolution related parts of cpu_base
683 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
685 base->expires_next.tv64 = KTIME_MAX;
686 base->hres_active = 0;
690 * Initialize the high resolution related parts of a hrtimer
692 static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
698 * When High resolution timers are active, try to reprogram. Note, that in case
699 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
700 * check happens. The timer gets enqueued into the rbtree. The reprogramming
701 * and expiry check is done in the hrtimer_interrupt or in the softirq.
703 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
704 struct hrtimer_clock_base *base,
705 int wakeup)
707 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
708 if (wakeup) {
709 spin_unlock(&base->cpu_base->lock);
710 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
711 spin_lock(&base->cpu_base->lock);
712 } else
713 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
715 return 1;
718 return 0;
722 * Switch to high resolution mode
724 static int hrtimer_switch_to_hres(void)
726 int cpu = smp_processor_id();
727 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
728 unsigned long flags;
730 if (base->hres_active)
731 return 1;
733 local_irq_save(flags);
735 if (tick_init_highres()) {
736 local_irq_restore(flags);
737 printk(KERN_WARNING "Could not switch to high resolution "
738 "mode on CPU %d\n", cpu);
739 return 0;
741 base->hres_active = 1;
742 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
743 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
745 tick_setup_sched_timer();
747 /* "Retrigger" the interrupt to get things going */
748 retrigger_next_event(NULL);
749 local_irq_restore(flags);
750 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
751 smp_processor_id());
752 return 1;
755 #else
757 static inline int hrtimer_hres_active(void) { return 0; }
758 static inline int hrtimer_is_hres_enabled(void) { return 0; }
759 static inline int hrtimer_switch_to_hres(void) { return 0; }
760 static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
761 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
762 struct hrtimer_clock_base *base,
763 int wakeup)
765 return 0;
767 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
768 static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
770 #endif /* CONFIG_HIGH_RES_TIMERS */
772 #ifdef CONFIG_TIMER_STATS
773 void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
775 if (timer->start_site)
776 return;
778 timer->start_site = addr;
779 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
780 timer->start_pid = current->pid;
782 #endif
785 * Counterpart to lock_hrtimer_base above:
787 static inline
788 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
790 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
794 * hrtimer_forward - forward the timer expiry
795 * @timer: hrtimer to forward
796 * @now: forward past this time
797 * @interval: the interval to forward
799 * Forward the timer expiry so it will expire in the future.
800 * Returns the number of overruns.
802 u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
804 u64 orun = 1;
805 ktime_t delta;
807 delta = ktime_sub(now, hrtimer_get_expires(timer));
809 if (delta.tv64 < 0)
810 return 0;
812 if (interval.tv64 < timer->base->resolution.tv64)
813 interval.tv64 = timer->base->resolution.tv64;
815 if (unlikely(delta.tv64 >= interval.tv64)) {
816 s64 incr = ktime_to_ns(interval);
818 orun = ktime_divns(delta, incr);
819 hrtimer_add_expires_ns(timer, incr * orun);
820 if (hrtimer_get_expires_tv64(timer) > now.tv64)
821 return orun;
823 * This (and the ktime_add() below) is the
824 * correction for exact:
826 orun++;
828 hrtimer_add_expires(timer, interval);
830 return orun;
832 EXPORT_SYMBOL_GPL(hrtimer_forward);
835 * enqueue_hrtimer - internal function to (re)start a timer
837 * The timer is inserted in expiry order. Insertion into the
838 * red black tree is O(log(n)). Must hold the base lock.
840 * Returns 1 when the new timer is the leftmost timer in the tree.
842 static int enqueue_hrtimer(struct hrtimer *timer,
843 struct hrtimer_clock_base *base)
845 struct rb_node **link = &base->active.rb_node;
846 struct rb_node *parent = NULL;
847 struct hrtimer *entry;
848 int leftmost = 1;
850 debug_hrtimer_activate(timer);
853 * Find the right place in the rbtree:
855 while (*link) {
856 parent = *link;
857 entry = rb_entry(parent, struct hrtimer, node);
859 * We dont care about collisions. Nodes with
860 * the same expiry time stay together.
862 if (hrtimer_get_expires_tv64(timer) <
863 hrtimer_get_expires_tv64(entry)) {
864 link = &(*link)->rb_left;
865 } else {
866 link = &(*link)->rb_right;
867 leftmost = 0;
872 * Insert the timer to the rbtree and check whether it
873 * replaces the first pending timer
875 if (leftmost)
876 base->first = &timer->node;
878 rb_link_node(&timer->node, parent, link);
879 rb_insert_color(&timer->node, &base->active);
881 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
882 * state of a possibly running callback.
884 timer->state |= HRTIMER_STATE_ENQUEUED;
886 return leftmost;
890 * __remove_hrtimer - internal function to remove a timer
892 * Caller must hold the base lock.
894 * High resolution timer mode reprograms the clock event device when the
895 * timer is the one which expires next. The caller can disable this by setting
896 * reprogram to zero. This is useful, when the context does a reprogramming
897 * anyway (e.g. timer interrupt)
899 static void __remove_hrtimer(struct hrtimer *timer,
900 struct hrtimer_clock_base *base,
901 unsigned long newstate, int reprogram)
903 if (timer->state & HRTIMER_STATE_ENQUEUED) {
905 * Remove the timer from the rbtree and replace the
906 * first entry pointer if necessary.
908 if (base->first == &timer->node) {
909 base->first = rb_next(&timer->node);
910 /* Reprogram the clock event device. if enabled */
911 if (reprogram && hrtimer_hres_active())
912 hrtimer_force_reprogram(base->cpu_base);
914 rb_erase(&timer->node, &base->active);
916 timer->state = newstate;
920 * remove hrtimer, called with base lock held
922 static inline int
923 remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
925 if (hrtimer_is_queued(timer)) {
926 int reprogram;
929 * Remove the timer and force reprogramming when high
930 * resolution mode is active and the timer is on the current
931 * CPU. If we remove a timer on another CPU, reprogramming is
932 * skipped. The interrupt event on this CPU is fired and
933 * reprogramming happens in the interrupt handler. This is a
934 * rare case and less expensive than a smp call.
936 debug_hrtimer_deactivate(timer);
937 timer_stats_hrtimer_clear_start_info(timer);
938 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
939 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
940 reprogram);
941 return 1;
943 return 0;
946 int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
947 unsigned long delta_ns, const enum hrtimer_mode mode,
948 int wakeup)
950 struct hrtimer_clock_base *base, *new_base;
951 unsigned long flags;
952 int ret, leftmost;
954 base = lock_hrtimer_base(timer, &flags);
956 /* Remove an active timer from the queue: */
957 ret = remove_hrtimer(timer, base);
959 /* Switch the timer base, if necessary: */
960 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
962 if (mode & HRTIMER_MODE_REL) {
963 tim = ktime_add_safe(tim, new_base->get_time());
965 * CONFIG_TIME_LOW_RES is a temporary way for architectures
966 * to signal that they simply return xtime in
967 * do_gettimeoffset(). In this case we want to round up by
968 * resolution when starting a relative timer, to avoid short
969 * timeouts. This will go away with the GTOD framework.
971 #ifdef CONFIG_TIME_LOW_RES
972 tim = ktime_add_safe(tim, base->resolution);
973 #endif
976 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
978 timer_stats_hrtimer_set_start_info(timer);
980 leftmost = enqueue_hrtimer(timer, new_base);
983 * Only allow reprogramming if the new base is on this CPU.
984 * (it might still be on another CPU if the timer was pending)
986 * XXX send_remote_softirq() ?
988 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases))
989 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
991 unlock_hrtimer_base(timer, &flags);
993 return ret;
997 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
998 * @timer: the timer to be added
999 * @tim: expiry time
1000 * @delta_ns: "slack" range for the timer
1001 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1003 * Returns:
1004 * 0 on success
1005 * 1 when the timer was active
1007 int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1008 unsigned long delta_ns, const enum hrtimer_mode mode)
1010 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1012 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1015 * hrtimer_start - (re)start an hrtimer on the current CPU
1016 * @timer: the timer to be added
1017 * @tim: expiry time
1018 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
1020 * Returns:
1021 * 0 on success
1022 * 1 when the timer was active
1025 hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1027 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
1029 EXPORT_SYMBOL_GPL(hrtimer_start);
1033 * hrtimer_try_to_cancel - try to deactivate a timer
1034 * @timer: hrtimer to stop
1036 * Returns:
1037 * 0 when the timer was not active
1038 * 1 when the timer was active
1039 * -1 when the timer is currently excuting the callback function and
1040 * cannot be stopped
1042 int hrtimer_try_to_cancel(struct hrtimer *timer)
1044 struct hrtimer_clock_base *base;
1045 unsigned long flags;
1046 int ret = -1;
1048 base = lock_hrtimer_base(timer, &flags);
1050 if (!hrtimer_callback_running(timer))
1051 ret = remove_hrtimer(timer, base);
1053 unlock_hrtimer_base(timer, &flags);
1055 return ret;
1058 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
1061 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1062 * @timer: the timer to be cancelled
1064 * Returns:
1065 * 0 when the timer was not active
1066 * 1 when the timer was active
1068 int hrtimer_cancel(struct hrtimer *timer)
1070 for (;;) {
1071 int ret = hrtimer_try_to_cancel(timer);
1073 if (ret >= 0)
1074 return ret;
1075 cpu_relax();
1078 EXPORT_SYMBOL_GPL(hrtimer_cancel);
1081 * hrtimer_get_remaining - get remaining time for the timer
1082 * @timer: the timer to read
1084 ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1086 struct hrtimer_clock_base *base;
1087 unsigned long flags;
1088 ktime_t rem;
1090 base = lock_hrtimer_base(timer, &flags);
1091 rem = hrtimer_expires_remaining(timer);
1092 unlock_hrtimer_base(timer, &flags);
1094 return rem;
1096 EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
1098 #ifdef CONFIG_NO_HZ
1100 * hrtimer_get_next_event - get the time until next expiry event
1102 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1103 * is pending.
1105 ktime_t hrtimer_get_next_event(void)
1107 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1108 struct hrtimer_clock_base *base = cpu_base->clock_base;
1109 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1110 unsigned long flags;
1111 int i;
1113 spin_lock_irqsave(&cpu_base->lock, flags);
1115 if (!hrtimer_hres_active()) {
1116 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1117 struct hrtimer *timer;
1119 if (!base->first)
1120 continue;
1122 timer = rb_entry(base->first, struct hrtimer, node);
1123 delta.tv64 = hrtimer_get_expires_tv64(timer);
1124 delta = ktime_sub(delta, base->get_time());
1125 if (delta.tv64 < mindelta.tv64)
1126 mindelta.tv64 = delta.tv64;
1130 spin_unlock_irqrestore(&cpu_base->lock, flags);
1132 if (mindelta.tv64 < 0)
1133 mindelta.tv64 = 0;
1134 return mindelta;
1136 #endif
1138 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1139 enum hrtimer_mode mode)
1141 struct hrtimer_cpu_base *cpu_base;
1143 memset(timer, 0, sizeof(struct hrtimer));
1145 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1147 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
1148 clock_id = CLOCK_MONOTONIC;
1150 timer->base = &cpu_base->clock_base[clock_id];
1151 INIT_LIST_HEAD(&timer->cb_entry);
1152 hrtimer_init_timer_hres(timer);
1154 #ifdef CONFIG_TIMER_STATS
1155 timer->start_site = NULL;
1156 timer->start_pid = -1;
1157 memset(timer->start_comm, 0, TASK_COMM_LEN);
1158 #endif
1162 * hrtimer_init - initialize a timer to the given clock
1163 * @timer: the timer to be initialized
1164 * @clock_id: the clock to be used
1165 * @mode: timer mode abs/rel
1167 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1168 enum hrtimer_mode mode)
1170 debug_hrtimer_init(timer);
1171 __hrtimer_init(timer, clock_id, mode);
1173 EXPORT_SYMBOL_GPL(hrtimer_init);
1176 * hrtimer_get_res - get the timer resolution for a clock
1177 * @which_clock: which clock to query
1178 * @tp: pointer to timespec variable to store the resolution
1180 * Store the resolution of the clock selected by @which_clock in the
1181 * variable pointed to by @tp.
1183 int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1185 struct hrtimer_cpu_base *cpu_base;
1187 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1188 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
1190 return 0;
1192 EXPORT_SYMBOL_GPL(hrtimer_get_res);
1194 static void __run_hrtimer(struct hrtimer *timer)
1196 struct hrtimer_clock_base *base = timer->base;
1197 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1198 enum hrtimer_restart (*fn)(struct hrtimer *);
1199 int restart;
1201 WARN_ON(!irqs_disabled());
1203 debug_hrtimer_deactivate(timer);
1204 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1205 timer_stats_account_hrtimer(timer);
1206 fn = timer->function;
1209 * Because we run timers from hardirq context, there is no chance
1210 * they get migrated to another cpu, therefore its safe to unlock
1211 * the timer base.
1213 spin_unlock(&cpu_base->lock);
1214 restart = fn(timer);
1215 spin_lock(&cpu_base->lock);
1218 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1219 * we do not reprogramm the event hardware. Happens either in
1220 * hrtimer_start_range_ns() or in hrtimer_interrupt()
1222 if (restart != HRTIMER_NORESTART) {
1223 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1224 enqueue_hrtimer(timer, base);
1226 timer->state &= ~HRTIMER_STATE_CALLBACK;
1229 #ifdef CONFIG_HIGH_RES_TIMERS
1231 static int force_clock_reprogram;
1234 * After 5 iteration's attempts, we consider that hrtimer_interrupt()
1235 * is hanging, which could happen with something that slows the interrupt
1236 * such as the tracing. Then we force the clock reprogramming for each future
1237 * hrtimer interrupts to avoid infinite loops and use the min_delta_ns
1238 * threshold that we will overwrite.
1239 * The next tick event will be scheduled to 3 times we currently spend on
1240 * hrtimer_interrupt(). This gives a good compromise, the cpus will spend
1241 * 1/4 of their time to process the hrtimer interrupts. This is enough to
1242 * let it running without serious starvation.
1245 static inline void
1246 hrtimer_interrupt_hanging(struct clock_event_device *dev,
1247 ktime_t try_time)
1249 force_clock_reprogram = 1;
1250 dev->min_delta_ns = (unsigned long)try_time.tv64 * 3;
1251 printk(KERN_WARNING "hrtimer: interrupt too slow, "
1252 "forcing clock min delta to %lu ns\n", dev->min_delta_ns);
1255 * High resolution timer interrupt
1256 * Called with interrupts disabled
1258 void hrtimer_interrupt(struct clock_event_device *dev)
1260 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1261 struct hrtimer_clock_base *base;
1262 ktime_t expires_next, now;
1263 int nr_retries = 0;
1264 int i;
1266 BUG_ON(!cpu_base->hres_active);
1267 cpu_base->nr_events++;
1268 dev->next_event.tv64 = KTIME_MAX;
1270 retry:
1271 /* 5 retries is enough to notice a hang */
1272 if (!(++nr_retries % 5))
1273 hrtimer_interrupt_hanging(dev, ktime_sub(ktime_get(), now));
1275 now = ktime_get();
1277 expires_next.tv64 = KTIME_MAX;
1279 base = cpu_base->clock_base;
1281 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1282 ktime_t basenow;
1283 struct rb_node *node;
1285 spin_lock(&cpu_base->lock);
1287 basenow = ktime_add(now, base->offset);
1289 while ((node = base->first)) {
1290 struct hrtimer *timer;
1292 timer = rb_entry(node, struct hrtimer, node);
1295 * The immediate goal for using the softexpires is
1296 * minimizing wakeups, not running timers at the
1297 * earliest interrupt after their soft expiration.
1298 * This allows us to avoid using a Priority Search
1299 * Tree, which can answer a stabbing querry for
1300 * overlapping intervals and instead use the simple
1301 * BST we already have.
1302 * We don't add extra wakeups by delaying timers that
1303 * are right-of a not yet expired timer, because that
1304 * timer will have to trigger a wakeup anyway.
1307 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
1308 ktime_t expires;
1310 expires = ktime_sub(hrtimer_get_expires(timer),
1311 base->offset);
1312 if (expires.tv64 < expires_next.tv64)
1313 expires_next = expires;
1314 break;
1317 __run_hrtimer(timer);
1319 spin_unlock(&cpu_base->lock);
1320 base++;
1323 cpu_base->expires_next = expires_next;
1325 /* Reprogramming necessary ? */
1326 if (expires_next.tv64 != KTIME_MAX) {
1327 if (tick_program_event(expires_next, force_clock_reprogram))
1328 goto retry;
1333 * local version of hrtimer_peek_ahead_timers() called with interrupts
1334 * disabled.
1336 static void __hrtimer_peek_ahead_timers(void)
1338 struct tick_device *td;
1340 if (!hrtimer_hres_active())
1341 return;
1343 td = &__get_cpu_var(tick_cpu_device);
1344 if (td && td->evtdev)
1345 hrtimer_interrupt(td->evtdev);
1349 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1351 * hrtimer_peek_ahead_timers will peek at the timer queue of
1352 * the current cpu and check if there are any timers for which
1353 * the soft expires time has passed. If any such timers exist,
1354 * they are run immediately and then removed from the timer queue.
1357 void hrtimer_peek_ahead_timers(void)
1359 unsigned long flags;
1361 local_irq_save(flags);
1362 __hrtimer_peek_ahead_timers();
1363 local_irq_restore(flags);
1366 static void run_hrtimer_softirq(struct softirq_action *h)
1368 hrtimer_peek_ahead_timers();
1371 #else /* CONFIG_HIGH_RES_TIMERS */
1373 static inline void __hrtimer_peek_ahead_timers(void) { }
1375 #endif /* !CONFIG_HIGH_RES_TIMERS */
1378 * Called from timer softirq every jiffy, expire hrtimers:
1380 * For HRT its the fall back code to run the softirq in the timer
1381 * softirq context in case the hrtimer initialization failed or has
1382 * not been done yet.
1384 void hrtimer_run_pending(void)
1386 if (hrtimer_hres_active())
1387 return;
1390 * This _is_ ugly: We have to check in the softirq context,
1391 * whether we can switch to highres and / or nohz mode. The
1392 * clocksource switch happens in the timer interrupt with
1393 * xtime_lock held. Notification from there only sets the
1394 * check bit in the tick_oneshot code, otherwise we might
1395 * deadlock vs. xtime_lock.
1397 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1398 hrtimer_switch_to_hres();
1402 * Called from hardirq context every jiffy
1404 void hrtimer_run_queues(void)
1406 struct rb_node *node;
1407 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1408 struct hrtimer_clock_base *base;
1409 int index, gettime = 1;
1411 if (hrtimer_hres_active())
1412 return;
1414 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1415 base = &cpu_base->clock_base[index];
1417 if (!base->first)
1418 continue;
1420 if (gettime) {
1421 hrtimer_get_softirq_time(cpu_base);
1422 gettime = 0;
1425 spin_lock(&cpu_base->lock);
1427 while ((node = base->first)) {
1428 struct hrtimer *timer;
1430 timer = rb_entry(node, struct hrtimer, node);
1431 if (base->softirq_time.tv64 <=
1432 hrtimer_get_expires_tv64(timer))
1433 break;
1435 __run_hrtimer(timer);
1437 spin_unlock(&cpu_base->lock);
1442 * Sleep related functions:
1444 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
1446 struct hrtimer_sleeper *t =
1447 container_of(timer, struct hrtimer_sleeper, timer);
1448 struct task_struct *task = t->task;
1450 t->task = NULL;
1451 if (task)
1452 wake_up_process(task);
1454 return HRTIMER_NORESTART;
1457 void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1459 sl->timer.function = hrtimer_wakeup;
1460 sl->task = task;
1463 static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
1465 hrtimer_init_sleeper(t, current);
1467 do {
1468 set_current_state(TASK_INTERRUPTIBLE);
1469 hrtimer_start_expires(&t->timer, mode);
1470 if (!hrtimer_active(&t->timer))
1471 t->task = NULL;
1473 if (likely(t->task))
1474 schedule();
1476 hrtimer_cancel(&t->timer);
1477 mode = HRTIMER_MODE_ABS;
1479 } while (t->task && !signal_pending(current));
1481 __set_current_state(TASK_RUNNING);
1483 return t->task == NULL;
1486 static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1488 struct timespec rmt;
1489 ktime_t rem;
1491 rem = hrtimer_expires_remaining(timer);
1492 if (rem.tv64 <= 0)
1493 return 0;
1494 rmt = ktime_to_timespec(rem);
1496 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1497 return -EFAULT;
1499 return 1;
1502 long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
1504 struct hrtimer_sleeper t;
1505 struct timespec __user *rmtp;
1506 int ret = 0;
1508 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1509 HRTIMER_MODE_ABS);
1510 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
1512 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
1513 goto out;
1515 rmtp = restart->nanosleep.rmtp;
1516 if (rmtp) {
1517 ret = update_rmtp(&t.timer, rmtp);
1518 if (ret <= 0)
1519 goto out;
1522 /* The other values in restart are already filled in */
1523 ret = -ERESTART_RESTARTBLOCK;
1524 out:
1525 destroy_hrtimer_on_stack(&t.timer);
1526 return ret;
1529 long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
1530 const enum hrtimer_mode mode, const clockid_t clockid)
1532 struct restart_block *restart;
1533 struct hrtimer_sleeper t;
1534 int ret = 0;
1535 unsigned long slack;
1537 slack = current->timer_slack_ns;
1538 if (rt_task(current))
1539 slack = 0;
1541 hrtimer_init_on_stack(&t.timer, clockid, mode);
1542 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
1543 if (do_nanosleep(&t, mode))
1544 goto out;
1546 /* Absolute timers do not update the rmtp value and restart: */
1547 if (mode == HRTIMER_MODE_ABS) {
1548 ret = -ERESTARTNOHAND;
1549 goto out;
1552 if (rmtp) {
1553 ret = update_rmtp(&t.timer, rmtp);
1554 if (ret <= 0)
1555 goto out;
1558 restart = &current_thread_info()->restart_block;
1559 restart->fn = hrtimer_nanosleep_restart;
1560 restart->nanosleep.index = t.timer.base->index;
1561 restart->nanosleep.rmtp = rmtp;
1562 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
1564 ret = -ERESTART_RESTARTBLOCK;
1565 out:
1566 destroy_hrtimer_on_stack(&t.timer);
1567 return ret;
1570 SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1571 struct timespec __user *, rmtp)
1573 struct timespec tu;
1575 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1576 return -EFAULT;
1578 if (!timespec_valid(&tu))
1579 return -EINVAL;
1581 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1585 * Functions related to boot-time initialization:
1587 static void __cpuinit init_hrtimers_cpu(int cpu)
1589 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
1590 int i;
1592 spin_lock_init(&cpu_base->lock);
1594 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1595 cpu_base->clock_base[i].cpu_base = cpu_base;
1597 hrtimer_init_hres(cpu_base);
1600 #ifdef CONFIG_HOTPLUG_CPU
1602 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1603 struct hrtimer_clock_base *new_base)
1605 struct hrtimer *timer;
1606 struct rb_node *node;
1608 while ((node = rb_first(&old_base->active))) {
1609 timer = rb_entry(node, struct hrtimer, node);
1610 BUG_ON(hrtimer_callback_running(timer));
1611 debug_hrtimer_deactivate(timer);
1614 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1615 * timer could be seen as !active and just vanish away
1616 * under us on another CPU
1618 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
1619 timer->base = new_base;
1621 * Enqueue the timers on the new cpu. This does not
1622 * reprogram the event device in case the timer
1623 * expires before the earliest on this CPU, but we run
1624 * hrtimer_interrupt after we migrated everything to
1625 * sort out already expired timers and reprogram the
1626 * event device.
1628 enqueue_hrtimer(timer, new_base);
1630 /* Clear the migration state bit */
1631 timer->state &= ~HRTIMER_STATE_MIGRATE;
1635 static void migrate_hrtimers(int scpu)
1637 struct hrtimer_cpu_base *old_base, *new_base;
1638 int i;
1640 BUG_ON(cpu_online(scpu));
1641 tick_cancel_sched_timer(scpu);
1643 local_irq_disable();
1644 old_base = &per_cpu(hrtimer_bases, scpu);
1645 new_base = &__get_cpu_var(hrtimer_bases);
1647 * The caller is globally serialized and nobody else
1648 * takes two locks at once, deadlock is not possible.
1650 spin_lock(&new_base->lock);
1651 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1653 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1654 migrate_hrtimer_list(&old_base->clock_base[i],
1655 &new_base->clock_base[i]);
1658 spin_unlock(&old_base->lock);
1659 spin_unlock(&new_base->lock);
1661 /* Check, if we got expired work to do */
1662 __hrtimer_peek_ahead_timers();
1663 local_irq_enable();
1666 #endif /* CONFIG_HOTPLUG_CPU */
1668 static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
1669 unsigned long action, void *hcpu)
1671 int scpu = (long)hcpu;
1673 switch (action) {
1675 case CPU_UP_PREPARE:
1676 case CPU_UP_PREPARE_FROZEN:
1677 init_hrtimers_cpu(scpu);
1678 break;
1680 #ifdef CONFIG_HOTPLUG_CPU
1681 case CPU_DYING:
1682 case CPU_DYING_FROZEN:
1683 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1684 break;
1685 case CPU_DEAD:
1686 case CPU_DEAD_FROZEN:
1688 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
1689 migrate_hrtimers(scpu);
1690 break;
1692 #endif
1694 default:
1695 break;
1698 return NOTIFY_OK;
1701 static struct notifier_block __cpuinitdata hrtimers_nb = {
1702 .notifier_call = hrtimer_cpu_notify,
1705 void __init hrtimers_init(void)
1707 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1708 (void *)(long)smp_processor_id());
1709 register_cpu_notifier(&hrtimers_nb);
1710 #ifdef CONFIG_HIGH_RES_TIMERS
1711 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1712 #endif
1716 * schedule_hrtimeout_range - sleep until timeout
1717 * @expires: timeout value (ktime_t)
1718 * @delta: slack in expires timeout (ktime_t)
1719 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1721 * Make the current task sleep until the given expiry time has
1722 * elapsed. The routine will return immediately unless
1723 * the current task state has been set (see set_current_state()).
1725 * The @delta argument gives the kernel the freedom to schedule the
1726 * actual wakeup to a time that is both power and performance friendly.
1727 * The kernel give the normal best effort behavior for "@expires+@delta",
1728 * but may decide to fire the timer earlier, but no earlier than @expires.
1730 * You can set the task state as follows -
1732 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1733 * pass before the routine returns.
1735 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1736 * delivered to the current task.
1738 * The current task state is guaranteed to be TASK_RUNNING when this
1739 * routine returns.
1741 * Returns 0 when the timer has expired otherwise -EINTR
1743 int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1744 const enum hrtimer_mode mode)
1746 struct hrtimer_sleeper t;
1749 * Optimize when a zero timeout value is given. It does not
1750 * matter whether this is an absolute or a relative time.
1752 if (expires && !expires->tv64) {
1753 __set_current_state(TASK_RUNNING);
1754 return 0;
1758 * A NULL parameter means "inifinte"
1760 if (!expires) {
1761 schedule();
1762 __set_current_state(TASK_RUNNING);
1763 return -EINTR;
1766 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode);
1767 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1769 hrtimer_init_sleeper(&t, current);
1771 hrtimer_start_expires(&t.timer, mode);
1772 if (!hrtimer_active(&t.timer))
1773 t.task = NULL;
1775 if (likely(t.task))
1776 schedule();
1778 hrtimer_cancel(&t.timer);
1779 destroy_hrtimer_on_stack(&t.timer);
1781 __set_current_state(TASK_RUNNING);
1783 return !t.task ? 0 : -EINTR;
1785 EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1788 * schedule_hrtimeout - sleep until timeout
1789 * @expires: timeout value (ktime_t)
1790 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1792 * Make the current task sleep until the given expiry time has
1793 * elapsed. The routine will return immediately unless
1794 * the current task state has been set (see set_current_state()).
1796 * You can set the task state as follows -
1798 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1799 * pass before the routine returns.
1801 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1802 * delivered to the current task.
1804 * The current task state is guaranteed to be TASK_RUNNING when this
1805 * routine returns.
1807 * Returns 0 when the timer has expired otherwise -EINTR
1809 int __sched schedule_hrtimeout(ktime_t *expires,
1810 const enum hrtimer_mode mode)
1812 return schedule_hrtimeout_range(expires, 0, mode);
1814 EXPORT_SYMBOL_GPL(schedule_hrtimeout);