blob: 838fc0777b68a26983ec61a0fa2e560c85fa083a [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
H Hartley Sweetenafa14e72011-01-11 17:59:38 -060030static struct timekeeper timekeeper;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +000031static DEFINE_RAW_SPINLOCK(timekeeper_lock);
32static seqcount_t timekeeper_seq;
Thomas Gleixner48cdc132013-02-21 22:51:40 +000033static struct timekeeper shadow_timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020034
John Stultz8fcce542011-11-14 11:46:39 -080035/* flag for if timekeeping is suspended */
36int __read_mostly timekeeping_suspended;
37
Feng Tang31ade302013-01-16 00:09:47 +080038/* Flag for if there is a persistent clock on this platform */
39bool __read_mostly persistent_clock_exist = false;
40
John Stultz1e75fa82012-07-13 01:21:53 -040041static inline void tk_normalize_xtime(struct timekeeper *tk)
42{
43 while (tk->xtime_nsec >= ((u64)NSEC_PER_SEC << tk->shift)) {
44 tk->xtime_nsec -= (u64)NSEC_PER_SEC << tk->shift;
45 tk->xtime_sec++;
46 }
47}
John Stultz8fcce542011-11-14 11:46:39 -080048
John Stultz1e75fa82012-07-13 01:21:53 -040049static void tk_set_xtime(struct timekeeper *tk, const struct timespec *ts)
50{
51 tk->xtime_sec = ts->tv_sec;
John Stultzb44d50d2012-07-23 16:22:37 -040052 tk->xtime_nsec = (u64)ts->tv_nsec << tk->shift;
John Stultz1e75fa82012-07-13 01:21:53 -040053}
54
55static void tk_xtime_add(struct timekeeper *tk, const struct timespec *ts)
56{
57 tk->xtime_sec += ts->tv_sec;
John Stultzb44d50d2012-07-23 16:22:37 -040058 tk->xtime_nsec += (u64)ts->tv_nsec << tk->shift;
John Stultz784ffcb2012-08-21 20:30:46 -040059 tk_normalize_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -040060}
John Stultz8fcce542011-11-14 11:46:39 -080061
John Stultz6d0ef902012-07-27 14:48:12 -040062static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec wtm)
63{
64 struct timespec tmp;
65
66 /*
67 * Verify consistency of: offset_real = -wall_to_monotonic
68 * before modifying anything
69 */
70 set_normalized_timespec(&tmp, -tk->wall_to_monotonic.tv_sec,
71 -tk->wall_to_monotonic.tv_nsec);
72 WARN_ON_ONCE(tk->offs_real.tv64 != timespec_to_ktime(tmp).tv64);
73 tk->wall_to_monotonic = wtm;
74 set_normalized_timespec(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
75 tk->offs_real = timespec_to_ktime(tmp);
John Stultz90adda92013-01-21 17:00:11 -080076 tk->offs_tai = ktime_sub(tk->offs_real, ktime_set(tk->tai_offset, 0));
John Stultz6d0ef902012-07-27 14:48:12 -040077}
78
79static void tk_set_sleep_time(struct timekeeper *tk, struct timespec t)
80{
81 /* Verify consistency before modifying */
82 WARN_ON_ONCE(tk->offs_boot.tv64 != timespec_to_ktime(tk->total_sleep_time).tv64);
83
84 tk->total_sleep_time = t;
85 tk->offs_boot = timespec_to_ktime(t);
86}
87
Martin Schwidefsky155ec602009-08-14 15:47:26 +020088/**
89 * timekeeper_setup_internals - Set up internals to use clocksource clock.
90 *
91 * @clock: Pointer to clocksource.
92 *
93 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
94 * pair and interval request.
95 *
96 * Unless you're the timekeeping code, you should not be using this!
97 */
John Stultzf726a692012-07-13 01:21:57 -040098static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
Martin Schwidefsky155ec602009-08-14 15:47:26 +020099{
100 cycle_t interval;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700101 u64 tmp, ntpinterval;
John Stultz1e75fa82012-07-13 01:21:53 -0400102 struct clocksource *old_clock;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200103
John Stultzf726a692012-07-13 01:21:57 -0400104 old_clock = tk->clock;
105 tk->clock = clock;
Thomas Gleixner14a3b6a2013-02-21 22:51:38 +0000106 tk->cycle_last = clock->cycle_last = clock->read(clock);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200107
108 /* Do the ns -> cycle conversion first, using original mult */
109 tmp = NTP_INTERVAL_LENGTH;
110 tmp <<= clock->shift;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700111 ntpinterval = tmp;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200112 tmp += clock->mult/2;
113 do_div(tmp, clock->mult);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200114 if (tmp == 0)
115 tmp = 1;
116
117 interval = (cycle_t) tmp;
John Stultzf726a692012-07-13 01:21:57 -0400118 tk->cycle_interval = interval;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200119
120 /* Go back from cycles -> shifted ns */
John Stultzf726a692012-07-13 01:21:57 -0400121 tk->xtime_interval = (u64) interval * clock->mult;
122 tk->xtime_remainder = ntpinterval - tk->xtime_interval;
123 tk->raw_interval =
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200124 ((u64) interval * clock->mult) >> clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200125
John Stultz1e75fa82012-07-13 01:21:53 -0400126 /* if changing clocks, convert xtime_nsec shift units */
127 if (old_clock) {
128 int shift_change = clock->shift - old_clock->shift;
129 if (shift_change < 0)
John Stultzf726a692012-07-13 01:21:57 -0400130 tk->xtime_nsec >>= -shift_change;
John Stultz1e75fa82012-07-13 01:21:53 -0400131 else
John Stultzf726a692012-07-13 01:21:57 -0400132 tk->xtime_nsec <<= shift_change;
John Stultz1e75fa82012-07-13 01:21:53 -0400133 }
John Stultzf726a692012-07-13 01:21:57 -0400134 tk->shift = clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200135
John Stultzf726a692012-07-13 01:21:57 -0400136 tk->ntp_error = 0;
137 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200138
139 /*
140 * The timekeeper keeps its own mult values for the currently
141 * active clocksource. These value will be adjusted via NTP
142 * to counteract clock drifting.
143 */
John Stultzf726a692012-07-13 01:21:57 -0400144 tk->mult = clock->mult;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200145}
john stultz85240702007-05-08 00:27:59 -0700146
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200147/* Timekeeper helper functions. */
Stephen Warren7b1f6202012-11-07 17:58:54 -0700148
149#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
150u32 (*arch_gettimeoffset)(void);
151
152u32 get_arch_timeoffset(void)
153{
154 if (likely(arch_gettimeoffset))
155 return arch_gettimeoffset();
156 return 0;
157}
158#else
159static inline u32 get_arch_timeoffset(void) { return 0; }
160#endif
161
John Stultzf726a692012-07-13 01:21:57 -0400162static inline s64 timekeeping_get_ns(struct timekeeper *tk)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200163{
164 cycle_t cycle_now, cycle_delta;
165 struct clocksource *clock;
John Stultz1e75fa82012-07-13 01:21:53 -0400166 s64 nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200167
168 /* read clocksource: */
John Stultzf726a692012-07-13 01:21:57 -0400169 clock = tk->clock;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200170 cycle_now = clock->read(clock);
171
172 /* calculate the delta since the last update_wall_time: */
173 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
174
John Stultzf726a692012-07-13 01:21:57 -0400175 nsec = cycle_delta * tk->mult + tk->xtime_nsec;
176 nsec >>= tk->shift;
John Stultzf2a5a082012-07-13 01:21:55 -0400177
Stephen Warren7b1f6202012-11-07 17:58:54 -0700178 /* If arch requires, add in get_arch_timeoffset() */
179 return nsec + get_arch_timeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200180}
181
John Stultzf726a692012-07-13 01:21:57 -0400182static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200183{
184 cycle_t cycle_now, cycle_delta;
185 struct clocksource *clock;
John Stultzf2a5a082012-07-13 01:21:55 -0400186 s64 nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200187
188 /* read clocksource: */
John Stultzf726a692012-07-13 01:21:57 -0400189 clock = tk->clock;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200190 cycle_now = clock->read(clock);
191
192 /* calculate the delta since the last update_wall_time: */
193 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
194
John Stultzf2a5a082012-07-13 01:21:55 -0400195 /* convert delta to nanoseconds. */
196 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
197
Stephen Warren7b1f6202012-11-07 17:58:54 -0700198 /* If arch requires, add in get_arch_timeoffset() */
199 return nsec + get_arch_timeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200200}
201
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200202static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
203
204static void update_pvclock_gtod(struct timekeeper *tk)
205{
206 raw_notifier_call_chain(&pvclock_gtod_chain, 0, tk);
207}
208
209/**
210 * pvclock_gtod_register_notifier - register a pvclock timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200211 */
212int pvclock_gtod_register_notifier(struct notifier_block *nb)
213{
214 struct timekeeper *tk = &timekeeper;
215 unsigned long flags;
216 int ret;
217
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000218 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200219 ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200220 update_pvclock_gtod(tk);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000221 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200222
223 return ret;
224}
225EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
226
227/**
228 * pvclock_gtod_unregister_notifier - unregister a pvclock
229 * timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200230 */
231int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
232{
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200233 unsigned long flags;
234 int ret;
235
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000236 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200237 ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000238 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200239
240 return ret;
241}
242EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
243
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000244/* must hold timekeeper_lock */
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000245static void timekeeping_update(struct timekeeper *tk, bool clearntp, bool mirror)
Thomas Gleixnercc062682011-11-13 23:19:49 +0000246{
247 if (clearntp) {
John Stultzf726a692012-07-13 01:21:57 -0400248 tk->ntp_error = 0;
Thomas Gleixnercc062682011-11-13 23:19:49 +0000249 ntp_clear();
250 }
John Stultz576094b2012-09-11 19:58:13 -0400251 update_vsyscall(tk);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200252 update_pvclock_gtod(tk);
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000253
254 if (mirror)
255 memcpy(&shadow_timekeeper, &timekeeper, sizeof(timekeeper));
Thomas Gleixnercc062682011-11-13 23:19:49 +0000256}
257
john stultz85240702007-05-08 00:27:59 -0700258/**
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200259 * timekeeping_forward_now - update clock to the current time
john stultz85240702007-05-08 00:27:59 -0700260 *
Roman Zippel9a055112008-08-20 16:37:28 -0700261 * Forward the current clock to update its state since the last call to
262 * update_wall_time(). This is useful before significant clock changes,
263 * as it avoids having to deal with this time offset explicitly.
john stultz85240702007-05-08 00:27:59 -0700264 */
John Stultzf726a692012-07-13 01:21:57 -0400265static void timekeeping_forward_now(struct timekeeper *tk)
john stultz85240702007-05-08 00:27:59 -0700266{
267 cycle_t cycle_now, cycle_delta;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200268 struct clocksource *clock;
Roman Zippel9a055112008-08-20 16:37:28 -0700269 s64 nsec;
john stultz85240702007-05-08 00:27:59 -0700270
John Stultzf726a692012-07-13 01:21:57 -0400271 clock = tk->clock;
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200272 cycle_now = clock->read(clock);
john stultz85240702007-05-08 00:27:59 -0700273 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
Thomas Gleixner14a3b6a2013-02-21 22:51:38 +0000274 tk->cycle_last = clock->cycle_last = cycle_now;
john stultz85240702007-05-08 00:27:59 -0700275
John Stultzf726a692012-07-13 01:21:57 -0400276 tk->xtime_nsec += cycle_delta * tk->mult;
john stultz7d275582009-05-01 13:10:26 -0700277
Stephen Warren7b1f6202012-11-07 17:58:54 -0700278 /* If arch requires, add in get_arch_timeoffset() */
279 tk->xtime_nsec += (u64)get_arch_timeoffset() << tk->shift;
john stultz7d275582009-05-01 13:10:26 -0700280
John Stultzf726a692012-07-13 01:21:57 -0400281 tk_normalize_xtime(tk);
John Stultz2d422442008-08-20 16:37:30 -0700282
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200283 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
John Stultzf726a692012-07-13 01:21:57 -0400284 timespec_add_ns(&tk->raw_time, nsec);
john stultz85240702007-05-08 00:27:59 -0700285}
286
287/**
Kees Cook1e817fb2012-11-19 10:26:16 -0800288 * __getnstimeofday - Returns the time of day in a timespec.
john stultz85240702007-05-08 00:27:59 -0700289 * @ts: pointer to the timespec to be set
290 *
Kees Cook1e817fb2012-11-19 10:26:16 -0800291 * Updates the time of day in the timespec.
292 * Returns 0 on success, or -ve when suspended (timespec will be undefined).
john stultz85240702007-05-08 00:27:59 -0700293 */
Kees Cook1e817fb2012-11-19 10:26:16 -0800294int __getnstimeofday(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700295{
John Stultz4e250fd2012-07-27 14:48:13 -0400296 struct timekeeper *tk = &timekeeper;
john stultz85240702007-05-08 00:27:59 -0700297 unsigned long seq;
John Stultz1e75fa82012-07-13 01:21:53 -0400298 s64 nsecs = 0;
john stultz85240702007-05-08 00:27:59 -0700299
300 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000301 seq = read_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700302
John Stultz4e250fd2012-07-27 14:48:13 -0400303 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -0400304 nsecs = timekeeping_get_ns(tk);
john stultz85240702007-05-08 00:27:59 -0700305
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000306 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultz85240702007-05-08 00:27:59 -0700307
John Stultzec145ba2012-09-11 19:26:03 -0400308 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700309 timespec_add_ns(ts, nsecs);
Kees Cook1e817fb2012-11-19 10:26:16 -0800310
311 /*
312 * Do not bail out early, in case there were callers still using
313 * the value, even in the face of the WARN_ON.
314 */
315 if (unlikely(timekeeping_suspended))
316 return -EAGAIN;
317 return 0;
318}
319EXPORT_SYMBOL(__getnstimeofday);
320
321/**
322 * getnstimeofday - Returns the time of day in a timespec.
323 * @ts: pointer to the timespec to be set
324 *
325 * Returns the time of day in a timespec (WARN if suspended).
326 */
327void getnstimeofday(struct timespec *ts)
328{
329 WARN_ON(__getnstimeofday(ts));
john stultz85240702007-05-08 00:27:59 -0700330}
john stultz85240702007-05-08 00:27:59 -0700331EXPORT_SYMBOL(getnstimeofday);
332
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200333ktime_t ktime_get(void)
334{
John Stultz4e250fd2012-07-27 14:48:13 -0400335 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200336 unsigned int seq;
337 s64 secs, nsecs;
338
339 WARN_ON(timekeeping_suspended);
340
341 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000342 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400343 secs = tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
344 nsecs = timekeeping_get_ns(tk) + tk->wall_to_monotonic.tv_nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200345
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000346 } while (read_seqcount_retry(&timekeeper_seq, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200347 /*
348 * Use ktime_set/ktime_add_ns to create a proper ktime on
349 * 32-bit architectures without CONFIG_KTIME_SCALAR.
350 */
351 return ktime_add_ns(ktime_set(secs, 0), nsecs);
352}
353EXPORT_SYMBOL_GPL(ktime_get);
354
355/**
356 * ktime_get_ts - get the monotonic clock in timespec format
357 * @ts: pointer to timespec variable
358 *
359 * The function calculates the monotonic clock from the realtime
360 * clock and the wall_to_monotonic offset and stores the result
361 * in normalized timespec format in the variable pointed to by @ts.
362 */
363void ktime_get_ts(struct timespec *ts)
364{
John Stultz4e250fd2012-07-27 14:48:13 -0400365 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200366 struct timespec tomono;
John Stultzec145ba2012-09-11 19:26:03 -0400367 s64 nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200368 unsigned int seq;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200369
370 WARN_ON(timekeeping_suspended);
371
372 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000373 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400374 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -0400375 nsec = timekeeping_get_ns(tk);
John Stultz4e250fd2012-07-27 14:48:13 -0400376 tomono = tk->wall_to_monotonic;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200377
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000378 } while (read_seqcount_retry(&timekeeper_seq, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200379
John Stultzec145ba2012-09-11 19:26:03 -0400380 ts->tv_sec += tomono.tv_sec;
381 ts->tv_nsec = 0;
382 timespec_add_ns(ts, nsec + tomono.tv_nsec);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200383}
384EXPORT_SYMBOL_GPL(ktime_get_ts);
385
John Stultz1ff3c962012-05-03 12:43:40 -0700386
387/**
388 * timekeeping_clocktai - Returns the TAI time of day in a timespec
389 * @ts: pointer to the timespec to be set
390 *
391 * Returns the time of day in a timespec.
392 */
393void timekeeping_clocktai(struct timespec *ts)
394{
395 struct timekeeper *tk = &timekeeper;
396 unsigned long seq;
397 u64 nsecs;
398
399 WARN_ON(timekeeping_suspended);
400
401 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000402 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz1ff3c962012-05-03 12:43:40 -0700403
404 ts->tv_sec = tk->xtime_sec + tk->tai_offset;
405 nsecs = timekeeping_get_ns(tk);
406
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000407 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz1ff3c962012-05-03 12:43:40 -0700408
409 ts->tv_nsec = 0;
410 timespec_add_ns(ts, nsecs);
411
412}
413EXPORT_SYMBOL(timekeeping_clocktai);
414
415
John Stultz90adda92013-01-21 17:00:11 -0800416/**
417 * ktime_get_clocktai - Returns the TAI time of day in a ktime
418 *
419 * Returns the time of day in a ktime.
420 */
421ktime_t ktime_get_clocktai(void)
422{
423 struct timespec ts;
424
425 timekeeping_clocktai(&ts);
426 return timespec_to_ktime(ts);
427}
428EXPORT_SYMBOL(ktime_get_clocktai);
429
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800430#ifdef CONFIG_NTP_PPS
431
432/**
433 * getnstime_raw_and_real - get day and raw monotonic time in timespec format
434 * @ts_raw: pointer to the timespec to be set to raw monotonic time
435 * @ts_real: pointer to the timespec to be set to the time of day
436 *
437 * This function reads both the time of day and raw monotonic time at the
438 * same time atomically and stores the resulting timestamps in timespec
439 * format.
440 */
441void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
442{
John Stultz4e250fd2012-07-27 14:48:13 -0400443 struct timekeeper *tk = &timekeeper;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800444 unsigned long seq;
445 s64 nsecs_raw, nsecs_real;
446
447 WARN_ON_ONCE(timekeeping_suspended);
448
449 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000450 seq = read_seqcount_begin(&timekeeper_seq);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800451
John Stultz4e250fd2012-07-27 14:48:13 -0400452 *ts_raw = tk->raw_time;
453 ts_real->tv_sec = tk->xtime_sec;
John Stultz1e75fa82012-07-13 01:21:53 -0400454 ts_real->tv_nsec = 0;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800455
John Stultz4e250fd2012-07-27 14:48:13 -0400456 nsecs_raw = timekeeping_get_ns_raw(tk);
457 nsecs_real = timekeeping_get_ns(tk);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800458
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000459 } while (read_seqcount_retry(&timekeeper_seq, seq));
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800460
461 timespec_add_ns(ts_raw, nsecs_raw);
462 timespec_add_ns(ts_real, nsecs_real);
463}
464EXPORT_SYMBOL(getnstime_raw_and_real);
465
466#endif /* CONFIG_NTP_PPS */
467
john stultz85240702007-05-08 00:27:59 -0700468/**
469 * do_gettimeofday - Returns the time of day in a timeval
470 * @tv: pointer to the timeval to be set
471 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100472 * NOTE: Users should be converted to using getnstimeofday()
john stultz85240702007-05-08 00:27:59 -0700473 */
474void do_gettimeofday(struct timeval *tv)
475{
476 struct timespec now;
477
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100478 getnstimeofday(&now);
john stultz85240702007-05-08 00:27:59 -0700479 tv->tv_sec = now.tv_sec;
480 tv->tv_usec = now.tv_nsec/1000;
481}
john stultz85240702007-05-08 00:27:59 -0700482EXPORT_SYMBOL(do_gettimeofday);
Richard Cochrand239f492012-04-27 10:12:42 +0200483
john stultz85240702007-05-08 00:27:59 -0700484/**
485 * do_settimeofday - Sets the time of day
486 * @tv: pointer to the timespec variable containing the new time
487 *
488 * Sets the time of day to the new time and update NTP and notify hrtimers
489 */
Richard Cochran1e6d7672011-02-01 13:50:58 +0000490int do_settimeofday(const struct timespec *tv)
john stultz85240702007-05-08 00:27:59 -0700491{
John Stultz4e250fd2012-07-27 14:48:13 -0400492 struct timekeeper *tk = &timekeeper;
John Stultz1e75fa82012-07-13 01:21:53 -0400493 struct timespec ts_delta, xt;
John Stultz92c1d3e2011-11-14 14:05:44 -0800494 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700495
John Stultzcee58482012-08-31 13:30:06 -0400496 if (!timespec_valid_strict(tv))
john stultz85240702007-05-08 00:27:59 -0700497 return -EINVAL;
498
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000499 raw_spin_lock_irqsave(&timekeeper_lock, flags);
500 write_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700501
John Stultz4e250fd2012-07-27 14:48:13 -0400502 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -0700503
John Stultz4e250fd2012-07-27 14:48:13 -0400504 xt = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -0400505 ts_delta.tv_sec = tv->tv_sec - xt.tv_sec;
506 ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec;
507
John Stultz4e250fd2012-07-27 14:48:13 -0400508 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, ts_delta));
john stultz85240702007-05-08 00:27:59 -0700509
John Stultz4e250fd2012-07-27 14:48:13 -0400510 tk_set_xtime(tk, tv);
John Stultz1e75fa82012-07-13 01:21:53 -0400511
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000512 timekeeping_update(tk, true, true);
john stultz85240702007-05-08 00:27:59 -0700513
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000514 write_seqcount_end(&timekeeper_seq);
515 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700516
517 /* signal hrtimers about time change */
518 clock_was_set();
519
520 return 0;
521}
john stultz85240702007-05-08 00:27:59 -0700522EXPORT_SYMBOL(do_settimeofday);
523
John Stultzc528f7c2011-02-01 13:52:17 +0000524/**
525 * timekeeping_inject_offset - Adds or subtracts from the current time.
526 * @tv: pointer to the timespec variable containing the offset
527 *
528 * Adds or subtracts an offset value from the current time.
529 */
530int timekeeping_inject_offset(struct timespec *ts)
531{
John Stultz4e250fd2012-07-27 14:48:13 -0400532 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800533 unsigned long flags;
John Stultz4e8b1452012-08-08 15:36:20 -0400534 struct timespec tmp;
535 int ret = 0;
John Stultzc528f7c2011-02-01 13:52:17 +0000536
537 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
538 return -EINVAL;
539
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000540 raw_spin_lock_irqsave(&timekeeper_lock, flags);
541 write_seqcount_begin(&timekeeper_seq);
John Stultzc528f7c2011-02-01 13:52:17 +0000542
John Stultz4e250fd2012-07-27 14:48:13 -0400543 timekeeping_forward_now(tk);
John Stultzc528f7c2011-02-01 13:52:17 +0000544
John Stultz4e8b1452012-08-08 15:36:20 -0400545 /* Make sure the proposed value is valid */
546 tmp = timespec_add(tk_xtime(tk), *ts);
John Stultzcee58482012-08-31 13:30:06 -0400547 if (!timespec_valid_strict(&tmp)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400548 ret = -EINVAL;
549 goto error;
550 }
John Stultz1e75fa82012-07-13 01:21:53 -0400551
John Stultz4e250fd2012-07-27 14:48:13 -0400552 tk_xtime_add(tk, ts);
553 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *ts));
John Stultzc528f7c2011-02-01 13:52:17 +0000554
John Stultz4e8b1452012-08-08 15:36:20 -0400555error: /* even if we error out, we forwarded the time, so call update */
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000556 timekeeping_update(tk, true, true);
John Stultzc528f7c2011-02-01 13:52:17 +0000557
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000558 write_seqcount_end(&timekeeper_seq);
559 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzc528f7c2011-02-01 13:52:17 +0000560
561 /* signal hrtimers about time change */
562 clock_was_set();
563
John Stultz4e8b1452012-08-08 15:36:20 -0400564 return ret;
John Stultzc528f7c2011-02-01 13:52:17 +0000565}
566EXPORT_SYMBOL(timekeeping_inject_offset);
567
John Stultzcc244dd2012-05-03 12:30:07 -0700568
569/**
570 * timekeeping_get_tai_offset - Returns current TAI offset from UTC
571 *
572 */
573s32 timekeeping_get_tai_offset(void)
574{
575 struct timekeeper *tk = &timekeeper;
576 unsigned int seq;
577 s32 ret;
578
579 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000580 seq = read_seqcount_begin(&timekeeper_seq);
John Stultzcc244dd2012-05-03 12:30:07 -0700581 ret = tk->tai_offset;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000582 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultzcc244dd2012-05-03 12:30:07 -0700583
584 return ret;
585}
586
587/**
588 * __timekeeping_set_tai_offset - Lock free worker function
589 *
590 */
Fengguang Wudd5d70e2013-03-25 12:24:24 -0700591static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
John Stultzcc244dd2012-05-03 12:30:07 -0700592{
593 tk->tai_offset = tai_offset;
John Stultz90adda92013-01-21 17:00:11 -0800594 tk->offs_tai = ktime_sub(tk->offs_real, ktime_set(tai_offset, 0));
John Stultzcc244dd2012-05-03 12:30:07 -0700595}
596
597/**
598 * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
599 *
600 */
601void timekeeping_set_tai_offset(s32 tai_offset)
602{
603 struct timekeeper *tk = &timekeeper;
604 unsigned long flags;
605
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000606 raw_spin_lock_irqsave(&timekeeper_lock, flags);
607 write_seqcount_begin(&timekeeper_seq);
John Stultzcc244dd2012-05-03 12:30:07 -0700608 __timekeeping_set_tai_offset(tk, tai_offset);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000609 write_seqcount_end(&timekeeper_seq);
610 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz4e8f8b32013-04-10 12:41:49 -0700611 clock_was_set();
John Stultzcc244dd2012-05-03 12:30:07 -0700612}
613
john stultz85240702007-05-08 00:27:59 -0700614/**
615 * change_clocksource - Swaps clocksources if a new one is available
616 *
617 * Accumulates current time interval and initializes new clocksource
618 */
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200619static int change_clocksource(void *data)
john stultz85240702007-05-08 00:27:59 -0700620{
John Stultz4e250fd2012-07-27 14:48:13 -0400621 struct timekeeper *tk = &timekeeper;
Magnus Damm4614e6a2009-04-21 12:24:02 -0700622 struct clocksource *new, *old;
John Stultzf695cf92012-03-14 16:38:15 -0700623 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700624
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200625 new = (struct clocksource *) data;
john stultz85240702007-05-08 00:27:59 -0700626
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000627 raw_spin_lock_irqsave(&timekeeper_lock, flags);
628 write_seqcount_begin(&timekeeper_seq);
John Stultzf695cf92012-03-14 16:38:15 -0700629
John Stultz4e250fd2012-07-27 14:48:13 -0400630 timekeeping_forward_now(tk);
Thomas Gleixner09ac3692013-04-25 20:31:44 +0000631 /*
632 * If the cs is in module, get a module reference. Succeeds
633 * for built-in code (owner == NULL) as well.
634 */
635 if (try_module_get(new->owner)) {
636 if (!new->enable || new->enable(new) == 0) {
637 old = tk->clock;
638 tk_setup_internals(tk, new);
639 if (old->disable)
640 old->disable(old);
641 module_put(old->owner);
642 } else {
643 module_put(new->owner);
644 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200645 }
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000646 timekeeping_update(tk, true, true);
John Stultzf695cf92012-03-14 16:38:15 -0700647
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000648 write_seqcount_end(&timekeeper_seq);
649 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzf695cf92012-03-14 16:38:15 -0700650
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200651 return 0;
652}
john stultz85240702007-05-08 00:27:59 -0700653
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200654/**
655 * timekeeping_notify - Install a new clock source
656 * @clock: pointer to the clock source
657 *
658 * This function is called from clocksource.c after a new, better clock
659 * source has been registered. The caller holds the clocksource_mutex.
660 */
Thomas Gleixnerba919d12013-04-25 20:31:44 +0000661int timekeeping_notify(struct clocksource *clock)
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200662{
John Stultz4e250fd2012-07-27 14:48:13 -0400663 struct timekeeper *tk = &timekeeper;
664
665 if (tk->clock == clock)
Thomas Gleixnerba919d12013-04-25 20:31:44 +0000666 return 0;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200667 stop_machine(change_clocksource, clock, NULL);
john stultz85240702007-05-08 00:27:59 -0700668 tick_clock_notify();
Thomas Gleixnerba919d12013-04-25 20:31:44 +0000669 return tk->clock == clock ? 0 : -1;
john stultz85240702007-05-08 00:27:59 -0700670}
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200671
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200672/**
673 * ktime_get_real - get the real (wall-) time in ktime_t format
674 *
675 * returns the time in ktime_t format
676 */
677ktime_t ktime_get_real(void)
678{
679 struct timespec now;
680
681 getnstimeofday(&now);
682
683 return timespec_to_ktime(now);
684}
685EXPORT_SYMBOL_GPL(ktime_get_real);
john stultz85240702007-05-08 00:27:59 -0700686
687/**
John Stultz2d422442008-08-20 16:37:30 -0700688 * getrawmonotonic - Returns the raw monotonic time in a timespec
689 * @ts: pointer to the timespec to be set
690 *
691 * Returns the raw monotonic time (completely un-modified by ntp)
692 */
693void getrawmonotonic(struct timespec *ts)
694{
John Stultz4e250fd2012-07-27 14:48:13 -0400695 struct timekeeper *tk = &timekeeper;
John Stultz2d422442008-08-20 16:37:30 -0700696 unsigned long seq;
697 s64 nsecs;
John Stultz2d422442008-08-20 16:37:30 -0700698
699 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000700 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400701 nsecs = timekeeping_get_ns_raw(tk);
702 *ts = tk->raw_time;
John Stultz2d422442008-08-20 16:37:30 -0700703
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000704 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz2d422442008-08-20 16:37:30 -0700705
706 timespec_add_ns(ts, nsecs);
707}
708EXPORT_SYMBOL(getrawmonotonic);
709
John Stultz2d422442008-08-20 16:37:30 -0700710/**
Li Zefancf4fc6c2008-02-08 04:19:24 -0800711 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -0700712 */
Li Zefancf4fc6c2008-02-08 04:19:24 -0800713int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -0700714{
John Stultz4e250fd2012-07-27 14:48:13 -0400715 struct timekeeper *tk = &timekeeper;
john stultz85240702007-05-08 00:27:59 -0700716 unsigned long seq;
717 int ret;
718
719 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000720 seq = read_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700721
John Stultz4e250fd2012-07-27 14:48:13 -0400722 ret = tk->clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -0700723
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000724 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultz85240702007-05-08 00:27:59 -0700725
726 return ret;
727}
728
729/**
Jon Hunter98962462009-08-18 12:45:10 -0500730 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
Jon Hunter98962462009-08-18 12:45:10 -0500731 */
732u64 timekeeping_max_deferment(void)
733{
John Stultz4e250fd2012-07-27 14:48:13 -0400734 struct timekeeper *tk = &timekeeper;
John Stultz70471f22011-11-14 12:48:10 -0800735 unsigned long seq;
736 u64 ret;
John Stultz42e71e82012-07-13 01:21:51 -0400737
John Stultz70471f22011-11-14 12:48:10 -0800738 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000739 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz70471f22011-11-14 12:48:10 -0800740
John Stultz4e250fd2012-07-27 14:48:13 -0400741 ret = tk->clock->max_idle_ns;
John Stultz70471f22011-11-14 12:48:10 -0800742
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000743 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz70471f22011-11-14 12:48:10 -0800744
745 return ret;
Jon Hunter98962462009-08-18 12:45:10 -0500746}
747
748/**
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200749 * read_persistent_clock - Return time from the persistent clock.
john stultz85240702007-05-08 00:27:59 -0700750 *
751 * Weak dummy function for arches that do not yet support it.
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200752 * Reads the time from the battery backed persistent clock.
753 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
john stultz85240702007-05-08 00:27:59 -0700754 *
755 * XXX - Do be sure to remove it once all arches implement it.
756 */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200757void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700758{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200759 ts->tv_sec = 0;
760 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700761}
762
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200763/**
764 * read_boot_clock - Return time of the system start.
765 *
766 * Weak dummy function for arches that do not yet support it.
767 * Function to read the exact time the system has been started.
768 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
769 *
770 * XXX - Do be sure to remove it once all arches implement it.
771 */
772void __attribute__((weak)) read_boot_clock(struct timespec *ts)
773{
774 ts->tv_sec = 0;
775 ts->tv_nsec = 0;
776}
777
john stultz85240702007-05-08 00:27:59 -0700778/*
779 * timekeeping_init - Initializes the clocksource and common timekeeping values
780 */
781void __init timekeeping_init(void)
782{
John Stultz4e250fd2012-07-27 14:48:13 -0400783 struct timekeeper *tk = &timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200784 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700785 unsigned long flags;
John Stultz6d0ef902012-07-27 14:48:12 -0400786 struct timespec now, boot, tmp;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200787
788 read_persistent_clock(&now);
Feng Tang31ade302013-01-16 00:09:47 +0800789
John Stultzcee58482012-08-31 13:30:06 -0400790 if (!timespec_valid_strict(&now)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400791 pr_warn("WARNING: Persistent clock returned invalid value!\n"
792 " Check your CMOS/BIOS settings.\n");
793 now.tv_sec = 0;
794 now.tv_nsec = 0;
Feng Tang31ade302013-01-16 00:09:47 +0800795 } else if (now.tv_sec || now.tv_nsec)
796 persistent_clock_exist = true;
John Stultz4e8b1452012-08-08 15:36:20 -0400797
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200798 read_boot_clock(&boot);
John Stultzcee58482012-08-31 13:30:06 -0400799 if (!timespec_valid_strict(&boot)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400800 pr_warn("WARNING: Boot clock returned invalid value!\n"
801 " Check your CMOS/BIOS settings.\n");
802 boot.tv_sec = 0;
803 boot.tv_nsec = 0;
804 }
john stultz85240702007-05-08 00:27:59 -0700805
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000806 raw_spin_lock_irqsave(&timekeeper_lock, flags);
807 write_seqcount_begin(&timekeeper_seq);
John Stultz06c017f2013-03-22 11:37:28 -0700808 ntp_init();
809
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200810 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200811 if (clock->enable)
812 clock->enable(clock);
John Stultz4e250fd2012-07-27 14:48:13 -0400813 tk_setup_internals(tk, clock);
john stultz85240702007-05-08 00:27:59 -0700814
John Stultz4e250fd2012-07-27 14:48:13 -0400815 tk_set_xtime(tk, &now);
816 tk->raw_time.tv_sec = 0;
817 tk->raw_time.tv_nsec = 0;
John Stultz1e75fa82012-07-13 01:21:53 -0400818 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
John Stultz4e250fd2012-07-27 14:48:13 -0400819 boot = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -0400820
John Stultz6d0ef902012-07-27 14:48:12 -0400821 set_normalized_timespec(&tmp, -boot.tv_sec, -boot.tv_nsec);
John Stultz4e250fd2012-07-27 14:48:13 -0400822 tk_set_wall_to_mono(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -0400823
824 tmp.tv_sec = 0;
825 tmp.tv_nsec = 0;
John Stultz4e250fd2012-07-27 14:48:13 -0400826 tk_set_sleep_time(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -0400827
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000828 memcpy(&shadow_timekeeper, &timekeeper, sizeof(timekeeper));
829
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000830 write_seqcount_end(&timekeeper_seq);
831 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700832}
833
john stultz85240702007-05-08 00:27:59 -0700834/* time in seconds when suspend began */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200835static struct timespec timekeeping_suspend_time;
john stultz85240702007-05-08 00:27:59 -0700836
837/**
John Stultz304529b2011-04-01 14:32:09 -0700838 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
839 * @delta: pointer to a timespec delta value
840 *
841 * Takes a timespec offset measuring a suspend interval and properly
842 * adds the sleep offset to the timekeeping variables.
843 */
John Stultzf726a692012-07-13 01:21:57 -0400844static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
845 struct timespec *delta)
John Stultz304529b2011-04-01 14:32:09 -0700846{
John Stultzcee58482012-08-31 13:30:06 -0400847 if (!timespec_valid_strict(delta)) {
John Stultzcbaa5152011-07-20 15:42:55 -0700848 printk(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid "
John Stultzcb5de2f8d2011-06-01 18:18:09 -0700849 "sleep delta value!\n");
850 return;
851 }
John Stultzf726a692012-07-13 01:21:57 -0400852 tk_xtime_add(tk, delta);
John Stultz6d0ef902012-07-27 14:48:12 -0400853 tk_set_wall_to_mono(tk, timespec_sub(tk->wall_to_monotonic, *delta));
854 tk_set_sleep_time(tk, timespec_add(tk->total_sleep_time, *delta));
Colin Cross5c835452013-05-21 22:32:14 -0700855 tk_debug_account_sleep_time(delta);
John Stultz304529b2011-04-01 14:32:09 -0700856}
857
John Stultz304529b2011-04-01 14:32:09 -0700858/**
859 * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
860 * @delta: pointer to a timespec delta value
861 *
862 * This hook is for architectures that cannot support read_persistent_clock
863 * because their RTC/persistent clock is only accessible when irqs are enabled.
864 *
865 * This function should only be called by rtc_resume(), and allows
866 * a suspend offset to be injected into the timekeeping values.
867 */
868void timekeeping_inject_sleeptime(struct timespec *delta)
869{
John Stultz4e250fd2012-07-27 14:48:13 -0400870 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800871 unsigned long flags;
John Stultz304529b2011-04-01 14:32:09 -0700872
Feng Tang31ade302013-01-16 00:09:47 +0800873 /*
874 * Make sure we don't set the clock twice, as timekeeping_resume()
875 * already did it
876 */
877 if (has_persistent_clock())
John Stultz304529b2011-04-01 14:32:09 -0700878 return;
879
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000880 raw_spin_lock_irqsave(&timekeeper_lock, flags);
881 write_seqcount_begin(&timekeeper_seq);
John Stultz70471f22011-11-14 12:48:10 -0800882
John Stultz4e250fd2012-07-27 14:48:13 -0400883 timekeeping_forward_now(tk);
John Stultz304529b2011-04-01 14:32:09 -0700884
John Stultz4e250fd2012-07-27 14:48:13 -0400885 __timekeeping_inject_sleeptime(tk, delta);
John Stultz304529b2011-04-01 14:32:09 -0700886
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000887 timekeeping_update(tk, true, true);
John Stultz304529b2011-04-01 14:32:09 -0700888
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000889 write_seqcount_end(&timekeeper_seq);
890 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz304529b2011-04-01 14:32:09 -0700891
892 /* signal hrtimers about time change */
893 clock_was_set();
894}
895
John Stultz304529b2011-04-01 14:32:09 -0700896/**
john stultz85240702007-05-08 00:27:59 -0700897 * timekeeping_resume - Resumes the generic timekeeping subsystem.
john stultz85240702007-05-08 00:27:59 -0700898 *
899 * This is for the generic clocksource timekeeping.
900 * xtime/wall_to_monotonic/jiffies/etc are
901 * still managed by arch specific suspend/resume code.
902 */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +0100903static void timekeeping_resume(void)
john stultz85240702007-05-08 00:27:59 -0700904{
John Stultz4e250fd2012-07-27 14:48:13 -0400905 struct timekeeper *tk = &timekeeper;
Feng Tange445cf12013-03-12 11:56:48 +0800906 struct clocksource *clock = tk->clock;
John Stultz92c1d3e2011-11-14 14:05:44 -0800907 unsigned long flags;
Feng Tange445cf12013-03-12 11:56:48 +0800908 struct timespec ts_new, ts_delta;
909 cycle_t cycle_now, cycle_delta;
910 bool suspendtime_found = false;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200911
Feng Tange445cf12013-03-12 11:56:48 +0800912 read_persistent_clock(&ts_new);
john stultz85240702007-05-08 00:27:59 -0700913
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200914 clockevents_resume();
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +0200915 clocksource_resume();
916
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000917 raw_spin_lock_irqsave(&timekeeper_lock, flags);
918 write_seqcount_begin(&timekeeper_seq);
john stultz85240702007-05-08 00:27:59 -0700919
Feng Tange445cf12013-03-12 11:56:48 +0800920 /*
921 * After system resumes, we need to calculate the suspended time and
922 * compensate it for the OS time. There are 3 sources that could be
923 * used: Nonstop clocksource during suspend, persistent clock and rtc
924 * device.
925 *
926 * One specific platform may have 1 or 2 or all of them, and the
927 * preference will be:
928 * suspend-nonstop clocksource -> persistent clock -> rtc
929 * The less preferred source will only be tried if there is no better
930 * usable source. The rtc part is handled separately in rtc core code.
931 */
932 cycle_now = clock->read(clock);
933 if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
934 cycle_now > clock->cycle_last) {
935 u64 num, max = ULLONG_MAX;
936 u32 mult = clock->mult;
937 u32 shift = clock->shift;
938 s64 nsec = 0;
939
940 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
941
942 /*
943 * "cycle_delta * mutl" may cause 64 bits overflow, if the
944 * suspended time is too long. In that case we need do the
945 * 64 bits math carefully
946 */
947 do_div(max, mult);
948 if (cycle_delta > max) {
949 num = div64_u64(cycle_delta, max);
950 nsec = (((u64) max * mult) >> shift) * num;
951 cycle_delta -= num * max;
952 }
953 nsec += ((u64) cycle_delta * mult) >> shift;
954
955 ts_delta = ns_to_timespec(nsec);
956 suspendtime_found = true;
957 } else if (timespec_compare(&ts_new, &timekeeping_suspend_time) > 0) {
958 ts_delta = timespec_sub(ts_new, timekeeping_suspend_time);
959 suspendtime_found = true;
john stultz85240702007-05-08 00:27:59 -0700960 }
Feng Tange445cf12013-03-12 11:56:48 +0800961
962 if (suspendtime_found)
963 __timekeeping_inject_sleeptime(tk, &ts_delta);
964
965 /* Re-base the last cycle value */
Thomas Gleixner77c675b2013-04-22 09:37:04 +0200966 tk->cycle_last = clock->cycle_last = cycle_now;
John Stultz4e250fd2012-07-27 14:48:13 -0400967 tk->ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -0700968 timekeeping_suspended = 0;
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000969 timekeeping_update(tk, false, true);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000970 write_seqcount_end(&timekeeper_seq);
971 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700972
973 touch_softlockup_watchdog();
974
975 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
976
977 /* Resume hrtimers */
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +0200978 hrtimers_resume();
john stultz85240702007-05-08 00:27:59 -0700979}
980
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +0100981static int timekeeping_suspend(void)
john stultz85240702007-05-08 00:27:59 -0700982{
John Stultz4e250fd2012-07-27 14:48:13 -0400983 struct timekeeper *tk = &timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800984 unsigned long flags;
John Stultzcb332172011-05-31 22:53:23 -0700985 struct timespec delta, delta_delta;
986 static struct timespec old_delta;
john stultz85240702007-05-08 00:27:59 -0700987
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200988 read_persistent_clock(&timekeeping_suspend_time);
Thomas Gleixner3be90952007-09-16 15:36:43 +0200989
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000990 raw_spin_lock_irqsave(&timekeeper_lock, flags);
991 write_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -0400992 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -0700993 timekeeping_suspended = 1;
John Stultzcb332172011-05-31 22:53:23 -0700994
995 /*
996 * To avoid drift caused by repeated suspend/resumes,
997 * which each can add ~1 second drift error,
998 * try to compensate so the difference in system time
999 * and persistent_clock time stays close to constant.
1000 */
John Stultz4e250fd2012-07-27 14:48:13 -04001001 delta = timespec_sub(tk_xtime(tk), timekeeping_suspend_time);
John Stultzcb332172011-05-31 22:53:23 -07001002 delta_delta = timespec_sub(delta, old_delta);
1003 if (abs(delta_delta.tv_sec) >= 2) {
1004 /*
1005 * if delta_delta is too large, assume time correction
1006 * has occured and set old_delta to the current delta.
1007 */
1008 old_delta = delta;
1009 } else {
1010 /* Otherwise try to adjust old_system to compensate */
1011 timekeeping_suspend_time =
1012 timespec_add(timekeeping_suspend_time, delta_delta);
1013 }
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001014 write_seqcount_end(&timekeeper_seq);
1015 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001016
1017 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
Magnus Dammc54a42b2010-02-02 14:41:41 -08001018 clocksource_suspend();
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +02001019 clockevents_suspend();
john stultz85240702007-05-08 00:27:59 -07001020
1021 return 0;
1022}
1023
1024/* sysfs resume/suspend bits for timekeeping */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001025static struct syscore_ops timekeeping_syscore_ops = {
john stultz85240702007-05-08 00:27:59 -07001026 .resume = timekeeping_resume,
1027 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -07001028};
1029
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001030static int __init timekeeping_init_ops(void)
john stultz85240702007-05-08 00:27:59 -07001031{
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001032 register_syscore_ops(&timekeeping_syscore_ops);
1033 return 0;
john stultz85240702007-05-08 00:27:59 -07001034}
1035
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001036device_initcall(timekeeping_init_ops);
john stultz85240702007-05-08 00:27:59 -07001037
1038/*
1039 * If the error is already larger, we look ahead even further
1040 * to compensate for late or lost adjustments.
1041 */
John Stultzf726a692012-07-13 01:21:57 -04001042static __always_inline int timekeeping_bigadjust(struct timekeeper *tk,
1043 s64 error, s64 *interval,
john stultz85240702007-05-08 00:27:59 -07001044 s64 *offset)
1045{
1046 s64 tick_error, i;
1047 u32 look_ahead, adj;
1048 s32 error2, mult;
1049
1050 /*
1051 * Use the current error value to determine how much to look ahead.
1052 * The larger the error the slower we adjust for it to avoid problems
1053 * with losing too many ticks, otherwise we would overadjust and
1054 * produce an even larger error. The smaller the adjustment the
1055 * faster we try to adjust for it, as lost ticks can do less harm
Li Zefan3eb05672008-02-08 04:19:25 -08001056 * here. This is tuned so that an error of about 1 msec is adjusted
john stultz85240702007-05-08 00:27:59 -07001057 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
1058 */
John Stultzf726a692012-07-13 01:21:57 -04001059 error2 = tk->ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
john stultz85240702007-05-08 00:27:59 -07001060 error2 = abs(error2);
1061 for (look_ahead = 0; error2 > 0; look_ahead++)
1062 error2 >>= 2;
1063
1064 /*
1065 * Now calculate the error in (1 << look_ahead) ticks, but first
1066 * remove the single look ahead already included in the error.
1067 */
John Stultzf726a692012-07-13 01:21:57 -04001068 tick_error = ntp_tick_length() >> (tk->ntp_error_shift + 1);
1069 tick_error -= tk->xtime_interval >> 1;
john stultz85240702007-05-08 00:27:59 -07001070 error = ((error - tick_error) >> look_ahead) + tick_error;
1071
1072 /* Finally calculate the adjustment shift value. */
1073 i = *interval;
1074 mult = 1;
1075 if (error < 0) {
1076 error = -error;
1077 *interval = -*interval;
1078 *offset = -*offset;
1079 mult = -1;
1080 }
1081 for (adj = 0; error > i; adj++)
1082 error >>= 1;
1083
1084 *interval <<= adj;
1085 *offset <<= adj;
1086 return mult << adj;
1087}
1088
1089/*
1090 * Adjust the multiplier to reduce the error value,
1091 * this is optimized for the most common adjustments of -1,0,1,
1092 * for other values we can do a bit more work.
1093 */
John Stultzf726a692012-07-13 01:21:57 -04001094static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
john stultz85240702007-05-08 00:27:59 -07001095{
John Stultzf726a692012-07-13 01:21:57 -04001096 s64 error, interval = tk->cycle_interval;
john stultz85240702007-05-08 00:27:59 -07001097 int adj;
1098
John Stultzc2bc1112011-10-27 18:12:42 -07001099 /*
Jim Cromie88b28ad2012-03-14 21:28:56 -06001100 * The point of this is to check if the error is greater than half
John Stultzc2bc1112011-10-27 18:12:42 -07001101 * an interval.
1102 *
1103 * First we shift it down from NTP_SHIFT to clocksource->shifted nsecs.
1104 *
1105 * Note we subtract one in the shift, so that error is really error*2.
John Stultz3f86f282011-10-27 17:41:17 -07001106 * This "saves" dividing(shifting) interval twice, but keeps the
1107 * (error > interval) comparison as still measuring if error is
Jim Cromie88b28ad2012-03-14 21:28:56 -06001108 * larger than half an interval.
John Stultzc2bc1112011-10-27 18:12:42 -07001109 *
John Stultz3f86f282011-10-27 17:41:17 -07001110 * Note: It does not "save" on aggravation when reading the code.
John Stultzc2bc1112011-10-27 18:12:42 -07001111 */
John Stultzf726a692012-07-13 01:21:57 -04001112 error = tk->ntp_error >> (tk->ntp_error_shift - 1);
john stultz85240702007-05-08 00:27:59 -07001113 if (error > interval) {
John Stultzc2bc1112011-10-27 18:12:42 -07001114 /*
1115 * We now divide error by 4(via shift), which checks if
Jim Cromie88b28ad2012-03-14 21:28:56 -06001116 * the error is greater than twice the interval.
John Stultzc2bc1112011-10-27 18:12:42 -07001117 * If it is greater, we need a bigadjust, if its smaller,
1118 * we can adjust by 1.
1119 */
john stultz85240702007-05-08 00:27:59 -07001120 error >>= 2;
John Stultzc2bc1112011-10-27 18:12:42 -07001121 /*
1122 * XXX - In update_wall_time, we round up to the next
1123 * nanosecond, and store the amount rounded up into
1124 * the error. This causes the likely below to be unlikely.
1125 *
John Stultz3f86f282011-10-27 17:41:17 -07001126 * The proper fix is to avoid rounding up by using
John Stultz4e250fd2012-07-27 14:48:13 -04001127 * the high precision tk->xtime_nsec instead of
John Stultzc2bc1112011-10-27 18:12:42 -07001128 * xtime.tv_nsec everywhere. Fixing this will take some
1129 * time.
1130 */
john stultz85240702007-05-08 00:27:59 -07001131 if (likely(error <= interval))
1132 adj = 1;
1133 else
Ingo Molnar1d17d172012-08-04 21:21:14 +02001134 adj = timekeeping_bigadjust(tk, error, &interval, &offset);
1135 } else {
1136 if (error < -interval) {
1137 /* See comment above, this is just switched for the negative */
1138 error >>= 2;
1139 if (likely(error >= -interval)) {
1140 adj = -1;
1141 interval = -interval;
1142 offset = -offset;
1143 } else {
1144 adj = timekeeping_bigadjust(tk, error, &interval, &offset);
1145 }
1146 } else {
1147 goto out_adjust;
1148 }
1149 }
john stultz85240702007-05-08 00:27:59 -07001150
John Stultzf726a692012-07-13 01:21:57 -04001151 if (unlikely(tk->clock->maxadj &&
1152 (tk->mult + adj > tk->clock->mult + tk->clock->maxadj))) {
John Stultze919cfd2012-03-22 19:14:46 -07001153 printk_once(KERN_WARNING
1154 "Adjusting %s more than 11%% (%ld vs %ld)\n",
John Stultzf726a692012-07-13 01:21:57 -04001155 tk->clock->name, (long)tk->mult + adj,
1156 (long)tk->clock->mult + tk->clock->maxadj);
John Stultze919cfd2012-03-22 19:14:46 -07001157 }
John Stultzc2bc1112011-10-27 18:12:42 -07001158 /*
1159 * So the following can be confusing.
1160 *
1161 * To keep things simple, lets assume adj == 1 for now.
1162 *
1163 * When adj != 1, remember that the interval and offset values
1164 * have been appropriately scaled so the math is the same.
1165 *
1166 * The basic idea here is that we're increasing the multiplier
1167 * by one, this causes the xtime_interval to be incremented by
1168 * one cycle_interval. This is because:
1169 * xtime_interval = cycle_interval * mult
1170 * So if mult is being incremented by one:
1171 * xtime_interval = cycle_interval * (mult + 1)
1172 * Its the same as:
1173 * xtime_interval = (cycle_interval * mult) + cycle_interval
1174 * Which can be shortened to:
1175 * xtime_interval += cycle_interval
1176 *
1177 * So offset stores the non-accumulated cycles. Thus the current
1178 * time (in shifted nanoseconds) is:
1179 * now = (offset * adj) + xtime_nsec
1180 * Now, even though we're adjusting the clock frequency, we have
1181 * to keep time consistent. In other words, we can't jump back
1182 * in time, and we also want to avoid jumping forward in time.
1183 *
1184 * So given the same offset value, we need the time to be the same
1185 * both before and after the freq adjustment.
1186 * now = (offset * adj_1) + xtime_nsec_1
1187 * now = (offset * adj_2) + xtime_nsec_2
1188 * So:
1189 * (offset * adj_1) + xtime_nsec_1 =
1190 * (offset * adj_2) + xtime_nsec_2
1191 * And we know:
1192 * adj_2 = adj_1 + 1
1193 * So:
1194 * (offset * adj_1) + xtime_nsec_1 =
1195 * (offset * (adj_1+1)) + xtime_nsec_2
1196 * (offset * adj_1) + xtime_nsec_1 =
1197 * (offset * adj_1) + offset + xtime_nsec_2
1198 * Canceling the sides:
1199 * xtime_nsec_1 = offset + xtime_nsec_2
1200 * Which gives us:
1201 * xtime_nsec_2 = xtime_nsec_1 - offset
1202 * Which simplfies to:
1203 * xtime_nsec -= offset
1204 *
1205 * XXX - TODO: Doc ntp_error calculation.
1206 */
John Stultzf726a692012-07-13 01:21:57 -04001207 tk->mult += adj;
1208 tk->xtime_interval += interval;
1209 tk->xtime_nsec -= offset;
1210 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -04001211
Ingo Molnar1d17d172012-08-04 21:21:14 +02001212out_adjust:
John Stultz2a8c0882012-07-13 01:21:56 -04001213 /*
1214 * It may be possible that when we entered this function, xtime_nsec
1215 * was very small. Further, if we're slightly speeding the clocksource
1216 * in the code above, its possible the required corrective factor to
1217 * xtime_nsec could cause it to underflow.
1218 *
1219 * Now, since we already accumulated the second, cannot simply roll
1220 * the accumulated second back, since the NTP subsystem has been
1221 * notified via second_overflow. So instead we push xtime_nsec forward
1222 * by the amount we underflowed, and add that amount into the error.
1223 *
1224 * We'll correct this error next time through this function, when
1225 * xtime_nsec is not as small.
1226 */
John Stultzf726a692012-07-13 01:21:57 -04001227 if (unlikely((s64)tk->xtime_nsec < 0)) {
1228 s64 neg = -(s64)tk->xtime_nsec;
1229 tk->xtime_nsec = 0;
1230 tk->ntp_error += neg << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -04001231 }
1232
john stultz85240702007-05-08 00:27:59 -07001233}
1234
1235/**
John Stultz1f4f9482012-07-13 01:21:54 -04001236 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
1237 *
1238 * Helper function that accumulates a the nsecs greater then a second
1239 * from the xtime_nsec field to the xtime_secs field.
1240 * It also calls into the NTP code to handle leapsecond processing.
1241 *
1242 */
1243static inline void accumulate_nsecs_to_secs(struct timekeeper *tk)
1244{
1245 u64 nsecps = (u64)NSEC_PER_SEC << tk->shift;
1246
1247 while (tk->xtime_nsec >= nsecps) {
1248 int leap;
1249
1250 tk->xtime_nsec -= nsecps;
1251 tk->xtime_sec++;
1252
1253 /* Figure out if its a leap sec and apply if needed */
1254 leap = second_overflow(tk->xtime_sec);
John Stultz6d0ef902012-07-27 14:48:12 -04001255 if (unlikely(leap)) {
1256 struct timespec ts;
John Stultz1f4f9482012-07-13 01:21:54 -04001257
John Stultz6d0ef902012-07-27 14:48:12 -04001258 tk->xtime_sec += leap;
1259
1260 ts.tv_sec = leap;
1261 ts.tv_nsec = 0;
1262 tk_set_wall_to_mono(tk,
1263 timespec_sub(tk->wall_to_monotonic, ts));
1264
John Stultzcc244dd2012-05-03 12:30:07 -07001265 __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
1266
John Stultz6d0ef902012-07-27 14:48:12 -04001267 clock_was_set_delayed();
1268 }
John Stultz1f4f9482012-07-13 01:21:54 -04001269 }
1270}
1271
John Stultz1f4f9482012-07-13 01:21:54 -04001272/**
john stultza092ff02009-10-02 16:17:53 -07001273 * logarithmic_accumulation - shifted accumulation of cycles
1274 *
1275 * This functions accumulates a shifted interval of cycles into
1276 * into a shifted interval nanoseconds. Allows for O(log) accumulation
1277 * loop.
1278 *
1279 * Returns the unconsumed cycles.
1280 */
John Stultzf726a692012-07-13 01:21:57 -04001281static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
1282 u32 shift)
john stultza092ff02009-10-02 16:17:53 -07001283{
Thomas Gleixner23a95372013-02-21 22:51:36 +00001284 cycle_t interval = tk->cycle_interval << shift;
Jason Wesseldeda2e82010-08-09 14:20:09 -07001285 u64 raw_nsecs;
john stultza092ff02009-10-02 16:17:53 -07001286
John Stultzf726a692012-07-13 01:21:57 -04001287 /* If the offset is smaller then a shifted interval, do nothing */
Thomas Gleixner23a95372013-02-21 22:51:36 +00001288 if (offset < interval)
john stultza092ff02009-10-02 16:17:53 -07001289 return offset;
1290
1291 /* Accumulate one shifted interval */
Thomas Gleixner23a95372013-02-21 22:51:36 +00001292 offset -= interval;
Thomas Gleixner7ec98e12013-02-21 22:51:39 +00001293 tk->cycle_last += interval;
john stultza092ff02009-10-02 16:17:53 -07001294
John Stultzf726a692012-07-13 01:21:57 -04001295 tk->xtime_nsec += tk->xtime_interval << shift;
1296 accumulate_nsecs_to_secs(tk);
john stultza092ff02009-10-02 16:17:53 -07001297
Jason Wesseldeda2e82010-08-09 14:20:09 -07001298 /* Accumulate raw time */
Dan Carpenter5b3900c2012-10-09 10:18:23 +03001299 raw_nsecs = (u64)tk->raw_interval << shift;
John Stultzf726a692012-07-13 01:21:57 -04001300 raw_nsecs += tk->raw_time.tv_nsec;
John Stultzc7dcf872010-08-13 11:30:58 -07001301 if (raw_nsecs >= NSEC_PER_SEC) {
1302 u64 raw_secs = raw_nsecs;
1303 raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
John Stultzf726a692012-07-13 01:21:57 -04001304 tk->raw_time.tv_sec += raw_secs;
john stultza092ff02009-10-02 16:17:53 -07001305 }
John Stultzf726a692012-07-13 01:21:57 -04001306 tk->raw_time.tv_nsec = raw_nsecs;
john stultza092ff02009-10-02 16:17:53 -07001307
1308 /* Accumulate error between NTP and clock interval */
John Stultzf726a692012-07-13 01:21:57 -04001309 tk->ntp_error += ntp_tick_length() << shift;
1310 tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
1311 (tk->ntp_error_shift + shift);
john stultza092ff02009-10-02 16:17:53 -07001312
1313 return offset;
1314}
1315
John Stultz92bb1fc2012-09-04 15:38:12 -04001316#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
1317static inline void old_vsyscall_fixup(struct timekeeper *tk)
1318{
1319 s64 remainder;
1320
1321 /*
1322 * Store only full nanoseconds into xtime_nsec after rounding
1323 * it up and add the remainder to the error difference.
1324 * XXX - This is necessary to avoid small 1ns inconsistnecies caused
1325 * by truncating the remainder in vsyscalls. However, it causes
1326 * additional work to be done in timekeeping_adjust(). Once
1327 * the vsyscall implementations are converted to use xtime_nsec
1328 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
1329 * users are removed, this can be killed.
1330 */
1331 remainder = tk->xtime_nsec & ((1ULL << tk->shift) - 1);
1332 tk->xtime_nsec -= remainder;
1333 tk->xtime_nsec += 1ULL << tk->shift;
1334 tk->ntp_error += remainder << tk->ntp_error_shift;
1335
1336}
1337#else
1338#define old_vsyscall_fixup(tk)
1339#endif
1340
1341
1342
john stultz85240702007-05-08 00:27:59 -07001343/**
1344 * update_wall_time - Uses the current clocksource to increment the wall time
1345 *
john stultz85240702007-05-08 00:27:59 -07001346 */
Torben Hohn871cf1e2011-01-27 15:58:55 +01001347static void update_wall_time(void)
john stultz85240702007-05-08 00:27:59 -07001348{
Martin Schwidefsky155ec602009-08-14 15:47:26 +02001349 struct clocksource *clock;
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001350 struct timekeeper *real_tk = &timekeeper;
1351 struct timekeeper *tk = &shadow_timekeeper;
john stultz85240702007-05-08 00:27:59 -07001352 cycle_t offset;
john stultza092ff02009-10-02 16:17:53 -07001353 int shift = 0, maxshift;
John Stultz70471f22011-11-14 12:48:10 -08001354 unsigned long flags;
1355
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001356 raw_spin_lock_irqsave(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001357
1358 /* Make sure we're fully resumed: */
1359 if (unlikely(timekeeping_suspended))
John Stultz70471f22011-11-14 12:48:10 -08001360 goto out;
john stultz85240702007-05-08 00:27:59 -07001361
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001362 clock = real_tk->clock;
John Stultz592913e2010-07-13 17:56:20 -07001363
1364#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001365 offset = real_tk->cycle_interval;
John Stultz592913e2010-07-13 17:56:20 -07001366#else
1367 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
john stultz85240702007-05-08 00:27:59 -07001368#endif
john stultz85240702007-05-08 00:27:59 -07001369
John Stultzbf2ac312012-08-21 20:30:49 -04001370 /* Check if there's really nothing to do */
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001371 if (offset < real_tk->cycle_interval)
John Stultzbf2ac312012-08-21 20:30:49 -04001372 goto out;
1373
john stultza092ff02009-10-02 16:17:53 -07001374 /*
1375 * With NO_HZ we may have to accumulate many cycle_intervals
1376 * (think "ticks") worth of time at once. To do this efficiently,
1377 * we calculate the largest doubling multiple of cycle_intervals
Jim Cromie88b28ad2012-03-14 21:28:56 -06001378 * that is smaller than the offset. We then accumulate that
john stultza092ff02009-10-02 16:17:53 -07001379 * chunk in one go, and then try to consume the next smaller
1380 * doubled multiple.
john stultz85240702007-05-08 00:27:59 -07001381 */
John Stultz4e250fd2012-07-27 14:48:13 -04001382 shift = ilog2(offset) - ilog2(tk->cycle_interval);
john stultza092ff02009-10-02 16:17:53 -07001383 shift = max(0, shift);
Jim Cromie88b28ad2012-03-14 21:28:56 -06001384 /* Bound shift to one less than what overflows tick_length */
John Stultzea7cf49a2011-11-14 13:18:07 -08001385 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
john stultza092ff02009-10-02 16:17:53 -07001386 shift = min(shift, maxshift);
John Stultz4e250fd2012-07-27 14:48:13 -04001387 while (offset >= tk->cycle_interval) {
1388 offset = logarithmic_accumulation(tk, offset, shift);
1389 if (offset < tk->cycle_interval<<shift)
John Stultz830ec042010-03-18 14:47:30 -07001390 shift--;
john stultz85240702007-05-08 00:27:59 -07001391 }
1392
1393 /* correct the clock when NTP error is too big */
John Stultz4e250fd2012-07-27 14:48:13 -04001394 timekeeping_adjust(tk, offset);
john stultz85240702007-05-08 00:27:59 -07001395
John Stultz6a867a32010-04-06 14:30:51 -07001396 /*
John Stultz92bb1fc2012-09-04 15:38:12 -04001397 * XXX This can be killed once everyone converts
1398 * to the new update_vsyscall.
1399 */
1400 old_vsyscall_fixup(tk);
john stultz85240702007-05-08 00:27:59 -07001401
John Stultz6a867a32010-04-06 14:30:51 -07001402 /*
1403 * Finally, make sure that after the rounding
John Stultz1e75fa82012-07-13 01:21:53 -04001404 * xtime_nsec isn't larger than NSEC_PER_SEC
John Stultz6a867a32010-04-06 14:30:51 -07001405 */
John Stultz4e250fd2012-07-27 14:48:13 -04001406 accumulate_nsecs_to_secs(tk);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001407
Thomas Gleixnerca4523c2013-02-21 22:51:40 +00001408 write_seqcount_begin(&timekeeper_seq);
Thomas Gleixner7ec98e12013-02-21 22:51:39 +00001409 /* Update clock->cycle_last with the new value */
1410 clock->cycle_last = tk->cycle_last;
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001411 /*
1412 * Update the real timekeeper.
1413 *
1414 * We could avoid this memcpy by switching pointers, but that
1415 * requires changes to all other timekeeper usage sites as
1416 * well, i.e. move the timekeeper pointer getter into the
1417 * spinlocked/seqcount protected sections. And we trade this
1418 * memcpy under the timekeeper_seq against one before we start
1419 * updating.
1420 */
1421 memcpy(real_tk, tk, sizeof(*tk));
1422 timekeeping_update(real_tk, false, false);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001423 write_seqcount_end(&timekeeper_seq);
Thomas Gleixnerca4523c2013-02-21 22:51:40 +00001424out:
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001425 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001426}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001427
1428/**
1429 * getboottime - Return the real time of system boot.
1430 * @ts: pointer to the timespec to be set
1431 *
John Stultzabb3a4e2011-02-14 17:52:09 -08001432 * Returns the wall-time of boot in a timespec.
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001433 *
1434 * This is based on the wall_to_monotonic offset and the total suspend
1435 * time. Calls to settimeofday will affect the value returned (which
1436 * basically means that however wrong your real time clock is at boot time,
1437 * you get the right time here).
1438 */
1439void getboottime(struct timespec *ts)
1440{
John Stultz4e250fd2012-07-27 14:48:13 -04001441 struct timekeeper *tk = &timekeeper;
Hiroshi Shimamoto36d47482009-08-25 15:08:30 +09001442 struct timespec boottime = {
John Stultz4e250fd2012-07-27 14:48:13 -04001443 .tv_sec = tk->wall_to_monotonic.tv_sec +
1444 tk->total_sleep_time.tv_sec,
1445 .tv_nsec = tk->wall_to_monotonic.tv_nsec +
1446 tk->total_sleep_time.tv_nsec
Hiroshi Shimamoto36d47482009-08-25 15:08:30 +09001447 };
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001448
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001449 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001450}
Jason Wangc93d89f2010-01-27 19:13:40 +08001451EXPORT_SYMBOL_GPL(getboottime);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001452
John Stultzabb3a4e2011-02-14 17:52:09 -08001453/**
1454 * get_monotonic_boottime - Returns monotonic time since boot
1455 * @ts: pointer to the timespec to be set
1456 *
1457 * Returns the monotonic time since boot in a timespec.
1458 *
1459 * This is similar to CLOCK_MONTONIC/ktime_get_ts, but also
1460 * includes the time spent in suspend.
1461 */
1462void get_monotonic_boottime(struct timespec *ts)
1463{
John Stultz4e250fd2012-07-27 14:48:13 -04001464 struct timekeeper *tk = &timekeeper;
John Stultzabb3a4e2011-02-14 17:52:09 -08001465 struct timespec tomono, sleep;
John Stultzec145ba2012-09-11 19:26:03 -04001466 s64 nsec;
John Stultzabb3a4e2011-02-14 17:52:09 -08001467 unsigned int seq;
John Stultzabb3a4e2011-02-14 17:52:09 -08001468
1469 WARN_ON(timekeeping_suspended);
1470
1471 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001472 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001473 ts->tv_sec = tk->xtime_sec;
John Stultzec145ba2012-09-11 19:26:03 -04001474 nsec = timekeeping_get_ns(tk);
John Stultz4e250fd2012-07-27 14:48:13 -04001475 tomono = tk->wall_to_monotonic;
1476 sleep = tk->total_sleep_time;
John Stultzabb3a4e2011-02-14 17:52:09 -08001477
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001478 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultzabb3a4e2011-02-14 17:52:09 -08001479
John Stultzec145ba2012-09-11 19:26:03 -04001480 ts->tv_sec += tomono.tv_sec + sleep.tv_sec;
1481 ts->tv_nsec = 0;
1482 timespec_add_ns(ts, nsec + tomono.tv_nsec + sleep.tv_nsec);
John Stultzabb3a4e2011-02-14 17:52:09 -08001483}
1484EXPORT_SYMBOL_GPL(get_monotonic_boottime);
1485
1486/**
1487 * ktime_get_boottime - Returns monotonic time since boot in a ktime
1488 *
1489 * Returns the monotonic time since boot in a ktime
1490 *
1491 * This is similar to CLOCK_MONTONIC/ktime_get, but also
1492 * includes the time spent in suspend.
1493 */
1494ktime_t ktime_get_boottime(void)
1495{
1496 struct timespec ts;
1497
1498 get_monotonic_boottime(&ts);
1499 return timespec_to_ktime(ts);
1500}
1501EXPORT_SYMBOL_GPL(ktime_get_boottime);
1502
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001503/**
1504 * monotonic_to_bootbased - Convert the monotonic time to boot based.
1505 * @ts: pointer to the timespec to be converted
1506 */
1507void monotonic_to_bootbased(struct timespec *ts)
1508{
John Stultz4e250fd2012-07-27 14:48:13 -04001509 struct timekeeper *tk = &timekeeper;
1510
1511 *ts = timespec_add(*ts, tk->total_sleep_time);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001512}
Jason Wangc93d89f2010-01-27 19:13:40 +08001513EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
john stultz2c6b47d2007-07-24 17:47:43 -07001514
john stultz17c38b72007-07-24 18:38:34 -07001515unsigned long get_seconds(void)
1516{
John Stultz4e250fd2012-07-27 14:48:13 -04001517 struct timekeeper *tk = &timekeeper;
1518
1519 return tk->xtime_sec;
john stultz17c38b72007-07-24 18:38:34 -07001520}
1521EXPORT_SYMBOL(get_seconds);
1522
john stultzda15cfd2009-08-19 19:13:34 -07001523struct timespec __current_kernel_time(void)
1524{
John Stultz4e250fd2012-07-27 14:48:13 -04001525 struct timekeeper *tk = &timekeeper;
1526
1527 return tk_xtime(tk);
john stultzda15cfd2009-08-19 19:13:34 -07001528}
john stultz17c38b72007-07-24 18:38:34 -07001529
john stultz2c6b47d2007-07-24 17:47:43 -07001530struct timespec current_kernel_time(void)
1531{
John Stultz4e250fd2012-07-27 14:48:13 -04001532 struct timekeeper *tk = &timekeeper;
john stultz2c6b47d2007-07-24 17:47:43 -07001533 struct timespec now;
1534 unsigned long seq;
1535
1536 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001537 seq = read_seqcount_begin(&timekeeper_seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001538
John Stultz4e250fd2012-07-27 14:48:13 -04001539 now = tk_xtime(tk);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001540 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultz2c6b47d2007-07-24 17:47:43 -07001541
1542 return now;
1543}
john stultz2c6b47d2007-07-24 17:47:43 -07001544EXPORT_SYMBOL(current_kernel_time);
john stultzda15cfd2009-08-19 19:13:34 -07001545
1546struct timespec get_monotonic_coarse(void)
1547{
John Stultz4e250fd2012-07-27 14:48:13 -04001548 struct timekeeper *tk = &timekeeper;
john stultzda15cfd2009-08-19 19:13:34 -07001549 struct timespec now, mono;
1550 unsigned long seq;
1551
1552 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001553 seq = read_seqcount_begin(&timekeeper_seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001554
John Stultz4e250fd2012-07-27 14:48:13 -04001555 now = tk_xtime(tk);
1556 mono = tk->wall_to_monotonic;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001557 } while (read_seqcount_retry(&timekeeper_seq, seq));
john stultzda15cfd2009-08-19 19:13:34 -07001558
1559 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
1560 now.tv_nsec + mono.tv_nsec);
1561 return now;
1562}
Torben Hohn871cf1e2011-01-27 15:58:55 +01001563
1564/*
John Stultzd6ad4182012-02-28 16:50:11 -08001565 * Must hold jiffies_lock
Torben Hohn871cf1e2011-01-27 15:58:55 +01001566 */
1567void do_timer(unsigned long ticks)
1568{
1569 jiffies_64 += ticks;
1570 update_wall_time();
1571 calc_global_load(ticks);
1572}
Torben Hohn48cf76f72011-01-27 15:59:05 +01001573
1574/**
John Stultz314ac372011-02-14 18:43:08 -08001575 * get_xtime_and_monotonic_and_sleep_offset() - get xtime, wall_to_monotonic,
1576 * and sleep offsets.
Torben Hohn48cf76f72011-01-27 15:59:05 +01001577 * @xtim: pointer to timespec to be set with xtime
1578 * @wtom: pointer to timespec to be set with wall_to_monotonic
John Stultz314ac372011-02-14 18:43:08 -08001579 * @sleep: pointer to timespec to be set with time in suspend
Torben Hohn48cf76f72011-01-27 15:59:05 +01001580 */
John Stultz314ac372011-02-14 18:43:08 -08001581void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
1582 struct timespec *wtom, struct timespec *sleep)
Torben Hohn48cf76f72011-01-27 15:59:05 +01001583{
John Stultz4e250fd2012-07-27 14:48:13 -04001584 struct timekeeper *tk = &timekeeper;
Torben Hohn48cf76f72011-01-27 15:59:05 +01001585 unsigned long seq;
1586
1587 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001588 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001589 *xtim = tk_xtime(tk);
1590 *wtom = tk->wall_to_monotonic;
1591 *sleep = tk->total_sleep_time;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001592 } while (read_seqcount_retry(&timekeeper_seq, seq));
Torben Hohn48cf76f72011-01-27 15:59:05 +01001593}
Torben Hohnf0af911a92011-01-27 15:59:10 +01001594
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001595#ifdef CONFIG_HIGH_RES_TIMERS
1596/**
1597 * ktime_get_update_offsets - hrtimer helper
1598 * @offs_real: pointer to storage for monotonic -> realtime offset
1599 * @offs_boot: pointer to storage for monotonic -> boottime offset
1600 *
1601 * Returns current monotonic time and updates the offsets
1602 * Called from hrtimer_interupt() or retrigger_next_event()
1603 */
John Stultz90adda92013-01-21 17:00:11 -08001604ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot,
1605 ktime_t *offs_tai)
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001606{
John Stultz4e250fd2012-07-27 14:48:13 -04001607 struct timekeeper *tk = &timekeeper;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001608 ktime_t now;
1609 unsigned int seq;
1610 u64 secs, nsecs;
1611
1612 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001613 seq = read_seqcount_begin(&timekeeper_seq);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001614
John Stultz4e250fd2012-07-27 14:48:13 -04001615 secs = tk->xtime_sec;
1616 nsecs = timekeeping_get_ns(tk);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001617
John Stultz4e250fd2012-07-27 14:48:13 -04001618 *offs_real = tk->offs_real;
1619 *offs_boot = tk->offs_boot;
John Stultz90adda92013-01-21 17:00:11 -08001620 *offs_tai = tk->offs_tai;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001621 } while (read_seqcount_retry(&timekeeper_seq, seq));
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001622
1623 now = ktime_add_ns(ktime_set(secs, 0), nsecs);
1624 now = ktime_sub(now, *offs_real);
1625 return now;
1626}
1627#endif
1628
Torben Hohnf0af911a92011-01-27 15:59:10 +01001629/**
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001630 * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
1631 */
1632ktime_t ktime_get_monotonic_offset(void)
1633{
John Stultz4e250fd2012-07-27 14:48:13 -04001634 struct timekeeper *tk = &timekeeper;
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001635 unsigned long seq;
1636 struct timespec wtom;
1637
1638 do {
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001639 seq = read_seqcount_begin(&timekeeper_seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001640 wtom = tk->wall_to_monotonic;
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001641 } while (read_seqcount_retry(&timekeeper_seq, seq));
John Stultz70471f22011-11-14 12:48:10 -08001642
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001643 return timespec_to_ktime(wtom);
1644}
John Stultza80b83b2012-02-03 00:19:07 -08001645EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset);
1646
Thomas Gleixner99ee5312011-04-27 14:16:42 +02001647/**
John Stultzaa6f9c592013-03-22 11:31:29 -07001648 * do_adjtimex() - Accessor function to NTP __do_adjtimex function
1649 */
1650int do_adjtimex(struct timex *txc)
1651{
John Stultz0b5154f2013-03-22 14:20:03 -07001652 struct timekeeper *tk = &timekeeper;
John Stultz06c017f2013-03-22 11:37:28 -07001653 unsigned long flags;
John Stultz87ace392013-03-22 12:28:15 -07001654 struct timespec ts;
John Stultz4e8f8b32013-04-10 12:41:49 -07001655 s32 orig_tai, tai;
John Stultze4085692013-03-22 12:08:52 -07001656 int ret;
1657
1658 /* Validate the data before disabling interrupts */
1659 ret = ntp_validate_timex(txc);
1660 if (ret)
1661 return ret;
1662
John Stultzcef90372013-03-22 15:04:13 -07001663 if (txc->modes & ADJ_SETOFFSET) {
1664 struct timespec delta;
1665 delta.tv_sec = txc->time.tv_sec;
1666 delta.tv_nsec = txc->time.tv_usec;
1667 if (!(txc->modes & ADJ_NANO))
1668 delta.tv_nsec *= 1000;
1669 ret = timekeeping_inject_offset(&delta);
1670 if (ret)
1671 return ret;
1672 }
1673
John Stultz87ace392013-03-22 12:28:15 -07001674 getnstimeofday(&ts);
John Stultzaa6f9c592013-03-22 11:31:29 -07001675
John Stultz06c017f2013-03-22 11:37:28 -07001676 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1677 write_seqcount_begin(&timekeeper_seq);
1678
John Stultz4e8f8b32013-04-10 12:41:49 -07001679 orig_tai = tai = tk->tai_offset;
John Stultz87ace392013-03-22 12:28:15 -07001680 ret = __do_adjtimex(txc, &ts, &tai);
1681
John Stultz4e8f8b32013-04-10 12:41:49 -07001682 if (tai != orig_tai) {
1683 __timekeeping_set_tai_offset(tk, tai);
1684 clock_was_set_delayed();
1685 }
John Stultz06c017f2013-03-22 11:37:28 -07001686 write_seqcount_end(&timekeeper_seq);
1687 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1688
John Stultz87ace392013-03-22 12:28:15 -07001689 return ret;
1690}
John Stultzaa6f9c592013-03-22 11:31:29 -07001691
1692#ifdef CONFIG_NTP_PPS
1693/**
1694 * hardpps() - Accessor function to NTP __hardpps function
1695 */
1696void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
1697{
John Stultz06c017f2013-03-22 11:37:28 -07001698 unsigned long flags;
1699
1700 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1701 write_seqcount_begin(&timekeeper_seq);
1702
John Stultzaa6f9c592013-03-22 11:31:29 -07001703 __hardpps(phase_ts, raw_ts);
John Stultz06c017f2013-03-22 11:37:28 -07001704
1705 write_seqcount_end(&timekeeper_seq);
1706 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzaa6f9c592013-03-22 11:31:29 -07001707}
1708EXPORT_SYMBOL(hardpps);
1709#endif
1710
1711/**
Torben Hohnf0af911a92011-01-27 15:59:10 +01001712 * xtime_update() - advances the timekeeping infrastructure
1713 * @ticks: number of ticks, that have elapsed since the last call.
1714 *
1715 * Must be called with interrupts disabled.
1716 */
1717void xtime_update(unsigned long ticks)
1718{
John Stultzd6ad4182012-02-28 16:50:11 -08001719 write_seqlock(&jiffies_lock);
Torben Hohnf0af911a92011-01-27 15:59:10 +01001720 do_timer(ticks);
John Stultzd6ad4182012-02-28 16:50:11 -08001721 write_sequnlock(&jiffies_lock);
Torben Hohnf0af911a92011-01-27 15:59:10 +01001722}