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 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) */
45 static long ntp_tick_adj;
47 static void ntp_update_frequency(void)
49 u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
51 second_length += (s64)ntp_tick_adj << TICK_LENGTH_SHIFT;
52 second_length += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC);
54 tick_length_base = second_length;
56 tick_nsec = div_u64(second_length, HZ) >> TICK_LENGTH_SHIFT;
57 tick_length_base = div_u64(tick_length_base, NTP_INTERVAL_FREQ);
60 static void ntp_update_offset(long offset)
65 if (!(time_status & STA_PLL))
69 if (!(time_status & STA_NANO))
70 time_offset *= NSEC_PER_USEC;
73 * Scale the phase adjustment and
74 * clamp to the operating range.
76 time_offset = min(time_offset, (s64)MAXPHASE * NSEC_PER_USEC);
77 time_offset = max(time_offset, (s64)-MAXPHASE * NSEC_PER_USEC);
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 = time_offset * mtemp;
89 freq_adj = shift_right(freq_adj, time_constant * 2 +
90 (SHIFT_PLL + 2) * 2 - SHIFT_NSEC);
91 time_status &= ~STA_MODE;
92 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
93 freq_adj += div_s64(time_offset << (SHIFT_NSEC - SHIFT_FLL), mtemp);
94 time_status |= STA_MODE;
96 freq_adj += time_freq;
97 freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC);
98 time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC);
99 time_offset = div_s64(time_offset, NTP_INTERVAL_FREQ);
100 time_offset <<= SHIFT_UPDATE;
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 >> SHIFT_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) {
166 wall_to_monotonic.tv_sec--;
167 time_state = TIME_WAIT;
168 printk(KERN_NOTICE "Clock: deleting leap second "
173 time_state = TIME_WAIT;
176 if (!(time_status & (STA_INS | STA_DEL)))
177 time_state = TIME_OK;
181 * Compute the phase adjustment for the next second. The offset is
182 * reduced by a fixed factor times the time constant.
184 tick_length = tick_length_base;
185 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
186 time_offset -= time_adj;
187 tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE);
189 if (unlikely(time_adjust)) {
190 if (time_adjust > MAX_TICKADJ) {
191 time_adjust -= MAX_TICKADJ;
192 tick_length += MAX_TICKADJ_SCALED;
193 } else if (time_adjust < -MAX_TICKADJ) {
194 time_adjust += MAX_TICKADJ;
195 tick_length -= MAX_TICKADJ_SCALED;
197 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
198 NTP_INTERVAL_FREQ) << TICK_LENGTH_SHIFT;
205 * Return how long ticks are at the moment, that is, how much time
206 * update_wall_time_one_tick will add to xtime next time we call it
207 * (assuming no calls to do_adjtimex in the meantime).
208 * The return value is in fixed-point nanoseconds shifted by the
209 * specified number of bits to the right of the binary point.
210 * This function has no side-effects.
212 u64 current_tick_length(void)
217 #ifdef CONFIG_GENERIC_CMOS_UPDATE
219 /* Disable the cmos update - used by virtualization and embedded */
220 int no_sync_cmos_clock __read_mostly;
222 static void sync_cmos_clock(unsigned long dummy);
224 static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0);
226 static void sync_cmos_clock(unsigned long dummy)
228 struct timespec now, next;
232 * If we have an externally synchronized Linux clock, then update
233 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
234 * called as close as possible to 500 ms before the new second starts.
235 * This code is run on a timer. If the clock is set, that timer
236 * may not expire at the correct time. Thus, we adjust...
240 * Not synced, exit, do not restart a timer (if one is
241 * running, let it run out).
245 getnstimeofday(&now);
246 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
247 fail = update_persistent_clock(now);
249 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec;
250 if (next.tv_nsec <= 0)
251 next.tv_nsec += NSEC_PER_SEC;
258 if (next.tv_nsec >= NSEC_PER_SEC) {
260 next.tv_nsec -= NSEC_PER_SEC;
262 mod_timer(&sync_cmos_timer, jiffies + timespec_to_jiffies(&next));
265 static void notify_cmos_timer(void)
267 if (!no_sync_cmos_clock)
268 mod_timer(&sync_cmos_timer, jiffies + 1);
272 static inline void notify_cmos_timer(void) { }
275 /* adjtimex mainly allows reading (and writing, if superuser) of
276 * kernel time-keeping variables. used by xntpd.
278 int do_adjtimex(struct timex *txc)
284 /* In order to modify anything, you gotta be super-user! */
285 if (txc->modes && !capable(CAP_SYS_TIME))
288 /* Now we validate the data before disabling interrupts */
290 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) {
291 /* singleshot must not be used with any other mode bits */
292 if (txc->modes & ~ADJ_OFFSET_SS_READ)
296 /* if the quartz is off by more than 10% something is VERY wrong ! */
297 if (txc->modes & ADJ_TICK)
298 if (txc->tick < 900000/USER_HZ ||
299 txc->tick > 1100000/USER_HZ)
302 write_seqlock_irq(&xtime_lock);
304 /* Save for later - semantics of adjtime is to return old value */
305 save_adjust = time_adjust;
307 /* If there are input parameters, then process them */
309 if (txc->modes & ADJ_STATUS) {
310 if ((time_status & STA_PLL) &&
311 !(txc->status & STA_PLL)) {
312 time_state = TIME_OK;
313 time_status = STA_UNSYNC;
315 /* only set allowed bits */
316 time_status &= STA_RONLY;
317 time_status |= txc->status & ~STA_RONLY;
320 if (txc->modes & ADJ_NANO)
321 time_status |= STA_NANO;
322 if (txc->modes & ADJ_MICRO)
323 time_status &= ~STA_NANO;
325 if (txc->modes & ADJ_FREQUENCY) {
326 time_freq = min(txc->freq, MAXFREQ);
327 time_freq = min(time_freq, -MAXFREQ);
328 time_freq = ((s64)time_freq * NSEC_PER_USEC)
329 >> (SHIFT_USEC - SHIFT_NSEC);
332 if (txc->modes & ADJ_MAXERROR)
333 time_maxerror = txc->maxerror;
334 if (txc->modes & ADJ_ESTERROR)
335 time_esterror = txc->esterror;
337 if (txc->modes & ADJ_TIMECONST) {
338 time_constant = txc->constant;
339 if (!(time_status & STA_NANO))
341 time_constant = min(time_constant, (long)MAXTC);
342 time_constant = max(time_constant, 0l);
345 if (txc->modes & ADJ_OFFSET) {
346 if (txc->modes == ADJ_OFFSET_SINGLESHOT)
347 /* adjtime() is independent from ntp_adjtime() */
348 time_adjust = txc->offset;
350 ntp_update_offset(txc->offset);
352 if (txc->modes & ADJ_TICK)
353 tick_usec = txc->tick;
355 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
356 ntp_update_frequency();
359 result = time_state; /* mostly `TIME_OK' */
360 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
363 if ((txc->modes == ADJ_OFFSET_SINGLESHOT) ||
364 (txc->modes == ADJ_OFFSET_SS_READ))
365 txc->offset = save_adjust;
367 txc->offset = ((long)shift_right(time_offset, SHIFT_UPDATE)) *
369 if (!(time_status & STA_NANO))
370 txc->offset /= NSEC_PER_USEC;
372 txc->freq = (time_freq / NSEC_PER_USEC) <<
373 (SHIFT_USEC - SHIFT_NSEC);
374 txc->maxerror = time_maxerror;
375 txc->esterror = time_esterror;
376 txc->status = time_status;
377 txc->constant = time_constant;
379 txc->tolerance = MAXFREQ;
380 txc->tick = tick_usec;
382 /* PPS is not implemented, so these are zero */
391 write_sequnlock_irq(&xtime_lock);
394 txc->time.tv_sec = ts.tv_sec;
395 txc->time.tv_usec = ts.tv_nsec;
396 if (!(time_status & STA_NANO))
397 txc->time.tv_usec /= NSEC_PER_USEC;
404 static int __init ntp_tick_adj_setup(char *str)
406 ntp_tick_adj = simple_strtol(str, NULL, 0);
410 __setup("ntp_tick_adj=", ntp_tick_adj_setup);