Blame view

kernel/time/timekeeping.c 49.6 KB
8524070b7   John Stultz   Move timekeeping ...
1
2
3
4
5
6
7
8
9
  /*
   *  linux/kernel/time/timekeeping.c
   *
   *  Kernel timekeeping code and accessor functions
   *
   *  This code was moved from linux/kernel/timer.c.
   *  Please see that file for copyright and history logs.
   *
   */
d7b4202e0   John Stultz   time: Move timeke...
10
  #include <linux/timekeeper_internal.h>
8524070b7   John Stultz   Move timekeeping ...
11
12
13
14
15
  #include <linux/module.h>
  #include <linux/interrupt.h>
  #include <linux/percpu.h>
  #include <linux/init.h>
  #include <linux/mm.h>
d43c36dc6   Alexey Dobriyan   headers: remove s...
16
  #include <linux/sched.h>
e1a85b2c5   Rafael J. Wysocki   timekeeping: Use ...
17
  #include <linux/syscore_ops.h>
8524070b7   John Stultz   Move timekeeping ...
18
19
20
21
  #include <linux/clocksource.h>
  #include <linux/jiffies.h>
  #include <linux/time.h>
  #include <linux/tick.h>
75c5158f7   Martin Schwidefsky   timekeeping: Upda...
22
  #include <linux/stop_machine.h>
e0b306fef   Marcelo Tosatti   time: export time...
23
  #include <linux/pvclock_gtod.h>
52f5684c8   Gideon Israel Dsouza   kernel: use macro...
24
  #include <linux/compiler.h>
8524070b7   John Stultz   Move timekeeping ...
25

eb93e4d93   Thomas Gleixner   timekeeping: Make...
26
  #include "tick-internal.h"
aa6f9c595   John Stultz   ntp: Move do_adjt...
27
  #include "ntp_internal.h"
5c83545f2   Colin Cross   power: Add option...
28
  #include "timekeeping_internal.h"
155ec6022   Martin Schwidefsky   timekeeping: Intr...
29

04397fe94   David Vrabel   timekeeping: Pass...
30
31
  #define TK_CLEAR_NTP		(1 << 0)
  #define TK_MIRROR		(1 << 1)
780427f0e   David Vrabel   timekeeping: Indi...
32
  #define TK_CLOCK_WAS_SET	(1 << 2)
04397fe94   David Vrabel   timekeeping: Pass...
33

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
34
35
36
37
38
39
40
41
  /*
   * The most important data for readout fits into a single 64 byte
   * cache line.
   */
  static struct {
  	seqcount_t		seq;
  	struct timekeeper	timekeeper;
  } tk_core ____cacheline_aligned;
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
42
  static DEFINE_RAW_SPINLOCK(timekeeper_lock);
48cdc135d   Thomas Gleixner   timekeeping: Impl...
43
  static struct timekeeper shadow_timekeeper;
155ec6022   Martin Schwidefsky   timekeeping: Intr...
44

4396e058c   Thomas Gleixner   timekeeping: Prov...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  /**
   * struct tk_fast - NMI safe timekeeper
   * @seq:	Sequence counter for protecting updates. The lowest bit
   *		is the index for the tk_read_base array
   * @base:	tk_read_base array. Access is indexed by the lowest bit of
   *		@seq.
   *
   * See @update_fast_timekeeper() below.
   */
  struct tk_fast {
  	seqcount_t		seq;
  	struct tk_read_base	base[2];
  };
  
  static struct tk_fast tk_fast_mono ____cacheline_aligned;
8fcce546b   John Stultz   time: Cleanup glo...
60
61
  /* flag for if timekeeping is suspended */
  int __read_mostly timekeeping_suspended;
31ade3069   Feng Tang   timekeeping: Add ...
62
63
  /* Flag for if there is a persistent clock on this platform */
  bool __read_mostly persistent_clock_exist = false;
1e75fa8be   John Stultz   time: Condense ti...
64
65
  static inline void tk_normalize_xtime(struct timekeeper *tk)
  {
d28ede837   Thomas Gleixner   timekeeping: Crea...
66
67
  	while (tk->tkr.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr.shift)) {
  		tk->tkr.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr.shift;
1e75fa8be   John Stultz   time: Condense ti...
68
69
70
  		tk->xtime_sec++;
  	}
  }
c905fae43   Thomas Gleixner   timekeeper: Move ...
71
72
73
74
75
  static inline struct timespec64 tk_xtime(struct timekeeper *tk)
  {
  	struct timespec64 ts;
  
  	ts.tv_sec = tk->xtime_sec;
d28ede837   Thomas Gleixner   timekeeping: Crea...
76
  	ts.tv_nsec = (long)(tk->tkr.xtime_nsec >> tk->tkr.shift);
c905fae43   Thomas Gleixner   timekeeper: Move ...
77
78
  	return ts;
  }
7d489d15c   John Stultz   timekeeping: Conv...
79
  static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts)
1e75fa8be   John Stultz   time: Condense ti...
80
81
  {
  	tk->xtime_sec = ts->tv_sec;
d28ede837   Thomas Gleixner   timekeeping: Crea...
82
  	tk->tkr.xtime_nsec = (u64)ts->tv_nsec << tk->tkr.shift;
1e75fa8be   John Stultz   time: Condense ti...
83
  }
7d489d15c   John Stultz   timekeeping: Conv...
84
  static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts)
1e75fa8be   John Stultz   time: Condense ti...
85
86
  {
  	tk->xtime_sec += ts->tv_sec;
d28ede837   Thomas Gleixner   timekeeping: Crea...
87
  	tk->tkr.xtime_nsec += (u64)ts->tv_nsec << tk->tkr.shift;
784ffcbb9   John Stultz   time: Ensure we n...
88
  	tk_normalize_xtime(tk);
1e75fa8be   John Stultz   time: Condense ti...
89
  }
8fcce546b   John Stultz   time: Cleanup glo...
90

7d489d15c   John Stultz   timekeeping: Conv...
91
  static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm)
6d0ef903e   John Stultz   time: Clean up of...
92
  {
7d489d15c   John Stultz   timekeeping: Conv...
93
  	struct timespec64 tmp;
6d0ef903e   John Stultz   time: Clean up of...
94
95
96
97
98
  
  	/*
  	 * Verify consistency of: offset_real = -wall_to_monotonic
  	 * before modifying anything
  	 */
7d489d15c   John Stultz   timekeeping: Conv...
99
  	set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec,
6d0ef903e   John Stultz   time: Clean up of...
100
  					-tk->wall_to_monotonic.tv_nsec);
7d489d15c   John Stultz   timekeeping: Conv...
101
  	WARN_ON_ONCE(tk->offs_real.tv64 != timespec64_to_ktime(tmp).tv64);
6d0ef903e   John Stultz   time: Clean up of...
102
  	tk->wall_to_monotonic = wtm;
7d489d15c   John Stultz   timekeeping: Conv...
103
104
  	set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
  	tk->offs_real = timespec64_to_ktime(tmp);
04005f601   John Stultz   timekeeping: Fix ...
105
  	tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0));
6d0ef903e   John Stultz   time: Clean up of...
106
  }
47da70d32   Thomas Gleixner   timekeeping: Remo...
107
  static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
6d0ef903e   John Stultz   time: Clean up of...
108
  {
47da70d32   Thomas Gleixner   timekeeping: Remo...
109
  	tk->offs_boot = ktime_add(tk->offs_boot, delta);
6d0ef903e   John Stultz   time: Clean up of...
110
  }
155ec6022   Martin Schwidefsky   timekeeping: Intr...
111
  /**
d26e4fe0d   Yijing Wang   timekeeper: fix c...
112
   * tk_setup_internals - Set up internals to use clocksource clock.
155ec6022   Martin Schwidefsky   timekeeping: Intr...
113
   *
d26e4fe0d   Yijing Wang   timekeeper: fix c...
114
   * @tk:		The target timekeeper to setup.
155ec6022   Martin Schwidefsky   timekeeping: Intr...
115
116
117
118
119
120
121
   * @clock:		Pointer to clocksource.
   *
   * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
   * pair and interval request.
   *
   * Unless you're the timekeeping code, you should not be using this!
   */
f726a697d   John Stultz   time: Rework time...
122
  static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
155ec6022   Martin Schwidefsky   timekeeping: Intr...
123
124
  {
  	cycle_t interval;
a386b5af8   Kasper Pedersen   time: Compensate ...
125
  	u64 tmp, ntpinterval;
1e75fa8be   John Stultz   time: Condense ti...
126
  	struct clocksource *old_clock;
155ec6022   Martin Schwidefsky   timekeeping: Intr...
127

d28ede837   Thomas Gleixner   timekeeping: Crea...
128
129
130
131
132
  	old_clock = tk->tkr.clock;
  	tk->tkr.clock = clock;
  	tk->tkr.read = clock->read;
  	tk->tkr.mask = clock->mask;
  	tk->tkr.cycle_last = tk->tkr.read(clock);
155ec6022   Martin Schwidefsky   timekeeping: Intr...
133
134
135
136
  
  	/* Do the ns -> cycle conversion first, using original mult */
  	tmp = NTP_INTERVAL_LENGTH;
  	tmp <<= clock->shift;
a386b5af8   Kasper Pedersen   time: Compensate ...
137
  	ntpinterval = tmp;
0a5441983   Martin Schwidefsky   timekeeping: Move...
138
139
  	tmp += clock->mult/2;
  	do_div(tmp, clock->mult);
155ec6022   Martin Schwidefsky   timekeeping: Intr...
140
141
142
143
  	if (tmp == 0)
  		tmp = 1;
  
  	interval = (cycle_t) tmp;
f726a697d   John Stultz   time: Rework time...
144
  	tk->cycle_interval = interval;
155ec6022   Martin Schwidefsky   timekeeping: Intr...
145
146
  
  	/* Go back from cycles -> shifted ns */
f726a697d   John Stultz   time: Rework time...
147
148
149
  	tk->xtime_interval = (u64) interval * clock->mult;
  	tk->xtime_remainder = ntpinterval - tk->xtime_interval;
  	tk->raw_interval =
0a5441983   Martin Schwidefsky   timekeeping: Move...
150
  		((u64) interval * clock->mult) >> clock->shift;
155ec6022   Martin Schwidefsky   timekeeping: Intr...
151

1e75fa8be   John Stultz   time: Condense ti...
152
153
154
155
  	 /* if changing clocks, convert xtime_nsec shift units */
  	if (old_clock) {
  		int shift_change = clock->shift - old_clock->shift;
  		if (shift_change < 0)
d28ede837   Thomas Gleixner   timekeeping: Crea...
156
  			tk->tkr.xtime_nsec >>= -shift_change;
1e75fa8be   John Stultz   time: Condense ti...
157
  		else
d28ede837   Thomas Gleixner   timekeeping: Crea...
158
  			tk->tkr.xtime_nsec <<= shift_change;
1e75fa8be   John Stultz   time: Condense ti...
159
  	}
d28ede837   Thomas Gleixner   timekeeping: Crea...
160
  	tk->tkr.shift = clock->shift;
155ec6022   Martin Schwidefsky   timekeeping: Intr...
161

f726a697d   John Stultz   time: Rework time...
162
163
  	tk->ntp_error = 0;
  	tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
375f45b5b   John Stultz   timekeeping: Use ...
164
  	tk->ntp_tick = ntpinterval << tk->ntp_error_shift;
0a5441983   Martin Schwidefsky   timekeeping: Move...
165
166
167
168
169
170
  
  	/*
  	 * The timekeeper keeps its own mult values for the currently
  	 * active clocksource. These value will be adjusted via NTP
  	 * to counteract clock drifting.
  	 */
d28ede837   Thomas Gleixner   timekeeping: Crea...
171
  	tk->tkr.mult = clock->mult;
dc491596f   John Stultz   timekeeping: Rewo...
172
  	tk->ntp_err_mult = 0;
155ec6022   Martin Schwidefsky   timekeeping: Intr...
173
  }
8524070b7   John Stultz   Move timekeeping ...
174

2ba2a3054   Martin Schwidefsky   timekeeping: Add ...
175
  /* Timekeeper helper functions. */
7b1f62076   Stephen Warren   time: convert arc...
176
177
  
  #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
e06fde37b   Thomas Gleixner   timekeeping: Simp...
178
179
  static u32 default_arch_gettimeoffset(void) { return 0; }
  u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset;
7b1f62076   Stephen Warren   time: convert arc...
180
  #else
e06fde37b   Thomas Gleixner   timekeeping: Simp...
181
  static inline u32 arch_gettimeoffset(void) { return 0; }
7b1f62076   Stephen Warren   time: convert arc...
182
  #endif
0e5ac3a8b   Thomas Gleixner   timekeeping: Use ...
183
  static inline s64 timekeeping_get_ns(struct tk_read_base *tkr)
2ba2a3054   Martin Schwidefsky   timekeeping: Add ...
184
  {
3a9783778   Thomas Gleixner   clocksource: Make...
185
  	cycle_t cycle_now, delta;
1e75fa8be   John Stultz   time: Condense ti...
186
  	s64 nsec;
2ba2a3054   Martin Schwidefsky   timekeeping: Add ...
187
188
  
  	/* read clocksource: */
0e5ac3a8b   Thomas Gleixner   timekeeping: Use ...
189
  	cycle_now = tkr->read(tkr->clock);
2ba2a3054   Martin Schwidefsky   timekeeping: Add ...
190
191
  
  	/* calculate the delta since the last update_wall_time: */
0e5ac3a8b   Thomas Gleixner   timekeeping: Use ...
192
  	delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask);
2ba2a3054   Martin Schwidefsky   timekeeping: Add ...
193

0e5ac3a8b   Thomas Gleixner   timekeeping: Use ...
194
195
  	nsec = delta * tkr->mult + tkr->xtime_nsec;
  	nsec >>= tkr->shift;
f2a5a0854   John Stultz   time: Move arch_g...
196

7b1f62076   Stephen Warren   time: convert arc...
197
  	/* If arch requires, add in get_arch_timeoffset() */
e06fde37b   Thomas Gleixner   timekeeping: Simp...
198
  	return nsec + arch_gettimeoffset();
2ba2a3054   Martin Schwidefsky   timekeeping: Add ...
199
  }
f726a697d   John Stultz   time: Rework time...
200
  static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk)
