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