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