2ba2a3054   Martin Schwidefsky   timekeeping: Add ...
201
  {
d28ede837   Thomas Gleixner   timekeeping: Crea...
202
  	struct clocksource *clock = tk->tkr.clock;
3a9783778   Thomas Gleixner   clocksource: Make...
203
  	cycle_t cycle_now, delta;
f2a5a0854   John Stultz   time: Move arch_g...
204
  	s64 nsec;
2ba2a3054   Martin Schwidefsky   timekeeping: Add ...
205
206
  
  	/* read clocksource: */
d28ede837   Thomas Gleixner   timekeeping: Crea...
207
  	cycle_now = tk->tkr.read(clock);
2ba2a3054   Martin Schwidefsky   timekeeping: Add ...
208
209
  
  	/* calculate the delta since the last update_wall_time: */
d28ede837   Thomas Gleixner   timekeeping: Crea...
210
  	delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask);
2ba2a3054   Martin Schwidefsky   timekeeping: Add ...
211

f2a5a0854   John Stultz   time: Move arch_g...
212
  	/* convert delta to nanoseconds. */
3a9783778   Thomas Gleixner   clocksource: Make...
213
  	nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift);
f2a5a0854   John Stultz   time: Move arch_g...
214

7b1f62076   Stephen Warren   time: convert arc...
215
  	/* If arch requires, add in get_arch_timeoffset() */
e06fde37b   Thomas Gleixner   timekeeping: Simp...
216
  	return nsec + arch_gettimeoffset();
2ba2a3054   Martin Schwidefsky   timekeeping: Add ...
217
  }
4396e058c   Thomas Gleixner   timekeeping: Prov...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  /**
   * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper.
   * @tk:		The timekeeper from which we take the update
   * @tkf:	The fast timekeeper to update
   * @tbase:	The time base for the fast timekeeper (mono/raw)
   *
   * We want to use this from any context including NMI and tracing /
   * instrumenting the timekeeping code itself.
   *
   * So we handle this differently than the other timekeeping accessor
   * functions which retry when the sequence count has changed. The
   * update side does:
   *
   * smp_wmb();	<- Ensure that the last base[1] update is visible
   * tkf->seq++;
   * smp_wmb();	<- Ensure that the seqcount update is visible
   * update(tkf->base[0], tk);
   * smp_wmb();	<- Ensure that the base[0] update is visible
   * tkf->seq++;
   * smp_wmb();	<- Ensure that the seqcount update is visible
   * update(tkf->base[1], tk);
   *
   * The reader side does:
   *
   * do {
   *	seq = tkf->seq;
   *	smp_rmb();
   *	idx = seq & 0x01;
   *	now = now(tkf->base[idx]);
   *	smp_rmb();
   * } while (seq != tkf->seq)
   *
   * As long as we update base[0] readers are forced off to
   * base[1]. Once base[0] is updated readers are redirected to base[0]
   * and the base[1] update takes place.
   *
   * So if a NMI hits the update of base[0] then it will use base[1]
   * which is still consistent. In the worst case this can result is a
   * slightly wrong timestamp (a few nanoseconds). See
   * @ktime_get_mono_fast_ns.
   */
  static void update_fast_timekeeper(struct timekeeper *tk)
  {
  	struct tk_read_base *base = tk_fast_mono.base;
  
  	/* Force readers off to base[1] */
  	raw_write_seqcount_latch(&tk_fast_mono.seq);
  
  	/* Update base[0] */
  	memcpy(base, &tk->tkr, sizeof(*base));
  
  	/* Force readers back to base[0] */
  	raw_write_seqcount_latch(&tk_fast_mono.seq);
  
  	/* Update base[1] */
  	memcpy(base + 1, base, sizeof(*base));
  }
  
  /**
   * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic
   *
   * This timestamp is not guaranteed to be monotonic across an update.
   * The timestamp is calculated by:
   *
   *	now = base_mono + clock_delta * slope
   *
   * So if the update lowers the slope, readers who are forced to the
   * not yet updated second array are still using the old steeper slope.
   *
   * tmono
   * ^
   * |    o  n
   * |   o n
   * |  u
   * | o
   * |o
   * |12345678---> reader order
   *
   * o = old slope
   * u = update
   * n = new slope
   *
   * So reader 6 will observe time going backwards versus reader 5.
   *
   * While other CPUs are likely to be able observe that, the only way
   * for a CPU local observation is when an NMI hits in the middle of
   * the update. Timestamps taken from that NMI context might be ahead
   * of the following timestamps. Callers need to be aware of that and
   * deal with it.
   */
  u64 notrace ktime_get_mono_fast_ns(void)
  {
  	struct tk_read_base *tkr;
  	unsigned int seq;
  	u64 now;
  
  	do {
  		seq = raw_read_seqcount(&tk_fast_mono.seq);
  		tkr = tk_fast_mono.base + (seq & 0x01);
  		now = ktime_to_ns(tkr->base_mono) + timekeeping_get_ns(tkr);
  
  	} while (read_seqcount_retry(&tk_fast_mono.seq, seq));
  	return now;
  }
  EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
c905fae43   Thomas Gleixner   timekeeper: Move ...
323
324
325
326
  #ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
  
  static inline void update_vsyscall(struct timekeeper *tk)
  {
0680eb1f4   John Stultz   timekeeping: Anot...
327
  	struct timespec xt, wm;
c905fae43   Thomas Gleixner   timekeeper: Move ...
328

e2dff1ec0   John Stultz   timekeeping: Mino...
329
  	xt = timespec64_to_timespec(tk_xtime(tk));
0680eb1f4   John Stultz   timekeeping: Anot...
330
331
  	wm = timespec64_to_timespec(tk->wall_to_monotonic);
  	update_vsyscall_old(&xt, &wm, tk->tkr.clock, tk->tkr.mult,
d28ede837   Thomas Gleixner   timekeeping: Crea...
332
  			    tk->tkr.cycle_last);
c905fae43   Thomas Gleixner   timekeeper: Move ...
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
  }
  
  static inline void old_vsyscall_fixup(struct timekeeper *tk)
  {
  	s64 remainder;
  
  	/*
  	* Store only full nanoseconds into xtime_nsec after rounding
  	* it up and add the remainder to the error difference.
  	* XXX - This is necessary to avoid small 1ns inconsistnecies caused
  	* by truncating the remainder in vsyscalls. However, it causes
  	* additional work to be done in timekeeping_adjust(). Once
  	* the vsyscall implementations are converted to use xtime_nsec
  	* (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD
  	* users are removed, this can be killed.
  	*/
d28ede837   Thomas Gleixner   timekeeping: Crea...
349
350
351
  	remainder = tk->tkr.xtime_nsec & ((1ULL << tk->tkr.shift) - 1);
  	tk->tkr.xtime_nsec -= remainder;
  	tk->tkr.xtime_nsec += 1ULL << tk->tkr.shift;
c905fae43   Thomas Gleixner   timekeeper: Move ...
352
  	tk->ntp_error += remainder << tk->ntp_error_shift;
d28ede837   Thomas Gleixner   timekeeping: Crea...
353
  	tk->ntp_error -= (1ULL << tk->tkr.shift) << tk->ntp_error_shift;
c905fae43   Thomas Gleixner   timekeeper: Move ...
354
355
356
357
  }
  #else
  #define old_vsyscall_fixup(tk)
  #endif
e0b306fef   Marcelo Tosatti   time: export time...
358
  static RAW_NOTIFIER_HEAD(pvclock_gtod_chain);
780427f0e   David Vrabel   timekeeping: Indi...
359
  static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
e0b306fef   Marcelo Tosatti   time: export time...
360
  {
780427f0e   David Vrabel   timekeeping: Indi...
361
  	raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
e0b306fef   Marcelo Tosatti   time: export time...
362
363
364
365
  }
  
  /**
   * pvclock_gtod_register_notifier - register a pvclock timedata update listener
e0b306fef   Marcelo Tosatti   time: export time...
366
367
368
   */
  int pvclock_gtod_register_notifier(struct notifier_block *nb)
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
369
  	struct timekeeper *tk = &tk_core.timekeeper;
e0b306fef   Marcelo Tosatti   time: export time...
370
371
  	unsigned long flags;
  	int ret;
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
372
  	raw_spin_lock_irqsave(&timekeeper_lock, flags);
e0b306fef   Marcelo Tosatti   time: export time...
373
  	ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
780427f0e   David Vrabel   timekeeping: Indi...
374
  	update_pvclock_gtod(tk, true);
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
375
  	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
e0b306fef   Marcelo Tosatti   time: export time...
376
377
378
379
380
381
382
383
  
  	return ret;
  }
  EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
  
  /**
   * pvclock_gtod_unregister_notifier - unregister a pvclock
   * timedata update listener
e0b306fef   Marcelo Tosatti   time: export time...
384
385
386
   */
  int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
  {
e0b306fef   Marcelo Tosatti   time: export time...
387
388
  	unsigned long flags;
  	int ret;
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
389
  	raw_spin_lock_irqsave(&timekeeper_lock, flags);
e0b306fef   Marcelo Tosatti   time: export time...
390
  	ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
391
  	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
e0b306fef   Marcelo Tosatti   time: export time...
392
393
394
395
  
  	return ret;
  }
  EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
7c032df55   Thomas Gleixner   timekeeping: Prov...
396
397
398
399
400
  /*
   * Update the ktime_t based scalar nsec members of the timekeeper
   */
  static inline void tk_update_ktime_data(struct timekeeper *tk)
  {
9e3680b17   Heena Sirwani   timekeeping: Prov...
401
402
  	u64 seconds;
  	u32 nsec;
7c032df55   Thomas Gleixner   timekeeping: Prov...
403
404
405
406
407
408
409
410
  
  	/*
  	 * The xtime based monotonic readout is:
  	 *	nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now();
  	 * The ktime based monotonic readout is:
  	 *	nsec = base_mono + now();
  	 * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec
  	 */
9e3680b17   Heena Sirwani   timekeeping: Prov...
411
412
413
  	seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec);
  	nsec = (u32) tk->wall_to_monotonic.tv_nsec;
  	tk->tkr.base_mono = ns_to_ktime(seconds * NSEC_PER_SEC + nsec);
f519b1a2e   Thomas Gleixner   timekeeping: Prov...
414
415
416
  
  	/* Update the monotonic raw base */
  	tk->base_raw = timespec64_to_ktime(tk->raw_time);
9e3680b17   Heena Sirwani   timekeeping: Prov...
417
418
419
420
421
422
423
424
425
426
  
  	/*
  	 * The sum of the nanoseconds portions of xtime and
  	 * wall_to_monotonic can be greater/equal one second. Take
  	 * this into account before updating tk->ktime_sec.
  	 */
  	nsec += (u32)(tk->tkr.xtime_nsec >> tk->tkr.shift);
  	if (nsec >= NSEC_PER_SEC)
  		seconds++;
  	tk->ktime_sec = seconds;
7c032df55   Thomas Gleixner   timekeeping: Prov...
427
  }
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
428
  /* must hold timekeeper_lock */
04397fe94   David Vrabel   timekeeping: Pass...
429
  static void timekeeping_update(struct timekeeper *tk, unsigned int action)
cc06268c6   Thomas Gleixner   time: Move common...
430
  {
04397fe94   David Vrabel   timekeeping: Pass...
431
  	if (action & TK_CLEAR_NTP) {
f726a697d   John Stultz   time: Rework time...
432
  		tk->ntp_error = 0;
cc06268c6   Thomas Gleixner   time: Move common...
433
434
  		ntp_clear();
  	}
48cdc135d   Thomas Gleixner   timekeeping: Impl...
435

7c032df55   Thomas Gleixner   timekeeping: Prov...
436
  	tk_update_ktime_data(tk);
9bf2419fa   Thomas Gleixner   timekeeping: Upda...
437
438
  	update_vsyscall(tk);
  	update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET);
04397fe94   David Vrabel   timekeeping: Pass...
439
  	if (action & TK_MIRROR)
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
440
441
  		memcpy(&shadow_timekeeper, &tk_core.timekeeper,
  		       sizeof(tk_core.timekeeper));
4396e058c   Thomas Gleixner   timekeeping: Prov...
442
443
  
  	update_fast_timekeeper(tk);
cc06268c6   Thomas Gleixner   time: Move common...
444
  }
8524070b7   John Stultz   Move timekeeping ...
445
  /**
155ec6022   Martin Schwidefsky   timekeeping: Intr...
446
   * timekeeping_forward_now - update clock to the current time
8524070b7   John Stultz   Move timekeeping ...
447
   *
9a055117d   Roman Zippel   clocksource: intr...
448
449
450
   * Forward the current clock to update its state since the last call to
   * update_wall_time(). This is useful before significant clock changes,
   * as it avoids having to deal with this time offset explicitly.
8524070b7   John Stultz   Move timekeeping ...
451
   */
f726a697d   John Stultz   time: Rework time...
452
  static void timekeeping_forward_now(struct timekeeper *tk)
8524070b7   John Stultz   Move timekeeping ...
453
  {
d28ede837   Thomas Gleixner   timekeeping: Crea...
454
  	struct clocksource *clock = tk->tkr.clock;
3a9783778   Thomas Gleixner   clocksource: Make...
455
  	cycle_t cycle_now, delta;
9a055117d   Roman Zippel   clocksource: intr...
456
  	s64 nsec;
8524070b7   John Stultz   Move timekeeping ...
457

d28ede837   Thomas Gleixner   timekeeping: Crea...
458
459
460
  	cycle_now = tk->tkr.read(clock);
  	delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask);
  	tk->tkr.cycle_last = cycle_now;
8524070b7   John Stultz   Move timekeeping ...
461

d28ede837   Thomas Gleixner   timekeeping: Crea...
462
  	tk->tkr.xtime_nsec += delta * tk->tkr.mult;
7d27558c4   John Stultz   timekeeping: crea...
463

7b1f62076   Stephen Warren   time: convert arc...
464
  	/* If arch requires, add in get_arch_timeoffset() */
d28ede837   Thomas Gleixner   timekeeping: Crea...
465
  	tk->tkr.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr.shift;
7d27558c4   John Stultz   timekeeping: crea...
466

f726a697d   John Stultz   time: Rework time...
467
  	tk_normalize_xtime(tk);
2d42244ae   John Stultz   clocksource: intr...
468

3a9783778   Thomas Gleixner   clocksource: Make...
469
  	nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift);
7d489d15c   John Stultz   timekeeping: Conv...
470
  	timespec64_add_ns(&tk->raw_time, nsec);
