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/timer.h>
14 #include <linux/timex.h>
15 #include <linux/jiffies.h>
16 #include <linux/hrtimer.h>
17 #include <linux/capability.h>
18 #include <linux/math64.h>
19 #include <linux/clocksource.h>
20 #include <asm/timex.h>
23 * Timekeeping variables
25 unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
26 unsigned long tick_nsec; /* ACTHZ period (nsec) */
28 static u64 tick_length_base;
30 static struct hrtimer leap_timer;
32 #define MAX_TICKADJ 500 /* microsecs */
33 #define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \
34 NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
37 * phase-lock loop variables
39 /* TIME_ERROR prevents overwriting the CMOS clock */
40 static int time_state = TIME_OK; /* clock synchronization status */
41 int time_status = STA_UNSYNC; /* clock status bits */
42 static long time_tai; /* TAI offset (s) */
43 static s64 time_offset; /* time adjustment (ns) */
44 static long time_constant = 2; /* pll time constant */
45 long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
46 long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
47 static s64 time_freq; /* frequency offset (scaled ns/s)*/
48 static long time_reftime; /* time at last adjustment (s) */
50 static long ntp_tick_adj;
52 static void ntp_update_frequency(void)
54 u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
56 second_length += (s64)ntp_tick_adj << NTP_SCALE_SHIFT;
57 second_length += time_freq;
59 tick_length_base = second_length;
61 tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
62 tick_length_base = div_u64(tick_length_base, NTP_INTERVAL_FREQ);
65 static void ntp_update_offset(long offset)
70 if (!(time_status & STA_PLL))
73 if (!(time_status & STA_NANO))
74 offset *= NSEC_PER_USEC;
77 * Scale the phase adjustment and
78 * clamp to the operating range.
80 offset = min(offset, MAXPHASE);
81 offset = max(offset, -MAXPHASE);
84 * Select how the frequency is to be controlled
85 * and in which mode (PLL or FLL).
87 if (time_status & STA_FREQHOLD || time_reftime == 0)
88 time_reftime = xtime.tv_sec;
89 mtemp = xtime.tv_sec - time_reftime;
90 time_reftime = xtime.tv_sec;
92 freq_adj = (s64)offset * mtemp;
93 freq_adj <<= NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant);
94 time_status &= ~STA_MODE;
95 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
96 freq_adj += div_s64((s64)offset << (NTP_SCALE_SHIFT - SHIFT_FLL),
98 time_status |= STA_MODE;
100 freq_adj += time_freq;
101 freq_adj = min(freq_adj, MAXFREQ_SCALED);
102 time_freq = max(freq_adj, -MAXFREQ_SCALED);
104 time_offset = div_s64((s64)offset << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
108 * ntp_clear - Clears the NTP state variables
110 * Must be called while holding a write on the xtime_lock
114 time_adjust = 0; /* stop active adjtime() */
115 time_status |= STA_UNSYNC;
116 time_maxerror = NTP_PHASE_LIMIT;
117 time_esterror = NTP_PHASE_LIMIT;
119 ntp_update_frequency();
121 tick_length = tick_length_base;
126 * Leap second processing. If in leap-insert state at the end of the
127 * day, the system clock is set back one second; if in leap-delete
128 * state, the system clock is set ahead one second.
130 static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
132 enum hrtimer_restart res = HRTIMER_NORESTART;
134 write_seqlock_irq(&xtime_lock);
136 switch (time_state) {
141 wall_to_monotonic.tv_sec++;
142 time_state = TIME_OOP;
143 printk(KERN_NOTICE "Clock: "
144 "inserting leap second 23:59:60 UTC\n");
145 leap_timer.expires = ktime_add_ns(leap_timer.expires,
147 res = HRTIMER_RESTART;
152 wall_to_monotonic.tv_sec--;
153 time_state = TIME_WAIT;
154 printk(KERN_NOTICE "Clock: "
155 "deleting leap second 23:59:59 UTC\n");
159 time_state = TIME_WAIT;
162 if (!(time_status & (STA_INS | STA_DEL)))
163 time_state = TIME_OK;
166 update_vsyscall(&xtime, clock);
168 write_sequnlock_irq(&xtime_lock);
174 * this routine handles the overflow of the microsecond field
176 * The tricky bits of code to handle the accurate clock support
177 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
178 * They were originally developed for SUN and DEC kernels.
179 * All the kudos should go to Dave for this stuff.
181 void second_overflow(void)
185 /* Bump the maxerror field */
186 time_maxerror += MAXFREQ / NSEC_PER_USEC;
187 if (time_maxerror > NTP_PHASE_LIMIT) {
188 time_maxerror = NTP_PHASE_LIMIT;
189 time_status |= STA_UNSYNC;
193 * Compute the phase adjustment for the next second. The offset is
194 * reduced by a fixed factor times the time constant.
196 tick_length = tick_length_base;
197 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
198 time_offset -= time_adj;
199 tick_length += time_adj;
201 if (unlikely(time_adjust)) {
202 if (time_adjust > MAX_TICKADJ) {
203 time_adjust -= MAX_TICKADJ;
204 tick_length += MAX_TICKADJ_SCALED;
205 } else if (time_adjust < -MAX_TICKADJ) {
206 time_adjust += MAX_TICKADJ;
207 tick_length -= MAX_TICKADJ_SCALED;
209 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
210 NTP_INTERVAL_FREQ) << NTP_SCALE_SHIFT;
216 #ifdef CONFIG_GENERIC_CMOS_UPDATE
218 /* Disable the cmos update - used by virtualization and embedded */
219 int no_sync_cmos_clock __read_mostly;
221 static void sync_cmos_clock(unsigned long dummy);
223 static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0);
225 static void sync_cmos_clock(unsigned long dummy)
227 struct timespec now, next;
231 * If we have an externally synchronized Linux clock, then update
232 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
233 * called as close as possible to 500 ms before the new second starts.
234 * This code is run on a timer. If the clock is set, that timer
235 * may not expire at the correct time. Thus, we adjust...
239 * Not synced, exit, do not restart a timer (if one is
240 * running, let it run out).
244 getnstimeofday(&now);
245 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
246 fail = update_persistent_clock(now);
248 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec;
249 if (next.tv_nsec <= 0)
250 next.tv_nsec += NSEC_PER_SEC;
257 if (next.tv_nsec >= NSEC_PER_SEC) {
259 next.tv_nsec -= NSEC_PER_SEC;
261 mod_timer(&sync_cmos_timer, jiffies + timespec_to_jiffies(&next));
264 static void notify_cmos_timer(void)
266 if (!no_sync_cmos_clock)
267 mod_timer(&sync_cmos_timer, jiffies + 1);
271 static inline void notify_cmos_timer(void) { }
274 /* adjtimex mainly allows reading (and writing, if superuser) of
275 * kernel time-keeping variables. used by xntpd.
277 int do_adjtimex(struct timex *txc)
280 long save_adjust, sec;
283 /* In order to modify anything, you gotta be super-user! */
284 if (txc->modes && !capable(CAP_SYS_TIME))
287 /* Now we validate the data before disabling interrupts */
289 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) {
290 /* singleshot must not be used with any other mode bits */
291 if (txc->modes & ~ADJ_OFFSET_SS_READ)
295 /* if the quartz is off by more than 10% something is VERY wrong ! */
296 if (txc->modes & ADJ_TICK)
297 if (txc->tick < 900000/USER_HZ ||
298 txc->tick > 1100000/USER_HZ)
301 if (time_state != TIME_OK && txc->modes & ADJ_STATUS)
302 hrtimer_cancel(&leap_timer);
305 write_seqlock_irq(&xtime_lock);
307 /* Save for later - semantics of adjtime is to return old value */
308 save_adjust = time_adjust;
310 /* If there are input parameters, then process them */
312 if (txc->modes & ADJ_STATUS) {
313 if ((time_status & STA_PLL) &&
314 !(txc->status & STA_PLL)) {
315 time_state = TIME_OK;
316 time_status = STA_UNSYNC;
318 /* only set allowed bits */
319 time_status &= STA_RONLY;
320 time_status |= txc->status & ~STA_RONLY;
322 switch (time_state) {
326 if (time_status & STA_INS) {
327 time_state = TIME_INS;
328 sec += 86400 - sec % 86400;
329 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
330 } else if (time_status & STA_DEL) {
331 time_state = TIME_DEL;
332 sec += 86400 - (sec + 1) % 86400;
333 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
338 time_state = TIME_OK;
342 if (!(time_status & (STA_INS | STA_DEL)))
343 time_state = TIME_OK;
346 hrtimer_restart(&leap_timer);
351 if (txc->modes & ADJ_NANO)
352 time_status |= STA_NANO;
353 if (txc->modes & ADJ_MICRO)
354 time_status &= ~STA_NANO;
356 if (txc->modes & ADJ_FREQUENCY) {
357 time_freq = (s64)txc->freq * PPM_SCALE;
358 time_freq = min(time_freq, MAXFREQ_SCALED);
359 time_freq = max(time_freq, -MAXFREQ_SCALED);
362 if (txc->modes & ADJ_MAXERROR)
363 time_maxerror = txc->maxerror;
364 if (txc->modes & ADJ_ESTERROR)
365 time_esterror = txc->esterror;
367 if (txc->modes & ADJ_TIMECONST) {
368 time_constant = txc->constant;
369 if (!(time_status & STA_NANO))
371 time_constant = min(time_constant, (long)MAXTC);
372 time_constant = max(time_constant, 0l);
375 if (txc->modes & ADJ_TAI && txc->constant > 0)
376 time_tai = txc->constant;
378 if (txc->modes & ADJ_OFFSET) {
379 if (txc->modes == ADJ_OFFSET_SINGLESHOT)
380 /* adjtime() is independent from ntp_adjtime() */
381 time_adjust = txc->offset;
383 ntp_update_offset(txc->offset);
385 if (txc->modes & ADJ_TICK)
386 tick_usec = txc->tick;
388 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
389 ntp_update_frequency();
392 result = time_state; /* mostly `TIME_OK' */
393 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
396 if ((txc->modes == ADJ_OFFSET_SINGLESHOT) ||
397 (txc->modes == ADJ_OFFSET_SS_READ))
398 txc->offset = save_adjust;
400 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
402 if (!(time_status & STA_NANO))
403 txc->offset /= NSEC_PER_USEC;
405 txc->freq = shift_right((s32)(time_freq >> PPM_SCALE_INV_SHIFT) *
408 txc->maxerror = time_maxerror;
409 txc->esterror = time_esterror;
410 txc->status = time_status;
411 txc->constant = time_constant;
413 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
414 txc->tick = tick_usec;
417 /* PPS is not implemented, so these are zero */
426 write_sequnlock_irq(&xtime_lock);
428 txc->time.tv_sec = ts.tv_sec;
429 txc->time.tv_usec = ts.tv_nsec;
430 if (!(time_status & STA_NANO))
431 txc->time.tv_usec /= NSEC_PER_USEC;
438 static int __init ntp_tick_adj_setup(char *str)
440 ntp_tick_adj = simple_strtol(str, NULL, 0);
444 __setup("ntp_tick_adj=", ntp_tick_adj_setup);
446 void __init ntp_init(void)
449 hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
450 leap_timer.function = ntp_leap_second;