headers: remove sched.h from interrupt.h
[safe/jmp/linux-2.6] / kernel / time / timekeeping.c
index acc417b..c3a4e29 100644 (file)
 #include <linux/percpu.h>
 #include <linux/init.h>
 #include <linux/mm.h>
+#include <linux/sched.h>
 #include <linux/sysdev.h>
 #include <linux/clocksource.h>
 #include <linux/jiffies.h>
 #include <linux/time.h>
 #include <linux/tick.h>
+#include <linux/stop_machine.h>
+
+/* Structure holding internal timekeeping values. */
+struct timekeeper {
+       /* Current clocksource used for timekeeping. */
+       struct clocksource *clock;
+       /* The shift value of the current clocksource. */
+       int     shift;
+
+       /* Number of clock cycles in one NTP interval. */
+       cycle_t cycle_interval;
+       /* Number of clock shifted nano seconds in one NTP interval. */
+       u64     xtime_interval;
+       /* Raw nano seconds accumulated per NTP interval. */
+       u32     raw_interval;
+
+       /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
+       u64     xtime_nsec;
+       /* Difference between accumulated time and NTP time in ntp
+        * shifted nano seconds. */
+       s64     ntp_error;
+       /* Shift conversion between clock shifted nano seconds and
+        * ntp shifted nano seconds. */
+       int     ntp_error_shift;
+       /* NTP adjusted clock multiplier */
+       u32     mult;
+};
+
+struct timekeeper timekeeper;
+
+/**
+ * timekeeper_setup_internals - Set up internals to use clocksource clock.
+ *
+ * @clock:             Pointer to clocksource.
+ *
+ * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
+ * pair and interval request.
+ *
+ * Unless you're the timekeeping code, you should not be using this!
+ */
+static void timekeeper_setup_internals(struct clocksource *clock)
+{
+       cycle_t interval;
+       u64 tmp;
+
+       timekeeper.clock = clock;
+       clock->cycle_last = clock->read(clock);
+
+       /* Do the ns -> cycle conversion first, using original mult */
+       tmp = NTP_INTERVAL_LENGTH;
+       tmp <<= clock->shift;
+       tmp += clock->mult/2;
+       do_div(tmp, clock->mult);
+       if (tmp == 0)
+               tmp = 1;
+
+       interval = (cycle_t) tmp;
+       timekeeper.cycle_interval = interval;
+
+       /* Go back from cycles -> shifted ns */
+       timekeeper.xtime_interval = (u64) interval * clock->mult;
+       timekeeper.raw_interval =
+               ((u64) interval * clock->mult) >> clock->shift;
+
+       timekeeper.xtime_nsec = 0;
+       timekeeper.shift = clock->shift;
+
+       timekeeper.ntp_error = 0;
+       timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
+
+       /*
+        * The timekeeper keeps its own mult values for the currently
+        * active clocksource. These value will be adjusted via NTP
+        * to counteract clock drifting.
+        */
+       timekeeper.mult = clock->mult;
+}
+
+/* Timekeeper helper functions. */
+static inline s64 timekeeping_get_ns(void)
+{
+       cycle_t cycle_now, cycle_delta;
+       struct clocksource *clock;
+
+       /* read clocksource: */
+       clock = timekeeper.clock;
+       cycle_now = clock->read(clock);
 
+       /* calculate the delta since the last update_wall_time: */
+       cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
+
+       /* return delta convert to nanoseconds using ntp adjusted mult. */
+       return clocksource_cyc2ns(cycle_delta, timekeeper.mult,
+                                 timekeeper.shift);
+}
+
+static inline s64 timekeeping_get_ns_raw(void)
+{
+       cycle_t cycle_now, cycle_delta;
+       struct clocksource *clock;
+
+       /* read clocksource: */
+       clock = timekeeper.clock;
+       cycle_now = clock->read(clock);
+
+       /* calculate the delta since the last update_wall_time: */
+       cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
+
+       /* return delta convert to nanoseconds using ntp adjusted mult. */
+       return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
+}
 
 /*
  * This read-write spinlock protects us from races in SMP while
- * playing with xtime and avenrun.
+ * playing with xtime.
  */
