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