blob: 16736379a9caaba6320f32f3df6e94cb5a9abbda [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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040016#include <linux/sched.h>
john stultz85240702007-05-08 00:27:59 -070017#include <linux/sysdev.h>
18#include <linux/clocksource.h>
19#include <linux/jiffies.h>
20#include <linux/time.h>
21#include <linux/tick.h>
Martin Schwidefsky75c51582009-08-14 15:47:30 +020022#include <linux/stop_machine.h>
john stultz85240702007-05-08 00:27:59 -070023
Martin Schwidefsky155ec602009-08-14 15:47:26 +020024/* Structure holding internal timekeeping values. */
25struct timekeeper {
26 /* Current clocksource used for timekeeping. */
27 struct clocksource *clock;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +020028 /* The shift value of the current clocksource. */
29 int shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020030
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;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +020043 /* Shift conversion between clock shifted nano seconds and
44 * ntp shifted nano seconds. */
45 int ntp_error_shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +020046 /* NTP adjusted clock multiplier */
47 u32 mult;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020048};
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;
Martin Schwidefsky0a544192009-08-14 15:47:28 +020073 tmp += clock->mult/2;
74 do_div(tmp, clock->mult);
Martin Schwidefsky155ec602009-08-14 15:47:26 +020075 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 =
Martin Schwidefsky0a544192009-08-14 15:47:28 +020084 ((u64) interval * clock->mult) >> clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020085
86 timekeeper.xtime_nsec = 0;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +020087 timekeeper.shift = clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020088
89 timekeeper.ntp_error = 0;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +020090 timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
Martin Schwidefsky0a544192009-08-14 15:47:28 +020091
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;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020098}
john stultz85240702007-05-08 00:27:59 -070099
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200100/* 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}
133
john stultz85240702007-05-08 00:27:59 -0700134/*
135 * This read-write spinlock protects us from races in SMP while
Thomas Gleixnerdce48a82009-04-11 10:43:41 +0200136 * playing with xtime.
john stultz85240702007-05-08 00:27:59 -0700137 */
Adrian Bunkba2a6312007-10-16 23:27:16 -0700138__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
john stultz85240702007-05-08 00:27:59 -0700139
140
141/*
142 * The current time
143 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
144 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
145 * at zero at system boot time, so wall_to_monotonic will be negative,
146 * however, we will ALWAYS keep the tv_nsec part positive so we can use
147 * the usual normalization.
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700148 *
149 * wall_to_monotonic is moved after resume from suspend for the monotonic
150 * time not to jump. We need to add total_sleep_time to wall_to_monotonic
151 * to get the real boot based time offset.
152 *
153 * - wall_to_monotonic is no longer the boot time, getboottime must be
154 * used instead.
john stultz85240702007-05-08 00:27:59 -0700155 */
156struct timespec xtime __attribute__ ((aligned (16)));
157struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200158static struct timespec total_sleep_time;
john stultz85240702007-05-08 00:27:59 -0700159
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200160/*
161 * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
162 */
163struct timespec raw_time;
164
Thomas Gleixner1c5745a2008-12-22 23:05:28 +0100165/* flag for if timekeeping is suspended */
166int __read_mostly timekeeping_suspended;
167
Linus Torvalds83f57a12009-12-22 14:10:37 -0800168static struct timespec xtime_cache __attribute__ ((aligned (16)));
169void update_xtime_cache(u64 nsec)
170{
171 xtime_cache = xtime;
172 timespec_add_ns(&xtime_cache, nsec);
173}
174
John Stultz31089c12009-08-14 15:47:18 +0200175/* must hold xtime_lock */
176void timekeeping_leap_insert(int leapsecond)
177{
178 xtime.tv_sec += leapsecond;
179 wall_to_monotonic.tv_sec -= leapsecond;
Lin Ming0696b712009-11-17 13:49:50 +0800180 update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult);
John Stultz31089c12009-08-14 15:47:18 +0200181}
john stultz85240702007-05-08 00:27:59 -0700182
183#ifdef CONFIG_GENERIC_TIME
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200184
john stultz85240702007-05-08 00:27:59 -0700185/**
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200186 * timekeeping_forward_now - update clock to the current time
john stultz85240702007-05-08 00:27:59 -0700187 *
Roman Zippel9a055112008-08-20 16:37:28 -0700188 * Forward the current clock to update its state since the last call to
189 * update_wall_time(). This is useful before significant clock changes,
190 * as it avoids having to deal with this time offset explicitly.
john stultz85240702007-05-08 00:27:59 -0700191 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200192static void timekeeping_forward_now(void)
john stultz85240702007-05-08 00:27:59 -0700193{
194 cycle_t cycle_now, cycle_delta;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200195 struct clocksource *clock;
Roman Zippel9a055112008-08-20 16:37:28 -0700196 s64 nsec;
john stultz85240702007-05-08 00:27:59 -0700197
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200198 clock = timekeeper.clock;
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200199 cycle_now = clock->read(clock);
john stultz85240702007-05-08 00:27:59 -0700200 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
Roman Zippel9a055112008-08-20 16:37:28 -0700201 clock->cycle_last = cycle_now;
john stultz85240702007-05-08 00:27:59 -0700202
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200203 nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
204 timekeeper.shift);
john stultz7d275582009-05-01 13:10:26 -0700205
206 /* If arch requires, add in gettimeoffset() */
207 nsec += arch_gettimeoffset();
208
Roman Zippel9a055112008-08-20 16:37:28 -0700209 timespec_add_ns(&xtime, nsec);
John Stultz2d422442008-08-20 16:37:30 -0700210
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200211 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200212 timespec_add_ns(&raw_time, nsec);
john stultz85240702007-05-08 00:27:59 -0700213}
214
215/**
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100216 * getnstimeofday - Returns the time of day in a timespec
john stultz85240702007-05-08 00:27:59 -0700217 * @ts: pointer to the timespec to be set
218 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100219 * Returns the time of day in a timespec.
john stultz85240702007-05-08 00:27:59 -0700220 */
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100221void getnstimeofday(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700222{
223 unsigned long seq;
224 s64 nsecs;
225
Thomas Gleixner1c5745a2008-12-22 23:05:28 +0100226 WARN_ON(timekeeping_suspended);
227
john stultz85240702007-05-08 00:27:59 -0700228 do {
229 seq = read_seqbegin(&xtime_lock);
230
231 *ts = xtime;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200232 nsecs = timekeeping_get_ns();
john stultz85240702007-05-08 00:27:59 -0700233
john stultz7d275582009-05-01 13:10:26 -0700234 /* If arch requires, add in gettimeoffset() */
235 nsecs += arch_gettimeoffset();
236
john stultz85240702007-05-08 00:27:59 -0700237 } while (read_seqretry(&xtime_lock, seq));
238
239 timespec_add_ns(ts, nsecs);
240}
241
john stultz85240702007-05-08 00:27:59 -0700242EXPORT_SYMBOL(getnstimeofday);
243
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200244ktime_t ktime_get(void)
245{
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200246 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;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200255 nsecs += timekeeping_get_ns();
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200256
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{
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200276 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;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200286 nsecs = timekeeping_get_ns();
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200287
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
john stultz85240702007-05-08 00:27:59 -0700295/**
296 * do_gettimeofday - Returns the time of day in a timeval
297 * @tv: pointer to the timeval to be set
298 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100299 * NOTE: Users should be converted to using getnstimeofday()
john stultz85240702007-05-08 00:27:59 -0700300 */
301void do_gettimeofday(struct timeval *tv)
302{
303 struct timespec now;
304
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100305 getnstimeofday(&now);
john stultz85240702007-05-08 00:27:59 -0700306 tv->tv_sec = now.tv_sec;
307 tv->tv_usec = now.tv_nsec/1000;
308}
309
310EXPORT_SYMBOL(do_gettimeofday);
311/**
312 * do_settimeofday - Sets the time of day
313 * @tv: pointer to the timespec variable containing the new time
314 *
315 * Sets the time of day to the new time and update NTP and notify hrtimers
316 */
317int do_settimeofday(struct timespec *tv)
318{
Roman Zippel9a055112008-08-20 16:37:28 -0700319 struct timespec ts_delta;
john stultz85240702007-05-08 00:27:59 -0700320 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700321
322 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
323 return -EINVAL;
324
325 write_seqlock_irqsave(&xtime_lock, flags);
326
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200327 timekeeping_forward_now();
john stultz85240702007-05-08 00:27:59 -0700328
Roman Zippel9a055112008-08-20 16:37:28 -0700329 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
330 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
331 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
john stultz85240702007-05-08 00:27:59 -0700332
Roman Zippel9a055112008-08-20 16:37:28 -0700333 xtime = *tv;
334
Linus Torvalds83f57a12009-12-22 14:10:37 -0800335 update_xtime_cache(0);
336
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200337 timekeeper.ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -0700338 ntp_clear();
339
Lin Ming0696b712009-11-17 13:49:50 +0800340 update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult);
john stultz85240702007-05-08 00:27:59 -0700341
342 write_sequnlock_irqrestore(&xtime_lock, flags);
343
344 /* signal hrtimers about time change */
345 clock_was_set();
346
347 return 0;
348}
349
350EXPORT_SYMBOL(do_settimeofday);
351
352/**
353 * change_clocksource - Swaps clocksources if a new one is available
354 *
355 * Accumulates current time interval and initializes new clocksource
356 */
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200357static int change_clocksource(void *data)
john stultz85240702007-05-08 00:27:59 -0700358{
Magnus Damm4614e6a2009-04-21 12:24:02 -0700359 struct clocksource *new, *old;
john stultz85240702007-05-08 00:27:59 -0700360
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200361 new = (struct clocksource *) data;
john stultz85240702007-05-08 00:27:59 -0700362
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200363 timekeeping_forward_now();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200364 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}
john stultz85240702007-05-08 00:27:59 -0700372
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200373/**
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)
Magnus Damm4614e6a2009-04-21 12:24:02 -0700383 return;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200384 stop_machine(change_clocksource, clock, NULL);
john stultz85240702007-05-08 00:27:59 -0700385 tick_clock_notify();
john stultz85240702007-05-08 00:27:59 -0700386}
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200387
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200388#else /* GENERIC_TIME */
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200389
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200390static inline void timekeeping_forward_now(void) { }
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200391
392/**
393 * ktime_get - get the monotonic time in ktime_t format
394 *
395 * returns the time in ktime_t format
396 */
397ktime_t ktime_get(void)
398{
399 struct timespec now;
400
401 ktime_get_ts(&now);
402
403 return timespec_to_ktime(now);
404}
405EXPORT_SYMBOL_GPL(ktime_get);
406
407/**
408 * ktime_get_ts - get the monotonic clock in timespec format
409 * @ts: pointer to timespec variable
410 *
411 * The function calculates the monotonic clock from the realtime
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);
429}
430EXPORT_SYMBOL_GPL(ktime_get_ts);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200431
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200432#endif /* !GENERIC_TIME */
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);
john stultz85240702007-05-08 00:27:59 -0700448
449/**
John Stultz2d422442008-08-20 16:37:30 -0700450 * getrawmonotonic - Returns the raw monotonic time in a timespec
451 * @ts: pointer to the timespec to be set
452 *
453 * Returns the raw monotonic time (completely un-modified by ntp)
454 */
455void getrawmonotonic(struct timespec *ts)
456{
457 unsigned long seq;
458 s64 nsecs;
John Stultz2d422442008-08-20 16:37:30 -0700459
460 do {
461 seq = read_seqbegin(&xtime_lock);
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200462 nsecs = timekeeping_get_ns_raw();
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200463 *ts = raw_time;
John Stultz2d422442008-08-20 16:37:30 -0700464
465 } while (read_seqretry(&xtime_lock, seq));
466
467 timespec_add_ns(ts, nsecs);
468}
469EXPORT_SYMBOL(getrawmonotonic);
470
471
472/**
Li Zefancf4fc6c2008-02-08 04:19:24 -0800473 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -0700474 */
Li Zefancf4fc6c2008-02-08 04:19:24 -0800475int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -0700476{
477 unsigned long seq;
478 int ret;
479
480 do {
481 seq = read_seqbegin(&xtime_lock);
482
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200483 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -0700484
485 } while (read_seqretry(&xtime_lock, seq));
486
487 return ret;
488}
489
490/**
Jon Hunter98962462009-08-18 12:45:10 -0500491 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
492 *
493 * Caller must observe xtime_lock via read_seqbegin/read_seqretry to
494 * ensure that the clocksource does not change!
495 */
496u64 timekeeping_max_deferment(void)
497{
498 return timekeeper.clock->max_idle_ns;
499}
500
501/**
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200502 * read_persistent_clock - Return time from the persistent clock.
john stultz85240702007-05-08 00:27:59 -0700503 *
504 * Weak dummy function for arches that do not yet support it.
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200505 * Reads the time from the battery backed persistent clock.
506 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
john stultz85240702007-05-08 00:27:59 -0700507 *
508 * XXX - Do be sure to remove it once all arches implement it.
509 */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200510void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700511{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200512 ts->tv_sec = 0;
513 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700514}
515
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200516/**
517 * read_boot_clock - Return time of the system start.
518 *
519 * Weak dummy function for arches that do not yet support it.
520 * Function to read the exact time the system has been started.
521 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
522 *
523 * XXX - Do be sure to remove it once all arches implement it.
524 */
525void __attribute__((weak)) read_boot_clock(struct timespec *ts)
526{
527 ts->tv_sec = 0;
528 ts->tv_nsec = 0;
529}
530
john stultz85240702007-05-08 00:27:59 -0700531/*
532 * timekeeping_init - Initializes the clocksource and common timekeeping values
533 */
534void __init timekeeping_init(void)
535{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200536 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700537 unsigned long flags;
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200538 struct timespec now, boot;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200539
540 read_persistent_clock(&now);
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200541 read_boot_clock(&boot);
john stultz85240702007-05-08 00:27:59 -0700542
543 write_seqlock_irqsave(&xtime_lock, flags);
544
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700545 ntp_init();
john stultz85240702007-05-08 00:27:59 -0700546
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200547 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200548 if (clock->enable)
549 clock->enable(clock);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200550 timekeeper_setup_internals(clock);
john stultz85240702007-05-08 00:27:59 -0700551
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200552 xtime.tv_sec = now.tv_sec;
553 xtime.tv_nsec = now.tv_nsec;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200554 raw_time.tv_sec = 0;
555 raw_time.tv_nsec = 0;
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200556 if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
557 boot.tv_sec = xtime.tv_sec;
558 boot.tv_nsec = xtime.tv_nsec;
559 }
john stultz85240702007-05-08 00:27:59 -0700560 set_normalized_timespec(&wall_to_monotonic,
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200561 -boot.tv_sec, -boot.tv_nsec);
Linus Torvalds83f57a12009-12-22 14:10:37 -0800562 update_xtime_cache(0);
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200563 total_sleep_time.tv_sec = 0;
564 total_sleep_time.tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700565 write_sequnlock_irqrestore(&xtime_lock, flags);
566}
567
john stultz85240702007-05-08 00:27:59 -0700568/* time in seconds when suspend began */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200569static struct timespec timekeeping_suspend_time;
john stultz85240702007-05-08 00:27:59 -0700570
571/**
572 * timekeeping_resume - Resumes the generic timekeeping subsystem.
573 * @dev: unused
574 *
575 * This is for the generic clocksource timekeeping.
576 * xtime/wall_to_monotonic/jiffies/etc are
577 * still managed by arch specific suspend/resume code.
578 */
579static int timekeeping_resume(struct sys_device *dev)
580{
581 unsigned long flags;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200582 struct timespec ts;
583
584 read_persistent_clock(&ts);
john stultz85240702007-05-08 00:27:59 -0700585
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +0200586 clocksource_resume();
587
john stultz85240702007-05-08 00:27:59 -0700588 write_seqlock_irqsave(&xtime_lock, flags);
589
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200590 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
591 ts = timespec_sub(ts, timekeeping_suspend_time);
592 xtime = timespec_add_safe(xtime, ts);
593 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts);
594 total_sleep_time = timespec_add_safe(total_sleep_time, ts);
john stultz85240702007-05-08 00:27:59 -0700595 }
Linus Torvalds83f57a12009-12-22 14:10:37 -0800596 update_xtime_cache(0);
john stultz85240702007-05-08 00:27:59 -0700597 /* re-base the last cycle value */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200598 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
599 timekeeper.ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -0700600 timekeeping_suspended = 0;
601 write_sequnlock_irqrestore(&xtime_lock, flags);
602
603 touch_softlockup_watchdog();
604
605 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
606
607 /* Resume hrtimers */
608 hres_timers_resume();
609
610 return 0;
611}
612
613static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
614{
615 unsigned long flags;
616
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200617 read_persistent_clock(&timekeeping_suspend_time);
Thomas Gleixner3be90952007-09-16 15:36:43 +0200618
john stultz85240702007-05-08 00:27:59 -0700619 write_seqlock_irqsave(&xtime_lock, flags);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200620 timekeeping_forward_now();
john stultz85240702007-05-08 00:27:59 -0700621 timekeeping_suspended = 1;
john stultz85240702007-05-08 00:27:59 -0700622 write_sequnlock_irqrestore(&xtime_lock, flags);
623
624 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
Magnus Dammc54a42b2010-02-02 14:41:41 -0800625 clocksource_suspend();
john stultz85240702007-05-08 00:27:59 -0700626
627 return 0;
628}
629
630/* sysfs resume/suspend bits for timekeeping */
631static struct sysdev_class timekeeping_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100632 .name = "timekeeping",
john stultz85240702007-05-08 00:27:59 -0700633 .resume = timekeeping_resume,
634 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -0700635};
636
637static struct sys_device device_timer = {
638 .id = 0,
639 .cls = &timekeeping_sysclass,
640};
641
642static int __init timekeeping_init_device(void)
643{
644 int error = sysdev_class_register(&timekeeping_sysclass);
645 if (!error)
646 error = sysdev_register(&device_timer);
647 return error;
648}
649
650device_initcall(timekeeping_init_device);
651
652/*
653 * If the error is already larger, we look ahead even further
654 * to compensate for late or lost adjustments.
655 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200656static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
john stultz85240702007-05-08 00:27:59 -0700657 s64 *offset)
658{
659 s64 tick_error, i;
660 u32 look_ahead, adj;
661 s32 error2, mult;
662
663 /*
664 * Use the current error value to determine how much to look ahead.
665 * The larger the error the slower we adjust for it to avoid problems
666 * with losing too many ticks, otherwise we would overadjust and
667 * produce an even larger error. The smaller the adjustment the
668 * faster we try to adjust for it, as lost ticks can do less harm
Li Zefan3eb05672008-02-08 04:19:25 -0800669 * here. This is tuned so that an error of about 1 msec is adjusted
john stultz85240702007-05-08 00:27:59 -0700670 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
671 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200672 error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
john stultz85240702007-05-08 00:27:59 -0700673 error2 = abs(error2);
674 for (look_ahead = 0; error2 > 0; look_ahead++)
675 error2 >>= 2;
676
677 /*
678 * Now calculate the error in (1 << look_ahead) ticks, but first
679 * remove the single look ahead already included in the error.
680 */
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200681 tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200682 tick_error -= timekeeper.xtime_interval >> 1;
john stultz85240702007-05-08 00:27:59 -0700683 error = ((error - tick_error) >> look_ahead) + tick_error;
684
685 /* Finally calculate the adjustment shift value. */
686 i = *interval;
687 mult = 1;
688 if (error < 0) {
689 error = -error;
690 *interval = -*interval;
691 *offset = -*offset;
692 mult = -1;
693 }
694 for (adj = 0; error > i; adj++)
695 error >>= 1;
696
697 *interval <<= adj;
698 *offset <<= adj;
699 return mult << adj;
700}
701
702/*
703 * Adjust the multiplier to reduce the error value,
704 * this is optimized for the most common adjustments of -1,0,1,
705 * for other values we can do a bit more work.
706 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200707static void timekeeping_adjust(s64 offset)
john stultz85240702007-05-08 00:27:59 -0700708{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200709 s64 error, interval = timekeeper.cycle_interval;
john stultz85240702007-05-08 00:27:59 -0700710 int adj;
711
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200712 error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
john stultz85240702007-05-08 00:27:59 -0700713 if (error > interval) {
714 error >>= 2;
715 if (likely(error <= interval))
716 adj = 1;
717 else
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200718 adj = timekeeping_bigadjust(error, &interval, &offset);
john stultz85240702007-05-08 00:27:59 -0700719 } else if (error < -interval) {
720 error >>= 2;
721 if (likely(error >= -interval)) {
722 adj = -1;
723 interval = -interval;
724 offset = -offset;
725 } else
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200726 adj = timekeeping_bigadjust(error, &interval, &offset);
john stultz85240702007-05-08 00:27:59 -0700727 } else
728 return;
729
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200730 timekeeper.mult += adj;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200731 timekeeper.xtime_interval += interval;
732 timekeeper.xtime_nsec -= offset;
733 timekeeper.ntp_error -= (interval - offset) <<
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200734 timekeeper.ntp_error_shift;
john stultz85240702007-05-08 00:27:59 -0700735}
736
Linus Torvalds83f57a12009-12-22 14:10:37 -0800737
john stultz85240702007-05-08 00:27:59 -0700738/**
john stultza092ff02009-10-02 16:17:53 -0700739 * logarithmic_accumulation - shifted accumulation of cycles
740 *
741 * This functions accumulates a shifted interval of cycles into
742 * into a shifted interval nanoseconds. Allows for O(log) accumulation
743 * loop.
744 *
745 * Returns the unconsumed cycles.
746 */
747static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
748{
749 u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
750
751 /* If the offset is smaller then a shifted interval, do nothing */
752 if (offset < timekeeper.cycle_interval<<shift)
753 return offset;
754
755 /* Accumulate one shifted interval */
756 offset -= timekeeper.cycle_interval << shift;
757 timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift;
758
759 timekeeper.xtime_nsec += timekeeper.xtime_interval << shift;
760 while (timekeeper.xtime_nsec >= nsecps) {
761 timekeeper.xtime_nsec -= nsecps;
762 xtime.tv_sec++;
763 second_overflow();
764 }
765
766 /* Accumulate into raw time */
767 raw_time.tv_nsec += timekeeper.raw_interval << shift;;
768 while (raw_time.tv_nsec >= NSEC_PER_SEC) {
769 raw_time.tv_nsec -= NSEC_PER_SEC;
770 raw_time.tv_sec++;
771 }
772
773 /* Accumulate error between NTP and clock interval */
774 timekeeper.ntp_error += tick_length << shift;
775 timekeeper.ntp_error -= timekeeper.xtime_interval <<
776 (timekeeper.ntp_error_shift + shift);
777
778 return offset;
779}
780
Linus Torvalds83f57a12009-12-22 14:10:37 -0800781
john stultz85240702007-05-08 00:27:59 -0700782/**
783 * update_wall_time - Uses the current clocksource to increment the wall time
784 *
785 * Called from the timer interrupt, must hold a write on xtime_lock.
786 */
787void update_wall_time(void)
788{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200789 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700790 cycle_t offset;
Linus Torvalds83f57a12009-12-22 14:10:37 -0800791 u64 nsecs;
john stultza092ff02009-10-02 16:17:53 -0700792 int shift = 0, maxshift;
john stultz85240702007-05-08 00:27:59 -0700793
794 /* Make sure we're fully resumed: */
795 if (unlikely(timekeeping_suspended))
796 return;
797
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200798 clock = timekeeper.clock;
john stultz85240702007-05-08 00:27:59 -0700799#ifdef CONFIG_GENERIC_TIME
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200800 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
john stultz85240702007-05-08 00:27:59 -0700801#else
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200802 offset = timekeeper.cycle_interval;
john stultz85240702007-05-08 00:27:59 -0700803#endif
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200804 timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
john stultz85240702007-05-08 00:27:59 -0700805
john stultza092ff02009-10-02 16:17:53 -0700806 /*
807 * With NO_HZ we may have to accumulate many cycle_intervals
808 * (think "ticks") worth of time at once. To do this efficiently,
809 * we calculate the largest doubling multiple of cycle_intervals
810 * that is smaller then the offset. We then accumulate that
811 * chunk in one go, and then try to consume the next smaller
812 * doubled multiple.
john stultz85240702007-05-08 00:27:59 -0700813 */
john stultza092ff02009-10-02 16:17:53 -0700814 shift = ilog2(offset) - ilog2(timekeeper.cycle_interval);
815 shift = max(0, shift);
816 /* Bound shift to one less then what overflows tick_length */
817 maxshift = (8*sizeof(tick_length) - (ilog2(tick_length)+1)) - 1;
818 shift = min(shift, maxshift);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200819 while (offset >= timekeeper.cycle_interval) {
john stultza092ff02009-10-02 16:17:53 -0700820 offset = logarithmic_accumulation(offset, shift);
821 shift--;
john stultz85240702007-05-08 00:27:59 -0700822 }
823
824 /* correct the clock when NTP error is too big */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200825 timekeeping_adjust(offset);
john stultz85240702007-05-08 00:27:59 -0700826
john stultz6c9bacb2008-12-01 18:34:41 -0800827 /*
828 * Since in the loop above, we accumulate any amount of time
829 * in xtime_nsec over a second into xtime.tv_sec, its possible for
830 * xtime_nsec to be fairly small after the loop. Further, if we're
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200831 * slightly speeding the clocksource up in timekeeping_adjust(),
john stultz6c9bacb2008-12-01 18:34:41 -0800832 * its possible the required corrective factor to xtime_nsec could
833 * cause it to underflow.
834 *
835 * Now, we cannot simply roll the accumulated second back, since
836 * the NTP subsystem has been notified via second_overflow. So
837 * instead we push xtime_nsec forward by the amount we underflowed,
838 * and add that amount into the error.
839 *
840 * We'll correct this error next time through this function, when
841 * xtime_nsec is not as small.
842 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200843 if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
844 s64 neg = -(s64)timekeeper.xtime_nsec;
845 timekeeper.xtime_nsec = 0;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200846 timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
john stultz6c9bacb2008-12-01 18:34:41 -0800847 }
848
Roman Zippel5cd1c9c2008-09-22 14:42:43 -0700849 /* store full nanoseconds into xtime after rounding it up and
850 * add the remainder to the error difference.
851 */
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200852 xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
853 timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
854 timekeeper.ntp_error += timekeeper.xtime_nsec <<
855 timekeeper.ntp_error_shift;
john stultz85240702007-05-08 00:27:59 -0700856
Linus Torvalds83f57a12009-12-22 14:10:37 -0800857 nsecs = clocksource_cyc2ns(offset, timekeeper.mult, timekeeper.shift);
858 update_xtime_cache(nsecs);
859
john stultz85240702007-05-08 00:27:59 -0700860 /* check to see if there is a new clocksource to use */
Lin Ming0696b712009-11-17 13:49:50 +0800861 update_vsyscall(&xtime, timekeeper.clock, timekeeper.mult);
john stultz85240702007-05-08 00:27:59 -0700862}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700863
864/**
865 * getboottime - Return the real time of system boot.
866 * @ts: pointer to the timespec to be set
867 *
868 * Returns the time of day in a timespec.
869 *
870 * This is based on the wall_to_monotonic offset and the total suspend
871 * time. Calls to settimeofday will affect the value returned (which
872 * basically means that however wrong your real time clock is at boot time,
873 * you get the right time here).
874 */
875void getboottime(struct timespec *ts)
876{
Hiroshi Shimamoto36d47482009-08-25 15:08:30 +0900877 struct timespec boottime = {
878 .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec,
879 .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec
880 };
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200881
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200882 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700883}
Jason Wangc93d89f2010-01-27 19:13:40 +0800884EXPORT_SYMBOL_GPL(getboottime);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700885
886/**
887 * monotonic_to_bootbased - Convert the monotonic time to boot based.
888 * @ts: pointer to the timespec to be converted
889 */
890void monotonic_to_bootbased(struct timespec *ts)
891{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200892 *ts = timespec_add_safe(*ts, total_sleep_time);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700893}
Jason Wangc93d89f2010-01-27 19:13:40 +0800894EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
john stultz2c6b47d2007-07-24 17:47:43 -0700895
john stultz17c38b72007-07-24 18:38:34 -0700896unsigned long get_seconds(void)
897{
Linus Torvalds83f57a12009-12-22 14:10:37 -0800898 return xtime_cache.tv_sec;
john stultz17c38b72007-07-24 18:38:34 -0700899}
900EXPORT_SYMBOL(get_seconds);
901
john stultzda15cfd2009-08-19 19:13:34 -0700902struct timespec __current_kernel_time(void)
903{
Linus Torvalds83f57a12009-12-22 14:10:37 -0800904 return xtime_cache;
john stultzda15cfd2009-08-19 19:13:34 -0700905}
john stultz17c38b72007-07-24 18:38:34 -0700906
john stultz2c6b47d2007-07-24 17:47:43 -0700907struct timespec current_kernel_time(void)
908{
909 struct timespec now;
910 unsigned long seq;
911
912 do {
913 seq = read_seqbegin(&xtime_lock);
Linus Torvalds83f57a12009-12-22 14:10:37 -0800914
915 now = xtime_cache;
john stultz2c6b47d2007-07-24 17:47:43 -0700916 } while (read_seqretry(&xtime_lock, seq));
917
918 return now;
919}
john stultz2c6b47d2007-07-24 17:47:43 -0700920EXPORT_SYMBOL(current_kernel_time);
john stultzda15cfd2009-08-19 19:13:34 -0700921
922struct timespec get_monotonic_coarse(void)
923{
924 struct timespec now, mono;
925 unsigned long seq;
926
927 do {
928 seq = read_seqbegin(&xtime_lock);
Linus Torvalds83f57a12009-12-22 14:10:37 -0800929
930 now = xtime_cache;
john stultzda15cfd2009-08-19 19:13:34 -0700931 mono = wall_to_monotonic;
932 } while (read_seqretry(&xtime_lock, seq));
933
934 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
935 now.tv_nsec + mono.tv_nsec);
936 return now;
937}