Blame view
kernel/time/timekeeping.c
49.6 KB
8524070b7
|
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
|
10 |
#include <linux/timekeeper_internal.h> |
8524070b7
|
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
|
16 |
#include <linux/sched.h> |
e1a85b2c5
|
17 |
#include <linux/syscore_ops.h> |
8524070b7
|
18 19 20 21 |
#include <linux/clocksource.h> #include <linux/jiffies.h> #include <linux/time.h> #include <linux/tick.h> |
75c5158f7
|
22 |
#include <linux/stop_machine.h> |
e0b306fef
|
23 |
#include <linux/pvclock_gtod.h> |
52f5684c8
|
24 |
#include <linux/compiler.h> |
8524070b7
|
25 |
|
eb93e4d93
|
26 |
#include "tick-internal.h" |
aa6f9c595
|
27 |
#include "ntp_internal.h" |
5c83545f2
|
28 |
#include "timekeeping_internal.h" |
155ec6022
|
29 |
|
04397fe94
|
30 31 |
#define TK_CLEAR_NTP (1 << 0) #define TK_MIRROR (1 << 1) |
780427f0e
|
32 |
#define TK_CLOCK_WAS_SET (1 << 2) |
04397fe94
|
33 |
|
3fdb14fd1
|
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
|
42 |
static DEFINE_RAW_SPINLOCK(timekeeper_lock); |
48cdc135d
|
43 |
static struct timekeeper shadow_timekeeper; |
155ec6022
|
44 |
|
4396e058c
|
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
|
60 61 |
/* flag for if timekeeping is suspended */ int __read_mostly timekeeping_suspended; |
31ade3069
|
62 63 |
/* Flag for if there is a persistent clock on this platform */ bool __read_mostly persistent_clock_exist = false; |
1e75fa8be
|
64 65 |
static inline void tk_normalize_xtime(struct timekeeper *tk) { |
d28ede837
|
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
|
68 69 70 |
tk->xtime_sec++; } } |
c905fae43
|
71 72 73 74 75 |
static inline struct timespec64 tk_xtime(struct timekeeper *tk) { struct timespec64 ts; ts.tv_sec = tk->xtime_sec; |
d28ede837
|
76 |
ts.tv_nsec = (long)(tk->tkr.xtime_nsec >> tk->tkr.shift); |
c905fae43
|
77 78 |
return ts; } |
7d489d15c
|
79 |
static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts) |
1e75fa8be
|
80 81 |
{ tk->xtime_sec = ts->tv_sec; |
d28ede837
|
82 |
tk->tkr.xtime_nsec = (u64)ts->tv_nsec << tk->tkr.shift; |
1e75fa8be
|
83 |
} |
7d489d15c
|
84 |
static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts) |
1e75fa8be
|
85 86 |
{ tk->xtime_sec += ts->tv_sec; |
d28ede837
|
87 |
tk->tkr.xtime_nsec += (u64)ts->tv_nsec << tk->tkr.shift; |
784ffcbb9
|
88 |
tk_normalize_xtime(tk); |
1e75fa8be
|
89 |
} |
8fcce546b
|
90 |
|
7d489d15c
|
91 |
static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm) |
6d0ef903e
|
92 |
{ |
7d489d15c
|
93 |
struct timespec64 tmp; |
6d0ef903e
|
94 95 96 97 98 |
/* * Verify consistency of: offset_real = -wall_to_monotonic * before modifying anything */ |
7d489d15c
|
99 |
set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec, |
6d0ef903e
|
100 |
-tk->wall_to_monotonic.tv_nsec); |
7d489d15c
|
101 |
WARN_ON_ONCE(tk->offs_real.tv64 != timespec64_to_ktime(tmp).tv64); |
6d0ef903e
|
102 |
tk->wall_to_monotonic = wtm; |
7d489d15c
|
103 104 |
set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec); tk->offs_real = timespec64_to_ktime(tmp); |
04005f601
|
105 |
tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0)); |
6d0ef903e
|
106 |
} |
47da70d32
|
107 |
static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta) |
6d0ef903e
|
108 |
{ |
47da70d32
|
109 |
tk->offs_boot = ktime_add(tk->offs_boot, delta); |
6d0ef903e
|
110 |
} |
155ec6022
|
111 |
/** |
d26e4fe0d
|
112 |
* tk_setup_internals - Set up internals to use clocksource clock. |
155ec6022
|
113 |
* |
d26e4fe0d
|
114 |
* @tk: The target timekeeper to setup. |
155ec6022
|
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
|
122 |
static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) |
155ec6022
|
123 124 |
{ cycle_t interval; |
a386b5af8
|
125 |
u64 tmp, ntpinterval; |
1e75fa8be
|
126 |
struct clocksource *old_clock; |
155ec6022
|
127 |
|
d28ede837
|
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
|
133 134 135 136 |
/* Do the ns -> cycle conversion first, using original mult */ tmp = NTP_INTERVAL_LENGTH; tmp <<= clock->shift; |
a386b5af8
|
137 |
ntpinterval = tmp; |
0a5441983
|
138 139 |
tmp += clock->mult/2; do_div(tmp, clock->mult); |
155ec6022
|
140 141 142 143 |
if (tmp == 0) tmp = 1; interval = (cycle_t) tmp; |
f726a697d
|
144 |
tk->cycle_interval = interval; |
155ec6022
|
145 146 |
/* Go back from cycles -> shifted ns */ |
f726a697d
|
147 148 149 |
tk->xtime_interval = (u64) interval * clock->mult; tk->xtime_remainder = ntpinterval - tk->xtime_interval; tk->raw_interval = |
0a5441983
|
150 |
((u64) interval * clock->mult) >> clock->shift; |
155ec6022
|
151 |
|
1e75fa8be
|
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
|
156 |
tk->tkr.xtime_nsec >>= -shift_change; |
1e75fa8be
|
157 |
else |
d28ede837
|
158 |
tk->tkr.xtime_nsec <<= shift_change; |
1e75fa8be
|
159 |
} |
d28ede837
|
160 |
tk->tkr.shift = clock->shift; |
155ec6022
|
161 |
|
f726a697d
|
162 163 |
tk->ntp_error = 0; tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift; |
375f45b5b
|
164 |
tk->ntp_tick = ntpinterval << tk->ntp_error_shift; |
0a5441983
|
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
|
171 |
tk->tkr.mult = clock->mult; |
dc491596f
|
172 |
tk->ntp_err_mult = 0; |
155ec6022
|
173 |
} |
8524070b7
|
174 |
|
2ba2a3054
|
175 |
/* Timekeeper helper functions. */ |
7b1f62076
|
176 177 |
#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET |
e06fde37b
|
178 179 |
static u32 default_arch_gettimeoffset(void) { return 0; } u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset; |
7b1f62076
|
180 |
#else |
e06fde37b
|
181 |
static inline u32 arch_gettimeoffset(void) { return 0; } |
7b1f62076
|
182 |
#endif |
0e5ac3a8b
|
183 |
static inline s64 timekeeping_get_ns(struct tk_read_base *tkr) |
2ba2a3054
|
184 |
{ |
3a9783778
|
185 |
cycle_t cycle_now, delta; |
1e75fa8be
|
186 |
s64 nsec; |
2ba2a3054
|
187 188 |
/* read clocksource: */ |
0e5ac3a8b
|
189 |
cycle_now = tkr->read(tkr->clock); |
2ba2a3054
|
190 191 |
/* calculate the delta since the last update_wall_time: */ |
0e5ac3a8b
|
192 |
delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask); |
2ba2a3054
|
193 |
|
0e5ac3a8b
|
194 195 |
nsec = delta * tkr->mult + tkr->xtime_nsec; nsec >>= tkr->shift; |
f2a5a0854
|
196 |
|
7b1f62076
|
197 |
/* If arch requires, add in get_arch_timeoffset() */ |
e06fde37b
|
198 |
return nsec + arch_gettimeoffset(); |
2ba2a3054
|
199 |
} |
f726a697d
|
200 |
static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk) |
2ba2a3054
|
201 |
{ |
d28ede837
|
202 |
struct clocksource *clock = tk->tkr.clock; |
3a9783778
|
203 |
cycle_t cycle_now, delta; |
f2a5a0854
|
204 |
s64 nsec; |
2ba2a3054
|
205 206 |
/* read clocksource: */ |
d28ede837
|
207 |
cycle_now = tk->tkr.read(clock); |
2ba2a3054
|
208 209 |
/* calculate the delta since the last update_wall_time: */ |
d28ede837
|
210 |
delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask); |
2ba2a3054
|
211 |
|
f2a5a0854
|
212 |
/* convert delta to nanoseconds. */ |
3a9783778
|
213 |
nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift); |
f2a5a0854
|
214 |
|
7b1f62076
|
215 |
/* If arch requires, add in get_arch_timeoffset() */ |
e06fde37b
|
216 |
return nsec + arch_gettimeoffset(); |
2ba2a3054
|
217 |
} |
4396e058c
|
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
|
323 324 325 326 |
#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD static inline void update_vsyscall(struct timekeeper *tk) { |
0680eb1f4
|
327 |
struct timespec xt, wm; |
c905fae43
|
328 |
|
e2dff1ec0
|
329 |
xt = timespec64_to_timespec(tk_xtime(tk)); |
0680eb1f4
|
330 331 |
wm = timespec64_to_timespec(tk->wall_to_monotonic); update_vsyscall_old(&xt, &wm, tk->tkr.clock, tk->tkr.mult, |
d28ede837
|
332 |
tk->tkr.cycle_last); |
c905fae43
|
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
|
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
|
352 |
tk->ntp_error += remainder << tk->ntp_error_shift; |
d28ede837
|
353 |
tk->ntp_error -= (1ULL << tk->tkr.shift) << tk->ntp_error_shift; |
c905fae43
|
354 355 356 357 |
} #else #define old_vsyscall_fixup(tk) #endif |
e0b306fef
|
358 |
static RAW_NOTIFIER_HEAD(pvclock_gtod_chain); |
780427f0e
|
359 |
static void update_pvclock_gtod(struct timekeeper *tk, bool was_set) |
e0b306fef
|
360 |
{ |
780427f0e
|
361 |
raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk); |
e0b306fef
|
362 363 364 365 |
} /** * pvclock_gtod_register_notifier - register a pvclock timedata update listener |
e0b306fef
|
366 367 368 |
*/ int pvclock_gtod_register_notifier(struct notifier_block *nb) { |
3fdb14fd1
|
369 |
struct timekeeper *tk = &tk_core.timekeeper; |
e0b306fef
|
370 371 |
unsigned long flags; int ret; |
9a7a71b1d
|
372 |
raw_spin_lock_irqsave(&timekeeper_lock, flags); |
e0b306fef
|
373 |
ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb); |
780427f0e
|
374 |
update_pvclock_gtod(tk, true); |
9a7a71b1d
|
375 |
raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
e0b306fef
|
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
|
384 385 386 |
*/ int pvclock_gtod_unregister_notifier(struct notifier_block *nb) { |
e0b306fef
|
387 388 |
unsigned long flags; int ret; |
9a7a71b1d
|
389 |
raw_spin_lock_irqsave(&timekeeper_lock, flags); |
e0b306fef
|
390 |
ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb); |
9a7a71b1d
|
391 |
raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
e0b306fef
|
392 393 394 395 |
return ret; } EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier); |
7c032df55
|
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
|
401 402 |
u64 seconds; u32 nsec; |
7c032df55
|
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
|
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
|
414 415 416 |
/* Update the monotonic raw base */ tk->base_raw = timespec64_to_ktime(tk->raw_time); |
9e3680b17
|
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
|
427 |
} |
9a7a71b1d
|
428 |
/* must hold timekeeper_lock */ |
04397fe94
|
429 |
static void timekeeping_update(struct timekeeper *tk, unsigned int action) |
cc06268c6
|
430 |
{ |
04397fe94
|
431 |
if (action & TK_CLEAR_NTP) { |
f726a697d
|
432 |
tk->ntp_error = 0; |
cc06268c6
|
433 434 |
ntp_clear(); } |
48cdc135d
|
435 |
|
7c032df55
|
436 |
tk_update_ktime_data(tk); |
9bf2419fa
|
437 438 |
update_vsyscall(tk); update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET); |
04397fe94
|
439 |
if (action & TK_MIRROR) |
3fdb14fd1
|
440 441 |
memcpy(&shadow_timekeeper, &tk_core.timekeeper, sizeof(tk_core.timekeeper)); |
4396e058c
|
442 443 |
update_fast_timekeeper(tk); |
cc06268c6
|
444 |
} |
8524070b7
|
445 |
/** |
155ec6022
|
446 |
* timekeeping_forward_now - update clock to the current time |
8524070b7
|
447 |
* |
9a055117d
|
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
|
451 |
*/ |
f726a697d
|
452 |
static void timekeeping_forward_now(struct timekeeper *tk) |
8524070b7
|
453 |
{ |
d28ede837
|
454 |
struct clocksource *clock = tk->tkr.clock; |
3a9783778
|
455 |
cycle_t cycle_now, delta; |
9a055117d
|
456 |
s64 nsec; |
8524070b7
|
457 |
|
d28ede837
|
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
|
461 |
|
d28ede837
|
462 |
tk->tkr.xtime_nsec += delta * tk->tkr.mult; |
7d27558c4
|
463 |
|
7b1f62076
|
464 |
/* If arch requires, add in get_arch_timeoffset() */ |
d28ede837
|
465 |
tk->tkr.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr.shift; |
7d27558c4
|
466 |
|
f726a697d
|
467 |
tk_normalize_xtime(tk); |
2d42244ae
|
468 |
|
3a9783778
|
469 |
nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift); |
7d489d15c
|
470 |
timespec64_add_ns(&tk->raw_time, nsec); |
8524070b7
|
471 472 473 |
} /** |
d6d29896c
|
474 |
* __getnstimeofday64 - Returns the time of day in a timespec64. |
8524070b7
|
475 476 |
* @ts: pointer to the timespec to be set * |
1e817fb62
|
477 478 |
* Updates the time of day in the timespec. * Returns 0 on success, or -ve when suspended (timespec will be undefined). |
8524070b7
|
479 |
*/ |
d6d29896c
|
480 |
int __getnstimeofday64(struct timespec64 *ts) |
8524070b7
|
481 |
{ |
3fdb14fd1
|
482 |
struct timekeeper *tk = &tk_core.timekeeper; |
8524070b7
|
483 |
unsigned long seq; |
1e75fa8be
|
484 |
s64 nsecs = 0; |
8524070b7
|
485 486 |
do { |
3fdb14fd1
|
487 |
seq = read_seqcount_begin(&tk_core.seq); |
8524070b7
|
488 |
|
4e250fdde
|
489 |
ts->tv_sec = tk->xtime_sec; |
0e5ac3a8b
|
490 |
nsecs = timekeeping_get_ns(&tk->tkr); |
8524070b7
|
491 |
|
3fdb14fd1
|
492 |
} while (read_seqcount_retry(&tk_core.seq, seq)); |
8524070b7
|
493 |
|
ec145babe
|
494 |
ts->tv_nsec = 0; |
d6d29896c
|
495 |
timespec64_add_ns(ts, nsecs); |
1e817fb62
|
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
|
505 |
EXPORT_SYMBOL(__getnstimeofday64); |
1e817fb62
|
506 507 |
/** |
d6d29896c
|
508 |
* getnstimeofday64 - Returns the time of day in a timespec64. |
5322e4c26
|
509 |
* @ts: pointer to the timespec64 to be set |
1e817fb62
|
510 |
* |
5322e4c26
|
511 |
* Returns the time of day in a timespec64 (WARN if suspended). |
1e817fb62
|
512 |
*/ |
d6d29896c
|
513 |
void getnstimeofday64(struct timespec64 *ts) |
1e817fb62
|
514 |
{ |
d6d29896c
|
515 |
WARN_ON(__getnstimeofday64(ts)); |
8524070b7
|
516 |
} |
d6d29896c
|
517 |
EXPORT_SYMBOL(getnstimeofday64); |
8524070b7
|
518 |
|
951ed4d36
|
519 520 |
ktime_t ktime_get(void) { |
3fdb14fd1
|
521 |
struct timekeeper *tk = &tk_core.timekeeper; |
951ed4d36
|
522 |
unsigned int seq; |
a016a5bd6
|
523 524 |
ktime_t base; s64 nsecs; |
951ed4d36
|
525 526 527 528 |
WARN_ON(timekeeping_suspended); do { |
3fdb14fd1
|
529 |
seq = read_seqcount_begin(&tk_core.seq); |
d28ede837
|
530 |
base = tk->tkr.base_mono; |
0e5ac3a8b
|
531 |
nsecs = timekeeping_get_ns(&tk->tkr); |
951ed4d36
|
532 |
|
3fdb14fd1
|
533 |
} while (read_seqcount_retry(&tk_core.seq, seq)); |
24e4a8c3e
|
534 |
|
a016a5bd6
|
535 |
return ktime_add_ns(base, nsecs); |
951ed4d36
|
536 537 |
} EXPORT_SYMBOL_GPL(ktime_get); |
0077dc60f
|
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
|
555 |
base = ktime_add(tk->tkr.base_mono, *offset); |
0e5ac3a8b
|
556 |
nsecs = timekeeping_get_ns(&tk->tkr); |
0077dc60f
|
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
|
564 |
/** |
9a6b51976
|
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
|
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
|
606 |
* ktime_get_ts64 - get the monotonic clock in timespec64 format |
951ed4d36
|
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
|
611 |
* in normalized timespec64 format in the variable pointed to by @ts. |
951ed4d36
|
612 |
*/ |
d6d29896c
|
613 |
void ktime_get_ts64(struct timespec64 *ts) |
951ed4d36
|
614 |
{ |
3fdb14fd1
|
615 |
struct timekeeper *tk = &tk_core.timekeeper; |
d6d29896c
|
616 |
struct timespec64 tomono; |
ec145babe
|
617 |
s64 nsec; |
951ed4d36
|
618 |
unsigned int seq; |
951ed4d36
|
619 620 621 622 |
WARN_ON(timekeeping_suspended); do { |
3fdb14fd1
|
623 |
seq = read_seqcount_begin(&tk_core.seq); |
d6d29896c
|
624 |
ts->tv_sec = tk->xtime_sec; |
0e5ac3a8b
|
625 |
nsec = timekeeping_get_ns(&tk->tkr); |
4e250fdde
|
626 |
tomono = tk->wall_to_monotonic; |
951ed4d36
|
627 |
|
3fdb14fd1
|
628 |
} while (read_seqcount_retry(&tk_core.seq, seq)); |
951ed4d36
|
629 |
|
d6d29896c
|
630 631 632 |
ts->tv_sec += tomono.tv_sec; ts->tv_nsec = 0; timespec64_add_ns(ts, nsec + tomono.tv_nsec); |
951ed4d36
|
633 |
} |
d6d29896c
|
634 |
EXPORT_SYMBOL_GPL(ktime_get_ts64); |
951ed4d36
|
635 |
|
9e3680b17
|
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
|
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
|
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
|
695 |
struct timekeeper *tk = &tk_core.timekeeper; |
e2c18e49a
|
696 697 698 699 700 701 |
unsigned long seq; s64 nsecs_raw, nsecs_real; WARN_ON_ONCE(timekeeping_suspended); do { |
3fdb14fd1
|
702 |
seq = read_seqcount_begin(&tk_core.seq); |
e2c18e49a
|
703 |
|
7d489d15c
|
704 |
*ts_raw = timespec64_to_timespec(tk->raw_time); |
4e250fdde
|
705 |
ts_real->tv_sec = tk->xtime_sec; |
1e75fa8be
|
706 |
ts_real->tv_nsec = 0; |
e2c18e49a
|
707 |
|
4e250fdde
|
708 |
nsecs_raw = timekeeping_get_ns_raw(tk); |
0e5ac3a8b
|
709 |
nsecs_real = timekeeping_get_ns(&tk->tkr); |
e2c18e49a
|
710 |
|
3fdb14fd1
|
711 |
} while (read_seqcount_retry(&tk_core.seq, seq)); |
e2c18e49a
|
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
|
719 720 721 722 |
/** * do_gettimeofday - Returns the time of day in a timeval * @tv: pointer to the timeval to be set * |
efd9ac863
|
723 |
* NOTE: Users should be converted to using getnstimeofday() |
8524070b7
|
724 725 726 |
*/ void do_gettimeofday(struct timeval *tv) { |
d6d29896c
|
727 |
struct timespec64 now; |
8524070b7
|
728 |
|
d6d29896c
|
729 |
getnstimeofday64(&now); |
8524070b7
|
730 731 732 |
tv->tv_sec = now.tv_sec; tv->tv_usec = now.tv_nsec/1000; } |
8524070b7
|
733 |
EXPORT_SYMBOL(do_gettimeofday); |
d239f49d7
|
734 |
|
8524070b7
|
735 |
/** |
21f7eca55
|
736 737 |
* do_settimeofday64 - Sets the time of day. * @ts: pointer to the timespec64 variable containing the new time |
8524070b7
|
738 739 740 |
* * Sets the time of day to the new time and update NTP and notify hrtimers */ |
21f7eca55
|
741 |
int do_settimeofday64(const struct timespec64 *ts) |
8524070b7
|
742 |
{ |
3fdb14fd1
|
743 |
struct timekeeper *tk = &tk_core.timekeeper; |
21f7eca55
|
744 |
struct timespec64 ts_delta, xt; |
92c1d3ed4
|
745 |
unsigned long flags; |
8524070b7
|
746 |
|
21f7eca55
|
747 |
if (!timespec64_valid_strict(ts)) |
8524070b7
|
748 |
return -EINVAL; |
9a7a71b1d
|
749 |
raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd1
|
750 |
write_seqcount_begin(&tk_core.seq); |
8524070b7
|
751 |
|
4e250fdde
|
752 |
timekeeping_forward_now(tk); |
9a055117d
|
753 |
|
4e250fdde
|
754 |
xt = tk_xtime(tk); |
21f7eca55
|
755 756 |
ts_delta.tv_sec = ts->tv_sec - xt.tv_sec; ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec; |
1e75fa8be
|
757 |
|
7d489d15c
|
758 |
tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta)); |
8524070b7
|
759 |
|
21f7eca55
|
760 |
tk_set_xtime(tk, ts); |
1e75fa8be
|
761 |
|
780427f0e
|
762 |
timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
8524070b7
|
763 |
|
3fdb14fd1
|
764 |
write_seqcount_end(&tk_core.seq); |
9a7a71b1d
|
765 |
raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
8524070b7
|
766 767 768 769 770 771 |
/* signal hrtimers about time change */ clock_was_set(); return 0; } |
21f7eca55
|
772 |
EXPORT_SYMBOL(do_settimeofday64); |
8524070b7
|
773 |
|
c528f7c6c
|
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
|
782 |
struct timekeeper *tk = &tk_core.timekeeper; |
92c1d3ed4
|
783 |
unsigned long flags; |
7d489d15c
|
784 |
struct timespec64 ts64, tmp; |
4e8b14526
|
785 |
int ret = 0; |
c528f7c6c
|
786 787 788 |
if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) return -EINVAL; |
7d489d15c
|
789 |
ts64 = timespec_to_timespec64(*ts); |
9a7a71b1d
|
790 |
raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd1
|
791 |
write_seqcount_begin(&tk_core.seq); |
c528f7c6c
|
792 |
|
4e250fdde
|
793 |
timekeeping_forward_now(tk); |
c528f7c6c
|
794 |
|
4e8b14526
|
795 |
/* Make sure the proposed value is valid */ |
7d489d15c
|
796 797 |
tmp = timespec64_add(tk_xtime(tk), ts64); if (!timespec64_valid_strict(&tmp)) { |
4e8b14526
|
798 799 800 |
ret = -EINVAL; goto error; } |
1e75fa8be
|
801 |
|
7d489d15c
|
802 803 |
tk_xtime_add(tk, &ts64); tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts64)); |
c528f7c6c
|
804 |
|
4e8b14526
|
805 |
error: /* even if we error out, we forwarded the time, so call update */ |
780427f0e
|
806 |
timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
c528f7c6c
|
807 |
|
3fdb14fd1
|
808 |
write_seqcount_end(&tk_core.seq); |
9a7a71b1d
|
809 |
raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
c528f7c6c
|
810 811 812 |
/* signal hrtimers about time change */ clock_was_set(); |
4e8b14526
|
813 |
return ret; |
c528f7c6c
|
814 815 |
} EXPORT_SYMBOL(timekeeping_inject_offset); |
cc244ddae
|
816 817 818 819 820 821 822 |
/** * timekeeping_get_tai_offset - Returns current TAI offset from UTC * */ s32 timekeeping_get_tai_offset(void) { |
3fdb14fd1
|
823 |
struct timekeeper *tk = &tk_core.timekeeper; |
cc244ddae
|
824 825 826 827 |
unsigned int seq; s32 ret; do { |
3fdb14fd1
|
828 |
seq = read_seqcount_begin(&tk_core.seq); |
cc244ddae
|
829 |
ret = tk->tai_offset; |
3fdb14fd1
|
830 |
} while (read_seqcount_retry(&tk_core.seq, seq)); |
cc244ddae
|
831 832 833 834 835 836 837 838 |
return ret; } /** * __timekeeping_set_tai_offset - Lock free worker function * */ |
dd5d70e86
|
839 |
static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset) |
cc244ddae
|
840 841 |
{ tk->tai_offset = tai_offset; |
04005f601
|
842 |
tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0)); |
cc244ddae
|
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
|
851 |
struct timekeeper *tk = &tk_core.timekeeper; |
cc244ddae
|
852 |
unsigned long flags; |
9a7a71b1d
|
853 |
raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd1
|
854 |
write_seqcount_begin(&tk_core.seq); |
cc244ddae
|
855 |
__timekeeping_set_tai_offset(tk, tai_offset); |
f55c07607
|
856 |
timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); |
3fdb14fd1
|
857 |
write_seqcount_end(&tk_core.seq); |
9a7a71b1d
|
858 |
raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
4e8f8b34b
|
859 |
clock_was_set(); |
cc244ddae
|
860 |
} |
8524070b7
|
861 862 863 864 865 |
/** * change_clocksource - Swaps clocksources if a new one is available * * Accumulates current time interval and initializes new clocksource */ |
75c5158f7
|
866 |
static int change_clocksource(void *data) |
8524070b7
|
867 |
{ |
3fdb14fd1
|
868 |
struct timekeeper *tk = &tk_core.timekeeper; |
4614e6ada
|
869 |
struct clocksource *new, *old; |
f695cf948
|
870 |
unsigned long flags; |
8524070b7
|
871 |
|
75c5158f7
|
872 |
new = (struct clocksource *) data; |
8524070b7
|
873 |
|
9a7a71b1d
|
874 |
raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd1
|
875 |
write_seqcount_begin(&tk_core.seq); |
f695cf948
|
876 |
|
4e250fdde
|
877 |
timekeeping_forward_now(tk); |
09ac369c8
|
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
|
884 |
old = tk->tkr.clock; |
09ac369c8
|
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
|
892 |
} |
780427f0e
|
893 |
timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
f695cf948
|
894 |
|
3fdb14fd1
|
895 |
write_seqcount_end(&tk_core.seq); |
9a7a71b1d
|
896 |
raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
f695cf948
|
897 |
|
75c5158f7
|
898 899 |
return 0; } |
8524070b7
|
900 |
|
75c5158f7
|
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
|
908 |
int timekeeping_notify(struct clocksource *clock) |
75c5158f7
|
909 |
{ |
3fdb14fd1
|
910 |
struct timekeeper *tk = &tk_core.timekeeper; |
4e250fdde
|
911 |
|
d28ede837
|
912 |
if (tk->tkr.clock == clock) |
ba919d1ca
|
913 |
return 0; |
75c5158f7
|
914 |
stop_machine(change_clocksource, clock, NULL); |
8524070b7
|
915 |
tick_clock_notify(); |
d28ede837
|
916 |
return tk->tkr.clock == clock ? 0 : -1; |
8524070b7
|
917 |
} |
75c5158f7
|
918 |
|
a40f262cc
|
919 |
/** |
cdba2ec53
|
920 921 |
* getrawmonotonic64 - Returns the raw monotonic time in a timespec * @ts: pointer to the timespec64 to be set |
2d42244ae
|
922 923 924 |
* * Returns the raw monotonic time (completely un-modified by ntp) */ |
cdba2ec53
|
925 |
void getrawmonotonic64(struct timespec64 *ts) |
2d42244ae
|
926 |
{ |
3fdb14fd1
|
927 |
struct timekeeper *tk = &tk_core.timekeeper; |
7d489d15c
|
928 |
struct timespec64 ts64; |
2d42244ae
|
929 930 |
unsigned long seq; s64 nsecs; |
2d42244ae
|
931 932 |
do { |
3fdb14fd1
|
933 |
seq = read_seqcount_begin(&tk_core.seq); |
4e250fdde
|
934 |
nsecs = timekeeping_get_ns_raw(tk); |
7d489d15c
|
935 |
ts64 = tk->raw_time; |
2d42244ae
|
936 |
|
3fdb14fd1
|
937 |
} while (read_seqcount_retry(&tk_core.seq, seq)); |
2d42244ae
|
938 |
|
7d489d15c
|
939 |
timespec64_add_ns(&ts64, nsecs); |
cdba2ec53
|
940 |
*ts = ts64; |
2d42244ae
|
941 |
} |
cdba2ec53
|
942 |
EXPORT_SYMBOL(getrawmonotonic64); |
2d42244ae
|
943 |
|
2d42244ae
|
944 |
/** |
cf4fc6cb7
|
945 |
* timekeeping_valid_for_hres - Check if timekeeping is suitable for hres |
8524070b7
|
946 |
*/ |
cf4fc6cb7
|
947 |
int timekeeping_valid_for_hres(void) |
8524070b7
|
948 |
{ |
3fdb14fd1
|
949 |
struct timekeeper *tk = &tk_core.timekeeper; |
8524070b7
|
950 951 952 953 |
unsigned long seq; int ret; do { |
3fdb14fd1
|
954 |
seq = read_seqcount_begin(&tk_core.seq); |
8524070b7
|
955 |
|
d28ede837
|
956 |
ret = tk->tkr.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; |
8524070b7
|
957 |
|
3fdb14fd1
|
958 |
} while (read_seqcount_retry(&tk_core.seq, seq)); |
8524070b7
|
959 960 961 962 963 |
return ret; } /** |
98962465e
|
964 |
* timekeeping_max_deferment - Returns max time the clocksource can be deferred |
98962465e
|
965 966 967 |
*/ u64 timekeeping_max_deferment(void) { |
3fdb14fd1
|
968 |
struct timekeeper *tk = &tk_core.timekeeper; |
70471f2f0
|
969 970 |
unsigned long seq; u64 ret; |
42e71e81f
|
971 |
|
70471f2f0
|
972 |
do { |
3fdb14fd1
|
973 |
seq = read_seqcount_begin(&tk_core.seq); |
70471f2f0
|
974 |
|
d28ede837
|
975 |
ret = tk->tkr.clock->max_idle_ns; |
70471f2f0
|
976 |
|
3fdb14fd1
|
977 |
} while (read_seqcount_retry(&tk_core.seq, seq)); |
70471f2f0
|
978 979 |
return ret; |
98962465e
|
980 981 982 |
} /** |
d4f587c67
|
983 |
* read_persistent_clock - Return time from the persistent clock. |
8524070b7
|
984 985 |
* * Weak dummy function for arches that do not yet support it. |
d4f587c67
|
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
|
988 989 990 |
* * XXX - Do be sure to remove it once all arches implement it. */ |
52f5684c8
|
991 |
void __weak read_persistent_clock(struct timespec *ts) |
8524070b7
|
992 |
{ |
d4f587c67
|
993 994 |
ts->tv_sec = 0; ts->tv_nsec = 0; |
8524070b7
|
995 |
} |
23970e389
|
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
|
1005 |
void __weak read_boot_clock(struct timespec *ts) |
23970e389
|
1006 1007 1008 1009 |
{ ts->tv_sec = 0; ts->tv_nsec = 0; } |
8524070b7
|
1010 1011 1012 1013 1014 |
/* * timekeeping_init - Initializes the clocksource and common timekeeping values */ void __init timekeeping_init(void) { |
3fdb14fd1
|
1015 |
struct timekeeper *tk = &tk_core.timekeeper; |
155ec6022
|
1016 |
struct clocksource *clock; |
8524070b7
|
1017 |
unsigned long flags; |
7d489d15c
|
1018 1019 |
struct timespec64 now, boot, tmp; struct timespec ts; |
31ade3069
|
1020 |
|
7d489d15c
|
1021 1022 1023 |
read_persistent_clock(&ts); now = timespec_to_timespec64(ts); if (!timespec64_valid_strict(&now)) { |
4e8b14526
|
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
|
1030 1031 |
} else if (now.tv_sec || now.tv_nsec) persistent_clock_exist = true; |
4e8b14526
|
1032 |
|
7d489d15c
|
1033 1034 1035 |
read_boot_clock(&ts); boot = timespec_to_timespec64(ts); if (!timespec64_valid_strict(&boot)) { |
4e8b14526
|
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
|
1043 |
|
9a7a71b1d
|
1044 |
raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd1
|
1045 |
write_seqcount_begin(&tk_core.seq); |
06c017fdd
|
1046 |
ntp_init(); |
f1b82746c
|
1047 |
clock = clocksource_default_clock(); |
a0f7d48bf
|
1048 1049 |
if (clock->enable) clock->enable(clock); |
4e250fdde
|
1050 |
tk_setup_internals(tk, clock); |
8524070b7
|
1051 |
|
4e250fdde
|
1052 1053 1054 |
tk_set_xtime(tk, &now); tk->raw_time.tv_sec = 0; tk->raw_time.tv_nsec = 0; |
f519b1a2e
|
1055 |
tk->base_raw.tv64 = 0; |
1e75fa8be
|
1056 |
if (boot.tv_sec == 0 && boot.tv_nsec == 0) |
4e250fdde
|
1057 |
boot = tk_xtime(tk); |
1e75fa8be
|
1058 |
|
7d489d15c
|
1059 |
set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec); |
4e250fdde
|
1060 |
tk_set_wall_to_mono(tk, tmp); |
6d0ef903e
|
1061 |
|
f111adfdd
|
1062 |
timekeeping_update(tk, TK_MIRROR); |
48cdc135d
|
1063 |
|
3fdb14fd1
|
1064 |
write_seqcount_end(&tk_core.seq); |
9a7a71b1d
|
1065 |
raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
8524070b7
|
1066 |
} |
8524070b7
|
1067 |
/* time in seconds when suspend began */ |
7d489d15c
|
1068 |
static struct timespec64 timekeeping_suspend_time; |
8524070b7
|
1069 1070 |
/** |
304529b1b
|
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
|
1077 |
static void __timekeeping_inject_sleeptime(struct timekeeper *tk, |
7d489d15c
|
1078 |
struct timespec64 *delta) |
304529b1b
|
1079 |
{ |
7d489d15c
|
1080 |
if (!timespec64_valid_strict(delta)) { |
6d9bcb621
|
1081 1082 1083 1084 |
printk_deferred(KERN_WARNING "__timekeeping_inject_sleeptime: Invalid " "sleep delta value! "); |
cb5de2f8d
|
1085 1086 |
return; } |
f726a697d
|
1087 |
tk_xtime_add(tk, delta); |
7d489d15c
|
1088 |
tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta)); |
47da70d32
|
1089 |
tk_update_sleep_time(tk, timespec64_to_ktime(*delta)); |
5c83545f2
|
1090 |
tk_debug_account_sleep_time(delta); |
304529b1b
|
1091 |
} |
304529b1b
|
1092 |
/** |
04d908908
|
1093 1094 |
* timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values * @delta: pointer to a timespec64 delta value |
304529b1b
|
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
|
1102 |
void timekeeping_inject_sleeptime64(struct timespec64 *delta) |
304529b1b
|
1103 |
{ |
3fdb14fd1
|
1104 |
struct timekeeper *tk = &tk_core.timekeeper; |
92c1d3ed4
|
1105 |
unsigned long flags; |
304529b1b
|
1106 |
|
31ade3069
|
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
|
1112 |
return; |
9a7a71b1d
|
1113 |
raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd1
|
1114 |
write_seqcount_begin(&tk_core.seq); |
70471f2f0
|
1115 |
|
4e250fdde
|
1116 |
timekeeping_forward_now(tk); |
304529b1b
|
1117 |
|
04d908908
|
1118 |
__timekeeping_inject_sleeptime(tk, delta); |
304529b1b
|
1119 |
|
780427f0e
|
1120 |
timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
304529b1b
|
1121 |
|
3fdb14fd1
|
1122 |
write_seqcount_end(&tk_core.seq); |
9a7a71b1d
|
1123 |
raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
304529b1b
|
1124 1125 1126 1127 |
/* signal hrtimers about time change */ clock_was_set(); } |
304529b1b
|
1128 |
/** |
8524070b7
|
1129 |
* timekeeping_resume - Resumes the generic timekeeping subsystem. |
8524070b7
|
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
|
1135 |
static void timekeeping_resume(void) |
8524070b7
|
1136 |
{ |
3fdb14fd1
|
1137 |
struct timekeeper *tk = &tk_core.timekeeper; |
d28ede837
|
1138 |
struct clocksource *clock = tk->tkr.clock; |
92c1d3ed4
|
1139 |
unsigned long flags; |
7d489d15c
|
1140 1141 |
struct timespec64 ts_new, ts_delta; struct timespec tmp; |
e445cf1c4
|
1142 1143 |
cycle_t cycle_now, cycle_delta; bool suspendtime_found = false; |
d4f587c67
|
1144 |
|
7d489d15c
|
1145 1146 |
read_persistent_clock(&tmp); ts_new = timespec_to_timespec64(tmp); |
8524070b7
|
1147 |
|
adc78e6b9
|
1148 |
clockevents_resume(); |
d10ff3fb6
|
1149 |
clocksource_resume(); |
9a7a71b1d
|
1150 |
raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd1
|
1151 |
write_seqcount_begin(&tk_core.seq); |
8524070b7
|
1152 |
|
e445cf1c4
|
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
|
1165 |
cycle_now = tk->tkr.read(clock); |
e445cf1c4
|
1166 |
if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) && |
d28ede837
|
1167 |
cycle_now > tk->tkr.cycle_last) { |
e445cf1c4
|
1168 1169 1170 1171 |
u64 num, max = ULLONG_MAX; u32 mult = clock->mult; u32 shift = clock->shift; s64 nsec = 0; |
d28ede837
|
1172 1173 |
cycle_delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask); |
e445cf1c4
|
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
|
1187 |
ts_delta = ns_to_timespec64(nsec); |
e445cf1c4
|
1188 |
suspendtime_found = true; |
7d489d15c
|
1189 1190 |
} else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) { ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time); |
e445cf1c4
|
1191 |
suspendtime_found = true; |
8524070b7
|
1192 |
} |
e445cf1c4
|
1193 1194 1195 1196 1197 |
if (suspendtime_found) __timekeeping_inject_sleeptime(tk, &ts_delta); /* Re-base the last cycle value */ |
d28ede837
|
1198 |
tk->tkr.cycle_last = cycle_now; |
4e250fdde
|
1199 |
tk->ntp_error = 0; |
8524070b7
|
1200 |
timekeeping_suspended = 0; |
780427f0e
|
1201 |
timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); |
3fdb14fd1
|
1202 |
write_seqcount_end(&tk_core.seq); |
9a7a71b1d
|
1203 |
raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
8524070b7
|
1204 1205 1206 1207 1208 1209 |
touch_softlockup_watchdog(); clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL); /* Resume hrtimers */ |
b12a03ce4
|
1210 |
hrtimers_resume(); |
8524070b7
|
1211 |
} |
e1a85b2c5
|
1212 |
static int timekeeping_suspend(void) |
8524070b7
|
1213 |
{ |
3fdb14fd1
|
1214 |
struct timekeeper *tk = &tk_core.timekeeper; |
92c1d3ed4
|
1215 |
unsigned long flags; |
7d489d15c
|
1216 1217 1218 |
struct timespec64 delta, delta_delta; static struct timespec64 old_delta; struct timespec tmp; |
8524070b7
|
1219 |
|
7d489d15c
|
1220 1221 |
read_persistent_clock(&tmp); timekeeping_suspend_time = timespec_to_timespec64(tmp); |
3be909506
|
1222 |
|
0d6bd9953
|
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
|
1230 |
raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd1
|
1231 |
write_seqcount_begin(&tk_core.seq); |
4e250fdde
|
1232 |
timekeeping_forward_now(tk); |
8524070b7
|
1233 |
timekeeping_suspended = 1; |
cb33217b1
|
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
|
1241 1242 |
delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time); delta_delta = timespec64_sub(delta, old_delta); |
cb33217b1
|
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
|
1252 |
timespec64_add(timekeeping_suspend_time, delta_delta); |
cb33217b1
|
1253 |
} |
330a1617b
|
1254 1255 |
timekeeping_update(tk, TK_MIRROR); |
3fdb14fd1
|
1256 |
write_seqcount_end(&tk_core.seq); |
9a7a71b1d
|
1257 |
raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
8524070b7
|
1258 1259 |
clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL); |
c54a42b19
|
1260 |
clocksource_suspend(); |
adc78e6b9
|
1261 |
clockevents_suspend(); |
8524070b7
|
1262 1263 1264 1265 1266 |
return 0; } /* sysfs resume/suspend bits for timekeeping */ |
e1a85b2c5
|
1267 |
static struct syscore_ops timekeeping_syscore_ops = { |
8524070b7
|
1268 1269 |
.resume = timekeeping_resume, .suspend = timekeeping_suspend, |
8524070b7
|
1270 |
}; |
e1a85b2c5
|
1271 |
static int __init timekeeping_init_ops(void) |
8524070b7
|
1272 |
{ |
e1a85b2c5
|
1273 1274 |
register_syscore_ops(&timekeeping_syscore_ops); return 0; |
8524070b7
|
1275 |
} |
e1a85b2c5
|
1276 |
device_initcall(timekeeping_init_ops); |
8524070b7
|
1277 1278 |
/* |
dc491596f
|
1279 |
* Apply a multiplier adjustment to the timekeeper |
8524070b7
|
1280 |
*/ |
dc491596f
|
1281 1282 1283 1284 |
static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, s64 offset, bool negative, int adj_scale) |
8524070b7
|
1285 |
{ |
dc491596f
|
1286 1287 |
s64 interval = tk->cycle_interval; s32 mult_adj = 1; |
8524070b7
|
1288 |
|
dc491596f
|
1289 1290 1291 1292 |
if (negative) { mult_adj = -mult_adj; interval = -interval; offset = -offset; |
1d17d1748
|
1293 |
} |
dc491596f
|
1294 1295 1296 |
mult_adj <<= adj_scale; interval <<= adj_scale; offset <<= adj_scale; |
8524070b7
|
1297 |
|
c2bc11113
|
1298 1299 1300 |
/* * So the following can be confusing. * |
dc491596f
|
1301 |
* To keep things simple, lets assume mult_adj == 1 for now. |
c2bc11113
|
1302 |
* |
dc491596f
|
1303 |
* When mult_adj != 1, remember that the interval and offset values |
c2bc11113
|
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
|
1347 |
if ((mult_adj > 0) && (tk->tkr.mult + mult_adj < mult_adj)) { |
6067dc5a8
|
1348 1349 1350 1351 |
/* NTP adjustment caused clocksource mult overflow */ WARN_ON_ONCE(1); return; } |
dc491596f
|
1352 |
tk->tkr.mult += mult_adj; |
f726a697d
|
1353 |
tk->xtime_interval += interval; |
d28ede837
|
1354 |
tk->tkr.xtime_nsec -= offset; |
f726a697d
|
1355 |
tk->ntp_error -= (interval - offset) << tk->ntp_error_shift; |
dc491596f
|
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
|
1374 |
tk->ntp_tick = ntp_tick_length(); |
dc491596f
|
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
|
1415 1416 |
(abs(tk->tkr.mult - tk->tkr.clock->mult) > tk->tkr.clock->maxadj))) { |
dc491596f
|
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
|
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
|
1438 1439 1440 |
if (unlikely((s64)tk->tkr.xtime_nsec < 0)) { s64 neg = -(s64)tk->tkr.xtime_nsec; tk->tkr.xtime_nsec = 0; |
f726a697d
|
1441 |
tk->ntp_error += neg << tk->ntp_error_shift; |
2a8c0883c
|
1442 |
} |
8524070b7
|
1443 1444 1445 |
} /** |
1f4f94870
|
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
|
1453 |
static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk) |
1f4f94870
|
1454 |
{ |
d28ede837
|
1455 |
u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr.shift; |
5258d3f25
|
1456 |
unsigned int clock_set = 0; |
1f4f94870
|
1457 |
|
d28ede837
|
1458 |
while (tk->tkr.xtime_nsec >= nsecps) { |
1f4f94870
|
1459 |
int leap; |
d28ede837
|
1460 |
tk->tkr.xtime_nsec -= nsecps; |
1f4f94870
|
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
|
1465 |
if (unlikely(leap)) { |
7d489d15c
|
1466 |
struct timespec64 ts; |
6d0ef903e
|
1467 1468 |
tk->xtime_sec += leap; |
1f4f94870
|
1469 |
|
6d0ef903e
|
1470 1471 1472 |
ts.tv_sec = leap; ts.tv_nsec = 0; tk_set_wall_to_mono(tk, |
7d489d15c
|
1473 |
timespec64_sub(tk->wall_to_monotonic, ts)); |
6d0ef903e
|
1474 |
|
cc244ddae
|
1475 |
__timekeeping_set_tai_offset(tk, tk->tai_offset - leap); |
5258d3f25
|
1476 |
clock_set = TK_CLOCK_WAS_SET; |
6d0ef903e
|
1477 |
} |
1f4f94870
|
1478 |
} |
5258d3f25
|
1479 |
return clock_set; |
1f4f94870
|
1480 |
} |
1f4f94870
|
1481 |
/** |
a092ff0f9
|
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
|
1490 |
static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset, |
5258d3f25
|
1491 1492 |
u32 shift, unsigned int *clock_set) |
a092ff0f9
|
1493 |
{ |
23a9537a6
|
1494 |
cycle_t interval = tk->cycle_interval << shift; |
deda2e819
|
1495 |
u64 raw_nsecs; |
a092ff0f9
|
1496 |
|
f726a697d
|
1497 |
/* If the offset is smaller then a shifted interval, do nothing */ |
23a9537a6
|
1498 |
if (offset < interval) |
a092ff0f9
|
1499 1500 1501 |
return offset; /* Accumulate one shifted interval */ |
23a9537a6
|
1502 |
offset -= interval; |
d28ede837
|
1503 |
tk->tkr.cycle_last += interval; |
a092ff0f9
|
1504 |
|
d28ede837
|
1505 |
tk->tkr.xtime_nsec += tk->xtime_interval << shift; |
5258d3f25
|
1506 |
*clock_set |= accumulate_nsecs_to_secs(tk); |
a092ff0f9
|
1507 |
|
deda2e819
|
1508 |
/* Accumulate raw time */ |
5b3900cd4
|
1509 |
raw_nsecs = (u64)tk->raw_interval << shift; |
f726a697d
|
1510 |
raw_nsecs += tk->raw_time.tv_nsec; |
c7dcf87a6
|
1511 1512 1513 |
if (raw_nsecs >= NSEC_PER_SEC) { u64 raw_secs = raw_nsecs; raw_nsecs = do_div(raw_secs, NSEC_PER_SEC); |
f726a697d
|
1514 |
tk->raw_time.tv_sec += raw_secs; |
a092ff0f9
|
1515 |
} |
f726a697d
|
1516 |
tk->raw_time.tv_nsec = raw_nsecs; |
a092ff0f9
|
1517 1518 |
/* Accumulate error between NTP and clock interval */ |
375f45b5b
|
1519 |
tk->ntp_error += tk->ntp_tick << shift; |
f726a697d
|
1520 1521 |
tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) << (tk->ntp_error_shift + shift); |
a092ff0f9
|
1522 1523 1524 |
return offset; } |
8524070b7
|
1525 1526 1527 |
/** * update_wall_time - Uses the current clocksource to increment the wall time * |
8524070b7
|
1528 |
*/ |
47a1b7963
|
1529 |
void update_wall_time(void) |
8524070b7
|
1530 |
{ |
3fdb14fd1
|
1531 |
struct timekeeper *real_tk = &tk_core.timekeeper; |
48cdc135d
|
1532 |
struct timekeeper *tk = &shadow_timekeeper; |
8524070b7
|
1533 |
cycle_t offset; |
a092ff0f9
|
1534 |
int shift = 0, maxshift; |
5258d3f25
|
1535 |
unsigned int clock_set = 0; |
70471f2f0
|
1536 |
unsigned long flags; |
9a7a71b1d
|
1537 |
raw_spin_lock_irqsave(&timekeeper_lock, flags); |
8524070b7
|
1538 1539 1540 |
/* Make sure we're fully resumed: */ if (unlikely(timekeeping_suspended)) |
70471f2f0
|
1541 |
goto out; |
8524070b7
|
1542 |
|
592913ecb
|
1543 |
#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET |
48cdc135d
|
1544 |
offset = real_tk->cycle_interval; |
592913ecb
|
1545 |
#else |
d28ede837
|
1546 1547 |
offset = clocksource_delta(tk->tkr.read(tk->tkr.clock), tk->tkr.cycle_last, tk->tkr.mask); |
8524070b7
|
1548 |
#endif |
8524070b7
|
1549 |
|
bf2ac3121
|
1550 |
/* Check if there's really nothing to do */ |
48cdc135d
|
1551 |
if (offset < real_tk->cycle_interval) |
bf2ac3121
|
1552 |
goto out; |
a092ff0f9
|
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
|
1557 |
* that is smaller than the offset. We then accumulate that |
a092ff0f9
|
1558 1559 |
* chunk in one go, and then try to consume the next smaller * doubled multiple. |
8524070b7
|
1560 |
*/ |
4e250fdde
|
1561 |
shift = ilog2(offset) - ilog2(tk->cycle_interval); |
a092ff0f9
|
1562 |
shift = max(0, shift); |
88b28adf6
|
1563 |
/* Bound shift to one less than what overflows tick_length */ |
ea7cf49a7
|
1564 |
maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1; |
a092ff0f9
|
1565 |
shift = min(shift, maxshift); |
4e250fdde
|
1566 |
while (offset >= tk->cycle_interval) { |
5258d3f25
|
1567 1568 |
offset = logarithmic_accumulation(tk, offset, shift, &clock_set); |
4e250fdde
|
1569 |
if (offset < tk->cycle_interval<<shift) |
830ec0458
|
1570 |
shift--; |
8524070b7
|
1571 1572 1573 |
} /* correct the clock when NTP error is too big */ |
4e250fdde
|
1574 |
timekeeping_adjust(tk, offset); |
8524070b7
|
1575 |
|
6a867a395
|
1576 |
/* |
92bb1fcf5
|
1577 1578 1579 1580 |
* XXX This can be killed once everyone converts * to the new update_vsyscall. */ old_vsyscall_fixup(tk); |
8524070b7
|
1581 |
|
6a867a395
|
1582 1583 |
/* * Finally, make sure that after the rounding |
1e75fa8be
|
1584 |
* xtime_nsec isn't larger than NSEC_PER_SEC |
6a867a395
|
1585 |
*/ |
5258d3f25
|
1586 |
clock_set |= accumulate_nsecs_to_secs(tk); |
83f57a11d
|
1587 |
|
3fdb14fd1
|
1588 |
write_seqcount_begin(&tk_core.seq); |
48cdc135d
|
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
|
1596 |
* memcpy under the tk_core.seq against one before we start |
48cdc135d
|
1597 1598 1599 |
* updating. */ memcpy(real_tk, tk, sizeof(*tk)); |
5258d3f25
|
1600 |
timekeeping_update(real_tk, clock_set); |
3fdb14fd1
|
1601 |
write_seqcount_end(&tk_core.seq); |
ca4523cda
|
1602 |
out: |
9a7a71b1d
|
1603 |
raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
47a1b7963
|
1604 |
if (clock_set) |
cab5e127e
|
1605 1606 |
/* Have to call _delayed version, since in irq context*/ clock_was_set_delayed(); |
8524070b7
|
1607 |
} |
7c3f1a573
|
1608 1609 1610 1611 1612 |
/** * getboottime - Return the real time of system boot. * @ts: pointer to the timespec to be set * |
abb3a4ea2
|
1613 |
* Returns the wall-time of boot in a timespec. |
7c3f1a573
|
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
|
1622 |
struct timekeeper *tk = &tk_core.timekeeper; |
02cba1598
|
1623 1624 1625 |
ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot); *ts = ktime_to_timespec(t); |
7c3f1a573
|
1626 |
} |
c93d89f3d
|
1627 |
EXPORT_SYMBOL_GPL(getboottime); |
7c3f1a573
|
1628 |
|
17c38b749
|
1629 1630 |
unsigned long get_seconds(void) { |
3fdb14fd1
|
1631 |
struct timekeeper *tk = &tk_core.timekeeper; |
4e250fdde
|
1632 1633 |
return tk->xtime_sec; |
17c38b749
|
1634 1635 |
} EXPORT_SYMBOL(get_seconds); |
da15cfdae
|
1636 1637 |
struct timespec __current_kernel_time(void) { |
3fdb14fd1
|
1638 |
struct timekeeper *tk = &tk_core.timekeeper; |
4e250fdde
|
1639 |
|
7d489d15c
|
1640 |
return timespec64_to_timespec(tk_xtime(tk)); |
da15cfdae
|
1641 |
} |
17c38b749
|
1642 |
|
2c6b47de1
|
1643 1644 |
struct timespec current_kernel_time(void) { |
3fdb14fd1
|
1645 |
struct timekeeper *tk = &tk_core.timekeeper; |
7d489d15c
|
1646 |
struct timespec64 now; |
2c6b47de1
|
1647 1648 1649 |
unsigned long seq; do { |
3fdb14fd1
|
1650 |
seq = read_seqcount_begin(&tk_core.seq); |
83f57a11d
|
1651 |
|
4e250fdde
|
1652 |
now = tk_xtime(tk); |
3fdb14fd1
|
1653 |
} while (read_seqcount_retry(&tk_core.seq, seq)); |
2c6b47de1
|
1654 |
|
7d489d15c
|
1655 |
return timespec64_to_timespec(now); |
2c6b47de1
|
1656 |
} |
2c6b47de1
|
1657 |
EXPORT_SYMBOL(current_kernel_time); |
da15cfdae
|
1658 |
|
334334b5f
|
1659 |
struct timespec64 get_monotonic_coarse64(void) |
da15cfdae
|
1660 |
{ |
3fdb14fd1
|
1661 |
struct timekeeper *tk = &tk_core.timekeeper; |
7d489d15c
|
1662 |
struct timespec64 now, mono; |
da15cfdae
|
1663 1664 1665 |
unsigned long seq; do { |
3fdb14fd1
|
1666 |
seq = read_seqcount_begin(&tk_core.seq); |
83f57a11d
|
1667 |
|
4e250fdde
|
1668 1669 |
now = tk_xtime(tk); mono = tk->wall_to_monotonic; |
3fdb14fd1
|
1670 |
} while (read_seqcount_retry(&tk_core.seq, seq)); |
da15cfdae
|
1671 |
|
7d489d15c
|
1672 |
set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec, |
da15cfdae
|
1673 |
now.tv_nsec + mono.tv_nsec); |
7d489d15c
|
1674 |
|
334334b5f
|
1675 |
return now; |
da15cfdae
|
1676 |
} |
871cf1e5f
|
1677 1678 |
/* |
d6ad41876
|
1679 |
* Must hold jiffies_lock |
871cf1e5f
|
1680 1681 1682 1683 |
*/ void do_timer(unsigned long ticks) { jiffies_64 += ticks; |
871cf1e5f
|
1684 1685 |
calc_global_load(ticks); } |
48cf76f71
|
1686 1687 |
/** |
76f410889
|
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
|
1694 |
*/ |
76f410889
|
1695 1696 |
ktime_t ktime_get_update_offsets_tick(ktime_t *offs_real, ktime_t *offs_boot, ktime_t *offs_tai) |
48cf76f71
|
1697 |
{ |
3fdb14fd1
|
1698 |
struct timekeeper *tk = &tk_core.timekeeper; |
76f410889
|
1699 |
unsigned int seq; |
48064f5f6
|
1700 1701 |
ktime_t base; u64 nsecs; |
48cf76f71
|
1702 1703 |
do { |
3fdb14fd1
|
1704 |
seq = read_seqcount_begin(&tk_core.seq); |
76f410889
|
1705 |
|
d28ede837
|
1706 1707 |
base = tk->tkr.base_mono; nsecs = tk->tkr.xtime_nsec >> tk->tkr.shift; |
48064f5f6
|
1708 |
|
76f410889
|
1709 1710 1711 |
*offs_real = tk->offs_real; *offs_boot = tk->offs_boot; *offs_tai = tk->offs_tai; |
3fdb14fd1
|
1712 |
} while (read_seqcount_retry(&tk_core.seq, seq)); |
76f410889
|
1713 |
|
48064f5f6
|
1714 |
return ktime_add_ns(base, nsecs); |
48cf76f71
|
1715 |
} |
f0af911a9
|
1716 |
|
f6c06abfb
|
1717 1718 |
#ifdef CONFIG_HIGH_RES_TIMERS /** |
76f410889
|
1719 |
* ktime_get_update_offsets_now - hrtimer helper |
f6c06abfb
|
1720 1721 |
* @offs_real: pointer to storage for monotonic -> realtime offset * @offs_boot: pointer to storage for monotonic -> boottime offset |
b7bc50e45
|
1722 |
* @offs_tai: pointer to storage for monotonic -> clock tai offset |
f6c06abfb
|
1723 1724 |
* * Returns current monotonic time and updates the offsets |
b7bc50e45
|
1725 |
* Called from hrtimer_interrupt() or retrigger_next_event() |
f6c06abfb
|
1726 |
*/ |
76f410889
|
1727 |
ktime_t ktime_get_update_offsets_now(ktime_t *offs_real, ktime_t *offs_boot, |
90adda98b
|
1728 |
ktime_t *offs_tai) |
f6c06abfb
|
1729 |
{ |
3fdb14fd1
|
1730 |
struct timekeeper *tk = &tk_core.timekeeper; |
f6c06abfb
|
1731 |
unsigned int seq; |
a37c0aad6
|
1732 1733 |
ktime_t base; u64 nsecs; |
f6c06abfb
|
1734 1735 |
do { |
3fdb14fd1
|
1736 |
seq = read_seqcount_begin(&tk_core.seq); |
f6c06abfb
|
1737 |
|
d28ede837
|
1738 |
base = tk->tkr.base_mono; |
0e5ac3a8b
|
1739 |
nsecs = timekeeping_get_ns(&tk->tkr); |
f6c06abfb
|
1740 |
|
4e250fdde
|
1741 1742 |
*offs_real = tk->offs_real; *offs_boot = tk->offs_boot; |
90adda98b
|
1743 |
*offs_tai = tk->offs_tai; |
3fdb14fd1
|
1744 |
} while (read_seqcount_retry(&tk_core.seq, seq)); |
f6c06abfb
|
1745 |
|
a37c0aad6
|
1746 |
return ktime_add_ns(base, nsecs); |
f6c06abfb
|
1747 1748 |
} #endif |
f0af911a9
|
1749 |
/** |
aa6f9c595
|
1750 1751 1752 1753 |
* do_adjtimex() - Accessor function to NTP __do_adjtimex function */ int do_adjtimex(struct timex *txc) { |
3fdb14fd1
|
1754 |
struct timekeeper *tk = &tk_core.timekeeper; |
06c017fdd
|
1755 |
unsigned long flags; |
7d489d15c
|
1756 |
struct timespec64 ts; |
4e8f8b34b
|
1757 |
s32 orig_tai, tai; |
e4085693f
|
1758 1759 1760 1761 1762 1763 |
int ret; /* Validate the data before disabling interrupts */ ret = ntp_validate_timex(txc); if (ret) return ret; |
cef90377f
|
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
|
1774 |
getnstimeofday64(&ts); |
87ace39b7
|
1775 |
|
06c017fdd
|
1776 |
raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd1
|
1777 |
write_seqcount_begin(&tk_core.seq); |
06c017fdd
|
1778 |
|
4e8f8b34b
|
1779 |
orig_tai = tai = tk->tai_offset; |
87ace39b7
|
1780 |
ret = __do_adjtimex(txc, &ts, &tai); |
aa6f9c595
|
1781 |
|
4e8f8b34b
|
1782 1783 |
if (tai != orig_tai) { __timekeeping_set_tai_offset(tk, tai); |
f55c07607
|
1784 |
timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); |
4e8f8b34b
|
1785 |
} |
3fdb14fd1
|
1786 |
write_seqcount_end(&tk_core.seq); |
06c017fdd
|
1787 |
raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
6fdda9a9c
|
1788 1789 |
if (tai != orig_tai) clock_was_set(); |
7bd360144
|
1790 |
ntp_notify_cmos_timer(); |
87ace39b7
|
1791 1792 |
return ret; } |
aa6f9c595
|
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
|
1800 1801 1802 |
unsigned long flags; raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd1
|
1803 |
write_seqcount_begin(&tk_core.seq); |
06c017fdd
|
1804 |
|
aa6f9c595
|
1805 |
__hardpps(phase_ts, raw_ts); |
06c017fdd
|
1806 |
|
3fdb14fd1
|
1807 |
write_seqcount_end(&tk_core.seq); |
06c017fdd
|
1808 |
raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
aa6f9c595
|
1809 1810 1811 1812 1813 |
} EXPORT_SYMBOL(hardpps); #endif /** |
f0af911a9
|
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
|
1821 |
write_seqlock(&jiffies_lock); |
f0af911a9
|
1822 |
do_timer(ticks); |
d6ad41876
|
1823 |
write_sequnlock(&jiffies_lock); |
47a1b7963
|
1824 |
update_wall_time(); |
f0af911a9
|
1825 |
} |