kern_ntptime.c revision 50663
1/***********************************************************************
2 *								       *
3 * Copyright (c) David L. Mills 1993-1999			       *
4 *								       *
5 * Permission to use, copy, modify, and distribute this software and   *
6 * its documentation for any purpose and without fee is hereby	       *
7 * granted, provided that the above copyright notice appears in all    *
8 * copies and that both the copyright notice and this permission       *
9 * notice appear in supporting documentation, and that the name	       *
10 * University of Delaware not be used in advertising or publicity      *
11 * pertaining to distribution of the software without specific,	       *
12 * written prior permission. The University of Delaware makes no       *
13 * representations about the suitability this software for any	       *
14 * purpose. It is provided "as is" without express or implied	       *
15 * warranty.							       *
16 *								       *
17 **********************************************************************/
18
19/*
20 * Adapted from the original sources for FreeBSD and timecounters by:
21 * Poul-Henning Kamp <phk@FreeBSD.org>.
22 *
23 * The 32bit version of the "LP" macros seems a bit past its "sell by"
24 * date so I have retained only the 64bit version and included it directly
25 * in this file.
26 *
27 * Only minor changes done to interface with the timecounters over in
28 * sys/kern/kern_clock.c.   Some of the comments below may be (even more)
29 * confusing and/or plain wrong in that context.
30 */
31
32#include "opt_ntp.h"
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/sysproto.h>
37#include <sys/kernel.h>
38#include <sys/proc.h>
39#include <sys/time.h>
40#include <sys/timex.h>
41#include <sys/timepps.h>
42#include <sys/sysctl.h>
43
44/*
45 * Single-precision macros for 64-bit machines
46 */
47typedef long long l_fp;
48#define L_ADD(v, u)	((v) += (u))
49#define L_SUB(v, u)	((v) -= (u))
50#define L_ADDHI(v, a)	((v) += (long long)(a) << 32)
51#define L_NEG(v)	((v) = -(v))
52#define L_RSHIFT(v, n) \
53	do { \
54		if ((v) < 0) \
55			(v) = -(-(v) >> (n)); \
56		else \
57			(v) = (v) >> (n); \
58	} while (0)
59#define L_MPY(v, a)	((v) *= (a))
60#define L_CLR(v)	((v) = 0)
61#define L_ISNEG(v)	((v) < 0)
62#define L_LINT(v, a)	((v) = (long long)(a) << 32)
63#define L_GINT(v)	((v) < 0 ? -(-(v) >> 32) : (v) >> 32)
64
65/*
66 * Generic NTP kernel interface
67 *
68 * These routines constitute the Network Time Protocol (NTP) interfaces
69 * for user and daemon application programs. The ntp_gettime() routine
70 * provides the time, maximum error (synch distance) and estimated error
71 * (dispersion) to client user application programs. The ntp_adjtime()
72 * routine is used by the NTP daemon to adjust the system clock to an
73 * externally derived time. The time offset and related variables set by
74 * this routine are used by other routines in this module to adjust the
75 * phase and frequency of the clock discipline loop which controls the
76 * system clock.
77 *
78 * When the kernel time is reckoned directly in nanoseconds (NTP_NANO
79 * defined), the time at each tick interrupt is derived directly from
80 * the kernel time variable. When the kernel time is reckoned in
81 * microseconds, (NTP_NANO undefined), the time is derived from the
82 * kernel time variable together with a variable representing the
83 * leftover nanoseconds at the last tick interrupt. In either case, the
84 * current nanosecond time is reckoned from these values plus an
85 * interpolated value derived by the clock routines in another
86 * architecture-specific module. The interpolation can use either a
87 * dedicated counter or a processor cycle counter (PCC) implemented in
88 * some architectures.
89 *
90 * Note that all routines must run at priority splclock or higher.
91 */
92
93/*
94 * Phase/frequency-lock loop (PLL/FLL) definitions
95 *
96 * The nanosecond clock discipline uses two variable types, time
97 * variables and frequency variables. Both types are represented as 64-
98 * bit fixed-point quantities with the decimal point between two 32-bit
99 * halves. On a 32-bit machine, each half is represented as a single
100 * word and mathematical operations are done using multiple-precision
101 * arithmetic. On a 64-bit machine, ordinary computer arithmetic is
102 * used.
103 *
104 * A time variable is a signed 64-bit fixed-point number in ns and
105 * fraction. It represents the remaining time offset to be amortized
106 * over succeeding tick interrupts. The maximum time offset is about
107 * 0.5 s and the resolution is about 2.3e-10 ns.
108 *
109 *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
110 *  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
111 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 * |s s s|			 ns				   |
113 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 * |			    fraction				   |
115 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 *
117 * A frequency variable is a signed 64-bit fixed-point number in ns/s
118 * and fraction. It represents the ns and fraction to be added to the
119 * kernel time variable at each second. The maximum frequency offset is
120 * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s.
121 *
122 *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
123 *  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
124 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
125 * |s s s s s s s s s s s s s|	          ns/s			   |
126 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
127 * |			    fraction				   |
128 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
129 */
130/*
131 * The following variables establish the state of the PLL/FLL and the
132 * residual time and frequency offset of the local clock.
133 */
134#define SHIFT_PLL	4		/* PLL loop gain (shift) */
135#define SHIFT_FLL	2		/* FLL loop gain (shift) */
136
137static int time_state = TIME_OK;	/* clock state */
138static int time_status = STA_UNSYNC;	/* clock status bits */
139static long time_constant;		/* poll interval (shift) (s) */
140static long time_precision = 1;		/* clock precision (ns) */
141static long time_maxerror = MAXPHASE / 1000; /* maximum error (us) */
142static long time_esterror = MAXPHASE / 1000; /* estimated error (us) */
143static long time_reftime;		/* time at last adjustment (s) */
144static long time_tick;			/* nanoseconds per tick (ns) */
145static l_fp time_offset;		/* time offset (ns) */
146static l_fp time_freq;			/* frequency offset (ns/s) */
147
148#ifdef PPS_SYNC
149/*
150 * The following variables are used when a pulse-per-second (PPS) signal
151 * is available and connected via a modem control lead. They establish
152 * the engineering parameters of the clock discipline loop when
153 * controlled by the PPS signal.
154 */
155#define PPS_FAVG	2		/* min freq avg interval (s) (shift) */
156#define PPS_FAVGDEF	7		/* default freq avg int (s) (shift) */
157#define PPS_FAVGMAX	15		/* max freq avg interval (s) (shift) */
158#define PPS_PAVG	4		/* phase avg interval (s) (shift) */
159#define PPS_VALID	120		/* PPS signal watchdog max (s) */
160#define PPS_MAXWANDER	100000		/* max PPS wander (ns/s) */
161#define PPS_POPCORN	2		/* popcorn spike threshold (shift) */
162
163static struct timespec pps_tf[3];	/* phase median filter */
164static l_fp pps_freq;			/* scaled frequency offset (ns/s) */
165static long pps_fcount;			/* frequency accumulator */
166static long pps_jitter;			/* nominal jitter (ns) */
167static long pps_stabil;			/* nominal stability (scaled ns/s) */
168static long pps_lastsec;		/* time at last calibration (s) */
169static int pps_valid;			/* signal watchdog counter */
170static int pps_shift = PPS_FAVG;	/* interval duration (s) (shift) */
171static int pps_shiftmax = PPS_FAVGDEF;	/* max interval duration (s) (shift) */
172static int pps_intcnt;			/* wander counter */
173
174/*
175 * PPS signal quality monitors
176 */
177static long pps_calcnt;			/* calibration intervals */
178static long pps_jitcnt;			/* jitter limit exceeded */
179static long pps_stbcnt;			/* stability limit exceeded */
180static long pps_errcnt;			/* calibration errors */
181#endif /* PPS_SYNC */
182/*
183 * End of phase/frequency-lock loop (PLL/FLL) definitions
184 */
185
186static void ntp_init(void);
187static void hardupdate(long offset);
188
189/*
190 * ntp_gettime() - NTP user application interface
191 *
192 * See the timex.h header file for synopsis and API description.
193 */
194static int
195ntp_sysctl SYSCTL_HANDLER_ARGS
196{
197	struct ntptimeval ntv;	/* temporary structure */
198	struct timespec atv;	/* nanosecond time */
199
200	nanotime(&atv);
201	ntv.time.tv_sec = atv.tv_sec;
202	ntv.time.tv_nsec = atv.tv_nsec;
203	ntv.maxerror = time_maxerror;
204	ntv.esterror = time_esterror;
205	ntv.time_state = time_state;
206
207	/*
208	 * Status word error decode. If any of these conditions occur,
209	 * an error is returned, instead of the status word. Most
210	 * applications will care only about the fact the system clock
211	 * may not be trusted, not about the details.
212	 *
213	 * Hardware or software error
214	 */
215	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
216
217	/*
218	 * PPS signal lost when either time or frequency synchronization
219	 * requested
220	 */
221	    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
222	    !(time_status & STA_PPSSIGNAL)) ||
223
224	/*
225	 * PPS jitter exceeded when time synchronization requested
226	 */
227	    (time_status & STA_PPSTIME &&
228	    time_status & STA_PPSJITTER) ||
229
230	/*
231	 * PPS wander exceeded or calibration error when frequency
232	 * synchronization requested
233	 */
234	    (time_status & STA_PPSFREQ &&
235	    time_status & (STA_PPSWANDER | STA_PPSERROR)))
236		ntv.time_state = TIME_ERROR;
237	return (sysctl_handle_opaque(oidp, &ntv, sizeof ntv, req));
238}
239
240SYSCTL_NODE(_kern, OID_AUTO, ntp_pll, CTLFLAG_RW, 0, "");
241SYSCTL_PROC(_kern_ntp_pll, OID_AUTO, gettime, CTLTYPE_OPAQUE|CTLFLAG_RD,
242	0, sizeof(struct ntptimeval) , ntp_sysctl, "S,ntptimeval", "");
243
244#ifdef PPS_SYNC
245SYSCTL_INT(_kern_ntp_pll, OID_AUTO, pps_shiftmax, CTLFLAG_RW, &pps_shiftmax, 0, "");
246#endif
247/*
248 * ntp_adjtime() - NTP daemon application interface
249 *
250 * See the timex.h header file for synopsis and API description.
251 */
252#ifndef _SYS_SYSPROTO_H_
253struct ntp_adjtime_args {
254	struct timex *tp;
255};
256#endif
257
258int
259ntp_adjtime(struct proc *p, struct ntp_adjtime_args *uap)
260{
261	struct timex ntv;	/* temporary structure */
262	long freq;		/* frequency ns/s) */
263	int modes;		/* mode bits from structure */
264	int s;			/* caller priority */
265	int error;
266
267	error = copyin((caddr_t)uap->tp, (caddr_t)&ntv, sizeof(ntv));
268	if (error)
269		return(error);
270
271	/*
272	 * Update selected clock variables - only the superuser can
273	 * change anything. Note that there is no error checking here on
274	 * the assumption the superuser should know what it is doing.
275	 */
276	modes = ntv.modes;
277	if (modes)
278		error = suser(p);
279	if (error)
280		return (error);
281	s = splclock();
282	if (modes & MOD_FREQUENCY) {
283		freq = (ntv.freq * 1000LL) >> 16;
284		if (freq > MAXFREQ)
285			L_LINT(time_freq, MAXFREQ);
286		else if (freq < -MAXFREQ)
287			L_LINT(time_freq, -MAXFREQ);
288		else
289			L_LINT(time_freq, freq);
290
291#ifdef PPS_SYNC
292		pps_freq = time_freq;
293#endif /* PPS_SYNC */
294	}
295	if (modes & MOD_MAXERROR)
296		time_maxerror = ntv.maxerror;
297	if (modes & MOD_ESTERROR)
298		time_esterror = ntv.esterror;
299	if (modes & MOD_STATUS) {
300		time_status &= STA_RONLY;
301		time_status |= ntv.status & ~STA_RONLY;
302	}
303	if (modes & MOD_TIMECONST) {
304		if (ntv.constant < 0)
305			time_constant = 0;
306		else if (ntv.constant > MAXTC)
307			time_constant = MAXTC;
308		else
309			time_constant = ntv.constant;
310	}
311#ifdef PPS_SYNC
312	if (modes & MOD_PPSMAX) {
313		if (ntv.shift < PPS_FAVG)
314			pps_shiftmax = PPS_FAVG;
315		else if (ntv.shift > PPS_FAVGMAX)
316			pps_shiftmax = PPS_FAVGMAX;
317		else
318			pps_shiftmax = ntv.shift;
319	}
320#endif /* PPS_SYNC */
321	if (modes & MOD_NANO)
322		time_status |= STA_NANO;
323	if (modes & MOD_MICRO)
324		time_status &= ~STA_NANO;
325	if (modes & MOD_CLKB)
326		time_status |= STA_CLK;
327	if (modes & MOD_CLKA)
328		time_status &= ~STA_CLK;
329	if (modes & MOD_OFFSET) {
330		if (time_status & STA_NANO)
331			hardupdate(ntv.offset);
332		else
333			hardupdate(ntv.offset * 1000);
334	}
335
336	/*
337	 * Retrieve all clock variables
338	 */
339	if (time_status & STA_NANO)
340		ntv.offset = L_GINT(time_offset);
341	else
342		ntv.offset = L_GINT(time_offset) / 1000;
343	ntv.freq = L_GINT((time_freq / 1000LL) << 16);
344	ntv.maxerror = time_maxerror;
345	ntv.esterror = time_esterror;
346	ntv.status = time_status;
347	ntv.constant = time_constant;
348	if (time_status & STA_NANO)
349		ntv.precision = time_precision;
350	else
351		ntv.precision = time_precision / 1000;
352	ntv.tolerance = MAXFREQ * SCALE_PPM;
353#ifdef PPS_SYNC
354	ntv.shift = pps_shift;
355	ntv.ppsfreq = L_GINT((pps_freq / 1000LL) << 16);
356	if (time_status & STA_NANO)
357		ntv.jitter = pps_jitter;
358	else
359		ntv.jitter = pps_jitter / 1000;
360	ntv.stabil = pps_stabil;
361	ntv.calcnt = pps_calcnt;
362	ntv.errcnt = pps_errcnt;
363	ntv.jitcnt = pps_jitcnt;
364	ntv.stbcnt = pps_stbcnt;
365#endif /* PPS_SYNC */
366	splx(s);
367
368	error = copyout((caddr_t)&ntv, (caddr_t)uap->tp, sizeof(ntv));
369	if (error)
370		return (error);
371
372	/*
373	 * Status word error decode. See comments in
374	 * ntp_gettime() routine.
375	 */
376	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
377	    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
378	    !(time_status & STA_PPSSIGNAL)) ||
379	    (time_status & STA_PPSTIME &&
380	    time_status & STA_PPSJITTER) ||
381	    (time_status & STA_PPSFREQ &&
382	    time_status & (STA_PPSWANDER | STA_PPSERROR)))
383		p->p_retval[0] = TIME_ERROR;
384	else
385		p->p_retval[0] = time_state;
386	return (error);
387}
388
389/*
390 * second_overflow() - called after ntp_tick_adjust()
391 *
392 * This routine is ordinarily called immediately following the above
393 * routine ntp_tick_adjust(). While these two routines are normally
394 * combined, they are separated here only for the purposes of
395 * simulation.
396 */
397void
398ntp_update_second(struct timecounter *tcp)
399{
400	u_int32_t *newsec;
401	l_fp ftemp, time_adj;		/* 32/64-bit temporaries */
402
403	newsec = &tcp->tc_offset_sec;
404	/*
405	 * On rollover of the second both the nanosecond and microsecond
406	 * clocks are updated and the state machine cranked as
407	 * necessary. The phase adjustment to be used for the next
408	 * second is calculated and the maximum error is increased by
409	 * the tolerance.
410	 */
411	time_maxerror += MAXFREQ / 1000;
412
413	/*
414	 * Leap second processing. If in leap-insert state at
415	 * the end of the day, the system clock is set back one
416	 * second; if in leap-delete state, the system clock is
417	 * set ahead one second. The nano_time() routine or
418	 * external clock driver will insure that reported time
419	 * is always monotonic.
420	 */
421	switch (time_state) {
422
423		/*
424		 * No warning.
425		 */
426		case TIME_OK:
427		if (time_status & STA_INS)
428			time_state = TIME_INS;
429		else if (time_status & STA_DEL)
430			time_state = TIME_DEL;
431		break;
432
433		/*
434		 * Insert second 23:59:60 following second
435		 * 23:59:59.
436		 */
437		case TIME_INS:
438		if (!(time_status & STA_INS))
439			time_state = TIME_OK;
440		else if ((*newsec) % 86400 == 0) {
441			(*newsec)--;
442			time_state = TIME_OOP;
443		}
444		break;
445
446		/*
447		 * Delete second 23:59:59.
448		 */
449		case TIME_DEL:
450		if (!(time_status & STA_DEL))
451			time_state = TIME_OK;
452		else if (((*newsec) + 1) % 86400 == 0) {
453			(*newsec)++;
454			time_state = TIME_WAIT;
455		}
456		break;
457
458		/*
459		 * Insert second in progress.
460		 */
461		case TIME_OOP:
462		time_state = TIME_WAIT;
463		break;
464
465		/*
466		 * Wait for status bits to clear.
467		 */
468		case TIME_WAIT:
469		if (!(time_status & (STA_INS | STA_DEL)))
470			time_state = TIME_OK;
471	}
472
473	/*
474	 * Compute the total time adjustment for the next second
475	 * in ns. The offset is reduced by a factor depending on
476	 * whether the PPS signal is operating. Note that the
477	 * value is in effect scaled by the clock frequency,
478	 * since the adjustment is added at each tick interrupt.
479	 */
480	ftemp = time_offset;
481#ifdef PPS_SYNC
482	if (time_status & STA_PPSTIME && time_status &
483	    STA_PPSSIGNAL)
484		L_RSHIFT(ftemp, PPS_FAVG);
485	else
486		L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
487#else
488		L_RSHIFT(ftemp, SHIFT_PLL + time_constant);
489#endif /* PPS_SYNC */
490	time_adj = ftemp;
491	L_SUB(time_offset, ftemp);
492	L_ADD(time_adj, time_freq);
493	tcp->tc_adjustment = time_adj;
494#ifdef PPS_SYNC
495	if (pps_valid > 0)
496		pps_valid--;
497	else
498		time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
499		    STA_PPSWANDER | STA_PPSERROR);
500#endif /* PPS_SYNC */
501}
502
503/*
504 * ntp_init() - initialize variables and structures
505 *
506 * This routine must be called after the kernel variables hz and tick
507 * are set or changed and before the next tick interrupt. In this
508 * particular implementation, these values are assumed set elsewhere in
509 * the kernel. The design allows the clock frequency and tick interval
510 * to be changed while the system is running. So, this routine should
511 * probably be integrated with the code that does that.
512 */
513static void
514ntp_init()
515{
516
517	/*
518	 * The following variable must be initialized any time the
519	 * kernel variable hz is changed.
520	 */
521	time_tick = NANOSECOND / hz;
522
523	/*
524	 * The following variables are initialized only at startup. Only
525	 * those structures not cleared by the compiler need to be
526	 * initialized, and these only in the simulator. In the actual
527	 * kernel, any nonzero values here will quickly evaporate.
528	 */
529	L_CLR(time_offset);
530	L_CLR(time_freq);
531#ifdef PPS_SYNC
532	pps_tf[0].tv_sec = pps_tf[0].tv_nsec = 0;
533	pps_tf[1].tv_sec = pps_tf[1].tv_nsec = 0;
534	pps_tf[2].tv_sec = pps_tf[2].tv_nsec = 0;
535	pps_fcount = 0;
536	L_CLR(pps_freq);
537#endif /* PPS_SYNC */
538}
539
540SYSINIT(ntpclocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, ntp_init, NULL)
541
542/*
543 * hardupdate() - local clock update
544 *
545 * This routine is called by ntp_adjtime() to update the local clock
546 * phase and frequency. The implementation is of an adaptive-parameter,
547 * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
548 * time and frequency offset estimates for each call. If the kernel PPS
549 * discipline code is configured (PPS_SYNC), the PPS signal itself
550 * determines the new time offset, instead of the calling argument.
551 * Presumably, calls to ntp_adjtime() occur only when the caller
552 * believes the local clock is valid within some bound (+-128 ms with
553 * NTP). If the caller's time is far different than the PPS time, an
554 * argument will ensue, and it's not clear who will lose.
555 *
556 * For uncompensated quartz crystal oscillators and nominal update
557 * intervals less than 256 s, operation should be in phase-lock mode,
558 * where the loop is disciplined to phase. For update intervals greater
559 * than 1024 s, operation should be in frequency-lock mode, where the
560 * loop is disciplined to frequency. Between 256 s and 1024 s, the mode
561 * is selected by the STA_MODE status bit.
562 */
563static void
564hardupdate(offset)
565	long offset;		/* clock offset (ns) */
566{
567	long ltemp, mtemp;
568	l_fp ftemp;
569
570	/*
571	 * Select how the phase is to be controlled and from which
572	 * source. If the PPS signal is present and enabled to
573	 * discipline the time, the PPS offset is used; otherwise, the
574	 * argument offset is used.
575	 */
576	if (!(time_status & STA_PLL))
577		return;
578	ltemp = offset;
579	if (ltemp > MAXPHASE)
580		ltemp = MAXPHASE;
581	else if (ltemp < -MAXPHASE)
582		ltemp = -MAXPHASE;
583	if (!(time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL))
584		L_LINT(time_offset, ltemp);
585
586	/*
587	 * Select how the frequency is to be controlled and in which
588	 * mode (PLL or FLL). If the PPS signal is present and enabled
589	 * to discipline the frequency, the PPS frequency is used;
590	 * otherwise, the argument offset is used to compute it.
591	 */
592	if (time_status & STA_PPSFREQ && time_status & STA_PPSSIGNAL) {
593		time_reftime = time_second;
594		return;
595	}
596	if (time_status & STA_FREQHOLD || time_reftime == 0)
597		time_reftime = time_second;
598	mtemp = time_second - time_reftime;
599	L_LINT(ftemp, ltemp);
600	L_RSHIFT(ftemp, (SHIFT_PLL + 2 + time_constant) << 1);
601	L_MPY(ftemp, mtemp);
602	L_ADD(time_freq, ftemp);
603	time_status &= ~STA_MODE;
604	if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
605		L_LINT(ftemp, (ltemp << 4) / mtemp);
606		L_RSHIFT(ftemp, SHIFT_FLL + 4);
607		L_ADD(time_freq, ftemp);
608		time_status |= STA_MODE;
609	}
610	time_reftime = time_second;
611	if (L_GINT(time_freq) > MAXFREQ)
612		L_LINT(time_freq, MAXFREQ);
613	else if (L_GINT(time_freq) < -MAXFREQ)
614		L_LINT(time_freq, -MAXFREQ);
615}
616
617#ifdef PPS_SYNC
618/*
619 * hardpps() - discipline CPU clock oscillator to external PPS signal
620 *
621 * This routine is called at each PPS interrupt in order to discipline
622 * the CPU clock oscillator to the PPS signal. It measures the PPS phase
623 * and leaves it in a handy spot for the hardclock() routine. It
624 * integrates successive PPS phase differences and calculates the
625 * frequency offset. This is used in hardclock() to discipline the CPU
626 * clock oscillator so that the intrinsic frequency error is cancelled
627 * out. The code requires the caller to capture the time and
628 * architecture-dependent hardware counter values in nanoseconds at the
629 * on-time PPS signal transition.
630 *
631 * Note that, on some Unix systems this routine runs at an interrupt
632 * priority level higher than the timer interrupt routine hardclock().
633 * Therefore, the variables used are distinct from the hardclock()
634 * variables, except for the actual time and frequency variables, which
635 * are determined by this routine and updated atomically.
636 */
637void
638hardpps(tsp, nsec)
639	struct timespec *tsp;	/* time at PPS */
640	long nsec;		/* hardware counter at PPS */
641{
642	long u_sec, u_nsec, v_nsec; /* temps */
643	l_fp ftemp;
644
645	/*
646	 * The signal is first processed by a frequency discriminator
647	 * which rejects noise and input signals with frequencies
648	 * outside the range 1 +-MAXFREQ PPS. If two hits occur in the
649	 * same second, we ignore the later hit; if not and a hit occurs
650	 * outside the range gate, keep the later hit but do not
651	 * process it.
652	 */
653	time_status |= STA_PPSSIGNAL | STA_PPSJITTER;
654	time_status &= ~(STA_PPSWANDER | STA_PPSERROR);
655	pps_valid = PPS_VALID;
656	u_sec = tsp->tv_sec;
657	u_nsec = tsp->tv_nsec;
658	if (u_nsec >= (NANOSECOND >> 1)) {
659		u_nsec -= NANOSECOND;
660		u_sec++;
661	}
662	v_nsec = u_nsec - pps_tf[0].tv_nsec;
663	if (u_sec == pps_tf[0].tv_sec && v_nsec < -MAXFREQ) {
664		return;
665	}
666	pps_tf[2] = pps_tf[1];
667	pps_tf[1] = pps_tf[0];
668	pps_tf[0].tv_sec = u_sec;
669	pps_tf[0].tv_nsec = u_nsec;
670
671	/*
672	 * Compute the difference between the current and previous
673	 * counter values. If the difference exceeds 0.5 s, assume it
674	 * has wrapped around, so correct 1.0 s. If the result exceeds
675	 * the tick interval, the sample point has crossed a tick
676	 * boundary during the last second, so correct the tick. Very
677	 * intricate.
678	 */
679	u_nsec = nsec;
680	if (u_nsec > (NANOSECOND >> 1))
681		u_nsec -= NANOSECOND;
682	else if (u_nsec < -(NANOSECOND >> 1))
683		u_nsec += NANOSECOND;
684	pps_fcount += u_nsec;
685	if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ) {
686		return;
687	}
688	time_status &= ~STA_PPSJITTER;
689
690	/*
691	 * A three-stage median filter is used to help denoise the PPS
692	 * time. The median sample becomes the time offset estimate; the
693	 * difference between the other two samples becomes the time
694	 * dispersion (jitter) estimate.
695	 */
696	if (pps_tf[0].tv_nsec > pps_tf[1].tv_nsec) {
697		if (pps_tf[1].tv_nsec > pps_tf[2].tv_nsec) {
698			v_nsec = pps_tf[1].tv_nsec;	/* 0 1 2 */
699			u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec;
700		} else if (pps_tf[2].tv_nsec > pps_tf[0].tv_nsec) {
701			v_nsec = pps_tf[0].tv_nsec;	/* 2 0 1 */
702			u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec;
703		} else {
704			v_nsec = pps_tf[2].tv_nsec;	/* 0 2 1 */
705			u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec;
706		}
707	} else {
708		if (pps_tf[1].tv_nsec < pps_tf[2].tv_nsec) {
709			v_nsec = pps_tf[1].tv_nsec;	/* 2 1 0 */
710			u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec;
711		} else  if (pps_tf[2].tv_nsec < pps_tf[0].tv_nsec) {
712			v_nsec = pps_tf[0].tv_nsec;	/* 1 0 2 */
713			u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec;
714		} else {
715			v_nsec = pps_tf[2].tv_nsec;	/* 1 2 0 */
716			u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec;
717		}
718	}
719
720	/*
721	 * Nominal jitter is due to PPS signal noise and  interrupt
722	 * latency. If it exceeds the popcorn threshold,
723	 * the sample is discarded. otherwise, if so enabled, the time
724	 * offset is updated. We can tolerate a modest loss of data here
725	 * without degrading time accuracy.
726	 */
727	if (u_nsec > (pps_jitter << PPS_POPCORN)) {
728		time_status |= STA_PPSJITTER;
729		pps_jitcnt++;
730	} else if (time_status & STA_PPSTIME) {
731		L_LINT(ftemp, v_nsec);
732		L_SUB(ftemp, time_offset);
733		L_RSHIFT(ftemp, pps_shift);
734		L_SUB(time_offset, ftemp);
735	}
736	pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG;
737	u_sec = pps_tf[0].tv_sec - pps_lastsec;
738	if (u_sec < (1 << pps_shift))
739		return;
740
741	/*
742	 * At the end of the calibration interval the difference between
743	 * the first and last counter values becomes the scaled
744	 * frequency. It will later be divided by the length of the
745	 * interval to determine the frequency update. If the frequency
746	 * exceeds a sanity threshold, or if the actual calibration
747	 * interval is not equal to the expected length, the data are
748	 * discarded. We can tolerate a modest loss of data here without
749	 * degrading frequency ccuracy.
750	 */
751	pps_calcnt++;
752	v_nsec = -pps_fcount;
753	pps_lastsec = pps_tf[0].tv_sec;
754	pps_fcount = 0;
755	u_nsec = MAXFREQ << pps_shift;
756	if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 <<
757	    pps_shift)) {
758		time_status |= STA_PPSERROR;
759		pps_errcnt++;
760		return;
761	}
762
763	/*
764	 * Here the raw frequency offset and wander (stability) is
765	 * calculated. If the wander is less than the wander threshold
766	 * for four consecutive averaging intervals, the interval is
767	 * doubled; if it is greater than the threshold for four
768	 * consecutive intervals, the interval is halved. The scaled
769	 * frequency offset is converted to frequency offset. The
770	 * stability metric is calculated as the average of recent
771	 * frequency changes, but is used only for performance
772	 * monitoring.
773	 */
774	L_LINT(ftemp, v_nsec);
775	L_RSHIFT(ftemp, pps_shift);
776	L_SUB(ftemp, pps_freq);
777	u_nsec = L_GINT(ftemp);
778	if (u_nsec > PPS_MAXWANDER) {
779		L_LINT(ftemp, PPS_MAXWANDER);
780		pps_intcnt--;
781		time_status |= STA_PPSWANDER;
782		pps_stbcnt++;
783	} else if (u_nsec < -PPS_MAXWANDER) {
784		L_LINT(ftemp, -PPS_MAXWANDER);
785		pps_intcnt--;
786		time_status |= STA_PPSWANDER;
787		pps_stbcnt++;
788	} else {
789		pps_intcnt++;
790	}
791	if (pps_intcnt >= 4) {
792		pps_intcnt = 4;
793		if (pps_shift < pps_shiftmax) {
794			pps_shift++;
795			pps_intcnt = 0;
796		}
797	} else if (pps_intcnt <= -4) {
798		pps_intcnt = -4;
799		if (pps_shift > PPS_FAVG) {
800			pps_shift--;
801			pps_intcnt = 0;
802		}
803	}
804	if (u_nsec < 0)
805		u_nsec = -u_nsec;
806	pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG;
807
808	/*
809	 * The PPS frequency is recalculated and clamped to the maximum
810	 * MAXFREQ. If enabled, the system clock frequency is updated as
811	 * well.
812	 */
813	L_ADD(pps_freq, ftemp);
814	u_nsec = L_GINT(pps_freq);
815	if (u_nsec > MAXFREQ)
816		L_LINT(pps_freq, MAXFREQ);
817	else if (u_nsec < -MAXFREQ)
818		L_LINT(pps_freq, -MAXFREQ);
819	if (time_status & STA_PPSFREQ)
820		time_freq = pps_freq;
821}
822#endif /* PPS_SYNC */
823