xref: /freebsd/contrib/ntp/kernel/sys/timex.h (revision c0b746e5e8d9479f05b3749cbf1f73b8928719bd)
1c0b746e5SOllivier Robert /******************************************************************************
2c0b746e5SOllivier Robert  *                                                                            *
3c0b746e5SOllivier Robert  * Copyright (c) David L. Mills 1993, 1994                                    *
4c0b746e5SOllivier Robert  *                                                                            *
5c0b746e5SOllivier Robert  * Permission to use, copy, modify, and distribute this software and its      *
6c0b746e5SOllivier Robert  * documentation for any purpose and without fee is hereby granted, provided  *
7c0b746e5SOllivier Robert  * that the above copyright notice appears in all copies and that both the    *
8c0b746e5SOllivier Robert  * copyright notice and this permission notice appear in supporting           *
9c0b746e5SOllivier Robert  * documentation, and that the name University of Delaware not be used in     *
10c0b746e5SOllivier Robert  * advertising or publicity pertaining to distribution of the software        *
11c0b746e5SOllivier Robert  * without specific, written prior permission.  The University of Delaware    *
12c0b746e5SOllivier Robert  * makes no representations about the suitability this software for any       *
13c0b746e5SOllivier Robert  * purpose.  It is provided "as is" without express or implied warranty.      *
14c0b746e5SOllivier Robert  *                                                                            *
15c0b746e5SOllivier Robert  ******************************************************************************/
16c0b746e5SOllivier Robert 
17c0b746e5SOllivier Robert /*
18c0b746e5SOllivier Robert  * Modification history timex.h
19c0b746e5SOllivier Robert  *
20c0b746e5SOllivier Robert  * 26 Sep 94	David L. Mills
21c0b746e5SOllivier Robert  *	Added defines for hybrid phase/frequency-lock loop.
22c0b746e5SOllivier Robert  *
23c0b746e5SOllivier Robert  * 19 Mar 94	David L. Mills
24c0b746e5SOllivier Robert  *	Moved defines from kernel routines to header file and added new
25c0b746e5SOllivier Robert  *	defines for PPS phase-lock loop.
26c0b746e5SOllivier Robert  *
27c0b746e5SOllivier Robert  * 20 Feb 94	David L. Mills
28c0b746e5SOllivier Robert  *	Revised status codes and structures for external clock and PPS
29c0b746e5SOllivier Robert  *	signal discipline.
30c0b746e5SOllivier Robert  *
31c0b746e5SOllivier Robert  * 28 Nov 93	David L. Mills
32c0b746e5SOllivier Robert  *	Adjusted parameters to improve stability and increase poll
33c0b746e5SOllivier Robert  *	interval.
34c0b746e5SOllivier Robert  *
35c0b746e5SOllivier Robert  * 17 Sep 93    David L. Mills
36c0b746e5SOllivier Robert  *      Created file
37c0b746e5SOllivier Robert  */
38c0b746e5SOllivier Robert /*
39c0b746e5SOllivier Robert  * This header file defines the Network Time Protocol (NTP) interfaces
40c0b746e5SOllivier Robert  * for user and daemon application programs. These are implemented using
41c0b746e5SOllivier Robert  * private syscalls and data structures and require specific kernel
42c0b746e5SOllivier Robert  * support.
43c0b746e5SOllivier Robert  *
44c0b746e5SOllivier Robert  * NAME
45c0b746e5SOllivier Robert  *	ntp_gettime - NTP user application interface
46c0b746e5SOllivier Robert  *
47c0b746e5SOllivier Robert  * SYNOPSIS
48c0b746e5SOllivier Robert  *	#include <sys/timex.h>
49c0b746e5SOllivier Robert  *
50c0b746e5SOllivier Robert  *	int syscall(SYS_ntp_gettime, tptr)
51c0b746e5SOllivier Robert  *
52c0b746e5SOllivier Robert  *	int SYS_ntp_gettime		defined in syscall.h header file
53c0b746e5SOllivier Robert  *	struct ntptimeval *tptr		pointer to ntptimeval structure
54c0b746e5SOllivier Robert  *
55c0b746e5SOllivier Robert  * NAME
56c0b746e5SOllivier Robert  *	ntp_adjtime - NTP daemon application interface
57c0b746e5SOllivier Robert  *
58c0b746e5SOllivier Robert  * SYNOPSIS
59c0b746e5SOllivier Robert  *	#include <sys/timex.h>
60c0b746e5SOllivier Robert  *
61c0b746e5SOllivier Robert  *	int syscall(SYS_ntp_adjtime, mode, tptr)
62c0b746e5SOllivier Robert  *
63c0b746e5SOllivier Robert  *	int SYS_ntp_adjtime		defined in syscall.h header file
64c0b746e5SOllivier Robert  *	struct timex *tptr		pointer to timex structure
65c0b746e5SOllivier Robert  *
66c0b746e5SOllivier Robert  */
67c0b746e5SOllivier Robert #ifndef _SYS_TIMEX_H_
68c0b746e5SOllivier Robert #define _SYS_TIMEX_H_ 1
69c0b746e5SOllivier Robert 
70c0b746e5SOllivier Robert #ifndef MSDOS			/* Microsoft specific */
71c0b746e5SOllivier Robert #include <sys/syscall.h>
72c0b746e5SOllivier Robert #endif /* MSDOS */
73c0b746e5SOllivier Robert 
74c0b746e5SOllivier Robert /*
75c0b746e5SOllivier Robert  * The following defines establish the engineering parameters of the
76c0b746e5SOllivier Robert  * phase-lock loop (PLL) model used in the kernel implementation. These
77c0b746e5SOllivier Robert  * parameters have been carefully chosen by analysis for good stability
78c0b746e5SOllivier Robert  * and wide dynamic range.
79c0b746e5SOllivier Robert  *
80c0b746e5SOllivier Robert  * The hz variable is defined in the kernel build environment. It
81c0b746e5SOllivier Robert  * establishes the timer interrupt frequency, 100 Hz for the SunOS
82c0b746e5SOllivier Robert  * kernel, 256 Hz for the Ultrix kernel and 1024 Hz for the OSF/1
83c0b746e5SOllivier Robert  * kernel. SHIFT_HZ expresses the same value as the nearest power of two
84c0b746e5SOllivier Robert  * in order to avoid hardware multiply operations.
85c0b746e5SOllivier Robert  *
86c0b746e5SOllivier Robert  * SHIFT_KG and SHIFT_KF establish the damping of the PLL and are chosen
87c0b746e5SOllivier Robert  * for a slightly underdamped convergence characteristic. SHIFT_KH
88c0b746e5SOllivier Robert  * establishes the damping of the FLL and is chosen by wisdom and black
89c0b746e5SOllivier Robert  * art.
90c0b746e5SOllivier Robert  *
91c0b746e5SOllivier Robert  * MAXTC establishes the maximum time constant of the PLL. With the
92c0b746e5SOllivier Robert  * SHIFT_KG and SHIFT_KF values given and a time constant range from
93c0b746e5SOllivier Robert  * zero to MAXTC, the PLL will converge in 15 minutes to 16 hours,
94c0b746e5SOllivier Robert  * respectively.
95c0b746e5SOllivier Robert  */
96c0b746e5SOllivier Robert #define SHIFT_HZ 7		/* log2(hz) */
97c0b746e5SOllivier Robert #define SHIFT_KG 6		/* phase factor (shift) */
98c0b746e5SOllivier Robert #define SHIFT_KF 16		/* PLL frequency factor (shift) */
99c0b746e5SOllivier Robert #define SHIFT_KH 2		/* FLL frequency factor (shift) */
100c0b746e5SOllivier Robert #define MAXTC 6			/* maximum time constant (shift) */
101c0b746e5SOllivier Robert 
102c0b746e5SOllivier Robert /*
103c0b746e5SOllivier Robert  * The following defines establish the scaling of the various variables
104c0b746e5SOllivier Robert  * used by the PLL. They are chosen to allow the greatest precision
105c0b746e5SOllivier Robert  * possible without overflow of a 32-bit word.
106c0b746e5SOllivier Robert  *
107c0b746e5SOllivier Robert  * SHIFT_SCALE defines the scaling (shift) of the time_phase variable,
108c0b746e5SOllivier Robert  * which serves as a an extension to the low-order bits of the system
109c0b746e5SOllivier Robert  * clock variable time.tv_usec.
110c0b746e5SOllivier Robert  *
111c0b746e5SOllivier Robert  * SHIFT_UPDATE defines the scaling (shift) of the time_offset variable,
112c0b746e5SOllivier Robert  * which represents the current time offset with respect to standard
113c0b746e5SOllivier Robert  * time.
114c0b746e5SOllivier Robert  *
115c0b746e5SOllivier Robert  * SHIFT_USEC defines the scaling (shift) of the time_freq and
116c0b746e5SOllivier Robert  * time_tolerance variables, which represent the current frequency
117c0b746e5SOllivier Robert  * offset and maximum frequency tolerance.
118c0b746e5SOllivier Robert  *
119c0b746e5SOllivier Robert  * FINEUSEC is 1 us in SHIFT_UPDATE units of the time_phase variable.
120c0b746e5SOllivier Robert  */
121c0b746e5SOllivier Robert #define SHIFT_SCALE 22		/* phase scale (shift) */
122c0b746e5SOllivier Robert #define SHIFT_UPDATE (SHIFT_KG + MAXTC) /* time offset scale (shift) */
123c0b746e5SOllivier Robert #define SHIFT_USEC 16		/* frequency offset scale (shift) */
124c0b746e5SOllivier Robert #define FINEUSEC (1L << SHIFT_SCALE) /* 1 us in phase units */
125c0b746e5SOllivier Robert 
126c0b746e5SOllivier Robert /*
127c0b746e5SOllivier Robert  * The following defines establish the performance envelope of the PLL.
128c0b746e5SOllivier Robert  * They insure it operates within predefined limits, in order to satisfy
129c0b746e5SOllivier Robert  * correctness assertions. An excursion which exceeds these bounds is
130c0b746e5SOllivier Robert  * clamped to the bound and operation proceeds accordingly. In practice,
131c0b746e5SOllivier Robert  * this can occur only if something has failed or is operating out of
132c0b746e5SOllivier Robert  * tolerance, but otherwise the PLL continues to operate in a stable
133c0b746e5SOllivier Robert  * mode.
134c0b746e5SOllivier Robert  *
135c0b746e5SOllivier Robert  * MAXPHASE must be set greater than or equal to CLOCK.MAX (128 ms), as
136c0b746e5SOllivier Robert  * defined in the NTP specification. CLOCK.MAX establishes the maximum
137c0b746e5SOllivier Robert  * time offset allowed before the system time is reset, rather than
138c0b746e5SOllivier Robert  * incrementally adjusted. Here, the maximum offset is clamped to
139c0b746e5SOllivier Robert  * MAXPHASE only in order to prevent overflow errors due to defective
140c0b746e5SOllivier Robert  * protocol implementations.
141c0b746e5SOllivier Robert  *
142c0b746e5SOllivier Robert  * MAXFREQ is the maximum frequency tolerance of the CPU clock
143c0b746e5SOllivier Robert  * oscillator plus the maximum slew rate allowed by the protocol. It
144c0b746e5SOllivier Robert  * should be set to at least the frequency tolerance of the oscillator
145c0b746e5SOllivier Robert  * plus 100 ppm for vernier frequency adjustments. If the kernel
146c0b746e5SOllivier Robert  * PPS discipline code is configured (PPS_SYNC), the oscillator time and
147c0b746e5SOllivier Robert  * frequency are disciplined to an external source, presumably with
148c0b746e5SOllivier Robert  * negligible time and frequency error relative to UTC, and MAXFREQ can
149c0b746e5SOllivier Robert  * be reduced.
150c0b746e5SOllivier Robert  *
151c0b746e5SOllivier Robert  * MAXTIME is the maximum jitter tolerance of the PPS signal if the
152c0b746e5SOllivier Robert  * kernel PPS discipline code is configured (PPS_SYNC).
153c0b746e5SOllivier Robert  *
154c0b746e5SOllivier Robert  * MINSEC and MAXSEC define the lower and upper bounds on the interval
155c0b746e5SOllivier Robert  * between protocol updates.
156c0b746e5SOllivier Robert  */
157c0b746e5SOllivier Robert #define MAXPHASE 512000L	/* max phase error (us) */
158c0b746e5SOllivier Robert #ifdef PPS_SYNC
159c0b746e5SOllivier Robert #define MAXFREQ (512L << SHIFT_USEC) /* max freq error (100 ppm) */
160c0b746e5SOllivier Robert #define MAXTIME (200L << PPS_AVG) /* max PPS error (jitter) (200 us) */
161c0b746e5SOllivier Robert #else
162c0b746e5SOllivier Robert #define MAXFREQ (512L << SHIFT_USEC) /* max freq error (200 ppm) */
163c0b746e5SOllivier Robert #endif /* PPS_SYNC */
164c0b746e5SOllivier Robert #define MINSEC 16L		/* min interval between updates (s) */
165c0b746e5SOllivier Robert #define MAXSEC 1200L		/* max interval between updates (s) */
166c0b746e5SOllivier Robert 
167c0b746e5SOllivier Robert #ifdef PPS_SYNC
168c0b746e5SOllivier Robert /*
169c0b746e5SOllivier Robert  * The following defines are used only if a pulse-per-second (PPS)
170c0b746e5SOllivier Robert  * signal is available and connected via a modem control lead, such as
171c0b746e5SOllivier Robert  * produced by the optional ppsclock feature incorporated in the Sun
172c0b746e5SOllivier Robert  * asynch driver. They establish the design parameters of the frequency-
173c0b746e5SOllivier Robert  * lock loop used to discipline the CPU clock oscillator to the PPS
174c0b746e5SOllivier Robert  * signal.
175c0b746e5SOllivier Robert  *
176c0b746e5SOllivier Robert  * PPS_AVG is the averaging factor for the frequency loop, as well as
177c0b746e5SOllivier Robert  * the time and frequency dispersion.
178c0b746e5SOllivier Robert  *
179c0b746e5SOllivier Robert  * PPS_SHIFT and PPS_SHIFTMAX specify the minimum and maximum
180c0b746e5SOllivier Robert  * calibration intervals, respectively, in seconds as a power of two.
181c0b746e5SOllivier Robert  *
182c0b746e5SOllivier Robert  * PPS_VALID is the maximum interval before the PPS signal is considered
183c0b746e5SOllivier Robert  * invalid and protocol updates used directly instead.
184c0b746e5SOllivier Robert  *
185c0b746e5SOllivier Robert  * MAXGLITCH is the maximum interval before a time offset of more than
186c0b746e5SOllivier Robert  * MAXTIME is believed.
187c0b746e5SOllivier Robert  */
188c0b746e5SOllivier Robert #define PPS_AVG 2		/* pps averaging constant (shift) */
189c0b746e5SOllivier Robert #define PPS_SHIFT 2		/* min interval duration (s) (shift) */
190c0b746e5SOllivier Robert #define PPS_SHIFTMAX 8		/* max interval duration (s) (shift) */
191c0b746e5SOllivier Robert #define PPS_VALID 120		/* pps signal watchdog max (s) */
192c0b746e5SOllivier Robert #define MAXGLITCH 30		/* pps signal glitch max (s) */
193c0b746e5SOllivier Robert #endif /* PPS_SYNC */
194c0b746e5SOllivier Robert 
195c0b746e5SOllivier Robert /*
196c0b746e5SOllivier Robert  * The following defines and structures define the user interface for
197c0b746e5SOllivier Robert  * the ntp_gettime() and ntp_adjtime() system calls.
198c0b746e5SOllivier Robert  *
199c0b746e5SOllivier Robert  * Control mode codes (timex.modes)
200c0b746e5SOllivier Robert  */
201c0b746e5SOllivier Robert #define MOD_OFFSET	0x0001	/* set time offset */
202c0b746e5SOllivier Robert #define MOD_FREQUENCY	0x0002	/* set frequency offset */
203c0b746e5SOllivier Robert #define MOD_MAXERROR	0x0004	/* set maximum time error */
204c0b746e5SOllivier Robert #define MOD_ESTERROR	0x0008	/* set estimated time error */
205c0b746e5SOllivier Robert #define MOD_STATUS	0x0010	/* set clock status bits */
206c0b746e5SOllivier Robert #define MOD_TIMECONST	0x0020	/* set pll time constant */
207c0b746e5SOllivier Robert #define MOD_CANSCALE	0x0040	/* kernel can scale offset field */
208c0b746e5SOllivier Robert #define MOD_DOSCALE	0x0080	/* userland wants to scale offset field */
209c0b746e5SOllivier Robert 
210c0b746e5SOllivier Robert /*
211c0b746e5SOllivier Robert  * Status codes (timex.status)
212c0b746e5SOllivier Robert  */
213c0b746e5SOllivier Robert #define STA_PLL		0x0001	/* enable PLL updates (rw) */
214c0b746e5SOllivier Robert #define STA_PPSFREQ	0x0002	/* enable PPS freq discipline (rw) */
215c0b746e5SOllivier Robert #define STA_PPSTIME	0x0004	/* enable PPS time discipline (rw) */
216c0b746e5SOllivier Robert #define STA_FLL		0x0008	/* select frequency-lock mode (rw) */
217c0b746e5SOllivier Robert 
218c0b746e5SOllivier Robert #define STA_INS		0x0010	/* insert leap (rw) */
219c0b746e5SOllivier Robert #define STA_DEL		0x0020	/* delete leap (rw) */
220c0b746e5SOllivier Robert #define STA_UNSYNC	0x0040	/* clock unsynchronized (rw) */
221c0b746e5SOllivier Robert #define STA_FREQHOLD	0x0080	/* hold frequency (rw) */
222c0b746e5SOllivier Robert 
223c0b746e5SOllivier Robert #define STA_PPSSIGNAL	0x0100	/* PPS signal present (ro) */
224c0b746e5SOllivier Robert #define STA_PPSJITTER	0x0200	/* PPS signal jitter exceeded (ro) */
225c0b746e5SOllivier Robert #define STA_PPSWANDER	0x0400	/* PPS signal wander exceeded (ro) */
226c0b746e5SOllivier Robert #define STA_PPSERROR	0x0800	/* PPS signal calibration error (ro) */
227c0b746e5SOllivier Robert 
228c0b746e5SOllivier Robert #define STA_CLOCKERR	0x1000	/* clock hardware fault (ro) */
229c0b746e5SOllivier Robert 
230c0b746e5SOllivier Robert #define STA_RONLY (STA_PPSSIGNAL | STA_PPSJITTER | STA_PPSWANDER | \
231c0b746e5SOllivier Robert     STA_PPSERROR | STA_CLOCKERR) /* read-only bits */
232c0b746e5SOllivier Robert 
233c0b746e5SOllivier Robert /*
234c0b746e5SOllivier Robert  * Clock states (time_state)
235c0b746e5SOllivier Robert  */
236c0b746e5SOllivier Robert #define TIME_OK		0	/* no leap second warning */
237c0b746e5SOllivier Robert #define TIME_INS	1	/* insert leap second warning */
238c0b746e5SOllivier Robert #define TIME_DEL	2	/* delete leap second warning */
239c0b746e5SOllivier Robert #define TIME_OOP	3	/* leap second in progress */
240c0b746e5SOllivier Robert #define TIME_WAIT	4	/* leap second has occurred */
241c0b746e5SOllivier Robert #define TIME_ERROR	5	/* clock not synchronized */
242c0b746e5SOllivier Robert 
243c0b746e5SOllivier Robert /*
244c0b746e5SOllivier Robert  * NTP user interface (ntp_gettime()) - used to read kernel clock values
245c0b746e5SOllivier Robert  *
246c0b746e5SOllivier Robert  * Note: maximum error = NTP synch distance = dispersion + delay / 2;
247c0b746e5SOllivier Robert  * estimated error = NTP dispersion.
248c0b746e5SOllivier Robert  */
249c0b746e5SOllivier Robert struct ntptimeval {
250c0b746e5SOllivier Robert 	struct timeval time;	/* current time (ro) */
251c0b746e5SOllivier Robert 	long maxerror;		/* maximum error (us) (ro) */
252c0b746e5SOllivier Robert 	long esterror;		/* estimated error (us) (ro) */
253c0b746e5SOllivier Robert };
254c0b746e5SOllivier Robert 
255c0b746e5SOllivier Robert /*
256c0b746e5SOllivier Robert  * NTP daemon interface - (ntp_adjtime()) used to discipline CPU clock
257c0b746e5SOllivier Robert  * oscillator
258c0b746e5SOllivier Robert  */
259c0b746e5SOllivier Robert struct timex {
260c0b746e5SOllivier Robert 	unsigned int modes;	/* clock mode bits (wo) */
261c0b746e5SOllivier Robert 	long offset;		/* time offset (us) (rw) */
262c0b746e5SOllivier Robert 	long freq;		/* frequency offset (scaled ppm) (rw) */
263c0b746e5SOllivier Robert 	long maxerror;		/* maximum error (us) (rw) */
264c0b746e5SOllivier Robert 	long esterror;		/* estimated error (us) (rw) */
265c0b746e5SOllivier Robert 	int status;		/* clock status bits (rw) */
266c0b746e5SOllivier Robert 	long constant;		/* pll time constant (rw) */
267c0b746e5SOllivier Robert 	long precision;		/* clock precision (us) (ro) */
268c0b746e5SOllivier Robert 	long tolerance;		/* clock frequency tolerance (scaled
269c0b746e5SOllivier Robert 				 * ppm) (ro) */
270c0b746e5SOllivier Robert 	/*
271c0b746e5SOllivier Robert 	 * The following read-only structure members are implemented
272c0b746e5SOllivier Robert 	 * only if the PPS signal discipline is configured in the
273c0b746e5SOllivier Robert 	 * kernel.
274c0b746e5SOllivier Robert 	 */
275c0b746e5SOllivier Robert 	long ppsfreq;		/* pps frequency (scaled ppm) (ro) */
276c0b746e5SOllivier Robert 	long jitter;		/* pps jitter (us) (ro) */
277c0b746e5SOllivier Robert 	int shift;		/* interval duration (s) (shift) (ro) */
278c0b746e5SOllivier Robert 	long stabil;		/* pps stability (scaled ppm) (ro) */
279c0b746e5SOllivier Robert 	long jitcnt;		/* jitter limit exceeded (ro) */
280c0b746e5SOllivier Robert 	long calcnt;		/* calibration intervals (ro) */
281c0b746e5SOllivier Robert 	long errcnt;		/* calibration errors (ro) */
282c0b746e5SOllivier Robert 	long stbcnt;		/* stability limit exceeded (ro) */
283c0b746e5SOllivier Robert 
284c0b746e5SOllivier Robert };
285c0b746e5SOllivier Robert #ifdef __FreeBSD__
286c0b746e5SOllivier Robert 
287c0b746e5SOllivier Robert /*
288c0b746e5SOllivier Robert  * sysctl identifiers underneath kern.ntp_pll
289c0b746e5SOllivier Robert  */
290c0b746e5SOllivier Robert #define NTP_PLL_GETTIME	1	/* used by ntp_gettime() */
291c0b746e5SOllivier Robert #define NTP_PLL_MAXID	2	/* number of valid ids */
292c0b746e5SOllivier Robert 
293c0b746e5SOllivier Robert #define NTP_PLL_NAMES { \
294c0b746e5SOllivier Robert 			  { 0, 0 }, \
295c0b746e5SOllivier Robert 			  { "gettime", CTLTYPE_STRUCT }, \
296c0b746e5SOllivier Robert 		      }
297c0b746e5SOllivier Robert 
298c0b746e5SOllivier Robert #ifndef KERNEL
299c0b746e5SOllivier Robert #include <sys/cdefs.h>
300c0b746e5SOllivier Robert 
301c0b746e5SOllivier Robert __BEGIN_DECLS
302c0b746e5SOllivier Robert extern int ntp_gettime        __P((struct ntptimeval *));
303c0b746e5SOllivier Robert extern int ntp_adjtime        __P((struct timex *));
304c0b746e5SOllivier Robert __END_DECLS
305c0b746e5SOllivier Robert 
306c0b746e5SOllivier Robert #endif /* not KERNEL */
307c0b746e5SOllivier Robert 
308c0b746e5SOllivier Robert #endif /* __FreeBSD__ */
309c0b746e5SOllivier Robert #endif /* _SYS_TIMEX_H_ */
310