blob: c5feb7aa3acb60ca60c7a81f199da43e1ff18caf [file] [log] [blame]
john stultz85240702007-05-08 00:27:59 -07001/*
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
John Stultzd7b42022012-09-04 15:12:07 -040011#include <linux/timekeeper_internal.h>
john stultz85240702007-05-08 00:27:59 -070012#include <linux/module.h>
13#include <linux/interrupt.h>
14#include <linux/percpu.h>
15#include <linux/init.h>
16#include <linux/mm.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040017#include <linux/sched.h>
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +010018#include <linux/syscore_ops.h>
john stultz85240702007-05-08 00:27:59 -070019#include <linux/clocksource.h>
20#include <linux/jiffies.h>
21#include <linux/time.h>
22#include <linux/tick.h>
Martin Schwidefsky75c51582009-08-14 15:47:30 +020023#include <linux/stop_machine.h>
Marcelo Tosattie0b306f2012-11-27 23:28:59 -020024#include <linux/pvclock_gtod.h>
john stultz85240702007-05-08 00:27:59 -070025
Thomas Gleixnereb93e4d2013-02-21 22:51:36 +000026#include "tick-internal.h"
Martin Schwidefsky155ec602009-08-14 15:47:26 +020027
H Hartley Sweetenafa14e72011-01-11 17:59:38 -060028static struct timekeeper timekeeper;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +000029static DEFINE_RAW_SPINLOCK(timekeeper_lock);
30static seqcount_t timekeeper_seq;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020031
John Stultz8fcce542011-11-14 11:46:39 -080032/* flag for if timekeeping is suspended */
33int __read_mostly timekeeping_suspended;
34
Feng Tang31ade302013-01-16 00:09:47 +080035/* Flag for if there is a persistent clock on this platform */
36bool __read_mostly persistent_clock_exist = false;
37
John Stultz1e75fa82012-07-13 01:21:53 -040038static inline void tk_normalize_xtime(struct timekeeper *tk)
39{
40 while (tk->xtime_nsec >= ((u64)NSEC_PER_SEC << tk->shift)) {
41 tk->xtime_nsec -= (u64)NSEC_PER_SEC << tk->shift;
42 tk->xtime_sec++;
43 }
44}
John Stultz8fcce542011-11-14 11:46:39 -080045
John Stultz1e75fa82012-07-13 01:21:53 -040046static void tk_set_xtime(struct timekeeper *tk, const struct timespec *ts)
47{
48 tk->xtime_sec = ts->tv_sec;
John Stultzb44d50d2012-07-23 16:22:37 -040049 tk->xtime_nsec = (u64)ts->tv_nsec << tk->shift;
John Stultz1e75fa82012-07-13 01:21:53 -040050}
51
52static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts)
53{
54 tk->xtime_sec += ts->tv_sec;
John Stultzb44d50d2012-07-23 16:22:37 -040055 tk->xtime_nsec += (u64)ts->tv_nsec << tk->shift;
John Stultz784ffcb2012-08-21 20:30:46 -040056 tk_normalize_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -040057}
John Stultz8fcce542011-11-14 11:46:39 -080058
John Stultz6d0ef902012-07-27 14:48:12 -040059static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec wtm)
60{
61 struct timespec tmp;
62
63 /*
64 * Verify consistency of: offset_real = -wall_to_monotonic
65 * before modifying anything
66 */
67 set_normalized_timespec(&tmp, -tk->wall_to_monotonic.tv_sec,
68 -tk->wall_to_monotonic.tv_nsec);
69 WARN_ON_ONCE(tk->offs_real.tv64 != timespec_to_ktime(tmp).tv64);
70 tk->wall_to_monotonic = wtm;
71 set_normalized_timespec(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
72 tk->offs_real = timespec_to_ktime(tmp);
John Stultz90adda92013-01-21 17:00:11 -080073 tk->offs_tai = ktime_sub(tk->offs_real, ktime_set(tk->tai_offset, 0));
John Stultz6d0ef902012-07-27 14:48:12 -040074}
75
76static void tk_set_sleep_time(struct timekeeper *tk, struct timespec t)
77{
78 /* Verify consistency before modifying */
79 WARN_ON_ONCE(tk->offs_boot.tv64 != timespec_to_ktime(tk->total_sleep_time).tv64);
80
81 tk->total_sleep_time = t;
82 tk->offs_boot = timespec_to_ktime(t);
83}
84
Martin Schwidefsky155ec602009-08-14 15:47:26 +020085/**
86 * timekeeper_setup_internals - Set up internals to use clocksource clock.
87 *
88 * @clock: Pointer to clocksource.
89 *
90 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
91 * pair and interval request.
92 *
93 * Unless you're the timekeeping code, you should not be using this!
94 */
John Stultzf726a692012-07-13 01:21:57 -040095static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
Martin Schwidefsky155ec602009-08-14 15:47:26 +020096{
97 cycle_t interval;
Kasper Pedersena386b5a2010-10-20 15:55:15 -070098 u64 tmp, ntpinterval;
John Stultz1e75fa82012-07-13 01:21:53 -040099 struct clocksource *old_clock;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200100
John Stultzf726a692012-07-13 01:21:57 -0400101 old_clock = tk->clock;
102 tk->clock = clock;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200103 clock->cycle_last = clock->read(clock);
104
105 /* Do the ns -> cycle conversion first, using original mult */
106 tmp = NTP_INTERVAL_LENGTH;
107 tmp <<= clock->shift;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700108 ntpinterval = tmp;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200109 tmp += clock->mult/2;
110 do_div(tmp, clock->mult);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200111 if (tmp == 0)
112 tmp = 1;
113
114 interval = (cycle_t) tmp;
John Stultzf726a692012-07-13 01:21:57 -0400115 tk->cycle_interval = interval;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200116
117 /* Go back from cycles -> shifted ns */
John Stultzf726a692012-07-13 01:21:57 -0400118 tk->xtime_interval = (u64) interval * clock->mult;
119 tk->xtime_remainder = ntpinterval - tk->xtime_interval;
120 tk->raw_interval =
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200121 ((u64) interval * clock->mult) >> clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200122
John Stultz1e75fa82012-07-13 01:21:53 -0400123 /* if changing clocks, convert xtime_nsec shift units */
124 if (old_clock) {
125 int shift_change = clock->shift - old_clock->shift;
126 if (shift_change < 0)
John Stultzf726a692012-07-13 01:21:57 -0400127 tk->xtime_nsec >>= -shift_change;
John Stultz1e75fa82012-07-13 01:21:53 -0400128 else
John Stultzf726a692012-07-13 01:21:57 -0400129 tk->xtime_nsec <<= shift_change;
John Stultz1e75fa82012-07-13 01:21:53 -0400130 }
John Stultzf726a692012-07-13 01:21:57 -0400131 tk->shift = clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200132
John Stultzf726a692012-07-13 01:21:57 -0400133 tk->ntp_error = 0;
134 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200135
136 /*
137 * The timekeeper keeps its own mult values for the currently
138 * active clocksource. These value will be adjusted via NTP
139 * to counteract clock drifting.
140 */
John Stultzf726a692012-07-13 01:21:57 -0400141 tk->mult = clock->mult;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200142}
john stultz85240702007-05-08 00:27:59 -0700143
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200144/* Timekeeper helper functions. */
Stephen Warren7b1f6202012-11-07 17:58:54 -0700145
146#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
147u32 (*arch_gettimeoffset)(void);
148
149u32 get_arch_timeoffset(void)
150{
151 if (likely(arch_gettimeoffset))
152 return arch_gettimeoffset();
153 return 0;
154}
155#else
156static inline u32 get_arch_timeoffset(void) { return 0; }
157#endif
158
John Stultzf726a692012-07-13 01:21:57 -0400159static inline s64 timekeeping_get_ns(struct timekeeper *tk)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200160{
161 cycle_t cycle_now, cycle_delta;
162 struct clocksource *clock;
John Stultz1e75fa82012-07-13 01:21:53 -0400163 s64 nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200164
165 /* read clocksource: */
John Stultzf726a692012-07-13 01:21:57 -0400166 clock = tk->clock;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200167 cycle_now = clock->read(clock);
168
169 /* calculate the delta since the last update_wall_time: */
170 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
171
John Stultzf726a692012-07-13 01:21:57 -0400172 nsec = cycle_delta * tk->mult + tk->xtime_nsec;
173 nsec >>= tk->shift;
John Stultzf2a5a082012-07-13 01:21:55 -0400174
Stephen Warren7b1f6202012-11-07 17:58:54 -0700175 /* If arch requires, add in get_arch_timeoffset() */
176 return nsec + get_arch_timeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200177}
178
John Stultzf726a692012-07-13 01:21:57 -0400179static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200180{
181 cycle_t cycle_now, cycle_delta;
182 struct clocksource *clock;
John Stultzf2a5a082012-07-13 01:21:55 -0400183 s64 nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200184
185 /* read clocksource: */
John Stultzf726a692012-07-13 01:21:57 -0400186 clock = tk->clock;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200187 cycle_now = clock->read(clock);
188
189 /* calculate the delta since the last update_wall_time: */
190 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
191
John Stultzf2a5a082012-07-13 01:21:55 -0400192 /* convert delta to nanoseconds. */
193 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
194
Stephen Warren7b1f6202012-11-07 17:58:54 -0700195 /* If arch requires, add in get_arch_timeoffset() */
196 return nsec + get_arch_timeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200197}
198
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200199static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
200
201static void update_pvclock_gtod(struct timekeeper *tk)
202{
203 raw_notifier_call_chain(&pvclock_gtod_chain, 0, tk);
204}
205
206/**
207 * pvclock_gtod_register_notifier - register a pvclock timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200208 */
209int pvclock_gtod_register_notifier(struct notifier_block *nb)
210{
211 struct timekeeper *tk = &timekeeper;
212 unsigned long flags;
213 int ret;
214
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000215 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200216 ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200217 update_pvclock_gtod(tk);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000218 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200219
220 return ret;
221}
222EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
223
224/**
225 * pvclock_gtod_unregister_notifier - unregister a pvclock
226 * timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200227 */
228int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
229{
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200230 unsigned long flags;
231 int ret;
232
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000233 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200234 ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000235 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200236
237 return ret;
238}
239EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
240
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000241/* must hold timekeeper_lock */
John Stultzf726a692012-07-13 01:21:57 -0400242static void timekeeping_update(struct timekeeper *tk, bool clearntp)
Thomas Gleixnercc062682011-11-13 23:19:49 +0000243{
244 if (clearntp) {
John Stultzf726a692012-07-13 01:21:57 -0400245 tk->ntp_error = 0;
Thomas Gleixnercc062682011-11-13 23:19:49 +0000246 ntp_clear();
247 }
John Stultz576094b2012-09-11 19:58:13 -0400248 update_vsyscall(tk);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200249 update_pvclock_gtod(tk);
Thomas Gleixnercc062682011-11-13 23:19:49 +0000250}
251
john stultz85240702007-05-08 00:27:59 -0700252/**
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200253 * timekeeping_forward_now - update clock to the current time
john stultz85240702007-05-08 00:27:59 -0700254 *
Roman Zippel9a055112008-08-20 16:37:28 -0700255 * Forward the current clock to update its state since the last call to
256 * update_wall_time(). This is useful before significant clock changes,
257 * as it avoids having to deal with this time offset explicitly.
john stultz85240702007-05-08 00:27:59 -0700258 */
John Stultzf726a692012-07-13 01:21:57 -0400259static void timekeeping_forward_now(struct timekeeper *tk)
john stultz85240702007-05-08 00:27:59 -0700260{
261 cycle_t cycle_now, cycle_delta;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200262 struct clocksource *clock;
Roman Zippel9a055112008-08-20 16:37:28 -0700263 s64 nsec;
john stultz85240702007-05-08 00:27:59 -0700264
John Stultzf726a692012-07-13 01:21:57 -0400265 clock = tk->clock;
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200266 cycle_now = clock->read(clock);
john stultz85240702007-05-08 00:27:59 -0700267 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
Roman Zippel9a055112008-08-20 16:37:28 -0700268 clock->cycle_last = cycle_now;
john stultz85240702007-05-08 00:27:59 -0700269
John Stultzf726a692012-07-13 01:21:57 -0400270 tk->xtime_nsec += cycle_delta * tk->mult;
john stultz7d275582009-05-01 13:10:26 -0700271
Stephen Warren7b1f6202012-11-07 17:58:54 -0700272 /* If arch requires, add in get_arch_timeoffset() */
273 tk->xtime_nsec += (u64)get_arch_timeoffset() << tk->shift;
john stultz7d275582009-05-01 13:10:26 -0700274
John Stultzf726a692012-07-13 01:21:57 -0400275 tk_normalize_xtime(tk);
John Stultz2d422442008-08-20 16:37:30 -0700276
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200277 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
John Stultzf726a692012-07-13 01:21:57 -0400278 timespec_add_ns(&tk->raw_time, nsec);
john stultz85240702007-05-08 00:27:59 -0700279}
280
281/**
Kees Cook1e817fb2012-11-19 10:26:16 -0800282 * __getnstimeofday - Returns the time of day in a timespec.
john stultz85240702007-05-08 00:27:59 -0700283 * @ts: pointer to the timespec to be set
284 *
Kees Cook1e817fb2012-11-19 10:26:16 -0800285 * Updates the time of day in the timespec.
286 * Returns 0 on success, or -ve when suspended (timespec will be undefined).
john stultz85240702007-05-08 00:27:59 -0700287 */
Kees Cook1e817fb2012-11-19 10:26:16 -0800288int __getnstimeofday(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700289{
John Stultz4e250fd2012-07-27 14:48:13 -0400290 struct timekeeper *tk = &timekeeper;
john stultz85240702007-05-08 00:27:59 -0700291 unsigned long seq;
John Stultz1e75fa82012-07-13 01:21:53 -0400292 s64 nsecs = 0;
john stultz85240702007-05-08 00:27:59 -0700293
294 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000295 seq = read_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700296
John Stultz4e250fd2012-07-27 14:48:13 -0400297 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -0400298 nsecs = timekeeping_get_ns(tk);
john stultz85240702007-05-08 00:27:59 -0700299
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000300 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultz85240702007-05-08 00:27:59 -0700301
John Stultzec145ba2012-09-11 19:26:03 -0400302 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700303 timespec_add_ns(ts, nsecs);
Kees Cook1e817fb2012-11-19 10:26:16 -0800304
305 /*
306 * Do not bail out early, in case there were callers still using
307 * the value, even in the face of the WARN_ON.
308 */
309 if (unlikely(timekeeping_suspended))
310 return -EAGAIN;
311 return 0;
312}
313EXPORT_SYMBOL(__getnstimeofday);
314
315/**
316 * getnstimeofday - Returns the time of day in a timespec.
317 * @ts: pointer to the timespec to be set
318 *
319 * Returns the time of day in a timespec (WARN if suspended).
320 */
321void getnstimeofday(struct timespec *ts)
322{
323 WARN_ON(__getnstimeofday(ts));
john stultz85240702007-05-08 00:27:59 -0700324}
john stultz85240702007-05-08 00:27:59 -0700325EXPORT_SYMBOL(getnstimeofday);
326
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200327ktime_t ktime_get(void)
328{
John Stultz4e250fd2012-07-27 14:48:13 -0400329 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200330 unsigned int seq;
331 s64 secs, nsecs;
332
333 WARN_ON(timekeeping_suspended);
334
335 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000336 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400337 secs = tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
338 nsecs = timekeeping_get_ns(tk) + tk->wall_to_monotonic.tv_nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200339
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000340 } while (read_seqcount_retry(&timekeeper_seq, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200341 /*
342 * Use ktime_set/ktime_add_ns to create a proper ktime on
343 * 32-bit architectures without CONFIG_KTIME_SCALAR.
344 */
345 return ktime_add_ns(ktime_set(secs, 0), nsecs);
346}
347EXPORT_SYMBOL_GPL(ktime_get);
348
349/**
350 * ktime_get_ts - get the monotonic clock in timespec format
351 * @ts: pointer to timespec variable
352 *
353 * The function calculates the monotonic clock from the realtime
354 * clock and the wall_to_monotonic offset and stores the result
355 * in normalized timespec format in the variable pointed to by @ts.
356 */
357void ktime_get_ts(struct timespec *ts)
358{
John Stultz4e250fd2012-07-27 14:48:13 -0400359 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200360 struct timespec tomono;
John Stultzec145ba2012-09-11 19:26:03 -0400361 s64 nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200362 unsigned int seq;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200363
364 WARN_ON(timekeeping_suspended);
365
366 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000367 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400368 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -0400369 nsec = timekeeping_get_ns(tk);
John Stultz4e250fd2012-07-27 14:48:13 -0400370 tomono = tk->wall_to_monotonic;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200371
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000372 } while (read_seqcount_retry(&timekeeper_seq, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200373
John Stultzec145ba2012-09-11 19:26:03 -0400374 ts->tv_sec += tomono.tv_sec;
375 ts->tv_nsec = 0;
376 timespec_add_ns(ts, nsec + tomono.tv_nsec);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200377}
378EXPORT_SYMBOL_GPL(ktime_get_ts);
379
John Stultz1ff3c962012-05-03 12:43:40 -0700380
381/**
382 * timekeeping_clocktai - Returns the TAI time of day in a timespec
383 * @ts: pointer to the timespec to be set
384 *
385 * Returns the time of day in a timespec.
386 */
387void timekeeping_clocktai(struct timespec *ts)
388{
389 struct timekeeper *tk = &timekeeper;
390 unsigned long seq;
391 u64 nsecs;
392
393 WARN_ON(timekeeping_suspended);
394
395 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000396 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz1ff3c962012-05-03 12:43:40 -0700397
398 ts->tv_sec = tk->xtime_sec + tk->tai_offset;
399 nsecs = timekeeping_get_ns(tk);
400
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000401 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz1ff3c962012-05-03 12:43:40 -0700402
403 ts->tv_nsec = 0;
404 timespec_add_ns(ts, nsecs);
405
406}
407EXPORT_SYMBOL(timekeeping_clocktai);
408
409
John Stultz90adda92013-01-21 17:00:11 -0800410/**
411 * ktime_get_clocktai - Returns the TAI time of day in a ktime
412 *
413 * Returns the time of day in a ktime.
414 */
415ktime_t ktime_get_clocktai(void)
416{
417 struct timespec ts;
418
419 timekeeping_clocktai(&ts);
420 return timespec_to_ktime(ts);
421}
422EXPORT_SYMBOL(ktime_get_clocktai);
423
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800424#ifdef CONFIG_NTP_PPS
425
426/**
427 * getnstime_raw_and_real - get day and raw monotonic time in timespec format
428 * @ts_raw: pointer to the timespec to be set to raw monotonic time
429 * @ts_real: pointer to the timespec to be set to the time of day
430 *
431 * This function reads both the time of day and raw monotonic time at the
432 * same time atomically and stores the resulting timestamps in timespec
433 * format.
434 */
435void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
436{
John Stultz4e250fd2012-07-27 14:48:13 -0400437 struct timekeeper *tk = &timekeeper;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800438 unsigned long seq;
439 s64 nsecs_raw, nsecs_real;
440
441 WARN_ON_ONCE(timekeeping_suspended);
442
443 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000444 seq = read_seqcount_begin(&timekeeper_seq);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800445
John Stultz4e250fd2012-07-27 14:48:13 -0400446 *ts_raw = tk->raw_time;
447 ts_real->tv_sec = tk->xtime_sec;
John Stultz1e75fa82012-07-13 01:21:53 -0400448 ts_real->tv_nsec = 0;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800449
John Stultz4e250fd2012-07-27 14:48:13 -0400450 nsecs_raw = timekeeping_get_ns_raw(tk);
451 nsecs_real = timekeeping_get_ns(tk);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800452
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000453 } while (read_seqcount_retry(&timekeeper_seq, seq));
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800454
455 timespec_add_ns(ts_raw, nsecs_raw);
456 timespec_add_ns(ts_real, nsecs_real);
457}
458EXPORT_SYMBOL(getnstime_raw_and_real);
459
460#endif /* CONFIG_NTP_PPS */
461
john stultz85240702007-05-08 00:27:59 -0700462/**
463 * do_gettimeofday - Returns the time of day in a timeval
464 * @tv: pointer to the timeval to be set
465 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100466 * NOTE: Users should be converted to using getnstimeofday()
john stultz85240702007-05-08 00:27:59 -0700467 */
468void do_gettimeofday(struct timeval *tv)
469{
470 struct timespec now;
471
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100472 getnstimeofday(&now);
john stultz85240702007-05-08 00:27:59 -0700473 tv->tv_sec = now.tv_sec;
474 tv->tv_usec = now.tv_nsec/1000;
475}
john stultz85240702007-05-08 00:27:59 -0700476EXPORT_SYMBOL(do_gettimeofday);
Richard Cochrand239f492012-04-27 10:12:42 +0200477
john stultz85240702007-05-08 00:27:59 -0700478/**
479 * do_settimeofday - Sets the time of day
480 * @tv: pointer to the timespec variable containing the new time
481 *
482 * Sets the time of day to the new time and update NTP and notify hrtimers
483 */
Richard Cochran1e6d7672011-02-01 13:50:58 +0000484int do_settimeofday(const struct timespec *tv)
john stultz85240702007-05-08 00:27:59 -0700485{
John Stultz4e250fd2012-07-27 14:48:13 -0400486 struct timekeeper *tk = &timekeeper;
John Stultz1e75fa82012-07-13 01:21:53 -0400487 struct timespec ts_delta, xt;
John Stultz92c1d3e2011-11-14 14:05:44 -0800488 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700489
John Stultzcee58482012-08-31 13:30:06 -0400490 if (!timespec_valid_strict(tv))
john stultz85240702007-05-08 00:27:59 -0700491 return -EINVAL;
492
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000493 raw_spin_lock_irqsave(&timekeeper_lock, flags);
494 write_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700495
John Stultz4e250fd2012-07-27 14:48:13 -0400496 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -0700497
John Stultz4e250fd2012-07-27 14:48:13 -0400498 xt = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -0400499 ts_delta.tv_sec = tv->tv_sec - xt.tv_sec;
500 ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec;
501
John Stultz4e250fd2012-07-27 14:48:13 -0400502 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, ts_delta));
john stultz85240702007-05-08 00:27:59 -0700503
John Stultz4e250fd2012-07-27 14:48:13 -0400504 tk_set_xtime(tk, tv);
John Stultz1e75fa82012-07-13 01:21:53 -0400505
John Stultz4e250fd2012-07-27 14:48:13 -0400506 timekeeping_update(tk, true);
john stultz85240702007-05-08 00:27:59 -0700507
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000508 write_seqcount_end(&timekeeper_seq);
509 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700510
511 /* signal hrtimers about time change */
512 clock_was_set();
513
514 return 0;
515}
john stultz85240702007-05-08 00:27:59 -0700516EXPORT_SYMBOL(do_settimeofday);
517
John Stultzc528f7c2011-02-01 13:52:17 +0000518/**
519 * timekeeping_inject_offset - Adds or subtracts from the current time.
520 * @tv: pointer to the timespec variable containing the offset
521 *
522 * Adds or subtracts an offset value from the current time.
523 */
524int timekeeping_inject_offset(struct timespec *ts)
525{
John Stultz4e250fd2012-07-27 14:48:13 -0400526 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800527 unsigned long flags;
John Stultz4e8b1452012-08-08 15:36:20 -0400528 struct timespec tmp;
529 int ret = 0;
John Stultzc528f7c2011-02-01 13:52:17 +0000530
531 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
532 return -EINVAL;
533
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000534 raw_spin_lock_irqsave(&timekeeper_lock, flags);
535 write_seqcount_begin(&timekeeper_seq);
John Stultzc528f7c2011-02-01 13:52:17 +0000536
John Stultz4e250fd2012-07-27 14:48:13 -0400537 timekeeping_forward_now(tk);
John Stultzc528f7c2011-02-01 13:52:17 +0000538
John Stultz4e8b1452012-08-08 15:36:20 -0400539 /* Make sure the proposed value is valid */
540 tmp = timespec_add(tk_xtime(tk), *ts);
John Stultzcee58482012-08-31 13:30:06 -0400541 if (!timespec_valid_strict(&tmp)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400542 ret = -EINVAL;
543 goto error;
544 }
John Stultz1e75fa82012-07-13 01:21:53 -0400545
John Stultz4e250fd2012-07-27 14:48:13 -0400546 tk_xtime_add(tk, ts);
547 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *ts));
John Stultzc528f7c2011-02-01 13:52:17 +0000548
John Stultz4e8b1452012-08-08 15:36:20 -0400549error: /* even if we error out, we forwarded the time, so call update */
John Stultz4e250fd2012-07-27 14:48:13 -0400550 timekeeping_update(tk, true);
John Stultzc528f7c2011-02-01 13:52:17 +0000551
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000552 write_seqcount_end(&timekeeper_seq);
553 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzc528f7c2011-02-01 13:52:17 +0000554
555 /* signal hrtimers about time change */
556 clock_was_set();
557
John Stultz4e8b1452012-08-08 15:36:20 -0400558 return ret;
John Stultzc528f7c2011-02-01 13:52:17 +0000559}
560EXPORT_SYMBOL(timekeeping_inject_offset);
561
John Stultzcc244dd2012-05-03 12:30:07 -0700562
563/**
564 * timekeeping_get_tai_offset - Returns current TAI offset from UTC
565 *
566 */
567s32 timekeeping_get_tai_offset(void)
568{
569 struct timekeeper *tk = &timekeeper;
570 unsigned int seq;
571 s32 ret;
572
573 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000574 seq = read_seqcount_begin(&timekeeper_seq);
John Stultzcc244dd2012-05-03 12:30:07 -0700575 ret = tk->tai_offset;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000576 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultzcc244dd2012-05-03 12:30:07 -0700577
578 return ret;
579}
580
581/**
582 * __timekeeping_set_tai_offset - Lock free worker function
583 *
584 */
Fengguang Wudd5d70e2013-03-25 12:24:24 -0700585static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
John Stultzcc244dd2012-05-03 12:30:07 -0700586{
587 tk->tai_offset = tai_offset;
John Stultz90adda92013-01-21 17:00:11 -0800588 tk->offs_tai = ktime_sub(tk->offs_real, ktime_set(tai_offset, 0));
John Stultzcc244dd2012-05-03 12:30:07 -0700589}
590
591/**
592 * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
593 *
594 */
595void timekeeping_set_tai_offset(s32 tai_offset)
596{
597 struct timekeeper *tk = &timekeeper;
598 unsigned long flags;
599
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000600 raw_spin_lock_irqsave(&timekeeper_lock, flags);
601 write_seqcount_begin(&timekeeper_seq);
John Stultzcc244dd2012-05-03 12:30:07 -0700602 __timekeeping_set_tai_offset(tk, tai_offset);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000603 write_seqcount_end(&timekeeper_seq);
604 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzcc244dd2012-05-03 12:30:07 -0700605}
606
john stultz85240702007-05-08 00:27:59 -0700607/**
608 * change_clocksource - Swaps clocksources if a new one is available
609 *
610 * Accumulates current time interval and initializes new clocksource
611 */
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200612static int change_clocksource(void *data)
john stultz85240702007-05-08 00:27:59 -0700613{
John Stultz4e250fd2012-07-27 14:48:13 -0400614 struct timekeeper *tk = &timekeeper;
Magnus Damm4614e6a2009-04-21 12:24:02 -0700615 struct clocksource *new, *old;
John Stultzf695cf92012-03-14 16:38:15 -0700616 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700617
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200618 new = (struct clocksource *) data;
john stultz85240702007-05-08 00:27:59 -0700619
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000620 raw_spin_lock_irqsave(&timekeeper_lock, flags);
621 write_seqcount_begin(&timekeeper_seq);
John Stultzf695cf92012-03-14 16:38:15 -0700622
John Stultz4e250fd2012-07-27 14:48:13 -0400623 timekeeping_forward_now(tk);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200624 if (!new->enable || new->enable(new) == 0) {
John Stultz4e250fd2012-07-27 14:48:13 -0400625 old = tk->clock;
626 tk_setup_internals(tk, new);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200627 if (old->disable)
628 old->disable(old);
629 }
John Stultz4e250fd2012-07-27 14:48:13 -0400630 timekeeping_update(tk, true);
John Stultzf695cf92012-03-14 16:38:15 -0700631
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000632 write_seqcount_end(&timekeeper_seq);
633 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzf695cf92012-03-14 16:38:15 -0700634
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200635 return 0;
636}
john stultz85240702007-05-08 00:27:59 -0700637
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200638/**
639 * timekeeping_notify - Install a new clock source
640 * @clock: pointer to the clock source
641 *
642 * This function is called from clocksource.c after a new, better clock
643 * source has been registered. The caller holds the clocksource_mutex.
644 */
645void timekeeping_notify(struct clocksource *clock)
646{
John Stultz4e250fd2012-07-27 14:48:13 -0400647 struct timekeeper *tk = &timekeeper;
648
649 if (tk->clock == clock)
Magnus Damm4614e6a2009-04-21 12:24:02 -0700650 return;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200651 stop_machine(change_clocksource, clock, NULL);
john stultz85240702007-05-08 00:27:59 -0700652 tick_clock_notify();
john stultz85240702007-05-08 00:27:59 -0700653}
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200654
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200655/**
656 * ktime_get_real - get the real (wall-) time in ktime_t format
657 *
658 * returns the time in ktime_t format
659 */
660ktime_t ktime_get_real(void)
661{
662 struct timespec now;
663
664 getnstimeofday(&now);
665
666 return timespec_to_ktime(now);
667}
668EXPORT_SYMBOL_GPL(ktime_get_real);
john stultz85240702007-05-08 00:27:59 -0700669
670/**
John Stultz2d422442008-08-20 16:37:30 -0700671 * getrawmonotonic - Returns the raw monotonic time in a timespec
672 * @ts: pointer to the timespec to be set
673 *
674 * Returns the raw monotonic time (completely un-modified by ntp)
675 */
676void getrawmonotonic(struct timespec *ts)
677{
John Stultz4e250fd2012-07-27 14:48:13 -0400678 struct timekeeper *tk = &timekeeper;
John Stultz2d422442008-08-20 16:37:30 -0700679 unsigned long seq;
680 s64 nsecs;
John Stultz2d422442008-08-20 16:37:30 -0700681
682 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000683 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400684 nsecs = timekeeping_get_ns_raw(tk);
685 *ts = tk->raw_time;
John Stultz2d422442008-08-20 16:37:30 -0700686
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000687 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz2d422442008-08-20 16:37:30 -0700688
689 timespec_add_ns(ts, nsecs);
690}
691EXPORT_SYMBOL(getrawmonotonic);
692
John Stultz2d422442008-08-20 16:37:30 -0700693/**
Li Zefancf4fc6c2008-02-08 04:19:24 -0800694 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -0700695 */
Li Zefancf4fc6c2008-02-08 04:19:24 -0800696int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -0700697{
John Stultz4e250fd2012-07-27 14:48:13 -0400698 struct timekeeper *tk = &timekeeper;
john stultz85240702007-05-08 00:27:59 -0700699 unsigned long seq;
700 int ret;
701
702 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000703 seq = read_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700704
John Stultz4e250fd2012-07-27 14:48:13 -0400705 ret = tk->clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -0700706
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000707 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultz85240702007-05-08 00:27:59 -0700708
709 return ret;
710}
711
712/**
Jon Hunter98962462009-08-18 12:45:10 -0500713 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
Jon Hunter98962462009-08-18 12:45:10 -0500714 */
715u64 timekeeping_max_deferment(void)
716{
John Stultz4e250fd2012-07-27 14:48:13 -0400717 struct timekeeper *tk = &timekeeper;
John Stultz70471f22011-11-14 12:48:10 -0800718 unsigned long seq;
719 u64 ret;
John Stultz42e71e82012-07-13 01:21:51 -0400720
John Stultz70471f22011-11-14 12:48:10 -0800721 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000722 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz70471f22011-11-14 12:48:10 -0800723
John Stultz4e250fd2012-07-27 14:48:13 -0400724 ret = tk->clock->max_idle_ns;
John Stultz70471f22011-11-14 12:48:10 -0800725
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000726 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz70471f22011-11-14 12:48:10 -0800727
728 return ret;
Jon Hunter98962462009-08-18 12:45:10 -0500729}
730
731/**
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200732 * read_persistent_clock - Return time from the persistent clock.
john stultz85240702007-05-08 00:27:59 -0700733 *
734 * Weak dummy function for arches that do not yet support it.
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200735 * Reads the time from the battery backed persistent clock.
736 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
john stultz85240702007-05-08 00:27:59 -0700737 *
738 * XXX - Do be sure to remove it once all arches implement it.
739 */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200740void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700741{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200742 ts->tv_sec = 0;
743 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700744}
745
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200746/**
747 * read_boot_clock - Return time of the system start.
748 *
749 * Weak dummy function for arches that do not yet support it.
750 * Function to read the exact time the system has been started.
751 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
752 *
753 * XXX - Do be sure to remove it once all arches implement it.
754 */
755void __attribute__((weak)) read_boot_clock(struct timespec *ts)
756{
757 ts->tv_sec = 0;
758 ts->tv_nsec = 0;
759}
760
john stultz85240702007-05-08 00:27:59 -0700761/*
762 * timekeeping_init - Initializes the clocksource and common timekeeping values
763 */
764void __init timekeeping_init(void)
765{
John Stultz4e250fd2012-07-27 14:48:13 -0400766 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200767 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700768 unsigned long flags;
John Stultz6d0ef902012-07-27 14:48:12 -0400769 struct timespec now, boot, tmp;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200770
771 read_persistent_clock(&now);
Feng Tang31ade302013-01-16 00:09:47 +0800772
John Stultzcee58482012-08-31 13:30:06 -0400773 if (!timespec_valid_strict(&now)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400774 pr_warn("WARNING: Persistent clock returned invalid value!\n"
775 " Check your CMOS/BIOS settings.\n");
776 now.tv_sec = 0;
777 now.tv_nsec = 0;
Feng Tang31ade302013-01-16 00:09:47 +0800778 } else if (now.tv_sec || now.tv_nsec)
779 persistent_clock_exist = true;
John Stultz4e8b1452012-08-08 15:36:20 -0400780
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200781 read_boot_clock(&boot);
John Stultzcee58482012-08-31 13:30:06 -0400782 if (!timespec_valid_strict(&boot)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400783 pr_warn("WARNING: Boot clock returned invalid value!\n"
784 " Check your CMOS/BIOS settings.\n");
785 boot.tv_sec = 0;
786 boot.tv_nsec = 0;
787 }
john stultz85240702007-05-08 00:27:59 -0700788
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700789 ntp_init();
john stultz85240702007-05-08 00:27:59 -0700790
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000791 raw_spin_lock_irqsave(&timekeeper_lock, flags);
792 write_seqcount_begin(&timekeeper_seq);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200793 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200794 if (clock->enable)
795 clock->enable(clock);
John Stultz4e250fd2012-07-27 14:48:13 -0400796 tk_setup_internals(tk, clock);
john stultz85240702007-05-08 00:27:59 -0700797
John Stultz4e250fd2012-07-27 14:48:13 -0400798 tk_set_xtime(tk, &now);
799 tk->raw_time.tv_sec = 0;
800 tk->raw_time.tv_nsec = 0;
John Stultz1e75fa82012-07-13 01:21:53 -0400801 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
John Stultz4e250fd2012-07-27 14:48:13 -0400802 boot = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -0400803
John Stultz6d0ef902012-07-27 14:48:12 -0400804 set_normalized_timespec(&tmp, -boot.tv_sec, -boot.tv_nsec);
John Stultz4e250fd2012-07-27 14:48:13 -0400805 tk_set_wall_to_mono(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -0400806
807 tmp.tv_sec = 0;
808 tmp.tv_nsec = 0;
John Stultz4e250fd2012-07-27 14:48:13 -0400809 tk_set_sleep_time(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -0400810
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000811 write_seqcount_end(&timekeeper_seq);
812 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700813}
814
john stultz85240702007-05-08 00:27:59 -0700815/* time in seconds when suspend began */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200816static struct timespec timekeeping_suspend_time;
john stultz85240702007-05-08 00:27:59 -0700817
818/**
John Stultz304529b2011-04-01 14:32:09 -0700819 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
820 * @delta: pointer to a timespec delta value
821 *
822 * Takes a timespec offset measuring a suspend interval and properly
823 * adds the sleep offset to the timekeeping variables.
824 */
John Stultzf726a692012-07-13 01:21:57 -0400825static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
826 struct timespec *delta)
John Stultz304529b2011-04-01 14:32:09 -0700827{
John Stultzcee58482012-08-31 13:30:06 -0400828 if (!timespec_valid_strict(delta)) {
John Stultzcbaa5152011-07-20 15:42:55 -0700829 printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid "
John Stultzcb5de2f2011-06-01 18:18:09 -0700830 "sleep delta value!\n");
831 return;
832 }
John Stultzf726a692012-07-13 01:21:57 -0400833 tk_xtime_add(tk, delta);
John Stultz6d0ef902012-07-27 14:48:12 -0400834 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *delta));
835 tk_set_sleep_time(tk, timespec_add(tk->total_sleep_time, *delta));
John Stultz304529b2011-04-01 14:32:09 -0700836}
837
John Stultz304529b2011-04-01 14:32:09 -0700838/**
839 * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
840 * @delta: pointer to a timespec delta value
841 *
842 * This hook is for architectures that cannot support read_persistent_clock
843 * because their RTC/persistent clock is only accessible when irqs are enabled.
844 *
845 * This function should only be called by rtc_resume(), and allows
846 * a suspend offset to be injected into the timekeeping values.
847 */
848void timekeeping_inject_sleeptime(struct timespec *delta)
849{
John Stultz4e250fd2012-07-27 14:48:13 -0400850 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800851 unsigned long flags;
John Stultz304529b2011-04-01 14:32:09 -0700852
Feng Tang31ade302013-01-16 00:09:47 +0800853 /*
854 * Make sure we don't set the clock twice, as timekeeping_resume()
855 * already did it
856 */
857 if (has_persistent_clock())
John Stultz304529b2011-04-01 14:32:09 -0700858 return;
859
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000860 raw_spin_lock_irqsave(&timekeeper_lock, flags);
861 write_seqcount_begin(&timekeeper_seq);
John Stultz70471f22011-11-14 12:48:10 -0800862
John Stultz4e250fd2012-07-27 14:48:13 -0400863 timekeeping_forward_now(tk);
John Stultz304529b2011-04-01 14:32:09 -0700864
John Stultz4e250fd2012-07-27 14:48:13 -0400865 __timekeeping_inject_sleeptime(tk, delta);
John Stultz304529b2011-04-01 14:32:09 -0700866
John Stultz4e250fd2012-07-27 14:48:13 -0400867 timekeeping_update(tk, true);
John Stultz304529b2011-04-01 14:32:09 -0700868
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000869 write_seqcount_end(&timekeeper_seq);
870 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz304529b2011-04-01 14:32:09 -0700871
872 /* signal hrtimers about time change */
873 clock_was_set();
874}
875
John Stultz304529b2011-04-01 14:32:09 -0700876/**
john stultz85240702007-05-08 00:27:59 -0700877 * timekeeping_resume - Resumes the generic timekeeping subsystem.
john stultz85240702007-05-08 00:27:59 -0700878 *
879 * This is for the generic clocksource timekeeping.
880 * xtime/wall_to_monotonic/jiffies/etc are
881 * still managed by arch specific suspend/resume code.
882 */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +0100883static void timekeeping_resume(void)
john stultz85240702007-05-08 00:27:59 -0700884{
John Stultz4e250fd2012-07-27 14:48:13 -0400885 struct timekeeper *tk = &timekeeper;
Feng Tange445cf12013-03-12 11:56:48 +0800886 struct clocksource *clock = tk->clock;
John Stultz92c1d3e2011-11-14 14:05:44 -0800887 unsigned long flags;
Feng Tange445cf12013-03-12 11:56:48 +0800888 struct timespec ts_new, ts_delta;
889 cycle_t cycle_now, cycle_delta;
890 bool suspendtime_found = false;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200891
Feng Tange445cf12013-03-12 11:56:48 +0800892 read_persistent_clock(&ts_new);
john stultz85240702007-05-08 00:27:59 -0700893
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200894 clockevents_resume();
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +0200895 clocksource_resume();
896
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000897 raw_spin_lock_irqsave(&timekeeper_lock, flags);
898 write_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700899
Feng Tange445cf12013-03-12 11:56:48 +0800900 /*
901 * After system resumes, we need to calculate the suspended time and
902 * compensate it for the OS time. There are 3 sources that could be
903 * used: Nonstop clocksource during suspend, persistent clock and rtc
904 * device.
905 *
906 * One specific platform may have 1 or 2 or all of them, and the
907 * preference will be:
908 * suspend-nonstop clocksource -> persistent clock -> rtc
909 * The less preferred source will only be tried if there is no better
910 * usable source. The rtc part is handled separately in rtc core code.
911 */
912 cycle_now = clock->read(clock);
913 if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
914 cycle_now > clock->cycle_last) {
915 u64 num, max = ULLONG_MAX;
916 u32 mult = clock->mult;
917 u32 shift = clock->shift;
918 s64 nsec = 0;
919
920 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
921
922 /*
923 * "cycle_delta * mutl" may cause 64 bits overflow, if the
924 * suspended time is too long. In that case we need do the
925 * 64 bits math carefully
926 */
927 do_div(max, mult);
928 if (cycle_delta > max) {
929 num = div64_u64(cycle_delta, max);
930 nsec = (((u64) max * mult) >> shift) * num;
931 cycle_delta -= num * max;
932 }
933 nsec += ((u64) cycle_delta * mult) >> shift;
934
935 ts_delta = ns_to_timespec(nsec);
936 suspendtime_found = true;
937 } else if (timespec_compare(&ts_new, &timekeeping_suspend_time) > 0) {
938 ts_delta = timespec_sub(ts_new, timekeeping_suspend_time);
939 suspendtime_found = true;
john stultz85240702007-05-08 00:27:59 -0700940 }
Feng Tange445cf12013-03-12 11:56:48 +0800941
942 if (suspendtime_found)
943 __timekeeping_inject_sleeptime(tk, &ts_delta);
944
945 /* Re-base the last cycle value */
946 clock->cycle_last = cycle_now;
John Stultz4e250fd2012-07-27 14:48:13 -0400947 tk->ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -0700948 timekeeping_suspended = 0;
John Stultz4e250fd2012-07-27 14:48:13 -0400949 timekeeping_update(tk, false);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000950 write_seqcount_end(&timekeeper_seq);
951 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700952
953 touch_softlockup_watchdog();
954
955 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
956
957 /* Resume hrtimers */
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200958 hrtimers_resume();
john stultz85240702007-05-08 00:27:59 -0700959}
960
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +0100961static int timekeeping_suspend(void)
john stultz85240702007-05-08 00:27:59 -0700962{
John Stultz4e250fd2012-07-27 14:48:13 -0400963 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800964 unsigned long flags;
John Stultzcb332172011-05-31 22:53:23 -0700965 struct timespec delta, delta_delta;
966 static struct timespec old_delta;
john stultz85240702007-05-08 00:27:59 -0700967
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200968 read_persistent_clock(&timekeeping_suspend_time);
Thomas Gleixner3be90952007-09-16 15:36:43 +0200969
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000970 raw_spin_lock_irqsave(&timekeeper_lock, flags);
971 write_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400972 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -0700973 timekeeping_suspended = 1;
John Stultzcb332172011-05-31 22:53:23 -0700974
975 /*
976 * To avoid drift caused by repeated suspend/resumes,
977 * which each can add ~1 second drift error,
978 * try to compensate so the difference in system time
979 * and persistent_clock time stays close to constant.
980 */
John Stultz4e250fd2012-07-27 14:48:13 -0400981 delta = timespec_sub(tk_xtime(tk), timekeeping_suspend_time);
John Stultzcb332172011-05-31 22:53:23 -0700982 delta_delta = timespec_sub(delta, old_delta);
983 if (abs(delta_delta.tv_sec) >= 2) {
984 /*
985 * if delta_delta is too large, assume time correction
986 * has occured and set old_delta to the current delta.
987 */
988 old_delta = delta;
989 } else {
990 /* Otherwise try to adjust old_system to compensate */
991 timekeeping_suspend_time =
992 timespec_add(timekeeping_suspend_time, delta_delta);
993 }
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000994 write_seqcount_end(&timekeeper_seq);
995 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700996
997 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
Magnus Dammc54a42b2010-02-02 14:41:41 -0800998 clocksource_suspend();
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200999 clockevents_suspend();
john stultz85240702007-05-08 00:27:59 -07001000
1001 return 0;
1002}
1003
1004/* sysfs resume/suspend bits for timekeeping */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001005static struct syscore_ops timekeeping_syscore_ops = {
john stultz85240702007-05-08 00:27:59 -07001006 .resume = timekeeping_resume,
1007 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -07001008};
1009
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001010static int __init timekeeping_init_ops(void)
john stultz85240702007-05-08 00:27:59 -07001011{
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001012 register_syscore_ops(&timekeeping_syscore_ops);
1013 return 0;
john stultz85240702007-05-08 00:27:59 -07001014}
1015
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001016device_initcall(timekeeping_init_ops);
john stultz85240702007-05-08 00:27:59 -07001017
1018/*
1019 * If the error is already larger, we look ahead even further
1020 * to compensate for late or lost adjustments.
1021 */
John Stultzf726a692012-07-13 01:21:57 -04001022static __always_inline int timekeeping_bigadjust(struct timekeeper *tk,
1023 s64 error, s64 *interval,
john stultz85240702007-05-08 00:27:59 -07001024 s64 *offset)
1025{
1026 s64 tick_error, i;
1027 u32 look_ahead, adj;
1028 s32 error2, mult;
1029
1030 /*
1031 * Use the current error value to determine how much to look ahead.
1032 * The larger the error the slower we adjust for it to avoid problems
1033 * with losing too many ticks, otherwise we would overadjust and
1034 * produce an even larger error. The smaller the adjustment the
1035 * faster we try to adjust for it, as lost ticks can do less harm
Li Zefan3eb05672008-02-08 04:19:25 -08001036 * here. This is tuned so that an error of about 1 msec is adjusted
john stultz85240702007-05-08 00:27:59 -07001037 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
1038 */
John Stultzf726a692012-07-13 01:21:57 -04001039 error2 = tk->ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
john stultz85240702007-05-08 00:27:59 -07001040 error2 = abs(error2);
1041 for (look_ahead = 0; error2 > 0; look_ahead++)
1042 error2 >>= 2;
1043
1044 /*
1045 * Now calculate the error in (1 << look_ahead) ticks, but first
1046 * remove the single look ahead already included in the error.
1047 */
John Stultzf726a692012-07-13 01:21:57 -04001048 tick_error = ntp_tick_length() >> (tk->ntp_error_shift + 1);
1049 tick_error -= tk->xtime_interval >> 1;
john stultz85240702007-05-08 00:27:59 -07001050 error = ((error - tick_error) >> look_ahead) + tick_error;
1051
1052 /* Finally calculate the adjustment shift value. */
1053 i = *interval;
1054 mult = 1;
1055 if (error < 0) {
1056 error = -error;
1057 *interval = -*interval;
1058 *offset = -*offset;
1059 mult = -1;
1060 }
1061 for (adj = 0; error > i; adj++)
1062 error >>= 1;
1063
1064 *interval <<= adj;
1065 *offset <<= adj;
1066 return mult << adj;
1067}
1068
1069/*
1070 * Adjust the multiplier to reduce the error value,
1071 * this is optimized for the most common adjustments of -1,0,1,
1072 * for other values we can do a bit more work.
1073 */
John Stultzf726a692012-07-13 01:21:57 -04001074static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
john stultz85240702007-05-08 00:27:59 -07001075{
John Stultzf726a692012-07-13 01:21:57 -04001076 s64 error, interval = tk->cycle_interval;
john stultz85240702007-05-08 00:27:59 -07001077 int adj;
1078
John Stultzc2bc1112011-10-27 18:12:42 -07001079 /*
Jim Cromie88b28ad2012-03-14 21:28:56 -06001080 * The point of this is to check if the error is greater than half
John Stultzc2bc1112011-10-27 18:12:42 -07001081 * an interval.
1082 *
1083 * First we shift it down from NTP_SHIFT to clocksource->shifted nsecs.
1084 *
1085 * Note we subtract one in the shift, so that error is really error*2.
John Stultz3f86f282011-10-27 17:41:17 -07001086 * This "saves" dividing(shifting) interval twice, but keeps the
1087 * (error > interval) comparison as still measuring if error is
Jim Cromie88b28ad2012-03-14 21:28:56 -06001088 * larger than half an interval.
John Stultzc2bc1112011-10-27 18:12:42 -07001089 *
John Stultz3f86f282011-10-27 17:41:17 -07001090 * Note: It does not "save" on aggravation when reading the code.
John Stultzc2bc1112011-10-27 18:12:42 -07001091 */
John Stultzf726a692012-07-13 01:21:57 -04001092 error = tk->ntp_error >> (tk->ntp_error_shift - 1);
john stultz85240702007-05-08 00:27:59 -07001093 if (error > interval) {
John Stultzc2bc1112011-10-27 18:12:42 -07001094 /*
1095 * We now divide error by 4(via shift), which checks if
Jim Cromie88b28ad2012-03-14 21:28:56 -06001096 * the error is greater than twice the interval.
John Stultzc2bc1112011-10-27 18:12:42 -07001097 * If it is greater, we need a bigadjust, if its smaller,
1098 * we can adjust by 1.
1099 */
john stultz85240702007-05-08 00:27:59 -07001100 error >>= 2;
John Stultzc2bc1112011-10-27 18:12:42 -07001101 /*
1102 * XXX - In update_wall_time, we round up to the next
1103 * nanosecond, and store the amount rounded up into
1104 * the error. This causes the likely below to be unlikely.
1105 *
John Stultz3f86f282011-10-27 17:41:17 -07001106 * The proper fix is to avoid rounding up by using
John Stultz4e250fd2012-07-27 14:48:13 -04001107 * the high precision tk->xtime_nsec instead of
John Stultzc2bc1112011-10-27 18:12:42 -07001108 * xtime.tv_nsec everywhere. Fixing this will take some
1109 * time.
1110 */
john stultz85240702007-05-08 00:27:59 -07001111 if (likely(error <= interval))
1112 adj = 1;
1113 else
Ingo Molnar1d17d172012-08-04 21:21:14 +02001114 adj = timekeeping_bigadjust(tk, error, &interval, &offset);
1115 } else {
1116 if (error < -interval) {
1117 /* See comment above, this is just switched for the negative */
1118 error >>= 2;
1119 if (likely(error >= -interval)) {
1120 adj = -1;
1121 interval = -interval;
1122 offset = -offset;
1123 } else {
1124 adj = timekeeping_bigadjust(tk, error, &interval, &offset);
1125 }
1126 } else {
1127 goto out_adjust;
1128 }
1129 }
john stultz85240702007-05-08 00:27:59 -07001130
John Stultzf726a692012-07-13 01:21:57 -04001131 if (unlikely(tk->clock->maxadj &&
1132 (tk->mult + adj > tk->clock->mult + tk->clock->maxadj))) {
John Stultze919cfd2012-03-22 19:14:46 -07001133 printk_once(KERN_WARNING
1134 "Adjusting %s more than 11%% (%ld vs %ld)\n",
John Stultzf726a692012-07-13 01:21:57 -04001135 tk->clock->name, (long)tk->mult + adj,
1136 (long)tk->clock->mult + tk->clock->maxadj);
John Stultze919cfd2012-03-22 19:14:46 -07001137 }
John Stultzc2bc1112011-10-27 18:12:42 -07001138 /*
1139 * So the following can be confusing.
1140 *
1141 * To keep things simple, lets assume adj == 1 for now.
1142 *
1143 * When adj != 1, remember that the interval and offset values
1144 * have been appropriately scaled so the math is the same.
1145 *
1146 * The basic idea here is that we're increasing the multiplier
1147 * by one, this causes the xtime_interval to be incremented by
1148 * one cycle_interval. This is because:
1149 * xtime_interval = cycle_interval * mult
1150 * So if mult is being incremented by one:
1151 * xtime_interval = cycle_interval * (mult + 1)
1152 * Its the same as:
1153 * xtime_interval = (cycle_interval * mult) + cycle_interval
1154 * Which can be shortened to:
1155 * xtime_interval += cycle_interval
1156 *
1157 * So offset stores the non-accumulated cycles. Thus the current
1158 * time (in shifted nanoseconds) is:
1159 * now = (offset * adj) + xtime_nsec
1160 * Now, even though we're adjusting the clock frequency, we have
1161 * to keep time consistent. In other words, we can't jump back
1162 * in time, and we also want to avoid jumping forward in time.
1163 *
1164 * So given the same offset value, we need the time to be the same
1165 * both before and after the freq adjustment.
1166 * now = (offset * adj_1) + xtime_nsec_1
1167 * now = (offset * adj_2) + xtime_nsec_2
1168 * So:
1169 * (offset * adj_1) + xtime_nsec_1 =
1170 * (offset * adj_2) + xtime_nsec_2
1171 * And we know:
1172 * adj_2 = adj_1 + 1
1173 * So:
1174 * (offset * adj_1) + xtime_nsec_1 =
1175 * (offset * (adj_1+1)) + xtime_nsec_2
1176 * (offset * adj_1) + xtime_nsec_1 =
1177 * (offset * adj_1) + offset + xtime_nsec_2
1178 * Canceling the sides:
1179 * xtime_nsec_1 = offset + xtime_nsec_2
1180 * Which gives us:
1181 * xtime_nsec_2 = xtime_nsec_1 - offset
1182 * Which simplfies to:
1183 * xtime_nsec -= offset
1184 *
1185 * XXX - TODO: Doc ntp_error calculation.
1186 */
John Stultzf726a692012-07-13 01:21:57 -04001187 tk->mult += adj;
1188 tk->xtime_interval += interval;
1189 tk->xtime_nsec -= offset;
1190 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -04001191
Ingo Molnar1d17d172012-08-04 21:21:14 +02001192out_adjust:
John Stultz2a8c0882012-07-13 01:21:56 -04001193 /*
1194 * It may be possible that when we entered this function, xtime_nsec
1195 * was very small. Further, if we're slightly speeding the clocksource
1196 * in the code above, its possible the required corrective factor to
1197 * xtime_nsec could cause it to underflow.
1198 *
1199 * Now, since we already accumulated the second, cannot simply roll
1200 * the accumulated second back, since the NTP subsystem has been
1201 * notified via second_overflow. So instead we push xtime_nsec forward
1202 * by the amount we underflowed, and add that amount into the error.
1203 *
1204 * We'll correct this error next time through this function, when
1205 * xtime_nsec is not as small.
1206 */
John Stultzf726a692012-07-13 01:21:57 -04001207 if (unlikely((s64)tk->xtime_nsec < 0)) {
1208 s64 neg = -(s64)tk->xtime_nsec;
1209 tk->xtime_nsec = 0;
1210 tk->ntp_error += neg << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -04001211 }
1212
john stultz85240702007-05-08 00:27:59 -07001213}
1214
1215/**
John Stultz1f4f9482012-07-13 01:21:54 -04001216 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
1217 *
1218 * Helper function that accumulates a the nsecs greater then a second
1219 * from the xtime_nsec field to the xtime_secs field.
1220 * It also calls into the NTP code to handle leapsecond processing.
1221 *
1222 */
1223static inline void accumulate_nsecs_to_secs(struct timekeeper *tk)
1224{
1225 u64 nsecps = (u64)NSEC_PER_SEC << tk->shift;
1226
1227 while (tk->xtime_nsec >= nsecps) {
1228 int leap;
1229
1230 tk->xtime_nsec -= nsecps;
1231 tk->xtime_sec++;
1232
1233 /* Figure out if its a leap sec and apply if needed */
1234 leap = second_overflow(tk->xtime_sec);
John Stultz6d0ef902012-07-27 14:48:12 -04001235 if (unlikely(leap)) {
1236 struct timespec ts;
John Stultz1f4f9482012-07-13 01:21:54 -04001237
John Stultz6d0ef902012-07-27 14:48:12 -04001238 tk->xtime_sec += leap;
1239
1240 ts.tv_sec = leap;
1241 ts.tv_nsec = 0;
1242 tk_set_wall_to_mono(tk,
1243 timespec_sub(tk->wall_to_monotonic, ts));
1244
John Stultzcc244dd2012-05-03 12:30:07 -07001245 __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
1246
John Stultz6d0ef902012-07-27 14:48:12 -04001247 clock_was_set_delayed();
1248 }
John Stultz1f4f9482012-07-13 01:21:54 -04001249 }
1250}
1251
John Stultz1f4f9482012-07-13 01:21:54 -04001252/**
john stultza092ff02009-10-02 16:17:53 -07001253 * logarithmic_accumulation - shifted accumulation of cycles
1254 *
1255 * This functions accumulates a shifted interval of cycles into
1256 * into a shifted interval nanoseconds. Allows for O(log) accumulation
1257 * loop.
1258 *
1259 * Returns the unconsumed cycles.
1260 */
John Stultzf726a692012-07-13 01:21:57 -04001261static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
1262 u32 shift)
john stultza092ff02009-10-02 16:17:53 -07001263{
Thomas Gleixner23a95372013-02-21 22:51:36 +00001264 cycle_t interval = tk->cycle_interval << shift;
Jason Wesseldeda2e82010-08-09 14:20:09 -07001265 u64 raw_nsecs;
john stultza092ff02009-10-02 16:17:53 -07001266
John Stultzf726a692012-07-13 01:21:57 -04001267 /* If the offset is smaller then a shifted interval, do nothing */
Thomas Gleixner23a95372013-02-21 22:51:36 +00001268 if (offset < interval)
john stultza092ff02009-10-02 16:17:53 -07001269 return offset;
1270
1271 /* Accumulate one shifted interval */
Thomas Gleixner23a95372013-02-21 22:51:36 +00001272 offset -= interval;
1273 tk->clock->cycle_last += interval;
john stultza092ff02009-10-02 16:17:53 -07001274
John Stultzf726a692012-07-13 01:21:57 -04001275 tk->xtime_nsec += tk->xtime_interval << shift;
1276 accumulate_nsecs_to_secs(tk);
john stultza092ff02009-10-02 16:17:53 -07001277
Jason Wesseldeda2e82010-08-09 14:20:09 -07001278 /* Accumulate raw time */
Dan Carpenter5b3900c2012-10-09 10:18:23 +03001279 raw_nsecs = (u64)tk->raw_interval << shift;
John Stultzf726a692012-07-13 01:21:57 -04001280 raw_nsecs += tk->raw_time.tv_nsec;
John Stultzc7dcf872010-08-13 11:30:58 -07001281 if (raw_nsecs >= NSEC_PER_SEC) {
1282 u64 raw_secs = raw_nsecs;
1283 raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
John Stultzf726a692012-07-13 01:21:57 -04001284 tk->raw_time.tv_sec += raw_secs;
john stultza092ff02009-10-02 16:17:53 -07001285 }
John Stultzf726a692012-07-13 01:21:57 -04001286 tk->raw_time.tv_nsec = raw_nsecs;
john stultza092ff02009-10-02 16:17:53 -07001287
1288 /* Accumulate error between NTP and clock interval */
John Stultzf726a692012-07-13 01:21:57 -04001289 tk->ntp_error += ntp_tick_length() << shift;
1290 tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
1291 (tk->ntp_error_shift + shift);
john stultza092ff02009-10-02 16:17:53 -07001292
1293 return offset;
1294}
1295
John Stultz92bb1fc2012-09-04 15:38:12 -04001296#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
1297static inline void old_vsyscall_fixup(struct timekeeper *tk)
1298{
1299 s64 remainder;
1300
1301 /*
1302 * Store only full nanoseconds into xtime_nsec after rounding
1303 * it up and add the remainder to the error difference.
1304 * XXX - This is necessary to avoid small 1ns inconsistnecies caused
1305 * by truncating the remainder in vsyscalls. However, it causes
1306 * additional work to be done in timekeeping_adjust(). Once
1307 * the vsyscall implementations are converted to use xtime_nsec
1308 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
1309 * users are removed, this can be killed.
1310 */
1311 remainder = tk->xtime_nsec & ((1ULL << tk->shift) - 1);
1312 tk->xtime_nsec -= remainder;
1313 tk->xtime_nsec += 1ULL << tk->shift;
1314 tk->ntp_error += remainder << tk->ntp_error_shift;
1315
1316}
1317#else
1318#define old_vsyscall_fixup(tk)
1319#endif
1320
1321
1322
john stultz85240702007-05-08 00:27:59 -07001323/**
1324 * update_wall_time - Uses the current clocksource to increment the wall time
1325 *
john stultz85240702007-05-08 00:27:59 -07001326 */
Torben Hohn871cf1e2011-01-27 15:58:55 +01001327static void update_wall_time(void)
john stultz85240702007-05-08 00:27:59 -07001328{
Martin Schwidefsky155ec602009-08-14 15:47:26 +02001329 struct clocksource *clock;
John Stultz4e250fd2012-07-27 14:48:13 -04001330 struct timekeeper *tk = &timekeeper;
john stultz85240702007-05-08 00:27:59 -07001331 cycle_t offset;
john stultza092ff02009-10-02 16:17:53 -07001332 int shift = 0, maxshift;
John Stultz70471f22011-11-14 12:48:10 -08001333 unsigned long flags;
1334
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001335 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1336 write_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -07001337
1338 /* Make sure we're fully resumed: */
1339 if (unlikely(timekeeping_suspended))
John Stultz70471f22011-11-14 12:48:10 -08001340 goto out;
john stultz85240702007-05-08 00:27:59 -07001341
John Stultz4e250fd2012-07-27 14:48:13 -04001342 clock = tk->clock;
John Stultz592913e2010-07-13 17:56:20 -07001343
1344#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
John Stultz4e250fd2012-07-27 14:48:13 -04001345 offset = tk->cycle_interval;
John Stultz592913e2010-07-13 17:56:20 -07001346#else
1347 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
john stultz85240702007-05-08 00:27:59 -07001348#endif
john stultz85240702007-05-08 00:27:59 -07001349
John Stultzbf2ac312012-08-21 20:30:49 -04001350 /* Check if there's really nothing to do */
1351 if (offset < tk->cycle_interval)
1352 goto out;
1353
john stultza092ff02009-10-02 16:17:53 -07001354 /*
1355 * With NO_HZ we may have to accumulate many cycle_intervals
1356 * (think "ticks") worth of time at once. To do this efficiently,
1357 * we calculate the largest doubling multiple of cycle_intervals
Jim Cromie88b28ad2012-03-14 21:28:56 -06001358 * that is smaller than the offset. We then accumulate that
john stultza092ff02009-10-02 16:17:53 -07001359 * chunk in one go, and then try to consume the next smaller
1360 * doubled multiple.
john stultz85240702007-05-08 00:27:59 -07001361 */
John Stultz4e250fd2012-07-27 14:48:13 -04001362 shift = ilog2(offset) - ilog2(tk->cycle_interval);
john stultza092ff02009-10-02 16:17:53 -07001363 shift = max(0, shift);
Jim Cromie88b28ad2012-03-14 21:28:56 -06001364 /* Bound shift to one less than what overflows tick_length */
John Stultzea7cf492011-11-14 13:18:07 -08001365 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
john stultza092ff02009-10-02 16:17:53 -07001366 shift = min(shift, maxshift);
John Stultz4e250fd2012-07-27 14:48:13 -04001367 while (offset >= tk->cycle_interval) {
1368 offset = logarithmic_accumulation(tk, offset, shift);
1369 if (offset < tk->cycle_interval<<shift)
John Stultz830ec042010-03-18 14:47:30 -07001370 shift--;
john stultz85240702007-05-08 00:27:59 -07001371 }
1372
1373 /* correct the clock when NTP error is too big */
John Stultz4e250fd2012-07-27 14:48:13 -04001374 timekeeping_adjust(tk, offset);
john stultz85240702007-05-08 00:27:59 -07001375
John Stultz6a867a32010-04-06 14:30:51 -07001376 /*
John Stultz92bb1fc2012-09-04 15:38:12 -04001377 * XXX This can be killed once everyone converts
1378 * to the new update_vsyscall.
1379 */
1380 old_vsyscall_fixup(tk);
john stultz85240702007-05-08 00:27:59 -07001381
John Stultz6a867a32010-04-06 14:30:51 -07001382 /*
1383 * Finally, make sure that after the rounding
John Stultz1e75fa82012-07-13 01:21:53 -04001384 * xtime_nsec isn't larger than NSEC_PER_SEC
John Stultz6a867a32010-04-06 14:30:51 -07001385 */
John Stultz4e250fd2012-07-27 14:48:13 -04001386 accumulate_nsecs_to_secs(tk);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001387
John Stultz4e250fd2012-07-27 14:48:13 -04001388 timekeeping_update(tk, false);
John Stultz70471f22011-11-14 12:48:10 -08001389
1390out:
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001391 write_seqcount_end(&timekeeper_seq);
1392 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz70471f22011-11-14 12:48:10 -08001393
john stultz85240702007-05-08 00:27:59 -07001394}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001395
1396/**
1397 * getboottime - Return the real time of system boot.
1398 * @ts: pointer to the timespec to be set
1399 *
John Stultzabb3a4e2011-02-14 17:52:09 -08001400 * Returns the wall-time of boot in a timespec.
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001401 *
1402 * This is based on the wall_to_monotonic offset and the total suspend
1403 * time. Calls to settimeofday will affect the value returned (which
1404 * basically means that however wrong your real time clock is at boot time,
1405 * you get the right time here).
1406 */
1407void getboottime(struct timespec *ts)
1408{
John Stultz4e250fd2012-07-27 14:48:13 -04001409 struct timekeeper *tk = &timekeeper;
Hiroshi Shimamoto36d47482009-08-25 15:08:30 +09001410 struct timespec boottime = {
John Stultz4e250fd2012-07-27 14:48:13 -04001411 .tv_sec = tk->wall_to_monotonic.tv_sec +
1412 tk->total_sleep_time.tv_sec,
1413 .tv_nsec = tk->wall_to_monotonic.tv_nsec +
1414 tk->total_sleep_time.tv_nsec
Hiroshi Shimamoto36d47482009-08-25 15:08:30 +09001415 };
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001416
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001417 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001418}
Jason Wangc93d89f2010-01-27 19:13:40 +08001419EXPORT_SYMBOL_GPL(getboottime);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001420
John Stultzabb3a4e2011-02-14 17:52:09 -08001421/**
1422 * get_monotonic_boottime - Returns monotonic time since boot
1423 * @ts: pointer to the timespec to be set
1424 *
1425 * Returns the monotonic time since boot in a timespec.
1426 *
1427 * This is similar to CLOCK_MONTONIC/ktime_get_ts, but also
1428 * includes the time spent in suspend.
1429 */
1430void get_monotonic_boottime(struct timespec *ts)
1431{
John Stultz4e250fd2012-07-27 14:48:13 -04001432 struct timekeeper *tk = &timekeeper;
John Stultzabb3a4e2011-02-14 17:52:09 -08001433 struct timespec tomono, sleep;
John Stultzec145ba2012-09-11 19:26:03 -04001434 s64 nsec;
John Stultzabb3a4e2011-02-14 17:52:09 -08001435 unsigned int seq;
John Stultzabb3a4e2011-02-14 17:52:09 -08001436
1437 WARN_ON(timekeeping_suspended);
1438
1439 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001440 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001441 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -04001442 nsec = timekeeping_get_ns(tk);
John Stultz4e250fd2012-07-27 14:48:13 -04001443 tomono = tk->wall_to_monotonic;
1444 sleep = tk->total_sleep_time;
John Stultzabb3a4e2011-02-14 17:52:09 -08001445
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001446 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultzabb3a4e2011-02-14 17:52:09 -08001447
John Stultzec145ba2012-09-11 19:26:03 -04001448 ts->tv_sec += tomono.tv_sec + sleep.tv_sec;
1449 ts->tv_nsec = 0;
1450 timespec_add_ns(ts, nsec + tomono.tv_nsec + sleep.tv_nsec);
John Stultzabb3a4e2011-02-14 17:52:09 -08001451}
1452EXPORT_SYMBOL_GPL(get_monotonic_boottime);
1453
1454/**
1455 * ktime_get_boottime - Returns monotonic time since boot in a ktime
1456 *
1457 * Returns the monotonic time since boot in a ktime
1458 *
1459 * This is similar to CLOCK_MONTONIC/ktime_get, but also
1460 * includes the time spent in suspend.
1461 */
1462ktime_t ktime_get_boottime(void)
1463{
1464 struct timespec ts;
1465
1466 get_monotonic_boottime(&ts);
1467 return timespec_to_ktime(ts);
1468}
1469EXPORT_SYMBOL_GPL(ktime_get_boottime);
1470
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001471/**
1472 * monotonic_to_bootbased - Convert the monotonic time to boot based.
1473 * @ts: pointer to the timespec to be converted
1474 */
1475void monotonic_to_bootbased(struct timespec *ts)
1476{
John Stultz4e250fd2012-07-27 14:48:13 -04001477 struct timekeeper *tk = &timekeeper;
1478
1479 *ts = timespec_add(*ts, tk->total_sleep_time);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001480}
Jason Wangc93d89f2010-01-27 19:13:40 +08001481EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
john stultz2c6b47d2007-07-24 17:47:43 -07001482
john stultz17c38b72007-07-24 18:38:34 -07001483unsigned long get_seconds(void)
1484{
John Stultz4e250fd2012-07-27 14:48:13 -04001485 struct timekeeper *tk = &timekeeper;
1486
1487 return tk->xtime_sec;
john stultz17c38b72007-07-24 18:38:34 -07001488}
1489EXPORT_SYMBOL(get_seconds);
1490
john stultzda15cfd2009-08-19 19:13:34 -07001491struct timespec __current_kernel_time(void)
1492{
John Stultz4e250fd2012-07-27 14:48:13 -04001493 struct timekeeper *tk = &timekeeper;
1494
1495 return tk_xtime(tk);
john stultzda15cfd2009-08-19 19:13:34 -07001496}
john stultz17c38b72007-07-24 18:38:34 -07001497
john stultz2c6b47d2007-07-24 17:47:43 -07001498struct timespec current_kernel_time(void)
1499{
John Stultz4e250fd2012-07-27 14:48:13 -04001500 struct timekeeper *tk = &timekeeper;
john stultz2c6b47d2007-07-24 17:47:43 -07001501 struct timespec now;
1502 unsigned long seq;
1503
1504 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001505 seq = read_seqcount_begin(&timekeeper_seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001506
John Stultz4e250fd2012-07-27 14:48:13 -04001507 now = tk_xtime(tk);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001508 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultz2c6b47d2007-07-24 17:47:43 -07001509
1510 return now;
1511}
john stultz2c6b47d2007-07-24 17:47:43 -07001512EXPORT_SYMBOL(current_kernel_time);
john stultzda15cfd2009-08-19 19:13:34 -07001513
1514struct timespec get_monotonic_coarse(void)
1515{
John Stultz4e250fd2012-07-27 14:48:13 -04001516 struct timekeeper *tk = &timekeeper;
john stultzda15cfd2009-08-19 19:13:34 -07001517 struct timespec now, mono;
1518 unsigned long seq;
1519
1520 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001521 seq = read_seqcount_begin(&timekeeper_seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001522
John Stultz4e250fd2012-07-27 14:48:13 -04001523 now = tk_xtime(tk);
1524 mono = tk->wall_to_monotonic;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001525 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultzda15cfd2009-08-19 19:13:34 -07001526
1527 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
1528 now.tv_nsec + mono.tv_nsec);
1529 return now;
1530}
Torben Hohn871cf1e2011-01-27 15:58:55 +01001531
1532/*
John Stultzd6ad4182012-02-28 16:50:11 -08001533 * Must hold jiffies_lock
Torben Hohn871cf1e2011-01-27 15:58:55 +01001534 */
1535void do_timer(unsigned long ticks)
1536{
1537 jiffies_64 += ticks;
1538 update_wall_time();
1539 calc_global_load(ticks);
1540}
Torben Hohn48cf76f72011-01-27 15:59:05 +01001541
1542/**
John Stultz314ac372011-02-14 18:43:08 -08001543 * get_xtime_and_monotonic_and_sleep_offset() - get xtime, wall_to_monotonic,
1544 * and sleep offsets.
Torben Hohn48cf76f72011-01-27 15:59:05 +01001545 * @xtim: pointer to timespec to be set with xtime
1546 * @wtom: pointer to timespec to be set with wall_to_monotonic
John Stultz314ac372011-02-14 18:43:08 -08001547 * @sleep: pointer to timespec to be set with time in suspend
Torben Hohn48cf76f72011-01-27 15:59:05 +01001548 */
John Stultz314ac372011-02-14 18:43:08 -08001549void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
1550 struct timespec *wtom, struct timespec *sleep)
Torben Hohn48cf76f72011-01-27 15:59:05 +01001551{
John Stultz4e250fd2012-07-27 14:48:13 -04001552 struct timekeeper *tk = &timekeeper;
Torben Hohn48cf76f72011-01-27 15:59:05 +01001553 unsigned long seq;
1554
1555 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001556 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001557 *xtim = tk_xtime(tk);
1558 *wtom = tk->wall_to_monotonic;
1559 *sleep = tk->total_sleep_time;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001560 } while (read_seqcount_retry(&timekeeper_seq, seq));
Torben Hohn48cf76f72011-01-27 15:59:05 +01001561}
Torben Hohnf0af911a92011-01-27 15:59:10 +01001562
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001563#ifdef CONFIG_HIGH_RES_TIMERS
1564/**
1565 * ktime_get_update_offsets - hrtimer helper
1566 * @offs_real: pointer to storage for monotonic -> realtime offset
1567 * @offs_boot: pointer to storage for monotonic -> boottime offset
1568 *
1569 * Returns current monotonic time and updates the offsets
1570 * Called from hrtimer_interupt() or retrigger_next_event()
1571 */
John Stultz90adda92013-01-21 17:00:11 -08001572ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot,
1573 ktime_t *offs_tai)
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001574{
John Stultz4e250fd2012-07-27 14:48:13 -04001575 struct timekeeper *tk = &timekeeper;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001576 ktime_t now;
1577 unsigned int seq;
1578 u64 secs, nsecs;
1579
1580 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001581 seq = read_seqcount_begin(&timekeeper_seq);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001582
John Stultz4e250fd2012-07-27 14:48:13 -04001583 secs = tk->xtime_sec;
1584 nsecs = timekeeping_get_ns(tk);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001585
John Stultz4e250fd2012-07-27 14:48:13 -04001586 *offs_real = tk->offs_real;
1587 *offs_boot = tk->offs_boot;
John Stultz90adda92013-01-21 17:00:11 -08001588 *offs_tai = tk->offs_tai;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001589 } while (read_seqcount_retry(&timekeeper_seq, seq));
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001590
1591 now = ktime_add_ns(ktime_set(secs, 0), nsecs);
1592 now = ktime_sub(now, *offs_real);
1593 return now;
1594}
1595#endif
1596
Torben Hohnf0af911a92011-01-27 15:59:10 +01001597/**
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001598 * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
1599 */
1600ktime_t ktime_get_monotonic_offset(void)
1601{
John Stultz4e250fd2012-07-27 14:48:13 -04001602 struct timekeeper *tk = &timekeeper;
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001603 unsigned long seq;
1604 struct timespec wtom;
1605
1606 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001607 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001608 wtom = tk->wall_to_monotonic;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001609 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz70471f22011-11-14 12:48:10 -08001610
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001611 return timespec_to_ktime(wtom);
1612}
John Stultza80b83b2012-02-03 00:19:07 -08001613EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset);
1614
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001615/**
Torben Hohnf0af911a92011-01-27 15:59:10 +01001616 * xtime_update() - advances the timekeeping infrastructure
1617 * @ticks: number of ticks, that have elapsed since the last call.
1618 *
1619 * Must be called with interrupts disabled.
1620 */
1621void xtime_update(unsigned long ticks)
1622{
John Stultzd6ad4182012-02-28 16:50:11 -08001623 write_seqlock(&jiffies_lock);
Torben Hohnf0af911a92011-01-27 15:59:10 +01001624 do_timer(ticks);
John Stultzd6ad4182012-02-28 16:50:11 -08001625 write_sequnlock(&jiffies_lock);
Torben Hohnf0af911a92011-01-27 15:59:10 +01001626}