blob: dfdab1cefe1e3720ac9ceae45dac97b1a3f5814f [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>
21
Martin Schwidefsky155ec602009-08-14 15:47:26 +020022/* Structure holding internal timekeeping values. */
23struct timekeeper {
24 /* Current clocksource used for timekeeping. */
25 struct clocksource *clock;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +020026 /* The shift value of the current clocksource. */
27 int shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020028
29 /* Number of clock cycles in one NTP interval. */
30 cycle_t cycle_interval;
31 /* Number of clock shifted nano seconds in one NTP interval. */
32 u64 xtime_interval;
33 /* Raw nano seconds accumulated per NTP interval. */
34 u32 raw_interval;
35
36 /* Clock shifted nano seconds remainder not stored in xtime.tv_nsec. */
37 u64 xtime_nsec;
38 /* Difference between accumulated time and NTP time in ntp
39 * shifted nano seconds. */
40 s64 ntp_error;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +020041 /* Shift conversion between clock shifted nano seconds and
42 * ntp shifted nano seconds. */
43 int ntp_error_shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020044};
45
46struct timekeeper timekeeper;
47
48/**
49 * timekeeper_setup_internals - Set up internals to use clocksource clock.
50 *
51 * @clock: Pointer to clocksource.
52 *
53 * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment
54 * pair and interval request.
55 *
56 * Unless you're the timekeeping code, you should not be using this!
57 */
58static void timekeeper_setup_internals(struct clocksource *clock)
59{
60 cycle_t interval;
61 u64 tmp;
62
63 timekeeper.clock = clock;
64 clock->cycle_last = clock->read(clock);
65
66 /* Do the ns -> cycle conversion first, using original mult */
67 tmp = NTP_INTERVAL_LENGTH;
68 tmp <<= clock->shift;
69 tmp += clock->mult_orig/2;
70 do_div(tmp, clock->mult_orig);
71 if (tmp == 0)
72 tmp = 1;
73
74 interval = (cycle_t) tmp;
75 timekeeper.cycle_interval = interval;
76
77 /* Go back from cycles -> shifted ns */
78 timekeeper.xtime_interval = (u64) interval * clock->mult;
79 timekeeper.raw_interval =
80 ((u64) interval * clock->mult_orig) >> clock->shift;
81
82 timekeeper.xtime_nsec = 0;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +020083 timekeeper.shift = clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020084
85 timekeeper.ntp_error = 0;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +020086 timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
Martin Schwidefsky155ec602009-08-14 15:47:26 +020087}
john stultz85240702007-05-08 00:27:59 -070088
89/*
90 * This read-write spinlock protects us from races in SMP while
Thomas Gleixnerdce48a82009-04-11 10:43:41 +020091 * playing with xtime.
john stultz85240702007-05-08 00:27:59 -070092 */
Adrian Bunkba2a6312007-10-16 23:27:16 -070093__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
john stultz85240702007-05-08 00:27:59 -070094
95
96/*
97 * The current time
98 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
99 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
100 * at zero at system boot time, so wall_to_monotonic will be negative,
101 * however, we will ALWAYS keep the tv_nsec part positive so we can use
102 * the usual normalization.
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700103 *
104 * wall_to_monotonic is moved after resume from suspend for the monotonic
105 * time not to jump. We need to add total_sleep_time to wall_to_monotonic
106 * to get the real boot based time offset.
107 *
108 * - wall_to_monotonic is no longer the boot time, getboottime must be
109 * used instead.
john stultz85240702007-05-08 00:27:59 -0700110 */
111struct timespec xtime __attribute__ ((aligned (16)));
112struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700113static unsigned long total_sleep_time; /* seconds */
john stultz85240702007-05-08 00:27:59 -0700114
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200115/*
116 * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
117 */
118struct timespec raw_time;
119
Thomas Gleixner1c5745a2008-12-22 23:05:28 +0100120/* flag for if timekeeping is suspended */
121int __read_mostly timekeeping_suspended;
122
john stultz17c38b72007-07-24 18:38:34 -0700123static struct timespec xtime_cache __attribute__ ((aligned (16)));
Thomas Gleixner1001d0a2008-02-01 17:45:13 +0100124void update_xtime_cache(u64 nsec)
john stultz17c38b72007-07-24 18:38:34 -0700125{
126 xtime_cache = xtime;
127 timespec_add_ns(&xtime_cache, nsec);
128}
john stultz17c38b72007-07-24 18:38:34 -0700129
John Stultz31089c12009-08-14 15:47:18 +0200130/* must hold xtime_lock */
131void timekeeping_leap_insert(int leapsecond)
132{
133 xtime.tv_sec += leapsecond;
134 wall_to_monotonic.tv_sec -= leapsecond;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200135 update_vsyscall(&xtime, timekeeper.clock);
John Stultz31089c12009-08-14 15:47:18 +0200136}
john stultz85240702007-05-08 00:27:59 -0700137
138#ifdef CONFIG_GENERIC_TIME
139/**
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200140 * timekeeping_forward_now - update clock to the current time
john stultz85240702007-05-08 00:27:59 -0700141 *
Roman Zippel9a055112008-08-20 16:37:28 -0700142 * Forward the current clock to update its state since the last call to
143 * update_wall_time(). This is useful before significant clock changes,
144 * as it avoids having to deal with this time offset explicitly.
john stultz85240702007-05-08 00:27:59 -0700145 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200146static void timekeeping_forward_now(void)
john stultz85240702007-05-08 00:27:59 -0700147{
148 cycle_t cycle_now, cycle_delta;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200149 struct clocksource *clock;
Roman Zippel9a055112008-08-20 16:37:28 -0700150 s64 nsec;
john stultz85240702007-05-08 00:27:59 -0700151
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200152 clock = timekeeper.clock;
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200153 cycle_now = clock->read(clock);
john stultz85240702007-05-08 00:27:59 -0700154 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
Roman Zippel9a055112008-08-20 16:37:28 -0700155 clock->cycle_last = cycle_now;
john stultz85240702007-05-08 00:27:59 -0700156
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200157 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
john stultz7d275582009-05-01 13:10:26 -0700158
159 /* If arch requires, add in gettimeoffset() */
160 nsec += arch_gettimeoffset();
161
Roman Zippel9a055112008-08-20 16:37:28 -0700162 timespec_add_ns(&xtime, nsec);
John Stultz2d422442008-08-20 16:37:30 -0700163
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200164 nsec = clocksource_cyc2ns(cycle_delta, clock->mult_orig, clock->shift);
165 timespec_add_ns(&raw_time, nsec);
john stultz85240702007-05-08 00:27:59 -0700166}
167
168/**
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100169 * getnstimeofday - Returns the time of day in a timespec
john stultz85240702007-05-08 00:27:59 -0700170 * @ts: pointer to the timespec to be set
171 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100172 * Returns the time of day in a timespec.
john stultz85240702007-05-08 00:27:59 -0700173 */
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100174void getnstimeofday(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700175{
Roman Zippel9a055112008-08-20 16:37:28 -0700176 cycle_t cycle_now, cycle_delta;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200177 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700178 unsigned long seq;
179 s64 nsecs;
180
Thomas Gleixner1c5745a2008-12-22 23:05:28 +0100181 WARN_ON(timekeeping_suspended);
182
john stultz85240702007-05-08 00:27:59 -0700183 do {
184 seq = read_seqbegin(&xtime_lock);
185
186 *ts = xtime;
Roman Zippel9a055112008-08-20 16:37:28 -0700187
188 /* read clocksource: */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200189 clock = timekeeper.clock;
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200190 cycle_now = clock->read(clock);
Roman Zippel9a055112008-08-20 16:37:28 -0700191
192 /* calculate the delta since the last update_wall_time: */
193 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
194
195 /* convert to nanoseconds: */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200196 nsecs = clocksource_cyc2ns(cycle_delta, clock->mult,
197 clock->shift);
john stultz85240702007-05-08 00:27:59 -0700198
john stultz7d275582009-05-01 13:10:26 -0700199 /* If arch requires, add in gettimeoffset() */
200 nsecs += arch_gettimeoffset();
201
john stultz85240702007-05-08 00:27:59 -0700202 } while (read_seqretry(&xtime_lock, seq));
203
204 timespec_add_ns(ts, nsecs);
205}
206
john stultz85240702007-05-08 00:27:59 -0700207EXPORT_SYMBOL(getnstimeofday);
208
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200209ktime_t ktime_get(void)
210{
211 cycle_t cycle_now, cycle_delta;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200212 struct clocksource *clock;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200213 unsigned int seq;
214 s64 secs, nsecs;
215
216 WARN_ON(timekeeping_suspended);
217
218 do {
219 seq = read_seqbegin(&xtime_lock);
220 secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
221 nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
222
223 /* read clocksource: */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200224 clock = timekeeper.clock;
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200225 cycle_now = clock->read(clock);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200226
227 /* calculate the delta since the last update_wall_time: */
228 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
229
230 /* convert to nanoseconds: */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200231 nsecs += clocksource_cyc2ns(cycle_delta, clock->mult,
232 clock->shift);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200233
234 } while (read_seqretry(&xtime_lock, seq));
235 /*
236 * Use ktime_set/ktime_add_ns to create a proper ktime on
237 * 32-bit architectures without CONFIG_KTIME_SCALAR.
238 */
239 return ktime_add_ns(ktime_set(secs, 0), nsecs);
240}
241EXPORT_SYMBOL_GPL(ktime_get);
242
243/**
244 * ktime_get_ts - get the monotonic clock in timespec format
245 * @ts: pointer to timespec variable
246 *
247 * The function calculates the monotonic clock from the realtime
248 * clock and the wall_to_monotonic offset and stores the result
249 * in normalized timespec format in the variable pointed to by @ts.
250 */
251void ktime_get_ts(struct timespec *ts)
252{
253 cycle_t cycle_now, cycle_delta;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200254 struct clocksource *clock;
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200255 struct timespec tomono;
256 unsigned int seq;
257 s64 nsecs;
258
259 WARN_ON(timekeeping_suspended);
260
261 do {
262 seq = read_seqbegin(&xtime_lock);
263 *ts = xtime;
264 tomono = wall_to_monotonic;
265
266 /* read clocksource: */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200267 clock = timekeeper.clock;
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200268 cycle_now = clock->read(clock);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200269
270 /* calculate the delta since the last update_wall_time: */
271 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
272
273 /* convert to nanoseconds: */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200274 nsecs = clocksource_cyc2ns(cycle_delta, clock->mult,
275 clock->shift);
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200276
277 } while (read_seqretry(&xtime_lock, seq));
278
279 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
280 ts->tv_nsec + tomono.tv_nsec + nsecs);
281}
282EXPORT_SYMBOL_GPL(ktime_get_ts);
283
john stultz85240702007-05-08 00:27:59 -0700284/**
285 * do_gettimeofday - Returns the time of day in a timeval
286 * @tv: pointer to the timeval to be set
287 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100288 * NOTE: Users should be converted to using getnstimeofday()
john stultz85240702007-05-08 00:27:59 -0700289 */
290void do_gettimeofday(struct timeval *tv)
291{
292 struct timespec now;
293
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100294 getnstimeofday(&now);
john stultz85240702007-05-08 00:27:59 -0700295 tv->tv_sec = now.tv_sec;
296 tv->tv_usec = now.tv_nsec/1000;
297}
298
299EXPORT_SYMBOL(do_gettimeofday);
300/**
301 * do_settimeofday - Sets the time of day
302 * @tv: pointer to the timespec variable containing the new time
303 *
304 * Sets the time of day to the new time and update NTP and notify hrtimers
305 */
306int do_settimeofday(struct timespec *tv)
307{
Roman Zippel9a055112008-08-20 16:37:28 -0700308 struct timespec ts_delta;
john stultz85240702007-05-08 00:27:59 -0700309 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700310
311 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
312 return -EINVAL;
313
314 write_seqlock_irqsave(&xtime_lock, flags);
315
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200316 timekeeping_forward_now();
john stultz85240702007-05-08 00:27:59 -0700317
Roman Zippel9a055112008-08-20 16:37:28 -0700318 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
319 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
320 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
john stultz85240702007-05-08 00:27:59 -0700321
Roman Zippel9a055112008-08-20 16:37:28 -0700322 xtime = *tv;
323
Thomas Gleixner1001d0a2008-02-01 17:45:13 +0100324 update_xtime_cache(0);
john stultz85240702007-05-08 00:27:59 -0700325
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200326 timekeeper.ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -0700327 ntp_clear();
328
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200329 update_vsyscall(&xtime, timekeeper.clock);
john stultz85240702007-05-08 00:27:59 -0700330
331 write_sequnlock_irqrestore(&xtime_lock, flags);
332
333 /* signal hrtimers about time change */
334 clock_was_set();
335
336 return 0;
337}
338
339EXPORT_SYMBOL(do_settimeofday);
340
341/**
342 * change_clocksource - Swaps clocksources if a new one is available
343 *
344 * Accumulates current time interval and initializes new clocksource
345 */
346static void change_clocksource(void)
347{
Magnus Damm4614e6a2009-04-21 12:24:02 -0700348 struct clocksource *new, *old;
john stultz85240702007-05-08 00:27:59 -0700349
350 new = clocksource_get_next();
351
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200352 if (!new || timekeeper.clock == new)
john stultz85240702007-05-08 00:27:59 -0700353 return;
354
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200355 timekeeping_forward_now();
john stultz85240702007-05-08 00:27:59 -0700356
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200357 if (new->enable && !new->enable(new))
Magnus Damm4614e6a2009-04-21 12:24:02 -0700358 return;
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200359 /*
360 * The frequency may have changed while the clocksource
361 * was disabled. If so the code in ->enable() must update
362 * the mult value to reflect the new frequency. Make sure
363 * mult_orig follows this change.
364 */
365 new->mult_orig = new->mult;
John Stultz2d422442008-08-20 16:37:30 -0700366
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200367 old = timekeeper.clock;
368 timekeeper_setup_internals(new);
369
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200370 /*
371 * Save mult_orig in mult so that the value can be restored
372 * regardless if ->enable() updates the value of mult or not.
373 */
374 old->mult = old->mult_orig;
375 if (old->disable)
376 old->disable(old);
Magnus Damm4614e6a2009-04-21 12:24:02 -0700377
john stultz85240702007-05-08 00:27:59 -0700378 tick_clock_notify();
john stultz85240702007-05-08 00:27:59 -0700379}
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200380#else /* GENERIC_TIME */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200381static inline void timekeeping_forward_now(void) { }
john stultz85240702007-05-08 00:27:59 -0700382static inline void change_clocksource(void) { }
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200383
384/**
385 * ktime_get - get the monotonic time in ktime_t format
386 *
387 * returns the time in ktime_t format
388 */
389ktime_t ktime_get(void)
390{
391 struct timespec now;
392
393 ktime_get_ts(&now);
394
395 return timespec_to_ktime(now);
396}
397EXPORT_SYMBOL_GPL(ktime_get);
398
399/**
400 * ktime_get_ts - get the monotonic clock in timespec format
401 * @ts: pointer to timespec variable
402 *
403 * The function calculates the monotonic clock from the realtime
404 * clock and the wall_to_monotonic offset and stores the result
405 * in normalized timespec format in the variable pointed to by @ts.
406 */
407void ktime_get_ts(struct timespec *ts)
408{
409 struct timespec tomono;
410 unsigned long seq;
411
412 do {
413 seq = read_seqbegin(&xtime_lock);
414 getnstimeofday(ts);
415 tomono = wall_to_monotonic;
416
417 } while (read_seqretry(&xtime_lock, seq));
418
419 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
420 ts->tv_nsec + tomono.tv_nsec);
421}
422EXPORT_SYMBOL_GPL(ktime_get_ts);
423#endif /* !GENERIC_TIME */
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);
john stultz85240702007-05-08 00:27:59 -0700439
440/**
John Stultz2d422442008-08-20 16:37:30 -0700441 * getrawmonotonic - Returns the raw monotonic time in a timespec
442 * @ts: pointer to the timespec to be set
443 *
444 * Returns the raw monotonic time (completely un-modified by ntp)
445 */
446void getrawmonotonic(struct timespec *ts)
447{
448 unsigned long seq;
449 s64 nsecs;
450 cycle_t cycle_now, cycle_delta;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200451 struct clocksource *clock;
John Stultz2d422442008-08-20 16:37:30 -0700452
453 do {
454 seq = read_seqbegin(&xtime_lock);
455
456 /* read clocksource: */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200457 clock = timekeeper.clock;
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200458 cycle_now = clock->read(clock);
John Stultz2d422442008-08-20 16:37:30 -0700459
460 /* calculate the delta since the last update_wall_time: */
461 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
462
463 /* convert to nanoseconds: */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200464 nsecs = clocksource_cyc2ns(cycle_delta, clock->mult_orig,
465 clock->shift);
John Stultz2d422442008-08-20 16:37:30 -0700466
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200467 *ts = raw_time;
John Stultz2d422442008-08-20 16:37:30 -0700468
469 } while (read_seqretry(&xtime_lock, seq));
470
471 timespec_add_ns(ts, nsecs);
472}
473EXPORT_SYMBOL(getrawmonotonic);
474
475
476/**
Li Zefancf4fc6c2008-02-08 04:19:24 -0800477 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -0700478 */
Li Zefancf4fc6c2008-02-08 04:19:24 -0800479int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -0700480{
481 unsigned long seq;
482 int ret;
483
484 do {
485 seq = read_seqbegin(&xtime_lock);
486
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200487 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -0700488
489 } while (read_seqretry(&xtime_lock, seq));
490
491 return ret;
492}
493
494/**
495 * read_persistent_clock - Return time in seconds from the persistent clock.
496 *
497 * Weak dummy function for arches that do not yet support it.
498 * Returns seconds from epoch using the battery backed persistent clock.
499 * Returns zero if unsupported.
500 *
501 * XXX - Do be sure to remove it once all arches implement it.
502 */
503unsigned long __attribute__((weak)) read_persistent_clock(void)
504{
505 return 0;
506}
507
508/*
509 * timekeeping_init - Initializes the clocksource and common timekeeping values
510 */
511void __init timekeeping_init(void)
512{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200513 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700514 unsigned long flags;
515 unsigned long sec = read_persistent_clock();
516
517 write_seqlock_irqsave(&xtime_lock, flags);
518
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700519 ntp_init();
john stultz85240702007-05-08 00:27:59 -0700520
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200521 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200522 if (clock->enable)
523 clock->enable(clock);
524 /* set mult_orig on enable */
525 clock->mult_orig = clock->mult;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200526
527 timekeeper_setup_internals(clock);
john stultz85240702007-05-08 00:27:59 -0700528
529 xtime.tv_sec = sec;
530 xtime.tv_nsec = 0;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200531 raw_time.tv_sec = 0;
532 raw_time.tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700533 set_normalized_timespec(&wall_to_monotonic,
534 -xtime.tv_sec, -xtime.tv_nsec);
Thomas Gleixner1001d0a2008-02-01 17:45:13 +0100535 update_xtime_cache(0);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700536 total_sleep_time = 0;
john stultz85240702007-05-08 00:27:59 -0700537 write_sequnlock_irqrestore(&xtime_lock, flags);
538}
539
john stultz85240702007-05-08 00:27:59 -0700540/* time in seconds when suspend began */
541static unsigned long timekeeping_suspend_time;
542
543/**
544 * timekeeping_resume - Resumes the generic timekeeping subsystem.
545 * @dev: unused
546 *
547 * This is for the generic clocksource timekeeping.
548 * xtime/wall_to_monotonic/jiffies/etc are
549 * still managed by arch specific suspend/resume code.
550 */
551static int timekeeping_resume(struct sys_device *dev)
552{
553 unsigned long flags;
554 unsigned long now = read_persistent_clock();
555
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +0200556 clocksource_resume();
557
john stultz85240702007-05-08 00:27:59 -0700558 write_seqlock_irqsave(&xtime_lock, flags);
559
560 if (now && (now > timekeeping_suspend_time)) {
561 unsigned long sleep_length = now - timekeeping_suspend_time;
562
563 xtime.tv_sec += sleep_length;
564 wall_to_monotonic.tv_sec -= sleep_length;
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700565 total_sleep_time += sleep_length;
john stultz85240702007-05-08 00:27:59 -0700566 }
Thomas Gleixner1001d0a2008-02-01 17:45:13 +0100567 update_xtime_cache(0);
john stultz85240702007-05-08 00:27:59 -0700568 /* re-base the last cycle value */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200569 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
570 timekeeper.ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -0700571 timekeeping_suspended = 0;
572 write_sequnlock_irqrestore(&xtime_lock, flags);
573
574 touch_softlockup_watchdog();
575
576 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
577
578 /* Resume hrtimers */
579 hres_timers_resume();
580
581 return 0;
582}
583
584static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
585{
586 unsigned long flags;
587
Thomas Gleixner3be90952007-09-16 15:36:43 +0200588 timekeeping_suspend_time = read_persistent_clock();
589
john stultz85240702007-05-08 00:27:59 -0700590 write_seqlock_irqsave(&xtime_lock, flags);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200591 timekeeping_forward_now();
john stultz85240702007-05-08 00:27:59 -0700592 timekeeping_suspended = 1;
john stultz85240702007-05-08 00:27:59 -0700593 write_sequnlock_irqrestore(&xtime_lock, flags);
594
595 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
596
597 return 0;
598}
599
600/* sysfs resume/suspend bits for timekeeping */
601static struct sysdev_class timekeeping_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100602 .name = "timekeeping",
john stultz85240702007-05-08 00:27:59 -0700603 .resume = timekeeping_resume,
604 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -0700605};
606
607static struct sys_device device_timer = {
608 .id = 0,
609 .cls = &timekeeping_sysclass,
610};
611
612static int __init timekeeping_init_device(void)
613{
614 int error = sysdev_class_register(&timekeeping_sysclass);
615 if (!error)
616 error = sysdev_register(&device_timer);
617 return error;
618}
619
620device_initcall(timekeeping_init_device);
621
622/*
623 * If the error is already larger, we look ahead even further
624 * to compensate for late or lost adjustments.
625 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200626static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
john stultz85240702007-05-08 00:27:59 -0700627 s64 *offset)
628{
629 s64 tick_error, i;
630 u32 look_ahead, adj;
631 s32 error2, mult;
632
633 /*
634 * Use the current error value to determine how much to look ahead.
635 * The larger the error the slower we adjust for it to avoid problems
636 * with losing too many ticks, otherwise we would overadjust and
637 * produce an even larger error. The smaller the adjustment the
638 * faster we try to adjust for it, as lost ticks can do less harm
Li Zefan3eb05672008-02-08 04:19:25 -0800639 * here. This is tuned so that an error of about 1 msec is adjusted
john stultz85240702007-05-08 00:27:59 -0700640 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
641 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200642 error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
john stultz85240702007-05-08 00:27:59 -0700643 error2 = abs(error2);
644 for (look_ahead = 0; error2 > 0; look_ahead++)
645 error2 >>= 2;
646
647 /*
648 * Now calculate the error in (1 << look_ahead) ticks, but first
649 * remove the single look ahead already included in the error.
650 */
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200651 tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200652 tick_error -= timekeeper.xtime_interval >> 1;
john stultz85240702007-05-08 00:27:59 -0700653 error = ((error - tick_error) >> look_ahead) + tick_error;
654
655 /* Finally calculate the adjustment shift value. */
656 i = *interval;
657 mult = 1;
658 if (error < 0) {
659 error = -error;
660 *interval = -*interval;
661 *offset = -*offset;
662 mult = -1;
663 }
664 for (adj = 0; error > i; adj++)
665 error >>= 1;
666
667 *interval <<= adj;
668 *offset <<= adj;
669 return mult << adj;
670}
671
672/*
673 * Adjust the multiplier to reduce the error value,
674 * this is optimized for the most common adjustments of -1,0,1,
675 * for other values we can do a bit more work.
676 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200677static void timekeeping_adjust(s64 offset)
john stultz85240702007-05-08 00:27:59 -0700678{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200679 s64 error, interval = timekeeper.cycle_interval;
john stultz85240702007-05-08 00:27:59 -0700680 int adj;
681
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200682 error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
john stultz85240702007-05-08 00:27:59 -0700683 if (error > interval) {
684 error >>= 2;
685 if (likely(error <= interval))
686 adj = 1;
687 else
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200688 adj = timekeeping_bigadjust(error, &interval, &offset);
john stultz85240702007-05-08 00:27:59 -0700689 } else if (error < -interval) {
690 error >>= 2;
691 if (likely(error >= -interval)) {
692 adj = -1;
693 interval = -interval;
694 offset = -offset;
695 } else
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200696 adj = timekeeping_bigadjust(error, &interval, &offset);
john stultz85240702007-05-08 00:27:59 -0700697 } else
698 return;
699
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200700 timekeeper.clock->mult += adj;
701 timekeeper.xtime_interval += interval;
702 timekeeper.xtime_nsec -= offset;
703 timekeeper.ntp_error -= (interval - offset) <<
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200704 timekeeper.ntp_error_shift;
john stultz85240702007-05-08 00:27:59 -0700705}
706
707/**
708 * update_wall_time - Uses the current clocksource to increment the wall time
709 *
710 * Called from the timer interrupt, must hold a write on xtime_lock.
711 */
712void update_wall_time(void)
713{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200714 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700715 cycle_t offset;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200716 u64 nsecs;
john stultz85240702007-05-08 00:27:59 -0700717
718 /* Make sure we're fully resumed: */
719 if (unlikely(timekeeping_suspended))
720 return;
721
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200722 clock = timekeeper.clock;
john stultz85240702007-05-08 00:27:59 -0700723#ifdef CONFIG_GENERIC_TIME
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200724 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
john stultz85240702007-05-08 00:27:59 -0700725#else
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200726 offset = timekeeper.cycle_interval;
john stultz85240702007-05-08 00:27:59 -0700727#endif
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200728 timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
john stultz85240702007-05-08 00:27:59 -0700729
730 /* normally this loop will run just once, however in the
731 * case of lost or late ticks, it will accumulate correctly.
732 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200733 while (offset >= timekeeper.cycle_interval) {
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200734 u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
john stultz85240702007-05-08 00:27:59 -0700735
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200736 /* accumulate one interval */
737 offset -= timekeeper.cycle_interval;
738 clock->cycle_last += timekeeper.cycle_interval;
739
740 timekeeper.xtime_nsec += timekeeper.xtime_interval;
741 if (timekeeper.xtime_nsec >= nsecps) {
742 timekeeper.xtime_nsec -= nsecps;
john stultz85240702007-05-08 00:27:59 -0700743 xtime.tv_sec++;
744 second_overflow();
745 }
746
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200747 raw_time.tv_nsec += timekeeper.raw_interval;
748 if (raw_time.tv_nsec >= NSEC_PER_SEC) {
749 raw_time.tv_nsec -= NSEC_PER_SEC;
750 raw_time.tv_sec++;
John Stultz2d422442008-08-20 16:37:30 -0700751 }
752
john stultz85240702007-05-08 00:27:59 -0700753 /* accumulate error between NTP and clock interval */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200754 timekeeper.ntp_error += tick_length;
755 timekeeper.ntp_error -= timekeeper.xtime_interval <<
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200756 timekeeper.ntp_error_shift;
john stultz85240702007-05-08 00:27:59 -0700757 }
758
759 /* correct the clock when NTP error is too big */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200760 timekeeping_adjust(offset);
john stultz85240702007-05-08 00:27:59 -0700761
john stultz6c9bacb2008-12-01 18:34:41 -0800762 /*
763 * Since in the loop above, we accumulate any amount of time
764 * in xtime_nsec over a second into xtime.tv_sec, its possible for
765 * xtime_nsec to be fairly small after the loop. Further, if we're
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200766 * slightly speeding the clocksource up in timekeeping_adjust(),
john stultz6c9bacb2008-12-01 18:34:41 -0800767 * its possible the required corrective factor to xtime_nsec could
768 * cause it to underflow.
769 *
770 * Now, we cannot simply roll the accumulated second back, since
771 * the NTP subsystem has been notified via second_overflow. So
772 * instead we push xtime_nsec forward by the amount we underflowed,
773 * and add that amount into the error.
774 *
775 * We'll correct this error next time through this function, when
776 * xtime_nsec is not as small.
777 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200778 if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
779 s64 neg = -(s64)timekeeper.xtime_nsec;
780 timekeeper.xtime_nsec = 0;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200781 timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
john stultz6c9bacb2008-12-01 18:34:41 -0800782 }
783
Roman Zippel5cd1c9c2008-09-22 14:42:43 -0700784 /* store full nanoseconds into xtime after rounding it up and
785 * add the remainder to the error difference.
786 */
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200787 xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
788 timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
789 timekeeper.ntp_error += timekeeper.xtime_nsec <<
790 timekeeper.ntp_error_shift;
john stultz85240702007-05-08 00:27:59 -0700791
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200792 nsecs = clocksource_cyc2ns(offset, clock->mult, clock->shift);
793 update_xtime_cache(nsecs);
john stultz17c38b72007-07-24 18:38:34 -0700794
john stultz85240702007-05-08 00:27:59 -0700795 /* check to see if there is a new clocksource to use */
796 change_clocksource();
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200797 update_vsyscall(&xtime, timekeeper.clock);
john stultz85240702007-05-08 00:27:59 -0700798}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700799
800/**
801 * getboottime - Return the real time of system boot.
802 * @ts: pointer to the timespec to be set
803 *
804 * Returns the time of day in a timespec.
805 *
806 * This is based on the wall_to_monotonic offset and the total suspend
807 * time. Calls to settimeofday will affect the value returned (which
808 * basically means that however wrong your real time clock is at boot time,
809 * you get the right time here).
810 */
811void getboottime(struct timespec *ts)
812{
813 set_normalized_timespec(ts,
814 - (wall_to_monotonic.tv_sec + total_sleep_time),
815 - wall_to_monotonic.tv_nsec);
816}
817
818/**
819 * monotonic_to_bootbased - Convert the monotonic time to boot based.
820 * @ts: pointer to the timespec to be converted
821 */
822void monotonic_to_bootbased(struct timespec *ts)
823{
824 ts->tv_sec += total_sleep_time;
825}
john stultz2c6b47d2007-07-24 17:47:43 -0700826
john stultz17c38b72007-07-24 18:38:34 -0700827unsigned long get_seconds(void)
828{
829 return xtime_cache.tv_sec;
830}
831EXPORT_SYMBOL(get_seconds);
832
833
john stultz2c6b47d2007-07-24 17:47:43 -0700834struct timespec current_kernel_time(void)
835{
836 struct timespec now;
837 unsigned long seq;
838
839 do {
840 seq = read_seqbegin(&xtime_lock);
841
john stultz17c38b72007-07-24 18:38:34 -0700842 now = xtime_cache;
john stultz2c6b47d2007-07-24 17:47:43 -0700843 } while (read_seqretry(&xtime_lock, seq));
844
845 return now;
846}
john stultz2c6b47d2007-07-24 17:47:43 -0700847EXPORT_SYMBOL(current_kernel_time);