8524070b7   John Stultz   Move timekeeping ...
471
472
473
  }
  
  /**
d6d29896c   Thomas Gleixner   timekeeping: Prov...
474
   * __getnstimeofday64 - Returns the time of day in a timespec64.
8524070b7   John Stultz   Move timekeeping ...
475
476
   * @ts:		pointer to the timespec to be set
   *
1e817fb62   Kees Cook   time: create __ge...
477
478
   * Updates the time of day in the timespec.
   * Returns 0 on success, or -ve when suspended (timespec will be undefined).
8524070b7   John Stultz   Move timekeeping ...
479
   */
d6d29896c   Thomas Gleixner   timekeeping: Prov...
480
  int __getnstimeofday64(struct timespec64 *ts)
8524070b7   John Stultz   Move timekeeping ...
481
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
482
  	struct timekeeper *tk = &tk_core.timekeeper;
8524070b7   John Stultz   Move timekeeping ...
483
  	unsigned long seq;
1e75fa8be   John Stultz   time: Condense ti...
484
  	s64 nsecs = 0;
8524070b7   John Stultz   Move timekeeping ...
485
486
  
  	do {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
487
  		seq = read_seqcount_begin(&tk_core.seq);
8524070b7   John Stultz   Move timekeeping ...
488

4e250fdde   John Stultz   time: Remove all ...
489
  		ts->tv_sec = tk->xtime_sec;
0e5ac3a8b   Thomas Gleixner   timekeeping: Use ...
490
  		nsecs = timekeeping_get_ns(&tk->tkr);
8524070b7   John Stultz   Move timekeeping ...
491

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
492
  	} while (read_seqcount_retry(&tk_core.seq, seq));
8524070b7   John Stultz   Move timekeeping ...
493

ec145babe   John Stultz   time: Fix timeeke...
494
  	ts->tv_nsec = 0;
d6d29896c   Thomas Gleixner   timekeeping: Prov...
495
  	timespec64_add_ns(ts, nsecs);
1e817fb62   Kees Cook   time: create __ge...
496
497
498
499
500
501
502
503
504
  
  	/*
  	 * Do not bail out early, in case there were callers still using
  	 * the value, even in the face of the WARN_ON.
  	 */
  	if (unlikely(timekeeping_suspended))
  		return -EAGAIN;
  	return 0;
  }
d6d29896c   Thomas Gleixner   timekeeping: Prov...
505
  EXPORT_SYMBOL(__getnstimeofday64);
1e817fb62   Kees Cook   time: create __ge...
506
507
  
  /**
d6d29896c   Thomas Gleixner   timekeeping: Prov...
508
   * getnstimeofday64 - Returns the time of day in a timespec64.
5322e4c26   John Stultz   time: Fixup comme...
509
   * @ts:		pointer to the timespec64 to be set
1e817fb62   Kees Cook   time: create __ge...
510
   *
5322e4c26   John Stultz   time: Fixup comme...
511
   * Returns the time of day in a timespec64 (WARN if suspended).
1e817fb62   Kees Cook   time: create __ge...
512
   */
d6d29896c   Thomas Gleixner   timekeeping: Prov...
513
  void getnstimeofday64(struct timespec64 *ts)
1e817fb62   Kees Cook   time: create __ge...
514
  {
d6d29896c   Thomas Gleixner   timekeeping: Prov...
515
  	WARN_ON(__getnstimeofday64(ts));
8524070b7   John Stultz   Move timekeeping ...
516
  }
d6d29896c   Thomas Gleixner   timekeeping: Prov...
517
  EXPORT_SYMBOL(getnstimeofday64);
8524070b7   John Stultz   Move timekeeping ...
518

951ed4d36   Martin Schwidefsky   timekeeping: opti...
519
520
  ktime_t ktime_get(void)
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
521
  	struct timekeeper *tk = &tk_core.timekeeper;
951ed4d36   Martin Schwidefsky   timekeeping: opti...
522
  	unsigned int seq;
a016a5bd6   Thomas Gleixner   timekeeping: Use ...
523
524
  	ktime_t base;
  	s64 nsecs;
951ed4d36   Martin Schwidefsky   timekeeping: opti...
525
526
527
528
  
  	WARN_ON(timekeeping_suspended);
  
  	do {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
529
  		seq = read_seqcount_begin(&tk_core.seq);
d28ede837   Thomas Gleixner   timekeeping: Crea...
530
  		base = tk->tkr.base_mono;
0e5ac3a8b   Thomas Gleixner   timekeeping: Use ...
531
  		nsecs = timekeeping_get_ns(&tk->tkr);
951ed4d36   Martin Schwidefsky   timekeeping: opti...
532

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
533
  	} while (read_seqcount_retry(&tk_core.seq, seq));
24e4a8c3e   John Stultz   ktime: Kill non-s...
534

a016a5bd6   Thomas Gleixner   timekeeping: Use ...
535
  	return ktime_add_ns(base, nsecs);
951ed4d36   Martin Schwidefsky   timekeeping: opti...
536
537
  }
  EXPORT_SYMBOL_GPL(ktime_get);
0077dc60f   Thomas Gleixner   timekeeping: Prov...
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
  static ktime_t *offsets[TK_OFFS_MAX] = {
  	[TK_OFFS_REAL]	= &tk_core.timekeeper.offs_real,
  	[TK_OFFS_BOOT]	= &tk_core.timekeeper.offs_boot,
  	[TK_OFFS_TAI]	= &tk_core.timekeeper.offs_tai,
  };
  
  ktime_t ktime_get_with_offset(enum tk_offsets offs)
  {
  	struct timekeeper *tk = &tk_core.timekeeper;
  	unsigned int seq;
  	ktime_t base, *offset = offsets[offs];
  	s64 nsecs;
  
  	WARN_ON(timekeeping_suspended);
  
  	do {
  		seq = read_seqcount_begin(&tk_core.seq);
d28ede837   Thomas Gleixner   timekeeping: Crea...
555
  		base = ktime_add(tk->tkr.base_mono, *offset);
0e5ac3a8b   Thomas Gleixner   timekeeping: Use ...
556
  		nsecs = timekeeping_get_ns(&tk->tkr);
0077dc60f   Thomas Gleixner   timekeeping: Prov...
557
558
559
560
561
562
563
  
  	} while (read_seqcount_retry(&tk_core.seq, seq));
  
  	return ktime_add_ns(base, nsecs);
  
  }
  EXPORT_SYMBOL_GPL(ktime_get_with_offset);
951ed4d36   Martin Schwidefsky   timekeeping: opti...
564
  /**
9a6b51976   Thomas Gleixner   timekeeping: Prov...
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
   * ktime_mono_to_any() - convert mononotic time to any other time
   * @tmono:	time to convert.
   * @offs:	which offset to use
   */
  ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
  {
  	ktime_t *offset = offsets[offs];
  	unsigned long seq;
  	ktime_t tconv;
  
  	do {
  		seq = read_seqcount_begin(&tk_core.seq);
  		tconv = ktime_add(tmono, *offset);
  	} while (read_seqcount_retry(&tk_core.seq, seq));
  
  	return tconv;
  }
  EXPORT_SYMBOL_GPL(ktime_mono_to_any);
  
  /**
f519b1a2e   Thomas Gleixner   timekeeping: Prov...
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
   * ktime_get_raw - Returns the raw monotonic time in ktime_t format
   */
  ktime_t ktime_get_raw(void)
  {
  	struct timekeeper *tk = &tk_core.timekeeper;
  	unsigned int seq;
  	ktime_t base;
  	s64 nsecs;
  
  	do {
  		seq = read_seqcount_begin(&tk_core.seq);
  		base = tk->base_raw;
  		nsecs = timekeeping_get_ns_raw(tk);
  
  	} while (read_seqcount_retry(&tk_core.seq, seq));
  
  	return ktime_add_ns(base, nsecs);
  }
  EXPORT_SYMBOL_GPL(ktime_get_raw);
  
  /**
d6d29896c   Thomas Gleixner   timekeeping: Prov...
606
   * ktime_get_ts64 - get the monotonic clock in timespec64 format
951ed4d36   Martin Schwidefsky   timekeeping: opti...
607
608
609
610
   * @ts:		pointer to timespec variable
   *
   * The function calculates the monotonic clock from the realtime
   * clock and the wall_to_monotonic offset and stores the result
5322e4c26   John Stultz   time: Fixup comme...
611
   * in normalized timespec64 format in the variable pointed to by @ts.
951ed4d36   Martin Schwidefsky   timekeeping: opti...
612
   */
d6d29896c   Thomas Gleixner   timekeeping: Prov...
613
  void ktime_get_ts64(struct timespec64 *ts)
951ed4d36   Martin Schwidefsky   timekeeping: opti...
614
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
615
  	struct timekeeper *tk = &tk_core.timekeeper;
d6d29896c   Thomas Gleixner   timekeeping: Prov...
616
  	struct timespec64 tomono;
ec145babe   John Stultz   time: Fix timeeke...
617
  	s64 nsec;
951ed4d36   Martin Schwidefsky   timekeeping: opti...
618
  	unsigned int seq;
951ed4d36   Martin Schwidefsky   timekeeping: opti...
619
620
621
622
  
  	WARN_ON(timekeeping_suspended);
  
  	do {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
623
  		seq = read_seqcount_begin(&tk_core.seq);
d6d29896c   Thomas Gleixner   timekeeping: Prov...
624
  		ts->tv_sec = tk->xtime_sec;
0e5ac3a8b   Thomas Gleixner   timekeeping: Use ...
625
  		nsec = timekeeping_get_ns(&tk->tkr);
4e250fdde   John Stultz   time: Remove all ...
626
  		tomono = tk->wall_to_monotonic;
951ed4d36   Martin Schwidefsky   timekeeping: opti...
627

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
628
  	} while (read_seqcount_retry(&tk_core.seq, seq));
951ed4d36   Martin Schwidefsky   timekeeping: opti...
629

d6d29896c   Thomas Gleixner   timekeeping: Prov...
630
631
632
  	ts->tv_sec += tomono.tv_sec;
  	ts->tv_nsec = 0;
  	timespec64_add_ns(ts, nsec + tomono.tv_nsec);
951ed4d36   Martin Schwidefsky   timekeeping: opti...
633
  }
d6d29896c   Thomas Gleixner   timekeeping: Prov...
634
  EXPORT_SYMBOL_GPL(ktime_get_ts64);
951ed4d36   Martin Schwidefsky   timekeeping: opti...
635

9e3680b17   Heena Sirwani   timekeeping: Prov...
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
  /**
   * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC
   *
   * Returns the seconds portion of CLOCK_MONOTONIC with a single non
   * serialized read. tk->ktime_sec is of type 'unsigned long' so this
   * works on both 32 and 64 bit systems. On 32 bit systems the readout
   * covers ~136 years of uptime which should be enough to prevent
   * premature wrap arounds.
   */
  time64_t ktime_get_seconds(void)
  {
  	struct timekeeper *tk = &tk_core.timekeeper;
  
  	WARN_ON(timekeeping_suspended);
  	return tk->ktime_sec;
  }
  EXPORT_SYMBOL_GPL(ktime_get_seconds);
dbe7aa622   Heena Sirwani   timekeeping: Prov...
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
  /**
   * ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME
   *
   * Returns the wall clock seconds since 1970. This replaces the
   * get_seconds() interface which is not y2038 safe on 32bit systems.
   *
   * For 64bit systems the fast access to tk->xtime_sec is preserved. On
   * 32bit systems the access must be protected with the sequence
   * counter to provide "atomic" access to the 64bit tk->xtime_sec
   * value.
   */
  time64_t ktime_get_real_seconds(void)
  {
  	struct timekeeper *tk = &tk_core.timekeeper;
  	time64_t seconds;
  	unsigned int seq;
  
  	if (IS_ENABLED(CONFIG_64BIT))
  		return tk->xtime_sec;
  
  	do {
  		seq = read_seqcount_begin(&tk_core.seq);
  		seconds = tk->xtime_sec;
  
  	} while (read_seqcount_retry(&tk_core.seq, seq));
  
  	return seconds;
  }
  EXPORT_SYMBOL_GPL(ktime_get_real_seconds);
e2c18e49a   Alexander Gordeev   pps: capture MONO...
682
683
684
685
686
687
688
689
690
691
692
693
694
  #ifdef CONFIG_NTP_PPS
  
  /**
   * getnstime_raw_and_real - get day and raw monotonic time in timespec format
   * @ts_raw:	pointer to the timespec to be set to raw monotonic time
   * @ts_real:	pointer to the timespec to be set to the time of day
   *
   * This function reads both the time of day and raw monotonic time at the
   * same time atomically and stores the resulting timestamps in timespec
   * format.
   */
  void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
695
  	struct timekeeper *tk = &tk_core.timekeeper;
e2c18e49a   Alexander Gordeev   pps: capture MONO...
696
697
698
699
700
701
  	unsigned long seq;
  	s64 nsecs_raw, nsecs_real;
  
  	WARN_ON_ONCE(timekeeping_suspended);
  
  	do {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
702
  		seq = read_seqcount_begin(&tk_core.seq);
e2c18e49a   Alexander Gordeev   pps: capture MONO...
703

7d489d15c   John Stultz   timekeeping: Conv...
704
  		*ts_raw = timespec64_to_timespec(tk->raw_time);
4e250fdde   John Stultz   time: Remove all ...
705
  		ts_real->tv_sec = tk->xtime_sec;
1e75fa8be   John Stultz   time: Condense ti...
706
  		ts_real->tv_nsec = 0;
e2c18e49a   Alexander Gordeev   pps: capture MONO...
707

4e250fdde   John Stultz   time: Remove all ...
708
  		nsecs_raw = timekeeping_get_ns_raw(tk);
0e5ac3a8b   Thomas Gleixner   timekeeping: Use ...
709
  		nsecs_real = timekeeping_get_ns(&tk->tkr);
e2c18e49a   Alexander Gordeev   pps: capture MONO...
710

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
711
  	} while (read_seqcount_retry(&tk_core.seq, seq));
e2c18e49a   Alexander Gordeev   pps: capture MONO...
712
713
714
715
716
717
718
  
  	timespec_add_ns(ts_raw, nsecs_raw);
  	timespec_add_ns(ts_real, nsecs_real);
  }
  EXPORT_SYMBOL(getnstime_raw_and_real);
  
  #endif /* CONFIG_NTP_PPS */
