2 * linux/kernel/time/timekeeping.c
4 * Kernel timekeeping code and accessor functions
6 * This code was moved from linux/kernel/timer.c.
7 * Please see that file for copyright and history logs.
11 #include <linux/timekeeper_internal.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/percpu.h>
15 #include <linux/init.h>
17 #include <linux/sched.h>
18 #include <linux/syscore_ops.h>
19 #include <linux/clocksource.h>
20 #include <linux/jiffies.h>
21 #include <linux/time.h>
22 #include <linux/tick.h>
23 #include <linux/stop_machine.h>
24 #include <linux/pvclock_gtod.h>
25 #include <linux/compiler.h>
27 #include "tick-internal.h"
28 #include "ntp_internal.h"
29 #include "timekeeping_internal.h"
31 #define TK_CLEAR_NTP (1 << 0)
32 #define TK_MIRROR (1 << 1)
33 #define TK_CLOCK_WAS_SET (1 << 2)
36 * The most important data for readout fits into a single 64 byte
41 struct timekeeper timekeeper;
42 } tk_core ____cacheline_aligned;
44 static DEFINE_RAW_SPINLOCK(timekeeper_lock);
45 static struct timekeeper shadow_timekeeper;
48 * struct tk_fast - NMI safe timekeeper
49 * @seq: Sequence counter for protecting updates. The lowest bit
50 * is the index for the tk_read_base array
51 * @base: tk_read_base array. Access is indexed by the lowest bit of
54 * See @update_fast_timekeeper() below.
58 struct tk_read_base base[2];
61 static struct tk_fast tk_fast_mono ____cacheline_aligned;
63 /* flag for if timekeeping is suspended */
64 int __read_mostly timekeeping_suspended;
66 /* Flag for if there is a persistent clock on this platform */
67 bool __read_mostly persistent_clock_exist = false;
69 static inline void tk_normalize_xtime(struct timekeeper *tk)
71 while (tk->tkr.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr.shift)) {
72 tk->tkr.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr.shift;
77 static inline struct timespec64 tk_xtime(struct timekeeper *tk)
81 ts.tv_sec = tk->xtime_sec;
82 ts.tv_nsec = (long)(tk->tkr.xtime_nsec >> tk->tkr.shift);
86 static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
88 tk->xtime_sec = ts->tv_sec;
89 tk->tkr.xtime_nsec = (u64)ts->tv_nsec << tk->tkr.shift;
92 static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
94 tk->xtime_sec += ts->tv_sec;
95 tk->tkr.xtime_nsec += (u64)ts->tv_nsec << tk->tkr.shift;
96 tk_normalize_xtime(tk);
99 static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
101 struct timespec64 tmp;
104 * Verify consistency of: offset_real = -wall_to_monotonic
105 * before modifying anything
107 set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec,
108 -tk->wall_to_monotonic.tv_nsec);
109 WARN_ON_ONCE(tk->offs_real.tv64 != timespec64_to_ktime(tmp).tv64);
110 tk->wall_to_monotonic = wtm;
111 set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
112 tk->offs_real = timespec64_to_ktime(tmp);
113 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
116 static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
118 tk->offs_boot = ktime_add(tk->offs_boot, delta);
122 * tk_setup_internals - Set up internals to use clocksource clock.
124 * @tk: The target timekeeper to setup.
125 * @clock: Pointer to clocksource.
127 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
128 * pair and interval request.
130 * Unless you're the timekeeping code, you should not be using this!
132 static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
135 u64 tmp, ntpinterval;
136 struct clocksource *old_clock;
138 old_clock = tk->tkr.clock;
139 tk->tkr.clock = clock;
140 tk->tkr.read = clock->read;
141 tk->tkr.mask = clock->mask;
142 tk->tkr.cycle_last = tk->tkr.read(clock);
144 /* Do the ns -> cycle conversion first, using original mult */
145 tmp = NTP_INTERVAL_LENGTH;
146 tmp <<= clock->shift;
148 tmp += clock->mult/2;
149 do_div(tmp, clock->mult);
153 interval = (cycle_t) tmp;
154 tk->cycle_interval = interval;
156 /* Go back from cycles -> shifted ns */
157 tk->xtime_interval = (u64) interval * clock->mult;
158 tk->xtime_remainder = ntpinterval - tk->xtime_interval;
160 ((u64) interval * clock->mult) >> clock->shift;
162 /* if changing clocks, convert xtime_nsec shift units */
164 int shift_change = clock->shift - old_clock->shift;
165 if (shift_change < 0)
166 tk->tkr.xtime_nsec >>= -shift_change;
168 tk->tkr.xtime_nsec <<= shift_change;
170 tk->tkr.shift = clock->shift;
173 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
176 * The timekeeper keeps its own mult values for the currently
177 * active clocksource. These value will be adjusted via NTP
178 * to counteract clock drifting.
180 tk->tkr.mult = clock->mult;
181 tk->ntp_err_mult = 0;
184 /* Timekeeper helper functions. */
186 #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
187 static u32 default_arch_gettimeoffset(void) { return 0; }
188 u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset;
190 static inline u32 arch_gettimeoffset(void) { return 0; }
193 static inline s64 timekeeping_get_ns(struct tk_read_base *tkr)
195 cycle_t cycle_now, delta;
198 /* read clocksource: */
199 cycle_now = tkr->read(tkr->clock);
201 /* calculate the delta since the last update_wall_time: */
202 delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
204 nsec = delta * tkr->mult + tkr->xtime_nsec;
207 /* If arch requires, add in get_arch_timeoffset() */
208 return nsec + arch_gettimeoffset();
211 static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
213 struct clocksource *clock = tk->tkr.clock;
214 cycle_t cycle_now, delta;
217 /* read clocksource: */
218 cycle_now = tk->tkr.read(clock);
220 /* calculate the delta since the last update_wall_time: */
221 delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask);
223 /* convert delta to nanoseconds. */
224 nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift);
226 /* If arch requires, add in get_arch_timeoffset() */
227 return nsec + arch_gettimeoffset();
231 * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
232 * @tk: The timekeeper from which we take the update
233 * @tkf: The fast timekeeper to update
234 * @tbase: The time base for the fast timekeeper (mono/raw)
236 * We want to use this from any context including NMI and tracing /
237 * instrumenting the timekeeping code itself.
239 * So we handle this differently than the other timekeeping accessor
240 * functions which retry when the sequence count has changed. The
243 * smp_wmb(); <- Ensure that the last base[1] update is visible
245 * smp_wmb(); <- Ensure that the seqcount update is visible
246 * update(tkf->base[0], tk);
247 * smp_wmb(); <- Ensure that the base[0] update is visible
249 * smp_wmb(); <- Ensure that the seqcount update is visible
250 * update(tkf->base[1], tk);
252 * The reader side does:
258 * now = now(tkf->base[idx]);
260 * } while (seq != tkf->seq)
262 * As long as we update base[0] readers are forced off to
263 * base[1]. Once base[0] is updated readers are redirected to base[0]
264 * and the base[1] update takes place.
266 * So if a NMI hits the update of base[0] then it will use base[1]
267 * which is still consistent. In the worst case this can result is a
268 * slightly wrong timestamp (a few nanoseconds). See
269 * @ktime_get_mono_fast_ns.
271 static void update_fast_timekeeper(struct timekeeper *tk)
273 struct tk_read_base *base = tk_fast_mono.base;
275 /* Force readers off to base[1] */
276 raw_write_seqcount_latch(&tk_fast_mono.seq);
279 memcpy(base, &tk->tkr, sizeof(*base));
281 /* Force readers back to base[0] */
282 raw_write_seqcount_latch(&tk_fast_mono.seq);
285 memcpy(base + 1, base, sizeof(*base));
289 * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic
291 * This timestamp is not guaranteed to be monotonic across an update.
292 * The timestamp is calculated by:
294 * now = base_mono + clock_delta * slope
296 * So if the update lowers the slope, readers who are forced to the
297 * not yet updated second array are still using the old steeper slope.
306 * |12345678---> reader order
312 * So reader 6 will observe time going backwards versus reader 5.
314 * While other CPUs are likely to be able observe that, the only way
315 * for a CPU local observation is when an NMI hits in the middle of
316 * the update. Timestamps taken from that NMI context might be ahead
317 * of the following timestamps. Callers need to be aware of that and
320 u64 notrace ktime_get_mono_fast_ns(void)
322 struct tk_read_base *tkr;
327 seq = raw_read_seqcount(&tk_fast_mono.seq);
328 tkr = tk_fast_mono.base + (seq & 0x01);
329 now = ktime_to_ns(tkr->base_mono) + timekeeping_get_ns(tkr);
331 } while (read_seqcount_retry(&tk_fast_mono.seq, seq));
334 EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
336 #ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
338 static inline void update_vsyscall(struct timekeeper *tk)
342 xt = timespec64_to_timespec(tk_xtime(tk));
343 update_vsyscall_old(&xt, &tk->wall_to_monotonic, tk->tkr.clock, tk->tkr.mult,
347 static inline void old_vsyscall_fixup(struct timekeeper *tk)
352 * Store only full nanoseconds into xtime_nsec after rounding
353 * it up and add the remainder to the error difference.
354 * XXX - This is necessary to avoid small 1ns inconsistnecies caused
355 * by truncating the remainder in vsyscalls. However, it causes
356 * additional work to be done in timekeeping_adjust(). Once
357 * the vsyscall implementations are converted to use xtime_nsec
358 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
359 * users are removed, this can be killed.
361 remainder = tk->tkr.xtime_nsec & ((1ULL << tk->tkr.shift) - 1);
362 tk->tkr.xtime_nsec -= remainder;
363 tk->tkr.xtime_nsec += 1ULL << tk->tkr.shift;
364 tk->ntp_error += remainder << tk->ntp_error_shift;
365 tk->ntp_error -= (1ULL << tk->tkr.shift) << tk->ntp_error_shift;
368 #define old_vsyscall_fixup(tk)
371 static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
373 static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
375 raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
379 * pvclock_gtod_register_notifier - register a pvclock timedata update listener
381 int pvclock_gtod_register_notifier(struct notifier_block *nb)
383 struct timekeeper *tk = &tk_core.timekeeper;
387 raw_spin_lock_irqsave(&timekeeper_lock, flags);
388 ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
389 update_pvclock_gtod(tk, true);
390 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
394 EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
397 * pvclock_gtod_unregister_notifier - unregister a pvclock
398 * timedata update listener
400 int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
405 raw_spin_lock_irqsave(&timekeeper_lock, flags);
406 ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
407 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
411 EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
414 * Update the ktime_t based scalar nsec members of the timekeeper
416 static inline void tk_update_ktime_data(struct timekeeper *tk)
421 * The xtime based monotonic readout is:
422 * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();
423 * The ktime based monotonic readout is:
424 * nsec = base_mono + now();
425 * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
427 nsec = (s64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
428 nsec *= NSEC_PER_SEC;
429 nsec += tk->wall_to_monotonic.tv_nsec;
430 tk->tkr.base_mono = ns_to_ktime(nsec);
432 /* Update the monotonic raw base */
433 tk->base_raw = timespec64_to_ktime(tk->raw_time);
436 /* must hold timekeeper_lock */
437 static void timekeeping_update(struct timekeeper *tk, unsigned int action)
439 if (action & TK_CLEAR_NTP) {
444 update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
446 tk_update_ktime_data(tk);
448 if (action & TK_MIRROR)
449 memcpy(&shadow_timekeeper, &tk_core.timekeeper,
450 sizeof(tk_core.timekeeper));
452 update_fast_timekeeper(tk);
456 * timekeeping_forward_now - update clock to the current time
458 * Forward the current clock to update its state since the last call to
459 * update_wall_time(). This is useful before significant clock changes,
460 * as it avoids having to deal with this time offset explicitly.
462 static void timekeeping_forward_now(struct timekeeper *tk)
464 struct clocksource *clock = tk->tkr.clock;
465 cycle_t cycle_now, delta;
468 cycle_now = tk->tkr.read(clock);
469 delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask);
470 tk->tkr.cycle_last = cycle_now;
472 tk->tkr.xtime_nsec += delta * tk->tkr.mult;
474 /* If arch requires, add in get_arch_timeoffset() */
475 tk->tkr.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr.shift;
477 tk_normalize_xtime(tk);
479 nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift);
480 timespec64_add_ns(&tk->raw_time, nsec);
484 * __getnstimeofday64 - Returns the time of day in a timespec64.
485 * @ts: pointer to the timespec to be set
487 * Updates the time of day in the timespec.
488 * Returns 0 on success, or -ve when suspended (timespec will be undefined).
490 int __getnstimeofday64(struct timespec64 *ts)
492 struct timekeeper *tk = &tk_core.timekeeper;
497 seq = read_seqcount_begin(&tk_core.seq);
499 ts->tv_sec = tk->xtime_sec;
500 nsecs = timekeeping_get_ns(&tk->tkr);
502 } while (read_seqcount_retry(&tk_core.seq, seq));
505 timespec64_add_ns(ts, nsecs);
508 * Do not bail out early, in case there were callers still using
509 * the value, even in the face of the WARN_ON.
511 if (unlikely(timekeeping_suspended))
515 EXPORT_SYMBOL(__getnstimeofday64);
518 * getnstimeofday64 - Returns the time of day in a timespec64.
519 * @ts: pointer to the timespec to be set
521 * Returns the time of day in a timespec (WARN if suspended).
523 void getnstimeofday64(struct timespec64 *ts)
525 WARN_ON(__getnstimeofday64(ts));
527 EXPORT_SYMBOL(getnstimeofday64);
529 ktime_t ktime_get(void)
531 struct timekeeper *tk = &tk_core.timekeeper;
536 WARN_ON(timekeeping_suspended);
539 seq = read_seqcount_begin(&tk_core.seq);
540 base = tk->tkr.base_mono;
541 nsecs = timekeeping_get_ns(&tk->tkr);
543 } while (read_seqcount_retry(&tk_core.seq, seq));
545 return ktime_add_ns(base, nsecs);
547 EXPORT_SYMBOL_GPL(ktime_get);
549 static ktime_t *offsets[TK_OFFS_MAX] = {
550 [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real,
551 [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot,
552 [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai,
555 ktime_t ktime_get_with_offset(enum tk_offsets offs)
557 struct timekeeper *tk = &tk_core.timekeeper;
559 ktime_t base, *offset = offsets[offs];
562 WARN_ON(timekeeping_suspended);
565 seq = read_seqcount_begin(&tk_core.seq);
566 base = ktime_add(tk->tkr.base_mono, *offset);
567 nsecs = timekeeping_get_ns(&tk->tkr);
569 } while (read_seqcount_retry(&tk_core.seq, seq));
571 return ktime_add_ns(base, nsecs);
574 EXPORT_SYMBOL_GPL(ktime_get_with_offset);
577 * ktime_mono_to_any() - convert mononotic time to any other time
578 * @tmono: time to convert.
579 * @offs: which offset to use
581 ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
583 ktime_t *offset = offsets[offs];
588 seq = read_seqcount_begin(&tk_core.seq);
589 tconv = ktime_add(tmono, *offset);
590 } while (read_seqcount_retry(&tk_core.seq, seq));
594 EXPORT_SYMBOL_GPL(ktime_mono_to_any);
597 * ktime_get_raw - Returns the raw monotonic time in ktime_t format
599 ktime_t ktime_get_raw(void)
601 struct timekeeper *tk = &tk_core.timekeeper;
607 seq = read_seqcount_begin(&tk_core.seq);
609 nsecs = timekeeping_get_ns_raw(tk);
611 } while (read_seqcount_retry(&tk_core.seq, seq));
613 return ktime_add_ns(base, nsecs);
615 EXPORT_SYMBOL_GPL(ktime_get_raw);
618 * ktime_get_ts64 - get the monotonic clock in timespec64 format
619 * @ts: pointer to timespec variable
621 * The function calculates the monotonic clock from the realtime
622 * clock and the wall_to_monotonic offset and stores the result
623 * in normalized timespec format in the variable pointed to by @ts.
625 void ktime_get_ts64(struct timespec64 *ts)
627 struct timekeeper *tk = &tk_core.timekeeper;
628 struct timespec64 tomono;
632 WARN_ON(timekeeping_suspended);
635 seq = read_seqcount_begin(&tk_core.seq);
636 ts->tv_sec = tk->xtime_sec;
637 nsec = timekeeping_get_ns(&tk->tkr);
638 tomono = tk->wall_to_monotonic;
640 } while (read_seqcount_retry(&tk_core.seq, seq));
642 ts->tv_sec += tomono.tv_sec;
644 timespec64_add_ns(ts, nsec + tomono.tv_nsec);
646 EXPORT_SYMBOL_GPL(ktime_get_ts64);
648 #ifdef CONFIG_NTP_PPS
651 * getnstime_raw_and_real - get day and raw monotonic time in timespec format
652 * @ts_raw: pointer to the timespec to be set to raw monotonic time
653 * @ts_real: pointer to the timespec to be set to the time of day
655 * This function reads both the time of day and raw monotonic time at the
656 * same time atomically and stores the resulting timestamps in timespec
659 void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
661 struct timekeeper *tk = &tk_core.timekeeper;
663 s64 nsecs_raw, nsecs_real;
665 WARN_ON_ONCE(timekeeping_suspended);
668 seq = read_seqcount_begin(&tk_core.seq);
670 *ts_raw = timespec64_to_timespec(tk->raw_time);
671 ts_real->tv_sec = tk->xtime_sec;
672 ts_real->tv_nsec = 0;
674 nsecs_raw = timekeeping_get_ns_raw(tk);
675 nsecs_real = timekeeping_get_ns(&tk->tkr);
677 } while (read_seqcount_retry(&tk_core.seq, seq));
679 timespec_add_ns(ts_raw, nsecs_raw);
680 timespec_add_ns(ts_real, nsecs_real);
682 EXPORT_SYMBOL(getnstime_raw_and_real);
684 #endif /* CONFIG_NTP_PPS */
687 * do_gettimeofday - Returns the time of day in a timeval
688 * @tv: pointer to the timeval to be set
690 * NOTE: Users should be converted to using getnstimeofday()
692 void do_gettimeofday(struct timeval *tv)
694 struct timespec64 now;
696 getnstimeofday64(&now);
697 tv->tv_sec = now.tv_sec;
698 tv->tv_usec = now.tv_nsec/1000;
700 EXPORT_SYMBOL(do_gettimeofday);
703 * do_settimeofday - Sets the time of day
704 * @tv: pointer to the timespec variable containing the new time
706 * Sets the time of day to the new time and update NTP and notify hrtimers
708 int do_settimeofday(const struct timespec *tv)
710 struct timekeeper *tk = &tk_core.timekeeper;
711 struct timespec64 ts_delta, xt, tmp;
714 if (!timespec_valid_strict(tv))
717 raw_spin_lock_irqsave(&timekeeper_lock, flags);
718 write_seqcount_begin(&tk_core.seq);
720 timekeeping_forward_now(tk);
723 ts_delta.tv_sec = tv->tv_sec - xt.tv_sec;
724 ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec;
726 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
728 tmp = timespec_to_timespec64(*tv);
729 tk_set_xtime(tk, &tmp);
731 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
733 write_seqcount_end(&tk_core.seq);
734 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
736 /* signal hrtimers about time change */
741 EXPORT_SYMBOL(do_settimeofday);
744 * timekeeping_inject_offset - Adds or subtracts from the current time.
745 * @tv: pointer to the timespec variable containing the offset
747 * Adds or subtracts an offset value from the current time.
749 int timekeeping_inject_offset(struct timespec *ts)
751 struct timekeeper *tk = &tk_core.timekeeper;
753 struct timespec64 ts64, tmp;
756 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
759 ts64 = timespec_to_timespec64(*ts);
761 raw_spin_lock_irqsave(&timekeeper_lock, flags);
762 write_seqcount_begin(&tk_core.seq);
764 timekeeping_forward_now(tk);
766 /* Make sure the proposed value is valid */
767 tmp = timespec64_add(tk_xtime(tk), ts64);
768 if (!timespec64_valid_strict(&tmp)) {
773 tk_xtime_add(tk, &ts64);
774 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts64));
776 error: /* even if we error out, we forwarded the time, so call update */
777 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
779 write_seqcount_end(&tk_core.seq);
780 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
782 /* signal hrtimers about time change */
787 EXPORT_SYMBOL(timekeeping_inject_offset);
791 * timekeeping_get_tai_offset - Returns current TAI offset from UTC
794 s32 timekeeping_get_tai_offset(void)
796 struct timekeeper *tk = &tk_core.timekeeper;
801 seq = read_seqcount_begin(&tk_core.seq);
802 ret = tk->tai_offset;
803 } while (read_seqcount_retry(&tk_core.seq, seq));
809 * __timekeeping_set_tai_offset - Lock free worker function
812 static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
814 tk->tai_offset = tai_offset;
815 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
819 * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
822 void timekeeping_set_tai_offset(s32 tai_offset)
824 struct timekeeper *tk = &tk_core.timekeeper;
827 raw_spin_lock_irqsave(&timekeeper_lock, flags);
828 write_seqcount_begin(&tk_core.seq);
829 __timekeeping_set_tai_offset(tk, tai_offset);
830 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
831 write_seqcount_end(&tk_core.seq);
832 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
837 * change_clocksource - Swaps clocksources if a new one is available
839 * Accumulates current time interval and initializes new clocksource
841 static int change_clocksource(void *data)
843 struct timekeeper *tk = &tk_core.timekeeper;
844 struct clocksource *new, *old;
847 new = (struct clocksource *) data;
849 raw_spin_lock_irqsave(&timekeeper_lock, flags);
850 write_seqcount_begin(&tk_core.seq);
852 timekeeping_forward_now(tk);
854 * If the cs is in module, get a module reference. Succeeds
855 * for built-in code (owner == NULL) as well.
857 if (try_module_get(new->owner)) {
858 if (!new->enable || new->enable(new) == 0) {
860 tk_setup_internals(tk, new);
863 module_put(old->owner);
865 module_put(new->owner);
868 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
870 write_seqcount_end(&tk_core.seq);
871 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
877 * timekeeping_notify - Install a new clock source
878 * @clock: pointer to the clock source
880 * This function is called from clocksource.c after a new, better clock
881 * source has been registered. The caller holds the clocksource_mutex.
883 int timekeeping_notify(struct clocksource *clock)
885 struct timekeeper *tk = &tk_core.timekeeper;
887 if (tk->tkr.clock == clock)
889 stop_machine(change_clocksource, clock, NULL);
891 return tk->tkr.clock == clock ? 0 : -1;
895 * getrawmonotonic - Returns the raw monotonic time in a timespec
896 * @ts: pointer to the timespec to be set
898 * Returns the raw monotonic time (completely un-modified by ntp)
900 void getrawmonotonic(struct timespec *ts)
902 struct timekeeper *tk = &tk_core.timekeeper;
903 struct timespec64 ts64;
908 seq = read_seqcount_begin(&tk_core.seq);
909 nsecs = timekeeping_get_ns_raw(tk);
912 } while (read_seqcount_retry(&tk_core.seq, seq));
914 timespec64_add_ns(&ts64, nsecs);
915 *ts = timespec64_to_timespec(ts64);
917 EXPORT_SYMBOL(getrawmonotonic);
920 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
922 int timekeeping_valid_for_hres(void)
924 struct timekeeper *tk = &tk_core.timekeeper;
929 seq = read_seqcount_begin(&tk_core.seq);
931 ret = tk->tkr.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
933 } while (read_seqcount_retry(&tk_core.seq, seq));
939 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
941 u64 timekeeping_max_deferment(void)
943 struct timekeeper *tk = &tk_core.timekeeper;
948 seq = read_seqcount_begin(&tk_core.seq);
950 ret = tk->tkr.clock->max_idle_ns;
952 } while (read_seqcount_retry(&tk_core.seq, seq));
958 * read_persistent_clock - Return time from the persistent clock.
960 * Weak dummy function for arches that do not yet support it.
961 * Reads the time from the battery backed persistent clock.
962 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
964 * XXX - Do be sure to remove it once all arches implement it.
966 void __weak read_persistent_clock(struct timespec *ts)
973 * read_boot_clock - Return time of the system start.
975 * Weak dummy function for arches that do not yet support it.
976 * Function to read the exact time the system has been started.
977 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
979 * XXX - Do be sure to remove it once all arches implement it.
981 void __weak read_boot_clock(struct timespec *ts)
988 * timekeeping_init - Initializes the clocksource and common timekeeping values
990 void __init timekeeping_init(void)
992 struct timekeeper *tk = &tk_core.timekeeper;
993 struct clocksource *clock;
995 struct timespec64 now, boot, tmp;
998 read_persistent_clock(&ts);
999 now = timespec_to_timespec64(ts);
1000 if (!timespec64_valid_strict(&now)) {
1001 pr_warn("WARNING: Persistent clock returned invalid value!\n"
1002 " Check your CMOS/BIOS settings.\n");
1005 } else if (now.tv_sec || now.tv_nsec)
1006 persistent_clock_exist = true;
1008 read_boot_clock(&ts);
1009 boot = timespec_to_timespec64(ts);
1010 if (!timespec64_valid_strict(&boot)) {
1011 pr_warn("WARNING: Boot clock returned invalid value!\n"
1012 " Check your CMOS/BIOS settings.\n");
1017 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1018 write_seqcount_begin(&tk_core.seq);
1021 clock = clocksource_default_clock();
1023 clock->enable(clock);
1024 tk_setup_internals(tk, clock);
1026 tk_set_xtime(tk, &now);
1027 tk->raw_time.tv_sec = 0;
1028 tk->raw_time.tv_nsec = 0;
1029 tk->base_raw.tv64 = 0;
1030 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
1031 boot = tk_xtime(tk);
1033 set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec);
1034 tk_set_wall_to_mono(tk, tmp);
1036 timekeeping_update(tk, TK_MIRROR);
1038 write_seqcount_end(&tk_core.seq);
1039 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1042 /* time in seconds when suspend began */
1043 static struct timespec64 timekeeping_suspend_time;
1046 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
1047 * @delta: pointer to a timespec delta value
1049 * Takes a timespec offset measuring a suspend interval and properly
1050 * adds the sleep offset to the timekeeping variables.
1052 static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
1053 struct timespec64 *delta)
1055 if (!timespec64_valid_strict(delta)) {
1056 printk_deferred(KERN_WARNING
1057 "__timekeeping_inject_sleeptime: Invalid "
1058 "sleep delta value!\n");
1061 tk_xtime_add(tk, delta);
1062 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
1063 tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
1064 tk_debug_account_sleep_time(delta);
1068 * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
1069 * @delta: pointer to a timespec delta value
1071 * This hook is for architectures that cannot support read_persistent_clock
1072 * because their RTC/persistent clock is only accessible when irqs are enabled.
1074 * This function should only be called by rtc_resume(), and allows
1075 * a suspend offset to be injected into the timekeeping values.
1077 void timekeeping_inject_sleeptime(struct timespec *delta)
1079 struct timekeeper *tk = &tk_core.timekeeper;
1080 struct timespec64 tmp;
1081 unsigned long flags;
1084 * Make sure we don't set the clock twice, as timekeeping_resume()
1087 if (has_persistent_clock())
1090 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1091 write_seqcount_begin(&tk_core.seq);
1093 timekeeping_forward_now(tk);
1095 tmp = timespec_to_timespec64(*delta);
1096 __timekeeping_inject_sleeptime(tk, &tmp);
1098 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
1100 write_seqcount_end(&tk_core.seq);
1101 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1103 /* signal hrtimers about time change */
1108 * timekeeping_resume - Resumes the generic timekeeping subsystem.
1110 * This is for the generic clocksource timekeeping.
1111 * xtime/wall_to_monotonic/jiffies/etc are
1112 * still managed by arch specific suspend/resume code.
1114 static void timekeeping_resume(void)
1116 struct timekeeper *tk = &tk_core.timekeeper;
1117 struct clocksource *clock = tk->tkr.clock;
1118 unsigned long flags;
1119 struct timespec64 ts_new, ts_delta;
1120 struct timespec tmp;
1121 cycle_t cycle_now, cycle_delta;
1122 bool suspendtime_found = false;
1124 read_persistent_clock(&tmp);
1125 ts_new = timespec_to_timespec64(tmp);
1127 clockevents_resume();
1128 clocksource_resume();
1130 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1131 write_seqcount_begin(&tk_core.seq);
1134 * After system resumes, we need to calculate the suspended time and
1135 * compensate it for the OS time. There are 3 sources that could be
1136 * used: Nonstop clocksource during suspend, persistent clock and rtc
1139 * One specific platform may have 1 or 2 or all of them, and the
1140 * preference will be:
1141 * suspend-nonstop clocksource -> persistent clock -> rtc
1142 * The less preferred source will only be tried if there is no better
1143 * usable source. The rtc part is handled separately in rtc core code.
1145 cycle_now = tk->tkr.read(clock);
1146 if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
1147 cycle_now > tk->tkr.cycle_last) {
1148 u64 num, max = ULLONG_MAX;
1149 u32 mult = clock->mult;
1150 u32 shift = clock->shift;
1153 cycle_delta = clocksource_delta(cycle_now, tk->tkr.cycle_last,
1157 * "cycle_delta * mutl" may cause 64 bits overflow, if the
1158 * suspended time is too long. In that case we need do the
1159 * 64 bits math carefully
1162 if (cycle_delta > max) {
1163 num = div64_u64(cycle_delta, max);
1164 nsec = (((u64) max * mult) >> shift) * num;
1165 cycle_delta -= num * max;
1167 nsec += ((u64) cycle_delta * mult) >> shift;
1169 ts_delta = ns_to_timespec64(nsec);
1170 suspendtime_found = true;
1171 } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
1172 ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
1173 suspendtime_found = true;
1176 if (suspendtime_found)
1177 __timekeeping_inject_sleeptime(tk, &ts_delta);
1179 /* Re-base the last cycle value */
1180 tk->tkr.cycle_last = cycle_now;
1182 timekeeping_suspended = 0;
1183 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
1184 write_seqcount_end(&tk_core.seq);
1185 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1187 touch_softlockup_watchdog();
1189 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
1191 /* Resume hrtimers */
1195 static int timekeeping_suspend(void)
1197 struct timekeeper *tk = &tk_core.timekeeper;
1198 unsigned long flags;
1199 struct timespec64 delta, delta_delta;
1200 static struct timespec64 old_delta;
1201 struct timespec tmp;
1203 read_persistent_clock(&tmp);
1204 timekeeping_suspend_time = timespec_to_timespec64(tmp);
1207 * On some systems the persistent_clock can not be detected at
1208 * timekeeping_init by its return value, so if we see a valid
1209 * value returned, update the persistent_clock_exists flag.
1211 if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
1212 persistent_clock_exist = true;
1214 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1215 write_seqcount_begin(&tk_core.seq);
1216 timekeeping_forward_now(tk);
1217 timekeeping_suspended = 1;
1220 * To avoid drift caused by repeated suspend/resumes,
1221 * which each can add ~1 second drift error,
1222 * try to compensate so the difference in system time
1223 * and persistent_clock time stays close to constant.
1225 delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time);
1226 delta_delta = timespec64_sub(delta, old_delta);
1227 if (abs(delta_delta.tv_sec) >= 2) {
1229 * if delta_delta is too large, assume time correction
1230 * has occured and set old_delta to the current delta.
1234 /* Otherwise try to adjust old_system to compensate */
1235 timekeeping_suspend_time =
1236 timespec64_add(timekeeping_suspend_time, delta_delta);
1239 timekeeping_update(tk, TK_MIRROR);
1240 write_seqcount_end(&tk_core.seq);
1241 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1243 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
1244 clocksource_suspend();
1245 clockevents_suspend();
1250 /* sysfs resume/suspend bits for timekeeping */
1251 static struct syscore_ops timekeeping_syscore_ops = {
1252 .resume = timekeeping_resume,
1253 .suspend = timekeeping_suspend,
1256 static int __init timekeeping_init_ops(void)
1258 register_syscore_ops(&timekeeping_syscore_ops);
1261 device_initcall(timekeeping_init_ops);
1264 * Apply a multiplier adjustment to the timekeeper
1266 static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
1271 s64 interval = tk->cycle_interval;
1275 mult_adj = -mult_adj;
1276 interval = -interval;
1279 mult_adj <<= adj_scale;
1280 interval <<= adj_scale;
1281 offset <<= adj_scale;
1284 * So the following can be confusing.
1286 * To keep things simple, lets assume mult_adj == 1 for now.
1288 * When mult_adj != 1, remember that the interval and offset values
1289 * have been appropriately scaled so the math is the same.
1291 * The basic idea here is that we're increasing the multiplier
1292 * by one, this causes the xtime_interval to be incremented by
1293 * one cycle_interval. This is because:
1294 * xtime_interval = cycle_interval * mult
1295 * So if mult is being incremented by one:
1296 * xtime_interval = cycle_interval * (mult + 1)
1298 * xtime_interval = (cycle_interval * mult) + cycle_interval
1299 * Which can be shortened to:
1300 * xtime_interval += cycle_interval
1302 * So offset stores the non-accumulated cycles. Thus the current
1303 * time (in shifted nanoseconds) is:
1304 * now = (offset * adj) + xtime_nsec
1305 * Now, even though we're adjusting the clock frequency, we have
1306 * to keep time consistent. In other words, we can't jump back
1307 * in time, and we also want to avoid jumping forward in time.
1309 * So given the same offset value, we need the time to be the same
1310 * both before and after the freq adjustment.
1311 * now = (offset * adj_1) + xtime_nsec_1
1312 * now = (offset * adj_2) + xtime_nsec_2
1314 * (offset * adj_1) + xtime_nsec_1 =
1315 * (offset * adj_2) + xtime_nsec_2
1319 * (offset * adj_1) + xtime_nsec_1 =
1320 * (offset * (adj_1+1)) + xtime_nsec_2
1321 * (offset * adj_1) + xtime_nsec_1 =
1322 * (offset * adj_1) + offset + xtime_nsec_2
1323 * Canceling the sides:
1324 * xtime_nsec_1 = offset + xtime_nsec_2
1326 * xtime_nsec_2 = xtime_nsec_1 - offset
1327 * Which simplfies to:
1328 * xtime_nsec -= offset
1330 * XXX - TODO: Doc ntp_error calculation.
1332 tk->tkr.mult += mult_adj;
1333 tk->xtime_interval += interval;
1334 tk->tkr.xtime_nsec -= offset;
1335 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
1339 * Calculate the multiplier adjustment needed to match the frequency
1342 static __always_inline void timekeeping_freqadjust(struct timekeeper *tk,
1345 s64 interval = tk->cycle_interval;
1346 s64 xinterval = tk->xtime_interval;
1351 /* Remove any current error adj from freq calculation */
1352 if (tk->ntp_err_mult)
1353 xinterval -= tk->cycle_interval;
1355 /* Calculate current error per tick */
1356 tick_error = ntp_tick_length() >> tk->ntp_error_shift;
1357 tick_error -= (xinterval + tk->xtime_remainder);
1359 /* Don't worry about correcting it if its small */
1360 if (likely((tick_error >= 0) && (tick_error <= interval)))
1363 /* preserve the direction of correction */
1364 negative = (tick_error < 0);
1366 /* Sort out the magnitude of the correction */
1367 tick_error = abs(tick_error);
1368 for (adj = 0; tick_error > interval; adj++)
1371 /* scale the corrections */
1372 timekeeping_apply_adjustment(tk, offset, negative, adj);
1376 * Adjust the timekeeper's multiplier to the correct frequency
1377 * and also to reduce the accumulated error value.
1379 static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
1381 /* Correct for the current frequency error */
1382 timekeeping_freqadjust(tk, offset);
1384 /* Next make a small adjustment to fix any cumulative error */
1385 if (!tk->ntp_err_mult && (tk->ntp_error > 0)) {
1386 tk->ntp_err_mult = 1;
1387 timekeeping_apply_adjustment(tk, offset, 0, 0);
1388 } else if (tk->ntp_err_mult && (tk->ntp_error <= 0)) {
1389 /* Undo any existing error adjustment */
1390 timekeeping_apply_adjustment(tk, offset, 1, 0);
1391 tk->ntp_err_mult = 0;
1394 if (unlikely(tk->tkr.clock->maxadj &&
1395 (tk->tkr.mult > tk->tkr.clock->mult + tk->tkr.clock->maxadj))) {
1396 printk_once(KERN_WARNING
1397 "Adjusting %s more than 11%% (%ld vs %ld)\n",
1398 tk->tkr.clock->name, (long)tk->tkr.mult,
1399 (long)tk->tkr.clock->mult + tk->tkr.clock->maxadj);
1403 * It may be possible that when we entered this function, xtime_nsec
1404 * was very small. Further, if we're slightly speeding the clocksource
1405 * in the code above, its possible the required corrective factor to
1406 * xtime_nsec could cause it to underflow.
1408 * Now, since we already accumulated the second, cannot simply roll
1409 * the accumulated second back, since the NTP subsystem has been
1410 * notified via second_overflow. So instead we push xtime_nsec forward
1411 * by the amount we underflowed, and add that amount into the error.
1413 * We'll correct this error next time through this function, when
1414 * xtime_nsec is not as small.
1416 if (unlikely((s64)tk->tkr.xtime_nsec < 0)) {
1417 s64 neg = -(s64)tk->tkr.xtime_nsec;
1418 tk->tkr.xtime_nsec = 0;
1419 tk->ntp_error += neg << tk->ntp_error_shift;
1424 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
1426 * Helper function that accumulates a the nsecs greater then a second
1427 * from the xtime_nsec field to the xtime_secs field.
1428 * It also calls into the NTP code to handle leapsecond processing.
1431 static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
1433 u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr.shift;
1434 unsigned int clock_set = 0;
1436 while (tk->tkr.xtime_nsec >= nsecps) {
1439 tk->tkr.xtime_nsec -= nsecps;
1442 /* Figure out if its a leap sec and apply if needed */
1443 leap = second_overflow(tk->xtime_sec);
1444 if (unlikely(leap)) {
1445 struct timespec64 ts;
1447 tk->xtime_sec += leap;
1451 tk_set_wall_to_mono(tk,
1452 timespec64_sub(tk->wall_to_monotonic, ts));
1454 __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
1456 clock_set = TK_CLOCK_WAS_SET;
1463 * logarithmic_accumulation - shifted accumulation of cycles
1465 * This functions accumulates a shifted interval of cycles into
1466 * into a shifted interval nanoseconds. Allows for O(log) accumulation
1469 * Returns the unconsumed cycles.
1471 static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
1473 unsigned int *clock_set)
1475 cycle_t interval = tk->cycle_interval << shift;
1478 /* If the offset is smaller then a shifted interval, do nothing */
1479 if (offset < interval)
1482 /* Accumulate one shifted interval */
1484 tk->tkr.cycle_last += interval;
1486 tk->tkr.xtime_nsec += tk->xtime_interval << shift;
1487 *clock_set |= accumulate_nsecs_to_secs(tk);
1489 /* Accumulate raw time */
1490 raw_nsecs = (u64)tk->raw_interval << shift;
1491 raw_nsecs += tk->raw_time.tv_nsec;
1492 if (raw_nsecs >= NSEC_PER_SEC) {
1493 u64 raw_secs = raw_nsecs;
1494 raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
1495 tk->raw_time.tv_sec += raw_secs;
1497 tk->raw_time.tv_nsec = raw_nsecs;
1499 /* Accumulate error between NTP and clock interval */
1500 tk->ntp_error += ntp_tick_length() << shift;
1501 tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
1502 (tk->ntp_error_shift + shift);
1508 * update_wall_time - Uses the current clocksource to increment the wall time
1511 void update_wall_time(void)
1513 struct timekeeper *real_tk = &tk_core.timekeeper;
1514 struct timekeeper *tk = &shadow_timekeeper;
1516 int shift = 0, maxshift;
1517 unsigned int clock_set = 0;
1518 unsigned long flags;
1520 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1522 /* Make sure we're fully resumed: */
1523 if (unlikely(timekeeping_suspended))
1526 #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
1527 offset = real_tk->cycle_interval;
1529 offset = clocksource_delta(tk->tkr.read(tk->tkr.clock),
1530 tk->tkr.cycle_last, tk->tkr.mask);
1533 /* Check if there's really nothing to do */
1534 if (offset < real_tk->cycle_interval)
1538 * With NO_HZ we may have to accumulate many cycle_intervals
1539 * (think "ticks") worth of time at once. To do this efficiently,
1540 * we calculate the largest doubling multiple of cycle_intervals
1541 * that is smaller than the offset. We then accumulate that
1542 * chunk in one go, and then try to consume the next smaller
1545 shift = ilog2(offset) - ilog2(tk->cycle_interval);
1546 shift = max(0, shift);
1547 /* Bound shift to one less than what overflows tick_length */
1548 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
1549 shift = min(shift, maxshift);
1550 while (offset >= tk->cycle_interval) {
1551 offset = logarithmic_accumulation(tk, offset, shift,
1553 if (offset < tk->cycle_interval<<shift)
1557 /* correct the clock when NTP error is too big */
1558 timekeeping_adjust(tk, offset);
1561 * XXX This can be killed once everyone converts
1562 * to the new update_vsyscall.
1564 old_vsyscall_fixup(tk);
1567 * Finally, make sure that after the rounding
1568 * xtime_nsec isn't larger than NSEC_PER_SEC
1570 clock_set |= accumulate_nsecs_to_secs(tk);
1572 write_seqcount_begin(&tk_core.seq);
1574 * Update the real timekeeper.
1576 * We could avoid this memcpy by switching pointers, but that
1577 * requires changes to all other timekeeper usage sites as
1578 * well, i.e. move the timekeeper pointer getter into the
1579 * spinlocked/seqcount protected sections. And we trade this
1580 * memcpy under the tk_core.seq against one before we start
1583 memcpy(real_tk, tk, sizeof(*tk));
1584 timekeeping_update(real_tk, clock_set);
1585 write_seqcount_end(&tk_core.seq);
1587 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1589 /* Have to call _delayed version, since in irq context*/
1590 clock_was_set_delayed();
1594 * getboottime - Return the real time of system boot.
1595 * @ts: pointer to the timespec to be set
1597 * Returns the wall-time of boot in a timespec.
1599 * This is based on the wall_to_monotonic offset and the total suspend
1600 * time. Calls to settimeofday will affect the value returned (which
1601 * basically means that however wrong your real time clock is at boot time,
1602 * you get the right time here).
1604 void getboottime(struct timespec *ts)
1606 struct timekeeper *tk = &tk_core.timekeeper;
1607 ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot);
1609 *ts = ktime_to_timespec(t);
1611 EXPORT_SYMBOL_GPL(getboottime);
1613 unsigned long get_seconds(void)
1615 struct timekeeper *tk = &tk_core.timekeeper;
1617 return tk->xtime_sec;
1619 EXPORT_SYMBOL(get_seconds);
1621 struct timespec __current_kernel_time(void)
1623 struct timekeeper *tk = &tk_core.timekeeper;
1625 return timespec64_to_timespec(tk_xtime(tk));
1628 struct timespec current_kernel_time(void)
1630 struct timekeeper *tk = &tk_core.timekeeper;
1631 struct timespec64 now;
1635 seq = read_seqcount_begin(&tk_core.seq);
1638 } while (read_seqcount_retry(&tk_core.seq, seq));
1640 return timespec64_to_timespec(now);
1642 EXPORT_SYMBOL(current_kernel_time);
1644 struct timespec get_monotonic_coarse(void)
1646 struct timekeeper *tk = &tk_core.timekeeper;
1647 struct timespec64 now, mono;
1651 seq = read_seqcount_begin(&tk_core.seq);
1654 mono = tk->wall_to_monotonic;
1655 } while (read_seqcount_retry(&tk_core.seq, seq));
1657 set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec,
1658 now.tv_nsec + mono.tv_nsec);
1660 return timespec64_to_timespec(now);
1664 * Must hold jiffies_lock
1666 void do_timer(unsigned long ticks)
1668 jiffies_64 += ticks;
1669 calc_global_load(ticks);
1673 * ktime_get_update_offsets_tick - hrtimer helper
1674 * @offs_real: pointer to storage for monotonic -> realtime offset
1675 * @offs_boot: pointer to storage for monotonic -> boottime offset
1676 * @offs_tai: pointer to storage for monotonic -> clock tai offset
1678 * Returns monotonic time at last tick and various offsets
1680 ktime_t ktime_get_update_offsets_tick(ktime_t *offs_real, ktime_t *offs_boot,
1683 struct timekeeper *tk = &tk_core.timekeeper;
1689 seq = read_seqcount_begin(&tk_core.seq);
1691 base = tk->tkr.base_mono;
1692 nsecs = tk->tkr.xtime_nsec >> tk->tkr.shift;
1694 *offs_real = tk->offs_real;
1695 *offs_boot = tk->offs_boot;
1696 *offs_tai = tk->offs_tai;
1697 } while (read_seqcount_retry(&tk_core.seq, seq));
1699 return ktime_add_ns(base, nsecs);
1702 #ifdef CONFIG_HIGH_RES_TIMERS
1704 * ktime_get_update_offsets_now - hrtimer helper
1705 * @offs_real: pointer to storage for monotonic -> realtime offset
1706 * @offs_boot: pointer to storage for monotonic -> boottime offset
1707 * @offs_tai: pointer to storage for monotonic -> clock tai offset
1709 * Returns current monotonic time and updates the offsets
1710 * Called from hrtimer_interrupt() or retrigger_next_event()
1712 ktime_t ktime_get_update_offsets_now(ktime_t *offs_real, ktime_t *offs_boot,
1715 struct timekeeper *tk = &tk_core.timekeeper;
1721 seq = read_seqcount_begin(&tk_core.seq);
1723 base = tk->tkr.base_mono;
1724 nsecs = timekeeping_get_ns(&tk->tkr);
1726 *offs_real = tk->offs_real;
1727 *offs_boot = tk->offs_boot;
1728 *offs_tai = tk->offs_tai;
1729 } while (read_seqcount_retry(&tk_core.seq, seq));
1731 return ktime_add_ns(base, nsecs);
1736 * do_adjtimex() - Accessor function to NTP __do_adjtimex function
1738 int do_adjtimex(struct timex *txc)
1740 struct timekeeper *tk = &tk_core.timekeeper;
1741 unsigned long flags;
1742 struct timespec64 ts;
1746 /* Validate the data before disabling interrupts */
1747 ret = ntp_validate_timex(txc);
1751 if (txc->modes & ADJ_SETOFFSET) {
1752 struct timespec delta;
1753 delta.tv_sec = txc->time.tv_sec;
1754 delta.tv_nsec = txc->time.tv_usec;
1755 if (!(txc->modes & ADJ_NANO))
1756 delta.tv_nsec *= 1000;
1757 ret = timekeeping_inject_offset(&delta);
1762 getnstimeofday64(&ts);
1764 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1765 write_seqcount_begin(&tk_core.seq);
1767 orig_tai = tai = tk->tai_offset;
1768 ret = __do_adjtimex(txc, &ts, &tai);
1770 if (tai != orig_tai) {
1771 __timekeeping_set_tai_offset(tk, tai);
1772 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
1774 write_seqcount_end(&tk_core.seq);
1775 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1777 if (tai != orig_tai)
1780 ntp_notify_cmos_timer();
1785 #ifdef CONFIG_NTP_PPS
1787 * hardpps() - Accessor function to NTP __hardpps function
1789 void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
1791 unsigned long flags;
1793 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1794 write_seqcount_begin(&tk_core.seq);
1796 __hardpps(phase_ts, raw_ts);
1798 write_seqcount_end(&tk_core.seq);
1799 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1801 EXPORT_SYMBOL(hardpps);
1805 * xtime_update() - advances the timekeeping infrastructure
1806 * @ticks: number of ticks, that have elapsed since the last call.
1808 * Must be called with interrupts disabled.
1810 void xtime_update(unsigned long ticks)
1812 write_seqlock(&jiffies_lock);
1814 write_sequnlock(&jiffies_lock);