blob: 0aa4ce81bc168e2432f8f76e1e62c0b41512d47b [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"
Colin Cross5c835452013-05-21 22:32:14 -070028#include "timekeeping_internal.h"
Martin Schwidefsky155ec602009-08-14 15:47:26 +020029
David Vrabel04397fe92013-06-27 11:35:45 +010030#define TK_CLEAR_NTP (1 << 0)
31#define TK_MIRROR (1 << 1)
David Vrabel780427f2013-06-27 11:35:46 +010032#define TK_CLOCK_WAS_SET (1 << 2)
David Vrabel04397fe92013-06-27 11:35:45 +010033
H Hartley Sweetenafa14e72011-01-11 17:59:38 -060034static struct timekeeper timekeeper;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +000035static DEFINE_RAW_SPINLOCK(timekeeper_lock);
36static seqcount_t timekeeper_seq;
Thomas Gleixner48cdc132013-02-21 22:51:40 +000037static struct timekeeper shadow_timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020038
John Stultz8fcce542011-11-14 11:46:39 -080039/* flag for if timekeeping is suspended */
40int __read_mostly timekeeping_suspended;
41
Feng Tang31ade302013-01-16 00:09:47 +080042/* Flag for if there is a persistent clock on this platform */
43bool __read_mostly persistent_clock_exist = false;
44
John Stultz1e75fa82012-07-13 01:21:53 -040045static inline void tk_normalize_xtime(struct timekeeper *tk)
46{
47 while (tk->xtime_nsec >= ((u64)NSEC_PER_SEC << tk->shift)) {
48 tk->xtime_nsec -= (u64)NSEC_PER_SEC << tk->shift;
49 tk->xtime_sec++;
50 }
51}
John Stultz8fcce542011-11-14 11:46:39 -080052
John Stultz1e75fa82012-07-13 01:21:53 -040053static void tk_set_xtime(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 Stultz1e75fa82012-07-13 01:21:53 -040057}
58
59static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts)
60{
61 tk->xtime_sec += ts->tv_sec;
John Stultzb44d50d2012-07-23 16:22:37 -040062 tk->xtime_nsec += (u64)ts->tv_nsec << tk->shift;
John Stultz784ffcb2012-08-21 20:30:46 -040063 tk_normalize_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -040064}
John Stultz8fcce542011-11-14 11:46:39 -080065
John Stultz6d0ef902012-07-27 14:48:12 -040066static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec wtm)
67{
68 struct timespec tmp;
69
70 /*
71 * Verify consistency of: offset_real = -wall_to_monotonic
72 * before modifying anything
73 */
74 set_normalized_timespec(&tmp, -tk->wall_to_monotonic.tv_sec,
75 -tk->wall_to_monotonic.tv_nsec);
76 WARN_ON_ONCE(tk->offs_real.tv64 != timespec_to_ktime(tmp).tv64);
77 tk->wall_to_monotonic = wtm;
78 set_normalized_timespec(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
79 tk->offs_real = timespec_to_ktime(tmp);
John Stultz04005f62013-12-10 17:13:35 -080080 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
John Stultz6d0ef902012-07-27 14:48:12 -040081}
82
83static void tk_set_sleep_time(struct timekeeper *tk, struct timespec t)
84{
85 /* Verify consistency before modifying */
86 WARN_ON_ONCE(tk->offs_boot.tv64 != timespec_to_ktime(tk->total_sleep_time).tv64);
87
88 tk->total_sleep_time = t;
89 tk->offs_boot = timespec_to_ktime(t);
90}
91
Martin Schwidefsky155ec602009-08-14 15:47:26 +020092/**
Yijing Wangd26e4fe2013-11-28 16:28:55 +080093 * tk_setup_internals - Set up internals to use clocksource clock.
Martin Schwidefsky155ec602009-08-14 15:47:26 +020094 *
Yijing Wangd26e4fe2013-11-28 16:28:55 +080095 * @tk: The target timekeeper to setup.
Martin Schwidefsky155ec602009-08-14 15:47:26 +020096 * @clock: Pointer to clocksource.
97 *
98 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
99 * pair and interval request.
100 *
101 * Unless you're the timekeeping code, you should not be using this!
102 */
John Stultzf726a692012-07-13 01:21:57 -0400103static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200104{
105 cycle_t interval;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700106 u64 tmp, ntpinterval;
John Stultz1e75fa82012-07-13 01:21:53 -0400107 struct clocksource *old_clock;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200108
John Stultzf726a692012-07-13 01:21:57 -0400109 old_clock = tk->clock;
110 tk->clock = clock;
Thomas Gleixner14a3b6a2013-02-21 22:51:38 +0000111 tk->cycle_last = clock->cycle_last = clock->read(clock);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200112
113 /* Do the ns -> cycle conversion first, using original mult */
114 tmp = NTP_INTERVAL_LENGTH;
115 tmp <<= clock->shift;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700116 ntpinterval = tmp;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200117 tmp += clock->mult/2;
118 do_div(tmp, clock->mult);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200119 if (tmp == 0)
120 tmp = 1;
121
122 interval = (cycle_t) tmp;
John Stultzf726a692012-07-13 01:21:57 -0400123 tk->cycle_interval = interval;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200124
125 /* Go back from cycles -> shifted ns */
John Stultzf726a692012-07-13 01:21:57 -0400126 tk->xtime_interval = (u64) interval * clock->mult;
127 tk->xtime_remainder = ntpinterval - tk->xtime_interval;
128 tk->raw_interval =
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200129 ((u64) interval * clock->mult) >> clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200130
John Stultz1e75fa82012-07-13 01:21:53 -0400131 /* if changing clocks, convert xtime_nsec shift units */
132 if (old_clock) {
133 int shift_change = clock->shift - old_clock->shift;
134 if (shift_change < 0)
John Stultzf726a692012-07-13 01:21:57 -0400135 tk->xtime_nsec >>= -shift_change;
John Stultz1e75fa82012-07-13 01:21:53 -0400136 else
John Stultzf726a692012-07-13 01:21:57 -0400137 tk->xtime_nsec <<= shift_change;
John Stultz1e75fa82012-07-13 01:21:53 -0400138 }
John Stultzf726a692012-07-13 01:21:57 -0400139 tk->shift = clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200140
John Stultzf726a692012-07-13 01:21:57 -0400141 tk->ntp_error = 0;
142 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200143
144 /*
145 * The timekeeper keeps its own mult values for the currently
146 * active clocksource. These value will be adjusted via NTP
147 * to counteract clock drifting.
148 */
John Stultzf726a692012-07-13 01:21:57 -0400149 tk->mult = clock->mult;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200150}
john stultz85240702007-05-08 00:27:59 -0700151
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200152/* Timekeeper helper functions. */
Stephen Warren7b1f6202012-11-07 17:58:54 -0700153
154#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
155u32 (*arch_gettimeoffset)(void);
156
157u32 get_arch_timeoffset(void)
158{
159 if (likely(arch_gettimeoffset))
160 return arch_gettimeoffset();
161 return 0;
162}
163#else
164static inline u32 get_arch_timeoffset(void) { return 0; }
165#endif
166
John Stultzf726a692012-07-13 01:21:57 -0400167static inline s64 timekeeping_get_ns(struct timekeeper *tk)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200168{
169 cycle_t cycle_now, cycle_delta;
170 struct clocksource *clock;
John Stultz1e75fa82012-07-13 01:21:53 -0400171 s64 nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200172
173 /* read clocksource: */
John Stultzf726a692012-07-13 01:21:57 -0400174 clock = tk->clock;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200175 cycle_now = clock->read(clock);
176
177 /* calculate the delta since the last update_wall_time: */
178 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
179
John Stultzf726a692012-07-13 01:21:57 -0400180 nsec = cycle_delta * tk->mult + tk->xtime_nsec;
181 nsec >>= tk->shift;
John Stultzf2a5a082012-07-13 01:21:55 -0400182
Stephen Warren7b1f6202012-11-07 17:58:54 -0700183 /* If arch requires, add in get_arch_timeoffset() */
184 return nsec + get_arch_timeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200185}
186
John Stultzf726a692012-07-13 01:21:57 -0400187static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200188{
189 cycle_t cycle_now, cycle_delta;
190 struct clocksource *clock;
John Stultzf2a5a082012-07-13 01:21:55 -0400191 s64 nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200192
193 /* read clocksource: */
John Stultzf726a692012-07-13 01:21:57 -0400194 clock = tk->clock;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200195 cycle_now = clock->read(clock);
196
197 /* calculate the delta since the last update_wall_time: */
198 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
199
John Stultzf2a5a082012-07-13 01:21:55 -0400200 /* convert delta to nanoseconds. */
201 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
202
Stephen Warren7b1f6202012-11-07 17:58:54 -0700203 /* If arch requires, add in get_arch_timeoffset() */
204 return nsec + get_arch_timeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200205}
206
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200207static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
208
David Vrabel780427f2013-06-27 11:35:46 +0100209static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200210{
David Vrabel780427f2013-06-27 11:35:46 +0100211 raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200212}
213
214/**
215 * pvclock_gtod_register_notifier - register a pvclock timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200216 */
217int pvclock_gtod_register_notifier(struct notifier_block *nb)
218{
219 struct timekeeper *tk = &timekeeper;
220 unsigned long flags;
221 int ret;
222
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000223 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200224 ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
David Vrabel780427f2013-06-27 11:35:46 +0100225 update_pvclock_gtod(tk, true);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000226 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200227
228 return ret;
229}
230EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
231
232/**
233 * pvclock_gtod_unregister_notifier - unregister a pvclock
234 * timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200235 */
236int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
237{
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200238 unsigned long flags;
239 int ret;
240
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000241 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200242 ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000243 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200244
245 return ret;
246}
247EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
248
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000249/* must hold timekeeper_lock */
David Vrabel04397fe92013-06-27 11:35:45 +0100250static void timekeeping_update(struct timekeeper *tk, unsigned int action)
Thomas Gleixnercc062682011-11-13 23:19:49 +0000251{
David Vrabel04397fe92013-06-27 11:35:45 +0100252 if (action & TK_CLEAR_NTP) {
John Stultzf726a692012-07-13 01:21:57 -0400253 tk->ntp_error = 0;
Thomas Gleixnercc062682011-11-13 23:19:49 +0000254 ntp_clear();
255 }
John Stultz576094b2012-09-11 19:58:13 -0400256 update_vsyscall(tk);
David Vrabel780427f2013-06-27 11:35:46 +0100257 update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000258
David Vrabel04397fe92013-06-27 11:35:45 +0100259 if (action & TK_MIRROR)
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000260 memcpy(&shadow_timekeeper, &timekeeper, sizeof(timekeeper));
Thomas Gleixnercc062682011-11-13 23:19:49 +0000261}
262
john stultz85240702007-05-08 00:27:59 -0700263/**
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200264 * timekeeping_forward_now - update clock to the current time
john stultz85240702007-05-08 00:27:59 -0700265 *
Roman Zippel9a055112008-08-20 16:37:28 -0700266 * Forward the current clock to update its state since the last call to
267 * update_wall_time(). This is useful before significant clock changes,
268 * as it avoids having to deal with this time offset explicitly.
john stultz85240702007-05-08 00:27:59 -0700269 */
John Stultzf726a692012-07-13 01:21:57 -0400270static void timekeeping_forward_now(struct timekeeper *tk)
john stultz85240702007-05-08 00:27:59 -0700271{
272 cycle_t cycle_now, cycle_delta;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200273 struct clocksource *clock;
Roman Zippel9a055112008-08-20 16:37:28 -0700274 s64 nsec;
john stultz85240702007-05-08 00:27:59 -0700275
John Stultzf726a692012-07-13 01:21:57 -0400276 clock = tk->clock;
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200277 cycle_now = clock->read(clock);
john stultz85240702007-05-08 00:27:59 -0700278 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
Thomas Gleixner14a3b6a2013-02-21 22:51:38 +0000279 tk->cycle_last = clock->cycle_last = cycle_now;
john stultz85240702007-05-08 00:27:59 -0700280
John Stultzf726a692012-07-13 01:21:57 -0400281 tk->xtime_nsec += cycle_delta * tk->mult;
john stultz7d275582009-05-01 13:10:26 -0700282
Stephen Warren7b1f6202012-11-07 17:58:54 -0700283 /* If arch requires, add in get_arch_timeoffset() */
284 tk->xtime_nsec += (u64)get_arch_timeoffset() << tk->shift;
john stultz7d275582009-05-01 13:10:26 -0700285
John Stultzf726a692012-07-13 01:21:57 -0400286 tk_normalize_xtime(tk);
John Stultz2d422442008-08-20 16:37:30 -0700287
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200288 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
John Stultzf726a692012-07-13 01:21:57 -0400289 timespec_add_ns(&tk->raw_time, nsec);
john stultz85240702007-05-08 00:27:59 -0700290}
291
292/**
Kees Cook1e817fb2012-11-19 10:26:16 -0800293 * __getnstimeofday - Returns the time of day in a timespec.
john stultz85240702007-05-08 00:27:59 -0700294 * @ts: pointer to the timespec to be set
295 *
Kees Cook1e817fb2012-11-19 10:26:16 -0800296 * Updates the time of day in the timespec.
297 * Returns 0 on success, or -ve when suspended (timespec will be undefined).
john stultz85240702007-05-08 00:27:59 -0700298 */
Kees Cook1e817fb2012-11-19 10:26:16 -0800299int __getnstimeofday(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700300{
John Stultz4e250fd2012-07-27 14:48:13 -0400301 struct timekeeper *tk = &timekeeper;
john stultz85240702007-05-08 00:27:59 -0700302 unsigned long seq;
John Stultz1e75fa82012-07-13 01:21:53 -0400303 s64 nsecs = 0;
john stultz85240702007-05-08 00:27:59 -0700304
305 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000306 seq = read_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700307
John Stultz4e250fd2012-07-27 14:48:13 -0400308 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -0400309 nsecs = timekeeping_get_ns(tk);
john stultz85240702007-05-08 00:27:59 -0700310
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000311 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultz85240702007-05-08 00:27:59 -0700312
John Stultzec145ba2012-09-11 19:26:03 -0400313 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700314 timespec_add_ns(ts, nsecs);
Kees Cook1e817fb2012-11-19 10:26:16 -0800315
316 /*
317 * Do not bail out early, in case there were callers still using
318 * the value, even in the face of the WARN_ON.
319 */
320 if (unlikely(timekeeping_suspended))
321 return -EAGAIN;
322 return 0;
323}
324EXPORT_SYMBOL(__getnstimeofday);
325
326/**
327 * getnstimeofday - Returns the time of day in a timespec.
328 * @ts: pointer to the timespec to be set
329 *
330 * Returns the time of day in a timespec (WARN if suspended).
331 */
332void getnstimeofday(struct timespec *ts)
333{
334 WARN_ON(__getnstimeofday(ts));
john stultz85240702007-05-08 00:27:59 -0700335}
john stultz85240702007-05-08 00:27:59 -0700336EXPORT_SYMBOL(getnstimeofday);
337
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200338ktime_t ktime_get(void)
339{
John Stultz4e250fd2012-07-27 14:48:13 -0400340 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200341 unsigned int seq;
342 s64 secs, nsecs;
343
344 WARN_ON(timekeeping_suspended);
345
346 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000347 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400348 secs = tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
349 nsecs = timekeeping_get_ns(tk) + tk->wall_to_monotonic.tv_nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200350
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000351 } while (read_seqcount_retry(&timekeeper_seq, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200352 /*
353 * Use ktime_set/ktime_add_ns to create a proper ktime on
354 * 32-bit architectures without CONFIG_KTIME_SCALAR.
355 */
356 return ktime_add_ns(ktime_set(secs, 0), nsecs);
357}
358EXPORT_SYMBOL_GPL(ktime_get);
359
360/**
361 * ktime_get_ts - get the monotonic clock in timespec format
362 * @ts: pointer to timespec variable
363 *
364 * The function calculates the monotonic clock from the realtime
365 * clock and the wall_to_monotonic offset and stores the result
366 * in normalized timespec format in the variable pointed to by @ts.
367 */
368void ktime_get_ts(struct timespec *ts)
369{
John Stultz4e250fd2012-07-27 14:48:13 -0400370 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200371 struct timespec tomono;
John Stultzec145ba2012-09-11 19:26:03 -0400372 s64 nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200373 unsigned int seq;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200374
375 WARN_ON(timekeeping_suspended);
376
377 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000378 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400379 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -0400380 nsec = timekeeping_get_ns(tk);
John Stultz4e250fd2012-07-27 14:48:13 -0400381 tomono = tk->wall_to_monotonic;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200382
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000383 } while (read_seqcount_retry(&timekeeper_seq, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200384
John Stultzec145ba2012-09-11 19:26:03 -0400385 ts->tv_sec += tomono.tv_sec;
386 ts->tv_nsec = 0;
387 timespec_add_ns(ts, nsec + tomono.tv_nsec);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200388}
389EXPORT_SYMBOL_GPL(ktime_get_ts);
390
John Stultz1ff3c962012-05-03 12:43:40 -0700391
392/**
393 * timekeeping_clocktai - Returns the TAI time of day in a timespec
394 * @ts: pointer to the timespec to be set
395 *
396 * Returns the time of day in a timespec.
397 */
398void timekeeping_clocktai(struct timespec *ts)
399{
400 struct timekeeper *tk = &timekeeper;
401 unsigned long seq;
402 u64 nsecs;
403
404 WARN_ON(timekeeping_suspended);
405
406 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000407 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz1ff3c962012-05-03 12:43:40 -0700408
409 ts->tv_sec = tk->xtime_sec + tk->tai_offset;
410 nsecs = timekeeping_get_ns(tk);
411
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000412 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz1ff3c962012-05-03 12:43:40 -0700413
414 ts->tv_nsec = 0;
415 timespec_add_ns(ts, nsecs);
416
417}
418EXPORT_SYMBOL(timekeeping_clocktai);
419
420
John Stultz90adda92013-01-21 17:00:11 -0800421/**
422 * ktime_get_clocktai - Returns the TAI time of day in a ktime
423 *
424 * Returns the time of day in a ktime.
425 */
426ktime_t ktime_get_clocktai(void)
427{
428 struct timespec ts;
429
430 timekeeping_clocktai(&ts);
431 return timespec_to_ktime(ts);
432}
433EXPORT_SYMBOL(ktime_get_clocktai);
434
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800435#ifdef CONFIG_NTP_PPS
436
437/**
438 * getnstime_raw_and_real - get day and raw monotonic time in timespec format
439 * @ts_raw: pointer to the timespec to be set to raw monotonic time
440 * @ts_real: pointer to the timespec to be set to the time of day
441 *
442 * This function reads both the time of day and raw monotonic time at the
443 * same time atomically and stores the resulting timestamps in timespec
444 * format.
445 */
446void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
447{
John Stultz4e250fd2012-07-27 14:48:13 -0400448 struct timekeeper *tk = &timekeeper;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800449 unsigned long seq;
450 s64 nsecs_raw, nsecs_real;
451
452 WARN_ON_ONCE(timekeeping_suspended);
453
454 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000455 seq = read_seqcount_begin(&timekeeper_seq);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800456
John Stultz4e250fd2012-07-27 14:48:13 -0400457 *ts_raw = tk->raw_time;
458 ts_real->tv_sec = tk->xtime_sec;
John Stultz1e75fa82012-07-13 01:21:53 -0400459 ts_real->tv_nsec = 0;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800460
John Stultz4e250fd2012-07-27 14:48:13 -0400461 nsecs_raw = timekeeping_get_ns_raw(tk);
462 nsecs_real = timekeeping_get_ns(tk);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800463
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000464 } while (read_seqcount_retry(&timekeeper_seq, seq));
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800465
466 timespec_add_ns(ts_raw, nsecs_raw);
467 timespec_add_ns(ts_real, nsecs_real);
468}
469EXPORT_SYMBOL(getnstime_raw_and_real);
470
471#endif /* CONFIG_NTP_PPS */
472
john stultz85240702007-05-08 00:27:59 -0700473/**
474 * do_gettimeofday - Returns the time of day in a timeval
475 * @tv: pointer to the timeval to be set
476 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100477 * NOTE: Users should be converted to using getnstimeofday()
john stultz85240702007-05-08 00:27:59 -0700478 */
479void do_gettimeofday(struct timeval *tv)
480{
481 struct timespec now;
482
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100483 getnstimeofday(&now);
john stultz85240702007-05-08 00:27:59 -0700484 tv->tv_sec = now.tv_sec;
485 tv->tv_usec = now.tv_nsec/1000;
486}
john stultz85240702007-05-08 00:27:59 -0700487EXPORT_SYMBOL(do_gettimeofday);
Richard Cochrand239f492012-04-27 10:12:42 +0200488
john stultz85240702007-05-08 00:27:59 -0700489/**
490 * do_settimeofday - Sets the time of day
491 * @tv: pointer to the timespec variable containing the new time
492 *
493 * Sets the time of day to the new time and update NTP and notify hrtimers
494 */
Richard Cochran1e6d7672011-02-01 13:50:58 +0000495int do_settimeofday(const struct timespec *tv)
john stultz85240702007-05-08 00:27:59 -0700496{
John Stultz4e250fd2012-07-27 14:48:13 -0400497 struct timekeeper *tk = &timekeeper;
John Stultz1e75fa82012-07-13 01:21:53 -0400498 struct timespec ts_delta, xt;
John Stultz92c1d3e2011-11-14 14:05:44 -0800499 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700500
John Stultzcee58482012-08-31 13:30:06 -0400501 if (!timespec_valid_strict(tv))
john stultz85240702007-05-08 00:27:59 -0700502 return -EINVAL;
503
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000504 raw_spin_lock_irqsave(&timekeeper_lock, flags);
505 write_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700506
John Stultz4e250fd2012-07-27 14:48:13 -0400507 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -0700508
John Stultz4e250fd2012-07-27 14:48:13 -0400509 xt = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -0400510 ts_delta.tv_sec = tv->tv_sec - xt.tv_sec;
511 ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec;
512
John Stultz4e250fd2012-07-27 14:48:13 -0400513 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, ts_delta));
john stultz85240702007-05-08 00:27:59 -0700514
John Stultz4e250fd2012-07-27 14:48:13 -0400515 tk_set_xtime(tk, tv);
John Stultz1e75fa82012-07-13 01:21:53 -0400516
David Vrabel780427f2013-06-27 11:35:46 +0100517 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
john stultz85240702007-05-08 00:27:59 -0700518
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000519 write_seqcount_end(&timekeeper_seq);
520 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700521
522 /* signal hrtimers about time change */
523 clock_was_set();
524
525 return 0;
526}
john stultz85240702007-05-08 00:27:59 -0700527EXPORT_SYMBOL(do_settimeofday);
528
John Stultzc528f7c2011-02-01 13:52:17 +0000529/**
530 * timekeeping_inject_offset - Adds or subtracts from the current time.
531 * @tv: pointer to the timespec variable containing the offset
532 *
533 * Adds or subtracts an offset value from the current time.
534 */
535int timekeeping_inject_offset(struct timespec *ts)
536{
John Stultz4e250fd2012-07-27 14:48:13 -0400537 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800538 unsigned long flags;
John Stultz4e8b1452012-08-08 15:36:20 -0400539 struct timespec tmp;
540 int ret = 0;
John Stultzc528f7c2011-02-01 13:52:17 +0000541
542 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
543 return -EINVAL;
544
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000545 raw_spin_lock_irqsave(&timekeeper_lock, flags);
546 write_seqcount_begin(&timekeeper_seq);
John Stultzc528f7c2011-02-01 13:52:17 +0000547
John Stultz4e250fd2012-07-27 14:48:13 -0400548 timekeeping_forward_now(tk);
John Stultzc528f7c2011-02-01 13:52:17 +0000549
John Stultz4e8b1452012-08-08 15:36:20 -0400550 /* Make sure the proposed value is valid */
551 tmp = timespec_add(tk_xtime(tk), *ts);
John Stultzcee58482012-08-31 13:30:06 -0400552 if (!timespec_valid_strict(&tmp)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400553 ret = -EINVAL;
554 goto error;
555 }
John Stultz1e75fa82012-07-13 01:21:53 -0400556
John Stultz4e250fd2012-07-27 14:48:13 -0400557 tk_xtime_add(tk, ts);
558 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *ts));
John Stultzc528f7c2011-02-01 13:52:17 +0000559
John Stultz4e8b1452012-08-08 15:36:20 -0400560error: /* even if we error out, we forwarded the time, so call update */
David Vrabel780427f2013-06-27 11:35:46 +0100561 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultzc528f7c2011-02-01 13:52:17 +0000562
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000563 write_seqcount_end(&timekeeper_seq);
564 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzc528f7c2011-02-01 13:52:17 +0000565
566 /* signal hrtimers about time change */
567 clock_was_set();
568
John Stultz4e8b1452012-08-08 15:36:20 -0400569 return ret;
John Stultzc528f7c2011-02-01 13:52:17 +0000570}
571EXPORT_SYMBOL(timekeeping_inject_offset);
572
John Stultzcc244dd2012-05-03 12:30:07 -0700573
574/**
575 * timekeeping_get_tai_offset - Returns current TAI offset from UTC
576 *
577 */
578s32 timekeeping_get_tai_offset(void)
579{
580 struct timekeeper *tk = &timekeeper;
581 unsigned int seq;
582 s32 ret;
583
584 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000585 seq = read_seqcount_begin(&timekeeper_seq);
John Stultzcc244dd2012-05-03 12:30:07 -0700586 ret = tk->tai_offset;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000587 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultzcc244dd2012-05-03 12:30:07 -0700588
589 return ret;
590}
591
592/**
593 * __timekeeping_set_tai_offset - Lock free worker function
594 *
595 */
Fengguang Wudd5d70e2013-03-25 12:24:24 -0700596static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
John Stultzcc244dd2012-05-03 12:30:07 -0700597{
598 tk->tai_offset = tai_offset;
John Stultz04005f62013-12-10 17:13:35 -0800599 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
John Stultzcc244dd2012-05-03 12:30:07 -0700600}
601
602/**
603 * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
604 *
605 */
606void timekeeping_set_tai_offset(s32 tai_offset)
607{
608 struct timekeeper *tk = &timekeeper;
609 unsigned long flags;
610
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000611 raw_spin_lock_irqsave(&timekeeper_lock, flags);
612 write_seqcount_begin(&timekeeper_seq);
John Stultzcc244dd2012-05-03 12:30:07 -0700613 __timekeeping_set_tai_offset(tk, tai_offset);
John Stultzf55c0762013-12-11 18:50:25 -0800614 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000615 write_seqcount_end(&timekeeper_seq);
616 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz4e8f8b32013-04-10 12:41:49 -0700617 clock_was_set();
John Stultzcc244dd2012-05-03 12:30:07 -0700618}
619
john stultz85240702007-05-08 00:27:59 -0700620/**
621 * change_clocksource - Swaps clocksources if a new one is available
622 *
623 * Accumulates current time interval and initializes new clocksource
624 */
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200625static int change_clocksource(void *data)
john stultz85240702007-05-08 00:27:59 -0700626{
John Stultz4e250fd2012-07-27 14:48:13 -0400627 struct timekeeper *tk = &timekeeper;
Magnus Damm4614e6a2009-04-21 12:24:02 -0700628 struct clocksource *new, *old;
John Stultzf695cf92012-03-14 16:38:15 -0700629 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700630
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200631 new = (struct clocksource *) data;
john stultz85240702007-05-08 00:27:59 -0700632
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000633 raw_spin_lock_irqsave(&timekeeper_lock, flags);
634 write_seqcount_begin(&timekeeper_seq);
John Stultzf695cf92012-03-14 16:38:15 -0700635
John Stultz4e250fd2012-07-27 14:48:13 -0400636 timekeeping_forward_now(tk);
Thomas Gleixner09ac3692013-04-25 20:31:44 +0000637 /*
638 * If the cs is in module, get a module reference. Succeeds
639 * for built-in code (owner == NULL) as well.
640 */
641 if (try_module_get(new->owner)) {
642 if (!new->enable || new->enable(new) == 0) {
643 old = tk->clock;
644 tk_setup_internals(tk, new);
645 if (old->disable)
646 old->disable(old);
647 module_put(old->owner);
648 } else {
649 module_put(new->owner);
650 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200651 }
David Vrabel780427f2013-06-27 11:35:46 +0100652 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultzf695cf92012-03-14 16:38:15 -0700653
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000654 write_seqcount_end(&timekeeper_seq);
655 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzf695cf92012-03-14 16:38:15 -0700656
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200657 return 0;
658}
john stultz85240702007-05-08 00:27:59 -0700659
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200660/**
661 * timekeeping_notify - Install a new clock source
662 * @clock: pointer to the clock source
663 *
664 * This function is called from clocksource.c after a new, better clock
665 * source has been registered. The caller holds the clocksource_mutex.
666 */
Thomas Gleixnerba919d12013-04-25 20:31:44 +0000667int timekeeping_notify(struct clocksource *clock)
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200668{
John Stultz4e250fd2012-07-27 14:48:13 -0400669 struct timekeeper *tk = &timekeeper;
670
671 if (tk->clock == clock)
Thomas Gleixnerba919d12013-04-25 20:31:44 +0000672 return 0;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200673 stop_machine(change_clocksource, clock, NULL);
john stultz85240702007-05-08 00:27:59 -0700674 tick_clock_notify();
Thomas Gleixnerba919d12013-04-25 20:31:44 +0000675 return tk->clock == clock ? 0 : -1;
john stultz85240702007-05-08 00:27:59 -0700676}
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200677
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200678/**
679 * ktime_get_real - get the real (wall-) time in ktime_t format
680 *
681 * returns the time in ktime_t format
682 */
683ktime_t ktime_get_real(void)
684{
685 struct timespec now;
686
687 getnstimeofday(&now);
688
689 return timespec_to_ktime(now);
690}
691EXPORT_SYMBOL_GPL(ktime_get_real);
john stultz85240702007-05-08 00:27:59 -0700692
693/**
John Stultz2d422442008-08-20 16:37:30 -0700694 * getrawmonotonic - Returns the raw monotonic time in a timespec
695 * @ts: pointer to the timespec to be set
696 *
697 * Returns the raw monotonic time (completely un-modified by ntp)
698 */
699void getrawmonotonic(struct timespec *ts)
700{
John Stultz4e250fd2012-07-27 14:48:13 -0400701 struct timekeeper *tk = &timekeeper;
John Stultz2d422442008-08-20 16:37:30 -0700702 unsigned long seq;
703 s64 nsecs;
John Stultz2d422442008-08-20 16:37:30 -0700704
705 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000706 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400707 nsecs = timekeeping_get_ns_raw(tk);
708 *ts = tk->raw_time;
John Stultz2d422442008-08-20 16:37:30 -0700709
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000710 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz2d422442008-08-20 16:37:30 -0700711
712 timespec_add_ns(ts, nsecs);
713}
714EXPORT_SYMBOL(getrawmonotonic);
715
John Stultz2d422442008-08-20 16:37:30 -0700716/**
Li Zefancf4fc6c2008-02-08 04:19:24 -0800717 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -0700718 */
Li Zefancf4fc6c2008-02-08 04:19:24 -0800719int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -0700720{
John Stultz4e250fd2012-07-27 14:48:13 -0400721 struct timekeeper *tk = &timekeeper;
john stultz85240702007-05-08 00:27:59 -0700722 unsigned long seq;
723 int ret;
724
725 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000726 seq = read_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700727
John Stultz4e250fd2012-07-27 14:48:13 -0400728 ret = tk->clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -0700729
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000730 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultz85240702007-05-08 00:27:59 -0700731
732 return ret;
733}
734
735/**
Jon Hunter98962462009-08-18 12:45:10 -0500736 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
Jon Hunter98962462009-08-18 12:45:10 -0500737 */
738u64 timekeeping_max_deferment(void)
739{
John Stultz4e250fd2012-07-27 14:48:13 -0400740 struct timekeeper *tk = &timekeeper;
John Stultz70471f22011-11-14 12:48:10 -0800741 unsigned long seq;
742 u64 ret;
John Stultz42e71e82012-07-13 01:21:51 -0400743
John Stultz70471f22011-11-14 12:48:10 -0800744 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000745 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz70471f22011-11-14 12:48:10 -0800746
John Stultz4e250fd2012-07-27 14:48:13 -0400747 ret = tk->clock->max_idle_ns;
John Stultz70471f22011-11-14 12:48:10 -0800748
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000749 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz70471f22011-11-14 12:48:10 -0800750
751 return ret;
Jon Hunter98962462009-08-18 12:45:10 -0500752}
753
754/**
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200755 * read_persistent_clock - Return time from the persistent clock.
john stultz85240702007-05-08 00:27:59 -0700756 *
757 * Weak dummy function for arches that do not yet support it.
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200758 * Reads the time from the battery backed persistent clock.
759 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
john stultz85240702007-05-08 00:27:59 -0700760 *
761 * XXX - Do be sure to remove it once all arches implement it.
762 */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200763void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700764{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200765 ts->tv_sec = 0;
766 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700767}
768
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200769/**
770 * read_boot_clock - Return time of the system start.
771 *
772 * Weak dummy function for arches that do not yet support it.
773 * Function to read the exact time the system has been started.
774 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
775 *
776 * XXX - Do be sure to remove it once all arches implement it.
777 */
778void __attribute__((weak)) read_boot_clock(struct timespec *ts)
779{
780 ts->tv_sec = 0;
781 ts->tv_nsec = 0;
782}
783
john stultz85240702007-05-08 00:27:59 -0700784/*
785 * timekeeping_init - Initializes the clocksource and common timekeeping values
786 */
787void __init timekeeping_init(void)
788{
John Stultz4e250fd2012-07-27 14:48:13 -0400789 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200790 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700791 unsigned long flags;
John Stultz6d0ef902012-07-27 14:48:12 -0400792 struct timespec now, boot, tmp;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200793
794 read_persistent_clock(&now);
Feng Tang31ade302013-01-16 00:09:47 +0800795
John Stultzcee58482012-08-31 13:30:06 -0400796 if (!timespec_valid_strict(&now)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400797 pr_warn("WARNING: Persistent clock returned invalid value!\n"
798 " Check your CMOS/BIOS settings.\n");
799 now.tv_sec = 0;
800 now.tv_nsec = 0;
Feng Tang31ade302013-01-16 00:09:47 +0800801 } else if (now.tv_sec || now.tv_nsec)
802 persistent_clock_exist = true;
John Stultz4e8b1452012-08-08 15:36:20 -0400803
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200804 read_boot_clock(&boot);
John Stultzcee58482012-08-31 13:30:06 -0400805 if (!timespec_valid_strict(&boot)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400806 pr_warn("WARNING: Boot clock returned invalid value!\n"
807 " Check your CMOS/BIOS settings.\n");
808 boot.tv_sec = 0;
809 boot.tv_nsec = 0;
810 }
john stultz85240702007-05-08 00:27:59 -0700811
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000812 raw_spin_lock_irqsave(&timekeeper_lock, flags);
813 write_seqcount_begin(&timekeeper_seq);
John Stultz06c017f2013-03-22 11:37:28 -0700814 ntp_init();
815
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200816 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200817 if (clock->enable)
818 clock->enable(clock);
John Stultz4e250fd2012-07-27 14:48:13 -0400819 tk_setup_internals(tk, clock);
john stultz85240702007-05-08 00:27:59 -0700820
John Stultz4e250fd2012-07-27 14:48:13 -0400821 tk_set_xtime(tk, &now);
822 tk->raw_time.tv_sec = 0;
823 tk->raw_time.tv_nsec = 0;
John Stultz1e75fa82012-07-13 01:21:53 -0400824 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
John Stultz4e250fd2012-07-27 14:48:13 -0400825 boot = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -0400826
John Stultz6d0ef902012-07-27 14:48:12 -0400827 set_normalized_timespec(&tmp, -boot.tv_sec, -boot.tv_nsec);
John Stultz4e250fd2012-07-27 14:48:13 -0400828 tk_set_wall_to_mono(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -0400829
830 tmp.tv_sec = 0;
831 tmp.tv_nsec = 0;
John Stultz4e250fd2012-07-27 14:48:13 -0400832 tk_set_sleep_time(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -0400833
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000834 memcpy(&shadow_timekeeper, &timekeeper, sizeof(timekeeper));
835
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000836 write_seqcount_end(&timekeeper_seq);
837 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700838}
839
john stultz85240702007-05-08 00:27:59 -0700840/* time in seconds when suspend began */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200841static struct timespec timekeeping_suspend_time;
john stultz85240702007-05-08 00:27:59 -0700842
843/**
John Stultz304529b2011-04-01 14:32:09 -0700844 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
845 * @delta: pointer to a timespec delta value
846 *
847 * Takes a timespec offset measuring a suspend interval and properly
848 * adds the sleep offset to the timekeeping variables.
849 */
John Stultzf726a692012-07-13 01:21:57 -0400850static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
851 struct timespec *delta)
John Stultz304529b2011-04-01 14:32:09 -0700852{
John Stultzcee58482012-08-31 13:30:06 -0400853 if (!timespec_valid_strict(delta)) {
John Stultzcbaa5152011-07-20 15:42:55 -0700854 printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid "
John Stultzcb5de2f8d2011-06-01 18:18:09 -0700855 "sleep delta value!\n");
856 return;
857 }
John Stultzf726a692012-07-13 01:21:57 -0400858 tk_xtime_add(tk, delta);
John Stultz6d0ef902012-07-27 14:48:12 -0400859 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *delta));
860 tk_set_sleep_time(tk, timespec_add(tk->total_sleep_time, *delta));
Colin Cross5c835452013-05-21 22:32:14 -0700861 tk_debug_account_sleep_time(delta);
John Stultz304529b2011-04-01 14:32:09 -0700862}
863
John Stultz304529b2011-04-01 14:32:09 -0700864/**
865 * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
866 * @delta: pointer to a timespec delta value
867 *
868 * This hook is for architectures that cannot support read_persistent_clock
869 * because their RTC/persistent clock is only accessible when irqs are enabled.
870 *
871 * This function should only be called by rtc_resume(), and allows
872 * a suspend offset to be injected into the timekeeping values.
873 */
874void timekeeping_inject_sleeptime(struct timespec *delta)
875{
John Stultz4e250fd2012-07-27 14:48:13 -0400876 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800877 unsigned long flags;
John Stultz304529b2011-04-01 14:32:09 -0700878
Feng Tang31ade302013-01-16 00:09:47 +0800879 /*
880 * Make sure we don't set the clock twice, as timekeeping_resume()
881 * already did it
882 */
883 if (has_persistent_clock())
John Stultz304529b2011-04-01 14:32:09 -0700884 return;
885
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000886 raw_spin_lock_irqsave(&timekeeper_lock, flags);
887 write_seqcount_begin(&timekeeper_seq);
John Stultz70471f22011-11-14 12:48:10 -0800888
John Stultz4e250fd2012-07-27 14:48:13 -0400889 timekeeping_forward_now(tk);
John Stultz304529b2011-04-01 14:32:09 -0700890
John Stultz4e250fd2012-07-27 14:48:13 -0400891 __timekeeping_inject_sleeptime(tk, delta);
John Stultz304529b2011-04-01 14:32:09 -0700892
David Vrabel780427f2013-06-27 11:35:46 +0100893 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultz304529b2011-04-01 14:32:09 -0700894
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000895 write_seqcount_end(&timekeeper_seq);
896 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz304529b2011-04-01 14:32:09 -0700897
898 /* signal hrtimers about time change */
899 clock_was_set();
900}
901
John Stultz304529b2011-04-01 14:32:09 -0700902/**
john stultz85240702007-05-08 00:27:59 -0700903 * timekeeping_resume - Resumes the generic timekeeping subsystem.
john stultz85240702007-05-08 00:27:59 -0700904 *
905 * This is for the generic clocksource timekeeping.
906 * xtime/wall_to_monotonic/jiffies/etc are
907 * still managed by arch specific suspend/resume code.
908 */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +0100909static void timekeeping_resume(void)
john stultz85240702007-05-08 00:27:59 -0700910{
John Stultz4e250fd2012-07-27 14:48:13 -0400911 struct timekeeper *tk = &timekeeper;
Feng Tange445cf12013-03-12 11:56:48 +0800912 struct clocksource *clock = tk->clock;
John Stultz92c1d3e2011-11-14 14:05:44 -0800913 unsigned long flags;
Feng Tange445cf12013-03-12 11:56:48 +0800914 struct timespec ts_new, ts_delta;
915 cycle_t cycle_now, cycle_delta;
916 bool suspendtime_found = false;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200917
Feng Tange445cf12013-03-12 11:56:48 +0800918 read_persistent_clock(&ts_new);
john stultz85240702007-05-08 00:27:59 -0700919
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200920 clockevents_resume();
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +0200921 clocksource_resume();
922
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000923 raw_spin_lock_irqsave(&timekeeper_lock, flags);
924 write_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700925
Feng Tange445cf12013-03-12 11:56:48 +0800926 /*
927 * After system resumes, we need to calculate the suspended time and
928 * compensate it for the OS time. There are 3 sources that could be
929 * used: Nonstop clocksource during suspend, persistent clock and rtc
930 * device.
931 *
932 * One specific platform may have 1 or 2 or all of them, and the
933 * preference will be:
934 * suspend-nonstop clocksource -> persistent clock -> rtc
935 * The less preferred source will only be tried if there is no better
936 * usable source. The rtc part is handled separately in rtc core code.
937 */
938 cycle_now = clock->read(clock);
939 if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
940 cycle_now > clock->cycle_last) {
941 u64 num, max = ULLONG_MAX;
942 u32 mult = clock->mult;
943 u32 shift = clock->shift;
944 s64 nsec = 0;
945
946 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
947
948 /*
949 * "cycle_delta * mutl" may cause 64 bits overflow, if the
950 * suspended time is too long. In that case we need do the
951 * 64 bits math carefully
952 */
953 do_div(max, mult);
954 if (cycle_delta > max) {
955 num = div64_u64(cycle_delta, max);
956 nsec = (((u64) max * mult) >> shift) * num;
957 cycle_delta -= num * max;
958 }
959 nsec += ((u64) cycle_delta * mult) >> shift;
960
961 ts_delta = ns_to_timespec(nsec);
962 suspendtime_found = true;
963 } else if (timespec_compare(&ts_new, &timekeeping_suspend_time) > 0) {
964 ts_delta = timespec_sub(ts_new, timekeeping_suspend_time);
965 suspendtime_found = true;
john stultz85240702007-05-08 00:27:59 -0700966 }
Feng Tange445cf12013-03-12 11:56:48 +0800967
968 if (suspendtime_found)
969 __timekeeping_inject_sleeptime(tk, &ts_delta);
970
971 /* Re-base the last cycle value */
Thomas Gleixner77c675b2013-04-22 09:37:04 +0200972 tk->cycle_last = clock->cycle_last = cycle_now;
John Stultz4e250fd2012-07-27 14:48:13 -0400973 tk->ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -0700974 timekeeping_suspended = 0;
David Vrabel780427f2013-06-27 11:35:46 +0100975 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000976 write_seqcount_end(&timekeeper_seq);
977 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700978
979 touch_softlockup_watchdog();
980
981 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
982
983 /* Resume hrtimers */
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200984 hrtimers_resume();
john stultz85240702007-05-08 00:27:59 -0700985}
986
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +0100987static int timekeeping_suspend(void)
john stultz85240702007-05-08 00:27:59 -0700988{
John Stultz4e250fd2012-07-27 14:48:13 -0400989 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800990 unsigned long flags;
John Stultzcb332172011-05-31 22:53:23 -0700991 struct timespec delta, delta_delta;
992 static struct timespec old_delta;
john stultz85240702007-05-08 00:27:59 -0700993
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200994 read_persistent_clock(&timekeeping_suspend_time);
Thomas Gleixner3be90952007-09-16 15:36:43 +0200995
Zoran Markovic0d6bd992013-05-17 11:24:05 -0700996 /*
997 * On some systems the persistent_clock can not be detected at
998 * timekeeping_init by its return value, so if we see a valid
999 * value returned, update the persistent_clock_exists flag.
1000 */
1001 if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
1002 persistent_clock_exist = true;
1003
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001004 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1005 write_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001006 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -07001007 timekeeping_suspended = 1;
John Stultzcb332172011-05-31 22:53:23 -07001008
1009 /*
1010 * To avoid drift caused by repeated suspend/resumes,
1011 * which each can add ~1 second drift error,
1012 * try to compensate so the difference in system time
1013 * and persistent_clock time stays close to constant.
1014 */
John Stultz4e250fd2012-07-27 14:48:13 -04001015 delta = timespec_sub(tk_xtime(tk), timekeeping_suspend_time);
John Stultzcb332172011-05-31 22:53:23 -07001016 delta_delta = timespec_sub(delta, old_delta);
1017 if (abs(delta_delta.tv_sec) >= 2) {
1018 /*
1019 * if delta_delta is too large, assume time correction
1020 * has occured and set old_delta to the current delta.
1021 */
1022 old_delta = delta;
1023 } else {
1024 /* Otherwise try to adjust old_system to compensate */
1025 timekeeping_suspend_time =
1026 timespec_add(timekeeping_suspend_time, delta_delta);
1027 }
John Stultz330a1612013-12-11 19:10:36 -08001028
1029 timekeeping_update(tk, TK_MIRROR);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001030 write_seqcount_end(&timekeeper_seq);
1031 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001032
1033 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
Magnus Dammc54a42b2010-02-02 14:41:41 -08001034 clocksource_suspend();
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +02001035 clockevents_suspend();
john stultz85240702007-05-08 00:27:59 -07001036
1037 return 0;
1038}
1039
1040/* sysfs resume/suspend bits for timekeeping */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001041static struct syscore_ops timekeeping_syscore_ops = {
john stultz85240702007-05-08 00:27:59 -07001042 .resume = timekeeping_resume,
1043 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -07001044};
1045
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001046static int __init timekeeping_init_ops(void)
john stultz85240702007-05-08 00:27:59 -07001047{
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001048 register_syscore_ops(&timekeeping_syscore_ops);
1049 return 0;
john stultz85240702007-05-08 00:27:59 -07001050}
1051
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001052device_initcall(timekeeping_init_ops);
john stultz85240702007-05-08 00:27:59 -07001053
1054/*
1055 * If the error is already larger, we look ahead even further
1056 * to compensate for late or lost adjustments.
1057 */
John Stultzf726a692012-07-13 01:21:57 -04001058static __always_inline int timekeeping_bigadjust(struct timekeeper *tk,
1059 s64 error, s64 *interval,
john stultz85240702007-05-08 00:27:59 -07001060 s64 *offset)
1061{
1062 s64 tick_error, i;
1063 u32 look_ahead, adj;
1064 s32 error2, mult;
1065
1066 /*
1067 * Use the current error value to determine how much to look ahead.
1068 * The larger the error the slower we adjust for it to avoid problems
1069 * with losing too many ticks, otherwise we would overadjust and
1070 * produce an even larger error. The smaller the adjustment the
1071 * faster we try to adjust for it, as lost ticks can do less harm
Li Zefan3eb05672008-02-08 04:19:25 -08001072 * here. This is tuned so that an error of about 1 msec is adjusted
john stultz85240702007-05-08 00:27:59 -07001073 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
1074 */
John Stultzf726a692012-07-13 01:21:57 -04001075 error2 = tk->ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
john stultz85240702007-05-08 00:27:59 -07001076 error2 = abs(error2);
1077 for (look_ahead = 0; error2 > 0; look_ahead++)
1078 error2 >>= 2;
1079
1080 /*
1081 * Now calculate the error in (1 << look_ahead) ticks, but first
1082 * remove the single look ahead already included in the error.
1083 */
John Stultzf726a692012-07-13 01:21:57 -04001084 tick_error = ntp_tick_length() >> (tk->ntp_error_shift + 1);
1085 tick_error -= tk->xtime_interval >> 1;
john stultz85240702007-05-08 00:27:59 -07001086 error = ((error - tick_error) >> look_ahead) + tick_error;
1087
1088 /* Finally calculate the adjustment shift value. */
1089 i = *interval;
1090 mult = 1;
1091 if (error < 0) {
1092 error = -error;
1093 *interval = -*interval;
1094 *offset = -*offset;
1095 mult = -1;
1096 }
1097 for (adj = 0; error > i; adj++)
1098 error >>= 1;
1099
1100 *interval <<= adj;
1101 *offset <<= adj;
1102 return mult << adj;
1103}
1104
1105/*
1106 * Adjust the multiplier to reduce the error value,
1107 * this is optimized for the most common adjustments of -1,0,1,
1108 * for other values we can do a bit more work.
1109 */
John Stultzf726a692012-07-13 01:21:57 -04001110static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
john stultz85240702007-05-08 00:27:59 -07001111{
John Stultzf726a692012-07-13 01:21:57 -04001112 s64 error, interval = tk->cycle_interval;
john stultz85240702007-05-08 00:27:59 -07001113 int adj;
1114
John Stultzc2bc1112011-10-27 18:12:42 -07001115 /*
Jim Cromie88b28ad2012-03-14 21:28:56 -06001116 * The point of this is to check if the error is greater than half
John Stultzc2bc1112011-10-27 18:12:42 -07001117 * an interval.
1118 *
1119 * First we shift it down from NTP_SHIFT to clocksource->shifted nsecs.
1120 *
1121 * Note we subtract one in the shift, so that error is really error*2.
John Stultz3f86f282011-10-27 17:41:17 -07001122 * This "saves" dividing(shifting) interval twice, but keeps the
1123 * (error > interval) comparison as still measuring if error is
Jim Cromie88b28ad2012-03-14 21:28:56 -06001124 * larger than half an interval.
John Stultzc2bc1112011-10-27 18:12:42 -07001125 *
John Stultz3f86f282011-10-27 17:41:17 -07001126 * Note: It does not "save" on aggravation when reading the code.
John Stultzc2bc1112011-10-27 18:12:42 -07001127 */
John Stultzf726a692012-07-13 01:21:57 -04001128 error = tk->ntp_error >> (tk->ntp_error_shift - 1);
john stultz85240702007-05-08 00:27:59 -07001129 if (error > interval) {
John Stultzc2bc1112011-10-27 18:12:42 -07001130 /*
1131 * We now divide error by 4(via shift), which checks if
Jim Cromie88b28ad2012-03-14 21:28:56 -06001132 * the error is greater than twice the interval.
John Stultzc2bc1112011-10-27 18:12:42 -07001133 * If it is greater, we need a bigadjust, if its smaller,
1134 * we can adjust by 1.
1135 */
john stultz85240702007-05-08 00:27:59 -07001136 error >>= 2;
1137 if (likely(error <= interval))
1138 adj = 1;
1139 else
Ingo Molnar1d17d172012-08-04 21:21:14 +02001140 adj = timekeeping_bigadjust(tk, error, &interval, &offset);
1141 } else {
1142 if (error < -interval) {
1143 /* See comment above, this is just switched for the negative */
1144 error >>= 2;
1145 if (likely(error >= -interval)) {
1146 adj = -1;
1147 interval = -interval;
1148 offset = -offset;
1149 } else {
1150 adj = timekeeping_bigadjust(tk, error, &interval, &offset);
1151 }
1152 } else {
1153 goto out_adjust;
1154 }
1155 }
john stultz85240702007-05-08 00:27:59 -07001156
John Stultzf726a692012-07-13 01:21:57 -04001157 if (unlikely(tk->clock->maxadj &&
1158 (tk->mult + adj > tk->clock->mult + tk->clock->maxadj))) {
John Stultze919cfd2012-03-22 19:14:46 -07001159 printk_once(KERN_WARNING
1160 "Adjusting %s more than 11%% (%ld vs %ld)\n",
John Stultzf726a692012-07-13 01:21:57 -04001161 tk->clock->name, (long)tk->mult + adj,
1162 (long)tk->clock->mult + tk->clock->maxadj);
John Stultze919cfd2012-03-22 19:14:46 -07001163 }
John Stultzc2bc1112011-10-27 18:12:42 -07001164 /*
1165 * So the following can be confusing.
1166 *
1167 * To keep things simple, lets assume adj == 1 for now.
1168 *
1169 * When adj != 1, remember that the interval and offset values
1170 * have been appropriately scaled so the math is the same.
1171 *
1172 * The basic idea here is that we're increasing the multiplier
1173 * by one, this causes the xtime_interval to be incremented by
1174 * one cycle_interval. This is because:
1175 * xtime_interval = cycle_interval * mult
1176 * So if mult is being incremented by one:
1177 * xtime_interval = cycle_interval * (mult + 1)
1178 * Its the same as:
1179 * xtime_interval = (cycle_interval * mult) + cycle_interval
1180 * Which can be shortened to:
1181 * xtime_interval += cycle_interval
1182 *
1183 * So offset stores the non-accumulated cycles. Thus the current
1184 * time (in shifted nanoseconds) is:
1185 * now = (offset * adj) + xtime_nsec
1186 * Now, even though we're adjusting the clock frequency, we have
1187 * to keep time consistent. In other words, we can't jump back
1188 * in time, and we also want to avoid jumping forward in time.
1189 *
1190 * So given the same offset value, we need the time to be the same
1191 * both before and after the freq adjustment.
1192 * now = (offset * adj_1) + xtime_nsec_1
1193 * now = (offset * adj_2) + xtime_nsec_2
1194 * So:
1195 * (offset * adj_1) + xtime_nsec_1 =
1196 * (offset * adj_2) + xtime_nsec_2
1197 * And we know:
1198 * adj_2 = adj_1 + 1
1199 * So:
1200 * (offset * adj_1) + xtime_nsec_1 =
1201 * (offset * (adj_1+1)) + xtime_nsec_2
1202 * (offset * adj_1) + xtime_nsec_1 =
1203 * (offset * adj_1) + offset + xtime_nsec_2
1204 * Canceling the sides:
1205 * xtime_nsec_1 = offset + xtime_nsec_2
1206 * Which gives us:
1207 * xtime_nsec_2 = xtime_nsec_1 - offset
1208 * Which simplfies to:
1209 * xtime_nsec -= offset
1210 *
1211 * XXX - TODO: Doc ntp_error calculation.
1212 */
John Stultzf726a692012-07-13 01:21:57 -04001213 tk->mult += adj;
1214 tk->xtime_interval += interval;
1215 tk->xtime_nsec -= offset;
1216 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -04001217
Ingo Molnar1d17d172012-08-04 21:21:14 +02001218out_adjust:
John Stultz2a8c0882012-07-13 01:21:56 -04001219 /*
1220 * It may be possible that when we entered this function, xtime_nsec
1221 * was very small. Further, if we're slightly speeding the clocksource
1222 * in the code above, its possible the required corrective factor to
1223 * xtime_nsec could cause it to underflow.
1224 *
1225 * Now, since we already accumulated the second, cannot simply roll
1226 * the accumulated second back, since the NTP subsystem has been
1227 * notified via second_overflow. So instead we push xtime_nsec forward
1228 * by the amount we underflowed, and add that amount into the error.
1229 *
1230 * We'll correct this error next time through this function, when
1231 * xtime_nsec is not as small.
1232 */
John Stultzf726a692012-07-13 01:21:57 -04001233 if (unlikely((s64)tk->xtime_nsec < 0)) {
1234 s64 neg = -(s64)tk->xtime_nsec;
1235 tk->xtime_nsec = 0;
1236 tk->ntp_error += neg << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -04001237 }
1238
john stultz85240702007-05-08 00:27:59 -07001239}
1240
1241/**
John Stultz1f4f9482012-07-13 01:21:54 -04001242 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
1243 *
1244 * Helper function that accumulates a the nsecs greater then a second
1245 * from the xtime_nsec field to the xtime_secs field.
1246 * It also calls into the NTP code to handle leapsecond processing.
1247 *
1248 */
David Vrabel780427f2013-06-27 11:35:46 +01001249static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
John Stultz1f4f9482012-07-13 01:21:54 -04001250{
1251 u64 nsecps = (u64)NSEC_PER_SEC << tk->shift;
John Stultz5258d3f2013-12-11 20:07:49 -08001252 unsigned int clock_set = 0;
John Stultz1f4f9482012-07-13 01:21:54 -04001253
1254 while (tk->xtime_nsec >= nsecps) {
1255 int leap;
1256
1257 tk->xtime_nsec -= nsecps;
1258 tk->xtime_sec++;
1259
1260 /* Figure out if its a leap sec and apply if needed */
1261 leap = second_overflow(tk->xtime_sec);
John Stultz6d0ef902012-07-27 14:48:12 -04001262 if (unlikely(leap)) {
1263 struct timespec ts;
John Stultz1f4f9482012-07-13 01:21:54 -04001264
John Stultz6d0ef902012-07-27 14:48:12 -04001265 tk->xtime_sec += leap;
1266
1267 ts.tv_sec = leap;
1268 ts.tv_nsec = 0;
1269 tk_set_wall_to_mono(tk,
1270 timespec_sub(tk->wall_to_monotonic, ts));
1271
John Stultzcc244dd2012-05-03 12:30:07 -07001272 __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
1273
John Stultz5258d3f2013-12-11 20:07:49 -08001274 clock_set = TK_CLOCK_WAS_SET;
John Stultz6d0ef902012-07-27 14:48:12 -04001275 }
John Stultz1f4f9482012-07-13 01:21:54 -04001276 }
John Stultz5258d3f2013-12-11 20:07:49 -08001277 return clock_set;
John Stultz1f4f9482012-07-13 01:21:54 -04001278}
1279
John Stultz1f4f9482012-07-13 01:21:54 -04001280/**
john stultza092ff02009-10-02 16:17:53 -07001281 * logarithmic_accumulation - shifted accumulation of cycles
1282 *
1283 * This functions accumulates a shifted interval of cycles into
1284 * into a shifted interval nanoseconds. Allows for O(log) accumulation
1285 * loop.
1286 *
1287 * Returns the unconsumed cycles.
1288 */
John Stultzf726a692012-07-13 01:21:57 -04001289static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
John Stultz5258d3f2013-12-11 20:07:49 -08001290 u32 shift,
1291 unsigned int *clock_set)
john stultza092ff02009-10-02 16:17:53 -07001292{
Thomas Gleixner23a95372013-02-21 22:51:36 +00001293 cycle_t interval = tk->cycle_interval << shift;
Jason Wesseldeda2e82010-08-09 14:20:09 -07001294 u64 raw_nsecs;
john stultza092ff02009-10-02 16:17:53 -07001295
John Stultzf726a692012-07-13 01:21:57 -04001296 /* If the offset is smaller then a shifted interval, do nothing */
Thomas Gleixner23a95372013-02-21 22:51:36 +00001297 if (offset < interval)
john stultza092ff02009-10-02 16:17:53 -07001298 return offset;
1299
1300 /* Accumulate one shifted interval */
Thomas Gleixner23a95372013-02-21 22:51:36 +00001301 offset -= interval;
Thomas Gleixner7ec98e12013-02-21 22:51:39 +00001302 tk->cycle_last += interval;
john stultza092ff02009-10-02 16:17:53 -07001303
John Stultzf726a692012-07-13 01:21:57 -04001304 tk->xtime_nsec += tk->xtime_interval << shift;
John Stultz5258d3f2013-12-11 20:07:49 -08001305 *clock_set |= accumulate_nsecs_to_secs(tk);
john stultza092ff02009-10-02 16:17:53 -07001306
Jason Wesseldeda2e82010-08-09 14:20:09 -07001307 /* Accumulate raw time */
Dan Carpenter5b3900c2012-10-09 10:18:23 +03001308 raw_nsecs = (u64)tk->raw_interval << shift;
John Stultzf726a692012-07-13 01:21:57 -04001309 raw_nsecs += tk->raw_time.tv_nsec;
John Stultzc7dcf872010-08-13 11:30:58 -07001310 if (raw_nsecs >= NSEC_PER_SEC) {
1311 u64 raw_secs = raw_nsecs;
1312 raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
John Stultzf726a692012-07-13 01:21:57 -04001313 tk->raw_time.tv_sec += raw_secs;
john stultza092ff02009-10-02 16:17:53 -07001314 }
John Stultzf726a692012-07-13 01:21:57 -04001315 tk->raw_time.tv_nsec = raw_nsecs;
john stultza092ff02009-10-02 16:17:53 -07001316
1317 /* Accumulate error between NTP and clock interval */
John Stultzf726a692012-07-13 01:21:57 -04001318 tk->ntp_error += ntp_tick_length() << shift;
1319 tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
1320 (tk->ntp_error_shift + shift);
john stultza092ff02009-10-02 16:17:53 -07001321
1322 return offset;
1323}
1324
John Stultz92bb1fc2012-09-04 15:38:12 -04001325#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
1326static inline void old_vsyscall_fixup(struct timekeeper *tk)
1327{
1328 s64 remainder;
1329
1330 /*
1331 * Store only full nanoseconds into xtime_nsec after rounding
1332 * it up and add the remainder to the error difference.
1333 * XXX - This is necessary to avoid small 1ns inconsistnecies caused
1334 * by truncating the remainder in vsyscalls. However, it causes
1335 * additional work to be done in timekeeping_adjust(). Once
1336 * the vsyscall implementations are converted to use xtime_nsec
1337 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
1338 * users are removed, this can be killed.
1339 */
1340 remainder = tk->xtime_nsec & ((1ULL << tk->shift) - 1);
1341 tk->xtime_nsec -= remainder;
1342 tk->xtime_nsec += 1ULL << tk->shift;
1343 tk->ntp_error += remainder << tk->ntp_error_shift;
Martin Schwidefsky4be77392013-11-22 11:44:51 -08001344 tk->ntp_error -= (1ULL << tk->shift) << tk->ntp_error_shift;
John Stultz92bb1fc2012-09-04 15:38:12 -04001345}
1346#else
1347#define old_vsyscall_fixup(tk)
1348#endif
1349
1350
1351
john stultz85240702007-05-08 00:27:59 -07001352/**
1353 * update_wall_time - Uses the current clocksource to increment the wall time
1354 *
john stultz85240702007-05-08 00:27:59 -07001355 */
John Stultz47a1b7962013-12-12 13:10:55 -08001356void update_wall_time(void)
john stultz85240702007-05-08 00:27:59 -07001357{
Martin Schwidefsky155ec602009-08-14 15:47:26 +02001358 struct clocksource *clock;
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001359 struct timekeeper *real_tk = &timekeeper;
1360 struct timekeeper *tk = &shadow_timekeeper;
john stultz85240702007-05-08 00:27:59 -07001361 cycle_t offset;
john stultza092ff02009-10-02 16:17:53 -07001362 int shift = 0, maxshift;
John Stultz5258d3f2013-12-11 20:07:49 -08001363 unsigned int clock_set = 0;
John Stultz70471f22011-11-14 12:48:10 -08001364 unsigned long flags;
1365
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001366 raw_spin_lock_irqsave(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001367
1368 /* Make sure we're fully resumed: */
1369 if (unlikely(timekeeping_suspended))
John Stultz70471f22011-11-14 12:48:10 -08001370 goto out;
john stultz85240702007-05-08 00:27:59 -07001371
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001372 clock = real_tk->clock;
John Stultz592913e2010-07-13 17:56:20 -07001373
1374#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001375 offset = real_tk->cycle_interval;
John Stultz592913e2010-07-13 17:56:20 -07001376#else
1377 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
john stultz85240702007-05-08 00:27:59 -07001378#endif
john stultz85240702007-05-08 00:27:59 -07001379
John Stultzbf2ac312012-08-21 20:30:49 -04001380 /* Check if there's really nothing to do */
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001381 if (offset < real_tk->cycle_interval)
John Stultzbf2ac312012-08-21 20:30:49 -04001382 goto out;
1383
john stultza092ff02009-10-02 16:17:53 -07001384 /*
1385 * With NO_HZ we may have to accumulate many cycle_intervals
1386 * (think "ticks") worth of time at once. To do this efficiently,
1387 * we calculate the largest doubling multiple of cycle_intervals
Jim Cromie88b28ad2012-03-14 21:28:56 -06001388 * that is smaller than the offset. We then accumulate that
john stultza092ff02009-10-02 16:17:53 -07001389 * chunk in one go, and then try to consume the next smaller
1390 * doubled multiple.
john stultz85240702007-05-08 00:27:59 -07001391 */
John Stultz4e250fd2012-07-27 14:48:13 -04001392 shift = ilog2(offset) - ilog2(tk->cycle_interval);
john stultza092ff02009-10-02 16:17:53 -07001393 shift = max(0, shift);
Jim Cromie88b28ad2012-03-14 21:28:56 -06001394 /* Bound shift to one less than what overflows tick_length */
John Stultzea7cf492011-11-14 13:18:07 -08001395 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
john stultza092ff02009-10-02 16:17:53 -07001396 shift = min(shift, maxshift);
John Stultz4e250fd2012-07-27 14:48:13 -04001397 while (offset >= tk->cycle_interval) {
John Stultz5258d3f2013-12-11 20:07:49 -08001398 offset = logarithmic_accumulation(tk, offset, shift,
1399 &clock_set);
John Stultz4e250fd2012-07-27 14:48:13 -04001400 if (offset < tk->cycle_interval<<shift)
John Stultz830ec042010-03-18 14:47:30 -07001401 shift--;
john stultz85240702007-05-08 00:27:59 -07001402 }
1403
1404 /* correct the clock when NTP error is too big */
John Stultz4e250fd2012-07-27 14:48:13 -04001405 timekeeping_adjust(tk, offset);
john stultz85240702007-05-08 00:27:59 -07001406
John Stultz6a867a32010-04-06 14:30:51 -07001407 /*
John Stultz92bb1fc2012-09-04 15:38:12 -04001408 * XXX This can be killed once everyone converts
1409 * to the new update_vsyscall.
1410 */
1411 old_vsyscall_fixup(tk);
john stultz85240702007-05-08 00:27:59 -07001412
John Stultz6a867a32010-04-06 14:30:51 -07001413 /*
1414 * Finally, make sure that after the rounding
John Stultz1e75fa82012-07-13 01:21:53 -04001415 * xtime_nsec isn't larger than NSEC_PER_SEC
John Stultz6a867a32010-04-06 14:30:51 -07001416 */
John Stultz5258d3f2013-12-11 20:07:49 -08001417 clock_set |= accumulate_nsecs_to_secs(tk);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001418
Thomas Gleixnerca4523c2013-02-21 22:51:40 +00001419 write_seqcount_begin(&timekeeper_seq);
Thomas Gleixner7ec98e12013-02-21 22:51:39 +00001420 /* Update clock->cycle_last with the new value */
1421 clock->cycle_last = tk->cycle_last;
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001422 /*
1423 * Update the real timekeeper.
1424 *
1425 * We could avoid this memcpy by switching pointers, but that
1426 * requires changes to all other timekeeper usage sites as
1427 * well, i.e. move the timekeeper pointer getter into the
1428 * spinlocked/seqcount protected sections. And we trade this
1429 * memcpy under the timekeeper_seq against one before we start
1430 * updating.
1431 */
1432 memcpy(real_tk, tk, sizeof(*tk));
John Stultz5258d3f2013-12-11 20:07:49 -08001433 timekeeping_update(real_tk, clock_set);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001434 write_seqcount_end(&timekeeper_seq);
Thomas Gleixnerca4523c2013-02-21 22:51:40 +00001435out:
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001436 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz47a1b7962013-12-12 13:10:55 -08001437 if (clock_set)
1438 clock_was_set();
john stultz85240702007-05-08 00:27:59 -07001439}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001440
1441/**
1442 * getboottime - Return the real time of system boot.
1443 * @ts: pointer to the timespec to be set
1444 *
John Stultzabb3a4e2011-02-14 17:52:09 -08001445 * Returns the wall-time of boot in a timespec.
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001446 *
1447 * This is based on the wall_to_monotonic offset and the total suspend
1448 * time. Calls to settimeofday will affect the value returned (which
1449 * basically means that however wrong your real time clock is at boot time,
1450 * you get the right time here).
1451 */
1452void getboottime(struct timespec *ts)
1453{
John Stultz4e250fd2012-07-27 14:48:13 -04001454 struct timekeeper *tk = &timekeeper;
Hiroshi Shimamoto36d47482009-08-25 15:08:30 +09001455 struct timespec boottime = {
John Stultz4e250fd2012-07-27 14:48:13 -04001456 .tv_sec = tk->wall_to_monotonic.tv_sec +
1457 tk->total_sleep_time.tv_sec,
1458 .tv_nsec = tk->wall_to_monotonic.tv_nsec +
1459 tk->total_sleep_time.tv_nsec
Hiroshi Shimamoto36d47482009-08-25 15:08:30 +09001460 };
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001461
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001462 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001463}
Jason Wangc93d89f2010-01-27 19:13:40 +08001464EXPORT_SYMBOL_GPL(getboottime);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001465
John Stultzabb3a4e2011-02-14 17:52:09 -08001466/**
1467 * get_monotonic_boottime - Returns monotonic time since boot
1468 * @ts: pointer to the timespec to be set
1469 *
1470 * Returns the monotonic time since boot in a timespec.
1471 *
1472 * This is similar to CLOCK_MONTONIC/ktime_get_ts, but also
1473 * includes the time spent in suspend.
1474 */
1475void get_monotonic_boottime(struct timespec *ts)
1476{
John Stultz4e250fd2012-07-27 14:48:13 -04001477 struct timekeeper *tk = &timekeeper;
John Stultzabb3a4e2011-02-14 17:52:09 -08001478 struct timespec tomono, sleep;
John Stultzec145ba2012-09-11 19:26:03 -04001479 s64 nsec;
John Stultzabb3a4e2011-02-14 17:52:09 -08001480 unsigned int seq;
John Stultzabb3a4e2011-02-14 17:52:09 -08001481
1482 WARN_ON(timekeeping_suspended);
1483
1484 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001485 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001486 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -04001487 nsec = timekeeping_get_ns(tk);
John Stultz4e250fd2012-07-27 14:48:13 -04001488 tomono = tk->wall_to_monotonic;
1489 sleep = tk->total_sleep_time;
John Stultzabb3a4e2011-02-14 17:52:09 -08001490
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001491 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultzabb3a4e2011-02-14 17:52:09 -08001492
John Stultzec145ba2012-09-11 19:26:03 -04001493 ts->tv_sec += tomono.tv_sec + sleep.tv_sec;
1494 ts->tv_nsec = 0;
1495 timespec_add_ns(ts, nsec + tomono.tv_nsec + sleep.tv_nsec);
John Stultzabb3a4e2011-02-14 17:52:09 -08001496}
1497EXPORT_SYMBOL_GPL(get_monotonic_boottime);
1498
1499/**
1500 * ktime_get_boottime - Returns monotonic time since boot in a ktime
1501 *
1502 * Returns the monotonic time since boot in a ktime
1503 *
1504 * This is similar to CLOCK_MONTONIC/ktime_get, but also
1505 * includes the time spent in suspend.
1506 */
1507ktime_t ktime_get_boottime(void)
1508{
1509 struct timespec ts;
1510
1511 get_monotonic_boottime(&ts);
1512 return timespec_to_ktime(ts);
1513}
1514EXPORT_SYMBOL_GPL(ktime_get_boottime);
1515
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001516/**
1517 * monotonic_to_bootbased - Convert the monotonic time to boot based.
1518 * @ts: pointer to the timespec to be converted
1519 */
1520void monotonic_to_bootbased(struct timespec *ts)
1521{
John Stultz4e250fd2012-07-27 14:48:13 -04001522 struct timekeeper *tk = &timekeeper;
1523
1524 *ts = timespec_add(*ts, tk->total_sleep_time);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001525}
Jason Wangc93d89f2010-01-27 19:13:40 +08001526EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
john stultz2c6b47d2007-07-24 17:47:43 -07001527
john stultz17c38b72007-07-24 18:38:34 -07001528unsigned long get_seconds(void)
1529{
John Stultz4e250fd2012-07-27 14:48:13 -04001530 struct timekeeper *tk = &timekeeper;
1531
1532 return tk->xtime_sec;
john stultz17c38b72007-07-24 18:38:34 -07001533}
1534EXPORT_SYMBOL(get_seconds);
1535
john stultzda15cfd2009-08-19 19:13:34 -07001536struct timespec __current_kernel_time(void)
1537{
John Stultz4e250fd2012-07-27 14:48:13 -04001538 struct timekeeper *tk = &timekeeper;
1539
1540 return tk_xtime(tk);
john stultzda15cfd2009-08-19 19:13:34 -07001541}
john stultz17c38b72007-07-24 18:38:34 -07001542
john stultz2c6b47d2007-07-24 17:47:43 -07001543struct timespec current_kernel_time(void)
1544{
John Stultz4e250fd2012-07-27 14:48:13 -04001545 struct timekeeper *tk = &timekeeper;
john stultz2c6b47d2007-07-24 17:47:43 -07001546 struct timespec now;
1547 unsigned long seq;
1548
1549 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001550 seq = read_seqcount_begin(&timekeeper_seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001551
John Stultz4e250fd2012-07-27 14:48:13 -04001552 now = tk_xtime(tk);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001553 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultz2c6b47d2007-07-24 17:47:43 -07001554
1555 return now;
1556}
john stultz2c6b47d2007-07-24 17:47:43 -07001557EXPORT_SYMBOL(current_kernel_time);
john stultzda15cfd2009-08-19 19:13:34 -07001558
1559struct timespec get_monotonic_coarse(void)
1560{
John Stultz4e250fd2012-07-27 14:48:13 -04001561 struct timekeeper *tk = &timekeeper;
john stultzda15cfd2009-08-19 19:13:34 -07001562 struct timespec now, mono;
1563 unsigned long seq;
1564
1565 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001566 seq = read_seqcount_begin(&timekeeper_seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001567
John Stultz4e250fd2012-07-27 14:48:13 -04001568 now = tk_xtime(tk);
1569 mono = tk->wall_to_monotonic;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001570 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultzda15cfd2009-08-19 19:13:34 -07001571
1572 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
1573 now.tv_nsec + mono.tv_nsec);
1574 return now;
1575}
Torben Hohn871cf1e2011-01-27 15:58:55 +01001576
1577/*
John Stultzd6ad4182012-02-28 16:50:11 -08001578 * Must hold jiffies_lock
Torben Hohn871cf1e2011-01-27 15:58:55 +01001579 */
1580void do_timer(unsigned long ticks)
1581{
1582 jiffies_64 += ticks;
Torben Hohn871cf1e2011-01-27 15:58:55 +01001583 calc_global_load(ticks);
1584}
Torben Hohn48cf76f72011-01-27 15:59:05 +01001585
1586/**
John Stultz314ac372011-02-14 18:43:08 -08001587 * get_xtime_and_monotonic_and_sleep_offset() - get xtime, wall_to_monotonic,
1588 * and sleep offsets.
Torben Hohn48cf76f72011-01-27 15:59:05 +01001589 * @xtim: pointer to timespec to be set with xtime
1590 * @wtom: pointer to timespec to be set with wall_to_monotonic
John Stultz314ac372011-02-14 18:43:08 -08001591 * @sleep: pointer to timespec to be set with time in suspend
Torben Hohn48cf76f72011-01-27 15:59:05 +01001592 */
John Stultz314ac372011-02-14 18:43:08 -08001593void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
1594 struct timespec *wtom, struct timespec *sleep)
Torben Hohn48cf76f72011-01-27 15:59:05 +01001595{
John Stultz4e250fd2012-07-27 14:48:13 -04001596 struct timekeeper *tk = &timekeeper;
Torben Hohn48cf76f72011-01-27 15:59:05 +01001597 unsigned long seq;
1598
1599 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001600 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001601 *xtim = tk_xtime(tk);
1602 *wtom = tk->wall_to_monotonic;
1603 *sleep = tk->total_sleep_time;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001604 } while (read_seqcount_retry(&timekeeper_seq, seq));
Torben Hohn48cf76f72011-01-27 15:59:05 +01001605}
Torben Hohnf0af911a92011-01-27 15:59:10 +01001606
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001607#ifdef CONFIG_HIGH_RES_TIMERS
1608/**
1609 * ktime_get_update_offsets - hrtimer helper
1610 * @offs_real: pointer to storage for monotonic -> realtime offset
1611 * @offs_boot: pointer to storage for monotonic -> boottime offset
Xie XiuQib7bc50e2013-10-18 09:13:30 +08001612 * @offs_tai: pointer to storage for monotonic -> clock tai offset
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001613 *
1614 * Returns current monotonic time and updates the offsets
Xie XiuQib7bc50e2013-10-18 09:13:30 +08001615 * Called from hrtimer_interrupt() or retrigger_next_event()
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001616 */
John Stultz90adda92013-01-21 17:00:11 -08001617ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot,
1618 ktime_t *offs_tai)
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001619{
John Stultz4e250fd2012-07-27 14:48:13 -04001620 struct timekeeper *tk = &timekeeper;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001621 ktime_t now;
1622 unsigned int seq;
1623 u64 secs, nsecs;
1624
1625 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001626 seq = read_seqcount_begin(&timekeeper_seq);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001627
John Stultz4e250fd2012-07-27 14:48:13 -04001628 secs = tk->xtime_sec;
1629 nsecs = timekeeping_get_ns(tk);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001630
John Stultz4e250fd2012-07-27 14:48:13 -04001631 *offs_real = tk->offs_real;
1632 *offs_boot = tk->offs_boot;
John Stultz90adda92013-01-21 17:00:11 -08001633 *offs_tai = tk->offs_tai;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001634 } while (read_seqcount_retry(&timekeeper_seq, seq));
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001635
1636 now = ktime_add_ns(ktime_set(secs, 0), nsecs);
1637 now = ktime_sub(now, *offs_real);
1638 return now;
1639}
1640#endif
1641
Torben Hohnf0af911a92011-01-27 15:59:10 +01001642/**
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001643 * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
1644 */
1645ktime_t ktime_get_monotonic_offset(void)
1646{
John Stultz4e250fd2012-07-27 14:48:13 -04001647 struct timekeeper *tk = &timekeeper;
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001648 unsigned long seq;
1649 struct timespec wtom;
1650
1651 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001652 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001653 wtom = tk->wall_to_monotonic;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001654 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz70471f22011-11-14 12:48:10 -08001655
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001656 return timespec_to_ktime(wtom);
1657}
John Stultza80b83b2012-02-03 00:19:07 -08001658EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset);
1659
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001660/**
John Stultzaa6f9c592013-03-22 11:31:29 -07001661 * do_adjtimex() - Accessor function to NTP __do_adjtimex function
1662 */
1663int do_adjtimex(struct timex *txc)
1664{
John Stultz0b5154f2013-03-22 14:20:03 -07001665 struct timekeeper *tk = &timekeeper;
John Stultz06c017f2013-03-22 11:37:28 -07001666 unsigned long flags;
John Stultz87ace392013-03-22 12:28:15 -07001667 struct timespec ts;
John Stultz4e8f8b32013-04-10 12:41:49 -07001668 s32 orig_tai, tai;
John Stultze4085692013-03-22 12:08:52 -07001669 int ret;
1670
1671 /* Validate the data before disabling interrupts */
1672 ret = ntp_validate_timex(txc);
1673 if (ret)
1674 return ret;
1675
John Stultzcef90372013-03-22 15:04:13 -07001676 if (txc->modes & ADJ_SETOFFSET) {
1677 struct timespec delta;
1678 delta.tv_sec = txc->time.tv_sec;
1679 delta.tv_nsec = txc->time.tv_usec;
1680 if (!(txc->modes & ADJ_NANO))
1681 delta.tv_nsec *= 1000;
1682 ret = timekeeping_inject_offset(&delta);
1683 if (ret)
1684 return ret;
1685 }
1686
John Stultz87ace392013-03-22 12:28:15 -07001687 getnstimeofday(&ts);
John Stultzaa6f9c592013-03-22 11:31:29 -07001688
John Stultz06c017f2013-03-22 11:37:28 -07001689 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1690 write_seqcount_begin(&timekeeper_seq);
1691
John Stultz4e8f8b32013-04-10 12:41:49 -07001692 orig_tai = tai = tk->tai_offset;
John Stultz87ace392013-03-22 12:28:15 -07001693 ret = __do_adjtimex(txc, &ts, &tai);
1694
John Stultz4e8f8b32013-04-10 12:41:49 -07001695 if (tai != orig_tai) {
1696 __timekeeping_set_tai_offset(tk, tai);
John Stultzf55c0762013-12-11 18:50:25 -08001697 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultz4e8f8b32013-04-10 12:41:49 -07001698 }
John Stultz06c017f2013-03-22 11:37:28 -07001699 write_seqcount_end(&timekeeper_seq);
1700 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1701
John Stultz6fdda9a2013-12-10 17:18:18 -08001702 if (tai != orig_tai)
1703 clock_was_set();
1704
John Stultz7bd36012013-09-11 16:50:56 -07001705 ntp_notify_cmos_timer();
1706
John Stultz87ace392013-03-22 12:28:15 -07001707 return ret;
1708}
John Stultzaa6f9c592013-03-22 11:31:29 -07001709
1710#ifdef CONFIG_NTP_PPS
1711/**
1712 * hardpps() - Accessor function to NTP __hardpps function
1713 */
1714void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
1715{
John Stultz06c017f2013-03-22 11:37:28 -07001716 unsigned long flags;
1717
1718 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1719 write_seqcount_begin(&timekeeper_seq);
1720
John Stultzaa6f9c592013-03-22 11:31:29 -07001721 __hardpps(phase_ts, raw_ts);
John Stultz06c017f2013-03-22 11:37:28 -07001722
1723 write_seqcount_end(&timekeeper_seq);
1724 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzaa6f9c592013-03-22 11:31:29 -07001725}
1726EXPORT_SYMBOL(hardpps);
1727#endif
1728
1729/**
Torben Hohnf0af911a92011-01-27 15:59:10 +01001730 * xtime_update() - advances the timekeeping infrastructure
1731 * @ticks: number of ticks, that have elapsed since the last call.
1732 *
1733 * Must be called with interrupts disabled.
1734 */
1735void xtime_update(unsigned long ticks)
1736{
John Stultzd6ad4182012-02-28 16:50:11 -08001737 write_seqlock(&jiffies_lock);
Torben Hohnf0af911a92011-01-27 15:59:10 +01001738 do_timer(ticks);
John Stultzd6ad4182012-02-28 16:50:11 -08001739 write_sequnlock(&jiffies_lock);
John Stultz47a1b7962013-12-12 13:10:55 -08001740 update_wall_time();
Torben Hohnf0af911a92011-01-27 15:59:10 +01001741}