8524070b7   John Stultz   Move timekeeping ...
719
720
721
722
  /**
   * do_gettimeofday - Returns the time of day in a timeval
   * @tv:		pointer to the timeval to be set
   *
efd9ac863   Geert Uytterhoeven   time: fold __get_...
723
   * NOTE: Users should be converted to using getnstimeofday()
8524070b7   John Stultz   Move timekeeping ...
724
725
726
   */
  void do_gettimeofday(struct timeval *tv)
  {
d6d29896c   Thomas Gleixner   timekeeping: Prov...
727
  	struct timespec64 now;
8524070b7   John Stultz   Move timekeeping ...
728

d6d29896c   Thomas Gleixner   timekeeping: Prov...
729
  	getnstimeofday64(&now);
8524070b7   John Stultz   Move timekeeping ...
730
731
732
  	tv->tv_sec = now.tv_sec;
  	tv->tv_usec = now.tv_nsec/1000;
  }
8524070b7   John Stultz   Move timekeeping ...
733
  EXPORT_SYMBOL(do_gettimeofday);
d239f49d7   Richard Cochran   timekeeping: Fix ...
734

8524070b7   John Stultz   Move timekeeping ...
735
  /**
21f7eca55   pang.xunlei   time: Provide y20...
736
737
   * do_settimeofday64 - Sets the time of day.
   * @ts:     pointer to the timespec64 variable containing the new time
8524070b7   John Stultz   Move timekeeping ...
738
739
740
   *
   * Sets the time of day to the new time and update NTP and notify hrtimers
   */
21f7eca55   pang.xunlei   time: Provide y20...
741
  int do_settimeofday64(const struct timespec64 *ts)
8524070b7   John Stultz   Move timekeeping ...
742
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
743
  	struct timekeeper *tk = &tk_core.timekeeper;
21f7eca55   pang.xunlei   time: Provide y20...
744
  	struct timespec64 ts_delta, xt;
92c1d3ed4   John Stultz   time: Remove most...
745
  	unsigned long flags;
8524070b7   John Stultz   Move timekeeping ...
746

21f7eca55   pang.xunlei   time: Provide y20...
747
  	if (!timespec64_valid_strict(ts))
8524070b7   John Stultz   Move timekeeping ...
748
  		return -EINVAL;
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
749
  	raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
750
  	write_seqcount_begin(&tk_core.seq);
8524070b7   John Stultz   Move timekeeping ...
751

4e250fdde   John Stultz   time: Remove all ...
752
  	timekeeping_forward_now(tk);
9a055117d   Roman Zippel   clocksource: intr...
753

4e250fdde   John Stultz   time: Remove all ...
754
  	xt = tk_xtime(tk);
21f7eca55   pang.xunlei   time: Provide y20...
755
756
  	ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
  	ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
1e75fa8be   John Stultz   time: Condense ti...
757

7d489d15c   John Stultz   timekeeping: Conv...
758
  	tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
8524070b7   John Stultz   Move timekeeping ...
759

21f7eca55   pang.xunlei   time: Provide y20...
760
  	tk_set_xtime(tk, ts);
1e75fa8be   John Stultz   time: Condense ti...
761

780427f0e   David Vrabel   timekeeping: Indi...
762
  	timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
8524070b7   John Stultz   Move timekeeping ...
763

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
764
  	write_seqcount_end(&tk_core.seq);
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
765
  	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
8524070b7   John Stultz   Move timekeeping ...
766
767
768
769
770
771
  
  	/* signal hrtimers about time change */
  	clock_was_set();
  
  	return 0;
  }
21f7eca55   pang.xunlei   time: Provide y20...
772
  EXPORT_SYMBOL(do_settimeofday64);
8524070b7   John Stultz   Move timekeeping ...
773

c528f7c6c   John Stultz   time: Introduce t...
774
775
776
777
778
779
780
781
  /**
   * timekeeping_inject_offset - Adds or subtracts from the current time.
   * @tv:		pointer to the timespec variable containing the offset
   *
   * Adds or subtracts an offset value from the current time.
   */
  int timekeeping_inject_offset(struct timespec *ts)
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
782
  	struct timekeeper *tk = &tk_core.timekeeper;
92c1d3ed4   John Stultz   time: Remove most...
783
  	unsigned long flags;
7d489d15c   John Stultz   timekeeping: Conv...
784
  	struct timespec64 ts64, tmp;
4e8b14526   John Stultz   time: Improve san...
785
  	int ret = 0;
c528f7c6c   John Stultz   time: Introduce t...
786
787
788
  
  	if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
  		return -EINVAL;
7d489d15c   John Stultz   timekeeping: Conv...
789
  	ts64 = timespec_to_timespec64(*ts);
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
790
  	raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
791
  	write_seqcount_begin(&tk_core.seq);
c528f7c6c   John Stultz   time: Introduce t...
792

4e250fdde   John Stultz   time: Remove all ...
793
  	timekeeping_forward_now(tk);
c528f7c6c   John Stultz   time: Introduce t...
794

4e8b14526   John Stultz   time: Improve san...
795
  	/* Make sure the proposed value is valid */
7d489d15c   John Stultz   timekeeping: Conv...
796
797
  	tmp = timespec64_add(tk_xtime(tk),  ts64);
  	if (!timespec64_valid_strict(&tmp)) {
4e8b14526   John Stultz   time: Improve san...
798
799
800
  		ret = -EINVAL;
  		goto error;
  	}
1e75fa8be   John Stultz   time: Condense ti...
801

7d489d15c   John Stultz   timekeeping: Conv...
802
803
  	tk_xtime_add(tk, &ts64);
  	tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts64));
c528f7c6c   John Stultz   time: Introduce t...
804

4e8b14526   John Stultz   time: Improve san...
805
  error: /* even if we error out, we forwarded the time, so call update */
780427f0e   David Vrabel   timekeeping: Indi...
806
  	timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
c528f7c6c   John Stultz   time: Introduce t...
807

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
808
  	write_seqcount_end(&tk_core.seq);
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
809
  	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
c528f7c6c   John Stultz   time: Introduce t...
810
811
812
  
  	/* signal hrtimers about time change */
  	clock_was_set();
4e8b14526   John Stultz   time: Improve san...
813
  	return ret;
c528f7c6c   John Stultz   time: Introduce t...
814
815
  }
  EXPORT_SYMBOL(timekeeping_inject_offset);
cc244ddae   John Stultz   timekeeping: Move...
816
817
818
819
820
821
822
  
  /**
   * timekeeping_get_tai_offset - Returns current TAI offset from UTC
   *
   */
  s32 timekeeping_get_tai_offset(void)
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
823
  	struct timekeeper *tk = &tk_core.timekeeper;
cc244ddae   John Stultz   timekeeping: Move...
824
825
826
827
  	unsigned int seq;
  	s32 ret;
  
  	do {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
828
  		seq = read_seqcount_begin(&tk_core.seq);
cc244ddae   John Stultz   timekeeping: Move...
829
  		ret = tk->tai_offset;
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
830
  	} while (read_seqcount_retry(&tk_core.seq, seq));
cc244ddae   John Stultz   timekeeping: Move...
831
832
833
834
835
836
837
838
  
  	return ret;
  }
  
  /**
   * __timekeeping_set_tai_offset - Lock free worker function
   *
   */
dd5d70e86   Fengguang Wu   timekeeping: __ti...
839
  static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
cc244ddae   John Stultz   timekeeping: Move...
840
841
  {
  	tk->tai_offset = tai_offset;
04005f601   John Stultz   timekeeping: Fix ...
842
  	tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0));
cc244ddae   John Stultz   timekeeping: Move...
843
844
845
846
847
848
849
850
  }
  
  /**
   * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
   *
   */
  void timekeeping_set_tai_offset(s32 tai_offset)
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
851
  	struct timekeeper *tk = &tk_core.timekeeper;
cc244ddae   John Stultz   timekeeping: Move...
852
  	unsigned long flags;
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
853
  	raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
854
  	write_seqcount_begin(&tk_core.seq);
cc244ddae   John Stultz   timekeeping: Move...
855
  	__timekeeping_set_tai_offset(tk, tai_offset);
f55c07607   John Stultz   timekeeping: Fix ...
856
  	timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
857
  	write_seqcount_end(&tk_core.seq);
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
858
  	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
4e8f8b34b   John Stultz   timekeeping: Make...
859
  	clock_was_set();
cc244ddae   John Stultz   timekeeping: Move...
860
  }
8524070b7   John Stultz   Move timekeeping ...
861
862
863
864
865
  /**
   * change_clocksource - Swaps clocksources if a new one is available
   *
   * Accumulates current time interval and initializes new clocksource
   */
75c5158f7   Martin Schwidefsky   timekeeping: Upda...
866
  static int change_clocksource(void *data)
8524070b7   John Stultz   Move timekeeping ...
867
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
868
  	struct timekeeper *tk = &tk_core.timekeeper;
4614e6ada   Magnus Damm   clocksource: add ...
869
  	struct clocksource *new, *old;
f695cf948   John Stultz   time: Fix change_...
870
  	unsigned long flags;
8524070b7   John Stultz   Move timekeeping ...
871

75c5158f7   Martin Schwidefsky   timekeeping: Upda...
872
  	new = (struct clocksource *) data;
8524070b7   John Stultz   Move timekeeping ...
873

9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
874
  	raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
875
  	write_seqcount_begin(&tk_core.seq);
f695cf948   John Stultz   time: Fix change_...
876

4e250fdde   John Stultz   time: Remove all ...
877
  	timekeeping_forward_now(tk);
09ac369c8   Thomas Gleixner   clocksource: Add ...
878
879
880
881
882
883
  	/*
  	 * If the cs is in module, get a module reference. Succeeds
  	 * for built-in code (owner == NULL) as well.
  	 */
  	if (try_module_get(new->owner)) {
  		if (!new->enable || new->enable(new) == 0) {
d28ede837   Thomas Gleixner   timekeeping: Crea...
884
  			old = tk->tkr.clock;
09ac369c8   Thomas Gleixner   clocksource: Add ...
885
886
887
888
889
890
891
  			tk_setup_internals(tk, new);
  			if (old->disable)
  				old->disable(old);
  			module_put(old->owner);
  		} else {
  			module_put(new->owner);
  		}
75c5158f7   Martin Schwidefsky   timekeeping: Upda...
892
  	}
780427f0e   David Vrabel   timekeeping: Indi...
893
  	timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
f695cf948   John Stultz   time: Fix change_...
894

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
895
  	write_seqcount_end(&tk_core.seq);
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
896
  	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
f695cf948   John Stultz   time: Fix change_...
897

75c5158f7   Martin Schwidefsky   timekeeping: Upda...
898
899
  	return 0;
  }
8524070b7   John Stultz   Move timekeeping ...
900

75c5158f7   Martin Schwidefsky   timekeeping: Upda...
901
902
903
904
905
906
907
  /**
   * timekeeping_notify - Install a new clock source
   * @clock:		pointer to the clock source
   *
   * This function is called from clocksource.c after a new, better clock
   * source has been registered. The caller holds the clocksource_mutex.
   */
ba919d1ca   Thomas Gleixner   clocksource: Let ...
908
  int timekeeping_notify(struct clocksource *clock)
75c5158f7   Martin Schwidefsky   timekeeping: Upda...
909
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
910
  	struct timekeeper *tk = &tk_core.timekeeper;
4e250fdde   John Stultz   time: Remove all ...
911

d28ede837   Thomas Gleixner   timekeeping: Crea...
912
  	if (tk->tkr.clock == clock)
ba919d1ca   Thomas Gleixner   clocksource: Let ...
913
  		return 0;
75c5158f7   Martin Schwidefsky   timekeeping: Upda...
914
  	stop_machine(change_clocksource, clock, NULL);
8524070b7   John Stultz   Move timekeeping ...
915
  	tick_clock_notify();
d28ede837   Thomas Gleixner   timekeeping: Crea...
916
  	return tk->tkr.clock == clock ? 0 : -1;
8524070b7   John Stultz   Move timekeeping ...
917
  }
75c5158f7   Martin Schwidefsky   timekeeping: Upda...
918

a40f262cc   Thomas Gleixner   timekeeping: Move...
919
  /**
cdba2ec53   John Stultz   time: Expose getr...
920
921
   * getrawmonotonic64 - Returns the raw monotonic time in a timespec
   * @ts:		pointer to the timespec64 to be set
2d42244ae   John Stultz   clocksource: intr...
922
923
924
   *
   * Returns the raw monotonic time (completely un-modified by ntp)
   */
cdba2ec53   John Stultz   time: Expose getr...
925
  void getrawmonotonic64(struct timespec64 *ts)
2d42244ae   John Stultz   clocksource: intr...
926
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
927
  	struct timekeeper *tk = &tk_core.timekeeper;
7d489d15c   John Stultz   timekeeping: Conv...
928
  	struct timespec64 ts64;
2d42244ae   John Stultz   clocksource: intr...
929
930
  	unsigned long seq;
  	s64 nsecs;
2d42244ae   John Stultz   clocksource: intr...
931
932
  
  	do {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
933
  		seq = read_seqcount_begin(&tk_core.seq);
4e250fdde   John Stultz   time: Remove all ...
934
  		nsecs = timekeeping_get_ns_raw(tk);
7d489d15c   John Stultz   timekeeping: Conv...
935
  		ts64 = tk->raw_time;
2d42244ae   John Stultz   clocksource: intr...
936

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
937
  	} while (read_seqcount_retry(&tk_core.seq, seq));
2d42244ae   John Stultz   clocksource: intr...
938

7d489d15c   John Stultz   timekeeping: Conv...
939
  	timespec64_add_ns(&ts64, nsecs);
cdba2ec53   John Stultz   time: Expose getr...
940
  	*ts = ts64;
2d42244ae   John Stultz   clocksource: intr...
941
  }
cdba2ec53   John Stultz   time: Expose getr...
942
  EXPORT_SYMBOL(getrawmonotonic64);
2d42244ae   John Stultz   clocksource: intr...
943

2d42244ae   John Stultz   clocksource: intr...
944
  /**
cf4fc6cb7   Li Zefan   timekeeping: rena...
945
   * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
8524070b7   John Stultz   Move timekeeping ...
946
   */
cf4fc6cb7   Li Zefan   timekeeping: rena...
947
  int timekeeping_valid_for_hres(void)
8524070b7   John Stultz   Move timekeeping ...
948
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
949
  	struct timekeeper *tk = &tk_core.timekeeper;
