2 * linux/kernel/time/ntp.c
4 * NTP state machine interfaces and logic.
6 * This code was mainly moved from kernel/timer.c and kernel/time.c
7 * Please see those files for relevant copyright info and historical
12 #include <linux/time.h>
13 #include <linux/timex.h>
15 #include <asm/div64.h>
16 #include <asm/timex.h>
18 /* Don't completely fail for HZ > 500. */
19 int tickadj = 500/HZ ? : 1; /* microsecs */
22 * phase-lock loop variables
24 /* TIME_ERROR prevents overwriting the CMOS clock */
25 int time_state = TIME_OK; /* clock synchronization status */
26 int time_status = STA_UNSYNC; /* clock status bits */
27 long time_offset; /* time adjustment (us) */
28 long time_constant = 2; /* pll time constant */
29 long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
30 long time_precision = 1; /* clock precision (us) */
31 long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
32 long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
33 long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
34 /* frequency offset (scaled ppm)*/
35 static long time_adj; /* tick adjust (scaled 1 / HZ) */
36 long time_reftime; /* time at last adjustment (s) */
38 long time_next_adjust;
41 * this routine handles the overflow of the microsecond field
43 * The tricky bits of code to handle the accurate clock support
44 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
45 * They were originally developed for SUN and DEC kernels.
46 * All the kudos should go to Dave for this stuff.
48 void second_overflow(void)
52 /* Bump the maxerror field */
53 time_maxerror += time_tolerance >> SHIFT_USEC;
54 if (time_maxerror > NTP_PHASE_LIMIT) {
55 time_maxerror = NTP_PHASE_LIMIT;
56 time_status |= STA_UNSYNC;
60 * Leap second processing. If in leap-insert state at the end of the
61 * day, the system clock is set back one second; if in leap-delete
62 * state, the system clock is set ahead one second. The microtime()
63 * routine or external clock driver will insure that reported time is
64 * always monotonic. The ugly divides should be replaced.
68 if (time_status & STA_INS)
69 time_state = TIME_INS;
70 else if (time_status & STA_DEL)
71 time_state = TIME_DEL;
74 if (xtime.tv_sec % 86400 == 0) {
76 wall_to_monotonic.tv_sec++;
78 * The timer interpolator will make time change
79 * gradually instead of an immediate jump by one second
81 time_interpolator_update(-NSEC_PER_SEC);
82 time_state = TIME_OOP;
84 printk(KERN_NOTICE "Clock: inserting leap second "
89 if ((xtime.tv_sec + 1) % 86400 == 0) {
91 wall_to_monotonic.tv_sec--;
93 * Use of time interpolator for a gradual change of
96 time_interpolator_update(NSEC_PER_SEC);
97 time_state = TIME_WAIT;
99 printk(KERN_NOTICE "Clock: deleting leap second "
104 time_state = TIME_WAIT;
107 if (!(time_status & (STA_INS | STA_DEL)))
108 time_state = TIME_OK;
112 * Compute the phase adjustment for the next second. In PLL mode, the
113 * offset is reduced by a fixed factor times the time constant. In FLL
114 * mode the offset is used directly. In either mode, the maximum phase
115 * adjustment for each second is clamped so as to spread the adjustment
116 * over not more than the number of seconds between updates.
119 if (!(time_status & STA_FLL))
120 ltemp = shift_right(ltemp, SHIFT_KG + time_constant);
121 ltemp = min(ltemp, (MAXPHASE / MINSEC) << SHIFT_UPDATE);
122 ltemp = max(ltemp, -(MAXPHASE / MINSEC) << SHIFT_UPDATE);
123 time_offset -= ltemp;
124 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
127 * Compute the frequency estimate and additional phase adjustment due
128 * to frequency error for the next second.
131 time_adj += shift_right(ltemp,(SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE));
135 * Compensate for (HZ==100) != (1 << SHIFT_HZ). Add 25% and 3.125% to
136 * get 128.125; => only 0.125% error (p. 14)
138 time_adj += shift_right(time_adj, 2) + shift_right(time_adj, 5);
142 * Compensate for (HZ==250) != (1 << SHIFT_HZ). Add 1.5625% and
143 * 0.78125% to get 255.85938; => only 0.05% error (p. 14)
145 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
149 * Compensate for (HZ==1000) != (1 << SHIFT_HZ). Add 1.5625% and
150 * 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
152 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
157 * Returns how many microseconds we need to add to xtime this tick
158 * in doing an adjustment requested with adjtime.
160 static long adjtime_adjustment(void)
162 long time_adjust_step;
164 time_adjust_step = time_adjust;
165 if (time_adjust_step) {
167 * We are doing an adjtime thing. Prepare time_adjust_step to
168 * be within bounds. Note that a positive time_adjust means we
169 * want the clock to run faster.
171 * Limit the amount of the step to be in the range
172 * -tickadj .. +tickadj
174 time_adjust_step = min(time_adjust_step, (long)tickadj);
175 time_adjust_step = max(time_adjust_step, (long)-tickadj);
177 return time_adjust_step;
180 /* in the NTP reference this is called "hardclock()" */
181 void update_ntp_one_tick(void)
183 long time_adjust_step;
185 time_adjust_step = adjtime_adjustment();
186 if (time_adjust_step)
187 /* Reduce by this step the amount of time left */
188 time_adjust -= time_adjust_step;
190 /* Changes by adjtime() do not take effect till next tick. */
191 if (time_next_adjust != 0) {
192 time_adjust = time_next_adjust;
193 time_next_adjust = 0;
198 * Return how long ticks are at the moment, that is, how much time
199 * update_wall_time_one_tick will add to xtime next time we call it
200 * (assuming no calls to do_adjtimex in the meantime).
201 * The return value is in fixed-point nanoseconds shifted by the
202 * specified number of bits to the right of the binary point.
203 * This function has no side-effects.
205 u64 current_tick_length(void)
210 /* calculate the finest interval NTP will allow.
211 * ie: nanosecond value shifted by (SHIFT_SCALE - 10)
213 delta_nsec = tick_nsec + adjtime_adjustment() * 1000;
214 ret = (u64)delta_nsec << TICK_LENGTH_SHIFT;
215 ret += (s64)time_adj << (TICK_LENGTH_SHIFT - (SHIFT_SCALE - 10));
221 void __attribute__ ((weak)) notify_arch_cmos_timer(void)
226 /* adjtimex mainly allows reading (and writing, if superuser) of
227 * kernel time-keeping variables. used by xntpd.
229 int do_adjtimex(struct timex *txc)
231 long ltemp, mtemp, save_adjust;
234 /* In order to modify anything, you gotta be super-user! */
235 if (txc->modes && !capable(CAP_SYS_TIME))
238 /* Now we validate the data before disabling interrupts */
240 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
241 /* singleshot must not be used with any other mode bits */
242 if (txc->modes != ADJ_OFFSET_SINGLESHOT)
245 if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
246 /* adjustment Offset limited to +- .512 seconds */
247 if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
250 /* if the quartz is off by more than 10% something is VERY wrong ! */
251 if (txc->modes & ADJ_TICK)
252 if (txc->tick < 900000/USER_HZ ||
253 txc->tick > 1100000/USER_HZ)
256 write_seqlock_irq(&xtime_lock);
257 result = time_state; /* mostly `TIME_OK' */
259 /* Save for later - semantics of adjtime is to return old value */
260 save_adjust = time_next_adjust ? time_next_adjust : time_adjust;
262 #if 0 /* STA_CLOCKERR is never set yet */
263 time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
265 /* If there are input parameters, then process them */
268 if (txc->modes & ADJ_STATUS) /* only set allowed bits */
269 time_status = (txc->status & ~STA_RONLY) |
270 (time_status & STA_RONLY);
272 if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
273 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
277 time_freq = txc->freq;
280 if (txc->modes & ADJ_MAXERROR) {
281 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
285 time_maxerror = txc->maxerror;
288 if (txc->modes & ADJ_ESTERROR) {
289 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
293 time_esterror = txc->esterror;
296 if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
297 if (txc->constant < 0) { /* NTP v4 uses values > 6 */
301 time_constant = txc->constant;
304 if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
305 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
306 /* adjtime() is independent from ntp_adjtime() */
307 if ((time_next_adjust = txc->offset) == 0)
310 else if (time_status & STA_PLL) {
314 * Scale the phase adjustment and
315 * clamp to the operating range.
317 if (ltemp > MAXPHASE)
318 time_offset = MAXPHASE << SHIFT_UPDATE;
319 else if (ltemp < -MAXPHASE)
320 time_offset = -(MAXPHASE << SHIFT_UPDATE);
322 time_offset = ltemp << SHIFT_UPDATE;
325 * Select whether the frequency is to be controlled
326 * and in which mode (PLL or FLL). Clamp to the operating
327 * range. Ugly multiply/divide should be replaced someday.
330 if (time_status & STA_FREQHOLD || time_reftime == 0)
331 time_reftime = xtime.tv_sec;
332 mtemp = xtime.tv_sec - time_reftime;
333 time_reftime = xtime.tv_sec;
334 if (time_status & STA_FLL) {
335 if (mtemp >= MINSEC) {
336 ltemp = (time_offset / mtemp) << (SHIFT_USEC -
338 time_freq += shift_right(ltemp, SHIFT_KH);
339 } else /* calibration interval too short (p. 12) */
341 } else { /* PLL mode */
342 if (mtemp < MAXSEC) {
344 time_freq += shift_right(ltemp,(time_constant +
346 SHIFT_KF - SHIFT_USEC));
347 } else /* calibration interval too long (p. 12) */
350 time_freq = min(time_freq, time_tolerance);
351 time_freq = max(time_freq, -time_tolerance);
353 } /* txc->modes & ADJ_OFFSET */
354 if (txc->modes & ADJ_TICK) {
355 tick_usec = txc->tick;
356 tick_nsec = TICK_USEC_TO_NSEC(tick_usec);
359 leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
362 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
363 txc->offset = save_adjust;
365 txc->offset = shift_right(time_offset, SHIFT_UPDATE);
367 txc->freq = time_freq;
368 txc->maxerror = time_maxerror;
369 txc->esterror = time_esterror;
370 txc->status = time_status;
371 txc->constant = time_constant;
372 txc->precision = time_precision;
373 txc->tolerance = time_tolerance;
374 txc->tick = tick_usec;
376 /* PPS is not implemented, so these are zero */
385 write_sequnlock_irq(&xtime_lock);
386 do_gettimeofday(&txc->time);
387 notify_arch_cmos_timer();