kern_ntptime.c revision 62454
1169689Skan/***********************************************************************
2169689Skan *								       *
390075Sobrien * Copyright (c) David L. Mills 1993-1999			       *
4132718Skan *								       *
590075Sobrien * Permission to use, copy, modify, and distribute this software and   *
6132718Skan * its documentation for any purpose and without fee is hereby	       *
790075Sobrien * granted, provided that the above copyright notice appears in all    *
890075Sobrien * copies and that both the copyright notice and this permission       *
990075Sobrien * notice appear in supporting documentation, and that the name	       *
1090075Sobrien * University of Delaware not be used in advertising or publicity      *
11132718Skan * pertaining to distribution of the software without specific,	       *
1290075Sobrien * written prior permission. The University of Delaware makes no       *
1390075Sobrien * representations about the suitability this software for any	       *
1490075Sobrien * purpose. It is provided "as is" without express or implied	       *
1590075Sobrien * warranty.							       *
1690075Sobrien *								       *
17132718Skan **********************************************************************/
18169689Skan
19169689Skan/*
2090075Sobrien * Adapted from the original sources for FreeBSD and timecounters by:
2190075Sobrien * Poul-Henning Kamp <phk@FreeBSD.org>.
2290075Sobrien *
2390075Sobrien * The 32bit version of the "LP" macros seems a bit past its "sell by"
2490075Sobrien * date so I have retained only the 64bit version and included it directly
2590075Sobrien * in this file.
2690075Sobrien *
2790075Sobrien * Only minor changes done to interface with the timecounters over in
2890075Sobrien * sys/kern/kern_clock.c.   Some of the comments below may be (even more)
29169689Skan * confusing and/or plain wrong in that context.
3090075Sobrien *
3190075Sobrien * $FreeBSD: head/sys/kern/kern_ntptime.c 62454 2000-07-03 09:35:31Z phk $
3290075Sobrien */
3390075Sobrien
34117395Skan#include "opt_ntp.h"
35117395Skan
36117395Skan#include <sys/param.h>
37117395Skan#include <sys/systm.h>
3890075Sobrien#include <sys/sysproto.h>
3990075Sobrien#include <sys/kernel.h>
4090075Sobrien#include <sys/proc.h>
41169689Skan#include <sys/time.h>
42242182Skan#include <sys/timex.h>
43169689Skan#include <sys/timetc.h>
44242182Skan#include <sys/timepps.h>
4590075Sobrien#include <sys/sysctl.h>
46169689Skan
47169689Skan/*
48169689Skan * Single-precision macros for 64-bit machines
49169689Skan */
50132718Skantypedef long long l_fp;
51169689Skan#define L_ADD(v, u)	((v) += (u))
5290075Sobrien#define L_SUB(v, u)	((v) -= (u))
5390075Sobrien#define L_ADDHI(v, a)	((v) += (long long)(a) << 32)
5490075Sobrien#define L_NEG(v)	((v) = -(v))
5590075Sobrien#define L_RSHIFT(v, n) \
5690075Sobrien	do { \
5790075Sobrien		if ((v) < 0) \
5890075Sobrien			(v) = -(-(v) >> (n)); \
5990075Sobrien		else \
6090075Sobrien			(v) = (v) >> (n); \
6190075Sobrien	} while (0)
6290075Sobrien#define L_MPY(v, a)	((v) *= (a))
6390075Sobrien#define L_CLR(v)	((v) = 0)
6490075Sobrien#define L_ISNEG(v)	((v) < 0)
6590075Sobrien#define L_LINT(v, a)	((v) = (long long)(a) << 32)
6690075Sobrien#define L_GINT(v)	((v) < 0 ? -(-(v) >> 32) : (v) >> 32)
6790075Sobrien
6890075Sobrien/*
6990075Sobrien * Generic NTP kernel interface
7090075Sobrien *
7190075Sobrien * These routines constitute the Network Time Protocol (NTP) interfaces
7290075Sobrien * for user and daemon application programs. The ntp_gettime() routine
7390075Sobrien * provides the time, maximum error (synch distance) and estimated error
7490075Sobrien * (dispersion) to client user application programs. The ntp_adjtime()
7590075Sobrien * routine is used by the NTP daemon to adjust the system clock to an
7690075Sobrien * externally derived time. The time offset and related variables set by
7790075Sobrien * this routine are used by other routines in this module to adjust the
7890075Sobrien * phase and frequency of the clock discipline loop which controls the
7990075Sobrien * system clock.
8090075Sobrien *
8190075Sobrien * When the kernel time is reckoned directly in nanoseconds (NTP_NANO
8290075Sobrien * defined), the time at each tick interrupt is derived directly from
8390075Sobrien * the kernel time variable. When the kernel time is reckoned in
8490075Sobrien * microseconds, (NTP_NANO undefined), the time is derived from the
8590075Sobrien * kernel time variable together with a variable representing the
8690075Sobrien * leftover nanoseconds at the last tick interrupt. In either case, the
8790075Sobrien * current nanosecond time is reckoned from these values plus an
8890075Sobrien * interpolated value derived by the clock routines in another
8990075Sobrien * architecture-specific module. The interpolation can use either a
9090075Sobrien * dedicated counter or a processor cycle counter (PCC) implemented in
9190075Sobrien * some architectures.
9290075Sobrien *
93169689Skan * Note that all routines must run at priority splclock or higher.
94169689Skan */
95169689Skan
96169689Skan/*
97169689Skan * Phase/frequency-lock loop (PLL/FLL) definitions
98169689Skan *
99169689Skan * The nanosecond clock discipline uses two variable types, time
10090075Sobrien * variables and frequency variables. Both types are represented as 64-
10190075Sobrien * bit fixed-point quantities with the decimal point between two 32-bit
10290075Sobrien * halves. On a 32-bit machine, each half is represented as a single
10390075Sobrien * word and mathematical operations are done using multiple-precision
104169689Skan * arithmetic. On a 64-bit machine, ordinary computer arithmetic is
10590075Sobrien * used.
10690075Sobrien *
10790075Sobrien * A time variable is a signed 64-bit fixed-point number in ns and
10890075Sobrien * fraction. It represents the remaining time offset to be amortized
10990075Sobrien * over succeeding tick interrupts. The maximum time offset is about
110169689Skan * 0.5 s and the resolution is about 2.3e-10 ns.
11190075Sobrien *
11290075Sobrien *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
11390075Sobrien *  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
11490075Sobrien * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
11590075Sobrien * |s s s|			 ns				   |
116169689Skan * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
11790075Sobrien * |			    fraction				   |
11890075Sobrien * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
11990075Sobrien *
12090075Sobrien * A frequency variable is a signed 64-bit fixed-point number in ns/s
12190075Sobrien * and fraction. It represents the ns and fraction to be added to the
122169689Skan * kernel time variable at each second. The maximum frequency offset is
12390075Sobrien * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s.
12490075Sobrien *
12590075Sobrien *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
12690075Sobrien *  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
12790075Sobrien * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128169689Skan * |s s s s s s s s s s s s s|	          ns/s			   |
12990075Sobrien * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
13090075Sobrien * |			    fraction				   |
13190075Sobrien * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
13290075Sobrien */
13390075Sobrien/*
134169689Skan * The following variables establish the state of the PLL/FLL and the
13590075Sobrien * residual time and frequency offset of the local clock.
13690075Sobrien */
13790075Sobrien#define SHIFT_PLL	4		/* PLL loop gain (shift) */
13890075Sobrien#define SHIFT_FLL	2		/* FLL loop gain (shift) */
13990075Sobrien
140169689Skanstatic int time_state = TIME_OK;	/* clock state */
14190075Sobrienstatic int time_status = STA_UNSYNC;	/* clock status bits */
14290075Sobrienstatic long time_constant;		/* poll interval (shift) (s) */
14390075Sobrienstatic long time_precision = 1;		/* clock precision (ns) */
14490075Sobrienstatic long time_maxerror = MAXPHASE / 1000; /* maximum error (us) */
14590075Sobrienstatic long time_esterror = MAXPHASE / 1000; /* estimated error (us) */
146169689Skanstatic long time_reftime;		/* time at last adjustment (s) */
14790075Sobrienstatic long time_tick;			/* nanoseconds per tick (ns) */
14890075Sobrienstatic l_fp time_offset;		/* time offset (ns) */
14990075Sobrienstatic l_fp time_freq;			/* frequency offset (ns/s) */
15090075Sobrienstatic l_fp time_adj;			/* resulting adjustment */
15190075Sobrien
152169689Skan#ifdef PPS_SYNC
15390075Sobrien/*
15490075Sobrien * The following variables are used when a pulse-per-second (PPS) signal
15590075Sobrien * is available and connected via a modem control lead. They establish
15690075Sobrien * the engineering parameters of the clock discipline loop when
15790075Sobrien * controlled by the PPS signal.
15890075Sobrien */
15990075Sobrien#define PPS_FAVG	2		/* min freq avg interval (s) (shift) */
160169689Skan#define PPS_FAVGDEF	7		/* default freq avg int (s) (shift) */
16190075Sobrien#define PPS_FAVGMAX	15		/* max freq avg interval (s) (shift) */
16290075Sobrien#define PPS_PAVG	4		/* phase avg interval (s) (shift) */
16390075Sobrien#define PPS_VALID	120		/* PPS signal watchdog max (s) */
16490075Sobrien#define PPS_MAXWANDER	100000		/* max PPS wander (ns/s) */
16590075Sobrien#define PPS_POPCORN	2		/* popcorn spike threshold (shift) */
166169689Skan
16790075Sobrienstatic struct timespec pps_tf[3];	/* phase median filter */
16890075Sobrienstatic l_fp pps_offset;		/* time offset (ns) */
16990075Sobrienstatic l_fp pps_freq;			/* scaled frequency offset (ns/s) */
17090075Sobrienstatic long pps_fcount;			/* frequency accumulator */
17190075Sobrienstatic long pps_jitter;			/* nominal jitter (ns) */
172169689Skanstatic long pps_stabil;			/* nominal stability (scaled ns/s) */
17390075Sobrienstatic long pps_lastsec;		/* time at last calibration (s) */
17490075Sobrienstatic int pps_valid;			/* signal watchdog counter */
17590075Sobrienstatic int pps_shift = PPS_FAVG;	/* interval duration (s) (shift) */
17690075Sobrienstatic int pps_shiftmax = PPS_FAVGDEF;	/* max interval duration (s) (shift) */
17790075Sobrienstatic int pps_intcnt;			/* wander counter */
178169689Skanstatic int pps_letgo;			/* PLL frequency hold-off */
17990075Sobrien
18090075Sobrien/*
18190075Sobrien * PPS signal quality monitors
18290075Sobrien */
18390075Sobrienstatic long pps_calcnt;			/* calibration intervals */
184169689Skanstatic long pps_jitcnt;			/* jitter limit exceeded */
18590075Sobrienstatic long pps_stbcnt;			/* stability limit exceeded */
18690075Sobrienstatic long pps_errcnt;			/* calibration errors */
18790075Sobrien#endif /* PPS_SYNC */
18890075Sobrien/*
18990075Sobrien * End of phase/frequency-lock loop (PLL/FLL) definitions
190169689Skan */
19190075Sobrien
19290075Sobrienstatic void ntp_init(void);
19390075Sobrienstatic void hardupdate(long offset);
19490075Sobrien
19590075Sobrien/*
196169689Skan * ntp_gettime() - NTP user application interface
19790075Sobrien *
19890075Sobrien * See the timex.h header file for synopsis and API description.
19990075Sobrien */
20090075Sobrienstatic int
20190075Sobrienntp_sysctl (SYSCTL_HANDLER_ARGS)
202169689Skan{
20390075Sobrien	struct ntptimeval ntv;	/* temporary structure */
20490075Sobrien	struct timespec atv;	/* nanosecond time */
20590075Sobrien
20690075Sobrien	nanotime(&atv);
20790075Sobrien	ntv.time.tv_sec = atv.tv_sec;
208169689Skan	ntv.time.tv_nsec = atv.tv_nsec;
20990075Sobrien	ntv.maxerror = time_maxerror;
21090075Sobrien	ntv.esterror = time_esterror;
21190075Sobrien	ntv.time_state = time_state;
21290075Sobrien
21390075Sobrien	/*
21490075Sobrien	 * Status word error decode. If any of these conditions occur,
21590075Sobrien	 * an error is returned, instead of the status word. Most
216169689Skan	 * applications will care only about the fact the system clock
21790075Sobrien	 * may not be trusted, not about the details.
21890075Sobrien	 *
21990075Sobrien	 * Hardware or software error
22090075Sobrien	 */
22190075Sobrien	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
222169689Skan
22390075Sobrien	/*
22490075Sobrien	 * PPS signal lost when either time or frequency synchronization
22590075Sobrien	 * requested
22690075Sobrien	 */
22790075Sobrien	    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
228169689Skan	    !(time_status & STA_PPSSIGNAL)) ||
22990075Sobrien
23090075Sobrien	/*
23190075Sobrien	 * PPS jitter exceeded when time synchronization requested
23290075Sobrien	 */
23390075Sobrien	    (time_status & STA_PPSTIME &&
234169689Skan	    time_status & STA_PPSJITTER) ||
23590075Sobrien
23690075Sobrien	/*
23790075Sobrien	 * PPS wander exceeded or calibration error when frequency
23890075Sobrien	 * synchronization requested
23990075Sobrien	 */
24090075Sobrien	    (time_status & STA_PPSFREQ &&
24190075Sobrien	    time_status & (STA_PPSWANDER | STA_PPSERROR)))
24290075Sobrien		ntv.time_state = TIME_ERROR;
24390075Sobrien	return (sysctl_handle_opaque(oidp, &ntv, sizeof ntv, req));
244169689Skan}
24590075Sobrien
24690075SobrienSYSCTL_NODE(_kern, OID_AUTO, ntp_pll, CTLFLAG_RW, 0, "");
24790075SobrienSYSCTL_PROC(_kern_ntp_pll, OID_AUTO, gettime, CTLTYPE_OPAQUE|CTLFLAG_RD,
24890075Sobrien	0, sizeof(struct ntptimeval) , ntp_sysctl, "S,ntptimeval", "");
24990075Sobrien
250169689Skan#ifdef PPS_SYNC
25190075SobrienSYSCTL_INT(_kern_ntp_pll, OID_AUTO, pps_shiftmax, CTLFLAG_RW, &pps_shiftmax, 0, "");
25290075SobrienSYSCTL_INT(_kern_ntp_pll, OID_AUTO, pps_shift, CTLFLAG_RW, &pps_shift, 0, "");
25390075Sobrien
25490075SobrienSYSCTL_OPAQUE(_kern_ntp_pll, OID_AUTO, pps_freq, CTLFLAG_RD, &pps_freq, sizeof(pps_freq), "I", "");
25590075SobrienSYSCTL_OPAQUE(_kern_ntp_pll, OID_AUTO, time_freq, CTLFLAG_RD, &time_freq, sizeof(time_freq), "I", "");
256169689SkanSYSCTL_OPAQUE(_kern_ntp_pll, OID_AUTO, pps_offset, CTLFLAG_RD, &pps_offset, sizeof(pps_offset), "I", "");
25790075Sobrien#endif
25890075Sobrien/*
25990075Sobrien * ntp_adjtime() - NTP daemon application interface
26090075Sobrien *
26190075Sobrien * See the timex.h header file for synopsis and API description.
262169689Skan */
26390075Sobrien#ifndef _SYS_SYSPROTO_H_
26490075Sobrienstruct ntp_adjtime_args {
265107590Sobrien	struct timex *tp;
266107590Sobrien};
267107590Sobrien#endif
268107590Sobrien
269107590Sobrienint
27090075Sobrienntp_adjtime(struct proc *p, struct ntp_adjtime_args *uap)
27190075Sobrien{
272169689Skan	struct timex ntv;	/* temporary structure */
27390075Sobrien	long freq;		/* frequency ns/s) */
27490075Sobrien	int modes;		/* mode bits from structure */
275107590Sobrien	int s;			/* caller priority */
276107590Sobrien	int error;
277107590Sobrien
278107590Sobrien	error = copyin((caddr_t)uap->tp, (caddr_t)&ntv, sizeof(ntv));
279107590Sobrien	if (error)
28090075Sobrien		return(error);
28190075Sobrien
282169689Skan	/*
28390075Sobrien	 * Update selected clock variables - only the superuser can
28490075Sobrien	 * change anything. Note that there is no error checking here on
28590075Sobrien	 * the assumption the superuser should know what it is doing.
28690075Sobrien	 */
28790075Sobrien	modes = ntv.modes;
288169689Skan	if (modes)
28990075Sobrien		error = suser(p);
29090075Sobrien	if (error)
29190075Sobrien		return (error);
29290075Sobrien	s = splclock();
29390075Sobrien	if (modes & MOD_FREQUENCY) {
294169689Skan		freq = (ntv.freq * 1000LL) >> 16;
29590075Sobrien		if (freq > MAXFREQ)
29690075Sobrien			L_LINT(time_freq, MAXFREQ);
29790075Sobrien		else if (freq < -MAXFREQ)
29890075Sobrien			L_LINT(time_freq, -MAXFREQ);
29990075Sobrien		else
300169689Skan			L_LINT(time_freq, freq);
30190075Sobrien
30290075Sobrien#ifdef PPS_SYNC
303107590Sobrien		pps_freq = time_freq;
304107590Sobrien#endif /* PPS_SYNC */
305107590Sobrien	}
306107590Sobrien	if (modes & MOD_MAXERROR)
307107590Sobrien		time_maxerror = ntv.maxerror;
30890075Sobrien	if (modes & MOD_ESTERROR)
30990075Sobrien		time_esterror = ntv.esterror;
310169689Skan	if (modes & MOD_STATUS) {
31190075Sobrien		time_status &= STA_RONLY;
31290075Sobrien		time_status |= ntv.status & ~STA_RONLY;
313107590Sobrien	}
314107590Sobrien	if (modes & MOD_TIMECONST) {
315107590Sobrien		if (ntv.constant < 0)
316107590Sobrien			time_constant = 0;
317107590Sobrien		else if (ntv.constant > MAXTC)
31890075Sobrien			time_constant = MAXTC;
31990075Sobrien		else
320169689Skan			time_constant = ntv.constant;
32190075Sobrien	}
32290075Sobrien#ifdef PPS_SYNC
32390075Sobrien	if (modes & MOD_PPSMAX) {
32490075Sobrien		if (ntv.shift < PPS_FAVG)
32590075Sobrien			pps_shiftmax = PPS_FAVG;
326169689Skan		else if (ntv.shift > PPS_FAVGMAX)
32790075Sobrien			pps_shiftmax = PPS_FAVGMAX;
32890075Sobrien		else
32990075Sobrien			pps_shiftmax = ntv.shift;
33090075Sobrien	}
33190075Sobrien#endif /* PPS_SYNC */
33290075Sobrien	if (modes & MOD_NANO)
33390075Sobrien		time_status |= STA_NANO;
33490075Sobrien	if (modes & MOD_MICRO)
33590075Sobrien		time_status &= ~STA_NANO;
336169689Skan	if (modes & MOD_CLKB)
33790075Sobrien		time_status |= STA_CLK;
33890075Sobrien	if (modes & MOD_CLKA)
33990075Sobrien		time_status &= ~STA_CLK;
34090075Sobrien	if (modes & MOD_OFFSET) {
34190075Sobrien		if (time_status & STA_NANO)
342169689Skan			hardupdate(ntv.offset);
34390075Sobrien		else
34490075Sobrien			hardupdate(ntv.offset * 1000);
34590075Sobrien	}
34690075Sobrien
34790075Sobrien	/*
348169689Skan	 * Retrieve all clock variables
34990075Sobrien	 */
35090075Sobrien	if (time_status & STA_NANO)
35190075Sobrien		ntv.offset = L_GINT(time_offset);
35290075Sobrien	else
35390075Sobrien		ntv.offset = L_GINT(time_offset) / 1000;
354169689Skan	ntv.freq = L_GINT((time_freq / 1000LL) << 16);
35590075Sobrien	ntv.maxerror = time_maxerror;
35690075Sobrien	ntv.esterror = time_esterror;
35790075Sobrien	ntv.status = time_status;
35890075Sobrien	ntv.constant = time_constant;
35990075Sobrien	if (time_status & STA_NANO)
360169689Skan		ntv.precision = time_precision;
36190075Sobrien	else
36290075Sobrien		ntv.precision = time_precision / 1000;
36390075Sobrien	ntv.tolerance = MAXFREQ * SCALE_PPM;
36490075Sobrien#ifdef PPS_SYNC
36590075Sobrien	ntv.shift = pps_shift;
366169689Skan	ntv.ppsfreq = L_GINT((pps_freq / 1000LL) << 16);
36790075Sobrien	if (time_status & STA_NANO)
36890075Sobrien		ntv.jitter = pps_jitter;
36990075Sobrien	else
37090075Sobrien		ntv.jitter = pps_jitter / 1000;
37190075Sobrien	ntv.stabil = pps_stabil;
372169689Skan	ntv.calcnt = pps_calcnt;
37390075Sobrien	ntv.errcnt = pps_errcnt;
37490075Sobrien	ntv.jitcnt = pps_jitcnt;
37590075Sobrien	ntv.stbcnt = pps_stbcnt;
37690075Sobrien#endif /* PPS_SYNC */
37790075Sobrien	splx(s);
378169689Skan
37990075Sobrien	error = copyout((caddr_t)&ntv, (caddr_t)uap->tp, sizeof(ntv));
38090075Sobrien	if (error)
38190075Sobrien		return (error);
38290075Sobrien
38390075Sobrien	/*
384169689Skan	 * Status word error decode. See comments in
38590075Sobrien	 * ntp_gettime() routine.
38690075Sobrien	 */
38790075Sobrien	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
38890075Sobrien	    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
38990075Sobrien	    !(time_status & STA_PPSSIGNAL)) ||
390169689Skan	    (time_status & STA_PPSTIME &&
39190075Sobrien	    time_status & STA_PPSJITTER) ||
39290075Sobrien	    (time_status & STA_PPSFREQ &&
39390075Sobrien	    time_status & (STA_PPSWANDER | STA_PPSERROR)))
39490075Sobrien		p->p_retval[0] = TIME_ERROR;
39590075Sobrien	else
396169689Skan		p->p_retval[0] = time_state;
39790075Sobrien	return (error);
39890075Sobrien}
39990075Sobrien
40090075Sobrien/*
40190075Sobrien * second_overflow() - called after ntp_tick_adjust()
402169689Skan *
40390075Sobrien * This routine is ordinarily called immediately following the above
40490075Sobrien * routine ntp_tick_adjust(). While these two routines are normally
40590075Sobrien * combined, they are separated here only for the purposes of
40690075Sobrien * simulation.
40790075Sobrien */
40890075Sobrienvoid
40990075Sobrienntp_update_second(struct timecounter *tcp)
41090075Sobrien{
411169689Skan	u_int32_t *newsec;
41290075Sobrien
41390075Sobrien	newsec = &tcp->tc_offset_sec;
41490075Sobrien	/*
41590075Sobrien	 * On rollover of the second both the nanosecond and microsecond
41690075Sobrien	 * clocks are updated and the state machine cranked as
417169689Skan	 * necessary. The phase adjustment to be used for the next
41890075Sobrien	 * second is calculated and the maximum error is increased by
41990075Sobrien	 * the tolerance.
42090075Sobrien	 */
42190075Sobrien	time_maxerror += MAXFREQ / 1000;
42290075Sobrien
423169689Skan	/*
42490075Sobrien	 * Leap second processing. If in leap-insert state at
42590075Sobrien	 * the end of the day, the system clock is set back one
42690075Sobrien	 * second; if in leap-delete state, the system clock is
42790075Sobrien	 * set ahead one second. The nano_time() routine or
42890075Sobrien	 * external clock driver will insure that reported time
429169689Skan	 * is always monotonic.
43090075Sobrien	 */
43190075Sobrien	switch (time_state) {
43290075Sobrien
43390075Sobrien		/*
43490075Sobrien		 * No warning.
435169689Skan		 */
43690075Sobrien		case TIME_OK:
43790075Sobrien		if (time_status & STA_INS)
43890075Sobrien			time_state = TIME_INS;
43990075Sobrien		else if (time_status & STA_DEL)
44090075Sobrien			time_state = TIME_DEL;
441169689Skan		break;
44290075Sobrien
44390075Sobrien		/*
44490075Sobrien		 * Insert second 23:59:60 following second
44590075Sobrien		 * 23:59:59.
44690075Sobrien		 */
447169689Skan		case TIME_INS:
44890075Sobrien		if (!(time_status & STA_INS))
44990075Sobrien			time_state = TIME_OK;
45090075Sobrien		else if ((*newsec) % 86400 == 0) {
45190075Sobrien			(*newsec)--;
45290075Sobrien			time_state = TIME_OOP;
453169689Skan		}
45490075Sobrien		break;
45590075Sobrien
45690075Sobrien		/*
45790075Sobrien		 * Delete second 23:59:59.
45890075Sobrien		 */
459169689Skan		case TIME_DEL:
46090075Sobrien		if (!(time_status & STA_DEL))
46190075Sobrien			time_state = TIME_OK;
46290075Sobrien		else if (((*newsec) + 1) % 86400 == 0) {
46390075Sobrien			(*newsec)++;
46490075Sobrien			time_state = TIME_WAIT;
465169689Skan		}
46690075Sobrien		break;
46790075Sobrien
46890075Sobrien		/*
46990075Sobrien		 * Insert second in progress.
47090075Sobrien		 */
471169689Skan		case TIME_OOP:
47290075Sobrien		time_state = TIME_WAIT;
47390075Sobrien		break;
47490075Sobrien
47590075Sobrien		/*
47690075Sobrien		 * Wait for status bits to clear.
477169689Skan		 */
47890075Sobrien		case TIME_WAIT:
47990075Sobrien		if (!(time_status & (STA_INS | STA_DEL)))
48090075Sobrien			time_state = TIME_OK;
48190075Sobrien	}
48290075Sobrien
48390075Sobrien	/*
48490075Sobrien	 * Compute the total time adjustment for the next second
485169689Skan	 * in ns. The offset is reduced by a factor depending on
48690075Sobrien	 * whether the PPS signal is operating. Note that the
48790075Sobrien	 * value is in effect scaled by the clock frequency,
48890075Sobrien	 * since the adjustment is added at each tick interrupt.
48990075Sobrien	 */
49090075Sobrien#ifdef PPS_SYNC
491169689Skan	/* XXX even if signal dies we should finish adjustment ? */
492122180Skan	if (time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL) {
493122180Skan		time_adj = pps_offset;
494122180Skan		L_RSHIFT(time_adj, pps_shift);
495122180Skan		L_SUB(pps_offset, time_adj);
496122180Skan	} else {
497117395Skan		time_adj = time_offset;
498169689Skan		L_RSHIFT(time_adj, SHIFT_PLL + time_constant);
499169689Skan		L_SUB(time_offset, time_adj);
500169689Skan	}
501169689Skan#else
502169689Skan	time_adj = time_offset;
503169689Skan	L_RSHIFT(time_adj, SHIFT_PLL + time_constant);
504169689Skan	L_SUB(time_offset, time_adj);
505169689Skan#endif /* PPS_SYNC */
506169689Skan	L_ADD(time_adj, time_freq);
507169689Skan	tcp->tc_adjustment = time_adj;
508169689Skan#ifdef PPS_SYNC
509169689Skan	if (pps_valid > 0)
510117395Skan		pps_valid--;
511117395Skan	else
512117395Skan		time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
513117395Skan		    STA_PPSWANDER | STA_PPSERROR);
514117395Skan#endif /* PPS_SYNC */
515117395Skan}
51690075Sobrien
51790075Sobrien/*
518169689Skan * ntp_init() - initialize variables and structures
51990075Sobrien *
52090075Sobrien * This routine must be called after the kernel variables hz and tick
52190075Sobrien * are set or changed and before the next tick interrupt. In this
52290075Sobrien * particular implementation, these values are assumed set elsewhere in
52390075Sobrien * the kernel. The design allows the clock frequency and tick interval
524169689Skan * to be changed while the system is running. So, this routine should
525122180Skan * probably be integrated with the code that does that.
526122180Skan */
527122180Skanstatic void
528122180Skanntp_init()
529122180Skan{
53090075Sobrien
531169689Skan	/*
53290075Sobrien	 * The following variable must be initialized any time the
53390075Sobrien	 * kernel variable hz is changed.
53490075Sobrien	 */
53590075Sobrien	time_tick = NANOSECOND / hz;
53690075Sobrien
537169689Skan	/*
538122180Skan	 * The following variables are initialized only at startup. Only
539122180Skan	 * those structures not cleared by the compiler need to be
540122180Skan	 * initialized, and these only in the simulator. In the actual
541122180Skan	 * kernel, any nonzero values here will quickly evaporate.
542122180Skan	 */
543117395Skan	L_CLR(time_offset);
544117395Skan	L_CLR(time_freq);
545169689Skan#ifdef PPS_SYNC
546169689Skan	pps_tf[0].tv_sec = pps_tf[0].tv_nsec = 0;
547169689Skan	pps_tf[1].tv_sec = pps_tf[1].tv_nsec = 0;
548169689Skan	pps_tf[2].tv_sec = pps_tf[2].tv_nsec = 0;
549169689Skan	pps_fcount = 0;
550169689Skan	L_CLR(pps_freq);
551169689Skan#endif /* PPS_SYNC */
552169689Skan}
553169689Skan
554169689SkanSYSINIT(ntpclocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, ntp_init, NULL)
555117395Skan
556117395Skan/*
557117395Skan * hardupdate() - local clock update
558117395Skan *
559117395Skan * This routine is called by ntp_adjtime() to update the local clock
560117395Skan * phase and frequency. The implementation is of an adaptive-parameter,
56190075Sobrien * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
56290075Sobrien * time and frequency offset estimates for each call. If the kernel PPS
563169689Skan * discipline code is configured (PPS_SYNC), the PPS signal itself
56490075Sobrien * determines the new time offset, instead of the calling argument.
56590075Sobrien * Presumably, calls to ntp_adjtime() occur only when the caller
56690075Sobrien * believes the local clock is valid within some bound (+-128 ms with
56790075Sobrien * NTP). If the caller's time is far different than the PPS time, an
56890075Sobrien * argument will ensue, and it's not clear who will lose.
569169689Skan *
570122180Skan * For uncompensated quartz crystal oscillators and nominal update
571122180Skan * intervals less than 256 s, operation should be in phase-lock mode,
572122180Skan * where the loop is disciplined to phase. For update intervals greater
573122180Skan * than 1024 s, operation should be in frequency-lock mode, where the
574122180Skan * loop is disciplined to frequency. Between 256 s and 1024 s, the mode
57590075Sobrien * is selected by the STA_MODE status bit.
576169689Skan */
57790075Sobrienstatic void
57890075Sobrienhardupdate(offset)
57990075Sobrien	long offset;		/* clock offset (ns) */
58090075Sobrien{
58190075Sobrien	long ltemp, mtemp;
582169689Skan	l_fp ftemp;
583122180Skan
584122180Skan	/*
585122180Skan	 * Select how the phase is to be controlled and from which
586122180Skan	 * source. If the PPS signal is present and enabled to
587122180Skan	 * discipline the time, the PPS offset is used; otherwise, the
588117395Skan	 * argument offset is used.
589117395Skan	 */
590169689Skan	if (!(time_status & STA_PLL))
591169689Skan		return;
592169689Skan	ltemp = offset;
593169689Skan	if (ltemp > MAXPHASE)
594169689Skan		ltemp = MAXPHASE;
595169689Skan	else if (ltemp < -MAXPHASE)
596169689Skan		ltemp = -MAXPHASE;
597169689Skan	if (!(time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL))
598169689Skan		L_LINT(time_offset, ltemp);
599169689Skan
600117395Skan	/*
601117395Skan	 * Select how the frequency is to be controlled and in which
602117395Skan	 * mode (PLL or FLL). If the PPS signal is present and enabled
603117395Skan	 * to discipline the frequency, the PPS frequency is used;
604117395Skan	 * otherwise, the argument offset is used to compute it.
605117395Skan	 */
60690075Sobrien	if (time_status & STA_PPSFREQ && time_status & STA_PPSSIGNAL) {
60790075Sobrien		time_reftime = time_second;
608169689Skan		return;
60990075Sobrien	}
61090075Sobrien	if (time_status & STA_FREQHOLD || time_reftime == 0)
61190075Sobrien		time_reftime = time_second;
61290075Sobrien	mtemp = time_second - time_reftime;
61390075Sobrien	L_LINT(ftemp, ltemp);
614169689Skan	L_RSHIFT(ftemp, (SHIFT_PLL + 2 + time_constant) << 1);
615122180Skan	L_MPY(ftemp, mtemp);
616122180Skan	L_ADD(time_freq, ftemp);
617122180Skan	time_status &= ~STA_MODE;
618122180Skan	if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
619122180Skan		L_LINT(ftemp, (ltemp << 4) / mtemp);
62090075Sobrien		L_RSHIFT(ftemp, SHIFT_FLL + 4);
621169689Skan		L_ADD(time_freq, ftemp);
62290075Sobrien		time_status |= STA_MODE;
62390075Sobrien	}
62490075Sobrien	time_reftime = time_second;
62590075Sobrien	if (L_GINT(time_freq) > MAXFREQ)
62690075Sobrien		L_LINT(time_freq, MAXFREQ);
62790075Sobrien	else if (L_GINT(time_freq) < -MAXFREQ)
62890075Sobrien		L_LINT(time_freq, -MAXFREQ);
62990075Sobrien}
63090075Sobrien
631169689Skan#ifdef PPS_SYNC
63290075Sobrien/*
63390075Sobrien * hardpps() - discipline CPU clock oscillator to external PPS signal
63490075Sobrien *
63590075Sobrien * This routine is called at each PPS interrupt in order to discipline
63690075Sobrien * the CPU clock oscillator to the PPS signal. It measures the PPS phase
63790075Sobrien * and leaves it in a handy spot for the hardclock() routine. It
638169689Skan * integrates successive PPS phase differences and calculates the
63990075Sobrien * frequency offset. This is used in hardclock() to discipline the CPU
64090075Sobrien * clock oscillator so that the intrinsic frequency error is cancelled
64190075Sobrien * out. The code requires the caller to capture the time and
64290075Sobrien * architecture-dependent hardware counter values in nanoseconds at the
64390075Sobrien * on-time PPS signal transition.
64490075Sobrien *
64590075Sobrien * Note that, on some Unix systems this routine runs at an interrupt
64690075Sobrien * priority level higher than the timer interrupt routine hardclock().
647169689Skan * Therefore, the variables used are distinct from the hardclock()
64890075Sobrien * variables, except for the actual time and frequency variables, which
64990075Sobrien * are determined by this routine and updated atomically.
65090075Sobrien */
65190075Sobrienvoid
65290075Sobrienhardpps(tsp, nsec)
65390075Sobrien	struct timespec *tsp;	/* time at PPS */
654169689Skan	long nsec;		/* hardware counter at PPS */
655169689Skan{
65690075Sobrien	long u_sec, u_nsec, v_nsec, w_nsec; /* temps */
65790075Sobrien	l_fp ftemp;
658169689Skan
65990075Sobrien	/*
66090075Sobrien	 * The signal is first processed by a frequency discriminator
66190075Sobrien	 * which rejects noise and input signals with frequencies
66290075Sobrien	 * outside the range 1 +-MAXFREQ PPS. If two hits occur in the
66390075Sobrien	 * same second, we ignore the later hit; if not and a hit occurs
66490075Sobrien	 * outside the range gate, keep the later hit but do not
66590075Sobrien	 * process it.
66690075Sobrien	 */
667169689Skan	time_status |= STA_PPSSIGNAL | STA_PPSJITTER;
66890075Sobrien	time_status &= ~(STA_PPSWANDER | STA_PPSERROR);
66990075Sobrien	pps_valid = PPS_VALID;
67090075Sobrien	u_sec = tsp->tv_sec;
67190075Sobrien	u_nsec = tsp->tv_nsec;
67290075Sobrien	if (u_nsec >= (NANOSECOND >> 1)) {
67390075Sobrien		u_nsec -= NANOSECOND;
67490075Sobrien		u_sec++;
675169689Skan	}
67690075Sobrien	v_nsec = u_nsec - pps_tf[0].tv_nsec;
67790075Sobrien	if (u_sec == pps_tf[0].tv_sec && v_nsec < -MAXFREQ) {
67890075Sobrien		return;
67990075Sobrien	}
68090075Sobrien	pps_tf[2] = pps_tf[1];
68190075Sobrien	pps_tf[1] = pps_tf[0];
68290075Sobrien	pps_tf[0].tv_sec = u_sec;
68390075Sobrien	pps_tf[0].tv_nsec = u_nsec;
684169689Skan
68590075Sobrien	/*
68690075Sobrien	 * Compute the difference between the current and previous
687169689Skan	 * counter values. If the difference exceeds 0.5 s, assume it
68890075Sobrien	 * has wrapped around, so correct 1.0 s. If the result exceeds
68990075Sobrien	 * the tick interval, the sample point has crossed a tick
69090075Sobrien	 * boundary during the last second, so correct the tick. Very
69190075Sobrien	 * intricate.
692169689Skan	 */
69390075Sobrien	u_nsec = nsec;
69490075Sobrien	if (u_nsec > (NANOSECOND >> 1))
695169689Skan		u_nsec -= NANOSECOND;
69690075Sobrien	else if (u_nsec < -(NANOSECOND >> 1))
69790075Sobrien		u_nsec += NANOSECOND;
69890075Sobrien	pps_fcount += u_nsec;
69990075Sobrien	if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ) {
70090075Sobrien		return;
70190075Sobrien	}
702169689Skan	time_status &= ~STA_PPSJITTER;
70390075Sobrien
70490075Sobrien	/*
70590075Sobrien	 * A three-stage median filter is used to help denoise the PPS
70690075Sobrien	 * time. The median sample becomes the time offset estimate; the
70790075Sobrien	 * difference between the other two samples becomes the time
70890075Sobrien	 * dispersion (jitter) estimate.
709117395Skan	 */
71090075Sobrien	if (pps_tf[0].tv_nsec > pps_tf[1].tv_nsec) {
71190075Sobrien		if (pps_tf[1].tv_nsec > pps_tf[2].tv_nsec) {
71290075Sobrien			v_nsec = pps_tf[1].tv_nsec;	/* 0 1 2 */
713169689Skan			u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec;
71490075Sobrien		} else if (pps_tf[2].tv_nsec > pps_tf[0].tv_nsec) {
71590075Sobrien			v_nsec = pps_tf[0].tv_nsec;	/* 2 0 1 */
71690075Sobrien			u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec;
717169689Skan		} else {
71890075Sobrien			v_nsec = pps_tf[2].tv_nsec;	/* 0 2 1 */
71990075Sobrien			u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec;
72090075Sobrien		}
72190075Sobrien	} else {
722169689Skan		if (pps_tf[1].tv_nsec < pps_tf[2].tv_nsec) {
72390075Sobrien			v_nsec = pps_tf[1].tv_nsec;	/* 2 1 0 */
72490075Sobrien			u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec;
72590075Sobrien		} else  if (pps_tf[2].tv_nsec < pps_tf[0].tv_nsec) {
72690075Sobrien			v_nsec = pps_tf[0].tv_nsec;	/* 1 0 2 */
72790075Sobrien			u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec;
72890075Sobrien		} else {
72990075Sobrien			v_nsec = pps_tf[2].tv_nsec;	/* 1 2 0 */
73090075Sobrien			u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec;
73190075Sobrien		}
73290075Sobrien	}
73390075Sobrien
734169689Skan	/*
73590075Sobrien	 * Nominal jitter is due to PPS signal noise and  interrupt
73690075Sobrien	 * latency. If it exceeds the popcorn threshold,
73790075Sobrien	 * the sample is discarded. otherwise, if so enabled, the time
73890075Sobrien	 * offset is updated. We can tolerate a modest loss of data here
73990075Sobrien	 * without degrading time accuracy.
74090075Sobrien	 */
741169689Skan	if (u_nsec > (pps_jitter << PPS_POPCORN)) {
74290075Sobrien		time_status |= STA_PPSJITTER;
74390075Sobrien		pps_jitcnt++;
74490075Sobrien	} else if (time_status & STA_PPSTIME) {
74590075Sobrien		L_LINT(time_offset, -v_nsec);
74690075Sobrien		L_LINT(pps_offset, -v_nsec);
74790075Sobrien
74890075Sobrien		if (pps_letgo >= 2) {
749169689Skan			L_LINT(ftemp, -v_nsec);
750117395Skan			L_RSHIFT(ftemp, (pps_shift * 2));
75190075Sobrien			L_ADD(ftemp, time_freq);
75290075Sobrien			w_nsec = L_GINT(ftemp);
75390075Sobrien			if (w_nsec > MAXFREQ)
75490075Sobrien				L_LINT(ftemp, MAXFREQ);
75590075Sobrien			else if (w_nsec < -MAXFREQ)
756169689Skan				L_LINT(ftemp, -MAXFREQ);
75790075Sobrien			time_freq = ftemp;
75890075Sobrien		}
75990075Sobrien
76090075Sobrien	}
76190075Sobrien	pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG;
76290075Sobrien	u_sec = pps_tf[0].tv_sec - pps_lastsec;
763169689Skan	if (u_sec < (1 << pps_shift))
76490075Sobrien		return;
76590075Sobrien
76690075Sobrien	/*
76790075Sobrien	 * At the end of the calibration interval the difference between
76890075Sobrien	 * the first and last counter values becomes the scaled
76990075Sobrien	 * frequency. It will later be divided by the length of the
770169689Skan	 * interval to determine the frequency update. If the frequency
77190075Sobrien	 * exceeds a sanity threshold, or if the actual calibration
77290075Sobrien	 * interval is not equal to the expected length, the data are
77390075Sobrien	 * discarded. We can tolerate a modest loss of data here without
77490075Sobrien	 * degrading frequency ccuracy.
77590075Sobrien	 */
77690075Sobrien	pps_calcnt++;
77790075Sobrien	v_nsec = -pps_fcount;
778169689Skan	pps_lastsec = pps_tf[0].tv_sec;
779117395Skan	pps_fcount = 0;
78090075Sobrien	u_nsec = MAXFREQ << pps_shift;
78190075Sobrien	if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 <<
78290075Sobrien	    pps_shift)) {
78390075Sobrien		time_status |= STA_PPSERROR;
78490075Sobrien		pps_errcnt++;
785169689Skan		return;
78690075Sobrien	}
78790075Sobrien
78890075Sobrien	/*
78990075Sobrien	 * Here the raw frequency offset and wander (stability) is
79090075Sobrien	 * calculated. If the wander is less than the wander threshold
79190075Sobrien	 * for four consecutive averaging intervals, the interval is
792169689Skan	 * doubled; if it is greater than the threshold for four
79390075Sobrien	 * consecutive intervals, the interval is halved. The scaled
79490075Sobrien	 * frequency offset is converted to frequency offset. The
79590075Sobrien	 * stability metric is calculated as the average of recent
79690075Sobrien	 * frequency changes, but is used only for performance
79790075Sobrien	 * monitoring.
79890075Sobrien	 */
799169689Skan	L_LINT(ftemp, v_nsec);
80090075Sobrien	L_RSHIFT(ftemp, pps_shift);
80190075Sobrien	L_SUB(ftemp, pps_freq);
80290075Sobrien	u_nsec = L_GINT(ftemp);
80390075Sobrien	if (u_nsec > PPS_MAXWANDER) {
80490075Sobrien		L_LINT(ftemp, PPS_MAXWANDER);
80590075Sobrien		pps_intcnt--;
806169689Skan		time_status |= STA_PPSWANDER;
80790075Sobrien		pps_stbcnt++;
80890075Sobrien	} else if (u_nsec < -PPS_MAXWANDER) {
80990075Sobrien		L_LINT(ftemp, -PPS_MAXWANDER);
81090075Sobrien		pps_intcnt--;
81190075Sobrien		time_status |= STA_PPSWANDER;
812169689Skan		pps_stbcnt++;
81390075Sobrien	} else {
81490075Sobrien		pps_intcnt++;
81590075Sobrien	}
81690075Sobrien	if (!(time_status & STA_PPSFREQ)) {
81790075Sobrien		pps_intcnt = 0;
818169689Skan		pps_shift = PPS_FAVG;
81990075Sobrien	} else if (pps_shift > pps_shiftmax) {
82090075Sobrien		/* If we lowered pps_shiftmax */
82190075Sobrien		pps_shift = pps_shiftmax;
82290075Sobrien		pps_intcnt = 0;
82390075Sobrien	} else if (pps_intcnt >= 4) {
824169689Skan		pps_intcnt = 4;
82590075Sobrien		if (pps_shift < pps_shiftmax) {
82690075Sobrien			pps_shift++;
82790075Sobrien			pps_intcnt = 0;
82890075Sobrien		}
82990075Sobrien	} else if (pps_intcnt <= -4) {
83090075Sobrien		pps_intcnt = -4;
831169689Skan		if (pps_shift > PPS_FAVG) {
83290075Sobrien			pps_shift--;
83390075Sobrien			pps_intcnt = 0;
83490075Sobrien		}
83590075Sobrien	}
83690075Sobrien	if (u_nsec < 0)
83790075Sobrien		u_nsec = -u_nsec;
838169689Skan	pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG;
83990075Sobrien
84090075Sobrien	/*
84190075Sobrien	 * The PPS frequency is recalculated and clamped to the maximum
84290075Sobrien	 * MAXFREQ. If enabled, the system clock frequency is updated as
84390075Sobrien	 * well.
844169689Skan	 */
84590075Sobrien	L_ADD(pps_freq, ftemp);
84690075Sobrien	u_nsec = L_GINT(pps_freq);
84790075Sobrien	if (u_nsec > MAXFREQ)
84890075Sobrien		L_LINT(pps_freq, MAXFREQ);
84990075Sobrien	else if (u_nsec < -MAXFREQ)
850169689Skan		L_LINT(pps_freq, -MAXFREQ);
85190075Sobrien	if ((time_status & (STA_PPSFREQ | STA_PPSTIME)) == STA_PPSFREQ) {
85290075Sobrien		pps_letgo = 0;
85390075Sobrien		time_freq = pps_freq;
85490075Sobrien	} else if (time_status & STA_PPSTIME) {
85590075Sobrien		if (pps_letgo < 2)
856169689Skan			pps_letgo++;
85790075Sobrien	}
85890075Sobrien}
85990075Sobrien#endif /* PPS_SYNC */
86090075Sobrien