blob: 427e33dbb98d75463b21cdb1f292f5755fd25ae4 [file] [log] [blame]
john stultz85240702007-05-08 00:27:59 -07001/*
2 * linux/kernel/time/timekeeping.c
3 *
4 * Kernel timekeeping code and accessor functions
5 *
6 * This code was moved from linux/kernel/timer.c.
7 * Please see that file for copyright and history logs.
8 *
9 */
10
John Stultzd7b42022012-09-04 15:12:07 -040011#include <linux/timekeeper_internal.h>
john stultz85240702007-05-08 00:27:59 -070012#include <linux/module.h>
13#include <linux/interrupt.h>
14#include <linux/percpu.h>
15#include <linux/init.h>
16#include <linux/mm.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040017#include <linux/sched.h>
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +010018#include <linux/syscore_ops.h>
john stultz85240702007-05-08 00:27:59 -070019#include <linux/clocksource.h>
20#include <linux/jiffies.h>
21#include <linux/time.h>
22#include <linux/tick.h>
Martin Schwidefsky75c51582009-08-14 15:47:30 +020023#include <linux/stop_machine.h>
Marcelo Tosattie0b306f2012-11-27 23:28:59 -020024#include <linux/pvclock_gtod.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070025#include <linux/compiler.h>
john stultz85240702007-05-08 00:27:59 -070026
Thomas Gleixnereb93e4d2013-02-21 22:51:36 +000027#include "tick-internal.h"
John Stultzaa6f9c592013-03-22 11:31:29 -070028#include "ntp_internal.h"
Colin Cross5c835452013-05-21 22:32:14 -070029#include "timekeeping_internal.h"
Martin Schwidefsky155ec602009-08-14 15:47:26 +020030
David Vrabel04397fe92013-06-27 11:35:45 +010031#define TK_CLEAR_NTP (1 << 0)
32#define TK_MIRROR (1 << 1)
David Vrabel780427f2013-06-27 11:35:46 +010033#define TK_CLOCK_WAS_SET (1 << 2)
David Vrabel04397fe92013-06-27 11:35:45 +010034
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +000035/*
36 * The most important data for readout fits into a single 64 byte
37 * cache line.
38 */
39static struct {
40 seqcount_t seq;
41 struct timekeeper timekeeper;
42} tk_core ____cacheline_aligned;
43
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +000044static DEFINE_RAW_SPINLOCK(timekeeper_lock);
Thomas Gleixner48cdc132013-02-21 22:51:40 +000045static struct timekeeper shadow_timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020046
Thomas Gleixner4396e052014-07-16 21:05:23 +000047/**
48 * struct tk_fast - NMI safe timekeeper
49 * @seq: Sequence counter for protecting updates. The lowest bit
50 * is the index for the tk_read_base array
51 * @base: tk_read_base array. Access is indexed by the lowest bit of
52 * @seq.
53 *
54 * See @update_fast_timekeeper() below.
55 */
56struct tk_fast {
57 seqcount_t seq;
58 struct tk_read_base base[2];
59};
60
61static struct tk_fast tk_fast_mono ____cacheline_aligned;
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +010062static struct tk_fast tk_fast_raw ____cacheline_aligned;
Thomas Gleixner4396e052014-07-16 21:05:23 +000063
John Stultz8fcce542011-11-14 11:46:39 -080064/* flag for if timekeeping is suspended */
65int __read_mostly timekeeping_suspended;
66
John Stultz1e75fa82012-07-13 01:21:53 -040067static inline void tk_normalize_xtime(struct timekeeper *tk)
68{
Peter Zijlstra876e7882015-03-19 10:09:06 +010069 while (tk->tkr_mono.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_mono.shift)) {
70 tk->tkr_mono.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
John Stultz1e75fa82012-07-13 01:21:53 -040071 tk->xtime_sec++;
72 }
John Stultz5311c742017-05-22 17:20:20 -070073 while (tk->tkr_raw.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_raw.shift)) {
74 tk->tkr_raw.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
75 tk->raw_sec++;
76 }
John Stultz1e75fa82012-07-13 01:21:53 -040077}
John Stultz8fcce542011-11-14 11:46:39 -080078
Thomas Gleixnerc905fae2014-07-16 21:04:05 +000079static inline struct timespec64 tk_xtime(struct timekeeper *tk)
80{
81 struct timespec64 ts;
82
83 ts.tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +010084 ts.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
Thomas Gleixnerc905fae2014-07-16 21:04:05 +000085 return ts;
86}
87
John Stultz7d489d12014-07-16 21:04:01 +000088static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
John Stultz1e75fa82012-07-13 01:21:53 -040089{
90 tk->xtime_sec = ts->tv_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +010091 tk->tkr_mono.xtime_nsec = (u64)ts->tv_nsec << tk->tkr_mono.shift;
John Stultz1e75fa82012-07-13 01:21:53 -040092}
93
John Stultz7d489d12014-07-16 21:04:01 +000094static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
John Stultz1e75fa82012-07-13 01:21:53 -040095{
96 tk->xtime_sec += ts->tv_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +010097 tk->tkr_mono.xtime_nsec += (u64)ts->tv_nsec << tk->tkr_mono.shift;
John Stultz784ffcb2012-08-21 20:30:46 -040098 tk_normalize_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -040099}
John Stultz8fcce542011-11-14 11:46:39 -0800100
John Stultz7d489d12014-07-16 21:04:01 +0000101static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
John Stultz6d0ef902012-07-27 14:48:12 -0400102{
John Stultz7d489d12014-07-16 21:04:01 +0000103 struct timespec64 tmp;
John Stultz6d0ef902012-07-27 14:48:12 -0400104
105 /*
106 * Verify consistency of: offset_real = -wall_to_monotonic
107 * before modifying anything
108 */
John Stultz7d489d12014-07-16 21:04:01 +0000109 set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec,
John Stultz6d0ef902012-07-27 14:48:12 -0400110 -tk->wall_to_monotonic.tv_nsec);
John Stultz7d489d12014-07-16 21:04:01 +0000111 WARN_ON_ONCE(tk->offs_real.tv64 != timespec64_to_ktime(tmp).tv64);
John Stultz6d0ef902012-07-27 14:48:12 -0400112 tk->wall_to_monotonic = wtm;
John Stultz7d489d12014-07-16 21:04:01 +0000113 set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
114 tk->offs_real = timespec64_to_ktime(tmp);
John Stultz04005f62013-12-10 17:13:35 -0800115 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
John Stultz6d0ef902012-07-27 14:48:12 -0400116}
117
Thomas Gleixner47da70d2014-07-16 21:05:00 +0000118static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
John Stultz6d0ef902012-07-27 14:48:12 -0400119{
Thomas Gleixner47da70d2014-07-16 21:05:00 +0000120 tk->offs_boot = ktime_add(tk->offs_boot, delta);
John Stultz6d0ef902012-07-27 14:48:12 -0400121}
122
John Stultz02a37cc2017-06-08 16:44:20 -0700123/*
124 * tk_clock_read - atomic clocksource read() helper
125 *
126 * This helper is necessary to use in the read paths because, while the
127 * seqlock ensures we don't return a bad value while structures are updated,
128 * it doesn't protect from potential crashes. There is the possibility that
129 * the tkr's clocksource may change between the read reference, and the
130 * clock reference passed to the read function. This can cause crashes if
131 * the wrong clocksource is passed to the wrong read function.
132 * This isn't necessary to use when holding the timekeeper_lock or doing
133 * a read of the fast-timekeeper tkrs (which is protected by its own locking
134 * and update logic).
135 */
136static inline u64 tk_clock_read(struct tk_read_base *tkr)
137{
138 struct clocksource *clock = READ_ONCE(tkr->clock);
139
140 return clock->read(clock);
141}
142
John Stultz3c17ad12015-03-11 21:16:32 -0700143#ifdef CONFIG_DEBUG_TIMEKEEPING
John Stultz4ca22c22015-03-11 21:16:35 -0700144#define WARNING_FREQ (HZ*300) /* 5 minute rate-limiting */
John Stultz4ca22c22015-03-11 21:16:35 -0700145
John Stultz3c17ad12015-03-11 21:16:32 -0700146static void timekeeping_check_update(struct timekeeper *tk, cycle_t offset)
147{
148
Peter Zijlstra876e7882015-03-19 10:09:06 +0100149 cycle_t max_cycles = tk->tkr_mono.clock->max_cycles;
150 const char *name = tk->tkr_mono.clock->name;
John Stultz3c17ad12015-03-11 21:16:32 -0700151
152 if (offset > max_cycles) {
John Stultza558cd02015-03-11 21:16:33 -0700153 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 -0700154 offset, name, max_cycles);
John Stultza558cd02015-03-11 21:16:33 -0700155 printk_deferred(" timekeeping: Your kernel is sick, but tries to cope by capping time updates\n");
John Stultz3c17ad12015-03-11 21:16:32 -0700156 } else {
157 if (offset > (max_cycles >> 1)) {
Masanari Iidafc4fa6e2015-12-13 15:26:11 +0900158 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 -0700159 offset, name, max_cycles >> 1);
160 printk_deferred(" timekeeping: Your kernel is still fine, but is feeling a bit nervous\n");
161 }
162 }
John Stultz4ca22c22015-03-11 21:16:35 -0700163
John Stultz57d05a92015-05-13 16:04:47 -0700164 if (tk->underflow_seen) {
165 if (jiffies - tk->last_warning > WARNING_FREQ) {
John Stultz4ca22c22015-03-11 21:16:35 -0700166 printk_deferred("WARNING: Underflow in clocksource '%s' observed, time update ignored.\n", name);
167 printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
168 printk_deferred(" Your kernel is probably still fine.\n");
John Stultz57d05a92015-05-13 16:04:47 -0700169 tk->last_warning = jiffies;
John Stultz4ca22c22015-03-11 21:16:35 -0700170 }
John Stultz57d05a92015-05-13 16:04:47 -0700171 tk->underflow_seen = 0;
John Stultz4ca22c22015-03-11 21:16:35 -0700172 }
173
John Stultz57d05a92015-05-13 16:04:47 -0700174 if (tk->overflow_seen) {
175 if (jiffies - tk->last_warning > WARNING_FREQ) {
John Stultz4ca22c22015-03-11 21:16:35 -0700176 printk_deferred("WARNING: Overflow in clocksource '%s' observed, time update capped.\n", name);
177 printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
178 printk_deferred(" Your kernel is probably still fine.\n");
John Stultz57d05a92015-05-13 16:04:47 -0700179 tk->last_warning = jiffies;
John Stultz4ca22c22015-03-11 21:16:35 -0700180 }
John Stultz57d05a92015-05-13 16:04:47 -0700181 tk->overflow_seen = 0;
John Stultz4ca22c22015-03-11 21:16:35 -0700182 }
John Stultz3c17ad12015-03-11 21:16:32 -0700183}
John Stultza558cd02015-03-11 21:16:33 -0700184
185static inline cycle_t timekeeping_get_delta(struct tk_read_base *tkr)
186{
John Stultz57d05a92015-05-13 16:04:47 -0700187 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4ca22c22015-03-11 21:16:35 -0700188 cycle_t now, last, mask, max, delta;
189 unsigned int seq;
John Stultza558cd02015-03-11 21:16:33 -0700190
John Stultz4ca22c22015-03-11 21:16:35 -0700191 /*
192 * Since we're called holding a seqlock, the data may shift
193 * under us while we're doing the calculation. This can cause
194 * false positives, since we'd note a problem but throw the
195 * results away. So nest another seqlock here to atomically
196 * grab the points we are checking with.
197 */
198 do {
199 seq = read_seqcount_begin(&tk_core.seq);
John Stultz02a37cc2017-06-08 16:44:20 -0700200 now = tk_clock_read(tkr);
John Stultz4ca22c22015-03-11 21:16:35 -0700201 last = tkr->cycle_last;
202 mask = tkr->mask;
203 max = tkr->clock->max_cycles;
204 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultza558cd02015-03-11 21:16:33 -0700205
John Stultz4ca22c22015-03-11 21:16:35 -0700206 delta = clocksource_delta(now, last, mask);
John Stultza558cd02015-03-11 21:16:33 -0700207
John Stultz057b87e2015-03-11 21:16:34 -0700208 /*
209 * Try to catch underflows by checking if we are seeing small
210 * mask-relative negative values.
211 */
John Stultz4ca22c22015-03-11 21:16:35 -0700212 if (unlikely((~delta & mask) < (mask >> 3))) {
John Stultz57d05a92015-05-13 16:04:47 -0700213 tk->underflow_seen = 1;
John Stultz057b87e2015-03-11 21:16:34 -0700214 delta = 0;
John Stultz4ca22c22015-03-11 21:16:35 -0700215 }
John Stultz057b87e2015-03-11 21:16:34 -0700216
John Stultza558cd02015-03-11 21:16:33 -0700217 /* Cap delta value to the max_cycles values to avoid mult overflows */
John Stultz4ca22c22015-03-11 21:16:35 -0700218 if (unlikely(delta > max)) {
John Stultz57d05a92015-05-13 16:04:47 -0700219 tk->overflow_seen = 1;
John Stultza558cd02015-03-11 21:16:33 -0700220 delta = tkr->clock->max_cycles;
John Stultz4ca22c22015-03-11 21:16:35 -0700221 }
John Stultza558cd02015-03-11 21:16:33 -0700222
223 return delta;
224}
John Stultz3c17ad12015-03-11 21:16:32 -0700225#else
226static inline void timekeeping_check_update(struct timekeeper *tk, cycle_t offset)
227{
228}
John Stultza558cd02015-03-11 21:16:33 -0700229static inline cycle_t timekeeping_get_delta(struct tk_read_base *tkr)
230{
231 cycle_t cycle_now, delta;
232
233 /* read clocksource */
John Stultz02a37cc2017-06-08 16:44:20 -0700234 cycle_now = tk_clock_read(tkr);
John Stultza558cd02015-03-11 21:16:33 -0700235
236 /* calculate the delta since the last update_wall_time */
237 delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
238
239 return delta;
240}
John Stultz3c17ad12015-03-11 21:16:32 -0700241#endif
242
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200243/**
Yijing Wangd26e4fe2013-11-28 16:28:55 +0800244 * tk_setup_internals - Set up internals to use clocksource clock.
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200245 *
Yijing Wangd26e4fe2013-11-28 16:28:55 +0800246 * @tk: The target timekeeper to setup.
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200247 * @clock: Pointer to clocksource.
248 *
249 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
250 * pair and interval request.
251 *
252 * Unless you're the timekeeping code, you should not be using this!
253 */
John Stultzf726a692012-07-13 01:21:57 -0400254static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200255{
256 cycle_t interval;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700257 u64 tmp, ntpinterval;
John Stultz1e75fa82012-07-13 01:21:53 -0400258 struct clocksource *old_clock;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200259
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800260 ++tk->cs_was_changed_seq;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100261 old_clock = tk->tkr_mono.clock;
262 tk->tkr_mono.clock = clock;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100263 tk->tkr_mono.mask = clock->mask;
John Stultz02a37cc2017-06-08 16:44:20 -0700264 tk->tkr_mono.cycle_last = tk_clock_read(&tk->tkr_mono);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200265
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100266 tk->tkr_raw.clock = clock;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100267 tk->tkr_raw.mask = clock->mask;
268 tk->tkr_raw.cycle_last = tk->tkr_mono.cycle_last;
269
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200270 /* Do the ns -> cycle conversion first, using original mult */
271 tmp = NTP_INTERVAL_LENGTH;
272 tmp <<= clock->shift;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700273 ntpinterval = tmp;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200274 tmp += clock->mult/2;
275 do_div(tmp, clock->mult);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200276 if (tmp == 0)
277 tmp = 1;
278
279 interval = (cycle_t) tmp;
John Stultzf726a692012-07-13 01:21:57 -0400280 tk->cycle_interval = interval;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200281
282 /* Go back from cycles -> shifted ns */
John Stultzf726a692012-07-13 01:21:57 -0400283 tk->xtime_interval = (u64) interval * clock->mult;
284 tk->xtime_remainder = ntpinterval - tk->xtime_interval;
John Stultza53bfdd2017-06-08 16:44:21 -0700285 tk->raw_interval = interval * clock->mult;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200286
John Stultz1e75fa82012-07-13 01:21:53 -0400287 /* if changing clocks, convert xtime_nsec shift units */
288 if (old_clock) {
289 int shift_change = clock->shift - old_clock->shift;
John Stultz5311c742017-05-22 17:20:20 -0700290 if (shift_change < 0) {
Peter Zijlstra876e7882015-03-19 10:09:06 +0100291 tk->tkr_mono.xtime_nsec >>= -shift_change;
John Stultz5311c742017-05-22 17:20:20 -0700292 tk->tkr_raw.xtime_nsec >>= -shift_change;
293 } else {
Peter Zijlstra876e7882015-03-19 10:09:06 +0100294 tk->tkr_mono.xtime_nsec <<= shift_change;
John Stultz5311c742017-05-22 17:20:20 -0700295 tk->tkr_raw.xtime_nsec <<= shift_change;
296 }
John Stultz1e75fa82012-07-13 01:21:53 -0400297 }
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100298
Peter Zijlstra876e7882015-03-19 10:09:06 +0100299 tk->tkr_mono.shift = clock->shift;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100300 tk->tkr_raw.shift = clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200301
John Stultzf726a692012-07-13 01:21:57 -0400302 tk->ntp_error = 0;
303 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
John Stultz375f45b2014-04-23 20:53:29 -0700304 tk->ntp_tick = ntpinterval << tk->ntp_error_shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200305
306 /*
307 * The timekeeper keeps its own mult values for the currently
308 * active clocksource. These value will be adjusted via NTP
309 * to counteract clock drifting.
310 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100311 tk->tkr_mono.mult = clock->mult;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100312 tk->tkr_raw.mult = clock->mult;
John Stultzdc491592013-12-06 17:25:21 -0800313 tk->ntp_err_mult = 0;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200314}
john stultz85240702007-05-08 00:27:59 -0700315
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200316/* Timekeeper helper functions. */
Stephen Warren7b1f6202012-11-07 17:58:54 -0700317
318#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000319static u32 default_arch_gettimeoffset(void) { return 0; }
320u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset;
Stephen Warren7b1f6202012-11-07 17:58:54 -0700321#else
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000322static inline u32 arch_gettimeoffset(void) { return 0; }
Stephen Warren7b1f6202012-11-07 17:58:54 -0700323#endif
324
Thomas Gleixnerca229752016-12-08 20:49:32 +0000325static inline u64 timekeeping_delta_to_ns(struct tk_read_base *tkr,
Christopher S. Hall6bd58f02016-02-22 03:15:19 -0800326 cycle_t delta)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200327{
Thomas Gleixnerca229752016-12-08 20:49:32 +0000328 u64 nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200329
Christopher S. Hall6bd58f02016-02-22 03:15:19 -0800330 nsec = delta * tkr->mult + tkr->xtime_nsec;
331 nsec >>= tkr->shift;
John Stultzf2a5a082012-07-13 01:21:55 -0400332
Stephen Warren7b1f6202012-11-07 17:58:54 -0700333 /* If arch requires, add in get_arch_timeoffset() */
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000334 return nsec + arch_gettimeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200335}
336
Christopher S. Hall6bd58f02016-02-22 03:15:19 -0800337static inline s64 timekeeping_get_ns(struct tk_read_base *tkr)
338{
339 cycle_t delta;
340
341 delta = timekeeping_get_delta(tkr);
342 return timekeeping_delta_to_ns(tkr, delta);
343}
344
345static inline s64 timekeeping_cycles_to_ns(struct tk_read_base *tkr,
346 cycle_t cycles)
347{
348 cycle_t delta;
349
350 /* calculate the delta since the last update_wall_time */
351 delta = clocksource_delta(cycles, tkr->cycle_last, tkr->mask);
352 return timekeeping_delta_to_ns(tkr, delta);
353}
354
Thomas Gleixner4396e052014-07-16 21:05:23 +0000355/**
356 * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
Rafael J. Wysockiaffe3e82015-02-11 05:01:52 +0100357 * @tkr: Timekeeping readout base from which we take the update
Thomas Gleixner4396e052014-07-16 21:05:23 +0000358 *
359 * We want to use this from any context including NMI and tracing /
360 * instrumenting the timekeeping code itself.
361 *
Peter Zijlstra6695b922015-05-27 11:09:36 +0930362 * Employ the latch technique; see @raw_write_seqcount_latch.
Thomas Gleixner4396e052014-07-16 21:05:23 +0000363 *
364 * So if a NMI hits the update of base[0] then it will use base[1]
365 * which is still consistent. In the worst case this can result is a
366 * slightly wrong timestamp (a few nanoseconds). See
367 * @ktime_get_mono_fast_ns.
368 */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100369static void update_fast_timekeeper(struct tk_read_base *tkr, struct tk_fast *tkf)
Thomas Gleixner4396e052014-07-16 21:05:23 +0000370{
Peter Zijlstra4498e742015-03-19 09:36:19 +0100371 struct tk_read_base *base = tkf->base;
Thomas Gleixner4396e052014-07-16 21:05:23 +0000372
373 /* Force readers off to base[1] */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100374 raw_write_seqcount_latch(&tkf->seq);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000375
376 /* Update base[0] */
Rafael J. Wysockiaffe3e82015-02-11 05:01:52 +0100377 memcpy(base, tkr, sizeof(*base));
Thomas Gleixner4396e052014-07-16 21:05:23 +0000378
379 /* Force readers back to base[0] */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100380 raw_write_seqcount_latch(&tkf->seq);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000381
382 /* Update base[1] */
383 memcpy(base + 1, base, sizeof(*base));
384}
385
386/**
387 * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic
388 *
389 * This timestamp is not guaranteed to be monotonic across an update.
390 * The timestamp is calculated by:
391 *
392 * now = base_mono + clock_delta * slope
393 *
394 * So if the update lowers the slope, readers who are forced to the
395 * not yet updated second array are still using the old steeper slope.
396 *
397 * tmono
398 * ^
399 * | o n
400 * | o n
401 * | u
402 * | o
403 * |o
404 * |12345678---> reader order
405 *
406 * o = old slope
407 * u = update
408 * n = new slope
409 *
410 * So reader 6 will observe time going backwards versus reader 5.
411 *
412 * While other CPUs are likely to be able observe that, the only way
413 * for a CPU local observation is when an NMI hits in the middle of
414 * the update. Timestamps taken from that NMI context might be ahead
415 * of the following timestamps. Callers need to be aware of that and
416 * deal with it.
417 */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100418static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf)
Thomas Gleixner4396e052014-07-16 21:05:23 +0000419{
420 struct tk_read_base *tkr;
421 unsigned int seq;
422 u64 now;
423
424 do {
Peter Zijlstra7fc26322015-05-27 11:09:36 +0930425 seq = raw_read_seqcount_latch(&tkf->seq);
Peter Zijlstra4498e742015-03-19 09:36:19 +0100426 tkr = tkf->base + (seq & 0x01);
John Stultz27727df2016-08-23 16:08:21 -0700427 now = ktime_to_ns(tkr->base);
428
John Stultz58bfea92016-10-04 19:55:48 -0700429 now += timekeeping_delta_to_ns(tkr,
430 clocksource_delta(
John Stultz02a37cc2017-06-08 16:44:20 -0700431 tk_clock_read(tkr),
John Stultz58bfea92016-10-04 19:55:48 -0700432 tkr->cycle_last,
433 tkr->mask));
Peter Zijlstra4498e742015-03-19 09:36:19 +0100434 } while (read_seqcount_retry(&tkf->seq, seq));
Thomas Gleixner4396e052014-07-16 21:05:23 +0000435
Thomas Gleixner4396e052014-07-16 21:05:23 +0000436 return now;
437}
Peter Zijlstra4498e742015-03-19 09:36:19 +0100438
439u64 ktime_get_mono_fast_ns(void)
440{
441 return __ktime_get_fast_ns(&tk_fast_mono);
442}
Thomas Gleixner4396e052014-07-16 21:05:23 +0000443EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
444
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100445u64 ktime_get_raw_fast_ns(void)
446{
447 return __ktime_get_fast_ns(&tk_fast_raw);
448}
449EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns);
450
Joel Fernandes72408a62016-11-28 14:35:22 -0800451/**
452 * ktime_get_boot_fast_ns - NMI safe and fast access to boot clock.
453 *
454 * To keep it NMI safe since we're accessing from tracing, we're not using a
455 * separate timekeeper with updates to monotonic clock and boot offset
456 * protected with seqlocks. This has the following minor side effects:
457 *
458 * (1) Its possible that a timestamp be taken after the boot offset is updated
459 * but before the timekeeper is updated. If this happens, the new boot offset
460 * is added to the old timekeeping making the clock appear to update slightly
461 * earlier:
462 * CPU 0 CPU 1
463 * timekeeping_inject_sleeptime64()
464 * __timekeeping_inject_sleeptime(tk, delta);
465 * timestamp();
466 * timekeeping_update(tk, TK_CLEAR_NTP...);
467 *
468 * (2) On 32-bit systems, the 64-bit boot offset (tk->offs_boot) may be
469 * partially updated. Since the tk->offs_boot update is a rare event, this
470 * should be a rare occurrence which postprocessing should be able to handle.
471 */
472u64 notrace ktime_get_boot_fast_ns(void)
473{
474 struct timekeeper *tk = &tk_core.timekeeper;
475
476 return (ktime_get_mono_fast_ns() + ktime_to_ns(tk->offs_boot));
477}
478EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns);
479
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100480/* Suspend-time cycles value for halted fast timekeeper. */
481static cycle_t cycles_at_suspend;
482
483static cycle_t dummy_clock_read(struct clocksource *cs)
484{
485 return cycles_at_suspend;
486}
487
John Stultz02a37cc2017-06-08 16:44:20 -0700488static struct clocksource dummy_clock = {
489 .read = dummy_clock_read,
490};
491
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100492/**
493 * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource.
494 * @tk: Timekeeper to snapshot.
495 *
496 * It generally is unsafe to access the clocksource after timekeeping has been
497 * suspended, so take a snapshot of the readout base of @tk and use it as the
498 * fast timekeeper's readout base while suspended. It will return the same
499 * number of cycles every time until timekeeping is resumed at which time the
500 * proper readout base for the fast timekeeper will be restored automatically.
501 */
502static void halt_fast_timekeeper(struct timekeeper *tk)
503{
504 static struct tk_read_base tkr_dummy;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100505 struct tk_read_base *tkr = &tk->tkr_mono;
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100506
507 memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
John Stultz02a37cc2017-06-08 16:44:20 -0700508 cycles_at_suspend = tk_clock_read(tkr);
509 tkr_dummy.clock = &dummy_clock;
Peter Zijlstra4498e742015-03-19 09:36:19 +0100510 update_fast_timekeeper(&tkr_dummy, &tk_fast_mono);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100511
512 tkr = &tk->tkr_raw;
513 memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
John Stultz02a37cc2017-06-08 16:44:20 -0700514 tkr_dummy.clock = &dummy_clock;
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100515 update_fast_timekeeper(&tkr_dummy, &tk_fast_raw);
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100516}
517
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000518#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
519
520static inline void update_vsyscall(struct timekeeper *tk)
521{
John Stultz0680eb12014-08-13 12:47:14 -0700522 struct timespec xt, wm;
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000523
John Stultze2dff1e2014-07-23 14:35:39 -0700524 xt = timespec64_to_timespec(tk_xtime(tk));
John Stultz0680eb12014-08-13 12:47:14 -0700525 wm = timespec64_to_timespec(tk->wall_to_monotonic);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100526 update_vsyscall_old(&xt, &wm, tk->tkr_mono.clock, tk->tkr_mono.mult,
527 tk->tkr_mono.cycle_last);
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000528}
529
530static inline void old_vsyscall_fixup(struct timekeeper *tk)
531{
532 s64 remainder;
533
534 /*
535 * Store only full nanoseconds into xtime_nsec after rounding
536 * it up and add the remainder to the error difference.
537 * XXX - This is necessary to avoid small 1ns inconsistnecies caused
538 * by truncating the remainder in vsyscalls. However, it causes
539 * additional work to be done in timekeeping_adjust(). Once
540 * the vsyscall implementations are converted to use xtime_nsec
541 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
542 * users are removed, this can be killed.
543 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100544 remainder = tk->tkr_mono.xtime_nsec & ((1ULL << tk->tkr_mono.shift) - 1);
Thomas Graziadei0209b932016-05-31 15:06:06 +0200545 if (remainder != 0) {
546 tk->tkr_mono.xtime_nsec -= remainder;
547 tk->tkr_mono.xtime_nsec += 1ULL << tk->tkr_mono.shift;
548 tk->ntp_error += remainder << tk->ntp_error_shift;
549 tk->ntp_error -= (1ULL << tk->tkr_mono.shift) << tk->ntp_error_shift;
550 }
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000551}
552#else
553#define old_vsyscall_fixup(tk)
554#endif
555
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200556static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
557
David Vrabel780427f2013-06-27 11:35:46 +0100558static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200559{
David Vrabel780427f2013-06-27 11:35:46 +0100560 raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200561}
562
563/**
564 * pvclock_gtod_register_notifier - register a pvclock timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200565 */
566int pvclock_gtod_register_notifier(struct notifier_block *nb)
567{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000568 struct timekeeper *tk = &tk_core.timekeeper;
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200569 unsigned long flags;
570 int ret;
571
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000572 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200573 ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
David Vrabel780427f2013-06-27 11:35:46 +0100574 update_pvclock_gtod(tk, true);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000575 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200576
577 return ret;
578}
579EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
580
581/**
582 * pvclock_gtod_unregister_notifier - unregister a pvclock
583 * timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200584 */
585int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
586{
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200587 unsigned long flags;
588 int ret;
589
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000590 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200591 ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000592 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200593
594 return ret;
595}
596EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
597
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000598/*
John Stultz833f32d2015-06-11 15:54:55 -0700599 * tk_update_leap_state - helper to update the next_leap_ktime
600 */
601static inline void tk_update_leap_state(struct timekeeper *tk)
602{
603 tk->next_leap_ktime = ntp_get_next_leap();
604 if (tk->next_leap_ktime.tv64 != KTIME_MAX)
605 /* Convert to monotonic time */
606 tk->next_leap_ktime = ktime_sub(tk->next_leap_ktime, tk->offs_real);
607}
608
609/*
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000610 * Update the ktime_t based scalar nsec members of the timekeeper
611 */
612static inline void tk_update_ktime_data(struct timekeeper *tk)
613{
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530614 u64 seconds;
615 u32 nsec;
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000616
617 /*
618 * The xtime based monotonic readout is:
619 * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();
620 * The ktime based monotonic readout is:
621 * nsec = base_mono + now();
622 * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
623 */
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530624 seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
625 nsec = (u32) tk->wall_to_monotonic.tv_nsec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100626 tk->tkr_mono.base = ns_to_ktime(seconds * NSEC_PER_SEC + nsec);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000627
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530628 /*
629 * The sum of the nanoseconds portions of xtime and
630 * wall_to_monotonic can be greater/equal one second. Take
631 * this into account before updating tk->ktime_sec.
632 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100633 nsec += (u32)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530634 if (nsec >= NSEC_PER_SEC)
635 seconds++;
636 tk->ktime_sec = seconds;
John Stultz5311c742017-05-22 17:20:20 -0700637
638 /* Update the monotonic raw base */
John Stultzd2c57b62017-08-25 15:57:04 -0700639 tk->tkr_raw.base = ns_to_ktime(tk->raw_sec * NSEC_PER_SEC);
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000640}
641
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000642/* must hold timekeeper_lock */
David Vrabel04397fe92013-06-27 11:35:45 +0100643static void timekeeping_update(struct timekeeper *tk, unsigned int action)
Thomas Gleixnercc062682011-11-13 23:19:49 +0000644{
David Vrabel04397fe92013-06-27 11:35:45 +0100645 if (action & TK_CLEAR_NTP) {
John Stultzf726a692012-07-13 01:21:57 -0400646 tk->ntp_error = 0;
Thomas Gleixnercc062682011-11-13 23:19:49 +0000647 ntp_clear();
648 }
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000649
John Stultz833f32d2015-06-11 15:54:55 -0700650 tk_update_leap_state(tk);
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000651 tk_update_ktime_data(tk);
652
Thomas Gleixner9bf24192014-09-06 12:24:49 +0200653 update_vsyscall(tk);
654 update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
655
Peter Zijlstra4498e742015-03-19 09:36:19 +0100656 update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100657 update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw);
Thomas Gleixner868a3e92015-04-14 21:08:37 +0000658
659 if (action & TK_CLOCK_WAS_SET)
660 tk->clock_was_set_seq++;
John Stultzd1518322015-06-11 15:54:53 -0700661 /*
662 * The mirroring of the data to the shadow-timekeeper needs
663 * to happen last here to ensure we don't over-write the
664 * timekeeper structure on the next update with stale data
665 */
666 if (action & TK_MIRROR)
667 memcpy(&shadow_timekeeper, &tk_core.timekeeper,
668 sizeof(tk_core.timekeeper));
Thomas Gleixnercc062682011-11-13 23:19:49 +0000669}
670
john stultz85240702007-05-08 00:27:59 -0700671/**
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200672 * timekeeping_forward_now - update clock to the current time
john stultz85240702007-05-08 00:27:59 -0700673 *
Roman Zippel9a055112008-08-20 16:37:28 -0700674 * Forward the current clock to update its state since the last call to
675 * update_wall_time(). This is useful before significant clock changes,
676 * as it avoids having to deal with this time offset explicitly.
john stultz85240702007-05-08 00:27:59 -0700677 */
John Stultzf726a692012-07-13 01:21:57 -0400678static void timekeeping_forward_now(struct timekeeper *tk)
john stultz85240702007-05-08 00:27:59 -0700679{
Thomas Gleixner3a978372014-07-16 21:05:10 +0000680 cycle_t cycle_now, delta;
john stultz85240702007-05-08 00:27:59 -0700681
John Stultz02a37cc2017-06-08 16:44:20 -0700682 cycle_now = tk_clock_read(&tk->tkr_mono);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100683 delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
684 tk->tkr_mono.cycle_last = cycle_now;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100685 tk->tkr_raw.cycle_last = cycle_now;
john stultz85240702007-05-08 00:27:59 -0700686
Peter Zijlstra876e7882015-03-19 10:09:06 +0100687 tk->tkr_mono.xtime_nsec += delta * tk->tkr_mono.mult;
john stultz7d275582009-05-01 13:10:26 -0700688
Stephen Warren7b1f6202012-11-07 17:58:54 -0700689 /* If arch requires, add in get_arch_timeoffset() */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100690 tk->tkr_mono.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_mono.shift;
john stultz7d275582009-05-01 13:10:26 -0700691
John Stultz2d422442008-08-20 16:37:30 -0700692
John Stultz5311c742017-05-22 17:20:20 -0700693 tk->tkr_raw.xtime_nsec += delta * tk->tkr_raw.mult;
694
695 /* If arch requires, add in get_arch_timeoffset() */
696 tk->tkr_raw.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_raw.shift;
697
698 tk_normalize_xtime(tk);
john stultz85240702007-05-08 00:27:59 -0700699}
700
701/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000702 * __getnstimeofday64 - Returns the time of day in a timespec64.
john stultz85240702007-05-08 00:27:59 -0700703 * @ts: pointer to the timespec to be set
704 *
Kees Cook1e817fb2012-11-19 10:26:16 -0800705 * Updates the time of day in the timespec.
706 * Returns 0 on success, or -ve when suspended (timespec will be undefined).
john stultz85240702007-05-08 00:27:59 -0700707 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000708int __getnstimeofday64(struct timespec64 *ts)
john stultz85240702007-05-08 00:27:59 -0700709{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000710 struct timekeeper *tk = &tk_core.timekeeper;
john stultz85240702007-05-08 00:27:59 -0700711 unsigned long seq;
John Stultz1e75fa82012-07-13 01:21:53 -0400712 s64 nsecs = 0;
john stultz85240702007-05-08 00:27:59 -0700713
714 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000715 seq = read_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -0700716
John Stultz4e250fd2012-07-27 14:48:13 -0400717 ts->tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100718 nsecs = timekeeping_get_ns(&tk->tkr_mono);
john stultz85240702007-05-08 00:27:59 -0700719
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000720 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz85240702007-05-08 00:27:59 -0700721
John Stultzec145ba2012-09-11 19:26:03 -0400722 ts->tv_nsec = 0;
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000723 timespec64_add_ns(ts, nsecs);
Kees Cook1e817fb2012-11-19 10:26:16 -0800724
725 /*
726 * Do not bail out early, in case there were callers still using
727 * the value, even in the face of the WARN_ON.
728 */
729 if (unlikely(timekeeping_suspended))
730 return -EAGAIN;
731 return 0;
732}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000733EXPORT_SYMBOL(__getnstimeofday64);
Kees Cook1e817fb2012-11-19 10:26:16 -0800734
735/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000736 * getnstimeofday64 - Returns the time of day in a timespec64.
John Stultz5322e4c2014-11-07 13:13:04 -0800737 * @ts: pointer to the timespec64 to be set
Kees Cook1e817fb2012-11-19 10:26:16 -0800738 *
John Stultz5322e4c2014-11-07 13:13:04 -0800739 * Returns the time of day in a timespec64 (WARN if suspended).
Kees Cook1e817fb2012-11-19 10:26:16 -0800740 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000741void getnstimeofday64(struct timespec64 *ts)
Kees Cook1e817fb2012-11-19 10:26:16 -0800742{
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000743 WARN_ON(__getnstimeofday64(ts));
john stultz85240702007-05-08 00:27:59 -0700744}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000745EXPORT_SYMBOL(getnstimeofday64);
john stultz85240702007-05-08 00:27:59 -0700746
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200747ktime_t ktime_get(void)
748{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000749 struct timekeeper *tk = &tk_core.timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200750 unsigned int seq;
Thomas Gleixnera016a5b2014-07-16 21:04:12 +0000751 ktime_t base;
752 s64 nsecs;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200753
754 WARN_ON(timekeeping_suspended);
755
756 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000757 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100758 base = tk->tkr_mono.base;
759 nsecs = timekeeping_get_ns(&tk->tkr_mono);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200760
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000761 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz24e4a8c2014-07-16 21:03:53 +0000762
Thomas Gleixnera016a5b2014-07-16 21:04:12 +0000763 return ktime_add_ns(base, nsecs);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200764}
765EXPORT_SYMBOL_GPL(ktime_get);
766
Harald Geyer6374f912015-04-07 11:12:35 +0000767u32 ktime_get_resolution_ns(void)
768{
769 struct timekeeper *tk = &tk_core.timekeeper;
770 unsigned int seq;
771 u32 nsecs;
772
773 WARN_ON(timekeeping_suspended);
774
775 do {
776 seq = read_seqcount_begin(&tk_core.seq);
777 nsecs = tk->tkr_mono.mult >> tk->tkr_mono.shift;
778 } while (read_seqcount_retry(&tk_core.seq, seq));
779
780 return nsecs;
781}
782EXPORT_SYMBOL_GPL(ktime_get_resolution_ns);
783
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000784static ktime_t *offsets[TK_OFFS_MAX] = {
785 [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real,
786 [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot,
787 [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai,
788};
789
790ktime_t ktime_get_with_offset(enum tk_offsets offs)
791{
792 struct timekeeper *tk = &tk_core.timekeeper;
793 unsigned int seq;
794 ktime_t base, *offset = offsets[offs];
795 s64 nsecs;
796
797 WARN_ON(timekeeping_suspended);
798
799 do {
800 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100801 base = ktime_add(tk->tkr_mono.base, *offset);
802 nsecs = timekeeping_get_ns(&tk->tkr_mono);
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000803
804 } while (read_seqcount_retry(&tk_core.seq, seq));
805
806 return ktime_add_ns(base, nsecs);
807
808}
809EXPORT_SYMBOL_GPL(ktime_get_with_offset);
810
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200811/**
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000812 * ktime_mono_to_any() - convert mononotic time to any other time
813 * @tmono: time to convert.
814 * @offs: which offset to use
815 */
816ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
817{
818 ktime_t *offset = offsets[offs];
819 unsigned long seq;
820 ktime_t tconv;
821
822 do {
823 seq = read_seqcount_begin(&tk_core.seq);
824 tconv = ktime_add(tmono, *offset);
825 } while (read_seqcount_retry(&tk_core.seq, seq));
826
827 return tconv;
828}
829EXPORT_SYMBOL_GPL(ktime_mono_to_any);
830
831/**
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000832 * ktime_get_raw - Returns the raw monotonic time in ktime_t format
833 */
834ktime_t ktime_get_raw(void)
835{
836 struct timekeeper *tk = &tk_core.timekeeper;
837 unsigned int seq;
838 ktime_t base;
839 s64 nsecs;
840
841 do {
842 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100843 base = tk->tkr_raw.base;
844 nsecs = timekeeping_get_ns(&tk->tkr_raw);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000845
846 } while (read_seqcount_retry(&tk_core.seq, seq));
847
848 return ktime_add_ns(base, nsecs);
849}
850EXPORT_SYMBOL_GPL(ktime_get_raw);
851
852/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000853 * ktime_get_ts64 - get the monotonic clock in timespec64 format
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200854 * @ts: pointer to timespec variable
855 *
856 * The function calculates the monotonic clock from the realtime
857 * clock and the wall_to_monotonic offset and stores the result
John Stultz5322e4c2014-11-07 13:13:04 -0800858 * in normalized timespec64 format in the variable pointed to by @ts.
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200859 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000860void ktime_get_ts64(struct timespec64 *ts)
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200861{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000862 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000863 struct timespec64 tomono;
John Stultzec145ba2012-09-11 19:26:03 -0400864 s64 nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200865 unsigned int seq;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200866
867 WARN_ON(timekeeping_suspended);
868
869 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000870 seq = read_seqcount_begin(&tk_core.seq);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000871 ts->tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100872 nsec = timekeeping_get_ns(&tk->tkr_mono);
John Stultz4e250fd2012-07-27 14:48:13 -0400873 tomono = tk->wall_to_monotonic;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200874
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000875 } while (read_seqcount_retry(&tk_core.seq, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200876
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000877 ts->tv_sec += tomono.tv_sec;
878 ts->tv_nsec = 0;
879 timespec64_add_ns(ts, nsec + tomono.tv_nsec);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200880}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000881EXPORT_SYMBOL_GPL(ktime_get_ts64);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200882
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530883/**
884 * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC
885 *
886 * Returns the seconds portion of CLOCK_MONOTONIC with a single non
887 * serialized read. tk->ktime_sec is of type 'unsigned long' so this
888 * works on both 32 and 64 bit systems. On 32 bit systems the readout
889 * covers ~136 years of uptime which should be enough to prevent
890 * premature wrap arounds.
891 */
892time64_t ktime_get_seconds(void)
893{
894 struct timekeeper *tk = &tk_core.timekeeper;
895
896 WARN_ON(timekeeping_suspended);
897 return tk->ktime_sec;
898}
899EXPORT_SYMBOL_GPL(ktime_get_seconds);
900
Heena Sirwanidbe7aa62014-10-29 16:01:50 +0530901/**
902 * ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME
903 *
904 * Returns the wall clock seconds since 1970. This replaces the
905 * get_seconds() interface which is not y2038 safe on 32bit systems.
906 *
907 * For 64bit systems the fast access to tk->xtime_sec is preserved. On
908 * 32bit systems the access must be protected with the sequence
909 * counter to provide "atomic" access to the 64bit tk->xtime_sec
910 * value.
911 */
912time64_t ktime_get_real_seconds(void)
913{
914 struct timekeeper *tk = &tk_core.timekeeper;
915 time64_t seconds;
916 unsigned int seq;
917
918 if (IS_ENABLED(CONFIG_64BIT))
919 return tk->xtime_sec;
920
921 do {
922 seq = read_seqcount_begin(&tk_core.seq);
923 seconds = tk->xtime_sec;
924
925 } while (read_seqcount_retry(&tk_core.seq, seq));
926
927 return seconds;
928}
929EXPORT_SYMBOL_GPL(ktime_get_real_seconds);
930
DengChaodee36652015-12-13 12:24:18 +0800931/**
932 * __ktime_get_real_seconds - The same as ktime_get_real_seconds
933 * but without the sequence counter protect. This internal function
934 * is called just when timekeeping lock is already held.
935 */
936time64_t __ktime_get_real_seconds(void)
937{
938 struct timekeeper *tk = &tk_core.timekeeper;
939
940 return tk->xtime_sec;
941}
942
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800943/**
944 * ktime_get_snapshot - snapshots the realtime/monotonic raw clocks with counter
945 * @systime_snapshot: pointer to struct receiving the system time snapshot
946 */
947void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
948{
949 struct timekeeper *tk = &tk_core.timekeeper;
950 unsigned long seq;
951 ktime_t base_raw;
952 ktime_t base_real;
953 s64 nsec_raw;
954 s64 nsec_real;
955 cycle_t now;
956
Christopher S. Hallba266212016-02-22 03:15:21 -0800957 WARN_ON_ONCE(timekeeping_suspended);
958
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800959 do {
960 seq = read_seqcount_begin(&tk_core.seq);
John Stultz02a37cc2017-06-08 16:44:20 -0700961 now = tk_clock_read(&tk->tkr_mono);
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800962 systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq;
963 systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq;
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800964 base_real = ktime_add(tk->tkr_mono.base,
965 tk_core.timekeeper.offs_real);
966 base_raw = tk->tkr_raw.base;
967 nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, now);
968 nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw, now);
969 } while (read_seqcount_retry(&tk_core.seq, seq));
970
971 systime_snapshot->cycles = now;
972 systime_snapshot->real = ktime_add_ns(base_real, nsec_real);
973 systime_snapshot->raw = ktime_add_ns(base_raw, nsec_raw);
974}
975EXPORT_SYMBOL_GPL(ktime_get_snapshot);
DengChaodee36652015-12-13 12:24:18 +0800976
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800977/* Scale base by mult/div checking for overflow */
978static int scale64_check_overflow(u64 mult, u64 div, u64 *base)
979{
980 u64 tmp, rem;
981
982 tmp = div64_u64_rem(*base, div, &rem);
983
984 if (((int)sizeof(u64)*8 - fls64(mult) < fls64(tmp)) ||
985 ((int)sizeof(u64)*8 - fls64(mult) < fls64(rem)))
986 return -EOVERFLOW;
987 tmp *= mult;
988 rem *= mult;
989
990 do_div(rem, div);
991 *base = tmp + rem;
992 return 0;
993}
994
995/**
996 * adjust_historical_crosststamp - adjust crosstimestamp previous to current interval
997 * @history: Snapshot representing start of history
998 * @partial_history_cycles: Cycle offset into history (fractional part)
999 * @total_history_cycles: Total history length in cycles
1000 * @discontinuity: True indicates clock was set on history period
1001 * @ts: Cross timestamp that should be adjusted using
1002 * partial/total ratio
1003 *
1004 * Helper function used by get_device_system_crosststamp() to correct the
1005 * crosstimestamp corresponding to the start of the current interval to the
1006 * system counter value (timestamp point) provided by the driver. The
1007 * total_history_* quantities are the total history starting at the provided
1008 * reference point and ending at the start of the current interval. The cycle
1009 * count between the driver timestamp point and the start of the current
1010 * interval is partial_history_cycles.
1011 */
1012static int adjust_historical_crosststamp(struct system_time_snapshot *history,
1013 cycle_t partial_history_cycles,
1014 cycle_t total_history_cycles,
1015 bool discontinuity,
1016 struct system_device_crosststamp *ts)
1017{
1018 struct timekeeper *tk = &tk_core.timekeeper;
1019 u64 corr_raw, corr_real;
1020 bool interp_forward;
1021 int ret;
1022
1023 if (total_history_cycles == 0 || partial_history_cycles == 0)
1024 return 0;
1025
1026 /* Interpolate shortest distance from beginning or end of history */
1027 interp_forward = partial_history_cycles > total_history_cycles/2 ?
1028 true : false;
1029 partial_history_cycles = interp_forward ?
1030 total_history_cycles - partial_history_cycles :
1031 partial_history_cycles;
1032
1033 /*
1034 * Scale the monotonic raw time delta by:
1035 * partial_history_cycles / total_history_cycles
1036 */
1037 corr_raw = (u64)ktime_to_ns(
1038 ktime_sub(ts->sys_monoraw, history->raw));
1039 ret = scale64_check_overflow(partial_history_cycles,
1040 total_history_cycles, &corr_raw);
1041 if (ret)
1042 return ret;
1043
1044 /*
1045 * If there is a discontinuity in the history, scale monotonic raw
1046 * correction by:
1047 * mult(real)/mult(raw) yielding the realtime correction
1048 * Otherwise, calculate the realtime correction similar to monotonic
1049 * raw calculation
1050 */
1051 if (discontinuity) {
1052 corr_real = mul_u64_u32_div
1053 (corr_raw, tk->tkr_mono.mult, tk->tkr_raw.mult);
1054 } else {
1055 corr_real = (u64)ktime_to_ns(
1056 ktime_sub(ts->sys_realtime, history->real));
1057 ret = scale64_check_overflow(partial_history_cycles,
1058 total_history_cycles, &corr_real);
1059 if (ret)
1060 return ret;
1061 }
1062
1063 /* Fixup monotonic raw and real time time values */
1064 if (interp_forward) {
1065 ts->sys_monoraw = ktime_add_ns(history->raw, corr_raw);
1066 ts->sys_realtime = ktime_add_ns(history->real, corr_real);
1067 } else {
1068 ts->sys_monoraw = ktime_sub_ns(ts->sys_monoraw, corr_raw);
1069 ts->sys_realtime = ktime_sub_ns(ts->sys_realtime, corr_real);
1070 }
1071
1072 return 0;
1073}
1074
1075/*
1076 * cycle_between - true if test occurs chronologically between before and after
1077 */
1078static bool cycle_between(cycle_t before, cycle_t test, cycle_t after)
1079{
1080 if (test > before && test < after)
1081 return true;
1082 if (test < before && before > after)
1083 return true;
1084 return false;
1085}
1086
john stultz85240702007-05-08 00:27:59 -07001087/**
Christopher S. Hall8006c242016-02-22 03:15:22 -08001088 * get_device_system_crosststamp - Synchronously capture system/device timestamp
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001089 * @get_time_fn: Callback to get simultaneous device time and
Christopher S. Hall8006c242016-02-22 03:15:22 -08001090 * system counter from the device driver
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001091 * @ctx: Context passed to get_time_fn()
1092 * @history_begin: Historical reference point used to interpolate system
1093 * time when counter provided by the driver is before the current interval
Christopher S. Hall8006c242016-02-22 03:15:22 -08001094 * @xtstamp: Receives simultaneously captured system and device time
1095 *
1096 * Reads a timestamp from a device and correlates it to system time
1097 */
1098int get_device_system_crosststamp(int (*get_time_fn)
1099 (ktime_t *device_time,
1100 struct system_counterval_t *sys_counterval,
1101 void *ctx),
1102 void *ctx,
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001103 struct system_time_snapshot *history_begin,
Christopher S. Hall8006c242016-02-22 03:15:22 -08001104 struct system_device_crosststamp *xtstamp)
1105{
1106 struct system_counterval_t system_counterval;
1107 struct timekeeper *tk = &tk_core.timekeeper;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001108 cycle_t cycles, now, interval_start;
Ingo Molnar64362572016-03-08 11:09:53 +01001109 unsigned int clock_was_set_seq = 0;
Christopher S. Hall8006c242016-02-22 03:15:22 -08001110 ktime_t base_real, base_raw;
1111 s64 nsec_real, nsec_raw;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001112 u8 cs_was_changed_seq;
Christopher S. Hall8006c242016-02-22 03:15:22 -08001113 unsigned long seq;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001114 bool do_interp;
Christopher S. Hall8006c242016-02-22 03:15:22 -08001115 int ret;
1116
1117 do {
1118 seq = read_seqcount_begin(&tk_core.seq);
1119 /*
1120 * Try to synchronously capture device time and a system
1121 * counter value calling back into the device driver
1122 */
1123 ret = get_time_fn(&xtstamp->device, &system_counterval, ctx);
1124 if (ret)
1125 return ret;
1126
1127 /*
1128 * Verify that the clocksource associated with the captured
1129 * system counter value is the same as the currently installed
1130 * timekeeper clocksource
1131 */
1132 if (tk->tkr_mono.clock != system_counterval.cs)
1133 return -ENODEV;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001134 cycles = system_counterval.cycles;
1135
1136 /*
1137 * Check whether the system counter value provided by the
1138 * device driver is on the current timekeeping interval.
1139 */
John Stultz02a37cc2017-06-08 16:44:20 -07001140 now = tk_clock_read(&tk->tkr_mono);
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001141 interval_start = tk->tkr_mono.cycle_last;
1142 if (!cycle_between(interval_start, cycles, now)) {
1143 clock_was_set_seq = tk->clock_was_set_seq;
1144 cs_was_changed_seq = tk->cs_was_changed_seq;
1145 cycles = interval_start;
1146 do_interp = true;
1147 } else {
1148 do_interp = false;
1149 }
Christopher S. Hall8006c242016-02-22 03:15:22 -08001150
1151 base_real = ktime_add(tk->tkr_mono.base,
1152 tk_core.timekeeper.offs_real);
1153 base_raw = tk->tkr_raw.base;
1154
1155 nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono,
1156 system_counterval.cycles);
1157 nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw,
1158 system_counterval.cycles);
1159 } while (read_seqcount_retry(&tk_core.seq, seq));
1160
1161 xtstamp->sys_realtime = ktime_add_ns(base_real, nsec_real);
1162 xtstamp->sys_monoraw = ktime_add_ns(base_raw, nsec_raw);
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001163
1164 /*
1165 * Interpolate if necessary, adjusting back from the start of the
1166 * current interval
1167 */
1168 if (do_interp) {
1169 cycle_t partial_history_cycles, total_history_cycles;
1170 bool discontinuity;
1171
1172 /*
1173 * Check that the counter value occurs after the provided
1174 * history reference and that the history doesn't cross a
1175 * clocksource change
1176 */
1177 if (!history_begin ||
1178 !cycle_between(history_begin->cycles,
1179 system_counterval.cycles, cycles) ||
1180 history_begin->cs_was_changed_seq != cs_was_changed_seq)
1181 return -EINVAL;
1182 partial_history_cycles = cycles - system_counterval.cycles;
1183 total_history_cycles = cycles - history_begin->cycles;
1184 discontinuity =
1185 history_begin->clock_was_set_seq != clock_was_set_seq;
1186
1187 ret = adjust_historical_crosststamp(history_begin,
1188 partial_history_cycles,
1189 total_history_cycles,
1190 discontinuity, xtstamp);
1191 if (ret)
1192 return ret;
1193 }
1194
Christopher S. Hall8006c242016-02-22 03:15:22 -08001195 return 0;
1196}
1197EXPORT_SYMBOL_GPL(get_device_system_crosststamp);
1198
1199/**
john stultz85240702007-05-08 00:27:59 -07001200 * do_gettimeofday - Returns the time of day in a timeval
1201 * @tv: pointer to the timeval to be set
1202 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +01001203 * NOTE: Users should be converted to using getnstimeofday()
john stultz85240702007-05-08 00:27:59 -07001204 */
1205void do_gettimeofday(struct timeval *tv)
1206{
Thomas Gleixnerd6d29892014-07-16 21:04:04 +00001207 struct timespec64 now;
john stultz85240702007-05-08 00:27:59 -07001208
Thomas Gleixnerd6d29892014-07-16 21:04:04 +00001209 getnstimeofday64(&now);
john stultz85240702007-05-08 00:27:59 -07001210 tv->tv_sec = now.tv_sec;
1211 tv->tv_usec = now.tv_nsec/1000;
1212}
john stultz85240702007-05-08 00:27:59 -07001213EXPORT_SYMBOL(do_gettimeofday);
Richard Cochrand239f492012-04-27 10:12:42 +02001214
john stultz85240702007-05-08 00:27:59 -07001215/**
pang.xunlei21f7eca2014-11-18 19:15:16 +08001216 * do_settimeofday64 - Sets the time of day.
1217 * @ts: pointer to the timespec64 variable containing the new time
john stultz85240702007-05-08 00:27:59 -07001218 *
1219 * Sets the time of day to the new time and update NTP and notify hrtimers
1220 */
pang.xunlei21f7eca2014-11-18 19:15:16 +08001221int do_settimeofday64(const struct timespec64 *ts)
john stultz85240702007-05-08 00:27:59 -07001222{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001223 struct timekeeper *tk = &tk_core.timekeeper;
pang.xunlei21f7eca2014-11-18 19:15:16 +08001224 struct timespec64 ts_delta, xt;
John Stultz92c1d3e2011-11-14 14:05:44 -08001225 unsigned long flags;
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001226 int ret = 0;
john stultz85240702007-05-08 00:27:59 -07001227
pang.xunlei21f7eca2014-11-18 19:15:16 +08001228 if (!timespec64_valid_strict(ts))
john stultz85240702007-05-08 00:27:59 -07001229 return -EINVAL;
1230
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001231 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001232 write_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -07001233
John Stultz4e250fd2012-07-27 14:48:13 -04001234 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -07001235
John Stultz4e250fd2012-07-27 14:48:13 -04001236 xt = tk_xtime(tk);
pang.xunlei21f7eca2014-11-18 19:15:16 +08001237 ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
1238 ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
John Stultz1e75fa82012-07-13 01:21:53 -04001239
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001240 if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) {
1241 ret = -EINVAL;
1242 goto out;
1243 }
1244
John Stultz7d489d12014-07-16 21:04:01 +00001245 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
john stultz85240702007-05-08 00:27:59 -07001246
pang.xunlei21f7eca2014-11-18 19:15:16 +08001247 tk_set_xtime(tk, ts);
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001248out:
David Vrabel780427f2013-06-27 11:35:46 +01001249 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
john stultz85240702007-05-08 00:27:59 -07001250
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001251 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001252 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001253
1254 /* signal hrtimers about time change */
1255 clock_was_set();
1256
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001257 return ret;
john stultz85240702007-05-08 00:27:59 -07001258}
pang.xunlei21f7eca2014-11-18 19:15:16 +08001259EXPORT_SYMBOL(do_settimeofday64);
john stultz85240702007-05-08 00:27:59 -07001260
John Stultzc528f7c2011-02-01 13:52:17 +00001261/**
1262 * timekeeping_inject_offset - Adds or subtracts from the current time.
1263 * @tv: pointer to the timespec variable containing the offset
1264 *
1265 * Adds or subtracts an offset value from the current time.
1266 */
1267int timekeeping_inject_offset(struct timespec *ts)
1268{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001269 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -08001270 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001271 struct timespec64 ts64, tmp;
John Stultz4e8b1452012-08-08 15:36:20 -04001272 int ret = 0;
John Stultzc528f7c2011-02-01 13:52:17 +00001273
John Stultz37cf4dc2015-12-03 22:09:31 -05001274 if (!timespec_inject_offset_valid(ts))
John Stultzc528f7c2011-02-01 13:52:17 +00001275 return -EINVAL;
1276
John Stultz7d489d12014-07-16 21:04:01 +00001277 ts64 = timespec_to_timespec64(*ts);
1278
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001279 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001280 write_seqcount_begin(&tk_core.seq);
John Stultzc528f7c2011-02-01 13:52:17 +00001281
John Stultz4e250fd2012-07-27 14:48:13 -04001282 timekeeping_forward_now(tk);
John Stultzc528f7c2011-02-01 13:52:17 +00001283
John Stultz4e8b1452012-08-08 15:36:20 -04001284 /* Make sure the proposed value is valid */
John Stultz7d489d12014-07-16 21:04:01 +00001285 tmp = timespec64_add(tk_xtime(tk), ts64);
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001286 if (timespec64_compare(&tk->wall_to_monotonic, &ts64) > 0 ||
1287 !timespec64_valid_strict(&tmp)) {
John Stultz4e8b1452012-08-08 15:36:20 -04001288 ret = -EINVAL;
1289 goto error;
1290 }
John Stultz1e75fa82012-07-13 01:21:53 -04001291
John Stultz7d489d12014-07-16 21:04:01 +00001292 tk_xtime_add(tk, &ts64);
1293 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts64));
John Stultzc528f7c2011-02-01 13:52:17 +00001294
John Stultz4e8b1452012-08-08 15:36:20 -04001295error: /* even if we error out, we forwarded the time, so call update */
David Vrabel780427f2013-06-27 11:35:46 +01001296 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultzc528f7c2011-02-01 13:52:17 +00001297
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001298 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001299 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzc528f7c2011-02-01 13:52:17 +00001300
1301 /* signal hrtimers about time change */
1302 clock_was_set();
1303
John Stultz4e8b1452012-08-08 15:36:20 -04001304 return ret;
John Stultzc528f7c2011-02-01 13:52:17 +00001305}
1306EXPORT_SYMBOL(timekeeping_inject_offset);
1307
John Stultzcc244dd2012-05-03 12:30:07 -07001308
1309/**
1310 * timekeeping_get_tai_offset - Returns current TAI offset from UTC
1311 *
1312 */
1313s32 timekeeping_get_tai_offset(void)
1314{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001315 struct timekeeper *tk = &tk_core.timekeeper;
John Stultzcc244dd2012-05-03 12:30:07 -07001316 unsigned int seq;
1317 s32 ret;
1318
1319 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001320 seq = read_seqcount_begin(&tk_core.seq);
John Stultzcc244dd2012-05-03 12:30:07 -07001321 ret = tk->tai_offset;
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001322 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultzcc244dd2012-05-03 12:30:07 -07001323
1324 return ret;
1325}
1326
1327/**
1328 * __timekeeping_set_tai_offset - Lock free worker function
1329 *
1330 */
Fengguang Wudd5d70e2013-03-25 12:24:24 -07001331static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
John Stultzcc244dd2012-05-03 12:30:07 -07001332{
1333 tk->tai_offset = tai_offset;
John Stultz04005f62013-12-10 17:13:35 -08001334 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
John Stultzcc244dd2012-05-03 12:30:07 -07001335}
1336
1337/**
1338 * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
1339 *
1340 */
1341void timekeeping_set_tai_offset(s32 tai_offset)
1342{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001343 struct timekeeper *tk = &tk_core.timekeeper;
John Stultzcc244dd2012-05-03 12:30:07 -07001344 unsigned long flags;
1345
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001346 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001347 write_seqcount_begin(&tk_core.seq);
John Stultzcc244dd2012-05-03 12:30:07 -07001348 __timekeeping_set_tai_offset(tk, tai_offset);
John Stultzf55c0762013-12-11 18:50:25 -08001349 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001350 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001351 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz4e8f8b32013-04-10 12:41:49 -07001352 clock_was_set();
John Stultzcc244dd2012-05-03 12:30:07 -07001353}
1354
john stultz85240702007-05-08 00:27:59 -07001355/**
1356 * change_clocksource - Swaps clocksources if a new one is available
1357 *
1358 * Accumulates current time interval and initializes new clocksource
1359 */
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001360static int change_clocksource(void *data)
john stultz85240702007-05-08 00:27:59 -07001361{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001362 struct timekeeper *tk = &tk_core.timekeeper;
Magnus Damm4614e6a2009-04-21 12:24:02 -07001363 struct clocksource *new, *old;
John Stultzf695cf92012-03-14 16:38:15 -07001364 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -07001365
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001366 new = (struct clocksource *) data;
john stultz85240702007-05-08 00:27:59 -07001367
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001368 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001369 write_seqcount_begin(&tk_core.seq);
John Stultzf695cf92012-03-14 16:38:15 -07001370
John Stultz4e250fd2012-07-27 14:48:13 -04001371 timekeeping_forward_now(tk);
Thomas Gleixner09ac3692013-04-25 20:31:44 +00001372 /*
1373 * If the cs is in module, get a module reference. Succeeds
1374 * for built-in code (owner == NULL) as well.
1375 */
1376 if (try_module_get(new->owner)) {
1377 if (!new->enable || new->enable(new) == 0) {
Peter Zijlstra876e7882015-03-19 10:09:06 +01001378 old = tk->tkr_mono.clock;
Thomas Gleixner09ac3692013-04-25 20:31:44 +00001379 tk_setup_internals(tk, new);
1380 if (old->disable)
1381 old->disable(old);
1382 module_put(old->owner);
1383 } else {
1384 module_put(new->owner);
1385 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001386 }
David Vrabel780427f2013-06-27 11:35:46 +01001387 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultzf695cf92012-03-14 16:38:15 -07001388
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001389 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001390 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzf695cf92012-03-14 16:38:15 -07001391
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001392 return 0;
1393}
john stultz85240702007-05-08 00:27:59 -07001394
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001395/**
1396 * timekeeping_notify - Install a new clock source
1397 * @clock: pointer to the clock source
1398 *
1399 * This function is called from clocksource.c after a new, better clock
1400 * source has been registered. The caller holds the clocksource_mutex.
1401 */
Thomas Gleixnerba919d12013-04-25 20:31:44 +00001402int timekeeping_notify(struct clocksource *clock)
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001403{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001404 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04001405
Peter Zijlstra876e7882015-03-19 10:09:06 +01001406 if (tk->tkr_mono.clock == clock)
Thomas Gleixnerba919d12013-04-25 20:31:44 +00001407 return 0;
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001408 stop_machine(change_clocksource, clock, NULL);
john stultz85240702007-05-08 00:27:59 -07001409 tick_clock_notify();
Peter Zijlstra876e7882015-03-19 10:09:06 +01001410 return tk->tkr_mono.clock == clock ? 0 : -1;
john stultz85240702007-05-08 00:27:59 -07001411}
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001412
Thomas Gleixnera40f2622009-07-07 13:00:31 +02001413/**
John Stultzcdba2ec2014-11-07 11:03:20 -08001414 * getrawmonotonic64 - Returns the raw monotonic time in a timespec
1415 * @ts: pointer to the timespec64 to be set
John Stultz2d422442008-08-20 16:37:30 -07001416 *
1417 * Returns the raw monotonic time (completely un-modified by ntp)
1418 */
John Stultzcdba2ec2014-11-07 11:03:20 -08001419void getrawmonotonic64(struct timespec64 *ts)
John Stultz2d422442008-08-20 16:37:30 -07001420{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001421 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz2d422442008-08-20 16:37:30 -07001422 unsigned long seq;
1423 s64 nsecs;
John Stultz2d422442008-08-20 16:37:30 -07001424
1425 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001426 seq = read_seqcount_begin(&tk_core.seq);
John Stultz5311c742017-05-22 17:20:20 -07001427 ts->tv_sec = tk->raw_sec;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01001428 nsecs = timekeeping_get_ns(&tk->tkr_raw);
John Stultz2d422442008-08-20 16:37:30 -07001429
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001430 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz2d422442008-08-20 16:37:30 -07001431
John Stultz5311c742017-05-22 17:20:20 -07001432 ts->tv_nsec = 0;
1433 timespec64_add_ns(ts, nsecs);
John Stultz2d422442008-08-20 16:37:30 -07001434}
John Stultzcdba2ec2014-11-07 11:03:20 -08001435EXPORT_SYMBOL(getrawmonotonic64);
1436
John Stultz2d422442008-08-20 16:37:30 -07001437
John Stultz2d422442008-08-20 16:37:30 -07001438/**
Li Zefancf4fc6c2008-02-08 04:19:24 -08001439 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -07001440 */
Li Zefancf4fc6c2008-02-08 04:19:24 -08001441int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -07001442{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001443 struct timekeeper *tk = &tk_core.timekeeper;
john stultz85240702007-05-08 00:27:59 -07001444 unsigned long seq;
1445 int ret;
1446
1447 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001448 seq = read_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -07001449
Peter Zijlstra876e7882015-03-19 10:09:06 +01001450 ret = tk->tkr_mono.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -07001451
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001452 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz85240702007-05-08 00:27:59 -07001453
1454 return ret;
1455}
1456
1457/**
Jon Hunter98962462009-08-18 12:45:10 -05001458 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
Jon Hunter98962462009-08-18 12:45:10 -05001459 */
1460u64 timekeeping_max_deferment(void)
1461{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001462 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz70471f22011-11-14 12:48:10 -08001463 unsigned long seq;
1464 u64 ret;
John Stultz42e71e82012-07-13 01:21:51 -04001465
John Stultz70471f22011-11-14 12:48:10 -08001466 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001467 seq = read_seqcount_begin(&tk_core.seq);
John Stultz70471f22011-11-14 12:48:10 -08001468
Peter Zijlstra876e7882015-03-19 10:09:06 +01001469 ret = tk->tkr_mono.clock->max_idle_ns;
John Stultz70471f22011-11-14 12:48:10 -08001470
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001471 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz70471f22011-11-14 12:48:10 -08001472
1473 return ret;
Jon Hunter98962462009-08-18 12:45:10 -05001474}
1475
1476/**
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001477 * read_persistent_clock - Return time from the persistent clock.
john stultz85240702007-05-08 00:27:59 -07001478 *
1479 * Weak dummy function for arches that do not yet support it.
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001480 * Reads the time from the battery backed persistent clock.
1481 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
john stultz85240702007-05-08 00:27:59 -07001482 *
1483 * XXX - Do be sure to remove it once all arches implement it.
1484 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001485void __weak read_persistent_clock(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -07001486{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001487 ts->tv_sec = 0;
1488 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -07001489}
1490
Xunlei Pang2ee96632015-04-01 20:34:22 -07001491void __weak read_persistent_clock64(struct timespec64 *ts64)
1492{
1493 struct timespec ts;
1494
1495 read_persistent_clock(&ts);
1496 *ts64 = timespec_to_timespec64(ts);
1497}
1498
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001499/**
Xunlei Pange83d0a42015-04-09 09:04:42 +08001500 * read_boot_clock64 - Return time of the system start.
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001501 *
1502 * Weak dummy function for arches that do not yet support it.
1503 * Function to read the exact time the system has been started.
Xunlei Pange83d0a42015-04-09 09:04:42 +08001504 * Returns a timespec64 with tv_sec=0 and tv_nsec=0 if unsupported.
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001505 *
1506 * XXX - Do be sure to remove it once all arches implement it.
1507 */
Xunlei Pange83d0a42015-04-09 09:04:42 +08001508void __weak read_boot_clock64(struct timespec64 *ts)
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001509{
1510 ts->tv_sec = 0;
1511 ts->tv_nsec = 0;
1512}
1513
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001514/* Flag for if timekeeping_resume() has injected sleeptime */
1515static bool sleeptime_injected;
1516
1517/* Flag for if there is a persistent clock on this platform */
1518static bool persistent_clock_exists;
1519
john stultz85240702007-05-08 00:27:59 -07001520/*
1521 * timekeeping_init - Initializes the clocksource and common timekeeping values
1522 */
1523void __init timekeeping_init(void)
1524{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001525 struct timekeeper *tk = &tk_core.timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +02001526 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -07001527 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001528 struct timespec64 now, boot, tmp;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001529
Xunlei Pang2ee96632015-04-01 20:34:22 -07001530 read_persistent_clock64(&now);
John Stultz7d489d12014-07-16 21:04:01 +00001531 if (!timespec64_valid_strict(&now)) {
John Stultz4e8b1452012-08-08 15:36:20 -04001532 pr_warn("WARNING: Persistent clock returned invalid value!\n"
1533 " Check your CMOS/BIOS settings.\n");
1534 now.tv_sec = 0;
1535 now.tv_nsec = 0;
Feng Tang31ade302013-01-16 00:09:47 +08001536 } else if (now.tv_sec || now.tv_nsec)
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001537 persistent_clock_exists = true;
John Stultz4e8b1452012-08-08 15:36:20 -04001538
Xunlei Pang9a806dd2015-04-01 20:34:21 -07001539 read_boot_clock64(&boot);
John Stultz7d489d12014-07-16 21:04:01 +00001540 if (!timespec64_valid_strict(&boot)) {
John Stultz4e8b1452012-08-08 15:36:20 -04001541 pr_warn("WARNING: Boot clock returned invalid value!\n"
1542 " Check your CMOS/BIOS settings.\n");
1543 boot.tv_sec = 0;
1544 boot.tv_nsec = 0;
1545 }
john stultz85240702007-05-08 00:27:59 -07001546
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001547 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001548 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07001549 ntp_init();
1550
Martin Schwidefskyf1b82742009-08-14 15:47:21 +02001551 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +02001552 if (clock->enable)
1553 clock->enable(clock);
John Stultz4e250fd2012-07-27 14:48:13 -04001554 tk_setup_internals(tk, clock);
john stultz85240702007-05-08 00:27:59 -07001555
John Stultz4e250fd2012-07-27 14:48:13 -04001556 tk_set_xtime(tk, &now);
John Stultz5311c742017-05-22 17:20:20 -07001557 tk->raw_sec = 0;
John Stultz1e75fa82012-07-13 01:21:53 -04001558 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
John Stultz4e250fd2012-07-27 14:48:13 -04001559 boot = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -04001560
John Stultz7d489d12014-07-16 21:04:01 +00001561 set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec);
John Stultz4e250fd2012-07-27 14:48:13 -04001562 tk_set_wall_to_mono(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -04001563
Thomas Gleixner56fd16c2015-10-16 15:50:22 +02001564 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001565
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001566 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001567 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001568}
1569
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001570/* time in seconds when suspend began for persistent clock */
John Stultz7d489d12014-07-16 21:04:01 +00001571static struct timespec64 timekeeping_suspend_time;
john stultz85240702007-05-08 00:27:59 -07001572
1573/**
John Stultz304529b2011-04-01 14:32:09 -07001574 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
1575 * @delta: pointer to a timespec delta value
1576 *
1577 * Takes a timespec offset measuring a suspend interval and properly
1578 * adds the sleep offset to the timekeeping variables.
1579 */
John Stultzf726a692012-07-13 01:21:57 -04001580static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
John Stultz7d489d12014-07-16 21:04:01 +00001581 struct timespec64 *delta)
John Stultz304529b2011-04-01 14:32:09 -07001582{
John Stultz7d489d12014-07-16 21:04:01 +00001583 if (!timespec64_valid_strict(delta)) {
John Stultz6d9bcb62014-06-04 16:11:43 -07001584 printk_deferred(KERN_WARNING
1585 "__timekeeping_inject_sleeptime: Invalid "
1586 "sleep delta value!\n");
John Stultzcb5de2f8d2011-06-01 18:18:09 -07001587 return;
1588 }
John Stultzf726a692012-07-13 01:21:57 -04001589 tk_xtime_add(tk, delta);
John Stultz7d489d12014-07-16 21:04:01 +00001590 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
Thomas Gleixner47da70d2014-07-16 21:05:00 +00001591 tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
Colin Cross5c835452013-05-21 22:32:14 -07001592 tk_debug_account_sleep_time(delta);
John Stultz304529b2011-04-01 14:32:09 -07001593}
1594
Xunlei Pang7f298132015-04-01 20:34:35 -07001595#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
John Stultz304529b2011-04-01 14:32:09 -07001596/**
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001597 * We have three kinds of time sources to use for sleep time
1598 * injection, the preference order is:
1599 * 1) non-stop clocksource
1600 * 2) persistent clock (ie: RTC accessible when irqs are off)
1601 * 3) RTC
1602 *
1603 * 1) and 2) are used by timekeeping, 3) by RTC subsystem.
1604 * If system has neither 1) nor 2), 3) will be used finally.
1605 *
1606 *
1607 * If timekeeping has injected sleeptime via either 1) or 2),
1608 * 3) becomes needless, so in this case we don't need to call
1609 * rtc_resume(), and this is what timekeeping_rtc_skipresume()
1610 * means.
1611 */
1612bool timekeeping_rtc_skipresume(void)
1613{
1614 return sleeptime_injected;
1615}
1616
1617/**
1618 * 1) can be determined whether to use or not only when doing
1619 * timekeeping_resume() which is invoked after rtc_suspend(),
1620 * so we can't skip rtc_suspend() surely if system has 1).
1621 *
1622 * But if system has 2), 2) will definitely be used, so in this
1623 * case we don't need to call rtc_suspend(), and this is what
1624 * timekeeping_rtc_skipsuspend() means.
1625 */
1626bool timekeeping_rtc_skipsuspend(void)
1627{
1628 return persistent_clock_exists;
1629}
1630
1631/**
pang.xunlei04d90892014-11-18 19:15:17 +08001632 * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values
1633 * @delta: pointer to a timespec64 delta value
John Stultz304529b2011-04-01 14:32:09 -07001634 *
Xunlei Pang2ee96632015-04-01 20:34:22 -07001635 * This hook is for architectures that cannot support read_persistent_clock64
John Stultz304529b2011-04-01 14:32:09 -07001636 * because their RTC/persistent clock is only accessible when irqs are enabled.
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001637 * and also don't have an effective nonstop clocksource.
John Stultz304529b2011-04-01 14:32:09 -07001638 *
1639 * This function should only be called by rtc_resume(), and allows
1640 * a suspend offset to be injected into the timekeeping values.
1641 */
pang.xunlei04d90892014-11-18 19:15:17 +08001642void timekeeping_inject_sleeptime64(struct timespec64 *delta)
John Stultz304529b2011-04-01 14:32:09 -07001643{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001644 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -08001645 unsigned long flags;
John Stultz304529b2011-04-01 14:32:09 -07001646
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001647 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001648 write_seqcount_begin(&tk_core.seq);
John Stultz70471f22011-11-14 12:48:10 -08001649
John Stultz4e250fd2012-07-27 14:48:13 -04001650 timekeeping_forward_now(tk);
John Stultz304529b2011-04-01 14:32:09 -07001651
pang.xunlei04d90892014-11-18 19:15:17 +08001652 __timekeeping_inject_sleeptime(tk, delta);
John Stultz304529b2011-04-01 14:32:09 -07001653
David Vrabel780427f2013-06-27 11:35:46 +01001654 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultz304529b2011-04-01 14:32:09 -07001655
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001656 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001657 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz304529b2011-04-01 14:32:09 -07001658
1659 /* signal hrtimers about time change */
1660 clock_was_set();
1661}
Xunlei Pang7f298132015-04-01 20:34:35 -07001662#endif
John Stultz304529b2011-04-01 14:32:09 -07001663
John Stultz304529b2011-04-01 14:32:09 -07001664/**
john stultz85240702007-05-08 00:27:59 -07001665 * timekeeping_resume - Resumes the generic timekeeping subsystem.
john stultz85240702007-05-08 00:27:59 -07001666 */
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +01001667void timekeeping_resume(void)
john stultz85240702007-05-08 00:27:59 -07001668{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001669 struct timekeeper *tk = &tk_core.timekeeper;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001670 struct clocksource *clock = tk->tkr_mono.clock;
John Stultz92c1d3e2011-11-14 14:05:44 -08001671 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001672 struct timespec64 ts_new, ts_delta;
Feng Tange445cf12013-03-12 11:56:48 +08001673 cycle_t cycle_now, cycle_delta;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001674
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001675 sleeptime_injected = false;
Xunlei Pang2ee96632015-04-01 20:34:22 -07001676 read_persistent_clock64(&ts_new);
john stultz85240702007-05-08 00:27:59 -07001677
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +02001678 clockevents_resume();
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +02001679 clocksource_resume();
1680
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001681 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001682 write_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -07001683
Feng Tange445cf12013-03-12 11:56:48 +08001684 /*
1685 * After system resumes, we need to calculate the suspended time and
1686 * compensate it for the OS time. There are 3 sources that could be
1687 * used: Nonstop clocksource during suspend, persistent clock and rtc
1688 * device.
1689 *
1690 * One specific platform may have 1 or 2 or all of them, and the
1691 * preference will be:
1692 * suspend-nonstop clocksource -> persistent clock -> rtc
1693 * The less preferred source will only be tried if there is no better
1694 * usable source. The rtc part is handled separately in rtc core code.
1695 */
John Stultz02a37cc2017-06-08 16:44:20 -07001696 cycle_now = tk_clock_read(&tk->tkr_mono);
Feng Tange445cf12013-03-12 11:56:48 +08001697 if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
Peter Zijlstra876e7882015-03-19 10:09:06 +01001698 cycle_now > tk->tkr_mono.cycle_last) {
Feng Tange445cf12013-03-12 11:56:48 +08001699 u64 num, max = ULLONG_MAX;
1700 u32 mult = clock->mult;
1701 u32 shift = clock->shift;
1702 s64 nsec = 0;
1703
Peter Zijlstra876e7882015-03-19 10:09:06 +01001704 cycle_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last,
1705 tk->tkr_mono.mask);
Feng Tange445cf12013-03-12 11:56:48 +08001706
1707 /*
1708 * "cycle_delta * mutl" may cause 64 bits overflow, if the
1709 * suspended time is too long. In that case we need do the
1710 * 64 bits math carefully
1711 */
1712 do_div(max, mult);
1713 if (cycle_delta > max) {
1714 num = div64_u64(cycle_delta, max);
1715 nsec = (((u64) max * mult) >> shift) * num;
1716 cycle_delta -= num * max;
1717 }
1718 nsec += ((u64) cycle_delta * mult) >> shift;
1719
John Stultz7d489d12014-07-16 21:04:01 +00001720 ts_delta = ns_to_timespec64(nsec);
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001721 sleeptime_injected = true;
John Stultz7d489d12014-07-16 21:04:01 +00001722 } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
1723 ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001724 sleeptime_injected = true;
john stultz85240702007-05-08 00:27:59 -07001725 }
Feng Tange445cf12013-03-12 11:56:48 +08001726
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001727 if (sleeptime_injected)
Feng Tange445cf12013-03-12 11:56:48 +08001728 __timekeeping_inject_sleeptime(tk, &ts_delta);
1729
1730 /* Re-base the last cycle value */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001731 tk->tkr_mono.cycle_last = cycle_now;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01001732 tk->tkr_raw.cycle_last = cycle_now;
1733
John Stultz4e250fd2012-07-27 14:48:13 -04001734 tk->ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -07001735 timekeeping_suspended = 0;
David Vrabel780427f2013-06-27 11:35:46 +01001736 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001737 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001738 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001739
1740 touch_softlockup_watchdog();
1741
Thomas Gleixner4ffee522015-03-25 13:09:16 +01001742 tick_resume();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +02001743 hrtimers_resume();
john stultz85240702007-05-08 00:27:59 -07001744}
1745
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +01001746int timekeeping_suspend(void)
john stultz85240702007-05-08 00:27:59 -07001747{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001748 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -08001749 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001750 struct timespec64 delta, delta_delta;
1751 static struct timespec64 old_delta;
john stultz85240702007-05-08 00:27:59 -07001752
Xunlei Pang2ee96632015-04-01 20:34:22 -07001753 read_persistent_clock64(&timekeeping_suspend_time);
Thomas Gleixner3be90952007-09-16 15:36:43 +02001754
Zoran Markovic0d6bd992013-05-17 11:24:05 -07001755 /*
1756 * On some systems the persistent_clock can not be detected at
1757 * timekeeping_init by its return value, so if we see a valid
1758 * value returned, update the persistent_clock_exists flag.
1759 */
1760 if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001761 persistent_clock_exists = true;
Zoran Markovic0d6bd992013-05-17 11:24:05 -07001762
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001763 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001764 write_seqcount_begin(&tk_core.seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001765 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -07001766 timekeeping_suspended = 1;
John Stultzcb332172011-05-31 22:53:23 -07001767
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001768 if (persistent_clock_exists) {
John Stultzcb332172011-05-31 22:53:23 -07001769 /*
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001770 * To avoid drift caused by repeated suspend/resumes,
1771 * which each can add ~1 second drift error,
1772 * try to compensate so the difference in system time
1773 * and persistent_clock time stays close to constant.
John Stultzcb332172011-05-31 22:53:23 -07001774 */
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001775 delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time);
1776 delta_delta = timespec64_sub(delta, old_delta);
1777 if (abs(delta_delta.tv_sec) >= 2) {
1778 /*
1779 * if delta_delta is too large, assume time correction
1780 * has occurred and set old_delta to the current delta.
1781 */
1782 old_delta = delta;
1783 } else {
1784 /* Otherwise try to adjust old_system to compensate */
1785 timekeeping_suspend_time =
1786 timespec64_add(timekeeping_suspend_time, delta_delta);
1787 }
John Stultzcb332172011-05-31 22:53:23 -07001788 }
John Stultz330a1612013-12-11 19:10:36 -08001789
1790 timekeeping_update(tk, TK_MIRROR);
Rafael J. Wysocki060407a2015-02-13 14:49:02 +01001791 halt_fast_timekeeper(tk);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001792 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001793 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001794
Thomas Gleixner4ffee522015-03-25 13:09:16 +01001795 tick_suspend();
Magnus Dammc54a42b2010-02-02 14:41:41 -08001796 clocksource_suspend();
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +02001797 clockevents_suspend();
john stultz85240702007-05-08 00:27:59 -07001798
1799 return 0;
1800}
1801
1802/* sysfs resume/suspend bits for timekeeping */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001803static struct syscore_ops timekeeping_syscore_ops = {
john stultz85240702007-05-08 00:27:59 -07001804 .resume = timekeeping_resume,
1805 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -07001806};
1807
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001808static int __init timekeeping_init_ops(void)
john stultz85240702007-05-08 00:27:59 -07001809{
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001810 register_syscore_ops(&timekeeping_syscore_ops);
1811 return 0;
john stultz85240702007-05-08 00:27:59 -07001812}
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001813device_initcall(timekeeping_init_ops);
john stultz85240702007-05-08 00:27:59 -07001814
1815/*
John Stultzdc491592013-12-06 17:25:21 -08001816 * Apply a multiplier adjustment to the timekeeper
john stultz85240702007-05-08 00:27:59 -07001817 */
John Stultzdc491592013-12-06 17:25:21 -08001818static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
1819 s64 offset,
1820 bool negative,
1821 int adj_scale)
john stultz85240702007-05-08 00:27:59 -07001822{
John Stultzdc491592013-12-06 17:25:21 -08001823 s64 interval = tk->cycle_interval;
1824 s32 mult_adj = 1;
john stultz85240702007-05-08 00:27:59 -07001825
John Stultzdc491592013-12-06 17:25:21 -08001826 if (negative) {
1827 mult_adj = -mult_adj;
1828 interval = -interval;
1829 offset = -offset;
john stultz85240702007-05-08 00:27:59 -07001830 }
John Stultzdc491592013-12-06 17:25:21 -08001831 mult_adj <<= adj_scale;
1832 interval <<= adj_scale;
1833 offset <<= adj_scale;
john stultz85240702007-05-08 00:27:59 -07001834
John Stultzc2bc1112011-10-27 18:12:42 -07001835 /*
1836 * So the following can be confusing.
1837 *
John Stultzdc491592013-12-06 17:25:21 -08001838 * To keep things simple, lets assume mult_adj == 1 for now.
John Stultzc2bc1112011-10-27 18:12:42 -07001839 *
John Stultzdc491592013-12-06 17:25:21 -08001840 * When mult_adj != 1, remember that the interval and offset values
John Stultzc2bc1112011-10-27 18:12:42 -07001841 * have been appropriately scaled so the math is the same.
1842 *
1843 * The basic idea here is that we're increasing the multiplier
1844 * by one, this causes the xtime_interval to be incremented by
1845 * one cycle_interval. This is because:
1846 * xtime_interval = cycle_interval * mult
1847 * So if mult is being incremented by one:
1848 * xtime_interval = cycle_interval * (mult + 1)
1849 * Its the same as:
1850 * xtime_interval = (cycle_interval * mult) + cycle_interval
1851 * Which can be shortened to:
1852 * xtime_interval += cycle_interval
1853 *
1854 * So offset stores the non-accumulated cycles. Thus the current
1855 * time (in shifted nanoseconds) is:
1856 * now = (offset * adj) + xtime_nsec
1857 * Now, even though we're adjusting the clock frequency, we have
1858 * to keep time consistent. In other words, we can't jump back
1859 * in time, and we also want to avoid jumping forward in time.
1860 *
1861 * So given the same offset value, we need the time to be the same
1862 * both before and after the freq adjustment.
1863 * now = (offset * adj_1) + xtime_nsec_1
1864 * now = (offset * adj_2) + xtime_nsec_2
1865 * So:
1866 * (offset * adj_1) + xtime_nsec_1 =
1867 * (offset * adj_2) + xtime_nsec_2
1868 * And we know:
1869 * adj_2 = adj_1 + 1
1870 * So:
1871 * (offset * adj_1) + xtime_nsec_1 =
1872 * (offset * (adj_1+1)) + xtime_nsec_2
1873 * (offset * adj_1) + xtime_nsec_1 =
1874 * (offset * adj_1) + offset + xtime_nsec_2
1875 * Canceling the sides:
1876 * xtime_nsec_1 = offset + xtime_nsec_2
1877 * Which gives us:
1878 * xtime_nsec_2 = xtime_nsec_1 - offset
1879 * Which simplfies to:
1880 * xtime_nsec -= offset
1881 *
1882 * XXX - TODO: Doc ntp_error calculation.
1883 */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001884 if ((mult_adj > 0) && (tk->tkr_mono.mult + mult_adj < mult_adj)) {
pang.xunlei6067dc52014-10-08 15:03:34 +08001885 /* NTP adjustment caused clocksource mult overflow */
1886 WARN_ON_ONCE(1);
1887 return;
1888 }
1889
Peter Zijlstra876e7882015-03-19 10:09:06 +01001890 tk->tkr_mono.mult += mult_adj;
John Stultzf726a692012-07-13 01:21:57 -04001891 tk->xtime_interval += interval;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001892 tk->tkr_mono.xtime_nsec -= offset;
John Stultzf726a692012-07-13 01:21:57 -04001893 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
John Stultzdc491592013-12-06 17:25:21 -08001894}
John Stultz2a8c0882012-07-13 01:21:56 -04001895
John Stultzdc491592013-12-06 17:25:21 -08001896/*
1897 * Calculate the multiplier adjustment needed to match the frequency
1898 * specified by NTP
1899 */
1900static __always_inline void timekeeping_freqadjust(struct timekeeper *tk,
1901 s64 offset)
1902{
1903 s64 interval = tk->cycle_interval;
1904 s64 xinterval = tk->xtime_interval;
John Stultzec02b072015-12-03 10:23:30 -08001905 u32 base = tk->tkr_mono.clock->mult;
1906 u32 max = tk->tkr_mono.clock->maxadj;
1907 u32 cur_adj = tk->tkr_mono.mult;
John Stultzdc491592013-12-06 17:25:21 -08001908 s64 tick_error;
1909 bool negative;
John Stultzec02b072015-12-03 10:23:30 -08001910 u32 adj_scale;
John Stultzdc491592013-12-06 17:25:21 -08001911
1912 /* Remove any current error adj from freq calculation */
1913 if (tk->ntp_err_mult)
1914 xinterval -= tk->cycle_interval;
1915
John Stultz375f45b2014-04-23 20:53:29 -07001916 tk->ntp_tick = ntp_tick_length();
1917
John Stultzdc491592013-12-06 17:25:21 -08001918 /* Calculate current error per tick */
1919 tick_error = ntp_tick_length() >> tk->ntp_error_shift;
1920 tick_error -= (xinterval + tk->xtime_remainder);
1921
1922 /* Don't worry about correcting it if its small */
1923 if (likely((tick_error >= 0) && (tick_error <= interval)))
1924 return;
1925
1926 /* preserve the direction of correction */
1927 negative = (tick_error < 0);
1928
John Stultzec02b072015-12-03 10:23:30 -08001929 /* If any adjustment would pass the max, just return */
1930 if (negative && (cur_adj - 1) <= (base - max))
1931 return;
1932 if (!negative && (cur_adj + 1) >= (base + max))
1933 return;
1934 /*
1935 * Sort out the magnitude of the correction, but
1936 * avoid making so large a correction that we go
1937 * over the max adjustment.
1938 */
1939 adj_scale = 0;
Andrew Morton79211c82015-11-09 14:58:13 -08001940 tick_error = abs(tick_error);
John Stultzec02b072015-12-03 10:23:30 -08001941 while (tick_error > interval) {
1942 u32 adj = 1 << (adj_scale + 1);
1943
1944 /* Check if adjustment gets us within 1 unit from the max */
1945 if (negative && (cur_adj - adj) <= (base - max))
1946 break;
1947 if (!negative && (cur_adj + adj) >= (base + max))
1948 break;
1949
1950 adj_scale++;
John Stultzdc491592013-12-06 17:25:21 -08001951 tick_error >>= 1;
John Stultzec02b072015-12-03 10:23:30 -08001952 }
John Stultzdc491592013-12-06 17:25:21 -08001953
1954 /* scale the corrections */
John Stultzec02b072015-12-03 10:23:30 -08001955 timekeeping_apply_adjustment(tk, offset, negative, adj_scale);
John Stultzdc491592013-12-06 17:25:21 -08001956}
1957
1958/*
1959 * Adjust the timekeeper's multiplier to the correct frequency
1960 * and also to reduce the accumulated error value.
1961 */
1962static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
1963{
1964 /* Correct for the current frequency error */
1965 timekeeping_freqadjust(tk, offset);
1966
1967 /* Next make a small adjustment to fix any cumulative error */
1968 if (!tk->ntp_err_mult && (tk->ntp_error > 0)) {
1969 tk->ntp_err_mult = 1;
1970 timekeeping_apply_adjustment(tk, offset, 0, 0);
1971 } else if (tk->ntp_err_mult && (tk->ntp_error <= 0)) {
1972 /* Undo any existing error adjustment */
1973 timekeeping_apply_adjustment(tk, offset, 1, 0);
1974 tk->ntp_err_mult = 0;
1975 }
1976
Peter Zijlstra876e7882015-03-19 10:09:06 +01001977 if (unlikely(tk->tkr_mono.clock->maxadj &&
1978 (abs(tk->tkr_mono.mult - tk->tkr_mono.clock->mult)
1979 > tk->tkr_mono.clock->maxadj))) {
John Stultzdc491592013-12-06 17:25:21 -08001980 printk_once(KERN_WARNING
1981 "Adjusting %s more than 11%% (%ld vs %ld)\n",
Peter Zijlstra876e7882015-03-19 10:09:06 +01001982 tk->tkr_mono.clock->name, (long)tk->tkr_mono.mult,
1983 (long)tk->tkr_mono.clock->mult + tk->tkr_mono.clock->maxadj);
John Stultzdc491592013-12-06 17:25:21 -08001984 }
1985
John Stultz2a8c0882012-07-13 01:21:56 -04001986 /*
1987 * It may be possible that when we entered this function, xtime_nsec
1988 * was very small. Further, if we're slightly speeding the clocksource
1989 * in the code above, its possible the required corrective factor to
1990 * xtime_nsec could cause it to underflow.
1991 *
1992 * Now, since we already accumulated the second, cannot simply roll
1993 * the accumulated second back, since the NTP subsystem has been
1994 * notified via second_overflow. So instead we push xtime_nsec forward
1995 * by the amount we underflowed, and add that amount into the error.
1996 *
1997 * We'll correct this error next time through this function, when
1998 * xtime_nsec is not as small.
1999 */
Peter Zijlstra876e7882015-03-19 10:09:06 +01002000 if (unlikely((s64)tk->tkr_mono.xtime_nsec < 0)) {
2001 s64 neg = -(s64)tk->tkr_mono.xtime_nsec;
2002 tk->tkr_mono.xtime_nsec = 0;
John Stultzf726a692012-07-13 01:21:57 -04002003 tk->ntp_error += neg << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -04002004 }
john stultz85240702007-05-08 00:27:59 -07002005}
2006
2007/**
John Stultz1f4f9482012-07-13 01:21:54 -04002008 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
2009 *
Zhen Lei571af552015-08-25 14:42:53 +08002010 * Helper function that accumulates the nsecs greater than a second
John Stultz1f4f9482012-07-13 01:21:54 -04002011 * from the xtime_nsec field to the xtime_secs field.
2012 * It also calls into the NTP code to handle leapsecond processing.
2013 *
2014 */
David Vrabel780427f2013-06-27 11:35:46 +01002015static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
John Stultz1f4f9482012-07-13 01:21:54 -04002016{
Peter Zijlstra876e7882015-03-19 10:09:06 +01002017 u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
John Stultz5258d3f2013-12-11 20:07:49 -08002018 unsigned int clock_set = 0;
John Stultz1f4f9482012-07-13 01:21:54 -04002019
Peter Zijlstra876e7882015-03-19 10:09:06 +01002020 while (tk->tkr_mono.xtime_nsec >= nsecps) {
John Stultz1f4f9482012-07-13 01:21:54 -04002021 int leap;
2022
Peter Zijlstra876e7882015-03-19 10:09:06 +01002023 tk->tkr_mono.xtime_nsec -= nsecps;
John Stultz1f4f9482012-07-13 01:21:54 -04002024 tk->xtime_sec++;
2025
2026 /* Figure out if its a leap sec and apply if needed */
2027 leap = second_overflow(tk->xtime_sec);
John Stultz6d0ef902012-07-27 14:48:12 -04002028 if (unlikely(leap)) {
John Stultz7d489d12014-07-16 21:04:01 +00002029 struct timespec64 ts;
John Stultz1f4f9482012-07-13 01:21:54 -04002030
John Stultz6d0ef902012-07-27 14:48:12 -04002031 tk->xtime_sec += leap;
2032
2033 ts.tv_sec = leap;
2034 ts.tv_nsec = 0;
2035 tk_set_wall_to_mono(tk,
John Stultz7d489d12014-07-16 21:04:01 +00002036 timespec64_sub(tk->wall_to_monotonic, ts));
John Stultz6d0ef902012-07-27 14:48:12 -04002037
John Stultzcc244dd2012-05-03 12:30:07 -07002038 __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
2039
John Stultz5258d3f2013-12-11 20:07:49 -08002040 clock_set = TK_CLOCK_WAS_SET;
John Stultz6d0ef902012-07-27 14:48:12 -04002041 }
John Stultz1f4f9482012-07-13 01:21:54 -04002042 }
John Stultz5258d3f2013-12-11 20:07:49 -08002043 return clock_set;
John Stultz1f4f9482012-07-13 01:21:54 -04002044}
2045
John Stultz1f4f9482012-07-13 01:21:54 -04002046/**
john stultza092ff02009-10-02 16:17:53 -07002047 * logarithmic_accumulation - shifted accumulation of cycles
2048 *
2049 * This functions accumulates a shifted interval of cycles into
2050 * into a shifted interval nanoseconds. Allows for O(log) accumulation
2051 * loop.
2052 *
2053 * Returns the unconsumed cycles.
2054 */
John Stultzf726a692012-07-13 01:21:57 -04002055static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
John Stultz5258d3f2013-12-11 20:07:49 -08002056 u32 shift,
2057 unsigned int *clock_set)
john stultza092ff02009-10-02 16:17:53 -07002058{
Thomas Gleixner23a95372013-02-21 22:51:36 +00002059 cycle_t interval = tk->cycle_interval << shift;
John Stultza53bfdd2017-06-08 16:44:21 -07002060 u64 snsec_per_sec;
john stultza092ff02009-10-02 16:17:53 -07002061
Zhen Lei571af552015-08-25 14:42:53 +08002062 /* If the offset is smaller than a shifted interval, do nothing */
Thomas Gleixner23a95372013-02-21 22:51:36 +00002063 if (offset < interval)
john stultza092ff02009-10-02 16:17:53 -07002064 return offset;
2065
2066 /* Accumulate one shifted interval */
Thomas Gleixner23a95372013-02-21 22:51:36 +00002067 offset -= interval;
Peter Zijlstra876e7882015-03-19 10:09:06 +01002068 tk->tkr_mono.cycle_last += interval;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01002069 tk->tkr_raw.cycle_last += interval;
john stultza092ff02009-10-02 16:17:53 -07002070
Peter Zijlstra876e7882015-03-19 10:09:06 +01002071 tk->tkr_mono.xtime_nsec += tk->xtime_interval << shift;
John Stultz5258d3f2013-12-11 20:07:49 -08002072 *clock_set |= accumulate_nsecs_to_secs(tk);
john stultza092ff02009-10-02 16:17:53 -07002073
Jason Wesseldeda2e82010-08-09 14:20:09 -07002074 /* Accumulate raw time */
John Stultza53bfdd2017-06-08 16:44:21 -07002075 tk->tkr_raw.xtime_nsec += tk->raw_interval << shift;
2076 snsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
2077 while (tk->tkr_raw.xtime_nsec >= snsec_per_sec) {
2078 tk->tkr_raw.xtime_nsec -= snsec_per_sec;
John Stultz5311c742017-05-22 17:20:20 -07002079 tk->raw_sec++;
john stultza092ff02009-10-02 16:17:53 -07002080 }
2081
2082 /* Accumulate error between NTP and clock interval */
John Stultz375f45b2014-04-23 20:53:29 -07002083 tk->ntp_error += tk->ntp_tick << shift;
John Stultzf726a692012-07-13 01:21:57 -04002084 tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
2085 (tk->ntp_error_shift + shift);
john stultza092ff02009-10-02 16:17:53 -07002086
2087 return offset;
2088}
2089
john stultz85240702007-05-08 00:27:59 -07002090/**
2091 * update_wall_time - Uses the current clocksource to increment the wall time
2092 *
john stultz85240702007-05-08 00:27:59 -07002093 */
John Stultz47a1b7962013-12-12 13:10:55 -08002094void update_wall_time(void)
john stultz85240702007-05-08 00:27:59 -07002095{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002096 struct timekeeper *real_tk = &tk_core.timekeeper;
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002097 struct timekeeper *tk = &shadow_timekeeper;
john stultz85240702007-05-08 00:27:59 -07002098 cycle_t offset;
john stultza092ff02009-10-02 16:17:53 -07002099 int shift = 0, maxshift;
John Stultz5258d3f2013-12-11 20:07:49 -08002100 unsigned int clock_set = 0;
John Stultz70471f22011-11-14 12:48:10 -08002101 unsigned long flags;
2102
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00002103 raw_spin_lock_irqsave(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07002104
2105 /* Make sure we're fully resumed: */
2106 if (unlikely(timekeeping_suspended))
John Stultz70471f22011-11-14 12:48:10 -08002107 goto out;
john stultz85240702007-05-08 00:27:59 -07002108
John Stultz592913e2010-07-13 17:56:20 -07002109#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002110 offset = real_tk->cycle_interval;
John Stultz592913e2010-07-13 17:56:20 -07002111#else
John Stultz02a37cc2017-06-08 16:44:20 -07002112 offset = clocksource_delta(tk_clock_read(&tk->tkr_mono),
Peter Zijlstra876e7882015-03-19 10:09:06 +01002113 tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
john stultz85240702007-05-08 00:27:59 -07002114#endif
john stultz85240702007-05-08 00:27:59 -07002115
John Stultzbf2ac312012-08-21 20:30:49 -04002116 /* Check if there's really nothing to do */
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002117 if (offset < real_tk->cycle_interval)
John Stultzbf2ac312012-08-21 20:30:49 -04002118 goto out;
2119
John Stultz3c17ad12015-03-11 21:16:32 -07002120 /* Do some additional sanity checking */
2121 timekeeping_check_update(real_tk, offset);
2122
john stultza092ff02009-10-02 16:17:53 -07002123 /*
2124 * With NO_HZ we may have to accumulate many cycle_intervals
2125 * (think "ticks") worth of time at once. To do this efficiently,
2126 * we calculate the largest doubling multiple of cycle_intervals
Jim Cromie88b28ad2012-03-14 21:28:56 -06002127 * that is smaller than the offset. We then accumulate that
john stultza092ff02009-10-02 16:17:53 -07002128 * chunk in one go, and then try to consume the next smaller
2129 * doubled multiple.
john stultz85240702007-05-08 00:27:59 -07002130 */
John Stultz4e250fd2012-07-27 14:48:13 -04002131 shift = ilog2(offset) - ilog2(tk->cycle_interval);
john stultza092ff02009-10-02 16:17:53 -07002132 shift = max(0, shift);
Jim Cromie88b28ad2012-03-14 21:28:56 -06002133 /* Bound shift to one less than what overflows tick_length */
John Stultzea7cf492011-11-14 13:18:07 -08002134 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
john stultza092ff02009-10-02 16:17:53 -07002135 shift = min(shift, maxshift);
John Stultz4e250fd2012-07-27 14:48:13 -04002136 while (offset >= tk->cycle_interval) {
John Stultz5258d3f2013-12-11 20:07:49 -08002137 offset = logarithmic_accumulation(tk, offset, shift,
2138 &clock_set);
John Stultz4e250fd2012-07-27 14:48:13 -04002139 if (offset < tk->cycle_interval<<shift)
John Stultz830ec042010-03-18 14:47:30 -07002140 shift--;
john stultz85240702007-05-08 00:27:59 -07002141 }
2142
2143 /* correct the clock when NTP error is too big */
John Stultz4e250fd2012-07-27 14:48:13 -04002144 timekeeping_adjust(tk, offset);
john stultz85240702007-05-08 00:27:59 -07002145
John Stultz6a867a32010-04-06 14:30:51 -07002146 /*
John Stultz92bb1fc2012-09-04 15:38:12 -04002147 * XXX This can be killed once everyone converts
2148 * to the new update_vsyscall.
2149 */
2150 old_vsyscall_fixup(tk);
john stultz85240702007-05-08 00:27:59 -07002151
John Stultz6a867a32010-04-06 14:30:51 -07002152 /*
2153 * Finally, make sure that after the rounding
John Stultz1e75fa82012-07-13 01:21:53 -04002154 * xtime_nsec isn't larger than NSEC_PER_SEC
John Stultz6a867a32010-04-06 14:30:51 -07002155 */
John Stultz5258d3f2013-12-11 20:07:49 -08002156 clock_set |= accumulate_nsecs_to_secs(tk);
Linus Torvalds83f57a12009-12-22 14:10:37 -08002157
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002158 write_seqcount_begin(&tk_core.seq);
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002159 /*
2160 * Update the real timekeeper.
2161 *
2162 * We could avoid this memcpy by switching pointers, but that
2163 * requires changes to all other timekeeper usage sites as
2164 * well, i.e. move the timekeeper pointer getter into the
2165 * spinlocked/seqcount protected sections. And we trade this
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002166 * memcpy under the tk_core.seq against one before we start
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002167 * updating.
2168 */
John Stultz906c5552015-06-17 10:05:53 -07002169 timekeeping_update(tk, clock_set);
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002170 memcpy(real_tk, tk, sizeof(*tk));
John Stultz906c5552015-06-17 10:05:53 -07002171 /* The memcpy must come last. Do not put anything here! */
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002172 write_seqcount_end(&tk_core.seq);
Thomas Gleixnerca4523c2013-02-21 22:51:40 +00002173out:
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00002174 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz47a1b7962013-12-12 13:10:55 -08002175 if (clock_set)
John Stultzcab5e122014-03-27 16:30:49 -07002176 /* Have to call _delayed version, since in irq context*/
2177 clock_was_set_delayed();
john stultz85240702007-05-08 00:27:59 -07002178}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002179
2180/**
John Stultzd08c0cd2014-12-08 12:00:09 -08002181 * getboottime64 - Return the real time of system boot.
2182 * @ts: pointer to the timespec64 to be set
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002183 *
John Stultzd08c0cd2014-12-08 12:00:09 -08002184 * Returns the wall-time of boot in a timespec64.
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002185 *
2186 * This is based on the wall_to_monotonic offset and the total suspend
2187 * time. Calls to settimeofday will affect the value returned (which
2188 * basically means that however wrong your real time clock is at boot time,
2189 * you get the right time here).
2190 */
John Stultzd08c0cd2014-12-08 12:00:09 -08002191void getboottime64(struct timespec64 *ts)
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002192{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002193 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixner02cba152014-07-16 21:04:58 +00002194 ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot);
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02002195
John Stultzd08c0cd2014-12-08 12:00:09 -08002196 *ts = ktime_to_timespec64(t);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002197}
John Stultzd08c0cd2014-12-08 12:00:09 -08002198EXPORT_SYMBOL_GPL(getboottime64);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002199
john stultz17c38b72007-07-24 18:38:34 -07002200unsigned long get_seconds(void)
2201{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002202 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04002203
2204 return tk->xtime_sec;
john stultz17c38b72007-07-24 18:38:34 -07002205}
2206EXPORT_SYMBOL(get_seconds);
2207
john stultzda15cfd2009-08-19 19:13:34 -07002208struct timespec __current_kernel_time(void)
2209{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002210 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04002211
John Stultz7d489d12014-07-16 21:04:01 +00002212 return timespec64_to_timespec(tk_xtime(tk));
john stultzda15cfd2009-08-19 19:13:34 -07002213}
john stultz17c38b72007-07-24 18:38:34 -07002214
Baolin Wang8758a242015-07-29 20:09:43 +08002215struct timespec64 current_kernel_time64(void)
john stultz2c6b47d2007-07-24 17:47:43 -07002216{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002217 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz7d489d12014-07-16 21:04:01 +00002218 struct timespec64 now;
john stultz2c6b47d2007-07-24 17:47:43 -07002219 unsigned long seq;
2220
2221 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002222 seq = read_seqcount_begin(&tk_core.seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08002223
John Stultz4e250fd2012-07-27 14:48:13 -04002224 now = tk_xtime(tk);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002225 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz2c6b47d2007-07-24 17:47:43 -07002226
Baolin Wang8758a242015-07-29 20:09:43 +08002227 return now;
john stultz2c6b47d2007-07-24 17:47:43 -07002228}
Baolin Wang8758a242015-07-29 20:09:43 +08002229EXPORT_SYMBOL(current_kernel_time64);
john stultzda15cfd2009-08-19 19:13:34 -07002230
John Stultz334334b2014-11-07 11:20:40 -08002231struct timespec64 get_monotonic_coarse64(void)
john stultzda15cfd2009-08-19 19:13:34 -07002232{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002233 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz7d489d12014-07-16 21:04:01 +00002234 struct timespec64 now, mono;
john stultzda15cfd2009-08-19 19:13:34 -07002235 unsigned long seq;
2236
2237 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002238 seq = read_seqcount_begin(&tk_core.seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08002239
John Stultz4e250fd2012-07-27 14:48:13 -04002240 now = tk_xtime(tk);
2241 mono = tk->wall_to_monotonic;
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002242 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultzda15cfd2009-08-19 19:13:34 -07002243
John Stultz7d489d12014-07-16 21:04:01 +00002244 set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec,
john stultzda15cfd2009-08-19 19:13:34 -07002245 now.tv_nsec + mono.tv_nsec);
John Stultz7d489d12014-07-16 21:04:01 +00002246
John Stultz334334b2014-11-07 11:20:40 -08002247 return now;
john stultzda15cfd2009-08-19 19:13:34 -07002248}
Gregor Boirieeaaa7ec2016-03-09 19:05:48 +01002249EXPORT_SYMBOL(get_monotonic_coarse64);
Torben Hohn871cf1e2011-01-27 15:58:55 +01002250
2251/*
John Stultzd6ad4182012-02-28 16:50:11 -08002252 * Must hold jiffies_lock
Torben Hohn871cf1e2011-01-27 15:58:55 +01002253 */
2254void do_timer(unsigned long ticks)
2255{
2256 jiffies_64 += ticks;
Torben Hohn871cf1e2011-01-27 15:58:55 +01002257 calc_global_load(ticks);
2258}
Torben Hohn48cf76f72011-01-27 15:59:05 +01002259
2260/**
John Stultz76f41082014-07-16 21:03:52 +00002261 * ktime_get_update_offsets_now - hrtimer helper
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002262 * @cwsseq: pointer to check and store the clock was set sequence number
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002263 * @offs_real: pointer to storage for monotonic -> realtime offset
2264 * @offs_boot: pointer to storage for monotonic -> boottime offset
Xie XiuQib7bc50e2013-10-18 09:13:30 +08002265 * @offs_tai: pointer to storage for monotonic -> clock tai offset
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002266 *
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002267 * Returns current monotonic time and updates the offsets if the
2268 * sequence number in @cwsseq and timekeeper.clock_was_set_seq are
2269 * different.
2270 *
Xie XiuQib7bc50e2013-10-18 09:13:30 +08002271 * Called from hrtimer_interrupt() or retrigger_next_event()
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002272 */
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002273ktime_t ktime_get_update_offsets_now(unsigned int *cwsseq, ktime_t *offs_real,
2274 ktime_t *offs_boot, ktime_t *offs_tai)
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002275{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002276 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002277 unsigned int seq;
Thomas Gleixnera37c0aa2014-07-16 21:04:19 +00002278 ktime_t base;
2279 u64 nsecs;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002280
2281 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002282 seq = read_seqcount_begin(&tk_core.seq);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002283
Peter Zijlstra876e7882015-03-19 10:09:06 +01002284 base = tk->tkr_mono.base;
2285 nsecs = timekeeping_get_ns(&tk->tkr_mono);
John Stultz833f32d2015-06-11 15:54:55 -07002286 base = ktime_add_ns(base, nsecs);
2287
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002288 if (*cwsseq != tk->clock_was_set_seq) {
2289 *cwsseq = tk->clock_was_set_seq;
2290 *offs_real = tk->offs_real;
2291 *offs_boot = tk->offs_boot;
2292 *offs_tai = tk->offs_tai;
2293 }
John Stultz833f32d2015-06-11 15:54:55 -07002294
2295 /* Handle leapsecond insertion adjustments */
2296 if (unlikely(base.tv64 >= tk->next_leap_ktime.tv64))
2297 *offs_real = ktime_sub(tk->offs_real, ktime_set(1, 0));
2298
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002299 } while (read_seqcount_retry(&tk_core.seq, seq));
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002300
John Stultz833f32d2015-06-11 15:54:55 -07002301 return base;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002302}
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002303
Torben Hohnf0af911a92011-01-27 15:59:10 +01002304/**
John Stultzaa6f9c592013-03-22 11:31:29 -07002305 * do_adjtimex() - Accessor function to NTP __do_adjtimex function
2306 */
2307int do_adjtimex(struct timex *txc)
2308{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002309 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz06c017f2013-03-22 11:37:28 -07002310 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00002311 struct timespec64 ts;
John Stultz4e8f8b32013-04-10 12:41:49 -07002312 s32 orig_tai, tai;
John Stultze4085692013-03-22 12:08:52 -07002313 int ret;
2314
2315 /* Validate the data before disabling interrupts */
2316 ret = ntp_validate_timex(txc);
2317 if (ret)
2318 return ret;
2319
John Stultzcef90372013-03-22 15:04:13 -07002320 if (txc->modes & ADJ_SETOFFSET) {
2321 struct timespec delta;
2322 delta.tv_sec = txc->time.tv_sec;
2323 delta.tv_nsec = txc->time.tv_usec;
2324 if (!(txc->modes & ADJ_NANO))
2325 delta.tv_nsec *= 1000;
2326 ret = timekeeping_inject_offset(&delta);
2327 if (ret)
2328 return ret;
2329 }
2330
Thomas Gleixnerd6d29892014-07-16 21:04:04 +00002331 getnstimeofday64(&ts);
John Stultzaa6f9c592013-03-22 11:31:29 -07002332
John Stultz06c017f2013-03-22 11:37:28 -07002333 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002334 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002335
John Stultz4e8f8b32013-04-10 12:41:49 -07002336 orig_tai = tai = tk->tai_offset;
John Stultz87ace392013-03-22 12:28:15 -07002337 ret = __do_adjtimex(txc, &ts, &tai);
2338
John Stultz4e8f8b32013-04-10 12:41:49 -07002339 if (tai != orig_tai) {
2340 __timekeeping_set_tai_offset(tk, tai);
John Stultzf55c0762013-12-11 18:50:25 -08002341 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultz4e8f8b32013-04-10 12:41:49 -07002342 }
John Stultz833f32d2015-06-11 15:54:55 -07002343 tk_update_leap_state(tk);
2344
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002345 write_seqcount_end(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002346 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
2347
John Stultz6fdda9a2013-12-10 17:18:18 -08002348 if (tai != orig_tai)
2349 clock_was_set();
2350
John Stultz7bd36012013-09-11 16:50:56 -07002351 ntp_notify_cmos_timer();
2352
John Stultz87ace392013-03-22 12:28:15 -07002353 return ret;
2354}
John Stultzaa6f9c592013-03-22 11:31:29 -07002355
2356#ifdef CONFIG_NTP_PPS
2357/**
2358 * hardpps() - Accessor function to NTP __hardpps function
2359 */
Arnd Bergmann7ec88e42015-09-28 22:21:28 +02002360void hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts)
John Stultzaa6f9c592013-03-22 11:31:29 -07002361{
John Stultz06c017f2013-03-22 11:37:28 -07002362 unsigned long flags;
2363
2364 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002365 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002366
John Stultzaa6f9c592013-03-22 11:31:29 -07002367 __hardpps(phase_ts, raw_ts);
John Stultz06c017f2013-03-22 11:37:28 -07002368
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002369 write_seqcount_end(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002370 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzaa6f9c592013-03-22 11:31:29 -07002371}
2372EXPORT_SYMBOL(hardpps);
2373#endif
2374
2375/**
Torben Hohnf0af911a92011-01-27 15:59:10 +01002376 * xtime_update() - advances the timekeeping infrastructure
2377 * @ticks: number of ticks, that have elapsed since the last call.
2378 *
2379 * Must be called with interrupts disabled.
2380 */
2381void xtime_update(unsigned long ticks)
2382{
John Stultzd6ad4182012-02-28 16:50:11 -08002383 write_seqlock(&jiffies_lock);
Torben Hohnf0af911a92011-01-27 15:59:10 +01002384 do_timer(ticks);
John Stultzd6ad4182012-02-28 16:50:11 -08002385 write_sequnlock(&jiffies_lock);
John Stultz47a1b7962013-12-12 13:10:55 -08002386 update_wall_time();
Torben Hohnf0af911a92011-01-27 15:59:10 +01002387}