162306a36Sopenharmony_ci/*****************************************************************************
262306a36Sopenharmony_ci *                                                                           *
362306a36Sopenharmony_ci * Copyright (c) David L. Mills 1993                                         *
462306a36Sopenharmony_ci *                                                                           *
562306a36Sopenharmony_ci * Permission to use, copy, modify, and distribute this software and its     *
662306a36Sopenharmony_ci * documentation for any purpose and without fee is hereby granted, provided *
762306a36Sopenharmony_ci * that the above copyright notice appears in all copies and that both the   *
862306a36Sopenharmony_ci * copyright notice and this permission notice appear in supporting          *
962306a36Sopenharmony_ci * documentation, and that the name University of Delaware not be used in    *
1062306a36Sopenharmony_ci * advertising or publicity pertaining to distribution of the software       *
1162306a36Sopenharmony_ci * without specific, written prior permission.  The University of Delaware   *
1262306a36Sopenharmony_ci * makes no representations about the suitability this software for any      *
1362306a36Sopenharmony_ci * purpose.  It is provided "as is" without express or implied warranty.     *
1462306a36Sopenharmony_ci *                                                                           *
1562306a36Sopenharmony_ci *****************************************************************************/
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/*
1862306a36Sopenharmony_ci * Modification history timex.h
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * 29 Dec 97	Russell King
2162306a36Sopenharmony_ci *	Moved CLOCK_TICK_RATE, CLOCK_TICK_FACTOR and FINETUNE to asm/timex.h
2262306a36Sopenharmony_ci *	for ARM machines
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci *  9 Jan 97    Adrian Sun
2562306a36Sopenharmony_ci *      Shifted LATCH define to allow access to alpha machines.
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * 26 Sep 94	David L. Mills
2862306a36Sopenharmony_ci *	Added defines for hybrid phase/frequency-lock loop.
2962306a36Sopenharmony_ci *
3062306a36Sopenharmony_ci * 19 Mar 94	David L. Mills
3162306a36Sopenharmony_ci *	Moved defines from kernel routines to header file and added new
3262306a36Sopenharmony_ci *	defines for PPS phase-lock loop.
3362306a36Sopenharmony_ci *
3462306a36Sopenharmony_ci * 20 Feb 94	David L. Mills
3562306a36Sopenharmony_ci *	Revised status codes and structures for external clock and PPS
3662306a36Sopenharmony_ci *	signal discipline.
3762306a36Sopenharmony_ci *
3862306a36Sopenharmony_ci * 28 Nov 93	David L. Mills
3962306a36Sopenharmony_ci *	Adjusted parameters to improve stability and increase poll
4062306a36Sopenharmony_ci *	interval.
4162306a36Sopenharmony_ci *
4262306a36Sopenharmony_ci * 17 Sep 93    David L. Mills
4362306a36Sopenharmony_ci *      Created file $NTP/include/sys/timex.h
4462306a36Sopenharmony_ci * 07 Oct 93    Torsten Duwe
4562306a36Sopenharmony_ci *      Derived linux/timex.h
4662306a36Sopenharmony_ci * 1995-08-13    Torsten Duwe
4762306a36Sopenharmony_ci *      kernel PLL updated to 1994-12-13 specs (rfc-1589)
4862306a36Sopenharmony_ci * 1997-08-30    Ulrich Windl
4962306a36Sopenharmony_ci *      Added new constant NTP_PHASE_LIMIT
5062306a36Sopenharmony_ci * 2004-08-12    Christoph Lameter
5162306a36Sopenharmony_ci *      Reworked time interpolation logic
5262306a36Sopenharmony_ci */
5362306a36Sopenharmony_ci#ifndef _LINUX_TIMEX_H
5462306a36Sopenharmony_ci#define _LINUX_TIMEX_H
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#include <uapi/linux/timex.h>
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci#define ADJ_ADJTIME		0x8000	/* switch between adjtime/adjtimex modes */
5962306a36Sopenharmony_ci#define ADJ_OFFSET_SINGLESHOT	0x0001	/* old-fashioned adjtime */
6062306a36Sopenharmony_ci#define ADJ_OFFSET_READONLY	0x2000	/* read-only adjtime */
6162306a36Sopenharmony_ci#include <linux/compiler.h>
6262306a36Sopenharmony_ci#include <linux/types.h>
6362306a36Sopenharmony_ci#include <linux/param.h>
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ciunsigned long random_get_entropy_fallback(void);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci#include <asm/timex.h>
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci#ifndef random_get_entropy
7062306a36Sopenharmony_ci/*
7162306a36Sopenharmony_ci * The random_get_entropy() function is used by the /dev/random driver
7262306a36Sopenharmony_ci * in order to extract entropy via the relative unpredictability of
7362306a36Sopenharmony_ci * when an interrupt takes places versus a high speed, fine-grained
7462306a36Sopenharmony_ci * timing source or cycle counter.  Since it will be occurred on every
7562306a36Sopenharmony_ci * single interrupt, it must have a very low cost/overhead.
7662306a36Sopenharmony_ci *
7762306a36Sopenharmony_ci * By default we use get_cycles() for this purpose, but individual
7862306a36Sopenharmony_ci * architectures may override this in their asm/timex.h header file.
7962306a36Sopenharmony_ci * If a given arch does not have get_cycles(), then we fallback to
8062306a36Sopenharmony_ci * using random_get_entropy_fallback().
8162306a36Sopenharmony_ci */
8262306a36Sopenharmony_ci#ifdef get_cycles
8362306a36Sopenharmony_ci#define random_get_entropy()	((unsigned long)get_cycles())
8462306a36Sopenharmony_ci#else
8562306a36Sopenharmony_ci#define random_get_entropy()	random_get_entropy_fallback()
8662306a36Sopenharmony_ci#endif
8762306a36Sopenharmony_ci#endif
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci/*
9062306a36Sopenharmony_ci * SHIFT_PLL is used as a dampening factor to define how much we
9162306a36Sopenharmony_ci * adjust the frequency correction for a given offset in PLL mode.
9262306a36Sopenharmony_ci * It also used in dampening the offset correction, to define how
9362306a36Sopenharmony_ci * much of the current value in time_offset we correct for each
9462306a36Sopenharmony_ci * second. Changing this value changes the stiffness of the ntp
9562306a36Sopenharmony_ci * adjustment code. A lower value makes it more flexible, reducing
9662306a36Sopenharmony_ci * NTP convergence time. A higher value makes it stiffer, increasing
9762306a36Sopenharmony_ci * convergence time, but making the clock more stable.
9862306a36Sopenharmony_ci *
9962306a36Sopenharmony_ci * In David Mills' nanokernel reference implementation SHIFT_PLL is 4.
10062306a36Sopenharmony_ci * However this seems to increase convergence time much too long.
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci * https://lists.ntp.org/pipermail/hackers/2008-January/003487.html
10362306a36Sopenharmony_ci *
10462306a36Sopenharmony_ci * In the above mailing list discussion, it seems the value of 4
10562306a36Sopenharmony_ci * was appropriate for other Unix systems with HZ=100, and that
10662306a36Sopenharmony_ci * SHIFT_PLL should be decreased as HZ increases. However, Linux's
10762306a36Sopenharmony_ci * clock steering implementation is HZ independent.
10862306a36Sopenharmony_ci *
10962306a36Sopenharmony_ci * Through experimentation, a SHIFT_PLL value of 2 was found to allow
11062306a36Sopenharmony_ci * for fast convergence (very similar to the NTPv3 code used prior to
11162306a36Sopenharmony_ci * v2.6.19), with good clock stability.
11262306a36Sopenharmony_ci *
11362306a36Sopenharmony_ci *
11462306a36Sopenharmony_ci * SHIFT_FLL is used as a dampening factor to define how much we
11562306a36Sopenharmony_ci * adjust the frequency correction for a given offset in FLL mode.
11662306a36Sopenharmony_ci * In David Mills' nanokernel reference implementation SHIFT_FLL is 2.
11762306a36Sopenharmony_ci *
11862306a36Sopenharmony_ci * MAXTC establishes the maximum time constant of the PLL.
11962306a36Sopenharmony_ci */
12062306a36Sopenharmony_ci#define SHIFT_PLL	2	/* PLL frequency factor (shift) */
12162306a36Sopenharmony_ci#define SHIFT_FLL	2	/* FLL frequency factor (shift) */
12262306a36Sopenharmony_ci#define MAXTC		10	/* maximum time constant (shift) */
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci/*
12562306a36Sopenharmony_ci * SHIFT_USEC defines the scaling (shift) of the time_freq and
12662306a36Sopenharmony_ci * time_tolerance variables, which represent the current frequency
12762306a36Sopenharmony_ci * offset and maximum frequency tolerance.
12862306a36Sopenharmony_ci */
12962306a36Sopenharmony_ci#define SHIFT_USEC 16		/* frequency offset scale (shift) */
13062306a36Sopenharmony_ci#define PPM_SCALE ((s64)NSEC_PER_USEC << (NTP_SCALE_SHIFT - SHIFT_USEC))
13162306a36Sopenharmony_ci#define PPM_SCALE_INV_SHIFT 19
13262306a36Sopenharmony_ci#define PPM_SCALE_INV ((1LL << (PPM_SCALE_INV_SHIFT + NTP_SCALE_SHIFT)) / \
13362306a36Sopenharmony_ci		       PPM_SCALE + 1)
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci#define MAXPHASE 500000000L	/* max phase error (ns) */
13662306a36Sopenharmony_ci#define MAXFREQ 500000		/* max frequency error (ns/s) */
13762306a36Sopenharmony_ci#define MAXFREQ_SCALED ((s64)MAXFREQ << NTP_SCALE_SHIFT)
13862306a36Sopenharmony_ci#define MINSEC 256		/* min interval between updates (s) */
13962306a36Sopenharmony_ci#define MAXSEC 2048		/* max interval between updates (s) */
14062306a36Sopenharmony_ci#define NTP_PHASE_LIMIT ((MAXPHASE / NSEC_PER_USEC) << 5) /* beyond max. dispersion */
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci/*
14362306a36Sopenharmony_ci * kernel variables
14462306a36Sopenharmony_ci * Note: maximum error = NTP sync distance = dispersion + delay / 2;
14562306a36Sopenharmony_ci * estimated error = NTP dispersion.
14662306a36Sopenharmony_ci */
14762306a36Sopenharmony_ciextern unsigned long tick_usec;		/* USER_HZ period (usec) */
14862306a36Sopenharmony_ciextern unsigned long tick_nsec;		/* SHIFTED_HZ period (nsec) */
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci/* Required to safely shift negative values */
15162306a36Sopenharmony_ci#define shift_right(x, s) ({	\
15262306a36Sopenharmony_ci	__typeof__(x) __x = (x);	\
15362306a36Sopenharmony_ci	__typeof__(s) __s = (s);	\
15462306a36Sopenharmony_ci	__x < 0 ? -(-__x >> __s) : __x >> __s;	\
15562306a36Sopenharmony_ci})
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci#define NTP_SCALE_SHIFT		32
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci#define NTP_INTERVAL_FREQ  (HZ)
16062306a36Sopenharmony_ci#define NTP_INTERVAL_LENGTH (NSEC_PER_SEC/NTP_INTERVAL_FREQ)
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ciextern int do_adjtimex(struct __kernel_timex *);
16362306a36Sopenharmony_ciextern int do_clock_adjtime(const clockid_t which_clock, struct __kernel_timex * ktx);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciextern void hardpps(const struct timespec64 *, const struct timespec64 *);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ciint read_current_timer(unsigned long *timer_val);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci/* The clock frequency of the i8253/i8254 PIT */
17062306a36Sopenharmony_ci#define PIT_TICK_RATE 1193182ul
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci#endif /* LINUX_TIMEX_H */
173