vserver 1.9.3
[linux-2.6.git] / include / linux / timex.h
1 /*****************************************************************************
2  *                                                                           *
3  * Copyright (c) David L. Mills 1993                                         *
4  *                                                                           *
5  * Permission to use, copy, modify, and distribute this software and its     *
6  * documentation for any purpose and without fee is hereby granted, provided *
7  * that the above copyright notice appears in all copies and that both the   *
8  * copyright notice and this permission notice appear in supporting          *
9  * documentation, and that the name University of Delaware not be used in    *
10  * advertising or publicity pertaining to distribution of the software       *
11  * without specific, written prior permission.  The University of Delaware   *
12  * makes no representations about the suitability this software for any      *
13  * purpose.  It is provided "as is" without express or implied warranty.     *
14  *                                                                           *
15  *****************************************************************************/
16
17 /*
18  * Modification history timex.h
19  *
20  * 29 Dec 97    Russell King
21  *      Moved CLOCK_TICK_RATE, CLOCK_TICK_FACTOR and FINETUNE to asm/timex.h
22  *      for ARM machines
23  *
24  *  9 Jan 97    Adrian Sun
25  *      Shifted LATCH define to allow access to alpha machines.
26  *
27  * 26 Sep 94    David L. Mills
28  *      Added defines for hybrid phase/frequency-lock loop.
29  *
30  * 19 Mar 94    David L. Mills
31  *      Moved defines from kernel routines to header file and added new
32  *      defines for PPS phase-lock loop.
33  *
34  * 20 Feb 94    David L. Mills
35  *      Revised status codes and structures for external clock and PPS
36  *      signal discipline.
37  *
38  * 28 Nov 93    David L. Mills
39  *      Adjusted parameters to improve stability and increase poll
40  *      interval.
41  *
42  * 17 Sep 93    David L. Mills
43  *      Created file $NTP/include/sys/timex.h
44  * 07 Oct 93    Torsten Duwe
45  *      Derived linux/timex.h
46  * 1995-08-13    Torsten Duwe
47  *      kernel PLL updated to 1994-12-13 specs (rfc-1589)
48  * 1997-08-30    Ulrich Windl
49  *      Added new constant NTP_PHASE_LIMIT
50  * 2004-08-12    Christoph Lameter
51  *      Reworked time interpolation logic
52  */
53 #ifndef _LINUX_TIMEX_H
54 #define _LINUX_TIMEX_H
55
56 #include <linux/config.h>
57 #include <linux/compiler.h>
58
59 #include <asm/param.h>
60
61 /*
62  * The following defines establish the engineering parameters of the PLL
63  * model. The HZ variable establishes the timer interrupt frequency, 100 Hz
64  * for the SunOS kernel, 256 Hz for the Ultrix kernel and 1024 Hz for the
65  * OSF/1 kernel. The SHIFT_HZ define expresses the same value as the
66  * nearest power of two in order to avoid hardware multiply operations.
67  */
68 #if HZ >= 12 && HZ < 24
69 # define SHIFT_HZ       4
70 #elif HZ >= 24 && HZ < 48
71 # define SHIFT_HZ       5
72 #elif HZ >= 48 && HZ < 96
73 # define SHIFT_HZ       6
74 #elif HZ >= 96 && HZ < 192
75 # define SHIFT_HZ       7
76 #elif HZ >= 192 && HZ < 384
77 # define SHIFT_HZ       8
78 #elif HZ >= 384 && HZ < 768
79 # define SHIFT_HZ       9
80 #elif HZ >= 768 && HZ < 1536
81 # define SHIFT_HZ       10
82 #else
83 # error You lose.
84 #endif
85
86 /*
87  * SHIFT_KG and SHIFT_KF establish the damping of the PLL and are chosen
88  * for a slightly underdamped convergence characteristic. SHIFT_KH
89  * establishes the damping of the FLL and is chosen by wisdom and black
90  * art.
91  *
92  * MAXTC establishes the maximum time constant of the PLL. With the
93  * SHIFT_KG and SHIFT_KF values given and a time constant range from
94  * zero to MAXTC, the PLL will converge in 15 minutes to 16 hours,
95  * respectively.
96  */
97 #define SHIFT_KG 6              /* phase factor (shift) */
98 #define SHIFT_KF 16             /* PLL frequency factor (shift) */
99 #define SHIFT_KH 2              /* FLL frequency factor (shift) */
100 #define MAXTC 6                 /* maximum time constant (shift) */
101
102 /*
103  * The SHIFT_SCALE define establishes the decimal point of the time_phase
104  * variable which serves as an extension to the low-order bits of the
105  * system clock variable. The SHIFT_UPDATE define establishes the decimal
106  * point of the time_offset variable which represents the current offset
107  * with respect to standard time. The FINENSEC define represents 1 nsec in
108  * scaled units.
109  *
110  * SHIFT_USEC defines the scaling (shift) of the time_freq and
111  * time_tolerance variables, which represent the current frequency
112  * offset and maximum frequency tolerance.
113  *
114  * FINENSEC is 1 ns in SHIFT_UPDATE units of the time_phase variable.
115  */
116 #define SHIFT_SCALE 22          /* phase scale (shift) */
117 #define SHIFT_UPDATE (SHIFT_KG + MAXTC) /* time offset scale (shift) */
118 #define SHIFT_USEC 16           /* frequency offset scale (shift) */
119 #define FINENSEC (1L << (SHIFT_SCALE - 10)) /* ~1 ns in phase units */
120
121 #define MAXPHASE 512000L        /* max phase error (us) */
122 #define MAXFREQ (512L << SHIFT_USEC)  /* max frequency error (ppm) */
123 #define MAXTIME (200L << PPS_AVG) /* max PPS error (jitter) (200 us) */
124 #define MINSEC 16L              /* min interval between updates (s) */
125 #define MAXSEC 1200L            /* max interval between updates (s) */
126 #define NTP_PHASE_LIMIT (MAXPHASE << 5) /* beyond max. dispersion */
127
128 /*
129  * The following defines are used only if a pulse-per-second (PPS)
130  * signal is available and connected via a modem control lead, such as
131  * produced by the optional ppsclock feature incorporated in the Sun
132  * asynch driver. They establish the design parameters of the frequency-
133  * lock loop used to discipline the CPU clock oscillator to the PPS
134  * signal.
135  *
136  * PPS_AVG is the averaging factor for the frequency loop, as well as
137  * the time and frequency dispersion.
138  *
139  * PPS_SHIFT and PPS_SHIFTMAX specify the minimum and maximum
140  * calibration intervals, respectively, in seconds as a power of two.
141  *
142  * PPS_VALID is the maximum interval before the PPS signal is considered
143  * invalid and protocol updates used directly instead.
144  *
145  * MAXGLITCH is the maximum interval before a time offset of more than
146  * MAXTIME is believed.
147  */
148 #define PPS_AVG 2               /* pps averaging constant (shift) */
149 #define PPS_SHIFT 2             /* min interval duration (s) (shift) */
150 #define PPS_SHIFTMAX 8          /* max interval duration (s) (shift) */
151 #define PPS_VALID 120           /* pps signal watchdog max (s) */
152 #define MAXGLITCH 30            /* pps signal glitch max (s) */
153
154 /*
155  * Pick up the architecture specific timex specifications
156  */
157 #include <asm/timex.h>
158
159 /* LATCH is used in the interval timer and ftape setup. */
160 #define LATCH  ((CLOCK_TICK_RATE + HZ/2) / HZ)  /* For divider */
161
162 /* Suppose we want to devide two numbers NOM and DEN: NOM/DEN, the we can
163  * improve accuracy by shifting LSH bits, hence calculating:
164  *     (NOM << LSH) / DEN
165  * This however means trouble for large NOM, because (NOM << LSH) may no
166  * longer fit in 32 bits. The following way of calculating this gives us
167  * some slack, under the following conditions:
168  *   - (NOM / DEN) fits in (32 - LSH) bits.
169  *   - (NOM % DEN) fits in (32 - LSH) bits.
170  */
171 #define SH_DIV(NOM,DEN,LSH) (   ((NOM / DEN) << LSH)                    \
172                              + (((NOM % DEN) << LSH) + DEN / 2) / DEN)
173
174 /* HZ is the requested value. ACTHZ is actual HZ ("<< 8" is for accuracy) */
175 #define ACTHZ (SH_DIV (CLOCK_TICK_RATE, LATCH, 8))
176
177 /* TICK_NSEC is the time between ticks in nsec assuming real ACTHZ */
178 #define TICK_NSEC (SH_DIV (1000000UL * 1000, ACTHZ, 8))
179
180 /* TICK_USEC is the time between ticks in usec assuming fake USER_HZ */
181 #define TICK_USEC ((1000000UL + USER_HZ/2) / USER_HZ)
182
183 /* TICK_USEC_TO_NSEC is the time between ticks in nsec assuming real ACTHZ and  */
184 /* a value TUSEC for TICK_USEC (can be set bij adjtimex)                */
185 #define TICK_USEC_TO_NSEC(TUSEC) (SH_DIV (TUSEC * USER_HZ * 1000, ACTHZ, 8))
186
187
188 #include <linux/time.h>
189 /*
190  * syscall interface - used (mainly by NTP daemon)
191  * to discipline kernel clock oscillator
192  */
193 struct timex {
194         unsigned int modes;     /* mode selector */
195         long offset;            /* time offset (usec) */
196         long freq;              /* frequency offset (scaled ppm) */
197         long maxerror;          /* maximum error (usec) */
198         long esterror;          /* estimated error (usec) */
199         int status;             /* clock command/status */
200         long constant;          /* pll time constant */
201         long precision;         /* clock precision (usec) (read only) */
202         long tolerance;         /* clock frequency tolerance (ppm)
203                                  * (read only)
204                                  */
205         struct timeval time;    /* (read only) */
206         long tick;              /* (modified) usecs between clock ticks */
207
208         long ppsfreq;           /* pps frequency (scaled ppm) (ro) */
209         long jitter;            /* pps jitter (us) (ro) */
210         int shift;              /* interval duration (s) (shift) (ro) */
211         long stabil;            /* pps stability (scaled ppm) (ro) */
212         long jitcnt;            /* jitter limit exceeded (ro) */
213         long calcnt;            /* calibration intervals (ro) */
214         long errcnt;            /* calibration errors (ro) */
215         long stbcnt;            /* stability limit exceeded (ro) */
216
217         int  :32; int  :32; int  :32; int  :32;
218         int  :32; int  :32; int  :32; int  :32;
219         int  :32; int  :32; int  :32; int  :32;
220 };
221
222 /*
223  * Mode codes (timex.mode)
224  */
225 #define ADJ_OFFSET              0x0001  /* time offset */
226 #define ADJ_FREQUENCY           0x0002  /* frequency offset */
227 #define ADJ_MAXERROR            0x0004  /* maximum time error */
228 #define ADJ_ESTERROR            0x0008  /* estimated time error */
229 #define ADJ_STATUS              0x0010  /* clock status */
230 #define ADJ_TIMECONST           0x0020  /* pll time constant */
231 #define ADJ_TICK                0x4000  /* tick value */
232 #define ADJ_OFFSET_SINGLESHOT   0x8001  /* old-fashioned adjtime */
233
234 /* xntp 3.4 compatibility names */
235 #define MOD_OFFSET      ADJ_OFFSET
236 #define MOD_FREQUENCY   ADJ_FREQUENCY
237 #define MOD_MAXERROR    ADJ_MAXERROR
238 #define MOD_ESTERROR    ADJ_ESTERROR
239 #define MOD_STATUS      ADJ_STATUS
240 #define MOD_TIMECONST   ADJ_TIMECONST
241 #define MOD_CLKB        ADJ_TICK
242 #define MOD_CLKA        ADJ_OFFSET_SINGLESHOT /* 0x8000 in original */
243
244
245 /*
246  * Status codes (timex.status)
247  */
248 #define STA_PLL         0x0001  /* enable PLL updates (rw) */
249 #define STA_PPSFREQ     0x0002  /* enable PPS freq discipline (rw) */
250 #define STA_PPSTIME     0x0004  /* enable PPS time discipline (rw) */
251 #define STA_FLL         0x0008  /* select frequency-lock mode (rw) */
252
253 #define STA_INS         0x0010  /* insert leap (rw) */
254 #define STA_DEL         0x0020  /* delete leap (rw) */
255 #define STA_UNSYNC      0x0040  /* clock unsynchronized (rw) */
256 #define STA_FREQHOLD    0x0080  /* hold frequency (rw) */
257
258 #define STA_PPSSIGNAL   0x0100  /* PPS signal present (ro) */
259 #define STA_PPSJITTER   0x0200  /* PPS signal jitter exceeded (ro) */
260 #define STA_PPSWANDER   0x0400  /* PPS signal wander exceeded (ro) */
261 #define STA_PPSERROR    0x0800  /* PPS signal calibration error (ro) */
262
263 #define STA_CLOCKERR    0x1000  /* clock hardware fault (ro) */
264
265 #define STA_RONLY (STA_PPSSIGNAL | STA_PPSJITTER | STA_PPSWANDER | \
266     STA_PPSERROR | STA_CLOCKERR) /* read-only bits */
267
268 /*
269  * Clock states (time_state)
270  */
271 #define TIME_OK         0       /* clock synchronized, no leap second */
272 #define TIME_INS        1       /* insert leap second */
273 #define TIME_DEL        2       /* delete leap second */
274 #define TIME_OOP        3       /* leap second in progress */
275 #define TIME_WAIT       4       /* leap second has occurred */
276 #define TIME_ERROR      5       /* clock not synchronized */
277 #define TIME_BAD        TIME_ERROR /* bw compat */
278
279 #ifdef __KERNEL__
280 /*
281  * kernel variables
282  * Note: maximum error = NTP synch distance = dispersion + delay / 2;
283  * estimated error = NTP dispersion.
284  */
285 extern unsigned long tick_usec;         /* USER_HZ period (usec) */
286 extern unsigned long tick_nsec;         /* ACTHZ          period (nsec) */
287 extern int tickadj;                     /* amount of adjustment per tick */
288
289 /*
290  * phase-lock loop variables
291  */
292 extern int time_state;          /* clock status */
293 extern int time_status;         /* clock synchronization status bits */
294 extern long time_offset;        /* time adjustment (us) */
295 extern long time_constant;      /* pll time constant */
296 extern long time_tolerance;     /* frequency tolerance (ppm) */
297 extern long time_precision;     /* clock precision (us) */
298 extern long time_maxerror;      /* maximum error */
299 extern long time_esterror;      /* estimated error */
300
301 extern long time_phase;         /* phase offset (scaled us) */
302 extern long time_freq;          /* frequency offset (scaled ppm) */
303 extern long time_adj;           /* tick adjust (scaled 1 / HZ) */
304 extern long time_reftime;       /* time at last adjustment (s) */
305
306 extern long time_adjust;        /* The amount of adjtime left */
307 extern long time_next_adjust;   /* Value for time_adjust at next tick */
308
309 /* interface variables pps->timer interrupt */
310 extern long pps_offset;         /* pps time offset (us) */
311 extern long pps_jitter;         /* time dispersion (jitter) (us) */
312 extern long pps_freq;           /* frequency offset (scaled ppm) */
313 extern long pps_stabil;         /* frequency dispersion (scaled ppm) */
314 extern long pps_valid;          /* pps signal watchdog counter */
315
316 /* interface variables pps->adjtimex */
317 extern int pps_shift;           /* interval duration (s) (shift) */
318 extern long pps_jitcnt;         /* jitter limit exceeded */
319 extern long pps_calcnt;         /* calibration intervals */
320 extern long pps_errcnt;         /* calibration errors */
321 extern long pps_stbcnt;         /* stability limit exceeded */
322
323 #ifdef CONFIG_TIME_INTERPOLATION
324
325 #define TIME_SOURCE_CPU 0
326 #define TIME_SOURCE_MMIO64 1
327 #define TIME_SOURCE_MMIO32 2
328 #define TIME_SOURCE_FUNCTION 3
329
330 /* For proper operations time_interpolator clocks must run slightly slower
331  * than the standard clock since the interpolator may only correct by having
332  * time jump forward during a tick. A slower clock is usually a side effect
333  * of the integer divide of the nanoseconds in a second by the frequency.
334  * The accuracy of the division can be increased by specifying a shift.
335  * However, this may cause the clock not to be slow enough.
336  * The interpolator will self-tune the clock by slowing down if no
337  * resets occur or speeding up if the time jumps per analysis cycle
338  * become too high.
339  *
340  * Setting jitter compensates for a fluctuating timesource by comparing
341  * to the last value read from the timesource to insure that an earlier value
342  * is not returned by a later call. The price to pay
343  * for the compensation is that the timer routines are not as scalable anymore.
344  */
345
346 #define INTERPOLATOR_ADJUST 65536
347 #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
348
349 struct time_interpolator {
350         unsigned short source;          /* time source flags */
351         unsigned char shift;            /* increases accuracy of multiply by shifting. */
352                         /* Note that bits may be lost if shift is set too high */
353         unsigned char jitter;           /* if set compensate for fluctuations */
354         unsigned nsec_per_cyc;          /* set by register_time_interpolator() */
355         void *addr;                     /* address of counter or function */
356         unsigned long offset;           /* nsec offset at last update of interpolator */
357         unsigned long last_counter;     /* counter value in units of the counter at last update */
358         unsigned long last_cycle;       /* Last timer value if TIME_SOURCE_JITTER is set */
359         unsigned long frequency;        /* frequency in counts/second */
360         long drift;                     /* drift in parts-per-million (or -1) */
361         unsigned long skips;            /* skips forward */
362         unsigned long ns_skipped;       /* nanoseconds skipped */
363         struct time_interpolator *next;
364 };
365
366 extern void register_time_interpolator(struct time_interpolator *);
367 extern void unregister_time_interpolator(struct time_interpolator *);
368 extern void time_interpolator_reset(void);
369 extern unsigned long time_interpolator_resolution(void);
370 extern unsigned long time_interpolator_get_offset(void);
371
372 #else /* !CONFIG_TIME_INTERPOLATION */
373
374 static inline void
375 time_interpolator_reset(void)
376 {
377 }
378
379 #endif /* !CONFIG_TIME_INTERPOLATION */
380
381 #endif /* KERNEL */
382
383 #endif /* LINUX_TIMEX_H */