blob: f1a21ce491e6ad63eab28b09b9bc630ded68d25b [file] [log] [blame]
john stultz85240702007-05-08 00:27:59 -07001/*
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>
16#include <linux/sysdev.h>
17#include <linux/clocksource.h>
18#include <linux/jiffies.h>
19#include <linux/time.h>
20#include <linux/tick.h>
Martin Schwidefsky75c51582009-08-14 15:47:30 +020021#include <linux/stop_machine.h>
john stultz85240702007-05-08 00:27:59 -070022
Martin Schwidefsky155ec602009-08-14 15:47:26 +020023/* Structure holding internal timekeeping values. */
24struct timekeeper {
25 /* Current clocksource used for timekeeping. */
26 struct clocksource *clock;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +020027 /* The shift value of the current clocksource. */
28 int shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020029
30 /* Number of clock cycles in one NTP interval. */
31 cycle_t cycle_interval;
32 /* Number of clock shifted nano seconds in one NTP interval. */
33 u64 xtime_interval;
34 /* Raw nano seconds accumulated per NTP interval. */
35 u32 raw_interval;
36
37 /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
38 u64 xtime_nsec;
39 /* Difference between accumulated time and NTP time in ntp
40 * shifted nano seconds. */
41 s64 ntp_error;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +020042 /* Shift conversion between clock shifted nano seconds and
43 * ntp shifted nano seconds. */
44 int ntp_error_shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +020045 /* NTP adjusted clock multiplier */
46 u32 mult;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020047};
48
49struct timekeeper timekeeper;
50
51/**
52 * timekeeper_setup_internals - Set up internals to use clocksource clock.
53 *
54 * @clock: Pointer to clocksource.
55 *
56 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
57 * pair and interval request.
58 *
59 * Unless you're the timekeeping code, you should not be using this!
60 */
61static void timekeeper_setup_internals(struct clocksource *clock)
62{
63 cycle_t interval;
64 u64 tmp;
65
66 timekeeper.clock = clock;
67 clock->cycle_last = clock->read(clock);
68
69 /* Do the ns -> cycle conversion first, using original mult */
70 tmp = NTP_INTERVAL_LENGTH;
71 tmp <<= clock->shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +020072 tmp += clock->mult/2;
73 do_div(tmp, clock->mult);
Martin Schwidefsky155ec602009-08-14 15:47:26 +020074 if (tmp == 0)
75 tmp = 1;
76
77 interval = (cycle_t) tmp;
78 timekeeper.cycle_interval = interval;
79
80 /* Go back from cycles -> shifted ns */
81 timekeeper.xtime_interval = (u64) interval * clock->mult;
82 timekeeper.raw_interval =
Martin Schwidefsky0a544192009-08-14 15:47:28 +020083 ((u64) interval * clock->mult) >> clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020084
85 timekeeper.xtime_nsec = 0;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +020086 timekeeper.shift = clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020087
88 timekeeper.ntp_error = 0;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +020089 timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +020090
91 /*
92 * The timekeeper keeps its own mult values for the currently
93 * active clocksource. These value will be adjusted via NTP
94 * to counteract clock drifting.
95 */
96 timekeeper.mult = clock->mult;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020097}
john stultz85240702007-05-08 00:27:59 -070098
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +020099/* Timekeeper helper functions. */
100static inline s64 timekeeping_get_ns(void)
101{
102 cycle_t cycle_now, cycle_delta;
103 struct clocksource *clock;
104
105 /* read clocksource: */
106 clock = timekeeper.clock;
107 cycle_now = clock->read(clock);
108
109 /* calculate the delta since the last update_wall_time: */
110 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
111
112 /* return delta convert to nanoseconds using ntp adjusted mult. */
113 return clocksource_cyc2ns(cycle_delta, timekeeper.mult,
114 timekeeper.shift);
115}
116
117static inline s64 timekeeping_get_ns_raw(void)
118{
119 cycle_t cycle_now, cycle_delta;
120 struct clocksource *clock;
121
122 /* read clocksource: */
123 clock = timekeeper.clock;
124 cycle_now = clock->read(clock);
125
126 /* calculate the delta since the last update_wall_time: */
127 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
128
129 /* return delta convert to nanoseconds using ntp adjusted mult. */
130 return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
131}
132
john stultz85240702007-05-08 00:27:59 -0700133/*
134 * This read-write spinlock protects us from races in SMP while
Thomas Gleixnerdce48a82009-04-11 10:43:41 +0200135 * playing with xtime.
john stultz85240702007-05-08 00:27:59 -0700136 */
Adrian Bunkba2a6312007-10-16 23:27:16 -0700137__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
john stultz85240702007-05-08 00:27:59 -0700138
139
140/*
141 * The current time
142 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
143 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
144 * at zero at system boot time, so wall_to_monotonic will be negative,
145 * however, we will ALWAYS keep the tv_nsec part positive so we can use
146 * the usual normalization.
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700147 *
148 * wall_to_monotonic is moved after resume from suspend for the monotonic
149 * time not to jump. We need to add total_sleep_time to wall_to_monotonic
150 * to get the real boot based time offset.
151 *
152 * - wall_to_monotonic is no longer the boot time, getboottime must be
153 * used instead.
john stultz85240702007-05-08 00:27:59 -0700154 */
155struct timespec xtime __attribute__ ((aligned (16)));
156struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200157static struct timespec total_sleep_time;
john stultz85240702007-05-08 00:27:59 -0700158
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200159/*
160 * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
161 */
162struct timespec raw_time;
163
Thomas Gleixner1c5745a2008-12-22 23:05:28 +0100164/* flag for if timekeeping is suspended */
165int __read_mostly timekeeping_suspended;
166
john stultz17c38b72007-07-24 18:38:34 -0700167static struct timespec xtime_cache __attribute__ ((aligned (16)));
Thomas Gleixner1001d0a2008-02-01 17:45:13 +0100168void update_xtime_cache(u64 nsec)
john stultz17c38b72007-07-24 18:38:34 -0700169{
170 xtime_cache = xtime;
171 timespec_add_ns(&xtime_cache, nsec);
172}
john stultz17c38b72007-07-24 18:38:34 -0700173
John Stultz31089c12009-08-14 15:47:18 +0200174/* must hold xtime_lock */
175void timekeeping_leap_insert(int leapsecond)
176{
177 xtime.tv_sec += leapsecond;
178 wall_to_monotonic.tv_sec -= leapsecond;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200179 update_vsyscall(&xtime, timekeeper.clock);
John Stultz31089c12009-08-14 15:47:18 +0200180}
john stultz85240702007-05-08 00:27:59 -0700181
182#ifdef CONFIG_GENERIC_TIME
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200183
john stultz85240702007-05-08 00:27:59 -0700184/**
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200185 * timekeeping_forward_now - update clock to the current time
john stultz85240702007-05-08 00:27:59 -0700186 *
Roman Zippel9a055112008-08-20 16:37:28 -0700187 * Forward the current clock to update its state since the last call to
188 * update_wall_time(). This is useful before significant clock changes,
189 * as it avoids having to deal with this time offset explicitly.
john stultz85240702007-05-08 00:27:59 -0700190 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200191static void timekeeping_forward_now(void)
john stultz85240702007-05-08 00:27:59 -0700192{
193 cycle_t cycle_now, cycle_delta;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200194 struct clocksource *clock;
Roman Zippel9a055112008-08-20 16:37:28 -0700195 s64 nsec;
john stultz85240702007-05-08 00:27:59 -0700196
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200197 clock = timekeeper.clock;
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200198 cycle_now = clock->read(clock);
john stultz85240702007-05-08 00:27:59 -0700199 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
Roman Zippel9a055112008-08-20 16:37:28 -0700200 clock->cycle_last = cycle_now;
john stultz85240702007-05-08 00:27:59 -0700201
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200202 nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
203 timekeeper.shift);
john stultz7d275582009-05-01 13:10:26 -0700204
205 /* If arch requires, add in gettimeoffset() */
206 nsec += arch_gettimeoffset();
207
Roman Zippel9a055112008-08-20 16:37:28 -0700208 timespec_add_ns(&xtime, nsec);
John Stultz2d422442008-08-20 16:37:30 -0700209
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200210 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200211 timespec_add_ns(&raw_time, nsec);
john stultz85240702007-05-08 00:27:59 -0700212}
213
214/**
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100215 * getnstimeofday - Returns the time of day in a timespec
john stultz85240702007-05-08 00:27:59 -0700216 * @ts: pointer to the timespec to be set
217 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100218 * Returns the time of day in a timespec.
john stultz85240702007-05-08 00:27:59 -0700219 */
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100220void getnstimeofday(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700221{
222 unsigned long seq;
223 s64 nsecs;
224
Thomas Gleixner1c5745a2008-12-22 23:05:28 +0100225 WARN_ON(timekeeping_suspended);
226
john stultz85240702007-05-08 00:27:59 -0700227 do {
228 seq = read_seqbegin(&xtime_lock);
229
230 *ts = xtime;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200231 nsecs = timekeeping_get_ns();
john stultz85240702007-05-08 00:27:59 -0700232
john stultz7d275582009-05-01 13:10:26 -0700233 /* If arch requires, add in gettimeoffset() */
234 nsecs += arch_gettimeoffset();
235
john stultz85240702007-05-08 00:27:59 -0700236 } while (read_seqretry(&xtime_lock, seq));
237
238 timespec_add_ns(ts, nsecs);
239}
240
john stultz85240702007-05-08 00:27:59 -0700241EXPORT_SYMBOL(getnstimeofday);
242
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200243ktime_t ktime_get(void)
244{
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200245 unsigned int seq;
246 s64 secs, nsecs;
247
248 WARN_ON(timekeeping_suspended);
249
250 do {
251 seq = read_seqbegin(&xtime_lock);
252 secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
253 nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200254 nsecs += timekeeping_get_ns();
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200255
256 } while (read_seqretry(&xtime_lock, seq));
257 /*
258 * Use ktime_set/ktime_add_ns to create a proper ktime on
259 * 32-bit architectures without CONFIG_KTIME_SCALAR.
260 */
261 return ktime_add_ns(ktime_set(secs, 0), nsecs);
262}
263EXPORT_SYMBOL_GPL(ktime_get);
264
265/**
266 * ktime_get_ts - get the monotonic clock in timespec format
267 * @ts: pointer to timespec variable
268 *
269 * The function calculates the monotonic clock from the realtime
270 * clock and the wall_to_monotonic offset and stores the result
271 * in normalized timespec format in the variable pointed to by @ts.
272 */
273void ktime_get_ts(struct timespec *ts)
274{
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200275 struct timespec tomono;
276 unsigned int seq;
277 s64 nsecs;
278
279 WARN_ON(timekeeping_suspended);
280
281 do {
282 seq = read_seqbegin(&xtime_lock);
283 *ts = xtime;
284 tomono = wall_to_monotonic;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200285 nsecs = timekeeping_get_ns();
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200286
287 } while (read_seqretry(&xtime_lock, seq));
288
289 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
290 ts->tv_nsec + tomono.tv_nsec + nsecs);
291}
292EXPORT_SYMBOL_GPL(ktime_get_ts);
293
john stultz85240702007-05-08 00:27:59 -0700294/**
295 * do_gettimeofday - Returns the time of day in a timeval
296 * @tv: pointer to the timeval to be set
297 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100298 * NOTE: Users should be converted to using getnstimeofday()
john stultz85240702007-05-08 00:27:59 -0700299 */
300void do_gettimeofday(struct timeval *tv)
301{
302 struct timespec now;
303
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100304 getnstimeofday(&now);
john stultz85240702007-05-08 00:27:59 -0700305 tv->tv_sec = now.tv_sec;
306 tv->tv_usec = now.tv_nsec/1000;
307}
308
309EXPORT_SYMBOL(do_gettimeofday);
310/**
311 * do_settimeofday - Sets the time of day
312 * @tv: pointer to the timespec variable containing the new time
313 *
314 * Sets the time of day to the new time and update NTP and notify hrtimers
315 */
316int do_settimeofday(struct timespec *tv)
317{
Roman Zippel9a055112008-08-20 16:37:28 -0700318 struct timespec ts_delta;
john stultz85240702007-05-08 00:27:59 -0700319 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700320
321 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
322 return -EINVAL;
323
324 write_seqlock_irqsave(&xtime_lock, flags);
325
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200326 timekeeping_forward_now();
john stultz85240702007-05-08 00:27:59 -0700327
Roman Zippel9a055112008-08-20 16:37:28 -0700328 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
329 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
330 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
john stultz85240702007-05-08 00:27:59 -0700331
Roman Zippel9a055112008-08-20 16:37:28 -0700332 xtime = *tv;
333
Thomas Gleixner1001d0a2008-02-01 17:45:13 +0100334 update_xtime_cache(0);
john stultz85240702007-05-08 00:27:59 -0700335
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200336 timekeeper.ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -0700337 ntp_clear();
338
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200339 update_vsyscall(&xtime, timekeeper.clock);
john stultz85240702007-05-08 00:27:59 -0700340
341 write_sequnlock_irqrestore(&xtime_lock, flags);
342
343 /* signal hrtimers about time change */
344 clock_was_set();
345
346 return 0;
347}
348
349EXPORT_SYMBOL(do_settimeofday);
350
351/**
352 * change_clocksource - Swaps clocksources if a new one is available
353 *
354 * Accumulates current time interval and initializes new clocksource
355 */
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200356static int change_clocksource(void *data)
john stultz85240702007-05-08 00:27:59 -0700357{
Magnus Damm4614e6a2009-04-21 12:24:02 -0700358 struct clocksource *new, *old;
john stultz85240702007-05-08 00:27:59 -0700359
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200360 new = (struct clocksource *) data;
john stultz85240702007-05-08 00:27:59 -0700361
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200362 timekeeping_forward_now();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200363 if (!new->enable || new->enable(new) == 0) {
364 old = timekeeper.clock;
365 timekeeper_setup_internals(new);
366 if (old->disable)
367 old->disable(old);
368 }
369 return 0;
370}
john stultz85240702007-05-08 00:27:59 -0700371
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200372/**
373 * timekeeping_notify - Install a new clock source
374 * @clock: pointer to the clock source
375 *
376 * This function is called from clocksource.c after a new, better clock
377 * source has been registered. The caller holds the clocksource_mutex.
378 */
379void timekeeping_notify(struct clocksource *clock)
380{
381 if (timekeeper.clock == clock)
Magnus Damm4614e6a2009-04-21 12:24:02 -0700382 return;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200383 stop_machine(change_clocksource, clock, NULL);
john stultz85240702007-05-08 00:27:59 -0700384 tick_clock_notify();
john stultz85240702007-05-08 00:27:59 -0700385}
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200386
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200387#else /* GENERIC_TIME */
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200388
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200389static inline void timekeeping_forward_now(void) { }
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200390
391/**
392 * ktime_get - get the monotonic time in ktime_t format
393 *
394 * returns the time in ktime_t format
395 */
396ktime_t ktime_get(void)
397{
398 struct timespec now;
399
400 ktime_get_ts(&now);
401
402 return timespec_to_ktime(now);
403}
404EXPORT_SYMBOL_GPL(ktime_get);
405
406/**
407 * ktime_get_ts - get the monotonic clock in timespec format
408 * @ts: pointer to timespec variable
409 *
410 * The function calculates the monotonic clock from the realtime
411 * clock and the wall_to_monotonic offset and stores the result
412 * in normalized timespec format in the variable pointed to by @ts.
413 */
414void ktime_get_ts(struct timespec *ts)
415{
416 struct timespec tomono;
417 unsigned long seq;
418
419 do {
420 seq = read_seqbegin(&xtime_lock);
421 getnstimeofday(ts);
422 tomono = wall_to_monotonic;
423
424 } while (read_seqretry(&xtime_lock, seq));
425
426 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
427 ts->tv_nsec + tomono.tv_nsec);
428}
429EXPORT_SYMBOL_GPL(ktime_get_ts);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200430
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200431#endif /* !GENERIC_TIME */
432
433/**
434 * ktime_get_real - get the real (wall-) time in ktime_t format
435 *
436 * returns the time in ktime_t format
437 */
438ktime_t ktime_get_real(void)
439{
440 struct timespec now;
441
442 getnstimeofday(&now);
443
444 return timespec_to_ktime(now);
445}
446EXPORT_SYMBOL_GPL(ktime_get_real);
john stultz85240702007-05-08 00:27:59 -0700447
448/**
John Stultz2d422442008-08-20 16:37:30 -0700449 * getrawmonotonic - Returns the raw monotonic time in a timespec
450 * @ts: pointer to the timespec to be set
451 *
452 * Returns the raw monotonic time (completely un-modified by ntp)
453 */
454void getrawmonotonic(struct timespec *ts)
455{
456 unsigned long seq;
457 s64 nsecs;
John Stultz2d422442008-08-20 16:37:30 -0700458
459 do {
460 seq = read_seqbegin(&xtime_lock);
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200461 nsecs = timekeeping_get_ns_raw();
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200462 *ts = raw_time;
John Stultz2d422442008-08-20 16:37:30 -0700463
464 } while (read_seqretry(&xtime_lock, seq));
465
466 timespec_add_ns(ts, nsecs);
467}
468EXPORT_SYMBOL(getrawmonotonic);
469
470
471/**
Li Zefancf4fc6c2008-02-08 04:19:24 -0800472 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -0700473 */
Li Zefancf4fc6c2008-02-08 04:19:24 -0800474int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -0700475{
476 unsigned long seq;
477 int ret;
478
479 do {
480 seq = read_seqbegin(&xtime_lock);
481
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200482 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -0700483
484 } while (read_seqretry(&xtime_lock, seq));
485
486 return ret;
487}
488
489/**
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200490 * read_persistent_clock - Return time from the persistent clock.
john stultz85240702007-05-08 00:27:59 -0700491 *
492 * Weak dummy function for arches that do not yet support it.
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200493 * Reads the time from the battery backed persistent clock.
494 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
john stultz85240702007-05-08 00:27:59 -0700495 *
496 * XXX - Do be sure to remove it once all arches implement it.
497 */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200498void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700499{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200500 ts->tv_sec = 0;
501 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700502}
503
504/*
505 * timekeeping_init - Initializes the clocksource and common timekeeping values
506 */
507void __init timekeeping_init(void)
508{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200509 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700510 unsigned long flags;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200511 struct timespec now;
512
513 read_persistent_clock(&now);
john stultz85240702007-05-08 00:27:59 -0700514
515 write_seqlock_irqsave(&xtime_lock, flags);
516
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700517 ntp_init();
john stultz85240702007-05-08 00:27:59 -0700518
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200519 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200520 if (clock->enable)
521 clock->enable(clock);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200522 timekeeper_setup_internals(clock);
john stultz85240702007-05-08 00:27:59 -0700523
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200524 xtime.tv_sec = now.tv_sec;
525 xtime.tv_nsec = now.tv_nsec;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200526 raw_time.tv_sec = 0;
527 raw_time.tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700528 set_normalized_timespec(&wall_to_monotonic,
529 -xtime.tv_sec, -xtime.tv_nsec);
Thomas Gleixner1001d0a2008-02-01 17:45:13 +0100530 update_xtime_cache(0);
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200531 total_sleep_time.tv_sec = 0;
532 total_sleep_time.tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700533 write_sequnlock_irqrestore(&xtime_lock, flags);
534}
535
john stultz85240702007-05-08 00:27:59 -0700536/* time in seconds when suspend began */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200537static struct timespec timekeeping_suspend_time;
john stultz85240702007-05-08 00:27:59 -0700538
539/**
540 * timekeeping_resume - Resumes the generic timekeeping subsystem.
541 * @dev: unused
542 *
543 * This is for the generic clocksource timekeeping.
544 * xtime/wall_to_monotonic/jiffies/etc are
545 * still managed by arch specific suspend/resume code.
546 */
547static int timekeeping_resume(struct sys_device *dev)
548{
549 unsigned long flags;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200550 struct timespec ts;
551
552 read_persistent_clock(&ts);
john stultz85240702007-05-08 00:27:59 -0700553
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +0200554 clocksource_resume();
555
john stultz85240702007-05-08 00:27:59 -0700556 write_seqlock_irqsave(&xtime_lock, flags);
557
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200558 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
559 ts = timespec_sub(ts, timekeeping_suspend_time);
560 xtime = timespec_add_safe(xtime, ts);
561 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts);
562 total_sleep_time = timespec_add_safe(total_sleep_time, ts);
john stultz85240702007-05-08 00:27:59 -0700563 }
Thomas Gleixner1001d0a2008-02-01 17:45:13 +0100564 update_xtime_cache(0);
john stultz85240702007-05-08 00:27:59 -0700565 /* re-base the last cycle value */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200566 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
567 timekeeper.ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -0700568 timekeeping_suspended = 0;
569 write_sequnlock_irqrestore(&xtime_lock, flags);
570
571 touch_softlockup_watchdog();
572
573 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
574
575 /* Resume hrtimers */
576 hres_timers_resume();
577
578 return 0;
579}
580
581static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
582{
583 unsigned long flags;
584
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200585 read_persistent_clock(&timekeeping_suspend_time);
Thomas Gleixner3be90952007-09-16 15:36:43 +0200586
john stultz85240702007-05-08 00:27:59 -0700587 write_seqlock_irqsave(&xtime_lock, flags);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200588 timekeeping_forward_now();
john stultz85240702007-05-08 00:27:59 -0700589 timekeeping_suspended = 1;
john stultz85240702007-05-08 00:27:59 -0700590 write_sequnlock_irqrestore(&xtime_lock, flags);
591
592 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
593
594 return 0;
595}
596
597/* sysfs resume/suspend bits for timekeeping */
598static struct sysdev_class timekeeping_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100599 .name = "timekeeping",
john stultz85240702007-05-08 00:27:59 -0700600 .resume = timekeeping_resume,
601 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -0700602};
603
604static struct sys_device device_timer = {
605 .id = 0,
606 .cls = &timekeeping_sysclass,
607};
608
609static int __init timekeeping_init_device(void)
610{
611 int error = sysdev_class_register(&timekeeping_sysclass);
612 if (!error)
613 error = sysdev_register(&device_timer);
614 return error;
615}
616
617device_initcall(timekeeping_init_device);
618
619/*
620 * If the error is already larger, we look ahead even further
621 * to compensate for late or lost adjustments.
622 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200623static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
john stultz85240702007-05-08 00:27:59 -0700624 s64 *offset)
625{
626 s64 tick_error, i;
627 u32 look_ahead, adj;
628 s32 error2, mult;
629
630 /*
631 * Use the current error value to determine how much to look ahead.
632 * The larger the error the slower we adjust for it to avoid problems
633 * with losing too many ticks, otherwise we would overadjust and
634 * produce an even larger error. The smaller the adjustment the
635 * faster we try to adjust for it, as lost ticks can do less harm
Li Zefan3eb05672008-02-08 04:19:25 -0800636 * here. This is tuned so that an error of about 1 msec is adjusted
john stultz85240702007-05-08 00:27:59 -0700637 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
638 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200639 error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
john stultz85240702007-05-08 00:27:59 -0700640 error2 = abs(error2);
641 for (look_ahead = 0; error2 > 0; look_ahead++)
642 error2 >>= 2;
643
644 /*
645 * Now calculate the error in (1 << look_ahead) ticks, but first
646 * remove the single look ahead already included in the error.
647 */
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200648 tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200649 tick_error -= timekeeper.xtime_interval >> 1;
john stultz85240702007-05-08 00:27:59 -0700650 error = ((error - tick_error) >> look_ahead) + tick_error;
651
652 /* Finally calculate the adjustment shift value. */
653 i = *interval;
654 mult = 1;
655 if (error < 0) {
656 error = -error;
657 *interval = -*interval;
658 *offset = -*offset;
659 mult = -1;
660 }
661 for (adj = 0; error > i; adj++)
662 error >>= 1;
663
664 *interval <<= adj;
665 *offset <<= adj;
666 return mult << adj;
667}
668
669/*
670 * Adjust the multiplier to reduce the error value,
671 * this is optimized for the most common adjustments of -1,0,1,
672 * for other values we can do a bit more work.
673 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200674static void timekeeping_adjust(s64 offset)
john stultz85240702007-05-08 00:27:59 -0700675{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200676 s64 error, interval = timekeeper.cycle_interval;
john stultz85240702007-05-08 00:27:59 -0700677 int adj;
678
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200679 error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
john stultz85240702007-05-08 00:27:59 -0700680 if (error > interval) {
681 error >>= 2;
682 if (likely(error <= interval))
683 adj = 1;
684 else
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200685 adj = timekeeping_bigadjust(error, &interval, &offset);
john stultz85240702007-05-08 00:27:59 -0700686 } else if (error < -interval) {
687 error >>= 2;
688 if (likely(error >= -interval)) {
689 adj = -1;
690 interval = -interval;
691 offset = -offset;
692 } else
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200693 adj = timekeeping_bigadjust(error, &interval, &offset);
john stultz85240702007-05-08 00:27:59 -0700694 } else
695 return;
696
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200697 timekeeper.mult += adj;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200698 timekeeper.xtime_interval += interval;
699 timekeeper.xtime_nsec -= offset;
700 timekeeper.ntp_error -= (interval - offset) <<
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200701 timekeeper.ntp_error_shift;
john stultz85240702007-05-08 00:27:59 -0700702}
703
704/**
705 * update_wall_time - Uses the current clocksource to increment the wall time
706 *
707 * Called from the timer interrupt, must hold a write on xtime_lock.
708 */
709void update_wall_time(void)
710{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200711 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700712 cycle_t offset;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200713 u64 nsecs;
john stultz85240702007-05-08 00:27:59 -0700714
715 /* Make sure we're fully resumed: */
716 if (unlikely(timekeeping_suspended))
717 return;
718
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200719 clock = timekeeper.clock;
john stultz85240702007-05-08 00:27:59 -0700720#ifdef CONFIG_GENERIC_TIME
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200721 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
john stultz85240702007-05-08 00:27:59 -0700722#else
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200723 offset = timekeeper.cycle_interval;
john stultz85240702007-05-08 00:27:59 -0700724#endif
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200725 timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
john stultz85240702007-05-08 00:27:59 -0700726
727 /* normally this loop will run just once, however in the
728 * case of lost or late ticks, it will accumulate correctly.
729 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200730 while (offset >= timekeeper.cycle_interval) {
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200731 u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
john stultz85240702007-05-08 00:27:59 -0700732
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200733 /* accumulate one interval */
734 offset -= timekeeper.cycle_interval;
735 clock->cycle_last += timekeeper.cycle_interval;
736
737 timekeeper.xtime_nsec += timekeeper.xtime_interval;
738 if (timekeeper.xtime_nsec >= nsecps) {
739 timekeeper.xtime_nsec -= nsecps;
john stultz85240702007-05-08 00:27:59 -0700740 xtime.tv_sec++;
741 second_overflow();
742 }
743
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200744 raw_time.tv_nsec += timekeeper.raw_interval;
745 if (raw_time.tv_nsec >= NSEC_PER_SEC) {
746 raw_time.tv_nsec -= NSEC_PER_SEC;
747 raw_time.tv_sec++;
John Stultz2d422442008-08-20 16:37:30 -0700748 }
749
john stultz85240702007-05-08 00:27:59 -0700750 /* accumulate error between NTP and clock interval */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200751 timekeeper.ntp_error += tick_length;
752 timekeeper.ntp_error -= timekeeper.xtime_interval <<
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200753 timekeeper.ntp_error_shift;
john stultz85240702007-05-08 00:27:59 -0700754 }
755
756 /* correct the clock when NTP error is too big */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200757 timekeeping_adjust(offset);
john stultz85240702007-05-08 00:27:59 -0700758
john stultz6c9bacb2008-12-01 18:34:41 -0800759 /*
760 * Since in the loop above, we accumulate any amount of time
761 * in xtime_nsec over a second into xtime.tv_sec, its possible for
762 * xtime_nsec to be fairly small after the loop. Further, if we're
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200763 * slightly speeding the clocksource up in timekeeping_adjust(),
john stultz6c9bacb2008-12-01 18:34:41 -0800764 * its possible the required corrective factor to xtime_nsec could
765 * cause it to underflow.
766 *
767 * Now, we cannot simply roll the accumulated second back, since
768 * the NTP subsystem has been notified via second_overflow. So
769 * instead we push xtime_nsec forward by the amount we underflowed,
770 * and add that amount into the error.
771 *
772 * We'll correct this error next time through this function, when
773 * xtime_nsec is not as small.
774 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200775 if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
776 s64 neg = -(s64)timekeeper.xtime_nsec;
777 timekeeper.xtime_nsec = 0;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200778 timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
john stultz6c9bacb2008-12-01 18:34:41 -0800779 }
780
Roman Zippel5cd1c9c2008-09-22 14:42:43 -0700781 /* store full nanoseconds into xtime after rounding it up and
782 * add the remainder to the error difference.
783 */
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200784 xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
785 timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
786 timekeeper.ntp_error += timekeeper.xtime_nsec <<
787 timekeeper.ntp_error_shift;
john stultz85240702007-05-08 00:27:59 -0700788
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200789 nsecs = clocksource_cyc2ns(offset, timekeeper.mult, timekeeper.shift);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200790 update_xtime_cache(nsecs);
john stultz17c38b72007-07-24 18:38:34 -0700791
john stultz85240702007-05-08 00:27:59 -0700792 /* check to see if there is a new clocksource to use */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200793 update_vsyscall(&xtime, timekeeper.clock);
john stultz85240702007-05-08 00:27:59 -0700794}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700795
796/**
797 * getboottime - Return the real time of system boot.
798 * @ts: pointer to the timespec to be set
799 *
800 * Returns the time of day in a timespec.
801 *
802 * This is based on the wall_to_monotonic offset and the total suspend
803 * time. Calls to settimeofday will affect the value returned (which
804 * basically means that however wrong your real time clock is at boot time,
805 * you get the right time here).
806 */
807void getboottime(struct timespec *ts)
808{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200809 struct timespec boottime;
810
811 boottime = timespec_add_safe(wall_to_monotonic, total_sleep_time);
812 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700813}
814
815/**
816 * monotonic_to_bootbased - Convert the monotonic time to boot based.
817 * @ts: pointer to the timespec to be converted
818 */
819void monotonic_to_bootbased(struct timespec *ts)
820{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200821 *ts = timespec_add_safe(*ts, total_sleep_time);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700822}
john stultz2c6b47d2007-07-24 17:47:43 -0700823
john stultz17c38b72007-07-24 18:38:34 -0700824unsigned long get_seconds(void)
825{
826 return xtime_cache.tv_sec;
827}
828EXPORT_SYMBOL(get_seconds);
829
830
john stultz2c6b47d2007-07-24 17:47:43 -0700831struct timespec current_kernel_time(void)
832{
833 struct timespec now;
834 unsigned long seq;
835
836 do {
837 seq = read_seqbegin(&xtime_lock);
838
john stultz17c38b72007-07-24 18:38:34 -0700839 now = xtime_cache;
john stultz2c6b47d2007-07-24 17:47:43 -0700840 } while (read_seqretry(&xtime_lock, seq));
841
842 return now;
843}
john stultz2c6b47d2007-07-24 17:47:43 -0700844EXPORT_SYMBOL(current_kernel_time);