4 * Kernel internal timers, kernel timekeeping, basic process system calls
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
10 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
11 * "A Kernel Model for Precision Timekeeping" by Dave Mills
12 * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
13 * serialize accesses to xtime/lost_ticks).
14 * Copyright (C) 1998 Andrea Arcangeli
15 * 1999-03-10 Improved NTP compatibility by Ulrich Windl
16 * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love
17 * 2000-10-05 Implemented scalable SMP per-CPU timer handling.
18 * Copyright (C) 2000, 2001, 2002 Ingo Molnar
19 * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
22 #include <linux/kernel_stat.h>
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/percpu.h>
26 #include <linux/init.h>
28 #include <linux/swap.h>
29 #include <linux/notifier.h>
30 #include <linux/thread_info.h>
31 #include <linux/time.h>
32 #include <linux/jiffies.h>
33 #include <linux/posix-timers.h>
34 #include <linux/cpu.h>
35 #include <linux/syscalls.h>
36 #include <linux/delay.h>
38 #include <asm/uaccess.h>
39 #include <asm/unistd.h>
40 #include <asm/div64.h>
41 #include <asm/timex.h>
44 #ifdef CONFIG_TIME_INTERPOLATION
45 static void time_interpolator_update(long delta_nsec
);
47 #define time_interpolator_update(x)
50 u64 jiffies_64 __cacheline_aligned_in_smp
= INITIAL_JIFFIES
;
52 EXPORT_SYMBOL(jiffies_64
);
55 * per-CPU timer vector definitions:
57 #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
58 #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
59 #define TVN_SIZE (1 << TVN_BITS)
60 #define TVR_SIZE (1 << TVR_BITS)
61 #define TVN_MASK (TVN_SIZE - 1)
62 #define TVR_MASK (TVR_SIZE - 1)
64 typedef struct tvec_s
{
65 struct list_head vec
[TVN_SIZE
];
68 typedef struct tvec_root_s
{
69 struct list_head vec
[TVR_SIZE
];
72 struct tvec_t_base_s
{
74 struct timer_list
*running_timer
;
75 unsigned long timer_jiffies
;
81 } ____cacheline_aligned_in_smp
;
83 typedef struct tvec_t_base_s tvec_base_t
;
85 tvec_base_t boot_tvec_bases
;
86 EXPORT_SYMBOL(boot_tvec_bases
);
87 static DEFINE_PER_CPU(tvec_base_t
*, tvec_bases
) = { &boot_tvec_bases
};
89 static inline void set_running_timer(tvec_base_t
*base
,
90 struct timer_list
*timer
)
93 base
->running_timer
= timer
;
97 static void internal_add_timer(tvec_base_t
*base
, struct timer_list
*timer
)
99 unsigned long expires
= timer
->expires
;
100 unsigned long idx
= expires
- base
->timer_jiffies
;
101 struct list_head
*vec
;
103 if (idx
< TVR_SIZE
) {
104 int i
= expires
& TVR_MASK
;
105 vec
= base
->tv1
.vec
+ i
;
106 } else if (idx
< 1 << (TVR_BITS
+ TVN_BITS
)) {
107 int i
= (expires
>> TVR_BITS
) & TVN_MASK
;
108 vec
= base
->tv2
.vec
+ i
;
109 } else if (idx
< 1 << (TVR_BITS
+ 2 * TVN_BITS
)) {
110 int i
= (expires
>> (TVR_BITS
+ TVN_BITS
)) & TVN_MASK
;
111 vec
= base
->tv3
.vec
+ i
;
112 } else if (idx
< 1 << (TVR_BITS
+ 3 * TVN_BITS
)) {
113 int i
= (expires
>> (TVR_BITS
+ 2 * TVN_BITS
)) & TVN_MASK
;
114 vec
= base
->tv4
.vec
+ i
;
115 } else if ((signed long) idx
< 0) {
117 * Can happen if you add a timer with expires == jiffies,
118 * or you set a timer to go off in the past
120 vec
= base
->tv1
.vec
+ (base
->timer_jiffies
& TVR_MASK
);
123 /* If the timeout is larger than 0xffffffff on 64-bit
124 * architectures then we use the maximum timeout:
126 if (idx
> 0xffffffffUL
) {
128 expires
= idx
+ base
->timer_jiffies
;
130 i
= (expires
>> (TVR_BITS
+ 3 * TVN_BITS
)) & TVN_MASK
;
131 vec
= base
->tv5
.vec
+ i
;
136 list_add_tail(&timer
->entry
, vec
);
140 * init_timer - initialize a timer.
141 * @timer: the timer to be initialized
143 * init_timer() must be done to a timer prior calling *any* of the
144 * other timer functions.
146 void fastcall
init_timer(struct timer_list
*timer
)
148 timer
->entry
.next
= NULL
;
149 timer
->base
= __raw_get_cpu_var(tvec_bases
);
151 EXPORT_SYMBOL(init_timer
);
153 static inline void detach_timer(struct timer_list
*timer
,
156 struct list_head
*entry
= &timer
->entry
;
158 __list_del(entry
->prev
, entry
->next
);
161 entry
->prev
= LIST_POISON2
;
165 * We are using hashed locking: holding per_cpu(tvec_bases).lock
166 * means that all timers which are tied to this base via timer->base are
167 * locked, and the base itself is locked too.
169 * So __run_timers/migrate_timers can safely modify all timers which could
170 * be found on ->tvX lists.
172 * When the timer's base is locked, and the timer removed from list, it is
173 * possible to set timer->base = NULL and drop the lock: the timer remains
176 static tvec_base_t
*lock_timer_base(struct timer_list
*timer
,
177 unsigned long *flags
)
183 if (likely(base
!= NULL
)) {
184 spin_lock_irqsave(&base
->lock
, *flags
);
185 if (likely(base
== timer
->base
))
187 /* The timer has migrated to another CPU */
188 spin_unlock_irqrestore(&base
->lock
, *flags
);
194 int __mod_timer(struct timer_list
*timer
, unsigned long expires
)
196 tvec_base_t
*base
, *new_base
;
200 BUG_ON(!timer
->function
);
202 base
= lock_timer_base(timer
, &flags
);
204 if (timer_pending(timer
)) {
205 detach_timer(timer
, 0);
209 new_base
= __get_cpu_var(tvec_bases
);
211 if (base
!= new_base
) {
213 * We are trying to schedule the timer on the local CPU.
214 * However we can't change timer's base while it is running,
215 * otherwise del_timer_sync() can't detect that the timer's
216 * handler yet has not finished. This also guarantees that
217 * the timer is serialized wrt itself.
219 if (likely(base
->running_timer
!= timer
)) {
220 /* See the comment in lock_timer_base() */
222 spin_unlock(&base
->lock
);
224 spin_lock(&base
->lock
);
229 timer
->expires
= expires
;
230 internal_add_timer(base
, timer
);
231 spin_unlock_irqrestore(&base
->lock
, flags
);
236 EXPORT_SYMBOL(__mod_timer
);
239 * add_timer_on - start a timer on a particular CPU
240 * @timer: the timer to be added
241 * @cpu: the CPU to start it on
243 * This is not very scalable on SMP. Double adds are not possible.
245 void add_timer_on(struct timer_list
*timer
, int cpu
)
247 tvec_base_t
*base
= per_cpu(tvec_bases
, cpu
);
250 BUG_ON(timer_pending(timer
) || !timer
->function
);
251 spin_lock_irqsave(&base
->lock
, flags
);
253 internal_add_timer(base
, timer
);
254 spin_unlock_irqrestore(&base
->lock
, flags
);
259 * mod_timer - modify a timer's timeout
260 * @timer: the timer to be modified
262 * mod_timer is a more efficient way to update the expire field of an
263 * active timer (if the timer is inactive it will be activated)
265 * mod_timer(timer, expires) is equivalent to:
267 * del_timer(timer); timer->expires = expires; add_timer(timer);
269 * Note that if there are multiple unserialized concurrent users of the
270 * same timer, then mod_timer() is the only safe way to modify the timeout,
271 * since add_timer() cannot modify an already running timer.
273 * The function returns whether it has modified a pending timer or not.
274 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
275 * active timer returns 1.)
277 int mod_timer(struct timer_list
*timer
, unsigned long expires
)
279 BUG_ON(!timer
->function
);
282 * This is a common optimization triggered by the
283 * networking code - if the timer is re-modified
284 * to be the same thing then just return:
286 if (timer
->expires
== expires
&& timer_pending(timer
))
289 return __mod_timer(timer
, expires
);
292 EXPORT_SYMBOL(mod_timer
);
295 * del_timer - deactive a timer.
296 * @timer: the timer to be deactivated
298 * del_timer() deactivates a timer - this works on both active and inactive
301 * The function returns whether it has deactivated a pending timer or not.
302 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
303 * active timer returns 1.)
305 int del_timer(struct timer_list
*timer
)
311 if (timer_pending(timer
)) {
312 base
= lock_timer_base(timer
, &flags
);
313 if (timer_pending(timer
)) {
314 detach_timer(timer
, 1);
317 spin_unlock_irqrestore(&base
->lock
, flags
);
323 EXPORT_SYMBOL(del_timer
);
327 * This function tries to deactivate a timer. Upon successful (ret >= 0)
328 * exit the timer is not queued and the handler is not running on any CPU.
330 * It must not be called from interrupt contexts.
332 int try_to_del_timer_sync(struct timer_list
*timer
)
338 base
= lock_timer_base(timer
, &flags
);
340 if (base
->running_timer
== timer
)
344 if (timer_pending(timer
)) {
345 detach_timer(timer
, 1);
349 spin_unlock_irqrestore(&base
->lock
, flags
);
355 * del_timer_sync - deactivate a timer and wait for the handler to finish.
356 * @timer: the timer to be deactivated
358 * This function only differs from del_timer() on SMP: besides deactivating
359 * the timer it also makes sure the handler has finished executing on other
362 * Synchronization rules: callers must prevent restarting of the timer,
363 * otherwise this function is meaningless. It must not be called from
364 * interrupt contexts. The caller must not hold locks which would prevent
365 * completion of the timer's handler. The timer's handler must not call
366 * add_timer_on(). Upon exit the timer is not queued and the handler is
367 * not running on any CPU.
369 * The function returns whether it has deactivated a pending timer or not.
371 int del_timer_sync(struct timer_list
*timer
)
374 int ret
= try_to_del_timer_sync(timer
);
380 EXPORT_SYMBOL(del_timer_sync
);
383 static int cascade(tvec_base_t
*base
, tvec_t
*tv
, int index
)
385 /* cascade all the timers from tv up one level */
386 struct timer_list
*timer
, *tmp
;
387 struct list_head tv_list
;
389 list_replace_init(tv
->vec
+ index
, &tv_list
);
392 * We are removing _all_ timers from the list, so we
393 * don't have to detach them individually.
395 list_for_each_entry_safe(timer
, tmp
, &tv_list
, entry
) {
396 BUG_ON(timer
->base
!= base
);
397 internal_add_timer(base
, timer
);
404 * __run_timers - run all expired timers (if any) on this CPU.
405 * @base: the timer vector to be processed.
407 * This function cascades all vectors and executes all expired timer
410 #define INDEX(N) (base->timer_jiffies >> (TVR_BITS + N * TVN_BITS)) & TVN_MASK
412 static inline void __run_timers(tvec_base_t
*base
)
414 struct timer_list
*timer
;
416 spin_lock_irq(&base
->lock
);
417 while (time_after_eq(jiffies
, base
->timer_jiffies
)) {
418 struct list_head work_list
;
419 struct list_head
*head
= &work_list
;
420 int index
= base
->timer_jiffies
& TVR_MASK
;
426 (!cascade(base
, &base
->tv2
, INDEX(0))) &&
427 (!cascade(base
, &base
->tv3
, INDEX(1))) &&
428 !cascade(base
, &base
->tv4
, INDEX(2)))
429 cascade(base
, &base
->tv5
, INDEX(3));
430 ++base
->timer_jiffies
;
431 list_replace_init(base
->tv1
.vec
+ index
, &work_list
);
432 while (!list_empty(head
)) {
433 void (*fn
)(unsigned long);
436 timer
= list_entry(head
->next
,struct timer_list
,entry
);
437 fn
= timer
->function
;
440 set_running_timer(base
, timer
);
441 detach_timer(timer
, 1);
442 spin_unlock_irq(&base
->lock
);
444 int preempt_count
= preempt_count();
446 if (preempt_count
!= preempt_count()) {
447 printk(KERN_WARNING
"huh, entered %p "
448 "with preempt_count %08x, exited"
455 spin_lock_irq(&base
->lock
);
458 set_running_timer(base
, NULL
);
459 spin_unlock_irq(&base
->lock
);
462 #ifdef CONFIG_NO_IDLE_HZ
464 * Find out when the next timer event is due to happen. This
465 * is used on S/390 to stop all activity when a cpus is idle.
466 * This functions needs to be called disabled.
468 unsigned long next_timer_interrupt(void)
471 struct list_head
*list
;
472 struct timer_list
*nte
;
473 unsigned long expires
;
474 unsigned long hr_expires
= MAX_JIFFY_OFFSET
;
479 hr_delta
= hrtimer_get_next_event();
480 if (hr_delta
.tv64
!= KTIME_MAX
) {
481 struct timespec tsdelta
;
482 tsdelta
= ktime_to_timespec(hr_delta
);
483 hr_expires
= timespec_to_jiffies(&tsdelta
);
485 return hr_expires
+ jiffies
;
487 hr_expires
+= jiffies
;
489 base
= __get_cpu_var(tvec_bases
);
490 spin_lock(&base
->lock
);
491 expires
= base
->timer_jiffies
+ (LONG_MAX
>> 1);
494 /* Look for timer events in tv1. */
495 j
= base
->timer_jiffies
& TVR_MASK
;
497 list_for_each_entry(nte
, base
->tv1
.vec
+ j
, entry
) {
498 expires
= nte
->expires
;
499 if (j
< (base
->timer_jiffies
& TVR_MASK
))
500 list
= base
->tv2
.vec
+ (INDEX(0));
503 j
= (j
+ 1) & TVR_MASK
;
504 } while (j
!= (base
->timer_jiffies
& TVR_MASK
));
507 varray
[0] = &base
->tv2
;
508 varray
[1] = &base
->tv3
;
509 varray
[2] = &base
->tv4
;
510 varray
[3] = &base
->tv5
;
511 for (i
= 0; i
< 4; i
++) {
514 if (list_empty(varray
[i
]->vec
+ j
)) {
515 j
= (j
+ 1) & TVN_MASK
;
518 list_for_each_entry(nte
, varray
[i
]->vec
+ j
, entry
)
519 if (time_before(nte
->expires
, expires
))
520 expires
= nte
->expires
;
521 if (j
< (INDEX(i
)) && i
< 3)
522 list
= varray
[i
+ 1]->vec
+ (INDEX(i
+ 1));
524 } while (j
!= (INDEX(i
)));
529 * The search wrapped. We need to look at the next list
530 * from next tv element that would cascade into tv element
531 * where we found the timer element.
533 list_for_each_entry(nte
, list
, entry
) {
534 if (time_before(nte
->expires
, expires
))
535 expires
= nte
->expires
;
538 spin_unlock(&base
->lock
);
541 * It can happen that other CPUs service timer IRQs and increment
542 * jiffies, but we have not yet got a local timer tick to process
543 * the timer wheels. In that case, the expiry time can be before
544 * jiffies, but since the high-resolution timer here is relative to
545 * jiffies, the default expression when high-resolution timers are
548 * time_before(MAX_JIFFY_OFFSET + jiffies, expires)
550 * would falsely evaluate to true. If that is the case, just
551 * return jiffies so that we can immediately fire the local timer
553 if (time_before(expires
, jiffies
))
556 if (time_before(hr_expires
, expires
))
563 /******************************************************************/
566 * Timekeeping variables
568 unsigned long tick_usec
= TICK_USEC
; /* USER_HZ period (usec) */
569 unsigned long tick_nsec
= TICK_NSEC
; /* ACTHZ period (nsec) */
573 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
574 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
575 * at zero at system boot time, so wall_to_monotonic will be negative,
576 * however, we will ALWAYS keep the tv_nsec part positive so we can use
577 * the usual normalization.
579 struct timespec xtime
__attribute__ ((aligned (16)));
580 struct timespec wall_to_monotonic
__attribute__ ((aligned (16)));
582 EXPORT_SYMBOL(xtime
);
584 /* Don't completely fail for HZ > 500. */
585 int tickadj
= 500/HZ
? : 1; /* microsecs */
589 * phase-lock loop variables
591 /* TIME_ERROR prevents overwriting the CMOS clock */
592 int time_state
= TIME_OK
; /* clock synchronization status */
593 int time_status
= STA_UNSYNC
; /* clock status bits */
594 long time_offset
; /* time adjustment (us) */
595 long time_constant
= 2; /* pll time constant */
596 long time_tolerance
= MAXFREQ
; /* frequency tolerance (ppm) */
597 long time_precision
= 1; /* clock precision (us) */
598 long time_maxerror
= NTP_PHASE_LIMIT
; /* maximum error (us) */
599 long time_esterror
= NTP_PHASE_LIMIT
; /* estimated error (us) */
600 long time_freq
= (((NSEC_PER_SEC
+ HZ
/2) % HZ
- HZ
/2) << SHIFT_USEC
) / NSEC_PER_USEC
;
601 /* frequency offset (scaled ppm)*/
602 static long time_adj
; /* tick adjust (scaled 1 / HZ) */
603 long time_reftime
; /* time at last adjustment (s) */
605 long time_next_adjust
;
608 * this routine handles the overflow of the microsecond field
610 * The tricky bits of code to handle the accurate clock support
611 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
612 * They were originally developed for SUN and DEC kernels.
613 * All the kudos should go to Dave for this stuff.
616 static void second_overflow(void)
620 /* Bump the maxerror field */
621 time_maxerror
+= time_tolerance
>> SHIFT_USEC
;
622 if (time_maxerror
> NTP_PHASE_LIMIT
) {
623 time_maxerror
= NTP_PHASE_LIMIT
;
624 time_status
|= STA_UNSYNC
;
628 * Leap second processing. If in leap-insert state at the end of the
629 * day, the system clock is set back one second; if in leap-delete
630 * state, the system clock is set ahead one second. The microtime()
631 * routine or external clock driver will insure that reported time is
632 * always monotonic. The ugly divides should be replaced.
634 switch (time_state
) {
636 if (time_status
& STA_INS
)
637 time_state
= TIME_INS
;
638 else if (time_status
& STA_DEL
)
639 time_state
= TIME_DEL
;
642 if (xtime
.tv_sec
% 86400 == 0) {
644 wall_to_monotonic
.tv_sec
++;
646 * The timer interpolator will make time change
647 * gradually instead of an immediate jump by one second
649 time_interpolator_update(-NSEC_PER_SEC
);
650 time_state
= TIME_OOP
;
652 printk(KERN_NOTICE
"Clock: inserting leap second "
657 if ((xtime
.tv_sec
+ 1) % 86400 == 0) {
659 wall_to_monotonic
.tv_sec
--;
661 * Use of time interpolator for a gradual change of
664 time_interpolator_update(NSEC_PER_SEC
);
665 time_state
= TIME_WAIT
;
667 printk(KERN_NOTICE
"Clock: deleting leap second "
672 time_state
= TIME_WAIT
;
675 if (!(time_status
& (STA_INS
| STA_DEL
)))
676 time_state
= TIME_OK
;
680 * Compute the phase adjustment for the next second. In PLL mode, the
681 * offset is reduced by a fixed factor times the time constant. In FLL
682 * mode the offset is used directly. In either mode, the maximum phase
683 * adjustment for each second is clamped so as to spread the adjustment
684 * over not more than the number of seconds between updates.
687 if (!(time_status
& STA_FLL
))
688 ltemp
= shift_right(ltemp
, SHIFT_KG
+ time_constant
);
689 ltemp
= min(ltemp
, (MAXPHASE
/ MINSEC
) << SHIFT_UPDATE
);
690 ltemp
= max(ltemp
, -(MAXPHASE
/ MINSEC
) << SHIFT_UPDATE
);
691 time_offset
-= ltemp
;
692 time_adj
= ltemp
<< (SHIFT_SCALE
- SHIFT_HZ
- SHIFT_UPDATE
);
695 * Compute the frequency estimate and additional phase adjustment due
696 * to frequency error for the next second.
699 time_adj
+= shift_right(ltemp
,(SHIFT_USEC
+ SHIFT_HZ
- SHIFT_SCALE
));
703 * Compensate for (HZ==100) != (1 << SHIFT_HZ). Add 25% and 3.125% to
704 * get 128.125; => only 0.125% error (p. 14)
706 time_adj
+= shift_right(time_adj
, 2) + shift_right(time_adj
, 5);
710 * Compensate for (HZ==250) != (1 << SHIFT_HZ). Add 1.5625% and
711 * 0.78125% to get 255.85938; => only 0.05% error (p. 14)
713 time_adj
+= shift_right(time_adj
, 6) + shift_right(time_adj
, 7);
717 * Compensate for (HZ==1000) != (1 << SHIFT_HZ). Add 1.5625% and
718 * 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
720 time_adj
+= shift_right(time_adj
, 6) + shift_right(time_adj
, 7);
725 * Returns how many microseconds we need to add to xtime this tick
726 * in doing an adjustment requested with adjtime.
728 static long adjtime_adjustment(void)
730 long time_adjust_step
;
732 time_adjust_step
= time_adjust
;
733 if (time_adjust_step
) {
735 * We are doing an adjtime thing. Prepare time_adjust_step to
736 * be within bounds. Note that a positive time_adjust means we
737 * want the clock to run faster.
739 * Limit the amount of the step to be in the range
740 * -tickadj .. +tickadj
742 time_adjust_step
= min(time_adjust_step
, (long)tickadj
);
743 time_adjust_step
= max(time_adjust_step
, (long)-tickadj
);
745 return time_adjust_step
;
748 /* in the NTP reference this is called "hardclock()" */
749 static void update_ntp_one_tick(void)
751 long time_adjust_step
;
753 time_adjust_step
= adjtime_adjustment();
754 if (time_adjust_step
)
755 /* Reduce by this step the amount of time left */
756 time_adjust
-= time_adjust_step
;
758 /* Changes by adjtime() do not take effect till next tick. */
759 if (time_next_adjust
!= 0) {
760 time_adjust
= time_next_adjust
;
761 time_next_adjust
= 0;
766 * Return how long ticks are at the moment, that is, how much time
767 * update_wall_time_one_tick will add to xtime next time we call it
768 * (assuming no calls to do_adjtimex in the meantime).
769 * The return value is in fixed-point nanoseconds shifted by the
770 * specified number of bits to the right of the binary point.
771 * This function has no side-effects.
773 u64
current_tick_length(long shift
)
778 /* calculate the finest interval NTP will allow.
779 * ie: nanosecond value shifted by (SHIFT_SCALE - 10)
781 delta_nsec
= tick_nsec
+ adjtime_adjustment() * 1000;
782 ret
= ((u64
) delta_nsec
<< (SHIFT_SCALE
- 10)) + time_adj
;
784 /* convert from (SHIFT_SCALE - 10) to specified shift scale: */
785 shift
= shift
- (SHIFT_SCALE
- 10);
794 /* XXX - all of this timekeeping code should be later moved to time.c */
795 #include <linux/clocksource.h>
796 static struct clocksource
*clock
; /* pointer to current clocksource */
797 static cycle_t last_clock_cycle
; /* cycle value at last update_wall_time */
799 #ifdef CONFIG_GENERIC_TIME
801 * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook
803 * private function, must hold xtime_lock lock when being
804 * called. Returns the number of nanoseconds since the
805 * last call to update_wall_time() (adjusted by NTP scaling)
807 static inline s64
__get_nsec_offset(void)
809 cycle_t cycle_now
, cycle_delta
;
812 /* read clocksource: */
813 cycle_now
= read_clocksource(clock
);
815 /* calculate the delta since the last update_wall_time: */
816 cycle_delta
= (cycle_now
- last_clock_cycle
) & clock
->mask
;
818 /* convert to nanoseconds: */
819 ns_offset
= cyc2ns(clock
, cycle_delta
);
825 * __get_realtime_clock_ts - Returns the time of day in a timespec
826 * @ts: pointer to the timespec to be set
828 * Returns the time of day in a timespec. Used by
829 * do_gettimeofday() and get_realtime_clock_ts().
831 static inline void __get_realtime_clock_ts(struct timespec
*ts
)
837 seq
= read_seqbegin(&xtime_lock
);
840 nsecs
= __get_nsec_offset();
842 } while (read_seqretry(&xtime_lock
, seq
));
844 timespec_add_ns(ts
, nsecs
);
848 * get_realtime_clock_ts - Returns the time of day in a timespec
849 * @ts: pointer to the timespec to be set
851 * Returns the time of day in a timespec.
853 void getnstimeofday(struct timespec
*ts
)
855 __get_realtime_clock_ts(ts
);
858 EXPORT_SYMBOL(getnstimeofday
);
861 * do_gettimeofday - Returns the time of day in a timeval
862 * @tv: pointer to the timeval to be set
864 * NOTE: Users should be converted to using get_realtime_clock_ts()
866 void do_gettimeofday(struct timeval
*tv
)
870 __get_realtime_clock_ts(&now
);
871 tv
->tv_sec
= now
.tv_sec
;
872 tv
->tv_usec
= now
.tv_nsec
/1000;
875 EXPORT_SYMBOL(do_gettimeofday
);
877 * do_settimeofday - Sets the time of day
878 * @tv: pointer to the timespec variable containing the new time
880 * Sets the time of day to the new time and update NTP and notify hrtimers
882 int do_settimeofday(struct timespec
*tv
)
885 time_t wtm_sec
, sec
= tv
->tv_sec
;
886 long wtm_nsec
, nsec
= tv
->tv_nsec
;
888 if ((unsigned long)tv
->tv_nsec
>= NSEC_PER_SEC
)
891 write_seqlock_irqsave(&xtime_lock
, flags
);
893 nsec
-= __get_nsec_offset();
895 wtm_sec
= wall_to_monotonic
.tv_sec
+ (xtime
.tv_sec
- sec
);
896 wtm_nsec
= wall_to_monotonic
.tv_nsec
+ (xtime
.tv_nsec
- nsec
);
898 set_normalized_timespec(&xtime
, sec
, nsec
);
899 set_normalized_timespec(&wall_to_monotonic
, wtm_sec
, wtm_nsec
);
903 write_sequnlock_irqrestore(&xtime_lock
, flags
);
905 /* signal hrtimers about time change */
911 EXPORT_SYMBOL(do_settimeofday
);
914 * change_clocksource - Swaps clocksources if a new one is available
916 * Accumulates current time interval and initializes new clocksource
918 static int change_clocksource(void)
920 struct clocksource
*new;
923 new = get_next_clocksource();
925 now
= read_clocksource(new);
926 nsec
= __get_nsec_offset();
927 timespec_add_ns(&xtime
, nsec
);
930 last_clock_cycle
= now
;
931 printk(KERN_INFO
"Time: %s clocksource has been installed.\n",
934 } else if (clock
->update_callback
) {
935 return clock
->update_callback();
940 #define change_clocksource() (0)
944 * timeofday_is_continuous - check to see if timekeeping is free running
946 int timekeeping_is_continuous(void)
952 seq
= read_seqbegin(&xtime_lock
);
954 ret
= clock
->is_continuous
;
956 } while (read_seqretry(&xtime_lock
, seq
));
962 * timekeeping_init - Initializes the clocksource and common timekeeping values
964 void __init
timekeeping_init(void)
968 write_seqlock_irqsave(&xtime_lock
, flags
);
969 clock
= get_next_clocksource();
970 calculate_clocksource_interval(clock
, tick_nsec
);
971 last_clock_cycle
= read_clocksource(clock
);
973 write_sequnlock_irqrestore(&xtime_lock
, flags
);
978 * timekeeping_resume - Resumes the generic timekeeping subsystem.
981 * This is for the generic clocksource timekeeping.
982 * xtime/wall_to_monotonic/jiffies/wall_jiffies/etc are
983 * still managed by arch specific suspend/resume code.
985 static int timekeeping_resume(struct sys_device
*dev
)
989 write_seqlock_irqsave(&xtime_lock
, flags
);
990 /* restart the last cycle value */
991 last_clock_cycle
= read_clocksource(clock
);
992 write_sequnlock_irqrestore(&xtime_lock
, flags
);
996 /* sysfs resume/suspend bits for timekeeping */
997 static struct sysdev_class timekeeping_sysclass
= {
998 .resume
= timekeeping_resume
,
999 set_kset_name("timekeeping"),
1002 static struct sys_device device_timer
= {
1004 .cls
= &timekeeping_sysclass
,
1007 static int __init
timekeeping_init_device(void)
1009 int error
= sysdev_class_register(&timekeeping_sysclass
);
1011 error
= sysdev_register(&device_timer
);
1015 device_initcall(timekeeping_init_device
);
1018 * update_wall_time - Uses the current clocksource to increment the wall time
1020 * Called from the timer interrupt, must hold a write on xtime_lock.
1022 static void update_wall_time(void)
1024 static s64 remainder_snsecs
, error
;
1026 cycle_t now
, offset
;
1028 snsecs_per_sec
= (s64
)NSEC_PER_SEC
<< clock
->shift
;
1029 remainder_snsecs
+= (s64
)xtime
.tv_nsec
<< clock
->shift
;
1031 now
= read_clocksource(clock
);
1032 offset
= (now
- last_clock_cycle
)&clock
->mask
;
1034 /* normally this loop will run just once, however in the
1035 * case of lost or late ticks, it will accumulate correctly.
1037 while (offset
> clock
->interval_cycles
) {
1038 /* get the ntp interval in clock shifted nanoseconds */
1039 s64 ntp_snsecs
= current_tick_length(clock
->shift
);
1041 /* accumulate one interval */
1042 remainder_snsecs
+= clock
->interval_snsecs
;
1043 last_clock_cycle
+= clock
->interval_cycles
;
1044 offset
-= clock
->interval_cycles
;
1046 /* interpolator bits */
1047 time_interpolator_update(clock
->interval_snsecs
1049 /* increment the NTP state machine */
1050 update_ntp_one_tick();
1052 /* accumulate error between NTP and clock interval */
1053 error
+= (ntp_snsecs
- (s64
)clock
->interval_snsecs
);
1055 /* correct the clock when NTP error is too big */
1056 remainder_snsecs
+= make_ntp_adj(clock
, offset
, &error
);
1058 if (remainder_snsecs
>= snsecs_per_sec
) {
1059 remainder_snsecs
-= snsecs_per_sec
;
1064 /* store full nanoseconds into xtime */
1065 xtime
.tv_nsec
= remainder_snsecs
>> clock
->shift
;
1066 remainder_snsecs
-= (s64
)xtime
.tv_nsec
<< clock
->shift
;
1068 /* check to see if there is a new clocksource to use */
1069 if (change_clocksource()) {
1071 remainder_snsecs
= 0;
1072 calculate_clocksource_interval(clock
, tick_nsec
);
1077 * Called from the timer interrupt handler to charge one tick to the current
1078 * process. user_tick is 1 if the tick is user time, 0 for system.
1080 void update_process_times(int user_tick
)
1082 struct task_struct
*p
= current
;
1083 int cpu
= smp_processor_id();
1085 /* Note: this timer irq context must be accounted for as well. */
1087 account_user_time(p
, jiffies_to_cputime(1));
1089 account_system_time(p
, HARDIRQ_OFFSET
, jiffies_to_cputime(1));
1091 if (rcu_pending(cpu
))
1092 rcu_check_callbacks(cpu
, user_tick
);
1094 run_posix_cpu_timers(p
);
1098 * Nr of active tasks - counted in fixed-point numbers
1100 static unsigned long count_active_tasks(void)
1102 return nr_active() * FIXED_1
;
1106 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
1107 * imply that avenrun[] is the standard name for this kind of thing.
1108 * Nothing else seems to be standardized: the fractional size etc
1109 * all seem to differ on different machines.
1111 * Requires xtime_lock to access.
1113 unsigned long avenrun
[3];
1115 EXPORT_SYMBOL(avenrun
);
1118 * calc_load - given tick count, update the avenrun load estimates.
1119 * This is called while holding a write_lock on xtime_lock.
1121 static inline void calc_load(unsigned long ticks
)
1123 unsigned long active_tasks
; /* fixed-point */
1124 static int count
= LOAD_FREQ
;
1129 active_tasks
= count_active_tasks();
1130 CALC_LOAD(avenrun
[0], EXP_1
, active_tasks
);
1131 CALC_LOAD(avenrun
[1], EXP_5
, active_tasks
);
1132 CALC_LOAD(avenrun
[2], EXP_15
, active_tasks
);
1136 /* jiffies at the most recent update of wall time */
1137 unsigned long wall_jiffies
= INITIAL_JIFFIES
;
1140 * This read-write spinlock protects us from races in SMP while
1141 * playing with xtime and avenrun.
1143 #ifndef ARCH_HAVE_XTIME_LOCK
1144 seqlock_t xtime_lock __cacheline_aligned_in_smp
= SEQLOCK_UNLOCKED
;
1146 EXPORT_SYMBOL(xtime_lock
);
1150 * This function runs timers and the timer-tq in bottom half context.
1152 static void run_timer_softirq(struct softirq_action
*h
)
1154 tvec_base_t
*base
= __get_cpu_var(tvec_bases
);
1156 hrtimer_run_queues();
1157 if (time_after_eq(jiffies
, base
->timer_jiffies
))
1162 * Called by the local, per-CPU timer interrupt on SMP.
1164 void run_local_timers(void)
1166 raise_softirq(TIMER_SOFTIRQ
);
1171 * Called by the timer interrupt. xtime_lock must already be taken
1174 static inline void update_times(void)
1176 unsigned long ticks
;
1178 ticks
= jiffies
- wall_jiffies
;
1179 wall_jiffies
+= ticks
;
1185 * The 64-bit jiffies value is not atomic - you MUST NOT read it
1186 * without sampling the sequence number in xtime_lock.
1187 * jiffies is defined in the linker script...
1190 void do_timer(struct pt_regs
*regs
)
1193 /* prevent loading jiffies before storing new jiffies_64 value. */
1198 #ifdef __ARCH_WANT_SYS_ALARM
1201 * For backwards compatibility? This can be done in libc so Alpha
1202 * and all newer ports shouldn't need it.
1204 asmlinkage
unsigned long sys_alarm(unsigned int seconds
)
1206 return alarm_setitimer(seconds
);
1214 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
1215 * should be moved into arch/i386 instead?
1219 * sys_getpid - return the thread group id of the current process
1221 * Note, despite the name, this returns the tgid not the pid. The tgid and
1222 * the pid are identical unless CLONE_THREAD was specified on clone() in
1223 * which case the tgid is the same in all threads of the same group.
1225 * This is SMP safe as current->tgid does not change.
1227 asmlinkage
long sys_getpid(void)
1229 return current
->tgid
;
1233 * Accessing ->group_leader->real_parent is not SMP-safe, it could
1234 * change from under us. However, rather than getting any lock
1235 * we can use an optimistic algorithm: get the parent
1236 * pid, and go back and check that the parent is still
1237 * the same. If it has changed (which is extremely unlikely
1238 * indeed), we just try again..
1240 * NOTE! This depends on the fact that even if we _do_
1241 * get an old value of "parent", we can happily dereference
1242 * the pointer (it was and remains a dereferencable kernel pointer
1243 * no matter what): we just can't necessarily trust the result
1244 * until we know that the parent pointer is valid.
1246 * NOTE2: ->group_leader never changes from under us.
1248 asmlinkage
long sys_getppid(void)
1251 struct task_struct
*me
= current
;
1252 struct task_struct
*parent
;
1254 parent
= me
->group_leader
->real_parent
;
1257 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
1259 struct task_struct
*old
= parent
;
1262 * Make sure we read the pid before re-reading the
1266 parent
= me
->group_leader
->real_parent
;
1276 asmlinkage
long sys_getuid(void)
1278 /* Only we change this so SMP safe */
1279 return current
->uid
;
1282 asmlinkage
long sys_geteuid(void)
1284 /* Only we change this so SMP safe */
1285 return current
->euid
;
1288 asmlinkage
long sys_getgid(void)
1290 /* Only we change this so SMP safe */
1291 return current
->gid
;
1294 asmlinkage
long sys_getegid(void)
1296 /* Only we change this so SMP safe */
1297 return current
->egid
;
1302 static void process_timeout(unsigned long __data
)
1304 wake_up_process((task_t
*)__data
);
1308 * schedule_timeout - sleep until timeout
1309 * @timeout: timeout value in jiffies
1311 * Make the current task sleep until @timeout jiffies have
1312 * elapsed. The routine will return immediately unless
1313 * the current task state has been set (see set_current_state()).
1315 * You can set the task state as follows -
1317 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1318 * pass before the routine returns. The routine will return 0
1320 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1321 * delivered to the current task. In this case the remaining time
1322 * in jiffies will be returned, or 0 if the timer expired in time
1324 * The current task state is guaranteed to be TASK_RUNNING when this
1327 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1328 * the CPU away without a bound on the timeout. In this case the return
1329 * value will be %MAX_SCHEDULE_TIMEOUT.
1331 * In all cases the return value is guaranteed to be non-negative.
1333 fastcall
signed long __sched
schedule_timeout(signed long timeout
)
1335 struct timer_list timer
;
1336 unsigned long expire
;
1340 case MAX_SCHEDULE_TIMEOUT
:
1342 * These two special cases are useful to be comfortable
1343 * in the caller. Nothing more. We could take
1344 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1345 * but I' d like to return a valid offset (>=0) to allow
1346 * the caller to do everything it want with the retval.
1352 * Another bit of PARANOID. Note that the retval will be
1353 * 0 since no piece of kernel is supposed to do a check
1354 * for a negative retval of schedule_timeout() (since it
1355 * should never happens anyway). You just have the printk()
1356 * that will tell you if something is gone wrong and where.
1360 printk(KERN_ERR
"schedule_timeout: wrong timeout "
1361 "value %lx from %p\n", timeout
,
1362 __builtin_return_address(0));
1363 current
->state
= TASK_RUNNING
;
1368 expire
= timeout
+ jiffies
;
1370 setup_timer(&timer
, process_timeout
, (unsigned long)current
);
1371 __mod_timer(&timer
, expire
);
1373 del_singleshot_timer_sync(&timer
);
1375 timeout
= expire
- jiffies
;
1378 return timeout
< 0 ? 0 : timeout
;
1380 EXPORT_SYMBOL(schedule_timeout
);
1383 * We can use __set_current_state() here because schedule_timeout() calls
1384 * schedule() unconditionally.
1386 signed long __sched
schedule_timeout_interruptible(signed long timeout
)
1388 __set_current_state(TASK_INTERRUPTIBLE
);
1389 return schedule_timeout(timeout
);
1391 EXPORT_SYMBOL(schedule_timeout_interruptible
);
1393 signed long __sched
schedule_timeout_uninterruptible(signed long timeout
)
1395 __set_current_state(TASK_UNINTERRUPTIBLE
);
1396 return schedule_timeout(timeout
);
1398 EXPORT_SYMBOL(schedule_timeout_uninterruptible
);
1400 /* Thread ID - the internal kernel "pid" */
1401 asmlinkage
long sys_gettid(void)
1403 return current
->pid
;
1407 * sys_sysinfo - fill in sysinfo struct
1409 asmlinkage
long sys_sysinfo(struct sysinfo __user
*info
)
1412 unsigned long mem_total
, sav_total
;
1413 unsigned int mem_unit
, bitcount
;
1416 memset((char *)&val
, 0, sizeof(struct sysinfo
));
1420 seq
= read_seqbegin(&xtime_lock
);
1423 * This is annoying. The below is the same thing
1424 * posix_get_clock_monotonic() does, but it wants to
1425 * take the lock which we want to cover the loads stuff
1429 getnstimeofday(&tp
);
1430 tp
.tv_sec
+= wall_to_monotonic
.tv_sec
;
1431 tp
.tv_nsec
+= wall_to_monotonic
.tv_nsec
;
1432 if (tp
.tv_nsec
- NSEC_PER_SEC
>= 0) {
1433 tp
.tv_nsec
= tp
.tv_nsec
- NSEC_PER_SEC
;
1436 val
.uptime
= tp
.tv_sec
+ (tp
.tv_nsec
? 1 : 0);
1438 val
.loads
[0] = avenrun
[0] << (SI_LOAD_SHIFT
- FSHIFT
);
1439 val
.loads
[1] = avenrun
[1] << (SI_LOAD_SHIFT
- FSHIFT
);
1440 val
.loads
[2] = avenrun
[2] << (SI_LOAD_SHIFT
- FSHIFT
);
1442 val
.procs
= nr_threads
;
1443 } while (read_seqretry(&xtime_lock
, seq
));
1449 * If the sum of all the available memory (i.e. ram + swap)
1450 * is less than can be stored in a 32 bit unsigned long then
1451 * we can be binary compatible with 2.2.x kernels. If not,
1452 * well, in that case 2.2.x was broken anyways...
1454 * -Erik Andersen <andersee@debian.org>
1457 mem_total
= val
.totalram
+ val
.totalswap
;
1458 if (mem_total
< val
.totalram
|| mem_total
< val
.totalswap
)
1461 mem_unit
= val
.mem_unit
;
1462 while (mem_unit
> 1) {
1465 sav_total
= mem_total
;
1467 if (mem_total
< sav_total
)
1472 * If mem_total did not overflow, multiply all memory values by
1473 * val.mem_unit and set it to 1. This leaves things compatible
1474 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1479 val
.totalram
<<= bitcount
;
1480 val
.freeram
<<= bitcount
;
1481 val
.sharedram
<<= bitcount
;
1482 val
.bufferram
<<= bitcount
;
1483 val
.totalswap
<<= bitcount
;
1484 val
.freeswap
<<= bitcount
;
1485 val
.totalhigh
<<= bitcount
;
1486 val
.freehigh
<<= bitcount
;
1489 if (copy_to_user(info
, &val
, sizeof(struct sysinfo
)))
1495 static int __devinit
init_timers_cpu(int cpu
)
1499 static char __devinitdata tvec_base_done
[NR_CPUS
];
1501 if (!tvec_base_done
[cpu
]) {
1502 static char boot_done
;
1506 * The APs use this path later in boot
1508 base
= kmalloc_node(sizeof(*base
), GFP_KERNEL
,
1512 memset(base
, 0, sizeof(*base
));
1513 per_cpu(tvec_bases
, cpu
) = base
;
1516 * This is for the boot CPU - we use compile-time
1517 * static initialisation because per-cpu memory isn't
1518 * ready yet and because the memory allocators are not
1519 * initialised either.
1522 base
= &boot_tvec_bases
;
1524 tvec_base_done
[cpu
] = 1;
1526 base
= per_cpu(tvec_bases
, cpu
);
1529 spin_lock_init(&base
->lock
);
1530 for (j
= 0; j
< TVN_SIZE
; j
++) {
1531 INIT_LIST_HEAD(base
->tv5
.vec
+ j
);
1532 INIT_LIST_HEAD(base
->tv4
.vec
+ j
);
1533 INIT_LIST_HEAD(base
->tv3
.vec
+ j
);
1534 INIT_LIST_HEAD(base
->tv2
.vec
+ j
);
1536 for (j
= 0; j
< TVR_SIZE
; j
++)
1537 INIT_LIST_HEAD(base
->tv1
.vec
+ j
);
1539 base
->timer_jiffies
= jiffies
;
1543 #ifdef CONFIG_HOTPLUG_CPU
1544 static void migrate_timer_list(tvec_base_t
*new_base
, struct list_head
*head
)
1546 struct timer_list
*timer
;
1548 while (!list_empty(head
)) {
1549 timer
= list_entry(head
->next
, struct timer_list
, entry
);
1550 detach_timer(timer
, 0);
1551 timer
->base
= new_base
;
1552 internal_add_timer(new_base
, timer
);
1556 static void __devinit
migrate_timers(int cpu
)
1558 tvec_base_t
*old_base
;
1559 tvec_base_t
*new_base
;
1562 BUG_ON(cpu_online(cpu
));
1563 old_base
= per_cpu(tvec_bases
, cpu
);
1564 new_base
= get_cpu_var(tvec_bases
);
1566 local_irq_disable();
1567 spin_lock(&new_base
->lock
);
1568 spin_lock(&old_base
->lock
);
1570 BUG_ON(old_base
->running_timer
);
1572 for (i
= 0; i
< TVR_SIZE
; i
++)
1573 migrate_timer_list(new_base
, old_base
->tv1
.vec
+ i
);
1574 for (i
= 0; i
< TVN_SIZE
; i
++) {
1575 migrate_timer_list(new_base
, old_base
->tv2
.vec
+ i
);
1576 migrate_timer_list(new_base
, old_base
->tv3
.vec
+ i
);
1577 migrate_timer_list(new_base
, old_base
->tv4
.vec
+ i
);
1578 migrate_timer_list(new_base
, old_base
->tv5
.vec
+ i
);
1581 spin_unlock(&old_base
->lock
);
1582 spin_unlock(&new_base
->lock
);
1584 put_cpu_var(tvec_bases
);
1586 #endif /* CONFIG_HOTPLUG_CPU */
1588 static int timer_cpu_notify(struct notifier_block
*self
,
1589 unsigned long action
, void *hcpu
)
1591 long cpu
= (long)hcpu
;
1593 case CPU_UP_PREPARE
:
1594 if (init_timers_cpu(cpu
) < 0)
1597 #ifdef CONFIG_HOTPLUG_CPU
1599 migrate_timers(cpu
);
1608 static struct notifier_block timers_nb
= {
1609 .notifier_call
= timer_cpu_notify
,
1613 void __init
init_timers(void)
1615 timer_cpu_notify(&timers_nb
, (unsigned long)CPU_UP_PREPARE
,
1616 (void *)(long)smp_processor_id());
1617 register_cpu_notifier(&timers_nb
);
1618 open_softirq(TIMER_SOFTIRQ
, run_timer_softirq
, NULL
);
1621 #ifdef CONFIG_TIME_INTERPOLATION
1623 struct time_interpolator
*time_interpolator __read_mostly
;
1624 static struct time_interpolator
*time_interpolator_list __read_mostly
;
1625 static DEFINE_SPINLOCK(time_interpolator_lock
);
1627 static inline u64
time_interpolator_get_cycles(unsigned int src
)
1629 unsigned long (*x
)(void);
1633 case TIME_SOURCE_FUNCTION
:
1634 x
= time_interpolator
->addr
;
1637 case TIME_SOURCE_MMIO64
:
1638 return readq_relaxed((void __iomem
*)time_interpolator
->addr
);
1640 case TIME_SOURCE_MMIO32
:
1641 return readl_relaxed((void __iomem
*)time_interpolator
->addr
);
1643 default: return get_cycles();
1647 static inline u64
time_interpolator_get_counter(int writelock
)
1649 unsigned int src
= time_interpolator
->source
;
1651 if (time_interpolator
->jitter
)
1657 lcycle
= time_interpolator
->last_cycle
;
1658 now
= time_interpolator_get_cycles(src
);
1659 if (lcycle
&& time_after(lcycle
, now
))
1662 /* When holding the xtime write lock, there's no need
1663 * to add the overhead of the cmpxchg. Readers are
1664 * force to retry until the write lock is released.
1667 time_interpolator
->last_cycle
= now
;
1670 /* Keep track of the last timer value returned. The use of cmpxchg here
1671 * will cause contention in an SMP environment.
1673 } while (unlikely(cmpxchg(&time_interpolator
->last_cycle
, lcycle
, now
) != lcycle
));
1677 return time_interpolator_get_cycles(src
);
1680 void time_interpolator_reset(void)
1682 time_interpolator
->offset
= 0;
1683 time_interpolator
->last_counter
= time_interpolator_get_counter(1);
1686 #define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
1688 unsigned long time_interpolator_get_offset(void)
1690 /* If we do not have a time interpolator set up then just return zero */
1691 if (!time_interpolator
)
1694 return time_interpolator
->offset
+
1695 GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator
);
1698 #define INTERPOLATOR_ADJUST 65536
1699 #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
1701 static void time_interpolator_update(long delta_nsec
)
1704 unsigned long offset
;
1706 /* If there is no time interpolator set up then do nothing */
1707 if (!time_interpolator
)
1711 * The interpolator compensates for late ticks by accumulating the late
1712 * time in time_interpolator->offset. A tick earlier than expected will
1713 * lead to a reset of the offset and a corresponding jump of the clock
1714 * forward. Again this only works if the interpolator clock is running
1715 * slightly slower than the regular clock and the tuning logic insures
1719 counter
= time_interpolator_get_counter(1);
1720 offset
= time_interpolator
->offset
+
1721 GET_TI_NSECS(counter
, time_interpolator
);
1723 if (delta_nsec
< 0 || (unsigned long) delta_nsec
< offset
)
1724 time_interpolator
->offset
= offset
- delta_nsec
;
1726 time_interpolator
->skips
++;
1727 time_interpolator
->ns_skipped
+= delta_nsec
- offset
;
1728 time_interpolator
->offset
= 0;
1730 time_interpolator
->last_counter
= counter
;
1732 /* Tuning logic for time interpolator invoked every minute or so.
1733 * Decrease interpolator clock speed if no skips occurred and an offset is carried.
1734 * Increase interpolator clock speed if we skip too much time.
1736 if (jiffies
% INTERPOLATOR_ADJUST
== 0)
1738 if (time_interpolator
->skips
== 0 && time_interpolator
->offset
> tick_nsec
)
1739 time_interpolator
->nsec_per_cyc
--;
1740 if (time_interpolator
->ns_skipped
> INTERPOLATOR_MAX_SKIP
&& time_interpolator
->offset
== 0)
1741 time_interpolator
->nsec_per_cyc
++;
1742 time_interpolator
->skips
= 0;
1743 time_interpolator
->ns_skipped
= 0;
1748 is_better_time_interpolator(struct time_interpolator
*new)
1750 if (!time_interpolator
)
1752 return new->frequency
> 2*time_interpolator
->frequency
||
1753 (unsigned long)new->drift
< (unsigned long)time_interpolator
->drift
;
1757 register_time_interpolator(struct time_interpolator
*ti
)
1759 unsigned long flags
;
1762 BUG_ON(ti
->frequency
== 0 || ti
->mask
== 0);
1764 ti
->nsec_per_cyc
= ((u64
)NSEC_PER_SEC
<< ti
->shift
) / ti
->frequency
;
1765 spin_lock(&time_interpolator_lock
);
1766 write_seqlock_irqsave(&xtime_lock
, flags
);
1767 if (is_better_time_interpolator(ti
)) {
1768 time_interpolator
= ti
;
1769 time_interpolator_reset();
1771 write_sequnlock_irqrestore(&xtime_lock
, flags
);
1773 ti
->next
= time_interpolator_list
;
1774 time_interpolator_list
= ti
;
1775 spin_unlock(&time_interpolator_lock
);
1779 unregister_time_interpolator(struct time_interpolator
*ti
)
1781 struct time_interpolator
*curr
, **prev
;
1782 unsigned long flags
;
1784 spin_lock(&time_interpolator_lock
);
1785 prev
= &time_interpolator_list
;
1786 for (curr
= *prev
; curr
; curr
= curr
->next
) {
1794 write_seqlock_irqsave(&xtime_lock
, flags
);
1795 if (ti
== time_interpolator
) {
1796 /* we lost the best time-interpolator: */
1797 time_interpolator
= NULL
;
1798 /* find the next-best interpolator */
1799 for (curr
= time_interpolator_list
; curr
; curr
= curr
->next
)
1800 if (is_better_time_interpolator(curr
))
1801 time_interpolator
= curr
;
1802 time_interpolator_reset();
1804 write_sequnlock_irqrestore(&xtime_lock
, flags
);
1805 spin_unlock(&time_interpolator_lock
);
1807 #endif /* CONFIG_TIME_INTERPOLATION */
1810 * msleep - sleep safely even with waitqueue interruptions
1811 * @msecs: Time in milliseconds to sleep for
1813 void msleep(unsigned int msecs
)
1815 unsigned long timeout
= msecs_to_jiffies(msecs
) + 1;
1818 timeout
= schedule_timeout_uninterruptible(timeout
);
1821 EXPORT_SYMBOL(msleep
);
1824 * msleep_interruptible - sleep waiting for signals
1825 * @msecs: Time in milliseconds to sleep for
1827 unsigned long msleep_interruptible(unsigned int msecs
)
1829 unsigned long timeout
= msecs_to_jiffies(msecs
) + 1;
1831 while (timeout
&& !signal_pending(current
))
1832 timeout
= schedule_timeout_interruptible(timeout
);
1833 return jiffies_to_msecs(timeout
);
1836 EXPORT_SYMBOL(msleep_interruptible
);