drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / hrtimer.c
1 /*
2 * linux/kernel/hrtimer.c
3 *
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
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 *
25 * Help, testing, suggestions, bugfixes, improvements were
26 * provided by:
27 *
28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
29 * et. al.
30 *
31 * For licencing details see kernel-base/COPYING
32 */
33
34 #include <linux/cpu.h>
35 #include <linux/export.h>
36 #include <linux/percpu.h>
37 #include <linux/hrtimer.h>
38 #include <linux/notifier.h>
39 #include <linux/syscalls.h>
40 #include <linux/kallsyms.h>
41 #include <linux/interrupt.h>
42 #include <linux/tick.h>
43 #include <linux/seq_file.h>
44 #include <linux/err.h>
45 #include <linux/debugobjects.h>
46 #include <linux/sched.h>
47 #include <linux/sched/sysctl.h>
48 #include <linux/sched/rt.h>
49 #include <linux/timer.h>
50 #include <linux/freezer.h>
51
52 #include <asm/uaccess.h>
53
54 #include <trace/events/timer.h>
55
56 #include <linux/mt_sched_mon.h>
57
58 //#define MTK_HRTIME_DEBUG /*MTK debug func*/
59 /*
60 * The timer bases:
61 *
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.
66 */
67 DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
68 {
69
70 .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock),
71 .clock_base =
72 {
73 {
74 .index = HRTIMER_BASE_MONOTONIC,
75 .clockid = CLOCK_MONOTONIC,
76 .get_time = &ktime_get,
77 .resolution = KTIME_LOW_RES,
78 },
79 {
80 .index = HRTIMER_BASE_REALTIME,
81 .clockid = CLOCK_REALTIME,
82 .get_time = &ktime_get_real,
83 .resolution = KTIME_LOW_RES,
84 },
85 {
86 .index = HRTIMER_BASE_BOOTTIME,
87 .clockid = CLOCK_BOOTTIME,
88 .get_time = &ktime_get_boottime,
89 .resolution = KTIME_LOW_RES,
90 },
91 {
92 .index = HRTIMER_BASE_TAI,
93 .clockid = CLOCK_TAI,
94 .get_time = &ktime_get_clocktai,
95 .resolution = KTIME_LOW_RES,
96 },
97 }
98 };
99
100 static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
101 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
102 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
103 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
104 [CLOCK_TAI] = HRTIMER_BASE_TAI,
105 };
106
107 static inline int hrtimer_clockid_to_base(clockid_t clock_id)
108 {
109 return hrtimer_clock_to_base_table[clock_id];
110 }
111
112
113 /*
114 * Get the coarse grained time at the softirq based on xtime and
115 * wall_to_monotonic.
116 */
117 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
118 {
119 ktime_t xtim, mono, boot;
120 struct timespec xts, tom, slp;
121 s32 tai_offset;
122
123 get_xtime_and_monotonic_and_sleep_offset(&xts, &tom, &slp);
124 tai_offset = timekeeping_get_tai_offset();
125
126 xtim = timespec_to_ktime(xts);
127 mono = ktime_add(xtim, timespec_to_ktime(tom));
128 boot = ktime_add(mono, timespec_to_ktime(slp));
129 base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
130 base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
131 base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
132 base->clock_base[HRTIMER_BASE_TAI].softirq_time =
133 ktime_add(xtim, ktime_set(tai_offset, 0));
134 }
135
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
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 */
154 static
155 struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
156 unsigned long *flags)
157 {
158 struct hrtimer_clock_base *base;
159
160 for (;;) {
161 base = timer->base;
162 if (likely(base != NULL)) {
163 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
164 if (likely(base == timer->base))
165 return base;
166 /* The timer has migrated to another CPU: */
167 raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
168 }
169 cpu_relax();
170 }
171 }
172
173
174 /*
175 * Get the preferred target CPU for NOHZ
176 */
177 static int hrtimer_get_target(int this_cpu, int pinned)
178 {
179 #ifdef CONFIG_NO_HZ_COMMON
180 if (!pinned && get_sysctl_timer_migration() && idle_cpu(this_cpu))
181 return get_nohz_timer_target();
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 */
193 static int
194 hrtimer_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
209 /*
210 * Switch the timer base to the current CPU when possible.
211 */
212 static inline struct hrtimer_clock_base *
213 switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
214 int pinned)
215 {
216 struct hrtimer_clock_base *new_base;
217 struct hrtimer_cpu_base *new_cpu_base;
218 int this_cpu = smp_processor_id();
219 int cpu = hrtimer_get_target(this_cpu, pinned);
220 int basenum = base->index;
221
222 again:
223 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
224 new_base = &new_cpu_base->clock_base[basenum];
225
226 if (base != new_base) {
227 /*
228 * We are trying to move timer to new_base.
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 */
236 if (unlikely(hrtimer_callback_running(timer)))
237 return base;
238
239 /* See the comment in lock_timer_base() */
240 timer->base = NULL;
241 raw_spin_unlock(&base->cpu_base->lock);
242 raw_spin_lock(&new_base->cpu_base->lock);
243
244 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
245 cpu = this_cpu;
246 raw_spin_unlock(&new_base->cpu_base->lock);
247 raw_spin_lock(&base->cpu_base->lock);
248 timer->base = base;
249 goto again;
250 }
251 timer->base = new_base;
252 } else {
253 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
254 cpu = this_cpu;
255 goto again;
256 }
257 }
258 return new_base;
259 }
260
261 #else /* CONFIG_SMP */
262
263 static inline struct hrtimer_clock_base *
264 lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
265 {
266 struct hrtimer_clock_base *base = timer->base;
267
268 raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
269
270 return base;
271 }
272
273 # define switch_hrtimer_base(t, b, p) (b)
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
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 */
290 ktime_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
299 /* Make sure nsec fits into long */
300 if (unlikely(nsec > KTIME_SEC_MAX))
301 return (ktime_t){ .tv64 = KTIME_MAX };
302
303 tmp = ktime_set((long)nsec, rem);
304 }
305
306 return ktime_add(kt, tmp);
307 }
308
309 EXPORT_SYMBOL_GPL(ktime_add_ns);
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 */
318 ktime_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
333 EXPORT_SYMBOL_GPL(ktime_sub_ns);
334 # endif /* !CONFIG_KTIME_SCALAR */
335
336 /*
337 * Divide a ktime value by a nanosecond value
338 */
339 u64 ktime_divns(const ktime_t kt, s64 div)
340 {
341 u64 dclc;
342 int sft = 0;
343
344 dclc = ktime_to_ns(kt);
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
353 return dclc;
354 }
355 #endif /* BITS_PER_LONG >= 64 */
356
357 /*
358 * Add two ktime values and do a safety check for overflow:
359 */
360 ktime_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
374 EXPORT_SYMBOL_GPL(ktime_add_safe);
375
376 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
377
378 static struct debug_obj_descr hrtimer_debug_descr;
379
380 static void *hrtimer_debug_hint(void *addr)
381 {
382 return ((struct hrtimer *) addr)->function;
383 }
384
385 /*
386 * fixup_init is called when:
387 * - an active object is initialized
388 */
389 static 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 */
408 static 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 */
428 static 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
442 static struct debug_obj_descr hrtimer_debug_descr = {
443 .name = "hrtimer",
444 .debug_hint = hrtimer_debug_hint,
445 .fixup_init = hrtimer_fixup_init,
446 .fixup_activate = hrtimer_fixup_activate,
447 .fixup_free = hrtimer_fixup_free,
448 };
449
450 static inline void debug_hrtimer_init(struct hrtimer *timer)
451 {
452 debug_object_init(timer, &hrtimer_debug_descr);
453 }
454
455 static inline void debug_hrtimer_activate(struct hrtimer *timer)
456 {
457 debug_object_activate(timer, &hrtimer_debug_descr);
458 }
459
460 static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
461 {
462 debug_object_deactivate(timer, &hrtimer_debug_descr);
463 }
464
465 static inline void debug_hrtimer_free(struct hrtimer *timer)
466 {
467 debug_object_free(timer, &hrtimer_debug_descr);
468 }
469
470 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
471 enum hrtimer_mode mode);
472
473 void 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 }
479 EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
480
481 void destroy_hrtimer_on_stack(struct hrtimer *timer)
482 {
483 debug_object_free(timer, &hrtimer_debug_descr);
484 }
485
486 #else
487 static inline void debug_hrtimer_init(struct hrtimer *timer) { }
488 static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
489 static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
490 #endif
491
492 static inline void
493 debug_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
500 static inline void debug_activate(struct hrtimer *timer)
501 {
502 debug_hrtimer_activate(timer);
503 trace_hrtimer_start(timer);
504 }
505
506 static inline void debug_deactivate(struct hrtimer *timer)
507 {
508 debug_hrtimer_deactivate(timer);
509 trace_hrtimer_cancel(timer);
510 }
511
512 /* High resolution timer related functions */
513 #ifdef CONFIG_HIGH_RES_TIMERS
514
515 /*
516 * High resolution timer enabled ?
517 */
518 static int hrtimer_hres_enabled __read_mostly = 1;
519
520 /*
521 * Enable / Disable high resolution mode
522 */
523 static 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 */
539 static inline int hrtimer_is_hres_enabled(void)
540 {
541 return hrtimer_hres_enabled;
542 }
543
544 /*
545 * Is the high resolution mode active ?
546 */
547 static inline int hrtimer_hres_active(void)
548 {
549 return __this_cpu_read(hrtimer_bases.hres_active);
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 */
557 static void
558 hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
559 {
560 int i;
561 struct hrtimer_clock_base *base = cpu_base->clock_base;
562 ktime_t expires, expires_next;
563
564 expires_next.tv64 = KTIME_MAX;
565
566 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
567 struct hrtimer *timer;
568 struct timerqueue_node *next;
569
570 next = timerqueue_getnext(&base->active);
571 if (!next)
572 continue;
573 timer = container_of(next, struct hrtimer, node);
574
575 expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
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;
583 if (expires.tv64 < expires_next.tv64)
584 expires_next = expires;
585 }
586
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
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
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 */
622 static int hrtimer_reprogram(struct hrtimer *timer,
623 struct hrtimer_clock_base *base)
624 {
625 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
626 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset);
627 int res;
628
629 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0);
630
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
634 * the callback is executed in the hrtimer_interrupt context. The
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
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
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)
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))
667 cpu_base->expires_next = expires;
668 return res;
669 }
670
671 /*
672 * Initialize the high resolution related parts of cpu_base
673 */
674 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
675 {
676 base->expires_next.tv64 = KTIME_MAX;
677 base->hres_active = 0;
678 }
679
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 */
686 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
687 struct hrtimer_clock_base *base)
688 {
689 return base->cpu_base->hres_active && hrtimer_reprogram(timer, base);
690 }
691
692 static 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;
696 ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
697
698 return ktime_get_update_offsets(offs_real, offs_boot, offs_tai);
699 }
700
701 /*
702 * Retrigger next event is called after clock was set
703 *
704 * Called with interrupts disabled via on_each_cpu()
705 */
706 static void retrigger_next_event(void *arg)
707 {
708 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
709
710 if (!hrtimer_hres_active())
711 return;
712
713 raw_spin_lock(&base->lock);
714 hrtimer_update_base(base);
715 hrtimer_force_reprogram(base, 0);
716 raw_spin_unlock(&base->lock);
717 }
718
719 /*
720 * Switch to high resolution mode
721 */
722 static int hrtimer_switch_to_hres(void)
723 {
724 int i, cpu = smp_processor_id();
725 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
726 unsigned long flags;
727
728 if (base->hres_active)
729 return 1;
730
731 local_irq_save(flags);
732
733 if (tick_init_highres()) {
734 local_irq_restore(flags);
735 printk(KERN_WARNING "Could not switch to high resolution "
736 "mode on CPU %d\n", cpu);
737 return 0;
738 }
739 base->hres_active = 1;
740 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
741 base->clock_base[i].resolution = KTIME_HIGH_RES;
742
743 tick_setup_sched_timer();
744 /* "Retrigger" the interrupt to get things going */
745 retrigger_next_event(NULL);
746 local_irq_restore(flags);
747 return 1;
748 }
749
750 static void clock_was_set_work(struct work_struct *work)
751 {
752 clock_was_set();
753 }
754
755 static DECLARE_WORK(hrtimer_work, clock_was_set_work);
756
757 /*
758 * Called from timekeeping and resume code to reprogramm the hrtimer
759 * interrupt device on all cpus.
760 */
761 void clock_was_set_delayed(void)
762 {
763 schedule_work(&hrtimer_work);
764 }
765
766 #else
767
768 static inline int hrtimer_hres_active(void) { return 0; }
769 static inline int hrtimer_is_hres_enabled(void) { return 0; }
770 static inline int hrtimer_switch_to_hres(void) { return 0; }
771 static inline void
772 hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
773 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
774 struct hrtimer_clock_base *base)
775 {
776 return 0;
777 }
778 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
779 static inline void retrigger_next_event(void *arg) { }
780
781 #endif /* CONFIG_HIGH_RES_TIMERS */
782
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 */
794 void clock_was_set(void)
795 {
796 #ifdef CONFIG_HIGH_RES_TIMERS
797 /* Retrigger the CPU local events everywhere */
798 on_each_cpu(retrigger_next_event, NULL, 1);
799 #endif
800 timerfd_clock_was_set();
801 }
802
803 /*
804 * During resume we might have to reprogram the high resolution timer
805 * interrupt (on the local CPU):
806 */
807 void hrtimers_resume(void)
808 {
809 WARN_ONCE(!irqs_disabled(),
810 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
811
812 /* Retrigger on the local CPU */
813 retrigger_next_event(NULL);
814 /* And schedule a retrigger for all others */
815 clock_was_set_delayed();
816 }
817
818 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
819 {
820 #ifdef CONFIG_TIMER_STATS
821 if (timer->start_site)
822 return;
823 timer->start_site = __builtin_return_address(0);
824 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
825 timer->start_pid = current->pid;
826 #endif
827 }
828
829 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
830 {
831 #ifdef CONFIG_TIMER_STATS
832 timer->start_site = NULL;
833 #endif
834 }
835
836 static 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);
843 #endif
844 }
845
846 /*
847 * Counterpart to lock_hrtimer_base above:
848 */
849 static inline
850 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
851 {
852 raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
853 }
854
855 /**
856 * hrtimer_forward - forward the timer expiry
857 * @timer: hrtimer to forward
858 * @now: forward past this time
859 * @interval: the interval to forward
860 *
861 * Forward the timer expiry so it will expire in the future.
862 * Returns the number of overruns.
863 */
864 u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
865 {
866 u64 orun = 1;
867 ktime_t delta;
868
869 delta = ktime_sub(now, hrtimer_get_expires(timer));
870
871 if (delta.tv64 < 0)
872 return 0;
873
874 if (interval.tv64 < timer->base->resolution.tv64)
875 interval.tv64 = timer->base->resolution.tv64;
876
877 if (unlikely(delta.tv64 >= interval.tv64)) {
878 s64 incr = ktime_to_ns(interval);
879
880 orun = ktime_divns(delta, incr);
881 hrtimer_add_expires_ns(timer, incr * orun);
882 if (hrtimer_get_expires_tv64(timer) > now.tv64)
883 return orun;
884 /*
885 * This (and the ktime_add() below) is the
886 * correction for exact:
887 */
888 orun++;
889 }
890 hrtimer_add_expires(timer, interval);
891
892 return orun;
893 }
894 EXPORT_SYMBOL_GPL(hrtimer_forward);
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.
901 *
902 * Returns 1 when the new timer is the leftmost timer in the tree.
903 */
904 static int enqueue_hrtimer(struct hrtimer *timer,
905 struct hrtimer_clock_base *base)
906 {
907 debug_activate(timer);
908
909 timerqueue_add(&base->active, &timer->node);
910 base->cpu_base->active_bases |= 1 << base->index;
911
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;
917
918 return (&timer->node == base->active.next);
919 }
920
921 /*
922 * __remove_hrtimer - internal function to remove a timer
923 *
924 * Caller must hold the base lock.
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)
930 */
931 static void __remove_hrtimer(struct hrtimer *timer,
932 struct hrtimer_clock_base *base,
933 unsigned long newstate, int reprogram)
934 {
935 struct timerqueue_node *next_timer;
936 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
937 goto out;
938
939 next_timer = timerqueue_getnext(&base->active);
940 timerqueue_del(&base->active, &timer->node);
941 if (&timer->node == next_timer) {
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);
951 }
952 #endif
953 }
954 if (!timerqueue_getnext(&base->active))
955 base->cpu_base->active_bases &= ~(1 << base->index);
956 out:
957 timer->state = newstate;
958 }
959
960 /*
961 * remove hrtimer, called with base lock held
962 */
963 static inline int
964 remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
965 {
966 if (hrtimer_is_queued(timer)) {
967 unsigned long state;
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 */
978 debug_deactivate(timer);
979 timer_stats_hrtimer_clear_start_info(timer);
980 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
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);
988 return 1;
989 }
990 return 0;
991 }
992
993 int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
994 unsigned long delta_ns, const enum hrtimer_mode mode,
995 int wakeup)
996 {
997 struct hrtimer_clock_base *base, *new_base;
998 unsigned long flags;
999 int ret, leftmost;
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
1006 base = lock_hrtimer_base(timer, &flags);
1007
1008 /* Remove an active timer from the queue: */
1009 ret = remove_hrtimer(timer, base);
1010
1011 if (mode & HRTIMER_MODE_REL) {
1012 tim = ktime_add_safe(tim, base->get_time());
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
1021 tim = ktime_add_safe(tim, base->resolution);
1022 #endif
1023 }
1024
1025 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
1026
1027 /* Switch the timer base, if necessary: */
1028 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
1029
1030 timer_stats_hrtimer_set_start_info(timer);
1031
1032 leftmost = enqueue_hrtimer(timer, new_base);
1033
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)
1037 *
1038 * XXX send_remote_softirq() ?
1039 */
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 }
1055
1056 unlock_hrtimer_base(timer, &flags);
1057
1058 return ret;
1059 }
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
1066 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1067 * relative (HRTIMER_MODE_REL)
1068 *
1069 * Returns:
1070 * 0 on success
1071 * 1 when the timer was active
1072 */
1073 int 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 }
1078 EXPORT_SYMBOL_GPL(hrtimer_start_range_ns);
1079
1080 /**
1081 * hrtimer_start - (re)start an hrtimer on the current CPU
1082 * @timer: the timer to be added
1083 * @tim: expiry time
1084 * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or
1085 * relative (HRTIMER_MODE_REL)
1086 *
1087 * Returns:
1088 * 0 on success
1089 * 1 when the timer was active
1090 */
1091 int
1092 hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
1093 {
1094 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1);
1095 }
1096 EXPORT_SYMBOL_GPL(hrtimer_start);
1097
1098
1099 /**
1100 * hrtimer_try_to_cancel - try to deactivate a timer
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
1107 * cannot be stopped
1108 */
1109 int hrtimer_try_to_cancel(struct hrtimer *timer)
1110 {
1111 struct hrtimer_clock_base *base;
1112 unsigned long flags;
1113 int ret = -1;
1114
1115 base = lock_hrtimer_base(timer, &flags);
1116
1117 if (!hrtimer_callback_running(timer))
1118 ret = remove_hrtimer(timer, base);
1119
1120 unlock_hrtimer_base(timer, &flags);
1121
1122 return ret;
1123
1124 }
1125 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
1126
1127 /**
1128 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
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 */
1135 int hrtimer_cancel(struct hrtimer *timer)
1136 {
1137 for (;;) {
1138 int ret = hrtimer_try_to_cancel(timer);
1139
1140 if (ret >= 0)
1141 return ret;
1142 cpu_relax();
1143 }
1144 }
1145 EXPORT_SYMBOL_GPL(hrtimer_cancel);
1146
1147 /**
1148 * hrtimer_get_remaining - get remaining time for the timer
1149 * @timer: the timer to read
1150 */
1151 ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1152 {
1153 unsigned long flags;
1154 ktime_t rem;
1155
1156 lock_hrtimer_base(timer, &flags);
1157 rem = hrtimer_expires_remaining(timer);
1158 unlock_hrtimer_base(timer, &flags);
1159
1160 return rem;
1161 }
1162 EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
1163
1164 #ifdef CONFIG_NO_HZ_COMMON
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 */
1171 ktime_t hrtimer_get_next_event(void)
1172 {
1173 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1174 struct hrtimer_clock_base *base = cpu_base->clock_base;
1175 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1176 unsigned long flags;
1177 int i;
1178
1179 raw_spin_lock_irqsave(&cpu_base->lock, flags);
1180
1181 if (!hrtimer_hres_active()) {
1182 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1183 struct hrtimer *timer;
1184 struct timerqueue_node *next;
1185
1186 next = timerqueue_getnext(&base->active);
1187 if (!next)
1188 continue;
1189
1190 timer = container_of(next, struct hrtimer, node);
1191 delta.tv64 = hrtimer_get_expires_tv64(timer);
1192 delta = ktime_sub(delta, base->get_time());
1193 if (delta.tv64 < mindelta.tv64)
1194 mindelta.tv64 = delta.tv64;
1195 }
1196 }
1197
1198 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
1199
1200 if (mindelta.tv64 < 0)
1201 mindelta.tv64 = 0;
1202 return mindelta;
1203 }
1204 #endif
1205
1206 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1207 enum hrtimer_mode mode)
1208 {
1209 struct hrtimer_cpu_base *cpu_base;
1210 int base;
1211
1212 memset(timer, 0, sizeof(struct hrtimer));
1213
1214 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1215
1216 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
1217 clock_id = CLOCK_MONOTONIC;
1218
1219 base = hrtimer_clockid_to_base(clock_id);
1220 timer->base = &cpu_base->clock_base[base];
1221 timerqueue_init(&timer->node);
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
1228 }
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 */
1236 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1237 enum hrtimer_mode mode)
1238 {
1239 debug_init(timer, clock_id, mode);
1240 __hrtimer_init(timer, clock_id, mode);
1241 }
1242 EXPORT_SYMBOL_GPL(hrtimer_init);
1243
1244 /**
1245 * hrtimer_get_res - get the timer resolution for a clock
1246 * @which_clock: which clock to query
1247 * @tp: pointer to timespec variable to store the resolution
1248 *
1249 * Store the resolution of the clock selected by @which_clock in the
1250 * variable pointed to by @tp.
1251 */
1252 int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1253 {
1254 struct hrtimer_cpu_base *cpu_base;
1255 int base = hrtimer_clockid_to_base(which_clock);
1256
1257 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1258 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
1259
1260 return 0;
1261 }
1262 EXPORT_SYMBOL_GPL(hrtimer_get_res);
1263
1264 #ifdef MTK_HRTIME_DEBUG
1265 static 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
1289 static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
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
1296 WARN_ON(!irqs_disabled());
1297
1298 debug_deactivate(timer);
1299 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1300 timer_stats_account_hrtimer(timer);
1301 fn = timer->function;
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 */
1308 raw_spin_unlock(&cpu_base->lock);
1309 trace_hrtimer_expire_entry(timer, now);
1310
1311 mt_trace_hrt_start(fn);
1312 restart = fn(timer);
1313 mt_trace_hrt_end(fn);
1314 trace_hrtimer_expire_exit(timer);
1315 raw_spin_lock(&cpu_base->lock);
1316
1317 /*
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()
1321 */
1322 if (restart != HRTIMER_NORESTART) {
1323 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1324 enqueue_hrtimer(timer, base);
1325 }
1326
1327 WARN_ON_ONCE(!(timer->state & HRTIMER_STATE_CALLBACK));
1328
1329 timer->state &= ~HRTIMER_STATE_CALLBACK;
1330 }
1331
1332 #ifdef CONFIG_HIGH_RES_TIMERS
1333
1334 /*
1335 * High resolution timer interrupt
1336 * Called with interrupts disabled
1337 */
1338 void hrtimer_interrupt(struct clock_event_device *dev)
1339 {
1340 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1341 ktime_t expires_next, now, entry_time, delta;
1342 int i, retries = 0;
1343
1344 BUG_ON(!cpu_base->hres_active);
1345 cpu_base->nr_events++;
1346 dev->next_event.tv64 = KTIME_MAX;
1347
1348 raw_spin_lock(&cpu_base->lock);
1349 entry_time = now = hrtimer_update_base(cpu_base);
1350 retry:
1351 expires_next.tv64 = KTIME_MAX;
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
1361 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1362 struct hrtimer_clock_base *base;
1363 struct timerqueue_node *node;
1364 ktime_t basenow;
1365
1366 if (!(cpu_base->active_bases & (1 << i)))
1367 continue;
1368
1369 base = cpu_base->clock_base + i;
1370 basenow = ktime_add(now, base->offset);
1371
1372 while ((node = timerqueue_getnext(&base->active))) {
1373 struct hrtimer *timer;
1374
1375 timer = container_of(node, struct hrtimer, node);
1376
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)) {
1391 ktime_t expires;
1392
1393 expires = ktime_sub(hrtimer_get_expires(timer),
1394 base->offset);
1395 if (expires.tv64 < 0)
1396 expires.tv64 = KTIME_MAX;
1397 if (expires.tv64 < expires_next.tv64)
1398 expires_next = expires;
1399 break;
1400 }
1401
1402 __run_hrtimer(timer, &basenow);
1403 }
1404 }
1405
1406 /*
1407 * Store the new expiry value so the migration code can verify
1408 * against it.
1409 */
1410 cpu_base->expires_next = expires_next;
1411 raw_spin_unlock(&cpu_base->lock);
1412
1413 /* Reprogramming necessary ? */
1414 if (expires_next.tv64 == KTIME_MAX ||
1415 !tick_program_event(expires_next, 0)) {
1416 cpu_base->hang_detected = 0;
1417 return;
1418 }
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.
1429 *
1430 * Acquire base lock for updating the offsets and retrieving
1431 * the current time.
1432 */
1433 raw_spin_lock(&cpu_base->lock);
1434 now = hrtimer_update_base(cpu_base);
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;
1446 raw_spin_unlock(&cpu_base->lock);
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));
1461 }
1462
1463 /*
1464 * local version of hrtimer_peek_ahead_timers() called with interrupts
1465 * disabled.
1466 */
1467 static 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
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 */
1488 void hrtimer_peek_ahead_timers(void)
1489 {
1490 unsigned long flags;
1491
1492 local_irq_save(flags);
1493 __hrtimer_peek_ahead_timers();
1494 local_irq_restore(flags);
1495 }
1496
1497 static void run_hrtimer_softirq(struct softirq_action *h)
1498 {
1499 hrtimer_peek_ahead_timers();
1500 }
1501
1502 #else /* CONFIG_HIGH_RES_TIMERS */
1503
1504 static inline void __hrtimer_peek_ahead_timers(void) { }
1505
1506 #endif /* !CONFIG_HIGH_RES_TIMERS */
1507
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 */
1515 void hrtimer_run_pending(void)
1516 {
1517 if (hrtimer_hres_active())
1518 return;
1519
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();
1530 }
1531
1532 /*
1533 * Called from hardirq context every jiffy
1534 */
1535 void hrtimer_run_queues(void)
1536 {
1537 struct timerqueue_node *node;
1538 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1539 struct hrtimer_clock_base *base;
1540 int index, gettime = 1;
1541
1542 if (hrtimer_hres_active())
1543 return;
1544
1545 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1546 base = &cpu_base->clock_base[index];
1547 if (!timerqueue_getnext(&base->active))
1548 continue;
1549
1550 if (gettime) {
1551 hrtimer_get_softirq_time(cpu_base);
1552 gettime = 0;
1553 }
1554
1555 raw_spin_lock(&cpu_base->lock);
1556
1557 while ((node = timerqueue_getnext(&base->active))) {
1558 struct hrtimer *timer;
1559
1560 timer = container_of(node, struct hrtimer, node);
1561 if (base->softirq_time.tv64 <=
1562 hrtimer_get_expires_tv64(timer))
1563 break;
1564
1565 __run_hrtimer(timer, &base->softirq_time);
1566 }
1567 raw_spin_unlock(&cpu_base->lock);
1568 }
1569 }
1570
1571 /*
1572 * Sleep related functions:
1573 */
1574 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
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
1587 void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1588 {
1589 sl->timer.function = hrtimer_wakeup;
1590 sl->task = task;
1591 }
1592 EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
1593
1594 static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
1595 {
1596 hrtimer_init_sleeper(t, current);
1597
1598 do {
1599 set_current_state(TASK_INTERRUPTIBLE);
1600 hrtimer_start_expires(&t->timer, mode);
1601 if (!hrtimer_active(&t->timer))
1602 t->task = NULL;
1603
1604 if (likely(t->task))
1605 freezable_schedule();
1606
1607 hrtimer_cancel(&t->timer);
1608 mode = HRTIMER_MODE_ABS;
1609
1610 } while (t->task && !signal_pending(current));
1611
1612 __set_current_state(TASK_RUNNING);
1613
1614 return t->task == NULL;
1615 }
1616
1617 static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1618 {
1619 struct timespec rmt;
1620 ktime_t rem;
1621
1622 rem = hrtimer_expires_remaining(timer);
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
1633 long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
1634 {
1635 struct hrtimer_sleeper t;
1636 struct timespec __user *rmtp;
1637 int ret = 0;
1638
1639 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
1640 HRTIMER_MODE_ABS);
1641 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
1642
1643 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
1644 goto out;
1645
1646 rmtp = restart->nanosleep.rmtp;
1647 if (rmtp) {
1648 ret = update_rmtp(&t.timer, rmtp);
1649 if (ret <= 0)
1650 goto out;
1651 }
1652
1653 /* The other values in restart are already filled in */
1654 ret = -ERESTART_RESTARTBLOCK;
1655 out:
1656 destroy_hrtimer_on_stack(&t.timer);
1657 return ret;
1658 }
1659
1660 long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
1661 const enum hrtimer_mode mode, const clockid_t clockid)
1662 {
1663 struct restart_block *restart;
1664 struct hrtimer_sleeper t;
1665 int ret = 0;
1666 unsigned long slack;
1667
1668 slack = current->timer_slack_ns;
1669 if (rt_task(current))
1670 slack = 0;
1671
1672 hrtimer_init_on_stack(&t.timer, clockid, mode);
1673 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
1674 if (do_nanosleep(&t, mode))
1675 goto out;
1676
1677 /* Absolute timers do not update the rmtp value and restart: */
1678 if (mode == HRTIMER_MODE_ABS) {
1679 ret = -ERESTARTNOHAND;
1680 goto out;
1681 }
1682
1683 if (rmtp) {
1684 ret = update_rmtp(&t.timer, rmtp);
1685 if (ret <= 0)
1686 goto out;
1687 }
1688
1689 restart = &current_thread_info()->restart_block;
1690 restart->fn = hrtimer_nanosleep_restart;
1691 restart->nanosleep.clockid = t.timer.base->clockid;
1692 restart->nanosleep.rmtp = rmtp;
1693 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
1694
1695 ret = -ERESTART_RESTARTBLOCK;
1696 out:
1697 destroy_hrtimer_on_stack(&t.timer);
1698 return ret;
1699 }
1700
1701 SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
1702 struct timespec __user *, rmtp)
1703 {
1704 struct timespec tu;
1705
1706 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1707 return -EFAULT;
1708
1709 if (!timespec_valid(&tu))
1710 return -EINVAL;
1711
1712 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1713 }
1714
1715 /*
1716 * Functions related to boot-time initialization:
1717 */
1718 static void __cpuinit init_hrtimers_cpu(int cpu)
1719 {
1720 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
1721 int i;
1722
1723 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1724 cpu_base->clock_base[i].cpu_base = cpu_base;
1725 timerqueue_init_head(&cpu_base->clock_base[i].active);
1726 }
1727
1728 hrtimer_init_hres(cpu_base);
1729 }
1730
1731 #ifdef CONFIG_HOTPLUG_CPU
1732
1733 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1734 struct hrtimer_clock_base *new_base)
1735 {
1736 struct hrtimer *timer;
1737 struct timerqueue_node *node;
1738
1739 while ((node = timerqueue_getnext(&old_base->active))) {
1740 timer = container_of(node, struct hrtimer, node);
1741 BUG_ON(hrtimer_callback_running(timer));
1742 debug_deactivate(timer);
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);
1750 timer->base = new_base;
1751 /*
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.
1758 */
1759 enqueue_hrtimer(timer, new_base);
1760
1761 /* Clear the migration state bit */
1762 timer->state &= ~HRTIMER_STATE_MIGRATE;
1763 }
1764 }
1765
1766 static void migrate_hrtimers(int scpu)
1767 {
1768 struct hrtimer_cpu_base *old_base, *new_base;
1769 int i;
1770
1771 BUG_ON(cpu_online(scpu));
1772 tick_cancel_sched_timer(scpu);
1773
1774 local_irq_disable();
1775 old_base = &per_cpu(hrtimer_bases, scpu);
1776 new_base = &__get_cpu_var(hrtimer_bases);
1777 /*
1778 * The caller is globally serialized and nobody else
1779 * takes two locks at once, deadlock is not possible.
1780 */
1781 raw_spin_lock(&new_base->lock);
1782 raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1783
1784 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1785 migrate_hrtimer_list(&old_base->clock_base[i],
1786 &new_base->clock_base[i]);
1787 }
1788
1789 raw_spin_unlock(&old_base->lock);
1790 raw_spin_unlock(&new_base->lock);
1791
1792 /* Check, if we got expired work to do */
1793 __hrtimer_peek_ahead_timers();
1794 local_irq_enable();
1795 }
1796
1797 #endif /* CONFIG_HOTPLUG_CPU */
1798
1799 static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
1800 unsigned long action, void *hcpu)
1801 {
1802 int scpu = (long)hcpu;
1803
1804 switch (action) {
1805
1806 case CPU_UP_PREPARE:
1807 case CPU_UP_PREPARE_FROZEN:
1808 init_hrtimers_cpu(scpu);
1809 break;
1810
1811 #ifdef CONFIG_HOTPLUG_CPU
1812 case CPU_DYING:
1813 case CPU_DYING_FROZEN:
1814 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu);
1815 break;
1816 case CPU_DEAD:
1817 case CPU_DEAD_FROZEN:
1818 {
1819 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu);
1820 migrate_hrtimers(scpu);
1821 break;
1822 }
1823 #endif
1824
1825 default:
1826 break;
1827 }
1828
1829 return NOTIFY_OK;
1830 }
1831
1832 static struct notifier_block __cpuinitdata hrtimers_nb = {
1833 .notifier_call = hrtimer_cpu_notify,
1834 };
1835
1836 void __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);
1841 #ifdef CONFIG_HIGH_RES_TIMERS
1842 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
1843 #endif
1844 }
1845
1846 /**
1847 * schedule_hrtimeout_range_clock - sleep until timeout
1848 * @expires: timeout value (ktime_t)
1849 * @delta: slack in expires timeout (ktime_t)
1850 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL
1851 * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME
1852 */
1853 int __sched
1854 schedule_hrtimeout_range_clock(ktime_t *expires, unsigned long delta,
1855 const enum hrtimer_mode mode, int clock)
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 /*
1869 * A NULL parameter means "infinite"
1870 */
1871 if (!expires) {
1872 schedule();
1873 __set_current_state(TASK_RUNNING);
1874 return -EINTR;
1875 }
1876
1877 hrtimer_init_on_stack(&t.timer, clock, mode);
1878 hrtimer_set_expires_range_ns(&t.timer, *expires, delta);
1879
1880 hrtimer_init_sleeper(&t, current);
1881
1882 hrtimer_start_expires(&t.timer, mode);
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 }
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 */
1925 int __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 }
1931 EXPORT_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 */
1955 int __sched schedule_hrtimeout(ktime_t *expires,
1956 const enum hrtimer_mode mode)
1957 {
1958 return schedule_hrtimeout_range(expires, 0, mode);
1959 }
1960 EXPORT_SYMBOL_GPL(schedule_hrtimeout);