time: Move total_sleep_time into the timekeeper structure
[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
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>
d43c36dc 16#include <linux/sched.h>
e1a85b2c 17#include <linux/syscore_ops.h>
8524070b 18#include <linux/clocksource.h>
19#include <linux/jiffies.h>
20#include <linux/time.h>
21#include <linux/tick.h>
75c5158f 22#include <linux/stop_machine.h>
8524070b 23
155ec602
MS
24/* Structure holding internal timekeeping values. */
25struct timekeeper {
26 /* Current clocksource used for timekeeping. */
27 struct clocksource *clock;
23ce7211
MS
28 /* The shift value of the current clocksource. */
29 int shift;
155ec602
MS
30
31 /* Number of clock cycles in one NTP interval. */
32 cycle_t cycle_interval;
33 /* Number of clock shifted nano seconds in one NTP interval. */
34 u64 xtime_interval;
a386b5af
KP
35 /* shifted nano seconds left over when rounding cycle_interval */
36 s64 xtime_remainder;
155ec602
MS
37 /* Raw nano seconds accumulated per NTP interval. */
38 u32 raw_interval;
39
40 /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
41 u64 xtime_nsec;
42 /* Difference between accumulated time and NTP time in ntp
43 * shifted nano seconds. */
44 s64 ntp_error;
23ce7211
MS
45 /* Shift conversion between clock shifted nano seconds and
46 * ntp shifted nano seconds. */
47 int ntp_error_shift;
0a544198
MS
48 /* NTP adjusted clock multiplier */
49 u32 mult;
00c5fb77
JS
50
51 /* time spent in suspend */
52 struct timespec total_sleep_time;
53
155ec602
MS
54};
55
afa14e7c 56static struct timekeeper timekeeper;
155ec602
MS
57
58/**
59 * timekeeper_setup_internals - Set up internals to use clocksource clock.
60 *
61 * @clock: Pointer to clocksource.
62 *
63 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
64 * pair and interval request.
65 *
66 * Unless you're the timekeeping code, you should not be using this!
67 */
68static void timekeeper_setup_internals(struct clocksource *clock)
69{
70 cycle_t interval;
a386b5af 71 u64 tmp, ntpinterval;
155ec602
MS
72
73 timekeeper.clock = clock;
74 clock->cycle_last = clock->read(clock);
75
76 /* Do the ns -> cycle conversion first, using original mult */
77 tmp = NTP_INTERVAL_LENGTH;
78 tmp <<= clock->shift;
a386b5af 79 ntpinterval = tmp;
0a544198
MS
80 tmp += clock->mult/2;
81 do_div(tmp, clock->mult);
155ec602
MS
82 if (tmp == 0)
83 tmp = 1;
84
85 interval = (cycle_t) tmp;
86 timekeeper.cycle_interval = interval;
87
88 /* Go back from cycles -> shifted ns */
89 timekeeper.xtime_interval = (u64) interval * clock->mult;
a386b5af 90 timekeeper.xtime_remainder = ntpinterval - timekeeper.xtime_interval;
155ec602 91 timekeeper.raw_interval =
0a544198 92 ((u64) interval * clock->mult) >> clock->shift;
155ec602
MS
93
94 timekeeper.xtime_nsec = 0;
23ce7211 95 timekeeper.shift = clock->shift;
155ec602
MS
96
97 timekeeper.ntp_error = 0;
23ce7211 98 timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
0a544198
MS
99
100 /*
101 * The timekeeper keeps its own mult values for the currently
102 * active clocksource. These value will be adjusted via NTP
103 * to counteract clock drifting.
104 */
105 timekeeper.mult = clock->mult;
155ec602 106}
8524070b 107
2ba2a305
MS
108/* Timekeeper helper functions. */
109static inline s64 timekeeping_get_ns(void)
110{
111 cycle_t cycle_now, cycle_delta;
112 struct clocksource *clock;
113
114 /* read clocksource: */
115 clock = timekeeper.clock;
116 cycle_now = clock->read(clock);
117
118 /* calculate the delta since the last update_wall_time: */
119 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
120
121 /* return delta convert to nanoseconds using ntp adjusted mult. */
122 return clocksource_cyc2ns(cycle_delta, timekeeper.mult,
123 timekeeper.shift);
124}
125
126static inline s64 timekeeping_get_ns_raw(void)
127{
128 cycle_t cycle_now, cycle_delta;
129 struct clocksource *clock;
130
131 /* read clocksource: */
132 clock = timekeeper.clock;
133 cycle_now = clock->read(clock);
134
135 /* calculate the delta since the last update_wall_time: */
136 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
137
c9fad429 138 /* return delta convert to nanoseconds. */
2ba2a305
MS
139 return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
140}
141
8524070b 142/*
143 * This read-write spinlock protects us from races in SMP while
dce48a84 144 * playing with xtime.
8524070b 145 */
ba2a631b 146__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
8524070b 147
148
149/*
150 * The current time
151 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
152 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
153 * at zero at system boot time, so wall_to_monotonic will be negative,
154 * however, we will ALWAYS keep the tv_nsec part positive so we can use
155 * the usual normalization.
7c3f1a57
TJ
156 *
157 * wall_to_monotonic is moved after resume from suspend for the monotonic
158 * time not to jump. We need to add total_sleep_time to wall_to_monotonic
159 * to get the real boot based time offset.
160 *
161 * - wall_to_monotonic is no longer the boot time, getboottime must be
162 * used instead.
8524070b 163 */
0fb86b06
JS
164static struct timespec xtime __attribute__ ((aligned (16)));
165static struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
8524070b 166
155ec602
MS
167/*
168 * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
169 */
afa14e7c 170static struct timespec raw_time;
155ec602 171
1c5745aa
TG
172/* flag for if timekeeping is suspended */
173int __read_mostly timekeeping_suspended;
174
31089c13
JS
175/* must hold xtime_lock */
176void timekeeping_leap_insert(int leapsecond)
177{
178 xtime.tv_sec += leapsecond;
179 wall_to_monotonic.tv_sec -= leapsecond;
7615856e
JS
180 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
181 timekeeper.mult);
31089c13 182}
8524070b 183
8524070b 184/**
155ec602 185 * timekeeping_forward_now - update clock to the current time
8524070b 186 *
9a055117
RZ
187 * Forward the current clock to update its state since the last call to
188 * update_wall_time(). This is useful before significant clock changes,
189 * as it avoids having to deal with this time offset explicitly.
8524070b 190 */
155ec602 191static void timekeeping_forward_now(void)
8524070b 192{
193 cycle_t cycle_now, cycle_delta;
155ec602 194 struct clocksource *clock;
9a055117 195 s64 nsec;
8524070b 196
155ec602 197 clock = timekeeper.clock;
a0f7d48b 198 cycle_now = clock->read(clock);
8524070b 199 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
9a055117 200 clock->cycle_last = cycle_now;
8524070b 201
0a544198
MS
202 nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
203 timekeeper.shift);
7d27558c 204
205 /* If arch requires, add in gettimeoffset() */
206 nsec += arch_gettimeoffset();
207
9a055117 208 timespec_add_ns(&xtime, nsec);
2d42244a 209
0a544198 210 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
155ec602 211 timespec_add_ns(&raw_time, nsec);
8524070b 212}
213
214/**
efd9ac86 215 * getnstimeofday - Returns the time of day in a timespec
8524070b 216 * @ts: pointer to the timespec to be set
217 *
efd9ac86 218 * Returns the time of day in a timespec.
8524070b 219 */
efd9ac86 220void getnstimeofday(struct timespec *ts)
8524070b 221{
222 unsigned long seq;
223 s64 nsecs;
224
1c5745aa
TG
225 WARN_ON(timekeeping_suspended);
226
8524070b 227 do {
228 seq = read_seqbegin(&xtime_lock);
229
230 *ts = xtime;
2ba2a305 231 nsecs = timekeeping_get_ns();
8524070b 232
7d27558c 233 /* If arch requires, add in gettimeoffset() */
234 nsecs += arch_gettimeoffset();
235
8524070b 236 } while (read_seqretry(&xtime_lock, seq));
237
238 timespec_add_ns(ts, nsecs);
239}
240
8524070b 241EXPORT_SYMBOL(getnstimeofday);
242
951ed4d3
MS
243ktime_t ktime_get(void)
244{
951ed4d3
MS
245 unsigned int seq;
246 s64 secs, nsecs;
247
248 WARN_ON(timekeeping_suspended);
249
250 do {
251 seq = read_seqbegin(&xtime_lock);
252 secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
253 nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
2ba2a305 254 nsecs += timekeeping_get_ns();
d004e024
HP
255 /* If arch requires, add in gettimeoffset() */
256 nsecs += arch_gettimeoffset();
951ed4d3
MS
257
258 } while (read_seqretry(&xtime_lock, seq));
259 /*
260 * Use ktime_set/ktime_add_ns to create a proper ktime on
261 * 32-bit architectures without CONFIG_KTIME_SCALAR.
262 */
263 return ktime_add_ns(ktime_set(secs, 0), nsecs);
264}
265EXPORT_SYMBOL_GPL(ktime_get);
266
267/**
268 * ktime_get_ts - get the monotonic clock in timespec format
269 * @ts: pointer to timespec variable
270 *
271 * The function calculates the monotonic clock from the realtime
272 * clock and the wall_to_monotonic offset and stores the result
273 * in normalized timespec format in the variable pointed to by @ts.
274 */
275void ktime_get_ts(struct timespec *ts)
276{
951ed4d3
MS
277 struct timespec tomono;
278 unsigned int seq;
279 s64 nsecs;
280
281 WARN_ON(timekeeping_suspended);
282
283 do {
284 seq = read_seqbegin(&xtime_lock);
285 *ts = xtime;
286 tomono = wall_to_monotonic;
2ba2a305 287 nsecs = timekeeping_get_ns();
d004e024
HP
288 /* If arch requires, add in gettimeoffset() */
289 nsecs += arch_gettimeoffset();
951ed4d3
MS
290
291 } while (read_seqretry(&xtime_lock, seq));
292
293 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
294 ts->tv_nsec + tomono.tv_nsec + nsecs);
295}
296EXPORT_SYMBOL_GPL(ktime_get_ts);
297
e2c18e49
AG
298#ifdef CONFIG_NTP_PPS
299
300/**
301 * getnstime_raw_and_real - get day and raw monotonic time in timespec format
302 * @ts_raw: pointer to the timespec to be set to raw monotonic time
303 * @ts_real: pointer to the timespec to be set to the time of day
304 *
305 * This function reads both the time of day and raw monotonic time at the
306 * same time atomically and stores the resulting timestamps in timespec
307 * format.
308 */
309void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
310{
311 unsigned long seq;
312 s64 nsecs_raw, nsecs_real;
313
314 WARN_ON_ONCE(timekeeping_suspended);
315
316 do {
317 u32 arch_offset;
318
319 seq = read_seqbegin(&xtime_lock);
320
321 *ts_raw = raw_time;
322 *ts_real = xtime;
323
324 nsecs_raw = timekeeping_get_ns_raw();
325 nsecs_real = timekeeping_get_ns();
326
327 /* If arch requires, add in gettimeoffset() */
328 arch_offset = arch_gettimeoffset();
329 nsecs_raw += arch_offset;
330 nsecs_real += arch_offset;
331
332 } while (read_seqretry(&xtime_lock, seq));
333
334 timespec_add_ns(ts_raw, nsecs_raw);
335 timespec_add_ns(ts_real, nsecs_real);
336}
337EXPORT_SYMBOL(getnstime_raw_and_real);
338
339#endif /* CONFIG_NTP_PPS */
340
8524070b 341/**
342 * do_gettimeofday - Returns the time of day in a timeval
343 * @tv: pointer to the timeval to be set
344 *
efd9ac86 345 * NOTE: Users should be converted to using getnstimeofday()
8524070b 346 */
347void do_gettimeofday(struct timeval *tv)
348{
349 struct timespec now;
350
efd9ac86 351 getnstimeofday(&now);
8524070b 352 tv->tv_sec = now.tv_sec;
353 tv->tv_usec = now.tv_nsec/1000;
354}
355
356EXPORT_SYMBOL(do_gettimeofday);
357/**
358 * do_settimeofday - Sets the time of day
359 * @tv: pointer to the timespec variable containing the new time
360 *
361 * Sets the time of day to the new time and update NTP and notify hrtimers
362 */
1e6d7679 363int do_settimeofday(const struct timespec *tv)
8524070b 364{
9a055117 365 struct timespec ts_delta;
8524070b 366 unsigned long flags;
8524070b 367
368 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
369 return -EINVAL;
370
371 write_seqlock_irqsave(&xtime_lock, flags);
372
155ec602 373 timekeeping_forward_now();
9a055117
RZ
374
375 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
376 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
377 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
8524070b 378
9a055117 379 xtime = *tv;
8524070b 380
155ec602 381 timekeeper.ntp_error = 0;
8524070b 382 ntp_clear();
383
7615856e
JS
384 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
385 timekeeper.mult);
8524070b 386
387 write_sequnlock_irqrestore(&xtime_lock, flags);
388
389 /* signal hrtimers about time change */
390 clock_was_set();
391
392 return 0;
393}
394
395EXPORT_SYMBOL(do_settimeofday);
396
c528f7c6
JS
397
398/**
399 * timekeeping_inject_offset - Adds or subtracts from the current time.
400 * @tv: pointer to the timespec variable containing the offset
401 *
402 * Adds or subtracts an offset value from the current time.
403 */
404int timekeeping_inject_offset(struct timespec *ts)
405{
406 unsigned long flags;
407
408 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
409 return -EINVAL;
410
411 write_seqlock_irqsave(&xtime_lock, flags);
412
413 timekeeping_forward_now();
414
415 xtime = timespec_add(xtime, *ts);
416 wall_to_monotonic = timespec_sub(wall_to_monotonic, *ts);
417
418 timekeeper.ntp_error = 0;
419 ntp_clear();
420
421 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
422 timekeeper.mult);
423
424 write_sequnlock_irqrestore(&xtime_lock, flags);
425
426 /* signal hrtimers about time change */
427 clock_was_set();
428
429 return 0;
430}
431EXPORT_SYMBOL(timekeeping_inject_offset);
432
8524070b 433/**
434 * change_clocksource - Swaps clocksources if a new one is available
435 *
436 * Accumulates current time interval and initializes new clocksource
437 */
75c5158f 438static int change_clocksource(void *data)
8524070b 439{
4614e6ad 440 struct clocksource *new, *old;
8524070b 441
75c5158f 442 new = (struct clocksource *) data;
8524070b 443
155ec602 444 timekeeping_forward_now();
75c5158f
MS
445 if (!new->enable || new->enable(new) == 0) {
446 old = timekeeper.clock;
447 timekeeper_setup_internals(new);
448 if (old->disable)
449 old->disable(old);
450 }
451 return 0;
452}
8524070b 453
75c5158f
MS
454/**
455 * timekeeping_notify - Install a new clock source
456 * @clock: pointer to the clock source
457 *
458 * This function is called from clocksource.c after a new, better clock
459 * source has been registered. The caller holds the clocksource_mutex.
460 */
461void timekeeping_notify(struct clocksource *clock)
462{
463 if (timekeeper.clock == clock)
4614e6ad 464 return;
75c5158f 465 stop_machine(change_clocksource, clock, NULL);
8524070b 466 tick_clock_notify();
8524070b 467}
75c5158f 468
a40f262c
TG
469/**
470 * ktime_get_real - get the real (wall-) time in ktime_t format
471 *
472 * returns the time in ktime_t format
473 */
474ktime_t ktime_get_real(void)
475{
476 struct timespec now;
477
478 getnstimeofday(&now);
479
480 return timespec_to_ktime(now);
481}
482EXPORT_SYMBOL_GPL(ktime_get_real);
8524070b 483
2d42244a
JS
484/**
485 * getrawmonotonic - Returns the raw monotonic time in a timespec
486 * @ts: pointer to the timespec to be set
487 *
488 * Returns the raw monotonic time (completely un-modified by ntp)
489 */
490void getrawmonotonic(struct timespec *ts)
491{
492 unsigned long seq;
493 s64 nsecs;
2d42244a
JS
494
495 do {
496 seq = read_seqbegin(&xtime_lock);
2ba2a305 497 nsecs = timekeeping_get_ns_raw();
155ec602 498 *ts = raw_time;
2d42244a
JS
499
500 } while (read_seqretry(&xtime_lock, seq));
501
502 timespec_add_ns(ts, nsecs);
503}
504EXPORT_SYMBOL(getrawmonotonic);
505
506
8524070b 507/**
cf4fc6cb 508 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
8524070b 509 */
cf4fc6cb 510int timekeeping_valid_for_hres(void)
8524070b 511{
512 unsigned long seq;
513 int ret;
514
515 do {
516 seq = read_seqbegin(&xtime_lock);
517
155ec602 518 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
8524070b 519
520 } while (read_seqretry(&xtime_lock, seq));
521
522 return ret;
523}
524
98962465
JH
525/**
526 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
527 *
528 * Caller must observe xtime_lock via read_seqbegin/read_seqretry to
529 * ensure that the clocksource does not change!
530 */
531u64 timekeeping_max_deferment(void)
532{
533 return timekeeper.clock->max_idle_ns;
534}
535
8524070b 536/**
d4f587c6 537 * read_persistent_clock - Return time from the persistent clock.
8524070b 538 *
539 * Weak dummy function for arches that do not yet support it.
d4f587c6
MS
540 * Reads the time from the battery backed persistent clock.
541 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
8524070b 542 *
543 * XXX - Do be sure to remove it once all arches implement it.
544 */
d4f587c6 545void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
8524070b 546{
d4f587c6
MS
547 ts->tv_sec = 0;
548 ts->tv_nsec = 0;
8524070b 549}
550
23970e38
MS
551/**
552 * read_boot_clock - Return time of the system start.
553 *
554 * Weak dummy function for arches that do not yet support it.
555 * Function to read the exact time the system has been started.
556 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
557 *
558 * XXX - Do be sure to remove it once all arches implement it.
559 */
560void __attribute__((weak)) read_boot_clock(struct timespec *ts)
561{
562 ts->tv_sec = 0;
563 ts->tv_nsec = 0;
564}
565
8524070b 566/*
567 * timekeeping_init - Initializes the clocksource and common timekeeping values
568 */
569void __init timekeeping_init(void)
570{
155ec602 571 struct clocksource *clock;
8524070b 572 unsigned long flags;
23970e38 573 struct timespec now, boot;
d4f587c6
MS
574
575 read_persistent_clock(&now);
23970e38 576 read_boot_clock(&boot);
8524070b 577
578 write_seqlock_irqsave(&xtime_lock, flags);
579
7dffa3c6 580 ntp_init();
8524070b 581
f1b82746 582 clock = clocksource_default_clock();
a0f7d48b
MS
583 if (clock->enable)
584 clock->enable(clock);
155ec602 585 timekeeper_setup_internals(clock);
8524070b 586
d4f587c6
MS
587 xtime.tv_sec = now.tv_sec;
588 xtime.tv_nsec = now.tv_nsec;
155ec602
MS
589 raw_time.tv_sec = 0;
590 raw_time.tv_nsec = 0;
23970e38
MS
591 if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
592 boot.tv_sec = xtime.tv_sec;
593 boot.tv_nsec = xtime.tv_nsec;
594 }
8524070b 595 set_normalized_timespec(&wall_to_monotonic,
23970e38 596 -boot.tv_sec, -boot.tv_nsec);
00c5fb77
JS
597 timekeeper.total_sleep_time.tv_sec = 0;
598 timekeeper.total_sleep_time.tv_nsec = 0;
8524070b 599 write_sequnlock_irqrestore(&xtime_lock, flags);
600}
601
8524070b 602/* time in seconds when suspend began */
d4f587c6 603static struct timespec timekeeping_suspend_time;
8524070b 604
304529b1
JS
605/**
606 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
607 * @delta: pointer to a timespec delta value
608 *
609 * Takes a timespec offset measuring a suspend interval and properly
610 * adds the sleep offset to the timekeeping variables.
611 */
612static void __timekeeping_inject_sleeptime(struct timespec *delta)
613{
cb5de2f8 614 if (!timespec_valid(delta)) {
cbaa5152 615 printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid "
cb5de2f8
JS
616 "sleep delta value!\n");
617 return;
618 }
619
304529b1
JS
620 xtime = timespec_add(xtime, *delta);
621 wall_to_monotonic = timespec_sub(wall_to_monotonic, *delta);
00c5fb77
JS
622 timekeeper.total_sleep_time = timespec_add(
623 timekeeper.total_sleep_time, *delta);
304529b1
JS
624}
625
626
627/**
628 * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
629 * @delta: pointer to a timespec delta value
630 *
631 * This hook is for architectures that cannot support read_persistent_clock
632 * because their RTC/persistent clock is only accessible when irqs are enabled.
633 *
634 * This function should only be called by rtc_resume(), and allows
635 * a suspend offset to be injected into the timekeeping values.
636 */
637void timekeeping_inject_sleeptime(struct timespec *delta)
638{
639 unsigned long flags;
640 struct timespec ts;
641
642 /* Make sure we don't set the clock twice */
643 read_persistent_clock(&ts);
644 if (!(ts.tv_sec == 0 && ts.tv_nsec == 0))
645 return;
646
647 write_seqlock_irqsave(&xtime_lock, flags);
648 timekeeping_forward_now();
649
650 __timekeeping_inject_sleeptime(delta);
651
652 timekeeper.ntp_error = 0;
653 ntp_clear();
654 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
655 timekeeper.mult);
656
657 write_sequnlock_irqrestore(&xtime_lock, flags);
658
659 /* signal hrtimers about time change */
660 clock_was_set();
661}
662
663
8524070b 664/**
665 * timekeeping_resume - Resumes the generic timekeeping subsystem.
8524070b 666 *
667 * This is for the generic clocksource timekeeping.
668 * xtime/wall_to_monotonic/jiffies/etc are
669 * still managed by arch specific suspend/resume code.
670 */
e1a85b2c 671static void timekeeping_resume(void)
8524070b 672{
673 unsigned long flags;
d4f587c6
MS
674 struct timespec ts;
675
676 read_persistent_clock(&ts);
8524070b 677
d10ff3fb
TG
678 clocksource_resume();
679
8524070b 680 write_seqlock_irqsave(&xtime_lock, flags);
681
d4f587c6
MS
682 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
683 ts = timespec_sub(ts, timekeeping_suspend_time);
304529b1 684 __timekeeping_inject_sleeptime(&ts);
8524070b 685 }
686 /* re-base the last cycle value */
155ec602
MS
687 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
688 timekeeper.ntp_error = 0;
8524070b 689 timekeeping_suspended = 0;
690 write_sequnlock_irqrestore(&xtime_lock, flags);
691
692 touch_softlockup_watchdog();
693
694 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
695
696 /* Resume hrtimers */
b12a03ce 697 hrtimers_resume();
8524070b 698}
699
e1a85b2c 700static int timekeeping_suspend(void)
8524070b 701{
702 unsigned long flags;
cb33217b
JS
703 struct timespec delta, delta_delta;
704 static struct timespec old_delta;
8524070b 705
d4f587c6 706 read_persistent_clock(&timekeeping_suspend_time);
3be90950 707
8524070b 708 write_seqlock_irqsave(&xtime_lock, flags);
155ec602 709 timekeeping_forward_now();
8524070b 710 timekeeping_suspended = 1;
cb33217b
JS
711
712 /*
713 * To avoid drift caused by repeated suspend/resumes,
714 * which each can add ~1 second drift error,
715 * try to compensate so the difference in system time
716 * and persistent_clock time stays close to constant.
717 */
718 delta = timespec_sub(xtime, timekeeping_suspend_time);
719 delta_delta = timespec_sub(delta, old_delta);
720 if (abs(delta_delta.tv_sec) >= 2) {
721 /*
722 * if delta_delta is too large, assume time correction
723 * has occured and set old_delta to the current delta.
724 */
725 old_delta = delta;
726 } else {
727 /* Otherwise try to adjust old_system to compensate */
728 timekeeping_suspend_time =
729 timespec_add(timekeeping_suspend_time, delta_delta);
730 }
8524070b 731 write_sequnlock_irqrestore(&xtime_lock, flags);
732
733 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
c54a42b1 734 clocksource_suspend();
8524070b 735
736 return 0;
737}
738
739/* sysfs resume/suspend bits for timekeeping */
e1a85b2c 740static struct syscore_ops timekeeping_syscore_ops = {
8524070b 741 .resume = timekeeping_resume,
742 .suspend = timekeeping_suspend,
8524070b 743};
744
e1a85b2c 745static int __init timekeeping_init_ops(void)
8524070b 746{
e1a85b2c
RW
747 register_syscore_ops(&timekeeping_syscore_ops);
748 return 0;
8524070b 749}
750
e1a85b2c 751device_initcall(timekeeping_init_ops);
8524070b 752
753/*
754 * If the error is already larger, we look ahead even further
755 * to compensate for late or lost adjustments.
756 */
155ec602 757static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
8524070b 758 s64 *offset)
759{
760 s64 tick_error, i;
761 u32 look_ahead, adj;
762 s32 error2, mult;
763
764 /*
765 * Use the current error value to determine how much to look ahead.
766 * The larger the error the slower we adjust for it to avoid problems
767 * with losing too many ticks, otherwise we would overadjust and
768 * produce an even larger error. The smaller the adjustment the
769 * faster we try to adjust for it, as lost ticks can do less harm
3eb05676 770 * here. This is tuned so that an error of about 1 msec is adjusted
8524070b 771 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
772 */
155ec602 773 error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
8524070b 774 error2 = abs(error2);
775 for (look_ahead = 0; error2 > 0; look_ahead++)
776 error2 >>= 2;
777
778 /*
779 * Now calculate the error in (1 << look_ahead) ticks, but first
780 * remove the single look ahead already included in the error.
781 */
23ce7211 782 tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
155ec602 783 tick_error -= timekeeper.xtime_interval >> 1;
8524070b 784 error = ((error - tick_error) >> look_ahead) + tick_error;
785
786 /* Finally calculate the adjustment shift value. */
787 i = *interval;
788 mult = 1;
789 if (error < 0) {
790 error = -error;
791 *interval = -*interval;
792 *offset = -*offset;
793 mult = -1;
794 }
795 for (adj = 0; error > i; adj++)
796 error >>= 1;
797
798 *interval <<= adj;
799 *offset <<= adj;
800 return mult << adj;
801}
802
803/*
804 * Adjust the multiplier to reduce the error value,
805 * this is optimized for the most common adjustments of -1,0,1,
806 * for other values we can do a bit more work.
807 */
155ec602 808static void timekeeping_adjust(s64 offset)
8524070b 809{
155ec602 810 s64 error, interval = timekeeper.cycle_interval;
8524070b 811 int adj;
812
c2bc1111
JS
813 /*
814 * The point of this is to check if the error is greater then half
815 * an interval.
816 *
817 * First we shift it down from NTP_SHIFT to clocksource->shifted nsecs.
818 *
819 * Note we subtract one in the shift, so that error is really error*2.
3f86f28f
JS
820 * This "saves" dividing(shifting) interval twice, but keeps the
821 * (error > interval) comparison as still measuring if error is
c2bc1111
JS
822 * larger then half an interval.
823 *
3f86f28f 824 * Note: It does not "save" on aggravation when reading the code.
c2bc1111 825 */
23ce7211 826 error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
8524070b 827 if (error > interval) {
c2bc1111
JS
828 /*
829 * We now divide error by 4(via shift), which checks if
830 * the error is greater then twice the interval.
831 * If it is greater, we need a bigadjust, if its smaller,
832 * we can adjust by 1.
833 */
8524070b 834 error >>= 2;
c2bc1111
JS
835 /*
836 * XXX - In update_wall_time, we round up to the next
837 * nanosecond, and store the amount rounded up into
838 * the error. This causes the likely below to be unlikely.
839 *
3f86f28f 840 * The proper fix is to avoid rounding up by using
c2bc1111
JS
841 * the high precision timekeeper.xtime_nsec instead of
842 * xtime.tv_nsec everywhere. Fixing this will take some
843 * time.
844 */
8524070b 845 if (likely(error <= interval))
846 adj = 1;
847 else
155ec602 848 adj = timekeeping_bigadjust(error, &interval, &offset);
8524070b 849 } else if (error < -interval) {
c2bc1111 850 /* See comment above, this is just switched for the negative */
8524070b 851 error >>= 2;
852 if (likely(error >= -interval)) {
853 adj = -1;
854 interval = -interval;
855 offset = -offset;
856 } else
155ec602 857 adj = timekeeping_bigadjust(error, &interval, &offset);
c2bc1111 858 } else /* No adjustment needed */
8524070b 859 return;
860
d65670a7
JS
861 WARN_ONCE(timekeeper.clock->maxadj &&
862 (timekeeper.mult + adj > timekeeper.clock->mult +
863 timekeeper.clock->maxadj),
864 "Adjusting %s more then 11%% (%ld vs %ld)\n",
865 timekeeper.clock->name, (long)timekeeper.mult + adj,
866 (long)timekeeper.clock->mult +
867 timekeeper.clock->maxadj);
c2bc1111
JS
868 /*
869 * So the following can be confusing.
870 *
871 * To keep things simple, lets assume adj == 1 for now.
872 *
873 * When adj != 1, remember that the interval and offset values
874 * have been appropriately scaled so the math is the same.
875 *
876 * The basic idea here is that we're increasing the multiplier
877 * by one, this causes the xtime_interval to be incremented by
878 * one cycle_interval. This is because:
879 * xtime_interval = cycle_interval * mult
880 * So if mult is being incremented by one:
881 * xtime_interval = cycle_interval * (mult + 1)
882 * Its the same as:
883 * xtime_interval = (cycle_interval * mult) + cycle_interval
884 * Which can be shortened to:
885 * xtime_interval += cycle_interval
886 *
887 * So offset stores the non-accumulated cycles. Thus the current
888 * time (in shifted nanoseconds) is:
889 * now = (offset * adj) + xtime_nsec
890 * Now, even though we're adjusting the clock frequency, we have
891 * to keep time consistent. In other words, we can't jump back
892 * in time, and we also want to avoid jumping forward in time.
893 *
894 * So given the same offset value, we need the time to be the same
895 * both before and after the freq adjustment.
896 * now = (offset * adj_1) + xtime_nsec_1
897 * now = (offset * adj_2) + xtime_nsec_2
898 * So:
899 * (offset * adj_1) + xtime_nsec_1 =
900 * (offset * adj_2) + xtime_nsec_2
901 * And we know:
902 * adj_2 = adj_1 + 1
903 * So:
904 * (offset * adj_1) + xtime_nsec_1 =
905 * (offset * (adj_1+1)) + xtime_nsec_2
906 * (offset * adj_1) + xtime_nsec_1 =
907 * (offset * adj_1) + offset + xtime_nsec_2
908 * Canceling the sides:
909 * xtime_nsec_1 = offset + xtime_nsec_2
910 * Which gives us:
911 * xtime_nsec_2 = xtime_nsec_1 - offset
912 * Which simplfies to:
913 * xtime_nsec -= offset
914 *
915 * XXX - TODO: Doc ntp_error calculation.
916 */
0a544198 917 timekeeper.mult += adj;
155ec602
MS
918 timekeeper.xtime_interval += interval;
919 timekeeper.xtime_nsec -= offset;
920 timekeeper.ntp_error -= (interval - offset) <<
23ce7211 921 timekeeper.ntp_error_shift;
8524070b 922}
923
83f57a11 924
a092ff0f 925/**
926 * logarithmic_accumulation - shifted accumulation of cycles
927 *
928 * This functions accumulates a shifted interval of cycles into
929 * into a shifted interval nanoseconds. Allows for O(log) accumulation
930 * loop.
931 *
932 * Returns the unconsumed cycles.
933 */
934static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
935{
936 u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
deda2e81 937 u64 raw_nsecs;
a092ff0f 938
939 /* If the offset is smaller then a shifted interval, do nothing */
940 if (offset < timekeeper.cycle_interval<<shift)
941 return offset;
942
943 /* Accumulate one shifted interval */
944 offset -= timekeeper.cycle_interval << shift;
945 timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift;
946
947 timekeeper.xtime_nsec += timekeeper.xtime_interval << shift;
948 while (timekeeper.xtime_nsec >= nsecps) {
949 timekeeper.xtime_nsec -= nsecps;
950 xtime.tv_sec++;
951 second_overflow();
952 }
953
deda2e81
JW
954 /* Accumulate raw time */
955 raw_nsecs = timekeeper.raw_interval << shift;
956 raw_nsecs += raw_time.tv_nsec;
c7dcf87a
JS
957 if (raw_nsecs >= NSEC_PER_SEC) {
958 u64 raw_secs = raw_nsecs;
959 raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
960 raw_time.tv_sec += raw_secs;
a092ff0f 961 }
deda2e81 962 raw_time.tv_nsec = raw_nsecs;
a092ff0f 963
964 /* Accumulate error between NTP and clock interval */
965 timekeeper.ntp_error += tick_length << shift;
a386b5af
KP
966 timekeeper.ntp_error -=
967 (timekeeper.xtime_interval + timekeeper.xtime_remainder) <<
a092ff0f 968 (timekeeper.ntp_error_shift + shift);
969
970 return offset;
971}
972
83f57a11 973
8524070b 974/**
975 * update_wall_time - Uses the current clocksource to increment the wall time
976 *
977 * Called from the timer interrupt, must hold a write on xtime_lock.
978 */
871cf1e5 979static void update_wall_time(void)
8524070b 980{
155ec602 981 struct clocksource *clock;
8524070b 982 cycle_t offset;
a092ff0f 983 int shift = 0, maxshift;
8524070b 984
985 /* Make sure we're fully resumed: */
986 if (unlikely(timekeeping_suspended))
987 return;
988
155ec602 989 clock = timekeeper.clock;
592913ec
JS
990
991#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
155ec602 992 offset = timekeeper.cycle_interval;
592913ec
JS
993#else
994 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
8524070b 995#endif
23ce7211 996 timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
8524070b 997
a092ff0f 998 /*
999 * With NO_HZ we may have to accumulate many cycle_intervals
1000 * (think "ticks") worth of time at once. To do this efficiently,
1001 * we calculate the largest doubling multiple of cycle_intervals
1002 * that is smaller then the offset. We then accumulate that
1003 * chunk in one go, and then try to consume the next smaller
1004 * doubled multiple.
8524070b 1005 */
a092ff0f 1006 shift = ilog2(offset) - ilog2(timekeeper.cycle_interval);
1007 shift = max(0, shift);
1008 /* Bound shift to one less then what overflows tick_length */
1009 maxshift = (8*sizeof(tick_length) - (ilog2(tick_length)+1)) - 1;
1010 shift = min(shift, maxshift);
155ec602 1011 while (offset >= timekeeper.cycle_interval) {
a092ff0f 1012 offset = logarithmic_accumulation(offset, shift);
830ec045
JS
1013 if(offset < timekeeper.cycle_interval<<shift)
1014 shift--;
8524070b 1015 }
1016
1017 /* correct the clock when NTP error is too big */
155ec602 1018 timekeeping_adjust(offset);
8524070b 1019
6c9bacb4 1020 /*
1021 * Since in the loop above, we accumulate any amount of time
1022 * in xtime_nsec over a second into xtime.tv_sec, its possible for
1023 * xtime_nsec to be fairly small after the loop. Further, if we're
155ec602 1024 * slightly speeding the clocksource up in timekeeping_adjust(),
6c9bacb4 1025 * its possible the required corrective factor to xtime_nsec could
1026 * cause it to underflow.
1027 *
1028 * Now, we cannot simply roll the accumulated second back, since
1029 * the NTP subsystem has been notified via second_overflow. So
1030 * instead we push xtime_nsec forward by the amount we underflowed,
1031 * and add that amount into the error.
1032 *
1033 * We'll correct this error next time through this function, when
1034 * xtime_nsec is not as small.
1035 */
155ec602
MS
1036 if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
1037 s64 neg = -(s64)timekeeper.xtime_nsec;
1038 timekeeper.xtime_nsec = 0;
23ce7211 1039 timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
6c9bacb4 1040 }
1041
6a867a39
JS
1042
1043 /*
1044 * Store full nanoseconds into xtime after rounding it up and
5cd1c9c5
RZ
1045 * add the remainder to the error difference.
1046 */
23ce7211
MS
1047 xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
1048 timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
1049 timekeeper.ntp_error += timekeeper.xtime_nsec <<
1050 timekeeper.ntp_error_shift;
8524070b 1051
6a867a39
JS
1052 /*
1053 * Finally, make sure that after the rounding
1054 * xtime.tv_nsec isn't larger then NSEC_PER_SEC
1055 */
1056 if (unlikely(xtime.tv_nsec >= NSEC_PER_SEC)) {
1057 xtime.tv_nsec -= NSEC_PER_SEC;
1058 xtime.tv_sec++;
1059 second_overflow();
1060 }
83f57a11 1061
8524070b 1062 /* check to see if there is a new clocksource to use */
7615856e
JS
1063 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
1064 timekeeper.mult);
8524070b 1065}
7c3f1a57
TJ
1066
1067/**
1068 * getboottime - Return the real time of system boot.
1069 * @ts: pointer to the timespec to be set
1070 *
abb3a4ea 1071 * Returns the wall-time of boot in a timespec.
7c3f1a57
TJ
1072 *
1073 * This is based on the wall_to_monotonic offset and the total suspend
1074 * time. Calls to settimeofday will affect the value returned (which
1075 * basically means that however wrong your real time clock is at boot time,
1076 * you get the right time here).
1077 */
1078void getboottime(struct timespec *ts)
1079{
36d47481 1080 struct timespec boottime = {
00c5fb77
JS
1081 .tv_sec = wall_to_monotonic.tv_sec +
1082 timekeeper.total_sleep_time.tv_sec,
1083 .tv_nsec = wall_to_monotonic.tv_nsec +
1084 timekeeper.total_sleep_time.tv_nsec
36d47481 1085 };
d4f587c6 1086
d4f587c6 1087 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
7c3f1a57 1088}
c93d89f3 1089EXPORT_SYMBOL_GPL(getboottime);
7c3f1a57 1090
abb3a4ea
JS
1091
1092/**
1093 * get_monotonic_boottime - Returns monotonic time since boot
1094 * @ts: pointer to the timespec to be set
1095 *
1096 * Returns the monotonic time since boot in a timespec.
1097 *
1098 * This is similar to CLOCK_MONTONIC/ktime_get_ts, but also
1099 * includes the time spent in suspend.
1100 */
1101void get_monotonic_boottime(struct timespec *ts)
1102{
1103 struct timespec tomono, sleep;
1104 unsigned int seq;
1105 s64 nsecs;
1106
1107 WARN_ON(timekeeping_suspended);
1108
1109 do {
1110 seq = read_seqbegin(&xtime_lock);
1111 *ts = xtime;
1112 tomono = wall_to_monotonic;
00c5fb77 1113 sleep = timekeeper.total_sleep_time;
abb3a4ea
JS
1114 nsecs = timekeeping_get_ns();
1115
1116 } while (read_seqretry(&xtime_lock, seq));
1117
1118 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec,
1119 ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec + nsecs);
1120}
1121EXPORT_SYMBOL_GPL(get_monotonic_boottime);
1122
1123/**
1124 * ktime_get_boottime - Returns monotonic time since boot in a ktime
1125 *
1126 * Returns the monotonic time since boot in a ktime
1127 *
1128 * This is similar to CLOCK_MONTONIC/ktime_get, but also
1129 * includes the time spent in suspend.
1130 */
1131ktime_t ktime_get_boottime(void)
1132{
1133 struct timespec ts;
1134
1135 get_monotonic_boottime(&ts);
1136 return timespec_to_ktime(ts);
1137}
1138EXPORT_SYMBOL_GPL(ktime_get_boottime);
1139
7c3f1a57
TJ
1140/**
1141 * monotonic_to_bootbased - Convert the monotonic time to boot based.
1142 * @ts: pointer to the timespec to be converted
1143 */
1144void monotonic_to_bootbased(struct timespec *ts)
1145{
00c5fb77 1146 *ts = timespec_add(*ts, timekeeper.total_sleep_time);
7c3f1a57 1147}
c93d89f3 1148EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
2c6b47de 1149
17c38b74 1150unsigned long get_seconds(void)
1151{
6a867a39 1152 return xtime.tv_sec;
17c38b74 1153}
1154EXPORT_SYMBOL(get_seconds);
1155
da15cfda 1156struct timespec __current_kernel_time(void)
1157{
6a867a39 1158 return xtime;
da15cfda 1159}
17c38b74 1160
2c6b47de 1161struct timespec current_kernel_time(void)
1162{
1163 struct timespec now;
1164 unsigned long seq;
1165
1166 do {
1167 seq = read_seqbegin(&xtime_lock);
83f57a11 1168
6a867a39 1169 now = xtime;
2c6b47de 1170 } while (read_seqretry(&xtime_lock, seq));
1171
1172 return now;
1173}
2c6b47de 1174EXPORT_SYMBOL(current_kernel_time);
da15cfda 1175
1176struct timespec get_monotonic_coarse(void)
1177{
1178 struct timespec now, mono;
1179 unsigned long seq;
1180
1181 do {
1182 seq = read_seqbegin(&xtime_lock);
83f57a11 1183
6a867a39 1184 now = xtime;
da15cfda 1185 mono = wall_to_monotonic;
1186 } while (read_seqretry(&xtime_lock, seq));
1187
1188 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
1189 now.tv_nsec + mono.tv_nsec);
1190 return now;
1191}
871cf1e5
TH
1192
1193/*
1194 * The 64-bit jiffies value is not atomic - you MUST NOT read it
1195 * without sampling the sequence number in xtime_lock.
1196 * jiffies is defined in the linker script...
1197 */
1198void do_timer(unsigned long ticks)
1199{
1200 jiffies_64 += ticks;
1201 update_wall_time();
1202 calc_global_load(ticks);
1203}
48cf76f7
TH
1204
1205/**
314ac371
JS
1206 * get_xtime_and_monotonic_and_sleep_offset() - get xtime, wall_to_monotonic,
1207 * and sleep offsets.
48cf76f7
TH
1208 * @xtim: pointer to timespec to be set with xtime
1209 * @wtom: pointer to timespec to be set with wall_to_monotonic
314ac371 1210 * @sleep: pointer to timespec to be set with time in suspend
48cf76f7 1211 */
314ac371
JS
1212void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
1213 struct timespec *wtom, struct timespec *sleep)
48cf76f7
TH
1214{
1215 unsigned long seq;
1216
1217 do {
1218 seq = read_seqbegin(&xtime_lock);
1219 *xtim = xtime;
1220 *wtom = wall_to_monotonic;
00c5fb77 1221 *sleep = timekeeper.total_sleep_time;
48cf76f7
TH
1222 } while (read_seqretry(&xtime_lock, seq));
1223}
f0af911a 1224
99ee5315
TG
1225/**
1226 * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
1227 */
1228ktime_t ktime_get_monotonic_offset(void)
1229{
1230 unsigned long seq;
1231 struct timespec wtom;
1232
1233 do {
1234 seq = read_seqbegin(&xtime_lock);
1235 wtom = wall_to_monotonic;
1236 } while (read_seqretry(&xtime_lock, seq));
1237 return timespec_to_ktime(wtom);
1238}
1239
f0af911a
TH
1240/**
1241 * xtime_update() - advances the timekeeping infrastructure
1242 * @ticks: number of ticks, that have elapsed since the last call.
1243 *
1244 * Must be called with interrupts disabled.
1245 */
1246void xtime_update(unsigned long ticks)
1247{
1248 write_seqlock(&xtime_lock);
1249 do_timer(ticks);
1250 write_sequnlock(&xtime_lock);
1251}
This page took 0.446405 seconds and 5 git commands to generate.