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