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