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.c611
1 files changed, 453 insertions, 158 deletions
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index e8c77d9c633a..af4135f05825 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);
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;
21 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,47 +155,54 @@ __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;
51 167
52static struct timespec xtime_cache __attribute__ ((aligned (16))); 168/* must hold xtime_lock */
53void update_xtime_cache(u64 nsec) 169void timekeeping_leap_insert(int leapsecond)
54{ 170{
55 xtime_cache = xtime; 171 xtime.tv_sec += leapsecond;
56 timespec_add_ns(&xtime_cache, nsec); 172 wall_to_monotonic.tv_sec -= leapsecond;
173 update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult);
57} 174}
58 175
59struct clocksource *clock;
60
61
62#ifdef CONFIG_GENERIC_TIME 176#ifdef CONFIG_GENERIC_TIME
177
63/** 178/**
64 * clocksource_forward_now - update clock to the current time 179 * timekeeping_forward_now - update clock to the current time
65 * 180 *
66 * Forward the current clock to update its state since the last call to 181 * Forward the current clock to update its state since the last call to
67 * update_wall_time(). This is useful before significant clock changes, 182 * update_wall_time(). This is useful before significant clock changes,
68 * as it avoids having to deal with this time offset explicitly. 183 * as it avoids having to deal with this time offset explicitly.
69 */ 184 */
70static void clocksource_forward_now(void) 185static void timekeeping_forward_now(void)
71{ 186{
72 cycle_t cycle_now, cycle_delta; 187 cycle_t cycle_now, cycle_delta;
188 struct clocksource *clock;
73 s64 nsec; 189 s64 nsec;
74 190
75 cycle_now = clocksource_read(clock); 191 clock = timekeeper.clock;
192 cycle_now = clock->read(clock);
76 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; 193 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
77 clock->cycle_last = cycle_now; 194 clock->cycle_last = cycle_now;
78 195
79 nsec = cyc2ns(clock, cycle_delta); 196 nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
197 timekeeper.shift);
80 198
81 /* If arch requires, add in gettimeoffset() */ 199 /* If arch requires, add in gettimeoffset() */
82 nsec += arch_gettimeoffset(); 200 nsec += arch_gettimeoffset();
83 201
84 timespec_add_ns(&xtime, nsec); 202 timespec_add_ns(&xtime, nsec);
85 203
86 nsec = ((s64)cycle_delta * clock->mult_orig) >> clock->shift; 204 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
87 clock->raw_time.tv_nsec += nsec; 205 timespec_add_ns(&raw_time, nsec);
88} 206}
89 207
90/** 208/**
@@ -95,7 +213,6 @@ static void clocksource_forward_now(void)
95 */ 213 */
96void getnstimeofday(struct timespec *ts) 214void getnstimeofday(struct timespec *ts)
97{ 215{
98 cycle_t cycle_now, cycle_delta;
99 unsigned long seq; 216 unsigned long seq;
100 s64 nsecs; 217 s64 nsecs;
101 218
@@ -105,15 +222,7 @@ void getnstimeofday(struct timespec *ts)
105 seq = read_seqbegin(&xtime_lock); 222 seq = read_seqbegin(&xtime_lock);
106 223
107 *ts = xtime; 224 *ts = xtime;
108 225 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 226
118 /* If arch requires, add in gettimeoffset() */ 227 /* If arch requires, add in gettimeoffset() */
119 nsecs += arch_gettimeoffset(); 228 nsecs += arch_gettimeoffset();
@@ -125,6 +234,57 @@ void getnstimeofday(struct timespec *ts)
125 234
126EXPORT_SYMBOL(getnstimeofday); 235EXPORT_SYMBOL(getnstimeofday);
127 236
237ktime_t ktime_get(void)
238{
239 unsigned int seq;
240 s64 secs, nsecs;
241
242 WARN_ON(timekeeping_suspended);
243
244 do {
245 seq = read_seqbegin(&xtime_lock);
246 secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
247 nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
248 nsecs += timekeeping_get_ns();
249
250 } while (read_seqretry(&xtime_lock, seq));
251 /*
252 * Use ktime_set/ktime_add_ns to create a proper ktime on
253 * 32-bit architectures without CONFIG_KTIME_SCALAR.
254 */
255 return ktime_add_ns(ktime_set(secs, 0), nsecs);
256}
257EXPORT_SYMBOL_GPL(ktime_get);
258
259/**
260 * ktime_get_ts - get the monotonic clock in timespec format
261 * @ts: pointer to timespec variable
262 *
263 * The function calculates the monotonic clock from the realtime
264 * clock and the wall_to_monotonic offset and stores the result
265 * in normalized timespec format in the variable pointed to by @ts.
266 */
267void ktime_get_ts(struct timespec *ts)
268{
269 struct timespec tomono;
270 unsigned int seq;
271 s64 nsecs;
272
273 WARN_ON(timekeeping_suspended);
274
275 do {
276 seq = read_seqbegin(&xtime_lock);
277 *ts = xtime;
278 tomono = wall_to_monotonic;
279 nsecs = timekeeping_get_ns();
280
281 } while (read_seqretry(&xtime_lock, seq));
282
283 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
284 ts->tv_nsec + tomono.tv_nsec + nsecs);
285}
286EXPORT_SYMBOL_GPL(ktime_get_ts);
287
128/** 288/**
129 * do_gettimeofday - Returns the time of day in a timeval 289 * do_gettimeofday - Returns the time of day in a timeval
130 * @tv: pointer to the timeval to be set 290 * @tv: pointer to the timeval to be set
@@ -157,7 +317,7 @@ int do_settimeofday(struct timespec *tv)
157 317
158 write_seqlock_irqsave(&xtime_lock, flags); 318 write_seqlock_irqsave(&xtime_lock, flags);
159 319
160 clocksource_forward_now(); 320 timekeeping_forward_now();
161 321
162 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec; 322 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
163 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec; 323 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
@@ -165,12 +325,10 @@ int do_settimeofday(struct timespec *tv)
165 325
166 xtime = *tv; 326 xtime = *tv;
167 327
168 update_xtime_cache(0); 328 timekeeper.ntp_error = 0;
169
170 clock->error = 0;
171 ntp_clear(); 329 ntp_clear();
172 330
173 update_vsyscall(&xtime, clock); 331 update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult);
174 332
175 write_sequnlock_irqrestore(&xtime_lock, flags); 333 write_sequnlock_irqrestore(&xtime_lock, flags);
176 334
@@ -187,44 +345,97 @@ EXPORT_SYMBOL(do_settimeofday);
187 * 345 *
188 * Accumulates current time interval and initializes new clocksource 346 * Accumulates current time interval and initializes new clocksource
189 */ 347 */
190static void change_clocksource(void) 348static int change_clocksource(void *data)
191{ 349{
192 struct clocksource *new, *old; 350 struct clocksource *new, *old;
193 351
194 new = clocksource_get_next(); 352 new = (struct clocksource *) data;
353
354 timekeeping_forward_now();
355 if (!new->enable || new->enable(new) == 0) {
356 old = timekeeper.clock;
357 timekeeper_setup_internals(new);
358 if (old->disable)
359 old->disable(old);
360 }
361 return 0;
362}
195 363
196 if (clock == new) 364/**
365 * timekeeping_notify - Install a new clock source
366 * @clock: pointer to the clock source
367 *
368 * This function is called from clocksource.c after a new, better clock
369 * source has been registered. The caller holds the clocksource_mutex.
370 */
371void timekeeping_notify(struct clocksource *clock)
372{
373 if (timekeeper.clock == clock)
197 return; 374 return;
375 stop_machine(change_clocksource, clock, NULL);
376 tick_clock_notify();
377}
198 378
199 clocksource_forward_now(); 379#else /* GENERIC_TIME */
200 380
201 if (clocksource_enable(new)) 381static inline void timekeeping_forward_now(void) { }
202 return; 382
383/**
384 * ktime_get - get the monotonic time in ktime_t format
385 *
386 * returns the time in ktime_t format
387 */
388ktime_t ktime_get(void)
389{
390 struct timespec now;
203 391
204 new->raw_time = clock->raw_time; 392 ktime_get_ts(&now);
205 old = clock;
206 clock = new;
207 clocksource_disable(old);
208 393
209 clock->cycle_last = 0; 394 return timespec_to_ktime(now);
210 clock->cycle_last = clocksource_read(clock); 395}
211 clock->error = 0; 396EXPORT_SYMBOL_GPL(ktime_get);
212 clock->xtime_nsec = 0;
213 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
214 397
215 tick_clock_notify(); 398/**
399 * ktime_get_ts - get the monotonic clock in timespec format
400 * @ts: pointer to timespec variable
401 *
402 * The function calculates the monotonic clock from the realtime
403 * clock and the wall_to_monotonic offset and stores the result
404 * in normalized timespec format in the variable pointed to by @ts.
405 */
406void ktime_get_ts(struct timespec *ts)
407{
408 struct timespec tomono;
409 unsigned long seq;
216 410
217 /* 411 do {
218 * We're holding xtime lock and waking up klogd would deadlock 412 seq = read_seqbegin(&xtime_lock);
219 * us on enqueue. So no printing! 413 getnstimeofday(ts);
220 printk(KERN_INFO "Time: %s clocksource has been installed.\n", 414 tomono = wall_to_monotonic;
221 clock->name); 415
222 */ 416 } while (read_seqretry(&xtime_lock, seq));
417
418 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
419 ts->tv_nsec + tomono.tv_nsec);
223} 420}
224#else 421EXPORT_SYMBOL_GPL(ktime_get_ts);
225static inline void clocksource_forward_now(void) { } 422
226static inline void change_clocksource(void) { } 423#endif /* !GENERIC_TIME */
227#endif 424
425/**
426 * ktime_get_real - get the real (wall-) time in ktime_t format
427 *
428 * returns the time in ktime_t format
429 */
430ktime_t ktime_get_real(void)
431{
432 struct timespec now;
433
434 getnstimeofday(&now);
435
436 return timespec_to_ktime(now);
437}
438EXPORT_SYMBOL_GPL(ktime_get_real);
228 439
229/** 440/**
230 * getrawmonotonic - Returns the raw monotonic time in a timespec 441 * getrawmonotonic - Returns the raw monotonic time in a timespec
@@ -236,21 +447,11 @@ void getrawmonotonic(struct timespec *ts)
236{ 447{
237 unsigned long seq; 448 unsigned long seq;
238 s64 nsecs; 449 s64 nsecs;
239 cycle_t cycle_now, cycle_delta;
240 450
241 do { 451 do {
242 seq = read_seqbegin(&xtime_lock); 452 seq = read_seqbegin(&xtime_lock);
243 453 nsecs = timekeeping_get_ns_raw();
244 /* read clocksource: */ 454 *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 455
255 } while (read_seqretry(&xtime_lock, seq)); 456 } while (read_seqretry(&xtime_lock, seq));
256 457
@@ -270,7 +471,7 @@ int timekeeping_valid_for_hres(void)
270 do { 471 do {
271 seq = read_seqbegin(&xtime_lock); 472 seq = read_seqbegin(&xtime_lock);
272 473
273 ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; 474 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
274 475
275 } while (read_seqretry(&xtime_lock, seq)); 476 } while (read_seqretry(&xtime_lock, seq));
276 477
@@ -278,17 +479,44 @@ int timekeeping_valid_for_hres(void)
278} 479}
279 480
280/** 481/**
281 * read_persistent_clock - Return time in seconds from the persistent clock. 482 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
483 *
484 * Caller must observe xtime_lock via read_seqbegin/read_seqretry to
485 * ensure that the clocksource does not change!
486 */
487u64 timekeeping_max_deferment(void)
488{
489 return timekeeper.clock->max_idle_ns;
490}
491
492/**
493 * read_persistent_clock - Return time from the persistent clock.
282 * 494 *
283 * Weak dummy function for arches that do not yet support it. 495 * Weak dummy function for arches that do not yet support it.
284 * Returns seconds from epoch using the battery backed persistent clock. 496 * Reads the time from the battery backed persistent clock.
285 * Returns zero if unsupported. 497 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
286 * 498 *
287 * XXX - Do be sure to remove it once all arches implement it. 499 * XXX - Do be sure to remove it once all arches implement it.
288 */ 500 */
289unsigned long __attribute__((weak)) read_persistent_clock(void) 501void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
290{ 502{
291 return 0; 503 ts->tv_sec = 0;
504 ts->tv_nsec = 0;
505}
506
507/**
508 * read_boot_clock - Return time of the system start.
509 *
510 * Weak dummy function for arches that do not yet support it.
511 * Function to read the exact time the system has been started.
512 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
513 *
514 * XXX - Do be sure to remove it once all arches implement it.
515 */
516void __attribute__((weak)) read_boot_clock(struct timespec *ts)
517{
518 ts->tv_sec = 0;
519 ts->tv_nsec = 0;
292} 520}
293 521
294/* 522/*
@@ -296,29 +524,39 @@ unsigned long __attribute__((weak)) read_persistent_clock(void)
296 */ 524 */
297void __init timekeeping_init(void) 525void __init timekeeping_init(void)
298{ 526{
527 struct clocksource *clock;
299 unsigned long flags; 528 unsigned long flags;
300 unsigned long sec = read_persistent_clock(); 529 struct timespec now, boot;
530
531 read_persistent_clock(&now);
532 read_boot_clock(&boot);
301 533
302 write_seqlock_irqsave(&xtime_lock, flags); 534 write_seqlock_irqsave(&xtime_lock, flags);
303 535
304 ntp_init(); 536 ntp_init();
305 537
306 clock = clocksource_get_next(); 538 clock = clocksource_default_clock();
307 clocksource_enable(clock); 539 if (clock->enable)
308 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH); 540 clock->enable(clock);
309 clock->cycle_last = clocksource_read(clock); 541 timekeeper_setup_internals(clock);
310 542
311 xtime.tv_sec = sec; 543 xtime.tv_sec = now.tv_sec;
312 xtime.tv_nsec = 0; 544 xtime.tv_nsec = now.tv_nsec;
545 raw_time.tv_sec = 0;
546 raw_time.tv_nsec = 0;
547 if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
548 boot.tv_sec = xtime.tv_sec;
549 boot.tv_nsec = xtime.tv_nsec;
550 }
313 set_normalized_timespec(&wall_to_monotonic, 551 set_normalized_timespec(&wall_to_monotonic,
314 -xtime.tv_sec, -xtime.tv_nsec); 552 -boot.tv_sec, -boot.tv_nsec);
315 update_xtime_cache(0); 553 total_sleep_time.tv_sec = 0;
316 total_sleep_time = 0; 554 total_sleep_time.tv_nsec = 0;
317 write_sequnlock_irqrestore(&xtime_lock, flags); 555 write_sequnlock_irqrestore(&xtime_lock, flags);
318} 556}
319 557
320/* time in seconds when suspend began */ 558/* time in seconds when suspend began */
321static unsigned long timekeeping_suspend_time; 559static struct timespec timekeeping_suspend_time;
322 560
323/** 561/**
324 * timekeeping_resume - Resumes the generic timekeeping subsystem. 562 * timekeeping_resume - Resumes the generic timekeeping subsystem.
@@ -331,24 +569,23 @@ static unsigned long timekeeping_suspend_time;
331static int timekeeping_resume(struct sys_device *dev) 569static int timekeeping_resume(struct sys_device *dev)
332{ 570{
333 unsigned long flags; 571 unsigned long flags;
334 unsigned long now = read_persistent_clock(); 572 struct timespec ts;
573
574 read_persistent_clock(&ts);
335 575
336 clocksource_resume(); 576 clocksource_resume();
337 577
338 write_seqlock_irqsave(&xtime_lock, flags); 578 write_seqlock_irqsave(&xtime_lock, flags);
339 579
340 if (now && (now > timekeeping_suspend_time)) { 580 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
341 unsigned long sleep_length = now - timekeeping_suspend_time; 581 ts = timespec_sub(ts, timekeeping_suspend_time);
342 582 xtime = timespec_add_safe(xtime, ts);
343 xtime.tv_sec += sleep_length; 583 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts);
344 wall_to_monotonic.tv_sec -= sleep_length; 584 total_sleep_time = timespec_add_safe(total_sleep_time, ts);
345 total_sleep_time += sleep_length;
346 } 585 }
347 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,58 @@ 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;
723}
724
725/**
726 * logarithmic_accumulation - shifted accumulation of cycles
727 *
728 * This functions accumulates a shifted interval of cycles into
729 * into a shifted interval nanoseconds. Allows for O(log) accumulation
730 * loop.
731 *
732 * Returns the unconsumed cycles.
733 */
734static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
735{
736 u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
737
738 /* If the offset is smaller then a shifted interval, do nothing */
739 if (offset < timekeeper.cycle_interval<<shift)
740 return offset;
741
742 /* Accumulate one shifted interval */
743 offset -= timekeeper.cycle_interval << shift;
744 timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift;
745
746 timekeeper.xtime_nsec += timekeeper.xtime_interval << shift;
747 while (timekeeper.xtime_nsec >= nsecps) {
748 timekeeper.xtime_nsec -= nsecps;
749 xtime.tv_sec++;
750 second_overflow();
751 }
752
753 /* Accumulate into raw time */
754 raw_time.tv_nsec += timekeeper.raw_interval << shift;;
755 while (raw_time.tv_nsec >= NSEC_PER_SEC) {
756 raw_time.tv_nsec -= NSEC_PER_SEC;
757 raw_time.tv_sec++;
758 }
759
760 /* Accumulate error between NTP and clock interval */
761 timekeeper.ntp_error += tick_length << shift;
762 timekeeper.ntp_error -= timekeeper.xtime_interval <<
763 (timekeeper.ntp_error_shift + shift);
764
765 return offset;
486} 766}
487 767
488/** 768/**
@@ -492,53 +772,48 @@ static void clocksource_adjust(s64 offset)
492 */ 772 */
493void update_wall_time(void) 773void update_wall_time(void)
494{ 774{
775 struct clocksource *clock;
495 cycle_t offset; 776 cycle_t offset;
777 int shift = 0, maxshift;
496 778
497 /* Make sure we're fully resumed: */ 779 /* Make sure we're fully resumed: */
498 if (unlikely(timekeeping_suspended)) 780 if (unlikely(timekeeping_suspended))
499 return; 781 return;
500 782
783 clock = timekeeper.clock;
501#ifdef CONFIG_GENERIC_TIME 784#ifdef CONFIG_GENERIC_TIME
502 offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask; 785 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
503#else 786#else
504 offset = clock->cycle_interval; 787 offset = timekeeper.cycle_interval;
505#endif 788#endif
506 clock->xtime_nsec = (s64)xtime.tv_nsec << clock->shift; 789 timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
507 790
508 /* normally this loop will run just once, however in the 791 /*
509 * case of lost or late ticks, it will accumulate correctly. 792 * With NO_HZ we may have to accumulate many cycle_intervals
793 * (think "ticks") worth of time at once. To do this efficiently,
794 * we calculate the largest doubling multiple of cycle_intervals
795 * that is smaller then the offset. We then accumulate that
796 * chunk in one go, and then try to consume the next smaller
797 * doubled multiple.
510 */ 798 */
511 while (offset >= clock->cycle_interval) { 799 shift = ilog2(offset) - ilog2(timekeeper.cycle_interval);
512 /* accumulate one interval */ 800 shift = max(0, shift);
513 offset -= clock->cycle_interval; 801 /* Bound shift to one less then what overflows tick_length */
514 clock->cycle_last += clock->cycle_interval; 802 maxshift = (8*sizeof(tick_length) - (ilog2(tick_length)+1)) - 1;
515 803 shift = min(shift, maxshift);
516 clock->xtime_nsec += clock->xtime_interval; 804 while (offset >= timekeeper.cycle_interval) {
517 if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) { 805 offset = logarithmic_accumulation(offset, shift);
518 clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift; 806 shift--;
519 xtime.tv_sec++;
520 second_overflow();
521 }
522
523 clock->raw_time.tv_nsec += clock->raw_interval;
524 if (clock->raw_time.tv_nsec >= NSEC_PER_SEC) {
525 clock->raw_time.tv_nsec -= NSEC_PER_SEC;
526 clock->raw_time.tv_sec++;
527 }
528
529 /* accumulate error between NTP and clock interval */
530 clock->error += tick_length;
531 clock->error -= clock->xtime_interval << (NTP_SCALE_SHIFT - clock->shift);
532 } 807 }
533 808
534 /* correct the clock when NTP error is too big */ 809 /* correct the clock when NTP error is too big */
535 clocksource_adjust(offset); 810 timekeeping_adjust(offset);
536 811
537 /* 812 /*
538 * Since in the loop above, we accumulate any amount of time 813 * 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 814 * 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 815 * xtime_nsec to be fairly small after the loop. Further, if we're
541 * slightly speeding the clocksource up in clocksource_adjust(), 816 * slightly speeding the clocksource up in timekeeping_adjust(),
542 * its possible the required corrective factor to xtime_nsec could 817 * its possible the required corrective factor to xtime_nsec could
543 * cause it to underflow. 818 * cause it to underflow.
544 * 819 *
@@ -550,24 +825,22 @@ void update_wall_time(void)
550 * We'll correct this error next time through this function, when 825 * We'll correct this error next time through this function, when
551 * xtime_nsec is not as small. 826 * xtime_nsec is not as small.
552 */ 827 */
553 if (unlikely((s64)clock->xtime_nsec < 0)) { 828 if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
554 s64 neg = -(s64)clock->xtime_nsec; 829 s64 neg = -(s64)timekeeper.xtime_nsec;
555 clock->xtime_nsec = 0; 830 timekeeper.xtime_nsec = 0;
556 clock->error += neg << (NTP_SCALE_SHIFT - clock->shift); 831 timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
557 } 832 }
558 833
559 /* store full nanoseconds into xtime after rounding it up and 834 /* store full nanoseconds into xtime after rounding it up and
560 * add the remainder to the error difference. 835 * add the remainder to the error difference.
561 */ 836 */
562 xtime.tv_nsec = ((s64)clock->xtime_nsec >> clock->shift) + 1; 837 xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
563 clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift; 838 timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
564 clock->error += clock->xtime_nsec << (NTP_SCALE_SHIFT - clock->shift); 839 timekeeper.ntp_error += timekeeper.xtime_nsec <<
565 840 timekeeper.ntp_error_shift;
566 update_xtime_cache(cyc2ns(clock, offset));
567 841
568 /* check to see if there is a new clocksource to use */ 842 /* check to see if there is a new clocksource to use */
569 change_clocksource(); 843 update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult);
570 update_vsyscall(&xtime, clock);
571} 844}
572 845
573/** 846/**
@@ -583,9 +856,12 @@ void update_wall_time(void)
583 */ 856 */
584void getboottime(struct timespec *ts) 857void getboottime(struct timespec *ts)
585{ 858{
586 set_normalized_timespec(ts, 859 struct timespec boottime = {
587 - (wall_to_monotonic.tv_sec + total_sleep_time), 860 .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec,
588 - wall_to_monotonic.tv_nsec); 861 .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec
862 };
863
864 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
589} 865}
590 866
591/** 867/**
@@ -594,15 +870,19 @@ void getboottime(struct timespec *ts)
594 */ 870 */
595void monotonic_to_bootbased(struct timespec *ts) 871void monotonic_to_bootbased(struct timespec *ts)
596{ 872{
597 ts->tv_sec += total_sleep_time; 873 *ts = timespec_add_safe(*ts, total_sleep_time);
598} 874}
599 875
600unsigned long get_seconds(void) 876unsigned long get_seconds(void)
601{ 877{
602 return xtime_cache.tv_sec; 878 return xtime.tv_sec;
603} 879}
604EXPORT_SYMBOL(get_seconds); 880EXPORT_SYMBOL(get_seconds);
605 881
882struct timespec __current_kernel_time(void)
883{
884 return xtime;
885}
606 886
607struct timespec current_kernel_time(void) 887struct timespec current_kernel_time(void)
608{ 888{
@@ -611,10 +891,25 @@ struct timespec current_kernel_time(void)
611 891
612 do { 892 do {
613 seq = read_seqbegin(&xtime_lock); 893 seq = read_seqbegin(&xtime_lock);
614 894 now = xtime;
615 now = xtime_cache;
616 } while (read_seqretry(&xtime_lock, seq)); 895 } while (read_seqretry(&xtime_lock, seq));
617 896
618 return now; 897 return now;
619} 898}
620EXPORT_SYMBOL(current_kernel_time); 899EXPORT_SYMBOL(current_kernel_time);
900
901struct timespec get_monotonic_coarse(void)
902{
903 struct timespec now, mono;
904 unsigned long seq;
905
906 do {
907 seq = read_seqbegin(&xtime_lock);
908 now = xtime;
909 mono = wall_to_monotonic;
910 } while (read_seqretry(&xtime_lock, seq));
911
912 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
913 now.tv_nsec + mono.tv_nsec);
914 return now;
915}