blob: 6a92794427c9612df4876452e7b8826ca44d9d1c [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>
Ingo Molnar38b8d202017-02-08 18:51:31 +010017#include <linux/nmi.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040018#include <linux/sched.h>
Ingo Molnar4f177222017-02-08 08:45:17 +010019#include <linux/sched/loadavg.h>
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +010020#include <linux/syscore_ops.h>
john stultz85240702007-05-08 00:27:59 -070021#include <linux/clocksource.h>
22#include <linux/jiffies.h>
23#include <linux/time.h>
24#include <linux/tick.h>
Martin Schwidefsky75c51582009-08-14 15:47:30 +020025#include <linux/stop_machine.h>
Marcelo Tosattie0b306f2012-11-27 23:28:59 -020026#include <linux/pvclock_gtod.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070027#include <linux/compiler.h>
john stultz85240702007-05-08 00:27:59 -070028
Thomas Gleixnereb93e4d2013-02-21 22:51:36 +000029#include "tick-internal.h"
John Stultzaa6f9c592013-03-22 11:31:29 -070030#include "ntp_internal.h"
Colin Cross5c835452013-05-21 22:32:14 -070031#include "timekeeping_internal.h"
Martin Schwidefsky155ec602009-08-14 15:47:26 +020032
David Vrabel04397fe92013-06-27 11:35:45 +010033#define TK_CLEAR_NTP (1 << 0)
34#define TK_MIRROR (1 << 1)
David Vrabel780427f2013-06-27 11:35:46 +010035#define TK_CLOCK_WAS_SET (1 << 2)
David Vrabel04397fe92013-06-27 11:35:45 +010036
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +000037/*
38 * The most important data for readout fits into a single 64 byte
39 * cache line.
40 */
41static struct {
42 seqcount_t seq;
43 struct timekeeper timekeeper;
44} tk_core ____cacheline_aligned;
45
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +000046static DEFINE_RAW_SPINLOCK(timekeeper_lock);
Thomas Gleixner48cdc132013-02-21 22:51:40 +000047static struct timekeeper shadow_timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020048
Thomas Gleixner4396e052014-07-16 21:05:23 +000049/**
50 * struct tk_fast - NMI safe timekeeper
51 * @seq: Sequence counter for protecting updates. The lowest bit
52 * is the index for the tk_read_base array
53 * @base: tk_read_base array. Access is indexed by the lowest bit of
54 * @seq.
55 *
56 * See @update_fast_timekeeper() below.
57 */
58struct tk_fast {
59 seqcount_t seq;
60 struct tk_read_base base[2];
61};
62
Prarit Bhargava5df32102017-08-28 08:21:53 -040063/* Suspend-time cycles value for halted fast timekeeper. */
64static u64 cycles_at_suspend;
65
66static u64 dummy_clock_read(struct clocksource *cs)
67{
68 return cycles_at_suspend;
69}
70
71static struct clocksource dummy_clock = {
72 .read = dummy_clock_read,
73};
74
75static struct tk_fast tk_fast_mono ____cacheline_aligned = {
76 .base[0] = { .clock = &dummy_clock, },
77 .base[1] = { .clock = &dummy_clock, },
78};
79
80static struct tk_fast tk_fast_raw ____cacheline_aligned = {
81 .base[0] = { .clock = &dummy_clock, },
82 .base[1] = { .clock = &dummy_clock, },
83};
Thomas Gleixner4396e052014-07-16 21:05:23 +000084
John Stultz8fcce542011-11-14 11:46:39 -080085/* flag for if timekeeping is suspended */
86int __read_mostly timekeeping_suspended;
87
John Stultz1e75fa82012-07-13 01:21:53 -040088static inline void tk_normalize_xtime(struct timekeeper *tk)
89{
Peter Zijlstra876e7882015-03-19 10:09:06 +010090 while (tk->tkr_mono.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_mono.shift)) {
91 tk->tkr_mono.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
John Stultz1e75fa82012-07-13 01:21:53 -040092 tk->xtime_sec++;
93 }
John Stultzfc6eead72017-05-22 17:20:20 -070094 while (tk->tkr_raw.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_raw.shift)) {
95 tk->tkr_raw.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
96 tk->raw_sec++;
97 }
John Stultz1e75fa82012-07-13 01:21:53 -040098}
John Stultz8fcce542011-11-14 11:46:39 -080099
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000100static inline struct timespec64 tk_xtime(struct timekeeper *tk)
101{
102 struct timespec64 ts;
103
104 ts.tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100105 ts.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000106 return ts;
107}
108
John Stultz7d489d12014-07-16 21:04:01 +0000109static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
John Stultz1e75fa82012-07-13 01:21:53 -0400110{
111 tk->xtime_sec = ts->tv_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100112 tk->tkr_mono.xtime_nsec = (u64)ts->tv_nsec << tk->tkr_mono.shift;
John Stultz1e75fa82012-07-13 01:21:53 -0400113}
114
John Stultz7d489d12014-07-16 21:04:01 +0000115static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
John Stultz1e75fa82012-07-13 01:21:53 -0400116{
117 tk->xtime_sec += ts->tv_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100118 tk->tkr_mono.xtime_nsec += (u64)ts->tv_nsec << tk->tkr_mono.shift;
John Stultz784ffcb2012-08-21 20:30:46 -0400119 tk_normalize_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -0400120}
John Stultz8fcce542011-11-14 11:46:39 -0800121
John Stultz7d489d12014-07-16 21:04:01 +0000122static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
John Stultz6d0ef902012-07-27 14:48:12 -0400123{
John Stultz7d489d12014-07-16 21:04:01 +0000124 struct timespec64 tmp;
John Stultz6d0ef902012-07-27 14:48:12 -0400125
126 /*
127 * Verify consistency of: offset_real = -wall_to_monotonic
128 * before modifying anything
129 */
John Stultz7d489d12014-07-16 21:04:01 +0000130 set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec,
John Stultz6d0ef902012-07-27 14:48:12 -0400131 -tk->wall_to_monotonic.tv_nsec);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100132 WARN_ON_ONCE(tk->offs_real != timespec64_to_ktime(tmp));
John Stultz6d0ef902012-07-27 14:48:12 -0400133 tk->wall_to_monotonic = wtm;
John Stultz7d489d12014-07-16 21:04:01 +0000134 set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
135 tk->offs_real = timespec64_to_ktime(tmp);
John Stultz04005f62013-12-10 17:13:35 -0800136 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
John Stultz6d0ef902012-07-27 14:48:12 -0400137}
138
Thomas Gleixner47da70d2014-07-16 21:05:00 +0000139static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
John Stultz6d0ef902012-07-27 14:48:12 -0400140{
Thomas Gleixner47da70d2014-07-16 21:05:00 +0000141 tk->offs_boot = ktime_add(tk->offs_boot, delta);
John Stultz6d0ef902012-07-27 14:48:12 -0400142}
143
John Stultzceea5e32017-06-08 16:44:20 -0700144/*
145 * tk_clock_read - atomic clocksource read() helper
146 *
147 * This helper is necessary to use in the read paths because, while the
148 * seqlock ensures we don't return a bad value while structures are updated,
149 * it doesn't protect from potential crashes. There is the possibility that
150 * the tkr's clocksource may change between the read reference, and the
151 * clock reference passed to the read function. This can cause crashes if
152 * the wrong clocksource is passed to the wrong read function.
153 * This isn't necessary to use when holding the timekeeper_lock or doing
154 * a read of the fast-timekeeper tkrs (which is protected by its own locking
155 * and update logic).
156 */
157static inline u64 tk_clock_read(struct tk_read_base *tkr)
158{
159 struct clocksource *clock = READ_ONCE(tkr->clock);
160
161 return clock->read(clock);
162}
163
John Stultz3c17ad12015-03-11 21:16:32 -0700164#ifdef CONFIG_DEBUG_TIMEKEEPING
John Stultz4ca22c22015-03-11 21:16:35 -0700165#define WARNING_FREQ (HZ*300) /* 5 minute rate-limiting */
John Stultz4ca22c22015-03-11 21:16:35 -0700166
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100167static void timekeeping_check_update(struct timekeeper *tk, u64 offset)
John Stultz3c17ad12015-03-11 21:16:32 -0700168{
169
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100170 u64 max_cycles = tk->tkr_mono.clock->max_cycles;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100171 const char *name = tk->tkr_mono.clock->name;
John Stultz3c17ad12015-03-11 21:16:32 -0700172
173 if (offset > max_cycles) {
John Stultza558cd02015-03-11 21:16:33 -0700174 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 -0700175 offset, name, max_cycles);
John Stultza558cd02015-03-11 21:16:33 -0700176 printk_deferred(" timekeeping: Your kernel is sick, but tries to cope by capping time updates\n");
John Stultz3c17ad12015-03-11 21:16:32 -0700177 } else {
178 if (offset > (max_cycles >> 1)) {
Masanari Iidafc4fa6e2015-12-13 15:26:11 +0900179 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 -0700180 offset, name, max_cycles >> 1);
181 printk_deferred(" timekeeping: Your kernel is still fine, but is feeling a bit nervous\n");
182 }
183 }
John Stultz4ca22c22015-03-11 21:16:35 -0700184
John Stultz57d05a92015-05-13 16:04:47 -0700185 if (tk->underflow_seen) {
186 if (jiffies - tk->last_warning > WARNING_FREQ) {
John Stultz4ca22c22015-03-11 21:16:35 -0700187 printk_deferred("WARNING: Underflow in clocksource '%s' observed, time update ignored.\n", name);
188 printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
189 printk_deferred(" Your kernel is probably still fine.\n");
John Stultz57d05a92015-05-13 16:04:47 -0700190 tk->last_warning = jiffies;
John Stultz4ca22c22015-03-11 21:16:35 -0700191 }
John Stultz57d05a92015-05-13 16:04:47 -0700192 tk->underflow_seen = 0;
John Stultz4ca22c22015-03-11 21:16:35 -0700193 }
194
John Stultz57d05a92015-05-13 16:04:47 -0700195 if (tk->overflow_seen) {
196 if (jiffies - tk->last_warning > WARNING_FREQ) {
John Stultz4ca22c22015-03-11 21:16:35 -0700197 printk_deferred("WARNING: Overflow in clocksource '%s' observed, time update capped.\n", name);
198 printk_deferred(" Please report this, consider using a different clocksource, if possible.\n");
199 printk_deferred(" Your kernel is probably still fine.\n");
John Stultz57d05a92015-05-13 16:04:47 -0700200 tk->last_warning = jiffies;
John Stultz4ca22c22015-03-11 21:16:35 -0700201 }
John Stultz57d05a92015-05-13 16:04:47 -0700202 tk->overflow_seen = 0;
John Stultz4ca22c22015-03-11 21:16:35 -0700203 }
John Stultz3c17ad12015-03-11 21:16:32 -0700204}
John Stultza558cd02015-03-11 21:16:33 -0700205
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100206static inline u64 timekeeping_get_delta(struct tk_read_base *tkr)
John Stultza558cd02015-03-11 21:16:33 -0700207{
John Stultz57d05a92015-05-13 16:04:47 -0700208 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100209 u64 now, last, mask, max, delta;
John Stultz4ca22c22015-03-11 21:16:35 -0700210 unsigned int seq;
John Stultza558cd02015-03-11 21:16:33 -0700211
John Stultz4ca22c22015-03-11 21:16:35 -0700212 /*
213 * Since we're called holding a seqlock, the data may shift
214 * under us while we're doing the calculation. This can cause
215 * false positives, since we'd note a problem but throw the
216 * results away. So nest another seqlock here to atomically
217 * grab the points we are checking with.
218 */
219 do {
220 seq = read_seqcount_begin(&tk_core.seq);
John Stultzceea5e32017-06-08 16:44:20 -0700221 now = tk_clock_read(tkr);
John Stultz4ca22c22015-03-11 21:16:35 -0700222 last = tkr->cycle_last;
223 mask = tkr->mask;
224 max = tkr->clock->max_cycles;
225 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultza558cd02015-03-11 21:16:33 -0700226
John Stultz4ca22c22015-03-11 21:16:35 -0700227 delta = clocksource_delta(now, last, mask);
John Stultza558cd02015-03-11 21:16:33 -0700228
John Stultz057b87e2015-03-11 21:16:34 -0700229 /*
230 * Try to catch underflows by checking if we are seeing small
231 * mask-relative negative values.
232 */
John Stultz4ca22c22015-03-11 21:16:35 -0700233 if (unlikely((~delta & mask) < (mask >> 3))) {
John Stultz57d05a92015-05-13 16:04:47 -0700234 tk->underflow_seen = 1;
John Stultz057b87e2015-03-11 21:16:34 -0700235 delta = 0;
John Stultz4ca22c22015-03-11 21:16:35 -0700236 }
John Stultz057b87e2015-03-11 21:16:34 -0700237
John Stultza558cd02015-03-11 21:16:33 -0700238 /* Cap delta value to the max_cycles values to avoid mult overflows */
John Stultz4ca22c22015-03-11 21:16:35 -0700239 if (unlikely(delta > max)) {
John Stultz57d05a92015-05-13 16:04:47 -0700240 tk->overflow_seen = 1;
John Stultza558cd02015-03-11 21:16:33 -0700241 delta = tkr->clock->max_cycles;
John Stultz4ca22c22015-03-11 21:16:35 -0700242 }
John Stultza558cd02015-03-11 21:16:33 -0700243
244 return delta;
245}
John Stultz3c17ad12015-03-11 21:16:32 -0700246#else
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100247static inline void timekeeping_check_update(struct timekeeper *tk, u64 offset)
John Stultz3c17ad12015-03-11 21:16:32 -0700248{
249}
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100250static inline u64 timekeeping_get_delta(struct tk_read_base *tkr)
John Stultza558cd02015-03-11 21:16:33 -0700251{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100252 u64 cycle_now, delta;
John Stultza558cd02015-03-11 21:16:33 -0700253
254 /* read clocksource */
John Stultzceea5e32017-06-08 16:44:20 -0700255 cycle_now = tk_clock_read(tkr);
John Stultza558cd02015-03-11 21:16:33 -0700256
257 /* calculate the delta since the last update_wall_time */
258 delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
259
260 return delta;
261}
John Stultz3c17ad12015-03-11 21:16:32 -0700262#endif
263
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200264/**
Yijing Wangd26e4fe2013-11-28 16:28:55 +0800265 * tk_setup_internals - Set up internals to use clocksource clock.
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200266 *
Yijing Wangd26e4fe2013-11-28 16:28:55 +0800267 * @tk: The target timekeeper to setup.
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200268 * @clock: Pointer to clocksource.
269 *
270 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
271 * pair and interval request.
272 *
273 * Unless you're the timekeeping code, you should not be using this!
274 */
John Stultzf726a692012-07-13 01:21:57 -0400275static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200276{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100277 u64 interval;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700278 u64 tmp, ntpinterval;
John Stultz1e75fa82012-07-13 01:21:53 -0400279 struct clocksource *old_clock;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200280
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800281 ++tk->cs_was_changed_seq;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100282 old_clock = tk->tkr_mono.clock;
283 tk->tkr_mono.clock = clock;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100284 tk->tkr_mono.mask = clock->mask;
John Stultzceea5e32017-06-08 16:44:20 -0700285 tk->tkr_mono.cycle_last = tk_clock_read(&tk->tkr_mono);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200286
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100287 tk->tkr_raw.clock = clock;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100288 tk->tkr_raw.mask = clock->mask;
289 tk->tkr_raw.cycle_last = tk->tkr_mono.cycle_last;
290
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200291 /* Do the ns -> cycle conversion first, using original mult */
292 tmp = NTP_INTERVAL_LENGTH;
293 tmp <<= clock->shift;
Kasper Pedersena386b5a2010-10-20 15:55:15 -0700294 ntpinterval = tmp;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200295 tmp += clock->mult/2;
296 do_div(tmp, clock->mult);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200297 if (tmp == 0)
298 tmp = 1;
299
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100300 interval = (u64) tmp;
John Stultzf726a692012-07-13 01:21:57 -0400301 tk->cycle_interval = interval;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200302
303 /* Go back from cycles -> shifted ns */
Thomas Gleixnercbd99e32016-12-08 20:49:36 +0000304 tk->xtime_interval = interval * clock->mult;
John Stultzf726a692012-07-13 01:21:57 -0400305 tk->xtime_remainder = ntpinterval - tk->xtime_interval;
John Stultz3d88d562017-06-08 16:44:21 -0700306 tk->raw_interval = interval * clock->mult;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200307
John Stultz1e75fa82012-07-13 01:21:53 -0400308 /* if changing clocks, convert xtime_nsec shift units */
309 if (old_clock) {
310 int shift_change = clock->shift - old_clock->shift;
John Stultzfc6eead72017-05-22 17:20:20 -0700311 if (shift_change < 0) {
Peter Zijlstra876e7882015-03-19 10:09:06 +0100312 tk->tkr_mono.xtime_nsec >>= -shift_change;
John Stultzfc6eead72017-05-22 17:20:20 -0700313 tk->tkr_raw.xtime_nsec >>= -shift_change;
314 } else {
Peter Zijlstra876e7882015-03-19 10:09:06 +0100315 tk->tkr_mono.xtime_nsec <<= shift_change;
John Stultzfc6eead72017-05-22 17:20:20 -0700316 tk->tkr_raw.xtime_nsec <<= shift_change;
317 }
John Stultz1e75fa82012-07-13 01:21:53 -0400318 }
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100319
Peter Zijlstra876e7882015-03-19 10:09:06 +0100320 tk->tkr_mono.shift = clock->shift;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100321 tk->tkr_raw.shift = clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200322
John Stultzf726a692012-07-13 01:21:57 -0400323 tk->ntp_error = 0;
324 tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
John Stultz375f45b2014-04-23 20:53:29 -0700325 tk->ntp_tick = ntpinterval << tk->ntp_error_shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200326
327 /*
328 * The timekeeper keeps its own mult values for the currently
329 * active clocksource. These value will be adjusted via NTP
330 * to counteract clock drifting.
331 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100332 tk->tkr_mono.mult = clock->mult;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100333 tk->tkr_raw.mult = clock->mult;
John Stultzdc491592013-12-06 17:25:21 -0800334 tk->ntp_err_mult = 0;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200335}
john stultz85240702007-05-08 00:27:59 -0700336
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200337/* Timekeeper helper functions. */
Stephen Warren7b1f6202012-11-07 17:58:54 -0700338
339#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000340static u32 default_arch_gettimeoffset(void) { return 0; }
341u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset;
Stephen Warren7b1f6202012-11-07 17:58:54 -0700342#else
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000343static inline u32 arch_gettimeoffset(void) { return 0; }
Stephen Warren7b1f6202012-11-07 17:58:54 -0700344#endif
345
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100346static inline u64 timekeeping_delta_to_ns(struct tk_read_base *tkr, u64 delta)
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200347{
Thomas Gleixner9c164572016-12-08 20:49:32 +0000348 u64 nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200349
Christopher S. Hall6bd58f02016-02-22 03:15:19 -0800350 nsec = delta * tkr->mult + tkr->xtime_nsec;
351 nsec >>= tkr->shift;
John Stultzf2a5a082012-07-13 01:21:55 -0400352
Stephen Warren7b1f6202012-11-07 17:58:54 -0700353 /* If arch requires, add in get_arch_timeoffset() */
Thomas Gleixnere06fde32014-07-16 21:03:50 +0000354 return nsec + arch_gettimeoffset();
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200355}
356
Thomas Gleixneracc89612016-12-08 20:49:34 +0000357static inline u64 timekeeping_get_ns(struct tk_read_base *tkr)
Christopher S. Hall6bd58f02016-02-22 03:15:19 -0800358{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100359 u64 delta;
Christopher S. Hall6bd58f02016-02-22 03:15:19 -0800360
361 delta = timekeeping_get_delta(tkr);
362 return timekeeping_delta_to_ns(tkr, delta);
363}
364
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100365static inline u64 timekeeping_cycles_to_ns(struct tk_read_base *tkr, u64 cycles)
Christopher S. Hall6bd58f02016-02-22 03:15:19 -0800366{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100367 u64 delta;
Christopher S. Hall6bd58f02016-02-22 03:15:19 -0800368
369 /* calculate the delta since the last update_wall_time */
370 delta = clocksource_delta(cycles, tkr->cycle_last, tkr->mask);
371 return timekeeping_delta_to_ns(tkr, delta);
372}
373
Thomas Gleixner4396e052014-07-16 21:05:23 +0000374/**
375 * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
Rafael J. Wysockiaffe3e82015-02-11 05:01:52 +0100376 * @tkr: Timekeeping readout base from which we take the update
Thomas Gleixner4396e052014-07-16 21:05:23 +0000377 *
378 * We want to use this from any context including NMI and tracing /
379 * instrumenting the timekeeping code itself.
380 *
Peter Zijlstra6695b922015-05-27 11:09:36 +0930381 * Employ the latch technique; see @raw_write_seqcount_latch.
Thomas Gleixner4396e052014-07-16 21:05:23 +0000382 *
383 * So if a NMI hits the update of base[0] then it will use base[1]
384 * which is still consistent. In the worst case this can result is a
385 * slightly wrong timestamp (a few nanoseconds). See
386 * @ktime_get_mono_fast_ns.
387 */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100388static void update_fast_timekeeper(struct tk_read_base *tkr, struct tk_fast *tkf)
Thomas Gleixner4396e052014-07-16 21:05:23 +0000389{
Peter Zijlstra4498e742015-03-19 09:36:19 +0100390 struct tk_read_base *base = tkf->base;
Thomas Gleixner4396e052014-07-16 21:05:23 +0000391
392 /* Force readers off to base[1] */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100393 raw_write_seqcount_latch(&tkf->seq);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000394
395 /* Update base[0] */
Rafael J. Wysockiaffe3e82015-02-11 05:01:52 +0100396 memcpy(base, tkr, sizeof(*base));
Thomas Gleixner4396e052014-07-16 21:05:23 +0000397
398 /* Force readers back to base[0] */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100399 raw_write_seqcount_latch(&tkf->seq);
Thomas Gleixner4396e052014-07-16 21:05:23 +0000400
401 /* Update base[1] */
402 memcpy(base + 1, base, sizeof(*base));
403}
404
405/**
406 * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic
407 *
408 * This timestamp is not guaranteed to be monotonic across an update.
409 * The timestamp is calculated by:
410 *
411 * now = base_mono + clock_delta * slope
412 *
413 * So if the update lowers the slope, readers who are forced to the
414 * not yet updated second array are still using the old steeper slope.
415 *
416 * tmono
417 * ^
418 * | o n
419 * | o n
420 * | u
421 * | o
422 * |o
423 * |12345678---> reader order
424 *
425 * o = old slope
426 * u = update
427 * n = new slope
428 *
429 * So reader 6 will observe time going backwards versus reader 5.
430 *
431 * While other CPUs are likely to be able observe that, the only way
432 * for a CPU local observation is when an NMI hits in the middle of
433 * the update. Timestamps taken from that NMI context might be ahead
434 * of the following timestamps. Callers need to be aware of that and
435 * deal with it.
436 */
Peter Zijlstra4498e742015-03-19 09:36:19 +0100437static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf)
Thomas Gleixner4396e052014-07-16 21:05:23 +0000438{
439 struct tk_read_base *tkr;
440 unsigned int seq;
441 u64 now;
442
443 do {
Peter Zijlstra7fc26322015-05-27 11:09:36 +0930444 seq = raw_read_seqcount_latch(&tkf->seq);
Peter Zijlstra4498e742015-03-19 09:36:19 +0100445 tkr = tkf->base + (seq & 0x01);
John Stultz27727df2016-08-23 16:08:21 -0700446 now = ktime_to_ns(tkr->base);
447
John Stultz58bfea92016-10-04 19:55:48 -0700448 now += timekeeping_delta_to_ns(tkr,
449 clocksource_delta(
John Stultzceea5e32017-06-08 16:44:20 -0700450 tk_clock_read(tkr),
John Stultz58bfea92016-10-04 19:55:48 -0700451 tkr->cycle_last,
452 tkr->mask));
Peter Zijlstra4498e742015-03-19 09:36:19 +0100453 } while (read_seqcount_retry(&tkf->seq, seq));
Thomas Gleixner4396e052014-07-16 21:05:23 +0000454
Thomas Gleixner4396e052014-07-16 21:05:23 +0000455 return now;
456}
Peter Zijlstra4498e742015-03-19 09:36:19 +0100457
458u64 ktime_get_mono_fast_ns(void)
459{
460 return __ktime_get_fast_ns(&tk_fast_mono);
461}
Thomas Gleixner4396e052014-07-16 21:05:23 +0000462EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
463
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100464u64 ktime_get_raw_fast_ns(void)
465{
466 return __ktime_get_fast_ns(&tk_fast_raw);
467}
468EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns);
469
Joel Fernandes948a5312016-11-28 14:35:22 -0800470/**
471 * ktime_get_boot_fast_ns - NMI safe and fast access to boot clock.
472 *
473 * To keep it NMI safe since we're accessing from tracing, we're not using a
474 * separate timekeeper with updates to monotonic clock and boot offset
475 * protected with seqlocks. This has the following minor side effects:
476 *
477 * (1) Its possible that a timestamp be taken after the boot offset is updated
478 * but before the timekeeper is updated. If this happens, the new boot offset
479 * is added to the old timekeeping making the clock appear to update slightly
480 * earlier:
481 * CPU 0 CPU 1
482 * timekeeping_inject_sleeptime64()
483 * __timekeeping_inject_sleeptime(tk, delta);
484 * timestamp();
485 * timekeeping_update(tk, TK_CLEAR_NTP...);
486 *
487 * (2) On 32-bit systems, the 64-bit boot offset (tk->offs_boot) may be
488 * partially updated. Since the tk->offs_boot update is a rare event, this
489 * should be a rare occurrence which postprocessing should be able to handle.
490 */
491u64 notrace ktime_get_boot_fast_ns(void)
492{
493 struct timekeeper *tk = &tk_core.timekeeper;
494
495 return (ktime_get_mono_fast_ns() + ktime_to_ns(tk->offs_boot));
496}
497EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns);
498
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100499/**
500 * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource.
501 * @tk: Timekeeper to snapshot.
502 *
503 * It generally is unsafe to access the clocksource after timekeeping has been
504 * suspended, so take a snapshot of the readout base of @tk and use it as the
505 * fast timekeeper's readout base while suspended. It will return the same
506 * number of cycles every time until timekeeping is resumed at which time the
507 * proper readout base for the fast timekeeper will be restored automatically.
508 */
509static void halt_fast_timekeeper(struct timekeeper *tk)
510{
511 static struct tk_read_base tkr_dummy;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100512 struct tk_read_base *tkr = &tk->tkr_mono;
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100513
514 memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
John Stultzceea5e32017-06-08 16:44:20 -0700515 cycles_at_suspend = tk_clock_read(tkr);
516 tkr_dummy.clock = &dummy_clock;
Peter Zijlstra4498e742015-03-19 09:36:19 +0100517 update_fast_timekeeper(&tkr_dummy, &tk_fast_mono);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100518
519 tkr = &tk->tkr_raw;
520 memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
John Stultzceea5e32017-06-08 16:44:20 -0700521 tkr_dummy.clock = &dummy_clock;
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100522 update_fast_timekeeper(&tkr_dummy, &tk_fast_raw);
Rafael J. Wysocki060407a2015-02-13 14:49:02 +0100523}
524
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000525#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
John Stultz369adf02017-05-12 10:58:18 -0700526#warning Please contact your maintainers, as GENERIC_TIME_VSYSCALL_OLD compatibity will disappear soon.
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000527
528static inline void update_vsyscall(struct timekeeper *tk)
529{
John Stultz0680eb12014-08-13 12:47:14 -0700530 struct timespec xt, wm;
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000531
John Stultze2dff1e2014-07-23 14:35:39 -0700532 xt = timespec64_to_timespec(tk_xtime(tk));
John Stultz0680eb12014-08-13 12:47:14 -0700533 wm = timespec64_to_timespec(tk->wall_to_monotonic);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100534 update_vsyscall_old(&xt, &wm, tk->tkr_mono.clock, tk->tkr_mono.mult,
535 tk->tkr_mono.cycle_last);
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000536}
537
538static inline void old_vsyscall_fixup(struct timekeeper *tk)
539{
540 s64 remainder;
541
542 /*
543 * Store only full nanoseconds into xtime_nsec after rounding
544 * it up and add the remainder to the error difference.
545 * XXX - This is necessary to avoid small 1ns inconsistnecies caused
546 * by truncating the remainder in vsyscalls. However, it causes
547 * additional work to be done in timekeeping_adjust(). Once
548 * the vsyscall implementations are converted to use xtime_nsec
549 * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
550 * users are removed, this can be killed.
551 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100552 remainder = tk->tkr_mono.xtime_nsec & ((1ULL << tk->tkr_mono.shift) - 1);
Thomas Graziadei0209b932016-05-31 15:06:06 +0200553 if (remainder != 0) {
554 tk->tkr_mono.xtime_nsec -= remainder;
555 tk->tkr_mono.xtime_nsec += 1ULL << tk->tkr_mono.shift;
556 tk->ntp_error += remainder << tk->ntp_error_shift;
557 tk->ntp_error -= (1ULL << tk->tkr_mono.shift) << tk->ntp_error_shift;
558 }
Thomas Gleixnerc905fae2014-07-16 21:04:05 +0000559}
560#else
561#define old_vsyscall_fixup(tk)
562#endif
563
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200564static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
565
David Vrabel780427f2013-06-27 11:35:46 +0100566static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200567{
David Vrabel780427f2013-06-27 11:35:46 +0100568 raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200569}
570
571/**
572 * pvclock_gtod_register_notifier - register a pvclock timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200573 */
574int pvclock_gtod_register_notifier(struct notifier_block *nb)
575{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000576 struct timekeeper *tk = &tk_core.timekeeper;
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200577 unsigned long flags;
578 int ret;
579
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000580 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200581 ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
David Vrabel780427f2013-06-27 11:35:46 +0100582 update_pvclock_gtod(tk, true);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000583 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200584
585 return ret;
586}
587EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
588
589/**
590 * pvclock_gtod_unregister_notifier - unregister a pvclock
591 * timedata update listener
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200592 */
593int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
594{
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200595 unsigned long flags;
596 int ret;
597
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000598 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200599 ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000600 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
Marcelo Tosattie0b306f2012-11-27 23:28:59 -0200601
602 return ret;
603}
604EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
605
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000606/*
John Stultz833f32d2015-06-11 15:54:55 -0700607 * tk_update_leap_state - helper to update the next_leap_ktime
608 */
609static inline void tk_update_leap_state(struct timekeeper *tk)
610{
611 tk->next_leap_ktime = ntp_get_next_leap();
Thomas Gleixner2456e852016-12-25 11:38:40 +0100612 if (tk->next_leap_ktime != KTIME_MAX)
John Stultz833f32d2015-06-11 15:54:55 -0700613 /* Convert to monotonic time */
614 tk->next_leap_ktime = ktime_sub(tk->next_leap_ktime, tk->offs_real);
615}
616
617/*
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000618 * Update the ktime_t based scalar nsec members of the timekeeper
619 */
620static inline void tk_update_ktime_data(struct timekeeper *tk)
621{
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530622 u64 seconds;
623 u32 nsec;
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000624
625 /*
626 * The xtime based monotonic readout is:
627 * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();
628 * The ktime based monotonic readout is:
629 * nsec = base_mono + now();
630 * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
631 */
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530632 seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
633 nsec = (u32) tk->wall_to_monotonic.tv_nsec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100634 tk->tkr_mono.base = ns_to_ktime(seconds * NSEC_PER_SEC + nsec);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000635
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530636 /*
637 * The sum of the nanoseconds portions of xtime and
638 * wall_to_monotonic can be greater/equal one second. Take
639 * this into account before updating tk->ktime_sec.
640 */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100641 nsec += (u32)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530642 if (nsec >= NSEC_PER_SEC)
643 seconds++;
644 tk->ktime_sec = seconds;
John Stultzfc6eead72017-05-22 17:20:20 -0700645
646 /* Update the monotonic raw base */
John Stultz0bcdc092017-08-25 15:57:04 -0700647 tk->tkr_raw.base = ns_to_ktime(tk->raw_sec * NSEC_PER_SEC);
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000648}
649
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +0000650/* must hold timekeeper_lock */
David Vrabel04397fe92013-06-27 11:35:45 +0100651static void timekeeping_update(struct timekeeper *tk, unsigned int action)
Thomas Gleixnercc062682011-11-13 23:19:49 +0000652{
David Vrabel04397fe92013-06-27 11:35:45 +0100653 if (action & TK_CLEAR_NTP) {
John Stultzf726a692012-07-13 01:21:57 -0400654 tk->ntp_error = 0;
Thomas Gleixnercc062682011-11-13 23:19:49 +0000655 ntp_clear();
656 }
Thomas Gleixner48cdc132013-02-21 22:51:40 +0000657
John Stultz833f32d2015-06-11 15:54:55 -0700658 tk_update_leap_state(tk);
Thomas Gleixner7c032df2014-07-16 21:04:10 +0000659 tk_update_ktime_data(tk);
660
Thomas Gleixner9bf24192014-09-06 12:24:49 +0200661 update_vsyscall(tk);
662 update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
663
Peter Zijlstra4498e742015-03-19 09:36:19 +0100664 update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono);
Peter Zijlstraf09cb9a2015-03-19 09:39:08 +0100665 update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw);
Thomas Gleixner868a3e92015-04-14 21:08:37 +0000666
667 if (action & TK_CLOCK_WAS_SET)
668 tk->clock_was_set_seq++;
John Stultzd1518322015-06-11 15:54:53 -0700669 /*
670 * The mirroring of the data to the shadow-timekeeper needs
671 * to happen last here to ensure we don't over-write the
672 * timekeeper structure on the next update with stale data
673 */
674 if (action & TK_MIRROR)
675 memcpy(&shadow_timekeeper, &tk_core.timekeeper,
676 sizeof(tk_core.timekeeper));
Thomas Gleixnercc062682011-11-13 23:19:49 +0000677}
678
john stultz85240702007-05-08 00:27:59 -0700679/**
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200680 * timekeeping_forward_now - update clock to the current time
john stultz85240702007-05-08 00:27:59 -0700681 *
Roman Zippel9a055112008-08-20 16:37:28 -0700682 * Forward the current clock to update its state since the last call to
683 * update_wall_time(). This is useful before significant clock changes,
684 * as it avoids having to deal with this time offset explicitly.
john stultz85240702007-05-08 00:27:59 -0700685 */
John Stultzf726a692012-07-13 01:21:57 -0400686static void timekeeping_forward_now(struct timekeeper *tk)
john stultz85240702007-05-08 00:27:59 -0700687{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100688 u64 cycle_now, delta;
john stultz85240702007-05-08 00:27:59 -0700689
John Stultzceea5e32017-06-08 16:44:20 -0700690 cycle_now = tk_clock_read(&tk->tkr_mono);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100691 delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
692 tk->tkr_mono.cycle_last = cycle_now;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100693 tk->tkr_raw.cycle_last = cycle_now;
john stultz85240702007-05-08 00:27:59 -0700694
Peter Zijlstra876e7882015-03-19 10:09:06 +0100695 tk->tkr_mono.xtime_nsec += delta * tk->tkr_mono.mult;
john stultz7d275582009-05-01 13:10:26 -0700696
Stephen Warren7b1f6202012-11-07 17:58:54 -0700697 /* If arch requires, add in get_arch_timeoffset() */
Peter Zijlstra876e7882015-03-19 10:09:06 +0100698 tk->tkr_mono.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_mono.shift;
john stultz7d275582009-05-01 13:10:26 -0700699
John Stultz2d422442008-08-20 16:37:30 -0700700
John Stultzfc6eead72017-05-22 17:20:20 -0700701 tk->tkr_raw.xtime_nsec += delta * tk->tkr_raw.mult;
702
703 /* If arch requires, add in get_arch_timeoffset() */
704 tk->tkr_raw.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_raw.shift;
705
706 tk_normalize_xtime(tk);
john stultz85240702007-05-08 00:27:59 -0700707}
708
709/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000710 * __getnstimeofday64 - Returns the time of day in a timespec64.
john stultz85240702007-05-08 00:27:59 -0700711 * @ts: pointer to the timespec to be set
712 *
Kees Cook1e817fb2012-11-19 10:26:16 -0800713 * Updates the time of day in the timespec.
714 * Returns 0 on success, or -ve when suspended (timespec will be undefined).
john stultz85240702007-05-08 00:27:59 -0700715 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000716int __getnstimeofday64(struct timespec64 *ts)
john stultz85240702007-05-08 00:27:59 -0700717{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000718 struct timekeeper *tk = &tk_core.timekeeper;
john stultz85240702007-05-08 00:27:59 -0700719 unsigned long seq;
Thomas Gleixneracc89612016-12-08 20:49:34 +0000720 u64 nsecs;
john stultz85240702007-05-08 00:27:59 -0700721
722 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000723 seq = read_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -0700724
John Stultz4e250fd2012-07-27 14:48:13 -0400725 ts->tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100726 nsecs = timekeeping_get_ns(&tk->tkr_mono);
john stultz85240702007-05-08 00:27:59 -0700727
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000728 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz85240702007-05-08 00:27:59 -0700729
John Stultzec145ba2012-09-11 19:26:03 -0400730 ts->tv_nsec = 0;
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000731 timespec64_add_ns(ts, nsecs);
Kees Cook1e817fb2012-11-19 10:26:16 -0800732
733 /*
734 * Do not bail out early, in case there were callers still using
735 * the value, even in the face of the WARN_ON.
736 */
737 if (unlikely(timekeeping_suspended))
738 return -EAGAIN;
739 return 0;
740}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000741EXPORT_SYMBOL(__getnstimeofday64);
Kees Cook1e817fb2012-11-19 10:26:16 -0800742
743/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000744 * getnstimeofday64 - Returns the time of day in a timespec64.
John Stultz5322e4c2014-11-07 13:13:04 -0800745 * @ts: pointer to the timespec64 to be set
Kees Cook1e817fb2012-11-19 10:26:16 -0800746 *
John Stultz5322e4c2014-11-07 13:13:04 -0800747 * Returns the time of day in a timespec64 (WARN if suspended).
Kees Cook1e817fb2012-11-19 10:26:16 -0800748 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000749void getnstimeofday64(struct timespec64 *ts)
Kees Cook1e817fb2012-11-19 10:26:16 -0800750{
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000751 WARN_ON(__getnstimeofday64(ts));
john stultz85240702007-05-08 00:27:59 -0700752}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000753EXPORT_SYMBOL(getnstimeofday64);
john stultz85240702007-05-08 00:27:59 -0700754
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200755ktime_t ktime_get(void)
756{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000757 struct timekeeper *tk = &tk_core.timekeeper;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200758 unsigned int seq;
Thomas Gleixnera016a5b2014-07-16 21:04:12 +0000759 ktime_t base;
Thomas Gleixneracc89612016-12-08 20:49:34 +0000760 u64 nsecs;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200761
762 WARN_ON(timekeeping_suspended);
763
764 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000765 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100766 base = tk->tkr_mono.base;
767 nsecs = timekeeping_get_ns(&tk->tkr_mono);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200768
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000769 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz24e4a8c2014-07-16 21:03:53 +0000770
Thomas Gleixnera016a5b2014-07-16 21:04:12 +0000771 return ktime_add_ns(base, nsecs);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200772}
773EXPORT_SYMBOL_GPL(ktime_get);
774
Harald Geyer6374f912015-04-07 11:12:35 +0000775u32 ktime_get_resolution_ns(void)
776{
777 struct timekeeper *tk = &tk_core.timekeeper;
778 unsigned int seq;
779 u32 nsecs;
780
781 WARN_ON(timekeeping_suspended);
782
783 do {
784 seq = read_seqcount_begin(&tk_core.seq);
785 nsecs = tk->tkr_mono.mult >> tk->tkr_mono.shift;
786 } while (read_seqcount_retry(&tk_core.seq, seq));
787
788 return nsecs;
789}
790EXPORT_SYMBOL_GPL(ktime_get_resolution_ns);
791
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000792static ktime_t *offsets[TK_OFFS_MAX] = {
793 [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real,
794 [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot,
795 [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai,
796};
797
798ktime_t ktime_get_with_offset(enum tk_offsets offs)
799{
800 struct timekeeper *tk = &tk_core.timekeeper;
801 unsigned int seq;
802 ktime_t base, *offset = offsets[offs];
Thomas Gleixneracc89612016-12-08 20:49:34 +0000803 u64 nsecs;
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000804
805 WARN_ON(timekeeping_suspended);
806
807 do {
808 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra876e7882015-03-19 10:09:06 +0100809 base = ktime_add(tk->tkr_mono.base, *offset);
810 nsecs = timekeeping_get_ns(&tk->tkr_mono);
Thomas Gleixner0077dc62014-07-16 21:04:13 +0000811
812 } while (read_seqcount_retry(&tk_core.seq, seq));
813
814 return ktime_add_ns(base, nsecs);
815
816}
817EXPORT_SYMBOL_GPL(ktime_get_with_offset);
818
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200819/**
Thomas Gleixner9a6b5192014-07-16 21:04:22 +0000820 * ktime_mono_to_any() - convert mononotic time to any other time
821 * @tmono: time to convert.
822 * @offs: which offset to use
823 */
824ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
825{
826 ktime_t *offset = offsets[offs];
827 unsigned long seq;
828 ktime_t tconv;
829
830 do {
831 seq = read_seqcount_begin(&tk_core.seq);
832 tconv = ktime_add(tmono, *offset);
833 } while (read_seqcount_retry(&tk_core.seq, seq));
834
835 return tconv;
836}
837EXPORT_SYMBOL_GPL(ktime_mono_to_any);
838
839/**
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000840 * ktime_get_raw - Returns the raw monotonic time in ktime_t format
841 */
842ktime_t ktime_get_raw(void)
843{
844 struct timekeeper *tk = &tk_core.timekeeper;
845 unsigned int seq;
846 ktime_t base;
Thomas Gleixneracc89612016-12-08 20:49:34 +0000847 u64 nsecs;
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000848
849 do {
850 seq = read_seqcount_begin(&tk_core.seq);
Peter Zijlstra4a4ad802015-03-19 09:28:44 +0100851 base = tk->tkr_raw.base;
852 nsecs = timekeeping_get_ns(&tk->tkr_raw);
Thomas Gleixnerf519b1a2014-07-16 21:05:04 +0000853
854 } while (read_seqcount_retry(&tk_core.seq, seq));
855
856 return ktime_add_ns(base, nsecs);
857}
858EXPORT_SYMBOL_GPL(ktime_get_raw);
859
860/**
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000861 * ktime_get_ts64 - get the monotonic clock in timespec64 format
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200862 * @ts: pointer to timespec variable
863 *
864 * The function calculates the monotonic clock from the realtime
865 * clock and the wall_to_monotonic offset and stores the result
John Stultz5322e4c2014-11-07 13:13:04 -0800866 * in normalized timespec64 format in the variable pointed to by @ts.
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200867 */
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000868void ktime_get_ts64(struct timespec64 *ts)
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200869{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000870 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000871 struct timespec64 tomono;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200872 unsigned int seq;
Thomas Gleixneracc89612016-12-08 20:49:34 +0000873 u64 nsec;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200874
875 WARN_ON(timekeeping_suspended);
876
877 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000878 seq = read_seqcount_begin(&tk_core.seq);
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000879 ts->tv_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100880 nsec = timekeeping_get_ns(&tk->tkr_mono);
John Stultz4e250fd2012-07-27 14:48:13 -0400881 tomono = tk->wall_to_monotonic;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200882
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +0000883 } while (read_seqcount_retry(&tk_core.seq, seq));
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200884
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000885 ts->tv_sec += tomono.tv_sec;
886 ts->tv_nsec = 0;
887 timespec64_add_ns(ts, nsec + tomono.tv_nsec);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200888}
Thomas Gleixnerd6d29892014-07-16 21:04:04 +0000889EXPORT_SYMBOL_GPL(ktime_get_ts64);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200890
Heena Sirwani9e3680b2014-10-29 16:01:16 +0530891/**
892 * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC
893 *
894 * Returns the seconds portion of CLOCK_MONOTONIC with a single non
895 * serialized read. tk->ktime_sec is of type 'unsigned long' so this
896 * works on both 32 and 64 bit systems. On 32 bit systems the readout
897 * covers ~136 years of uptime which should be enough to prevent
898 * premature wrap arounds.
899 */
900time64_t ktime_get_seconds(void)
901{
902 struct timekeeper *tk = &tk_core.timekeeper;
903
904 WARN_ON(timekeeping_suspended);
905 return tk->ktime_sec;
906}
907EXPORT_SYMBOL_GPL(ktime_get_seconds);
908
Heena Sirwanidbe7aa62014-10-29 16:01:50 +0530909/**
910 * ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME
911 *
912 * Returns the wall clock seconds since 1970. This replaces the
913 * get_seconds() interface which is not y2038 safe on 32bit systems.
914 *
915 * For 64bit systems the fast access to tk->xtime_sec is preserved. On
916 * 32bit systems the access must be protected with the sequence
917 * counter to provide "atomic" access to the 64bit tk->xtime_sec
918 * value.
919 */
920time64_t ktime_get_real_seconds(void)
921{
922 struct timekeeper *tk = &tk_core.timekeeper;
923 time64_t seconds;
924 unsigned int seq;
925
926 if (IS_ENABLED(CONFIG_64BIT))
927 return tk->xtime_sec;
928
929 do {
930 seq = read_seqcount_begin(&tk_core.seq);
931 seconds = tk->xtime_sec;
932
933 } while (read_seqcount_retry(&tk_core.seq, seq));
934
935 return seconds;
936}
937EXPORT_SYMBOL_GPL(ktime_get_real_seconds);
938
DengChaodee36652015-12-13 12:24:18 +0800939/**
940 * __ktime_get_real_seconds - The same as ktime_get_real_seconds
941 * but without the sequence counter protect. This internal function
942 * is called just when timekeeping lock is already held.
943 */
944time64_t __ktime_get_real_seconds(void)
945{
946 struct timekeeper *tk = &tk_core.timekeeper;
947
948 return tk->xtime_sec;
949}
950
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800951/**
952 * ktime_get_snapshot - snapshots the realtime/monotonic raw clocks with counter
953 * @systime_snapshot: pointer to struct receiving the system time snapshot
954 */
955void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot)
956{
957 struct timekeeper *tk = &tk_core.timekeeper;
958 unsigned long seq;
959 ktime_t base_raw;
960 ktime_t base_real;
Thomas Gleixneracc89612016-12-08 20:49:34 +0000961 u64 nsec_raw;
962 u64 nsec_real;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100963 u64 now;
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800964
Christopher S. Hallba266212016-02-22 03:15:21 -0800965 WARN_ON_ONCE(timekeeping_suspended);
966
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800967 do {
968 seq = read_seqcount_begin(&tk_core.seq);
John Stultzceea5e32017-06-08 16:44:20 -0700969 now = tk_clock_read(&tk->tkr_mono);
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800970 systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq;
971 systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq;
Christopher S. Hall9da0f492016-02-22 03:15:20 -0800972 base_real = ktime_add(tk->tkr_mono.base,
973 tk_core.timekeeper.offs_real);
974 base_raw = tk->tkr_raw.base;
975 nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, now);
976 nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw, now);
977 } while (read_seqcount_retry(&tk_core.seq, seq));
978
979 systime_snapshot->cycles = now;
980 systime_snapshot->real = ktime_add_ns(base_real, nsec_real);
981 systime_snapshot->raw = ktime_add_ns(base_raw, nsec_raw);
982}
983EXPORT_SYMBOL_GPL(ktime_get_snapshot);
DengChaodee36652015-12-13 12:24:18 +0800984
Christopher S. Hall2c756fe2016-02-22 03:15:23 -0800985/* Scale base by mult/div checking for overflow */
986static int scale64_check_overflow(u64 mult, u64 div, u64 *base)
987{
988 u64 tmp, rem;
989
990 tmp = div64_u64_rem(*base, div, &rem);
991
992 if (((int)sizeof(u64)*8 - fls64(mult) < fls64(tmp)) ||
993 ((int)sizeof(u64)*8 - fls64(mult) < fls64(rem)))
994 return -EOVERFLOW;
995 tmp *= mult;
996 rem *= mult;
997
998 do_div(rem, div);
999 *base = tmp + rem;
1000 return 0;
1001}
1002
1003/**
1004 * adjust_historical_crosststamp - adjust crosstimestamp previous to current interval
1005 * @history: Snapshot representing start of history
1006 * @partial_history_cycles: Cycle offset into history (fractional part)
1007 * @total_history_cycles: Total history length in cycles
1008 * @discontinuity: True indicates clock was set on history period
1009 * @ts: Cross timestamp that should be adjusted using
1010 * partial/total ratio
1011 *
1012 * Helper function used by get_device_system_crosststamp() to correct the
1013 * crosstimestamp corresponding to the start of the current interval to the
1014 * system counter value (timestamp point) provided by the driver. The
1015 * total_history_* quantities are the total history starting at the provided
1016 * reference point and ending at the start of the current interval. The cycle
1017 * count between the driver timestamp point and the start of the current
1018 * interval is partial_history_cycles.
1019 */
1020static int adjust_historical_crosststamp(struct system_time_snapshot *history,
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001021 u64 partial_history_cycles,
1022 u64 total_history_cycles,
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001023 bool discontinuity,
1024 struct system_device_crosststamp *ts)
1025{
1026 struct timekeeper *tk = &tk_core.timekeeper;
1027 u64 corr_raw, corr_real;
1028 bool interp_forward;
1029 int ret;
1030
1031 if (total_history_cycles == 0 || partial_history_cycles == 0)
1032 return 0;
1033
1034 /* Interpolate shortest distance from beginning or end of history */
Nicholas Mc Guire5fc63f92017-03-24 20:03:35 +01001035 interp_forward = partial_history_cycles > total_history_cycles / 2;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001036 partial_history_cycles = interp_forward ?
1037 total_history_cycles - partial_history_cycles :
1038 partial_history_cycles;
1039
1040 /*
1041 * Scale the monotonic raw time delta by:
1042 * partial_history_cycles / total_history_cycles
1043 */
1044 corr_raw = (u64)ktime_to_ns(
1045 ktime_sub(ts->sys_monoraw, history->raw));
1046 ret = scale64_check_overflow(partial_history_cycles,
1047 total_history_cycles, &corr_raw);
1048 if (ret)
1049 return ret;
1050
1051 /*
1052 * If there is a discontinuity in the history, scale monotonic raw
1053 * correction by:
1054 * mult(real)/mult(raw) yielding the realtime correction
1055 * Otherwise, calculate the realtime correction similar to monotonic
1056 * raw calculation
1057 */
1058 if (discontinuity) {
1059 corr_real = mul_u64_u32_div
1060 (corr_raw, tk->tkr_mono.mult, tk->tkr_raw.mult);
1061 } else {
1062 corr_real = (u64)ktime_to_ns(
1063 ktime_sub(ts->sys_realtime, history->real));
1064 ret = scale64_check_overflow(partial_history_cycles,
1065 total_history_cycles, &corr_real);
1066 if (ret)
1067 return ret;
1068 }
1069
1070 /* Fixup monotonic raw and real time time values */
1071 if (interp_forward) {
1072 ts->sys_monoraw = ktime_add_ns(history->raw, corr_raw);
1073 ts->sys_realtime = ktime_add_ns(history->real, corr_real);
1074 } else {
1075 ts->sys_monoraw = ktime_sub_ns(ts->sys_monoraw, corr_raw);
1076 ts->sys_realtime = ktime_sub_ns(ts->sys_realtime, corr_real);
1077 }
1078
1079 return 0;
1080}
1081
1082/*
1083 * cycle_between - true if test occurs chronologically between before and after
1084 */
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001085static bool cycle_between(u64 before, u64 test, u64 after)
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001086{
1087 if (test > before && test < after)
1088 return true;
1089 if (test < before && before > after)
1090 return true;
1091 return false;
1092}
1093
john stultz85240702007-05-08 00:27:59 -07001094/**
Christopher S. Hall8006c242016-02-22 03:15:22 -08001095 * get_device_system_crosststamp - Synchronously capture system/device timestamp
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001096 * @get_time_fn: Callback to get simultaneous device time and
Christopher S. Hall8006c242016-02-22 03:15:22 -08001097 * system counter from the device driver
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001098 * @ctx: Context passed to get_time_fn()
1099 * @history_begin: Historical reference point used to interpolate system
1100 * time when counter provided by the driver is before the current interval
Christopher S. Hall8006c242016-02-22 03:15:22 -08001101 * @xtstamp: Receives simultaneously captured system and device time
1102 *
1103 * Reads a timestamp from a device and correlates it to system time
1104 */
1105int get_device_system_crosststamp(int (*get_time_fn)
1106 (ktime_t *device_time,
1107 struct system_counterval_t *sys_counterval,
1108 void *ctx),
1109 void *ctx,
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001110 struct system_time_snapshot *history_begin,
Christopher S. Hall8006c242016-02-22 03:15:22 -08001111 struct system_device_crosststamp *xtstamp)
1112{
1113 struct system_counterval_t system_counterval;
1114 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001115 u64 cycles, now, interval_start;
Ingo Molnar64362572016-03-08 11:09:53 +01001116 unsigned int clock_was_set_seq = 0;
Christopher S. Hall8006c242016-02-22 03:15:22 -08001117 ktime_t base_real, base_raw;
Thomas Gleixneracc89612016-12-08 20:49:34 +00001118 u64 nsec_real, nsec_raw;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001119 u8 cs_was_changed_seq;
Christopher S. Hall8006c242016-02-22 03:15:22 -08001120 unsigned long seq;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001121 bool do_interp;
Christopher S. Hall8006c242016-02-22 03:15:22 -08001122 int ret;
1123
1124 do {
1125 seq = read_seqcount_begin(&tk_core.seq);
1126 /*
1127 * Try to synchronously capture device time and a system
1128 * counter value calling back into the device driver
1129 */
1130 ret = get_time_fn(&xtstamp->device, &system_counterval, ctx);
1131 if (ret)
1132 return ret;
1133
1134 /*
1135 * Verify that the clocksource associated with the captured
1136 * system counter value is the same as the currently installed
1137 * timekeeper clocksource
1138 */
1139 if (tk->tkr_mono.clock != system_counterval.cs)
1140 return -ENODEV;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001141 cycles = system_counterval.cycles;
1142
1143 /*
1144 * Check whether the system counter value provided by the
1145 * device driver is on the current timekeeping interval.
1146 */
John Stultzceea5e32017-06-08 16:44:20 -07001147 now = tk_clock_read(&tk->tkr_mono);
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001148 interval_start = tk->tkr_mono.cycle_last;
1149 if (!cycle_between(interval_start, cycles, now)) {
1150 clock_was_set_seq = tk->clock_was_set_seq;
1151 cs_was_changed_seq = tk->cs_was_changed_seq;
1152 cycles = interval_start;
1153 do_interp = true;
1154 } else {
1155 do_interp = false;
1156 }
Christopher S. Hall8006c242016-02-22 03:15:22 -08001157
1158 base_real = ktime_add(tk->tkr_mono.base,
1159 tk_core.timekeeper.offs_real);
1160 base_raw = tk->tkr_raw.base;
1161
1162 nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono,
1163 system_counterval.cycles);
1164 nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw,
1165 system_counterval.cycles);
1166 } while (read_seqcount_retry(&tk_core.seq, seq));
1167
1168 xtstamp->sys_realtime = ktime_add_ns(base_real, nsec_real);
1169 xtstamp->sys_monoraw = ktime_add_ns(base_raw, nsec_raw);
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001170
1171 /*
1172 * Interpolate if necessary, adjusting back from the start of the
1173 * current interval
1174 */
1175 if (do_interp) {
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001176 u64 partial_history_cycles, total_history_cycles;
Christopher S. Hall2c756fe2016-02-22 03:15:23 -08001177 bool discontinuity;
1178
1179 /*
1180 * Check that the counter value occurs after the provided
1181 * history reference and that the history doesn't cross a
1182 * clocksource change
1183 */
1184 if (!history_begin ||
1185 !cycle_between(history_begin->cycles,
1186 system_counterval.cycles, cycles) ||
1187 history_begin->cs_was_changed_seq != cs_was_changed_seq)
1188 return -EINVAL;
1189 partial_history_cycles = cycles - system_counterval.cycles;
1190 total_history_cycles = cycles - history_begin->cycles;
1191 discontinuity =
1192 history_begin->clock_was_set_seq != clock_was_set_seq;
1193
1194 ret = adjust_historical_crosststamp(history_begin,
1195 partial_history_cycles,
1196 total_history_cycles,
1197 discontinuity, xtstamp);
1198 if (ret)
1199 return ret;
1200 }
1201
Christopher S. Hall8006c242016-02-22 03:15:22 -08001202 return 0;
1203}
1204EXPORT_SYMBOL_GPL(get_device_system_crosststamp);
1205
1206/**
john stultz85240702007-05-08 00:27:59 -07001207 * do_gettimeofday - Returns the time of day in a timeval
1208 * @tv: pointer to the timeval to be set
1209 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +01001210 * NOTE: Users should be converted to using getnstimeofday()
john stultz85240702007-05-08 00:27:59 -07001211 */
1212void do_gettimeofday(struct timeval *tv)
1213{
Thomas Gleixnerd6d29892014-07-16 21:04:04 +00001214 struct timespec64 now;
john stultz85240702007-05-08 00:27:59 -07001215
Thomas Gleixnerd6d29892014-07-16 21:04:04 +00001216 getnstimeofday64(&now);
john stultz85240702007-05-08 00:27:59 -07001217 tv->tv_sec = now.tv_sec;
1218 tv->tv_usec = now.tv_nsec/1000;
1219}
john stultz85240702007-05-08 00:27:59 -07001220EXPORT_SYMBOL(do_gettimeofday);
Richard Cochrand239f492012-04-27 10:12:42 +02001221
john stultz85240702007-05-08 00:27:59 -07001222/**
pang.xunlei21f7eca2014-11-18 19:15:16 +08001223 * do_settimeofday64 - Sets the time of day.
1224 * @ts: pointer to the timespec64 variable containing the new time
john stultz85240702007-05-08 00:27:59 -07001225 *
1226 * Sets the time of day to the new time and update NTP and notify hrtimers
1227 */
pang.xunlei21f7eca2014-11-18 19:15:16 +08001228int do_settimeofday64(const struct timespec64 *ts)
john stultz85240702007-05-08 00:27:59 -07001229{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001230 struct timekeeper *tk = &tk_core.timekeeper;
pang.xunlei21f7eca2014-11-18 19:15:16 +08001231 struct timespec64 ts_delta, xt;
John Stultz92c1d3e2011-11-14 14:05:44 -08001232 unsigned long flags;
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001233 int ret = 0;
john stultz85240702007-05-08 00:27:59 -07001234
pang.xunlei21f7eca2014-11-18 19:15:16 +08001235 if (!timespec64_valid_strict(ts))
john stultz85240702007-05-08 00:27:59 -07001236 return -EINVAL;
1237
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001238 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001239 write_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -07001240
John Stultz4e250fd2012-07-27 14:48:13 -04001241 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -07001242
John Stultz4e250fd2012-07-27 14:48:13 -04001243 xt = tk_xtime(tk);
pang.xunlei21f7eca2014-11-18 19:15:16 +08001244 ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
1245 ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
John Stultz1e75fa82012-07-13 01:21:53 -04001246
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001247 if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) {
1248 ret = -EINVAL;
1249 goto out;
1250 }
1251
John Stultz7d489d12014-07-16 21:04:01 +00001252 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
john stultz85240702007-05-08 00:27:59 -07001253
pang.xunlei21f7eca2014-11-18 19:15:16 +08001254 tk_set_xtime(tk, ts);
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001255out:
David Vrabel780427f2013-06-27 11:35:46 +01001256 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
john stultz85240702007-05-08 00:27:59 -07001257
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001258 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001259 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001260
1261 /* signal hrtimers about time change */
1262 clock_was_set();
1263
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001264 return ret;
john stultz85240702007-05-08 00:27:59 -07001265}
pang.xunlei21f7eca2014-11-18 19:15:16 +08001266EXPORT_SYMBOL(do_settimeofday64);
john stultz85240702007-05-08 00:27:59 -07001267
John Stultzc528f7c2011-02-01 13:52:17 +00001268/**
1269 * timekeeping_inject_offset - Adds or subtracts from the current time.
1270 * @tv: pointer to the timespec variable containing the offset
1271 *
1272 * Adds or subtracts an offset value from the current time.
1273 */
1274int timekeeping_inject_offset(struct timespec *ts)
1275{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001276 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -08001277 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001278 struct timespec64 ts64, tmp;
John Stultz4e8b1452012-08-08 15:36:20 -04001279 int ret = 0;
John Stultzc528f7c2011-02-01 13:52:17 +00001280
John Stultz37cf4dc2015-12-03 22:09:31 -05001281 if (!timespec_inject_offset_valid(ts))
John Stultzc528f7c2011-02-01 13:52:17 +00001282 return -EINVAL;
1283
John Stultz7d489d12014-07-16 21:04:01 +00001284 ts64 = timespec_to_timespec64(*ts);
1285
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001286 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001287 write_seqcount_begin(&tk_core.seq);
John Stultzc528f7c2011-02-01 13:52:17 +00001288
John Stultz4e250fd2012-07-27 14:48:13 -04001289 timekeeping_forward_now(tk);
John Stultzc528f7c2011-02-01 13:52:17 +00001290
John Stultz4e8b1452012-08-08 15:36:20 -04001291 /* Make sure the proposed value is valid */
John Stultz7d489d12014-07-16 21:04:01 +00001292 tmp = timespec64_add(tk_xtime(tk), ts64);
Wang YanQinge1d7ba82015-06-23 18:38:54 +08001293 if (timespec64_compare(&tk->wall_to_monotonic, &ts64) > 0 ||
1294 !timespec64_valid_strict(&tmp)) {
John Stultz4e8b1452012-08-08 15:36:20 -04001295 ret = -EINVAL;
1296 goto error;
1297 }
John Stultz1e75fa82012-07-13 01:21:53 -04001298
John Stultz7d489d12014-07-16 21:04:01 +00001299 tk_xtime_add(tk, &ts64);
1300 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts64));
John Stultzc528f7c2011-02-01 13:52:17 +00001301
John Stultz4e8b1452012-08-08 15:36:20 -04001302error: /* even if we error out, we forwarded the time, so call update */
David Vrabel780427f2013-06-27 11:35:46 +01001303 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultzc528f7c2011-02-01 13:52:17 +00001304
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001305 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001306 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzc528f7c2011-02-01 13:52:17 +00001307
1308 /* signal hrtimers about time change */
1309 clock_was_set();
1310
John Stultz4e8b1452012-08-08 15:36:20 -04001311 return ret;
John Stultzc528f7c2011-02-01 13:52:17 +00001312}
1313EXPORT_SYMBOL(timekeeping_inject_offset);
1314
John Stultzcc244dd2012-05-03 12:30:07 -07001315/**
Stephen Boyd40d9f822016-12-07 14:33:23 -08001316 * __timekeeping_set_tai_offset - Sets the TAI offset from UTC and monotonic
John Stultzcc244dd2012-05-03 12:30:07 -07001317 *
1318 */
Fengguang Wudd5d70e82013-03-25 12:24:24 -07001319static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
John Stultzcc244dd2012-05-03 12:30:07 -07001320{
1321 tk->tai_offset = tai_offset;
John Stultz04005f62013-12-10 17:13:35 -08001322 tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
John Stultzcc244dd2012-05-03 12:30:07 -07001323}
1324
1325/**
john stultz85240702007-05-08 00:27:59 -07001326 * change_clocksource - Swaps clocksources if a new one is available
1327 *
1328 * Accumulates current time interval and initializes new clocksource
1329 */
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001330static int change_clocksource(void *data)
john stultz85240702007-05-08 00:27:59 -07001331{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001332 struct timekeeper *tk = &tk_core.timekeeper;
Magnus Damm4614e6a2009-04-21 12:24:02 -07001333 struct clocksource *new, *old;
John Stultzf695cf92012-03-14 16:38:15 -07001334 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -07001335
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001336 new = (struct clocksource *) data;
john stultz85240702007-05-08 00:27:59 -07001337
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001338 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001339 write_seqcount_begin(&tk_core.seq);
John Stultzf695cf92012-03-14 16:38:15 -07001340
John Stultz4e250fd2012-07-27 14:48:13 -04001341 timekeeping_forward_now(tk);
Thomas Gleixner09ac3692013-04-25 20:31:44 +00001342 /*
1343 * If the cs is in module, get a module reference. Succeeds
1344 * for built-in code (owner == NULL) as well.
1345 */
1346 if (try_module_get(new->owner)) {
1347 if (!new->enable || new->enable(new) == 0) {
Peter Zijlstra876e7882015-03-19 10:09:06 +01001348 old = tk->tkr_mono.clock;
Thomas Gleixner09ac3692013-04-25 20:31:44 +00001349 tk_setup_internals(tk, new);
1350 if (old->disable)
1351 old->disable(old);
1352 module_put(old->owner);
1353 } else {
1354 module_put(new->owner);
1355 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001356 }
David Vrabel780427f2013-06-27 11:35:46 +01001357 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultzf695cf92012-03-14 16:38:15 -07001358
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001359 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001360 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzf695cf92012-03-14 16:38:15 -07001361
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001362 return 0;
1363}
john stultz85240702007-05-08 00:27:59 -07001364
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001365/**
1366 * timekeeping_notify - Install a new clock source
1367 * @clock: pointer to the clock source
1368 *
1369 * This function is called from clocksource.c after a new, better clock
1370 * source has been registered. The caller holds the clocksource_mutex.
1371 */
Thomas Gleixnerba919d12013-04-25 20:31:44 +00001372int timekeeping_notify(struct clocksource *clock)
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001373{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001374 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04001375
Peter Zijlstra876e7882015-03-19 10:09:06 +01001376 if (tk->tkr_mono.clock == clock)
Thomas Gleixnerba919d12013-04-25 20:31:44 +00001377 return 0;
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001378 stop_machine(change_clocksource, clock, NULL);
john stultz85240702007-05-08 00:27:59 -07001379 tick_clock_notify();
Peter Zijlstra876e7882015-03-19 10:09:06 +01001380 return tk->tkr_mono.clock == clock ? 0 : -1;
john stultz85240702007-05-08 00:27:59 -07001381}
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001382
Thomas Gleixnera40f2622009-07-07 13:00:31 +02001383/**
John Stultzcdba2ec2014-11-07 11:03:20 -08001384 * getrawmonotonic64 - Returns the raw monotonic time in a timespec
1385 * @ts: pointer to the timespec64 to be set
John Stultz2d422442008-08-20 16:37:30 -07001386 *
1387 * Returns the raw monotonic time (completely un-modified by ntp)
1388 */
John Stultzcdba2ec2014-11-07 11:03:20 -08001389void getrawmonotonic64(struct timespec64 *ts)
John Stultz2d422442008-08-20 16:37:30 -07001390{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001391 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz2d422442008-08-20 16:37:30 -07001392 unsigned long seq;
Thomas Gleixneracc89612016-12-08 20:49:34 +00001393 u64 nsecs;
John Stultz2d422442008-08-20 16:37:30 -07001394
1395 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001396 seq = read_seqcount_begin(&tk_core.seq);
John Stultzfc6eead72017-05-22 17:20:20 -07001397 ts->tv_sec = tk->raw_sec;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01001398 nsecs = timekeeping_get_ns(&tk->tkr_raw);
John Stultz2d422442008-08-20 16:37:30 -07001399
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001400 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz2d422442008-08-20 16:37:30 -07001401
John Stultzfc6eead72017-05-22 17:20:20 -07001402 ts->tv_nsec = 0;
1403 timespec64_add_ns(ts, nsecs);
John Stultz2d422442008-08-20 16:37:30 -07001404}
John Stultzcdba2ec2014-11-07 11:03:20 -08001405EXPORT_SYMBOL(getrawmonotonic64);
1406
John Stultz2d422442008-08-20 16:37:30 -07001407
John Stultz2d422442008-08-20 16:37:30 -07001408/**
Li Zefancf4fc6c2008-02-08 04:19:24 -08001409 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -07001410 */
Li Zefancf4fc6c2008-02-08 04:19:24 -08001411int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -07001412{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001413 struct timekeeper *tk = &tk_core.timekeeper;
john stultz85240702007-05-08 00:27:59 -07001414 unsigned long seq;
1415 int ret;
1416
1417 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001418 seq = read_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -07001419
Peter Zijlstra876e7882015-03-19 10:09:06 +01001420 ret = tk->tkr_mono.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -07001421
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001422 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz85240702007-05-08 00:27:59 -07001423
1424 return ret;
1425}
1426
1427/**
Jon Hunter98962462009-08-18 12:45:10 -05001428 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
Jon Hunter98962462009-08-18 12:45:10 -05001429 */
1430u64 timekeeping_max_deferment(void)
1431{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001432 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz70471f22011-11-14 12:48:10 -08001433 unsigned long seq;
1434 u64 ret;
John Stultz42e71e82012-07-13 01:21:51 -04001435
John Stultz70471f22011-11-14 12:48:10 -08001436 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001437 seq = read_seqcount_begin(&tk_core.seq);
John Stultz70471f22011-11-14 12:48:10 -08001438
Peter Zijlstra876e7882015-03-19 10:09:06 +01001439 ret = tk->tkr_mono.clock->max_idle_ns;
John Stultz70471f22011-11-14 12:48:10 -08001440
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001441 } while (read_seqcount_retry(&tk_core.seq, seq));
John Stultz70471f22011-11-14 12:48:10 -08001442
1443 return ret;
Jon Hunter98962462009-08-18 12:45:10 -05001444}
1445
1446/**
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001447 * read_persistent_clock - Return time from the persistent clock.
john stultz85240702007-05-08 00:27:59 -07001448 *
1449 * Weak dummy function for arches that do not yet support it.
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001450 * Reads the time from the battery backed persistent clock.
1451 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
john stultz85240702007-05-08 00:27:59 -07001452 *
1453 * XXX - Do be sure to remove it once all arches implement it.
1454 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07001455void __weak read_persistent_clock(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -07001456{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001457 ts->tv_sec = 0;
1458 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -07001459}
1460
Xunlei Pang2ee96632015-04-01 20:34:22 -07001461void __weak read_persistent_clock64(struct timespec64 *ts64)
1462{
1463 struct timespec ts;
1464
1465 read_persistent_clock(&ts);
1466 *ts64 = timespec_to_timespec64(ts);
1467}
1468
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001469/**
Xunlei Pange83d0a42015-04-09 09:04:42 +08001470 * read_boot_clock64 - Return time of the system start.
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001471 *
1472 * Weak dummy function for arches that do not yet support it.
1473 * Function to read the exact time the system has been started.
Xunlei Pange83d0a42015-04-09 09:04:42 +08001474 * Returns a timespec64 with tv_sec=0 and tv_nsec=0 if unsupported.
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001475 *
1476 * XXX - Do be sure to remove it once all arches implement it.
1477 */
Xunlei Pange83d0a42015-04-09 09:04:42 +08001478void __weak read_boot_clock64(struct timespec64 *ts)
Martin Schwidefsky23970e32009-08-14 15:47:32 +02001479{
1480 ts->tv_sec = 0;
1481 ts->tv_nsec = 0;
1482}
1483
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001484/* Flag for if timekeeping_resume() has injected sleeptime */
1485static bool sleeptime_injected;
1486
1487/* Flag for if there is a persistent clock on this platform */
1488static bool persistent_clock_exists;
1489
john stultz85240702007-05-08 00:27:59 -07001490/*
1491 * timekeeping_init - Initializes the clocksource and common timekeeping values
1492 */
1493void __init timekeeping_init(void)
1494{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001495 struct timekeeper *tk = &tk_core.timekeeper;
Martin Schwidefsky155ec602009-08-14 15:47:26 +02001496 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -07001497 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001498 struct timespec64 now, boot, tmp;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001499
Xunlei Pang2ee96632015-04-01 20:34:22 -07001500 read_persistent_clock64(&now);
John Stultz7d489d12014-07-16 21:04:01 +00001501 if (!timespec64_valid_strict(&now)) {
John Stultz4e8b1452012-08-08 15:36:20 -04001502 pr_warn("WARNING: Persistent clock returned invalid value!\n"
1503 " Check your CMOS/BIOS settings.\n");
1504 now.tv_sec = 0;
1505 now.tv_nsec = 0;
Feng Tang31ade302013-01-16 00:09:47 +08001506 } else if (now.tv_sec || now.tv_nsec)
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001507 persistent_clock_exists = true;
John Stultz4e8b1452012-08-08 15:36:20 -04001508
Xunlei Pang9a806dd2015-04-01 20:34:21 -07001509 read_boot_clock64(&boot);
John Stultz7d489d12014-07-16 21:04:01 +00001510 if (!timespec64_valid_strict(&boot)) {
John Stultz4e8b1452012-08-08 15:36:20 -04001511 pr_warn("WARNING: Boot clock returned invalid value!\n"
1512 " Check your CMOS/BIOS settings.\n");
1513 boot.tv_sec = 0;
1514 boot.tv_nsec = 0;
1515 }
john stultz85240702007-05-08 00:27:59 -07001516
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001517 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001518 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07001519 ntp_init();
1520
Martin Schwidefskyf1b82742009-08-14 15:47:21 +02001521 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +02001522 if (clock->enable)
1523 clock->enable(clock);
John Stultz4e250fd2012-07-27 14:48:13 -04001524 tk_setup_internals(tk, clock);
john stultz85240702007-05-08 00:27:59 -07001525
John Stultz4e250fd2012-07-27 14:48:13 -04001526 tk_set_xtime(tk, &now);
John Stultzfc6eead72017-05-22 17:20:20 -07001527 tk->raw_sec = 0;
John Stultz1e75fa82012-07-13 01:21:53 -04001528 if (boot.tv_sec == 0 && boot.tv_nsec == 0)
John Stultz4e250fd2012-07-27 14:48:13 -04001529 boot = tk_xtime(tk);
John Stultz1e75fa82012-07-13 01:21:53 -04001530
John Stultz7d489d12014-07-16 21:04:01 +00001531 set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec);
John Stultz4e250fd2012-07-27 14:48:13 -04001532 tk_set_wall_to_mono(tk, tmp);
John Stultz6d0ef902012-07-27 14:48:12 -04001533
Thomas Gleixner56fd16c2015-10-16 15:50:22 +02001534 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner48cdc132013-02-21 22:51:40 +00001535
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001536 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001537 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001538}
1539
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001540/* time in seconds when suspend began for persistent clock */
John Stultz7d489d12014-07-16 21:04:01 +00001541static struct timespec64 timekeeping_suspend_time;
john stultz85240702007-05-08 00:27:59 -07001542
1543/**
John Stultz304529b2011-04-01 14:32:09 -07001544 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
1545 * @delta: pointer to a timespec delta value
1546 *
1547 * Takes a timespec offset measuring a suspend interval and properly
1548 * adds the sleep offset to the timekeeping variables.
1549 */
John Stultzf726a692012-07-13 01:21:57 -04001550static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
John Stultz7d489d12014-07-16 21:04:01 +00001551 struct timespec64 *delta)
John Stultz304529b2011-04-01 14:32:09 -07001552{
John Stultz7d489d12014-07-16 21:04:01 +00001553 if (!timespec64_valid_strict(delta)) {
John Stultz6d9bcb62014-06-04 16:11:43 -07001554 printk_deferred(KERN_WARNING
1555 "__timekeeping_inject_sleeptime: Invalid "
1556 "sleep delta value!\n");
John Stultzcb5de2f8d2011-06-01 18:18:09 -07001557 return;
1558 }
John Stultzf726a692012-07-13 01:21:57 -04001559 tk_xtime_add(tk, delta);
John Stultz7d489d12014-07-16 21:04:01 +00001560 tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
Thomas Gleixner47da70d2014-07-16 21:05:00 +00001561 tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
Colin Cross5c835452013-05-21 22:32:14 -07001562 tk_debug_account_sleep_time(delta);
John Stultz304529b2011-04-01 14:32:09 -07001563}
1564
Xunlei Pang7f298132015-04-01 20:34:35 -07001565#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE)
John Stultz304529b2011-04-01 14:32:09 -07001566/**
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001567 * We have three kinds of time sources to use for sleep time
1568 * injection, the preference order is:
1569 * 1) non-stop clocksource
1570 * 2) persistent clock (ie: RTC accessible when irqs are off)
1571 * 3) RTC
1572 *
1573 * 1) and 2) are used by timekeeping, 3) by RTC subsystem.
1574 * If system has neither 1) nor 2), 3) will be used finally.
1575 *
1576 *
1577 * If timekeeping has injected sleeptime via either 1) or 2),
1578 * 3) becomes needless, so in this case we don't need to call
1579 * rtc_resume(), and this is what timekeeping_rtc_skipresume()
1580 * means.
1581 */
1582bool timekeeping_rtc_skipresume(void)
1583{
1584 return sleeptime_injected;
1585}
1586
1587/**
1588 * 1) can be determined whether to use or not only when doing
1589 * timekeeping_resume() which is invoked after rtc_suspend(),
1590 * so we can't skip rtc_suspend() surely if system has 1).
1591 *
1592 * But if system has 2), 2) will definitely be used, so in this
1593 * case we don't need to call rtc_suspend(), and this is what
1594 * timekeeping_rtc_skipsuspend() means.
1595 */
1596bool timekeeping_rtc_skipsuspend(void)
1597{
1598 return persistent_clock_exists;
1599}
1600
1601/**
pang.xunlei04d90892014-11-18 19:15:17 +08001602 * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values
1603 * @delta: pointer to a timespec64 delta value
John Stultz304529b2011-04-01 14:32:09 -07001604 *
Xunlei Pang2ee96632015-04-01 20:34:22 -07001605 * This hook is for architectures that cannot support read_persistent_clock64
John Stultz304529b2011-04-01 14:32:09 -07001606 * because their RTC/persistent clock is only accessible when irqs are enabled.
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001607 * and also don't have an effective nonstop clocksource.
John Stultz304529b2011-04-01 14:32:09 -07001608 *
1609 * This function should only be called by rtc_resume(), and allows
1610 * a suspend offset to be injected into the timekeeping values.
1611 */
pang.xunlei04d90892014-11-18 19:15:17 +08001612void timekeeping_inject_sleeptime64(struct timespec64 *delta)
John Stultz304529b2011-04-01 14:32:09 -07001613{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001614 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -08001615 unsigned long flags;
John Stultz304529b2011-04-01 14:32:09 -07001616
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001617 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001618 write_seqcount_begin(&tk_core.seq);
John Stultz70471f22011-11-14 12:48:10 -08001619
John Stultz4e250fd2012-07-27 14:48:13 -04001620 timekeeping_forward_now(tk);
John Stultz304529b2011-04-01 14:32:09 -07001621
pang.xunlei04d90892014-11-18 19:15:17 +08001622 __timekeeping_inject_sleeptime(tk, delta);
John Stultz304529b2011-04-01 14:32:09 -07001623
David Vrabel780427f2013-06-27 11:35:46 +01001624 timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultz304529b2011-04-01 14:32:09 -07001625
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001626 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001627 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz304529b2011-04-01 14:32:09 -07001628
1629 /* signal hrtimers about time change */
1630 clock_was_set();
1631}
Xunlei Pang7f298132015-04-01 20:34:35 -07001632#endif
John Stultz304529b2011-04-01 14:32:09 -07001633
John Stultz304529b2011-04-01 14:32:09 -07001634/**
john stultz85240702007-05-08 00:27:59 -07001635 * timekeeping_resume - Resumes the generic timekeeping subsystem.
john stultz85240702007-05-08 00:27:59 -07001636 */
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +01001637void timekeeping_resume(void)
john stultz85240702007-05-08 00:27:59 -07001638{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001639 struct timekeeper *tk = &tk_core.timekeeper;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001640 struct clocksource *clock = tk->tkr_mono.clock;
John Stultz92c1d3e2011-11-14 14:05:44 -08001641 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001642 struct timespec64 ts_new, ts_delta;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01001643 u64 cycle_now;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02001644
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001645 sleeptime_injected = false;
Xunlei Pang2ee96632015-04-01 20:34:22 -07001646 read_persistent_clock64(&ts_new);
john stultz85240702007-05-08 00:27:59 -07001647
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +02001648 clockevents_resume();
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +02001649 clocksource_resume();
1650
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001651 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001652 write_seqcount_begin(&tk_core.seq);
john stultz85240702007-05-08 00:27:59 -07001653
Feng Tange445cf12013-03-12 11:56:48 +08001654 /*
1655 * After system resumes, we need to calculate the suspended time and
1656 * compensate it for the OS time. There are 3 sources that could be
1657 * used: Nonstop clocksource during suspend, persistent clock and rtc
1658 * device.
1659 *
1660 * One specific platform may have 1 or 2 or all of them, and the
1661 * preference will be:
1662 * suspend-nonstop clocksource -> persistent clock -> rtc
1663 * The less preferred source will only be tried if there is no better
1664 * usable source. The rtc part is handled separately in rtc core code.
1665 */
John Stultzceea5e32017-06-08 16:44:20 -07001666 cycle_now = tk_clock_read(&tk->tkr_mono);
Feng Tange445cf12013-03-12 11:56:48 +08001667 if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
Peter Zijlstra876e7882015-03-19 10:09:06 +01001668 cycle_now > tk->tkr_mono.cycle_last) {
Thomas Gleixnerc029a2b2016-12-08 20:49:38 +00001669 u64 nsec, cyc_delta;
Feng Tange445cf12013-03-12 11:56:48 +08001670
Thomas Gleixnerc029a2b2016-12-08 20:49:38 +00001671 cyc_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last,
1672 tk->tkr_mono.mask);
1673 nsec = mul_u64_u32_shr(cyc_delta, clock->mult, clock->shift);
John Stultz7d489d12014-07-16 21:04:01 +00001674 ts_delta = ns_to_timespec64(nsec);
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001675 sleeptime_injected = true;
John Stultz7d489d12014-07-16 21:04:01 +00001676 } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
1677 ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001678 sleeptime_injected = true;
john stultz85240702007-05-08 00:27:59 -07001679 }
Feng Tange445cf12013-03-12 11:56:48 +08001680
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001681 if (sleeptime_injected)
Feng Tange445cf12013-03-12 11:56:48 +08001682 __timekeeping_inject_sleeptime(tk, &ts_delta);
1683
1684 /* Re-base the last cycle value */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001685 tk->tkr_mono.cycle_last = cycle_now;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01001686 tk->tkr_raw.cycle_last = cycle_now;
1687
John Stultz4e250fd2012-07-27 14:48:13 -04001688 tk->ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -07001689 timekeeping_suspended = 0;
David Vrabel780427f2013-06-27 11:35:46 +01001690 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001691 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001692 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001693
1694 touch_softlockup_watchdog();
1695
Thomas Gleixner4ffee522015-03-25 13:09:16 +01001696 tick_resume();
Thomas Gleixnerb12a03c2011-05-02 16:48:57 +02001697 hrtimers_resume();
john stultz85240702007-05-08 00:27:59 -07001698}
1699
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +01001700int timekeeping_suspend(void)
john stultz85240702007-05-08 00:27:59 -07001701{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001702 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz92c1d3e2011-11-14 14:05:44 -08001703 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00001704 struct timespec64 delta, delta_delta;
1705 static struct timespec64 old_delta;
john stultz85240702007-05-08 00:27:59 -07001706
Xunlei Pang2ee96632015-04-01 20:34:22 -07001707 read_persistent_clock64(&timekeeping_suspend_time);
Thomas Gleixner3be90952007-09-16 15:36:43 +02001708
Zoran Markovic0d6bd992013-05-17 11:24:05 -07001709 /*
1710 * On some systems the persistent_clock can not be detected at
1711 * timekeeping_init by its return value, so if we see a valid
1712 * value returned, update the persistent_clock_exists flag.
1713 */
1714 if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001715 persistent_clock_exists = true;
Zoran Markovic0d6bd992013-05-17 11:24:05 -07001716
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001717 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001718 write_seqcount_begin(&tk_core.seq);
John Stultz4e250fd2012-07-27 14:48:13 -04001719 timekeeping_forward_now(tk);
john stultz85240702007-05-08 00:27:59 -07001720 timekeeping_suspended = 1;
John Stultzcb332172011-05-31 22:53:23 -07001721
Xunlei Pang0fa88cb2015-04-01 20:34:38 -07001722 if (persistent_clock_exists) {
John Stultzcb332172011-05-31 22:53:23 -07001723 /*
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001724 * To avoid drift caused by repeated suspend/resumes,
1725 * which each can add ~1 second drift error,
1726 * try to compensate so the difference in system time
1727 * and persistent_clock time stays close to constant.
John Stultzcb332172011-05-31 22:53:23 -07001728 */
Xunlei Pang264bb3f2015-04-01 20:34:37 -07001729 delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time);
1730 delta_delta = timespec64_sub(delta, old_delta);
1731 if (abs(delta_delta.tv_sec) >= 2) {
1732 /*
1733 * if delta_delta is too large, assume time correction
1734 * has occurred and set old_delta to the current delta.
1735 */
1736 old_delta = delta;
1737 } else {
1738 /* Otherwise try to adjust old_system to compensate */
1739 timekeeping_suspend_time =
1740 timespec64_add(timekeeping_suspend_time, delta_delta);
1741 }
John Stultzcb332172011-05-31 22:53:23 -07001742 }
John Stultz330a1612013-12-11 19:10:36 -08001743
1744 timekeeping_update(tk, TK_MIRROR);
Rafael J. Wysocki060407a2015-02-13 14:49:02 +01001745 halt_fast_timekeeper(tk);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00001746 write_seqcount_end(&tk_core.seq);
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00001747 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07001748
Thomas Gleixner4ffee522015-03-25 13:09:16 +01001749 tick_suspend();
Magnus Dammc54a42b2010-02-02 14:41:41 -08001750 clocksource_suspend();
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +02001751 clockevents_suspend();
john stultz85240702007-05-08 00:27:59 -07001752
1753 return 0;
1754}
1755
1756/* sysfs resume/suspend bits for timekeeping */
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001757static struct syscore_ops timekeeping_syscore_ops = {
john stultz85240702007-05-08 00:27:59 -07001758 .resume = timekeeping_resume,
1759 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -07001760};
1761
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001762static int __init timekeeping_init_ops(void)
john stultz85240702007-05-08 00:27:59 -07001763{
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001764 register_syscore_ops(&timekeeping_syscore_ops);
1765 return 0;
john stultz85240702007-05-08 00:27:59 -07001766}
Rafael J. Wysockie1a85b22011-03-23 22:16:04 +01001767device_initcall(timekeeping_init_ops);
john stultz85240702007-05-08 00:27:59 -07001768
1769/*
John Stultzdc491592013-12-06 17:25:21 -08001770 * Apply a multiplier adjustment to the timekeeper
john stultz85240702007-05-08 00:27:59 -07001771 */
John Stultzdc491592013-12-06 17:25:21 -08001772static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
1773 s64 offset,
1774 bool negative,
1775 int adj_scale)
john stultz85240702007-05-08 00:27:59 -07001776{
John Stultzdc491592013-12-06 17:25:21 -08001777 s64 interval = tk->cycle_interval;
1778 s32 mult_adj = 1;
john stultz85240702007-05-08 00:27:59 -07001779
John Stultzdc491592013-12-06 17:25:21 -08001780 if (negative) {
1781 mult_adj = -mult_adj;
1782 interval = -interval;
1783 offset = -offset;
john stultz85240702007-05-08 00:27:59 -07001784 }
John Stultzdc491592013-12-06 17:25:21 -08001785 mult_adj <<= adj_scale;
1786 interval <<= adj_scale;
1787 offset <<= adj_scale;
john stultz85240702007-05-08 00:27:59 -07001788
John Stultzc2bc1112011-10-27 18:12:42 -07001789 /*
1790 * So the following can be confusing.
1791 *
John Stultzdc491592013-12-06 17:25:21 -08001792 * To keep things simple, lets assume mult_adj == 1 for now.
John Stultzc2bc1112011-10-27 18:12:42 -07001793 *
John Stultzdc491592013-12-06 17:25:21 -08001794 * When mult_adj != 1, remember that the interval and offset values
John Stultzc2bc1112011-10-27 18:12:42 -07001795 * have been appropriately scaled so the math is the same.
1796 *
1797 * The basic idea here is that we're increasing the multiplier
1798 * by one, this causes the xtime_interval to be incremented by
1799 * one cycle_interval. This is because:
1800 * xtime_interval = cycle_interval * mult
1801 * So if mult is being incremented by one:
1802 * xtime_interval = cycle_interval * (mult + 1)
1803 * Its the same as:
1804 * xtime_interval = (cycle_interval * mult) + cycle_interval
1805 * Which can be shortened to:
1806 * xtime_interval += cycle_interval
1807 *
1808 * So offset stores the non-accumulated cycles. Thus the current
1809 * time (in shifted nanoseconds) is:
1810 * now = (offset * adj) + xtime_nsec
1811 * Now, even though we're adjusting the clock frequency, we have
1812 * to keep time consistent. In other words, we can't jump back
1813 * in time, and we also want to avoid jumping forward in time.
1814 *
1815 * So given the same offset value, we need the time to be the same
1816 * both before and after the freq adjustment.
1817 * now = (offset * adj_1) + xtime_nsec_1
1818 * now = (offset * adj_2) + xtime_nsec_2
1819 * So:
1820 * (offset * adj_1) + xtime_nsec_1 =
1821 * (offset * adj_2) + xtime_nsec_2
1822 * And we know:
1823 * adj_2 = adj_1 + 1
1824 * So:
1825 * (offset * adj_1) + xtime_nsec_1 =
1826 * (offset * (adj_1+1)) + xtime_nsec_2
1827 * (offset * adj_1) + xtime_nsec_1 =
1828 * (offset * adj_1) + offset + xtime_nsec_2
1829 * Canceling the sides:
1830 * xtime_nsec_1 = offset + xtime_nsec_2
1831 * Which gives us:
1832 * xtime_nsec_2 = xtime_nsec_1 - offset
1833 * Which simplfies to:
1834 * xtime_nsec -= offset
1835 *
1836 * XXX - TODO: Doc ntp_error calculation.
1837 */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001838 if ((mult_adj > 0) && (tk->tkr_mono.mult + mult_adj < mult_adj)) {
pang.xunlei6067dc52014-10-08 15:03:34 +08001839 /* NTP adjustment caused clocksource mult overflow */
1840 WARN_ON_ONCE(1);
1841 return;
1842 }
1843
Peter Zijlstra876e7882015-03-19 10:09:06 +01001844 tk->tkr_mono.mult += mult_adj;
John Stultzf726a692012-07-13 01:21:57 -04001845 tk->xtime_interval += interval;
Peter Zijlstra876e7882015-03-19 10:09:06 +01001846 tk->tkr_mono.xtime_nsec -= offset;
John Stultzf726a692012-07-13 01:21:57 -04001847 tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
John Stultzdc491592013-12-06 17:25:21 -08001848}
John Stultz2a8c0882012-07-13 01:21:56 -04001849
John Stultzdc491592013-12-06 17:25:21 -08001850/*
1851 * Calculate the multiplier adjustment needed to match the frequency
1852 * specified by NTP
1853 */
1854static __always_inline void timekeeping_freqadjust(struct timekeeper *tk,
1855 s64 offset)
1856{
1857 s64 interval = tk->cycle_interval;
1858 s64 xinterval = tk->xtime_interval;
John Stultzec02b072015-12-03 10:23:30 -08001859 u32 base = tk->tkr_mono.clock->mult;
1860 u32 max = tk->tkr_mono.clock->maxadj;
1861 u32 cur_adj = tk->tkr_mono.mult;
John Stultzdc491592013-12-06 17:25:21 -08001862 s64 tick_error;
1863 bool negative;
John Stultzec02b072015-12-03 10:23:30 -08001864 u32 adj_scale;
John Stultzdc491592013-12-06 17:25:21 -08001865
1866 /* Remove any current error adj from freq calculation */
1867 if (tk->ntp_err_mult)
1868 xinterval -= tk->cycle_interval;
1869
John Stultz375f45b2014-04-23 20:53:29 -07001870 tk->ntp_tick = ntp_tick_length();
1871
John Stultzdc491592013-12-06 17:25:21 -08001872 /* Calculate current error per tick */
1873 tick_error = ntp_tick_length() >> tk->ntp_error_shift;
1874 tick_error -= (xinterval + tk->xtime_remainder);
1875
1876 /* Don't worry about correcting it if its small */
1877 if (likely((tick_error >= 0) && (tick_error <= interval)))
1878 return;
1879
1880 /* preserve the direction of correction */
1881 negative = (tick_error < 0);
1882
John Stultzec02b072015-12-03 10:23:30 -08001883 /* If any adjustment would pass the max, just return */
1884 if (negative && (cur_adj - 1) <= (base - max))
1885 return;
1886 if (!negative && (cur_adj + 1) >= (base + max))
1887 return;
1888 /*
1889 * Sort out the magnitude of the correction, but
1890 * avoid making so large a correction that we go
1891 * over the max adjustment.
1892 */
1893 adj_scale = 0;
Andrew Morton79211c82015-11-09 14:58:13 -08001894 tick_error = abs(tick_error);
John Stultzec02b072015-12-03 10:23:30 -08001895 while (tick_error > interval) {
1896 u32 adj = 1 << (adj_scale + 1);
1897
1898 /* Check if adjustment gets us within 1 unit from the max */
1899 if (negative && (cur_adj - adj) <= (base - max))
1900 break;
1901 if (!negative && (cur_adj + adj) >= (base + max))
1902 break;
1903
1904 adj_scale++;
John Stultzdc491592013-12-06 17:25:21 -08001905 tick_error >>= 1;
John Stultzec02b072015-12-03 10:23:30 -08001906 }
John Stultzdc491592013-12-06 17:25:21 -08001907
1908 /* scale the corrections */
John Stultzec02b072015-12-03 10:23:30 -08001909 timekeeping_apply_adjustment(tk, offset, negative, adj_scale);
John Stultzdc491592013-12-06 17:25:21 -08001910}
1911
1912/*
1913 * Adjust the timekeeper's multiplier to the correct frequency
1914 * and also to reduce the accumulated error value.
1915 */
1916static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
1917{
1918 /* Correct for the current frequency error */
1919 timekeeping_freqadjust(tk, offset);
1920
1921 /* Next make a small adjustment to fix any cumulative error */
1922 if (!tk->ntp_err_mult && (tk->ntp_error > 0)) {
1923 tk->ntp_err_mult = 1;
1924 timekeeping_apply_adjustment(tk, offset, 0, 0);
1925 } else if (tk->ntp_err_mult && (tk->ntp_error <= 0)) {
1926 /* Undo any existing error adjustment */
1927 timekeeping_apply_adjustment(tk, offset, 1, 0);
1928 tk->ntp_err_mult = 0;
1929 }
1930
Peter Zijlstra876e7882015-03-19 10:09:06 +01001931 if (unlikely(tk->tkr_mono.clock->maxadj &&
1932 (abs(tk->tkr_mono.mult - tk->tkr_mono.clock->mult)
1933 > tk->tkr_mono.clock->maxadj))) {
John Stultzdc491592013-12-06 17:25:21 -08001934 printk_once(KERN_WARNING
1935 "Adjusting %s more than 11%% (%ld vs %ld)\n",
Peter Zijlstra876e7882015-03-19 10:09:06 +01001936 tk->tkr_mono.clock->name, (long)tk->tkr_mono.mult,
1937 (long)tk->tkr_mono.clock->mult + tk->tkr_mono.clock->maxadj);
John Stultzdc491592013-12-06 17:25:21 -08001938 }
1939
John Stultz2a8c0882012-07-13 01:21:56 -04001940 /*
1941 * It may be possible that when we entered this function, xtime_nsec
1942 * was very small. Further, if we're slightly speeding the clocksource
1943 * in the code above, its possible the required corrective factor to
1944 * xtime_nsec could cause it to underflow.
1945 *
1946 * Now, since we already accumulated the second, cannot simply roll
1947 * the accumulated second back, since the NTP subsystem has been
1948 * notified via second_overflow. So instead we push xtime_nsec forward
1949 * by the amount we underflowed, and add that amount into the error.
1950 *
1951 * We'll correct this error next time through this function, when
1952 * xtime_nsec is not as small.
1953 */
Peter Zijlstra876e7882015-03-19 10:09:06 +01001954 if (unlikely((s64)tk->tkr_mono.xtime_nsec < 0)) {
1955 s64 neg = -(s64)tk->tkr_mono.xtime_nsec;
1956 tk->tkr_mono.xtime_nsec = 0;
John Stultzf726a692012-07-13 01:21:57 -04001957 tk->ntp_error += neg << tk->ntp_error_shift;
John Stultz2a8c0882012-07-13 01:21:56 -04001958 }
john stultz85240702007-05-08 00:27:59 -07001959}
1960
1961/**
John Stultz1f4f9482012-07-13 01:21:54 -04001962 * accumulate_nsecs_to_secs - Accumulates nsecs into secs
1963 *
Zhen Lei571af552015-08-25 14:42:53 +08001964 * Helper function that accumulates the nsecs greater than a second
John Stultz1f4f9482012-07-13 01:21:54 -04001965 * from the xtime_nsec field to the xtime_secs field.
1966 * It also calls into the NTP code to handle leapsecond processing.
1967 *
1968 */
David Vrabel780427f2013-06-27 11:35:46 +01001969static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
John Stultz1f4f9482012-07-13 01:21:54 -04001970{
Peter Zijlstra876e7882015-03-19 10:09:06 +01001971 u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
John Stultz5258d3f2013-12-11 20:07:49 -08001972 unsigned int clock_set = 0;
John Stultz1f4f9482012-07-13 01:21:54 -04001973
Peter Zijlstra876e7882015-03-19 10:09:06 +01001974 while (tk->tkr_mono.xtime_nsec >= nsecps) {
John Stultz1f4f9482012-07-13 01:21:54 -04001975 int leap;
1976
Peter Zijlstra876e7882015-03-19 10:09:06 +01001977 tk->tkr_mono.xtime_nsec -= nsecps;
John Stultz1f4f9482012-07-13 01:21:54 -04001978 tk->xtime_sec++;
1979
1980 /* Figure out if its a leap sec and apply if needed */
1981 leap = second_overflow(tk->xtime_sec);
John Stultz6d0ef902012-07-27 14:48:12 -04001982 if (unlikely(leap)) {
John Stultz7d489d12014-07-16 21:04:01 +00001983 struct timespec64 ts;
John Stultz1f4f9482012-07-13 01:21:54 -04001984
John Stultz6d0ef902012-07-27 14:48:12 -04001985 tk->xtime_sec += leap;
1986
1987 ts.tv_sec = leap;
1988 ts.tv_nsec = 0;
1989 tk_set_wall_to_mono(tk,
John Stultz7d489d12014-07-16 21:04:01 +00001990 timespec64_sub(tk->wall_to_monotonic, ts));
John Stultz6d0ef902012-07-27 14:48:12 -04001991
John Stultzcc244dd2012-05-03 12:30:07 -07001992 __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
1993
John Stultz5258d3f2013-12-11 20:07:49 -08001994 clock_set = TK_CLOCK_WAS_SET;
John Stultz6d0ef902012-07-27 14:48:12 -04001995 }
John Stultz1f4f9482012-07-13 01:21:54 -04001996 }
John Stultz5258d3f2013-12-11 20:07:49 -08001997 return clock_set;
John Stultz1f4f9482012-07-13 01:21:54 -04001998}
1999
John Stultz1f4f9482012-07-13 01:21:54 -04002000/**
john stultza092ff02009-10-02 16:17:53 -07002001 * logarithmic_accumulation - shifted accumulation of cycles
2002 *
2003 * This functions accumulates a shifted interval of cycles into
2004 * into a shifted interval nanoseconds. Allows for O(log) accumulation
2005 * loop.
2006 *
2007 * Returns the unconsumed cycles.
2008 */
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002009static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset,
2010 u32 shift, unsigned int *clock_set)
john stultza092ff02009-10-02 16:17:53 -07002011{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002012 u64 interval = tk->cycle_interval << shift;
John Stultz3d88d562017-06-08 16:44:21 -07002013 u64 snsec_per_sec;
john stultza092ff02009-10-02 16:17:53 -07002014
Zhen Lei571af552015-08-25 14:42:53 +08002015 /* If the offset is smaller than a shifted interval, do nothing */
Thomas Gleixner23a95372013-02-21 22:51:36 +00002016 if (offset < interval)
john stultza092ff02009-10-02 16:17:53 -07002017 return offset;
2018
2019 /* Accumulate one shifted interval */
Thomas Gleixner23a95372013-02-21 22:51:36 +00002020 offset -= interval;
Peter Zijlstra876e7882015-03-19 10:09:06 +01002021 tk->tkr_mono.cycle_last += interval;
Peter Zijlstra4a4ad802015-03-19 09:28:44 +01002022 tk->tkr_raw.cycle_last += interval;
john stultza092ff02009-10-02 16:17:53 -07002023
Peter Zijlstra876e7882015-03-19 10:09:06 +01002024 tk->tkr_mono.xtime_nsec += tk->xtime_interval << shift;
John Stultz5258d3f2013-12-11 20:07:49 -08002025 *clock_set |= accumulate_nsecs_to_secs(tk);
john stultza092ff02009-10-02 16:17:53 -07002026
Jason Wesseldeda2e82010-08-09 14:20:09 -07002027 /* Accumulate raw time */
John Stultz3d88d562017-06-08 16:44:21 -07002028 tk->tkr_raw.xtime_nsec += tk->raw_interval << shift;
2029 snsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_raw.shift;
2030 while (tk->tkr_raw.xtime_nsec >= snsec_per_sec) {
2031 tk->tkr_raw.xtime_nsec -= snsec_per_sec;
John Stultzfc6eead72017-05-22 17:20:20 -07002032 tk->raw_sec++;
john stultza092ff02009-10-02 16:17:53 -07002033 }
2034
2035 /* Accumulate error between NTP and clock interval */
John Stultz375f45b2014-04-23 20:53:29 -07002036 tk->ntp_error += tk->ntp_tick << shift;
John Stultzf726a692012-07-13 01:21:57 -04002037 tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
2038 (tk->ntp_error_shift + shift);
john stultza092ff02009-10-02 16:17:53 -07002039
2040 return offset;
2041}
2042
john stultz85240702007-05-08 00:27:59 -07002043/**
2044 * update_wall_time - Uses the current clocksource to increment the wall time
2045 *
john stultz85240702007-05-08 00:27:59 -07002046 */
John Stultz47a1b7962013-12-12 13:10:55 -08002047void update_wall_time(void)
john stultz85240702007-05-08 00:27:59 -07002048{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002049 struct timekeeper *real_tk = &tk_core.timekeeper;
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002050 struct timekeeper *tk = &shadow_timekeeper;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002051 u64 offset;
john stultza092ff02009-10-02 16:17:53 -07002052 int shift = 0, maxshift;
John Stultz5258d3f2013-12-11 20:07:49 -08002053 unsigned int clock_set = 0;
John Stultz70471f22011-11-14 12:48:10 -08002054 unsigned long flags;
2055
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00002056 raw_spin_lock_irqsave(&timekeeper_lock, flags);
john stultz85240702007-05-08 00:27:59 -07002057
2058 /* Make sure we're fully resumed: */
2059 if (unlikely(timekeeping_suspended))
John Stultz70471f22011-11-14 12:48:10 -08002060 goto out;
john stultz85240702007-05-08 00:27:59 -07002061
John Stultz592913e2010-07-13 17:56:20 -07002062#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002063 offset = real_tk->cycle_interval;
John Stultz592913e2010-07-13 17:56:20 -07002064#else
John Stultzceea5e32017-06-08 16:44:20 -07002065 offset = clocksource_delta(tk_clock_read(&tk->tkr_mono),
Peter Zijlstra876e7882015-03-19 10:09:06 +01002066 tk->tkr_mono.cycle_last, tk->tkr_mono.mask);
john stultz85240702007-05-08 00:27:59 -07002067#endif
john stultz85240702007-05-08 00:27:59 -07002068
John Stultzbf2ac312012-08-21 20:30:49 -04002069 /* Check if there's really nothing to do */
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002070 if (offset < real_tk->cycle_interval)
John Stultzbf2ac312012-08-21 20:30:49 -04002071 goto out;
2072
John Stultz3c17ad12015-03-11 21:16:32 -07002073 /* Do some additional sanity checking */
Stafford Hornea529bea2017-06-28 22:21:35 +09002074 timekeeping_check_update(tk, offset);
John Stultz3c17ad12015-03-11 21:16:32 -07002075
john stultza092ff02009-10-02 16:17:53 -07002076 /*
2077 * With NO_HZ we may have to accumulate many cycle_intervals
2078 * (think "ticks") worth of time at once. To do this efficiently,
2079 * we calculate the largest doubling multiple of cycle_intervals
Jim Cromie88b28ad2012-03-14 21:28:56 -06002080 * that is smaller than the offset. We then accumulate that
john stultza092ff02009-10-02 16:17:53 -07002081 * chunk in one go, and then try to consume the next smaller
2082 * doubled multiple.
john stultz85240702007-05-08 00:27:59 -07002083 */
John Stultz4e250fd2012-07-27 14:48:13 -04002084 shift = ilog2(offset) - ilog2(tk->cycle_interval);
john stultza092ff02009-10-02 16:17:53 -07002085 shift = max(0, shift);
Jim Cromie88b28ad2012-03-14 21:28:56 -06002086 /* Bound shift to one less than what overflows tick_length */
John Stultzea7cf492011-11-14 13:18:07 -08002087 maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
john stultza092ff02009-10-02 16:17:53 -07002088 shift = min(shift, maxshift);
John Stultz4e250fd2012-07-27 14:48:13 -04002089 while (offset >= tk->cycle_interval) {
John Stultz5258d3f2013-12-11 20:07:49 -08002090 offset = logarithmic_accumulation(tk, offset, shift,
2091 &clock_set);
John Stultz4e250fd2012-07-27 14:48:13 -04002092 if (offset < tk->cycle_interval<<shift)
John Stultz830ec042010-03-18 14:47:30 -07002093 shift--;
john stultz85240702007-05-08 00:27:59 -07002094 }
2095
2096 /* correct the clock when NTP error is too big */
John Stultz4e250fd2012-07-27 14:48:13 -04002097 timekeeping_adjust(tk, offset);
john stultz85240702007-05-08 00:27:59 -07002098
John Stultz6a867a32010-04-06 14:30:51 -07002099 /*
John Stultz92bb1fc2012-09-04 15:38:12 -04002100 * XXX This can be killed once everyone converts
2101 * to the new update_vsyscall.
2102 */
2103 old_vsyscall_fixup(tk);
john stultz85240702007-05-08 00:27:59 -07002104
John Stultz6a867a32010-04-06 14:30:51 -07002105 /*
2106 * Finally, make sure that after the rounding
John Stultz1e75fa82012-07-13 01:21:53 -04002107 * xtime_nsec isn't larger than NSEC_PER_SEC
John Stultz6a867a32010-04-06 14:30:51 -07002108 */
John Stultz5258d3f2013-12-11 20:07:49 -08002109 clock_set |= accumulate_nsecs_to_secs(tk);
Linus Torvalds83f57a12009-12-22 14:10:37 -08002110
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002111 write_seqcount_begin(&tk_core.seq);
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002112 /*
2113 * Update the real timekeeper.
2114 *
2115 * We could avoid this memcpy by switching pointers, but that
2116 * requires changes to all other timekeeper usage sites as
2117 * well, i.e. move the timekeeper pointer getter into the
2118 * spinlocked/seqcount protected sections. And we trade this
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002119 * memcpy under the tk_core.seq against one before we start
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002120 * updating.
2121 */
John Stultz906c5552015-06-17 10:05:53 -07002122 timekeeping_update(tk, clock_set);
Thomas Gleixner48cdc132013-02-21 22:51:40 +00002123 memcpy(real_tk, tk, sizeof(*tk));
John Stultz906c5552015-06-17 10:05:53 -07002124 /* The memcpy must come last. Do not put anything here! */
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002125 write_seqcount_end(&tk_core.seq);
Thomas Gleixnerca4523c2013-02-21 22:51:40 +00002126out:
Thomas Gleixner9a7a71b2013-02-21 22:51:38 +00002127 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultz47a1b7962013-12-12 13:10:55 -08002128 if (clock_set)
John Stultzcab5e122014-03-27 16:30:49 -07002129 /* Have to call _delayed version, since in irq context*/
2130 clock_was_set_delayed();
john stultz85240702007-05-08 00:27:59 -07002131}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002132
2133/**
John Stultzd08c0cd2014-12-08 12:00:09 -08002134 * getboottime64 - Return the real time of system boot.
2135 * @ts: pointer to the timespec64 to be set
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002136 *
John Stultzd08c0cd2014-12-08 12:00:09 -08002137 * Returns the wall-time of boot in a timespec64.
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002138 *
2139 * This is based on the wall_to_monotonic offset and the total suspend
2140 * time. Calls to settimeofday will affect the value returned (which
2141 * basically means that however wrong your real time clock is at boot time,
2142 * you get the right time here).
2143 */
John Stultzd08c0cd2014-12-08 12:00:09 -08002144void getboottime64(struct timespec64 *ts)
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002145{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002146 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixner02cba152014-07-16 21:04:58 +00002147 ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot);
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +02002148
John Stultzd08c0cd2014-12-08 12:00:09 -08002149 *ts = ktime_to_timespec64(t);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002150}
John Stultzd08c0cd2014-12-08 12:00:09 -08002151EXPORT_SYMBOL_GPL(getboottime64);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -07002152
john stultz17c38b72007-07-24 18:38:34 -07002153unsigned long get_seconds(void)
2154{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002155 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04002156
2157 return tk->xtime_sec;
john stultz17c38b72007-07-24 18:38:34 -07002158}
2159EXPORT_SYMBOL(get_seconds);
2160
john stultzda15cfd2009-08-19 19:13:34 -07002161struct timespec __current_kernel_time(void)
2162{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002163 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz4e250fd2012-07-27 14:48:13 -04002164
John Stultz7d489d12014-07-16 21:04:01 +00002165 return timespec64_to_timespec(tk_xtime(tk));
john stultzda15cfd2009-08-19 19:13:34 -07002166}
john stultz17c38b72007-07-24 18:38:34 -07002167
Baolin Wang8758a242015-07-29 20:09:43 +08002168struct timespec64 current_kernel_time64(void)
john stultz2c6b47d2007-07-24 17:47:43 -07002169{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002170 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz7d489d12014-07-16 21:04:01 +00002171 struct timespec64 now;
john stultz2c6b47d2007-07-24 17:47:43 -07002172 unsigned long seq;
2173
2174 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002175 seq = read_seqcount_begin(&tk_core.seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08002176
John Stultz4e250fd2012-07-27 14:48:13 -04002177 now = tk_xtime(tk);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002178 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultz2c6b47d2007-07-24 17:47:43 -07002179
Baolin Wang8758a242015-07-29 20:09:43 +08002180 return now;
john stultz2c6b47d2007-07-24 17:47:43 -07002181}
Baolin Wang8758a242015-07-29 20:09:43 +08002182EXPORT_SYMBOL(current_kernel_time64);
john stultzda15cfd2009-08-19 19:13:34 -07002183
John Stultz334334b2014-11-07 11:20:40 -08002184struct timespec64 get_monotonic_coarse64(void)
john stultzda15cfd2009-08-19 19:13:34 -07002185{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002186 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz7d489d12014-07-16 21:04:01 +00002187 struct timespec64 now, mono;
john stultzda15cfd2009-08-19 19:13:34 -07002188 unsigned long seq;
2189
2190 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002191 seq = read_seqcount_begin(&tk_core.seq);
Linus Torvalds83f57a12009-12-22 14:10:37 -08002192
John Stultz4e250fd2012-07-27 14:48:13 -04002193 now = tk_xtime(tk);
2194 mono = tk->wall_to_monotonic;
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002195 } while (read_seqcount_retry(&tk_core.seq, seq));
john stultzda15cfd2009-08-19 19:13:34 -07002196
John Stultz7d489d12014-07-16 21:04:01 +00002197 set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec,
john stultzda15cfd2009-08-19 19:13:34 -07002198 now.tv_nsec + mono.tv_nsec);
John Stultz7d489d12014-07-16 21:04:01 +00002199
John Stultz334334b2014-11-07 11:20:40 -08002200 return now;
john stultzda15cfd2009-08-19 19:13:34 -07002201}
Gregor Boirieeaaa7ec2016-03-09 19:05:48 +01002202EXPORT_SYMBOL(get_monotonic_coarse64);
Torben Hohn871cf1e2011-01-27 15:58:55 +01002203
2204/*
John Stultzd6ad4182012-02-28 16:50:11 -08002205 * Must hold jiffies_lock
Torben Hohn871cf1e2011-01-27 15:58:55 +01002206 */
2207void do_timer(unsigned long ticks)
2208{
2209 jiffies_64 += ticks;
Torben Hohn871cf1e2011-01-27 15:58:55 +01002210 calc_global_load(ticks);
2211}
Torben Hohn48cf76f72011-01-27 15:59:05 +01002212
2213/**
John Stultz76f41082014-07-16 21:03:52 +00002214 * ktime_get_update_offsets_now - hrtimer helper
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002215 * @cwsseq: pointer to check and store the clock was set sequence number
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002216 * @offs_real: pointer to storage for monotonic -> realtime offset
2217 * @offs_boot: pointer to storage for monotonic -> boottime offset
Xie XiuQib7bc50e2013-10-18 09:13:30 +08002218 * @offs_tai: pointer to storage for monotonic -> clock tai offset
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002219 *
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002220 * Returns current monotonic time and updates the offsets if the
2221 * sequence number in @cwsseq and timekeeper.clock_was_set_seq are
2222 * different.
2223 *
Xie XiuQib7bc50e2013-10-18 09:13:30 +08002224 * Called from hrtimer_interrupt() or retrigger_next_event()
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002225 */
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002226ktime_t ktime_get_update_offsets_now(unsigned int *cwsseq, ktime_t *offs_real,
2227 ktime_t *offs_boot, ktime_t *offs_tai)
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002228{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002229 struct timekeeper *tk = &tk_core.timekeeper;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002230 unsigned int seq;
Thomas Gleixnera37c0aa2014-07-16 21:04:19 +00002231 ktime_t base;
2232 u64 nsecs;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002233
2234 do {
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002235 seq = read_seqcount_begin(&tk_core.seq);
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002236
Peter Zijlstra876e7882015-03-19 10:09:06 +01002237 base = tk->tkr_mono.base;
2238 nsecs = timekeeping_get_ns(&tk->tkr_mono);
John Stultz833f32d2015-06-11 15:54:55 -07002239 base = ktime_add_ns(base, nsecs);
2240
Thomas Gleixner868a3e92015-04-14 21:08:37 +00002241 if (*cwsseq != tk->clock_was_set_seq) {
2242 *cwsseq = tk->clock_was_set_seq;
2243 *offs_real = tk->offs_real;
2244 *offs_boot = tk->offs_boot;
2245 *offs_tai = tk->offs_tai;
2246 }
John Stultz833f32d2015-06-11 15:54:55 -07002247
2248 /* Handle leapsecond insertion adjustments */
Thomas Gleixner2456e852016-12-25 11:38:40 +01002249 if (unlikely(base >= tk->next_leap_ktime))
John Stultz833f32d2015-06-11 15:54:55 -07002250 *offs_real = ktime_sub(tk->offs_real, ktime_set(1, 0));
2251
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002252 } while (read_seqcount_retry(&tk_core.seq, seq));
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002253
John Stultz833f32d2015-06-11 15:54:55 -07002254 return base;
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002255}
Thomas Gleixnerf6c06ab2012-07-10 18:43:24 -04002256
Torben Hohnf0af911a92011-01-27 15:59:10 +01002257/**
John Stultzaa6f9c592013-03-22 11:31:29 -07002258 * do_adjtimex() - Accessor function to NTP __do_adjtimex function
2259 */
2260int do_adjtimex(struct timex *txc)
2261{
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002262 struct timekeeper *tk = &tk_core.timekeeper;
John Stultz06c017f2013-03-22 11:37:28 -07002263 unsigned long flags;
John Stultz7d489d12014-07-16 21:04:01 +00002264 struct timespec64 ts;
John Stultz4e8f8b32013-04-10 12:41:49 -07002265 s32 orig_tai, tai;
John Stultze4085692013-03-22 12:08:52 -07002266 int ret;
2267
2268 /* Validate the data before disabling interrupts */
2269 ret = ntp_validate_timex(txc);
2270 if (ret)
2271 return ret;
2272
John Stultzcef90372013-03-22 15:04:13 -07002273 if (txc->modes & ADJ_SETOFFSET) {
2274 struct timespec delta;
2275 delta.tv_sec = txc->time.tv_sec;
2276 delta.tv_nsec = txc->time.tv_usec;
2277 if (!(txc->modes & ADJ_NANO))
2278 delta.tv_nsec *= 1000;
2279 ret = timekeeping_inject_offset(&delta);
2280 if (ret)
2281 return ret;
2282 }
2283
Thomas Gleixnerd6d29892014-07-16 21:04:04 +00002284 getnstimeofday64(&ts);
John Stultzaa6f9c592013-03-22 11:31:29 -07002285
John Stultz06c017f2013-03-22 11:37:28 -07002286 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002287 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002288
John Stultz4e8f8b32013-04-10 12:41:49 -07002289 orig_tai = tai = tk->tai_offset;
John Stultz87ace392013-03-22 12:28:15 -07002290 ret = __do_adjtimex(txc, &ts, &tai);
2291
John Stultz4e8f8b32013-04-10 12:41:49 -07002292 if (tai != orig_tai) {
2293 __timekeeping_set_tai_offset(tk, tai);
John Stultzf55c0762013-12-11 18:50:25 -08002294 timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
John Stultz4e8f8b32013-04-10 12:41:49 -07002295 }
John Stultz833f32d2015-06-11 15:54:55 -07002296 tk_update_leap_state(tk);
2297
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002298 write_seqcount_end(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002299 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
2300
John Stultz6fdda9a2013-12-10 17:18:18 -08002301 if (tai != orig_tai)
2302 clock_was_set();
2303
John Stultz7bd36012013-09-11 16:50:56 -07002304 ntp_notify_cmos_timer();
2305
John Stultz87ace392013-03-22 12:28:15 -07002306 return ret;
2307}
John Stultzaa6f9c592013-03-22 11:31:29 -07002308
2309#ifdef CONFIG_NTP_PPS
2310/**
2311 * hardpps() - Accessor function to NTP __hardpps function
2312 */
Arnd Bergmann7ec88e42015-09-28 22:21:28 +02002313void hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts)
John Stultzaa6f9c592013-03-22 11:31:29 -07002314{
John Stultz06c017f2013-03-22 11:37:28 -07002315 unsigned long flags;
2316
2317 raw_spin_lock_irqsave(&timekeeper_lock, flags);
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002318 write_seqcount_begin(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002319
John Stultzaa6f9c592013-03-22 11:31:29 -07002320 __hardpps(phase_ts, raw_ts);
John Stultz06c017f2013-03-22 11:37:28 -07002321
Thomas Gleixner3fdb14f2014-07-16 21:04:07 +00002322 write_seqcount_end(&tk_core.seq);
John Stultz06c017f2013-03-22 11:37:28 -07002323 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
John Stultzaa6f9c592013-03-22 11:31:29 -07002324}
2325EXPORT_SYMBOL(hardpps);
Robert P. J. Daya2d81802017-09-08 16:17:19 -07002326#endif /* CONFIG_NTP_PPS */
John Stultzaa6f9c592013-03-22 11:31:29 -07002327
2328/**
Torben Hohnf0af911a92011-01-27 15:59:10 +01002329 * xtime_update() - advances the timekeeping infrastructure
2330 * @ticks: number of ticks, that have elapsed since the last call.
2331 *
2332 * Must be called with interrupts disabled.
2333 */
2334void xtime_update(unsigned long ticks)
2335{
John Stultzd6ad4182012-02-28 16:50:11 -08002336 write_seqlock(&jiffies_lock);
Torben Hohnf0af911a92011-01-27 15:59:10 +01002337 do_timer(ticks);
John Stultzd6ad4182012-02-28 16:50:11 -08002338 write_sequnlock(&jiffies_lock);
John Stultz47a1b7962013-12-12 13:10:55 -08002339 update_wall_time();
Torben Hohnf0af911a92011-01-27 15:59:10 +01002340}