8524070b7   John Stultz   Move timekeeping ...
950
951
952
953
  	unsigned long seq;
  	int ret;
  
  	do {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
954
  		seq = read_seqcount_begin(&tk_core.seq);
8524070b7   John Stultz   Move timekeeping ...
955

d28ede837   Thomas Gleixner   timekeeping: Crea...
956
  		ret = tk->tkr.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
8524070b7   John Stultz   Move timekeeping ...
957

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
958
  	} while (read_seqcount_retry(&tk_core.seq, seq));
8524070b7   John Stultz   Move timekeeping ...
959
960
961
962
963
  
  	return ret;
  }
  
  /**
98962465e   Jon Hunter   nohz: Prevent clo...
964
   * timekeeping_max_deferment - Returns max time the clocksource can be deferred
98962465e   Jon Hunter   nohz: Prevent clo...
965
966
967
   */
  u64 timekeeping_max_deferment(void)
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
968
  	struct timekeeper *tk = &tk_core.timekeeper;
70471f2f0   John Stultz   time: Add timekee...
969
970
  	unsigned long seq;
  	u64 ret;
42e71e81f   John Stultz   time: Whitespace ...
971

70471f2f0   John Stultz   time: Add timekee...
972
  	do {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
973
  		seq = read_seqcount_begin(&tk_core.seq);
70471f2f0   John Stultz   time: Add timekee...
974

d28ede837   Thomas Gleixner   timekeeping: Crea...
975
  		ret = tk->tkr.clock->max_idle_ns;
70471f2f0   John Stultz   time: Add timekee...
976

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
977
  	} while (read_seqcount_retry(&tk_core.seq, seq));
70471f2f0   John Stultz   time: Add timekee...
978
979
  
  	return ret;
98962465e   Jon Hunter   nohz: Prevent clo...
980
981
982
  }
  
  /**
d4f587c67   Martin Schwidefsky   timekeeping: Incr...
983
   * read_persistent_clock -  Return time from the persistent clock.
8524070b7   John Stultz   Move timekeeping ...
984
985
   *
   * Weak dummy function for arches that do not yet support it.
d4f587c67   Martin Schwidefsky   timekeeping: Incr...
986
987
   * Reads the time from the battery backed persistent clock.
   * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
8524070b7   John Stultz   Move timekeeping ...
988
989
990
   *
   *  XXX - Do be sure to remove it once all arches implement it.
   */
52f5684c8   Gideon Israel Dsouza   kernel: use macro...
991
  void __weak read_persistent_clock(struct timespec *ts)
8524070b7   John Stultz   Move timekeeping ...
992
  {
d4f587c67   Martin Schwidefsky   timekeeping: Incr...
993
994
  	ts->tv_sec = 0;
  	ts->tv_nsec = 0;
8524070b7   John Stultz   Move timekeeping ...
995
  }
23970e389   Martin Schwidefsky   timekeeping: Intr...
996
997
998
999
1000
1001
1002
1003
1004
  /**
   * read_boot_clock -  Return time of the system start.
   *
   * Weak dummy function for arches that do not yet support it.
   * Function to read the exact time the system has been started.
   * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
   *
   *  XXX - Do be sure to remove it once all arches implement it.
   */
52f5684c8   Gideon Israel Dsouza   kernel: use macro...
1005
  void __weak read_boot_clock(struct timespec *ts)
23970e389   Martin Schwidefsky   timekeeping: Intr...
1006
1007
1008
1009
  {
  	ts->tv_sec = 0;
  	ts->tv_nsec = 0;
  }
8524070b7   John Stultz   Move timekeeping ...
1010
1011
1012
1013
1014
  /*
   * timekeeping_init - Initializes the clocksource and common timekeeping values
   */
  void __init timekeeping_init(void)
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1015
  	struct timekeeper *tk = &tk_core.timekeeper;
155ec6022   Martin Schwidefsky   timekeeping: Intr...
1016
  	struct clocksource *clock;
8524070b7   John Stultz   Move timekeeping ...
1017
  	unsigned long flags;
7d489d15c   John Stultz   timekeeping: Conv...
1018
1019
  	struct timespec64 now, boot, tmp;
  	struct timespec ts;
31ade3069   Feng Tang   timekeeping: Add ...
1020

7d489d15c   John Stultz   timekeeping: Conv...
1021
1022
1023
  	read_persistent_clock(&ts);
  	now = timespec_to_timespec64(ts);
  	if (!timespec64_valid_strict(&now)) {
4e8b14526   John Stultz   time: Improve san...
1024
1025
1026
1027
1028
1029
  		pr_warn("WARNING: Persistent clock returned invalid value!
  "
  			"         Check your CMOS/BIOS settings.
  ");
  		now.tv_sec = 0;
  		now.tv_nsec = 0;
31ade3069   Feng Tang   timekeeping: Add ...
1030
1031
  	} else if (now.tv_sec || now.tv_nsec)
  		persistent_clock_exist = true;
4e8b14526   John Stultz   time: Improve san...
1032

7d489d15c   John Stultz   timekeeping: Conv...
1033
1034
1035
  	read_boot_clock(&ts);
  	boot = timespec_to_timespec64(ts);
  	if (!timespec64_valid_strict(&boot)) {
4e8b14526   John Stultz   time: Improve san...
1036
1037
1038
1039
1040
1041
1042
  		pr_warn("WARNING: Boot clock returned invalid value!
  "
  			"         Check your CMOS/BIOS settings.
  ");
  		boot.tv_sec = 0;
  		boot.tv_nsec = 0;
  	}
8524070b7   John Stultz   Move timekeeping ...
1043

9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
1044
  	raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1045
  	write_seqcount_begin(&tk_core.seq);
06c017fdd   John Stultz   timekeeping: Hold...
1046
  	ntp_init();
f1b82746c   Martin Schwidefsky   clocksource: Clea...
1047
  	clock = clocksource_default_clock();
a0f7d48bf   Martin Schwidefsky   timekeeping: Remo...
1048
1049
  	if (clock->enable)
  		clock->enable(clock);
4e250fdde   John Stultz   time: Remove all ...
1050
  	tk_setup_internals(tk, clock);
8524070b7   John Stultz   Move timekeeping ...
1051

4e250fdde   John Stultz   time: Remove all ...
1052
1053
1054
  	tk_set_xtime(tk, &now);
  	tk->raw_time.tv_sec = 0;
  	tk->raw_time.tv_nsec = 0;
f519b1a2e   Thomas Gleixner   timekeeping: Prov...
1055
  	tk->base_raw.tv64 = 0;
1e75fa8be   John Stultz   time: Condense ti...
1056
  	if (boot.tv_sec == 0 && boot.tv_nsec == 0)
4e250fdde   John Stultz   time: Remove all ...
1057
  		boot = tk_xtime(tk);
1e75fa8be   John Stultz   time: Condense ti...
1058

7d489d15c   John Stultz   timekeeping: Conv...
1059
  	set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec);
4e250fdde   John Stultz   time: Remove all ...
1060
  	tk_set_wall_to_mono(tk, tmp);
6d0ef903e   John Stultz   time: Clean up of...
1061

f111adfdd   Thomas Gleixner   timekeeping: Use ...
1062
  	timekeeping_update(tk, TK_MIRROR);
48cdc135d   Thomas Gleixner   timekeeping: Impl...
1063

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1064
  	write_seqcount_end(&tk_core.seq);
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
1065
  	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
8524070b7   John Stultz   Move timekeeping ...
1066
  }
8524070b7   John Stultz   Move timekeeping ...
1067
  /* time in seconds when suspend began */
7d489d15c   John Stultz   timekeeping: Conv...
1068
  static struct timespec64 timekeeping_suspend_time;
8524070b7   John Stultz   Move timekeeping ...
1069
1070
  
  /**
304529b1b   John Stultz   time: Add timekee...
1071
1072
1073
1074
1075
1076
   * __timekeeping_inject_sleeptime - Internal function to add sleep interval
   * @delta: pointer to a timespec delta value
   *
   * Takes a timespec offset measuring a suspend interval and properly
   * adds the sleep offset to the timekeeping variables.
   */
f726a697d   John Stultz   time: Rework time...
1077
  static void __timekeeping_inject_sleeptime(struct timekeeper *tk,
7d489d15c   John Stultz   timekeeping: Conv...
1078
  					   struct timespec64 *delta)
304529b1b   John Stultz   time: Add timekee...
1079
  {
7d489d15c   John Stultz   timekeeping: Conv...
1080
  	if (!timespec64_valid_strict(delta)) {
6d9bcb621   John Stultz   timekeeping: use ...
1081
1082
1083
1084
  		printk_deferred(KERN_WARNING
  				"__timekeeping_inject_sleeptime: Invalid "
  				"sleep delta value!
  ");
cb5de2f8d   John Stultz   time: Catch inval...
1085
1086
  		return;
  	}
f726a697d   John Stultz   time: Rework time...
1087
  	tk_xtime_add(tk, delta);
7d489d15c   John Stultz   timekeeping: Conv...
1088
  	tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
47da70d32   Thomas Gleixner   timekeeping: Remo...
1089
  	tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
5c83545f2   Colin Cross   power: Add option...
1090
  	tk_debug_account_sleep_time(delta);
304529b1b   John Stultz   time: Add timekee...
1091
  }
304529b1b   John Stultz   time: Add timekee...
1092
  /**
04d908908   pang.xunlei   time: Provide y20...
1093
1094
   * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values
   * @delta: pointer to a timespec64 delta value
304529b1b   John Stultz   time: Add timekee...
1095
1096
1097
1098
1099
1100
1101
   *
   * This hook is for architectures that cannot support read_persistent_clock
   * because their RTC/persistent clock is only accessible when irqs are enabled.
   *
   * This function should only be called by rtc_resume(), and allows
   * a suspend offset to be injected into the timekeeping values.
   */
04d908908   pang.xunlei   time: Provide y20...
1102
  void timekeeping_inject_sleeptime64(struct timespec64 *delta)
304529b1b   John Stultz   time: Add timekee...
1103
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1104
  	struct timekeeper *tk = &tk_core.timekeeper;
92c1d3ed4   John Stultz   time: Remove most...
1105
  	unsigned long flags;
304529b1b   John Stultz   time: Add timekee...
1106

31ade3069   Feng Tang   timekeeping: Add ...
1107
1108
1109
1110
1111
  	/*
  	 * Make sure we don't set the clock twice, as timekeeping_resume()
  	 * already did it
  	 */
  	if (has_persistent_clock())
304529b1b   John Stultz   time: Add timekee...
1112
  		return;
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
1113
  	raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1114
  	write_seqcount_begin(&tk_core.seq);
70471f2f0   John Stultz   time: Add timekee...
1115

4e250fdde   John Stultz   time: Remove all ...
1116
  	timekeeping_forward_now(tk);
304529b1b   John Stultz   time: Add timekee...
1117

04d908908   pang.xunlei   time: Provide y20...
1118
  	__timekeeping_inject_sleeptime(tk, delta);
304529b1b   John Stultz   time: Add timekee...
1119

780427f0e   David Vrabel   timekeeping: Indi...
1120
  	timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
304529b1b   John Stultz   time: Add timekee...
1121

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1122
  	write_seqcount_end(&tk_core.seq);
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
1123
  	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
304529b1b   John Stultz   time: Add timekee...
1124
1125
1126
1127
  
  	/* signal hrtimers about time change */
  	clock_was_set();
  }
304529b1b   John Stultz   time: Add timekee...
1128
  /**
8524070b7   John Stultz   Move timekeeping ...
1129
   * timekeeping_resume - Resumes the generic timekeeping subsystem.
8524070b7   John Stultz   Move timekeeping ...
1130
1131
1132
1133
1134
   *
   * This is for the generic clocksource timekeeping.
   * xtime/wall_to_monotonic/jiffies/etc are
   * still managed by arch specific suspend/resume code.
   */
e1a85b2c5   Rafael J. Wysocki   timekeeping: Use ...
1135
  static void timekeeping_resume(void)
8524070b7   John Stultz   Move timekeeping ...
1136
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1137
  	struct timekeeper *tk = &tk_core.timekeeper;
d28ede837   Thomas Gleixner   timekeeping: Crea...
1138
  	struct clocksource *clock = tk->tkr.clock;
92c1d3ed4   John Stultz   time: Remove most...
1139
  	unsigned long flags;
7d489d15c   John Stultz   timekeeping: Conv...
1140
1141
  	struct timespec64 ts_new, ts_delta;
  	struct timespec tmp;
e445cf1c4   Feng Tang   timekeeping: util...
1142
1143
  	cycle_t cycle_now, cycle_delta;
  	bool suspendtime_found = false;
d4f587c67   Martin Schwidefsky   timekeeping: Incr...
1144

7d489d15c   John Stultz   timekeeping: Conv...
1145
1146
  	read_persistent_clock(&tmp);
  	ts_new = timespec_to_timespec64(tmp);
8524070b7   John Stultz   Move timekeeping ...
1147

adc78e6b9   Rafael J. Wysocki   timekeeping: Add ...
1148
  	clockevents_resume();
d10ff3fb6   Thomas Gleixner   timekeeping fix p...
1149
  	clocksource_resume();
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
1150
  	raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1151
  	write_seqcount_begin(&tk_core.seq);
8524070b7   John Stultz   Move timekeeping ...
1152

e445cf1c4   Feng Tang   timekeeping: util...
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
  	/*
  	 * After system resumes, we need to calculate the suspended time and
  	 * compensate it for the OS time. There are 3 sources that could be
  	 * used: Nonstop clocksource during suspend, persistent clock and rtc
  	 * device.
  	 *
  	 * One specific platform may have 1 or 2 or all of them, and the
  	 * preference will be:
  	 *	suspend-nonstop clocksource -> persistent clock -> rtc
  	 * The less preferred source will only be tried if there is no better
  	 * usable source. The rtc part is handled separately in rtc core code.
  	 */
d28ede837   Thomas Gleixner   timekeeping: Crea...
1165
  	cycle_now = tk->tkr.read(clock);
e445cf1c4   Feng Tang   timekeeping: util...
1166
  	if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
