blob: 96b3f0dfa5dcdfb2d746d2a0c66c92620b605c46 [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 Stultz31089c12009-08-14 15:47:18 +0200167/* must hold xtime_lock */
168void timekeeping_leap_insert(int leapsecond)
169{
170 xtime.tv_sec += leapsecond;
171 wall_to_monotonic.tv_sec -= leapsecond;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200172 update_vsyscall(&xtime, timekeeper.clock);
John Stultz31089c12009-08-14 15:47:18 +0200173}
john stultz85240702007-05-08 00:27:59 -0700174
175#ifdef CONFIG_GENERIC_TIME
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200176
john stultz85240702007-05-08 00:27:59 -0700177/**
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200178 * timekeeping_forward_now - update clock to the current time
john stultz85240702007-05-08 00:27:59 -0700179 *
Roman Zippel9a055112008-08-20 16:37:28 -0700180 * Forward the current clock to update its state since the last call to
181 * update_wall_time(). This is useful before significant clock changes,
182 * as it avoids having to deal with this time offset explicitly.
john stultz85240702007-05-08 00:27:59 -0700183 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200184static void timekeeping_forward_now(void)
john stultz85240702007-05-08 00:27:59 -0700185{
186 cycle_t cycle_now, cycle_delta;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200187 struct clocksource *clock;
Roman Zippel9a055112008-08-20 16:37:28 -0700188 s64 nsec;
john stultz85240702007-05-08 00:27:59 -0700189
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200190 clock = timekeeper.clock;
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200191 cycle_now = clock->read(clock);
john stultz85240702007-05-08 00:27:59 -0700192 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
Roman Zippel9a055112008-08-20 16:37:28 -0700193 clock->cycle_last = cycle_now;
john stultz85240702007-05-08 00:27:59 -0700194
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200195 nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
196 timekeeper.shift);
john stultz7d275582009-05-01 13:10:26 -0700197
198 /* If arch requires, add in gettimeoffset() */
199 nsec += arch_gettimeoffset();
200
Roman Zippel9a055112008-08-20 16:37:28 -0700201 timespec_add_ns(&xtime, nsec);
John Stultz2d422442008-08-20 16:37:30 -0700202
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200203 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200204 timespec_add_ns(&raw_time, nsec);
john stultz85240702007-05-08 00:27:59 -0700205}
206
207/**
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100208 * getnstimeofday - Returns the time of day in a timespec
john stultz85240702007-05-08 00:27:59 -0700209 * @ts: pointer to the timespec to be set
210 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100211 * Returns the time of day in a timespec.
john stultz85240702007-05-08 00:27:59 -0700212 */
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100213void getnstimeofday(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700214{
215 unsigned long seq;
216 s64 nsecs;
217
Thomas Gleixner1c5745a2008-12-22 23:05:28 +0100218 WARN_ON(timekeeping_suspended);
219
john stultz85240702007-05-08 00:27:59 -0700220 do {
221 seq = read_seqbegin(&xtime_lock);
222
223 *ts = xtime;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200224 nsecs = timekeeping_get_ns();
john stultz85240702007-05-08 00:27:59 -0700225
john stultz7d275582009-05-01 13:10:26 -0700226 /* If arch requires, add in gettimeoffset() */
227 nsecs += arch_gettimeoffset();
228
john stultz85240702007-05-08 00:27:59 -0700229 } while (read_seqretry(&xtime_lock, seq));
230
231 timespec_add_ns(ts, nsecs);
232}
233
john stultz85240702007-05-08 00:27:59 -0700234EXPORT_SYMBOL(getnstimeofday);
235
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200236ktime_t ktime_get(void)
237{
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200238 unsigned int seq;
239 s64 secs, nsecs;
240
241 WARN_ON(timekeeping_suspended);
242
243 do {
244 seq = read_seqbegin(&xtime_lock);
245 secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
246 nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200247 nsecs += timekeeping_get_ns();
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200248
249 } while (read_seqretry(&xtime_lock, seq));
250 /*
251 * Use ktime_set/ktime_add_ns to create a proper ktime on
252 * 32-bit architectures without CONFIG_KTIME_SCALAR.
253 */
254 return ktime_add_ns(ktime_set(secs, 0), nsecs);
255}
256EXPORT_SYMBOL_GPL(ktime_get);
257
258/**
259 * ktime_get_ts - get the monotonic clock in timespec format
260 * @ts: pointer to timespec variable
261 *
262 * The function calculates the monotonic clock from the realtime
263 * clock and the wall_to_monotonic offset and stores the result
264 * in normalized timespec format in the variable pointed to by @ts.
265 */
266void ktime_get_ts(struct timespec *ts)
267{
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200268 struct timespec tomono;
269 unsigned int seq;
270 s64 nsecs;
271
272 WARN_ON(timekeeping_suspended);
273
274 do {
275 seq = read_seqbegin(&xtime_lock);
276 *ts = xtime;
277 tomono = wall_to_monotonic;
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200278 nsecs = timekeeping_get_ns();
Martin Schwidefsky951ed4d2009-07-07 11:27:28 +0200279
280 } while (read_seqretry(&xtime_lock, seq));
281
282 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
283 ts->tv_nsec + tomono.tv_nsec + nsecs);
284}
285EXPORT_SYMBOL_GPL(ktime_get_ts);
286
john stultz85240702007-05-08 00:27:59 -0700287/**
288 * do_gettimeofday - Returns the time of day in a timeval
289 * @tv: pointer to the timeval to be set
290 *
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100291 * NOTE: Users should be converted to using getnstimeofday()
john stultz85240702007-05-08 00:27:59 -0700292 */
293void do_gettimeofday(struct timeval *tv)
294{
295 struct timespec now;
296
Geert Uytterhoevenefd9ac82008-01-30 13:30:01 +0100297 getnstimeofday(&now);
john stultz85240702007-05-08 00:27:59 -0700298 tv->tv_sec = now.tv_sec;
299 tv->tv_usec = now.tv_nsec/1000;
300}
301
302EXPORT_SYMBOL(do_gettimeofday);
303/**
304 * do_settimeofday - Sets the time of day
305 * @tv: pointer to the timespec variable containing the new time
306 *
307 * Sets the time of day to the new time and update NTP and notify hrtimers
308 */
309int do_settimeofday(struct timespec *tv)
310{
Roman Zippel9a055112008-08-20 16:37:28 -0700311 struct timespec ts_delta;
john stultz85240702007-05-08 00:27:59 -0700312 unsigned long flags;
john stultz85240702007-05-08 00:27:59 -0700313
314 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
315 return -EINVAL;
316
317 write_seqlock_irqsave(&xtime_lock, flags);
318
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200319 timekeeping_forward_now();
john stultz85240702007-05-08 00:27:59 -0700320
Roman Zippel9a055112008-08-20 16:37:28 -0700321 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
322 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
323 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
john stultz85240702007-05-08 00:27:59 -0700324
Roman Zippel9a055112008-08-20 16:37:28 -0700325 xtime = *tv;
326
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200327 timekeeper.ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -0700328 ntp_clear();
329
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200330 update_vsyscall(&xtime, timekeeper.clock);
john stultz85240702007-05-08 00:27:59 -0700331
332 write_sequnlock_irqrestore(&xtime_lock, flags);
333
334 /* signal hrtimers about time change */
335 clock_was_set();
336
337 return 0;
338}
339
340EXPORT_SYMBOL(do_settimeofday);
341
342/**
343 * change_clocksource - Swaps clocksources if a new one is available
344 *
345 * Accumulates current time interval and initializes new clocksource
346 */
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200347static int change_clocksource(void *data)
john stultz85240702007-05-08 00:27:59 -0700348{
Magnus Damm4614e6a2009-04-21 12:24:02 -0700349 struct clocksource *new, *old;
john stultz85240702007-05-08 00:27:59 -0700350
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200351 new = (struct clocksource *) data;
john stultz85240702007-05-08 00:27:59 -0700352
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200353 timekeeping_forward_now();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200354 if (!new->enable || new->enable(new) == 0) {
355 old = timekeeper.clock;
356 timekeeper_setup_internals(new);
357 if (old->disable)
358 old->disable(old);
359 }
360 return 0;
361}
john stultz85240702007-05-08 00:27:59 -0700362
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200363/**
364 * timekeeping_notify - Install a new clock source
365 * @clock: pointer to the clock source
366 *
367 * This function is called from clocksource.c after a new, better clock
368 * source has been registered. The caller holds the clocksource_mutex.
369 */
370void timekeeping_notify(struct clocksource *clock)
371{
372 if (timekeeper.clock == clock)
Magnus Damm4614e6a2009-04-21 12:24:02 -0700373 return;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200374 stop_machine(change_clocksource, clock, NULL);
john stultz85240702007-05-08 00:27:59 -0700375 tick_clock_notify();
john stultz85240702007-05-08 00:27:59 -0700376}
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200377
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200378#else /* GENERIC_TIME */
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200379
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200380static inline void timekeeping_forward_now(void) { }
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200381
382/**
383 * ktime_get - get the monotonic time in ktime_t format
384 *
385 * returns the time in ktime_t format
386 */
387ktime_t ktime_get(void)
388{
389 struct timespec now;
390
391 ktime_get_ts(&now);
392
393 return timespec_to_ktime(now);
394}
395EXPORT_SYMBOL_GPL(ktime_get);
396
397/**
398 * ktime_get_ts - get the monotonic clock in timespec format
399 * @ts: pointer to timespec variable
400 *
401 * The function calculates the monotonic clock from the realtime
402 * clock and the wall_to_monotonic offset and stores the result
403 * in normalized timespec format in the variable pointed to by @ts.
404 */
405void ktime_get_ts(struct timespec *ts)
406{
407 struct timespec tomono;
408 unsigned long seq;
409
410 do {
411 seq = read_seqbegin(&xtime_lock);
412 getnstimeofday(ts);
413 tomono = wall_to_monotonic;
414
415 } while (read_seqretry(&xtime_lock, seq));
416
417 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
418 ts->tv_nsec + tomono.tv_nsec);
419}
420EXPORT_SYMBOL_GPL(ktime_get_ts);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200421
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200422#endif /* !GENERIC_TIME */
423
424/**
425 * ktime_get_real - get the real (wall-) time in ktime_t format
426 *
427 * returns the time in ktime_t format
428 */
429ktime_t ktime_get_real(void)
430{
431 struct timespec now;
432
433 getnstimeofday(&now);
434
435 return timespec_to_ktime(now);
436}
437EXPORT_SYMBOL_GPL(ktime_get_real);
john stultz85240702007-05-08 00:27:59 -0700438
439/**
John Stultz2d422442008-08-20 16:37:30 -0700440 * getrawmonotonic - Returns the raw monotonic time in a timespec
441 * @ts: pointer to the timespec to be set
442 *
443 * Returns the raw monotonic time (completely un-modified by ntp)
444 */
445void getrawmonotonic(struct timespec *ts)
446{
447 unsigned long seq;
448 s64 nsecs;
John Stultz2d422442008-08-20 16:37:30 -0700449
450 do {
451 seq = read_seqbegin(&xtime_lock);
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200452 nsecs = timekeeping_get_ns_raw();
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200453 *ts = raw_time;
John Stultz2d422442008-08-20 16:37:30 -0700454
455 } while (read_seqretry(&xtime_lock, seq));
456
457 timespec_add_ns(ts, nsecs);
458}
459EXPORT_SYMBOL(getrawmonotonic);
460
461
462/**
Li Zefancf4fc6c2008-02-08 04:19:24 -0800463 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -0700464 */
Li Zefancf4fc6c2008-02-08 04:19:24 -0800465int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -0700466{
467 unsigned long seq;
468 int ret;
469
470 do {
471 seq = read_seqbegin(&xtime_lock);
472
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200473 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -0700474
475 } while (read_seqretry(&xtime_lock, seq));
476
477 return ret;
478}
479
480/**
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200481 * read_persistent_clock - Return time from the persistent clock.
john stultz85240702007-05-08 00:27:59 -0700482 *
483 * Weak dummy function for arches that do not yet support it.
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200484 * Reads the time from the battery backed persistent clock.
485 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
john stultz85240702007-05-08 00:27:59 -0700486 *
487 * XXX - Do be sure to remove it once all arches implement it.
488 */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200489void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700490{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200491 ts->tv_sec = 0;
492 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700493}
494
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200495/**
496 * read_boot_clock - Return time of the system start.
497 *
498 * Weak dummy function for arches that do not yet support it.
499 * Function to read the exact time the system has been started.
500 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
501 *
502 * XXX - Do be sure to remove it once all arches implement it.
503 */
504void __attribute__((weak)) read_boot_clock(struct timespec *ts)
505{
506 ts->tv_sec = 0;
507 ts->tv_nsec = 0;
508}
509
john stultz85240702007-05-08 00:27:59 -0700510/*
511 * timekeeping_init - Initializes the clocksource and common timekeeping values
512 */
513void __init timekeeping_init(void)
514{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200515 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700516 unsigned long flags;
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200517 struct timespec now, boot;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200518
519 read_persistent_clock(&now);
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200520 read_boot_clock(&boot);
john stultz85240702007-05-08 00:27:59 -0700521
522 write_seqlock_irqsave(&xtime_lock, flags);
523
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700524 ntp_init();
john stultz85240702007-05-08 00:27:59 -0700525
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200526 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200527 if (clock->enable)
528 clock->enable(clock);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200529 timekeeper_setup_internals(clock);
john stultz85240702007-05-08 00:27:59 -0700530
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200531 xtime.tv_sec = now.tv_sec;
532 xtime.tv_nsec = now.tv_nsec;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200533 raw_time.tv_sec = 0;
534 raw_time.tv_nsec = 0;
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200535 if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
536 boot.tv_sec = xtime.tv_sec;
537 boot.tv_nsec = xtime.tv_nsec;
538 }
john stultz85240702007-05-08 00:27:59 -0700539 set_normalized_timespec(&wall_to_monotonic,
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200540 -boot.tv_sec, -boot.tv_nsec);
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200541 total_sleep_time.tv_sec = 0;
542 total_sleep_time.tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700543 write_sequnlock_irqrestore(&xtime_lock, flags);
544}
545
john stultz85240702007-05-08 00:27:59 -0700546/* time in seconds when suspend began */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200547static struct timespec timekeeping_suspend_time;
john stultz85240702007-05-08 00:27:59 -0700548
549/**
550 * timekeeping_resume - Resumes the generic timekeeping subsystem.
551 * @dev: unused
552 *
553 * This is for the generic clocksource timekeeping.
554 * xtime/wall_to_monotonic/jiffies/etc are
555 * still managed by arch specific suspend/resume code.
556 */
557static int timekeeping_resume(struct sys_device *dev)
558{
559 unsigned long flags;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200560 struct timespec ts;
561
562 read_persistent_clock(&ts);
john stultz85240702007-05-08 00:27:59 -0700563
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +0200564 clocksource_resume();
565
john stultz85240702007-05-08 00:27:59 -0700566 write_seqlock_irqsave(&xtime_lock, flags);
567
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200568 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
569 ts = timespec_sub(ts, timekeeping_suspend_time);
570 xtime = timespec_add_safe(xtime, ts);
571 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts);
572 total_sleep_time = timespec_add_safe(total_sleep_time, ts);
john stultz85240702007-05-08 00:27:59 -0700573 }
574 /* re-base the last cycle value */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200575 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
576 timekeeper.ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -0700577 timekeeping_suspended = 0;
578 write_sequnlock_irqrestore(&xtime_lock, flags);
579
580 touch_softlockup_watchdog();
581
582 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
583
584 /* Resume hrtimers */
585 hres_timers_resume();
586
587 return 0;
588}
589
590static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
591{
592 unsigned long flags;
593
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200594 read_persistent_clock(&timekeeping_suspend_time);
Thomas Gleixner3be90952007-09-16 15:36:43 +0200595
john stultz85240702007-05-08 00:27:59 -0700596 write_seqlock_irqsave(&xtime_lock, flags);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200597 timekeeping_forward_now();
john stultz85240702007-05-08 00:27:59 -0700598 timekeeping_suspended = 1;
john stultz85240702007-05-08 00:27:59 -0700599 write_sequnlock_irqrestore(&xtime_lock, flags);
600
601 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
602
603 return 0;
604}
605
606/* sysfs resume/suspend bits for timekeeping */
607static struct sysdev_class timekeeping_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100608 .name = "timekeeping",
john stultz85240702007-05-08 00:27:59 -0700609 .resume = timekeeping_resume,
610 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -0700611};
612
613static struct sys_device device_timer = {
614 .id = 0,
615 .cls = &timekeeping_sysclass,
616};
617
618static int __init timekeeping_init_device(void)
619{
620 int error = sysdev_class_register(&timekeeping_sysclass);
621 if (!error)
622 error = sysdev_register(&device_timer);
623 return error;
624}
625
626device_initcall(timekeeping_init_device);
627
628/*
629 * If the error is already larger, we look ahead even further
630 * to compensate for late or lost adjustments.
631 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200632static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
john stultz85240702007-05-08 00:27:59 -0700633 s64 *offset)
634{
635 s64 tick_error, i;
636 u32 look_ahead, adj;
637 s32 error2, mult;
638
639 /*
640 * Use the current error value to determine how much to look ahead.
641 * The larger the error the slower we adjust for it to avoid problems
642 * with losing too many ticks, otherwise we would overadjust and
643 * produce an even larger error. The smaller the adjustment the
644 * faster we try to adjust for it, as lost ticks can do less harm
Li Zefan3eb05672008-02-08 04:19:25 -0800645 * here. This is tuned so that an error of about 1 msec is adjusted
john stultz85240702007-05-08 00:27:59 -0700646 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
647 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200648 error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
john stultz85240702007-05-08 00:27:59 -0700649 error2 = abs(error2);
650 for (look_ahead = 0; error2 > 0; look_ahead++)
651 error2 >>= 2;
652
653 /*
654 * Now calculate the error in (1 << look_ahead) ticks, but first
655 * remove the single look ahead already included in the error.
656 */
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200657 tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200658 tick_error -= timekeeper.xtime_interval >> 1;
john stultz85240702007-05-08 00:27:59 -0700659 error = ((error - tick_error) >> look_ahead) + tick_error;
660
661 /* Finally calculate the adjustment shift value. */
662 i = *interval;
663 mult = 1;
664 if (error < 0) {
665 error = -error;
666 *interval = -*interval;
667 *offset = -*offset;
668 mult = -1;
669 }
670 for (adj = 0; error > i; adj++)
671 error >>= 1;
672
673 *interval <<= adj;
674 *offset <<= adj;
675 return mult << adj;
676}
677
678/*
679 * Adjust the multiplier to reduce the error value,
680 * this is optimized for the most common adjustments of -1,0,1,
681 * for other values we can do a bit more work.
682 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200683static void timekeeping_adjust(s64 offset)
john stultz85240702007-05-08 00:27:59 -0700684{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200685 s64 error, interval = timekeeper.cycle_interval;
john stultz85240702007-05-08 00:27:59 -0700686 int adj;
687
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200688 error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
john stultz85240702007-05-08 00:27:59 -0700689 if (error > interval) {
690 error >>= 2;
691 if (likely(error <= interval))
692 adj = 1;
693 else
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200694 adj = timekeeping_bigadjust(error, &interval, &offset);
john stultz85240702007-05-08 00:27:59 -0700695 } else if (error < -interval) {
696 error >>= 2;
697 if (likely(error >= -interval)) {
698 adj = -1;
699 interval = -interval;
700 offset = -offset;
701 } else
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200702 adj = timekeeping_bigadjust(error, &interval, &offset);
john stultz85240702007-05-08 00:27:59 -0700703 } else
704 return;
705
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200706 timekeeper.mult += adj;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200707 timekeeper.xtime_interval += interval;
708 timekeeper.xtime_nsec -= offset;
709 timekeeper.ntp_error -= (interval - offset) <<
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200710 timekeeper.ntp_error_shift;
john stultz85240702007-05-08 00:27:59 -0700711}
712
john stultza092ff02009-10-02 16:17:53 -0700713/**
714 * logarithmic_accumulation - shifted accumulation of cycles
715 *
716 * This functions accumulates a shifted interval of cycles into
717 * into a shifted interval nanoseconds. Allows for O(log) accumulation
718 * loop.
719 *
720 * Returns the unconsumed cycles.
721 */
722static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
723{
724 u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
725
726 /* If the offset is smaller then a shifted interval, do nothing */
727 if (offset < timekeeper.cycle_interval<<shift)
728 return offset;
729
730 /* Accumulate one shifted interval */
731 offset -= timekeeper.cycle_interval << shift;
732 timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift;
733
734 timekeeper.xtime_nsec += timekeeper.xtime_interval << shift;
735 while (timekeeper.xtime_nsec >= nsecps) {
736 timekeeper.xtime_nsec -= nsecps;
737 xtime.tv_sec++;
738 second_overflow();
739 }
740
741 /* Accumulate into raw time */
742 raw_time.tv_nsec += timekeeper.raw_interval << shift;;
743 while (raw_time.tv_nsec >= NSEC_PER_SEC) {
744 raw_time.tv_nsec -= NSEC_PER_SEC;
745 raw_time.tv_sec++;
746 }
747
748 /* Accumulate error between NTP and clock interval */
749 timekeeper.ntp_error += tick_length << shift;
750 timekeeper.ntp_error -= timekeeper.xtime_interval <<
751 (timekeeper.ntp_error_shift + shift);
752
753 return offset;
754}
755
john stultz85240702007-05-08 00:27:59 -0700756/**
757 * update_wall_time - Uses the current clocksource to increment the wall time
758 *
759 * Called from the timer interrupt, must hold a write on xtime_lock.
760 */
761void update_wall_time(void)
762{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200763 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700764 cycle_t offset;
john stultza092ff02009-10-02 16:17:53 -0700765 int shift = 0, maxshift;
john stultz85240702007-05-08 00:27:59 -0700766
767 /* Make sure we're fully resumed: */
768 if (unlikely(timekeeping_suspended))
769 return;
770
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200771 clock = timekeeper.clock;
john stultz85240702007-05-08 00:27:59 -0700772#ifdef CONFIG_GENERIC_TIME
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200773 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
john stultz85240702007-05-08 00:27:59 -0700774#else
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200775 offset = timekeeper.cycle_interval;
john stultz85240702007-05-08 00:27:59 -0700776#endif
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200777 timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
john stultz85240702007-05-08 00:27:59 -0700778
john stultza092ff02009-10-02 16:17:53 -0700779 /*
780 * With NO_HZ we may have to accumulate many cycle_intervals
781 * (think "ticks") worth of time at once. To do this efficiently,
782 * we calculate the largest doubling multiple of cycle_intervals
783 * that is smaller then the offset. We then accumulate that
784 * chunk in one go, and then try to consume the next smaller
785 * doubled multiple.
john stultz85240702007-05-08 00:27:59 -0700786 */
john stultza092ff02009-10-02 16:17:53 -0700787 shift = ilog2(offset) - ilog2(timekeeper.cycle_interval);
788 shift = max(0, shift);
789 /* Bound shift to one less then what overflows tick_length */
790 maxshift = (8*sizeof(tick_length) - (ilog2(tick_length)+1)) - 1;
791 shift = min(shift, maxshift);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200792 while (offset >= timekeeper.cycle_interval) {
john stultza092ff02009-10-02 16:17:53 -0700793 offset = logarithmic_accumulation(offset, shift);
794 shift--;
john stultz85240702007-05-08 00:27:59 -0700795 }
796
797 /* correct the clock when NTP error is too big */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200798 timekeeping_adjust(offset);
john stultz85240702007-05-08 00:27:59 -0700799
john stultz6c9bacb2008-12-01 18:34:41 -0800800 /*
801 * Since in the loop above, we accumulate any amount of time
802 * in xtime_nsec over a second into xtime.tv_sec, its possible for
803 * xtime_nsec to be fairly small after the loop. Further, if we're
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200804 * slightly speeding the clocksource up in timekeeping_adjust(),
john stultz6c9bacb2008-12-01 18:34:41 -0800805 * its possible the required corrective factor to xtime_nsec could
806 * cause it to underflow.
807 *
808 * Now, we cannot simply roll the accumulated second back, since
809 * the NTP subsystem has been notified via second_overflow. So
810 * instead we push xtime_nsec forward by the amount we underflowed,
811 * and add that amount into the error.
812 *
813 * We'll correct this error next time through this function, when
814 * xtime_nsec is not as small.
815 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200816 if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
817 s64 neg = -(s64)timekeeper.xtime_nsec;
818 timekeeper.xtime_nsec = 0;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200819 timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
john stultz6c9bacb2008-12-01 18:34:41 -0800820 }
821
Roman Zippel5cd1c9c2008-09-22 14:42:43 -0700822 /* store full nanoseconds into xtime after rounding it up and
823 * add the remainder to the error difference.
824 */
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200825 xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
826 timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
827 timekeeper.ntp_error += timekeeper.xtime_nsec <<
828 timekeeper.ntp_error_shift;
john stultz85240702007-05-08 00:27:59 -0700829
830 /* check to see if there is a new clocksource to use */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200831 update_vsyscall(&xtime, timekeeper.clock);
john stultz85240702007-05-08 00:27:59 -0700832}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700833
834/**
835 * getboottime - Return the real time of system boot.
836 * @ts: pointer to the timespec to be set
837 *
838 * Returns the time of day in a timespec.
839 *
840 * This is based on the wall_to_monotonic offset and the total suspend
841 * time. Calls to settimeofday will affect the value returned (which
842 * basically means that however wrong your real time clock is at boot time,
843 * you get the right time here).
844 */
845void getboottime(struct timespec *ts)
846{
Hiroshi Shimamoto36d47482009-08-25 15:08:30 +0900847 struct timespec boottime = {
848 .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec,
849 .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec
850 };
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200851
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200852 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700853}
854
855/**
856 * monotonic_to_bootbased - Convert the monotonic time to boot based.
857 * @ts: pointer to the timespec to be converted
858 */
859void monotonic_to_bootbased(struct timespec *ts)
860{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200861 *ts = timespec_add_safe(*ts, total_sleep_time);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700862}
john stultz2c6b47d2007-07-24 17:47:43 -0700863
john stultz17c38b72007-07-24 18:38:34 -0700864unsigned long get_seconds(void)
865{
john stultz7bc7d632009-10-02 16:24:15 -0700866 return xtime.tv_sec;
john stultz17c38b72007-07-24 18:38:34 -0700867}
868EXPORT_SYMBOL(get_seconds);
869
john stultzda15cfd2009-08-19 19:13:34 -0700870struct timespec __current_kernel_time(void)
871{
john stultz7bc7d632009-10-02 16:24:15 -0700872 return xtime;
john stultzda15cfd2009-08-19 19:13:34 -0700873}
john stultz17c38b72007-07-24 18:38:34 -0700874
john stultz2c6b47d2007-07-24 17:47:43 -0700875struct timespec current_kernel_time(void)
876{
877 struct timespec now;
878 unsigned long seq;
879
880 do {
881 seq = read_seqbegin(&xtime_lock);
john stultz7bc7d632009-10-02 16:24:15 -0700882 now = xtime;
john stultz2c6b47d2007-07-24 17:47:43 -0700883 } while (read_seqretry(&xtime_lock, seq));
884
885 return now;
886}
john stultz2c6b47d2007-07-24 17:47:43 -0700887EXPORT_SYMBOL(current_kernel_time);
john stultzda15cfd2009-08-19 19:13:34 -0700888
889struct timespec get_monotonic_coarse(void)
890{
891 struct timespec now, mono;
892 unsigned long seq;
893
894 do {
895 seq = read_seqbegin(&xtime_lock);
john stultz7bc7d632009-10-02 16:24:15 -0700896 now = xtime;
john stultzda15cfd2009-08-19 19:13:34 -0700897 mono = wall_to_monotonic;
898 } while (read_seqretry(&xtime_lock, seq));
899
900 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
901 now.tv_nsec + mono.tv_nsec);
902 return now;
903}