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 <asm/div64.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) */
46 #define CLOCK_TICK_OVERFLOW (LATCH * HZ - CLOCK_TICK_RATE)
47 #define CLOCK_TICK_ADJUST (((s64)CLOCK_TICK_OVERFLOW * NSEC_PER_SEC) / \
50 static void ntp_update_frequency(void)
52 u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
54 second_length += (s64)CLOCK_TICK_ADJUST << TICK_LENGTH_SHIFT;
55 second_length += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC);
57 tick_length_base = second_length;
59 do_div(second_length, HZ);
60 tick_nsec = second_length >> TICK_LENGTH_SHIFT;
62 do_div(tick_length_base, NTP_INTERVAL_FREQ);
66 * ntp_clear - Clears the NTP state variables
68 * Must be called while holding a write on the xtime_lock
72 time_adjust = 0; /* stop active adjtime() */
73 time_status |= STA_UNSYNC;
74 time_maxerror = NTP_PHASE_LIMIT;
75 time_esterror = NTP_PHASE_LIMIT;
77 ntp_update_frequency();
79 tick_length = tick_length_base;
84 * this routine handles the overflow of the microsecond field
86 * The tricky bits of code to handle the accurate clock support
87 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
88 * They were originally developed for SUN and DEC kernels.
89 * All the kudos should go to Dave for this stuff.
91 void second_overflow(void)
95 /* Bump the maxerror field */
96 time_maxerror += MAXFREQ >> SHIFT_USEC;
97 if (time_maxerror > NTP_PHASE_LIMIT) {
98 time_maxerror = NTP_PHASE_LIMIT;
99 time_status |= STA_UNSYNC;
103 * Leap second processing. If in leap-insert state at the end of the
104 * day, the system clock is set back one second; if in leap-delete
105 * state, the system clock is set ahead one second. The microtime()
106 * routine or external clock driver will insure that reported time is
107 * always monotonic. The ugly divides should be replaced.
109 switch (time_state) {
111 if (time_status & STA_INS)
112 time_state = TIME_INS;
113 else if (time_status & STA_DEL)
114 time_state = TIME_DEL;
117 if (xtime.tv_sec % 86400 == 0) {
119 wall_to_monotonic.tv_sec++;
120 time_state = TIME_OOP;
121 printk(KERN_NOTICE "Clock: inserting leap second "
126 if ((xtime.tv_sec + 1) % 86400 == 0) {
128 wall_to_monotonic.tv_sec--;
129 time_state = TIME_WAIT;
130 printk(KERN_NOTICE "Clock: deleting leap second "
135 time_state = TIME_WAIT;
138 if (!(time_status & (STA_INS | STA_DEL)))
139 time_state = TIME_OK;
143 * Compute the phase adjustment for the next second. The offset is
144 * reduced by a fixed factor times the time constant.
146 tick_length = tick_length_base;
147 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
148 time_offset -= time_adj;
149 tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE);
151 if (unlikely(time_adjust)) {
152 if (time_adjust > MAX_TICKADJ) {
153 time_adjust -= MAX_TICKADJ;
154 tick_length += MAX_TICKADJ_SCALED;
155 } else if (time_adjust < -MAX_TICKADJ) {
156 time_adjust += MAX_TICKADJ;
157 tick_length -= MAX_TICKADJ_SCALED;
159 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
160 NTP_INTERVAL_FREQ) << TICK_LENGTH_SHIFT;
167 * Return how long ticks are at the moment, that is, how much time
168 * update_wall_time_one_tick will add to xtime next time we call it
169 * (assuming no calls to do_adjtimex in the meantime).
170 * The return value is in fixed-point nanoseconds shifted by the
171 * specified number of bits to the right of the binary point.
172 * This function has no side-effects.
174 u64 current_tick_length(void)
179 #ifdef CONFIG_GENERIC_CMOS_UPDATE
181 /* Disable the cmos update - used by virtualization and embedded */
182 int no_sync_cmos_clock __read_mostly;
184 static void sync_cmos_clock(unsigned long dummy);
186 static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0);
188 static void sync_cmos_clock(unsigned long dummy)
190 struct timespec now, next;
194 * If we have an externally synchronized Linux clock, then update
195 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
196 * called as close as possible to 500 ms before the new second starts.
197 * This code is run on a timer. If the clock is set, that timer
198 * may not expire at the correct time. Thus, we adjust...
202 * Not synced, exit, do not restart a timer (if one is
203 * running, let it run out).
207 getnstimeofday(&now);
208 if (abs(xtime.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
209 fail = update_persistent_clock(now);
211 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec;
212 if (next.tv_nsec <= 0)
213 next.tv_nsec += NSEC_PER_SEC;
220 if (next.tv_nsec >= NSEC_PER_SEC) {
222 next.tv_nsec -= NSEC_PER_SEC;
224 mod_timer(&sync_cmos_timer, jiffies + timespec_to_jiffies(&next));
227 static void notify_cmos_timer(void)
229 if (no_sync_cmos_clock)
230 mod_timer(&sync_cmos_timer, jiffies + 1);
234 static inline void notify_cmos_timer(void) { }
237 /* adjtimex mainly allows reading (and writing, if superuser) of
238 * kernel time-keeping variables. used by xntpd.
240 int do_adjtimex(struct timex *txc)
242 long mtemp, save_adjust, rem;
243 s64 freq_adj, temp64;
246 /* In order to modify anything, you gotta be super-user! */
247 if (txc->modes && !capable(CAP_SYS_TIME))
250 /* Now we validate the data before disabling interrupts */
252 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
253 /* singleshot must not be used with any other mode bits */
254 if (txc->modes != ADJ_OFFSET_SINGLESHOT)
257 if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
258 /* adjustment Offset limited to +- .512 seconds */
259 if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
262 /* if the quartz is off by more than 10% something is VERY wrong ! */
263 if (txc->modes & ADJ_TICK)
264 if (txc->tick < 900000/USER_HZ ||
265 txc->tick > 1100000/USER_HZ)
268 write_seqlock_irq(&xtime_lock);
269 result = time_state; /* mostly `TIME_OK' */
271 /* Save for later - semantics of adjtime is to return old value */
272 save_adjust = time_adjust;
274 #if 0 /* STA_CLOCKERR is never set yet */
275 time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
277 /* If there are input parameters, then process them */
280 if (txc->modes & ADJ_STATUS) /* only set allowed bits */
281 time_status = (txc->status & ~STA_RONLY) |
282 (time_status & STA_RONLY);
284 if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
285 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
289 time_freq = ((s64)txc->freq * NSEC_PER_USEC)
290 >> (SHIFT_USEC - SHIFT_NSEC);
293 if (txc->modes & ADJ_MAXERROR) {
294 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
298 time_maxerror = txc->maxerror;
301 if (txc->modes & ADJ_ESTERROR) {
302 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
306 time_esterror = txc->esterror;
309 if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
310 if (txc->constant < 0) { /* NTP v4 uses values > 6 */
314 time_constant = min(txc->constant + 4, (long)MAXTC);
317 if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
318 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
319 /* adjtime() is independent from ntp_adjtime() */
320 time_adjust = txc->offset;
322 else if (time_status & STA_PLL) {
323 time_offset = txc->offset * NSEC_PER_USEC;
326 * Scale the phase adjustment and
327 * clamp to the operating range.
329 time_offset = min(time_offset, (s64)MAXPHASE * NSEC_PER_USEC);
330 time_offset = max(time_offset, (s64)-MAXPHASE * NSEC_PER_USEC);
333 * Select whether the frequency is to be controlled
334 * and in which mode (PLL or FLL). Clamp to the operating
335 * range. Ugly multiply/divide should be replaced someday.
338 if (time_status & STA_FREQHOLD || time_reftime == 0)
339 time_reftime = xtime.tv_sec;
340 mtemp = xtime.tv_sec - time_reftime;
341 time_reftime = xtime.tv_sec;
343 freq_adj = time_offset * mtemp;
344 freq_adj = shift_right(freq_adj, time_constant * 2 +
345 (SHIFT_PLL + 2) * 2 - SHIFT_NSEC);
346 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
347 temp64 = time_offset << (SHIFT_NSEC - SHIFT_FLL);
348 if (time_offset < 0) {
350 do_div(temp64, mtemp);
353 do_div(temp64, mtemp);
357 freq_adj += time_freq;
358 freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC);
359 time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC);
360 time_offset = div_long_long_rem_signed(time_offset,
363 time_offset <<= SHIFT_UPDATE;
365 } /* txc->modes & ADJ_OFFSET */
366 if (txc->modes & ADJ_TICK)
367 tick_usec = txc->tick;
369 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
370 ntp_update_frequency();
372 leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
375 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
376 txc->offset = save_adjust;
378 txc->offset = ((long)shift_right(time_offset, SHIFT_UPDATE)) *
379 NTP_INTERVAL_FREQ / 1000;
380 txc->freq = (time_freq / NSEC_PER_USEC) <<
381 (SHIFT_USEC - SHIFT_NSEC);
382 txc->maxerror = time_maxerror;
383 txc->esterror = time_esterror;
384 txc->status = time_status;
385 txc->constant = time_constant;
387 txc->tolerance = MAXFREQ;
388 txc->tick = tick_usec;
390 /* PPS is not implemented, so these are zero */
399 write_sequnlock_irq(&xtime_lock);
400 do_gettimeofday(&txc->time);