blob: 1ab2e305e962f566a05d78091656805fe0d09722 [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>
Ingo Molnarbadaff82017-02-08 08:45:17 +010018#include <linux/sched/loadavg.h>
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +010019#include <linux/syscore_ops.h>
john stultz85240702007-05-08 00:27:59 -070020#include <linux/clocksource.h>
21#include <linux/jiffies.h>
22#include <linux/time.h>
23#include <linux/tick.h>
Martin Schwidefsky75c51582009-08-14 15:47:30 +020024#include <linux/stop_machine.h>
Marcelo Tosattie0b306f2012-11-27 23:28:59 -020025#include <linux/pvclock_gtod.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070026#include <linux/compiler.h>
john stultz85240702007-05-08 00:27:59 -070027
Thomas Gleixnereb93e4d2013-02-21 22:51:36 +000028#include "tick-internal.h"
John Stultzaa6f9c592013-03-22 11:31:29 -070029#include "ntp_internal.h"
Colin Cross5c835452013-05-21 22:32:14 -070030#include "timekeeping_internal.h"
Martin Schwidefsky155ec602009-08-14 15:47:26 +020031
David Vrabel04397fe92013-06-27 11:35:45 +010032#define TK_CLEAR_NTP (1 << 0)
33#define TK_MIRROR (1 << 1)
David Vrabel780427f2013-06-27 11:35:46 +010034#define TK_CLOCK_WAS_SET (1 << 2)
David Vrabel04397fe92013-06-27 11:35:45 +010035
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +000036/*
37 * The most important data for readout fits into a single 64 byte
38 * cache line.
39 */
40static struct {
41 seqcount_t seq;
42 struct timekeeper timekeeper;
Bart Van Asscheb6fc5a52018-11-28 15:43:09 -080043} tk_core ____cacheline_aligned = {
44 .seq = SEQCNT_ZERO(tk_core.seq),
45};
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +000046
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +000047static DEFINE_RAW_SPINLOCK(timekeeper_lock);
Thomas Gleixner48cdc132013-02-21 22:51:40 +000048static struct timekeeper shadow_timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020049
Thomas Gleixner4396e052014-07-16 21:05:23 +000050/**
51 * struct tk_fast - NMI safe timekeeper
52 * @seq: Sequence counter for protecting updates. The lowest bit
53 * is the index for the tk_read_base array
54 * @base: tk_read_base array. Access is indexed by the lowest bit of
55 * @seq.
56 *
57 * See @update_fast_timekeeper() below.
58 */
59struct tk_fast {
60 seqcount_t seq;
61 struct tk_read_base base[2];
62};
63
64static struct tk_fast tk_fast_mono ____cacheline_aligned;
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +010065static struct tk_fast tk_fast_raw ____cacheline_aligned;
Thomas Gleixner4396e052014-07-16 21:05:23 +000066
John Stultz8fcce542011-11-14 11:46:39 -080067/* flag for if timekeeping is suspended */
68int __read_mostly timekeeping_suspended;
69
John Stultz1e75fa82012-07-13 01:21:53 -040070static inline void tk_normalize_xtime(struct timekeeper *tk)
71{
Peter Zijlstra876e7882015-03-19 10:09:06 +010072 while (tk->tkr_mono.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_mono.shift)) {
73 tk->tkr_mono.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
John Stultz1e75fa82012-07-13 01:21:53 -040074 tk->xtime_sec++;
75 }
John Stultz5311c742017-05-22 17:20:20 -070076 while (tk->tkr_raw.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_raw.shift)) {
77 tk->tkr_raw.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
78 tk->raw_sec++;
79 }
John Stultz1e75fa82012-07-13 01:21:53 -040080}
John Stultz8fcce542011-11-14 11:46:39 -080081
Thomas Gleixnerc905fae2014-07-16 21:04:05 +000082static inline struct timespec64 tk_xtime(struct timekeeper *tk)
83{
84 struct timespec64 ts;
85
86 ts.tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +010087 ts.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
Thomas Gleixnerc905fae2014-07-16 21:04:05 +000088 return ts;
89}
90
John Stultz7d489d12014-07-16 21:04:01 +000091static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
John Stultz1e75fa82012-07-13 01:21:53 -040092{
93 tk->xtime_sec = ts->tv_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +010094 tk->tkr_mono.xtime_nsec = (u64)ts->tv_nsec << tk->tkr_mono.shift;
John Stultz1e75fa82012-07-13 01:21:53 -040095}
96
John Stultz7d489d12014-07-16 21:04:01 +000097static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
John Stultz1e75fa82012-07-13 01:21:53 -040098{
99 tk->xtime_sec += ts->tv_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100100 tk->tkr_mono.xtime_nsec += (u64)ts->tv_nsec << tk->tkr_mono.shift;
John Stultz784ffcb2012-08-21 20:30:46 -0400101 tk_normalize_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -0400102}
John Stultz8fcce542011-11-14 11:46:39 -0800103
John Stultz7d489d12014-07-16 21:04:01 +0000104static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
John Stultz6d0ef902012-07-27 14:48:12 -0400105{
John Stultz7d489d12014-07-16 21:04:01 +0000106 struct timespec64 tmp;
John Stultz6d0ef902012-07-27 14:48:12 -0400107
108 /*
109 * Verify consistency of: offset_real = -wall_to_monotonic
110 * before modifying anything
111 */
John Stultz7d489d12014-07-16 21:04:01 +0000112 set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec,
John Stultz6d0ef902012-07-27 14:48:12 -0400113 -tk->wall_to_monotonic.tv_nsec);
John Stultz7d489d12014-07-16 21:04:01 +0000114 WARN_ON_ONCE(tk->offs_real.tv64 != timespec64_to_ktime(tmp).tv64);
John Stultz6d0ef902012-07-27 14:48:12 -0400115 tk->wall_to_monotonic = wtm;
John Stultz7d489d12014-07-16 21:04:01 +0000116 set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
117 tk->offs_real = timespec64_to_ktime(tmp);
John Stultz04005f62013-12-10 17:13:35 -0800118 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
John Stultz6d0ef902012-07-27 14:48:12 -0400119}
120
Thomas Gleixner47da70d2014-07-16 21:05:00 +0000121static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
John Stultz6d0ef902012-07-27 14:48:12 -0400122{
Thomas Gleixner47da70d2014-07-16 21:05:00 +0000123 tk->offs_boot = ktime_add(tk->offs_boot, delta);
John Stultz6d0ef902012-07-27 14:48:12 -0400124}
125
John Stultz02a37cc2017-06-08 16:44:20 -0700126/*
127 * tk_clock_read - atomic clocksource read() helper
128 *
129 * This helper is necessary to use in the read paths because, while the
130 * seqlock ensures we don't return a bad value while structures are updated,
131 * it doesn't protect from potential crashes. There is the possibility that
132 * the tkr's clocksource may change between the read reference, and the
133 * clock reference passed to the read function. This can cause crashes if
134 * the wrong clocksource is passed to the wrong read function.
135 * This isn't necessary to use when holding the timekeeper_lock or doing
136 * a read of the fast-timekeeper tkrs (which is protected by its own locking
137 * and update logic).
138 */
139static inline u64 tk_clock_read(struct tk_read_base *tkr)
140{
141 struct clocksource *clock = READ_ONCE(tkr->clock);
142
143 return clock->read(clock);
144}
145
John Stultz3c17ad12015-03-11 21:16:32 -0700146#ifdef CONFIG_DEBUG_TIMEKEEPING
John Stultz4ca22c22015-03-11 21:16:35 -0700147#define WARNING_FREQ (HZ*300) /* 5 minute rate-limiting */
John Stultz4ca22c22015-03-11 21:16:35 -0700148
John Stultz3c17ad12015-03-11 21:16:32 -0700149static void timekeeping_check_update(struct timekeeper *tk, cycle_t offset)
150{
151
Peter Zijlstra876e7882015-03-19 10:09:06 +0100152 cycle_t max_cycles = tk->tkr_mono.clock->max_cycles;
153 const char *name = tk->tkr_mono.clock->name;
John Stultz3c17ad12015-03-11 21:16:32 -0700154
155 if (offset > max_cycles) {
John Stultza558cd02015-03-11 21:16:33 -0700156 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 -0700157 offset, name, max_cycles);
John Stultza558cd02015-03-11 21:16:33 -0700158 printk_deferred(" timekeeping: Your kernel is sick, but tries to cope by capping time updates\n");
John Stultz3c17ad12015-03-11 21:16:32 -0700159 } else {
160 if (offset > (max_cycles >> 1)) {
Masanari Iidafc4fa6e2015-12-13 15:26:11 +0900161 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 -0700162 offset, name, max_cycles >> 1);
163 printk_deferred(" timekeeping: Your kernel is still fine, but is feeling a bit nervous\n");
164 }
165 }
John Stultz4ca22c22015-03-11 21:16:35 -0700166
John Stultz57d05a92015-05-13 16:04:47 -0700167 if (tk->underflow_seen) {
168 if (jiffies - tk->last_warning > WARNING_FREQ) {
John Stultz4ca22c22015-03-11 21:16:35 -0700169 printk_deferred("WARNING: Underflow in clocksource '%s' observed, time update ignored.\n", name);
170 printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
171 printk_deferred(" Your kernel is probably still fine.\n");
John Stultz57d05a92015-05-13 16:04:47 -0700172 tk->last_warning = jiffies;
John Stultz4ca22c22015-03-11 21:16:35 -0700173 }
John Stultz57d05a92015-05-13 16:04:47 -0700174 tk->underflow_seen = 0;
John Stultz4ca22c22015-03-11 21:16:35 -0700175 }
176
John Stultz57d05a92015-05-13 16:04:47 -0700177 if (tk->overflow_seen) {
178 if (jiffies - tk->last_warning > WARNING_FREQ) {
John Stultz4ca22c22015-03-11 21:16:35 -0700179 printk_deferred("WARNING: Overflow in clocksource '%s' observed, time update capped.\n", name);
180 printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
181 printk_deferred(" Your kernel is probably still fine.\n");
John Stultz57d05a92015-05-13 16:04:47 -0700182 tk->last_warning = jiffies;
John Stultz4ca22c22015-03-11 21:16:35 -0700183 }
John Stultz57d05a92015-05-13 16:04:47 -0700184 tk->overflow_seen = 0;
John Stultz4ca22c22015-03-11 21:16:35 -0700185 }
John Stultz3c17ad12015-03-11 21:16:32 -0700186}
John Stultza558cd02015-03-11 21:16:33 -0700187
188static inline cycle_t timekeeping_get_delta(struct tk_read_base *tkr)
189{
John Stultz57d05a92015-05-13 16:04:47 -0700190 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4ca22c22015-03-11 21:16:35 -0700191 cycle_t now, last, mask, max, delta;
192 unsigned int seq;
John Stultza558cd02015-03-11 21:16:33 -0700193
John Stultz4ca22c22015-03-11 21:16:35 -0700194 /*
195 * Since we're called holding a seqlock, the data may shift
196 * under us while we're doing the calculation. This can cause
197 * false positives, since we'd note a problem but throw the
198 * results away. So nest another seqlock here to atomically
199 * grab the points we are checking with.
200 */
201 do {
202 seq = read_seqcount_begin(&tk_core.seq);
John Stultz02a37cc2017-06-08 16:44:20 -0700203 now = tk_clock_read(tkr);
John Stultz4ca22c22015-03-11 21:16:35 -0700204 last = tkr->cycle_last;
205 mask = tkr->mask;
206 max = tkr->clock->max_cycles;
207 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultza558cd02015-03-11 21:16:33 -0700208
John Stultz4ca22c22015-03-11 21:16:35 -0700209 delta = clocksource_delta(now, last, mask);
John Stultza558cd02015-03-11 21:16:33 -0700210
John Stultz057b87e2015-03-11 21:16:34 -0700211 /*
212 * Try to catch underflows by checking if we are seeing small
213 * mask-relative negative values.
214 */
John Stultz4ca22c22015-03-11 21:16:35 -0700215 if (unlikely((~delta & mask) < (mask >> 3))) {
John Stultz57d05a92015-05-13 16:04:47 -0700216 tk->underflow_seen = 1;
John Stultz057b87e2015-03-11 21:16:34 -0700217 delta = 0;
John Stultz4ca22c22015-03-11 21:16:35 -0700218 }
John Stultz057b87e2015-03-11 21:16:34 -0700219
John Stultza558cd02015-03-11 21:16:33 -0700220 /* Cap delta value to the max_cycles values to avoid mult overflows */
John Stultz4ca22c22015-03-11 21:16:35 -0700221 if (unlikely(delta > max)) {
John Stultz57d05a92015-05-13 16:04:47 -0700222 tk->overflow_seen = 1;
John Stultza558cd02015-03-11 21:16:33 -0700223 delta = tkr->clock->max_cycles;
John Stultz4ca22c22015-03-11 21:16:35 -0700224 }
John Stultza558cd02015-03-11 21:16:33 -0700225
226 return delta;
227}
John Stultz3c17ad12015-03-11 21:16:32 -0700228#else
229static inline void timekeeping_check_update(struct timekeeper *tk, cycle_t offset)
230{
231}
John Stultza558cd02015-03-11 21:16:33 -0700232static inline cycle_t timekeeping_get_delta(struct tk_read_base *tkr)
233{
234 cycle_t cycle_now, delta;
235
236 /* read clocksource */
John Stultz02a37cc2017-06-08 16:44:20 -0700237 cycle_now = tk_clock_read(tkr);
John Stultza558cd02015-03-11 21:16:33 -0700238
239 /* calculate the delta since the last update_wall_time */
240 delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
241
242 return delta;
243}
John Stultz3c17ad12015-03-11 21:16:32 -0700244#endif
245
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200246/**
Yijing Wangd26e4fe2013-11-28 16:28:55 +0800247 * tk_setup_internals - Set up internals to use clocksource clock.
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200248 *
Yijing Wangd26e4fe2013-11-28 16:28:55 +0800249 * @tk: The target timekeeper to setup.
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200250 * @clock: Pointer to clocksource.
251 *
252 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
253 * pair and interval request.
254 *
255 * Unless you're the timekeeping code, you should not be using this!
256 */
John Stultzf726a692012-07-13 01:21:57 -0400257static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200258{
259 cycle_t interval;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700260 u64 tmp, ntpinterval;
John Stultz1e75fa82012-07-13 01:21:53 -0400261 struct clocksource *old_clock;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200262
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800263 ++tk->cs_was_changed_seq;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100264 old_clock = tk->tkr_mono.clock;
265 tk->tkr_mono.clock = clock;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100266 tk->tkr_mono.mask = clock->mask;
John Stultz02a37cc2017-06-08 16:44:20 -0700267 tk->tkr_mono.cycle_last = tk_clock_read(&tk->tkr_mono);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200268
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100269 tk->tkr_raw.clock = clock;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100270 tk->tkr_raw.mask = clock->mask;
271 tk->tkr_raw.cycle_last = tk->tkr_mono.cycle_last;
272
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200273 /* Do the ns -> cycle conversion first, using original mult */
274 tmp = NTP_INTERVAL_LENGTH;
275 tmp <<= clock->shift;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700276 ntpinterval = tmp;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200277 tmp += clock->mult/2;
278 do_div(tmp, clock->mult);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200279 if (tmp == 0)
280 tmp = 1;
281
282 interval = (cycle_t) tmp;
John Stultzf726a692012-07-13 01:21:57 -0400283 tk->cycle_interval = interval;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200284
285 /* Go back from cycles -> shifted ns */
John Stultzf726a692012-07-13 01:21:57 -0400286 tk->xtime_interval = (u64) interval * clock->mult;
287 tk->xtime_remainder = ntpinterval - tk->xtime_interval;
John Stultza53bfdd2017-06-08 16:44:21 -0700288 tk->raw_interval = interval * clock->mult;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200289
John Stultz1e75fa82012-07-13 01:21:53 -0400290 /* if changing clocks, convert xtime_nsec shift units */
291 if (old_clock) {
292 int shift_change = clock->shift - old_clock->shift;
John Stultz5311c742017-05-22 17:20:20 -0700293 if (shift_change < 0) {
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 } else {
Peter Zijlstra876e7882015-03-19 10:09:06 +0100297 tk->tkr_mono.xtime_nsec <<= shift_change;
John Stultz5311c742017-05-22 17:20:20 -0700298 tk->tkr_raw.xtime_nsec <<= shift_change;
299 }
John Stultz1e75fa82012-07-13 01:21:53 -0400300 }
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100301
Peter Zijlstra876e7882015-03-19 10:09:06 +0100302 tk->tkr_mono.shift = clock->shift;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100303 tk->tkr_raw.shift = clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200304
John Stultzf726a692012-07-13 01:21:57 -0400305 tk->ntp_error = 0;
306 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
John Stultz375f45b2014-04-23 20:53:29 -0700307 tk->ntp_tick = ntpinterval << tk->ntp_error_shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200308
309 /*
310 * The timekeeper keeps its own mult values for the currently
311 * active clocksource. These value will be adjusted via NTP
312 * to counteract clock drifting.
313 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100314 tk->tkr_mono.mult = clock->mult;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100315 tk->tkr_raw.mult = clock->mult;
John Stultzdc491592013-12-06 17:25:21 -0800316 tk->ntp_err_mult = 0;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200317}
john stultz85240702007-05-08 00:27:59 -0700318
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200319/* Timekeeper helper functions. */
Stephen Warren7b1f6202012-11-07 17:58:54 -0700320
321#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000322static u32 default_arch_gettimeoffset(void) { return 0; }
323u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset;
Stephen Warren7b1f6202012-11-07 17:58:54 -0700324#else
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000325static inline u32 arch_gettimeoffset(void) { return 0; }
Stephen Warren7b1f6202012-11-07 17:58:54 -0700326#endif
327
Thomas Gleixnerca229752016-12-08 20:49:32 +0000328static inline u64 timekeeping_delta_to_ns(struct tk_read_base *tkr,
Christopher S. Hall6bd58f02016-02-22 03:15:19 -0800329 cycle_t delta)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200330{
Thomas Gleixnerca229752016-12-08 20:49:32 +0000331 u64 nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200332
Christopher S. Hall6bd58f02016-02-22 03:15:19 -0800333 nsec = delta * tkr->mult + tkr->xtime_nsec;
334 nsec >>= tkr->shift;
John Stultzf2a5a082012-07-13 01:21:55 -0400335
Stephen Warren7b1f6202012-11-07 17:58:54 -0700336 /* If arch requires, add in get_arch_timeoffset() */
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000337 return nsec + arch_gettimeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200338}
339
Christopher S. Hall6bd58f02016-02-22 03:15:19 -0800340static inline s64 timekeeping_get_ns(struct tk_read_base *tkr)
341{
342 cycle_t delta;
343
344 delta = timekeeping_get_delta(tkr);
345 return timekeeping_delta_to_ns(tkr, delta);
346}
347
348static inline s64 timekeeping_cycles_to_ns(struct tk_read_base *tkr,
349 cycle_t cycles)
350{
351 cycle_t delta;
352
353 /* calculate the delta since the last update_wall_time */
354 delta = clocksource_delta(cycles, tkr->cycle_last, tkr->mask);
355 return timekeeping_delta_to_ns(tkr, delta);
356}
357
Thomas Gleixner4396e052014-07-16 21:05:23 +0000358/**
359 * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
Rafael J. Wysockiaffe3e82015-02-11 05:01:52 +0100360 * @tkr: Timekeeping readout base from which we take the update
Thomas Gleixner4396e052014-07-16 21:05:23 +0000361 *
362 * We want to use this from any context including NMI and tracing /
363 * instrumenting the timekeeping code itself.
364 *
Peter Zijlstra6695b922015-05-27 11:09:36 +0930365 * Employ the latch technique; see @raw_write_seqcount_latch.
Thomas Gleixner4396e052014-07-16 21:05:23 +0000366 *
367 * So if a NMI hits the update of base[0] then it will use base[1]
368 * which is still consistent. In the worst case this can result is a
369 * slightly wrong timestamp (a few nanoseconds). See
370 * @ktime_get_mono_fast_ns.
371 */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100372static void update_fast_timekeeper(struct tk_read_base *tkr, struct tk_fast *tkf)
Thomas Gleixner4396e052014-07-16 21:05:23 +0000373{
Peter Zijlstra4498e742015-03-19 09:36:19 +0100374 struct tk_read_base *base = tkf->base;
Thomas Gleixner4396e052014-07-16 21:05:23 +0000375
376 /* Force readers off to base[1] */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100377 raw_write_seqcount_latch(&tkf->seq);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000378
379 /* Update base[0] */
Rafael J. Wysockiaffe3e82015-02-11 05:01:52 +0100380 memcpy(base, tkr, sizeof(*base));
Thomas Gleixner4396e052014-07-16 21:05:23 +0000381
382 /* Force readers back to base[0] */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100383 raw_write_seqcount_latch(&tkf->seq);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000384
385 /* Update base[1] */
386 memcpy(base + 1, base, sizeof(*base));
387}
388
389/**
390 * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic
391 *
392 * This timestamp is not guaranteed to be monotonic across an update.
393 * The timestamp is calculated by:
394 *
395 * now = base_mono + clock_delta * slope
396 *
397 * So if the update lowers the slope, readers who are forced to the
398 * not yet updated second array are still using the old steeper slope.
399 *
400 * tmono
401 * ^
402 * | o n
403 * | o n
404 * | u
405 * | o
406 * |o
407 * |12345678---> reader order
408 *
409 * o = old slope
410 * u = update
411 * n = new slope
412 *
413 * So reader 6 will observe time going backwards versus reader 5.
414 *
415 * While other CPUs are likely to be able observe that, the only way
416 * for a CPU local observation is when an NMI hits in the middle of
417 * the update. Timestamps taken from that NMI context might be ahead
418 * of the following timestamps. Callers need to be aware of that and
419 * deal with it.
420 */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100421static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf)
Thomas Gleixner4396e052014-07-16 21:05:23 +0000422{
423 struct tk_read_base *tkr;
424 unsigned int seq;
425 u64 now;
426
427 do {
Peter Zijlstra7fc26322015-05-27 11:09:36 +0930428 seq = raw_read_seqcount_latch(&tkf->seq);
Peter Zijlstra4498e742015-03-19 09:36:19 +0100429 tkr = tkf->base + (seq & 0x01);
John Stultz27727df2016-08-23 16:08:21 -0700430 now = ktime_to_ns(tkr->base);
431
John Stultz58bfea92016-10-04 19:55:48 -0700432 now += timekeeping_delta_to_ns(tkr,
433 clocksource_delta(
John Stultz02a37cc2017-06-08 16:44:20 -0700434 tk_clock_read(tkr),
John Stultz58bfea92016-10-04 19:55:48 -0700435 tkr->cycle_last,
436 tkr->mask));
Peter Zijlstra4498e742015-03-19 09:36:19 +0100437 } while (read_seqcount_retry(&tkf->seq, seq));
Thomas Gleixner4396e052014-07-16 21:05:23 +0000438
Thomas Gleixner4396e052014-07-16 21:05:23 +0000439 return now;
440}
Peter Zijlstra4498e742015-03-19 09:36:19 +0100441
442u64 ktime_get_mono_fast_ns(void)
443{
444 return __ktime_get_fast_ns(&tk_fast_mono);
445}
Thomas Gleixner4396e052014-07-16 21:05:23 +0000446EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
447
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100448u64 ktime_get_raw_fast_ns(void)
449{
450 return __ktime_get_fast_ns(&tk_fast_raw);
451}
452EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns);
453
Joel Fernandes72408a62016-11-28 14:35:22 -0800454/**
455 * ktime_get_boot_fast_ns - NMI safe and fast access to boot clock.
456 *
457 * To keep it NMI safe since we're accessing from tracing, we're not using a
458 * separate timekeeper with updates to monotonic clock and boot offset
459 * protected with seqlocks. This has the following minor side effects:
460 *
461 * (1) Its possible that a timestamp be taken after the boot offset is updated
462 * but before the timekeeper is updated. If this happens, the new boot offset
463 * is added to the old timekeeping making the clock appear to update slightly
464 * earlier:
465 * CPU 0 CPU 1
466 * timekeeping_inject_sleeptime64()
467 * __timekeeping_inject_sleeptime(tk, delta);
468 * timestamp();
469 * timekeeping_update(tk, TK_CLEAR_NTP...);
470 *
471 * (2) On 32-bit systems, the 64-bit boot offset (tk->offs_boot) may be
472 * partially updated. Since the tk->offs_boot update is a rare event, this
473 * should be a rare occurrence which postprocessing should be able to handle.
474 */
475u64 notrace ktime_get_boot_fast_ns(void)
476{
477 struct timekeeper *tk = &tk_core.timekeeper;
478
479 return (ktime_get_mono_fast_ns() + ktime_to_ns(tk->offs_boot));
480}
481EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns);
482
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100483/* Suspend-time cycles value for halted fast timekeeper. */
484static cycle_t cycles_at_suspend;
485
486static cycle_t dummy_clock_read(struct clocksource *cs)
487{
488 return cycles_at_suspend;
489}
490
John Stultz02a37cc2017-06-08 16:44:20 -0700491static struct clocksource dummy_clock = {
492 .read = dummy_clock_read,
493};
494
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100495/**
496 * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource.
497 * @tk: Timekeeper to snapshot.
498 *
499 * It generally is unsafe to access the clocksource after timekeeping has been
500 * suspended, so take a snapshot of the readout base of @tk and use it as the
501 * fast timekeeper's readout base while suspended. It will return the same
502 * number of cycles every time until timekeeping is resumed at which time the
503 * proper readout base for the fast timekeeper will be restored automatically.
504 */
505static void halt_fast_timekeeper(struct timekeeper *tk)
506{
507 static struct tk_read_base tkr_dummy;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100508 struct tk_read_base *tkr = &tk->tkr_mono;
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100509
510 memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
John Stultz02a37cc2017-06-08 16:44:20 -0700511 cycles_at_suspend = tk_clock_read(tkr);
512 tkr_dummy.clock = &dummy_clock;
Peter Zijlstra4498e742015-03-19 09:36:19 +0100513 update_fast_timekeeper(&tkr_dummy, &tk_fast_mono);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100514
515 tkr = &tk->tkr_raw;
516 memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
John Stultz02a37cc2017-06-08 16:44:20 -0700517 tkr_dummy.clock = &dummy_clock;
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100518 update_fast_timekeeper(&tkr_dummy, &tk_fast_raw);
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100519}
520
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000521#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
522
523static inline void update_vsyscall(struct timekeeper *tk)
524{
John Stultz0680eb12014-08-13 12:47:14 -0700525 struct timespec xt, wm;
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000526
John Stultze2dff1e2014-07-23 14:35:39 -0700527 xt = timespec64_to_timespec(tk_xtime(tk));
John Stultz0680eb12014-08-13 12:47:14 -0700528 wm = timespec64_to_timespec(tk->wall_to_monotonic);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100529 update_vsyscall_old(&xt, &wm, tk->tkr_mono.clock, tk->tkr_mono.mult,
530 tk->tkr_mono.cycle_last);
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000531}
532
533static inline void old_vsyscall_fixup(struct timekeeper *tk)
534{
535 s64 remainder;
536
537 /*
538 * Store only full nanoseconds into xtime_nsec after rounding
539 * it up and add the remainder to the error difference.
540 * XXX - This is necessary to avoid small 1ns inconsistnecies caused
541 * by truncating the remainder in vsyscalls. However, it causes
542 * additional work to be done in timekeeping_adjust(). Once
543 * the vsyscall implementations are converted to use xtime_nsec
544 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
545 * users are removed, this can be killed.
546 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100547 remainder = tk->tkr_mono.xtime_nsec & ((1ULL << tk->tkr_mono.shift) - 1);
Thomas Graziadei0209b932016-05-31 15:06:06 +0200548 if (remainder != 0) {
549 tk->tkr_mono.xtime_nsec -= remainder;
550 tk->tkr_mono.xtime_nsec += 1ULL << tk->tkr_mono.shift;
551 tk->ntp_error += remainder << tk->ntp_error_shift;
552 tk->ntp_error -= (1ULL << tk->tkr_mono.shift) << tk->ntp_error_shift;
553 }
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000554}
555#else
556#define old_vsyscall_fixup(tk)
557#endif
558
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200559static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
560
David Vrabel780427f2013-06-27 11:35:46 +0100561static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200562{
David Vrabel780427f2013-06-27 11:35:46 +0100563 raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200564}
565
566/**
567 * pvclock_gtod_register_notifier - register a pvclock timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200568 */
569int pvclock_gtod_register_notifier(struct notifier_block *nb)
570{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000571 struct timekeeper *tk = &tk_core.timekeeper;
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200572 unsigned long flags;
573 int ret;
574
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000575 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200576 ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
David Vrabel780427f2013-06-27 11:35:46 +0100577 update_pvclock_gtod(tk, true);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000578 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200579
580 return ret;
581}
582EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
583
584/**
585 * pvclock_gtod_unregister_notifier - unregister a pvclock
586 * timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200587 */
588int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
589{
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200590 unsigned long flags;
591 int ret;
592
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000593 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200594 ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000595 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200596
597 return ret;
598}
599EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
600
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000601/*
John Stultz833f32d2015-06-11 15:54:55 -0700602 * tk_update_leap_state - helper to update the next_leap_ktime
603 */
604static inline void tk_update_leap_state(struct timekeeper *tk)
605{
606 tk->next_leap_ktime = ntp_get_next_leap();
607 if (tk->next_leap_ktime.tv64 != KTIME_MAX)
608 /* Convert to monotonic time */
609 tk->next_leap_ktime = ktime_sub(tk->next_leap_ktime, tk->offs_real);
610}
611
612/*
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000613 * Update the ktime_t based scalar nsec members of the timekeeper
614 */
615static inline void tk_update_ktime_data(struct timekeeper *tk)
616{
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530617 u64 seconds;
618 u32 nsec;
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000619
620 /*
621 * The xtime based monotonic readout is:
622 * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();
623 * The ktime based monotonic readout is:
624 * nsec = base_mono + now();
625 * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
626 */
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530627 seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
628 nsec = (u32) tk->wall_to_monotonic.tv_nsec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100629 tk->tkr_mono.base = ns_to_ktime(seconds * NSEC_PER_SEC + nsec);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000630
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530631 /*
632 * The sum of the nanoseconds portions of xtime and
633 * wall_to_monotonic can be greater/equal one second. Take
634 * this into account before updating tk->ktime_sec.
635 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100636 nsec += (u32)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530637 if (nsec >= NSEC_PER_SEC)
638 seconds++;
639 tk->ktime_sec = seconds;
John Stultz5311c742017-05-22 17:20:20 -0700640
641 /* Update the monotonic raw base */
John Stultzd2c57b62017-08-25 15:57:04 -0700642 tk->tkr_raw.base = ns_to_ktime(tk->raw_sec * NSEC_PER_SEC);
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000643}
644
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000645/* must hold timekeeper_lock */
David Vrabel04397fe92013-06-27 11:35:45 +0100646static void timekeeping_update(struct timekeeper *tk, unsigned int action)
Thomas Gleixnercc062682011-11-13 23:19:49 +0000647{
David Vrabel04397fe92013-06-27 11:35:45 +0100648 if (action & TK_CLEAR_NTP) {
John Stultzf726a692012-07-13 01:21:57 -0400649 tk->ntp_error = 0;
Thomas Gleixnercc062682011-11-13 23:19:49 +0000650 ntp_clear();
651 }
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000652
John Stultz833f32d2015-06-11 15:54:55 -0700653 tk_update_leap_state(tk);
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000654 tk_update_ktime_data(tk);
655
Thomas Gleixner9bf24192014-09-06 12:24:49 +0200656 update_vsyscall(tk);
657 update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
658
Peter Zijlstra4498e742015-03-19 09:36:19 +0100659 update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100660 update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw);
Thomas Gleixner868a3e92015-04-14 21:08:37 +0000661
662 if (action & TK_CLOCK_WAS_SET)
663 tk->clock_was_set_seq++;
John Stultzd1518322015-06-11 15:54:53 -0700664 /*
665 * The mirroring of the data to the shadow-timekeeper needs
666 * to happen last here to ensure we don't over-write the
667 * timekeeper structure on the next update with stale data
668 */
669 if (action & TK_MIRROR)
670 memcpy(&shadow_timekeeper, &tk_core.timekeeper,
671 sizeof(tk_core.timekeeper));
Thomas Gleixnercc062682011-11-13 23:19:49 +0000672}
673
john stultz85240702007-05-08 00:27:59 -0700674/**
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200675 * timekeeping_forward_now - update clock to the current time
john stultz85240702007-05-08 00:27:59 -0700676 *
Roman Zippel9a055112008-08-20 16:37:28 -0700677 * Forward the current clock to update its state since the last call to
678 * update_wall_time(). This is useful before significant clock changes,
679 * as it avoids having to deal with this time offset explicitly.
john stultz85240702007-05-08 00:27:59 -0700680 */
John Stultzf726a692012-07-13 01:21:57 -0400681static void timekeeping_forward_now(struct timekeeper *tk)
john stultz85240702007-05-08 00:27:59 -0700682{
Thomas Gleixner3a978372014-07-16 21:05:10 +0000683 cycle_t cycle_now, delta;
john stultz85240702007-05-08 00:27:59 -0700684
John Stultz02a37cc2017-06-08 16:44:20 -0700685 cycle_now = tk_clock_read(&tk->tkr_mono);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100686 delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
687 tk->tkr_mono.cycle_last = cycle_now;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100688 tk->tkr_raw.cycle_last = cycle_now;
john stultz85240702007-05-08 00:27:59 -0700689
Peter Zijlstra876e7882015-03-19 10:09:06 +0100690 tk->tkr_mono.xtime_nsec += delta * tk->tkr_mono.mult;
john stultz7d275582009-05-01 13:10:26 -0700691
Stephen Warren7b1f6202012-11-07 17:58:54 -0700692 /* If arch requires, add in get_arch_timeoffset() */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100693 tk->tkr_mono.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_mono.shift;
john stultz7d275582009-05-01 13:10:26 -0700694
John Stultz2d422442008-08-20 16:37:30 -0700695
John Stultz5311c742017-05-22 17:20:20 -0700696 tk->tkr_raw.xtime_nsec += delta * tk->tkr_raw.mult;
697
698 /* If arch requires, add in get_arch_timeoffset() */
699 tk->tkr_raw.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_raw.shift;
700
701 tk_normalize_xtime(tk);
john stultz85240702007-05-08 00:27:59 -0700702}
703
704/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000705 * __getnstimeofday64 - Returns the time of day in a timespec64.
john stultz85240702007-05-08 00:27:59 -0700706 * @ts: pointer to the timespec to be set
707 *
Kees Cook1e817fb2012-11-19 10:26:16 -0800708 * Updates the time of day in the timespec.
709 * Returns 0 on success, or -ve when suspended (timespec will be undefined).
john stultz85240702007-05-08 00:27:59 -0700710 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000711int __getnstimeofday64(struct timespec64 *ts)
john stultz85240702007-05-08 00:27:59 -0700712{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000713 struct timekeeper *tk = &tk_core.timekeeper;
john stultz85240702007-05-08 00:27:59 -0700714 unsigned long seq;
John Stultz1e75fa82012-07-13 01:21:53 -0400715 s64 nsecs = 0;
john stultz85240702007-05-08 00:27:59 -0700716
717 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000718 seq = read_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -0700719
John Stultz4e250fd2012-07-27 14:48:13 -0400720 ts->tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100721 nsecs = timekeeping_get_ns(&tk->tkr_mono);
john stultz85240702007-05-08 00:27:59 -0700722
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000723 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz85240702007-05-08 00:27:59 -0700724
John Stultzec145ba2012-09-11 19:26:03 -0400725 ts->tv_nsec = 0;
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000726 timespec64_add_ns(ts, nsecs);
Kees Cook1e817fb2012-11-19 10:26:16 -0800727
728 /*
729 * Do not bail out early, in case there were callers still using
730 * the value, even in the face of the WARN_ON.
731 */
732 if (unlikely(timekeeping_suspended))
733 return -EAGAIN;
734 return 0;
735}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000736EXPORT_SYMBOL(__getnstimeofday64);
Kees Cook1e817fb2012-11-19 10:26:16 -0800737
738/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000739 * getnstimeofday64 - Returns the time of day in a timespec64.
John Stultz5322e4c2014-11-07 13:13:04 -0800740 * @ts: pointer to the timespec64 to be set
Kees Cook1e817fb2012-11-19 10:26:16 -0800741 *
John Stultz5322e4c2014-11-07 13:13:04 -0800742 * Returns the time of day in a timespec64 (WARN if suspended).
Kees Cook1e817fb2012-11-19 10:26:16 -0800743 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000744void getnstimeofday64(struct timespec64 *ts)
Kees Cook1e817fb2012-11-19 10:26:16 -0800745{
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000746 WARN_ON(__getnstimeofday64(ts));
john stultz85240702007-05-08 00:27:59 -0700747}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000748EXPORT_SYMBOL(getnstimeofday64);
john stultz85240702007-05-08 00:27:59 -0700749
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200750ktime_t ktime_get(void)
751{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000752 struct timekeeper *tk = &tk_core.timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200753 unsigned int seq;
Thomas Gleixnera016a5b2014-07-16 21:04:12 +0000754 ktime_t base;
755 s64 nsecs;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200756
757 WARN_ON(timekeeping_suspended);
758
759 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000760 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100761 base = tk->tkr_mono.base;
762 nsecs = timekeeping_get_ns(&tk->tkr_mono);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200763
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000764 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz24e4a8c2014-07-16 21:03:53 +0000765
Thomas Gleixnera016a5b2014-07-16 21:04:12 +0000766 return ktime_add_ns(base, nsecs);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200767}
768EXPORT_SYMBOL_GPL(ktime_get);
769
Harald Geyer6374f912015-04-07 11:12:35 +0000770u32 ktime_get_resolution_ns(void)
771{
772 struct timekeeper *tk = &tk_core.timekeeper;
773 unsigned int seq;
774 u32 nsecs;
775
776 WARN_ON(timekeeping_suspended);
777
778 do {
779 seq = read_seqcount_begin(&tk_core.seq);
780 nsecs = tk->tkr_mono.mult >> tk->tkr_mono.shift;
781 } while (read_seqcount_retry(&tk_core.seq, seq));
782
783 return nsecs;
784}
785EXPORT_SYMBOL_GPL(ktime_get_resolution_ns);
786
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000787static ktime_t *offsets[TK_OFFS_MAX] = {
788 [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real,
789 [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot,
790 [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai,
791};
792
793ktime_t ktime_get_with_offset(enum tk_offsets offs)
794{
795 struct timekeeper *tk = &tk_core.timekeeper;
796 unsigned int seq;
797 ktime_t base, *offset = offsets[offs];
798 s64 nsecs;
799
800 WARN_ON(timekeeping_suspended);
801
802 do {
803 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100804 base = ktime_add(tk->tkr_mono.base, *offset);
805 nsecs = timekeeping_get_ns(&tk->tkr_mono);
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000806
807 } while (read_seqcount_retry(&tk_core.seq, seq));
808
809 return ktime_add_ns(base, nsecs);
810
811}
812EXPORT_SYMBOL_GPL(ktime_get_with_offset);
813
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200814/**
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000815 * ktime_mono_to_any() - convert mononotic time to any other time
816 * @tmono: time to convert.
817 * @offs: which offset to use
818 */
819ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
820{
821 ktime_t *offset = offsets[offs];
822 unsigned long seq;
823 ktime_t tconv;
824
825 do {
826 seq = read_seqcount_begin(&tk_core.seq);
827 tconv = ktime_add(tmono, *offset);
828 } while (read_seqcount_retry(&tk_core.seq, seq));
829
830 return tconv;
831}
832EXPORT_SYMBOL_GPL(ktime_mono_to_any);
833
834/**
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000835 * ktime_get_raw - Returns the raw monotonic time in ktime_t format
836 */
837ktime_t ktime_get_raw(void)
838{
839 struct timekeeper *tk = &tk_core.timekeeper;
840 unsigned int seq;
841 ktime_t base;
842 s64 nsecs;
843
844 do {
845 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100846 base = tk->tkr_raw.base;
847 nsecs = timekeeping_get_ns(&tk->tkr_raw);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000848
849 } while (read_seqcount_retry(&tk_core.seq, seq));
850
851 return ktime_add_ns(base, nsecs);
852}
853EXPORT_SYMBOL_GPL(ktime_get_raw);
854
855/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000856 * ktime_get_ts64 - get the monotonic clock in timespec64 format
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200857 * @ts: pointer to timespec variable
858 *
859 * The function calculates the monotonic clock from the realtime
860 * clock and the wall_to_monotonic offset and stores the result
John Stultz5322e4c2014-11-07 13:13:04 -0800861 * in normalized timespec64 format in the variable pointed to by @ts.
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200862 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000863void ktime_get_ts64(struct timespec64 *ts)
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200864{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000865 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000866 struct timespec64 tomono;
John Stultzec145ba2012-09-11 19:26:03 -0400867 s64 nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200868 unsigned int seq;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200869
870 WARN_ON(timekeeping_suspended);
871
872 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000873 seq = read_seqcount_begin(&tk_core.seq);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000874 ts->tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100875 nsec = timekeeping_get_ns(&tk->tkr_mono);
John Stultz4e250fd2012-07-27 14:48:13 -0400876 tomono = tk->wall_to_monotonic;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200877
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000878 } while (read_seqcount_retry(&tk_core.seq, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200879
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000880 ts->tv_sec += tomono.tv_sec;
881 ts->tv_nsec = 0;
882 timespec64_add_ns(ts, nsec + tomono.tv_nsec);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200883}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000884EXPORT_SYMBOL_GPL(ktime_get_ts64);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200885
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530886/**
887 * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC
888 *
889 * Returns the seconds portion of CLOCK_MONOTONIC with a single non
890 * serialized read. tk->ktime_sec is of type 'unsigned long' so this
891 * works on both 32 and 64 bit systems. On 32 bit systems the readout
892 * covers ~136 years of uptime which should be enough to prevent
893 * premature wrap arounds.
894 */
895time64_t ktime_get_seconds(void)
896{
897 struct timekeeper *tk = &tk_core.timekeeper;
898
899 WARN_ON(timekeeping_suspended);
900 return tk->ktime_sec;
901}
902EXPORT_SYMBOL_GPL(ktime_get_seconds);
903
Heena Sirwanidbe7aa62014-10-29 16:01:50 +0530904/**
905 * ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME
906 *
907 * Returns the wall clock seconds since 1970. This replaces the
908 * get_seconds() interface which is not y2038 safe on 32bit systems.
909 *
910 * For 64bit systems the fast access to tk->xtime_sec is preserved. On
911 * 32bit systems the access must be protected with the sequence
912 * counter to provide "atomic" access to the 64bit tk->xtime_sec
913 * value.
914 */
915time64_t ktime_get_real_seconds(void)
916{
917 struct timekeeper *tk = &tk_core.timekeeper;
918 time64_t seconds;
919 unsigned int seq;
920
921 if (IS_ENABLED(CONFIG_64BIT))
922 return tk->xtime_sec;
923
924 do {
925 seq = read_seqcount_begin(&tk_core.seq);
926 seconds = tk->xtime_sec;
927
928 } while (read_seqcount_retry(&tk_core.seq, seq));
929
930 return seconds;
931}
932EXPORT_SYMBOL_GPL(ktime_get_real_seconds);
933
DengChaodee36652015-12-13 12:24:18 +0800934/**
935 * __ktime_get_real_seconds - The same as ktime_get_real_seconds
936 * but without the sequence counter protect. This internal function
937 * is called just when timekeeping lock is already held.
938 */
939time64_t __ktime_get_real_seconds(void)
940{
941 struct timekeeper *tk = &tk_core.timekeeper;
942
943 return tk->xtime_sec;
944}
945
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800946/**
947 * ktime_get_snapshot - snapshots the realtime/monotonic raw clocks with counter
948 * @systime_snapshot: pointer to struct receiving the system time snapshot
949 */
950void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
951{
952 struct timekeeper *tk = &tk_core.timekeeper;
953 unsigned long seq;
954 ktime_t base_raw;
955 ktime_t base_real;
956 s64 nsec_raw;
957 s64 nsec_real;
958 cycle_t now;
959
Christopher S. Hallba266212016-02-22 03:15:21 -0800960 WARN_ON_ONCE(timekeeping_suspended);
961
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800962 do {
963 seq = read_seqcount_begin(&tk_core.seq);
John Stultz02a37cc2017-06-08 16:44:20 -0700964 now = tk_clock_read(&tk->tkr_mono);
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800965 systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq;
966 systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq;
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800967 base_real = ktime_add(tk->tkr_mono.base,
968 tk_core.timekeeper.offs_real);
969 base_raw = tk->tkr_raw.base;
970 nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, now);
971 nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw, now);
972 } while (read_seqcount_retry(&tk_core.seq, seq));
973
974 systime_snapshot->cycles = now;
975 systime_snapshot->real = ktime_add_ns(base_real, nsec_real);
976 systime_snapshot->raw = ktime_add_ns(base_raw, nsec_raw);
977}
978EXPORT_SYMBOL_GPL(ktime_get_snapshot);
DengChaodee36652015-12-13 12:24:18 +0800979
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800980/* Scale base by mult/div checking for overflow */
981static int scale64_check_overflow(u64 mult, u64 div, u64 *base)
982{
983 u64 tmp, rem;
984
985 tmp = div64_u64_rem(*base, div, &rem);
986
987 if (((int)sizeof(u64)*8 - fls64(mult) < fls64(tmp)) ||
988 ((int)sizeof(u64)*8 - fls64(mult) < fls64(rem)))
989 return -EOVERFLOW;
990 tmp *= mult;
991 rem *= mult;
992
993 do_div(rem, div);
994 *base = tmp + rem;
995 return 0;
996}
997
998/**
999 * adjust_historical_crosststamp - adjust crosstimestamp previous to current interval
1000 * @history: Snapshot representing start of history
1001 * @partial_history_cycles: Cycle offset into history (fractional part)
1002 * @total_history_cycles: Total history length in cycles
1003 * @discontinuity: True indicates clock was set on history period
1004 * @ts: Cross timestamp that should be adjusted using
1005 * partial/total ratio
1006 *
1007 * Helper function used by get_device_system_crosststamp() to correct the
1008 * crosstimestamp corresponding to the start of the current interval to the
1009 * system counter value (timestamp point) provided by the driver. The
1010 * total_history_* quantities are the total history starting at the provided
1011 * reference point and ending at the start of the current interval. The cycle
1012 * count between the driver timestamp point and the start of the current
1013 * interval is partial_history_cycles.
1014 */
1015static int adjust_historical_crosststamp(struct system_time_snapshot *history,
1016 cycle_t partial_history_cycles,
1017 cycle_t total_history_cycles,
1018 bool discontinuity,
1019 struct system_device_crosststamp *ts)
1020{
1021 struct timekeeper *tk = &tk_core.timekeeper;
1022 u64 corr_raw, corr_real;
1023 bool interp_forward;
1024 int ret;
1025
1026 if (total_history_cycles == 0 || partial_history_cycles == 0)
1027 return 0;
1028
1029 /* Interpolate shortest distance from beginning or end of history */
1030 interp_forward = partial_history_cycles > total_history_cycles/2 ?
1031 true : false;
1032 partial_history_cycles = interp_forward ?
1033 total_history_cycles - partial_history_cycles :
1034 partial_history_cycles;
1035
1036 /*
1037 * Scale the monotonic raw time delta by:
1038 * partial_history_cycles / total_history_cycles
1039 */
1040 corr_raw = (u64)ktime_to_ns(
1041 ktime_sub(ts->sys_monoraw, history->raw));
1042 ret = scale64_check_overflow(partial_history_cycles,
1043 total_history_cycles, &corr_raw);
1044 if (ret)
1045 return ret;
1046
1047 /*
1048 * If there is a discontinuity in the history, scale monotonic raw
1049 * correction by:
1050 * mult(real)/mult(raw) yielding the realtime correction
1051 * Otherwise, calculate the realtime correction similar to monotonic
1052 * raw calculation
1053 */
1054 if (discontinuity) {
1055 corr_real = mul_u64_u32_div
1056 (corr_raw, tk->tkr_mono.mult, tk->tkr_raw.mult);
1057 } else {
1058 corr_real = (u64)ktime_to_ns(
1059 ktime_sub(ts->sys_realtime, history->real));
1060 ret = scale64_check_overflow(partial_history_cycles,
1061 total_history_cycles, &corr_real);
1062 if (ret)
1063 return ret;
1064 }
1065
1066 /* Fixup monotonic raw and real time time values */
1067 if (interp_forward) {
1068 ts->sys_monoraw = ktime_add_ns(history->raw, corr_raw);
1069 ts->sys_realtime = ktime_add_ns(history->real, corr_real);
1070 } else {
1071 ts->sys_monoraw = ktime_sub_ns(ts->sys_monoraw, corr_raw);
1072 ts->sys_realtime = ktime_sub_ns(ts->sys_realtime, corr_real);
1073 }
1074
1075 return 0;
1076}
1077
1078/*
1079 * cycle_between - true if test occurs chronologically between before and after
1080 */
1081static bool cycle_between(cycle_t before, cycle_t test, cycle_t after)
1082{
1083 if (test > before && test < after)
1084 return true;
1085 if (test < before && before > after)
1086 return true;
1087 return false;
1088}
1089
john stultz85240702007-05-08 00:27:59 -07001090/**
Christopher S. Hall8006c242016-02-22 03:15:22 -08001091 * get_device_system_crosststamp - Synchronously capture system/device timestamp
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001092 * @get_time_fn: Callback to get simultaneous device time and
Christopher S. Hall8006c242016-02-22 03:15:22 -08001093 * system counter from the device driver
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001094 * @ctx: Context passed to get_time_fn()
1095 * @history_begin: Historical reference point used to interpolate system
1096 * time when counter provided by the driver is before the current interval
Christopher S. Hall8006c242016-02-22 03:15:22 -08001097 * @xtstamp: Receives simultaneously captured system and device time
1098 *
1099 * Reads a timestamp from a device and correlates it to system time
1100 */
1101int get_device_system_crosststamp(int (*get_time_fn)
1102 (ktime_t *device_time,
1103 struct system_counterval_t *sys_counterval,
1104 void *ctx),
1105 void *ctx,
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001106 struct system_time_snapshot *history_begin,
Christopher S. Hall8006c242016-02-22 03:15:22 -08001107 struct system_device_crosststamp *xtstamp)
1108{
1109 struct system_counterval_t system_counterval;
1110 struct timekeeper *tk = &tk_core.timekeeper;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001111 cycle_t cycles, now, interval_start;
Ingo Molnar64362572016-03-08 11:09:53 +01001112 unsigned int clock_was_set_seq = 0;
Christopher S. Hall8006c242016-02-22 03:15:22 -08001113 ktime_t base_real, base_raw;
1114 s64 nsec_real, nsec_raw;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001115 u8 cs_was_changed_seq;
Christopher S. Hall8006c242016-02-22 03:15:22 -08001116 unsigned long seq;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001117 bool do_interp;
Christopher S. Hall8006c242016-02-22 03:15:22 -08001118 int ret;
1119
1120 do {
1121 seq = read_seqcount_begin(&tk_core.seq);
1122 /*
1123 * Try to synchronously capture device time and a system
1124 * counter value calling back into the device driver
1125 */
1126 ret = get_time_fn(&xtstamp->device, &system_counterval, ctx);
1127 if (ret)
1128 return ret;
1129
1130 /*
1131 * Verify that the clocksource associated with the captured
1132 * system counter value is the same as the currently installed
1133 * timekeeper clocksource
1134 */
1135 if (tk->tkr_mono.clock != system_counterval.cs)
1136 return -ENODEV;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001137 cycles = system_counterval.cycles;
1138
1139 /*
1140 * Check whether the system counter value provided by the
1141 * device driver is on the current timekeeping interval.
1142 */
John Stultz02a37cc2017-06-08 16:44:20 -07001143 now = tk_clock_read(&tk->tkr_mono);
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001144 interval_start = tk->tkr_mono.cycle_last;
1145 if (!cycle_between(interval_start, cycles, now)) {
1146 clock_was_set_seq = tk->clock_was_set_seq;
1147 cs_was_changed_seq = tk->cs_was_changed_seq;
1148 cycles = interval_start;
1149 do_interp = true;
1150 } else {
1151 do_interp = false;
1152 }
Christopher S. Hall8006c242016-02-22 03:15:22 -08001153
1154 base_real = ktime_add(tk->tkr_mono.base,
1155 tk_core.timekeeper.offs_real);
1156 base_raw = tk->tkr_raw.base;
1157
1158 nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono,
1159 system_counterval.cycles);
1160 nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw,
1161 system_counterval.cycles);
1162 } while (read_seqcount_retry(&tk_core.seq, seq));
1163
1164 xtstamp->sys_realtime = ktime_add_ns(base_real, nsec_real);
1165 xtstamp->sys_monoraw = ktime_add_ns(base_raw, nsec_raw);
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001166
1167 /*
1168 * Interpolate if necessary, adjusting back from the start of the
1169 * current interval
1170 */
1171 if (do_interp) {
1172 cycle_t partial_history_cycles, total_history_cycles;
1173 bool discontinuity;
1174
1175 /*
1176 * Check that the counter value occurs after the provided
1177 * history reference and that the history doesn't cross a
1178 * clocksource change
1179 */
1180 if (!history_begin ||
1181 !cycle_between(history_begin->cycles,
1182 system_counterval.cycles, cycles) ||
1183 history_begin->cs_was_changed_seq != cs_was_changed_seq)
1184 return -EINVAL;
1185 partial_history_cycles = cycles - system_counterval.cycles;
1186 total_history_cycles = cycles - history_begin->cycles;
1187 discontinuity =
1188 history_begin->clock_was_set_seq != clock_was_set_seq;
1189
1190 ret = adjust_historical_crosststamp(history_begin,
1191 partial_history_cycles,
1192 total_history_cycles,
1193 discontinuity, xtstamp);
1194 if (ret)
1195 return ret;
1196 }
1197
Christopher S. Hall8006c242016-02-22 03:15:22 -08001198 return 0;
1199}
1200EXPORT_SYMBOL_GPL(get_device_system_crosststamp);
1201
1202/**
john stultz85240702007-05-08 00:27:59 -07001203 * do_gettimeofday - Returns the time of day in a timeval
1204 * @tv: pointer to the timeval to be set
1205 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +01001206 * NOTE: Users should be converted to using getnstimeofday()
john stultz85240702007-05-08 00:27:59 -07001207 */
1208void do_gettimeofday(struct timeval *tv)
1209{
Thomas Gleixnerd6d29892014-07-16 21:04:04 +00001210 struct timespec64 now;
john stultz85240702007-05-08 00:27:59 -07001211
Thomas Gleixnerd6d29892014-07-16 21:04:04 +00001212 getnstimeofday64(&now);
john stultz85240702007-05-08 00:27:59 -07001213 tv->tv_sec = now.tv_sec;
1214 tv->tv_usec = now.tv_nsec/1000;
1215}
john stultz85240702007-05-08 00:27:59 -07001216EXPORT_SYMBOL(do_gettimeofday);
Richard Cochrand239f492012-04-27 10:12:42 +02001217
john stultz85240702007-05-08 00:27:59 -07001218/**
pang.xunlei21f7eca2014-11-18 19:15:16 +08001219 * do_settimeofday64 - Sets the time of day.
1220 * @ts: pointer to the timespec64 variable containing the new time
john stultz85240702007-05-08 00:27:59 -07001221 *
1222 * Sets the time of day to the new time and update NTP and notify hrtimers
1223 */
pang.xunlei21f7eca2014-11-18 19:15:16 +08001224int do_settimeofday64(const struct timespec64 *ts)
john stultz85240702007-05-08 00:27:59 -07001225{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001226 struct timekeeper *tk = &tk_core.timekeeper;
pang.xunlei21f7eca2014-11-18 19:15:16 +08001227 struct timespec64 ts_delta, xt;
John Stultz92c1d3e2011-11-14 14:05:44 -08001228 unsigned long flags;
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001229 int ret = 0;
john stultz85240702007-05-08 00:27:59 -07001230
pang.xunlei21f7eca2014-11-18 19:15:16 +08001231 if (!timespec64_valid_strict(ts))
john stultz85240702007-05-08 00:27:59 -07001232 return -EINVAL;
1233
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001234 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001235 write_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -07001236
John Stultz4e250fd2012-07-27 14:48:13 -04001237 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -07001238
John Stultz4e250fd2012-07-27 14:48:13 -04001239 xt = tk_xtime(tk);
pang.xunlei21f7eca2014-11-18 19:15:16 +08001240 ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
1241 ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
John Stultz1e75fa82012-07-13 01:21:53 -04001242
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001243 if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) {
1244 ret = -EINVAL;
1245 goto out;
1246 }
1247
John Stultz7d489d12014-07-16 21:04:01 +00001248 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
john stultz85240702007-05-08 00:27:59 -07001249
pang.xunlei21f7eca2014-11-18 19:15:16 +08001250 tk_set_xtime(tk, ts);
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001251out:
David Vrabel780427f2013-06-27 11:35:46 +01001252 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
john stultz85240702007-05-08 00:27:59 -07001253
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001254 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001255 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001256
1257 /* signal hrtimers about time change */
1258 clock_was_set();
1259
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001260 return ret;
john stultz85240702007-05-08 00:27:59 -07001261}
pang.xunlei21f7eca2014-11-18 19:15:16 +08001262EXPORT_SYMBOL(do_settimeofday64);
john stultz85240702007-05-08 00:27:59 -07001263
John Stultzc528f7c2011-02-01 13:52:17 +00001264/**
1265 * timekeeping_inject_offset - Adds or subtracts from the current time.
1266 * @tv: pointer to the timespec variable containing the offset
1267 *
1268 * Adds or subtracts an offset value from the current time.
1269 */
1270int timekeeping_inject_offset(struct timespec *ts)
1271{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001272 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -08001273 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001274 struct timespec64 ts64, tmp;
John Stultz4e8b1452012-08-08 15:36:20 -04001275 int ret = 0;
John Stultzc528f7c2011-02-01 13:52:17 +00001276
John Stultz37cf4dc2015-12-03 22:09:31 -05001277 if (!timespec_inject_offset_valid(ts))
John Stultzc528f7c2011-02-01 13:52:17 +00001278 return -EINVAL;
1279
John Stultz7d489d12014-07-16 21:04:01 +00001280 ts64 = timespec_to_timespec64(*ts);
1281
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001282 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001283 write_seqcount_begin(&tk_core.seq);
John Stultzc528f7c2011-02-01 13:52:17 +00001284
John Stultz4e250fd2012-07-27 14:48:13 -04001285 timekeeping_forward_now(tk);
John Stultzc528f7c2011-02-01 13:52:17 +00001286
John Stultz4e8b1452012-08-08 15:36:20 -04001287 /* Make sure the proposed value is valid */
John Stultz7d489d12014-07-16 21:04:01 +00001288 tmp = timespec64_add(tk_xtime(tk), ts64);
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001289 if (timespec64_compare(&tk->wall_to_monotonic, &ts64) > 0 ||
1290 !timespec64_valid_strict(&tmp)) {
John Stultz4e8b1452012-08-08 15:36:20 -04001291 ret = -EINVAL;
1292 goto error;
1293 }
John Stultz1e75fa82012-07-13 01:21:53 -04001294
John Stultz7d489d12014-07-16 21:04:01 +00001295 tk_xtime_add(tk, &ts64);
1296 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts64));
John Stultzc528f7c2011-02-01 13:52:17 +00001297
John Stultz4e8b1452012-08-08 15:36:20 -04001298error: /* even if we error out, we forwarded the time, so call update */
David Vrabel780427f2013-06-27 11:35:46 +01001299 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultzc528f7c2011-02-01 13:52:17 +00001300
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001301 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001302 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzc528f7c2011-02-01 13:52:17 +00001303
1304 /* signal hrtimers about time change */
1305 clock_was_set();
1306
John Stultz4e8b1452012-08-08 15:36:20 -04001307 return ret;
John Stultzc528f7c2011-02-01 13:52:17 +00001308}
1309EXPORT_SYMBOL(timekeeping_inject_offset);
1310
John Stultzcc244dd2012-05-03 12:30:07 -07001311
1312/**
1313 * timekeeping_get_tai_offset - Returns current TAI offset from UTC
1314 *
1315 */
1316s32 timekeeping_get_tai_offset(void)
1317{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001318 struct timekeeper *tk = &tk_core.timekeeper;
John Stultzcc244dd2012-05-03 12:30:07 -07001319 unsigned int seq;
1320 s32 ret;
1321
1322 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001323 seq = read_seqcount_begin(&tk_core.seq);
John Stultzcc244dd2012-05-03 12:30:07 -07001324 ret = tk->tai_offset;
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001325 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultzcc244dd2012-05-03 12:30:07 -07001326
1327 return ret;
1328}
1329
1330/**
1331 * __timekeeping_set_tai_offset - Lock free worker function
1332 *
1333 */
Fengguang Wudd5d70e2013-03-25 12:24:24 -07001334static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
John Stultzcc244dd2012-05-03 12:30:07 -07001335{
1336 tk->tai_offset = tai_offset;
John Stultz04005f62013-12-10 17:13:35 -08001337 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
John Stultzcc244dd2012-05-03 12:30:07 -07001338}
1339
1340/**
1341 * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
1342 *
1343 */
1344void timekeeping_set_tai_offset(s32 tai_offset)
1345{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001346 struct timekeeper *tk = &tk_core.timekeeper;
John Stultzcc244dd2012-05-03 12:30:07 -07001347 unsigned long flags;
1348
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001349 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001350 write_seqcount_begin(&tk_core.seq);
John Stultzcc244dd2012-05-03 12:30:07 -07001351 __timekeeping_set_tai_offset(tk, tai_offset);
John Stultzf55c0762013-12-11 18:50:25 -08001352 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001353 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001354 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz4e8f8b32013-04-10 12:41:49 -07001355 clock_was_set();
John Stultzcc244dd2012-05-03 12:30:07 -07001356}
1357
john stultz85240702007-05-08 00:27:59 -07001358/**
1359 * change_clocksource - Swaps clocksources if a new one is available
1360 *
1361 * Accumulates current time interval and initializes new clocksource
1362 */
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001363static int change_clocksource(void *data)
john stultz85240702007-05-08 00:27:59 -07001364{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001365 struct timekeeper *tk = &tk_core.timekeeper;
Magnus Damm4614e6a2009-04-21 12:24:02 -07001366 struct clocksource *new, *old;
John Stultzf695cf92012-03-14 16:38:15 -07001367 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -07001368
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001369 new = (struct clocksource *) data;
john stultz85240702007-05-08 00:27:59 -07001370
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001371 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001372 write_seqcount_begin(&tk_core.seq);
John Stultzf695cf92012-03-14 16:38:15 -07001373
John Stultz4e250fd2012-07-27 14:48:13 -04001374 timekeeping_forward_now(tk);
Thomas Gleixner09ac3692013-04-25 20:31:44 +00001375 /*
1376 * If the cs is in module, get a module reference. Succeeds
1377 * for built-in code (owner == NULL) as well.
1378 */
1379 if (try_module_get(new->owner)) {
1380 if (!new->enable || new->enable(new) == 0) {
Peter Zijlstra876e7882015-03-19 10:09:06 +01001381 old = tk->tkr_mono.clock;
Thomas Gleixner09ac3692013-04-25 20:31:44 +00001382 tk_setup_internals(tk, new);
1383 if (old->disable)
1384 old->disable(old);
1385 module_put(old->owner);
1386 } else {
1387 module_put(new->owner);
1388 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001389 }
David Vrabel780427f2013-06-27 11:35:46 +01001390 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultzf695cf92012-03-14 16:38:15 -07001391
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001392 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001393 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzf695cf92012-03-14 16:38:15 -07001394
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001395 return 0;
1396}
john stultz85240702007-05-08 00:27:59 -07001397
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001398/**
1399 * timekeeping_notify - Install a new clock source
1400 * @clock: pointer to the clock source
1401 *
1402 * This function is called from clocksource.c after a new, better clock
1403 * source has been registered. The caller holds the clocksource_mutex.
1404 */
Thomas Gleixnerba919d12013-04-25 20:31:44 +00001405int timekeeping_notify(struct clocksource *clock)
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001406{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001407 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04001408
Peter Zijlstra876e7882015-03-19 10:09:06 +01001409 if (tk->tkr_mono.clock == clock)
Thomas Gleixnerba919d12013-04-25 20:31:44 +00001410 return 0;
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001411 stop_machine(change_clocksource, clock, NULL);
john stultz85240702007-05-08 00:27:59 -07001412 tick_clock_notify();
Peter Zijlstra876e7882015-03-19 10:09:06 +01001413 return tk->tkr_mono.clock == clock ? 0 : -1;
john stultz85240702007-05-08 00:27:59 -07001414}
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001415
Thomas Gleixnera40f2622009-07-07 13:00:31 +02001416/**
John Stultzcdba2ec2014-11-07 11:03:20 -08001417 * getrawmonotonic64 - Returns the raw monotonic time in a timespec
1418 * @ts: pointer to the timespec64 to be set
John Stultz2d422442008-08-20 16:37:30 -07001419 *
1420 * Returns the raw monotonic time (completely un-modified by ntp)
1421 */
John Stultzcdba2ec2014-11-07 11:03:20 -08001422void getrawmonotonic64(struct timespec64 *ts)
John Stultz2d422442008-08-20 16:37:30 -07001423{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001424 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz2d422442008-08-20 16:37:30 -07001425 unsigned long seq;
1426 s64 nsecs;
John Stultz2d422442008-08-20 16:37:30 -07001427
1428 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001429 seq = read_seqcount_begin(&tk_core.seq);
John Stultz5311c742017-05-22 17:20:20 -07001430 ts->tv_sec = tk->raw_sec;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01001431 nsecs = timekeeping_get_ns(&tk->tkr_raw);
John Stultz2d422442008-08-20 16:37:30 -07001432
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001433 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz2d422442008-08-20 16:37:30 -07001434
John Stultz5311c742017-05-22 17:20:20 -07001435 ts->tv_nsec = 0;
1436 timespec64_add_ns(ts, nsecs);
John Stultz2d422442008-08-20 16:37:30 -07001437}
John Stultzcdba2ec2014-11-07 11:03:20 -08001438EXPORT_SYMBOL(getrawmonotonic64);
1439
John Stultz2d422442008-08-20 16:37:30 -07001440
John Stultz2d422442008-08-20 16:37:30 -07001441/**
Li Zefancf4fc6c2008-02-08 04:19:24 -08001442 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -07001443 */
Li Zefancf4fc6c2008-02-08 04:19:24 -08001444int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -07001445{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001446 struct timekeeper *tk = &tk_core.timekeeper;
john stultz85240702007-05-08 00:27:59 -07001447 unsigned long seq;
1448 int ret;
1449
1450 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001451 seq = read_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -07001452
Peter Zijlstra876e7882015-03-19 10:09:06 +01001453 ret = tk->tkr_mono.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -07001454
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001455 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz85240702007-05-08 00:27:59 -07001456
1457 return ret;
1458}
1459
1460/**
Jon Hunter98962462009-08-18 12:45:10 -05001461 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
Jon Hunter98962462009-08-18 12:45:10 -05001462 */
1463u64 timekeeping_max_deferment(void)
1464{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001465 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz70471f22011-11-14 12:48:10 -08001466 unsigned long seq;
1467 u64 ret;
John Stultz42e71e82012-07-13 01:21:51 -04001468
John Stultz70471f22011-11-14 12:48:10 -08001469 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001470 seq = read_seqcount_begin(&tk_core.seq);
John Stultz70471f22011-11-14 12:48:10 -08001471
Peter Zijlstra876e7882015-03-19 10:09:06 +01001472 ret = tk->tkr_mono.clock->max_idle_ns;
John Stultz70471f22011-11-14 12:48:10 -08001473
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001474 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz70471f22011-11-14 12:48:10 -08001475
1476 return ret;
Jon Hunter98962462009-08-18 12:45:10 -05001477}
1478
1479/**
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001480 * read_persistent_clock - Return time from the persistent clock.
john stultz85240702007-05-08 00:27:59 -07001481 *
1482 * Weak dummy function for arches that do not yet support it.
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001483 * Reads the time from the battery backed persistent clock.
1484 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
john stultz85240702007-05-08 00:27:59 -07001485 *
1486 * XXX - Do be sure to remove it once all arches implement it.
1487 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001488void __weak read_persistent_clock(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -07001489{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001490 ts->tv_sec = 0;
1491 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -07001492}
1493
Xunlei Pang2ee96632015-04-01 20:34:22 -07001494void __weak read_persistent_clock64(struct timespec64 *ts64)
1495{
1496 struct timespec ts;
1497
1498 read_persistent_clock(&ts);
1499 *ts64 = timespec_to_timespec64(ts);
1500}
1501
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001502/**
Xunlei Pange83d0a42015-04-09 09:04:42 +08001503 * read_boot_clock64 - Return time of the system start.
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001504 *
1505 * Weak dummy function for arches that do not yet support it.
1506 * Function to read the exact time the system has been started.
Xunlei Pange83d0a42015-04-09 09:04:42 +08001507 * Returns a timespec64 with tv_sec=0 and tv_nsec=0 if unsupported.
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001508 *
1509 * XXX - Do be sure to remove it once all arches implement it.
1510 */
Xunlei Pange83d0a42015-04-09 09:04:42 +08001511void __weak read_boot_clock64(struct timespec64 *ts)
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001512{
1513 ts->tv_sec = 0;
1514 ts->tv_nsec = 0;
1515}
1516
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001517/* Flag for if timekeeping_resume() has injected sleeptime */
1518static bool sleeptime_injected;
1519
1520/* Flag for if there is a persistent clock on this platform */
1521static bool persistent_clock_exists;
1522
john stultz85240702007-05-08 00:27:59 -07001523/*
1524 * timekeeping_init - Initializes the clocksource and common timekeeping values
1525 */
1526void __init timekeeping_init(void)
1527{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001528 struct timekeeper *tk = &tk_core.timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +02001529 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -07001530 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001531 struct timespec64 now, boot, tmp;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001532
Xunlei Pang2ee96632015-04-01 20:34:22 -07001533 read_persistent_clock64(&now);
John Stultz7d489d12014-07-16 21:04:01 +00001534 if (!timespec64_valid_strict(&now)) {
John Stultz4e8b1452012-08-08 15:36:20 -04001535 pr_warn("WARNING: Persistent clock returned invalid value!\n"
1536 " Check your CMOS/BIOS settings.\n");
1537 now.tv_sec = 0;
1538 now.tv_nsec = 0;
Feng Tang31ade302013-01-16 00:09:47 +08001539 } else if (now.tv_sec || now.tv_nsec)
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001540 persistent_clock_exists = true;
John Stultz4e8b1452012-08-08 15:36:20 -04001541
Xunlei Pang9a806dd2015-04-01 20:34:21 -07001542 read_boot_clock64(&boot);
John Stultz7d489d12014-07-16 21:04:01 +00001543 if (!timespec64_valid_strict(&boot)) {
John Stultz4e8b1452012-08-08 15:36:20 -04001544 pr_warn("WARNING: Boot clock returned invalid value!\n"
1545 " Check your CMOS/BIOS settings.\n");
1546 boot.tv_sec = 0;
1547 boot.tv_nsec = 0;
1548 }
john stultz85240702007-05-08 00:27:59 -07001549
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001550 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001551 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07001552 ntp_init();
1553
Martin Schwidefskyf1b82742009-08-14 15:47:21 +02001554 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +02001555 if (clock->enable)
1556 clock->enable(clock);
John Stultz4e250fd2012-07-27 14:48:13 -04001557 tk_setup_internals(tk, clock);
john stultz85240702007-05-08 00:27:59 -07001558
John Stultz4e250fd2012-07-27 14:48:13 -04001559 tk_set_xtime(tk, &now);
John Stultz5311c742017-05-22 17:20:20 -07001560 tk->raw_sec = 0;
John Stultz1e75fa82012-07-13 01:21:53 -04001561 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
John Stultz4e250fd2012-07-27 14:48:13 -04001562 boot = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -04001563
John Stultz7d489d12014-07-16 21:04:01 +00001564 set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec);
John Stultz4e250fd2012-07-27 14:48:13 -04001565 tk_set_wall_to_mono(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -04001566
Thomas Gleixner56fd16c2015-10-16 15:50:22 +02001567 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001568
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001569 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001570 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001571}
1572
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001573/* time in seconds when suspend began for persistent clock */
John Stultz7d489d12014-07-16 21:04:01 +00001574static struct timespec64 timekeeping_suspend_time;
john stultz85240702007-05-08 00:27:59 -07001575
1576/**
John Stultz304529b2011-04-01 14:32:09 -07001577 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
1578 * @delta: pointer to a timespec delta value
1579 *
1580 * Takes a timespec offset measuring a suspend interval and properly
1581 * adds the sleep offset to the timekeeping variables.
1582 */
John Stultzf726a692012-07-13 01:21:57 -04001583static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
John Stultz7d489d12014-07-16 21:04:01 +00001584 struct timespec64 *delta)
John Stultz304529b2011-04-01 14:32:09 -07001585{
John Stultz7d489d12014-07-16 21:04:01 +00001586 if (!timespec64_valid_strict(delta)) {
John Stultz6d9bcb62014-06-04 16:11:43 -07001587 printk_deferred(KERN_WARNING
1588 "__timekeeping_inject_sleeptime: Invalid "
1589 "sleep delta value!\n");
John Stultzcb5de2f8d2011-06-01 18:18:09 -07001590 return;
1591 }
John Stultzf726a692012-07-13 01:21:57 -04001592 tk_xtime_add(tk, delta);
John Stultz7d489d12014-07-16 21:04:01 +00001593 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
Thomas Gleixner47da70d2014-07-16 21:05:00 +00001594 tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
Colin Cross5c835452013-05-21 22:32:14 -07001595 tk_debug_account_sleep_time(delta);
John Stultz304529b2011-04-01 14:32:09 -07001596}
1597
Xunlei Pang7f298132015-04-01 20:34:35 -07001598#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
John Stultz304529b2011-04-01 14:32:09 -07001599/**
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001600 * We have three kinds of time sources to use for sleep time
1601 * injection, the preference order is:
1602 * 1) non-stop clocksource
1603 * 2) persistent clock (ie: RTC accessible when irqs are off)
1604 * 3) RTC
1605 *
1606 * 1) and 2) are used by timekeeping, 3) by RTC subsystem.
1607 * If system has neither 1) nor 2), 3) will be used finally.
1608 *
1609 *
1610 * If timekeeping has injected sleeptime via either 1) or 2),
1611 * 3) becomes needless, so in this case we don't need to call
1612 * rtc_resume(), and this is what timekeeping_rtc_skipresume()
1613 * means.
1614 */
1615bool timekeeping_rtc_skipresume(void)
1616{
1617 return sleeptime_injected;
1618}
1619
1620/**
1621 * 1) can be determined whether to use or not only when doing
1622 * timekeeping_resume() which is invoked after rtc_suspend(),
1623 * so we can't skip rtc_suspend() surely if system has 1).
1624 *
1625 * But if system has 2), 2) will definitely be used, so in this
1626 * case we don't need to call rtc_suspend(), and this is what
1627 * timekeeping_rtc_skipsuspend() means.
1628 */
1629bool timekeeping_rtc_skipsuspend(void)
1630{
1631 return persistent_clock_exists;
1632}
1633
1634/**
pang.xunlei04d90892014-11-18 19:15:17 +08001635 * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values
1636 * @delta: pointer to a timespec64 delta value
John Stultz304529b2011-04-01 14:32:09 -07001637 *
Xunlei Pang2ee96632015-04-01 20:34:22 -07001638 * This hook is for architectures that cannot support read_persistent_clock64
John Stultz304529b2011-04-01 14:32:09 -07001639 * because their RTC/persistent clock is only accessible when irqs are enabled.
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001640 * and also don't have an effective nonstop clocksource.
John Stultz304529b2011-04-01 14:32:09 -07001641 *
1642 * This function should only be called by rtc_resume(), and allows
1643 * a suspend offset to be injected into the timekeeping values.
1644 */
pang.xunlei04d90892014-11-18 19:15:17 +08001645void timekeeping_inject_sleeptime64(struct timespec64 *delta)
John Stultz304529b2011-04-01 14:32:09 -07001646{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001647 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -08001648 unsigned long flags;
John Stultz304529b2011-04-01 14:32:09 -07001649
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001650 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001651 write_seqcount_begin(&tk_core.seq);
John Stultz70471f22011-11-14 12:48:10 -08001652
John Stultz4e250fd2012-07-27 14:48:13 -04001653 timekeeping_forward_now(tk);
John Stultz304529b2011-04-01 14:32:09 -07001654
pang.xunlei04d90892014-11-18 19:15:17 +08001655 __timekeeping_inject_sleeptime(tk, delta);
John Stultz304529b2011-04-01 14:32:09 -07001656
David Vrabel780427f2013-06-27 11:35:46 +01001657 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultz304529b2011-04-01 14:32:09 -07001658
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001659 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001660 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz304529b2011-04-01 14:32:09 -07001661
1662 /* signal hrtimers about time change */
1663 clock_was_set();
1664}
Xunlei Pang7f298132015-04-01 20:34:35 -07001665#endif
John Stultz304529b2011-04-01 14:32:09 -07001666
John Stultz304529b2011-04-01 14:32:09 -07001667/**
john stultz85240702007-05-08 00:27:59 -07001668 * timekeeping_resume - Resumes the generic timekeeping subsystem.
john stultz85240702007-05-08 00:27:59 -07001669 */
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +01001670void timekeeping_resume(void)
john stultz85240702007-05-08 00:27:59 -07001671{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001672 struct timekeeper *tk = &tk_core.timekeeper;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001673 struct clocksource *clock = tk->tkr_mono.clock;
John Stultz92c1d3e2011-11-14 14:05:44 -08001674 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001675 struct timespec64 ts_new, ts_delta;
Feng Tange445cf12013-03-12 11:56:48 +08001676 cycle_t cycle_now, cycle_delta;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001677
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001678 sleeptime_injected = false;
Xunlei Pang2ee96632015-04-01 20:34:22 -07001679 read_persistent_clock64(&ts_new);
john stultz85240702007-05-08 00:27:59 -07001680
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +02001681 clockevents_resume();
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +02001682 clocksource_resume();
1683
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001684 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001685 write_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -07001686
Feng Tange445cf12013-03-12 11:56:48 +08001687 /*
1688 * After system resumes, we need to calculate the suspended time and
1689 * compensate it for the OS time. There are 3 sources that could be
1690 * used: Nonstop clocksource during suspend, persistent clock and rtc
1691 * device.
1692 *
1693 * One specific platform may have 1 or 2 or all of them, and the
1694 * preference will be:
1695 * suspend-nonstop clocksource -> persistent clock -> rtc
1696 * The less preferred source will only be tried if there is no better
1697 * usable source. The rtc part is handled separately in rtc core code.
1698 */
John Stultz02a37cc2017-06-08 16:44:20 -07001699 cycle_now = tk_clock_read(&tk->tkr_mono);
Feng Tange445cf12013-03-12 11:56:48 +08001700 if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
Peter Zijlstra876e7882015-03-19 10:09:06 +01001701 cycle_now > tk->tkr_mono.cycle_last) {
Feng Tange445cf12013-03-12 11:56:48 +08001702 u64 num, max = ULLONG_MAX;
1703 u32 mult = clock->mult;
1704 u32 shift = clock->shift;
1705 s64 nsec = 0;
1706
Peter Zijlstra876e7882015-03-19 10:09:06 +01001707 cycle_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last,
1708 tk->tkr_mono.mask);
Feng Tange445cf12013-03-12 11:56:48 +08001709
1710 /*
1711 * "cycle_delta * mutl" may cause 64 bits overflow, if the
1712 * suspended time is too long. In that case we need do the
1713 * 64 bits math carefully
1714 */
1715 do_div(max, mult);
1716 if (cycle_delta > max) {
1717 num = div64_u64(cycle_delta, max);
1718 nsec = (((u64) max * mult) >> shift) * num;
1719 cycle_delta -= num * max;
1720 }
1721 nsec += ((u64) cycle_delta * mult) >> shift;
1722
John Stultz7d489d12014-07-16 21:04:01 +00001723 ts_delta = ns_to_timespec64(nsec);
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001724 sleeptime_injected = true;
John Stultz7d489d12014-07-16 21:04:01 +00001725 } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
1726 ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001727 sleeptime_injected = true;
john stultz85240702007-05-08 00:27:59 -07001728 }
Feng Tange445cf12013-03-12 11:56:48 +08001729
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001730 if (sleeptime_injected)
Feng Tange445cf12013-03-12 11:56:48 +08001731 __timekeeping_inject_sleeptime(tk, &ts_delta);
1732
1733 /* Re-base the last cycle value */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001734 tk->tkr_mono.cycle_last = cycle_now;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01001735 tk->tkr_raw.cycle_last = cycle_now;
1736
John Stultz4e250fd2012-07-27 14:48:13 -04001737 tk->ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -07001738 timekeeping_suspended = 0;
David Vrabel780427f2013-06-27 11:35:46 +01001739 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001740 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001741 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001742
1743 touch_softlockup_watchdog();
1744
Thomas Gleixner4ffee522015-03-25 13:09:16 +01001745 tick_resume();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +02001746 hrtimers_resume();
john stultz85240702007-05-08 00:27:59 -07001747}
1748
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +01001749int timekeeping_suspend(void)
john stultz85240702007-05-08 00:27:59 -07001750{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001751 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -08001752 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001753 struct timespec64 delta, delta_delta;
1754 static struct timespec64 old_delta;
john stultz85240702007-05-08 00:27:59 -07001755
Xunlei Pang2ee96632015-04-01 20:34:22 -07001756 read_persistent_clock64(&timekeeping_suspend_time);
Thomas Gleixner3be90952007-09-16 15:36:43 +02001757
Zoran Markovic0d6bd992013-05-17 11:24:05 -07001758 /*
1759 * On some systems the persistent_clock can not be detected at
1760 * timekeeping_init by its return value, so if we see a valid
1761 * value returned, update the persistent_clock_exists flag.
1762 */
1763 if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001764 persistent_clock_exists = true;
Zoran Markovic0d6bd992013-05-17 11:24:05 -07001765
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001766 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001767 write_seqcount_begin(&tk_core.seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001768 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -07001769 timekeeping_suspended = 1;
John Stultzcb332172011-05-31 22:53:23 -07001770
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001771 if (persistent_clock_exists) {
John Stultzcb332172011-05-31 22:53:23 -07001772 /*
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001773 * To avoid drift caused by repeated suspend/resumes,
1774 * which each can add ~1 second drift error,
1775 * try to compensate so the difference in system time
1776 * and persistent_clock time stays close to constant.
John Stultzcb332172011-05-31 22:53:23 -07001777 */
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001778 delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time);
1779 delta_delta = timespec64_sub(delta, old_delta);
1780 if (abs(delta_delta.tv_sec) >= 2) {
1781 /*
1782 * if delta_delta is too large, assume time correction
1783 * has occurred and set old_delta to the current delta.
1784 */
1785 old_delta = delta;
1786 } else {
1787 /* Otherwise try to adjust old_system to compensate */
1788 timekeeping_suspend_time =
1789 timespec64_add(timekeeping_suspend_time, delta_delta);
1790 }
John Stultzcb332172011-05-31 22:53:23 -07001791 }
John Stultz330a1612013-12-11 19:10:36 -08001792
1793 timekeeping_update(tk, TK_MIRROR);
Rafael J. Wysocki060407a2015-02-13 14:49:02 +01001794 halt_fast_timekeeper(tk);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001795 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001796 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001797
Thomas Gleixner4ffee522015-03-25 13:09:16 +01001798 tick_suspend();
Magnus Dammc54a42b2010-02-02 14:41:41 -08001799 clocksource_suspend();
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +02001800 clockevents_suspend();
john stultz85240702007-05-08 00:27:59 -07001801
1802 return 0;
1803}
1804
1805/* sysfs resume/suspend bits for timekeeping */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001806static struct syscore_ops timekeeping_syscore_ops = {
john stultz85240702007-05-08 00:27:59 -07001807 .resume = timekeeping_resume,
1808 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -07001809};
1810
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001811static int __init timekeeping_init_ops(void)
john stultz85240702007-05-08 00:27:59 -07001812{
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001813 register_syscore_ops(&timekeeping_syscore_ops);
1814 return 0;
john stultz85240702007-05-08 00:27:59 -07001815}
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001816device_initcall(timekeeping_init_ops);
john stultz85240702007-05-08 00:27:59 -07001817
1818/*
John Stultzdc491592013-12-06 17:25:21 -08001819 * Apply a multiplier adjustment to the timekeeper
john stultz85240702007-05-08 00:27:59 -07001820 */
John Stultzdc491592013-12-06 17:25:21 -08001821static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
1822 s64 offset,
1823 bool negative,
1824 int adj_scale)
john stultz85240702007-05-08 00:27:59 -07001825{
John Stultzdc491592013-12-06 17:25:21 -08001826 s64 interval = tk->cycle_interval;
1827 s32 mult_adj = 1;
john stultz85240702007-05-08 00:27:59 -07001828
John Stultzdc491592013-12-06 17:25:21 -08001829 if (negative) {
1830 mult_adj = -mult_adj;
1831 interval = -interval;
1832 offset = -offset;
john stultz85240702007-05-08 00:27:59 -07001833 }
John Stultzdc491592013-12-06 17:25:21 -08001834 mult_adj <<= adj_scale;
1835 interval <<= adj_scale;
1836 offset <<= adj_scale;
john stultz85240702007-05-08 00:27:59 -07001837
John Stultzc2bc1112011-10-27 18:12:42 -07001838 /*
1839 * So the following can be confusing.
1840 *
John Stultzdc491592013-12-06 17:25:21 -08001841 * To keep things simple, lets assume mult_adj == 1 for now.
John Stultzc2bc1112011-10-27 18:12:42 -07001842 *
John Stultzdc491592013-12-06 17:25:21 -08001843 * When mult_adj != 1, remember that the interval and offset values
John Stultzc2bc1112011-10-27 18:12:42 -07001844 * have been appropriately scaled so the math is the same.
1845 *
1846 * The basic idea here is that we're increasing the multiplier
1847 * by one, this causes the xtime_interval to be incremented by
1848 * one cycle_interval. This is because:
1849 * xtime_interval = cycle_interval * mult
1850 * So if mult is being incremented by one:
1851 * xtime_interval = cycle_interval * (mult + 1)
1852 * Its the same as:
1853 * xtime_interval = (cycle_interval * mult) + cycle_interval
1854 * Which can be shortened to:
1855 * xtime_interval += cycle_interval
1856 *
1857 * So offset stores the non-accumulated cycles. Thus the current
1858 * time (in shifted nanoseconds) is:
1859 * now = (offset * adj) + xtime_nsec
1860 * Now, even though we're adjusting the clock frequency, we have
1861 * to keep time consistent. In other words, we can't jump back
1862 * in time, and we also want to avoid jumping forward in time.
1863 *
1864 * So given the same offset value, we need the time to be the same
1865 * both before and after the freq adjustment.
1866 * now = (offset * adj_1) + xtime_nsec_1
1867 * now = (offset * adj_2) + xtime_nsec_2
1868 * So:
1869 * (offset * adj_1) + xtime_nsec_1 =
1870 * (offset * adj_2) + xtime_nsec_2
1871 * And we know:
1872 * adj_2 = adj_1 + 1
1873 * So:
1874 * (offset * adj_1) + xtime_nsec_1 =
1875 * (offset * (adj_1+1)) + xtime_nsec_2
1876 * (offset * adj_1) + xtime_nsec_1 =
1877 * (offset * adj_1) + offset + xtime_nsec_2
1878 * Canceling the sides:
1879 * xtime_nsec_1 = offset + xtime_nsec_2
1880 * Which gives us:
1881 * xtime_nsec_2 = xtime_nsec_1 - offset
1882 * Which simplfies to:
1883 * xtime_nsec -= offset
1884 *
1885 * XXX - TODO: Doc ntp_error calculation.
1886 */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001887 if ((mult_adj > 0) && (tk->tkr_mono.mult + mult_adj < mult_adj)) {
pang.xunlei6067dc52014-10-08 15:03:34 +08001888 /* NTP adjustment caused clocksource mult overflow */
1889 WARN_ON_ONCE(1);
1890 return;
1891 }
1892
Peter Zijlstra876e7882015-03-19 10:09:06 +01001893 tk->tkr_mono.mult += mult_adj;
John Stultzf726a692012-07-13 01:21:57 -04001894 tk->xtime_interval += interval;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001895 tk->tkr_mono.xtime_nsec -= offset;
John Stultzf726a692012-07-13 01:21:57 -04001896 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
John Stultzdc491592013-12-06 17:25:21 -08001897}
John Stultz2a8c0882012-07-13 01:21:56 -04001898
John Stultzdc491592013-12-06 17:25:21 -08001899/*
1900 * Calculate the multiplier adjustment needed to match the frequency
1901 * specified by NTP
1902 */
1903static __always_inline void timekeeping_freqadjust(struct timekeeper *tk,
1904 s64 offset)
1905{
1906 s64 interval = tk->cycle_interval;
1907 s64 xinterval = tk->xtime_interval;
John Stultzec02b072015-12-03 10:23:30 -08001908 u32 base = tk->tkr_mono.clock->mult;
1909 u32 max = tk->tkr_mono.clock->maxadj;
1910 u32 cur_adj = tk->tkr_mono.mult;
John Stultzdc491592013-12-06 17:25:21 -08001911 s64 tick_error;
1912 bool negative;
John Stultzec02b072015-12-03 10:23:30 -08001913 u32 adj_scale;
John Stultzdc491592013-12-06 17:25:21 -08001914
1915 /* Remove any current error adj from freq calculation */
1916 if (tk->ntp_err_mult)
1917 xinterval -= tk->cycle_interval;
1918
John Stultz375f45b2014-04-23 20:53:29 -07001919 tk->ntp_tick = ntp_tick_length();
1920
John Stultzdc491592013-12-06 17:25:21 -08001921 /* Calculate current error per tick */
1922 tick_error = ntp_tick_length() >> tk->ntp_error_shift;
1923 tick_error -= (xinterval + tk->xtime_remainder);
1924
1925 /* Don't worry about correcting it if its small */
1926 if (likely((tick_error >= 0) && (tick_error <= interval)))
1927 return;
1928
1929 /* preserve the direction of correction */
1930 negative = (tick_error < 0);
1931
John Stultzec02b072015-12-03 10:23:30 -08001932 /* If any adjustment would pass the max, just return */
1933 if (negative && (cur_adj - 1) <= (base - max))
1934 return;
1935 if (!negative && (cur_adj + 1) >= (base + max))
1936 return;
1937 /*
1938 * Sort out the magnitude of the correction, but
1939 * avoid making so large a correction that we go
1940 * over the max adjustment.
1941 */
1942 adj_scale = 0;
Andrew Morton79211c82015-11-09 14:58:13 -08001943 tick_error = abs(tick_error);
John Stultzec02b072015-12-03 10:23:30 -08001944 while (tick_error > interval) {
1945 u32 adj = 1 << (adj_scale + 1);
1946
1947 /* Check if adjustment gets us within 1 unit from the max */
1948 if (negative && (cur_adj - adj) <= (base - max))
1949 break;
1950 if (!negative && (cur_adj + adj) >= (base + max))
1951 break;
1952
1953 adj_scale++;
John Stultzdc491592013-12-06 17:25:21 -08001954 tick_error >>= 1;
John Stultzec02b072015-12-03 10:23:30 -08001955 }
John Stultzdc491592013-12-06 17:25:21 -08001956
1957 /* scale the corrections */
John Stultzec02b072015-12-03 10:23:30 -08001958 timekeeping_apply_adjustment(tk, offset, negative, adj_scale);
John Stultzdc491592013-12-06 17:25:21 -08001959}
1960
1961/*
1962 * Adjust the timekeeper's multiplier to the correct frequency
1963 * and also to reduce the accumulated error value.
1964 */
1965static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
1966{
1967 /* Correct for the current frequency error */
1968 timekeeping_freqadjust(tk, offset);
1969
1970 /* Next make a small adjustment to fix any cumulative error */
1971 if (!tk->ntp_err_mult && (tk->ntp_error > 0)) {
1972 tk->ntp_err_mult = 1;
1973 timekeeping_apply_adjustment(tk, offset, 0, 0);
1974 } else if (tk->ntp_err_mult && (tk->ntp_error <= 0)) {
1975 /* Undo any existing error adjustment */
1976 timekeeping_apply_adjustment(tk, offset, 1, 0);
1977 tk->ntp_err_mult = 0;
1978 }
1979
Peter Zijlstra876e7882015-03-19 10:09:06 +01001980 if (unlikely(tk->tkr_mono.clock->maxadj &&
1981 (abs(tk->tkr_mono.mult - tk->tkr_mono.clock->mult)
1982 > tk->tkr_mono.clock->maxadj))) {
John Stultzdc491592013-12-06 17:25:21 -08001983 printk_once(KERN_WARNING
1984 "Adjusting %s more than 11%% (%ld vs %ld)\n",
Peter Zijlstra876e7882015-03-19 10:09:06 +01001985 tk->tkr_mono.clock->name, (long)tk->tkr_mono.mult,
1986 (long)tk->tkr_mono.clock->mult + tk->tkr_mono.clock->maxadj);
John Stultzdc491592013-12-06 17:25:21 -08001987 }
1988
John Stultz2a8c0882012-07-13 01:21:56 -04001989 /*
1990 * It may be possible that when we entered this function, xtime_nsec
1991 * was very small. Further, if we're slightly speeding the clocksource
1992 * in the code above, its possible the required corrective factor to
1993 * xtime_nsec could cause it to underflow.
1994 *
1995 * Now, since we already accumulated the second, cannot simply roll
1996 * the accumulated second back, since the NTP subsystem has been
1997 * notified via second_overflow. So instead we push xtime_nsec forward
1998 * by the amount we underflowed, and add that amount into the error.
1999 *
2000 * We'll correct this error next time through this function, when
2001 * xtime_nsec is not as small.
2002 */
Peter Zijlstra876e7882015-03-19 10:09:06 +01002003 if (unlikely((s64)tk->tkr_mono.xtime_nsec < 0)) {
2004 s64 neg = -(s64)tk->tkr_mono.xtime_nsec;
2005 tk->tkr_mono.xtime_nsec = 0;
John Stultzf726a692012-07-13 01:21:57 -04002006 tk->ntp_error += neg << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -04002007 }
john stultz85240702007-05-08 00:27:59 -07002008}
2009
2010/**
John Stultz1f4f9482012-07-13 01:21:54 -04002011 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
2012 *
Zhen Lei571af552015-08-25 14:42:53 +08002013 * Helper function that accumulates the nsecs greater than a second
John Stultz1f4f9482012-07-13 01:21:54 -04002014 * from the xtime_nsec field to the xtime_secs field.
2015 * It also calls into the NTP code to handle leapsecond processing.
2016 *
2017 */
David Vrabel780427f2013-06-27 11:35:46 +01002018static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
John Stultz1f4f9482012-07-13 01:21:54 -04002019{
Peter Zijlstra876e7882015-03-19 10:09:06 +01002020 u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
John Stultz5258d3f2013-12-11 20:07:49 -08002021 unsigned int clock_set = 0;
John Stultz1f4f9482012-07-13 01:21:54 -04002022
Peter Zijlstra876e7882015-03-19 10:09:06 +01002023 while (tk->tkr_mono.xtime_nsec >= nsecps) {
John Stultz1f4f9482012-07-13 01:21:54 -04002024 int leap;
2025
Peter Zijlstra876e7882015-03-19 10:09:06 +01002026 tk->tkr_mono.xtime_nsec -= nsecps;
John Stultz1f4f9482012-07-13 01:21:54 -04002027 tk->xtime_sec++;
2028
2029 /* Figure out if its a leap sec and apply if needed */
2030 leap = second_overflow(tk->xtime_sec);
John Stultz6d0ef902012-07-27 14:48:12 -04002031 if (unlikely(leap)) {
John Stultz7d489d12014-07-16 21:04:01 +00002032 struct timespec64 ts;
John Stultz1f4f9482012-07-13 01:21:54 -04002033
John Stultz6d0ef902012-07-27 14:48:12 -04002034 tk->xtime_sec += leap;
2035
2036 ts.tv_sec = leap;
2037 ts.tv_nsec = 0;
2038 tk_set_wall_to_mono(tk,
John Stultz7d489d12014-07-16 21:04:01 +00002039 timespec64_sub(tk->wall_to_monotonic, ts));
John Stultz6d0ef902012-07-27 14:48:12 -04002040
John Stultzcc244dd2012-05-03 12:30:07 -07002041 __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
2042
John Stultz5258d3f2013-12-11 20:07:49 -08002043 clock_set = TK_CLOCK_WAS_SET;
John Stultz6d0ef902012-07-27 14:48:12 -04002044 }
John Stultz1f4f9482012-07-13 01:21:54 -04002045 }
John Stultz5258d3f2013-12-11 20:07:49 -08002046 return clock_set;
John Stultz1f4f9482012-07-13 01:21:54 -04002047}
2048
John Stultz1f4f9482012-07-13 01:21:54 -04002049/**
john stultza092ff02009-10-02 16:17:53 -07002050 * logarithmic_accumulation - shifted accumulation of cycles
2051 *
2052 * This functions accumulates a shifted interval of cycles into
2053 * into a shifted interval nanoseconds. Allows for O(log) accumulation
2054 * loop.
2055 *
2056 * Returns the unconsumed cycles.
2057 */
John Stultzf726a692012-07-13 01:21:57 -04002058static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
John Stultz5258d3f2013-12-11 20:07:49 -08002059 u32 shift,
2060 unsigned int *clock_set)
john stultza092ff02009-10-02 16:17:53 -07002061{
Thomas Gleixner23a95372013-02-21 22:51:36 +00002062 cycle_t interval = tk->cycle_interval << shift;
John Stultza53bfdd2017-06-08 16:44:21 -07002063 u64 snsec_per_sec;
john stultza092ff02009-10-02 16:17:53 -07002064
Zhen Lei571af552015-08-25 14:42:53 +08002065 /* If the offset is smaller than a shifted interval, do nothing */
Thomas Gleixner23a95372013-02-21 22:51:36 +00002066 if (offset < interval)
john stultza092ff02009-10-02 16:17:53 -07002067 return offset;
2068
2069 /* Accumulate one shifted interval */
Thomas Gleixner23a95372013-02-21 22:51:36 +00002070 offset -= interval;
Peter Zijlstra876e7882015-03-19 10:09:06 +01002071 tk->tkr_mono.cycle_last += interval;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01002072 tk->tkr_raw.cycle_last += interval;
john stultza092ff02009-10-02 16:17:53 -07002073
Peter Zijlstra876e7882015-03-19 10:09:06 +01002074 tk->tkr_mono.xtime_nsec += tk->xtime_interval << shift;
John Stultz5258d3f2013-12-11 20:07:49 -08002075 *clock_set |= accumulate_nsecs_to_secs(tk);
john stultza092ff02009-10-02 16:17:53 -07002076
Jason Wesseldeda2e82010-08-09 14:20:09 -07002077 /* Accumulate raw time */
John Stultza53bfdd2017-06-08 16:44:21 -07002078 tk->tkr_raw.xtime_nsec += tk->raw_interval << shift;
2079 snsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
2080 while (tk->tkr_raw.xtime_nsec >= snsec_per_sec) {
2081 tk->tkr_raw.xtime_nsec -= snsec_per_sec;
John Stultz5311c742017-05-22 17:20:20 -07002082 tk->raw_sec++;
john stultza092ff02009-10-02 16:17:53 -07002083 }
2084
2085 /* Accumulate error between NTP and clock interval */
John Stultz375f45b2014-04-23 20:53:29 -07002086 tk->ntp_error += tk->ntp_tick << shift;
John Stultzf726a692012-07-13 01:21:57 -04002087 tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
2088 (tk->ntp_error_shift + shift);
john stultza092ff02009-10-02 16:17:53 -07002089
2090 return offset;
2091}
2092
john stultz85240702007-05-08 00:27:59 -07002093/**
2094 * update_wall_time - Uses the current clocksource to increment the wall time
2095 *
john stultz85240702007-05-08 00:27:59 -07002096 */
John Stultz47a1b7962013-12-12 13:10:55 -08002097void update_wall_time(void)
john stultz85240702007-05-08 00:27:59 -07002098{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002099 struct timekeeper *real_tk = &tk_core.timekeeper;
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002100 struct timekeeper *tk = &shadow_timekeeper;
john stultz85240702007-05-08 00:27:59 -07002101 cycle_t offset;
john stultza092ff02009-10-02 16:17:53 -07002102 int shift = 0, maxshift;
John Stultz5258d3f2013-12-11 20:07:49 -08002103 unsigned int clock_set = 0;
John Stultz70471f22011-11-14 12:48:10 -08002104 unsigned long flags;
2105
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00002106 raw_spin_lock_irqsave(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07002107
2108 /* Make sure we're fully resumed: */
2109 if (unlikely(timekeeping_suspended))
John Stultz70471f22011-11-14 12:48:10 -08002110 goto out;
john stultz85240702007-05-08 00:27:59 -07002111
John Stultz592913e2010-07-13 17:56:20 -07002112#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002113 offset = real_tk->cycle_interval;
John Stultz592913e2010-07-13 17:56:20 -07002114#else
John Stultz02a37cc2017-06-08 16:44:20 -07002115 offset = clocksource_delta(tk_clock_read(&tk->tkr_mono),
Peter Zijlstra876e7882015-03-19 10:09:06 +01002116 tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
john stultz85240702007-05-08 00:27:59 -07002117#endif
john stultz85240702007-05-08 00:27:59 -07002118
John Stultzbf2ac312012-08-21 20:30:49 -04002119 /* Check if there's really nothing to do */
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002120 if (offset < real_tk->cycle_interval)
John Stultzbf2ac312012-08-21 20:30:49 -04002121 goto out;
2122
John Stultz3c17ad12015-03-11 21:16:32 -07002123 /* Do some additional sanity checking */
2124 timekeeping_check_update(real_tk, offset);
2125
john stultza092ff02009-10-02 16:17:53 -07002126 /*
2127 * With NO_HZ we may have to accumulate many cycle_intervals
2128 * (think "ticks") worth of time at once. To do this efficiently,
2129 * we calculate the largest doubling multiple of cycle_intervals
Jim Cromie88b28ad2012-03-14 21:28:56 -06002130 * that is smaller than the offset. We then accumulate that
john stultza092ff02009-10-02 16:17:53 -07002131 * chunk in one go, and then try to consume the next smaller
2132 * doubled multiple.
john stultz85240702007-05-08 00:27:59 -07002133 */
John Stultz4e250fd2012-07-27 14:48:13 -04002134 shift = ilog2(offset) - ilog2(tk->cycle_interval);
john stultza092ff02009-10-02 16:17:53 -07002135 shift = max(0, shift);
Jim Cromie88b28ad2012-03-14 21:28:56 -06002136 /* Bound shift to one less than what overflows tick_length */
John Stultzea7cf492011-11-14 13:18:07 -08002137 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
john stultza092ff02009-10-02 16:17:53 -07002138 shift = min(shift, maxshift);
John Stultz4e250fd2012-07-27 14:48:13 -04002139 while (offset >= tk->cycle_interval) {
John Stultz5258d3f2013-12-11 20:07:49 -08002140 offset = logarithmic_accumulation(tk, offset, shift,
2141 &clock_set);
John Stultz4e250fd2012-07-27 14:48:13 -04002142 if (offset < tk->cycle_interval<<shift)
John Stultz830ec042010-03-18 14:47:30 -07002143 shift--;
john stultz85240702007-05-08 00:27:59 -07002144 }
2145
2146 /* correct the clock when NTP error is too big */
John Stultz4e250fd2012-07-27 14:48:13 -04002147 timekeeping_adjust(tk, offset);
john stultz85240702007-05-08 00:27:59 -07002148
John Stultz6a867a32010-04-06 14:30:51 -07002149 /*
John Stultz92bb1fc2012-09-04 15:38:12 -04002150 * XXX This can be killed once everyone converts
2151 * to the new update_vsyscall.
2152 */
2153 old_vsyscall_fixup(tk);
john stultz85240702007-05-08 00:27:59 -07002154
John Stultz6a867a32010-04-06 14:30:51 -07002155 /*
2156 * Finally, make sure that after the rounding
John Stultz1e75fa82012-07-13 01:21:53 -04002157 * xtime_nsec isn't larger than NSEC_PER_SEC
John Stultz6a867a32010-04-06 14:30:51 -07002158 */
John Stultz5258d3f2013-12-11 20:07:49 -08002159 clock_set |= accumulate_nsecs_to_secs(tk);
Linus Torvalds83f57a12009-12-22 14:10:37 -08002160
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002161 write_seqcount_begin(&tk_core.seq);
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002162 /*
2163 * Update the real timekeeper.
2164 *
2165 * We could avoid this memcpy by switching pointers, but that
2166 * requires changes to all other timekeeper usage sites as
2167 * well, i.e. move the timekeeper pointer getter into the
2168 * spinlocked/seqcount protected sections. And we trade this
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002169 * memcpy under the tk_core.seq against one before we start
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002170 * updating.
2171 */
John Stultz906c5552015-06-17 10:05:53 -07002172 timekeeping_update(tk, clock_set);
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002173 memcpy(real_tk, tk, sizeof(*tk));
John Stultz906c5552015-06-17 10:05:53 -07002174 /* The memcpy must come last. Do not put anything here! */
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002175 write_seqcount_end(&tk_core.seq);
Thomas Gleixnerca4523c2013-02-21 22:51:40 +00002176out:
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00002177 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz47a1b7962013-12-12 13:10:55 -08002178 if (clock_set)
John Stultzcab5e122014-03-27 16:30:49 -07002179 /* Have to call _delayed version, since in irq context*/
2180 clock_was_set_delayed();
john stultz85240702007-05-08 00:27:59 -07002181}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002182
2183/**
John Stultzd08c0cd2014-12-08 12:00:09 -08002184 * getboottime64 - Return the real time of system boot.
2185 * @ts: pointer to the timespec64 to be set
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002186 *
John Stultzd08c0cd2014-12-08 12:00:09 -08002187 * Returns the wall-time of boot in a timespec64.
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002188 *
2189 * This is based on the wall_to_monotonic offset and the total suspend
2190 * time. Calls to settimeofday will affect the value returned (which
2191 * basically means that however wrong your real time clock is at boot time,
2192 * you get the right time here).
2193 */
John Stultzd08c0cd2014-12-08 12:00:09 -08002194void getboottime64(struct timespec64 *ts)
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002195{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002196 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixner02cba152014-07-16 21:04:58 +00002197 ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot);
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02002198
John Stultzd08c0cd2014-12-08 12:00:09 -08002199 *ts = ktime_to_timespec64(t);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002200}
John Stultzd08c0cd2014-12-08 12:00:09 -08002201EXPORT_SYMBOL_GPL(getboottime64);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002202
john stultz17c38b72007-07-24 18:38:34 -07002203unsigned long get_seconds(void)
2204{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002205 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04002206
2207 return tk->xtime_sec;
john stultz17c38b72007-07-24 18:38:34 -07002208}
2209EXPORT_SYMBOL(get_seconds);
2210
john stultzda15cfd2009-08-19 19:13:34 -07002211struct timespec __current_kernel_time(void)
2212{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002213 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04002214
John Stultz7d489d12014-07-16 21:04:01 +00002215 return timespec64_to_timespec(tk_xtime(tk));
john stultzda15cfd2009-08-19 19:13:34 -07002216}
john stultz17c38b72007-07-24 18:38:34 -07002217
Baolin Wang8758a242015-07-29 20:09:43 +08002218struct timespec64 current_kernel_time64(void)
john stultz2c6b47d2007-07-24 17:47:43 -07002219{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002220 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz7d489d12014-07-16 21:04:01 +00002221 struct timespec64 now;
john stultz2c6b47d2007-07-24 17:47:43 -07002222 unsigned long seq;
2223
2224 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002225 seq = read_seqcount_begin(&tk_core.seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08002226
John Stultz4e250fd2012-07-27 14:48:13 -04002227 now = tk_xtime(tk);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002228 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz2c6b47d2007-07-24 17:47:43 -07002229
Baolin Wang8758a242015-07-29 20:09:43 +08002230 return now;
john stultz2c6b47d2007-07-24 17:47:43 -07002231}
Baolin Wang8758a242015-07-29 20:09:43 +08002232EXPORT_SYMBOL(current_kernel_time64);
john stultzda15cfd2009-08-19 19:13:34 -07002233
John Stultz334334b2014-11-07 11:20:40 -08002234struct timespec64 get_monotonic_coarse64(void)
john stultzda15cfd2009-08-19 19:13:34 -07002235{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002236 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz7d489d12014-07-16 21:04:01 +00002237 struct timespec64 now, mono;
john stultzda15cfd2009-08-19 19:13:34 -07002238 unsigned long seq;
2239
2240 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002241 seq = read_seqcount_begin(&tk_core.seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08002242
John Stultz4e250fd2012-07-27 14:48:13 -04002243 now = tk_xtime(tk);
2244 mono = tk->wall_to_monotonic;
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002245 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultzda15cfd2009-08-19 19:13:34 -07002246
John Stultz7d489d12014-07-16 21:04:01 +00002247 set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec,
john stultzda15cfd2009-08-19 19:13:34 -07002248 now.tv_nsec + mono.tv_nsec);
John Stultz7d489d12014-07-16 21:04:01 +00002249
John Stultz334334b2014-11-07 11:20:40 -08002250 return now;
john stultzda15cfd2009-08-19 19:13:34 -07002251}
Gregor Boirieeaaa7ec2016-03-09 19:05:48 +01002252EXPORT_SYMBOL(get_monotonic_coarse64);
Torben Hohn871cf1e2011-01-27 15:58:55 +01002253
2254/*
John Stultzd6ad4182012-02-28 16:50:11 -08002255 * Must hold jiffies_lock
Torben Hohn871cf1e2011-01-27 15:58:55 +01002256 */
2257void do_timer(unsigned long ticks)
2258{
2259 jiffies_64 += ticks;
Torben Hohn871cf1e2011-01-27 15:58:55 +01002260 calc_global_load(ticks);
2261}
Torben Hohn48cf76f72011-01-27 15:59:05 +01002262
2263/**
John Stultz76f41082014-07-16 21:03:52 +00002264 * ktime_get_update_offsets_now - hrtimer helper
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002265 * @cwsseq: pointer to check and store the clock was set sequence number
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002266 * @offs_real: pointer to storage for monotonic -> realtime offset
2267 * @offs_boot: pointer to storage for monotonic -> boottime offset
Xie XiuQib7bc50e2013-10-18 09:13:30 +08002268 * @offs_tai: pointer to storage for monotonic -> clock tai offset
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002269 *
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002270 * Returns current monotonic time and updates the offsets if the
2271 * sequence number in @cwsseq and timekeeper.clock_was_set_seq are
2272 * different.
2273 *
Xie XiuQib7bc50e2013-10-18 09:13:30 +08002274 * Called from hrtimer_interrupt() or retrigger_next_event()
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002275 */
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002276ktime_t ktime_get_update_offsets_now(unsigned int *cwsseq, ktime_t *offs_real,
2277 ktime_t *offs_boot, ktime_t *offs_tai)
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002278{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002279 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002280 unsigned int seq;
Thomas Gleixnera37c0aa2014-07-16 21:04:19 +00002281 ktime_t base;
2282 u64 nsecs;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002283
2284 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002285 seq = read_seqcount_begin(&tk_core.seq);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002286
Peter Zijlstra876e7882015-03-19 10:09:06 +01002287 base = tk->tkr_mono.base;
2288 nsecs = timekeeping_get_ns(&tk->tkr_mono);
John Stultz833f32d2015-06-11 15:54:55 -07002289 base = ktime_add_ns(base, nsecs);
2290
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002291 if (*cwsseq != tk->clock_was_set_seq) {
2292 *cwsseq = tk->clock_was_set_seq;
2293 *offs_real = tk->offs_real;
2294 *offs_boot = tk->offs_boot;
2295 *offs_tai = tk->offs_tai;
2296 }
John Stultz833f32d2015-06-11 15:54:55 -07002297
2298 /* Handle leapsecond insertion adjustments */
2299 if (unlikely(base.tv64 >= tk->next_leap_ktime.tv64))
2300 *offs_real = ktime_sub(tk->offs_real, ktime_set(1, 0));
2301
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002302 } while (read_seqcount_retry(&tk_core.seq, seq));
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002303
John Stultz833f32d2015-06-11 15:54:55 -07002304 return base;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002305}
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002306
Torben Hohnf0af911a92011-01-27 15:59:10 +01002307/**
John Stultzaa6f9c592013-03-22 11:31:29 -07002308 * do_adjtimex() - Accessor function to NTP __do_adjtimex function
2309 */
2310int do_adjtimex(struct timex *txc)
2311{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002312 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz06c017f2013-03-22 11:37:28 -07002313 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00002314 struct timespec64 ts;
John Stultz4e8f8b32013-04-10 12:41:49 -07002315 s32 orig_tai, tai;
John Stultze4085692013-03-22 12:08:52 -07002316 int ret;
2317
2318 /* Validate the data before disabling interrupts */
2319 ret = ntp_validate_timex(txc);
2320 if (ret)
2321 return ret;
2322
John Stultzcef90372013-03-22 15:04:13 -07002323 if (txc->modes & ADJ_SETOFFSET) {
2324 struct timespec delta;
2325 delta.tv_sec = txc->time.tv_sec;
2326 delta.tv_nsec = txc->time.tv_usec;
2327 if (!(txc->modes & ADJ_NANO))
2328 delta.tv_nsec *= 1000;
2329 ret = timekeeping_inject_offset(&delta);
2330 if (ret)
2331 return ret;
2332 }
2333
Thomas Gleixnerd6d29892014-07-16 21:04:04 +00002334 getnstimeofday64(&ts);
John Stultzaa6f9c592013-03-22 11:31:29 -07002335
John Stultz06c017f2013-03-22 11:37:28 -07002336 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002337 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002338
John Stultz4e8f8b32013-04-10 12:41:49 -07002339 orig_tai = tai = tk->tai_offset;
John Stultz87ace392013-03-22 12:28:15 -07002340 ret = __do_adjtimex(txc, &ts, &tai);
2341
John Stultz4e8f8b32013-04-10 12:41:49 -07002342 if (tai != orig_tai) {
2343 __timekeeping_set_tai_offset(tk, tai);
John Stultzf55c0762013-12-11 18:50:25 -08002344 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultz4e8f8b32013-04-10 12:41:49 -07002345 }
John Stultz833f32d2015-06-11 15:54:55 -07002346 tk_update_leap_state(tk);
2347
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002348 write_seqcount_end(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002349 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
2350
John Stultz6fdda9a2013-12-10 17:18:18 -08002351 if (tai != orig_tai)
2352 clock_was_set();
2353
John Stultz7bd36012013-09-11 16:50:56 -07002354 ntp_notify_cmos_timer();
2355
John Stultz87ace392013-03-22 12:28:15 -07002356 return ret;
2357}
John Stultzaa6f9c592013-03-22 11:31:29 -07002358
2359#ifdef CONFIG_NTP_PPS
2360/**
2361 * hardpps() - Accessor function to NTP __hardpps function
2362 */
Arnd Bergmann7ec88e42015-09-28 22:21:28 +02002363void hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts)
John Stultzaa6f9c592013-03-22 11:31:29 -07002364{
John Stultz06c017f2013-03-22 11:37:28 -07002365 unsigned long flags;
2366
2367 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002368 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002369
John Stultzaa6f9c592013-03-22 11:31:29 -07002370 __hardpps(phase_ts, raw_ts);
John Stultz06c017f2013-03-22 11:37:28 -07002371
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002372 write_seqcount_end(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002373 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzaa6f9c592013-03-22 11:31:29 -07002374}
2375EXPORT_SYMBOL(hardpps);
2376#endif
2377
2378/**
Torben Hohnf0af911a92011-01-27 15:59:10 +01002379 * xtime_update() - advances the timekeeping infrastructure
2380 * @ticks: number of ticks, that have elapsed since the last call.
2381 *
2382 * Must be called with interrupts disabled.
2383 */
2384void xtime_update(unsigned long ticks)
2385{
John Stultzd6ad4182012-02-28 16:50:11 -08002386 write_seqlock(&jiffies_lock);
Torben Hohnf0af911a92011-01-27 15:59:10 +01002387 do_timer(ticks);
John Stultzd6ad4182012-02-28 16:50:11 -08002388 write_sequnlock(&jiffies_lock);
John Stultz47a1b7962013-12-12 13:10:55 -08002389 update_wall_time();
Torben Hohnf0af911a92011-01-27 15:59:10 +01002390}