sock_buffertracker conn_reset bugfix, cmsg timings, dynamic cmsg timer tick, add_mess...
[cor_2_6_31.git] / kernel / time / timekeeping.c
blobe8c77d9c633acbffc4872b8d0c4b8a9929d399c2
1 /*
2 * linux/kernel/time/timekeeping.c
4 * Kernel timekeeping code and accessor functions
6 * This code was moved from linux/kernel/timer.c.
7 * Please see that file for copyright and history logs.
9 */
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>
24 * This read-write spinlock protects us from races in SMP while
25 * playing with xtime.
27 __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
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.
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.
42 * - wall_to_monotonic is no longer the boot time, getboottime must be
43 * used instead.
45 struct timespec xtime __attribute__ ((aligned (16)));
46 struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
47 static unsigned long total_sleep_time; /* seconds */
49 /* flag for if timekeeping is suspended */
50 int __read_mostly timekeeping_suspended;
52 static struct timespec xtime_cache __attribute__ ((aligned (16)));
53 void update_xtime_cache(u64 nsec)
55 xtime_cache = xtime;
56 timespec_add_ns(&xtime_cache, nsec);
59 struct clocksource *clock;
62 #ifdef CONFIG_GENERIC_TIME
63 /**
64 * clocksource_forward_now - update clock to the current time
66 * Forward the current clock to update its state since the last call to
67 * update_wall_time(). This is useful before significant clock changes,
68 * as it avoids having to deal with this time offset explicitly.
70 static void clocksource_forward_now(void)
72 cycle_t cycle_now, cycle_delta;
73 s64 nsec;
75 cycle_now = clocksource_read(clock);
76 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
77 clock->cycle_last = cycle_now;
79 nsec = cyc2ns(clock, cycle_delta);
81 /* If arch requires, add in gettimeoffset() */
82 nsec += arch_gettimeoffset();
84 timespec_add_ns(&xtime, nsec);
86 nsec = ((s64)cycle_delta * clock->mult_orig) >> clock->shift;
87 clock->raw_time.tv_nsec += nsec;
90 /**
91 * getnstimeofday - Returns the time of day in a timespec
92 * @ts: pointer to the timespec to be set
94 * Returns the time of day in a timespec.
96 void getnstimeofday(struct timespec *ts)
98 cycle_t cycle_now, cycle_delta;
99 unsigned long seq;
100 s64 nsecs;
102 WARN_ON(timekeeping_suspended);
104 do {
105 seq = read_seqbegin(&xtime_lock);
107 *ts = xtime;
109 /* read clocksource: */
110 cycle_now = clocksource_read(clock);
112 /* calculate the delta since the last update_wall_time: */
113 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
115 /* convert to nanoseconds: */
116 nsecs = cyc2ns(clock, cycle_delta);
118 /* If arch requires, add in gettimeoffset() */
119 nsecs += arch_gettimeoffset();
121 } while (read_seqretry(&xtime_lock, seq));
123 timespec_add_ns(ts, nsecs);
126 EXPORT_SYMBOL(getnstimeofday);
129 * do_gettimeofday - Returns the time of day in a timeval
130 * @tv: pointer to the timeval to be set
132 * NOTE: Users should be converted to using getnstimeofday()
134 void do_gettimeofday(struct timeval *tv)
136 struct timespec now;
138 getnstimeofday(&now);
139 tv->tv_sec = now.tv_sec;
140 tv->tv_usec = now.tv_nsec/1000;
143 EXPORT_SYMBOL(do_gettimeofday);
145 * do_settimeofday - Sets the time of day
146 * @tv: pointer to the timespec variable containing the new time
148 * Sets the time of day to the new time and update NTP and notify hrtimers
150 int do_settimeofday(struct timespec *tv)
152 struct timespec ts_delta;
153 unsigned long flags;
155 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
156 return -EINVAL;
158 write_seqlock_irqsave(&xtime_lock, flags);
160 clocksource_forward_now();
162 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
163 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
164 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
166 xtime = *tv;
168 update_xtime_cache(0);
170 clock->error = 0;
171 ntp_clear();
173 update_vsyscall(&xtime, clock);
175 write_sequnlock_irqrestore(&xtime_lock, flags);
177 /* signal hrtimers about time change */
178 clock_was_set();
180 return 0;
183 EXPORT_SYMBOL(do_settimeofday);
186 * change_clocksource - Swaps clocksources if a new one is available
188 * Accumulates current time interval and initializes new clocksource
190 static void change_clocksource(void)
192 struct clocksource *new, *old;
194 new = clocksource_get_next();
196 if (clock == new)
197 return;
199 clocksource_forward_now();
201 if (clocksource_enable(new))
202 return;
204 new->raw_time = clock->raw_time;
205 old = clock;
206 clock = new;
207 clocksource_disable(old);
209 clock->cycle_last = 0;
210 clock->cycle_last = clocksource_read(clock);
211 clock->error = 0;
212 clock->xtime_nsec = 0;
213 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
215 tick_clock_notify();
218 * We're holding xtime lock and waking up klogd would deadlock
219 * us on enqueue. So no printing!
220 printk(KERN_INFO "Time: %s clocksource has been installed.\n",
221 clock->name);
224 #else
225 static inline void clocksource_forward_now(void) { }
226 static inline void change_clocksource(void) { }
227 #endif
230 * getrawmonotonic - Returns the raw monotonic time in a timespec
231 * @ts: pointer to the timespec to be set
233 * Returns the raw monotonic time (completely un-modified by ntp)
235 void getrawmonotonic(struct timespec *ts)
237 unsigned long seq;
238 s64 nsecs;
239 cycle_t cycle_now, cycle_delta;
241 do {
242 seq = read_seqbegin(&xtime_lock);
244 /* read clocksource: */
245 cycle_now = clocksource_read(clock);
247 /* calculate the delta since the last update_wall_time: */
248 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
250 /* convert to nanoseconds: */
251 nsecs = ((s64)cycle_delta * clock->mult_orig) >> clock->shift;
253 *ts = clock->raw_time;
255 } while (read_seqretry(&xtime_lock, seq));
257 timespec_add_ns(ts, nsecs);
259 EXPORT_SYMBOL(getrawmonotonic);
263 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
265 int timekeeping_valid_for_hres(void)
267 unsigned long seq;
268 int ret;
270 do {
271 seq = read_seqbegin(&xtime_lock);
273 ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
275 } while (read_seqretry(&xtime_lock, seq));
277 return ret;
281 * read_persistent_clock - Return time in seconds from the persistent clock.
283 * Weak dummy function for arches that do not yet support it.
284 * Returns seconds from epoch using the battery backed persistent clock.
285 * Returns zero if unsupported.
287 * XXX - Do be sure to remove it once all arches implement it.
289 unsigned long __attribute__((weak)) read_persistent_clock(void)
291 return 0;
295 * timekeeping_init - Initializes the clocksource and common timekeeping values
297 void __init timekeeping_init(void)
299 unsigned long flags;
300 unsigned long sec = read_persistent_clock();
302 write_seqlock_irqsave(&xtime_lock, flags);
304 ntp_init();
306 clock = clocksource_get_next();
307 clocksource_enable(clock);
308 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
309 clock->cycle_last = clocksource_read(clock);
311 xtime.tv_sec = sec;
312 xtime.tv_nsec = 0;
313 set_normalized_timespec(&wall_to_monotonic,
314 -xtime.tv_sec, -xtime.tv_nsec);
315 update_xtime_cache(0);
316 total_sleep_time = 0;
317 write_sequnlock_irqrestore(&xtime_lock, flags);
320 /* time in seconds when suspend began */
321 static unsigned long timekeeping_suspend_time;
324 * timekeeping_resume - Resumes the generic timekeeping subsystem.
325 * @dev: unused
327 * This is for the generic clocksource timekeeping.
328 * xtime/wall_to_monotonic/jiffies/etc are
329 * still managed by arch specific suspend/resume code.
331 static int timekeeping_resume(struct sys_device *dev)
333 unsigned long flags;
334 unsigned long now = read_persistent_clock();
336 clocksource_resume();
338 write_seqlock_irqsave(&xtime_lock, flags);
340 if (now && (now > timekeeping_suspend_time)) {
341 unsigned long sleep_length = now - timekeeping_suspend_time;
343 xtime.tv_sec += sleep_length;
344 wall_to_monotonic.tv_sec -= sleep_length;
345 total_sleep_time += sleep_length;
347 update_xtime_cache(0);
348 /* re-base the last cycle value */
349 clock->cycle_last = 0;
350 clock->cycle_last = clocksource_read(clock);
351 clock->error = 0;
352 timekeeping_suspended = 0;
353 write_sequnlock_irqrestore(&xtime_lock, flags);
355 touch_softlockup_watchdog();
357 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
359 /* Resume hrtimers */
360 hres_timers_resume();
362 return 0;
365 static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
367 unsigned long flags;
369 timekeeping_suspend_time = read_persistent_clock();
371 write_seqlock_irqsave(&xtime_lock, flags);
372 clocksource_forward_now();
373 timekeeping_suspended = 1;
374 write_sequnlock_irqrestore(&xtime_lock, flags);
376 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
378 return 0;
381 /* sysfs resume/suspend bits for timekeeping */
382 static struct sysdev_class timekeeping_sysclass = {
383 .name = "timekeeping",
384 .resume = timekeeping_resume,
385 .suspend = timekeeping_suspend,
388 static struct sys_device device_timer = {
389 .id = 0,
390 .cls = &timekeeping_sysclass,
393 static int __init timekeeping_init_device(void)
395 int error = sysdev_class_register(&timekeeping_sysclass);
396 if (!error)
397 error = sysdev_register(&device_timer);
398 return error;
401 device_initcall(timekeeping_init_device);
404 * If the error is already larger, we look ahead even further
405 * to compensate for late or lost adjustments.
407 static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
408 s64 *offset)
410 s64 tick_error, i;
411 u32 look_ahead, adj;
412 s32 error2, mult;
415 * Use the current error value to determine how much to look ahead.
416 * The larger the error the slower we adjust for it to avoid problems
417 * with losing too many ticks, otherwise we would overadjust and
418 * produce an even larger error. The smaller the adjustment the
419 * faster we try to adjust for it, as lost ticks can do less harm
420 * here. This is tuned so that an error of about 1 msec is adjusted
421 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
423 error2 = clock->error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
424 error2 = abs(error2);
425 for (look_ahead = 0; error2 > 0; look_ahead++)
426 error2 >>= 2;
429 * Now calculate the error in (1 << look_ahead) ticks, but first
430 * remove the single look ahead already included in the error.
432 tick_error = tick_length >> (NTP_SCALE_SHIFT - clock->shift + 1);
433 tick_error -= clock->xtime_interval >> 1;
434 error = ((error - tick_error) >> look_ahead) + tick_error;
436 /* Finally calculate the adjustment shift value. */
437 i = *interval;
438 mult = 1;
439 if (error < 0) {
440 error = -error;
441 *interval = -*interval;
442 *offset = -*offset;
443 mult = -1;
445 for (adj = 0; error > i; adj++)
446 error >>= 1;
448 *interval <<= adj;
449 *offset <<= adj;
450 return mult << adj;
454 * Adjust the multiplier to reduce the error value,
455 * this is optimized for the most common adjustments of -1,0,1,
456 * for other values we can do a bit more work.
458 static void clocksource_adjust(s64 offset)
460 s64 error, interval = clock->cycle_interval;
461 int adj;
463 error = clock->error >> (NTP_SCALE_SHIFT - clock->shift - 1);
464 if (error > interval) {
465 error >>= 2;
466 if (likely(error <= interval))
467 adj = 1;
468 else
469 adj = clocksource_bigadjust(error, &interval, &offset);
470 } else if (error < -interval) {
471 error >>= 2;
472 if (likely(error >= -interval)) {
473 adj = -1;
474 interval = -interval;
475 offset = -offset;
476 } else
477 adj = clocksource_bigadjust(error, &interval, &offset);
478 } else
479 return;
481 clock->mult += adj;
482 clock->xtime_interval += interval;
483 clock->xtime_nsec -= offset;
484 clock->error -= (interval - offset) <<
485 (NTP_SCALE_SHIFT - clock->shift);
489 * update_wall_time - Uses the current clocksource to increment the wall time
491 * Called from the timer interrupt, must hold a write on xtime_lock.
493 void update_wall_time(void)
495 cycle_t offset;
497 /* Make sure we're fully resumed: */
498 if (unlikely(timekeeping_suspended))
499 return;
501 #ifdef CONFIG_GENERIC_TIME
502 offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
503 #else
504 offset = clock->cycle_interval;
505 #endif
506 clock->xtime_nsec = (s64)xtime.tv_nsec << clock->shift;
508 /* normally this loop will run just once, however in the
509 * case of lost or late ticks, it will accumulate correctly.
511 while (offset >= clock->cycle_interval) {
512 /* accumulate one interval */
513 offset -= clock->cycle_interval;
514 clock->cycle_last += clock->cycle_interval;
516 clock->xtime_nsec += clock->xtime_interval;
517 if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
518 clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
519 xtime.tv_sec++;
520 second_overflow();
523 clock->raw_time.tv_nsec += clock->raw_interval;
524 if (clock->raw_time.tv_nsec >= NSEC_PER_SEC) {
525 clock->raw_time.tv_nsec -= NSEC_PER_SEC;
526 clock->raw_time.tv_sec++;
529 /* accumulate error between NTP and clock interval */
530 clock->error += tick_length;
531 clock->error -= clock->xtime_interval << (NTP_SCALE_SHIFT - clock->shift);
534 /* correct the clock when NTP error is too big */
535 clocksource_adjust(offset);
538 * Since in the loop above, we accumulate any amount of time
539 * in xtime_nsec over a second into xtime.tv_sec, its possible for
540 * xtime_nsec to be fairly small after the loop. Further, if we're
541 * slightly speeding the clocksource up in clocksource_adjust(),
542 * its possible the required corrective factor to xtime_nsec could
543 * cause it to underflow.
545 * Now, we cannot simply roll the accumulated second back, since
546 * the NTP subsystem has been notified via second_overflow. So
547 * instead we push xtime_nsec forward by the amount we underflowed,
548 * and add that amount into the error.
550 * We'll correct this error next time through this function, when
551 * xtime_nsec is not as small.
553 if (unlikely((s64)clock->xtime_nsec < 0)) {
554 s64 neg = -(s64)clock->xtime_nsec;
555 clock->xtime_nsec = 0;
556 clock->error += neg << (NTP_SCALE_SHIFT - clock->shift);
559 /* store full nanoseconds into xtime after rounding it up and
560 * add the remainder to the error difference.
562 xtime.tv_nsec = ((s64)clock->xtime_nsec >> clock->shift) + 1;
563 clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
564 clock->error += clock->xtime_nsec << (NTP_SCALE_SHIFT - clock->shift);
566 update_xtime_cache(cyc2ns(clock, offset));
568 /* check to see if there is a new clocksource to use */
569 change_clocksource();
570 update_vsyscall(&xtime, clock);
574 * getboottime - Return the real time of system boot.
575 * @ts: pointer to the timespec to be set
577 * Returns the time of day in a timespec.
579 * This is based on the wall_to_monotonic offset and the total suspend
580 * time. Calls to settimeofday will affect the value returned (which
581 * basically means that however wrong your real time clock is at boot time,
582 * you get the right time here).
584 void getboottime(struct timespec *ts)
586 set_normalized_timespec(ts,
587 - (wall_to_monotonic.tv_sec + total_sleep_time),
588 - wall_to_monotonic.tv_nsec);
592 * monotonic_to_bootbased - Convert the monotonic time to boot based.
593 * @ts: pointer to the timespec to be converted
595 void monotonic_to_bootbased(struct timespec *ts)
597 ts->tv_sec += total_sleep_time;
600 unsigned long get_seconds(void)
602 return xtime_cache.tv_sec;
604 EXPORT_SYMBOL(get_seconds);
607 struct timespec current_kernel_time(void)
609 struct timespec now;
610 unsigned long seq;
612 do {
613 seq = read_seqbegin(&xtime_lock);
615 now = xtime_cache;
616 } while (read_seqretry(&xtime_lock, seq));
618 return now;
620 EXPORT_SYMBOL(current_kernel_time);