time: ntp: simplify ntp_tick_adj calculations
[linux-2.6/cjktty.git] / kernel / time / ntp.c
bloba3fe7ef2d83b55b24c9deb3b48384b9a7c5c41bf
1 /*
2 * NTP state machine interfaces and logic.
4 * This code was mainly moved from kernel/timer.c and kernel/time.c
5 * Please see those files for relevant copyright info and historical
6 * changelogs.
7 */
8 #include <linux/capability.h>
9 #include <linux/clocksource.h>
10 #include <linux/workqueue.h>
11 #include <linux/hrtimer.h>
12 #include <linux/jiffies.h>
13 #include <linux/math64.h>
14 #include <linux/timex.h>
15 #include <linux/time.h>
16 #include <linux/mm.h>
19 * NTP timekeeping variables:
22 /* USER_HZ period (usecs): */
23 unsigned long tick_usec = TICK_USEC;
25 /* ACTHZ period (nsecs): */
26 unsigned long tick_nsec;
28 u64 tick_length;
29 static u64 tick_length_base;
31 static struct hrtimer leap_timer;
33 #define MAX_TICKADJ 500LL /* usecs */
34 #define MAX_TICKADJ_SCALED \
35 (((MAX_TICKADJ * NSEC_PER_USEC) << NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
38 * phase-lock loop variables
42 * clock synchronization status
44 * (TIME_ERROR prevents overwriting the CMOS clock)
46 static int time_state = TIME_OK;
48 /* clock status bits: */
49 int time_status = STA_UNSYNC;
51 /* TAI offset (secs): */
52 static long time_tai;
54 /* time adjustment (nsecs): */
55 static s64 time_offset;
57 /* pll time constant: */
58 static long time_constant = 2;
60 /* maximum error (usecs): */
61 long time_maxerror = NTP_PHASE_LIMIT;
63 /* estimated error (usecs): */
64 long time_esterror = NTP_PHASE_LIMIT;
66 /* frequency offset (scaled nsecs/secs): */
67 static s64 time_freq;
69 /* time at last adjustment (secs): */
70 static long time_reftime;
72 long time_adjust;
74 /* constant (boot-param configurable) NTP tick adjustment (upscaled) */
75 static s64 ntp_tick_adj;
78 * NTP methods:
82 * Update (tick_length, tick_length_base, tick_nsec), based
83 * on (tick_usec, ntp_tick_adj, time_freq):
85 static void ntp_update_frequency(void)
87 u64 second_length;
88 u64 new_base;
90 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
91 << NTP_SCALE_SHIFT;
93 second_length += ntp_tick_adj;
94 second_length += time_freq;
96 tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
97 new_base = div_u64(second_length, NTP_INTERVAL_FREQ);
100 * Don't wait for the next second_overflow, apply
101 * the change to the tick length immediately:
103 tick_length += new_base - tick_length_base;
104 tick_length_base = new_base;
107 static inline s64 ntp_update_offset_fll(s64 offset64, long secs)
109 time_status &= ~STA_MODE;
111 if (secs < MINSEC)
112 return 0;
114 if (!(time_status & STA_FLL) && (secs <= MAXSEC))
115 return 0;
117 time_status |= STA_MODE;
119 return div_s64(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
122 static void ntp_update_offset(long offset)
124 s64 freq_adj;
125 s64 offset64;
126 long secs;
128 if (!(time_status & STA_PLL))
129 return;
131 if (!(time_status & STA_NANO))
132 offset *= NSEC_PER_USEC;
135 * Scale the phase adjustment and
136 * clamp to the operating range.
138 offset = min(offset, MAXPHASE);
139 offset = max(offset, -MAXPHASE);
142 * Select how the frequency is to be controlled
143 * and in which mode (PLL or FLL).
145 secs = xtime.tv_sec - time_reftime;
146 if (unlikely(time_status & STA_FREQHOLD))
147 secs = 0;
149 time_reftime = xtime.tv_sec;
151 offset64 = offset;
152 freq_adj = (offset64 * secs) <<
153 (NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant));
155 freq_adj += ntp_update_offset_fll(offset64, secs);
157 freq_adj = min(freq_adj + time_freq, MAXFREQ_SCALED);
159 time_freq = max(freq_adj, -MAXFREQ_SCALED);
161 time_offset = div_s64(offset64 << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
165 * ntp_clear - Clears the NTP state variables
167 * Must be called while holding a write on the xtime_lock
169 void ntp_clear(void)
171 time_adjust = 0; /* stop active adjtime() */
172 time_status |= STA_UNSYNC;
173 time_maxerror = NTP_PHASE_LIMIT;
174 time_esterror = NTP_PHASE_LIMIT;
176 ntp_update_frequency();
178 tick_length = tick_length_base;
179 time_offset = 0;
183 * Leap second processing. If in leap-insert state at the end of the
184 * day, the system clock is set back one second; if in leap-delete
185 * state, the system clock is set ahead one second.
187 static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
189 enum hrtimer_restart res = HRTIMER_NORESTART;
191 write_seqlock(&xtime_lock);
193 switch (time_state) {
194 case TIME_OK:
195 break;
196 case TIME_INS:
197 xtime.tv_sec--;
198 wall_to_monotonic.tv_sec++;
199 time_state = TIME_OOP;
200 printk(KERN_NOTICE
201 "Clock: inserting leap second 23:59:60 UTC\n");
202 hrtimer_add_expires_ns(&leap_timer, NSEC_PER_SEC);
203 res = HRTIMER_RESTART;
204 break;
205 case TIME_DEL:
206 xtime.tv_sec++;
207 time_tai--;
208 wall_to_monotonic.tv_sec--;
209 time_state = TIME_WAIT;
210 printk(KERN_NOTICE
211 "Clock: deleting leap second 23:59:59 UTC\n");
212 break;
213 case TIME_OOP:
214 time_tai++;
215 time_state = TIME_WAIT;
216 /* fall through */
217 case TIME_WAIT:
218 if (!(time_status & (STA_INS | STA_DEL)))
219 time_state = TIME_OK;
220 break;
222 update_vsyscall(&xtime, clock);
224 write_sequnlock(&xtime_lock);
226 return res;
230 * this routine handles the overflow of the microsecond field
232 * The tricky bits of code to handle the accurate clock support
233 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
234 * They were originally developed for SUN and DEC kernels.
235 * All the kudos should go to Dave for this stuff.
237 void second_overflow(void)
239 s64 time_adj;
241 /* Bump the maxerror field */
242 time_maxerror += MAXFREQ / NSEC_PER_USEC;
243 if (time_maxerror > NTP_PHASE_LIMIT) {
244 time_maxerror = NTP_PHASE_LIMIT;
245 time_status |= STA_UNSYNC;
249 * Compute the phase adjustment for the next second. The offset is
250 * reduced by a fixed factor times the time constant.
252 tick_length = tick_length_base;
253 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
254 time_offset -= time_adj;
255 tick_length += time_adj;
257 if (!time_adjust)
258 return;
260 if (time_adjust > MAX_TICKADJ) {
261 time_adjust -= MAX_TICKADJ;
262 tick_length += MAX_TICKADJ_SCALED;
263 return;
266 if (time_adjust < -MAX_TICKADJ) {
267 time_adjust += MAX_TICKADJ;
268 tick_length -= MAX_TICKADJ_SCALED;
269 return;
272 tick_length += (s64)(time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ)
273 << NTP_SCALE_SHIFT;
274 time_adjust = 0;
277 #ifdef CONFIG_GENERIC_CMOS_UPDATE
279 /* Disable the cmos update - used by virtualization and embedded */
280 int no_sync_cmos_clock __read_mostly;
282 static void sync_cmos_clock(struct work_struct *work);
284 static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
286 static void sync_cmos_clock(struct work_struct *work)
288 struct timespec now, next;
289 int fail = 1;
292 * If we have an externally synchronized Linux clock, then update
293 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
294 * called as close as possible to 500 ms before the new second starts.
295 * This code is run on a timer. If the clock is set, that timer
296 * may not expire at the correct time. Thus, we adjust...
298 if (!ntp_synced()) {
300 * Not synced, exit, do not restart a timer (if one is
301 * running, let it run out).
303 return;
306 getnstimeofday(&now);
307 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
308 fail = update_persistent_clock(now);
310 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2);
311 if (next.tv_nsec <= 0)
312 next.tv_nsec += NSEC_PER_SEC;
314 if (!fail)
315 next.tv_sec = 659;
316 else
317 next.tv_sec = 0;
319 if (next.tv_nsec >= NSEC_PER_SEC) {
320 next.tv_sec++;
321 next.tv_nsec -= NSEC_PER_SEC;
323 schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next));
326 static void notify_cmos_timer(void)
328 if (!no_sync_cmos_clock)
329 schedule_delayed_work(&sync_cmos_work, 0);
332 #else
333 static inline void notify_cmos_timer(void) { }
334 #endif
337 * Start the leap seconds timer:
339 static inline void ntp_start_leap_timer(struct timespec *ts)
341 long now = ts->tv_sec;
343 if (time_status & STA_INS) {
344 time_state = TIME_INS;
345 now += 86400 - now % 86400;
346 hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS);
348 return;
351 if (time_status & STA_DEL) {
352 time_state = TIME_DEL;
353 now += 86400 - (now + 1) % 86400;
354 hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS);
359 * Propagate a new txc->status value into the NTP state:
361 static inline void process_adj_status(struct timex *txc, struct timespec *ts)
363 if ((time_status & STA_PLL) && !(txc->status & STA_PLL)) {
364 time_state = TIME_OK;
365 time_status = STA_UNSYNC;
367 /* only set allowed bits */
368 time_status &= STA_RONLY;
371 * If we turn on PLL adjustments then reset the
372 * reference time to current time.
374 if (!(time_status & STA_PLL) && (txc->status & STA_PLL))
375 time_reftime = xtime.tv_sec;
377 time_status |= txc->status & ~STA_RONLY;
379 switch (time_state) {
380 case TIME_OK:
381 ntp_start_leap_timer(ts);
382 break;
383 case TIME_INS:
384 case TIME_DEL:
385 time_state = TIME_OK;
386 ntp_start_leap_timer(ts);
387 case TIME_WAIT:
388 if (!(time_status & (STA_INS | STA_DEL)))
389 time_state = TIME_OK;
390 break;
391 case TIME_OOP:
392 hrtimer_restart(&leap_timer);
393 break;
397 * Called with the xtime lock held, so we can access and modify
398 * all the global NTP state:
400 static inline void process_adjtimex_modes(struct timex *txc, struct timespec *ts)
402 if (txc->modes & ADJ_STATUS)
403 process_adj_status(txc, ts);
405 if (txc->modes & ADJ_NANO)
406 time_status |= STA_NANO;
408 if (txc->modes & ADJ_MICRO)
409 time_status &= ~STA_NANO;
411 if (txc->modes & ADJ_FREQUENCY) {
412 time_freq = txc->freq * PPM_SCALE;
413 time_freq = min(time_freq, MAXFREQ_SCALED);
414 time_freq = max(time_freq, -MAXFREQ_SCALED);
417 if (txc->modes & ADJ_MAXERROR)
418 time_maxerror = txc->maxerror;
420 if (txc->modes & ADJ_ESTERROR)
421 time_esterror = txc->esterror;
423 if (txc->modes & ADJ_TIMECONST) {
424 time_constant = txc->constant;
425 if (!(time_status & STA_NANO))
426 time_constant += 4;
427 time_constant = min(time_constant, (long)MAXTC);
428 time_constant = max(time_constant, 0l);
431 if (txc->modes & ADJ_TAI && txc->constant > 0)
432 time_tai = txc->constant;
434 if (txc->modes & ADJ_OFFSET)
435 ntp_update_offset(txc->offset);
437 if (txc->modes & ADJ_TICK)
438 tick_usec = txc->tick;
440 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
441 ntp_update_frequency();
445 * adjtimex mainly allows reading (and writing, if superuser) of
446 * kernel time-keeping variables. used by xntpd.
448 int do_adjtimex(struct timex *txc)
450 struct timespec ts;
451 int result;
453 /* Validate the data before disabling interrupts */
454 if (txc->modes & ADJ_ADJTIME) {
455 /* singleshot must not be used with any other mode bits */
456 if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
457 return -EINVAL;
458 if (!(txc->modes & ADJ_OFFSET_READONLY) &&
459 !capable(CAP_SYS_TIME))
460 return -EPERM;
461 } else {
462 /* In order to modify anything, you gotta be super-user! */
463 if (txc->modes && !capable(CAP_SYS_TIME))
464 return -EPERM;
467 * if the quartz is off by more than 10% then
468 * something is VERY wrong!
470 if (txc->modes & ADJ_TICK &&
471 (txc->tick < 900000/USER_HZ ||
472 txc->tick > 1100000/USER_HZ))
473 return -EINVAL;
475 if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
476 hrtimer_cancel(&leap_timer);
479 getnstimeofday(&ts);
481 write_seqlock_irq(&xtime_lock);
483 if (txc->modes & ADJ_ADJTIME) {
484 long save_adjust = time_adjust;
486 if (!(txc->modes & ADJ_OFFSET_READONLY)) {
487 /* adjtime() is independent from ntp_adjtime() */
488 time_adjust = txc->offset;
489 ntp_update_frequency();
491 txc->offset = save_adjust;
492 } else {
494 /* If there are input parameters, then process them: */
495 if (txc->modes)
496 process_adjtimex_modes(txc, &ts);
498 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
499 NTP_SCALE_SHIFT);
500 if (!(time_status & STA_NANO))
501 txc->offset /= NSEC_PER_USEC;
504 result = time_state; /* mostly `TIME_OK' */
505 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
506 result = TIME_ERROR;
508 txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
509 PPM_SCALE_INV, NTP_SCALE_SHIFT);
510 txc->maxerror = time_maxerror;
511 txc->esterror = time_esterror;
512 txc->status = time_status;
513 txc->constant = time_constant;
514 txc->precision = 1;
515 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
516 txc->tick = tick_usec;
517 txc->tai = time_tai;
519 /* PPS is not implemented, so these are zero */
520 txc->ppsfreq = 0;
521 txc->jitter = 0;
522 txc->shift = 0;
523 txc->stabil = 0;
524 txc->jitcnt = 0;
525 txc->calcnt = 0;
526 txc->errcnt = 0;
527 txc->stbcnt = 0;
529 write_sequnlock_irq(&xtime_lock);
531 txc->time.tv_sec = ts.tv_sec;
532 txc->time.tv_usec = ts.tv_nsec;
533 if (!(time_status & STA_NANO))
534 txc->time.tv_usec /= NSEC_PER_USEC;
536 notify_cmos_timer();
538 return result;
541 static int __init ntp_tick_adj_setup(char *str)
543 ntp_tick_adj = simple_strtol(str, NULL, 0);
544 ntp_tick_adj <<= NTP_SCALE_SHIFT;
546 return 1;
549 __setup("ntp_tick_adj=", ntp_tick_adj_setup);
551 void __init ntp_init(void)
553 ntp_clear();
554 hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
555 leap_timer.function = ntp_leap_second;