-__attribute__((weak)) __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
-
-EXPORT_SYMBOL(xtime_lock);
+__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
 
 
 /*
@@ -46,98 +155,154 @@ EXPORT_SYMBOL(xtime_lock);
  */
 struct timespec xtime __attribute__ ((aligned (16)));
 struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
-static unsigned long total_sleep_time;         /* seconds */
-EXPORT_SYMBOL(xtime);
+static struct timespec total_sleep_time;
+
+/*
+ * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
+ */
+struct timespec raw_time;
 
+/* flag for if timekeeping is suspended */
+int __read_mostly timekeeping_suspended;
 
-#ifdef CONFIG_NO_HZ
 static struct timespec xtime_cache __attribute__ ((aligned (16)));
-static inline void update_xtime_cache(u64 nsec)
+void update_xtime_cache(u64 nsec)
 {
        xtime_cache = xtime;
        timespec_add_ns(&xtime_cache, nsec);
 }
-#else
-#define xtime_cache xtime
-/* We do *not* want to evaluate the argument for this case */
-#define update_xtime_cache(n) do { } while (0)
-#endif
-
-static struct clocksource *clock; /* pointer to current clocksource */
 
+/* must hold xtime_lock */
+void timekeeping_leap_insert(int leapsecond)
+{
+       xtime.tv_sec += leapsecond;
+       wall_to_monotonic.tv_sec -= leapsecond;
+       update_vsyscall(&xtime, timekeeper.clock);
+}
 
 #ifdef CONFIG_GENERIC_TIME
+
 /**
- * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook
+ * timekeeping_forward_now - update clock to the current time
  *
- * private function, must hold xtime_lock lock when being
- * called. Returns the number of nanoseconds since the
- * last call to update_wall_time() (adjusted by NTP scaling)
+ * Forward the current clock to update its state since the last call to
+ * update_wall_time(). This is useful before significant clock changes,
+ * as it avoids having to deal with this time offset explicitly.
  */
-static inline s64 __get_nsec_offset(void)
+static void timekeeping_forward_now(void)
 {
        cycle_t cycle_now, cycle_delta;
-       s64 ns_offset;
-
-       /* read clocksource: */
-       cycle_now = clocksource_read(clock);
+       struct clocksource *clock;
+       s64 nsec;
 
-       /* calculate the delta since the last update_wall_time: */
+       clock = timekeeper.clock;
+       cycle_now = clock->read(clock);
        cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
+       clock->cycle_last = cycle_now;
+
+       nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
+                                 timekeeper.shift);
+
+       /* If arch requires, add in gettimeoffset() */
+       nsec += arch_gettimeoffset();
 
-       /* convert to nanoseconds: */
-       ns_offset = cyc2ns(clock, cycle_delta);
+       timespec_add_ns(&xtime, nsec);
 
-       return ns_offset;
+       nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
+       timespec_add_ns(&raw_time, nsec);
 }
 
 /**
- * __get_realtime_clock_ts - Returns the time of day in a timespec
+ * getnstimeofday - Returns the time of day in a timespec
  * @ts:                pointer to the timespec to be set
  *
- * Returns the time of day in a timespec. Used by
- * do_gettimeofday() and get_realtime_clock_ts().
+ * Returns the time of day in a timespec.
  */
-static inline void __get_realtime_clock_ts(struct timespec *ts)
+void getnstimeofday(struct timespec *ts)
 {
        unsigned long seq;
        s64 nsecs;
 
+       WARN_ON(timekeeping_suspended);
+
        do {
                seq = read_seqbegin(&xtime_lock);
 
                *ts = xtime;
-               nsecs = __get_nsec_offset();
+               nsecs = timekeeping_get_ns();
+
+               /* If arch requires, add in gettimeoffset() */
+               nsecs += arch_gettimeoffset();
 
        } while (read_seqretry(&xtime_lock, seq));
 
        timespec_add_ns(ts, nsecs);
 }
 
