xref: /linux-6.15/kernel/time/timekeeping.c (revision b71f9804)
135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
28524070bSjohn stultz /*
358c5fc2bSThomas Gleixner  *  Kernel timekeeping code and accessor functions. Based on code from
458c5fc2bSThomas Gleixner  *  timer.c, moved in commit 8524070b7982.
58524070bSjohn stultz  */
6d7b4202eSJohn Stultz #include <linux/timekeeper_internal.h>
78524070bSjohn stultz #include <linux/module.h>
88524070bSjohn stultz #include <linux/interrupt.h>
98524070bSjohn stultz #include <linux/percpu.h>
108524070bSjohn stultz #include <linux/init.h>
118524070bSjohn stultz #include <linux/mm.h>
1238b8d208SIngo Molnar #include <linux/nmi.h>
13d43c36dcSAlexey Dobriyan #include <linux/sched.h>
144f17722cSIngo Molnar #include <linux/sched/loadavg.h>
153eca9937SPavel Tatashin #include <linux/sched/clock.h>
16e1a85b2cSRafael J. Wysocki #include <linux/syscore_ops.h>
178524070bSjohn stultz #include <linux/clocksource.h>
188524070bSjohn stultz #include <linux/jiffies.h>
198524070bSjohn stultz #include <linux/time.h>
201366992eSJason A. Donenfeld #include <linux/timex.h>
218524070bSjohn stultz #include <linux/tick.h>
2275c5158fSMartin Schwidefsky #include <linux/stop_machine.h>
23e0b306feSMarcelo Tosatti #include <linux/pvclock_gtod.h>
2452f5684cSGideon Israel Dsouza #include <linux/compiler.h>
252d87a067SOndrej Mosnacek #include <linux/audit.h>
26b8ac29b4SJason A. Donenfeld #include <linux/random.h>
278524070bSjohn stultz 
28eb93e4d9SThomas Gleixner #include "tick-internal.h"
29aa6f9c59SJohn Stultz #include "ntp_internal.h"
305c83545fSColin Cross #include "timekeeping_internal.h"
31155ec602SMartin Schwidefsky 
3204397fe9SDavid Vrabel #define TK_CLEAR_NTP		(1 << 0)
330026766dSAnna-Maria Behnsen #define TK_CLOCK_WAS_SET	(1 << 1)
3404397fe9SDavid Vrabel 
356b1ef640SAnna-Maria Behnsen #define TK_UPDATE_ALL		(TK_CLEAR_NTP | TK_CLOCK_WAS_SET)
36155ec602SMartin Schwidefsky 
37b061c7a5SMiroslav Lichvar enum timekeeping_adv_mode {
38b061c7a5SMiroslav Lichvar 	/* Update timekeeper when a tick has passed */
39b061c7a5SMiroslav Lichvar 	TK_ADV_TICK,
40b061c7a5SMiroslav Lichvar 
41b061c7a5SMiroslav Lichvar 	/* Update timekeeper on a direct frequency change */
42b061c7a5SMiroslav Lichvar 	TK_ADV_FREQ
43b061c7a5SMiroslav Lichvar };
44b061c7a5SMiroslav Lichvar 
453fdb14fdSThomas Gleixner /*
463fdb14fdSThomas Gleixner  * The most important data for readout fits into a single 64 byte
473fdb14fdSThomas Gleixner  * cache line.
483fdb14fdSThomas Gleixner  */
4910f7c178SAnna-Maria Behnsen struct tk_data {
50025e82bcSAhmed S. Darwish 	seqcount_raw_spinlock_t	seq;
513fdb14fdSThomas Gleixner 	struct timekeeper	timekeeper;
5220c7b582SThomas Gleixner 	struct timekeeper	shadow_timekeeper;
538c4799b1SAnna-Maria Behnsen 	raw_spinlock_t		lock;
5410f7c178SAnna-Maria Behnsen } ____cacheline_aligned;
553fdb14fdSThomas Gleixner 
5610f7c178SAnna-Maria Behnsen static struct tk_data tk_core;
57155ec602SMartin Schwidefsky 
5871419b30SThomas Gleixner /* flag for if timekeeping is suspended */
5971419b30SThomas Gleixner int __read_mostly timekeeping_suspended;
6071419b30SThomas Gleixner 
614396e058SThomas Gleixner /**
624396e058SThomas Gleixner  * struct tk_fast - NMI safe timekeeper
634396e058SThomas Gleixner  * @seq:	Sequence counter for protecting updates. The lowest bit
644396e058SThomas Gleixner  *		is the index for the tk_read_base array
654396e058SThomas Gleixner  * @base:	tk_read_base array. Access is indexed by the lowest bit of
664396e058SThomas Gleixner  *		@seq.
674396e058SThomas Gleixner  *
684396e058SThomas Gleixner  * See @update_fast_timekeeper() below.
694396e058SThomas Gleixner  */
704396e058SThomas Gleixner struct tk_fast {
71249d0538SAhmed S. Darwish 	seqcount_latch_t	seq;
724396e058SThomas Gleixner 	struct tk_read_base	base[2];
734396e058SThomas Gleixner };
744396e058SThomas Gleixner 
755df32107SPrarit Bhargava /* Suspend-time cycles value for halted fast timekeeper. */
765df32107SPrarit Bhargava static u64 cycles_at_suspend;
775df32107SPrarit Bhargava 
dummy_clock_read(struct clocksource * cs)785df32107SPrarit Bhargava static u64 dummy_clock_read(struct clocksource *cs)
795df32107SPrarit Bhargava {
8071419b30SThomas Gleixner 	if (timekeeping_suspended)
815df32107SPrarit Bhargava 		return cycles_at_suspend;
8271419b30SThomas Gleixner 	return local_clock();
835df32107SPrarit Bhargava }
845df32107SPrarit Bhargava 
855df32107SPrarit Bhargava static struct clocksource dummy_clock = {
865df32107SPrarit Bhargava 	.read = dummy_clock_read,
875df32107SPrarit Bhargava };
885df32107SPrarit Bhargava 
8971419b30SThomas Gleixner /*
9071419b30SThomas Gleixner  * Boot time initialization which allows local_clock() to be utilized
9171419b30SThomas Gleixner  * during early boot when clocksources are not available. local_clock()
9271419b30SThomas Gleixner  * returns nanoseconds already so no conversion is required, hence mult=1
9371419b30SThomas Gleixner  * and shift=0. When the first proper clocksource is installed then
9471419b30SThomas Gleixner  * the fast time keepers are updated with the correct values.
9571419b30SThomas Gleixner  */
9671419b30SThomas Gleixner #define FAST_TK_INIT						\
9771419b30SThomas Gleixner 	{							\
9871419b30SThomas Gleixner 		.clock		= &dummy_clock,			\
9971419b30SThomas Gleixner 		.mask		= CLOCKSOURCE_MASK(64),		\
10071419b30SThomas Gleixner 		.mult		= 1,				\
10171419b30SThomas Gleixner 		.shift		= 0,				\
10271419b30SThomas Gleixner 	}
10371419b30SThomas Gleixner 
1045df32107SPrarit Bhargava static struct tk_fast tk_fast_mono ____cacheline_aligned = {
105249d0538SAhmed S. Darwish 	.seq     = SEQCNT_LATCH_ZERO(tk_fast_mono.seq),
10671419b30SThomas Gleixner 	.base[0] = FAST_TK_INIT,
10771419b30SThomas Gleixner 	.base[1] = FAST_TK_INIT,
1085df32107SPrarit Bhargava };
1095df32107SPrarit Bhargava 
1105df32107SPrarit Bhargava static struct tk_fast tk_fast_raw  ____cacheline_aligned = {
111249d0538SAhmed S. Darwish 	.seq     = SEQCNT_LATCH_ZERO(tk_fast_raw.seq),
11271419b30SThomas Gleixner 	.base[0] = FAST_TK_INIT,
11371419b30SThomas Gleixner 	.base[1] = FAST_TK_INIT,
1145df32107SPrarit Bhargava };
1154396e058SThomas Gleixner 
timekeeper_lock_irqsave(void)116dbdcf8c4SThomas Gleixner unsigned long timekeeper_lock_irqsave(void)
117dbdcf8c4SThomas Gleixner {
118dbdcf8c4SThomas Gleixner 	unsigned long flags;
119dbdcf8c4SThomas Gleixner 
1208c4799b1SAnna-Maria Behnsen 	raw_spin_lock_irqsave(&tk_core.lock, flags);
121dbdcf8c4SThomas Gleixner 	return flags;
122dbdcf8c4SThomas Gleixner }
123dbdcf8c4SThomas Gleixner 
timekeeper_unlock_irqrestore(unsigned long flags)124dbdcf8c4SThomas Gleixner void timekeeper_unlock_irqrestore(unsigned long flags)
125dbdcf8c4SThomas Gleixner {
1268c4799b1SAnna-Maria Behnsen 	raw_spin_unlock_irqrestore(&tk_core.lock, flags);
127dbdcf8c4SThomas Gleixner }
128dbdcf8c4SThomas Gleixner 
129ee3283c6SJeff Layton /*
130ee3283c6SJeff Layton  * Multigrain timestamps require tracking the latest fine-grained timestamp
131ee3283c6SJeff Layton  * that has been issued, and never returning a coarse-grained timestamp that is
132ee3283c6SJeff Layton  * earlier than that value.
133ee3283c6SJeff Layton  *
134ee3283c6SJeff Layton  * mg_floor represents the latest fine-grained time that has been handed out as
135ee3283c6SJeff Layton  * a file timestamp on the system. This is tracked as a monotonic ktime_t, and
136ee3283c6SJeff Layton  * converted to a realtime clock value on an as-needed basis.
137ee3283c6SJeff Layton  *
138ee3283c6SJeff Layton  * Maintaining mg_floor ensures the multigrain interfaces never issue a
139ee3283c6SJeff Layton  * timestamp earlier than one that has been previously issued.
140ee3283c6SJeff Layton  *
141ee3283c6SJeff Layton  * The exception to this rule is when there is a backward realtime clock jump. If
142ee3283c6SJeff Layton  * such an event occurs, a timestamp can appear to be earlier than a previous one.
143ee3283c6SJeff Layton  */
144ee3283c6SJeff Layton static __cacheline_aligned_in_smp atomic64_t mg_floor;
145ee3283c6SJeff Layton 
tk_normalize_xtime(struct timekeeper * tk)1461e75fa8bSJohn Stultz static inline void tk_normalize_xtime(struct timekeeper *tk)
1471e75fa8bSJohn Stultz {
148876e7881SPeter Zijlstra 	while (tk->tkr_mono.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_mono.shift)) {
149876e7881SPeter Zijlstra 		tk->tkr_mono.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
1501e75fa8bSJohn Stultz 		tk->xtime_sec++;
1511e75fa8bSJohn Stultz 	}
152fc6eead7SJohn Stultz 	while (tk->tkr_raw.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_raw.shift)) {
153fc6eead7SJohn Stultz 		tk->tkr_raw.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
154fc6eead7SJohn Stultz 		tk->raw_sec++;
155fc6eead7SJohn Stultz 	}
1561e75fa8bSJohn Stultz }
1578fcce546SJohn Stultz 
tk_xtime(const struct timekeeper * tk)158985e6950SOndrej Mosnacek static inline struct timespec64 tk_xtime(const struct timekeeper *tk)
159c905fae4SThomas Gleixner {
160c905fae4SThomas Gleixner 	struct timespec64 ts;
161c905fae4SThomas Gleixner 
162c905fae4SThomas Gleixner 	ts.tv_sec = tk->xtime_sec;
163876e7881SPeter Zijlstra 	ts.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
164c905fae4SThomas Gleixner 	return ts;
165c905fae4SThomas Gleixner }
166c905fae4SThomas Gleixner 
tk_xtime_coarse(const struct timekeeper * tk)167*b71f9804SThomas Gleixner static inline struct timespec64 tk_xtime_coarse(const struct timekeeper *tk)
168*b71f9804SThomas Gleixner {
169*b71f9804SThomas Gleixner 	struct timespec64 ts;
170*b71f9804SThomas Gleixner 
171*b71f9804SThomas Gleixner 	ts.tv_sec = tk->xtime_sec;
172*b71f9804SThomas Gleixner 	ts.tv_nsec = tk->coarse_nsec;
173*b71f9804SThomas Gleixner 	return ts;
174*b71f9804SThomas Gleixner }
175*b71f9804SThomas Gleixner 
176*b71f9804SThomas Gleixner /*
177*b71f9804SThomas Gleixner  * Update the nanoseconds part for the coarse time keepers. They can't rely
178*b71f9804SThomas Gleixner  * on xtime_nsec because xtime_nsec could be adjusted by a small negative
179*b71f9804SThomas Gleixner  * amount when the multiplication factor of the clock is adjusted, which
180*b71f9804SThomas Gleixner  * could cause the coarse clocks to go slightly backwards. See
181*b71f9804SThomas Gleixner  * timekeeping_apply_adjustment(). Thus we keep a separate copy for the coarse
182*b71f9804SThomas Gleixner  * clockids which only is updated when the clock has been set or  we have
183*b71f9804SThomas Gleixner  * accumulated time.
184*b71f9804SThomas Gleixner  */
tk_update_coarse_nsecs(struct timekeeper * tk)185*b71f9804SThomas Gleixner static inline void tk_update_coarse_nsecs(struct timekeeper *tk)
186*b71f9804SThomas Gleixner {
187*b71f9804SThomas Gleixner 	tk->coarse_nsec = tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
188*b71f9804SThomas Gleixner }
189*b71f9804SThomas Gleixner 
tk_set_xtime(struct timekeeper * tk,const struct timespec64 * ts)1907d489d15SJohn Stultz static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
1911e75fa8bSJohn Stultz {
1921e75fa8bSJohn Stultz 	tk->xtime_sec = ts->tv_sec;
193876e7881SPeter Zijlstra 	tk->tkr_mono.xtime_nsec = (u64)ts->tv_nsec << tk->tkr_mono.shift;
194*b71f9804SThomas Gleixner 	tk_update_coarse_nsecs(tk);
1951e75fa8bSJohn Stultz }
1961e75fa8bSJohn Stultz 
tk_xtime_add(struct timekeeper * tk,const struct timespec64 * ts)1977d489d15SJohn Stultz static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
1981e75fa8bSJohn Stultz {
1991e75fa8bSJohn Stultz 	tk->xtime_sec += ts->tv_sec;
200876e7881SPeter Zijlstra 	tk->tkr_mono.xtime_nsec += (u64)ts->tv_nsec << tk->tkr_mono.shift;
201784ffcbbSJohn Stultz 	tk_normalize_xtime(tk);
202*b71f9804SThomas Gleixner 	tk_update_coarse_nsecs(tk);
2031e75fa8bSJohn Stultz }
2048fcce546SJohn Stultz 
tk_set_wall_to_mono(struct timekeeper * tk,struct timespec64 wtm)2057d489d15SJohn Stultz static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
2066d0ef903SJohn Stultz {
2077d489d15SJohn Stultz 	struct timespec64 tmp;
2086d0ef903SJohn Stultz 
2096d0ef903SJohn Stultz 	/*
2106d0ef903SJohn Stultz 	 * Verify consistency of: offset_real = -wall_to_monotonic
2116d0ef903SJohn Stultz 	 * before modifying anything
2126d0ef903SJohn Stultz 	 */
2137d489d15SJohn Stultz 	set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec,
2146d0ef903SJohn Stultz 					-tk->wall_to_monotonic.tv_nsec);
2152456e855SThomas Gleixner 	WARN_ON_ONCE(tk->offs_real != timespec64_to_ktime(tmp));
2166d0ef903SJohn Stultz 	tk->wall_to_monotonic = wtm;
2177d489d15SJohn Stultz 	set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
2188c111f1bSJeff Layton 	/* Paired with READ_ONCE() in ktime_mono_to_any() */
2198c111f1bSJeff Layton 	WRITE_ONCE(tk->offs_real, timespec64_to_ktime(tmp));
2208c111f1bSJeff Layton 	WRITE_ONCE(tk->offs_tai, ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0)));
2216d0ef903SJohn Stultz }
2226d0ef903SJohn Stultz 
tk_update_sleep_time(struct timekeeper * tk,ktime_t delta)22347da70d3SThomas Gleixner static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
2246d0ef903SJohn Stultz {
2258c111f1bSJeff Layton 	/* Paired with READ_ONCE() in ktime_mono_to_any() */
2268c111f1bSJeff Layton 	WRITE_ONCE(tk->offs_boot, ktime_add(tk->offs_boot, delta));
227b99328a6SThomas Gleixner 	/*
228b99328a6SThomas Gleixner 	 * Timespec representation for VDSO update to avoid 64bit division
229b99328a6SThomas Gleixner 	 * on every update.
230b99328a6SThomas Gleixner 	 */
231b99328a6SThomas Gleixner 	tk->monotonic_to_boot = ktime_to_timespec64(tk->offs_boot);
2326d0ef903SJohn Stultz }
2336d0ef903SJohn Stultz 
234ceea5e37SJohn Stultz /*
235ceea5e37SJohn Stultz  * tk_clock_read - atomic clocksource read() helper
236ceea5e37SJohn Stultz  *
237ceea5e37SJohn Stultz  * This helper is necessary to use in the read paths because, while the
238025e82bcSAhmed S. Darwish  * seqcount ensures we don't return a bad value while structures are updated,
239ceea5e37SJohn Stultz  * it doesn't protect from potential crashes. There is the possibility that
240ceea5e37SJohn Stultz  * the tkr's clocksource may change between the read reference, and the
241ceea5e37SJohn Stultz  * clock reference passed to the read function.  This can cause crashes if
242ceea5e37SJohn Stultz  * the wrong clocksource is passed to the wrong read function.
2438c4799b1SAnna-Maria Behnsen  * This isn't necessary to use when holding the tk_core.lock or doing
244ceea5e37SJohn Stultz  * a read of the fast-timekeeper tkrs (which is protected by its own locking
245ceea5e37SJohn Stultz  * and update logic).
246ceea5e37SJohn Stultz  */
tk_clock_read(const struct tk_read_base * tkr)247985e6950SOndrej Mosnacek static inline u64 tk_clock_read(const struct tk_read_base *tkr)
248ceea5e37SJohn Stultz {
249ceea5e37SJohn Stultz 	struct clocksource *clock = READ_ONCE(tkr->clock);
250ceea5e37SJohn Stultz 
251ceea5e37SJohn Stultz 	return clock->read(clock);
252ceea5e37SJohn Stultz }
253ceea5e37SJohn Stultz 
254155ec602SMartin Schwidefsky /**
255d26e4fe0SYijing Wang  * tk_setup_internals - Set up internals to use clocksource clock.
256155ec602SMartin Schwidefsky  *
257d26e4fe0SYijing Wang  * @tk:		The target timekeeper to setup.
258155ec602SMartin Schwidefsky  * @clock:		Pointer to clocksource.
259155ec602SMartin Schwidefsky  *
260155ec602SMartin Schwidefsky  * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
261155ec602SMartin Schwidefsky  * pair and interval request.
262155ec602SMartin Schwidefsky  *
263155ec602SMartin Schwidefsky  * Unless you're the timekeeping code, you should not be using this!
264155ec602SMartin Schwidefsky  */
tk_setup_internals(struct timekeeper * tk,struct clocksource * clock)265f726a697SJohn Stultz static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
266155ec602SMartin Schwidefsky {
267a5a1d1c2SThomas Gleixner 	u64 interval;
268a386b5afSKasper Pedersen 	u64 tmp, ntpinterval;
2691e75fa8bSJohn Stultz 	struct clocksource *old_clock;
270155ec602SMartin Schwidefsky 
2712c756febSChristopher S. Hall 	++tk->cs_was_changed_seq;
272876e7881SPeter Zijlstra 	old_clock = tk->tkr_mono.clock;
273876e7881SPeter Zijlstra 	tk->tkr_mono.clock = clock;
274876e7881SPeter Zijlstra 	tk->tkr_mono.mask = clock->mask;
275ceea5e37SJohn Stultz 	tk->tkr_mono.cycle_last = tk_clock_read(&tk->tkr_mono);
276155ec602SMartin Schwidefsky 
2774a4ad80dSPeter Zijlstra 	tk->tkr_raw.clock = clock;
2784a4ad80dSPeter Zijlstra 	tk->tkr_raw.mask = clock->mask;
2794a4ad80dSPeter Zijlstra 	tk->tkr_raw.cycle_last = tk->tkr_mono.cycle_last;
2804a4ad80dSPeter Zijlstra 
281155ec602SMartin Schwidefsky 	/* Do the ns -> cycle conversion first, using original mult */
282155ec602SMartin Schwidefsky 	tmp = NTP_INTERVAL_LENGTH;
283155ec602SMartin Schwidefsky 	tmp <<= clock->shift;
284a386b5afSKasper Pedersen 	ntpinterval = tmp;
2850a544198SMartin Schwidefsky 	tmp += clock->mult/2;
2860a544198SMartin Schwidefsky 	do_div(tmp, clock->mult);
287155ec602SMartin Schwidefsky 	if (tmp == 0)
288155ec602SMartin Schwidefsky 		tmp = 1;
289155ec602SMartin Schwidefsky 
290a5a1d1c2SThomas Gleixner 	interval = (u64) tmp;
291f726a697SJohn Stultz 	tk->cycle_interval = interval;
292155ec602SMartin Schwidefsky 
293155ec602SMartin Schwidefsky 	/* Go back from cycles -> shifted ns */
294cbd99e3bSThomas Gleixner 	tk->xtime_interval = interval * clock->mult;
295f726a697SJohn Stultz 	tk->xtime_remainder = ntpinterval - tk->xtime_interval;
2963d88d56cSJohn Stultz 	tk->raw_interval = interval * clock->mult;
297155ec602SMartin Schwidefsky 
2981e75fa8bSJohn Stultz 	 /* if changing clocks, convert xtime_nsec shift units */
2991e75fa8bSJohn Stultz 	if (old_clock) {
3001e75fa8bSJohn Stultz 		int shift_change = clock->shift - old_clock->shift;
301fc6eead7SJohn Stultz 		if (shift_change < 0) {
302876e7881SPeter Zijlstra 			tk->tkr_mono.xtime_nsec >>= -shift_change;
303fc6eead7SJohn Stultz 			tk->tkr_raw.xtime_nsec >>= -shift_change;
304fc6eead7SJohn Stultz 		} else {
305876e7881SPeter Zijlstra 			tk->tkr_mono.xtime_nsec <<= shift_change;
306fc6eead7SJohn Stultz 			tk->tkr_raw.xtime_nsec <<= shift_change;
3071e75fa8bSJohn Stultz 		}
308fc6eead7SJohn Stultz 	}
3094a4ad80dSPeter Zijlstra 
310876e7881SPeter Zijlstra 	tk->tkr_mono.shift = clock->shift;
3114a4ad80dSPeter Zijlstra 	tk->tkr_raw.shift = clock->shift;
312155ec602SMartin Schwidefsky 
313f726a697SJohn Stultz 	tk->ntp_error = 0;
314f726a697SJohn Stultz 	tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
315375f45b5SJohn Stultz 	tk->ntp_tick = ntpinterval << tk->ntp_error_shift;
3160a544198SMartin Schwidefsky 
3170a544198SMartin Schwidefsky 	/*
3180a544198SMartin Schwidefsky 	 * The timekeeper keeps its own mult values for the currently
3190a544198SMartin Schwidefsky 	 * active clocksource. These value will be adjusted via NTP
3200a544198SMartin Schwidefsky 	 * to counteract clock drifting.
3210a544198SMartin Schwidefsky 	 */
322876e7881SPeter Zijlstra 	tk->tkr_mono.mult = clock->mult;
3234a4ad80dSPeter Zijlstra 	tk->tkr_raw.mult = clock->mult;
324dc491596SJohn Stultz 	tk->ntp_err_mult = 0;
32578b98e3cSMiroslav Lichvar 	tk->skip_second_overflow = 0;
326155ec602SMartin Schwidefsky }
3278524070bSjohn stultz 
3282ba2a305SMartin Schwidefsky /* Timekeeper helper functions. */
delta_to_ns_safe(const struct tk_read_base * tkr,u64 delta)329fcf190c3SAdrian Hunter static noinline u64 delta_to_ns_safe(const struct tk_read_base *tkr, u64 delta)
330fcf190c3SAdrian Hunter {
331fcf190c3SAdrian Hunter 	return mul_u64_u32_add_u64_shr(delta, tkr->mult, tkr->xtime_nsec, tkr->shift);
332fcf190c3SAdrian Hunter }
333fcf190c3SAdrian Hunter 
timekeeping_cycles_to_ns(const struct tk_read_base * tkr,u64 cycles)334985e6950SOndrej Mosnacek static inline u64 timekeeping_cycles_to_ns(const struct tk_read_base *tkr, u64 cycles)
3356bd58f09SChristopher S. Hall {
3369af4548eSAdrian Hunter 	/* Calculate the delta since the last update_wall_time() */
337e809a80aSAdrian Hunter 	u64 mask = tkr->mask, delta = (cycles - tkr->cycle_last) & mask;
338e809a80aSAdrian Hunter 
339fcf190c3SAdrian Hunter 	/*
340135225a3SAdrian Hunter 	 * This detects both negative motion and the case where the delta
341135225a3SAdrian Hunter 	 * overflows the multiplication with tkr->mult.
342fcf190c3SAdrian Hunter 	 */
343fcf190c3SAdrian Hunter 	if (unlikely(delta > tkr->clock->max_cycles)) {
344e809a80aSAdrian Hunter 		/*
345e809a80aSAdrian Hunter 		 * Handle clocksource inconsistency between CPUs to prevent
346e809a80aSAdrian Hunter 		 * time from going backwards by checking for the MSB of the
347e809a80aSAdrian Hunter 		 * mask being set in the delta.
348e809a80aSAdrian Hunter 		 */
349135225a3SAdrian Hunter 		if (delta & ~(mask >> 1))
350e809a80aSAdrian Hunter 			return tkr->xtime_nsec >> tkr->shift;
3516bd58f09SChristopher S. Hall 
352fcf190c3SAdrian Hunter 		return delta_to_ns_safe(tkr, delta);
353fcf190c3SAdrian Hunter 	}
354fcf190c3SAdrian Hunter 
3553094c6dbSAdrian Hunter 	return ((delta * tkr->mult) + tkr->xtime_nsec) >> tkr->shift;
3566bd58f09SChristopher S. Hall }
3576bd58f09SChristopher S. Hall 
timekeeping_get_ns(const struct tk_read_base * tkr)358d44d2698SThomas Gleixner static __always_inline u64 timekeeping_get_ns(const struct tk_read_base *tkr)
359e98ab3d4SAdrian Hunter {
360670be12bSAdrian Hunter 	return timekeeping_cycles_to_ns(tkr, tk_clock_read(tkr));
361e98ab3d4SAdrian Hunter }
362e98ab3d4SAdrian Hunter 
3634396e058SThomas Gleixner /**
3644396e058SThomas Gleixner  * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
365affe3e85SRafael J. Wysocki  * @tkr: Timekeeping readout base from which we take the update
366e025b031SAlex Shi  * @tkf: Pointer to NMI safe timekeeper
3674396e058SThomas Gleixner  *
3684396e058SThomas Gleixner  * We want to use this from any context including NMI and tracing /
3694396e058SThomas Gleixner  * instrumenting the timekeeping code itself.
3704396e058SThomas Gleixner  *
37193190bc3SMarco Elver  * Employ the latch technique; see @write_seqcount_latch.
3724396e058SThomas Gleixner  *
3734396e058SThomas Gleixner  * So if a NMI hits the update of base[0] then it will use base[1]
3744396e058SThomas Gleixner  * which is still consistent. In the worst case this can result is a
3754396e058SThomas Gleixner  * slightly wrong timestamp (a few nanoseconds). See
3764396e058SThomas Gleixner  * @ktime_get_mono_fast_ns.
3774396e058SThomas Gleixner  */
update_fast_timekeeper(const struct tk_read_base * tkr,struct tk_fast * tkf)378985e6950SOndrej Mosnacek static void update_fast_timekeeper(const struct tk_read_base *tkr,
379985e6950SOndrej Mosnacek 				   struct tk_fast *tkf)
3804396e058SThomas Gleixner {
3814498e746SPeter Zijlstra 	struct tk_read_base *base = tkf->base;
3824396e058SThomas Gleixner 
3834396e058SThomas Gleixner 	/* Force readers off to base[1] */
38493190bc3SMarco Elver 	write_seqcount_latch_begin(&tkf->seq);
3854396e058SThomas Gleixner 
3864396e058SThomas Gleixner 	/* Update base[0] */
387affe3e85SRafael J. Wysocki 	memcpy(base, tkr, sizeof(*base));
3884396e058SThomas Gleixner 
3894396e058SThomas Gleixner 	/* Force readers back to base[0] */
39093190bc3SMarco Elver 	write_seqcount_latch(&tkf->seq);
3914396e058SThomas Gleixner 
3924396e058SThomas Gleixner 	/* Update base[1] */
3934396e058SThomas Gleixner 	memcpy(base + 1, base, sizeof(*base));
39493190bc3SMarco Elver 
39593190bc3SMarco Elver 	write_seqcount_latch_end(&tkf->seq);
3964396e058SThomas Gleixner }
3974396e058SThomas Gleixner 
__ktime_get_fast_ns(struct tk_fast * tkf)398c1ce406eSThomas Gleixner static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf)
399c1ce406eSThomas Gleixner {
400c1ce406eSThomas Gleixner 	struct tk_read_base *tkr;
401c1ce406eSThomas Gleixner 	unsigned int seq;
402c1ce406eSThomas Gleixner 	u64 now;
403c1ce406eSThomas Gleixner 
404c1ce406eSThomas Gleixner 	do {
40593190bc3SMarco Elver 		seq = read_seqcount_latch(&tkf->seq);
406c1ce406eSThomas Gleixner 		tkr = tkf->base + (seq & 0x01);
407c1ce406eSThomas Gleixner 		now = ktime_to_ns(tkr->base);
408d44d2698SThomas Gleixner 		now += timekeeping_get_ns(tkr);
40993190bc3SMarco Elver 	} while (read_seqcount_latch_retry(&tkf->seq, seq));
410c1ce406eSThomas Gleixner 
411c1ce406eSThomas Gleixner 	return now;
412c1ce406eSThomas Gleixner }
413c1ce406eSThomas Gleixner 
4144396e058SThomas Gleixner /**
4154396e058SThomas Gleixner  * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic
4164396e058SThomas Gleixner  *
4174396e058SThomas Gleixner  * This timestamp is not guaranteed to be monotonic across an update.
4184396e058SThomas Gleixner  * The timestamp is calculated by:
4194396e058SThomas Gleixner  *
4204396e058SThomas Gleixner  *	now = base_mono + clock_delta * slope
4214396e058SThomas Gleixner  *
4224396e058SThomas Gleixner  * So if the update lowers the slope, readers who are forced to the
4234396e058SThomas Gleixner  * not yet updated second array are still using the old steeper slope.
4244396e058SThomas Gleixner  *
4254396e058SThomas Gleixner  * tmono
4264396e058SThomas Gleixner  * ^
4274396e058SThomas Gleixner  * |    o  n
4284396e058SThomas Gleixner  * |   o n
4294396e058SThomas Gleixner  * |  u
4304396e058SThomas Gleixner  * | o
4314396e058SThomas Gleixner  * |o
4324396e058SThomas Gleixner  * |12345678---> reader order
4334396e058SThomas Gleixner  *
4344396e058SThomas Gleixner  * o = old slope
4354396e058SThomas Gleixner  * u = update
4364396e058SThomas Gleixner  * n = new slope
4374396e058SThomas Gleixner  *
4384396e058SThomas Gleixner  * So reader 6 will observe time going backwards versus reader 5.
4394396e058SThomas Gleixner  *
440c1ce406eSThomas Gleixner  * While other CPUs are likely to be able to observe that, the only way
4414396e058SThomas Gleixner  * for a CPU local observation is when an NMI hits in the middle of
4424396e058SThomas Gleixner  * the update. Timestamps taken from that NMI context might be ahead
4434396e058SThomas Gleixner  * of the following timestamps. Callers need to be aware of that and
4444396e058SThomas Gleixner  * deal with it.
4454396e058SThomas Gleixner  */
ktime_get_mono_fast_ns(void)4462c33d775SKurt Kanzenbach u64 notrace ktime_get_mono_fast_ns(void)
4474498e746SPeter Zijlstra {
4484498e746SPeter Zijlstra 	return __ktime_get_fast_ns(&tk_fast_mono);
4494498e746SPeter Zijlstra }
4504396e058SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
4514396e058SThomas Gleixner 
452c1ce406eSThomas Gleixner /**
453c1ce406eSThomas Gleixner  * ktime_get_raw_fast_ns - Fast NMI safe access to clock monotonic raw
454c1ce406eSThomas Gleixner  *
455c1ce406eSThomas Gleixner  * Contrary to ktime_get_mono_fast_ns() this is always correct because the
456c1ce406eSThomas Gleixner  * conversion factor is not affected by NTP/PTP correction.
457c1ce406eSThomas Gleixner  */
ktime_get_raw_fast_ns(void)4582c33d775SKurt Kanzenbach u64 notrace ktime_get_raw_fast_ns(void)
459f09cb9a1SPeter Zijlstra {
460f09cb9a1SPeter Zijlstra 	return __ktime_get_fast_ns(&tk_fast_raw);
461f09cb9a1SPeter Zijlstra }
462f09cb9a1SPeter Zijlstra EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns);
463f09cb9a1SPeter Zijlstra 
464a3ed0e43SThomas Gleixner /**
465a3ed0e43SThomas Gleixner  * ktime_get_boot_fast_ns - NMI safe and fast access to boot clock.
466a3ed0e43SThomas Gleixner  *
467a3ed0e43SThomas Gleixner  * To keep it NMI safe since we're accessing from tracing, we're not using a
468a3ed0e43SThomas Gleixner  * separate timekeeper with updates to monotonic clock and boot offset
469025e82bcSAhmed S. Darwish  * protected with seqcounts. This has the following minor side effects:
470a3ed0e43SThomas Gleixner  *
471a3ed0e43SThomas Gleixner  * (1) Its possible that a timestamp be taken after the boot offset is updated
472a3ed0e43SThomas Gleixner  * but before the timekeeper is updated. If this happens, the new boot offset
473a3ed0e43SThomas Gleixner  * is added to the old timekeeping making the clock appear to update slightly
474a3ed0e43SThomas Gleixner  * earlier:
475a3ed0e43SThomas Gleixner  *    CPU 0                                        CPU 1
476a3ed0e43SThomas Gleixner  *    timekeeping_inject_sleeptime64()
477a3ed0e43SThomas Gleixner  *    __timekeeping_inject_sleeptime(tk, delta);
478a3ed0e43SThomas Gleixner  *                                                 timestamp();
479147ba943SAnna-Maria Behnsen  *    timekeeping_update_staged(tkd, TK_CLEAR_NTP...);
480a3ed0e43SThomas Gleixner  *
481a3ed0e43SThomas Gleixner  * (2) On 32-bit systems, the 64-bit boot offset (tk->offs_boot) may be
482a3ed0e43SThomas Gleixner  * partially updated.  Since the tk->offs_boot update is a rare event, this
483a3ed0e43SThomas Gleixner  * should be a rare occurrence which postprocessing should be able to handle.
484c1ce406eSThomas Gleixner  *
485158009f1SGeert Uytterhoeven  * The caveats vs. timestamp ordering as documented for ktime_get_mono_fast_ns()
486c1ce406eSThomas Gleixner  * apply as well.
487a3ed0e43SThomas Gleixner  */
ktime_get_boot_fast_ns(void)488a3ed0e43SThomas Gleixner u64 notrace ktime_get_boot_fast_ns(void)
489a3ed0e43SThomas Gleixner {
490a3ed0e43SThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
491a3ed0e43SThomas Gleixner 
492eff4849fSThomas Gleixner 	return (ktime_get_mono_fast_ns() + ktime_to_ns(data_race(tk->offs_boot)));
493a3ed0e43SThomas Gleixner }
494a3ed0e43SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns);
495a3ed0e43SThomas Gleixner 
4963dc6ffaeSKurt Kanzenbach /**
4973dc6ffaeSKurt Kanzenbach  * ktime_get_tai_fast_ns - NMI safe and fast access to tai clock.
4983dc6ffaeSKurt Kanzenbach  *
4993dc6ffaeSKurt Kanzenbach  * The same limitations as described for ktime_get_boot_fast_ns() apply. The
5003dc6ffaeSKurt Kanzenbach  * mono time and the TAI offset are not read atomically which may yield wrong
5013dc6ffaeSKurt Kanzenbach  * readouts. However, an update of the TAI offset is an rare event e.g., caused
5023dc6ffaeSKurt Kanzenbach  * by settime or adjtimex with an offset. The user of this function has to deal
5033dc6ffaeSKurt Kanzenbach  * with the possibility of wrong timestamps in post processing.
5043dc6ffaeSKurt Kanzenbach  */
ktime_get_tai_fast_ns(void)5053dc6ffaeSKurt Kanzenbach u64 notrace ktime_get_tai_fast_ns(void)
5063dc6ffaeSKurt Kanzenbach {
5073dc6ffaeSKurt Kanzenbach 	struct timekeeper *tk = &tk_core.timekeeper;
5083dc6ffaeSKurt Kanzenbach 
5093dc6ffaeSKurt Kanzenbach 	return (ktime_get_mono_fast_ns() + ktime_to_ns(data_race(tk->offs_tai)));
5103dc6ffaeSKurt Kanzenbach }
5113dc6ffaeSKurt Kanzenbach EXPORT_SYMBOL_GPL(ktime_get_tai_fast_ns);
5123dc6ffaeSKurt Kanzenbach 
5134c3711d7SThomas Gleixner /**
5144c3711d7SThomas Gleixner  * ktime_get_real_fast_ns: - NMI safe and fast access to clock realtime.
515c1ce406eSThomas Gleixner  *
516158009f1SGeert Uytterhoeven  * See ktime_get_mono_fast_ns() for documentation of the time stamp ordering.
5174c3711d7SThomas Gleixner  */
ktime_get_real_fast_ns(void)5184c3711d7SThomas Gleixner u64 ktime_get_real_fast_ns(void)
5194c3711d7SThomas Gleixner {
5202d2a46cfSDr. David Alan Gilbert 	struct tk_fast *tkf = &tk_fast_mono;
5212d2a46cfSDr. David Alan Gilbert 	struct tk_read_base *tkr;
5222d2a46cfSDr. David Alan Gilbert 	u64 baser, delta;
5232d2a46cfSDr. David Alan Gilbert 	unsigned int seq;
5242d2a46cfSDr. David Alan Gilbert 
5252d2a46cfSDr. David Alan Gilbert 	do {
5262d2a46cfSDr. David Alan Gilbert 		seq = raw_read_seqcount_latch(&tkf->seq);
5272d2a46cfSDr. David Alan Gilbert 		tkr = tkf->base + (seq & 0x01);
5282d2a46cfSDr. David Alan Gilbert 		baser = ktime_to_ns(tkr->base_real);
5292d2a46cfSDr. David Alan Gilbert 		delta = timekeeping_get_ns(tkr);
5302d2a46cfSDr. David Alan Gilbert 	} while (raw_read_seqcount_latch_retry(&tkf->seq, seq));
5312d2a46cfSDr. David Alan Gilbert 
5322d2a46cfSDr. David Alan Gilbert 	return baser + delta;
5334c3711d7SThomas Gleixner }
534df27067eSArnd Bergmann EXPORT_SYMBOL_GPL(ktime_get_real_fast_ns);
5354c3711d7SThomas Gleixner 
536060407aeSRafael J. Wysocki /**
537060407aeSRafael J. Wysocki  * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource.
538060407aeSRafael J. Wysocki  * @tk: Timekeeper to snapshot.
539060407aeSRafael J. Wysocki  *
540060407aeSRafael J. Wysocki  * It generally is unsafe to access the clocksource after timekeeping has been
541060407aeSRafael J. Wysocki  * suspended, so take a snapshot of the readout base of @tk and use it as the
542060407aeSRafael J. Wysocki  * fast timekeeper's readout base while suspended.  It will return the same
543060407aeSRafael J. Wysocki  * number of cycles every time until timekeeping is resumed at which time the
544060407aeSRafael J. Wysocki  * proper readout base for the fast timekeeper will be restored automatically.
545060407aeSRafael J. Wysocki  */
halt_fast_timekeeper(const struct timekeeper * tk)546985e6950SOndrej Mosnacek static void halt_fast_timekeeper(const struct timekeeper *tk)
547060407aeSRafael J. Wysocki {
548060407aeSRafael J. Wysocki 	static struct tk_read_base tkr_dummy;
549985e6950SOndrej Mosnacek 	const struct tk_read_base *tkr = &tk->tkr_mono;
550060407aeSRafael J. Wysocki 
551060407aeSRafael J. Wysocki 	memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
552ceea5e37SJohn Stultz 	cycles_at_suspend = tk_clock_read(tkr);
553ceea5e37SJohn Stultz 	tkr_dummy.clock = &dummy_clock;
5544c3711d7SThomas Gleixner 	tkr_dummy.base_real = tkr->base + tk->offs_real;
5554498e746SPeter Zijlstra 	update_fast_timekeeper(&tkr_dummy, &tk_fast_mono);
556f09cb9a1SPeter Zijlstra 
557f09cb9a1SPeter Zijlstra 	tkr = &tk->tkr_raw;
558f09cb9a1SPeter Zijlstra 	memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
559ceea5e37SJohn Stultz 	tkr_dummy.clock = &dummy_clock;
560f09cb9a1SPeter Zijlstra 	update_fast_timekeeper(&tkr_dummy, &tk_fast_raw);
561060407aeSRafael J. Wysocki }
562060407aeSRafael J. Wysocki 
563e0b306feSMarcelo Tosatti static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
564e0b306feSMarcelo Tosatti 
update_pvclock_gtod(struct timekeeper * tk,bool was_set)565780427f0SDavid Vrabel static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
566e0b306feSMarcelo Tosatti {
567780427f0SDavid Vrabel 	raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
568e0b306feSMarcelo Tosatti }
569e0b306feSMarcelo Tosatti 
570e0b306feSMarcelo Tosatti /**
571e0b306feSMarcelo Tosatti  * pvclock_gtod_register_notifier - register a pvclock timedata update listener
572f27f7c3fSAlex Shi  * @nb: Pointer to the notifier block to register
573e0b306feSMarcelo Tosatti  */
pvclock_gtod_register_notifier(struct notifier_block * nb)574e0b306feSMarcelo Tosatti int pvclock_gtod_register_notifier(struct notifier_block *nb)
575e0b306feSMarcelo Tosatti {
5763fdb14fdSThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
577e0b306feSMarcelo Tosatti 	int ret;
578e0b306feSMarcelo Tosatti 
5798c4799b1SAnna-Maria Behnsen 	guard(raw_spinlock_irqsave)(&tk_core.lock);
580e0b306feSMarcelo Tosatti 	ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
581780427f0SDavid Vrabel 	update_pvclock_gtod(tk, true);
582e0b306feSMarcelo Tosatti 
583e0b306feSMarcelo Tosatti 	return ret;
584e0b306feSMarcelo Tosatti }
585e0b306feSMarcelo Tosatti EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
586e0b306feSMarcelo Tosatti 
587e0b306feSMarcelo Tosatti /**
588e0b306feSMarcelo Tosatti  * pvclock_gtod_unregister_notifier - unregister a pvclock
589e0b306feSMarcelo Tosatti  * timedata update listener
590f27f7c3fSAlex Shi  * @nb: Pointer to the notifier block to unregister
591e0b306feSMarcelo Tosatti  */
pvclock_gtod_unregister_notifier(struct notifier_block * nb)592e0b306feSMarcelo Tosatti int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
593e0b306feSMarcelo Tosatti {
5948c4799b1SAnna-Maria Behnsen 	guard(raw_spinlock_irqsave)(&tk_core.lock);
5958c4799b1SAnna-Maria Behnsen 	return raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
596e0b306feSMarcelo Tosatti }
597e0b306feSMarcelo Tosatti EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
598e0b306feSMarcelo Tosatti 
5997c032df5SThomas Gleixner /*
600833f32d7SJohn Stultz  * tk_update_leap_state - helper to update the next_leap_ktime
601833f32d7SJohn Stultz  */
tk_update_leap_state(struct timekeeper * tk)602833f32d7SJohn Stultz static inline void tk_update_leap_state(struct timekeeper *tk)
603833f32d7SJohn Stultz {
604833f32d7SJohn Stultz 	tk->next_leap_ktime = ntp_get_next_leap();
6052456e855SThomas Gleixner 	if (tk->next_leap_ktime != KTIME_MAX)
606833f32d7SJohn Stultz 		/* Convert to monotonic time */
607833f32d7SJohn Stultz 		tk->next_leap_ktime = ktime_sub(tk->next_leap_ktime, tk->offs_real);
608833f32d7SJohn Stultz }
609833f32d7SJohn Stultz 
610833f32d7SJohn Stultz /*
611ae455cb7SAnna-Maria Behnsen  * Leap state update for both shadow and the real timekeeper
612ae455cb7SAnna-Maria Behnsen  * Separate to spare a full memcpy() of the timekeeper.
613ae455cb7SAnna-Maria Behnsen  */
tk_update_leap_state_all(struct tk_data * tkd)614ae455cb7SAnna-Maria Behnsen static void tk_update_leap_state_all(struct tk_data *tkd)
615ae455cb7SAnna-Maria Behnsen {
616ae455cb7SAnna-Maria Behnsen 	write_seqcount_begin(&tkd->seq);
617ae455cb7SAnna-Maria Behnsen 	tk_update_leap_state(&tkd->shadow_timekeeper);
618ae455cb7SAnna-Maria Behnsen 	tkd->timekeeper.next_leap_ktime = tkd->shadow_timekeeper.next_leap_ktime;
619ae455cb7SAnna-Maria Behnsen 	write_seqcount_end(&tkd->seq);
620ae455cb7SAnna-Maria Behnsen }
621ae455cb7SAnna-Maria Behnsen 
622ae455cb7SAnna-Maria Behnsen /*
6237c032df5SThomas Gleixner  * Update the ktime_t based scalar nsec members of the timekeeper
6247c032df5SThomas Gleixner  */
tk_update_ktime_data(struct timekeeper * tk)6257c032df5SThomas Gleixner static inline void tk_update_ktime_data(struct timekeeper *tk)
6267c032df5SThomas Gleixner {
6279e3680b1SHeena Sirwani 	u64 seconds;
6289e3680b1SHeena Sirwani 	u32 nsec;
6297c032df5SThomas Gleixner 
6307c032df5SThomas Gleixner 	/*
6317c032df5SThomas Gleixner 	 * The xtime based monotonic readout is:
6327c032df5SThomas Gleixner 	 *	nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();
6337c032df5SThomas Gleixner 	 * The ktime based monotonic readout is:
6347c032df5SThomas Gleixner 	 *	nsec = base_mono + now();
6357c032df5SThomas Gleixner 	 * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
6367c032df5SThomas Gleixner 	 */
6379e3680b1SHeena Sirwani 	seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
6389e3680b1SHeena Sirwani 	nsec = (u32) tk->wall_to_monotonic.tv_nsec;
639876e7881SPeter Zijlstra 	tk->tkr_mono.base = ns_to_ktime(seconds * NSEC_PER_SEC + nsec);
640f519b1a2SThomas Gleixner 
6419e3680b1SHeena Sirwani 	/*
6429e3680b1SHeena Sirwani 	 * The sum of the nanoseconds portions of xtime and
6439e3680b1SHeena Sirwani 	 * wall_to_monotonic can be greater/equal one second. Take
6449e3680b1SHeena Sirwani 	 * this into account before updating tk->ktime_sec.
6459e3680b1SHeena Sirwani 	 */
646876e7881SPeter Zijlstra 	nsec += (u32)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
6479e3680b1SHeena Sirwani 	if (nsec >= NSEC_PER_SEC)
6489e3680b1SHeena Sirwani 		seconds++;
6499e3680b1SHeena Sirwani 	tk->ktime_sec = seconds;
650fc6eead7SJohn Stultz 
651fc6eead7SJohn Stultz 	/* Update the monotonic raw base */
6520bcdc098SJohn Stultz 	tk->tkr_raw.base = ns_to_ktime(tk->raw_sec * NSEC_PER_SEC);
6537c032df5SThomas Gleixner }
6547c032df5SThomas Gleixner 
65597e53792SThomas Gleixner /*
65697e53792SThomas Gleixner  * Restore the shadow timekeeper from the real timekeeper.
65797e53792SThomas Gleixner  */
timekeeping_restore_shadow(struct tk_data * tkd)65897e53792SThomas Gleixner static void timekeeping_restore_shadow(struct tk_data *tkd)
659cc06268cSThomas Gleixner {
66097e53792SThomas Gleixner 	lockdep_assert_held(&tkd->lock);
66197e53792SThomas Gleixner 	memcpy(&tkd->shadow_timekeeper, &tkd->timekeeper, sizeof(tkd->timekeeper));
66297e53792SThomas Gleixner }
66397e53792SThomas Gleixner 
timekeeping_update_from_shadow(struct tk_data * tkd,unsigned int action)664147ba943SAnna-Maria Behnsen static void timekeeping_update_from_shadow(struct tk_data *tkd, unsigned int action)
665cc06268cSThomas Gleixner {
666147ba943SAnna-Maria Behnsen 	struct timekeeper *tk = &tk_core.shadow_timekeeper;
667147ba943SAnna-Maria Behnsen 
6681d72d7b5SAnna-Maria Behnsen 	lockdep_assert_held(&tkd->lock);
6691d72d7b5SAnna-Maria Behnsen 
670147ba943SAnna-Maria Behnsen 	/*
671147ba943SAnna-Maria Behnsen 	 * Block out readers before running the updates below because that
672147ba943SAnna-Maria Behnsen 	 * updates VDSO and other time related infrastructure. Not blocking
673147ba943SAnna-Maria Behnsen 	 * the readers might let a reader see time going backwards when
674147ba943SAnna-Maria Behnsen 	 * reading from the VDSO after the VDSO update and then reading in
675147ba943SAnna-Maria Behnsen 	 * the kernel from the timekeeper before that got updated.
676147ba943SAnna-Maria Behnsen 	 */
677147ba943SAnna-Maria Behnsen 	write_seqcount_begin(&tkd->seq);
678147ba943SAnna-Maria Behnsen 
67904397fe9SDavid Vrabel 	if (action & TK_CLEAR_NTP) {
680f726a697SJohn Stultz 		tk->ntp_error = 0;
681cc06268cSThomas Gleixner 		ntp_clear();
682cc06268cSThomas Gleixner 	}
68348cdc135SThomas Gleixner 
684833f32d7SJohn Stultz 	tk_update_leap_state(tk);
6857c032df5SThomas Gleixner 	tk_update_ktime_data(tk);
6867c032df5SThomas Gleixner 
6879bf2419fSThomas Gleixner 	update_vsyscall(tk);
6889bf2419fSThomas Gleixner 	update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
6899bf2419fSThomas Gleixner 
6904c3711d7SThomas Gleixner 	tk->tkr_mono.base_real = tk->tkr_mono.base + tk->offs_real;
6914498e746SPeter Zijlstra 	update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono);
692f09cb9a1SPeter Zijlstra 	update_fast_timekeeper(&tk->tkr_raw,  &tk_fast_raw);
693868a3e91SThomas Gleixner 
694868a3e91SThomas Gleixner 	if (action & TK_CLOCK_WAS_SET)
695868a3e91SThomas Gleixner 		tk->clock_was_set_seq++;
6965aa6c43eSAnna-Maria Behnsen 
697d1518326SJohn Stultz 	/*
6985aa6c43eSAnna-Maria Behnsen 	 * Update the real timekeeper.
6995aa6c43eSAnna-Maria Behnsen 	 *
7005aa6c43eSAnna-Maria Behnsen 	 * We could avoid this memcpy() by switching pointers, but that has
7015aa6c43eSAnna-Maria Behnsen 	 * the downside that the reader side does not longer benefit from
7025aa6c43eSAnna-Maria Behnsen 	 * the cacheline optimized data layout of the timekeeper and requires
7035aa6c43eSAnna-Maria Behnsen 	 * another indirection.
704d1518326SJohn Stultz 	 */
705147ba943SAnna-Maria Behnsen 	memcpy(&tkd->timekeeper, tk, sizeof(*tk));
7065aa6c43eSAnna-Maria Behnsen 	write_seqcount_end(&tkd->seq);
707cc06268cSThomas Gleixner }
708cc06268cSThomas Gleixner 
7098524070bSjohn stultz /**
710324a2219SThomas Gleixner  * timekeeping_forward_now - update clock to the current time
7116e5a9190SAlex Shi  * @tk:		Pointer to the timekeeper to update
7128524070bSjohn stultz  *
7139a055117SRoman Zippel  * Forward the current clock to update its state since the last call to
7149a055117SRoman Zippel  * update_wall_time(). This is useful before significant clock changes,
7159a055117SRoman Zippel  * as it avoids having to deal with this time offset explicitly.
7168524070bSjohn stultz  */
timekeeping_forward_now(struct timekeeper * tk)717324a2219SThomas Gleixner static void timekeeping_forward_now(struct timekeeper *tk)
7188524070bSjohn stultz {
719324a2219SThomas Gleixner 	u64 cycle_now, delta;
720757b000fSJohn Stultz 
721324a2219SThomas Gleixner 	cycle_now = tk_clock_read(&tk->tkr_mono);
722324a2219SThomas Gleixner 	delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask,
723324a2219SThomas Gleixner 				  tk->tkr_mono.clock->max_raw_delta);
724876e7881SPeter Zijlstra 	tk->tkr_mono.cycle_last = cycle_now;
7254a4ad80dSPeter Zijlstra 	tk->tkr_raw.cycle_last  = cycle_now;
7268524070bSjohn stultz 
727fcf190c3SAdrian Hunter 	while (delta > 0) {
728fcf190c3SAdrian Hunter 		u64 max = tk->tkr_mono.clock->max_cycles;
729fcf190c3SAdrian Hunter 		u64 incr = delta < max ? delta : max;
730fc6eead7SJohn Stultz 
731fcf190c3SAdrian Hunter 		tk->tkr_mono.xtime_nsec += incr * tk->tkr_mono.mult;
732fcf190c3SAdrian Hunter 		tk->tkr_raw.xtime_nsec += incr * tk->tkr_raw.mult;
733fc6eead7SJohn Stultz 		tk_normalize_xtime(tk);
734fcf190c3SAdrian Hunter 		delta -= incr;
735fcf190c3SAdrian Hunter 	}
736*b71f9804SThomas Gleixner 	tk_update_coarse_nsecs(tk);
7378524070bSjohn stultz }
7388524070bSjohn stultz 
7398524070bSjohn stultz /**
740edca71feSArnd Bergmann  * ktime_get_real_ts64 - Returns the time of day in a timespec64.
7418524070bSjohn stultz  * @ts:		pointer to the timespec to be set
7428524070bSjohn stultz  *
743edca71feSArnd Bergmann  * Returns the time of day in a timespec64 (WARN if suspended).
7448524070bSjohn stultz  */
ktime_get_real_ts64(struct timespec64 * ts)745edca71feSArnd Bergmann void ktime_get_real_ts64(struct timespec64 *ts)
7468524070bSjohn stultz {
7473fdb14fdSThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
748e1e41b6cSRasmus Villemoes 	unsigned int seq;
749acc89612SThomas Gleixner 	u64 nsecs;
7508524070bSjohn stultz 
751edca71feSArnd Bergmann 	WARN_ON(timekeeping_suspended);
752edca71feSArnd Bergmann 
7538524070bSjohn stultz 	do {
7543fdb14fdSThomas Gleixner 		seq = read_seqcount_begin(&tk_core.seq);
7558524070bSjohn stultz 
7564e250fddSJohn Stultz 		ts->tv_sec = tk->xtime_sec;
757876e7881SPeter Zijlstra 		nsecs = timekeeping_get_ns(&tk->tkr_mono);
7588524070bSjohn stultz 
7593fdb14fdSThomas Gleixner 	} while (read_seqcount_retry(&tk_core.seq, seq));
7608524070bSjohn stultz 
761ec145babSJohn Stultz 	ts->tv_nsec = 0;
762d6d29896SThomas Gleixner 	timespec64_add_ns(ts, nsecs);
7631e817fb6SKees Cook }
764edca71feSArnd Bergmann EXPORT_SYMBOL(ktime_get_real_ts64);
7658524070bSjohn stultz 
ktime_get(void)766951ed4d3SMartin Schwidefsky ktime_t ktime_get(void)
767951ed4d3SMartin Schwidefsky {
7683fdb14fdSThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
769951ed4d3SMartin Schwidefsky 	unsigned int seq;
770a016a5bdSThomas Gleixner 	ktime_t base;
771acc89612SThomas Gleixner 	u64 nsecs;
772951ed4d3SMartin Schwidefsky 
773951ed4d3SMartin Schwidefsky 	WARN_ON(timekeeping_suspended);
774951ed4d3SMartin Schwidefsky 
775951ed4d3SMartin Schwidefsky 	do {
7763fdb14fdSThomas Gleixner 		seq = read_seqcount_begin(&tk_core.seq);
777876e7881SPeter Zijlstra 		base = tk->tkr_mono.base;
778876e7881SPeter Zijlstra 		nsecs = timekeeping_get_ns(&tk->tkr_mono);
779951ed4d3SMartin Schwidefsky 
7803fdb14fdSThomas Gleixner 	} while (read_seqcount_retry(&tk_core.seq, seq));
78124e4a8c3SJohn Stultz 
782a016a5bdSThomas Gleixner 	return ktime_add_ns(base, nsecs);
783951ed4d3SMartin Schwidefsky }
784951ed4d3SMartin Schwidefsky EXPORT_SYMBOL_GPL(ktime_get);
785951ed4d3SMartin Schwidefsky 
ktime_get_resolution_ns(void)7866374f912SHarald Geyer u32 ktime_get_resolution_ns(void)
7876374f912SHarald Geyer {
7886374f912SHarald Geyer 	struct timekeeper *tk = &tk_core.timekeeper;
7896374f912SHarald Geyer 	unsigned int seq;
7906374f912SHarald Geyer 	u32 nsecs;
7916374f912SHarald Geyer 
7926374f912SHarald Geyer 	WARN_ON(timekeeping_suspended);
7936374f912SHarald Geyer 
7946374f912SHarald Geyer 	do {
7956374f912SHarald Geyer 		seq = read_seqcount_begin(&tk_core.seq);
7966374f912SHarald Geyer 		nsecs = tk->tkr_mono.mult >> tk->tkr_mono.shift;
7976374f912SHarald Geyer 	} while (read_seqcount_retry(&tk_core.seq, seq));
7986374f912SHarald Geyer 
7996374f912SHarald Geyer 	return nsecs;
8006374f912SHarald Geyer }
8016374f912SHarald Geyer EXPORT_SYMBOL_GPL(ktime_get_resolution_ns);
8026374f912SHarald Geyer 
8030077dc60SThomas Gleixner static ktime_t *offsets[TK_OFFS_MAX] = {
8040077dc60SThomas Gleixner 	[TK_OFFS_REAL]	= &tk_core.timekeeper.offs_real,
805a3ed0e43SThomas Gleixner 	[TK_OFFS_BOOT]	= &tk_core.timekeeper.offs_boot,
8060077dc60SThomas Gleixner 	[TK_OFFS_TAI]	= &tk_core.timekeeper.offs_tai,
8070077dc60SThomas Gleixner };
8080077dc60SThomas Gleixner 
ktime_get_with_offset(enum tk_offsets offs)8090077dc60SThomas Gleixner ktime_t ktime_get_with_offset(enum tk_offsets offs)
8100077dc60SThomas Gleixner {
8110077dc60SThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
8120077dc60SThomas Gleixner 	unsigned int seq;
8130077dc60SThomas Gleixner 	ktime_t base, *offset = offsets[offs];
814acc89612SThomas Gleixner 	u64 nsecs;
8150077dc60SThomas Gleixner 
8160077dc60SThomas Gleixner 	WARN_ON(timekeeping_suspended);
8170077dc60SThomas Gleixner 
8180077dc60SThomas Gleixner 	do {
8190077dc60SThomas Gleixner 		seq = read_seqcount_begin(&tk_core.seq);
820876e7881SPeter Zijlstra 		base = ktime_add(tk->tkr_mono.base, *offset);
821876e7881SPeter Zijlstra 		nsecs = timekeeping_get_ns(&tk->tkr_mono);
8220077dc60SThomas Gleixner 
8230077dc60SThomas Gleixner 	} while (read_seqcount_retry(&tk_core.seq, seq));
8240077dc60SThomas Gleixner 
8250077dc60SThomas Gleixner 	return ktime_add_ns(base, nsecs);
8260077dc60SThomas Gleixner 
8270077dc60SThomas Gleixner }
8280077dc60SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_get_with_offset);
8290077dc60SThomas Gleixner 
ktime_get_coarse_with_offset(enum tk_offsets offs)830b9ff604cSArnd Bergmann ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs)
831b9ff604cSArnd Bergmann {
832b9ff604cSArnd Bergmann 	struct timekeeper *tk = &tk_core.timekeeper;
833b9ff604cSArnd Bergmann 	ktime_t base, *offset = offsets[offs];
834*b71f9804SThomas Gleixner 	unsigned int seq;
835e3ff9c36SThomas Gleixner 	u64 nsecs;
836b9ff604cSArnd Bergmann 
837b9ff604cSArnd Bergmann 	WARN_ON(timekeeping_suspended);
838b9ff604cSArnd Bergmann 
839b9ff604cSArnd Bergmann 	do {
840b9ff604cSArnd Bergmann 		seq = read_seqcount_begin(&tk_core.seq);
841b9ff604cSArnd Bergmann 		base = ktime_add(tk->tkr_mono.base, *offset);
842*b71f9804SThomas Gleixner 		nsecs = tk->coarse_nsec;
843b9ff604cSArnd Bergmann 
844b9ff604cSArnd Bergmann 	} while (read_seqcount_retry(&tk_core.seq, seq));
845b9ff604cSArnd Bergmann 
8460354c1a3SJason A. Donenfeld 	return ktime_add_ns(base, nsecs);
847b9ff604cSArnd Bergmann }
848b9ff604cSArnd Bergmann EXPORT_SYMBOL_GPL(ktime_get_coarse_with_offset);
849b9ff604cSArnd Bergmann 
850951ed4d3SMartin Schwidefsky /**
8514bf07f65SIngo Molnar  * ktime_mono_to_any() - convert monotonic time to any other time
8529a6b5197SThomas Gleixner  * @tmono:	time to convert.
8539a6b5197SThomas Gleixner  * @offs:	which offset to use
8549a6b5197SThomas Gleixner  */
ktime_mono_to_any(ktime_t tmono,enum tk_offsets offs)8559a6b5197SThomas Gleixner ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
8569a6b5197SThomas Gleixner {
8579a6b5197SThomas Gleixner 	ktime_t *offset = offsets[offs];
858e1e41b6cSRasmus Villemoes 	unsigned int seq;
8599a6b5197SThomas Gleixner 	ktime_t tconv;
8609a6b5197SThomas Gleixner 
8618c111f1bSJeff Layton 	if (IS_ENABLED(CONFIG_64BIT)) {
8628c111f1bSJeff Layton 		/*
8638c111f1bSJeff Layton 		 * Paired with WRITE_ONCE()s in tk_set_wall_to_mono() and
8648c111f1bSJeff Layton 		 * tk_update_sleep_time().
8658c111f1bSJeff Layton 		 */
8668c111f1bSJeff Layton 		return ktime_add(tmono, READ_ONCE(*offset));
8678c111f1bSJeff Layton 	}
8688c111f1bSJeff Layton 
8699a6b5197SThomas Gleixner 	do {
8709a6b5197SThomas Gleixner 		seq = read_seqcount_begin(&tk_core.seq);
8719a6b5197SThomas Gleixner 		tconv = ktime_add(tmono, *offset);
8729a6b5197SThomas Gleixner 	} while (read_seqcount_retry(&tk_core.seq, seq));
8739a6b5197SThomas Gleixner 
8749a6b5197SThomas Gleixner 	return tconv;
8759a6b5197SThomas Gleixner }
8769a6b5197SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_mono_to_any);
8779a6b5197SThomas Gleixner 
8789a6b5197SThomas Gleixner /**
879f519b1a2SThomas Gleixner  * ktime_get_raw - Returns the raw monotonic time in ktime_t format
880f519b1a2SThomas Gleixner  */
ktime_get_raw(void)881f519b1a2SThomas Gleixner ktime_t ktime_get_raw(void)
882f519b1a2SThomas Gleixner {
883f519b1a2SThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
884f519b1a2SThomas Gleixner 	unsigned int seq;
885f519b1a2SThomas Gleixner 	ktime_t base;
886acc89612SThomas Gleixner 	u64 nsecs;
887f519b1a2SThomas Gleixner 
888f519b1a2SThomas Gleixner 	do {
889f519b1a2SThomas Gleixner 		seq = read_seqcount_begin(&tk_core.seq);
8904a4ad80dSPeter Zijlstra 		base = tk->tkr_raw.base;
8914a4ad80dSPeter Zijlstra 		nsecs = timekeeping_get_ns(&tk->tkr_raw);
892f519b1a2SThomas Gleixner 
893f519b1a2SThomas Gleixner 	} while (read_seqcount_retry(&tk_core.seq, seq));
894f519b1a2SThomas Gleixner 
895f519b1a2SThomas Gleixner 	return ktime_add_ns(base, nsecs);
896f519b1a2SThomas Gleixner }
897f519b1a2SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_get_raw);
898f519b1a2SThomas Gleixner 
899f519b1a2SThomas Gleixner /**
900d6d29896SThomas Gleixner  * ktime_get_ts64 - get the monotonic clock in timespec64 format
901951ed4d3SMartin Schwidefsky  * @ts:		pointer to timespec variable
902951ed4d3SMartin Schwidefsky  *
903951ed4d3SMartin Schwidefsky  * The function calculates the monotonic clock from the realtime
904951ed4d3SMartin Schwidefsky  * clock and the wall_to_monotonic offset and stores the result
9055322e4c2SJohn Stultz  * in normalized timespec64 format in the variable pointed to by @ts.
906951ed4d3SMartin Schwidefsky  */
ktime_get_ts64(struct timespec64 * ts)907d6d29896SThomas Gleixner void ktime_get_ts64(struct timespec64 *ts)
908951ed4d3SMartin Schwidefsky {
9093fdb14fdSThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
910d6d29896SThomas Gleixner 	struct timespec64 tomono;
911951ed4d3SMartin Schwidefsky 	unsigned int seq;
912acc89612SThomas Gleixner 	u64 nsec;
913951ed4d3SMartin Schwidefsky 
914951ed4d3SMartin Schwidefsky 	WARN_ON(timekeeping_suspended);
915951ed4d3SMartin Schwidefsky 
916951ed4d3SMartin Schwidefsky 	do {
9173fdb14fdSThomas Gleixner 		seq = read_seqcount_begin(&tk_core.seq);
918d6d29896SThomas Gleixner 		ts->tv_sec = tk->xtime_sec;
919876e7881SPeter Zijlstra 		nsec = timekeeping_get_ns(&tk->tkr_mono);
9204e250fddSJohn Stultz 		tomono = tk->wall_to_monotonic;
921951ed4d3SMartin Schwidefsky 
9223fdb14fdSThomas Gleixner 	} while (read_seqcount_retry(&tk_core.seq, seq));
923951ed4d3SMartin Schwidefsky 
924d6d29896SThomas Gleixner 	ts->tv_sec += tomono.tv_sec;
925d6d29896SThomas Gleixner 	ts->tv_nsec = 0;
926d6d29896SThomas Gleixner 	timespec64_add_ns(ts, nsec + tomono.tv_nsec);
927951ed4d3SMartin Schwidefsky }
928d6d29896SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_get_ts64);
929951ed4d3SMartin Schwidefsky 
9309e3680b1SHeena Sirwani /**
9319e3680b1SHeena Sirwani  * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC
9329e3680b1SHeena Sirwani  *
9339e3680b1SHeena Sirwani  * Returns the seconds portion of CLOCK_MONOTONIC with a single non
9349e3680b1SHeena Sirwani  * serialized read. tk->ktime_sec is of type 'unsigned long' so this
9359e3680b1SHeena Sirwani  * works on both 32 and 64 bit systems. On 32 bit systems the readout
9369e3680b1SHeena Sirwani  * covers ~136 years of uptime which should be enough to prevent
9379e3680b1SHeena Sirwani  * premature wrap arounds.
9389e3680b1SHeena Sirwani  */
ktime_get_seconds(void)9399e3680b1SHeena Sirwani time64_t ktime_get_seconds(void)
9409e3680b1SHeena Sirwani {
9419e3680b1SHeena Sirwani 	struct timekeeper *tk = &tk_core.timekeeper;
9429e3680b1SHeena Sirwani 
9439e3680b1SHeena Sirwani 	WARN_ON(timekeeping_suspended);
9449e3680b1SHeena Sirwani 	return tk->ktime_sec;
9459e3680b1SHeena Sirwani }
9469e3680b1SHeena Sirwani EXPORT_SYMBOL_GPL(ktime_get_seconds);
9479e3680b1SHeena Sirwani 
948dbe7aa62SHeena Sirwani /**
949dbe7aa62SHeena Sirwani  * ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME
950dbe7aa62SHeena Sirwani  *
951aba428a0SChunguang Xu  * Returns the wall clock seconds since 1970.
952dbe7aa62SHeena Sirwani  *
953dbe7aa62SHeena Sirwani  * For 64bit systems the fast access to tk->xtime_sec is preserved. On
954dbe7aa62SHeena Sirwani  * 32bit systems the access must be protected with the sequence
955dbe7aa62SHeena Sirwani  * counter to provide "atomic" access to the 64bit tk->xtime_sec
956dbe7aa62SHeena Sirwani  * value.
957dbe7aa62SHeena Sirwani  */
ktime_get_real_seconds(void)958dbe7aa62SHeena Sirwani time64_t ktime_get_real_seconds(void)
959dbe7aa62SHeena Sirwani {
960dbe7aa62SHeena Sirwani 	struct timekeeper *tk = &tk_core.timekeeper;
961dbe7aa62SHeena Sirwani 	time64_t seconds;
962dbe7aa62SHeena Sirwani 	unsigned int seq;
963dbe7aa62SHeena Sirwani 
964dbe7aa62SHeena Sirwani 	if (IS_ENABLED(CONFIG_64BIT))
965dbe7aa62SHeena Sirwani 		return tk->xtime_sec;
966dbe7aa62SHeena Sirwani 
967dbe7aa62SHeena Sirwani 	do {
968dbe7aa62SHeena Sirwani 		seq = read_seqcount_begin(&tk_core.seq);
969dbe7aa62SHeena Sirwani 		seconds = tk->xtime_sec;
970dbe7aa62SHeena Sirwani 
971dbe7aa62SHeena Sirwani 	} while (read_seqcount_retry(&tk_core.seq, seq));
972dbe7aa62SHeena Sirwani 
973dbe7aa62SHeena Sirwani 	return seconds;
974dbe7aa62SHeena Sirwani }
975dbe7aa62SHeena Sirwani EXPORT_SYMBOL_GPL(ktime_get_real_seconds);
976dbe7aa62SHeena Sirwani 
977dee36654SDengChao /**
978dee36654SDengChao  * __ktime_get_real_seconds - The same as ktime_get_real_seconds
979dee36654SDengChao  * but without the sequence counter protect. This internal function
980dee36654SDengChao  * is called just when timekeeping lock is already held.
981dee36654SDengChao  */
__ktime_get_real_seconds(void)982865d3a9aSThomas Gleixner noinstr time64_t __ktime_get_real_seconds(void)
983dee36654SDengChao {
984dee36654SDengChao 	struct timekeeper *tk = &tk_core.timekeeper;
985dee36654SDengChao 
986dee36654SDengChao 	return tk->xtime_sec;
987dee36654SDengChao }
988dee36654SDengChao 
9899da0f49cSChristopher S. Hall /**
9909da0f49cSChristopher S. Hall  * ktime_get_snapshot - snapshots the realtime/monotonic raw clocks with counter
9919da0f49cSChristopher S. Hall  * @systime_snapshot:	pointer to struct receiving the system time snapshot
9929da0f49cSChristopher S. Hall  */
ktime_get_snapshot(struct system_time_snapshot * systime_snapshot)9939da0f49cSChristopher S. Hall void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
9949da0f49cSChristopher S. Hall {
9959da0f49cSChristopher S. Hall 	struct timekeeper *tk = &tk_core.timekeeper;
996e1e41b6cSRasmus Villemoes 	unsigned int seq;
9979da0f49cSChristopher S. Hall 	ktime_t base_raw;
9989da0f49cSChristopher S. Hall 	ktime_t base_real;
9998102c4daSVincent Donnefort 	ktime_t base_boot;
1000acc89612SThomas Gleixner 	u64 nsec_raw;
1001acc89612SThomas Gleixner 	u64 nsec_real;
1002a5a1d1c2SThomas Gleixner 	u64 now;
10039da0f49cSChristopher S. Hall 
1004ba26621eSChristopher S. Hall 	WARN_ON_ONCE(timekeeping_suspended);
1005ba26621eSChristopher S. Hall 
10069da0f49cSChristopher S. Hall 	do {
10079da0f49cSChristopher S. Hall 		seq = read_seqcount_begin(&tk_core.seq);
1008ceea5e37SJohn Stultz 		now = tk_clock_read(&tk->tkr_mono);
1009b2c67cbeSThomas Gleixner 		systime_snapshot->cs_id = tk->tkr_mono.clock->id;
10102c756febSChristopher S. Hall 		systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq;
10112c756febSChristopher S. Hall 		systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq;
10129da0f49cSChristopher S. Hall 		base_real = ktime_add(tk->tkr_mono.base,
10139da0f49cSChristopher S. Hall 				      tk_core.timekeeper.offs_real);
10148102c4daSVincent Donnefort 		base_boot = ktime_add(tk->tkr_mono.base,
10158102c4daSVincent Donnefort 				      tk_core.timekeeper.offs_boot);
10169da0f49cSChristopher S. Hall 		base_raw = tk->tkr_raw.base;
10179da0f49cSChristopher S. Hall 		nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, now);
10189da0f49cSChristopher S. Hall 		nsec_raw  = timekeeping_cycles_to_ns(&tk->tkr_raw, now);
10199da0f49cSChristopher S. Hall 	} while (read_seqcount_retry(&tk_core.seq, seq));
10209da0f49cSChristopher S. Hall 
10219da0f49cSChristopher S. Hall 	systime_snapshot->cycles = now;
10229da0f49cSChristopher S. Hall 	systime_snapshot->real = ktime_add_ns(base_real, nsec_real);
10238102c4daSVincent Donnefort 	systime_snapshot->boot = ktime_add_ns(base_boot, nsec_real);
10249da0f49cSChristopher S. Hall 	systime_snapshot->raw = ktime_add_ns(base_raw, nsec_raw);
10259da0f49cSChristopher S. Hall }
10269da0f49cSChristopher S. Hall EXPORT_SYMBOL_GPL(ktime_get_snapshot);
1027dee36654SDengChao 
10282c756febSChristopher S. Hall /* Scale base by mult/div checking for overflow */
scale64_check_overflow(u64 mult,u64 div,u64 * base)10292c756febSChristopher S. Hall static int scale64_check_overflow(u64 mult, u64 div, u64 *base)
10302c756febSChristopher S. Hall {
10312c756febSChristopher S. Hall 	u64 tmp, rem;
10322c756febSChristopher S. Hall 
10332c756febSChristopher S. Hall 	tmp = div64_u64_rem(*base, div, &rem);
10342c756febSChristopher S. Hall 
10352c756febSChristopher S. Hall 	if (((int)sizeof(u64)*8 - fls64(mult) < fls64(tmp)) ||
10362c756febSChristopher S. Hall 	    ((int)sizeof(u64)*8 - fls64(mult) < fls64(rem)))
10372c756febSChristopher S. Hall 		return -EOVERFLOW;
10382c756febSChristopher S. Hall 	tmp *= mult;
10392c756febSChristopher S. Hall 
10404cbbc3a0SWen Yang 	rem = div64_u64(rem * mult, div);
10412c756febSChristopher S. Hall 	*base = tmp + rem;
10422c756febSChristopher S. Hall 	return 0;
10432c756febSChristopher S. Hall }
10442c756febSChristopher S. Hall 
10452c756febSChristopher S. Hall /**
10462c756febSChristopher S. Hall  * adjust_historical_crosststamp - adjust crosstimestamp previous to current interval
10472c756febSChristopher S. Hall  * @history:			Snapshot representing start of history
10482c756febSChristopher S. Hall  * @partial_history_cycles:	Cycle offset into history (fractional part)
10492c756febSChristopher S. Hall  * @total_history_cycles:	Total history length in cycles
10502c756febSChristopher S. Hall  * @discontinuity:		True indicates clock was set on history period
10512c756febSChristopher S. Hall  * @ts:				Cross timestamp that should be adjusted using
10522c756febSChristopher S. Hall  *	partial/total ratio
10532c756febSChristopher S. Hall  *
10542c756febSChristopher S. Hall  * Helper function used by get_device_system_crosststamp() to correct the
10552c756febSChristopher S. Hall  * crosstimestamp corresponding to the start of the current interval to the
10562c756febSChristopher S. Hall  * system counter value (timestamp point) provided by the driver. The
10572c756febSChristopher S. Hall  * total_history_* quantities are the total history starting at the provided
10582c756febSChristopher S. Hall  * reference point and ending at the start of the current interval. The cycle
10592c756febSChristopher S. Hall  * count between the driver timestamp point and the start of the current
10602c756febSChristopher S. Hall  * interval is partial_history_cycles.
10612c756febSChristopher S. Hall  */
adjust_historical_crosststamp(struct system_time_snapshot * history,u64 partial_history_cycles,u64 total_history_cycles,bool discontinuity,struct system_device_crosststamp * ts)10622c756febSChristopher S. Hall static int adjust_historical_crosststamp(struct system_time_snapshot *history,
1063a5a1d1c2SThomas Gleixner 					 u64 partial_history_cycles,
1064a5a1d1c2SThomas Gleixner 					 u64 total_history_cycles,
10652c756febSChristopher S. Hall 					 bool discontinuity,
10662c756febSChristopher S. Hall 					 struct system_device_crosststamp *ts)
10672c756febSChristopher S. Hall {
10682c756febSChristopher S. Hall 	struct timekeeper *tk = &tk_core.timekeeper;
10692c756febSChristopher S. Hall 	u64 corr_raw, corr_real;
10702c756febSChristopher S. Hall 	bool interp_forward;
10712c756febSChristopher S. Hall 	int ret;
10722c756febSChristopher S. Hall 
10732c756febSChristopher S. Hall 	if (total_history_cycles == 0 || partial_history_cycles == 0)
10742c756febSChristopher S. Hall 		return 0;
10752c756febSChristopher S. Hall 
10762c756febSChristopher S. Hall 	/* Interpolate shortest distance from beginning or end of history */
10775fc63f95SNicholas Mc Guire 	interp_forward = partial_history_cycles > total_history_cycles / 2;
10782c756febSChristopher S. Hall 	partial_history_cycles = interp_forward ?
10792c756febSChristopher S. Hall 		total_history_cycles - partial_history_cycles :
10802c756febSChristopher S. Hall 		partial_history_cycles;
10812c756febSChristopher S. Hall 
10822c756febSChristopher S. Hall 	/*
10832c756febSChristopher S. Hall 	 * Scale the monotonic raw time delta by:
10842c756febSChristopher S. Hall 	 *	partial_history_cycles / total_history_cycles
10852c756febSChristopher S. Hall 	 */
10862c756febSChristopher S. Hall 	corr_raw = (u64)ktime_to_ns(
10872c756febSChristopher S. Hall 		ktime_sub(ts->sys_monoraw, history->raw));
10882c756febSChristopher S. Hall 	ret = scale64_check_overflow(partial_history_cycles,
10892c756febSChristopher S. Hall 				     total_history_cycles, &corr_raw);
10902c756febSChristopher S. Hall 	if (ret)
10912c756febSChristopher S. Hall 		return ret;
10922c756febSChristopher S. Hall 
10932c756febSChristopher S. Hall 	/*
10942c756febSChristopher S. Hall 	 * If there is a discontinuity in the history, scale monotonic raw
10952c756febSChristopher S. Hall 	 *	correction by:
10962c756febSChristopher S. Hall 	 *	mult(real)/mult(raw) yielding the realtime correction
10972c756febSChristopher S. Hall 	 * Otherwise, calculate the realtime correction similar to monotonic
10982c756febSChristopher S. Hall 	 *	raw calculation
10992c756febSChristopher S. Hall 	 */
11002c756febSChristopher S. Hall 	if (discontinuity) {
11012c756febSChristopher S. Hall 		corr_real = mul_u64_u32_div
11022c756febSChristopher S. Hall 			(corr_raw, tk->tkr_mono.mult, tk->tkr_raw.mult);
11032c756febSChristopher S. Hall 	} else {
11042c756febSChristopher S. Hall 		corr_real = (u64)ktime_to_ns(
11052c756febSChristopher S. Hall 			ktime_sub(ts->sys_realtime, history->real));
11062c756febSChristopher S. Hall 		ret = scale64_check_overflow(partial_history_cycles,
11072c756febSChristopher S. Hall 					     total_history_cycles, &corr_real);
11082c756febSChristopher S. Hall 		if (ret)
11092c756febSChristopher S. Hall 			return ret;
11102c756febSChristopher S. Hall 	}
11112c756febSChristopher S. Hall 
11122c756febSChristopher S. Hall 	/* Fixup monotonic raw and real time time values */
11132c756febSChristopher S. Hall 	if (interp_forward) {
11142c756febSChristopher S. Hall 		ts->sys_monoraw = ktime_add_ns(history->raw, corr_raw);
11152c756febSChristopher S. Hall 		ts->sys_realtime = ktime_add_ns(history->real, corr_real);
11162c756febSChristopher S. Hall 	} else {
11172c756febSChristopher S. Hall 		ts->sys_monoraw = ktime_sub_ns(ts->sys_monoraw, corr_raw);
11182c756febSChristopher S. Hall 		ts->sys_realtime = ktime_sub_ns(ts->sys_realtime, corr_real);
11192c756febSChristopher S. Hall 	}
11202c756febSChristopher S. Hall 
11212c756febSChristopher S. Hall 	return 0;
11222c756febSChristopher S. Hall }
11232c756febSChristopher S. Hall 
11242c756febSChristopher S. Hall /*
112587a41130SPeter Hilber  * timestamp_in_interval - true if ts is chronologically in [start, end]
112687a41130SPeter Hilber  *
112787a41130SPeter Hilber  * True if ts occurs chronologically at or after start, and before or at end.
11282c756febSChristopher S. Hall  */
timestamp_in_interval(u64 start,u64 end,u64 ts)112987a41130SPeter Hilber static bool timestamp_in_interval(u64 start, u64 end, u64 ts)
11302c756febSChristopher S. Hall {
113187a41130SPeter Hilber 	if (ts >= start && ts <= end)
11322c756febSChristopher S. Hall 		return true;
113387a41130SPeter Hilber 	if (start > end && (ts >= start || ts <= end))
11342c756febSChristopher S. Hall 		return true;
11352c756febSChristopher S. Hall 	return false;
11362c756febSChristopher S. Hall }
11372c756febSChristopher S. Hall 
convert_clock(u64 * val,u32 numerator,u32 denominator)11386b2e2997SLakshmi Sowjanya D static bool convert_clock(u64 *val, u32 numerator, u32 denominator)
11396b2e2997SLakshmi Sowjanya D {
11406b2e2997SLakshmi Sowjanya D 	u64 rem, res;
11416b2e2997SLakshmi Sowjanya D 
11426b2e2997SLakshmi Sowjanya D 	if (!numerator || !denominator)
11436b2e2997SLakshmi Sowjanya D 		return false;
11446b2e2997SLakshmi Sowjanya D 
11456b2e2997SLakshmi Sowjanya D 	res = div64_u64_rem(*val, denominator, &rem) * numerator;
11466b2e2997SLakshmi Sowjanya D 	*val = res + div_u64(rem * numerator, denominator);
11476b2e2997SLakshmi Sowjanya D 	return true;
11486b2e2997SLakshmi Sowjanya D }
11496b2e2997SLakshmi Sowjanya D 
convert_base_to_cs(struct system_counterval_t * scv)11506b2e2997SLakshmi Sowjanya D static bool convert_base_to_cs(struct system_counterval_t *scv)
11516b2e2997SLakshmi Sowjanya D {
11526b2e2997SLakshmi Sowjanya D 	struct clocksource *cs = tk_core.timekeeper.tkr_mono.clock;
11536b2e2997SLakshmi Sowjanya D 	struct clocksource_base *base;
11546b2e2997SLakshmi Sowjanya D 	u32 num, den;
11556b2e2997SLakshmi Sowjanya D 
11566b2e2997SLakshmi Sowjanya D 	/* The timestamp was taken from the time keeper clock source */
11576b2e2997SLakshmi Sowjanya D 	if (cs->id == scv->cs_id)
11586b2e2997SLakshmi Sowjanya D 		return true;
11596b2e2997SLakshmi Sowjanya D 
11606b2e2997SLakshmi Sowjanya D 	/*
11616b2e2997SLakshmi Sowjanya D 	 * Check whether cs_id matches the base clock. Prevent the compiler from
11626b2e2997SLakshmi Sowjanya D 	 * re-evaluating @base as the clocksource might change concurrently.
11636b2e2997SLakshmi Sowjanya D 	 */
11646b2e2997SLakshmi Sowjanya D 	base = READ_ONCE(cs->base);
11656b2e2997SLakshmi Sowjanya D 	if (!base || base->id != scv->cs_id)
11666b2e2997SLakshmi Sowjanya D 		return false;
11676b2e2997SLakshmi Sowjanya D 
11686b2e2997SLakshmi Sowjanya D 	num = scv->use_nsecs ? cs->freq_khz : base->numerator;
11696b2e2997SLakshmi Sowjanya D 	den = scv->use_nsecs ? USEC_PER_SEC : base->denominator;
11706b2e2997SLakshmi Sowjanya D 
11716b2e2997SLakshmi Sowjanya D 	if (!convert_clock(&scv->cycles, num, den))
11726b2e2997SLakshmi Sowjanya D 		return false;
11736b2e2997SLakshmi Sowjanya D 
11746b2e2997SLakshmi Sowjanya D 	scv->cycles += base->offset;
11756b2e2997SLakshmi Sowjanya D 	return true;
11766b2e2997SLakshmi Sowjanya D }
11776b2e2997SLakshmi Sowjanya D 
convert_cs_to_base(u64 * cycles,enum clocksource_ids base_id)117802ecee07SLakshmi Sowjanya D static bool convert_cs_to_base(u64 *cycles, enum clocksource_ids base_id)
117902ecee07SLakshmi Sowjanya D {
118002ecee07SLakshmi Sowjanya D 	struct clocksource *cs = tk_core.timekeeper.tkr_mono.clock;
118102ecee07SLakshmi Sowjanya D 	struct clocksource_base *base;
118202ecee07SLakshmi Sowjanya D 
118302ecee07SLakshmi Sowjanya D 	/*
118402ecee07SLakshmi Sowjanya D 	 * Check whether base_id matches the base clock. Prevent the compiler from
118502ecee07SLakshmi Sowjanya D 	 * re-evaluating @base as the clocksource might change concurrently.
118602ecee07SLakshmi Sowjanya D 	 */
118702ecee07SLakshmi Sowjanya D 	base = READ_ONCE(cs->base);
118802ecee07SLakshmi Sowjanya D 	if (!base || base->id != base_id)
118902ecee07SLakshmi Sowjanya D 		return false;
119002ecee07SLakshmi Sowjanya D 
119102ecee07SLakshmi Sowjanya D 	*cycles -= base->offset;
119202ecee07SLakshmi Sowjanya D 	if (!convert_clock(cycles, base->denominator, base->numerator))
119302ecee07SLakshmi Sowjanya D 		return false;
119402ecee07SLakshmi Sowjanya D 	return true;
119502ecee07SLakshmi Sowjanya D }
119602ecee07SLakshmi Sowjanya D 
convert_ns_to_cs(u64 * delta)119702ecee07SLakshmi Sowjanya D static bool convert_ns_to_cs(u64 *delta)
119802ecee07SLakshmi Sowjanya D {
119902ecee07SLakshmi Sowjanya D 	struct tk_read_base *tkr = &tk_core.timekeeper.tkr_mono;
120002ecee07SLakshmi Sowjanya D 
120102ecee07SLakshmi Sowjanya D 	if (BITS_TO_BYTES(fls64(*delta) + tkr->shift) >= sizeof(*delta))
120202ecee07SLakshmi Sowjanya D 		return false;
120302ecee07SLakshmi Sowjanya D 
120402ecee07SLakshmi Sowjanya D 	*delta = div_u64((*delta << tkr->shift) - tkr->xtime_nsec, tkr->mult);
120502ecee07SLakshmi Sowjanya D 	return true;
120602ecee07SLakshmi Sowjanya D }
120702ecee07SLakshmi Sowjanya D 
120802ecee07SLakshmi Sowjanya D /**
120902ecee07SLakshmi Sowjanya D  * ktime_real_to_base_clock() - Convert CLOCK_REALTIME timestamp to a base clock timestamp
121002ecee07SLakshmi Sowjanya D  * @treal:	CLOCK_REALTIME timestamp to convert
121102ecee07SLakshmi Sowjanya D  * @base_id:	base clocksource id
121202ecee07SLakshmi Sowjanya D  * @cycles:	pointer to store the converted base clock timestamp
121302ecee07SLakshmi Sowjanya D  *
121402ecee07SLakshmi Sowjanya D  * Converts a supplied, future realtime clock value to the corresponding base clock value.
121502ecee07SLakshmi Sowjanya D  *
121602ecee07SLakshmi Sowjanya D  * Return:  true if the conversion is successful, false otherwise.
121702ecee07SLakshmi Sowjanya D  */
ktime_real_to_base_clock(ktime_t treal,enum clocksource_ids base_id,u64 * cycles)121802ecee07SLakshmi Sowjanya D bool ktime_real_to_base_clock(ktime_t treal, enum clocksource_ids base_id, u64 *cycles)
121902ecee07SLakshmi Sowjanya D {
122002ecee07SLakshmi Sowjanya D 	struct timekeeper *tk = &tk_core.timekeeper;
122102ecee07SLakshmi Sowjanya D 	unsigned int seq;
122202ecee07SLakshmi Sowjanya D 	u64 delta;
122302ecee07SLakshmi Sowjanya D 
122402ecee07SLakshmi Sowjanya D 	do {
122502ecee07SLakshmi Sowjanya D 		seq = read_seqcount_begin(&tk_core.seq);
122602ecee07SLakshmi Sowjanya D 		if ((u64)treal < tk->tkr_mono.base_real)
122702ecee07SLakshmi Sowjanya D 			return false;
122802ecee07SLakshmi Sowjanya D 		delta = (u64)treal - tk->tkr_mono.base_real;
122902ecee07SLakshmi Sowjanya D 		if (!convert_ns_to_cs(&delta))
123002ecee07SLakshmi Sowjanya D 			return false;
123102ecee07SLakshmi Sowjanya D 		*cycles = tk->tkr_mono.cycle_last + delta;
123202ecee07SLakshmi Sowjanya D 		if (!convert_cs_to_base(cycles, base_id))
123302ecee07SLakshmi Sowjanya D 			return false;
123402ecee07SLakshmi Sowjanya D 	} while (read_seqcount_retry(&tk_core.seq, seq));
123502ecee07SLakshmi Sowjanya D 
123602ecee07SLakshmi Sowjanya D 	return true;
123702ecee07SLakshmi Sowjanya D }
123802ecee07SLakshmi Sowjanya D EXPORT_SYMBOL_GPL(ktime_real_to_base_clock);
123902ecee07SLakshmi Sowjanya D 
12408524070bSjohn stultz /**
12418006c245SChristopher S. Hall  * get_device_system_crosststamp - Synchronously capture system/device timestamp
12422c756febSChristopher S. Hall  * @get_time_fn:	Callback to get simultaneous device time and
12438006c245SChristopher S. Hall  *	system counter from the device driver
12442c756febSChristopher S. Hall  * @ctx:		Context passed to get_time_fn()
12452c756febSChristopher S. Hall  * @history_begin:	Historical reference point used to interpolate system
12462c756febSChristopher S. Hall  *	time when counter provided by the driver is before the current interval
12478006c245SChristopher S. Hall  * @xtstamp:		Receives simultaneously captured system and device time
12488006c245SChristopher S. Hall  *
12498006c245SChristopher S. Hall  * Reads a timestamp from a device and correlates it to system time
12508006c245SChristopher S. Hall  */
get_device_system_crosststamp(int (* get_time_fn)(ktime_t * device_time,struct system_counterval_t * sys_counterval,void * ctx),void * ctx,struct system_time_snapshot * history_begin,struct system_device_crosststamp * xtstamp)12518006c245SChristopher S. Hall int get_device_system_crosststamp(int (*get_time_fn)
12528006c245SChristopher S. Hall 				  (ktime_t *device_time,
12538006c245SChristopher S. Hall 				   struct system_counterval_t *sys_counterval,
12548006c245SChristopher S. Hall 				   void *ctx),
12558006c245SChristopher S. Hall 				  void *ctx,
12562c756febSChristopher S. Hall 				  struct system_time_snapshot *history_begin,
12578006c245SChristopher S. Hall 				  struct system_device_crosststamp *xtstamp)
12588006c245SChristopher S. Hall {
12598006c245SChristopher S. Hall 	struct system_counterval_t system_counterval;
12608006c245SChristopher S. Hall 	struct timekeeper *tk = &tk_core.timekeeper;
1261a5a1d1c2SThomas Gleixner 	u64 cycles, now, interval_start;
12626436257bSIngo Molnar 	unsigned int clock_was_set_seq = 0;
12638006c245SChristopher S. Hall 	ktime_t base_real, base_raw;
1264acc89612SThomas Gleixner 	u64 nsec_real, nsec_raw;
12652c756febSChristopher S. Hall 	u8 cs_was_changed_seq;
1266e1e41b6cSRasmus Villemoes 	unsigned int seq;
12672c756febSChristopher S. Hall 	bool do_interp;
12688006c245SChristopher S. Hall 	int ret;
12698006c245SChristopher S. Hall 
12708006c245SChristopher S. Hall 	do {
12718006c245SChristopher S. Hall 		seq = read_seqcount_begin(&tk_core.seq);
12728006c245SChristopher S. Hall 		/*
12738006c245SChristopher S. Hall 		 * Try to synchronously capture device time and a system
12748006c245SChristopher S. Hall 		 * counter value calling back into the device driver
12758006c245SChristopher S. Hall 		 */
12768006c245SChristopher S. Hall 		ret = get_time_fn(&xtstamp->device, &system_counterval, ctx);
12778006c245SChristopher S. Hall 		if (ret)
12788006c245SChristopher S. Hall 			return ret;
12798006c245SChristopher S. Hall 
12808006c245SChristopher S. Hall 		/*
12814b7f5212SPeter Hilber 		 * Verify that the clocksource ID associated with the captured
12824b7f5212SPeter Hilber 		 * system counter value is the same as for the currently
12834b7f5212SPeter Hilber 		 * installed timekeeper clocksource
12848006c245SChristopher S. Hall 		 */
12854b7f5212SPeter Hilber 		if (system_counterval.cs_id == CSID_GENERIC ||
12866b2e2997SLakshmi Sowjanya D 		    !convert_base_to_cs(&system_counterval))
12878006c245SChristopher S. Hall 			return -ENODEV;
12882c756febSChristopher S. Hall 		cycles = system_counterval.cycles;
12892c756febSChristopher S. Hall 
12902c756febSChristopher S. Hall 		/*
12912c756febSChristopher S. Hall 		 * Check whether the system counter value provided by the
12922c756febSChristopher S. Hall 		 * device driver is on the current timekeeping interval.
12932c756febSChristopher S. Hall 		 */
1294ceea5e37SJohn Stultz 		now = tk_clock_read(&tk->tkr_mono);
12952c756febSChristopher S. Hall 		interval_start = tk->tkr_mono.cycle_last;
129687a41130SPeter Hilber 		if (!timestamp_in_interval(interval_start, now, cycles)) {
12972c756febSChristopher S. Hall 			clock_was_set_seq = tk->clock_was_set_seq;
12982c756febSChristopher S. Hall 			cs_was_changed_seq = tk->cs_was_changed_seq;
12992c756febSChristopher S. Hall 			cycles = interval_start;
13002c756febSChristopher S. Hall 			do_interp = true;
13012c756febSChristopher S. Hall 		} else {
13022c756febSChristopher S. Hall 			do_interp = false;
13032c756febSChristopher S. Hall 		}
13048006c245SChristopher S. Hall 
13058006c245SChristopher S. Hall 		base_real = ktime_add(tk->tkr_mono.base,
13068006c245SChristopher S. Hall 				      tk_core.timekeeper.offs_real);
13078006c245SChristopher S. Hall 		base_raw = tk->tkr_raw.base;
13088006c245SChristopher S. Hall 
130914274d0bSPeter Hilber 		nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, cycles);
131014274d0bSPeter Hilber 		nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw, cycles);
13118006c245SChristopher S. Hall 	} while (read_seqcount_retry(&tk_core.seq, seq));
13128006c245SChristopher S. Hall 
13138006c245SChristopher S. Hall 	xtstamp->sys_realtime = ktime_add_ns(base_real, nsec_real);
13148006c245SChristopher S. Hall 	xtstamp->sys_monoraw = ktime_add_ns(base_raw, nsec_raw);
13152c756febSChristopher S. Hall 
13162c756febSChristopher S. Hall 	/*
13172c756febSChristopher S. Hall 	 * Interpolate if necessary, adjusting back from the start of the
13182c756febSChristopher S. Hall 	 * current interval
13192c756febSChristopher S. Hall 	 */
13202c756febSChristopher S. Hall 	if (do_interp) {
1321a5a1d1c2SThomas Gleixner 		u64 partial_history_cycles, total_history_cycles;
13222c756febSChristopher S. Hall 		bool discontinuity;
13232c756febSChristopher S. Hall 
13242c756febSChristopher S. Hall 		/*
132587a41130SPeter Hilber 		 * Check that the counter value is not before the provided
13262c756febSChristopher S. Hall 		 * history reference and that the history doesn't cross a
13272c756febSChristopher S. Hall 		 * clocksource change
13282c756febSChristopher S. Hall 		 */
13292c756febSChristopher S. Hall 		if (!history_begin ||
133087a41130SPeter Hilber 		    !timestamp_in_interval(history_begin->cycles,
133187a41130SPeter Hilber 					   cycles, system_counterval.cycles) ||
13322c756febSChristopher S. Hall 		    history_begin->cs_was_changed_seq != cs_was_changed_seq)
13332c756febSChristopher S. Hall 			return -EINVAL;
13342c756febSChristopher S. Hall 		partial_history_cycles = cycles - system_counterval.cycles;
13352c756febSChristopher S. Hall 		total_history_cycles = cycles - history_begin->cycles;
13362c756febSChristopher S. Hall 		discontinuity =
13372c756febSChristopher S. Hall 			history_begin->clock_was_set_seq != clock_was_set_seq;
13382c756febSChristopher S. Hall 
13392c756febSChristopher S. Hall 		ret = adjust_historical_crosststamp(history_begin,
13402c756febSChristopher S. Hall 						    partial_history_cycles,
13412c756febSChristopher S. Hall 						    total_history_cycles,
13422c756febSChristopher S. Hall 						    discontinuity, xtstamp);
13432c756febSChristopher S. Hall 		if (ret)
13442c756febSChristopher S. Hall 			return ret;
13452c756febSChristopher S. Hall 	}
13462c756febSChristopher S. Hall 
13478006c245SChristopher S. Hall 	return 0;
13488006c245SChristopher S. Hall }
13498006c245SChristopher S. Hall EXPORT_SYMBOL_GPL(get_device_system_crosststamp);
13508006c245SChristopher S. Hall 
13518006c245SChristopher S. Hall /**
135202ecee07SLakshmi Sowjanya D  * timekeeping_clocksource_has_base - Check whether the current clocksource
135302ecee07SLakshmi Sowjanya D  *				      is based on given a base clock
135402ecee07SLakshmi Sowjanya D  * @id:		base clocksource ID
135502ecee07SLakshmi Sowjanya D  *
135602ecee07SLakshmi Sowjanya D  * Note:	The return value is a snapshot which can become invalid right
135702ecee07SLakshmi Sowjanya D  *		after the function returns.
135802ecee07SLakshmi Sowjanya D  *
135902ecee07SLakshmi Sowjanya D  * Return:	true if the timekeeper clocksource has a base clock with @id,
136002ecee07SLakshmi Sowjanya D  *		false otherwise
136102ecee07SLakshmi Sowjanya D  */
timekeeping_clocksource_has_base(enum clocksource_ids id)136202ecee07SLakshmi Sowjanya D bool timekeeping_clocksource_has_base(enum clocksource_ids id)
136302ecee07SLakshmi Sowjanya D {
136402ecee07SLakshmi Sowjanya D 	/*
136502ecee07SLakshmi Sowjanya D 	 * This is a snapshot, so no point in using the sequence
136602ecee07SLakshmi Sowjanya D 	 * count. Just prevent the compiler from re-evaluating @base as the
136702ecee07SLakshmi Sowjanya D 	 * clocksource might change concurrently.
136802ecee07SLakshmi Sowjanya D 	 */
136902ecee07SLakshmi Sowjanya D 	struct clocksource_base *base = READ_ONCE(tk_core.timekeeper.tkr_mono.clock->base);
137002ecee07SLakshmi Sowjanya D 
137102ecee07SLakshmi Sowjanya D 	return base ? base->id == id : false;
137202ecee07SLakshmi Sowjanya D }
137302ecee07SLakshmi Sowjanya D EXPORT_SYMBOL_GPL(timekeeping_clocksource_has_base);
137402ecee07SLakshmi Sowjanya D 
137502ecee07SLakshmi Sowjanya D /**
137621f7eca5Spang.xunlei  * do_settimeofday64 - Sets the time of day.
137721f7eca5Spang.xunlei  * @ts:     pointer to the timespec64 variable containing the new time
13788524070bSjohn stultz  *
13798524070bSjohn stultz  * Sets the time of day to the new time and update NTP and notify hrtimers
13808524070bSjohn stultz  */
do_settimeofday64(const struct timespec64 * ts)138121f7eca5Spang.xunlei int do_settimeofday64(const struct timespec64 *ts)
13828524070bSjohn stultz {
138321f7eca5Spang.xunlei 	struct timespec64 ts_delta, xt;
13848524070bSjohn stultz 
13857a8e61f8SThomas Gleixner 	if (!timespec64_valid_settod(ts))
13868524070bSjohn stultz 		return -EINVAL;
13878524070bSjohn stultz 
1388bba9898eSAnna-Maria Behnsen 	scoped_guard (raw_spinlock_irqsave, &tk_core.lock) {
1389bba9898eSAnna-Maria Behnsen 		struct timekeeper *tks = &tk_core.shadow_timekeeper;
13908524070bSjohn stultz 
1391bba9898eSAnna-Maria Behnsen 		timekeeping_forward_now(tks);
13928524070bSjohn stultz 
1393bba9898eSAnna-Maria Behnsen 		xt = tk_xtime(tks);
13944e8c11b6SYu Liao 		ts_delta = timespec64_sub(*ts, xt);
13951e75fa8bSJohn Stultz 
1396bba9898eSAnna-Maria Behnsen 		if (timespec64_compare(&tks->wall_to_monotonic, &ts_delta) > 0) {
1397bba9898eSAnna-Maria Behnsen 			timekeeping_restore_shadow(&tk_core);
1398bba9898eSAnna-Maria Behnsen 			return -EINVAL;
1399e1d7ba87SWang YanQing 		}
1400e1d7ba87SWang YanQing 
1401bba9898eSAnna-Maria Behnsen 		tk_set_wall_to_mono(tks, timespec64_sub(tks->wall_to_monotonic, ts_delta));
1402bba9898eSAnna-Maria Behnsen 		tk_set_xtime(tks, ts);
1403bba9898eSAnna-Maria Behnsen 		timekeeping_update_from_shadow(&tk_core, TK_UPDATE_ALL);
1404bba9898eSAnna-Maria Behnsen 	}
14058524070bSjohn stultz 
140617a1b882SThomas Gleixner 	/* Signal hrtimers about time change */
140717a1b882SThomas Gleixner 	clock_was_set(CLOCK_SET_WALL);
14088524070bSjohn stultz 
14092d87a067SOndrej Mosnacek 	audit_tk_injoffset(ts_delta);
1410b8ac29b4SJason A. Donenfeld 	add_device_randomness(ts, sizeof(*ts));
1411bba9898eSAnna-Maria Behnsen 	return 0;
14128524070bSjohn stultz }
141321f7eca5Spang.xunlei EXPORT_SYMBOL(do_settimeofday64);
14148524070bSjohn stultz 
1415c528f7c6SJohn Stultz /**
1416c528f7c6SJohn Stultz  * timekeeping_inject_offset - Adds or subtracts from the current time.
14176e5a9190SAlex Shi  * @ts:		Pointer to the timespec variable containing the offset
1418c528f7c6SJohn Stultz  *
1419c528f7c6SJohn Stultz  * Adds or subtracts an offset value from the current time.
1420c528f7c6SJohn Stultz  */
timekeeping_inject_offset(const struct timespec64 * ts)1421985e6950SOndrej Mosnacek static int timekeeping_inject_offset(const struct timespec64 *ts)
1422c528f7c6SJohn Stultz {
14231572fa03SArnd Bergmann 	if (ts->tv_nsec < 0 || ts->tv_nsec >= NSEC_PER_SEC)
1424c528f7c6SJohn Stultz 		return -EINVAL;
1425c528f7c6SJohn Stultz 
142682214756SAnna-Maria Behnsen 	scoped_guard (raw_spinlock_irqsave, &tk_core.lock) {
142782214756SAnna-Maria Behnsen 		struct timekeeper *tks = &tk_core.shadow_timekeeper;
142882214756SAnna-Maria Behnsen 		struct timespec64 tmp;
1429c528f7c6SJohn Stultz 
143082214756SAnna-Maria Behnsen 		timekeeping_forward_now(tks);
1431c528f7c6SJohn Stultz 
14324e8b1452SJohn Stultz 		/* Make sure the proposed value is valid */
143382214756SAnna-Maria Behnsen 		tmp = timespec64_add(tk_xtime(tks), *ts);
143482214756SAnna-Maria Behnsen 		if (timespec64_compare(&tks->wall_to_monotonic, ts) > 0 ||
14357a8e61f8SThomas Gleixner 		    !timespec64_valid_settod(&tmp)) {
143682214756SAnna-Maria Behnsen 			timekeeping_restore_shadow(&tk_core);
143782214756SAnna-Maria Behnsen 			return -EINVAL;
14384e8b1452SJohn Stultz 		}
14391e75fa8bSJohn Stultz 
144082214756SAnna-Maria Behnsen 		tk_xtime_add(tks, ts);
144182214756SAnna-Maria Behnsen 		tk_set_wall_to_mono(tks, timespec64_sub(tks->wall_to_monotonic, *ts));
144282214756SAnna-Maria Behnsen 		timekeeping_update_from_shadow(&tk_core, TK_UPDATE_ALL);
144382214756SAnna-Maria Behnsen 	}
1444c528f7c6SJohn Stultz 
144517a1b882SThomas Gleixner 	/* Signal hrtimers about time change */
144617a1b882SThomas Gleixner 	clock_was_set(CLOCK_SET_WALL);
144782214756SAnna-Maria Behnsen 	return 0;
1448c528f7c6SJohn Stultz }
1449e0956dccSArnd Bergmann 
1450e0956dccSArnd Bergmann /*
1451e0956dccSArnd Bergmann  * Indicates if there is an offset between the system clock and the hardware
1452e0956dccSArnd Bergmann  * clock/persistent clock/rtc.
1453e0956dccSArnd Bergmann  */
1454e0956dccSArnd Bergmann int persistent_clock_is_local;
1455e0956dccSArnd Bergmann 
1456e0956dccSArnd Bergmann /*
1457e0956dccSArnd Bergmann  * Adjust the time obtained from the CMOS to be UTC time instead of
1458e0956dccSArnd Bergmann  * local time.
1459e0956dccSArnd Bergmann  *
1460e0956dccSArnd Bergmann  * This is ugly, but preferable to the alternatives.  Otherwise we
1461e0956dccSArnd Bergmann  * would either need to write a program to do it in /etc/rc (and risk
1462e0956dccSArnd Bergmann  * confusion if the program gets run more than once; it would also be
1463e0956dccSArnd Bergmann  * hard to make the program warp the clock precisely n hours)  or
1464e0956dccSArnd Bergmann  * compile in the timezone information into the kernel.  Bad, bad....
1465e0956dccSArnd Bergmann  *
1466e0956dccSArnd Bergmann  *						- TYT, 1992-01-01
1467e0956dccSArnd Bergmann  *
1468e0956dccSArnd Bergmann  * The best thing to do is to keep the CMOS clock in universal time (UTC)
1469e0956dccSArnd Bergmann  * as real UNIX machines always do it. This avoids all headaches about
1470e0956dccSArnd Bergmann  * daylight saving times and warping kernel clocks.
1471e0956dccSArnd Bergmann  */
timekeeping_warp_clock(void)1472e0956dccSArnd Bergmann void timekeeping_warp_clock(void)
1473e0956dccSArnd Bergmann {
1474e0956dccSArnd Bergmann 	if (sys_tz.tz_minuteswest != 0) {
14751572fa03SArnd Bergmann 		struct timespec64 adjust;
1476e0956dccSArnd Bergmann 
1477e0956dccSArnd Bergmann 		persistent_clock_is_local = 1;
1478e0956dccSArnd Bergmann 		adjust.tv_sec = sys_tz.tz_minuteswest * 60;
1479e0956dccSArnd Bergmann 		adjust.tv_nsec = 0;
1480e0956dccSArnd Bergmann 		timekeeping_inject_offset(&adjust);
1481e0956dccSArnd Bergmann 	}
1482e0956dccSArnd Bergmann }
1483c528f7c6SJohn Stultz 
1484199d280cSAlex Shi /*
148540d9f827SStephen Boyd  * __timekeeping_set_tai_offset - Sets the TAI offset from UTC and monotonic
1486cc244ddaSJohn Stultz  */
__timekeeping_set_tai_offset(struct timekeeper * tk,s32 tai_offset)1487dd5d70e8SFengguang Wu static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
1488cc244ddaSJohn Stultz {
1489cc244ddaSJohn Stultz 	tk->tai_offset = tai_offset;
149004005f60SJohn Stultz 	tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
1491cc244ddaSJohn Stultz }
1492cc244ddaSJohn Stultz 
1493199d280cSAlex Shi /*
14948524070bSjohn stultz  * change_clocksource - Swaps clocksources if a new one is available
14958524070bSjohn stultz  *
14968524070bSjohn stultz  * Accumulates current time interval and initializes new clocksource
14978524070bSjohn stultz  */
change_clocksource(void * data)149875c5158fSMartin Schwidefsky static int change_clocksource(void *data)
14998524070bSjohn stultz {
15001f7226b1SThomas Gleixner 	struct clocksource *new = data, *old = NULL;
15018524070bSjohn stultz 
150209ac369cSThomas Gleixner 	/*
15031f7226b1SThomas Gleixner 	 * If the clocksource is in a module, get a module reference.
15041f7226b1SThomas Gleixner 	 * Succeeds for built-in code (owner == NULL) as well. Abort if the
15051f7226b1SThomas Gleixner 	 * reference can't be acquired.
150609ac369cSThomas Gleixner 	 */
15071f7226b1SThomas Gleixner 	if (!try_module_get(new->owner))
15081f7226b1SThomas Gleixner 		return 0;
15091f7226b1SThomas Gleixner 
15101f7226b1SThomas Gleixner 	/* Abort if the device can't be enabled */
15111f7226b1SThomas Gleixner 	if (new->enable && new->enable(new) != 0) {
151209ac369cSThomas Gleixner 		module_put(new->owner);
15131f7226b1SThomas Gleixner 		return 0;
151409ac369cSThomas Gleixner 	}
1515d4c7c288SNiklas Söderlund 
1516351619fcSAnna-Maria Behnsen 	scoped_guard (raw_spinlock_irqsave, &tk_core.lock) {
1517351619fcSAnna-Maria Behnsen 		struct timekeeper *tks = &tk_core.shadow_timekeeper;
1518d4c7c288SNiklas Söderlund 
1519351619fcSAnna-Maria Behnsen 		timekeeping_forward_now(tks);
1520351619fcSAnna-Maria Behnsen 		old = tks->tkr_mono.clock;
1521351619fcSAnna-Maria Behnsen 		tk_setup_internals(tks, new);
1522351619fcSAnna-Maria Behnsen 		timekeeping_update_from_shadow(&tk_core, TK_UPDATE_ALL);
152375c5158fSMartin Schwidefsky 	}
1524d4c7c288SNiklas Söderlund 
1525d4c7c288SNiklas Söderlund 	if (old) {
1526d4c7c288SNiklas Söderlund 		if (old->disable)
1527d4c7c288SNiklas Söderlund 			old->disable(old);
1528d4c7c288SNiklas Söderlund 		module_put(old->owner);
1529d4c7c288SNiklas Söderlund 	}
1530d4c7c288SNiklas Söderlund 
153175c5158fSMartin Schwidefsky 	return 0;
153275c5158fSMartin Schwidefsky }
15334614e6adSMagnus Damm 
153475c5158fSMartin Schwidefsky /**
153575c5158fSMartin Schwidefsky  * timekeeping_notify - Install a new clock source
153675c5158fSMartin Schwidefsky  * @clock:		pointer to the clock source
153775c5158fSMartin Schwidefsky  *
153875c5158fSMartin Schwidefsky  * This function is called from clocksource.c after a new, better clock
153975c5158fSMartin Schwidefsky  * source has been registered. The caller holds the clocksource_mutex.
154075c5158fSMartin Schwidefsky  */
timekeeping_notify(struct clocksource * clock)1541ba919d1cSThomas Gleixner int timekeeping_notify(struct clocksource *clock)
154275c5158fSMartin Schwidefsky {
15433fdb14fdSThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
15444e250fddSJohn Stultz 
1545876e7881SPeter Zijlstra 	if (tk->tkr_mono.clock == clock)
1546ba919d1cSThomas Gleixner 		return 0;
154775c5158fSMartin Schwidefsky 	stop_machine(change_clocksource, clock, NULL);
15488524070bSjohn stultz 	tick_clock_notify();
1549876e7881SPeter Zijlstra 	return tk->tkr_mono.clock == clock ? 0 : -1;
15508524070bSjohn stultz }
155175c5158fSMartin Schwidefsky 
1552a40f262cSThomas Gleixner /**
1553fb7fcc96SArnd Bergmann  * ktime_get_raw_ts64 - Returns the raw monotonic time in a timespec
1554cdba2ec5SJohn Stultz  * @ts:		pointer to the timespec64 to be set
15552d42244aSJohn Stultz  *
15562d42244aSJohn Stultz  * Returns the raw monotonic time (completely un-modified by ntp)
15572d42244aSJohn Stultz  */
ktime_get_raw_ts64(struct timespec64 * ts)1558fb7fcc96SArnd Bergmann void ktime_get_raw_ts64(struct timespec64 *ts)
15592d42244aSJohn Stultz {
15603fdb14fdSThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
1561e1e41b6cSRasmus Villemoes 	unsigned int seq;
1562acc89612SThomas Gleixner 	u64 nsecs;
15632d42244aSJohn Stultz 
15642d42244aSJohn Stultz 	do {
15653fdb14fdSThomas Gleixner 		seq = read_seqcount_begin(&tk_core.seq);
1566fc6eead7SJohn Stultz 		ts->tv_sec = tk->raw_sec;
15674a4ad80dSPeter Zijlstra 		nsecs = timekeeping_get_ns(&tk->tkr_raw);
15682d42244aSJohn Stultz 
15693fdb14fdSThomas Gleixner 	} while (read_seqcount_retry(&tk_core.seq, seq));
15702d42244aSJohn Stultz 
1571fc6eead7SJohn Stultz 	ts->tv_nsec = 0;
1572fc6eead7SJohn Stultz 	timespec64_add_ns(ts, nsecs);
15732d42244aSJohn Stultz }
1574fb7fcc96SArnd Bergmann EXPORT_SYMBOL(ktime_get_raw_ts64);
1575cdba2ec5SJohn Stultz 
15762d42244aSJohn Stultz 
15772d42244aSJohn Stultz /**
1578cf4fc6cbSLi Zefan  * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
15798524070bSjohn stultz  */
timekeeping_valid_for_hres(void)1580cf4fc6cbSLi Zefan int timekeeping_valid_for_hres(void)
15818524070bSjohn stultz {
15823fdb14fdSThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
1583e1e41b6cSRasmus Villemoes 	unsigned int seq;
15848524070bSjohn stultz 	int ret;
15858524070bSjohn stultz 
15868524070bSjohn stultz 	do {
15873fdb14fdSThomas Gleixner 		seq = read_seqcount_begin(&tk_core.seq);
15888524070bSjohn stultz 
1589876e7881SPeter Zijlstra 		ret = tk->tkr_mono.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
15908524070bSjohn stultz 
15913fdb14fdSThomas Gleixner 	} while (read_seqcount_retry(&tk_core.seq, seq));
15928524070bSjohn stultz 
15938524070bSjohn stultz 	return ret;
15948524070bSjohn stultz }
15958524070bSjohn stultz 
15968524070bSjohn stultz /**
159798962465SJon Hunter  * timekeeping_max_deferment - Returns max time the clocksource can be deferred
159898962465SJon Hunter  */
timekeeping_max_deferment(void)159998962465SJon Hunter u64 timekeeping_max_deferment(void)
160098962465SJon Hunter {
16013fdb14fdSThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
1602e1e41b6cSRasmus Villemoes 	unsigned int seq;
160370471f2fSJohn Stultz 	u64 ret;
160442e71e81SJohn Stultz 
160570471f2fSJohn Stultz 	do {
16063fdb14fdSThomas Gleixner 		seq = read_seqcount_begin(&tk_core.seq);
160770471f2fSJohn Stultz 
1608876e7881SPeter Zijlstra 		ret = tk->tkr_mono.clock->max_idle_ns;
160970471f2fSJohn Stultz 
16103fdb14fdSThomas Gleixner 	} while (read_seqcount_retry(&tk_core.seq, seq));
161170471f2fSJohn Stultz 
161270471f2fSJohn Stultz 	return ret;
161398962465SJon Hunter }
161498962465SJon Hunter 
161598962465SJon Hunter /**
161692661788SArnd Bergmann  * read_persistent_clock64 -  Return time from the persistent clock.
16176e5a9190SAlex Shi  * @ts: Pointer to the storage for the readout value
16188524070bSjohn stultz  *
16198524070bSjohn stultz  * Weak dummy function for arches that do not yet support it.
1620d4f587c6SMartin Schwidefsky  * Reads the time from the battery backed persistent clock.
1621d4f587c6SMartin Schwidefsky  * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
16228524070bSjohn stultz  *
16238524070bSjohn stultz  *  XXX - Do be sure to remove it once all arches implement it.
16248524070bSjohn stultz  */
read_persistent_clock64(struct timespec64 * ts)162592661788SArnd Bergmann void __weak read_persistent_clock64(struct timespec64 *ts)
16268524070bSjohn stultz {
1627d4f587c6SMartin Schwidefsky 	ts->tv_sec = 0;
1628d4f587c6SMartin Schwidefsky 	ts->tv_nsec = 0;
16298524070bSjohn stultz }
16308524070bSjohn stultz 
163123970e38SMartin Schwidefsky /**
16323eca9937SPavel Tatashin  * read_persistent_wall_and_boot_offset - Read persistent clock, and also offset
16333eca9937SPavel Tatashin  *                                        from the boot.
1634f3cb8080SRandy Dunlap  * @wall_time:	  current time as returned by persistent clock
1635f3cb8080SRandy Dunlap  * @boot_offset:  offset that is defined as wall_time - boot_time
163623970e38SMartin Schwidefsky  *
163723970e38SMartin Schwidefsky  * Weak dummy function for arches that do not yet support it.
163829efc461SAlex Shi  *
16394b1b7f80SPavel Tatashin  * The default function calculates offset based on the current value of
16404b1b7f80SPavel Tatashin  * local_clock(). This way architectures that support sched_clock() but don't
16414b1b7f80SPavel Tatashin  * support dedicated boot time clock will provide the best estimate of the
16424b1b7f80SPavel Tatashin  * boot time.
164323970e38SMartin Schwidefsky  */
16443eca9937SPavel Tatashin void __weak __init
read_persistent_wall_and_boot_offset(struct timespec64 * wall_time,struct timespec64 * boot_offset)16453eca9937SPavel Tatashin read_persistent_wall_and_boot_offset(struct timespec64 *wall_time,
16463eca9937SPavel Tatashin 				     struct timespec64 *boot_offset)
164723970e38SMartin Schwidefsky {
16483eca9937SPavel Tatashin 	read_persistent_clock64(wall_time);
16494b1b7f80SPavel Tatashin 	*boot_offset = ns_to_timespec64(local_clock());
165023970e38SMartin Schwidefsky }
165123970e38SMartin Schwidefsky 
tkd_basic_setup(struct tk_data * tkd)1652a5f9e4e4SAnna-Maria Behnsen static __init void tkd_basic_setup(struct tk_data *tkd)
1653a5f9e4e4SAnna-Maria Behnsen {
1654a5f9e4e4SAnna-Maria Behnsen 	raw_spin_lock_init(&tkd->lock);
1655a5f9e4e4SAnna-Maria Behnsen 	seqcount_raw_spinlock_init(&tkd->seq, &tkd->lock);
1656a5f9e4e4SAnna-Maria Behnsen }
1657a5f9e4e4SAnna-Maria Behnsen 
1658f473e5f4SMukesh Ojha /*
1659f473e5f4SMukesh Ojha  * Flag reflecting whether timekeeping_resume() has injected sleeptime.
1660f473e5f4SMukesh Ojha  *
1661f473e5f4SMukesh Ojha  * The flag starts of false and is only set when a suspend reaches
1662f473e5f4SMukesh Ojha  * timekeeping_suspend(), timekeeping_resume() sets it to false when the
1663f473e5f4SMukesh Ojha  * timekeeper clocksource is not stopping across suspend and has been
1664f473e5f4SMukesh Ojha  * used to update sleep time. If the timekeeper clocksource has stopped
1665f473e5f4SMukesh Ojha  * then the flag stays true and is used by the RTC resume code to decide
1666f473e5f4SMukesh Ojha  * whether sleeptime must be injected and if so the flag gets false then.
1667f473e5f4SMukesh Ojha  *
1668f473e5f4SMukesh Ojha  * If a suspend fails before reaching timekeeping_resume() then the flag
1669f473e5f4SMukesh Ojha  * stays false and prevents erroneous sleeptime injection.
1670f473e5f4SMukesh Ojha  */
1671f473e5f4SMukesh Ojha static bool suspend_timing_needed;
16720fa88cb4SXunlei Pang 
16730fa88cb4SXunlei Pang /* Flag for if there is a persistent clock on this platform */
16740fa88cb4SXunlei Pang static bool persistent_clock_exists;
16750fa88cb4SXunlei Pang 
16768524070bSjohn stultz /*
16778524070bSjohn stultz  * timekeeping_init - Initializes the clocksource and common timekeeping values
16788524070bSjohn stultz  */
timekeeping_init(void)16798524070bSjohn stultz void __init timekeeping_init(void)
16808524070bSjohn stultz {
16813eca9937SPavel Tatashin 	struct timespec64 wall_time, boot_offset, wall_to_mono;
16822cab490bSAnna-Maria Behnsen 	struct timekeeper *tks = &tk_core.shadow_timekeeper;
1683155ec602SMartin Schwidefsky 	struct clocksource *clock;
16848c4799b1SAnna-Maria Behnsen 
1685a5f9e4e4SAnna-Maria Behnsen 	tkd_basic_setup(&tk_core);
1686d4f587c6SMartin Schwidefsky 
16873eca9937SPavel Tatashin 	read_persistent_wall_and_boot_offset(&wall_time, &boot_offset);
16887a8e61f8SThomas Gleixner 	if (timespec64_valid_settod(&wall_time) &&
16893eca9937SPavel Tatashin 	    timespec64_to_ns(&wall_time) > 0) {
16900fa88cb4SXunlei Pang 		persistent_clock_exists = true;
1691684ad537SPavel Tatashin 	} else if (timespec64_to_ns(&wall_time) != 0) {
16923eca9937SPavel Tatashin 		pr_warn("Persistent clock returned invalid value");
16933eca9937SPavel Tatashin 		wall_time = (struct timespec64){0};
16944e8b1452SJohn Stultz 	}
16958524070bSjohn stultz 
16963eca9937SPavel Tatashin 	if (timespec64_compare(&wall_time, &boot_offset) < 0)
16973eca9937SPavel Tatashin 		boot_offset = (struct timespec64){0};
16983eca9937SPavel Tatashin 
16993eca9937SPavel Tatashin 	/*
17003eca9937SPavel Tatashin 	 * We want set wall_to_mono, so the following is true:
17013eca9937SPavel Tatashin 	 * wall time + wall_to_mono = boot time
17023eca9937SPavel Tatashin 	 */
17033eca9937SPavel Tatashin 	wall_to_mono = timespec64_sub(boot_offset, wall_time);
17043eca9937SPavel Tatashin 
17058c4799b1SAnna-Maria Behnsen 	guard(raw_spinlock_irqsave)(&tk_core.lock);
17062cab490bSAnna-Maria Behnsen 
170706c017fdSJohn Stultz 	ntp_init();
170806c017fdSJohn Stultz 
1709f1b82746SMartin Schwidefsky 	clock = clocksource_default_clock();
1710a0f7d48bSMartin Schwidefsky 	if (clock->enable)
1711a0f7d48bSMartin Schwidefsky 		clock->enable(clock);
17122cab490bSAnna-Maria Behnsen 	tk_setup_internals(tks, clock);
17138524070bSjohn stultz 
17142cab490bSAnna-Maria Behnsen 	tk_set_xtime(tks, &wall_time);
17152cab490bSAnna-Maria Behnsen 	tks->raw_sec = 0;
17161e75fa8bSJohn Stultz 
17172cab490bSAnna-Maria Behnsen 	tk_set_wall_to_mono(tks, wall_to_mono);
17186d0ef903SJohn Stultz 
17192cab490bSAnna-Maria Behnsen 	timekeeping_update_from_shadow(&tk_core, TK_CLOCK_WAS_SET);
17208524070bSjohn stultz }
17218524070bSjohn stultz 
1722264bb3f7SXunlei Pang /* time in seconds when suspend began for persistent clock */
17237d489d15SJohn Stultz static struct timespec64 timekeeping_suspend_time;
17248524070bSjohn stultz 
17258524070bSjohn stultz /**
1726304529b1SJohn Stultz  * __timekeeping_inject_sleeptime - Internal function to add sleep interval
17276e5a9190SAlex Shi  * @tk:		Pointer to the timekeeper to be updated
17286e5a9190SAlex Shi  * @delta:	Pointer to the delta value in timespec64 format
1729304529b1SJohn Stultz  *
1730304529b1SJohn Stultz  * Takes a timespec offset measuring a suspend interval and properly
1731304529b1SJohn Stultz  * adds the sleep offset to the timekeeping variables.
1732304529b1SJohn Stultz  */
__timekeeping_inject_sleeptime(struct timekeeper * tk,const struct timespec64 * delta)1733f726a697SJohn Stultz static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
1734985e6950SOndrej Mosnacek 					   const struct timespec64 *delta)
1735304529b1SJohn Stultz {
17367d489d15SJohn Stultz 	if (!timespec64_valid_strict(delta)) {
17376d9bcb62SJohn Stultz 		printk_deferred(KERN_WARNING
17386d9bcb62SJohn Stultz 				"__timekeeping_inject_sleeptime: Invalid "
1739cb5de2f8SJohn Stultz 				"sleep delta value!\n");
1740cb5de2f8SJohn Stultz 		return;
1741cb5de2f8SJohn Stultz 	}
1742f726a697SJohn Stultz 	tk_xtime_add(tk, delta);
1743a3ed0e43SThomas Gleixner 	tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
174447da70d3SThomas Gleixner 	tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
17455c83545fSColin Cross 	tk_debug_account_sleep_time(delta);
1746304529b1SJohn Stultz }
1747304529b1SJohn Stultz 
17487f298139SXunlei Pang #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
1749f3cb8080SRandy Dunlap /*
17500fa88cb4SXunlei Pang  * We have three kinds of time sources to use for sleep time
17510fa88cb4SXunlei Pang  * injection, the preference order is:
17520fa88cb4SXunlei Pang  * 1) non-stop clocksource
17530fa88cb4SXunlei Pang  * 2) persistent clock (ie: RTC accessible when irqs are off)
17540fa88cb4SXunlei Pang  * 3) RTC
17550fa88cb4SXunlei Pang  *
17560fa88cb4SXunlei Pang  * 1) and 2) are used by timekeeping, 3) by RTC subsystem.
17570fa88cb4SXunlei Pang  * If system has neither 1) nor 2), 3) will be used finally.
17580fa88cb4SXunlei Pang  *
17590fa88cb4SXunlei Pang  *
17600fa88cb4SXunlei Pang  * If timekeeping has injected sleeptime via either 1) or 2),
17610fa88cb4SXunlei Pang  * 3) becomes needless, so in this case we don't need to call
17620fa88cb4SXunlei Pang  * rtc_resume(), and this is what timekeeping_rtc_skipresume()
17630fa88cb4SXunlei Pang  * means.
17640fa88cb4SXunlei Pang  */
timekeeping_rtc_skipresume(void)17650fa88cb4SXunlei Pang bool timekeeping_rtc_skipresume(void)
17660fa88cb4SXunlei Pang {
1767f473e5f4SMukesh Ojha 	return !suspend_timing_needed;
17680fa88cb4SXunlei Pang }
17690fa88cb4SXunlei Pang 
1770f3cb8080SRandy Dunlap /*
17710fa88cb4SXunlei Pang  * 1) can be determined whether to use or not only when doing
17720fa88cb4SXunlei Pang  * timekeeping_resume() which is invoked after rtc_suspend(),
17730fa88cb4SXunlei Pang  * so we can't skip rtc_suspend() surely if system has 1).
17740fa88cb4SXunlei Pang  *
17750fa88cb4SXunlei Pang  * But if system has 2), 2) will definitely be used, so in this
17760fa88cb4SXunlei Pang  * case we don't need to call rtc_suspend(), and this is what
17770fa88cb4SXunlei Pang  * timekeeping_rtc_skipsuspend() means.
17780fa88cb4SXunlei Pang  */
timekeeping_rtc_skipsuspend(void)17790fa88cb4SXunlei Pang bool timekeeping_rtc_skipsuspend(void)
17800fa88cb4SXunlei Pang {
17810fa88cb4SXunlei Pang 	return persistent_clock_exists;
17820fa88cb4SXunlei Pang }
17830fa88cb4SXunlei Pang 
17840fa88cb4SXunlei Pang /**
178504d90890Spang.xunlei  * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values
178604d90890Spang.xunlei  * @delta: pointer to a timespec64 delta value
1787304529b1SJohn Stultz  *
17882ee96632SXunlei Pang  * This hook is for architectures that cannot support read_persistent_clock64
1789304529b1SJohn Stultz  * because their RTC/persistent clock is only accessible when irqs are enabled.
17900fa88cb4SXunlei Pang  * and also don't have an effective nonstop clocksource.
1791304529b1SJohn Stultz  *
1792304529b1SJohn Stultz  * This function should only be called by rtc_resume(), and allows
1793304529b1SJohn Stultz  * a suspend offset to be injected into the timekeeping values.
1794304529b1SJohn Stultz  */
timekeeping_inject_sleeptime64(const struct timespec64 * delta)1795985e6950SOndrej Mosnacek void timekeeping_inject_sleeptime64(const struct timespec64 *delta)
1796304529b1SJohn Stultz {
17972b473e65SAnna-Maria Behnsen 	scoped_guard(raw_spinlock_irqsave, &tk_core.lock) {
17982b473e65SAnna-Maria Behnsen 		struct timekeeper *tks = &tk_core.shadow_timekeeper;
179970471f2fSJohn Stultz 
1800f473e5f4SMukesh Ojha 		suspend_timing_needed = false;
18012b473e65SAnna-Maria Behnsen 		timekeeping_forward_now(tks);
18022b473e65SAnna-Maria Behnsen 		__timekeeping_inject_sleeptime(tks, delta);
18032b473e65SAnna-Maria Behnsen 		timekeeping_update_from_shadow(&tk_core, TK_UPDATE_ALL);
18042b473e65SAnna-Maria Behnsen 	}
1805304529b1SJohn Stultz 
180617a1b882SThomas Gleixner 	/* Signal hrtimers about time change */
180717a1b882SThomas Gleixner 	clock_was_set(CLOCK_SET_WALL | CLOCK_SET_BOOT);
1808304529b1SJohn Stultz }
18097f298139SXunlei Pang #endif
1810304529b1SJohn Stultz 
1811304529b1SJohn Stultz /**
18128524070bSjohn stultz  * timekeeping_resume - Resumes the generic timekeeping subsystem.
18138524070bSjohn stultz  */
timekeeping_resume(void)1814124cf911SRafael J. Wysocki void timekeeping_resume(void)
18158524070bSjohn stultz {
1816b2350d95SAnna-Maria Behnsen 	struct timekeeper *tks = &tk_core.shadow_timekeeper;
1817b2350d95SAnna-Maria Behnsen 	struct clocksource *clock = tks->tkr_mono.clock;
18187d489d15SJohn Stultz 	struct timespec64 ts_new, ts_delta;
1819f473e5f4SMukesh Ojha 	bool inject_sleeptime = false;
1820b2350d95SAnna-Maria Behnsen 	u64 cycle_now, nsec;
1821b2350d95SAnna-Maria Behnsen 	unsigned long flags;
1822d4f587c6SMartin Schwidefsky 
18232ee96632SXunlei Pang 	read_persistent_clock64(&ts_new);
18248524070bSjohn stultz 
1825adc78e6bSRafael J. Wysocki 	clockevents_resume();
1826d10ff3fbSThomas Gleixner 	clocksource_resume();
1827d10ff3fbSThomas Gleixner 
18288c4799b1SAnna-Maria Behnsen 	raw_spin_lock_irqsave(&tk_core.lock, flags);
18298524070bSjohn stultz 
1830e445cf1cSFeng Tang 	/*
1831e445cf1cSFeng Tang 	 * After system resumes, we need to calculate the suspended time and
1832e445cf1cSFeng Tang 	 * compensate it for the OS time. There are 3 sources that could be
1833e445cf1cSFeng Tang 	 * used: Nonstop clocksource during suspend, persistent clock and rtc
1834e445cf1cSFeng Tang 	 * device.
1835e445cf1cSFeng Tang 	 *
1836e445cf1cSFeng Tang 	 * One specific platform may have 1 or 2 or all of them, and the
1837e445cf1cSFeng Tang 	 * preference will be:
1838e445cf1cSFeng Tang 	 *	suspend-nonstop clocksource -> persistent clock -> rtc
1839e445cf1cSFeng Tang 	 * The less preferred source will only be tried if there is no better
1840e445cf1cSFeng Tang 	 * usable source. The rtc part is handled separately in rtc core code.
1841e445cf1cSFeng Tang 	 */
1842b2350d95SAnna-Maria Behnsen 	cycle_now = tk_clock_read(&tks->tkr_mono);
184339232ed5SBaolin Wang 	nsec = clocksource_stop_suspend_timing(clock, cycle_now);
184439232ed5SBaolin Wang 	if (nsec > 0) {
18457d489d15SJohn Stultz 		ts_delta = ns_to_timespec64(nsec);
1846f473e5f4SMukesh Ojha 		inject_sleeptime = true;
18477d489d15SJohn Stultz 	} else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
18487d489d15SJohn Stultz 		ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
1849f473e5f4SMukesh Ojha 		inject_sleeptime = true;
1850e445cf1cSFeng Tang 	}
1851e445cf1cSFeng Tang 
1852f473e5f4SMukesh Ojha 	if (inject_sleeptime) {
1853f473e5f4SMukesh Ojha 		suspend_timing_needed = false;
1854b2350d95SAnna-Maria Behnsen 		__timekeeping_inject_sleeptime(tks, &ts_delta);
1855f473e5f4SMukesh Ojha 	}
1856e445cf1cSFeng Tang 
1857e445cf1cSFeng Tang 	/* Re-base the last cycle value */
1858b2350d95SAnna-Maria Behnsen 	tks->tkr_mono.cycle_last = cycle_now;
1859b2350d95SAnna-Maria Behnsen 	tks->tkr_raw.cycle_last  = cycle_now;
18604a4ad80dSPeter Zijlstra 
1861b2350d95SAnna-Maria Behnsen 	tks->ntp_error = 0;
18628524070bSjohn stultz 	timekeeping_suspended = 0;
1863b2350d95SAnna-Maria Behnsen 	timekeeping_update_from_shadow(&tk_core, TK_CLOCK_WAS_SET);
18648c4799b1SAnna-Maria Behnsen 	raw_spin_unlock_irqrestore(&tk_core.lock, flags);
18658524070bSjohn stultz 
18668524070bSjohn stultz 	touch_softlockup_watchdog();
18678524070bSjohn stultz 
1868a761a67fSThomas Gleixner 	/* Resume the clockevent device(s) and hrtimers */
18694ffee521SThomas Gleixner 	tick_resume();
1870a761a67fSThomas Gleixner 	/* Notify timerfd as resume is equivalent to clock_was_set() */
1871a761a67fSThomas Gleixner 	timerfd_resume();
18728524070bSjohn stultz }
18738524070bSjohn stultz 
timekeeping_suspend(void)1874124cf911SRafael J. Wysocki int timekeeping_suspend(void)
18758524070bSjohn stultz {
1876d05eae87SAnna-Maria Behnsen 	struct timekeeper *tks = &tk_core.shadow_timekeeper;
18777d489d15SJohn Stultz 	struct timespec64 delta, delta_delta;
18787d489d15SJohn Stultz 	static struct timespec64 old_delta;
187939232ed5SBaolin Wang 	struct clocksource *curr_clock;
1880d05eae87SAnna-Maria Behnsen 	unsigned long flags;
188139232ed5SBaolin Wang 	u64 cycle_now;
18828524070bSjohn stultz 
18832ee96632SXunlei Pang 	read_persistent_clock64(&timekeeping_suspend_time);
18843be90950SThomas Gleixner 
18850d6bd995SZoran Markovic 	/*
18860d6bd995SZoran Markovic 	 * On some systems the persistent_clock can not be detected at
18870d6bd995SZoran Markovic 	 * timekeeping_init by its return value, so if we see a valid
18880d6bd995SZoran Markovic 	 * value returned, update the persistent_clock_exists flag.
18890d6bd995SZoran Markovic 	 */
18900d6bd995SZoran Markovic 	if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
18910fa88cb4SXunlei Pang 		persistent_clock_exists = true;
18920d6bd995SZoran Markovic 
1893f473e5f4SMukesh Ojha 	suspend_timing_needed = true;
1894f473e5f4SMukesh Ojha 
18958c4799b1SAnna-Maria Behnsen 	raw_spin_lock_irqsave(&tk_core.lock, flags);
1896d05eae87SAnna-Maria Behnsen 	timekeeping_forward_now(tks);
18978524070bSjohn stultz 	timekeeping_suspended = 1;
1898cb33217bSJohn Stultz 
189939232ed5SBaolin Wang 	/*
190039232ed5SBaolin Wang 	 * Since we've called forward_now, cycle_last stores the value
190139232ed5SBaolin Wang 	 * just read from the current clocksource. Save this to potentially
190239232ed5SBaolin Wang 	 * use in suspend timing.
190339232ed5SBaolin Wang 	 */
1904d05eae87SAnna-Maria Behnsen 	curr_clock = tks->tkr_mono.clock;
1905d05eae87SAnna-Maria Behnsen 	cycle_now = tks->tkr_mono.cycle_last;
190639232ed5SBaolin Wang 	clocksource_start_suspend_timing(curr_clock, cycle_now);
190739232ed5SBaolin Wang 
19080fa88cb4SXunlei Pang 	if (persistent_clock_exists) {
1909cb33217bSJohn Stultz 		/*
1910cb33217bSJohn Stultz 		 * To avoid drift caused by repeated suspend/resumes,
1911cb33217bSJohn Stultz 		 * which each can add ~1 second drift error,
1912cb33217bSJohn Stultz 		 * try to compensate so the difference in system time
1913cb33217bSJohn Stultz 		 * and persistent_clock time stays close to constant.
1914cb33217bSJohn Stultz 		 */
1915d05eae87SAnna-Maria Behnsen 		delta = timespec64_sub(tk_xtime(tks), timekeeping_suspend_time);
19167d489d15SJohn Stultz 		delta_delta = timespec64_sub(delta, old_delta);
1917cb33217bSJohn Stultz 		if (abs(delta_delta.tv_sec) >= 2) {
1918cb33217bSJohn Stultz 			/*
1919cb33217bSJohn Stultz 			 * if delta_delta is too large, assume time correction
1920264bb3f7SXunlei Pang 			 * has occurred and set old_delta to the current delta.
1921cb33217bSJohn Stultz 			 */
1922cb33217bSJohn Stultz 			old_delta = delta;
1923cb33217bSJohn Stultz 		} else {
1924cb33217bSJohn Stultz 			/* Otherwise try to adjust old_system to compensate */
1925cb33217bSJohn Stultz 			timekeeping_suspend_time =
19267d489d15SJohn Stultz 				timespec64_add(timekeeping_suspend_time, delta_delta);
1927cb33217bSJohn Stultz 		}
1928264bb3f7SXunlei Pang 	}
1929330a1617SJohn Stultz 
1930d05eae87SAnna-Maria Behnsen 	timekeeping_update_from_shadow(&tk_core, 0);
1931d05eae87SAnna-Maria Behnsen 	halt_fast_timekeeper(tks);
19328c4799b1SAnna-Maria Behnsen 	raw_spin_unlock_irqrestore(&tk_core.lock, flags);
19338524070bSjohn stultz 
19344ffee521SThomas Gleixner 	tick_suspend();
1935c54a42b1SMagnus Damm 	clocksource_suspend();
1936adc78e6bSRafael J. Wysocki 	clockevents_suspend();
19378524070bSjohn stultz 
19388524070bSjohn stultz 	return 0;
19398524070bSjohn stultz }
19408524070bSjohn stultz 
19418524070bSjohn stultz /* sysfs resume/suspend bits for timekeeping */
1942e1a85b2cSRafael J. Wysocki static struct syscore_ops timekeeping_syscore_ops = {
19438524070bSjohn stultz 	.resume		= timekeeping_resume,
19448524070bSjohn stultz 	.suspend	= timekeeping_suspend,
19458524070bSjohn stultz };
19468524070bSjohn stultz 
timekeeping_init_ops(void)1947e1a85b2cSRafael J. Wysocki static int __init timekeeping_init_ops(void)
19488524070bSjohn stultz {
1949e1a85b2cSRafael J. Wysocki 	register_syscore_ops(&timekeeping_syscore_ops);
1950e1a85b2cSRafael J. Wysocki 	return 0;
19518524070bSjohn stultz }
1952e1a85b2cSRafael J. Wysocki device_initcall(timekeeping_init_ops);
19538524070bSjohn stultz 
19548524070bSjohn stultz /*
1955dc491596SJohn Stultz  * Apply a multiplier adjustment to the timekeeper
19568524070bSjohn stultz  */
timekeeping_apply_adjustment(struct timekeeper * tk,s64 offset,s32 mult_adj)1957dc491596SJohn Stultz static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
1958dc491596SJohn Stultz 							 s64 offset,
195978b98e3cSMiroslav Lichvar 							 s32 mult_adj)
19608524070bSjohn stultz {
1961dc491596SJohn Stultz 	s64 interval = tk->cycle_interval;
19628524070bSjohn stultz 
196378b98e3cSMiroslav Lichvar 	if (mult_adj == 0) {
196478b98e3cSMiroslav Lichvar 		return;
196578b98e3cSMiroslav Lichvar 	} else if (mult_adj == -1) {
19668524070bSjohn stultz 		interval = -interval;
19678524070bSjohn stultz 		offset = -offset;
196878b98e3cSMiroslav Lichvar 	} else if (mult_adj != 1) {
196978b98e3cSMiroslav Lichvar 		interval *= mult_adj;
197078b98e3cSMiroslav Lichvar 		offset *= mult_adj;
19711d17d174SIngo Molnar 	}
19728524070bSjohn stultz 
1973c2bc1111SJohn Stultz 	/*
1974c2bc1111SJohn Stultz 	 * So the following can be confusing.
1975c2bc1111SJohn Stultz 	 *
1976dc491596SJohn Stultz 	 * To keep things simple, lets assume mult_adj == 1 for now.
1977c2bc1111SJohn Stultz 	 *
1978dc491596SJohn Stultz 	 * When mult_adj != 1, remember that the interval and offset values
1979c2bc1111SJohn Stultz 	 * have been appropriately scaled so the math is the same.
1980c2bc1111SJohn Stultz 	 *
1981c2bc1111SJohn Stultz 	 * The basic idea here is that we're increasing the multiplier
1982c2bc1111SJohn Stultz 	 * by one, this causes the xtime_interval to be incremented by
1983c2bc1111SJohn Stultz 	 * one cycle_interval. This is because:
1984c2bc1111SJohn Stultz 	 *	xtime_interval = cycle_interval * mult
1985c2bc1111SJohn Stultz 	 * So if mult is being incremented by one:
1986c2bc1111SJohn Stultz 	 *	xtime_interval = cycle_interval * (mult + 1)
1987c2bc1111SJohn Stultz 	 * Its the same as:
1988c2bc1111SJohn Stultz 	 *	xtime_interval = (cycle_interval * mult) + cycle_interval
1989c2bc1111SJohn Stultz 	 * Which can be shortened to:
1990c2bc1111SJohn Stultz 	 *	xtime_interval += cycle_interval
1991c2bc1111SJohn Stultz 	 *
1992c2bc1111SJohn Stultz 	 * So offset stores the non-accumulated cycles. Thus the current
1993c2bc1111SJohn Stultz 	 * time (in shifted nanoseconds) is:
1994c2bc1111SJohn Stultz 	 *	now = (offset * adj) + xtime_nsec
1995c2bc1111SJohn Stultz 	 * Now, even though we're adjusting the clock frequency, we have
1996c2bc1111SJohn Stultz 	 * to keep time consistent. In other words, we can't jump back
1997c2bc1111SJohn Stultz 	 * in time, and we also want to avoid jumping forward in time.
1998c2bc1111SJohn Stultz 	 *
1999c2bc1111SJohn Stultz 	 * So given the same offset value, we need the time to be the same
2000c2bc1111SJohn Stultz 	 * both before and after the freq adjustment.
2001c2bc1111SJohn Stultz 	 *	now = (offset * adj_1) + xtime_nsec_1
2002c2bc1111SJohn Stultz 	 *	now = (offset * adj_2) + xtime_nsec_2
2003c2bc1111SJohn Stultz 	 * So:
2004c2bc1111SJohn Stultz 	 *	(offset * adj_1) + xtime_nsec_1 =
2005c2bc1111SJohn Stultz 	 *		(offset * adj_2) + xtime_nsec_2
2006c2bc1111SJohn Stultz 	 * And we know:
2007c2bc1111SJohn Stultz 	 *	adj_2 = adj_1 + 1
2008c2bc1111SJohn Stultz 	 * So:
2009c2bc1111SJohn Stultz 	 *	(offset * adj_1) + xtime_nsec_1 =
2010c2bc1111SJohn Stultz 	 *		(offset * (adj_1+1)) + xtime_nsec_2
2011c2bc1111SJohn Stultz 	 *	(offset * adj_1) + xtime_nsec_1 =
2012c2bc1111SJohn Stultz 	 *		(offset * adj_1) + offset + xtime_nsec_2
2013c2bc1111SJohn Stultz 	 * Canceling the sides:
2014c2bc1111SJohn Stultz 	 *	xtime_nsec_1 = offset + xtime_nsec_2
2015c2bc1111SJohn Stultz 	 * Which gives us:
2016c2bc1111SJohn Stultz 	 *	xtime_nsec_2 = xtime_nsec_1 - offset
20174bf07f65SIngo Molnar 	 * Which simplifies to:
2018c2bc1111SJohn Stultz 	 *	xtime_nsec -= offset
2019c2bc1111SJohn Stultz 	 */
2020876e7881SPeter Zijlstra 	if ((mult_adj > 0) && (tk->tkr_mono.mult + mult_adj < mult_adj)) {
20216067dc5aSpang.xunlei 		/* NTP adjustment caused clocksource mult overflow */
20226067dc5aSpang.xunlei 		WARN_ON_ONCE(1);
20236067dc5aSpang.xunlei 		return;
20246067dc5aSpang.xunlei 	}
20256067dc5aSpang.xunlei 
2026876e7881SPeter Zijlstra 	tk->tkr_mono.mult += mult_adj;
2027f726a697SJohn Stultz 	tk->xtime_interval += interval;
2028876e7881SPeter Zijlstra 	tk->tkr_mono.xtime_nsec -= offset;
2029dc491596SJohn Stultz }
20302a8c0883SJohn Stultz 
2031dc491596SJohn Stultz /*
2032dc491596SJohn Stultz  * Adjust the timekeeper's multiplier to the correct frequency
2033dc491596SJohn Stultz  * and also to reduce the accumulated error value.
2034dc491596SJohn Stultz  */
timekeeping_adjust(struct timekeeper * tk,s64 offset)2035dc491596SJohn Stultz static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
2036dc491596SJohn Stultz {
203714f1e3b3SThomas Gleixner 	u64 ntp_tl = ntp_tick_length();
203878b98e3cSMiroslav Lichvar 	u32 mult;
2039dc491596SJohn Stultz 
204078b98e3cSMiroslav Lichvar 	/*
204178b98e3cSMiroslav Lichvar 	 * Determine the multiplier from the current NTP tick length.
204278b98e3cSMiroslav Lichvar 	 * Avoid expensive division when the tick length doesn't change.
204378b98e3cSMiroslav Lichvar 	 */
204414f1e3b3SThomas Gleixner 	if (likely(tk->ntp_tick == ntp_tl)) {
204578b98e3cSMiroslav Lichvar 		mult = tk->tkr_mono.mult - tk->ntp_err_mult;
204678b98e3cSMiroslav Lichvar 	} else {
204714f1e3b3SThomas Gleixner 		tk->ntp_tick = ntp_tl;
204878b98e3cSMiroslav Lichvar 		mult = div64_u64((tk->ntp_tick >> tk->ntp_error_shift) -
204978b98e3cSMiroslav Lichvar 				 tk->xtime_remainder, tk->cycle_interval);
2050dc491596SJohn Stultz 	}
2051dc491596SJohn Stultz 
205278b98e3cSMiroslav Lichvar 	/*
205378b98e3cSMiroslav Lichvar 	 * If the clock is behind the NTP time, increase the multiplier by 1
205478b98e3cSMiroslav Lichvar 	 * to catch up with it. If it's ahead and there was a remainder in the
205578b98e3cSMiroslav Lichvar 	 * tick division, the clock will slow down. Otherwise it will stay
205678b98e3cSMiroslav Lichvar 	 * ahead until the tick length changes to a non-divisible value.
205778b98e3cSMiroslav Lichvar 	 */
205878b98e3cSMiroslav Lichvar 	tk->ntp_err_mult = tk->ntp_error > 0 ? 1 : 0;
205978b98e3cSMiroslav Lichvar 	mult += tk->ntp_err_mult;
206078b98e3cSMiroslav Lichvar 
206178b98e3cSMiroslav Lichvar 	timekeeping_apply_adjustment(tk, offset, mult - tk->tkr_mono.mult);
206278b98e3cSMiroslav Lichvar 
2063876e7881SPeter Zijlstra 	if (unlikely(tk->tkr_mono.clock->maxadj &&
2064876e7881SPeter Zijlstra 		(abs(tk->tkr_mono.mult - tk->tkr_mono.clock->mult)
2065876e7881SPeter Zijlstra 			> tk->tkr_mono.clock->maxadj))) {
2066dc491596SJohn Stultz 		printk_once(KERN_WARNING
2067dc491596SJohn Stultz 			"Adjusting %s more than 11%% (%ld vs %ld)\n",
2068876e7881SPeter Zijlstra 			tk->tkr_mono.clock->name, (long)tk->tkr_mono.mult,
2069876e7881SPeter Zijlstra 			(long)tk->tkr_mono.clock->mult + tk->tkr_mono.clock->maxadj);
2070dc491596SJohn Stultz 	}
2071dc491596SJohn Stultz 
20722a8c0883SJohn Stultz 	/*
20732a8c0883SJohn Stultz 	 * It may be possible that when we entered this function, xtime_nsec
20742a8c0883SJohn Stultz 	 * was very small.  Further, if we're slightly speeding the clocksource
20752a8c0883SJohn Stultz 	 * in the code above, its possible the required corrective factor to
20762a8c0883SJohn Stultz 	 * xtime_nsec could cause it to underflow.
20772a8c0883SJohn Stultz 	 *
207878b98e3cSMiroslav Lichvar 	 * Now, since we have already accumulated the second and the NTP
207978b98e3cSMiroslav Lichvar 	 * subsystem has been notified via second_overflow(), we need to skip
208078b98e3cSMiroslav Lichvar 	 * the next update.
20812a8c0883SJohn Stultz 	 */
2082876e7881SPeter Zijlstra 	if (unlikely((s64)tk->tkr_mono.xtime_nsec < 0)) {
208378b98e3cSMiroslav Lichvar 		tk->tkr_mono.xtime_nsec += (u64)NSEC_PER_SEC <<
208478b98e3cSMiroslav Lichvar 							tk->tkr_mono.shift;
208578b98e3cSMiroslav Lichvar 		tk->xtime_sec--;
208678b98e3cSMiroslav Lichvar 		tk->skip_second_overflow = 1;
20872a8c0883SJohn Stultz 	}
20888524070bSjohn stultz }
20898524070bSjohn stultz 
2090199d280cSAlex Shi /*
20911f4f9487SJohn Stultz  * accumulate_nsecs_to_secs - Accumulates nsecs into secs
20921f4f9487SJohn Stultz  *
2093571af55aSZhen Lei  * Helper function that accumulates the nsecs greater than a second
20941f4f9487SJohn Stultz  * from the xtime_nsec field to the xtime_secs field.
20951f4f9487SJohn Stultz  * It also calls into the NTP code to handle leapsecond processing.
20961f4f9487SJohn Stultz  */
accumulate_nsecs_to_secs(struct timekeeper * tk)2097780427f0SDavid Vrabel static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
20981f4f9487SJohn Stultz {
2099876e7881SPeter Zijlstra 	u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
21005258d3f2SJohn Stultz 	unsigned int clock_set = 0;
21011f4f9487SJohn Stultz 
2102876e7881SPeter Zijlstra 	while (tk->tkr_mono.xtime_nsec >= nsecps) {
21031f4f9487SJohn Stultz 		int leap;
21041f4f9487SJohn Stultz 
2105876e7881SPeter Zijlstra 		tk->tkr_mono.xtime_nsec -= nsecps;
21061f4f9487SJohn Stultz 		tk->xtime_sec++;
21071f4f9487SJohn Stultz 
210878b98e3cSMiroslav Lichvar 		/*
210978b98e3cSMiroslav Lichvar 		 * Skip NTP update if this second was accumulated before,
211078b98e3cSMiroslav Lichvar 		 * i.e. xtime_nsec underflowed in timekeeping_adjust()
211178b98e3cSMiroslav Lichvar 		 */
211278b98e3cSMiroslav Lichvar 		if (unlikely(tk->skip_second_overflow)) {
211378b98e3cSMiroslav Lichvar 			tk->skip_second_overflow = 0;
211478b98e3cSMiroslav Lichvar 			continue;
211578b98e3cSMiroslav Lichvar 		}
211678b98e3cSMiroslav Lichvar 
21171f4f9487SJohn Stultz 		/* Figure out if its a leap sec and apply if needed */
21181f4f9487SJohn Stultz 		leap = second_overflow(tk->xtime_sec);
21196d0ef903SJohn Stultz 		if (unlikely(leap)) {
21207d489d15SJohn Stultz 			struct timespec64 ts;
21211f4f9487SJohn Stultz 
21226d0ef903SJohn Stultz 			tk->xtime_sec += leap;
21236d0ef903SJohn Stultz 
21246d0ef903SJohn Stultz 			ts.tv_sec = leap;
21256d0ef903SJohn Stultz 			ts.tv_nsec = 0;
21266d0ef903SJohn Stultz 			tk_set_wall_to_mono(tk,
21277d489d15SJohn Stultz 				timespec64_sub(tk->wall_to_monotonic, ts));
21286d0ef903SJohn Stultz 
2129cc244ddaSJohn Stultz 			__timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
2130cc244ddaSJohn Stultz 
21315258d3f2SJohn Stultz 			clock_set = TK_CLOCK_WAS_SET;
21326d0ef903SJohn Stultz 		}
21331f4f9487SJohn Stultz 	}
21345258d3f2SJohn Stultz 	return clock_set;
21351f4f9487SJohn Stultz }
21361f4f9487SJohn Stultz 
2137199d280cSAlex Shi /*
2138a092ff0fSjohn stultz  * logarithmic_accumulation - shifted accumulation of cycles
2139a092ff0fSjohn stultz  *
2140a092ff0fSjohn stultz  * This functions accumulates a shifted interval of cycles into
2141b0294f30SRandy Dunlap  * a shifted interval nanoseconds. Allows for O(log) accumulation
2142a092ff0fSjohn stultz  * loop.
2143a092ff0fSjohn stultz  *
2144a092ff0fSjohn stultz  * Returns the unconsumed cycles.
2145a092ff0fSjohn stultz  */
logarithmic_accumulation(struct timekeeper * tk,u64 offset,u32 shift,unsigned int * clock_set)2146a5a1d1c2SThomas Gleixner static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset,
2147a5a1d1c2SThomas Gleixner 				    u32 shift, unsigned int *clock_set)
2148a092ff0fSjohn stultz {
2149a5a1d1c2SThomas Gleixner 	u64 interval = tk->cycle_interval << shift;
21503d88d56cSJohn Stultz 	u64 snsec_per_sec;
2151a092ff0fSjohn stultz 
2152571af55aSZhen Lei 	/* If the offset is smaller than a shifted interval, do nothing */
215323a9537aSThomas Gleixner 	if (offset < interval)
2154a092ff0fSjohn stultz 		return offset;
2155a092ff0fSjohn stultz 
2156a092ff0fSjohn stultz 	/* Accumulate one shifted interval */
215723a9537aSThomas Gleixner 	offset -= interval;
2158876e7881SPeter Zijlstra 	tk->tkr_mono.cycle_last += interval;
21594a4ad80dSPeter Zijlstra 	tk->tkr_raw.cycle_last  += interval;
2160a092ff0fSjohn stultz 
2161876e7881SPeter Zijlstra 	tk->tkr_mono.xtime_nsec += tk->xtime_interval << shift;
21625258d3f2SJohn Stultz 	*clock_set |= accumulate_nsecs_to_secs(tk);
2163a092ff0fSjohn stultz 
2164deda2e81SJason Wessel 	/* Accumulate raw time */
21653d88d56cSJohn Stultz 	tk->tkr_raw.xtime_nsec += tk->raw_interval << shift;
21663d88d56cSJohn Stultz 	snsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
21673d88d56cSJohn Stultz 	while (tk->tkr_raw.xtime_nsec >= snsec_per_sec) {
21683d88d56cSJohn Stultz 		tk->tkr_raw.xtime_nsec -= snsec_per_sec;
2169fc6eead7SJohn Stultz 		tk->raw_sec++;
2170a092ff0fSjohn stultz 	}
2171a092ff0fSjohn stultz 
2172a092ff0fSjohn stultz 	/* Accumulate error between NTP and clock interval */
2173375f45b5SJohn Stultz 	tk->ntp_error += tk->ntp_tick << shift;
2174f726a697SJohn Stultz 	tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
2175f726a697SJohn Stultz 						(tk->ntp_error_shift + shift);
2176a092ff0fSjohn stultz 
2177a092ff0fSjohn stultz 	return offset;
2178a092ff0fSjohn stultz }
2179a092ff0fSjohn stultz 
2180b061c7a5SMiroslav Lichvar /*
2181b061c7a5SMiroslav Lichvar  * timekeeping_advance - Updates the timekeeper to the current time and
2182b061c7a5SMiroslav Lichvar  * current NTP tick length
21838524070bSjohn stultz  */
timekeeping_advance(enum timekeeping_adv_mode mode)21841b267793SThomas Gleixner static bool timekeeping_advance(enum timekeeping_adv_mode mode)
21858524070bSjohn stultz {
218620c7b582SThomas Gleixner 	struct timekeeper *tk = &tk_core.shadow_timekeeper;
21873fdb14fdSThomas Gleixner 	struct timekeeper *real_tk = &tk_core.timekeeper;
21885258d3f2SJohn Stultz 	unsigned int clock_set = 0;
2189324a2219SThomas Gleixner 	int shift = 0, maxshift;
2190*b71f9804SThomas Gleixner 	u64 offset, orig_offset;
219170471f2fSJohn Stultz 
21928c4799b1SAnna-Maria Behnsen 	guard(raw_spinlock_irqsave)(&tk_core.lock);
21938524070bSjohn stultz 
21948524070bSjohn stultz 	/* Make sure we're fully resumed: */
21958524070bSjohn stultz 	if (unlikely(timekeeping_suspended))
2196c2a32956SThomas Gleixner 		return false;
21978524070bSjohn stultz 
2198ceea5e37SJohn Stultz 	offset = clocksource_delta(tk_clock_read(&tk->tkr_mono),
219976031d95SThomas Gleixner 				   tk->tkr_mono.cycle_last, tk->tkr_mono.mask,
220076031d95SThomas Gleixner 				   tk->tkr_mono.clock->max_raw_delta);
2201*b71f9804SThomas Gleixner 	orig_offset = offset;
2202bf2ac312SJohn Stultz 	/* Check if there's really nothing to do */
2203b061c7a5SMiroslav Lichvar 	if (offset < real_tk->cycle_interval && mode == TK_ADV_TICK)
2204c2a32956SThomas Gleixner 		return false;
22053c17ad19SJohn Stultz 
2206324a2219SThomas Gleixner 	/*
2207324a2219SThomas Gleixner 	 * With NO_HZ we may have to accumulate many cycle_intervals
2208324a2219SThomas Gleixner 	 * (think "ticks") worth of time at once. To do this efficiently,
2209324a2219SThomas Gleixner 	 * we calculate the largest doubling multiple of cycle_intervals
2210324a2219SThomas Gleixner 	 * that is smaller than the offset.  We then accumulate that
2211324a2219SThomas Gleixner 	 * chunk in one go, and then try to consume the next smaller
2212324a2219SThomas Gleixner 	 * doubled multiple.
2213324a2219SThomas Gleixner 	 */
2214324a2219SThomas Gleixner 	shift = ilog2(offset) - ilog2(tk->cycle_interval);
2215324a2219SThomas Gleixner 	shift = max(0, shift);
2216324a2219SThomas Gleixner 	/* Bound shift to one less than what overflows tick_length */
2217324a2219SThomas Gleixner 	maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
2218324a2219SThomas Gleixner 	shift = min(shift, maxshift);
2219324a2219SThomas Gleixner 	while (offset >= tk->cycle_interval) {
2220324a2219SThomas Gleixner 		offset = logarithmic_accumulation(tk, offset, shift, &clock_set);
2221324a2219SThomas Gleixner 		if (offset < tk->cycle_interval<<shift)
2222324a2219SThomas Gleixner 			shift--;
2223324a2219SThomas Gleixner 	}
22248524070bSjohn stultz 
222578b98e3cSMiroslav Lichvar 	/* Adjust the multiplier to correct NTP error */
22264e250fddSJohn Stultz 	timekeeping_adjust(tk, offset);
22278524070bSjohn stultz 
22286a867a39SJohn Stultz 	/*
22296a867a39SJohn Stultz 	 * Finally, make sure that after the rounding
22301e75fa8bSJohn Stultz 	 * xtime_nsec isn't larger than NSEC_PER_SEC
22316a867a39SJohn Stultz 	 */
22325258d3f2SJohn Stultz 	clock_set |= accumulate_nsecs_to_secs(tk);
223383f57a11SLinus Torvalds 
2234*b71f9804SThomas Gleixner 	/*
2235*b71f9804SThomas Gleixner 	 * To avoid inconsistencies caused adjtimex TK_ADV_FREQ calls
2236*b71f9804SThomas Gleixner 	 * making small negative adjustments to the base xtime_nsec
2237*b71f9804SThomas Gleixner 	 * value, only update the coarse clocks if we accumulated time
2238*b71f9804SThomas Gleixner 	 */
2239*b71f9804SThomas Gleixner 	if (orig_offset != offset)
2240*b71f9804SThomas Gleixner 		tk_update_coarse_nsecs(tk);
2241*b71f9804SThomas Gleixner 
22425aa6c43eSAnna-Maria Behnsen 	timekeeping_update_from_shadow(&tk_core, clock_set);
22431b267793SThomas Gleixner 
22441b267793SThomas Gleixner 	return !!clock_set;
22458524070bSjohn stultz }
22467c3f1a57STomas Janousek 
22477c3f1a57STomas Janousek /**
2248b061c7a5SMiroslav Lichvar  * update_wall_time - Uses the current clocksource to increment the wall time
2249b061c7a5SMiroslav Lichvar  *
2250b061c7a5SMiroslav Lichvar  */
update_wall_time(void)2251b061c7a5SMiroslav Lichvar void update_wall_time(void)
2252b061c7a5SMiroslav Lichvar {
22531b267793SThomas Gleixner 	if (timekeeping_advance(TK_ADV_TICK))
22541b267793SThomas Gleixner 		clock_was_set_delayed();
2255b061c7a5SMiroslav Lichvar }
2256b061c7a5SMiroslav Lichvar 
2257b061c7a5SMiroslav Lichvar /**
2258d08c0cddSJohn Stultz  * getboottime64 - Return the real time of system boot.
2259d08c0cddSJohn Stultz  * @ts:		pointer to the timespec64 to be set
22607c3f1a57STomas Janousek  *
2261d08c0cddSJohn Stultz  * Returns the wall-time of boot in a timespec64.
22627c3f1a57STomas Janousek  *
22637c3f1a57STomas Janousek  * This is based on the wall_to_monotonic offset and the total suspend
22647c3f1a57STomas Janousek  * time. Calls to settimeofday will affect the value returned (which
22657c3f1a57STomas Janousek  * basically means that however wrong your real time clock is at boot time,
22667c3f1a57STomas Janousek  * you get the right time here).
22677c3f1a57STomas Janousek  */
getboottime64(struct timespec64 * ts)2268d08c0cddSJohn Stultz void getboottime64(struct timespec64 *ts)
22697c3f1a57STomas Janousek {
22703fdb14fdSThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
2271a3ed0e43SThomas Gleixner 	ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot);
2272d4f587c6SMartin Schwidefsky 
2273d08c0cddSJohn Stultz 	*ts = ktime_to_timespec64(t);
22747c3f1a57STomas Janousek }
2275d08c0cddSJohn Stultz EXPORT_SYMBOL_GPL(getboottime64);
22767c3f1a57STomas Janousek 
ktime_get_coarse_real_ts64(struct timespec64 * ts)2277fb7fcc96SArnd Bergmann void ktime_get_coarse_real_ts64(struct timespec64 *ts)
22782c6b47deSjohn stultz {
22793fdb14fdSThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
2280e1e41b6cSRasmus Villemoes 	unsigned int seq;
22812c6b47deSjohn stultz 
22822c6b47deSjohn stultz 	do {
22833fdb14fdSThomas Gleixner 		seq = read_seqcount_begin(&tk_core.seq);
228483f57a11SLinus Torvalds 
2285*b71f9804SThomas Gleixner 		*ts = tk_xtime_coarse(tk);
22863fdb14fdSThomas Gleixner 	} while (read_seqcount_retry(&tk_core.seq, seq));
22872c6b47deSjohn stultz }
2288fb7fcc96SArnd Bergmann EXPORT_SYMBOL(ktime_get_coarse_real_ts64);
2289da15cfdaSjohn stultz 
2290ee3283c6SJeff Layton /**
2291ee3283c6SJeff Layton  * ktime_get_coarse_real_ts64_mg - return latter of coarse grained time or floor
2292ee3283c6SJeff Layton  * @ts:		timespec64 to be filled
2293ee3283c6SJeff Layton  *
2294ee3283c6SJeff Layton  * Fetch the global mg_floor value, convert it to realtime and compare it
2295ee3283c6SJeff Layton  * to the current coarse-grained time. Fill @ts with whichever is
2296ee3283c6SJeff Layton  * latest. Note that this is a filesystem-specific interface and should be
2297ee3283c6SJeff Layton  * avoided outside of that context.
2298ee3283c6SJeff Layton  */
ktime_get_coarse_real_ts64_mg(struct timespec64 * ts)2299ee3283c6SJeff Layton void ktime_get_coarse_real_ts64_mg(struct timespec64 *ts)
2300ee3283c6SJeff Layton {
2301ee3283c6SJeff Layton 	struct timekeeper *tk = &tk_core.timekeeper;
2302ee3283c6SJeff Layton 	u64 floor = atomic64_read(&mg_floor);
2303ee3283c6SJeff Layton 	ktime_t f_real, offset, coarse;
2304ee3283c6SJeff Layton 	unsigned int seq;
2305ee3283c6SJeff Layton 
2306ee3283c6SJeff Layton 	do {
2307ee3283c6SJeff Layton 		seq = read_seqcount_begin(&tk_core.seq);
2308*b71f9804SThomas Gleixner 		*ts = tk_xtime_coarse(tk);
2309ee3283c6SJeff Layton 		offset = tk_core.timekeeper.offs_real;
2310ee3283c6SJeff Layton 	} while (read_seqcount_retry(&tk_core.seq, seq));
2311ee3283c6SJeff Layton 
2312ee3283c6SJeff Layton 	coarse = timespec64_to_ktime(*ts);
2313ee3283c6SJeff Layton 	f_real = ktime_add(floor, offset);
2314ee3283c6SJeff Layton 	if (ktime_after(f_real, coarse))
2315ee3283c6SJeff Layton 		*ts = ktime_to_timespec64(f_real);
2316ee3283c6SJeff Layton }
2317ee3283c6SJeff Layton 
2318ee3283c6SJeff Layton /**
2319ee3283c6SJeff Layton  * ktime_get_real_ts64_mg - attempt to update floor value and return result
2320ee3283c6SJeff Layton  * @ts:		pointer to the timespec to be set
2321ee3283c6SJeff Layton  *
2322ee3283c6SJeff Layton  * Get a monotonic fine-grained time value and attempt to swap it into
2323ee3283c6SJeff Layton  * mg_floor. If that succeeds then accept the new floor value. If it fails
2324ee3283c6SJeff Layton  * then another task raced in during the interim time and updated the
2325ee3283c6SJeff Layton  * floor.  Since any update to the floor must be later than the previous
2326ee3283c6SJeff Layton  * floor, either outcome is acceptable.
2327ee3283c6SJeff Layton  *
2328ee3283c6SJeff Layton  * Typically this will be called after calling ktime_get_coarse_real_ts64_mg(),
2329ee3283c6SJeff Layton  * and determining that the resulting coarse-grained timestamp did not effect
2330ee3283c6SJeff Layton  * a change in ctime. Any more recent floor value would effect a change to
2331ee3283c6SJeff Layton  * ctime, so there is no need to retry the atomic64_try_cmpxchg() on failure.
2332ee3283c6SJeff Layton  *
2333ee3283c6SJeff Layton  * @ts will be filled with the latest floor value, regardless of the outcome of
2334ee3283c6SJeff Layton  * the cmpxchg. Note that this is a filesystem specific interface and should be
2335ee3283c6SJeff Layton  * avoided outside of that context.
2336ee3283c6SJeff Layton  */
ktime_get_real_ts64_mg(struct timespec64 * ts)2337ee3283c6SJeff Layton void ktime_get_real_ts64_mg(struct timespec64 *ts)
2338ee3283c6SJeff Layton {
2339ee3283c6SJeff Layton 	struct timekeeper *tk = &tk_core.timekeeper;
2340ee3283c6SJeff Layton 	ktime_t old = atomic64_read(&mg_floor);
2341ee3283c6SJeff Layton 	ktime_t offset, mono;
2342ee3283c6SJeff Layton 	unsigned int seq;
2343ee3283c6SJeff Layton 	u64 nsecs;
2344ee3283c6SJeff Layton 
2345ee3283c6SJeff Layton 	do {
2346ee3283c6SJeff Layton 		seq = read_seqcount_begin(&tk_core.seq);
2347ee3283c6SJeff Layton 
2348ee3283c6SJeff Layton 		ts->tv_sec = tk->xtime_sec;
2349ee3283c6SJeff Layton 		mono = tk->tkr_mono.base;
2350ee3283c6SJeff Layton 		nsecs = timekeeping_get_ns(&tk->tkr_mono);
2351ee3283c6SJeff Layton 		offset = tk_core.timekeeper.offs_real;
2352ee3283c6SJeff Layton 	} while (read_seqcount_retry(&tk_core.seq, seq));
2353ee3283c6SJeff Layton 
2354ee3283c6SJeff Layton 	mono = ktime_add_ns(mono, nsecs);
2355ee3283c6SJeff Layton 
2356ee3283c6SJeff Layton 	/*
2357ee3283c6SJeff Layton 	 * Attempt to update the floor with the new time value. As any
2358ee3283c6SJeff Layton 	 * update must be later then the existing floor, and would effect
2359ee3283c6SJeff Layton 	 * a change to ctime from the perspective of the current task,
2360ee3283c6SJeff Layton 	 * accept the resulting floor value regardless of the outcome of
2361ee3283c6SJeff Layton 	 * the swap.
2362ee3283c6SJeff Layton 	 */
2363ee3283c6SJeff Layton 	if (atomic64_try_cmpxchg(&mg_floor, &old, mono)) {
2364ee3283c6SJeff Layton 		ts->tv_nsec = 0;
2365ee3283c6SJeff Layton 		timespec64_add_ns(ts, nsecs);
23662a153857SJeff Layton 		timekeeping_inc_mg_floor_swaps();
2367ee3283c6SJeff Layton 	} else {
2368ee3283c6SJeff Layton 		/*
2369ee3283c6SJeff Layton 		 * Another task changed mg_floor since "old" was fetched.
2370ee3283c6SJeff Layton 		 * "old" has been updated with the latest value of "mg_floor".
2371ee3283c6SJeff Layton 		 * That value is newer than the previous floor value, which
2372ee3283c6SJeff Layton 		 * is enough to effect a change to ctime. Accept it.
2373ee3283c6SJeff Layton 		 */
2374ee3283c6SJeff Layton 		*ts = ktime_to_timespec64(ktime_add(old, offset));
2375ee3283c6SJeff Layton 	}
2376ee3283c6SJeff Layton }
2377ee3283c6SJeff Layton 
ktime_get_coarse_ts64(struct timespec64 * ts)2378fb7fcc96SArnd Bergmann void ktime_get_coarse_ts64(struct timespec64 *ts)
2379da15cfdaSjohn stultz {
23803fdb14fdSThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
23817d489d15SJohn Stultz 	struct timespec64 now, mono;
2382e1e41b6cSRasmus Villemoes 	unsigned int seq;
2383da15cfdaSjohn stultz 
2384da15cfdaSjohn stultz 	do {
23853fdb14fdSThomas Gleixner 		seq = read_seqcount_begin(&tk_core.seq);
238683f57a11SLinus Torvalds 
2387*b71f9804SThomas Gleixner 		now = tk_xtime_coarse(tk);
23884e250fddSJohn Stultz 		mono = tk->wall_to_monotonic;
23893fdb14fdSThomas Gleixner 	} while (read_seqcount_retry(&tk_core.seq, seq));
2390da15cfdaSjohn stultz 
2391fb7fcc96SArnd Bergmann 	set_normalized_timespec64(ts, now.tv_sec + mono.tv_sec,
2392da15cfdaSjohn stultz 				  now.tv_nsec + mono.tv_nsec);
2393da15cfdaSjohn stultz }
2394fb7fcc96SArnd Bergmann EXPORT_SYMBOL(ktime_get_coarse_ts64);
2395871cf1e5STorben Hohn 
2396871cf1e5STorben Hohn /*
2397d6ad4187SJohn Stultz  * Must hold jiffies_lock
2398871cf1e5STorben Hohn  */
do_timer(unsigned long ticks)2399871cf1e5STorben Hohn void do_timer(unsigned long ticks)
2400871cf1e5STorben Hohn {
2401871cf1e5STorben Hohn 	jiffies_64 += ticks;
240246132e3aSPaul Gortmaker 	calc_global_load();
2403871cf1e5STorben Hohn }
240448cf76f7STorben Hohn 
240548cf76f7STorben Hohn /**
240676f41088SJohn Stultz  * ktime_get_update_offsets_now - hrtimer helper
2407868a3e91SThomas Gleixner  * @cwsseq:	pointer to check and store the clock was set sequence number
2408f6c06abfSThomas Gleixner  * @offs_real:	pointer to storage for monotonic -> realtime offset
2409a3ed0e43SThomas Gleixner  * @offs_boot:	pointer to storage for monotonic -> boottime offset
2410b7bc50e4SXie XiuQi  * @offs_tai:	pointer to storage for monotonic -> clock tai offset
2411f6c06abfSThomas Gleixner  *
2412868a3e91SThomas Gleixner  * Returns current monotonic time and updates the offsets if the
2413868a3e91SThomas Gleixner  * sequence number in @cwsseq and timekeeper.clock_was_set_seq are
2414868a3e91SThomas Gleixner  * different.
2415868a3e91SThomas Gleixner  *
2416b7bc50e4SXie XiuQi  * Called from hrtimer_interrupt() or retrigger_next_event()
2417f6c06abfSThomas Gleixner  */
ktime_get_update_offsets_now(unsigned int * cwsseq,ktime_t * offs_real,ktime_t * offs_boot,ktime_t * offs_tai)2418868a3e91SThomas Gleixner ktime_t ktime_get_update_offsets_now(unsigned int *cwsseq, ktime_t *offs_real,
2419a3ed0e43SThomas Gleixner 				     ktime_t *offs_boot, ktime_t *offs_tai)
2420f6c06abfSThomas Gleixner {
24213fdb14fdSThomas Gleixner 	struct timekeeper *tk = &tk_core.timekeeper;
2422f6c06abfSThomas Gleixner 	unsigned int seq;
2423a37c0aadSThomas Gleixner 	ktime_t base;
2424a37c0aadSThomas Gleixner 	u64 nsecs;
2425f6c06abfSThomas Gleixner 
2426f6c06abfSThomas Gleixner 	do {
24273fdb14fdSThomas Gleixner 		seq = read_seqcount_begin(&tk_core.seq);
2428f6c06abfSThomas Gleixner 
2429876e7881SPeter Zijlstra 		base = tk->tkr_mono.base;
2430876e7881SPeter Zijlstra 		nsecs = timekeeping_get_ns(&tk->tkr_mono);
2431833f32d7SJohn Stultz 		base = ktime_add_ns(base, nsecs);
2432833f32d7SJohn Stultz 
2433868a3e91SThomas Gleixner 		if (*cwsseq != tk->clock_was_set_seq) {
2434868a3e91SThomas Gleixner 			*cwsseq = tk->clock_was_set_seq;
24354e250fddSJohn Stultz 			*offs_real = tk->offs_real;
2436a3ed0e43SThomas Gleixner 			*offs_boot = tk->offs_boot;
243790adda98SJohn Stultz 			*offs_tai = tk->offs_tai;
2438868a3e91SThomas Gleixner 		}
2439833f32d7SJohn Stultz 
2440833f32d7SJohn Stultz 		/* Handle leapsecond insertion adjustments */
24412456e855SThomas Gleixner 		if (unlikely(base >= tk->next_leap_ktime))
2442833f32d7SJohn Stultz 			*offs_real = ktime_sub(tk->offs_real, ktime_set(1, 0));
2443833f32d7SJohn Stultz 
24443fdb14fdSThomas Gleixner 	} while (read_seqcount_retry(&tk_core.seq, seq));
2445f6c06abfSThomas Gleixner 
2446833f32d7SJohn Stultz 	return base;
2447f6c06abfSThomas Gleixner }
2448f6c06abfSThomas Gleixner 
2449199d280cSAlex Shi /*
24501572fa03SArnd Bergmann  * timekeeping_validate_timex - Ensures the timex is ok for use in do_adjtimex
2451e0956dccSArnd Bergmann  */
timekeeping_validate_timex(const struct __kernel_timex * txc)2452ead25417SDeepa Dinamani static int timekeeping_validate_timex(const struct __kernel_timex *txc)
2453e0956dccSArnd Bergmann {
2454e0956dccSArnd Bergmann 	if (txc->modes & ADJ_ADJTIME) {
2455e0956dccSArnd Bergmann 		/* singleshot must not be used with any other mode bits */
2456e0956dccSArnd Bergmann 		if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
2457e0956dccSArnd Bergmann 			return -EINVAL;
2458e0956dccSArnd Bergmann 		if (!(txc->modes & ADJ_OFFSET_READONLY) &&
2459e0956dccSArnd Bergmann 		    !capable(CAP_SYS_TIME))
2460e0956dccSArnd Bergmann 			return -EPERM;
2461e0956dccSArnd Bergmann 	} else {
2462e0956dccSArnd Bergmann 		/* In order to modify anything, you gotta be super-user! */
2463e0956dccSArnd Bergmann 		if (txc->modes && !capable(CAP_SYS_TIME))
2464e0956dccSArnd Bergmann 			return -EPERM;
2465e0956dccSArnd Bergmann 		/*
2466e0956dccSArnd Bergmann 		 * if the quartz is off by more than 10% then
2467e0956dccSArnd Bergmann 		 * something is VERY wrong!
2468e0956dccSArnd Bergmann 		 */
2469e0956dccSArnd Bergmann 		if (txc->modes & ADJ_TICK &&
2470e0956dccSArnd Bergmann 		    (txc->tick <  900000/USER_HZ ||
2471e0956dccSArnd Bergmann 		     txc->tick > 1100000/USER_HZ))
2472e0956dccSArnd Bergmann 			return -EINVAL;
2473e0956dccSArnd Bergmann 	}
2474e0956dccSArnd Bergmann 
2475e0956dccSArnd Bergmann 	if (txc->modes & ADJ_SETOFFSET) {
2476e0956dccSArnd Bergmann 		/* In order to inject time, you gotta be super-user! */
2477e0956dccSArnd Bergmann 		if (!capable(CAP_SYS_TIME))
2478e0956dccSArnd Bergmann 			return -EPERM;
2479e0956dccSArnd Bergmann 
24801572fa03SArnd Bergmann 		/*
24811572fa03SArnd Bergmann 		 * Validate if a timespec/timeval used to inject a time
24824bf07f65SIngo Molnar 		 * offset is valid.  Offsets can be positive or negative, so
24831572fa03SArnd Bergmann 		 * we don't check tv_sec. The value of the timeval/timespec
24841572fa03SArnd Bergmann 		 * is the sum of its fields,but *NOTE*:
24851572fa03SArnd Bergmann 		 * The field tv_usec/tv_nsec must always be non-negative and
24861572fa03SArnd Bergmann 		 * we can't have more nanoseconds/microseconds than a second.
24871572fa03SArnd Bergmann 		 */
24881572fa03SArnd Bergmann 		if (txc->time.tv_usec < 0)
2489e0956dccSArnd Bergmann 			return -EINVAL;
2490e0956dccSArnd Bergmann 
24911572fa03SArnd Bergmann 		if (txc->modes & ADJ_NANO) {
24921572fa03SArnd Bergmann 			if (txc->time.tv_usec >= NSEC_PER_SEC)
24931572fa03SArnd Bergmann 				return -EINVAL;
2494e0956dccSArnd Bergmann 		} else {
24951572fa03SArnd Bergmann 			if (txc->time.tv_usec >= USEC_PER_SEC)
2496e0956dccSArnd Bergmann 				return -EINVAL;
2497e0956dccSArnd Bergmann 		}
2498e0956dccSArnd Bergmann 	}
2499e0956dccSArnd Bergmann 
2500e0956dccSArnd Bergmann 	/*
2501e0956dccSArnd Bergmann 	 * Check for potential multiplication overflows that can
2502e0956dccSArnd Bergmann 	 * only happen on 64-bit systems:
2503e0956dccSArnd Bergmann 	 */
2504e0956dccSArnd Bergmann 	if ((txc->modes & ADJ_FREQUENCY) && (BITS_PER_LONG == 64)) {
2505e0956dccSArnd Bergmann 		if (LLONG_MIN / PPM_SCALE > txc->freq)
2506e0956dccSArnd Bergmann 			return -EINVAL;
2507e0956dccSArnd Bergmann 		if (LLONG_MAX / PPM_SCALE < txc->freq)
2508e0956dccSArnd Bergmann 			return -EINVAL;
2509e0956dccSArnd Bergmann 	}
2510e0956dccSArnd Bergmann 
2511e0956dccSArnd Bergmann 	return 0;
2512e0956dccSArnd Bergmann }
2513e0956dccSArnd Bergmann 
25141366992eSJason A. Donenfeld /**
25151366992eSJason A. Donenfeld  * random_get_entropy_fallback - Returns the raw clock source value,
25161366992eSJason A. Donenfeld  * used by random.c for platforms with no valid random_get_entropy().
25171366992eSJason A. Donenfeld  */
random_get_entropy_fallback(void)25181366992eSJason A. Donenfeld unsigned long random_get_entropy_fallback(void)
25191366992eSJason A. Donenfeld {
25201366992eSJason A. Donenfeld 	struct tk_read_base *tkr = &tk_core.timekeeper.tkr_mono;
25211366992eSJason A. Donenfeld 	struct clocksource *clock = READ_ONCE(tkr->clock);
25221366992eSJason A. Donenfeld 
25231366992eSJason A. Donenfeld 	if (unlikely(timekeeping_suspended || !clock))
25241366992eSJason A. Donenfeld 		return 0;
25251366992eSJason A. Donenfeld 	return clock->read(clock);
25261366992eSJason A. Donenfeld }
25271366992eSJason A. Donenfeld EXPORT_SYMBOL_GPL(random_get_entropy_fallback);
2528e0956dccSArnd Bergmann 
2529e0956dccSArnd Bergmann /**
2530aa6f9c59SJohn Stultz  * do_adjtimex() - Accessor function to NTP __do_adjtimex function
2531e1b6a78bSYang Li  * @txc:	Pointer to kernel_timex structure containing NTP parameters
2532aa6f9c59SJohn Stultz  */
do_adjtimex(struct __kernel_timex * txc)2533ead25417SDeepa Dinamani int do_adjtimex(struct __kernel_timex *txc)
2534aa6f9c59SJohn Stultz {
25357e8eda73SOndrej Mosnacek 	struct audit_ntp_data ad;
253635b603f8SBenjamin ROBIN 	bool offset_set = false;
25371b267793SThomas Gleixner 	bool clock_set = false;
25387d489d15SJohn Stultz 	struct timespec64 ts;
2539e4085693SJohn Stultz 	int ret;
2540e4085693SJohn Stultz 
2541e4085693SJohn Stultz 	/* Validate the data before disabling interrupts */
25421572fa03SArnd Bergmann 	ret = timekeeping_validate_timex(txc);
2543e4085693SJohn Stultz 	if (ret)
2544e4085693SJohn Stultz 		return ret;
2545b8ac29b4SJason A. Donenfeld 	add_device_randomness(txc, sizeof(*txc));
2546e4085693SJohn Stultz 
2547cef90377SJohn Stultz 	if (txc->modes & ADJ_SETOFFSET) {
25481572fa03SArnd Bergmann 		struct timespec64 delta;
2549ae455cb7SAnna-Maria Behnsen 
2550cef90377SJohn Stultz 		delta.tv_sec  = txc->time.tv_sec;
2551cef90377SJohn Stultz 		delta.tv_nsec = txc->time.tv_usec;
2552cef90377SJohn Stultz 		if (!(txc->modes & ADJ_NANO))
2553cef90377SJohn Stultz 			delta.tv_nsec *= 1000;
2554cef90377SJohn Stultz 		ret = timekeeping_inject_offset(&delta);
2555cef90377SJohn Stultz 		if (ret)
2556cef90377SJohn Stultz 			return ret;
25572d87a067SOndrej Mosnacek 
255835b603f8SBenjamin ROBIN 		offset_set = delta.tv_sec != 0;
25592d87a067SOndrej Mosnacek 		audit_tk_injoffset(delta);
2560cef90377SJohn Stultz 	}
2561cef90377SJohn Stultz 
25627e8eda73SOndrej Mosnacek 	audit_ntp_init(&ad);
25637e8eda73SOndrej Mosnacek 
2564d30faff9SArnd Bergmann 	ktime_get_real_ts64(&ts);
2565b8ac29b4SJason A. Donenfeld 	add_device_randomness(&ts, sizeof(ts));
2566aa6f9c59SJohn Stultz 
2567ae455cb7SAnna-Maria Behnsen 	scoped_guard (raw_spinlock_irqsave, &tk_core.lock) {
2568ae455cb7SAnna-Maria Behnsen 		struct timekeeper *tks = &tk_core.shadow_timekeeper;
2569ae455cb7SAnna-Maria Behnsen 		s32 orig_tai, tai;
257006c017fdSJohn Stultz 
2571ae455cb7SAnna-Maria Behnsen 		orig_tai = tai = tks->tai_offset;
25727e8eda73SOndrej Mosnacek 		ret = __do_adjtimex(txc, &ts, &tai, &ad);
257387ace39bSJohn Stultz 
25744e8f8b34SJohn Stultz 		if (tai != orig_tai) {
2575ae455cb7SAnna-Maria Behnsen 			__timekeeping_set_tai_offset(tks, tai);
2576ae455cb7SAnna-Maria Behnsen 			timekeeping_update_from_shadow(&tk_core, TK_CLOCK_WAS_SET);
25771b267793SThomas Gleixner 			clock_set = true;
25789fe7d9a9SAnna-Maria Behnsen 		} else {
2579ae455cb7SAnna-Maria Behnsen 			tk_update_leap_state_all(&tk_core);
25804e8f8b34SJohn Stultz 		}
2581ae455cb7SAnna-Maria Behnsen 	}
258206c017fdSJohn Stultz 
25837e8eda73SOndrej Mosnacek 	audit_ntp_log(&ad);
25847e8eda73SOndrej Mosnacek 
2585b061c7a5SMiroslav Lichvar 	/* Update the multiplier immediately if frequency was set directly */
2586b061c7a5SMiroslav Lichvar 	if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK))
25871b267793SThomas Gleixner 		clock_set |= timekeeping_advance(TK_ADV_FREQ);
2588b061c7a5SMiroslav Lichvar 
25891b267793SThomas Gleixner 	if (clock_set)
25905916be8aSThomas Gleixner 		clock_was_set(CLOCK_SET_WALL);
25916fdda9a9SJohn Stultz 
259235b603f8SBenjamin ROBIN 	ntp_notify_cmos_timer(offset_set);
25937bd36014SJohn Stultz 
259487ace39bSJohn Stultz 	return ret;
259587ace39bSJohn Stultz }
2596aa6f9c59SJohn Stultz 
2597aa6f9c59SJohn Stultz #ifdef CONFIG_NTP_PPS
2598aa6f9c59SJohn Stultz /**
2599aa6f9c59SJohn Stultz  * hardpps() - Accessor function to NTP __hardpps function
2600e1b6a78bSYang Li  * @phase_ts:	Pointer to timespec64 structure representing phase timestamp
2601e1b6a78bSYang Li  * @raw_ts:	Pointer to timespec64 structure representing raw timestamp
2602aa6f9c59SJohn Stultz  */
hardpps(const struct timespec64 * phase_ts,const struct timespec64 * raw_ts)26037ec88e4bSArnd Bergmann void hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts)
2604aa6f9c59SJohn Stultz {
26058c4799b1SAnna-Maria Behnsen 	guard(raw_spinlock_irqsave)(&tk_core.lock);
2606aa6f9c59SJohn Stultz 	__hardpps(phase_ts, raw_ts);
2607aa6f9c59SJohn Stultz }
2608aa6f9c59SJohn Stultz EXPORT_SYMBOL(hardpps);
2609a2d81803SRobert P. J. Day #endif /* CONFIG_NTP_PPS */
2610