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