1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Kernel timekeeping code and accessor functions. Based on code from
4*4882a593Smuzhiyun * timer.c, moved in commit 8524070b7982.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun #include <linux/timekeeper_internal.h>
7*4882a593Smuzhiyun #include <linux/module.h>
8*4882a593Smuzhiyun #include <linux/interrupt.h>
9*4882a593Smuzhiyun #include <linux/percpu.h>
10*4882a593Smuzhiyun #include <linux/init.h>
11*4882a593Smuzhiyun #include <linux/mm.h>
12*4882a593Smuzhiyun #include <linux/nmi.h>
13*4882a593Smuzhiyun #include <linux/sched.h>
14*4882a593Smuzhiyun #include <linux/sched/loadavg.h>
15*4882a593Smuzhiyun #include <linux/sched/clock.h>
16*4882a593Smuzhiyun #include <linux/syscore_ops.h>
17*4882a593Smuzhiyun #include <linux/clocksource.h>
18*4882a593Smuzhiyun #include <linux/jiffies.h>
19*4882a593Smuzhiyun #include <linux/time.h>
20*4882a593Smuzhiyun #include <linux/timex.h>
21*4882a593Smuzhiyun #include <linux/tick.h>
22*4882a593Smuzhiyun #include <linux/stop_machine.h>
23*4882a593Smuzhiyun #include <linux/pvclock_gtod.h>
24*4882a593Smuzhiyun #include <linux/compiler.h>
25*4882a593Smuzhiyun #include <linux/audit.h>
26*4882a593Smuzhiyun #include <linux/random.h>
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #include "tick-internal.h"
29*4882a593Smuzhiyun #include "ntp_internal.h"
30*4882a593Smuzhiyun #include "timekeeping_internal.h"
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define TK_CLEAR_NTP (1 << 0)
33*4882a593Smuzhiyun #define TK_MIRROR (1 << 1)
34*4882a593Smuzhiyun #define TK_CLOCK_WAS_SET (1 << 2)
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun enum timekeeping_adv_mode {
37*4882a593Smuzhiyun /* Update timekeeper when a tick has passed */
38*4882a593Smuzhiyun TK_ADV_TICK,
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /* Update timekeeper on a direct frequency change */
41*4882a593Smuzhiyun TK_ADV_FREQ
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun DEFINE_RAW_SPINLOCK(timekeeper_lock);
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun * The most important data for readout fits into a single 64 byte
48*4882a593Smuzhiyun * cache line.
49*4882a593Smuzhiyun */
50*4882a593Smuzhiyun static struct {
51*4882a593Smuzhiyun seqcount_raw_spinlock_t seq;
52*4882a593Smuzhiyun struct timekeeper timekeeper;
53*4882a593Smuzhiyun } tk_core ____cacheline_aligned = {
54*4882a593Smuzhiyun .seq = SEQCNT_RAW_SPINLOCK_ZERO(tk_core.seq, &timekeeper_lock),
55*4882a593Smuzhiyun };
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun static struct timekeeper shadow_timekeeper;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /* flag for if timekeeping is suspended */
60*4882a593Smuzhiyun int __read_mostly timekeeping_suspended;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /**
63*4882a593Smuzhiyun * struct tk_fast - NMI safe timekeeper
64*4882a593Smuzhiyun * @seq: Sequence counter for protecting updates. The lowest bit
65*4882a593Smuzhiyun * is the index for the tk_read_base array
66*4882a593Smuzhiyun * @base: tk_read_base array. Access is indexed by the lowest bit of
67*4882a593Smuzhiyun * @seq.
68*4882a593Smuzhiyun *
69*4882a593Smuzhiyun * See @update_fast_timekeeper() below.
70*4882a593Smuzhiyun */
71*4882a593Smuzhiyun struct tk_fast {
72*4882a593Smuzhiyun seqcount_latch_t seq;
73*4882a593Smuzhiyun struct tk_read_base base[2];
74*4882a593Smuzhiyun };
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /* Suspend-time cycles value for halted fast timekeeper. */
77*4882a593Smuzhiyun static u64 cycles_at_suspend;
78*4882a593Smuzhiyun
dummy_clock_read(struct clocksource * cs)79*4882a593Smuzhiyun static u64 dummy_clock_read(struct clocksource *cs)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun if (timekeeping_suspended)
82*4882a593Smuzhiyun return cycles_at_suspend;
83*4882a593Smuzhiyun return local_clock();
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun static struct clocksource dummy_clock = {
87*4882a593Smuzhiyun .read = dummy_clock_read,
88*4882a593Smuzhiyun };
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /*
91*4882a593Smuzhiyun * Boot time initialization which allows local_clock() to be utilized
92*4882a593Smuzhiyun * during early boot when clocksources are not available. local_clock()
93*4882a593Smuzhiyun * returns nanoseconds already so no conversion is required, hence mult=1
94*4882a593Smuzhiyun * and shift=0. When the first proper clocksource is installed then
95*4882a593Smuzhiyun * the fast time keepers are updated with the correct values.
96*4882a593Smuzhiyun */
97*4882a593Smuzhiyun #define FAST_TK_INIT \
98*4882a593Smuzhiyun { \
99*4882a593Smuzhiyun .clock = &dummy_clock, \
100*4882a593Smuzhiyun .mask = CLOCKSOURCE_MASK(64), \
101*4882a593Smuzhiyun .mult = 1, \
102*4882a593Smuzhiyun .shift = 0, \
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun static struct tk_fast tk_fast_mono ____cacheline_aligned = {
106*4882a593Smuzhiyun .seq = SEQCNT_LATCH_ZERO(tk_fast_mono.seq),
107*4882a593Smuzhiyun .base[0] = FAST_TK_INIT,
108*4882a593Smuzhiyun .base[1] = FAST_TK_INIT,
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun static struct tk_fast tk_fast_raw ____cacheline_aligned = {
112*4882a593Smuzhiyun .seq = SEQCNT_LATCH_ZERO(tk_fast_raw.seq),
113*4882a593Smuzhiyun .base[0] = FAST_TK_INIT,
114*4882a593Smuzhiyun .base[1] = FAST_TK_INIT,
115*4882a593Smuzhiyun };
116*4882a593Smuzhiyun
tk_normalize_xtime(struct timekeeper * tk)117*4882a593Smuzhiyun static inline void tk_normalize_xtime(struct timekeeper *tk)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun while (tk->tkr_mono.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_mono.shift)) {
120*4882a593Smuzhiyun tk->tkr_mono.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
121*4882a593Smuzhiyun tk->xtime_sec++;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun while (tk->tkr_raw.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_raw.shift)) {
124*4882a593Smuzhiyun tk->tkr_raw.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
125*4882a593Smuzhiyun tk->raw_sec++;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
tk_xtime(const struct timekeeper * tk)129*4882a593Smuzhiyun static inline struct timespec64 tk_xtime(const struct timekeeper *tk)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun struct timespec64 ts;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun ts.tv_sec = tk->xtime_sec;
134*4882a593Smuzhiyun ts.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
135*4882a593Smuzhiyun return ts;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
tk_set_xtime(struct timekeeper * tk,const struct timespec64 * ts)138*4882a593Smuzhiyun static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun tk->xtime_sec = ts->tv_sec;
141*4882a593Smuzhiyun tk->tkr_mono.xtime_nsec = (u64)ts->tv_nsec << tk->tkr_mono.shift;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
tk_xtime_add(struct timekeeper * tk,const struct timespec64 * ts)144*4882a593Smuzhiyun static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun tk->xtime_sec += ts->tv_sec;
147*4882a593Smuzhiyun tk->tkr_mono.xtime_nsec += (u64)ts->tv_nsec << tk->tkr_mono.shift;
148*4882a593Smuzhiyun tk_normalize_xtime(tk);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
tk_set_wall_to_mono(struct timekeeper * tk,struct timespec64 wtm)151*4882a593Smuzhiyun static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun struct timespec64 tmp;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun * Verify consistency of: offset_real = -wall_to_monotonic
157*4882a593Smuzhiyun * before modifying anything
158*4882a593Smuzhiyun */
159*4882a593Smuzhiyun set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec,
160*4882a593Smuzhiyun -tk->wall_to_monotonic.tv_nsec);
161*4882a593Smuzhiyun WARN_ON_ONCE(tk->offs_real != timespec64_to_ktime(tmp));
162*4882a593Smuzhiyun tk->wall_to_monotonic = wtm;
163*4882a593Smuzhiyun set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
164*4882a593Smuzhiyun tk->offs_real = timespec64_to_ktime(tmp);
165*4882a593Smuzhiyun tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
tk_update_sleep_time(struct timekeeper * tk,ktime_t delta)168*4882a593Smuzhiyun static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun tk->offs_boot = ktime_add(tk->offs_boot, delta);
171*4882a593Smuzhiyun /*
172*4882a593Smuzhiyun * Timespec representation for VDSO update to avoid 64bit division
173*4882a593Smuzhiyun * on every update.
174*4882a593Smuzhiyun */
175*4882a593Smuzhiyun tk->monotonic_to_boot = ktime_to_timespec64(tk->offs_boot);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun /*
179*4882a593Smuzhiyun * tk_clock_read - atomic clocksource read() helper
180*4882a593Smuzhiyun *
181*4882a593Smuzhiyun * This helper is necessary to use in the read paths because, while the
182*4882a593Smuzhiyun * seqcount ensures we don't return a bad value while structures are updated,
183*4882a593Smuzhiyun * it doesn't protect from potential crashes. There is the possibility that
184*4882a593Smuzhiyun * the tkr's clocksource may change between the read reference, and the
185*4882a593Smuzhiyun * clock reference passed to the read function. This can cause crashes if
186*4882a593Smuzhiyun * the wrong clocksource is passed to the wrong read function.
187*4882a593Smuzhiyun * This isn't necessary to use when holding the timekeeper_lock or doing
188*4882a593Smuzhiyun * a read of the fast-timekeeper tkrs (which is protected by its own locking
189*4882a593Smuzhiyun * and update logic).
190*4882a593Smuzhiyun */
tk_clock_read(const struct tk_read_base * tkr)191*4882a593Smuzhiyun static inline u64 tk_clock_read(const struct tk_read_base *tkr)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun struct clocksource *clock = READ_ONCE(tkr->clock);
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun return clock->read(clock);
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_TIMEKEEPING
199*4882a593Smuzhiyun #define WARNING_FREQ (HZ*300) /* 5 minute rate-limiting */
200*4882a593Smuzhiyun
timekeeping_check_update(struct timekeeper * tk,u64 offset)201*4882a593Smuzhiyun static void timekeeping_check_update(struct timekeeper *tk, u64 offset)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun u64 max_cycles = tk->tkr_mono.clock->max_cycles;
205*4882a593Smuzhiyun const char *name = tk->tkr_mono.clock->name;
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun if (offset > max_cycles) {
208*4882a593Smuzhiyun printk_deferred("WARNING: timekeeping: Cycle offset (%lld) is larger than allowed by the '%s' clock's max_cycles value (%lld): time overflow danger\n",
209*4882a593Smuzhiyun offset, name, max_cycles);
210*4882a593Smuzhiyun printk_deferred(" timekeeping: Your kernel is sick, but tries to cope by capping time updates\n");
211*4882a593Smuzhiyun } else {
212*4882a593Smuzhiyun if (offset > (max_cycles >> 1)) {
213*4882a593Smuzhiyun printk_deferred("INFO: timekeeping: Cycle offset (%lld) is larger than the '%s' clock's 50%% safety margin (%lld)\n",
214*4882a593Smuzhiyun offset, name, max_cycles >> 1);
215*4882a593Smuzhiyun printk_deferred(" timekeeping: Your kernel is still fine, but is feeling a bit nervous\n");
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun if (tk->underflow_seen) {
220*4882a593Smuzhiyun if (jiffies - tk->last_warning > WARNING_FREQ) {
221*4882a593Smuzhiyun printk_deferred("WARNING: Underflow in clocksource '%s' observed, time update ignored.\n", name);
222*4882a593Smuzhiyun printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
223*4882a593Smuzhiyun printk_deferred(" Your kernel is probably still fine.\n");
224*4882a593Smuzhiyun tk->last_warning = jiffies;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun tk->underflow_seen = 0;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun if (tk->overflow_seen) {
230*4882a593Smuzhiyun if (jiffies - tk->last_warning > WARNING_FREQ) {
231*4882a593Smuzhiyun printk_deferred("WARNING: Overflow in clocksource '%s' observed, time update capped.\n", name);
232*4882a593Smuzhiyun printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
233*4882a593Smuzhiyun printk_deferred(" Your kernel is probably still fine.\n");
234*4882a593Smuzhiyun tk->last_warning = jiffies;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun tk->overflow_seen = 0;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
timekeeping_get_delta(const struct tk_read_base * tkr)240*4882a593Smuzhiyun static inline u64 timekeeping_get_delta(const struct tk_read_base *tkr)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
243*4882a593Smuzhiyun u64 now, last, mask, max, delta;
244*4882a593Smuzhiyun unsigned int seq;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun /*
247*4882a593Smuzhiyun * Since we're called holding a seqcount, the data may shift
248*4882a593Smuzhiyun * under us while we're doing the calculation. This can cause
249*4882a593Smuzhiyun * false positives, since we'd note a problem but throw the
250*4882a593Smuzhiyun * results away. So nest another seqcount here to atomically
251*4882a593Smuzhiyun * grab the points we are checking with.
252*4882a593Smuzhiyun */
253*4882a593Smuzhiyun do {
254*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
255*4882a593Smuzhiyun now = tk_clock_read(tkr);
256*4882a593Smuzhiyun last = tkr->cycle_last;
257*4882a593Smuzhiyun mask = tkr->mask;
258*4882a593Smuzhiyun max = tkr->clock->max_cycles;
259*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun delta = clocksource_delta(now, last, mask);
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /*
264*4882a593Smuzhiyun * Try to catch underflows by checking if we are seeing small
265*4882a593Smuzhiyun * mask-relative negative values.
266*4882a593Smuzhiyun */
267*4882a593Smuzhiyun if (unlikely((~delta & mask) < (mask >> 3))) {
268*4882a593Smuzhiyun tk->underflow_seen = 1;
269*4882a593Smuzhiyun delta = 0;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun /* Cap delta value to the max_cycles values to avoid mult overflows */
273*4882a593Smuzhiyun if (unlikely(delta > max)) {
274*4882a593Smuzhiyun tk->overflow_seen = 1;
275*4882a593Smuzhiyun delta = tkr->clock->max_cycles;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun return delta;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun #else
timekeeping_check_update(struct timekeeper * tk,u64 offset)281*4882a593Smuzhiyun static inline void timekeeping_check_update(struct timekeeper *tk, u64 offset)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun }
timekeeping_get_delta(const struct tk_read_base * tkr)284*4882a593Smuzhiyun static inline u64 timekeeping_get_delta(const struct tk_read_base *tkr)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun u64 cycle_now, delta;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun /* read clocksource */
289*4882a593Smuzhiyun cycle_now = tk_clock_read(tkr);
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun /* calculate the delta since the last update_wall_time */
292*4882a593Smuzhiyun delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun return delta;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun #endif
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun /**
299*4882a593Smuzhiyun * tk_setup_internals - Set up internals to use clocksource clock.
300*4882a593Smuzhiyun *
301*4882a593Smuzhiyun * @tk: The target timekeeper to setup.
302*4882a593Smuzhiyun * @clock: Pointer to clocksource.
303*4882a593Smuzhiyun *
304*4882a593Smuzhiyun * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
305*4882a593Smuzhiyun * pair and interval request.
306*4882a593Smuzhiyun *
307*4882a593Smuzhiyun * Unless you're the timekeeping code, you should not be using this!
308*4882a593Smuzhiyun */
tk_setup_internals(struct timekeeper * tk,struct clocksource * clock)309*4882a593Smuzhiyun static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun u64 interval;
312*4882a593Smuzhiyun u64 tmp, ntpinterval;
313*4882a593Smuzhiyun struct clocksource *old_clock;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun ++tk->cs_was_changed_seq;
316*4882a593Smuzhiyun old_clock = tk->tkr_mono.clock;
317*4882a593Smuzhiyun tk->tkr_mono.clock = clock;
318*4882a593Smuzhiyun tk->tkr_mono.mask = clock->mask;
319*4882a593Smuzhiyun tk->tkr_mono.cycle_last = tk_clock_read(&tk->tkr_mono);
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun tk->tkr_raw.clock = clock;
322*4882a593Smuzhiyun tk->tkr_raw.mask = clock->mask;
323*4882a593Smuzhiyun tk->tkr_raw.cycle_last = tk->tkr_mono.cycle_last;
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun /* Do the ns -> cycle conversion first, using original mult */
326*4882a593Smuzhiyun tmp = NTP_INTERVAL_LENGTH;
327*4882a593Smuzhiyun tmp <<= clock->shift;
328*4882a593Smuzhiyun ntpinterval = tmp;
329*4882a593Smuzhiyun tmp += clock->mult/2;
330*4882a593Smuzhiyun do_div(tmp, clock->mult);
331*4882a593Smuzhiyun if (tmp == 0)
332*4882a593Smuzhiyun tmp = 1;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun interval = (u64) tmp;
335*4882a593Smuzhiyun tk->cycle_interval = interval;
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun /* Go back from cycles -> shifted ns */
338*4882a593Smuzhiyun tk->xtime_interval = interval * clock->mult;
339*4882a593Smuzhiyun tk->xtime_remainder = ntpinterval - tk->xtime_interval;
340*4882a593Smuzhiyun tk->raw_interval = interval * clock->mult;
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun /* if changing clocks, convert xtime_nsec shift units */
343*4882a593Smuzhiyun if (old_clock) {
344*4882a593Smuzhiyun int shift_change = clock->shift - old_clock->shift;
345*4882a593Smuzhiyun if (shift_change < 0) {
346*4882a593Smuzhiyun tk->tkr_mono.xtime_nsec >>= -shift_change;
347*4882a593Smuzhiyun tk->tkr_raw.xtime_nsec >>= -shift_change;
348*4882a593Smuzhiyun } else {
349*4882a593Smuzhiyun tk->tkr_mono.xtime_nsec <<= shift_change;
350*4882a593Smuzhiyun tk->tkr_raw.xtime_nsec <<= shift_change;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun tk->tkr_mono.shift = clock->shift;
355*4882a593Smuzhiyun tk->tkr_raw.shift = clock->shift;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun tk->ntp_error = 0;
358*4882a593Smuzhiyun tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
359*4882a593Smuzhiyun tk->ntp_tick = ntpinterval << tk->ntp_error_shift;
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun /*
362*4882a593Smuzhiyun * The timekeeper keeps its own mult values for the currently
363*4882a593Smuzhiyun * active clocksource. These value will be adjusted via NTP
364*4882a593Smuzhiyun * to counteract clock drifting.
365*4882a593Smuzhiyun */
366*4882a593Smuzhiyun tk->tkr_mono.mult = clock->mult;
367*4882a593Smuzhiyun tk->tkr_raw.mult = clock->mult;
368*4882a593Smuzhiyun tk->ntp_err_mult = 0;
369*4882a593Smuzhiyun tk->skip_second_overflow = 0;
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun /* Timekeeper helper functions. */
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
default_arch_gettimeoffset(void)375*4882a593Smuzhiyun static u32 default_arch_gettimeoffset(void) { return 0; }
376*4882a593Smuzhiyun u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset;
377*4882a593Smuzhiyun #else
arch_gettimeoffset(void)378*4882a593Smuzhiyun static inline u32 arch_gettimeoffset(void) { return 0; }
379*4882a593Smuzhiyun #endif
380*4882a593Smuzhiyun
timekeeping_delta_to_ns(const struct tk_read_base * tkr,u64 delta)381*4882a593Smuzhiyun static inline u64 timekeeping_delta_to_ns(const struct tk_read_base *tkr, u64 delta)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun u64 nsec;
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun nsec = delta * tkr->mult + tkr->xtime_nsec;
386*4882a593Smuzhiyun nsec >>= tkr->shift;
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun /* If arch requires, add in get_arch_timeoffset() */
389*4882a593Smuzhiyun return nsec + arch_gettimeoffset();
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun
timekeeping_get_ns(const struct tk_read_base * tkr)392*4882a593Smuzhiyun static inline u64 timekeeping_get_ns(const struct tk_read_base *tkr)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun u64 delta;
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun delta = timekeeping_get_delta(tkr);
397*4882a593Smuzhiyun return timekeeping_delta_to_ns(tkr, delta);
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun
timekeeping_cycles_to_ns(const struct tk_read_base * tkr,u64 cycles)400*4882a593Smuzhiyun static inline u64 timekeeping_cycles_to_ns(const struct tk_read_base *tkr, u64 cycles)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun u64 delta;
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun /* calculate the delta since the last update_wall_time */
405*4882a593Smuzhiyun delta = clocksource_delta(cycles, tkr->cycle_last, tkr->mask);
406*4882a593Smuzhiyun return timekeeping_delta_to_ns(tkr, delta);
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun /**
410*4882a593Smuzhiyun * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
411*4882a593Smuzhiyun * @tkr: Timekeeping readout base from which we take the update
412*4882a593Smuzhiyun *
413*4882a593Smuzhiyun * We want to use this from any context including NMI and tracing /
414*4882a593Smuzhiyun * instrumenting the timekeeping code itself.
415*4882a593Smuzhiyun *
416*4882a593Smuzhiyun * Employ the latch technique; see @raw_write_seqcount_latch.
417*4882a593Smuzhiyun *
418*4882a593Smuzhiyun * So if a NMI hits the update of base[0] then it will use base[1]
419*4882a593Smuzhiyun * which is still consistent. In the worst case this can result is a
420*4882a593Smuzhiyun * slightly wrong timestamp (a few nanoseconds). See
421*4882a593Smuzhiyun * @ktime_get_mono_fast_ns.
422*4882a593Smuzhiyun */
update_fast_timekeeper(const struct tk_read_base * tkr,struct tk_fast * tkf)423*4882a593Smuzhiyun static void update_fast_timekeeper(const struct tk_read_base *tkr,
424*4882a593Smuzhiyun struct tk_fast *tkf)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun struct tk_read_base *base = tkf->base;
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun /* Force readers off to base[1] */
429*4882a593Smuzhiyun raw_write_seqcount_latch(&tkf->seq);
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun /* Update base[0] */
432*4882a593Smuzhiyun memcpy(base, tkr, sizeof(*base));
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun /* Force readers back to base[0] */
435*4882a593Smuzhiyun raw_write_seqcount_latch(&tkf->seq);
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun /* Update base[1] */
438*4882a593Smuzhiyun memcpy(base + 1, base, sizeof(*base));
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun /**
442*4882a593Smuzhiyun * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic
443*4882a593Smuzhiyun *
444*4882a593Smuzhiyun * This timestamp is not guaranteed to be monotonic across an update.
445*4882a593Smuzhiyun * The timestamp is calculated by:
446*4882a593Smuzhiyun *
447*4882a593Smuzhiyun * now = base_mono + clock_delta * slope
448*4882a593Smuzhiyun *
449*4882a593Smuzhiyun * So if the update lowers the slope, readers who are forced to the
450*4882a593Smuzhiyun * not yet updated second array are still using the old steeper slope.
451*4882a593Smuzhiyun *
452*4882a593Smuzhiyun * tmono
453*4882a593Smuzhiyun * ^
454*4882a593Smuzhiyun * | o n
455*4882a593Smuzhiyun * | o n
456*4882a593Smuzhiyun * | u
457*4882a593Smuzhiyun * | o
458*4882a593Smuzhiyun * |o
459*4882a593Smuzhiyun * |12345678---> reader order
460*4882a593Smuzhiyun *
461*4882a593Smuzhiyun * o = old slope
462*4882a593Smuzhiyun * u = update
463*4882a593Smuzhiyun * n = new slope
464*4882a593Smuzhiyun *
465*4882a593Smuzhiyun * So reader 6 will observe time going backwards versus reader 5.
466*4882a593Smuzhiyun *
467*4882a593Smuzhiyun * While other CPUs are likely to be able observe that, the only way
468*4882a593Smuzhiyun * for a CPU local observation is when an NMI hits in the middle of
469*4882a593Smuzhiyun * the update. Timestamps taken from that NMI context might be ahead
470*4882a593Smuzhiyun * of the following timestamps. Callers need to be aware of that and
471*4882a593Smuzhiyun * deal with it.
472*4882a593Smuzhiyun */
__ktime_get_fast_ns(struct tk_fast * tkf)473*4882a593Smuzhiyun static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf)
474*4882a593Smuzhiyun {
475*4882a593Smuzhiyun struct tk_read_base *tkr;
476*4882a593Smuzhiyun unsigned int seq;
477*4882a593Smuzhiyun u64 now;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun do {
480*4882a593Smuzhiyun seq = raw_read_seqcount_latch(&tkf->seq);
481*4882a593Smuzhiyun tkr = tkf->base + (seq & 0x01);
482*4882a593Smuzhiyun now = ktime_to_ns(tkr->base);
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun now += timekeeping_delta_to_ns(tkr,
485*4882a593Smuzhiyun clocksource_delta(
486*4882a593Smuzhiyun tk_clock_read(tkr),
487*4882a593Smuzhiyun tkr->cycle_last,
488*4882a593Smuzhiyun tkr->mask));
489*4882a593Smuzhiyun } while (read_seqcount_latch_retry(&tkf->seq, seq));
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun return now;
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun
ktime_get_mono_fast_ns(void)494*4882a593Smuzhiyun u64 ktime_get_mono_fast_ns(void)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun return __ktime_get_fast_ns(&tk_fast_mono);
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
499*4882a593Smuzhiyun
ktime_get_raw_fast_ns(void)500*4882a593Smuzhiyun u64 ktime_get_raw_fast_ns(void)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun return __ktime_get_fast_ns(&tk_fast_raw);
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns);
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun /**
507*4882a593Smuzhiyun * ktime_get_boot_fast_ns - NMI safe and fast access to boot clock.
508*4882a593Smuzhiyun *
509*4882a593Smuzhiyun * To keep it NMI safe since we're accessing from tracing, we're not using a
510*4882a593Smuzhiyun * separate timekeeper with updates to monotonic clock and boot offset
511*4882a593Smuzhiyun * protected with seqcounts. This has the following minor side effects:
512*4882a593Smuzhiyun *
513*4882a593Smuzhiyun * (1) Its possible that a timestamp be taken after the boot offset is updated
514*4882a593Smuzhiyun * but before the timekeeper is updated. If this happens, the new boot offset
515*4882a593Smuzhiyun * is added to the old timekeeping making the clock appear to update slightly
516*4882a593Smuzhiyun * earlier:
517*4882a593Smuzhiyun * CPU 0 CPU 1
518*4882a593Smuzhiyun * timekeeping_inject_sleeptime64()
519*4882a593Smuzhiyun * __timekeeping_inject_sleeptime(tk, delta);
520*4882a593Smuzhiyun * timestamp();
521*4882a593Smuzhiyun * timekeeping_update(tk, TK_CLEAR_NTP...);
522*4882a593Smuzhiyun *
523*4882a593Smuzhiyun * (2) On 32-bit systems, the 64-bit boot offset (tk->offs_boot) may be
524*4882a593Smuzhiyun * partially updated. Since the tk->offs_boot update is a rare event, this
525*4882a593Smuzhiyun * should be a rare occurrence which postprocessing should be able to handle.
526*4882a593Smuzhiyun */
ktime_get_boot_fast_ns(void)527*4882a593Smuzhiyun u64 notrace ktime_get_boot_fast_ns(void)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun return (ktime_get_mono_fast_ns() + ktime_to_ns(tk->offs_boot));
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns);
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun /*
536*4882a593Smuzhiyun * See comment for __ktime_get_fast_ns() vs. timestamp ordering
537*4882a593Smuzhiyun */
__ktime_get_real_fast(struct tk_fast * tkf,u64 * mono)538*4882a593Smuzhiyun static __always_inline u64 __ktime_get_real_fast(struct tk_fast *tkf, u64 *mono)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun struct tk_read_base *tkr;
541*4882a593Smuzhiyun u64 basem, baser, delta;
542*4882a593Smuzhiyun unsigned int seq;
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun do {
545*4882a593Smuzhiyun seq = raw_read_seqcount_latch(&tkf->seq);
546*4882a593Smuzhiyun tkr = tkf->base + (seq & 0x01);
547*4882a593Smuzhiyun basem = ktime_to_ns(tkr->base);
548*4882a593Smuzhiyun baser = ktime_to_ns(tkr->base_real);
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun delta = timekeeping_delta_to_ns(tkr,
551*4882a593Smuzhiyun clocksource_delta(tk_clock_read(tkr),
552*4882a593Smuzhiyun tkr->cycle_last, tkr->mask));
553*4882a593Smuzhiyun } while (read_seqcount_latch_retry(&tkf->seq, seq));
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun if (mono)
556*4882a593Smuzhiyun *mono = basem + delta;
557*4882a593Smuzhiyun return baser + delta;
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun /**
561*4882a593Smuzhiyun * ktime_get_real_fast_ns: - NMI safe and fast access to clock realtime.
562*4882a593Smuzhiyun */
ktime_get_real_fast_ns(void)563*4882a593Smuzhiyun u64 ktime_get_real_fast_ns(void)
564*4882a593Smuzhiyun {
565*4882a593Smuzhiyun return __ktime_get_real_fast(&tk_fast_mono, NULL);
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ktime_get_real_fast_ns);
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun /**
570*4882a593Smuzhiyun * ktime_get_fast_timestamps: - NMI safe timestamps
571*4882a593Smuzhiyun * @snapshot: Pointer to timestamp storage
572*4882a593Smuzhiyun *
573*4882a593Smuzhiyun * Stores clock monotonic, boottime and realtime timestamps.
574*4882a593Smuzhiyun *
575*4882a593Smuzhiyun * Boot time is a racy access on 32bit systems if the sleep time injection
576*4882a593Smuzhiyun * happens late during resume and not in timekeeping_resume(). That could
577*4882a593Smuzhiyun * be avoided by expanding struct tk_read_base with boot offset for 32bit
578*4882a593Smuzhiyun * and adding more overhead to the update. As this is a hard to observe
579*4882a593Smuzhiyun * once per resume event which can be filtered with reasonable effort using
580*4882a593Smuzhiyun * the accurate mono/real timestamps, it's probably not worth the trouble.
581*4882a593Smuzhiyun *
582*4882a593Smuzhiyun * Aside of that it might be possible on 32 and 64 bit to observe the
583*4882a593Smuzhiyun * following when the sleep time injection happens late:
584*4882a593Smuzhiyun *
585*4882a593Smuzhiyun * CPU 0 CPU 1
586*4882a593Smuzhiyun * timekeeping_resume()
587*4882a593Smuzhiyun * ktime_get_fast_timestamps()
588*4882a593Smuzhiyun * mono, real = __ktime_get_real_fast()
589*4882a593Smuzhiyun * inject_sleep_time()
590*4882a593Smuzhiyun * update boot offset
591*4882a593Smuzhiyun * boot = mono + bootoffset;
592*4882a593Smuzhiyun *
593*4882a593Smuzhiyun * That means that boot time already has the sleep time adjustment, but
594*4882a593Smuzhiyun * real time does not. On the next readout both are in sync again.
595*4882a593Smuzhiyun *
596*4882a593Smuzhiyun * Preventing this for 64bit is not really feasible without destroying the
597*4882a593Smuzhiyun * careful cache layout of the timekeeper because the sequence count and
598*4882a593Smuzhiyun * struct tk_read_base would then need two cache lines instead of one.
599*4882a593Smuzhiyun *
600*4882a593Smuzhiyun * Access to the time keeper clock source is disabled accross the innermost
601*4882a593Smuzhiyun * steps of suspend/resume. The accessors still work, but the timestamps
602*4882a593Smuzhiyun * are frozen until time keeping is resumed which happens very early.
603*4882a593Smuzhiyun *
604*4882a593Smuzhiyun * For regular suspend/resume there is no observable difference vs. sched
605*4882a593Smuzhiyun * clock, but it might affect some of the nasty low level debug printks.
606*4882a593Smuzhiyun *
607*4882a593Smuzhiyun * OTOH, access to sched clock is not guaranteed accross suspend/resume on
608*4882a593Smuzhiyun * all systems either so it depends on the hardware in use.
609*4882a593Smuzhiyun *
610*4882a593Smuzhiyun * If that turns out to be a real problem then this could be mitigated by
611*4882a593Smuzhiyun * using sched clock in a similar way as during early boot. But it's not as
612*4882a593Smuzhiyun * trivial as on early boot because it needs some careful protection
613*4882a593Smuzhiyun * against the clock monotonic timestamp jumping backwards on resume.
614*4882a593Smuzhiyun */
ktime_get_fast_timestamps(struct ktime_timestamps * snapshot)615*4882a593Smuzhiyun void ktime_get_fast_timestamps(struct ktime_timestamps *snapshot)
616*4882a593Smuzhiyun {
617*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun snapshot->real = __ktime_get_real_fast(&tk_fast_mono, &snapshot->mono);
620*4882a593Smuzhiyun snapshot->boot = snapshot->mono + ktime_to_ns(data_race(tk->offs_boot));
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun /**
624*4882a593Smuzhiyun * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource.
625*4882a593Smuzhiyun * @tk: Timekeeper to snapshot.
626*4882a593Smuzhiyun *
627*4882a593Smuzhiyun * It generally is unsafe to access the clocksource after timekeeping has been
628*4882a593Smuzhiyun * suspended, so take a snapshot of the readout base of @tk and use it as the
629*4882a593Smuzhiyun * fast timekeeper's readout base while suspended. It will return the same
630*4882a593Smuzhiyun * number of cycles every time until timekeeping is resumed at which time the
631*4882a593Smuzhiyun * proper readout base for the fast timekeeper will be restored automatically.
632*4882a593Smuzhiyun */
halt_fast_timekeeper(const struct timekeeper * tk)633*4882a593Smuzhiyun static void halt_fast_timekeeper(const struct timekeeper *tk)
634*4882a593Smuzhiyun {
635*4882a593Smuzhiyun static struct tk_read_base tkr_dummy;
636*4882a593Smuzhiyun const struct tk_read_base *tkr = &tk->tkr_mono;
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
639*4882a593Smuzhiyun cycles_at_suspend = tk_clock_read(tkr);
640*4882a593Smuzhiyun tkr_dummy.clock = &dummy_clock;
641*4882a593Smuzhiyun tkr_dummy.base_real = tkr->base + tk->offs_real;
642*4882a593Smuzhiyun update_fast_timekeeper(&tkr_dummy, &tk_fast_mono);
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun tkr = &tk->tkr_raw;
645*4882a593Smuzhiyun memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
646*4882a593Smuzhiyun tkr_dummy.clock = &dummy_clock;
647*4882a593Smuzhiyun update_fast_timekeeper(&tkr_dummy, &tk_fast_raw);
648*4882a593Smuzhiyun }
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
651*4882a593Smuzhiyun
update_pvclock_gtod(struct timekeeper * tk,bool was_set)652*4882a593Smuzhiyun static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
653*4882a593Smuzhiyun {
654*4882a593Smuzhiyun raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun /**
658*4882a593Smuzhiyun * pvclock_gtod_register_notifier - register a pvclock timedata update listener
659*4882a593Smuzhiyun */
pvclock_gtod_register_notifier(struct notifier_block * nb)660*4882a593Smuzhiyun int pvclock_gtod_register_notifier(struct notifier_block *nb)
661*4882a593Smuzhiyun {
662*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
663*4882a593Smuzhiyun unsigned long flags;
664*4882a593Smuzhiyun int ret;
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun raw_spin_lock_irqsave(&timekeeper_lock, flags);
667*4882a593Smuzhiyun ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
668*4882a593Smuzhiyun update_pvclock_gtod(tk, true);
669*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun return ret;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun /**
676*4882a593Smuzhiyun * pvclock_gtod_unregister_notifier - unregister a pvclock
677*4882a593Smuzhiyun * timedata update listener
678*4882a593Smuzhiyun */
pvclock_gtod_unregister_notifier(struct notifier_block * nb)679*4882a593Smuzhiyun int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
680*4882a593Smuzhiyun {
681*4882a593Smuzhiyun unsigned long flags;
682*4882a593Smuzhiyun int ret;
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun raw_spin_lock_irqsave(&timekeeper_lock, flags);
685*4882a593Smuzhiyun ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
686*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun return ret;
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun /*
693*4882a593Smuzhiyun * tk_update_leap_state - helper to update the next_leap_ktime
694*4882a593Smuzhiyun */
tk_update_leap_state(struct timekeeper * tk)695*4882a593Smuzhiyun static inline void tk_update_leap_state(struct timekeeper *tk)
696*4882a593Smuzhiyun {
697*4882a593Smuzhiyun tk->next_leap_ktime = ntp_get_next_leap();
698*4882a593Smuzhiyun if (tk->next_leap_ktime != KTIME_MAX)
699*4882a593Smuzhiyun /* Convert to monotonic time */
700*4882a593Smuzhiyun tk->next_leap_ktime = ktime_sub(tk->next_leap_ktime, tk->offs_real);
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun /*
704*4882a593Smuzhiyun * Update the ktime_t based scalar nsec members of the timekeeper
705*4882a593Smuzhiyun */
tk_update_ktime_data(struct timekeeper * tk)706*4882a593Smuzhiyun static inline void tk_update_ktime_data(struct timekeeper *tk)
707*4882a593Smuzhiyun {
708*4882a593Smuzhiyun u64 seconds;
709*4882a593Smuzhiyun u32 nsec;
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun /*
712*4882a593Smuzhiyun * The xtime based monotonic readout is:
713*4882a593Smuzhiyun * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();
714*4882a593Smuzhiyun * The ktime based monotonic readout is:
715*4882a593Smuzhiyun * nsec = base_mono + now();
716*4882a593Smuzhiyun * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
717*4882a593Smuzhiyun */
718*4882a593Smuzhiyun seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
719*4882a593Smuzhiyun nsec = (u32) tk->wall_to_monotonic.tv_nsec;
720*4882a593Smuzhiyun tk->tkr_mono.base = ns_to_ktime(seconds * NSEC_PER_SEC + nsec);
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun /*
723*4882a593Smuzhiyun * The sum of the nanoseconds portions of xtime and
724*4882a593Smuzhiyun * wall_to_monotonic can be greater/equal one second. Take
725*4882a593Smuzhiyun * this into account before updating tk->ktime_sec.
726*4882a593Smuzhiyun */
727*4882a593Smuzhiyun nsec += (u32)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
728*4882a593Smuzhiyun if (nsec >= NSEC_PER_SEC)
729*4882a593Smuzhiyun seconds++;
730*4882a593Smuzhiyun tk->ktime_sec = seconds;
731*4882a593Smuzhiyun
732*4882a593Smuzhiyun /* Update the monotonic raw base */
733*4882a593Smuzhiyun tk->tkr_raw.base = ns_to_ktime(tk->raw_sec * NSEC_PER_SEC);
734*4882a593Smuzhiyun }
735*4882a593Smuzhiyun
736*4882a593Smuzhiyun /* must hold timekeeper_lock */
timekeeping_update(struct timekeeper * tk,unsigned int action)737*4882a593Smuzhiyun static void timekeeping_update(struct timekeeper *tk, unsigned int action)
738*4882a593Smuzhiyun {
739*4882a593Smuzhiyun if (action & TK_CLEAR_NTP) {
740*4882a593Smuzhiyun tk->ntp_error = 0;
741*4882a593Smuzhiyun ntp_clear();
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun tk_update_leap_state(tk);
745*4882a593Smuzhiyun tk_update_ktime_data(tk);
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun update_vsyscall(tk);
748*4882a593Smuzhiyun update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun tk->tkr_mono.base_real = tk->tkr_mono.base + tk->offs_real;
751*4882a593Smuzhiyun update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono);
752*4882a593Smuzhiyun update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw);
753*4882a593Smuzhiyun
754*4882a593Smuzhiyun if (action & TK_CLOCK_WAS_SET)
755*4882a593Smuzhiyun tk->clock_was_set_seq++;
756*4882a593Smuzhiyun /*
757*4882a593Smuzhiyun * The mirroring of the data to the shadow-timekeeper needs
758*4882a593Smuzhiyun * to happen last here to ensure we don't over-write the
759*4882a593Smuzhiyun * timekeeper structure on the next update with stale data
760*4882a593Smuzhiyun */
761*4882a593Smuzhiyun if (action & TK_MIRROR)
762*4882a593Smuzhiyun memcpy(&shadow_timekeeper, &tk_core.timekeeper,
763*4882a593Smuzhiyun sizeof(tk_core.timekeeper));
764*4882a593Smuzhiyun }
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun /**
767*4882a593Smuzhiyun * timekeeping_forward_now - update clock to the current time
768*4882a593Smuzhiyun *
769*4882a593Smuzhiyun * Forward the current clock to update its state since the last call to
770*4882a593Smuzhiyun * update_wall_time(). This is useful before significant clock changes,
771*4882a593Smuzhiyun * as it avoids having to deal with this time offset explicitly.
772*4882a593Smuzhiyun */
timekeeping_forward_now(struct timekeeper * tk)773*4882a593Smuzhiyun static void timekeeping_forward_now(struct timekeeper *tk)
774*4882a593Smuzhiyun {
775*4882a593Smuzhiyun u64 cycle_now, delta;
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun cycle_now = tk_clock_read(&tk->tkr_mono);
778*4882a593Smuzhiyun delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
779*4882a593Smuzhiyun tk->tkr_mono.cycle_last = cycle_now;
780*4882a593Smuzhiyun tk->tkr_raw.cycle_last = cycle_now;
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun tk->tkr_mono.xtime_nsec += delta * tk->tkr_mono.mult;
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun /* If arch requires, add in get_arch_timeoffset() */
785*4882a593Smuzhiyun tk->tkr_mono.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_mono.shift;
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun
788*4882a593Smuzhiyun tk->tkr_raw.xtime_nsec += delta * tk->tkr_raw.mult;
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun /* If arch requires, add in get_arch_timeoffset() */
791*4882a593Smuzhiyun tk->tkr_raw.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_raw.shift;
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun tk_normalize_xtime(tk);
794*4882a593Smuzhiyun }
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun /**
797*4882a593Smuzhiyun * ktime_get_real_ts64 - Returns the time of day in a timespec64.
798*4882a593Smuzhiyun * @ts: pointer to the timespec to be set
799*4882a593Smuzhiyun *
800*4882a593Smuzhiyun * Returns the time of day in a timespec64 (WARN if suspended).
801*4882a593Smuzhiyun */
ktime_get_real_ts64(struct timespec64 * ts)802*4882a593Smuzhiyun void ktime_get_real_ts64(struct timespec64 *ts)
803*4882a593Smuzhiyun {
804*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
805*4882a593Smuzhiyun unsigned int seq;
806*4882a593Smuzhiyun u64 nsecs;
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun WARN_ON(timekeeping_suspended);
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun do {
811*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
812*4882a593Smuzhiyun
813*4882a593Smuzhiyun ts->tv_sec = tk->xtime_sec;
814*4882a593Smuzhiyun nsecs = timekeeping_get_ns(&tk->tkr_mono);
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun ts->tv_nsec = 0;
819*4882a593Smuzhiyun timespec64_add_ns(ts, nsecs);
820*4882a593Smuzhiyun }
821*4882a593Smuzhiyun EXPORT_SYMBOL(ktime_get_real_ts64);
822*4882a593Smuzhiyun
ktime_get(void)823*4882a593Smuzhiyun ktime_t ktime_get(void)
824*4882a593Smuzhiyun {
825*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
826*4882a593Smuzhiyun unsigned int seq;
827*4882a593Smuzhiyun ktime_t base;
828*4882a593Smuzhiyun u64 nsecs;
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun WARN_ON(timekeeping_suspended);
831*4882a593Smuzhiyun
832*4882a593Smuzhiyun do {
833*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
834*4882a593Smuzhiyun base = tk->tkr_mono.base;
835*4882a593Smuzhiyun nsecs = timekeeping_get_ns(&tk->tkr_mono);
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
838*4882a593Smuzhiyun
839*4882a593Smuzhiyun return ktime_add_ns(base, nsecs);
840*4882a593Smuzhiyun }
841*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ktime_get);
842*4882a593Smuzhiyun
ktime_get_resolution_ns(void)843*4882a593Smuzhiyun u32 ktime_get_resolution_ns(void)
844*4882a593Smuzhiyun {
845*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
846*4882a593Smuzhiyun unsigned int seq;
847*4882a593Smuzhiyun u32 nsecs;
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun WARN_ON(timekeeping_suspended);
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun do {
852*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
853*4882a593Smuzhiyun nsecs = tk->tkr_mono.mult >> tk->tkr_mono.shift;
854*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun return nsecs;
857*4882a593Smuzhiyun }
858*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ktime_get_resolution_ns);
859*4882a593Smuzhiyun
860*4882a593Smuzhiyun static ktime_t *offsets[TK_OFFS_MAX] = {
861*4882a593Smuzhiyun [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real,
862*4882a593Smuzhiyun [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot,
863*4882a593Smuzhiyun [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai,
864*4882a593Smuzhiyun };
865*4882a593Smuzhiyun
ktime_get_with_offset(enum tk_offsets offs)866*4882a593Smuzhiyun ktime_t ktime_get_with_offset(enum tk_offsets offs)
867*4882a593Smuzhiyun {
868*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
869*4882a593Smuzhiyun unsigned int seq;
870*4882a593Smuzhiyun ktime_t base, *offset = offsets[offs];
871*4882a593Smuzhiyun u64 nsecs;
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun WARN_ON(timekeeping_suspended);
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun do {
876*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
877*4882a593Smuzhiyun base = ktime_add(tk->tkr_mono.base, *offset);
878*4882a593Smuzhiyun nsecs = timekeeping_get_ns(&tk->tkr_mono);
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
881*4882a593Smuzhiyun
882*4882a593Smuzhiyun return ktime_add_ns(base, nsecs);
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun }
885*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ktime_get_with_offset);
886*4882a593Smuzhiyun
ktime_get_coarse_with_offset(enum tk_offsets offs)887*4882a593Smuzhiyun ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs)
888*4882a593Smuzhiyun {
889*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
890*4882a593Smuzhiyun unsigned int seq;
891*4882a593Smuzhiyun ktime_t base, *offset = offsets[offs];
892*4882a593Smuzhiyun u64 nsecs;
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun WARN_ON(timekeeping_suspended);
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun do {
897*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
898*4882a593Smuzhiyun base = ktime_add(tk->tkr_mono.base, *offset);
899*4882a593Smuzhiyun nsecs = tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
900*4882a593Smuzhiyun
901*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
902*4882a593Smuzhiyun
903*4882a593Smuzhiyun return ktime_add_ns(base, nsecs);
904*4882a593Smuzhiyun }
905*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ktime_get_coarse_with_offset);
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun /**
908*4882a593Smuzhiyun * ktime_mono_to_any() - convert mononotic time to any other time
909*4882a593Smuzhiyun * @tmono: time to convert.
910*4882a593Smuzhiyun * @offs: which offset to use
911*4882a593Smuzhiyun */
ktime_mono_to_any(ktime_t tmono,enum tk_offsets offs)912*4882a593Smuzhiyun ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
913*4882a593Smuzhiyun {
914*4882a593Smuzhiyun ktime_t *offset = offsets[offs];
915*4882a593Smuzhiyun unsigned int seq;
916*4882a593Smuzhiyun ktime_t tconv;
917*4882a593Smuzhiyun
918*4882a593Smuzhiyun do {
919*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
920*4882a593Smuzhiyun tconv = ktime_add(tmono, *offset);
921*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun return tconv;
924*4882a593Smuzhiyun }
925*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ktime_mono_to_any);
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun /**
928*4882a593Smuzhiyun * ktime_get_raw - Returns the raw monotonic time in ktime_t format
929*4882a593Smuzhiyun */
ktime_get_raw(void)930*4882a593Smuzhiyun ktime_t ktime_get_raw(void)
931*4882a593Smuzhiyun {
932*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
933*4882a593Smuzhiyun unsigned int seq;
934*4882a593Smuzhiyun ktime_t base;
935*4882a593Smuzhiyun u64 nsecs;
936*4882a593Smuzhiyun
937*4882a593Smuzhiyun do {
938*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
939*4882a593Smuzhiyun base = tk->tkr_raw.base;
940*4882a593Smuzhiyun nsecs = timekeeping_get_ns(&tk->tkr_raw);
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
943*4882a593Smuzhiyun
944*4882a593Smuzhiyun return ktime_add_ns(base, nsecs);
945*4882a593Smuzhiyun }
946*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ktime_get_raw);
947*4882a593Smuzhiyun
948*4882a593Smuzhiyun /**
949*4882a593Smuzhiyun * ktime_get_ts64 - get the monotonic clock in timespec64 format
950*4882a593Smuzhiyun * @ts: pointer to timespec variable
951*4882a593Smuzhiyun *
952*4882a593Smuzhiyun * The function calculates the monotonic clock from the realtime
953*4882a593Smuzhiyun * clock and the wall_to_monotonic offset and stores the result
954*4882a593Smuzhiyun * in normalized timespec64 format in the variable pointed to by @ts.
955*4882a593Smuzhiyun */
ktime_get_ts64(struct timespec64 * ts)956*4882a593Smuzhiyun void ktime_get_ts64(struct timespec64 *ts)
957*4882a593Smuzhiyun {
958*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
959*4882a593Smuzhiyun struct timespec64 tomono;
960*4882a593Smuzhiyun unsigned int seq;
961*4882a593Smuzhiyun u64 nsec;
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun WARN_ON(timekeeping_suspended);
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun do {
966*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
967*4882a593Smuzhiyun ts->tv_sec = tk->xtime_sec;
968*4882a593Smuzhiyun nsec = timekeeping_get_ns(&tk->tkr_mono);
969*4882a593Smuzhiyun tomono = tk->wall_to_monotonic;
970*4882a593Smuzhiyun
971*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
972*4882a593Smuzhiyun
973*4882a593Smuzhiyun ts->tv_sec += tomono.tv_sec;
974*4882a593Smuzhiyun ts->tv_nsec = 0;
975*4882a593Smuzhiyun timespec64_add_ns(ts, nsec + tomono.tv_nsec);
976*4882a593Smuzhiyun }
977*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ktime_get_ts64);
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun /**
980*4882a593Smuzhiyun * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC
981*4882a593Smuzhiyun *
982*4882a593Smuzhiyun * Returns the seconds portion of CLOCK_MONOTONIC with a single non
983*4882a593Smuzhiyun * serialized read. tk->ktime_sec is of type 'unsigned long' so this
984*4882a593Smuzhiyun * works on both 32 and 64 bit systems. On 32 bit systems the readout
985*4882a593Smuzhiyun * covers ~136 years of uptime which should be enough to prevent
986*4882a593Smuzhiyun * premature wrap arounds.
987*4882a593Smuzhiyun */
ktime_get_seconds(void)988*4882a593Smuzhiyun time64_t ktime_get_seconds(void)
989*4882a593Smuzhiyun {
990*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
991*4882a593Smuzhiyun
992*4882a593Smuzhiyun WARN_ON(timekeeping_suspended);
993*4882a593Smuzhiyun return tk->ktime_sec;
994*4882a593Smuzhiyun }
995*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ktime_get_seconds);
996*4882a593Smuzhiyun
997*4882a593Smuzhiyun /**
998*4882a593Smuzhiyun * ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME
999*4882a593Smuzhiyun *
1000*4882a593Smuzhiyun * Returns the wall clock seconds since 1970. This replaces the
1001*4882a593Smuzhiyun * get_seconds() interface which is not y2038 safe on 32bit systems.
1002*4882a593Smuzhiyun *
1003*4882a593Smuzhiyun * For 64bit systems the fast access to tk->xtime_sec is preserved. On
1004*4882a593Smuzhiyun * 32bit systems the access must be protected with the sequence
1005*4882a593Smuzhiyun * counter to provide "atomic" access to the 64bit tk->xtime_sec
1006*4882a593Smuzhiyun * value.
1007*4882a593Smuzhiyun */
ktime_get_real_seconds(void)1008*4882a593Smuzhiyun time64_t ktime_get_real_seconds(void)
1009*4882a593Smuzhiyun {
1010*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1011*4882a593Smuzhiyun time64_t seconds;
1012*4882a593Smuzhiyun unsigned int seq;
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_64BIT))
1015*4882a593Smuzhiyun return tk->xtime_sec;
1016*4882a593Smuzhiyun
1017*4882a593Smuzhiyun do {
1018*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
1019*4882a593Smuzhiyun seconds = tk->xtime_sec;
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun return seconds;
1024*4882a593Smuzhiyun }
1025*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ktime_get_real_seconds);
1026*4882a593Smuzhiyun
1027*4882a593Smuzhiyun /**
1028*4882a593Smuzhiyun * __ktime_get_real_seconds - The same as ktime_get_real_seconds
1029*4882a593Smuzhiyun * but without the sequence counter protect. This internal function
1030*4882a593Smuzhiyun * is called just when timekeeping lock is already held.
1031*4882a593Smuzhiyun */
__ktime_get_real_seconds(void)1032*4882a593Smuzhiyun noinstr time64_t __ktime_get_real_seconds(void)
1033*4882a593Smuzhiyun {
1034*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1035*4882a593Smuzhiyun
1036*4882a593Smuzhiyun return tk->xtime_sec;
1037*4882a593Smuzhiyun }
1038*4882a593Smuzhiyun
1039*4882a593Smuzhiyun /**
1040*4882a593Smuzhiyun * ktime_get_snapshot - snapshots the realtime/monotonic raw clocks with counter
1041*4882a593Smuzhiyun * @systime_snapshot: pointer to struct receiving the system time snapshot
1042*4882a593Smuzhiyun */
ktime_get_snapshot(struct system_time_snapshot * systime_snapshot)1043*4882a593Smuzhiyun void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
1044*4882a593Smuzhiyun {
1045*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1046*4882a593Smuzhiyun unsigned int seq;
1047*4882a593Smuzhiyun ktime_t base_raw;
1048*4882a593Smuzhiyun ktime_t base_real;
1049*4882a593Smuzhiyun u64 nsec_raw;
1050*4882a593Smuzhiyun u64 nsec_real;
1051*4882a593Smuzhiyun u64 now;
1052*4882a593Smuzhiyun
1053*4882a593Smuzhiyun WARN_ON_ONCE(timekeeping_suspended);
1054*4882a593Smuzhiyun
1055*4882a593Smuzhiyun do {
1056*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
1057*4882a593Smuzhiyun now = tk_clock_read(&tk->tkr_mono);
1058*4882a593Smuzhiyun systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq;
1059*4882a593Smuzhiyun systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq;
1060*4882a593Smuzhiyun base_real = ktime_add(tk->tkr_mono.base,
1061*4882a593Smuzhiyun tk_core.timekeeper.offs_real);
1062*4882a593Smuzhiyun base_raw = tk->tkr_raw.base;
1063*4882a593Smuzhiyun nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, now);
1064*4882a593Smuzhiyun nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw, now);
1065*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
1066*4882a593Smuzhiyun
1067*4882a593Smuzhiyun systime_snapshot->cycles = now;
1068*4882a593Smuzhiyun systime_snapshot->real = ktime_add_ns(base_real, nsec_real);
1069*4882a593Smuzhiyun systime_snapshot->raw = ktime_add_ns(base_raw, nsec_raw);
1070*4882a593Smuzhiyun }
1071*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ktime_get_snapshot);
1072*4882a593Smuzhiyun
1073*4882a593Smuzhiyun /* Scale base by mult/div checking for overflow */
scale64_check_overflow(u64 mult,u64 div,u64 * base)1074*4882a593Smuzhiyun static int scale64_check_overflow(u64 mult, u64 div, u64 *base)
1075*4882a593Smuzhiyun {
1076*4882a593Smuzhiyun u64 tmp, rem;
1077*4882a593Smuzhiyun
1078*4882a593Smuzhiyun tmp = div64_u64_rem(*base, div, &rem);
1079*4882a593Smuzhiyun
1080*4882a593Smuzhiyun if (((int)sizeof(u64)*8 - fls64(mult) < fls64(tmp)) ||
1081*4882a593Smuzhiyun ((int)sizeof(u64)*8 - fls64(mult) < fls64(rem)))
1082*4882a593Smuzhiyun return -EOVERFLOW;
1083*4882a593Smuzhiyun tmp *= mult;
1084*4882a593Smuzhiyun
1085*4882a593Smuzhiyun rem = div64_u64(rem * mult, div);
1086*4882a593Smuzhiyun *base = tmp + rem;
1087*4882a593Smuzhiyun return 0;
1088*4882a593Smuzhiyun }
1089*4882a593Smuzhiyun
1090*4882a593Smuzhiyun /**
1091*4882a593Smuzhiyun * adjust_historical_crosststamp - adjust crosstimestamp previous to current interval
1092*4882a593Smuzhiyun * @history: Snapshot representing start of history
1093*4882a593Smuzhiyun * @partial_history_cycles: Cycle offset into history (fractional part)
1094*4882a593Smuzhiyun * @total_history_cycles: Total history length in cycles
1095*4882a593Smuzhiyun * @discontinuity: True indicates clock was set on history period
1096*4882a593Smuzhiyun * @ts: Cross timestamp that should be adjusted using
1097*4882a593Smuzhiyun * partial/total ratio
1098*4882a593Smuzhiyun *
1099*4882a593Smuzhiyun * Helper function used by get_device_system_crosststamp() to correct the
1100*4882a593Smuzhiyun * crosstimestamp corresponding to the start of the current interval to the
1101*4882a593Smuzhiyun * system counter value (timestamp point) provided by the driver. The
1102*4882a593Smuzhiyun * total_history_* quantities are the total history starting at the provided
1103*4882a593Smuzhiyun * reference point and ending at the start of the current interval. The cycle
1104*4882a593Smuzhiyun * count between the driver timestamp point and the start of the current
1105*4882a593Smuzhiyun * interval is partial_history_cycles.
1106*4882a593Smuzhiyun */
adjust_historical_crosststamp(struct system_time_snapshot * history,u64 partial_history_cycles,u64 total_history_cycles,bool discontinuity,struct system_device_crosststamp * ts)1107*4882a593Smuzhiyun static int adjust_historical_crosststamp(struct system_time_snapshot *history,
1108*4882a593Smuzhiyun u64 partial_history_cycles,
1109*4882a593Smuzhiyun u64 total_history_cycles,
1110*4882a593Smuzhiyun bool discontinuity,
1111*4882a593Smuzhiyun struct system_device_crosststamp *ts)
1112*4882a593Smuzhiyun {
1113*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1114*4882a593Smuzhiyun u64 corr_raw, corr_real;
1115*4882a593Smuzhiyun bool interp_forward;
1116*4882a593Smuzhiyun int ret;
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun if (total_history_cycles == 0 || partial_history_cycles == 0)
1119*4882a593Smuzhiyun return 0;
1120*4882a593Smuzhiyun
1121*4882a593Smuzhiyun /* Interpolate shortest distance from beginning or end of history */
1122*4882a593Smuzhiyun interp_forward = partial_history_cycles > total_history_cycles / 2;
1123*4882a593Smuzhiyun partial_history_cycles = interp_forward ?
1124*4882a593Smuzhiyun total_history_cycles - partial_history_cycles :
1125*4882a593Smuzhiyun partial_history_cycles;
1126*4882a593Smuzhiyun
1127*4882a593Smuzhiyun /*
1128*4882a593Smuzhiyun * Scale the monotonic raw time delta by:
1129*4882a593Smuzhiyun * partial_history_cycles / total_history_cycles
1130*4882a593Smuzhiyun */
1131*4882a593Smuzhiyun corr_raw = (u64)ktime_to_ns(
1132*4882a593Smuzhiyun ktime_sub(ts->sys_monoraw, history->raw));
1133*4882a593Smuzhiyun ret = scale64_check_overflow(partial_history_cycles,
1134*4882a593Smuzhiyun total_history_cycles, &corr_raw);
1135*4882a593Smuzhiyun if (ret)
1136*4882a593Smuzhiyun return ret;
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun /*
1139*4882a593Smuzhiyun * If there is a discontinuity in the history, scale monotonic raw
1140*4882a593Smuzhiyun * correction by:
1141*4882a593Smuzhiyun * mult(real)/mult(raw) yielding the realtime correction
1142*4882a593Smuzhiyun * Otherwise, calculate the realtime correction similar to monotonic
1143*4882a593Smuzhiyun * raw calculation
1144*4882a593Smuzhiyun */
1145*4882a593Smuzhiyun if (discontinuity) {
1146*4882a593Smuzhiyun corr_real = mul_u64_u32_div
1147*4882a593Smuzhiyun (corr_raw, tk->tkr_mono.mult, tk->tkr_raw.mult);
1148*4882a593Smuzhiyun } else {
1149*4882a593Smuzhiyun corr_real = (u64)ktime_to_ns(
1150*4882a593Smuzhiyun ktime_sub(ts->sys_realtime, history->real));
1151*4882a593Smuzhiyun ret = scale64_check_overflow(partial_history_cycles,
1152*4882a593Smuzhiyun total_history_cycles, &corr_real);
1153*4882a593Smuzhiyun if (ret)
1154*4882a593Smuzhiyun return ret;
1155*4882a593Smuzhiyun }
1156*4882a593Smuzhiyun
1157*4882a593Smuzhiyun /* Fixup monotonic raw and real time time values */
1158*4882a593Smuzhiyun if (interp_forward) {
1159*4882a593Smuzhiyun ts->sys_monoraw = ktime_add_ns(history->raw, corr_raw);
1160*4882a593Smuzhiyun ts->sys_realtime = ktime_add_ns(history->real, corr_real);
1161*4882a593Smuzhiyun } else {
1162*4882a593Smuzhiyun ts->sys_monoraw = ktime_sub_ns(ts->sys_monoraw, corr_raw);
1163*4882a593Smuzhiyun ts->sys_realtime = ktime_sub_ns(ts->sys_realtime, corr_real);
1164*4882a593Smuzhiyun }
1165*4882a593Smuzhiyun
1166*4882a593Smuzhiyun return 0;
1167*4882a593Smuzhiyun }
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun /*
1170*4882a593Smuzhiyun * cycle_between - true if test occurs chronologically between before and after
1171*4882a593Smuzhiyun */
cycle_between(u64 before,u64 test,u64 after)1172*4882a593Smuzhiyun static bool cycle_between(u64 before, u64 test, u64 after)
1173*4882a593Smuzhiyun {
1174*4882a593Smuzhiyun if (test > before && test < after)
1175*4882a593Smuzhiyun return true;
1176*4882a593Smuzhiyun if (test < before && before > after)
1177*4882a593Smuzhiyun return true;
1178*4882a593Smuzhiyun return false;
1179*4882a593Smuzhiyun }
1180*4882a593Smuzhiyun
1181*4882a593Smuzhiyun /**
1182*4882a593Smuzhiyun * get_device_system_crosststamp - Synchronously capture system/device timestamp
1183*4882a593Smuzhiyun * @get_time_fn: Callback to get simultaneous device time and
1184*4882a593Smuzhiyun * system counter from the device driver
1185*4882a593Smuzhiyun * @ctx: Context passed to get_time_fn()
1186*4882a593Smuzhiyun * @history_begin: Historical reference point used to interpolate system
1187*4882a593Smuzhiyun * time when counter provided by the driver is before the current interval
1188*4882a593Smuzhiyun * @xtstamp: Receives simultaneously captured system and device time
1189*4882a593Smuzhiyun *
1190*4882a593Smuzhiyun * Reads a timestamp from a device and correlates it to system time
1191*4882a593Smuzhiyun */
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)1192*4882a593Smuzhiyun int get_device_system_crosststamp(int (*get_time_fn)
1193*4882a593Smuzhiyun (ktime_t *device_time,
1194*4882a593Smuzhiyun struct system_counterval_t *sys_counterval,
1195*4882a593Smuzhiyun void *ctx),
1196*4882a593Smuzhiyun void *ctx,
1197*4882a593Smuzhiyun struct system_time_snapshot *history_begin,
1198*4882a593Smuzhiyun struct system_device_crosststamp *xtstamp)
1199*4882a593Smuzhiyun {
1200*4882a593Smuzhiyun struct system_counterval_t system_counterval;
1201*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1202*4882a593Smuzhiyun u64 cycles, now, interval_start;
1203*4882a593Smuzhiyun unsigned int clock_was_set_seq = 0;
1204*4882a593Smuzhiyun ktime_t base_real, base_raw;
1205*4882a593Smuzhiyun u64 nsec_real, nsec_raw;
1206*4882a593Smuzhiyun u8 cs_was_changed_seq;
1207*4882a593Smuzhiyun unsigned int seq;
1208*4882a593Smuzhiyun bool do_interp;
1209*4882a593Smuzhiyun int ret;
1210*4882a593Smuzhiyun
1211*4882a593Smuzhiyun do {
1212*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
1213*4882a593Smuzhiyun /*
1214*4882a593Smuzhiyun * Try to synchronously capture device time and a system
1215*4882a593Smuzhiyun * counter value calling back into the device driver
1216*4882a593Smuzhiyun */
1217*4882a593Smuzhiyun ret = get_time_fn(&xtstamp->device, &system_counterval, ctx);
1218*4882a593Smuzhiyun if (ret)
1219*4882a593Smuzhiyun return ret;
1220*4882a593Smuzhiyun
1221*4882a593Smuzhiyun /*
1222*4882a593Smuzhiyun * Verify that the clocksource associated with the captured
1223*4882a593Smuzhiyun * system counter value is the same as the currently installed
1224*4882a593Smuzhiyun * timekeeper clocksource
1225*4882a593Smuzhiyun */
1226*4882a593Smuzhiyun if (tk->tkr_mono.clock != system_counterval.cs)
1227*4882a593Smuzhiyun return -ENODEV;
1228*4882a593Smuzhiyun cycles = system_counterval.cycles;
1229*4882a593Smuzhiyun
1230*4882a593Smuzhiyun /*
1231*4882a593Smuzhiyun * Check whether the system counter value provided by the
1232*4882a593Smuzhiyun * device driver is on the current timekeeping interval.
1233*4882a593Smuzhiyun */
1234*4882a593Smuzhiyun now = tk_clock_read(&tk->tkr_mono);
1235*4882a593Smuzhiyun interval_start = tk->tkr_mono.cycle_last;
1236*4882a593Smuzhiyun if (!cycle_between(interval_start, cycles, now)) {
1237*4882a593Smuzhiyun clock_was_set_seq = tk->clock_was_set_seq;
1238*4882a593Smuzhiyun cs_was_changed_seq = tk->cs_was_changed_seq;
1239*4882a593Smuzhiyun cycles = interval_start;
1240*4882a593Smuzhiyun do_interp = true;
1241*4882a593Smuzhiyun } else {
1242*4882a593Smuzhiyun do_interp = false;
1243*4882a593Smuzhiyun }
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun base_real = ktime_add(tk->tkr_mono.base,
1246*4882a593Smuzhiyun tk_core.timekeeper.offs_real);
1247*4882a593Smuzhiyun base_raw = tk->tkr_raw.base;
1248*4882a593Smuzhiyun
1249*4882a593Smuzhiyun nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono,
1250*4882a593Smuzhiyun system_counterval.cycles);
1251*4882a593Smuzhiyun nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw,
1252*4882a593Smuzhiyun system_counterval.cycles);
1253*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
1254*4882a593Smuzhiyun
1255*4882a593Smuzhiyun xtstamp->sys_realtime = ktime_add_ns(base_real, nsec_real);
1256*4882a593Smuzhiyun xtstamp->sys_monoraw = ktime_add_ns(base_raw, nsec_raw);
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun /*
1259*4882a593Smuzhiyun * Interpolate if necessary, adjusting back from the start of the
1260*4882a593Smuzhiyun * current interval
1261*4882a593Smuzhiyun */
1262*4882a593Smuzhiyun if (do_interp) {
1263*4882a593Smuzhiyun u64 partial_history_cycles, total_history_cycles;
1264*4882a593Smuzhiyun bool discontinuity;
1265*4882a593Smuzhiyun
1266*4882a593Smuzhiyun /*
1267*4882a593Smuzhiyun * Check that the counter value occurs after the provided
1268*4882a593Smuzhiyun * history reference and that the history doesn't cross a
1269*4882a593Smuzhiyun * clocksource change
1270*4882a593Smuzhiyun */
1271*4882a593Smuzhiyun if (!history_begin ||
1272*4882a593Smuzhiyun !cycle_between(history_begin->cycles,
1273*4882a593Smuzhiyun system_counterval.cycles, cycles) ||
1274*4882a593Smuzhiyun history_begin->cs_was_changed_seq != cs_was_changed_seq)
1275*4882a593Smuzhiyun return -EINVAL;
1276*4882a593Smuzhiyun partial_history_cycles = cycles - system_counterval.cycles;
1277*4882a593Smuzhiyun total_history_cycles = cycles - history_begin->cycles;
1278*4882a593Smuzhiyun discontinuity =
1279*4882a593Smuzhiyun history_begin->clock_was_set_seq != clock_was_set_seq;
1280*4882a593Smuzhiyun
1281*4882a593Smuzhiyun ret = adjust_historical_crosststamp(history_begin,
1282*4882a593Smuzhiyun partial_history_cycles,
1283*4882a593Smuzhiyun total_history_cycles,
1284*4882a593Smuzhiyun discontinuity, xtstamp);
1285*4882a593Smuzhiyun if (ret)
1286*4882a593Smuzhiyun return ret;
1287*4882a593Smuzhiyun }
1288*4882a593Smuzhiyun
1289*4882a593Smuzhiyun return 0;
1290*4882a593Smuzhiyun }
1291*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(get_device_system_crosststamp);
1292*4882a593Smuzhiyun
1293*4882a593Smuzhiyun /**
1294*4882a593Smuzhiyun * do_settimeofday64 - Sets the time of day.
1295*4882a593Smuzhiyun * @ts: pointer to the timespec64 variable containing the new time
1296*4882a593Smuzhiyun *
1297*4882a593Smuzhiyun * Sets the time of day to the new time and update NTP and notify hrtimers
1298*4882a593Smuzhiyun */
do_settimeofday64(const struct timespec64 * ts)1299*4882a593Smuzhiyun int do_settimeofday64(const struct timespec64 *ts)
1300*4882a593Smuzhiyun {
1301*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1302*4882a593Smuzhiyun struct timespec64 ts_delta, xt;
1303*4882a593Smuzhiyun unsigned long flags;
1304*4882a593Smuzhiyun int ret = 0;
1305*4882a593Smuzhiyun
1306*4882a593Smuzhiyun if (!timespec64_valid_settod(ts))
1307*4882a593Smuzhiyun return -EINVAL;
1308*4882a593Smuzhiyun
1309*4882a593Smuzhiyun raw_spin_lock_irqsave(&timekeeper_lock, flags);
1310*4882a593Smuzhiyun write_seqcount_begin(&tk_core.seq);
1311*4882a593Smuzhiyun
1312*4882a593Smuzhiyun timekeeping_forward_now(tk);
1313*4882a593Smuzhiyun
1314*4882a593Smuzhiyun xt = tk_xtime(tk);
1315*4882a593Smuzhiyun ts_delta = timespec64_sub(*ts, xt);
1316*4882a593Smuzhiyun
1317*4882a593Smuzhiyun if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) {
1318*4882a593Smuzhiyun ret = -EINVAL;
1319*4882a593Smuzhiyun goto out;
1320*4882a593Smuzhiyun }
1321*4882a593Smuzhiyun
1322*4882a593Smuzhiyun tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
1323*4882a593Smuzhiyun
1324*4882a593Smuzhiyun tk_set_xtime(tk, ts);
1325*4882a593Smuzhiyun out:
1326*4882a593Smuzhiyun timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
1327*4882a593Smuzhiyun
1328*4882a593Smuzhiyun write_seqcount_end(&tk_core.seq);
1329*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun /* signal hrtimers about time change */
1332*4882a593Smuzhiyun clock_was_set();
1333*4882a593Smuzhiyun
1334*4882a593Smuzhiyun if (!ret) {
1335*4882a593Smuzhiyun audit_tk_injoffset(ts_delta);
1336*4882a593Smuzhiyun add_device_randomness(ts, sizeof(*ts));
1337*4882a593Smuzhiyun }
1338*4882a593Smuzhiyun
1339*4882a593Smuzhiyun return ret;
1340*4882a593Smuzhiyun }
1341*4882a593Smuzhiyun EXPORT_SYMBOL(do_settimeofday64);
1342*4882a593Smuzhiyun
1343*4882a593Smuzhiyun /**
1344*4882a593Smuzhiyun * timekeeping_inject_offset - Adds or subtracts from the current time.
1345*4882a593Smuzhiyun * @tv: pointer to the timespec variable containing the offset
1346*4882a593Smuzhiyun *
1347*4882a593Smuzhiyun * Adds or subtracts an offset value from the current time.
1348*4882a593Smuzhiyun */
timekeeping_inject_offset(const struct timespec64 * ts)1349*4882a593Smuzhiyun static int timekeeping_inject_offset(const struct timespec64 *ts)
1350*4882a593Smuzhiyun {
1351*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1352*4882a593Smuzhiyun unsigned long flags;
1353*4882a593Smuzhiyun struct timespec64 tmp;
1354*4882a593Smuzhiyun int ret = 0;
1355*4882a593Smuzhiyun
1356*4882a593Smuzhiyun if (ts->tv_nsec < 0 || ts->tv_nsec >= NSEC_PER_SEC)
1357*4882a593Smuzhiyun return -EINVAL;
1358*4882a593Smuzhiyun
1359*4882a593Smuzhiyun raw_spin_lock_irqsave(&timekeeper_lock, flags);
1360*4882a593Smuzhiyun write_seqcount_begin(&tk_core.seq);
1361*4882a593Smuzhiyun
1362*4882a593Smuzhiyun timekeeping_forward_now(tk);
1363*4882a593Smuzhiyun
1364*4882a593Smuzhiyun /* Make sure the proposed value is valid */
1365*4882a593Smuzhiyun tmp = timespec64_add(tk_xtime(tk), *ts);
1366*4882a593Smuzhiyun if (timespec64_compare(&tk->wall_to_monotonic, ts) > 0 ||
1367*4882a593Smuzhiyun !timespec64_valid_settod(&tmp)) {
1368*4882a593Smuzhiyun ret = -EINVAL;
1369*4882a593Smuzhiyun goto error;
1370*4882a593Smuzhiyun }
1371*4882a593Smuzhiyun
1372*4882a593Smuzhiyun tk_xtime_add(tk, ts);
1373*4882a593Smuzhiyun tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *ts));
1374*4882a593Smuzhiyun
1375*4882a593Smuzhiyun error: /* even if we error out, we forwarded the time, so call update */
1376*4882a593Smuzhiyun timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun write_seqcount_end(&tk_core.seq);
1379*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1380*4882a593Smuzhiyun
1381*4882a593Smuzhiyun /* signal hrtimers about time change */
1382*4882a593Smuzhiyun clock_was_set();
1383*4882a593Smuzhiyun
1384*4882a593Smuzhiyun return ret;
1385*4882a593Smuzhiyun }
1386*4882a593Smuzhiyun
1387*4882a593Smuzhiyun /*
1388*4882a593Smuzhiyun * Indicates if there is an offset between the system clock and the hardware
1389*4882a593Smuzhiyun * clock/persistent clock/rtc.
1390*4882a593Smuzhiyun */
1391*4882a593Smuzhiyun int persistent_clock_is_local;
1392*4882a593Smuzhiyun
1393*4882a593Smuzhiyun /*
1394*4882a593Smuzhiyun * Adjust the time obtained from the CMOS to be UTC time instead of
1395*4882a593Smuzhiyun * local time.
1396*4882a593Smuzhiyun *
1397*4882a593Smuzhiyun * This is ugly, but preferable to the alternatives. Otherwise we
1398*4882a593Smuzhiyun * would either need to write a program to do it in /etc/rc (and risk
1399*4882a593Smuzhiyun * confusion if the program gets run more than once; it would also be
1400*4882a593Smuzhiyun * hard to make the program warp the clock precisely n hours) or
1401*4882a593Smuzhiyun * compile in the timezone information into the kernel. Bad, bad....
1402*4882a593Smuzhiyun *
1403*4882a593Smuzhiyun * - TYT, 1992-01-01
1404*4882a593Smuzhiyun *
1405*4882a593Smuzhiyun * The best thing to do is to keep the CMOS clock in universal time (UTC)
1406*4882a593Smuzhiyun * as real UNIX machines always do it. This avoids all headaches about
1407*4882a593Smuzhiyun * daylight saving times and warping kernel clocks.
1408*4882a593Smuzhiyun */
timekeeping_warp_clock(void)1409*4882a593Smuzhiyun void timekeeping_warp_clock(void)
1410*4882a593Smuzhiyun {
1411*4882a593Smuzhiyun if (sys_tz.tz_minuteswest != 0) {
1412*4882a593Smuzhiyun struct timespec64 adjust;
1413*4882a593Smuzhiyun
1414*4882a593Smuzhiyun persistent_clock_is_local = 1;
1415*4882a593Smuzhiyun adjust.tv_sec = sys_tz.tz_minuteswest * 60;
1416*4882a593Smuzhiyun adjust.tv_nsec = 0;
1417*4882a593Smuzhiyun timekeeping_inject_offset(&adjust);
1418*4882a593Smuzhiyun }
1419*4882a593Smuzhiyun }
1420*4882a593Smuzhiyun
1421*4882a593Smuzhiyun /**
1422*4882a593Smuzhiyun * __timekeeping_set_tai_offset - Sets the TAI offset from UTC and monotonic
1423*4882a593Smuzhiyun *
1424*4882a593Smuzhiyun */
__timekeeping_set_tai_offset(struct timekeeper * tk,s32 tai_offset)1425*4882a593Smuzhiyun static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
1426*4882a593Smuzhiyun {
1427*4882a593Smuzhiyun tk->tai_offset = tai_offset;
1428*4882a593Smuzhiyun tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
1429*4882a593Smuzhiyun }
1430*4882a593Smuzhiyun
1431*4882a593Smuzhiyun /**
1432*4882a593Smuzhiyun * change_clocksource - Swaps clocksources if a new one is available
1433*4882a593Smuzhiyun *
1434*4882a593Smuzhiyun * Accumulates current time interval and initializes new clocksource
1435*4882a593Smuzhiyun */
change_clocksource(void * data)1436*4882a593Smuzhiyun static int change_clocksource(void *data)
1437*4882a593Smuzhiyun {
1438*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1439*4882a593Smuzhiyun struct clocksource *new, *old;
1440*4882a593Smuzhiyun unsigned long flags;
1441*4882a593Smuzhiyun
1442*4882a593Smuzhiyun new = (struct clocksource *) data;
1443*4882a593Smuzhiyun
1444*4882a593Smuzhiyun raw_spin_lock_irqsave(&timekeeper_lock, flags);
1445*4882a593Smuzhiyun write_seqcount_begin(&tk_core.seq);
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun timekeeping_forward_now(tk);
1448*4882a593Smuzhiyun /*
1449*4882a593Smuzhiyun * If the cs is in module, get a module reference. Succeeds
1450*4882a593Smuzhiyun * for built-in code (owner == NULL) as well.
1451*4882a593Smuzhiyun */
1452*4882a593Smuzhiyun if (try_module_get(new->owner)) {
1453*4882a593Smuzhiyun if (!new->enable || new->enable(new) == 0) {
1454*4882a593Smuzhiyun old = tk->tkr_mono.clock;
1455*4882a593Smuzhiyun tk_setup_internals(tk, new);
1456*4882a593Smuzhiyun if (old->disable)
1457*4882a593Smuzhiyun old->disable(old);
1458*4882a593Smuzhiyun module_put(old->owner);
1459*4882a593Smuzhiyun } else {
1460*4882a593Smuzhiyun module_put(new->owner);
1461*4882a593Smuzhiyun }
1462*4882a593Smuzhiyun }
1463*4882a593Smuzhiyun timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
1464*4882a593Smuzhiyun
1465*4882a593Smuzhiyun write_seqcount_end(&tk_core.seq);
1466*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1467*4882a593Smuzhiyun
1468*4882a593Smuzhiyun return 0;
1469*4882a593Smuzhiyun }
1470*4882a593Smuzhiyun
1471*4882a593Smuzhiyun /**
1472*4882a593Smuzhiyun * timekeeping_notify - Install a new clock source
1473*4882a593Smuzhiyun * @clock: pointer to the clock source
1474*4882a593Smuzhiyun *
1475*4882a593Smuzhiyun * This function is called from clocksource.c after a new, better clock
1476*4882a593Smuzhiyun * source has been registered. The caller holds the clocksource_mutex.
1477*4882a593Smuzhiyun */
timekeeping_notify(struct clocksource * clock)1478*4882a593Smuzhiyun int timekeeping_notify(struct clocksource *clock)
1479*4882a593Smuzhiyun {
1480*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1481*4882a593Smuzhiyun
1482*4882a593Smuzhiyun if (tk->tkr_mono.clock == clock)
1483*4882a593Smuzhiyun return 0;
1484*4882a593Smuzhiyun stop_machine(change_clocksource, clock, NULL);
1485*4882a593Smuzhiyun tick_clock_notify();
1486*4882a593Smuzhiyun return tk->tkr_mono.clock == clock ? 0 : -1;
1487*4882a593Smuzhiyun }
1488*4882a593Smuzhiyun
1489*4882a593Smuzhiyun /**
1490*4882a593Smuzhiyun * ktime_get_raw_ts64 - Returns the raw monotonic time in a timespec
1491*4882a593Smuzhiyun * @ts: pointer to the timespec64 to be set
1492*4882a593Smuzhiyun *
1493*4882a593Smuzhiyun * Returns the raw monotonic time (completely un-modified by ntp)
1494*4882a593Smuzhiyun */
ktime_get_raw_ts64(struct timespec64 * ts)1495*4882a593Smuzhiyun void ktime_get_raw_ts64(struct timespec64 *ts)
1496*4882a593Smuzhiyun {
1497*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1498*4882a593Smuzhiyun unsigned int seq;
1499*4882a593Smuzhiyun u64 nsecs;
1500*4882a593Smuzhiyun
1501*4882a593Smuzhiyun do {
1502*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
1503*4882a593Smuzhiyun ts->tv_sec = tk->raw_sec;
1504*4882a593Smuzhiyun nsecs = timekeeping_get_ns(&tk->tkr_raw);
1505*4882a593Smuzhiyun
1506*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
1507*4882a593Smuzhiyun
1508*4882a593Smuzhiyun ts->tv_nsec = 0;
1509*4882a593Smuzhiyun timespec64_add_ns(ts, nsecs);
1510*4882a593Smuzhiyun }
1511*4882a593Smuzhiyun EXPORT_SYMBOL(ktime_get_raw_ts64);
1512*4882a593Smuzhiyun
1513*4882a593Smuzhiyun
1514*4882a593Smuzhiyun /**
1515*4882a593Smuzhiyun * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
1516*4882a593Smuzhiyun */
timekeeping_valid_for_hres(void)1517*4882a593Smuzhiyun int timekeeping_valid_for_hres(void)
1518*4882a593Smuzhiyun {
1519*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1520*4882a593Smuzhiyun unsigned int seq;
1521*4882a593Smuzhiyun int ret;
1522*4882a593Smuzhiyun
1523*4882a593Smuzhiyun do {
1524*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
1525*4882a593Smuzhiyun
1526*4882a593Smuzhiyun ret = tk->tkr_mono.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
1527*4882a593Smuzhiyun
1528*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
1529*4882a593Smuzhiyun
1530*4882a593Smuzhiyun return ret;
1531*4882a593Smuzhiyun }
1532*4882a593Smuzhiyun
1533*4882a593Smuzhiyun /**
1534*4882a593Smuzhiyun * timekeeping_max_deferment - Returns max time the clocksource can be deferred
1535*4882a593Smuzhiyun */
timekeeping_max_deferment(void)1536*4882a593Smuzhiyun u64 timekeeping_max_deferment(void)
1537*4882a593Smuzhiyun {
1538*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1539*4882a593Smuzhiyun unsigned int seq;
1540*4882a593Smuzhiyun u64 ret;
1541*4882a593Smuzhiyun
1542*4882a593Smuzhiyun do {
1543*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
1544*4882a593Smuzhiyun
1545*4882a593Smuzhiyun ret = tk->tkr_mono.clock->max_idle_ns;
1546*4882a593Smuzhiyun
1547*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
1548*4882a593Smuzhiyun
1549*4882a593Smuzhiyun return ret;
1550*4882a593Smuzhiyun }
1551*4882a593Smuzhiyun
1552*4882a593Smuzhiyun /**
1553*4882a593Smuzhiyun * read_persistent_clock64 - Return time from the persistent clock.
1554*4882a593Smuzhiyun *
1555*4882a593Smuzhiyun * Weak dummy function for arches that do not yet support it.
1556*4882a593Smuzhiyun * Reads the time from the battery backed persistent clock.
1557*4882a593Smuzhiyun * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
1558*4882a593Smuzhiyun *
1559*4882a593Smuzhiyun * XXX - Do be sure to remove it once all arches implement it.
1560*4882a593Smuzhiyun */
read_persistent_clock64(struct timespec64 * ts)1561*4882a593Smuzhiyun void __weak read_persistent_clock64(struct timespec64 *ts)
1562*4882a593Smuzhiyun {
1563*4882a593Smuzhiyun ts->tv_sec = 0;
1564*4882a593Smuzhiyun ts->tv_nsec = 0;
1565*4882a593Smuzhiyun }
1566*4882a593Smuzhiyun
1567*4882a593Smuzhiyun /**
1568*4882a593Smuzhiyun * read_persistent_wall_and_boot_offset - Read persistent clock, and also offset
1569*4882a593Smuzhiyun * from the boot.
1570*4882a593Smuzhiyun *
1571*4882a593Smuzhiyun * Weak dummy function for arches that do not yet support it.
1572*4882a593Smuzhiyun * wall_time - current time as returned by persistent clock
1573*4882a593Smuzhiyun * boot_offset - offset that is defined as wall_time - boot_time
1574*4882a593Smuzhiyun * The default function calculates offset based on the current value of
1575*4882a593Smuzhiyun * local_clock(). This way architectures that support sched_clock() but don't
1576*4882a593Smuzhiyun * support dedicated boot time clock will provide the best estimate of the
1577*4882a593Smuzhiyun * boot time.
1578*4882a593Smuzhiyun */
1579*4882a593Smuzhiyun void __weak __init
read_persistent_wall_and_boot_offset(struct timespec64 * wall_time,struct timespec64 * boot_offset)1580*4882a593Smuzhiyun read_persistent_wall_and_boot_offset(struct timespec64 *wall_time,
1581*4882a593Smuzhiyun struct timespec64 *boot_offset)
1582*4882a593Smuzhiyun {
1583*4882a593Smuzhiyun read_persistent_clock64(wall_time);
1584*4882a593Smuzhiyun *boot_offset = ns_to_timespec64(local_clock());
1585*4882a593Smuzhiyun }
1586*4882a593Smuzhiyun
1587*4882a593Smuzhiyun /*
1588*4882a593Smuzhiyun * Flag reflecting whether timekeeping_resume() has injected sleeptime.
1589*4882a593Smuzhiyun *
1590*4882a593Smuzhiyun * The flag starts of false and is only set when a suspend reaches
1591*4882a593Smuzhiyun * timekeeping_suspend(), timekeeping_resume() sets it to false when the
1592*4882a593Smuzhiyun * timekeeper clocksource is not stopping across suspend and has been
1593*4882a593Smuzhiyun * used to update sleep time. If the timekeeper clocksource has stopped
1594*4882a593Smuzhiyun * then the flag stays true and is used by the RTC resume code to decide
1595*4882a593Smuzhiyun * whether sleeptime must be injected and if so the flag gets false then.
1596*4882a593Smuzhiyun *
1597*4882a593Smuzhiyun * If a suspend fails before reaching timekeeping_resume() then the flag
1598*4882a593Smuzhiyun * stays false and prevents erroneous sleeptime injection.
1599*4882a593Smuzhiyun */
1600*4882a593Smuzhiyun static bool suspend_timing_needed;
1601*4882a593Smuzhiyun
1602*4882a593Smuzhiyun /* Flag for if there is a persistent clock on this platform */
1603*4882a593Smuzhiyun static bool persistent_clock_exists;
1604*4882a593Smuzhiyun
1605*4882a593Smuzhiyun /*
1606*4882a593Smuzhiyun * timekeeping_init - Initializes the clocksource and common timekeeping values
1607*4882a593Smuzhiyun */
timekeeping_init(void)1608*4882a593Smuzhiyun void __init timekeeping_init(void)
1609*4882a593Smuzhiyun {
1610*4882a593Smuzhiyun struct timespec64 wall_time, boot_offset, wall_to_mono;
1611*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1612*4882a593Smuzhiyun struct clocksource *clock;
1613*4882a593Smuzhiyun unsigned long flags;
1614*4882a593Smuzhiyun
1615*4882a593Smuzhiyun read_persistent_wall_and_boot_offset(&wall_time, &boot_offset);
1616*4882a593Smuzhiyun if (timespec64_valid_settod(&wall_time) &&
1617*4882a593Smuzhiyun timespec64_to_ns(&wall_time) > 0) {
1618*4882a593Smuzhiyun persistent_clock_exists = true;
1619*4882a593Smuzhiyun } else if (timespec64_to_ns(&wall_time) != 0) {
1620*4882a593Smuzhiyun pr_warn("Persistent clock returned invalid value");
1621*4882a593Smuzhiyun wall_time = (struct timespec64){0};
1622*4882a593Smuzhiyun }
1623*4882a593Smuzhiyun
1624*4882a593Smuzhiyun if (timespec64_compare(&wall_time, &boot_offset) < 0)
1625*4882a593Smuzhiyun boot_offset = (struct timespec64){0};
1626*4882a593Smuzhiyun
1627*4882a593Smuzhiyun /*
1628*4882a593Smuzhiyun * We want set wall_to_mono, so the following is true:
1629*4882a593Smuzhiyun * wall time + wall_to_mono = boot time
1630*4882a593Smuzhiyun */
1631*4882a593Smuzhiyun wall_to_mono = timespec64_sub(boot_offset, wall_time);
1632*4882a593Smuzhiyun
1633*4882a593Smuzhiyun raw_spin_lock_irqsave(&timekeeper_lock, flags);
1634*4882a593Smuzhiyun write_seqcount_begin(&tk_core.seq);
1635*4882a593Smuzhiyun ntp_init();
1636*4882a593Smuzhiyun
1637*4882a593Smuzhiyun clock = clocksource_default_clock();
1638*4882a593Smuzhiyun if (clock->enable)
1639*4882a593Smuzhiyun clock->enable(clock);
1640*4882a593Smuzhiyun tk_setup_internals(tk, clock);
1641*4882a593Smuzhiyun
1642*4882a593Smuzhiyun tk_set_xtime(tk, &wall_time);
1643*4882a593Smuzhiyun tk->raw_sec = 0;
1644*4882a593Smuzhiyun
1645*4882a593Smuzhiyun tk_set_wall_to_mono(tk, wall_to_mono);
1646*4882a593Smuzhiyun
1647*4882a593Smuzhiyun timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
1648*4882a593Smuzhiyun
1649*4882a593Smuzhiyun write_seqcount_end(&tk_core.seq);
1650*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1651*4882a593Smuzhiyun }
1652*4882a593Smuzhiyun
1653*4882a593Smuzhiyun /* time in seconds when suspend began for persistent clock */
1654*4882a593Smuzhiyun static struct timespec64 timekeeping_suspend_time;
1655*4882a593Smuzhiyun
1656*4882a593Smuzhiyun /**
1657*4882a593Smuzhiyun * __timekeeping_inject_sleeptime - Internal function to add sleep interval
1658*4882a593Smuzhiyun * @delta: pointer to a timespec delta value
1659*4882a593Smuzhiyun *
1660*4882a593Smuzhiyun * Takes a timespec offset measuring a suspend interval and properly
1661*4882a593Smuzhiyun * adds the sleep offset to the timekeeping variables.
1662*4882a593Smuzhiyun */
__timekeeping_inject_sleeptime(struct timekeeper * tk,const struct timespec64 * delta)1663*4882a593Smuzhiyun static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
1664*4882a593Smuzhiyun const struct timespec64 *delta)
1665*4882a593Smuzhiyun {
1666*4882a593Smuzhiyun if (!timespec64_valid_strict(delta)) {
1667*4882a593Smuzhiyun printk_deferred(KERN_WARNING
1668*4882a593Smuzhiyun "__timekeeping_inject_sleeptime: Invalid "
1669*4882a593Smuzhiyun "sleep delta value!\n");
1670*4882a593Smuzhiyun return;
1671*4882a593Smuzhiyun }
1672*4882a593Smuzhiyun tk_xtime_add(tk, delta);
1673*4882a593Smuzhiyun tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
1674*4882a593Smuzhiyun tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
1675*4882a593Smuzhiyun tk_debug_account_sleep_time(delta);
1676*4882a593Smuzhiyun }
1677*4882a593Smuzhiyun
1678*4882a593Smuzhiyun #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
1679*4882a593Smuzhiyun /**
1680*4882a593Smuzhiyun * We have three kinds of time sources to use for sleep time
1681*4882a593Smuzhiyun * injection, the preference order is:
1682*4882a593Smuzhiyun * 1) non-stop clocksource
1683*4882a593Smuzhiyun * 2) persistent clock (ie: RTC accessible when irqs are off)
1684*4882a593Smuzhiyun * 3) RTC
1685*4882a593Smuzhiyun *
1686*4882a593Smuzhiyun * 1) and 2) are used by timekeeping, 3) by RTC subsystem.
1687*4882a593Smuzhiyun * If system has neither 1) nor 2), 3) will be used finally.
1688*4882a593Smuzhiyun *
1689*4882a593Smuzhiyun *
1690*4882a593Smuzhiyun * If timekeeping has injected sleeptime via either 1) or 2),
1691*4882a593Smuzhiyun * 3) becomes needless, so in this case we don't need to call
1692*4882a593Smuzhiyun * rtc_resume(), and this is what timekeeping_rtc_skipresume()
1693*4882a593Smuzhiyun * means.
1694*4882a593Smuzhiyun */
timekeeping_rtc_skipresume(void)1695*4882a593Smuzhiyun bool timekeeping_rtc_skipresume(void)
1696*4882a593Smuzhiyun {
1697*4882a593Smuzhiyun return !suspend_timing_needed;
1698*4882a593Smuzhiyun }
1699*4882a593Smuzhiyun
1700*4882a593Smuzhiyun /**
1701*4882a593Smuzhiyun * 1) can be determined whether to use or not only when doing
1702*4882a593Smuzhiyun * timekeeping_resume() which is invoked after rtc_suspend(),
1703*4882a593Smuzhiyun * so we can't skip rtc_suspend() surely if system has 1).
1704*4882a593Smuzhiyun *
1705*4882a593Smuzhiyun * But if system has 2), 2) will definitely be used, so in this
1706*4882a593Smuzhiyun * case we don't need to call rtc_suspend(), and this is what
1707*4882a593Smuzhiyun * timekeeping_rtc_skipsuspend() means.
1708*4882a593Smuzhiyun */
timekeeping_rtc_skipsuspend(void)1709*4882a593Smuzhiyun bool timekeeping_rtc_skipsuspend(void)
1710*4882a593Smuzhiyun {
1711*4882a593Smuzhiyun return persistent_clock_exists;
1712*4882a593Smuzhiyun }
1713*4882a593Smuzhiyun
1714*4882a593Smuzhiyun /**
1715*4882a593Smuzhiyun * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values
1716*4882a593Smuzhiyun * @delta: pointer to a timespec64 delta value
1717*4882a593Smuzhiyun *
1718*4882a593Smuzhiyun * This hook is for architectures that cannot support read_persistent_clock64
1719*4882a593Smuzhiyun * because their RTC/persistent clock is only accessible when irqs are enabled.
1720*4882a593Smuzhiyun * and also don't have an effective nonstop clocksource.
1721*4882a593Smuzhiyun *
1722*4882a593Smuzhiyun * This function should only be called by rtc_resume(), and allows
1723*4882a593Smuzhiyun * a suspend offset to be injected into the timekeeping values.
1724*4882a593Smuzhiyun */
timekeeping_inject_sleeptime64(const struct timespec64 * delta)1725*4882a593Smuzhiyun void timekeeping_inject_sleeptime64(const struct timespec64 *delta)
1726*4882a593Smuzhiyun {
1727*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1728*4882a593Smuzhiyun unsigned long flags;
1729*4882a593Smuzhiyun
1730*4882a593Smuzhiyun raw_spin_lock_irqsave(&timekeeper_lock, flags);
1731*4882a593Smuzhiyun write_seqcount_begin(&tk_core.seq);
1732*4882a593Smuzhiyun
1733*4882a593Smuzhiyun suspend_timing_needed = false;
1734*4882a593Smuzhiyun
1735*4882a593Smuzhiyun timekeeping_forward_now(tk);
1736*4882a593Smuzhiyun
1737*4882a593Smuzhiyun __timekeeping_inject_sleeptime(tk, delta);
1738*4882a593Smuzhiyun
1739*4882a593Smuzhiyun timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
1740*4882a593Smuzhiyun
1741*4882a593Smuzhiyun write_seqcount_end(&tk_core.seq);
1742*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1743*4882a593Smuzhiyun
1744*4882a593Smuzhiyun /* signal hrtimers about time change */
1745*4882a593Smuzhiyun clock_was_set();
1746*4882a593Smuzhiyun }
1747*4882a593Smuzhiyun #endif
1748*4882a593Smuzhiyun
1749*4882a593Smuzhiyun /**
1750*4882a593Smuzhiyun * timekeeping_resume - Resumes the generic timekeeping subsystem.
1751*4882a593Smuzhiyun */
timekeeping_resume(void)1752*4882a593Smuzhiyun void timekeeping_resume(void)
1753*4882a593Smuzhiyun {
1754*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1755*4882a593Smuzhiyun struct clocksource *clock = tk->tkr_mono.clock;
1756*4882a593Smuzhiyun unsigned long flags;
1757*4882a593Smuzhiyun struct timespec64 ts_new, ts_delta;
1758*4882a593Smuzhiyun u64 cycle_now, nsec;
1759*4882a593Smuzhiyun bool inject_sleeptime = false;
1760*4882a593Smuzhiyun
1761*4882a593Smuzhiyun read_persistent_clock64(&ts_new);
1762*4882a593Smuzhiyun
1763*4882a593Smuzhiyun clockevents_resume();
1764*4882a593Smuzhiyun clocksource_resume();
1765*4882a593Smuzhiyun
1766*4882a593Smuzhiyun raw_spin_lock_irqsave(&timekeeper_lock, flags);
1767*4882a593Smuzhiyun write_seqcount_begin(&tk_core.seq);
1768*4882a593Smuzhiyun
1769*4882a593Smuzhiyun /*
1770*4882a593Smuzhiyun * After system resumes, we need to calculate the suspended time and
1771*4882a593Smuzhiyun * compensate it for the OS time. There are 3 sources that could be
1772*4882a593Smuzhiyun * used: Nonstop clocksource during suspend, persistent clock and rtc
1773*4882a593Smuzhiyun * device.
1774*4882a593Smuzhiyun *
1775*4882a593Smuzhiyun * One specific platform may have 1 or 2 or all of them, and the
1776*4882a593Smuzhiyun * preference will be:
1777*4882a593Smuzhiyun * suspend-nonstop clocksource -> persistent clock -> rtc
1778*4882a593Smuzhiyun * The less preferred source will only be tried if there is no better
1779*4882a593Smuzhiyun * usable source. The rtc part is handled separately in rtc core code.
1780*4882a593Smuzhiyun */
1781*4882a593Smuzhiyun cycle_now = tk_clock_read(&tk->tkr_mono);
1782*4882a593Smuzhiyun nsec = clocksource_stop_suspend_timing(clock, cycle_now);
1783*4882a593Smuzhiyun if (nsec > 0) {
1784*4882a593Smuzhiyun ts_delta = ns_to_timespec64(nsec);
1785*4882a593Smuzhiyun inject_sleeptime = true;
1786*4882a593Smuzhiyun } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
1787*4882a593Smuzhiyun ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
1788*4882a593Smuzhiyun inject_sleeptime = true;
1789*4882a593Smuzhiyun }
1790*4882a593Smuzhiyun
1791*4882a593Smuzhiyun if (inject_sleeptime) {
1792*4882a593Smuzhiyun suspend_timing_needed = false;
1793*4882a593Smuzhiyun __timekeeping_inject_sleeptime(tk, &ts_delta);
1794*4882a593Smuzhiyun }
1795*4882a593Smuzhiyun
1796*4882a593Smuzhiyun /* Re-base the last cycle value */
1797*4882a593Smuzhiyun tk->tkr_mono.cycle_last = cycle_now;
1798*4882a593Smuzhiyun tk->tkr_raw.cycle_last = cycle_now;
1799*4882a593Smuzhiyun
1800*4882a593Smuzhiyun tk->ntp_error = 0;
1801*4882a593Smuzhiyun timekeeping_suspended = 0;
1802*4882a593Smuzhiyun timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
1803*4882a593Smuzhiyun write_seqcount_end(&tk_core.seq);
1804*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1805*4882a593Smuzhiyun
1806*4882a593Smuzhiyun touch_softlockup_watchdog();
1807*4882a593Smuzhiyun
1808*4882a593Smuzhiyun tick_resume();
1809*4882a593Smuzhiyun hrtimers_resume();
1810*4882a593Smuzhiyun }
1811*4882a593Smuzhiyun
timekeeping_suspend(void)1812*4882a593Smuzhiyun int timekeeping_suspend(void)
1813*4882a593Smuzhiyun {
1814*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
1815*4882a593Smuzhiyun unsigned long flags;
1816*4882a593Smuzhiyun struct timespec64 delta, delta_delta;
1817*4882a593Smuzhiyun static struct timespec64 old_delta;
1818*4882a593Smuzhiyun struct clocksource *curr_clock;
1819*4882a593Smuzhiyun u64 cycle_now;
1820*4882a593Smuzhiyun
1821*4882a593Smuzhiyun read_persistent_clock64(&timekeeping_suspend_time);
1822*4882a593Smuzhiyun
1823*4882a593Smuzhiyun /*
1824*4882a593Smuzhiyun * On some systems the persistent_clock can not be detected at
1825*4882a593Smuzhiyun * timekeeping_init by its return value, so if we see a valid
1826*4882a593Smuzhiyun * value returned, update the persistent_clock_exists flag.
1827*4882a593Smuzhiyun */
1828*4882a593Smuzhiyun if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
1829*4882a593Smuzhiyun persistent_clock_exists = true;
1830*4882a593Smuzhiyun
1831*4882a593Smuzhiyun suspend_timing_needed = true;
1832*4882a593Smuzhiyun
1833*4882a593Smuzhiyun raw_spin_lock_irqsave(&timekeeper_lock, flags);
1834*4882a593Smuzhiyun write_seqcount_begin(&tk_core.seq);
1835*4882a593Smuzhiyun timekeeping_forward_now(tk);
1836*4882a593Smuzhiyun timekeeping_suspended = 1;
1837*4882a593Smuzhiyun
1838*4882a593Smuzhiyun /*
1839*4882a593Smuzhiyun * Since we've called forward_now, cycle_last stores the value
1840*4882a593Smuzhiyun * just read from the current clocksource. Save this to potentially
1841*4882a593Smuzhiyun * use in suspend timing.
1842*4882a593Smuzhiyun */
1843*4882a593Smuzhiyun curr_clock = tk->tkr_mono.clock;
1844*4882a593Smuzhiyun cycle_now = tk->tkr_mono.cycle_last;
1845*4882a593Smuzhiyun clocksource_start_suspend_timing(curr_clock, cycle_now);
1846*4882a593Smuzhiyun
1847*4882a593Smuzhiyun if (persistent_clock_exists) {
1848*4882a593Smuzhiyun /*
1849*4882a593Smuzhiyun * To avoid drift caused by repeated suspend/resumes,
1850*4882a593Smuzhiyun * which each can add ~1 second drift error,
1851*4882a593Smuzhiyun * try to compensate so the difference in system time
1852*4882a593Smuzhiyun * and persistent_clock time stays close to constant.
1853*4882a593Smuzhiyun */
1854*4882a593Smuzhiyun delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time);
1855*4882a593Smuzhiyun delta_delta = timespec64_sub(delta, old_delta);
1856*4882a593Smuzhiyun if (abs(delta_delta.tv_sec) >= 2) {
1857*4882a593Smuzhiyun /*
1858*4882a593Smuzhiyun * if delta_delta is too large, assume time correction
1859*4882a593Smuzhiyun * has occurred and set old_delta to the current delta.
1860*4882a593Smuzhiyun */
1861*4882a593Smuzhiyun old_delta = delta;
1862*4882a593Smuzhiyun } else {
1863*4882a593Smuzhiyun /* Otherwise try to adjust old_system to compensate */
1864*4882a593Smuzhiyun timekeeping_suspend_time =
1865*4882a593Smuzhiyun timespec64_add(timekeeping_suspend_time, delta_delta);
1866*4882a593Smuzhiyun }
1867*4882a593Smuzhiyun }
1868*4882a593Smuzhiyun
1869*4882a593Smuzhiyun timekeeping_update(tk, TK_MIRROR);
1870*4882a593Smuzhiyun halt_fast_timekeeper(tk);
1871*4882a593Smuzhiyun write_seqcount_end(&tk_core.seq);
1872*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1873*4882a593Smuzhiyun
1874*4882a593Smuzhiyun tick_suspend();
1875*4882a593Smuzhiyun clocksource_suspend();
1876*4882a593Smuzhiyun clockevents_suspend();
1877*4882a593Smuzhiyun
1878*4882a593Smuzhiyun return 0;
1879*4882a593Smuzhiyun }
1880*4882a593Smuzhiyun
1881*4882a593Smuzhiyun /* sysfs resume/suspend bits for timekeeping */
1882*4882a593Smuzhiyun static struct syscore_ops timekeeping_syscore_ops = {
1883*4882a593Smuzhiyun .resume = timekeeping_resume,
1884*4882a593Smuzhiyun .suspend = timekeeping_suspend,
1885*4882a593Smuzhiyun };
1886*4882a593Smuzhiyun
timekeeping_init_ops(void)1887*4882a593Smuzhiyun static int __init timekeeping_init_ops(void)
1888*4882a593Smuzhiyun {
1889*4882a593Smuzhiyun register_syscore_ops(&timekeeping_syscore_ops);
1890*4882a593Smuzhiyun return 0;
1891*4882a593Smuzhiyun }
1892*4882a593Smuzhiyun device_initcall(timekeeping_init_ops);
1893*4882a593Smuzhiyun
1894*4882a593Smuzhiyun /*
1895*4882a593Smuzhiyun * Apply a multiplier adjustment to the timekeeper
1896*4882a593Smuzhiyun */
timekeeping_apply_adjustment(struct timekeeper * tk,s64 offset,s32 mult_adj)1897*4882a593Smuzhiyun static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
1898*4882a593Smuzhiyun s64 offset,
1899*4882a593Smuzhiyun s32 mult_adj)
1900*4882a593Smuzhiyun {
1901*4882a593Smuzhiyun s64 interval = tk->cycle_interval;
1902*4882a593Smuzhiyun
1903*4882a593Smuzhiyun if (mult_adj == 0) {
1904*4882a593Smuzhiyun return;
1905*4882a593Smuzhiyun } else if (mult_adj == -1) {
1906*4882a593Smuzhiyun interval = -interval;
1907*4882a593Smuzhiyun offset = -offset;
1908*4882a593Smuzhiyun } else if (mult_adj != 1) {
1909*4882a593Smuzhiyun interval *= mult_adj;
1910*4882a593Smuzhiyun offset *= mult_adj;
1911*4882a593Smuzhiyun }
1912*4882a593Smuzhiyun
1913*4882a593Smuzhiyun /*
1914*4882a593Smuzhiyun * So the following can be confusing.
1915*4882a593Smuzhiyun *
1916*4882a593Smuzhiyun * To keep things simple, lets assume mult_adj == 1 for now.
1917*4882a593Smuzhiyun *
1918*4882a593Smuzhiyun * When mult_adj != 1, remember that the interval and offset values
1919*4882a593Smuzhiyun * have been appropriately scaled so the math is the same.
1920*4882a593Smuzhiyun *
1921*4882a593Smuzhiyun * The basic idea here is that we're increasing the multiplier
1922*4882a593Smuzhiyun * by one, this causes the xtime_interval to be incremented by
1923*4882a593Smuzhiyun * one cycle_interval. This is because:
1924*4882a593Smuzhiyun * xtime_interval = cycle_interval * mult
1925*4882a593Smuzhiyun * So if mult is being incremented by one:
1926*4882a593Smuzhiyun * xtime_interval = cycle_interval * (mult + 1)
1927*4882a593Smuzhiyun * Its the same as:
1928*4882a593Smuzhiyun * xtime_interval = (cycle_interval * mult) + cycle_interval
1929*4882a593Smuzhiyun * Which can be shortened to:
1930*4882a593Smuzhiyun * xtime_interval += cycle_interval
1931*4882a593Smuzhiyun *
1932*4882a593Smuzhiyun * So offset stores the non-accumulated cycles. Thus the current
1933*4882a593Smuzhiyun * time (in shifted nanoseconds) is:
1934*4882a593Smuzhiyun * now = (offset * adj) + xtime_nsec
1935*4882a593Smuzhiyun * Now, even though we're adjusting the clock frequency, we have
1936*4882a593Smuzhiyun * to keep time consistent. In other words, we can't jump back
1937*4882a593Smuzhiyun * in time, and we also want to avoid jumping forward in time.
1938*4882a593Smuzhiyun *
1939*4882a593Smuzhiyun * So given the same offset value, we need the time to be the same
1940*4882a593Smuzhiyun * both before and after the freq adjustment.
1941*4882a593Smuzhiyun * now = (offset * adj_1) + xtime_nsec_1
1942*4882a593Smuzhiyun * now = (offset * adj_2) + xtime_nsec_2
1943*4882a593Smuzhiyun * So:
1944*4882a593Smuzhiyun * (offset * adj_1) + xtime_nsec_1 =
1945*4882a593Smuzhiyun * (offset * adj_2) + xtime_nsec_2
1946*4882a593Smuzhiyun * And we know:
1947*4882a593Smuzhiyun * adj_2 = adj_1 + 1
1948*4882a593Smuzhiyun * So:
1949*4882a593Smuzhiyun * (offset * adj_1) + xtime_nsec_1 =
1950*4882a593Smuzhiyun * (offset * (adj_1+1)) + xtime_nsec_2
1951*4882a593Smuzhiyun * (offset * adj_1) + xtime_nsec_1 =
1952*4882a593Smuzhiyun * (offset * adj_1) + offset + xtime_nsec_2
1953*4882a593Smuzhiyun * Canceling the sides:
1954*4882a593Smuzhiyun * xtime_nsec_1 = offset + xtime_nsec_2
1955*4882a593Smuzhiyun * Which gives us:
1956*4882a593Smuzhiyun * xtime_nsec_2 = xtime_nsec_1 - offset
1957*4882a593Smuzhiyun * Which simplfies to:
1958*4882a593Smuzhiyun * xtime_nsec -= offset
1959*4882a593Smuzhiyun */
1960*4882a593Smuzhiyun if ((mult_adj > 0) && (tk->tkr_mono.mult + mult_adj < mult_adj)) {
1961*4882a593Smuzhiyun /* NTP adjustment caused clocksource mult overflow */
1962*4882a593Smuzhiyun WARN_ON_ONCE(1);
1963*4882a593Smuzhiyun return;
1964*4882a593Smuzhiyun }
1965*4882a593Smuzhiyun
1966*4882a593Smuzhiyun tk->tkr_mono.mult += mult_adj;
1967*4882a593Smuzhiyun tk->xtime_interval += interval;
1968*4882a593Smuzhiyun tk->tkr_mono.xtime_nsec -= offset;
1969*4882a593Smuzhiyun }
1970*4882a593Smuzhiyun
1971*4882a593Smuzhiyun /*
1972*4882a593Smuzhiyun * Adjust the timekeeper's multiplier to the correct frequency
1973*4882a593Smuzhiyun * and also to reduce the accumulated error value.
1974*4882a593Smuzhiyun */
timekeeping_adjust(struct timekeeper * tk,s64 offset)1975*4882a593Smuzhiyun static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
1976*4882a593Smuzhiyun {
1977*4882a593Smuzhiyun u32 mult;
1978*4882a593Smuzhiyun
1979*4882a593Smuzhiyun /*
1980*4882a593Smuzhiyun * Determine the multiplier from the current NTP tick length.
1981*4882a593Smuzhiyun * Avoid expensive division when the tick length doesn't change.
1982*4882a593Smuzhiyun */
1983*4882a593Smuzhiyun if (likely(tk->ntp_tick == ntp_tick_length())) {
1984*4882a593Smuzhiyun mult = tk->tkr_mono.mult - tk->ntp_err_mult;
1985*4882a593Smuzhiyun } else {
1986*4882a593Smuzhiyun tk->ntp_tick = ntp_tick_length();
1987*4882a593Smuzhiyun mult = div64_u64((tk->ntp_tick >> tk->ntp_error_shift) -
1988*4882a593Smuzhiyun tk->xtime_remainder, tk->cycle_interval);
1989*4882a593Smuzhiyun }
1990*4882a593Smuzhiyun
1991*4882a593Smuzhiyun /*
1992*4882a593Smuzhiyun * If the clock is behind the NTP time, increase the multiplier by 1
1993*4882a593Smuzhiyun * to catch up with it. If it's ahead and there was a remainder in the
1994*4882a593Smuzhiyun * tick division, the clock will slow down. Otherwise it will stay
1995*4882a593Smuzhiyun * ahead until the tick length changes to a non-divisible value.
1996*4882a593Smuzhiyun */
1997*4882a593Smuzhiyun tk->ntp_err_mult = tk->ntp_error > 0 ? 1 : 0;
1998*4882a593Smuzhiyun mult += tk->ntp_err_mult;
1999*4882a593Smuzhiyun
2000*4882a593Smuzhiyun timekeeping_apply_adjustment(tk, offset, mult - tk->tkr_mono.mult);
2001*4882a593Smuzhiyun
2002*4882a593Smuzhiyun if (unlikely(tk->tkr_mono.clock->maxadj &&
2003*4882a593Smuzhiyun (abs(tk->tkr_mono.mult - tk->tkr_mono.clock->mult)
2004*4882a593Smuzhiyun > tk->tkr_mono.clock->maxadj))) {
2005*4882a593Smuzhiyun printk_once(KERN_WARNING
2006*4882a593Smuzhiyun "Adjusting %s more than 11%% (%ld vs %ld)\n",
2007*4882a593Smuzhiyun tk->tkr_mono.clock->name, (long)tk->tkr_mono.mult,
2008*4882a593Smuzhiyun (long)tk->tkr_mono.clock->mult + tk->tkr_mono.clock->maxadj);
2009*4882a593Smuzhiyun }
2010*4882a593Smuzhiyun
2011*4882a593Smuzhiyun /*
2012*4882a593Smuzhiyun * It may be possible that when we entered this function, xtime_nsec
2013*4882a593Smuzhiyun * was very small. Further, if we're slightly speeding the clocksource
2014*4882a593Smuzhiyun * in the code above, its possible the required corrective factor to
2015*4882a593Smuzhiyun * xtime_nsec could cause it to underflow.
2016*4882a593Smuzhiyun *
2017*4882a593Smuzhiyun * Now, since we have already accumulated the second and the NTP
2018*4882a593Smuzhiyun * subsystem has been notified via second_overflow(), we need to skip
2019*4882a593Smuzhiyun * the next update.
2020*4882a593Smuzhiyun */
2021*4882a593Smuzhiyun if (unlikely((s64)tk->tkr_mono.xtime_nsec < 0)) {
2022*4882a593Smuzhiyun tk->tkr_mono.xtime_nsec += (u64)NSEC_PER_SEC <<
2023*4882a593Smuzhiyun tk->tkr_mono.shift;
2024*4882a593Smuzhiyun tk->xtime_sec--;
2025*4882a593Smuzhiyun tk->skip_second_overflow = 1;
2026*4882a593Smuzhiyun }
2027*4882a593Smuzhiyun }
2028*4882a593Smuzhiyun
2029*4882a593Smuzhiyun /**
2030*4882a593Smuzhiyun * accumulate_nsecs_to_secs - Accumulates nsecs into secs
2031*4882a593Smuzhiyun *
2032*4882a593Smuzhiyun * Helper function that accumulates the nsecs greater than a second
2033*4882a593Smuzhiyun * from the xtime_nsec field to the xtime_secs field.
2034*4882a593Smuzhiyun * It also calls into the NTP code to handle leapsecond processing.
2035*4882a593Smuzhiyun *
2036*4882a593Smuzhiyun */
accumulate_nsecs_to_secs(struct timekeeper * tk)2037*4882a593Smuzhiyun static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
2038*4882a593Smuzhiyun {
2039*4882a593Smuzhiyun u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
2040*4882a593Smuzhiyun unsigned int clock_set = 0;
2041*4882a593Smuzhiyun
2042*4882a593Smuzhiyun while (tk->tkr_mono.xtime_nsec >= nsecps) {
2043*4882a593Smuzhiyun int leap;
2044*4882a593Smuzhiyun
2045*4882a593Smuzhiyun tk->tkr_mono.xtime_nsec -= nsecps;
2046*4882a593Smuzhiyun tk->xtime_sec++;
2047*4882a593Smuzhiyun
2048*4882a593Smuzhiyun /*
2049*4882a593Smuzhiyun * Skip NTP update if this second was accumulated before,
2050*4882a593Smuzhiyun * i.e. xtime_nsec underflowed in timekeeping_adjust()
2051*4882a593Smuzhiyun */
2052*4882a593Smuzhiyun if (unlikely(tk->skip_second_overflow)) {
2053*4882a593Smuzhiyun tk->skip_second_overflow = 0;
2054*4882a593Smuzhiyun continue;
2055*4882a593Smuzhiyun }
2056*4882a593Smuzhiyun
2057*4882a593Smuzhiyun /* Figure out if its a leap sec and apply if needed */
2058*4882a593Smuzhiyun leap = second_overflow(tk->xtime_sec);
2059*4882a593Smuzhiyun if (unlikely(leap)) {
2060*4882a593Smuzhiyun struct timespec64 ts;
2061*4882a593Smuzhiyun
2062*4882a593Smuzhiyun tk->xtime_sec += leap;
2063*4882a593Smuzhiyun
2064*4882a593Smuzhiyun ts.tv_sec = leap;
2065*4882a593Smuzhiyun ts.tv_nsec = 0;
2066*4882a593Smuzhiyun tk_set_wall_to_mono(tk,
2067*4882a593Smuzhiyun timespec64_sub(tk->wall_to_monotonic, ts));
2068*4882a593Smuzhiyun
2069*4882a593Smuzhiyun __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
2070*4882a593Smuzhiyun
2071*4882a593Smuzhiyun clock_set = TK_CLOCK_WAS_SET;
2072*4882a593Smuzhiyun }
2073*4882a593Smuzhiyun }
2074*4882a593Smuzhiyun return clock_set;
2075*4882a593Smuzhiyun }
2076*4882a593Smuzhiyun
2077*4882a593Smuzhiyun /**
2078*4882a593Smuzhiyun * logarithmic_accumulation - shifted accumulation of cycles
2079*4882a593Smuzhiyun *
2080*4882a593Smuzhiyun * This functions accumulates a shifted interval of cycles into
2081*4882a593Smuzhiyun * a shifted interval nanoseconds. Allows for O(log) accumulation
2082*4882a593Smuzhiyun * loop.
2083*4882a593Smuzhiyun *
2084*4882a593Smuzhiyun * Returns the unconsumed cycles.
2085*4882a593Smuzhiyun */
logarithmic_accumulation(struct timekeeper * tk,u64 offset,u32 shift,unsigned int * clock_set)2086*4882a593Smuzhiyun static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset,
2087*4882a593Smuzhiyun u32 shift, unsigned int *clock_set)
2088*4882a593Smuzhiyun {
2089*4882a593Smuzhiyun u64 interval = tk->cycle_interval << shift;
2090*4882a593Smuzhiyun u64 snsec_per_sec;
2091*4882a593Smuzhiyun
2092*4882a593Smuzhiyun /* If the offset is smaller than a shifted interval, do nothing */
2093*4882a593Smuzhiyun if (offset < interval)
2094*4882a593Smuzhiyun return offset;
2095*4882a593Smuzhiyun
2096*4882a593Smuzhiyun /* Accumulate one shifted interval */
2097*4882a593Smuzhiyun offset -= interval;
2098*4882a593Smuzhiyun tk->tkr_mono.cycle_last += interval;
2099*4882a593Smuzhiyun tk->tkr_raw.cycle_last += interval;
2100*4882a593Smuzhiyun
2101*4882a593Smuzhiyun tk->tkr_mono.xtime_nsec += tk->xtime_interval << shift;
2102*4882a593Smuzhiyun *clock_set |= accumulate_nsecs_to_secs(tk);
2103*4882a593Smuzhiyun
2104*4882a593Smuzhiyun /* Accumulate raw time */
2105*4882a593Smuzhiyun tk->tkr_raw.xtime_nsec += tk->raw_interval << shift;
2106*4882a593Smuzhiyun snsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
2107*4882a593Smuzhiyun while (tk->tkr_raw.xtime_nsec >= snsec_per_sec) {
2108*4882a593Smuzhiyun tk->tkr_raw.xtime_nsec -= snsec_per_sec;
2109*4882a593Smuzhiyun tk->raw_sec++;
2110*4882a593Smuzhiyun }
2111*4882a593Smuzhiyun
2112*4882a593Smuzhiyun /* Accumulate error between NTP and clock interval */
2113*4882a593Smuzhiyun tk->ntp_error += tk->ntp_tick << shift;
2114*4882a593Smuzhiyun tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
2115*4882a593Smuzhiyun (tk->ntp_error_shift + shift);
2116*4882a593Smuzhiyun
2117*4882a593Smuzhiyun return offset;
2118*4882a593Smuzhiyun }
2119*4882a593Smuzhiyun
2120*4882a593Smuzhiyun /*
2121*4882a593Smuzhiyun * timekeeping_advance - Updates the timekeeper to the current time and
2122*4882a593Smuzhiyun * current NTP tick length
2123*4882a593Smuzhiyun */
timekeeping_advance(enum timekeeping_adv_mode mode)2124*4882a593Smuzhiyun static void timekeeping_advance(enum timekeeping_adv_mode mode)
2125*4882a593Smuzhiyun {
2126*4882a593Smuzhiyun struct timekeeper *real_tk = &tk_core.timekeeper;
2127*4882a593Smuzhiyun struct timekeeper *tk = &shadow_timekeeper;
2128*4882a593Smuzhiyun u64 offset;
2129*4882a593Smuzhiyun int shift = 0, maxshift;
2130*4882a593Smuzhiyun unsigned int clock_set = 0;
2131*4882a593Smuzhiyun unsigned long flags;
2132*4882a593Smuzhiyun
2133*4882a593Smuzhiyun raw_spin_lock_irqsave(&timekeeper_lock, flags);
2134*4882a593Smuzhiyun
2135*4882a593Smuzhiyun /* Make sure we're fully resumed: */
2136*4882a593Smuzhiyun if (unlikely(timekeeping_suspended))
2137*4882a593Smuzhiyun goto out;
2138*4882a593Smuzhiyun
2139*4882a593Smuzhiyun #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
2140*4882a593Smuzhiyun offset = real_tk->cycle_interval;
2141*4882a593Smuzhiyun
2142*4882a593Smuzhiyun if (mode != TK_ADV_TICK)
2143*4882a593Smuzhiyun goto out;
2144*4882a593Smuzhiyun #else
2145*4882a593Smuzhiyun offset = clocksource_delta(tk_clock_read(&tk->tkr_mono),
2146*4882a593Smuzhiyun tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
2147*4882a593Smuzhiyun
2148*4882a593Smuzhiyun /* Check if there's really nothing to do */
2149*4882a593Smuzhiyun if (offset < real_tk->cycle_interval && mode == TK_ADV_TICK)
2150*4882a593Smuzhiyun goto out;
2151*4882a593Smuzhiyun #endif
2152*4882a593Smuzhiyun
2153*4882a593Smuzhiyun /* Do some additional sanity checking */
2154*4882a593Smuzhiyun timekeeping_check_update(tk, offset);
2155*4882a593Smuzhiyun
2156*4882a593Smuzhiyun /*
2157*4882a593Smuzhiyun * With NO_HZ we may have to accumulate many cycle_intervals
2158*4882a593Smuzhiyun * (think "ticks") worth of time at once. To do this efficiently,
2159*4882a593Smuzhiyun * we calculate the largest doubling multiple of cycle_intervals
2160*4882a593Smuzhiyun * that is smaller than the offset. We then accumulate that
2161*4882a593Smuzhiyun * chunk in one go, and then try to consume the next smaller
2162*4882a593Smuzhiyun * doubled multiple.
2163*4882a593Smuzhiyun */
2164*4882a593Smuzhiyun shift = ilog2(offset) - ilog2(tk->cycle_interval);
2165*4882a593Smuzhiyun shift = max(0, shift);
2166*4882a593Smuzhiyun /* Bound shift to one less than what overflows tick_length */
2167*4882a593Smuzhiyun maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
2168*4882a593Smuzhiyun shift = min(shift, maxshift);
2169*4882a593Smuzhiyun while (offset >= tk->cycle_interval) {
2170*4882a593Smuzhiyun offset = logarithmic_accumulation(tk, offset, shift,
2171*4882a593Smuzhiyun &clock_set);
2172*4882a593Smuzhiyun if (offset < tk->cycle_interval<<shift)
2173*4882a593Smuzhiyun shift--;
2174*4882a593Smuzhiyun }
2175*4882a593Smuzhiyun
2176*4882a593Smuzhiyun /* Adjust the multiplier to correct NTP error */
2177*4882a593Smuzhiyun timekeeping_adjust(tk, offset);
2178*4882a593Smuzhiyun
2179*4882a593Smuzhiyun /*
2180*4882a593Smuzhiyun * Finally, make sure that after the rounding
2181*4882a593Smuzhiyun * xtime_nsec isn't larger than NSEC_PER_SEC
2182*4882a593Smuzhiyun */
2183*4882a593Smuzhiyun clock_set |= accumulate_nsecs_to_secs(tk);
2184*4882a593Smuzhiyun
2185*4882a593Smuzhiyun write_seqcount_begin(&tk_core.seq);
2186*4882a593Smuzhiyun /*
2187*4882a593Smuzhiyun * Update the real timekeeper.
2188*4882a593Smuzhiyun *
2189*4882a593Smuzhiyun * We could avoid this memcpy by switching pointers, but that
2190*4882a593Smuzhiyun * requires changes to all other timekeeper usage sites as
2191*4882a593Smuzhiyun * well, i.e. move the timekeeper pointer getter into the
2192*4882a593Smuzhiyun * spinlocked/seqcount protected sections. And we trade this
2193*4882a593Smuzhiyun * memcpy under the tk_core.seq against one before we start
2194*4882a593Smuzhiyun * updating.
2195*4882a593Smuzhiyun */
2196*4882a593Smuzhiyun timekeeping_update(tk, clock_set);
2197*4882a593Smuzhiyun memcpy(real_tk, tk, sizeof(*tk));
2198*4882a593Smuzhiyun /* The memcpy must come last. Do not put anything here! */
2199*4882a593Smuzhiyun write_seqcount_end(&tk_core.seq);
2200*4882a593Smuzhiyun out:
2201*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
2202*4882a593Smuzhiyun if (clock_set)
2203*4882a593Smuzhiyun /* Have to call _delayed version, since in irq context*/
2204*4882a593Smuzhiyun clock_was_set_delayed();
2205*4882a593Smuzhiyun }
2206*4882a593Smuzhiyun
2207*4882a593Smuzhiyun /**
2208*4882a593Smuzhiyun * update_wall_time - Uses the current clocksource to increment the wall time
2209*4882a593Smuzhiyun *
2210*4882a593Smuzhiyun */
update_wall_time(void)2211*4882a593Smuzhiyun void update_wall_time(void)
2212*4882a593Smuzhiyun {
2213*4882a593Smuzhiyun timekeeping_advance(TK_ADV_TICK);
2214*4882a593Smuzhiyun }
2215*4882a593Smuzhiyun
2216*4882a593Smuzhiyun /**
2217*4882a593Smuzhiyun * getboottime64 - Return the real time of system boot.
2218*4882a593Smuzhiyun * @ts: pointer to the timespec64 to be set
2219*4882a593Smuzhiyun *
2220*4882a593Smuzhiyun * Returns the wall-time of boot in a timespec64.
2221*4882a593Smuzhiyun *
2222*4882a593Smuzhiyun * This is based on the wall_to_monotonic offset and the total suspend
2223*4882a593Smuzhiyun * time. Calls to settimeofday will affect the value returned (which
2224*4882a593Smuzhiyun * basically means that however wrong your real time clock is at boot time,
2225*4882a593Smuzhiyun * you get the right time here).
2226*4882a593Smuzhiyun */
getboottime64(struct timespec64 * ts)2227*4882a593Smuzhiyun void getboottime64(struct timespec64 *ts)
2228*4882a593Smuzhiyun {
2229*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
2230*4882a593Smuzhiyun ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot);
2231*4882a593Smuzhiyun
2232*4882a593Smuzhiyun *ts = ktime_to_timespec64(t);
2233*4882a593Smuzhiyun }
2234*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(getboottime64);
2235*4882a593Smuzhiyun
ktime_get_coarse_real_ts64(struct timespec64 * ts)2236*4882a593Smuzhiyun void ktime_get_coarse_real_ts64(struct timespec64 *ts)
2237*4882a593Smuzhiyun {
2238*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
2239*4882a593Smuzhiyun unsigned int seq;
2240*4882a593Smuzhiyun
2241*4882a593Smuzhiyun do {
2242*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
2243*4882a593Smuzhiyun
2244*4882a593Smuzhiyun *ts = tk_xtime(tk);
2245*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
2246*4882a593Smuzhiyun }
2247*4882a593Smuzhiyun EXPORT_SYMBOL(ktime_get_coarse_real_ts64);
2248*4882a593Smuzhiyun
ktime_get_coarse_ts64(struct timespec64 * ts)2249*4882a593Smuzhiyun void ktime_get_coarse_ts64(struct timespec64 *ts)
2250*4882a593Smuzhiyun {
2251*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
2252*4882a593Smuzhiyun struct timespec64 now, mono;
2253*4882a593Smuzhiyun unsigned int seq;
2254*4882a593Smuzhiyun
2255*4882a593Smuzhiyun do {
2256*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
2257*4882a593Smuzhiyun
2258*4882a593Smuzhiyun now = tk_xtime(tk);
2259*4882a593Smuzhiyun mono = tk->wall_to_monotonic;
2260*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
2261*4882a593Smuzhiyun
2262*4882a593Smuzhiyun set_normalized_timespec64(ts, now.tv_sec + mono.tv_sec,
2263*4882a593Smuzhiyun now.tv_nsec + mono.tv_nsec);
2264*4882a593Smuzhiyun }
2265*4882a593Smuzhiyun EXPORT_SYMBOL(ktime_get_coarse_ts64);
2266*4882a593Smuzhiyun
2267*4882a593Smuzhiyun /*
2268*4882a593Smuzhiyun * Must hold jiffies_lock
2269*4882a593Smuzhiyun */
do_timer(unsigned long ticks)2270*4882a593Smuzhiyun void do_timer(unsigned long ticks)
2271*4882a593Smuzhiyun {
2272*4882a593Smuzhiyun jiffies_64 += ticks;
2273*4882a593Smuzhiyun calc_global_load();
2274*4882a593Smuzhiyun }
2275*4882a593Smuzhiyun
2276*4882a593Smuzhiyun /**
2277*4882a593Smuzhiyun * ktime_get_update_offsets_now - hrtimer helper
2278*4882a593Smuzhiyun * @cwsseq: pointer to check and store the clock was set sequence number
2279*4882a593Smuzhiyun * @offs_real: pointer to storage for monotonic -> realtime offset
2280*4882a593Smuzhiyun * @offs_boot: pointer to storage for monotonic -> boottime offset
2281*4882a593Smuzhiyun * @offs_tai: pointer to storage for monotonic -> clock tai offset
2282*4882a593Smuzhiyun *
2283*4882a593Smuzhiyun * Returns current monotonic time and updates the offsets if the
2284*4882a593Smuzhiyun * sequence number in @cwsseq and timekeeper.clock_was_set_seq are
2285*4882a593Smuzhiyun * different.
2286*4882a593Smuzhiyun *
2287*4882a593Smuzhiyun * Called from hrtimer_interrupt() or retrigger_next_event()
2288*4882a593Smuzhiyun */
ktime_get_update_offsets_now(unsigned int * cwsseq,ktime_t * offs_real,ktime_t * offs_boot,ktime_t * offs_tai)2289*4882a593Smuzhiyun ktime_t ktime_get_update_offsets_now(unsigned int *cwsseq, ktime_t *offs_real,
2290*4882a593Smuzhiyun ktime_t *offs_boot, ktime_t *offs_tai)
2291*4882a593Smuzhiyun {
2292*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
2293*4882a593Smuzhiyun unsigned int seq;
2294*4882a593Smuzhiyun ktime_t base;
2295*4882a593Smuzhiyun u64 nsecs;
2296*4882a593Smuzhiyun
2297*4882a593Smuzhiyun do {
2298*4882a593Smuzhiyun seq = read_seqcount_begin(&tk_core.seq);
2299*4882a593Smuzhiyun
2300*4882a593Smuzhiyun base = tk->tkr_mono.base;
2301*4882a593Smuzhiyun nsecs = timekeeping_get_ns(&tk->tkr_mono);
2302*4882a593Smuzhiyun base = ktime_add_ns(base, nsecs);
2303*4882a593Smuzhiyun
2304*4882a593Smuzhiyun if (*cwsseq != tk->clock_was_set_seq) {
2305*4882a593Smuzhiyun *cwsseq = tk->clock_was_set_seq;
2306*4882a593Smuzhiyun *offs_real = tk->offs_real;
2307*4882a593Smuzhiyun *offs_boot = tk->offs_boot;
2308*4882a593Smuzhiyun *offs_tai = tk->offs_tai;
2309*4882a593Smuzhiyun }
2310*4882a593Smuzhiyun
2311*4882a593Smuzhiyun /* Handle leapsecond insertion adjustments */
2312*4882a593Smuzhiyun if (unlikely(base >= tk->next_leap_ktime))
2313*4882a593Smuzhiyun *offs_real = ktime_sub(tk->offs_real, ktime_set(1, 0));
2314*4882a593Smuzhiyun
2315*4882a593Smuzhiyun } while (read_seqcount_retry(&tk_core.seq, seq));
2316*4882a593Smuzhiyun
2317*4882a593Smuzhiyun return base;
2318*4882a593Smuzhiyun }
2319*4882a593Smuzhiyun
2320*4882a593Smuzhiyun /**
2321*4882a593Smuzhiyun * timekeeping_validate_timex - Ensures the timex is ok for use in do_adjtimex
2322*4882a593Smuzhiyun */
timekeeping_validate_timex(const struct __kernel_timex * txc)2323*4882a593Smuzhiyun static int timekeeping_validate_timex(const struct __kernel_timex *txc)
2324*4882a593Smuzhiyun {
2325*4882a593Smuzhiyun if (txc->modes & ADJ_ADJTIME) {
2326*4882a593Smuzhiyun /* singleshot must not be used with any other mode bits */
2327*4882a593Smuzhiyun if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
2328*4882a593Smuzhiyun return -EINVAL;
2329*4882a593Smuzhiyun if (!(txc->modes & ADJ_OFFSET_READONLY) &&
2330*4882a593Smuzhiyun !capable(CAP_SYS_TIME))
2331*4882a593Smuzhiyun return -EPERM;
2332*4882a593Smuzhiyun } else {
2333*4882a593Smuzhiyun /* In order to modify anything, you gotta be super-user! */
2334*4882a593Smuzhiyun if (txc->modes && !capable(CAP_SYS_TIME))
2335*4882a593Smuzhiyun return -EPERM;
2336*4882a593Smuzhiyun /*
2337*4882a593Smuzhiyun * if the quartz is off by more than 10% then
2338*4882a593Smuzhiyun * something is VERY wrong!
2339*4882a593Smuzhiyun */
2340*4882a593Smuzhiyun if (txc->modes & ADJ_TICK &&
2341*4882a593Smuzhiyun (txc->tick < 900000/USER_HZ ||
2342*4882a593Smuzhiyun txc->tick > 1100000/USER_HZ))
2343*4882a593Smuzhiyun return -EINVAL;
2344*4882a593Smuzhiyun }
2345*4882a593Smuzhiyun
2346*4882a593Smuzhiyun if (txc->modes & ADJ_SETOFFSET) {
2347*4882a593Smuzhiyun /* In order to inject time, you gotta be super-user! */
2348*4882a593Smuzhiyun if (!capable(CAP_SYS_TIME))
2349*4882a593Smuzhiyun return -EPERM;
2350*4882a593Smuzhiyun
2351*4882a593Smuzhiyun /*
2352*4882a593Smuzhiyun * Validate if a timespec/timeval used to inject a time
2353*4882a593Smuzhiyun * offset is valid. Offsets can be postive or negative, so
2354*4882a593Smuzhiyun * we don't check tv_sec. The value of the timeval/timespec
2355*4882a593Smuzhiyun * is the sum of its fields,but *NOTE*:
2356*4882a593Smuzhiyun * The field tv_usec/tv_nsec must always be non-negative and
2357*4882a593Smuzhiyun * we can't have more nanoseconds/microseconds than a second.
2358*4882a593Smuzhiyun */
2359*4882a593Smuzhiyun if (txc->time.tv_usec < 0)
2360*4882a593Smuzhiyun return -EINVAL;
2361*4882a593Smuzhiyun
2362*4882a593Smuzhiyun if (txc->modes & ADJ_NANO) {
2363*4882a593Smuzhiyun if (txc->time.tv_usec >= NSEC_PER_SEC)
2364*4882a593Smuzhiyun return -EINVAL;
2365*4882a593Smuzhiyun } else {
2366*4882a593Smuzhiyun if (txc->time.tv_usec >= USEC_PER_SEC)
2367*4882a593Smuzhiyun return -EINVAL;
2368*4882a593Smuzhiyun }
2369*4882a593Smuzhiyun }
2370*4882a593Smuzhiyun
2371*4882a593Smuzhiyun /*
2372*4882a593Smuzhiyun * Check for potential multiplication overflows that can
2373*4882a593Smuzhiyun * only happen on 64-bit systems:
2374*4882a593Smuzhiyun */
2375*4882a593Smuzhiyun if ((txc->modes & ADJ_FREQUENCY) && (BITS_PER_LONG == 64)) {
2376*4882a593Smuzhiyun if (LLONG_MIN / PPM_SCALE > txc->freq)
2377*4882a593Smuzhiyun return -EINVAL;
2378*4882a593Smuzhiyun if (LLONG_MAX / PPM_SCALE < txc->freq)
2379*4882a593Smuzhiyun return -EINVAL;
2380*4882a593Smuzhiyun }
2381*4882a593Smuzhiyun
2382*4882a593Smuzhiyun return 0;
2383*4882a593Smuzhiyun }
2384*4882a593Smuzhiyun
2385*4882a593Smuzhiyun /**
2386*4882a593Smuzhiyun * random_get_entropy_fallback - Returns the raw clock source value,
2387*4882a593Smuzhiyun * used by random.c for platforms with no valid random_get_entropy().
2388*4882a593Smuzhiyun */
random_get_entropy_fallback(void)2389*4882a593Smuzhiyun unsigned long random_get_entropy_fallback(void)
2390*4882a593Smuzhiyun {
2391*4882a593Smuzhiyun struct tk_read_base *tkr = &tk_core.timekeeper.tkr_mono;
2392*4882a593Smuzhiyun struct clocksource *clock = READ_ONCE(tkr->clock);
2393*4882a593Smuzhiyun
2394*4882a593Smuzhiyun if (unlikely(timekeeping_suspended || !clock))
2395*4882a593Smuzhiyun return 0;
2396*4882a593Smuzhiyun return clock->read(clock);
2397*4882a593Smuzhiyun }
2398*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(random_get_entropy_fallback);
2399*4882a593Smuzhiyun
2400*4882a593Smuzhiyun /**
2401*4882a593Smuzhiyun * do_adjtimex() - Accessor function to NTP __do_adjtimex function
2402*4882a593Smuzhiyun */
do_adjtimex(struct __kernel_timex * txc)2403*4882a593Smuzhiyun int do_adjtimex(struct __kernel_timex *txc)
2404*4882a593Smuzhiyun {
2405*4882a593Smuzhiyun struct timekeeper *tk = &tk_core.timekeeper;
2406*4882a593Smuzhiyun struct audit_ntp_data ad;
2407*4882a593Smuzhiyun unsigned long flags;
2408*4882a593Smuzhiyun struct timespec64 ts;
2409*4882a593Smuzhiyun s32 orig_tai, tai;
2410*4882a593Smuzhiyun int ret;
2411*4882a593Smuzhiyun
2412*4882a593Smuzhiyun /* Validate the data before disabling interrupts */
2413*4882a593Smuzhiyun ret = timekeeping_validate_timex(txc);
2414*4882a593Smuzhiyun if (ret)
2415*4882a593Smuzhiyun return ret;
2416*4882a593Smuzhiyun add_device_randomness(txc, sizeof(*txc));
2417*4882a593Smuzhiyun
2418*4882a593Smuzhiyun if (txc->modes & ADJ_SETOFFSET) {
2419*4882a593Smuzhiyun struct timespec64 delta;
2420*4882a593Smuzhiyun delta.tv_sec = txc->time.tv_sec;
2421*4882a593Smuzhiyun delta.tv_nsec = txc->time.tv_usec;
2422*4882a593Smuzhiyun if (!(txc->modes & ADJ_NANO))
2423*4882a593Smuzhiyun delta.tv_nsec *= 1000;
2424*4882a593Smuzhiyun ret = timekeeping_inject_offset(&delta);
2425*4882a593Smuzhiyun if (ret)
2426*4882a593Smuzhiyun return ret;
2427*4882a593Smuzhiyun
2428*4882a593Smuzhiyun audit_tk_injoffset(delta);
2429*4882a593Smuzhiyun }
2430*4882a593Smuzhiyun
2431*4882a593Smuzhiyun audit_ntp_init(&ad);
2432*4882a593Smuzhiyun
2433*4882a593Smuzhiyun ktime_get_real_ts64(&ts);
2434*4882a593Smuzhiyun add_device_randomness(&ts, sizeof(ts));
2435*4882a593Smuzhiyun
2436*4882a593Smuzhiyun raw_spin_lock_irqsave(&timekeeper_lock, flags);
2437*4882a593Smuzhiyun write_seqcount_begin(&tk_core.seq);
2438*4882a593Smuzhiyun
2439*4882a593Smuzhiyun orig_tai = tai = tk->tai_offset;
2440*4882a593Smuzhiyun ret = __do_adjtimex(txc, &ts, &tai, &ad);
2441*4882a593Smuzhiyun
2442*4882a593Smuzhiyun if (tai != orig_tai) {
2443*4882a593Smuzhiyun __timekeeping_set_tai_offset(tk, tai);
2444*4882a593Smuzhiyun timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
2445*4882a593Smuzhiyun }
2446*4882a593Smuzhiyun tk_update_leap_state(tk);
2447*4882a593Smuzhiyun
2448*4882a593Smuzhiyun write_seqcount_end(&tk_core.seq);
2449*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
2450*4882a593Smuzhiyun
2451*4882a593Smuzhiyun audit_ntp_log(&ad);
2452*4882a593Smuzhiyun
2453*4882a593Smuzhiyun /* Update the multiplier immediately if frequency was set directly */
2454*4882a593Smuzhiyun if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK))
2455*4882a593Smuzhiyun timekeeping_advance(TK_ADV_FREQ);
2456*4882a593Smuzhiyun
2457*4882a593Smuzhiyun if (tai != orig_tai)
2458*4882a593Smuzhiyun clock_was_set();
2459*4882a593Smuzhiyun
2460*4882a593Smuzhiyun ntp_notify_cmos_timer();
2461*4882a593Smuzhiyun
2462*4882a593Smuzhiyun return ret;
2463*4882a593Smuzhiyun }
2464*4882a593Smuzhiyun
2465*4882a593Smuzhiyun #ifdef CONFIG_NTP_PPS
2466*4882a593Smuzhiyun /**
2467*4882a593Smuzhiyun * hardpps() - Accessor function to NTP __hardpps function
2468*4882a593Smuzhiyun */
hardpps(const struct timespec64 * phase_ts,const struct timespec64 * raw_ts)2469*4882a593Smuzhiyun void hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts)
2470*4882a593Smuzhiyun {
2471*4882a593Smuzhiyun unsigned long flags;
2472*4882a593Smuzhiyun
2473*4882a593Smuzhiyun raw_spin_lock_irqsave(&timekeeper_lock, flags);
2474*4882a593Smuzhiyun write_seqcount_begin(&tk_core.seq);
2475*4882a593Smuzhiyun
2476*4882a593Smuzhiyun __hardpps(phase_ts, raw_ts);
2477*4882a593Smuzhiyun
2478*4882a593Smuzhiyun write_seqcount_end(&tk_core.seq);
2479*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
2480*4882a593Smuzhiyun }
2481*4882a593Smuzhiyun EXPORT_SYMBOL(hardpps);
2482*4882a593Smuzhiyun #endif /* CONFIG_NTP_PPS */
2483*4882a593Smuzhiyun
2484*4882a593Smuzhiyun /**
2485*4882a593Smuzhiyun * xtime_update() - advances the timekeeping infrastructure
2486*4882a593Smuzhiyun * @ticks: number of ticks, that have elapsed since the last call.
2487*4882a593Smuzhiyun *
2488*4882a593Smuzhiyun * Must be called with interrupts disabled.
2489*4882a593Smuzhiyun */
xtime_update(unsigned long ticks)2490*4882a593Smuzhiyun void xtime_update(unsigned long ticks)
2491*4882a593Smuzhiyun {
2492*4882a593Smuzhiyun raw_spin_lock(&jiffies_lock);
2493*4882a593Smuzhiyun write_seqcount_begin(&jiffies_seq);
2494*4882a593Smuzhiyun do_timer(ticks);
2495*4882a593Smuzhiyun write_seqcount_end(&jiffies_seq);
2496*4882a593Smuzhiyun raw_spin_unlock(&jiffies_lock);
2497*4882a593Smuzhiyun update_wall_time();
2498*4882a593Smuzhiyun }
2499