+EXPORT_SYMBOL(getnstimeofday);
+
+ktime_t ktime_get(void)
+{
+       unsigned int seq;
+       s64 secs, nsecs;
+
+       WARN_ON(timekeeping_suspended);
+
+       do {
+               seq = read_seqbegin(&xtime_lock);
+               secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
+               nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
+               nsecs += timekeeping_get_ns();
+
+       } while (read_seqretry(&xtime_lock, seq));
+       /*
+        * Use ktime_set/ktime_add_ns to create a proper ktime on
+        * 32-bit architectures without CONFIG_KTIME_SCALAR.
+        */
+       return ktime_add_ns(ktime_set(secs, 0), nsecs);
+}
+EXPORT_SYMBOL_GPL(ktime_get);
+
 /**
- * getnstimeofday - Returns the time of day in a timespec
- * @ts:                pointer to the timespec to be set
+ * ktime_get_ts - get the monotonic clock in timespec format
+ * @ts:                pointer to timespec variable
  *
- * Returns the time of day in a timespec.
+ * The function calculates the monotonic clock from the realtime
+ * clock and the wall_to_monotonic offset and stores the result
+ * in normalized timespec format in the variable pointed to by @ts.
  */
-void getnstimeofday(struct timespec *ts)
+void ktime_get_ts(struct timespec *ts)
 {
-       __get_realtime_clock_ts(ts);
-}
+       struct timespec tomono;
+       unsigned int seq;
+       s64 nsecs;
 
-EXPORT_SYMBOL(getnstimeofday);
+       WARN_ON(timekeeping_suspended);
+
+       do {
+               seq = read_seqbegin(&xtime_lock);
+               *ts = xtime;
+               tomono = wall_to_monotonic;
+               nsecs = timekeeping_get_ns();
+
+       } while (read_seqretry(&xtime_lock, seq));
+
+       set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
+                               ts->tv_nsec + tomono.tv_nsec + nsecs);
+}
+EXPORT_SYMBOL_GPL(ktime_get_ts);
 
 /**
  * do_gettimeofday - Returns the time of day in a timeval
  * @tv:                pointer to the timeval to be set
  *
- * NOTE: Users should be converted to using get_realtime_clock_ts()
+ * NOTE: Users should be converted to using getnstimeofday()
  */
 void do_gettimeofday(struct timeval *tv)
 {
        struct timespec now;
 
-       __get_realtime_clock_ts(&now);
+       getnstimeofday(&now);
        tv->tv_sec = now.tv_sec;
        tv->tv_usec = now.tv_nsec/1000;
 }
