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