2 * linux/kernel/time/timekeeping.c
4 * Kernel timekeeping code and accessor functions
6 * This code was moved from linux/kernel/timer.c.
7 * Please see that file for copyright and history logs.
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>
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>
27 #include "tick-internal.h"
28 #include "ntp_internal.h"
29 #include "timekeeping_internal.h"
31 #define TK_CLEAR_NTP (1 << 0)
32 #define TK_MIRROR (1 << 1)
33 #define TK_CLOCK_WAS_SET (1 << 2)
35 static struct timekeeper timekeeper
;
36 static DEFINE_RAW_SPINLOCK(timekeeper_lock
);
37 static seqcount_t timekeeper_seq
;
38 static struct timekeeper shadow_timekeeper
;
40 /* flag for if timekeeping is suspended */
41 int __read_mostly timekeeping_suspended
;
43 /* Flag for if there is a persistent clock on this platform */
44 bool __read_mostly persistent_clock_exist
= false;
46 static inline void tk_normalize_xtime(struct timekeeper
*tk
)
48 while (tk
->xtime_nsec
>= ((u64
)NSEC_PER_SEC
<< tk
->shift
)) {
49 tk
->xtime_nsec
-= (u64
)NSEC_PER_SEC
<< tk
->shift
;
54 static void tk_set_xtime(struct timekeeper
*tk
, const struct timespec
*ts
)
56 tk
->xtime_sec
= ts
->tv_sec
;
57 tk
->xtime_nsec
= (u64
)ts
->tv_nsec
<< tk
->shift
;
60 static void tk_xtime_add(struct timekeeper
*tk
, const struct timespec
*ts
)
62 tk
->xtime_sec
+= ts
->tv_sec
;
63 tk
->xtime_nsec
+= (u64
)ts
->tv_nsec
<< tk
->shift
;
64 tk_normalize_xtime(tk
);
67 static void tk_set_wall_to_mono(struct timekeeper
*tk
, struct timespec wtm
)
72 * Verify consistency of: offset_real = -wall_to_monotonic
73 * before modifying anything
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));
84 static void tk_set_sleep_time(struct timekeeper
*tk
, struct timespec t
)
86 /* Verify consistency before modifying */
87 WARN_ON_ONCE(tk
->offs_boot
.tv64
!= timespec_to_ktime(tk
->total_sleep_time
).tv64
);
89 tk
->total_sleep_time
= t
;
90 tk
->offs_boot
= timespec_to_ktime(t
);
94 * tk_setup_internals - Set up internals to use clocksource clock.
96 * @tk: The target timekeeper to setup.
97 * @clock: Pointer to clocksource.
99 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
100 * pair and interval request.
102 * Unless you're the timekeeping code, you should not be using this!
104 static void tk_setup_internals(struct timekeeper
*tk
, struct clocksource
*clock
)
107 u64 tmp
, ntpinterval
;
108 struct clocksource
*old_clock
;
110 old_clock
= tk
->clock
;
112 tk
->cycle_last
= clock
->cycle_last
= clock
->read(clock
);
114 /* Do the ns -> cycle conversion first, using original mult */
115 tmp
= NTP_INTERVAL_LENGTH
;
116 tmp
<<= clock
->shift
;
118 tmp
+= clock
->mult
/2;
119 do_div(tmp
, clock
->mult
);
123 interval
= (cycle_t
) tmp
;
124 tk
->cycle_interval
= interval
;
126 /* Go back from cycles -> shifted ns */
127 tk
->xtime_interval
= (u64
) interval
* clock
->mult
;
128 tk
->xtime_remainder
= ntpinterval
- tk
->xtime_interval
;
130 ((u64
) interval
* clock
->mult
) >> clock
->shift
;
132 /* if changing clocks, convert xtime_nsec shift units */
134 int shift_change
= clock
->shift
- old_clock
->shift
;
135 if (shift_change
< 0)
136 tk
->xtime_nsec
>>= -shift_change
;
138 tk
->xtime_nsec
<<= shift_change
;
140 tk
->shift
= clock
->shift
;
143 tk
->ntp_error_shift
= NTP_SCALE_SHIFT
- clock
->shift
;
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.
150 tk
->mult
= clock
->mult
;
153 /* Timekeeper helper functions. */
155 #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
156 static u32
default_arch_gettimeoffset(void) { return 0; }
157 u32 (*arch_gettimeoffset
)(void) = default_arch_gettimeoffset
;
159 static inline u32
arch_gettimeoffset(void) { return 0; }
162 static inline s64
timekeeping_get_ns(struct timekeeper
*tk
)
164 cycle_t cycle_now
, cycle_delta
;
165 struct clocksource
*clock
;
168 /* read clocksource: */
170 cycle_now
= clock
->read(clock
);
172 /* calculate the delta since the last update_wall_time: */
173 cycle_delta
= (cycle_now
- clock
->cycle_last
) & clock
->mask
;
175 nsec
= cycle_delta
* tk
->mult
+ tk
->xtime_nsec
;
178 /* If arch requires, add in get_arch_timeoffset() */
179 return nsec
+ arch_gettimeoffset();
182 static inline s64
timekeeping_get_ns_raw(struct timekeeper
*tk
)
184 cycle_t cycle_now
, cycle_delta
;
185 struct clocksource
*clock
;
188 /* read clocksource: */
190 cycle_now
= clock
->read(clock
);
192 /* calculate the delta since the last update_wall_time: */
193 cycle_delta
= (cycle_now
- clock
->cycle_last
) & clock
->mask
;
195 /* convert delta to nanoseconds. */
196 nsec
= clocksource_cyc2ns(cycle_delta
, clock
->mult
, clock
->shift
);
198 /* If arch requires, add in get_arch_timeoffset() */
199 return nsec
+ arch_gettimeoffset();
202 static RAW_NOTIFIER_HEAD(pvclock_gtod_chain
);
204 static void update_pvclock_gtod(struct timekeeper
*tk
, bool was_set
)
206 raw_notifier_call_chain(&pvclock_gtod_chain
, was_set
, tk
);
210 * pvclock_gtod_register_notifier - register a pvclock timedata update listener
212 int pvclock_gtod_register_notifier(struct notifier_block
*nb
)
214 struct timekeeper
*tk
= &timekeeper
;
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
);
225 EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier
);
228 * pvclock_gtod_unregister_notifier - unregister a pvclock
229 * timedata update listener
231 int pvclock_gtod_unregister_notifier(struct notifier_block
*nb
)
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
);
242 EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier
);
244 /* must hold timekeeper_lock */
245 static void timekeeping_update(struct timekeeper
*tk
, unsigned int action
)
247 if (action
& TK_CLEAR_NTP
) {
252 update_pvclock_gtod(tk
, action
& TK_CLOCK_WAS_SET
);
254 if (action
& TK_MIRROR
)
255 memcpy(&shadow_timekeeper
, &timekeeper
, sizeof(timekeeper
));
259 * timekeeping_forward_now - update clock to the current time
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.
265 static void timekeeping_forward_now(struct timekeeper
*tk
)
267 cycle_t cycle_now
, cycle_delta
;
268 struct clocksource
*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
;
276 tk
->xtime_nsec
+= cycle_delta
* tk
->mult
;
278 /* If arch requires, add in get_arch_timeoffset() */
279 tk
->xtime_nsec
+= (u64
)arch_gettimeoffset() << tk
->shift
;
281 tk_normalize_xtime(tk
);
283 nsec
= clocksource_cyc2ns(cycle_delta
, clock
->mult
, clock
->shift
);
284 timespec_add_ns(&tk
->raw_time
, nsec
);
288 * __getnstimeofday - Returns the time of day in a timespec.
289 * @ts: pointer to the timespec to be set
291 * Updates the time of day in the timespec.
292 * Returns 0 on success, or -ve when suspended (timespec will be undefined).
294 int __getnstimeofday(struct timespec
*ts
)
296 struct timekeeper
*tk
= &timekeeper
;
301 seq
= read_seqcount_begin(&timekeeper_seq
);
303 ts
->tv_sec
= tk
->xtime_sec
;
304 nsecs
= timekeeping_get_ns(tk
);
306 } while (read_seqcount_retry(&timekeeper_seq
, seq
));
309 timespec_add_ns(ts
, nsecs
);
312 * Do not bail out early, in case there were callers still using
313 * the value, even in the face of the WARN_ON.
315 if (unlikely(timekeeping_suspended
))
319 EXPORT_SYMBOL(__getnstimeofday
);
322 * getnstimeofday - Returns the time of day in a timespec.
323 * @ts: pointer to the timespec to be set
325 * Returns the time of day in a timespec (WARN if suspended).
327 void getnstimeofday(struct timespec
*ts
)
329 WARN_ON(__getnstimeofday(ts
));
331 EXPORT_SYMBOL(getnstimeofday
);
333 ktime_t
ktime_get(void)
335 struct timekeeper
*tk
= &timekeeper
;
339 WARN_ON(timekeeping_suspended
);
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
;
346 } while (read_seqcount_retry(&timekeeper_seq
, seq
));
348 * Use ktime_set/ktime_add_ns to create a proper ktime on
349 * 32-bit architectures without CONFIG_KTIME_SCALAR.
351 return ktime_add_ns(ktime_set(secs
, 0), nsecs
);
353 EXPORT_SYMBOL_GPL(ktime_get
);
356 * ktime_get_ts - get the monotonic clock in timespec format
357 * @ts: pointer to timespec variable
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.
363 void ktime_get_ts(struct timespec
*ts
)
365 struct timekeeper
*tk
= &timekeeper
;
366 struct timespec tomono
;
370 WARN_ON(timekeeping_suspended
);
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
;
378 } while (read_seqcount_retry(&timekeeper_seq
, seq
));
380 ts
->tv_sec
+= tomono
.tv_sec
;
382 timespec_add_ns(ts
, nsec
+ tomono
.tv_nsec
);
384 EXPORT_SYMBOL_GPL(ktime_get_ts
);
388 * timekeeping_clocktai - Returns the TAI time of day in a timespec
389 * @ts: pointer to the timespec to be set
391 * Returns the time of day in a timespec.
393 void timekeeping_clocktai(struct timespec
*ts
)
395 struct timekeeper
*tk
= &timekeeper
;
399 WARN_ON(timekeeping_suspended
);
402 seq
= read_seqcount_begin(&timekeeper_seq
);
404 ts
->tv_sec
= tk
->xtime_sec
+ tk
->tai_offset
;
405 nsecs
= timekeeping_get_ns(tk
);
407 } while (read_seqcount_retry(&timekeeper_seq
, seq
));
410 timespec_add_ns(ts
, nsecs
);
413 EXPORT_SYMBOL(timekeeping_clocktai
);
417 * ktime_get_clocktai - Returns the TAI time of day in a ktime
419 * Returns the time of day in a ktime.
421 ktime_t
ktime_get_clocktai(void)
425 timekeeping_clocktai(&ts
);
426 return timespec_to_ktime(ts
);
428 EXPORT_SYMBOL(ktime_get_clocktai
);
430 #ifdef CONFIG_NTP_PPS
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
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
441 void getnstime_raw_and_real(struct timespec
*ts_raw
, struct timespec
*ts_real
)
443 struct timekeeper
*tk
= &timekeeper
;
445 s64 nsecs_raw
, nsecs_real
;
447 WARN_ON_ONCE(timekeeping_suspended
);
450 seq
= read_seqcount_begin(&timekeeper_seq
);
452 *ts_raw
= tk
->raw_time
;
453 ts_real
->tv_sec
= tk
->xtime_sec
;
454 ts_real
->tv_nsec
= 0;
456 nsecs_raw
= timekeeping_get_ns_raw(tk
);
457 nsecs_real
= timekeeping_get_ns(tk
);
459 } while (read_seqcount_retry(&timekeeper_seq
, seq
));
461 timespec_add_ns(ts_raw
, nsecs_raw
);
462 timespec_add_ns(ts_real
, nsecs_real
);
464 EXPORT_SYMBOL(getnstime_raw_and_real
);
466 #endif /* CONFIG_NTP_PPS */
469 * do_gettimeofday - Returns the time of day in a timeval
470 * @tv: pointer to the timeval to be set
472 * NOTE: Users should be converted to using getnstimeofday()
474 void do_gettimeofday(struct timeval
*tv
)
478 getnstimeofday(&now
);
479 tv
->tv_sec
= now
.tv_sec
;
480 tv
->tv_usec
= now
.tv_nsec
/1000;
482 EXPORT_SYMBOL(do_gettimeofday
);
485 * do_settimeofday - Sets the time of day
486 * @tv: pointer to the timespec variable containing the new time
488 * Sets the time of day to the new time and update NTP and notify hrtimers
490 int do_settimeofday(const struct timespec
*tv
)
492 struct timekeeper
*tk
= &timekeeper
;
493 struct timespec ts_delta
, xt
;
496 if (!timespec_valid_strict(tv
))
499 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
500 write_seqcount_begin(&timekeeper_seq
);
502 timekeeping_forward_now(tk
);
505 ts_delta
.tv_sec
= tv
->tv_sec
- xt
.tv_sec
;
506 ts_delta
.tv_nsec
= tv
->tv_nsec
- xt
.tv_nsec
;
508 tk_set_wall_to_mono(tk
, timespec_sub(tk
->wall_to_monotonic
, ts_delta
));
510 tk_set_xtime(tk
, tv
);
512 timekeeping_update(tk
, TK_CLEAR_NTP
| TK_MIRROR
| TK_CLOCK_WAS_SET
);
514 write_seqcount_end(&timekeeper_seq
);
515 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
517 /* signal hrtimers about time change */
522 EXPORT_SYMBOL(do_settimeofday
);
525 * timekeeping_inject_offset - Adds or subtracts from the current time.
526 * @tv: pointer to the timespec variable containing the offset
528 * Adds or subtracts an offset value from the current time.
530 int timekeeping_inject_offset(struct timespec
*ts
)
532 struct timekeeper
*tk
= &timekeeper
;
537 if ((unsigned long)ts
->tv_nsec
>= NSEC_PER_SEC
)
540 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
541 write_seqcount_begin(&timekeeper_seq
);
543 timekeeping_forward_now(tk
);
545 /* Make sure the proposed value is valid */
546 tmp
= timespec_add(tk_xtime(tk
), *ts
);
547 if (!timespec_valid_strict(&tmp
)) {
552 tk_xtime_add(tk
, ts
);
553 tk_set_wall_to_mono(tk
, timespec_sub(tk
->wall_to_monotonic
, *ts
));
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
);
558 write_seqcount_end(&timekeeper_seq
);
559 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
561 /* signal hrtimers about time change */
566 EXPORT_SYMBOL(timekeeping_inject_offset
);
570 * timekeeping_get_tai_offset - Returns current TAI offset from UTC
573 s32
timekeeping_get_tai_offset(void)
575 struct timekeeper
*tk
= &timekeeper
;
580 seq
= read_seqcount_begin(&timekeeper_seq
);
581 ret
= tk
->tai_offset
;
582 } while (read_seqcount_retry(&timekeeper_seq
, seq
));
588 * __timekeeping_set_tai_offset - Lock free worker function
591 static void __timekeeping_set_tai_offset(struct timekeeper
*tk
, s32 tai_offset
)
593 tk
->tai_offset
= tai_offset
;
594 tk
->offs_tai
= ktime_add(tk
->offs_real
, ktime_set(tai_offset
, 0));
598 * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
601 void timekeeping_set_tai_offset(s32 tai_offset
)
603 struct timekeeper
*tk
= &timekeeper
;
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
);
616 * change_clocksource - Swaps clocksources if a new one is available
618 * Accumulates current time interval and initializes new clocksource
620 static int change_clocksource(void *data
)
622 struct timekeeper
*tk
= &timekeeper
;
623 struct clocksource
*new, *old
;
626 new = (struct clocksource
*) data
;
628 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
629 write_seqcount_begin(&timekeeper_seq
);
631 timekeeping_forward_now(tk
);
633 * If the cs is in module, get a module reference. Succeeds
634 * for built-in code (owner == NULL) as well.
636 if (try_module_get(new->owner
)) {
637 if (!new->enable
|| new->enable(new) == 0) {
639 tk_setup_internals(tk
, new);
642 module_put(old
->owner
);
644 module_put(new->owner
);
647 timekeeping_update(tk
, TK_CLEAR_NTP
| TK_MIRROR
| TK_CLOCK_WAS_SET
);
649 write_seqcount_end(&timekeeper_seq
);
650 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
656 * timekeeping_notify - Install a new clock source
657 * @clock: pointer to the clock source
659 * This function is called from clocksource.c after a new, better clock
660 * source has been registered. The caller holds the clocksource_mutex.
662 int timekeeping_notify(struct clocksource
*clock
)
664 struct timekeeper
*tk
= &timekeeper
;
666 if (tk
->clock
== clock
)
668 stop_machine(change_clocksource
, clock
, NULL
);
670 return tk
->clock
== clock
? 0 : -1;
674 * ktime_get_real - get the real (wall-) time in ktime_t format
676 * returns the time in ktime_t format
678 ktime_t
ktime_get_real(void)
682 getnstimeofday(&now
);
684 return timespec_to_ktime(now
);
686 EXPORT_SYMBOL_GPL(ktime_get_real
);
689 * getrawmonotonic - Returns the raw monotonic time in a timespec
690 * @ts: pointer to the timespec to be set
692 * Returns the raw monotonic time (completely un-modified by ntp)
694 void getrawmonotonic(struct timespec
*ts
)
696 struct timekeeper
*tk
= &timekeeper
;
701 seq
= read_seqcount_begin(&timekeeper_seq
);
702 nsecs
= timekeeping_get_ns_raw(tk
);
705 } while (read_seqcount_retry(&timekeeper_seq
, seq
));
707 timespec_add_ns(ts
, nsecs
);
709 EXPORT_SYMBOL(getrawmonotonic
);
712 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
714 int timekeeping_valid_for_hres(void)
716 struct timekeeper
*tk
= &timekeeper
;
721 seq
= read_seqcount_begin(&timekeeper_seq
);
723 ret
= tk
->clock
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
;
725 } while (read_seqcount_retry(&timekeeper_seq
, seq
));
731 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
733 u64
timekeeping_max_deferment(void)
735 struct timekeeper
*tk
= &timekeeper
;
740 seq
= read_seqcount_begin(&timekeeper_seq
);
742 ret
= tk
->clock
->max_idle_ns
;
744 } while (read_seqcount_retry(&timekeeper_seq
, seq
));
750 * read_persistent_clock - Return time from the persistent clock.
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.
756 * XXX - Do be sure to remove it once all arches implement it.
758 void __weak
read_persistent_clock(struct timespec
*ts
)
765 * read_boot_clock - Return time of the system start.
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.
771 * XXX - Do be sure to remove it once all arches implement it.
773 void __weak
read_boot_clock(struct timespec
*ts
)
780 * timekeeping_init - Initializes the clocksource and common timekeeping values
782 void __init
timekeeping_init(void)
784 struct timekeeper
*tk
= &timekeeper
;
785 struct clocksource
*clock
;
787 struct timespec now
, boot
, tmp
;
789 read_persistent_clock(&now
);
791 if (!timespec_valid_strict(&now
)) {
792 pr_warn("WARNING: Persistent clock returned invalid value!\n"
793 " Check your CMOS/BIOS settings.\n");
796 } else if (now
.tv_sec
|| now
.tv_nsec
)
797 persistent_clock_exist
= true;
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");
807 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
808 write_seqcount_begin(&timekeeper_seq
);
811 clock
= clocksource_default_clock();
813 clock
->enable(clock
);
814 tk_setup_internals(tk
, clock
);
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)
822 set_normalized_timespec(&tmp
, -boot
.tv_sec
, -boot
.tv_nsec
);
823 tk_set_wall_to_mono(tk
, tmp
);
827 tk_set_sleep_time(tk
, tmp
);
829 memcpy(&shadow_timekeeper
, &timekeeper
, sizeof(timekeeper
));
831 write_seqcount_end(&timekeeper_seq
);
832 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
835 /* time in seconds when suspend began */
836 static struct timespec timekeeping_suspend_time
;
839 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
840 * @delta: pointer to a timespec delta value
842 * Takes a timespec offset measuring a suspend interval and properly
843 * adds the sleep offset to the timekeeping variables.
845 static void __timekeeping_inject_sleeptime(struct timekeeper
*tk
,
846 struct timespec
*delta
)
848 if (!timespec_valid_strict(delta
)) {
849 printk_deferred(KERN_WARNING
850 "__timekeeping_inject_sleeptime: Invalid "
851 "sleep delta value!\n");
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
);
861 * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
862 * @delta: pointer to a timespec delta value
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.
867 * This function should only be called by rtc_resume(), and allows
868 * a suspend offset to be injected into the timekeeping values.
870 void timekeeping_inject_sleeptime(struct timespec
*delta
)
872 struct timekeeper
*tk
= &timekeeper
;
876 * Make sure we don't set the clock twice, as timekeeping_resume()
879 if (has_persistent_clock())
882 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
883 write_seqcount_begin(&timekeeper_seq
);
885 timekeeping_forward_now(tk
);
887 __timekeeping_inject_sleeptime(tk
, delta
);
889 timekeeping_update(tk
, TK_CLEAR_NTP
| TK_MIRROR
| TK_CLOCK_WAS_SET
);
891 write_seqcount_end(&timekeeper_seq
);
892 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
894 /* signal hrtimers about time change */
899 * timekeeping_resume - Resumes the generic timekeeping subsystem.
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.
905 static void timekeeping_resume(void)
907 struct timekeeper
*tk
= &timekeeper
;
908 struct clocksource
*clock
= tk
->clock
;
910 struct timespec ts_new
, ts_delta
;
911 cycle_t cycle_now
, cycle_delta
;
912 bool suspendtime_found
= false;
914 read_persistent_clock(&ts_new
);
916 clockevents_resume();
917 clocksource_resume();
919 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
920 write_seqcount_begin(&timekeeper_seq
);
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
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.
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
;
942 cycle_delta
= (cycle_now
- clock
->cycle_last
) & clock
->mask
;
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
950 if (cycle_delta
> max
) {
951 num
= div64_u64(cycle_delta
, max
);
952 nsec
= (((u64
) max
* mult
) >> shift
) * num
;
953 cycle_delta
-= num
* max
;
955 nsec
+= ((u64
) cycle_delta
* mult
) >> shift
;
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;
964 if (suspendtime_found
)
965 __timekeeping_inject_sleeptime(tk
, &ts_delta
);
967 /* Re-base the last cycle value */
968 tk
->cycle_last
= clock
->cycle_last
= cycle_now
;
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
);
975 touch_softlockup_watchdog();
977 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME
, NULL
);
979 /* Resume hrtimers */
983 static int timekeeping_suspend(void)
985 struct timekeeper
*tk
= &timekeeper
;
987 struct timespec delta
, delta_delta
;
988 static struct timespec old_delta
;
990 read_persistent_clock(&timekeeping_suspend_time
);
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.
997 if (timekeeping_suspend_time
.tv_sec
|| timekeeping_suspend_time
.tv_nsec
)
998 persistent_clock_exist
= true;
1000 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
1001 write_seqcount_begin(&timekeeper_seq
);
1002 timekeeping_forward_now(tk
);
1003 timekeeping_suspended
= 1;
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.
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) {
1015 * if delta_delta is too large, assume time correction
1016 * has occured and set old_delta to the current delta.
1020 /* Otherwise try to adjust old_system to compensate */
1021 timekeeping_suspend_time
=
1022 timespec_add(timekeeping_suspend_time
, delta_delta
);
1025 timekeeping_update(tk
, TK_MIRROR
);
1026 write_seqcount_end(&timekeeper_seq
);
1027 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
1029 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND
, NULL
);
1030 clocksource_suspend();
1031 clockevents_suspend();
1036 /* sysfs resume/suspend bits for timekeeping */
1037 static struct syscore_ops timekeeping_syscore_ops
= {
1038 .resume
= timekeeping_resume
,
1039 .suspend
= timekeeping_suspend
,
1042 static int __init
timekeeping_init_ops(void)
1044 register_syscore_ops(&timekeeping_syscore_ops
);
1048 device_initcall(timekeeping_init_ops
);
1051 * If the error is already larger, we look ahead even further
1052 * to compensate for late or lost adjustments.
1054 static __always_inline
int timekeeping_bigadjust(struct timekeeper
*tk
,
1055 s64 error
, s64
*interval
,
1059 u32 look_ahead
, adj
;
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).
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
++)
1077 * Now calculate the error in (1 << look_ahead) ticks, but first
1078 * remove the single look ahead already included in the error.
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
;
1084 /* Finally calculate the adjustment shift value. */
1089 *interval
= -*interval
;
1093 for (adj
= 0; error
> i
; adj
++)
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.
1106 static void timekeeping_adjust(struct timekeeper
*tk
, s64 offset
)
1108 s64 error
, interval
= tk
->cycle_interval
;
1112 * The point of this is to check if the error is greater than half
1115 * First we shift it down from NTP_SHIFT to clocksource->shifted nsecs.
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.
1122 * Note: It does not "save" on aggravation when reading the code.
1124 error
= tk
->ntp_error
>> (tk
->ntp_error_shift
- 1);
1125 if (error
> interval
) {
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.
1133 if (likely(error
<= interval
))
1136 adj
= timekeeping_bigadjust(tk
, error
, &interval
, &offset
);
1138 if (error
< -interval
) {
1139 /* See comment above, this is just switched for the negative */
1141 if (likely(error
>= -interval
)) {
1143 interval
= -interval
;
1146 adj
= timekeeping_bigadjust(tk
, error
, &interval
, &offset
);
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
);
1161 * So the following can be confusing.
1163 * To keep things simple, lets assume adj == 1 for now.
1165 * When adj != 1, remember that the interval and offset values
1166 * have been appropriately scaled so the math is the same.
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)
1175 * xtime_interval = (cycle_interval * mult) + cycle_interval
1176 * Which can be shortened to:
1177 * xtime_interval += cycle_interval
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.
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
1191 * (offset * adj_1) + xtime_nsec_1 =
1192 * (offset * adj_2) + xtime_nsec_2
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
1203 * xtime_nsec_2 = xtime_nsec_1 - offset
1204 * Which simplfies to:
1205 * xtime_nsec -= offset
1207 * XXX - TODO: Doc ntp_error calculation.
1210 tk
->xtime_interval
+= interval
;
1211 tk
->xtime_nsec
-= offset
;
1212 tk
->ntp_error
-= (interval
- offset
) << tk
->ntp_error_shift
;
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.
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.
1226 * We'll correct this error next time through this function, when
1227 * xtime_nsec is not as small.
1229 if (unlikely((s64
)tk
->xtime_nsec
< 0)) {
1230 s64 neg
= -(s64
)tk
->xtime_nsec
;
1232 tk
->ntp_error
+= neg
<< tk
->ntp_error_shift
;
1238 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
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.
1245 static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper
*tk
)
1247 u64 nsecps
= (u64
)NSEC_PER_SEC
<< tk
->shift
;
1248 unsigned int clock_set
= 0;
1250 while (tk
->xtime_nsec
>= nsecps
) {
1253 tk
->xtime_nsec
-= nsecps
;
1256 /* Figure out if its a leap sec and apply if needed */
1257 leap
= second_overflow(tk
->xtime_sec
);
1258 if (unlikely(leap
)) {
1261 tk
->xtime_sec
+= leap
;
1265 tk_set_wall_to_mono(tk
,
1266 timespec_sub(tk
->wall_to_monotonic
, ts
));
1268 __timekeeping_set_tai_offset(tk
, tk
->tai_offset
- leap
);
1270 clock_set
= TK_CLOCK_WAS_SET
;
1277 * logarithmic_accumulation - shifted accumulation of cycles
1279 * This functions accumulates a shifted interval of cycles into
1280 * into a shifted interval nanoseconds. Allows for O(log) accumulation
1283 * Returns the unconsumed cycles.
1285 static cycle_t
logarithmic_accumulation(struct timekeeper
*tk
, cycle_t offset
,
1287 unsigned int *clock_set
)
1289 cycle_t interval
= tk
->cycle_interval
<< shift
;
1292 /* If the offset is smaller then a shifted interval, do nothing */
1293 if (offset
< interval
)
1296 /* Accumulate one shifted interval */
1298 tk
->cycle_last
+= interval
;
1300 tk
->xtime_nsec
+= tk
->xtime_interval
<< shift
;
1301 *clock_set
|= accumulate_nsecs_to_secs(tk
);
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
;
1311 tk
->raw_time
.tv_nsec
= raw_nsecs
;
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
);
1321 #ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
1322 static inline void old_vsyscall_fixup(struct timekeeper
*tk
)
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.
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
;
1343 #define old_vsyscall_fixup(tk)
1349 * update_wall_time - Uses the current clocksource to increment the wall time
1352 void update_wall_time(void)
1354 struct clocksource
*clock
;
1355 struct timekeeper
*real_tk
= &timekeeper
;
1356 struct timekeeper
*tk
= &shadow_timekeeper
;
1358 int shift
= 0, maxshift
;
1359 unsigned int clock_set
= 0;
1360 unsigned long flags
;
1362 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
1364 /* Make sure we're fully resumed: */
1365 if (unlikely(timekeeping_suspended
))
1368 clock
= real_tk
->clock
;
1370 #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
1371 offset
= real_tk
->cycle_interval
;
1373 offset
= (clock
->read(clock
) - clock
->cycle_last
) & clock
->mask
;
1376 /* Check if there's really nothing to do */
1377 if (offset
< real_tk
->cycle_interval
)
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
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
,
1396 if (offset
< tk
->cycle_interval
<<shift
)
1400 /* correct the clock when NTP error is too big */
1401 timekeeping_adjust(tk
, offset
);
1404 * XXX This can be killed once everyone converts
1405 * to the new update_vsyscall.
1407 old_vsyscall_fixup(tk
);
1410 * Finally, make sure that after the rounding
1411 * xtime_nsec isn't larger than NSEC_PER_SEC
1413 clock_set
|= accumulate_nsecs_to_secs(tk
);
1415 write_seqcount_begin(&timekeeper_seq
);
1416 /* Update clock->cycle_last with the new value */
1417 clock
->cycle_last
= tk
->cycle_last
;
1419 * Update the real timekeeper.
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
1428 memcpy(real_tk
, tk
, sizeof(*tk
));
1429 timekeeping_update(real_tk
, clock_set
);
1430 write_seqcount_end(&timekeeper_seq
);
1432 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
1434 /* Have to call _delayed version, since in irq context*/
1435 clock_was_set_delayed();
1439 * getboottime - Return the real time of system boot.
1440 * @ts: pointer to the timespec to be set
1442 * Returns the wall-time of boot in a timespec.
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).
1449 void getboottime(struct timespec
*ts
)
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
1459 set_normalized_timespec(ts
, -boottime
.tv_sec
, -boottime
.tv_nsec
);
1461 EXPORT_SYMBOL_GPL(getboottime
);
1464 * get_monotonic_boottime - Returns monotonic time since boot
1465 * @ts: pointer to the timespec to be set
1467 * Returns the monotonic time since boot in a timespec.
1469 * This is similar to CLOCK_MONTONIC/ktime_get_ts, but also
1470 * includes the time spent in suspend.
1472 void get_monotonic_boottime(struct timespec
*ts
)
1474 struct timekeeper
*tk
= &timekeeper
;
1475 struct timespec tomono
, sleep
;
1479 WARN_ON(timekeeping_suspended
);
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
;
1488 } while (read_seqcount_retry(&timekeeper_seq
, seq
));
1490 ts
->tv_sec
+= tomono
.tv_sec
+ sleep
.tv_sec
;
1492 timespec_add_ns(ts
, nsec
+ tomono
.tv_nsec
+ sleep
.tv_nsec
);
1494 EXPORT_SYMBOL_GPL(get_monotonic_boottime
);
1497 * ktime_get_boottime - Returns monotonic time since boot in a ktime
1499 * Returns the monotonic time since boot in a ktime
1501 * This is similar to CLOCK_MONTONIC/ktime_get, but also
1502 * includes the time spent in suspend.
1504 ktime_t
ktime_get_boottime(void)
1508 get_monotonic_boottime(&ts
);
1509 return timespec_to_ktime(ts
);
1511 EXPORT_SYMBOL_GPL(ktime_get_boottime
);
1514 * monotonic_to_bootbased - Convert the monotonic time to boot based.
1515 * @ts: pointer to the timespec to be converted
1517 void monotonic_to_bootbased(struct timespec
*ts
)
1519 struct timekeeper
*tk
= &timekeeper
;
1521 *ts
= timespec_add(*ts
, tk
->total_sleep_time
);
1523 EXPORT_SYMBOL_GPL(monotonic_to_bootbased
);
1525 unsigned long get_seconds(void)
1527 struct timekeeper
*tk
= &timekeeper
;
1529 return tk
->xtime_sec
;
1531 EXPORT_SYMBOL(get_seconds
);
1533 struct timespec
__current_kernel_time(void)
1535 struct timekeeper
*tk
= &timekeeper
;
1537 return tk_xtime(tk
);
1540 struct timespec
current_kernel_time(void)
1542 struct timekeeper
*tk
= &timekeeper
;
1543 struct timespec now
;
1547 seq
= read_seqcount_begin(&timekeeper_seq
);
1550 } while (read_seqcount_retry(&timekeeper_seq
, seq
));
1554 EXPORT_SYMBOL(current_kernel_time
);
1556 struct timespec
get_monotonic_coarse(void)
1558 struct timekeeper
*tk
= &timekeeper
;
1559 struct timespec now
, mono
;
1563 seq
= read_seqcount_begin(&timekeeper_seq
);
1566 mono
= tk
->wall_to_monotonic
;
1567 } while (read_seqcount_retry(&timekeeper_seq
, seq
));
1569 set_normalized_timespec(&now
, now
.tv_sec
+ mono
.tv_sec
,
1570 now
.tv_nsec
+ mono
.tv_nsec
);
1575 * Must hold jiffies_lock
1577 void do_timer(unsigned long ticks
)
1579 jiffies_64
+= ticks
;
1580 calc_global_load(ticks
);
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
1589 * Returns monotonic time at last tick and various offsets
1591 ktime_t
ktime_get_update_offsets_tick(ktime_t
*offs_real
, ktime_t
*offs_boot
,
1594 struct timekeeper
*tk
= &timekeeper
;
1600 seq
= read_seqcount_begin(&timekeeper_seq
);
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
));
1609 now
= ktime_set(ts
.tv_sec
, ts
.tv_nsec
);
1610 now
= ktime_sub(now
, *offs_real
);
1614 #ifdef CONFIG_HIGH_RES_TIMERS
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
1621 * Returns current monotonic time and updates the offsets
1622 * Called from hrtimer_interrupt() or retrigger_next_event()
1624 ktime_t
ktime_get_update_offsets_now(ktime_t
*offs_real
, ktime_t
*offs_boot
,
1627 struct timekeeper
*tk
= &timekeeper
;
1633 seq
= read_seqcount_begin(&timekeeper_seq
);
1635 secs
= tk
->xtime_sec
;
1636 nsecs
= timekeeping_get_ns(tk
);
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
));
1643 now
= ktime_add_ns(ktime_set(secs
, 0), nsecs
);
1644 now
= ktime_sub(now
, *offs_real
);
1650 * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
1652 ktime_t
ktime_get_monotonic_offset(void)
1654 struct timekeeper
*tk
= &timekeeper
;
1656 struct timespec wtom
;
1659 seq
= read_seqcount_begin(&timekeeper_seq
);
1660 wtom
= tk
->wall_to_monotonic
;
1661 } while (read_seqcount_retry(&timekeeper_seq
, seq
));
1663 return timespec_to_ktime(wtom
);
1665 EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset
);
1668 * do_adjtimex() - Accessor function to NTP __do_adjtimex function
1670 int do_adjtimex(struct timex
*txc
)
1672 struct timekeeper
*tk
= &timekeeper
;
1673 unsigned long flags
;
1678 /* Validate the data before disabling interrupts */
1679 ret
= ntp_validate_timex(txc
);
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
);
1694 getnstimeofday(&ts
);
1696 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
1697 write_seqcount_begin(&timekeeper_seq
);
1699 orig_tai
= tai
= tk
->tai_offset
;
1700 ret
= __do_adjtimex(txc
, &ts
, &tai
);
1702 if (tai
!= orig_tai
) {
1703 __timekeeping_set_tai_offset(tk
, tai
);
1704 timekeeping_update(tk
, TK_MIRROR
| TK_CLOCK_WAS_SET
);
1706 write_seqcount_end(&timekeeper_seq
);
1707 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
1709 if (tai
!= orig_tai
)
1712 ntp_notify_cmos_timer();
1717 #ifdef CONFIG_NTP_PPS
1719 * hardpps() - Accessor function to NTP __hardpps function
1721 void hardpps(const struct timespec
*phase_ts
, const struct timespec
*raw_ts
)
1723 unsigned long flags
;
1725 raw_spin_lock_irqsave(&timekeeper_lock
, flags
);
1726 write_seqcount_begin(&timekeeper_seq
);
1728 __hardpps(phase_ts
, raw_ts
);
1730 write_seqcount_end(&timekeeper_seq
);
1731 raw_spin_unlock_irqrestore(&timekeeper_lock
, flags
);
1733 EXPORT_SYMBOL(hardpps
);
1737 * xtime_update() - advances the timekeeping infrastructure
1738 * @ticks: number of ticks, that have elapsed since the last call.
1740 * Must be called with interrupts disabled.
1742 void xtime_update(unsigned long ticks
)
1744 write_seqlock(&jiffies_lock
);
1746 write_sequnlock(&jiffies_lock
);