@@ -151,27 +316,28 @@ EXPORT_SYMBOL(do_gettimeofday);
  */
 int do_settimeofday(struct timespec *tv)
 {
+       struct timespec ts_delta;
        unsigned long flags;
-       time_t wtm_sec, sec = tv->tv_sec;
-       long wtm_nsec, nsec = tv->tv_nsec;
 
        if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
                return -EINVAL;
 
        write_seqlock_irqsave(&xtime_lock, flags);
 
-       nsec -= __get_nsec_offset();
+       timekeeping_forward_now();
+
+       ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
+       ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
+       wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
 
-       wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
-       wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
+       xtime = *tv;
 
-       set_normalized_timespec(&xtime, sec, nsec);
-       set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
+       update_xtime_cache(0);
 
-       clock->error = 0;
+       timekeeper.ntp_error = 0;
        ntp_clear();
 
-       update_vsyscall(&xtime, clock);
+       update_vsyscall(&xtime, timekeeper.clock);
 
        write_sequnlock_irqrestore(&xtime_lock, flags);
 
@@ -188,41 +354,125 @@ EXPORT_SYMBOL(do_settimeofday);
  *
  * Accumulates current time interval and initializes new clocksource
  */
-static void change_clocksource(void)
+static int change_clocksource(void *data)
 {
-       struct clocksource *new;
-       cycle_t now;
-       u64 nsec;
+       struct clocksource *new, *old;
 
-       new = clocksource_get_next();
+       new = (struct clocksource *) data;
 
-       if (clock == new)
+       timekeeping_forward_now();
+       if (!new->enable || new->enable(new) == 0) {
+               old = timekeeper.clock;
+               timekeeper_setup_internals(new);
+               if (old->disable)
+                       old->disable(old);
+       }
+       return 0;
+}
+
+/**
+ * timekeeping_notify - Install a new clock source
+ * @clock:             pointer to the clock source
+ *
+ * This function is called from clocksource.c after a new, better clock
+ * source has been registered. The caller holds the clocksource_mutex.
+ */
+void timekeeping_notify(struct clocksource *clock)
+{
+       if (timekeeper.clock == clock)
                return;
+       stop_machine(change_clocksource, clock, NULL);
+       tick_clock_notify();
+}
 
-       now = clocksource_read(new);
-       nsec =  __get_nsec_offset();
-       timespec_add_ns(&xtime, nsec);
+#else /* GENERIC_TIME */
 
-       clock = new;
-       clock->cycle_last = now;
+static inline void timekeeping_forward_now(void) { }
 
-       clock->error = 0;
-       clock->xtime_nsec = 0;
-       clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
+/**
+ * ktime_get - get the monotonic time in ktime_t format
+ *
+ * returns the time in ktime_t format
+ */
+ktime_t ktime_get(void)
+{
+       struct timespec now;
 
-       tick_clock_notify();
+       ktime_get_ts(&now);
 
-       printk(KERN_INFO "Time: %s clocksource has been installed.\n",
-              clock->name);
+       return timespec_to_ktime(now);
 }
-#else
-static inline void change_clocksource(void) { }
-#endif
+EXPORT_SYMBOL_GPL(ktime_get);
+
+/**
+ * ktime_get_ts - get the monotonic clock in timespec format
+ * @ts:                pointer to timespec variable
+ *
+ * The function calculates the monotonic clock from the realtime
+ * clock and the wall_to_monotonic offset and stores the result
+ * in normalized timespec format in the variable pointed to by @ts.
+ */
+void ktime_get_ts(struct timespec *ts)
+{
+       struct timespec tomono;
+       unsigned long seq;
+
+       do {
+               seq = read_seqbegin(&xtime_lock);
+               getnstimeofday(ts);
+               tomono = wall_to_monotonic;
+
+       } while (read_seqretry(&xtime_lock, seq));
+
+       set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
+                               ts->tv_nsec + tomono.tv_nsec);
+}
+EXPORT_SYMBOL_GPL(ktime_get_ts);
+
+#endif /* !GENERIC_TIME */
+
+/**
+ * ktime_get_real - get the real (wall-) time in ktime_t format
+ *
+ * returns the time in ktime_t format
+ */
+ktime_t ktime_get_real(void)
+{
+       struct timespec now;
+
+       getnstimeofday(&now);
+
+       return timespec_to_ktime(now);
+}
+EXPORT_SYMBOL_GPL(ktime_get_real);
+
+/**
+ * getrawmonotonic - Returns the raw monotonic time in a timespec
+ * @ts:                pointer to the timespec to be set
+ *
+ * Returns the raw monotonic time (completely un-modified by ntp)
+ */
+void getrawmonotonic(struct timespec *ts)
+{
+       unsigned long seq;
+       s64 nsecs;
+
+       do {
+               seq = read_seqbegin(&xtime_lock);
+               nsecs = timekeeping_get_ns_raw();
+               *ts = raw_time;
+
+       } while (read_seqretry(&xtime_lock, seq));
+
+       timespec_add_ns(ts, nsecs);
+}
+EXPORT_SYMBOL(getrawmonotonic);
+
 
 /**
- * timekeeping_is_continuous - check to see if timekeeping is free running
+ * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
  */
-int timekeeping_is_continuous(void)
+int timekeeping_valid_for_hres(void)
 {
        unsigned long seq;
        int ret;
@@ -230,7 +480,7 @@ int timekeeping_is_continuous(void)
        do {
                seq = read_seqbegin(&xtime_lock);
 
-               ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
+               ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
 
        } while (read_seqretry(&xtime_lock, seq));
 
@@ -238,17 +488,33 @@ int timekeeping_is_continuous(void)
 }
 
 /**
- * read_persistent_clock -  Return time in seconds from the persistent clock.
+ * read_persistent_clock -  Return time from the persistent clock.
  *
  * Weak dummy function for arches that do not yet support it.
- * Returns seconds from epoch using the battery backed persistent clock.
- * Returns zero if unsupported.
+ * Reads the time from the battery backed persistent clock.
+ * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
  *
  *  XXX - Do be sure to remove it once all arches implement it.
  */
