clocksource: introduce CLOCK_MONOTONIC_RAW
[deliverable/linux.git] / kernel / time / timekeeping.c
CommitLineData
8524070b 1/*
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
22
23/*
24 * This read-write spinlock protects us from races in SMP while
25 * playing with xtime and avenrun.
26 */
ba2a631b 27__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
8524070b 28
29
30/*
31 * The current time
32 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
33 * for sub jiffie times) to get to monotonic time. Monotonic is pegged
34 * at zero at system boot time, so wall_to_monotonic will be negative,
35 * however, we will ALWAYS keep the tv_nsec part positive so we can use
36 * the usual normalization.
7c3f1a57
TJ
37 *
38 * wall_to_monotonic is moved after resume from suspend for the monotonic
39 * time not to jump. We need to add total_sleep_time to wall_to_monotonic
40 * to get the real boot based time offset.
41 *
42 * - wall_to_monotonic is no longer the boot time, getboottime must be
43 * used instead.
8524070b 44 */
45struct timespec xtime __attribute__ ((aligned (16)));
46struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
7c3f1a57 47static unsigned long total_sleep_time; /* seconds */
8524070b 48
17c38b74 49static struct timespec xtime_cache __attribute__ ((aligned (16)));
1001d0a9 50void update_xtime_cache(u64 nsec)
17c38b74 51{
52 xtime_cache = xtime;
53 timespec_add_ns(&xtime_cache, nsec);
54}
17c38b74 55
7dffa3c6 56struct clocksource *clock;
8524070b 57
58
59#ifdef CONFIG_GENERIC_TIME
60/**
9a055117 61 * clocksource_forward_now - update clock to the current time
8524070b 62 *
9a055117
RZ
63 * Forward the current clock to update its state since the last call to
64 * update_wall_time(). This is useful before significant clock changes,
65 * as it avoids having to deal with this time offset explicitly.
8524070b 66 */
9a055117 67static void clocksource_forward_now(void)
8524070b 68{
69 cycle_t cycle_now, cycle_delta;
9a055117 70 s64 nsec;
8524070b 71
8524070b 72 cycle_now = clocksource_read(clock);
8524070b 73 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
9a055117 74 clock->cycle_last = cycle_now;
8524070b 75
9a055117
RZ
76 nsec = cyc2ns(clock, cycle_delta);
77 timespec_add_ns(&xtime, nsec);
2d42244a
JS
78
79 nsec = ((s64)cycle_delta * clock->mult_orig) >> clock->shift;
80 clock->raw_time.tv_nsec += nsec;
8524070b 81}
82
83/**
efd9ac86 84 * getnstimeofday - Returns the time of day in a timespec
8524070b 85 * @ts: pointer to the timespec to be set
86 *
efd9ac86 87 * Returns the time of day in a timespec.
8524070b 88 */
efd9ac86 89void getnstimeofday(struct timespec *ts)
8524070b 90{
9a055117 91 cycle_t cycle_now, cycle_delta;
8524070b 92 unsigned long seq;
93 s64 nsecs;
94
95 do {
96 seq = read_seqbegin(&xtime_lock);
97
98 *ts = xtime;
9a055117
RZ
99
100 /* read clocksource: */
101 cycle_now = clocksource_read(clock);
102
103 /* calculate the delta since the last update_wall_time: */
104 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
105
106 /* convert to nanoseconds: */
107 nsecs = cyc2ns(clock, cycle_delta);
8524070b 108
109 } while (read_seqretry(&xtime_lock, seq));
110
111 timespec_add_ns(ts, nsecs);
112}
113
8524070b 114EXPORT_SYMBOL(getnstimeofday);
115
116/**
117 * do_gettimeofday - Returns the time of day in a timeval
118 * @tv: pointer to the timeval to be set
119 *
efd9ac86 120 * NOTE: Users should be converted to using getnstimeofday()
8524070b 121 */
122void do_gettimeofday(struct timeval *tv)
123{
124 struct timespec now;
125
efd9ac86 126 getnstimeofday(&now);
8524070b 127 tv->tv_sec = now.tv_sec;
128 tv->tv_usec = now.tv_nsec/1000;
129}
130
131EXPORT_SYMBOL(do_gettimeofday);
132/**
133 * do_settimeofday - Sets the time of day
134 * @tv: pointer to the timespec variable containing the new time
135 *
136 * Sets the time of day to the new time and update NTP and notify hrtimers
137 */
138int do_settimeofday(struct timespec *tv)
139{
9a055117 140 struct timespec ts_delta;
8524070b 141 unsigned long flags;
8524070b 142
143 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
144 return -EINVAL;
145
146 write_seqlock_irqsave(&xtime_lock, flags);
147
9a055117
RZ
148 clocksource_forward_now();
149
150 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
151 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
152 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
8524070b 153
9a055117 154 xtime = *tv;
8524070b 155
1001d0a9 156 update_xtime_cache(0);
8524070b 157
158 clock->error = 0;
159 ntp_clear();
160
161 update_vsyscall(&xtime, clock);
162
163 write_sequnlock_irqrestore(&xtime_lock, flags);
164
165 /* signal hrtimers about time change */
166 clock_was_set();
167
168 return 0;
169}
170
171EXPORT_SYMBOL(do_settimeofday);
172
173/**
174 * change_clocksource - Swaps clocksources if a new one is available
175 *
176 * Accumulates current time interval and initializes new clocksource
177 */
178static void change_clocksource(void)
179{
180 struct clocksource *new;
8524070b 181
182 new = clocksource_get_next();
183
184 if (clock == new)
185 return;
186
9a055117 187 clocksource_forward_now();
8524070b 188
2d42244a
JS
189 new->raw_time = clock->raw_time;
190
8524070b 191 clock = new;
9a055117
RZ
192 clock->cycle_last = 0;
193 clock->cycle_last = clocksource_read(new);
8524070b 194 clock->error = 0;
195 clock->xtime_nsec = 0;
10a398d0 196 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
8524070b 197
198 tick_clock_notify();
199
92896bd9
LT
200 /*
201 * We're holding xtime lock and waking up klogd would deadlock
202 * us on enqueue. So no printing!
8524070b 203 printk(KERN_INFO "Time: %s clocksource has been installed.\n",
204 clock->name);
92896bd9 205 */
8524070b 206}
207#else
9a055117 208static inline void clocksource_forward_now(void) { }
8524070b 209static inline void change_clocksource(void) { }
210#endif
211
2d42244a
JS
212/**
213 * getrawmonotonic - Returns the raw monotonic time in a timespec
214 * @ts: pointer to the timespec to be set
215 *
216 * Returns the raw monotonic time (completely un-modified by ntp)
217 */
218void getrawmonotonic(struct timespec *ts)
219{
220 unsigned long seq;
221 s64 nsecs;
222 cycle_t cycle_now, cycle_delta;
223
224 do {
225 seq = read_seqbegin(&xtime_lock);
226
227 /* read clocksource: */
228 cycle_now = clocksource_read(clock);
229
230 /* calculate the delta since the last update_wall_time: */
231 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
232
233 /* convert to nanoseconds: */
234 nsecs = ((s64)cycle_delta * clock->mult_orig) >> clock->shift;
235
236 *ts = clock->raw_time;
237
238 } while (read_seqretry(&xtime_lock, seq));
239
240 timespec_add_ns(ts, nsecs);
241}
242EXPORT_SYMBOL(getrawmonotonic);
243
244
8524070b 245/**
cf4fc6cb 246 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
8524070b 247 */
cf4fc6cb 248int timekeeping_valid_for_hres(void)
8524070b 249{
250 unsigned long seq;
251 int ret;
252
253 do {
254 seq = read_seqbegin(&xtime_lock);
255
256 ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
257
258 } while (read_seqretry(&xtime_lock, seq));
259
260 return ret;
261}
262
263/**
264 * read_persistent_clock - Return time in seconds from the persistent clock.
265 *
266 * Weak dummy function for arches that do not yet support it.
267 * Returns seconds from epoch using the battery backed persistent clock.
268 * Returns zero if unsupported.
269 *
270 * XXX - Do be sure to remove it once all arches implement it.
271 */
272unsigned long __attribute__((weak)) read_persistent_clock(void)
273{
274 return 0;
275}
276
277/*
278 * timekeeping_init - Initializes the clocksource and common timekeeping values
279 */
280void __init timekeeping_init(void)
281{
282 unsigned long flags;
283 unsigned long sec = read_persistent_clock();
284
285 write_seqlock_irqsave(&xtime_lock, flags);
286
7dffa3c6 287 ntp_init();
8524070b 288
289 clock = clocksource_get_next();
10a398d0 290 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
8524070b 291 clock->cycle_last = clocksource_read(clock);
292
293 xtime.tv_sec = sec;
294 xtime.tv_nsec = 0;
295 set_normalized_timespec(&wall_to_monotonic,
296 -xtime.tv_sec, -xtime.tv_nsec);
1001d0a9 297 update_xtime_cache(0);
7c3f1a57 298 total_sleep_time = 0;
8524070b 299 write_sequnlock_irqrestore(&xtime_lock, flags);
300}
301
302/* flag for if timekeeping is suspended */
303static int timekeeping_suspended;
304/* time in seconds when suspend began */
305static unsigned long timekeeping_suspend_time;
306
307/**
308 * timekeeping_resume - Resumes the generic timekeeping subsystem.
309 * @dev: unused
310 *
311 * This is for the generic clocksource timekeeping.
312 * xtime/wall_to_monotonic/jiffies/etc are
313 * still managed by arch specific suspend/resume code.
314 */
315static int timekeeping_resume(struct sys_device *dev)
316{
317 unsigned long flags;
318 unsigned long now = read_persistent_clock();
319
d10ff3fb
TG
320 clocksource_resume();
321
8524070b 322 write_seqlock_irqsave(&xtime_lock, flags);
323
324 if (now && (now > timekeeping_suspend_time)) {
325 unsigned long sleep_length = now - timekeeping_suspend_time;
326
327 xtime.tv_sec += sleep_length;
328 wall_to_monotonic.tv_sec -= sleep_length;
7c3f1a57 329 total_sleep_time += sleep_length;
8524070b 330 }
1001d0a9 331 update_xtime_cache(0);
8524070b 332 /* re-base the last cycle value */
d8bb6f4c 333 clock->cycle_last = 0;
8524070b 334 clock->cycle_last = clocksource_read(clock);
335 clock->error = 0;
336 timekeeping_suspended = 0;
337 write_sequnlock_irqrestore(&xtime_lock, flags);
338
339 touch_softlockup_watchdog();
340
341 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
342
343 /* Resume hrtimers */
344 hres_timers_resume();
345
346 return 0;
347}
348
349static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
350{
351 unsigned long flags;
352
3be90950
TG
353 timekeeping_suspend_time = read_persistent_clock();
354
8524070b 355 write_seqlock_irqsave(&xtime_lock, flags);
9a055117 356 clocksource_forward_now();
8524070b 357 timekeeping_suspended = 1;
8524070b 358 write_sequnlock_irqrestore(&xtime_lock, flags);
359
360 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
361
362 return 0;
363}
364
365/* sysfs resume/suspend bits for timekeeping */
366static struct sysdev_class timekeeping_sysclass = {
af5ca3f4 367 .name = "timekeeping",
8524070b 368 .resume = timekeeping_resume,
369 .suspend = timekeeping_suspend,
8524070b 370};
371
372static struct sys_device device_timer = {
373 .id = 0,
374 .cls = &timekeeping_sysclass,
375};
376
377static int __init timekeeping_init_device(void)
378{
379 int error = sysdev_class_register(&timekeeping_sysclass);
380 if (!error)
381 error = sysdev_register(&device_timer);
382 return error;
383}
384
385device_initcall(timekeeping_init_device);
386
387/*
388 * If the error is already larger, we look ahead even further
389 * to compensate for late or lost adjustments.
390 */
391static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
392 s64 *offset)
393{
394 s64 tick_error, i;
395 u32 look_ahead, adj;
396 s32 error2, mult;
397
398 /*
399 * Use the current error value to determine how much to look ahead.
400 * The larger the error the slower we adjust for it to avoid problems
401 * with losing too many ticks, otherwise we would overadjust and
402 * produce an even larger error. The smaller the adjustment the
403 * faster we try to adjust for it, as lost ticks can do less harm
3eb05676 404 * here. This is tuned so that an error of about 1 msec is adjusted
8524070b 405 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
406 */
7fc5c784 407 error2 = clock->error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
8524070b 408 error2 = abs(error2);
409 for (look_ahead = 0; error2 > 0; look_ahead++)
410 error2 >>= 2;
411
412 /*
413 * Now calculate the error in (1 << look_ahead) ticks, but first
414 * remove the single look ahead already included in the error.
415 */
8383c423 416 tick_error = tick_length >> (NTP_SCALE_SHIFT - clock->shift + 1);
8524070b 417 tick_error -= clock->xtime_interval >> 1;
418 error = ((error - tick_error) >> look_ahead) + tick_error;
419
420 /* Finally calculate the adjustment shift value. */
421 i = *interval;
422 mult = 1;
423 if (error < 0) {
424 error = -error;
425 *interval = -*interval;
426 *offset = -*offset;
427 mult = -1;
428 }
429 for (adj = 0; error > i; adj++)
430 error >>= 1;
431
432 *interval <<= adj;
433 *offset <<= adj;
434 return mult << adj;
435}
436
437/*
438 * Adjust the multiplier to reduce the error value,
439 * this is optimized for the most common adjustments of -1,0,1,
440 * for other values we can do a bit more work.
441 */
71120f18 442static void clocksource_adjust(s64 offset)
8524070b 443{
444 s64 error, interval = clock->cycle_interval;
445 int adj;
446
7fc5c784 447 error = clock->error >> (NTP_SCALE_SHIFT - clock->shift - 1);
8524070b 448 if (error > interval) {
449 error >>= 2;
450 if (likely(error <= interval))
451 adj = 1;
452 else
453 adj = clocksource_bigadjust(error, &interval, &offset);
454 } else if (error < -interval) {
455 error >>= 2;
456 if (likely(error >= -interval)) {
457 adj = -1;
458 interval = -interval;
459 offset = -offset;
460 } else
461 adj = clocksource_bigadjust(error, &interval, &offset);
462 } else
463 return;
464
465 clock->mult += adj;
466 clock->xtime_interval += interval;
467 clock->xtime_nsec -= offset;
468 clock->error -= (interval - offset) <<
7fc5c784 469 (NTP_SCALE_SHIFT - clock->shift);
8524070b 470}
471
472/**
473 * update_wall_time - Uses the current clocksource to increment the wall time
474 *
475 * Called from the timer interrupt, must hold a write on xtime_lock.
476 */
477void update_wall_time(void)
478{
479 cycle_t offset;
480
481 /* Make sure we're fully resumed: */
482 if (unlikely(timekeeping_suspended))
483 return;
484
485#ifdef CONFIG_GENERIC_TIME
486 offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
487#else
488 offset = clock->cycle_interval;
489#endif
490 clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift;
491
492 /* normally this loop will run just once, however in the
493 * case of lost or late ticks, it will accumulate correctly.
494 */
495 while (offset >= clock->cycle_interval) {
496 /* accumulate one interval */
8524070b 497 offset -= clock->cycle_interval;
9a055117 498 clock->cycle_last += clock->cycle_interval;
8524070b 499
9a055117 500 clock->xtime_nsec += clock->xtime_interval;
8524070b 501 if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
502 clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
503 xtime.tv_sec++;
504 second_overflow();
505 }
506
2d42244a
JS
507 clock->raw_time.tv_nsec += clock->raw_interval;
508 if (clock->raw_time.tv_nsec >= NSEC_PER_SEC) {
509 clock->raw_time.tv_nsec -= NSEC_PER_SEC;
510 clock->raw_time.tv_sec++;
511 }
512
8524070b 513 /* accumulate error between NTP and clock interval */
8383c423 514 clock->error += tick_length;
7fc5c784 515 clock->error -= clock->xtime_interval << (NTP_SCALE_SHIFT - clock->shift);
8524070b 516 }
517
518 /* correct the clock when NTP error is too big */
71120f18 519 clocksource_adjust(offset);
8524070b 520
521 /* store full nanoseconds into xtime */
522 xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift;
523 clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
524
17c38b74 525 update_xtime_cache(cyc2ns(clock, offset));
526
8524070b 527 /* check to see if there is a new clocksource to use */
528 change_clocksource();
529 update_vsyscall(&xtime, clock);
530}
7c3f1a57
TJ
531
532/**
533 * getboottime - Return the real time of system boot.
534 * @ts: pointer to the timespec to be set
535 *
536 * Returns the time of day in a timespec.
537 *
538 * This is based on the wall_to_monotonic offset and the total suspend
539 * time. Calls to settimeofday will affect the value returned (which
540 * basically means that however wrong your real time clock is at boot time,
541 * you get the right time here).
542 */
543void getboottime(struct timespec *ts)
544{
545 set_normalized_timespec(ts,
546 - (wall_to_monotonic.tv_sec + total_sleep_time),
547 - wall_to_monotonic.tv_nsec);
548}
549
550/**
551 * monotonic_to_bootbased - Convert the monotonic time to boot based.
552 * @ts: pointer to the timespec to be converted
553 */
554void monotonic_to_bootbased(struct timespec *ts)
555{
556 ts->tv_sec += total_sleep_time;
557}
2c6b47de 558
17c38b74 559unsigned long get_seconds(void)
560{
561 return xtime_cache.tv_sec;
562}
563EXPORT_SYMBOL(get_seconds);
564
565
2c6b47de 566struct timespec current_kernel_time(void)
567{
568 struct timespec now;
569 unsigned long seq;
570
571 do {
572 seq = read_seqbegin(&xtime_lock);
573
17c38b74 574 now = xtime_cache;
2c6b47de 575 } while (read_seqretry(&xtime_lock, seq));
576
577 return now;
578}
2c6b47de 579EXPORT_SYMBOL(current_kernel_time);
This page took 0.28142 seconds and 5 git commands to generate.