d28ede837   Thomas Gleixner   timekeeping: Crea...
1167
  		cycle_now > tk->tkr.cycle_last) {
e445cf1c4   Feng Tang   timekeeping: util...
1168
1169
1170
1171
  		u64 num, max = ULLONG_MAX;
  		u32 mult = clock->mult;
  		u32 shift = clock->shift;
  		s64 nsec = 0;
d28ede837   Thomas Gleixner   timekeeping: Crea...
1172
1173
  		cycle_delta = clocksource_delta(cycle_now, tk->tkr.cycle_last,
  						tk->tkr.mask);
e445cf1c4   Feng Tang   timekeeping: util...
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
  
  		/*
  		 * "cycle_delta * mutl" may cause 64 bits overflow, if the
  		 * suspended time is too long. In that case we need do the
  		 * 64 bits math carefully
  		 */
  		do_div(max, mult);
  		if (cycle_delta > max) {
  			num = div64_u64(cycle_delta, max);
  			nsec = (((u64) max * mult) >> shift) * num;
  			cycle_delta -= num * max;
  		}
  		nsec += ((u64) cycle_delta * mult) >> shift;
7d489d15c   John Stultz   timekeeping: Conv...
1187
  		ts_delta = ns_to_timespec64(nsec);
e445cf1c4   Feng Tang   timekeeping: util...
1188
  		suspendtime_found = true;
7d489d15c   John Stultz   timekeeping: Conv...
1189
1190
  	} else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
  		ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time);
e445cf1c4   Feng Tang   timekeeping: util...
1191
  		suspendtime_found = true;
8524070b7   John Stultz   Move timekeeping ...
1192
  	}
e445cf1c4   Feng Tang   timekeeping: util...
1193
1194
1195
1196
1197
  
  	if (suspendtime_found)
  		__timekeeping_inject_sleeptime(tk, &ts_delta);
  
  	/* Re-base the last cycle value */
d28ede837   Thomas Gleixner   timekeeping: Crea...
1198
  	tk->tkr.cycle_last = cycle_now;
4e250fdde   John Stultz   time: Remove all ...
1199
  	tk->ntp_error = 0;
8524070b7   John Stultz   Move timekeeping ...
1200
  	timekeeping_suspended = 0;
780427f0e   David Vrabel   timekeeping: Indi...
1201
  	timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1202
  	write_seqcount_end(&tk_core.seq);
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
1203
  	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
8524070b7   John Stultz   Move timekeeping ...
1204
1205
1206
1207
1208
1209
  
  	touch_softlockup_watchdog();
  
  	clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
  
  	/* Resume hrtimers */
b12a03ce4   Thomas Gleixner   hrtimers: Prepare...
1210
  	hrtimers_resume();
8524070b7   John Stultz   Move timekeeping ...
1211
  }
e1a85b2c5   Rafael J. Wysocki   timekeeping: Use ...
1212
  static int timekeeping_suspend(void)
8524070b7   John Stultz   Move timekeeping ...
1213
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1214
  	struct timekeeper *tk = &tk_core.timekeeper;
92c1d3ed4   John Stultz   time: Remove most...
1215
  	unsigned long flags;
7d489d15c   John Stultz   timekeeping: Conv...
1216
1217
1218
  	struct timespec64		delta, delta_delta;
  	static struct timespec64	old_delta;
  	struct timespec tmp;
8524070b7   John Stultz   Move timekeeping ...
1219

7d489d15c   John Stultz   timekeeping: Conv...
1220
1221
  	read_persistent_clock(&tmp);
  	timekeeping_suspend_time = timespec_to_timespec64(tmp);
3be909506   Thomas Gleixner   timekeeping: acce...
1222

0d6bd9953   Zoran Markovic   timekeeping: Corr...
1223
1224
1225
1226
1227
1228
1229
  	/*
  	 * On some systems the persistent_clock can not be detected at
  	 * timekeeping_init by its return value, so if we see a valid
  	 * value returned, update the persistent_clock_exists flag.
  	 */
  	if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec)
  		persistent_clock_exist = true;
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
1230
  	raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1231
  	write_seqcount_begin(&tk_core.seq);
4e250fdde   John Stultz   time: Remove all ...
1232
  	timekeeping_forward_now(tk);
8524070b7   John Stultz   Move timekeeping ...
1233
  	timekeeping_suspended = 1;
cb33217b1   John Stultz   time: Avoid accum...
1234
1235
1236
1237
1238
1239
1240
  
  	/*
  	 * To avoid drift caused by repeated suspend/resumes,
  	 * which each can add ~1 second drift error,
  	 * try to compensate so the difference in system time
  	 * and persistent_clock time stays close to constant.
  	 */
7d489d15c   John Stultz   timekeeping: Conv...
1241
1242
  	delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time);
  	delta_delta = timespec64_sub(delta, old_delta);
cb33217b1   John Stultz   time: Avoid accum...
1243
1244
1245
1246
1247
1248
1249
1250
1251
  	if (abs(delta_delta.tv_sec)  >= 2) {
  		/*
  		 * if delta_delta is too large, assume time correction
  		 * has occured and set old_delta to the current delta.
  		 */
  		old_delta = delta;
  	} else {
  		/* Otherwise try to adjust old_system to compensate */
  		timekeeping_suspend_time =
7d489d15c   John Stultz   timekeeping: Conv...
1252
  			timespec64_add(timekeeping_suspend_time, delta_delta);
cb33217b1   John Stultz   time: Avoid accum...
1253
  	}
330a1617b   John Stultz   timekeeping: Fix ...
1254
1255
  
  	timekeeping_update(tk, TK_MIRROR);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1256
  	write_seqcount_end(&tk_core.seq);
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
1257
  	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
8524070b7   John Stultz   Move timekeeping ...
1258
1259
  
  	clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
c54a42b19   Magnus Damm   clocksource: add ...
1260
  	clocksource_suspend();
adc78e6b9   Rafael J. Wysocki   timekeeping: Add ...
1261
  	clockevents_suspend();
8524070b7   John Stultz   Move timekeeping ...
1262
1263
1264
1265
1266
  
  	return 0;
  }
  
  /* sysfs resume/suspend bits for timekeeping */
e1a85b2c5   Rafael J. Wysocki   timekeeping: Use ...
1267
  static struct syscore_ops timekeeping_syscore_ops = {
8524070b7   John Stultz   Move timekeeping ...
1268
1269
  	.resume		= timekeeping_resume,
  	.suspend	= timekeeping_suspend,
8524070b7   John Stultz   Move timekeeping ...
1270
  };
e1a85b2c5   Rafael J. Wysocki   timekeeping: Use ...
1271
  static int __init timekeeping_init_ops(void)
8524070b7   John Stultz   Move timekeeping ...
1272
  {
e1a85b2c5   Rafael J. Wysocki   timekeeping: Use ...
1273
1274
  	register_syscore_ops(&timekeeping_syscore_ops);
  	return 0;
8524070b7   John Stultz   Move timekeeping ...
1275
  }
e1a85b2c5   Rafael J. Wysocki   timekeeping: Use ...
1276
  device_initcall(timekeeping_init_ops);
8524070b7   John Stultz   Move timekeeping ...
1277
1278
  
  /*
dc491596f   John Stultz   timekeeping: Rewo...
1279
   * Apply a multiplier adjustment to the timekeeper
8524070b7   John Stultz   Move timekeeping ...
1280
   */
dc491596f   John Stultz   timekeeping: Rewo...
1281
1282
1283
1284
  static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
  							 s64 offset,
  							 bool negative,
  							 int adj_scale)
8524070b7   John Stultz   Move timekeeping ...
1285
  {
dc491596f   John Stultz   timekeeping: Rewo...
1286
1287
  	s64 interval = tk->cycle_interval;
  	s32 mult_adj = 1;
8524070b7   John Stultz   Move timekeeping ...
1288

dc491596f   John Stultz   timekeeping: Rewo...
1289
1290
1291
1292
  	if (negative) {
  		mult_adj = -mult_adj;
  		interval = -interval;
  		offset  = -offset;
1d17d1748   Ingo Molnar   time: Fix adjustm...
1293
  	}
dc491596f   John Stultz   timekeeping: Rewo...
1294
1295
1296
  	mult_adj <<= adj_scale;
  	interval <<= adj_scale;
  	offset <<= adj_scale;
8524070b7   John Stultz   Move timekeeping ...
1297

c2bc11113   John Stultz   time: Improve doc...
1298
1299
1300
  	/*
  	 * So the following can be confusing.
  	 *
dc491596f   John Stultz   timekeeping: Rewo...
1301
  	 * To keep things simple, lets assume mult_adj == 1 for now.
c2bc11113   John Stultz   time: Improve doc...
1302
  	 *
dc491596f   John Stultz   timekeeping: Rewo...
1303
  	 * When mult_adj != 1, remember that the interval and offset values
c2bc11113   John Stultz   time: Improve doc...
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
  	 * have been appropriately scaled so the math is the same.
  	 *
  	 * The basic idea here is that we're increasing the multiplier
  	 * by one, this causes the xtime_interval to be incremented by
  	 * one cycle_interval. This is because:
  	 *	xtime_interval = cycle_interval * mult
  	 * So if mult is being incremented by one:
  	 *	xtime_interval = cycle_interval * (mult + 1)
  	 * Its the same as:
  	 *	xtime_interval = (cycle_interval * mult) + cycle_interval
  	 * Which can be shortened to:
  	 *	xtime_interval += cycle_interval
  	 *
  	 * So offset stores the non-accumulated cycles. Thus the current
  	 * time (in shifted nanoseconds) is:
  	 *	now = (offset * adj) + xtime_nsec
  	 * Now, even though we're adjusting the clock frequency, we have
  	 * to keep time consistent. In other words, we can't jump back
  	 * in time, and we also want to avoid jumping forward in time.
  	 *
  	 * So given the same offset value, we need the time to be the same
  	 * both before and after the freq adjustment.
  	 *	now = (offset * adj_1) + xtime_nsec_1
  	 *	now = (offset * adj_2) + xtime_nsec_2
  	 * So:
  	 *	(offset * adj_1) + xtime_nsec_1 =
  	 *		(offset * adj_2) + xtime_nsec_2
  	 * And we know:
  	 *	adj_2 = adj_1 + 1
  	 * So:
  	 *	(offset * adj_1) + xtime_nsec_1 =
  	 *		(offset * (adj_1+1)) + xtime_nsec_2
  	 *	(offset * adj_1) + xtime_nsec_1 =
  	 *		(offset * adj_1) + offset + xtime_nsec_2
  	 * Canceling the sides:
  	 *	xtime_nsec_1 = offset + xtime_nsec_2
  	 * Which gives us:
  	 *	xtime_nsec_2 = xtime_nsec_1 - offset
  	 * Which simplfies to:
  	 *	xtime_nsec -= offset
  	 *
  	 * XXX - TODO: Doc ntp_error calculation.
  	 */
cb2aa6346   John Stultz   time: Fix sign bu...
1347
  	if ((mult_adj > 0) && (tk->tkr.mult + mult_adj < mult_adj)) {
6067dc5a8   pang.xunlei   time: Avoid possi...
1348
1349
1350
1351
  		/* NTP adjustment caused clocksource mult overflow */
  		WARN_ON_ONCE(1);
  		return;
  	}
dc491596f   John Stultz   timekeeping: Rewo...
1352
  	tk->tkr.mult += mult_adj;
f726a697d   John Stultz   time: Rework time...
1353
  	tk->xtime_interval += interval;
d28ede837   Thomas Gleixner   timekeeping: Crea...
1354
  	tk->tkr.xtime_nsec -= offset;
f726a697d   John Stultz   time: Rework time...
1355
  	tk->ntp_error -= (interval - offset) << tk->ntp_error_shift;
dc491596f   John Stultz   timekeeping: Rewo...
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
  }
  
  /*
   * Calculate the multiplier adjustment needed to match the frequency
   * specified by NTP
   */
  static __always_inline void timekeeping_freqadjust(struct timekeeper *tk,
  							s64 offset)
  {
  	s64 interval = tk->cycle_interval;
  	s64 xinterval = tk->xtime_interval;
  	s64 tick_error;
  	bool negative;
  	u32 adj;
  
  	/* Remove any current error adj from freq calculation */
  	if (tk->ntp_err_mult)
  		xinterval -= tk->cycle_interval;
375f45b5b   John Stultz   timekeeping: Use ...
1374
  	tk->ntp_tick = ntp_tick_length();
dc491596f   John Stultz   timekeeping: Rewo...
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
  	/* Calculate current error per tick */
  	tick_error = ntp_tick_length() >> tk->ntp_error_shift;
  	tick_error -= (xinterval + tk->xtime_remainder);
  
  	/* Don't worry about correcting it if its small */
  	if (likely((tick_error >= 0) && (tick_error <= interval)))
  		return;
  
  	/* preserve the direction of correction */
  	negative = (tick_error < 0);
  
  	/* Sort out the magnitude of the correction */
  	tick_error = abs(tick_error);
  	for (adj = 0; tick_error > interval; adj++)
  		tick_error >>= 1;
  
  	/* scale the corrections */
  	timekeeping_apply_adjustment(tk, offset, negative, adj);
  }
  
  /*
   * Adjust the timekeeper's multiplier to the correct frequency
   * and also to reduce the accumulated error value.
   */
  static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
  {
  	/* Correct for the current frequency error */
  	timekeeping_freqadjust(tk, offset);
  
  	/* Next make a small adjustment to fix any cumulative error */
  	if (!tk->ntp_err_mult && (tk->ntp_error > 0)) {
  		tk->ntp_err_mult = 1;
  		timekeeping_apply_adjustment(tk, offset, 0, 0);
  	} else if (tk->ntp_err_mult && (tk->ntp_error <= 0)) {
  		/* Undo any existing error adjustment */
  		timekeeping_apply_adjustment(tk, offset, 1, 0);
  		tk->ntp_err_mult = 0;
  	}
  
  	if (unlikely(tk->tkr.clock->maxadj &&
659bc17b8   pang.xunlei   time: Complete NT...
1415
1416
  		(abs(tk->tkr.mult - tk->tkr.clock->mult)
  			> tk->tkr.clock->maxadj))) {
dc491596f   John Stultz   timekeeping: Rewo...
1417
1418
1419
1420
1421
1422
  		printk_once(KERN_WARNING
  			"Adjusting %s more than 11%% (%ld vs %ld)
  ",
  			tk->tkr.clock->name, (long)tk->tkr.mult,
  			(long)tk->tkr.clock->mult + tk->tkr.clock->maxadj);
  	}
2a8c0883c   John Stultz   time: Move xtime_...
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
  
  	/*
  	 * It may be possible that when we entered this function, xtime_nsec
  	 * was very small.  Further, if we're slightly speeding the clocksource
  	 * in the code above, its possible the required corrective factor to
  	 * xtime_nsec could cause it to underflow.
  	 *
  	 * Now, since we already accumulated the second, cannot simply roll
  	 * the accumulated second back, since the NTP subsystem has been
  	 * notified via second_overflow. So instead we push xtime_nsec forward
  	 * by the amount we underflowed, and add that amount into the error.
  	 *
  	 * We'll correct this error next time through this function, when
  	 * xtime_nsec is not as small.
  	 */
d28ede837   Thomas Gleixner   timekeeping: Crea...
1438
1439
1440
  	if (unlikely((s64)tk->tkr.xtime_nsec < 0)) {
  		s64 neg = -(s64)tk->tkr.xtime_nsec;
  		tk->tkr.xtime_nsec = 0;
f726a697d   John Stultz   time: Rework time...
1441
  		tk->ntp_error += neg << tk->ntp_error_shift;
2a8c0883c   John Stultz   time: Move xtime_...
1442
  	}
8524070b7   John Stultz   Move timekeeping ...
1443
1444
1445
  }
  
  /**
1f4f94870   John Stultz   time: Refactor ac...
1446
1447
1448
1449
1450
1451
1452
   * accumulate_nsecs_to_secs - Accumulates nsecs into secs
   *
   * Helper function that accumulates a the nsecs greater then a second
   * from the xtime_nsec field to the xtime_secs field.
   * It also calls into the NTP code to handle leapsecond processing.
   *
   */
