]>
Commit | Line | Data |
---|---|---|
8524070b JS |
1 | /* |
2 | * linux/kernel/time/timekeeping.c | |
3 | * | |
4 | * Kernel timekeeping code and accessor functions | |
5 | * | |
6 | * This code was moved from linux/kernel/timer.c. | |
7 | * Please see that file for copyright and history logs. | |
8 | * | |
9 | */ | |
10 | ||
d7b4202e | 11 | #include <linux/timekeeper_internal.h> |
8524070b JS |
12 | #include <linux/module.h> |
13 | #include <linux/interrupt.h> | |
14 | #include <linux/percpu.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/mm.h> | |
d43c36dc | 17 | #include <linux/sched.h> |
e1a85b2c | 18 | #include <linux/syscore_ops.h> |
8524070b JS |
19 | #include <linux/clocksource.h> |
20 | #include <linux/jiffies.h> | |
21 | #include <linux/time.h> | |
22 | #include <linux/tick.h> | |
75c5158f | 23 | #include <linux/stop_machine.h> |
e0b306fe | 24 | #include <linux/pvclock_gtod.h> |
52f5684c | 25 | #include <linux/compiler.h> |
8524070b | 26 | |
eb93e4d9 | 27 | #include "tick-internal.h" |
aa6f9c59 | 28 | #include "ntp_internal.h" |
5c83545f | 29 | #include "timekeeping_internal.h" |
155ec602 | 30 | |
04397fe9 DV |
31 | #define TK_CLEAR_NTP (1 << 0) |
32 | #define TK_MIRROR (1 << 1) | |
780427f0 | 33 | #define TK_CLOCK_WAS_SET (1 << 2) |
04397fe9 | 34 | |
3fdb14fd TG |
35 | /* |
36 | * The most important data for readout fits into a single 64 byte | |
37 | * cache line. | |
38 | */ | |
39 | static struct { | |
40 | seqcount_t seq; | |
41 | struct timekeeper timekeeper; | |
42 | } tk_core ____cacheline_aligned; | |
43 | ||
9a7a71b1 | 44 | static DEFINE_RAW_SPINLOCK(timekeeper_lock); |
48cdc135 | 45 | static struct timekeeper shadow_timekeeper; |
155ec602 | 46 | |
8fcce546 JS |
47 | /* flag for if timekeeping is suspended */ |
48 | int __read_mostly timekeeping_suspended; | |
49 | ||
31ade306 FT |
50 | /* Flag for if there is a persistent clock on this platform */ |
51 | bool __read_mostly persistent_clock_exist = false; | |
52 | ||
1e75fa8b JS |
53 | static inline void tk_normalize_xtime(struct timekeeper *tk) |
54 | { | |
55 | while (tk->xtime_nsec >= ((u64)NSEC_PER_SEC << tk->shift)) { | |
56 | tk->xtime_nsec -= (u64)NSEC_PER_SEC << tk->shift; | |
57 | tk->xtime_sec++; | |
58 | } | |
59 | } | |
60 | ||
c905fae4 TG |
61 | static inline struct timespec64 tk_xtime(struct timekeeper *tk) |
62 | { | |
63 | struct timespec64 ts; | |
64 | ||
65 | ts.tv_sec = tk->xtime_sec; | |
66 | ts.tv_nsec = (long)(tk->xtime_nsec >> tk->shift); | |
67 | return ts; | |
68 | } | |
69 | ||
7d489d15 | 70 | static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts) |
1e75fa8b JS |
71 | { |
72 | tk->xtime_sec = ts->tv_sec; | |
b44d50dc | 73 | tk->xtime_nsec = (u64)ts->tv_nsec << tk->shift; |
1e75fa8b JS |
74 | } |
75 | ||
7d489d15 | 76 | static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts) |
1e75fa8b JS |
77 | { |
78 | tk->xtime_sec += ts->tv_sec; | |
b44d50dc | 79 | tk->xtime_nsec += (u64)ts->tv_nsec << tk->shift; |
784ffcbb | 80 | tk_normalize_xtime(tk); |
1e75fa8b | 81 | } |
8fcce546 | 82 | |
7d489d15 | 83 | static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm) |
6d0ef903 | 84 | { |
7d489d15 | 85 | struct timespec64 tmp; |
6d0ef903 JS |
86 | |
87 | /* | |
88 | * Verify consistency of: offset_real = -wall_to_monotonic | |
89 | * before modifying anything | |
90 | */ | |
7d489d15 | 91 | set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec, |
6d0ef903 | 92 | -tk->wall_to_monotonic.tv_nsec); |
7d489d15 | 93 | WARN_ON_ONCE(tk->offs_real.tv64 != timespec64_to_ktime(tmp).tv64); |
6d0ef903 | 94 | tk->wall_to_monotonic = wtm; |
7d489d15 JS |
95 | set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec); |
96 | tk->offs_real = timespec64_to_ktime(tmp); | |
04005f60 | 97 | tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0)); |
6d0ef903 JS |
98 | } |
99 | ||
7d489d15 | 100 | static void tk_set_sleep_time(struct timekeeper *tk, struct timespec64 t) |
6d0ef903 JS |
101 | { |
102 | /* Verify consistency before modifying */ | |
7d489d15 | 103 | WARN_ON_ONCE(tk->offs_boot.tv64 != timespec64_to_ktime(tk->total_sleep_time).tv64); |
6d0ef903 JS |
104 | |
105 | tk->total_sleep_time = t; | |
7d489d15 | 106 | tk->offs_boot = timespec64_to_ktime(t); |
6d0ef903 JS |
107 | } |
108 | ||
155ec602 | 109 | /** |
d26e4fe0 | 110 | * tk_setup_internals - Set up internals to use clocksource clock. |
155ec602 | 111 | * |
d26e4fe0 | 112 | * @tk: The target timekeeper to setup. |
155ec602 MS |
113 | * @clock: Pointer to clocksource. |
114 | * | |
115 | * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment | |
116 | * pair and interval request. | |
117 | * | |
118 | * Unless you're the timekeeping code, you should not be using this! | |
119 | */ | |
f726a697 | 120 | static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) |
155ec602 MS |
121 | { |
122 | cycle_t interval; | |
a386b5af | 123 | u64 tmp, ntpinterval; |
1e75fa8b | 124 | struct clocksource *old_clock; |
155ec602 | 125 | |
f726a697 JS |
126 | old_clock = tk->clock; |
127 | tk->clock = clock; | |
14a3b6ab | 128 | tk->cycle_last = clock->cycle_last = clock->read(clock); |
155ec602 MS |
129 | |
130 | /* Do the ns -> cycle conversion first, using original mult */ | |
131 | tmp = NTP_INTERVAL_LENGTH; | |
132 | tmp <<= clock->shift; | |
a386b5af | 133 | ntpinterval = tmp; |
0a544198 MS |
134 | tmp += clock->mult/2; |
135 | do_div(tmp, clock->mult); | |
155ec602 MS |
136 | if (tmp == 0) |
137 | tmp = 1; | |
138 | ||
139 | interval = (cycle_t) tmp; | |
f726a697 | 140 | tk->cycle_interval = interval; |
155ec602 MS |
141 | |
142 | /* Go back from cycles -> shifted ns */ | |
f726a697 JS |
143 | tk->xtime_interval = (u64) interval * clock->mult; |
144 | tk->xtime_remainder = ntpinterval - tk->xtime_interval; | |
145 | tk->raw_interval = | |
0a544198 | 146 | ((u64) interval * clock->mult) >> clock->shift; |
155ec602 | 147 | |
1e75fa8b JS |
148 | /* if changing clocks, convert xtime_nsec shift units */ |
149 | if (old_clock) { | |
150 | int shift_change = clock->shift - old_clock->shift; | |
151 | if (shift_change < 0) | |
f726a697 | 152 | tk->xtime_nsec >>= -shift_change; |
1e75fa8b | 153 | else |
f726a697 | 154 | tk->xtime_nsec <<= shift_change; |
1e75fa8b | 155 | } |
f726a697 | 156 | tk->shift = clock->shift; |
155ec602 | 157 | |
f726a697 JS |
158 | tk->ntp_error = 0; |
159 | tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift; | |
0a544198 MS |
160 | |
161 | /* | |
162 | * The timekeeper keeps its own mult values for the currently | |
163 | * active clocksource. These value will be adjusted via NTP | |
164 | * to counteract clock drifting. | |
165 | */ | |
f726a697 | 166 | tk->mult = clock->mult; |
155ec602 | 167 | } |
8524070b | 168 | |
2ba2a305 | 169 | /* Timekeeper helper functions. */ |
7b1f6207 SW |
170 | |
171 | #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET | |
e06fde37 TG |
172 | static u32 default_arch_gettimeoffset(void) { return 0; } |
173 | u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset; | |
7b1f6207 | 174 | #else |
e06fde37 | 175 | static inline u32 arch_gettimeoffset(void) { return 0; } |
7b1f6207 SW |
176 | #endif |
177 | ||
f726a697 | 178 | static inline s64 timekeeping_get_ns(struct timekeeper *tk) |
2ba2a305 MS |
179 | { |
180 | cycle_t cycle_now, cycle_delta; | |
181 | struct clocksource *clock; | |
1e75fa8b | 182 | s64 nsec; |
2ba2a305 MS |
183 | |
184 | /* read clocksource: */ | |
f726a697 | 185 | clock = tk->clock; |
2ba2a305 MS |
186 | cycle_now = clock->read(clock); |
187 | ||
188 | /* calculate the delta since the last update_wall_time: */ | |
189 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; | |
190 | ||
f726a697 JS |
191 | nsec = cycle_delta * tk->mult + tk->xtime_nsec; |
192 | nsec >>= tk->shift; | |
f2a5a085 | 193 | |
7b1f6207 | 194 | /* If arch requires, add in get_arch_timeoffset() */ |
e06fde37 | 195 | return nsec + arch_gettimeoffset(); |
2ba2a305 MS |
196 | } |
197 | ||
f726a697 | 198 | static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk) |
2ba2a305 MS |
199 | { |
200 | cycle_t cycle_now, cycle_delta; | |
201 | struct clocksource *clock; | |
f2a5a085 | 202 | s64 nsec; |
2ba2a305 MS |
203 | |
204 | /* read clocksource: */ | |
f726a697 | 205 | clock = tk->clock; |
2ba2a305 MS |
206 | cycle_now = clock->read(clock); |
207 | ||
208 | /* calculate the delta since the last update_wall_time: */ | |
209 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; | |
210 | ||
f2a5a085 JS |
211 | /* convert delta to nanoseconds. */ |
212 | nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift); | |
213 | ||
7b1f6207 | 214 | /* If arch requires, add in get_arch_timeoffset() */ |
e06fde37 | 215 | return nsec + arch_gettimeoffset(); |
2ba2a305 MS |
216 | } |
217 | ||
c905fae4 TG |
218 | #ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD |
219 | ||
220 | static inline void update_vsyscall(struct timekeeper *tk) | |
221 | { | |
222 | struct timespec xt; | |
223 | ||
224 | xt = tk_xtime(tk); | |
225 | update_vsyscall_old(&xt, &tk->wall_to_monotonic, tk->clock, tk->mult); | |
226 | } | |
227 | ||
228 | static inline void old_vsyscall_fixup(struct timekeeper *tk) | |
229 | { | |
230 | s64 remainder; | |
231 | ||
232 | /* | |
233 | * Store only full nanoseconds into xtime_nsec after rounding | |
234 | * it up and add the remainder to the error difference. | |
235 | * XXX - This is necessary to avoid small 1ns inconsistnecies caused | |
236 | * by truncating the remainder in vsyscalls. However, it causes | |
237 | * additional work to be done in timekeeping_adjust(). Once | |
238 | * the vsyscall implementations are converted to use xtime_nsec | |
239 | * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD | |
240 | * users are removed, this can be killed. | |
241 | */ | |
242 | remainder = tk->xtime_nsec & ((1ULL << tk->shift) - 1); | |
243 | tk->xtime_nsec -= remainder; | |
244 | tk->xtime_nsec += 1ULL << tk->shift; | |
245 | tk->ntp_error += remainder << tk->ntp_error_shift; | |
246 | tk->ntp_error -= (1ULL << tk->shift) << tk->ntp_error_shift; | |
247 | } | |
248 | #else | |
249 | #define old_vsyscall_fixup(tk) | |
250 | #endif | |
251 | ||
e0b306fe MT |
252 | static RAW_NOTIFIER_HEAD(pvclock_gtod_chain); |
253 | ||
780427f0 | 254 | static void update_pvclock_gtod(struct timekeeper *tk, bool was_set) |
e0b306fe | 255 | { |
780427f0 | 256 | raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk); |
e0b306fe MT |
257 | } |
258 | ||
259 | /** | |
260 | * pvclock_gtod_register_notifier - register a pvclock timedata update listener | |
e0b306fe MT |
261 | */ |
262 | int pvclock_gtod_register_notifier(struct notifier_block *nb) | |
263 | { | |
3fdb14fd | 264 | struct timekeeper *tk = &tk_core.timekeeper; |
e0b306fe MT |
265 | unsigned long flags; |
266 | int ret; | |
267 | ||
9a7a71b1 | 268 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
e0b306fe | 269 | ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb); |
780427f0 | 270 | update_pvclock_gtod(tk, true); |
9a7a71b1 | 271 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
e0b306fe MT |
272 | |
273 | return ret; | |
274 | } | |
275 | EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier); | |
276 | ||
277 | /** | |
278 | * pvclock_gtod_unregister_notifier - unregister a pvclock | |
279 | * timedata update listener | |
e0b306fe MT |
280 | */ |
281 | int pvclock_gtod_unregister_notifier(struct notifier_block *nb) | |
282 | { | |
e0b306fe MT |
283 | unsigned long flags; |
284 | int ret; | |
285 | ||
9a7a71b1 | 286 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
e0b306fe | 287 | ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb); |
9a7a71b1 | 288 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
e0b306fe MT |
289 | |
290 | return ret; | |
291 | } | |
292 | EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier); | |
293 | ||
9a7a71b1 | 294 | /* must hold timekeeper_lock */ |
04397fe9 | 295 | static void timekeeping_update(struct timekeeper *tk, unsigned int action) |
cc06268c | 296 | { |
04397fe9 | 297 | if (action & TK_CLEAR_NTP) { |
f726a697 | 298 | tk->ntp_error = 0; |
cc06268c TG |
299 | ntp_clear(); |
300 | } | |
576094b7 | 301 | update_vsyscall(tk); |
780427f0 | 302 | update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET); |
48cdc135 | 303 | |
04397fe9 | 304 | if (action & TK_MIRROR) |
3fdb14fd TG |
305 | memcpy(&shadow_timekeeper, &tk_core.timekeeper, |
306 | sizeof(tk_core.timekeeper)); | |
cc06268c TG |
307 | } |
308 | ||
8524070b | 309 | /** |
155ec602 | 310 | * timekeeping_forward_now - update clock to the current time |
8524070b | 311 | * |
9a055117 RZ |
312 | * Forward the current clock to update its state since the last call to |
313 | * update_wall_time(). This is useful before significant clock changes, | |
314 | * as it avoids having to deal with this time offset explicitly. | |
8524070b | 315 | */ |
f726a697 | 316 | static void timekeeping_forward_now(struct timekeeper *tk) |
8524070b JS |
317 | { |
318 | cycle_t cycle_now, cycle_delta; | |
155ec602 | 319 | struct clocksource *clock; |
9a055117 | 320 | s64 nsec; |
8524070b | 321 | |
f726a697 | 322 | clock = tk->clock; |
a0f7d48b | 323 | cycle_now = clock->read(clock); |
8524070b | 324 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; |
14a3b6ab | 325 | tk->cycle_last = clock->cycle_last = cycle_now; |
8524070b | 326 | |
f726a697 | 327 | tk->xtime_nsec += cycle_delta * tk->mult; |
7d27558c | 328 | |
7b1f6207 | 329 | /* If arch requires, add in get_arch_timeoffset() */ |
e06fde37 | 330 | tk->xtime_nsec += (u64)arch_gettimeoffset() << tk->shift; |
7d27558c | 331 | |
f726a697 | 332 | tk_normalize_xtime(tk); |
2d42244a | 333 | |
0a544198 | 334 | nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift); |
7d489d15 | 335 | timespec64_add_ns(&tk->raw_time, nsec); |
8524070b JS |
336 | } |
337 | ||
338 | /** | |
d6d29896 | 339 | * __getnstimeofday64 - Returns the time of day in a timespec64. |
8524070b JS |
340 | * @ts: pointer to the timespec to be set |
341 | * | |
1e817fb6 KC |
342 | * Updates the time of day in the timespec. |
343 | * Returns 0 on success, or -ve when suspended (timespec will be undefined). | |
8524070b | 344 | */ |
d6d29896 | 345 | int __getnstimeofday64(struct timespec64 *ts) |
8524070b | 346 | { |
3fdb14fd | 347 | struct timekeeper *tk = &tk_core.timekeeper; |
8524070b | 348 | unsigned long seq; |
1e75fa8b | 349 | s64 nsecs = 0; |
8524070b JS |
350 | |
351 | do { | |
3fdb14fd | 352 | seq = read_seqcount_begin(&tk_core.seq); |
8524070b | 353 | |
4e250fdd | 354 | ts->tv_sec = tk->xtime_sec; |
ec145bab | 355 | nsecs = timekeeping_get_ns(tk); |
8524070b | 356 | |
3fdb14fd | 357 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
8524070b | 358 | |
ec145bab | 359 | ts->tv_nsec = 0; |
d6d29896 | 360 | timespec64_add_ns(ts, nsecs); |
1e817fb6 KC |
361 | |
362 | /* | |
363 | * Do not bail out early, in case there were callers still using | |
364 | * the value, even in the face of the WARN_ON. | |
365 | */ | |
366 | if (unlikely(timekeeping_suspended)) | |
367 | return -EAGAIN; | |
368 | return 0; | |
369 | } | |
d6d29896 | 370 | EXPORT_SYMBOL(__getnstimeofday64); |
1e817fb6 KC |
371 | |
372 | /** | |
d6d29896 | 373 | * getnstimeofday64 - Returns the time of day in a timespec64. |
1e817fb6 KC |
374 | * @ts: pointer to the timespec to be set |
375 | * | |
376 | * Returns the time of day in a timespec (WARN if suspended). | |
377 | */ | |
d6d29896 | 378 | void getnstimeofday64(struct timespec64 *ts) |
1e817fb6 | 379 | { |
d6d29896 | 380 | WARN_ON(__getnstimeofday64(ts)); |
8524070b | 381 | } |
d6d29896 | 382 | EXPORT_SYMBOL(getnstimeofday64); |
8524070b | 383 | |
951ed4d3 MS |
384 | ktime_t ktime_get(void) |
385 | { | |
3fdb14fd | 386 | struct timekeeper *tk = &tk_core.timekeeper; |
951ed4d3 MS |
387 | unsigned int seq; |
388 | s64 secs, nsecs; | |
389 | ||
390 | WARN_ON(timekeeping_suspended); | |
391 | ||
392 | do { | |
3fdb14fd | 393 | seq = read_seqcount_begin(&tk_core.seq); |
4e250fdd JS |
394 | secs = tk->xtime_sec + tk->wall_to_monotonic.tv_sec; |
395 | nsecs = timekeeping_get_ns(tk) + tk->wall_to_monotonic.tv_nsec; | |
951ed4d3 | 396 | |
3fdb14fd | 397 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
24e4a8c3 JS |
398 | |
399 | return ktime_set(secs, nsecs); | |
951ed4d3 MS |
400 | } |
401 | EXPORT_SYMBOL_GPL(ktime_get); | |
402 | ||
403 | /** | |
d6d29896 | 404 | * ktime_get_ts64 - get the monotonic clock in timespec64 format |
951ed4d3 MS |
405 | * @ts: pointer to timespec variable |
406 | * | |
407 | * The function calculates the monotonic clock from the realtime | |
408 | * clock and the wall_to_monotonic offset and stores the result | |
409 | * in normalized timespec format in the variable pointed to by @ts. | |
410 | */ | |
d6d29896 | 411 | void ktime_get_ts64(struct timespec64 *ts) |
951ed4d3 | 412 | { |
3fdb14fd | 413 | struct timekeeper *tk = &tk_core.timekeeper; |
d6d29896 | 414 | struct timespec64 tomono; |
ec145bab | 415 | s64 nsec; |
951ed4d3 | 416 | unsigned int seq; |
951ed4d3 MS |
417 | |
418 | WARN_ON(timekeeping_suspended); | |
419 | ||
420 | do { | |
3fdb14fd | 421 | seq = read_seqcount_begin(&tk_core.seq); |
d6d29896 | 422 | ts->tv_sec = tk->xtime_sec; |
ec145bab | 423 | nsec = timekeeping_get_ns(tk); |
4e250fdd | 424 | tomono = tk->wall_to_monotonic; |
951ed4d3 | 425 | |
3fdb14fd | 426 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
951ed4d3 | 427 | |
d6d29896 TG |
428 | ts->tv_sec += tomono.tv_sec; |
429 | ts->tv_nsec = 0; | |
430 | timespec64_add_ns(ts, nsec + tomono.tv_nsec); | |
951ed4d3 | 431 | } |
d6d29896 | 432 | EXPORT_SYMBOL_GPL(ktime_get_ts64); |
951ed4d3 | 433 | |
1ff3c967 JS |
434 | |
435 | /** | |
436 | * timekeeping_clocktai - Returns the TAI time of day in a timespec | |
437 | * @ts: pointer to the timespec to be set | |
438 | * | |
439 | * Returns the time of day in a timespec. | |
440 | */ | |
441 | void timekeeping_clocktai(struct timespec *ts) | |
442 | { | |
3fdb14fd | 443 | struct timekeeper *tk = &tk_core.timekeeper; |
7d489d15 | 444 | struct timespec64 ts64; |
1ff3c967 JS |
445 | unsigned long seq; |
446 | u64 nsecs; | |
447 | ||
448 | WARN_ON(timekeeping_suspended); | |
449 | ||
450 | do { | |
3fdb14fd | 451 | seq = read_seqcount_begin(&tk_core.seq); |
1ff3c967 | 452 | |
7d489d15 | 453 | ts64.tv_sec = tk->xtime_sec + tk->tai_offset; |
1ff3c967 JS |
454 | nsecs = timekeeping_get_ns(tk); |
455 | ||
3fdb14fd | 456 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
1ff3c967 | 457 | |
7d489d15 JS |
458 | ts64.tv_nsec = 0; |
459 | timespec64_add_ns(&ts64, nsecs); | |
460 | *ts = timespec64_to_timespec(ts64); | |
1ff3c967 JS |
461 | |
462 | } | |
463 | EXPORT_SYMBOL(timekeeping_clocktai); | |
464 | ||
465 | ||
90adda98 JS |
466 | /** |
467 | * ktime_get_clocktai - Returns the TAI time of day in a ktime | |
468 | * | |
469 | * Returns the time of day in a ktime. | |
470 | */ | |
471 | ktime_t ktime_get_clocktai(void) | |
472 | { | |
473 | struct timespec ts; | |
474 | ||
475 | timekeeping_clocktai(&ts); | |
476 | return timespec_to_ktime(ts); | |
477 | } | |
478 | EXPORT_SYMBOL(ktime_get_clocktai); | |
479 | ||
e2c18e49 AG |
480 | #ifdef CONFIG_NTP_PPS |
481 | ||
482 | /** | |
483 | * getnstime_raw_and_real - get day and raw monotonic time in timespec format | |
484 | * @ts_raw: pointer to the timespec to be set to raw monotonic time | |
485 | * @ts_real: pointer to the timespec to be set to the time of day | |
486 | * | |
487 | * This function reads both the time of day and raw monotonic time at the | |
488 | * same time atomically and stores the resulting timestamps in timespec | |
489 | * format. | |
490 | */ | |
491 | void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real) | |
492 | { | |
3fdb14fd | 493 | struct timekeeper *tk = &tk_core.timekeeper; |
e2c18e49 AG |
494 | unsigned long seq; |
495 | s64 nsecs_raw, nsecs_real; | |
496 | ||
497 | WARN_ON_ONCE(timekeeping_suspended); | |
498 | ||
499 | do { | |
3fdb14fd | 500 | seq = read_seqcount_begin(&tk_core.seq); |
e2c18e49 | 501 | |
7d489d15 | 502 | *ts_raw = timespec64_to_timespec(tk->raw_time); |
4e250fdd | 503 | ts_real->tv_sec = tk->xtime_sec; |
1e75fa8b | 504 | ts_real->tv_nsec = 0; |
e2c18e49 | 505 | |
4e250fdd JS |
506 | nsecs_raw = timekeeping_get_ns_raw(tk); |
507 | nsecs_real = timekeeping_get_ns(tk); | |
e2c18e49 | 508 | |
3fdb14fd | 509 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
e2c18e49 AG |
510 | |
511 | timespec_add_ns(ts_raw, nsecs_raw); | |
512 | timespec_add_ns(ts_real, nsecs_real); | |
513 | } | |
514 | EXPORT_SYMBOL(getnstime_raw_and_real); | |
515 | ||
516 | #endif /* CONFIG_NTP_PPS */ | |
517 | ||
8524070b JS |
518 | /** |
519 | * do_gettimeofday - Returns the time of day in a timeval | |
520 | * @tv: pointer to the timeval to be set | |
521 | * | |
efd9ac86 | 522 | * NOTE: Users should be converted to using getnstimeofday() |
8524070b JS |
523 | */ |
524 | void do_gettimeofday(struct timeval *tv) | |
525 | { | |
d6d29896 | 526 | struct timespec64 now; |
8524070b | 527 | |
d6d29896 | 528 | getnstimeofday64(&now); |
8524070b JS |
529 | tv->tv_sec = now.tv_sec; |
530 | tv->tv_usec = now.tv_nsec/1000; | |
531 | } | |
8524070b | 532 | EXPORT_SYMBOL(do_gettimeofday); |
d239f49d | 533 | |
8524070b JS |
534 | /** |
535 | * do_settimeofday - Sets the time of day | |
536 | * @tv: pointer to the timespec variable containing the new time | |
537 | * | |
538 | * Sets the time of day to the new time and update NTP and notify hrtimers | |
539 | */ | |
1e6d7679 | 540 | int do_settimeofday(const struct timespec *tv) |
8524070b | 541 | { |
3fdb14fd | 542 | struct timekeeper *tk = &tk_core.timekeeper; |
7d489d15 | 543 | struct timespec64 ts_delta, xt, tmp; |
92c1d3ed | 544 | unsigned long flags; |
8524070b | 545 | |
cee58483 | 546 | if (!timespec_valid_strict(tv)) |
8524070b JS |
547 | return -EINVAL; |
548 | ||
9a7a71b1 | 549 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd | 550 | write_seqcount_begin(&tk_core.seq); |
8524070b | 551 | |
4e250fdd | 552 | timekeeping_forward_now(tk); |
9a055117 | 553 | |
4e250fdd | 554 | xt = tk_xtime(tk); |
1e75fa8b JS |
555 | ts_delta.tv_sec = tv->tv_sec - xt.tv_sec; |
556 | ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec; | |
557 | ||
7d489d15 | 558 | tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta)); |
8524070b | 559 | |
7d489d15 JS |
560 | tmp = timespec_to_timespec64(*tv); |
561 | tk_set_xtime(tk, &tmp); | |
1e75fa8b | 562 | |
780427f0 | 563 | timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
8524070b | 564 | |
3fdb14fd | 565 | write_seqcount_end(&tk_core.seq); |
9a7a71b1 | 566 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
8524070b JS |
567 | |
568 | /* signal hrtimers about time change */ | |
569 | clock_was_set(); | |
570 | ||
571 | return 0; | |
572 | } | |
8524070b JS |
573 | EXPORT_SYMBOL(do_settimeofday); |
574 | ||
c528f7c6 JS |
575 | /** |
576 | * timekeeping_inject_offset - Adds or subtracts from the current time. | |
577 | * @tv: pointer to the timespec variable containing the offset | |
578 | * | |
579 | * Adds or subtracts an offset value from the current time. | |
580 | */ | |
581 | int timekeeping_inject_offset(struct timespec *ts) | |
582 | { | |
3fdb14fd | 583 | struct timekeeper *tk = &tk_core.timekeeper; |
92c1d3ed | 584 | unsigned long flags; |
7d489d15 | 585 | struct timespec64 ts64, tmp; |
4e8b1452 | 586 | int ret = 0; |
c528f7c6 JS |
587 | |
588 | if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) | |
589 | return -EINVAL; | |
590 | ||
7d489d15 JS |
591 | ts64 = timespec_to_timespec64(*ts); |
592 | ||
9a7a71b1 | 593 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd | 594 | write_seqcount_begin(&tk_core.seq); |
c528f7c6 | 595 | |
4e250fdd | 596 | timekeeping_forward_now(tk); |
c528f7c6 | 597 | |
4e8b1452 | 598 | /* Make sure the proposed value is valid */ |
7d489d15 JS |
599 | tmp = timespec64_add(tk_xtime(tk), ts64); |
600 | if (!timespec64_valid_strict(&tmp)) { | |
4e8b1452 JS |
601 | ret = -EINVAL; |
602 | goto error; | |
603 | } | |
1e75fa8b | 604 | |
7d489d15 JS |
605 | tk_xtime_add(tk, &ts64); |
606 | tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts64)); | |
c528f7c6 | 607 | |
4e8b1452 | 608 | error: /* even if we error out, we forwarded the time, so call update */ |
780427f0 | 609 | timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
c528f7c6 | 610 | |
3fdb14fd | 611 | write_seqcount_end(&tk_core.seq); |
9a7a71b1 | 612 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
c528f7c6 JS |
613 | |
614 | /* signal hrtimers about time change */ | |
615 | clock_was_set(); | |
616 | ||
4e8b1452 | 617 | return ret; |
c528f7c6 JS |
618 | } |
619 | EXPORT_SYMBOL(timekeeping_inject_offset); | |
620 | ||
cc244dda JS |
621 | |
622 | /** | |
623 | * timekeeping_get_tai_offset - Returns current TAI offset from UTC | |
624 | * | |
625 | */ | |
626 | s32 timekeeping_get_tai_offset(void) | |
627 | { | |
3fdb14fd | 628 | struct timekeeper *tk = &tk_core.timekeeper; |
cc244dda JS |
629 | unsigned int seq; |
630 | s32 ret; | |
631 | ||
632 | do { | |
3fdb14fd | 633 | seq = read_seqcount_begin(&tk_core.seq); |
cc244dda | 634 | ret = tk->tai_offset; |
3fdb14fd | 635 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
cc244dda JS |
636 | |
637 | return ret; | |
638 | } | |
639 | ||
640 | /** | |
641 | * __timekeeping_set_tai_offset - Lock free worker function | |
642 | * | |
643 | */ | |
dd5d70e8 | 644 | static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset) |
cc244dda JS |
645 | { |
646 | tk->tai_offset = tai_offset; | |
04005f60 | 647 | tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0)); |
cc244dda JS |
648 | } |
649 | ||
650 | /** | |
651 | * timekeeping_set_tai_offset - Sets the current TAI offset from UTC | |
652 | * | |
653 | */ | |
654 | void timekeeping_set_tai_offset(s32 tai_offset) | |
655 | { | |
3fdb14fd | 656 | struct timekeeper *tk = &tk_core.timekeeper; |
cc244dda JS |
657 | unsigned long flags; |
658 | ||
9a7a71b1 | 659 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd | 660 | write_seqcount_begin(&tk_core.seq); |
cc244dda | 661 | __timekeeping_set_tai_offset(tk, tai_offset); |
f55c0760 | 662 | timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); |
3fdb14fd | 663 | write_seqcount_end(&tk_core.seq); |
9a7a71b1 | 664 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
4e8f8b34 | 665 | clock_was_set(); |
cc244dda JS |
666 | } |
667 | ||
8524070b JS |
668 | /** |
669 | * change_clocksource - Swaps clocksources if a new one is available | |
670 | * | |
671 | * Accumulates current time interval and initializes new clocksource | |
672 | */ | |
75c5158f | 673 | static int change_clocksource(void *data) |
8524070b | 674 | { |
3fdb14fd | 675 | struct timekeeper *tk = &tk_core.timekeeper; |
4614e6ad | 676 | struct clocksource *new, *old; |
f695cf94 | 677 | unsigned long flags; |
8524070b | 678 | |
75c5158f | 679 | new = (struct clocksource *) data; |
8524070b | 680 | |
9a7a71b1 | 681 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd | 682 | write_seqcount_begin(&tk_core.seq); |
f695cf94 | 683 | |
4e250fdd | 684 | timekeeping_forward_now(tk); |
09ac369c TG |
685 | /* |
686 | * If the cs is in module, get a module reference. Succeeds | |
687 | * for built-in code (owner == NULL) as well. | |
688 | */ | |
689 | if (try_module_get(new->owner)) { | |
690 | if (!new->enable || new->enable(new) == 0) { | |
691 | old = tk->clock; | |
692 | tk_setup_internals(tk, new); | |
693 | if (old->disable) | |
694 | old->disable(old); | |
695 | module_put(old->owner); | |
696 | } else { | |
697 | module_put(new->owner); | |
698 | } | |
75c5158f | 699 | } |
780427f0 | 700 | timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
f695cf94 | 701 | |
3fdb14fd | 702 | write_seqcount_end(&tk_core.seq); |
9a7a71b1 | 703 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
f695cf94 | 704 | |
75c5158f MS |
705 | return 0; |
706 | } | |
8524070b | 707 | |
75c5158f MS |
708 | /** |
709 | * timekeeping_notify - Install a new clock source | |
710 | * @clock: pointer to the clock source | |
711 | * | |
712 | * This function is called from clocksource.c after a new, better clock | |
713 | * source has been registered. The caller holds the clocksource_mutex. | |
714 | */ | |
ba919d1c | 715 | int timekeeping_notify(struct clocksource *clock) |
75c5158f | 716 | { |
3fdb14fd | 717 | struct timekeeper *tk = &tk_core.timekeeper; |
4e250fdd JS |
718 | |
719 | if (tk->clock == clock) | |
ba919d1c | 720 | return 0; |
75c5158f | 721 | stop_machine(change_clocksource, clock, NULL); |
8524070b | 722 | tick_clock_notify(); |
ba919d1c | 723 | return tk->clock == clock ? 0 : -1; |
8524070b | 724 | } |
75c5158f | 725 | |
a40f262c TG |
726 | /** |
727 | * ktime_get_real - get the real (wall-) time in ktime_t format | |
728 | * | |
729 | * returns the time in ktime_t format | |
730 | */ | |
731 | ktime_t ktime_get_real(void) | |
732 | { | |
d6d29896 | 733 | struct timespec64 now; |
a40f262c | 734 | |
d6d29896 | 735 | getnstimeofday64(&now); |
a40f262c | 736 | |
d6d29896 | 737 | return timespec64_to_ktime(now); |
a40f262c TG |
738 | } |
739 | EXPORT_SYMBOL_GPL(ktime_get_real); | |
8524070b | 740 | |
2d42244a JS |
741 | /** |
742 | * getrawmonotonic - Returns the raw monotonic time in a timespec | |
743 | * @ts: pointer to the timespec to be set | |
744 | * | |
745 | * Returns the raw monotonic time (completely un-modified by ntp) | |
746 | */ | |
747 | void getrawmonotonic(struct timespec *ts) | |
748 | { | |
3fdb14fd | 749 | struct timekeeper *tk = &tk_core.timekeeper; |
7d489d15 | 750 | struct timespec64 ts64; |
2d42244a JS |
751 | unsigned long seq; |
752 | s64 nsecs; | |
2d42244a JS |
753 | |
754 | do { | |
3fdb14fd | 755 | seq = read_seqcount_begin(&tk_core.seq); |
4e250fdd | 756 | nsecs = timekeeping_get_ns_raw(tk); |
7d489d15 | 757 | ts64 = tk->raw_time; |
2d42244a | 758 | |
3fdb14fd | 759 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
2d42244a | 760 | |
7d489d15 JS |
761 | timespec64_add_ns(&ts64, nsecs); |
762 | *ts = timespec64_to_timespec(ts64); | |
2d42244a JS |
763 | } |
764 | EXPORT_SYMBOL(getrawmonotonic); | |
765 | ||
8524070b | 766 | /** |
cf4fc6cb | 767 | * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres |
8524070b | 768 | */ |
cf4fc6cb | 769 | int timekeeping_valid_for_hres(void) |
8524070b | 770 | { |
3fdb14fd | 771 | struct timekeeper *tk = &tk_core.timekeeper; |
8524070b JS |
772 | unsigned long seq; |
773 | int ret; | |
774 | ||
775 | do { | |
3fdb14fd | 776 | seq = read_seqcount_begin(&tk_core.seq); |
8524070b | 777 | |
4e250fdd | 778 | ret = tk->clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; |
8524070b | 779 | |
3fdb14fd | 780 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
8524070b JS |
781 | |
782 | return ret; | |
783 | } | |
784 | ||
98962465 JH |
785 | /** |
786 | * timekeeping_max_deferment - Returns max time the clocksource can be deferred | |
98962465 JH |
787 | */ |
788 | u64 timekeeping_max_deferment(void) | |
789 | { | |
3fdb14fd | 790 | struct timekeeper *tk = &tk_core.timekeeper; |
70471f2f JS |
791 | unsigned long seq; |
792 | u64 ret; | |
42e71e81 | 793 | |
70471f2f | 794 | do { |
3fdb14fd | 795 | seq = read_seqcount_begin(&tk_core.seq); |
70471f2f | 796 | |
4e250fdd | 797 | ret = tk->clock->max_idle_ns; |
70471f2f | 798 | |
3fdb14fd | 799 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
70471f2f JS |
800 | |
801 | return ret; | |
98962465 JH |
802 | } |
803 | ||
8524070b | 804 | /** |
d4f587c6 | 805 | * read_persistent_clock - Return time from the persistent clock. |
8524070b JS |
806 | * |
807 | * Weak dummy function for arches that do not yet support it. | |
d4f587c6 MS |
808 | * Reads the time from the battery backed persistent clock. |
809 | * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported. | |
8524070b JS |
810 | * |
811 | * XXX - Do be sure to remove it once all arches implement it. | |
812 | */ | |
52f5684c | 813 | void __weak read_persistent_clock(struct timespec *ts) |
8524070b | 814 | { |
d4f587c6 MS |
815 | ts->tv_sec = 0; |
816 | ts->tv_nsec = 0; | |
8524070b JS |
817 | } |
818 | ||
23970e38 MS |
819 | /** |
820 | * read_boot_clock - Return time of the system start. | |
821 | * | |
822 | * Weak dummy function for arches that do not yet support it. | |
823 | * Function to read the exact time the system has been started. | |
824 | * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported. | |
825 | * | |
826 | * XXX - Do be sure to remove it once all arches implement it. | |
827 | */ | |
52f5684c | 828 | void __weak read_boot_clock(struct timespec *ts) |
23970e38 MS |
829 | { |
830 | ts->tv_sec = 0; | |
831 | ts->tv_nsec = 0; | |
832 | } | |
833 | ||
8524070b JS |
834 | /* |
835 | * timekeeping_init - Initializes the clocksource and common timekeeping values | |
836 | */ | |
837 | void __init timekeeping_init(void) | |
838 | { | |
3fdb14fd | 839 | struct timekeeper *tk = &tk_core.timekeeper; |
155ec602 | 840 | struct clocksource *clock; |
8524070b | 841 | unsigned long flags; |
7d489d15 JS |
842 | struct timespec64 now, boot, tmp; |
843 | struct timespec ts; | |
31ade306 | 844 | |
7d489d15 JS |
845 | read_persistent_clock(&ts); |
846 | now = timespec_to_timespec64(ts); | |
847 | if (!timespec64_valid_strict(&now)) { | |
4e8b1452 JS |
848 | pr_warn("WARNING: Persistent clock returned invalid value!\n" |
849 | " Check your CMOS/BIOS settings.\n"); | |
850 | now.tv_sec = 0; | |
851 | now.tv_nsec = 0; | |
31ade306 FT |
852 | } else if (now.tv_sec || now.tv_nsec) |
853 | persistent_clock_exist = true; | |
4e8b1452 | 854 | |
7d489d15 JS |
855 | read_boot_clock(&ts); |
856 | boot = timespec_to_timespec64(ts); | |
857 | if (!timespec64_valid_strict(&boot)) { | |
4e8b1452 JS |
858 | pr_warn("WARNING: Boot clock returned invalid value!\n" |
859 | " Check your CMOS/BIOS settings.\n"); | |
860 | boot.tv_sec = 0; | |
861 | boot.tv_nsec = 0; | |
862 | } | |
8524070b | 863 | |
9a7a71b1 | 864 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd | 865 | write_seqcount_begin(&tk_core.seq); |
06c017fd JS |
866 | ntp_init(); |
867 | ||
f1b82746 | 868 | clock = clocksource_default_clock(); |
a0f7d48b MS |
869 | if (clock->enable) |
870 | clock->enable(clock); | |
4e250fdd | 871 | tk_setup_internals(tk, clock); |
8524070b | 872 | |
4e250fdd JS |
873 | tk_set_xtime(tk, &now); |
874 | tk->raw_time.tv_sec = 0; | |
875 | tk->raw_time.tv_nsec = 0; | |
1e75fa8b | 876 | if (boot.tv_sec == 0 && boot.tv_nsec == 0) |
4e250fdd | 877 | boot = tk_xtime(tk); |
1e75fa8b | 878 | |
7d489d15 | 879 | set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec); |
4e250fdd | 880 | tk_set_wall_to_mono(tk, tmp); |
6d0ef903 JS |
881 | |
882 | tmp.tv_sec = 0; | |
883 | tmp.tv_nsec = 0; | |
4e250fdd | 884 | tk_set_sleep_time(tk, tmp); |
6d0ef903 | 885 | |
f111adfd | 886 | timekeeping_update(tk, TK_MIRROR); |
48cdc135 | 887 | |
3fdb14fd | 888 | write_seqcount_end(&tk_core.seq); |
9a7a71b1 | 889 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
8524070b JS |
890 | } |
891 | ||
8524070b | 892 | /* time in seconds when suspend began */ |
7d489d15 | 893 | static struct timespec64 timekeeping_suspend_time; |
8524070b | 894 | |
304529b1 JS |
895 | /** |
896 | * __timekeeping_inject_sleeptime - Internal function to add sleep interval | |
897 | * @delta: pointer to a timespec delta value | |
898 | * | |
899 | * Takes a timespec offset measuring a suspend interval and properly | |
900 | * adds the sleep offset to the timekeeping variables. | |
901 | */ | |
f726a697 | 902 | static void __timekeeping_inject_sleeptime(struct timekeeper *tk, |
7d489d15 | 903 | struct timespec64 *delta) |
304529b1 | 904 | { |
7d489d15 | 905 | if (!timespec64_valid_strict(delta)) { |
6d9bcb62 JS |
906 | printk_deferred(KERN_WARNING |
907 | "__timekeeping_inject_sleeptime: Invalid " | |
908 | "sleep delta value!\n"); | |
cb5de2f8 JS |
909 | return; |
910 | } | |
f726a697 | 911 | tk_xtime_add(tk, delta); |
7d489d15 JS |
912 | tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta)); |
913 | tk_set_sleep_time(tk, timespec64_add(tk->total_sleep_time, *delta)); | |
5c83545f | 914 | tk_debug_account_sleep_time(delta); |
304529b1 JS |
915 | } |
916 | ||
304529b1 JS |
917 | /** |
918 | * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values | |
919 | * @delta: pointer to a timespec delta value | |
920 | * | |
921 | * This hook is for architectures that cannot support read_persistent_clock | |
922 | * because their RTC/persistent clock is only accessible when irqs are enabled. | |
923 | * | |
924 | * This function should only be called by rtc_resume(), and allows | |
925 | * a suspend offset to be injected into the timekeeping values. | |
926 | */ | |
927 | void timekeeping_inject_sleeptime(struct timespec *delta) | |
928 | { | |
3fdb14fd | 929 | struct timekeeper *tk = &tk_core.timekeeper; |
7d489d15 | 930 | struct timespec64 tmp; |
92c1d3ed | 931 | unsigned long flags; |
304529b1 | 932 | |
31ade306 FT |
933 | /* |
934 | * Make sure we don't set the clock twice, as timekeeping_resume() | |
935 | * already did it | |
936 | */ | |
937 | if (has_persistent_clock()) | |
304529b1 JS |
938 | return; |
939 | ||
9a7a71b1 | 940 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd | 941 | write_seqcount_begin(&tk_core.seq); |
70471f2f | 942 | |
4e250fdd | 943 | timekeeping_forward_now(tk); |
304529b1 | 944 | |
7d489d15 JS |
945 | tmp = timespec_to_timespec64(*delta); |
946 | __timekeeping_inject_sleeptime(tk, &tmp); | |
304529b1 | 947 | |
780427f0 | 948 | timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
304529b1 | 949 | |
3fdb14fd | 950 | write_seqcount_end(&tk_core.seq); |
9a7a71b1 | 951 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
304529b1 JS |
952 | |
953 | /* signal hrtimers about time change */ | |
954 | clock_was_set(); | |
955 | } | |
956 | ||
8524070b JS |
957 | /** |
958 | * timekeeping_resume - Resumes the generic timekeeping subsystem. | |
8524070b JS |
959 | * |
960 | * This is for the generic clocksource timekeeping. | |
961 | * xtime/wall_to_monotonic/jiffies/etc are | |
962 | * still managed by arch specific suspend/resume code. | |
963 | */ | |
e1a85b2c | 964 | static void timekeeping_resume(void) |
8524070b | 965 | { |
3fdb14fd | 966 | struct timekeeper *tk = &tk_core.timekeeper; |
e445cf1c | 967 | struct clocksource *clock = tk->clock; |
92c1d3ed | 968 | unsigned long flags; |
7d489d15 JS |
969 | struct timespec64 ts_new, ts_delta; |
970 | struct timespec tmp; | |
e445cf1c FT |
971 | cycle_t cycle_now, cycle_delta; |
972 | bool suspendtime_found = false; | |
d4f587c6 | 973 | |
7d489d15 JS |
974 | read_persistent_clock(&tmp); |
975 | ts_new = timespec_to_timespec64(tmp); | |
8524070b | 976 | |
adc78e6b | 977 | clockevents_resume(); |
d10ff3fb TG |
978 | clocksource_resume(); |
979 | ||
9a7a71b1 | 980 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd | 981 | write_seqcount_begin(&tk_core.seq); |
8524070b | 982 | |
e445cf1c FT |
983 | /* |
984 | * After system resumes, we need to calculate the suspended time and | |
985 | * compensate it for the OS time. There are 3 sources that could be | |
986 | * used: Nonstop clocksource during suspend, persistent clock and rtc | |
987 | * device. | |
988 | * | |
989 | * One specific platform may have 1 or 2 or all of them, and the | |
990 | * preference will be: | |
991 | * suspend-nonstop clocksource -> persistent clock -> rtc | |
992 | * The less preferred source will only be tried if there is no better | |
993 | * usable source. The rtc part is handled separately in rtc core code. | |
994 | */ | |
995 | cycle_now = clock->read(clock); | |
996 | if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) && | |
997 | cycle_now > clock->cycle_last) { | |
998 | u64 num, max = ULLONG_MAX; | |
999 | u32 mult = clock->mult; | |
1000 | u32 shift = clock->shift; | |
1001 | s64 nsec = 0; | |
1002 | ||
1003 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; | |
1004 | ||
1005 | /* | |
1006 | * "cycle_delta * mutl" may cause 64 bits overflow, if the | |
1007 | * suspended time is too long. In that case we need do the | |
1008 | * 64 bits math carefully | |
1009 | */ | |
1010 | do_div(max, mult); | |
1011 | if (cycle_delta > max) { | |
1012 | num = div64_u64(cycle_delta, max); | |
1013 | nsec = (((u64) max * mult) >> shift) * num; | |
1014 | cycle_delta -= num * max; | |
1015 | } | |
1016 | nsec += ((u64) cycle_delta * mult) >> shift; | |
1017 | ||
7d489d15 | 1018 | ts_delta = ns_to_timespec64(nsec); |
e445cf1c | 1019 | suspendtime_found = true; |
7d489d15 JS |
1020 | } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) { |
1021 | ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time); | |
e445cf1c | 1022 | suspendtime_found = true; |
8524070b | 1023 | } |
e445cf1c FT |
1024 | |
1025 | if (suspendtime_found) | |
1026 | __timekeeping_inject_sleeptime(tk, &ts_delta); | |
1027 | ||
1028 | /* Re-base the last cycle value */ | |
77c675ba | 1029 | tk->cycle_last = clock->cycle_last = cycle_now; |
4e250fdd | 1030 | tk->ntp_error = 0; |
8524070b | 1031 | timekeeping_suspended = 0; |
780427f0 | 1032 | timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); |
3fdb14fd | 1033 | write_seqcount_end(&tk_core.seq); |
9a7a71b1 | 1034 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
8524070b JS |
1035 | |
1036 | touch_softlockup_watchdog(); | |
1037 | ||
1038 | clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL); | |
1039 | ||
1040 | /* Resume hrtimers */ | |
b12a03ce | 1041 | hrtimers_resume(); |
8524070b JS |
1042 | } |
1043 | ||
e1a85b2c | 1044 | static int timekeeping_suspend(void) |
8524070b | 1045 | { |
3fdb14fd | 1046 | struct timekeeper *tk = &tk_core.timekeeper; |
92c1d3ed | 1047 | unsigned long flags; |
7d489d15 JS |
1048 | struct timespec64 delta, delta_delta; |
1049 | static struct timespec64 old_delta; | |
1050 | struct timespec tmp; | |
8524070b | 1051 | |
7d489d15 JS |
1052 | read_persistent_clock(&tmp); |
1053 | timekeeping_suspend_time = timespec_to_timespec64(tmp); | |
3be90950 | 1054 | |
0d6bd995 ZM |
1055 | /* |
1056 | * On some systems the persistent_clock can not be detected at | |
1057 | * timekeeping_init by its return value, so if we see a valid | |
1058 | * value returned, update the persistent_clock_exists flag. | |
1059 | */ | |
1060 | if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec) | |
1061 | persistent_clock_exist = true; | |
1062 | ||
9a7a71b1 | 1063 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd | 1064 | write_seqcount_begin(&tk_core.seq); |
4e250fdd | 1065 | timekeeping_forward_now(tk); |
8524070b | 1066 | timekeeping_suspended = 1; |
cb33217b JS |
1067 | |
1068 | /* | |
1069 | * To avoid drift caused by repeated suspend/resumes, | |
1070 | * which each can add ~1 second drift error, | |
1071 | * try to compensate so the difference in system time | |
1072 | * and persistent_clock time stays close to constant. | |
1073 | */ | |
7d489d15 JS |
1074 | delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time); |
1075 | delta_delta = timespec64_sub(delta, old_delta); | |
cb33217b JS |
1076 | if (abs(delta_delta.tv_sec) >= 2) { |
1077 | /* | |
1078 | * if delta_delta is too large, assume time correction | |
1079 | * has occured and set old_delta to the current delta. | |
1080 | */ | |
1081 | old_delta = delta; | |
1082 | } else { | |
1083 | /* Otherwise try to adjust old_system to compensate */ | |
1084 | timekeeping_suspend_time = | |
7d489d15 | 1085 | timespec64_add(timekeeping_suspend_time, delta_delta); |
cb33217b | 1086 | } |
330a1617 JS |
1087 | |
1088 | timekeeping_update(tk, TK_MIRROR); | |
3fdb14fd | 1089 | write_seqcount_end(&tk_core.seq); |
9a7a71b1 | 1090 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
8524070b JS |
1091 | |
1092 | clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL); | |
c54a42b1 | 1093 | clocksource_suspend(); |
adc78e6b | 1094 | clockevents_suspend(); |
8524070b JS |
1095 | |
1096 | return 0; | |
1097 | } | |
1098 | ||
1099 | /* sysfs resume/suspend bits for timekeeping */ | |
e1a85b2c | 1100 | static struct syscore_ops timekeeping_syscore_ops = { |
8524070b JS |
1101 | .resume = timekeeping_resume, |
1102 | .suspend = timekeeping_suspend, | |
8524070b JS |
1103 | }; |
1104 | ||
e1a85b2c | 1105 | static int __init timekeeping_init_ops(void) |
8524070b | 1106 | { |
e1a85b2c RW |
1107 | register_syscore_ops(&timekeeping_syscore_ops); |
1108 | return 0; | |
8524070b JS |
1109 | } |
1110 | ||
e1a85b2c | 1111 | device_initcall(timekeeping_init_ops); |
8524070b JS |
1112 | |
1113 | /* | |
1114 | * If the error is already larger, we look ahead even further | |
1115 | * to compensate for late or lost adjustments. | |
1116 | */ | |
f726a697 JS |
1117 | static __always_inline int timekeeping_bigadjust(struct timekeeper *tk, |
1118 | s64 error, s64 *interval, | |
8524070b JS |
1119 | s64 *offset) |
1120 | { | |
1121 | s64 tick_error, i; | |
1122 | u32 look_ahead, adj; | |
1123 | s32 error2, mult; | |
1124 | ||
1125 | /* | |
1126 | * Use the current error value to determine how much to look ahead. | |
1127 | * The larger the error the slower we adjust for it to avoid problems | |
1128 | * with losing too many ticks, otherwise we would overadjust and | |
1129 | * produce an even larger error. The smaller the adjustment the | |
1130 | * faster we try to adjust for it, as lost ticks can do less harm | |
3eb05676 | 1131 | * here. This is tuned so that an error of about 1 msec is adjusted |
8524070b JS |
1132 | * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks). |
1133 | */ | |
f726a697 | 1134 | error2 = tk->ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ); |
8524070b JS |
1135 | error2 = abs(error2); |
1136 | for (look_ahead = 0; error2 > 0; look_ahead++) | |
1137 | error2 >>= 2; | |
1138 | ||
1139 | /* | |
1140 | * Now calculate the error in (1 << look_ahead) ticks, but first | |
1141 | * remove the single look ahead already included in the error. | |
1142 | */ | |
f726a697 JS |
1143 | tick_error = ntp_tick_length() >> (tk->ntp_error_shift + 1); |
1144 | tick_error -= tk->xtime_interval >> 1; | |
8524070b JS |
1145 | error = ((error - tick_error) >> look_ahead) + tick_error; |
1146 | ||
1147 | /* Finally calculate the adjustment shift value. */ | |
1148 | i = *interval; | |
1149 | mult = 1; | |
1150 | if (error < 0) { | |
1151 | error = -error; | |
1152 | *interval = -*interval; | |
1153 | *offset = -*offset; | |
1154 | mult = -1; | |
1155 | } | |
1156 | for (adj = 0; error > i; adj++) | |
1157 | error >>= 1; | |
1158 | ||
1159 | *interval <<= adj; | |
1160 | *offset <<= adj; | |
1161 | return mult << adj; | |
1162 | } | |
1163 | ||
1164 | /* | |
1165 | * Adjust the multiplier to reduce the error value, | |
1166 | * this is optimized for the most common adjustments of -1,0,1, | |
1167 | * for other values we can do a bit more work. | |
1168 | */ | |
f726a697 | 1169 | static void timekeeping_adjust(struct timekeeper *tk, s64 offset) |
8524070b | 1170 | { |
f726a697 | 1171 | s64 error, interval = tk->cycle_interval; |
8524070b JS |
1172 | int adj; |
1173 | ||
c2bc1111 | 1174 | /* |
88b28adf | 1175 | * The point of this is to check if the error is greater than half |
c2bc1111 JS |
1176 | * an interval. |
1177 | * | |
1178 | * First we shift it down from NTP_SHIFT to clocksource->shifted nsecs. | |
1179 | * | |
1180 | * Note we subtract one in the shift, so that error is really error*2. | |
3f86f28f JS |
1181 | * This "saves" dividing(shifting) interval twice, but keeps the |
1182 | * (error > interval) comparison as still measuring if error is | |
88b28adf | 1183 | * larger than half an interval. |
c2bc1111 | 1184 | * |
3f86f28f | 1185 | * Note: It does not "save" on aggravation when reading the code. |
c2bc1111 | 1186 | */ |
f726a697 | 1187 | error = tk->ntp_error >> (tk->ntp_error_shift - 1); |
8524070b | 1188 | if (error > interval) { |
c2bc1111 JS |
1189 | /* |
1190 | * We now divide error by 4(via shift), which checks if | |
88b28adf | 1191 | * the error is greater than twice the interval. |
c2bc1111 JS |
1192 | * If it is greater, we need a bigadjust, if its smaller, |
1193 | * we can adjust by 1. | |
1194 | */ | |
8524070b JS |
1195 | error >>= 2; |
1196 | if (likely(error <= interval)) | |
1197 | adj = 1; | |
1198 | else | |
1d17d174 IM |
1199 | adj = timekeeping_bigadjust(tk, error, &interval, &offset); |
1200 | } else { | |
1201 | if (error < -interval) { | |
1202 | /* See comment above, this is just switched for the negative */ | |
1203 | error >>= 2; | |
1204 | if (likely(error >= -interval)) { | |
1205 | adj = -1; | |
1206 | interval = -interval; | |
1207 | offset = -offset; | |
1208 | } else { | |
1209 | adj = timekeeping_bigadjust(tk, error, &interval, &offset); | |
1210 | } | |
1211 | } else { | |
1212 | goto out_adjust; | |
1213 | } | |
1214 | } | |
8524070b | 1215 | |
f726a697 JS |
1216 | if (unlikely(tk->clock->maxadj && |
1217 | (tk->mult + adj > tk->clock->mult + tk->clock->maxadj))) { | |
6d9bcb62 | 1218 | printk_deferred_once(KERN_WARNING |
e919cfd4 | 1219 | "Adjusting %s more than 11%% (%ld vs %ld)\n", |
f726a697 JS |
1220 | tk->clock->name, (long)tk->mult + adj, |
1221 | (long)tk->clock->mult + tk->clock->maxadj); | |
e919cfd4 | 1222 | } |
c2bc1111 JS |
1223 | /* |
1224 | * So the following can be confusing. | |
1225 | * | |
1226 | * To keep things simple, lets assume adj == 1 for now. | |
1227 | * | |
1228 | * When adj != 1, remember that the interval and offset values | |
1229 | * have been appropriately scaled so the math is the same. | |
1230 | * | |
1231 | * The basic idea here is that we're increasing the multiplier | |
1232 | * by one, this causes the xtime_interval to be incremented by | |
1233 | * one cycle_interval. This is because: | |
1234 | * xtime_interval = cycle_interval * mult | |
1235 | * So if mult is being incremented by one: | |
1236 | * xtime_interval = cycle_interval * (mult + 1) | |
1237 | * Its the same as: | |
1238 | * xtime_interval = (cycle_interval * mult) + cycle_interval | |
1239 | * Which can be shortened to: | |
1240 | * xtime_interval += cycle_interval | |
1241 | * | |
1242 | * So offset stores the non-accumulated cycles. Thus the current | |
1243 | * time (in shifted nanoseconds) is: | |
1244 | * now = (offset * adj) + xtime_nsec | |
1245 | * Now, even though we're adjusting the clock frequency, we have | |
1246 | * to keep time consistent. In other words, we can't jump back | |
1247 | * in time, and we also want to avoid jumping forward in time. | |
1248 | * | |
1249 | * So given the same offset value, we need the time to be the same | |
1250 | * both before and after the freq adjustment. | |
1251 | * now = (offset * adj_1) + xtime_nsec_1 | |
1252 | * now = (offset * adj_2) + xtime_nsec_2 | |
1253 | * So: | |
1254 | * (offset * adj_1) + xtime_nsec_1 = | |
1255 | * (offset * adj_2) + xtime_nsec_2 | |
1256 | * And we know: | |
1257 | * adj_2 = adj_1 + 1 | |
1258 | * So: | |
1259 | * (offset * adj_1) + xtime_nsec_1 = | |
1260 | * (offset * (adj_1+1)) + xtime_nsec_2 | |
1261 | * (offset * adj_1) + xtime_nsec_1 = | |
1262 | * (offset * adj_1) + offset + xtime_nsec_2 | |
1263 | * Canceling the sides: | |
1264 | * xtime_nsec_1 = offset + xtime_nsec_2 | |
1265 | * Which gives us: | |
1266 | * xtime_nsec_2 = xtime_nsec_1 - offset | |
1267 | * Which simplfies to: | |
1268 | * xtime_nsec -= offset | |
1269 | * | |
1270 | * XXX - TODO: Doc ntp_error calculation. | |
1271 | */ | |
f726a697 JS |
1272 | tk->mult += adj; |
1273 | tk->xtime_interval += interval; | |
1274 | tk->xtime_nsec -= offset; | |
1275 | tk->ntp_error -= (interval - offset) << tk->ntp_error_shift; | |
2a8c0883 | 1276 | |
1d17d174 | 1277 | out_adjust: |
2a8c0883 JS |
1278 | /* |
1279 | * It may be possible that when we entered this function, xtime_nsec | |
1280 | * was very small. Further, if we're slightly speeding the clocksource | |
1281 | * in the code above, its possible the required corrective factor to | |
1282 | * xtime_nsec could cause it to underflow. | |
1283 | * | |
1284 | * Now, since we already accumulated the second, cannot simply roll | |
1285 | * the accumulated second back, since the NTP subsystem has been | |
1286 | * notified via second_overflow. So instead we push xtime_nsec forward | |
1287 | * by the amount we underflowed, and add that amount into the error. | |
1288 | * | |
1289 | * We'll correct this error next time through this function, when | |
1290 | * xtime_nsec is not as small. | |
1291 | */ | |
f726a697 JS |
1292 | if (unlikely((s64)tk->xtime_nsec < 0)) { |
1293 | s64 neg = -(s64)tk->xtime_nsec; | |
1294 | tk->xtime_nsec = 0; | |
1295 | tk->ntp_error += neg << tk->ntp_error_shift; | |
2a8c0883 JS |
1296 | } |
1297 | ||
8524070b JS |
1298 | } |
1299 | ||
1f4f9487 JS |
1300 | /** |
1301 | * accumulate_nsecs_to_secs - Accumulates nsecs into secs | |
1302 | * | |
1303 | * Helper function that accumulates a the nsecs greater then a second | |
1304 | * from the xtime_nsec field to the xtime_secs field. | |
1305 | * It also calls into the NTP code to handle leapsecond processing. | |
1306 | * | |
1307 | */ | |
780427f0 | 1308 | static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk) |
1f4f9487 JS |
1309 | { |
1310 | u64 nsecps = (u64)NSEC_PER_SEC << tk->shift; | |
5258d3f2 | 1311 | unsigned int clock_set = 0; |
1f4f9487 JS |
1312 | |
1313 | while (tk->xtime_nsec >= nsecps) { | |
1314 | int leap; | |
1315 | ||
1316 | tk->xtime_nsec -= nsecps; | |
1317 | tk->xtime_sec++; | |
1318 | ||
1319 | /* Figure out if its a leap sec and apply if needed */ | |
1320 | leap = second_overflow(tk->xtime_sec); | |
6d0ef903 | 1321 | if (unlikely(leap)) { |
7d489d15 | 1322 | struct timespec64 ts; |
6d0ef903 JS |
1323 | |
1324 | tk->xtime_sec += leap; | |
1f4f9487 | 1325 | |
6d0ef903 JS |
1326 | ts.tv_sec = leap; |
1327 | ts.tv_nsec = 0; | |
1328 | tk_set_wall_to_mono(tk, | |
7d489d15 | 1329 | timespec64_sub(tk->wall_to_monotonic, ts)); |
6d0ef903 | 1330 | |
cc244dda JS |
1331 | __timekeeping_set_tai_offset(tk, tk->tai_offset - leap); |
1332 | ||
5258d3f2 | 1333 | clock_set = TK_CLOCK_WAS_SET; |
6d0ef903 | 1334 | } |
1f4f9487 | 1335 | } |
5258d3f2 | 1336 | return clock_set; |
1f4f9487 JS |
1337 | } |
1338 | ||
a092ff0f JS |
1339 | /** |
1340 | * logarithmic_accumulation - shifted accumulation of cycles | |
1341 | * | |
1342 | * This functions accumulates a shifted interval of cycles into | |
1343 | * into a shifted interval nanoseconds. Allows for O(log) accumulation | |
1344 | * loop. | |
1345 | * | |
1346 | * Returns the unconsumed cycles. | |
1347 | */ | |
f726a697 | 1348 | static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset, |
5258d3f2 JS |
1349 | u32 shift, |
1350 | unsigned int *clock_set) | |
a092ff0f | 1351 | { |
23a9537a | 1352 | cycle_t interval = tk->cycle_interval << shift; |
deda2e81 | 1353 | u64 raw_nsecs; |
a092ff0f | 1354 | |
f726a697 | 1355 | /* If the offset is smaller then a shifted interval, do nothing */ |
23a9537a | 1356 | if (offset < interval) |
a092ff0f JS |
1357 | return offset; |
1358 | ||
1359 | /* Accumulate one shifted interval */ | |
23a9537a | 1360 | offset -= interval; |
7ec98e15 | 1361 | tk->cycle_last += interval; |
a092ff0f | 1362 | |
f726a697 | 1363 | tk->xtime_nsec += tk->xtime_interval << shift; |
5258d3f2 | 1364 | *clock_set |= accumulate_nsecs_to_secs(tk); |
a092ff0f | 1365 | |
deda2e81 | 1366 | /* Accumulate raw time */ |
5b3900cd | 1367 | raw_nsecs = (u64)tk->raw_interval << shift; |
f726a697 | 1368 | raw_nsecs += tk->raw_time.tv_nsec; |
c7dcf87a JS |
1369 | if (raw_nsecs >= NSEC_PER_SEC) { |
1370 | u64 raw_secs = raw_nsecs; | |
1371 | raw_nsecs = do_div(raw_secs, NSEC_PER_SEC); | |
f726a697 | 1372 | tk->raw_time.tv_sec += raw_secs; |
a092ff0f | 1373 | } |
f726a697 | 1374 | tk->raw_time.tv_nsec = raw_nsecs; |
a092ff0f JS |
1375 | |
1376 | /* Accumulate error between NTP and clock interval */ | |
f726a697 JS |
1377 | tk->ntp_error += ntp_tick_length() << shift; |
1378 | tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) << | |
1379 | (tk->ntp_error_shift + shift); | |
a092ff0f JS |
1380 | |
1381 | return offset; | |
1382 | } | |
1383 | ||
8524070b JS |
1384 | /** |
1385 | * update_wall_time - Uses the current clocksource to increment the wall time | |
1386 | * | |
8524070b | 1387 | */ |
47a1b796 | 1388 | void update_wall_time(void) |
8524070b | 1389 | { |
155ec602 | 1390 | struct clocksource *clock; |
3fdb14fd | 1391 | struct timekeeper *real_tk = &tk_core.timekeeper; |
48cdc135 | 1392 | struct timekeeper *tk = &shadow_timekeeper; |
8524070b | 1393 | cycle_t offset; |
a092ff0f | 1394 | int shift = 0, maxshift; |
5258d3f2 | 1395 | unsigned int clock_set = 0; |
70471f2f JS |
1396 | unsigned long flags; |
1397 | ||
9a7a71b1 | 1398 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
8524070b JS |
1399 | |
1400 | /* Make sure we're fully resumed: */ | |
1401 | if (unlikely(timekeeping_suspended)) | |
70471f2f | 1402 | goto out; |
8524070b | 1403 | |
48cdc135 | 1404 | clock = real_tk->clock; |
592913ec JS |
1405 | |
1406 | #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET | |
48cdc135 | 1407 | offset = real_tk->cycle_interval; |
592913ec JS |
1408 | #else |
1409 | offset = (clock->read(clock) - clock->cycle_last) & clock->mask; | |
8524070b | 1410 | #endif |
8524070b | 1411 | |
bf2ac312 | 1412 | /* Check if there's really nothing to do */ |
48cdc135 | 1413 | if (offset < real_tk->cycle_interval) |
bf2ac312 JS |
1414 | goto out; |
1415 | ||
a092ff0f JS |
1416 | /* |
1417 | * With NO_HZ we may have to accumulate many cycle_intervals | |
1418 | * (think "ticks") worth of time at once. To do this efficiently, | |
1419 | * we calculate the largest doubling multiple of cycle_intervals | |
88b28adf | 1420 | * that is smaller than the offset. We then accumulate that |
a092ff0f JS |
1421 | * chunk in one go, and then try to consume the next smaller |
1422 | * doubled multiple. | |
8524070b | 1423 | */ |
4e250fdd | 1424 | shift = ilog2(offset) - ilog2(tk->cycle_interval); |
a092ff0f | 1425 | shift = max(0, shift); |
88b28adf | 1426 | /* Bound shift to one less than what overflows tick_length */ |
ea7cf49a | 1427 | maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1; |
a092ff0f | 1428 | shift = min(shift, maxshift); |
4e250fdd | 1429 | while (offset >= tk->cycle_interval) { |
5258d3f2 JS |
1430 | offset = logarithmic_accumulation(tk, offset, shift, |
1431 | &clock_set); | |
4e250fdd | 1432 | if (offset < tk->cycle_interval<<shift) |
830ec045 | 1433 | shift--; |
8524070b JS |
1434 | } |
1435 | ||
1436 | /* correct the clock when NTP error is too big */ | |
4e250fdd | 1437 | timekeeping_adjust(tk, offset); |
8524070b | 1438 | |
6a867a39 | 1439 | /* |
92bb1fcf JS |
1440 | * XXX This can be killed once everyone converts |
1441 | * to the new update_vsyscall. | |
1442 | */ | |
1443 | old_vsyscall_fixup(tk); | |
8524070b | 1444 | |
6a867a39 JS |
1445 | /* |
1446 | * Finally, make sure that after the rounding | |
1e75fa8b | 1447 | * xtime_nsec isn't larger than NSEC_PER_SEC |
6a867a39 | 1448 | */ |
5258d3f2 | 1449 | clock_set |= accumulate_nsecs_to_secs(tk); |
83f57a11 | 1450 | |
3fdb14fd | 1451 | write_seqcount_begin(&tk_core.seq); |
7ec98e15 TG |
1452 | /* Update clock->cycle_last with the new value */ |
1453 | clock->cycle_last = tk->cycle_last; | |
48cdc135 TG |
1454 | /* |
1455 | * Update the real timekeeper. | |
1456 | * | |
1457 | * We could avoid this memcpy by switching pointers, but that | |
1458 | * requires changes to all other timekeeper usage sites as | |
1459 | * well, i.e. move the timekeeper pointer getter into the | |
1460 | * spinlocked/seqcount protected sections. And we trade this | |
3fdb14fd | 1461 | * memcpy under the tk_core.seq against one before we start |
48cdc135 TG |
1462 | * updating. |
1463 | */ | |
1464 | memcpy(real_tk, tk, sizeof(*tk)); | |
5258d3f2 | 1465 | timekeeping_update(real_tk, clock_set); |
3fdb14fd | 1466 | write_seqcount_end(&tk_core.seq); |
ca4523cd | 1467 | out: |
9a7a71b1 | 1468 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
47a1b796 | 1469 | if (clock_set) |
cab5e127 JS |
1470 | /* Have to call _delayed version, since in irq context*/ |
1471 | clock_was_set_delayed(); | |
8524070b | 1472 | } |
7c3f1a57 TJ |
1473 | |
1474 | /** | |
1475 | * getboottime - Return the real time of system boot. | |
1476 | * @ts: pointer to the timespec to be set | |
1477 | * | |
abb3a4ea | 1478 | * Returns the wall-time of boot in a timespec. |
7c3f1a57 TJ |
1479 | * |
1480 | * This is based on the wall_to_monotonic offset and the total suspend | |
1481 | * time. Calls to settimeofday will affect the value returned (which | |
1482 | * basically means that however wrong your real time clock is at boot time, | |
1483 | * you get the right time here). | |
1484 | */ | |
1485 | void getboottime(struct timespec *ts) | |
1486 | { | |
3fdb14fd | 1487 | struct timekeeper *tk = &tk_core.timekeeper; |
36d47481 | 1488 | struct timespec boottime = { |
4e250fdd JS |
1489 | .tv_sec = tk->wall_to_monotonic.tv_sec + |
1490 | tk->total_sleep_time.tv_sec, | |
1491 | .tv_nsec = tk->wall_to_monotonic.tv_nsec + | |
1492 | tk->total_sleep_time.tv_nsec | |
36d47481 | 1493 | }; |
d4f587c6 | 1494 | |
d4f587c6 | 1495 | set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec); |
7c3f1a57 | 1496 | } |
c93d89f3 | 1497 | EXPORT_SYMBOL_GPL(getboottime); |
7c3f1a57 | 1498 | |
abb3a4ea JS |
1499 | /** |
1500 | * get_monotonic_boottime - Returns monotonic time since boot | |
1501 | * @ts: pointer to the timespec to be set | |
1502 | * | |
1503 | * Returns the monotonic time since boot in a timespec. | |
1504 | * | |
1505 | * This is similar to CLOCK_MONTONIC/ktime_get_ts, but also | |
1506 | * includes the time spent in suspend. | |
1507 | */ | |
1508 | void get_monotonic_boottime(struct timespec *ts) | |
1509 | { | |
3fdb14fd | 1510 | struct timekeeper *tk = &tk_core.timekeeper; |
7d489d15 | 1511 | struct timespec64 tomono, sleep, ret; |
ec145bab | 1512 | s64 nsec; |
abb3a4ea | 1513 | unsigned int seq; |
abb3a4ea JS |
1514 | |
1515 | WARN_ON(timekeeping_suspended); | |
1516 | ||
1517 | do { | |
3fdb14fd | 1518 | seq = read_seqcount_begin(&tk_core.seq); |
7d489d15 | 1519 | ret.tv_sec = tk->xtime_sec; |
ec145bab | 1520 | nsec = timekeeping_get_ns(tk); |
4e250fdd JS |
1521 | tomono = tk->wall_to_monotonic; |
1522 | sleep = tk->total_sleep_time; | |
abb3a4ea | 1523 | |
3fdb14fd | 1524 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
abb3a4ea | 1525 | |
7d489d15 JS |
1526 | ret.tv_sec += tomono.tv_sec + sleep.tv_sec; |
1527 | ret.tv_nsec = 0; | |
1528 | timespec64_add_ns(&ret, nsec + tomono.tv_nsec + sleep.tv_nsec); | |
1529 | *ts = timespec64_to_timespec(ret); | |
abb3a4ea JS |
1530 | } |
1531 | EXPORT_SYMBOL_GPL(get_monotonic_boottime); | |
1532 | ||
1533 | /** | |
1534 | * ktime_get_boottime - Returns monotonic time since boot in a ktime | |
1535 | * | |
1536 | * Returns the monotonic time since boot in a ktime | |
1537 | * | |
1538 | * This is similar to CLOCK_MONTONIC/ktime_get, but also | |
1539 | * includes the time spent in suspend. | |
1540 | */ | |
1541 | ktime_t ktime_get_boottime(void) | |
1542 | { | |
1543 | struct timespec ts; | |
1544 | ||
1545 | get_monotonic_boottime(&ts); | |
1546 | return timespec_to_ktime(ts); | |
1547 | } | |
1548 | EXPORT_SYMBOL_GPL(ktime_get_boottime); | |
1549 | ||
7c3f1a57 TJ |
1550 | /** |
1551 | * monotonic_to_bootbased - Convert the monotonic time to boot based. | |
1552 | * @ts: pointer to the timespec to be converted | |
1553 | */ | |
1554 | void monotonic_to_bootbased(struct timespec *ts) | |
1555 | { | |
3fdb14fd | 1556 | struct timekeeper *tk = &tk_core.timekeeper; |
7d489d15 | 1557 | struct timespec64 ts64; |
4e250fdd | 1558 | |
7d489d15 JS |
1559 | ts64 = timespec_to_timespec64(*ts); |
1560 | ts64 = timespec64_add(ts64, tk->total_sleep_time); | |
1561 | *ts = timespec64_to_timespec(ts64); | |
7c3f1a57 | 1562 | } |
c93d89f3 | 1563 | EXPORT_SYMBOL_GPL(monotonic_to_bootbased); |
2c6b47de | 1564 | |
17c38b74 JS |
1565 | unsigned long get_seconds(void) |
1566 | { | |
3fdb14fd | 1567 | struct timekeeper *tk = &tk_core.timekeeper; |
4e250fdd JS |
1568 | |
1569 | return tk->xtime_sec; | |
17c38b74 JS |
1570 | } |
1571 | EXPORT_SYMBOL(get_seconds); | |
1572 | ||
da15cfda JS |
1573 | struct timespec __current_kernel_time(void) |
1574 | { | |
3fdb14fd | 1575 | struct timekeeper *tk = &tk_core.timekeeper; |
4e250fdd | 1576 | |
7d489d15 | 1577 | return timespec64_to_timespec(tk_xtime(tk)); |
da15cfda | 1578 | } |
17c38b74 | 1579 | |
2c6b47de JS |
1580 | struct timespec current_kernel_time(void) |
1581 | { | |
3fdb14fd | 1582 | struct timekeeper *tk = &tk_core.timekeeper; |
7d489d15 | 1583 | struct timespec64 now; |
2c6b47de JS |
1584 | unsigned long seq; |
1585 | ||
1586 | do { | |
3fdb14fd | 1587 | seq = read_seqcount_begin(&tk_core.seq); |
83f57a11 | 1588 | |
4e250fdd | 1589 | now = tk_xtime(tk); |
3fdb14fd | 1590 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
2c6b47de | 1591 | |
7d489d15 | 1592 | return timespec64_to_timespec(now); |
2c6b47de | 1593 | } |
2c6b47de | 1594 | EXPORT_SYMBOL(current_kernel_time); |
da15cfda JS |
1595 | |
1596 | struct timespec get_monotonic_coarse(void) | |
1597 | { | |
3fdb14fd | 1598 | struct timekeeper *tk = &tk_core.timekeeper; |
7d489d15 | 1599 | struct timespec64 now, mono; |
da15cfda JS |
1600 | unsigned long seq; |
1601 | ||
1602 | do { | |
3fdb14fd | 1603 | seq = read_seqcount_begin(&tk_core.seq); |
83f57a11 | 1604 | |
4e250fdd JS |
1605 | now = tk_xtime(tk); |
1606 | mono = tk->wall_to_monotonic; | |
3fdb14fd | 1607 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
da15cfda | 1608 | |
7d489d15 | 1609 | set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec, |
da15cfda | 1610 | now.tv_nsec + mono.tv_nsec); |
7d489d15 JS |
1611 | |
1612 | return timespec64_to_timespec(now); | |
da15cfda | 1613 | } |
871cf1e5 TH |
1614 | |
1615 | /* | |
d6ad4187 | 1616 | * Must hold jiffies_lock |
871cf1e5 TH |
1617 | */ |
1618 | void do_timer(unsigned long ticks) | |
1619 | { | |
1620 | jiffies_64 += ticks; | |
871cf1e5 TH |
1621 | calc_global_load(ticks); |
1622 | } | |
48cf76f7 TH |
1623 | |
1624 | /** | |
76f41088 JS |
1625 | * ktime_get_update_offsets_tick - hrtimer helper |
1626 | * @offs_real: pointer to storage for monotonic -> realtime offset | |
1627 | * @offs_boot: pointer to storage for monotonic -> boottime offset | |
1628 | * @offs_tai: pointer to storage for monotonic -> clock tai offset | |
1629 | * | |
1630 | * Returns monotonic time at last tick and various offsets | |
48cf76f7 | 1631 | */ |
76f41088 JS |
1632 | ktime_t ktime_get_update_offsets_tick(ktime_t *offs_real, ktime_t *offs_boot, |
1633 | ktime_t *offs_tai) | |
48cf76f7 | 1634 | { |
3fdb14fd | 1635 | struct timekeeper *tk = &tk_core.timekeeper; |
7d489d15 | 1636 | struct timespec64 ts; |
76f41088 JS |
1637 | ktime_t now; |
1638 | unsigned int seq; | |
48cf76f7 TH |
1639 | |
1640 | do { | |
3fdb14fd | 1641 | seq = read_seqcount_begin(&tk_core.seq); |
76f41088 JS |
1642 | |
1643 | ts = tk_xtime(tk); | |
76f41088 JS |
1644 | *offs_real = tk->offs_real; |
1645 | *offs_boot = tk->offs_boot; | |
1646 | *offs_tai = tk->offs_tai; | |
3fdb14fd | 1647 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
76f41088 JS |
1648 | |
1649 | now = ktime_set(ts.tv_sec, ts.tv_nsec); | |
1650 | now = ktime_sub(now, *offs_real); | |
1651 | return now; | |
48cf76f7 | 1652 | } |
f0af911a | 1653 | |
f6c06abf TG |
1654 | #ifdef CONFIG_HIGH_RES_TIMERS |
1655 | /** | |
76f41088 | 1656 | * ktime_get_update_offsets_now - hrtimer helper |
f6c06abf TG |
1657 | * @offs_real: pointer to storage for monotonic -> realtime offset |
1658 | * @offs_boot: pointer to storage for monotonic -> boottime offset | |
b7bc50e4 | 1659 | * @offs_tai: pointer to storage for monotonic -> clock tai offset |
f6c06abf TG |
1660 | * |
1661 | * Returns current monotonic time and updates the offsets | |
b7bc50e4 | 1662 | * Called from hrtimer_interrupt() or retrigger_next_event() |
f6c06abf | 1663 | */ |
76f41088 | 1664 | ktime_t ktime_get_update_offsets_now(ktime_t *offs_real, ktime_t *offs_boot, |
90adda98 | 1665 | ktime_t *offs_tai) |
f6c06abf | 1666 | { |
3fdb14fd | 1667 | struct timekeeper *tk = &tk_core.timekeeper; |
f6c06abf TG |
1668 | ktime_t now; |
1669 | unsigned int seq; | |
1670 | u64 secs, nsecs; | |
1671 | ||
1672 | do { | |
3fdb14fd | 1673 | seq = read_seqcount_begin(&tk_core.seq); |
f6c06abf | 1674 | |
4e250fdd JS |
1675 | secs = tk->xtime_sec; |
1676 | nsecs = timekeeping_get_ns(tk); | |
f6c06abf | 1677 | |
4e250fdd JS |
1678 | *offs_real = tk->offs_real; |
1679 | *offs_boot = tk->offs_boot; | |
90adda98 | 1680 | *offs_tai = tk->offs_tai; |
3fdb14fd | 1681 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
f6c06abf TG |
1682 | |
1683 | now = ktime_add_ns(ktime_set(secs, 0), nsecs); | |
1684 | now = ktime_sub(now, *offs_real); | |
1685 | return now; | |
1686 | } | |
1687 | #endif | |
1688 | ||
99ee5315 TG |
1689 | /** |
1690 | * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format | |
1691 | */ | |
1692 | ktime_t ktime_get_monotonic_offset(void) | |
1693 | { | |
3fdb14fd | 1694 | struct timekeeper *tk = &tk_core.timekeeper; |
99ee5315 | 1695 | unsigned long seq; |
7d489d15 | 1696 | struct timespec64 wtom; |
99ee5315 TG |
1697 | |
1698 | do { | |
3fdb14fd | 1699 | seq = read_seqcount_begin(&tk_core.seq); |
4e250fdd | 1700 | wtom = tk->wall_to_monotonic; |
3fdb14fd | 1701 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
70471f2f | 1702 | |
7d489d15 | 1703 | return timespec64_to_ktime(wtom); |
99ee5315 | 1704 | } |
a80b83b7 JS |
1705 | EXPORT_SYMBOL_GPL(ktime_get_monotonic_offset); |
1706 | ||
aa6f9c59 JS |
1707 | /** |
1708 | * do_adjtimex() - Accessor function to NTP __do_adjtimex function | |
1709 | */ | |
1710 | int do_adjtimex(struct timex *txc) | |
1711 | { | |
3fdb14fd | 1712 | struct timekeeper *tk = &tk_core.timekeeper; |
06c017fd | 1713 | unsigned long flags; |
7d489d15 | 1714 | struct timespec64 ts; |
4e8f8b34 | 1715 | s32 orig_tai, tai; |
e4085693 JS |
1716 | int ret; |
1717 | ||
1718 | /* Validate the data before disabling interrupts */ | |
1719 | ret = ntp_validate_timex(txc); | |
1720 | if (ret) | |
1721 | return ret; | |
1722 | ||
cef90377 JS |
1723 | if (txc->modes & ADJ_SETOFFSET) { |
1724 | struct timespec delta; | |
1725 | delta.tv_sec = txc->time.tv_sec; | |
1726 | delta.tv_nsec = txc->time.tv_usec; | |
1727 | if (!(txc->modes & ADJ_NANO)) | |
1728 | delta.tv_nsec *= 1000; | |
1729 | ret = timekeeping_inject_offset(&delta); | |
1730 | if (ret) | |
1731 | return ret; | |
1732 | } | |
1733 | ||
d6d29896 | 1734 | getnstimeofday64(&ts); |
87ace39b | 1735 | |
06c017fd | 1736 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
3fdb14fd | 1737 | write_seqcount_begin(&tk_core.seq); |
06c017fd | 1738 | |
4e8f8b34 | 1739 | orig_tai = tai = tk->tai_offset; |
87ace39b | 1740 | ret = __do_adjtimex(txc, &ts, &tai); |
aa6f9c59 | 1741 | |
4e8f8b34 JS |
1742 | if (tai != orig_tai) { |
1743 | __timekeeping_set_tai_offset(tk, tai); | |
f55c0760 | 1744 | timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); |
4e8f8b34 | 1745 | } |
3fdb14fd | 1746 | write_seqcount_end(&tk_core.seq); |
06c017fd JS |
1747 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
1748 | ||
6fdda9a9 JS |
1749 | if (tai != orig_tai) |
1750 | clock_was_set(); | |
1751 | ||
7bd36014 JS |
1752 | ntp_notify_cmos_timer(); |
1753 | ||
87ace39b JS |
1754 | return ret; |
1755 | } | |
aa6f9c59 JS |
1756 | |
1757 | #ifdef CONFIG_NTP_PPS | |
1758 | /** | |
1759 | * hardpps() - Accessor function to NTP __hardpps function | |
1760 | */ | |
1761 | void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts) | |
1762 | { | |
06c017fd JS |
1763 | unsigned long flags; |
1764 | ||
1765 | raw_spin_lock_irqsave(&timekeeper_lock, flags); | |
3fdb14fd | 1766 | write_seqcount_begin(&tk_core.seq); |
06c017fd | 1767 | |
aa6f9c59 | 1768 | __hardpps(phase_ts, raw_ts); |
06c017fd | 1769 | |
3fdb14fd | 1770 | write_seqcount_end(&tk_core.seq); |
06c017fd | 1771 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
aa6f9c59 JS |
1772 | } |
1773 | EXPORT_SYMBOL(hardpps); | |
1774 | #endif | |
1775 | ||
f0af911a TH |
1776 | /** |
1777 | * xtime_update() - advances the timekeeping infrastructure | |
1778 | * @ticks: number of ticks, that have elapsed since the last call. | |
1779 | * | |
1780 | * Must be called with interrupts disabled. | |
1781 | */ | |
1782 | void xtime_update(unsigned long ticks) | |
1783 | { | |
d6ad4187 | 1784 | write_seqlock(&jiffies_lock); |
f0af911a | 1785 | do_timer(ticks); |
d6ad4187 | 1786 | write_sequnlock(&jiffies_lock); |
47a1b796 | 1787 | update_wall_time(); |
f0af911a | 1788 | } |