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