aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/time/timekeeping.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/time/timekeeping.c')
-rw-r--r--kernel/time/timekeeping.c536
1 files changed, 402 insertions, 134 deletions
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index e8c77d9c633a..c3a4e2907eaa 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -13,12 +13,123 @@
13#include <linux/percpu.h> 13#include <linux/percpu.h>
14#include <linux/init.h> 14#include <linux/init.h>
15#include <linux/mm.h> 15#include <linux/mm.h>
16#include <linux/sched.h>
16#include <linux/sysdev.h> 17#include <linux/sysdev.h>
17#include <linux/clocksource.h> 18#include <linux/clocksource.h>
18#include <linux/jiffies.h> 19#include <linux/jiffies.h>
19#include <linux/time.h> 20#include <linux/time.h>
20#include <linux/tick.h> 21#include <linux/tick.h>
22#include <linux/stop_machine.h>
23
24/* Structure holding internal timekeeping values. */
25struct timekeeper {
26 /* Current clocksource used for timekeeping. */
27 struct clocksource *clock;
28 /* The shift value of the current clocksource. */
29 int shift;
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;
43 /* Shift conversion between clock shifted nano seconds and
44 * ntp shifted nano seconds. */
45 int ntp_error_shift;
46 /* NTP adjusted clock multiplier */
47 u32 mult;
48};
49
50struct 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 */
62static 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);
21 69
70 /* Do the ns -> cycle conversion first, using original mult */
71 tmp = NTP_INTERVAL_LENGTH;
72 tmp <<= clock->shift;
73 tmp += clock->mult/2;
74 do_div(tmp, clock->mult);
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 =
84 ((u64) interval * clock->mult) >> clock->shift;
85
86 timekeeper.xtime_nsec = 0;
87 timekeeper.shift = clock->shift;
88
89 timekeeper.ntp_error = 0;
90 timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
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;
98}
99
100/* Timekeeper helper functions. */
101static 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
118static 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}
22 133
23/* 134/*
24 * This read-write spinlock protects us from races in SMP while 135 * This read-write spinlock protects us from races in SMP while
@@ -44,7 +155,12 @@ __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
44 */ 155 */
45struct timespec xtime __attribute__ ((aligned (16))); 156struct timespec xtime __attribute__ ((aligned (16)));
46struct timespec wall_to_monotonic __attribute__ ((aligned (16))); 157struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
47static unsigned long total_sleep_time; /* seconds */ 158static struct timespec total_sleep_time;
159
160/*
161 * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
162 */
163struct timespec raw_time;
48 164
49/* flag for if timekeeping is suspended */ 165/* flag for if timekeeping is suspended */
50int __read_mostly timekeeping_suspended; 166int __read_mostly timekeeping_suspended;
@@ -56,35 +172,44 @@ void update_xtime_cache(u64 nsec)
56 timespec_add_ns(&xtime_cache, nsec); 172 timespec_add_ns(&xtime_cache, nsec);
57} 173}
58 174
59struct clocksource *clock; 175/* must hold xtime_lock */
60 176void timekeeping_leap_insert(int leapsecond)
177{
178 xtime.tv_sec += leapsecond;
179 wall_to_monotonic.tv_sec -= leapsecond;
180 update_vsyscall(&xtime, timekeeper.clock);
181}
61 182
62#ifdef CONFIG_GENERIC_TIME 183#ifdef CONFIG_GENERIC_TIME
184
63/** 185/**
64 * clocksource_forward_now - update clock to the current time 186 * timekeeping_forward_now - update clock to the current time
65 * 187 *
66 * Forward the current clock to update its state since the last call to 188 * Forward the current clock to update its state since the last call to
67 * update_wall_time(). This is useful before significant clock changes, 189 * update_wall_time(). This is useful before significant clock changes,
68 * as it avoids having to deal with this time offset explicitly. 190 * as it avoids having to deal with this time offset explicitly.
69 */ 191 */
70static void clocksource_forward_now(void) 192static void timekeeping_forward_now(void)
71{ 193{
72 cycle_t cycle_now, cycle_delta; 194 cycle_t cycle_now, cycle_delta;
195 struct clocksource *clock;
73 s64 nsec; 196 s64 nsec;
74 197
75 cycle_now = clocksource_read(clock); 198 clock = timekeeper.clock;
199 cycle_now = clock->read(clock);
76 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; 200 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
77 clock->cycle_last = cycle_now; 201 clock->cycle_last = cycle_now;
78 202
79 nsec = cyc2ns(clock, cycle_delta); 203 nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
204 timekeeper.shift);
80 205
81 /* If arch requires, add in gettimeoffset() */ 206 /* If arch requires, add in gettimeoffset() */
82 nsec += arch_gettimeoffset(); 207 nsec += arch_gettimeoffset();
83 208
84 timespec_add_ns(&xtime, nsec); 209 timespec_add_ns(&xtime, nsec);
85 210
86 nsec = ((s64)cycle_delta * clock->mult_orig) >> clock->shift; 211 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
87 clock->raw_time.tv_nsec += nsec; 212 timespec_add_ns(&raw_time, nsec);
88} 213}
89 214
90/** 215/**
@@ -95,7 +220,6 @@ static void clocksource_forward_now(void)
95 */ 220 */
96void getnstimeofday(struct timespec *ts) 221void getnstimeofday(struct timespec *ts)
97{ 222{
98 cycle_t cycle_now, cycle_delta;
99 unsigned long seq; 223 unsigned long seq;
100 s64 nsecs; 224 s64 nsecs;
101 225
@@ -105,15 +229,7 @@ void getnstimeofday(struct timespec *ts)
105 seq = read_seqbegin(&xtime_lock); 229 seq = read_seqbegin(&xtime_lock);
106 230
107 *ts = xtime; 231 *ts = xtime;
108 232 nsecs = timekeeping_get_ns();
109 /* read clocksource: */
110 cycle_now = clocksource_read(clock);
111
112 /* calculate the delta since the last update_wall_time: */
113 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
114
115 /* convert to nanoseconds: */
116 nsecs = cyc2ns(clock, cycle_delta);
117 233
118 /* If arch requires, add in gettimeoffset() */ 234 /* If arch requires, add in gettimeoffset() */
119 nsecs += arch_gettimeoffset(); 235 nsecs += arch_gettimeoffset();
@@ -125,6 +241,57 @@ void getnstimeofday(struct timespec *ts)
125 241
126EXPORT_SYMBOL(getnstimeofday); 242EXPORT_SYMBOL(getnstimeofday);
127 243
244ktime_t ktime_get(void)
245{
246 unsigned int seq;
247 s64 secs, nsecs;
248
249 WARN_ON(timekeeping_suspended);
250
251 do {
252 seq = read_seqbegin(&xtime_lock);
253 secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
254 nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
255 nsecs += timekeeping_get_ns();
256
257 } while (read_seqretry(&xtime_lock, seq));
258 /*
259 * Use ktime_set/ktime_add_ns to create a proper ktime on
260 * 32-bit architectures without CONFIG_KTIME_SCALAR.
261 */
262 return ktime_add_ns(ktime_set(secs, 0), nsecs);
263}
264EXPORT_SYMBOL_GPL(ktime_get);
265
266/**
267 * ktime_get_ts - get the monotonic clock in timespec format
268 * @ts: pointer to timespec variable
269 *
270 * The function calculates the monotonic clock from the realtime
271 * clock and the wall_to_monotonic offset and stores the result
272 * in normalized timespec format in the variable pointed to by @ts.
273 */
274void ktime_get_ts(struct timespec *ts)
275{
276 struct timespec tomono;
277 unsigned int seq;
278 s64 nsecs;
279
280 WARN_ON(timekeeping_suspended);
281
282 do {
283 seq = read_seqbegin(&xtime_lock);
284 *ts = xtime;
285 tomono = wall_to_monotonic;
286 nsecs = timekeeping_get_ns();
287
288 } while (read_seqretry(&xtime_lock, seq));
289
290 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
291 ts->tv_nsec + tomono.tv_nsec + nsecs);
292}
293EXPORT_SYMBOL_GPL(ktime_get_ts);
294
128/** 295/**
129 * do_gettimeofday - Returns the time of day in a timeval 296 * do_gettimeofday - Returns the time of day in a timeval
130 * @tv: pointer to the timeval to be set 297 * @tv: pointer to the timeval to be set
@@ -157,7 +324,7 @@ int do_settimeofday(struct timespec *tv)
157 324
158 write_seqlock_irqsave(&xtime_lock, flags); 325 write_seqlock_irqsave(&xtime_lock, flags);
159 326
160 clocksource_forward_now(); 327 timekeeping_forward_now();
161 328
162 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec; 329 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
163 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec; 330 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
@@ -167,10 +334,10 @@ int do_settimeofday(struct timespec *tv)
167 334
168 update_xtime_cache(0); 335 update_xtime_cache(0);
169 336
170 clock->error = 0; 337 timekeeper.ntp_error = 0;
171 ntp_clear(); 338 ntp_clear();
172 339
173 update_vsyscall(&xtime, clock); 340 update_vsyscall(&xtime, timekeeper.clock);
174 341
175 write_sequnlock_irqrestore(&xtime_lock, flags); 342 write_sequnlock_irqrestore(&xtime_lock, flags);
176 343
@@ -187,44 +354,97 @@ EXPORT_SYMBOL(do_settimeofday);
187 * 354 *
188 * Accumulates current time interval and initializes new clocksource 355 * Accumulates current time interval and initializes new clocksource
189 */ 356 */
190static void change_clocksource(void) 357static int change_clocksource(void *data)
191{ 358{
192 struct clocksource *new, *old; 359 struct clocksource *new, *old;
193 360
194 new = clocksource_get_next(); 361 new = (struct clocksource *) data;
362
363 timekeeping_forward_now();
364 if (!new->enable || new->enable(new) == 0) {
365 old = timekeeper.clock;
366 timekeeper_setup_internals(new);
367 if (old->disable)
368 old->disable(old);
369 }
370 return 0;
371}
195 372
196 if (clock == new) 373/**
374 * timekeeping_notify - Install a new clock source
375 * @clock: pointer to the clock source
376 *
377 * This function is called from clocksource.c after a new, better clock
378 * source has been registered. The caller holds the clocksource_mutex.
379 */
380void timekeeping_notify(struct clocksource *clock)
381{
382 if (timekeeper.clock == clock)
197 return; 383 return;
384 stop_machine(change_clocksource, clock, NULL);
385 tick_clock_notify();
386}
198 387
199 clocksource_forward_now(); 388#else /* GENERIC_TIME */
200 389
201 if (clocksource_enable(new)) 390static inline void timekeeping_forward_now(void) { }
202 return;
203 391
204 new->raw_time = clock->raw_time; 392/**
205 old = clock; 393 * ktime_get - get the monotonic time in ktime_t format
206 clock = new; 394 *
207 clocksource_disable(old); 395 * returns the time in ktime_t format
396 */
397ktime_t ktime_get(void)
398{
399 struct timespec now;
208 400
209 clock->cycle_last = 0; 401 ktime_get_ts(&now);
210 clock->cycle_last = clocksource_read(clock);
211 clock->error = 0;
212 clock->xtime_nsec = 0;
213 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
214 402
215 tick_clock_notify(); 403 return timespec_to_ktime(now);
404}
405EXPORT_SYMBOL_GPL(ktime_get);
216 406
217 /* 407/**
218 * We're holding xtime lock and waking up klogd would deadlock 408 * ktime_get_ts - get the monotonic clock in timespec format
219 * us on enqueue. So no printing! 409 * @ts: pointer to timespec variable
220 printk(KERN_INFO "Time: %s clocksource has been installed.\n", 410 *
221 clock->name); 411 * The function calculates the monotonic clock from the realtime
222 */ 412 * clock and the wall_to_monotonic offset and stores the result
413 * in normalized timespec format in the variable pointed to by @ts.
414 */
415void ktime_get_ts(struct timespec *ts)
416{
417 struct timespec tomono;
418 unsigned long seq;
419
420 do {
421 seq = read_seqbegin(&xtime_lock);
422 getnstimeofday(ts);
423 tomono = wall_to_monotonic;
424
425 } while (read_seqretry(&xtime_lock, seq));
426
427 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
428 ts->tv_nsec + tomono.tv_nsec);
223} 429}
224#else 430EXPORT_SYMBOL_GPL(ktime_get_ts);
225static inline void clocksource_forward_now(void) { } 431
226static inline void change_clocksource(void) { } 432#endif /* !GENERIC_TIME */
227#endif 433
434/**
435 * ktime_get_real - get the real (wall-) time in ktime_t format
436 *
437 * returns the time in ktime_t format
438 */
439ktime_t ktime_get_real(void)
440{
441 struct timespec now;
442
443 getnstimeofday(&now);
444
445 return timespec_to_ktime(now);
446}
447EXPORT_SYMBOL_GPL(ktime_get_real);
228 448
229/** 449/**
230 * getrawmonotonic - Returns the raw monotonic time in a timespec 450 * getrawmonotonic - Returns the raw monotonic time in a timespec
@@ -236,21 +456,11 @@ void getrawmonotonic(struct timespec *ts)
236{ 456{
237 unsigned long seq; 457 unsigned long seq;
238 s64 nsecs; 458 s64 nsecs;
239 cycle_t cycle_now, cycle_delta;
240 459
241 do { 460 do {
242 seq = read_seqbegin(&xtime_lock); 461 seq = read_seqbegin(&xtime_lock);
243 462 nsecs = timekeeping_get_ns_raw();
244 /* read clocksource: */ 463 *ts = raw_time;
245 cycle_now = clocksource_read(clock);
246
247 /* calculate the delta since the last update_wall_time: */
248 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
249
250 /* convert to nanoseconds: */
251 nsecs = ((s64)cycle_delta * clock->mult_orig) >> clock->shift;
252
253 *ts = clock->raw_time;
254 464
255 } while (read_seqretry(&xtime_lock, seq)); 465 } while (read_seqretry(&xtime_lock, seq));
256 466
@@ -270,7 +480,7 @@ int timekeeping_valid_for_hres(void)
270 do { 480 do {
271 seq = read_seqbegin(&xtime_lock); 481 seq = read_seqbegin(&xtime_lock);
272 482
273 ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; 483 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
274 484
275 } while (read_seqretry(&xtime_lock, seq)); 485 } while (read_seqretry(&xtime_lock, seq));
276 486
@@ -278,17 +488,33 @@ int timekeeping_valid_for_hres(void)
278} 488}
279 489
280/** 490/**
281 * read_persistent_clock - Return time in seconds from the persistent clock. 491 * read_persistent_clock - Return time from the persistent clock.
282 * 492 *
283 * Weak dummy function for arches that do not yet support it. 493 * Weak dummy function for arches that do not yet support it.
284 * Returns seconds from epoch using the battery backed persistent clock. 494 * Reads the time from the battery backed persistent clock.
285 * Returns zero if unsupported. 495 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
286 * 496 *
287 * XXX - Do be sure to remove it once all arches implement it. 497 * XXX - Do be sure to remove it once all arches implement it.
288 */ 498 */
289unsigned long __attribute__((weak)) read_persistent_clock(void) 499void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
290{ 500{
291 return 0; 501 ts->tv_sec = 0;
502 ts->tv_nsec = 0;
503}
504
505/**
506 * read_boot_clock - Return time of the system start.
507 *
508 * Weak dummy function for arches that do not yet support it.
509 * Function to read the exact time the system has been started.
510 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
511 *
512 * XXX - Do be sure to remove it once all arches implement it.
513 */
514void __attribute__((weak)) read_boot_clock(struct timespec *ts)
515{
516 ts->tv_sec = 0;
517 ts->tv_nsec = 0;
292} 518}
293 519
294/* 520/*
@@ -296,29 +522,40 @@ unsigned long __attribute__((weak)) read_persistent_clock(void)
296 */ 522 */
297void __init timekeeping_init(void) 523void __init timekeeping_init(void)
298{ 524{
525 struct clocksource *clock;
299 unsigned long flags; 526 unsigned long flags;
300 unsigned long sec = read_persistent_clock(); 527 struct timespec now, boot;
528
529 read_persistent_clock(&now);
530 read_boot_clock(&boot);
301 531
302 write_seqlock_irqsave(&xtime_lock, flags); 532 write_seqlock_irqsave(&xtime_lock, flags);
303 533
304 ntp_init(); 534 ntp_init();
305 535
306 clock = clocksource_get_next(); 536 clock = clocksource_default_clock();
307 clocksource_enable(clock); 537 if (clock->enable)
308 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH); 538 clock->enable(clock);
309 clock->cycle_last = clocksource_read(clock); 539 timekeeper_setup_internals(clock);
310 540
311 xtime.tv_sec = sec; 541 xtime.tv_sec = now.tv_sec;
312 xtime.tv_nsec = 0; 542 xtime.tv_nsec = now.tv_nsec;
543 raw_time.tv_sec = 0;
544 raw_time.tv_nsec = 0;
545 if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
546 boot.tv_sec = xtime.tv_sec;
547 boot.tv_nsec = xtime.tv_nsec;
548 }
313 set_normalized_timespec(&wall_to_monotonic, 549 set_normalized_timespec(&wall_to_monotonic,
314 -xtime.tv_sec, -xtime.tv_nsec); 550 -boot.tv_sec, -boot.tv_nsec);
315 update_xtime_cache(0); 551 update_xtime_cache(0);
316 total_sleep_time = 0; 552 total_sleep_time.tv_sec = 0;
553 total_sleep_time.tv_nsec = 0;
317 write_sequnlock_irqrestore(&xtime_lock, flags); 554 write_sequnlock_irqrestore(&xtime_lock, flags);
318} 555}
319 556
320/* time in seconds when suspend began */ 557/* time in seconds when suspend began */
321static unsigned long timekeeping_suspend_time; 558static struct timespec timekeeping_suspend_time;
322 559
323/** 560/**
324 * timekeeping_resume - Resumes the generic timekeeping subsystem. 561 * timekeeping_resume - Resumes the generic timekeeping subsystem.
@@ -331,24 +568,24 @@ static unsigned long timekeeping_suspend_time;
331static int timekeeping_resume(struct sys_device *dev) 568static int timekeeping_resume(struct sys_device *dev)
332{ 569{
333 unsigned long flags; 570 unsigned long flags;
334 unsigned long now = read_persistent_clock(); 571 struct timespec ts;
572
573 read_persistent_clock(&ts);
335 574
336 clocksource_resume(); 575 clocksource_resume();
337 576
338 write_seqlock_irqsave(&xtime_lock, flags); 577 write_seqlock_irqsave(&xtime_lock, flags);
339 578
340 if (now && (now > timekeeping_suspend_time)) { 579 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
341 unsigned long sleep_length = now - timekeeping_suspend_time; 580 ts = timespec_sub(ts, timekeeping_suspend_time);
342 581 xtime = timespec_add_safe(xtime, ts);
343 xtime.tv_sec += sleep_length; 582 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts);
344 wall_to_monotonic.tv_sec -= sleep_length; 583 total_sleep_time = timespec_add_safe(total_sleep_time, ts);
345 total_sleep_time += sleep_length;
346 } 584 }
347 update_xtime_cache(0); 585 update_xtime_cache(0);
348 /* re-base the last cycle value */ 586 /* re-base the last cycle value */
349 clock->cycle_last = 0; 587 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
350 clock->cycle_last = clocksource_read(clock); 588 timekeeper.ntp_error = 0;
351 clock->error = 0;
352 timekeeping_suspended = 0; 589 timekeeping_suspended = 0;
353 write_sequnlock_irqrestore(&xtime_lock, flags); 590 write_sequnlock_irqrestore(&xtime_lock, flags);
354 591
@@ -366,10 +603,10 @@ static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
366{ 603{
367 unsigned long flags; 604 unsigned long flags;
368 605
369 timekeeping_suspend_time = read_persistent_clock(); 606 read_persistent_clock(&timekeeping_suspend_time);
370 607
371 write_seqlock_irqsave(&xtime_lock, flags); 608 write_seqlock_irqsave(&xtime_lock, flags);
372 clocksource_forward_now(); 609 timekeeping_forward_now();
373 timekeeping_suspended = 1; 610 timekeeping_suspended = 1;
374 write_sequnlock_irqrestore(&xtime_lock, flags); 611 write_sequnlock_irqrestore(&xtime_lock, flags);
375 612
@@ -404,7 +641,7 @@ device_initcall(timekeeping_init_device);
404 * If the error is already larger, we look ahead even further 641 * If the error is already larger, we look ahead even further
405 * to compensate for late or lost adjustments. 642 * to compensate for late or lost adjustments.
406 */ 643 */
407static __always_inline int clocksource_bigadjust(s64 error, s64 *interval, 644static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
408 s64 *offset) 645 s64 *offset)
409{ 646{
410 s64 tick_error, i; 647 s64 tick_error, i;
@@ -420,7 +657,7 @@ static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
420 * here. This is tuned so that an error of about 1 msec is adjusted 657 * here. This is tuned so that an error of about 1 msec is adjusted
421 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks). 658 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
422 */ 659 */
423 error2 = clock->error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ); 660 error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
424 error2 = abs(error2); 661 error2 = abs(error2);
425 for (look_ahead = 0; error2 > 0; look_ahead++) 662 for (look_ahead = 0; error2 > 0; look_ahead++)
426 error2 >>= 2; 663 error2 >>= 2;
@@ -429,8 +666,8 @@ static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
429 * Now calculate the error in (1 << look_ahead) ticks, but first 666 * Now calculate the error in (1 << look_ahead) ticks, but first
430 * remove the single look ahead already included in the error. 667 * remove the single look ahead already included in the error.
431 */ 668 */
432 tick_error = tick_length >> (NTP_SCALE_SHIFT - clock->shift + 1); 669 tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
433 tick_error -= clock->xtime_interval >> 1; 670 tick_error -= timekeeper.xtime_interval >> 1;
434 error = ((error - tick_error) >> look_ahead) + tick_error; 671 error = ((error - tick_error) >> look_ahead) + tick_error;
435 672
436 /* Finally calculate the adjustment shift value. */ 673 /* Finally calculate the adjustment shift value. */
@@ -455,18 +692,18 @@ static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
455 * this is optimized for the most common adjustments of -1,0,1, 692 * this is optimized for the most common adjustments of -1,0,1,
456 * for other values we can do a bit more work. 693 * for other values we can do a bit more work.
457 */ 694 */
458static void clocksource_adjust(s64 offset) 695static void timekeeping_adjust(s64 offset)
459{ 696{
460 s64 error, interval = clock->cycle_interval; 697 s64 error, interval = timekeeper.cycle_interval;
461 int adj; 698 int adj;
462 699
463 error = clock->error >> (NTP_SCALE_SHIFT - clock->shift - 1); 700 error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
464 if (error > interval) { 701 if (error > interval) {
465 error >>= 2; 702 error >>= 2;
466 if (likely(error <= interval)) 703 if (likely(error <= interval))
467 adj = 1; 704 adj = 1;
468 else 705 else
469 adj = clocksource_bigadjust(error, &interval, &offset); 706 adj = timekeeping_bigadjust(error, &interval, &offset);
470 } else if (error < -interval) { 707 } else if (error < -interval) {
471 error >>= 2; 708 error >>= 2;
472 if (likely(error >= -interval)) { 709 if (likely(error >= -interval)) {
@@ -474,15 +711,15 @@ static void clocksource_adjust(s64 offset)
474 interval = -interval; 711 interval = -interval;
475 offset = -offset; 712 offset = -offset;
476 } else 713 } else
477 adj = clocksource_bigadjust(error, &interval, &offset); 714 adj = timekeeping_bigadjust(error, &interval, &offset);
478 } else 715 } else
479 return; 716 return;
480 717
481 clock->mult += adj; 718 timekeeper.mult += adj;
482 clock->xtime_interval += interval; 719 timekeeper.xtime_interval += interval;
483 clock->xtime_nsec -= offset; 720 timekeeper.xtime_nsec -= offset;
484 clock->error -= (interval - offset) << 721 timekeeper.ntp_error -= (interval - offset) <<
485 (NTP_SCALE_SHIFT - clock->shift); 722 timekeeper.ntp_error_shift;
486} 723}
487 724
488/** 725/**
@@ -492,53 +729,59 @@ static void clocksource_adjust(s64 offset)
492 */ 729 */
493void update_wall_time(void) 730void update_wall_time(void)
494{ 731{
732 struct clocksource *clock;
495 cycle_t offset; 733 cycle_t offset;
734 u64 nsecs;
496 735
497 /* Make sure we're fully resumed: */ 736 /* Make sure we're fully resumed: */
498 if (unlikely(timekeeping_suspended)) 737 if (unlikely(timekeeping_suspended))
499 return; 738 return;
500 739
740 clock = timekeeper.clock;
501#ifdef CONFIG_GENERIC_TIME 741#ifdef CONFIG_GENERIC_TIME
502 offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask; 742 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
503#else 743#else
504 offset = clock->cycle_interval; 744 offset = timekeeper.cycle_interval;
505#endif 745#endif
506 clock->xtime_nsec = (s64)xtime.tv_nsec << clock->shift; 746 timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
507 747
508 /* normally this loop will run just once, however in the 748 /* normally this loop will run just once, however in the
509 * case of lost or late ticks, it will accumulate correctly. 749 * case of lost or late ticks, it will accumulate correctly.
510 */ 750 */
511 while (offset >= clock->cycle_interval) { 751 while (offset >= timekeeper.cycle_interval) {
752 u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
753
512 /* accumulate one interval */ 754 /* accumulate one interval */
513 offset -= clock->cycle_interval; 755 offset -= timekeeper.cycle_interval;
514 clock->cycle_last += clock->cycle_interval; 756 clock->cycle_last += timekeeper.cycle_interval;
515 757
516 clock->xtime_nsec += clock->xtime_interval; 758 timekeeper.xtime_nsec += timekeeper.xtime_interval;
517 if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) { 759 if (timekeeper.xtime_nsec >= nsecps) {
518 clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift; 760 timekeeper.xtime_nsec -= nsecps;
519 xtime.tv_sec++; 761 xtime.tv_sec++;
520 second_overflow(); 762 second_overflow();
521 } 763 }
522 764
523 clock->raw_time.tv_nsec += clock->raw_interval; 765 raw_time.tv_nsec += timekeeper.raw_interval;
524 if (clock->raw_time.tv_nsec >= NSEC_PER_SEC) { 766 if (raw_time.tv_nsec >= NSEC_PER_SEC) {
525 clock->raw_time.tv_nsec -= NSEC_PER_SEC; 767 raw_time.tv_nsec -= NSEC_PER_SEC;
526 clock->raw_time.tv_sec++; 768 raw_time.tv_sec++;
527 } 769 }
528 770
529 /* accumulate error between NTP and clock interval */ 771 /* accumulate error between NTP and clock interval */
530 clock->error += tick_length; 772 timekeeper.ntp_error += tick_length;
531 clock->error -= clock->xtime_interval << (NTP_SCALE_SHIFT - clock->shift); 773 timekeeper.ntp_error -= timekeeper.xtime_interval <<
774 timekeeper.ntp_error_shift;
532 } 775 }
533 776
534 /* correct the clock when NTP error is too big */ 777 /* correct the clock when NTP error is too big */
535 clocksource_adjust(offset); 778 timekeeping_adjust(offset);
536 779
537 /* 780 /*
538 * Since in the loop above, we accumulate any amount of time 781 * Since in the loop above, we accumulate any amount of time
539 * in xtime_nsec over a second into xtime.tv_sec, its possible for 782 * in xtime_nsec over a second into xtime.tv_sec, its possible for
540 * xtime_nsec to be fairly small after the loop. Further, if we're 783 * xtime_nsec to be fairly small after the loop. Further, if we're
541 * slightly speeding the clocksource up in clocksource_adjust(), 784 * slightly speeding the clocksource up in timekeeping_adjust(),
542 * its possible the required corrective factor to xtime_nsec could 785 * its possible the required corrective factor to xtime_nsec could
543 * cause it to underflow. 786 * cause it to underflow.
544 * 787 *
@@ -550,24 +793,25 @@ void update_wall_time(void)
550 * We'll correct this error next time through this function, when 793 * We'll correct this error next time through this function, when
551 * xtime_nsec is not as small. 794 * xtime_nsec is not as small.
552 */ 795 */
553 if (unlikely((s64)clock->xtime_nsec < 0)) { 796 if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
554 s64 neg = -(s64)clock->xtime_nsec; 797 s64 neg = -(s64)timekeeper.xtime_nsec;
555 clock->xtime_nsec = 0; 798 timekeeper.xtime_nsec = 0;
556 clock->error += neg << (NTP_SCALE_SHIFT - clock->shift); 799 timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
557 } 800 }
558 801
559 /* store full nanoseconds into xtime after rounding it up and 802 /* store full nanoseconds into xtime after rounding it up and
560 * add the remainder to the error difference. 803 * add the remainder to the error difference.
561 */ 804 */
562 xtime.tv_nsec = ((s64)clock->xtime_nsec >> clock->shift) + 1; 805 xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
563 clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift; 806 timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
564 clock->error += clock->xtime_nsec << (NTP_SCALE_SHIFT - clock->shift); 807 timekeeper.ntp_error += timekeeper.xtime_nsec <<
808 timekeeper.ntp_error_shift;
565 809
566 update_xtime_cache(cyc2ns(clock, offset)); 810 nsecs = clocksource_cyc2ns(offset, timekeeper.mult, timekeeper.shift);
811 update_xtime_cache(nsecs);
567 812
568 /* check to see if there is a new clocksource to use */ 813 /* check to see if there is a new clocksource to use */
569 change_clocksource(); 814 update_vsyscall(&xtime, timekeeper.clock);
570 update_vsyscall(&xtime, clock);
571} 815}
572 816
573/** 817/**
@@ -583,9 +827,12 @@ void update_wall_time(void)
583 */ 827 */
584void getboottime(struct timespec *ts) 828void getboottime(struct timespec *ts)
585{ 829{
586 set_normalized_timespec(ts, 830 struct timespec boottime = {
587 - (wall_to_monotonic.tv_sec + total_sleep_time), 831 .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec,
588 - wall_to_monotonic.tv_nsec); 832 .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec
833 };
834
835 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
589} 836}
590 837
591/** 838/**
@@ -594,7 +841,7 @@ void getboottime(struct timespec *ts)
594 */ 841 */
595void monotonic_to_bootbased(struct timespec *ts) 842void monotonic_to_bootbased(struct timespec *ts)
596{ 843{
597 ts->tv_sec += total_sleep_time; 844 *ts = timespec_add_safe(*ts, total_sleep_time);
598} 845}
599 846
600unsigned long get_seconds(void) 847unsigned long get_seconds(void)
@@ -603,6 +850,10 @@ unsigned long get_seconds(void)
603} 850}
604EXPORT_SYMBOL(get_seconds); 851EXPORT_SYMBOL(get_seconds);
605 852
853struct timespec __current_kernel_time(void)
854{
855 return xtime_cache;
856}
606 857
607struct timespec current_kernel_time(void) 858struct timespec current_kernel_time(void)
608{ 859{
@@ -618,3 +869,20 @@ struct timespec current_kernel_time(void)
618 return now; 869 return now;
619} 870}
620EXPORT_SYMBOL(current_kernel_time); 871EXPORT_SYMBOL(current_kernel_time);
872
873struct timespec get_monotonic_coarse(void)
874{
875 struct timespec now, mono;
876 unsigned long seq;
877
878 do {
879 seq = read_seqbegin(&xtime_lock);
880
881 now = xtime_cache;
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}