Staging: rt2860: enable WPA_SUPPLICANT support
[linux-2.6/verdex.git] / kernel / time / timekeeping.c
blobfa05e88aa76f36af2dc5cb0cc26f737466d0a423
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 and avenrun.
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 static struct timespec xtime_cache __attribute__ ((aligned (16)));
50 void update_xtime_cache(u64 nsec)
52 xtime_cache = xtime;
53 timespec_add_ns(&xtime_cache, nsec);
56 struct clocksource *clock;
59 #ifdef CONFIG_GENERIC_TIME
60 /**
61 * clocksource_forward_now - update clock to the current time
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.
67 static void clocksource_forward_now(void)
69 cycle_t cycle_now, cycle_delta;
70 s64 nsec;
72 cycle_now = clocksource_read(clock);
73 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
74 clock->cycle_last = cycle_now;
76 nsec = cyc2ns(clock, cycle_delta);
77 timespec_add_ns(&xtime, nsec);
79 nsec = ((s64)cycle_delta * clock->mult_orig) >> clock->shift;
80 clock->raw_time.tv_nsec += nsec;
83 /**
84 * getnstimeofday - Returns the time of day in a timespec
85 * @ts: pointer to the timespec to be set
87 * Returns the time of day in a timespec.
89 void getnstimeofday(struct timespec *ts)
91 cycle_t cycle_now, cycle_delta;
92 unsigned long seq;
93 s64 nsecs;
95 do {
96 seq = read_seqbegin(&xtime_lock);
98 *ts = xtime;
100 /* read clocksource: */
101 cycle_now = clocksource_read(clock);
103 /* calculate the delta since the last update_wall_time: */
104 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
106 /* convert to nanoseconds: */
107 nsecs = cyc2ns(clock, cycle_delta);
109 } while (read_seqretry(&xtime_lock, seq));
111 timespec_add_ns(ts, nsecs);
114 EXPORT_SYMBOL(getnstimeofday);
117 * do_gettimeofday - Returns the time of day in a timeval
118 * @tv: pointer to the timeval to be set
120 * NOTE: Users should be converted to using getnstimeofday()
122 void do_gettimeofday(struct timeval *tv)
124 struct timespec now;
126 getnstimeofday(&now);
127 tv->tv_sec = now.tv_sec;
128 tv->tv_usec = now.tv_nsec/1000;
131 EXPORT_SYMBOL(do_gettimeofday);
133 * do_settimeofday - Sets the time of day
134 * @tv: pointer to the timespec variable containing the new time
136 * Sets the time of day to the new time and update NTP and notify hrtimers
138 int do_settimeofday(struct timespec *tv)
140 struct timespec ts_delta;
141 unsigned long flags;
143 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
144 return -EINVAL;
146 write_seqlock_irqsave(&xtime_lock, flags);
148 clocksource_forward_now();
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);
154 xtime = *tv;
156 update_xtime_cache(0);
158 clock->error = 0;
159 ntp_clear();
161 update_vsyscall(&xtime, clock);
163 write_sequnlock_irqrestore(&xtime_lock, flags);
165 /* signal hrtimers about time change */
166 clock_was_set();
168 return 0;
171 EXPORT_SYMBOL(do_settimeofday);
174 * change_clocksource - Swaps clocksources if a new one is available
176 * Accumulates current time interval and initializes new clocksource
178 static void change_clocksource(void)
180 struct clocksource *new;
182 new = clocksource_get_next();
184 if (clock == new)
185 return;
187 clocksource_forward_now();
189 new->raw_time = clock->raw_time;
191 clock = new;
192 clock->cycle_last = 0;
193 clock->cycle_last = clocksource_read(new);
194 clock->error = 0;
195 clock->xtime_nsec = 0;
196 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
198 tick_clock_notify();
201 * We're holding xtime lock and waking up klogd would deadlock
202 * us on enqueue. So no printing!
203 printk(KERN_INFO "Time: %s clocksource has been installed.\n",
204 clock->name);
207 #else
208 static inline void clocksource_forward_now(void) { }
209 static inline void change_clocksource(void) { }
210 #endif
213 * getrawmonotonic - Returns the raw monotonic time in a timespec
214 * @ts: pointer to the timespec to be set
216 * Returns the raw monotonic time (completely un-modified by ntp)
218 void getrawmonotonic(struct timespec *ts)
220 unsigned long seq;
221 s64 nsecs;
222 cycle_t cycle_now, cycle_delta;
224 do {
225 seq = read_seqbegin(&xtime_lock);
227 /* read clocksource: */
228 cycle_now = clocksource_read(clock);
230 /* calculate the delta since the last update_wall_time: */
231 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
233 /* convert to nanoseconds: */
234 nsecs = ((s64)cycle_delta * clock->mult_orig) >> clock->shift;
236 *ts = clock->raw_time;
238 } while (read_seqretry(&xtime_lock, seq));
240 timespec_add_ns(ts, nsecs);
242 EXPORT_SYMBOL(getrawmonotonic);
246 * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
248 int timekeeping_valid_for_hres(void)
250 unsigned long seq;
251 int ret;
253 do {
254 seq = read_seqbegin(&xtime_lock);
256 ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
258 } while (read_seqretry(&xtime_lock, seq));
260 return ret;
264 * read_persistent_clock - Return time in seconds from the persistent clock.
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.
270 * XXX - Do be sure to remove it once all arches implement it.
272 unsigned long __attribute__((weak)) read_persistent_clock(void)
274 return 0;
278 * timekeeping_init - Initializes the clocksource and common timekeeping values
280 void __init timekeeping_init(void)
282 unsigned long flags;
283 unsigned long sec = read_persistent_clock();
285 write_seqlock_irqsave(&xtime_lock, flags);
287 ntp_init();
289 clock = clocksource_get_next();
290 clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH);
291 clock->cycle_last = clocksource_read(clock);
293 xtime.tv_sec = sec;
294 xtime.tv_nsec = 0;
295 set_normalized_timespec(&wall_to_monotonic,
296 -xtime.tv_sec, -xtime.tv_nsec);
297 update_xtime_cache(0);
298 total_sleep_time = 0;
299 write_sequnlock_irqrestore(&xtime_lock, flags);
302 /* flag for if timekeeping is suspended */
303 static int timekeeping_suspended;
304 /* time in seconds when suspend began */
305 static unsigned long timekeeping_suspend_time;
308 * timekeeping_resume - Resumes the generic timekeeping subsystem.
309 * @dev: unused
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.
315 static int timekeeping_resume(struct sys_device *dev)
317 unsigned long flags;
318 unsigned long now = read_persistent_clock();
320 clocksource_resume();
322 write_seqlock_irqsave(&xtime_lock, flags);
324 if (now && (now > timekeeping_suspend_time)) {
325 unsigned long sleep_length = now - timekeeping_suspend_time;
327 xtime.tv_sec += sleep_length;
328 wall_to_monotonic.tv_sec -= sleep_length;
329 total_sleep_time += sleep_length;
331 update_xtime_cache(0);
332 /* re-base the last cycle value */
333 clock->cycle_last = 0;
334 clock->cycle_last = clocksource_read(clock);
335 clock->error = 0;
336 timekeeping_suspended = 0;
337 write_sequnlock_irqrestore(&xtime_lock, flags);
339 touch_softlockup_watchdog();
341 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
343 /* Resume hrtimers */
344 hres_timers_resume();
346 return 0;
349 static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
351 unsigned long flags;
353 timekeeping_suspend_time = read_persistent_clock();
355 write_seqlock_irqsave(&xtime_lock, flags);
356 clocksource_forward_now();
357 timekeeping_suspended = 1;
358 write_sequnlock_irqrestore(&xtime_lock, flags);
360 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
362 return 0;
365 /* sysfs resume/suspend bits for timekeeping */
366 static struct sysdev_class timekeeping_sysclass = {
367 .name = "timekeeping",
368 .resume = timekeeping_resume,
369 .suspend = timekeeping_suspend,
372 static struct sys_device device_timer = {
373 .id = 0,
374 .cls = &timekeeping_sysclass,
377 static int __init timekeeping_init_device(void)
379 int error = sysdev_class_register(&timekeeping_sysclass);
380 if (!error)
381 error = sysdev_register(&device_timer);
382 return error;
385 device_initcall(timekeeping_init_device);
388 * If the error is already larger, we look ahead even further
389 * to compensate for late or lost adjustments.
391 static __always_inline int clocksource_bigadjust(s64 error, s64 *interval,
392 s64 *offset)
394 s64 tick_error, i;
395 u32 look_ahead, adj;
396 s32 error2, mult;
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
404 * here. This is tuned so that an error of about 1 msec is adjusted
405 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
407 error2 = clock->error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
408 error2 = abs(error2);
409 for (look_ahead = 0; error2 > 0; look_ahead++)
410 error2 >>= 2;
413 * Now calculate the error in (1 << look_ahead) ticks, but first
414 * remove the single look ahead already included in the error.
416 tick_error = tick_length >> (NTP_SCALE_SHIFT - clock->shift + 1);
417 tick_error -= clock->xtime_interval >> 1;
418 error = ((error - tick_error) >> look_ahead) + tick_error;
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;
429 for (adj = 0; error > i; adj++)
430 error >>= 1;
432 *interval <<= adj;
433 *offset <<= adj;
434 return mult << adj;
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.
442 static void clocksource_adjust(s64 offset)
444 s64 error, interval = clock->cycle_interval;
445 int adj;
447 error = clock->error >> (NTP_SCALE_SHIFT - clock->shift - 1);
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;
465 clock->mult += adj;
466 clock->xtime_interval += interval;
467 clock->xtime_nsec -= offset;
468 clock->error -= (interval - offset) <<
469 (NTP_SCALE_SHIFT - clock->shift);
473 * update_wall_time - Uses the current clocksource to increment the wall time
475 * Called from the timer interrupt, must hold a write on xtime_lock.
477 void update_wall_time(void)
479 cycle_t offset;
481 /* Make sure we're fully resumed: */
482 if (unlikely(timekeeping_suspended))
483 return;
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;
492 /* normally this loop will run just once, however in the
493 * case of lost or late ticks, it will accumulate correctly.
495 while (offset >= clock->cycle_interval) {
496 /* accumulate one interval */
497 offset -= clock->cycle_interval;
498 clock->cycle_last += clock->cycle_interval;
500 clock->xtime_nsec += clock->xtime_interval;
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();
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++;
513 /* accumulate error between NTP and clock interval */
514 clock->error += tick_length;
515 clock->error -= clock->xtime_interval << (NTP_SCALE_SHIFT - clock->shift);
518 /* correct the clock when NTP error is too big */
519 clocksource_adjust(offset);
522 * Since in the loop above, we accumulate any amount of time
523 * in xtime_nsec over a second into xtime.tv_sec, its possible for
524 * xtime_nsec to be fairly small after the loop. Further, if we're
525 * slightly speeding the clocksource up in clocksource_adjust(),
526 * its possible the required corrective factor to xtime_nsec could
527 * cause it to underflow.
529 * Now, we cannot simply roll the accumulated second back, since
530 * the NTP subsystem has been notified via second_overflow. So
531 * instead we push xtime_nsec forward by the amount we underflowed,
532 * and add that amount into the error.
534 * We'll correct this error next time through this function, when
535 * xtime_nsec is not as small.
537 if (unlikely((s64)clock->xtime_nsec < 0)) {
538 s64 neg = -(s64)clock->xtime_nsec;
539 clock->xtime_nsec = 0;
540 clock->error += neg << (NTP_SCALE_SHIFT - clock->shift);
543 /* store full nanoseconds into xtime after rounding it up and
544 * add the remainder to the error difference.
546 xtime.tv_nsec = ((s64)clock->xtime_nsec >> clock->shift) + 1;
547 clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
548 clock->error += clock->xtime_nsec << (NTP_SCALE_SHIFT - clock->shift);
550 update_xtime_cache(cyc2ns(clock, offset));
552 /* check to see if there is a new clocksource to use */
553 change_clocksource();
554 update_vsyscall(&xtime, clock);
558 * getboottime - Return the real time of system boot.
559 * @ts: pointer to the timespec to be set
561 * Returns the time of day in a timespec.
563 * This is based on the wall_to_monotonic offset and the total suspend
564 * time. Calls to settimeofday will affect the value returned (which
565 * basically means that however wrong your real time clock is at boot time,
566 * you get the right time here).
568 void getboottime(struct timespec *ts)
570 set_normalized_timespec(ts,
571 - (wall_to_monotonic.tv_sec + total_sleep_time),
572 - wall_to_monotonic.tv_nsec);
576 * monotonic_to_bootbased - Convert the monotonic time to boot based.
577 * @ts: pointer to the timespec to be converted
579 void monotonic_to_bootbased(struct timespec *ts)
581 ts->tv_sec += total_sleep_time;
584 unsigned long get_seconds(void)
586 return xtime_cache.tv_sec;
588 EXPORT_SYMBOL(get_seconds);
591 struct timespec current_kernel_time(void)
593 struct timespec now;
594 unsigned long seq;
596 do {
597 seq = read_seqbegin(&xtime_lock);
599 now = xtime_cache;
600 } while (read_seqretry(&xtime_lock, seq));
602 return now;
604 EXPORT_SYMBOL(current_kernel_time);