Merge tag 'v3.10.55' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / hrtimer.c
CommitLineData
c0a31329
TG
1/*
2 * linux/kernel/hrtimer.c
3 *
3c8aa39d 4 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
79bf2bb3 5 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
54cdfdb4 6 * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
c0a31329
TG
7 *
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 *
66188fae
TG
25 * Help, testing, suggestions, bugfixes, improvements were
26 * provided by:
27 *
28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
29 * et. al.
30 *
c0a31329
TG
31 * For licencing details see kernel-base/COPYING
32 */
33
34#include <linux/cpu.h>
9984de1a 35#include <linux/export.h>
c0a31329
TG
36#include <linux/percpu.h>
37#include <linux/hrtimer.h>
38#include <linux/notifier.h>
39#include <linux/syscalls.h>
54cdfdb4 40#include <linux/kallsyms.h>
c0a31329 41#include <linux/interrupt.h>
79bf2bb3 42#include <linux/tick.h>
54cdfdb4
TG
43#include <linux/seq_file.h>
44#include <linux/err.h>
237fc6e7 45#include <linux/debugobjects.h>
eea08f32 46#include <linux/sched.h>
cf4aebc2 47#include <linux/sched/sysctl.h>
8bd75c77 48#include <linux/sched/rt.h>
eea08f32 49#include <linux/timer.h>
6fa3eb70 50#include <linux/freezer.h>
c0a31329
TG
51
52#include <asm/uaccess.h>
53
c6a2a177
XG
54#include <trace/events/timer.h>
55
6fa3eb70
S
56#include <linux/mt_sched_mon.h>
57
58//#define MTK_HRTIME_DEBUG /*MTK debug func*/
c0a31329
TG
59/*
60 * The timer bases:
7978672c 61 *
e06383db
JS
62 * There are more clockids then hrtimer bases. Thus, we index
63 * into the timer bases by the hrtimer_base_type enum. When trying
64 * to reach a base using a clockid, hrtimer_clockid_to_base()
65 * is used to convert from clockid to the proper hrtimer_base_type.
c0a31329 66 */
54cdfdb4 67DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
c0a31329 68{
3c8aa39d 69
84cc8fd2 70 .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
3c8aa39d 71 .clock_base =
c0a31329 72 {
3c8aa39d 73 {
ab8177bc
TG
74 .index = HRTIMER_BASE_MONOTONIC,
75 .clockid = CLOCK_MONOTONIC,
3c8aa39d 76 .get_time = &ktime_get,
54cdfdb4 77 .resolution = KTIME_LOW_RES,
3c8aa39d 78 },
68fa61c0
TG
79 {
80 .index = HRTIMER_BASE_REALTIME,
81 .clockid = CLOCK_REALTIME,
82 .get_time = &ktime_get_real,
83 .resolution = KTIME_LOW_RES,
84 },
70a08cca 85 {
ab8177bc
TG
86 .index = HRTIMER_BASE_BOOTTIME,
87 .clockid = CLOCK_BOOTTIME,
70a08cca
JS
88 .get_time = &ktime_get_boottime,
89 .resolution = KTIME_LOW_RES,
90 },
90adda98
JS
91 {
92 .index = HRTIMER_BASE_TAI,
93 .clockid = CLOCK_TAI,
94 .get_time = &ktime_get_clocktai,
95 .resolution = KTIME_LOW_RES,
96 },
3c8aa39d 97 }
c0a31329
TG
98};
99
942c3c5c 100static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
ce31332d
TG
101 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
102 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
103 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
90adda98 104 [CLOCK_TAI] = HRTIMER_BASE_TAI,
ce31332d 105};
e06383db
JS
106
107static inline int hrtimer_clockid_to_base(clockid_t clock_id)
108{
109 return hrtimer_clock_to_base_table[clock_id];
110}
111
112
92127c7a
TG
113/*
114 * Get the coarse grained time at the softirq based on xtime and
115 * wall_to_monotonic.
116 */
3c8aa39d 117static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
92127c7a 118{
70a08cca 119 ktime_t xtim, mono, boot;
314ac371 120 struct timespec xts, tom, slp;
90adda98 121 s32 tai_offset;
92127c7a 122
314ac371 123 get_xtime_and_monotonic_and_sleep_offset(&xts, &tom, &slp);
90adda98 124 tai_offset = timekeeping_get_tai_offset();
92127c7a 125
f4304ab2 126 xtim = timespec_to_ktime(xts);
70a08cca
JS
127 mono = ktime_add(xtim, timespec_to_ktime(tom));
128 boot = ktime_add(mono, timespec_to_ktime(slp));
e06383db 129 base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
70a08cca
JS
130 base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
131 base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
90adda98
JS
132 base->clock_base[HRTIMER_BASE_TAI].softirq_time =
133 ktime_add(xtim, ktime_set(tai_offset, 0));
92127c7a
TG
134}
135
c0a31329
TG
136/*
137 * Functions and macros which are different for UP/SMP systems are kept in a
138 * single place
139 */
140#ifdef CONFIG_SMP
141
c0a31329
TG
142/*
143 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
144 * means that all timers which are tied to this base via timer->base are
145 * locked, and the base itself is locked too.
146 *
147 * So __run_timers/migrate_timers can safely modify all timers which could
148 * be found on the lists/queues.
149 *
150 * When the timer's base is locked, and the timer removed from list, it is
151 * possible to set timer->base = NULL and drop the lock: the timer remains
152 * locked.
153 */
3c8aa39d
TG
154static
155struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
156 unsigned long *flags)
c0a31329 157{
3c8aa39d 158 struct hrtimer_clock_base *base;
c0a31329
TG
159
160 for (;;) {
161 base = timer->base;
162 if (likely(base != NULL)) {
ecb49d1a 163 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
c0a31329
TG
164 if (likely(base == timer->base))
165 return base;
166 /* The timer has migrated to another CPU: */
ecb49d1a 167 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
c0a31329
TG
168 }
169 cpu_relax();
170 }
171}
172
6ff7041d
TG
173
174/*
175 * Get the preferred target CPU for NOHZ
176 */
177static int hrtimer_get_target(int this_cpu, int pinned)
178{
3451d024 179#ifdef CONFIG_NO_HZ_COMMON
83cd4fe2
VP
180 if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu))
181 return get_nohz_timer_target();
6ff7041d
TG
182#endif
183 return this_cpu;
184}
185
186/*
187 * With HIGHRES=y we do not migrate the timer when it is expiring
188 * before the next event on the target cpu because we cannot reprogram
189 * the target cpu hardware and we would cause it to fire late.
190 *
191 * Called with cpu_base->lock of target cpu held.
192 */
193static int
194hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base)
195{
196#ifdef CONFIG_HIGH_RES_TIMERS
197 ktime_t expires;
198
199 if (!new_base->cpu_base->hres_active)
200 return 0;
201
202 expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset);
203 return expires.tv64 <= new_base->cpu_base->expires_next.tv64;
204#else
205 return 0;
206#endif
207}
208
c0a31329
TG
209/*
210 * Switch the timer base to the current CPU when possible.
211 */
3c8aa39d 212static inline struct hrtimer_clock_base *
597d0275
AB
213switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
214 int pinned)
c0a31329 215{
3c8aa39d
TG
216 struct hrtimer_clock_base *new_base;
217 struct hrtimer_cpu_base *new_cpu_base;
6ff7041d
TG
218 int this_cpu = smp_processor_id();
219 int cpu = hrtimer_get_target(this_cpu, pinned);
ab8177bc 220 int basenum = base->index;
c0a31329 221
eea08f32
AB
222again:
223 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
e06383db 224 new_base = &new_cpu_base->clock_base[basenum];
c0a31329
TG
225
226 if (base != new_base) {
227 /*
6ff7041d 228 * We are trying to move timer to new_base.
c0a31329
TG
229 * However we can't change timer's base while it is running,
230 * so we keep it on the same CPU. No hassle vs. reprogramming
231 * the event source in the high resolution case. The softirq
232 * code will take care of this when the timer function has
233 * completed. There is no conflict as we hold the lock until
234 * the timer is enqueued.
235 */
54cdfdb4 236 if (unlikely(hrtimer_callback_running(timer)))
c0a31329
TG
237 return base;
238
239 /* See the comment in lock_timer_base() */
240 timer->base = NULL;
ecb49d1a
TG
241 raw_spin_unlock(&base->cpu_base->lock);
242 raw_spin_lock(&new_base->cpu_base->lock);
eea08f32 243
6ff7041d
TG
244 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
245 cpu = this_cpu;
ecb49d1a
TG
246 raw_spin_unlock(&new_base->cpu_base->lock);
247 raw_spin_lock(&base->cpu_base->lock);
6ff7041d
TG
248 timer->base = base;
249 goto again;
eea08f32 250 }
c0a31329 251 timer->base = new_base;
7f7bb020
LM
252 } else {
253 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
254 cpu = this_cpu;
255 goto again;
256 }
c0a31329
TG
257 }
258 return new_base;
259}
260
261#else /* CONFIG_SMP */
262
3c8aa39d 263static inline struct hrtimer_clock_base *
c0a31329
TG
264lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
265{
3c8aa39d 266 struct hrtimer_clock_base *base = timer->base;
c0a31329 267
ecb49d1a 268 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
c0a31329
TG
269
270 return base;
271}
272
eea08f32 273# define switch_hrtimer_base(t, b, p) (b)
c0a31329
TG
274
275#endif /* !CONFIG_SMP */
276
277/*
278 * Functions for the union type storage format of ktime_t which are
279 * too large for inlining:
280 */
281#if BITS_PER_LONG < 64
282# ifndef CONFIG_KTIME_SCALAR
283/**
284 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
c0a31329
TG
285 * @kt: addend
286 * @nsec: the scalar nsec value to add
287 *
288 * Returns the sum of kt and nsec in ktime_t format
289 */
290ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
291{
292 ktime_t tmp;
293
294 if (likely(nsec < NSEC_PER_SEC)) {
295 tmp.tv64 = nsec;
296 } else {
297 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
298
51fd36f3
DE
299 /* Make sure nsec fits into long */
300 if (unlikely(nsec > KTIME_SEC_MAX))
301 return (ktime_t){ .tv64 = KTIME_MAX };
302
c0a31329
TG
303 tmp = ktime_set((long)nsec, rem);
304 }
305
306 return ktime_add(kt, tmp);
307}
b8b8fd2d
DH
308
309EXPORT_SYMBOL_GPL(ktime_add_ns);
a272378d
ACM
310
311/**
312 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
313 * @kt: minuend
314 * @nsec: the scalar nsec value to subtract
315 *
316 * Returns the subtraction of @nsec from @kt in ktime_t format
317 */
318ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
319{
320 ktime_t tmp;
321
322 if (likely(nsec < NSEC_PER_SEC)) {
323 tmp.tv64 = nsec;
324 } else {
325 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
326
327 tmp = ktime_set((long)nsec, rem);
328 }
329
330 return ktime_sub(kt, tmp);
331}
332
333EXPORT_SYMBOL_GPL(ktime_sub_ns);
c0a31329
TG
334# endif /* !CONFIG_KTIME_SCALAR */
335
336/*
337 * Divide a ktime value by a nanosecond value
338 */
4d672e7a 339u64 ktime_divns(const ktime_t kt, s64 div)
c0a31329 340{
900cfa46 341 u64 dclc;
c0a31329
TG
342 int sft = 0;
343
900cfa46 344 dclc = ktime_to_ns(kt);
c0a31329
TG
345 /* Make sure the divisor is less than 2^32: */
346 while (div >> 32) {
347 sft++;
348 div >>= 1;
349 }
350 dclc >>= sft;
351 do_div(dclc, (unsigned long) div);
352
4d672e7a 353 return dclc;
c0a31329 354}
c0a31329
TG
355#endif /* BITS_PER_LONG >= 64 */
356
5a7780e7
TG
357/*
358 * Add two ktime values and do a safety check for overflow:
359 */
360ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
361{
362 ktime_t res = ktime_add(lhs, rhs);
363
364 /*
365 * We use KTIME_SEC_MAX here, the maximum timeout which we can
366 * return to user space in a timespec:
367 */
368 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
369 res = ktime_set(KTIME_SEC_MAX, 0);
370
371 return res;
372}
373
8daa21e6
AB
374EXPORT_SYMBOL_GPL(ktime_add_safe);
375
237fc6e7
TG
376#ifdef CONFIG_DEBUG_OBJECTS_TIMERS
377
378static struct debug_obj_descr hrtimer_debug_descr;
379
99777288
SG
380static void *hrtimer_debug_hint(void *addr)
381{
382 return ((struct hrtimer *) addr)->function;
383}
384
237fc6e7
TG
385/*
386 * fixup_init is called when:
387 * - an active object is initialized
388 */
389static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
390{
391 struct hrtimer *timer = addr;
392
393 switch (state) {
394 case ODEBUG_STATE_ACTIVE:
395 hrtimer_cancel(timer);
396 debug_object_init(timer, &hrtimer_debug_descr);
397 return 1;
398 default:
399 return 0;
400 }
401}
402
403/*
404 * fixup_activate is called when:
405 * - an active object is activated
406 * - an unknown object is activated (might be a statically initialized object)
407 */
408static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
409{
410 switch (state) {
411
412 case ODEBUG_STATE_NOTAVAILABLE:
413 WARN_ON_ONCE(1);
414 return 0;
415
416 case ODEBUG_STATE_ACTIVE:
417 WARN_ON(1);
418
419 default:
420 return 0;
421 }
422}
423
424/*
425 * fixup_free is called when:
426 * - an active object is freed
427 */
428static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
429{
430 struct hrtimer *timer = addr;
431
432 switch (state) {
433 case ODEBUG_STATE_ACTIVE:
434 hrtimer_cancel(timer);
435 debug_object_free(timer, &hrtimer_debug_descr);
436 return 1;
437 default:
438 return 0;
439 }
440}
441
442static struct debug_obj_descr hrtimer_debug_descr = {
443 .name = "hrtimer",
99777288 444 .debug_hint = hrtimer_debug_hint,
237fc6e7
TG
445 .fixup_init = hrtimer_fixup_init,
446 .fixup_activate = hrtimer_fixup_activate,
447 .fixup_free = hrtimer_fixup_free,
448};
449
450static inline void debug_hrtimer_init(struct hrtimer *timer)
451{
452 debug_object_init(timer, &hrtimer_debug_descr);
453}
454
455static inline void debug_hrtimer_activate(struct hrtimer *timer)
456{
457 debug_object_activate(timer, &hrtimer_debug_descr);
458}
459
460static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
461{
462 debug_object_deactivate(timer, &hrtimer_debug_descr);
463}
464
465static inline void debug_hrtimer_free(struct hrtimer *timer)
466{
467 debug_object_free(timer, &hrtimer_debug_descr);
468}
469
470static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
471 enum hrtimer_mode mode);
472
473void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
474 enum hrtimer_mode mode)
475{
476 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
477 __hrtimer_init(timer, clock_id, mode);
478}
2bc481cf 479EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
237fc6e7
TG
480
481void destroy_hrtimer_on_stack(struct hrtimer *timer)
482{
483 debug_object_free(timer, &hrtimer_debug_descr);
484}
485
486#else
487static inline void debug_hrtimer_init(struct hrtimer *timer) { }
488static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
489static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
490#endif
491
c6a2a177
XG
492static inline void
493debug_init(struct hrtimer *timer, clockid_t clockid,
494 enum hrtimer_mode mode)
495{
496 debug_hrtimer_init(timer);
497 trace_hrtimer_init(timer, clockid, mode);
498}
499
500static inline void debug_activate(struct hrtimer *timer)
501{
502 debug_hrtimer_activate(timer);
503 trace_hrtimer_start(timer);
504}
505
506static inline void debug_deactivate(struct hrtimer *timer)
507{
508 debug_hrtimer_deactivate(timer);
509 trace_hrtimer_cancel(timer);
510}
511
54cdfdb4
TG
512/* High resolution timer related functions */
513#ifdef CONFIG_HIGH_RES_TIMERS
514
515/*
516 * High resolution timer enabled ?
517 */
518static int hrtimer_hres_enabled __read_mostly = 1;
519
520/*
521 * Enable / Disable high resolution mode
522 */
523static int __init setup_hrtimer_hres(char *str)
524{
525 if (!strcmp(str, "off"))
526 hrtimer_hres_enabled = 0;
527 else if (!strcmp(str, "on"))
528 hrtimer_hres_enabled = 1;
529 else
530 return 0;
531 return 1;
532}
533
534__setup("highres=", setup_hrtimer_hres);
535
536/*
537 * hrtimer_high_res_enabled - query, if the highres mode is enabled
538 */
539static inline int hrtimer_is_hres_enabled(void)
540{
541 return hrtimer_hres_enabled;
542}
543
544/*
545 * Is the high resolution mode active ?
546 */
547static inline int hrtimer_hres_active(void)
548{
909ea964 549 return __this_cpu_read(hrtimer_bases.hres_active);
54cdfdb4
TG
550}
551
552/*
553 * Reprogram the event source with checking both queues for the
554 * next event
555 * Called with interrupts disabled and base->lock held
556 */
7403f41f
AC
557static void
558hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
54cdfdb4
TG
559{
560 int i;
561 struct hrtimer_clock_base *base = cpu_base->clock_base;
7403f41f 562 ktime_t expires, expires_next;
54cdfdb4 563
7403f41f 564 expires_next.tv64 = KTIME_MAX;
54cdfdb4
TG
565
566 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
567 struct hrtimer *timer;
998adc3d 568 struct timerqueue_node *next;
54cdfdb4 569
998adc3d
JS
570 next = timerqueue_getnext(&base->active);
571 if (!next)
54cdfdb4 572 continue;
998adc3d
JS
573 timer = container_of(next, struct hrtimer, node);
574
cc584b21 575 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
b0a9b511
TG
576 /*
577 * clock_was_set() has changed base->offset so the
578 * result might be negative. Fix it up to prevent a
579 * false positive in clockevents_program_event()
580 */
581 if (expires.tv64 < 0)
582 expires.tv64 = 0;
7403f41f
AC
583 if (expires.tv64 < expires_next.tv64)
584 expires_next = expires;
54cdfdb4
TG
585 }
586
7403f41f
AC
587 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
588 return;
589
590 cpu_base->expires_next.tv64 = expires_next.tv64;
591
be6e0ece
SH
592 /*
593 * If a hang was detected in the last timer interrupt then we
594 * leave the hang delay active in the hardware. We want the
595 * system to make progress. That also prevents the following
596 * scenario:
597 * T1 expires 50ms from now
598 * T2 expires 5s from now
599 *
600 * T1 is removed, so this code is called and would reprogram
601 * the hardware to 5s from now. Any hrtimer_start after that
602 * will not reprogram the hardware due to hang_detected being
603 * set. So we'd effectivly block all timers until the T2 event
604 * fires.
605 */
606 if (cpu_base->hang_detected)
607 return;
608
54cdfdb4
TG
609 if (cpu_base->expires_next.tv64 != KTIME_MAX)
610 tick_program_event(cpu_base->expires_next, 1);
611}
612
613/*
614 * Shared reprogramming for clock_realtime and clock_monotonic
615 *
616 * When a timer is enqueued and expires earlier than the already enqueued
617 * timers, we have to check, whether it expires earlier than the timer for
618 * which the clock event device was armed.
619 *
620 * Called with interrupts disabled and base->cpu_base.lock held
621 */
622static int hrtimer_reprogram(struct hrtimer *timer,
623 struct hrtimer_clock_base *base)
624{
41d2e494 625 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
cc584b21 626 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
54cdfdb4
TG
627 int res;
628
cc584b21 629 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
63070a79 630
54cdfdb4
TG
631 /*
632 * When the callback is running, we do not reprogram the clock event
633 * device. The timer callback is either running on a different CPU or
3a4fa0a2 634 * the callback is executed in the hrtimer_interrupt context. The
54cdfdb4
TG
635 * reprogramming is handled either by the softirq, which called the
636 * callback or at the end of the hrtimer_interrupt.
637 */
638 if (hrtimer_callback_running(timer))
639 return 0;
640
63070a79
TG
641 /*
642 * CLOCK_REALTIME timer might be requested with an absolute
643 * expiry time which is less than base->offset. Nothing wrong
644 * about that, just avoid to call into the tick code, which
645 * has now objections against negative expiry values.
646 */
647 if (expires.tv64 < 0)
648 return -ETIME;
649
41d2e494
TG
650 if (expires.tv64 >= cpu_base->expires_next.tv64)
651 return 0;
652
653 /*
654 * If a hang was detected in the last timer interrupt then we
655 * do not schedule a timer which is earlier than the expiry
656 * which we enforced in the hang detection. We want the system
657 * to make progress.
658 */
659 if (cpu_base->hang_detected)
54cdfdb4
TG
660 return 0;
661
662 /*
663 * Clockevents returns -ETIME, when the event was in the past.
664 */
665 res = tick_program_event(expires, 0);
666 if (!IS_ERR_VALUE(res))
41d2e494 667 cpu_base->expires_next = expires;
54cdfdb4
TG
668 return res;
669}
670
54cdfdb4
TG
671/*
672 * Initialize the high resolution related parts of cpu_base
673 */
674static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
675{
676 base->expires_next.tv64 = KTIME_MAX;
677 base->hres_active = 0;
54cdfdb4
TG
678}
679
54cdfdb4
TG
680/*
681 * When High resolution timers are active, try to reprogram. Note, that in case
682 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
683 * check happens. The timer gets enqueued into the rbtree. The reprogramming
684 * and expiry check is done in the hrtimer_interrupt or in the softirq.
685 */
686static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
b22affe0 687 struct hrtimer_clock_base *base)
54cdfdb4 688{
b22affe0 689 return base->cpu_base->hres_active && hrtimer_reprogram(timer, base);
54cdfdb4
TG
690}
691
5baefd6d
JS
692static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
693{
694 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
695 ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
90adda98 696 ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
5baefd6d 697
90adda98 698 return ktime_get_update_offsets(offs_real, offs_boot, offs_tai);
5baefd6d
JS
699}
700
9ec26907
TG
701/*
702 * Retrigger next event is called after clock was set
703 *
704 * Called with interrupts disabled via on_each_cpu()
705 */
706static void retrigger_next_event(void *arg)
707{
708 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
9ec26907
TG
709
710 if (!hrtimer_hres_active())
711 return;
712
9ec26907 713 raw_spin_lock(&base->lock);
5baefd6d 714 hrtimer_update_base(base);
9ec26907
TG
715 hrtimer_force_reprogram(base, 0);
716 raw_spin_unlock(&base->lock);
717}
b12a03ce 718
54cdfdb4
TG
719/*
720 * Switch to high resolution mode
721 */
f8953856 722static int hrtimer_switch_to_hres(void)
54cdfdb4 723{
b12a03ce 724 int i, cpu = smp_processor_id();
820de5c3 725 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
54cdfdb4
TG
726 unsigned long flags;
727
728 if (base->hres_active)
f8953856 729 return 1;
54cdfdb4
TG
730
731 local_irq_save(flags);
732
733 if (tick_init_highres()) {
734 local_irq_restore(flags);
820de5c3
IM
735 printk(KERN_WARNING "Could not switch to high resolution "
736 "mode on CPU %d\n", cpu);
f8953856 737 return 0;
54cdfdb4
TG
738 }
739 base->hres_active = 1;
b12a03ce
TG
740 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
741 base->clock_base[i].resolution = KTIME_HIGH_RES;
54cdfdb4
TG
742
743 tick_setup_sched_timer();
54cdfdb4
TG
744 /* "Retrigger" the interrupt to get things going */
745 retrigger_next_event(NULL);
746 local_irq_restore(flags);
f8953856 747 return 1;
54cdfdb4
TG
748}
749
2d06fa0f
TG
750static void clock_was_set_work(struct work_struct *work)
751{
752 clock_was_set();
753}
754
755static DECLARE_WORK(hrtimer_work, clock_was_set_work);
756
f55a6faa 757/*
2d06fa0f
TG
758 * Called from timekeeping and resume code to reprogramm the hrtimer
759 * interrupt device on all cpus.
f55a6faa
JS
760 */
761void clock_was_set_delayed(void)
762{
2d06fa0f 763 schedule_work(&hrtimer_work);
f55a6faa
JS
764}
765
54cdfdb4
TG
766#else
767
768static inline int hrtimer_hres_active(void) { return 0; }
769static inline int hrtimer_is_hres_enabled(void) { return 0; }
f8953856 770static inline int hrtimer_switch_to_hres(void) { return 0; }
7403f41f
AC
771static inline void
772hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
54cdfdb4 773static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
b22affe0 774 struct hrtimer_clock_base *base)
54cdfdb4
TG
775{
776 return 0;
777}
54cdfdb4 778static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
9ec26907 779static inline void retrigger_next_event(void *arg) { }
54cdfdb4
TG
780
781#endif /* CONFIG_HIGH_RES_TIMERS */
782
b12a03ce
TG
783/*
784 * Clock realtime was set
785 *
786 * Change the offset of the realtime clock vs. the monotonic
787 * clock.
788 *
789 * We might have to reprogram the high resolution timer interrupt. On
790 * SMP we call the architecture specific code to retrigger _all_ high
791 * resolution timer interrupts. On UP we just disable interrupts and
792 * call the high resolution interrupt code.
793 */
794void clock_was_set(void)
795{
90ff1f30 796#ifdef CONFIG_HIGH_RES_TIMERS
b12a03ce
TG
797 /* Retrigger the CPU local events everywhere */
798 on_each_cpu(retrigger_next_event, NULL, 1);
9ec26907
TG
799#endif
800 timerfd_clock_was_set();
b12a03ce
TG
801}
802
803/*
804 * During resume we might have to reprogram the high resolution timer
805 * interrupt (on the local CPU):
806 */
807void hrtimers_resume(void)
808{
809 WARN_ONCE(!irqs_disabled(),
810 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
811
2d06fa0f 812 /* Retrigger on the local CPU */
b12a03ce 813 retrigger_next_event(NULL);
2d06fa0f
TG
814 /* And schedule a retrigger for all others */
815 clock_was_set_delayed();
b12a03ce
TG
816}
817
5f201907 818static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
82f67cd9 819{
5f201907 820#ifdef CONFIG_TIMER_STATS
82f67cd9
IM
821 if (timer->start_site)
822 return;
5f201907 823 timer->start_site = __builtin_return_address(0);
82f67cd9
IM
824 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
825 timer->start_pid = current->pid;
5f201907
HC
826#endif
827}
828
829static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
830{
831#ifdef CONFIG_TIMER_STATS
832 timer->start_site = NULL;
833#endif
82f67cd9 834}
5f201907
HC
835
836static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
837{
838#ifdef CONFIG_TIMER_STATS
839 if (likely(!timer_stats_active))
840 return;
841 timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
842 timer->function, timer->start_comm, 0);
82f67cd9 843#endif
5f201907 844}
82f67cd9 845
c0a31329 846/*
6506f2aa 847 * Counterpart to lock_hrtimer_base above:
c0a31329
TG
848 */
849static inline
850void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
851{
ecb49d1a 852 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
c0a31329
TG
853}
854
855/**
856 * hrtimer_forward - forward the timer expiry
c0a31329 857 * @timer: hrtimer to forward
44f21475 858 * @now: forward past this time
c0a31329
TG
859 * @interval: the interval to forward
860 *
861 * Forward the timer expiry so it will expire in the future.
8dca6f33 862 * Returns the number of overruns.
c0a31329 863 */
4d672e7a 864u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
c0a31329 865{
4d672e7a 866 u64 orun = 1;
44f21475 867 ktime_t delta;
c0a31329 868
cc584b21 869 delta = ktime_sub(now, hrtimer_get_expires(timer));
c0a31329
TG
870
871 if (delta.tv64 < 0)
872 return 0;
873
c9db4fa1
TG
874 if (interval.tv64 < timer->base->resolution.tv64)
875 interval.tv64 = timer->base->resolution.tv64;
876
c0a31329 877 if (unlikely(delta.tv64 >= interval.tv64)) {
df869b63 878 s64 incr = ktime_to_ns(interval);
c0a31329
TG
879
880 orun = ktime_divns(delta, incr);
cc584b21
AV
881 hrtimer_add_expires_ns(timer, incr * orun);
882 if (hrtimer_get_expires_tv64(timer) > now.tv64)
c0a31329
TG
883 return orun;
884 /*
885 * This (and the ktime_add() below) is the
886 * correction for exact:
887 */
888 orun++;
889 }
cc584b21 890 hrtimer_add_expires(timer, interval);
c0a31329
TG
891
892 return orun;
893}
6bdb6b62 894EXPORT_SYMBOL_GPL(hrtimer_forward);
c0a31329
TG
895
896/*
897 * enqueue_hrtimer - internal function to (re)start a timer
898 *
899 * The timer is inserted in expiry order. Insertion into the
900 * red black tree is O(log(n)). Must hold the base lock.
a6037b61
PZ
901 *
902 * Returns 1 when the new timer is the leftmost timer in the tree.
c0a31329 903 */
a6037b61
PZ
904static int enqueue_hrtimer(struct hrtimer *timer,
905 struct hrtimer_clock_base *base)
c0a31329 906{
c6a2a177 907 debug_activate(timer);
237fc6e7 908
998adc3d 909 timerqueue_add(&base->active, &timer->node);
ab8177bc 910 base->cpu_base->active_bases |= 1 << base->index;
54cdfdb4 911
303e967f
TG
912 /*
913 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
914 * state of a possibly running callback.
915 */
916 timer->state |= HRTIMER_STATE_ENQUEUED;
a6037b61 917
998adc3d 918 return (&timer->node == base->active.next);
288867ec 919}
c0a31329
TG
920
921/*
922 * __remove_hrtimer - internal function to remove a timer
923 *
924 * Caller must hold the base lock.
54cdfdb4
TG
925 *
926 * High resolution timer mode reprograms the clock event device when the
927 * timer is the one which expires next. The caller can disable this by setting
928 * reprogram to zero. This is useful, when the context does a reprogramming
929 * anyway (e.g. timer interrupt)
c0a31329 930 */
3c8aa39d 931static void __remove_hrtimer(struct hrtimer *timer,
303e967f 932 struct hrtimer_clock_base *base,
54cdfdb4 933 unsigned long newstate, int reprogram)
c0a31329 934{
27c9cd7e 935 struct timerqueue_node *next_timer;
7403f41f
AC
936 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
937 goto out;
938
27c9cd7e
JO
939 next_timer = timerqueue_getnext(&base->active);
940 timerqueue_del(&base->active, &timer->node);
941 if (&timer->node == next_timer) {
7403f41f
AC
942#ifdef CONFIG_HIGH_RES_TIMERS
943 /* Reprogram the clock event device. if enabled */
944 if (reprogram && hrtimer_hres_active()) {
945 ktime_t expires;
946
947 expires = ktime_sub(hrtimer_get_expires(timer),
948 base->offset);
949 if (base->cpu_base->expires_next.tv64 == expires.tv64)
950 hrtimer_force_reprogram(base->cpu_base, 1);
54cdfdb4 951 }
7403f41f 952#endif
54cdfdb4 953 }
ab8177bc
TG
954 if (!timerqueue_getnext(&base->active))
955 base->cpu_base->active_bases &= ~(1 << base->index);
7403f41f 956out:
303e967f 957 timer->state = newstate;
c0a31329
TG
958}
959
960/*
961 * remove hrtimer, called with base lock held
962 */
963static inline int
3c8aa39d 964remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
c0a31329 965{
303e967f 966 if (hrtimer_is_queued(timer)) {
f13d4f97 967 unsigned long state;
54cdfdb4
TG
968 int reprogram;
969
970 /*
971 * Remove the timer and force reprogramming when high
972 * resolution mode is active and the timer is on the current
973 * CPU. If we remove a timer on another CPU, reprogramming is
974 * skipped. The interrupt event on this CPU is fired and
975 * reprogramming happens in the interrupt handler. This is a
976 * rare case and less expensive than a smp call.
977 */
c6a2a177 978 debug_deactivate(timer);
82f67cd9 979 timer_stats_hrtimer_clear_start_info(timer);
54cdfdb4 980 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
f13d4f97
SQ
981 /*
982 * We must preserve the CALLBACK state flag here,
983 * otherwise we could move the timer base in
984 * switch_hrtimer_base.
985 */
986 state = timer->state & HRTIMER_STATE_CALLBACK;
987 __remove_hrtimer(timer, base, state, reprogram);
c0a31329
TG
988 return 1;
989 }
990 return 0;
991}
992
7f1e2ca9
PZ
993int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
994 unsigned long delta_ns, const enum hrtimer_mode mode,
995 int wakeup)
c0a31329 996{
3c8aa39d 997 struct hrtimer_clock_base *base, *new_base;
c0a31329 998 unsigned long flags;
a6037b61 999 int ret, leftmost;
6fa3eb70
S
1000 /*add MTK debug log for ALPS01804694*/
1001 if(timer->function == NULL) {
1002 pr_alert("add hrtimer but do nothing");
1003 dump_stack();
1004 }
1005
c0a31329
TG
1006 base = lock_hrtimer_base(timer, &flags);
1007
1008 /* Remove an active timer from the queue: */
1009 ret = remove_hrtimer(timer, base);
1010
597d0275 1011 if (mode & HRTIMER_MODE_REL) {
1c0301d1 1012 tim = ktime_add_safe(tim, base->get_time());
06027bdd
IM
1013 /*
1014 * CONFIG_TIME_LOW_RES is a temporary way for architectures
1015 * to signal that they simply return xtime in
1016 * do_gettimeoffset(). In this case we want to round up by
1017 * resolution when starting a relative timer, to avoid short
1018 * timeouts. This will go away with the GTOD framework.
1019 */
1020#ifdef CONFIG_TIME_LOW_RES
5a7780e7 1021 tim = ktime_add_safe(tim, base->resolution);
06027bdd
IM
1022#endif
1023 }
237fc6e7 1024
da8f2e17 1025 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
c0a31329 1026
1c0301d1
VK
1027 /* Switch the timer base, if necessary: */
1028 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
1029
82f67cd9
IM
1030 timer_stats_hrtimer_set_start_info(timer);
1031
a6037b61
PZ
1032 leftmost = enqueue_hrtimer(timer, new_base);
1033
935c631d
IM
1034 /*
1035 * Only allow reprogramming if the new base is on this CPU.
1036 * (it might still be on another CPU if the timer was pending)
a6037b61
PZ
1037 *
1038 * XXX send_remote_softirq() ?
935c631d 1039 */
b22affe0
LS
1040 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases)
1041 && hrtimer_enqueue_reprogram(timer, new_base)) {
1042 if (wakeup) {
1043 /*
1044 * We need to drop cpu_base->lock to avoid a
1045 * lock ordering issue vs. rq->lock.
1046 */
1047 raw_spin_unlock(&new_base->cpu_base->lock);
1048 raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1049 local_irq_restore(flags);
1050 return ret;
1051 } else {
1052 __raise_softirq_irqoff(HRTIMER_SOFTIRQ);
1053 }
1054 }
c0a31329
TG
1055
1056 unlock_hrtimer_base(timer, &flags);
1057
1058 return ret;
1059}
7f1e2ca9
PZ
1060
1061/**
1062 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU
1063 * @timer: the timer to be added
1064 * @tim: expiry time
1065 * @delta_ns: "slack" range for the timer
8ffbc7d9
DD
1066 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1067 * relative (HRTIMER_MODE_REL)
7f1e2ca9
PZ
1068 *
1069 * Returns:
1070 * 0 on success
1071 * 1 when the timer was active
1072 */
1073int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
1074 unsigned long delta_ns, const enum hrtimer_mode mode)
1075{
1076 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1);
1077}
da8f2e17
AV
1078EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1079
1080/**
e1dd7bc5 1081 * hrtimer_start - (re)start an hrtimer on the current CPU
da8f2e17
AV
1082 * @timer: the timer to be added
1083 * @tim: expiry time
8ffbc7d9
DD
1084 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1085 * relative (HRTIMER_MODE_REL)
da8f2e17
AV
1086 *
1087 * Returns:
1088 * 0 on success
1089 * 1 when the timer was active
1090 */
1091int
1092hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1093{
7f1e2ca9 1094 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
da8f2e17 1095}
8d16b764 1096EXPORT_SYMBOL_GPL(hrtimer_start);
c0a31329 1097
da8f2e17 1098
c0a31329
TG
1099/**
1100 * hrtimer_try_to_cancel - try to deactivate a timer
c0a31329
TG
1101 * @timer: hrtimer to stop
1102 *
1103 * Returns:
1104 * 0 when the timer was not active
1105 * 1 when the timer was active
1106 * -1 when the timer is currently excuting the callback function and
fa9799e3 1107 * cannot be stopped
c0a31329
TG
1108 */
1109int hrtimer_try_to_cancel(struct hrtimer *timer)
1110{
3c8aa39d 1111 struct hrtimer_clock_base *base;
c0a31329
TG
1112 unsigned long flags;
1113 int ret = -1;
1114
1115 base = lock_hrtimer_base(timer, &flags);
1116
303e967f 1117 if (!hrtimer_callback_running(timer))
c0a31329
TG
1118 ret = remove_hrtimer(timer, base);
1119
1120 unlock_hrtimer_base(timer, &flags);
1121
1122 return ret;
1123
1124}
8d16b764 1125EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
c0a31329
TG
1126
1127/**
1128 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
c0a31329
TG
1129 * @timer: the timer to be cancelled
1130 *
1131 * Returns:
1132 * 0 when the timer was not active
1133 * 1 when the timer was active
1134 */
1135int hrtimer_cancel(struct hrtimer *timer)
1136{
1137 for (;;) {
1138 int ret = hrtimer_try_to_cancel(timer);
1139
1140 if (ret >= 0)
1141 return ret;
5ef37b19 1142 cpu_relax();
c0a31329
TG
1143 }
1144}
8d16b764 1145EXPORT_SYMBOL_GPL(hrtimer_cancel);
c0a31329
TG
1146
1147/**
1148 * hrtimer_get_remaining - get remaining time for the timer
c0a31329
TG
1149 * @timer: the timer to read
1150 */
1151ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1152{
c0a31329
TG
1153 unsigned long flags;
1154 ktime_t rem;
1155
b3bd3de6 1156 lock_hrtimer_base(timer, &flags);
cc584b21 1157 rem = hrtimer_expires_remaining(timer);
c0a31329
TG
1158 unlock_hrtimer_base(timer, &flags);
1159
1160 return rem;
1161}
8d16b764 1162EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
c0a31329 1163
3451d024 1164#ifdef CONFIG_NO_HZ_COMMON
69239749
TL
1165/**
1166 * hrtimer_get_next_event - get the time until next expiry event
1167 *
1168 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1169 * is pending.
1170 */
1171ktime_t hrtimer_get_next_event(void)
1172{
3c8aa39d
TG
1173 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1174 struct hrtimer_clock_base *base = cpu_base->clock_base;
69239749
TL
1175 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1176 unsigned long flags;
1177 int i;
1178
ecb49d1a 1179 raw_spin_lock_irqsave(&cpu_base->lock, flags);
3c8aa39d 1180
54cdfdb4
TG
1181 if (!hrtimer_hres_active()) {
1182 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1183 struct hrtimer *timer;
998adc3d 1184 struct timerqueue_node *next;
69239749 1185
998adc3d
JS
1186 next = timerqueue_getnext(&base->active);
1187 if (!next)
54cdfdb4 1188 continue;
3c8aa39d 1189
998adc3d 1190 timer = container_of(next, struct hrtimer, node);
cc584b21 1191 delta.tv64 = hrtimer_get_expires_tv64(timer);
54cdfdb4
TG
1192 delta = ktime_sub(delta, base->get_time());
1193 if (delta.tv64 < mindelta.tv64)
1194 mindelta.tv64 = delta.tv64;
1195 }
69239749 1196 }
3c8aa39d 1197
ecb49d1a 1198 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
3c8aa39d 1199
69239749
TL
1200 if (mindelta.tv64 < 0)
1201 mindelta.tv64 = 0;
1202 return mindelta;
1203}
1204#endif
1205
237fc6e7
TG
1206static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1207 enum hrtimer_mode mode)
c0a31329 1208{
3c8aa39d 1209 struct hrtimer_cpu_base *cpu_base;
e06383db 1210 int base;
c0a31329 1211
7978672c
GA
1212 memset(timer, 0, sizeof(struct hrtimer));
1213
3c8aa39d 1214 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
c0a31329 1215
c9cb2e3d 1216 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
7978672c
GA
1217 clock_id = CLOCK_MONOTONIC;
1218
e06383db
JS
1219 base = hrtimer_clockid_to_base(clock_id);
1220 timer->base = &cpu_base->clock_base[base];
998adc3d 1221 timerqueue_init(&timer->node);
82f67cd9
IM
1222
1223#ifdef CONFIG_TIMER_STATS
1224 timer->start_site = NULL;
1225 timer->start_pid = -1;
1226 memset(timer->start_comm, 0, TASK_COMM_LEN);
1227#endif
c0a31329 1228}
237fc6e7
TG
1229
1230/**
1231 * hrtimer_init - initialize a timer to the given clock
1232 * @timer: the timer to be initialized
1233 * @clock_id: the clock to be used
1234 * @mode: timer mode abs/rel
1235 */
1236void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1237 enum hrtimer_mode mode)
1238{
c6a2a177 1239 debug_init(timer, clock_id, mode);
237fc6e7
TG
1240 __hrtimer_init(timer, clock_id, mode);
1241}
8d16b764 1242EXPORT_SYMBOL_GPL(hrtimer_init);
c0a31329
TG
1243
1244/**
1245 * hrtimer_get_res - get the timer resolution for a clock
c0a31329
TG
1246 * @which_clock: which clock to query
1247 * @tp: pointer to timespec variable to store the resolution
1248 *
72fd4a35
RD
1249 * Store the resolution of the clock selected by @which_clock in the
1250 * variable pointed to by @tp.
c0a31329
TG
1251 */
1252int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1253{
3c8aa39d 1254 struct hrtimer_cpu_base *cpu_base;
e06383db 1255 int base = hrtimer_clockid_to_base(which_clock);
c0a31329 1256
3c8aa39d 1257 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
e06383db 1258 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
c0a31329
TG
1259
1260 return 0;
1261}
8d16b764 1262EXPORT_SYMBOL_GPL(hrtimer_get_res);
c0a31329 1263
6fa3eb70
S
1264#ifdef MTK_HRTIME_DEBUG
1265static void dump_hrtimer_callinfo(struct hrtimer *timer)
1266{
1267
1268 char symname[KSYM_NAME_LEN];
1269 if (lookup_symbol_name((unsigned long)(timer->function), symname) < 0) {
1270 pr_err("timer info1: state/%lx, func/%pK\n",
1271 timer->state, timer->function);
1272 } else {
1273 pr_err("timer info2: state/%lx, func/%s\n",
1274 timer->state, symname);
1275 }
1276
1277 #ifdef CONFIG_TIMER_STATS
1278 if (lookup_symbol_name((unsigned long)(timer->start_site),
1279 symname) < 0) {
1280 pr_err("timer stats1: pid/%d(%s), site/%pK\n",
1281 timer->start_pid, timer->start_comm, timer->start_site);
1282 } else {
1283 pr_err("timer stats2: pid/%d(%s), site/%s\n",
1284 timer->start_pid, timer->start_comm, symname);
1285 }
1286 #endif
1287}
1288#endif
c6a2a177 1289static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
d3d74453
PZ
1290{
1291 struct hrtimer_clock_base *base = timer->base;
1292 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1293 enum hrtimer_restart (*fn)(struct hrtimer *);
1294 int restart;
1295
ca109491
PZ
1296 WARN_ON(!irqs_disabled());
1297
c6a2a177 1298 debug_deactivate(timer);
d3d74453
PZ
1299 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1300 timer_stats_account_hrtimer(timer);
d3d74453 1301 fn = timer->function;
ca109491
PZ
1302
1303 /*
1304 * Because we run timers from hardirq context, there is no chance
1305 * they get migrated to another cpu, therefore its safe to unlock
1306 * the timer base.
1307 */
ecb49d1a 1308 raw_spin_unlock(&cpu_base->lock);
c6a2a177 1309 trace_hrtimer_expire_entry(timer, now);
6fa3eb70
S
1310
1311 mt_trace_hrt_start(fn);
ca109491 1312 restart = fn(timer);
6fa3eb70 1313 mt_trace_hrt_end(fn);
c6a2a177 1314 trace_hrtimer_expire_exit(timer);
ecb49d1a 1315 raw_spin_lock(&cpu_base->lock);
d3d74453
PZ
1316
1317 /*
e3f1d883
TG
1318 * Note: We clear the CALLBACK bit after enqueue_hrtimer and
1319 * we do not reprogramm the event hardware. Happens either in
1320 * hrtimer_start_range_ns() or in hrtimer_interrupt()
d3d74453
PZ
1321 */
1322 if (restart != HRTIMER_NORESTART) {
1323 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
a6037b61 1324 enqueue_hrtimer(timer, base);
d3d74453 1325 }
f13d4f97
SQ
1326
1327 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1328
d3d74453
PZ
1329 timer->state &= ~HRTIMER_STATE_CALLBACK;
1330}
1331
54cdfdb4
TG
1332#ifdef CONFIG_HIGH_RES_TIMERS
1333
1334/*
1335 * High resolution timer interrupt
1336 * Called with interrupts disabled
1337 */
1338void hrtimer_interrupt(struct clock_event_device *dev)
1339{
1340 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
41d2e494
TG
1341 ktime_t expires_next, now, entry_time, delta;
1342 int i, retries = 0;
54cdfdb4
TG
1343
1344 BUG_ON(!cpu_base->hres_active);
1345 cpu_base->nr_events++;
1346 dev->next_event.tv64 = KTIME_MAX;
1347
196951e9 1348 raw_spin_lock(&cpu_base->lock);
5baefd6d 1349 entry_time = now = hrtimer_update_base(cpu_base);
41d2e494 1350retry:
54cdfdb4 1351 expires_next.tv64 = KTIME_MAX;
6ff7041d
TG
1352 /*
1353 * We set expires_next to KTIME_MAX here with cpu_base->lock
1354 * held to prevent that a timer is enqueued in our queue via
1355 * the migration code. This does not affect enqueueing of
1356 * timers which run their callback and need to be requeued on
1357 * this CPU.
1358 */
1359 cpu_base->expires_next.tv64 = KTIME_MAX;
1360
54cdfdb4 1361 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
ab8177bc 1362 struct hrtimer_clock_base *base;
998adc3d 1363 struct timerqueue_node *node;
ab8177bc
TG
1364 ktime_t basenow;
1365
1366 if (!(cpu_base->active_bases & (1 << i)))
1367 continue;
54cdfdb4 1368
ab8177bc 1369 base = cpu_base->clock_base + i;
54cdfdb4
TG
1370 basenow = ktime_add(now, base->offset);
1371
998adc3d 1372 while ((node = timerqueue_getnext(&base->active))) {
54cdfdb4
TG
1373 struct hrtimer *timer;
1374
998adc3d 1375 timer = container_of(node, struct hrtimer, node);
54cdfdb4 1376
654c8e0b
AV
1377 /*
1378 * The immediate goal for using the softexpires is
1379 * minimizing wakeups, not running timers at the
1380 * earliest interrupt after their soft expiration.
1381 * This allows us to avoid using a Priority Search
1382 * Tree, which can answer a stabbing querry for
1383 * overlapping intervals and instead use the simple
1384 * BST we already have.
1385 * We don't add extra wakeups by delaying timers that
1386 * are right-of a not yet expired timer, because that
1387 * timer will have to trigger a wakeup anyway.
1388 */
1389
1390 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) {
54cdfdb4
TG
1391 ktime_t expires;
1392
cc584b21 1393 expires = ktime_sub(hrtimer_get_expires(timer),
54cdfdb4 1394 base->offset);
8f294b5a
PB
1395 if (expires.tv64 < 0)
1396 expires.tv64 = KTIME_MAX;
54cdfdb4
TG
1397 if (expires.tv64 < expires_next.tv64)
1398 expires_next = expires;
1399 break;
1400 }
1401
c6a2a177 1402 __run_hrtimer(timer, &basenow);
54cdfdb4 1403 }
54cdfdb4
TG
1404 }
1405
6ff7041d
TG
1406 /*
1407 * Store the new expiry value so the migration code can verify
1408 * against it.
1409 */
54cdfdb4 1410 cpu_base->expires_next = expires_next;
ecb49d1a 1411 raw_spin_unlock(&cpu_base->lock);
54cdfdb4
TG
1412
1413 /* Reprogramming necessary ? */
41d2e494
TG
1414 if (expires_next.tv64 == KTIME_MAX ||
1415 !tick_program_event(expires_next, 0)) {
1416 cpu_base->hang_detected = 0;
1417 return;
54cdfdb4 1418 }
41d2e494
TG
1419
1420 /*
1421 * The next timer was already expired due to:
1422 * - tracing
1423 * - long lasting callbacks
1424 * - being scheduled away when running in a VM
1425 *
1426 * We need to prevent that we loop forever in the hrtimer
1427 * interrupt routine. We give it 3 attempts to avoid
1428 * overreacting on some spurious event.
5baefd6d
JS
1429 *
1430 * Acquire base lock for updating the offsets and retrieving
1431 * the current time.
41d2e494 1432 */
196951e9 1433 raw_spin_lock(&cpu_base->lock);
5baefd6d 1434 now = hrtimer_update_base(cpu_base);
41d2e494
TG
1435 cpu_base->nr_retries++;
1436 if (++retries < 3)
1437 goto retry;
1438 /*
1439 * Give the system a chance to do something else than looping
1440 * here. We stored the entry time, so we know exactly how long
1441 * we spent here. We schedule the next event this amount of
1442 * time away.
1443 */
1444 cpu_base->nr_hangs++;
1445 cpu_base->hang_detected = 1;
196951e9 1446 raw_spin_unlock(&cpu_base->lock);
41d2e494
TG
1447 delta = ktime_sub(now, entry_time);
1448 if (delta.tv64 > cpu_base->max_hang_time.tv64)
1449 cpu_base->max_hang_time = delta;
1450 /*
1451 * Limit it to a sensible value as we enforce a longer
1452 * delay. Give the CPU at least 100ms to catch up.
1453 */
1454 if (delta.tv64 > 100 * NSEC_PER_MSEC)
1455 expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC);
1456 else
1457 expires_next = ktime_add(now, delta);
1458 tick_program_event(expires_next, 1);
1459 printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n",
1460 ktime_to_ns(delta));
54cdfdb4
TG
1461}
1462
8bdec955
TG
1463/*
1464 * local version of hrtimer_peek_ahead_timers() called with interrupts
1465 * disabled.
1466 */
1467static void __hrtimer_peek_ahead_timers(void)
1468{
1469 struct tick_device *td;
1470
1471 if (!hrtimer_hres_active())
1472 return;
1473
1474 td = &__get_cpu_var(tick_cpu_device);
1475 if (td && td->evtdev)
1476 hrtimer_interrupt(td->evtdev);
1477}
1478
2e94d1f7
AV
1479/**
1480 * hrtimer_peek_ahead_timers -- run soft-expired timers now
1481 *
1482 * hrtimer_peek_ahead_timers will peek at the timer queue of
1483 * the current cpu and check if there are any timers for which
1484 * the soft expires time has passed. If any such timers exist,
1485 * they are run immediately and then removed from the timer queue.
1486 *
1487 */
1488void hrtimer_peek_ahead_timers(void)
1489{
643bdf68 1490 unsigned long flags;
dc4304f7 1491
2e94d1f7 1492 local_irq_save(flags);
8bdec955 1493 __hrtimer_peek_ahead_timers();
2e94d1f7
AV
1494 local_irq_restore(flags);
1495}
1496
a6037b61
PZ
1497static void run_hrtimer_softirq(struct softirq_action *h)
1498{
1499 hrtimer_peek_ahead_timers();
1500}
1501
82c5b7b5
IM
1502#else /* CONFIG_HIGH_RES_TIMERS */
1503
1504static inline void __hrtimer_peek_ahead_timers(void) { }
1505
1506#endif /* !CONFIG_HIGH_RES_TIMERS */
82f67cd9 1507
d3d74453
PZ
1508/*
1509 * Called from timer softirq every jiffy, expire hrtimers:
1510 *
1511 * For HRT its the fall back code to run the softirq in the timer
1512 * softirq context in case the hrtimer initialization failed or has
1513 * not been done yet.
1514 */
1515void hrtimer_run_pending(void)
1516{
d3d74453
PZ
1517 if (hrtimer_hres_active())
1518 return;
54cdfdb4 1519
d3d74453
PZ
1520 /*
1521 * This _is_ ugly: We have to check in the softirq context,
1522 * whether we can switch to highres and / or nohz mode. The
1523 * clocksource switch happens in the timer interrupt with
1524 * xtime_lock held. Notification from there only sets the
1525 * check bit in the tick_oneshot code, otherwise we might
1526 * deadlock vs. xtime_lock.
1527 */
1528 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1529 hrtimer_switch_to_hres();
54cdfdb4
TG
1530}
1531
c0a31329 1532/*
d3d74453 1533 * Called from hardirq context every jiffy
c0a31329 1534 */
833883d9 1535void hrtimer_run_queues(void)
c0a31329 1536{
998adc3d 1537 struct timerqueue_node *node;
833883d9
DS
1538 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1539 struct hrtimer_clock_base *base;
1540 int index, gettime = 1;
c0a31329 1541
833883d9 1542 if (hrtimer_hres_active())
3055adda
DS
1543 return;
1544
833883d9
DS
1545 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1546 base = &cpu_base->clock_base[index];
b007c389 1547 if (!timerqueue_getnext(&base->active))
d3d74453 1548 continue;
833883d9 1549
d7cfb60c 1550 if (gettime) {
833883d9
DS
1551 hrtimer_get_softirq_time(cpu_base);
1552 gettime = 0;
b75f7a51 1553 }
d3d74453 1554
ecb49d1a 1555 raw_spin_lock(&cpu_base->lock);
c0a31329 1556
b007c389 1557 while ((node = timerqueue_getnext(&base->active))) {
833883d9 1558 struct hrtimer *timer;
54cdfdb4 1559
998adc3d 1560 timer = container_of(node, struct hrtimer, node);
cc584b21
AV
1561 if (base->softirq_time.tv64 <=
1562 hrtimer_get_expires_tv64(timer))
833883d9
DS
1563 break;
1564
c6a2a177 1565 __run_hrtimer(timer, &base->softirq_time);
833883d9 1566 }
ecb49d1a 1567 raw_spin_unlock(&cpu_base->lock);
833883d9 1568 }
c0a31329
TG
1569}
1570
10c94ec1
TG
1571/*
1572 * Sleep related functions:
1573 */
c9cb2e3d 1574static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
00362e33
TG
1575{
1576 struct hrtimer_sleeper *t =
1577 container_of(timer, struct hrtimer_sleeper, timer);
1578 struct task_struct *task = t->task;
1579
1580 t->task = NULL;
1581 if (task)
1582 wake_up_process(task);
1583
1584 return HRTIMER_NORESTART;
1585}
1586
36c8b586 1587void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
00362e33
TG
1588{
1589 sl->timer.function = hrtimer_wakeup;
1590 sl->task = task;
1591}
2bc481cf 1592EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
00362e33 1593
669d7868 1594static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
432569bb 1595{
669d7868 1596 hrtimer_init_sleeper(t, current);
10c94ec1 1597
432569bb
RZ
1598 do {
1599 set_current_state(TASK_INTERRUPTIBLE);
cc584b21 1600 hrtimer_start_expires(&t->timer, mode);
37bb6cb4
PZ
1601 if (!hrtimer_active(&t->timer))
1602 t->task = NULL;
432569bb 1603
54cdfdb4 1604 if (likely(t->task))
6fa3eb70 1605 freezable_schedule();
432569bb 1606
669d7868 1607 hrtimer_cancel(&t->timer);
c9cb2e3d 1608 mode = HRTIMER_MODE_ABS;
669d7868
TG
1609
1610 } while (t->task && !signal_pending(current));
432569bb 1611
3588a085
PZ
1612 __set_current_state(TASK_RUNNING);
1613
669d7868 1614 return t->task == NULL;
10c94ec1
TG
1615}
1616
080344b9
ON
1617static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1618{
1619 struct timespec rmt;
1620 ktime_t rem;
1621
cc584b21 1622 rem = hrtimer_expires_remaining(timer);
080344b9
ON
1623 if (rem.tv64 <= 0)
1624 return 0;
1625 rmt = ktime_to_timespec(rem);
1626
1627 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1628 return -EFAULT;
1629
1630 return 1;
1631}
1632
1711ef38 1633long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
10c94ec1 1634{
669d7868 1635 struct hrtimer_sleeper t;
080344b9 1636 struct timespec __user *rmtp;
237fc6e7 1637 int ret = 0;
10c94ec1 1638
ab8177bc 1639 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
237fc6e7 1640 HRTIMER_MODE_ABS);
cc584b21 1641 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
10c94ec1 1642
c9cb2e3d 1643 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
237fc6e7 1644 goto out;
10c94ec1 1645
029a07e0 1646 rmtp = restart->nanosleep.rmtp;
432569bb 1647 if (rmtp) {
237fc6e7 1648 ret = update_rmtp(&t.timer, rmtp);
080344b9 1649 if (ret <= 0)
237fc6e7 1650 goto out;
432569bb 1651 }
10c94ec1 1652
10c94ec1 1653 /* The other values in restart are already filled in */
237fc6e7
TG
1654 ret = -ERESTART_RESTARTBLOCK;
1655out:
1656 destroy_hrtimer_on_stack(&t.timer);
1657 return ret;
10c94ec1
TG
1658}
1659
080344b9 1660long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
10c94ec1
TG
1661 const enum hrtimer_mode mode, const clockid_t clockid)
1662{
1663 struct restart_block *restart;
669d7868 1664 struct hrtimer_sleeper t;
237fc6e7 1665 int ret = 0;
3bd01206
AV
1666 unsigned long slack;
1667
1668 slack = current->timer_slack_ns;
1669 if (rt_task(current))
1670 slack = 0;
10c94ec1 1671
237fc6e7 1672 hrtimer_init_on_stack(&t.timer, clockid, mode);
3bd01206 1673 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
432569bb 1674 if (do_nanosleep(&t, mode))
237fc6e7 1675 goto out;
10c94ec1 1676
7978672c 1677 /* Absolute timers do not update the rmtp value and restart: */
237fc6e7
TG
1678 if (mode == HRTIMER_MODE_ABS) {
1679 ret = -ERESTARTNOHAND;
1680 goto out;
1681 }
10c94ec1 1682
432569bb 1683 if (rmtp) {
237fc6e7 1684 ret = update_rmtp(&t.timer, rmtp);
080344b9 1685 if (ret <= 0)
237fc6e7 1686 goto out;
432569bb 1687 }
10c94ec1
TG
1688
1689 restart = &current_thread_info()->restart_block;
1711ef38 1690 restart->fn = hrtimer_nanosleep_restart;
ab8177bc 1691 restart->nanosleep.clockid = t.timer.base->clockid;
029a07e0 1692 restart->nanosleep.rmtp = rmtp;
cc584b21 1693 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
10c94ec1 1694
237fc6e7
TG
1695 ret = -ERESTART_RESTARTBLOCK;
1696out:
1697 destroy_hrtimer_on_stack(&t.timer);
1698 return ret;
10c94ec1
TG
1699}
1700
58fd3aa2
HC
1701SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1702 struct timespec __user *, rmtp)
6ba1b912 1703{
080344b9 1704 struct timespec tu;
6ba1b912
TG
1705
1706 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1707 return -EFAULT;
1708
1709 if (!timespec_valid(&tu))
1710 return -EINVAL;
1711
080344b9 1712 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
6ba1b912
TG
1713}
1714
c0a31329
TG
1715/*
1716 * Functions related to boot-time initialization:
1717 */
0ec160dd 1718static void __cpuinit init_hrtimers_cpu(int cpu)
c0a31329 1719{
3c8aa39d 1720 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
c0a31329
TG
1721 int i;
1722
998adc3d 1723 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
3c8aa39d 1724 cpu_base->clock_base[i].cpu_base = cpu_base;
998adc3d
JS
1725 timerqueue_init_head(&cpu_base->clock_base[i].active);
1726 }
3c8aa39d 1727
54cdfdb4 1728 hrtimer_init_hres(cpu_base);
c0a31329
TG
1729}
1730
1731#ifdef CONFIG_HOTPLUG_CPU
1732
ca109491 1733static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
37810659 1734 struct hrtimer_clock_base *new_base)
c0a31329
TG
1735{
1736 struct hrtimer *timer;
998adc3d 1737 struct timerqueue_node *node;
c0a31329 1738
998adc3d
JS
1739 while ((node = timerqueue_getnext(&old_base->active))) {
1740 timer = container_of(node, struct hrtimer, node);
54cdfdb4 1741 BUG_ON(hrtimer_callback_running(timer));
c6a2a177 1742 debug_deactivate(timer);
b00c1a99
TG
1743
1744 /*
1745 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
1746 * timer could be seen as !active and just vanish away
1747 * under us on another CPU
1748 */
1749 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0);
c0a31329 1750 timer->base = new_base;
54cdfdb4 1751 /*
e3f1d883
TG
1752 * Enqueue the timers on the new cpu. This does not
1753 * reprogram the event device in case the timer
1754 * expires before the earliest on this CPU, but we run
1755 * hrtimer_interrupt after we migrated everything to
1756 * sort out already expired timers and reprogram the
1757 * event device.
54cdfdb4 1758 */
a6037b61 1759 enqueue_hrtimer(timer, new_base);
41e1022e 1760
b00c1a99
TG
1761 /* Clear the migration state bit */
1762 timer->state &= ~HRTIMER_STATE_MIGRATE;
c0a31329
TG
1763 }
1764}
1765
d5fd43c4 1766static void migrate_hrtimers(int scpu)
c0a31329 1767{
3c8aa39d 1768 struct hrtimer_cpu_base *old_base, *new_base;
731a55ba 1769 int i;
c0a31329 1770
37810659 1771 BUG_ON(cpu_online(scpu));
37810659 1772 tick_cancel_sched_timer(scpu);
731a55ba
TG
1773
1774 local_irq_disable();
1775 old_base = &per_cpu(hrtimer_bases, scpu);
1776 new_base = &__get_cpu_var(hrtimer_bases);
d82f0b0f
ON
1777 /*
1778 * The caller is globally serialized and nobody else
1779 * takes two locks at once, deadlock is not possible.
1780 */
ecb49d1a
TG
1781 raw_spin_lock(&new_base->lock);
1782 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
c0a31329 1783
3c8aa39d 1784 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
ca109491 1785 migrate_hrtimer_list(&old_base->clock_base[i],
37810659 1786 &new_base->clock_base[i]);
c0a31329
TG
1787 }
1788
ecb49d1a
TG
1789 raw_spin_unlock(&old_base->lock);
1790 raw_spin_unlock(&new_base->lock);
37810659 1791
731a55ba
TG
1792 /* Check, if we got expired work to do */
1793 __hrtimer_peek_ahead_timers();
1794 local_irq_enable();
c0a31329 1795}
37810659 1796
c0a31329
TG
1797#endif /* CONFIG_HOTPLUG_CPU */
1798
8c78f307 1799static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
c0a31329
TG
1800 unsigned long action, void *hcpu)
1801{
b2e3c0ad 1802 int scpu = (long)hcpu;
c0a31329
TG
1803
1804 switch (action) {
1805
1806 case CPU_UP_PREPARE:
8bb78442 1807 case CPU_UP_PREPARE_FROZEN:
37810659 1808 init_hrtimers_cpu(scpu);
c0a31329
TG
1809 break;
1810
1811#ifdef CONFIG_HOTPLUG_CPU
94df7de0
SD
1812 case CPU_DYING:
1813 case CPU_DYING_FROZEN:
1814 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1815 break;
c0a31329 1816 case CPU_DEAD:
8bb78442 1817 case CPU_DEAD_FROZEN:
b2e3c0ad 1818 {
37810659 1819 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
d5fd43c4 1820 migrate_hrtimers(scpu);
c0a31329 1821 break;
b2e3c0ad 1822 }
c0a31329
TG
1823#endif
1824
1825 default:
1826 break;
1827 }
1828
1829 return NOTIFY_OK;
1830}
1831
8c78f307 1832static struct notifier_block __cpuinitdata hrtimers_nb = {
c0a31329
TG
1833 .notifier_call = hrtimer_cpu_notify,
1834};
1835
1836void __init hrtimers_init(void)
1837{
1838 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1839 (void *)(long)smp_processor_id());
1840 register_cpu_notifier(&hrtimers_nb);
a6037b61
PZ
1841#ifdef CONFIG_HIGH_RES_TIMERS
1842 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1843#endif
c0a31329
TG
1844}
1845
7bb67439 1846/**
351b3f7a 1847 * schedule_hrtimeout_range_clock - sleep until timeout
7bb67439 1848 * @expires: timeout value (ktime_t)
654c8e0b 1849 * @delta: slack in expires timeout (ktime_t)
7bb67439 1850 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
351b3f7a 1851 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
7bb67439 1852 */
351b3f7a
CE
1853int __sched
1854schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1855 const enum hrtimer_mode mode, int clock)
7bb67439
AV
1856{
1857 struct hrtimer_sleeper t;
1858
1859 /*
1860 * Optimize when a zero timeout value is given. It does not
1861 * matter whether this is an absolute or a relative time.
1862 */
1863 if (expires && !expires->tv64) {
1864 __set_current_state(TASK_RUNNING);
1865 return 0;
1866 }
1867
1868 /*
43b21013 1869 * A NULL parameter means "infinite"
7bb67439
AV
1870 */
1871 if (!expires) {
1872 schedule();
1873 __set_current_state(TASK_RUNNING);
1874 return -EINTR;
1875 }
1876
351b3f7a 1877 hrtimer_init_on_stack(&t.timer, clock, mode);
654c8e0b 1878 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
7bb67439
AV
1879
1880 hrtimer_init_sleeper(&t, current);
1881
cc584b21 1882 hrtimer_start_expires(&t.timer, mode);
7bb67439
AV
1883 if (!hrtimer_active(&t.timer))
1884 t.task = NULL;
1885
1886 if (likely(t.task))
1887 schedule();
1888
1889 hrtimer_cancel(&t.timer);
1890 destroy_hrtimer_on_stack(&t.timer);
1891
1892 __set_current_state(TASK_RUNNING);
1893
1894 return !t.task ? 0 : -EINTR;
1895}
351b3f7a
CE
1896
1897/**
1898 * schedule_hrtimeout_range - sleep until timeout
1899 * @expires: timeout value (ktime_t)
1900 * @delta: slack in expires timeout (ktime_t)
1901 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1902 *
1903 * Make the current task sleep until the given expiry time has
1904 * elapsed. The routine will return immediately unless
1905 * the current task state has been set (see set_current_state()).
1906 *
1907 * The @delta argument gives the kernel the freedom to schedule the
1908 * actual wakeup to a time that is both power and performance friendly.
1909 * The kernel give the normal best effort behavior for "@expires+@delta",
1910 * but may decide to fire the timer earlier, but no earlier than @expires.
1911 *
1912 * You can set the task state as follows -
1913 *
1914 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1915 * pass before the routine returns.
1916 *
1917 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1918 * delivered to the current task.
1919 *
1920 * The current task state is guaranteed to be TASK_RUNNING when this
1921 * routine returns.
1922 *
1923 * Returns 0 when the timer has expired otherwise -EINTR
1924 */
1925int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
1926 const enum hrtimer_mode mode)
1927{
1928 return schedule_hrtimeout_range_clock(expires, delta, mode,
1929 CLOCK_MONOTONIC);
1930}
654c8e0b
AV
1931EXPORT_SYMBOL_GPL(schedule_hrtimeout_range);
1932
1933/**
1934 * schedule_hrtimeout - sleep until timeout
1935 * @expires: timeout value (ktime_t)
1936 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1937 *
1938 * Make the current task sleep until the given expiry time has
1939 * elapsed. The routine will return immediately unless
1940 * the current task state has been set (see set_current_state()).
1941 *
1942 * You can set the task state as follows -
1943 *
1944 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to
1945 * pass before the routine returns.
1946 *
1947 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1948 * delivered to the current task.
1949 *
1950 * The current task state is guaranteed to be TASK_RUNNING when this
1951 * routine returns.
1952 *
1953 * Returns 0 when the timer has expired otherwise -EINTR
1954 */
1955int __sched schedule_hrtimeout(ktime_t *expires,
1956 const enum hrtimer_mode mode)
1957{
1958 return schedule_hrtimeout_range(expires, 0, mode);
1959}
7bb67439 1960EXPORT_SYMBOL_GPL(schedule_hrtimeout);