]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - kernel/time/timekeeping.c
timekeeping: Rework frequency adjustments to work better w/ nohz
[mirror_ubuntu-zesty-kernel.git] / kernel / time / timekeeping.c
CommitLineData
8524070b
JS
1/*
2 * linux/kernel/time/timekeeping.c
3 *
4 * Kernel timekeeping code and accessor functions
5 *
6 * This code was moved from linux/kernel/timer.c.
7 * Please see that file for copyright and history logs.
8 *
9 */
10
d7b4202e 11#include <linux/timekeeper_internal.h>
8524070b
JS
12#include <linux/module.h>
13#include <linux/interrupt.h>
14#include <linux/percpu.h>
15#include <linux/init.h>
16#include <linux/mm.h>
d43c36dc 17#include <linux/sched.h>
e1a85b2c 18#include <linux/syscore_ops.h>
8524070b
JS
19#include <linux/clocksource.h>
20#include <linux/jiffies.h>
21#include <linux/time.h>
22#include <linux/tick.h>
75c5158f 23#include <linux/stop_machine.h>
e0b306fe 24#include <linux/pvclock_gtod.h>
52f5684c 25#include <linux/compiler.h>
8524070b 26
eb93e4d9 27#include "tick-internal.h"
aa6f9c59 28#include "ntp_internal.h"
5c83545f 29#include "timekeeping_internal.h"
155ec602 30
04397fe9
DV
31#define TK_CLEAR_NTP (1 << 0)
32#define TK_MIRROR (1 << 1)
780427f0 33#define TK_CLOCK_WAS_SET (1 << 2)
04397fe9 34
3fdb14fd
TG
35/*
36 * The most important data for readout fits into a single 64 byte
37 * cache line.
38 */
39static struct {
40 seqcount_t seq;
41 struct timekeeper timekeeper;
42} tk_core ____cacheline_aligned;
43
9a7a71b1 44static DEFINE_RAW_SPINLOCK(timekeeper_lock);
48cdc135 45static struct timekeeper shadow_timekeeper;
155ec602 46
4396e058
TG
47/**
48 * struct tk_fast - NMI safe timekeeper
49 * @seq: Sequence counter for protecting updates. The lowest bit
50 * is the index for the tk_read_base array
51 * @base: tk_read_base array. Access is indexed by the lowest bit of
52 * @seq.
53 *
54 * See @update_fast_timekeeper() below.
55 */
56struct tk_fast {
57 seqcount_t seq;
58 struct tk_read_base base[2];
59};
60
61static struct tk_fast tk_fast_mono ____cacheline_aligned;
62
8fcce546
JS
63/* flag for if timekeeping is suspended */
64int __read_mostly timekeeping_suspended;
65
31ade306
FT
66/* Flag for if there is a persistent clock on this platform */
67bool __read_mostly persistent_clock_exist = false;
68
1e75fa8b
JS
69static inline void tk_normalize_xtime(struct timekeeper *tk)
70{
d28ede83
TG
71 while (tk->tkr.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr.shift)) {
72 tk->tkr.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr.shift;
1e75fa8b
JS
73 tk->xtime_sec++;
74 }
75}
76
c905fae4
TG
77static inline struct timespec64 tk_xtime(struct timekeeper *tk)
78{
79 struct timespec64 ts;
80
81 ts.tv_sec = tk->xtime_sec;
d28ede83 82 ts.tv_nsec = (long)(tk->tkr.xtime_nsec >> tk->tkr.shift);
c905fae4
TG
83 return ts;
84}
85
7d489d15 86static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
1e75fa8b
JS
87{
88 tk->xtime_sec = ts->tv_sec;
d28ede83 89 tk->tkr.xtime_nsec = (u64)ts->tv_nsec << tk->tkr.shift;
1e75fa8b
JS
90}
91
7d489d15 92static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
1e75fa8b
JS
93{
94 tk->xtime_sec += ts->tv_sec;
d28ede83 95 tk->tkr.xtime_nsec += (u64)ts->tv_nsec << tk->tkr.shift;
784ffcbb 96 tk_normalize_xtime(tk);
1e75fa8b 97}
8fcce546 98
7d489d15 99static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
6d0ef903 100{
7d489d15 101 struct timespec64 tmp;
6d0ef903
JS
102
103 /*
104 * Verify consistency of: offset_real = -wall_to_monotonic
105 * before modifying anything
106 */
7d489d15 107 set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec,
6d0ef903 108 -tk->wall_to_monotonic.tv_nsec);
7d489d15 109 WARN_ON_ONCE(tk->offs_real.tv64 != timespec64_to_ktime(tmp).tv64);
6d0ef903 110 tk->wall_to_monotonic = wtm;
7d489d15
JS
111 set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
112 tk->offs_real = timespec64_to_ktime(tmp);
04005f60 113 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
6d0ef903
JS
114}
115
47da70d3 116static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
6d0ef903 117{
47da70d3 118 tk->offs_boot = ktime_add(tk->offs_boot, delta);
6d0ef903
JS
119}
120
155ec602 121/**
d26e4fe0 122 * tk_setup_internals - Set up internals to use clocksource clock.
155ec602 123 *
d26e4fe0 124 * @tk: The target timekeeper to setup.
155ec602
MS
125 * @clock: Pointer to clocksource.
126 *
127 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
128 * pair and interval request.
129 *
130 * Unless you're the timekeeping code, you should not be using this!
131 */
f726a697 132static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
155ec602
MS
133{
134 cycle_t interval;
a386b5af 135 u64 tmp, ntpinterval;
1e75fa8b 136 struct clocksource *old_clock;
155ec602 137
d28ede83
TG
138 old_clock = tk->tkr.clock;
139 tk->tkr.clock = clock;
140 tk->tkr.read = clock->read;
141 tk->tkr.mask = clock->mask;
142 tk->tkr.cycle_last = tk->tkr.read(clock);
155ec602
MS
143
144 /* Do the ns -> cycle conversion first, using original mult */
145 tmp = NTP_INTERVAL_LENGTH;
146 tmp <<= clock->shift;
a386b5af 147 ntpinterval = tmp;
0a544198
MS
148 tmp += clock->mult/2;
149 do_div(tmp, clock->mult);
155ec602
MS
150 if (tmp == 0)
151 tmp = 1;
152
153 interval = (cycle_t) tmp;
f726a697 154 tk->cycle_interval = interval;
155ec602
MS
155
156 /* Go back from cycles -> shifted ns */
f726a697
JS
157 tk->xtime_interval = (u64) interval * clock->mult;
158 tk->xtime_remainder = ntpinterval - tk->xtime_interval;
159 tk->raw_interval =
0a544198 160 ((u64) interval * clock->mult) >> clock->shift;
155ec602 161
1e75fa8b
JS
162 /* if changing clocks, convert xtime_nsec shift units */
163 if (old_clock) {
164 int shift_change = clock->shift - old_clock->shift;
165 if (shift_change < 0)
d28ede83 166 tk->tkr.xtime_nsec >>= -shift_change;
1e75fa8b 167 else
d28ede83 168 tk->tkr.xtime_nsec <<= shift_change;
1e75fa8b 169 }
d28ede83 170 tk->tkr.shift = clock->shift;
155ec602 171
f726a697
JS
172 tk->ntp_error = 0;
173 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
0a544198
MS
174
175 /*
176 * The timekeeper keeps its own mult values for the currently
177 * active clocksource. These value will be adjusted via NTP
178 * to counteract clock drifting.
179 */
d28ede83 180 tk->tkr.mult = clock->mult;
dc491596 181 tk->ntp_err_mult = 0;
155ec602 182}
8524070b 183
2ba2a305 184/* Timekeeper helper functions. */
7b1f6207
SW
185
186#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
e06fde37
TG
187static u32 default_arch_gettimeoffset(void) { return 0; }
188u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset;
7b1f6207 189#else
e06fde37 190static inline u32 arch_gettimeoffset(void) { return 0; }
7b1f6207
SW
191#endif
192
0e5ac3a8 193static inline s64 timekeeping_get_ns(struct tk_read_base *tkr)
2ba2a305 194{
3a978377 195 cycle_t cycle_now, delta;
1e75fa8b 196 s64 nsec;
2ba2a305
MS
197
198 /* read clocksource: */
0e5ac3a8 199 cycle_now = tkr->read(tkr->clock);
2ba2a305
MS
200
201 /* calculate the delta since the last update_wall_time: */
0e5ac3a8 202 delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
2ba2a305 203
0e5ac3a8
TG
204 nsec = delta * tkr->mult + tkr->xtime_nsec;
205 nsec >>= tkr->shift;
f2a5a085 206
7b1f6207 207 /* If arch requires, add in get_arch_timeoffset() */
e06fde37 208 return nsec + arch_gettimeoffset();
2ba2a305
MS
209}
210
f726a697 211static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
2ba2a305 212{
d28ede83 213 struct clocksource *clock = tk->tkr.clock;
3a978377 214 cycle_t cycle_now, delta;
f2a5a085 215 s64 nsec;
2ba2a305
MS
216
217 /* read clocksource: */
d28ede83 218 cycle_now = tk->tkr.read(clock);
2ba2a305
MS
219
220 /* calculate the delta since the last update_wall_time: */
d28ede83 221 delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask);
2ba2a305 222
f2a5a085 223 /* convert delta to nanoseconds. */
3a978377 224 nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift);
f2a5a085 225
7b1f6207 226 /* If arch requires, add in get_arch_timeoffset() */
e06fde37 227 return nsec + arch_gettimeoffset();
2ba2a305
MS
228}
229
4396e058
TG
230/**
231 * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
232 * @tk: The timekeeper from which we take the update
233 * @tkf: The fast timekeeper to update
234 * @tbase: The time base for the fast timekeeper (mono/raw)
235 *
236 * We want to use this from any context including NMI and tracing /
237 * instrumenting the timekeeping code itself.
238 *
239 * So we handle this differently than the other timekeeping accessor
240 * functions which retry when the sequence count has changed. The
241 * update side does:
242 *
243 * smp_wmb(); <- Ensure that the last base[1] update is visible
244 * tkf->seq++;
245 * smp_wmb(); <- Ensure that the seqcount update is visible
246 * update(tkf->base[0], tk);
247 * smp_wmb(); <- Ensure that the base[0] update is visible
248 * tkf->seq++;
249 * smp_wmb(); <- Ensure that the seqcount update is visible
250 * update(tkf->base[1], tk);
251 *
252 * The reader side does:
253 *
254 * do {
255 * seq = tkf->seq;
256 * smp_rmb();
257 * idx = seq & 0x01;
258 * now = now(tkf->base[idx]);
259 * smp_rmb();
260 * } while (seq != tkf->seq)
261 *
262 * As long as we update base[0] readers are forced off to
263 * base[1]. Once base[0] is updated readers are redirected to base[0]
264 * and the base[1] update takes place.
265 *
266 * So if a NMI hits the update of base[0] then it will use base[1]
267 * which is still consistent. In the worst case this can result is a
268 * slightly wrong timestamp (a few nanoseconds). See
269 * @ktime_get_mono_fast_ns.
270 */
271static void update_fast_timekeeper(struct timekeeper *tk)
272{
273 struct tk_read_base *base = tk_fast_mono.base;
274
275 /* Force readers off to base[1] */
276 raw_write_seqcount_latch(&tk_fast_mono.seq);
277
278 /* Update base[0] */
279 memcpy(base, &tk->tkr, sizeof(*base));
280
281 /* Force readers back to base[0] */
282 raw_write_seqcount_latch(&tk_fast_mono.seq);
283
284 /* Update base[1] */
285 memcpy(base + 1, base, sizeof(*base));
286}
287
288/**
289 * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic
290 *
291 * This timestamp is not guaranteed to be monotonic across an update.
292 * The timestamp is calculated by:
293 *
294 * now = base_mono + clock_delta * slope
295 *
296 * So if the update lowers the slope, readers who are forced to the
297 * not yet updated second array are still using the old steeper slope.
298 *
299 * tmono
300 * ^
301 * | o n
302 * | o n
303 * | u
304 * | o
305 * |o
306 * |12345678---> reader order
307 *
308 * o = old slope
309 * u = update
310 * n = new slope
311 *
312 * So reader 6 will observe time going backwards versus reader 5.
313 *
314 * While other CPUs are likely to be able observe that, the only way
315 * for a CPU local observation is when an NMI hits in the middle of
316 * the update. Timestamps taken from that NMI context might be ahead
317 * of the following timestamps. Callers need to be aware of that and
318 * deal with it.
319 */
320u64 notrace ktime_get_mono_fast_ns(void)
321{
322 struct tk_read_base *tkr;
323 unsigned int seq;
324 u64 now;
325
326 do {
327 seq = raw_read_seqcount(&tk_fast_mono.seq);
328 tkr = tk_fast_mono.base + (seq & 0x01);
329 now = ktime_to_ns(tkr->base_mono) + timekeeping_get_ns(tkr);
330
331 } while (read_seqcount_retry(&tk_fast_mono.seq, seq));
332 return now;
333}
334EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
335
c905fae4
TG
336#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
337
338static inline void update_vsyscall(struct timekeeper *tk)
339{
340 struct timespec xt;
341
e2dff1ec 342 xt = timespec64_to_timespec(tk_xtime(tk));
d28ede83
TG
343 update_vsyscall_old(&xt, &tk->wall_to_monotonic, tk->tkr.clock, tk->tkr.mult,
344 tk->tkr.cycle_last);
c905fae4
TG
345}
346
347static inline void old_vsyscall_fixup(struct timekeeper *tk)
348{
349 s64 remainder;
350
351 /*
352 * Store only full nanoseconds into xtime_nsec after rounding
353 * it up and add the remainder to the error difference.
354 * XXX - This is necessary to avoid small 1ns inconsistnecies caused
355 * by truncating the remainder in vsyscalls. However, it causes
356 * additional work to be done in timekeeping_adjust(). Once
357 * the vsyscall implementations are converted to use xtime_nsec
358 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
359 * users are removed, this can be killed.
360 */
d28ede83
TG
361 remainder = tk->tkr.xtime_nsec & ((1ULL << tk->tkr.shift) - 1);
362 tk->tkr.xtime_nsec -= remainder;
363 tk->tkr.xtime_nsec += 1ULL << tk->tkr.shift;
c905fae4 364 tk->ntp_error += remainder << tk->ntp_error_shift;
d28ede83 365 tk->ntp_error -= (1ULL << tk->tkr.shift) << tk->ntp_error_shift;
c905fae4
TG
366}
367#else
368#define old_vsyscall_fixup(tk)
369#endif
370
e0b306fe
MT
371static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
372
780427f0 373static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
e0b306fe 374{
780427f0 375 raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
e0b306fe
MT
376}
377
378/**
379 * pvclock_gtod_register_notifier - register a pvclock timedata update listener
e0b306fe
MT
380 */
381int pvclock_gtod_register_notifier(struct notifier_block *nb)
382{
3fdb14fd 383 struct timekeeper *tk = &tk_core.timekeeper;
e0b306fe
MT
384 unsigned long flags;
385 int ret;
386
9a7a71b1 387 raw_spin_lock_irqsave(&timekeeper_lock, flags);
e0b306fe 388 ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
780427f0 389 update_pvclock_gtod(tk, true);
9a7a71b1 390 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
e0b306fe
MT
391
392 return ret;
393}
394EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
395
396/**
397 * pvclock_gtod_unregister_notifier - unregister a pvclock
398 * timedata update listener
e0b306fe
MT
399 */
400int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
401{
e0b306fe
MT
402 unsigned long flags;
403 int ret;
404
9a7a71b1 405 raw_spin_lock_irqsave(&timekeeper_lock, flags);
e0b306fe 406 ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
9a7a71b1 407 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
e0b306fe
MT
408
409 return ret;
410}
411EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
412
7c032df5
TG
413/*
414 * Update the ktime_t based scalar nsec members of the timekeeper
415 */
416static inline void tk_update_ktime_data(struct timekeeper *tk)
417{
418 s64 nsec;
419
420 /*
421 * The xtime based monotonic readout is:
422 * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();
423 * The ktime based monotonic readout is:
424 * nsec = base_mono + now();
425 * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
426 */
427 nsec = (s64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
428 nsec *= NSEC_PER_SEC;
429 nsec += tk->wall_to_monotonic.tv_nsec;
d28ede83 430 tk->tkr.base_mono = ns_to_ktime(nsec);
f519b1a2
TG
431
432 /* Update the monotonic raw base */
433 tk->base_raw = timespec64_to_ktime(tk->raw_time);
7c032df5
TG
434}
435
9a7a71b1 436/* must hold timekeeper_lock */
04397fe9 437static void timekeeping_update(struct timekeeper *tk, unsigned int action)
cc06268c 438{
04397fe9 439 if (action & TK_CLEAR_NTP) {
f726a697 440 tk->ntp_error = 0;
cc06268c
TG
441 ntp_clear();
442 }
576094b7 443 update_vsyscall(tk);
780427f0 444 update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
48cdc135 445
7c032df5
TG
446 tk_update_ktime_data(tk);
447
04397fe9 448 if (action & TK_MIRROR)
3fdb14fd
TG
449 memcpy(&shadow_timekeeper, &tk_core.timekeeper,
450 sizeof(tk_core.timekeeper));
4396e058
TG
451
452 update_fast_timekeeper(tk);
cc06268c
TG
453}
454
8524070b 455/**
155ec602 456 * timekeeping_forward_now - update clock to the current time
8524070b 457 *
9a055117
RZ
458 * Forward the current clock to update its state since the last call to
459 * update_wall_time(). This is useful before significant clock changes,
460 * as it avoids having to deal with this time offset explicitly.
8524070b 461 */
f726a697 462static void timekeeping_forward_now(struct timekeeper *tk)
8524070b 463{
d28ede83 464 struct clocksource *clock = tk->tkr.clock;
3a978377 465 cycle_t cycle_now, delta;
9a055117 466 s64 nsec;
8524070b 467
d28ede83
TG
468 cycle_now = tk->tkr.read(clock);
469 delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask);
470 tk->tkr.cycle_last = cycle_now;
8524070b 471
d28ede83 472 tk->tkr.xtime_nsec += delta * tk->tkr.mult;
7d27558c 473
7b1f6207 474 /* If arch requires, add in get_arch_timeoffset() */
d28ede83 475 tk->tkr.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr.shift;
7d27558c 476
f726a697 477 tk_normalize_xtime(tk);
2d42244a 478
3a978377 479 nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift);
7d489d15 480 timespec64_add_ns(&tk->raw_time, nsec);
8524070b
JS
481}
482
483/**
d6d29896 484 * __getnstimeofday64 - Returns the time of day in a timespec64.
8524070b
JS
485 * @ts: pointer to the timespec to be set
486 *
1e817fb6
KC
487 * Updates the time of day in the timespec.
488 * Returns 0 on success, or -ve when suspended (timespec will be undefined).
8524070b 489 */
d6d29896 490int __getnstimeofday64(struct timespec64 *ts)
8524070b 491{
3fdb14fd 492 struct timekeeper *tk = &tk_core.timekeeper;
8524070b 493 unsigned long seq;
1e75fa8b 494 s64 nsecs = 0;
8524070b
JS
495
496 do {
3fdb14fd 497 seq = read_seqcount_begin(&tk_core.seq);
8524070b 498
4e250fdd 499 ts->tv_sec = tk->xtime_sec;
0e5ac3a8 500 nsecs = timekeeping_get_ns(&tk->tkr);
8524070b 501
3fdb14fd 502 } while (read_seqcount_retry(&tk_core.seq, seq));
8524070b 503
ec145bab 504 ts->tv_nsec = 0;
d6d29896 505 timespec64_add_ns(ts, nsecs);
1e817fb6
KC
506
507 /*
508 * Do not bail out early, in case there were callers still using
509 * the value, even in the face of the WARN_ON.
510 */
511 if (unlikely(timekeeping_suspended))
512 return -EAGAIN;
513 return 0;
514}
d6d29896 515EXPORT_SYMBOL(__getnstimeofday64);
1e817fb6
KC
516
517/**
d6d29896 518 * getnstimeofday64 - Returns the time of day in a timespec64.
1e817fb6
KC
519 * @ts: pointer to the timespec to be set
520 *
521 * Returns the time of day in a timespec (WARN if suspended).
522 */
d6d29896 523void getnstimeofday64(struct timespec64 *ts)
1e817fb6 524{
d6d29896 525 WARN_ON(__getnstimeofday64(ts));
8524070b 526}
d6d29896 527EXPORT_SYMBOL(getnstimeofday64);
8524070b 528
951ed4d3
MS
529ktime_t ktime_get(void)
530{
3fdb14fd 531 struct timekeeper *tk = &tk_core.timekeeper;
951ed4d3 532 unsigned int seq;
a016a5bd
TG
533 ktime_t base;
534 s64 nsecs;
951ed4d3
MS
535
536 WARN_ON(timekeeping_suspended);
537
538 do {
3fdb14fd 539 seq = read_seqcount_begin(&tk_core.seq);
d28ede83 540 base = tk->tkr.base_mono;
0e5ac3a8 541 nsecs = timekeeping_get_ns(&tk->tkr);
951ed4d3 542
3fdb14fd 543 } while (read_seqcount_retry(&tk_core.seq, seq));
24e4a8c3 544
a016a5bd 545 return ktime_add_ns(base, nsecs);
951ed4d3
MS
546}
547EXPORT_SYMBOL_GPL(ktime_get);
548
0077dc60
TG
549static ktime_t *offsets[TK_OFFS_MAX] = {
550 [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real,
551 [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot,
552 [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai,
553};
554
555ktime_t ktime_get_with_offset(enum tk_offsets offs)
556{
557 struct timekeeper *tk = &tk_core.timekeeper;
558 unsigned int seq;
559 ktime_t base, *offset = offsets[offs];
560 s64 nsecs;
561
562 WARN_ON(timekeeping_suspended);
563
564 do {
565 seq = read_seqcount_begin(&tk_core.seq);
d28ede83 566 base = ktime_add(tk->tkr.base_mono, *offset);
0e5ac3a8 567 nsecs = timekeeping_get_ns(&tk->tkr);
0077dc60
TG
568
569 } while (read_seqcount_retry(&tk_core.seq, seq));
570
571 return ktime_add_ns(base, nsecs);
572
573}
574EXPORT_SYMBOL_GPL(ktime_get_with_offset);
575
9a6b5197
TG
576/**
577 * ktime_mono_to_any() - convert mononotic time to any other time
578 * @tmono: time to convert.
579 * @offs: which offset to use
580 */
581ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
582{
583 ktime_t *offset = offsets[offs];
584 unsigned long seq;
585 ktime_t tconv;
586
587 do {
588 seq = read_seqcount_begin(&tk_core.seq);
589 tconv = ktime_add(tmono, *offset);
590 } while (read_seqcount_retry(&tk_core.seq, seq));
591
592 return tconv;
593}
594EXPORT_SYMBOL_GPL(ktime_mono_to_any);
595
f519b1a2
TG
596/**
597 * ktime_get_raw - Returns the raw monotonic time in ktime_t format
598 */
599ktime_t ktime_get_raw(void)
600{
601 struct timekeeper *tk = &tk_core.timekeeper;
602 unsigned int seq;
603 ktime_t base;
604 s64 nsecs;
605
606 do {
607 seq = read_seqcount_begin(&tk_core.seq);
608 base = tk->base_raw;
609 nsecs = timekeeping_get_ns_raw(tk);
610
611 } while (read_seqcount_retry(&tk_core.seq, seq));
612
613 return ktime_add_ns(base, nsecs);
614}
615EXPORT_SYMBOL_GPL(ktime_get_raw);
616
951ed4d3 617/**
d6d29896 618 * ktime_get_ts64 - get the monotonic clock in timespec64 format
951ed4d3
MS
619 * @ts: pointer to timespec variable
620 *
621 * The function calculates the monotonic clock from the realtime
622 * clock and the wall_to_monotonic offset and stores the result
623 * in normalized timespec format in the variable pointed to by @ts.
624 */
d6d29896 625void ktime_get_ts64(struct timespec64 *ts)
951ed4d3 626{
3fdb14fd 627 struct timekeeper *tk = &tk_core.timekeeper;
d6d29896 628 struct timespec64 tomono;
ec145bab 629 s64 nsec;
951ed4d3 630 unsigned int seq;
951ed4d3
MS
631
632 WARN_ON(timekeeping_suspended);
633
634 do {
3fdb14fd 635 seq = read_seqcount_begin(&tk_core.seq);
d6d29896 636 ts->tv_sec = tk->xtime_sec;
0e5ac3a8 637 nsec = timekeeping_get_ns(&tk->tkr);
4e250fdd 638 tomono = tk->wall_to_monotonic;
951ed4d3 639
3fdb14fd 640 } while (read_seqcount_retry(&tk_core.seq, seq));
951ed4d3 641
d6d29896
TG
642 ts->tv_sec += tomono.tv_sec;
643 ts->tv_nsec = 0;
644 timespec64_add_ns(ts, nsec + tomono.tv_nsec);
951ed4d3 645}
d6d29896 646EXPORT_SYMBOL_GPL(ktime_get_ts64);
951ed4d3 647
e2c18e49
AG
648#ifdef CONFIG_NTP_PPS
649
650/**
651 * getnstime_raw_and_real - get day and raw monotonic time in timespec format
652 * @ts_raw: pointer to the timespec to be set to raw monotonic time
653 * @ts_real: pointer to the timespec to be set to the time of day
654 *
655 * This function reads both the time of day and raw monotonic time at the
656 * same time atomically and stores the resulting timestamps in timespec
657 * format.
658 */
659void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
660{
3fdb14fd 661 struct timekeeper *tk = &tk_core.timekeeper;
e2c18e49
AG
662 unsigned long seq;
663 s64 nsecs_raw, nsecs_real;
664
665 WARN_ON_ONCE(timekeeping_suspended);
666
667 do {
3fdb14fd 668 seq = read_seqcount_begin(&tk_core.seq);
e2c18e49 669
7d489d15 670 *ts_raw = timespec64_to_timespec(tk->raw_time);
4e250fdd 671 ts_real->tv_sec = tk->xtime_sec;
1e75fa8b 672 ts_real->tv_nsec = 0;
e2c18e49 673
4e250fdd 674 nsecs_raw = timekeeping_get_ns_raw(tk);
0e5ac3a8 675 nsecs_real = timekeeping_get_ns(&tk->tkr);
e2c18e49 676
3fdb14fd 677 } while (read_seqcount_retry(&tk_core.seq, seq));
e2c18e49
AG
678
679 timespec_add_ns(ts_raw, nsecs_raw);
680 timespec_add_ns(ts_real, nsecs_real);
681}
682EXPORT_SYMBOL(getnstime_raw_and_real);
683
684#endif /* CONFIG_NTP_PPS */
685
8524070b
JS
686/**
687 * do_gettimeofday - Returns the time of day in a timeval
688 * @tv: pointer to the timeval to be set
689 *
efd9ac86 690 * NOTE: Users should be converted to using getnstimeofday()
8524070b
JS
691 */
692void do_gettimeofday(struct timeval *tv)
693{
d6d29896 694 struct timespec64 now;
8524070b 695
d6d29896 696 getnstimeofday64(&now);
8524070b
JS
697 tv->tv_sec = now.tv_sec;
698 tv->tv_usec = now.tv_nsec/1000;
699}
8524070b 700EXPORT_SYMBOL(do_gettimeofday);
d239f49d 701
8524070b
JS
702/**
703 * do_settimeofday - Sets the time of day
704 * @tv: pointer to the timespec variable containing the new time
705 *
706 * Sets the time of day to the new time and update NTP and notify hrtimers
707 */
1e6d7679 708int do_settimeofday(const struct timespec *tv)
8524070b 709{
3fdb14fd 710 struct timekeeper *tk = &tk_core.timekeeper;
7d489d15 711 struct timespec64 ts_delta, xt, tmp;
92c1d3ed 712 unsigned long flags;
8524070b 713
cee58483 714 if (!timespec_valid_strict(tv))
8524070b
JS
715 return -EINVAL;
716
9a7a71b1 717 raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd 718 write_seqcount_begin(&tk_core.seq);
8524070b 719
4e250fdd 720 timekeeping_forward_now(tk);
9a055117 721
4e250fdd 722 xt = tk_xtime(tk);
1e75fa8b
JS
723 ts_delta.tv_sec = tv->tv_sec - xt.tv_sec;
724 ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec;
725
7d489d15 726 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
8524070b 727
7d489d15
JS
728 tmp = timespec_to_timespec64(*tv);
729 tk_set_xtime(tk, &tmp);
1e75fa8b 730
780427f0 731 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
8524070b 732
3fdb14fd 733 write_seqcount_end(&tk_core.seq);
9a7a71b1 734 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
8524070b
JS
735
736 /* signal hrtimers about time change */
737 clock_was_set();
738
739 return 0;
740}
8524070b
JS
741EXPORT_SYMBOL(do_settimeofday);
742
c528f7c6
JS
743/**
744 * timekeeping_inject_offset - Adds or subtracts from the current time.
745 * @tv: pointer to the timespec variable containing the offset
746 *
747 * Adds or subtracts an offset value from the current time.
748 */
749int timekeeping_inject_offset(struct timespec *ts)
750{
3fdb14fd 751 struct timekeeper *tk = &tk_core.timekeeper;
92c1d3ed 752 unsigned long flags;
7d489d15 753 struct timespec64 ts64, tmp;
4e8b1452 754 int ret = 0;
c528f7c6
JS
755
756 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
757 return -EINVAL;
758
7d489d15
JS
759 ts64 = timespec_to_timespec64(*ts);
760
9a7a71b1 761 raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd 762 write_seqcount_begin(&tk_core.seq);
c528f7c6 763
4e250fdd 764 timekeeping_forward_now(tk);
c528f7c6 765
4e8b1452 766 /* Make sure the proposed value is valid */
7d489d15
JS
767 tmp = timespec64_add(tk_xtime(tk), ts64);
768 if (!timespec64_valid_strict(&tmp)) {
4e8b1452
JS
769 ret = -EINVAL;
770 goto error;
771 }
1e75fa8b 772
7d489d15
JS
773 tk_xtime_add(tk, &ts64);
774 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts64));
c528f7c6 775
4e8b1452 776error: /* even if we error out, we forwarded the time, so call update */
780427f0 777 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
c528f7c6 778
3fdb14fd 779 write_seqcount_end(&tk_core.seq);
9a7a71b1 780 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
c528f7c6
JS
781
782 /* signal hrtimers about time change */
783 clock_was_set();
784
4e8b1452 785 return ret;
c528f7c6
JS
786}
787EXPORT_SYMBOL(timekeeping_inject_offset);
788
cc244dda
JS
789
790/**
791 * timekeeping_get_tai_offset - Returns current TAI offset from UTC
792 *
793 */
794s32 timekeeping_get_tai_offset(void)
795{
3fdb14fd 796 struct timekeeper *tk = &tk_core.timekeeper;
cc244dda
JS
797 unsigned int seq;
798 s32 ret;
799
800 do {
3fdb14fd 801 seq = read_seqcount_begin(&tk_core.seq);
cc244dda 802 ret = tk->tai_offset;
3fdb14fd 803 } while (read_seqcount_retry(&tk_core.seq, seq));
cc244dda
JS
804
805 return ret;
806}
807
808/**
809 * __timekeeping_set_tai_offset - Lock free worker function
810 *
811 */
dd5d70e8 812static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
cc244dda
JS
813{
814 tk->tai_offset = tai_offset;
04005f60 815 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
cc244dda
JS
816}
817
818/**
819 * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
820 *
821 */
822void timekeeping_set_tai_offset(s32 tai_offset)
823{
3fdb14fd 824 struct timekeeper *tk = &tk_core.timekeeper;
cc244dda
JS
825 unsigned long flags;
826
9a7a71b1 827 raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd 828 write_seqcount_begin(&tk_core.seq);
cc244dda 829 __timekeeping_set_tai_offset(tk, tai_offset);
f55c0760 830 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
3fdb14fd 831 write_seqcount_end(&tk_core.seq);
9a7a71b1 832 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
4e8f8b34 833 clock_was_set();
cc244dda
JS
834}
835
8524070b
JS
836/**
837 * change_clocksource - Swaps clocksources if a new one is available
838 *
839 * Accumulates current time interval and initializes new clocksource
840 */
75c5158f 841static int change_clocksource(void *data)
8524070b 842{
3fdb14fd 843 struct timekeeper *tk = &tk_core.timekeeper;
4614e6ad 844 struct clocksource *new, *old;
f695cf94 845 unsigned long flags;
8524070b 846
75c5158f 847 new = (struct clocksource *) data;
8524070b 848
9a7a71b1 849 raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd 850 write_seqcount_begin(&tk_core.seq);
f695cf94 851
4e250fdd 852 timekeeping_forward_now(tk);
09ac369c
TG
853 /*
854 * If the cs is in module, get a module reference. Succeeds
855 * for built-in code (owner == NULL) as well.
856 */
857 if (try_module_get(new->owner)) {
858 if (!new->enable || new->enable(new) == 0) {
d28ede83 859 old = tk->tkr.clock;
09ac369c
TG
860 tk_setup_internals(tk, new);
861 if (old->disable)
862 old->disable(old);
863 module_put(old->owner);
864 } else {
865 module_put(new->owner);
866 }
75c5158f 867 }
780427f0 868 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
f695cf94 869
3fdb14fd 870 write_seqcount_end(&tk_core.seq);
9a7a71b1 871 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
f695cf94 872
75c5158f
MS
873 return 0;
874}
8524070b 875
75c5158f
MS
876/**
877 * timekeeping_notify - Install a new clock source
878 * @clock: pointer to the clock source
879 *
880 * This function is called from clocksource.c after a new, better clock
881 * source has been registered. The caller holds the clocksource_mutex.
882 */
ba919d1c 883int timekeeping_notify(struct clocksource *clock)
75c5158f 884{
3fdb14fd 885 struct timekeeper *tk = &tk_core.timekeeper;
4e250fdd 886
d28ede83 887 if (tk->tkr.clock == clock)
ba919d1c 888 return 0;
75c5158f 889 stop_machine(change_clocksource, clock, NULL);
8524070b 890 tick_clock_notify();
d28ede83 891 return tk->tkr.clock == clock ? 0 : -1;
8524070b 892}
75c5158f 893
2d42244a
JS
894/**
895 * getrawmonotonic - Returns the raw monotonic time in a timespec
896 * @ts: pointer to the timespec to be set
897 *
898 * Returns the raw monotonic time (completely un-modified by ntp)
899 */
900void getrawmonotonic(struct timespec *ts)
901{
3fdb14fd 902 struct timekeeper *tk = &tk_core.timekeeper;
7d489d15 903 struct timespec64 ts64;
2d42244a
JS
904 unsigned long seq;
905 s64 nsecs;
2d42244a
JS
906
907 do {
3fdb14fd 908 seq = read_seqcount_begin(&tk_core.seq);
4e250fdd 909 nsecs = timekeeping_get_ns_raw(tk);
7d489d15 910 ts64 = tk->raw_time;
2d42244a 911
3fdb14fd 912 } while (read_seqcount_retry(&tk_core.seq, seq));
2d42244a 913
7d489d15
JS
914 timespec64_add_ns(&ts64, nsecs);
915 *ts = timespec64_to_timespec(ts64);
2d42244a
JS
916}
917EXPORT_SYMBOL(getrawmonotonic);
918
8524070b 919/**
cf4fc6cb 920 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
8524070b 921 */
cf4fc6cb 922int timekeeping_valid_for_hres(void)
8524070b 923{
3fdb14fd 924 struct timekeeper *tk = &tk_core.timekeeper;
8524070b
JS
925 unsigned long seq;
926 int ret;
927
928 do {
3fdb14fd 929 seq = read_seqcount_begin(&tk_core.seq);
8524070b 930
d28ede83 931 ret = tk->tkr.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
8524070b 932
3fdb14fd 933 } while (read_seqcount_retry(&tk_core.seq, seq));
8524070b
JS
934
935 return ret;
936}
937
98962465
JH
938/**
939 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
98962465
JH
940 */
941u64 timekeeping_max_deferment(void)
942{
3fdb14fd 943 struct timekeeper *tk = &tk_core.timekeeper;
70471f2f
JS
944 unsigned long seq;
945 u64 ret;
42e71e81 946
70471f2f 947 do {
3fdb14fd 948 seq = read_seqcount_begin(&tk_core.seq);
70471f2f 949
d28ede83 950 ret = tk->tkr.clock->max_idle_ns;
70471f2f 951
3fdb14fd 952 } while (read_seqcount_retry(&tk_core.seq, seq));
70471f2f
JS
953
954 return ret;
98962465
JH
955}
956
8524070b 957/**
d4f587c6 958 * read_persistent_clock - Return time from the persistent clock.
8524070b
JS
959 *
960 * Weak dummy function for arches that do not yet support it.
d4f587c6
MS
961 * Reads the time from the battery backed persistent clock.
962 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
8524070b
JS
963 *
964 * XXX - Do be sure to remove it once all arches implement it.
965 */
52f5684c 966void __weak read_persistent_clock(struct timespec *ts)
8524070b 967{
d4f587c6
MS
968 ts->tv_sec = 0;
969 ts->tv_nsec = 0;
8524070b
JS
970}
971
23970e38
MS
972/**
973 * read_boot_clock - Return time of the system start.
974 *
975 * Weak dummy function for arches that do not yet support it.
976 * Function to read the exact time the system has been started.
977 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
978 *
979 * XXX - Do be sure to remove it once all arches implement it.
980 */
52f5684c 981void __weak read_boot_clock(struct timespec *ts)
23970e38
MS
982{
983 ts->tv_sec = 0;
984 ts->tv_nsec = 0;
985}
986
8524070b
JS
987/*
988 * timekeeping_init - Initializes the clocksource and common timekeeping values
989 */
990void __init timekeeping_init(void)
991{
3fdb14fd 992 struct timekeeper *tk = &tk_core.timekeeper;
155ec602 993 struct clocksource *clock;
8524070b 994 unsigned long flags;
7d489d15
JS
995 struct timespec64 now, boot, tmp;
996 struct timespec ts;
31ade306 997
7d489d15
JS
998 read_persistent_clock(&ts);
999 now = timespec_to_timespec64(ts);
1000 if (!timespec64_valid_strict(&now)) {
4e8b1452
JS
1001 pr_warn("WARNING: Persistent clock returned invalid value!\n"
1002 " Check your CMOS/BIOS settings.\n");
1003 now.tv_sec = 0;
1004 now.tv_nsec = 0;
31ade306
FT
1005 } else if (now.tv_sec || now.tv_nsec)
1006 persistent_clock_exist = true;
4e8b1452 1007
7d489d15
JS
1008 read_boot_clock(&ts);
1009 boot = timespec_to_timespec64(ts);
1010 if (!timespec64_valid_strict(&boot)) {
4e8b1452
JS
1011 pr_warn("WARNING: Boot clock returned invalid value!\n"
1012 " Check your CMOS/BIOS settings.\n");
1013 boot.tv_sec = 0;
1014 boot.tv_nsec = 0;
1015 }
8524070b 1016
9a7a71b1 1017 raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd 1018 write_seqcount_begin(&tk_core.seq);
06c017fd
JS
1019 ntp_init();
1020
f1b82746 1021 clock = clocksource_default_clock();
a0f7d48b
MS
1022 if (clock->enable)
1023 clock->enable(clock);
4e250fdd 1024 tk_setup_internals(tk, clock);
8524070b 1025
4e250fdd
JS
1026 tk_set_xtime(tk, &now);
1027 tk->raw_time.tv_sec = 0;
1028 tk->raw_time.tv_nsec = 0;
f519b1a2 1029 tk->base_raw.tv64 = 0;
1e75fa8b 1030 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
4e250fdd 1031 boot = tk_xtime(tk);
1e75fa8b 1032
7d489d15 1033 set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec);
4e250fdd 1034 tk_set_wall_to_mono(tk, tmp);
6d0ef903 1035
f111adfd 1036 timekeeping_update(tk, TK_MIRROR);
48cdc135 1037
3fdb14fd 1038 write_seqcount_end(&tk_core.seq);
9a7a71b1 1039 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
8524070b
JS
1040}
1041
8524070b 1042/* time in seconds when suspend began */
7d489d15 1043static struct timespec64 timekeeping_suspend_time;
8524070b 1044
304529b1
JS
1045/**
1046 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
1047 * @delta: pointer to a timespec delta value
1048 *
1049 * Takes a timespec offset measuring a suspend interval and properly
1050 * adds the sleep offset to the timekeeping variables.
1051 */
f726a697 1052static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
7d489d15 1053 struct timespec64 *delta)
304529b1 1054{
7d489d15 1055 if (!timespec64_valid_strict(delta)) {
6d9bcb62
JS
1056 printk_deferred(KERN_WARNING
1057 "__timekeeping_inject_sleeptime: Invalid "
1058 "sleep delta value!\n");
cb5de2f8
JS
1059 return;
1060 }
f726a697 1061 tk_xtime_add(tk, delta);
7d489d15 1062 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
47da70d3 1063 tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
5c83545f 1064 tk_debug_account_sleep_time(delta);
304529b1
JS
1065}
1066
304529b1
JS
1067/**
1068 * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
1069 * @delta: pointer to a timespec delta value
1070 *
1071 * This hook is for architectures that cannot support read_persistent_clock
1072 * because their RTC/persistent clock is only accessible when irqs are enabled.
1073 *
1074 * This function should only be called by rtc_resume(), and allows
1075 * a suspend offset to be injected into the timekeeping values.
1076 */
1077void timekeeping_inject_sleeptime(struct timespec *delta)
1078{
3fdb14fd 1079 struct timekeeper *tk = &tk_core.timekeeper;
7d489d15 1080 struct timespec64 tmp;
92c1d3ed 1081 unsigned long flags;
304529b1 1082
31ade306
FT
1083 /*
1084 * Make sure we don't set the clock twice, as timekeeping_resume()
1085 * already did it
1086 */
1087 if (has_persistent_clock())
304529b1
JS
1088 return;
1089
9a7a71b1 1090 raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd 1091 write_seqcount_begin(&tk_core.seq);
70471f2f 1092
4e250fdd 1093 timekeeping_forward_now(tk);
304529b1 1094
7d489d15
JS
1095 tmp = timespec_to_timespec64(*delta);
1096 __timekeeping_inject_sleeptime(tk, &tmp);
304529b1 1097
780427f0 1098 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
304529b1 1099
3fdb14fd 1100 write_seqcount_end(&tk_core.seq);
9a7a71b1 1101 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
304529b1
JS
1102
1103 /* signal hrtimers about time change */
1104 clock_was_set();
1105}
1106
8524070b
JS
1107/**
1108 * timekeeping_resume - Resumes the generic timekeeping subsystem.
8524070b
JS
1109 *
1110 * This is for the generic clocksource timekeeping.
1111 * xtime/wall_to_monotonic/jiffies/etc are
1112 * still managed by arch specific suspend/resume code.
1113 */
e1a85b2c 1114static void timekeeping_resume(void)
8524070b 1115{
3fdb14fd 1116 struct timekeeper *tk = &tk_core.timekeeper;
d28ede83 1117 struct clocksource *clock = tk->tkr.clock;
92c1d3ed 1118 unsigned long flags;
7d489d15
JS
1119 struct timespec64 ts_new, ts_delta;
1120 struct timespec tmp;
e445cf1c
FT
1121 cycle_t cycle_now, cycle_delta;
1122 bool suspendtime_found = false;
d4f587c6 1123
7d489d15
JS
1124 read_persistent_clock(&tmp);
1125 ts_new = timespec_to_timespec64(tmp);
8524070b 1126
adc78e6b 1127 clockevents_resume();
d10ff3fb
TG
1128 clocksource_resume();
1129
9a7a71b1 1130 raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd 1131 write_seqcount_begin(&tk_core.seq);
8524070b 1132
e445cf1c
FT
1133 /*
1134 * After system resumes, we need to calculate the suspended time and
1135 * compensate it for the OS time. There are 3 sources that could be
1136 * used: Nonstop clocksource during suspend, persistent clock and rtc
1137 * device.
1138 *
1139 * One specific platform may have 1 or 2 or all of them, and the
1140 * preference will be:
1141 * suspend-nonstop clocksource -> persistent clock -> rtc
1142 * The less preferred source will only be tried if there is no better
1143 * usable source. The rtc part is handled separately in rtc core code.
1144 */
d28ede83 1145 cycle_now = tk->tkr.read(clock);
e445cf1c 1146 if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
d28ede83 1147 cycle_now > tk->tkr.cycle_last) {
e445cf1c
FT
1148 u64 num, max = ULLONG_MAX;
1149 u32 mult = clock->mult;
1150 u32 shift = clock->shift;
1151 s64 nsec = 0;
1152
d28ede83
TG
1153 cycle_delta = clocksource_delta(cycle_now, tk->tkr.cycle_last,
1154 tk->tkr.mask);
e445cf1c
FT
1155
1156 /*
1157 * "cycle_delta * mutl" may cause 64 bits overflow, if the
1158 * suspended time is too long. In that case we need do the
1159 * 64 bits math carefully
1160 */
1161 do_div(max, mult);
1162 if (cycle_delta > max) {
1163 num = div64_u64(cycle_delta, max);
1164 nsec = (((u64) max * mult) >> shift) * num;
1165 cycle_delta -= num * max;
1166 }
1167 nsec += ((u64) cycle_delta * mult) >> shift;
1168
7d489d15 1169 ts_delta = ns_to_timespec64(nsec);
e445cf1c 1170 suspendtime_found = true;
7d489d15
JS
1171 } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
1172 ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
e445cf1c 1173 suspendtime_found = true;
8524070b 1174 }
e445cf1c
FT
1175
1176 if (suspendtime_found)
1177 __timekeeping_inject_sleeptime(tk, &ts_delta);
1178
1179 /* Re-base the last cycle value */
d28ede83 1180 tk->tkr.cycle_last = cycle_now;
4e250fdd 1181 tk->ntp_error = 0;
8524070b 1182 timekeeping_suspended = 0;
780427f0 1183 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
3fdb14fd 1184 write_seqcount_end(&tk_core.seq);
9a7a71b1 1185 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
8524070b
JS
1186
1187 touch_softlockup_watchdog();
1188
1189 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
1190
1191 /* Resume hrtimers */
b12a03ce 1192 hrtimers_resume();
8524070b
JS
1193}
1194
e1a85b2c 1195static int timekeeping_suspend(void)
8524070b 1196{
3fdb14fd 1197 struct timekeeper *tk = &tk_core.timekeeper;
92c1d3ed 1198 unsigned long flags;
7d489d15
JS
1199 struct timespec64 delta, delta_delta;
1200 static struct timespec64 old_delta;
1201 struct timespec tmp;
8524070b 1202
7d489d15
JS
1203 read_persistent_clock(&tmp);
1204 timekeeping_suspend_time = timespec_to_timespec64(tmp);
3be90950 1205
0d6bd995
ZM
1206 /*
1207 * On some systems the persistent_clock can not be detected at
1208 * timekeeping_init by its return value, so if we see a valid
1209 * value returned, update the persistent_clock_exists flag.
1210 */
1211 if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
1212 persistent_clock_exist = true;
1213
9a7a71b1 1214 raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd 1215 write_seqcount_begin(&tk_core.seq);
4e250fdd 1216 timekeeping_forward_now(tk);
8524070b 1217 timekeeping_suspended = 1;
cb33217b
JS
1218
1219 /*
1220 * To avoid drift caused by repeated suspend/resumes,
1221 * which each can add ~1 second drift error,
1222 * try to compensate so the difference in system time
1223 * and persistent_clock time stays close to constant.
1224 */
7d489d15
JS
1225 delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time);
1226 delta_delta = timespec64_sub(delta, old_delta);
cb33217b
JS
1227 if (abs(delta_delta.tv_sec) >= 2) {
1228 /*
1229 * if delta_delta is too large, assume time correction
1230 * has occured and set old_delta to the current delta.
1231 */
1232 old_delta = delta;
1233 } else {
1234 /* Otherwise try to adjust old_system to compensate */
1235 timekeeping_suspend_time =
7d489d15 1236 timespec64_add(timekeeping_suspend_time, delta_delta);
cb33217b 1237 }
330a1617
JS
1238
1239 timekeeping_update(tk, TK_MIRROR);
3fdb14fd 1240 write_seqcount_end(&tk_core.seq);
9a7a71b1 1241 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
8524070b
JS
1242
1243 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
c54a42b1 1244 clocksource_suspend();
adc78e6b 1245 clockevents_suspend();
8524070b
JS
1246
1247 return 0;
1248}
1249
1250/* sysfs resume/suspend bits for timekeeping */
e1a85b2c 1251static struct syscore_ops timekeeping_syscore_ops = {
8524070b
JS
1252 .resume = timekeeping_resume,
1253 .suspend = timekeeping_suspend,
8524070b
JS
1254};
1255
e1a85b2c 1256static int __init timekeeping_init_ops(void)
8524070b 1257{
e1a85b2c
RW
1258 register_syscore_ops(&timekeeping_syscore_ops);
1259 return 0;
8524070b 1260}
e1a85b2c 1261device_initcall(timekeeping_init_ops);
8524070b
JS
1262
1263/*
dc491596 1264 * Apply a multiplier adjustment to the timekeeper
8524070b 1265 */
dc491596
JS
1266static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
1267 s64 offset,
1268 bool negative,
1269 int adj_scale)
8524070b 1270{
dc491596
JS
1271 s64 interval = tk->cycle_interval;
1272 s32 mult_adj = 1;
8524070b 1273
dc491596
JS
1274 if (negative) {
1275 mult_adj = -mult_adj;
1276 interval = -interval;
1277 offset = -offset;
1d17d174 1278 }
dc491596
JS
1279 mult_adj <<= adj_scale;
1280 interval <<= adj_scale;
1281 offset <<= adj_scale;
8524070b 1282
c2bc1111
JS
1283 /*
1284 * So the following can be confusing.
1285 *
dc491596 1286 * To keep things simple, lets assume mult_adj == 1 for now.
c2bc1111 1287 *
dc491596 1288 * When mult_adj != 1, remember that the interval and offset values
c2bc1111
JS
1289 * have been appropriately scaled so the math is the same.
1290 *
1291 * The basic idea here is that we're increasing the multiplier
1292 * by one, this causes the xtime_interval to be incremented by
1293 * one cycle_interval. This is because:
1294 * xtime_interval = cycle_interval * mult
1295 * So if mult is being incremented by one:
1296 * xtime_interval = cycle_interval * (mult + 1)
1297 * Its the same as:
1298 * xtime_interval = (cycle_interval * mult) + cycle_interval
1299 * Which can be shortened to:
1300 * xtime_interval += cycle_interval
1301 *
1302 * So offset stores the non-accumulated cycles. Thus the current
1303 * time (in shifted nanoseconds) is:
1304 * now = (offset * adj) + xtime_nsec
1305 * Now, even though we're adjusting the clock frequency, we have
1306 * to keep time consistent. In other words, we can't jump back
1307 * in time, and we also want to avoid jumping forward in time.
1308 *
1309 * So given the same offset value, we need the time to be the same
1310 * both before and after the freq adjustment.
1311 * now = (offset * adj_1) + xtime_nsec_1
1312 * now = (offset * adj_2) + xtime_nsec_2
1313 * So:
1314 * (offset * adj_1) + xtime_nsec_1 =
1315 * (offset * adj_2) + xtime_nsec_2
1316 * And we know:
1317 * adj_2 = adj_1 + 1
1318 * So:
1319 * (offset * adj_1) + xtime_nsec_1 =
1320 * (offset * (adj_1+1)) + xtime_nsec_2
1321 * (offset * adj_1) + xtime_nsec_1 =
1322 * (offset * adj_1) + offset + xtime_nsec_2
1323 * Canceling the sides:
1324 * xtime_nsec_1 = offset + xtime_nsec_2
1325 * Which gives us:
1326 * xtime_nsec_2 = xtime_nsec_1 - offset
1327 * Which simplfies to:
1328 * xtime_nsec -= offset
1329 *
1330 * XXX - TODO: Doc ntp_error calculation.
1331 */
dc491596 1332 tk->tkr.mult += mult_adj;
f726a697 1333 tk->xtime_interval += interval;
d28ede83 1334 tk->tkr.xtime_nsec -= offset;
f726a697 1335 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
dc491596
JS
1336}
1337
1338/*
1339 * Calculate the multiplier adjustment needed to match the frequency
1340 * specified by NTP
1341 */
1342static __always_inline void timekeeping_freqadjust(struct timekeeper *tk,
1343 s64 offset)
1344{
1345 s64 interval = tk->cycle_interval;
1346 s64 xinterval = tk->xtime_interval;
1347 s64 tick_error;
1348 bool negative;
1349 u32 adj;
1350
1351 /* Remove any current error adj from freq calculation */
1352 if (tk->ntp_err_mult)
1353 xinterval -= tk->cycle_interval;
1354
1355 /* Calculate current error per tick */
1356 tick_error = ntp_tick_length() >> tk->ntp_error_shift;
1357 tick_error -= (xinterval + tk->xtime_remainder);
1358
1359 /* Don't worry about correcting it if its small */
1360 if (likely((tick_error >= 0) && (tick_error <= interval)))
1361 return;
1362
1363 /* preserve the direction of correction */
1364 negative = (tick_error < 0);
1365
1366 /* Sort out the magnitude of the correction */
1367 tick_error = abs(tick_error);
1368 for (adj = 0; tick_error > interval; adj++)
1369 tick_error >>= 1;
1370
1371 /* scale the corrections */
1372 timekeeping_apply_adjustment(tk, offset, negative, adj);
1373}
1374
1375/*
1376 * Adjust the timekeeper's multiplier to the correct frequency
1377 * and also to reduce the accumulated error value.
1378 */
1379static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
1380{
1381 /* Correct for the current frequency error */
1382 timekeeping_freqadjust(tk, offset);
1383
1384 /* Next make a small adjustment to fix any cumulative error */
1385 if (!tk->ntp_err_mult && (tk->ntp_error > 0)) {
1386 tk->ntp_err_mult = 1;
1387 timekeeping_apply_adjustment(tk, offset, 0, 0);
1388 } else if (tk->ntp_err_mult && (tk->ntp_error <= 0)) {
1389 /* Undo any existing error adjustment */
1390 timekeeping_apply_adjustment(tk, offset, 1, 0);
1391 tk->ntp_err_mult = 0;
1392 }
1393
1394 if (unlikely(tk->tkr.clock->maxadj &&
1395 (tk->tkr.mult > tk->tkr.clock->mult + tk->tkr.clock->maxadj))) {
1396 printk_once(KERN_WARNING
1397 "Adjusting %s more than 11%% (%ld vs %ld)\n",
1398 tk->tkr.clock->name, (long)tk->tkr.mult,
1399 (long)tk->tkr.clock->mult + tk->tkr.clock->maxadj);
1400 }
2a8c0883
JS
1401
1402 /*
1403 * It may be possible that when we entered this function, xtime_nsec
1404 * was very small. Further, if we're slightly speeding the clocksource
1405 * in the code above, its possible the required corrective factor to
1406 * xtime_nsec could cause it to underflow.
1407 *
1408 * Now, since we already accumulated the second, cannot simply roll
1409 * the accumulated second back, since the NTP subsystem has been
1410 * notified via second_overflow. So instead we push xtime_nsec forward
1411 * by the amount we underflowed, and add that amount into the error.
1412 *
1413 * We'll correct this error next time through this function, when
1414 * xtime_nsec is not as small.
1415 */
d28ede83
TG
1416 if (unlikely((s64)tk->tkr.xtime_nsec < 0)) {
1417 s64 neg = -(s64)tk->tkr.xtime_nsec;
1418 tk->tkr.xtime_nsec = 0;
f726a697 1419 tk->ntp_error += neg << tk->ntp_error_shift;
2a8c0883 1420 }
8524070b
JS
1421}
1422
1f4f9487
JS
1423/**
1424 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
1425 *
1426 * Helper function that accumulates a the nsecs greater then a second
1427 * from the xtime_nsec field to the xtime_secs field.
1428 * It also calls into the NTP code to handle leapsecond processing.
1429 *
1430 */
780427f0 1431static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
1f4f9487 1432{
d28ede83 1433 u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr.shift;
5258d3f2 1434 unsigned int clock_set = 0;
1f4f9487 1435
d28ede83 1436 while (tk->tkr.xtime_nsec >= nsecps) {
1f4f9487
JS
1437 int leap;
1438
d28ede83 1439 tk->tkr.xtime_nsec -= nsecps;
1f4f9487
JS
1440 tk->xtime_sec++;
1441
1442 /* Figure out if its a leap sec and apply if needed */
1443 leap = second_overflow(tk->xtime_sec);
6d0ef903 1444 if (unlikely(leap)) {
7d489d15 1445 struct timespec64 ts;
6d0ef903
JS
1446
1447 tk->xtime_sec += leap;
1f4f9487 1448
6d0ef903
JS
1449 ts.tv_sec = leap;
1450 ts.tv_nsec = 0;
1451 tk_set_wall_to_mono(tk,
7d489d15 1452 timespec64_sub(tk->wall_to_monotonic, ts));
6d0ef903 1453
cc244dda
JS
1454 __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
1455
5258d3f2 1456 clock_set = TK_CLOCK_WAS_SET;
6d0ef903 1457 }
1f4f9487 1458 }
5258d3f2 1459 return clock_set;
1f4f9487
JS
1460}
1461
a092ff0f
JS
1462/**
1463 * logarithmic_accumulation - shifted accumulation of cycles
1464 *
1465 * This functions accumulates a shifted interval of cycles into
1466 * into a shifted interval nanoseconds. Allows for O(log) accumulation
1467 * loop.
1468 *
1469 * Returns the unconsumed cycles.
1470 */
f726a697 1471static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
5258d3f2
JS
1472 u32 shift,
1473 unsigned int *clock_set)
a092ff0f 1474{
23a9537a 1475 cycle_t interval = tk->cycle_interval << shift;
deda2e81 1476 u64 raw_nsecs;
a092ff0f 1477
f726a697 1478 /* If the offset is smaller then a shifted interval, do nothing */
23a9537a 1479 if (offset < interval)
a092ff0f
JS
1480 return offset;
1481
1482 /* Accumulate one shifted interval */
23a9537a 1483 offset -= interval;
d28ede83 1484 tk->tkr.cycle_last += interval;
a092ff0f 1485
d28ede83 1486 tk->tkr.xtime_nsec += tk->xtime_interval << shift;
5258d3f2 1487 *clock_set |= accumulate_nsecs_to_secs(tk);
a092ff0f 1488
deda2e81 1489 /* Accumulate raw time */
5b3900cd 1490 raw_nsecs = (u64)tk->raw_interval << shift;
f726a697 1491 raw_nsecs += tk->raw_time.tv_nsec;
c7dcf87a
JS
1492 if (raw_nsecs >= NSEC_PER_SEC) {
1493 u64 raw_secs = raw_nsecs;
1494 raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
f726a697 1495 tk->raw_time.tv_sec += raw_secs;
a092ff0f 1496 }
f726a697 1497 tk->raw_time.tv_nsec = raw_nsecs;
a092ff0f
JS
1498
1499 /* Accumulate error between NTP and clock interval */
f726a697
JS
1500 tk->ntp_error += ntp_tick_length() << shift;
1501 tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
1502 (tk->ntp_error_shift + shift);
a092ff0f
JS
1503
1504 return offset;
1505}
1506
8524070b
JS
1507/**
1508 * update_wall_time - Uses the current clocksource to increment the wall time
1509 *
8524070b 1510 */
47a1b796 1511void update_wall_time(void)
8524070b 1512{
3fdb14fd 1513 struct timekeeper *real_tk = &tk_core.timekeeper;
48cdc135 1514 struct timekeeper *tk = &shadow_timekeeper;
8524070b 1515 cycle_t offset;
a092ff0f 1516 int shift = 0, maxshift;
5258d3f2 1517 unsigned int clock_set = 0;
70471f2f
JS
1518 unsigned long flags;
1519
9a7a71b1 1520 raw_spin_lock_irqsave(&timekeeper_lock, flags);
8524070b
JS
1521
1522 /* Make sure we're fully resumed: */
1523 if (unlikely(timekeeping_suspended))
70471f2f 1524 goto out;
8524070b 1525
592913ec 1526#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
48cdc135 1527 offset = real_tk->cycle_interval;
592913ec 1528#else
d28ede83
TG
1529 offset = clocksource_delta(tk->tkr.read(tk->tkr.clock),
1530 tk->tkr.cycle_last, tk->tkr.mask);
8524070b 1531#endif
8524070b 1532
bf2ac312 1533 /* Check if there's really nothing to do */
48cdc135 1534 if (offset < real_tk->cycle_interval)
bf2ac312
JS
1535 goto out;
1536
a092ff0f
JS
1537 /*
1538 * With NO_HZ we may have to accumulate many cycle_intervals
1539 * (think "ticks") worth of time at once. To do this efficiently,
1540 * we calculate the largest doubling multiple of cycle_intervals
88b28adf 1541 * that is smaller than the offset. We then accumulate that
a092ff0f
JS
1542 * chunk in one go, and then try to consume the next smaller
1543 * doubled multiple.
8524070b 1544 */
4e250fdd 1545 shift = ilog2(offset) - ilog2(tk->cycle_interval);
a092ff0f 1546 shift = max(0, shift);
88b28adf 1547 /* Bound shift to one less than what overflows tick_length */
ea7cf49a 1548 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
a092ff0f 1549 shift = min(shift, maxshift);
4e250fdd 1550 while (offset >= tk->cycle_interval) {
5258d3f2
JS
1551 offset = logarithmic_accumulation(tk, offset, shift,
1552 &clock_set);
4e250fdd 1553 if (offset < tk->cycle_interval<<shift)
830ec045 1554 shift--;
8524070b
JS
1555 }
1556
1557 /* correct the clock when NTP error is too big */
4e250fdd 1558 timekeeping_adjust(tk, offset);
8524070b 1559
6a867a39 1560 /*
92bb1fcf
JS
1561 * XXX This can be killed once everyone converts
1562 * to the new update_vsyscall.
1563 */
1564 old_vsyscall_fixup(tk);
8524070b 1565
6a867a39
JS
1566 /*
1567 * Finally, make sure that after the rounding
1e75fa8b 1568 * xtime_nsec isn't larger than NSEC_PER_SEC
6a867a39 1569 */
5258d3f2 1570 clock_set |= accumulate_nsecs_to_secs(tk);
83f57a11 1571
3fdb14fd 1572 write_seqcount_begin(&tk_core.seq);
48cdc135
TG
1573 /*
1574 * Update the real timekeeper.
1575 *
1576 * We could avoid this memcpy by switching pointers, but that
1577 * requires changes to all other timekeeper usage sites as
1578 * well, i.e. move the timekeeper pointer getter into the
1579 * spinlocked/seqcount protected sections. And we trade this
3fdb14fd 1580 * memcpy under the tk_core.seq against one before we start
48cdc135
TG
1581 * updating.
1582 */
1583 memcpy(real_tk, tk, sizeof(*tk));
5258d3f2 1584 timekeeping_update(real_tk, clock_set);
3fdb14fd 1585 write_seqcount_end(&tk_core.seq);
ca4523cd 1586out:
9a7a71b1 1587 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
47a1b796 1588 if (clock_set)
cab5e127
JS
1589 /* Have to call _delayed version, since in irq context*/
1590 clock_was_set_delayed();
8524070b 1591}
7c3f1a57
TJ
1592
1593/**
1594 * getboottime - Return the real time of system boot.
1595 * @ts: pointer to the timespec to be set
1596 *
abb3a4ea 1597 * Returns the wall-time of boot in a timespec.
7c3f1a57
TJ
1598 *
1599 * This is based on the wall_to_monotonic offset and the total suspend
1600 * time. Calls to settimeofday will affect the value returned (which
1601 * basically means that however wrong your real time clock is at boot time,
1602 * you get the right time here).
1603 */
1604void getboottime(struct timespec *ts)
1605{
3fdb14fd 1606 struct timekeeper *tk = &tk_core.timekeeper;
02cba159
TG
1607 ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot);
1608
1609 *ts = ktime_to_timespec(t);
7c3f1a57 1610}
c93d89f3 1611EXPORT_SYMBOL_GPL(getboottime);
7c3f1a57 1612
17c38b74
JS
1613unsigned long get_seconds(void)
1614{
3fdb14fd 1615 struct timekeeper *tk = &tk_core.timekeeper;
4e250fdd
JS
1616
1617 return tk->xtime_sec;
17c38b74
JS
1618}
1619EXPORT_SYMBOL(get_seconds);
1620
da15cfda
JS
1621struct timespec __current_kernel_time(void)
1622{
3fdb14fd 1623 struct timekeeper *tk = &tk_core.timekeeper;
4e250fdd 1624
7d489d15 1625 return timespec64_to_timespec(tk_xtime(tk));
da15cfda 1626}
17c38b74 1627
2c6b47de
JS
1628struct timespec current_kernel_time(void)
1629{
3fdb14fd 1630 struct timekeeper *tk = &tk_core.timekeeper;
7d489d15 1631 struct timespec64 now;
2c6b47de
JS
1632 unsigned long seq;
1633
1634 do {
3fdb14fd 1635 seq = read_seqcount_begin(&tk_core.seq);
83f57a11 1636
4e250fdd 1637 now = tk_xtime(tk);
3fdb14fd 1638 } while (read_seqcount_retry(&tk_core.seq, seq));
2c6b47de 1639
7d489d15 1640 return timespec64_to_timespec(now);
2c6b47de 1641}
2c6b47de 1642EXPORT_SYMBOL(current_kernel_time);
da15cfda
JS
1643
1644struct timespec get_monotonic_coarse(void)
1645{
3fdb14fd 1646 struct timekeeper *tk = &tk_core.timekeeper;
7d489d15 1647 struct timespec64 now, mono;
da15cfda
JS
1648 unsigned long seq;
1649
1650 do {
3fdb14fd 1651 seq = read_seqcount_begin(&tk_core.seq);
83f57a11 1652
4e250fdd
JS
1653 now = tk_xtime(tk);
1654 mono = tk->wall_to_monotonic;
3fdb14fd 1655 } while (read_seqcount_retry(&tk_core.seq, seq));
da15cfda 1656
7d489d15 1657 set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec,
da15cfda 1658 now.tv_nsec + mono.tv_nsec);
7d489d15
JS
1659
1660 return timespec64_to_timespec(now);
da15cfda 1661}
871cf1e5
TH
1662
1663/*
d6ad4187 1664 * Must hold jiffies_lock
871cf1e5
TH
1665 */
1666void do_timer(unsigned long ticks)
1667{
1668 jiffies_64 += ticks;
871cf1e5
TH
1669 calc_global_load(ticks);
1670}
48cf76f7
TH
1671
1672/**
76f41088
JS
1673 * ktime_get_update_offsets_tick - hrtimer helper
1674 * @offs_real: pointer to storage for monotonic -> realtime offset
1675 * @offs_boot: pointer to storage for monotonic -> boottime offset
1676 * @offs_tai: pointer to storage for monotonic -> clock tai offset
1677 *
1678 * Returns monotonic time at last tick and various offsets
48cf76f7 1679 */
76f41088
JS
1680ktime_t ktime_get_update_offsets_tick(ktime_t *offs_real, ktime_t *offs_boot,
1681 ktime_t *offs_tai)
48cf76f7 1682{
3fdb14fd 1683 struct timekeeper *tk = &tk_core.timekeeper;
76f41088 1684 unsigned int seq;
48064f5f
TG
1685 ktime_t base;
1686 u64 nsecs;
48cf76f7
TH
1687
1688 do {
3fdb14fd 1689 seq = read_seqcount_begin(&tk_core.seq);
76f41088 1690
d28ede83
TG
1691 base = tk->tkr.base_mono;
1692 nsecs = tk->tkr.xtime_nsec >> tk->tkr.shift;
48064f5f 1693
76f41088
JS
1694 *offs_real = tk->offs_real;
1695 *offs_boot = tk->offs_boot;
1696 *offs_tai = tk->offs_tai;
3fdb14fd 1697 } while (read_seqcount_retry(&tk_core.seq, seq));
76f41088 1698
48064f5f 1699 return ktime_add_ns(base, nsecs);
48cf76f7 1700}
f0af911a 1701
f6c06abf
TG
1702#ifdef CONFIG_HIGH_RES_TIMERS
1703/**
76f41088 1704 * ktime_get_update_offsets_now - hrtimer helper
f6c06abf
TG
1705 * @offs_real: pointer to storage for monotonic -> realtime offset
1706 * @offs_boot: pointer to storage for monotonic -> boottime offset
b7bc50e4 1707 * @offs_tai: pointer to storage for monotonic -> clock tai offset
f6c06abf
TG
1708 *
1709 * Returns current monotonic time and updates the offsets
b7bc50e4 1710 * Called from hrtimer_interrupt() or retrigger_next_event()
f6c06abf 1711 */
76f41088 1712ktime_t ktime_get_update_offsets_now(ktime_t *offs_real, ktime_t *offs_boot,
90adda98 1713 ktime_t *offs_tai)
f6c06abf 1714{
3fdb14fd 1715 struct timekeeper *tk = &tk_core.timekeeper;
f6c06abf 1716 unsigned int seq;
a37c0aad
TG
1717 ktime_t base;
1718 u64 nsecs;
f6c06abf
TG
1719
1720 do {
3fdb14fd 1721 seq = read_seqcount_begin(&tk_core.seq);
f6c06abf 1722
d28ede83 1723 base = tk->tkr.base_mono;
0e5ac3a8 1724 nsecs = timekeeping_get_ns(&tk->tkr);
f6c06abf 1725
4e250fdd
JS
1726 *offs_real = tk->offs_real;
1727 *offs_boot = tk->offs_boot;
90adda98 1728 *offs_tai = tk->offs_tai;
3fdb14fd 1729 } while (read_seqcount_retry(&tk_core.seq, seq));
f6c06abf 1730
a37c0aad 1731 return ktime_add_ns(base, nsecs);
f6c06abf
TG
1732}
1733#endif
1734
aa6f9c59
JS
1735/**
1736 * do_adjtimex() - Accessor function to NTP __do_adjtimex function
1737 */
1738int do_adjtimex(struct timex *txc)
1739{
3fdb14fd 1740 struct timekeeper *tk = &tk_core.timekeeper;
06c017fd 1741 unsigned long flags;
7d489d15 1742 struct timespec64 ts;
4e8f8b34 1743 s32 orig_tai, tai;
e4085693
JS
1744 int ret;
1745
1746 /* Validate the data before disabling interrupts */
1747 ret = ntp_validate_timex(txc);
1748 if (ret)
1749 return ret;
1750
cef90377
JS
1751 if (txc->modes & ADJ_SETOFFSET) {
1752 struct timespec delta;
1753 delta.tv_sec = txc->time.tv_sec;
1754 delta.tv_nsec = txc->time.tv_usec;
1755 if (!(txc->modes & ADJ_NANO))
1756 delta.tv_nsec *= 1000;
1757 ret = timekeeping_inject_offset(&delta);
1758 if (ret)
1759 return ret;
1760 }
1761
d6d29896 1762 getnstimeofday64(&ts);
87ace39b 1763
06c017fd 1764 raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd 1765 write_seqcount_begin(&tk_core.seq);
06c017fd 1766
4e8f8b34 1767 orig_tai = tai = tk->tai_offset;
87ace39b 1768 ret = __do_adjtimex(txc, &ts, &tai);
aa6f9c59 1769
4e8f8b34
JS
1770 if (tai != orig_tai) {
1771 __timekeeping_set_tai_offset(tk, tai);
f55c0760 1772 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
4e8f8b34 1773 }
3fdb14fd 1774 write_seqcount_end(&tk_core.seq);
06c017fd
JS
1775 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1776
6fdda9a9
JS
1777 if (tai != orig_tai)
1778 clock_was_set();
1779
7bd36014
JS
1780 ntp_notify_cmos_timer();
1781
87ace39b
JS
1782 return ret;
1783}
aa6f9c59
JS
1784
1785#ifdef CONFIG_NTP_PPS
1786/**
1787 * hardpps() - Accessor function to NTP __hardpps function
1788 */
1789void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
1790{
06c017fd
JS
1791 unsigned long flags;
1792
1793 raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd 1794 write_seqcount_begin(&tk_core.seq);
06c017fd 1795
aa6f9c59 1796 __hardpps(phase_ts, raw_ts);
06c017fd 1797
3fdb14fd 1798 write_seqcount_end(&tk_core.seq);
06c017fd 1799 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
aa6f9c59
JS
1800}
1801EXPORT_SYMBOL(hardpps);
1802#endif
1803
f0af911a
TH
1804/**
1805 * xtime_update() - advances the timekeeping infrastructure
1806 * @ticks: number of ticks, that have elapsed since the last call.
1807 *
1808 * Must be called with interrupts disabled.
1809 */
1810void xtime_update(unsigned long ticks)
1811{
d6ad4187 1812 write_seqlock(&jiffies_lock);
f0af911a 1813 do_timer(ticks);
d6ad4187 1814 write_sequnlock(&jiffies_lock);
47a1b796 1815 update_wall_time();
f0af911a 1816}