Lines Matching +full:default +full:- +full:sample +full:- +full:phase
1 // SPDX-License-Identifier: GPL-2.0
26 * struct ntp_data - Structure holding all NTP related state
40 * @ntp_tick_adj: Constant boot-param configurable NTP tick adjustment (upscaled)
44 * @pps_tf: PPS phase median filter
108 * The following variables are used when a pulse-per-second (PPS) signal
122 * PPS kernel consumer compensates the whole phase error immediately.
127 if (ntpdata->time_status & STA_PPSTIME && ntpdata->time_status & STA_PPSSIGNAL)
130 return shift_right(offset, SHIFT_PLL + ntpdata->time_constant);
136 ntpdata->pps_shift = PPS_INTMIN;
137 ntpdata->pps_intcnt = 0;
141 * pps_clear - Clears the PPS state variables
147 ntpdata->pps_tf[0] = 0;
148 ntpdata->pps_tf[1] = 0;
149 ntpdata->pps_tf[2] = 0;
150 ntpdata->pps_fbase.tv_sec = ntpdata->pps_fbase.tv_nsec = 0;
151 ntpdata->pps_freq = 0;
160 if (ntpdata->pps_valid > 0) {
161 ntpdata->pps_valid--;
163 ntpdata->time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
171 ntpdata->pps_freq = ntpdata->time_freq;
199 txc->ppsfreq = shift_right((ntpdata->pps_freq >> PPM_SCALE_INV_SHIFT) *
201 txc->jitter = ntpdata->pps_jitter;
202 if (!(ntpdata->time_status & STA_NANO))
203 txc->jitter = ntpdata->pps_jitter / NSEC_PER_USEC;
204 txc->shift = ntpdata->pps_shift;
205 txc->stabil = ntpdata->pps_stabil;
206 txc->jitcnt = ntpdata->pps_jitcnt;
207 txc->calcnt = ntpdata->pps_calcnt;
208 txc->errcnt = ntpdata->pps_errcnt;
209 txc->stbcnt = ntpdata->pps_stbcnt;
216 return shift_right(offset, SHIFT_PLL + ntpdata->time_constant);
232 txc->ppsfreq = 0;
233 txc->jitter = 0;
234 txc->shift = 0;
235 txc->stabil = 0;
236 txc->jitcnt = 0;
237 txc->calcnt = 0;
238 txc->errcnt = 0;
239 txc->stbcnt = 0;
250 u64 second_length, new_base, tick_usec = (u64)ntpdata->tick_usec;
254 second_length += ntpdata->ntp_tick_adj;
255 second_length += ntpdata->time_freq;
263 ntpdata->tick_length += new_base - ntpdata->tick_length_base;
264 ntpdata->tick_length_base = new_base;
269 ntpdata->time_status &= ~STA_MODE;
274 if (!(ntpdata->time_status & STA_FLL) && (secs <= MAXSEC))
277 ntpdata->time_status |= STA_MODE;
279 return div64_long(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
287 if (!(ntpdata->time_status & STA_PLL))
290 if (!(ntpdata->time_status & STA_NANO)) {
292 offset = clamp(offset, -USEC_PER_SEC, USEC_PER_SEC);
296 /* Scale the phase adjustment and clamp to the operating range. */
297 offset = clamp(offset, -MAXPHASE, MAXPHASE);
304 secs = (long)(real_secs - ntpdata->time_reftime);
305 if (unlikely(ntpdata->time_status & STA_FREQHOLD))
308 ntpdata->time_reftime = real_secs;
318 if (unlikely(secs > 1 << (SHIFT_PLL + 1 + ntpdata->time_constant)))
319 secs = 1 << (SHIFT_PLL + 1 + ntpdata->time_constant);
322 (NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + ntpdata->time_constant));
324 freq_adj = min(freq_adj + ntpdata->time_freq, MAXFREQ_SCALED);
326 ntpdata->time_freq = max(freq_adj, -MAXFREQ_SCALED);
328 ntpdata->time_offset = div_s64(offset64 << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
334 ntpdata->time_adjust = 0;
335 ntpdata->time_status |= STA_UNSYNC;
336 ntpdata->time_maxerror = NTP_PHASE_LIMIT;
337 ntpdata->time_esterror = NTP_PHASE_LIMIT;
341 ntpdata->tick_length = ntpdata->tick_length_base;
342 ntpdata->time_offset = 0;
344 ntpdata->ntp_next_leap_sec = TIME64_MAX;
350 * ntp_clear - Clears the NTP state variables
364 * ntp_get_next_leap - Returns the next leapsecond in CLOCK_REALTIME ktime_t
374 if ((ntpdata->time_state == TIME_INS) && (ntpdata->time_status & STA_INS))
375 return ktime_set(ntpdata->ntp_next_leap_sec, 0);
398 * Leap second processing. If in leap-insert state at the end of the
399 * day, the system clock is set back one second; if in leap-delete
402 switch (ntpdata->time_state) {
404 if (ntpdata->time_status & STA_INS) {
405 ntpdata->time_state = TIME_INS;
407 ntpdata->ntp_next_leap_sec = secs + SECS_PER_DAY - rem;
408 } else if (ntpdata->time_status & STA_DEL) {
409 ntpdata->time_state = TIME_DEL;
411 ntpdata->ntp_next_leap_sec = secs + SECS_PER_DAY - rem;
415 if (!(ntpdata->time_status & STA_INS)) {
416 ntpdata->ntp_next_leap_sec = TIME64_MAX;
417 ntpdata->time_state = TIME_OK;
418 } else if (secs == ntpdata->ntp_next_leap_sec) {
419 leap = -1;
420 ntpdata->time_state = TIME_OOP;
425 if (!(ntpdata->time_status & STA_DEL)) {
426 ntpdata->ntp_next_leap_sec = TIME64_MAX;
427 ntpdata->time_state = TIME_OK;
428 } else if (secs == ntpdata->ntp_next_leap_sec) {
430 ntpdata->ntp_next_leap_sec = TIME64_MAX;
431 ntpdata->time_state = TIME_WAIT;
436 ntpdata->ntp_next_leap_sec = TIME64_MAX;
437 ntpdata->time_state = TIME_WAIT;
440 if (!(ntpdata->time_status & (STA_INS | STA_DEL)))
441 ntpdata->time_state = TIME_OK;
446 ntpdata->time_maxerror += MAXFREQ / NSEC_PER_USEC;
447 if (ntpdata->time_maxerror > NTP_PHASE_LIMIT) {
448 ntpdata->time_maxerror = NTP_PHASE_LIMIT;
449 ntpdata->time_status |= STA_UNSYNC;
452 /* Compute the phase adjustment for the next second */
453 ntpdata->tick_length = ntpdata->tick_length_base;
455 delta = ntp_offset_chunk(ntpdata, ntpdata->time_offset);
456 ntpdata->time_offset -= delta;
457 ntpdata->tick_length += delta;
462 if (!ntpdata->time_adjust)
465 if (ntpdata->time_adjust > MAX_TICKADJ) {
466 ntpdata->time_adjust -= MAX_TICKADJ;
467 ntpdata->tick_length += MAX_TICKADJ_SCALED;
471 if (ntpdata->time_adjust < -MAX_TICKADJ) {
472 ntpdata->time_adjust += MAX_TICKADJ;
473 ntpdata->tick_length -= MAX_TICKADJ_SCALED;
477 ntpdata->tick_length += (s64)(ntpdata->time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ)
479 ntpdata->time_adjust = 0;
503 exp = ktime_add_ns(exp, 2ULL * NSEC_PER_SEC - offset_nsec);
505 exp = ktime_add_ns(exp, SYNC_PERIOD_NS - offset_nsec);
516 * tsched t1 write(t2.tv_sec - 1sec)) t2 RTC increments seconds
519 * tsched = t2 - set_offset_nsec
520 * newval = t2 - NSEC_PER_SEC
522 * ==> neval = tsched + set_offset_nsec - NSEC_PER_SEC
527 * abs(now - tsched) < FUZZ
537 struct timespec64 delay = {.tv_sec = -1,
542 if (to_set->tv_nsec < TIME_SET_NSEC_FUZZ) {
543 to_set->tv_nsec = 0;
547 if (to_set->tv_nsec > NSEC_PER_SEC - TIME_SET_NSEC_FUZZ) {
548 to_set->tv_sec++;
549 to_set->tv_nsec = 0;
558 return -ENODEV;
563 return -ENODEV;
573 int err = -ENODEV;
577 return -ENODEV;
579 if (!rtc->ops || !rtc->ops->set_time)
583 if (*offset_nsec == rtc->set_offset_nsec) {
584 rtc_time64_to_tm(to_set->tv_sec, &tm);
588 *offset_nsec = rtc->set_offset_nsec;
589 err = -EAGAIN;
598 return -ENODEV;
603 * ntp_synced - Tells whether the NTP status is not UNSYNC
614 * precision, but many RTCs will adjust the phase of their second tick to
616 * set at the correct moment to phase synchronize the RTC second tick over
622 * The default synchronization offset is 500ms for the deprecated
628 int res = -EAGAIN;
645 to_set.tv_sec -= (sys_tz.tz_minuteswest * 60);
649 if (res != -ENODEV)
654 if (res == -ENODEV)
688 * Propagate a new txc->status value into the NTP state:
692 if ((ntpdata->time_status & STA_PLL) && !(txc->status & STA_PLL)) {
693 ntpdata->time_state = TIME_OK;
694 ntpdata->time_status = STA_UNSYNC;
695 ntpdata->ntp_next_leap_sec = TIME64_MAX;
704 if (!(ntpdata->time_status & STA_PLL) && (txc->status & STA_PLL))
705 ntpdata->time_reftime = __ktime_get_real_seconds();
708 ntpdata->time_status &= STA_RONLY;
709 ntpdata->time_status |= txc->status & ~STA_RONLY;
715 if (txc->modes & ADJ_STATUS)
718 if (txc->modes & ADJ_NANO)
719 ntpdata->time_status |= STA_NANO;
721 if (txc->modes & ADJ_MICRO)
722 ntpdata->time_status &= ~STA_NANO;
724 if (txc->modes & ADJ_FREQUENCY) {
725 ntpdata->time_freq = txc->freq * PPM_SCALE;
726 ntpdata->time_freq = min(ntpdata->time_freq, MAXFREQ_SCALED);
727 ntpdata->time_freq = max(ntpdata->time_freq, -MAXFREQ_SCALED);
732 if (txc->modes & ADJ_MAXERROR)
733 ntpdata->time_maxerror = clamp(txc->maxerror, 0, NTP_PHASE_LIMIT);
735 if (txc->modes & ADJ_ESTERROR)
736 ntpdata->time_esterror = clamp(txc->esterror, 0, NTP_PHASE_LIMIT);
738 if (txc->modes & ADJ_TIMECONST) {
739 ntpdata->time_constant = clamp(txc->constant, 0, MAXTC);
740 if (!(ntpdata->time_status & STA_NANO))
741 ntpdata->time_constant += 4;
742 ntpdata->time_constant = clamp(ntpdata->time_constant, 0, MAXTC);
745 if (txc->modes & ADJ_TAI && txc->constant >= 0 && txc->constant <= MAX_TAI_OFFSET)
746 *time_tai = txc->constant;
748 if (txc->modes & ADJ_OFFSET)
749 ntp_update_offset(ntpdata, txc->offset);
751 if (txc->modes & ADJ_TICK)
752 ntpdata->tick_usec = txc->tick;
754 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
760 * kernel time-keeping variables. used by xntpd.
768 if (txc->modes & ADJ_ADJTIME) {
769 long save_adjust = ntpdata->time_adjust;
771 if (!(txc->modes & ADJ_OFFSET_READONLY)) {
773 ntpdata->time_adjust = txc->offset;
777 audit_ntp_set_new(ad, AUDIT_NTP_ADJUST, ntpdata->time_adjust);
779 txc->offset = save_adjust;
782 if (txc->modes) {
783 audit_ntp_set_old(ad, AUDIT_NTP_OFFSET, ntpdata->time_offset);
784 audit_ntp_set_old(ad, AUDIT_NTP_FREQ, ntpdata->time_freq);
785 audit_ntp_set_old(ad, AUDIT_NTP_STATUS, ntpdata->time_status);
787 audit_ntp_set_old(ad, AUDIT_NTP_TICK, ntpdata->tick_usec);
791 audit_ntp_set_new(ad, AUDIT_NTP_OFFSET, ntpdata->time_offset);
792 audit_ntp_set_new(ad, AUDIT_NTP_FREQ, ntpdata->time_freq);
793 audit_ntp_set_new(ad, AUDIT_NTP_STATUS, ntpdata->time_status);
795 audit_ntp_set_new(ad, AUDIT_NTP_TICK, ntpdata->tick_usec);
798 txc->offset = shift_right(ntpdata->time_offset * NTP_INTERVAL_FREQ, NTP_SCALE_SHIFT);
799 if (!(ntpdata->time_status & STA_NANO))
800 txc->offset = div_s64(txc->offset, NSEC_PER_USEC);
803 result = ntpdata->time_state;
804 if (is_error_status(ntpdata->time_status))
807 txc->freq = shift_right((ntpdata->time_freq >> PPM_SCALE_INV_SHIFT) *
809 txc->maxerror = ntpdata->time_maxerror;
810 txc->esterror = ntpdata->time_esterror;
811 txc->status = ntpdata->time_status;
812 txc->constant = ntpdata->time_constant;
813 txc->precision = 1;
814 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
815 txc->tick = ntpdata->tick_usec;
816 txc->tai = *time_tai;
821 txc->time.tv_sec = ts->tv_sec;
822 txc->time.tv_usec = ts->tv_nsec;
823 if (!(ntpdata->time_status & STA_NANO))
824 txc->time.tv_usec = ts->tv_nsec / NSEC_PER_USEC;
827 if (unlikely(ts->tv_sec >= ntpdata->ntp_next_leap_sec)) {
828 if ((ntpdata->time_state == TIME_INS) && (ntpdata->time_status & STA_INS)) {
830 txc->tai++;
831 txc->time.tv_sec--;
833 if ((ntpdata->time_state == TIME_DEL) && (ntpdata->time_status & STA_DEL)) {
835 txc->tai--;
836 txc->time.tv_sec++;
838 if ((ntpdata->time_state == TIME_OOP) && (ts->tv_sec == ntpdata->ntp_next_leap_sec))
850 * pps_normtime.nsec has a range of ( -NSEC_PER_SEC / 2, NSEC_PER_SEC / 2 ]
860 * [ -NSEC_PER_SEC / 2, NSEC_PER_SEC / 2 ] interval
870 norm.nsec -= NSEC_PER_SEC;
877 /* Get current phase correction and jitter */
880 *jitter = ntpdata->pps_tf[0] - ntpdata->pps_tf[1];
882 *jitter = -*jitter;
885 return ntpdata->pps_tf[0];
888 /* Add the sample to the phase filter */
891 ntpdata->pps_tf[2] = ntpdata->pps_tf[1];
892 ntpdata->pps_tf[1] = ntpdata->pps_tf[0];
893 ntpdata->pps_tf[0] = err;
902 if (--ntpdata->pps_intcnt <= -PPS_INTCOUNT) {
903 ntpdata->pps_intcnt = -PPS_INTCOUNT;
904 if (ntpdata->pps_shift > PPS_INTMIN) {
905 ntpdata->pps_shift--;
906 ntpdata->pps_intcnt = 0;
917 if (++ntpdata->pps_intcnt >= PPS_INTCOUNT) {
918 ntpdata->pps_intcnt = PPS_INTCOUNT;
919 if (ntpdata->pps_shift < PPS_INTMAX) {
920 ntpdata->pps_shift++;
921 ntpdata->pps_intcnt = 0;
942 if (freq_norm.sec > (2 << ntpdata->pps_shift)) {
943 ntpdata->time_status |= STA_PPSERROR;
944 ntpdata->pps_errcnt++;
946 printk_deferred(KERN_ERR "hardpps: PPSERROR: interval too long - %lld s\n",
956 ftemp = div_s64(((s64)(-freq_norm.nsec)) << NTP_SCALE_SHIFT,
958 delta = shift_right(ftemp - ntpdata->pps_freq, NTP_SCALE_SHIFT);
959 ntpdata->pps_freq = ftemp;
960 if (delta > PPS_MAXWANDER || delta < -PPS_MAXWANDER) {
962 ntpdata->time_status |= STA_PPSWANDER;
963 ntpdata->pps_stbcnt++;
966 /* Good sample */
976 delta_mod = -delta_mod;
977 ntpdata->pps_stabil += (div_s64(((s64)delta_mod) << (NTP_SCALE_SHIFT - SHIFT_USEC),
978 NSEC_PER_USEC) - ntpdata->pps_stabil) >> PPS_INTMIN;
981 if ((ntpdata->time_status & STA_PPSFREQ) && !(ntpdata->time_status & STA_FREQHOLD)) {
982 ntpdata->time_freq = ntpdata->pps_freq;
989 /* Correct REALTIME clock phase error against PPS signal */
992 long correction = -error;
995 /* Add the sample to the median filter */
1001 * threshold, the sample is discarded; otherwise, if so enabled,
1004 if (jitter > (ntpdata->pps_jitter << PPS_POPCORN)) {
1006 jitter, (ntpdata->pps_jitter << PPS_POPCORN));
1007 ntpdata->time_status |= STA_PPSJITTER;
1008 ntpdata->pps_jitcnt++;
1009 } else if (ntpdata->time_status & STA_PPSTIME) {
1010 /* Correct the time using the phase offset */
1011 ntpdata->time_offset = div_s64(((s64)correction) << NTP_SCALE_SHIFT,
1014 ntpdata->time_adjust = 0;
1017 ntpdata->pps_jitter += (jitter - ntpdata->pps_jitter) >> PPS_INTMIN;
1021 * __hardpps() - discipline CPU clock oscillator to external PPS signal
1026 * is used to correct clock phase error and the latter is used to
1040 ntpdata->time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
1043 ntpdata->time_status |= STA_PPSSIGNAL;
1044 ntpdata->pps_valid = PPS_VALID;
1050 if (unlikely(ntpdata->pps_fbase.tv_sec == 0)) {
1051 ntpdata->pps_fbase = *raw_ts;
1056 freq_norm = pps_normalize_ts(timespec64_sub(*raw_ts, ntpdata->pps_fbase));
1060 * [1s - MAXFREQ us, 1s + MAXFREQ us], otherwise reject it
1063 (freq_norm.nsec < -MAXFREQ * freq_norm.sec)) {
1064 ntpdata->time_status |= STA_PPSJITTER;
1066 ntpdata->pps_fbase = *raw_ts;
1072 if (freq_norm.sec >= (1 << ntpdata->pps_shift)) {
1073 ntpdata->pps_calcnt++;
1075 ntpdata->pps_fbase = *raw_ts;