blob: 79b9bc6e787634a06cff4413ce334a9d64f77c77 [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>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070025#include <linux/compiler.h>
john stultz85240702007-05-08 00:27:59 -070026
Thomas Gleixnereb93e4d2013-02-21 22:51:36 +000027#include "tick-internal.h"
John Stultzaa6f9c592013-03-22 11:31:29 -070028#include "ntp_internal.h"
Colin Cross5c835452013-05-21 22:32:14 -070029#include "timekeeping_internal.h"
Martin Schwidefsky155ec602009-08-14 15:47:26 +020030
David Vrabel04397fe92013-06-27 11:35:45 +010031#define TK_CLEAR_NTP (1 << 0)
32#define TK_MIRROR (1 << 1)
David Vrabel780427f2013-06-27 11:35:46 +010033#define TK_CLOCK_WAS_SET (1 << 2)
David Vrabel04397fe92013-06-27 11:35:45 +010034
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +000035/*
36 * The most important data for readout fits into a single 64 byte
37 * cache line.
38 */
39static struct {
40 seqcount_t seq;
41 struct timekeeper timekeeper;
42} tk_core ____cacheline_aligned;
43
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +000044static DEFINE_RAW_SPINLOCK(timekeeper_lock);
Thomas Gleixner48cdc132013-02-21 22:51:40 +000045static struct timekeeper shadow_timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020046
Thomas Gleixner4396e052014-07-16 21:05:23 +000047/**
48 * struct tk_fast - NMI safe timekeeper
49 * @seq: Sequence counter for protecting updates. The lowest bit
50 * is the index for the tk_read_base array
51 * @base: tk_read_base array. Access is indexed by the lowest bit of
52 * @seq.
53 *
54 * See @update_fast_timekeeper() below.
55 */
56struct tk_fast {
57 seqcount_t seq;
58 struct tk_read_base base[2];
59};
60
61static struct tk_fast tk_fast_mono ____cacheline_aligned;
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +010062static struct tk_fast tk_fast_raw ____cacheline_aligned;
Thomas Gleixner4396e052014-07-16 21:05:23 +000063
John Stultz8fcce542011-11-14 11:46:39 -080064/* flag for if timekeeping is suspended */
65int __read_mostly timekeeping_suspended;
66
John Stultz1e75fa82012-07-13 01:21:53 -040067static inline void tk_normalize_xtime(struct timekeeper *tk)
68{
Peter Zijlstra876e7882015-03-19 10:09:06 +010069 while (tk->tkr_mono.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_mono.shift)) {
70 tk->tkr_mono.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
John Stultz1e75fa82012-07-13 01:21:53 -040071 tk->xtime_sec++;
72 }
73}
John Stultz8fcce542011-11-14 11:46:39 -080074
Thomas Gleixnerc905fae2014-07-16 21:04:05 +000075static inline struct timespec64 tk_xtime(struct timekeeper *tk)
76{
77 struct timespec64 ts;
78
79 ts.tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +010080 ts.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
Thomas Gleixnerc905fae2014-07-16 21:04:05 +000081 return ts;
82}
83
John Stultz7d489d12014-07-16 21:04:01 +000084static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
John Stultz1e75fa82012-07-13 01:21:53 -040085{
86 tk->xtime_sec = ts->tv_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +010087 tk->tkr_mono.xtime_nsec = (u64)ts->tv_nsec << tk->tkr_mono.shift;
John Stultz1e75fa82012-07-13 01:21:53 -040088}
89
John Stultz7d489d12014-07-16 21:04:01 +000090static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
John Stultz1e75fa82012-07-13 01:21:53 -040091{
92 tk->xtime_sec += ts->tv_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +010093 tk->tkr_mono.xtime_nsec += (u64)ts->tv_nsec << tk->tkr_mono.shift;
John Stultz784ffcb2012-08-21 20:30:46 -040094 tk_normalize_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -040095}
John Stultz8fcce542011-11-14 11:46:39 -080096
John Stultz7d489d12014-07-16 21:04:01 +000097static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
John Stultz6d0ef902012-07-27 14:48:12 -040098{
John Stultz7d489d12014-07-16 21:04:01 +000099 struct timespec64 tmp;
John Stultz6d0ef902012-07-27 14:48:12 -0400100
101 /*
102 * Verify consistency of: offset_real = -wall_to_monotonic
103 * before modifying anything
104 */
John Stultz7d489d12014-07-16 21:04:01 +0000105 set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec,
John Stultz6d0ef902012-07-27 14:48:12 -0400106 -tk->wall_to_monotonic.tv_nsec);
John Stultz7d489d12014-07-16 21:04:01 +0000107 WARN_ON_ONCE(tk->offs_real.tv64 != timespec64_to_ktime(tmp).tv64);
John Stultz6d0ef902012-07-27 14:48:12 -0400108 tk->wall_to_monotonic = wtm;
John Stultz7d489d12014-07-16 21:04:01 +0000109 set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
110 tk->offs_real = timespec64_to_ktime(tmp);
John Stultz04005f62013-12-10 17:13:35 -0800111 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
John Stultz6d0ef902012-07-27 14:48:12 -0400112}
113
Thomas Gleixner47da70d2014-07-16 21:05:00 +0000114static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
John Stultz6d0ef902012-07-27 14:48:12 -0400115{
Thomas Gleixner47da70d2014-07-16 21:05:00 +0000116 tk->offs_boot = ktime_add(tk->offs_boot, delta);
John Stultz6d0ef902012-07-27 14:48:12 -0400117}
118
John Stultz3c17ad12015-03-11 21:16:32 -0700119#ifdef CONFIG_DEBUG_TIMEKEEPING
John Stultz4ca22c22015-03-11 21:16:35 -0700120#define WARNING_FREQ (HZ*300) /* 5 minute rate-limiting */
121/*
122 * These simple flag variables are managed
123 * without locks, which is racy, but ok since
124 * we don't really care about being super
125 * precise about how many events were seen,
126 * just that a problem was observed.
127 */
128static int timekeeping_underflow_seen;
129static int timekeeping_overflow_seen;
130
131/* last_warning is only modified under the timekeeping lock */
132static long timekeeping_last_warning;
133
John Stultz3c17ad12015-03-11 21:16:32 -0700134static void timekeeping_check_update(struct timekeeper *tk, cycle_t offset)
135{
136
Peter Zijlstra876e7882015-03-19 10:09:06 +0100137 cycle_t max_cycles = tk->tkr_mono.clock->max_cycles;
138 const char *name = tk->tkr_mono.clock->name;
John Stultz3c17ad12015-03-11 21:16:32 -0700139
140 if (offset > max_cycles) {
John Stultza558cd02015-03-11 21:16:33 -0700141 printk_deferred("WARNING: timekeeping: Cycle offset (%lld) is larger than allowed by the '%s' clock's max_cycles value (%lld): time overflow danger\n",
John Stultz3c17ad12015-03-11 21:16:32 -0700142 offset, name, max_cycles);
John Stultza558cd02015-03-11 21:16:33 -0700143 printk_deferred(" timekeeping: Your kernel is sick, but tries to cope by capping time updates\n");
John Stultz3c17ad12015-03-11 21:16:32 -0700144 } else {
145 if (offset > (max_cycles >> 1)) {
146 printk_deferred("INFO: timekeeping: Cycle offset (%lld) is larger than the the '%s' clock's 50%% safety margin (%lld)\n",
147 offset, name, max_cycles >> 1);
148 printk_deferred(" timekeeping: Your kernel is still fine, but is feeling a bit nervous\n");
149 }
150 }
John Stultz4ca22c22015-03-11 21:16:35 -0700151
152 if (timekeeping_underflow_seen) {
153 if (jiffies - timekeeping_last_warning > WARNING_FREQ) {
154 printk_deferred("WARNING: Underflow in clocksource '%s' observed, time update ignored.\n", name);
155 printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
156 printk_deferred(" Your kernel is probably still fine.\n");
157 timekeeping_last_warning = jiffies;
158 }
159 timekeeping_underflow_seen = 0;
160 }
161
162 if (timekeeping_overflow_seen) {
163 if (jiffies - timekeeping_last_warning > WARNING_FREQ) {
164 printk_deferred("WARNING: Overflow in clocksource '%s' observed, time update capped.\n", name);
165 printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
166 printk_deferred(" Your kernel is probably still fine.\n");
167 timekeeping_last_warning = jiffies;
168 }
169 timekeeping_overflow_seen = 0;
170 }
John Stultz3c17ad12015-03-11 21:16:32 -0700171}
John Stultza558cd02015-03-11 21:16:33 -0700172
173static inline cycle_t timekeeping_get_delta(struct tk_read_base *tkr)
174{
John Stultz4ca22c22015-03-11 21:16:35 -0700175 cycle_t now, last, mask, max, delta;
176 unsigned int seq;
John Stultza558cd02015-03-11 21:16:33 -0700177
John Stultz4ca22c22015-03-11 21:16:35 -0700178 /*
179 * Since we're called holding a seqlock, the data may shift
180 * under us while we're doing the calculation. This can cause
181 * false positives, since we'd note a problem but throw the
182 * results away. So nest another seqlock here to atomically
183 * grab the points we are checking with.
184 */
185 do {
186 seq = read_seqcount_begin(&tk_core.seq);
187 now = tkr->read(tkr->clock);
188 last = tkr->cycle_last;
189 mask = tkr->mask;
190 max = tkr->clock->max_cycles;
191 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultza558cd02015-03-11 21:16:33 -0700192
John Stultz4ca22c22015-03-11 21:16:35 -0700193 delta = clocksource_delta(now, last, mask);
John Stultza558cd02015-03-11 21:16:33 -0700194
John Stultz057b87e2015-03-11 21:16:34 -0700195 /*
196 * Try to catch underflows by checking if we are seeing small
197 * mask-relative negative values.
198 */
John Stultz4ca22c22015-03-11 21:16:35 -0700199 if (unlikely((~delta & mask) < (mask >> 3))) {
200 timekeeping_underflow_seen = 1;
John Stultz057b87e2015-03-11 21:16:34 -0700201 delta = 0;
John Stultz4ca22c22015-03-11 21:16:35 -0700202 }
John Stultz057b87e2015-03-11 21:16:34 -0700203
John Stultza558cd02015-03-11 21:16:33 -0700204 /* Cap delta value to the max_cycles values to avoid mult overflows */
John Stultz4ca22c22015-03-11 21:16:35 -0700205 if (unlikely(delta > max)) {
206 timekeeping_overflow_seen = 1;
John Stultza558cd02015-03-11 21:16:33 -0700207 delta = tkr->clock->max_cycles;
John Stultz4ca22c22015-03-11 21:16:35 -0700208 }
John Stultza558cd02015-03-11 21:16:33 -0700209
210 return delta;
211}
John Stultz3c17ad12015-03-11 21:16:32 -0700212#else
213static inline void timekeeping_check_update(struct timekeeper *tk, cycle_t offset)
214{
215}
John Stultza558cd02015-03-11 21:16:33 -0700216static inline cycle_t timekeeping_get_delta(struct tk_read_base *tkr)
217{
218 cycle_t cycle_now, delta;
219
220 /* read clocksource */
221 cycle_now = tkr->read(tkr->clock);
222
223 /* calculate the delta since the last update_wall_time */
224 delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
225
226 return delta;
227}
John Stultz3c17ad12015-03-11 21:16:32 -0700228#endif
229
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200230/**
Yijing Wangd26e4fe2013-11-28 16:28:55 +0800231 * tk_setup_internals - Set up internals to use clocksource clock.
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200232 *
Yijing Wangd26e4fe2013-11-28 16:28:55 +0800233 * @tk: The target timekeeper to setup.
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200234 * @clock: Pointer to clocksource.
235 *
236 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
237 * pair and interval request.
238 *
239 * Unless you're the timekeeping code, you should not be using this!
240 */
John Stultzf726a692012-07-13 01:21:57 -0400241static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200242{
243 cycle_t interval;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700244 u64 tmp, ntpinterval;
John Stultz1e75fa82012-07-13 01:21:53 -0400245 struct clocksource *old_clock;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200246
Peter Zijlstra876e7882015-03-19 10:09:06 +0100247 old_clock = tk->tkr_mono.clock;
248 tk->tkr_mono.clock = clock;
249 tk->tkr_mono.read = clock->read;
250 tk->tkr_mono.mask = clock->mask;
251 tk->tkr_mono.cycle_last = tk->tkr_mono.read(clock);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200252
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100253 tk->tkr_raw.clock = clock;
254 tk->tkr_raw.read = clock->read;
255 tk->tkr_raw.mask = clock->mask;
256 tk->tkr_raw.cycle_last = tk->tkr_mono.cycle_last;
257
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200258 /* Do the ns -> cycle conversion first, using original mult */
259 tmp = NTP_INTERVAL_LENGTH;
260 tmp <<= clock->shift;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700261 ntpinterval = tmp;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200262 tmp += clock->mult/2;
263 do_div(tmp, clock->mult);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200264 if (tmp == 0)
265 tmp = 1;
266
267 interval = (cycle_t) tmp;
John Stultzf726a692012-07-13 01:21:57 -0400268 tk->cycle_interval = interval;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200269
270 /* Go back from cycles -> shifted ns */
John Stultzf726a692012-07-13 01:21:57 -0400271 tk->xtime_interval = (u64) interval * clock->mult;
272 tk->xtime_remainder = ntpinterval - tk->xtime_interval;
273 tk->raw_interval =
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200274 ((u64) interval * clock->mult) >> clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200275
John Stultz1e75fa82012-07-13 01:21:53 -0400276 /* if changing clocks, convert xtime_nsec shift units */
277 if (old_clock) {
278 int shift_change = clock->shift - old_clock->shift;
279 if (shift_change < 0)
Peter Zijlstra876e7882015-03-19 10:09:06 +0100280 tk->tkr_mono.xtime_nsec >>= -shift_change;
John Stultz1e75fa82012-07-13 01:21:53 -0400281 else
Peter Zijlstra876e7882015-03-19 10:09:06 +0100282 tk->tkr_mono.xtime_nsec <<= shift_change;
John Stultz1e75fa82012-07-13 01:21:53 -0400283 }
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100284 tk->tkr_raw.xtime_nsec = 0;
285
Peter Zijlstra876e7882015-03-19 10:09:06 +0100286 tk->tkr_mono.shift = clock->shift;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100287 tk->tkr_raw.shift = clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200288
John Stultzf726a692012-07-13 01:21:57 -0400289 tk->ntp_error = 0;
290 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
John Stultz375f45b2014-04-23 20:53:29 -0700291 tk->ntp_tick = ntpinterval << tk->ntp_error_shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200292
293 /*
294 * The timekeeper keeps its own mult values for the currently
295 * active clocksource. These value will be adjusted via NTP
296 * to counteract clock drifting.
297 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100298 tk->tkr_mono.mult = clock->mult;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100299 tk->tkr_raw.mult = clock->mult;
John Stultzdc491592013-12-06 17:25:21 -0800300 tk->ntp_err_mult = 0;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200301}
john stultz85240702007-05-08 00:27:59 -0700302
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200303/* Timekeeper helper functions. */
Stephen Warren7b1f6202012-11-07 17:58:54 -0700304
305#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000306static u32 default_arch_gettimeoffset(void) { return 0; }
307u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset;
Stephen Warren7b1f6202012-11-07 17:58:54 -0700308#else
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000309static inline u32 arch_gettimeoffset(void) { return 0; }
Stephen Warren7b1f6202012-11-07 17:58:54 -0700310#endif
311
Thomas Gleixner0e5ac3a2014-07-16 21:05:18 +0000312static inline s64 timekeeping_get_ns(struct tk_read_base *tkr)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200313{
John Stultza558cd02015-03-11 21:16:33 -0700314 cycle_t delta;
John Stultz1e75fa82012-07-13 01:21:53 -0400315 s64 nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200316
John Stultza558cd02015-03-11 21:16:33 -0700317 delta = timekeeping_get_delta(tkr);
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200318
Thomas Gleixner0e5ac3a2014-07-16 21:05:18 +0000319 nsec = delta * tkr->mult + tkr->xtime_nsec;
320 nsec >>= tkr->shift;
John Stultzf2a5a082012-07-13 01:21:55 -0400321
Stephen Warren7b1f6202012-11-07 17:58:54 -0700322 /* If arch requires, add in get_arch_timeoffset() */
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000323 return nsec + arch_gettimeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200324}
325
Thomas Gleixner4396e052014-07-16 21:05:23 +0000326/**
327 * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
Rafael J. Wysockiaffe3e82015-02-11 05:01:52 +0100328 * @tkr: Timekeeping readout base from which we take the update
Thomas Gleixner4396e052014-07-16 21:05:23 +0000329 *
330 * We want to use this from any context including NMI and tracing /
331 * instrumenting the timekeeping code itself.
332 *
333 * So we handle this differently than the other timekeeping accessor
334 * functions which retry when the sequence count has changed. The
335 * update side does:
336 *
337 * smp_wmb(); <- Ensure that the last base[1] update is visible
338 * tkf->seq++;
339 * smp_wmb(); <- Ensure that the seqcount update is visible
Rafael J. Wysockiaffe3e82015-02-11 05:01:52 +0100340 * update(tkf->base[0], tkr);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000341 * smp_wmb(); <- Ensure that the base[0] update is visible
342 * tkf->seq++;
343 * smp_wmb(); <- Ensure that the seqcount update is visible
Rafael J. Wysockiaffe3e82015-02-11 05:01:52 +0100344 * update(tkf->base[1], tkr);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000345 *
346 * The reader side does:
347 *
348 * do {
349 * seq = tkf->seq;
350 * smp_rmb();
351 * idx = seq & 0x01;
352 * now = now(tkf->base[idx]);
353 * smp_rmb();
354 * } while (seq != tkf->seq)
355 *
356 * As long as we update base[0] readers are forced off to
357 * base[1]. Once base[0] is updated readers are redirected to base[0]
358 * and the base[1] update takes place.
359 *
360 * So if a NMI hits the update of base[0] then it will use base[1]
361 * which is still consistent. In the worst case this can result is a
362 * slightly wrong timestamp (a few nanoseconds). See
363 * @ktime_get_mono_fast_ns.
364 */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100365static void update_fast_timekeeper(struct tk_read_base *tkr, struct tk_fast *tkf)
Thomas Gleixner4396e052014-07-16 21:05:23 +0000366{
Peter Zijlstra4498e742015-03-19 09:36:19 +0100367 struct tk_read_base *base = tkf->base;
Thomas Gleixner4396e052014-07-16 21:05:23 +0000368
369 /* Force readers off to base[1] */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100370 raw_write_seqcount_latch(&tkf->seq);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000371
372 /* Update base[0] */
Rafael J. Wysockiaffe3e82015-02-11 05:01:52 +0100373 memcpy(base, tkr, sizeof(*base));
Thomas Gleixner4396e052014-07-16 21:05:23 +0000374
375 /* Force readers back to base[0] */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100376 raw_write_seqcount_latch(&tkf->seq);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000377
378 /* Update base[1] */
379 memcpy(base + 1, base, sizeof(*base));
380}
381
382/**
383 * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic
384 *
385 * This timestamp is not guaranteed to be monotonic across an update.
386 * The timestamp is calculated by:
387 *
388 * now = base_mono + clock_delta * slope
389 *
390 * So if the update lowers the slope, readers who are forced to the
391 * not yet updated second array are still using the old steeper slope.
392 *
393 * tmono
394 * ^
395 * | o n
396 * | o n
397 * | u
398 * | o
399 * |o
400 * |12345678---> reader order
401 *
402 * o = old slope
403 * u = update
404 * n = new slope
405 *
406 * So reader 6 will observe time going backwards versus reader 5.
407 *
408 * While other CPUs are likely to be able observe that, the only way
409 * for a CPU local observation is when an NMI hits in the middle of
410 * the update. Timestamps taken from that NMI context might be ahead
411 * of the following timestamps. Callers need to be aware of that and
412 * deal with it.
413 */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100414static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf)
Thomas Gleixner4396e052014-07-16 21:05:23 +0000415{
416 struct tk_read_base *tkr;
417 unsigned int seq;
418 u64 now;
419
420 do {
Peter Zijlstra4498e742015-03-19 09:36:19 +0100421 seq = raw_read_seqcount(&tkf->seq);
422 tkr = tkf->base + (seq & 0x01);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100423 now = ktime_to_ns(tkr->base) + timekeeping_get_ns(tkr);
Peter Zijlstra4498e742015-03-19 09:36:19 +0100424 } while (read_seqcount_retry(&tkf->seq, seq));
Thomas Gleixner4396e052014-07-16 21:05:23 +0000425
Thomas Gleixner4396e052014-07-16 21:05:23 +0000426 return now;
427}
Peter Zijlstra4498e742015-03-19 09:36:19 +0100428
429u64 ktime_get_mono_fast_ns(void)
430{
431 return __ktime_get_fast_ns(&tk_fast_mono);
432}
Thomas Gleixner4396e052014-07-16 21:05:23 +0000433EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
434
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100435u64 ktime_get_raw_fast_ns(void)
436{
437 return __ktime_get_fast_ns(&tk_fast_raw);
438}
439EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns);
440
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100441/* Suspend-time cycles value for halted fast timekeeper. */
442static cycle_t cycles_at_suspend;
443
444static cycle_t dummy_clock_read(struct clocksource *cs)
445{
446 return cycles_at_suspend;
447}
448
449/**
450 * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource.
451 * @tk: Timekeeper to snapshot.
452 *
453 * It generally is unsafe to access the clocksource after timekeeping has been
454 * suspended, so take a snapshot of the readout base of @tk and use it as the
455 * fast timekeeper's readout base while suspended. It will return the same
456 * number of cycles every time until timekeeping is resumed at which time the
457 * proper readout base for the fast timekeeper will be restored automatically.
458 */
459static void halt_fast_timekeeper(struct timekeeper *tk)
460{
461 static struct tk_read_base tkr_dummy;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100462 struct tk_read_base *tkr = &tk->tkr_mono;
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100463
464 memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
465 cycles_at_suspend = tkr->read(tkr->clock);
466 tkr_dummy.read = dummy_clock_read;
Peter Zijlstra4498e742015-03-19 09:36:19 +0100467 update_fast_timekeeper(&tkr_dummy, &tk_fast_mono);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100468
469 tkr = &tk->tkr_raw;
470 memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
471 tkr_dummy.read = dummy_clock_read;
472 update_fast_timekeeper(&tkr_dummy, &tk_fast_raw);
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100473}
474
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000475#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
476
477static inline void update_vsyscall(struct timekeeper *tk)
478{
John Stultz0680eb12014-08-13 12:47:14 -0700479 struct timespec xt, wm;
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000480
John Stultze2dff1e2014-07-23 14:35:39 -0700481 xt = timespec64_to_timespec(tk_xtime(tk));
John Stultz0680eb12014-08-13 12:47:14 -0700482 wm = timespec64_to_timespec(tk->wall_to_monotonic);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100483 update_vsyscall_old(&xt, &wm, tk->tkr_mono.clock, tk->tkr_mono.mult,
484 tk->tkr_mono.cycle_last);
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000485}
486
487static inline void old_vsyscall_fixup(struct timekeeper *tk)
488{
489 s64 remainder;
490
491 /*
492 * Store only full nanoseconds into xtime_nsec after rounding
493 * it up and add the remainder to the error difference.
494 * XXX - This is necessary to avoid small 1ns inconsistnecies caused
495 * by truncating the remainder in vsyscalls. However, it causes
496 * additional work to be done in timekeeping_adjust(). Once
497 * the vsyscall implementations are converted to use xtime_nsec
498 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
499 * users are removed, this can be killed.
500 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100501 remainder = tk->tkr_mono.xtime_nsec & ((1ULL << tk->tkr_mono.shift) - 1);
502 tk->tkr_mono.xtime_nsec -= remainder;
503 tk->tkr_mono.xtime_nsec += 1ULL << tk->tkr_mono.shift;
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000504 tk->ntp_error += remainder << tk->ntp_error_shift;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100505 tk->ntp_error -= (1ULL << tk->tkr_mono.shift) << tk->ntp_error_shift;
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000506}
507#else
508#define old_vsyscall_fixup(tk)
509#endif
510
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200511static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
512
David Vrabel780427f2013-06-27 11:35:46 +0100513static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200514{
David Vrabel780427f2013-06-27 11:35:46 +0100515 raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200516}
517
518/**
519 * pvclock_gtod_register_notifier - register a pvclock timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200520 */
521int pvclock_gtod_register_notifier(struct notifier_block *nb)
522{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000523 struct timekeeper *tk = &tk_core.timekeeper;
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200524 unsigned long flags;
525 int ret;
526
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000527 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200528 ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
David Vrabel780427f2013-06-27 11:35:46 +0100529 update_pvclock_gtod(tk, true);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000530 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200531
532 return ret;
533}
534EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
535
536/**
537 * pvclock_gtod_unregister_notifier - unregister a pvclock
538 * timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200539 */
540int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
541{
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200542 unsigned long flags;
543 int ret;
544
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000545 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200546 ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000547 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200548
549 return ret;
550}
551EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
552
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000553/*
554 * Update the ktime_t based scalar nsec members of the timekeeper
555 */
556static inline void tk_update_ktime_data(struct timekeeper *tk)
557{
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530558 u64 seconds;
559 u32 nsec;
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000560
561 /*
562 * The xtime based monotonic readout is:
563 * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();
564 * The ktime based monotonic readout is:
565 * nsec = base_mono + now();
566 * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
567 */
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530568 seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
569 nsec = (u32) tk->wall_to_monotonic.tv_nsec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100570 tk->tkr_mono.base = ns_to_ktime(seconds * NSEC_PER_SEC + nsec);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000571
572 /* Update the monotonic raw base */
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100573 tk->tkr_raw.base = timespec64_to_ktime(tk->raw_time);
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530574
575 /*
576 * The sum of the nanoseconds portions of xtime and
577 * wall_to_monotonic can be greater/equal one second. Take
578 * this into account before updating tk->ktime_sec.
579 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100580 nsec += (u32)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530581 if (nsec >= NSEC_PER_SEC)
582 seconds++;
583 tk->ktime_sec = seconds;
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000584}
585
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000586/* must hold timekeeper_lock */
David Vrabel04397fe92013-06-27 11:35:45 +0100587static void timekeeping_update(struct timekeeper *tk, unsigned int action)
Thomas Gleixnercc062682011-11-13 23:19:49 +0000588{
David Vrabel04397fe92013-06-27 11:35:45 +0100589 if (action & TK_CLEAR_NTP) {
John Stultzf726a692012-07-13 01:21:57 -0400590 tk->ntp_error = 0;
Thomas Gleixnercc062682011-11-13 23:19:49 +0000591 ntp_clear();
592 }
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000593
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000594 tk_update_ktime_data(tk);
595
Thomas Gleixner9bf24192014-09-06 12:24:49 +0200596 update_vsyscall(tk);
597 update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
598
David Vrabel04397fe92013-06-27 11:35:45 +0100599 if (action & TK_MIRROR)
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000600 memcpy(&shadow_timekeeper, &tk_core.timekeeper,
601 sizeof(tk_core.timekeeper));
Thomas Gleixner4396e052014-07-16 21:05:23 +0000602
Peter Zijlstra4498e742015-03-19 09:36:19 +0100603 update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100604 update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw);
Thomas Gleixnercc062682011-11-13 23:19:49 +0000605}
606
john stultz85240702007-05-08 00:27:59 -0700607/**
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200608 * timekeeping_forward_now - update clock to the current time
john stultz85240702007-05-08 00:27:59 -0700609 *
Roman Zippel9a055112008-08-20 16:37:28 -0700610 * Forward the current clock to update its state since the last call to
611 * update_wall_time(). This is useful before significant clock changes,
612 * as it avoids having to deal with this time offset explicitly.
john stultz85240702007-05-08 00:27:59 -0700613 */
John Stultzf726a692012-07-13 01:21:57 -0400614static void timekeeping_forward_now(struct timekeeper *tk)
john stultz85240702007-05-08 00:27:59 -0700615{
Peter Zijlstra876e7882015-03-19 10:09:06 +0100616 struct clocksource *clock = tk->tkr_mono.clock;
Thomas Gleixner3a978372014-07-16 21:05:10 +0000617 cycle_t cycle_now, delta;
Roman Zippel9a055112008-08-20 16:37:28 -0700618 s64 nsec;
john stultz85240702007-05-08 00:27:59 -0700619
Peter Zijlstra876e7882015-03-19 10:09:06 +0100620 cycle_now = tk->tkr_mono.read(clock);
621 delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
622 tk->tkr_mono.cycle_last = cycle_now;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100623 tk->tkr_raw.cycle_last = cycle_now;
john stultz85240702007-05-08 00:27:59 -0700624
Peter Zijlstra876e7882015-03-19 10:09:06 +0100625 tk->tkr_mono.xtime_nsec += delta * tk->tkr_mono.mult;
john stultz7d275582009-05-01 13:10:26 -0700626
Stephen Warren7b1f6202012-11-07 17:58:54 -0700627 /* If arch requires, add in get_arch_timeoffset() */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100628 tk->tkr_mono.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_mono.shift;
john stultz7d275582009-05-01 13:10:26 -0700629
John Stultzf726a692012-07-13 01:21:57 -0400630 tk_normalize_xtime(tk);
John Stultz2d422442008-08-20 16:37:30 -0700631
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100632 nsec = clocksource_cyc2ns(delta, tk->tkr_raw.mult, tk->tkr_raw.shift);
John Stultz7d489d12014-07-16 21:04:01 +0000633 timespec64_add_ns(&tk->raw_time, nsec);
john stultz85240702007-05-08 00:27:59 -0700634}
635
636/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000637 * __getnstimeofday64 - Returns the time of day in a timespec64.
john stultz85240702007-05-08 00:27:59 -0700638 * @ts: pointer to the timespec to be set
639 *
Kees Cook1e817fb2012-11-19 10:26:16 -0800640 * Updates the time of day in the timespec.
641 * Returns 0 on success, or -ve when suspended (timespec will be undefined).
john stultz85240702007-05-08 00:27:59 -0700642 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000643int __getnstimeofday64(struct timespec64 *ts)
john stultz85240702007-05-08 00:27:59 -0700644{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000645 struct timekeeper *tk = &tk_core.timekeeper;
john stultz85240702007-05-08 00:27:59 -0700646 unsigned long seq;
John Stultz1e75fa82012-07-13 01:21:53 -0400647 s64 nsecs = 0;
john stultz85240702007-05-08 00:27:59 -0700648
649 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000650 seq = read_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -0700651
John Stultz4e250fd2012-07-27 14:48:13 -0400652 ts->tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100653 nsecs = timekeeping_get_ns(&tk->tkr_mono);
john stultz85240702007-05-08 00:27:59 -0700654
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000655 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz85240702007-05-08 00:27:59 -0700656
John Stultzec145ba2012-09-11 19:26:03 -0400657 ts->tv_nsec = 0;
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000658 timespec64_add_ns(ts, nsecs);
Kees Cook1e817fb2012-11-19 10:26:16 -0800659
660 /*
661 * Do not bail out early, in case there were callers still using
662 * the value, even in the face of the WARN_ON.
663 */
664 if (unlikely(timekeeping_suspended))
665 return -EAGAIN;
666 return 0;
667}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000668EXPORT_SYMBOL(__getnstimeofday64);
Kees Cook1e817fb2012-11-19 10:26:16 -0800669
670/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000671 * getnstimeofday64 - Returns the time of day in a timespec64.
John Stultz5322e4c2014-11-07 13:13:04 -0800672 * @ts: pointer to the timespec64 to be set
Kees Cook1e817fb2012-11-19 10:26:16 -0800673 *
John Stultz5322e4c2014-11-07 13:13:04 -0800674 * Returns the time of day in a timespec64 (WARN if suspended).
Kees Cook1e817fb2012-11-19 10:26:16 -0800675 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000676void getnstimeofday64(struct timespec64 *ts)
Kees Cook1e817fb2012-11-19 10:26:16 -0800677{
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000678 WARN_ON(__getnstimeofday64(ts));
john stultz85240702007-05-08 00:27:59 -0700679}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000680EXPORT_SYMBOL(getnstimeofday64);
john stultz85240702007-05-08 00:27:59 -0700681
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200682ktime_t ktime_get(void)
683{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000684 struct timekeeper *tk = &tk_core.timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200685 unsigned int seq;
Thomas Gleixnera016a5b2014-07-16 21:04:12 +0000686 ktime_t base;
687 s64 nsecs;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200688
689 WARN_ON(timekeeping_suspended);
690
691 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000692 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100693 base = tk->tkr_mono.base;
694 nsecs = timekeeping_get_ns(&tk->tkr_mono);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200695
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000696 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz24e4a8c2014-07-16 21:03:53 +0000697
Thomas Gleixnera016a5b2014-07-16 21:04:12 +0000698 return ktime_add_ns(base, nsecs);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200699}
700EXPORT_SYMBOL_GPL(ktime_get);
701
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000702static ktime_t *offsets[TK_OFFS_MAX] = {
703 [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real,
704 [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot,
705 [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai,
706};
707
708ktime_t ktime_get_with_offset(enum tk_offsets offs)
709{
710 struct timekeeper *tk = &tk_core.timekeeper;
711 unsigned int seq;
712 ktime_t base, *offset = offsets[offs];
713 s64 nsecs;
714
715 WARN_ON(timekeeping_suspended);
716
717 do {
718 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100719 base = ktime_add(tk->tkr_mono.base, *offset);
720 nsecs = timekeeping_get_ns(&tk->tkr_mono);
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000721
722 } while (read_seqcount_retry(&tk_core.seq, seq));
723
724 return ktime_add_ns(base, nsecs);
725
726}
727EXPORT_SYMBOL_GPL(ktime_get_with_offset);
728
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200729/**
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000730 * ktime_mono_to_any() - convert mononotic time to any other time
731 * @tmono: time to convert.
732 * @offs: which offset to use
733 */
734ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
735{
736 ktime_t *offset = offsets[offs];
737 unsigned long seq;
738 ktime_t tconv;
739
740 do {
741 seq = read_seqcount_begin(&tk_core.seq);
742 tconv = ktime_add(tmono, *offset);
743 } while (read_seqcount_retry(&tk_core.seq, seq));
744
745 return tconv;
746}
747EXPORT_SYMBOL_GPL(ktime_mono_to_any);
748
749/**
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000750 * ktime_get_raw - Returns the raw monotonic time in ktime_t format
751 */
752ktime_t ktime_get_raw(void)
753{
754 struct timekeeper *tk = &tk_core.timekeeper;
755 unsigned int seq;
756 ktime_t base;
757 s64 nsecs;
758
759 do {
760 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100761 base = tk->tkr_raw.base;
762 nsecs = timekeeping_get_ns(&tk->tkr_raw);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000763
764 } while (read_seqcount_retry(&tk_core.seq, seq));
765
766 return ktime_add_ns(base, nsecs);
767}
768EXPORT_SYMBOL_GPL(ktime_get_raw);
769
770/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000771 * ktime_get_ts64 - get the monotonic clock in timespec64 format
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200772 * @ts: pointer to timespec variable
773 *
774 * The function calculates the monotonic clock from the realtime
775 * clock and the wall_to_monotonic offset and stores the result
John Stultz5322e4c2014-11-07 13:13:04 -0800776 * in normalized timespec64 format in the variable pointed to by @ts.
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200777 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000778void ktime_get_ts64(struct timespec64 *ts)
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200779{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000780 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000781 struct timespec64 tomono;
John Stultzec145ba2012-09-11 19:26:03 -0400782 s64 nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200783 unsigned int seq;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200784
785 WARN_ON(timekeeping_suspended);
786
787 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000788 seq = read_seqcount_begin(&tk_core.seq);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000789 ts->tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100790 nsec = timekeeping_get_ns(&tk->tkr_mono);
John Stultz4e250fd2012-07-27 14:48:13 -0400791 tomono = tk->wall_to_monotonic;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200792
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000793 } while (read_seqcount_retry(&tk_core.seq, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200794
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000795 ts->tv_sec += tomono.tv_sec;
796 ts->tv_nsec = 0;
797 timespec64_add_ns(ts, nsec + tomono.tv_nsec);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200798}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000799EXPORT_SYMBOL_GPL(ktime_get_ts64);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200800
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530801/**
802 * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC
803 *
804 * Returns the seconds portion of CLOCK_MONOTONIC with a single non
805 * serialized read. tk->ktime_sec is of type 'unsigned long' so this
806 * works on both 32 and 64 bit systems. On 32 bit systems the readout
807 * covers ~136 years of uptime which should be enough to prevent
808 * premature wrap arounds.
809 */
810time64_t ktime_get_seconds(void)
811{
812 struct timekeeper *tk = &tk_core.timekeeper;
813
814 WARN_ON(timekeeping_suspended);
815 return tk->ktime_sec;
816}
817EXPORT_SYMBOL_GPL(ktime_get_seconds);
818
Heena Sirwanidbe7aa62014-10-29 16:01:50 +0530819/**
820 * ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME
821 *
822 * Returns the wall clock seconds since 1970. This replaces the
823 * get_seconds() interface which is not y2038 safe on 32bit systems.
824 *
825 * For 64bit systems the fast access to tk->xtime_sec is preserved. On
826 * 32bit systems the access must be protected with the sequence
827 * counter to provide "atomic" access to the 64bit tk->xtime_sec
828 * value.
829 */
830time64_t ktime_get_real_seconds(void)
831{
832 struct timekeeper *tk = &tk_core.timekeeper;
833 time64_t seconds;
834 unsigned int seq;
835
836 if (IS_ENABLED(CONFIG_64BIT))
837 return tk->xtime_sec;
838
839 do {
840 seq = read_seqcount_begin(&tk_core.seq);
841 seconds = tk->xtime_sec;
842
843 } while (read_seqcount_retry(&tk_core.seq, seq));
844
845 return seconds;
846}
847EXPORT_SYMBOL_GPL(ktime_get_real_seconds);
848
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800849#ifdef CONFIG_NTP_PPS
850
851/**
852 * getnstime_raw_and_real - get day and raw monotonic time in timespec format
853 * @ts_raw: pointer to the timespec to be set to raw monotonic time
854 * @ts_real: pointer to the timespec to be set to the time of day
855 *
856 * This function reads both the time of day and raw monotonic time at the
857 * same time atomically and stores the resulting timestamps in timespec
858 * format.
859 */
860void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
861{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000862 struct timekeeper *tk = &tk_core.timekeeper;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800863 unsigned long seq;
864 s64 nsecs_raw, nsecs_real;
865
866 WARN_ON_ONCE(timekeeping_suspended);
867
868 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000869 seq = read_seqcount_begin(&tk_core.seq);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800870
John Stultz7d489d12014-07-16 21:04:01 +0000871 *ts_raw = timespec64_to_timespec(tk->raw_time);
John Stultz4e250fd2012-07-27 14:48:13 -0400872 ts_real->tv_sec = tk->xtime_sec;
John Stultz1e75fa82012-07-13 01:21:53 -0400873 ts_real->tv_nsec = 0;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800874
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100875 nsecs_raw = timekeeping_get_ns(&tk->tkr_raw);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100876 nsecs_real = timekeeping_get_ns(&tk->tkr_mono);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800877
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000878 } while (read_seqcount_retry(&tk_core.seq, seq));
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800879
880 timespec_add_ns(ts_raw, nsecs_raw);
881 timespec_add_ns(ts_real, nsecs_real);
882}
883EXPORT_SYMBOL(getnstime_raw_and_real);
884
885#endif /* CONFIG_NTP_PPS */
886
john stultz85240702007-05-08 00:27:59 -0700887/**
888 * do_gettimeofday - Returns the time of day in a timeval
889 * @tv: pointer to the timeval to be set
890 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100891 * NOTE: Users should be converted to using getnstimeofday()
john stultz85240702007-05-08 00:27:59 -0700892 */
893void do_gettimeofday(struct timeval *tv)
894{
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000895 struct timespec64 now;
john stultz85240702007-05-08 00:27:59 -0700896
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000897 getnstimeofday64(&now);
john stultz85240702007-05-08 00:27:59 -0700898 tv->tv_sec = now.tv_sec;
899 tv->tv_usec = now.tv_nsec/1000;
900}
john stultz85240702007-05-08 00:27:59 -0700901EXPORT_SYMBOL(do_gettimeofday);
Richard Cochrand239f492012-04-27 10:12:42 +0200902
john stultz85240702007-05-08 00:27:59 -0700903/**
pang.xunlei21f7eca2014-11-18 19:15:16 +0800904 * do_settimeofday64 - Sets the time of day.
905 * @ts: pointer to the timespec64 variable containing the new time
john stultz85240702007-05-08 00:27:59 -0700906 *
907 * Sets the time of day to the new time and update NTP and notify hrtimers
908 */
pang.xunlei21f7eca2014-11-18 19:15:16 +0800909int do_settimeofday64(const struct timespec64 *ts)
john stultz85240702007-05-08 00:27:59 -0700910{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000911 struct timekeeper *tk = &tk_core.timekeeper;
pang.xunlei21f7eca2014-11-18 19:15:16 +0800912 struct timespec64 ts_delta, xt;
John Stultz92c1d3e2011-11-14 14:05:44 -0800913 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700914
pang.xunlei21f7eca2014-11-18 19:15:16 +0800915 if (!timespec64_valid_strict(ts))
john stultz85240702007-05-08 00:27:59 -0700916 return -EINVAL;
917
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000918 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000919 write_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -0700920
John Stultz4e250fd2012-07-27 14:48:13 -0400921 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -0700922
John Stultz4e250fd2012-07-27 14:48:13 -0400923 xt = tk_xtime(tk);
pang.xunlei21f7eca2014-11-18 19:15:16 +0800924 ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
925 ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
John Stultz1e75fa82012-07-13 01:21:53 -0400926
John Stultz7d489d12014-07-16 21:04:01 +0000927 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
john stultz85240702007-05-08 00:27:59 -0700928
pang.xunlei21f7eca2014-11-18 19:15:16 +0800929 tk_set_xtime(tk, ts);
John Stultz1e75fa82012-07-13 01:21:53 -0400930
David Vrabel780427f2013-06-27 11:35:46 +0100931 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
john stultz85240702007-05-08 00:27:59 -0700932
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000933 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000934 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -0700935
936 /* signal hrtimers about time change */
937 clock_was_set();
938
939 return 0;
940}
pang.xunlei21f7eca2014-11-18 19:15:16 +0800941EXPORT_SYMBOL(do_settimeofday64);
john stultz85240702007-05-08 00:27:59 -0700942
John Stultzc528f7c2011-02-01 13:52:17 +0000943/**
944 * timekeeping_inject_offset - Adds or subtracts from the current time.
945 * @tv: pointer to the timespec variable containing the offset
946 *
947 * Adds or subtracts an offset value from the current time.
948 */
949int timekeeping_inject_offset(struct timespec *ts)
950{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000951 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -0800952 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +0000953 struct timespec64 ts64, tmp;
John Stultz4e8b1452012-08-08 15:36:20 -0400954 int ret = 0;
John Stultzc528f7c2011-02-01 13:52:17 +0000955
956 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
957 return -EINVAL;
958
John Stultz7d489d12014-07-16 21:04:01 +0000959 ts64 = timespec_to_timespec64(*ts);
960
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000961 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000962 write_seqcount_begin(&tk_core.seq);
John Stultzc528f7c2011-02-01 13:52:17 +0000963
John Stultz4e250fd2012-07-27 14:48:13 -0400964 timekeeping_forward_now(tk);
John Stultzc528f7c2011-02-01 13:52:17 +0000965
John Stultz4e8b1452012-08-08 15:36:20 -0400966 /* Make sure the proposed value is valid */
John Stultz7d489d12014-07-16 21:04:01 +0000967 tmp = timespec64_add(tk_xtime(tk), ts64);
968 if (!timespec64_valid_strict(&tmp)) {
John Stultz4e8b1452012-08-08 15:36:20 -0400969 ret = -EINVAL;
970 goto error;
971 }
John Stultz1e75fa82012-07-13 01:21:53 -0400972
John Stultz7d489d12014-07-16 21:04:01 +0000973 tk_xtime_add(tk, &ts64);
974 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts64));
John Stultzc528f7c2011-02-01 13:52:17 +0000975
John Stultz4e8b1452012-08-08 15:36:20 -0400976error: /* even if we error out, we forwarded the time, so call update */
David Vrabel780427f2013-06-27 11:35:46 +0100977 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultzc528f7c2011-02-01 13:52:17 +0000978
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000979 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000980 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzc528f7c2011-02-01 13:52:17 +0000981
982 /* signal hrtimers about time change */
983 clock_was_set();
984
John Stultz4e8b1452012-08-08 15:36:20 -0400985 return ret;
John Stultzc528f7c2011-02-01 13:52:17 +0000986}
987EXPORT_SYMBOL(timekeeping_inject_offset);
988
John Stultzcc244dd2012-05-03 12:30:07 -0700989
990/**
991 * timekeeping_get_tai_offset - Returns current TAI offset from UTC
992 *
993 */
994s32 timekeeping_get_tai_offset(void)
995{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000996 struct timekeeper *tk = &tk_core.timekeeper;
John Stultzcc244dd2012-05-03 12:30:07 -0700997 unsigned int seq;
998 s32 ret;
999
1000 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001001 seq = read_seqcount_begin(&tk_core.seq);
John Stultzcc244dd2012-05-03 12:30:07 -07001002 ret = tk->tai_offset;
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001003 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultzcc244dd2012-05-03 12:30:07 -07001004
1005 return ret;
1006}
1007
1008/**
1009 * __timekeeping_set_tai_offset - Lock free worker function
1010 *
1011 */
Fengguang Wudd5d70e82013-03-25 12:24:24 -07001012static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
John Stultzcc244dd2012-05-03 12:30:07 -07001013{
1014 tk->tai_offset = tai_offset;
John Stultz04005f62013-12-10 17:13:35 -08001015 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
John Stultzcc244dd2012-05-03 12:30:07 -07001016}
1017
1018/**
1019 * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
1020 *
1021 */
1022void timekeeping_set_tai_offset(s32 tai_offset)
1023{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001024 struct timekeeper *tk = &tk_core.timekeeper;
John Stultzcc244dd2012-05-03 12:30:07 -07001025 unsigned long flags;
1026
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001027 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001028 write_seqcount_begin(&tk_core.seq);
John Stultzcc244dd2012-05-03 12:30:07 -07001029 __timekeeping_set_tai_offset(tk, tai_offset);
John Stultzf55c0762013-12-11 18:50:25 -08001030 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001031 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001032 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz4e8f8b32013-04-10 12:41:49 -07001033 clock_was_set();
John Stultzcc244dd2012-05-03 12:30:07 -07001034}
1035
john stultz85240702007-05-08 00:27:59 -07001036/**
1037 * change_clocksource - Swaps clocksources if a new one is available
1038 *
1039 * Accumulates current time interval and initializes new clocksource
1040 */
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001041static int change_clocksource(void *data)
john stultz85240702007-05-08 00:27:59 -07001042{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001043 struct timekeeper *tk = &tk_core.timekeeper;
Magnus Damm4614e6a2009-04-21 12:24:02 -07001044 struct clocksource *new, *old;
John Stultzf695cf92012-03-14 16:38:15 -07001045 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -07001046
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001047 new = (struct clocksource *) data;
john stultz85240702007-05-08 00:27:59 -07001048
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001049 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001050 write_seqcount_begin(&tk_core.seq);
John Stultzf695cf92012-03-14 16:38:15 -07001051
John Stultz4e250fd2012-07-27 14:48:13 -04001052 timekeeping_forward_now(tk);
Thomas Gleixner09ac3692013-04-25 20:31:44 +00001053 /*
1054 * If the cs is in module, get a module reference. Succeeds
1055 * for built-in code (owner == NULL) as well.
1056 */
1057 if (try_module_get(new->owner)) {
1058 if (!new->enable || new->enable(new) == 0) {
Peter Zijlstra876e7882015-03-19 10:09:06 +01001059 old = tk->tkr_mono.clock;
Thomas Gleixner09ac3692013-04-25 20:31:44 +00001060 tk_setup_internals(tk, new);
1061 if (old->disable)
1062 old->disable(old);
1063 module_put(old->owner);
1064 } else {
1065 module_put(new->owner);
1066 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001067 }
David Vrabel780427f2013-06-27 11:35:46 +01001068 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultzf695cf92012-03-14 16:38:15 -07001069
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001070 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001071 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzf695cf92012-03-14 16:38:15 -07001072
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001073 return 0;
1074}
john stultz85240702007-05-08 00:27:59 -07001075
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001076/**
1077 * timekeeping_notify - Install a new clock source
1078 * @clock: pointer to the clock source
1079 *
1080 * This function is called from clocksource.c after a new, better clock
1081 * source has been registered. The caller holds the clocksource_mutex.
1082 */
Thomas Gleixnerba919d12013-04-25 20:31:44 +00001083int timekeeping_notify(struct clocksource *clock)
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001084{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001085 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04001086
Peter Zijlstra876e7882015-03-19 10:09:06 +01001087 if (tk->tkr_mono.clock == clock)
Thomas Gleixnerba919d12013-04-25 20:31:44 +00001088 return 0;
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001089 stop_machine(change_clocksource, clock, NULL);
john stultz85240702007-05-08 00:27:59 -07001090 tick_clock_notify();
Peter Zijlstra876e7882015-03-19 10:09:06 +01001091 return tk->tkr_mono.clock == clock ? 0 : -1;
john stultz85240702007-05-08 00:27:59 -07001092}
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001093
Thomas Gleixnera40f2622009-07-07 13:00:31 +02001094/**
John Stultzcdba2ec2014-11-07 11:03:20 -08001095 * getrawmonotonic64 - Returns the raw monotonic time in a timespec
1096 * @ts: pointer to the timespec64 to be set
John Stultz2d422442008-08-20 16:37:30 -07001097 *
1098 * Returns the raw monotonic time (completely un-modified by ntp)
1099 */
John Stultzcdba2ec2014-11-07 11:03:20 -08001100void getrawmonotonic64(struct timespec64 *ts)
John Stultz2d422442008-08-20 16:37:30 -07001101{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001102 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz7d489d12014-07-16 21:04:01 +00001103 struct timespec64 ts64;
John Stultz2d422442008-08-20 16:37:30 -07001104 unsigned long seq;
1105 s64 nsecs;
John Stultz2d422442008-08-20 16:37:30 -07001106
1107 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001108 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01001109 nsecs = timekeeping_get_ns(&tk->tkr_raw);
John Stultz7d489d12014-07-16 21:04:01 +00001110 ts64 = tk->raw_time;
John Stultz2d422442008-08-20 16:37:30 -07001111
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001112 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz2d422442008-08-20 16:37:30 -07001113
John Stultz7d489d12014-07-16 21:04:01 +00001114 timespec64_add_ns(&ts64, nsecs);
John Stultzcdba2ec2014-11-07 11:03:20 -08001115 *ts = ts64;
John Stultz2d422442008-08-20 16:37:30 -07001116}
John Stultzcdba2ec2014-11-07 11:03:20 -08001117EXPORT_SYMBOL(getrawmonotonic64);
1118
John Stultz2d422442008-08-20 16:37:30 -07001119
John Stultz2d422442008-08-20 16:37:30 -07001120/**
Li Zefancf4fc6c2008-02-08 04:19:24 -08001121 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -07001122 */
Li Zefancf4fc6c2008-02-08 04:19:24 -08001123int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -07001124{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001125 struct timekeeper *tk = &tk_core.timekeeper;
john stultz85240702007-05-08 00:27:59 -07001126 unsigned long seq;
1127 int ret;
1128
1129 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001130 seq = read_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -07001131
Peter Zijlstra876e7882015-03-19 10:09:06 +01001132 ret = tk->tkr_mono.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -07001133
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001134 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz85240702007-05-08 00:27:59 -07001135
1136 return ret;
1137}
1138
1139/**
Jon Hunter98962462009-08-18 12:45:10 -05001140 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
Jon Hunter98962462009-08-18 12:45:10 -05001141 */
1142u64 timekeeping_max_deferment(void)
1143{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001144 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz70471f22011-11-14 12:48:10 -08001145 unsigned long seq;
1146 u64 ret;
John Stultz42e71e82012-07-13 01:21:51 -04001147
John Stultz70471f22011-11-14 12:48:10 -08001148 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001149 seq = read_seqcount_begin(&tk_core.seq);
John Stultz70471f22011-11-14 12:48:10 -08001150
Peter Zijlstra876e7882015-03-19 10:09:06 +01001151 ret = tk->tkr_mono.clock->max_idle_ns;
John Stultz70471f22011-11-14 12:48:10 -08001152
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001153 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz70471f22011-11-14 12:48:10 -08001154
1155 return ret;
Jon Hunter98962462009-08-18 12:45:10 -05001156}
1157
1158/**
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001159 * read_persistent_clock - Return time from the persistent clock.
john stultz85240702007-05-08 00:27:59 -07001160 *
1161 * Weak dummy function for arches that do not yet support it.
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001162 * Reads the time from the battery backed persistent clock.
1163 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
john stultz85240702007-05-08 00:27:59 -07001164 *
1165 * XXX - Do be sure to remove it once all arches implement it.
1166 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001167void __weak read_persistent_clock(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -07001168{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001169 ts->tv_sec = 0;
1170 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -07001171}
1172
Xunlei Pang2ee96632015-04-01 20:34:22 -07001173void __weak read_persistent_clock64(struct timespec64 *ts64)
1174{
1175 struct timespec ts;
1176
1177 read_persistent_clock(&ts);
1178 *ts64 = timespec_to_timespec64(ts);
1179}
1180
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001181/**
1182 * read_boot_clock - Return time of the system start.
1183 *
1184 * Weak dummy function for arches that do not yet support it.
1185 * Function to read the exact time the system has been started.
1186 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
1187 *
1188 * XXX - Do be sure to remove it once all arches implement it.
1189 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001190void __weak read_boot_clock(struct timespec *ts)
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001191{
1192 ts->tv_sec = 0;
1193 ts->tv_nsec = 0;
1194}
1195
Xunlei Pang9a806dd2015-04-01 20:34:21 -07001196void __weak read_boot_clock64(struct timespec64 *ts64)
1197{
1198 struct timespec ts;
1199
1200 read_boot_clock(&ts);
1201 *ts64 = timespec_to_timespec64(ts);
1202}
1203
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001204/* Flag for if timekeeping_resume() has injected sleeptime */
1205static bool sleeptime_injected;
1206
1207/* Flag for if there is a persistent clock on this platform */
1208static bool persistent_clock_exists;
1209
john stultz85240702007-05-08 00:27:59 -07001210/*
1211 * timekeeping_init - Initializes the clocksource and common timekeeping values
1212 */
1213void __init timekeeping_init(void)
1214{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001215 struct timekeeper *tk = &tk_core.timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +02001216 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -07001217 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001218 struct timespec64 now, boot, tmp;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001219
Xunlei Pang2ee96632015-04-01 20:34:22 -07001220 read_persistent_clock64(&now);
John Stultz7d489d12014-07-16 21:04:01 +00001221 if (!timespec64_valid_strict(&now)) {
John Stultz4e8b1452012-08-08 15:36:20 -04001222 pr_warn("WARNING: Persistent clock returned invalid value!\n"
1223 " Check your CMOS/BIOS settings.\n");
1224 now.tv_sec = 0;
1225 now.tv_nsec = 0;
Feng Tang31ade302013-01-16 00:09:47 +08001226 } else if (now.tv_sec || now.tv_nsec)
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001227 persistent_clock_exists = true;
John Stultz4e8b1452012-08-08 15:36:20 -04001228
Xunlei Pang9a806dd2015-04-01 20:34:21 -07001229 read_boot_clock64(&boot);
John Stultz7d489d12014-07-16 21:04:01 +00001230 if (!timespec64_valid_strict(&boot)) {
John Stultz4e8b1452012-08-08 15:36:20 -04001231 pr_warn("WARNING: Boot clock returned invalid value!\n"
1232 " Check your CMOS/BIOS settings.\n");
1233 boot.tv_sec = 0;
1234 boot.tv_nsec = 0;
1235 }
john stultz85240702007-05-08 00:27:59 -07001236
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001237 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001238 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07001239 ntp_init();
1240
Martin Schwidefskyf1b82742009-08-14 15:47:21 +02001241 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +02001242 if (clock->enable)
1243 clock->enable(clock);
John Stultz4e250fd2012-07-27 14:48:13 -04001244 tk_setup_internals(tk, clock);
john stultz85240702007-05-08 00:27:59 -07001245
John Stultz4e250fd2012-07-27 14:48:13 -04001246 tk_set_xtime(tk, &now);
1247 tk->raw_time.tv_sec = 0;
1248 tk->raw_time.tv_nsec = 0;
John Stultz1e75fa82012-07-13 01:21:53 -04001249 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
John Stultz4e250fd2012-07-27 14:48:13 -04001250 boot = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -04001251
John Stultz7d489d12014-07-16 21:04:01 +00001252 set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec);
John Stultz4e250fd2012-07-27 14:48:13 -04001253 tk_set_wall_to_mono(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -04001254
Thomas Gleixnerf111adf2014-07-16 21:04:09 +00001255 timekeeping_update(tk, TK_MIRROR);
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001256
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001257 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001258 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001259}
1260
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001261/* time in seconds when suspend began for persistent clock */
John Stultz7d489d12014-07-16 21:04:01 +00001262static struct timespec64 timekeeping_suspend_time;
john stultz85240702007-05-08 00:27:59 -07001263
1264/**
John Stultz304529b2011-04-01 14:32:09 -07001265 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
1266 * @delta: pointer to a timespec delta value
1267 *
1268 * Takes a timespec offset measuring a suspend interval and properly
1269 * adds the sleep offset to the timekeeping variables.
1270 */
John Stultzf726a692012-07-13 01:21:57 -04001271static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
John Stultz7d489d12014-07-16 21:04:01 +00001272 struct timespec64 *delta)
John Stultz304529b2011-04-01 14:32:09 -07001273{
John Stultz7d489d12014-07-16 21:04:01 +00001274 if (!timespec64_valid_strict(delta)) {
John Stultz6d9bcb62014-06-04 16:11:43 -07001275 printk_deferred(KERN_WARNING
1276 "__timekeeping_inject_sleeptime: Invalid "
1277 "sleep delta value!\n");
John Stultzcb5de2f8d2011-06-01 18:18:09 -07001278 return;
1279 }
John Stultzf726a692012-07-13 01:21:57 -04001280 tk_xtime_add(tk, delta);
John Stultz7d489d12014-07-16 21:04:01 +00001281 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
Thomas Gleixner47da70d2014-07-16 21:05:00 +00001282 tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
Colin Cross5c835452013-05-21 22:32:14 -07001283 tk_debug_account_sleep_time(delta);
John Stultz304529b2011-04-01 14:32:09 -07001284}
1285
Xunlei Pang7f298132015-04-01 20:34:35 -07001286#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
John Stultz304529b2011-04-01 14:32:09 -07001287/**
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001288 * We have three kinds of time sources to use for sleep time
1289 * injection, the preference order is:
1290 * 1) non-stop clocksource
1291 * 2) persistent clock (ie: RTC accessible when irqs are off)
1292 * 3) RTC
1293 *
1294 * 1) and 2) are used by timekeeping, 3) by RTC subsystem.
1295 * If system has neither 1) nor 2), 3) will be used finally.
1296 *
1297 *
1298 * If timekeeping has injected sleeptime via either 1) or 2),
1299 * 3) becomes needless, so in this case we don't need to call
1300 * rtc_resume(), and this is what timekeeping_rtc_skipresume()
1301 * means.
1302 */
1303bool timekeeping_rtc_skipresume(void)
1304{
1305 return sleeptime_injected;
1306}
1307
1308/**
1309 * 1) can be determined whether to use or not only when doing
1310 * timekeeping_resume() which is invoked after rtc_suspend(),
1311 * so we can't skip rtc_suspend() surely if system has 1).
1312 *
1313 * But if system has 2), 2) will definitely be used, so in this
1314 * case we don't need to call rtc_suspend(), and this is what
1315 * timekeeping_rtc_skipsuspend() means.
1316 */
1317bool timekeeping_rtc_skipsuspend(void)
1318{
1319 return persistent_clock_exists;
1320}
1321
1322/**
pang.xunlei04d90892014-11-18 19:15:17 +08001323 * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values
1324 * @delta: pointer to a timespec64 delta value
John Stultz304529b2011-04-01 14:32:09 -07001325 *
Xunlei Pang2ee96632015-04-01 20:34:22 -07001326 * This hook is for architectures that cannot support read_persistent_clock64
John Stultz304529b2011-04-01 14:32:09 -07001327 * because their RTC/persistent clock is only accessible when irqs are enabled.
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001328 * and also don't have an effective nonstop clocksource.
John Stultz304529b2011-04-01 14:32:09 -07001329 *
1330 * This function should only be called by rtc_resume(), and allows
1331 * a suspend offset to be injected into the timekeeping values.
1332 */
pang.xunlei04d90892014-11-18 19:15:17 +08001333void timekeeping_inject_sleeptime64(struct timespec64 *delta)
John Stultz304529b2011-04-01 14:32:09 -07001334{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001335 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -08001336 unsigned long flags;
John Stultz304529b2011-04-01 14:32:09 -07001337
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001338 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001339 write_seqcount_begin(&tk_core.seq);
John Stultz70471f22011-11-14 12:48:10 -08001340
John Stultz4e250fd2012-07-27 14:48:13 -04001341 timekeeping_forward_now(tk);
John Stultz304529b2011-04-01 14:32:09 -07001342
pang.xunlei04d90892014-11-18 19:15:17 +08001343 __timekeeping_inject_sleeptime(tk, delta);
John Stultz304529b2011-04-01 14:32:09 -07001344
David Vrabel780427f2013-06-27 11:35:46 +01001345 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultz304529b2011-04-01 14:32:09 -07001346
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001347 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001348 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz304529b2011-04-01 14:32:09 -07001349
1350 /* signal hrtimers about time change */
1351 clock_was_set();
1352}
Xunlei Pang7f298132015-04-01 20:34:35 -07001353#endif
John Stultz304529b2011-04-01 14:32:09 -07001354
John Stultz304529b2011-04-01 14:32:09 -07001355/**
john stultz85240702007-05-08 00:27:59 -07001356 * timekeeping_resume - Resumes the generic timekeeping subsystem.
john stultz85240702007-05-08 00:27:59 -07001357 *
1358 * This is for the generic clocksource timekeeping.
1359 * xtime/wall_to_monotonic/jiffies/etc are
1360 * still managed by arch specific suspend/resume code.
1361 */
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +01001362void timekeeping_resume(void)
john stultz85240702007-05-08 00:27:59 -07001363{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001364 struct timekeeper *tk = &tk_core.timekeeper;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001365 struct clocksource *clock = tk->tkr_mono.clock;
John Stultz92c1d3e2011-11-14 14:05:44 -08001366 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001367 struct timespec64 ts_new, ts_delta;
Feng Tange445cf12013-03-12 11:56:48 +08001368 cycle_t cycle_now, cycle_delta;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001369
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001370 sleeptime_injected = false;
Xunlei Pang2ee96632015-04-01 20:34:22 -07001371 read_persistent_clock64(&ts_new);
john stultz85240702007-05-08 00:27:59 -07001372
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +02001373 clockevents_resume();
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +02001374 clocksource_resume();
1375
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001376 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001377 write_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -07001378
Feng Tange445cf12013-03-12 11:56:48 +08001379 /*
1380 * After system resumes, we need to calculate the suspended time and
1381 * compensate it for the OS time. There are 3 sources that could be
1382 * used: Nonstop clocksource during suspend, persistent clock and rtc
1383 * device.
1384 *
1385 * One specific platform may have 1 or 2 or all of them, and the
1386 * preference will be:
1387 * suspend-nonstop clocksource -> persistent clock -> rtc
1388 * The less preferred source will only be tried if there is no better
1389 * usable source. The rtc part is handled separately in rtc core code.
1390 */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001391 cycle_now = tk->tkr_mono.read(clock);
Feng Tange445cf12013-03-12 11:56:48 +08001392 if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
Peter Zijlstra876e7882015-03-19 10:09:06 +01001393 cycle_now > tk->tkr_mono.cycle_last) {
Feng Tange445cf12013-03-12 11:56:48 +08001394 u64 num, max = ULLONG_MAX;
1395 u32 mult = clock->mult;
1396 u32 shift = clock->shift;
1397 s64 nsec = 0;
1398
Peter Zijlstra876e7882015-03-19 10:09:06 +01001399 cycle_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last,
1400 tk->tkr_mono.mask);
Feng Tange445cf12013-03-12 11:56:48 +08001401
1402 /*
1403 * "cycle_delta * mutl" may cause 64 bits overflow, if the
1404 * suspended time is too long. In that case we need do the
1405 * 64 bits math carefully
1406 */
1407 do_div(max, mult);
1408 if (cycle_delta > max) {
1409 num = div64_u64(cycle_delta, max);
1410 nsec = (((u64) max * mult) >> shift) * num;
1411 cycle_delta -= num * max;
1412 }
1413 nsec += ((u64) cycle_delta * mult) >> shift;
1414
John Stultz7d489d12014-07-16 21:04:01 +00001415 ts_delta = ns_to_timespec64(nsec);
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001416 sleeptime_injected = true;
John Stultz7d489d12014-07-16 21:04:01 +00001417 } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
1418 ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001419 sleeptime_injected = true;
john stultz85240702007-05-08 00:27:59 -07001420 }
Feng Tange445cf12013-03-12 11:56:48 +08001421
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001422 if (sleeptime_injected)
Feng Tange445cf12013-03-12 11:56:48 +08001423 __timekeeping_inject_sleeptime(tk, &ts_delta);
1424
1425 /* Re-base the last cycle value */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001426 tk->tkr_mono.cycle_last = cycle_now;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01001427 tk->tkr_raw.cycle_last = cycle_now;
1428
John Stultz4e250fd2012-07-27 14:48:13 -04001429 tk->ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -07001430 timekeeping_suspended = 0;
David Vrabel780427f2013-06-27 11:35:46 +01001431 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001432 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001433 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001434
1435 touch_softlockup_watchdog();
1436
Thomas Gleixner4ffee522015-03-25 13:09:16 +01001437 tick_resume();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +02001438 hrtimers_resume();
john stultz85240702007-05-08 00:27:59 -07001439}
1440
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +01001441int timekeeping_suspend(void)
john stultz85240702007-05-08 00:27:59 -07001442{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001443 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -08001444 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001445 struct timespec64 delta, delta_delta;
1446 static struct timespec64 old_delta;
john stultz85240702007-05-08 00:27:59 -07001447
Xunlei Pang2ee96632015-04-01 20:34:22 -07001448 read_persistent_clock64(&timekeeping_suspend_time);
Thomas Gleixner3be90952007-09-16 15:36:43 +02001449
Zoran Markovic0d6bd992013-05-17 11:24:05 -07001450 /*
1451 * On some systems the persistent_clock can not be detected at
1452 * timekeeping_init by its return value, so if we see a valid
1453 * value returned, update the persistent_clock_exists flag.
1454 */
1455 if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001456 persistent_clock_exists = true;
Zoran Markovic0d6bd992013-05-17 11:24:05 -07001457
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001458 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001459 write_seqcount_begin(&tk_core.seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001460 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -07001461 timekeeping_suspended = 1;
John Stultzcb332172011-05-31 22:53:23 -07001462
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001463 if (persistent_clock_exists) {
John Stultzcb332172011-05-31 22:53:23 -07001464 /*
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001465 * To avoid drift caused by repeated suspend/resumes,
1466 * which each can add ~1 second drift error,
1467 * try to compensate so the difference in system time
1468 * and persistent_clock time stays close to constant.
John Stultzcb332172011-05-31 22:53:23 -07001469 */
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001470 delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time);
1471 delta_delta = timespec64_sub(delta, old_delta);
1472 if (abs(delta_delta.tv_sec) >= 2) {
1473 /*
1474 * if delta_delta is too large, assume time correction
1475 * has occurred and set old_delta to the current delta.
1476 */
1477 old_delta = delta;
1478 } else {
1479 /* Otherwise try to adjust old_system to compensate */
1480 timekeeping_suspend_time =
1481 timespec64_add(timekeeping_suspend_time, delta_delta);
1482 }
John Stultzcb332172011-05-31 22:53:23 -07001483 }
John Stultz330a1612013-12-11 19:10:36 -08001484
1485 timekeeping_update(tk, TK_MIRROR);
Rafael J. Wysocki060407a2015-02-13 14:49:02 +01001486 halt_fast_timekeeper(tk);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001487 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001488 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001489
Thomas Gleixner4ffee522015-03-25 13:09:16 +01001490 tick_suspend();
Magnus Dammc54a42b2010-02-02 14:41:41 -08001491 clocksource_suspend();
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +02001492 clockevents_suspend();
john stultz85240702007-05-08 00:27:59 -07001493
1494 return 0;
1495}
1496
1497/* sysfs resume/suspend bits for timekeeping */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001498static struct syscore_ops timekeeping_syscore_ops = {
john stultz85240702007-05-08 00:27:59 -07001499 .resume = timekeeping_resume,
1500 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -07001501};
1502
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001503static int __init timekeeping_init_ops(void)
john stultz85240702007-05-08 00:27:59 -07001504{
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001505 register_syscore_ops(&timekeeping_syscore_ops);
1506 return 0;
john stultz85240702007-05-08 00:27:59 -07001507}
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001508device_initcall(timekeeping_init_ops);
john stultz85240702007-05-08 00:27:59 -07001509
1510/*
John Stultzdc491592013-12-06 17:25:21 -08001511 * Apply a multiplier adjustment to the timekeeper
john stultz85240702007-05-08 00:27:59 -07001512 */
John Stultzdc491592013-12-06 17:25:21 -08001513static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
1514 s64 offset,
1515 bool negative,
1516 int adj_scale)
john stultz85240702007-05-08 00:27:59 -07001517{
John Stultzdc491592013-12-06 17:25:21 -08001518 s64 interval = tk->cycle_interval;
1519 s32 mult_adj = 1;
john stultz85240702007-05-08 00:27:59 -07001520
John Stultzdc491592013-12-06 17:25:21 -08001521 if (negative) {
1522 mult_adj = -mult_adj;
1523 interval = -interval;
1524 offset = -offset;
john stultz85240702007-05-08 00:27:59 -07001525 }
John Stultzdc491592013-12-06 17:25:21 -08001526 mult_adj <<= adj_scale;
1527 interval <<= adj_scale;
1528 offset <<= adj_scale;
john stultz85240702007-05-08 00:27:59 -07001529
John Stultzc2bc1112011-10-27 18:12:42 -07001530 /*
1531 * So the following can be confusing.
1532 *
John Stultzdc491592013-12-06 17:25:21 -08001533 * To keep things simple, lets assume mult_adj == 1 for now.
John Stultzc2bc1112011-10-27 18:12:42 -07001534 *
John Stultzdc491592013-12-06 17:25:21 -08001535 * When mult_adj != 1, remember that the interval and offset values
John Stultzc2bc1112011-10-27 18:12:42 -07001536 * have been appropriately scaled so the math is the same.
1537 *
1538 * The basic idea here is that we're increasing the multiplier
1539 * by one, this causes the xtime_interval to be incremented by
1540 * one cycle_interval. This is because:
1541 * xtime_interval = cycle_interval * mult
1542 * So if mult is being incremented by one:
1543 * xtime_interval = cycle_interval * (mult + 1)
1544 * Its the same as:
1545 * xtime_interval = (cycle_interval * mult) + cycle_interval
1546 * Which can be shortened to:
1547 * xtime_interval += cycle_interval
1548 *
1549 * So offset stores the non-accumulated cycles. Thus the current
1550 * time (in shifted nanoseconds) is:
1551 * now = (offset * adj) + xtime_nsec
1552 * Now, even though we're adjusting the clock frequency, we have
1553 * to keep time consistent. In other words, we can't jump back
1554 * in time, and we also want to avoid jumping forward in time.
1555 *
1556 * So given the same offset value, we need the time to be the same
1557 * both before and after the freq adjustment.
1558 * now = (offset * adj_1) + xtime_nsec_1
1559 * now = (offset * adj_2) + xtime_nsec_2
1560 * So:
1561 * (offset * adj_1) + xtime_nsec_1 =
1562 * (offset * adj_2) + xtime_nsec_2
1563 * And we know:
1564 * adj_2 = adj_1 + 1
1565 * So:
1566 * (offset * adj_1) + xtime_nsec_1 =
1567 * (offset * (adj_1+1)) + xtime_nsec_2
1568 * (offset * adj_1) + xtime_nsec_1 =
1569 * (offset * adj_1) + offset + xtime_nsec_2
1570 * Canceling the sides:
1571 * xtime_nsec_1 = offset + xtime_nsec_2
1572 * Which gives us:
1573 * xtime_nsec_2 = xtime_nsec_1 - offset
1574 * Which simplfies to:
1575 * xtime_nsec -= offset
1576 *
1577 * XXX - TODO: Doc ntp_error calculation.
1578 */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001579 if ((mult_adj > 0) && (tk->tkr_mono.mult + mult_adj < mult_adj)) {
pang.xunlei6067dc52014-10-08 15:03:34 +08001580 /* NTP adjustment caused clocksource mult overflow */
1581 WARN_ON_ONCE(1);
1582 return;
1583 }
1584
Peter Zijlstra876e7882015-03-19 10:09:06 +01001585 tk->tkr_mono.mult += mult_adj;
John Stultzf726a692012-07-13 01:21:57 -04001586 tk->xtime_interval += interval;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001587 tk->tkr_mono.xtime_nsec -= offset;
John Stultzf726a692012-07-13 01:21:57 -04001588 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
John Stultzdc491592013-12-06 17:25:21 -08001589}
John Stultz2a8c0882012-07-13 01:21:56 -04001590
John Stultzdc491592013-12-06 17:25:21 -08001591/*
1592 * Calculate the multiplier adjustment needed to match the frequency
1593 * specified by NTP
1594 */
1595static __always_inline void timekeeping_freqadjust(struct timekeeper *tk,
1596 s64 offset)
1597{
1598 s64 interval = tk->cycle_interval;
1599 s64 xinterval = tk->xtime_interval;
1600 s64 tick_error;
1601 bool negative;
1602 u32 adj;
1603
1604 /* Remove any current error adj from freq calculation */
1605 if (tk->ntp_err_mult)
1606 xinterval -= tk->cycle_interval;
1607
John Stultz375f45b2014-04-23 20:53:29 -07001608 tk->ntp_tick = ntp_tick_length();
1609
John Stultzdc491592013-12-06 17:25:21 -08001610 /* Calculate current error per tick */
1611 tick_error = ntp_tick_length() >> tk->ntp_error_shift;
1612 tick_error -= (xinterval + tk->xtime_remainder);
1613
1614 /* Don't worry about correcting it if its small */
1615 if (likely((tick_error >= 0) && (tick_error <= interval)))
1616 return;
1617
1618 /* preserve the direction of correction */
1619 negative = (tick_error < 0);
1620
1621 /* Sort out the magnitude of the correction */
1622 tick_error = abs(tick_error);
1623 for (adj = 0; tick_error > interval; adj++)
1624 tick_error >>= 1;
1625
1626 /* scale the corrections */
1627 timekeeping_apply_adjustment(tk, offset, negative, adj);
1628}
1629
1630/*
1631 * Adjust the timekeeper's multiplier to the correct frequency
1632 * and also to reduce the accumulated error value.
1633 */
1634static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
1635{
1636 /* Correct for the current frequency error */
1637 timekeeping_freqadjust(tk, offset);
1638
1639 /* Next make a small adjustment to fix any cumulative error */
1640 if (!tk->ntp_err_mult && (tk->ntp_error > 0)) {
1641 tk->ntp_err_mult = 1;
1642 timekeeping_apply_adjustment(tk, offset, 0, 0);
1643 } else if (tk->ntp_err_mult && (tk->ntp_error <= 0)) {
1644 /* Undo any existing error adjustment */
1645 timekeeping_apply_adjustment(tk, offset, 1, 0);
1646 tk->ntp_err_mult = 0;
1647 }
1648
Peter Zijlstra876e7882015-03-19 10:09:06 +01001649 if (unlikely(tk->tkr_mono.clock->maxadj &&
1650 (abs(tk->tkr_mono.mult - tk->tkr_mono.clock->mult)
1651 > tk->tkr_mono.clock->maxadj))) {
John Stultzdc491592013-12-06 17:25:21 -08001652 printk_once(KERN_WARNING
1653 "Adjusting %s more than 11%% (%ld vs %ld)\n",
Peter Zijlstra876e7882015-03-19 10:09:06 +01001654 tk->tkr_mono.clock->name, (long)tk->tkr_mono.mult,
1655 (long)tk->tkr_mono.clock->mult + tk->tkr_mono.clock->maxadj);
John Stultzdc491592013-12-06 17:25:21 -08001656 }
1657
John Stultz2a8c0882012-07-13 01:21:56 -04001658 /*
1659 * It may be possible that when we entered this function, xtime_nsec
1660 * was very small. Further, if we're slightly speeding the clocksource
1661 * in the code above, its possible the required corrective factor to
1662 * xtime_nsec could cause it to underflow.
1663 *
1664 * Now, since we already accumulated the second, cannot simply roll
1665 * the accumulated second back, since the NTP subsystem has been
1666 * notified via second_overflow. So instead we push xtime_nsec forward
1667 * by the amount we underflowed, and add that amount into the error.
1668 *
1669 * We'll correct this error next time through this function, when
1670 * xtime_nsec is not as small.
1671 */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001672 if (unlikely((s64)tk->tkr_mono.xtime_nsec < 0)) {
1673 s64 neg = -(s64)tk->tkr_mono.xtime_nsec;
1674 tk->tkr_mono.xtime_nsec = 0;
John Stultzf726a692012-07-13 01:21:57 -04001675 tk->ntp_error += neg << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -04001676 }
john stultz85240702007-05-08 00:27:59 -07001677}
1678
1679/**
John Stultz1f4f9482012-07-13 01:21:54 -04001680 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
1681 *
1682 * Helper function that accumulates a the nsecs greater then a second
1683 * from the xtime_nsec field to the xtime_secs field.
1684 * It also calls into the NTP code to handle leapsecond processing.
1685 *
1686 */
David Vrabel780427f2013-06-27 11:35:46 +01001687static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
John Stultz1f4f9482012-07-13 01:21:54 -04001688{
Peter Zijlstra876e7882015-03-19 10:09:06 +01001689 u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
John Stultz5258d3f2013-12-11 20:07:49 -08001690 unsigned int clock_set = 0;
John Stultz1f4f9482012-07-13 01:21:54 -04001691
Peter Zijlstra876e7882015-03-19 10:09:06 +01001692 while (tk->tkr_mono.xtime_nsec >= nsecps) {
John Stultz1f4f9482012-07-13 01:21:54 -04001693 int leap;
1694
Peter Zijlstra876e7882015-03-19 10:09:06 +01001695 tk->tkr_mono.xtime_nsec -= nsecps;
John Stultz1f4f9482012-07-13 01:21:54 -04001696 tk->xtime_sec++;
1697
1698 /* Figure out if its a leap sec and apply if needed */
1699 leap = second_overflow(tk->xtime_sec);
John Stultz6d0ef902012-07-27 14:48:12 -04001700 if (unlikely(leap)) {
John Stultz7d489d12014-07-16 21:04:01 +00001701 struct timespec64 ts;
John Stultz1f4f9482012-07-13 01:21:54 -04001702
John Stultz6d0ef902012-07-27 14:48:12 -04001703 tk->xtime_sec += leap;
1704
1705 ts.tv_sec = leap;
1706 ts.tv_nsec = 0;
1707 tk_set_wall_to_mono(tk,
John Stultz7d489d12014-07-16 21:04:01 +00001708 timespec64_sub(tk->wall_to_monotonic, ts));
John Stultz6d0ef902012-07-27 14:48:12 -04001709
John Stultzcc244dd2012-05-03 12:30:07 -07001710 __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
1711
John Stultz5258d3f2013-12-11 20:07:49 -08001712 clock_set = TK_CLOCK_WAS_SET;
John Stultz6d0ef902012-07-27 14:48:12 -04001713 }
John Stultz1f4f9482012-07-13 01:21:54 -04001714 }
John Stultz5258d3f2013-12-11 20:07:49 -08001715 return clock_set;
John Stultz1f4f9482012-07-13 01:21:54 -04001716}
1717
John Stultz1f4f9482012-07-13 01:21:54 -04001718/**
john stultza092ff02009-10-02 16:17:53 -07001719 * logarithmic_accumulation - shifted accumulation of cycles
1720 *
1721 * This functions accumulates a shifted interval of cycles into
1722 * into a shifted interval nanoseconds. Allows for O(log) accumulation
1723 * loop.
1724 *
1725 * Returns the unconsumed cycles.
1726 */
John Stultzf726a692012-07-13 01:21:57 -04001727static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
John Stultz5258d3f2013-12-11 20:07:49 -08001728 u32 shift,
1729 unsigned int *clock_set)
john stultza092ff02009-10-02 16:17:53 -07001730{
Thomas Gleixner23a95372013-02-21 22:51:36 +00001731 cycle_t interval = tk->cycle_interval << shift;
Jason Wesseldeda2e82010-08-09 14:20:09 -07001732 u64 raw_nsecs;
john stultza092ff02009-10-02 16:17:53 -07001733
John Stultzf726a692012-07-13 01:21:57 -04001734 /* If the offset is smaller then a shifted interval, do nothing */
Thomas Gleixner23a95372013-02-21 22:51:36 +00001735 if (offset < interval)
john stultza092ff02009-10-02 16:17:53 -07001736 return offset;
1737
1738 /* Accumulate one shifted interval */
Thomas Gleixner23a95372013-02-21 22:51:36 +00001739 offset -= interval;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001740 tk->tkr_mono.cycle_last += interval;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01001741 tk->tkr_raw.cycle_last += interval;
john stultza092ff02009-10-02 16:17:53 -07001742
Peter Zijlstra876e7882015-03-19 10:09:06 +01001743 tk->tkr_mono.xtime_nsec += tk->xtime_interval << shift;
John Stultz5258d3f2013-12-11 20:07:49 -08001744 *clock_set |= accumulate_nsecs_to_secs(tk);
john stultza092ff02009-10-02 16:17:53 -07001745
Jason Wesseldeda2e82010-08-09 14:20:09 -07001746 /* Accumulate raw time */
Dan Carpenter5b3900c2012-10-09 10:18:23 +03001747 raw_nsecs = (u64)tk->raw_interval << shift;
John Stultzf726a692012-07-13 01:21:57 -04001748 raw_nsecs += tk->raw_time.tv_nsec;
John Stultzc7dcf872010-08-13 11:30:58 -07001749 if (raw_nsecs >= NSEC_PER_SEC) {
1750 u64 raw_secs = raw_nsecs;
1751 raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
John Stultzf726a692012-07-13 01:21:57 -04001752 tk->raw_time.tv_sec += raw_secs;
john stultza092ff02009-10-02 16:17:53 -07001753 }
John Stultzf726a692012-07-13 01:21:57 -04001754 tk->raw_time.tv_nsec = raw_nsecs;
john stultza092ff02009-10-02 16:17:53 -07001755
1756 /* Accumulate error between NTP and clock interval */
John Stultz375f45b2014-04-23 20:53:29 -07001757 tk->ntp_error += tk->ntp_tick << shift;
John Stultzf726a692012-07-13 01:21:57 -04001758 tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
1759 (tk->ntp_error_shift + shift);
john stultza092ff02009-10-02 16:17:53 -07001760
1761 return offset;
1762}
1763
john stultz85240702007-05-08 00:27:59 -07001764/**
1765 * update_wall_time - Uses the current clocksource to increment the wall time
1766 *
john stultz85240702007-05-08 00:27:59 -07001767 */
John Stultz47a1b7962013-12-12 13:10:55 -08001768void update_wall_time(void)
john stultz85240702007-05-08 00:27:59 -07001769{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001770 struct timekeeper *real_tk = &tk_core.timekeeper;
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001771 struct timekeeper *tk = &shadow_timekeeper;
john stultz85240702007-05-08 00:27:59 -07001772 cycle_t offset;
john stultza092ff02009-10-02 16:17:53 -07001773 int shift = 0, maxshift;
John Stultz5258d3f2013-12-11 20:07:49 -08001774 unsigned int clock_set = 0;
John Stultz70471f22011-11-14 12:48:10 -08001775 unsigned long flags;
1776
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001777 raw_spin_lock_irqsave(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001778
1779 /* Make sure we're fully resumed: */
1780 if (unlikely(timekeeping_suspended))
John Stultz70471f22011-11-14 12:48:10 -08001781 goto out;
john stultz85240702007-05-08 00:27:59 -07001782
John Stultz592913e2010-07-13 17:56:20 -07001783#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001784 offset = real_tk->cycle_interval;
John Stultz592913e2010-07-13 17:56:20 -07001785#else
Peter Zijlstra876e7882015-03-19 10:09:06 +01001786 offset = clocksource_delta(tk->tkr_mono.read(tk->tkr_mono.clock),
1787 tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
john stultz85240702007-05-08 00:27:59 -07001788#endif
john stultz85240702007-05-08 00:27:59 -07001789
John Stultzbf2ac312012-08-21 20:30:49 -04001790 /* Check if there's really nothing to do */
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001791 if (offset < real_tk->cycle_interval)
John Stultzbf2ac312012-08-21 20:30:49 -04001792 goto out;
1793
John Stultz3c17ad12015-03-11 21:16:32 -07001794 /* Do some additional sanity checking */
1795 timekeeping_check_update(real_tk, offset);
1796
john stultza092ff02009-10-02 16:17:53 -07001797 /*
1798 * With NO_HZ we may have to accumulate many cycle_intervals
1799 * (think "ticks") worth of time at once. To do this efficiently,
1800 * we calculate the largest doubling multiple of cycle_intervals
Jim Cromie88b28ad2012-03-14 21:28:56 -06001801 * that is smaller than the offset. We then accumulate that
john stultza092ff02009-10-02 16:17:53 -07001802 * chunk in one go, and then try to consume the next smaller
1803 * doubled multiple.
john stultz85240702007-05-08 00:27:59 -07001804 */
John Stultz4e250fd2012-07-27 14:48:13 -04001805 shift = ilog2(offset) - ilog2(tk->cycle_interval);
john stultza092ff02009-10-02 16:17:53 -07001806 shift = max(0, shift);
Jim Cromie88b28ad2012-03-14 21:28:56 -06001807 /* Bound shift to one less than what overflows tick_length */
John Stultzea7cf492011-11-14 13:18:07 -08001808 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
john stultza092ff02009-10-02 16:17:53 -07001809 shift = min(shift, maxshift);
John Stultz4e250fd2012-07-27 14:48:13 -04001810 while (offset >= tk->cycle_interval) {
John Stultz5258d3f2013-12-11 20:07:49 -08001811 offset = logarithmic_accumulation(tk, offset, shift,
1812 &clock_set);
John Stultz4e250fd2012-07-27 14:48:13 -04001813 if (offset < tk->cycle_interval<<shift)
John Stultz830ec042010-03-18 14:47:30 -07001814 shift--;
john stultz85240702007-05-08 00:27:59 -07001815 }
1816
1817 /* correct the clock when NTP error is too big */
John Stultz4e250fd2012-07-27 14:48:13 -04001818 timekeeping_adjust(tk, offset);
john stultz85240702007-05-08 00:27:59 -07001819
John Stultz6a867a32010-04-06 14:30:51 -07001820 /*
John Stultz92bb1fc2012-09-04 15:38:12 -04001821 * XXX This can be killed once everyone converts
1822 * to the new update_vsyscall.
1823 */
1824 old_vsyscall_fixup(tk);
john stultz85240702007-05-08 00:27:59 -07001825
John Stultz6a867a32010-04-06 14:30:51 -07001826 /*
1827 * Finally, make sure that after the rounding
John Stultz1e75fa82012-07-13 01:21:53 -04001828 * xtime_nsec isn't larger than NSEC_PER_SEC
John Stultz6a867a32010-04-06 14:30:51 -07001829 */
John Stultz5258d3f2013-12-11 20:07:49 -08001830 clock_set |= accumulate_nsecs_to_secs(tk);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001831
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001832 write_seqcount_begin(&tk_core.seq);
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001833 /*
1834 * Update the real timekeeper.
1835 *
1836 * We could avoid this memcpy by switching pointers, but that
1837 * requires changes to all other timekeeper usage sites as
1838 * well, i.e. move the timekeeper pointer getter into the
1839 * spinlocked/seqcount protected sections. And we trade this
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001840 * memcpy under the tk_core.seq against one before we start
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001841 * updating.
1842 */
1843 memcpy(real_tk, tk, sizeof(*tk));
John Stultz5258d3f2013-12-11 20:07:49 -08001844 timekeeping_update(real_tk, clock_set);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001845 write_seqcount_end(&tk_core.seq);
Thomas Gleixnerca4523c2013-02-21 22:51:40 +00001846out:
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001847 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz47a1b7962013-12-12 13:10:55 -08001848 if (clock_set)
John Stultzcab5e122014-03-27 16:30:49 -07001849 /* Have to call _delayed version, since in irq context*/
1850 clock_was_set_delayed();
john stultz85240702007-05-08 00:27:59 -07001851}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001852
1853/**
John Stultzd08c0cd2014-12-08 12:00:09 -08001854 * getboottime64 - Return the real time of system boot.
1855 * @ts: pointer to the timespec64 to be set
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001856 *
John Stultzd08c0cd2014-12-08 12:00:09 -08001857 * Returns the wall-time of boot in a timespec64.
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001858 *
1859 * This is based on the wall_to_monotonic offset and the total suspend
1860 * time. Calls to settimeofday will affect the value returned (which
1861 * basically means that however wrong your real time clock is at boot time,
1862 * you get the right time here).
1863 */
John Stultzd08c0cd2014-12-08 12:00:09 -08001864void getboottime64(struct timespec64 *ts)
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001865{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001866 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixner02cba152014-07-16 21:04:58 +00001867 ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot);
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001868
John Stultzd08c0cd2014-12-08 12:00:09 -08001869 *ts = ktime_to_timespec64(t);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001870}
John Stultzd08c0cd2014-12-08 12:00:09 -08001871EXPORT_SYMBOL_GPL(getboottime64);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07001872
john stultz17c38b72007-07-24 18:38:34 -07001873unsigned long get_seconds(void)
1874{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001875 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04001876
1877 return tk->xtime_sec;
john stultz17c38b72007-07-24 18:38:34 -07001878}
1879EXPORT_SYMBOL(get_seconds);
1880
john stultzda15cfd2009-08-19 19:13:34 -07001881struct timespec __current_kernel_time(void)
1882{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001883 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04001884
John Stultz7d489d12014-07-16 21:04:01 +00001885 return timespec64_to_timespec(tk_xtime(tk));
john stultzda15cfd2009-08-19 19:13:34 -07001886}
john stultz17c38b72007-07-24 18:38:34 -07001887
john stultz2c6b47d2007-07-24 17:47:43 -07001888struct timespec current_kernel_time(void)
1889{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001890 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz7d489d12014-07-16 21:04:01 +00001891 struct timespec64 now;
john stultz2c6b47d2007-07-24 17:47:43 -07001892 unsigned long seq;
1893
1894 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001895 seq = read_seqcount_begin(&tk_core.seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001896
John Stultz4e250fd2012-07-27 14:48:13 -04001897 now = tk_xtime(tk);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001898 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz2c6b47d2007-07-24 17:47:43 -07001899
John Stultz7d489d12014-07-16 21:04:01 +00001900 return timespec64_to_timespec(now);
john stultz2c6b47d2007-07-24 17:47:43 -07001901}
john stultz2c6b47d2007-07-24 17:47:43 -07001902EXPORT_SYMBOL(current_kernel_time);
john stultzda15cfd2009-08-19 19:13:34 -07001903
John Stultz334334b2014-11-07 11:20:40 -08001904struct timespec64 get_monotonic_coarse64(void)
john stultzda15cfd2009-08-19 19:13:34 -07001905{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001906 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz7d489d12014-07-16 21:04:01 +00001907 struct timespec64 now, mono;
john stultzda15cfd2009-08-19 19:13:34 -07001908 unsigned long seq;
1909
1910 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001911 seq = read_seqcount_begin(&tk_core.seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08001912
John Stultz4e250fd2012-07-27 14:48:13 -04001913 now = tk_xtime(tk);
1914 mono = tk->wall_to_monotonic;
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001915 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultzda15cfd2009-08-19 19:13:34 -07001916
John Stultz7d489d12014-07-16 21:04:01 +00001917 set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec,
john stultzda15cfd2009-08-19 19:13:34 -07001918 now.tv_nsec + mono.tv_nsec);
John Stultz7d489d12014-07-16 21:04:01 +00001919
John Stultz334334b2014-11-07 11:20:40 -08001920 return now;
john stultzda15cfd2009-08-19 19:13:34 -07001921}
Torben Hohn871cf1e2011-01-27 15:58:55 +01001922
1923/*
John Stultzd6ad4182012-02-28 16:50:11 -08001924 * Must hold jiffies_lock
Torben Hohn871cf1e2011-01-27 15:58:55 +01001925 */
1926void do_timer(unsigned long ticks)
1927{
1928 jiffies_64 += ticks;
Torben Hohn871cf1e2011-01-27 15:58:55 +01001929 calc_global_load(ticks);
1930}
Torben Hohn48cf76f72011-01-27 15:59:05 +01001931
1932/**
John Stultz76f41082014-07-16 21:03:52 +00001933 * ktime_get_update_offsets_tick - hrtimer helper
1934 * @offs_real: pointer to storage for monotonic -> realtime offset
1935 * @offs_boot: pointer to storage for monotonic -> boottime offset
1936 * @offs_tai: pointer to storage for monotonic -> clock tai offset
1937 *
1938 * Returns monotonic time at last tick and various offsets
Torben Hohn48cf76f72011-01-27 15:59:05 +01001939 */
John Stultz76f41082014-07-16 21:03:52 +00001940ktime_t ktime_get_update_offsets_tick(ktime_t *offs_real, ktime_t *offs_boot,
1941 ktime_t *offs_tai)
Torben Hohn48cf76f72011-01-27 15:59:05 +01001942{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001943 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz76f41082014-07-16 21:03:52 +00001944 unsigned int seq;
Thomas Gleixner48064f52014-07-16 21:04:20 +00001945 ktime_t base;
1946 u64 nsecs;
Torben Hohn48cf76f72011-01-27 15:59:05 +01001947
1948 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001949 seq = read_seqcount_begin(&tk_core.seq);
John Stultz76f41082014-07-16 21:03:52 +00001950
Peter Zijlstra876e7882015-03-19 10:09:06 +01001951 base = tk->tkr_mono.base;
1952 nsecs = tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
Thomas Gleixner48064f52014-07-16 21:04:20 +00001953
John Stultz76f41082014-07-16 21:03:52 +00001954 *offs_real = tk->offs_real;
1955 *offs_boot = tk->offs_boot;
1956 *offs_tai = tk->offs_tai;
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001957 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz76f41082014-07-16 21:03:52 +00001958
Thomas Gleixner48064f52014-07-16 21:04:20 +00001959 return ktime_add_ns(base, nsecs);
Torben Hohn48cf76f72011-01-27 15:59:05 +01001960}
Torben Hohnf0af911a92011-01-27 15:59:10 +01001961
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001962#ifdef CONFIG_HIGH_RES_TIMERS
1963/**
John Stultz76f41082014-07-16 21:03:52 +00001964 * ktime_get_update_offsets_now - hrtimer helper
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001965 * @offs_real: pointer to storage for monotonic -> realtime offset
1966 * @offs_boot: pointer to storage for monotonic -> boottime offset
Xie XiuQib7bc50e2013-10-18 09:13:30 +08001967 * @offs_tai: pointer to storage for monotonic -> clock tai offset
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001968 *
1969 * Returns current monotonic time and updates the offsets
Xie XiuQib7bc50e2013-10-18 09:13:30 +08001970 * Called from hrtimer_interrupt() or retrigger_next_event()
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001971 */
John Stultz76f41082014-07-16 21:03:52 +00001972ktime_t ktime_get_update_offsets_now(ktime_t *offs_real, ktime_t *offs_boot,
John Stultz90adda92013-01-21 17:00:11 -08001973 ktime_t *offs_tai)
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001974{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001975 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001976 unsigned int seq;
Thomas Gleixnera37c0aa2014-07-16 21:04:19 +00001977 ktime_t base;
1978 u64 nsecs;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001979
1980 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001981 seq = read_seqcount_begin(&tk_core.seq);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001982
Peter Zijlstra876e7882015-03-19 10:09:06 +01001983 base = tk->tkr_mono.base;
1984 nsecs = timekeeping_get_ns(&tk->tkr_mono);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001985
John Stultz4e250fd2012-07-27 14:48:13 -04001986 *offs_real = tk->offs_real;
1987 *offs_boot = tk->offs_boot;
John Stultz90adda92013-01-21 17:00:11 -08001988 *offs_tai = tk->offs_tai;
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001989 } while (read_seqcount_retry(&tk_core.seq, seq));
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001990
Thomas Gleixnera37c0aa2014-07-16 21:04:19 +00001991 return ktime_add_ns(base, nsecs);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04001992}
1993#endif
1994
Torben Hohnf0af911a92011-01-27 15:59:10 +01001995/**
John Stultzaa6f9c592013-03-22 11:31:29 -07001996 * do_adjtimex() - Accessor function to NTP __do_adjtimex function
1997 */
1998int do_adjtimex(struct timex *txc)
1999{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002000 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz06c017f2013-03-22 11:37:28 -07002001 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00002002 struct timespec64 ts;
John Stultz4e8f8b32013-04-10 12:41:49 -07002003 s32 orig_tai, tai;
John Stultze4085692013-03-22 12:08:52 -07002004 int ret;
2005
2006 /* Validate the data before disabling interrupts */
2007 ret = ntp_validate_timex(txc);
2008 if (ret)
2009 return ret;
2010
John Stultzcef90372013-03-22 15:04:13 -07002011 if (txc->modes & ADJ_SETOFFSET) {
2012 struct timespec delta;
2013 delta.tv_sec = txc->time.tv_sec;
2014 delta.tv_nsec = txc->time.tv_usec;
2015 if (!(txc->modes & ADJ_NANO))
2016 delta.tv_nsec *= 1000;
2017 ret = timekeeping_inject_offset(&delta);
2018 if (ret)
2019 return ret;
2020 }
2021
Thomas Gleixnerd6d29892014-07-16 21:04:04 +00002022 getnstimeofday64(&ts);
John Stultzaa6f9c592013-03-22 11:31:29 -07002023
John Stultz06c017f2013-03-22 11:37:28 -07002024 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002025 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002026
John Stultz4e8f8b32013-04-10 12:41:49 -07002027 orig_tai = tai = tk->tai_offset;
John Stultz87ace392013-03-22 12:28:15 -07002028 ret = __do_adjtimex(txc, &ts, &tai);
2029
John Stultz4e8f8b32013-04-10 12:41:49 -07002030 if (tai != orig_tai) {
2031 __timekeeping_set_tai_offset(tk, tai);
John Stultzf55c0762013-12-11 18:50:25 -08002032 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultz4e8f8b32013-04-10 12:41:49 -07002033 }
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002034 write_seqcount_end(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002035 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
2036
John Stultz6fdda9a2013-12-10 17:18:18 -08002037 if (tai != orig_tai)
2038 clock_was_set();
2039
John Stultz7bd36012013-09-11 16:50:56 -07002040 ntp_notify_cmos_timer();
2041
John Stultz87ace392013-03-22 12:28:15 -07002042 return ret;
2043}
John Stultzaa6f9c592013-03-22 11:31:29 -07002044
2045#ifdef CONFIG_NTP_PPS
2046/**
2047 * hardpps() - Accessor function to NTP __hardpps function
2048 */
2049void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
2050{
John Stultz06c017f2013-03-22 11:37:28 -07002051 unsigned long flags;
2052
2053 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002054 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002055
John Stultzaa6f9c592013-03-22 11:31:29 -07002056 __hardpps(phase_ts, raw_ts);
John Stultz06c017f2013-03-22 11:37:28 -07002057
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002058 write_seqcount_end(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002059 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzaa6f9c592013-03-22 11:31:29 -07002060}
2061EXPORT_SYMBOL(hardpps);
2062#endif
2063
2064/**
Torben Hohnf0af911a92011-01-27 15:59:10 +01002065 * xtime_update() - advances the timekeeping infrastructure
2066 * @ticks: number of ticks, that have elapsed since the last call.
2067 *
2068 * Must be called with interrupts disabled.
2069 */
2070void xtime_update(unsigned long ticks)
2071{
John Stultzd6ad4182012-02-28 16:50:11 -08002072 write_seqlock(&jiffies_lock);
Torben Hohnf0af911a92011-01-27 15:59:10 +01002073 do_timer(ticks);
John Stultzd6ad4182012-02-28 16:50:11 -08002074 write_sequnlock(&jiffies_lock);
John Stultz47a1b7962013-12-12 13:10:55 -08002075 update_wall_time();
Torben Hohnf0af911a92011-01-27 15:59:10 +01002076}