Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * NTP state machine interfaces and logic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * This code was mainly moved from kernel/timer.c and kernel/time.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Please see those files for relevant copyright info and historical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * changelogs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/clocksource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/math64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/timex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/audit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "ntp_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "timekeeping_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * NTP timekeeping variables:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  * Note: All of the NTP state is protected by the timekeeping locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) /* USER_HZ period (usecs): */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) unsigned long			tick_usec = USER_TICK_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) /* SHIFTED_HZ period (nsecs): */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) unsigned long			tick_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) static u64			tick_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) static u64			tick_length_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #define SECS_PER_DAY		86400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #define MAX_TICKADJ		500LL		/* usecs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #define MAX_TICKADJ_SCALED \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	(((MAX_TICKADJ * NSEC_PER_USEC) << NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #define MAX_TAI_OFFSET		100000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  * phase-lock loop variables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * clock synchronization status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * (TIME_ERROR prevents overwriting the CMOS clock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) static int			time_state = TIME_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) /* clock status bits:							*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) static int			time_status = STA_UNSYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) /* time adjustment (nsecs):						*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) static s64			time_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) /* pll time constant:							*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) static long			time_constant = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) /* maximum error (usecs):						*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) static long			time_maxerror = NTP_PHASE_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) /* estimated error (usecs):						*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) static long			time_esterror = NTP_PHASE_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) /* frequency offset (scaled nsecs/secs):				*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) static s64			time_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) /* time at last adjustment (secs):					*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) static time64_t		time_reftime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) static long			time_adjust;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) /* constant (boot-param configurable) NTP tick adjustment (upscaled)	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) static s64			ntp_tick_adj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) /* second value of the next pending leapsecond, or TIME64_MAX if no leap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) static time64_t			ntp_next_leap_sec = TIME64_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #ifdef CONFIG_NTP_PPS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * The following variables are used when a pulse-per-second (PPS) signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  * is available. They establish the engineering parameters of the clock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  * discipline loop when controlled by the PPS signal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) #define PPS_VALID	10	/* PPS signal watchdog max (s) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) #define PPS_POPCORN	4	/* popcorn spike threshold (shift) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) #define PPS_INTMIN	2	/* min freq interval (s) (shift) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) #define PPS_INTMAX	8	/* max freq interval (s) (shift) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) #define PPS_INTCOUNT	4	/* number of consecutive good intervals to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 				   increase pps_shift or consecutive bad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 				   intervals to decrease it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) #define PPS_MAXWANDER	100000	/* max PPS freq wander (ns/s) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) static int pps_valid;		/* signal watchdog counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) static long pps_tf[3];		/* phase median filter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) static long pps_jitter;		/* current jitter (ns) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) static struct timespec64 pps_fbase; /* beginning of the last freq interval */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) static int pps_shift;		/* current interval duration (s) (shift) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) static int pps_intcnt;		/* interval counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) static s64 pps_freq;		/* frequency offset (scaled ns/s) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) static long pps_stabil;		/* current stability (scaled ns/s) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114)  * PPS signal quality monitors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) static long pps_calcnt;		/* calibration intervals */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) static long pps_jitcnt;		/* jitter limit exceeded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) static long pps_stbcnt;		/* stability limit exceeded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) static long pps_errcnt;		/* calibration errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) /* PPS kernel consumer compensates the whole phase error immediately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123)  * Otherwise, reduce the offset by a fixed factor times the time constant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) static inline s64 ntp_offset_chunk(s64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	if (time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		return offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		return shift_right(offset, SHIFT_PLL + time_constant);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) static inline void pps_reset_freq_interval(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	/* the PPS calibration interval may end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	   surprisingly early */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	pps_shift = PPS_INTMIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	pps_intcnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142)  * pps_clear - Clears the PPS state variables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) static inline void pps_clear(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	pps_reset_freq_interval();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	pps_tf[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	pps_tf[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	pps_tf[2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	pps_fbase.tv_sec = pps_fbase.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	pps_freq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) /* Decrease pps_valid to indicate that another second has passed since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155)  * the last PPS signal. When it reaches 0, indicate that PPS signal is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156)  * missing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) static inline void pps_dec_valid(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	if (pps_valid > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		pps_valid--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 				 STA_PPSWANDER | STA_PPSERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		pps_clear();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) static inline void pps_set_freq(s64 freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	pps_freq = freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) static inline int is_error_status(int status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	return (status & (STA_UNSYNC|STA_CLOCKERR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		/* PPS signal lost when either PPS time or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		 * PPS frequency synchronization requested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		|| ((status & (STA_PPSFREQ|STA_PPSTIME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 			&& !(status & STA_PPSSIGNAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		/* PPS jitter exceeded when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		 * PPS time synchronization requested */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		|| ((status & (STA_PPSTIME|STA_PPSJITTER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 			== (STA_PPSTIME|STA_PPSJITTER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		/* PPS wander exceeded or calibration error when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		 * PPS frequency synchronization requested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		|| ((status & STA_PPSFREQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 			&& (status & (STA_PPSWANDER|STA_PPSERROR)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) static inline void pps_fill_timex(struct __kernel_timex *txc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	txc->ppsfreq	   = shift_right((pps_freq >> PPM_SCALE_INV_SHIFT) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 					 PPM_SCALE_INV, NTP_SCALE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	txc->jitter	   = pps_jitter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	if (!(time_status & STA_NANO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		txc->jitter = pps_jitter / NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	txc->shift	   = pps_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	txc->stabil	   = pps_stabil;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	txc->jitcnt	   = pps_jitcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	txc->calcnt	   = pps_calcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	txc->errcnt	   = pps_errcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	txc->stbcnt	   = pps_stbcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) #else /* !CONFIG_NTP_PPS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) static inline s64 ntp_offset_chunk(s64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	return shift_right(offset, SHIFT_PLL + time_constant);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) static inline void pps_reset_freq_interval(void) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) static inline void pps_clear(void) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) static inline void pps_dec_valid(void) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) static inline void pps_set_freq(s64 freq) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) static inline int is_error_status(int status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	return status & (STA_UNSYNC|STA_CLOCKERR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) static inline void pps_fill_timex(struct __kernel_timex *txc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	/* PPS is not implemented, so these are zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	txc->ppsfreq	   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	txc->jitter	   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	txc->shift	   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	txc->stabil	   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	txc->jitcnt	   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	txc->calcnt	   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	txc->errcnt	   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	txc->stbcnt	   = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) #endif /* CONFIG_NTP_PPS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242)  * ntp_synced - Returns 1 if the NTP status is not UNSYNC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) static inline int ntp_synced(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	return !(time_status & STA_UNSYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252)  * NTP methods:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256)  * Update (tick_length, tick_length_base, tick_nsec), based
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257)  * on (tick_usec, ntp_tick_adj, time_freq):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) static void ntp_update_frequency(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	u64 second_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	u64 new_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	second_length		 = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 						<< NTP_SCALE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	second_length		+= ntp_tick_adj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	second_length		+= time_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	tick_nsec		 = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	new_base		 = div_u64(second_length, NTP_INTERVAL_FREQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	 * Don't wait for the next second_overflow, apply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	 * the change to the tick length immediately:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	tick_length		+= new_base - tick_length_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	tick_length_base	 = new_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) static inline s64 ntp_update_offset_fll(s64 offset64, long secs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	time_status &= ~STA_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	if (secs < MINSEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	if (!(time_status & STA_FLL) && (secs <= MAXSEC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	time_status |= STA_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	return div64_long(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) static void ntp_update_offset(long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	s64 freq_adj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	s64 offset64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	long secs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	if (!(time_status & STA_PLL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	if (!(time_status & STA_NANO)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		/* Make sure the multiplication below won't overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		offset = clamp(offset, -USEC_PER_SEC, USEC_PER_SEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		offset *= NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	 * Scale the phase adjustment and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	 * clamp to the operating range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	offset = clamp(offset, -MAXPHASE, MAXPHASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	 * Select how the frequency is to be controlled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	 * and in which mode (PLL or FLL).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	secs = (long)(__ktime_get_real_seconds() - time_reftime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	if (unlikely(time_status & STA_FREQHOLD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		secs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	time_reftime = __ktime_get_real_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	offset64    = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	freq_adj    = ntp_update_offset_fll(offset64, secs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	 * Clamp update interval to reduce PLL gain with low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	 * sampling rate (e.g. intermittent network connection)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	 * to avoid instability.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	if (unlikely(secs > 1 << (SHIFT_PLL + 1 + time_constant)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		secs = 1 << (SHIFT_PLL + 1 + time_constant);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	freq_adj    += (offset64 * secs) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 			(NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	freq_adj    = min(freq_adj + time_freq, MAXFREQ_SCALED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	time_freq   = max(freq_adj, -MAXFREQ_SCALED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	time_offset = div_s64(offset64 << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349)  * ntp_clear - Clears the NTP state variables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) void ntp_clear(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	time_adjust	= 0;		/* stop active adjtime() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	time_status	|= STA_UNSYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	time_maxerror	= NTP_PHASE_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	time_esterror	= NTP_PHASE_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	ntp_update_frequency();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	tick_length	= tick_length_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	time_offset	= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	ntp_next_leap_sec = TIME64_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	/* Clear PPS state variables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	pps_clear();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) u64 ntp_tick_length(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	return tick_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375)  * ntp_get_next_leap - Returns the next leapsecond in CLOCK_REALTIME ktime_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377)  * Provides the time of the next leapsecond against CLOCK_REALTIME in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378)  * a ktime_t format. Returns KTIME_MAX if no leapsecond is pending.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) ktime_t ntp_get_next_leap(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	ktime_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	if ((time_state == TIME_INS) && (time_status & STA_INS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		return ktime_set(ntp_next_leap_sec, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	ret = KTIME_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391)  * this routine handles the overflow of the microsecond field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393)  * The tricky bits of code to handle the accurate clock support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394)  * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395)  * They were originally developed for SUN and DEC kernels.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396)  * All the kudos should go to Dave for this stuff.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398)  * Also handles leap second processing, and returns leap offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) int second_overflow(time64_t secs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	s64 delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	int leap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	s32 rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	 * Leap second processing. If in leap-insert state at the end of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	 * day, the system clock is set back one second; if in leap-delete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	 * state, the system clock is set ahead one second.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	switch (time_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	case TIME_OK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		if (time_status & STA_INS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 			time_state = TIME_INS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 			div_s64_rem(secs, SECS_PER_DAY, &rem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 			ntp_next_leap_sec = secs + SECS_PER_DAY - rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		} else if (time_status & STA_DEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 			time_state = TIME_DEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 			div_s64_rem(secs + 1, SECS_PER_DAY, &rem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 			ntp_next_leap_sec = secs + SECS_PER_DAY - rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	case TIME_INS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		if (!(time_status & STA_INS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 			ntp_next_leap_sec = TIME64_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 			time_state = TIME_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		} else if (secs == ntp_next_leap_sec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 			leap = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 			time_state = TIME_OOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 			printk(KERN_NOTICE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 				"Clock: inserting leap second 23:59:60 UTC\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	case TIME_DEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		if (!(time_status & STA_DEL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 			ntp_next_leap_sec = TIME64_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 			time_state = TIME_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		} else if (secs == ntp_next_leap_sec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 			leap = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 			ntp_next_leap_sec = TIME64_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 			time_state = TIME_WAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 			printk(KERN_NOTICE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 				"Clock: deleting leap second 23:59:59 UTC\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	case TIME_OOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		ntp_next_leap_sec = TIME64_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		time_state = TIME_WAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	case TIME_WAIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		if (!(time_status & (STA_INS | STA_DEL)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 			time_state = TIME_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	/* Bump the maxerror field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	time_maxerror += MAXFREQ / NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	if (time_maxerror > NTP_PHASE_LIMIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		time_maxerror = NTP_PHASE_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		time_status |= STA_UNSYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	/* Compute the phase adjustment for the next second */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	tick_length	 = tick_length_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	delta		 = ntp_offset_chunk(time_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	time_offset	-= delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	tick_length	+= delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	/* Check PPS signal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	pps_dec_valid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	if (!time_adjust)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	if (time_adjust > MAX_TICKADJ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		time_adjust -= MAX_TICKADJ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		tick_length += MAX_TICKADJ_SCALED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	if (time_adjust < -MAX_TICKADJ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		time_adjust += MAX_TICKADJ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		tick_length -= MAX_TICKADJ_SCALED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	tick_length += (s64)(time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 							 << NTP_SCALE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	time_adjust = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	return leap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) static void sync_hw_clock(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) static DECLARE_DELAYED_WORK(sync_work, sync_hw_clock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) static void sched_sync_hw_clock(struct timespec64 now,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 				unsigned long target_nsec, bool fail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	struct timespec64 next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	ktime_get_real_ts64(&next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	if (!fail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		next.tv_sec = 659;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		 * Try again as soon as possible. Delaying long periods
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		 * decreases the accuracy of the work queue timer. Due to this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		 * the algorithm is very likely to require a short-sleep retry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		 * after the above long sleep to synchronize ts_nsec.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		next.tv_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	/* Compute the needed delay that will get to tv_nsec == target_nsec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	next.tv_nsec = target_nsec - next.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	if (next.tv_nsec <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		next.tv_nsec += NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	if (next.tv_nsec >= NSEC_PER_SEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		next.tv_sec++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		next.tv_nsec -= NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	queue_delayed_work(system_power_efficient_wq, &sync_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 			   timespec64_to_jiffies(&next));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) static void sync_rtc_clock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	unsigned long target_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	struct timespec64 adjust, now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	if (!IS_ENABLED(CONFIG_RTC_SYSTOHC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	ktime_get_real_ts64(&now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	adjust = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	if (persistent_clock_is_local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		adjust.tv_sec -= (sys_tz.tz_minuteswest * 60);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	 * The current RTC in use will provide the target_nsec it wants to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	 * called at, and does rtc_tv_nsec_ok internally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	rc = rtc_set_ntp_time(adjust, &target_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	if (rc == -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	sched_sync_hw_clock(now, target_nsec, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) #ifdef CONFIG_GENERIC_CMOS_UPDATE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) int __weak update_persistent_clock64(struct timespec64 now64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) static bool sync_cmos_clock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	static bool no_cmos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	struct timespec64 now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	struct timespec64 adjust;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	int rc = -EPROTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	long target_nsec = NSEC_PER_SEC / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	if (!IS_ENABLED(CONFIG_GENERIC_CMOS_UPDATE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	if (no_cmos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	 * Historically update_persistent_clock64() has followed x86
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	 * semantics, which match the MC146818A/etc RTC. This RTC will store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	 * 'adjust' and then in .5s it will advance once second.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	 * Architectures are strongly encouraged to use rtclib and not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	 * implement this legacy API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	ktime_get_real_ts64(&now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	if (rtc_tv_nsec_ok(-1 * target_nsec, &adjust, &now)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		if (persistent_clock_is_local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 			adjust.tv_sec -= (sys_tz.tz_minuteswest * 60);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		rc = update_persistent_clock64(adjust);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		 * The machine does not support update_persistent_clock64 even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		 * though it defines CONFIG_GENERIC_CMOS_UPDATE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		if (rc == -ENODEV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 			no_cmos = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	sched_sync_hw_clock(now, target_nsec, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607)  * If we have an externally synchronized Linux clock, then update RTC clock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608)  * accordingly every ~11 minutes. Generally RTCs can only store second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609)  * precision, but many RTCs will adjust the phase of their second tick to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610)  * match the moment of update. This infrastructure arranges to call to the RTC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611)  * set at the correct moment to phase synchronize the RTC second tick over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612)  * with the kernel clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) static void sync_hw_clock(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	if (!ntp_synced())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	if (sync_cmos_clock())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	sync_rtc_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) void ntp_notify_cmos_timer(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	if (!ntp_synced())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	if (IS_ENABLED(CONFIG_GENERIC_CMOS_UPDATE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	    IS_ENABLED(CONFIG_RTC_SYSTOHC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		queue_delayed_work(system_power_efficient_wq, &sync_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636)  * Propagate a new txc->status value into the NTP state:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) static inline void process_adj_status(const struct __kernel_timex *txc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	if ((time_status & STA_PLL) && !(txc->status & STA_PLL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		time_state = TIME_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		time_status = STA_UNSYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		ntp_next_leap_sec = TIME64_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		/* restart PPS frequency calibration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		pps_reset_freq_interval();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	 * If we turn on PLL adjustments then reset the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	 * reference time to current time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	if (!(time_status & STA_PLL) && (txc->status & STA_PLL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		time_reftime = __ktime_get_real_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	/* only set allowed bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	time_status &= STA_RONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	time_status |= txc->status & ~STA_RONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) static inline void process_adjtimex_modes(const struct __kernel_timex *txc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 					  s32 *time_tai)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	if (txc->modes & ADJ_STATUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		process_adj_status(txc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	if (txc->modes & ADJ_NANO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		time_status |= STA_NANO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	if (txc->modes & ADJ_MICRO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		time_status &= ~STA_NANO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	if (txc->modes & ADJ_FREQUENCY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		time_freq = txc->freq * PPM_SCALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		time_freq = min(time_freq, MAXFREQ_SCALED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		time_freq = max(time_freq, -MAXFREQ_SCALED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		/* update pps_freq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		pps_set_freq(time_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	if (txc->modes & ADJ_MAXERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		time_maxerror = txc->maxerror;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	if (txc->modes & ADJ_ESTERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		time_esterror = txc->esterror;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	if (txc->modes & ADJ_TIMECONST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		time_constant = txc->constant;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		if (!(time_status & STA_NANO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 			time_constant += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		time_constant = min(time_constant, (long)MAXTC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		time_constant = max(time_constant, 0l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	if (txc->modes & ADJ_TAI &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 			txc->constant >= 0 && txc->constant <= MAX_TAI_OFFSET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		*time_tai = txc->constant;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	if (txc->modes & ADJ_OFFSET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		ntp_update_offset(txc->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	if (txc->modes & ADJ_TICK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		tick_usec = txc->tick;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		ntp_update_frequency();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711)  * adjtimex mainly allows reading (and writing, if superuser) of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712)  * kernel time-keeping variables. used by xntpd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) int __do_adjtimex(struct __kernel_timex *txc, const struct timespec64 *ts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		  s32 *time_tai, struct audit_ntp_data *ad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	if (txc->modes & ADJ_ADJTIME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		long save_adjust = time_adjust;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		if (!(txc->modes & ADJ_OFFSET_READONLY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 			/* adjtime() is independent from ntp_adjtime() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 			time_adjust = txc->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 			ntp_update_frequency();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 			audit_ntp_set_old(ad, AUDIT_NTP_ADJUST,	save_adjust);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 			audit_ntp_set_new(ad, AUDIT_NTP_ADJUST,	time_adjust);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		txc->offset = save_adjust;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		/* If there are input parameters, then process them: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		if (txc->modes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 			audit_ntp_set_old(ad, AUDIT_NTP_OFFSET,	time_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 			audit_ntp_set_old(ad, AUDIT_NTP_FREQ,	time_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 			audit_ntp_set_old(ad, AUDIT_NTP_STATUS,	time_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 			audit_ntp_set_old(ad, AUDIT_NTP_TAI,	*time_tai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 			audit_ntp_set_old(ad, AUDIT_NTP_TICK,	tick_usec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 			process_adjtimex_modes(txc, time_tai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 			audit_ntp_set_new(ad, AUDIT_NTP_OFFSET,	time_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 			audit_ntp_set_new(ad, AUDIT_NTP_FREQ,	time_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 			audit_ntp_set_new(ad, AUDIT_NTP_STATUS,	time_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 			audit_ntp_set_new(ad, AUDIT_NTP_TAI,	*time_tai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 			audit_ntp_set_new(ad, AUDIT_NTP_TICK,	tick_usec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 				  NTP_SCALE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		if (!(time_status & STA_NANO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			txc->offset = (u32)txc->offset / NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	result = time_state;	/* mostly `TIME_OK' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	/* check for errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	if (is_error_status(time_status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		result = TIME_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	txc->freq	   = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 					 PPM_SCALE_INV, NTP_SCALE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	txc->maxerror	   = time_maxerror;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	txc->esterror	   = time_esterror;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	txc->status	   = time_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	txc->constant	   = time_constant;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	txc->precision	   = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	txc->tolerance	   = MAXFREQ_SCALED / PPM_SCALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	txc->tick	   = tick_usec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	txc->tai	   = *time_tai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	/* fill PPS status fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	pps_fill_timex(txc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	txc->time.tv_sec = ts->tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	txc->time.tv_usec = ts->tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	if (!(time_status & STA_NANO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		txc->time.tv_usec = ts->tv_nsec / NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	/* Handle leapsec adjustments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	if (unlikely(ts->tv_sec >= ntp_next_leap_sec)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		if ((time_state == TIME_INS) && (time_status & STA_INS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 			result = TIME_OOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 			txc->tai++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 			txc->time.tv_sec--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		if ((time_state == TIME_DEL) && (time_status & STA_DEL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 			result = TIME_WAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 			txc->tai--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 			txc->time.tv_sec++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		if ((time_state == TIME_OOP) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 					(ts->tv_sec == ntp_next_leap_sec)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 			result = TIME_WAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) #ifdef	CONFIG_NTP_PPS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) /* actually struct pps_normtime is good old struct timespec, but it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803)  * semantically different (and it is the reason why it was invented):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804)  * pps_normtime.nsec has a range of ( -NSEC_PER_SEC / 2, NSEC_PER_SEC / 2 ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805)  * while timespec.tv_nsec has a range of [0, NSEC_PER_SEC) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) struct pps_normtime {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	s64		sec;	/* seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	long		nsec;	/* nanoseconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) /* normalize the timestamp so that nsec is in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812)    ( -NSEC_PER_SEC / 2, NSEC_PER_SEC / 2 ] interval */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) static inline struct pps_normtime pps_normalize_ts(struct timespec64 ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	struct pps_normtime norm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		.sec = ts.tv_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		.nsec = ts.tv_nsec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	if (norm.nsec > (NSEC_PER_SEC >> 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		norm.nsec -= NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		norm.sec++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	return norm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) /* get current phase correction and jitter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) static inline long pps_phase_filter_get(long *jitter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	*jitter = pps_tf[0] - pps_tf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	if (*jitter < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		*jitter = -*jitter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	/* TODO: test various filters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	return pps_tf[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) /* add the sample to the phase filter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) static inline void pps_phase_filter_add(long err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	pps_tf[2] = pps_tf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	pps_tf[1] = pps_tf[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	pps_tf[0] = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) /* decrease frequency calibration interval length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848)  * It is halved after four consecutive unstable intervals.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) static inline void pps_dec_freq_interval(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	if (--pps_intcnt <= -PPS_INTCOUNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		pps_intcnt = -PPS_INTCOUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		if (pps_shift > PPS_INTMIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 			pps_shift--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 			pps_intcnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) /* increase frequency calibration interval length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862)  * It is doubled after four consecutive stable intervals.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) static inline void pps_inc_freq_interval(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	if (++pps_intcnt >= PPS_INTCOUNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		pps_intcnt = PPS_INTCOUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		if (pps_shift < PPS_INTMAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 			pps_shift++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			pps_intcnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) /* update clock frequency based on MONOTONIC_RAW clock PPS signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876)  * timestamps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878)  * At the end of the calibration interval the difference between the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879)  * first and last MONOTONIC_RAW clock timestamps divided by the length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880)  * of the interval becomes the frequency update. If the interval was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881)  * too long, the data are discarded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882)  * Returns the difference between old and new frequency values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) static long hardpps_update_freq(struct pps_normtime freq_norm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	long delta, delta_mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	s64 ftemp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	/* check if the frequency interval was too long */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	if (freq_norm.sec > (2 << pps_shift)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		time_status |= STA_PPSERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		pps_errcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		pps_dec_freq_interval();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		printk_deferred(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 			"hardpps: PPSERROR: interval too long - %lld s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 			freq_norm.sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	/* here the raw frequency offset and wander (stability) is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	 * calculated. If the wander is less than the wander threshold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	 * the interval is increased; otherwise it is decreased.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	ftemp = div_s64(((s64)(-freq_norm.nsec)) << NTP_SCALE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 			freq_norm.sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	delta = shift_right(ftemp - pps_freq, NTP_SCALE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	pps_freq = ftemp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	if (delta > PPS_MAXWANDER || delta < -PPS_MAXWANDER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		printk_deferred(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 				"hardpps: PPSWANDER: change=%ld\n", delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		time_status |= STA_PPSWANDER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		pps_stbcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		pps_dec_freq_interval();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	} else {	/* good sample */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		pps_inc_freq_interval();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	/* the stability metric is calculated as the average of recent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	 * frequency changes, but is used only for performance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	 * monitoring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	delta_mod = delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	if (delta_mod < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		delta_mod = -delta_mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	pps_stabil += (div_s64(((s64)delta_mod) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 				(NTP_SCALE_SHIFT - SHIFT_USEC),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 				NSEC_PER_USEC) - pps_stabil) >> PPS_INTMIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	/* if enabled, the system clock frequency is updated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	if ((time_status & STA_PPSFREQ) != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	    (time_status & STA_FREQHOLD) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		time_freq = pps_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		ntp_update_frequency();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	return delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) /* correct REALTIME clock phase error against PPS signal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) static void hardpps_update_phase(long error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	long correction = -error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	long jitter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	/* add the sample to the median filter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	pps_phase_filter_add(correction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	correction = pps_phase_filter_get(&jitter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	/* Nominal jitter is due to PPS signal noise. If it exceeds the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	 * threshold, the sample is discarded; otherwise, if so enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	 * the time offset is updated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	if (jitter > (pps_jitter << PPS_POPCORN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		printk_deferred(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 				"hardpps: PPSJITTER: jitter=%ld, limit=%ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 				jitter, (pps_jitter << PPS_POPCORN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		time_status |= STA_PPSJITTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		pps_jitcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	} else if (time_status & STA_PPSTIME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		/* correct the time using the phase offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		time_offset = div_s64(((s64)correction) << NTP_SCALE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 				NTP_INTERVAL_FREQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		/* cancel running adjtime() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		time_adjust = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	/* update jitter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	pps_jitter += (jitter - pps_jitter) >> PPS_INTMIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971)  * __hardpps() - discipline CPU clock oscillator to external PPS signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973)  * This routine is called at each PPS signal arrival in order to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974)  * discipline the CPU clock oscillator to the PPS signal. It takes two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975)  * parameters: REALTIME and MONOTONIC_RAW clock timestamps. The former
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976)  * is used to correct clock phase error and the latter is used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977)  * correct the frequency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979)  * This code is based on David Mills's reference nanokernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980)  * implementation. It was mostly rewritten but keeps the same idea.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) void __hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	struct pps_normtime pts_norm, freq_norm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	pts_norm = pps_normalize_ts(*phase_ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	/* clear the error bits, they will be set again if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	/* indicate signal presence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	time_status |= STA_PPSSIGNAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	pps_valid = PPS_VALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	/* when called for the first time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	 * just start the frequency interval */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	if (unlikely(pps_fbase.tv_sec == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		pps_fbase = *raw_ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	/* ok, now we have a base for frequency calculation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	freq_norm = pps_normalize_ts(timespec64_sub(*raw_ts, pps_fbase));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	/* check that the signal is in the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	 * [1s - MAXFREQ us, 1s + MAXFREQ us], otherwise reject it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	if ((freq_norm.sec == 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 			(freq_norm.nsec > MAXFREQ * freq_norm.sec) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 			(freq_norm.nsec < -MAXFREQ * freq_norm.sec)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		time_status |= STA_PPSJITTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		/* restart the frequency calibration interval */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		pps_fbase = *raw_ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		printk_deferred(KERN_ERR "hardpps: PPSJITTER: bad pulse\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	/* signal is ok */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	/* check if the current frequency interval is finished */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	if (freq_norm.sec >= (1 << pps_shift)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		pps_calcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		/* restart the frequency calibration interval */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		pps_fbase = *raw_ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		hardpps_update_freq(freq_norm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	hardpps_update_phase(pts_norm.nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) #endif	/* CONFIG_NTP_PPS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) static int __init ntp_tick_adj_setup(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	int rc = kstrtos64(str, 0, &ntp_tick_adj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	ntp_tick_adj <<= NTP_SCALE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) __setup("ntp_tick_adj=", ntp_tick_adj_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) void __init ntp_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	ntp_clear();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) }