Back to home page

OSCL-LXR

 
 

    


0001 /*****************************************************************************
0002  *                                                                           *
0003  * Copyright (c) David L. Mills 1993                                         *
0004  *                                                                           *
0005  * Permission to use, copy, modify, and distribute this software and its     *
0006  * documentation for any purpose and without fee is hereby granted, provided *
0007  * that the above copyright notice appears in all copies and that both the   *
0008  * copyright notice and this permission notice appear in supporting          *
0009  * documentation, and that the name University of Delaware not be used in    *
0010  * advertising or publicity pertaining to distribution of the software       *
0011  * without specific, written prior permission.  The University of Delaware   *
0012  * makes no representations about the suitability this software for any      *
0013  * purpose.  It is provided "as is" without express or implied warranty.     *
0014  *                                                                           *
0015  *****************************************************************************/
0016 
0017 /*
0018  * Modification history timex.h
0019  *
0020  * 29 Dec 97    Russell King
0021  *  Moved CLOCK_TICK_RATE, CLOCK_TICK_FACTOR and FINETUNE to asm/timex.h
0022  *  for ARM machines
0023  *
0024  *  9 Jan 97    Adrian Sun
0025  *      Shifted LATCH define to allow access to alpha machines.
0026  *
0027  * 26 Sep 94    David L. Mills
0028  *  Added defines for hybrid phase/frequency-lock loop.
0029  *
0030  * 19 Mar 94    David L. Mills
0031  *  Moved defines from kernel routines to header file and added new
0032  *  defines for PPS phase-lock loop.
0033  *
0034  * 20 Feb 94    David L. Mills
0035  *  Revised status codes and structures for external clock and PPS
0036  *  signal discipline.
0037  *
0038  * 28 Nov 93    David L. Mills
0039  *  Adjusted parameters to improve stability and increase poll
0040  *  interval.
0041  *
0042  * 17 Sep 93    David L. Mills
0043  *      Created file $NTP/include/sys/timex.h
0044  * 07 Oct 93    Torsten Duwe
0045  *      Derived linux/timex.h
0046  * 1995-08-13    Torsten Duwe
0047  *      kernel PLL updated to 1994-12-13 specs (rfc-1589)
0048  * 1997-08-30    Ulrich Windl
0049  *      Added new constant NTP_PHASE_LIMIT
0050  * 2004-08-12    Christoph Lameter
0051  *      Reworked time interpolation logic
0052  */
0053 #ifndef _LINUX_TIMEX_H
0054 #define _LINUX_TIMEX_H
0055 
0056 #include <uapi/linux/timex.h>
0057 
0058 #define ADJ_ADJTIME     0x8000  /* switch between adjtime/adjtimex modes */
0059 #define ADJ_OFFSET_SINGLESHOT   0x0001  /* old-fashioned adjtime */
0060 #define ADJ_OFFSET_READONLY 0x2000  /* read-only adjtime */
0061 #include <linux/compiler.h>
0062 #include <linux/types.h>
0063 #include <linux/param.h>
0064 
0065 unsigned long random_get_entropy_fallback(void);
0066 
0067 #include <asm/timex.h>
0068 
0069 #ifndef random_get_entropy
0070 /*
0071  * The random_get_entropy() function is used by the /dev/random driver
0072  * in order to extract entropy via the relative unpredictability of
0073  * when an interrupt takes places versus a high speed, fine-grained
0074  * timing source or cycle counter.  Since it will be occurred on every
0075  * single interrupt, it must have a very low cost/overhead.
0076  *
0077  * By default we use get_cycles() for this purpose, but individual
0078  * architectures may override this in their asm/timex.h header file.
0079  * If a given arch does not have get_cycles(), then we fallback to
0080  * using random_get_entropy_fallback().
0081  */
0082 #ifdef get_cycles
0083 #define random_get_entropy()    ((unsigned long)get_cycles())
0084 #else
0085 #define random_get_entropy()    random_get_entropy_fallback()
0086 #endif
0087 #endif
0088 
0089 /*
0090  * SHIFT_PLL is used as a dampening factor to define how much we
0091  * adjust the frequency correction for a given offset in PLL mode.
0092  * It also used in dampening the offset correction, to define how
0093  * much of the current value in time_offset we correct for each
0094  * second. Changing this value changes the stiffness of the ntp
0095  * adjustment code. A lower value makes it more flexible, reducing
0096  * NTP convergence time. A higher value makes it stiffer, increasing
0097  * convergence time, but making the clock more stable.
0098  *
0099  * In David Mills' nanokernel reference implementation SHIFT_PLL is 4.
0100  * However this seems to increase convergence time much too long.
0101  *
0102  * https://lists.ntp.org/pipermail/hackers/2008-January/003487.html
0103  *
0104  * In the above mailing list discussion, it seems the value of 4
0105  * was appropriate for other Unix systems with HZ=100, and that
0106  * SHIFT_PLL should be decreased as HZ increases. However, Linux's
0107  * clock steering implementation is HZ independent.
0108  *
0109  * Through experimentation, a SHIFT_PLL value of 2 was found to allow
0110  * for fast convergence (very similar to the NTPv3 code used prior to
0111  * v2.6.19), with good clock stability.
0112  *
0113  *
0114  * SHIFT_FLL is used as a dampening factor to define how much we
0115  * adjust the frequency correction for a given offset in FLL mode.
0116  * In David Mills' nanokernel reference implementation SHIFT_FLL is 2.
0117  *
0118  * MAXTC establishes the maximum time constant of the PLL.
0119  */
0120 #define SHIFT_PLL   2   /* PLL frequency factor (shift) */
0121 #define SHIFT_FLL   2   /* FLL frequency factor (shift) */
0122 #define MAXTC       10  /* maximum time constant (shift) */
0123 
0124 /*
0125  * SHIFT_USEC defines the scaling (shift) of the time_freq and
0126  * time_tolerance variables, which represent the current frequency
0127  * offset and maximum frequency tolerance.
0128  */
0129 #define SHIFT_USEC 16       /* frequency offset scale (shift) */
0130 #define PPM_SCALE ((s64)NSEC_PER_USEC << (NTP_SCALE_SHIFT - SHIFT_USEC))
0131 #define PPM_SCALE_INV_SHIFT 19
0132 #define PPM_SCALE_INV ((1LL << (PPM_SCALE_INV_SHIFT + NTP_SCALE_SHIFT)) / \
0133                PPM_SCALE + 1)
0134 
0135 #define MAXPHASE 500000000L /* max phase error (ns) */
0136 #define MAXFREQ 500000      /* max frequency error (ns/s) */
0137 #define MAXFREQ_SCALED ((s64)MAXFREQ << NTP_SCALE_SHIFT)
0138 #define MINSEC 256      /* min interval between updates (s) */
0139 #define MAXSEC 2048     /* max interval between updates (s) */
0140 #define NTP_PHASE_LIMIT ((MAXPHASE / NSEC_PER_USEC) << 5) /* beyond max. dispersion */
0141 
0142 /*
0143  * kernel variables
0144  * Note: maximum error = NTP sync distance = dispersion + delay / 2;
0145  * estimated error = NTP dispersion.
0146  */
0147 extern unsigned long tick_usec;     /* USER_HZ period (usec) */
0148 extern unsigned long tick_nsec;     /* SHIFTED_HZ period (nsec) */
0149 
0150 /* Required to safely shift negative values */
0151 #define shift_right(x, s) ({    \
0152     __typeof__(x) __x = (x);    \
0153     __typeof__(s) __s = (s);    \
0154     __x < 0 ? -(-__x >> __s) : __x >> __s;  \
0155 })
0156 
0157 #define NTP_SCALE_SHIFT     32
0158 
0159 #define NTP_INTERVAL_FREQ  (HZ)
0160 #define NTP_INTERVAL_LENGTH (NSEC_PER_SEC/NTP_INTERVAL_FREQ)
0161 
0162 extern int do_adjtimex(struct __kernel_timex *);
0163 extern int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx);
0164 
0165 extern void hardpps(const struct timespec64 *, const struct timespec64 *);
0166 
0167 int read_current_timer(unsigned long *timer_val);
0168 
0169 /* The clock frequency of the i8253/i8254 PIT */
0170 #define PIT_TICK_RATE 1193182ul
0171 
0172 #endif /* LINUX_TIMEX_H */