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 static s64 time_freq; /* frequency offset (scaled ns/s)*/
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 += time_freq;
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))
68 if (!(time_status & STA_NANO))
69 offset *= NSEC_PER_USEC;
72 * Scale the phase adjustment and
73 * clamp to the operating range.
75 offset = min(offset, MAXPHASE);
76 offset = max(offset, -MAXPHASE);
79 * Select how the frequency is to be controlled
80 * and in which mode (PLL or FLL).
82 if (time_status & STA_FREQHOLD || time_reftime == 0)
83 time_reftime = xtime.tv_sec;
84 mtemp = xtime.tv_sec - time_reftime;
85 time_reftime = xtime.tv_sec;
87 freq_adj = (s64)offset * mtemp;
88 freq_adj <<= TICK_LENGTH_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant);
89 time_status &= ~STA_MODE;
90 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
91 freq_adj += div_s64((s64)offset << (TICK_LENGTH_SHIFT - SHIFT_FLL),
93 time_status |= STA_MODE;
95 freq_adj += time_freq;
96 freq_adj = min(freq_adj, MAXFREQ_SCALED);
97 time_freq = max(freq_adj, -MAXFREQ_SCALED);
99 time_offset = div_s64((s64)offset << TICK_LENGTH_SHIFT, NTP_INTERVAL_FREQ);
103 * ntp_clear - Clears the NTP state variables
105 * Must be called while holding a write on the xtime_lock
109 time_adjust = 0; /* stop active adjtime() */
110 time_status |= STA_UNSYNC;
111 time_maxerror = NTP_PHASE_LIMIT;
112 time_esterror = NTP_PHASE_LIMIT;
114 ntp_update_frequency();
116 tick_length = tick_length_base;
121 * this routine handles the overflow of the microsecond field
123 * The tricky bits of code to handle the accurate clock support
124 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
125 * They were originally developed for SUN and DEC kernels.
126 * All the kudos should go to Dave for this stuff.
128 void second_overflow(void)
132 /* Bump the maxerror field */
133 time_maxerror += MAXFREQ / NSEC_PER_USEC;
134 if (time_maxerror > NTP_PHASE_LIMIT) {
135 time_maxerror = NTP_PHASE_LIMIT;
136 time_status |= STA_UNSYNC;
140 * Leap second processing. If in leap-insert state at the end of the
141 * day, the system clock is set back one second; if in leap-delete
142 * state, the system clock is set ahead one second. The microtime()
143 * routine or external clock driver will insure that reported time is
144 * always monotonic. The ugly divides should be replaced.
146 switch (time_state) {
148 if (time_status & STA_INS)
149 time_state = TIME_INS;
150 else if (time_status & STA_DEL)
151 time_state = TIME_DEL;
154 if (xtime.tv_sec % 86400 == 0) {
156 wall_to_monotonic.tv_sec++;
157 time_state = TIME_OOP;
158 printk(KERN_NOTICE "Clock: inserting leap second "
163 if ((xtime.tv_sec + 1) % 86400 == 0) {
165 wall_to_monotonic.tv_sec--;
166 time_state = TIME_WAIT;
167 printk(KERN_NOTICE "Clock: deleting leap second "
172 time_state = TIME_WAIT;
175 if (!(time_status & (STA_INS | STA_DEL)))
176 time_state = TIME_OK;
180 * Compute the phase adjustment for the next second. The offset is
181 * reduced by a fixed factor times the time constant.
183 tick_length = tick_length_base;
184 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
185 time_offset -= time_adj;
186 tick_length += time_adj;
188 if (unlikely(time_adjust)) {
189 if (time_adjust > MAX_TICKADJ) {
190 time_adjust -= MAX_TICKADJ;
191 tick_length += MAX_TICKADJ_SCALED;
192 } else if (time_adjust < -MAX_TICKADJ) {
193 time_adjust += MAX_TICKADJ;
194 tick_length -= MAX_TICKADJ_SCALED;
196 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
197 NTP_INTERVAL_FREQ) << TICK_LENGTH_SHIFT;
204 * Return how long ticks are at the moment, that is, how much time
205 * update_wall_time_one_tick will add to xtime next time we call it
206 * (assuming no calls to do_adjtimex in the meantime).
207 * The return value is in fixed-point nanoseconds shifted by the
208 * specified number of bits to the right of the binary point.
209 * This function has no side-effects.
211 u64 current_tick_length(void)
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)
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 write_seqlock_irq(&xtime_lock);
303 /* Save for later - semantics of adjtime is to return old value */
304 save_adjust = time_adjust;
306 /* If there are input parameters, then process them */
308 if (txc->modes & ADJ_STATUS) {
309 if ((time_status & STA_PLL) &&
310 !(txc->status & STA_PLL)) {
311 time_state = TIME_OK;
312 time_status = STA_UNSYNC;
314 /* only set allowed bits */
315 time_status &= STA_RONLY;
316 time_status |= txc->status & ~STA_RONLY;
319 if (txc->modes & ADJ_NANO)
320 time_status |= STA_NANO;
321 if (txc->modes & ADJ_MICRO)
322 time_status &= ~STA_NANO;
324 if (txc->modes & ADJ_FREQUENCY) {
325 time_freq = (s64)txc->freq * PPM_SCALE;
326 time_freq = min(time_freq, MAXFREQ_SCALED);
327 time_freq = max(time_freq, -MAXFREQ_SCALED);
330 if (txc->modes & ADJ_MAXERROR)
331 time_maxerror = txc->maxerror;
332 if (txc->modes & ADJ_ESTERROR)
333 time_esterror = txc->esterror;
335 if (txc->modes & ADJ_TIMECONST) {
336 time_constant = txc->constant;
337 if (!(time_status & STA_NANO))
339 time_constant = min(time_constant, (long)MAXTC);
340 time_constant = max(time_constant, 0l);
343 if (txc->modes & ADJ_OFFSET) {
344 if (txc->modes == ADJ_OFFSET_SINGLESHOT)
345 /* adjtime() is independent from ntp_adjtime() */
346 time_adjust = txc->offset;
348 ntp_update_offset(txc->offset);
350 if (txc->modes & ADJ_TICK)
351 tick_usec = txc->tick;
353 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
354 ntp_update_frequency();
357 result = time_state; /* mostly `TIME_OK' */
358 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
361 if ((txc->modes == ADJ_OFFSET_SINGLESHOT) ||
362 (txc->modes == ADJ_OFFSET_SS_READ))
363 txc->offset = save_adjust;
365 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
367 if (!(time_status & STA_NANO))
368 txc->offset /= NSEC_PER_USEC;
370 txc->freq = shift_right((s32)(time_freq >> PPM_SCALE_INV_SHIFT) *
373 txc->maxerror = time_maxerror;
374 txc->esterror = time_esterror;
375 txc->status = time_status;
376 txc->constant = time_constant;
378 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
379 txc->tick = tick_usec;
381 /* PPS is not implemented, so these are zero */
390 write_sequnlock_irq(&xtime_lock);
393 txc->time.tv_sec = ts.tv_sec;
394 txc->time.tv_usec = ts.tv_nsec;
395 if (!(time_status & STA_NANO))
396 txc->time.tv_usec /= NSEC_PER_USEC;
403 static int __init ntp_tick_adj_setup(char *str)
405 ntp_tick_adj = simple_strtol(str, NULL, 0);
409 __setup("ntp_tick_adj=", ntp_tick_adj_setup);