blob: 4c276b2d022d7d0202308c4a7a74ce0ccf7dc89d [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"
John Stultzaa6f9c592013-03-22 11:31:29 -070027#include "ntp_internal.h"
Martin Schwidefsky155ec602009-08-14 15:47:26 +020028
H Hartley Sweetenafa14e72011-01-11 17:59:38 -060029static struct timekeeper timekeeper;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +000030static DEFINE_RAW_SPINLOCK(timekeeper_lock);
31static seqcount_t timekeeper_seq;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020032
John Stultz8fcce542011-11-14 11:46:39 -080033/* flag for if timekeeping is suspended */
34int __read_mostly timekeeping_suspended;
35
Feng Tang31ade302013-01-16 00:09:47 +080036/* Flag for if there is a persistent clock on this platform */
37bool __read_mostly persistent_clock_exist = false;
38
John Stultz1e75fa82012-07-13 01:21:53 -040039static inline void tk_normalize_xtime(struct timekeeper *tk)
40{
41 while (tk->xtime_nsec >= ((u64)NSEC_PER_SEC << tk->shift)) {
42 tk->xtime_nsec -= (u64)NSEC_PER_SEC << tk->shift;
43 tk->xtime_sec++;
44 }
45}
John Stultz8fcce542011-11-14 11:46:39 -080046
John Stultz1e75fa82012-07-13 01:21:53 -040047static void tk_set_xtime(struct timekeeper *tk, const struct timespec *ts)
48{
49 tk->xtime_sec = ts->tv_sec;
John Stultzb44d50d2012-07-23 16:22:37 -040050 tk->xtime_nsec = (u64)ts->tv_nsec << tk->shift;
John Stultz1e75fa82012-07-13 01:21:53 -040051}
52
53static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts)
54{
55 tk->xtime_sec += ts->tv_sec;
John Stultzb44d50d2012-07-23 16:22:37 -040056 tk->xtime_nsec += (u64)ts->tv_nsec << tk->shift;
John Stultz784ffcb2012-08-21 20:30:46 -040057 tk_normalize_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -040058}
John Stultz8fcce542011-11-14 11:46:39 -080059
John Stultz6d0ef902012-07-27 14:48:12 -040060static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec wtm)
61{
62 struct timespec tmp;
63
64 /*
65 * Verify consistency of: offset_real = -wall_to_monotonic
66 * before modifying anything
67 */
68 set_normalized_timespec(&tmp, -tk->wall_to_monotonic.tv_sec,
69 -tk->wall_to_monotonic.tv_nsec);
70 WARN_ON_ONCE(tk->offs_real.tv64 != timespec_to_ktime(tmp).tv64);
71 tk->wall_to_monotonic = wtm;
72 set_normalized_timespec(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
73 tk->offs_real = timespec_to_ktime(tmp);
John Stultz90adda92013-01-21 17:00:11 -080074 tk->offs_tai = ktime_sub(tk->offs_real, ktime_set(tk->tai_offset, 0));
John Stultz6d0ef902012-07-27 14:48:12 -040075}
76
77static void tk_set_sleep_time(struct timekeeper *tk, struct timespec t)
78{
79 /* Verify consistency before modifying */
80 WARN_ON_ONCE(tk->offs_boot.tv64 != timespec_to_ktime(tk->total_sleep_time).tv64);
81
82 tk->total_sleep_time = t;
83 tk->offs_boot = timespec_to_ktime(t);
84}
85
Martin Schwidefsky155ec602009-08-14 15:47:26 +020086/**
87 * timekeeper_setup_internals - Set up internals to use clocksource clock.
88 *
89 * @clock: Pointer to clocksource.
90 *
91 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
92 * pair and interval request.
93 *
94 * Unless you're the timekeeping code, you should not be using this!
95 */
John Stultzf726a692012-07-13 01:21:57 -040096static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
Martin Schwidefsky155ec602009-08-14 15:47:26 +020097{
98 cycle_t interval;
Kasper Pedersena386b5a2010-10-20 15:55:15 -070099 u64 tmp, ntpinterval;
John Stultz1e75fa82012-07-13 01:21:53 -0400100 struct clocksource *old_clock;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200101
John Stultzf726a692012-07-13 01:21:57 -0400102 old_clock = tk->clock;
103 tk->clock = clock;
Thomas Gleixner14a3b6a2013-02-21 22:51:38 +0000104 tk->cycle_last = clock->cycle_last = clock->read(clock);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200105
106 /* Do the ns -> cycle conversion first, using original mult */
107 tmp = NTP_INTERVAL_LENGTH;
108 tmp <<= clock->shift;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700109 ntpinterval = tmp;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200110 tmp += clock->mult/2;
111 do_div(tmp, clock->mult);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200112 if (tmp == 0)
113 tmp = 1;
114
115 interval = (cycle_t) tmp;
John Stultzf726a692012-07-13 01:21:57 -0400116 tk->cycle_interval = interval;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200117
118 /* Go back from cycles -> shifted ns */
John Stultzf726a692012-07-13 01:21:57 -0400119 tk->xtime_interval = (u64) interval * clock->mult;
120 tk->xtime_remainder = ntpinterval - tk->xtime_interval;
121 tk->raw_interval =
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200122 ((u64) interval * clock->mult) >> clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200123
John Stultz1e75fa82012-07-13 01:21:53 -0400124 /* if changing clocks, convert xtime_nsec shift units */
125 if (old_clock) {
126 int shift_change = clock->shift - old_clock->shift;
127 if (shift_change < 0)
John Stultzf726a692012-07-13 01:21:57 -0400128 tk->xtime_nsec >>= -shift_change;
John Stultz1e75fa82012-07-13 01:21:53 -0400129 else
John Stultzf726a692012-07-13 01:21:57 -0400130 tk->xtime_nsec <<= shift_change;
John Stultz1e75fa82012-07-13 01:21:53 -0400131 }
John Stultzf726a692012-07-13 01:21:57 -0400132 tk->shift = clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200133
John Stultzf726a692012-07-13 01:21:57 -0400134 tk->ntp_error = 0;
135 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200136
137 /*
138 * The timekeeper keeps its own mult values for the currently
139 * active clocksource. These value will be adjusted via NTP
140 * to counteract clock drifting.
141 */
John Stultzf726a692012-07-13 01:21:57 -0400142 tk->mult = clock->mult;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200143}
john stultz85240702007-05-08 00:27:59 -0700144
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200145/* Timekeeper helper functions. */
Stephen Warren7b1f6202012-11-07 17:58:54 -0700146
147#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
148u32 (*arch_gettimeoffset)(void);
149
150u32 get_arch_timeoffset(void)
151{
152 if (likely(arch_gettimeoffset))
153 return arch_gettimeoffset();
154 return 0;
155}
156#else
157static inline u32 get_arch_timeoffset(void) { return 0; }
158#endif
159
John Stultzf726a692012-07-13 01:21:57 -0400160static inline s64 timekeeping_get_ns(struct timekeeper *tk)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200161{
162 cycle_t cycle_now, cycle_delta;
163 struct clocksource *clock;
John Stultz1e75fa82012-07-13 01:21:53 -0400164 s64 nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200165
166 /* read clocksource: */
John Stultzf726a692012-07-13 01:21:57 -0400167 clock = tk->clock;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200168 cycle_now = clock->read(clock);
169
170 /* calculate the delta since the last update_wall_time: */
171 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
172
John Stultzf726a692012-07-13 01:21:57 -0400173 nsec = cycle_delta * tk->mult + tk->xtime_nsec;
174 nsec >>= tk->shift;
John Stultzf2a5a082012-07-13 01:21:55 -0400175
Stephen Warren7b1f6202012-11-07 17:58:54 -0700176 /* If arch requires, add in get_arch_timeoffset() */
177 return nsec + get_arch_timeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200178}
179
John Stultzf726a692012-07-13 01:21:57 -0400180static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200181{
182 cycle_t cycle_now, cycle_delta;
183 struct clocksource *clock;
John Stultzf2a5a082012-07-13 01:21:55 -0400184 s64 nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200185
186 /* read clocksource: */
John Stultzf726a692012-07-13 01:21:57 -0400187 clock = tk->clock;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200188 cycle_now = clock->read(clock);
189
190 /* calculate the delta since the last update_wall_time: */
191 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
192
John Stultzf2a5a082012-07-13 01:21:55 -0400193 /* convert delta to nanoseconds. */
194 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
195
Stephen Warren7b1f6202012-11-07 17:58:54 -0700196 /* If arch requires, add in get_arch_timeoffset() */
197 return nsec + get_arch_timeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200198}
199
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200200static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
201
202static void update_pvclock_gtod(struct timekeeper *tk)
203{
204 raw_notifier_call_chain(&pvclock_gtod_chain, 0, tk);
205}
206
207/**
208 * pvclock_gtod_register_notifier - register a pvclock timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200209 */
210int pvclock_gtod_register_notifier(struct notifier_block *nb)
211{
212 struct timekeeper *tk = &timekeeper;
213 unsigned long flags;
214 int ret;
215
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000216 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200217 ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200218 update_pvclock_gtod(tk);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000219 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200220
221 return ret;
222}
223EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
224
225/**
226 * pvclock_gtod_unregister_notifier - unregister a pvclock
227 * timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200228 */
229int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
230{
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200231 unsigned long flags;
232 int ret;
233
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000234 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200235 ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000236 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200237
238 return ret;
239}
240EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
241
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000242/* must hold timekeeper_lock */
John Stultzf726a692012-07-13 01:21:57 -0400243static void timekeeping_update(struct timekeeper *tk, bool clearntp)
Thomas Gleixnercc062682011-11-13 23:19:49 +0000244{
245 if (clearntp) {
John Stultzf726a692012-07-13 01:21:57 -0400246 tk->ntp_error = 0;
Thomas Gleixnercc062682011-11-13 23:19:49 +0000247 ntp_clear();
248 }
John Stultz576094b2012-09-11 19:58:13 -0400249 update_vsyscall(tk);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200250 update_pvclock_gtod(tk);
Thomas Gleixnercc062682011-11-13 23:19:49 +0000251}
252
john stultz85240702007-05-08 00:27:59 -0700253/**
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200254 * timekeeping_forward_now - update clock to the current time
john stultz85240702007-05-08 00:27:59 -0700255 *
Roman Zippel9a055112008-08-20 16:37:28 -0700256 * Forward the current clock to update its state since the last call to
257 * update_wall_time(). This is useful before significant clock changes,
258 * as it avoids having to deal with this time offset explicitly.
john stultz85240702007-05-08 00:27:59 -0700259 */
John Stultzf726a692012-07-13 01:21:57 -0400260static void timekeeping_forward_now(struct timekeeper *tk)
john stultz85240702007-05-08 00:27:59 -0700261{
262 cycle_t cycle_now, cycle_delta;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200263 struct clocksource *clock;
Roman Zippel9a055112008-08-20 16:37:28 -0700264 s64 nsec;
john stultz85240702007-05-08 00:27:59 -0700265
John Stultzf726a692012-07-13 01:21:57 -0400266 clock = tk->clock;
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200267 cycle_now = clock->read(clock);
john stultz85240702007-05-08 00:27:59 -0700268 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
Thomas Gleixner14a3b6a2013-02-21 22:51:38 +0000269 tk->cycle_last = clock->cycle_last = cycle_now;
john stultz85240702007-05-08 00:27:59 -0700270
John Stultzf726a692012-07-13 01:21:57 -0400271 tk->xtime_nsec += cycle_delta * tk->mult;
john stultz7d275582009-05-01 13:10:26 -0700272
Stephen Warren7b1f6202012-11-07 17:58:54 -0700273 /* If arch requires, add in get_arch_timeoffset() */
274 tk->xtime_nsec += (u64)get_arch_timeoffset() << tk->shift;
john stultz7d275582009-05-01 13:10:26 -0700275
John Stultzf726a692012-07-13 01:21:57 -0400276 tk_normalize_xtime(tk);
John Stultz2d422442008-08-20 16:37:30 -0700277
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200278 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
John Stultzf726a692012-07-13 01:21:57 -0400279 timespec_add_ns(&tk->raw_time, nsec);
john stultz85240702007-05-08 00:27:59 -0700280}
281
282/**
Kees Cook1e817fb2012-11-19 10:26:16 -0800283 * __getnstimeofday - Returns the time of day in a timespec.
john stultz85240702007-05-08 00:27:59 -0700284 * @ts: pointer to the timespec to be set
285 *
Kees Cook1e817fb2012-11-19 10:26:16 -0800286 * Updates the time of day in the timespec.
287 * Returns 0 on success, or -ve when suspended (timespec will be undefined).
john stultz85240702007-05-08 00:27:59 -0700288 */
Kees Cook1e817fb2012-11-19 10:26:16 -0800289int __getnstimeofday(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700290{
John Stultz4e250fd2012-07-27 14:48:13 -0400291 struct timekeeper *tk = &timekeeper;
john stultz85240702007-05-08 00:27:59 -0700292 unsigned long seq;
John Stultz1e75fa82012-07-13 01:21:53 -0400293 s64 nsecs = 0;
john stultz85240702007-05-08 00:27:59 -0700294
295 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000296 seq = read_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700297
John Stultz4e250fd2012-07-27 14:48:13 -0400298 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -0400299 nsecs = timekeeping_get_ns(tk);
john stultz85240702007-05-08 00:27:59 -0700300
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000301 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultz85240702007-05-08 00:27:59 -0700302
John Stultzec145ba2012-09-11 19:26:03 -0400303 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700304 timespec_add_ns(ts, nsecs);
Kees Cook1e817fb2012-11-19 10:26:16 -0800305
306 /*
307 * Do not bail out early, in case there were callers still using
308 * the value, even in the face of the WARN_ON.
309 */
310 if (unlikely(timekeeping_suspended))
311 return -EAGAIN;
312 return 0;
313}
314EXPORT_SYMBOL(__getnstimeofday);
315
316/**
317 * getnstimeofday - Returns the time of day in a timespec.
318 * @ts: pointer to the timespec to be set
319 *
320 * Returns the time of day in a timespec (WARN if suspended).
321 */
322void getnstimeofday(struct timespec *ts)
323{
324 WARN_ON(__getnstimeofday(ts));
john stultz85240702007-05-08 00:27:59 -0700325}
john stultz85240702007-05-08 00:27:59 -0700326EXPORT_SYMBOL(getnstimeofday);
327
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200328ktime_t ktime_get(void)
329{
John Stultz4e250fd2012-07-27 14:48:13 -0400330 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200331 unsigned int seq;
332 s64 secs, nsecs;
333
334 WARN_ON(timekeeping_suspended);
335
336 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000337 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400338 secs = tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
339 nsecs = timekeeping_get_ns(tk) + tk->wall_to_monotonic.tv_nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200340
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000341 } while (read_seqcount_retry(&timekeeper_seq, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200342 /*
343 * Use ktime_set/ktime_add_ns to create a proper ktime on
344 * 32-bit architectures without CONFIG_KTIME_SCALAR.
345 */
346 return ktime_add_ns(ktime_set(secs, 0), nsecs);
347}
348EXPORT_SYMBOL_GPL(ktime_get);
349
350/**
351 * ktime_get_ts - get the monotonic clock in timespec format
352 * @ts: pointer to timespec variable
353 *
354 * The function calculates the monotonic clock from the realtime
355 * clock and the wall_to_monotonic offset and stores the result
356 * in normalized timespec format in the variable pointed to by @ts.
357 */
358void ktime_get_ts(struct timespec *ts)
359{
John Stultz4e250fd2012-07-27 14:48:13 -0400360 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200361 struct timespec tomono;
John Stultzec145ba2012-09-11 19:26:03 -0400362 s64 nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200363 unsigned int seq;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200364
365 WARN_ON(timekeeping_suspended);
366
367 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000368 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400369 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -0400370 nsec = timekeeping_get_ns(tk);
John Stultz4e250fd2012-07-27 14:48:13 -0400371 tomono = tk->wall_to_monotonic;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200372
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000373 } while (read_seqcount_retry(&timekeeper_seq, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200374
John Stultzec145ba2012-09-11 19:26:03 -0400375 ts->tv_sec += tomono.tv_sec;
376 ts->tv_nsec = 0;
377 timespec_add_ns(ts, nsec + tomono.tv_nsec);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200378}
379EXPORT_SYMBOL_GPL(ktime_get_ts);
380
John Stultz1ff3c962012-05-03 12:43:40 -0700381
382/**
383 * timekeeping_clocktai - Returns the TAI time of day in a timespec
384 * @ts: pointer to the timespec to be set
385 *
386 * Returns the time of day in a timespec.
387 */
388void timekeeping_clocktai(struct timespec *ts)
389{
390 struct timekeeper *tk = &timekeeper;
391 unsigned long seq;
392 u64 nsecs;
393
394 WARN_ON(timekeeping_suspended);
395
396 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000397 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz1ff3c962012-05-03 12:43:40 -0700398
399 ts->tv_sec = tk->xtime_sec + tk->tai_offset;
400 nsecs = timekeeping_get_ns(tk);
401
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000402 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz1ff3c962012-05-03 12:43:40 -0700403
404 ts->tv_nsec = 0;
405 timespec_add_ns(ts, nsecs);
406
407}
408EXPORT_SYMBOL(timekeeping_clocktai);
409
410
John Stultz90adda92013-01-21 17:00:11 -0800411/**
412 * ktime_get_clocktai - Returns the TAI time of day in a ktime
413 *
414 * Returns the time of day in a ktime.
415 */
416ktime_t ktime_get_clocktai(void)
417{
418 struct timespec ts;
419
420 timekeeping_clocktai(&ts);
421 return timespec_to_ktime(ts);
422}
423EXPORT_SYMBOL(ktime_get_clocktai);
424
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800425#ifdef CONFIG_NTP_PPS
426
427/**
428 * getnstime_raw_and_real - get day and raw monotonic time in timespec format
429 * @ts_raw: pointer to the timespec to be set to raw monotonic time
430 * @ts_real: pointer to the timespec to be set to the time of day
431 *
432 * This function reads both the time of day and raw monotonic time at the
433 * same time atomically and stores the resulting timestamps in timespec
434 * format.
435 */
436void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
437{
John Stultz4e250fd2012-07-27 14:48:13 -0400438 struct timekeeper *tk = &timekeeper;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800439 unsigned long seq;
440 s64 nsecs_raw, nsecs_real;
441
442 WARN_ON_ONCE(timekeeping_suspended);
443
444 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000445 seq = read_seqcount_begin(&timekeeper_seq);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800446
John Stultz4e250fd2012-07-27 14:48:13 -0400447 *ts_raw = tk->raw_time;
448 ts_real->tv_sec = tk->xtime_sec;
John Stultz1e75fa82012-07-13 01:21:53 -0400449 ts_real->tv_nsec = 0;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800450
John Stultz4e250fd2012-07-27 14:48:13 -0400451 nsecs_raw = timekeeping_get_ns_raw(tk);
452 nsecs_real = timekeeping_get_ns(tk);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800453
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000454 } while (read_seqcount_retry(&timekeeper_seq, seq));
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800455
456 timespec_add_ns(ts_raw, nsecs_raw);
457 timespec_add_ns(ts_real, nsecs_real);
458}
459EXPORT_SYMBOL(getnstime_raw_and_real);
460
461#endif /* CONFIG_NTP_PPS */
462
john stultz85240702007-05-08 00:27:59 -0700463/**
464 * do_gettimeofday - Returns the time of day in a timeval
465 * @tv: pointer to the timeval to be set
466 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100467 * NOTE: Users should be converted to using getnstimeofday()
john stultz85240702007-05-08 00:27:59 -0700468 */
469void do_gettimeofday(struct timeval *tv)
470{
471 struct timespec now;
472
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100473 getnstimeofday(&now);
john stultz85240702007-05-08 00:27:59 -0700474 tv->tv_sec = now.tv_sec;
475 tv->tv_usec = now.tv_nsec/1000;
476}
john stultz85240702007-05-08 00:27:59 -0700477EXPORT_SYMBOL(do_gettimeofday);
Richard Cochrand239f492012-04-27 10:12:42 +0200478
john stultz85240702007-05-08 00:27:59 -0700479/**
480 * do_settimeofday - Sets the time of day
481 * @tv: pointer to the timespec variable containing the new time
482 *
483 * Sets the time of day to the new time and update NTP and notify hrtimers
484 */
Richard Cochran1e6d7672011-02-01 13:50:58 +0000485int do_settimeofday(const struct timespec *tv)
john stultz85240702007-05-08 00:27:59 -0700486{
John Stultz4e250fd2012-07-27 14:48:13 -0400487 struct timekeeper *tk = &timekeeper;
John Stultz1e75fa82012-07-13 01:21:53 -0400488 struct timespec ts_delta, xt;
John Stultz92c1d3e2011-11-14 14:05:44 -0800489 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700490
John Stultzcee58482012-08-31 13:30:06 -0400491 if (!timespec_valid_strict(tv))
john stultz85240702007-05-08 00:27:59 -0700492 return -EINVAL;
493
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000494 raw_spin_lock_irqsave(&timekeeper_lock, flags);
495 write_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700496
John Stultz4e250fd2012-07-27 14:48:13 -0400497 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -0700498
John Stultz4e250fd2012-07-27 14:48:13 -0400499 xt = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -0400500 ts_delta.tv_sec = tv->tv_sec - xt.tv_sec;
501 ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec;
502
John Stultz4e250fd2012-07-27 14:48:13 -0400503 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, ts_delta));
john stultz85240702007-05-08 00:27:59 -0700504
John Stultz4e250fd2012-07-27 14:48:13 -0400505 tk_set_xtime(tk, tv);
John Stultz1e75fa82012-07-13 01:21:53 -0400506
John Stultz4e250fd2012-07-27 14:48:13 -0400507 timekeeping_update(tk, true);
john stultz85240702007-05-08 00:27:59 -0700508
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000509 write_seqcount_end(&timekeeper_seq);
510 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700511
512 /* signal hrtimers about time change */
513 clock_was_set();
514
515 return 0;
516}
john stultz85240702007-05-08 00:27:59 -0700517EXPORT_SYMBOL(do_settimeofday);
518
John Stultzc528f7c2011-02-01 13:52:17 +0000519/**
520 * timekeeping_inject_offset - Adds or subtracts from the current time.
521 * @tv: pointer to the timespec variable containing the offset
522 *
523 * Adds or subtracts an offset value from the current time.
524 */
525int timekeeping_inject_offset(struct timespec *ts)
526{
John Stultz4e250fd2012-07-27 14:48:13 -0400527 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800528 unsigned long flags;
John Stultz4e8b1452012-08-08 15:36:20 -0400529 struct timespec tmp;
530 int ret = 0;
John Stultzc528f7c2011-02-01 13:52:17 +0000531
532 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
533 return -EINVAL;
534
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000535 raw_spin_lock_irqsave(&timekeeper_lock, flags);
536 write_seqcount_begin(&timekeeper_seq);
John Stultzc528f7c2011-02-01 13:52:17 +0000537
John Stultz4e250fd2012-07-27 14:48:13 -0400538 timekeeping_forward_now(tk);
John Stultzc528f7c2011-02-01 13:52:17 +0000539
John Stultz4e8b1452012-08-08 15:36:20 -0400540 /* Make sure the proposed value is valid */
541 tmp = timespec_add(tk_xtime(tk), *ts);
John Stultzcee58482012-08-31 13:30:06 -0400542 if (!timespec_valid_strict(&tmp)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400543 ret = -EINVAL;
544 goto error;
545 }
John Stultz1e75fa82012-07-13 01:21:53 -0400546
John Stultz4e250fd2012-07-27 14:48:13 -0400547 tk_xtime_add(tk, ts);
548 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *ts));
John Stultzc528f7c2011-02-01 13:52:17 +0000549
John Stultz4e8b1452012-08-08 15:36:20 -0400550error: /* even if we error out, we forwarded the time, so call update */
John Stultz4e250fd2012-07-27 14:48:13 -0400551 timekeeping_update(tk, true);
John Stultzc528f7c2011-02-01 13:52:17 +0000552
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000553 write_seqcount_end(&timekeeper_seq);
554 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzc528f7c2011-02-01 13:52:17 +0000555
556 /* signal hrtimers about time change */
557 clock_was_set();
558
John Stultz4e8b1452012-08-08 15:36:20 -0400559 return ret;
John Stultzc528f7c2011-02-01 13:52:17 +0000560}
561EXPORT_SYMBOL(timekeeping_inject_offset);
562
John Stultzcc244dd2012-05-03 12:30:07 -0700563
564/**
565 * timekeeping_get_tai_offset - Returns current TAI offset from UTC
566 *
567 */
568s32 timekeeping_get_tai_offset(void)
569{
570 struct timekeeper *tk = &timekeeper;
571 unsigned int seq;
572 s32 ret;
573
574 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000575 seq = read_seqcount_begin(&timekeeper_seq);
John Stultzcc244dd2012-05-03 12:30:07 -0700576 ret = tk->tai_offset;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000577 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultzcc244dd2012-05-03 12:30:07 -0700578
579 return ret;
580}
581
582/**
583 * __timekeeping_set_tai_offset - Lock free worker function
584 *
585 */
Fengguang Wudd5d70e82013-03-25 12:24:24 -0700586static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
John Stultzcc244dd2012-05-03 12:30:07 -0700587{
588 tk->tai_offset = tai_offset;
John Stultz90adda92013-01-21 17:00:11 -0800589 tk->offs_tai = ktime_sub(tk->offs_real, ktime_set(tai_offset, 0));
John Stultzcc244dd2012-05-03 12:30:07 -0700590}
591
592/**
593 * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
594 *
595 */
596void timekeeping_set_tai_offset(s32 tai_offset)
597{
598 struct timekeeper *tk = &timekeeper;
599 unsigned long flags;
600
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000601 raw_spin_lock_irqsave(&timekeeper_lock, flags);
602 write_seqcount_begin(&timekeeper_seq);
John Stultzcc244dd2012-05-03 12:30:07 -0700603 __timekeeping_set_tai_offset(tk, tai_offset);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000604 write_seqcount_end(&timekeeper_seq);
605 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzcc244dd2012-05-03 12:30:07 -0700606}
607
john stultz85240702007-05-08 00:27:59 -0700608/**
609 * change_clocksource - Swaps clocksources if a new one is available
610 *
611 * Accumulates current time interval and initializes new clocksource
612 */
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200613static int change_clocksource(void *data)
john stultz85240702007-05-08 00:27:59 -0700614{
John Stultz4e250fd2012-07-27 14:48:13 -0400615 struct timekeeper *tk = &timekeeper;
Magnus Damm4614e6a2009-04-21 12:24:02 -0700616 struct clocksource *new, *old;
John Stultzf695cf92012-03-14 16:38:15 -0700617 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700618
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200619 new = (struct clocksource *) data;
john stultz85240702007-05-08 00:27:59 -0700620
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000621 raw_spin_lock_irqsave(&timekeeper_lock, flags);
622 write_seqcount_begin(&timekeeper_seq);
John Stultzf695cf92012-03-14 16:38:15 -0700623
John Stultz4e250fd2012-07-27 14:48:13 -0400624 timekeeping_forward_now(tk);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200625 if (!new->enable || new->enable(new) == 0) {
John Stultz4e250fd2012-07-27 14:48:13 -0400626 old = tk->clock;
627 tk_setup_internals(tk, new);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200628 if (old->disable)
629 old->disable(old);
630 }
John Stultz4e250fd2012-07-27 14:48:13 -0400631 timekeeping_update(tk, true);
John Stultzf695cf92012-03-14 16:38:15 -0700632
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000633 write_seqcount_end(&timekeeper_seq);
634 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzf695cf92012-03-14 16:38:15 -0700635
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200636 return 0;
637}
john stultz85240702007-05-08 00:27:59 -0700638
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200639/**
640 * timekeeping_notify - Install a new clock source
641 * @clock: pointer to the clock source
642 *
643 * This function is called from clocksource.c after a new, better clock
644 * source has been registered. The caller holds the clocksource_mutex.
645 */
646void timekeeping_notify(struct clocksource *clock)
647{
John Stultz4e250fd2012-07-27 14:48:13 -0400648 struct timekeeper *tk = &timekeeper;
649
650 if (tk->clock == clock)
Magnus Damm4614e6a2009-04-21 12:24:02 -0700651 return;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200652 stop_machine(change_clocksource, clock, NULL);
john stultz85240702007-05-08 00:27:59 -0700653 tick_clock_notify();
john stultz85240702007-05-08 00:27:59 -0700654}
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200655
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200656/**
657 * ktime_get_real - get the real (wall-) time in ktime_t format
658 *
659 * returns the time in ktime_t format
660 */
661ktime_t ktime_get_real(void)
662{
663 struct timespec now;
664
665 getnstimeofday(&now);
666
667 return timespec_to_ktime(now);
668}
669EXPORT_SYMBOL_GPL(ktime_get_real);
john stultz85240702007-05-08 00:27:59 -0700670
671/**
John Stultz2d422442008-08-20 16:37:30 -0700672 * getrawmonotonic - Returns the raw monotonic time in a timespec
673 * @ts: pointer to the timespec to be set
674 *
675 * Returns the raw monotonic time (completely un-modified by ntp)
676 */
677void getrawmonotonic(struct timespec *ts)
678{
John Stultz4e250fd2012-07-27 14:48:13 -0400679 struct timekeeper *tk = &timekeeper;
John Stultz2d422442008-08-20 16:37:30 -0700680 unsigned long seq;
681 s64 nsecs;
John Stultz2d422442008-08-20 16:37:30 -0700682
683 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000684 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400685 nsecs = timekeeping_get_ns_raw(tk);
686 *ts = tk->raw_time;
John Stultz2d422442008-08-20 16:37:30 -0700687
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000688 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz2d422442008-08-20 16:37:30 -0700689
690 timespec_add_ns(ts, nsecs);
691}
692EXPORT_SYMBOL(getrawmonotonic);
693
John Stultz2d422442008-08-20 16:37:30 -0700694/**
Li Zefancf4fc6c2008-02-08 04:19:24 -0800695 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -0700696 */
Li Zefancf4fc6c2008-02-08 04:19:24 -0800697int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -0700698{
John Stultz4e250fd2012-07-27 14:48:13 -0400699 struct timekeeper *tk = &timekeeper;
john stultz85240702007-05-08 00:27:59 -0700700 unsigned long seq;
701 int ret;
702
703 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000704 seq = read_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700705
John Stultz4e250fd2012-07-27 14:48:13 -0400706 ret = tk->clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -0700707
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000708 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultz85240702007-05-08 00:27:59 -0700709
710 return ret;
711}
712
713/**
Jon Hunter98962462009-08-18 12:45:10 -0500714 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
Jon Hunter98962462009-08-18 12:45:10 -0500715 */
716u64 timekeeping_max_deferment(void)
717{
John Stultz4e250fd2012-07-27 14:48:13 -0400718 struct timekeeper *tk = &timekeeper;
John Stultz70471f22011-11-14 12:48:10 -0800719 unsigned long seq;
720 u64 ret;
John Stultz42e71e82012-07-13 01:21:51 -0400721
John Stultz70471f22011-11-14 12:48:10 -0800722 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000723 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz70471f22011-11-14 12:48:10 -0800724
John Stultz4e250fd2012-07-27 14:48:13 -0400725 ret = tk->clock->max_idle_ns;
John Stultz70471f22011-11-14 12:48:10 -0800726
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000727 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz70471f22011-11-14 12:48:10 -0800728
729 return ret;
Jon Hunter98962462009-08-18 12:45:10 -0500730}
731
732/**
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200733 * read_persistent_clock - Return time from the persistent clock.
john stultz85240702007-05-08 00:27:59 -0700734 *
735 * Weak dummy function for arches that do not yet support it.
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200736 * Reads the time from the battery backed persistent clock.
737 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
john stultz85240702007-05-08 00:27:59 -0700738 *
739 * XXX - Do be sure to remove it once all arches implement it.
740 */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200741void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700742{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200743 ts->tv_sec = 0;
744 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700745}
746
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200747/**
748 * read_boot_clock - Return time of the system start.
749 *
750 * Weak dummy function for arches that do not yet support it.
751 * Function to read the exact time the system has been started.
752 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
753 *
754 * XXX - Do be sure to remove it once all arches implement it.
755 */
756void __attribute__((weak)) read_boot_clock(struct timespec *ts)
757{
758 ts->tv_sec = 0;
759 ts->tv_nsec = 0;
760}
761
john stultz85240702007-05-08 00:27:59 -0700762/*
763 * timekeeping_init - Initializes the clocksource and common timekeeping values
764 */
765void __init timekeeping_init(void)
766{
John Stultz4e250fd2012-07-27 14:48:13 -0400767 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200768 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700769 unsigned long flags;
John Stultz6d0ef902012-07-27 14:48:12 -0400770 struct timespec now, boot, tmp;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200771
772 read_persistent_clock(&now);
Feng Tang31ade302013-01-16 00:09:47 +0800773
John Stultzcee58482012-08-31 13:30:06 -0400774 if (!timespec_valid_strict(&now)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400775 pr_warn("WARNING: Persistent clock returned invalid value!\n"
776 " Check your CMOS/BIOS settings.\n");
777 now.tv_sec = 0;
778 now.tv_nsec = 0;
Feng Tang31ade302013-01-16 00:09:47 +0800779 } else if (now.tv_sec || now.tv_nsec)
780 persistent_clock_exist = true;
John Stultz4e8b1452012-08-08 15:36:20 -0400781
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200782 read_boot_clock(&boot);
John Stultzcee58482012-08-31 13:30:06 -0400783 if (!timespec_valid_strict(&boot)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400784 pr_warn("WARNING: Boot clock returned invalid value!\n"
785 " Check your CMOS/BIOS settings.\n");
786 boot.tv_sec = 0;
787 boot.tv_nsec = 0;
788 }
john stultz85240702007-05-08 00:27:59 -0700789
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000790 raw_spin_lock_irqsave(&timekeeper_lock, flags);
791 write_seqcount_begin(&timekeeper_seq);
John Stultz06c017f2013-03-22 11:37:28 -0700792 ntp_init();
793
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200794 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200795 if (clock->enable)
796 clock->enable(clock);
John Stultz4e250fd2012-07-27 14:48:13 -0400797 tk_setup_internals(tk, clock);
john stultz85240702007-05-08 00:27:59 -0700798
John Stultz4e250fd2012-07-27 14:48:13 -0400799 tk_set_xtime(tk, &now);
800 tk->raw_time.tv_sec = 0;
801 tk->raw_time.tv_nsec = 0;
John Stultz1e75fa82012-07-13 01:21:53 -0400802 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
John Stultz4e250fd2012-07-27 14:48:13 -0400803 boot = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -0400804
John Stultz6d0ef902012-07-27 14:48:12 -0400805 set_normalized_timespec(&tmp, -boot.tv_sec, -boot.tv_nsec);
John Stultz4e250fd2012-07-27 14:48:13 -0400806 tk_set_wall_to_mono(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -0400807
808 tmp.tv_sec = 0;
809 tmp.tv_nsec = 0;
John Stultz4e250fd2012-07-27 14:48:13 -0400810 tk_set_sleep_time(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -0400811
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000812 write_seqcount_end(&timekeeper_seq);
813 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700814}
815
john stultz85240702007-05-08 00:27:59 -0700816/* time in seconds when suspend began */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200817static struct timespec timekeeping_suspend_time;
john stultz85240702007-05-08 00:27:59 -0700818
819/**
John Stultz304529b2011-04-01 14:32:09 -0700820 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
821 * @delta: pointer to a timespec delta value
822 *
823 * Takes a timespec offset measuring a suspend interval and properly
824 * adds the sleep offset to the timekeeping variables.
825 */
John Stultzf726a692012-07-13 01:21:57 -0400826static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
827 struct timespec *delta)
John Stultz304529b2011-04-01 14:32:09 -0700828{
John Stultzcee58482012-08-31 13:30:06 -0400829 if (!timespec_valid_strict(delta)) {
John Stultzcbaa5152011-07-20 15:42:55 -0700830 printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid "
John Stultzcb5de2f8d2011-06-01 18:18:09 -0700831 "sleep delta value!\n");
832 return;
833 }
John Stultzf726a692012-07-13 01:21:57 -0400834 tk_xtime_add(tk, delta);
John Stultz6d0ef902012-07-27 14:48:12 -0400835 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *delta));
836 tk_set_sleep_time(tk, timespec_add(tk->total_sleep_time, *delta));
John Stultz304529b2011-04-01 14:32:09 -0700837}
838
John Stultz304529b2011-04-01 14:32:09 -0700839/**
840 * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
841 * @delta: pointer to a timespec delta value
842 *
843 * This hook is for architectures that cannot support read_persistent_clock
844 * because their RTC/persistent clock is only accessible when irqs are enabled.
845 *
846 * This function should only be called by rtc_resume(), and allows
847 * a suspend offset to be injected into the timekeeping values.
848 */
849void timekeeping_inject_sleeptime(struct timespec *delta)
850{
John Stultz4e250fd2012-07-27 14:48:13 -0400851 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800852 unsigned long flags;
John Stultz304529b2011-04-01 14:32:09 -0700853
Feng Tang31ade302013-01-16 00:09:47 +0800854 /*
855 * Make sure we don't set the clock twice, as timekeeping_resume()
856 * already did it
857 */
858 if (has_persistent_clock())
John Stultz304529b2011-04-01 14:32:09 -0700859 return;
860
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000861 raw_spin_lock_irqsave(&timekeeper_lock, flags);
862 write_seqcount_begin(&timekeeper_seq);
John Stultz70471f22011-11-14 12:48:10 -0800863
John Stultz4e250fd2012-07-27 14:48:13 -0400864 timekeeping_forward_now(tk);
John Stultz304529b2011-04-01 14:32:09 -0700865
John Stultz4e250fd2012-07-27 14:48:13 -0400866 __timekeeping_inject_sleeptime(tk, delta);
John Stultz304529b2011-04-01 14:32:09 -0700867
John Stultz4e250fd2012-07-27 14:48:13 -0400868 timekeeping_update(tk, true);
John Stultz304529b2011-04-01 14:32:09 -0700869
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000870 write_seqcount_end(&timekeeper_seq);
871 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz304529b2011-04-01 14:32:09 -0700872
873 /* signal hrtimers about time change */
874 clock_was_set();
875}
876
John Stultz304529b2011-04-01 14:32:09 -0700877/**
john stultz85240702007-05-08 00:27:59 -0700878 * timekeeping_resume - Resumes the generic timekeeping subsystem.
john stultz85240702007-05-08 00:27:59 -0700879 *
880 * This is for the generic clocksource timekeeping.
881 * xtime/wall_to_monotonic/jiffies/etc are
882 * still managed by arch specific suspend/resume code.
883 */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +0100884static void timekeeping_resume(void)
john stultz85240702007-05-08 00:27:59 -0700885{
John Stultz4e250fd2012-07-27 14:48:13 -0400886 struct timekeeper *tk = &timekeeper;
Feng Tange445cf12013-03-12 11:56:48 +0800887 struct clocksource *clock = tk->clock;
John Stultz92c1d3e2011-11-14 14:05:44 -0800888 unsigned long flags;
Feng Tange445cf12013-03-12 11:56:48 +0800889 struct timespec ts_new, ts_delta;
890 cycle_t cycle_now, cycle_delta;
891 bool suspendtime_found = false;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200892
Feng Tange445cf12013-03-12 11:56:48 +0800893 read_persistent_clock(&ts_new);
john stultz85240702007-05-08 00:27:59 -0700894
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200895 clockevents_resume();
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +0200896 clocksource_resume();
897
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000898 raw_spin_lock_irqsave(&timekeeper_lock, flags);
899 write_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700900
Feng Tange445cf12013-03-12 11:56:48 +0800901 /*
902 * After system resumes, we need to calculate the suspended time and
903 * compensate it for the OS time. There are 3 sources that could be
904 * used: Nonstop clocksource during suspend, persistent clock and rtc
905 * device.
906 *
907 * One specific platform may have 1 or 2 or all of them, and the
908 * preference will be:
909 * suspend-nonstop clocksource -> persistent clock -> rtc
910 * The less preferred source will only be tried if there is no better
911 * usable source. The rtc part is handled separately in rtc core code.
912 */
913 cycle_now = clock->read(clock);
914 if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
915 cycle_now > clock->cycle_last) {
916 u64 num, max = ULLONG_MAX;
917 u32 mult = clock->mult;
918 u32 shift = clock->shift;
919 s64 nsec = 0;
920
921 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
922
923 /*
924 * "cycle_delta * mutl" may cause 64 bits overflow, if the
925 * suspended time is too long. In that case we need do the
926 * 64 bits math carefully
927 */
928 do_div(max, mult);
929 if (cycle_delta > max) {
930 num = div64_u64(cycle_delta, max);
931 nsec = (((u64) max * mult) >> shift) * num;
932 cycle_delta -= num * max;
933 }
934 nsec += ((u64) cycle_delta * mult) >> shift;
935
936 ts_delta = ns_to_timespec(nsec);
937 suspendtime_found = true;
938 } else if (timespec_compare(&ts_new, &timekeeping_suspend_time) > 0) {
939 ts_delta = timespec_sub(ts_new, timekeeping_suspend_time);
940 suspendtime_found = true;
john stultz85240702007-05-08 00:27:59 -0700941 }
Feng Tange445cf12013-03-12 11:56:48 +0800942
943 if (suspendtime_found)
944 __timekeeping_inject_sleeptime(tk, &ts_delta);
945
946 /* Re-base the last cycle value */
947 clock->cycle_last = cycle_now;
John Stultz4e250fd2012-07-27 14:48:13 -0400948 tk->ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -0700949 timekeeping_suspended = 0;
John Stultz4e250fd2012-07-27 14:48:13 -0400950 timekeeping_update(tk, false);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000951 write_seqcount_end(&timekeeper_seq);
952 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700953
954 touch_softlockup_watchdog();
955
956 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
957
958 /* Resume hrtimers */
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200959 hrtimers_resume();
john stultz85240702007-05-08 00:27:59 -0700960}
961
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +0100962static int timekeeping_suspend(void)
john stultz85240702007-05-08 00:27:59 -0700963{
John Stultz4e250fd2012-07-27 14:48:13 -0400964 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800965 unsigned long flags;
John Stultzcb332172011-05-31 22:53:23 -0700966 struct timespec delta, delta_delta;
967 static struct timespec old_delta;
john stultz85240702007-05-08 00:27:59 -0700968
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200969 read_persistent_clock(&timekeeping_suspend_time);
Thomas Gleixner3be90952007-09-16 15:36:43 +0200970
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000971 raw_spin_lock_irqsave(&timekeeper_lock, flags);
972 write_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400973 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -0700974 timekeeping_suspended = 1;
John Stultzcb332172011-05-31 22:53:23 -0700975
976 /*
977 * To avoid drift caused by repeated suspend/resumes,
978 * which each can add ~1 second drift error,
979 * try to compensate so the difference in system time
980 * and persistent_clock time stays close to constant.
981 */
John Stultz4e250fd2012-07-27 14:48:13 -0400982 delta = timespec_sub(tk_xtime(tk), timekeeping_suspend_time);
John Stultzcb332172011-05-31 22:53:23 -0700983 delta_delta = timespec_sub(delta, old_delta);
984 if (abs(delta_delta.tv_sec) >= 2) {
985 /*
986 * if delta_delta is too large, assume time correction
987 * has occured and set old_delta to the current delta.
988 */
989 old_delta = delta;
990 } else {
991 /* Otherwise try to adjust old_system to compensate */
992 timekeeping_suspend_time =
993 timespec_add(timekeeping_suspend_time, delta_delta);
994 }
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000995 write_seqcount_end(&timekeeper_seq);
996 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700997
998 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
Magnus Dammc54a42b2010-02-02 14:41:41 -0800999 clocksource_suspend();
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +02001000 clockevents_suspend();
john stultz85240702007-05-08 00:27:59 -07001001
1002 return 0;
1003}
1004
1005/* sysfs resume/suspend bits for timekeeping */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001006static struct syscore_ops timekeeping_syscore_ops = {
john stultz85240702007-05-08 00:27:59 -07001007 .resume = timekeeping_resume,
1008 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -07001009};
1010
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001011static int __init timekeeping_init_ops(void)
john stultz85240702007-05-08 00:27:59 -07001012{
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001013 register_syscore_ops(&timekeeping_syscore_ops);
1014 return 0;
john stultz85240702007-05-08 00:27:59 -07001015}
1016
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001017device_initcall(timekeeping_init_ops);
john stultz85240702007-05-08 00:27:59 -07001018
1019/*
1020 * If the error is already larger, we look ahead even further
1021 * to compensate for late or lost adjustments.
1022 */
John Stultzf726a692012-07-13 01:21:57 -04001023static __always_inline int timekeeping_bigadjust(struct timekeeper *tk,
1024 s64 error, s64 *interval,
john stultz85240702007-05-08 00:27:59 -07001025 s64 *offset)
1026{
1027 s64 tick_error, i;
1028 u32 look_ahead, adj;
1029 s32 error2, mult;
1030
1031 /*
1032 * Use the current error value to determine how much to look ahead.
1033 * The larger the error the slower we adjust for it to avoid problems
1034 * with losing too many ticks, otherwise we would overadjust and
1035 * produce an even larger error. The smaller the adjustment the
1036 * faster we try to adjust for it, as lost ticks can do less harm
Li Zefan3eb05672008-02-08 04:19:25 -08001037 * here. This is tuned so that an error of about 1 msec is adjusted
john stultz85240702007-05-08 00:27:59 -07001038 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
1039 */
John Stultzf726a692012-07-13 01:21:57 -04001040 error2 = tk->ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
john stultz85240702007-05-08 00:27:59 -07001041 error2 = abs(error2);
1042 for (look_ahead = 0; error2 > 0; look_ahead++)
1043 error2 >>= 2;
1044
1045 /*
1046 * Now calculate the error in (1 << look_ahead) ticks, but first
1047 * remove the single look ahead already included in the error.
1048 */
John Stultzf726a692012-07-13 01:21:57 -04001049 tick_error = ntp_tick_length() >> (tk->ntp_error_shift + 1);
1050 tick_error -= tk->xtime_interval >> 1;
john stultz85240702007-05-08 00:27:59 -07001051 error = ((error - tick_error) >> look_ahead) + tick_error;
1052
1053 /* Finally calculate the adjustment shift value. */
1054 i = *interval;
1055 mult = 1;
1056 if (error < 0) {
1057 error = -error;
1058 *interval = -*interval;
1059 *offset = -*offset;
1060 mult = -1;
1061 }
1062 for (adj = 0; error > i; adj++)
1063 error >>= 1;
1064
1065 *interval <<= adj;
1066 *offset <<= adj;
1067 return mult << adj;
1068}
1069
1070/*
1071 * Adjust the multiplier to reduce the error value,
1072 * this is optimized for the most common adjustments of -1,0,1,
1073 * for other values we can do a bit more work.
1074 */
John Stultzf726a692012-07-13 01:21:57 -04001075static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
john stultz85240702007-05-08 00:27:59 -07001076{
John Stultzf726a692012-07-13 01:21:57 -04001077 s64 error, interval = tk->cycle_interval;
john stultz85240702007-05-08 00:27:59 -07001078 int adj;
1079
John Stultzc2bc1112011-10-27 18:12:42 -07001080 /*
Jim Cromie88b28ad2012-03-14 21:28:56 -06001081 * The point of this is to check if the error is greater than half
John Stultzc2bc1112011-10-27 18:12:42 -07001082 * an interval.
1083 *
1084 * First we shift it down from NTP_SHIFT to clocksource->shifted nsecs.
1085 *
1086 * Note we subtract one in the shift, so that error is really error*2.
John Stultz3f86f282011-10-27 17:41:17 -07001087 * This "saves" dividing(shifting) interval twice, but keeps the
1088 * (error > interval) comparison as still measuring if error is
Jim Cromie88b28ad2012-03-14 21:28:56 -06001089 * larger than half an interval.
John Stultzc2bc1112011-10-27 18:12:42 -07001090 *
John Stultz3f86f282011-10-27 17:41:17 -07001091 * Note: It does not "save" on aggravation when reading the code.
John Stultzc2bc1112011-10-27 18:12:42 -07001092 */
John Stultzf726a692012-07-13 01:21:57 -04001093 error = tk->ntp_error >> (tk->ntp_error_shift - 1);
john stultz85240702007-05-08 00:27:59 -07001094 if (error > interval) {
John Stultzc2bc1112011-10-27 18:12:42 -07001095 /*
1096 * We now divide error by 4(via shift), which checks if
Jim Cromie88b28ad2012-03-14 21:28:56 -06001097 * the error is greater than twice the interval.
John Stultzc2bc1112011-10-27 18:12:42 -07001098 * If it is greater, we need a bigadjust, if its smaller,
1099 * we can adjust by 1.
1100 */
john stultz85240702007-05-08 00:27:59 -07001101 error >>= 2;
John Stultzc2bc1112011-10-27 18:12:42 -07001102 /*
1103 * XXX - In update_wall_time, we round up to the next
1104 * nanosecond, and store the amount rounded up into
1105 * the error. This causes the likely below to be unlikely.
1106 *
John Stultz3f86f282011-10-27 17:41:17 -07001107 * The proper fix is to avoid rounding up by using
John Stultz4e250fd2012-07-27 14:48:13 -04001108 * the high precision tk->xtime_nsec instead of
John Stultzc2bc1112011-10-27 18:12:42 -07001109 * xtime.tv_nsec everywhere. Fixing this will take some
1110 * time.
1111 */
john stultz85240702007-05-08 00:27:59 -07001112 if (likely(error <= interval))
1113 adj = 1;
1114 else
Ingo Molnar1d17d172012-08-04 21:21:14 +02001115 adj = timekeeping_bigadjust(tk, error, &interval, &offset);
1116 } else {
1117 if (error < -interval) {
1118 /* See comment above, this is just switched for the negative */
1119 error >>= 2;
1120 if (likely(error >= -interval)) {
1121 adj = -1;
1122 interval = -interval;
1123 offset = -offset;
1124 } else {
1125 adj = timekeeping_bigadjust(tk, error, &interval, &offset);
1126 }
1127 } else {
1128 goto out_adjust;
1129 }
1130 }
john stultz85240702007-05-08 00:27:59 -07001131
John Stultzf726a692012-07-13 01:21:57 -04001132 if (unlikely(tk->clock->maxadj &&
1133 (tk->mult + adj > tk->clock->mult + tk->clock->maxadj))) {
John Stultze919cfd2012-03-22 19:14:46 -07001134 printk_once(KERN_WARNING
1135 "Adjusting %s more than 11%% (%ld vs %ld)\n",
John Stultzf726a692012-07-13 01:21:57 -04001136 tk->clock->name, (long)tk->mult + adj,
1137 (long)tk->clock->mult + tk->clock->maxadj);
John Stultze919cfd2012-03-22 19:14:46 -07001138 }
John Stultzc2bc1112011-10-27 18:12:42 -07001139 /*
1140 * So the following can be confusing.
1141 *
1142 * To keep things simple, lets assume adj == 1 for now.
1143 *
1144 * When adj != 1, remember that the interval and offset values
1145 * have been appropriately scaled so the math is the same.
1146 *
1147 * The basic idea here is that we're increasing the multiplier
1148 * by one, this causes the xtime_interval to be incremented by
1149 * one cycle_interval. This is because:
1150 * xtime_interval = cycle_interval * mult
1151 * So if mult is being incremented by one:
1152 * xtime_interval = cycle_interval * (mult + 1)
1153 * Its the same as:
1154 * xtime_interval = (cycle_interval * mult) + cycle_interval
1155 * Which can be shortened to:
1156 * xtime_interval += cycle_interval
1157 *
1158 * So offset stores the non-accumulated cycles. Thus the current
1159 * time (in shifted nanoseconds) is:
1160 * now = (offset * adj) + xtime_nsec
1161 * Now, even though we're adjusting the clock frequency, we have
1162 * to keep time consistent. In other words, we can't jump back
1163 * in time, and we also want to avoid jumping forward in time.
1164 *
1165 * So given the same offset value, we need the time to be the same
1166 * both before and after the freq adjustment.
1167 * now = (offset * adj_1) + xtime_nsec_1
1168 * now = (offset * adj_2) + xtime_nsec_2
1169 * So:
1170 * (offset * adj_1) + xtime_nsec_1 =
1171 * (offset * adj_2) + xtime_nsec_2
1172 * And we know:
1173 * adj_2 = adj_1 + 1
1174 * So:
1175 * (offset * adj_1) + xtime_nsec_1 =
1176 * (offset * (adj_1+1)) + xtime_nsec_2
1177 * (offset * adj_1) + xtime_nsec_1 =
1178 * (offset * adj_1) + offset + xtime_nsec_2
1179 * Canceling the sides:
1180 * xtime_nsec_1 = offset + xtime_nsec_2
1181 * Which gives us:
1182 * xtime_nsec_2 = xtime_nsec_1 - offset
1183 * Which simplfies to:
1184 * xtime_nsec -= offset
1185 *
1186 * XXX - TODO: Doc ntp_error calculation.
1187 */
John Stultzf726a692012-07-13 01:21:57 -04001188 tk->mult += adj;
1189 tk->xtime_interval += interval;
1190 tk->xtime_nsec -= offset;
1191 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -04001192
Ingo Molnar1d17d172012-08-04 21:21:14 +02001193out_adjust:
John Stultz2a8c0882012-07-13 01:21:56 -04001194 /*
1195 * It may be possible that when we entered this function, xtime_nsec
1196 * was very small. Further, if we're slightly speeding the clocksource
1197 * in the code above, its possible the required corrective factor to
1198 * xtime_nsec could cause it to underflow.
1199 *
1200 * Now, since we already accumulated the second, cannot simply roll
1201 * the accumulated second back, since the NTP subsystem has been
1202 * notified via second_overflow. So instead we push xtime_nsec forward
1203 * by the amount we underflowed, and add that amount into the error.
1204 *
1205 * We'll correct this error next time through this function, when
1206 * xtime_nsec is not as small.
1207 */
John Stultzf726a692012-07-13 01:21:57 -04001208 if (unlikely((s64)tk->xtime_nsec < 0)) {
1209 s64 neg = -(s64)tk->xtime_nsec;
1210 tk->xtime_nsec = 0;
1211 tk->ntp_error += neg << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -04001212 }
1213
john stultz85240702007-05-08 00:27:59 -07001214}
1215
1216/**
John Stultz1f4f9482012-07-13 01:21:54 -04001217 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
1218 *
1219 * Helper function that accumulates a the nsecs greater then a second
1220 * from the xtime_nsec field to the xtime_secs field.
1221 * It also calls into the NTP code to handle leapsecond processing.
1222 *
1223 */
1224static inline void accumulate_nsecs_to_secs(struct timekeeper *tk)
1225{
1226 u64 nsecps = (u64)NSEC_PER_SEC << tk->shift;
1227
1228 while (tk->xtime_nsec >= nsecps) {
1229 int leap;
1230
1231 tk->xtime_nsec -= nsecps;
1232 tk->xtime_sec++;
1233
1234 /* Figure out if its a leap sec and apply if needed */
1235 leap = second_overflow(tk->xtime_sec);
John Stultz6d0ef902012-07-27 14:48:12 -04001236 if (unlikely(leap)) {
1237 struct timespec ts;
John Stultz1f4f9482012-07-13 01:21:54 -04001238
John Stultz6d0ef902012-07-27 14:48:12 -04001239 tk->xtime_sec += leap;
1240
1241 ts.tv_sec = leap;
1242 ts.tv_nsec = 0;
1243 tk_set_wall_to_mono(tk,
1244 timespec_sub(tk->wall_to_monotonic, ts));
1245
John Stultzcc244dd2012-05-03 12:30:07 -07001246 __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
1247
John Stultz6d0ef902012-07-27 14:48:12 -04001248 clock_was_set_delayed();
1249 }
John Stultz1f4f9482012-07-13 01:21:54 -04001250 }
1251}
1252
John Stultz1f4f9482012-07-13 01:21:54 -04001253/**
john stultza092ff02009-10-02 16:17:53 -07001254 * logarithmic_accumulation - shifted accumulation of cycles
1255 *
1256 * This functions accumulates a shifted interval of cycles into
1257 * into a shifted interval nanoseconds. Allows for O(log) accumulation
1258 * loop.
1259 *
1260 * Returns the unconsumed cycles.
1261 */
John Stultzf726a692012-07-13 01:21:57 -04001262static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
1263 u32 shift)
john stultza092ff02009-10-02 16:17:53 -07001264{
Thomas Gleixner23a95372013-02-21 22:51:36 +00001265 cycle_t interval = tk->cycle_interval << shift;
Jason Wesseldeda2e82010-08-09 14:20:09 -07001266 u64 raw_nsecs;
john stultza092ff02009-10-02 16:17:53 -07001267
John Stultzf726a692012-07-13 01:21:57 -04001268 /* If the offset is smaller then a shifted interval, do nothing */
Thomas Gleixner23a95372013-02-21 22:51:36 +00001269 if (offset < interval)
john stultza092ff02009-10-02 16:17:53 -07001270 return offset;
1271
1272 /* Accumulate one shifted interval */
Thomas Gleixner23a95372013-02-21 22:51:36 +00001273 offset -= interval;
1274 tk->clock->cycle_last += interval;
john stultza092ff02009-10-02 16:17:53 -07001275
John Stultzf726a692012-07-13 01:21:57 -04001276 tk->xtime_nsec += tk->xtime_interval << shift;
1277 accumulate_nsecs_to_secs(tk);
john stultza092ff02009-10-02 16:17:53 -07001278
Jason Wesseldeda2e82010-08-09 14:20:09 -07001279 /* Accumulate raw time */
Dan Carpenter5b3900c2012-10-09 10:18:23 +03001280 raw_nsecs = (u64)tk->raw_interval << shift;
John Stultzf726a692012-07-13 01:21:57 -04001281 raw_nsecs += tk->raw_time.tv_nsec;
John Stultzc7dcf872010-08-13 11:30:58 -07001282 if (raw_nsecs >= NSEC_PER_SEC) {
1283 u64 raw_secs = raw_nsecs;
1284 raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
John Stultzf726a692012-07-13 01:21:57 -04001285 tk->raw_time.tv_sec += raw_secs;
john stultza092ff02009-10-02 16:17:53 -07001286 }
John Stultzf726a692012-07-13 01:21:57 -04001287 tk->raw_time.tv_nsec = raw_nsecs;
john stultza092ff02009-10-02 16:17:53 -07001288
1289 /* Accumulate error between NTP and clock interval */
John Stultzf726a692012-07-13 01:21:57 -04001290 tk->ntp_error += ntp_tick_length() << shift;
1291 tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
1292 (tk->ntp_error_shift + shift);
john stultza092ff02009-10-02 16:17:53 -07001293
1294 return offset;
1295}
1296
John Stultz92bb1fc2012-09-04 15:38:12 -04001297#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
1298static inline void old_vsyscall_fixup(struct timekeeper *tk)
1299{
1300 s64 remainder;
1301
1302 /*
1303 * Store only full nanoseconds into xtime_nsec after rounding
1304 * it up and add the remainder to the error difference.
1305 * XXX - This is necessary to avoid small 1ns inconsistnecies caused
1306 * by truncating the remainder in vsyscalls. However, it causes
1307 * additional work to be done in timekeeping_adjust(). Once
1308 * the vsyscall implementations are converted to use xtime_nsec
1309 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
1310 * users are removed, this can be killed.
1311 */
1312 remainder = tk->xtime_nsec & ((1ULL << tk->shift) - 1);
1313 tk->xtime_nsec -= remainder;
1314 tk->xtime_nsec += 1ULL << tk->shift;
1315 tk->ntp_error += remainder << tk->ntp_error_shift;
1316
1317}
1318#else
1319#define old_vsyscall_fixup(tk)
1320#endif
1321
1322
1323
john stultz85240702007-05-08 00:27:59 -07001324/**
1325 * update_wall_time - Uses the current clocksource to increment the wall time
1326 *
john stultz85240702007-05-08 00:27:59 -07001327 */
Torben Hohn871cf1e2011-01-27 15:58:55 +01001328static void update_wall_time(void)
john stultz85240702007-05-08 00:27:59 -07001329{
Martin Schwidefsky155ec602009-08-14 15:47:26 +02001330 struct clocksource *clock;
John Stultz4e250fd2012-07-27 14:48:13 -04001331 struct timekeeper *tk = &timekeeper;
john stultz85240702007-05-08 00:27:59 -07001332 cycle_t offset;
john stultza092ff02009-10-02 16:17:53 -07001333 int shift = 0, maxshift;
John Stultz70471f22011-11-14 12:48:10 -08001334 unsigned long flags;
1335
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001336 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1337 write_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -07001338
1339 /* Make sure we're fully resumed: */
1340 if (unlikely(timekeeping_suspended))
John Stultz70471f22011-11-14 12:48:10 -08001341 goto out;
john stultz85240702007-05-08 00:27:59 -07001342
John Stultz4e250fd2012-07-27 14:48:13 -04001343 clock = tk->clock;
John Stultz592913e2010-07-13 17:56:20 -07001344
1345#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
John Stultz4e250fd2012-07-27 14:48:13 -04001346 offset = tk->cycle_interval;
John Stultz592913e2010-07-13 17:56:20 -07001347#else
1348 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
john stultz85240702007-05-08 00:27:59 -07001349#endif
john stultz85240702007-05-08 00:27:59 -07001350
John Stultzbf2ac312012-08-21 20:30:49 -04001351 /* Check if there's really nothing to do */
1352 if (offset < tk->cycle_interval)
1353 goto out;
1354
john stultza092ff02009-10-02 16:17:53 -07001355 /*
1356 * With NO_HZ we may have to accumulate many cycle_intervals
1357 * (think "ticks") worth of time at once. To do this efficiently,
1358 * we calculate the largest doubling multiple of cycle_intervals
Jim Cromie88b28ad2012-03-14 21:28:56 -06001359 * that is smaller than the offset. We then accumulate that
john stultza092ff02009-10-02 16:17:53 -07001360 * chunk in one go, and then try to consume the next smaller
1361 * doubled multiple.
john stultz85240702007-05-08 00:27:59 -07001362 */
John Stultz4e250fd2012-07-27 14:48:13 -04001363 shift = ilog2(offset) - ilog2(tk->cycle_interval);
john stultza092ff02009-10-02 16:17:53 -07001364 shift = max(0, shift);
Jim Cromie88b28ad2012-03-14 21:28:56 -06001365 /* Bound shift to one less than what overflows tick_length */
John Stultzea7cf492011-11-14 13:18:07 -08001366 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
john stultza092ff02009-10-02 16:17:53 -07001367 shift = min(shift, maxshift);
John Stultz4e250fd2012-07-27 14:48:13 -04001368 while (offset >= tk->cycle_interval) {
1369 offset = logarithmic_accumulation(tk, offset, shift);
1370 if (offset < tk->cycle_interval<<shift)
John Stultz830ec042010-03-18 14:47:30 -07001371 shift--;
john stultz85240702007-05-08 00:27:59 -07001372 }
1373
1374 /* correct the clock when NTP error is too big */
John Stultz4e250fd2012-07-27 14:48:13 -04001375 timekeeping_adjust(tk, offset);
john stultz85240702007-05-08 00:27:59 -07001376
John Stultz6a867a32010-04-06 14:30:51 -07001377 /*
John Stultz92bb1fc2012-09-04 15:38:12 -04001378 * XXX This can be killed once everyone converts
1379 * to the new update_vsyscall.
1380 */
1381 old_vsyscall_fixup(tk);
john stultz85240702007-05-08 00:27:59 -07001382
John Stultz6a867a32010-04-06 14:30:51 -07001383 /*
1384 * Finally, make sure that after the rounding
John Stultz1e75fa82012-07-13 01:21:53 -04001385 * xtime_nsec isn't larger than NSEC_PER_SEC
John Stultz6a867a32010-04-06 14:30:51 -07001386 */
John Stultz4e250fd2012-07-27 14:48:13 -04001387 accumulate_nsecs_to_secs(tk);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001388
John Stultz4e250fd2012-07-27 14:48:13 -04001389 timekeeping_update(tk, false);
John Stultz70471f22011-11-14 12:48:10 -08001390
1391out:
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001392 write_seqcount_end(&timekeeper_seq);
1393 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz70471f22011-11-14 12:48:10 -08001394
john stultz85240702007-05-08 00:27:59 -07001395}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001396
1397/**
1398 * getboottime - Return the real time of system boot.
1399 * @ts: pointer to the timespec to be set
1400 *
John Stultzabb3a4e2011-02-14 17:52:09 -08001401 * Returns the wall-time of boot in a timespec.
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001402 *
1403 * This is based on the wall_to_monotonic offset and the total suspend
1404 * time. Calls to settimeofday will affect the value returned (which
1405 * basically means that however wrong your real time clock is at boot time,
1406 * you get the right time here).
1407 */
1408void getboottime(struct timespec *ts)
1409{
John Stultz4e250fd2012-07-27 14:48:13 -04001410 struct timekeeper *tk = &timekeeper;
Hiroshi Shimamoto36d47482009-08-25 15:08:30 +09001411 struct timespec boottime = {
John Stultz4e250fd2012-07-27 14:48:13 -04001412 .tv_sec = tk->wall_to_monotonic.tv_sec +
1413 tk->total_sleep_time.tv_sec,
1414 .tv_nsec = tk->wall_to_monotonic.tv_nsec +
1415 tk->total_sleep_time.tv_nsec
Hiroshi Shimamoto36d47482009-08-25 15:08:30 +09001416 };
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001417
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001418 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001419}
Jason Wangc93d89f2010-01-27 19:13:40 +08001420EXPORT_SYMBOL_GPL(getboottime);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001421
John Stultzabb3a4e2011-02-14 17:52:09 -08001422/**
1423 * get_monotonic_boottime - Returns monotonic time since boot
1424 * @ts: pointer to the timespec to be set
1425 *
1426 * Returns the monotonic time since boot in a timespec.
1427 *
1428 * This is similar to CLOCK_MONTONIC/ktime_get_ts, but also
1429 * includes the time spent in suspend.
1430 */
1431void get_monotonic_boottime(struct timespec *ts)
1432{
John Stultz4e250fd2012-07-27 14:48:13 -04001433 struct timekeeper *tk = &timekeeper;
John Stultzabb3a4e2011-02-14 17:52:09 -08001434 struct timespec tomono, sleep;
John Stultzec145ba2012-09-11 19:26:03 -04001435 s64 nsec;
John Stultzabb3a4e2011-02-14 17:52:09 -08001436 unsigned int seq;
John Stultzabb3a4e2011-02-14 17:52:09 -08001437
1438 WARN_ON(timekeeping_suspended);
1439
1440 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001441 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001442 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -04001443 nsec = timekeeping_get_ns(tk);
John Stultz4e250fd2012-07-27 14:48:13 -04001444 tomono = tk->wall_to_monotonic;
1445 sleep = tk->total_sleep_time;
John Stultzabb3a4e2011-02-14 17:52:09 -08001446
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001447 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultzabb3a4e2011-02-14 17:52:09 -08001448
John Stultzec145ba2012-09-11 19:26:03 -04001449 ts->tv_sec += tomono.tv_sec + sleep.tv_sec;
1450 ts->tv_nsec = 0;
1451 timespec_add_ns(ts, nsec + tomono.tv_nsec + sleep.tv_nsec);
John Stultzabb3a4e2011-02-14 17:52:09 -08001452}
1453EXPORT_SYMBOL_GPL(get_monotonic_boottime);
1454
1455/**
1456 * ktime_get_boottime - Returns monotonic time since boot in a ktime
1457 *
1458 * Returns the monotonic time since boot in a ktime
1459 *
1460 * This is similar to CLOCK_MONTONIC/ktime_get, but also
1461 * includes the time spent in suspend.
1462 */
1463ktime_t ktime_get_boottime(void)
1464{
1465 struct timespec ts;
1466
1467 get_monotonic_boottime(&ts);
1468 return timespec_to_ktime(ts);
1469}
1470EXPORT_SYMBOL_GPL(ktime_get_boottime);
1471
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001472/**
1473 * monotonic_to_bootbased - Convert the monotonic time to boot based.
1474 * @ts: pointer to the timespec to be converted
1475 */
1476void monotonic_to_bootbased(struct timespec *ts)
1477{
John Stultz4e250fd2012-07-27 14:48:13 -04001478 struct timekeeper *tk = &timekeeper;
1479
1480 *ts = timespec_add(*ts, tk->total_sleep_time);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001481}
Jason Wangc93d89f2010-01-27 19:13:40 +08001482EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
john stultz2c6b47d2007-07-24 17:47:43 -07001483
john stultz17c38b72007-07-24 18:38:34 -07001484unsigned long get_seconds(void)
1485{
John Stultz4e250fd2012-07-27 14:48:13 -04001486 struct timekeeper *tk = &timekeeper;
1487
1488 return tk->xtime_sec;
john stultz17c38b72007-07-24 18:38:34 -07001489}
1490EXPORT_SYMBOL(get_seconds);
1491
john stultzda15cfd2009-08-19 19:13:34 -07001492struct timespec __current_kernel_time(void)
1493{
John Stultz4e250fd2012-07-27 14:48:13 -04001494 struct timekeeper *tk = &timekeeper;
1495
1496 return tk_xtime(tk);
john stultzda15cfd2009-08-19 19:13:34 -07001497}
john stultz17c38b72007-07-24 18:38:34 -07001498
john stultz2c6b47d2007-07-24 17:47:43 -07001499struct timespec current_kernel_time(void)
1500{
John Stultz4e250fd2012-07-27 14:48:13 -04001501 struct timekeeper *tk = &timekeeper;
john stultz2c6b47d2007-07-24 17:47:43 -07001502 struct timespec now;
1503 unsigned long seq;
1504
1505 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001506 seq = read_seqcount_begin(&timekeeper_seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001507
John Stultz4e250fd2012-07-27 14:48:13 -04001508 now = tk_xtime(tk);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001509 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultz2c6b47d2007-07-24 17:47:43 -07001510
1511 return now;
1512}
john stultz2c6b47d2007-07-24 17:47:43 -07001513EXPORT_SYMBOL(current_kernel_time);
john stultzda15cfd2009-08-19 19:13:34 -07001514
1515struct timespec get_monotonic_coarse(void)
1516{
John Stultz4e250fd2012-07-27 14:48:13 -04001517 struct timekeeper *tk = &timekeeper;
john stultzda15cfd2009-08-19 19:13:34 -07001518 struct timespec now, mono;
1519 unsigned long seq;
1520
1521 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001522 seq = read_seqcount_begin(&timekeeper_seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001523
John Stultz4e250fd2012-07-27 14:48:13 -04001524 now = tk_xtime(tk);
1525 mono = tk->wall_to_monotonic;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001526 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultzda15cfd2009-08-19 19:13:34 -07001527
1528 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
1529 now.tv_nsec + mono.tv_nsec);
1530 return now;
1531}
Torben Hohn871cf1e2011-01-27 15:58:55 +01001532
1533/*
John Stultzd6ad4182012-02-28 16:50:11 -08001534 * Must hold jiffies_lock
Torben Hohn871cf1e2011-01-27 15:58:55 +01001535 */
1536void do_timer(unsigned long ticks)
1537{
1538 jiffies_64 += ticks;
1539 update_wall_time();
1540 calc_global_load(ticks);
1541}
Torben Hohn48cf76f72011-01-27 15:59:05 +01001542
1543/**
John Stultz314ac372011-02-14 18:43:08 -08001544 * get_xtime_and_monotonic_and_sleep_offset() - get xtime, wall_to_monotonic,
1545 * and sleep offsets.
Torben Hohn48cf76f72011-01-27 15:59:05 +01001546 * @xtim: pointer to timespec to be set with xtime
1547 * @wtom: pointer to timespec to be set with wall_to_monotonic
John Stultz314ac372011-02-14 18:43:08 -08001548 * @sleep: pointer to timespec to be set with time in suspend
Torben Hohn48cf76f72011-01-27 15:59:05 +01001549 */
John Stultz314ac372011-02-14 18:43:08 -08001550void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
1551 struct timespec *wtom, struct timespec *sleep)
Torben Hohn48cf76f72011-01-27 15:59:05 +01001552{
John Stultz4e250fd2012-07-27 14:48:13 -04001553 struct timekeeper *tk = &timekeeper;
Torben Hohn48cf76f72011-01-27 15:59:05 +01001554 unsigned long seq;
1555
1556 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001557 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001558 *xtim = tk_xtime(tk);
1559 *wtom = tk->wall_to_monotonic;
1560 *sleep = tk->total_sleep_time;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001561 } while (read_seqcount_retry(&timekeeper_seq, seq));
Torben Hohn48cf76f72011-01-27 15:59:05 +01001562}
Torben Hohnf0af911a92011-01-27 15:59:10 +01001563
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001564#ifdef CONFIG_HIGH_RES_TIMERS
1565/**
1566 * ktime_get_update_offsets - hrtimer helper
1567 * @offs_real: pointer to storage for monotonic -> realtime offset
1568 * @offs_boot: pointer to storage for monotonic -> boottime offset
1569 *
1570 * Returns current monotonic time and updates the offsets
1571 * Called from hrtimer_interupt() or retrigger_next_event()
1572 */
John Stultz90adda92013-01-21 17:00:11 -08001573ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot,
1574 ktime_t *offs_tai)
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001575{
John Stultz4e250fd2012-07-27 14:48:13 -04001576 struct timekeeper *tk = &timekeeper;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001577 ktime_t now;
1578 unsigned int seq;
1579 u64 secs, nsecs;
1580
1581 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001582 seq = read_seqcount_begin(&timekeeper_seq);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001583
John Stultz4e250fd2012-07-27 14:48:13 -04001584 secs = tk->xtime_sec;
1585 nsecs = timekeeping_get_ns(tk);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001586
John Stultz4e250fd2012-07-27 14:48:13 -04001587 *offs_real = tk->offs_real;
1588 *offs_boot = tk->offs_boot;
John Stultz90adda92013-01-21 17:00:11 -08001589 *offs_tai = tk->offs_tai;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001590 } while (read_seqcount_retry(&timekeeper_seq, seq));
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001591
1592 now = ktime_add_ns(ktime_set(secs, 0), nsecs);
1593 now = ktime_sub(now, *offs_real);
1594 return now;
1595}
1596#endif
1597
Torben Hohnf0af911a92011-01-27 15:59:10 +01001598/**
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001599 * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
1600 */
1601ktime_t ktime_get_monotonic_offset(void)
1602{
John Stultz4e250fd2012-07-27 14:48:13 -04001603 struct timekeeper *tk = &timekeeper;
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001604 unsigned long seq;
1605 struct timespec wtom;
1606
1607 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001608 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001609 wtom = tk->wall_to_monotonic;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001610 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz70471f22011-11-14 12:48:10 -08001611
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001612 return timespec_to_ktime(wtom);
1613}
John Stultza80b83b2012-02-03 00:19:07 -08001614EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset);
1615
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001616/**
John Stultzaa6f9c592013-03-22 11:31:29 -07001617 * do_adjtimex() - Accessor function to NTP __do_adjtimex function
1618 */
1619int do_adjtimex(struct timex *txc)
1620{
John Stultz0b5154f2013-03-22 14:20:03 -07001621 struct timekeeper *tk = &timekeeper;
John Stultz06c017f2013-03-22 11:37:28 -07001622 unsigned long flags;
John Stultz87ace392013-03-22 12:28:15 -07001623 struct timespec ts;
John Stultz0b5154f2013-03-22 14:20:03 -07001624 s32 tai;
John Stultze4085692013-03-22 12:08:52 -07001625 int ret;
1626
1627 /* Validate the data before disabling interrupts */
1628 ret = ntp_validate_timex(txc);
1629 if (ret)
1630 return ret;
1631
John Stultzcef90372013-03-22 15:04:13 -07001632 if (txc->modes & ADJ_SETOFFSET) {
1633 struct timespec delta;
1634 delta.tv_sec = txc->time.tv_sec;
1635 delta.tv_nsec = txc->time.tv_usec;
1636 if (!(txc->modes & ADJ_NANO))
1637 delta.tv_nsec *= 1000;
1638 ret = timekeeping_inject_offset(&delta);
1639 if (ret)
1640 return ret;
1641 }
1642
John Stultz87ace392013-03-22 12:28:15 -07001643 getnstimeofday(&ts);
John Stultzaa6f9c592013-03-22 11:31:29 -07001644
John Stultz06c017f2013-03-22 11:37:28 -07001645 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1646 write_seqcount_begin(&timekeeper_seq);
1647
John Stultz0b5154f2013-03-22 14:20:03 -07001648 tai = tk->tai_offset;
John Stultz87ace392013-03-22 12:28:15 -07001649 ret = __do_adjtimex(txc, &ts, &tai);
1650
John Stultz0b5154f2013-03-22 14:20:03 -07001651 __timekeeping_set_tai_offset(tk, tai);
John Stultz06c017f2013-03-22 11:37:28 -07001652 write_seqcount_end(&timekeeper_seq);
1653 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1654
John Stultz87ace392013-03-22 12:28:15 -07001655 return ret;
1656}
John Stultzaa6f9c592013-03-22 11:31:29 -07001657
1658#ifdef CONFIG_NTP_PPS
1659/**
1660 * hardpps() - Accessor function to NTP __hardpps function
1661 */
1662void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
1663{
John Stultz06c017f2013-03-22 11:37:28 -07001664 unsigned long flags;
1665
1666 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1667 write_seqcount_begin(&timekeeper_seq);
1668
John Stultzaa6f9c592013-03-22 11:31:29 -07001669 __hardpps(phase_ts, raw_ts);
John Stultz06c017f2013-03-22 11:37:28 -07001670
1671 write_seqcount_end(&timekeeper_seq);
1672 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzaa6f9c592013-03-22 11:31:29 -07001673}
1674EXPORT_SYMBOL(hardpps);
1675#endif
1676
1677/**
Torben Hohnf0af911a92011-01-27 15:59:10 +01001678 * xtime_update() - advances the timekeeping infrastructure
1679 * @ticks: number of ticks, that have elapsed since the last call.
1680 *
1681 * Must be called with interrupts disabled.
1682 */
1683void xtime_update(unsigned long ticks)
1684{
John Stultzd6ad4182012-02-28 16:50:11 -08001685 write_seqlock(&jiffies_lock);
Torben Hohnf0af911a92011-01-27 15:59:10 +01001686 do_timer(ticks);
John Stultzd6ad4182012-02-28 16:50:11 -08001687 write_sequnlock(&jiffies_lock);
Torben Hohnf0af911a92011-01-27 15:59:10 +01001688}