Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / kernel / time / ntp.c
blob0d7a3a3ec415f79ff74173e570e0ffefee9daaae
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/timer.h>
14 #include <linux/timex.h>
15 #include <linux/jiffies.h>
16 #include <linux/hrtimer.h>
17 #include <linux/capability.h>
18 #include <asm/div64.h>
19 #include <asm/timex.h>
22 * Timekeeping variables
24 unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
25 unsigned long tick_nsec; /* ACTHZ period (nsec) */
26 static u64 tick_length, tick_length_base;
28 #define MAX_TICKADJ 500 /* microsecs */
29 #define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \
30 TICK_LENGTH_SHIFT) / NTP_INTERVAL_FREQ)
33 * phase-lock loop variables
35 /* TIME_ERROR prevents overwriting the CMOS clock */
36 static int time_state = TIME_OK; /* clock synchronization status */
37 int time_status = STA_UNSYNC; /* clock status bits */
38 static s64 time_offset; /* time adjustment (ns) */
39 static long time_constant = 2; /* pll time constant */
40 long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
41 long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
42 long time_freq; /* frequency offset (scaled ppm)*/
43 static long time_reftime; /* time at last adjustment (s) */
44 long time_adjust;
45 <<<<<<< HEAD:kernel/time/ntp.c
46 =======
47 static long ntp_tick_adj;
48 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/time/ntp.c
50 static void ntp_update_frequency(void)
52 u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
53 << TICK_LENGTH_SHIFT;
54 <<<<<<< HEAD:kernel/time/ntp.c
55 second_length += (s64)CLOCK_TICK_ADJUST << TICK_LENGTH_SHIFT;
56 =======
57 second_length += (s64)ntp_tick_adj << TICK_LENGTH_SHIFT;
58 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/time/ntp.c
59 second_length += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC);
61 tick_length_base = second_length;
63 do_div(second_length, HZ);
64 tick_nsec = second_length >> TICK_LENGTH_SHIFT;
66 do_div(tick_length_base, NTP_INTERVAL_FREQ);
69 /**
70 * ntp_clear - Clears the NTP state variables
72 * Must be called while holding a write on the xtime_lock
74 void ntp_clear(void)
76 time_adjust = 0; /* stop active adjtime() */
77 time_status |= STA_UNSYNC;
78 time_maxerror = NTP_PHASE_LIMIT;
79 time_esterror = NTP_PHASE_LIMIT;
81 ntp_update_frequency();
83 tick_length = tick_length_base;
84 time_offset = 0;
88 * this routine handles the overflow of the microsecond field
90 * The tricky bits of code to handle the accurate clock support
91 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
92 * They were originally developed for SUN and DEC kernels.
93 * All the kudos should go to Dave for this stuff.
95 void second_overflow(void)
97 long time_adj;
99 /* Bump the maxerror field */
100 time_maxerror += MAXFREQ >> SHIFT_USEC;
101 if (time_maxerror > NTP_PHASE_LIMIT) {
102 time_maxerror = NTP_PHASE_LIMIT;
103 time_status |= STA_UNSYNC;
107 * Leap second processing. If in leap-insert state at the end of the
108 * day, the system clock is set back one second; if in leap-delete
109 * state, the system clock is set ahead one second. The microtime()
110 * routine or external clock driver will insure that reported time is
111 * always monotonic. The ugly divides should be replaced.
113 switch (time_state) {
114 case TIME_OK:
115 if (time_status & STA_INS)
116 time_state = TIME_INS;
117 else if (time_status & STA_DEL)
118 time_state = TIME_DEL;
119 break;
120 case TIME_INS:
121 if (xtime.tv_sec % 86400 == 0) {
122 xtime.tv_sec--;
123 wall_to_monotonic.tv_sec++;
124 time_state = TIME_OOP;
125 printk(KERN_NOTICE "Clock: inserting leap second "
126 "23:59:60 UTC\n");
128 break;
129 case TIME_DEL:
130 if ((xtime.tv_sec + 1) % 86400 == 0) {
131 xtime.tv_sec++;
132 wall_to_monotonic.tv_sec--;
133 time_state = TIME_WAIT;
134 printk(KERN_NOTICE "Clock: deleting leap second "
135 "23:59:59 UTC\n");
137 break;
138 case TIME_OOP:
139 time_state = TIME_WAIT;
140 break;
141 case TIME_WAIT:
142 if (!(time_status & (STA_INS | STA_DEL)))
143 time_state = TIME_OK;
147 * Compute the phase adjustment for the next second. The offset is
148 * reduced by a fixed factor times the time constant.
150 tick_length = tick_length_base;
151 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
152 time_offset -= time_adj;
153 tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE);
155 if (unlikely(time_adjust)) {
156 if (time_adjust > MAX_TICKADJ) {
157 time_adjust -= MAX_TICKADJ;
158 tick_length += MAX_TICKADJ_SCALED;
159 } else if (time_adjust < -MAX_TICKADJ) {
160 time_adjust += MAX_TICKADJ;
161 tick_length -= MAX_TICKADJ_SCALED;
162 } else {
163 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
164 NTP_INTERVAL_FREQ) << TICK_LENGTH_SHIFT;
165 time_adjust = 0;
171 * Return how long ticks are at the moment, that is, how much time
172 * update_wall_time_one_tick will add to xtime next time we call it
173 * (assuming no calls to do_adjtimex in the meantime).
174 * The return value is in fixed-point nanoseconds shifted by the
175 * specified number of bits to the right of the binary point.
176 * This function has no side-effects.
178 u64 current_tick_length(void)
180 return tick_length;
183 #ifdef CONFIG_GENERIC_CMOS_UPDATE
185 /* Disable the cmos update - used by virtualization and embedded */
186 int no_sync_cmos_clock __read_mostly;
188 static void sync_cmos_clock(unsigned long dummy);
190 static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0);
192 static void sync_cmos_clock(unsigned long dummy)
194 struct timespec now, next;
195 int fail = 1;
198 * If we have an externally synchronized Linux clock, then update
199 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
200 * called as close as possible to 500 ms before the new second starts.
201 * This code is run on a timer. If the clock is set, that timer
202 * may not expire at the correct time. Thus, we adjust...
204 if (!ntp_synced())
206 * Not synced, exit, do not restart a timer (if one is
207 * running, let it run out).
209 return;
211 getnstimeofday(&now);
212 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
213 fail = update_persistent_clock(now);
215 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec;
216 if (next.tv_nsec <= 0)
217 next.tv_nsec += NSEC_PER_SEC;
219 if (!fail)
220 next.tv_sec = 659;
221 else
222 next.tv_sec = 0;
224 if (next.tv_nsec >= NSEC_PER_SEC) {
225 next.tv_sec++;
226 next.tv_nsec -= NSEC_PER_SEC;
228 mod_timer(&sync_cmos_timer, jiffies + timespec_to_jiffies(&next));
231 static void notify_cmos_timer(void)
233 if (!no_sync_cmos_clock)
234 mod_timer(&sync_cmos_timer, jiffies + 1);
237 #else
238 static inline void notify_cmos_timer(void) { }
239 #endif
241 /* adjtimex mainly allows reading (and writing, if superuser) of
242 * kernel time-keeping variables. used by xntpd.
244 int do_adjtimex(struct timex *txc)
246 long mtemp, save_adjust, rem;
247 s64 freq_adj, temp64;
248 int result;
250 /* In order to modify anything, you gotta be super-user! */
251 if (txc->modes && !capable(CAP_SYS_TIME))
252 return -EPERM;
254 /* Now we validate the data before disabling interrupts */
256 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) {
257 /* singleshot must not be used with any other mode bits */
258 if (txc->modes != ADJ_OFFSET_SINGLESHOT &&
259 txc->modes != ADJ_OFFSET_SS_READ)
260 return -EINVAL;
263 if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
264 /* adjustment Offset limited to +- .512 seconds */
265 if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
266 return -EINVAL;
268 /* if the quartz is off by more than 10% something is VERY wrong ! */
269 if (txc->modes & ADJ_TICK)
270 if (txc->tick < 900000/USER_HZ ||
271 txc->tick > 1100000/USER_HZ)
272 return -EINVAL;
274 write_seqlock_irq(&xtime_lock);
275 result = time_state; /* mostly `TIME_OK' */
277 /* Save for later - semantics of adjtime is to return old value */
278 save_adjust = time_adjust;
280 #if 0 /* STA_CLOCKERR is never set yet */
281 time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
282 #endif
283 /* If there are input parameters, then process them */
284 if (txc->modes)
286 if (txc->modes & ADJ_STATUS) /* only set allowed bits */
287 time_status = (txc->status & ~STA_RONLY) |
288 (time_status & STA_RONLY);
290 if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
291 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
292 result = -EINVAL;
293 goto leave;
295 time_freq = ((s64)txc->freq * NSEC_PER_USEC)
296 >> (SHIFT_USEC - SHIFT_NSEC);
299 if (txc->modes & ADJ_MAXERROR) {
300 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
301 result = -EINVAL;
302 goto leave;
304 time_maxerror = txc->maxerror;
307 if (txc->modes & ADJ_ESTERROR) {
308 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
309 result = -EINVAL;
310 goto leave;
312 time_esterror = txc->esterror;
315 if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
316 if (txc->constant < 0) { /* NTP v4 uses values > 6 */
317 result = -EINVAL;
318 goto leave;
320 time_constant = min(txc->constant + 4, (long)MAXTC);
323 if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
324 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
325 /* adjtime() is independent from ntp_adjtime() */
326 time_adjust = txc->offset;
328 else if (time_status & STA_PLL) {
329 time_offset = txc->offset * NSEC_PER_USEC;
332 * Scale the phase adjustment and
333 * clamp to the operating range.
335 time_offset = min(time_offset, (s64)MAXPHASE * NSEC_PER_USEC);
336 time_offset = max(time_offset, (s64)-MAXPHASE * NSEC_PER_USEC);
339 * Select whether the frequency is to be controlled
340 * and in which mode (PLL or FLL). Clamp to the operating
341 * range. Ugly multiply/divide should be replaced someday.
344 if (time_status & STA_FREQHOLD || time_reftime == 0)
345 time_reftime = xtime.tv_sec;
346 mtemp = xtime.tv_sec - time_reftime;
347 time_reftime = xtime.tv_sec;
349 freq_adj = time_offset * mtemp;
350 freq_adj = shift_right(freq_adj, time_constant * 2 +
351 (SHIFT_PLL + 2) * 2 - SHIFT_NSEC);
352 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
353 <<<<<<< HEAD:kernel/time/ntp.c
354 =======
355 u64 utemp64;
356 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/time/ntp.c
357 temp64 = time_offset << (SHIFT_NSEC - SHIFT_FLL);
358 if (time_offset < 0) {
359 <<<<<<< HEAD:kernel/time/ntp.c
360 temp64 = -temp64;
361 do_div(temp64, mtemp);
362 freq_adj -= temp64;
363 =======
364 utemp64 = -temp64;
365 do_div(utemp64, mtemp);
366 freq_adj -= utemp64;
367 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/time/ntp.c
368 } else {
369 <<<<<<< HEAD:kernel/time/ntp.c
370 do_div(temp64, mtemp);
371 freq_adj += temp64;
372 =======
373 utemp64 = temp64;
374 do_div(utemp64, mtemp);
375 freq_adj += utemp64;
376 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/time/ntp.c
379 freq_adj += time_freq;
380 freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC);
381 time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC);
382 time_offset = div_long_long_rem_signed(time_offset,
383 NTP_INTERVAL_FREQ,
384 &rem);
385 time_offset <<= SHIFT_UPDATE;
386 } /* STA_PLL */
387 } /* txc->modes & ADJ_OFFSET */
388 if (txc->modes & ADJ_TICK)
389 tick_usec = txc->tick;
391 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
392 ntp_update_frequency();
393 } /* txc->modes */
394 leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
395 result = TIME_ERROR;
397 if ((txc->modes == ADJ_OFFSET_SINGLESHOT) ||
398 (txc->modes == ADJ_OFFSET_SS_READ))
399 txc->offset = save_adjust;
400 else
401 txc->offset = ((long)shift_right(time_offset, SHIFT_UPDATE)) *
402 NTP_INTERVAL_FREQ / 1000;
403 txc->freq = (time_freq / NSEC_PER_USEC) <<
404 (SHIFT_USEC - SHIFT_NSEC);
405 txc->maxerror = time_maxerror;
406 txc->esterror = time_esterror;
407 txc->status = time_status;
408 txc->constant = time_constant;
409 txc->precision = 1;
410 txc->tolerance = MAXFREQ;
411 txc->tick = tick_usec;
413 /* PPS is not implemented, so these are zero */
414 txc->ppsfreq = 0;
415 txc->jitter = 0;
416 txc->shift = 0;
417 txc->stabil = 0;
418 txc->jitcnt = 0;
419 txc->calcnt = 0;
420 txc->errcnt = 0;
421 txc->stbcnt = 0;
422 write_sequnlock_irq(&xtime_lock);
423 do_gettimeofday(&txc->time);
424 notify_cmos_timer();
425 return(result);
427 <<<<<<< HEAD:kernel/time/ntp.c
428 =======
430 static int __init ntp_tick_adj_setup(char *str)
432 ntp_tick_adj = simple_strtol(str, NULL, 0);
433 return 1;
436 __setup("ntp_tick_adj=", ntp_tick_adj_setup);
437 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:kernel/time/ntp.c