780427f0e   David Vrabel   timekeeping: Indi...
1453
  static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk)
1f4f94870   John Stultz   time: Refactor ac...
1454
  {
d28ede837   Thomas Gleixner   timekeeping: Crea...
1455
  	u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr.shift;
5258d3f25   John Stultz   timekeeping: Fix ...
1456
  	unsigned int clock_set = 0;
1f4f94870   John Stultz   time: Refactor ac...
1457

d28ede837   Thomas Gleixner   timekeeping: Crea...
1458
  	while (tk->tkr.xtime_nsec >= nsecps) {
1f4f94870   John Stultz   time: Refactor ac...
1459
  		int leap;
d28ede837   Thomas Gleixner   timekeeping: Crea...
1460
  		tk->tkr.xtime_nsec -= nsecps;
1f4f94870   John Stultz   time: Refactor ac...
1461
1462
1463
1464
  		tk->xtime_sec++;
  
  		/* Figure out if its a leap sec and apply if needed */
  		leap = second_overflow(tk->xtime_sec);
6d0ef903e   John Stultz   time: Clean up of...
1465
  		if (unlikely(leap)) {
7d489d15c   John Stultz   timekeeping: Conv...
1466
  			struct timespec64 ts;
6d0ef903e   John Stultz   time: Clean up of...
1467
1468
  
  			tk->xtime_sec += leap;
1f4f94870   John Stultz   time: Refactor ac...
1469

6d0ef903e   John Stultz   time: Clean up of...
1470
1471
1472
  			ts.tv_sec = leap;
  			ts.tv_nsec = 0;
  			tk_set_wall_to_mono(tk,
7d489d15c   John Stultz   timekeeping: Conv...
1473
  				timespec64_sub(tk->wall_to_monotonic, ts));
6d0ef903e   John Stultz   time: Clean up of...
1474

cc244ddae   John Stultz   timekeeping: Move...
1475
  			__timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
5258d3f25   John Stultz   timekeeping: Fix ...
1476
  			clock_set = TK_CLOCK_WAS_SET;
6d0ef903e   John Stultz   time: Clean up of...
1477
  		}
1f4f94870   John Stultz   time: Refactor ac...
1478
  	}
5258d3f25   John Stultz   timekeeping: Fix ...
1479
  	return clock_set;
1f4f94870   John Stultz   time: Refactor ac...
1480
  }
1f4f94870   John Stultz   time: Refactor ac...
1481
  /**
a092ff0f9   John Stultz   time: Implement l...
1482
1483
1484
1485
1486
1487
1488
1489
   * logarithmic_accumulation - shifted accumulation of cycles
   *
   * This functions accumulates a shifted interval of cycles into
   * into a shifted interval nanoseconds. Allows for O(log) accumulation
   * loop.
   *
   * Returns the unconsumed cycles.
   */
f726a697d   John Stultz   time: Rework time...
1490
  static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
5258d3f25   John Stultz   timekeeping: Fix ...
1491
1492
  						u32 shift,
  						unsigned int *clock_set)
a092ff0f9   John Stultz   time: Implement l...
1493
  {
23a9537a6   Thomas Gleixner   timekeeping: Calc...
1494
  	cycle_t interval = tk->cycle_interval << shift;
deda2e819   Jason Wessel   timekeeping: Fix ...
1495
  	u64 raw_nsecs;
a092ff0f9   John Stultz   time: Implement l...
1496

f726a697d   John Stultz   time: Rework time...
1497
  	/* If the offset is smaller then a shifted interval, do nothing */
23a9537a6   Thomas Gleixner   timekeeping: Calc...
1498
  	if (offset < interval)
a092ff0f9   John Stultz   time: Implement l...
1499
1500
1501
  		return offset;
  
  	/* Accumulate one shifted interval */
23a9537a6   Thomas Gleixner   timekeeping: Calc...
1502
  	offset -= interval;
d28ede837   Thomas Gleixner   timekeeping: Crea...
1503
  	tk->tkr.cycle_last += interval;
a092ff0f9   John Stultz   time: Implement l...
1504

d28ede837   Thomas Gleixner   timekeeping: Crea...
1505
  	tk->tkr.xtime_nsec += tk->xtime_interval << shift;
5258d3f25   John Stultz   timekeeping: Fix ...
1506
  	*clock_set |= accumulate_nsecs_to_secs(tk);
a092ff0f9   John Stultz   time: Implement l...
1507

deda2e819   Jason Wessel   timekeeping: Fix ...
1508
  	/* Accumulate raw time */
5b3900cd4   Dan Carpenter   timekeeping: Cast...
1509
  	raw_nsecs = (u64)tk->raw_interval << shift;
f726a697d   John Stultz   time: Rework time...
1510
  	raw_nsecs += tk->raw_time.tv_nsec;
c7dcf87a6   John Stultz   time: Workaround ...
1511
1512
1513
  	if (raw_nsecs >= NSEC_PER_SEC) {
  		u64 raw_secs = raw_nsecs;
  		raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
f726a697d   John Stultz   time: Rework time...
1514
  		tk->raw_time.tv_sec += raw_secs;
a092ff0f9   John Stultz   time: Implement l...
1515
  	}
f726a697d   John Stultz   time: Rework time...
1516
  	tk->raw_time.tv_nsec = raw_nsecs;
a092ff0f9   John Stultz   time: Implement l...
1517
1518
  
  	/* Accumulate error between NTP and clock interval */
375f45b5b   John Stultz   timekeeping: Use ...
1519
  	tk->ntp_error += tk->ntp_tick << shift;
f726a697d   John Stultz   time: Rework time...
1520
1521
  	tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) <<
  						(tk->ntp_error_shift + shift);
a092ff0f9   John Stultz   time: Implement l...
1522
1523
1524
  
  	return offset;
  }
8524070b7   John Stultz   Move timekeeping ...
1525
1526
1527
  /**
   * update_wall_time - Uses the current clocksource to increment the wall time
   *
8524070b7   John Stultz   Move timekeeping ...
1528
   */
47a1b7963   John Stultz   tick/timekeeping:...
1529
  void update_wall_time(void)
8524070b7   John Stultz   Move timekeeping ...
1530
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1531
  	struct timekeeper *real_tk = &tk_core.timekeeper;
48cdc135d   Thomas Gleixner   timekeeping: Impl...
1532
  	struct timekeeper *tk = &shadow_timekeeper;
8524070b7   John Stultz   Move timekeeping ...
1533
  	cycle_t offset;
a092ff0f9   John Stultz   time: Implement l...
1534
  	int shift = 0, maxshift;
5258d3f25   John Stultz   timekeeping: Fix ...
1535
  	unsigned int clock_set = 0;
70471f2f0   John Stultz   time: Add timekee...
1536
  	unsigned long flags;
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
1537
  	raw_spin_lock_irqsave(&timekeeper_lock, flags);
8524070b7   John Stultz   Move timekeeping ...
1538
1539
1540
  
  	/* Make sure we're fully resumed: */
  	if (unlikely(timekeeping_suspended))
70471f2f0   John Stultz   time: Add timekee...
1541
  		goto out;
8524070b7   John Stultz   Move timekeeping ...
1542

592913ecb   John Stultz   time: Kill off CO...
1543
  #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
48cdc135d   Thomas Gleixner   timekeeping: Impl...
1544
  	offset = real_tk->cycle_interval;
592913ecb   John Stultz   time: Kill off CO...
1545
  #else
d28ede837   Thomas Gleixner   timekeeping: Crea...
1546
1547
  	offset = clocksource_delta(tk->tkr.read(tk->tkr.clock),
  				   tk->tkr.cycle_last, tk->tkr.mask);
8524070b7   John Stultz   Move timekeeping ...
1548
  #endif
8524070b7   John Stultz   Move timekeeping ...
1549

bf2ac3121   John Stultz   time: Avoid makin...
1550
  	/* Check if there's really nothing to do */
48cdc135d   Thomas Gleixner   timekeeping: Impl...
1551
  	if (offset < real_tk->cycle_interval)
bf2ac3121   John Stultz   time: Avoid makin...
1552
  		goto out;
a092ff0f9   John Stultz   time: Implement l...
1553
1554
1555
1556
  	/*
  	 * With NO_HZ we may have to accumulate many cycle_intervals
  	 * (think "ticks") worth of time at once. To do this efficiently,
  	 * we calculate the largest doubling multiple of cycle_intervals
88b28adf6   Jim Cromie   kernel-time: fix ...
1557
  	 * that is smaller than the offset.  We then accumulate that
a092ff0f9   John Stultz   time: Implement l...
1558
1559
  	 * chunk in one go, and then try to consume the next smaller
  	 * doubled multiple.
8524070b7   John Stultz   Move timekeeping ...
1560
  	 */
4e250fdde   John Stultz   time: Remove all ...
1561
  	shift = ilog2(offset) - ilog2(tk->cycle_interval);
a092ff0f9   John Stultz   time: Implement l...
1562
  	shift = max(0, shift);
88b28adf6   Jim Cromie   kernel-time: fix ...
1563
  	/* Bound shift to one less than what overflows tick_length */
ea7cf49a7   John Stultz   ntp: Access tick_...
1564
  	maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1;
a092ff0f9   John Stultz   time: Implement l...
1565
  	shift = min(shift, maxshift);
4e250fdde   John Stultz   time: Remove all ...
1566
  	while (offset >= tk->cycle_interval) {
5258d3f25   John Stultz   timekeeping: Fix ...
1567
1568
  		offset = logarithmic_accumulation(tk, offset, shift,
  							&clock_set);
4e250fdde   John Stultz   time: Remove all ...
1569
  		if (offset < tk->cycle_interval<<shift)
830ec0458   John Stultz   time: Fix accumul...
1570
  			shift--;
8524070b7   John Stultz   Move timekeeping ...
1571
1572
1573
  	}
  
  	/* correct the clock when NTP error is too big */
4e250fdde   John Stultz   time: Remove all ...
1574
  	timekeeping_adjust(tk, offset);
8524070b7   John Stultz   Move timekeeping ...
1575

6a867a395   John Stultz   time: Remove xtim...
1576
  	/*
92bb1fcf5   John Stultz   time: Only do nan...
1577
1578
1579
1580
  	 * XXX This can be killed once everyone converts
  	 * to the new update_vsyscall.
  	 */
  	old_vsyscall_fixup(tk);
8524070b7   John Stultz   Move timekeeping ...
1581

6a867a395   John Stultz   time: Remove xtim...
1582
1583
  	/*
  	 * Finally, make sure that after the rounding
1e75fa8be   John Stultz   time: Condense ti...
1584
  	 * xtime_nsec isn't larger than NSEC_PER_SEC
6a867a395   John Stultz   time: Remove xtim...
1585
  	 */
5258d3f25   John Stultz   timekeeping: Fix ...
1586
  	clock_set |= accumulate_nsecs_to_secs(tk);
83f57a11d   Linus Torvalds   Revert "time: Rem...
1587

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1588
  	write_seqcount_begin(&tk_core.seq);
48cdc135d   Thomas Gleixner   timekeeping: Impl...
1589
1590
1591
1592
1593
1594
1595
  	/*
  	 * Update the real timekeeper.
  	 *
  	 * We could avoid this memcpy by switching pointers, but that
  	 * requires changes to all other timekeeper usage sites as
  	 * well, i.e. move the timekeeper pointer getter into the
  	 * spinlocked/seqcount protected sections. And we trade this
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1596
  	 * memcpy under the tk_core.seq against one before we start
48cdc135d   Thomas Gleixner   timekeeping: Impl...
1597
1598
1599
  	 * updating.
  	 */
  	memcpy(real_tk, tk, sizeof(*tk));
5258d3f25   John Stultz   timekeeping: Fix ...
1600
  	timekeeping_update(real_tk, clock_set);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1601
  	write_seqcount_end(&tk_core.seq);
ca4523cda   Thomas Gleixner   timekeeping: Shor...
1602
  out:
9a7a71b1d   Thomas Gleixner   timekeeping: Spli...
1603
  	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
47a1b7963   John Stultz   tick/timekeeping:...
1604
  	if (clock_set)
cab5e127e   John Stultz   time: Revert to c...
1605
1606
  		/* Have to call _delayed version, since in irq context*/
  		clock_was_set_delayed();
8524070b7   John Stultz   Move timekeeping ...
1607
  }
7c3f1a573   Tomas Janousek   Introduce boot ba...
1608
1609
1610
1611
1612
  
  /**
   * getboottime - Return the real time of system boot.
   * @ts:		pointer to the timespec to be set
   *
abb3a4ea2   John Stultz   time: Introduce g...
1613
   * Returns the wall-time of boot in a timespec.
7c3f1a573   Tomas Janousek   Introduce boot ba...
1614
1615
1616
1617
1618
1619
1620
1621
   *
   * This is based on the wall_to_monotonic offset and the total suspend
   * time. Calls to settimeofday will affect the value returned (which
   * basically means that however wrong your real time clock is at boot time,
   * you get the right time here).
   */
  void getboottime(struct timespec *ts)
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1622
  	struct timekeeper *tk = &tk_core.timekeeper;
02cba1598   Thomas Gleixner   timekeeping: Simp...
1623
1624
1625
  	ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot);
  
  	*ts = ktime_to_timespec(t);
