blob: 49010d822f725b47726742fa7e1b45aad076ef90 [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 */
John Stultz0fb86b02010-07-13 17:56:26 -0700156static struct timespec xtime __attribute__ ((aligned (16)));
157static struct 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
John Stultz31089c12009-08-14 15:47:18 +0200168/* must hold xtime_lock */
169void timekeeping_leap_insert(int leapsecond)
170{
171 xtime.tv_sec += leapsecond;
172 wall_to_monotonic.tv_sec -= leapsecond;
John Stultz76158562010-07-13 17:56:23 -0700173 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
174 timekeeper.mult);
John Stultz31089c12009-08-14 15:47:18 +0200175}
john stultz85240702007-05-08 00:27:59 -0700176
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
John Stultz76158562010-07-13 17:56:23 -0700330 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
331 timekeeper.mult);
john stultz85240702007-05-08 00:27:59 -0700332
333 write_sequnlock_irqrestore(&xtime_lock, flags);
334
335 /* signal hrtimers about time change */
336 clock_was_set();
337
338 return 0;
339}
340
341EXPORT_SYMBOL(do_settimeofday);
342
343/**
344 * change_clocksource - Swaps clocksources if a new one is available
345 *
346 * Accumulates current time interval and initializes new clocksource
347 */
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200348static int change_clocksource(void *data)
john stultz85240702007-05-08 00:27:59 -0700349{
Magnus Damm4614e6a2009-04-21 12:24:02 -0700350 struct clocksource *new, *old;
john stultz85240702007-05-08 00:27:59 -0700351
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200352 new = (struct clocksource *) data;
john stultz85240702007-05-08 00:27:59 -0700353
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200354 timekeeping_forward_now();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200355 if (!new->enable || new->enable(new) == 0) {
356 old = timekeeper.clock;
357 timekeeper_setup_internals(new);
358 if (old->disable)
359 old->disable(old);
360 }
361 return 0;
362}
john stultz85240702007-05-08 00:27:59 -0700363
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200364/**
365 * timekeeping_notify - Install a new clock source
366 * @clock: pointer to the clock source
367 *
368 * This function is called from clocksource.c after a new, better clock
369 * source has been registered. The caller holds the clocksource_mutex.
370 */
371void timekeeping_notify(struct clocksource *clock)
372{
373 if (timekeeper.clock == clock)
Magnus Damm4614e6a2009-04-21 12:24:02 -0700374 return;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200375 stop_machine(change_clocksource, clock, NULL);
john stultz85240702007-05-08 00:27:59 -0700376 tick_clock_notify();
john stultz85240702007-05-08 00:27:59 -0700377}
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200378
Thomas Gleixnera40f2622009-07-07 13:00:31 +0200379/**
380 * ktime_get_real - get the real (wall-) time in ktime_t format
381 *
382 * returns the time in ktime_t format
383 */
384ktime_t ktime_get_real(void)
385{
386 struct timespec now;
387
388 getnstimeofday(&now);
389
390 return timespec_to_ktime(now);
391}
392EXPORT_SYMBOL_GPL(ktime_get_real);
john stultz85240702007-05-08 00:27:59 -0700393
394/**
John Stultz2d422442008-08-20 16:37:30 -0700395 * getrawmonotonic - Returns the raw monotonic time in a timespec
396 * @ts: pointer to the timespec to be set
397 *
398 * Returns the raw monotonic time (completely un-modified by ntp)
399 */
400void getrawmonotonic(struct timespec *ts)
401{
402 unsigned long seq;
403 s64 nsecs;
John Stultz2d422442008-08-20 16:37:30 -0700404
405 do {
406 seq = read_seqbegin(&xtime_lock);
Martin Schwidefsky2ba2a302009-08-14 15:47:29 +0200407 nsecs = timekeeping_get_ns_raw();
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200408 *ts = raw_time;
John Stultz2d422442008-08-20 16:37:30 -0700409
410 } while (read_seqretry(&xtime_lock, seq));
411
412 timespec_add_ns(ts, nsecs);
413}
414EXPORT_SYMBOL(getrawmonotonic);
415
416
417/**
Li Zefancf4fc6c2008-02-08 04:19:24 -0800418 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
john stultz85240702007-05-08 00:27:59 -0700419 */
Li Zefancf4fc6c2008-02-08 04:19:24 -0800420int timekeeping_valid_for_hres(void)
john stultz85240702007-05-08 00:27:59 -0700421{
422 unsigned long seq;
423 int ret;
424
425 do {
426 seq = read_seqbegin(&xtime_lock);
427
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200428 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
john stultz85240702007-05-08 00:27:59 -0700429
430 } while (read_seqretry(&xtime_lock, seq));
431
432 return ret;
433}
434
435/**
Jon Hunter98962462009-08-18 12:45:10 -0500436 * timekeeping_max_deferment - Returns max time the clocksource can be deferred
437 *
438 * Caller must observe xtime_lock via read_seqbegin/read_seqretry to
439 * ensure that the clocksource does not change!
440 */
441u64 timekeeping_max_deferment(void)
442{
443 return timekeeper.clock->max_idle_ns;
444}
445
446/**
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200447 * read_persistent_clock - Return time from the persistent clock.
john stultz85240702007-05-08 00:27:59 -0700448 *
449 * Weak dummy function for arches that do not yet support it.
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200450 * Reads the time from the battery backed persistent clock.
451 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
john stultz85240702007-05-08 00:27:59 -0700452 *
453 * XXX - Do be sure to remove it once all arches implement it.
454 */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200455void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
john stultz85240702007-05-08 00:27:59 -0700456{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200457 ts->tv_sec = 0;
458 ts->tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700459}
460
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200461/**
462 * read_boot_clock - Return time of the system start.
463 *
464 * Weak dummy function for arches that do not yet support it.
465 * Function to read the exact time the system has been started.
466 * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported.
467 *
468 * XXX - Do be sure to remove it once all arches implement it.
469 */
470void __attribute__((weak)) read_boot_clock(struct timespec *ts)
471{
472 ts->tv_sec = 0;
473 ts->tv_nsec = 0;
474}
475
john stultz85240702007-05-08 00:27:59 -0700476/*
477 * timekeeping_init - Initializes the clocksource and common timekeeping values
478 */
479void __init timekeeping_init(void)
480{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200481 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700482 unsigned long flags;
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200483 struct timespec now, boot;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200484
485 read_persistent_clock(&now);
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200486 read_boot_clock(&boot);
john stultz85240702007-05-08 00:27:59 -0700487
488 write_seqlock_irqsave(&xtime_lock, flags);
489
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700490 ntp_init();
john stultz85240702007-05-08 00:27:59 -0700491
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200492 clock = clocksource_default_clock();
Martin Schwidefskya0f7d482009-08-14 15:47:19 +0200493 if (clock->enable)
494 clock->enable(clock);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200495 timekeeper_setup_internals(clock);
john stultz85240702007-05-08 00:27:59 -0700496
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200497 xtime.tv_sec = now.tv_sec;
498 xtime.tv_nsec = now.tv_nsec;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200499 raw_time.tv_sec = 0;
500 raw_time.tv_nsec = 0;
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200501 if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
502 boot.tv_sec = xtime.tv_sec;
503 boot.tv_nsec = xtime.tv_nsec;
504 }
john stultz85240702007-05-08 00:27:59 -0700505 set_normalized_timespec(&wall_to_monotonic,
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200506 -boot.tv_sec, -boot.tv_nsec);
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200507 total_sleep_time.tv_sec = 0;
508 total_sleep_time.tv_nsec = 0;
john stultz85240702007-05-08 00:27:59 -0700509 write_sequnlock_irqrestore(&xtime_lock, flags);
510}
511
john stultz85240702007-05-08 00:27:59 -0700512/* time in seconds when suspend began */
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200513static struct timespec timekeeping_suspend_time;
john stultz85240702007-05-08 00:27:59 -0700514
515/**
516 * timekeeping_resume - Resumes the generic timekeeping subsystem.
517 * @dev: unused
518 *
519 * This is for the generic clocksource timekeeping.
520 * xtime/wall_to_monotonic/jiffies/etc are
521 * still managed by arch specific suspend/resume code.
522 */
523static int timekeeping_resume(struct sys_device *dev)
524{
525 unsigned long flags;
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200526 struct timespec ts;
527
528 read_persistent_clock(&ts);
john stultz85240702007-05-08 00:27:59 -0700529
Thomas Gleixnerd10ff3f2007-05-14 11:10:02 +0200530 clocksource_resume();
531
john stultz85240702007-05-08 00:27:59 -0700532 write_seqlock_irqsave(&xtime_lock, flags);
533
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200534 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
535 ts = timespec_sub(ts, timekeeping_suspend_time);
John Stultzce3bf7a2010-07-13 17:56:19 -0700536 xtime = timespec_add(xtime, ts);
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200537 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts);
John Stultzce3bf7a2010-07-13 17:56:19 -0700538 total_sleep_time = timespec_add(total_sleep_time, ts);
john stultz85240702007-05-08 00:27:59 -0700539 }
540 /* re-base the last cycle value */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200541 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
542 timekeeper.ntp_error = 0;
john stultz85240702007-05-08 00:27:59 -0700543 timekeeping_suspended = 0;
544 write_sequnlock_irqrestore(&xtime_lock, flags);
545
546 touch_softlockup_watchdog();
547
548 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
549
550 /* Resume hrtimers */
551 hres_timers_resume();
552
553 return 0;
554}
555
556static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
557{
558 unsigned long flags;
559
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200560 read_persistent_clock(&timekeeping_suspend_time);
Thomas Gleixner3be90952007-09-16 15:36:43 +0200561
john stultz85240702007-05-08 00:27:59 -0700562 write_seqlock_irqsave(&xtime_lock, flags);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200563 timekeeping_forward_now();
john stultz85240702007-05-08 00:27:59 -0700564 timekeeping_suspended = 1;
john stultz85240702007-05-08 00:27:59 -0700565 write_sequnlock_irqrestore(&xtime_lock, flags);
566
567 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
Magnus Dammc54a42b2010-02-02 14:41:41 -0800568 clocksource_suspend();
john stultz85240702007-05-08 00:27:59 -0700569
570 return 0;
571}
572
573/* sysfs resume/suspend bits for timekeeping */
574static struct sysdev_class timekeeping_sysclass = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +0100575 .name = "timekeeping",
john stultz85240702007-05-08 00:27:59 -0700576 .resume = timekeeping_resume,
577 .suspend = timekeeping_suspend,
john stultz85240702007-05-08 00:27:59 -0700578};
579
580static struct sys_device device_timer = {
581 .id = 0,
582 .cls = &timekeeping_sysclass,
583};
584
585static int __init timekeeping_init_device(void)
586{
587 int error = sysdev_class_register(&timekeeping_sysclass);
588 if (!error)
589 error = sysdev_register(&device_timer);
590 return error;
591}
592
593device_initcall(timekeeping_init_device);
594
595/*
596 * If the error is already larger, we look ahead even further
597 * to compensate for late or lost adjustments.
598 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200599static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
john stultz85240702007-05-08 00:27:59 -0700600 s64 *offset)
601{
602 s64 tick_error, i;
603 u32 look_ahead, adj;
604 s32 error2, mult;
605
606 /*
607 * Use the current error value to determine how much to look ahead.
608 * The larger the error the slower we adjust for it to avoid problems
609 * with losing too many ticks, otherwise we would overadjust and
610 * produce an even larger error. The smaller the adjustment the
611 * faster we try to adjust for it, as lost ticks can do less harm
Li Zefan3eb05672008-02-08 04:19:25 -0800612 * here. This is tuned so that an error of about 1 msec is adjusted
john stultz85240702007-05-08 00:27:59 -0700613 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
614 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200615 error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
john stultz85240702007-05-08 00:27:59 -0700616 error2 = abs(error2);
617 for (look_ahead = 0; error2 > 0; look_ahead++)
618 error2 >>= 2;
619
620 /*
621 * Now calculate the error in (1 << look_ahead) ticks, but first
622 * remove the single look ahead already included in the error.
623 */
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200624 tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200625 tick_error -= timekeeper.xtime_interval >> 1;
john stultz85240702007-05-08 00:27:59 -0700626 error = ((error - tick_error) >> look_ahead) + tick_error;
627
628 /* Finally calculate the adjustment shift value. */
629 i = *interval;
630 mult = 1;
631 if (error < 0) {
632 error = -error;
633 *interval = -*interval;
634 *offset = -*offset;
635 mult = -1;
636 }
637 for (adj = 0; error > i; adj++)
638 error >>= 1;
639
640 *interval <<= adj;
641 *offset <<= adj;
642 return mult << adj;
643}
644
645/*
646 * Adjust the multiplier to reduce the error value,
647 * this is optimized for the most common adjustments of -1,0,1,
648 * for other values we can do a bit more work.
649 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200650static void timekeeping_adjust(s64 offset)
john stultz85240702007-05-08 00:27:59 -0700651{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200652 s64 error, interval = timekeeper.cycle_interval;
john stultz85240702007-05-08 00:27:59 -0700653 int adj;
654
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200655 error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
john stultz85240702007-05-08 00:27:59 -0700656 if (error > interval) {
657 error >>= 2;
658 if (likely(error <= interval))
659 adj = 1;
660 else
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200661 adj = timekeeping_bigadjust(error, &interval, &offset);
john stultz85240702007-05-08 00:27:59 -0700662 } else if (error < -interval) {
663 error >>= 2;
664 if (likely(error >= -interval)) {
665 adj = -1;
666 interval = -interval;
667 offset = -offset;
668 } else
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200669 adj = timekeeping_bigadjust(error, &interval, &offset);
john stultz85240702007-05-08 00:27:59 -0700670 } else
671 return;
672
Martin Schwidefsky0a544192009-08-14 15:47:28 +0200673 timekeeper.mult += adj;
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200674 timekeeper.xtime_interval += interval;
675 timekeeper.xtime_nsec -= offset;
676 timekeeper.ntp_error -= (interval - offset) <<
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200677 timekeeper.ntp_error_shift;
john stultz85240702007-05-08 00:27:59 -0700678}
679
Linus Torvalds83f57a12009-12-22 14:10:37 -0800680
john stultz85240702007-05-08 00:27:59 -0700681/**
john stultza092ff02009-10-02 16:17:53 -0700682 * logarithmic_accumulation - shifted accumulation of cycles
683 *
684 * This functions accumulates a shifted interval of cycles into
685 * into a shifted interval nanoseconds. Allows for O(log) accumulation
686 * loop.
687 *
688 * Returns the unconsumed cycles.
689 */
690static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
691{
692 u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
Jason Wesseldeda2e82010-08-09 14:20:09 -0700693 u64 raw_nsecs;
john stultza092ff02009-10-02 16:17:53 -0700694
695 /* If the offset is smaller then a shifted interval, do nothing */
696 if (offset < timekeeper.cycle_interval<<shift)
697 return offset;
698
699 /* Accumulate one shifted interval */
700 offset -= timekeeper.cycle_interval << shift;
701 timekeeper.clock->cycle_last += timekeeper.cycle_interval << shift;
702
703 timekeeper.xtime_nsec += timekeeper.xtime_interval << shift;
704 while (timekeeper.xtime_nsec >= nsecps) {
705 timekeeper.xtime_nsec -= nsecps;
706 xtime.tv_sec++;
707 second_overflow();
708 }
709
Jason Wesseldeda2e82010-08-09 14:20:09 -0700710 /* Accumulate raw time */
711 raw_nsecs = timekeeper.raw_interval << shift;
712 raw_nsecs += raw_time.tv_nsec;
John Stultzc7dcf872010-08-13 11:30:58 -0700713 if (raw_nsecs >= NSEC_PER_SEC) {
714 u64 raw_secs = raw_nsecs;
715 raw_nsecs = do_div(raw_secs, NSEC_PER_SEC);
716 raw_time.tv_sec += raw_secs;
john stultza092ff02009-10-02 16:17:53 -0700717 }
Jason Wesseldeda2e82010-08-09 14:20:09 -0700718 raw_time.tv_nsec = raw_nsecs;
john stultza092ff02009-10-02 16:17:53 -0700719
720 /* Accumulate error between NTP and clock interval */
721 timekeeper.ntp_error += tick_length << shift;
722 timekeeper.ntp_error -= timekeeper.xtime_interval <<
723 (timekeeper.ntp_error_shift + shift);
724
725 return offset;
726}
727
Linus Torvalds83f57a12009-12-22 14:10:37 -0800728
john stultz85240702007-05-08 00:27:59 -0700729/**
730 * update_wall_time - Uses the current clocksource to increment the wall time
731 *
732 * Called from the timer interrupt, must hold a write on xtime_lock.
733 */
734void update_wall_time(void)
735{
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200736 struct clocksource *clock;
john stultz85240702007-05-08 00:27:59 -0700737 cycle_t offset;
john stultza092ff02009-10-02 16:17:53 -0700738 int shift = 0, maxshift;
john stultz85240702007-05-08 00:27:59 -0700739
740 /* Make sure we're fully resumed: */
741 if (unlikely(timekeeping_suspended))
742 return;
743
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200744 clock = timekeeper.clock;
John Stultz592913e2010-07-13 17:56:20 -0700745
746#ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200747 offset = timekeeper.cycle_interval;
John Stultz592913e2010-07-13 17:56:20 -0700748#else
749 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
john stultz85240702007-05-08 00:27:59 -0700750#endif
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200751 timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
john stultz85240702007-05-08 00:27:59 -0700752
john stultza092ff02009-10-02 16:17:53 -0700753 /*
754 * With NO_HZ we may have to accumulate many cycle_intervals
755 * (think "ticks") worth of time at once. To do this efficiently,
756 * we calculate the largest doubling multiple of cycle_intervals
757 * that is smaller then the offset. We then accumulate that
758 * chunk in one go, and then try to consume the next smaller
759 * doubled multiple.
john stultz85240702007-05-08 00:27:59 -0700760 */
john stultza092ff02009-10-02 16:17:53 -0700761 shift = ilog2(offset) - ilog2(timekeeper.cycle_interval);
762 shift = max(0, shift);
763 /* Bound shift to one less then what overflows tick_length */
764 maxshift = (8*sizeof(tick_length) - (ilog2(tick_length)+1)) - 1;
765 shift = min(shift, maxshift);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200766 while (offset >= timekeeper.cycle_interval) {
john stultza092ff02009-10-02 16:17:53 -0700767 offset = logarithmic_accumulation(offset, shift);
John Stultz830ec042010-03-18 14:47:30 -0700768 if(offset < timekeeper.cycle_interval<<shift)
769 shift--;
john stultz85240702007-05-08 00:27:59 -0700770 }
771
772 /* correct the clock when NTP error is too big */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200773 timekeeping_adjust(offset);
john stultz85240702007-05-08 00:27:59 -0700774
john stultz6c9bacb2008-12-01 18:34:41 -0800775 /*
776 * Since in the loop above, we accumulate any amount of time
777 * in xtime_nsec over a second into xtime.tv_sec, its possible for
778 * xtime_nsec to be fairly small after the loop. Further, if we're
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200779 * slightly speeding the clocksource up in timekeeping_adjust(),
john stultz6c9bacb2008-12-01 18:34:41 -0800780 * its possible the required corrective factor to xtime_nsec could
781 * cause it to underflow.
782 *
783 * Now, we cannot simply roll the accumulated second back, since
784 * the NTP subsystem has been notified via second_overflow. So
785 * instead we push xtime_nsec forward by the amount we underflowed,
786 * and add that amount into the error.
787 *
788 * We'll correct this error next time through this function, when
789 * xtime_nsec is not as small.
790 */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200791 if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
792 s64 neg = -(s64)timekeeper.xtime_nsec;
793 timekeeper.xtime_nsec = 0;
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200794 timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
john stultz6c9bacb2008-12-01 18:34:41 -0800795 }
796
John Stultz6a867a32010-04-06 14:30:51 -0700797
798 /*
799 * Store full nanoseconds into xtime after rounding it up and
Roman Zippel5cd1c9c2008-09-22 14:42:43 -0700800 * add the remainder to the error difference.
801 */
Martin Schwidefsky23ce7212009-08-14 15:47:27 +0200802 xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
803 timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
804 timekeeper.ntp_error += timekeeper.xtime_nsec <<
805 timekeeper.ntp_error_shift;
john stultz85240702007-05-08 00:27:59 -0700806
John Stultz6a867a32010-04-06 14:30:51 -0700807 /*
808 * Finally, make sure that after the rounding
809 * xtime.tv_nsec isn't larger then NSEC_PER_SEC
810 */
811 if (unlikely(xtime.tv_nsec >= NSEC_PER_SEC)) {
812 xtime.tv_nsec -= NSEC_PER_SEC;
813 xtime.tv_sec++;
814 second_overflow();
815 }
Linus Torvalds83f57a12009-12-22 14:10:37 -0800816
john stultz85240702007-05-08 00:27:59 -0700817 /* check to see if there is a new clocksource to use */
John Stultz76158562010-07-13 17:56:23 -0700818 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
819 timekeeper.mult);
john stultz85240702007-05-08 00:27:59 -0700820}
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700821
822/**
823 * getboottime - Return the real time of system boot.
824 * @ts: pointer to the timespec to be set
825 *
826 * Returns the time of day in a timespec.
827 *
828 * This is based on the wall_to_monotonic offset and the total suspend
829 * time. Calls to settimeofday will affect the value returned (which
830 * basically means that however wrong your real time clock is at boot time,
831 * you get the right time here).
832 */
833void getboottime(struct timespec *ts)
834{
Hiroshi Shimamoto36d47482009-08-25 15:08:30 +0900835 struct timespec boottime = {
836 .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec,
837 .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec
838 };
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200839
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200840 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700841}
Jason Wangc93d89f2010-01-27 19:13:40 +0800842EXPORT_SYMBOL_GPL(getboottime);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700843
844/**
845 * monotonic_to_bootbased - Convert the monotonic time to boot based.
846 * @ts: pointer to the timespec to be converted
847 */
848void monotonic_to_bootbased(struct timespec *ts)
849{
John Stultzce3bf7a2010-07-13 17:56:19 -0700850 *ts = timespec_add(*ts, total_sleep_time);
Tomas Janousek7c3f1a52007-07-15 23:39:41 -0700851}
Jason Wangc93d89f2010-01-27 19:13:40 +0800852EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
john stultz2c6b47d2007-07-24 17:47:43 -0700853
john stultz17c38b72007-07-24 18:38:34 -0700854unsigned long get_seconds(void)
855{
John Stultz6a867a32010-04-06 14:30:51 -0700856 return xtime.tv_sec;
john stultz17c38b72007-07-24 18:38:34 -0700857}
858EXPORT_SYMBOL(get_seconds);
859
john stultzda15cfd2009-08-19 19:13:34 -0700860struct timespec __current_kernel_time(void)
861{
John Stultz6a867a32010-04-06 14:30:51 -0700862 return xtime;
john stultzda15cfd2009-08-19 19:13:34 -0700863}
john stultz17c38b72007-07-24 18:38:34 -0700864
John Stultz8ab43512010-07-13 17:56:25 -0700865struct timespec __get_wall_to_monotonic(void)
866{
867 return wall_to_monotonic;
868}
869
john stultz2c6b47d2007-07-24 17:47:43 -0700870struct timespec current_kernel_time(void)
871{
872 struct timespec now;
873 unsigned long seq;
874
875 do {
876 seq = read_seqbegin(&xtime_lock);
Linus Torvalds83f57a12009-12-22 14:10:37 -0800877
John Stultz6a867a32010-04-06 14:30:51 -0700878 now = xtime;
john stultz2c6b47d2007-07-24 17:47:43 -0700879 } while (read_seqretry(&xtime_lock, seq));
880
881 return now;
882}
john stultz2c6b47d2007-07-24 17:47:43 -0700883EXPORT_SYMBOL(current_kernel_time);
john stultzda15cfd2009-08-19 19:13:34 -0700884
885struct timespec get_monotonic_coarse(void)
886{
887 struct timespec now, mono;
888 unsigned long seq;
889
890 do {
891 seq = read_seqbegin(&xtime_lock);
Linus Torvalds83f57a12009-12-22 14:10:37 -0800892
John Stultz6a867a32010-04-06 14:30:51 -0700893 now = xtime;
john stultzda15cfd2009-08-19 19:13:34 -0700894 mono = wall_to_monotonic;
895 } while (read_seqretry(&xtime_lock, seq));
896
897 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
898 now.tv_nsec + mono.tv_nsec);
899 return now;
900}