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