]>
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 | ||
11 | #include <linux/module.h> | |
12 | #include <linux/interrupt.h> | |
13 | #include <linux/percpu.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/mm.h> | |
d43c36dc | 16 | #include <linux/sched.h> |
8524070b JS |
17 | #include <linux/sysdev.h> |
18 | #include <linux/clocksource.h> | |
19 | #include <linux/jiffies.h> | |
20 | #include <linux/time.h> | |
21 | #include <linux/tick.h> | |
75c5158f | 22 | #include <linux/stop_machine.h> |
8524070b | 23 | |
155ec602 MS |
24 | /* Structure holding internal timekeeping values. */ |
25 | struct timekeeper { | |
26 | /* Current clocksource used for timekeeping. */ | |
27 | struct clocksource *clock; | |
23ce7211 MS |
28 | /* The shift value of the current clocksource. */ |
29 | int shift; | |
155ec602 MS |
30 | |
31 | /* Number of clock cycles in one NTP interval. */ | |
32 | cycle_t cycle_interval; | |
33 | /* Number of clock shifted nano seconds in one NTP interval. */ | |
34 | u64 xtime_interval; | |
35 | /* Raw nano seconds accumulated per NTP interval. */ | |
36 | u32 raw_interval; | |
37 | ||
38 | /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */ | |
39 | u64 xtime_nsec; | |
40 | /* Difference between accumulated time and NTP time in ntp | |
41 | * shifted nano seconds. */ | |
42 | s64 ntp_error; | |
23ce7211 MS |
43 | /* Shift conversion between clock shifted nano seconds and |
44 | * ntp shifted nano seconds. */ | |
45 | int ntp_error_shift; | |
0a544198 MS |
46 | /* NTP adjusted clock multiplier */ |
47 | u32 mult; | |
155ec602 MS |
48 | }; |
49 | ||
50 | struct timekeeper timekeeper; | |
51 | ||
52 | /** | |
53 | * timekeeper_setup_internals - Set up internals to use clocksource clock. | |
54 | * | |
55 | * @clock: Pointer to clocksource. | |
56 | * | |
57 | * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment | |
58 | * pair and interval request. | |
59 | * | |
60 | * Unless you're the timekeeping code, you should not be using this! | |
61 | */ | |
62 | static void timekeeper_setup_internals(struct clocksource *clock) | |
63 | { | |
64 | cycle_t interval; | |
65 | u64 tmp; | |
66 | ||
67 | timekeeper.clock = clock; | |
68 | clock->cycle_last = clock->read(clock); | |
69 | ||
70 | /* Do the ns -> cycle conversion first, using original mult */ | |
71 | tmp = NTP_INTERVAL_LENGTH; | |
72 | tmp <<= clock->shift; | |
0a544198 MS |
73 | tmp += clock->mult/2; |
74 | do_div(tmp, clock->mult); | |
155ec602 MS |
75 | if (tmp == 0) |
76 | tmp = 1; | |
77 | ||
78 | interval = (cycle_t) tmp; | |
79 | timekeeper.cycle_interval = interval; | |
80 | ||
81 | /* Go back from cycles -> shifted ns */ | |
82 | timekeeper.xtime_interval = (u64) interval * clock->mult; | |
83 | timekeeper.raw_interval = | |
0a544198 | 84 | ((u64) interval * clock->mult) >> clock->shift; |
155ec602 MS |
85 | |
86 | timekeeper.xtime_nsec = 0; | |
23ce7211 | 87 | timekeeper.shift = clock->shift; |
155ec602 MS |
88 | |
89 | timekeeper.ntp_error = 0; | |
23ce7211 | 90 | timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift; |
0a544198 MS |
91 | |
92 | /* | |
93 | * The timekeeper keeps its own mult values for the currently | |
94 | * active clocksource. These value will be adjusted via NTP | |
95 | * to counteract clock drifting. | |
96 | */ | |
97 | timekeeper.mult = clock->mult; | |
155ec602 | 98 | } |
8524070b | 99 | |
2ba2a305 MS |
100 | /* Timekeeper helper functions. */ |
101 | static inline s64 timekeeping_get_ns(void) | |
102 | { | |
103 | cycle_t cycle_now, cycle_delta; | |
104 | struct clocksource *clock; | |
105 | ||
106 | /* read clocksource: */ | |
107 | clock = timekeeper.clock; | |
108 | cycle_now = clock->read(clock); | |
109 | ||
110 | /* calculate the delta since the last update_wall_time: */ | |
111 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; | |
112 | ||
113 | /* return delta convert to nanoseconds using ntp adjusted mult. */ | |
114 | return clocksource_cyc2ns(cycle_delta, timekeeper.mult, | |
115 | timekeeper.shift); | |
116 | } | |
117 | ||
118 | static inline s64 timekeeping_get_ns_raw(void) | |
119 | { | |
120 | cycle_t cycle_now, cycle_delta; | |
121 | struct clocksource *clock; | |
122 | ||
123 | /* read clocksource: */ | |
124 | clock = timekeeper.clock; | |
125 | cycle_now = clock->read(clock); | |
126 | ||
127 | /* calculate the delta since the last update_wall_time: */ | |
128 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; | |
129 | ||
130 | /* return delta convert to nanoseconds using ntp adjusted mult. */ | |
131 | return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift); | |
132 | } | |
133 | ||
8524070b JS |
134 | /* |
135 | * This read-write spinlock protects us from races in SMP while | |
dce48a84 | 136 | * playing with xtime. |
8524070b | 137 | */ |
ba2a631b | 138 | __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock); |
8524070b JS |
139 | |
140 | ||
141 | /* | |
142 | * The current time | |
143 | * wall_to_monotonic is what we need to add to xtime (or xtime corrected | |
144 | * for sub jiffie times) to get to monotonic time. Monotonic is pegged | |
145 | * at zero at system boot time, so wall_to_monotonic will be negative, | |
146 | * however, we will ALWAYS keep the tv_nsec part positive so we can use | |
147 | * the usual normalization. | |
7c3f1a57 TJ |
148 | * |
149 | * wall_to_monotonic is moved after resume from suspend for the monotonic | |
150 | * time not to jump. We need to add total_sleep_time to wall_to_monotonic | |
151 | * to get the real boot based time offset. | |
152 | * | |
153 | * - wall_to_monotonic is no longer the boot time, getboottime must be | |
154 | * used instead. | |
8524070b JS |
155 | */ |
156 | struct timespec xtime __attribute__ ((aligned (16))); | |
157 | struct timespec wall_to_monotonic __attribute__ ((aligned (16))); | |
d4f587c6 | 158 | static struct timespec total_sleep_time; |
8524070b | 159 | |
155ec602 MS |
160 | /* |
161 | * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. | |
162 | */ | |
163 | struct timespec raw_time; | |
164 | ||
1c5745aa TG |
165 | /* flag for if timekeeping is suspended */ |
166 | int __read_mostly timekeeping_suspended; | |
167 | ||
31089c13 JS |
168 | /* must hold xtime_lock */ |
169 | void timekeeping_leap_insert(int leapsecond) | |
170 | { | |
171 | xtime.tv_sec += leapsecond; | |
172 | wall_to_monotonic.tv_sec -= leapsecond; | |
0696b711 | 173 | update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult); |
31089c13 | 174 | } |
8524070b | 175 | |
8524070b | 176 | /** |
155ec602 | 177 | * timekeeping_forward_now - update clock to the current time |
8524070b | 178 | * |
9a055117 RZ |
179 | * Forward the current clock to update its state since the last call to |
180 | * update_wall_time(). This is useful before significant clock changes, | |
181 | * as it avoids having to deal with this time offset explicitly. | |
8524070b | 182 | */ |
155ec602 | 183 | static void timekeeping_forward_now(void) |
8524070b JS |
184 | { |
185 | cycle_t cycle_now, cycle_delta; | |
155ec602 | 186 | struct clocksource *clock; |
9a055117 | 187 | s64 nsec; |
8524070b | 188 | |
155ec602 | 189 | clock = timekeeper.clock; |
a0f7d48b | 190 | cycle_now = clock->read(clock); |
8524070b | 191 | cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; |
9a055117 | 192 | clock->cycle_last = cycle_now; |
8524070b | 193 | |
0a544198 MS |
194 | nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult, |
195 | timekeeper.shift); | |
7d27558c JS |
196 | |
197 | /* If arch requires, add in gettimeoffset() */ | |
198 | nsec += arch_gettimeoffset(); | |
199 | ||
9a055117 | 200 | timespec_add_ns(&xtime, nsec); |
2d42244a | 201 | |
0a544198 | 202 | nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift); |
155ec602 | 203 | timespec_add_ns(&raw_time, nsec); |
8524070b JS |
204 | } |
205 | ||
206 | /** | |
efd9ac86 | 207 | * getnstimeofday - Returns the time of day in a timespec |
8524070b JS |
208 | * @ts: pointer to the timespec to be set |
209 | * | |
efd9ac86 | 210 | * Returns the time of day in a timespec. |
8524070b | 211 | */ |
efd9ac86 | 212 | void getnstimeofday(struct timespec *ts) |
8524070b JS |
213 | { |
214 | unsigned long seq; | |
215 | s64 nsecs; | |
216 | ||
1c5745aa TG |
217 | WARN_ON(timekeeping_suspended); |
218 | ||
8524070b JS |
219 | do { |
220 | seq = read_seqbegin(&xtime_lock); | |
221 | ||
222 | *ts = xtime; | |
2ba2a305 | 223 | nsecs = timekeeping_get_ns(); |
8524070b | 224 | |
7d27558c JS |
225 | /* If arch requires, add in gettimeoffset() */ |
226 | nsecs += arch_gettimeoffset(); | |
227 | ||
8524070b JS |
228 | } while (read_seqretry(&xtime_lock, seq)); |
229 | ||
230 | timespec_add_ns(ts, nsecs); | |
231 | } | |
232 | ||
8524070b JS |
233 | EXPORT_SYMBOL(getnstimeofday); |
234 | ||
951ed4d3 MS |
235 | ktime_t ktime_get(void) |
236 | { | |
951ed4d3 MS |
237 | unsigned int seq; |
238 | s64 secs, nsecs; | |
239 | ||
240 | WARN_ON(timekeeping_suspended); | |
241 | ||
242 | do { | |
243 | seq = read_seqbegin(&xtime_lock); | |
244 | secs = xtime.tv_sec + wall_to_monotonic.tv_sec; | |
245 | nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec; | |
2ba2a305 | 246 | nsecs += timekeeping_get_ns(); |
951ed4d3 MS |
247 | |
248 | } while (read_seqretry(&xtime_lock, seq)); | |
249 | /* | |
250 | * Use ktime_set/ktime_add_ns to create a proper ktime on | |
251 | * 32-bit architectures without CONFIG_KTIME_SCALAR. | |
252 | */ | |
253 | return ktime_add_ns(ktime_set(secs, 0), nsecs); | |
254 | } | |
255 | EXPORT_SYMBOL_GPL(ktime_get); | |
256 | ||
257 | /** | |
258 | * ktime_get_ts - get the monotonic clock in timespec format | |
259 | * @ts: pointer to timespec variable | |
260 | * | |
261 | * The function calculates the monotonic clock from the realtime | |
262 | * clock and the wall_to_monotonic offset and stores the result | |
263 | * in normalized timespec format in the variable pointed to by @ts. | |
264 | */ | |
265 | void ktime_get_ts(struct timespec *ts) | |
266 | { | |
951ed4d3 MS |
267 | struct timespec tomono; |
268 | unsigned int seq; | |
269 | s64 nsecs; | |
270 | ||
271 | WARN_ON(timekeeping_suspended); | |
272 | ||
273 | do { | |
274 | seq = read_seqbegin(&xtime_lock); | |
275 | *ts = xtime; | |
276 | tomono = wall_to_monotonic; | |
2ba2a305 | 277 | nsecs = timekeeping_get_ns(); |
951ed4d3 MS |
278 | |
279 | } while (read_seqretry(&xtime_lock, seq)); | |
280 | ||
281 | set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec, | |
282 | ts->tv_nsec + tomono.tv_nsec + nsecs); | |
283 | } | |
284 | EXPORT_SYMBOL_GPL(ktime_get_ts); | |
285 | ||
8524070b JS |
286 | /** |
287 | * do_gettimeofday - Returns the time of day in a timeval | |
288 | * @tv: pointer to the timeval to be set | |
289 | * | |
efd9ac86 | 290 | * NOTE: Users should be converted to using getnstimeofday() |
8524070b JS |
291 | */ |
292 | void do_gettimeofday(struct timeval *tv) | |
293 | { | |
294 | struct timespec now; | |
295 | ||
efd9ac86 | 296 | getnstimeofday(&now); |
8524070b JS |
297 | tv->tv_sec = now.tv_sec; |
298 | tv->tv_usec = now.tv_nsec/1000; | |
299 | } | |
300 | ||
301 | EXPORT_SYMBOL(do_gettimeofday); | |
302 | /** | |
303 | * do_settimeofday - Sets the time of day | |
304 | * @tv: pointer to the timespec variable containing the new time | |
305 | * | |
306 | * Sets the time of day to the new time and update NTP and notify hrtimers | |
307 | */ | |
308 | int do_settimeofday(struct timespec *tv) | |
309 | { | |
9a055117 | 310 | struct timespec ts_delta; |
8524070b | 311 | unsigned long flags; |
8524070b JS |
312 | |
313 | if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) | |
314 | return -EINVAL; | |
315 | ||
316 | write_seqlock_irqsave(&xtime_lock, flags); | |
317 | ||
155ec602 | 318 | timekeeping_forward_now(); |
9a055117 RZ |
319 | |
320 | ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec; | |
321 | ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec; | |
322 | wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta); | |
8524070b | 323 | |
9a055117 | 324 | xtime = *tv; |
8524070b | 325 | |
155ec602 | 326 | timekeeper.ntp_error = 0; |
8524070b JS |
327 | ntp_clear(); |
328 | ||
0696b711 | 329 | update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult); |
8524070b JS |
330 | |
331 | write_sequnlock_irqrestore(&xtime_lock, flags); | |
332 | ||
333 | /* signal hrtimers about time change */ | |
334 | clock_was_set(); | |
335 | ||
336 | return 0; | |
337 | } | |
338 | ||
339 | EXPORT_SYMBOL(do_settimeofday); | |
340 | ||
341 | /** | |
342 | * change_clocksource - Swaps clocksources if a new one is available | |
343 | * | |
344 | * Accumulates current time interval and initializes new clocksource | |
345 | */ | |
75c5158f | 346 | static int change_clocksource(void *data) |
8524070b | 347 | { |
4614e6ad | 348 | struct clocksource *new, *old; |
8524070b | 349 | |
75c5158f | 350 | new = (struct clocksource *) data; |
8524070b | 351 | |
155ec602 | 352 | timekeeping_forward_now(); |
75c5158f MS |
353 | if (!new->enable || new->enable(new) == 0) { |
354 | old = timekeeper.clock; | |
355 | timekeeper_setup_internals(new); | |
356 | if (old->disable) | |
357 | old->disable(old); | |
358 | } | |
359 | return 0; | |
360 | } | |
8524070b | 361 | |
75c5158f MS |
362 | /** |
363 | * timekeeping_notify - Install a new clock source | |
364 | * @clock: pointer to the clock source | |
365 | * | |
366 | * This function is called from clocksource.c after a new, better clock | |
367 | * source has been registered. The caller holds the clocksource_mutex. | |
368 | */ | |
369 | void timekeeping_notify(struct clocksource *clock) | |
370 | { | |
371 | if (timekeeper.clock == clock) | |
4614e6ad | 372 | return; |
75c5158f | 373 | stop_machine(change_clocksource, clock, NULL); |
8524070b | 374 | tick_clock_notify(); |
8524070b | 375 | } |
75c5158f | 376 | |
a40f262c TG |
377 | /** |
378 | * ktime_get_real - get the real (wall-) time in ktime_t format | |
379 | * | |
380 | * returns the time in ktime_t format | |
381 | */ | |
382 | ktime_t ktime_get_real(void) | |
383 | { | |
384 | struct timespec now; | |
385 | ||
386 | getnstimeofday(&now); | |
387 | ||
388 | return timespec_to_ktime(now); | |
389 | } | |
390 | EXPORT_SYMBOL_GPL(ktime_get_real); | |
8524070b | 391 | |
2d42244a JS |
392 | /** |
393 | * getrawmonotonic - Returns the raw monotonic time in a timespec | |
394 | * @ts: pointer to the timespec to be set | |
395 | * | |
396 | * Returns the raw monotonic time (completely un-modified by ntp) | |
397 | */ | |
398 | void getrawmonotonic(struct timespec *ts) | |
399 | { | |
400 | unsigned long seq; | |
401 | s64 nsecs; | |
2d42244a JS |
402 | |
403 | do { | |
404 | seq = read_seqbegin(&xtime_lock); | |
2ba2a305 | 405 | nsecs = timekeeping_get_ns_raw(); |
155ec602 | 406 | *ts = raw_time; |
2d42244a JS |
407 | |
408 | } while (read_seqretry(&xtime_lock, seq)); | |
409 | ||
410 | timespec_add_ns(ts, nsecs); | |
411 | } | |
412 | EXPORT_SYMBOL(getrawmonotonic); | |
413 | ||
414 | ||
8524070b | 415 | /** |
cf4fc6cb | 416 | * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres |
8524070b | 417 | */ |
cf4fc6cb | 418 | int timekeeping_valid_for_hres(void) |
8524070b JS |
419 | { |
420 | unsigned long seq; | |
421 | int ret; | |
422 | ||
423 | do { | |
424 | seq = read_seqbegin(&xtime_lock); | |
425 | ||
155ec602 | 426 | ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; |
8524070b JS |
427 | |
428 | } while (read_seqretry(&xtime_lock, seq)); | |
429 | ||
430 | return ret; | |
431 | } | |
432 | ||
98962465 JH |
433 | /** |
434 | * timekeeping_max_deferment - Returns max time the clocksource can be deferred | |
435 | * | |
436 | * Caller must observe xtime_lock via read_seqbegin/read_seqretry to | |
437 | * ensure that the clocksource does not change! | |
438 | */ | |
439 | u64 timekeeping_max_deferment(void) | |
440 | { | |
441 | return timekeeper.clock->max_idle_ns; | |
442 | } | |
443 | ||
8524070b | 444 | /** |
d4f587c6 | 445 | * read_persistent_clock - Return time from the persistent clock. |
8524070b JS |
446 | * |
447 | * Weak dummy function for arches that do not yet support it. | |
d4f587c6 MS |
448 | * Reads the time from the battery backed persistent clock. |
449 | * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported. | |
8524070b JS |
450 | * |
451 | * XXX - Do be sure to remove it once all arches implement it. | |
452 | */ | |
d4f587c6 | 453 | void __attribute__((weak)) read_persistent_clock(struct timespec *ts) |
8524070b | 454 | { |
d4f587c6 MS |
455 | ts->tv_sec = 0; |
456 | ts->tv_nsec = 0; | |
8524070b JS |
457 | } |
458 | ||
23970e38 MS |
459 | /** |
460 | * read_boot_clock - Return time of the system start. | |
461 | * | |
462 | * Weak dummy function for arches that do not yet support it. | |
463 | * Function to read the exact time the system has been started. | |
464 | * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported. | |
465 | * | |
466 | * XXX - Do be sure to remove it once all arches implement it. | |
467 | */ | |
468 | void __attribute__((weak)) read_boot_clock(struct timespec *ts) | |
469 | { | |
470 | ts->tv_sec = 0; | |
471 | ts->tv_nsec = 0; | |
472 | } | |
473 | ||
8524070b JS |
474 | /* |
475 | * timekeeping_init - Initializes the clocksource and common timekeeping values | |
476 | */ | |
477 | void __init timekeeping_init(void) | |
478 | { | |
155ec602 | 479 | struct clocksource *clock; |
8524070b | 480 | unsigned long flags; |
23970e38 | 481 | struct timespec now, boot; |
d4f587c6 MS |
482 | |
483 | read_persistent_clock(&now); | |
23970e38 | 484 | read_boot_clock(&boot); |
8524070b JS |
485 | |
486 | write_seqlock_irqsave(&xtime_lock, flags); | |
487 | ||
7dffa3c6 | 488 | ntp_init(); |
8524070b | 489 | |
f1b82746 | 490 | clock = clocksource_default_clock(); |
a0f7d48b MS |
491 | if (clock->enable) |
492 | clock->enable(clock); | |
155ec602 | 493 | timekeeper_setup_internals(clock); |
8524070b | 494 | |
d4f587c6 MS |
495 | xtime.tv_sec = now.tv_sec; |
496 | xtime.tv_nsec = now.tv_nsec; | |
155ec602 MS |
497 | raw_time.tv_sec = 0; |
498 | raw_time.tv_nsec = 0; | |
23970e38 MS |
499 | if (boot.tv_sec == 0 && boot.tv_nsec == 0) { |
500 | boot.tv_sec = xtime.tv_sec; | |
501 | boot.tv_nsec = xtime.tv_nsec; | |
502 | } | |
8524070b | 503 | set_normalized_timespec(&wall_to_monotonic, |
23970e38 | 504 | -boot.tv_sec, -boot.tv_nsec); |
d4f587c6 MS |
505 | total_sleep_time.tv_sec = 0; |
506 | total_sleep_time.tv_nsec = 0; | |
8524070b JS |
507 | write_sequnlock_irqrestore(&xtime_lock, flags); |
508 | } | |
509 | ||
8524070b | 510 | /* time in seconds when suspend began */ |
d4f587c6 | 511 | static struct timespec timekeeping_suspend_time; |
8524070b JS |
512 | |
513 | /** | |
514 | * timekeeping_resume - Resumes the generic timekeeping subsystem. | |
515 | * @dev: unused | |
516 | * | |
517 | * This is for the generic clocksource timekeeping. | |
518 | * xtime/wall_to_monotonic/jiffies/etc are | |
519 | * still managed by arch specific suspend/resume code. | |
520 | */ | |
521 | static int timekeeping_resume(struct sys_device *dev) | |
522 | { | |
523 | unsigned long flags; | |
d4f587c6 MS |
524 | struct timespec ts; |
525 | ||
526 | read_persistent_clock(&ts); | |
8524070b | 527 | |
d10ff3fb TG |
528 | clocksource_resume(); |
529 | ||
8524070b JS |
530 | write_seqlock_irqsave(&xtime_lock, flags); |
531 | ||
d4f587c6 MS |
532 | if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) { |
533 | ts = timespec_sub(ts, timekeeping_suspend_time); | |
ce3bf7ab | 534 | xtime = timespec_add(xtime, ts); |
d4f587c6 | 535 | wall_to_monotonic = timespec_sub(wall_to_monotonic, ts); |
ce3bf7ab | 536 | total_sleep_time = timespec_add(total_sleep_time, ts); |
8524070b JS |
537 | } |
538 | /* re-base the last cycle value */ | |
155ec602 MS |
539 | timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock); |
540 | timekeeper.ntp_error = 0; | |
8524070b JS |
541 | timekeeping_suspended = 0; |
542 | write_sequnlock_irqrestore(&xtime_lock, flags); | |
543 | ||
544 | touch_softlockup_watchdog(); | |
545 | ||
546 | clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL); | |
547 | ||
548 | /* Resume hrtimers */ | |
549 | hres_timers_resume(); | |
550 | ||
551 | return 0; | |
552 | } | |
553 | ||
554 | static int timekeeping_suspend(struct sys_device *dev, pm_message_t state) | |
555 | { | |
556 | unsigned long flags; | |
557 | ||
d4f587c6 | 558 | read_persistent_clock(&timekeeping_suspend_time); |
3be90950 | 559 | |
8524070b | 560 | write_seqlock_irqsave(&xtime_lock, flags); |
155ec602 | 561 | timekeeping_forward_now(); |
8524070b | 562 | timekeeping_suspended = 1; |
8524070b JS |
563 | write_sequnlock_irqrestore(&xtime_lock, flags); |
564 | ||
565 | clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL); | |
c54a42b1 | 566 | clocksource_suspend(); |
8524070b JS |
567 | |
568 | return 0; | |
569 | } | |
570 | ||
571 | /* sysfs resume/suspend bits for timekeeping */ | |
572 | static struct sysdev_class timekeeping_sysclass = { | |
af5ca3f4 | 573 | .name = "timekeeping", |
8524070b JS |
574 | .resume = timekeeping_resume, |
575 | .suspend = timekeeping_suspend, | |
8524070b JS |
576 | }; |
577 | ||
578 | static struct sys_device device_timer = { | |
579 | .id = 0, | |
580 | .cls = &timekeeping_sysclass, | |
581 | }; | |
582 | ||
583 | static int __init timekeeping_init_device(void) | |
584 | { | |
585 | int error = sysdev_class_register(&timekeeping_sysclass); | |
586 | if (!error) | |
587 | error = sysdev_register(&device_timer); | |
588 | return error; | |
589 | } | |
590 | ||
591 | device_initcall(timekeeping_init_device); | |
592 | ||
593 | /* | |
594 | * If the error is already larger, we look ahead even further | |
595 | * to compensate for late or lost adjustments. | |
596 | */ | |
155ec602 | 597 | static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval, |
8524070b JS |
598 | s64 *offset) |
599 | { | |
600 | s64 tick_error, i; | |
601 | u32 look_ahead, adj; | |
602 | s32 error2, mult; | |
603 | ||
604 | /* | |
605 | * Use the current error value to determine how much to look ahead. | |
606 | * The larger the error the slower we adjust for it to avoid problems | |
607 | * with losing too many ticks, otherwise we would overadjust and | |
608 | * produce an even larger error. The smaller the adjustment the | |
609 | * faster we try to adjust for it, as lost ticks can do less harm | |
3eb05676 | 610 | * here. This is tuned so that an error of about 1 msec is adjusted |
8524070b JS |
611 | * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks). |
612 | */ | |
155ec602 | 613 | error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ); |
8524070b JS |
614 | error2 = abs(error2); |
615 | for (look_ahead = 0; error2 > 0; look_ahead++) | |
616 | error2 >>= 2; | |
617 | ||
618 | /* | |
619 | * Now calculate the error in (1 << look_ahead) ticks, but first | |
620 | * remove the single look ahead already included in the error. | |
621 | */ | |
23ce7211 | 622 | tick_error = tick_length >> (timekeeper.ntp_error_shift + 1); |
155ec602 | 623 | tick_error -= timekeeper.xtime_interval >> 1; |
8524070b JS |
624 | error = ((error - tick_error) >> look_ahead) + tick_error; |
625 | ||
626 | /* Finally calculate the adjustment shift value. */ | |
627 | i = *interval; | |
628 | mult = 1; | |
629 | if (error < 0) { | |
630 | error = -error; | |
631 | *interval = -*interval; | |
632 | *offset = -*offset; | |
633 | mult = -1; | |
634 | } | |
635 | for (adj = 0; error > i; adj++) | |
636 | error >>= 1; | |
637 | ||
638 | *interval <<= adj; | |
639 | *offset <<= adj; | |
640 | return mult << adj; | |
641 | } | |
642 | ||
643 | /* | |
644 | * Adjust the multiplier to reduce the error value, | |
645 | * this is optimized for the most common adjustments of -1,0,1, | |
646 | * for other values we can do a bit more work. | |
647 | */ | |
155ec602 | 648 | static void timekeeping_adjust(s64 offset) |
8524070b | 649 | { |
155ec602 | 650 | s64 error, interval = timekeeper.cycle_interval; |
8524070b JS |
651 | int adj; |
652 | ||
23ce7211 | 653 | error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1); |
8524070b JS |
654 | if (error > interval) { |
655 | error >>= 2; | |
656 | if (likely(error <= interval)) | |
657 | adj = 1; | |
658 | else | |
155ec602 | 659 | adj = timekeeping_bigadjust(error, &interval, &offset); |
8524070b JS |
660 | } else if (error < -interval) { |
661 | error >>= 2; | |
662 | if (likely(error >= -interval)) { | |
663 | adj = -1; | |
664 | interval = -interval; | |
665 | offset = -offset; | |
666 | } else | |
155ec602 | 667 | adj = timekeeping_bigadjust(error, &interval, &offset); |
8524070b JS |
668 | } else |
669 | return; | |
670 | ||
0a544198 | 671 | timekeeper.mult += adj; |
155ec602 MS |
672 | timekeeper.xtime_interval += interval; |
673 | timekeeper.xtime_nsec -= offset; | |
674 | timekeeper.ntp_error -= (interval - offset) << | |
23ce7211 | 675 | timekeeper.ntp_error_shift; |
8524070b JS |
676 | } |
677 | ||
83f57a11 | 678 | |
a092ff0f JS |
679 | /** |
680 | * logarithmic_accumulation - shifted accumulation of cycles | |
681 | * | |
682 | * This functions accumulates a shifted interval of cycles into | |
683 | * into a shifted interval nanoseconds. Allows for O(log) accumulation | |
684 | * loop. | |
685 | * | |
686 | * Returns the unconsumed cycles. | |
687 | */ | |
688 | static cycle_t logarithmic_accumulation(cycle_t offset, int shift) | |
689 | { | |
690 | u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift; | |
691 | ||
692 | /* If the offset is smaller then a shifted interval, do nothing */ | |
693 | if (offset < timekeeper.cycle_interval<<shift) | |
694 | return offset; | |
695 | ||
696 | /* Accumulate one shifted interval */ | |
697 | offset -= timekeeper.cycle_interval << shift; | |
698 | timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift; | |
699 | ||
700 | timekeeper.xtime_nsec += timekeeper.xtime_interval << shift; | |
701 | while (timekeeper.xtime_nsec >= nsecps) { | |
702 | timekeeper.xtime_nsec -= nsecps; | |
703 | xtime.tv_sec++; | |
704 | second_overflow(); | |
705 | } | |
706 | ||
707 | /* Accumulate into raw time */ | |
708 | raw_time.tv_nsec += timekeeper.raw_interval << shift;; | |
709 | while (raw_time.tv_nsec >= NSEC_PER_SEC) { | |
710 | raw_time.tv_nsec -= NSEC_PER_SEC; | |
711 | raw_time.tv_sec++; | |
712 | } | |
713 | ||
714 | /* Accumulate error between NTP and clock interval */ | |
715 | timekeeper.ntp_error += tick_length << shift; | |
716 | timekeeper.ntp_error -= timekeeper.xtime_interval << | |
717 | (timekeeper.ntp_error_shift + shift); | |
718 | ||
719 | return offset; | |
720 | } | |
721 | ||
83f57a11 | 722 | |
8524070b JS |
723 | /** |
724 | * update_wall_time - Uses the current clocksource to increment the wall time | |
725 | * | |
726 | * Called from the timer interrupt, must hold a write on xtime_lock. | |
727 | */ | |
728 | void update_wall_time(void) | |
729 | { | |
155ec602 | 730 | struct clocksource *clock; |
8524070b | 731 | cycle_t offset; |
a092ff0f | 732 | int shift = 0, maxshift; |
8524070b JS |
733 | |
734 | /* Make sure we're fully resumed: */ | |
735 | if (unlikely(timekeeping_suspended)) | |
736 | return; | |
737 | ||
155ec602 | 738 | clock = timekeeper.clock; |
592913ec JS |
739 | |
740 | #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET | |
155ec602 | 741 | offset = timekeeper.cycle_interval; |
592913ec JS |
742 | #else |
743 | offset = (clock->read(clock) - clock->cycle_last) & clock->mask; | |
8524070b | 744 | #endif |
23ce7211 | 745 | timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift; |
8524070b | 746 | |
a092ff0f JS |
747 | /* |
748 | * With NO_HZ we may have to accumulate many cycle_intervals | |
749 | * (think "ticks") worth of time at once. To do this efficiently, | |
750 | * we calculate the largest doubling multiple of cycle_intervals | |
751 | * that is smaller then the offset. We then accumulate that | |
752 | * chunk in one go, and then try to consume the next smaller | |
753 | * doubled multiple. | |
8524070b | 754 | */ |
a092ff0f JS |
755 | shift = ilog2(offset) - ilog2(timekeeper.cycle_interval); |
756 | shift = max(0, shift); | |
757 | /* Bound shift to one less then what overflows tick_length */ | |
758 | maxshift = (8*sizeof(tick_length) - (ilog2(tick_length)+1)) - 1; | |
759 | shift = min(shift, maxshift); | |
155ec602 | 760 | while (offset >= timekeeper.cycle_interval) { |
a092ff0f | 761 | offset = logarithmic_accumulation(offset, shift); |
830ec045 JS |
762 | if(offset < timekeeper.cycle_interval<<shift) |
763 | shift--; | |
8524070b JS |
764 | } |
765 | ||
766 | /* correct the clock when NTP error is too big */ | |
155ec602 | 767 | timekeeping_adjust(offset); |
8524070b | 768 | |
6c9bacb4 JS |
769 | /* |
770 | * Since in the loop above, we accumulate any amount of time | |
771 | * in xtime_nsec over a second into xtime.tv_sec, its possible for | |
772 | * xtime_nsec to be fairly small after the loop. Further, if we're | |
155ec602 | 773 | * slightly speeding the clocksource up in timekeeping_adjust(), |
6c9bacb4 JS |
774 | * its possible the required corrective factor to xtime_nsec could |
775 | * cause it to underflow. | |
776 | * | |
777 | * Now, we cannot simply roll the accumulated second back, since | |
778 | * the NTP subsystem has been notified via second_overflow. So | |
779 | * instead we push xtime_nsec forward by the amount we underflowed, | |
780 | * and add that amount into the error. | |
781 | * | |
782 | * We'll correct this error next time through this function, when | |
783 | * xtime_nsec is not as small. | |
784 | */ | |
155ec602 MS |
785 | if (unlikely((s64)timekeeper.xtime_nsec < 0)) { |
786 | s64 neg = -(s64)timekeeper.xtime_nsec; | |
787 | timekeeper.xtime_nsec = 0; | |
23ce7211 | 788 | timekeeper.ntp_error += neg << timekeeper.ntp_error_shift; |
6c9bacb4 JS |
789 | } |
790 | ||
6a867a39 JS |
791 | |
792 | /* | |
793 | * Store full nanoseconds into xtime after rounding it up and | |
5cd1c9c5 RZ |
794 | * add the remainder to the error difference. |
795 | */ | |
23ce7211 MS |
796 | xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1; |
797 | timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift; | |
798 | timekeeper.ntp_error += timekeeper.xtime_nsec << | |
799 | timekeeper.ntp_error_shift; | |
8524070b | 800 | |
6a867a39 JS |
801 | /* |
802 | * Finally, make sure that after the rounding | |
803 | * xtime.tv_nsec isn't larger then NSEC_PER_SEC | |
804 | */ | |
805 | if (unlikely(xtime.tv_nsec >= NSEC_PER_SEC)) { | |
806 | xtime.tv_nsec -= NSEC_PER_SEC; | |
807 | xtime.tv_sec++; | |
808 | second_overflow(); | |
809 | } | |
83f57a11 | 810 | |
8524070b | 811 | /* check to see if there is a new clocksource to use */ |
0696b711 | 812 | update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult); |
8524070b | 813 | } |
7c3f1a57 TJ |
814 | |
815 | /** | |
816 | * getboottime - Return the real time of system boot. | |
817 | * @ts: pointer to the timespec to be set | |
818 | * | |
819 | * Returns the time of day in a timespec. | |
820 | * | |
821 | * This is based on the wall_to_monotonic offset and the total suspend | |
822 | * time. Calls to settimeofday will affect the value returned (which | |
823 | * basically means that however wrong your real time clock is at boot time, | |
824 | * you get the right time here). | |
825 | */ | |
826 | void getboottime(struct timespec *ts) | |
827 | { | |
36d47481 HS |
828 | struct timespec boottime = { |
829 | .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec, | |
830 | .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec | |
831 | }; | |
d4f587c6 | 832 | |
d4f587c6 | 833 | set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec); |
7c3f1a57 | 834 | } |
c93d89f3 | 835 | EXPORT_SYMBOL_GPL(getboottime); |
7c3f1a57 TJ |
836 | |
837 | /** | |
838 | * monotonic_to_bootbased - Convert the monotonic time to boot based. | |
839 | * @ts: pointer to the timespec to be converted | |
840 | */ | |
841 | void monotonic_to_bootbased(struct timespec *ts) | |
842 | { | |
ce3bf7ab | 843 | *ts = timespec_add(*ts, total_sleep_time); |
7c3f1a57 | 844 | } |
c93d89f3 | 845 | EXPORT_SYMBOL_GPL(monotonic_to_bootbased); |
2c6b47de | 846 | |
17c38b74 JS |
847 | unsigned long get_seconds(void) |
848 | { | |
6a867a39 | 849 | return xtime.tv_sec; |
17c38b74 JS |
850 | } |
851 | EXPORT_SYMBOL(get_seconds); | |
852 | ||
da15cfda JS |
853 | struct timespec __current_kernel_time(void) |
854 | { | |
6a867a39 | 855 | return xtime; |
da15cfda | 856 | } |
17c38b74 | 857 | |
2c6b47de JS |
858 | struct timespec current_kernel_time(void) |
859 | { | |
860 | struct timespec now; | |
861 | unsigned long seq; | |
862 | ||
863 | do { | |
864 | seq = read_seqbegin(&xtime_lock); | |
83f57a11 | 865 | |
6a867a39 | 866 | now = xtime; |
2c6b47de JS |
867 | } while (read_seqretry(&xtime_lock, seq)); |
868 | ||
869 | return now; | |
870 | } | |
2c6b47de | 871 | EXPORT_SYMBOL(current_kernel_time); |
da15cfda JS |
872 | |
873 | struct timespec get_monotonic_coarse(void) | |
874 | { | |
875 | struct timespec now, mono; | |
876 | unsigned long seq; | |
877 | ||
878 | do { | |
879 | seq = read_seqbegin(&xtime_lock); | |
83f57a11 | 880 | |
6a867a39 | 881 | now = xtime; |
da15cfda JS |
882 | mono = wall_to_monotonic; |
883 | } while (read_seqretry(&xtime_lock, seq)); | |
884 | ||
885 | set_normalized_timespec(&now, now.tv_sec + mono.tv_sec, | |
886 | now.tv_nsec + mono.tv_nsec); | |
887 | return now; | |
888 | } |