init from v2.6.32.60
[mach-moxart.git] / kernel / time / timekeeping.c
blob3d35af308f6bb4ba7c461ee97429525fc1d8cbc6
1 /*
2 * linux/kernel/time/timekeeping.c
4 * Kernel timekeeping code and accessor functions
6 * This code was moved from linux/kernel/timer.c.
7 * Please see that file for copyright and history logs.
9 */
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/percpu.h>
14 #include <linux/init.h>
15 #include <linux/mm.h>
16 #include <linux/sched.h>
17 #include <linux/sysdev.h>
18 #include <linux/clocksource.h>
19 #include <linux/jiffies.h>
20 #include <linux/time.h>
21 #include <linux/tick.h>
22 #include <linux/stop_machine.h>
24 /* Structure holding internal timekeeping values. */
25 struct timekeeper {
26 /* Current clocksource used for timekeeping. */
27 struct clocksource *clock;
28 /* The shift value of the current clocksource. */
29 int shift;
31 /* Number of clock cycles in one NTP interval. */
32 cycle_t cycle_interval;
33 /* Number of clock shifted nano seconds in one NTP interval. */
34 u64 xtime_interval;
35 /* shifted nano seconds left over when rounding cycle_interval */
36 s64 xtime_remainder;
37 /* Raw nano seconds accumulated per NTP interval. */
38 u32 raw_interval;
40 /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
41 u64 xtime_nsec;
42 /* Difference between accumulated time and NTP time in ntp
43 * shifted nano seconds. */
44 s64 ntp_error;
45 /* Shift conversion between clock shifted nano seconds and
46 * ntp shifted nano seconds. */
47 int ntp_error_shift;
48 /* NTP adjusted clock multiplier */
49 u32 mult;
52 struct timekeeper timekeeper;
54 /**
55 * timekeeper_setup_internals - Set up internals to use clocksource clock.
57 * @clock: Pointer to clocksource.
59 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
60 * pair and interval request.
62 * Unless you're the timekeeping code, you should not be using this!
64 static void timekeeper_setup_internals(struct clocksource *clock)
66 cycle_t interval;
67 u64 tmp, ntpinterval;
69 timekeeper.clock = clock;
70 clock->cycle_last = clock->read(clock);
72 /* Do the ns -> cycle conversion first, using original mult */
73 tmp = NTP_INTERVAL_LENGTH;
74 tmp <<= clock->shift;
75 ntpinterval = tmp;
76 tmp += clock->mult/2;
77 do_div(tmp, clock->mult);
78 if (tmp == 0)
79 tmp = 1;
81 interval = (cycle_t) tmp;
82 timekeeper.cycle_interval = interval;
84 /* Go back from cycles -> shifted ns */
85 timekeeper.xtime_interval = (u64) interval * clock->mult;
86 timekeeper.xtime_remainder = ntpinterval - timekeeper.xtime_interval;
87 timekeeper.raw_interval =
88 ((u64) interval * clock->mult) >> clock->shift;
90 timekeeper.xtime_nsec = 0;
91 timekeeper.shift = clock->shift;
93 timekeeper.ntp_error = 0;
94 timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
97 * The timekeeper keeps its own mult values for the currently
98 * active clocksource. These value will be adjusted via NTP
99 * to counteract clock drifting.
101 timekeeper.mult = clock->mult;
104 /* Timekeeper helper functions. */
105 static inline s64 timekeeping_get_ns(void)
107 cycle_t cycle_now, cycle_delta;
108 struct clocksource *clock;
110 /* read clocksource: */
111 clock = timekeeper.clock;
112 cycle_now = clock->read(clock);
114 /* calculate the delta since the last update_wall_time: */
115 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
117 /* return delta convert to nanoseconds using ntp adjusted mult. */
118 return clocksource_cyc2ns(cycle_delta, timekeeper.mult,
119 timekeeper.shift);
122 static inline s64 timekeeping_get_ns_raw(void)
124 cycle_t cycle_now, cycle_delta;
125 struct clocksource *clock;
127 /* read clocksource: */
128 clock = timekeeper.clock;
129 cycle_now = clock->read(clock);
131 /* calculate the delta since the last update_wall_time: */
132 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
134 /* return delta convert to nanoseconds using ntp adjusted mult. */
135 return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
139 * This read-write spinlock protects us from races in SMP while
140 * playing with xtime.
142 __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
146 * The current time
147 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
148 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
149 * at zero at system boot time, so wall_to_monotonic will be negative,
150 * however, we will ALWAYS keep the tv_nsec part positive so we can use
151 * the usual normalization.
153 * wall_to_monotonic is moved after resume from suspend for the monotonic
154 * time not to jump. We need to add total_sleep_time to wall_to_monotonic
155 * to get the real boot based time offset.
157 * - wall_to_monotonic is no longer the boot time, getboottime must be
158 * used instead.
160 struct timespec xtime __attribute__ ((aligned (16)));
161 struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
162 static struct timespec total_sleep_time;
164 /* Offset clock monotonic -> clock realtime */
165 static ktime_t offs_real;
167 /* Offset clock monotonic -> clock boottime */
168 static ktime_t offs_boot;
171 * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
173 struct timespec raw_time;
175 /* must hold write on xtime_lock */
176 static void update_rt_offset(void)
178 struct timespec tmp, *wtm = &wall_to_monotonic;
180 set_normalized_timespec(&tmp, -wtm->tv_sec, -wtm->tv_nsec);
181 offs_real = timespec_to_ktime(tmp);
184 /* must hold write on xtime_lock */
185 static void timekeeping_update(bool clearntp)
187 if (clearntp) {
188 timekeeper.ntp_error = 0;
189 ntp_clear();
191 update_rt_offset();
192 update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult);
197 /* flag for if timekeeping is suspended */
198 int __read_mostly timekeeping_suspended;
200 static struct timespec xtime_cache __attribute__ ((aligned (16)));
201 void update_xtime_cache(u64 nsec)
204 * Use temporary variable so get_seconds() cannot catch
205 * an intermediate xtime_cache.tv_sec value.
206 * The ACCESS_ONCE() keeps the compiler from optimizing
207 * out the intermediate value.
209 struct timespec ts = xtime;
210 timespec_add_ns(&ts, nsec);
211 ACCESS_ONCE(xtime_cache) = ts;
214 #ifdef CONFIG_GENERIC_TIME
217 * timekeeping_forward_now - update clock to the current time
219 * Forward the current clock to update its state since the last call to
220 * update_wall_time(). This is useful before significant clock changes,
221 * as it avoids having to deal with this time offset explicitly.
223 static void timekeeping_forward_now(void)
225 cycle_t cycle_now, cycle_delta;
226 struct clocksource *clock;
227 s64 nsec;
229 clock = timekeeper.clock;
230 cycle_now = clock->read(clock);
231 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
232 clock->cycle_last = cycle_now;
234 nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
235 timekeeper.shift);
237 /* If arch requires, add in gettimeoffset() */
238 nsec += arch_gettimeoffset();
240 timespec_add_ns(&xtime, nsec);
242 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
243 timespec_add_ns(&raw_time, nsec);
247 * getnstimeofday - Returns the time of day in a timespec
248 * @ts: pointer to the timespec to be set
250 * Returns the time of day in a timespec.
252 void getnstimeofday(struct timespec *ts)
254 unsigned long seq;
255 s64 nsecs;
257 WARN_ON(timekeeping_suspended);
259 do {
260 seq = read_seqbegin(&xtime_lock);
262 *ts = xtime;
263 nsecs = timekeeping_get_ns();
265 /* If arch requires, add in gettimeoffset() */
266 nsecs += arch_gettimeoffset();
268 } while (read_seqretry(&xtime_lock, seq));
270 timespec_add_ns(ts, nsecs);
273 EXPORT_SYMBOL(getnstimeofday);
275 ktime_t ktime_get(void)
277 unsigned int seq;
278 s64 secs, nsecs;
280 WARN_ON(timekeeping_suspended);
282 do {
283 seq = read_seqbegin(&xtime_lock);
284 secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
285 nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
286 nsecs += timekeeping_get_ns();
287 /* If arch requires, add in gettimeoffset() */
288 nsecs += arch_gettimeoffset();
290 } while (read_seqretry(&xtime_lock, seq));
292 * Use ktime_set/ktime_add_ns to create a proper ktime on
293 * 32-bit architectures without CONFIG_KTIME_SCALAR.
295 return ktime_add_ns(ktime_set(secs, 0), nsecs);
297 EXPORT_SYMBOL_GPL(ktime_get);
300 * ktime_get_ts - get the monotonic clock in timespec format
301 * @ts: pointer to timespec variable
303 * The function calculates the monotonic clock from the realtime
304 * clock and the wall_to_monotonic offset and stores the result
305 * in normalized timespec format in the variable pointed to by @ts.
307 void ktime_get_ts(struct timespec *ts)
309 struct timespec tomono;
310 unsigned int seq;
311 s64 nsecs;
313 WARN_ON(timekeeping_suspended);
315 do {
316 seq = read_seqbegin(&xtime_lock);
317 *ts = xtime;
318 tomono = wall_to_monotonic;
319 nsecs = timekeeping_get_ns();
320 /* If arch requires, add in gettimeoffset() */
321 nsecs += arch_gettimeoffset();
323 } while (read_seqretry(&xtime_lock, seq));
325 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
326 ts->tv_nsec + tomono.tv_nsec + nsecs);
328 EXPORT_SYMBOL_GPL(ktime_get_ts);
331 * do_gettimeofday - Returns the time of day in a timeval
332 * @tv: pointer to the timeval to be set
334 * NOTE: Users should be converted to using getnstimeofday()
336 void do_gettimeofday(struct timeval *tv)
338 struct timespec now;
340 getnstimeofday(&now);
341 tv->tv_sec = now.tv_sec;
342 tv->tv_usec = now.tv_nsec/1000;
345 EXPORT_SYMBOL(do_gettimeofday);
347 * do_settimeofday - Sets the time of day
348 * @tv: pointer to the timespec variable containing the new time
350 * Sets the time of day to the new time and update NTP and notify hrtimers
352 int do_settimeofday(struct timespec *tv)
354 struct timespec ts_delta;
355 unsigned long flags;
357 if (!timespec_valid_strict(tv))
358 return -EINVAL;
360 write_seqlock_irqsave(&xtime_lock, flags);
362 timekeeping_forward_now();
364 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
365 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
366 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
368 xtime = *tv;
370 update_xtime_cache(0);
372 timekeeping_update(true);
374 write_sequnlock_irqrestore(&xtime_lock, flags);
376 /* signal hrtimers about time change */
377 clock_was_set();
379 return 0;
382 EXPORT_SYMBOL(do_settimeofday);
385 * change_clocksource - Swaps clocksources if a new one is available
387 * Accumulates current time interval and initializes new clocksource
389 static int change_clocksource(void *data)
391 struct clocksource *new, *old;
393 new = (struct clocksource *) data;
395 timekeeping_forward_now();
396 if (!new->enable || new->enable(new) == 0) {
397 old = timekeeper.clock;
398 timekeeper_setup_internals(new);
399 if (old->disable)
400 old->disable(old);
402 return 0;
406 * timekeeping_notify - Install a new clock source
407 * @clock: pointer to the clock source
409 * This function is called from clocksource.c after a new, better clock
410 * source has been registered. The caller holds the clocksource_mutex.
412 void timekeeping_notify(struct clocksource *clock)
414 if (timekeeper.clock == clock)
415 return;
416 stop_machine(change_clocksource, clock, NULL);
417 tick_clock_notify();
420 #else /* GENERIC_TIME */
422 static inline void timekeeping_forward_now(void) { }
425 * ktime_get - get the monotonic time in ktime_t format
427 * returns the time in ktime_t format
429 ktime_t ktime_get(void)
431 struct timespec now;
433 ktime_get_ts(&now);
435 return timespec_to_ktime(now);
437 EXPORT_SYMBOL_GPL(ktime_get);
440 * ktime_get_ts - get the monotonic clock in timespec format
441 * @ts: pointer to timespec variable
443 * The function calculates the monotonic clock from the realtime
444 * clock and the wall_to_monotonic offset and stores the result
445 * in normalized timespec format in the variable pointed to by @ts.
447 void ktime_get_ts(struct timespec *ts)
449 struct timespec tomono;
450 unsigned long seq;
452 do {
453 seq = read_seqbegin(&xtime_lock);
454 getnstimeofday(ts);
455 tomono = wall_to_monotonic;
457 } while (read_seqretry(&xtime_lock, seq));
459 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
460 ts->tv_nsec + tomono.tv_nsec);
462 EXPORT_SYMBOL_GPL(ktime_get_ts);
464 #endif /* !GENERIC_TIME */
467 * ktime_get_real - get the real (wall-) time in ktime_t format
469 * returns the time in ktime_t format
471 ktime_t ktime_get_real(void)
473 struct timespec now;
475 getnstimeofday(&now);
477 return timespec_to_ktime(now);
479 EXPORT_SYMBOL_GPL(ktime_get_real);
482 * getrawmonotonic - Returns the raw monotonic time in a timespec
483 * @ts: pointer to the timespec to be set
485 * Returns the raw monotonic time (completely un-modified by ntp)
487 void getrawmonotonic(struct timespec *ts)
489 unsigned long seq;
490 s64 nsecs;
492 do {
493 seq = read_seqbegin(&xtime_lock);
494 nsecs = timekeeping_get_ns_raw();
495 *ts = raw_time;
497 } while (read_seqretry(&xtime_lock, seq));
499 timespec_add_ns(ts, nsecs);
501 EXPORT_SYMBOL(getrawmonotonic);
505 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
507 int timekeeping_valid_for_hres(void)
509 unsigned long seq;
510 int ret;
512 do {
513 seq = read_seqbegin(&xtime_lock);
515 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
517 } while (read_seqretry(&xtime_lock, seq));
519 return ret;
523 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
525 * Caller must observe xtime_lock via read_seqbegin/read_seqretry to
526 * ensure that the clocksource does not change!
528 u64 timekeeping_max_deferment(void)
530 return timekeeper.clock->max_idle_ns;
534 * read_persistent_clock - Return time from the persistent clock.
536 * Weak dummy function for arches that do not yet support it.
537 * Reads the time from the battery backed persistent clock.
538 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
540 * XXX - Do be sure to remove it once all arches implement it.
542 void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
544 ts->tv_sec = 0;
545 ts->tv_nsec = 0;
549 * read_boot_clock - Return time of the system start.
551 * Weak dummy function for arches that do not yet support it.
552 * Function to read the exact time the system has been started.
553 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
555 * XXX - Do be sure to remove it once all arches implement it.
557 void __attribute__((weak)) read_boot_clock(struct timespec *ts)
559 ts->tv_sec = 0;
560 ts->tv_nsec = 0;
564 * timekeeping_init - Initializes the clocksource and common timekeeping values
566 void __init timekeeping_init(void)
568 struct clocksource *clock;
569 unsigned long flags;
570 struct timespec now, boot;
572 read_persistent_clock(&now);
573 if (!timespec_valid_strict(&now)) {
574 printk("WARNING: Persistent clock returned invalid value!\n"
575 " Check your CMOS/BIOS settings.\n");
576 now.tv_sec = 0;
577 now.tv_nsec = 0;
580 read_boot_clock(&boot);
581 if (!timespec_valid_strict(&boot)) {
582 printk("WARNING: Boot clock returned invalid value!\n"
583 " Check your CMOS/BIOS settings.\n");
584 boot.tv_sec = 0;
585 boot.tv_nsec = 0;
588 write_seqlock_irqsave(&xtime_lock, flags);
590 ntp_init();
592 clock = clocksource_default_clock();
593 if (clock->enable)
594 clock->enable(clock);
595 timekeeper_setup_internals(clock);
597 xtime.tv_sec = now.tv_sec;
598 xtime.tv_nsec = now.tv_nsec;
599 raw_time.tv_sec = 0;
600 raw_time.tv_nsec = 0;
601 if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
602 boot.tv_sec = xtime.tv_sec;
603 boot.tv_nsec = xtime.tv_nsec;
605 set_normalized_timespec(&wall_to_monotonic,
606 -boot.tv_sec, -boot.tv_nsec);
607 update_xtime_cache(0);
608 update_rt_offset();
609 total_sleep_time.tv_sec = 0;
610 total_sleep_time.tv_nsec = 0;
611 write_sequnlock_irqrestore(&xtime_lock, flags);
614 /* time in seconds when suspend began */
615 static struct timespec timekeeping_suspend_time;
617 static void update_sleep_time(struct timespec t)
619 total_sleep_time = t;
620 offs_boot = timespec_to_ktime(t);
624 * timekeeping_resume - Resumes the generic timekeeping subsystem.
625 * @dev: unused
627 * This is for the generic clocksource timekeeping.
628 * xtime/wall_to_monotonic/jiffies/etc are
629 * still managed by arch specific suspend/resume code.
631 static int timekeeping_resume(struct sys_device *dev)
633 unsigned long flags;
634 struct timespec ts;
636 read_persistent_clock(&ts);
638 clocksource_resume();
640 write_seqlock_irqsave(&xtime_lock, flags);
642 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
643 ts = timespec_sub(ts, timekeeping_suspend_time);
644 xtime = timespec_add_safe(xtime, ts);
645 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts);
646 update_sleep_time(timespec_add_safe(total_sleep_time, ts));
648 update_xtime_cache(0);
649 /* re-base the last cycle value */
650 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
651 timekeeper.ntp_error = 0;
652 timekeeping_suspended = 0;
653 timekeeping_update(false);
654 write_sequnlock_irqrestore(&xtime_lock, flags);
656 touch_softlockup_watchdog();
658 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
660 /* Resume hrtimers */
661 hres_timers_resume();
663 return 0;
666 static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
668 unsigned long flags;
670 read_persistent_clock(&timekeeping_suspend_time);
672 write_seqlock_irqsave(&xtime_lock, flags);
673 timekeeping_forward_now();
674 timekeeping_suspended = 1;
675 write_sequnlock_irqrestore(&xtime_lock, flags);
677 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
679 return 0;
682 /* sysfs resume/suspend bits for timekeeping */
683 static struct sysdev_class timekeeping_sysclass = {
684 .name = "timekeeping",
685 .resume = timekeeping_resume,
686 .suspend = timekeeping_suspend,
689 static struct sys_device device_timer = {
690 .id = 0,
691 .cls = &timekeeping_sysclass,
694 static int __init timekeeping_init_device(void)
696 int error = sysdev_class_register(&timekeeping_sysclass);
697 if (!error)
698 error = sysdev_register(&device_timer);
699 return error;
702 device_initcall(timekeeping_init_device);
705 * If the error is already larger, we look ahead even further
706 * to compensate for late or lost adjustments.
708 static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
709 s64 *offset)
711 s64 tick_error, i;
712 u32 look_ahead, adj;
713 s32 error2, mult;
716 * Use the current error value to determine how much to look ahead.
717 * The larger the error the slower we adjust for it to avoid problems
718 * with losing too many ticks, otherwise we would overadjust and
719 * produce an even larger error. The smaller the adjustment the
720 * faster we try to adjust for it, as lost ticks can do less harm
721 * here. This is tuned so that an error of about 1 msec is adjusted
722 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
724 error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
725 error2 = abs(error2);
726 for (look_ahead = 0; error2 > 0; look_ahead++)
727 error2 >>= 2;
730 * Now calculate the error in (1 << look_ahead) ticks, but first
731 * remove the single look ahead already included in the error.
733 tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
734 tick_error -= timekeeper.xtime_interval >> 1;
735 error = ((error - tick_error) >> look_ahead) + tick_error;
737 /* Finally calculate the adjustment shift value. */
738 i = *interval;
739 mult = 1;
740 if (error < 0) {
741 error = -error;
742 *interval = -*interval;
743 *offset = -*offset;
744 mult = -1;
746 for (adj = 0; error > i; adj++)
747 error >>= 1;
749 *interval <<= adj;
750 *offset <<= adj;
751 return mult << adj;
755 * Adjust the multiplier to reduce the error value,
756 * this is optimized for the most common adjustments of -1,0,1,
757 * for other values we can do a bit more work.
759 static void timekeeping_adjust(s64 offset)
761 s64 error, interval = timekeeper.cycle_interval;
762 int adj;
764 error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
765 if (error > interval) {
766 error >>= 2;
767 if (likely(error <= interval))
768 adj = 1;
769 else
770 adj = timekeeping_bigadjust(error, &interval, &offset);
771 } else if (error < -interval) {
772 error >>= 2;
773 if (likely(error >= -interval)) {
774 adj = -1;
775 interval = -interval;
776 offset = -offset;
777 } else
778 adj = timekeeping_bigadjust(error, &interval, &offset);
779 } else
780 return;
782 timekeeper.mult += adj;
783 timekeeper.xtime_interval += interval;
784 timekeeper.xtime_nsec -= offset;
785 timekeeper.ntp_error -= (interval - offset) <<
786 timekeeper.ntp_error_shift;
790 * update_wall_time - Uses the current clocksource to increment the wall time
792 * Called from the timer interrupt, must hold a write on xtime_lock.
794 void update_wall_time(void)
796 struct clocksource *clock;
797 cycle_t offset;
798 u64 nsecs;
800 /* Make sure we're fully resumed: */
801 if (unlikely(timekeeping_suspended))
802 return;
804 clock = timekeeper.clock;
805 #ifdef CONFIG_GENERIC_TIME
806 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
807 #else
808 offset = timekeeper.cycle_interval;
809 #endif
810 /* Check if there's really nothing to do */
811 if (offset < timekeeper.cycle_interval)
812 return;
814 timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
816 /* normally this loop will run just once, however in the
817 * case of lost or late ticks, it will accumulate correctly.
819 while (offset >= timekeeper.cycle_interval) {
820 u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
822 /* accumulate one interval */
823 offset -= timekeeper.cycle_interval;
824 clock->cycle_last += timekeeper.cycle_interval;
826 timekeeper.xtime_nsec += timekeeper.xtime_interval;
827 if (timekeeper.xtime_nsec >= nsecps) {
828 int leap;
829 timekeeper.xtime_nsec -= nsecps;
830 xtime.tv_sec++;
831 leap = second_overflow(xtime.tv_sec);
832 xtime.tv_sec += leap;
833 wall_to_monotonic.tv_sec -= leap;
834 if (leap)
835 clock_was_set_delayed();
838 raw_time.tv_nsec += timekeeper.raw_interval;
839 if (raw_time.tv_nsec >= NSEC_PER_SEC) {
840 raw_time.tv_nsec -= NSEC_PER_SEC;
841 raw_time.tv_sec++;
844 /* accumulate error between NTP and clock interval */
845 timekeeper.ntp_error += tick_length;
846 timekeeper.ntp_error -=
847 (timekeeper.xtime_interval + timekeeper.xtime_remainder) <<
848 timekeeper.ntp_error_shift;
851 /* correct the clock when NTP error is too big */
852 timekeeping_adjust(offset);
855 * Since in the loop above, we accumulate any amount of time
856 * in xtime_nsec over a second into xtime.tv_sec, its possible for
857 * xtime_nsec to be fairly small after the loop. Further, if we're
858 * slightly speeding the clocksource up in timekeeping_adjust(),
859 * its possible the required corrective factor to xtime_nsec could
860 * cause it to underflow.
862 * Now, we cannot simply roll the accumulated second back, since
863 * the NTP subsystem has been notified via second_overflow. So
864 * instead we push xtime_nsec forward by the amount we underflowed,
865 * and add that amount into the error.
867 * We'll correct this error next time through this function, when
868 * xtime_nsec is not as small.
870 if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
871 s64 neg = -(s64)timekeeper.xtime_nsec;
872 timekeeper.xtime_nsec = 0;
873 timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
876 /* store full nanoseconds into xtime after rounding it up and
877 * add the remainder to the error difference.
879 xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
880 timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
881 timekeeper.ntp_error += timekeeper.xtime_nsec <<
882 timekeeper.ntp_error_shift;
884 nsecs = clocksource_cyc2ns(offset, timekeeper.mult, timekeeper.shift);
885 update_xtime_cache(nsecs);
887 timekeeping_update(false);
891 * getboottime - Return the real time of system boot.
892 * @ts: pointer to the timespec to be set
894 * Returns the time of day in a timespec.
896 * This is based on the wall_to_monotonic offset and the total suspend
897 * time. Calls to settimeofday will affect the value returned (which
898 * basically means that however wrong your real time clock is at boot time,
899 * you get the right time here).
901 void getboottime(struct timespec *ts)
903 struct timespec boottime = {
904 .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec,
905 .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec
908 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
910 EXPORT_SYMBOL_GPL(getboottime);
913 * monotonic_to_bootbased - Convert the monotonic time to boot based.
914 * @ts: pointer to the timespec to be converted
916 void monotonic_to_bootbased(struct timespec *ts)
918 *ts = timespec_add_safe(*ts, total_sleep_time);
920 EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
922 unsigned long get_seconds(void)
924 return xtime_cache.tv_sec;
926 EXPORT_SYMBOL(get_seconds);
928 struct timespec __current_kernel_time(void)
930 return xtime_cache;
933 struct timespec current_kernel_time(void)
935 struct timespec now;
936 unsigned long seq;
938 do {
939 seq = read_seqbegin(&xtime_lock);
941 now = xtime_cache;
942 } while (read_seqretry(&xtime_lock, seq));
944 return now;
946 EXPORT_SYMBOL(current_kernel_time);
948 struct timespec get_monotonic_coarse(void)
950 struct timespec now, mono;
951 unsigned long seq;
953 do {
954 seq = read_seqbegin(&xtime_lock);
956 now = xtime_cache;
957 mono = wall_to_monotonic;
958 } while (read_seqretry(&xtime_lock, seq));
960 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
961 now.tv_nsec + mono.tv_nsec);
962 return now;
965 #ifdef CONFIG_HIGH_RES_TIMERS
967 * ktime_get_update_offsets - hrtimer helper
968 * @real: pointer to storage for monotonic -> realtime offset
970 * Returns current monotonic time and updates the offsets
971 * Called from hrtimer_interupt() or retrigger_next_event()
973 ktime_t ktime_get_update_offsets(ktime_t *real)
975 ktime_t now;
976 unsigned int seq;
977 u64 secs, nsecs;
979 do {
980 seq = read_seqbegin(&xtime_lock);
982 secs = xtime.tv_sec;
983 nsecs = xtime.tv_nsec;
984 nsecs += timekeeping_get_ns();
985 /* If arch requires, add in gettimeoffset() */
986 nsecs += arch_gettimeoffset();
988 *real = offs_real;
989 } while (read_seqretry(&xtime_lock, seq));
991 now = ktime_add_ns(ktime_set(secs, 0), nsecs);
992 now = ktime_sub(now, *real);
993 return now;
995 #endif