aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/timer.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/timer.c')
-rw-r--r--kernel/timer.c211
1 files changed, 1 insertions, 210 deletions
diff --git a/kernel/timer.c b/kernel/timer.c
index 4f55622b0d38..5fccc7cbf3b4 100644
--- a/kernel/timer.c
+++ b/kernel/timer.c
@@ -41,12 +41,6 @@
41#include <asm/timex.h> 41#include <asm/timex.h>
42#include <asm/io.h> 42#include <asm/io.h>
43 43
44#ifdef CONFIG_TIME_INTERPOLATION
45static void time_interpolator_update(long delta_nsec);
46#else
47#define time_interpolator_update(x)
48#endif
49
50u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES; 44u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
51 45
52EXPORT_SYMBOL(jiffies_64); 46EXPORT_SYMBOL(jiffies_64);
@@ -587,209 +581,6 @@ struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
587 581
588EXPORT_SYMBOL(xtime); 582EXPORT_SYMBOL(xtime);
589 583
590/* Don't completely fail for HZ > 500. */
591int tickadj = 500/HZ ? : 1; /* microsecs */
592
593
594/*
595 * phase-lock loop variables
596 */
597/* TIME_ERROR prevents overwriting the CMOS clock */
598int time_state = TIME_OK; /* clock synchronization status */
599int time_status = STA_UNSYNC; /* clock status bits */
600long time_offset; /* time adjustment (us) */
601long time_constant = 2; /* pll time constant */
602long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
603long time_precision = 1; /* clock precision (us) */
604long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
605long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
606long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
607 /* frequency offset (scaled ppm)*/
608static long time_adj; /* tick adjust (scaled 1 / HZ) */
609long time_reftime; /* time at last adjustment (s) */
610long time_adjust;
611long time_next_adjust;
612
613/*
614 * this routine handles the overflow of the microsecond field
615 *
616 * The tricky bits of code to handle the accurate clock support
617 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
618 * They were originally developed for SUN and DEC kernels.
619 * All the kudos should go to Dave for this stuff.
620 *
621 */
622static void second_overflow(void)
623{
624 long ltemp;
625
626 /* Bump the maxerror field */
627 time_maxerror += time_tolerance >> SHIFT_USEC;
628 if (time_maxerror > NTP_PHASE_LIMIT) {
629 time_maxerror = NTP_PHASE_LIMIT;
630 time_status |= STA_UNSYNC;
631 }
632
633 /*
634 * Leap second processing. If in leap-insert state at the end of the
635 * day, the system clock is set back one second; if in leap-delete
636 * state, the system clock is set ahead one second. The microtime()
637 * routine or external clock driver will insure that reported time is
638 * always monotonic. The ugly divides should be replaced.
639 */
640 switch (time_state) {
641 case TIME_OK:
642 if (time_status & STA_INS)
643 time_state = TIME_INS;
644 else if (time_status & STA_DEL)
645 time_state = TIME_DEL;
646 break;
647 case TIME_INS:
648 if (xtime.tv_sec % 86400 == 0) {
649 xtime.tv_sec--;
650 wall_to_monotonic.tv_sec++;
651 /*
652 * The timer interpolator will make time change
653 * gradually instead of an immediate jump by one second
654 */
655 time_interpolator_update(-NSEC_PER_SEC);
656 time_state = TIME_OOP;
657 clock_was_set();
658 printk(KERN_NOTICE "Clock: inserting leap second "
659 "23:59:60 UTC\n");
660 }
661 break;
662 case TIME_DEL:
663 if ((xtime.tv_sec + 1) % 86400 == 0) {
664 xtime.tv_sec++;
665 wall_to_monotonic.tv_sec--;
666 /*
667 * Use of time interpolator for a gradual change of
668 * time
669 */
670 time_interpolator_update(NSEC_PER_SEC);
671 time_state = TIME_WAIT;
672 clock_was_set();
673 printk(KERN_NOTICE "Clock: deleting leap second "
674 "23:59:59 UTC\n");
675 }
676 break;
677 case TIME_OOP:
678 time_state = TIME_WAIT;
679 break;
680 case TIME_WAIT:
681 if (!(time_status & (STA_INS | STA_DEL)))
682 time_state = TIME_OK;
683 }
684
685 /*
686 * Compute the phase adjustment for the next second. In PLL mode, the
687 * offset is reduced by a fixed factor times the time constant. In FLL
688 * mode the offset is used directly. In either mode, the maximum phase
689 * adjustment for each second is clamped so as to spread the adjustment
690 * over not more than the number of seconds between updates.
691 */
692 ltemp = time_offset;
693 if (!(time_status & STA_FLL))
694 ltemp = shift_right(ltemp, SHIFT_KG + time_constant);
695 ltemp = min(ltemp, (MAXPHASE / MINSEC) << SHIFT_UPDATE);
696 ltemp = max(ltemp, -(MAXPHASE / MINSEC) << SHIFT_UPDATE);
697 time_offset -= ltemp;
698 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
699
700 /*
701 * Compute the frequency estimate and additional phase adjustment due
702 * to frequency error for the next second.
703 */
704 ltemp = time_freq;
705 time_adj += shift_right(ltemp,(SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE));
706
707#if HZ == 100
708 /*
709 * Compensate for (HZ==100) != (1 << SHIFT_HZ). Add 25% and 3.125% to
710 * get 128.125; => only 0.125% error (p. 14)
711 */
712 time_adj += shift_right(time_adj, 2) + shift_right(time_adj, 5);
713#endif
714#if HZ == 250
715 /*
716 * Compensate for (HZ==250) != (1 << SHIFT_HZ). Add 1.5625% and
717 * 0.78125% to get 255.85938; => only 0.05% error (p. 14)
718 */
719 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
720#endif
721#if HZ == 1000
722 /*
723 * Compensate for (HZ==1000) != (1 << SHIFT_HZ). Add 1.5625% and
724 * 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
725 */
726 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
727#endif
728}
729
730/*
731 * Returns how many microseconds we need to add to xtime this tick
732 * in doing an adjustment requested with adjtime.
733 */
734static long adjtime_adjustment(void)
735{
736 long time_adjust_step;
737
738 time_adjust_step = time_adjust;
739 if (time_adjust_step) {
740 /*
741 * We are doing an adjtime thing. Prepare time_adjust_step to
742 * be within bounds. Note that a positive time_adjust means we
743 * want the clock to run faster.
744 *
745 * Limit the amount of the step to be in the range
746 * -tickadj .. +tickadj
747 */
748 time_adjust_step = min(time_adjust_step, (long)tickadj);
749 time_adjust_step = max(time_adjust_step, (long)-tickadj);
750 }
751 return time_adjust_step;
752}
753
754/* in the NTP reference this is called "hardclock()" */
755static void update_ntp_one_tick(void)
756{
757 long time_adjust_step;
758
759 time_adjust_step = adjtime_adjustment();
760 if (time_adjust_step)
761 /* Reduce by this step the amount of time left */
762 time_adjust -= time_adjust_step;
763
764 /* Changes by adjtime() do not take effect till next tick. */
765 if (time_next_adjust != 0) {
766 time_adjust = time_next_adjust;
767 time_next_adjust = 0;
768 }
769}
770
771/*
772 * Return how long ticks are at the moment, that is, how much time
773 * update_wall_time_one_tick will add to xtime next time we call it
774 * (assuming no calls to do_adjtimex in the meantime).
775 * The return value is in fixed-point nanoseconds shifted by the
776 * specified number of bits to the right of the binary point.
777 * This function has no side-effects.
778 */
779u64 current_tick_length(void)
780{
781 long delta_nsec;
782 u64 ret;
783
784 /* calculate the finest interval NTP will allow.
785 * ie: nanosecond value shifted by (SHIFT_SCALE - 10)
786 */
787 delta_nsec = tick_nsec + adjtime_adjustment() * 1000;
788 ret = (u64)delta_nsec << TICK_LENGTH_SHIFT;
789 ret += (s64)time_adj << (TICK_LENGTH_SHIFT - (SHIFT_SCALE - 10));
790
791 return ret;
792}
793 584
794/* XXX - all of this timekeeping code should be later moved to time.c */ 585/* XXX - all of this timekeeping code should be later moved to time.c */
795#include <linux/clocksource.h> 586#include <linux/clocksource.h>
@@ -1775,7 +1566,7 @@ unsigned long time_interpolator_get_offset(void)
1775#define INTERPOLATOR_ADJUST 65536 1566#define INTERPOLATOR_ADJUST 65536
1776#define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST 1567#define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
1777 1568
1778static void time_interpolator_update(long delta_nsec) 1569void time_interpolator_update(long delta_nsec)
1779{ 1570{
1780 u64 counter; 1571 u64 counter;
1781 unsigned long offset; 1572 unsigned long offset;