18524070bSjohn stultz /* 28524070bSjohn stultz * linux/kernel/time/timekeeping.c 38524070bSjohn stultz * 48524070bSjohn stultz * Kernel timekeeping code and accessor functions 58524070bSjohn stultz * 68524070bSjohn stultz * This code was moved from linux/kernel/timer.c. 78524070bSjohn stultz * Please see that file for copyright and history logs. 88524070bSjohn stultz * 98524070bSjohn stultz */ 108524070bSjohn stultz 11d7b4202eSJohn Stultz #include <linux/timekeeper_internal.h> 128524070bSjohn stultz #include <linux/module.h> 138524070bSjohn stultz #include <linux/interrupt.h> 148524070bSjohn stultz #include <linux/percpu.h> 158524070bSjohn stultz #include <linux/init.h> 168524070bSjohn stultz #include <linux/mm.h> 1738b8d208SIngo Molnar #include <linux/nmi.h> 18d43c36dcSAlexey Dobriyan #include <linux/sched.h> 194f17722cSIngo Molnar #include <linux/sched/loadavg.h> 20e1a85b2cSRafael J. Wysocki #include <linux/syscore_ops.h> 218524070bSjohn stultz #include <linux/clocksource.h> 228524070bSjohn stultz #include <linux/jiffies.h> 238524070bSjohn stultz #include <linux/time.h> 248524070bSjohn stultz #include <linux/tick.h> 2575c5158fSMartin Schwidefsky #include <linux/stop_machine.h> 26e0b306feSMarcelo Tosatti #include <linux/pvclock_gtod.h> 2752f5684cSGideon Israel Dsouza #include <linux/compiler.h> 288524070bSjohn stultz 29eb93e4d9SThomas Gleixner #include "tick-internal.h" 30aa6f9c59SJohn Stultz #include "ntp_internal.h" 315c83545fSColin Cross #include "timekeeping_internal.h" 32155ec602SMartin Schwidefsky 3304397fe9SDavid Vrabel #define TK_CLEAR_NTP (1 << 0) 3404397fe9SDavid Vrabel #define TK_MIRROR (1 << 1) 35780427f0SDavid Vrabel #define TK_CLOCK_WAS_SET (1 << 2) 3604397fe9SDavid Vrabel 373fdb14fdSThomas Gleixner /* 383fdb14fdSThomas Gleixner * The most important data for readout fits into a single 64 byte 393fdb14fdSThomas Gleixner * cache line. 403fdb14fdSThomas Gleixner */ 413fdb14fdSThomas Gleixner static struct { 423fdb14fdSThomas Gleixner seqcount_t seq; 433fdb14fdSThomas Gleixner struct timekeeper timekeeper; 443fdb14fdSThomas Gleixner } tk_core ____cacheline_aligned; 453fdb14fdSThomas Gleixner 469a7a71b1SThomas Gleixner static DEFINE_RAW_SPINLOCK(timekeeper_lock); 4748cdc135SThomas Gleixner static struct timekeeper shadow_timekeeper; 48155ec602SMartin Schwidefsky 494396e058SThomas Gleixner /** 504396e058SThomas Gleixner * struct tk_fast - NMI safe timekeeper 514396e058SThomas Gleixner * @seq: Sequence counter for protecting updates. The lowest bit 524396e058SThomas Gleixner * is the index for the tk_read_base array 534396e058SThomas Gleixner * @base: tk_read_base array. Access is indexed by the lowest bit of 544396e058SThomas Gleixner * @seq. 554396e058SThomas Gleixner * 564396e058SThomas Gleixner * See @update_fast_timekeeper() below. 574396e058SThomas Gleixner */ 584396e058SThomas Gleixner struct tk_fast { 594396e058SThomas Gleixner seqcount_t seq; 604396e058SThomas Gleixner struct tk_read_base base[2]; 614396e058SThomas Gleixner }; 624396e058SThomas Gleixner 635df32107SPrarit Bhargava /* Suspend-time cycles value for halted fast timekeeper. */ 645df32107SPrarit Bhargava static u64 cycles_at_suspend; 655df32107SPrarit Bhargava 665df32107SPrarit Bhargava static u64 dummy_clock_read(struct clocksource *cs) 675df32107SPrarit Bhargava { 685df32107SPrarit Bhargava return cycles_at_suspend; 695df32107SPrarit Bhargava } 705df32107SPrarit Bhargava 715df32107SPrarit Bhargava static struct clocksource dummy_clock = { 725df32107SPrarit Bhargava .read = dummy_clock_read, 735df32107SPrarit Bhargava }; 745df32107SPrarit Bhargava 755df32107SPrarit Bhargava static struct tk_fast tk_fast_mono ____cacheline_aligned = { 765df32107SPrarit Bhargava .base[0] = { .clock = &dummy_clock, }, 775df32107SPrarit Bhargava .base[1] = { .clock = &dummy_clock, }, 785df32107SPrarit Bhargava }; 795df32107SPrarit Bhargava 805df32107SPrarit Bhargava static struct tk_fast tk_fast_raw ____cacheline_aligned = { 815df32107SPrarit Bhargava .base[0] = { .clock = &dummy_clock, }, 825df32107SPrarit Bhargava .base[1] = { .clock = &dummy_clock, }, 835df32107SPrarit Bhargava }; 844396e058SThomas Gleixner 858fcce546SJohn Stultz /* flag for if timekeeping is suspended */ 868fcce546SJohn Stultz int __read_mostly timekeeping_suspended; 878fcce546SJohn Stultz 881e75fa8bSJohn Stultz static inline void tk_normalize_xtime(struct timekeeper *tk) 891e75fa8bSJohn Stultz { 90876e7881SPeter Zijlstra while (tk->tkr_mono.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_mono.shift)) { 91876e7881SPeter Zijlstra tk->tkr_mono.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_mono.shift; 921e75fa8bSJohn Stultz tk->xtime_sec++; 931e75fa8bSJohn Stultz } 94fc6eead7SJohn Stultz while (tk->tkr_raw.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_raw.shift)) { 95fc6eead7SJohn Stultz tk->tkr_raw.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_raw.shift; 96fc6eead7SJohn Stultz tk->raw_sec++; 97fc6eead7SJohn Stultz } 981e75fa8bSJohn Stultz } 998fcce546SJohn Stultz 100c905fae4SThomas Gleixner static inline struct timespec64 tk_xtime(struct timekeeper *tk) 101c905fae4SThomas Gleixner { 102c905fae4SThomas Gleixner struct timespec64 ts; 103c905fae4SThomas Gleixner 104c905fae4SThomas Gleixner ts.tv_sec = tk->xtime_sec; 105876e7881SPeter Zijlstra ts.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift); 106c905fae4SThomas Gleixner return ts; 107c905fae4SThomas Gleixner } 108c905fae4SThomas Gleixner 1097d489d15SJohn Stultz static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts) 1101e75fa8bSJohn Stultz { 1111e75fa8bSJohn Stultz tk->xtime_sec = ts->tv_sec; 112876e7881SPeter Zijlstra tk->tkr_mono.xtime_nsec = (u64)ts->tv_nsec << tk->tkr_mono.shift; 1131e75fa8bSJohn Stultz } 1141e75fa8bSJohn Stultz 1157d489d15SJohn Stultz static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts) 1161e75fa8bSJohn Stultz { 1171e75fa8bSJohn Stultz tk->xtime_sec += ts->tv_sec; 118876e7881SPeter Zijlstra tk->tkr_mono.xtime_nsec += (u64)ts->tv_nsec << tk->tkr_mono.shift; 119784ffcbbSJohn Stultz tk_normalize_xtime(tk); 1201e75fa8bSJohn Stultz } 1218fcce546SJohn Stultz 1227d489d15SJohn Stultz static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm) 1236d0ef903SJohn Stultz { 1247d489d15SJohn Stultz struct timespec64 tmp; 1256d0ef903SJohn Stultz 1266d0ef903SJohn Stultz /* 1276d0ef903SJohn Stultz * Verify consistency of: offset_real = -wall_to_monotonic 1286d0ef903SJohn Stultz * before modifying anything 1296d0ef903SJohn Stultz */ 1307d489d15SJohn Stultz set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec, 1316d0ef903SJohn Stultz -tk->wall_to_monotonic.tv_nsec); 1322456e855SThomas Gleixner WARN_ON_ONCE(tk->offs_real != timespec64_to_ktime(tmp)); 1336d0ef903SJohn Stultz tk->wall_to_monotonic = wtm; 1347d489d15SJohn Stultz set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec); 1357d489d15SJohn Stultz tk->offs_real = timespec64_to_ktime(tmp); 13604005f60SJohn Stultz tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0)); 1376d0ef903SJohn Stultz } 1386d0ef903SJohn Stultz 13947da70d3SThomas Gleixner static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta) 1406d0ef903SJohn Stultz { 141a3ed0e43SThomas Gleixner tk->offs_boot = ktime_add(tk->offs_boot, delta); 1426d0ef903SJohn Stultz } 1436d0ef903SJohn Stultz 144ceea5e37SJohn Stultz /* 145ceea5e37SJohn Stultz * tk_clock_read - atomic clocksource read() helper 146ceea5e37SJohn Stultz * 147ceea5e37SJohn Stultz * This helper is necessary to use in the read paths because, while the 148ceea5e37SJohn Stultz * seqlock ensures we don't return a bad value while structures are updated, 149ceea5e37SJohn Stultz * it doesn't protect from potential crashes. There is the possibility that 150ceea5e37SJohn Stultz * the tkr's clocksource may change between the read reference, and the 151ceea5e37SJohn Stultz * clock reference passed to the read function. This can cause crashes if 152ceea5e37SJohn Stultz * the wrong clocksource is passed to the wrong read function. 153ceea5e37SJohn Stultz * This isn't necessary to use when holding the timekeeper_lock or doing 154ceea5e37SJohn Stultz * a read of the fast-timekeeper tkrs (which is protected by its own locking 155ceea5e37SJohn Stultz * and update logic). 156ceea5e37SJohn Stultz */ 157ceea5e37SJohn Stultz static inline u64 tk_clock_read(struct tk_read_base *tkr) 158ceea5e37SJohn Stultz { 159ceea5e37SJohn Stultz struct clocksource *clock = READ_ONCE(tkr->clock); 160ceea5e37SJohn Stultz 161ceea5e37SJohn Stultz return clock->read(clock); 162ceea5e37SJohn Stultz } 163ceea5e37SJohn Stultz 1643c17ad19SJohn Stultz #ifdef CONFIG_DEBUG_TIMEKEEPING 1654ca22c26SJohn Stultz #define WARNING_FREQ (HZ*300) /* 5 minute rate-limiting */ 1664ca22c26SJohn Stultz 167a5a1d1c2SThomas Gleixner static void timekeeping_check_update(struct timekeeper *tk, u64 offset) 1683c17ad19SJohn Stultz { 1693c17ad19SJohn Stultz 170a5a1d1c2SThomas Gleixner u64 max_cycles = tk->tkr_mono.clock->max_cycles; 171876e7881SPeter Zijlstra const char *name = tk->tkr_mono.clock->name; 1723c17ad19SJohn Stultz 1733c17ad19SJohn Stultz if (offset > max_cycles) { 174a558cd02SJohn Stultz printk_deferred("WARNING: timekeeping: Cycle offset (%lld) is larger than allowed by the '%s' clock's max_cycles value (%lld): time overflow danger\n", 1753c17ad19SJohn Stultz offset, name, max_cycles); 176a558cd02SJohn Stultz printk_deferred(" timekeeping: Your kernel is sick, but tries to cope by capping time updates\n"); 1773c17ad19SJohn Stultz } else { 1783c17ad19SJohn Stultz if (offset > (max_cycles >> 1)) { 179fc4fa6e1SMasanari Iida printk_deferred("INFO: timekeeping: Cycle offset (%lld) is larger than the '%s' clock's 50%% safety margin (%lld)\n", 1803c17ad19SJohn Stultz offset, name, max_cycles >> 1); 1813c17ad19SJohn Stultz printk_deferred(" timekeeping: Your kernel is still fine, but is feeling a bit nervous\n"); 1823c17ad19SJohn Stultz } 1833c17ad19SJohn Stultz } 1844ca22c26SJohn Stultz 18557d05a93SJohn Stultz if (tk->underflow_seen) { 18657d05a93SJohn Stultz if (jiffies - tk->last_warning > WARNING_FREQ) { 1874ca22c26SJohn Stultz printk_deferred("WARNING: Underflow in clocksource '%s' observed, time update ignored.\n", name); 1884ca22c26SJohn Stultz printk_deferred(" Please report this, consider using a different clocksource, if possible.\n"); 1894ca22c26SJohn Stultz printk_deferred(" Your kernel is probably still fine.\n"); 19057d05a93SJohn Stultz tk->last_warning = jiffies; 1914ca22c26SJohn Stultz } 19257d05a93SJohn Stultz tk->underflow_seen = 0; 1934ca22c26SJohn Stultz } 1944ca22c26SJohn Stultz 19557d05a93SJohn Stultz if (tk->overflow_seen) { 19657d05a93SJohn Stultz if (jiffies - tk->last_warning > WARNING_FREQ) { 1974ca22c26SJohn Stultz printk_deferred("WARNING: Overflow in clocksource '%s' observed, time update capped.\n", name); 1984ca22c26SJohn Stultz printk_deferred(" Please report this, consider using a different clocksource, if possible.\n"); 1994ca22c26SJohn Stultz printk_deferred(" Your kernel is probably still fine.\n"); 20057d05a93SJohn Stultz tk->last_warning = jiffies; 2014ca22c26SJohn Stultz } 20257d05a93SJohn Stultz tk->overflow_seen = 0; 2034ca22c26SJohn Stultz } 2043c17ad19SJohn Stultz } 205a558cd02SJohn Stultz 206a5a1d1c2SThomas Gleixner static inline u64 timekeeping_get_delta(struct tk_read_base *tkr) 207a558cd02SJohn Stultz { 20857d05a93SJohn Stultz struct timekeeper *tk = &tk_core.timekeeper; 209a5a1d1c2SThomas Gleixner u64 now, last, mask, max, delta; 2104ca22c26SJohn Stultz unsigned int seq; 211a558cd02SJohn Stultz 2124ca22c26SJohn Stultz /* 2134ca22c26SJohn Stultz * Since we're called holding a seqlock, the data may shift 2144ca22c26SJohn Stultz * under us while we're doing the calculation. This can cause 2154ca22c26SJohn Stultz * false positives, since we'd note a problem but throw the 2164ca22c26SJohn Stultz * results away. So nest another seqlock here to atomically 2174ca22c26SJohn Stultz * grab the points we are checking with. 2184ca22c26SJohn Stultz */ 2194ca22c26SJohn Stultz do { 2204ca22c26SJohn Stultz seq = read_seqcount_begin(&tk_core.seq); 221ceea5e37SJohn Stultz now = tk_clock_read(tkr); 2224ca22c26SJohn Stultz last = tkr->cycle_last; 2234ca22c26SJohn Stultz mask = tkr->mask; 2244ca22c26SJohn Stultz max = tkr->clock->max_cycles; 2254ca22c26SJohn Stultz } while (read_seqcount_retry(&tk_core.seq, seq)); 226a558cd02SJohn Stultz 2274ca22c26SJohn Stultz delta = clocksource_delta(now, last, mask); 228a558cd02SJohn Stultz 229057b87e3SJohn Stultz /* 230057b87e3SJohn Stultz * Try to catch underflows by checking if we are seeing small 231057b87e3SJohn Stultz * mask-relative negative values. 232057b87e3SJohn Stultz */ 2334ca22c26SJohn Stultz if (unlikely((~delta & mask) < (mask >> 3))) { 23457d05a93SJohn Stultz tk->underflow_seen = 1; 235057b87e3SJohn Stultz delta = 0; 2364ca22c26SJohn Stultz } 237057b87e3SJohn Stultz 238a558cd02SJohn Stultz /* Cap delta value to the max_cycles values to avoid mult overflows */ 2394ca22c26SJohn Stultz if (unlikely(delta > max)) { 24057d05a93SJohn Stultz tk->overflow_seen = 1; 241a558cd02SJohn Stultz delta = tkr->clock->max_cycles; 2424ca22c26SJohn Stultz } 243a558cd02SJohn Stultz 244a558cd02SJohn Stultz return delta; 245a558cd02SJohn Stultz } 2463c17ad19SJohn Stultz #else 247a5a1d1c2SThomas Gleixner static inline void timekeeping_check_update(struct timekeeper *tk, u64 offset) 2483c17ad19SJohn Stultz { 2493c17ad19SJohn Stultz } 250a5a1d1c2SThomas Gleixner static inline u64 timekeeping_get_delta(struct tk_read_base *tkr) 251a558cd02SJohn Stultz { 252a5a1d1c2SThomas Gleixner u64 cycle_now, delta; 253a558cd02SJohn Stultz 254a558cd02SJohn Stultz /* read clocksource */ 255ceea5e37SJohn Stultz cycle_now = tk_clock_read(tkr); 256a558cd02SJohn Stultz 257a558cd02SJohn Stultz /* calculate the delta since the last update_wall_time */ 258a558cd02SJohn Stultz delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask); 259a558cd02SJohn Stultz 260a558cd02SJohn Stultz return delta; 261a558cd02SJohn Stultz } 2623c17ad19SJohn Stultz #endif 2633c17ad19SJohn Stultz 264155ec602SMartin Schwidefsky /** 265d26e4fe0SYijing Wang * tk_setup_internals - Set up internals to use clocksource clock. 266155ec602SMartin Schwidefsky * 267d26e4fe0SYijing Wang * @tk: The target timekeeper to setup. 268155ec602SMartin Schwidefsky * @clock: Pointer to clocksource. 269155ec602SMartin Schwidefsky * 270155ec602SMartin Schwidefsky * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment 271155ec602SMartin Schwidefsky * pair and interval request. 272155ec602SMartin Schwidefsky * 273155ec602SMartin Schwidefsky * Unless you're the timekeeping code, you should not be using this! 274155ec602SMartin Schwidefsky */ 275f726a697SJohn Stultz static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) 276155ec602SMartin Schwidefsky { 277a5a1d1c2SThomas Gleixner u64 interval; 278a386b5afSKasper Pedersen u64 tmp, ntpinterval; 2791e75fa8bSJohn Stultz struct clocksource *old_clock; 280155ec602SMartin Schwidefsky 2812c756febSChristopher S. Hall ++tk->cs_was_changed_seq; 282876e7881SPeter Zijlstra old_clock = tk->tkr_mono.clock; 283876e7881SPeter Zijlstra tk->tkr_mono.clock = clock; 284876e7881SPeter Zijlstra tk->tkr_mono.mask = clock->mask; 285ceea5e37SJohn Stultz tk->tkr_mono.cycle_last = tk_clock_read(&tk->tkr_mono); 286155ec602SMartin Schwidefsky 2874a4ad80dSPeter Zijlstra tk->tkr_raw.clock = clock; 2884a4ad80dSPeter Zijlstra tk->tkr_raw.mask = clock->mask; 2894a4ad80dSPeter Zijlstra tk->tkr_raw.cycle_last = tk->tkr_mono.cycle_last; 2904a4ad80dSPeter Zijlstra 291155ec602SMartin Schwidefsky /* Do the ns -> cycle conversion first, using original mult */ 292155ec602SMartin Schwidefsky tmp = NTP_INTERVAL_LENGTH; 293155ec602SMartin Schwidefsky tmp <<= clock->shift; 294a386b5afSKasper Pedersen ntpinterval = tmp; 2950a544198SMartin Schwidefsky tmp += clock->mult/2; 2960a544198SMartin Schwidefsky do_div(tmp, clock->mult); 297155ec602SMartin Schwidefsky if (tmp == 0) 298155ec602SMartin Schwidefsky tmp = 1; 299155ec602SMartin Schwidefsky 300a5a1d1c2SThomas Gleixner interval = (u64) tmp; 301f726a697SJohn Stultz tk->cycle_interval = interval; 302155ec602SMartin Schwidefsky 303155ec602SMartin Schwidefsky /* Go back from cycles -> shifted ns */ 304cbd99e3bSThomas Gleixner tk->xtime_interval = interval * clock->mult; 305f726a697SJohn Stultz tk->xtime_remainder = ntpinterval - tk->xtime_interval; 3063d88d56cSJohn Stultz tk->raw_interval = interval * clock->mult; 307155ec602SMartin Schwidefsky 3081e75fa8bSJohn Stultz /* if changing clocks, convert xtime_nsec shift units */ 3091e75fa8bSJohn Stultz if (old_clock) { 3101e75fa8bSJohn Stultz int shift_change = clock->shift - old_clock->shift; 311fc6eead7SJohn Stultz if (shift_change < 0) { 312876e7881SPeter Zijlstra tk->tkr_mono.xtime_nsec >>= -shift_change; 313fc6eead7SJohn Stultz tk->tkr_raw.xtime_nsec >>= -shift_change; 314fc6eead7SJohn Stultz } else { 315876e7881SPeter Zijlstra tk->tkr_mono.xtime_nsec <<= shift_change; 316fc6eead7SJohn Stultz tk->tkr_raw.xtime_nsec <<= shift_change; 3171e75fa8bSJohn Stultz } 318fc6eead7SJohn Stultz } 3194a4ad80dSPeter Zijlstra 320876e7881SPeter Zijlstra tk->tkr_mono.shift = clock->shift; 3214a4ad80dSPeter Zijlstra tk->tkr_raw.shift = clock->shift; 322155ec602SMartin Schwidefsky 323f726a697SJohn Stultz tk->ntp_error = 0; 324f726a697SJohn Stultz tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift; 325375f45b5SJohn Stultz tk->ntp_tick = ntpinterval << tk->ntp_error_shift; 3260a544198SMartin Schwidefsky 3270a544198SMartin Schwidefsky /* 3280a544198SMartin Schwidefsky * The timekeeper keeps its own mult values for the currently 3290a544198SMartin Schwidefsky * active clocksource. These value will be adjusted via NTP 3300a544198SMartin Schwidefsky * to counteract clock drifting. 3310a544198SMartin Schwidefsky */ 332876e7881SPeter Zijlstra tk->tkr_mono.mult = clock->mult; 3334a4ad80dSPeter Zijlstra tk->tkr_raw.mult = clock->mult; 334dc491596SJohn Stultz tk->ntp_err_mult = 0; 33578b98e3cSMiroslav Lichvar tk->skip_second_overflow = 0; 336155ec602SMartin Schwidefsky } 3378524070bSjohn stultz 3382ba2a305SMartin Schwidefsky /* Timekeeper helper functions. */ 3397b1f6207SStephen Warren 3407b1f6207SStephen Warren #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET 341e06fde37SThomas Gleixner static u32 default_arch_gettimeoffset(void) { return 0; } 342e06fde37SThomas Gleixner u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset; 3437b1f6207SStephen Warren #else 344e06fde37SThomas Gleixner static inline u32 arch_gettimeoffset(void) { return 0; } 3457b1f6207SStephen Warren #endif 3467b1f6207SStephen Warren 347a5a1d1c2SThomas Gleixner static inline u64 timekeeping_delta_to_ns(struct tk_read_base *tkr, u64 delta) 3482ba2a305SMartin Schwidefsky { 3499c164572SThomas Gleixner u64 nsec; 3502ba2a305SMartin Schwidefsky 3516bd58f09SChristopher S. Hall nsec = delta * tkr->mult + tkr->xtime_nsec; 3526bd58f09SChristopher S. Hall nsec >>= tkr->shift; 353f2a5a085SJohn Stultz 3547b1f6207SStephen Warren /* If arch requires, add in get_arch_timeoffset() */ 355e06fde37SThomas Gleixner return nsec + arch_gettimeoffset(); 3562ba2a305SMartin Schwidefsky } 3572ba2a305SMartin Schwidefsky 358acc89612SThomas Gleixner static inline u64 timekeeping_get_ns(struct tk_read_base *tkr) 3596bd58f09SChristopher S. Hall { 360a5a1d1c2SThomas Gleixner u64 delta; 3616bd58f09SChristopher S. Hall 3626bd58f09SChristopher S. Hall delta = timekeeping_get_delta(tkr); 3636bd58f09SChristopher S. Hall return timekeeping_delta_to_ns(tkr, delta); 3646bd58f09SChristopher S. Hall } 3656bd58f09SChristopher S. Hall 366a5a1d1c2SThomas Gleixner static inline u64 timekeeping_cycles_to_ns(struct tk_read_base *tkr, u64 cycles) 3676bd58f09SChristopher S. Hall { 368a5a1d1c2SThomas Gleixner u64 delta; 3696bd58f09SChristopher S. Hall 3706bd58f09SChristopher S. Hall /* calculate the delta since the last update_wall_time */ 3716bd58f09SChristopher S. Hall delta = clocksource_delta(cycles, tkr->cycle_last, tkr->mask); 3726bd58f09SChristopher S. Hall return timekeeping_delta_to_ns(tkr, delta); 3736bd58f09SChristopher S. Hall } 3746bd58f09SChristopher S. Hall 3754396e058SThomas Gleixner /** 3764396e058SThomas Gleixner * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper. 377affe3e85SRafael J. Wysocki * @tkr: Timekeeping readout base from which we take the update 3784396e058SThomas Gleixner * 3794396e058SThomas Gleixner * We want to use this from any context including NMI and tracing / 3804396e058SThomas Gleixner * instrumenting the timekeeping code itself. 3814396e058SThomas Gleixner * 3826695b92aSPeter Zijlstra * Employ the latch technique; see @raw_write_seqcount_latch. 3834396e058SThomas Gleixner * 3844396e058SThomas Gleixner * So if a NMI hits the update of base[0] then it will use base[1] 3854396e058SThomas Gleixner * which is still consistent. In the worst case this can result is a 3864396e058SThomas Gleixner * slightly wrong timestamp (a few nanoseconds). See 3874396e058SThomas Gleixner * @ktime_get_mono_fast_ns. 3884396e058SThomas Gleixner */ 3894498e746SPeter Zijlstra static void update_fast_timekeeper(struct tk_read_base *tkr, struct tk_fast *tkf) 3904396e058SThomas Gleixner { 3914498e746SPeter Zijlstra struct tk_read_base *base = tkf->base; 3924396e058SThomas Gleixner 3934396e058SThomas Gleixner /* Force readers off to base[1] */ 3944498e746SPeter Zijlstra raw_write_seqcount_latch(&tkf->seq); 3954396e058SThomas Gleixner 3964396e058SThomas Gleixner /* Update base[0] */ 397affe3e85SRafael J. Wysocki memcpy(base, tkr, sizeof(*base)); 3984396e058SThomas Gleixner 3994396e058SThomas Gleixner /* Force readers back to base[0] */ 4004498e746SPeter Zijlstra raw_write_seqcount_latch(&tkf->seq); 4014396e058SThomas Gleixner 4024396e058SThomas Gleixner /* Update base[1] */ 4034396e058SThomas Gleixner memcpy(base + 1, base, sizeof(*base)); 4044396e058SThomas Gleixner } 4054396e058SThomas Gleixner 4064396e058SThomas Gleixner /** 4074396e058SThomas Gleixner * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic 4084396e058SThomas Gleixner * 4094396e058SThomas Gleixner * This timestamp is not guaranteed to be monotonic across an update. 4104396e058SThomas Gleixner * The timestamp is calculated by: 4114396e058SThomas Gleixner * 4124396e058SThomas Gleixner * now = base_mono + clock_delta * slope 4134396e058SThomas Gleixner * 4144396e058SThomas Gleixner * So if the update lowers the slope, readers who are forced to the 4154396e058SThomas Gleixner * not yet updated second array are still using the old steeper slope. 4164396e058SThomas Gleixner * 4174396e058SThomas Gleixner * tmono 4184396e058SThomas Gleixner * ^ 4194396e058SThomas Gleixner * | o n 4204396e058SThomas Gleixner * | o n 4214396e058SThomas Gleixner * | u 4224396e058SThomas Gleixner * | o 4234396e058SThomas Gleixner * |o 4244396e058SThomas Gleixner * |12345678---> reader order 4254396e058SThomas Gleixner * 4264396e058SThomas Gleixner * o = old slope 4274396e058SThomas Gleixner * u = update 4284396e058SThomas Gleixner * n = new slope 4294396e058SThomas Gleixner * 4304396e058SThomas Gleixner * So reader 6 will observe time going backwards versus reader 5. 4314396e058SThomas Gleixner * 4324396e058SThomas Gleixner * While other CPUs are likely to be able observe that, the only way 4334396e058SThomas Gleixner * for a CPU local observation is when an NMI hits in the middle of 4344396e058SThomas Gleixner * the update. Timestamps taken from that NMI context might be ahead 4354396e058SThomas Gleixner * of the following timestamps. Callers need to be aware of that and 4364396e058SThomas Gleixner * deal with it. 4374396e058SThomas Gleixner */ 4384498e746SPeter Zijlstra static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf) 4394396e058SThomas Gleixner { 4404396e058SThomas Gleixner struct tk_read_base *tkr; 4414396e058SThomas Gleixner unsigned int seq; 4424396e058SThomas Gleixner u64 now; 4434396e058SThomas Gleixner 4444396e058SThomas Gleixner do { 4457fc26327SPeter Zijlstra seq = raw_read_seqcount_latch(&tkf->seq); 4464498e746SPeter Zijlstra tkr = tkf->base + (seq & 0x01); 44727727df2SJohn Stultz now = ktime_to_ns(tkr->base); 44827727df2SJohn Stultz 44958bfea95SJohn Stultz now += timekeeping_delta_to_ns(tkr, 45058bfea95SJohn Stultz clocksource_delta( 451ceea5e37SJohn Stultz tk_clock_read(tkr), 45258bfea95SJohn Stultz tkr->cycle_last, 45358bfea95SJohn Stultz tkr->mask)); 4544498e746SPeter Zijlstra } while (read_seqcount_retry(&tkf->seq, seq)); 4554396e058SThomas Gleixner 4564396e058SThomas Gleixner return now; 4574396e058SThomas Gleixner } 4584498e746SPeter Zijlstra 4594498e746SPeter Zijlstra u64 ktime_get_mono_fast_ns(void) 4604498e746SPeter Zijlstra { 4614498e746SPeter Zijlstra return __ktime_get_fast_ns(&tk_fast_mono); 4624498e746SPeter Zijlstra } 4634396e058SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns); 4644396e058SThomas Gleixner 465f09cb9a1SPeter Zijlstra u64 ktime_get_raw_fast_ns(void) 466f09cb9a1SPeter Zijlstra { 467f09cb9a1SPeter Zijlstra return __ktime_get_fast_ns(&tk_fast_raw); 468f09cb9a1SPeter Zijlstra } 469f09cb9a1SPeter Zijlstra EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns); 470f09cb9a1SPeter Zijlstra 471a3ed0e43SThomas Gleixner /** 472a3ed0e43SThomas Gleixner * ktime_get_boot_fast_ns - NMI safe and fast access to boot clock. 473a3ed0e43SThomas Gleixner * 474a3ed0e43SThomas Gleixner * To keep it NMI safe since we're accessing from tracing, we're not using a 475a3ed0e43SThomas Gleixner * separate timekeeper with updates to monotonic clock and boot offset 476a3ed0e43SThomas Gleixner * protected with seqlocks. This has the following minor side effects: 477a3ed0e43SThomas Gleixner * 478a3ed0e43SThomas Gleixner * (1) Its possible that a timestamp be taken after the boot offset is updated 479a3ed0e43SThomas Gleixner * but before the timekeeper is updated. If this happens, the new boot offset 480a3ed0e43SThomas Gleixner * is added to the old timekeeping making the clock appear to update slightly 481a3ed0e43SThomas Gleixner * earlier: 482a3ed0e43SThomas Gleixner * CPU 0 CPU 1 483a3ed0e43SThomas Gleixner * timekeeping_inject_sleeptime64() 484a3ed0e43SThomas Gleixner * __timekeeping_inject_sleeptime(tk, delta); 485a3ed0e43SThomas Gleixner * timestamp(); 486a3ed0e43SThomas Gleixner * timekeeping_update(tk, TK_CLEAR_NTP...); 487a3ed0e43SThomas Gleixner * 488a3ed0e43SThomas Gleixner * (2) On 32-bit systems, the 64-bit boot offset (tk->offs_boot) may be 489a3ed0e43SThomas Gleixner * partially updated. Since the tk->offs_boot update is a rare event, this 490a3ed0e43SThomas Gleixner * should be a rare occurrence which postprocessing should be able to handle. 491a3ed0e43SThomas Gleixner */ 492a3ed0e43SThomas Gleixner u64 notrace ktime_get_boot_fast_ns(void) 493a3ed0e43SThomas Gleixner { 494a3ed0e43SThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 495a3ed0e43SThomas Gleixner 496a3ed0e43SThomas Gleixner return (ktime_get_mono_fast_ns() + ktime_to_ns(tk->offs_boot)); 497a3ed0e43SThomas Gleixner } 498a3ed0e43SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns); 499a3ed0e43SThomas Gleixner 500a3ed0e43SThomas Gleixner 5014c3711d7SThomas Gleixner /* 5024c3711d7SThomas Gleixner * See comment for __ktime_get_fast_ns() vs. timestamp ordering 5034c3711d7SThomas Gleixner */ 5044c3711d7SThomas Gleixner static __always_inline u64 __ktime_get_real_fast_ns(struct tk_fast *tkf) 5054c3711d7SThomas Gleixner { 5064c3711d7SThomas Gleixner struct tk_read_base *tkr; 5074c3711d7SThomas Gleixner unsigned int seq; 5084c3711d7SThomas Gleixner u64 now; 5094c3711d7SThomas Gleixner 5104c3711d7SThomas Gleixner do { 5114c3711d7SThomas Gleixner seq = raw_read_seqcount_latch(&tkf->seq); 5124c3711d7SThomas Gleixner tkr = tkf->base + (seq & 0x01); 5134c3711d7SThomas Gleixner now = ktime_to_ns(tkr->base_real); 5144c3711d7SThomas Gleixner 5154c3711d7SThomas Gleixner now += timekeeping_delta_to_ns(tkr, 5164c3711d7SThomas Gleixner clocksource_delta( 5174c3711d7SThomas Gleixner tk_clock_read(tkr), 5184c3711d7SThomas Gleixner tkr->cycle_last, 5194c3711d7SThomas Gleixner tkr->mask)); 5204c3711d7SThomas Gleixner } while (read_seqcount_retry(&tkf->seq, seq)); 5214c3711d7SThomas Gleixner 5224c3711d7SThomas Gleixner return now; 5234c3711d7SThomas Gleixner } 5244c3711d7SThomas Gleixner 5254c3711d7SThomas Gleixner /** 5264c3711d7SThomas Gleixner * ktime_get_real_fast_ns: - NMI safe and fast access to clock realtime. 5274c3711d7SThomas Gleixner */ 5284c3711d7SThomas Gleixner u64 ktime_get_real_fast_ns(void) 5294c3711d7SThomas Gleixner { 5304c3711d7SThomas Gleixner return __ktime_get_real_fast_ns(&tk_fast_mono); 5314c3711d7SThomas Gleixner } 532df27067eSArnd Bergmann EXPORT_SYMBOL_GPL(ktime_get_real_fast_ns); 5334c3711d7SThomas Gleixner 534060407aeSRafael J. Wysocki /** 535060407aeSRafael J. Wysocki * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource. 536060407aeSRafael J. Wysocki * @tk: Timekeeper to snapshot. 537060407aeSRafael J. Wysocki * 538060407aeSRafael J. Wysocki * It generally is unsafe to access the clocksource after timekeeping has been 539060407aeSRafael J. Wysocki * suspended, so take a snapshot of the readout base of @tk and use it as the 540060407aeSRafael J. Wysocki * fast timekeeper's readout base while suspended. It will return the same 541060407aeSRafael J. Wysocki * number of cycles every time until timekeeping is resumed at which time the 542060407aeSRafael J. Wysocki * proper readout base for the fast timekeeper will be restored automatically. 543060407aeSRafael J. Wysocki */ 544060407aeSRafael J. Wysocki static void halt_fast_timekeeper(struct timekeeper *tk) 545060407aeSRafael J. Wysocki { 546060407aeSRafael J. Wysocki static struct tk_read_base tkr_dummy; 547876e7881SPeter Zijlstra struct tk_read_base *tkr = &tk->tkr_mono; 548060407aeSRafael J. Wysocki 549060407aeSRafael J. Wysocki memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy)); 550ceea5e37SJohn Stultz cycles_at_suspend = tk_clock_read(tkr); 551ceea5e37SJohn Stultz tkr_dummy.clock = &dummy_clock; 5524c3711d7SThomas Gleixner tkr_dummy.base_real = tkr->base + tk->offs_real; 5534498e746SPeter Zijlstra update_fast_timekeeper(&tkr_dummy, &tk_fast_mono); 554f09cb9a1SPeter Zijlstra 555f09cb9a1SPeter Zijlstra tkr = &tk->tkr_raw; 556f09cb9a1SPeter Zijlstra memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy)); 557ceea5e37SJohn Stultz tkr_dummy.clock = &dummy_clock; 558f09cb9a1SPeter Zijlstra update_fast_timekeeper(&tkr_dummy, &tk_fast_raw); 559060407aeSRafael J. Wysocki } 560060407aeSRafael J. Wysocki 561e0b306feSMarcelo Tosatti static RAW_NOTIFIER_HEAD(pvclock_gtod_chain); 562e0b306feSMarcelo Tosatti 563780427f0SDavid Vrabel static void update_pvclock_gtod(struct timekeeper *tk, bool was_set) 564e0b306feSMarcelo Tosatti { 565780427f0SDavid Vrabel raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk); 566e0b306feSMarcelo Tosatti } 567e0b306feSMarcelo Tosatti 568e0b306feSMarcelo Tosatti /** 569e0b306feSMarcelo Tosatti * pvclock_gtod_register_notifier - register a pvclock timedata update listener 570e0b306feSMarcelo Tosatti */ 571e0b306feSMarcelo Tosatti int pvclock_gtod_register_notifier(struct notifier_block *nb) 572e0b306feSMarcelo Tosatti { 5733fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 574e0b306feSMarcelo Tosatti unsigned long flags; 575e0b306feSMarcelo Tosatti int ret; 576e0b306feSMarcelo Tosatti 5779a7a71b1SThomas Gleixner raw_spin_lock_irqsave(&timekeeper_lock, flags); 578e0b306feSMarcelo Tosatti ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb); 579780427f0SDavid Vrabel update_pvclock_gtod(tk, true); 5809a7a71b1SThomas Gleixner raw_spin_unlock_irqrestore(&timekeeper_lock, flags); 581e0b306feSMarcelo Tosatti 582e0b306feSMarcelo Tosatti return ret; 583e0b306feSMarcelo Tosatti } 584e0b306feSMarcelo Tosatti EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier); 585e0b306feSMarcelo Tosatti 586e0b306feSMarcelo Tosatti /** 587e0b306feSMarcelo Tosatti * pvclock_gtod_unregister_notifier - unregister a pvclock 588e0b306feSMarcelo Tosatti * timedata update listener 589e0b306feSMarcelo Tosatti */ 590e0b306feSMarcelo Tosatti int pvclock_gtod_unregister_notifier(struct notifier_block *nb) 591e0b306feSMarcelo Tosatti { 592e0b306feSMarcelo Tosatti unsigned long flags; 593e0b306feSMarcelo Tosatti int ret; 594e0b306feSMarcelo Tosatti 5959a7a71b1SThomas Gleixner raw_spin_lock_irqsave(&timekeeper_lock, flags); 596e0b306feSMarcelo Tosatti ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb); 5979a7a71b1SThomas Gleixner raw_spin_unlock_irqrestore(&timekeeper_lock, flags); 598e0b306feSMarcelo Tosatti 599e0b306feSMarcelo Tosatti return ret; 600e0b306feSMarcelo Tosatti } 601e0b306feSMarcelo Tosatti EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier); 602e0b306feSMarcelo Tosatti 6037c032df5SThomas Gleixner /* 604833f32d7SJohn Stultz * tk_update_leap_state - helper to update the next_leap_ktime 605833f32d7SJohn Stultz */ 606833f32d7SJohn Stultz static inline void tk_update_leap_state(struct timekeeper *tk) 607833f32d7SJohn Stultz { 608833f32d7SJohn Stultz tk->next_leap_ktime = ntp_get_next_leap(); 6092456e855SThomas Gleixner if (tk->next_leap_ktime != KTIME_MAX) 610833f32d7SJohn Stultz /* Convert to monotonic time */ 611833f32d7SJohn Stultz tk->next_leap_ktime = ktime_sub(tk->next_leap_ktime, tk->offs_real); 612833f32d7SJohn Stultz } 613833f32d7SJohn Stultz 614833f32d7SJohn Stultz /* 6157c032df5SThomas Gleixner * Update the ktime_t based scalar nsec members of the timekeeper 6167c032df5SThomas Gleixner */ 6177c032df5SThomas Gleixner static inline void tk_update_ktime_data(struct timekeeper *tk) 6187c032df5SThomas Gleixner { 6199e3680b1SHeena Sirwani u64 seconds; 6209e3680b1SHeena Sirwani u32 nsec; 6217c032df5SThomas Gleixner 6227c032df5SThomas Gleixner /* 6237c032df5SThomas Gleixner * The xtime based monotonic readout is: 6247c032df5SThomas Gleixner * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now(); 6257c032df5SThomas Gleixner * The ktime based monotonic readout is: 6267c032df5SThomas Gleixner * nsec = base_mono + now(); 6277c032df5SThomas Gleixner * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec 6287c032df5SThomas Gleixner */ 6299e3680b1SHeena Sirwani seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec); 6309e3680b1SHeena Sirwani nsec = (u32) tk->wall_to_monotonic.tv_nsec; 631876e7881SPeter Zijlstra tk->tkr_mono.base = ns_to_ktime(seconds * NSEC_PER_SEC + nsec); 632f519b1a2SThomas Gleixner 6339e3680b1SHeena Sirwani /* 6349e3680b1SHeena Sirwani * The sum of the nanoseconds portions of xtime and 6359e3680b1SHeena Sirwani * wall_to_monotonic can be greater/equal one second. Take 6369e3680b1SHeena Sirwani * this into account before updating tk->ktime_sec. 6379e3680b1SHeena Sirwani */ 638876e7881SPeter Zijlstra nsec += (u32)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift); 6399e3680b1SHeena Sirwani if (nsec >= NSEC_PER_SEC) 6409e3680b1SHeena Sirwani seconds++; 6419e3680b1SHeena Sirwani tk->ktime_sec = seconds; 642fc6eead7SJohn Stultz 643fc6eead7SJohn Stultz /* Update the monotonic raw base */ 6440bcdc098SJohn Stultz tk->tkr_raw.base = ns_to_ktime(tk->raw_sec * NSEC_PER_SEC); 6457c032df5SThomas Gleixner } 6467c032df5SThomas Gleixner 6479a7a71b1SThomas Gleixner /* must hold timekeeper_lock */ 64804397fe9SDavid Vrabel static void timekeeping_update(struct timekeeper *tk, unsigned int action) 649cc06268cSThomas Gleixner { 65004397fe9SDavid Vrabel if (action & TK_CLEAR_NTP) { 651f726a697SJohn Stultz tk->ntp_error = 0; 652cc06268cSThomas Gleixner ntp_clear(); 653cc06268cSThomas Gleixner } 65448cdc135SThomas Gleixner 655833f32d7SJohn Stultz tk_update_leap_state(tk); 6567c032df5SThomas Gleixner tk_update_ktime_data(tk); 6577c032df5SThomas Gleixner 6589bf2419fSThomas Gleixner update_vsyscall(tk); 6599bf2419fSThomas Gleixner update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET); 6609bf2419fSThomas Gleixner 6614c3711d7SThomas Gleixner tk->tkr_mono.base_real = tk->tkr_mono.base + tk->offs_real; 6624498e746SPeter Zijlstra update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono); 663f09cb9a1SPeter Zijlstra update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw); 664868a3e91SThomas Gleixner 665868a3e91SThomas Gleixner if (action & TK_CLOCK_WAS_SET) 666868a3e91SThomas Gleixner tk->clock_was_set_seq++; 667d1518326SJohn Stultz /* 668d1518326SJohn Stultz * The mirroring of the data to the shadow-timekeeper needs 669d1518326SJohn Stultz * to happen last here to ensure we don't over-write the 670d1518326SJohn Stultz * timekeeper structure on the next update with stale data 671d1518326SJohn Stultz */ 672d1518326SJohn Stultz if (action & TK_MIRROR) 673d1518326SJohn Stultz memcpy(&shadow_timekeeper, &tk_core.timekeeper, 674d1518326SJohn Stultz sizeof(tk_core.timekeeper)); 675cc06268cSThomas Gleixner } 676cc06268cSThomas Gleixner 6778524070bSjohn stultz /** 678155ec602SMartin Schwidefsky * timekeeping_forward_now - update clock to the current time 6798524070bSjohn stultz * 6809a055117SRoman Zippel * Forward the current clock to update its state since the last call to 6819a055117SRoman Zippel * update_wall_time(). This is useful before significant clock changes, 6829a055117SRoman Zippel * as it avoids having to deal with this time offset explicitly. 6838524070bSjohn stultz */ 684f726a697SJohn Stultz static void timekeeping_forward_now(struct timekeeper *tk) 6858524070bSjohn stultz { 686a5a1d1c2SThomas Gleixner u64 cycle_now, delta; 6878524070bSjohn stultz 688ceea5e37SJohn Stultz cycle_now = tk_clock_read(&tk->tkr_mono); 689876e7881SPeter Zijlstra delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask); 690876e7881SPeter Zijlstra tk->tkr_mono.cycle_last = cycle_now; 6914a4ad80dSPeter Zijlstra tk->tkr_raw.cycle_last = cycle_now; 6928524070bSjohn stultz 693876e7881SPeter Zijlstra tk->tkr_mono.xtime_nsec += delta * tk->tkr_mono.mult; 6947d27558cSjohn stultz 6957b1f6207SStephen Warren /* If arch requires, add in get_arch_timeoffset() */ 696876e7881SPeter Zijlstra tk->tkr_mono.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_mono.shift; 6977d27558cSjohn stultz 6982d42244aSJohn Stultz 699fc6eead7SJohn Stultz tk->tkr_raw.xtime_nsec += delta * tk->tkr_raw.mult; 700fc6eead7SJohn Stultz 701fc6eead7SJohn Stultz /* If arch requires, add in get_arch_timeoffset() */ 702fc6eead7SJohn Stultz tk->tkr_raw.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_raw.shift; 703fc6eead7SJohn Stultz 704fc6eead7SJohn Stultz tk_normalize_xtime(tk); 7058524070bSjohn stultz } 7068524070bSjohn stultz 7078524070bSjohn stultz /** 708edca71feSArnd Bergmann * ktime_get_real_ts64 - Returns the time of day in a timespec64. 7098524070bSjohn stultz * @ts: pointer to the timespec to be set 7108524070bSjohn stultz * 711edca71feSArnd Bergmann * Returns the time of day in a timespec64 (WARN if suspended). 7128524070bSjohn stultz */ 713edca71feSArnd Bergmann void ktime_get_real_ts64(struct timespec64 *ts) 7148524070bSjohn stultz { 7153fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 7168524070bSjohn stultz unsigned long seq; 717acc89612SThomas Gleixner u64 nsecs; 7188524070bSjohn stultz 719edca71feSArnd Bergmann WARN_ON(timekeeping_suspended); 720edca71feSArnd Bergmann 7218524070bSjohn stultz do { 7223fdb14fdSThomas Gleixner seq = read_seqcount_begin(&tk_core.seq); 7238524070bSjohn stultz 7244e250fddSJohn Stultz ts->tv_sec = tk->xtime_sec; 725876e7881SPeter Zijlstra nsecs = timekeeping_get_ns(&tk->tkr_mono); 7268524070bSjohn stultz 7273fdb14fdSThomas Gleixner } while (read_seqcount_retry(&tk_core.seq, seq)); 7288524070bSjohn stultz 729ec145babSJohn Stultz ts->tv_nsec = 0; 730d6d29896SThomas Gleixner timespec64_add_ns(ts, nsecs); 7311e817fb6SKees Cook } 732edca71feSArnd Bergmann EXPORT_SYMBOL(ktime_get_real_ts64); 7338524070bSjohn stultz 734951ed4d3SMartin Schwidefsky ktime_t ktime_get(void) 735951ed4d3SMartin Schwidefsky { 7363fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 737951ed4d3SMartin Schwidefsky unsigned int seq; 738a016a5bdSThomas Gleixner ktime_t base; 739acc89612SThomas Gleixner u64 nsecs; 740951ed4d3SMartin Schwidefsky 741951ed4d3SMartin Schwidefsky WARN_ON(timekeeping_suspended); 742951ed4d3SMartin Schwidefsky 743951ed4d3SMartin Schwidefsky do { 7443fdb14fdSThomas Gleixner seq = read_seqcount_begin(&tk_core.seq); 745876e7881SPeter Zijlstra base = tk->tkr_mono.base; 746876e7881SPeter Zijlstra nsecs = timekeeping_get_ns(&tk->tkr_mono); 747951ed4d3SMartin Schwidefsky 7483fdb14fdSThomas Gleixner } while (read_seqcount_retry(&tk_core.seq, seq)); 74924e4a8c3SJohn Stultz 750a016a5bdSThomas Gleixner return ktime_add_ns(base, nsecs); 751951ed4d3SMartin Schwidefsky } 752951ed4d3SMartin Schwidefsky EXPORT_SYMBOL_GPL(ktime_get); 753951ed4d3SMartin Schwidefsky 7546374f912SHarald Geyer u32 ktime_get_resolution_ns(void) 7556374f912SHarald Geyer { 7566374f912SHarald Geyer struct timekeeper *tk = &tk_core.timekeeper; 7576374f912SHarald Geyer unsigned int seq; 7586374f912SHarald Geyer u32 nsecs; 7596374f912SHarald Geyer 7606374f912SHarald Geyer WARN_ON(timekeeping_suspended); 7616374f912SHarald Geyer 7626374f912SHarald Geyer do { 7636374f912SHarald Geyer seq = read_seqcount_begin(&tk_core.seq); 7646374f912SHarald Geyer nsecs = tk->tkr_mono.mult >> tk->tkr_mono.shift; 7656374f912SHarald Geyer } while (read_seqcount_retry(&tk_core.seq, seq)); 7666374f912SHarald Geyer 7676374f912SHarald Geyer return nsecs; 7686374f912SHarald Geyer } 7696374f912SHarald Geyer EXPORT_SYMBOL_GPL(ktime_get_resolution_ns); 7706374f912SHarald Geyer 7710077dc60SThomas Gleixner static ktime_t *offsets[TK_OFFS_MAX] = { 7720077dc60SThomas Gleixner [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real, 773a3ed0e43SThomas Gleixner [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot, 7740077dc60SThomas Gleixner [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai, 7750077dc60SThomas Gleixner }; 7760077dc60SThomas Gleixner 7770077dc60SThomas Gleixner ktime_t ktime_get_with_offset(enum tk_offsets offs) 7780077dc60SThomas Gleixner { 7790077dc60SThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 7800077dc60SThomas Gleixner unsigned int seq; 7810077dc60SThomas Gleixner ktime_t base, *offset = offsets[offs]; 782acc89612SThomas Gleixner u64 nsecs; 7830077dc60SThomas Gleixner 7840077dc60SThomas Gleixner WARN_ON(timekeeping_suspended); 7850077dc60SThomas Gleixner 7860077dc60SThomas Gleixner do { 7870077dc60SThomas Gleixner seq = read_seqcount_begin(&tk_core.seq); 788876e7881SPeter Zijlstra base = ktime_add(tk->tkr_mono.base, *offset); 789876e7881SPeter Zijlstra nsecs = timekeeping_get_ns(&tk->tkr_mono); 7900077dc60SThomas Gleixner 7910077dc60SThomas Gleixner } while (read_seqcount_retry(&tk_core.seq, seq)); 7920077dc60SThomas Gleixner 7930077dc60SThomas Gleixner return ktime_add_ns(base, nsecs); 7940077dc60SThomas Gleixner 7950077dc60SThomas Gleixner } 7960077dc60SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_get_with_offset); 7970077dc60SThomas Gleixner 798951ed4d3SMartin Schwidefsky /** 7999a6b5197SThomas Gleixner * ktime_mono_to_any() - convert mononotic time to any other time 8009a6b5197SThomas Gleixner * @tmono: time to convert. 8019a6b5197SThomas Gleixner * @offs: which offset to use 8029a6b5197SThomas Gleixner */ 8039a6b5197SThomas Gleixner ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs) 8049a6b5197SThomas Gleixner { 8059a6b5197SThomas Gleixner ktime_t *offset = offsets[offs]; 8069a6b5197SThomas Gleixner unsigned long seq; 8079a6b5197SThomas Gleixner ktime_t tconv; 8089a6b5197SThomas Gleixner 8099a6b5197SThomas Gleixner do { 8109a6b5197SThomas Gleixner seq = read_seqcount_begin(&tk_core.seq); 8119a6b5197SThomas Gleixner tconv = ktime_add(tmono, *offset); 8129a6b5197SThomas Gleixner } while (read_seqcount_retry(&tk_core.seq, seq)); 8139a6b5197SThomas Gleixner 8149a6b5197SThomas Gleixner return tconv; 8159a6b5197SThomas Gleixner } 8169a6b5197SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_mono_to_any); 8179a6b5197SThomas Gleixner 8189a6b5197SThomas Gleixner /** 819f519b1a2SThomas Gleixner * ktime_get_raw - Returns the raw monotonic time in ktime_t format 820f519b1a2SThomas Gleixner */ 821f519b1a2SThomas Gleixner ktime_t ktime_get_raw(void) 822f519b1a2SThomas Gleixner { 823f519b1a2SThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 824f519b1a2SThomas Gleixner unsigned int seq; 825f519b1a2SThomas Gleixner ktime_t base; 826acc89612SThomas Gleixner u64 nsecs; 827f519b1a2SThomas Gleixner 828f519b1a2SThomas Gleixner do { 829f519b1a2SThomas Gleixner seq = read_seqcount_begin(&tk_core.seq); 8304a4ad80dSPeter Zijlstra base = tk->tkr_raw.base; 8314a4ad80dSPeter Zijlstra nsecs = timekeeping_get_ns(&tk->tkr_raw); 832f519b1a2SThomas Gleixner 833f519b1a2SThomas Gleixner } while (read_seqcount_retry(&tk_core.seq, seq)); 834f519b1a2SThomas Gleixner 835f519b1a2SThomas Gleixner return ktime_add_ns(base, nsecs); 836f519b1a2SThomas Gleixner } 837f519b1a2SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_get_raw); 838f519b1a2SThomas Gleixner 839f519b1a2SThomas Gleixner /** 840d6d29896SThomas Gleixner * ktime_get_ts64 - get the monotonic clock in timespec64 format 841951ed4d3SMartin Schwidefsky * @ts: pointer to timespec variable 842951ed4d3SMartin Schwidefsky * 843951ed4d3SMartin Schwidefsky * The function calculates the monotonic clock from the realtime 844951ed4d3SMartin Schwidefsky * clock and the wall_to_monotonic offset and stores the result 8455322e4c2SJohn Stultz * in normalized timespec64 format in the variable pointed to by @ts. 846951ed4d3SMartin Schwidefsky */ 847d6d29896SThomas Gleixner void ktime_get_ts64(struct timespec64 *ts) 848951ed4d3SMartin Schwidefsky { 8493fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 850d6d29896SThomas Gleixner struct timespec64 tomono; 851951ed4d3SMartin Schwidefsky unsigned int seq; 852acc89612SThomas Gleixner u64 nsec; 853951ed4d3SMartin Schwidefsky 854951ed4d3SMartin Schwidefsky WARN_ON(timekeeping_suspended); 855951ed4d3SMartin Schwidefsky 856951ed4d3SMartin Schwidefsky do { 8573fdb14fdSThomas Gleixner seq = read_seqcount_begin(&tk_core.seq); 858d6d29896SThomas Gleixner ts->tv_sec = tk->xtime_sec; 859876e7881SPeter Zijlstra nsec = timekeeping_get_ns(&tk->tkr_mono); 8604e250fddSJohn Stultz tomono = tk->wall_to_monotonic; 861951ed4d3SMartin Schwidefsky 8623fdb14fdSThomas Gleixner } while (read_seqcount_retry(&tk_core.seq, seq)); 863951ed4d3SMartin Schwidefsky 864d6d29896SThomas Gleixner ts->tv_sec += tomono.tv_sec; 865d6d29896SThomas Gleixner ts->tv_nsec = 0; 866d6d29896SThomas Gleixner timespec64_add_ns(ts, nsec + tomono.tv_nsec); 867951ed4d3SMartin Schwidefsky } 868d6d29896SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_get_ts64); 869951ed4d3SMartin Schwidefsky 8709e3680b1SHeena Sirwani /** 8719e3680b1SHeena Sirwani * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC 8729e3680b1SHeena Sirwani * 8739e3680b1SHeena Sirwani * Returns the seconds portion of CLOCK_MONOTONIC with a single non 8749e3680b1SHeena Sirwani * serialized read. tk->ktime_sec is of type 'unsigned long' so this 8759e3680b1SHeena Sirwani * works on both 32 and 64 bit systems. On 32 bit systems the readout 8769e3680b1SHeena Sirwani * covers ~136 years of uptime which should be enough to prevent 8779e3680b1SHeena Sirwani * premature wrap arounds. 8789e3680b1SHeena Sirwani */ 8799e3680b1SHeena Sirwani time64_t ktime_get_seconds(void) 8809e3680b1SHeena Sirwani { 8819e3680b1SHeena Sirwani struct timekeeper *tk = &tk_core.timekeeper; 8829e3680b1SHeena Sirwani 8839e3680b1SHeena Sirwani WARN_ON(timekeeping_suspended); 8849e3680b1SHeena Sirwani return tk->ktime_sec; 8859e3680b1SHeena Sirwani } 8869e3680b1SHeena Sirwani EXPORT_SYMBOL_GPL(ktime_get_seconds); 8879e3680b1SHeena Sirwani 888dbe7aa62SHeena Sirwani /** 889dbe7aa62SHeena Sirwani * ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME 890dbe7aa62SHeena Sirwani * 891dbe7aa62SHeena Sirwani * Returns the wall clock seconds since 1970. This replaces the 892dbe7aa62SHeena Sirwani * get_seconds() interface which is not y2038 safe on 32bit systems. 893dbe7aa62SHeena Sirwani * 894dbe7aa62SHeena Sirwani * For 64bit systems the fast access to tk->xtime_sec is preserved. On 895dbe7aa62SHeena Sirwani * 32bit systems the access must be protected with the sequence 896dbe7aa62SHeena Sirwani * counter to provide "atomic" access to the 64bit tk->xtime_sec 897dbe7aa62SHeena Sirwani * value. 898dbe7aa62SHeena Sirwani */ 899dbe7aa62SHeena Sirwani time64_t ktime_get_real_seconds(void) 900dbe7aa62SHeena Sirwani { 901dbe7aa62SHeena Sirwani struct timekeeper *tk = &tk_core.timekeeper; 902dbe7aa62SHeena Sirwani time64_t seconds; 903dbe7aa62SHeena Sirwani unsigned int seq; 904dbe7aa62SHeena Sirwani 905dbe7aa62SHeena Sirwani if (IS_ENABLED(CONFIG_64BIT)) 906dbe7aa62SHeena Sirwani return tk->xtime_sec; 907dbe7aa62SHeena Sirwani 908dbe7aa62SHeena Sirwani do { 909dbe7aa62SHeena Sirwani seq = read_seqcount_begin(&tk_core.seq); 910dbe7aa62SHeena Sirwani seconds = tk->xtime_sec; 911dbe7aa62SHeena Sirwani 912dbe7aa62SHeena Sirwani } while (read_seqcount_retry(&tk_core.seq, seq)); 913dbe7aa62SHeena Sirwani 914dbe7aa62SHeena Sirwani return seconds; 915dbe7aa62SHeena Sirwani } 916dbe7aa62SHeena Sirwani EXPORT_SYMBOL_GPL(ktime_get_real_seconds); 917dbe7aa62SHeena Sirwani 918dee36654SDengChao /** 919dee36654SDengChao * __ktime_get_real_seconds - The same as ktime_get_real_seconds 920dee36654SDengChao * but without the sequence counter protect. This internal function 921dee36654SDengChao * is called just when timekeeping lock is already held. 922dee36654SDengChao */ 923dee36654SDengChao time64_t __ktime_get_real_seconds(void) 924dee36654SDengChao { 925dee36654SDengChao struct timekeeper *tk = &tk_core.timekeeper; 926dee36654SDengChao 927dee36654SDengChao return tk->xtime_sec; 928dee36654SDengChao } 929dee36654SDengChao 9309da0f49cSChristopher S. Hall /** 9319da0f49cSChristopher S. Hall * ktime_get_snapshot - snapshots the realtime/monotonic raw clocks with counter 9329da0f49cSChristopher S. Hall * @systime_snapshot: pointer to struct receiving the system time snapshot 9339da0f49cSChristopher S. Hall */ 9349da0f49cSChristopher S. Hall void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot) 9359da0f49cSChristopher S. Hall { 9369da0f49cSChristopher S. Hall struct timekeeper *tk = &tk_core.timekeeper; 9379da0f49cSChristopher S. Hall unsigned long seq; 9389da0f49cSChristopher S. Hall ktime_t base_raw; 9399da0f49cSChristopher S. Hall ktime_t base_real; 940acc89612SThomas Gleixner u64 nsec_raw; 941acc89612SThomas Gleixner u64 nsec_real; 942a5a1d1c2SThomas Gleixner u64 now; 9439da0f49cSChristopher S. Hall 944ba26621eSChristopher S. Hall WARN_ON_ONCE(timekeeping_suspended); 945ba26621eSChristopher S. Hall 9469da0f49cSChristopher S. Hall do { 9479da0f49cSChristopher S. Hall seq = read_seqcount_begin(&tk_core.seq); 948ceea5e37SJohn Stultz now = tk_clock_read(&tk->tkr_mono); 9492c756febSChristopher S. Hall systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq; 9502c756febSChristopher S. Hall systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq; 9519da0f49cSChristopher S. Hall base_real = ktime_add(tk->tkr_mono.base, 9529da0f49cSChristopher S. Hall tk_core.timekeeper.offs_real); 9539da0f49cSChristopher S. Hall base_raw = tk->tkr_raw.base; 9549da0f49cSChristopher S. Hall nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, now); 9559da0f49cSChristopher S. Hall nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw, now); 9569da0f49cSChristopher S. Hall } while (read_seqcount_retry(&tk_core.seq, seq)); 9579da0f49cSChristopher S. Hall 9589da0f49cSChristopher S. Hall systime_snapshot->cycles = now; 9599da0f49cSChristopher S. Hall systime_snapshot->real = ktime_add_ns(base_real, nsec_real); 9609da0f49cSChristopher S. Hall systime_snapshot->raw = ktime_add_ns(base_raw, nsec_raw); 9619da0f49cSChristopher S. Hall } 9629da0f49cSChristopher S. Hall EXPORT_SYMBOL_GPL(ktime_get_snapshot); 963dee36654SDengChao 9642c756febSChristopher S. Hall /* Scale base by mult/div checking for overflow */ 9652c756febSChristopher S. Hall static int scale64_check_overflow(u64 mult, u64 div, u64 *base) 9662c756febSChristopher S. Hall { 9672c756febSChristopher S. Hall u64 tmp, rem; 9682c756febSChristopher S. Hall 9692c756febSChristopher S. Hall tmp = div64_u64_rem(*base, div, &rem); 9702c756febSChristopher S. Hall 9712c756febSChristopher S. Hall if (((int)sizeof(u64)*8 - fls64(mult) < fls64(tmp)) || 9722c756febSChristopher S. Hall ((int)sizeof(u64)*8 - fls64(mult) < fls64(rem))) 9732c756febSChristopher S. Hall return -EOVERFLOW; 9742c756febSChristopher S. Hall tmp *= mult; 9752c756febSChristopher S. Hall rem *= mult; 9762c756febSChristopher S. Hall 9772c756febSChristopher S. Hall do_div(rem, div); 9782c756febSChristopher S. Hall *base = tmp + rem; 9792c756febSChristopher S. Hall return 0; 9802c756febSChristopher S. Hall } 9812c756febSChristopher S. Hall 9822c756febSChristopher S. Hall /** 9832c756febSChristopher S. Hall * adjust_historical_crosststamp - adjust crosstimestamp previous to current interval 9842c756febSChristopher S. Hall * @history: Snapshot representing start of history 9852c756febSChristopher S. Hall * @partial_history_cycles: Cycle offset into history (fractional part) 9862c756febSChristopher S. Hall * @total_history_cycles: Total history length in cycles 9872c756febSChristopher S. Hall * @discontinuity: True indicates clock was set on history period 9882c756febSChristopher S. Hall * @ts: Cross timestamp that should be adjusted using 9892c756febSChristopher S. Hall * partial/total ratio 9902c756febSChristopher S. Hall * 9912c756febSChristopher S. Hall * Helper function used by get_device_system_crosststamp() to correct the 9922c756febSChristopher S. Hall * crosstimestamp corresponding to the start of the current interval to the 9932c756febSChristopher S. Hall * system counter value (timestamp point) provided by the driver. The 9942c756febSChristopher S. Hall * total_history_* quantities are the total history starting at the provided 9952c756febSChristopher S. Hall * reference point and ending at the start of the current interval. The cycle 9962c756febSChristopher S. Hall * count between the driver timestamp point and the start of the current 9972c756febSChristopher S. Hall * interval is partial_history_cycles. 9982c756febSChristopher S. Hall */ 9992c756febSChristopher S. Hall static int adjust_historical_crosststamp(struct system_time_snapshot *history, 1000a5a1d1c2SThomas Gleixner u64 partial_history_cycles, 1001a5a1d1c2SThomas Gleixner u64 total_history_cycles, 10022c756febSChristopher S. Hall bool discontinuity, 10032c756febSChristopher S. Hall struct system_device_crosststamp *ts) 10042c756febSChristopher S. Hall { 10052c756febSChristopher S. Hall struct timekeeper *tk = &tk_core.timekeeper; 10062c756febSChristopher S. Hall u64 corr_raw, corr_real; 10072c756febSChristopher S. Hall bool interp_forward; 10082c756febSChristopher S. Hall int ret; 10092c756febSChristopher S. Hall 10102c756febSChristopher S. Hall if (total_history_cycles == 0 || partial_history_cycles == 0) 10112c756febSChristopher S. Hall return 0; 10122c756febSChristopher S. Hall 10132c756febSChristopher S. Hall /* Interpolate shortest distance from beginning or end of history */ 10145fc63f95SNicholas Mc Guire interp_forward = partial_history_cycles > total_history_cycles / 2; 10152c756febSChristopher S. Hall partial_history_cycles = interp_forward ? 10162c756febSChristopher S. Hall total_history_cycles - partial_history_cycles : 10172c756febSChristopher S. Hall partial_history_cycles; 10182c756febSChristopher S. Hall 10192c756febSChristopher S. Hall /* 10202c756febSChristopher S. Hall * Scale the monotonic raw time delta by: 10212c756febSChristopher S. Hall * partial_history_cycles / total_history_cycles 10222c756febSChristopher S. Hall */ 10232c756febSChristopher S. Hall corr_raw = (u64)ktime_to_ns( 10242c756febSChristopher S. Hall ktime_sub(ts->sys_monoraw, history->raw)); 10252c756febSChristopher S. Hall ret = scale64_check_overflow(partial_history_cycles, 10262c756febSChristopher S. Hall total_history_cycles, &corr_raw); 10272c756febSChristopher S. Hall if (ret) 10282c756febSChristopher S. Hall return ret; 10292c756febSChristopher S. Hall 10302c756febSChristopher S. Hall /* 10312c756febSChristopher S. Hall * If there is a discontinuity in the history, scale monotonic raw 10322c756febSChristopher S. Hall * correction by: 10332c756febSChristopher S. Hall * mult(real)/mult(raw) yielding the realtime correction 10342c756febSChristopher S. Hall * Otherwise, calculate the realtime correction similar to monotonic 10352c756febSChristopher S. Hall * raw calculation 10362c756febSChristopher S. Hall */ 10372c756febSChristopher S. Hall if (discontinuity) { 10382c756febSChristopher S. Hall corr_real = mul_u64_u32_div 10392c756febSChristopher S. Hall (corr_raw, tk->tkr_mono.mult, tk->tkr_raw.mult); 10402c756febSChristopher S. Hall } else { 10412c756febSChristopher S. Hall corr_real = (u64)ktime_to_ns( 10422c756febSChristopher S. Hall ktime_sub(ts->sys_realtime, history->real)); 10432c756febSChristopher S. Hall ret = scale64_check_overflow(partial_history_cycles, 10442c756febSChristopher S. Hall total_history_cycles, &corr_real); 10452c756febSChristopher S. Hall if (ret) 10462c756febSChristopher S. Hall return ret; 10472c756febSChristopher S. Hall } 10482c756febSChristopher S. Hall 10492c756febSChristopher S. Hall /* Fixup monotonic raw and real time time values */ 10502c756febSChristopher S. Hall if (interp_forward) { 10512c756febSChristopher S. Hall ts->sys_monoraw = ktime_add_ns(history->raw, corr_raw); 10522c756febSChristopher S. Hall ts->sys_realtime = ktime_add_ns(history->real, corr_real); 10532c756febSChristopher S. Hall } else { 10542c756febSChristopher S. Hall ts->sys_monoraw = ktime_sub_ns(ts->sys_monoraw, corr_raw); 10552c756febSChristopher S. Hall ts->sys_realtime = ktime_sub_ns(ts->sys_realtime, corr_real); 10562c756febSChristopher S. Hall } 10572c756febSChristopher S. Hall 10582c756febSChristopher S. Hall return 0; 10592c756febSChristopher S. Hall } 10602c756febSChristopher S. Hall 10612c756febSChristopher S. Hall /* 10622c756febSChristopher S. Hall * cycle_between - true if test occurs chronologically between before and after 10632c756febSChristopher S. Hall */ 1064a5a1d1c2SThomas Gleixner static bool cycle_between(u64 before, u64 test, u64 after) 10652c756febSChristopher S. Hall { 10662c756febSChristopher S. Hall if (test > before && test < after) 10672c756febSChristopher S. Hall return true; 10682c756febSChristopher S. Hall if (test < before && before > after) 10692c756febSChristopher S. Hall return true; 10702c756febSChristopher S. Hall return false; 10712c756febSChristopher S. Hall } 10722c756febSChristopher S. Hall 10738524070bSjohn stultz /** 10748006c245SChristopher S. Hall * get_device_system_crosststamp - Synchronously capture system/device timestamp 10752c756febSChristopher S. Hall * @get_time_fn: Callback to get simultaneous device time and 10768006c245SChristopher S. Hall * system counter from the device driver 10772c756febSChristopher S. Hall * @ctx: Context passed to get_time_fn() 10782c756febSChristopher S. Hall * @history_begin: Historical reference point used to interpolate system 10792c756febSChristopher S. Hall * time when counter provided by the driver is before the current interval 10808006c245SChristopher S. Hall * @xtstamp: Receives simultaneously captured system and device time 10818006c245SChristopher S. Hall * 10828006c245SChristopher S. Hall * Reads a timestamp from a device and correlates it to system time 10838006c245SChristopher S. Hall */ 10848006c245SChristopher S. Hall int get_device_system_crosststamp(int (*get_time_fn) 10858006c245SChristopher S. Hall (ktime_t *device_time, 10868006c245SChristopher S. Hall struct system_counterval_t *sys_counterval, 10878006c245SChristopher S. Hall void *ctx), 10888006c245SChristopher S. Hall void *ctx, 10892c756febSChristopher S. Hall struct system_time_snapshot *history_begin, 10908006c245SChristopher S. Hall struct system_device_crosststamp *xtstamp) 10918006c245SChristopher S. Hall { 10928006c245SChristopher S. Hall struct system_counterval_t system_counterval; 10938006c245SChristopher S. Hall struct timekeeper *tk = &tk_core.timekeeper; 1094a5a1d1c2SThomas Gleixner u64 cycles, now, interval_start; 10956436257bSIngo Molnar unsigned int clock_was_set_seq = 0; 10968006c245SChristopher S. Hall ktime_t base_real, base_raw; 1097acc89612SThomas Gleixner u64 nsec_real, nsec_raw; 10982c756febSChristopher S. Hall u8 cs_was_changed_seq; 10998006c245SChristopher S. Hall unsigned long seq; 11002c756febSChristopher S. Hall bool do_interp; 11018006c245SChristopher S. Hall int ret; 11028006c245SChristopher S. Hall 11038006c245SChristopher S. Hall do { 11048006c245SChristopher S. Hall seq = read_seqcount_begin(&tk_core.seq); 11058006c245SChristopher S. Hall /* 11068006c245SChristopher S. Hall * Try to synchronously capture device time and a system 11078006c245SChristopher S. Hall * counter value calling back into the device driver 11088006c245SChristopher S. Hall */ 11098006c245SChristopher S. Hall ret = get_time_fn(&xtstamp->device, &system_counterval, ctx); 11108006c245SChristopher S. Hall if (ret) 11118006c245SChristopher S. Hall return ret; 11128006c245SChristopher S. Hall 11138006c245SChristopher S. Hall /* 11148006c245SChristopher S. Hall * Verify that the clocksource associated with the captured 11158006c245SChristopher S. Hall * system counter value is the same as the currently installed 11168006c245SChristopher S. Hall * timekeeper clocksource 11178006c245SChristopher S. Hall */ 11188006c245SChristopher S. Hall if (tk->tkr_mono.clock != system_counterval.cs) 11198006c245SChristopher S. Hall return -ENODEV; 11202c756febSChristopher S. Hall cycles = system_counterval.cycles; 11212c756febSChristopher S. Hall 11222c756febSChristopher S. Hall /* 11232c756febSChristopher S. Hall * Check whether the system counter value provided by the 11242c756febSChristopher S. Hall * device driver is on the current timekeeping interval. 11252c756febSChristopher S. Hall */ 1126ceea5e37SJohn Stultz now = tk_clock_read(&tk->tkr_mono); 11272c756febSChristopher S. Hall interval_start = tk->tkr_mono.cycle_last; 11282c756febSChristopher S. Hall if (!cycle_between(interval_start, cycles, now)) { 11292c756febSChristopher S. Hall clock_was_set_seq = tk->clock_was_set_seq; 11302c756febSChristopher S. Hall cs_was_changed_seq = tk->cs_was_changed_seq; 11312c756febSChristopher S. Hall cycles = interval_start; 11322c756febSChristopher S. Hall do_interp = true; 11332c756febSChristopher S. Hall } else { 11342c756febSChristopher S. Hall do_interp = false; 11352c756febSChristopher S. Hall } 11368006c245SChristopher S. Hall 11378006c245SChristopher S. Hall base_real = ktime_add(tk->tkr_mono.base, 11388006c245SChristopher S. Hall tk_core.timekeeper.offs_real); 11398006c245SChristopher S. Hall base_raw = tk->tkr_raw.base; 11408006c245SChristopher S. Hall 11418006c245SChristopher S. Hall nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, 11428006c245SChristopher S. Hall system_counterval.cycles); 11438006c245SChristopher S. Hall nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw, 11448006c245SChristopher S. Hall system_counterval.cycles); 11458006c245SChristopher S. Hall } while (read_seqcount_retry(&tk_core.seq, seq)); 11468006c245SChristopher S. Hall 11478006c245SChristopher S. Hall xtstamp->sys_realtime = ktime_add_ns(base_real, nsec_real); 11488006c245SChristopher S. Hall xtstamp->sys_monoraw = ktime_add_ns(base_raw, nsec_raw); 11492c756febSChristopher S. Hall 11502c756febSChristopher S. Hall /* 11512c756febSChristopher S. Hall * Interpolate if necessary, adjusting back from the start of the 11522c756febSChristopher S. Hall * current interval 11532c756febSChristopher S. Hall */ 11542c756febSChristopher S. Hall if (do_interp) { 1155a5a1d1c2SThomas Gleixner u64 partial_history_cycles, total_history_cycles; 11562c756febSChristopher S. Hall bool discontinuity; 11572c756febSChristopher S. Hall 11582c756febSChristopher S. Hall /* 11592c756febSChristopher S. Hall * Check that the counter value occurs after the provided 11602c756febSChristopher S. Hall * history reference and that the history doesn't cross a 11612c756febSChristopher S. Hall * clocksource change 11622c756febSChristopher S. Hall */ 11632c756febSChristopher S. Hall if (!history_begin || 11642c756febSChristopher S. Hall !cycle_between(history_begin->cycles, 11652c756febSChristopher S. Hall system_counterval.cycles, cycles) || 11662c756febSChristopher S. Hall history_begin->cs_was_changed_seq != cs_was_changed_seq) 11672c756febSChristopher S. Hall return -EINVAL; 11682c756febSChristopher S. Hall partial_history_cycles = cycles - system_counterval.cycles; 11692c756febSChristopher S. Hall total_history_cycles = cycles - history_begin->cycles; 11702c756febSChristopher S. Hall discontinuity = 11712c756febSChristopher S. Hall history_begin->clock_was_set_seq != clock_was_set_seq; 11722c756febSChristopher S. Hall 11732c756febSChristopher S. Hall ret = adjust_historical_crosststamp(history_begin, 11742c756febSChristopher S. Hall partial_history_cycles, 11752c756febSChristopher S. Hall total_history_cycles, 11762c756febSChristopher S. Hall discontinuity, xtstamp); 11772c756febSChristopher S. Hall if (ret) 11782c756febSChristopher S. Hall return ret; 11792c756febSChristopher S. Hall } 11802c756febSChristopher S. Hall 11818006c245SChristopher S. Hall return 0; 11828006c245SChristopher S. Hall } 11838006c245SChristopher S. Hall EXPORT_SYMBOL_GPL(get_device_system_crosststamp); 11848006c245SChristopher S. Hall 11858006c245SChristopher S. Hall /** 11868524070bSjohn stultz * do_gettimeofday - Returns the time of day in a timeval 11878524070bSjohn stultz * @tv: pointer to the timeval to be set 11888524070bSjohn stultz * 1189efd9ac86SGeert Uytterhoeven * NOTE: Users should be converted to using getnstimeofday() 11908524070bSjohn stultz */ 11918524070bSjohn stultz void do_gettimeofday(struct timeval *tv) 11928524070bSjohn stultz { 1193d6d29896SThomas Gleixner struct timespec64 now; 11948524070bSjohn stultz 1195d6d29896SThomas Gleixner getnstimeofday64(&now); 11968524070bSjohn stultz tv->tv_sec = now.tv_sec; 11978524070bSjohn stultz tv->tv_usec = now.tv_nsec/1000; 11988524070bSjohn stultz } 11998524070bSjohn stultz EXPORT_SYMBOL(do_gettimeofday); 1200d239f49dSRichard Cochran 12018524070bSjohn stultz /** 120221f7eca5Spang.xunlei * do_settimeofday64 - Sets the time of day. 120321f7eca5Spang.xunlei * @ts: pointer to the timespec64 variable containing the new time 12048524070bSjohn stultz * 12058524070bSjohn stultz * Sets the time of day to the new time and update NTP and notify hrtimers 12068524070bSjohn stultz */ 120721f7eca5Spang.xunlei int do_settimeofday64(const struct timespec64 *ts) 12088524070bSjohn stultz { 12093fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 121021f7eca5Spang.xunlei struct timespec64 ts_delta, xt; 121192c1d3edSJohn Stultz unsigned long flags; 1212e1d7ba87SWang YanQing int ret = 0; 12138524070bSjohn stultz 121421f7eca5Spang.xunlei if (!timespec64_valid_strict(ts)) 12158524070bSjohn stultz return -EINVAL; 12168524070bSjohn stultz 12179a7a71b1SThomas Gleixner raw_spin_lock_irqsave(&timekeeper_lock, flags); 12183fdb14fdSThomas Gleixner write_seqcount_begin(&tk_core.seq); 12198524070bSjohn stultz 12204e250fddSJohn Stultz timekeeping_forward_now(tk); 12218524070bSjohn stultz 12224e250fddSJohn Stultz xt = tk_xtime(tk); 122321f7eca5Spang.xunlei ts_delta.tv_sec = ts->tv_sec - xt.tv_sec; 122421f7eca5Spang.xunlei ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec; 12251e75fa8bSJohn Stultz 1226e1d7ba87SWang YanQing if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) { 1227e1d7ba87SWang YanQing ret = -EINVAL; 1228e1d7ba87SWang YanQing goto out; 1229e1d7ba87SWang YanQing } 1230e1d7ba87SWang YanQing 12317d489d15SJohn Stultz tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta)); 12328524070bSjohn stultz 123321f7eca5Spang.xunlei tk_set_xtime(tk, ts); 1234e1d7ba87SWang YanQing out: 1235780427f0SDavid Vrabel timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); 12368524070bSjohn stultz 12373fdb14fdSThomas Gleixner write_seqcount_end(&tk_core.seq); 12389a7a71b1SThomas Gleixner raw_spin_unlock_irqrestore(&timekeeper_lock, flags); 12398524070bSjohn stultz 12408524070bSjohn stultz /* signal hrtimers about time change */ 12418524070bSjohn stultz clock_was_set(); 12428524070bSjohn stultz 1243e1d7ba87SWang YanQing return ret; 12448524070bSjohn stultz } 124521f7eca5Spang.xunlei EXPORT_SYMBOL(do_settimeofday64); 12468524070bSjohn stultz 1247c528f7c6SJohn Stultz /** 1248c528f7c6SJohn Stultz * timekeeping_inject_offset - Adds or subtracts from the current time. 1249c528f7c6SJohn Stultz * @tv: pointer to the timespec variable containing the offset 1250c528f7c6SJohn Stultz * 1251c528f7c6SJohn Stultz * Adds or subtracts an offset value from the current time. 1252c528f7c6SJohn Stultz */ 12531572fa03SArnd Bergmann static int timekeeping_inject_offset(struct timespec64 *ts) 1254c528f7c6SJohn Stultz { 12553fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 125692c1d3edSJohn Stultz unsigned long flags; 12571572fa03SArnd Bergmann struct timespec64 tmp; 12584e8b1452SJohn Stultz int ret = 0; 1259c528f7c6SJohn Stultz 12601572fa03SArnd Bergmann if (ts->tv_nsec < 0 || ts->tv_nsec >= NSEC_PER_SEC) 1261c528f7c6SJohn Stultz return -EINVAL; 1262c528f7c6SJohn Stultz 12639a7a71b1SThomas Gleixner raw_spin_lock_irqsave(&timekeeper_lock, flags); 12643fdb14fdSThomas Gleixner write_seqcount_begin(&tk_core.seq); 1265c528f7c6SJohn Stultz 12664e250fddSJohn Stultz timekeeping_forward_now(tk); 1267c528f7c6SJohn Stultz 12684e8b1452SJohn Stultz /* Make sure the proposed value is valid */ 12691572fa03SArnd Bergmann tmp = timespec64_add(tk_xtime(tk), *ts); 12701572fa03SArnd Bergmann if (timespec64_compare(&tk->wall_to_monotonic, ts) > 0 || 1271e1d7ba87SWang YanQing !timespec64_valid_strict(&tmp)) { 12724e8b1452SJohn Stultz ret = -EINVAL; 12734e8b1452SJohn Stultz goto error; 12744e8b1452SJohn Stultz } 12751e75fa8bSJohn Stultz 12761572fa03SArnd Bergmann tk_xtime_add(tk, ts); 12771572fa03SArnd Bergmann tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *ts)); 1278c528f7c6SJohn Stultz 12794e8b1452SJohn Stultz error: /* even if we error out, we forwarded the time, so call update */ 1280780427f0SDavid Vrabel timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); 1281c528f7c6SJohn Stultz 12823fdb14fdSThomas Gleixner write_seqcount_end(&tk_core.seq); 12839a7a71b1SThomas Gleixner raw_spin_unlock_irqrestore(&timekeeper_lock, flags); 1284c528f7c6SJohn Stultz 1285c528f7c6SJohn Stultz /* signal hrtimers about time change */ 1286c528f7c6SJohn Stultz clock_was_set(); 1287c528f7c6SJohn Stultz 12884e8b1452SJohn Stultz return ret; 1289c528f7c6SJohn Stultz } 1290e0956dccSArnd Bergmann 1291e0956dccSArnd Bergmann /* 1292e0956dccSArnd Bergmann * Indicates if there is an offset between the system clock and the hardware 1293e0956dccSArnd Bergmann * clock/persistent clock/rtc. 1294e0956dccSArnd Bergmann */ 1295e0956dccSArnd Bergmann int persistent_clock_is_local; 1296e0956dccSArnd Bergmann 1297e0956dccSArnd Bergmann /* 1298e0956dccSArnd Bergmann * Adjust the time obtained from the CMOS to be UTC time instead of 1299e0956dccSArnd Bergmann * local time. 1300e0956dccSArnd Bergmann * 1301e0956dccSArnd Bergmann * This is ugly, but preferable to the alternatives. Otherwise we 1302e0956dccSArnd Bergmann * would either need to write a program to do it in /etc/rc (and risk 1303e0956dccSArnd Bergmann * confusion if the program gets run more than once; it would also be 1304e0956dccSArnd Bergmann * hard to make the program warp the clock precisely n hours) or 1305e0956dccSArnd Bergmann * compile in the timezone information into the kernel. Bad, bad.... 1306e0956dccSArnd Bergmann * 1307e0956dccSArnd Bergmann * - TYT, 1992-01-01 1308e0956dccSArnd Bergmann * 1309e0956dccSArnd Bergmann * The best thing to do is to keep the CMOS clock in universal time (UTC) 1310e0956dccSArnd Bergmann * as real UNIX machines always do it. This avoids all headaches about 1311e0956dccSArnd Bergmann * daylight saving times and warping kernel clocks. 1312e0956dccSArnd Bergmann */ 1313e0956dccSArnd Bergmann void timekeeping_warp_clock(void) 1314e0956dccSArnd Bergmann { 1315e0956dccSArnd Bergmann if (sys_tz.tz_minuteswest != 0) { 13161572fa03SArnd Bergmann struct timespec64 adjust; 1317e0956dccSArnd Bergmann 1318e0956dccSArnd Bergmann persistent_clock_is_local = 1; 1319e0956dccSArnd Bergmann adjust.tv_sec = sys_tz.tz_minuteswest * 60; 1320e0956dccSArnd Bergmann adjust.tv_nsec = 0; 1321e0956dccSArnd Bergmann timekeeping_inject_offset(&adjust); 1322e0956dccSArnd Bergmann } 1323e0956dccSArnd Bergmann } 1324c528f7c6SJohn Stultz 1325cc244ddaSJohn Stultz /** 132640d9f827SStephen Boyd * __timekeeping_set_tai_offset - Sets the TAI offset from UTC and monotonic 1327cc244ddaSJohn Stultz * 1328cc244ddaSJohn Stultz */ 1329dd5d70e8SFengguang Wu static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset) 1330cc244ddaSJohn Stultz { 1331cc244ddaSJohn Stultz tk->tai_offset = tai_offset; 133204005f60SJohn Stultz tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0)); 1333cc244ddaSJohn Stultz } 1334cc244ddaSJohn Stultz 1335cc244ddaSJohn Stultz /** 13368524070bSjohn stultz * change_clocksource - Swaps clocksources if a new one is available 13378524070bSjohn stultz * 13388524070bSjohn stultz * Accumulates current time interval and initializes new clocksource 13398524070bSjohn stultz */ 134075c5158fSMartin Schwidefsky static int change_clocksource(void *data) 13418524070bSjohn stultz { 13423fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 13434614e6adSMagnus Damm struct clocksource *new, *old; 1344f695cf94SJohn Stultz unsigned long flags; 13458524070bSjohn stultz 134675c5158fSMartin Schwidefsky new = (struct clocksource *) data; 13478524070bSjohn stultz 13489a7a71b1SThomas Gleixner raw_spin_lock_irqsave(&timekeeper_lock, flags); 13493fdb14fdSThomas Gleixner write_seqcount_begin(&tk_core.seq); 1350f695cf94SJohn Stultz 13514e250fddSJohn Stultz timekeeping_forward_now(tk); 135209ac369cSThomas Gleixner /* 135309ac369cSThomas Gleixner * If the cs is in module, get a module reference. Succeeds 135409ac369cSThomas Gleixner * for built-in code (owner == NULL) as well. 135509ac369cSThomas Gleixner */ 135609ac369cSThomas Gleixner if (try_module_get(new->owner)) { 135775c5158fSMartin Schwidefsky if (!new->enable || new->enable(new) == 0) { 1358876e7881SPeter Zijlstra old = tk->tkr_mono.clock; 13594e250fddSJohn Stultz tk_setup_internals(tk, new); 1360a0f7d48bSMartin Schwidefsky if (old->disable) 1361a0f7d48bSMartin Schwidefsky old->disable(old); 136209ac369cSThomas Gleixner module_put(old->owner); 136309ac369cSThomas Gleixner } else { 136409ac369cSThomas Gleixner module_put(new->owner); 136509ac369cSThomas Gleixner } 136675c5158fSMartin Schwidefsky } 1367780427f0SDavid Vrabel timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); 1368f695cf94SJohn Stultz 13693fdb14fdSThomas Gleixner write_seqcount_end(&tk_core.seq); 13709a7a71b1SThomas Gleixner raw_spin_unlock_irqrestore(&timekeeper_lock, flags); 1371f695cf94SJohn Stultz 137275c5158fSMartin Schwidefsky return 0; 137375c5158fSMartin Schwidefsky } 13744614e6adSMagnus Damm 137575c5158fSMartin Schwidefsky /** 137675c5158fSMartin Schwidefsky * timekeeping_notify - Install a new clock source 137775c5158fSMartin Schwidefsky * @clock: pointer to the clock source 137875c5158fSMartin Schwidefsky * 137975c5158fSMartin Schwidefsky * This function is called from clocksource.c after a new, better clock 138075c5158fSMartin Schwidefsky * source has been registered. The caller holds the clocksource_mutex. 138175c5158fSMartin Schwidefsky */ 1382ba919d1cSThomas Gleixner int timekeeping_notify(struct clocksource *clock) 138375c5158fSMartin Schwidefsky { 13843fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 13854e250fddSJohn Stultz 1386876e7881SPeter Zijlstra if (tk->tkr_mono.clock == clock) 1387ba919d1cSThomas Gleixner return 0; 138875c5158fSMartin Schwidefsky stop_machine(change_clocksource, clock, NULL); 13898524070bSjohn stultz tick_clock_notify(); 1390876e7881SPeter Zijlstra return tk->tkr_mono.clock == clock ? 0 : -1; 13918524070bSjohn stultz } 139275c5158fSMartin Schwidefsky 1393a40f262cSThomas Gleixner /** 1394*fb7fcc96SArnd Bergmann * ktime_get_raw_ts64 - Returns the raw monotonic time in a timespec 1395cdba2ec5SJohn Stultz * @ts: pointer to the timespec64 to be set 13962d42244aSJohn Stultz * 13972d42244aSJohn Stultz * Returns the raw monotonic time (completely un-modified by ntp) 13982d42244aSJohn Stultz */ 1399*fb7fcc96SArnd Bergmann void ktime_get_raw_ts64(struct timespec64 *ts) 14002d42244aSJohn Stultz { 14013fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 14022d42244aSJohn Stultz unsigned long seq; 1403acc89612SThomas Gleixner u64 nsecs; 14042d42244aSJohn Stultz 14052d42244aSJohn Stultz do { 14063fdb14fdSThomas Gleixner seq = read_seqcount_begin(&tk_core.seq); 1407fc6eead7SJohn Stultz ts->tv_sec = tk->raw_sec; 14084a4ad80dSPeter Zijlstra nsecs = timekeeping_get_ns(&tk->tkr_raw); 14092d42244aSJohn Stultz 14103fdb14fdSThomas Gleixner } while (read_seqcount_retry(&tk_core.seq, seq)); 14112d42244aSJohn Stultz 1412fc6eead7SJohn Stultz ts->tv_nsec = 0; 1413fc6eead7SJohn Stultz timespec64_add_ns(ts, nsecs); 14142d42244aSJohn Stultz } 1415*fb7fcc96SArnd Bergmann EXPORT_SYMBOL(ktime_get_raw_ts64); 1416cdba2ec5SJohn Stultz 14172d42244aSJohn Stultz 14182d42244aSJohn Stultz /** 1419cf4fc6cbSLi Zefan * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres 14208524070bSjohn stultz */ 1421cf4fc6cbSLi Zefan int timekeeping_valid_for_hres(void) 14228524070bSjohn stultz { 14233fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 14248524070bSjohn stultz unsigned long seq; 14258524070bSjohn stultz int ret; 14268524070bSjohn stultz 14278524070bSjohn stultz do { 14283fdb14fdSThomas Gleixner seq = read_seqcount_begin(&tk_core.seq); 14298524070bSjohn stultz 1430876e7881SPeter Zijlstra ret = tk->tkr_mono.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; 14318524070bSjohn stultz 14323fdb14fdSThomas Gleixner } while (read_seqcount_retry(&tk_core.seq, seq)); 14338524070bSjohn stultz 14348524070bSjohn stultz return ret; 14358524070bSjohn stultz } 14368524070bSjohn stultz 14378524070bSjohn stultz /** 143898962465SJon Hunter * timekeeping_max_deferment - Returns max time the clocksource can be deferred 143998962465SJon Hunter */ 144098962465SJon Hunter u64 timekeeping_max_deferment(void) 144198962465SJon Hunter { 14423fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 144370471f2fSJohn Stultz unsigned long seq; 144470471f2fSJohn Stultz u64 ret; 144542e71e81SJohn Stultz 144670471f2fSJohn Stultz do { 14473fdb14fdSThomas Gleixner seq = read_seqcount_begin(&tk_core.seq); 144870471f2fSJohn Stultz 1449876e7881SPeter Zijlstra ret = tk->tkr_mono.clock->max_idle_ns; 145070471f2fSJohn Stultz 14513fdb14fdSThomas Gleixner } while (read_seqcount_retry(&tk_core.seq, seq)); 145270471f2fSJohn Stultz 145370471f2fSJohn Stultz return ret; 145498962465SJon Hunter } 145598962465SJon Hunter 145698962465SJon Hunter /** 1457d4f587c6SMartin Schwidefsky * read_persistent_clock - Return time from the persistent clock. 14588524070bSjohn stultz * 14598524070bSjohn stultz * Weak dummy function for arches that do not yet support it. 1460d4f587c6SMartin Schwidefsky * Reads the time from the battery backed persistent clock. 1461d4f587c6SMartin Schwidefsky * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported. 14628524070bSjohn stultz * 14638524070bSjohn stultz * XXX - Do be sure to remove it once all arches implement it. 14648524070bSjohn stultz */ 146552f5684cSGideon Israel Dsouza void __weak read_persistent_clock(struct timespec *ts) 14668524070bSjohn stultz { 1467d4f587c6SMartin Schwidefsky ts->tv_sec = 0; 1468d4f587c6SMartin Schwidefsky ts->tv_nsec = 0; 14698524070bSjohn stultz } 14708524070bSjohn stultz 14712ee96632SXunlei Pang void __weak read_persistent_clock64(struct timespec64 *ts64) 14722ee96632SXunlei Pang { 14732ee96632SXunlei Pang struct timespec ts; 14742ee96632SXunlei Pang 14752ee96632SXunlei Pang read_persistent_clock(&ts); 14762ee96632SXunlei Pang *ts64 = timespec_to_timespec64(ts); 14772ee96632SXunlei Pang } 14782ee96632SXunlei Pang 147923970e38SMartin Schwidefsky /** 1480e83d0a41SXunlei Pang * read_boot_clock64 - Return time of the system start. 148123970e38SMartin Schwidefsky * 148223970e38SMartin Schwidefsky * Weak dummy function for arches that do not yet support it. 148323970e38SMartin Schwidefsky * Function to read the exact time the system has been started. 1484e83d0a41SXunlei Pang * Returns a timespec64 with tv_sec=0 and tv_nsec=0 if unsupported. 148523970e38SMartin Schwidefsky * 148623970e38SMartin Schwidefsky * XXX - Do be sure to remove it once all arches implement it. 148723970e38SMartin Schwidefsky */ 1488e83d0a41SXunlei Pang void __weak read_boot_clock64(struct timespec64 *ts) 148923970e38SMartin Schwidefsky { 149023970e38SMartin Schwidefsky ts->tv_sec = 0; 149123970e38SMartin Schwidefsky ts->tv_nsec = 0; 149223970e38SMartin Schwidefsky } 149323970e38SMartin Schwidefsky 14940fa88cb4SXunlei Pang /* Flag for if timekeeping_resume() has injected sleeptime */ 14950fa88cb4SXunlei Pang static bool sleeptime_injected; 14960fa88cb4SXunlei Pang 14970fa88cb4SXunlei Pang /* Flag for if there is a persistent clock on this platform */ 14980fa88cb4SXunlei Pang static bool persistent_clock_exists; 14990fa88cb4SXunlei Pang 15008524070bSjohn stultz /* 15018524070bSjohn stultz * timekeeping_init - Initializes the clocksource and common timekeeping values 15028524070bSjohn stultz */ 15038524070bSjohn stultz void __init timekeeping_init(void) 15048524070bSjohn stultz { 15053fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 1506155ec602SMartin Schwidefsky struct clocksource *clock; 15078524070bSjohn stultz unsigned long flags; 15087d489d15SJohn Stultz struct timespec64 now, boot, tmp; 1509d4f587c6SMartin Schwidefsky 15102ee96632SXunlei Pang read_persistent_clock64(&now); 15117d489d15SJohn Stultz if (!timespec64_valid_strict(&now)) { 15124e8b1452SJohn Stultz pr_warn("WARNING: Persistent clock returned invalid value!\n" 15134e8b1452SJohn Stultz " Check your CMOS/BIOS settings.\n"); 15144e8b1452SJohn Stultz now.tv_sec = 0; 15154e8b1452SJohn Stultz now.tv_nsec = 0; 151631ade306SFeng Tang } else if (now.tv_sec || now.tv_nsec) 15170fa88cb4SXunlei Pang persistent_clock_exists = true; 15184e8b1452SJohn Stultz 15199a806ddbSXunlei Pang read_boot_clock64(&boot); 15207d489d15SJohn Stultz if (!timespec64_valid_strict(&boot)) { 15214e8b1452SJohn Stultz pr_warn("WARNING: Boot clock returned invalid value!\n" 15224e8b1452SJohn Stultz " Check your CMOS/BIOS settings.\n"); 15234e8b1452SJohn Stultz boot.tv_sec = 0; 15244e8b1452SJohn Stultz boot.tv_nsec = 0; 15254e8b1452SJohn Stultz } 15268524070bSjohn stultz 15279a7a71b1SThomas Gleixner raw_spin_lock_irqsave(&timekeeper_lock, flags); 15283fdb14fdSThomas Gleixner write_seqcount_begin(&tk_core.seq); 152906c017fdSJohn Stultz ntp_init(); 153006c017fdSJohn Stultz 1531f1b82746SMartin Schwidefsky clock = clocksource_default_clock(); 1532a0f7d48bSMartin Schwidefsky if (clock->enable) 1533a0f7d48bSMartin Schwidefsky clock->enable(clock); 15344e250fddSJohn Stultz tk_setup_internals(tk, clock); 15358524070bSjohn stultz 15364e250fddSJohn Stultz tk_set_xtime(tk, &now); 1537fc6eead7SJohn Stultz tk->raw_sec = 0; 15381e75fa8bSJohn Stultz if (boot.tv_sec == 0 && boot.tv_nsec == 0) 15394e250fddSJohn Stultz boot = tk_xtime(tk); 15401e75fa8bSJohn Stultz 15417d489d15SJohn Stultz set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec); 15424e250fddSJohn Stultz tk_set_wall_to_mono(tk, tmp); 15436d0ef903SJohn Stultz 154456fd16caSThomas Gleixner timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); 154548cdc135SThomas Gleixner 15463fdb14fdSThomas Gleixner write_seqcount_end(&tk_core.seq); 15479a7a71b1SThomas Gleixner raw_spin_unlock_irqrestore(&timekeeper_lock, flags); 15488524070bSjohn stultz } 15498524070bSjohn stultz 1550264bb3f7SXunlei Pang /* time in seconds when suspend began for persistent clock */ 15517d489d15SJohn Stultz static struct timespec64 timekeeping_suspend_time; 15528524070bSjohn stultz 15538524070bSjohn stultz /** 1554304529b1SJohn Stultz * __timekeeping_inject_sleeptime - Internal function to add sleep interval 1555304529b1SJohn Stultz * @delta: pointer to a timespec delta value 1556304529b1SJohn Stultz * 1557304529b1SJohn Stultz * Takes a timespec offset measuring a suspend interval and properly 1558304529b1SJohn Stultz * adds the sleep offset to the timekeeping variables. 1559304529b1SJohn Stultz */ 1560f726a697SJohn Stultz static void __timekeeping_inject_sleeptime(struct timekeeper *tk, 15617d489d15SJohn Stultz struct timespec64 *delta) 1562304529b1SJohn Stultz { 15637d489d15SJohn Stultz if (!timespec64_valid_strict(delta)) { 15646d9bcb62SJohn Stultz printk_deferred(KERN_WARNING 15656d9bcb62SJohn Stultz "__timekeeping_inject_sleeptime: Invalid " 1566cb5de2f8SJohn Stultz "sleep delta value!\n"); 1567cb5de2f8SJohn Stultz return; 1568cb5de2f8SJohn Stultz } 1569f726a697SJohn Stultz tk_xtime_add(tk, delta); 1570a3ed0e43SThomas Gleixner tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta)); 157147da70d3SThomas Gleixner tk_update_sleep_time(tk, timespec64_to_ktime(*delta)); 15725c83545fSColin Cross tk_debug_account_sleep_time(delta); 1573304529b1SJohn Stultz } 1574304529b1SJohn Stultz 15757f298139SXunlei Pang #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE) 1576304529b1SJohn Stultz /** 15770fa88cb4SXunlei Pang * We have three kinds of time sources to use for sleep time 15780fa88cb4SXunlei Pang * injection, the preference order is: 15790fa88cb4SXunlei Pang * 1) non-stop clocksource 15800fa88cb4SXunlei Pang * 2) persistent clock (ie: RTC accessible when irqs are off) 15810fa88cb4SXunlei Pang * 3) RTC 15820fa88cb4SXunlei Pang * 15830fa88cb4SXunlei Pang * 1) and 2) are used by timekeeping, 3) by RTC subsystem. 15840fa88cb4SXunlei Pang * If system has neither 1) nor 2), 3) will be used finally. 15850fa88cb4SXunlei Pang * 15860fa88cb4SXunlei Pang * 15870fa88cb4SXunlei Pang * If timekeeping has injected sleeptime via either 1) or 2), 15880fa88cb4SXunlei Pang * 3) becomes needless, so in this case we don't need to call 15890fa88cb4SXunlei Pang * rtc_resume(), and this is what timekeeping_rtc_skipresume() 15900fa88cb4SXunlei Pang * means. 15910fa88cb4SXunlei Pang */ 15920fa88cb4SXunlei Pang bool timekeeping_rtc_skipresume(void) 15930fa88cb4SXunlei Pang { 15940fa88cb4SXunlei Pang return sleeptime_injected; 15950fa88cb4SXunlei Pang } 15960fa88cb4SXunlei Pang 15970fa88cb4SXunlei Pang /** 15980fa88cb4SXunlei Pang * 1) can be determined whether to use or not only when doing 15990fa88cb4SXunlei Pang * timekeeping_resume() which is invoked after rtc_suspend(), 16000fa88cb4SXunlei Pang * so we can't skip rtc_suspend() surely if system has 1). 16010fa88cb4SXunlei Pang * 16020fa88cb4SXunlei Pang * But if system has 2), 2) will definitely be used, so in this 16030fa88cb4SXunlei Pang * case we don't need to call rtc_suspend(), and this is what 16040fa88cb4SXunlei Pang * timekeeping_rtc_skipsuspend() means. 16050fa88cb4SXunlei Pang */ 16060fa88cb4SXunlei Pang bool timekeeping_rtc_skipsuspend(void) 16070fa88cb4SXunlei Pang { 16080fa88cb4SXunlei Pang return persistent_clock_exists; 16090fa88cb4SXunlei Pang } 16100fa88cb4SXunlei Pang 16110fa88cb4SXunlei Pang /** 161204d90890Spang.xunlei * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values 161304d90890Spang.xunlei * @delta: pointer to a timespec64 delta value 1614304529b1SJohn Stultz * 16152ee96632SXunlei Pang * This hook is for architectures that cannot support read_persistent_clock64 1616304529b1SJohn Stultz * because their RTC/persistent clock is only accessible when irqs are enabled. 16170fa88cb4SXunlei Pang * and also don't have an effective nonstop clocksource. 1618304529b1SJohn Stultz * 1619304529b1SJohn Stultz * This function should only be called by rtc_resume(), and allows 1620304529b1SJohn Stultz * a suspend offset to be injected into the timekeeping values. 1621304529b1SJohn Stultz */ 162204d90890Spang.xunlei void timekeeping_inject_sleeptime64(struct timespec64 *delta) 1623304529b1SJohn Stultz { 16243fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 162592c1d3edSJohn Stultz unsigned long flags; 1626304529b1SJohn Stultz 16279a7a71b1SThomas Gleixner raw_spin_lock_irqsave(&timekeeper_lock, flags); 16283fdb14fdSThomas Gleixner write_seqcount_begin(&tk_core.seq); 162970471f2fSJohn Stultz 16304e250fddSJohn Stultz timekeeping_forward_now(tk); 1631304529b1SJohn Stultz 163204d90890Spang.xunlei __timekeeping_inject_sleeptime(tk, delta); 1633304529b1SJohn Stultz 1634780427f0SDavid Vrabel timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); 1635304529b1SJohn Stultz 16363fdb14fdSThomas Gleixner write_seqcount_end(&tk_core.seq); 16379a7a71b1SThomas Gleixner raw_spin_unlock_irqrestore(&timekeeper_lock, flags); 1638304529b1SJohn Stultz 1639304529b1SJohn Stultz /* signal hrtimers about time change */ 1640304529b1SJohn Stultz clock_was_set(); 1641304529b1SJohn Stultz } 16427f298139SXunlei Pang #endif 1643304529b1SJohn Stultz 1644304529b1SJohn Stultz /** 16458524070bSjohn stultz * timekeeping_resume - Resumes the generic timekeeping subsystem. 16468524070bSjohn stultz */ 1647124cf911SRafael J. Wysocki void timekeeping_resume(void) 16488524070bSjohn stultz { 16493fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 1650876e7881SPeter Zijlstra struct clocksource *clock = tk->tkr_mono.clock; 165192c1d3edSJohn Stultz unsigned long flags; 16527d489d15SJohn Stultz struct timespec64 ts_new, ts_delta; 1653a5a1d1c2SThomas Gleixner u64 cycle_now; 1654d4f587c6SMartin Schwidefsky 16550fa88cb4SXunlei Pang sleeptime_injected = false; 16562ee96632SXunlei Pang read_persistent_clock64(&ts_new); 16578524070bSjohn stultz 1658adc78e6bSRafael J. Wysocki clockevents_resume(); 1659d10ff3fbSThomas Gleixner clocksource_resume(); 1660d10ff3fbSThomas Gleixner 16619a7a71b1SThomas Gleixner raw_spin_lock_irqsave(&timekeeper_lock, flags); 16623fdb14fdSThomas Gleixner write_seqcount_begin(&tk_core.seq); 16638524070bSjohn stultz 1664e445cf1cSFeng Tang /* 1665e445cf1cSFeng Tang * After system resumes, we need to calculate the suspended time and 1666e445cf1cSFeng Tang * compensate it for the OS time. There are 3 sources that could be 1667e445cf1cSFeng Tang * used: Nonstop clocksource during suspend, persistent clock and rtc 1668e445cf1cSFeng Tang * device. 1669e445cf1cSFeng Tang * 1670e445cf1cSFeng Tang * One specific platform may have 1 or 2 or all of them, and the 1671e445cf1cSFeng Tang * preference will be: 1672e445cf1cSFeng Tang * suspend-nonstop clocksource -> persistent clock -> rtc 1673e445cf1cSFeng Tang * The less preferred source will only be tried if there is no better 1674e445cf1cSFeng Tang * usable source. The rtc part is handled separately in rtc core code. 1675e445cf1cSFeng Tang */ 1676ceea5e37SJohn Stultz cycle_now = tk_clock_read(&tk->tkr_mono); 1677e445cf1cSFeng Tang if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) && 1678876e7881SPeter Zijlstra cycle_now > tk->tkr_mono.cycle_last) { 1679c029a2beSThomas Gleixner u64 nsec, cyc_delta; 1680e445cf1cSFeng Tang 1681c029a2beSThomas Gleixner cyc_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, 1682876e7881SPeter Zijlstra tk->tkr_mono.mask); 1683c029a2beSThomas Gleixner nsec = mul_u64_u32_shr(cyc_delta, clock->mult, clock->shift); 16847d489d15SJohn Stultz ts_delta = ns_to_timespec64(nsec); 16850fa88cb4SXunlei Pang sleeptime_injected = true; 16867d489d15SJohn Stultz } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) { 16877d489d15SJohn Stultz ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time); 16880fa88cb4SXunlei Pang sleeptime_injected = true; 1689e445cf1cSFeng Tang } 1690e445cf1cSFeng Tang 16910fa88cb4SXunlei Pang if (sleeptime_injected) 1692e445cf1cSFeng Tang __timekeeping_inject_sleeptime(tk, &ts_delta); 1693e445cf1cSFeng Tang 1694e445cf1cSFeng Tang /* Re-base the last cycle value */ 1695876e7881SPeter Zijlstra tk->tkr_mono.cycle_last = cycle_now; 16964a4ad80dSPeter Zijlstra tk->tkr_raw.cycle_last = cycle_now; 16974a4ad80dSPeter Zijlstra 16984e250fddSJohn Stultz tk->ntp_error = 0; 16998524070bSjohn stultz timekeeping_suspended = 0; 1700780427f0SDavid Vrabel timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); 17013fdb14fdSThomas Gleixner write_seqcount_end(&tk_core.seq); 17029a7a71b1SThomas Gleixner raw_spin_unlock_irqrestore(&timekeeper_lock, flags); 17038524070bSjohn stultz 17048524070bSjohn stultz touch_softlockup_watchdog(); 17058524070bSjohn stultz 17064ffee521SThomas Gleixner tick_resume(); 1707b12a03ceSThomas Gleixner hrtimers_resume(); 17088524070bSjohn stultz } 17098524070bSjohn stultz 1710124cf911SRafael J. Wysocki int timekeeping_suspend(void) 17118524070bSjohn stultz { 17123fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 171392c1d3edSJohn Stultz unsigned long flags; 17147d489d15SJohn Stultz struct timespec64 delta, delta_delta; 17157d489d15SJohn Stultz static struct timespec64 old_delta; 17168524070bSjohn stultz 17172ee96632SXunlei Pang read_persistent_clock64(&timekeeping_suspend_time); 17183be90950SThomas Gleixner 17190d6bd995SZoran Markovic /* 17200d6bd995SZoran Markovic * On some systems the persistent_clock can not be detected at 17210d6bd995SZoran Markovic * timekeeping_init by its return value, so if we see a valid 17220d6bd995SZoran Markovic * value returned, update the persistent_clock_exists flag. 17230d6bd995SZoran Markovic */ 17240d6bd995SZoran Markovic if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec) 17250fa88cb4SXunlei Pang persistent_clock_exists = true; 17260d6bd995SZoran Markovic 17279a7a71b1SThomas Gleixner raw_spin_lock_irqsave(&timekeeper_lock, flags); 17283fdb14fdSThomas Gleixner write_seqcount_begin(&tk_core.seq); 17294e250fddSJohn Stultz timekeeping_forward_now(tk); 17308524070bSjohn stultz timekeeping_suspended = 1; 1731cb33217bSJohn Stultz 17320fa88cb4SXunlei Pang if (persistent_clock_exists) { 1733cb33217bSJohn Stultz /* 1734cb33217bSJohn Stultz * To avoid drift caused by repeated suspend/resumes, 1735cb33217bSJohn Stultz * which each can add ~1 second drift error, 1736cb33217bSJohn Stultz * try to compensate so the difference in system time 1737cb33217bSJohn Stultz * and persistent_clock time stays close to constant. 1738cb33217bSJohn Stultz */ 17397d489d15SJohn Stultz delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time); 17407d489d15SJohn Stultz delta_delta = timespec64_sub(delta, old_delta); 1741cb33217bSJohn Stultz if (abs(delta_delta.tv_sec) >= 2) { 1742cb33217bSJohn Stultz /* 1743cb33217bSJohn Stultz * if delta_delta is too large, assume time correction 1744264bb3f7SXunlei Pang * has occurred and set old_delta to the current delta. 1745cb33217bSJohn Stultz */ 1746cb33217bSJohn Stultz old_delta = delta; 1747cb33217bSJohn Stultz } else { 1748cb33217bSJohn Stultz /* Otherwise try to adjust old_system to compensate */ 1749cb33217bSJohn Stultz timekeeping_suspend_time = 17507d489d15SJohn Stultz timespec64_add(timekeeping_suspend_time, delta_delta); 1751cb33217bSJohn Stultz } 1752264bb3f7SXunlei Pang } 1753330a1617SJohn Stultz 1754330a1617SJohn Stultz timekeeping_update(tk, TK_MIRROR); 1755060407aeSRafael J. Wysocki halt_fast_timekeeper(tk); 17563fdb14fdSThomas Gleixner write_seqcount_end(&tk_core.seq); 17579a7a71b1SThomas Gleixner raw_spin_unlock_irqrestore(&timekeeper_lock, flags); 17588524070bSjohn stultz 17594ffee521SThomas Gleixner tick_suspend(); 1760c54a42b1SMagnus Damm clocksource_suspend(); 1761adc78e6bSRafael J. Wysocki clockevents_suspend(); 17628524070bSjohn stultz 17638524070bSjohn stultz return 0; 17648524070bSjohn stultz } 17658524070bSjohn stultz 17668524070bSjohn stultz /* sysfs resume/suspend bits for timekeeping */ 1767e1a85b2cSRafael J. Wysocki static struct syscore_ops timekeeping_syscore_ops = { 17688524070bSjohn stultz .resume = timekeeping_resume, 17698524070bSjohn stultz .suspend = timekeeping_suspend, 17708524070bSjohn stultz }; 17718524070bSjohn stultz 1772e1a85b2cSRafael J. Wysocki static int __init timekeeping_init_ops(void) 17738524070bSjohn stultz { 1774e1a85b2cSRafael J. Wysocki register_syscore_ops(&timekeeping_syscore_ops); 1775e1a85b2cSRafael J. Wysocki return 0; 17768524070bSjohn stultz } 1777e1a85b2cSRafael J. Wysocki device_initcall(timekeeping_init_ops); 17788524070bSjohn stultz 17798524070bSjohn stultz /* 1780dc491596SJohn Stultz * Apply a multiplier adjustment to the timekeeper 17818524070bSjohn stultz */ 1782dc491596SJohn Stultz static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, 1783dc491596SJohn Stultz s64 offset, 178478b98e3cSMiroslav Lichvar s32 mult_adj) 17858524070bSjohn stultz { 1786dc491596SJohn Stultz s64 interval = tk->cycle_interval; 17878524070bSjohn stultz 178878b98e3cSMiroslav Lichvar if (mult_adj == 0) { 178978b98e3cSMiroslav Lichvar return; 179078b98e3cSMiroslav Lichvar } else if (mult_adj == -1) { 17918524070bSjohn stultz interval = -interval; 17928524070bSjohn stultz offset = -offset; 179378b98e3cSMiroslav Lichvar } else if (mult_adj != 1) { 179478b98e3cSMiroslav Lichvar interval *= mult_adj; 179578b98e3cSMiroslav Lichvar offset *= mult_adj; 17961d17d174SIngo Molnar } 17978524070bSjohn stultz 1798c2bc1111SJohn Stultz /* 1799c2bc1111SJohn Stultz * So the following can be confusing. 1800c2bc1111SJohn Stultz * 1801dc491596SJohn Stultz * To keep things simple, lets assume mult_adj == 1 for now. 1802c2bc1111SJohn Stultz * 1803dc491596SJohn Stultz * When mult_adj != 1, remember that the interval and offset values 1804c2bc1111SJohn Stultz * have been appropriately scaled so the math is the same. 1805c2bc1111SJohn Stultz * 1806c2bc1111SJohn Stultz * The basic idea here is that we're increasing the multiplier 1807c2bc1111SJohn Stultz * by one, this causes the xtime_interval to be incremented by 1808c2bc1111SJohn Stultz * one cycle_interval. This is because: 1809c2bc1111SJohn Stultz * xtime_interval = cycle_interval * mult 1810c2bc1111SJohn Stultz * So if mult is being incremented by one: 1811c2bc1111SJohn Stultz * xtime_interval = cycle_interval * (mult + 1) 1812c2bc1111SJohn Stultz * Its the same as: 1813c2bc1111SJohn Stultz * xtime_interval = (cycle_interval * mult) + cycle_interval 1814c2bc1111SJohn Stultz * Which can be shortened to: 1815c2bc1111SJohn Stultz * xtime_interval += cycle_interval 1816c2bc1111SJohn Stultz * 1817c2bc1111SJohn Stultz * So offset stores the non-accumulated cycles. Thus the current 1818c2bc1111SJohn Stultz * time (in shifted nanoseconds) is: 1819c2bc1111SJohn Stultz * now = (offset * adj) + xtime_nsec 1820c2bc1111SJohn Stultz * Now, even though we're adjusting the clock frequency, we have 1821c2bc1111SJohn Stultz * to keep time consistent. In other words, we can't jump back 1822c2bc1111SJohn Stultz * in time, and we also want to avoid jumping forward in time. 1823c2bc1111SJohn Stultz * 1824c2bc1111SJohn Stultz * So given the same offset value, we need the time to be the same 1825c2bc1111SJohn Stultz * both before and after the freq adjustment. 1826c2bc1111SJohn Stultz * now = (offset * adj_1) + xtime_nsec_1 1827c2bc1111SJohn Stultz * now = (offset * adj_2) + xtime_nsec_2 1828c2bc1111SJohn Stultz * So: 1829c2bc1111SJohn Stultz * (offset * adj_1) + xtime_nsec_1 = 1830c2bc1111SJohn Stultz * (offset * adj_2) + xtime_nsec_2 1831c2bc1111SJohn Stultz * And we know: 1832c2bc1111SJohn Stultz * adj_2 = adj_1 + 1 1833c2bc1111SJohn Stultz * So: 1834c2bc1111SJohn Stultz * (offset * adj_1) + xtime_nsec_1 = 1835c2bc1111SJohn Stultz * (offset * (adj_1+1)) + xtime_nsec_2 1836c2bc1111SJohn Stultz * (offset * adj_1) + xtime_nsec_1 = 1837c2bc1111SJohn Stultz * (offset * adj_1) + offset + xtime_nsec_2 1838c2bc1111SJohn Stultz * Canceling the sides: 1839c2bc1111SJohn Stultz * xtime_nsec_1 = offset + xtime_nsec_2 1840c2bc1111SJohn Stultz * Which gives us: 1841c2bc1111SJohn Stultz * xtime_nsec_2 = xtime_nsec_1 - offset 1842c2bc1111SJohn Stultz * Which simplfies to: 1843c2bc1111SJohn Stultz * xtime_nsec -= offset 1844c2bc1111SJohn Stultz */ 1845876e7881SPeter Zijlstra if ((mult_adj > 0) && (tk->tkr_mono.mult + mult_adj < mult_adj)) { 18466067dc5aSpang.xunlei /* NTP adjustment caused clocksource mult overflow */ 18476067dc5aSpang.xunlei WARN_ON_ONCE(1); 18486067dc5aSpang.xunlei return; 18496067dc5aSpang.xunlei } 18506067dc5aSpang.xunlei 1851876e7881SPeter Zijlstra tk->tkr_mono.mult += mult_adj; 1852f726a697SJohn Stultz tk->xtime_interval += interval; 1853876e7881SPeter Zijlstra tk->tkr_mono.xtime_nsec -= offset; 1854dc491596SJohn Stultz } 18552a8c0883SJohn Stultz 1856dc491596SJohn Stultz /* 1857dc491596SJohn Stultz * Adjust the timekeeper's multiplier to the correct frequency 1858dc491596SJohn Stultz * and also to reduce the accumulated error value. 1859dc491596SJohn Stultz */ 1860dc491596SJohn Stultz static void timekeeping_adjust(struct timekeeper *tk, s64 offset) 1861dc491596SJohn Stultz { 186278b98e3cSMiroslav Lichvar u32 mult; 1863dc491596SJohn Stultz 186478b98e3cSMiroslav Lichvar /* 186578b98e3cSMiroslav Lichvar * Determine the multiplier from the current NTP tick length. 186678b98e3cSMiroslav Lichvar * Avoid expensive division when the tick length doesn't change. 186778b98e3cSMiroslav Lichvar */ 186878b98e3cSMiroslav Lichvar if (likely(tk->ntp_tick == ntp_tick_length())) { 186978b98e3cSMiroslav Lichvar mult = tk->tkr_mono.mult - tk->ntp_err_mult; 187078b98e3cSMiroslav Lichvar } else { 187178b98e3cSMiroslav Lichvar tk->ntp_tick = ntp_tick_length(); 187278b98e3cSMiroslav Lichvar mult = div64_u64((tk->ntp_tick >> tk->ntp_error_shift) - 187378b98e3cSMiroslav Lichvar tk->xtime_remainder, tk->cycle_interval); 1874dc491596SJohn Stultz } 1875dc491596SJohn Stultz 187678b98e3cSMiroslav Lichvar /* 187778b98e3cSMiroslav Lichvar * If the clock is behind the NTP time, increase the multiplier by 1 187878b98e3cSMiroslav Lichvar * to catch up with it. If it's ahead and there was a remainder in the 187978b98e3cSMiroslav Lichvar * tick division, the clock will slow down. Otherwise it will stay 188078b98e3cSMiroslav Lichvar * ahead until the tick length changes to a non-divisible value. 188178b98e3cSMiroslav Lichvar */ 188278b98e3cSMiroslav Lichvar tk->ntp_err_mult = tk->ntp_error > 0 ? 1 : 0; 188378b98e3cSMiroslav Lichvar mult += tk->ntp_err_mult; 188478b98e3cSMiroslav Lichvar 188578b98e3cSMiroslav Lichvar timekeeping_apply_adjustment(tk, offset, mult - tk->tkr_mono.mult); 188678b98e3cSMiroslav Lichvar 1887876e7881SPeter Zijlstra if (unlikely(tk->tkr_mono.clock->maxadj && 1888876e7881SPeter Zijlstra (abs(tk->tkr_mono.mult - tk->tkr_mono.clock->mult) 1889876e7881SPeter Zijlstra > tk->tkr_mono.clock->maxadj))) { 1890dc491596SJohn Stultz printk_once(KERN_WARNING 1891dc491596SJohn Stultz "Adjusting %s more than 11%% (%ld vs %ld)\n", 1892876e7881SPeter Zijlstra tk->tkr_mono.clock->name, (long)tk->tkr_mono.mult, 1893876e7881SPeter Zijlstra (long)tk->tkr_mono.clock->mult + tk->tkr_mono.clock->maxadj); 1894dc491596SJohn Stultz } 1895dc491596SJohn Stultz 18962a8c0883SJohn Stultz /* 18972a8c0883SJohn Stultz * It may be possible that when we entered this function, xtime_nsec 18982a8c0883SJohn Stultz * was very small. Further, if we're slightly speeding the clocksource 18992a8c0883SJohn Stultz * in the code above, its possible the required corrective factor to 19002a8c0883SJohn Stultz * xtime_nsec could cause it to underflow. 19012a8c0883SJohn Stultz * 190278b98e3cSMiroslav Lichvar * Now, since we have already accumulated the second and the NTP 190378b98e3cSMiroslav Lichvar * subsystem has been notified via second_overflow(), we need to skip 190478b98e3cSMiroslav Lichvar * the next update. 19052a8c0883SJohn Stultz */ 1906876e7881SPeter Zijlstra if (unlikely((s64)tk->tkr_mono.xtime_nsec < 0)) { 190778b98e3cSMiroslav Lichvar tk->tkr_mono.xtime_nsec += (u64)NSEC_PER_SEC << 190878b98e3cSMiroslav Lichvar tk->tkr_mono.shift; 190978b98e3cSMiroslav Lichvar tk->xtime_sec--; 191078b98e3cSMiroslav Lichvar tk->skip_second_overflow = 1; 19112a8c0883SJohn Stultz } 19128524070bSjohn stultz } 19138524070bSjohn stultz 19148524070bSjohn stultz /** 19151f4f9487SJohn Stultz * accumulate_nsecs_to_secs - Accumulates nsecs into secs 19161f4f9487SJohn Stultz * 1917571af55aSZhen Lei * Helper function that accumulates the nsecs greater than a second 19181f4f9487SJohn Stultz * from the xtime_nsec field to the xtime_secs field. 19191f4f9487SJohn Stultz * It also calls into the NTP code to handle leapsecond processing. 19201f4f9487SJohn Stultz * 19211f4f9487SJohn Stultz */ 1922780427f0SDavid Vrabel static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk) 19231f4f9487SJohn Stultz { 1924876e7881SPeter Zijlstra u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr_mono.shift; 19255258d3f2SJohn Stultz unsigned int clock_set = 0; 19261f4f9487SJohn Stultz 1927876e7881SPeter Zijlstra while (tk->tkr_mono.xtime_nsec >= nsecps) { 19281f4f9487SJohn Stultz int leap; 19291f4f9487SJohn Stultz 1930876e7881SPeter Zijlstra tk->tkr_mono.xtime_nsec -= nsecps; 19311f4f9487SJohn Stultz tk->xtime_sec++; 19321f4f9487SJohn Stultz 193378b98e3cSMiroslav Lichvar /* 193478b98e3cSMiroslav Lichvar * Skip NTP update if this second was accumulated before, 193578b98e3cSMiroslav Lichvar * i.e. xtime_nsec underflowed in timekeeping_adjust() 193678b98e3cSMiroslav Lichvar */ 193778b98e3cSMiroslav Lichvar if (unlikely(tk->skip_second_overflow)) { 193878b98e3cSMiroslav Lichvar tk->skip_second_overflow = 0; 193978b98e3cSMiroslav Lichvar continue; 194078b98e3cSMiroslav Lichvar } 194178b98e3cSMiroslav Lichvar 19421f4f9487SJohn Stultz /* Figure out if its a leap sec and apply if needed */ 19431f4f9487SJohn Stultz leap = second_overflow(tk->xtime_sec); 19446d0ef903SJohn Stultz if (unlikely(leap)) { 19457d489d15SJohn Stultz struct timespec64 ts; 19461f4f9487SJohn Stultz 19476d0ef903SJohn Stultz tk->xtime_sec += leap; 19486d0ef903SJohn Stultz 19496d0ef903SJohn Stultz ts.tv_sec = leap; 19506d0ef903SJohn Stultz ts.tv_nsec = 0; 19516d0ef903SJohn Stultz tk_set_wall_to_mono(tk, 19527d489d15SJohn Stultz timespec64_sub(tk->wall_to_monotonic, ts)); 19536d0ef903SJohn Stultz 1954cc244ddaSJohn Stultz __timekeeping_set_tai_offset(tk, tk->tai_offset - leap); 1955cc244ddaSJohn Stultz 19565258d3f2SJohn Stultz clock_set = TK_CLOCK_WAS_SET; 19576d0ef903SJohn Stultz } 19581f4f9487SJohn Stultz } 19595258d3f2SJohn Stultz return clock_set; 19601f4f9487SJohn Stultz } 19611f4f9487SJohn Stultz 19621f4f9487SJohn Stultz /** 1963a092ff0fSjohn stultz * logarithmic_accumulation - shifted accumulation of cycles 1964a092ff0fSjohn stultz * 1965a092ff0fSjohn stultz * This functions accumulates a shifted interval of cycles into 1966a092ff0fSjohn stultz * into a shifted interval nanoseconds. Allows for O(log) accumulation 1967a092ff0fSjohn stultz * loop. 1968a092ff0fSjohn stultz * 1969a092ff0fSjohn stultz * Returns the unconsumed cycles. 1970a092ff0fSjohn stultz */ 1971a5a1d1c2SThomas Gleixner static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset, 1972a5a1d1c2SThomas Gleixner u32 shift, unsigned int *clock_set) 1973a092ff0fSjohn stultz { 1974a5a1d1c2SThomas Gleixner u64 interval = tk->cycle_interval << shift; 19753d88d56cSJohn Stultz u64 snsec_per_sec; 1976a092ff0fSjohn stultz 1977571af55aSZhen Lei /* If the offset is smaller than a shifted interval, do nothing */ 197823a9537aSThomas Gleixner if (offset < interval) 1979a092ff0fSjohn stultz return offset; 1980a092ff0fSjohn stultz 1981a092ff0fSjohn stultz /* Accumulate one shifted interval */ 198223a9537aSThomas Gleixner offset -= interval; 1983876e7881SPeter Zijlstra tk->tkr_mono.cycle_last += interval; 19844a4ad80dSPeter Zijlstra tk->tkr_raw.cycle_last += interval; 1985a092ff0fSjohn stultz 1986876e7881SPeter Zijlstra tk->tkr_mono.xtime_nsec += tk->xtime_interval << shift; 19875258d3f2SJohn Stultz *clock_set |= accumulate_nsecs_to_secs(tk); 1988a092ff0fSjohn stultz 1989deda2e81SJason Wessel /* Accumulate raw time */ 19903d88d56cSJohn Stultz tk->tkr_raw.xtime_nsec += tk->raw_interval << shift; 19913d88d56cSJohn Stultz snsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_raw.shift; 19923d88d56cSJohn Stultz while (tk->tkr_raw.xtime_nsec >= snsec_per_sec) { 19933d88d56cSJohn Stultz tk->tkr_raw.xtime_nsec -= snsec_per_sec; 1994fc6eead7SJohn Stultz tk->raw_sec++; 1995a092ff0fSjohn stultz } 1996a092ff0fSjohn stultz 1997a092ff0fSjohn stultz /* Accumulate error between NTP and clock interval */ 1998375f45b5SJohn Stultz tk->ntp_error += tk->ntp_tick << shift; 1999f726a697SJohn Stultz tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) << 2000f726a697SJohn Stultz (tk->ntp_error_shift + shift); 2001a092ff0fSjohn stultz 2002a092ff0fSjohn stultz return offset; 2003a092ff0fSjohn stultz } 2004a092ff0fSjohn stultz 20058524070bSjohn stultz /** 20068524070bSjohn stultz * update_wall_time - Uses the current clocksource to increment the wall time 20078524070bSjohn stultz * 20088524070bSjohn stultz */ 200947a1b796SJohn Stultz void update_wall_time(void) 20108524070bSjohn stultz { 20113fdb14fdSThomas Gleixner struct timekeeper *real_tk = &tk_core.timekeeper; 201248cdc135SThomas Gleixner struct timekeeper *tk = &shadow_timekeeper; 2013a5a1d1c2SThomas Gleixner u64 offset; 2014a092ff0fSjohn stultz int shift = 0, maxshift; 20155258d3f2SJohn Stultz unsigned int clock_set = 0; 201670471f2fSJohn Stultz unsigned long flags; 201770471f2fSJohn Stultz 20189a7a71b1SThomas Gleixner raw_spin_lock_irqsave(&timekeeper_lock, flags); 20198524070bSjohn stultz 20208524070bSjohn stultz /* Make sure we're fully resumed: */ 20218524070bSjohn stultz if (unlikely(timekeeping_suspended)) 202270471f2fSJohn Stultz goto out; 20238524070bSjohn stultz 2024592913ecSJohn Stultz #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET 202548cdc135SThomas Gleixner offset = real_tk->cycle_interval; 2026592913ecSJohn Stultz #else 2027ceea5e37SJohn Stultz offset = clocksource_delta(tk_clock_read(&tk->tkr_mono), 2028876e7881SPeter Zijlstra tk->tkr_mono.cycle_last, tk->tkr_mono.mask); 20298524070bSjohn stultz #endif 20308524070bSjohn stultz 2031bf2ac312SJohn Stultz /* Check if there's really nothing to do */ 203248cdc135SThomas Gleixner if (offset < real_tk->cycle_interval) 2033bf2ac312SJohn Stultz goto out; 2034bf2ac312SJohn Stultz 20353c17ad19SJohn Stultz /* Do some additional sanity checking */ 2036a529bea8SStafford Horne timekeeping_check_update(tk, offset); 20373c17ad19SJohn Stultz 2038a092ff0fSjohn stultz /* 2039a092ff0fSjohn stultz * With NO_HZ we may have to accumulate many cycle_intervals 2040a092ff0fSjohn stultz * (think "ticks") worth of time at once. To do this efficiently, 2041a092ff0fSjohn stultz * we calculate the largest doubling multiple of cycle_intervals 204288b28adfSJim Cromie * that is smaller than the offset. We then accumulate that 2043a092ff0fSjohn stultz * chunk in one go, and then try to consume the next smaller 2044a092ff0fSjohn stultz * doubled multiple. 20458524070bSjohn stultz */ 20464e250fddSJohn Stultz shift = ilog2(offset) - ilog2(tk->cycle_interval); 2047a092ff0fSjohn stultz shift = max(0, shift); 204888b28adfSJim Cromie /* Bound shift to one less than what overflows tick_length */ 2049ea7cf49aSJohn Stultz maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1; 2050a092ff0fSjohn stultz shift = min(shift, maxshift); 20514e250fddSJohn Stultz while (offset >= tk->cycle_interval) { 20525258d3f2SJohn Stultz offset = logarithmic_accumulation(tk, offset, shift, 20535258d3f2SJohn Stultz &clock_set); 20544e250fddSJohn Stultz if (offset < tk->cycle_interval<<shift) 2055a092ff0fSjohn stultz shift--; 20568524070bSjohn stultz } 20578524070bSjohn stultz 205878b98e3cSMiroslav Lichvar /* Adjust the multiplier to correct NTP error */ 20594e250fddSJohn Stultz timekeeping_adjust(tk, offset); 20608524070bSjohn stultz 20616a867a39SJohn Stultz /* 20626a867a39SJohn Stultz * Finally, make sure that after the rounding 20631e75fa8bSJohn Stultz * xtime_nsec isn't larger than NSEC_PER_SEC 20646a867a39SJohn Stultz */ 20655258d3f2SJohn Stultz clock_set |= accumulate_nsecs_to_secs(tk); 206683f57a11SLinus Torvalds 20673fdb14fdSThomas Gleixner write_seqcount_begin(&tk_core.seq); 206848cdc135SThomas Gleixner /* 206948cdc135SThomas Gleixner * Update the real timekeeper. 207048cdc135SThomas Gleixner * 207148cdc135SThomas Gleixner * We could avoid this memcpy by switching pointers, but that 207248cdc135SThomas Gleixner * requires changes to all other timekeeper usage sites as 207348cdc135SThomas Gleixner * well, i.e. move the timekeeper pointer getter into the 207448cdc135SThomas Gleixner * spinlocked/seqcount protected sections. And we trade this 20753fdb14fdSThomas Gleixner * memcpy under the tk_core.seq against one before we start 207648cdc135SThomas Gleixner * updating. 207748cdc135SThomas Gleixner */ 2078906c5557SJohn Stultz timekeeping_update(tk, clock_set); 207948cdc135SThomas Gleixner memcpy(real_tk, tk, sizeof(*tk)); 2080906c5557SJohn Stultz /* The memcpy must come last. Do not put anything here! */ 20813fdb14fdSThomas Gleixner write_seqcount_end(&tk_core.seq); 2082ca4523cdSThomas Gleixner out: 20839a7a71b1SThomas Gleixner raw_spin_unlock_irqrestore(&timekeeper_lock, flags); 208447a1b796SJohn Stultz if (clock_set) 2085cab5e127SJohn Stultz /* Have to call _delayed version, since in irq context*/ 2086cab5e127SJohn Stultz clock_was_set_delayed(); 20878524070bSjohn stultz } 20887c3f1a57STomas Janousek 20897c3f1a57STomas Janousek /** 2090d08c0cddSJohn Stultz * getboottime64 - Return the real time of system boot. 2091d08c0cddSJohn Stultz * @ts: pointer to the timespec64 to be set 20927c3f1a57STomas Janousek * 2093d08c0cddSJohn Stultz * Returns the wall-time of boot in a timespec64. 20947c3f1a57STomas Janousek * 20957c3f1a57STomas Janousek * This is based on the wall_to_monotonic offset and the total suspend 20967c3f1a57STomas Janousek * time. Calls to settimeofday will affect the value returned (which 20977c3f1a57STomas Janousek * basically means that however wrong your real time clock is at boot time, 20987c3f1a57STomas Janousek * you get the right time here). 20997c3f1a57STomas Janousek */ 2100d08c0cddSJohn Stultz void getboottime64(struct timespec64 *ts) 21017c3f1a57STomas Janousek { 21023fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 2103a3ed0e43SThomas Gleixner ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot); 2104d4f587c6SMartin Schwidefsky 2105d08c0cddSJohn Stultz *ts = ktime_to_timespec64(t); 21067c3f1a57STomas Janousek } 2107d08c0cddSJohn Stultz EXPORT_SYMBOL_GPL(getboottime64); 21087c3f1a57STomas Janousek 210917c38b74Sjohn stultz unsigned long get_seconds(void) 211017c38b74Sjohn stultz { 21113fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 21124e250fddSJohn Stultz 21134e250fddSJohn Stultz return tk->xtime_sec; 211417c38b74Sjohn stultz } 211517c38b74Sjohn stultz EXPORT_SYMBOL(get_seconds); 211617c38b74Sjohn stultz 2117*fb7fcc96SArnd Bergmann void ktime_get_coarse_real_ts64(struct timespec64 *ts) 21182c6b47deSjohn stultz { 21193fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 21202c6b47deSjohn stultz unsigned long seq; 21212c6b47deSjohn stultz 21222c6b47deSjohn stultz do { 21233fdb14fdSThomas Gleixner seq = read_seqcount_begin(&tk_core.seq); 212483f57a11SLinus Torvalds 2125*fb7fcc96SArnd Bergmann *ts = tk_xtime(tk); 21263fdb14fdSThomas Gleixner } while (read_seqcount_retry(&tk_core.seq, seq)); 21272c6b47deSjohn stultz } 2128*fb7fcc96SArnd Bergmann EXPORT_SYMBOL(ktime_get_coarse_real_ts64); 2129da15cfdaSjohn stultz 2130*fb7fcc96SArnd Bergmann void ktime_get_coarse_ts64(struct timespec64 *ts) 2131da15cfdaSjohn stultz { 21323fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 21337d489d15SJohn Stultz struct timespec64 now, mono; 2134da15cfdaSjohn stultz unsigned long seq; 2135da15cfdaSjohn stultz 2136da15cfdaSjohn stultz do { 21373fdb14fdSThomas Gleixner seq = read_seqcount_begin(&tk_core.seq); 213883f57a11SLinus Torvalds 21394e250fddSJohn Stultz now = tk_xtime(tk); 21404e250fddSJohn Stultz mono = tk->wall_to_monotonic; 21413fdb14fdSThomas Gleixner } while (read_seqcount_retry(&tk_core.seq, seq)); 2142da15cfdaSjohn stultz 2143*fb7fcc96SArnd Bergmann set_normalized_timespec64(ts, now.tv_sec + mono.tv_sec, 2144da15cfdaSjohn stultz now.tv_nsec + mono.tv_nsec); 2145da15cfdaSjohn stultz } 2146*fb7fcc96SArnd Bergmann EXPORT_SYMBOL(ktime_get_coarse_ts64); 2147871cf1e5STorben Hohn 2148871cf1e5STorben Hohn /* 2149d6ad4187SJohn Stultz * Must hold jiffies_lock 2150871cf1e5STorben Hohn */ 2151871cf1e5STorben Hohn void do_timer(unsigned long ticks) 2152871cf1e5STorben Hohn { 2153871cf1e5STorben Hohn jiffies_64 += ticks; 2154871cf1e5STorben Hohn calc_global_load(ticks); 2155871cf1e5STorben Hohn } 215648cf76f7STorben Hohn 215748cf76f7STorben Hohn /** 215876f41088SJohn Stultz * ktime_get_update_offsets_now - hrtimer helper 2159868a3e91SThomas Gleixner * @cwsseq: pointer to check and store the clock was set sequence number 2160f6c06abfSThomas Gleixner * @offs_real: pointer to storage for monotonic -> realtime offset 2161a3ed0e43SThomas Gleixner * @offs_boot: pointer to storage for monotonic -> boottime offset 2162b7bc50e4SXie XiuQi * @offs_tai: pointer to storage for monotonic -> clock tai offset 2163f6c06abfSThomas Gleixner * 2164868a3e91SThomas Gleixner * Returns current monotonic time and updates the offsets if the 2165868a3e91SThomas Gleixner * sequence number in @cwsseq and timekeeper.clock_was_set_seq are 2166868a3e91SThomas Gleixner * different. 2167868a3e91SThomas Gleixner * 2168b7bc50e4SXie XiuQi * Called from hrtimer_interrupt() or retrigger_next_event() 2169f6c06abfSThomas Gleixner */ 2170868a3e91SThomas Gleixner ktime_t ktime_get_update_offsets_now(unsigned int *cwsseq, ktime_t *offs_real, 2171a3ed0e43SThomas Gleixner ktime_t *offs_boot, ktime_t *offs_tai) 2172f6c06abfSThomas Gleixner { 21733fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 2174f6c06abfSThomas Gleixner unsigned int seq; 2175a37c0aadSThomas Gleixner ktime_t base; 2176a37c0aadSThomas Gleixner u64 nsecs; 2177f6c06abfSThomas Gleixner 2178f6c06abfSThomas Gleixner do { 21793fdb14fdSThomas Gleixner seq = read_seqcount_begin(&tk_core.seq); 2180f6c06abfSThomas Gleixner 2181876e7881SPeter Zijlstra base = tk->tkr_mono.base; 2182876e7881SPeter Zijlstra nsecs = timekeeping_get_ns(&tk->tkr_mono); 2183833f32d7SJohn Stultz base = ktime_add_ns(base, nsecs); 2184833f32d7SJohn Stultz 2185868a3e91SThomas Gleixner if (*cwsseq != tk->clock_was_set_seq) { 2186868a3e91SThomas Gleixner *cwsseq = tk->clock_was_set_seq; 21874e250fddSJohn Stultz *offs_real = tk->offs_real; 2188a3ed0e43SThomas Gleixner *offs_boot = tk->offs_boot; 218990adda98SJohn Stultz *offs_tai = tk->offs_tai; 2190868a3e91SThomas Gleixner } 2191833f32d7SJohn Stultz 2192833f32d7SJohn Stultz /* Handle leapsecond insertion adjustments */ 21932456e855SThomas Gleixner if (unlikely(base >= tk->next_leap_ktime)) 2194833f32d7SJohn Stultz *offs_real = ktime_sub(tk->offs_real, ktime_set(1, 0)); 2195833f32d7SJohn Stultz 21963fdb14fdSThomas Gleixner } while (read_seqcount_retry(&tk_core.seq, seq)); 2197f6c06abfSThomas Gleixner 2198833f32d7SJohn Stultz return base; 2199f6c06abfSThomas Gleixner } 2200f6c06abfSThomas Gleixner 2201f0af911aSTorben Hohn /** 22021572fa03SArnd Bergmann * timekeeping_validate_timex - Ensures the timex is ok for use in do_adjtimex 2203e0956dccSArnd Bergmann */ 22041572fa03SArnd Bergmann static int timekeeping_validate_timex(struct timex *txc) 2205e0956dccSArnd Bergmann { 2206e0956dccSArnd Bergmann if (txc->modes & ADJ_ADJTIME) { 2207e0956dccSArnd Bergmann /* singleshot must not be used with any other mode bits */ 2208e0956dccSArnd Bergmann if (!(txc->modes & ADJ_OFFSET_SINGLESHOT)) 2209e0956dccSArnd Bergmann return -EINVAL; 2210e0956dccSArnd Bergmann if (!(txc->modes & ADJ_OFFSET_READONLY) && 2211e0956dccSArnd Bergmann !capable(CAP_SYS_TIME)) 2212e0956dccSArnd Bergmann return -EPERM; 2213e0956dccSArnd Bergmann } else { 2214e0956dccSArnd Bergmann /* In order to modify anything, you gotta be super-user! */ 2215e0956dccSArnd Bergmann if (txc->modes && !capable(CAP_SYS_TIME)) 2216e0956dccSArnd Bergmann return -EPERM; 2217e0956dccSArnd Bergmann /* 2218e0956dccSArnd Bergmann * if the quartz is off by more than 10% then 2219e0956dccSArnd Bergmann * something is VERY wrong! 2220e0956dccSArnd Bergmann */ 2221e0956dccSArnd Bergmann if (txc->modes & ADJ_TICK && 2222e0956dccSArnd Bergmann (txc->tick < 900000/USER_HZ || 2223e0956dccSArnd Bergmann txc->tick > 1100000/USER_HZ)) 2224e0956dccSArnd Bergmann return -EINVAL; 2225e0956dccSArnd Bergmann } 2226e0956dccSArnd Bergmann 2227e0956dccSArnd Bergmann if (txc->modes & ADJ_SETOFFSET) { 2228e0956dccSArnd Bergmann /* In order to inject time, you gotta be super-user! */ 2229e0956dccSArnd Bergmann if (!capable(CAP_SYS_TIME)) 2230e0956dccSArnd Bergmann return -EPERM; 2231e0956dccSArnd Bergmann 22321572fa03SArnd Bergmann /* 22331572fa03SArnd Bergmann * Validate if a timespec/timeval used to inject a time 22341572fa03SArnd Bergmann * offset is valid. Offsets can be postive or negative, so 22351572fa03SArnd Bergmann * we don't check tv_sec. The value of the timeval/timespec 22361572fa03SArnd Bergmann * is the sum of its fields,but *NOTE*: 22371572fa03SArnd Bergmann * The field tv_usec/tv_nsec must always be non-negative and 22381572fa03SArnd Bergmann * we can't have more nanoseconds/microseconds than a second. 22391572fa03SArnd Bergmann */ 22401572fa03SArnd Bergmann if (txc->time.tv_usec < 0) 2241e0956dccSArnd Bergmann return -EINVAL; 2242e0956dccSArnd Bergmann 22431572fa03SArnd Bergmann if (txc->modes & ADJ_NANO) { 22441572fa03SArnd Bergmann if (txc->time.tv_usec >= NSEC_PER_SEC) 22451572fa03SArnd Bergmann return -EINVAL; 2246e0956dccSArnd Bergmann } else { 22471572fa03SArnd Bergmann if (txc->time.tv_usec >= USEC_PER_SEC) 2248e0956dccSArnd Bergmann return -EINVAL; 2249e0956dccSArnd Bergmann } 2250e0956dccSArnd Bergmann } 2251e0956dccSArnd Bergmann 2252e0956dccSArnd Bergmann /* 2253e0956dccSArnd Bergmann * Check for potential multiplication overflows that can 2254e0956dccSArnd Bergmann * only happen on 64-bit systems: 2255e0956dccSArnd Bergmann */ 2256e0956dccSArnd Bergmann if ((txc->modes & ADJ_FREQUENCY) && (BITS_PER_LONG == 64)) { 2257e0956dccSArnd Bergmann if (LLONG_MIN / PPM_SCALE > txc->freq) 2258e0956dccSArnd Bergmann return -EINVAL; 2259e0956dccSArnd Bergmann if (LLONG_MAX / PPM_SCALE < txc->freq) 2260e0956dccSArnd Bergmann return -EINVAL; 2261e0956dccSArnd Bergmann } 2262e0956dccSArnd Bergmann 2263e0956dccSArnd Bergmann return 0; 2264e0956dccSArnd Bergmann } 2265e0956dccSArnd Bergmann 2266e0956dccSArnd Bergmann 2267e0956dccSArnd Bergmann /** 2268aa6f9c59SJohn Stultz * do_adjtimex() - Accessor function to NTP __do_adjtimex function 2269aa6f9c59SJohn Stultz */ 2270aa6f9c59SJohn Stultz int do_adjtimex(struct timex *txc) 2271aa6f9c59SJohn Stultz { 22723fdb14fdSThomas Gleixner struct timekeeper *tk = &tk_core.timekeeper; 227306c017fdSJohn Stultz unsigned long flags; 22747d489d15SJohn Stultz struct timespec64 ts; 22754e8f8b34SJohn Stultz s32 orig_tai, tai; 2276e4085693SJohn Stultz int ret; 2277e4085693SJohn Stultz 2278e4085693SJohn Stultz /* Validate the data before disabling interrupts */ 22791572fa03SArnd Bergmann ret = timekeeping_validate_timex(txc); 2280e4085693SJohn Stultz if (ret) 2281e4085693SJohn Stultz return ret; 2282e4085693SJohn Stultz 2283cef90377SJohn Stultz if (txc->modes & ADJ_SETOFFSET) { 22841572fa03SArnd Bergmann struct timespec64 delta; 2285cef90377SJohn Stultz delta.tv_sec = txc->time.tv_sec; 2286cef90377SJohn Stultz delta.tv_nsec = txc->time.tv_usec; 2287cef90377SJohn Stultz if (!(txc->modes & ADJ_NANO)) 2288cef90377SJohn Stultz delta.tv_nsec *= 1000; 2289cef90377SJohn Stultz ret = timekeeping_inject_offset(&delta); 2290cef90377SJohn Stultz if (ret) 2291cef90377SJohn Stultz return ret; 2292cef90377SJohn Stultz } 2293cef90377SJohn Stultz 2294d6d29896SThomas Gleixner getnstimeofday64(&ts); 2295aa6f9c59SJohn Stultz 229606c017fdSJohn Stultz raw_spin_lock_irqsave(&timekeeper_lock, flags); 22973fdb14fdSThomas Gleixner write_seqcount_begin(&tk_core.seq); 229806c017fdSJohn Stultz 22994e8f8b34SJohn Stultz orig_tai = tai = tk->tai_offset; 230087ace39bSJohn Stultz ret = __do_adjtimex(txc, &ts, &tai); 230187ace39bSJohn Stultz 23024e8f8b34SJohn Stultz if (tai != orig_tai) { 23030b5154fbSJohn Stultz __timekeeping_set_tai_offset(tk, tai); 2304f55c0760SJohn Stultz timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); 23054e8f8b34SJohn Stultz } 2306833f32d7SJohn Stultz tk_update_leap_state(tk); 2307833f32d7SJohn Stultz 23083fdb14fdSThomas Gleixner write_seqcount_end(&tk_core.seq); 230906c017fdSJohn Stultz raw_spin_unlock_irqrestore(&timekeeper_lock, flags); 231006c017fdSJohn Stultz 23116fdda9a9SJohn Stultz if (tai != orig_tai) 23126fdda9a9SJohn Stultz clock_was_set(); 23136fdda9a9SJohn Stultz 23147bd36014SJohn Stultz ntp_notify_cmos_timer(); 23157bd36014SJohn Stultz 231687ace39bSJohn Stultz return ret; 231787ace39bSJohn Stultz } 2318aa6f9c59SJohn Stultz 2319aa6f9c59SJohn Stultz #ifdef CONFIG_NTP_PPS 2320aa6f9c59SJohn Stultz /** 2321aa6f9c59SJohn Stultz * hardpps() - Accessor function to NTP __hardpps function 2322aa6f9c59SJohn Stultz */ 23237ec88e4bSArnd Bergmann void hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts) 2324aa6f9c59SJohn Stultz { 232506c017fdSJohn Stultz unsigned long flags; 232606c017fdSJohn Stultz 232706c017fdSJohn Stultz raw_spin_lock_irqsave(&timekeeper_lock, flags); 23283fdb14fdSThomas Gleixner write_seqcount_begin(&tk_core.seq); 232906c017fdSJohn Stultz 2330aa6f9c59SJohn Stultz __hardpps(phase_ts, raw_ts); 233106c017fdSJohn Stultz 23323fdb14fdSThomas Gleixner write_seqcount_end(&tk_core.seq); 233306c017fdSJohn Stultz raw_spin_unlock_irqrestore(&timekeeper_lock, flags); 2334aa6f9c59SJohn Stultz } 2335aa6f9c59SJohn Stultz EXPORT_SYMBOL(hardpps); 2336a2d81803SRobert P. J. Day #endif /* CONFIG_NTP_PPS */ 2337aa6f9c59SJohn Stultz 2338aa6f9c59SJohn Stultz /** 2339f0af911aSTorben Hohn * xtime_update() - advances the timekeeping infrastructure 2340f0af911aSTorben Hohn * @ticks: number of ticks, that have elapsed since the last call. 2341f0af911aSTorben Hohn * 2342f0af911aSTorben Hohn * Must be called with interrupts disabled. 2343f0af911aSTorben Hohn */ 2344f0af911aSTorben Hohn void xtime_update(unsigned long ticks) 2345f0af911aSTorben Hohn { 2346d6ad4187SJohn Stultz write_seqlock(&jiffies_lock); 2347f0af911aSTorben Hohn do_timer(ticks); 2348d6ad4187SJohn Stultz write_sequnlock(&jiffies_lock); 234947a1b796SJohn Stultz update_wall_time(); 2350f0af911aSTorben Hohn } 2351