USB: UHCI: disable remote wakeup when it's not needed
[linux-2.6/mini2440.git] / kernel / hrtimer.c
blob9af1d6a8095e40f80e64cdade6c0c3b505184466
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/irq.h>
36 #include <linux/module.h>
37 #include <linux/percpu.h>
38 #include <linux/hrtimer.h>
39 #include <linux/notifier.h>
40 #include <linux/syscalls.h>
41 #include <linux/kallsyms.h>
42 #include <linux/interrupt.h>
43 #include <linux/tick.h>
44 #include <linux/seq_file.h>
45 #include <linux/err.h>
46 #include <linux/debugobjects.h>
48 #include <asm/uaccess.h>
50 /**
51 * ktime_get - get the monotonic time in ktime_t format
53 * returns the time in ktime_t format
55 ktime_t ktime_get(void)
57 struct timespec now;
59 ktime_get_ts(&now);
61 return timespec_to_ktime(now);
63 EXPORT_SYMBOL_GPL(ktime_get);
65 /**
66 * ktime_get_real - get the real (wall-) time in ktime_t format
68 * returns the time in ktime_t format
70 ktime_t ktime_get_real(void)
72 struct timespec now;
74 getnstimeofday(&now);
76 return timespec_to_ktime(now);
79 EXPORT_SYMBOL_GPL(ktime_get_real);
82 * The timer bases:
84 * Note: If we want to add new timer bases, we have to skip the two
85 * clock ids captured by the cpu-timers. We do this by holding empty
86 * entries rather than doing math adjustment of the clock ids.
87 * This ensures that we capture erroneous accesses to these clock ids
88 * rather than moving them into the range of valid clock id's.
90 DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
93 .clock_base =
96 .index = CLOCK_REALTIME,
97 .get_time = &ktime_get_real,
98 .resolution = KTIME_LOW_RES,
101 .index = CLOCK_MONOTONIC,
102 .get_time = &ktime_get,
103 .resolution = KTIME_LOW_RES,
109 * ktime_get_ts - get the monotonic clock in timespec format
110 * @ts: pointer to timespec variable
112 * The function calculates the monotonic clock from the realtime
113 * clock and the wall_to_monotonic offset and stores the result
114 * in normalized timespec format in the variable pointed to by @ts.
116 void ktime_get_ts(struct timespec *ts)
118 struct timespec tomono;
119 unsigned long seq;
121 do {
122 seq = read_seqbegin(&xtime_lock);
123 getnstimeofday(ts);
124 tomono = wall_to_monotonic;
126 } while (read_seqretry(&xtime_lock, seq));
128 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
129 ts->tv_nsec + tomono.tv_nsec);
131 EXPORT_SYMBOL_GPL(ktime_get_ts);
134 * Get the coarse grained time at the softirq based on xtime and
135 * wall_to_monotonic.
137 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
139 ktime_t xtim, tomono;
140 struct timespec xts, tom;
141 unsigned long seq;
143 do {
144 seq = read_seqbegin(&xtime_lock);
145 xts = current_kernel_time();
146 tom = wall_to_monotonic;
147 } while (read_seqretry(&xtime_lock, seq));
149 xtim = timespec_to_ktime(xts);
150 tomono = timespec_to_ktime(tom);
151 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
152 base->clock_base[CLOCK_MONOTONIC].softirq_time =
153 ktime_add(xtim, tomono);
157 * Helper function to check, whether the timer is running the callback
158 * function
160 static inline int hrtimer_callback_running(struct hrtimer *timer)
162 return timer->state & HRTIMER_STATE_CALLBACK;
166 * Functions and macros which are different for UP/SMP systems are kept in a
167 * single place
169 #ifdef CONFIG_SMP
172 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
173 * means that all timers which are tied to this base via timer->base are
174 * locked, and the base itself is locked too.
176 * So __run_timers/migrate_timers can safely modify all timers which could
177 * be found on the lists/queues.
179 * When the timer's base is locked, and the timer removed from list, it is
180 * possible to set timer->base = NULL and drop the lock: the timer remains
181 * locked.
183 static
184 struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
185 unsigned long *flags)
187 struct hrtimer_clock_base *base;
189 for (;;) {
190 base = timer->base;
191 if (likely(base != NULL)) {
192 spin_lock_irqsave(&base->cpu_base->lock, *flags);
193 if (likely(base == timer->base))
194 return base;
195 /* The timer has migrated to another CPU: */
196 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
198 cpu_relax();
203 * Switch the timer base to the current CPU when possible.
205 static inline struct hrtimer_clock_base *
206 switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
208 struct hrtimer_clock_base *new_base;
209 struct hrtimer_cpu_base *new_cpu_base;
211 new_cpu_base = &__get_cpu_var(hrtimer_bases);
212 new_base = &new_cpu_base->clock_base[base->index];
214 if (base != new_base) {
216 * We are trying to schedule the timer on the local CPU.
217 * However we can't change timer's base while it is running,
218 * so we keep it on the same CPU. No hassle vs. reprogramming
219 * the event source in the high resolution case. The softirq
220 * code will take care of this when the timer function has
221 * completed. There is no conflict as we hold the lock until
222 * the timer is enqueued.
224 if (unlikely(hrtimer_callback_running(timer)))
225 return base;
227 /* See the comment in lock_timer_base() */
228 timer->base = NULL;
229 spin_unlock(&base->cpu_base->lock);
230 spin_lock(&new_base->cpu_base->lock);
231 timer->base = new_base;
233 return new_base;
236 #else /* CONFIG_SMP */
238 static inline struct hrtimer_clock_base *
239 lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
241 struct hrtimer_clock_base *base = timer->base;
243 spin_lock_irqsave(&base->cpu_base->lock, *flags);
245 return base;
248 # define switch_hrtimer_base(t, b) (b)
250 #endif /* !CONFIG_SMP */
253 * Functions for the union type storage format of ktime_t which are
254 * too large for inlining:
256 #if BITS_PER_LONG < 64
257 # ifndef CONFIG_KTIME_SCALAR
259 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
260 * @kt: addend
261 * @nsec: the scalar nsec value to add
263 * Returns the sum of kt and nsec in ktime_t format
265 ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
267 ktime_t tmp;
269 if (likely(nsec < NSEC_PER_SEC)) {
270 tmp.tv64 = nsec;
271 } else {
272 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
274 tmp = ktime_set((long)nsec, rem);
277 return ktime_add(kt, tmp);
280 EXPORT_SYMBOL_GPL(ktime_add_ns);
283 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
284 * @kt: minuend
285 * @nsec: the scalar nsec value to subtract
287 * Returns the subtraction of @nsec from @kt in ktime_t format
289 ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
291 ktime_t tmp;
293 if (likely(nsec < NSEC_PER_SEC)) {
294 tmp.tv64 = nsec;
295 } else {
296 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
298 tmp = ktime_set((long)nsec, rem);
301 return ktime_sub(kt, tmp);
304 EXPORT_SYMBOL_GPL(ktime_sub_ns);
305 # endif /* !CONFIG_KTIME_SCALAR */
308 * Divide a ktime value by a nanosecond value
310 u64 ktime_divns(const ktime_t kt, s64 div)
312 u64 dclc, inc, dns;
313 int sft = 0;
315 dclc = dns = ktime_to_ns(kt);
316 inc = div;
317 /* Make sure the divisor is less than 2^32: */
318 while (div >> 32) {
319 sft++;
320 div >>= 1;
322 dclc >>= sft;
323 do_div(dclc, (unsigned long) div);
325 return dclc;
327 #endif /* BITS_PER_LONG >= 64 */
330 * Add two ktime values and do a safety check for overflow:
332 ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
334 ktime_t res = ktime_add(lhs, rhs);
337 * We use KTIME_SEC_MAX here, the maximum timeout which we can
338 * return to user space in a timespec:
340 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
341 res = ktime_set(KTIME_SEC_MAX, 0);
343 return res;
346 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
348 static struct debug_obj_descr hrtimer_debug_descr;
351 * fixup_init is called when:
352 * - an active object is initialized
354 static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
356 struct hrtimer *timer = addr;
358 switch (state) {
359 case ODEBUG_STATE_ACTIVE:
360 hrtimer_cancel(timer);
361 debug_object_init(timer, &hrtimer_debug_descr);
362 return 1;
363 default:
364 return 0;
369 * fixup_activate is called when:
370 * - an active object is activated
371 * - an unknown object is activated (might be a statically initialized object)
373 static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
375 switch (state) {
377 case ODEBUG_STATE_NOTAVAILABLE:
378 WARN_ON_ONCE(1);
379 return 0;
381 case ODEBUG_STATE_ACTIVE:
382 WARN_ON(1);
384 default:
385 return 0;
390 * fixup_free is called when:
391 * - an active object is freed
393 static int hrtimer_fixup_free(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_free(timer, &hrtimer_debug_descr);
401 return 1;
402 default:
403 return 0;
407 static struct debug_obj_descr hrtimer_debug_descr = {
408 .name = "hrtimer",
409 .fixup_init = hrtimer_fixup_init,
410 .fixup_activate = hrtimer_fixup_activate,
411 .fixup_free = hrtimer_fixup_free,
414 static inline void debug_hrtimer_init(struct hrtimer *timer)
416 debug_object_init(timer, &hrtimer_debug_descr);
419 static inline void debug_hrtimer_activate(struct hrtimer *timer)
421 debug_object_activate(timer, &hrtimer_debug_descr);
424 static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
426 debug_object_deactivate(timer, &hrtimer_debug_descr);
429 static inline void debug_hrtimer_free(struct hrtimer *timer)
431 debug_object_free(timer, &hrtimer_debug_descr);
434 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
435 enum hrtimer_mode mode);
437 void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
438 enum hrtimer_mode mode)
440 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
441 __hrtimer_init(timer, clock_id, mode);
444 void destroy_hrtimer_on_stack(struct hrtimer *timer)
446 debug_object_free(timer, &hrtimer_debug_descr);
449 #else
450 static inline void debug_hrtimer_init(struct hrtimer *timer) { }
451 static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
452 static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
453 #endif
456 * Check, whether the timer is on the callback pending list
458 static inline int hrtimer_cb_pending(const struct hrtimer *timer)
460 return timer->state & HRTIMER_STATE_PENDING;
464 * Remove a timer from the callback pending list
466 static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
468 list_del_init(&timer->cb_entry);
471 /* High resolution timer related functions */
472 #ifdef CONFIG_HIGH_RES_TIMERS
475 * High resolution timer enabled ?
477 static int hrtimer_hres_enabled __read_mostly = 1;
480 * Enable / Disable high resolution mode
482 static int __init setup_hrtimer_hres(char *str)
484 if (!strcmp(str, "off"))
485 hrtimer_hres_enabled = 0;
486 else if (!strcmp(str, "on"))
487 hrtimer_hres_enabled = 1;
488 else
489 return 0;
490 return 1;
493 __setup("highres=", setup_hrtimer_hres);
496 * hrtimer_high_res_enabled - query, if the highres mode is enabled
498 static inline int hrtimer_is_hres_enabled(void)
500 return hrtimer_hres_enabled;
504 * Is the high resolution mode active ?
506 static inline int hrtimer_hres_active(void)
508 return __get_cpu_var(hrtimer_bases).hres_active;
512 * Reprogram the event source with checking both queues for the
513 * next event
514 * Called with interrupts disabled and base->lock held
516 static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
518 int i;
519 struct hrtimer_clock_base *base = cpu_base->clock_base;
520 ktime_t expires;
522 cpu_base->expires_next.tv64 = KTIME_MAX;
524 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
525 struct hrtimer *timer;
527 if (!base->first)
528 continue;
529 timer = rb_entry(base->first, struct hrtimer, node);
530 expires = ktime_sub(timer->expires, base->offset);
531 if (expires.tv64 < cpu_base->expires_next.tv64)
532 cpu_base->expires_next = expires;
535 if (cpu_base->expires_next.tv64 != KTIME_MAX)
536 tick_program_event(cpu_base->expires_next, 1);
540 * Shared reprogramming for clock_realtime and clock_monotonic
542 * When a timer is enqueued and expires earlier than the already enqueued
543 * timers, we have to check, whether it expires earlier than the timer for
544 * which the clock event device was armed.
546 * Called with interrupts disabled and base->cpu_base.lock held
548 static int hrtimer_reprogram(struct hrtimer *timer,
549 struct hrtimer_clock_base *base)
551 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
552 ktime_t expires = ktime_sub(timer->expires, base->offset);
553 int res;
555 WARN_ON_ONCE(timer->expires.tv64 < 0);
558 * When the callback is running, we do not reprogram the clock event
559 * device. The timer callback is either running on a different CPU or
560 * the callback is executed in the hrtimer_interrupt context. The
561 * reprogramming is handled either by the softirq, which called the
562 * callback or at the end of the hrtimer_interrupt.
564 if (hrtimer_callback_running(timer))
565 return 0;
568 * CLOCK_REALTIME timer might be requested with an absolute
569 * expiry time which is less than base->offset. Nothing wrong
570 * about that, just avoid to call into the tick code, which
571 * has now objections against negative expiry values.
573 if (expires.tv64 < 0)
574 return -ETIME;
576 if (expires.tv64 >= expires_next->tv64)
577 return 0;
580 * Clockevents returns -ETIME, when the event was in the past.
582 res = tick_program_event(expires, 0);
583 if (!IS_ERR_VALUE(res))
584 *expires_next = expires;
585 return res;
590 * Retrigger next event is called after clock was set
592 * Called with interrupts disabled via on_each_cpu()
594 static void retrigger_next_event(void *arg)
596 struct hrtimer_cpu_base *base;
597 struct timespec realtime_offset;
598 unsigned long seq;
600 if (!hrtimer_hres_active())
601 return;
603 do {
604 seq = read_seqbegin(&xtime_lock);
605 set_normalized_timespec(&realtime_offset,
606 -wall_to_monotonic.tv_sec,
607 -wall_to_monotonic.tv_nsec);
608 } while (read_seqretry(&xtime_lock, seq));
610 base = &__get_cpu_var(hrtimer_bases);
612 /* Adjust CLOCK_REALTIME offset */
613 spin_lock(&base->lock);
614 base->clock_base[CLOCK_REALTIME].offset =
615 timespec_to_ktime(realtime_offset);
617 hrtimer_force_reprogram(base);
618 spin_unlock(&base->lock);
622 * Clock realtime was set
624 * Change the offset of the realtime clock vs. the monotonic
625 * clock.
627 * We might have to reprogram the high resolution timer interrupt. On
628 * SMP we call the architecture specific code to retrigger _all_ high
629 * resolution timer interrupts. On UP we just disable interrupts and
630 * call the high resolution interrupt code.
632 void clock_was_set(void)
634 /* Retrigger the CPU local events everywhere */
635 on_each_cpu(retrigger_next_event, NULL, 0, 1);
639 * During resume we might have to reprogram the high resolution timer
640 * interrupt (on the local CPU):
642 void hres_timers_resume(void)
644 WARN_ON_ONCE(num_online_cpus() > 1);
646 /* Retrigger the CPU local events: */
647 retrigger_next_event(NULL);
651 * Initialize the high resolution related parts of cpu_base
653 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
655 base->expires_next.tv64 = KTIME_MAX;
656 base->hres_active = 0;
660 * Initialize the high resolution related parts of a hrtimer
662 static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
667 * When High resolution timers are active, try to reprogram. Note, that in case
668 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
669 * check happens. The timer gets enqueued into the rbtree. The reprogramming
670 * and expiry check is done in the hrtimer_interrupt or in the softirq.
672 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
673 struct hrtimer_clock_base *base)
675 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
677 /* Timer is expired, act upon the callback mode */
678 switch(timer->cb_mode) {
679 case HRTIMER_CB_IRQSAFE_NO_RESTART:
680 debug_hrtimer_deactivate(timer);
682 * We can call the callback from here. No restart
683 * happens, so no danger of recursion
685 BUG_ON(timer->function(timer) != HRTIMER_NORESTART);
686 return 1;
687 case HRTIMER_CB_IRQSAFE_NO_SOFTIRQ:
689 * This is solely for the sched tick emulation with
690 * dynamic tick support to ensure that we do not
691 * restart the tick right on the edge and end up with
692 * the tick timer in the softirq ! The calling site
693 * takes care of this.
695 debug_hrtimer_deactivate(timer);
696 return 1;
697 case HRTIMER_CB_IRQSAFE:
698 case HRTIMER_CB_SOFTIRQ:
700 * Move everything else into the softirq pending list !
702 list_add_tail(&timer->cb_entry,
703 &base->cpu_base->cb_pending);
704 timer->state = HRTIMER_STATE_PENDING;
705 return 1;
706 default:
707 BUG();
710 return 0;
714 * Switch to high resolution mode
716 static int hrtimer_switch_to_hres(void)
718 int cpu = smp_processor_id();
719 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
720 unsigned long flags;
722 if (base->hres_active)
723 return 1;
725 local_irq_save(flags);
727 if (tick_init_highres()) {
728 local_irq_restore(flags);
729 printk(KERN_WARNING "Could not switch to high resolution "
730 "mode on CPU %d\n", cpu);
731 return 0;
733 base->hres_active = 1;
734 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
735 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
737 tick_setup_sched_timer();
739 /* "Retrigger" the interrupt to get things going */
740 retrigger_next_event(NULL);
741 local_irq_restore(flags);
742 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
743 smp_processor_id());
744 return 1;
747 static inline void hrtimer_raise_softirq(void)
749 raise_softirq(HRTIMER_SOFTIRQ);
752 #else
754 static inline int hrtimer_hres_active(void) { return 0; }
755 static inline int hrtimer_is_hres_enabled(void) { return 0; }
756 static inline int hrtimer_switch_to_hres(void) { return 0; }
757 static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
758 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
759 struct hrtimer_clock_base *base)
761 return 0;
763 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
764 static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
765 static inline int hrtimer_reprogram(struct hrtimer *timer,
766 struct hrtimer_clock_base *base)
768 return 0;
770 static inline void hrtimer_raise_softirq(void) { }
772 #endif /* CONFIG_HIGH_RES_TIMERS */
774 #ifdef CONFIG_TIMER_STATS
775 void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
777 if (timer->start_site)
778 return;
780 timer->start_site = addr;
781 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
782 timer->start_pid = current->pid;
784 #endif
787 * Counterpart to lock_hrtimer_base above:
789 static inline
790 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
792 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
796 * hrtimer_forward - forward the timer expiry
797 * @timer: hrtimer to forward
798 * @now: forward past this time
799 * @interval: the interval to forward
801 * Forward the timer expiry so it will expire in the future.
802 * Returns the number of overruns.
804 u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
806 u64 orun = 1;
807 ktime_t delta;
809 delta = ktime_sub(now, timer->expires);
811 if (delta.tv64 < 0)
812 return 0;
814 if (interval.tv64 < timer->base->resolution.tv64)
815 interval.tv64 = timer->base->resolution.tv64;
817 if (unlikely(delta.tv64 >= interval.tv64)) {
818 s64 incr = ktime_to_ns(interval);
820 orun = ktime_divns(delta, incr);
821 timer->expires = ktime_add_ns(timer->expires, incr * orun);
822 if (timer->expires.tv64 > now.tv64)
823 return orun;
825 * This (and the ktime_add() below) is the
826 * correction for exact:
828 orun++;
830 timer->expires = ktime_add_safe(timer->expires, interval);
832 return orun;
834 EXPORT_SYMBOL_GPL(hrtimer_forward);
837 * enqueue_hrtimer - internal function to (re)start a timer
839 * The timer is inserted in expiry order. Insertion into the
840 * red black tree is O(log(n)). Must hold the base lock.
842 static void enqueue_hrtimer(struct hrtimer *timer,
843 struct hrtimer_clock_base *base, int reprogram)
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 (timer->expires.tv64 < entry->expires.tv64) {
863 link = &(*link)->rb_left;
864 } else {
865 link = &(*link)->rb_right;
866 leftmost = 0;
871 * Insert the timer to the rbtree and check whether it
872 * replaces the first pending timer
874 if (leftmost) {
876 * Reprogram the clock event device. When the timer is already
877 * expired hrtimer_enqueue_reprogram has either called the
878 * callback or added it to the pending list and raised the
879 * softirq.
881 * This is a NOP for !HIGHRES
883 if (reprogram && hrtimer_enqueue_reprogram(timer, base))
884 return;
886 base->first = &timer->node;
889 rb_link_node(&timer->node, parent, link);
890 rb_insert_color(&timer->node, &base->active);
892 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
893 * state of a possibly running callback.
895 timer->state |= HRTIMER_STATE_ENQUEUED;
899 * __remove_hrtimer - internal function to remove a timer
901 * Caller must hold the base lock.
903 * High resolution timer mode reprograms the clock event device when the
904 * timer is the one which expires next. The caller can disable this by setting
905 * reprogram to zero. This is useful, when the context does a reprogramming
906 * anyway (e.g. timer interrupt)
908 static void __remove_hrtimer(struct hrtimer *timer,
909 struct hrtimer_clock_base *base,
910 unsigned long newstate, int reprogram)
912 /* High res. callback list. NOP for !HIGHRES */
913 if (hrtimer_cb_pending(timer))
914 hrtimer_remove_cb_pending(timer);
915 else {
917 * Remove the timer from the rbtree and replace the
918 * first entry pointer if necessary.
920 if (base->first == &timer->node) {
921 base->first = rb_next(&timer->node);
922 /* Reprogram the clock event device. if enabled */
923 if (reprogram && hrtimer_hres_active())
924 hrtimer_force_reprogram(base->cpu_base);
926 rb_erase(&timer->node, &base->active);
928 timer->state = newstate;
932 * remove hrtimer, called with base lock held
934 static inline int
935 remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
937 if (hrtimer_is_queued(timer)) {
938 int reprogram;
941 * Remove the timer and force reprogramming when high
942 * resolution mode is active and the timer is on the current
943 * CPU. If we remove a timer on another CPU, reprogramming is
944 * skipped. The interrupt event on this CPU is fired and
945 * reprogramming happens in the interrupt handler. This is a
946 * rare case and less expensive than a smp call.
948 debug_hrtimer_deactivate(timer);
949 timer_stats_hrtimer_clear_start_info(timer);
950 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
951 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
952 reprogram);
953 return 1;
955 return 0;
959 * hrtimer_start - (re)start an relative timer on the current CPU
960 * @timer: the timer to be added
961 * @tim: expiry time
962 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
964 * Returns:
965 * 0 on success
966 * 1 when the timer was active
969 hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
971 struct hrtimer_clock_base *base, *new_base;
972 unsigned long flags;
973 int ret, raise;
975 base = lock_hrtimer_base(timer, &flags);
977 /* Remove an active timer from the queue: */
978 ret = remove_hrtimer(timer, base);
980 /* Switch the timer base, if necessary: */
981 new_base = switch_hrtimer_base(timer, base);
983 if (mode == HRTIMER_MODE_REL) {
984 tim = ktime_add_safe(tim, new_base->get_time());
986 * CONFIG_TIME_LOW_RES is a temporary way for architectures
987 * to signal that they simply return xtime in
988 * do_gettimeoffset(). In this case we want to round up by
989 * resolution when starting a relative timer, to avoid short
990 * timeouts. This will go away with the GTOD framework.
992 #ifdef CONFIG_TIME_LOW_RES
993 tim = ktime_add_safe(tim, base->resolution);
994 #endif
997 timer->expires = tim;
999 timer_stats_hrtimer_set_start_info(timer);
1002 * Only allow reprogramming if the new base is on this CPU.
1003 * (it might still be on another CPU if the timer was pending)
1005 enqueue_hrtimer(timer, new_base,
1006 new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
1009 * The timer may be expired and moved to the cb_pending
1010 * list. We can not raise the softirq with base lock held due
1011 * to a possible deadlock with runqueue lock.
1013 raise = timer->state == HRTIMER_STATE_PENDING;
1015 unlock_hrtimer_base(timer, &flags);
1017 if (raise)
1018 hrtimer_raise_softirq();
1020 return ret;
1022 EXPORT_SYMBOL_GPL(hrtimer_start);
1025 * hrtimer_try_to_cancel - try to deactivate a timer
1026 * @timer: hrtimer to stop
1028 * Returns:
1029 * 0 when the timer was not active
1030 * 1 when the timer was active
1031 * -1 when the timer is currently excuting the callback function and
1032 * cannot be stopped
1034 int hrtimer_try_to_cancel(struct hrtimer *timer)
1036 struct hrtimer_clock_base *base;
1037 unsigned long flags;
1038 int ret = -1;
1040 base = lock_hrtimer_base(timer, &flags);
1042 if (!hrtimer_callback_running(timer))
1043 ret = remove_hrtimer(timer, base);
1045 unlock_hrtimer_base(timer, &flags);
1047 return ret;
1050 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
1053 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1054 * @timer: the timer to be cancelled
1056 * Returns:
1057 * 0 when the timer was not active
1058 * 1 when the timer was active
1060 int hrtimer_cancel(struct hrtimer *timer)
1062 for (;;) {
1063 int ret = hrtimer_try_to_cancel(timer);
1065 if (ret >= 0)
1066 return ret;
1067 cpu_relax();
1070 EXPORT_SYMBOL_GPL(hrtimer_cancel);
1073 * hrtimer_get_remaining - get remaining time for the timer
1074 * @timer: the timer to read
1076 ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1078 struct hrtimer_clock_base *base;
1079 unsigned long flags;
1080 ktime_t rem;
1082 base = lock_hrtimer_base(timer, &flags);
1083 rem = ktime_sub(timer->expires, base->get_time());
1084 unlock_hrtimer_base(timer, &flags);
1086 return rem;
1088 EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
1090 #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
1092 * hrtimer_get_next_event - get the time until next expiry event
1094 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1095 * is pending.
1097 ktime_t hrtimer_get_next_event(void)
1099 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1100 struct hrtimer_clock_base *base = cpu_base->clock_base;
1101 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1102 unsigned long flags;
1103 int i;
1105 spin_lock_irqsave(&cpu_base->lock, flags);
1107 if (!hrtimer_hres_active()) {
1108 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1109 struct hrtimer *timer;
1111 if (!base->first)
1112 continue;
1114 timer = rb_entry(base->first, struct hrtimer, node);
1115 delta.tv64 = timer->expires.tv64;
1116 delta = ktime_sub(delta, base->get_time());
1117 if (delta.tv64 < mindelta.tv64)
1118 mindelta.tv64 = delta.tv64;
1122 spin_unlock_irqrestore(&cpu_base->lock, flags);
1124 if (mindelta.tv64 < 0)
1125 mindelta.tv64 = 0;
1126 return mindelta;
1128 #endif
1130 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1131 enum hrtimer_mode mode)
1133 struct hrtimer_cpu_base *cpu_base;
1135 memset(timer, 0, sizeof(struct hrtimer));
1137 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1139 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
1140 clock_id = CLOCK_MONOTONIC;
1142 timer->base = &cpu_base->clock_base[clock_id];
1143 INIT_LIST_HEAD(&timer->cb_entry);
1144 hrtimer_init_timer_hres(timer);
1146 #ifdef CONFIG_TIMER_STATS
1147 timer->start_site = NULL;
1148 timer->start_pid = -1;
1149 memset(timer->start_comm, 0, TASK_COMM_LEN);
1150 #endif
1154 * hrtimer_init - initialize a timer to the given clock
1155 * @timer: the timer to be initialized
1156 * @clock_id: the clock to be used
1157 * @mode: timer mode abs/rel
1159 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1160 enum hrtimer_mode mode)
1162 debug_hrtimer_init(timer);
1163 __hrtimer_init(timer, clock_id, mode);
1165 EXPORT_SYMBOL_GPL(hrtimer_init);
1168 * hrtimer_get_res - get the timer resolution for a clock
1169 * @which_clock: which clock to query
1170 * @tp: pointer to timespec variable to store the resolution
1172 * Store the resolution of the clock selected by @which_clock in the
1173 * variable pointed to by @tp.
1175 int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1177 struct hrtimer_cpu_base *cpu_base;
1179 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1180 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
1182 return 0;
1184 EXPORT_SYMBOL_GPL(hrtimer_get_res);
1186 static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base)
1188 spin_lock_irq(&cpu_base->lock);
1190 while (!list_empty(&cpu_base->cb_pending)) {
1191 enum hrtimer_restart (*fn)(struct hrtimer *);
1192 struct hrtimer *timer;
1193 int restart;
1195 timer = list_entry(cpu_base->cb_pending.next,
1196 struct hrtimer, cb_entry);
1198 debug_hrtimer_deactivate(timer);
1199 timer_stats_account_hrtimer(timer);
1201 fn = timer->function;
1202 __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
1203 spin_unlock_irq(&cpu_base->lock);
1205 restart = fn(timer);
1207 spin_lock_irq(&cpu_base->lock);
1209 timer->state &= ~HRTIMER_STATE_CALLBACK;
1210 if (restart == HRTIMER_RESTART) {
1211 BUG_ON(hrtimer_active(timer));
1213 * Enqueue the timer, allow reprogramming of the event
1214 * device
1216 enqueue_hrtimer(timer, timer->base, 1);
1217 } else if (hrtimer_active(timer)) {
1219 * If the timer was rearmed on another CPU, reprogram
1220 * the event device.
1222 struct hrtimer_clock_base *base = timer->base;
1224 if (base->first == &timer->node &&
1225 hrtimer_reprogram(timer, base)) {
1227 * Timer is expired. Thus move it from tree to
1228 * pending list again.
1230 __remove_hrtimer(timer, base,
1231 HRTIMER_STATE_PENDING, 0);
1232 list_add_tail(&timer->cb_entry,
1233 &base->cpu_base->cb_pending);
1237 spin_unlock_irq(&cpu_base->lock);
1240 static void __run_hrtimer(struct hrtimer *timer)
1242 struct hrtimer_clock_base *base = timer->base;
1243 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1244 enum hrtimer_restart (*fn)(struct hrtimer *);
1245 int restart;
1247 debug_hrtimer_deactivate(timer);
1248 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1249 timer_stats_account_hrtimer(timer);
1251 fn = timer->function;
1252 if (timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ) {
1254 * Used for scheduler timers, avoid lock inversion with
1255 * rq->lock and tasklist_lock.
1257 * These timers are required to deal with enqueue expiry
1258 * themselves and are not allowed to migrate.
1260 spin_unlock(&cpu_base->lock);
1261 restart = fn(timer);
1262 spin_lock(&cpu_base->lock);
1263 } else
1264 restart = fn(timer);
1267 * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
1268 * reprogramming of the event hardware. This happens at the end of this
1269 * function anyway.
1271 if (restart != HRTIMER_NORESTART) {
1272 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1273 enqueue_hrtimer(timer, base, 0);
1275 timer->state &= ~HRTIMER_STATE_CALLBACK;
1278 #ifdef CONFIG_HIGH_RES_TIMERS
1281 * High resolution timer interrupt
1282 * Called with interrupts disabled
1284 void hrtimer_interrupt(struct clock_event_device *dev)
1286 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1287 struct hrtimer_clock_base *base;
1288 ktime_t expires_next, now;
1289 int i, raise = 0;
1291 BUG_ON(!cpu_base->hres_active);
1292 cpu_base->nr_events++;
1293 dev->next_event.tv64 = KTIME_MAX;
1295 retry:
1296 now = ktime_get();
1298 expires_next.tv64 = KTIME_MAX;
1300 base = cpu_base->clock_base;
1302 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1303 ktime_t basenow;
1304 struct rb_node *node;
1306 spin_lock(&cpu_base->lock);
1308 basenow = ktime_add(now, base->offset);
1310 while ((node = base->first)) {
1311 struct hrtimer *timer;
1313 timer = rb_entry(node, struct hrtimer, node);
1315 if (basenow.tv64 < timer->expires.tv64) {
1316 ktime_t expires;
1318 expires = ktime_sub(timer->expires,
1319 base->offset);
1320 if (expires.tv64 < expires_next.tv64)
1321 expires_next = expires;
1322 break;
1325 /* Move softirq callbacks to the pending list */
1326 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1327 __remove_hrtimer(timer, base,
1328 HRTIMER_STATE_PENDING, 0);
1329 list_add_tail(&timer->cb_entry,
1330 &base->cpu_base->cb_pending);
1331 raise = 1;
1332 continue;
1335 __run_hrtimer(timer);
1337 spin_unlock(&cpu_base->lock);
1338 base++;
1341 cpu_base->expires_next = expires_next;
1343 /* Reprogramming necessary ? */
1344 if (expires_next.tv64 != KTIME_MAX) {
1345 if (tick_program_event(expires_next, 0))
1346 goto retry;
1349 /* Raise softirq ? */
1350 if (raise)
1351 raise_softirq(HRTIMER_SOFTIRQ);
1354 static void run_hrtimer_softirq(struct softirq_action *h)
1356 run_hrtimer_pending(&__get_cpu_var(hrtimer_bases));
1359 #endif /* CONFIG_HIGH_RES_TIMERS */
1362 * Called from timer softirq every jiffy, expire hrtimers:
1364 * For HRT its the fall back code to run the softirq in the timer
1365 * softirq context in case the hrtimer initialization failed or has
1366 * not been done yet.
1368 void hrtimer_run_pending(void)
1370 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1372 if (hrtimer_hres_active())
1373 return;
1376 * This _is_ ugly: We have to check in the softirq context,
1377 * whether we can switch to highres and / or nohz mode. The
1378 * clocksource switch happens in the timer interrupt with
1379 * xtime_lock held. Notification from there only sets the
1380 * check bit in the tick_oneshot code, otherwise we might
1381 * deadlock vs. xtime_lock.
1383 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1384 hrtimer_switch_to_hres();
1386 run_hrtimer_pending(cpu_base);
1390 * Called from hardirq context every jiffy
1392 void hrtimer_run_queues(void)
1394 struct rb_node *node;
1395 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1396 struct hrtimer_clock_base *base;
1397 int index, gettime = 1;
1399 if (hrtimer_hres_active())
1400 return;
1402 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1403 base = &cpu_base->clock_base[index];
1405 if (!base->first)
1406 continue;
1408 if (base->get_softirq_time)
1409 base->softirq_time = base->get_softirq_time();
1410 else if (gettime) {
1411 hrtimer_get_softirq_time(cpu_base);
1412 gettime = 0;
1415 spin_lock(&cpu_base->lock);
1417 while ((node = base->first)) {
1418 struct hrtimer *timer;
1420 timer = rb_entry(node, struct hrtimer, node);
1421 if (base->softirq_time.tv64 <= timer->expires.tv64)
1422 break;
1424 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1425 __remove_hrtimer(timer, base,
1426 HRTIMER_STATE_PENDING, 0);
1427 list_add_tail(&timer->cb_entry,
1428 &base->cpu_base->cb_pending);
1429 continue;
1432 __run_hrtimer(timer);
1434 spin_unlock(&cpu_base->lock);
1439 * Sleep related functions:
1441 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
1443 struct hrtimer_sleeper *t =
1444 container_of(timer, struct hrtimer_sleeper, timer);
1445 struct task_struct *task = t->task;
1447 t->task = NULL;
1448 if (task)
1449 wake_up_process(task);
1451 return HRTIMER_NORESTART;
1454 void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1456 sl->timer.function = hrtimer_wakeup;
1457 sl->task = task;
1458 #ifdef CONFIG_HIGH_RES_TIMERS
1459 sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1460 #endif
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(&t->timer, t->timer.expires, 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 = ktime_sub(timer->expires, timer->base->get_time());
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 t.timer.expires.tv64 = 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;
1536 hrtimer_init_on_stack(&t.timer, clockid, mode);
1537 t.timer.expires = timespec_to_ktime(*rqtp);
1538 if (do_nanosleep(&t, mode))
1539 goto out;
1541 /* Absolute timers do not update the rmtp value and restart: */
1542 if (mode == HRTIMER_MODE_ABS) {
1543 ret = -ERESTARTNOHAND;
1544 goto out;
1547 if (rmtp) {
1548 ret = update_rmtp(&t.timer, rmtp);
1549 if (ret <= 0)
1550 goto out;
1553 restart = &current_thread_info()->restart_block;
1554 restart->fn = hrtimer_nanosleep_restart;
1555 restart->nanosleep.index = t.timer.base->index;
1556 restart->nanosleep.rmtp = rmtp;
1557 restart->nanosleep.expires = t.timer.expires.tv64;
1559 ret = -ERESTART_RESTARTBLOCK;
1560 out:
1561 destroy_hrtimer_on_stack(&t.timer);
1562 return ret;
1565 asmlinkage long
1566 sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1568 struct timespec tu;
1570 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1571 return -EFAULT;
1573 if (!timespec_valid(&tu))
1574 return -EINVAL;
1576 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1580 * Functions related to boot-time initialization:
1582 static void __cpuinit init_hrtimers_cpu(int cpu)
1584 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
1585 int i;
1587 spin_lock_init(&cpu_base->lock);
1589 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1590 cpu_base->clock_base[i].cpu_base = cpu_base;
1592 INIT_LIST_HEAD(&cpu_base->cb_pending);
1593 hrtimer_init_hres(cpu_base);
1596 #ifdef CONFIG_HOTPLUG_CPU
1598 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1599 struct hrtimer_clock_base *new_base)
1601 struct hrtimer *timer;
1602 struct rb_node *node;
1604 while ((node = rb_first(&old_base->active))) {
1605 timer = rb_entry(node, struct hrtimer, node);
1606 BUG_ON(hrtimer_callback_running(timer));
1607 debug_hrtimer_deactivate(timer);
1608 __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE, 0);
1609 timer->base = new_base;
1611 * Enqueue the timer. Allow reprogramming of the event device
1613 enqueue_hrtimer(timer, new_base, 1);
1617 static void migrate_hrtimers(int cpu)
1619 struct hrtimer_cpu_base *old_base, *new_base;
1620 int i;
1622 BUG_ON(cpu_online(cpu));
1623 old_base = &per_cpu(hrtimer_bases, cpu);
1624 new_base = &get_cpu_var(hrtimer_bases);
1626 tick_cancel_sched_timer(cpu);
1628 local_irq_disable();
1629 spin_lock(&new_base->lock);
1630 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1632 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1633 migrate_hrtimer_list(&old_base->clock_base[i],
1634 &new_base->clock_base[i]);
1637 spin_unlock(&old_base->lock);
1638 spin_unlock(&new_base->lock);
1639 local_irq_enable();
1640 put_cpu_var(hrtimer_bases);
1642 #endif /* CONFIG_HOTPLUG_CPU */
1644 static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
1645 unsigned long action, void *hcpu)
1647 unsigned int cpu = (long)hcpu;
1649 switch (action) {
1651 case CPU_UP_PREPARE:
1652 case CPU_UP_PREPARE_FROZEN:
1653 init_hrtimers_cpu(cpu);
1654 break;
1656 #ifdef CONFIG_HOTPLUG_CPU
1657 case CPU_DEAD:
1658 case CPU_DEAD_FROZEN:
1659 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
1660 migrate_hrtimers(cpu);
1661 break;
1662 #endif
1664 default:
1665 break;
1668 return NOTIFY_OK;
1671 static struct notifier_block __cpuinitdata hrtimers_nb = {
1672 .notifier_call = hrtimer_cpu_notify,
1675 void __init hrtimers_init(void)
1677 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1678 (void *)(long)smp_processor_id());
1679 register_cpu_notifier(&hrtimers_nb);
1680 #ifdef CONFIG_HIGH_RES_TIMERS
1681 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq, NULL);
1682 #endif