Home | History | Annotate | Line # | Download | only in kern
kern_clock.c revision 1.113
      1  1.113        ad /*	$NetBSD: kern_clock.c,v 1.113 2007/10/04 12:55:48 ad Exp $	*/
      2   1.52   thorpej 
      3   1.52   thorpej /*-
      4  1.106        ad  * Copyright (c) 2000, 2004, 2006, 2007 The NetBSD Foundation, Inc.
      5   1.52   thorpej  * All rights reserved.
      6   1.52   thorpej  *
      7   1.52   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.52   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.52   thorpej  * NASA Ames Research Center.
     10   1.94   mycroft  * This code is derived from software contributed to The NetBSD Foundation
     11   1.94   mycroft  * by Charles M. Hannum.
     12   1.52   thorpej  *
     13   1.52   thorpej  * Redistribution and use in source and binary forms, with or without
     14   1.52   thorpej  * modification, are permitted provided that the following conditions
     15   1.52   thorpej  * are met:
     16   1.52   thorpej  * 1. Redistributions of source code must retain the above copyright
     17   1.52   thorpej  *    notice, this list of conditions and the following disclaimer.
     18   1.52   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     19   1.52   thorpej  *    notice, this list of conditions and the following disclaimer in the
     20   1.52   thorpej  *    documentation and/or other materials provided with the distribution.
     21   1.52   thorpej  * 3. All advertising materials mentioning features or use of this software
     22   1.52   thorpej  *    must display the following acknowledgement:
     23   1.52   thorpej  *	This product includes software developed by the NetBSD
     24   1.52   thorpej  *	Foundation, Inc. and its contributors.
     25   1.52   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     26   1.52   thorpej  *    contributors may be used to endorse or promote products derived
     27   1.52   thorpej  *    from this software without specific prior written permission.
     28   1.52   thorpej  *
     29   1.52   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     30   1.52   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     31   1.52   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     32   1.52   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     33   1.52   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     34   1.52   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     35   1.52   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     36   1.52   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     37   1.52   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     38   1.52   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     39   1.52   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     40   1.52   thorpej  */
     41   1.19       cgd 
     42   1.19       cgd /*-
     43   1.19       cgd  * Copyright (c) 1982, 1986, 1991, 1993
     44   1.19       cgd  *	The Regents of the University of California.  All rights reserved.
     45   1.19       cgd  * (c) UNIX System Laboratories, Inc.
     46   1.19       cgd  * All or some portions of this file are derived from material licensed
     47   1.19       cgd  * to the University of California by American Telephone and Telegraph
     48   1.19       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     49   1.19       cgd  * the permission of UNIX System Laboratories, Inc.
     50   1.19       cgd  *
     51   1.19       cgd  * Redistribution and use in source and binary forms, with or without
     52   1.19       cgd  * modification, are permitted provided that the following conditions
     53   1.19       cgd  * are met:
     54   1.19       cgd  * 1. Redistributions of source code must retain the above copyright
     55   1.19       cgd  *    notice, this list of conditions and the following disclaimer.
     56   1.19       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     57   1.19       cgd  *    notice, this list of conditions and the following disclaimer in the
     58   1.19       cgd  *    documentation and/or other materials provided with the distribution.
     59   1.87       agc  * 3. Neither the name of the University nor the names of its contributors
     60   1.19       cgd  *    may be used to endorse or promote products derived from this software
     61   1.19       cgd  *    without specific prior written permission.
     62   1.19       cgd  *
     63   1.19       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64   1.19       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65   1.19       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66   1.19       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67   1.19       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68   1.19       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69   1.19       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70   1.19       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71   1.19       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72   1.19       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73   1.19       cgd  * SUCH DAMAGE.
     74   1.19       cgd  *
     75   1.19       cgd  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
     76   1.19       cgd  */
     77   1.78     lukem 
     78   1.78     lukem #include <sys/cdefs.h>
     79  1.113        ad __KERNEL_RCSID(0, "$NetBSD: kern_clock.c,v 1.113 2007/10/04 12:55:48 ad Exp $");
     80   1.44  jonathan 
     81   1.44  jonathan #include "opt_ntp.h"
     82   1.86    martin #include "opt_multiprocessor.h"
     83   1.80    briggs #include "opt_perfctrs.h"
     84   1.19       cgd 
     85   1.19       cgd #include <sys/param.h>
     86   1.19       cgd #include <sys/systm.h>
     87   1.19       cgd #include <sys/callout.h>
     88   1.19       cgd #include <sys/kernel.h>
     89   1.19       cgd #include <sys/proc.h>
     90   1.19       cgd #include <sys/resourcevar.h>
     91   1.25  christos #include <sys/signalvar.h>
     92   1.26  christos #include <sys/sysctl.h>
     93   1.27  jonathan #include <sys/timex.h>
     94   1.45      ross #include <sys/sched.h>
     95   1.82   thorpej #include <sys/time.h>
     96   1.99    kardel #include <sys/timetc.h>
     97  1.109        ad #include <sys/cpu.h>
     98   1.25  christos 
     99   1.19       cgd #ifdef GPROF
    100   1.19       cgd #include <sys/gmon.h>
    101   1.19       cgd #endif
    102   1.19       cgd 
    103   1.19       cgd /*
    104   1.19       cgd  * Clock handling routines.
    105   1.19       cgd  *
    106   1.19       cgd  * This code is written to operate with two timers that run independently of
    107   1.19       cgd  * each other.  The main clock, running hz times per second, is used to keep
    108   1.19       cgd  * track of real time.  The second timer handles kernel and user profiling,
    109   1.19       cgd  * and does resource use estimation.  If the second timer is programmable,
    110   1.19       cgd  * it is randomized to avoid aliasing between the two clocks.  For example,
    111   1.90       wiz  * the randomization prevents an adversary from always giving up the CPU
    112   1.19       cgd  * just before its quantum expires.  Otherwise, it would never accumulate
    113   1.90       wiz  * CPU ticks.  The mean frequency of the second timer is stathz.
    114   1.19       cgd  *
    115   1.19       cgd  * If no second timer exists, stathz will be zero; in this case we drive
    116   1.19       cgd  * profiling and statistics off the main clock.  This WILL NOT be accurate;
    117   1.19       cgd  * do not do it unless absolutely necessary.
    118   1.19       cgd  *
    119   1.19       cgd  * The statistics clock may (or may not) be run at a higher rate while
    120   1.19       cgd  * profiling.  This profile clock runs at profhz.  We require that profhz
    121   1.19       cgd  * be an integral multiple of stathz.
    122   1.19       cgd  *
    123   1.19       cgd  * If the statistics clock is running fast, it must be divided by the ratio
    124   1.19       cgd  * profhz/stathz for statistics.  (For profiling, every tick counts.)
    125   1.19       cgd  */
    126   1.19       cgd 
    127   1.99    kardel #ifndef __HAVE_TIMECOUNTER
    128   1.27  jonathan #ifdef NTP	/* NTP phase-locked loop in kernel */
    129   1.27  jonathan /*
    130   1.27  jonathan  * Phase/frequency-lock loop (PLL/FLL) definitions
    131   1.27  jonathan  *
    132   1.27  jonathan  * The following variables are read and set by the ntp_adjtime() system
    133   1.27  jonathan  * call.
    134   1.27  jonathan  *
    135   1.27  jonathan  * time_state shows the state of the system clock, with values defined
    136   1.27  jonathan  * in the timex.h header file.
    137   1.27  jonathan  *
    138   1.27  jonathan  * time_status shows the status of the system clock, with bits defined
    139   1.27  jonathan  * in the timex.h header file.
    140   1.27  jonathan  *
    141   1.27  jonathan  * time_offset is used by the PLL/FLL to adjust the system time in small
    142   1.27  jonathan  * increments.
    143   1.27  jonathan  *
    144   1.27  jonathan  * time_constant determines the bandwidth or "stiffness" of the PLL.
    145   1.27  jonathan  *
    146   1.27  jonathan  * time_tolerance determines maximum frequency error or tolerance of the
    147   1.27  jonathan  * CPU clock oscillator and is a property of the architecture; however,
    148   1.27  jonathan  * in principle it could change as result of the presence of external
    149   1.27  jonathan  * discipline signals, for instance.
    150   1.27  jonathan  *
    151   1.27  jonathan  * time_precision is usually equal to the kernel tick variable; however,
    152   1.27  jonathan  * in cases where a precision clock counter or external clock is
    153   1.27  jonathan  * available, the resolution can be much less than this and depend on
    154   1.27  jonathan  * whether the external clock is working or not.
    155   1.27  jonathan  *
    156   1.27  jonathan  * time_maxerror is initialized by a ntp_adjtime() call and increased by
    157   1.27  jonathan  * the kernel once each second to reflect the maximum error bound
    158   1.27  jonathan  * growth.
    159   1.27  jonathan  *
    160   1.27  jonathan  * time_esterror is set and read by the ntp_adjtime() call, but
    161   1.27  jonathan  * otherwise not used by the kernel.
    162   1.27  jonathan  */
    163   1.27  jonathan int time_state = TIME_OK;	/* clock state */
    164   1.27  jonathan int time_status = STA_UNSYNC;	/* clock status bits */
    165   1.27  jonathan long time_offset = 0;		/* time offset (us) */
    166   1.27  jonathan long time_constant = 0;		/* pll time constant */
    167   1.27  jonathan long time_tolerance = MAXFREQ;	/* frequency tolerance (scaled ppm) */
    168   1.27  jonathan long time_precision = 1;	/* clock precision (us) */
    169   1.27  jonathan long time_maxerror = MAXPHASE;	/* maximum error (us) */
    170   1.27  jonathan long time_esterror = MAXPHASE;	/* estimated error (us) */
    171   1.27  jonathan 
    172   1.27  jonathan /*
    173   1.27  jonathan  * The following variables establish the state of the PLL/FLL and the
    174   1.27  jonathan  * residual time and frequency offset of the local clock. The scale
    175   1.27  jonathan  * factors are defined in the timex.h header file.
    176   1.27  jonathan  *
    177   1.27  jonathan  * time_phase and time_freq are the phase increment and the frequency
    178   1.27  jonathan  * increment, respectively, of the kernel time variable.
    179   1.27  jonathan  *
    180   1.27  jonathan  * time_freq is set via ntp_adjtime() from a value stored in a file when
    181   1.27  jonathan  * the synchronization daemon is first started. Its value is retrieved
    182   1.27  jonathan  * via ntp_adjtime() and written to the file about once per hour by the
    183   1.27  jonathan  * daemon.
    184   1.27  jonathan  *
    185   1.27  jonathan  * time_adj is the adjustment added to the value of tick at each timer
    186   1.27  jonathan  * interrupt and is recomputed from time_phase and time_freq at each
    187   1.27  jonathan  * seconds rollover.
    188   1.27  jonathan  *
    189   1.27  jonathan  * time_reftime is the second's portion of the system time at the last
    190   1.27  jonathan  * call to ntp_adjtime(). It is used to adjust the time_freq variable
    191   1.27  jonathan  * and to increase the time_maxerror as the time since last update
    192   1.27  jonathan  * increases.
    193   1.27  jonathan  */
    194   1.27  jonathan long time_phase = 0;		/* phase offset (scaled us) */
    195   1.27  jonathan long time_freq = 0;		/* frequency offset (scaled ppm) */
    196   1.27  jonathan long time_adj = 0;		/* tick adjust (scaled 1 / hz) */
    197   1.27  jonathan long time_reftime = 0;		/* time at last adjustment (s) */
    198   1.27  jonathan 
    199   1.27  jonathan #ifdef PPS_SYNC
    200   1.27  jonathan /*
    201   1.27  jonathan  * The following variables are used only if the kernel PPS discipline
    202   1.27  jonathan  * code is configured (PPS_SYNC). The scale factors are defined in the
    203   1.27  jonathan  * timex.h header file.
    204   1.27  jonathan  *
    205   1.27  jonathan  * pps_time contains the time at each calibration interval, as read by
    206   1.27  jonathan  * microtime(). pps_count counts the seconds of the calibration
    207   1.27  jonathan  * interval, the duration of which is nominally pps_shift in powers of
    208   1.27  jonathan  * two.
    209   1.27  jonathan  *
    210   1.27  jonathan  * pps_offset is the time offset produced by the time median filter
    211   1.27  jonathan  * pps_tf[], while pps_jitter is the dispersion (jitter) measured by
    212   1.27  jonathan  * this filter.
    213   1.27  jonathan  *
    214   1.27  jonathan  * pps_freq is the frequency offset produced by the frequency median
    215   1.27  jonathan  * filter pps_ff[], while pps_stabil is the dispersion (wander) measured
    216   1.27  jonathan  * by this filter.
    217   1.27  jonathan  *
    218   1.27  jonathan  * pps_usec is latched from a high resolution counter or external clock
    219   1.27  jonathan  * at pps_time. Here we want the hardware counter contents only, not the
    220   1.27  jonathan  * contents plus the time_tv.usec as usual.
    221   1.27  jonathan  *
    222   1.27  jonathan  * pps_valid counts the number of seconds since the last PPS update. It
    223   1.27  jonathan  * is used as a watchdog timer to disable the PPS discipline should the
    224   1.27  jonathan  * PPS signal be lost.
    225   1.27  jonathan  *
    226   1.27  jonathan  * pps_glitch counts the number of seconds since the beginning of an
    227   1.27  jonathan  * offset burst more than tick/2 from current nominal offset. It is used
    228   1.27  jonathan  * mainly to suppress error bursts due to priority conflicts between the
    229   1.27  jonathan  * PPS interrupt and timer interrupt.
    230   1.27  jonathan  *
    231   1.27  jonathan  * pps_intcnt counts the calibration intervals for use in the interval-
    232   1.27  jonathan  * adaptation algorithm. It's just too complicated for words.
    233   1.89    simonb  *
    234   1.89    simonb  * pps_kc_hardpps_source contains an arbitrary value that uniquely
    235   1.89    simonb  * identifies the currently bound source of the PPS signal, or NULL
    236   1.89    simonb  * if no source is bound.
    237   1.89    simonb  *
    238   1.89    simonb  * pps_kc_hardpps_mode indicates which transitions, if any, of the PPS
    239   1.89    simonb  * signal should be reported.
    240   1.27  jonathan  */
    241   1.27  jonathan struct timeval pps_time;	/* kernel time at last interval */
    242   1.27  jonathan long pps_tf[] = {0, 0, 0};	/* pps time offset median filter (us) */
    243   1.27  jonathan long pps_offset = 0;		/* pps time offset (us) */
    244   1.27  jonathan long pps_jitter = MAXTIME;	/* time dispersion (jitter) (us) */
    245   1.27  jonathan long pps_ff[] = {0, 0, 0};	/* pps frequency offset median filter */
    246   1.27  jonathan long pps_freq = 0;		/* frequency offset (scaled ppm) */
    247   1.27  jonathan long pps_stabil = MAXFREQ;	/* frequency dispersion (scaled ppm) */
    248   1.27  jonathan long pps_usec = 0;		/* microsec counter at last interval */
    249   1.27  jonathan long pps_valid = PPS_VALID;	/* pps signal watchdog counter */
    250   1.27  jonathan int pps_glitch = 0;		/* pps signal glitch counter */
    251   1.27  jonathan int pps_count = 0;		/* calibration interval counter (s) */
    252   1.27  jonathan int pps_shift = PPS_SHIFT;	/* interval duration (s) (shift) */
    253   1.27  jonathan int pps_intcnt = 0;		/* intervals at current duration */
    254   1.89    simonb void *pps_kc_hardpps_source = NULL; /* current PPS supplier's identifier */
    255   1.89    simonb int pps_kc_hardpps_mode = 0;	/* interesting edges of PPS signal */
    256   1.27  jonathan 
    257   1.27  jonathan /*
    258   1.27  jonathan  * PPS signal quality monitors
    259   1.27  jonathan  *
    260   1.27  jonathan  * pps_jitcnt counts the seconds that have been discarded because the
    261   1.27  jonathan  * jitter measured by the time median filter exceeds the limit MAXTIME
    262   1.27  jonathan  * (100 us).
    263   1.27  jonathan  *
    264   1.27  jonathan  * pps_calcnt counts the frequency calibration intervals, which are
    265   1.27  jonathan  * variable from 4 s to 256 s.
    266   1.27  jonathan  *
    267   1.27  jonathan  * pps_errcnt counts the calibration intervals which have been discarded
    268   1.27  jonathan  * because the wander exceeds the limit MAXFREQ (100 ppm) or where the
    269   1.27  jonathan  * calibration interval jitter exceeds two ticks.
    270   1.27  jonathan  *
    271   1.27  jonathan  * pps_stbcnt counts the calibration intervals that have been discarded
    272   1.27  jonathan  * because the frequency wander exceeds the limit MAXFREQ / 4 (25 us).
    273   1.27  jonathan  */
    274   1.27  jonathan long pps_jitcnt = 0;		/* jitter limit exceeded */
    275   1.27  jonathan long pps_calcnt = 0;		/* calibration intervals */
    276   1.27  jonathan long pps_errcnt = 0;		/* calibration errors */
    277   1.27  jonathan long pps_stbcnt = 0;		/* stability limit exceeded */
    278   1.27  jonathan #endif /* PPS_SYNC */
    279   1.27  jonathan 
    280   1.27  jonathan #ifdef EXT_CLOCK
    281   1.27  jonathan /*
    282   1.27  jonathan  * External clock definitions
    283   1.27  jonathan  *
    284   1.27  jonathan  * The following definitions and declarations are used only if an
    285   1.27  jonathan  * external clock is configured on the system.
    286   1.27  jonathan  */
    287   1.27  jonathan #define CLOCK_INTERVAL 30	/* CPU clock update interval (s) */
    288   1.27  jonathan 
    289   1.27  jonathan /*
    290   1.27  jonathan  * The clock_count variable is set to CLOCK_INTERVAL at each PPS
    291   1.27  jonathan  * interrupt and decremented once each second.
    292   1.27  jonathan  */
    293   1.27  jonathan int clock_count = 0;		/* CPU clock counter */
    294   1.27  jonathan 
    295   1.27  jonathan #endif /* EXT_CLOCK */
    296   1.27  jonathan #endif /* NTP */
    297   1.27  jonathan 
    298   1.19       cgd /*
    299   1.19       cgd  * Bump a timeval by a small number of usec's.
    300   1.19       cgd  */
    301   1.19       cgd #define BUMPTIME(t, usec) { \
    302   1.55  augustss 	volatile struct timeval *tp = (t); \
    303   1.55  augustss 	long us; \
    304   1.19       cgd  \
    305   1.19       cgd 	tp->tv_usec = us = tp->tv_usec + (usec); \
    306   1.19       cgd 	if (us >= 1000000) { \
    307   1.19       cgd 		tp->tv_usec = us - 1000000; \
    308   1.19       cgd 		tp->tv_sec++; \
    309   1.19       cgd 	} \
    310   1.19       cgd }
    311   1.99    kardel #endif /* !__HAVE_TIMECOUNTER */
    312   1.19       cgd 
    313   1.19       cgd int	stathz;
    314   1.19       cgd int	profhz;
    315   1.80    briggs int	profsrc;
    316   1.75    simonb int	schedhz;
    317   1.19       cgd int	profprocs;
    318  1.100  drochner int	hardclock_ticks;
    319   1.91      yamt static int statscheddiv; /* stat => sched divider (used if schedhz == 0) */
    320   1.70  sommerfe static int psdiv;			/* prof => stat divider */
    321   1.22       cgd int	psratio;			/* ratio: prof / stat */
    322   1.99    kardel #ifndef __HAVE_TIMECOUNTER
    323   1.22       cgd int	tickfix, tickfixinterval;	/* used if tick not really integral */
    324   1.34    briggs #ifndef NTP
    325   1.39       cgd static int tickfixcnt;			/* accumulated fractional error */
    326   1.34    briggs #else
    327   1.27  jonathan int	fixtick;			/* used by NTP for same */
    328   1.31   mycroft int	shifthz;
    329   1.31   mycroft #endif
    330   1.19       cgd 
    331   1.48  christos /*
    332   1.48  christos  * We might want ldd to load the both words from time at once.
    333   1.48  christos  * To succeed we need to be quadword aligned.
    334   1.48  christos  * The sparc already does that, and that it has worked so far is a fluke.
    335   1.48  christos  */
    336   1.48  christos volatile struct	timeval time  __attribute__((__aligned__(__alignof__(quad_t))));
    337   1.19       cgd volatile struct	timeval mono_time;
    338   1.99    kardel #endif /* !__HAVE_TIMECOUNTER */
    339   1.19       cgd 
    340   1.99    kardel #ifdef __HAVE_TIMECOUNTER
    341   1.99    kardel static u_int get_intr_timecount(struct timecounter *);
    342   1.99    kardel 
    343   1.99    kardel static struct timecounter intr_timecounter = {
    344   1.99    kardel 	get_intr_timecount,	/* get_timecount */
    345   1.99    kardel 	0,			/* no poll_pps */
    346   1.99    kardel 	~0u,			/* counter_mask */
    347   1.99    kardel 	0,		        /* frequency */
    348   1.99    kardel 	"clockinterrupt",	/* name */
    349  1.102  christos 	0,			/* quality - minimum implementation level for a clock */
    350  1.102  christos 	NULL,			/* prev */
    351  1.102  christos 	NULL,			/* next */
    352   1.99    kardel };
    353   1.99    kardel 
    354   1.99    kardel static u_int
    355  1.104      yamt get_intr_timecount(struct timecounter *tc)
    356   1.99    kardel {
    357  1.104      yamt 
    358  1.100  drochner 	return (u_int)hardclock_ticks;
    359   1.99    kardel }
    360   1.99    kardel #endif
    361   1.73   thorpej 
    362   1.66   thorpej /*
    363   1.19       cgd  * Initialize clock frequencies and start both clocks running.
    364   1.19       cgd  */
    365   1.19       cgd void
    366   1.63   thorpej initclocks(void)
    367   1.19       cgd {
    368   1.55  augustss 	int i;
    369   1.19       cgd 
    370   1.19       cgd 	/*
    371   1.19       cgd 	 * Set divisors to 1 (normal case) and let the machine-specific
    372   1.19       cgd 	 * code do its bit.
    373   1.19       cgd 	 */
    374   1.70  sommerfe 	psdiv = 1;
    375   1.99    kardel #ifdef __HAVE_TIMECOUNTER
    376   1.99    kardel 	/*
    377   1.99    kardel 	 * provide minimum default time counter
    378   1.99    kardel 	 * will only run at interrupt resolution
    379   1.99    kardel 	 */
    380   1.99    kardel 	intr_timecounter.tc_frequency = hz;
    381   1.99    kardel 	tc_init(&intr_timecounter);
    382   1.99    kardel #endif
    383   1.19       cgd 	cpu_initclocks();
    384   1.19       cgd 
    385   1.19       cgd 	/*
    386  1.108      yamt 	 * Compute profhz and stathz, fix profhz if needed.
    387   1.19       cgd 	 */
    388   1.19       cgd 	i = stathz ? stathz : hz;
    389   1.19       cgd 	if (profhz == 0)
    390   1.19       cgd 		profhz = i;
    391   1.19       cgd 	psratio = profhz / i;
    392   1.91      yamt 	if (schedhz == 0) {
    393   1.91      yamt 		/* 16Hz is best */
    394   1.91      yamt 		statscheddiv = i / 16;
    395   1.91      yamt 		if (statscheddiv <= 0)
    396   1.91      yamt 			panic("statscheddiv");
    397   1.91      yamt 	}
    398   1.31   mycroft 
    399   1.99    kardel #ifndef __HAVE_TIMECOUNTER
    400   1.31   mycroft #ifdef NTP
    401   1.31   mycroft 	switch (hz) {
    402   1.57   mycroft 	case 1:
    403   1.57   mycroft 		shifthz = SHIFT_SCALE - 0;
    404   1.57   mycroft 		break;
    405   1.57   mycroft 	case 2:
    406   1.57   mycroft 		shifthz = SHIFT_SCALE - 1;
    407   1.57   mycroft 		break;
    408   1.57   mycroft 	case 4:
    409   1.57   mycroft 		shifthz = SHIFT_SCALE - 2;
    410   1.57   mycroft 		break;
    411   1.57   mycroft 	case 8:
    412   1.57   mycroft 		shifthz = SHIFT_SCALE - 3;
    413   1.57   mycroft 		break;
    414   1.57   mycroft 	case 16:
    415   1.57   mycroft 		shifthz = SHIFT_SCALE - 4;
    416   1.57   mycroft 		break;
    417   1.57   mycroft 	case 32:
    418   1.57   mycroft 		shifthz = SHIFT_SCALE - 5;
    419   1.57   mycroft 		break;
    420   1.92       tls 	case 50:
    421   1.31   mycroft 	case 60:
    422   1.31   mycroft 	case 64:
    423   1.31   mycroft 		shifthz = SHIFT_SCALE - 6;
    424   1.31   mycroft 		break;
    425   1.31   mycroft 	case 96:
    426   1.31   mycroft 	case 100:
    427   1.31   mycroft 	case 128:
    428   1.31   mycroft 		shifthz = SHIFT_SCALE - 7;
    429   1.31   mycroft 		break;
    430   1.31   mycroft 	case 256:
    431   1.31   mycroft 		shifthz = SHIFT_SCALE - 8;
    432   1.41       tls 		break;
    433   1.41       tls 	case 512:
    434   1.41       tls 		shifthz = SHIFT_SCALE - 9;
    435   1.31   mycroft 		break;
    436   1.43      ross 	case 1000:
    437   1.31   mycroft 	case 1024:
    438   1.31   mycroft 		shifthz = SHIFT_SCALE - 10;
    439   1.31   mycroft 		break;
    440   1.57   mycroft 	case 1200:
    441   1.57   mycroft 	case 2048:
    442   1.57   mycroft 		shifthz = SHIFT_SCALE - 11;
    443   1.57   mycroft 		break;
    444   1.57   mycroft 	case 4096:
    445   1.57   mycroft 		shifthz = SHIFT_SCALE - 12;
    446   1.57   mycroft 		break;
    447   1.57   mycroft 	case 8192:
    448   1.57   mycroft 		shifthz = SHIFT_SCALE - 13;
    449   1.57   mycroft 		break;
    450   1.57   mycroft 	case 16384:
    451   1.57   mycroft 		shifthz = SHIFT_SCALE - 14;
    452   1.57   mycroft 		break;
    453   1.57   mycroft 	case 32768:
    454   1.57   mycroft 		shifthz = SHIFT_SCALE - 15;
    455   1.57   mycroft 		break;
    456   1.57   mycroft 	case 65536:
    457   1.57   mycroft 		shifthz = SHIFT_SCALE - 16;
    458   1.57   mycroft 		break;
    459   1.31   mycroft 	default:
    460   1.31   mycroft 		panic("weird hz");
    461   1.50  sommerfe 	}
    462   1.50  sommerfe 	if (fixtick == 0) {
    463   1.52   thorpej 		/*
    464   1.52   thorpej 		 * Give MD code a chance to set this to a better
    465   1.52   thorpej 		 * value; but, if it doesn't, we should.
    466   1.52   thorpej 		 */
    467   1.50  sommerfe 		fixtick = (1000000 - (hz*tick));
    468   1.31   mycroft 	}
    469   1.99    kardel #endif /* NTP */
    470   1.99    kardel #endif /* !__HAVE_TIMECOUNTER */
    471   1.19       cgd }
    472   1.19       cgd 
    473   1.19       cgd /*
    474   1.19       cgd  * The real-time timer, interrupting hz times per second.
    475   1.19       cgd  */
    476   1.19       cgd void
    477   1.63   thorpej hardclock(struct clockframe *frame)
    478   1.19       cgd {
    479   1.82   thorpej 	struct lwp *l;
    480   1.55  augustss 	struct proc *p;
    481   1.99    kardel 	struct cpu_info *ci = curcpu();
    482   1.99    kardel 	struct ptimer *pt;
    483   1.99    kardel #ifndef __HAVE_TIMECOUNTER
    484   1.55  augustss 	int delta;
    485   1.19       cgd 	extern int tickdelta;
    486   1.19       cgd 	extern long timedelta;
    487   1.30   mycroft #ifdef NTP
    488   1.55  augustss 	int time_update;
    489   1.55  augustss 	int ltemp;
    490   1.99    kardel #endif /* NTP */
    491   1.99    kardel #endif /* __HAVE_TIMECOUNTER */
    492   1.19       cgd 
    493   1.82   thorpej 	l = curlwp;
    494  1.108      yamt 	if (!CURCPU_IDLE_P()) {
    495   1.82   thorpej 		p = l->l_proc;
    496   1.19       cgd 		/*
    497   1.19       cgd 		 * Run current process's virtual and profile time, as needed.
    498   1.19       cgd 		 */
    499   1.82   thorpej 		if (CLKF_USERMODE(frame) && p->p_timers &&
    500   1.82   thorpej 		    (pt = LIST_FIRST(&p->p_timers->pts_virtual)) != NULL)
    501   1.82   thorpej 			if (itimerdecr(pt, tick) == 0)
    502   1.82   thorpej 				itimerfire(pt);
    503   1.82   thorpej 		if (p->p_timers &&
    504   1.82   thorpej 		    (pt = LIST_FIRST(&p->p_timers->pts_prof)) != NULL)
    505   1.82   thorpej 			if (itimerdecr(pt, tick) == 0)
    506   1.82   thorpej 				itimerfire(pt);
    507   1.19       cgd 	}
    508   1.19       cgd 
    509   1.19       cgd 	/*
    510   1.19       cgd 	 * If no separate statistics clock is available, run it from here.
    511   1.19       cgd 	 */
    512   1.19       cgd 	if (stathz == 0)
    513   1.19       cgd 		statclock(frame);
    514  1.108      yamt 	if ((--ci->ci_schedstate.spc_ticks) <= 0)
    515  1.108      yamt 		sched_tick(ci);
    516   1.93     perry 
    517   1.60   thorpej #if defined(MULTIPROCESSOR)
    518   1.60   thorpej 	/*
    519   1.60   thorpej 	 * If we are not the primary CPU, we're not allowed to do
    520   1.60   thorpej 	 * any more work.
    521   1.60   thorpej 	 */
    522   1.70  sommerfe 	if (CPU_IS_PRIMARY(ci) == 0)
    523   1.60   thorpej 		return;
    524   1.60   thorpej #endif
    525   1.60   thorpej 
    526   1.99    kardel 	hardclock_ticks++;
    527   1.99    kardel 
    528   1.99    kardel #ifdef __HAVE_TIMECOUNTER
    529   1.99    kardel 	tc_ticktock();
    530   1.99    kardel #else /* __HAVE_TIMECOUNTER */
    531   1.19       cgd 	/*
    532   1.22       cgd 	 * Increment the time-of-day.  The increment is normally just
    533   1.22       cgd 	 * ``tick''.  If the machine is one which has a clock frequency
    534   1.22       cgd 	 * such that ``hz'' would not divide the second evenly into
    535   1.22       cgd 	 * milliseconds, a periodic adjustment must be applied.  Finally,
    536   1.22       cgd 	 * if we are still adjusting the time (see adjtime()),
    537   1.22       cgd 	 * ``tickdelta'' may also be added in.
    538   1.19       cgd 	 */
    539   1.22       cgd 	delta = tick;
    540   1.27  jonathan 
    541   1.27  jonathan #ifndef NTP
    542   1.22       cgd 	if (tickfix) {
    543   1.39       cgd 		tickfixcnt += tickfix;
    544   1.24       cgd 		if (tickfixcnt >= tickfixinterval) {
    545   1.39       cgd 			delta++;
    546   1.39       cgd 			tickfixcnt -= tickfixinterval;
    547   1.22       cgd 		}
    548   1.22       cgd 	}
    549   1.27  jonathan #endif /* !NTP */
    550   1.27  jonathan 	/* Imprecise 4bsd adjtime() handling */
    551   1.22       cgd 	if (timedelta != 0) {
    552   1.38       cgd 		delta += tickdelta;
    553   1.19       cgd 		timedelta -= tickdelta;
    554   1.19       cgd 	}
    555   1.27  jonathan 
    556   1.27  jonathan #ifdef notyet
    557   1.27  jonathan 	microset();
    558   1.27  jonathan #endif
    559   1.27  jonathan 
    560   1.27  jonathan #ifndef NTP
    561   1.27  jonathan 	BUMPTIME(&time, delta);		/* XXX Now done using NTP code below */
    562   1.27  jonathan #endif
    563   1.19       cgd 	BUMPTIME(&mono_time, delta);
    564   1.27  jonathan 
    565   1.31   mycroft #ifdef NTP
    566   1.30   mycroft 	time_update = delta;
    567   1.27  jonathan 
    568   1.27  jonathan 	/*
    569   1.27  jonathan 	 * Compute the phase adjustment. If the low-order bits
    570   1.27  jonathan 	 * (time_phase) of the update overflow, bump the high-order bits
    571   1.27  jonathan 	 * (time_update).
    572   1.27  jonathan 	 */
    573   1.27  jonathan 	time_phase += time_adj;
    574   1.27  jonathan 	if (time_phase <= -FINEUSEC) {
    575   1.27  jonathan 		ltemp = -time_phase >> SHIFT_SCALE;
    576   1.27  jonathan 		time_phase += ltemp << SHIFT_SCALE;
    577   1.27  jonathan 		time_update -= ltemp;
    578   1.31   mycroft 	} else if (time_phase >= FINEUSEC) {
    579   1.27  jonathan 		ltemp = time_phase >> SHIFT_SCALE;
    580   1.27  jonathan 		time_phase -= ltemp << SHIFT_SCALE;
    581   1.27  jonathan 		time_update += ltemp;
    582   1.27  jonathan 	}
    583   1.27  jonathan 	time.tv_usec += time_update;
    584   1.27  jonathan 
    585   1.27  jonathan 	/*
    586   1.27  jonathan 	 * On rollover of the second the phase adjustment to be used for
    587   1.27  jonathan 	 * the next second is calculated. Also, the maximum error is
    588   1.27  jonathan 	 * increased by the tolerance. If the PPS frequency discipline
    589   1.27  jonathan 	 * code is present, the phase is increased to compensate for the
    590   1.27  jonathan 	 * CPU clock oscillator frequency error.
    591   1.27  jonathan 	 *
    592   1.27  jonathan  	 * On a 32-bit machine and given parameters in the timex.h
    593   1.27  jonathan 	 * header file, the maximum phase adjustment is +-512 ms and
    594   1.27  jonathan 	 * maximum frequency offset is a tad less than) +-512 ppm. On a
    595   1.27  jonathan 	 * 64-bit machine, you shouldn't need to ask.
    596   1.27  jonathan 	 */
    597   1.27  jonathan 	if (time.tv_usec >= 1000000) {
    598   1.27  jonathan 		time.tv_usec -= 1000000;
    599   1.27  jonathan 		time.tv_sec++;
    600   1.27  jonathan 		time_maxerror += time_tolerance >> SHIFT_USEC;
    601   1.27  jonathan 
    602   1.27  jonathan 		/*
    603   1.27  jonathan 		 * Leap second processing. If in leap-insert state at
    604   1.27  jonathan 		 * the end of the day, the system clock is set back one
    605   1.27  jonathan 		 * second; if in leap-delete state, the system clock is
    606   1.27  jonathan 		 * set ahead one second. The microtime() routine or
    607   1.27  jonathan 		 * external clock driver will insure that reported time
    608   1.27  jonathan 		 * is always monotonic. The ugly divides should be
    609   1.27  jonathan 		 * replaced.
    610   1.27  jonathan 		 */
    611   1.27  jonathan 		switch (time_state) {
    612   1.31   mycroft 		case TIME_OK:
    613   1.27  jonathan 			if (time_status & STA_INS)
    614   1.27  jonathan 				time_state = TIME_INS;
    615   1.27  jonathan 			else if (time_status & STA_DEL)
    616   1.27  jonathan 				time_state = TIME_DEL;
    617   1.27  jonathan 			break;
    618   1.27  jonathan 
    619   1.31   mycroft 		case TIME_INS:
    620   1.27  jonathan 			if (time.tv_sec % 86400 == 0) {
    621   1.27  jonathan 				time.tv_sec--;
    622   1.27  jonathan 				time_state = TIME_OOP;
    623   1.27  jonathan 			}
    624   1.27  jonathan 			break;
    625   1.27  jonathan 
    626   1.31   mycroft 		case TIME_DEL:
    627   1.27  jonathan 			if ((time.tv_sec + 1) % 86400 == 0) {
    628   1.27  jonathan 				time.tv_sec++;
    629   1.27  jonathan 				time_state = TIME_WAIT;
    630   1.27  jonathan 			}
    631   1.27  jonathan 			break;
    632   1.27  jonathan 
    633   1.31   mycroft 		case TIME_OOP:
    634   1.27  jonathan 			time_state = TIME_WAIT;
    635   1.27  jonathan 			break;
    636   1.27  jonathan 
    637   1.31   mycroft 		case TIME_WAIT:
    638   1.27  jonathan 			if (!(time_status & (STA_INS | STA_DEL)))
    639   1.27  jonathan 				time_state = TIME_OK;
    640   1.31   mycroft 			break;
    641   1.27  jonathan 		}
    642   1.27  jonathan 
    643   1.27  jonathan 		/*
    644   1.27  jonathan 		 * Compute the phase adjustment for the next second. In
    645   1.27  jonathan 		 * PLL mode, the offset is reduced by a fixed factor
    646   1.27  jonathan 		 * times the time constant. In FLL mode the offset is
    647   1.27  jonathan 		 * used directly. In either mode, the maximum phase
    648   1.27  jonathan 		 * adjustment for each second is clamped so as to spread
    649   1.27  jonathan 		 * the adjustment over not more than the number of
    650   1.27  jonathan 		 * seconds between updates.
    651   1.27  jonathan 		 */
    652   1.27  jonathan 		if (time_offset < 0) {
    653   1.27  jonathan 			ltemp = -time_offset;
    654   1.27  jonathan 			if (!(time_status & STA_FLL))
    655   1.27  jonathan 				ltemp >>= SHIFT_KG + time_constant;
    656   1.27  jonathan 			if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
    657   1.27  jonathan 				ltemp = (MAXPHASE / MINSEC) <<
    658   1.27  jonathan 				    SHIFT_UPDATE;
    659   1.27  jonathan 			time_offset += ltemp;
    660   1.31   mycroft 			time_adj = -ltemp << (shifthz - SHIFT_UPDATE);
    661   1.31   mycroft 		} else if (time_offset > 0) {
    662   1.27  jonathan 			ltemp = time_offset;
    663   1.27  jonathan 			if (!(time_status & STA_FLL))
    664   1.27  jonathan 				ltemp >>= SHIFT_KG + time_constant;
    665   1.27  jonathan 			if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
    666   1.27  jonathan 				ltemp = (MAXPHASE / MINSEC) <<
    667   1.27  jonathan 				    SHIFT_UPDATE;
    668   1.27  jonathan 			time_offset -= ltemp;
    669   1.31   mycroft 			time_adj = ltemp << (shifthz - SHIFT_UPDATE);
    670   1.31   mycroft 		} else
    671   1.31   mycroft 			time_adj = 0;
    672   1.27  jonathan 
    673   1.27  jonathan 		/*
    674   1.27  jonathan 		 * Compute the frequency estimate and additional phase
    675   1.27  jonathan 		 * adjustment due to frequency error for the next
    676   1.27  jonathan 		 * second. When the PPS signal is engaged, gnaw on the
    677   1.27  jonathan 		 * watchdog counter and update the frequency computed by
    678   1.27  jonathan 		 * the pll and the PPS signal.
    679   1.27  jonathan 		 */
    680   1.27  jonathan #ifdef PPS_SYNC
    681   1.27  jonathan 		pps_valid++;
    682   1.27  jonathan 		if (pps_valid == PPS_VALID) {
    683   1.27  jonathan 			pps_jitter = MAXTIME;
    684   1.27  jonathan 			pps_stabil = MAXFREQ;
    685   1.27  jonathan 			time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
    686   1.27  jonathan 			    STA_PPSWANDER | STA_PPSERROR);
    687   1.27  jonathan 		}
    688   1.27  jonathan 		ltemp = time_freq + pps_freq;
    689   1.27  jonathan #else
    690   1.27  jonathan 		ltemp = time_freq;
    691   1.27  jonathan #endif /* PPS_SYNC */
    692   1.27  jonathan 
    693   1.27  jonathan 		if (ltemp < 0)
    694   1.31   mycroft 			time_adj -= -ltemp >> (SHIFT_USEC - shifthz);
    695   1.27  jonathan 		else
    696   1.31   mycroft 			time_adj += ltemp >> (SHIFT_USEC - shifthz);
    697   1.31   mycroft 		time_adj += (long)fixtick << shifthz;
    698   1.27  jonathan 
    699   1.27  jonathan 		/*
    700   1.27  jonathan 		 * When the CPU clock oscillator frequency is not a
    701   1.31   mycroft 		 * power of 2 in Hz, shifthz is only an approximate
    702   1.31   mycroft 		 * scale factor.
    703   1.46   mycroft 		 *
    704   1.46   mycroft 		 * To determine the adjustment, you can do the following:
    705   1.46   mycroft 		 *   bc -q
    706   1.46   mycroft 		 *   scale=24
    707   1.46   mycroft 		 *   obase=2
    708   1.46   mycroft 		 *   idealhz/realhz
    709   1.46   mycroft 		 * where `idealhz' is the next higher power of 2, and `realhz'
    710   1.57   mycroft 		 * is the actual value.  You may need to factor this result
    711   1.57   mycroft 		 * into a sequence of 2 multipliers to get better precision.
    712   1.46   mycroft 		 *
    713   1.46   mycroft 		 * Likewise, the error can be calculated with (e.g. for 100Hz):
    714   1.46   mycroft 		 *   bc -q
    715   1.46   mycroft 		 *   scale=24
    716   1.57   mycroft 		 *   ((1+2^-2+2^-5)*(1-2^-10)*realhz-idealhz)/idealhz
    717   1.57   mycroft 		 * (and then multiply by 1000000 to get ppm).
    718   1.27  jonathan 		 */
    719   1.31   mycroft 		switch (hz) {
    720   1.58   mycroft 		case 60:
    721   1.58   mycroft 			/* A factor of 1.000100010001 gives about 15ppm
    722   1.58   mycroft 			   error. */
    723   1.58   mycroft 			if (time_adj < 0) {
    724   1.58   mycroft 				time_adj -= (-time_adj >> 4);
    725   1.58   mycroft 				time_adj -= (-time_adj >> 8);
    726   1.58   mycroft 			} else {
    727   1.58   mycroft 				time_adj += (time_adj >> 4);
    728   1.58   mycroft 				time_adj += (time_adj >> 8);
    729   1.58   mycroft 			}
    730   1.58   mycroft 			break;
    731   1.58   mycroft 
    732   1.31   mycroft 		case 96:
    733   1.56   mycroft 			/* A factor of 1.0101010101 gives about 244ppm error. */
    734   1.46   mycroft 			if (time_adj < 0) {
    735   1.46   mycroft 				time_adj -= (-time_adj >> 2);
    736   1.46   mycroft 				time_adj -= (-time_adj >> 4) + (-time_adj >> 8);
    737   1.46   mycroft 			} else {
    738   1.46   mycroft 				time_adj += (time_adj >> 2);
    739   1.46   mycroft 				time_adj += (time_adj >> 4) + (time_adj >> 8);
    740   1.46   mycroft 			}
    741   1.46   mycroft 			break;
    742   1.46   mycroft 
    743   1.92       tls 		case 50:
    744   1.31   mycroft 		case 100:
    745   1.56   mycroft 			/* A factor of 1.010001111010111 gives about 1ppm
    746   1.56   mycroft 			   error. */
    747   1.56   mycroft 			if (time_adj < 0) {
    748   1.46   mycroft 				time_adj -= (-time_adj >> 2) + (-time_adj >> 5);
    749   1.56   mycroft 				time_adj += (-time_adj >> 10);
    750   1.56   mycroft 			} else {
    751   1.46   mycroft 				time_adj += (time_adj >> 2) + (time_adj >> 5);
    752   1.56   mycroft 				time_adj -= (time_adj >> 10);
    753   1.56   mycroft 			}
    754   1.43      ross 			break;
    755   1.46   mycroft 
    756   1.43      ross 		case 1000:
    757   1.56   mycroft 			/* A factor of 1.000001100010100001 gives about 50ppm
    758   1.56   mycroft 			   error. */
    759   1.56   mycroft 			if (time_adj < 0) {
    760   1.56   mycroft 				time_adj -= (-time_adj >> 6) + (-time_adj >> 11);
    761   1.56   mycroft 				time_adj -= (-time_adj >> 7);
    762   1.56   mycroft 			} else {
    763   1.56   mycroft 				time_adj += (time_adj >> 6) + (time_adj >> 11);
    764   1.56   mycroft 				time_adj += (time_adj >> 7);
    765   1.56   mycroft 			}
    766   1.56   mycroft 			break;
    767   1.56   mycroft 
    768   1.56   mycroft 		case 1200:
    769   1.56   mycroft 			/* A factor of 1.1011010011100001 gives about 64ppm
    770   1.56   mycroft 			   error. */
    771   1.56   mycroft 			if (time_adj < 0) {
    772   1.56   mycroft 				time_adj -= (-time_adj >> 1) + (-time_adj >> 6);
    773   1.56   mycroft 				time_adj -= (-time_adj >> 3) + (-time_adj >> 10);
    774   1.56   mycroft 			} else {
    775   1.56   mycroft 				time_adj += (time_adj >> 1) + (time_adj >> 6);
    776   1.56   mycroft 				time_adj += (time_adj >> 3) + (time_adj >> 10);
    777   1.56   mycroft 			}
    778   1.31   mycroft 			break;
    779   1.27  jonathan 		}
    780   1.27  jonathan 
    781   1.27  jonathan #ifdef EXT_CLOCK
    782   1.27  jonathan 		/*
    783   1.27  jonathan 		 * If an external clock is present, it is necessary to
    784   1.27  jonathan 		 * discipline the kernel time variable anyway, since not
    785   1.27  jonathan 		 * all system components use the microtime() interface.
    786   1.27  jonathan 		 * Here, the time offset between the external clock and
    787   1.27  jonathan 		 * kernel time variable is computed every so often.
    788   1.27  jonathan 		 */
    789   1.27  jonathan 		clock_count++;
    790   1.27  jonathan 		if (clock_count > CLOCK_INTERVAL) {
    791   1.27  jonathan 			clock_count = 0;
    792   1.27  jonathan 			microtime(&clock_ext);
    793   1.27  jonathan 			delta.tv_sec = clock_ext.tv_sec - time.tv_sec;
    794   1.27  jonathan 			delta.tv_usec = clock_ext.tv_usec -
    795   1.27  jonathan 			    time.tv_usec;
    796   1.27  jonathan 			if (delta.tv_usec < 0)
    797   1.27  jonathan 				delta.tv_sec--;
    798   1.27  jonathan 			if (delta.tv_usec >= 500000) {
    799   1.27  jonathan 				delta.tv_usec -= 1000000;
    800   1.27  jonathan 				delta.tv_sec++;
    801   1.27  jonathan 			}
    802   1.27  jonathan 			if (delta.tv_usec < -500000) {
    803   1.27  jonathan 				delta.tv_usec += 1000000;
    804   1.27  jonathan 				delta.tv_sec--;
    805   1.27  jonathan 			}
    806   1.27  jonathan 			if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
    807   1.27  jonathan 			    delta.tv_usec > MAXPHASE) ||
    808   1.27  jonathan 			    delta.tv_sec < -1 || (delta.tv_sec == -1 &&
    809   1.27  jonathan 			    delta.tv_usec < -MAXPHASE)) {
    810   1.27  jonathan 				time = clock_ext;
    811   1.27  jonathan 				delta.tv_sec = 0;
    812   1.27  jonathan 				delta.tv_usec = 0;
    813   1.27  jonathan 			}
    814   1.27  jonathan 			hardupdate(delta.tv_usec);
    815   1.27  jonathan 		}
    816   1.27  jonathan #endif /* EXT_CLOCK */
    817   1.27  jonathan 	}
    818   1.27  jonathan 
    819   1.31   mycroft #endif /* NTP */
    820   1.99    kardel #endif /* !__HAVE_TIMECOUNTER */
    821   1.19       cgd 
    822   1.19       cgd 	/*
    823  1.106        ad 	 * Update real-time timeout queue.  Callouts are processed at a
    824  1.106        ad 	 * very low CPU priority, so we don't keep the relatively high
    825  1.106        ad 	 * clock interrupt priority any longer than necessary.
    826  1.106        ad 	 */
    827  1.109        ad 	callout_hardclock();
    828   1.19       cgd }
    829   1.19       cgd 
    830   1.19       cgd /*
    831   1.19       cgd  * Start profiling on a process.
    832   1.19       cgd  *
    833   1.19       cgd  * Kernel profiling passes proc0 which never exits and hence
    834   1.19       cgd  * keeps the profile clock running constantly.
    835   1.19       cgd  */
    836   1.19       cgd void
    837   1.63   thorpej startprofclock(struct proc *p)
    838   1.19       cgd {
    839   1.19       cgd 
    840  1.109        ad 	KASSERT(mutex_owned(&p->p_stmutex));
    841  1.105        ad 
    842  1.105        ad 	if ((p->p_stflag & PST_PROFIL) == 0) {
    843  1.105        ad 		p->p_stflag |= PST_PROFIL;
    844   1.80    briggs 		/*
    845   1.80    briggs 		 * This is only necessary if using the clock as the
    846   1.80    briggs 		 * profiling source.
    847   1.80    briggs 		 */
    848   1.70  sommerfe 		if (++profprocs == 1 && stathz != 0)
    849   1.70  sommerfe 			psdiv = psratio;
    850   1.19       cgd 	}
    851   1.19       cgd }
    852   1.19       cgd 
    853   1.19       cgd /*
    854   1.19       cgd  * Stop profiling on a process.
    855   1.19       cgd  */
    856   1.19       cgd void
    857   1.63   thorpej stopprofclock(struct proc *p)
    858   1.19       cgd {
    859   1.19       cgd 
    860  1.109        ad 	KASSERT(mutex_owned(&p->p_stmutex));
    861  1.105        ad 
    862  1.105        ad 	if (p->p_stflag & PST_PROFIL) {
    863  1.105        ad 		p->p_stflag &= ~PST_PROFIL;
    864   1.80    briggs 		/*
    865   1.80    briggs 		 * This is only necessary if using the clock as the
    866   1.80    briggs 		 * profiling source.
    867   1.80    briggs 		 */
    868   1.70  sommerfe 		if (--profprocs == 0 && stathz != 0)
    869   1.70  sommerfe 			psdiv = 1;
    870   1.19       cgd 	}
    871   1.19       cgd }
    872   1.19       cgd 
    873   1.80    briggs #if defined(PERFCTRS)
    874   1.80    briggs /*
    875   1.80    briggs  * Independent profiling "tick" in case we're using a separate
    876   1.80    briggs  * clock or profiling event source.  Currently, that's just
    877   1.80    briggs  * performance counters--hence the wrapper.
    878   1.80    briggs  */
    879   1.80    briggs void
    880   1.80    briggs proftick(struct clockframe *frame)
    881   1.80    briggs {
    882   1.80    briggs #ifdef GPROF
    883   1.93     perry         struct gmonparam *g;
    884   1.93     perry         intptr_t i;
    885   1.80    briggs #endif
    886  1.105        ad 	struct lwp *l;
    887   1.80    briggs 	struct proc *p;
    888   1.80    briggs 
    889  1.105        ad 	l = curlwp;
    890  1.105        ad 	p = (l ? l->l_proc : NULL);
    891   1.80    briggs 	if (CLKF_USERMODE(frame)) {
    892  1.105        ad 		mutex_spin_enter(&p->p_stmutex);
    893  1.105        ad 		if (p->p_stflag & PST_PROFIL)
    894  1.105        ad 			addupc_intr(l, CLKF_PC(frame));
    895  1.105        ad 		mutex_spin_exit(&p->p_stmutex);
    896   1.80    briggs 	} else {
    897   1.80    briggs #ifdef GPROF
    898   1.80    briggs 		g = &_gmonparam;
    899   1.80    briggs 		if (g->state == GMON_PROF_ON) {
    900   1.80    briggs 			i = CLKF_PC(frame) - g->lowpc;
    901   1.80    briggs 			if (i < g->textsize) {
    902   1.80    briggs 				i /= HISTFRACTION * sizeof(*g->kcount);
    903   1.80    briggs 				g->kcount[i]++;
    904   1.80    briggs 			}
    905   1.80    briggs 		}
    906   1.80    briggs #endif
    907  1.111        ad #ifdef LWP_PC
    908  1.111        ad 		if (p != NULL && (p->p_stflag & PST_PROFIL) != 0)
    909  1.112        ad 			addupc_intr(l, LWP_PC(l));
    910   1.93     perry #endif
    911   1.80    briggs 	}
    912   1.80    briggs }
    913   1.80    briggs #endif
    914   1.80    briggs 
    915  1.108      yamt void
    916  1.108      yamt schedclock(struct lwp *l)
    917  1.108      yamt {
    918  1.108      yamt 
    919  1.108      yamt 	if ((l->l_flag & LW_IDLE) != 0)
    920  1.108      yamt 		return;
    921  1.108      yamt 
    922  1.108      yamt 	sched_schedclock(l);
    923  1.108      yamt }
    924  1.108      yamt 
    925   1.19       cgd /*
    926   1.19       cgd  * Statistics clock.  Grab profile sample, and if divider reaches 0,
    927   1.19       cgd  * do process and kernel statistics.
    928   1.19       cgd  */
    929   1.19       cgd void
    930   1.63   thorpej statclock(struct clockframe *frame)
    931   1.19       cgd {
    932   1.19       cgd #ifdef GPROF
    933   1.55  augustss 	struct gmonparam *g;
    934   1.68       eeh 	intptr_t i;
    935   1.19       cgd #endif
    936   1.60   thorpej 	struct cpu_info *ci = curcpu();
    937   1.60   thorpej 	struct schedstate_percpu *spc = &ci->ci_schedstate;
    938   1.55  augustss 	struct proc *p;
    939   1.98  christos 	struct lwp *l;
    940   1.19       cgd 
    941   1.70  sommerfe 	/*
    942   1.70  sommerfe 	 * Notice changes in divisor frequency, and adjust clock
    943   1.70  sommerfe 	 * frequency accordingly.
    944   1.70  sommerfe 	 */
    945   1.70  sommerfe 	if (spc->spc_psdiv != psdiv) {
    946   1.70  sommerfe 		spc->spc_psdiv = psdiv;
    947   1.70  sommerfe 		spc->spc_pscnt = psdiv;
    948   1.70  sommerfe 		if (psdiv == 1) {
    949   1.70  sommerfe 			setstatclockrate(stathz);
    950   1.70  sommerfe 		} else {
    951   1.93     perry 			setstatclockrate(profhz);
    952   1.70  sommerfe 		}
    953   1.70  sommerfe 	}
    954   1.98  christos 	l = curlwp;
    955  1.108      yamt 	if ((l->l_flag & LW_IDLE) != 0) {
    956  1.108      yamt 		/*
    957  1.108      yamt 		 * don't account idle lwps as swapper.
    958  1.108      yamt 		 */
    959  1.108      yamt 		p = NULL;
    960  1.108      yamt 	} else {
    961  1.108      yamt 		p = l->l_proc;
    962  1.105        ad 		mutex_spin_enter(&p->p_stmutex);
    963  1.108      yamt 	}
    964  1.108      yamt 
    965   1.19       cgd 	if (CLKF_USERMODE(frame)) {
    966  1.105        ad 		if ((p->p_stflag & PST_PROFIL) && profsrc == PROFSRC_CLOCK)
    967  1.105        ad 			addupc_intr(l, CLKF_PC(frame));
    968  1.105        ad 		if (--spc->spc_pscnt > 0) {
    969  1.105        ad 			mutex_spin_exit(&p->p_stmutex);
    970   1.19       cgd 			return;
    971  1.105        ad 		}
    972  1.105        ad 
    973   1.19       cgd 		/*
    974   1.19       cgd 		 * Came from user mode; CPU was in user state.
    975   1.19       cgd 		 * If this process is being profiled record the tick.
    976   1.19       cgd 		 */
    977   1.19       cgd 		p->p_uticks++;
    978   1.19       cgd 		if (p->p_nice > NZERO)
    979   1.60   thorpej 			spc->spc_cp_time[CP_NICE]++;
    980   1.19       cgd 		else
    981   1.60   thorpej 			spc->spc_cp_time[CP_USER]++;
    982   1.19       cgd 	} else {
    983   1.19       cgd #ifdef GPROF
    984   1.19       cgd 		/*
    985   1.19       cgd 		 * Kernel statistics are just like addupc_intr, only easier.
    986   1.19       cgd 		 */
    987   1.19       cgd 		g = &_gmonparam;
    988   1.80    briggs 		if (profsrc == PROFSRC_CLOCK && g->state == GMON_PROF_ON) {
    989   1.19       cgd 			i = CLKF_PC(frame) - g->lowpc;
    990   1.19       cgd 			if (i < g->textsize) {
    991   1.19       cgd 				i /= HISTFRACTION * sizeof(*g->kcount);
    992   1.19       cgd 				g->kcount[i]++;
    993   1.19       cgd 			}
    994   1.19       cgd 		}
    995   1.19       cgd #endif
    996   1.82   thorpej #ifdef LWP_PC
    997  1.108      yamt 		if (p != NULL && profsrc == PROFSRC_CLOCK &&
    998  1.108      yamt 		    (p->p_stflag & PST_PROFIL)) {
    999  1.105        ad 			addupc_intr(l, LWP_PC(l));
   1000  1.108      yamt 		}
   1001   1.72   mycroft #endif
   1002  1.105        ad 		if (--spc->spc_pscnt > 0) {
   1003  1.105        ad 			if (p != NULL)
   1004  1.105        ad 				mutex_spin_exit(&p->p_stmutex);
   1005   1.19       cgd 			return;
   1006  1.105        ad 		}
   1007   1.19       cgd 		/*
   1008   1.19       cgd 		 * Came from kernel mode, so we were:
   1009   1.19       cgd 		 * - handling an interrupt,
   1010   1.19       cgd 		 * - doing syscall or trap work on behalf of the current
   1011   1.19       cgd 		 *   user process, or
   1012   1.19       cgd 		 * - spinning in the idle loop.
   1013   1.19       cgd 		 * Whichever it is, charge the time as appropriate.
   1014   1.19       cgd 		 * Note that we charge interrupts to the current process,
   1015   1.19       cgd 		 * regardless of whether they are ``for'' that process,
   1016   1.19       cgd 		 * so that we know how much of its real time was spent
   1017   1.19       cgd 		 * in ``non-process'' (i.e., interrupt) work.
   1018   1.19       cgd 		 */
   1019  1.109        ad 		if (CLKF_INTR(frame) || (l->l_flag & LW_INTR) != 0) {
   1020  1.108      yamt 			if (p != NULL) {
   1021   1.19       cgd 				p->p_iticks++;
   1022  1.108      yamt 			}
   1023   1.60   thorpej 			spc->spc_cp_time[CP_INTR]++;
   1024   1.19       cgd 		} else if (p != NULL) {
   1025   1.19       cgd 			p->p_sticks++;
   1026   1.60   thorpej 			spc->spc_cp_time[CP_SYS]++;
   1027  1.108      yamt 		} else {
   1028   1.60   thorpej 			spc->spc_cp_time[CP_IDLE]++;
   1029  1.108      yamt 		}
   1030   1.19       cgd 	}
   1031   1.70  sommerfe 	spc->spc_pscnt = psdiv;
   1032   1.19       cgd 
   1033   1.97      elad 	if (p != NULL) {
   1034  1.108      yamt 		++l->l_cpticks;
   1035  1.105        ad 		mutex_spin_exit(&p->p_stmutex);
   1036  1.108      yamt 	}
   1037  1.105        ad 
   1038  1.108      yamt 	/*
   1039  1.108      yamt 	 * If no separate schedclock is provided, call it here
   1040  1.108      yamt 	 * at about 16 Hz.
   1041  1.108      yamt 	 */
   1042  1.108      yamt 	if (schedhz == 0) {
   1043  1.108      yamt 		if ((int)(--ci->ci_schedstate.spc_schedticks) <= 0) {
   1044  1.108      yamt 			schedclock(l);
   1045  1.108      yamt 			ci->ci_schedstate.spc_schedticks = statscheddiv;
   1046  1.108      yamt 		}
   1047   1.19       cgd 	}
   1048   1.19       cgd }
   1049   1.27  jonathan 
   1050   1.99    kardel #ifndef __HAVE_TIMECOUNTER
   1051   1.27  jonathan #ifdef NTP	/* NTP phase-locked loop in kernel */
   1052   1.27  jonathan /*
   1053   1.27  jonathan  * hardupdate() - local clock update
   1054   1.27  jonathan  *
   1055   1.27  jonathan  * This routine is called by ntp_adjtime() to update the local clock
   1056   1.27  jonathan  * phase and frequency. The implementation is of an adaptive-parameter,
   1057   1.27  jonathan  * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
   1058   1.27  jonathan  * time and frequency offset estimates for each call. If the kernel PPS
   1059   1.27  jonathan  * discipline code is configured (PPS_SYNC), the PPS signal itself
   1060   1.27  jonathan  * determines the new time offset, instead of the calling argument.
   1061   1.27  jonathan  * Presumably, calls to ntp_adjtime() occur only when the caller
   1062   1.27  jonathan  * believes the local clock is valid within some bound (+-128 ms with
   1063   1.27  jonathan  * NTP). If the caller's time is far different than the PPS time, an
   1064   1.27  jonathan  * argument will ensue, and it's not clear who will lose.
   1065   1.27  jonathan  *
   1066   1.27  jonathan  * For uncompensated quartz crystal oscillatores and nominal update
   1067   1.27  jonathan  * intervals less than 1024 s, operation should be in phase-lock mode
   1068   1.27  jonathan  * (STA_FLL = 0), where the loop is disciplined to phase. For update
   1069   1.27  jonathan  * intervals greater than thiss, operation should be in frequency-lock
   1070   1.27  jonathan  * mode (STA_FLL = 1), where the loop is disciplined to frequency.
   1071   1.27  jonathan  *
   1072   1.27  jonathan  * Note: splclock() is in effect.
   1073   1.27  jonathan  */
   1074   1.27  jonathan void
   1075   1.63   thorpej hardupdate(long offset)
   1076   1.27  jonathan {
   1077   1.27  jonathan 	long ltemp, mtemp;
   1078   1.27  jonathan 
   1079   1.27  jonathan 	if (!(time_status & STA_PLL) && !(time_status & STA_PPSTIME))
   1080   1.27  jonathan 		return;
   1081   1.27  jonathan 	ltemp = offset;
   1082   1.27  jonathan #ifdef PPS_SYNC
   1083   1.27  jonathan 	if (time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL)
   1084   1.27  jonathan 		ltemp = pps_offset;
   1085   1.27  jonathan #endif /* PPS_SYNC */
   1086   1.27  jonathan 
   1087   1.27  jonathan 	/*
   1088   1.27  jonathan 	 * Scale the phase adjustment and clamp to the operating range.
   1089   1.27  jonathan 	 */
   1090   1.27  jonathan 	if (ltemp > MAXPHASE)
   1091   1.27  jonathan 		time_offset = MAXPHASE << SHIFT_UPDATE;
   1092   1.27  jonathan 	else if (ltemp < -MAXPHASE)
   1093   1.27  jonathan 		time_offset = -(MAXPHASE << SHIFT_UPDATE);
   1094   1.27  jonathan 	else
   1095   1.27  jonathan 		time_offset = ltemp << SHIFT_UPDATE;
   1096   1.27  jonathan 
   1097   1.27  jonathan 	/*
   1098   1.27  jonathan 	 * Select whether the frequency is to be controlled and in which
   1099   1.27  jonathan 	 * mode (PLL or FLL). Clamp to the operating range. Ugly
   1100   1.27  jonathan 	 * multiply/divide should be replaced someday.
   1101   1.27  jonathan 	 */
   1102   1.27  jonathan 	if (time_status & STA_FREQHOLD || time_reftime == 0)
   1103   1.27  jonathan 		time_reftime = time.tv_sec;
   1104   1.27  jonathan 	mtemp = time.tv_sec - time_reftime;
   1105   1.27  jonathan 	time_reftime = time.tv_sec;
   1106   1.27  jonathan 	if (time_status & STA_FLL) {
   1107   1.27  jonathan 		if (mtemp >= MINSEC) {
   1108   1.27  jonathan 			ltemp = ((time_offset / mtemp) << (SHIFT_USEC -
   1109   1.27  jonathan 			    SHIFT_UPDATE));
   1110   1.27  jonathan 			if (ltemp < 0)
   1111   1.27  jonathan 				time_freq -= -ltemp >> SHIFT_KH;
   1112   1.27  jonathan 			else
   1113   1.27  jonathan 				time_freq += ltemp >> SHIFT_KH;
   1114   1.27  jonathan 		}
   1115   1.27  jonathan 	} else {
   1116   1.27  jonathan 		if (mtemp < MAXSEC) {
   1117   1.27  jonathan 			ltemp *= mtemp;
   1118   1.27  jonathan 			if (ltemp < 0)
   1119   1.27  jonathan 				time_freq -= -ltemp >> (time_constant +
   1120   1.27  jonathan 				    time_constant + SHIFT_KF -
   1121   1.27  jonathan 				    SHIFT_USEC);
   1122   1.27  jonathan 			else
   1123   1.27  jonathan 				time_freq += ltemp >> (time_constant +
   1124   1.27  jonathan 				    time_constant + SHIFT_KF -
   1125   1.27  jonathan 				    SHIFT_USEC);
   1126   1.27  jonathan 		}
   1127   1.27  jonathan 	}
   1128   1.27  jonathan 	if (time_freq > time_tolerance)
   1129   1.27  jonathan 		time_freq = time_tolerance;
   1130   1.27  jonathan 	else if (time_freq < -time_tolerance)
   1131   1.27  jonathan 		time_freq = -time_tolerance;
   1132   1.27  jonathan }
   1133   1.27  jonathan 
   1134   1.27  jonathan #ifdef PPS_SYNC
   1135   1.27  jonathan /*
   1136   1.27  jonathan  * hardpps() - discipline CPU clock oscillator to external PPS signal
   1137   1.27  jonathan  *
   1138   1.27  jonathan  * This routine is called at each PPS interrupt in order to discipline
   1139   1.27  jonathan  * the CPU clock oscillator to the PPS signal. It measures the PPS phase
   1140   1.27  jonathan  * and leaves it in a handy spot for the hardclock() routine. It
   1141   1.27  jonathan  * integrates successive PPS phase differences and calculates the
   1142   1.27  jonathan  * frequency offset. This is used in hardclock() to discipline the CPU
   1143   1.27  jonathan  * clock oscillator so that intrinsic frequency error is cancelled out.
   1144   1.27  jonathan  * The code requires the caller to capture the time and hardware counter
   1145   1.27  jonathan  * value at the on-time PPS signal transition.
   1146   1.27  jonathan  *
   1147   1.27  jonathan  * Note that, on some Unix systems, this routine runs at an interrupt
   1148   1.27  jonathan  * priority level higher than the timer interrupt routine hardclock().
   1149   1.27  jonathan  * Therefore, the variables used are distinct from the hardclock()
   1150   1.27  jonathan  * variables, except for certain exceptions: The PPS frequency pps_freq
   1151   1.27  jonathan  * and phase pps_offset variables are determined by this routine and
   1152   1.27  jonathan  * updated atomically. The time_tolerance variable can be considered a
   1153   1.27  jonathan  * constant, since it is infrequently changed, and then only when the
   1154   1.27  jonathan  * PPS signal is disabled. The watchdog counter pps_valid is updated
   1155   1.27  jonathan  * once per second by hardclock() and is atomically cleared in this
   1156   1.27  jonathan  * routine.
   1157   1.27  jonathan  */
   1158   1.27  jonathan void
   1159   1.63   thorpej hardpps(struct timeval *tvp,		/* time at PPS */
   1160   1.63   thorpej 	long usec			/* hardware counter at PPS */)
   1161   1.27  jonathan {
   1162   1.27  jonathan 	long u_usec, v_usec, bigtick;
   1163   1.27  jonathan 	long cal_sec, cal_usec;
   1164   1.27  jonathan 
   1165   1.27  jonathan 	/*
   1166   1.27  jonathan 	 * An occasional glitch can be produced when the PPS interrupt
   1167   1.27  jonathan 	 * occurs in the hardclock() routine before the time variable is
   1168   1.27  jonathan 	 * updated. Here the offset is discarded when the difference
   1169   1.27  jonathan 	 * between it and the last one is greater than tick/2, but not
   1170   1.27  jonathan 	 * if the interval since the first discard exceeds 30 s.
   1171   1.27  jonathan 	 */
   1172   1.27  jonathan 	time_status |= STA_PPSSIGNAL;
   1173   1.27  jonathan 	time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
   1174   1.27  jonathan 	pps_valid = 0;
   1175   1.27  jonathan 	u_usec = -tvp->tv_usec;
   1176   1.27  jonathan 	if (u_usec < -500000)
   1177   1.27  jonathan 		u_usec += 1000000;
   1178   1.27  jonathan 	v_usec = pps_offset - u_usec;
   1179   1.27  jonathan 	if (v_usec < 0)
   1180   1.27  jonathan 		v_usec = -v_usec;
   1181   1.27  jonathan 	if (v_usec > (tick >> 1)) {
   1182   1.27  jonathan 		if (pps_glitch > MAXGLITCH) {
   1183   1.27  jonathan 			pps_glitch = 0;
   1184   1.27  jonathan 			pps_tf[2] = u_usec;
   1185   1.27  jonathan 			pps_tf[1] = u_usec;
   1186   1.27  jonathan 		} else {
   1187   1.27  jonathan 			pps_glitch++;
   1188   1.27  jonathan 			u_usec = pps_offset;
   1189   1.27  jonathan 		}
   1190   1.27  jonathan 	} else
   1191   1.27  jonathan 		pps_glitch = 0;
   1192   1.27  jonathan 
   1193   1.27  jonathan 	/*
   1194   1.27  jonathan 	 * A three-stage median filter is used to help deglitch the pps
   1195   1.27  jonathan 	 * time. The median sample becomes the time offset estimate; the
   1196   1.27  jonathan 	 * difference between the other two samples becomes the time
   1197   1.27  jonathan 	 * dispersion (jitter) estimate.
   1198   1.27  jonathan 	 */
   1199   1.27  jonathan 	pps_tf[2] = pps_tf[1];
   1200   1.27  jonathan 	pps_tf[1] = pps_tf[0];
   1201   1.27  jonathan 	pps_tf[0] = u_usec;
   1202   1.27  jonathan 	if (pps_tf[0] > pps_tf[1]) {
   1203   1.27  jonathan 		if (pps_tf[1] > pps_tf[2]) {
   1204   1.27  jonathan 			pps_offset = pps_tf[1];		/* 0 1 2 */
   1205   1.27  jonathan 			v_usec = pps_tf[0] - pps_tf[2];
   1206   1.27  jonathan 		} else if (pps_tf[2] > pps_tf[0]) {
   1207   1.27  jonathan 			pps_offset = pps_tf[0];		/* 2 0 1 */
   1208   1.27  jonathan 			v_usec = pps_tf[2] - pps_tf[1];
   1209   1.27  jonathan 		} else {
   1210   1.27  jonathan 			pps_offset = pps_tf[2];		/* 0 2 1 */
   1211   1.27  jonathan 			v_usec = pps_tf[0] - pps_tf[1];
   1212   1.27  jonathan 		}
   1213   1.27  jonathan 	} else {
   1214   1.27  jonathan 		if (pps_tf[1] < pps_tf[2]) {
   1215   1.27  jonathan 			pps_offset = pps_tf[1];		/* 2 1 0 */
   1216   1.27  jonathan 			v_usec = pps_tf[2] - pps_tf[0];
   1217   1.27  jonathan 		} else  if (pps_tf[2] < pps_tf[0]) {
   1218   1.27  jonathan 			pps_offset = pps_tf[0];		/* 1 0 2 */
   1219   1.27  jonathan 			v_usec = pps_tf[1] - pps_tf[2];
   1220   1.27  jonathan 		} else {
   1221   1.27  jonathan 			pps_offset = pps_tf[2];		/* 1 2 0 */
   1222   1.27  jonathan 			v_usec = pps_tf[1] - pps_tf[0];
   1223   1.27  jonathan 		}
   1224   1.27  jonathan 	}
   1225   1.27  jonathan 	if (v_usec > MAXTIME)
   1226   1.27  jonathan 		pps_jitcnt++;
   1227   1.27  jonathan 	v_usec = (v_usec << PPS_AVG) - pps_jitter;
   1228   1.27  jonathan 	if (v_usec < 0)
   1229   1.27  jonathan 		pps_jitter -= -v_usec >> PPS_AVG;
   1230   1.27  jonathan 	else
   1231   1.27  jonathan 		pps_jitter += v_usec >> PPS_AVG;
   1232   1.27  jonathan 	if (pps_jitter > (MAXTIME >> 1))
   1233   1.27  jonathan 		time_status |= STA_PPSJITTER;
   1234   1.27  jonathan 
   1235   1.27  jonathan 	/*
   1236   1.27  jonathan 	 * During the calibration interval adjust the starting time when
   1237   1.27  jonathan 	 * the tick overflows. At the end of the interval compute the
   1238   1.27  jonathan 	 * duration of the interval and the difference of the hardware
   1239   1.27  jonathan 	 * counters at the beginning and end of the interval. This code
   1240   1.27  jonathan 	 * is deliciously complicated by the fact valid differences may
   1241   1.27  jonathan 	 * exceed the value of tick when using long calibration
   1242   1.27  jonathan 	 * intervals and small ticks. Note that the counter can be
   1243   1.27  jonathan 	 * greater than tick if caught at just the wrong instant, but
   1244   1.27  jonathan 	 * the values returned and used here are correct.
   1245   1.27  jonathan 	 */
   1246   1.27  jonathan 	bigtick = (long)tick << SHIFT_USEC;
   1247   1.27  jonathan 	pps_usec -= pps_freq;
   1248   1.27  jonathan 	if (pps_usec >= bigtick)
   1249   1.27  jonathan 		pps_usec -= bigtick;
   1250   1.27  jonathan 	if (pps_usec < 0)
   1251   1.27  jonathan 		pps_usec += bigtick;
   1252   1.27  jonathan 	pps_time.tv_sec++;
   1253   1.27  jonathan 	pps_count++;
   1254   1.27  jonathan 	if (pps_count < (1 << pps_shift))
   1255   1.27  jonathan 		return;
   1256   1.27  jonathan 	pps_count = 0;
   1257   1.27  jonathan 	pps_calcnt++;
   1258   1.27  jonathan 	u_usec = usec << SHIFT_USEC;
   1259   1.27  jonathan 	v_usec = pps_usec - u_usec;
   1260   1.27  jonathan 	if (v_usec >= bigtick >> 1)
   1261   1.27  jonathan 		v_usec -= bigtick;
   1262   1.27  jonathan 	if (v_usec < -(bigtick >> 1))
   1263   1.27  jonathan 		v_usec += bigtick;
   1264   1.27  jonathan 	if (v_usec < 0)
   1265   1.27  jonathan 		v_usec = -(-v_usec >> pps_shift);
   1266   1.27  jonathan 	else
   1267   1.27  jonathan 		v_usec = v_usec >> pps_shift;
   1268   1.27  jonathan 	pps_usec = u_usec;
   1269   1.27  jonathan 	cal_sec = tvp->tv_sec;
   1270   1.27  jonathan 	cal_usec = tvp->tv_usec;
   1271   1.27  jonathan 	cal_sec -= pps_time.tv_sec;
   1272   1.27  jonathan 	cal_usec -= pps_time.tv_usec;
   1273   1.27  jonathan 	if (cal_usec < 0) {
   1274   1.27  jonathan 		cal_usec += 1000000;
   1275   1.27  jonathan 		cal_sec--;
   1276   1.27  jonathan 	}
   1277   1.27  jonathan 	pps_time = *tvp;
   1278   1.27  jonathan 
   1279   1.27  jonathan 	/*
   1280   1.27  jonathan 	 * Check for lost interrupts, noise, excessive jitter and
   1281   1.27  jonathan 	 * excessive frequency error. The number of timer ticks during
   1282   1.27  jonathan 	 * the interval may vary +-1 tick. Add to this a margin of one
   1283   1.27  jonathan 	 * tick for the PPS signal jitter and maximum frequency
   1284   1.27  jonathan 	 * deviation. If the limits are exceeded, the calibration
   1285   1.27  jonathan 	 * interval is reset to the minimum and we start over.
   1286   1.27  jonathan 	 */
   1287   1.27  jonathan 	u_usec = (long)tick << 1;
   1288   1.27  jonathan 	if (!((cal_sec == -1 && cal_usec > (1000000 - u_usec))
   1289   1.27  jonathan 	    || (cal_sec == 0 && cal_usec < u_usec))
   1290   1.27  jonathan 	    || v_usec > time_tolerance || v_usec < -time_tolerance) {
   1291   1.27  jonathan 		pps_errcnt++;
   1292   1.27  jonathan 		pps_shift = PPS_SHIFT;
   1293   1.27  jonathan 		pps_intcnt = 0;
   1294   1.27  jonathan 		time_status |= STA_PPSERROR;
   1295   1.27  jonathan 		return;
   1296   1.27  jonathan 	}
   1297   1.27  jonathan 
   1298   1.27  jonathan 	/*
   1299   1.27  jonathan 	 * A three-stage median filter is used to help deglitch the pps
   1300   1.27  jonathan 	 * frequency. The median sample becomes the frequency offset
   1301   1.27  jonathan 	 * estimate; the difference between the other two samples
   1302   1.27  jonathan 	 * becomes the frequency dispersion (stability) estimate.
   1303   1.27  jonathan 	 */
   1304   1.27  jonathan 	pps_ff[2] = pps_ff[1];
   1305   1.27  jonathan 	pps_ff[1] = pps_ff[0];
   1306   1.27  jonathan 	pps_ff[0] = v_usec;
   1307   1.27  jonathan 	if (pps_ff[0] > pps_ff[1]) {
   1308   1.27  jonathan 		if (pps_ff[1] > pps_ff[2]) {
   1309   1.27  jonathan 			u_usec = pps_ff[1];		/* 0 1 2 */
   1310   1.27  jonathan 			v_usec = pps_ff[0] - pps_ff[2];
   1311   1.27  jonathan 		} else if (pps_ff[2] > pps_ff[0]) {
   1312   1.27  jonathan 			u_usec = pps_ff[0];		/* 2 0 1 */
   1313   1.27  jonathan 			v_usec = pps_ff[2] - pps_ff[1];
   1314   1.27  jonathan 		} else {
   1315   1.27  jonathan 			u_usec = pps_ff[2];		/* 0 2 1 */
   1316   1.27  jonathan 			v_usec = pps_ff[0] - pps_ff[1];
   1317   1.27  jonathan 		}
   1318   1.27  jonathan 	} else {
   1319   1.27  jonathan 		if (pps_ff[1] < pps_ff[2]) {
   1320   1.27  jonathan 			u_usec = pps_ff[1];		/* 2 1 0 */
   1321   1.27  jonathan 			v_usec = pps_ff[2] - pps_ff[0];
   1322   1.27  jonathan 		} else  if (pps_ff[2] < pps_ff[0]) {
   1323   1.27  jonathan 			u_usec = pps_ff[0];		/* 1 0 2 */
   1324   1.27  jonathan 			v_usec = pps_ff[1] - pps_ff[2];
   1325   1.27  jonathan 		} else {
   1326   1.27  jonathan 			u_usec = pps_ff[2];		/* 1 2 0 */
   1327   1.27  jonathan 			v_usec = pps_ff[1] - pps_ff[0];
   1328   1.27  jonathan 		}
   1329   1.27  jonathan 	}
   1330   1.27  jonathan 
   1331   1.27  jonathan 	/*
   1332   1.27  jonathan 	 * Here the frequency dispersion (stability) is updated. If it
   1333   1.27  jonathan 	 * is less than one-fourth the maximum (MAXFREQ), the frequency
   1334   1.27  jonathan 	 * offset is updated as well, but clamped to the tolerance. It
   1335   1.27  jonathan 	 * will be processed later by the hardclock() routine.
   1336   1.27  jonathan 	 */
   1337   1.27  jonathan 	v_usec = (v_usec >> 1) - pps_stabil;
   1338   1.27  jonathan 	if (v_usec < 0)
   1339   1.27  jonathan 		pps_stabil -= -v_usec >> PPS_AVG;
   1340   1.27  jonathan 	else
   1341   1.27  jonathan 		pps_stabil += v_usec >> PPS_AVG;
   1342   1.27  jonathan 	if (pps_stabil > MAXFREQ >> 2) {
   1343   1.27  jonathan 		pps_stbcnt++;
   1344   1.27  jonathan 		time_status |= STA_PPSWANDER;
   1345   1.27  jonathan 		return;
   1346   1.27  jonathan 	}
   1347   1.27  jonathan 	if (time_status & STA_PPSFREQ) {
   1348   1.27  jonathan 		if (u_usec < 0) {
   1349   1.27  jonathan 			pps_freq -= -u_usec >> PPS_AVG;
   1350   1.27  jonathan 			if (pps_freq < -time_tolerance)
   1351   1.27  jonathan 				pps_freq = -time_tolerance;
   1352   1.27  jonathan 			u_usec = -u_usec;
   1353   1.27  jonathan 		} else {
   1354   1.27  jonathan 			pps_freq += u_usec >> PPS_AVG;
   1355   1.27  jonathan 			if (pps_freq > time_tolerance)
   1356   1.27  jonathan 				pps_freq = time_tolerance;
   1357   1.27  jonathan 		}
   1358   1.27  jonathan 	}
   1359   1.27  jonathan 
   1360   1.27  jonathan 	/*
   1361   1.27  jonathan 	 * Here the calibration interval is adjusted. If the maximum
   1362   1.27  jonathan 	 * time difference is greater than tick / 4, reduce the interval
   1363   1.27  jonathan 	 * by half. If this is not the case for four consecutive
   1364   1.27  jonathan 	 * intervals, double the interval.
   1365   1.27  jonathan 	 */
   1366   1.27  jonathan 	if (u_usec << pps_shift > bigtick >> 2) {
   1367   1.27  jonathan 		pps_intcnt = 0;
   1368   1.27  jonathan 		if (pps_shift > PPS_SHIFT)
   1369   1.27  jonathan 			pps_shift--;
   1370   1.27  jonathan 	} else if (pps_intcnt >= 4) {
   1371   1.27  jonathan 		pps_intcnt = 0;
   1372   1.27  jonathan 		if (pps_shift < PPS_SHIFTMAX)
   1373   1.27  jonathan 			pps_shift++;
   1374   1.27  jonathan 	} else
   1375   1.27  jonathan 		pps_intcnt++;
   1376   1.27  jonathan }
   1377   1.27  jonathan #endif /* PPS_SYNC */
   1378   1.27  jonathan #endif /* NTP  */
   1379   1.95  christos 
   1380   1.99    kardel /* timecounter compat functions */
   1381   1.99    kardel void
   1382   1.95  christos nanotime(struct timespec *ts)
   1383   1.95  christos {
   1384   1.95  christos 	struct timeval tv;
   1385   1.95  christos 
   1386   1.95  christos 	microtime(&tv);
   1387   1.95  christos 	TIMEVAL_TO_TIMESPEC(&tv, ts);
   1388   1.95  christos }
   1389   1.99    kardel 
   1390   1.99    kardel void
   1391   1.99    kardel getbinuptime(struct bintime *bt)
   1392   1.99    kardel {
   1393   1.99    kardel 	struct timeval tv;
   1394   1.99    kardel 
   1395   1.99    kardel 	microtime(&tv);
   1396   1.99    kardel 	timeval2bintime(&tv, bt);
   1397   1.99    kardel }
   1398   1.99    kardel 
   1399   1.99    kardel void
   1400   1.99    kardel nanouptime(struct timespec *tsp)
   1401   1.99    kardel {
   1402   1.99    kardel 	int s;
   1403   1.99    kardel 
   1404   1.99    kardel 	s = splclock();
   1405   1.99    kardel 	TIMEVAL_TO_TIMESPEC(&mono_time, tsp);
   1406   1.99    kardel 	splx(s);
   1407   1.99    kardel }
   1408   1.99    kardel 
   1409   1.99    kardel void
   1410   1.99    kardel getnanouptime(struct timespec *tsp)
   1411   1.99    kardel {
   1412   1.99    kardel 	int s;
   1413   1.99    kardel 
   1414   1.99    kardel 	s = splclock();
   1415   1.99    kardel 	TIMEVAL_TO_TIMESPEC(&mono_time, tsp);
   1416   1.99    kardel 	splx(s);
   1417   1.99    kardel }
   1418   1.99    kardel 
   1419   1.99    kardel void
   1420   1.99    kardel getmicrouptime(struct timeval *tvp)
   1421   1.99    kardel {
   1422   1.99    kardel 	int s;
   1423   1.99    kardel 
   1424   1.99    kardel 	s = splclock();
   1425   1.99    kardel 	*tvp = mono_time;
   1426   1.99    kardel 	splx(s);
   1427   1.99    kardel }
   1428   1.99    kardel 
   1429   1.99    kardel void
   1430   1.99    kardel getnanotime(struct timespec *tsp)
   1431   1.99    kardel {
   1432   1.99    kardel 	int s;
   1433   1.99    kardel 
   1434   1.99    kardel 	s = splclock();
   1435   1.99    kardel 	TIMEVAL_TO_TIMESPEC(&time, tsp);
   1436   1.99    kardel 	splx(s);
   1437   1.99    kardel }
   1438   1.99    kardel 
   1439   1.99    kardel void
   1440   1.99    kardel getmicrotime(struct timeval *tvp)
   1441   1.99    kardel {
   1442   1.99    kardel 	int s;
   1443   1.99    kardel 
   1444   1.99    kardel 	s = splclock();
   1445   1.99    kardel 	*tvp = time;
   1446   1.99    kardel 	splx(s);
   1447   1.99    kardel }
   1448  1.107       dsl 
   1449  1.107       dsl u_int64_t
   1450  1.107       dsl tc_getfrequency(void)
   1451  1.107       dsl {
   1452  1.107       dsl 	return hz;
   1453  1.107       dsl }
   1454   1.99    kardel #endif /* !__HAVE_TIMECOUNTER */
   1455