Lines Matching +full:higher +full:- +full:than +full:- +full:threshold
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
108 * Note that all routines must run at priority splclock or higher.
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
189 #define PPS_POPCORN 2 /* popcorn spike threshold (shift) */
211 * End of phase/frequency-lock loop (PLL/FLL) definitions
263 ntvp->time.tv_sec = atv.tv_sec;
264 ntvp->time.tv_nsec = atv.tv_nsec;
265 ntvp->maxerror = time_maxerror;
266 ntvp->esterror = time_esterror;
267 ntvp->tai = time_tai;
268 ntvp->time_state = time_state;
271 ntvp->time_state = TIME_ERROR;
275 * ntp_gettime() - NTP user application interface
297 td->td_retval[0] = ntv.time_state;
298 return (copyout(&ntv, uap->ntvp, sizeof(ntv)));
338 * ntp_adjtime() - NTP daemon application interface
352 * Update selected clock variables - only the superuser can
360 modes = ntv->modes;
368 time_maxerror = ntv->maxerror;
370 time_esterror = ntv->esterror;
372 if (time_status & STA_PLL && !(ntv->status & STA_PLL)) {
380 time_status |= ntv->status & ~STA_RONLY;
383 if (ntv->constant < 0)
385 else if (ntv->constant > MAXTC)
388 time_constant = ntv->constant;
391 if (ntv->constant > 0) /* XXX zero & negative numbers ? */
392 time_tai = ntv->constant;
396 if (ntv->shift < PPS_FAVG)
398 else if (ntv->shift > PPS_FAVGMAX)
401 pps_shiftmax = ntv->shift;
413 freq = (ntv->freq * 1000LL) >> 16;
416 else if (freq < -MAXFREQ)
417 L_LINT(time_freq, -MAXFREQ);
420 * ntv->freq is [PPM * 2^16] = [us/s * 2^16]
423 time_freq = ntv->freq * 1000LL * 65536LL;
431 hardupdate(ntv->offset);
433 hardupdate(ntv->offset * 1000);
441 ntv->offset = L_GINT(time_offset);
443 ntv->offset = L_GINT(time_offset) / 1000; /* XXX rounding ? */
444 ntv->freq = L_GINT((time_freq / 1000LL) << 16);
445 ntv->maxerror = time_maxerror;
446 ntv->esterror = time_esterror;
447 ntv->status = time_status;
448 ntv->constant = time_constant;
450 ntv->precision = time_precision;
452 ntv->precision = time_precision / 1000;
453 ntv->tolerance = MAXFREQ * SCALE_PPM;
455 ntv->shift = pps_shift;
456 ntv->ppsfreq = L_GINT((pps_freq / 1000LL) << 16);
458 ntv->jitter = pps_jitter;
460 ntv->jitter = pps_jitter / 1000;
461 ntv->stabil = pps_stabil;
462 ntv->calcnt = pps_calcnt;
463 ntv->errcnt = pps_errcnt;
464 ntv->jitcnt = pps_jitcnt;
465 ntv->stbcnt = pps_stbcnt;
486 error = copyin(uap->tp, &ntv, sizeof(ntv));
490 error = copyout(&ntv, uap->tp, sizeof(ntv));
492 td->td_retval[0] = retval;
499 * second_overflow() - called after ntp_tick_adjust()
510 l_fp ftemp; /* 32/64-bit temporary */
524 * Leap second processing. If in leap-insert state at
526 * second; if in leap-delete state, the system clock is
550 (*newsec)--;
564 time_tai--;
607 * Apply any correction from adjtime(2). If more than one second
614 else if (time_adjtime < -1000000)
615 tickrate = -5000;
618 else if (time_adjtime < -500)
619 tickrate = -500;
622 time_adjtime -= tickrate;
630 pps_valid--;
639 * hardupdate() - local clock update
642 * phase and frequency. The implementation is of an adaptive-parameter,
643 * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
648 * believes the local clock is valid within some bound (+-128 ms with
649 * NTP). If the caller's time is far different than the PPS time, an
653 * intervals less than 256 s, operation should be in phase-lock mode,
655 * than 1024 s, operation should be in frequency-lock mode, where the
679 else if (offset < -MAXPHASE)
680 time_monitor = -MAXPHASE;
698 mtemp = time_uptime - time_reftime;
714 else if (L_GINT(time_freq) < -MAXFREQ)
715 L_LINT(time_freq, -MAXFREQ);
720 * hardpps() - discipline CPU clock oscillator to external PPS signal
724 * first-order feedback loops, one for the phase, the other for the
730 * time and architecture-dependent hardware counter values in
731 * nanoseconds at the on-time PPS signal transition.
734 * priority level higher than the timer interrupt routine hardclock().
739 * tsp - time at current PPS event
740 * delta_nsec - time elapsed between the previous and current PPS event
754 * the range +-500 us. The frequency discriminator rejects input
755 * signals with apparent frequency outside the range 1 +-500
764 u_sec = tsp->tv_sec;
765 u_nsec = tsp->tv_nsec;
767 u_nsec -= NANOSECOND;
770 v_nsec = u_nsec - pps_tf[0].tv_nsec;
771 if (u_sec == pps_tf[0].tv_sec && v_nsec < NANOSECOND - MAXFREQ)
782 pps_fcount += delta_nsec - NANOSECOND;
783 if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ)
788 * A three-stage median filter is used to help denoise the PPS
796 u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec;
799 u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec;
802 u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec;
807 u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec;
810 u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec;
813 u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec;
819 * latency. If it exceeds the popcorn threshold, the sample is
825 * timecounter, so the popcorn threshold is not allowed to fall below
827 * timecounter running faster than 1 GHz the lower bound is 2ns, just
828 * to avoid a nonsensical threshold of zero.
835 time_monitor = -v_nsec;
838 pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG;
839 u_sec = pps_tf[0].tv_sec - pps_lastsec;
848 * exceeds a sanity threshold, or if the actual calibration
854 v_nsec = -pps_fcount;
858 if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 << pps_shift)) {
866 * calculated. If the wander is less than the wander threshold
868 * doubled; if it is greater than the threshold for four
881 pps_intcnt--;
884 } else if (u_nsec < -PPS_MAXWANDER) {
885 L_LINT(ftemp, -PPS_MAXWANDER);
886 pps_intcnt--;
898 } else if (pps_intcnt <= -4 || pps_shift > pps_shiftmax) {
899 pps_intcnt = -4;
901 pps_shift--;
906 u_nsec = -u_nsec;
907 pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG;
918 else if (u_nsec < -MAXFREQ)
919 L_LINT(pps_freq, -MAXFREQ);
941 if (uap->delta) {
942 error = copyin(uap->delta, &delta, sizeof(delta));
949 if (uap->olddelta && error == 0)
950 error = copyout(&olddelta, uap->olddelta, sizeof(olddelta));
965 ltw = (int64_t)delta->tv_sec * 1000000 + delta->tv_usec;
977 atv.tv_sec--;
992 * Read of time_status is lock-less, which is fine since
1016 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
1017 if (error || !req->newptr)