[PATCH] ntp: add ntp_update_frequency
[linux-2.6/linux-2.6-openrd.git] / kernel / time / ntp.c
blob77137bec2aeacd171cee83c85ae5ec8e9c0559f1
1 /*
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
8 * changelogs.
9 */
11 #include <linux/mm.h>
12 #include <linux/time.h>
13 #include <linux/timex.h>
15 #include <asm/div64.h>
16 #include <asm/timex.h>
19 * Timekeeping variables
21 unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
22 unsigned long tick_nsec; /* ACTHZ period (nsec) */
23 static u64 tick_length, tick_length_base;
25 /* Don't completely fail for HZ > 500. */
26 int tickadj = 500/HZ ? : 1; /* microsecs */
29 * phase-lock loop variables
31 /* TIME_ERROR prevents overwriting the CMOS clock */
32 int time_state = TIME_OK; /* clock synchronization status */
33 int time_status = STA_UNSYNC; /* clock status bits */
34 long time_offset; /* time adjustment (us) */
35 long time_constant = 2; /* pll time constant */
36 long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
37 long time_precision = 1; /* clock precision (us) */
38 long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
39 long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
40 long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
41 /* frequency offset (scaled ppm)*/
42 static long time_adj; /* tick adjust (scaled 1 / HZ) */
43 long time_reftime; /* time at last adjustment (s) */
44 long time_adjust;
45 long time_next_adjust;
47 /**
48 * ntp_clear - Clears the NTP state variables
50 * Must be called while holding a write on the xtime_lock
52 void ntp_clear(void)
54 time_adjust = 0; /* stop active adjtime() */
55 time_status |= STA_UNSYNC;
56 time_maxerror = NTP_PHASE_LIMIT;
57 time_esterror = NTP_PHASE_LIMIT;
59 ntp_update_frequency();
61 tick_length = tick_length_base;
64 #define CLOCK_TICK_OVERFLOW (LATCH * HZ - CLOCK_TICK_RATE)
65 #define CLOCK_TICK_ADJUST (((s64)CLOCK_TICK_OVERFLOW * NSEC_PER_SEC) / (s64)CLOCK_TICK_RATE)
67 void ntp_update_frequency(void)
69 tick_length_base = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ) << TICK_LENGTH_SHIFT;
70 tick_length_base += (s64)CLOCK_TICK_ADJUST << TICK_LENGTH_SHIFT;
72 do_div(tick_length_base, HZ);
74 tick_nsec = tick_length_base >> TICK_LENGTH_SHIFT;
78 * this routine handles the overflow of the microsecond field
80 * The tricky bits of code to handle the accurate clock support
81 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
82 * They were originally developed for SUN and DEC kernels.
83 * All the kudos should go to Dave for this stuff.
85 void second_overflow(void)
87 long ltemp;
89 /* Bump the maxerror field */
90 time_maxerror += time_tolerance >> SHIFT_USEC;
91 if (time_maxerror > NTP_PHASE_LIMIT) {
92 time_maxerror = NTP_PHASE_LIMIT;
93 time_status |= STA_UNSYNC;
97 * Leap second processing. If in leap-insert state at the end of the
98 * day, the system clock is set back one second; if in leap-delete
99 * state, the system clock is set ahead one second. The microtime()
100 * routine or external clock driver will insure that reported time is
101 * always monotonic. The ugly divides should be replaced.
103 switch (time_state) {
104 case TIME_OK:
105 if (time_status & STA_INS)
106 time_state = TIME_INS;
107 else if (time_status & STA_DEL)
108 time_state = TIME_DEL;
109 break;
110 case TIME_INS:
111 if (xtime.tv_sec % 86400 == 0) {
112 xtime.tv_sec--;
113 wall_to_monotonic.tv_sec++;
115 * The timer interpolator will make time change
116 * gradually instead of an immediate jump by one second
118 time_interpolator_update(-NSEC_PER_SEC);
119 time_state = TIME_OOP;
120 clock_was_set();
121 printk(KERN_NOTICE "Clock: inserting leap second "
122 "23:59:60 UTC\n");
124 break;
125 case TIME_DEL:
126 if ((xtime.tv_sec + 1) % 86400 == 0) {
127 xtime.tv_sec++;
128 wall_to_monotonic.tv_sec--;
130 * Use of time interpolator for a gradual change of
131 * time
133 time_interpolator_update(NSEC_PER_SEC);
134 time_state = TIME_WAIT;
135 clock_was_set();
136 printk(KERN_NOTICE "Clock: deleting leap second "
137 "23:59:59 UTC\n");
139 break;
140 case TIME_OOP:
141 time_state = TIME_WAIT;
142 break;
143 case TIME_WAIT:
144 if (!(time_status & (STA_INS | STA_DEL)))
145 time_state = TIME_OK;
149 * Compute the phase adjustment for the next second. In PLL mode, the
150 * offset is reduced by a fixed factor times the time constant. In FLL
151 * mode the offset is used directly. In either mode, the maximum phase
152 * adjustment for each second is clamped so as to spread the adjustment
153 * over not more than the number of seconds between updates.
155 ltemp = time_offset;
156 if (!(time_status & STA_FLL))
157 ltemp = shift_right(ltemp, SHIFT_KG + time_constant);
158 ltemp = min(ltemp, (MAXPHASE / MINSEC) << SHIFT_UPDATE);
159 ltemp = max(ltemp, -(MAXPHASE / MINSEC) << SHIFT_UPDATE);
160 time_offset -= ltemp;
161 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
164 * Compute the frequency estimate and additional phase adjustment due
165 * to frequency error for the next second.
167 ltemp = time_freq;
168 time_adj += shift_right(ltemp,(SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE));
170 #if HZ == 100
172 * Compensate for (HZ==100) != (1 << SHIFT_HZ). Add 25% and 3.125% to
173 * get 128.125; => only 0.125% error (p. 14)
175 time_adj += shift_right(time_adj, 2) + shift_right(time_adj, 5);
176 #endif
177 #if HZ == 250
179 * Compensate for (HZ==250) != (1 << SHIFT_HZ). Add 1.5625% and
180 * 0.78125% to get 255.85938; => only 0.05% error (p. 14)
182 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
183 #endif
184 #if HZ == 1000
186 * Compensate for (HZ==1000) != (1 << SHIFT_HZ). Add 1.5625% and
187 * 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
189 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
190 #endif
191 tick_length = tick_length_base;
195 * Returns how many microseconds we need to add to xtime this tick
196 * in doing an adjustment requested with adjtime.
198 static long adjtime_adjustment(void)
200 long time_adjust_step;
202 time_adjust_step = time_adjust;
203 if (time_adjust_step) {
205 * We are doing an adjtime thing. Prepare time_adjust_step to
206 * be within bounds. Note that a positive time_adjust means we
207 * want the clock to run faster.
209 * Limit the amount of the step to be in the range
210 * -tickadj .. +tickadj
212 time_adjust_step = min(time_adjust_step, (long)tickadj);
213 time_adjust_step = max(time_adjust_step, (long)-tickadj);
215 return time_adjust_step;
218 /* in the NTP reference this is called "hardclock()" */
219 void update_ntp_one_tick(void)
221 long time_adjust_step;
223 time_adjust_step = adjtime_adjustment();
224 if (time_adjust_step)
225 /* Reduce by this step the amount of time left */
226 time_adjust -= time_adjust_step;
228 /* Changes by adjtime() do not take effect till next tick. */
229 if (time_next_adjust != 0) {
230 time_adjust = time_next_adjust;
231 time_next_adjust = 0;
236 * Return how long ticks are at the moment, that is, how much time
237 * update_wall_time_one_tick will add to xtime next time we call it
238 * (assuming no calls to do_adjtimex in the meantime).
239 * The return value is in fixed-point nanoseconds shifted by the
240 * specified number of bits to the right of the binary point.
241 * This function has no side-effects.
243 u64 current_tick_length(void)
245 u64 ret;
247 /* calculate the finest interval NTP will allow.
248 * ie: nanosecond value shifted by (SHIFT_SCALE - 10)
250 ret = tick_length;
251 ret += (u64)(adjtime_adjustment() * 1000) << TICK_LENGTH_SHIFT;
252 ret += (s64)time_adj << (TICK_LENGTH_SHIFT - (SHIFT_SCALE - 10));
254 return ret;
258 void __attribute__ ((weak)) notify_arch_cmos_timer(void)
260 return;
263 /* adjtimex mainly allows reading (and writing, if superuser) of
264 * kernel time-keeping variables. used by xntpd.
266 int do_adjtimex(struct timex *txc)
268 long ltemp, mtemp, save_adjust;
269 int result;
271 /* In order to modify anything, you gotta be super-user! */
272 if (txc->modes && !capable(CAP_SYS_TIME))
273 return -EPERM;
275 /* Now we validate the data before disabling interrupts */
277 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
278 /* singleshot must not be used with any other mode bits */
279 if (txc->modes != ADJ_OFFSET_SINGLESHOT)
280 return -EINVAL;
282 if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
283 /* adjustment Offset limited to +- .512 seconds */
284 if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
285 return -EINVAL;
287 /* if the quartz is off by more than 10% something is VERY wrong ! */
288 if (txc->modes & ADJ_TICK)
289 if (txc->tick < 900000/USER_HZ ||
290 txc->tick > 1100000/USER_HZ)
291 return -EINVAL;
293 write_seqlock_irq(&xtime_lock);
294 result = time_state; /* mostly `TIME_OK' */
296 /* Save for later - semantics of adjtime is to return old value */
297 save_adjust = time_next_adjust ? time_next_adjust : time_adjust;
299 #if 0 /* STA_CLOCKERR is never set yet */
300 time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
301 #endif
302 /* If there are input parameters, then process them */
303 if (txc->modes)
305 if (txc->modes & ADJ_STATUS) /* only set allowed bits */
306 time_status = (txc->status & ~STA_RONLY) |
307 (time_status & STA_RONLY);
309 if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
310 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
311 result = -EINVAL;
312 goto leave;
314 time_freq = txc->freq;
317 if (txc->modes & ADJ_MAXERROR) {
318 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
319 result = -EINVAL;
320 goto leave;
322 time_maxerror = txc->maxerror;
325 if (txc->modes & ADJ_ESTERROR) {
326 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
327 result = -EINVAL;
328 goto leave;
330 time_esterror = txc->esterror;
333 if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
334 if (txc->constant < 0) { /* NTP v4 uses values > 6 */
335 result = -EINVAL;
336 goto leave;
338 time_constant = txc->constant;
341 if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
342 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
343 /* adjtime() is independent from ntp_adjtime() */
344 if ((time_next_adjust = txc->offset) == 0)
345 time_adjust = 0;
347 else if (time_status & STA_PLL) {
348 ltemp = txc->offset;
351 * Scale the phase adjustment and
352 * clamp to the operating range.
354 if (ltemp > MAXPHASE)
355 time_offset = MAXPHASE << SHIFT_UPDATE;
356 else if (ltemp < -MAXPHASE)
357 time_offset = -(MAXPHASE << SHIFT_UPDATE);
358 else
359 time_offset = ltemp << SHIFT_UPDATE;
362 * Select whether the frequency is to be controlled
363 * and in which mode (PLL or FLL). Clamp to the operating
364 * range. Ugly multiply/divide should be replaced someday.
367 if (time_status & STA_FREQHOLD || time_reftime == 0)
368 time_reftime = xtime.tv_sec;
369 mtemp = xtime.tv_sec - time_reftime;
370 time_reftime = xtime.tv_sec;
371 if (time_status & STA_FLL) {
372 if (mtemp >= MINSEC) {
373 ltemp = (time_offset / mtemp) << (SHIFT_USEC -
374 SHIFT_UPDATE);
375 time_freq += shift_right(ltemp, SHIFT_KH);
376 } else /* calibration interval too short (p. 12) */
377 result = TIME_ERROR;
378 } else { /* PLL mode */
379 if (mtemp < MAXSEC) {
380 ltemp *= mtemp;
381 time_freq += shift_right(ltemp,(time_constant +
382 time_constant +
383 SHIFT_KF - SHIFT_USEC));
384 } else /* calibration interval too long (p. 12) */
385 result = TIME_ERROR;
387 time_freq = min(time_freq, time_tolerance);
388 time_freq = max(time_freq, -time_tolerance);
389 } /* STA_PLL */
390 } /* txc->modes & ADJ_OFFSET */
391 if (txc->modes & ADJ_TICK)
392 tick_usec = txc->tick;
394 if (txc->modes & ADJ_TICK)
395 ntp_update_frequency();
396 } /* txc->modes */
397 leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
398 result = TIME_ERROR;
400 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
401 txc->offset = save_adjust;
402 else {
403 txc->offset = shift_right(time_offset, SHIFT_UPDATE);
405 txc->freq = time_freq;
406 txc->maxerror = time_maxerror;
407 txc->esterror = time_esterror;
408 txc->status = time_status;
409 txc->constant = time_constant;
410 txc->precision = time_precision;
411 txc->tolerance = time_tolerance;
412 txc->tick = tick_usec;
414 /* PPS is not implemented, so these are zero */
415 txc->ppsfreq = 0;
416 txc->jitter = 0;
417 txc->shift = 0;
418 txc->stabil = 0;
419 txc->jitcnt = 0;
420 txc->calcnt = 0;
421 txc->errcnt = 0;
422 txc->stbcnt = 0;
423 write_sequnlock_irq(&xtime_lock);
424 do_gettimeofday(&txc->time);
425 notify_arch_cmos_timer();
426 return(result);