-unsigned long __attribute__((weak)) read_persistent_clock(void)
+void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
 {
-       return 0;
+       ts->tv_sec = 0;
+       ts->tv_nsec = 0;
+}
+
+/**
+ * read_boot_clock -  Return time of the system start.
+ *
+ * Weak dummy function for arches that do not yet support it.
+ * Function to read the exact time the system has been started.
+ * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
+ *
+ *  XXX - Do be sure to remove it once all arches implement it.
+ */
+void __attribute__((weak)) read_boot_clock(struct timespec *ts)
+{
+       ts->tv_sec = 0;
+       ts->tv_nsec = 0;
 }
 
 /*
@@ -256,30 +522,40 @@ unsigned long __attribute__((weak)) read_persistent_clock(void)
  */
 void __init timekeeping_init(void)
 {
+       struct clocksource *clock;
        unsigned long flags;
-       unsigned long sec = read_persistent_clock();
+       struct timespec now, boot;
+
+       read_persistent_clock(&now);
+       read_boot_clock(&boot);
 
        write_seqlock_irqsave(&xtime_lock, flags);
 
-       ntp_clear();
+       ntp_init();
 
-       clock = clocksource_get_next();
-       clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
-       clock->cycle_last = clocksource_read(clock);
+       clock = clocksource_default_clock();
+       if (clock->enable)
+               clock->enable(clock);
+       timekeeper_setup_internals(clock);
 
-       xtime.tv_sec = sec;
-       xtime.tv_nsec = 0;
+       xtime.tv_sec = now.tv_sec;
+       xtime.tv_nsec = now.tv_nsec;
+       raw_time.tv_sec = 0;
+       raw_time.tv_nsec = 0;
+       if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
+               boot.tv_sec = xtime.tv_sec;
+               boot.tv_nsec = xtime.tv_nsec;
+       }
        set_normalized_timespec(&wall_to_monotonic,
-               -xtime.tv_sec, -xtime.tv_nsec);
-       total_sleep_time = 0;
-
+                               -boot.tv_sec, -boot.tv_nsec);
+       update_xtime_cache(0);
+       total_sleep_time.tv_sec = 0;
+       total_sleep_time.tv_nsec = 0;
        write_sequnlock_irqrestore(&xtime_lock, flags);
 }
 
-/* flag for if timekeeping is suspended */
-static int timekeeping_suspended;
 /* time in seconds when suspend began */
-static unsigned long timekeeping_suspend_time;
+static struct timespec timekeeping_suspend_time;
 
 /**
  * timekeeping_resume - Resumes the generic timekeeping subsystem.
@@ -292,22 +568,24 @@ static unsigned long timekeeping_suspend_time;
 static int timekeeping_resume(struct sys_device *dev)
 {
        unsigned long flags;
-       unsigned long now = read_persistent_clock();
+       struct timespec ts;
+
+       read_persistent_clock(&ts);
 
        clocksource_resume();
 
        write_seqlock_irqsave(&xtime_lock, flags);
 
-       if (now && (now > timekeeping_suspend_time)) {
-               unsigned long sleep_length = now - timekeeping_suspend_time;
-
-               xtime.tv_sec += sleep_length;
-               wall_to_monotonic.tv_sec -= sleep_length;
-               total_sleep_time += sleep_length;
+       if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
+               ts = timespec_sub(ts, timekeeping_suspend_time);
+               xtime = timespec_add_safe(xtime, ts);
+               wall_to_monotonic = timespec_sub(wall_to_monotonic, ts);
+               total_sleep_time = timespec_add_safe(total_sleep_time, ts);
        }
+       update_xtime_cache(0);
        /* re-base the last cycle value */
-       clock->cycle_last = clocksource_read(clock);
-       clock->error = 0;
+       timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
+       timekeeper.ntp_error = 0;
        timekeeping_suspended = 0;
        write_sequnlock_irqrestore(&xtime_lock, flags);
 
