sched: Fix wake_affine() vs RT tasks
[linux/fpc-iii.git] / kernel / time / timekeeping.c
blob26e2f3705cc159e75324c28e3caacfdfea379484
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 /* Raw nano seconds accumulated per NTP interval. */
36 u32 raw_interval;
38 /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
39 u64 xtime_nsec;
40 /* Difference between accumulated time and NTP time in ntp
41 * shifted nano seconds. */
42 s64 ntp_error;
43 /* Shift conversion between clock shifted nano seconds and
44 * ntp shifted nano seconds. */
45 int ntp_error_shift;
46 /* NTP adjusted clock multiplier */
47 u32 mult;
50 struct timekeeper timekeeper;
52 /**
53 * timekeeper_setup_internals - Set up internals to use clocksource clock.
55 * @clock: Pointer to clocksource.
57 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
58 * pair and interval request.
60 * Unless you're the timekeeping code, you should not be using this!
62 static void timekeeper_setup_internals(struct clocksource *clock)
64 cycle_t interval;
65 u64 tmp;
67 timekeeper.clock = clock;
68 clock->cycle_last = clock->read(clock);
70 /* Do the ns -> cycle conversion first, using original mult */
71 tmp = NTP_INTERVAL_LENGTH;
72 tmp <<= clock->shift;
73 tmp += clock->mult/2;
74 do_div(tmp, clock->mult);
75 if (tmp == 0)
76 tmp = 1;
78 interval = (cycle_t) tmp;
79 timekeeper.cycle_interval = interval;
81 /* Go back from cycles -> shifted ns */
82 timekeeper.xtime_interval = (u64) interval * clock->mult;
83 timekeeper.raw_interval =
84 ((u64) interval * clock->mult) >> clock->shift;
86 timekeeper.xtime_nsec = 0;
87 timekeeper.shift = clock->shift;
89 timekeeper.ntp_error = 0;
90 timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
93 * The timekeeper keeps its own mult values for the currently
94 * active clocksource. These value will be adjusted via NTP
95 * to counteract clock drifting.
97 timekeeper.mult = clock->mult;
100 /* Timekeeper helper functions. */
101 static inline s64 timekeeping_get_ns(void)
103 cycle_t cycle_now, cycle_delta;
104 struct clocksource *clock;
106 /* read clocksource: */
107 clock = timekeeper.clock;
108 cycle_now = clock->read(clock);
110 /* calculate the delta since the last update_wall_time: */
111 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
113 /* return delta convert to nanoseconds using ntp adjusted mult. */
114 return clocksource_cyc2ns(cycle_delta, timekeeper.mult,
115 timekeeper.shift);
118 static inline s64 timekeeping_get_ns_raw(void)
120 cycle_t cycle_now, cycle_delta;
121 struct clocksource *clock;
123 /* read clocksource: */
124 clock = timekeeper.clock;
125 cycle_now = clock->read(clock);
127 /* calculate the delta since the last update_wall_time: */
128 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
130 /* return delta convert to nanoseconds using ntp adjusted mult. */
131 return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
135 * This read-write spinlock protects us from races in SMP while
136 * playing with xtime.
138 __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
142 * The current time
143 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
144 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
145 * at zero at system boot time, so wall_to_monotonic will be negative,
146 * however, we will ALWAYS keep the tv_nsec part positive so we can use
147 * the usual normalization.
149 * wall_to_monotonic is moved after resume from suspend for the monotonic
150 * time not to jump. We need to add total_sleep_time to wall_to_monotonic
151 * to get the real boot based time offset.
153 * - wall_to_monotonic is no longer the boot time, getboottime must be
154 * used instead.
156 struct timespec xtime __attribute__ ((aligned (16)));
157 struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
158 static struct timespec total_sleep_time;
161 * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
163 struct timespec raw_time;
165 /* flag for if timekeeping is suspended */
166 int __read_mostly timekeeping_suspended;
168 static struct timespec xtime_cache __attribute__ ((aligned (16)));
169 void update_xtime_cache(u64 nsec)
171 xtime_cache = xtime;
172 timespec_add_ns(&xtime_cache, nsec);
175 /* must hold xtime_lock */
176 void timekeeping_leap_insert(int leapsecond)
178 xtime.tv_sec += leapsecond;
179 wall_to_monotonic.tv_sec -= leapsecond;
180 update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult);
183 #ifdef CONFIG_GENERIC_TIME
186 * timekeeping_forward_now - update clock to the current time
188 * Forward the current clock to update its state since the last call to
189 * update_wall_time(). This is useful before significant clock changes,
190 * as it avoids having to deal with this time offset explicitly.
192 static void timekeeping_forward_now(void)
194 cycle_t cycle_now, cycle_delta;
195 struct clocksource *clock;
196 s64 nsec;
198 clock = timekeeper.clock;
199 cycle_now = clock->read(clock);
200 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
201 clock->cycle_last = cycle_now;
203 nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
204 timekeeper.shift);
206 /* If arch requires, add in gettimeoffset() */
207 nsec += arch_gettimeoffset();
209 timespec_add_ns(&xtime, nsec);
211 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
212 timespec_add_ns(&raw_time, nsec);
216 * getnstimeofday - Returns the time of day in a timespec
217 * @ts: pointer to the timespec to be set
219 * Returns the time of day in a timespec.
221 void getnstimeofday(struct timespec *ts)
223 unsigned long seq;
224 s64 nsecs;
226 WARN_ON(timekeeping_suspended);
228 do {
229 seq = read_seqbegin(&xtime_lock);
231 *ts = xtime;
232 nsecs = timekeeping_get_ns();
234 /* If arch requires, add in gettimeoffset() */
235 nsecs += arch_gettimeoffset();
237 } while (read_seqretry(&xtime_lock, seq));
239 timespec_add_ns(ts, nsecs);
242 EXPORT_SYMBOL(getnstimeofday);
244 ktime_t ktime_get(void)
246 unsigned int seq;
247 s64 secs, nsecs;
249 WARN_ON(timekeeping_suspended);
251 do {
252 seq = read_seqbegin(&xtime_lock);
253 secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
254 nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
255 nsecs += timekeeping_get_ns();
257 } while (read_seqretry(&xtime_lock, seq));
259 * Use ktime_set/ktime_add_ns to create a proper ktime on
260 * 32-bit architectures without CONFIG_KTIME_SCALAR.
262 return ktime_add_ns(ktime_set(secs, 0), nsecs);
264 EXPORT_SYMBOL_GPL(ktime_get);
267 * ktime_get_ts - get the monotonic clock in timespec format
268 * @ts: pointer to timespec variable
270 * The function calculates the monotonic clock from the realtime
271 * clock and the wall_to_monotonic offset and stores the result
272 * in normalized timespec format in the variable pointed to by @ts.
274 void ktime_get_ts(struct timespec *ts)
276 struct timespec tomono;
277 unsigned int seq;
278 s64 nsecs;
280 WARN_ON(timekeeping_suspended);
282 do {
283 seq = read_seqbegin(&xtime_lock);
284 *ts = xtime;
285 tomono = wall_to_monotonic;
286 nsecs = timekeeping_get_ns();
288 } while (read_seqretry(&xtime_lock, seq));
290 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
291 ts->tv_nsec + tomono.tv_nsec + nsecs);
293 EXPORT_SYMBOL_GPL(ktime_get_ts);
296 * do_gettimeofday - Returns the time of day in a timeval
297 * @tv: pointer to the timeval to be set
299 * NOTE: Users should be converted to using getnstimeofday()
301 void do_gettimeofday(struct timeval *tv)
303 struct timespec now;
305 getnstimeofday(&now);
306 tv->tv_sec = now.tv_sec;
307 tv->tv_usec = now.tv_nsec/1000;
310 EXPORT_SYMBOL(do_gettimeofday);
312 * do_settimeofday - Sets the time of day
313 * @tv: pointer to the timespec variable containing the new time
315 * Sets the time of day to the new time and update NTP and notify hrtimers
317 int do_settimeofday(struct timespec *tv)
319 struct timespec ts_delta;
320 unsigned long flags;
322 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
323 return -EINVAL;
325 write_seqlock_irqsave(&xtime_lock, flags);
327 timekeeping_forward_now();
329 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
330 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
331 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
333 xtime = *tv;
335 update_xtime_cache(0);
337 timekeeper.ntp_error = 0;
338 ntp_clear();
340 update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult);
342 write_sequnlock_irqrestore(&xtime_lock, flags);
344 /* signal hrtimers about time change */
345 clock_was_set();
347 return 0;
350 EXPORT_SYMBOL(do_settimeofday);
353 * change_clocksource - Swaps clocksources if a new one is available
355 * Accumulates current time interval and initializes new clocksource
357 static int change_clocksource(void *data)
359 struct clocksource *new, *old;
361 new = (struct clocksource *) data;
363 timekeeping_forward_now();
364 if (!new->enable || new->enable(new) == 0) {
365 old = timekeeper.clock;
366 timekeeper_setup_internals(new);
367 if (old->disable)
368 old->disable(old);
370 return 0;
374 * timekeeping_notify - Install a new clock source
375 * @clock: pointer to the clock source
377 * This function is called from clocksource.c after a new, better clock
378 * source has been registered. The caller holds the clocksource_mutex.
380 void timekeeping_notify(struct clocksource *clock)
382 if (timekeeper.clock == clock)
383 return;
384 stop_machine(change_clocksource, clock, NULL);
385 tick_clock_notify();
388 #else /* GENERIC_TIME */
390 static inline void timekeeping_forward_now(void) { }
393 * ktime_get - get the monotonic time in ktime_t format
395 * returns the time in ktime_t format
397 ktime_t ktime_get(void)
399 struct timespec now;
401 ktime_get_ts(&now);
403 return timespec_to_ktime(now);
405 EXPORT_SYMBOL_GPL(ktime_get);
408 * ktime_get_ts - get the monotonic clock in timespec format
409 * @ts: pointer to timespec variable
411 * The function calculates the monotonic clock from the realtime
412 * clock and the wall_to_monotonic offset and stores the result
413 * in normalized timespec format in the variable pointed to by @ts.
415 void ktime_get_ts(struct timespec *ts)
417 struct timespec tomono;
418 unsigned long seq;
420 do {
421 seq = read_seqbegin(&xtime_lock);
422 getnstimeofday(ts);
423 tomono = wall_to_monotonic;
425 } while (read_seqretry(&xtime_lock, seq));
427 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
428 ts->tv_nsec + tomono.tv_nsec);
430 EXPORT_SYMBOL_GPL(ktime_get_ts);
432 #endif /* !GENERIC_TIME */
435 * ktime_get_real - get the real (wall-) time in ktime_t format
437 * returns the time in ktime_t format
439 ktime_t ktime_get_real(void)
441 struct timespec now;
443 getnstimeofday(&now);
445 return timespec_to_ktime(now);
447 EXPORT_SYMBOL_GPL(ktime_get_real);
450 * getrawmonotonic - Returns the raw monotonic time in a timespec
451 * @ts: pointer to the timespec to be set
453 * Returns the raw monotonic time (completely un-modified by ntp)
455 void getrawmonotonic(struct timespec *ts)
457 unsigned long seq;
458 s64 nsecs;
460 do {
461 seq = read_seqbegin(&xtime_lock);
462 nsecs = timekeeping_get_ns_raw();
463 *ts = raw_time;
465 } while (read_seqretry(&xtime_lock, seq));
467 timespec_add_ns(ts, nsecs);
469 EXPORT_SYMBOL(getrawmonotonic);
473 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
475 int timekeeping_valid_for_hres(void)
477 unsigned long seq;
478 int ret;
480 do {
481 seq = read_seqbegin(&xtime_lock);
483 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
485 } while (read_seqretry(&xtime_lock, seq));
487 return ret;
491 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
493 * Caller must observe xtime_lock via read_seqbegin/read_seqretry to
494 * ensure that the clocksource does not change!
496 u64 timekeeping_max_deferment(void)
498 return timekeeper.clock->max_idle_ns;
502 * read_persistent_clock - Return time from the persistent clock.
504 * Weak dummy function for arches that do not yet support it.
505 * Reads the time from the battery backed persistent clock.
506 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
508 * XXX - Do be sure to remove it once all arches implement it.
510 void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
512 ts->tv_sec = 0;
513 ts->tv_nsec = 0;
517 * read_boot_clock - Return time of the system start.
519 * Weak dummy function for arches that do not yet support it.
520 * Function to read the exact time the system has been started.
521 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
523 * XXX - Do be sure to remove it once all arches implement it.
525 void __attribute__((weak)) read_boot_clock(struct timespec *ts)
527 ts->tv_sec = 0;
528 ts->tv_nsec = 0;
532 * timekeeping_init - Initializes the clocksource and common timekeeping values
534 void __init timekeeping_init(void)
536 struct clocksource *clock;
537 unsigned long flags;
538 struct timespec now, boot;
540 read_persistent_clock(&now);
541 read_boot_clock(&boot);
543 write_seqlock_irqsave(&xtime_lock, flags);
545 ntp_init();
547 clock = clocksource_default_clock();
548 if (clock->enable)
549 clock->enable(clock);
550 timekeeper_setup_internals(clock);
552 xtime.tv_sec = now.tv_sec;
553 xtime.tv_nsec = now.tv_nsec;
554 raw_time.tv_sec = 0;
555 raw_time.tv_nsec = 0;
556 if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
557 boot.tv_sec = xtime.tv_sec;
558 boot.tv_nsec = xtime.tv_nsec;
560 set_normalized_timespec(&wall_to_monotonic,
561 -boot.tv_sec, -boot.tv_nsec);
562 update_xtime_cache(0);
563 total_sleep_time.tv_sec = 0;
564 total_sleep_time.tv_nsec = 0;
565 write_sequnlock_irqrestore(&xtime_lock, flags);
568 /* time in seconds when suspend began */
569 static struct timespec timekeeping_suspend_time;
572 * timekeeping_resume - Resumes the generic timekeeping subsystem.
573 * @dev: unused
575 * This is for the generic clocksource timekeeping.
576 * xtime/wall_to_monotonic/jiffies/etc are
577 * still managed by arch specific suspend/resume code.
579 static int timekeeping_resume(struct sys_device *dev)
581 unsigned long flags;
582 struct timespec ts;
584 read_persistent_clock(&ts);
586 clocksource_resume();
588 write_seqlock_irqsave(&xtime_lock, flags);
590 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
591 ts = timespec_sub(ts, timekeeping_suspend_time);
592 xtime = timespec_add_safe(xtime, ts);
593 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts);
594 total_sleep_time = timespec_add_safe(total_sleep_time, ts);
596 update_xtime_cache(0);
597 /* re-base the last cycle value */
598 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
599 timekeeper.ntp_error = 0;
600 timekeeping_suspended = 0;
601 write_sequnlock_irqrestore(&xtime_lock, flags);
603 touch_softlockup_watchdog();
605 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
607 /* Resume hrtimers */
608 hres_timers_resume();
610 return 0;
613 static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
615 unsigned long flags;
617 read_persistent_clock(&timekeeping_suspend_time);
619 write_seqlock_irqsave(&xtime_lock, flags);
620 timekeeping_forward_now();
621 timekeeping_suspended = 1;
622 write_sequnlock_irqrestore(&xtime_lock, flags);
624 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
626 return 0;
629 /* sysfs resume/suspend bits for timekeeping */
630 static struct sysdev_class timekeeping_sysclass = {
631 .name = "timekeeping",
632 .resume = timekeeping_resume,
633 .suspend = timekeeping_suspend,
636 static struct sys_device device_timer = {
637 .id = 0,
638 .cls = &timekeeping_sysclass,
641 static int __init timekeeping_init_device(void)
643 int error = sysdev_class_register(&timekeeping_sysclass);
644 if (!error)
645 error = sysdev_register(&device_timer);
646 return error;
649 device_initcall(timekeeping_init_device);
652 * If the error is already larger, we look ahead even further
653 * to compensate for late or lost adjustments.
655 static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
656 s64 *offset)
658 s64 tick_error, i;
659 u32 look_ahead, adj;
660 s32 error2, mult;
663 * Use the current error value to determine how much to look ahead.
664 * The larger the error the slower we adjust for it to avoid problems
665 * with losing too many ticks, otherwise we would overadjust and
666 * produce an even larger error. The smaller the adjustment the
667 * faster we try to adjust for it, as lost ticks can do less harm
668 * here. This is tuned so that an error of about 1 msec is adjusted
669 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
671 error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
672 error2 = abs(error2);
673 for (look_ahead = 0; error2 > 0; look_ahead++)
674 error2 >>= 2;
677 * Now calculate the error in (1 << look_ahead) ticks, but first
678 * remove the single look ahead already included in the error.
680 tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
681 tick_error -= timekeeper.xtime_interval >> 1;
682 error = ((error - tick_error) >> look_ahead) + tick_error;
684 /* Finally calculate the adjustment shift value. */
685 i = *interval;
686 mult = 1;
687 if (error < 0) {
688 error = -error;
689 *interval = -*interval;
690 *offset = -*offset;
691 mult = -1;
693 for (adj = 0; error > i; adj++)
694 error >>= 1;
696 *interval <<= adj;
697 *offset <<= adj;
698 return mult << adj;
702 * Adjust the multiplier to reduce the error value,
703 * this is optimized for the most common adjustments of -1,0,1,
704 * for other values we can do a bit more work.
706 static void timekeeping_adjust(s64 offset)
708 s64 error, interval = timekeeper.cycle_interval;
709 int adj;
711 error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
712 if (error > interval) {
713 error >>= 2;
714 if (likely(error <= interval))
715 adj = 1;
716 else
717 adj = timekeeping_bigadjust(error, &interval, &offset);
718 } else if (error < -interval) {
719 error >>= 2;
720 if (likely(error >= -interval)) {
721 adj = -1;
722 interval = -interval;
723 offset = -offset;
724 } else
725 adj = timekeeping_bigadjust(error, &interval, &offset);
726 } else
727 return;
729 timekeeper.mult += adj;
730 timekeeper.xtime_interval += interval;
731 timekeeper.xtime_nsec -= offset;
732 timekeeper.ntp_error -= (interval - offset) <<
733 timekeeper.ntp_error_shift;
737 * update_wall_time - Uses the current clocksource to increment the wall time
739 * Called from the timer interrupt, must hold a write on xtime_lock.
741 void update_wall_time(void)
743 struct clocksource *clock;
744 cycle_t offset;
745 u64 nsecs;
747 /* Make sure we're fully resumed: */
748 if (unlikely(timekeeping_suspended))
749 return;
751 clock = timekeeper.clock;
752 #ifdef CONFIG_GENERIC_TIME
753 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
754 #else
755 offset = timekeeper.cycle_interval;
756 #endif
757 timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
759 /* normally this loop will run just once, however in the
760 * case of lost or late ticks, it will accumulate correctly.
762 while (offset >= timekeeper.cycle_interval) {
763 u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
765 /* accumulate one interval */
766 offset -= timekeeper.cycle_interval;
767 clock->cycle_last += timekeeper.cycle_interval;
769 timekeeper.xtime_nsec += timekeeper.xtime_interval;
770 if (timekeeper.xtime_nsec >= nsecps) {
771 timekeeper.xtime_nsec -= nsecps;
772 xtime.tv_sec++;
773 second_overflow();
776 raw_time.tv_nsec += timekeeper.raw_interval;
777 if (raw_time.tv_nsec >= NSEC_PER_SEC) {
778 raw_time.tv_nsec -= NSEC_PER_SEC;
779 raw_time.tv_sec++;
782 /* accumulate error between NTP and clock interval */
783 timekeeper.ntp_error += tick_length;
784 timekeeper.ntp_error -= timekeeper.xtime_interval <<
785 timekeeper.ntp_error_shift;
788 /* correct the clock when NTP error is too big */
789 timekeeping_adjust(offset);
792 * Since in the loop above, we accumulate any amount of time
793 * in xtime_nsec over a second into xtime.tv_sec, its possible for
794 * xtime_nsec to be fairly small after the loop. Further, if we're
795 * slightly speeding the clocksource up in timekeeping_adjust(),
796 * its possible the required corrective factor to xtime_nsec could
797 * cause it to underflow.
799 * Now, we cannot simply roll the accumulated second back, since
800 * the NTP subsystem has been notified via second_overflow. So
801 * instead we push xtime_nsec forward by the amount we underflowed,
802 * and add that amount into the error.
804 * We'll correct this error next time through this function, when
805 * xtime_nsec is not as small.
807 if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
808 s64 neg = -(s64)timekeeper.xtime_nsec;
809 timekeeper.xtime_nsec = 0;
810 timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
813 /* store full nanoseconds into xtime after rounding it up and
814 * add the remainder to the error difference.
816 xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
817 timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
818 timekeeper.ntp_error += timekeeper.xtime_nsec <<
819 timekeeper.ntp_error_shift;
821 nsecs = clocksource_cyc2ns(offset, timekeeper.mult, timekeeper.shift);
822 update_xtime_cache(nsecs);
824 /* check to see if there is a new clocksource to use */
825 update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult);
829 * getboottime - Return the real time of system boot.
830 * @ts: pointer to the timespec to be set
832 * Returns the time of day in a timespec.
834 * This is based on the wall_to_monotonic offset and the total suspend
835 * time. Calls to settimeofday will affect the value returned (which
836 * basically means that however wrong your real time clock is at boot time,
837 * you get the right time here).
839 void getboottime(struct timespec *ts)
841 struct timespec boottime = {
842 .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec,
843 .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec
846 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
848 EXPORT_SYMBOL_GPL(getboottime);
851 * monotonic_to_bootbased - Convert the monotonic time to boot based.
852 * @ts: pointer to the timespec to be converted
854 void monotonic_to_bootbased(struct timespec *ts)
856 *ts = timespec_add_safe(*ts, total_sleep_time);
858 EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
860 unsigned long get_seconds(void)
862 return xtime_cache.tv_sec;
864 EXPORT_SYMBOL(get_seconds);
866 struct timespec __current_kernel_time(void)
868 return xtime_cache;
871 struct timespec current_kernel_time(void)
873 struct timespec now;
874 unsigned long seq;
876 do {
877 seq = read_seqbegin(&xtime_lock);
879 now = xtime_cache;
880 } while (read_seqretry(&xtime_lock, seq));
882 return now;
884 EXPORT_SYMBOL(current_kernel_time);
886 struct timespec get_monotonic_coarse(void)
888 struct timespec now, mono;
889 unsigned long seq;
891 do {
892 seq = read_seqbegin(&xtime_lock);
894 now = xtime_cache;
895 mono = wall_to_monotonic;
896 } while (read_seqretry(&xtime_lock, seq));
898 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
899 now.tv_nsec + mono.tv_nsec);
900 return now;