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 <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 NTP_SCALE_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 long time_tai; /* TAI offset (s) */
39 static s64 time_offset; /* time adjustment (ns) */
40 static long time_constant = 2; /* pll time constant */
41 long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
42 long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
43 static s64 time_freq; /* frequency offset (scaled ns/s)*/
44 static long time_reftime; /* time at last adjustment (s) */
46 static long ntp_tick_adj;
48 static void ntp_update_frequency(void)
50 u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
52 second_length += (s64)ntp_tick_adj << NTP_SCALE_SHIFT;
53 second_length += time_freq;
55 tick_length_base = second_length;
57 tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
58 tick_length_base = div_u64(tick_length_base, NTP_INTERVAL_FREQ);
61 static void ntp_update_offset(long offset)
66 if (!(time_status & STA_PLL))
69 if (!(time_status & STA_NANO))
70 offset *= NSEC_PER_USEC;
73 * Scale the phase adjustment and
74 * clamp to the operating range.
76 offset = min(offset, MAXPHASE);
77 offset = max(offset, -MAXPHASE);
80 * Select how the frequency is to be controlled
81 * and in which mode (PLL or FLL).
83 if (time_status & STA_FREQHOLD || time_reftime == 0)
84 time_reftime = xtime.tv_sec;
85 mtemp = xtime.tv_sec - time_reftime;
86 time_reftime = xtime.tv_sec;
88 freq_adj = (s64)offset * mtemp;
89 freq_adj <<= NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant);
90 time_status &= ~STA_MODE;
91 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
92 freq_adj += div_s64((s64)offset << (NTP_SCALE_SHIFT - SHIFT_FLL),
94 time_status |= STA_MODE;
96 freq_adj += time_freq;
97 freq_adj = min(freq_adj, MAXFREQ_SCALED);
98 time_freq = max(freq_adj, -MAXFREQ_SCALED);
100 time_offset = div_s64((s64)offset << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
104 * ntp_clear - Clears the NTP state variables
106 * Must be called while holding a write on the xtime_lock
110 time_adjust = 0; /* stop active adjtime() */
111 time_status |= STA_UNSYNC;
112 time_maxerror = NTP_PHASE_LIMIT;
113 time_esterror = NTP_PHASE_LIMIT;
115 ntp_update_frequency();
117 tick_length = tick_length_base;
122 * this routine handles the overflow of the microsecond field
124 * The tricky bits of code to handle the accurate clock support
125 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
126 * They were originally developed for SUN and DEC kernels.
127 * All the kudos should go to Dave for this stuff.
129 void second_overflow(void)
133 /* Bump the maxerror field */
134 time_maxerror += MAXFREQ / NSEC_PER_USEC;
135 if (time_maxerror > NTP_PHASE_LIMIT) {
136 time_maxerror = NTP_PHASE_LIMIT;
137 time_status |= STA_UNSYNC;
141 * Leap second processing. If in leap-insert state at the end of the
142 * day, the system clock is set back one second; if in leap-delete
143 * state, the system clock is set ahead one second. The microtime()
144 * routine or external clock driver will insure that reported time is
145 * always monotonic. The ugly divides should be replaced.
147 switch (time_state) {
149 if (time_status & STA_INS)
150 time_state = TIME_INS;
151 else if (time_status & STA_DEL)
152 time_state = TIME_DEL;
155 if (xtime.tv_sec % 86400 == 0) {
157 wall_to_monotonic.tv_sec++;
158 time_state = TIME_OOP;
159 printk(KERN_NOTICE "Clock: inserting leap second "
164 if ((xtime.tv_sec + 1) % 86400 == 0) {
167 wall_to_monotonic.tv_sec--;
168 time_state = TIME_WAIT;
169 printk(KERN_NOTICE "Clock: deleting leap second "
175 time_state = TIME_WAIT;
178 if (!(time_status & (STA_INS | STA_DEL)))
179 time_state = TIME_OK;
183 * Compute the phase adjustment for the next second. The offset is
184 * reduced by a fixed factor times the time constant.
186 tick_length = tick_length_base;
187 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
188 time_offset -= time_adj;
189 tick_length += time_adj;
191 if (unlikely(time_adjust)) {
192 if (time_adjust > MAX_TICKADJ) {
193 time_adjust -= MAX_TICKADJ;
194 tick_length += MAX_TICKADJ_SCALED;
195 } else if (time_adjust < -MAX_TICKADJ) {
196 time_adjust += MAX_TICKADJ;
197 tick_length -= MAX_TICKADJ_SCALED;
199 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
200 NTP_INTERVAL_FREQ) << NTP_SCALE_SHIFT;
207 * Return how long ticks are at the moment, that is, how much time
208 * update_wall_time_one_tick will add to xtime next time we call it
209 * (assuming no calls to do_adjtimex in the meantime).
210 * The return value is in fixed-point nanoseconds shifted by the
211 * specified number of bits to the right of the binary point.
212 * This function has no side-effects.
214 u64 current_tick_length(void)
219 #ifdef CONFIG_GENERIC_CMOS_UPDATE
221 /* Disable the cmos update - used by virtualization and embedded */
222 int no_sync_cmos_clock __read_mostly;
224 static void sync_cmos_clock(unsigned long dummy);
226 static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0);
228 static void sync_cmos_clock(unsigned long dummy)
230 struct timespec now, next;
234 * If we have an externally synchronized Linux clock, then update
235 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
236 * called as close as possible to 500 ms before the new second starts.
237 * This code is run on a timer. If the clock is set, that timer
238 * may not expire at the correct time. Thus, we adjust...
242 * Not synced, exit, do not restart a timer (if one is
243 * running, let it run out).
247 getnstimeofday(&now);
248 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
249 fail = update_persistent_clock(now);
251 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec;
252 if (next.tv_nsec <= 0)
253 next.tv_nsec += NSEC_PER_SEC;
260 if (next.tv_nsec >= NSEC_PER_SEC) {
262 next.tv_nsec -= NSEC_PER_SEC;
264 mod_timer(&sync_cmos_timer, jiffies + timespec_to_jiffies(&next));
267 static void notify_cmos_timer(void)
269 if (!no_sync_cmos_clock)
270 mod_timer(&sync_cmos_timer, jiffies + 1);
274 static inline void notify_cmos_timer(void) { }
277 /* adjtimex mainly allows reading (and writing, if superuser) of
278 * kernel time-keeping variables. used by xntpd.
280 int do_adjtimex(struct timex *txc)
286 /* In order to modify anything, you gotta be super-user! */
287 if (txc->modes && !capable(CAP_SYS_TIME))
290 /* Now we validate the data before disabling interrupts */
292 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) {
293 /* singleshot must not be used with any other mode bits */
294 if (txc->modes & ~ADJ_OFFSET_SS_READ)
298 /* if the quartz is off by more than 10% something is VERY wrong ! */
299 if (txc->modes & ADJ_TICK)
300 if (txc->tick < 900000/USER_HZ ||
301 txc->tick > 1100000/USER_HZ)
304 write_seqlock_irq(&xtime_lock);
306 /* Save for later - semantics of adjtime is to return old value */
307 save_adjust = time_adjust;
309 /* If there are input parameters, then process them */
311 if (txc->modes & ADJ_STATUS) {
312 if ((time_status & STA_PLL) &&
313 !(txc->status & STA_PLL)) {
314 time_state = TIME_OK;
315 time_status = STA_UNSYNC;
317 /* only set allowed bits */
318 time_status &= STA_RONLY;
319 time_status |= txc->status & ~STA_RONLY;
322 if (txc->modes & ADJ_NANO)
323 time_status |= STA_NANO;
324 if (txc->modes & ADJ_MICRO)
325 time_status &= ~STA_NANO;
327 if (txc->modes & ADJ_FREQUENCY) {
328 time_freq = (s64)txc->freq * PPM_SCALE;
329 time_freq = min(time_freq, MAXFREQ_SCALED);
330 time_freq = max(time_freq, -MAXFREQ_SCALED);
333 if (txc->modes & ADJ_MAXERROR)
334 time_maxerror = txc->maxerror;
335 if (txc->modes & ADJ_ESTERROR)
336 time_esterror = txc->esterror;
338 if (txc->modes & ADJ_TIMECONST) {
339 time_constant = txc->constant;
340 if (!(time_status & STA_NANO))
342 time_constant = min(time_constant, (long)MAXTC);
343 time_constant = max(time_constant, 0l);
346 if (txc->modes & ADJ_TAI && txc->constant > 0)
347 time_tai = txc->constant;
349 if (txc->modes & ADJ_OFFSET) {
350 if (txc->modes == ADJ_OFFSET_SINGLESHOT)
351 /* adjtime() is independent from ntp_adjtime() */
352 time_adjust = txc->offset;
354 ntp_update_offset(txc->offset);
356 if (txc->modes & ADJ_TICK)
357 tick_usec = txc->tick;
359 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
360 ntp_update_frequency();
363 result = time_state; /* mostly `TIME_OK' */
364 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
367 if ((txc->modes == ADJ_OFFSET_SINGLESHOT) ||
368 (txc->modes == ADJ_OFFSET_SS_READ))
369 txc->offset = save_adjust;
371 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
373 if (!(time_status & STA_NANO))
374 txc->offset /= NSEC_PER_USEC;
376 txc->freq = shift_right((s32)(time_freq >> PPM_SCALE_INV_SHIFT) *
379 txc->maxerror = time_maxerror;
380 txc->esterror = time_esterror;
381 txc->status = time_status;
382 txc->constant = time_constant;
384 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
385 txc->tick = tick_usec;
388 /* PPS is not implemented, so these are zero */
397 write_sequnlock_irq(&xtime_lock);
400 txc->time.tv_sec = ts.tv_sec;
401 txc->time.tv_usec = ts.tv_nsec;
402 if (!(time_status & STA_NANO))
403 txc->time.tv_usec /= NSEC_PER_USEC;
410 static int __init ntp_tick_adj_setup(char *str)
412 ntp_tick_adj = simple_strtol(str, NULL, 0);
416 __setup("ntp_tick_adj=", ntp_tick_adj_setup);