blob: b7a58417761806b568c11ae5f4516dd0dcdcfaf9 [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>
john stultz85240702007-05-08 00:27:59 -070024
Martin Schwidefsky155ec602009-08-14 15:47:26 +020025
H Hartley Sweetenafa14e72011-01-11 17:59:38 -060026static struct timekeeper timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020027
John Stultz8fcce542011-11-14 11:46:39 -080028/* flag for if timekeeping is suspended */
29int __read_mostly timekeeping_suspended;
30
Feng Tang31ade302013-01-16 00:09:47 +080031/* Flag for if there is a persistent clock on this platform */
32bool __read_mostly persistent_clock_exist = false;
33
John Stultz1e75fa82012-07-13 01:21:53 -040034static inline void tk_normalize_xtime(struct timekeeper *tk)
35{
36 while (tk->xtime_nsec >= ((u64)NSEC_PER_SEC << tk->shift)) {
37 tk->xtime_nsec -= (u64)NSEC_PER_SEC << tk->shift;
38 tk->xtime_sec++;
39 }
40}
John Stultz8fcce542011-11-14 11:46:39 -080041
John Stultz1e75fa82012-07-13 01:21:53 -040042static void tk_set_xtime(struct timekeeper *tk, const struct timespec *ts)
43{
44 tk->xtime_sec = ts->tv_sec;
John Stultzb44d50d2012-07-23 16:22:37 -040045 tk->xtime_nsec = (u64)ts->tv_nsec << tk->shift;
John Stultz1e75fa82012-07-13 01:21:53 -040046}
47
48static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts)
49{
50 tk->xtime_sec += ts->tv_sec;
John Stultzb44d50d2012-07-23 16:22:37 -040051 tk->xtime_nsec += (u64)ts->tv_nsec << tk->shift;
John Stultz784ffcb2012-08-21 20:30:46 -040052 tk_normalize_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -040053}
John Stultz8fcce542011-11-14 11:46:39 -080054
John Stultz6d0ef902012-07-27 14:48:12 -040055static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec wtm)
56{
57 struct timespec tmp;
58
59 /*
60 * Verify consistency of: offset_real = -wall_to_monotonic
61 * before modifying anything
62 */
63 set_normalized_timespec(&tmp, -tk->wall_to_monotonic.tv_sec,
64 -tk->wall_to_monotonic.tv_nsec);
65 WARN_ON_ONCE(tk->offs_real.tv64 != timespec_to_ktime(tmp).tv64);
66 tk->wall_to_monotonic = wtm;
67 set_normalized_timespec(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
68 tk->offs_real = timespec_to_ktime(tmp);
69}
70
71static void tk_set_sleep_time(struct timekeeper *tk, struct timespec t)
72{
73 /* Verify consistency before modifying */
74 WARN_ON_ONCE(tk->offs_boot.tv64 != timespec_to_ktime(tk->total_sleep_time).tv64);
75
76 tk->total_sleep_time = t;
77 tk->offs_boot = timespec_to_ktime(t);
78}
79
Martin Schwidefsky155ec602009-08-14 15:47:26 +020080/**
81 * timekeeper_setup_internals - Set up internals to use clocksource clock.
82 *
83 * @clock: Pointer to clocksource.
84 *
85 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
86 * pair and interval request.
87 *
88 * Unless you're the timekeeping code, you should not be using this!
89 */
John Stultzf726a692012-07-13 01:21:57 -040090static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
Martin Schwidefsky155ec602009-08-14 15:47:26 +020091{
92 cycle_t interval;
Kasper Pedersena386b5a2010-10-20 15:55:15 -070093 u64 tmp, ntpinterval;
John Stultz1e75fa82012-07-13 01:21:53 -040094 struct clocksource *old_clock;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020095
John Stultzf726a692012-07-13 01:21:57 -040096 old_clock = tk->clock;
97 tk->clock = clock;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020098 clock->cycle_last = clock->read(clock);
99
100 /* Do the ns -> cycle conversion first, using original mult */
101 tmp = NTP_INTERVAL_LENGTH;
102 tmp <<= clock->shift;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700103 ntpinterval = tmp;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200104 tmp += clock->mult/2;
105 do_div(tmp, clock->mult);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200106 if (tmp == 0)
107 tmp = 1;
108
109 interval = (cycle_t) tmp;
John Stultzf726a692012-07-13 01:21:57 -0400110 tk->cycle_interval = interval;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200111
112 /* Go back from cycles -> shifted ns */
John Stultzf726a692012-07-13 01:21:57 -0400113 tk->xtime_interval = (u64) interval * clock->mult;
114 tk->xtime_remainder = ntpinterval - tk->xtime_interval;
115 tk->raw_interval =
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200116 ((u64) interval * clock->mult) >> clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200117
John Stultz1e75fa82012-07-13 01:21:53 -0400118 /* if changing clocks, convert xtime_nsec shift units */
119 if (old_clock) {
120 int shift_change = clock->shift - old_clock->shift;
121 if (shift_change < 0)
John Stultzf726a692012-07-13 01:21:57 -0400122 tk->xtime_nsec >>= -shift_change;
John Stultz1e75fa82012-07-13 01:21:53 -0400123 else
John Stultzf726a692012-07-13 01:21:57 -0400124 tk->xtime_nsec <<= shift_change;
John Stultz1e75fa82012-07-13 01:21:53 -0400125 }
John Stultzf726a692012-07-13 01:21:57 -0400126 tk->shift = clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200127
John Stultzf726a692012-07-13 01:21:57 -0400128 tk->ntp_error = 0;
129 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200130
131 /*
132 * The timekeeper keeps its own mult values for the currently
133 * active clocksource. These value will be adjusted via NTP
134 * to counteract clock drifting.
135 */
John Stultzf726a692012-07-13 01:21:57 -0400136 tk->mult = clock->mult;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200137}
john stultz85240702007-05-08 00:27:59 -0700138
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200139/* Timekeeper helper functions. */
John Stultzf726a692012-07-13 01:21:57 -0400140static inline s64 timekeeping_get_ns(struct timekeeper *tk)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200141{
142 cycle_t cycle_now, cycle_delta;
143 struct clocksource *clock;
John Stultz1e75fa82012-07-13 01:21:53 -0400144 s64 nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200145
146 /* read clocksource: */
John Stultzf726a692012-07-13 01:21:57 -0400147 clock = tk->clock;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200148 cycle_now = clock->read(clock);
149
150 /* calculate the delta since the last update_wall_time: */
151 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
152
John Stultzf726a692012-07-13 01:21:57 -0400153 nsec = cycle_delta * tk->mult + tk->xtime_nsec;
154 nsec >>= tk->shift;
John Stultzf2a5a082012-07-13 01:21:55 -0400155
156 /* If arch requires, add in gettimeoffset() */
157 return nsec + arch_gettimeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200158}
159
John Stultzf726a692012-07-13 01:21:57 -0400160static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200161{
162 cycle_t cycle_now, cycle_delta;
163 struct clocksource *clock;
John Stultzf2a5a082012-07-13 01:21:55 -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 Stultzf2a5a082012-07-13 01:21:55 -0400173 /* convert delta to nanoseconds. */
174 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
175
176 /* If arch requires, add in gettimeoffset() */
177 return nsec + arch_gettimeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200178}
179
Thomas Gleixnercc062682011-11-13 23:19:49 +0000180/* must hold write on timekeeper.lock */
John Stultzf726a692012-07-13 01:21:57 -0400181static void timekeeping_update(struct timekeeper *tk, bool clearntp)
Thomas Gleixnercc062682011-11-13 23:19:49 +0000182{
183 if (clearntp) {
John Stultzf726a692012-07-13 01:21:57 -0400184 tk->ntp_error = 0;
Thomas Gleixnercc062682011-11-13 23:19:49 +0000185 ntp_clear();
186 }
John Stultz576094b2012-09-11 19:58:13 -0400187 update_vsyscall(tk);
Thomas Gleixnercc062682011-11-13 23:19:49 +0000188}
189
john stultz85240702007-05-08 00:27:59 -0700190/**
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200191 * timekeeping_forward_now - update clock to the current time
john stultz85240702007-05-08 00:27:59 -0700192 *
Roman Zippel9a055112008-08-20 16:37:28 -0700193 * Forward the current clock to update its state since the last call to
194 * update_wall_time(). This is useful before significant clock changes,
195 * as it avoids having to deal with this time offset explicitly.
john stultz85240702007-05-08 00:27:59 -0700196 */
John Stultzf726a692012-07-13 01:21:57 -0400197static void timekeeping_forward_now(struct timekeeper *tk)
john stultz85240702007-05-08 00:27:59 -0700198{
199 cycle_t cycle_now, cycle_delta;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200200 struct clocksource *clock;
Roman Zippel9a055112008-08-20 16:37:28 -0700201 s64 nsec;
john stultz85240702007-05-08 00:27:59 -0700202
John Stultzf726a692012-07-13 01:21:57 -0400203 clock = tk->clock;
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200204 cycle_now = clock->read(clock);
john stultz85240702007-05-08 00:27:59 -0700205 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
Roman Zippel9a055112008-08-20 16:37:28 -0700206 clock->cycle_last = cycle_now;
john stultz85240702007-05-08 00:27:59 -0700207
John Stultzf726a692012-07-13 01:21:57 -0400208 tk->xtime_nsec += cycle_delta * tk->mult;
john stultz7d275582009-05-01 13:10:26 -0700209
210 /* If arch requires, add in gettimeoffset() */
Andreas Schwab85dc8f02012-08-21 20:30:47 -0400211 tk->xtime_nsec += (u64)arch_gettimeoffset() << tk->shift;
john stultz7d275582009-05-01 13:10:26 -0700212
John Stultzf726a692012-07-13 01:21:57 -0400213 tk_normalize_xtime(tk);
John Stultz2d422442008-08-20 16:37:30 -0700214
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200215 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
John Stultzf726a692012-07-13 01:21:57 -0400216 timespec_add_ns(&tk->raw_time, nsec);
john stultz85240702007-05-08 00:27:59 -0700217}
218
219/**
Kees Cook1e817fb2012-11-19 10:26:16 -0800220 * __getnstimeofday - Returns the time of day in a timespec.
john stultz85240702007-05-08 00:27:59 -0700221 * @ts: pointer to the timespec to be set
222 *
Kees Cook1e817fb2012-11-19 10:26:16 -0800223 * Updates the time of day in the timespec.
224 * Returns 0 on success, or -ve when suspended (timespec will be undefined).
john stultz85240702007-05-08 00:27:59 -0700225 */
Kees Cook1e817fb2012-11-19 10:26:16 -0800226int __getnstimeofday(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700227{
John Stultz4e250fd2012-07-27 14:48:13 -0400228 struct timekeeper *tk = &timekeeper;
john stultz85240702007-05-08 00:27:59 -0700229 unsigned long seq;
John Stultz1e75fa82012-07-13 01:21:53 -0400230 s64 nsecs = 0;
john stultz85240702007-05-08 00:27:59 -0700231
232 do {
John Stultz4e250fd2012-07-27 14:48:13 -0400233 seq = read_seqbegin(&tk->lock);
john stultz85240702007-05-08 00:27:59 -0700234
John Stultz4e250fd2012-07-27 14:48:13 -0400235 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -0400236 nsecs = timekeeping_get_ns(tk);
john stultz85240702007-05-08 00:27:59 -0700237
John Stultz4e250fd2012-07-27 14:48:13 -0400238 } while (read_seqretry(&tk->lock, seq));
john stultz85240702007-05-08 00:27:59 -0700239
John Stultzec145ba2012-09-11 19:26:03 -0400240 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700241 timespec_add_ns(ts, nsecs);
Kees Cook1e817fb2012-11-19 10:26:16 -0800242
243 /*
244 * Do not bail out early, in case there were callers still using
245 * the value, even in the face of the WARN_ON.
246 */
247 if (unlikely(timekeeping_suspended))
248 return -EAGAIN;
249 return 0;
250}
251EXPORT_SYMBOL(__getnstimeofday);
252
253/**
254 * getnstimeofday - Returns the time of day in a timespec.
255 * @ts: pointer to the timespec to be set
256 *
257 * Returns the time of day in a timespec (WARN if suspended).
258 */
259void getnstimeofday(struct timespec *ts)
260{
261 WARN_ON(__getnstimeofday(ts));
john stultz85240702007-05-08 00:27:59 -0700262}
john stultz85240702007-05-08 00:27:59 -0700263EXPORT_SYMBOL(getnstimeofday);
264
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200265ktime_t ktime_get(void)
266{
John Stultz4e250fd2012-07-27 14:48:13 -0400267 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200268 unsigned int seq;
269 s64 secs, nsecs;
270
271 WARN_ON(timekeeping_suspended);
272
273 do {
John Stultz4e250fd2012-07-27 14:48:13 -0400274 seq = read_seqbegin(&tk->lock);
275 secs = tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
276 nsecs = timekeeping_get_ns(tk) + tk->wall_to_monotonic.tv_nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200277
John Stultz4e250fd2012-07-27 14:48:13 -0400278 } while (read_seqretry(&tk->lock, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200279 /*
280 * Use ktime_set/ktime_add_ns to create a proper ktime on
281 * 32-bit architectures without CONFIG_KTIME_SCALAR.
282 */
283 return ktime_add_ns(ktime_set(secs, 0), nsecs);
284}
285EXPORT_SYMBOL_GPL(ktime_get);
286
287/**
288 * ktime_get_ts - get the monotonic clock in timespec format
289 * @ts: pointer to timespec variable
290 *
291 * The function calculates the monotonic clock from the realtime
292 * clock and the wall_to_monotonic offset and stores the result
293 * in normalized timespec format in the variable pointed to by @ts.
294 */
295void ktime_get_ts(struct timespec *ts)
296{
John Stultz4e250fd2012-07-27 14:48:13 -0400297 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200298 struct timespec tomono;
John Stultzec145ba2012-09-11 19:26:03 -0400299 s64 nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200300 unsigned int seq;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200301
302 WARN_ON(timekeeping_suspended);
303
304 do {
John Stultz4e250fd2012-07-27 14:48:13 -0400305 seq = read_seqbegin(&tk->lock);
306 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -0400307 nsec = timekeeping_get_ns(tk);
John Stultz4e250fd2012-07-27 14:48:13 -0400308 tomono = tk->wall_to_monotonic;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200309
John Stultz4e250fd2012-07-27 14:48:13 -0400310 } while (read_seqretry(&tk->lock, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200311
John Stultzec145ba2012-09-11 19:26:03 -0400312 ts->tv_sec += tomono.tv_sec;
313 ts->tv_nsec = 0;
314 timespec_add_ns(ts, nsec + tomono.tv_nsec);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200315}
316EXPORT_SYMBOL_GPL(ktime_get_ts);
317
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800318#ifdef CONFIG_NTP_PPS
319
320/**
321 * getnstime_raw_and_real - get day and raw monotonic time in timespec format
322 * @ts_raw: pointer to the timespec to be set to raw monotonic time
323 * @ts_real: pointer to the timespec to be set to the time of day
324 *
325 * This function reads both the time of day and raw monotonic time at the
326 * same time atomically and stores the resulting timestamps in timespec
327 * format.
328 */
329void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
330{
John Stultz4e250fd2012-07-27 14:48:13 -0400331 struct timekeeper *tk = &timekeeper;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800332 unsigned long seq;
333 s64 nsecs_raw, nsecs_real;
334
335 WARN_ON_ONCE(timekeeping_suspended);
336
337 do {
John Stultz4e250fd2012-07-27 14:48:13 -0400338 seq = read_seqbegin(&tk->lock);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800339
John Stultz4e250fd2012-07-27 14:48:13 -0400340 *ts_raw = tk->raw_time;
341 ts_real->tv_sec = tk->xtime_sec;
John Stultz1e75fa82012-07-13 01:21:53 -0400342 ts_real->tv_nsec = 0;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800343
John Stultz4e250fd2012-07-27 14:48:13 -0400344 nsecs_raw = timekeeping_get_ns_raw(tk);
345 nsecs_real = timekeeping_get_ns(tk);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800346
John Stultz4e250fd2012-07-27 14:48:13 -0400347 } while (read_seqretry(&tk->lock, seq));
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800348
349 timespec_add_ns(ts_raw, nsecs_raw);
350 timespec_add_ns(ts_real, nsecs_real);
351}
352EXPORT_SYMBOL(getnstime_raw_and_real);
353
354#endif /* CONFIG_NTP_PPS */
355
john stultz85240702007-05-08 00:27:59 -0700356/**
357 * do_gettimeofday - Returns the time of day in a timeval
358 * @tv: pointer to the timeval to be set
359 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100360 * NOTE: Users should be converted to using getnstimeofday()
john stultz85240702007-05-08 00:27:59 -0700361 */
362void do_gettimeofday(struct timeval *tv)
363{
364 struct timespec now;
365
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100366 getnstimeofday(&now);
john stultz85240702007-05-08 00:27:59 -0700367 tv->tv_sec = now.tv_sec;
368 tv->tv_usec = now.tv_nsec/1000;
369}
john stultz85240702007-05-08 00:27:59 -0700370EXPORT_SYMBOL(do_gettimeofday);
Richard Cochrand239f492012-04-27 10:12:42 +0200371
john stultz85240702007-05-08 00:27:59 -0700372/**
373 * do_settimeofday - Sets the time of day
374 * @tv: pointer to the timespec variable containing the new time
375 *
376 * Sets the time of day to the new time and update NTP and notify hrtimers
377 */
Richard Cochran1e6d7672011-02-01 13:50:58 +0000378int do_settimeofday(const struct timespec *tv)
john stultz85240702007-05-08 00:27:59 -0700379{
John Stultz4e250fd2012-07-27 14:48:13 -0400380 struct timekeeper *tk = &timekeeper;
John Stultz1e75fa82012-07-13 01:21:53 -0400381 struct timespec ts_delta, xt;
John Stultz92c1d3e2011-11-14 14:05:44 -0800382 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700383
John Stultzcee58482012-08-31 13:30:06 -0400384 if (!timespec_valid_strict(tv))
john stultz85240702007-05-08 00:27:59 -0700385 return -EINVAL;
386
John Stultz4e250fd2012-07-27 14:48:13 -0400387 write_seqlock_irqsave(&tk->lock, flags);
john stultz85240702007-05-08 00:27:59 -0700388
John Stultz4e250fd2012-07-27 14:48:13 -0400389 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -0700390
John Stultz4e250fd2012-07-27 14:48:13 -0400391 xt = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -0400392 ts_delta.tv_sec = tv->tv_sec - xt.tv_sec;
393 ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec;
394
John Stultz4e250fd2012-07-27 14:48:13 -0400395 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, ts_delta));
john stultz85240702007-05-08 00:27:59 -0700396
John Stultz4e250fd2012-07-27 14:48:13 -0400397 tk_set_xtime(tk, tv);
John Stultz1e75fa82012-07-13 01:21:53 -0400398
John Stultz4e250fd2012-07-27 14:48:13 -0400399 timekeeping_update(tk, true);
john stultz85240702007-05-08 00:27:59 -0700400
John Stultz4e250fd2012-07-27 14:48:13 -0400401 write_sequnlock_irqrestore(&tk->lock, flags);
john stultz85240702007-05-08 00:27:59 -0700402
403 /* signal hrtimers about time change */
404 clock_was_set();
405
406 return 0;
407}
john stultz85240702007-05-08 00:27:59 -0700408EXPORT_SYMBOL(do_settimeofday);
409
John Stultzc528f7c2011-02-01 13:52:17 +0000410/**
411 * timekeeping_inject_offset - Adds or subtracts from the current time.
412 * @tv: pointer to the timespec variable containing the offset
413 *
414 * Adds or subtracts an offset value from the current time.
415 */
416int timekeeping_inject_offset(struct timespec *ts)
417{
John Stultz4e250fd2012-07-27 14:48:13 -0400418 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800419 unsigned long flags;
John Stultz4e8b1452012-08-08 15:36:20 -0400420 struct timespec tmp;
421 int ret = 0;
John Stultzc528f7c2011-02-01 13:52:17 +0000422
423 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
424 return -EINVAL;
425
John Stultz4e250fd2012-07-27 14:48:13 -0400426 write_seqlock_irqsave(&tk->lock, flags);
John Stultzc528f7c2011-02-01 13:52:17 +0000427
John Stultz4e250fd2012-07-27 14:48:13 -0400428 timekeeping_forward_now(tk);
John Stultzc528f7c2011-02-01 13:52:17 +0000429
John Stultz4e8b1452012-08-08 15:36:20 -0400430 /* Make sure the proposed value is valid */
431 tmp = timespec_add(tk_xtime(tk), *ts);
John Stultzcee58482012-08-31 13:30:06 -0400432 if (!timespec_valid_strict(&tmp)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400433 ret = -EINVAL;
434 goto error;
435 }
John Stultz1e75fa82012-07-13 01:21:53 -0400436
John Stultz4e250fd2012-07-27 14:48:13 -0400437 tk_xtime_add(tk, ts);
438 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *ts));
John Stultzc528f7c2011-02-01 13:52:17 +0000439
John Stultz4e8b1452012-08-08 15:36:20 -0400440error: /* even if we error out, we forwarded the time, so call update */
John Stultz4e250fd2012-07-27 14:48:13 -0400441 timekeeping_update(tk, true);
John Stultzc528f7c2011-02-01 13:52:17 +0000442
John Stultz4e250fd2012-07-27 14:48:13 -0400443 write_sequnlock_irqrestore(&tk->lock, flags);
John Stultzc528f7c2011-02-01 13:52:17 +0000444
445 /* signal hrtimers about time change */
446 clock_was_set();
447
John Stultz4e8b1452012-08-08 15:36:20 -0400448 return ret;
John Stultzc528f7c2011-02-01 13:52:17 +0000449}
450EXPORT_SYMBOL(timekeeping_inject_offset);
451
john stultz85240702007-05-08 00:27:59 -0700452/**
453 * change_clocksource - Swaps clocksources if a new one is available
454 *
455 * Accumulates current time interval and initializes new clocksource
456 */
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200457static int change_clocksource(void *data)
john stultz85240702007-05-08 00:27:59 -0700458{
John Stultz4e250fd2012-07-27 14:48:13 -0400459 struct timekeeper *tk = &timekeeper;
Magnus Damm4614e6a2009-04-21 12:24:02 -0700460 struct clocksource *new, *old;
John Stultzf695cf92012-03-14 16:38:15 -0700461 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700462
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200463 new = (struct clocksource *) data;
john stultz85240702007-05-08 00:27:59 -0700464
John Stultz4e250fd2012-07-27 14:48:13 -0400465 write_seqlock_irqsave(&tk->lock, flags);
John Stultzf695cf92012-03-14 16:38:15 -0700466
John Stultz4e250fd2012-07-27 14:48:13 -0400467 timekeeping_forward_now(tk);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200468 if (!new->enable || new->enable(new) == 0) {
John Stultz4e250fd2012-07-27 14:48:13 -0400469 old = tk->clock;
470 tk_setup_internals(tk, new);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200471 if (old->disable)
472 old->disable(old);
473 }
John Stultz4e250fd2012-07-27 14:48:13 -0400474 timekeeping_update(tk, true);
John Stultzf695cf92012-03-14 16:38:15 -0700475
John Stultz4e250fd2012-07-27 14:48:13 -0400476 write_sequnlock_irqrestore(&tk->lock, flags);
John Stultzf695cf92012-03-14 16:38:15 -0700477
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200478 return 0;
479}
john stultz85240702007-05-08 00:27:59 -0700480
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200481/**
482 * timekeeping_notify - Install a new clock source
483 * @clock: pointer to the clock source
484 *
485 * This function is called from clocksource.c after a new, better clock
486 * source has been registered. The caller holds the clocksource_mutex.
487 */
488void timekeeping_notify(struct clocksource *clock)
489{
John Stultz4e250fd2012-07-27 14:48:13 -0400490 struct timekeeper *tk = &timekeeper;
491
492 if (tk->clock == clock)
Magnus Damm4614e6a2009-04-21 12:24:02 -0700493 return;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200494 stop_machine(change_clocksource, clock, NULL);
john stultz85240702007-05-08 00:27:59 -0700495 tick_clock_notify();
john stultz85240702007-05-08 00:27:59 -0700496}
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200497
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200498/**
499 * ktime_get_real - get the real (wall-) time in ktime_t format
500 *
501 * returns the time in ktime_t format
502 */
503ktime_t ktime_get_real(void)
504{
505 struct timespec now;
506
507 getnstimeofday(&now);
508
509 return timespec_to_ktime(now);
510}
511EXPORT_SYMBOL_GPL(ktime_get_real);
john stultz85240702007-05-08 00:27:59 -0700512
513/**
John Stultz2d422442008-08-20 16:37:30 -0700514 * getrawmonotonic - Returns the raw monotonic time in a timespec
515 * @ts: pointer to the timespec to be set
516 *
517 * Returns the raw monotonic time (completely un-modified by ntp)
518 */
519void getrawmonotonic(struct timespec *ts)
520{
John Stultz4e250fd2012-07-27 14:48:13 -0400521 struct timekeeper *tk = &timekeeper;
John Stultz2d422442008-08-20 16:37:30 -0700522 unsigned long seq;
523 s64 nsecs;
John Stultz2d422442008-08-20 16:37:30 -0700524
525 do {
John Stultz4e250fd2012-07-27 14:48:13 -0400526 seq = read_seqbegin(&tk->lock);
527 nsecs = timekeeping_get_ns_raw(tk);
528 *ts = tk->raw_time;
John Stultz2d422442008-08-20 16:37:30 -0700529
John Stultz4e250fd2012-07-27 14:48:13 -0400530 } while (read_seqretry(&tk->lock, seq));
John Stultz2d422442008-08-20 16:37:30 -0700531
532 timespec_add_ns(ts, nsecs);
533}
534EXPORT_SYMBOL(getrawmonotonic);
535
John Stultz2d422442008-08-20 16:37:30 -0700536/**
Li Zefancf4fc6c2008-02-08 04:19:24 -0800537 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -0700538 */
Li Zefancf4fc6c2008-02-08 04:19:24 -0800539int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -0700540{
John Stultz4e250fd2012-07-27 14:48:13 -0400541 struct timekeeper *tk = &timekeeper;
john stultz85240702007-05-08 00:27:59 -0700542 unsigned long seq;
543 int ret;
544
545 do {
John Stultz4e250fd2012-07-27 14:48:13 -0400546 seq = read_seqbegin(&tk->lock);
john stultz85240702007-05-08 00:27:59 -0700547
John Stultz4e250fd2012-07-27 14:48:13 -0400548 ret = tk->clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -0700549
John Stultz4e250fd2012-07-27 14:48:13 -0400550 } while (read_seqretry(&tk->lock, seq));
john stultz85240702007-05-08 00:27:59 -0700551
552 return ret;
553}
554
555/**
Jon Hunter98962462009-08-18 12:45:10 -0500556 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
Jon Hunter98962462009-08-18 12:45:10 -0500557 */
558u64 timekeeping_max_deferment(void)
559{
John Stultz4e250fd2012-07-27 14:48:13 -0400560 struct timekeeper *tk = &timekeeper;
John Stultz70471f22011-11-14 12:48:10 -0800561 unsigned long seq;
562 u64 ret;
John Stultz42e71e82012-07-13 01:21:51 -0400563
John Stultz70471f22011-11-14 12:48:10 -0800564 do {
John Stultz4e250fd2012-07-27 14:48:13 -0400565 seq = read_seqbegin(&tk->lock);
John Stultz70471f22011-11-14 12:48:10 -0800566
John Stultz4e250fd2012-07-27 14:48:13 -0400567 ret = tk->clock->max_idle_ns;
John Stultz70471f22011-11-14 12:48:10 -0800568
John Stultz4e250fd2012-07-27 14:48:13 -0400569 } while (read_seqretry(&tk->lock, seq));
John Stultz70471f22011-11-14 12:48:10 -0800570
571 return ret;
Jon Hunter98962462009-08-18 12:45:10 -0500572}
573
574/**
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200575 * read_persistent_clock - Return time from the persistent clock.
john stultz85240702007-05-08 00:27:59 -0700576 *
577 * Weak dummy function for arches that do not yet support it.
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200578 * Reads the time from the battery backed persistent clock.
579 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
john stultz85240702007-05-08 00:27:59 -0700580 *
581 * XXX - Do be sure to remove it once all arches implement it.
582 */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200583void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700584{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200585 ts->tv_sec = 0;
586 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700587}
588
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200589/**
590 * read_boot_clock - Return time of the system start.
591 *
592 * Weak dummy function for arches that do not yet support it.
593 * Function to read the exact time the system has been started.
594 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
595 *
596 * XXX - Do be sure to remove it once all arches implement it.
597 */
598void __attribute__((weak)) read_boot_clock(struct timespec *ts)
599{
600 ts->tv_sec = 0;
601 ts->tv_nsec = 0;
602}
603
john stultz85240702007-05-08 00:27:59 -0700604/*
605 * timekeeping_init - Initializes the clocksource and common timekeeping values
606 */
607void __init timekeeping_init(void)
608{
John Stultz4e250fd2012-07-27 14:48:13 -0400609 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200610 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700611 unsigned long flags;
John Stultz6d0ef902012-07-27 14:48:12 -0400612 struct timespec now, boot, tmp;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200613
614 read_persistent_clock(&now);
Feng Tang31ade302013-01-16 00:09:47 +0800615
John Stultzcee58482012-08-31 13:30:06 -0400616 if (!timespec_valid_strict(&now)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400617 pr_warn("WARNING: Persistent clock returned invalid value!\n"
618 " Check your CMOS/BIOS settings.\n");
619 now.tv_sec = 0;
620 now.tv_nsec = 0;
Feng Tang31ade302013-01-16 00:09:47 +0800621 } else if (now.tv_sec || now.tv_nsec)
622 persistent_clock_exist = true;
John Stultz4e8b1452012-08-08 15:36:20 -0400623
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200624 read_boot_clock(&boot);
John Stultzcee58482012-08-31 13:30:06 -0400625 if (!timespec_valid_strict(&boot)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400626 pr_warn("WARNING: Boot clock returned invalid value!\n"
627 " Check your CMOS/BIOS settings.\n");
628 boot.tv_sec = 0;
629 boot.tv_nsec = 0;
630 }
john stultz85240702007-05-08 00:27:59 -0700631
John Stultz4e250fd2012-07-27 14:48:13 -0400632 seqlock_init(&tk->lock);
John Stultz70471f22011-11-14 12:48:10 -0800633
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700634 ntp_init();
john stultz85240702007-05-08 00:27:59 -0700635
John Stultz4e250fd2012-07-27 14:48:13 -0400636 write_seqlock_irqsave(&tk->lock, flags);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200637 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200638 if (clock->enable)
639 clock->enable(clock);
John Stultz4e250fd2012-07-27 14:48:13 -0400640 tk_setup_internals(tk, clock);
john stultz85240702007-05-08 00:27:59 -0700641
John Stultz4e250fd2012-07-27 14:48:13 -0400642 tk_set_xtime(tk, &now);
643 tk->raw_time.tv_sec = 0;
644 tk->raw_time.tv_nsec = 0;
John Stultz1e75fa82012-07-13 01:21:53 -0400645 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
John Stultz4e250fd2012-07-27 14:48:13 -0400646 boot = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -0400647
John Stultz6d0ef902012-07-27 14:48:12 -0400648 set_normalized_timespec(&tmp, -boot.tv_sec, -boot.tv_nsec);
John Stultz4e250fd2012-07-27 14:48:13 -0400649 tk_set_wall_to_mono(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -0400650
651 tmp.tv_sec = 0;
652 tmp.tv_nsec = 0;
John Stultz4e250fd2012-07-27 14:48:13 -0400653 tk_set_sleep_time(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -0400654
John Stultz4e250fd2012-07-27 14:48:13 -0400655 write_sequnlock_irqrestore(&tk->lock, flags);
john stultz85240702007-05-08 00:27:59 -0700656}
657
john stultz85240702007-05-08 00:27:59 -0700658/* time in seconds when suspend began */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200659static struct timespec timekeeping_suspend_time;
john stultz85240702007-05-08 00:27:59 -0700660
661/**
John Stultz304529b2011-04-01 14:32:09 -0700662 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
663 * @delta: pointer to a timespec delta value
664 *
665 * Takes a timespec offset measuring a suspend interval and properly
666 * adds the sleep offset to the timekeeping variables.
667 */
John Stultzf726a692012-07-13 01:21:57 -0400668static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
669 struct timespec *delta)
John Stultz304529b2011-04-01 14:32:09 -0700670{
John Stultzcee58482012-08-31 13:30:06 -0400671 if (!timespec_valid_strict(delta)) {
John Stultzcbaa5152011-07-20 15:42:55 -0700672 printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid "
John Stultzcb5de2f2011-06-01 18:18:09 -0700673 "sleep delta value!\n");
674 return;
675 }
John Stultzf726a692012-07-13 01:21:57 -0400676 tk_xtime_add(tk, delta);
John Stultz6d0ef902012-07-27 14:48:12 -0400677 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *delta));
678 tk_set_sleep_time(tk, timespec_add(tk->total_sleep_time, *delta));
John Stultz304529b2011-04-01 14:32:09 -0700679}
680
John Stultz304529b2011-04-01 14:32:09 -0700681/**
682 * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
683 * @delta: pointer to a timespec delta value
684 *
685 * This hook is for architectures that cannot support read_persistent_clock
686 * because their RTC/persistent clock is only accessible when irqs are enabled.
687 *
688 * This function should only be called by rtc_resume(), and allows
689 * a suspend offset to be injected into the timekeeping values.
690 */
691void timekeeping_inject_sleeptime(struct timespec *delta)
692{
John Stultz4e250fd2012-07-27 14:48:13 -0400693 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800694 unsigned long flags;
John Stultz304529b2011-04-01 14:32:09 -0700695
Feng Tang31ade302013-01-16 00:09:47 +0800696 /*
697 * Make sure we don't set the clock twice, as timekeeping_resume()
698 * already did it
699 */
700 if (has_persistent_clock())
John Stultz304529b2011-04-01 14:32:09 -0700701 return;
702
John Stultz4e250fd2012-07-27 14:48:13 -0400703 write_seqlock_irqsave(&tk->lock, flags);
John Stultz70471f22011-11-14 12:48:10 -0800704
John Stultz4e250fd2012-07-27 14:48:13 -0400705 timekeeping_forward_now(tk);
John Stultz304529b2011-04-01 14:32:09 -0700706
John Stultz4e250fd2012-07-27 14:48:13 -0400707 __timekeeping_inject_sleeptime(tk, delta);
John Stultz304529b2011-04-01 14:32:09 -0700708
John Stultz4e250fd2012-07-27 14:48:13 -0400709 timekeeping_update(tk, true);
John Stultz304529b2011-04-01 14:32:09 -0700710
John Stultz4e250fd2012-07-27 14:48:13 -0400711 write_sequnlock_irqrestore(&tk->lock, flags);
John Stultz304529b2011-04-01 14:32:09 -0700712
713 /* signal hrtimers about time change */
714 clock_was_set();
715}
716
John Stultz304529b2011-04-01 14:32:09 -0700717/**
john stultz85240702007-05-08 00:27:59 -0700718 * timekeeping_resume - Resumes the generic timekeeping subsystem.
john stultz85240702007-05-08 00:27:59 -0700719 *
720 * This is for the generic clocksource timekeeping.
721 * xtime/wall_to_monotonic/jiffies/etc are
722 * still managed by arch specific suspend/resume code.
723 */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +0100724static void timekeeping_resume(void)
john stultz85240702007-05-08 00:27:59 -0700725{
John Stultz4e250fd2012-07-27 14:48:13 -0400726 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800727 unsigned long flags;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200728 struct timespec ts;
729
730 read_persistent_clock(&ts);
john stultz85240702007-05-08 00:27:59 -0700731
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200732 clockevents_resume();
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +0200733 clocksource_resume();
734
John Stultz4e250fd2012-07-27 14:48:13 -0400735 write_seqlock_irqsave(&tk->lock, flags);
john stultz85240702007-05-08 00:27:59 -0700736
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200737 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
738 ts = timespec_sub(ts, timekeeping_suspend_time);
John Stultz4e250fd2012-07-27 14:48:13 -0400739 __timekeeping_inject_sleeptime(tk, &ts);
john stultz85240702007-05-08 00:27:59 -0700740 }
741 /* re-base the last cycle value */
John Stultz4e250fd2012-07-27 14:48:13 -0400742 tk->clock->cycle_last = tk->clock->read(tk->clock);
743 tk->ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -0700744 timekeeping_suspended = 0;
John Stultz4e250fd2012-07-27 14:48:13 -0400745 timekeeping_update(tk, false);
746 write_sequnlock_irqrestore(&tk->lock, flags);
john stultz85240702007-05-08 00:27:59 -0700747
748 touch_softlockup_watchdog();
749
750 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
751
752 /* Resume hrtimers */
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200753 hrtimers_resume();
john stultz85240702007-05-08 00:27:59 -0700754}
755
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +0100756static int timekeeping_suspend(void)
john stultz85240702007-05-08 00:27:59 -0700757{
John Stultz4e250fd2012-07-27 14:48:13 -0400758 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800759 unsigned long flags;
John Stultzcb332172011-05-31 22:53:23 -0700760 struct timespec delta, delta_delta;
761 static struct timespec old_delta;
john stultz85240702007-05-08 00:27:59 -0700762
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200763 read_persistent_clock(&timekeeping_suspend_time);
Thomas Gleixner3be90952007-09-16 15:36:43 +0200764
John Stultz4e250fd2012-07-27 14:48:13 -0400765 write_seqlock_irqsave(&tk->lock, flags);
766 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -0700767 timekeeping_suspended = 1;
John Stultzcb332172011-05-31 22:53:23 -0700768
769 /*
770 * To avoid drift caused by repeated suspend/resumes,
771 * which each can add ~1 second drift error,
772 * try to compensate so the difference in system time
773 * and persistent_clock time stays close to constant.
774 */
John Stultz4e250fd2012-07-27 14:48:13 -0400775 delta = timespec_sub(tk_xtime(tk), timekeeping_suspend_time);
John Stultzcb332172011-05-31 22:53:23 -0700776 delta_delta = timespec_sub(delta, old_delta);
777 if (abs(delta_delta.tv_sec) >= 2) {
778 /*
779 * if delta_delta is too large, assume time correction
780 * has occured and set old_delta to the current delta.
781 */
782 old_delta = delta;
783 } else {
784 /* Otherwise try to adjust old_system to compensate */
785 timekeeping_suspend_time =
786 timespec_add(timekeeping_suspend_time, delta_delta);
787 }
John Stultz4e250fd2012-07-27 14:48:13 -0400788 write_sequnlock_irqrestore(&tk->lock, flags);
john stultz85240702007-05-08 00:27:59 -0700789
790 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
Magnus Dammc54a42b2010-02-02 14:41:41 -0800791 clocksource_suspend();
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200792 clockevents_suspend();
john stultz85240702007-05-08 00:27:59 -0700793
794 return 0;
795}
796
797/* sysfs resume/suspend bits for timekeeping */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +0100798static struct syscore_ops timekeeping_syscore_ops = {
john stultz85240702007-05-08 00:27:59 -0700799 .resume = timekeeping_resume,
800 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -0700801};
802
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +0100803static int __init timekeeping_init_ops(void)
john stultz85240702007-05-08 00:27:59 -0700804{
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +0100805 register_syscore_ops(&timekeeping_syscore_ops);
806 return 0;
john stultz85240702007-05-08 00:27:59 -0700807}
808
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +0100809device_initcall(timekeeping_init_ops);
john stultz85240702007-05-08 00:27:59 -0700810
811/*
812 * If the error is already larger, we look ahead even further
813 * to compensate for late or lost adjustments.
814 */
John Stultzf726a692012-07-13 01:21:57 -0400815static __always_inline int timekeeping_bigadjust(struct timekeeper *tk,
816 s64 error, s64 *interval,
john stultz85240702007-05-08 00:27:59 -0700817 s64 *offset)
818{
819 s64 tick_error, i;
820 u32 look_ahead, adj;
821 s32 error2, mult;
822
823 /*
824 * Use the current error value to determine how much to look ahead.
825 * The larger the error the slower we adjust for it to avoid problems
826 * with losing too many ticks, otherwise we would overadjust and
827 * produce an even larger error. The smaller the adjustment the
828 * faster we try to adjust for it, as lost ticks can do less harm
Li Zefan3eb05672008-02-08 04:19:25 -0800829 * here. This is tuned so that an error of about 1 msec is adjusted
john stultz85240702007-05-08 00:27:59 -0700830 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
831 */
John Stultzf726a692012-07-13 01:21:57 -0400832 error2 = tk->ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
john stultz85240702007-05-08 00:27:59 -0700833 error2 = abs(error2);
834 for (look_ahead = 0; error2 > 0; look_ahead++)
835 error2 >>= 2;
836
837 /*
838 * Now calculate the error in (1 << look_ahead) ticks, but first
839 * remove the single look ahead already included in the error.
840 */
John Stultzf726a692012-07-13 01:21:57 -0400841 tick_error = ntp_tick_length() >> (tk->ntp_error_shift + 1);
842 tick_error -= tk->xtime_interval >> 1;
john stultz85240702007-05-08 00:27:59 -0700843 error = ((error - tick_error) >> look_ahead) + tick_error;
844
845 /* Finally calculate the adjustment shift value. */
846 i = *interval;
847 mult = 1;
848 if (error < 0) {
849 error = -error;
850 *interval = -*interval;
851 *offset = -*offset;
852 mult = -1;
853 }
854 for (adj = 0; error > i; adj++)
855 error >>= 1;
856
857 *interval <<= adj;
858 *offset <<= adj;
859 return mult << adj;
860}
861
862/*
863 * Adjust the multiplier to reduce the error value,
864 * this is optimized for the most common adjustments of -1,0,1,
865 * for other values we can do a bit more work.
866 */
John Stultzf726a692012-07-13 01:21:57 -0400867static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
john stultz85240702007-05-08 00:27:59 -0700868{
John Stultzf726a692012-07-13 01:21:57 -0400869 s64 error, interval = tk->cycle_interval;
john stultz85240702007-05-08 00:27:59 -0700870 int adj;
871
John Stultzc2bc1112011-10-27 18:12:42 -0700872 /*
Jim Cromie88b28ad2012-03-14 21:28:56 -0600873 * The point of this is to check if the error is greater than half
John Stultzc2bc1112011-10-27 18:12:42 -0700874 * an interval.
875 *
876 * First we shift it down from NTP_SHIFT to clocksource->shifted nsecs.
877 *
878 * Note we subtract one in the shift, so that error is really error*2.
John Stultz3f86f282011-10-27 17:41:17 -0700879 * This "saves" dividing(shifting) interval twice, but keeps the
880 * (error > interval) comparison as still measuring if error is
Jim Cromie88b28ad2012-03-14 21:28:56 -0600881 * larger than half an interval.
John Stultzc2bc1112011-10-27 18:12:42 -0700882 *
John Stultz3f86f282011-10-27 17:41:17 -0700883 * Note: It does not "save" on aggravation when reading the code.
John Stultzc2bc1112011-10-27 18:12:42 -0700884 */
John Stultzf726a692012-07-13 01:21:57 -0400885 error = tk->ntp_error >> (tk->ntp_error_shift - 1);
john stultz85240702007-05-08 00:27:59 -0700886 if (error > interval) {
John Stultzc2bc1112011-10-27 18:12:42 -0700887 /*
888 * We now divide error by 4(via shift), which checks if
Jim Cromie88b28ad2012-03-14 21:28:56 -0600889 * the error is greater than twice the interval.
John Stultzc2bc1112011-10-27 18:12:42 -0700890 * If it is greater, we need a bigadjust, if its smaller,
891 * we can adjust by 1.
892 */
john stultz85240702007-05-08 00:27:59 -0700893 error >>= 2;
John Stultzc2bc1112011-10-27 18:12:42 -0700894 /*
895 * XXX - In update_wall_time, we round up to the next
896 * nanosecond, and store the amount rounded up into
897 * the error. This causes the likely below to be unlikely.
898 *
John Stultz3f86f282011-10-27 17:41:17 -0700899 * The proper fix is to avoid rounding up by using
John Stultz4e250fd2012-07-27 14:48:13 -0400900 * the high precision tk->xtime_nsec instead of
John Stultzc2bc1112011-10-27 18:12:42 -0700901 * xtime.tv_nsec everywhere. Fixing this will take some
902 * time.
903 */
john stultz85240702007-05-08 00:27:59 -0700904 if (likely(error <= interval))
905 adj = 1;
906 else
Ingo Molnar1d17d172012-08-04 21:21:14 +0200907 adj = timekeeping_bigadjust(tk, error, &interval, &offset);
908 } else {
909 if (error < -interval) {
910 /* See comment above, this is just switched for the negative */
911 error >>= 2;
912 if (likely(error >= -interval)) {
913 adj = -1;
914 interval = -interval;
915 offset = -offset;
916 } else {
917 adj = timekeeping_bigadjust(tk, error, &interval, &offset);
918 }
919 } else {
920 goto out_adjust;
921 }
922 }
john stultz85240702007-05-08 00:27:59 -0700923
John Stultzf726a692012-07-13 01:21:57 -0400924 if (unlikely(tk->clock->maxadj &&
925 (tk->mult + adj > tk->clock->mult + tk->clock->maxadj))) {
John Stultze919cfd2012-03-22 19:14:46 -0700926 printk_once(KERN_WARNING
927 "Adjusting %s more than 11%% (%ld vs %ld)\n",
John Stultzf726a692012-07-13 01:21:57 -0400928 tk->clock->name, (long)tk->mult + adj,
929 (long)tk->clock->mult + tk->clock->maxadj);
John Stultze919cfd2012-03-22 19:14:46 -0700930 }
John Stultzc2bc1112011-10-27 18:12:42 -0700931 /*
932 * So the following can be confusing.
933 *
934 * To keep things simple, lets assume adj == 1 for now.
935 *
936 * When adj != 1, remember that the interval and offset values
937 * have been appropriately scaled so the math is the same.
938 *
939 * The basic idea here is that we're increasing the multiplier
940 * by one, this causes the xtime_interval to be incremented by
941 * one cycle_interval. This is because:
942 * xtime_interval = cycle_interval * mult
943 * So if mult is being incremented by one:
944 * xtime_interval = cycle_interval * (mult + 1)
945 * Its the same as:
946 * xtime_interval = (cycle_interval * mult) + cycle_interval
947 * Which can be shortened to:
948 * xtime_interval += cycle_interval
949 *
950 * So offset stores the non-accumulated cycles. Thus the current
951 * time (in shifted nanoseconds) is:
952 * now = (offset * adj) + xtime_nsec
953 * Now, even though we're adjusting the clock frequency, we have
954 * to keep time consistent. In other words, we can't jump back
955 * in time, and we also want to avoid jumping forward in time.
956 *
957 * So given the same offset value, we need the time to be the same
958 * both before and after the freq adjustment.
959 * now = (offset * adj_1) + xtime_nsec_1
960 * now = (offset * adj_2) + xtime_nsec_2
961 * So:
962 * (offset * adj_1) + xtime_nsec_1 =
963 * (offset * adj_2) + xtime_nsec_2
964 * And we know:
965 * adj_2 = adj_1 + 1
966 * So:
967 * (offset * adj_1) + xtime_nsec_1 =
968 * (offset * (adj_1+1)) + xtime_nsec_2
969 * (offset * adj_1) + xtime_nsec_1 =
970 * (offset * adj_1) + offset + xtime_nsec_2
971 * Canceling the sides:
972 * xtime_nsec_1 = offset + xtime_nsec_2
973 * Which gives us:
974 * xtime_nsec_2 = xtime_nsec_1 - offset
975 * Which simplfies to:
976 * xtime_nsec -= offset
977 *
978 * XXX - TODO: Doc ntp_error calculation.
979 */
John Stultzf726a692012-07-13 01:21:57 -0400980 tk->mult += adj;
981 tk->xtime_interval += interval;
982 tk->xtime_nsec -= offset;
983 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -0400984
Ingo Molnar1d17d172012-08-04 21:21:14 +0200985out_adjust:
John Stultz2a8c0882012-07-13 01:21:56 -0400986 /*
987 * It may be possible that when we entered this function, xtime_nsec
988 * was very small. Further, if we're slightly speeding the clocksource
989 * in the code above, its possible the required corrective factor to
990 * xtime_nsec could cause it to underflow.
991 *
992 * Now, since we already accumulated the second, cannot simply roll
993 * the accumulated second back, since the NTP subsystem has been
994 * notified via second_overflow. So instead we push xtime_nsec forward
995 * by the amount we underflowed, and add that amount into the error.
996 *
997 * We'll correct this error next time through this function, when
998 * xtime_nsec is not as small.
999 */
John Stultzf726a692012-07-13 01:21:57 -04001000 if (unlikely((s64)tk->xtime_nsec < 0)) {
1001 s64 neg = -(s64)tk->xtime_nsec;
1002 tk->xtime_nsec = 0;
1003 tk->ntp_error += neg << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -04001004 }
1005
john stultz85240702007-05-08 00:27:59 -07001006}
1007
1008/**
John Stultz1f4f9482012-07-13 01:21:54 -04001009 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
1010 *
1011 * Helper function that accumulates a the nsecs greater then a second
1012 * from the xtime_nsec field to the xtime_secs field.
1013 * It also calls into the NTP code to handle leapsecond processing.
1014 *
1015 */
1016static inline void accumulate_nsecs_to_secs(struct timekeeper *tk)
1017{
1018 u64 nsecps = (u64)NSEC_PER_SEC << tk->shift;
1019
1020 while (tk->xtime_nsec >= nsecps) {
1021 int leap;
1022
1023 tk->xtime_nsec -= nsecps;
1024 tk->xtime_sec++;
1025
1026 /* Figure out if its a leap sec and apply if needed */
1027 leap = second_overflow(tk->xtime_sec);
John Stultz6d0ef902012-07-27 14:48:12 -04001028 if (unlikely(leap)) {
1029 struct timespec ts;
John Stultz1f4f9482012-07-13 01:21:54 -04001030
John Stultz6d0ef902012-07-27 14:48:12 -04001031 tk->xtime_sec += leap;
1032
1033 ts.tv_sec = leap;
1034 ts.tv_nsec = 0;
1035 tk_set_wall_to_mono(tk,
1036 timespec_sub(tk->wall_to_monotonic, ts));
1037
1038 clock_was_set_delayed();
1039 }
John Stultz1f4f9482012-07-13 01:21:54 -04001040 }
1041}
1042
John Stultz1f4f9482012-07-13 01:21:54 -04001043/**
john stultza092ff02009-10-02 16:17:53 -07001044 * logarithmic_accumulation - shifted accumulation of cycles
1045 *
1046 * This functions accumulates a shifted interval of cycles into
1047 * into a shifted interval nanoseconds. Allows for O(log) accumulation
1048 * loop.
1049 *
1050 * Returns the unconsumed cycles.
1051 */
John Stultzf726a692012-07-13 01:21:57 -04001052static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
1053 u32 shift)
john stultza092ff02009-10-02 16:17:53 -07001054{
Jason Wesseldeda2e82010-08-09 14:20:09 -07001055 u64 raw_nsecs;
john stultza092ff02009-10-02 16:17:53 -07001056
John Stultzf726a692012-07-13 01:21:57 -04001057 /* If the offset is smaller then a shifted interval, do nothing */
1058 if (offset < tk->cycle_interval<<shift)
john stultza092ff02009-10-02 16:17:53 -07001059 return offset;
1060
1061 /* Accumulate one shifted interval */
John Stultzf726a692012-07-13 01:21:57 -04001062 offset -= tk->cycle_interval << shift;
1063 tk->clock->cycle_last += tk->cycle_interval << shift;
john stultza092ff02009-10-02 16:17:53 -07001064
John Stultzf726a692012-07-13 01:21:57 -04001065 tk->xtime_nsec += tk->xtime_interval << shift;
1066 accumulate_nsecs_to_secs(tk);
john stultza092ff02009-10-02 16:17:53 -07001067
Jason Wesseldeda2e82010-08-09 14:20:09 -07001068 /* Accumulate raw time */
Dan Carpenter5b3900c2012-10-09 10:18:23 +03001069 raw_nsecs = (u64)tk->raw_interval << shift;
John Stultzf726a692012-07-13 01:21:57 -04001070 raw_nsecs += tk->raw_time.tv_nsec;
John Stultzc7dcf872010-08-13 11:30:58 -07001071 if (raw_nsecs >= NSEC_PER_SEC) {
1072 u64 raw_secs = raw_nsecs;
1073 raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
John Stultzf726a692012-07-13 01:21:57 -04001074 tk->raw_time.tv_sec += raw_secs;
john stultza092ff02009-10-02 16:17:53 -07001075 }
John Stultzf726a692012-07-13 01:21:57 -04001076 tk->raw_time.tv_nsec = raw_nsecs;
john stultza092ff02009-10-02 16:17:53 -07001077
1078 /* Accumulate error between NTP and clock interval */
John Stultzf726a692012-07-13 01:21:57 -04001079 tk->ntp_error += ntp_tick_length() << shift;
1080 tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
1081 (tk->ntp_error_shift + shift);
john stultza092ff02009-10-02 16:17:53 -07001082
1083 return offset;
1084}
1085
John Stultz92bb1fc2012-09-04 15:38:12 -04001086#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
1087static inline void old_vsyscall_fixup(struct timekeeper *tk)
1088{
1089 s64 remainder;
1090
1091 /*
1092 * Store only full nanoseconds into xtime_nsec after rounding
1093 * it up and add the remainder to the error difference.
1094 * XXX - This is necessary to avoid small 1ns inconsistnecies caused
1095 * by truncating the remainder in vsyscalls. However, it causes
1096 * additional work to be done in timekeeping_adjust(). Once
1097 * the vsyscall implementations are converted to use xtime_nsec
1098 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
1099 * users are removed, this can be killed.
1100 */
1101 remainder = tk->xtime_nsec & ((1ULL << tk->shift) - 1);
1102 tk->xtime_nsec -= remainder;
1103 tk->xtime_nsec += 1ULL << tk->shift;
1104 tk->ntp_error += remainder << tk->ntp_error_shift;
1105
1106}
1107#else
1108#define old_vsyscall_fixup(tk)
1109#endif
1110
1111
1112
john stultz85240702007-05-08 00:27:59 -07001113/**
1114 * update_wall_time - Uses the current clocksource to increment the wall time
1115 *
john stultz85240702007-05-08 00:27:59 -07001116 */
Torben Hohn871cf1e2011-01-27 15:58:55 +01001117static void update_wall_time(void)
john stultz85240702007-05-08 00:27:59 -07001118{
Martin Schwidefsky155ec602009-08-14 15:47:26 +02001119 struct clocksource *clock;
John Stultz4e250fd2012-07-27 14:48:13 -04001120 struct timekeeper *tk = &timekeeper;
john stultz85240702007-05-08 00:27:59 -07001121 cycle_t offset;
john stultza092ff02009-10-02 16:17:53 -07001122 int shift = 0, maxshift;
John Stultz70471f22011-11-14 12:48:10 -08001123 unsigned long flags;
1124
John Stultz4e250fd2012-07-27 14:48:13 -04001125 write_seqlock_irqsave(&tk->lock, flags);
john stultz85240702007-05-08 00:27:59 -07001126
1127 /* Make sure we're fully resumed: */
1128 if (unlikely(timekeeping_suspended))
John Stultz70471f22011-11-14 12:48:10 -08001129 goto out;
john stultz85240702007-05-08 00:27:59 -07001130
John Stultz4e250fd2012-07-27 14:48:13 -04001131 clock = tk->clock;
John Stultz592913e2010-07-13 17:56:20 -07001132
1133#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
John Stultz4e250fd2012-07-27 14:48:13 -04001134 offset = tk->cycle_interval;
John Stultz592913e2010-07-13 17:56:20 -07001135#else
1136 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
john stultz85240702007-05-08 00:27:59 -07001137#endif
john stultz85240702007-05-08 00:27:59 -07001138
John Stultzbf2ac312012-08-21 20:30:49 -04001139 /* Check if there's really nothing to do */
1140 if (offset < tk->cycle_interval)
1141 goto out;
1142
john stultza092ff02009-10-02 16:17:53 -07001143 /*
1144 * With NO_HZ we may have to accumulate many cycle_intervals
1145 * (think "ticks") worth of time at once. To do this efficiently,
1146 * we calculate the largest doubling multiple of cycle_intervals
Jim Cromie88b28ad2012-03-14 21:28:56 -06001147 * that is smaller than the offset. We then accumulate that
john stultza092ff02009-10-02 16:17:53 -07001148 * chunk in one go, and then try to consume the next smaller
1149 * doubled multiple.
john stultz85240702007-05-08 00:27:59 -07001150 */
John Stultz4e250fd2012-07-27 14:48:13 -04001151 shift = ilog2(offset) - ilog2(tk->cycle_interval);
john stultza092ff02009-10-02 16:17:53 -07001152 shift = max(0, shift);
Jim Cromie88b28ad2012-03-14 21:28:56 -06001153 /* Bound shift to one less than what overflows tick_length */
John Stultzea7cf492011-11-14 13:18:07 -08001154 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
john stultza092ff02009-10-02 16:17:53 -07001155 shift = min(shift, maxshift);
John Stultz4e250fd2012-07-27 14:48:13 -04001156 while (offset >= tk->cycle_interval) {
1157 offset = logarithmic_accumulation(tk, offset, shift);
1158 if (offset < tk->cycle_interval<<shift)
John Stultz830ec042010-03-18 14:47:30 -07001159 shift--;
john stultz85240702007-05-08 00:27:59 -07001160 }
1161
1162 /* correct the clock when NTP error is too big */
John Stultz4e250fd2012-07-27 14:48:13 -04001163 timekeeping_adjust(tk, offset);
john stultz85240702007-05-08 00:27:59 -07001164
John Stultz6a867a32010-04-06 14:30:51 -07001165 /*
John Stultz92bb1fc2012-09-04 15:38:12 -04001166 * XXX This can be killed once everyone converts
1167 * to the new update_vsyscall.
1168 */
1169 old_vsyscall_fixup(tk);
john stultz85240702007-05-08 00:27:59 -07001170
John Stultz6a867a32010-04-06 14:30:51 -07001171 /*
1172 * Finally, make sure that after the rounding
John Stultz1e75fa82012-07-13 01:21:53 -04001173 * xtime_nsec isn't larger than NSEC_PER_SEC
John Stultz6a867a32010-04-06 14:30:51 -07001174 */
John Stultz4e250fd2012-07-27 14:48:13 -04001175 accumulate_nsecs_to_secs(tk);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001176
John Stultz4e250fd2012-07-27 14:48:13 -04001177 timekeeping_update(tk, false);
John Stultz70471f22011-11-14 12:48:10 -08001178
1179out:
John Stultz4e250fd2012-07-27 14:48:13 -04001180 write_sequnlock_irqrestore(&tk->lock, flags);
John Stultz70471f22011-11-14 12:48:10 -08001181
john stultz85240702007-05-08 00:27:59 -07001182}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001183
1184/**
1185 * getboottime - Return the real time of system boot.
1186 * @ts: pointer to the timespec to be set
1187 *
John Stultzabb3a4e2011-02-14 17:52:09 -08001188 * Returns the wall-time of boot in a timespec.
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001189 *
1190 * This is based on the wall_to_monotonic offset and the total suspend
1191 * time. Calls to settimeofday will affect the value returned (which
1192 * basically means that however wrong your real time clock is at boot time,
1193 * you get the right time here).
1194 */
1195void getboottime(struct timespec *ts)
1196{
John Stultz4e250fd2012-07-27 14:48:13 -04001197 struct timekeeper *tk = &timekeeper;
Hiroshi Shimamoto36d47482009-08-25 15:08:30 +09001198 struct timespec boottime = {
John Stultz4e250fd2012-07-27 14:48:13 -04001199 .tv_sec = tk->wall_to_monotonic.tv_sec +
1200 tk->total_sleep_time.tv_sec,
1201 .tv_nsec = tk->wall_to_monotonic.tv_nsec +
1202 tk->total_sleep_time.tv_nsec
Hiroshi Shimamoto36d47482009-08-25 15:08:30 +09001203 };
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001204
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001205 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001206}
Jason Wangc93d89f2010-01-27 19:13:40 +08001207EXPORT_SYMBOL_GPL(getboottime);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001208
John Stultzabb3a4e2011-02-14 17:52:09 -08001209/**
1210 * get_monotonic_boottime - Returns monotonic time since boot
1211 * @ts: pointer to the timespec to be set
1212 *
1213 * Returns the monotonic time since boot in a timespec.
1214 *
1215 * This is similar to CLOCK_MONTONIC/ktime_get_ts, but also
1216 * includes the time spent in suspend.
1217 */
1218void get_monotonic_boottime(struct timespec *ts)
1219{
John Stultz4e250fd2012-07-27 14:48:13 -04001220 struct timekeeper *tk = &timekeeper;
John Stultzabb3a4e2011-02-14 17:52:09 -08001221 struct timespec tomono, sleep;
John Stultzec145ba2012-09-11 19:26:03 -04001222 s64 nsec;
John Stultzabb3a4e2011-02-14 17:52:09 -08001223 unsigned int seq;
John Stultzabb3a4e2011-02-14 17:52:09 -08001224
1225 WARN_ON(timekeeping_suspended);
1226
1227 do {
John Stultz4e250fd2012-07-27 14:48:13 -04001228 seq = read_seqbegin(&tk->lock);
1229 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -04001230 nsec = timekeeping_get_ns(tk);
John Stultz4e250fd2012-07-27 14:48:13 -04001231 tomono = tk->wall_to_monotonic;
1232 sleep = tk->total_sleep_time;
John Stultzabb3a4e2011-02-14 17:52:09 -08001233
John Stultz4e250fd2012-07-27 14:48:13 -04001234 } while (read_seqretry(&tk->lock, seq));
John Stultzabb3a4e2011-02-14 17:52:09 -08001235
John Stultzec145ba2012-09-11 19:26:03 -04001236 ts->tv_sec += tomono.tv_sec + sleep.tv_sec;
1237 ts->tv_nsec = 0;
1238 timespec_add_ns(ts, nsec + tomono.tv_nsec + sleep.tv_nsec);
John Stultzabb3a4e2011-02-14 17:52:09 -08001239}
1240EXPORT_SYMBOL_GPL(get_monotonic_boottime);
1241
1242/**
1243 * ktime_get_boottime - Returns monotonic time since boot in a ktime
1244 *
1245 * Returns the monotonic time since boot in a ktime
1246 *
1247 * This is similar to CLOCK_MONTONIC/ktime_get, but also
1248 * includes the time spent in suspend.
1249 */
1250ktime_t ktime_get_boottime(void)
1251{
1252 struct timespec ts;
1253
1254 get_monotonic_boottime(&ts);
1255 return timespec_to_ktime(ts);
1256}
1257EXPORT_SYMBOL_GPL(ktime_get_boottime);
1258
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001259/**
1260 * monotonic_to_bootbased - Convert the monotonic time to boot based.
1261 * @ts: pointer to the timespec to be converted
1262 */
1263void monotonic_to_bootbased(struct timespec *ts)
1264{
John Stultz4e250fd2012-07-27 14:48:13 -04001265 struct timekeeper *tk = &timekeeper;
1266
1267 *ts = timespec_add(*ts, tk->total_sleep_time);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001268}
Jason Wangc93d89f2010-01-27 19:13:40 +08001269EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
john stultz2c6b47d2007-07-24 17:47:43 -07001270
john stultz17c38b72007-07-24 18:38:34 -07001271unsigned long get_seconds(void)
1272{
John Stultz4e250fd2012-07-27 14:48:13 -04001273 struct timekeeper *tk = &timekeeper;
1274
1275 return tk->xtime_sec;
john stultz17c38b72007-07-24 18:38:34 -07001276}
1277EXPORT_SYMBOL(get_seconds);
1278
john stultzda15cfd2009-08-19 19:13:34 -07001279struct timespec __current_kernel_time(void)
1280{
John Stultz4e250fd2012-07-27 14:48:13 -04001281 struct timekeeper *tk = &timekeeper;
1282
1283 return tk_xtime(tk);
john stultzda15cfd2009-08-19 19:13:34 -07001284}
john stultz17c38b72007-07-24 18:38:34 -07001285
john stultz2c6b47d2007-07-24 17:47:43 -07001286struct timespec current_kernel_time(void)
1287{
John Stultz4e250fd2012-07-27 14:48:13 -04001288 struct timekeeper *tk = &timekeeper;
john stultz2c6b47d2007-07-24 17:47:43 -07001289 struct timespec now;
1290 unsigned long seq;
1291
1292 do {
John Stultz4e250fd2012-07-27 14:48:13 -04001293 seq = read_seqbegin(&tk->lock);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001294
John Stultz4e250fd2012-07-27 14:48:13 -04001295 now = tk_xtime(tk);
1296 } while (read_seqretry(&tk->lock, seq));
john stultz2c6b47d2007-07-24 17:47:43 -07001297
1298 return now;
1299}
john stultz2c6b47d2007-07-24 17:47:43 -07001300EXPORT_SYMBOL(current_kernel_time);
john stultzda15cfd2009-08-19 19:13:34 -07001301
1302struct timespec get_monotonic_coarse(void)
1303{
John Stultz4e250fd2012-07-27 14:48:13 -04001304 struct timekeeper *tk = &timekeeper;
john stultzda15cfd2009-08-19 19:13:34 -07001305 struct timespec now, mono;
1306 unsigned long seq;
1307
1308 do {
John Stultz4e250fd2012-07-27 14:48:13 -04001309 seq = read_seqbegin(&tk->lock);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001310
John Stultz4e250fd2012-07-27 14:48:13 -04001311 now = tk_xtime(tk);
1312 mono = tk->wall_to_monotonic;
1313 } while (read_seqretry(&tk->lock, seq));
john stultzda15cfd2009-08-19 19:13:34 -07001314
1315 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
1316 now.tv_nsec + mono.tv_nsec);
1317 return now;
1318}
Torben Hohn871cf1e2011-01-27 15:58:55 +01001319
1320/*
John Stultzd6ad4182012-02-28 16:50:11 -08001321 * Must hold jiffies_lock
Torben Hohn871cf1e2011-01-27 15:58:55 +01001322 */
1323void do_timer(unsigned long ticks)
1324{
1325 jiffies_64 += ticks;
1326 update_wall_time();
1327 calc_global_load(ticks);
1328}
Torben Hohn48cf76f72011-01-27 15:59:05 +01001329
1330/**
John Stultz314ac372011-02-14 18:43:08 -08001331 * get_xtime_and_monotonic_and_sleep_offset() - get xtime, wall_to_monotonic,
1332 * and sleep offsets.
Torben Hohn48cf76f72011-01-27 15:59:05 +01001333 * @xtim: pointer to timespec to be set with xtime
1334 * @wtom: pointer to timespec to be set with wall_to_monotonic
John Stultz314ac372011-02-14 18:43:08 -08001335 * @sleep: pointer to timespec to be set with time in suspend
Torben Hohn48cf76f72011-01-27 15:59:05 +01001336 */
John Stultz314ac372011-02-14 18:43:08 -08001337void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
1338 struct timespec *wtom, struct timespec *sleep)
Torben Hohn48cf76f72011-01-27 15:59:05 +01001339{
John Stultz4e250fd2012-07-27 14:48:13 -04001340 struct timekeeper *tk = &timekeeper;
Torben Hohn48cf76f72011-01-27 15:59:05 +01001341 unsigned long seq;
1342
1343 do {
John Stultz4e250fd2012-07-27 14:48:13 -04001344 seq = read_seqbegin(&tk->lock);
1345 *xtim = tk_xtime(tk);
1346 *wtom = tk->wall_to_monotonic;
1347 *sleep = tk->total_sleep_time;
1348 } while (read_seqretry(&tk->lock, seq));
Torben Hohn48cf76f72011-01-27 15:59:05 +01001349}
Torben Hohnf0af911a92011-01-27 15:59:10 +01001350
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001351#ifdef CONFIG_HIGH_RES_TIMERS
1352/**
1353 * ktime_get_update_offsets - hrtimer helper
1354 * @offs_real: pointer to storage for monotonic -> realtime offset
1355 * @offs_boot: pointer to storage for monotonic -> boottime offset
1356 *
1357 * Returns current monotonic time and updates the offsets
1358 * Called from hrtimer_interupt() or retrigger_next_event()
1359 */
1360ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot)
1361{
John Stultz4e250fd2012-07-27 14:48:13 -04001362 struct timekeeper *tk = &timekeeper;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001363 ktime_t now;
1364 unsigned int seq;
1365 u64 secs, nsecs;
1366
1367 do {
John Stultz4e250fd2012-07-27 14:48:13 -04001368 seq = read_seqbegin(&tk->lock);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001369
John Stultz4e250fd2012-07-27 14:48:13 -04001370 secs = tk->xtime_sec;
1371 nsecs = timekeeping_get_ns(tk);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001372
John Stultz4e250fd2012-07-27 14:48:13 -04001373 *offs_real = tk->offs_real;
1374 *offs_boot = tk->offs_boot;
1375 } while (read_seqretry(&tk->lock, seq));
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001376
1377 now = ktime_add_ns(ktime_set(secs, 0), nsecs);
1378 now = ktime_sub(now, *offs_real);
1379 return now;
1380}
1381#endif
1382
Torben Hohnf0af911a92011-01-27 15:59:10 +01001383/**
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001384 * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
1385 */
1386ktime_t ktime_get_monotonic_offset(void)
1387{
John Stultz4e250fd2012-07-27 14:48:13 -04001388 struct timekeeper *tk = &timekeeper;
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001389 unsigned long seq;
1390 struct timespec wtom;
1391
1392 do {
John Stultz4e250fd2012-07-27 14:48:13 -04001393 seq = read_seqbegin(&tk->lock);
1394 wtom = tk->wall_to_monotonic;
1395 } while (read_seqretry(&tk->lock, seq));
John Stultz70471f22011-11-14 12:48:10 -08001396
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001397 return timespec_to_ktime(wtom);
1398}
John Stultza80b83b2012-02-03 00:19:07 -08001399EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset);
1400
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001401/**
Torben Hohnf0af911a92011-01-27 15:59:10 +01001402 * xtime_update() - advances the timekeeping infrastructure
1403 * @ticks: number of ticks, that have elapsed since the last call.
1404 *
1405 * Must be called with interrupts disabled.
1406 */
1407void xtime_update(unsigned long ticks)
1408{
John Stultzd6ad4182012-02-28 16:50:11 -08001409 write_seqlock(&jiffies_lock);
Torben Hohnf0af911a92011-01-27 15:59:10 +01001410 do_timer(ticks);
John Stultzd6ad4182012-02-28 16:50:11 -08001411 write_sequnlock(&jiffies_lock);
Torben Hohnf0af911a92011-01-27 15:59:10 +01001412}