@@ -325,9 +603,11 @@ static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
 {
        unsigned long flags;
 
+       read_persistent_clock(&timekeeping_suspend_time);
+
        write_seqlock_irqsave(&xtime_lock, flags);
+       timekeeping_forward_now();
        timekeeping_suspended = 1;
-       timekeeping_suspend_time = read_persistent_clock();
        write_sequnlock_irqrestore(&xtime_lock, flags);
 
        clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
@@ -337,9 +617,9 @@ static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
 
 /* sysfs resume/suspend bits for timekeeping */
 static struct sysdev_class timekeeping_sysclass = {
+       .name           = "timekeeping",
        .resume         = timekeeping_resume,
        .suspend        = timekeeping_suspend,
-       set_kset_name("timekeeping"),
 };
 
 static struct sys_device device_timer = {
@@ -361,7 +641,7 @@ device_initcall(timekeeping_init_device);
  * If the error is already larger, we look ahead even further
  * to compensate for late or lost adjustments.
  */
-static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
+static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
                                                 s64 *offset)
 {
        s64 tick_error, i;
@@ -374,10 +654,10 @@ static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
         * with losing too many ticks, otherwise we would overadjust and
         * produce an even larger error.  The smaller the adjustment the
         * faster we try to adjust for it, as lost ticks can do less harm
-        * here.  This is tuned so that an error of about 1 msec is adusted
+        * here.  This is tuned so that an error of about 1 msec is adjusted
         * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
         */
-       error2 = clock->error >> (TICK_LENGTH_SHIFT + 22 - 2 * SHIFT_HZ);
+       error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
        error2 = abs(error2);
        for (look_ahead = 0; error2 > 0; look_ahead++)
                error2 >>= 2;
@@ -386,9 +666,8 @@ static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
         * Now calculate the error in (1 << look_ahead) ticks, but first
         * remove the single look ahead already included in the error.
         */
-       tick_error = current_tick_length() >>
-               (TICK_LENGTH_SHIFT - clock->shift + 1);
-       tick_error -= clock->xtime_interval >> 1;
+       tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
+       tick_error -= timekeeper.xtime_interval >> 1;
        error = ((error - tick_error) >> look_ahead) + tick_error;
 
        /* Finally calculate the adjustment shift value.  */
@@ -413,18 +692,18 @@ static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
  * this is optimized for the most common adjustments of -1,0,1,
  * for other values we can do a bit more work.
  */
-static void clocksource_adjust(s64 offset)
+static void timekeeping_adjust(s64 offset)
 {
-       s64 error, interval = clock->cycle_interval;
+       s64 error, interval = timekeeper.cycle_interval;
        int adj;
 
-       error = clock->error >> (TICK_LENGTH_SHIFT - clock->shift - 1);
+       error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
        if (error > interval) {
                error >>= 2;
                if (likely(error <= interval))
                        adj = 1;
                else
-                       adj = clocksource_bigadjust(error, &interval, &offset);
+                       adj = timekeeping_bigadjust(error, &interval, &offset);
        } else if (error < -interval) {
                error >>= 2;
                if (likely(error >= -interval)) {
@@ -432,15 +711,15 @@ static void clocksource_adjust(s64 offset)
                        interval = -interval;
                        offset = -offset;
                } else
-                       adj = clocksource_bigadjust(error, &interval, &offset);
+                       adj = timekeeping_bigadjust(error, &interval, &offset);
        } else
                return;
 
-       clock->mult += adj;
-       clock->xtime_interval += interval;
-       clock->xtime_nsec -= offset;
-       clock->error -= (interval - offset) <<
-                       (TICK_LENGTH_SHIFT - clock->shift);
+       timekeeper.mult += adj;
+       timekeeper.xtime_interval += interval;
+       timekeeper.xtime_nsec -= offset;
+       timekeeper.ntp_error -= (interval - offset) <<
+                               timekeeper.ntp_error_shift;
 }
 
 /**
@@ -450,51 +729,89 @@ static void clocksource_adjust(s64 offset)
  */
 void update_wall_time(void)
 {
+       struct clocksource *clock;
        cycle_t offset;
+       u64 nsecs;
 
        /* Make sure we're fully resumed: */
        if (unlikely(timekeeping_suspended))
                return;
 
+       clock = timekeeper.clock;
 #ifdef CONFIG_GENERIC_TIME
-       offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
+       offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
 #else
-       offset = clock->cycle_interval;
+       offset = timekeeper.cycle_interval;
 #endif
-       clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift;
+       timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
 
        /* normally this loop will run just once, however in the
         * case of lost or late ticks, it will accumulate correctly.
         */
-       while (offset >= clock->cycle_interval) {
+       while (offset >= timekeeper.cycle_interval) {
+               u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
+
                /* accumulate one interval */
-               clock->xtime_nsec += clock->xtime_interval;
-               clock->cycle_last += clock->cycle_interval;
-               offset -= clock->cycle_interval;
+               offset -= timekeeper.cycle_interval;
+               clock->cycle_last += timekeeper.cycle_interval;
 
-               if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
-                       clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
+               timekeeper.xtime_nsec += timekeeper.xtime_interval;
+               if (timekeeper.xtime_nsec >= nsecps) {
+                       timekeeper.xtime_nsec -= nsecps;
                        xtime.tv_sec++;
                        second_overflow();
                }
 
+               raw_time.tv_nsec += timekeeper.raw_interval;
+               if (raw_time.tv_nsec >= NSEC_PER_SEC) {
+                       raw_time.tv_nsec -= NSEC_PER_SEC;
+                       raw_time.tv_sec++;
+               }
+
                /* accumulate error between NTP and clock interval */
-               clock->error += current_tick_length();
-               clock->error -= clock->xtime_interval << (TICK_LENGTH_SHIFT - clock->shift);
+               timekeeper.ntp_error += tick_length;
+               timekeeper.ntp_error -= timekeeper.xtime_interval <<
+                                       timekeeper.ntp_error_shift;
        }
 
        /* correct the clock when NTP error is too big */
-       clocksource_adjust(offset);
+       timekeeping_adjust(offset);
+
+       /*
+        * Since in the loop above, we accumulate any amount of time
+        * in xtime_nsec over a second into xtime.tv_sec, its possible for
+        * xtime_nsec to be fairly small after the loop. Further, if we're
+        * slightly speeding the clocksource up in timekeeping_adjust(),
+        * its possible the required corrective factor to xtime_nsec could
+        * cause it to underflow.
+        *
+        * Now, we cannot simply roll the accumulated second back, since
+        * the NTP subsystem has been notified via second_overflow. So
+        * instead we push xtime_nsec forward by the amount we underflowed,
+        * and add that amount into the error.
+        *
+        * We'll correct this error next time through this function, when
+        * xtime_nsec is not as small.
+        */
+       if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
+               s64 neg = -(s64)timekeeper.xtime_nsec;
+               timekeeper.xtime_nsec = 0;
+               timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
+       }
 
-       /* store full nanoseconds into xtime */
-       xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift;
-       clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
+       /* store full nanoseconds into xtime after rounding it up and
+        * add the remainder to the error difference.
+        */
+       xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
+       timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
+       timekeeper.ntp_error += timekeeper.xtime_nsec <<
+                               timekeeper.ntp_error_shift;
 
-       update_xtime_cache(cyc2ns(clock, offset));
+       nsecs = clocksource_cyc2ns(offset, timekeeper.mult, timekeeper.shift);
+       update_xtime_cache(nsecs);
 
        /* check to see if there is a new clocksource to use */
-       change_clocksource();
-       update_vsyscall(&xtime, clock);
+       update_vsyscall(&xtime, timekeeper.clock);
 }
 
 /**
@@ -510,9 +827,12 @@ void update_wall_time(void)
  */
 void getboottime(struct timespec *ts)
 {
-       set_normalized_timespec(ts,
-               - (wall_to_monotonic.tv_sec + total_sleep_time),
-               - wall_to_monotonic.tv_nsec);
+       struct timespec boottime = {
+               .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec,
+               .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec
+       };
+
+       set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
 }
 
 /**
@@ -521,7 +841,7 @@ void getboottime(struct timespec *ts)
  */
 void monotonic_to_bootbased(struct timespec *ts)
 {
-       ts->tv_sec += total_sleep_time;
+       *ts = timespec_add_safe(*ts, total_sleep_time);
 }
 
 unsigned long get_seconds(void)
@@ -530,6 +850,10 @@ unsigned long get_seconds(void)
 }
 EXPORT_SYMBOL(get_seconds);
 
+struct timespec __current_kernel_time(void)
+{
+       return xtime_cache;
+}
 
 struct timespec current_kernel_time(void)
 {
@@ -545,3 +869,20 @@ struct timespec current_kernel_time(void)
        return now;
 }
 EXPORT_SYMBOL(current_kernel_time);
+
+struct timespec get_monotonic_coarse(void)
+{
+       struct timespec now, mono;
+       unsigned long seq;
+
+       do {
+               seq = read_seqbegin(&xtime_lock);
+
+               now = xtime_cache;
+               mono = wall_to_monotonic;
+       } while (read_seqretry(&xtime_lock, seq));
+
+       set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
+                               now.tv_nsec + mono.tv_nsec);
+       return now;
+}