7c3f1a573   Tomas Janousek   Introduce boot ba...
1626
  }
c93d89f3d   Jason Wang   Export the symbol...
1627
  EXPORT_SYMBOL_GPL(getboottime);
7c3f1a573   Tomas Janousek   Introduce boot ba...
1628

17c38b749   John Stultz   Cache xtime every...
1629
1630
  unsigned long get_seconds(void)
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1631
  	struct timekeeper *tk = &tk_core.timekeeper;
4e250fdde   John Stultz   time: Remove all ...
1632
1633
  
  	return tk->xtime_sec;
17c38b749   John Stultz   Cache xtime every...
1634
1635
  }
  EXPORT_SYMBOL(get_seconds);
da15cfdae   John Stultz   time: Introduce C...
1636
1637
  struct timespec __current_kernel_time(void)
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1638
  	struct timekeeper *tk = &tk_core.timekeeper;
4e250fdde   John Stultz   time: Remove all ...
1639

7d489d15c   John Stultz   timekeeping: Conv...
1640
  	return timespec64_to_timespec(tk_xtime(tk));
da15cfdae   John Stultz   time: Introduce C...
1641
  }
17c38b749   John Stultz   Cache xtime every...
1642

2c6b47de1   John Stultz   Cleanup non-arch ...
1643
1644
  struct timespec current_kernel_time(void)
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1645
  	struct timekeeper *tk = &tk_core.timekeeper;
7d489d15c   John Stultz   timekeeping: Conv...
1646
  	struct timespec64 now;
2c6b47de1   John Stultz   Cleanup non-arch ...
1647
1648
1649
  	unsigned long seq;
  
  	do {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1650
  		seq = read_seqcount_begin(&tk_core.seq);
83f57a11d   Linus Torvalds   Revert "time: Rem...
1651

4e250fdde   John Stultz   time: Remove all ...
1652
  		now = tk_xtime(tk);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1653
  	} while (read_seqcount_retry(&tk_core.seq, seq));
2c6b47de1   John Stultz   Cleanup non-arch ...
1654

7d489d15c   John Stultz   timekeeping: Conv...
1655
  	return timespec64_to_timespec(now);
2c6b47de1   John Stultz   Cleanup non-arch ...
1656
  }
2c6b47de1   John Stultz   Cleanup non-arch ...
1657
  EXPORT_SYMBOL(current_kernel_time);
da15cfdae   John Stultz   time: Introduce C...
1658

334334b5f   John Stultz   time: Expose get_...
1659
  struct timespec64 get_monotonic_coarse64(void)
da15cfdae   John Stultz   time: Introduce C...
1660
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1661
  	struct timekeeper *tk = &tk_core.timekeeper;
7d489d15c   John Stultz   timekeeping: Conv...
1662
  	struct timespec64 now, mono;
da15cfdae   John Stultz   time: Introduce C...
1663
1664
1665
  	unsigned long seq;
  
  	do {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1666
  		seq = read_seqcount_begin(&tk_core.seq);
83f57a11d   Linus Torvalds   Revert "time: Rem...
1667

4e250fdde   John Stultz   time: Remove all ...
1668
1669
  		now = tk_xtime(tk);
  		mono = tk->wall_to_monotonic;
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1670
  	} while (read_seqcount_retry(&tk_core.seq, seq));
da15cfdae   John Stultz   time: Introduce C...
1671

7d489d15c   John Stultz   timekeeping: Conv...
1672
  	set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec,
da15cfdae   John Stultz   time: Introduce C...
1673
  				now.tv_nsec + mono.tv_nsec);
7d489d15c   John Stultz   timekeeping: Conv...
1674

334334b5f   John Stultz   time: Expose get_...
1675
  	return now;
da15cfdae   John Stultz   time: Introduce C...
1676
  }
871cf1e5f   Torben Hohn   time: Move do_tim...
1677
1678
  
  /*
d6ad41876   John Stultz   time: Kill xtime_...
1679
   * Must hold jiffies_lock
871cf1e5f   Torben Hohn   time: Move do_tim...
1680
1681
1682
1683
   */
  void do_timer(unsigned long ticks)
  {
  	jiffies_64 += ticks;
871cf1e5f   Torben Hohn   time: Move do_tim...
1684
1685
  	calc_global_load(ticks);
  }
48cf76f71   Torben Hohn   time: Provide get...
1686
1687
  
  /**
76f410889   John Stultz   hrtimer: Cleanup ...
1688
1689
1690
1691
1692
1693
   * ktime_get_update_offsets_tick - hrtimer helper
   * @offs_real:	pointer to storage for monotonic -> realtime offset
   * @offs_boot:	pointer to storage for monotonic -> boottime offset
   * @offs_tai:	pointer to storage for monotonic -> clock tai offset
   *
   * Returns monotonic time at last tick and various offsets
48cf76f71   Torben Hohn   time: Provide get...
1694
   */
76f410889   John Stultz   hrtimer: Cleanup ...
1695
1696
  ktime_t ktime_get_update_offsets_tick(ktime_t *offs_real, ktime_t *offs_boot,
  							ktime_t *offs_tai)
48cf76f71   Torben Hohn   time: Provide get...
1697
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1698
  	struct timekeeper *tk = &tk_core.timekeeper;
76f410889   John Stultz   hrtimer: Cleanup ...
1699
  	unsigned int seq;
48064f5f6   Thomas Gleixner   timekeeping; Use ...
1700
1701
  	ktime_t base;
  	u64 nsecs;
48cf76f71   Torben Hohn   time: Provide get...
1702
1703
  
  	do {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1704
  		seq = read_seqcount_begin(&tk_core.seq);
76f410889   John Stultz   hrtimer: Cleanup ...
1705

d28ede837   Thomas Gleixner   timekeeping: Crea...
1706
1707
  		base = tk->tkr.base_mono;
  		nsecs = tk->tkr.xtime_nsec >> tk->tkr.shift;
48064f5f6   Thomas Gleixner   timekeeping; Use ...
1708

76f410889   John Stultz   hrtimer: Cleanup ...
1709
1710
1711
  		*offs_real = tk->offs_real;
  		*offs_boot = tk->offs_boot;
  		*offs_tai = tk->offs_tai;
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1712
  	} while (read_seqcount_retry(&tk_core.seq, seq));
76f410889   John Stultz   hrtimer: Cleanup ...
1713

48064f5f6   Thomas Gleixner   timekeeping; Use ...
1714
  	return ktime_add_ns(base, nsecs);
48cf76f71   Torben Hohn   time: Provide get...
1715
  }
f0af911a9   Torben Hohn   time: Provide xti...
1716

f6c06abfb   Thomas Gleixner   timekeeping: Prov...
1717
1718
  #ifdef CONFIG_HIGH_RES_TIMERS
  /**
76f410889   John Stultz   hrtimer: Cleanup ...
1719
   * ktime_get_update_offsets_now - hrtimer helper
f6c06abfb   Thomas Gleixner   timekeeping: Prov...
1720
1721
   * @offs_real:	pointer to storage for monotonic -> realtime offset
   * @offs_boot:	pointer to storage for monotonic -> boottime offset
b7bc50e45   Xie XiuQi   timekeeping: Fix ...
1722
   * @offs_tai:	pointer to storage for monotonic -> clock tai offset
f6c06abfb   Thomas Gleixner   timekeeping: Prov...
1723
1724
   *
   * Returns current monotonic time and updates the offsets
b7bc50e45   Xie XiuQi   timekeeping: Fix ...
1725
   * Called from hrtimer_interrupt() or retrigger_next_event()
f6c06abfb   Thomas Gleixner   timekeeping: Prov...
1726
   */
76f410889   John Stultz   hrtimer: Cleanup ...
1727
  ktime_t ktime_get_update_offsets_now(ktime_t *offs_real, ktime_t *offs_boot,
90adda98b   John Stultz   hrtimer: Add hrti...
1728
  							ktime_t *offs_tai)
f6c06abfb   Thomas Gleixner   timekeeping: Prov...
1729
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1730
  	struct timekeeper *tk = &tk_core.timekeeper;
f6c06abfb   Thomas Gleixner   timekeeping: Prov...
1731
  	unsigned int seq;
a37c0aad6   Thomas Gleixner   timekeeping: Use ...
1732
1733
  	ktime_t base;
  	u64 nsecs;
f6c06abfb   Thomas Gleixner   timekeeping: Prov...
1734
1735
  
  	do {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1736
  		seq = read_seqcount_begin(&tk_core.seq);
f6c06abfb   Thomas Gleixner   timekeeping: Prov...
1737

d28ede837   Thomas Gleixner   timekeeping: Crea...
1738
  		base = tk->tkr.base_mono;
0e5ac3a8b   Thomas Gleixner   timekeeping: Use ...
1739
  		nsecs = timekeeping_get_ns(&tk->tkr);
f6c06abfb   Thomas Gleixner   timekeeping: Prov...
1740

4e250fdde   John Stultz   time: Remove all ...
1741
1742
  		*offs_real = tk->offs_real;
  		*offs_boot = tk->offs_boot;
90adda98b   John Stultz   hrtimer: Add hrti...
1743
  		*offs_tai = tk->offs_tai;
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1744
  	} while (read_seqcount_retry(&tk_core.seq, seq));
f6c06abfb   Thomas Gleixner   timekeeping: Prov...
1745

a37c0aad6   Thomas Gleixner   timekeeping: Use ...
1746
  	return ktime_add_ns(base, nsecs);
f6c06abfb   Thomas Gleixner   timekeeping: Prov...
1747
1748
  }
  #endif
f0af911a9   Torben Hohn   time: Provide xti...
1749
  /**
aa6f9c595   John Stultz   ntp: Move do_adjt...
1750
1751
1752
1753
   * do_adjtimex() - Accessor function to NTP __do_adjtimex function
   */
  int do_adjtimex(struct timex *txc)
  {
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1754
  	struct timekeeper *tk = &tk_core.timekeeper;
06c017fdd   John Stultz   timekeeping: Hold...
1755
  	unsigned long flags;
7d489d15c   John Stultz   timekeeping: Conv...
1756
  	struct timespec64 ts;
4e8f8b34b   John Stultz   timekeeping: Make...
1757
  	s32 orig_tai, tai;
e4085693f   John Stultz   ntp: Move timex v...
1758
1759
1760
1761
1762
1763
  	int ret;
  
  	/* Validate the data before disabling interrupts */
  	ret = ntp_validate_timex(txc);
  	if (ret)
  		return ret;
cef90377f   John Stultz   timekeeping: Move...
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
  	if (txc->modes & ADJ_SETOFFSET) {
  		struct timespec delta;
  		delta.tv_sec  = txc->time.tv_sec;
  		delta.tv_nsec = txc->time.tv_usec;
  		if (!(txc->modes & ADJ_NANO))
  			delta.tv_nsec *= 1000;
  		ret = timekeeping_inject_offset(&delta);
  		if (ret)
  			return ret;
  	}
d6d29896c   Thomas Gleixner   timekeeping: Prov...
1774
  	getnstimeofday64(&ts);
87ace39b7   John Stultz   ntp: Rework do_ad...
1775

06c017fdd   John Stultz   timekeeping: Hold...
1776
  	raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1777
  	write_seqcount_begin(&tk_core.seq);
06c017fdd   John Stultz   timekeeping: Hold...
1778

4e8f8b34b   John Stultz   timekeeping: Make...
1779
  	orig_tai = tai = tk->tai_offset;
87ace39b7   John Stultz   ntp: Rework do_ad...
1780
  	ret = __do_adjtimex(txc, &ts, &tai);
aa6f9c595   John Stultz   ntp: Move do_adjt...
1781

4e8f8b34b   John Stultz   timekeeping: Make...
1782
1783
  	if (tai != orig_tai) {
  		__timekeeping_set_tai_offset(tk, tai);
f55c07607   John Stultz   timekeeping: Fix ...
1784
  		timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET);
4e8f8b34b   John Stultz   timekeeping: Make...
1785
  	}
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1786
  	write_seqcount_end(&tk_core.seq);
06c017fdd   John Stultz   timekeeping: Hold...
1787
  	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
6fdda9a9c   John Stultz   timekeeping: Avoi...
1788
1789
  	if (tai != orig_tai)
  		clock_was_set();
7bd360144   John Stultz   timekeeping: Fix ...
1790
  	ntp_notify_cmos_timer();
87ace39b7   John Stultz   ntp: Rework do_ad...
1791
1792
  	return ret;
  }
aa6f9c595   John Stultz   ntp: Move do_adjt...
1793
1794
1795
1796
1797
1798
1799
  
  #ifdef CONFIG_NTP_PPS
  /**
   * hardpps() - Accessor function to NTP __hardpps function
   */
  void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
  {
06c017fdd   John Stultz   timekeeping: Hold...
1800
1801
1802
  	unsigned long flags;
  
  	raw_spin_lock_irqsave(&timekeeper_lock, flags);
3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1803
  	write_seqcount_begin(&tk_core.seq);
06c017fdd   John Stultz   timekeeping: Hold...
1804

aa6f9c595   John Stultz   ntp: Move do_adjt...
1805
  	__hardpps(phase_ts, raw_ts);
06c017fdd   John Stultz   timekeeping: Hold...
1806

3fdb14fd1   Thomas Gleixner   timekeeping: Cach...
1807
  	write_seqcount_end(&tk_core.seq);
06c017fdd   John Stultz   timekeeping: Hold...
1808
  	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
aa6f9c595   John Stultz   ntp: Move do_adjt...
1809
1810
1811
1812
1813
  }
  EXPORT_SYMBOL(hardpps);
  #endif
  
  /**
f0af911a9   Torben Hohn   time: Provide xti...
1814
1815
1816
1817
1818
1819
1820
   * xtime_update() - advances the timekeeping infrastructure
   * @ticks:	number of ticks, that have elapsed since the last call.
   *
   * Must be called with interrupts disabled.
   */
  void xtime_update(unsigned long ticks)
  {
d6ad41876   John Stultz   time: Kill xtime_...
1821
  	write_seqlock(&jiffies_lock);
f0af911a9   Torben Hohn   time: Provide xti...
1822
  	do_timer(ticks);
d6ad41876   John Stultz   time: Kill xtime_...
1823
  	write_sequnlock(&jiffies_lock);
47a1b7963   John Stultz   tick/timekeeping:...
1824
  	update_wall_time();
f0af911a9   Torben Hohn   time: Provide xti...
1825
  }