]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - kernel/time/timekeeping.c
timekeeping: Add xtime_shift and ntp_error_shift to struct timekeeper
[mirror_ubuntu-zesty-kernel.git] / kernel / time / timekeeping.c
CommitLineData
8524070b
JS
1/*
2 * linux/kernel/time/timekeeping.c
3 *
4 * Kernel timekeeping code and accessor functions
5 *
6 * This code was moved from linux/kernel/timer.c.
7 * Please see that file for copyright and history logs.
8 *
9 */
10
11#include <linux/module.h>
12#include <linux/interrupt.h>
13#include <linux/percpu.h>
14#include <linux/init.h>
15#include <linux/mm.h>
16#include <linux/sysdev.h>
17#include <linux/clocksource.h>
18#include <linux/jiffies.h>
19#include <linux/time.h>
20#include <linux/tick.h>
21
155ec602
MS
22/* Structure holding internal timekeeping values. */
23struct timekeeper {
24 /* Current clocksource used for timekeeping. */
25 struct clocksource *clock;
23ce7211
MS
26 /* The shift value of the current clocksource. */
27 int shift;
155ec602
MS
28
29 /* Number of clock cycles in one NTP interval. */
30 cycle_t cycle_interval;
31 /* Number of clock shifted nano seconds in one NTP interval. */
32 u64 xtime_interval;
33 /* Raw nano seconds accumulated per NTP interval. */
34 u32 raw_interval;
35
36 /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
37 u64 xtime_nsec;
38 /* Difference between accumulated time and NTP time in ntp
39 * shifted nano seconds. */
40 s64 ntp_error;
23ce7211
MS
41 /* Shift conversion between clock shifted nano seconds and
42 * ntp shifted nano seconds. */
43 int ntp_error_shift;
155ec602
MS
44};
45
46struct timekeeper timekeeper;
47
48/**
49 * timekeeper_setup_internals - Set up internals to use clocksource clock.
50 *
51 * @clock: Pointer to clocksource.
52 *
53 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
54 * pair and interval request.
55 *
56 * Unless you're the timekeeping code, you should not be using this!
57 */
58static void timekeeper_setup_internals(struct clocksource *clock)
59{
60 cycle_t interval;
61 u64 tmp;
62
63 timekeeper.clock = clock;
64 clock->cycle_last = clock->read(clock);
65
66 /* Do the ns -> cycle conversion first, using original mult */
67 tmp = NTP_INTERVAL_LENGTH;
68 tmp <<= clock->shift;
69 tmp += clock->mult_orig/2;
70 do_div(tmp, clock->mult_orig);
71 if (tmp == 0)
72 tmp = 1;
73
74 interval = (cycle_t) tmp;
75 timekeeper.cycle_interval = interval;
76
77 /* Go back from cycles -> shifted ns */
78 timekeeper.xtime_interval = (u64) interval * clock->mult;
79 timekeeper.raw_interval =
80 ((u64) interval * clock->mult_orig) >> clock->shift;
81
82 timekeeper.xtime_nsec = 0;
23ce7211 83 timekeeper.shift = clock->shift;
155ec602
MS
84
85 timekeeper.ntp_error = 0;
23ce7211 86 timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
155ec602 87}
8524070b
JS
88
89/*
90 * This read-write spinlock protects us from races in SMP while
dce48a84 91 * playing with xtime.
8524070b 92 */
ba2a631b 93__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
8524070b
JS
94
95
96/*
97 * The current time
98 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
99 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
100 * at zero at system boot time, so wall_to_monotonic will be negative,
101 * however, we will ALWAYS keep the tv_nsec part positive so we can use
102 * the usual normalization.
7c3f1a57
TJ
103 *
104 * wall_to_monotonic is moved after resume from suspend for the monotonic
105 * time not to jump. We need to add total_sleep_time to wall_to_monotonic
106 * to get the real boot based time offset.
107 *
108 * - wall_to_monotonic is no longer the boot time, getboottime must be
109 * used instead.
8524070b
JS
110 */
111struct timespec xtime __attribute__ ((aligned (16)));
112struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
7c3f1a57 113static unsigned long total_sleep_time; /* seconds */
8524070b 114
155ec602
MS
115/*
116 * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
117 */
118struct timespec raw_time;
119
1c5745aa
TG
120/* flag for if timekeeping is suspended */
121int __read_mostly timekeeping_suspended;
122
17c38b74 123static struct timespec xtime_cache __attribute__ ((aligned (16)));
1001d0a9 124void update_xtime_cache(u64 nsec)
17c38b74
JS
125{
126 xtime_cache = xtime;
127 timespec_add_ns(&xtime_cache, nsec);
128}
17c38b74 129
31089c13
JS
130/* must hold xtime_lock */
131void timekeeping_leap_insert(int leapsecond)
132{
133 xtime.tv_sec += leapsecond;
134 wall_to_monotonic.tv_sec -= leapsecond;
155ec602 135 update_vsyscall(&xtime, timekeeper.clock);
31089c13 136}
8524070b
JS
137
138#ifdef CONFIG_GENERIC_TIME
139/**
155ec602 140 * timekeeping_forward_now - update clock to the current time
8524070b 141 *
9a055117
RZ
142 * Forward the current clock to update its state since the last call to
143 * update_wall_time(). This is useful before significant clock changes,
144 * as it avoids having to deal with this time offset explicitly.
8524070b 145 */
155ec602 146static void timekeeping_forward_now(void)
8524070b
JS
147{
148 cycle_t cycle_now, cycle_delta;
155ec602 149 struct clocksource *clock;
9a055117 150 s64 nsec;
8524070b 151
155ec602 152 clock = timekeeper.clock;
a0f7d48b 153 cycle_now = clock->read(clock);
8524070b 154 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
9a055117 155 clock->cycle_last = cycle_now;
8524070b 156
155ec602 157 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
7d27558c
JS
158
159 /* If arch requires, add in gettimeoffset() */
160 nsec += arch_gettimeoffset();
161
9a055117 162 timespec_add_ns(&xtime, nsec);
2d42244a 163
155ec602
MS
164 nsec = clocksource_cyc2ns(cycle_delta, clock->mult_orig, clock->shift);
165 timespec_add_ns(&raw_time, nsec);
8524070b
JS
166}
167
168/**
efd9ac86 169 * getnstimeofday - Returns the time of day in a timespec
8524070b
JS
170 * @ts: pointer to the timespec to be set
171 *
efd9ac86 172 * Returns the time of day in a timespec.
8524070b 173 */
efd9ac86 174void getnstimeofday(struct timespec *ts)
8524070b 175{
9a055117 176 cycle_t cycle_now, cycle_delta;
155ec602 177 struct clocksource *clock;
8524070b
JS
178 unsigned long seq;
179 s64 nsecs;
180
1c5745aa
TG
181 WARN_ON(timekeeping_suspended);
182
8524070b
JS
183 do {
184 seq = read_seqbegin(&xtime_lock);
185
186 *ts = xtime;
9a055117
RZ
187
188 /* read clocksource: */
155ec602 189 clock = timekeeper.clock;
a0f7d48b 190 cycle_now = clock->read(clock);
9a055117
RZ
191
192 /* calculate the delta since the last update_wall_time: */
193 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
194
195 /* convert to nanoseconds: */
155ec602
MS
196 nsecs = clocksource_cyc2ns(cycle_delta, clock->mult,
197 clock->shift);
8524070b 198
7d27558c
JS
199 /* If arch requires, add in gettimeoffset() */
200 nsecs += arch_gettimeoffset();
201
8524070b
JS
202 } while (read_seqretry(&xtime_lock, seq));
203
204 timespec_add_ns(ts, nsecs);
205}
206
8524070b
JS
207EXPORT_SYMBOL(getnstimeofday);
208
951ed4d3
MS
209ktime_t ktime_get(void)
210{
211 cycle_t cycle_now, cycle_delta;
155ec602 212 struct clocksource *clock;
951ed4d3
MS
213 unsigned int seq;
214 s64 secs, nsecs;
215
216 WARN_ON(timekeeping_suspended);
217
218 do {
219 seq = read_seqbegin(&xtime_lock);
220 secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
221 nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
222
223 /* read clocksource: */
155ec602 224 clock = timekeeper.clock;
a0f7d48b 225 cycle_now = clock->read(clock);
951ed4d3
MS
226
227 /* calculate the delta since the last update_wall_time: */
228 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
229
230 /* convert to nanoseconds: */
155ec602
MS
231 nsecs += clocksource_cyc2ns(cycle_delta, clock->mult,
232 clock->shift);
951ed4d3
MS
233
234 } while (read_seqretry(&xtime_lock, seq));
235 /*
236 * Use ktime_set/ktime_add_ns to create a proper ktime on
237 * 32-bit architectures without CONFIG_KTIME_SCALAR.
238 */
239 return ktime_add_ns(ktime_set(secs, 0), nsecs);
240}
241EXPORT_SYMBOL_GPL(ktime_get);
242
243/**
244 * ktime_get_ts - get the monotonic clock in timespec format
245 * @ts: pointer to timespec variable
246 *
247 * The function calculates the monotonic clock from the realtime
248 * clock and the wall_to_monotonic offset and stores the result
249 * in normalized timespec format in the variable pointed to by @ts.
250 */
251void ktime_get_ts(struct timespec *ts)
252{
253 cycle_t cycle_now, cycle_delta;
155ec602 254 struct clocksource *clock;
951ed4d3
MS
255 struct timespec tomono;
256 unsigned int seq;
257 s64 nsecs;
258
259 WARN_ON(timekeeping_suspended);
260
261 do {
262 seq = read_seqbegin(&xtime_lock);
263 *ts = xtime;
264 tomono = wall_to_monotonic;
265
266 /* read clocksource: */
155ec602 267 clock = timekeeper.clock;
a0f7d48b 268 cycle_now = clock->read(clock);
951ed4d3
MS
269
270 /* calculate the delta since the last update_wall_time: */
271 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
272
273 /* convert to nanoseconds: */
155ec602
MS
274 nsecs = clocksource_cyc2ns(cycle_delta, clock->mult,
275 clock->shift);
951ed4d3
MS
276
277 } while (read_seqretry(&xtime_lock, seq));
278
279 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
280 ts->tv_nsec + tomono.tv_nsec + nsecs);
281}
282EXPORT_SYMBOL_GPL(ktime_get_ts);
283
8524070b
JS
284/**
285 * do_gettimeofday - Returns the time of day in a timeval
286 * @tv: pointer to the timeval to be set
287 *
efd9ac86 288 * NOTE: Users should be converted to using getnstimeofday()
8524070b
JS
289 */
290void do_gettimeofday(struct timeval *tv)
291{
292 struct timespec now;
293
efd9ac86 294 getnstimeofday(&now);
8524070b
JS
295 tv->tv_sec = now.tv_sec;
296 tv->tv_usec = now.tv_nsec/1000;
297}
298
299EXPORT_SYMBOL(do_gettimeofday);
300/**
301 * do_settimeofday - Sets the time of day
302 * @tv: pointer to the timespec variable containing the new time
303 *
304 * Sets the time of day to the new time and update NTP and notify hrtimers
305 */
306int do_settimeofday(struct timespec *tv)
307{
9a055117 308 struct timespec ts_delta;
8524070b 309 unsigned long flags;
8524070b
JS
310
311 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
312 return -EINVAL;
313
314 write_seqlock_irqsave(&xtime_lock, flags);
315
155ec602 316 timekeeping_forward_now();
9a055117
RZ
317
318 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
319 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
320 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
8524070b 321
9a055117 322 xtime = *tv;
8524070b 323
1001d0a9 324 update_xtime_cache(0);
8524070b 325
155ec602 326 timekeeper.ntp_error = 0;
8524070b
JS
327 ntp_clear();
328
155ec602 329 update_vsyscall(&xtime, timekeeper.clock);
8524070b
JS
330
331 write_sequnlock_irqrestore(&xtime_lock, flags);
332
333 /* signal hrtimers about time change */
334 clock_was_set();
335
336 return 0;
337}
338
339EXPORT_SYMBOL(do_settimeofday);
340
341/**
342 * change_clocksource - Swaps clocksources if a new one is available
343 *
344 * Accumulates current time interval and initializes new clocksource
345 */
346static void change_clocksource(void)
347{
4614e6ad 348 struct clocksource *new, *old;
8524070b
JS
349
350 new = clocksource_get_next();
351
155ec602 352 if (!new || timekeeper.clock == new)
8524070b
JS
353 return;
354
155ec602 355 timekeeping_forward_now();
8524070b 356
a0f7d48b 357 if (new->enable && !new->enable(new))
4614e6ad 358 return;
a0f7d48b
MS
359 /*
360 * The frequency may have changed while the clocksource
361 * was disabled. If so the code in ->enable() must update
362 * the mult value to reflect the new frequency. Make sure
363 * mult_orig follows this change.
364 */
365 new->mult_orig = new->mult;
2d42244a 366
155ec602
MS
367 old = timekeeper.clock;
368 timekeeper_setup_internals(new);
369
a0f7d48b
MS
370 /*
371 * Save mult_orig in mult so that the value can be restored
372 * regardless if ->enable() updates the value of mult or not.
373 */
374 old->mult = old->mult_orig;
375 if (old->disable)
376 old->disable(old);
4614e6ad 377
8524070b 378 tick_clock_notify();
8524070b 379}
a40f262c 380#else /* GENERIC_TIME */
155ec602 381static inline void timekeeping_forward_now(void) { }
8524070b 382static inline void change_clocksource(void) { }
a40f262c
TG
383
384/**
385 * ktime_get - get the monotonic time in ktime_t format
386 *
387 * returns the time in ktime_t format
388 */
389ktime_t ktime_get(void)
390{
391 struct timespec now;
392
393 ktime_get_ts(&now);
394
395 return timespec_to_ktime(now);
396}
397EXPORT_SYMBOL_GPL(ktime_get);
398
399/**
400 * ktime_get_ts - get the monotonic clock in timespec format
401 * @ts: pointer to timespec variable
402 *
403 * The function calculates the monotonic clock from the realtime
404 * clock and the wall_to_monotonic offset and stores the result
405 * in normalized timespec format in the variable pointed to by @ts.
406 */
407void ktime_get_ts(struct timespec *ts)
408{
409 struct timespec tomono;
410 unsigned long seq;
411
412 do {
413 seq = read_seqbegin(&xtime_lock);
414 getnstimeofday(ts);
415 tomono = wall_to_monotonic;
416
417 } while (read_seqretry(&xtime_lock, seq));
418
419 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
420 ts->tv_nsec + tomono.tv_nsec);
421}
422EXPORT_SYMBOL_GPL(ktime_get_ts);
423#endif /* !GENERIC_TIME */
424
425/**
426 * ktime_get_real - get the real (wall-) time in ktime_t format
427 *
428 * returns the time in ktime_t format
429 */
430ktime_t ktime_get_real(void)
431{
432 struct timespec now;
433
434 getnstimeofday(&now);
435
436 return timespec_to_ktime(now);
437}
438EXPORT_SYMBOL_GPL(ktime_get_real);
8524070b 439
2d42244a
JS
440/**
441 * getrawmonotonic - Returns the raw monotonic time in a timespec
442 * @ts: pointer to the timespec to be set
443 *
444 * Returns the raw monotonic time (completely un-modified by ntp)
445 */
446void getrawmonotonic(struct timespec *ts)
447{
448 unsigned long seq;
449 s64 nsecs;
450 cycle_t cycle_now, cycle_delta;
155ec602 451 struct clocksource *clock;
2d42244a
JS
452
453 do {
454 seq = read_seqbegin(&xtime_lock);
455
456 /* read clocksource: */
155ec602 457 clock = timekeeper.clock;
a0f7d48b 458 cycle_now = clock->read(clock);
2d42244a
JS
459
460 /* calculate the delta since the last update_wall_time: */
461 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
462
463 /* convert to nanoseconds: */
155ec602
MS
464 nsecs = clocksource_cyc2ns(cycle_delta, clock->mult_orig,
465 clock->shift);
2d42244a 466
155ec602 467 *ts = raw_time;
2d42244a
JS
468
469 } while (read_seqretry(&xtime_lock, seq));
470
471 timespec_add_ns(ts, nsecs);
472}
473EXPORT_SYMBOL(getrawmonotonic);
474
475
8524070b 476/**
cf4fc6cb 477 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
8524070b 478 */
cf4fc6cb 479int timekeeping_valid_for_hres(void)
8524070b
JS
480{
481 unsigned long seq;
482 int ret;
483
484 do {
485 seq = read_seqbegin(&xtime_lock);
486
155ec602 487 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
8524070b
JS
488
489 } while (read_seqretry(&xtime_lock, seq));
490
491 return ret;
492}
493
494/**
495 * read_persistent_clock - Return time in seconds from the persistent clock.
496 *
497 * Weak dummy function for arches that do not yet support it.
498 * Returns seconds from epoch using the battery backed persistent clock.
499 * Returns zero if unsupported.
500 *
501 * XXX - Do be sure to remove it once all arches implement it.
502 */
503unsigned long __attribute__((weak)) read_persistent_clock(void)
504{
505 return 0;
506}
507
508/*
509 * timekeeping_init - Initializes the clocksource and common timekeeping values
510 */
511void __init timekeeping_init(void)
512{
155ec602 513 struct clocksource *clock;
8524070b
JS
514 unsigned long flags;
515 unsigned long sec = read_persistent_clock();
516
517 write_seqlock_irqsave(&xtime_lock, flags);
518
7dffa3c6 519 ntp_init();
8524070b 520
f1b82746 521 clock = clocksource_default_clock();
a0f7d48b
MS
522 if (clock->enable)
523 clock->enable(clock);
524 /* set mult_orig on enable */
525 clock->mult_orig = clock->mult;
155ec602
MS
526
527 timekeeper_setup_internals(clock);
8524070b
JS
528
529 xtime.tv_sec = sec;
530 xtime.tv_nsec = 0;
155ec602
MS
531 raw_time.tv_sec = 0;
532 raw_time.tv_nsec = 0;
8524070b
JS
533 set_normalized_timespec(&wall_to_monotonic,
534 -xtime.tv_sec, -xtime.tv_nsec);
1001d0a9 535 update_xtime_cache(0);
7c3f1a57 536 total_sleep_time = 0;
8524070b
JS
537 write_sequnlock_irqrestore(&xtime_lock, flags);
538}
539
8524070b
JS
540/* time in seconds when suspend began */
541static unsigned long timekeeping_suspend_time;
542
543/**
544 * timekeeping_resume - Resumes the generic timekeeping subsystem.
545 * @dev: unused
546 *
547 * This is for the generic clocksource timekeeping.
548 * xtime/wall_to_monotonic/jiffies/etc are
549 * still managed by arch specific suspend/resume code.
550 */
551static int timekeeping_resume(struct sys_device *dev)
552{
553 unsigned long flags;
554 unsigned long now = read_persistent_clock();
555
d10ff3fb
TG
556 clocksource_resume();
557
8524070b
JS
558 write_seqlock_irqsave(&xtime_lock, flags);
559
560 if (now && (now > timekeeping_suspend_time)) {
561 unsigned long sleep_length = now - timekeeping_suspend_time;
562
563 xtime.tv_sec += sleep_length;
564 wall_to_monotonic.tv_sec -= sleep_length;
7c3f1a57 565 total_sleep_time += sleep_length;
8524070b 566 }
1001d0a9 567 update_xtime_cache(0);
8524070b 568 /* re-base the last cycle value */
155ec602
MS
569 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
570 timekeeper.ntp_error = 0;
8524070b
JS
571 timekeeping_suspended = 0;
572 write_sequnlock_irqrestore(&xtime_lock, flags);
573
574 touch_softlockup_watchdog();
575
576 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
577
578 /* Resume hrtimers */
579 hres_timers_resume();
580
581 return 0;
582}
583
584static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
585{
586 unsigned long flags;
587
3be90950
TG
588 timekeeping_suspend_time = read_persistent_clock();
589
8524070b 590 write_seqlock_irqsave(&xtime_lock, flags);
155ec602 591 timekeeping_forward_now();
8524070b 592 timekeeping_suspended = 1;
8524070b
JS
593 write_sequnlock_irqrestore(&xtime_lock, flags);
594
595 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
596
597 return 0;
598}
599
600/* sysfs resume/suspend bits for timekeeping */
601static struct sysdev_class timekeeping_sysclass = {
af5ca3f4 602 .name = "timekeeping",
8524070b
JS
603 .resume = timekeeping_resume,
604 .suspend = timekeeping_suspend,
8524070b
JS
605};
606
607static struct sys_device device_timer = {
608 .id = 0,
609 .cls = &timekeeping_sysclass,
610};
611
612static int __init timekeeping_init_device(void)
613{
614 int error = sysdev_class_register(&timekeeping_sysclass);
615 if (!error)
616 error = sysdev_register(&device_timer);
617 return error;
618}
619
620device_initcall(timekeeping_init_device);
621
622/*
623 * If the error is already larger, we look ahead even further
624 * to compensate for late or lost adjustments.
625 */
155ec602 626static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
8524070b
JS
627 s64 *offset)
628{
629 s64 tick_error, i;
630 u32 look_ahead, adj;
631 s32 error2, mult;
632
633 /*
634 * Use the current error value to determine how much to look ahead.
635 * The larger the error the slower we adjust for it to avoid problems
636 * with losing too many ticks, otherwise we would overadjust and
637 * produce an even larger error. The smaller the adjustment the
638 * faster we try to adjust for it, as lost ticks can do less harm
3eb05676 639 * here. This is tuned so that an error of about 1 msec is adjusted
8524070b
JS
640 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
641 */
155ec602 642 error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
8524070b
JS
643 error2 = abs(error2);
644 for (look_ahead = 0; error2 > 0; look_ahead++)
645 error2 >>= 2;
646
647 /*
648 * Now calculate the error in (1 << look_ahead) ticks, but first
649 * remove the single look ahead already included in the error.
650 */
23ce7211 651 tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
155ec602 652 tick_error -= timekeeper.xtime_interval >> 1;
8524070b
JS
653 error = ((error - tick_error) >> look_ahead) + tick_error;
654
655 /* Finally calculate the adjustment shift value. */
656 i = *interval;
657 mult = 1;
658 if (error < 0) {
659 error = -error;
660 *interval = -*interval;
661 *offset = -*offset;
662 mult = -1;
663 }
664 for (adj = 0; error > i; adj++)
665 error >>= 1;
666
667 *interval <<= adj;
668 *offset <<= adj;
669 return mult << adj;
670}
671
672/*
673 * Adjust the multiplier to reduce the error value,
674 * this is optimized for the most common adjustments of -1,0,1,
675 * for other values we can do a bit more work.
676 */
155ec602 677static void timekeeping_adjust(s64 offset)
8524070b 678{
155ec602 679 s64 error, interval = timekeeper.cycle_interval;
8524070b
JS
680 int adj;
681
23ce7211 682 error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
8524070b
JS
683 if (error > interval) {
684 error >>= 2;
685 if (likely(error <= interval))
686 adj = 1;
687 else
155ec602 688 adj = timekeeping_bigadjust(error, &interval, &offset);
8524070b
JS
689 } else if (error < -interval) {
690 error >>= 2;
691 if (likely(error >= -interval)) {
692 adj = -1;
693 interval = -interval;
694 offset = -offset;
695 } else
155ec602 696 adj = timekeeping_bigadjust(error, &interval, &offset);
8524070b
JS
697 } else
698 return;
699
155ec602
MS
700 timekeeper.clock->mult += adj;
701 timekeeper.xtime_interval += interval;
702 timekeeper.xtime_nsec -= offset;
703 timekeeper.ntp_error -= (interval - offset) <<
23ce7211 704 timekeeper.ntp_error_shift;
8524070b
JS
705}
706
707/**
708 * update_wall_time - Uses the current clocksource to increment the wall time
709 *
710 * Called from the timer interrupt, must hold a write on xtime_lock.
711 */
712void update_wall_time(void)
713{
155ec602 714 struct clocksource *clock;
8524070b 715 cycle_t offset;
23ce7211 716 u64 nsecs;
8524070b
JS
717
718 /* Make sure we're fully resumed: */
719 if (unlikely(timekeeping_suspended))
720 return;
721
155ec602 722 clock = timekeeper.clock;
8524070b 723#ifdef CONFIG_GENERIC_TIME
a0f7d48b 724 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
8524070b 725#else
155ec602 726 offset = timekeeper.cycle_interval;
8524070b 727#endif
23ce7211 728 timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
8524070b
JS
729
730 /* normally this loop will run just once, however in the
731 * case of lost or late ticks, it will accumulate correctly.
732 */
155ec602 733 while (offset >= timekeeper.cycle_interval) {
23ce7211 734 u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
155ec602 735
8524070b 736 /* accumulate one interval */
155ec602
MS
737 offset -= timekeeper.cycle_interval;
738 clock->cycle_last += timekeeper.cycle_interval;
8524070b 739
155ec602
MS
740 timekeeper.xtime_nsec += timekeeper.xtime_interval;
741 if (timekeeper.xtime_nsec >= nsecps) {
742 timekeeper.xtime_nsec -= nsecps;
8524070b
JS
743 xtime.tv_sec++;
744 second_overflow();
745 }
746
155ec602
MS
747 raw_time.tv_nsec += timekeeper.raw_interval;
748 if (raw_time.tv_nsec >= NSEC_PER_SEC) {
749 raw_time.tv_nsec -= NSEC_PER_SEC;
750 raw_time.tv_sec++;
2d42244a
JS
751 }
752
8524070b 753 /* accumulate error between NTP and clock interval */
155ec602
MS
754 timekeeper.ntp_error += tick_length;
755 timekeeper.ntp_error -= timekeeper.xtime_interval <<
23ce7211 756 timekeeper.ntp_error_shift;
8524070b
JS
757 }
758
759 /* correct the clock when NTP error is too big */
155ec602 760 timekeeping_adjust(offset);
8524070b 761
6c9bacb4
JS
762 /*
763 * Since in the loop above, we accumulate any amount of time
764 * in xtime_nsec over a second into xtime.tv_sec, its possible for
765 * xtime_nsec to be fairly small after the loop. Further, if we're
155ec602 766 * slightly speeding the clocksource up in timekeeping_adjust(),
6c9bacb4
JS
767 * its possible the required corrective factor to xtime_nsec could
768 * cause it to underflow.
769 *
770 * Now, we cannot simply roll the accumulated second back, since
771 * the NTP subsystem has been notified via second_overflow. So
772 * instead we push xtime_nsec forward by the amount we underflowed,
773 * and add that amount into the error.
774 *
775 * We'll correct this error next time through this function, when
776 * xtime_nsec is not as small.
777 */
155ec602
MS
778 if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
779 s64 neg = -(s64)timekeeper.xtime_nsec;
780 timekeeper.xtime_nsec = 0;
23ce7211 781 timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
6c9bacb4
JS
782 }
783
5cd1c9c5
RZ
784 /* store full nanoseconds into xtime after rounding it up and
785 * add the remainder to the error difference.
786 */
23ce7211
MS
787 xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
788 timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
789 timekeeper.ntp_error += timekeeper.xtime_nsec <<
790 timekeeper.ntp_error_shift;
8524070b 791
155ec602
MS
792 nsecs = clocksource_cyc2ns(offset, clock->mult, clock->shift);
793 update_xtime_cache(nsecs);
17c38b74 794
8524070b
JS
795 /* check to see if there is a new clocksource to use */
796 change_clocksource();
155ec602 797 update_vsyscall(&xtime, timekeeper.clock);
8524070b 798}
7c3f1a57
TJ
799
800/**
801 * getboottime - Return the real time of system boot.
802 * @ts: pointer to the timespec to be set
803 *
804 * Returns the time of day in a timespec.
805 *
806 * This is based on the wall_to_monotonic offset and the total suspend
807 * time. Calls to settimeofday will affect the value returned (which
808 * basically means that however wrong your real time clock is at boot time,
809 * you get the right time here).
810 */
811void getboottime(struct timespec *ts)
812{
813 set_normalized_timespec(ts,
814 - (wall_to_monotonic.tv_sec + total_sleep_time),
815 - wall_to_monotonic.tv_nsec);
816}
817
818/**
819 * monotonic_to_bootbased - Convert the monotonic time to boot based.
820 * @ts: pointer to the timespec to be converted
821 */
822void monotonic_to_bootbased(struct timespec *ts)
823{
824 ts->tv_sec += total_sleep_time;
825}
2c6b47de 826
17c38b74
JS
827unsigned long get_seconds(void)
828{
829 return xtime_cache.tv_sec;
830}
831EXPORT_SYMBOL(get_seconds);
832
833
2c6b47de
JS
834struct timespec current_kernel_time(void)
835{
836 struct timespec now;
837 unsigned long seq;
838
839 do {
840 seq = read_seqbegin(&xtime_lock);
841
17c38b74 842 now = xtime_cache;
2c6b47de
JS
843 } while (read_seqretry(&xtime_lock, seq));
844
845 return now;
846}
2c6b47de 847EXPORT_SYMBOL(current_kernel_time);