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 500 /* usecs */
34 #define MAX_TICKADJ_SCALED \
35 (((u64)(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;
80 static void ntp_update_frequency(void)
82 u64 old_tick_length_base = tick_length_base;
83 u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
85 second_length += (s64)ntp_tick_adj << NTP_SCALE_SHIFT;
86 second_length += time_freq;
88 tick_length_base = second_length;
90 tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
91 tick_length_base = div_u64(tick_length_base, NTP_INTERVAL_FREQ);
94 * Don't wait for the next second_overflow, apply
95 * the change to the tick length immediately
97 tick_length += tick_length_base - old_tick_length_base;
100 static void ntp_update_offset(long offset)
105 if (!(time_status & STA_PLL))
108 if (!(time_status & STA_NANO))
109 offset *= NSEC_PER_USEC;
112 * Scale the phase adjustment and
113 * clamp to the operating range.
115 offset = min(offset, MAXPHASE);
116 offset = max(offset, -MAXPHASE);
119 * Select how the frequency is to be controlled
120 * and in which mode (PLL or FLL).
122 if (time_status & STA_FREQHOLD || time_reftime == 0)
123 time_reftime = xtime.tv_sec;
124 mtemp = xtime.tv_sec - time_reftime;
125 time_reftime = xtime.tv_sec;
127 freq_adj = (s64)offset * mtemp;
128 freq_adj <<= NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant);
129 time_status &= ~STA_MODE;
130 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
131 freq_adj += div_s64((s64)offset << (NTP_SCALE_SHIFT - SHIFT_FLL),
133 time_status |= STA_MODE;
135 freq_adj += time_freq;
136 freq_adj = min(freq_adj, MAXFREQ_SCALED);
137 time_freq = max(freq_adj, -MAXFREQ_SCALED);
139 time_offset = div_s64((s64)offset << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
143 * ntp_clear - Clears the NTP state variables
145 * Must be called while holding a write on the xtime_lock
149 time_adjust = 0; /* stop active adjtime() */
150 time_status |= STA_UNSYNC;
151 time_maxerror = NTP_PHASE_LIMIT;
152 time_esterror = NTP_PHASE_LIMIT;
154 ntp_update_frequency();
156 tick_length = tick_length_base;
161 * Leap second processing. If in leap-insert state at the end of the
162 * day, the system clock is set back one second; if in leap-delete
163 * state, the system clock is set ahead one second.
165 static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
167 enum hrtimer_restart res = HRTIMER_NORESTART;
169 write_seqlock(&xtime_lock);
171 switch (time_state) {
176 wall_to_monotonic.tv_sec++;
177 time_state = TIME_OOP;
179 "Clock: inserting leap second 23:59:60 UTC\n");
180 hrtimer_add_expires_ns(&leap_timer, NSEC_PER_SEC);
181 res = HRTIMER_RESTART;
186 wall_to_monotonic.tv_sec--;
187 time_state = TIME_WAIT;
189 "Clock: deleting leap second 23:59:59 UTC\n");
193 time_state = TIME_WAIT;
196 if (!(time_status & (STA_INS | STA_DEL)))
197 time_state = TIME_OK;
200 update_vsyscall(&xtime, clock);
202 write_sequnlock(&xtime_lock);
208 * this routine handles the overflow of the microsecond field
210 * The tricky bits of code to handle the accurate clock support
211 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
212 * They were originally developed for SUN and DEC kernels.
213 * All the kudos should go to Dave for this stuff.
215 void second_overflow(void)
219 /* Bump the maxerror field */
220 time_maxerror += MAXFREQ / NSEC_PER_USEC;
221 if (time_maxerror > NTP_PHASE_LIMIT) {
222 time_maxerror = NTP_PHASE_LIMIT;
223 time_status |= STA_UNSYNC;
227 * Compute the phase adjustment for the next second. The offset is
228 * reduced by a fixed factor times the time constant.
230 tick_length = tick_length_base;
231 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
232 time_offset -= time_adj;
233 tick_length += time_adj;
235 if (unlikely(time_adjust)) {
236 if (time_adjust > MAX_TICKADJ) {
237 time_adjust -= MAX_TICKADJ;
238 tick_length += MAX_TICKADJ_SCALED;
239 } else if (time_adjust < -MAX_TICKADJ) {
240 time_adjust += MAX_TICKADJ;
241 tick_length -= MAX_TICKADJ_SCALED;
243 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
244 NTP_INTERVAL_FREQ) << NTP_SCALE_SHIFT;
250 #ifdef CONFIG_GENERIC_CMOS_UPDATE
252 /* Disable the cmos update - used by virtualization and embedded */
253 int no_sync_cmos_clock __read_mostly;
255 static void sync_cmos_clock(struct work_struct *work);
257 static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
259 static void sync_cmos_clock(struct work_struct *work)
261 struct timespec now, next;
265 * If we have an externally synchronized Linux clock, then update
266 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
267 * called as close as possible to 500 ms before the new second starts.
268 * This code is run on a timer. If the clock is set, that timer
269 * may not expire at the correct time. Thus, we adjust...
273 * Not synced, exit, do not restart a timer (if one is
274 * running, let it run out).
279 getnstimeofday(&now);
280 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
281 fail = update_persistent_clock(now);
283 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2);
284 if (next.tv_nsec <= 0)
285 next.tv_nsec += NSEC_PER_SEC;
292 if (next.tv_nsec >= NSEC_PER_SEC) {
294 next.tv_nsec -= NSEC_PER_SEC;
296 schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next));
299 static void notify_cmos_timer(void)
301 if (!no_sync_cmos_clock)
302 schedule_delayed_work(&sync_cmos_work, 0);
306 static inline void notify_cmos_timer(void) { }
310 * adjtimex mainly allows reading (and writing, if superuser) of
311 * kernel time-keeping variables. used by xntpd.
313 int do_adjtimex(struct timex *txc)
318 /* Validate the data before disabling interrupts */
319 if (txc->modes & ADJ_ADJTIME) {
320 /* singleshot must not be used with any other mode bits */
321 if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
323 if (!(txc->modes & ADJ_OFFSET_READONLY) &&
324 !capable(CAP_SYS_TIME))
327 /* In order to modify anything, you gotta be super-user! */
328 if (txc->modes && !capable(CAP_SYS_TIME))
332 * if the quartz is off by more than 10% then
333 * something is VERY wrong!
335 if (txc->modes & ADJ_TICK &&
336 (txc->tick < 900000/USER_HZ ||
337 txc->tick > 1100000/USER_HZ))
340 if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
341 hrtimer_cancel(&leap_timer);
346 write_seqlock_irq(&xtime_lock);
348 /* If there are input parameters, then process them */
349 if (txc->modes & ADJ_ADJTIME) {
350 long save_adjust = time_adjust;
352 if (!(txc->modes & ADJ_OFFSET_READONLY)) {
353 /* adjtime() is independent from ntp_adjtime() */
354 time_adjust = txc->offset;
355 ntp_update_frequency();
357 txc->offset = save_adjust;
363 if (txc->modes & ADJ_STATUS) {
364 if ((time_status & STA_PLL) &&
365 !(txc->status & STA_PLL)) {
366 time_state = TIME_OK;
367 time_status = STA_UNSYNC;
369 /* only set allowed bits */
370 time_status &= STA_RONLY;
371 time_status |= txc->status & ~STA_RONLY;
373 switch (time_state) {
377 if (time_status & STA_INS) {
378 time_state = TIME_INS;
379 sec += 86400 - sec % 86400;
380 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
381 } else if (time_status & STA_DEL) {
382 time_state = TIME_DEL;
383 sec += 86400 - (sec + 1) % 86400;
384 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
389 time_state = TIME_OK;
393 if (!(time_status & (STA_INS | STA_DEL)))
394 time_state = TIME_OK;
397 hrtimer_restart(&leap_timer);
402 if (txc->modes & ADJ_NANO)
403 time_status |= STA_NANO;
404 if (txc->modes & ADJ_MICRO)
405 time_status &= ~STA_NANO;
407 if (txc->modes & ADJ_FREQUENCY) {
408 time_freq = (s64)txc->freq * PPM_SCALE;
409 time_freq = min(time_freq, MAXFREQ_SCALED);
410 time_freq = max(time_freq, -MAXFREQ_SCALED);
413 if (txc->modes & ADJ_MAXERROR)
414 time_maxerror = txc->maxerror;
415 if (txc->modes & ADJ_ESTERROR)
416 time_esterror = txc->esterror;
418 if (txc->modes & ADJ_TIMECONST) {
419 time_constant = txc->constant;
420 if (!(time_status & STA_NANO))
422 time_constant = min(time_constant, (long)MAXTC);
423 time_constant = max(time_constant, 0l);
426 if (txc->modes & ADJ_TAI && txc->constant > 0)
427 time_tai = txc->constant;
429 if (txc->modes & ADJ_OFFSET)
430 ntp_update_offset(txc->offset);
431 if (txc->modes & ADJ_TICK)
432 tick_usec = txc->tick;
434 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
435 ntp_update_frequency();
438 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
440 if (!(time_status & STA_NANO))
441 txc->offset /= NSEC_PER_USEC;
444 result = time_state; /* mostly `TIME_OK' */
445 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
448 txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
449 (s64)PPM_SCALE_INV, NTP_SCALE_SHIFT);
450 txc->maxerror = time_maxerror;
451 txc->esterror = time_esterror;
452 txc->status = time_status;
453 txc->constant = time_constant;
455 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
456 txc->tick = tick_usec;
459 /* PPS is not implemented, so these are zero */
468 write_sequnlock_irq(&xtime_lock);
470 txc->time.tv_sec = ts.tv_sec;
471 txc->time.tv_usec = ts.tv_nsec;
472 if (!(time_status & STA_NANO))
473 txc->time.tv_usec /= NSEC_PER_USEC;
480 static int __init ntp_tick_adj_setup(char *str)
482 ntp_tick_adj = simple_strtol(str, NULL, 0);
486 __setup("ntp_tick_adj=", ntp_tick_adj_setup);
488 void __init ntp_init(void)
491 hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
492 leap_timer.function = ntp_leap_second;