blob: e24e1f0c5690618fdb17f70c1d56a6340e0e3ee4 [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;
Bart Van Asscheb6fc5a52018-11-28 15:43:09 -080042} tk_core ____cacheline_aligned = {
43 .seq = SEQCNT_ZERO(tk_core.seq),
44};
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +000045
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +000046static DEFINE_RAW_SPINLOCK(timekeeper_lock);
Thomas Gleixner48cdc132013-02-21 22:51:40 +000047static struct timekeeper shadow_timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020048
Thomas Gleixner4396e052014-07-16 21:05:23 +000049/**
50 * struct tk_fast - NMI safe timekeeper
51 * @seq: Sequence counter for protecting updates. The lowest bit
52 * is the index for the tk_read_base array
53 * @base: tk_read_base array. Access is indexed by the lowest bit of
54 * @seq.
55 *
56 * See @update_fast_timekeeper() below.
57 */
58struct tk_fast {
59 seqcount_t seq;
60 struct tk_read_base base[2];
61};
62
63static struct tk_fast tk_fast_mono ____cacheline_aligned;
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +010064static struct tk_fast tk_fast_raw ____cacheline_aligned;
Thomas Gleixner4396e052014-07-16 21:05:23 +000065
John Stultz8fcce542011-11-14 11:46:39 -080066/* flag for if timekeeping is suspended */
67int __read_mostly timekeeping_suspended;
68
John Stultz1e75fa82012-07-13 01:21:53 -040069static inline void tk_normalize_xtime(struct timekeeper *tk)
70{
Peter Zijlstra876e7882015-03-19 10:09:06 +010071 while (tk->tkr_mono.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_mono.shift)) {
72 tk->tkr_mono.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
John Stultz1e75fa82012-07-13 01:21:53 -040073 tk->xtime_sec++;
74 }
75}
John Stultz8fcce542011-11-14 11:46:39 -080076
Thomas Gleixnerc905fae2014-07-16 21:04:05 +000077static inline struct timespec64 tk_xtime(struct timekeeper *tk)
78{
79 struct timespec64 ts;
80
81 ts.tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +010082 ts.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
Thomas Gleixnerc905fae2014-07-16 21:04:05 +000083 return ts;
84}
85
John Stultz7d489d12014-07-16 21:04:01 +000086static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
John Stultz1e75fa82012-07-13 01:21:53 -040087{
88 tk->xtime_sec = ts->tv_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +010089 tk->tkr_mono.xtime_nsec = (u64)ts->tv_nsec << tk->tkr_mono.shift;
John Stultz1e75fa82012-07-13 01:21:53 -040090}
91
John Stultz7d489d12014-07-16 21:04:01 +000092static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
John Stultz1e75fa82012-07-13 01:21:53 -040093{
94 tk->xtime_sec += ts->tv_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +010095 tk->tkr_mono.xtime_nsec += (u64)ts->tv_nsec << tk->tkr_mono.shift;
John Stultz784ffcb2012-08-21 20:30:46 -040096 tk_normalize_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -040097}
John Stultz8fcce542011-11-14 11:46:39 -080098
John Stultz7d489d12014-07-16 21:04:01 +000099static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
John Stultz6d0ef902012-07-27 14:48:12 -0400100{
John Stultz7d489d12014-07-16 21:04:01 +0000101 struct timespec64 tmp;
John Stultz6d0ef902012-07-27 14:48:12 -0400102
103 /*
104 * Verify consistency of: offset_real = -wall_to_monotonic
105 * before modifying anything
106 */
John Stultz7d489d12014-07-16 21:04:01 +0000107 set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec,
John Stultz6d0ef902012-07-27 14:48:12 -0400108 -tk->wall_to_monotonic.tv_nsec);
John Stultz7d489d12014-07-16 21:04:01 +0000109 WARN_ON_ONCE(tk->offs_real.tv64 != timespec64_to_ktime(tmp).tv64);
John Stultz6d0ef902012-07-27 14:48:12 -0400110 tk->wall_to_monotonic = wtm;
John Stultz7d489d12014-07-16 21:04:01 +0000111 set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
112 tk->offs_real = timespec64_to_ktime(tmp);
John Stultz04005f62013-12-10 17:13:35 -0800113 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
John Stultz6d0ef902012-07-27 14:48:12 -0400114}
115
Thomas Gleixner47da70d2014-07-16 21:05:00 +0000116static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
John Stultz6d0ef902012-07-27 14:48:12 -0400117{
Thomas Gleixner47da70d2014-07-16 21:05:00 +0000118 tk->offs_boot = ktime_add(tk->offs_boot, delta);
John Stultz6d0ef902012-07-27 14:48:12 -0400119}
120
John Stultz02a37cc2017-06-08 16:44:20 -0700121/*
122 * tk_clock_read - atomic clocksource read() helper
123 *
124 * This helper is necessary to use in the read paths because, while the
125 * seqlock ensures we don't return a bad value while structures are updated,
126 * it doesn't protect from potential crashes. There is the possibility that
127 * the tkr's clocksource may change between the read reference, and the
128 * clock reference passed to the read function. This can cause crashes if
129 * the wrong clocksource is passed to the wrong read function.
130 * This isn't necessary to use when holding the timekeeper_lock or doing
131 * a read of the fast-timekeeper tkrs (which is protected by its own locking
132 * and update logic).
133 */
134static inline u64 tk_clock_read(struct tk_read_base *tkr)
135{
136 struct clocksource *clock = READ_ONCE(tkr->clock);
137
138 return clock->read(clock);
139}
140
John Stultz3c17ad12015-03-11 21:16:32 -0700141#ifdef CONFIG_DEBUG_TIMEKEEPING
John Stultz4ca22c22015-03-11 21:16:35 -0700142#define WARNING_FREQ (HZ*300) /* 5 minute rate-limiting */
John Stultz4ca22c22015-03-11 21:16:35 -0700143
John Stultz3c17ad12015-03-11 21:16:32 -0700144static void timekeeping_check_update(struct timekeeper *tk, cycle_t offset)
145{
146
Peter Zijlstra876e7882015-03-19 10:09:06 +0100147 cycle_t max_cycles = tk->tkr_mono.clock->max_cycles;
148 const char *name = tk->tkr_mono.clock->name;
John Stultz3c17ad12015-03-11 21:16:32 -0700149
150 if (offset > max_cycles) {
John Stultza558cd02015-03-11 21:16:33 -0700151 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 -0700152 offset, name, max_cycles);
John Stultza558cd02015-03-11 21:16:33 -0700153 printk_deferred(" timekeeping: Your kernel is sick, but tries to cope by capping time updates\n");
John Stultz3c17ad12015-03-11 21:16:32 -0700154 } else {
155 if (offset > (max_cycles >> 1)) {
Masanari Iidafc4fa6e2015-12-13 15:26:11 +0900156 printk_deferred("INFO: timekeeping: Cycle offset (%lld) is larger than the '%s' clock's 50%% safety margin (%lld)\n",
John Stultz3c17ad12015-03-11 21:16:32 -0700157 offset, name, max_cycles >> 1);
158 printk_deferred(" timekeeping: Your kernel is still fine, but is feeling a bit nervous\n");
159 }
160 }
John Stultz4ca22c22015-03-11 21:16:35 -0700161
John Stultz57d05a92015-05-13 16:04:47 -0700162 if (tk->underflow_seen) {
163 if (jiffies - tk->last_warning > WARNING_FREQ) {
John Stultz4ca22c22015-03-11 21:16:35 -0700164 printk_deferred("WARNING: Underflow in clocksource '%s' observed, time update ignored.\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");
John Stultz57d05a92015-05-13 16:04:47 -0700167 tk->last_warning = jiffies;
John Stultz4ca22c22015-03-11 21:16:35 -0700168 }
John Stultz57d05a92015-05-13 16:04:47 -0700169 tk->underflow_seen = 0;
John Stultz4ca22c22015-03-11 21:16:35 -0700170 }
171
John Stultz57d05a92015-05-13 16:04:47 -0700172 if (tk->overflow_seen) {
173 if (jiffies - tk->last_warning > WARNING_FREQ) {
John Stultz4ca22c22015-03-11 21:16:35 -0700174 printk_deferred("WARNING: Overflow in clocksource '%s' observed, time update capped.\n", name);
175 printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
176 printk_deferred(" Your kernel is probably still fine.\n");
John Stultz57d05a92015-05-13 16:04:47 -0700177 tk->last_warning = jiffies;
John Stultz4ca22c22015-03-11 21:16:35 -0700178 }
John Stultz57d05a92015-05-13 16:04:47 -0700179 tk->overflow_seen = 0;
John Stultz4ca22c22015-03-11 21:16:35 -0700180 }
John Stultz3c17ad12015-03-11 21:16:32 -0700181}
John Stultza558cd02015-03-11 21:16:33 -0700182
183static inline cycle_t timekeeping_get_delta(struct tk_read_base *tkr)
184{
John Stultz57d05a92015-05-13 16:04:47 -0700185 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4ca22c22015-03-11 21:16:35 -0700186 cycle_t now, last, mask, max, delta;
187 unsigned int seq;
John Stultza558cd02015-03-11 21:16:33 -0700188
John Stultz4ca22c22015-03-11 21:16:35 -0700189 /*
190 * Since we're called holding a seqlock, the data may shift
191 * under us while we're doing the calculation. This can cause
192 * false positives, since we'd note a problem but throw the
193 * results away. So nest another seqlock here to atomically
194 * grab the points we are checking with.
195 */
196 do {
197 seq = read_seqcount_begin(&tk_core.seq);
John Stultz02a37cc2017-06-08 16:44:20 -0700198 now = tk_clock_read(tkr);
John Stultz4ca22c22015-03-11 21:16:35 -0700199 last = tkr->cycle_last;
200 mask = tkr->mask;
201 max = tkr->clock->max_cycles;
202 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultza558cd02015-03-11 21:16:33 -0700203
John Stultz4ca22c22015-03-11 21:16:35 -0700204 delta = clocksource_delta(now, last, mask);
John Stultza558cd02015-03-11 21:16:33 -0700205
John Stultz057b87e2015-03-11 21:16:34 -0700206 /*
207 * Try to catch underflows by checking if we are seeing small
208 * mask-relative negative values.
209 */
John Stultz4ca22c22015-03-11 21:16:35 -0700210 if (unlikely((~delta & mask) < (mask >> 3))) {
John Stultz57d05a92015-05-13 16:04:47 -0700211 tk->underflow_seen = 1;
John Stultz057b87e2015-03-11 21:16:34 -0700212 delta = 0;
John Stultz4ca22c22015-03-11 21:16:35 -0700213 }
John Stultz057b87e2015-03-11 21:16:34 -0700214
John Stultza558cd02015-03-11 21:16:33 -0700215 /* Cap delta value to the max_cycles values to avoid mult overflows */
John Stultz4ca22c22015-03-11 21:16:35 -0700216 if (unlikely(delta > max)) {
John Stultz57d05a92015-05-13 16:04:47 -0700217 tk->overflow_seen = 1;
John Stultza558cd02015-03-11 21:16:33 -0700218 delta = tkr->clock->max_cycles;
John Stultz4ca22c22015-03-11 21:16:35 -0700219 }
John Stultza558cd02015-03-11 21:16:33 -0700220
221 return delta;
222}
John Stultz3c17ad12015-03-11 21:16:32 -0700223#else
224static inline void timekeeping_check_update(struct timekeeper *tk, cycle_t offset)
225{
226}
John Stultza558cd02015-03-11 21:16:33 -0700227static inline cycle_t timekeeping_get_delta(struct tk_read_base *tkr)
228{
229 cycle_t cycle_now, delta;
230
231 /* read clocksource */
John Stultz02a37cc2017-06-08 16:44:20 -0700232 cycle_now = tk_clock_read(tkr);
John Stultza558cd02015-03-11 21:16:33 -0700233
234 /* calculate the delta since the last update_wall_time */
235 delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
236
237 return delta;
238}
John Stultz3c17ad12015-03-11 21:16:32 -0700239#endif
240
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200241/**
Yijing Wangd26e4fe2013-11-28 16:28:55 +0800242 * tk_setup_internals - Set up internals to use clocksource clock.
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200243 *
Yijing Wangd26e4fe2013-11-28 16:28:55 +0800244 * @tk: The target timekeeper to setup.
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200245 * @clock: Pointer to clocksource.
246 *
247 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
248 * pair and interval request.
249 *
250 * Unless you're the timekeeping code, you should not be using this!
251 */
John Stultzf726a692012-07-13 01:21:57 -0400252static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200253{
254 cycle_t interval;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700255 u64 tmp, ntpinterval;
John Stultz1e75fa82012-07-13 01:21:53 -0400256 struct clocksource *old_clock;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200257
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800258 ++tk->cs_was_changed_seq;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100259 old_clock = tk->tkr_mono.clock;
260 tk->tkr_mono.clock = clock;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100261 tk->tkr_mono.mask = clock->mask;
John Stultz02a37cc2017-06-08 16:44:20 -0700262 tk->tkr_mono.cycle_last = tk_clock_read(&tk->tkr_mono);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200263
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100264 tk->tkr_raw.clock = clock;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100265 tk->tkr_raw.mask = clock->mask;
266 tk->tkr_raw.cycle_last = tk->tkr_mono.cycle_last;
267
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200268 /* Do the ns -> cycle conversion first, using original mult */
269 tmp = NTP_INTERVAL_LENGTH;
270 tmp <<= clock->shift;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700271 ntpinterval = tmp;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200272 tmp += clock->mult/2;
273 do_div(tmp, clock->mult);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200274 if (tmp == 0)
275 tmp = 1;
276
277 interval = (cycle_t) tmp;
John Stultzf726a692012-07-13 01:21:57 -0400278 tk->cycle_interval = interval;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200279
280 /* Go back from cycles -> shifted ns */
John Stultzf726a692012-07-13 01:21:57 -0400281 tk->xtime_interval = (u64) interval * clock->mult;
282 tk->xtime_remainder = ntpinterval - tk->xtime_interval;
John Stultza53bfdd2017-06-08 16:44:21 -0700283 tk->raw_interval = interval * clock->mult;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200284
John Stultz1e75fa82012-07-13 01:21:53 -0400285 /* if changing clocks, convert xtime_nsec shift units */
286 if (old_clock) {
287 int shift_change = clock->shift - old_clock->shift;
288 if (shift_change < 0)
Peter Zijlstra876e7882015-03-19 10:09:06 +0100289 tk->tkr_mono.xtime_nsec >>= -shift_change;
John Stultz1e75fa82012-07-13 01:21:53 -0400290 else
Peter Zijlstra876e7882015-03-19 10:09:06 +0100291 tk->tkr_mono.xtime_nsec <<= shift_change;
John Stultz1e75fa82012-07-13 01:21:53 -0400292 }
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100293 tk->tkr_raw.xtime_nsec = 0;
294
Peter Zijlstra876e7882015-03-19 10:09:06 +0100295 tk->tkr_mono.shift = clock->shift;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100296 tk->tkr_raw.shift = clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200297
John Stultzf726a692012-07-13 01:21:57 -0400298 tk->ntp_error = 0;
299 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
John Stultz375f45b2014-04-23 20:53:29 -0700300 tk->ntp_tick = ntpinterval << tk->ntp_error_shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200301
302 /*
303 * The timekeeper keeps its own mult values for the currently
304 * active clocksource. These value will be adjusted via NTP
305 * to counteract clock drifting.
306 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100307 tk->tkr_mono.mult = clock->mult;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100308 tk->tkr_raw.mult = clock->mult;
John Stultzdc491592013-12-06 17:25:21 -0800309 tk->ntp_err_mult = 0;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200310}
john stultz85240702007-05-08 00:27:59 -0700311
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200312/* Timekeeper helper functions. */
Stephen Warren7b1f6202012-11-07 17:58:54 -0700313
314#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000315static u32 default_arch_gettimeoffset(void) { return 0; }
316u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset;
Stephen Warren7b1f6202012-11-07 17:58:54 -0700317#else
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000318static inline u32 arch_gettimeoffset(void) { return 0; }
Stephen Warren7b1f6202012-11-07 17:58:54 -0700319#endif
320
Thomas Gleixnerca229752016-12-08 20:49:32 +0000321static inline u64 timekeeping_delta_to_ns(struct tk_read_base *tkr,
Christopher S. Hall6bd58f02016-02-22 03:15:19 -0800322 cycle_t delta)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200323{
Thomas Gleixnerca229752016-12-08 20:49:32 +0000324 u64 nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200325
Christopher S. Hall6bd58f02016-02-22 03:15:19 -0800326 nsec = delta * tkr->mult + tkr->xtime_nsec;
327 nsec >>= tkr->shift;
John Stultzf2a5a082012-07-13 01:21:55 -0400328
Stephen Warren7b1f6202012-11-07 17:58:54 -0700329 /* If arch requires, add in get_arch_timeoffset() */
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000330 return nsec + arch_gettimeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200331}
332
Christopher S. Hall6bd58f02016-02-22 03:15:19 -0800333static inline s64 timekeeping_get_ns(struct tk_read_base *tkr)
334{
335 cycle_t delta;
336
337 delta = timekeeping_get_delta(tkr);
338 return timekeeping_delta_to_ns(tkr, delta);
339}
340
341static inline s64 timekeeping_cycles_to_ns(struct tk_read_base *tkr,
342 cycle_t cycles)
343{
344 cycle_t delta;
345
346 /* calculate the delta since the last update_wall_time */
347 delta = clocksource_delta(cycles, tkr->cycle_last, tkr->mask);
348 return timekeeping_delta_to_ns(tkr, delta);
349}
350
Thomas Gleixner4396e052014-07-16 21:05:23 +0000351/**
352 * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
Rafael J. Wysockiaffe3e82015-02-11 05:01:52 +0100353 * @tkr: Timekeeping readout base from which we take the update
Thomas Gleixner4396e052014-07-16 21:05:23 +0000354 *
355 * We want to use this from any context including NMI and tracing /
356 * instrumenting the timekeeping code itself.
357 *
Peter Zijlstra6695b922015-05-27 11:09:36 +0930358 * Employ the latch technique; see @raw_write_seqcount_latch.
Thomas Gleixner4396e052014-07-16 21:05:23 +0000359 *
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 Zijlstra7fc26322015-05-27 11:09:36 +0930421 seq = raw_read_seqcount_latch(&tkf->seq);
Peter Zijlstra4498e742015-03-19 09:36:19 +0100422 tkr = tkf->base + (seq & 0x01);
John Stultz27727df2016-08-23 16:08:21 -0700423 now = ktime_to_ns(tkr->base);
424
John Stultz58bfea92016-10-04 19:55:48 -0700425 now += timekeeping_delta_to_ns(tkr,
426 clocksource_delta(
John Stultz02a37cc2017-06-08 16:44:20 -0700427 tk_clock_read(tkr),
John Stultz58bfea92016-10-04 19:55:48 -0700428 tkr->cycle_last,
429 tkr->mask));
Peter Zijlstra4498e742015-03-19 09:36:19 +0100430 } while (read_seqcount_retry(&tkf->seq, seq));
Thomas Gleixner4396e052014-07-16 21:05:23 +0000431
Thomas Gleixner4396e052014-07-16 21:05:23 +0000432 return now;
433}
Peter Zijlstra4498e742015-03-19 09:36:19 +0100434
435u64 ktime_get_mono_fast_ns(void)
436{
437 return __ktime_get_fast_ns(&tk_fast_mono);
438}
Thomas Gleixner4396e052014-07-16 21:05:23 +0000439EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
440
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100441u64 ktime_get_raw_fast_ns(void)
442{
443 return __ktime_get_fast_ns(&tk_fast_raw);
444}
445EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns);
446
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100447/* Suspend-time cycles value for halted fast timekeeper. */
448static cycle_t cycles_at_suspend;
449
450static cycle_t dummy_clock_read(struct clocksource *cs)
451{
452 return cycles_at_suspend;
453}
454
John Stultz02a37cc2017-06-08 16:44:20 -0700455static struct clocksource dummy_clock = {
456 .read = dummy_clock_read,
457};
458
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100459/**
460 * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource.
461 * @tk: Timekeeper to snapshot.
462 *
463 * It generally is unsafe to access the clocksource after timekeeping has been
464 * suspended, so take a snapshot of the readout base of @tk and use it as the
465 * fast timekeeper's readout base while suspended. It will return the same
466 * number of cycles every time until timekeeping is resumed at which time the
467 * proper readout base for the fast timekeeper will be restored automatically.
468 */
469static void halt_fast_timekeeper(struct timekeeper *tk)
470{
471 static struct tk_read_base tkr_dummy;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100472 struct tk_read_base *tkr = &tk->tkr_mono;
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100473
474 memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
John Stultz02a37cc2017-06-08 16:44:20 -0700475 cycles_at_suspend = tk_clock_read(tkr);
476 tkr_dummy.clock = &dummy_clock;
Peter Zijlstra4498e742015-03-19 09:36:19 +0100477 update_fast_timekeeper(&tkr_dummy, &tk_fast_mono);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100478
479 tkr = &tk->tkr_raw;
480 memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
John Stultz02a37cc2017-06-08 16:44:20 -0700481 tkr_dummy.clock = &dummy_clock;
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100482 update_fast_timekeeper(&tkr_dummy, &tk_fast_raw);
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100483}
484
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000485#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
486
487static inline void update_vsyscall(struct timekeeper *tk)
488{
John Stultz0680eb12014-08-13 12:47:14 -0700489 struct timespec xt, wm;
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000490
John Stultze2dff1e2014-07-23 14:35:39 -0700491 xt = timespec64_to_timespec(tk_xtime(tk));
John Stultz0680eb12014-08-13 12:47:14 -0700492 wm = timespec64_to_timespec(tk->wall_to_monotonic);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100493 update_vsyscall_old(&xt, &wm, tk->tkr_mono.clock, tk->tkr_mono.mult,
494 tk->tkr_mono.cycle_last);
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000495}
496
497static inline void old_vsyscall_fixup(struct timekeeper *tk)
498{
499 s64 remainder;
500
501 /*
502 * Store only full nanoseconds into xtime_nsec after rounding
503 * it up and add the remainder to the error difference.
504 * XXX - This is necessary to avoid small 1ns inconsistnecies caused
505 * by truncating the remainder in vsyscalls. However, it causes
506 * additional work to be done in timekeeping_adjust(). Once
507 * the vsyscall implementations are converted to use xtime_nsec
508 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
509 * users are removed, this can be killed.
510 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100511 remainder = tk->tkr_mono.xtime_nsec & ((1ULL << tk->tkr_mono.shift) - 1);
Thomas Graziadei0209b932016-05-31 15:06:06 +0200512 if (remainder != 0) {
513 tk->tkr_mono.xtime_nsec -= remainder;
514 tk->tkr_mono.xtime_nsec += 1ULL << tk->tkr_mono.shift;
515 tk->ntp_error += remainder << tk->ntp_error_shift;
516 tk->ntp_error -= (1ULL << tk->tkr_mono.shift) << tk->ntp_error_shift;
517 }
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000518}
519#else
520#define old_vsyscall_fixup(tk)
521#endif
522
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200523static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
524
David Vrabel780427f2013-06-27 11:35:46 +0100525static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200526{
David Vrabel780427f2013-06-27 11:35:46 +0100527 raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200528}
529
530/**
531 * pvclock_gtod_register_notifier - register a pvclock timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200532 */
533int pvclock_gtod_register_notifier(struct notifier_block *nb)
534{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000535 struct timekeeper *tk = &tk_core.timekeeper;
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200536 unsigned long flags;
537 int ret;
538
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000539 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200540 ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
David Vrabel780427f2013-06-27 11:35:46 +0100541 update_pvclock_gtod(tk, true);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000542 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200543
544 return ret;
545}
546EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
547
548/**
549 * pvclock_gtod_unregister_notifier - unregister a pvclock
550 * timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200551 */
552int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
553{
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200554 unsigned long flags;
555 int ret;
556
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000557 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200558 ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000559 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200560
561 return ret;
562}
563EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
564
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000565/*
John Stultz833f32d2015-06-11 15:54:55 -0700566 * tk_update_leap_state - helper to update the next_leap_ktime
567 */
568static inline void tk_update_leap_state(struct timekeeper *tk)
569{
570 tk->next_leap_ktime = ntp_get_next_leap();
571 if (tk->next_leap_ktime.tv64 != KTIME_MAX)
572 /* Convert to monotonic time */
573 tk->next_leap_ktime = ktime_sub(tk->next_leap_ktime, tk->offs_real);
574}
575
576/*
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000577 * Update the ktime_t based scalar nsec members of the timekeeper
578 */
579static inline void tk_update_ktime_data(struct timekeeper *tk)
580{
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530581 u64 seconds;
582 u32 nsec;
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000583
584 /*
585 * The xtime based monotonic readout is:
586 * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();
587 * The ktime based monotonic readout is:
588 * nsec = base_mono + now();
589 * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
590 */
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530591 seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
592 nsec = (u32) tk->wall_to_monotonic.tv_nsec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100593 tk->tkr_mono.base = ns_to_ktime(seconds * NSEC_PER_SEC + nsec);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000594
595 /* Update the monotonic raw base */
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100596 tk->tkr_raw.base = timespec64_to_ktime(tk->raw_time);
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530597
598 /*
599 * The sum of the nanoseconds portions of xtime and
600 * wall_to_monotonic can be greater/equal one second. Take
601 * this into account before updating tk->ktime_sec.
602 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100603 nsec += (u32)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530604 if (nsec >= NSEC_PER_SEC)
605 seconds++;
606 tk->ktime_sec = seconds;
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000607}
608
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000609/* must hold timekeeper_lock */
David Vrabel04397fe92013-06-27 11:35:45 +0100610static void timekeeping_update(struct timekeeper *tk, unsigned int action)
Thomas Gleixnercc062682011-11-13 23:19:49 +0000611{
David Vrabel04397fe92013-06-27 11:35:45 +0100612 if (action & TK_CLEAR_NTP) {
John Stultzf726a692012-07-13 01:21:57 -0400613 tk->ntp_error = 0;
Thomas Gleixnercc062682011-11-13 23:19:49 +0000614 ntp_clear();
615 }
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000616
John Stultz833f32d2015-06-11 15:54:55 -0700617 tk_update_leap_state(tk);
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000618 tk_update_ktime_data(tk);
619
Thomas Gleixner9bf24192014-09-06 12:24:49 +0200620 update_vsyscall(tk);
621 update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
622
Peter Zijlstra4498e742015-03-19 09:36:19 +0100623 update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100624 update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw);
Thomas Gleixner868a3e92015-04-14 21:08:37 +0000625
626 if (action & TK_CLOCK_WAS_SET)
627 tk->clock_was_set_seq++;
John Stultzd1518322015-06-11 15:54:53 -0700628 /*
629 * The mirroring of the data to the shadow-timekeeper needs
630 * to happen last here to ensure we don't over-write the
631 * timekeeper structure on the next update with stale data
632 */
633 if (action & TK_MIRROR)
634 memcpy(&shadow_timekeeper, &tk_core.timekeeper,
635 sizeof(tk_core.timekeeper));
Thomas Gleixnercc062682011-11-13 23:19:49 +0000636}
637
john stultz85240702007-05-08 00:27:59 -0700638/**
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200639 * timekeeping_forward_now - update clock to the current time
john stultz85240702007-05-08 00:27:59 -0700640 *
Roman Zippel9a055112008-08-20 16:37:28 -0700641 * Forward the current clock to update its state since the last call to
642 * update_wall_time(). This is useful before significant clock changes,
643 * as it avoids having to deal with this time offset explicitly.
john stultz85240702007-05-08 00:27:59 -0700644 */
John Stultzf726a692012-07-13 01:21:57 -0400645static void timekeeping_forward_now(struct timekeeper *tk)
john stultz85240702007-05-08 00:27:59 -0700646{
Thomas Gleixner3a978372014-07-16 21:05:10 +0000647 cycle_t cycle_now, delta;
Roman Zippel9a055112008-08-20 16:37:28 -0700648 s64 nsec;
john stultz85240702007-05-08 00:27:59 -0700649
John Stultz02a37cc2017-06-08 16:44:20 -0700650 cycle_now = tk_clock_read(&tk->tkr_mono);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100651 delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
652 tk->tkr_mono.cycle_last = cycle_now;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100653 tk->tkr_raw.cycle_last = cycle_now;
john stultz85240702007-05-08 00:27:59 -0700654
Peter Zijlstra876e7882015-03-19 10:09:06 +0100655 tk->tkr_mono.xtime_nsec += delta * tk->tkr_mono.mult;
john stultz7d275582009-05-01 13:10:26 -0700656
Stephen Warren7b1f6202012-11-07 17:58:54 -0700657 /* If arch requires, add in get_arch_timeoffset() */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100658 tk->tkr_mono.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_mono.shift;
john stultz7d275582009-05-01 13:10:26 -0700659
John Stultzf726a692012-07-13 01:21:57 -0400660 tk_normalize_xtime(tk);
John Stultz2d422442008-08-20 16:37:30 -0700661
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100662 nsec = clocksource_cyc2ns(delta, tk->tkr_raw.mult, tk->tkr_raw.shift);
John Stultz7d489d12014-07-16 21:04:01 +0000663 timespec64_add_ns(&tk->raw_time, nsec);
john stultz85240702007-05-08 00:27:59 -0700664}
665
666/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000667 * __getnstimeofday64 - Returns the time of day in a timespec64.
john stultz85240702007-05-08 00:27:59 -0700668 * @ts: pointer to the timespec to be set
669 *
Kees Cook1e817fb2012-11-19 10:26:16 -0800670 * Updates the time of day in the timespec.
671 * Returns 0 on success, or -ve when suspended (timespec will be undefined).
john stultz85240702007-05-08 00:27:59 -0700672 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000673int __getnstimeofday64(struct timespec64 *ts)
john stultz85240702007-05-08 00:27:59 -0700674{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000675 struct timekeeper *tk = &tk_core.timekeeper;
john stultz85240702007-05-08 00:27:59 -0700676 unsigned long seq;
John Stultz1e75fa82012-07-13 01:21:53 -0400677 s64 nsecs = 0;
john stultz85240702007-05-08 00:27:59 -0700678
679 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000680 seq = read_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -0700681
John Stultz4e250fd2012-07-27 14:48:13 -0400682 ts->tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100683 nsecs = timekeeping_get_ns(&tk->tkr_mono);
john stultz85240702007-05-08 00:27:59 -0700684
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000685 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz85240702007-05-08 00:27:59 -0700686
John Stultzec145ba2012-09-11 19:26:03 -0400687 ts->tv_nsec = 0;
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000688 timespec64_add_ns(ts, nsecs);
Kees Cook1e817fb2012-11-19 10:26:16 -0800689
690 /*
691 * Do not bail out early, in case there were callers still using
692 * the value, even in the face of the WARN_ON.
693 */
694 if (unlikely(timekeeping_suspended))
695 return -EAGAIN;
696 return 0;
697}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000698EXPORT_SYMBOL(__getnstimeofday64);
Kees Cook1e817fb2012-11-19 10:26:16 -0800699
700/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000701 * getnstimeofday64 - Returns the time of day in a timespec64.
John Stultz5322e4c2014-11-07 13:13:04 -0800702 * @ts: pointer to the timespec64 to be set
Kees Cook1e817fb2012-11-19 10:26:16 -0800703 *
John Stultz5322e4c2014-11-07 13:13:04 -0800704 * Returns the time of day in a timespec64 (WARN if suspended).
Kees Cook1e817fb2012-11-19 10:26:16 -0800705 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000706void getnstimeofday64(struct timespec64 *ts)
Kees Cook1e817fb2012-11-19 10:26:16 -0800707{
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000708 WARN_ON(__getnstimeofday64(ts));
john stultz85240702007-05-08 00:27:59 -0700709}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000710EXPORT_SYMBOL(getnstimeofday64);
john stultz85240702007-05-08 00:27:59 -0700711
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200712ktime_t ktime_get(void)
713{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000714 struct timekeeper *tk = &tk_core.timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200715 unsigned int seq;
Thomas Gleixnera016a5b2014-07-16 21:04:12 +0000716 ktime_t base;
717 s64 nsecs;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200718
719 WARN_ON(timekeeping_suspended);
720
721 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000722 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100723 base = tk->tkr_mono.base;
724 nsecs = timekeeping_get_ns(&tk->tkr_mono);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200725
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000726 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz24e4a8c2014-07-16 21:03:53 +0000727
Thomas Gleixnera016a5b2014-07-16 21:04:12 +0000728 return ktime_add_ns(base, nsecs);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200729}
730EXPORT_SYMBOL_GPL(ktime_get);
731
Harald Geyer6374f912015-04-07 11:12:35 +0000732u32 ktime_get_resolution_ns(void)
733{
734 struct timekeeper *tk = &tk_core.timekeeper;
735 unsigned int seq;
736 u32 nsecs;
737
738 WARN_ON(timekeeping_suspended);
739
740 do {
741 seq = read_seqcount_begin(&tk_core.seq);
742 nsecs = tk->tkr_mono.mult >> tk->tkr_mono.shift;
743 } while (read_seqcount_retry(&tk_core.seq, seq));
744
745 return nsecs;
746}
747EXPORT_SYMBOL_GPL(ktime_get_resolution_ns);
748
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000749static ktime_t *offsets[TK_OFFS_MAX] = {
750 [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real,
751 [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot,
752 [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai,
753};
754
755ktime_t ktime_get_with_offset(enum tk_offsets offs)
756{
757 struct timekeeper *tk = &tk_core.timekeeper;
758 unsigned int seq;
759 ktime_t base, *offset = offsets[offs];
760 s64 nsecs;
761
762 WARN_ON(timekeeping_suspended);
763
764 do {
765 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100766 base = ktime_add(tk->tkr_mono.base, *offset);
767 nsecs = timekeeping_get_ns(&tk->tkr_mono);
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000768
769 } while (read_seqcount_retry(&tk_core.seq, seq));
770
771 return ktime_add_ns(base, nsecs);
772
773}
774EXPORT_SYMBOL_GPL(ktime_get_with_offset);
775
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200776/**
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000777 * ktime_mono_to_any() - convert mononotic time to any other time
778 * @tmono: time to convert.
779 * @offs: which offset to use
780 */
781ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
782{
783 ktime_t *offset = offsets[offs];
784 unsigned long seq;
785 ktime_t tconv;
786
787 do {
788 seq = read_seqcount_begin(&tk_core.seq);
789 tconv = ktime_add(tmono, *offset);
790 } while (read_seqcount_retry(&tk_core.seq, seq));
791
792 return tconv;
793}
794EXPORT_SYMBOL_GPL(ktime_mono_to_any);
795
796/**
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000797 * ktime_get_raw - Returns the raw monotonic time in ktime_t format
798 */
799ktime_t ktime_get_raw(void)
800{
801 struct timekeeper *tk = &tk_core.timekeeper;
802 unsigned int seq;
803 ktime_t base;
804 s64 nsecs;
805
806 do {
807 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100808 base = tk->tkr_raw.base;
809 nsecs = timekeeping_get_ns(&tk->tkr_raw);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000810
811 } while (read_seqcount_retry(&tk_core.seq, seq));
812
813 return ktime_add_ns(base, nsecs);
814}
815EXPORT_SYMBOL_GPL(ktime_get_raw);
816
817/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000818 * ktime_get_ts64 - get the monotonic clock in timespec64 format
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200819 * @ts: pointer to timespec variable
820 *
821 * The function calculates the monotonic clock from the realtime
822 * clock and the wall_to_monotonic offset and stores the result
John Stultz5322e4c2014-11-07 13:13:04 -0800823 * in normalized timespec64 format in the variable pointed to by @ts.
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200824 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000825void ktime_get_ts64(struct timespec64 *ts)
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200826{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000827 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000828 struct timespec64 tomono;
John Stultzec145ba2012-09-11 19:26:03 -0400829 s64 nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200830 unsigned int seq;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200831
832 WARN_ON(timekeeping_suspended);
833
834 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000835 seq = read_seqcount_begin(&tk_core.seq);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000836 ts->tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100837 nsec = timekeeping_get_ns(&tk->tkr_mono);
John Stultz4e250fd2012-07-27 14:48:13 -0400838 tomono = tk->wall_to_monotonic;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200839
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000840 } while (read_seqcount_retry(&tk_core.seq, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200841
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000842 ts->tv_sec += tomono.tv_sec;
843 ts->tv_nsec = 0;
844 timespec64_add_ns(ts, nsec + tomono.tv_nsec);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200845}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000846EXPORT_SYMBOL_GPL(ktime_get_ts64);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200847
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530848/**
849 * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC
850 *
851 * Returns the seconds portion of CLOCK_MONOTONIC with a single non
852 * serialized read. tk->ktime_sec is of type 'unsigned long' so this
853 * works on both 32 and 64 bit systems. On 32 bit systems the readout
854 * covers ~136 years of uptime which should be enough to prevent
855 * premature wrap arounds.
856 */
857time64_t ktime_get_seconds(void)
858{
859 struct timekeeper *tk = &tk_core.timekeeper;
860
861 WARN_ON(timekeeping_suspended);
862 return tk->ktime_sec;
863}
864EXPORT_SYMBOL_GPL(ktime_get_seconds);
865
Heena Sirwanidbe7aa62014-10-29 16:01:50 +0530866/**
867 * ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME
868 *
869 * Returns the wall clock seconds since 1970. This replaces the
870 * get_seconds() interface which is not y2038 safe on 32bit systems.
871 *
872 * For 64bit systems the fast access to tk->xtime_sec is preserved. On
873 * 32bit systems the access must be protected with the sequence
874 * counter to provide "atomic" access to the 64bit tk->xtime_sec
875 * value.
876 */
877time64_t ktime_get_real_seconds(void)
878{
879 struct timekeeper *tk = &tk_core.timekeeper;
880 time64_t seconds;
881 unsigned int seq;
882
883 if (IS_ENABLED(CONFIG_64BIT))
884 return tk->xtime_sec;
885
886 do {
887 seq = read_seqcount_begin(&tk_core.seq);
888 seconds = tk->xtime_sec;
889
890 } while (read_seqcount_retry(&tk_core.seq, seq));
891
892 return seconds;
893}
894EXPORT_SYMBOL_GPL(ktime_get_real_seconds);
895
DengChaodee36652015-12-13 12:24:18 +0800896/**
897 * __ktime_get_real_seconds - The same as ktime_get_real_seconds
898 * but without the sequence counter protect. This internal function
899 * is called just when timekeeping lock is already held.
900 */
901time64_t __ktime_get_real_seconds(void)
902{
903 struct timekeeper *tk = &tk_core.timekeeper;
904
905 return tk->xtime_sec;
906}
907
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800908/**
909 * ktime_get_snapshot - snapshots the realtime/monotonic raw clocks with counter
910 * @systime_snapshot: pointer to struct receiving the system time snapshot
911 */
912void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
913{
914 struct timekeeper *tk = &tk_core.timekeeper;
915 unsigned long seq;
916 ktime_t base_raw;
917 ktime_t base_real;
918 s64 nsec_raw;
919 s64 nsec_real;
920 cycle_t now;
921
Christopher S. Hallba266212016-02-22 03:15:21 -0800922 WARN_ON_ONCE(timekeeping_suspended);
923
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800924 do {
925 seq = read_seqcount_begin(&tk_core.seq);
John Stultz02a37cc2017-06-08 16:44:20 -0700926 now = tk_clock_read(&tk->tkr_mono);
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800927 systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq;
928 systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq;
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800929 base_real = ktime_add(tk->tkr_mono.base,
930 tk_core.timekeeper.offs_real);
931 base_raw = tk->tkr_raw.base;
932 nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, now);
933 nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw, now);
934 } while (read_seqcount_retry(&tk_core.seq, seq));
935
936 systime_snapshot->cycles = now;
937 systime_snapshot->real = ktime_add_ns(base_real, nsec_real);
938 systime_snapshot->raw = ktime_add_ns(base_raw, nsec_raw);
939}
940EXPORT_SYMBOL_GPL(ktime_get_snapshot);
DengChaodee36652015-12-13 12:24:18 +0800941
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800942/* Scale base by mult/div checking for overflow */
943static int scale64_check_overflow(u64 mult, u64 div, u64 *base)
944{
945 u64 tmp, rem;
946
947 tmp = div64_u64_rem(*base, div, &rem);
948
949 if (((int)sizeof(u64)*8 - fls64(mult) < fls64(tmp)) ||
950 ((int)sizeof(u64)*8 - fls64(mult) < fls64(rem)))
951 return -EOVERFLOW;
952 tmp *= mult;
953 rem *= mult;
954
955 do_div(rem, div);
956 *base = tmp + rem;
957 return 0;
958}
959
960/**
961 * adjust_historical_crosststamp - adjust crosstimestamp previous to current interval
962 * @history: Snapshot representing start of history
963 * @partial_history_cycles: Cycle offset into history (fractional part)
964 * @total_history_cycles: Total history length in cycles
965 * @discontinuity: True indicates clock was set on history period
966 * @ts: Cross timestamp that should be adjusted using
967 * partial/total ratio
968 *
969 * Helper function used by get_device_system_crosststamp() to correct the
970 * crosstimestamp corresponding to the start of the current interval to the
971 * system counter value (timestamp point) provided by the driver. The
972 * total_history_* quantities are the total history starting at the provided
973 * reference point and ending at the start of the current interval. The cycle
974 * count between the driver timestamp point and the start of the current
975 * interval is partial_history_cycles.
976 */
977static int adjust_historical_crosststamp(struct system_time_snapshot *history,
978 cycle_t partial_history_cycles,
979 cycle_t total_history_cycles,
980 bool discontinuity,
981 struct system_device_crosststamp *ts)
982{
983 struct timekeeper *tk = &tk_core.timekeeper;
984 u64 corr_raw, corr_real;
985 bool interp_forward;
986 int ret;
987
988 if (total_history_cycles == 0 || partial_history_cycles == 0)
989 return 0;
990
991 /* Interpolate shortest distance from beginning or end of history */
992 interp_forward = partial_history_cycles > total_history_cycles/2 ?
993 true : false;
994 partial_history_cycles = interp_forward ?
995 total_history_cycles - partial_history_cycles :
996 partial_history_cycles;
997
998 /*
999 * Scale the monotonic raw time delta by:
1000 * partial_history_cycles / total_history_cycles
1001 */
1002 corr_raw = (u64)ktime_to_ns(
1003 ktime_sub(ts->sys_monoraw, history->raw));
1004 ret = scale64_check_overflow(partial_history_cycles,
1005 total_history_cycles, &corr_raw);
1006 if (ret)
1007 return ret;
1008
1009 /*
1010 * If there is a discontinuity in the history, scale monotonic raw
1011 * correction by:
1012 * mult(real)/mult(raw) yielding the realtime correction
1013 * Otherwise, calculate the realtime correction similar to monotonic
1014 * raw calculation
1015 */
1016 if (discontinuity) {
1017 corr_real = mul_u64_u32_div
1018 (corr_raw, tk->tkr_mono.mult, tk->tkr_raw.mult);
1019 } else {
1020 corr_real = (u64)ktime_to_ns(
1021 ktime_sub(ts->sys_realtime, history->real));
1022 ret = scale64_check_overflow(partial_history_cycles,
1023 total_history_cycles, &corr_real);
1024 if (ret)
1025 return ret;
1026 }
1027
1028 /* Fixup monotonic raw and real time time values */
1029 if (interp_forward) {
1030 ts->sys_monoraw = ktime_add_ns(history->raw, corr_raw);
1031 ts->sys_realtime = ktime_add_ns(history->real, corr_real);
1032 } else {
1033 ts->sys_monoraw = ktime_sub_ns(ts->sys_monoraw, corr_raw);
1034 ts->sys_realtime = ktime_sub_ns(ts->sys_realtime, corr_real);
1035 }
1036
1037 return 0;
1038}
1039
1040/*
1041 * cycle_between - true if test occurs chronologically between before and after
1042 */
1043static bool cycle_between(cycle_t before, cycle_t test, cycle_t after)
1044{
1045 if (test > before && test < after)
1046 return true;
1047 if (test < before && before > after)
1048 return true;
1049 return false;
1050}
1051
john stultz85240702007-05-08 00:27:59 -07001052/**
Christopher S. Hall8006c242016-02-22 03:15:22 -08001053 * get_device_system_crosststamp - Synchronously capture system/device timestamp
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001054 * @get_time_fn: Callback to get simultaneous device time and
Christopher S. Hall8006c242016-02-22 03:15:22 -08001055 * system counter from the device driver
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001056 * @ctx: Context passed to get_time_fn()
1057 * @history_begin: Historical reference point used to interpolate system
1058 * time when counter provided by the driver is before the current interval
Christopher S. Hall8006c242016-02-22 03:15:22 -08001059 * @xtstamp: Receives simultaneously captured system and device time
1060 *
1061 * Reads a timestamp from a device and correlates it to system time
1062 */
1063int get_device_system_crosststamp(int (*get_time_fn)
1064 (ktime_t *device_time,
1065 struct system_counterval_t *sys_counterval,
1066 void *ctx),
1067 void *ctx,
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001068 struct system_time_snapshot *history_begin,
Christopher S. Hall8006c242016-02-22 03:15:22 -08001069 struct system_device_crosststamp *xtstamp)
1070{
1071 struct system_counterval_t system_counterval;
1072 struct timekeeper *tk = &tk_core.timekeeper;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001073 cycle_t cycles, now, interval_start;
Ingo Molnar64362572016-03-08 11:09:53 +01001074 unsigned int clock_was_set_seq = 0;
Christopher S. Hall8006c242016-02-22 03:15:22 -08001075 ktime_t base_real, base_raw;
1076 s64 nsec_real, nsec_raw;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001077 u8 cs_was_changed_seq;
Christopher S. Hall8006c242016-02-22 03:15:22 -08001078 unsigned long seq;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001079 bool do_interp;
Christopher S. Hall8006c242016-02-22 03:15:22 -08001080 int ret;
1081
1082 do {
1083 seq = read_seqcount_begin(&tk_core.seq);
1084 /*
1085 * Try to synchronously capture device time and a system
1086 * counter value calling back into the device driver
1087 */
1088 ret = get_time_fn(&xtstamp->device, &system_counterval, ctx);
1089 if (ret)
1090 return ret;
1091
1092 /*
1093 * Verify that the clocksource associated with the captured
1094 * system counter value is the same as the currently installed
1095 * timekeeper clocksource
1096 */
1097 if (tk->tkr_mono.clock != system_counterval.cs)
1098 return -ENODEV;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001099 cycles = system_counterval.cycles;
1100
1101 /*
1102 * Check whether the system counter value provided by the
1103 * device driver is on the current timekeeping interval.
1104 */
John Stultz02a37cc2017-06-08 16:44:20 -07001105 now = tk_clock_read(&tk->tkr_mono);
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001106 interval_start = tk->tkr_mono.cycle_last;
1107 if (!cycle_between(interval_start, cycles, now)) {
1108 clock_was_set_seq = tk->clock_was_set_seq;
1109 cs_was_changed_seq = tk->cs_was_changed_seq;
1110 cycles = interval_start;
1111 do_interp = true;
1112 } else {
1113 do_interp = false;
1114 }
Christopher S. Hall8006c242016-02-22 03:15:22 -08001115
1116 base_real = ktime_add(tk->tkr_mono.base,
1117 tk_core.timekeeper.offs_real);
1118 base_raw = tk->tkr_raw.base;
1119
1120 nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono,
1121 system_counterval.cycles);
1122 nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw,
1123 system_counterval.cycles);
1124 } while (read_seqcount_retry(&tk_core.seq, seq));
1125
1126 xtstamp->sys_realtime = ktime_add_ns(base_real, nsec_real);
1127 xtstamp->sys_monoraw = ktime_add_ns(base_raw, nsec_raw);
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001128
1129 /*
1130 * Interpolate if necessary, adjusting back from the start of the
1131 * current interval
1132 */
1133 if (do_interp) {
1134 cycle_t partial_history_cycles, total_history_cycles;
1135 bool discontinuity;
1136
1137 /*
1138 * Check that the counter value occurs after the provided
1139 * history reference and that the history doesn't cross a
1140 * clocksource change
1141 */
1142 if (!history_begin ||
1143 !cycle_between(history_begin->cycles,
1144 system_counterval.cycles, cycles) ||
1145 history_begin->cs_was_changed_seq != cs_was_changed_seq)
1146 return -EINVAL;
1147 partial_history_cycles = cycles - system_counterval.cycles;
1148 total_history_cycles = cycles - history_begin->cycles;
1149 discontinuity =
1150 history_begin->clock_was_set_seq != clock_was_set_seq;
1151
1152 ret = adjust_historical_crosststamp(history_begin,
1153 partial_history_cycles,
1154 total_history_cycles,
1155 discontinuity, xtstamp);
1156 if (ret)
1157 return ret;
1158 }
1159
Christopher S. Hall8006c242016-02-22 03:15:22 -08001160 return 0;
1161}
1162EXPORT_SYMBOL_GPL(get_device_system_crosststamp);
1163
1164/**
john stultz85240702007-05-08 00:27:59 -07001165 * do_gettimeofday - Returns the time of day in a timeval
1166 * @tv: pointer to the timeval to be set
1167 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +01001168 * NOTE: Users should be converted to using getnstimeofday()
john stultz85240702007-05-08 00:27:59 -07001169 */
1170void do_gettimeofday(struct timeval *tv)
1171{
Thomas Gleixnerd6d29892014-07-16 21:04:04 +00001172 struct timespec64 now;
john stultz85240702007-05-08 00:27:59 -07001173
Thomas Gleixnerd6d29892014-07-16 21:04:04 +00001174 getnstimeofday64(&now);
john stultz85240702007-05-08 00:27:59 -07001175 tv->tv_sec = now.tv_sec;
1176 tv->tv_usec = now.tv_nsec/1000;
1177}
john stultz85240702007-05-08 00:27:59 -07001178EXPORT_SYMBOL(do_gettimeofday);
Richard Cochrand239f492012-04-27 10:12:42 +02001179
john stultz85240702007-05-08 00:27:59 -07001180/**
pang.xunlei21f7eca2014-11-18 19:15:16 +08001181 * do_settimeofday64 - Sets the time of day.
1182 * @ts: pointer to the timespec64 variable containing the new time
john stultz85240702007-05-08 00:27:59 -07001183 *
1184 * Sets the time of day to the new time and update NTP and notify hrtimers
1185 */
pang.xunlei21f7eca2014-11-18 19:15:16 +08001186int do_settimeofday64(const struct timespec64 *ts)
john stultz85240702007-05-08 00:27:59 -07001187{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001188 struct timekeeper *tk = &tk_core.timekeeper;
pang.xunlei21f7eca2014-11-18 19:15:16 +08001189 struct timespec64 ts_delta, xt;
John Stultz92c1d3e2011-11-14 14:05:44 -08001190 unsigned long flags;
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001191 int ret = 0;
john stultz85240702007-05-08 00:27:59 -07001192
pang.xunlei21f7eca2014-11-18 19:15:16 +08001193 if (!timespec64_valid_strict(ts))
john stultz85240702007-05-08 00:27:59 -07001194 return -EINVAL;
1195
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001196 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001197 write_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -07001198
John Stultz4e250fd2012-07-27 14:48:13 -04001199 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -07001200
John Stultz4e250fd2012-07-27 14:48:13 -04001201 xt = tk_xtime(tk);
pang.xunlei21f7eca2014-11-18 19:15:16 +08001202 ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
1203 ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
John Stultz1e75fa82012-07-13 01:21:53 -04001204
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001205 if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) {
1206 ret = -EINVAL;
1207 goto out;
1208 }
1209
John Stultz7d489d12014-07-16 21:04:01 +00001210 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
john stultz85240702007-05-08 00:27:59 -07001211
pang.xunlei21f7eca2014-11-18 19:15:16 +08001212 tk_set_xtime(tk, ts);
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001213out:
David Vrabel780427f2013-06-27 11:35:46 +01001214 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
john stultz85240702007-05-08 00:27:59 -07001215
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001216 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001217 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001218
1219 /* signal hrtimers about time change */
1220 clock_was_set();
1221
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001222 return ret;
john stultz85240702007-05-08 00:27:59 -07001223}
pang.xunlei21f7eca2014-11-18 19:15:16 +08001224EXPORT_SYMBOL(do_settimeofday64);
john stultz85240702007-05-08 00:27:59 -07001225
John Stultzc528f7c2011-02-01 13:52:17 +00001226/**
1227 * timekeeping_inject_offset - Adds or subtracts from the current time.
1228 * @tv: pointer to the timespec variable containing the offset
1229 *
1230 * Adds or subtracts an offset value from the current time.
1231 */
1232int timekeeping_inject_offset(struct timespec *ts)
1233{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001234 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -08001235 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001236 struct timespec64 ts64, tmp;
John Stultz4e8b1452012-08-08 15:36:20 -04001237 int ret = 0;
John Stultzc528f7c2011-02-01 13:52:17 +00001238
John Stultz37cf4dc2015-12-03 22:09:31 -05001239 if (!timespec_inject_offset_valid(ts))
John Stultzc528f7c2011-02-01 13:52:17 +00001240 return -EINVAL;
1241
John Stultz7d489d12014-07-16 21:04:01 +00001242 ts64 = timespec_to_timespec64(*ts);
1243
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001244 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001245 write_seqcount_begin(&tk_core.seq);
John Stultzc528f7c2011-02-01 13:52:17 +00001246
John Stultz4e250fd2012-07-27 14:48:13 -04001247 timekeeping_forward_now(tk);
John Stultzc528f7c2011-02-01 13:52:17 +00001248
John Stultz4e8b1452012-08-08 15:36:20 -04001249 /* Make sure the proposed value is valid */
John Stultz7d489d12014-07-16 21:04:01 +00001250 tmp = timespec64_add(tk_xtime(tk), ts64);
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001251 if (timespec64_compare(&tk->wall_to_monotonic, &ts64) > 0 ||
1252 !timespec64_valid_strict(&tmp)) {
John Stultz4e8b1452012-08-08 15:36:20 -04001253 ret = -EINVAL;
1254 goto error;
1255 }
John Stultz1e75fa82012-07-13 01:21:53 -04001256
John Stultz7d489d12014-07-16 21:04:01 +00001257 tk_xtime_add(tk, &ts64);
1258 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts64));
John Stultzc528f7c2011-02-01 13:52:17 +00001259
John Stultz4e8b1452012-08-08 15:36:20 -04001260error: /* even if we error out, we forwarded the time, so call update */
David Vrabel780427f2013-06-27 11:35:46 +01001261 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultzc528f7c2011-02-01 13:52:17 +00001262
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001263 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001264 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzc528f7c2011-02-01 13:52:17 +00001265
1266 /* signal hrtimers about time change */
1267 clock_was_set();
1268
John Stultz4e8b1452012-08-08 15:36:20 -04001269 return ret;
John Stultzc528f7c2011-02-01 13:52:17 +00001270}
1271EXPORT_SYMBOL(timekeeping_inject_offset);
1272
John Stultzcc244dd2012-05-03 12:30:07 -07001273
1274/**
1275 * timekeeping_get_tai_offset - Returns current TAI offset from UTC
1276 *
1277 */
1278s32 timekeeping_get_tai_offset(void)
1279{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001280 struct timekeeper *tk = &tk_core.timekeeper;
John Stultzcc244dd2012-05-03 12:30:07 -07001281 unsigned int seq;
1282 s32 ret;
1283
1284 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001285 seq = read_seqcount_begin(&tk_core.seq);
John Stultzcc244dd2012-05-03 12:30:07 -07001286 ret = tk->tai_offset;
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001287 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultzcc244dd2012-05-03 12:30:07 -07001288
1289 return ret;
1290}
1291
1292/**
1293 * __timekeeping_set_tai_offset - Lock free worker function
1294 *
1295 */
Fengguang Wudd5d70e2013-03-25 12:24:24 -07001296static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
John Stultzcc244dd2012-05-03 12:30:07 -07001297{
1298 tk->tai_offset = tai_offset;
John Stultz04005f62013-12-10 17:13:35 -08001299 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
John Stultzcc244dd2012-05-03 12:30:07 -07001300}
1301
1302/**
1303 * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
1304 *
1305 */
1306void timekeeping_set_tai_offset(s32 tai_offset)
1307{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001308 struct timekeeper *tk = &tk_core.timekeeper;
John Stultzcc244dd2012-05-03 12:30:07 -07001309 unsigned long flags;
1310
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001311 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001312 write_seqcount_begin(&tk_core.seq);
John Stultzcc244dd2012-05-03 12:30:07 -07001313 __timekeeping_set_tai_offset(tk, tai_offset);
John Stultzf55c0762013-12-11 18:50:25 -08001314 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001315 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001316 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz4e8f8b32013-04-10 12:41:49 -07001317 clock_was_set();
John Stultzcc244dd2012-05-03 12:30:07 -07001318}
1319
john stultz85240702007-05-08 00:27:59 -07001320/**
1321 * change_clocksource - Swaps clocksources if a new one is available
1322 *
1323 * Accumulates current time interval and initializes new clocksource
1324 */
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001325static int change_clocksource(void *data)
john stultz85240702007-05-08 00:27:59 -07001326{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001327 struct timekeeper *tk = &tk_core.timekeeper;
Magnus Damm4614e6a2009-04-21 12:24:02 -07001328 struct clocksource *new, *old;
John Stultzf695cf92012-03-14 16:38:15 -07001329 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -07001330
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001331 new = (struct clocksource *) data;
john stultz85240702007-05-08 00:27:59 -07001332
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001333 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001334 write_seqcount_begin(&tk_core.seq);
John Stultzf695cf92012-03-14 16:38:15 -07001335
John Stultz4e250fd2012-07-27 14:48:13 -04001336 timekeeping_forward_now(tk);
Thomas Gleixner09ac3692013-04-25 20:31:44 +00001337 /*
1338 * If the cs is in module, get a module reference. Succeeds
1339 * for built-in code (owner == NULL) as well.
1340 */
1341 if (try_module_get(new->owner)) {
1342 if (!new->enable || new->enable(new) == 0) {
Peter Zijlstra876e7882015-03-19 10:09:06 +01001343 old = tk->tkr_mono.clock;
Thomas Gleixner09ac3692013-04-25 20:31:44 +00001344 tk_setup_internals(tk, new);
1345 if (old->disable)
1346 old->disable(old);
1347 module_put(old->owner);
1348 } else {
1349 module_put(new->owner);
1350 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001351 }
David Vrabel780427f2013-06-27 11:35:46 +01001352 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultzf695cf92012-03-14 16:38:15 -07001353
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001354 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001355 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzf695cf92012-03-14 16:38:15 -07001356
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001357 return 0;
1358}
john stultz85240702007-05-08 00:27:59 -07001359
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001360/**
1361 * timekeeping_notify - Install a new clock source
1362 * @clock: pointer to the clock source
1363 *
1364 * This function is called from clocksource.c after a new, better clock
1365 * source has been registered. The caller holds the clocksource_mutex.
1366 */
Thomas Gleixnerba919d12013-04-25 20:31:44 +00001367int timekeeping_notify(struct clocksource *clock)
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001368{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001369 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04001370
Peter Zijlstra876e7882015-03-19 10:09:06 +01001371 if (tk->tkr_mono.clock == clock)
Thomas Gleixnerba919d12013-04-25 20:31:44 +00001372 return 0;
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001373 stop_machine(change_clocksource, clock, NULL);
john stultz85240702007-05-08 00:27:59 -07001374 tick_clock_notify();
Peter Zijlstra876e7882015-03-19 10:09:06 +01001375 return tk->tkr_mono.clock == clock ? 0 : -1;
john stultz85240702007-05-08 00:27:59 -07001376}
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001377
Thomas Gleixnera40f2622009-07-07 13:00:31 +02001378/**
John Stultzcdba2ec2014-11-07 11:03:20 -08001379 * getrawmonotonic64 - Returns the raw monotonic time in a timespec
1380 * @ts: pointer to the timespec64 to be set
John Stultz2d422442008-08-20 16:37:30 -07001381 *
1382 * Returns the raw monotonic time (completely un-modified by ntp)
1383 */
John Stultzcdba2ec2014-11-07 11:03:20 -08001384void getrawmonotonic64(struct timespec64 *ts)
John Stultz2d422442008-08-20 16:37:30 -07001385{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001386 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz7d489d12014-07-16 21:04:01 +00001387 struct timespec64 ts64;
John Stultz2d422442008-08-20 16:37:30 -07001388 unsigned long seq;
1389 s64 nsecs;
John Stultz2d422442008-08-20 16:37:30 -07001390
1391 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001392 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01001393 nsecs = timekeeping_get_ns(&tk->tkr_raw);
John Stultz7d489d12014-07-16 21:04:01 +00001394 ts64 = tk->raw_time;
John Stultz2d422442008-08-20 16:37:30 -07001395
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001396 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz2d422442008-08-20 16:37:30 -07001397
John Stultz7d489d12014-07-16 21:04:01 +00001398 timespec64_add_ns(&ts64, nsecs);
John Stultzcdba2ec2014-11-07 11:03:20 -08001399 *ts = ts64;
John Stultz2d422442008-08-20 16:37:30 -07001400}
John Stultzcdba2ec2014-11-07 11:03:20 -08001401EXPORT_SYMBOL(getrawmonotonic64);
1402
John Stultz2d422442008-08-20 16:37:30 -07001403
John Stultz2d422442008-08-20 16:37:30 -07001404/**
Li Zefancf4fc6c2008-02-08 04:19:24 -08001405 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -07001406 */
Li Zefancf4fc6c2008-02-08 04:19:24 -08001407int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -07001408{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001409 struct timekeeper *tk = &tk_core.timekeeper;
john stultz85240702007-05-08 00:27:59 -07001410 unsigned long seq;
1411 int ret;
1412
1413 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001414 seq = read_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -07001415
Peter Zijlstra876e7882015-03-19 10:09:06 +01001416 ret = tk->tkr_mono.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -07001417
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001418 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz85240702007-05-08 00:27:59 -07001419
1420 return ret;
1421}
1422
1423/**
Jon Hunter98962462009-08-18 12:45:10 -05001424 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
Jon Hunter98962462009-08-18 12:45:10 -05001425 */
1426u64 timekeeping_max_deferment(void)
1427{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001428 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz70471f22011-11-14 12:48:10 -08001429 unsigned long seq;
1430 u64 ret;
John Stultz42e71e82012-07-13 01:21:51 -04001431
John Stultz70471f22011-11-14 12:48:10 -08001432 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001433 seq = read_seqcount_begin(&tk_core.seq);
John Stultz70471f22011-11-14 12:48:10 -08001434
Peter Zijlstra876e7882015-03-19 10:09:06 +01001435 ret = tk->tkr_mono.clock->max_idle_ns;
John Stultz70471f22011-11-14 12:48:10 -08001436
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001437 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz70471f22011-11-14 12:48:10 -08001438
1439 return ret;
Jon Hunter98962462009-08-18 12:45:10 -05001440}
1441
1442/**
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001443 * read_persistent_clock - Return time from the persistent clock.
john stultz85240702007-05-08 00:27:59 -07001444 *
1445 * Weak dummy function for arches that do not yet support it.
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001446 * Reads the time from the battery backed persistent clock.
1447 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
john stultz85240702007-05-08 00:27:59 -07001448 *
1449 * XXX - Do be sure to remove it once all arches implement it.
1450 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001451void __weak read_persistent_clock(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -07001452{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001453 ts->tv_sec = 0;
1454 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -07001455}
1456
Xunlei Pang2ee96632015-04-01 20:34:22 -07001457void __weak read_persistent_clock64(struct timespec64 *ts64)
1458{
1459 struct timespec ts;
1460
1461 read_persistent_clock(&ts);
1462 *ts64 = timespec_to_timespec64(ts);
1463}
1464
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001465/**
Xunlei Pange83d0a42015-04-09 09:04:42 +08001466 * read_boot_clock64 - Return time of the system start.
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001467 *
1468 * Weak dummy function for arches that do not yet support it.
1469 * Function to read the exact time the system has been started.
Xunlei Pange83d0a42015-04-09 09:04:42 +08001470 * Returns a timespec64 with tv_sec=0 and tv_nsec=0 if unsupported.
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001471 *
1472 * XXX - Do be sure to remove it once all arches implement it.
1473 */
Xunlei Pange83d0a42015-04-09 09:04:42 +08001474void __weak read_boot_clock64(struct timespec64 *ts)
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001475{
1476 ts->tv_sec = 0;
1477 ts->tv_nsec = 0;
1478}
1479
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001480/* Flag for if timekeeping_resume() has injected sleeptime */
1481static bool sleeptime_injected;
1482
1483/* Flag for if there is a persistent clock on this platform */
1484static bool persistent_clock_exists;
1485
john stultz85240702007-05-08 00:27:59 -07001486/*
1487 * timekeeping_init - Initializes the clocksource and common timekeeping values
1488 */
1489void __init timekeeping_init(void)
1490{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001491 struct timekeeper *tk = &tk_core.timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +02001492 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -07001493 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001494 struct timespec64 now, boot, tmp;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001495
Xunlei Pang2ee96632015-04-01 20:34:22 -07001496 read_persistent_clock64(&now);
John Stultz7d489d12014-07-16 21:04:01 +00001497 if (!timespec64_valid_strict(&now)) {
John Stultz4e8b1452012-08-08 15:36:20 -04001498 pr_warn("WARNING: Persistent clock returned invalid value!\n"
1499 " Check your CMOS/BIOS settings.\n");
1500 now.tv_sec = 0;
1501 now.tv_nsec = 0;
Feng Tang31ade302013-01-16 00:09:47 +08001502 } else if (now.tv_sec || now.tv_nsec)
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001503 persistent_clock_exists = true;
John Stultz4e8b1452012-08-08 15:36:20 -04001504
Xunlei Pang9a806dd2015-04-01 20:34:21 -07001505 read_boot_clock64(&boot);
John Stultz7d489d12014-07-16 21:04:01 +00001506 if (!timespec64_valid_strict(&boot)) {
John Stultz4e8b1452012-08-08 15:36:20 -04001507 pr_warn("WARNING: Boot clock returned invalid value!\n"
1508 " Check your CMOS/BIOS settings.\n");
1509 boot.tv_sec = 0;
1510 boot.tv_nsec = 0;
1511 }
john stultz85240702007-05-08 00:27:59 -07001512
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001513 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001514 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07001515 ntp_init();
1516
Martin Schwidefskyf1b82742009-08-14 15:47:21 +02001517 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +02001518 if (clock->enable)
1519 clock->enable(clock);
John Stultz4e250fd2012-07-27 14:48:13 -04001520 tk_setup_internals(tk, clock);
john stultz85240702007-05-08 00:27:59 -07001521
John Stultz4e250fd2012-07-27 14:48:13 -04001522 tk_set_xtime(tk, &now);
1523 tk->raw_time.tv_sec = 0;
1524 tk->raw_time.tv_nsec = 0;
John Stultz1e75fa82012-07-13 01:21:53 -04001525 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
John Stultz4e250fd2012-07-27 14:48:13 -04001526 boot = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -04001527
John Stultz7d489d12014-07-16 21:04:01 +00001528 set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec);
John Stultz4e250fd2012-07-27 14:48:13 -04001529 tk_set_wall_to_mono(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -04001530
Thomas Gleixner56fd16c2015-10-16 15:50:22 +02001531 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001532
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001533 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001534 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001535}
1536
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001537/* time in seconds when suspend began for persistent clock */
John Stultz7d489d12014-07-16 21:04:01 +00001538static struct timespec64 timekeeping_suspend_time;
john stultz85240702007-05-08 00:27:59 -07001539
1540/**
John Stultz304529b2011-04-01 14:32:09 -07001541 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
1542 * @delta: pointer to a timespec delta value
1543 *
1544 * Takes a timespec offset measuring a suspend interval and properly
1545 * adds the sleep offset to the timekeeping variables.
1546 */
John Stultzf726a692012-07-13 01:21:57 -04001547static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
John Stultz7d489d12014-07-16 21:04:01 +00001548 struct timespec64 *delta)
John Stultz304529b2011-04-01 14:32:09 -07001549{
John Stultz7d489d12014-07-16 21:04:01 +00001550 if (!timespec64_valid_strict(delta)) {
John Stultz6d9bcb62014-06-04 16:11:43 -07001551 printk_deferred(KERN_WARNING
1552 "__timekeeping_inject_sleeptime: Invalid "
1553 "sleep delta value!\n");
John Stultzcb5de2f8d2011-06-01 18:18:09 -07001554 return;
1555 }
John Stultzf726a692012-07-13 01:21:57 -04001556 tk_xtime_add(tk, delta);
John Stultz7d489d12014-07-16 21:04:01 +00001557 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
Thomas Gleixner47da70d2014-07-16 21:05:00 +00001558 tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
Colin Cross5c835452013-05-21 22:32:14 -07001559 tk_debug_account_sleep_time(delta);
John Stultz304529b2011-04-01 14:32:09 -07001560}
1561
Xunlei Pang7f298132015-04-01 20:34:35 -07001562#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
John Stultz304529b2011-04-01 14:32:09 -07001563/**
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001564 * We have three kinds of time sources to use for sleep time
1565 * injection, the preference order is:
1566 * 1) non-stop clocksource
1567 * 2) persistent clock (ie: RTC accessible when irqs are off)
1568 * 3) RTC
1569 *
1570 * 1) and 2) are used by timekeeping, 3) by RTC subsystem.
1571 * If system has neither 1) nor 2), 3) will be used finally.
1572 *
1573 *
1574 * If timekeeping has injected sleeptime via either 1) or 2),
1575 * 3) becomes needless, so in this case we don't need to call
1576 * rtc_resume(), and this is what timekeeping_rtc_skipresume()
1577 * means.
1578 */
1579bool timekeeping_rtc_skipresume(void)
1580{
1581 return sleeptime_injected;
1582}
1583
1584/**
1585 * 1) can be determined whether to use or not only when doing
1586 * timekeeping_resume() which is invoked after rtc_suspend(),
1587 * so we can't skip rtc_suspend() surely if system has 1).
1588 *
1589 * But if system has 2), 2) will definitely be used, so in this
1590 * case we don't need to call rtc_suspend(), and this is what
1591 * timekeeping_rtc_skipsuspend() means.
1592 */
1593bool timekeeping_rtc_skipsuspend(void)
1594{
1595 return persistent_clock_exists;
1596}
1597
1598/**
pang.xunlei04d90892014-11-18 19:15:17 +08001599 * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values
1600 * @delta: pointer to a timespec64 delta value
John Stultz304529b2011-04-01 14:32:09 -07001601 *
Xunlei Pang2ee96632015-04-01 20:34:22 -07001602 * This hook is for architectures that cannot support read_persistent_clock64
John Stultz304529b2011-04-01 14:32:09 -07001603 * because their RTC/persistent clock is only accessible when irqs are enabled.
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001604 * and also don't have an effective nonstop clocksource.
John Stultz304529b2011-04-01 14:32:09 -07001605 *
1606 * This function should only be called by rtc_resume(), and allows
1607 * a suspend offset to be injected into the timekeeping values.
1608 */
pang.xunlei04d90892014-11-18 19:15:17 +08001609void timekeeping_inject_sleeptime64(struct timespec64 *delta)
John Stultz304529b2011-04-01 14:32:09 -07001610{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001611 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -08001612 unsigned long flags;
John Stultz304529b2011-04-01 14:32:09 -07001613
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001614 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001615 write_seqcount_begin(&tk_core.seq);
John Stultz70471f22011-11-14 12:48:10 -08001616
John Stultz4e250fd2012-07-27 14:48:13 -04001617 timekeeping_forward_now(tk);
John Stultz304529b2011-04-01 14:32:09 -07001618
pang.xunlei04d90892014-11-18 19:15:17 +08001619 __timekeeping_inject_sleeptime(tk, delta);
John Stultz304529b2011-04-01 14:32:09 -07001620
David Vrabel780427f2013-06-27 11:35:46 +01001621 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultz304529b2011-04-01 14:32:09 -07001622
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001623 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001624 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz304529b2011-04-01 14:32:09 -07001625
1626 /* signal hrtimers about time change */
1627 clock_was_set();
1628}
Xunlei Pang7f298132015-04-01 20:34:35 -07001629#endif
John Stultz304529b2011-04-01 14:32:09 -07001630
John Stultz304529b2011-04-01 14:32:09 -07001631/**
john stultz85240702007-05-08 00:27:59 -07001632 * timekeeping_resume - Resumes the generic timekeeping subsystem.
john stultz85240702007-05-08 00:27:59 -07001633 */
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +01001634void timekeeping_resume(void)
john stultz85240702007-05-08 00:27:59 -07001635{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001636 struct timekeeper *tk = &tk_core.timekeeper;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001637 struct clocksource *clock = tk->tkr_mono.clock;
John Stultz92c1d3e2011-11-14 14:05:44 -08001638 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001639 struct timespec64 ts_new, ts_delta;
Feng Tange445cf12013-03-12 11:56:48 +08001640 cycle_t cycle_now, cycle_delta;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001641
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001642 sleeptime_injected = false;
Xunlei Pang2ee96632015-04-01 20:34:22 -07001643 read_persistent_clock64(&ts_new);
john stultz85240702007-05-08 00:27:59 -07001644
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +02001645 clockevents_resume();
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +02001646 clocksource_resume();
1647
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001648 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001649 write_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -07001650
Feng Tange445cf12013-03-12 11:56:48 +08001651 /*
1652 * After system resumes, we need to calculate the suspended time and
1653 * compensate it for the OS time. There are 3 sources that could be
1654 * used: Nonstop clocksource during suspend, persistent clock and rtc
1655 * device.
1656 *
1657 * One specific platform may have 1 or 2 or all of them, and the
1658 * preference will be:
1659 * suspend-nonstop clocksource -> persistent clock -> rtc
1660 * The less preferred source will only be tried if there is no better
1661 * usable source. The rtc part is handled separately in rtc core code.
1662 */
John Stultz02a37cc2017-06-08 16:44:20 -07001663 cycle_now = tk_clock_read(&tk->tkr_mono);
Feng Tange445cf12013-03-12 11:56:48 +08001664 if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
Peter Zijlstra876e7882015-03-19 10:09:06 +01001665 cycle_now > tk->tkr_mono.cycle_last) {
Feng Tange445cf12013-03-12 11:56:48 +08001666 u64 num, max = ULLONG_MAX;
1667 u32 mult = clock->mult;
1668 u32 shift = clock->shift;
1669 s64 nsec = 0;
1670
Peter Zijlstra876e7882015-03-19 10:09:06 +01001671 cycle_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last,
1672 tk->tkr_mono.mask);
Feng Tange445cf12013-03-12 11:56:48 +08001673
1674 /*
1675 * "cycle_delta * mutl" may cause 64 bits overflow, if the
1676 * suspended time is too long. In that case we need do the
1677 * 64 bits math carefully
1678 */
1679 do_div(max, mult);
1680 if (cycle_delta > max) {
1681 num = div64_u64(cycle_delta, max);
1682 nsec = (((u64) max * mult) >> shift) * num;
1683 cycle_delta -= num * max;
1684 }
1685 nsec += ((u64) cycle_delta * mult) >> shift;
1686
John Stultz7d489d12014-07-16 21:04:01 +00001687 ts_delta = ns_to_timespec64(nsec);
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001688 sleeptime_injected = true;
John Stultz7d489d12014-07-16 21:04:01 +00001689 } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
1690 ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001691 sleeptime_injected = true;
john stultz85240702007-05-08 00:27:59 -07001692 }
Feng Tange445cf12013-03-12 11:56:48 +08001693
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001694 if (sleeptime_injected)
Feng Tange445cf12013-03-12 11:56:48 +08001695 __timekeeping_inject_sleeptime(tk, &ts_delta);
1696
1697 /* Re-base the last cycle value */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001698 tk->tkr_mono.cycle_last = cycle_now;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01001699 tk->tkr_raw.cycle_last = cycle_now;
1700
John Stultz4e250fd2012-07-27 14:48:13 -04001701 tk->ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -07001702 timekeeping_suspended = 0;
David Vrabel780427f2013-06-27 11:35:46 +01001703 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001704 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001705 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001706
1707 touch_softlockup_watchdog();
1708
Thomas Gleixner4ffee522015-03-25 13:09:16 +01001709 tick_resume();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +02001710 hrtimers_resume();
john stultz85240702007-05-08 00:27:59 -07001711}
1712
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +01001713int timekeeping_suspend(void)
john stultz85240702007-05-08 00:27:59 -07001714{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001715 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -08001716 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001717 struct timespec64 delta, delta_delta;
1718 static struct timespec64 old_delta;
john stultz85240702007-05-08 00:27:59 -07001719
Xunlei Pang2ee96632015-04-01 20:34:22 -07001720 read_persistent_clock64(&timekeeping_suspend_time);
Thomas Gleixner3be90952007-09-16 15:36:43 +02001721
Zoran Markovic0d6bd992013-05-17 11:24:05 -07001722 /*
1723 * On some systems the persistent_clock can not be detected at
1724 * timekeeping_init by its return value, so if we see a valid
1725 * value returned, update the persistent_clock_exists flag.
1726 */
1727 if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001728 persistent_clock_exists = true;
Zoran Markovic0d6bd992013-05-17 11:24:05 -07001729
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001730 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001731 write_seqcount_begin(&tk_core.seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001732 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -07001733 timekeeping_suspended = 1;
John Stultzcb332172011-05-31 22:53:23 -07001734
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001735 if (persistent_clock_exists) {
John Stultzcb332172011-05-31 22:53:23 -07001736 /*
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001737 * To avoid drift caused by repeated suspend/resumes,
1738 * which each can add ~1 second drift error,
1739 * try to compensate so the difference in system time
1740 * and persistent_clock time stays close to constant.
John Stultzcb332172011-05-31 22:53:23 -07001741 */
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001742 delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time);
1743 delta_delta = timespec64_sub(delta, old_delta);
1744 if (abs(delta_delta.tv_sec) >= 2) {
1745 /*
1746 * if delta_delta is too large, assume time correction
1747 * has occurred and set old_delta to the current delta.
1748 */
1749 old_delta = delta;
1750 } else {
1751 /* Otherwise try to adjust old_system to compensate */
1752 timekeeping_suspend_time =
1753 timespec64_add(timekeeping_suspend_time, delta_delta);
1754 }
John Stultzcb332172011-05-31 22:53:23 -07001755 }
John Stultz330a1612013-12-11 19:10:36 -08001756
1757 timekeeping_update(tk, TK_MIRROR);
Rafael J. Wysocki060407a2015-02-13 14:49:02 +01001758 halt_fast_timekeeper(tk);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001759 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001760 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001761
Thomas Gleixner4ffee522015-03-25 13:09:16 +01001762 tick_suspend();
Magnus Dammc54a42b2010-02-02 14:41:41 -08001763 clocksource_suspend();
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +02001764 clockevents_suspend();
john stultz85240702007-05-08 00:27:59 -07001765
1766 return 0;
1767}
1768
1769/* sysfs resume/suspend bits for timekeeping */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001770static struct syscore_ops timekeeping_syscore_ops = {
john stultz85240702007-05-08 00:27:59 -07001771 .resume = timekeeping_resume,
1772 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -07001773};
1774
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001775static int __init timekeeping_init_ops(void)
john stultz85240702007-05-08 00:27:59 -07001776{
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001777 register_syscore_ops(&timekeeping_syscore_ops);
1778 return 0;
john stultz85240702007-05-08 00:27:59 -07001779}
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001780device_initcall(timekeeping_init_ops);
john stultz85240702007-05-08 00:27:59 -07001781
1782/*
John Stultzdc491592013-12-06 17:25:21 -08001783 * Apply a multiplier adjustment to the timekeeper
john stultz85240702007-05-08 00:27:59 -07001784 */
John Stultzdc491592013-12-06 17:25:21 -08001785static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
1786 s64 offset,
1787 bool negative,
1788 int adj_scale)
john stultz85240702007-05-08 00:27:59 -07001789{
John Stultzdc491592013-12-06 17:25:21 -08001790 s64 interval = tk->cycle_interval;
1791 s32 mult_adj = 1;
john stultz85240702007-05-08 00:27:59 -07001792
John Stultzdc491592013-12-06 17:25:21 -08001793 if (negative) {
1794 mult_adj = -mult_adj;
1795 interval = -interval;
1796 offset = -offset;
john stultz85240702007-05-08 00:27:59 -07001797 }
John Stultzdc491592013-12-06 17:25:21 -08001798 mult_adj <<= adj_scale;
1799 interval <<= adj_scale;
1800 offset <<= adj_scale;
john stultz85240702007-05-08 00:27:59 -07001801
John Stultzc2bc1112011-10-27 18:12:42 -07001802 /*
1803 * So the following can be confusing.
1804 *
John Stultzdc491592013-12-06 17:25:21 -08001805 * To keep things simple, lets assume mult_adj == 1 for now.
John Stultzc2bc1112011-10-27 18:12:42 -07001806 *
John Stultzdc491592013-12-06 17:25:21 -08001807 * When mult_adj != 1, remember that the interval and offset values
John Stultzc2bc1112011-10-27 18:12:42 -07001808 * have been appropriately scaled so the math is the same.
1809 *
1810 * The basic idea here is that we're increasing the multiplier
1811 * by one, this causes the xtime_interval to be incremented by
1812 * one cycle_interval. This is because:
1813 * xtime_interval = cycle_interval * mult
1814 * So if mult is being incremented by one:
1815 * xtime_interval = cycle_interval * (mult + 1)
1816 * Its the same as:
1817 * xtime_interval = (cycle_interval * mult) + cycle_interval
1818 * Which can be shortened to:
1819 * xtime_interval += cycle_interval
1820 *
1821 * So offset stores the non-accumulated cycles. Thus the current
1822 * time (in shifted nanoseconds) is:
1823 * now = (offset * adj) + xtime_nsec
1824 * Now, even though we're adjusting the clock frequency, we have
1825 * to keep time consistent. In other words, we can't jump back
1826 * in time, and we also want to avoid jumping forward in time.
1827 *
1828 * So given the same offset value, we need the time to be the same
1829 * both before and after the freq adjustment.
1830 * now = (offset * adj_1) + xtime_nsec_1
1831 * now = (offset * adj_2) + xtime_nsec_2
1832 * So:
1833 * (offset * adj_1) + xtime_nsec_1 =
1834 * (offset * adj_2) + xtime_nsec_2
1835 * And we know:
1836 * adj_2 = adj_1 + 1
1837 * So:
1838 * (offset * adj_1) + xtime_nsec_1 =
1839 * (offset * (adj_1+1)) + xtime_nsec_2
1840 * (offset * adj_1) + xtime_nsec_1 =
1841 * (offset * adj_1) + offset + xtime_nsec_2
1842 * Canceling the sides:
1843 * xtime_nsec_1 = offset + xtime_nsec_2
1844 * Which gives us:
1845 * xtime_nsec_2 = xtime_nsec_1 - offset
1846 * Which simplfies to:
1847 * xtime_nsec -= offset
1848 *
1849 * XXX - TODO: Doc ntp_error calculation.
1850 */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001851 if ((mult_adj > 0) && (tk->tkr_mono.mult + mult_adj < mult_adj)) {
pang.xunlei6067dc52014-10-08 15:03:34 +08001852 /* NTP adjustment caused clocksource mult overflow */
1853 WARN_ON_ONCE(1);
1854 return;
1855 }
1856
Peter Zijlstra876e7882015-03-19 10:09:06 +01001857 tk->tkr_mono.mult += mult_adj;
John Stultzf726a692012-07-13 01:21:57 -04001858 tk->xtime_interval += interval;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001859 tk->tkr_mono.xtime_nsec -= offset;
John Stultzf726a692012-07-13 01:21:57 -04001860 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
John Stultzdc491592013-12-06 17:25:21 -08001861}
John Stultz2a8c0882012-07-13 01:21:56 -04001862
John Stultzdc491592013-12-06 17:25:21 -08001863/*
1864 * Calculate the multiplier adjustment needed to match the frequency
1865 * specified by NTP
1866 */
1867static __always_inline void timekeeping_freqadjust(struct timekeeper *tk,
1868 s64 offset)
1869{
1870 s64 interval = tk->cycle_interval;
1871 s64 xinterval = tk->xtime_interval;
John Stultzec02b072015-12-03 10:23:30 -08001872 u32 base = tk->tkr_mono.clock->mult;
1873 u32 max = tk->tkr_mono.clock->maxadj;
1874 u32 cur_adj = tk->tkr_mono.mult;
John Stultzdc491592013-12-06 17:25:21 -08001875 s64 tick_error;
1876 bool negative;
John Stultzec02b072015-12-03 10:23:30 -08001877 u32 adj_scale;
John Stultzdc491592013-12-06 17:25:21 -08001878
1879 /* Remove any current error adj from freq calculation */
1880 if (tk->ntp_err_mult)
1881 xinterval -= tk->cycle_interval;
1882
John Stultz375f45b2014-04-23 20:53:29 -07001883 tk->ntp_tick = ntp_tick_length();
1884
John Stultzdc491592013-12-06 17:25:21 -08001885 /* Calculate current error per tick */
1886 tick_error = ntp_tick_length() >> tk->ntp_error_shift;
1887 tick_error -= (xinterval + tk->xtime_remainder);
1888
1889 /* Don't worry about correcting it if its small */
1890 if (likely((tick_error >= 0) && (tick_error <= interval)))
1891 return;
1892
1893 /* preserve the direction of correction */
1894 negative = (tick_error < 0);
1895
John Stultzec02b072015-12-03 10:23:30 -08001896 /* If any adjustment would pass the max, just return */
1897 if (negative && (cur_adj - 1) <= (base - max))
1898 return;
1899 if (!negative && (cur_adj + 1) >= (base + max))
1900 return;
1901 /*
1902 * Sort out the magnitude of the correction, but
1903 * avoid making so large a correction that we go
1904 * over the max adjustment.
1905 */
1906 adj_scale = 0;
Andrew Morton79211c82015-11-09 14:58:13 -08001907 tick_error = abs(tick_error);
John Stultzec02b072015-12-03 10:23:30 -08001908 while (tick_error > interval) {
1909 u32 adj = 1 << (adj_scale + 1);
1910
1911 /* Check if adjustment gets us within 1 unit from the max */
1912 if (negative && (cur_adj - adj) <= (base - max))
1913 break;
1914 if (!negative && (cur_adj + adj) >= (base + max))
1915 break;
1916
1917 adj_scale++;
John Stultzdc491592013-12-06 17:25:21 -08001918 tick_error >>= 1;
John Stultzec02b072015-12-03 10:23:30 -08001919 }
John Stultzdc491592013-12-06 17:25:21 -08001920
1921 /* scale the corrections */
John Stultzec02b072015-12-03 10:23:30 -08001922 timekeeping_apply_adjustment(tk, offset, negative, adj_scale);
John Stultzdc491592013-12-06 17:25:21 -08001923}
1924
1925/*
1926 * Adjust the timekeeper's multiplier to the correct frequency
1927 * and also to reduce the accumulated error value.
1928 */
1929static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
1930{
1931 /* Correct for the current frequency error */
1932 timekeeping_freqadjust(tk, offset);
1933
1934 /* Next make a small adjustment to fix any cumulative error */
1935 if (!tk->ntp_err_mult && (tk->ntp_error > 0)) {
1936 tk->ntp_err_mult = 1;
1937 timekeeping_apply_adjustment(tk, offset, 0, 0);
1938 } else if (tk->ntp_err_mult && (tk->ntp_error <= 0)) {
1939 /* Undo any existing error adjustment */
1940 timekeeping_apply_adjustment(tk, offset, 1, 0);
1941 tk->ntp_err_mult = 0;
1942 }
1943
Peter Zijlstra876e7882015-03-19 10:09:06 +01001944 if (unlikely(tk->tkr_mono.clock->maxadj &&
1945 (abs(tk->tkr_mono.mult - tk->tkr_mono.clock->mult)
1946 > tk->tkr_mono.clock->maxadj))) {
John Stultzdc491592013-12-06 17:25:21 -08001947 printk_once(KERN_WARNING
1948 "Adjusting %s more than 11%% (%ld vs %ld)\n",
Peter Zijlstra876e7882015-03-19 10:09:06 +01001949 tk->tkr_mono.clock->name, (long)tk->tkr_mono.mult,
1950 (long)tk->tkr_mono.clock->mult + tk->tkr_mono.clock->maxadj);
John Stultzdc491592013-12-06 17:25:21 -08001951 }
1952
John Stultz2a8c0882012-07-13 01:21:56 -04001953 /*
1954 * It may be possible that when we entered this function, xtime_nsec
1955 * was very small. Further, if we're slightly speeding the clocksource
1956 * in the code above, its possible the required corrective factor to
1957 * xtime_nsec could cause it to underflow.
1958 *
1959 * Now, since we already accumulated the second, cannot simply roll
1960 * the accumulated second back, since the NTP subsystem has been
1961 * notified via second_overflow. So instead we push xtime_nsec forward
1962 * by the amount we underflowed, and add that amount into the error.
1963 *
1964 * We'll correct this error next time through this function, when
1965 * xtime_nsec is not as small.
1966 */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001967 if (unlikely((s64)tk->tkr_mono.xtime_nsec < 0)) {
1968 s64 neg = -(s64)tk->tkr_mono.xtime_nsec;
1969 tk->tkr_mono.xtime_nsec = 0;
John Stultzf726a692012-07-13 01:21:57 -04001970 tk->ntp_error += neg << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -04001971 }
john stultz85240702007-05-08 00:27:59 -07001972}
1973
1974/**
John Stultz1f4f9482012-07-13 01:21:54 -04001975 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
1976 *
Zhen Lei571af552015-08-25 14:42:53 +08001977 * Helper function that accumulates the nsecs greater than a second
John Stultz1f4f9482012-07-13 01:21:54 -04001978 * from the xtime_nsec field to the xtime_secs field.
1979 * It also calls into the NTP code to handle leapsecond processing.
1980 *
1981 */
David Vrabel780427f2013-06-27 11:35:46 +01001982static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
John Stultz1f4f9482012-07-13 01:21:54 -04001983{
Peter Zijlstra876e7882015-03-19 10:09:06 +01001984 u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
John Stultz5258d3f2013-12-11 20:07:49 -08001985 unsigned int clock_set = 0;
John Stultz1f4f9482012-07-13 01:21:54 -04001986
Peter Zijlstra876e7882015-03-19 10:09:06 +01001987 while (tk->tkr_mono.xtime_nsec >= nsecps) {
John Stultz1f4f9482012-07-13 01:21:54 -04001988 int leap;
1989
Peter Zijlstra876e7882015-03-19 10:09:06 +01001990 tk->tkr_mono.xtime_nsec -= nsecps;
John Stultz1f4f9482012-07-13 01:21:54 -04001991 tk->xtime_sec++;
1992
1993 /* Figure out if its a leap sec and apply if needed */
1994 leap = second_overflow(tk->xtime_sec);
John Stultz6d0ef902012-07-27 14:48:12 -04001995 if (unlikely(leap)) {
John Stultz7d489d12014-07-16 21:04:01 +00001996 struct timespec64 ts;
John Stultz1f4f9482012-07-13 01:21:54 -04001997
John Stultz6d0ef902012-07-27 14:48:12 -04001998 tk->xtime_sec += leap;
1999
2000 ts.tv_sec = leap;
2001 ts.tv_nsec = 0;
2002 tk_set_wall_to_mono(tk,
John Stultz7d489d12014-07-16 21:04:01 +00002003 timespec64_sub(tk->wall_to_monotonic, ts));
John Stultz6d0ef902012-07-27 14:48:12 -04002004
John Stultzcc244dd2012-05-03 12:30:07 -07002005 __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
2006
John Stultz5258d3f2013-12-11 20:07:49 -08002007 clock_set = TK_CLOCK_WAS_SET;
John Stultz6d0ef902012-07-27 14:48:12 -04002008 }
John Stultz1f4f9482012-07-13 01:21:54 -04002009 }
John Stultz5258d3f2013-12-11 20:07:49 -08002010 return clock_set;
John Stultz1f4f9482012-07-13 01:21:54 -04002011}
2012
John Stultz1f4f9482012-07-13 01:21:54 -04002013/**
john stultza092ff02009-10-02 16:17:53 -07002014 * logarithmic_accumulation - shifted accumulation of cycles
2015 *
2016 * This functions accumulates a shifted interval of cycles into
2017 * into a shifted interval nanoseconds. Allows for O(log) accumulation
2018 * loop.
2019 *
2020 * Returns the unconsumed cycles.
2021 */
John Stultzf726a692012-07-13 01:21:57 -04002022static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
John Stultz5258d3f2013-12-11 20:07:49 -08002023 u32 shift,
2024 unsigned int *clock_set)
john stultza092ff02009-10-02 16:17:53 -07002025{
Thomas Gleixner23a95372013-02-21 22:51:36 +00002026 cycle_t interval = tk->cycle_interval << shift;
John Stultza53bfdd2017-06-08 16:44:21 -07002027 u64 snsec_per_sec;
john stultza092ff02009-10-02 16:17:53 -07002028
Zhen Lei571af552015-08-25 14:42:53 +08002029 /* If the offset is smaller than a shifted interval, do nothing */
Thomas Gleixner23a95372013-02-21 22:51:36 +00002030 if (offset < interval)
john stultza092ff02009-10-02 16:17:53 -07002031 return offset;
2032
2033 /* Accumulate one shifted interval */
Thomas Gleixner23a95372013-02-21 22:51:36 +00002034 offset -= interval;
Peter Zijlstra876e7882015-03-19 10:09:06 +01002035 tk->tkr_mono.cycle_last += interval;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01002036 tk->tkr_raw.cycle_last += interval;
john stultza092ff02009-10-02 16:17:53 -07002037
Peter Zijlstra876e7882015-03-19 10:09:06 +01002038 tk->tkr_mono.xtime_nsec += tk->xtime_interval << shift;
John Stultz5258d3f2013-12-11 20:07:49 -08002039 *clock_set |= accumulate_nsecs_to_secs(tk);
john stultza092ff02009-10-02 16:17:53 -07002040
Jason Wesseldeda2e82010-08-09 14:20:09 -07002041 /* Accumulate raw time */
John Stultza53bfdd2017-06-08 16:44:21 -07002042 tk->tkr_raw.xtime_nsec += (u64)tk->raw_time.tv_nsec << tk->tkr_raw.shift;
2043 tk->tkr_raw.xtime_nsec += tk->raw_interval << shift;
2044 snsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
2045 while (tk->tkr_raw.xtime_nsec >= snsec_per_sec) {
2046 tk->tkr_raw.xtime_nsec -= snsec_per_sec;
2047 tk->raw_time.tv_sec++;
john stultza092ff02009-10-02 16:17:53 -07002048 }
John Stultza53bfdd2017-06-08 16:44:21 -07002049 tk->raw_time.tv_nsec = tk->tkr_raw.xtime_nsec >> tk->tkr_raw.shift;
2050 tk->tkr_raw.xtime_nsec -= (u64)tk->raw_time.tv_nsec << tk->tkr_raw.shift;
john stultza092ff02009-10-02 16:17:53 -07002051
2052 /* Accumulate error between NTP and clock interval */
John Stultz375f45b2014-04-23 20:53:29 -07002053 tk->ntp_error += tk->ntp_tick << shift;
John Stultzf726a692012-07-13 01:21:57 -04002054 tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
2055 (tk->ntp_error_shift + shift);
john stultza092ff02009-10-02 16:17:53 -07002056
2057 return offset;
2058}
2059
john stultz85240702007-05-08 00:27:59 -07002060/**
2061 * update_wall_time - Uses the current clocksource to increment the wall time
2062 *
john stultz85240702007-05-08 00:27:59 -07002063 */
John Stultz47a1b7962013-12-12 13:10:55 -08002064void update_wall_time(void)
john stultz85240702007-05-08 00:27:59 -07002065{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002066 struct timekeeper *real_tk = &tk_core.timekeeper;
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002067 struct timekeeper *tk = &shadow_timekeeper;
john stultz85240702007-05-08 00:27:59 -07002068 cycle_t offset;
john stultza092ff02009-10-02 16:17:53 -07002069 int shift = 0, maxshift;
John Stultz5258d3f2013-12-11 20:07:49 -08002070 unsigned int clock_set = 0;
John Stultz70471f22011-11-14 12:48:10 -08002071 unsigned long flags;
2072
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00002073 raw_spin_lock_irqsave(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07002074
2075 /* Make sure we're fully resumed: */
2076 if (unlikely(timekeeping_suspended))
John Stultz70471f22011-11-14 12:48:10 -08002077 goto out;
john stultz85240702007-05-08 00:27:59 -07002078
John Stultz592913e2010-07-13 17:56:20 -07002079#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002080 offset = real_tk->cycle_interval;
John Stultz592913e2010-07-13 17:56:20 -07002081#else
John Stultz02a37cc2017-06-08 16:44:20 -07002082 offset = clocksource_delta(tk_clock_read(&tk->tkr_mono),
Peter Zijlstra876e7882015-03-19 10:09:06 +01002083 tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
john stultz85240702007-05-08 00:27:59 -07002084#endif
john stultz85240702007-05-08 00:27:59 -07002085
John Stultzbf2ac312012-08-21 20:30:49 -04002086 /* Check if there's really nothing to do */
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002087 if (offset < real_tk->cycle_interval)
John Stultzbf2ac312012-08-21 20:30:49 -04002088 goto out;
2089
John Stultz3c17ad12015-03-11 21:16:32 -07002090 /* Do some additional sanity checking */
2091 timekeeping_check_update(real_tk, offset);
2092
john stultza092ff02009-10-02 16:17:53 -07002093 /*
2094 * With NO_HZ we may have to accumulate many cycle_intervals
2095 * (think "ticks") worth of time at once. To do this efficiently,
2096 * we calculate the largest doubling multiple of cycle_intervals
Jim Cromie88b28ad2012-03-14 21:28:56 -06002097 * that is smaller than the offset. We then accumulate that
john stultza092ff02009-10-02 16:17:53 -07002098 * chunk in one go, and then try to consume the next smaller
2099 * doubled multiple.
john stultz85240702007-05-08 00:27:59 -07002100 */
John Stultz4e250fd2012-07-27 14:48:13 -04002101 shift = ilog2(offset) - ilog2(tk->cycle_interval);
john stultza092ff02009-10-02 16:17:53 -07002102 shift = max(0, shift);
Jim Cromie88b28ad2012-03-14 21:28:56 -06002103 /* Bound shift to one less than what overflows tick_length */
John Stultzea7cf492011-11-14 13:18:07 -08002104 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
john stultza092ff02009-10-02 16:17:53 -07002105 shift = min(shift, maxshift);
John Stultz4e250fd2012-07-27 14:48:13 -04002106 while (offset >= tk->cycle_interval) {
John Stultz5258d3f2013-12-11 20:07:49 -08002107 offset = logarithmic_accumulation(tk, offset, shift,
2108 &clock_set);
John Stultz4e250fd2012-07-27 14:48:13 -04002109 if (offset < tk->cycle_interval<<shift)
John Stultz830ec042010-03-18 14:47:30 -07002110 shift--;
john stultz85240702007-05-08 00:27:59 -07002111 }
2112
2113 /* correct the clock when NTP error is too big */
John Stultz4e250fd2012-07-27 14:48:13 -04002114 timekeeping_adjust(tk, offset);
john stultz85240702007-05-08 00:27:59 -07002115
John Stultz6a867a32010-04-06 14:30:51 -07002116 /*
John Stultz92bb1fc2012-09-04 15:38:12 -04002117 * XXX This can be killed once everyone converts
2118 * to the new update_vsyscall.
2119 */
2120 old_vsyscall_fixup(tk);
john stultz85240702007-05-08 00:27:59 -07002121
John Stultz6a867a32010-04-06 14:30:51 -07002122 /*
2123 * Finally, make sure that after the rounding
John Stultz1e75fa82012-07-13 01:21:53 -04002124 * xtime_nsec isn't larger than NSEC_PER_SEC
John Stultz6a867a32010-04-06 14:30:51 -07002125 */
John Stultz5258d3f2013-12-11 20:07:49 -08002126 clock_set |= accumulate_nsecs_to_secs(tk);
Linus Torvalds83f57a12009-12-22 14:10:37 -08002127
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002128 write_seqcount_begin(&tk_core.seq);
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002129 /*
2130 * Update the real timekeeper.
2131 *
2132 * We could avoid this memcpy by switching pointers, but that
2133 * requires changes to all other timekeeper usage sites as
2134 * well, i.e. move the timekeeper pointer getter into the
2135 * spinlocked/seqcount protected sections. And we trade this
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002136 * memcpy under the tk_core.seq against one before we start
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002137 * updating.
2138 */
John Stultz906c5552015-06-17 10:05:53 -07002139 timekeeping_update(tk, clock_set);
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002140 memcpy(real_tk, tk, sizeof(*tk));
John Stultz906c5552015-06-17 10:05:53 -07002141 /* The memcpy must come last. Do not put anything here! */
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002142 write_seqcount_end(&tk_core.seq);
Thomas Gleixnerca4523c2013-02-21 22:51:40 +00002143out:
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00002144 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz47a1b7962013-12-12 13:10:55 -08002145 if (clock_set)
John Stultzcab5e122014-03-27 16:30:49 -07002146 /* Have to call _delayed version, since in irq context*/
2147 clock_was_set_delayed();
john stultz85240702007-05-08 00:27:59 -07002148}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002149
2150/**
John Stultzd08c0cd2014-12-08 12:00:09 -08002151 * getboottime64 - Return the real time of system boot.
2152 * @ts: pointer to the timespec64 to be set
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002153 *
John Stultzd08c0cd2014-12-08 12:00:09 -08002154 * Returns the wall-time of boot in a timespec64.
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002155 *
2156 * This is based on the wall_to_monotonic offset and the total suspend
2157 * time. Calls to settimeofday will affect the value returned (which
2158 * basically means that however wrong your real time clock is at boot time,
2159 * you get the right time here).
2160 */
John Stultzd08c0cd2014-12-08 12:00:09 -08002161void getboottime64(struct timespec64 *ts)
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002162{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002163 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixner02cba152014-07-16 21:04:58 +00002164 ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot);
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02002165
John Stultzd08c0cd2014-12-08 12:00:09 -08002166 *ts = ktime_to_timespec64(t);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002167}
John Stultzd08c0cd2014-12-08 12:00:09 -08002168EXPORT_SYMBOL_GPL(getboottime64);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002169
john stultz17c38b72007-07-24 18:38:34 -07002170unsigned long get_seconds(void)
2171{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002172 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04002173
2174 return tk->xtime_sec;
john stultz17c38b72007-07-24 18:38:34 -07002175}
2176EXPORT_SYMBOL(get_seconds);
2177
john stultzda15cfd2009-08-19 19:13:34 -07002178struct timespec __current_kernel_time(void)
2179{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002180 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04002181
John Stultz7d489d12014-07-16 21:04:01 +00002182 return timespec64_to_timespec(tk_xtime(tk));
john stultzda15cfd2009-08-19 19:13:34 -07002183}
john stultz17c38b72007-07-24 18:38:34 -07002184
Baolin Wang8758a242015-07-29 20:09:43 +08002185struct timespec64 current_kernel_time64(void)
john stultz2c6b47d2007-07-24 17:47:43 -07002186{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002187 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz7d489d12014-07-16 21:04:01 +00002188 struct timespec64 now;
john stultz2c6b47d2007-07-24 17:47:43 -07002189 unsigned long seq;
2190
2191 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002192 seq = read_seqcount_begin(&tk_core.seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08002193
John Stultz4e250fd2012-07-27 14:48:13 -04002194 now = tk_xtime(tk);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002195 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz2c6b47d2007-07-24 17:47:43 -07002196
Baolin Wang8758a242015-07-29 20:09:43 +08002197 return now;
john stultz2c6b47d2007-07-24 17:47:43 -07002198}
Baolin Wang8758a242015-07-29 20:09:43 +08002199EXPORT_SYMBOL(current_kernel_time64);
john stultzda15cfd2009-08-19 19:13:34 -07002200
John Stultz334334b2014-11-07 11:20:40 -08002201struct timespec64 get_monotonic_coarse64(void)
john stultzda15cfd2009-08-19 19:13:34 -07002202{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002203 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz7d489d12014-07-16 21:04:01 +00002204 struct timespec64 now, mono;
john stultzda15cfd2009-08-19 19:13:34 -07002205 unsigned long seq;
2206
2207 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002208 seq = read_seqcount_begin(&tk_core.seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08002209
John Stultz4e250fd2012-07-27 14:48:13 -04002210 now = tk_xtime(tk);
2211 mono = tk->wall_to_monotonic;
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002212 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultzda15cfd2009-08-19 19:13:34 -07002213
John Stultz7d489d12014-07-16 21:04:01 +00002214 set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec,
john stultzda15cfd2009-08-19 19:13:34 -07002215 now.tv_nsec + mono.tv_nsec);
John Stultz7d489d12014-07-16 21:04:01 +00002216
John Stultz334334b2014-11-07 11:20:40 -08002217 return now;
john stultzda15cfd2009-08-19 19:13:34 -07002218}
Gregor Boirieeaaa7ec2016-03-09 19:05:48 +01002219EXPORT_SYMBOL(get_monotonic_coarse64);
Torben Hohn871cf1e2011-01-27 15:58:55 +01002220
2221/*
John Stultzd6ad4182012-02-28 16:50:11 -08002222 * Must hold jiffies_lock
Torben Hohn871cf1e2011-01-27 15:58:55 +01002223 */
2224void do_timer(unsigned long ticks)
2225{
2226 jiffies_64 += ticks;
Torben Hohn871cf1e2011-01-27 15:58:55 +01002227 calc_global_load(ticks);
2228}
Torben Hohn48cf76f72011-01-27 15:59:05 +01002229
2230/**
John Stultz76f41082014-07-16 21:03:52 +00002231 * ktime_get_update_offsets_now - hrtimer helper
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002232 * @cwsseq: pointer to check and store the clock was set sequence number
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002233 * @offs_real: pointer to storage for monotonic -> realtime offset
2234 * @offs_boot: pointer to storage for monotonic -> boottime offset
Xie XiuQib7bc50e2013-10-18 09:13:30 +08002235 * @offs_tai: pointer to storage for monotonic -> clock tai offset
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002236 *
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002237 * Returns current monotonic time and updates the offsets if the
2238 * sequence number in @cwsseq and timekeeper.clock_was_set_seq are
2239 * different.
2240 *
Xie XiuQib7bc50e2013-10-18 09:13:30 +08002241 * Called from hrtimer_interrupt() or retrigger_next_event()
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002242 */
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002243ktime_t ktime_get_update_offsets_now(unsigned int *cwsseq, ktime_t *offs_real,
2244 ktime_t *offs_boot, ktime_t *offs_tai)
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002245{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002246 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002247 unsigned int seq;
Thomas Gleixnera37c0aa2014-07-16 21:04:19 +00002248 ktime_t base;
2249 u64 nsecs;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002250
2251 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002252 seq = read_seqcount_begin(&tk_core.seq);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002253
Peter Zijlstra876e7882015-03-19 10:09:06 +01002254 base = tk->tkr_mono.base;
2255 nsecs = timekeeping_get_ns(&tk->tkr_mono);
John Stultz833f32d2015-06-11 15:54:55 -07002256 base = ktime_add_ns(base, nsecs);
2257
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002258 if (*cwsseq != tk->clock_was_set_seq) {
2259 *cwsseq = tk->clock_was_set_seq;
2260 *offs_real = tk->offs_real;
2261 *offs_boot = tk->offs_boot;
2262 *offs_tai = tk->offs_tai;
2263 }
John Stultz833f32d2015-06-11 15:54:55 -07002264
2265 /* Handle leapsecond insertion adjustments */
2266 if (unlikely(base.tv64 >= tk->next_leap_ktime.tv64))
2267 *offs_real = ktime_sub(tk->offs_real, ktime_set(1, 0));
2268
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002269 } while (read_seqcount_retry(&tk_core.seq, seq));
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002270
John Stultz833f32d2015-06-11 15:54:55 -07002271 return base;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002272}
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002273
Torben Hohnf0af911a92011-01-27 15:59:10 +01002274/**
John Stultzaa6f9c592013-03-22 11:31:29 -07002275 * do_adjtimex() - Accessor function to NTP __do_adjtimex function
2276 */
2277int do_adjtimex(struct timex *txc)
2278{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002279 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz06c017f2013-03-22 11:37:28 -07002280 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00002281 struct timespec64 ts;
John Stultz4e8f8b32013-04-10 12:41:49 -07002282 s32 orig_tai, tai;
John Stultze4085692013-03-22 12:08:52 -07002283 int ret;
2284
2285 /* Validate the data before disabling interrupts */
2286 ret = ntp_validate_timex(txc);
2287 if (ret)
2288 return ret;
2289
John Stultzcef90372013-03-22 15:04:13 -07002290 if (txc->modes & ADJ_SETOFFSET) {
2291 struct timespec delta;
2292 delta.tv_sec = txc->time.tv_sec;
2293 delta.tv_nsec = txc->time.tv_usec;
2294 if (!(txc->modes & ADJ_NANO))
2295 delta.tv_nsec *= 1000;
2296 ret = timekeeping_inject_offset(&delta);
2297 if (ret)
2298 return ret;
2299 }
2300
Thomas Gleixnerd6d29892014-07-16 21:04:04 +00002301 getnstimeofday64(&ts);
John Stultzaa6f9c592013-03-22 11:31:29 -07002302
John Stultz06c017f2013-03-22 11:37:28 -07002303 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002304 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002305
John Stultz4e8f8b32013-04-10 12:41:49 -07002306 orig_tai = tai = tk->tai_offset;
John Stultz87ace392013-03-22 12:28:15 -07002307 ret = __do_adjtimex(txc, &ts, &tai);
2308
John Stultz4e8f8b32013-04-10 12:41:49 -07002309 if (tai != orig_tai) {
2310 __timekeeping_set_tai_offset(tk, tai);
John Stultzf55c0762013-12-11 18:50:25 -08002311 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultz4e8f8b32013-04-10 12:41:49 -07002312 }
John Stultz833f32d2015-06-11 15:54:55 -07002313 tk_update_leap_state(tk);
2314
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002315 write_seqcount_end(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002316 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
2317
John Stultz6fdda9a2013-12-10 17:18:18 -08002318 if (tai != orig_tai)
2319 clock_was_set();
2320
John Stultz7bd36012013-09-11 16:50:56 -07002321 ntp_notify_cmos_timer();
2322
John Stultz87ace392013-03-22 12:28:15 -07002323 return ret;
2324}
John Stultzaa6f9c592013-03-22 11:31:29 -07002325
2326#ifdef CONFIG_NTP_PPS
2327/**
2328 * hardpps() - Accessor function to NTP __hardpps function
2329 */
Arnd Bergmann7ec88e42015-09-28 22:21:28 +02002330void hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts)
John Stultzaa6f9c592013-03-22 11:31:29 -07002331{
John Stultz06c017f2013-03-22 11:37:28 -07002332 unsigned long flags;
2333
2334 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002335 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002336
John Stultzaa6f9c592013-03-22 11:31:29 -07002337 __hardpps(phase_ts, raw_ts);
John Stultz06c017f2013-03-22 11:37:28 -07002338
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002339 write_seqcount_end(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002340 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzaa6f9c592013-03-22 11:31:29 -07002341}
2342EXPORT_SYMBOL(hardpps);
2343#endif
2344
2345/**
Torben Hohnf0af911a92011-01-27 15:59:10 +01002346 * xtime_update() - advances the timekeeping infrastructure
2347 * @ticks: number of ticks, that have elapsed since the last call.
2348 *
2349 * Must be called with interrupts disabled.
2350 */
2351void xtime_update(unsigned long ticks)
2352{
John Stultzd6ad4182012-02-28 16:50:11 -08002353 write_seqlock(&jiffies_lock);
Torben Hohnf0af911a92011-01-27 15:59:10 +01002354 do_timer(ticks);
John Stultzd6ad4182012-02-28 16:50:11 -08002355 write_sequnlock(&jiffies_lock);
John Stultz47a1b7962013-12-12 13:10:55 -08002356 update_wall_time();
Torben Hohnf0af911a92011-01-27 15:59:10 +01002357}