Lines Matching +full:min +full:- +full:sample +full:- +full:rate +full:- +full:hz
1 /*-
4 * Copyright (c) David L. Mills 1993-2001 *
22 * Poul-Henning Kamp <phk@FreeBSD.org>.
57 * Single-precision macros for 64-bit machines
61 #define L_SUB(v, u) ((v) -= (u))
63 #define L_NEG(v) ((v) = -(v))
67 (v) = -(-(v) >> (n)); \
77 ((v) = -((int64_t)(-(a)) << 32)); \
81 #define L_GINT(v) ((v) < 0 ? -(-(v) >> 32) : (v) >> 32)
104 * architecture-specific module. The interpolation can use either a
111 * Phase/frequency-lock loop (PLL/FLL) definitions
114 * variables and frequency variables. Both types are represented as 64-
115 * bit fixed-point quantities with the decimal point between two 32-bit
116 * halves. On a 32-bit machine, each half is represented as a single
117 * word and mathematical operations are done using multiple-precision
118 * arithmetic. On a 64-bit machine, ordinary computer arithmetic is
121 * A time variable is a signed 64-bit fixed-point number in ns and
124 * 0.5 s and the resolution is about 2.3e-10 ns.
128 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134 * A frequency variable is a signed 64-bit fixed-point number in ns/s
137 * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s.
141 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
178 * The following variables are used when a pulse-per-second (PPS) signal
183 #define PPS_FAVG 2 /* min freq avg interval (s) (shift) */
211 * End of phase/frequency-lock loop (PLL/FLL) definitions
263 ntvp->time.tv_sec = atv.tv_sec; in ntp_gettime1()
264 ntvp->time.tv_nsec = atv.tv_nsec; in ntp_gettime1()
265 ntvp->maxerror = time_maxerror; in ntp_gettime1()
266 ntvp->esterror = time_esterror; in ntp_gettime1()
267 ntvp->tai = time_tai; in ntp_gettime1()
268 ntvp->time_state = time_state; in ntp_gettime1()
271 ntvp->time_state = TIME_ERROR; in ntp_gettime1()
275 * ntp_gettime() - NTP user application interface
297 td->td_retval[0] = ntv.time_state; in sys_ntp_gettime()
298 return (copyout(&ntv, uap->ntvp, sizeof(ntv))); in sys_ntp_gettime()
338 * ntp_adjtime() - NTP daemon application interface
352 * Update selected clock variables - only the superuser can in kern_ntp_adjtime()
360 modes = ntv->modes; in kern_ntp_adjtime()
368 time_maxerror = ntv->maxerror; in kern_ntp_adjtime()
370 time_esterror = ntv->esterror; in kern_ntp_adjtime()
372 if (time_status & STA_PLL && !(ntv->status & STA_PLL)) { in kern_ntp_adjtime()
380 time_status |= ntv->status & ~STA_RONLY; in kern_ntp_adjtime()
383 if (ntv->constant < 0) in kern_ntp_adjtime()
385 else if (ntv->constant > MAXTC) in kern_ntp_adjtime()
388 time_constant = ntv->constant; in kern_ntp_adjtime()
391 if (ntv->constant > 0) /* XXX zero & negative numbers ? */ in kern_ntp_adjtime()
392 time_tai = ntv->constant; in kern_ntp_adjtime()
396 if (ntv->shift < PPS_FAVG) in kern_ntp_adjtime()
398 else if (ntv->shift > PPS_FAVGMAX) in kern_ntp_adjtime()
401 pps_shiftmax = ntv->shift; in kern_ntp_adjtime()
413 freq = (ntv->freq * 1000LL) >> 16; in kern_ntp_adjtime()
416 else if (freq < -MAXFREQ) in kern_ntp_adjtime()
417 L_LINT(time_freq, -MAXFREQ); in kern_ntp_adjtime()
420 * ntv->freq is [PPM * 2^16] = [us/s * 2^16] in kern_ntp_adjtime()
423 time_freq = ntv->freq * 1000LL * 65536LL; in kern_ntp_adjtime()
431 hardupdate(ntv->offset); in kern_ntp_adjtime()
433 hardupdate(ntv->offset * 1000); in kern_ntp_adjtime()
441 ntv->offset = L_GINT(time_offset); in kern_ntp_adjtime()
443 ntv->offset = L_GINT(time_offset) / 1000; /* XXX rounding ? */ in kern_ntp_adjtime()
444 ntv->freq = L_GINT((time_freq / 1000LL) << 16); in kern_ntp_adjtime()
445 ntv->maxerror = time_maxerror; in kern_ntp_adjtime()
446 ntv->esterror = time_esterror; in kern_ntp_adjtime()
447 ntv->status = time_status; in kern_ntp_adjtime()
448 ntv->constant = time_constant; in kern_ntp_adjtime()
450 ntv->precision = time_precision; in kern_ntp_adjtime()
452 ntv->precision = time_precision / 1000; in kern_ntp_adjtime()
453 ntv->tolerance = MAXFREQ * SCALE_PPM; in kern_ntp_adjtime()
455 ntv->shift = pps_shift; in kern_ntp_adjtime()
456 ntv->ppsfreq = L_GINT((pps_freq / 1000LL) << 16); in kern_ntp_adjtime()
458 ntv->jitter = pps_jitter; in kern_ntp_adjtime()
460 ntv->jitter = pps_jitter / 1000; in kern_ntp_adjtime()
461 ntv->stabil = pps_stabil; in kern_ntp_adjtime()
462 ntv->calcnt = pps_calcnt; in kern_ntp_adjtime()
463 ntv->errcnt = pps_errcnt; in kern_ntp_adjtime()
464 ntv->jitcnt = pps_jitcnt; in kern_ntp_adjtime()
465 ntv->stbcnt = pps_stbcnt; in kern_ntp_adjtime()
486 error = copyin(uap->tp, &ntv, sizeof(ntv)); in sys_ntp_adjtime()
490 error = copyout(&ntv, uap->tp, sizeof(ntv)); in sys_ntp_adjtime()
492 td->td_retval[0] = retval; in sys_ntp_adjtime()
499 * second_overflow() - called after ntp_tick_adjust()
510 l_fp ftemp; /* 32/64-bit temporary */ in ntp_update_second()
524 * Leap second processing. If in leap-insert state at in ntp_update_second()
526 * second; if in leap-delete state, the system clock is in ntp_update_second()
550 (*newsec)--; in ntp_update_second()
564 time_tai--; in ntp_update_second()
608 * off we slew at a rate of 5ms/s (5000 PPM) else 500us/s (500 PPM) in ntp_update_second()
614 else if (time_adjtime < -1000000) in ntp_update_second()
615 tickrate = -5000; in ntp_update_second()
618 else if (time_adjtime < -500) in ntp_update_second()
619 tickrate = -500; in ntp_update_second()
622 time_adjtime -= tickrate; in ntp_update_second()
631 pps_valid--; in ntp_update_second()
640 * hardupdate() - local clock update
643 * phase and frequency. The implementation is of an adaptive-parameter,
644 * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
649 * believes the local clock is valid within some bound (+-128 ms with
654 * intervals less than 256 s, operation should be in phase-lock mode,
656 * than 1024 s, operation should be in frequency-lock mode, where the
680 else if (offset < -MAXPHASE) in hardupdate()
681 time_monitor = -MAXPHASE; in hardupdate()
699 mtemp = time_uptime - time_reftime; in hardupdate()
715 else if (L_GINT(time_freq) < -MAXFREQ) in hardupdate()
716 L_LINT(time_freq, -MAXFREQ); in hardupdate()
721 * hardpps() - discipline CPU clock oscillator to external PPS signal
725 * first-order feedback loops, one for the phase, the other for the
731 * time and architecture-dependent hardware counter values in
732 * nanoseconds at the on-time PPS signal transition.
740 * tsp - time at current PPS event
741 * delta_nsec - time elapsed between the previous and current PPS event
755 * the range +-500 us. The frequency discriminator rejects input in hardpps()
756 * signals with apparent frequency outside the range 1 +-500 in hardpps()
765 u_sec = tsp->tv_sec; in hardpps()
766 u_nsec = tsp->tv_nsec; in hardpps()
768 u_nsec -= NANOSECOND; in hardpps()
771 v_nsec = u_nsec - pps_tf[0].tv_nsec; in hardpps()
772 if (u_sec == pps_tf[0].tv_sec && v_nsec < NANOSECOND - MAXFREQ) in hardpps()
783 pps_fcount += delta_nsec - NANOSECOND; in hardpps()
784 if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ) in hardpps()
789 * A three-stage median filter is used to help denoise the PPS in hardpps()
790 * time. The median sample becomes the time offset estimate; the in hardpps()
797 u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec; in hardpps()
800 u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec; in hardpps()
803 u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec; in hardpps()
808 u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec; in hardpps()
811 u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec; in hardpps()
814 u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec; in hardpps()
820 * latency. If it exceeds the popcorn threshold, the sample is in hardpps()
836 time_monitor = -v_nsec; in hardpps()
839 pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG; in hardpps()
840 u_sec = pps_tf[0].tv_sec - pps_lastsec; in hardpps()
855 v_nsec = -pps_fcount; in hardpps()
859 if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 << pps_shift)) { in hardpps()
882 pps_intcnt--; in hardpps()
885 } else if (u_nsec < -PPS_MAXWANDER) { in hardpps()
886 L_LINT(ftemp, -PPS_MAXWANDER); in hardpps()
887 pps_intcnt--; in hardpps()
899 } else if (pps_intcnt <= -4 || pps_shift > pps_shiftmax) { in hardpps()
900 pps_intcnt = -4; in hardpps()
902 pps_shift--; in hardpps()
907 u_nsec = -u_nsec; in hardpps()
908 pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG; in hardpps()
919 else if (u_nsec < -MAXFREQ) in hardpps()
920 L_LINT(pps_freq, -MAXFREQ); in hardpps()
942 if (uap->delta) { in sys_adjtime()
943 error = copyin(uap->delta, &delta, sizeof(delta)); in sys_adjtime()
950 if (uap->olddelta && error == 0) in sys_adjtime()
951 error = copyout(&olddelta, uap->olddelta, sizeof(olddelta)); in sys_adjtime()
966 ltw = (int64_t)delta->tv_sec * 1000000 + delta->tv_usec; in kern_adjtime()
978 atv.tv_sec--; in kern_adjtime()
993 * Read of time_status is lock-less, which is fine since in periodic_resettodr()
999 callout_schedule(&resettodr_callout, resettodr_period * hz); in periodic_resettodr()
1017 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); in sysctl_resettodr_period()
1018 if (error || !req->newptr) in sysctl_resettodr_period()
1025 callout_reset(&resettodr_callout, resettodr_period * hz, in sysctl_resettodr_period()
1044 callout_reset(&resettodr_callout, resettodr_period * hz, in start_periodic_resettodr()