kern_ntptime.c revision 50656
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
1482858Swollman#ifdef PPS_SYNC
1492858Swollman/*
15044574Sphk * The following variables are used when a pulse-per-second (PPS) signal
15144574Sphk * is available and connected via a modem control lead. They establish
15244574Sphk * the engineering parameters of the clock discipline loop when
15344574Sphk * controlled by the PPS signal.
1542858Swollman */
15544574Sphk#define PPS_FAVG	2		/* min freq avg interval (s) (shift) */
15650656Sphk#define PPS_FAVGDEF	7		/* default freq avg int (s) (shift) */
15750656Sphk#define PPS_FAVGMAX	15		/* max freq avg interval (s) (shift) */
15844574Sphk#define PPS_PAVG	4		/* phase avg interval (s) (shift) */
15944574Sphk#define PPS_VALID	120		/* PPS signal watchdog max (s) */
16050656Sphk#define PPS_MAXWANDER	100000		/* max PPS wander (ns/s) */
16150656Sphk#define PPS_POPCORN	2		/* popcorn spike threshold (shift) */
16232513Sphk
16350656Sphkstatic struct timespec pps_tf[3];	/* phase median filter */
16444574Sphkstatic l_fp pps_freq;			/* scaled frequency offset (ns/s) */
16545294Sphkstatic long pps_fcount;			/* frequency accumulator */
16650656Sphkstatic long pps_jitter;			/* nominal jitter (ns) */
16750656Sphkstatic long pps_stabil;			/* nominal stability (scaled ns/s) */
16844574Sphkstatic long pps_lastsec;		/* time at last calibration (s) */
16944574Sphkstatic int pps_valid;			/* signal watchdog counter */
17044574Sphkstatic int pps_shift = PPS_FAVG;	/* interval duration (s) (shift) */
17150656Sphkstatic int pps_shiftmax = PPS_FAVGDEF;	/* max interval duration (s) (shift) */
17244574Sphkstatic int pps_intcnt;			/* wander counter */
17344574Sphk
17432513Sphk/*
17532513Sphk * PPS signal quality monitors
17632513Sphk */
17744574Sphkstatic long pps_calcnt;			/* calibration intervals */
17844574Sphkstatic long pps_jitcnt;			/* jitter limit exceeded */
17944574Sphkstatic long pps_stbcnt;			/* stability limit exceeded */
18044574Sphkstatic long pps_errcnt;			/* calibration errors */
1812858Swollman#endif /* PPS_SYNC */
18232513Sphk/*
18344574Sphk * End of phase/frequency-lock loop (PLL/FLL) definitions
18432513Sphk */
18532513Sphk
18644574Sphkstatic void ntp_init(void);
18744574Sphkstatic void hardupdate(long offset);
18832513Sphk
18933690Sphk/*
19044574Sphk * ntp_gettime() - NTP user application interface
19133690Sphk *
19244574Sphk * See the timex.h header file for synopsis and API description.
19333690Sphk */
19412279Sphkstatic int
19512279Sphkntp_sysctl SYSCTL_HANDLER_ARGS
1962858Swollman{
19744574Sphk	struct ntptimeval ntv;	/* temporary structure */
19844574Sphk	struct timespec atv;	/* nanosecond time */
1992858Swollman
20044574Sphk	nanotime(&atv);
20144574Sphk	ntv.time.tv_sec = atv.tv_sec;
20244574Sphk	ntv.time.tv_nsec = atv.tv_nsec;
2032858Swollman	ntv.maxerror = time_maxerror;
2042858Swollman	ntv.esterror = time_esterror;
2052858Swollman	ntv.time_state = time_state;
2062858Swollman
2072858Swollman	/*
20844574Sphk	 * Status word error decode. If any of these conditions occur,
20944574Sphk	 * an error is returned, instead of the status word. Most
21044574Sphk	 * applications will care only about the fact the system clock
21144574Sphk	 * may not be trusted, not about the details.
2122858Swollman	 *
2132858Swollman	 * Hardware or software error
2142858Swollman	 */
21544574Sphk	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
2162858Swollman
2172858Swollman	/*
21844574Sphk	 * PPS signal lost when either time or frequency synchronization
21944574Sphk	 * requested
2202858Swollman	 */
22144574Sphk	    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
22244574Sphk	    !(time_status & STA_PPSSIGNAL)) ||
2232858Swollman
2242858Swollman	/*
22544574Sphk	 * PPS jitter exceeded when time synchronization requested
2262858Swollman	 */
22744574Sphk	    (time_status & STA_PPSTIME &&
22844574Sphk	    time_status & STA_PPSJITTER) ||
2292858Swollman
2302858Swollman	/*
23144574Sphk	 * PPS wander exceeded or calibration error when frequency
23244574Sphk	 * synchronization requested
2332858Swollman	 */
23444574Sphk	    (time_status & STA_PPSFREQ &&
23544574Sphk	    time_status & (STA_PPSWANDER | STA_PPSERROR)))
2362858Swollman		ntv.time_state = TIME_ERROR;
23712279Sphk	return (sysctl_handle_opaque(oidp, &ntv, sizeof ntv, req));
2382858Swollman}
2392858Swollman
24044574SphkSYSCTL_NODE(_kern, OID_AUTO, ntp_pll, CTLFLAG_RW, 0, "");
24144574SphkSYSCTL_PROC(_kern_ntp_pll, OID_AUTO, gettime, CTLTYPE_OPAQUE|CTLFLAG_RD,
24212623Sphk	0, sizeof(struct ntptimeval) , ntp_sysctl, "S,ntptimeval", "");
24312279Sphk
24450656SphkSYSCTL_INT(_kern_ntp_pll, OID_AUTO, pps_shiftmax, CTLFLAG_RW, &pps_shiftmax, 0, "");
2452858Swollman/*
2462858Swollman * ntp_adjtime() - NTP daemon application interface
24744574Sphk *
24844574Sphk * See the timex.h header file for synopsis and API description.
2492858Swollman */
25012221Sbde#ifndef _SYS_SYSPROTO_H_
2512858Swollmanstruct ntp_adjtime_args {
25244574Sphk	struct timex *tp;
2532858Swollman};
25412221Sbde#endif
2552858Swollman
2562858Swollmanint
25730994Sphkntp_adjtime(struct proc *p, struct ntp_adjtime_args *uap)
2582858Swollman{
25944574Sphk	struct timex ntv;	/* temporary structure */
26045294Sphk	long freq;		/* frequency ns/s) */
26144574Sphk	int modes;		/* mode bits from structure */
26244574Sphk	int s;			/* caller priority */
2632858Swollman	int error;
2642858Swollman
2652858Swollman	error = copyin((caddr_t)uap->tp, (caddr_t)&ntv, sizeof(ntv));
2662858Swollman	if (error)
26744574Sphk		return(error);
2682858Swollman
2692858Swollman	/*
2702858Swollman	 * Update selected clock variables - only the superuser can
2712858Swollman	 * change anything. Note that there is no error checking here on
2722858Swollman	 * the assumption the superuser should know what it is doing.
2732858Swollman	 */
2742858Swollman	modes = ntv.modes;
27544776Sphk	if (modes)
27646112Sphk		error = suser(p);
27744574Sphk	if (error)
27844574Sphk		return (error);
2792858Swollman	s = splclock();
28044574Sphk	if (modes & MOD_FREQUENCY) {
28145302Sphk		freq = (ntv.freq * 1000LL) >> 16;
28245294Sphk		if (freq > MAXFREQ)
28345294Sphk			L_LINT(time_freq, MAXFREQ);
28445294Sphk		else if (freq < -MAXFREQ)
28545294Sphk			L_LINT(time_freq, -MAXFREQ);
28645294Sphk		else
28745294Sphk			L_LINT(time_freq, freq);
28845294Sphk
2892858Swollman#ifdef PPS_SYNC
29044574Sphk		pps_freq = time_freq;
2912858Swollman#endif /* PPS_SYNC */
29244574Sphk	}
2932858Swollman	if (modes & MOD_MAXERROR)
2942858Swollman		time_maxerror = ntv.maxerror;
2952858Swollman	if (modes & MOD_ESTERROR)
2962858Swollman		time_esterror = ntv.esterror;
2972858Swollman	if (modes & MOD_STATUS) {
2982858Swollman		time_status &= STA_RONLY;
2992858Swollman		time_status |= ntv.status & ~STA_RONLY;
3002858Swollman	}
30145294Sphk	if (modes & MOD_TIMECONST) {
30245294Sphk		if (ntv.constant < 0)
30345294Sphk			time_constant = 0;
30445294Sphk		else if (ntv.constant > MAXTC)
30545294Sphk			time_constant = MAXTC;
30645294Sphk		else
30745294Sphk			time_constant = ntv.constant;
30845294Sphk	}
30950656Sphk#ifdef PPS_SYNC
31050656Sphk	if (modes & MOD_PPSMAX) {
31150656Sphk		if (ntv.shift < PPS_FAVG)
31250656Sphk			pps_shiftmax = PPS_FAVG;
31350656Sphk		else if (ntv.shift > PPS_FAVGMAX)
31450656Sphk			pps_shiftmax = PPS_FAVGMAX;
31550656Sphk		else
31650656Sphk			pps_shiftmax = ntv.shift;
31750656Sphk	}
31850656Sphk#endif /* PPS_SYNC */
31944574Sphk	if (modes & MOD_NANO)
32044574Sphk		time_status |= STA_NANO;
32144574Sphk	if (modes & MOD_MICRO)
32244574Sphk		time_status &= ~STA_NANO;
32344574Sphk	if (modes & MOD_CLKB)
32444574Sphk		time_status |= STA_CLK;
32544574Sphk	if (modes & MOD_CLKA)
32644574Sphk		time_status &= ~STA_CLK;
32744574Sphk	if (modes & MOD_OFFSET) {
32844574Sphk		if (time_status & STA_NANO)
32944574Sphk			hardupdate(ntv.offset);
33044574Sphk		else
33144574Sphk			hardupdate(ntv.offset * 1000);
33244574Sphk	}
3332858Swollman
3342858Swollman	/*
3352858Swollman	 * Retrieve all clock variables
3362858Swollman	 */
33744574Sphk	if (time_status & STA_NANO)
33844574Sphk		ntv.offset = L_GINT(time_offset);
3392858Swollman	else
34044574Sphk		ntv.offset = L_GINT(time_offset) / 1000;
34145295Sphk	ntv.freq = L_GINT((time_freq / 1000LL) << 16);
3422858Swollman	ntv.maxerror = time_maxerror;
3432858Swollman	ntv.esterror = time_esterror;
3442858Swollman	ntv.status = time_status;
34545294Sphk	ntv.constant = time_constant;
34644574Sphk	if (time_status & STA_NANO)
34744574Sphk		ntv.precision = time_precision;
34844574Sphk	else
34944574Sphk		ntv.precision = time_precision / 1000;
35044574Sphk	ntv.tolerance = MAXFREQ * SCALE_PPM;
3512858Swollman#ifdef PPS_SYNC
3522858Swollman	ntv.shift = pps_shift;
35345295Sphk	ntv.ppsfreq = L_GINT((pps_freq / 1000LL) << 16);
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)))
38145302Sphk		p->p_retval[0] = TIME_ERROR;
38245302Sphk	else
38345302Sphk		p->p_retval[0] = time_state;
38445302Sphk	return (error);
38544574Sphk}
38644574Sphk
38744574Sphk/*
38844574Sphk * second_overflow() - called after ntp_tick_adjust()
38944574Sphk *
39044574Sphk * This routine is ordinarily called immediately following the above
39144574Sphk * routine ntp_tick_adjust(). While these two routines are normally
39244574Sphk * combined, they are separated here only for the purposes of
39344574Sphk * simulation.
39444574Sphk */
39544574Sphkvoid
39644574Sphkntp_update_second(struct timecounter *tcp)
39744574Sphk{
39844574Sphk	u_int32_t *newsec;
39944666Sphk	l_fp ftemp, time_adj;		/* 32/64-bit temporaries */
40044574Sphk
40144574Sphk	newsec = &tcp->tc_offset_sec;
40250656Sphk	/*
40350656Sphk	 * On rollover of the second both the nanosecond and microsecond
40450656Sphk	 * clocks are updated and the state machine cranked as
40550656Sphk	 * necessary. The phase adjustment to be used for the next
40650656Sphk	 * second is calculated and the maximum error is increased by
40750656Sphk	 * the tolerance.
40850656Sphk	 */
40944574Sphk	time_maxerror += MAXFREQ / 1000;
41044574Sphk
41144574Sphk	/*
41244574Sphk	 * Leap second processing. If in leap-insert state at
41344574Sphk	 * the end of the day, the system clock is set back one
41444574Sphk	 * second; if in leap-delete state, the system clock is
41544574Sphk	 * set ahead one second. The nano_time() routine or
41644574Sphk	 * external clock driver will insure that reported time
41744574Sphk	 * is always monotonic.
41844574Sphk	 */
41944574Sphk	switch (time_state) {
42044574Sphk
4212858Swollman		/*
42244574Sphk		 * No warning.
4232858Swollman		 */
42444574Sphk		case TIME_OK:
42544574Sphk		if (time_status & STA_INS)
42644574Sphk			time_state = TIME_INS;
42744574Sphk		else if (time_status & STA_DEL)
42844574Sphk			time_state = TIME_DEL;
42944574Sphk		break;
43044574Sphk
43144574Sphk		/*
43244574Sphk		 * Insert second 23:59:60 following second
43344574Sphk		 * 23:59:59.
43444574Sphk		 */
43544574Sphk		case TIME_INS:
43644574Sphk		if (!(time_status & STA_INS))
43744574Sphk			time_state = TIME_OK;
43844574Sphk		else if ((*newsec) % 86400 == 0) {
43944574Sphk			(*newsec)--;
44044574Sphk			time_state = TIME_OOP;
44144574Sphk		}
44244574Sphk		break;
44344574Sphk
44444574Sphk		/*
44544574Sphk		 * Delete second 23:59:59.
44644574Sphk		 */
44744574Sphk		case TIME_DEL:
44844574Sphk		if (!(time_status & STA_DEL))
44944574Sphk			time_state = TIME_OK;
45044574Sphk		else if (((*newsec) + 1) % 86400 == 0) {
45144574Sphk			(*newsec)++;
45244574Sphk			time_state = TIME_WAIT;
45344574Sphk		}
45444574Sphk		break;
45544574Sphk
45644574Sphk		/*
45744574Sphk		 * Insert second in progress.
45844574Sphk		 */
45944574Sphk		case TIME_OOP:
46044574Sphk		time_state = TIME_WAIT;
46144574Sphk		break;
46244574Sphk
46344574Sphk		/*
46444574Sphk		 * Wait for status bits to clear.
46544574Sphk		 */
46644574Sphk		case TIME_WAIT:
46744574Sphk		if (!(time_status & (STA_INS | STA_DEL)))
46844574Sphk			time_state = TIME_OK;
4692858Swollman	}
47044574Sphk
47144574Sphk	/*
47250656Sphk	 * Compute the total time adjustment for the next second
47350656Sphk	 * in ns. The offset is reduced by a factor depending on
47450656Sphk	 * whether the PPS signal is operating. Note that the
47550656Sphk	 * value is in effect scaled by the clock frequency,
47650656Sphk	 * since the adjustment is added at each tick interrupt.
47744574Sphk	 */
47844574Sphk	ftemp = time_offset;
47944574Sphk#ifdef PPS_SYNC
48044574Sphk	if (time_status & STA_PPSTIME && time_status &
48144574Sphk	    STA_PPSSIGNAL)
48244574Sphk		L_RSHIFT(ftemp, PPS_FAVG);
48350656Sphk	else
48450656Sphk		L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
48544574Sphk#else
48650656Sphk		L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
48744574Sphk#endif /* PPS_SYNC */
48844574Sphk	time_adj = ftemp;
48944574Sphk	L_SUB(time_offset, ftemp);
49044574Sphk	L_ADD(time_adj, time_freq);
49144574Sphk	tcp->tc_adjustment = time_adj;
49244574Sphk#ifdef PPS_SYNC
49344574Sphk	if (pps_valid > 0)
49444574Sphk		pps_valid--;
49544574Sphk	else
49644574Sphk		time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
49744574Sphk		    STA_PPSWANDER | STA_PPSERROR);
49844574Sphk#endif /* PPS_SYNC */
4992858Swollman}
5002858Swollman
50144574Sphk/*
50244574Sphk * ntp_init() - initialize variables and structures
50344574Sphk *
50444574Sphk * This routine must be called after the kernel variables hz and tick
50544574Sphk * are set or changed and before the next tick interrupt. In this
50644574Sphk * particular implementation, these values are assumed set elsewhere in
50744574Sphk * the kernel. The design allows the clock frequency and tick interval
50844574Sphk * to be changed while the system is running. So, this routine should
50944574Sphk * probably be integrated with the code that does that.
51044574Sphk */
51144574Sphkstatic void
51244574Sphkntp_init()
51344574Sphk{
51444574Sphk
51544574Sphk	/*
51644574Sphk	 * The following variable must be initialized any time the
51744574Sphk	 * kernel variable hz is changed.
51844574Sphk	 */
51944574Sphk	time_tick = NANOSECOND / hz;
52044574Sphk
52144574Sphk	/*
52244574Sphk	 * The following variables are initialized only at startup. Only
52344574Sphk	 * those structures not cleared by the compiler need to be
52444574Sphk	 * initialized, and these only in the simulator. In the actual
52544574Sphk	 * kernel, any nonzero values here will quickly evaporate.
52644574Sphk	 */
52744574Sphk	L_CLR(time_offset);
52844574Sphk	L_CLR(time_freq);
52932513Sphk#ifdef PPS_SYNC
53050656Sphk	pps_tf[0].tv_sec = pps_tf[0].tv_nsec = 0;
53150656Sphk	pps_tf[1].tv_sec = pps_tf[1].tv_nsec = 0;
53250656Sphk	pps_tf[2].tv_sec = pps_tf[2].tv_nsec = 0;
53344794Sphk	pps_fcount = 0;
53444574Sphk	L_CLR(pps_freq);
53544574Sphk#endif /* PPS_SYNC */
53644574Sphk}
5372858Swollman
53844574SphkSYSINIT(ntpclocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, ntp_init, NULL)
53932513Sphk
54044574Sphk/*
54144574Sphk * hardupdate() - local clock update
54244574Sphk *
54344574Sphk * This routine is called by ntp_adjtime() to update the local clock
54444574Sphk * phase and frequency. The implementation is of an adaptive-parameter,
54544574Sphk * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
54644574Sphk * time and frequency offset estimates for each call. If the kernel PPS
54744574Sphk * discipline code is configured (PPS_SYNC), the PPS signal itself
54844574Sphk * determines the new time offset, instead of the calling argument.
54944574Sphk * Presumably, calls to ntp_adjtime() occur only when the caller
55044574Sphk * believes the local clock is valid within some bound (+-128 ms with
55144574Sphk * NTP). If the caller's time is far different than the PPS time, an
55244574Sphk * argument will ensue, and it's not clear who will lose.
55344574Sphk *
55444574Sphk * For uncompensated quartz crystal oscillators and nominal update
55544574Sphk * intervals less than 256 s, operation should be in phase-lock mode,
55644574Sphk * where the loop is disciplined to phase. For update intervals greater
55744574Sphk * than 1024 s, operation should be in frequency-lock mode, where the
55844574Sphk * loop is disciplined to frequency. Between 256 s and 1024 s, the mode
55944574Sphk * is selected by the STA_MODE status bit.
56044574Sphk */
56144574Sphkstatic void
56244574Sphkhardupdate(offset)
56344574Sphk	long offset;		/* clock offset (ns) */
56444574Sphk{
56544574Sphk	long ltemp, mtemp;
56644574Sphk	l_fp ftemp;
56732513Sphk
56844574Sphk	/*
56944574Sphk	 * Select how the phase is to be controlled and from which
57044574Sphk	 * source. If the PPS signal is present and enabled to
57144574Sphk	 * discipline the time, the PPS offset is used; otherwise, the
57244574Sphk	 * argument offset is used.
57344574Sphk	 */
57450656Sphk	if (!(time_status & STA_PLL))
57550656Sphk		return;
57644574Sphk	ltemp = offset;
57744574Sphk	if (ltemp > MAXPHASE)
57844574Sphk		ltemp = MAXPHASE;
57944574Sphk	else if (ltemp < -MAXPHASE)
58044574Sphk		ltemp = -MAXPHASE;
58144574Sphk	if (!(time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL))
58244574Sphk		L_LINT(time_offset, ltemp);
58332513Sphk
58444574Sphk	/*
58544574Sphk	 * Select how the frequency is to be controlled and in which
58644574Sphk	 * mode (PLL or FLL). If the PPS signal is present and enabled
58744574Sphk	 * to discipline the frequency, the PPS frequency is used;
58844574Sphk	 * otherwise, the argument offset is used to compute it.
58944574Sphk	 */
59044574Sphk	if (time_status & STA_PPSFREQ && time_status & STA_PPSSIGNAL) {
59144574Sphk		time_reftime = time_second;
59244574Sphk		return;
59344574Sphk	}
59444574Sphk	if (time_status & STA_FREQHOLD || time_reftime == 0)
59544574Sphk		time_reftime = time_second;
59644574Sphk	mtemp = time_second - time_reftime;
59750656Sphk	L_LINT(ftemp, ltemp);
59850656Sphk	L_RSHIFT(ftemp, (SHIFT_PLL + 2 + time_constant) << 1);
59950656Sphk	L_MPY(ftemp, mtemp);
60050656Sphk	L_ADD(time_freq, ftemp);
60150656Sphk	time_status &= ~STA_MODE;
60250656Sphk	if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
60344574Sphk		L_LINT(ftemp, (ltemp << 4) / mtemp);
60444574Sphk		L_RSHIFT(ftemp, SHIFT_FLL + 4);
60544574Sphk		L_ADD(time_freq, ftemp);
60644574Sphk		time_status |= STA_MODE;
60744574Sphk	}
60844574Sphk	time_reftime = time_second;
60944574Sphk	if (L_GINT(time_freq) > MAXFREQ)
61044574Sphk		L_LINT(time_freq, MAXFREQ);
61144574Sphk	else if (L_GINT(time_freq) < -MAXFREQ)
61244574Sphk		L_LINT(time_freq, -MAXFREQ);
61344574Sphk}
61444574Sphk
61544574Sphk#ifdef PPS_SYNC
61632513Sphk/*
61732513Sphk * hardpps() - discipline CPU clock oscillator to external PPS signal
61832513Sphk *
61932513Sphk * This routine is called at each PPS interrupt in order to discipline
62032513Sphk * the CPU clock oscillator to the PPS signal. It measures the PPS phase
62132513Sphk * and leaves it in a handy spot for the hardclock() routine. It
62232513Sphk * integrates successive PPS phase differences and calculates the
62332513Sphk * frequency offset. This is used in hardclock() to discipline the CPU
62444574Sphk * clock oscillator so that the intrinsic frequency error is cancelled
62544574Sphk * out. The code requires the caller to capture the time and
62644574Sphk * architecture-dependent hardware counter values in nanoseconds at the
62744574Sphk * on-time PPS signal transition.
62832513Sphk *
62944574Sphk * Note that, on some Unix systems this routine runs at an interrupt
63032513Sphk * priority level higher than the timer interrupt routine hardclock().
63132513Sphk * Therefore, the variables used are distinct from the hardclock()
63244574Sphk * variables, except for the actual time and frequency variables, which
63344574Sphk * are determined by this routine and updated atomically.
63432513Sphk */
63532513Sphkvoid
63644574Sphkhardpps(tsp, nsec)
63744574Sphk	struct timespec *tsp;	/* time at PPS */
63844574Sphk	long nsec;		/* hardware counter at PPS */
63932513Sphk{
64044574Sphk	long u_sec, u_nsec, v_nsec; /* temps */
64144574Sphk	l_fp ftemp;
64232513Sphk
64332513Sphk	/*
64444574Sphk	 * The signal is first processed by a frequency discriminator
64544574Sphk	 * which rejects noise and input signals with frequencies
64644574Sphk	 * outside the range 1 +-MAXFREQ PPS. If two hits occur in the
64744574Sphk	 * same second, we ignore the later hit; if not and a hit occurs
64844574Sphk	 * outside the range gate, keep the later hit but do not
64944574Sphk	 * process it.
65032513Sphk	 */
65144574Sphk	time_status |= STA_PPSSIGNAL | STA_PPSJITTER;
65244574Sphk	time_status &= ~(STA_PPSWANDER | STA_PPSERROR);
65344574Sphk	pps_valid = PPS_VALID;
65444574Sphk	u_sec = tsp->tv_sec;
65544574Sphk	u_nsec = tsp->tv_nsec;
65644574Sphk	if (u_nsec >= (NANOSECOND >> 1)) {
65744574Sphk		u_nsec -= NANOSECOND;
65844574Sphk		u_sec++;
65944574Sphk	}
66050656Sphk	v_nsec = u_nsec - pps_tf[0].tv_nsec;
66150656Sphk	if (u_sec == pps_tf[0].tv_sec && v_nsec < -MAXFREQ) {
66244574Sphk		return;
66344574Sphk	}
66444574Sphk	pps_tf[2] = pps_tf[1];
66544574Sphk	pps_tf[1] = pps_tf[0];
66650656Sphk	pps_tf[0].tv_sec = u_sec;
66750656Sphk	pps_tf[0].tv_nsec = u_nsec;
66832513Sphk
66932513Sphk	/*
67044574Sphk	 * Compute the difference between the current and previous
67144574Sphk	 * counter values. If the difference exceeds 0.5 s, assume it
67244574Sphk	 * has wrapped around, so correct 1.0 s. If the result exceeds
67344574Sphk	 * the tick interval, the sample point has crossed a tick
67444574Sphk	 * boundary during the last second, so correct the tick. Very
67544574Sphk	 * intricate.
67644574Sphk	 */
67744666Sphk	u_nsec = nsec;
67844574Sphk	if (u_nsec > (NANOSECOND >> 1))
67944574Sphk		u_nsec -= NANOSECOND;
68044574Sphk	else if (u_nsec < -(NANOSECOND >> 1))
68144574Sphk		u_nsec += NANOSECOND;
68244794Sphk	pps_fcount += u_nsec;
68350656Sphk	if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ) {
68444574Sphk		return;
68544574Sphk	}
68644574Sphk	time_status &= ~STA_PPSJITTER;
68744574Sphk
68844574Sphk	/*
68944574Sphk	 * A three-stage median filter is used to help denoise the PPS
69032513Sphk	 * time. The median sample becomes the time offset estimate; the
69132513Sphk	 * difference between the other two samples becomes the time
69232513Sphk	 * dispersion (jitter) estimate.
69332513Sphk	 */
69450656Sphk	if (pps_tf[0].tv_nsec > pps_tf[1].tv_nsec) {
69550656Sphk		if (pps_tf[1].tv_nsec > pps_tf[2].tv_nsec) {
69650656Sphk			v_nsec = pps_tf[1].tv_nsec;	/* 0 1 2 */
69750656Sphk			u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec;
69850656Sphk		} else if (pps_tf[2].tv_nsec > pps_tf[0].tv_nsec) {
69950656Sphk			v_nsec = pps_tf[0].tv_nsec;	/* 2 0 1 */
70050656Sphk			u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec;
70144574Sphk		} else {
70250656Sphk			v_nsec = pps_tf[2].tv_nsec;	/* 0 2 1 */
70350656Sphk			u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec;
70444574Sphk		}
70544574Sphk	} else {
70650656Sphk		if (pps_tf[1].tv_nsec < pps_tf[2].tv_nsec) {
70750656Sphk			v_nsec = pps_tf[1].tv_nsec;	/* 2 1 0 */
70850656Sphk			u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec;
70950656Sphk		} else  if (pps_tf[2].tv_nsec < pps_tf[0].tv_nsec) {
71050656Sphk			v_nsec = pps_tf[0].tv_nsec;	/* 1 0 2 */
71150656Sphk			u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec;
71244574Sphk		} else {
71350656Sphk			v_nsec = pps_tf[2].tv_nsec;	/* 1 2 0 */
71450656Sphk			u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec;
71544574Sphk		}
71644574Sphk	}
71732513Sphk
71832513Sphk	/*
71944574Sphk	 * Nominal jitter is due to PPS signal noise and  interrupt
72050656Sphk	 * latency. If it exceeds the popcorn threshold,
72150656Sphk	 * the sample is discarded. otherwise, if so enabled, the time
72250656Sphk	 * offset is updated. We can tolerate a modest loss of data here
72350656Sphk	 * without degrading time accuracy.
72432513Sphk	 */
72550656Sphk	if (u_nsec > (pps_jitter << PPS_POPCORN)) {
72644574Sphk		time_status |= STA_PPSJITTER;
72744574Sphk		pps_jitcnt++;
72844574Sphk	} else if (time_status & STA_PPSTIME) {
72950656Sphk		L_LINT(ftemp, v_nsec);
73050656Sphk		L_SUB(ftemp, time_offset);
73150656Sphk		L_RSHIFT(ftemp, pps_shift);
73250656Sphk		L_SUB(time_offset, ftemp);
73332513Sphk	}
73444574Sphk	pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG;
73550656Sphk	u_sec = pps_tf[0].tv_sec - pps_lastsec;
73644574Sphk	if (u_sec < (1 << pps_shift))
73744574Sphk		return;
73844574Sphk
73932513Sphk	/*
74044574Sphk	 * At the end of the calibration interval the difference between
74144574Sphk	 * the first and last counter values becomes the scaled
74244574Sphk	 * frequency. It will later be divided by the length of the
74344574Sphk	 * interval to determine the frequency update. If the frequency
74444574Sphk	 * exceeds a sanity threshold, or if the actual calibration
74544574Sphk	 * interval is not equal to the expected length, the data are
74644574Sphk	 * discarded. We can tolerate a modest loss of data here without
74744574Sphk	 * degrading frequency ccuracy.
74832513Sphk	 */
74944574Sphk	pps_calcnt++;
75044794Sphk	v_nsec = -pps_fcount;
75150656Sphk	pps_lastsec = pps_tf[0].tv_sec;
75244794Sphk	pps_fcount = 0;
75344574Sphk	u_nsec = MAXFREQ << pps_shift;
75444574Sphk	if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 <<
75544574Sphk	    pps_shift)) {
75644574Sphk		time_status |= STA_PPSERROR;
75732513Sphk		pps_errcnt++;
75832513Sphk		return;
75932513Sphk	}
76032513Sphk
76132513Sphk	/*
76250656Sphk	 * Here the raw frequency offset and wander (stability) is
76350656Sphk	 * calculated. If the wander is less than the wander threshold
76450656Sphk	 * for four consecutive averaging intervals, the interval is
76550656Sphk	 * doubled; if it is greater than the threshold for four
76650656Sphk	 * consecutive intervals, the interval is halved. The scaled
76750656Sphk	 * frequency offset is converted to frequency offset. The
76850656Sphk	 * stability metric is calculated as the average of recent
76950656Sphk	 * frequency changes, but is used only for performance
77044574Sphk	 * monitoring.
77132513Sphk	 */
77244574Sphk	L_LINT(ftemp, v_nsec);
77344574Sphk	L_RSHIFT(ftemp, pps_shift);
77444574Sphk	L_SUB(ftemp, pps_freq);
77544574Sphk	u_nsec = L_GINT(ftemp);
77650656Sphk	if (u_nsec > PPS_MAXWANDER) {
77750656Sphk		L_LINT(ftemp, PPS_MAXWANDER);
77844574Sphk		pps_intcnt--;
77944574Sphk		time_status |= STA_PPSWANDER;
78032513Sphk		pps_stbcnt++;
78150656Sphk	} else if (u_nsec < -PPS_MAXWANDER) {
78250656Sphk		L_LINT(ftemp, -PPS_MAXWANDER);
78344574Sphk		pps_intcnt--;
78432513Sphk		time_status |= STA_PPSWANDER;
78544574Sphk		pps_stbcnt++;
78644574Sphk	} else {
78744574Sphk		pps_intcnt++;
78832513Sphk	}
78944574Sphk	if (pps_intcnt >= 4) {
79044574Sphk		pps_intcnt = 4;
79150656Sphk		if (pps_shift < pps_shiftmax) {
79244574Sphk			pps_shift++;
79344574Sphk			pps_intcnt = 0;
79432513Sphk		}
79544574Sphk	} else if (pps_intcnt <= -4) {
79644574Sphk		pps_intcnt = -4;
79744574Sphk		if (pps_shift > PPS_FAVG) {
79844574Sphk			pps_shift--;
79944574Sphk			pps_intcnt = 0;
80044574Sphk		}
80132513Sphk	}
80244574Sphk	if (u_nsec < 0)
80344574Sphk		u_nsec = -u_nsec;
80444574Sphk	pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG;
80532513Sphk
80632513Sphk	/*
80750656Sphk	 * The PPS frequency is recalculated and clamped to the maximum
80850656Sphk	 * MAXFREQ. If enabled, the system clock frequency is updated as
80950656Sphk	 * well.
81032513Sphk	 */
81144574Sphk	L_ADD(pps_freq, ftemp);
81244574Sphk	u_nsec = L_GINT(pps_freq);
81344574Sphk	if (u_nsec > MAXFREQ)
81444574Sphk		L_LINT(pps_freq, MAXFREQ);
81544574Sphk	else if (u_nsec < -MAXFREQ)
81644574Sphk		L_LINT(pps_freq, -MAXFREQ);
81744574Sphk	if (time_status & STA_PPSFREQ)
81844574Sphk		time_freq = pps_freq;
81932513Sphk}
82032513Sphk#endif /* PPS_SYNC */
821