kern_ntptime.c revision 45295
144574Sphk/***********************************************************************
244574Sphk *								       *
345294Sphk * Copyright (c) David L. Mills 1993-1999			       *
444574Sphk *								       *
544574Sphk * Permission to use, copy, modify, and distribute this software and   *
644574Sphk * its documentation for any purpose and without fee is hereby	       *
744574Sphk * granted, provided that the above copyright notice appears in all    *
844574Sphk * copies and that both the copyright notice and this permission       *
944574Sphk * notice appear in supporting documentation, and that the name	       *
1044574Sphk * University of Delaware not be used in advertising or publicity      *
1144574Sphk * pertaining to distribution of the software without specific,	       *
1244574Sphk * written prior permission. The University of Delaware makes no       *
1344574Sphk * representations about the suitability this software for any	       *
1444574Sphk * purpose. It is provided "as is" without express or implied	       *
1544574Sphk * warranty.							       *
1644574Sphk *								       *
1744574Sphk **********************************************************************/
182858Swollman
192858Swollman/*
2044574Sphk * Adapted from the original sources for FreeBSD and timecounters by:
2144666Sphk * Poul-Henning Kamp <phk@FreeBSD.org>.
222858Swollman *
2344574Sphk * The 32bit version of the "LP" macros seems a bit past its "sell by"
2444574Sphk * date so I have retained only the 64bit version and included it directly
2544574Sphk * in this file.
2621101Sjhay *
2744574Sphk * Only minor changes done to interface with the timecounters over in
2844574Sphk * sys/kern/kern_clock.c.   Some of the comments below may be (even more)
2944574Sphk * confusing and/or plain wrong in that context.
302858Swollman */
3132925Seivind
3244666Sphk#include "opt_ntp.h"
3344666Sphk
342858Swollman#include <sys/param.h>
352858Swollman#include <sys/systm.h>
3612221Sbde#include <sys/sysproto.h>
372858Swollman#include <sys/kernel.h>
382858Swollman#include <sys/proc.h>
3944574Sphk#include <sys/time.h>
402858Swollman#include <sys/timex.h>
4136941Sphk#include <sys/timepps.h>
422858Swollman#include <sys/sysctl.h>
432858Swollman
442858Swollman/*
4544574Sphk * Single-precision macros for 64-bit machines
4644574Sphk */
4744574Sphktypedef long long l_fp;
4844574Sphk#define L_ADD(v, u)	((v) += (u))
4944574Sphk#define L_SUB(v, u)	((v) -= (u))
5044574Sphk#define L_ADDHI(v, a)	((v) += (long long)(a) << 32)
5144574Sphk#define L_NEG(v)	((v) = -(v))
5244574Sphk#define L_RSHIFT(v, n) \
5344574Sphk	do { \
5444574Sphk		if ((v) < 0) \
5544574Sphk			(v) = -(-(v) >> (n)); \
5644574Sphk		else \
5744574Sphk			(v) = (v) >> (n); \
5844574Sphk	} while (0)
5944574Sphk#define L_MPY(v, a)	((v) *= (a))
6044574Sphk#define L_CLR(v)	((v) = 0)
6144574Sphk#define L_ISNEG(v)	((v) < 0)
6244574Sphk#define L_LINT(v, a)	((v) = (long long)(a) << 32)
6344574Sphk#define L_GINT(v)	((v) < 0 ? -(-(v) >> 32) : (v) >> 32)
6444574Sphk
6544574Sphk/*
6644574Sphk * Generic NTP kernel interface
6732513Sphk *
6844574Sphk * These routines constitute the Network Time Protocol (NTP) interfaces
6944574Sphk * for user and daemon application programs. The ntp_gettime() routine
7044574Sphk * provides the time, maximum error (synch distance) and estimated error
7144574Sphk * (dispersion) to client user application programs. The ntp_adjtime()
7244574Sphk * routine is used by the NTP daemon to adjust the system clock to an
7344574Sphk * externally derived time. The time offset and related variables set by
7444574Sphk * this routine are used by other routines in this module to adjust the
7544574Sphk * phase and frequency of the clock discipline loop which controls the
7644574Sphk * system clock.
7732513Sphk *
7845294Sphk * When the kernel time is reckoned directly in nanoseconds (NTP_NANO
7944574Sphk * defined), the time at each tick interrupt is derived directly from
8044574Sphk * the kernel time variable. When the kernel time is reckoned in
8145294Sphk * microseconds, (NTP_NANO undefined), the time is derived from the
8245294Sphk * kernel time variable together with a variable representing the
8345294Sphk * leftover nanoseconds at the last tick interrupt. In either case, the
8445294Sphk * current nanosecond time is reckoned from these values plus an
8545294Sphk * interpolated value derived by the clock routines in another
8645294Sphk * architecture-specific module. The interpolation can use either a
8745294Sphk * dedicated counter or a processor cycle counter (PCC) implemented in
8845294Sphk * some architectures.
8932513Sphk *
9044574Sphk * Note that all routines must run at priority splclock or higher.
9144574Sphk */
9244574Sphk
9344574Sphk/*
9444574Sphk * Phase/frequency-lock loop (PLL/FLL) definitions
9532513Sphk *
9644574Sphk * The nanosecond clock discipline uses two variable types, time
9744574Sphk * variables and frequency variables. Both types are represented as 64-
9844574Sphk * bit fixed-point quantities with the decimal point between two 32-bit
9944574Sphk * halves. On a 32-bit machine, each half is represented as a single
10044574Sphk * word and mathematical operations are done using multiple-precision
10144574Sphk * arithmetic. On a 64-bit machine, ordinary computer arithmetic is
10244574Sphk * used.
10332513Sphk *
10444574Sphk * A time variable is a signed 64-bit fixed-point number in ns and
10544574Sphk * fraction. It represents the remaining time offset to be amortized
10644574Sphk * over succeeding tick interrupts. The maximum time offset is about
10745294Sphk * 0.5 s and the resolution is about 2.3e-10 ns.
10832513Sphk *
10944574Sphk *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
11044574Sphk *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
11144574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
11244574Sphk * |s s s|			 ns				   |
11344574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
11444574Sphk * |			    fraction				   |
11544574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
11632513Sphk *
11744574Sphk * A frequency variable is a signed 64-bit fixed-point number in ns/s
11844574Sphk * and fraction. It represents the ns and fraction to be added to the
11944574Sphk * kernel time variable at each second. The maximum frequency offset is
12045294Sphk * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s.
12132513Sphk *
12244574Sphk *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
12344574Sphk *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
12444574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12544574Sphk * |s s s s s s s s s s s s s|	          ns/s			   |
12644574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12744574Sphk * |			    fraction				   |
12844574Sphk * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1292858Swollman */
13032513Sphk/*
13132513Sphk * The following variables establish the state of the PLL/FLL and the
13244574Sphk * residual time and frequency offset of the local clock.
13332513Sphk */
13444574Sphk#define SHIFT_PLL	4		/* PLL loop gain (shift) */
13544574Sphk#define SHIFT_FLL	2		/* FLL loop gain (shift) */
13632513Sphk
13744574Sphkstatic int time_state = TIME_OK;	/* clock state */
13844574Sphkstatic int time_status = STA_UNSYNC;	/* clock status bits */
13944574Sphkstatic long time_constant;		/* poll interval (shift) (s) */
14044574Sphkstatic long time_precision = 1;		/* clock precision (ns) */
14144574Sphkstatic long time_maxerror = MAXPHASE / 1000; /* maximum error (us) */
14244574Sphkstatic long time_esterror = MAXPHASE / 1000; /* estimated error (us) */
14344574Sphkstatic long time_reftime;		/* time at last adjustment (s) */
14444574Sphkstatic long time_tick;			/* nanoseconds per tick (ns) */
14544574Sphkstatic l_fp time_offset;		/* time offset (ns) */
14644574Sphkstatic l_fp time_freq;			/* frequency offset (ns/s) */
14744574Sphk
14845294Sphkint ntp_mult;
14945294Sphkint ntp_div;
1502858Swollman#ifdef PPS_SYNC
1512858Swollman/*
15244574Sphk * The following variables are used when a pulse-per-second (PPS) signal
15344574Sphk * is available and connected via a modem control lead. They establish
15444574Sphk * the engineering parameters of the clock discipline loop when
15544574Sphk * controlled by the PPS signal.
1562858Swollman */
15744574Sphk#define PPS_FAVG	2		/* min freq avg interval (s) (shift) */
15845294Sphk#define PPS_FAVGMAX	8		/* max freq avg interval (s) (shift) */
15944574Sphk#define PPS_PAVG	4		/* phase avg interval (s) (shift) */
16044574Sphk#define PPS_VALID	120		/* PPS signal watchdog max (s) */
16144574Sphk#define MAXTIME		500000		/* max PPS error (jitter) (ns) */
16244574Sphk#define MAXWANDER	500000		/* max PPS wander (ns/s/s) */
16332513Sphk
16444574Sphkstruct ppstime {
16544574Sphk	long sec;			/* PPS seconds */
16644574Sphk	long nsec;			/* PPS nanoseconds */
16744574Sphk};
16844574Sphkstatic struct ppstime pps_tf[3];	/* phase median filter */
16944574Sphkstatic struct ppstime pps_filt;		/* phase offset */
17044574Sphkstatic l_fp pps_freq;			/* scaled frequency offset (ns/s) */
17144574Sphkstatic long pps_offacc;			/* offset accumulator */
17245294Sphkstatic long pps_fcount;			/* frequency accumulator */
17344574Sphkstatic long pps_jitter;			/* scaled time dispersion (ns) */
17444574Sphkstatic long pps_stabil;			/* scaled frequency dispersion (ns/s) */
17544574Sphkstatic long pps_lastsec;		/* time at last calibration (s) */
17644574Sphkstatic int pps_valid;			/* signal watchdog counter */
17744574Sphkstatic int pps_shift = PPS_FAVG;	/* interval duration (s) (shift) */
17844574Sphkstatic int pps_intcnt;			/* wander counter */
17944574Sphkstatic int pps_offcnt;			/* offset accumulator counter */
18044574Sphk
18132513Sphk/*
18232513Sphk * PPS signal quality monitors
18332513Sphk */
18444574Sphkstatic long pps_calcnt;			/* calibration intervals */
18544574Sphkstatic long pps_jitcnt;			/* jitter limit exceeded */
18644574Sphkstatic long pps_stbcnt;			/* stability limit exceeded */
18744574Sphkstatic long pps_errcnt;			/* calibration errors */
1882858Swollman#endif /* PPS_SYNC */
18932513Sphk/*
19044574Sphk * End of phase/frequency-lock loop (PLL/FLL) definitions
19132513Sphk */
19232513Sphk
19344574Sphkstatic void ntp_init(void);
19444574Sphkstatic void hardupdate(long offset);
19532513Sphk
19633690Sphk/*
19744574Sphk * ntp_gettime() - NTP user application interface
19833690Sphk *
19944574Sphk * See the timex.h header file for synopsis and API description.
20033690Sphk */
20112279Sphkstatic int
20212279Sphkntp_sysctl SYSCTL_HANDLER_ARGS
2032858Swollman{
20444574Sphk	struct ntptimeval ntv;	/* temporary structure */
20544574Sphk	struct timespec atv;	/* nanosecond time */
2062858Swollman
20744574Sphk	nanotime(&atv);
20844574Sphk	ntv.time.tv_sec = atv.tv_sec;
20944574Sphk	ntv.time.tv_nsec = atv.tv_nsec;
2102858Swollman	ntv.maxerror = time_maxerror;
2112858Swollman	ntv.esterror = time_esterror;
2122858Swollman	ntv.time_state = time_state;
2132858Swollman
2142858Swollman	/*
21544574Sphk	 * Status word error decode. If any of these conditions occur,
21644574Sphk	 * an error is returned, instead of the status word. Most
21744574Sphk	 * applications will care only about the fact the system clock
21844574Sphk	 * may not be trusted, not about the details.
2192858Swollman	 *
2202858Swollman	 * Hardware or software error
2212858Swollman	 */
22244574Sphk	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
2232858Swollman
2242858Swollman	/*
22544574Sphk	 * PPS signal lost when either time or frequency synchronization
22644574Sphk	 * requested
2272858Swollman	 */
22844574Sphk	    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
22944574Sphk	    !(time_status & STA_PPSSIGNAL)) ||
2302858Swollman
2312858Swollman	/*
23244574Sphk	 * PPS jitter exceeded when time synchronization requested
2332858Swollman	 */
23444574Sphk	    (time_status & STA_PPSTIME &&
23544574Sphk	    time_status & STA_PPSJITTER) ||
2362858Swollman
2372858Swollman	/*
23844574Sphk	 * PPS wander exceeded or calibration error when frequency
23944574Sphk	 * synchronization requested
2402858Swollman	 */
24144574Sphk	    (time_status & STA_PPSFREQ &&
24244574Sphk	    time_status & (STA_PPSWANDER | STA_PPSERROR)))
2432858Swollman		ntv.time_state = TIME_ERROR;
24412279Sphk	return (sysctl_handle_opaque(oidp, &ntv, sizeof ntv, req));
2452858Swollman}
2462858Swollman
24744574SphkSYSCTL_NODE(_kern, OID_AUTO, ntp_pll, CTLFLAG_RW, 0, "");
24844574SphkSYSCTL_PROC(_kern_ntp_pll, OID_AUTO, gettime, CTLTYPE_OPAQUE|CTLFLAG_RD,
24912623Sphk	0, sizeof(struct ntptimeval) , ntp_sysctl, "S,ntptimeval", "");
25012279Sphk
25145294SphkSYSCTL_INT(_kern_ntp_pll, OID_AUTO, mult, CTLFLAG_RW, &ntp_mult, 0, "");
25245294SphkSYSCTL_INT(_kern_ntp_pll, OID_AUTO, div, CTLFLAG_RW, &ntp_div, 0, "");
25344574Sphk
2542858Swollman/*
2552858Swollman * ntp_adjtime() - NTP daemon application interface
25644574Sphk *
25744574Sphk * See the timex.h header file for synopsis and API description.
2582858Swollman */
25912221Sbde#ifndef _SYS_SYSPROTO_H_
2602858Swollmanstruct ntp_adjtime_args {
26144574Sphk	struct timex *tp;
2622858Swollman};
26312221Sbde#endif
2642858Swollman
2652858Swollmanint
26630994Sphkntp_adjtime(struct proc *p, struct ntp_adjtime_args *uap)
2672858Swollman{
26844574Sphk	struct timex ntv;	/* temporary structure */
26945294Sphk	long freq;		/* frequency ns/s) */
27044574Sphk	int modes;		/* mode bits from structure */
27144574Sphk	int s;			/* caller priority */
2722858Swollman	int error;
2732858Swollman
2742858Swollman	error = copyin((caddr_t)uap->tp, (caddr_t)&ntv, sizeof(ntv));
2752858Swollman	if (error)
27644574Sphk		return(error);
2772858Swollman
2782858Swollman	/*
2792858Swollman	 * Update selected clock variables - only the superuser can
2802858Swollman	 * change anything. Note that there is no error checking here on
2812858Swollman	 * the assumption the superuser should know what it is doing.
2822858Swollman	 */
2832858Swollman	modes = ntv.modes;
28444776Sphk	if (modes)
28544776Sphk		error = suser(p->p_cred->pc_ucred, &p->p_acflag);
28644574Sphk	if (error)
28744574Sphk		return (error);
2882858Swollman	s = splclock();
28944574Sphk	if (modes & MOD_FREQUENCY) {
29045295Sphk		freq = (ntv.freq * 1000LL) << 16;
29145294Sphk		if (freq > MAXFREQ)
29245294Sphk			L_LINT(time_freq, MAXFREQ);
29345294Sphk		else if (freq < -MAXFREQ)
29445294Sphk			L_LINT(time_freq, -MAXFREQ);
29545294Sphk		else
29645294Sphk			L_LINT(time_freq, freq);
29745294Sphk
2982858Swollman#ifdef PPS_SYNC
29944574Sphk		pps_freq = time_freq;
3002858Swollman#endif /* PPS_SYNC */
30144574Sphk	}
3022858Swollman	if (modes & MOD_MAXERROR)
3032858Swollman		time_maxerror = ntv.maxerror;
3042858Swollman	if (modes & MOD_ESTERROR)
3052858Swollman		time_esterror = ntv.esterror;
3062858Swollman	if (modes & MOD_STATUS) {
3072858Swollman		time_status &= STA_RONLY;
3082858Swollman		time_status |= ntv.status & ~STA_RONLY;
3092858Swollman	}
31045294Sphk	if (modes & MOD_TIMECONST) {
31145294Sphk		if (ntv.constant < 0)
31245294Sphk			time_constant = 0;
31345294Sphk		else if (ntv.constant > MAXTC)
31445294Sphk			time_constant = MAXTC;
31545294Sphk		else
31645294Sphk			time_constant = ntv.constant;
31745294Sphk	}
31844574Sphk	if (modes & MOD_NANO)
31944574Sphk		time_status |= STA_NANO;
32044574Sphk	if (modes & MOD_MICRO)
32144574Sphk		time_status &= ~STA_NANO;
32244574Sphk	if (modes & MOD_CLKB)
32344574Sphk		time_status |= STA_CLK;
32444574Sphk	if (modes & MOD_CLKA)
32544574Sphk		time_status &= ~STA_CLK;
32644574Sphk	if (modes & MOD_OFFSET) {
32744574Sphk		if (time_status & STA_NANO)
32844574Sphk			hardupdate(ntv.offset);
32944574Sphk		else
33044574Sphk			hardupdate(ntv.offset * 1000);
33144574Sphk	}
3322858Swollman
3332858Swollman	/*
3342858Swollman	 * Retrieve all clock variables
3352858Swollman	 */
33644574Sphk	if (time_status & STA_NANO)
33744574Sphk		ntv.offset = L_GINT(time_offset);
3382858Swollman	else
33944574Sphk		ntv.offset = L_GINT(time_offset) / 1000;
34045295Sphk	ntv.freq = L_GINT((time_freq / 1000LL) << 16);
3412858Swollman	ntv.maxerror = time_maxerror;
3422858Swollman	ntv.esterror = time_esterror;
3432858Swollman	ntv.status = time_status;
34445294Sphk	ntv.constant = time_constant;
34544574Sphk	if (time_status & STA_NANO)
34644574Sphk		ntv.precision = time_precision;
34744574Sphk	else
34844574Sphk		ntv.precision = time_precision / 1000;
34944574Sphk	ntv.tolerance = MAXFREQ * SCALE_PPM;
3502858Swollman#ifdef PPS_SYNC
3512858Swollman	ntv.shift = pps_shift;
35245295Sphk	ntv.ppsfreq = L_GINT((pps_freq / 1000LL) << 16);
35344574Sphk	ntv.jitter = pps_jitter;
35444574Sphk	if (time_status & STA_NANO)
35544574Sphk		ntv.jitter = pps_jitter;
35644574Sphk	else
35744574Sphk		ntv.jitter = pps_jitter / 1000;
3582858Swollman	ntv.stabil = pps_stabil;
3592858Swollman	ntv.calcnt = pps_calcnt;
3602858Swollman	ntv.errcnt = pps_errcnt;
3612858Swollman	ntv.jitcnt = pps_jitcnt;
3622858Swollman	ntv.stbcnt = pps_stbcnt;
3632858Swollman#endif /* PPS_SYNC */
36444574Sphk	splx(s);
3652858Swollman
3662858Swollman	error = copyout((caddr_t)&ntv, (caddr_t)uap->tp, sizeof(ntv));
36744574Sphk	if (error)
36844574Sphk		return (error);
36944574Sphk
37044574Sphk	/*
37144574Sphk	 * Status word error decode. See comments in
37244574Sphk	 * ntp_gettime() routine.
37344574Sphk	 */
37444574Sphk	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
37544574Sphk	    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
37644574Sphk	    !(time_status & STA_PPSSIGNAL)) ||
37744574Sphk	    (time_status & STA_PPSTIME &&
37844574Sphk	    time_status & STA_PPSJITTER) ||
37944574Sphk	    (time_status & STA_PPSFREQ &&
38044574Sphk	    time_status & (STA_PPSWANDER | STA_PPSERROR)))
38144574Sphk		return (TIME_ERROR);
38244574Sphk	return (time_state);
38344574Sphk}
38444574Sphk
38544574Sphk/*
38644574Sphk * second_overflow() - called after ntp_tick_adjust()
38744574Sphk *
38844574Sphk * This routine is ordinarily called immediately following the above
38944574Sphk * routine ntp_tick_adjust(). While these two routines are normally
39044574Sphk * combined, they are separated here only for the purposes of
39144574Sphk * simulation.
39244574Sphk */
39344574Sphkvoid
39444574Sphkntp_update_second(struct timecounter *tcp)
39544574Sphk{
39644574Sphk	u_int32_t *newsec;
39744666Sphk	l_fp ftemp, time_adj;		/* 32/64-bit temporaries */
39844574Sphk
39944574Sphk	newsec = &tcp->tc_offset_sec;
40044574Sphk	time_maxerror += MAXFREQ / 1000;
40144574Sphk
40244574Sphk	/*
40344574Sphk	 * Leap second processing. If in leap-insert state at
40444574Sphk	 * the end of the day, the system clock is set back one
40544574Sphk	 * second; if in leap-delete state, the system clock is
40644574Sphk	 * set ahead one second. The nano_time() routine or
40744574Sphk	 * external clock driver will insure that reported time
40844574Sphk	 * is always monotonic.
40944574Sphk	 */
41044574Sphk	switch (time_state) {
41144574Sphk
4122858Swollman		/*
41344574Sphk		 * No warning.
4142858Swollman		 */
41544574Sphk		case TIME_OK:
41644574Sphk		if (time_status & STA_INS)
41744574Sphk			time_state = TIME_INS;
41844574Sphk		else if (time_status & STA_DEL)
41944574Sphk			time_state = TIME_DEL;
42044574Sphk		break;
42144574Sphk
42244574Sphk		/*
42344574Sphk		 * Insert second 23:59:60 following second
42444574Sphk		 * 23:59:59.
42544574Sphk		 */
42644574Sphk		case TIME_INS:
42744574Sphk		if (!(time_status & STA_INS))
42844574Sphk			time_state = TIME_OK;
42944574Sphk		else if ((*newsec) % 86400 == 0) {
43044574Sphk			(*newsec)--;
43144574Sphk			time_state = TIME_OOP;
43244574Sphk		}
43344574Sphk		break;
43444574Sphk
43544574Sphk		/*
43644574Sphk		 * Delete second 23:59:59.
43744574Sphk		 */
43844574Sphk		case TIME_DEL:
43944574Sphk		if (!(time_status & STA_DEL))
44044574Sphk			time_state = TIME_OK;
44144574Sphk		else if (((*newsec) + 1) % 86400 == 0) {
44244574Sphk			(*newsec)++;
44344574Sphk			time_state = TIME_WAIT;
44444574Sphk		}
44544574Sphk		break;
44644574Sphk
44744574Sphk		/*
44844574Sphk		 * Insert second in progress.
44944574Sphk		 */
45044574Sphk		case TIME_OOP:
45144574Sphk		time_state = TIME_WAIT;
45244574Sphk		break;
45344574Sphk
45444574Sphk		/*
45544574Sphk		 * Wait for status bits to clear.
45644574Sphk		 */
45744574Sphk		case TIME_WAIT:
45844574Sphk		if (!(time_status & (STA_INS | STA_DEL)))
45944574Sphk			time_state = TIME_OK;
4602858Swollman	}
46144574Sphk
46244574Sphk	/*
46344574Sphk	 * Compute the total time adjustment for the next
46444574Sphk	 * second in ns. The offset is reduced by a factor
46544574Sphk	 * depending on FLL or PLL mode and whether the PPS
46644574Sphk	 * signal is operating. Note that the value is in effect
46744574Sphk	 * scaled by the clock frequency, since the adjustment
46844574Sphk	 * is added at each tick interrupt.
46944574Sphk	 */
47044574Sphk	ftemp = time_offset;
47144574Sphk#ifdef PPS_SYNC
47244574Sphk	if (time_status & STA_PPSTIME && time_status &
47344574Sphk	    STA_PPSSIGNAL)
47444574Sphk		L_RSHIFT(ftemp, PPS_FAVG);
47544574Sphk	else if (time_status & STA_MODE)
47644574Sphk#else
47744574Sphk	if (time_status & STA_MODE)
47844574Sphk#endif /* PPS_SYNC */
47944574Sphk		L_RSHIFT(ftemp, SHIFT_FLL);
48044574Sphk	else
48144574Sphk		L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
48244574Sphk	time_adj = ftemp;
48344574Sphk	L_SUB(time_offset, ftemp);
48444574Sphk	L_ADD(time_adj, time_freq);
48544574Sphk	tcp->tc_adjustment = time_adj;
48644574Sphk#ifdef PPS_SYNC
48744574Sphk	if (pps_valid > 0)
48844574Sphk		pps_valid--;
48944574Sphk	else
49044574Sphk		time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
49144574Sphk		    STA_PPSWANDER | STA_PPSERROR);
49244574Sphk#endif /* PPS_SYNC */
4932858Swollman}
4942858Swollman
49544574Sphk/*
49644574Sphk * ntp_init() - initialize variables and structures
49744574Sphk *
49844574Sphk * This routine must be called after the kernel variables hz and tick
49944574Sphk * are set or changed and before the next tick interrupt. In this
50044574Sphk * particular implementation, these values are assumed set elsewhere in
50144574Sphk * the kernel. The design allows the clock frequency and tick interval
50244574Sphk * to be changed while the system is running. So, this routine should
50344574Sphk * probably be integrated with the code that does that.
50444574Sphk */
50544574Sphkstatic void
50644574Sphkntp_init()
50744574Sphk{
50844574Sphk
50944574Sphk	/*
51044574Sphk	 * The following variable must be initialized any time the
51144574Sphk	 * kernel variable hz is changed.
51244574Sphk	 */
51344574Sphk	time_tick = NANOSECOND / hz;
51444574Sphk
51544574Sphk	/*
51644574Sphk	 * The following variables are initialized only at startup. Only
51744574Sphk	 * those structures not cleared by the compiler need to be
51844574Sphk	 * initialized, and these only in the simulator. In the actual
51944574Sphk	 * kernel, any nonzero values here will quickly evaporate.
52044574Sphk	 */
52144574Sphk	L_CLR(time_offset);
52244574Sphk	L_CLR(time_freq);
52332513Sphk#ifdef PPS_SYNC
52444794Sphk	pps_filt.sec = pps_filt.nsec = 0;
52545294Sphk	pps_tf[0] = pps_tf[1] = pps_tf[2] = pps_filt;
52644794Sphk	pps_fcount = 0;
52744574Sphk	L_CLR(pps_freq);
52844574Sphk#endif /* PPS_SYNC */
52944574Sphk}
5302858Swollman
53144574SphkSYSINIT(ntpclocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, ntp_init, NULL)
53232513Sphk
53344574Sphk/*
53444574Sphk * hardupdate() - local clock update
53544574Sphk *
53644574Sphk * This routine is called by ntp_adjtime() to update the local clock
53744574Sphk * phase and frequency. The implementation is of an adaptive-parameter,
53844574Sphk * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
53944574Sphk * time and frequency offset estimates for each call. If the kernel PPS
54044574Sphk * discipline code is configured (PPS_SYNC), the PPS signal itself
54144574Sphk * determines the new time offset, instead of the calling argument.
54244574Sphk * Presumably, calls to ntp_adjtime() occur only when the caller
54344574Sphk * believes the local clock is valid within some bound (+-128 ms with
54444574Sphk * NTP). If the caller's time is far different than the PPS time, an
54544574Sphk * argument will ensue, and it's not clear who will lose.
54644574Sphk *
54744574Sphk * For uncompensated quartz crystal oscillators and nominal update
54844574Sphk * intervals less than 256 s, operation should be in phase-lock mode,
54944574Sphk * where the loop is disciplined to phase. For update intervals greater
55044574Sphk * than 1024 s, operation should be in frequency-lock mode, where the
55144574Sphk * loop is disciplined to frequency. Between 256 s and 1024 s, the mode
55244574Sphk * is selected by the STA_MODE status bit.
55344574Sphk */
55444574Sphkstatic void
55544574Sphkhardupdate(offset)
55644574Sphk	long offset;		/* clock offset (ns) */
55744574Sphk{
55844574Sphk	long ltemp, mtemp;
55944574Sphk	l_fp ftemp;
56032513Sphk
56144574Sphk	/*
56244574Sphk	 * Select how the phase is to be controlled and from which
56344574Sphk	 * source. If the PPS signal is present and enabled to
56444574Sphk	 * discipline the time, the PPS offset is used; otherwise, the
56544574Sphk	 * argument offset is used.
56644574Sphk	 */
56744574Sphk	ltemp = offset;
56844574Sphk	if (ltemp > MAXPHASE)
56944574Sphk		ltemp = MAXPHASE;
57044574Sphk	else if (ltemp < -MAXPHASE)
57144574Sphk		ltemp = -MAXPHASE;
57244574Sphk	if (!(time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL))
57344574Sphk		L_LINT(time_offset, ltemp);
57432513Sphk
57544574Sphk	/*
57644574Sphk	 * Select how the frequency is to be controlled and in which
57744574Sphk	 * mode (PLL or FLL). If the PPS signal is present and enabled
57844574Sphk	 * to discipline the frequency, the PPS frequency is used;
57944574Sphk	 * otherwise, the argument offset is used to compute it.
58044574Sphk	 */
58144574Sphk	if (time_status & STA_PPSFREQ && time_status & STA_PPSSIGNAL) {
58244574Sphk		time_reftime = time_second;
58344574Sphk		return;
58444574Sphk	}
58544574Sphk	if (time_status & STA_FREQHOLD || time_reftime == 0)
58644574Sphk		time_reftime = time_second;
58744574Sphk	mtemp = time_second - time_reftime;
58844574Sphk	if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)
58944574Sphk	    ) {
59044574Sphk		L_LINT(ftemp, (ltemp << 4) / mtemp);
59144574Sphk		L_RSHIFT(ftemp, SHIFT_FLL + 4);
59244574Sphk		L_ADD(time_freq, ftemp);
59344574Sphk		time_status |= STA_MODE;
59444574Sphk	} else {
59544574Sphk		L_LINT(ftemp, ltemp);
59644574Sphk		L_RSHIFT(ftemp, (SHIFT_PLL + 2 + time_constant) << 1);
59744574Sphk		L_MPY(ftemp, mtemp);
59844574Sphk		L_ADD(time_freq, ftemp);
59944574Sphk		time_status &= ~STA_MODE;
60044574Sphk	}
60144574Sphk	time_reftime = time_second;
60244574Sphk	if (L_GINT(time_freq) > MAXFREQ)
60344574Sphk		L_LINT(time_freq, MAXFREQ);
60444574Sphk	else if (L_GINT(time_freq) < -MAXFREQ)
60544574Sphk		L_LINT(time_freq, -MAXFREQ);
60644574Sphk}
60744574Sphk
60844574Sphk#ifdef PPS_SYNC
60932513Sphk/*
61032513Sphk * hardpps() - discipline CPU clock oscillator to external PPS signal
61132513Sphk *
61232513Sphk * This routine is called at each PPS interrupt in order to discipline
61332513Sphk * the CPU clock oscillator to the PPS signal. It measures the PPS phase
61432513Sphk * and leaves it in a handy spot for the hardclock() routine. It
61532513Sphk * integrates successive PPS phase differences and calculates the
61632513Sphk * frequency offset. This is used in hardclock() to discipline the CPU
61744574Sphk * clock oscillator so that the intrinsic frequency error is cancelled
61844574Sphk * out. The code requires the caller to capture the time and
61944574Sphk * architecture-dependent hardware counter values in nanoseconds at the
62044574Sphk * on-time PPS signal transition.
62132513Sphk *
62244574Sphk * Note that, on some Unix systems this routine runs at an interrupt
62332513Sphk * priority level higher than the timer interrupt routine hardclock().
62432513Sphk * Therefore, the variables used are distinct from the hardclock()
62544574Sphk * variables, except for the actual time and frequency variables, which
62644574Sphk * are determined by this routine and updated atomically.
62732513Sphk */
62832513Sphkvoid
62944574Sphkhardpps(tsp, nsec)
63044574Sphk	struct timespec *tsp;	/* time at PPS */
63144574Sphk	long nsec;		/* hardware counter at PPS */
63232513Sphk{
63344574Sphk	long u_sec, u_nsec, v_nsec; /* temps */
63444574Sphk	l_fp ftemp;
63532513Sphk
63632513Sphk	/*
63744574Sphk	 * The signal is first processed by a frequency discriminator
63844574Sphk	 * which rejects noise and input signals with frequencies
63944574Sphk	 * outside the range 1 +-MAXFREQ PPS. If two hits occur in the
64044574Sphk	 * same second, we ignore the later hit; if not and a hit occurs
64144574Sphk	 * outside the range gate, keep the later hit but do not
64244574Sphk	 * process it.
64332513Sphk	 */
64444574Sphk	time_status |= STA_PPSSIGNAL | STA_PPSJITTER;
64544574Sphk	time_status &= ~(STA_PPSWANDER | STA_PPSERROR);
64644574Sphk	pps_valid = PPS_VALID;
64744574Sphk	u_sec = tsp->tv_sec;
64844574Sphk	u_nsec = tsp->tv_nsec;
64944574Sphk	if (u_nsec >= (NANOSECOND >> 1)) {
65044574Sphk		u_nsec -= NANOSECOND;
65144574Sphk		u_sec++;
65244574Sphk	}
65344574Sphk	v_nsec = u_nsec - pps_tf[0].nsec;
65444574Sphk	if (u_sec == pps_tf[0].sec && v_nsec < -MAXFREQ) {
65544574Sphk		return;
65644574Sphk	}
65744574Sphk	pps_tf[2] = pps_tf[1];
65844574Sphk	pps_tf[1] = pps_tf[0];
65944574Sphk	pps_tf[0].sec = u_sec;
66044574Sphk	pps_tf[0].nsec = u_nsec;
66132513Sphk
66232513Sphk	/*
66344574Sphk	 * Compute the difference between the current and previous
66444574Sphk	 * counter values. If the difference exceeds 0.5 s, assume it
66544574Sphk	 * has wrapped around, so correct 1.0 s. If the result exceeds
66644574Sphk	 * the tick interval, the sample point has crossed a tick
66744574Sphk	 * boundary during the last second, so correct the tick. Very
66844574Sphk	 * intricate.
66944574Sphk	 */
67044666Sphk	u_nsec = nsec;
67144574Sphk	if (u_nsec > (NANOSECOND >> 1))
67244574Sphk		u_nsec -= NANOSECOND;
67344574Sphk	else if (u_nsec < -(NANOSECOND >> 1))
67444574Sphk		u_nsec += NANOSECOND;
67544794Sphk	pps_fcount += u_nsec;
67644574Sphk	if (v_nsec > MAXFREQ) {
67744574Sphk		return;
67844574Sphk	}
67944574Sphk	time_status &= ~STA_PPSJITTER;
68044574Sphk
68144574Sphk	/*
68244574Sphk	 * A three-stage median filter is used to help denoise the PPS
68332513Sphk	 * time. The median sample becomes the time offset estimate; the
68432513Sphk	 * difference between the other two samples becomes the time
68532513Sphk	 * dispersion (jitter) estimate.
68632513Sphk	 */
68744574Sphk	if (pps_tf[0].nsec > pps_tf[1].nsec) {
68844574Sphk		if (pps_tf[1].nsec > pps_tf[2].nsec) {
68944574Sphk			pps_filt = pps_tf[1];	/* 0 1 2 */
69044574Sphk			u_nsec = pps_tf[0].nsec - pps_tf[2].nsec;
69144574Sphk		} else if (pps_tf[2].nsec > pps_tf[0].nsec) {
69244574Sphk			pps_filt = pps_tf[0];	/* 2 0 1 */
69344574Sphk			u_nsec = pps_tf[2].nsec - pps_tf[1].nsec;
69444574Sphk		} else {
69544574Sphk			pps_filt = pps_tf[2];	/* 0 2 1 */
69644574Sphk			u_nsec = pps_tf[0].nsec - pps_tf[1].nsec;
69744574Sphk		}
69844574Sphk	} else {
69944574Sphk		if (pps_tf[1].nsec < pps_tf[2].nsec) {
70044574Sphk			pps_filt = pps_tf[1];	/* 2 1 0 */
70144574Sphk			u_nsec = pps_tf[2].nsec - pps_tf[0].nsec;
70244574Sphk		} else  if (pps_tf[2].nsec < pps_tf[0].nsec) {
70344574Sphk			pps_filt = pps_tf[0];	/* 1 0 2 */
70444574Sphk			u_nsec = pps_tf[1].nsec - pps_tf[2].nsec;
70544574Sphk		} else {
70644574Sphk			pps_filt = pps_tf[2];	/* 1 2 0 */
70744574Sphk			u_nsec = pps_tf[1].nsec - pps_tf[0].nsec;
70844574Sphk		}
70944574Sphk	}
71032513Sphk
71132513Sphk	/*
71244574Sphk	 * Nominal jitter is due to PPS signal noise and  interrupt
71344574Sphk	 * latency. If it exceeds the jitter limit, the sample is
71444574Sphk	 * discarded. otherwise, if so enabled, the time offset is
71544574Sphk	 * updated. The offsets are accumulated over the phase averaging
71644574Sphk	 * interval to improve accuracy. The jitter is averaged only for
71744574Sphk	 * performance monitoring. We can tolerate a modest loss of data
71844574Sphk	 * here without degrading time accuracy.
71932513Sphk	 */
72044574Sphk	if (u_nsec > MAXTIME) {
72144574Sphk		time_status |= STA_PPSJITTER;
72244574Sphk		pps_jitcnt++;
72344574Sphk	} else if (time_status & STA_PPSTIME) {
72444574Sphk		pps_offacc -= pps_filt.nsec;
72544574Sphk		pps_offcnt++;
72632513Sphk	}
72744574Sphk	if (pps_offcnt >= (1 << PPS_PAVG)) {
72844574Sphk		if (time_status & STA_PPSTIME) {
72944574Sphk			L_LINT(time_offset, pps_offacc);
73044574Sphk			L_RSHIFT(time_offset, PPS_PAVG);
73144574Sphk		}
73244574Sphk		pps_offacc = 0;
73344574Sphk		pps_offcnt = 0;
73444574Sphk	}
73544574Sphk	pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG;
73644574Sphk	u_sec = pps_tf[0].sec - pps_lastsec;
73745294Sphk	if (ntp_div && ntp_mult) {
73845294Sphk		L_LINT(ftemp, (pps_filt.nsec));
73945294Sphk		L_RSHIFT(ftemp, ntp_div);
74045294Sphk		L_MPY(ftemp, ntp_mult);
74145294Sphk		L_ADD(pps_freq, ftemp);
74245294Sphk		if (time_status & STA_PPSFREQ)
74345294Sphk			time_freq = pps_freq;
74445294Sphk		return;
74545294Sphk	}
74644574Sphk	if (u_sec < (1 << pps_shift))
74744574Sphk		return;
74844574Sphk
74932513Sphk	/*
75044574Sphk	 * At the end of the calibration interval the difference between
75144574Sphk	 * the first and last counter values becomes the scaled
75244574Sphk	 * frequency. It will later be divided by the length of the
75344574Sphk	 * interval to determine the frequency update. If the frequency
75444574Sphk	 * exceeds a sanity threshold, or if the actual calibration
75544574Sphk	 * interval is not equal to the expected length, the data are
75644574Sphk	 * discarded. We can tolerate a modest loss of data here without
75744574Sphk	 * degrading frequency ccuracy.
75832513Sphk	 */
75944574Sphk	pps_calcnt++;
76044794Sphk	v_nsec = -pps_fcount;
76144574Sphk	pps_lastsec = pps_tf[0].sec;
76244794Sphk	pps_fcount = 0;
76344574Sphk	u_nsec = MAXFREQ << pps_shift;
76444574Sphk	if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 <<
76544574Sphk	    pps_shift)) {
76644574Sphk		time_status |= STA_PPSERROR;
76732513Sphk		pps_errcnt++;
76832513Sphk		return;
76932513Sphk	}
77032513Sphk
77132513Sphk	/*
77244574Sphk	 * If the actual calibration interval is not equal to the
77344574Sphk	 * expected length, the data are discarded. If the wander is
77444574Sphk	 * less than the wander threshold for four consecutive
77544574Sphk	 * intervals, the interval is doubled; if it is greater than the
77644574Sphk	 * threshold for four consecutive intervals, the interval is
77744574Sphk	 * halved. The scaled frequency offset is converted to frequency
77844574Sphk	 * offset. The stability metric is calculated as the average of
77944574Sphk	 * recent frequency changes, but is used only for performance
78044574Sphk	 * monitoring.
78132513Sphk	 */
78244574Sphk	L_LINT(ftemp, v_nsec);
78344574Sphk	L_RSHIFT(ftemp, pps_shift);
78444574Sphk	L_SUB(ftemp, pps_freq);
78544574Sphk	u_nsec = L_GINT(ftemp);
78644574Sphk	if (u_nsec > MAXWANDER) {
78744574Sphk		L_LINT(ftemp, MAXWANDER);
78844574Sphk		pps_intcnt--;
78944574Sphk		time_status |= STA_PPSWANDER;
79032513Sphk		pps_stbcnt++;
79144574Sphk	} else if (u_nsec < -MAXWANDER) {
79244574Sphk		L_LINT(ftemp, -MAXWANDER);
79344574Sphk		pps_intcnt--;
79432513Sphk		time_status |= STA_PPSWANDER;
79544574Sphk		pps_stbcnt++;
79644574Sphk	} else {
79744574Sphk		pps_intcnt++;
79832513Sphk	}
79944574Sphk	if (pps_intcnt >= 4) {
80044574Sphk		pps_intcnt = 4;
80144574Sphk		if (pps_shift < PPS_FAVGMAX) {
80244574Sphk			pps_shift++;
80344574Sphk			pps_intcnt = 0;
80432513Sphk		}
80544574Sphk	} else if (pps_intcnt <= -4) {
80644574Sphk		pps_intcnt = -4;
80744574Sphk		if (pps_shift > PPS_FAVG) {
80844574Sphk			pps_shift--;
80944574Sphk			pps_intcnt = 0;
81044574Sphk		}
81132513Sphk	}
81244574Sphk	if (u_nsec < 0)
81344574Sphk		u_nsec = -u_nsec;
81444574Sphk	pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG;
81532513Sphk
81632513Sphk	/*
81744574Sphk	 * The frequency offset is averaged into the PPS frequency. If
81844574Sphk	 * enabled, the system clock frequency is updated as well.
81932513Sphk	 */
82045294Sphk	L_RSHIFT(ftemp, PPS_FAVG);
82144574Sphk	L_ADD(pps_freq, ftemp);
82244574Sphk	u_nsec = L_GINT(pps_freq);
82344574Sphk	if (u_nsec > MAXFREQ)
82444574Sphk		L_LINT(pps_freq, MAXFREQ);
82544574Sphk	else if (u_nsec < -MAXFREQ)
82644574Sphk		L_LINT(pps_freq, -MAXFREQ);
82744574Sphk	if (time_status & STA_PPSFREQ)
82844574Sphk		time_freq = pps_freq;
82932513Sphk}
83032513Sphk#endif /* PPS_SYNC */
831