2 * NTP state machine interfaces and logic.
4 * This code was mainly moved from kernel/timer.c and kernel/time.c
5 * Please see those files for relevant copyright info and historical
8 #include <linux/capability.h>
9 #include <linux/clocksource.h>
10 #include <linux/workqueue.h>
11 #include <linux/hrtimer.h>
12 #include <linux/jiffies.h>
13 #include <linux/math64.h>
14 #include <linux/timex.h>
15 #include <linux/time.h>
19 * NTP timekeeping variables:
22 /* USER_HZ period (usecs): */
23 unsigned long tick_usec = TICK_USEC;
25 /* ACTHZ period (nsecs): */
26 unsigned long tick_nsec;
29 static u64 tick_length_base;
31 static struct hrtimer leap_timer;
33 #define MAX_TICKADJ 500LL /* usecs */
34 #define MAX_TICKADJ_SCALED \
35 (((MAX_TICKADJ * NSEC_PER_USEC) << NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
38 * phase-lock loop variables
42 * clock synchronization status
44 * (TIME_ERROR prevents overwriting the CMOS clock)
46 static int time_state = TIME_OK;
48 /* clock status bits: */
49 int time_status = STA_UNSYNC;
51 /* TAI offset (secs): */
54 /* time adjustment (nsecs): */
55 static s64 time_offset;
57 /* pll time constant: */
58 static long time_constant = 2;
60 /* maximum error (usecs): */
61 long time_maxerror = NTP_PHASE_LIMIT;
63 /* estimated error (usecs): */
64 long time_esterror = NTP_PHASE_LIMIT;
66 /* frequency offset (scaled nsecs/secs): */
69 /* time at last adjustment (secs): */
70 static long time_reftime;
74 static long ntp_tick_adj;
81 * Update (tick_length, tick_length_base, tick_nsec), based
82 * on (tick_usec, ntp_tick_adj, time_freq):
84 static void ntp_update_frequency(void)
89 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
92 second_length += (s64)ntp_tick_adj << NTP_SCALE_SHIFT;
93 second_length += time_freq;
95 tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
96 new_base = div_u64(second_length, NTP_INTERVAL_FREQ);
99 * Don't wait for the next second_overflow, apply
100 * the change to the tick length immediately:
102 tick_length += new_base - tick_length_base;
103 tick_length_base = new_base;
106 static void ntp_update_offset(long offset)
111 if (!(time_status & STA_PLL))
114 if (!(time_status & STA_NANO))
115 offset *= NSEC_PER_USEC;
118 * Scale the phase adjustment and
119 * clamp to the operating range.
121 offset = min(offset, MAXPHASE);
122 offset = max(offset, -MAXPHASE);
125 * Select how the frequency is to be controlled
126 * and in which mode (PLL or FLL).
128 if (time_status & STA_FREQHOLD || time_reftime == 0)
129 time_reftime = xtime.tv_sec;
130 mtemp = xtime.tv_sec - time_reftime;
131 time_reftime = xtime.tv_sec;
133 freq_adj = (s64)offset * mtemp;
134 freq_adj <<= NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant);
135 time_status &= ~STA_MODE;
136 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
137 freq_adj += div_s64((s64)offset << (NTP_SCALE_SHIFT - SHIFT_FLL),
139 time_status |= STA_MODE;
141 freq_adj += time_freq;
142 freq_adj = min(freq_adj, MAXFREQ_SCALED);
143 time_freq = max(freq_adj, -MAXFREQ_SCALED);
145 time_offset = div_s64((s64)offset << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
149 * ntp_clear - Clears the NTP state variables
151 * Must be called while holding a write on the xtime_lock
155 time_adjust = 0; /* stop active adjtime() */
156 time_status |= STA_UNSYNC;
157 time_maxerror = NTP_PHASE_LIMIT;
158 time_esterror = NTP_PHASE_LIMIT;
160 ntp_update_frequency();
162 tick_length = tick_length_base;
167 * Leap second processing. If in leap-insert state at the end of the
168 * day, the system clock is set back one second; if in leap-delete
169 * state, the system clock is set ahead one second.
171 static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
173 enum hrtimer_restart res = HRTIMER_NORESTART;
175 write_seqlock(&xtime_lock);
177 switch (time_state) {
182 wall_to_monotonic.tv_sec++;
183 time_state = TIME_OOP;
185 "Clock: inserting leap second 23:59:60 UTC\n");
186 hrtimer_add_expires_ns(&leap_timer, NSEC_PER_SEC);
187 res = HRTIMER_RESTART;
192 wall_to_monotonic.tv_sec--;
193 time_state = TIME_WAIT;
195 "Clock: deleting leap second 23:59:59 UTC\n");
199 time_state = TIME_WAIT;
202 if (!(time_status & (STA_INS | STA_DEL)))
203 time_state = TIME_OK;
206 update_vsyscall(&xtime, clock);
208 write_sequnlock(&xtime_lock);
214 * this routine handles the overflow of the microsecond field
216 * The tricky bits of code to handle the accurate clock support
217 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
218 * They were originally developed for SUN and DEC kernels.
219 * All the kudos should go to Dave for this stuff.
221 void second_overflow(void)
225 /* Bump the maxerror field */
226 time_maxerror += MAXFREQ / NSEC_PER_USEC;
227 if (time_maxerror > NTP_PHASE_LIMIT) {
228 time_maxerror = NTP_PHASE_LIMIT;
229 time_status |= STA_UNSYNC;
233 * Compute the phase adjustment for the next second. The offset is
234 * reduced by a fixed factor times the time constant.
236 tick_length = tick_length_base;
237 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
238 time_offset -= time_adj;
239 tick_length += time_adj;
244 if (time_adjust > MAX_TICKADJ) {
245 time_adjust -= MAX_TICKADJ;
246 tick_length += MAX_TICKADJ_SCALED;
250 if (time_adjust < -MAX_TICKADJ) {
251 time_adjust += MAX_TICKADJ;
252 tick_length -= MAX_TICKADJ_SCALED;
256 tick_length += (s64)(time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ)
261 #ifdef CONFIG_GENERIC_CMOS_UPDATE
263 /* Disable the cmos update - used by virtualization and embedded */
264 int no_sync_cmos_clock __read_mostly;
266 static void sync_cmos_clock(struct work_struct *work);
268 static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
270 static void sync_cmos_clock(struct work_struct *work)
272 struct timespec now, next;
276 * If we have an externally synchronized Linux clock, then update
277 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
278 * called as close as possible to 500 ms before the new second starts.
279 * This code is run on a timer. If the clock is set, that timer
280 * may not expire at the correct time. Thus, we adjust...
284 * Not synced, exit, do not restart a timer (if one is
285 * running, let it run out).
290 getnstimeofday(&now);
291 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
292 fail = update_persistent_clock(now);
294 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2);
295 if (next.tv_nsec <= 0)
296 next.tv_nsec += NSEC_PER_SEC;
303 if (next.tv_nsec >= NSEC_PER_SEC) {
305 next.tv_nsec -= NSEC_PER_SEC;
307 schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next));
310 static void notify_cmos_timer(void)
312 if (!no_sync_cmos_clock)
313 schedule_delayed_work(&sync_cmos_work, 0);
317 static inline void notify_cmos_timer(void) { }
321 * adjtimex mainly allows reading (and writing, if superuser) of
322 * kernel time-keeping variables. used by xntpd.
324 int do_adjtimex(struct timex *txc)
329 /* Validate the data before disabling interrupts */
330 if (txc->modes & ADJ_ADJTIME) {
331 /* singleshot must not be used with any other mode bits */
332 if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
334 if (!(txc->modes & ADJ_OFFSET_READONLY) &&
335 !capable(CAP_SYS_TIME))
338 /* In order to modify anything, you gotta be super-user! */
339 if (txc->modes && !capable(CAP_SYS_TIME))
343 * if the quartz is off by more than 10% then
344 * something is VERY wrong!
346 if (txc->modes & ADJ_TICK &&
347 (txc->tick < 900000/USER_HZ ||
348 txc->tick > 1100000/USER_HZ))
351 if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
352 hrtimer_cancel(&leap_timer);
357 write_seqlock_irq(&xtime_lock);
359 /* If there are input parameters, then process them */
360 if (txc->modes & ADJ_ADJTIME) {
361 long save_adjust = time_adjust;
363 if (!(txc->modes & ADJ_OFFSET_READONLY)) {
364 /* adjtime() is independent from ntp_adjtime() */
365 time_adjust = txc->offset;
366 ntp_update_frequency();
368 txc->offset = save_adjust;
374 if (txc->modes & ADJ_STATUS) {
375 if ((time_status & STA_PLL) &&
376 !(txc->status & STA_PLL)) {
377 time_state = TIME_OK;
378 time_status = STA_UNSYNC;
380 /* only set allowed bits */
381 time_status &= STA_RONLY;
382 time_status |= txc->status & ~STA_RONLY;
384 switch (time_state) {
388 if (time_status & STA_INS) {
389 time_state = TIME_INS;
390 sec += 86400 - sec % 86400;
391 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
392 } else if (time_status & STA_DEL) {
393 time_state = TIME_DEL;
394 sec += 86400 - (sec + 1) % 86400;
395 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
400 time_state = TIME_OK;
404 if (!(time_status & (STA_INS | STA_DEL)))
405 time_state = TIME_OK;
408 hrtimer_restart(&leap_timer);
413 if (txc->modes & ADJ_NANO)
414 time_status |= STA_NANO;
415 if (txc->modes & ADJ_MICRO)
416 time_status &= ~STA_NANO;
418 if (txc->modes & ADJ_FREQUENCY) {
419 time_freq = (s64)txc->freq * PPM_SCALE;
420 time_freq = min(time_freq, MAXFREQ_SCALED);
421 time_freq = max(time_freq, -MAXFREQ_SCALED);
424 if (txc->modes & ADJ_MAXERROR)
425 time_maxerror = txc->maxerror;
426 if (txc->modes & ADJ_ESTERROR)
427 time_esterror = txc->esterror;
429 if (txc->modes & ADJ_TIMECONST) {
430 time_constant = txc->constant;
431 if (!(time_status & STA_NANO))
433 time_constant = min(time_constant, (long)MAXTC);
434 time_constant = max(time_constant, 0l);
437 if (txc->modes & ADJ_TAI && txc->constant > 0)
438 time_tai = txc->constant;
440 if (txc->modes & ADJ_OFFSET)
441 ntp_update_offset(txc->offset);
442 if (txc->modes & ADJ_TICK)
443 tick_usec = txc->tick;
445 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
446 ntp_update_frequency();
449 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
451 if (!(time_status & STA_NANO))
452 txc->offset /= NSEC_PER_USEC;
455 result = time_state; /* mostly `TIME_OK' */
456 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
459 txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
460 (s64)PPM_SCALE_INV, NTP_SCALE_SHIFT);
461 txc->maxerror = time_maxerror;
462 txc->esterror = time_esterror;
463 txc->status = time_status;
464 txc->constant = time_constant;
466 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
467 txc->tick = tick_usec;
470 /* PPS is not implemented, so these are zero */
479 write_sequnlock_irq(&xtime_lock);
481 txc->time.tv_sec = ts.tv_sec;
482 txc->time.tv_usec = ts.tv_nsec;
483 if (!(time_status & STA_NANO))
484 txc->time.tv_usec /= NSEC_PER_USEC;
491 static int __init ntp_tick_adj_setup(char *str)
493 ntp_tick_adj = simple_strtol(str, NULL, 0);
497 __setup("ntp_tick_adj=", ntp_tick_adj_setup);
499 void __init ntp_init(void)
502 hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
503 leap_timer.function = ntp_leap_second;