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/timex.h>
14 #include <linux/jiffies.h>
15 #include <linux/hrtimer.h>
16 #include <linux/capability.h>
17 #include <linux/math64.h>
18 #include <linux/clocksource.h>
19 #include <linux/workqueue.h>
20 #include <asm/timex.h>
23 * Timekeeping variables
25 unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
26 unsigned long tick_nsec; /* ACTHZ period (nsec) */
28 static u64 tick_length_base;
30 static struct hrtimer leap_timer;
32 #define MAX_TICKADJ 500 /* microsecs */
33 #define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \
34 NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
37 * phase-lock loop variables
39 /* TIME_ERROR prevents overwriting the CMOS clock */
40 static int time_state = TIME_OK; /* clock synchronization status */
41 int time_status = STA_UNSYNC; /* clock status bits */
42 static long time_tai; /* TAI offset (s) */
43 static s64 time_offset; /* time adjustment (ns) */
44 static long time_constant = 2; /* pll time constant */
45 long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
46 long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
47 static s64 time_freq; /* frequency offset (scaled ns/s)*/
48 static long time_reftime; /* time at last adjustment (s) */
50 static long ntp_tick_adj;
52 static void ntp_update_frequency(void)
54 u64 old_tick_length_base = tick_length_base;
55 u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
57 second_length += (s64)ntp_tick_adj << NTP_SCALE_SHIFT;
58 second_length += time_freq;
60 tick_length_base = second_length;
62 tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
63 tick_length_base = div_u64(tick_length_base, NTP_INTERVAL_FREQ);
66 * Don't wait for the next second_overflow, apply
67 * the change to the tick length immediately
69 tick_length += tick_length_base - old_tick_length_base;
72 static void ntp_update_offset(long offset)
77 if (!(time_status & STA_PLL))
80 if (!(time_status & STA_NANO))
81 offset *= NSEC_PER_USEC;
84 * Scale the phase adjustment and
85 * clamp to the operating range.
87 offset = min(offset, MAXPHASE);
88 offset = max(offset, -MAXPHASE);
91 * Select how the frequency is to be controlled
92 * and in which mode (PLL or FLL).
94 if (time_status & STA_FREQHOLD || time_reftime == 0)
95 time_reftime = xtime.tv_sec;
96 mtemp = xtime.tv_sec - time_reftime;
97 time_reftime = xtime.tv_sec;
99 freq_adj = (s64)offset * mtemp;
100 freq_adj <<= NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant);
101 time_status &= ~STA_MODE;
102 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
103 freq_adj += div_s64((s64)offset << (NTP_SCALE_SHIFT - SHIFT_FLL),
105 time_status |= STA_MODE;
107 freq_adj += time_freq;
108 freq_adj = min(freq_adj, MAXFREQ_SCALED);
109 time_freq = max(freq_adj, -MAXFREQ_SCALED);
111 time_offset = div_s64((s64)offset << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
115 * ntp_clear - Clears the NTP state variables
117 * Must be called while holding a write on the xtime_lock
121 time_adjust = 0; /* stop active adjtime() */
122 time_status |= STA_UNSYNC;
123 time_maxerror = NTP_PHASE_LIMIT;
124 time_esterror = NTP_PHASE_LIMIT;
126 ntp_update_frequency();
128 tick_length = tick_length_base;
133 * Leap second processing. If in leap-insert state at the end of the
134 * day, the system clock is set back one second; if in leap-delete
135 * state, the system clock is set ahead one second.
137 static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
139 enum hrtimer_restart res = HRTIMER_NORESTART;
141 write_seqlock(&xtime_lock);
143 switch (time_state) {
148 wall_to_monotonic.tv_sec++;
149 time_state = TIME_OOP;
150 printk(KERN_NOTICE "Clock: "
151 "inserting leap second 23:59:60 UTC\n");
152 hrtimer_add_expires_ns(&leap_timer, NSEC_PER_SEC);
153 res = HRTIMER_RESTART;
158 wall_to_monotonic.tv_sec--;
159 time_state = TIME_WAIT;
160 printk(KERN_NOTICE "Clock: "
161 "deleting leap second 23:59:59 UTC\n");
165 time_state = TIME_WAIT;
168 if (!(time_status & (STA_INS | STA_DEL)))
169 time_state = TIME_OK;
172 update_vsyscall(&xtime, clock);
174 write_sequnlock(&xtime_lock);
180 * this routine handles the overflow of the microsecond field
182 * The tricky bits of code to handle the accurate clock support
183 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
184 * They were originally developed for SUN and DEC kernels.
185 * All the kudos should go to Dave for this stuff.
187 void second_overflow(void)
191 /* Bump the maxerror field */
192 time_maxerror += MAXFREQ / NSEC_PER_USEC;
193 if (time_maxerror > NTP_PHASE_LIMIT) {
194 time_maxerror = NTP_PHASE_LIMIT;
195 time_status |= STA_UNSYNC;
199 * Compute the phase adjustment for the next second. The offset is
200 * reduced by a fixed factor times the time constant.
202 tick_length = tick_length_base;
203 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
204 time_offset -= time_adj;
205 tick_length += time_adj;
207 if (unlikely(time_adjust)) {
208 if (time_adjust > MAX_TICKADJ) {
209 time_adjust -= MAX_TICKADJ;
210 tick_length += MAX_TICKADJ_SCALED;
211 } else if (time_adjust < -MAX_TICKADJ) {
212 time_adjust += MAX_TICKADJ;
213 tick_length -= MAX_TICKADJ_SCALED;
215 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
216 NTP_INTERVAL_FREQ) << NTP_SCALE_SHIFT;
222 #ifdef CONFIG_GENERIC_CMOS_UPDATE
224 /* Disable the cmos update - used by virtualization and embedded */
225 int no_sync_cmos_clock __read_mostly;
227 static void sync_cmos_clock(struct work_struct *work);
229 static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
231 static void sync_cmos_clock(struct work_struct *work)
233 struct timespec now, next;
237 * If we have an externally synchronized Linux clock, then update
238 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
239 * called as close as possible to 500 ms before the new second starts.
240 * This code is run on a timer. If the clock is set, that timer
241 * may not expire at the correct time. Thus, we adjust...
245 * Not synced, exit, do not restart a timer (if one is
246 * running, let it run out).
250 getnstimeofday(&now);
251 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
252 fail = update_persistent_clock(now);
254 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2);
255 if (next.tv_nsec <= 0)
256 next.tv_nsec += NSEC_PER_SEC;
263 if (next.tv_nsec >= NSEC_PER_SEC) {
265 next.tv_nsec -= NSEC_PER_SEC;
267 schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next));
270 static void notify_cmos_timer(void)
272 if (!no_sync_cmos_clock)
273 schedule_delayed_work(&sync_cmos_work, 0);
277 static inline void notify_cmos_timer(void) { }
280 /* adjtimex mainly allows reading (and writing, if superuser) of
281 * kernel time-keeping variables. used by xntpd.
283 int do_adjtimex(struct timex *txc)
288 /* Validate the data before disabling interrupts */
289 if (txc->modes & ADJ_ADJTIME) {
290 /* singleshot must not be used with any other mode bits */
291 if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
293 if (!(txc->modes & ADJ_OFFSET_READONLY) &&
294 !capable(CAP_SYS_TIME))
297 /* In order to modify anything, you gotta be super-user! */
298 if (txc->modes && !capable(CAP_SYS_TIME))
301 /* if the quartz is off by more than 10% something is VERY wrong! */
302 if (txc->modes & ADJ_TICK &&
303 (txc->tick < 900000/USER_HZ ||
304 txc->tick > 1100000/USER_HZ))
307 if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
308 hrtimer_cancel(&leap_timer);
313 write_seqlock_irq(&xtime_lock);
315 /* If there are input parameters, then process them */
316 if (txc->modes & ADJ_ADJTIME) {
317 long save_adjust = time_adjust;
319 if (!(txc->modes & ADJ_OFFSET_READONLY)) {
320 /* adjtime() is independent from ntp_adjtime() */
321 time_adjust = txc->offset;
322 ntp_update_frequency();
324 txc->offset = save_adjust;
330 if (txc->modes & ADJ_STATUS) {
331 if ((time_status & STA_PLL) &&
332 !(txc->status & STA_PLL)) {
333 time_state = TIME_OK;
334 time_status = STA_UNSYNC;
336 /* only set allowed bits */
337 time_status &= STA_RONLY;
338 time_status |= txc->status & ~STA_RONLY;
340 switch (time_state) {
344 if (time_status & STA_INS) {
345 time_state = TIME_INS;
346 sec += 86400 - sec % 86400;
347 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
348 } else if (time_status & STA_DEL) {
349 time_state = TIME_DEL;
350 sec += 86400 - (sec + 1) % 86400;
351 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
356 time_state = TIME_OK;
360 if (!(time_status & (STA_INS | STA_DEL)))
361 time_state = TIME_OK;
364 hrtimer_restart(&leap_timer);
369 if (txc->modes & ADJ_NANO)
370 time_status |= STA_NANO;
371 if (txc->modes & ADJ_MICRO)
372 time_status &= ~STA_NANO;
374 if (txc->modes & ADJ_FREQUENCY) {
375 time_freq = (s64)txc->freq * PPM_SCALE;
376 time_freq = min(time_freq, MAXFREQ_SCALED);
377 time_freq = max(time_freq, -MAXFREQ_SCALED);
380 if (txc->modes & ADJ_MAXERROR)
381 time_maxerror = txc->maxerror;
382 if (txc->modes & ADJ_ESTERROR)
383 time_esterror = txc->esterror;
385 if (txc->modes & ADJ_TIMECONST) {
386 time_constant = txc->constant;
387 if (!(time_status & STA_NANO))
389 time_constant = min(time_constant, (long)MAXTC);
390 time_constant = max(time_constant, 0l);
393 if (txc->modes & ADJ_TAI && txc->constant > 0)
394 time_tai = txc->constant;
396 if (txc->modes & ADJ_OFFSET)
397 ntp_update_offset(txc->offset);
398 if (txc->modes & ADJ_TICK)
399 tick_usec = txc->tick;
401 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
402 ntp_update_frequency();
405 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
407 if (!(time_status & STA_NANO))
408 txc->offset /= NSEC_PER_USEC;
411 result = time_state; /* mostly `TIME_OK' */
412 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
415 txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
416 (s64)PPM_SCALE_INV, NTP_SCALE_SHIFT);
417 txc->maxerror = time_maxerror;
418 txc->esterror = time_esterror;
419 txc->status = time_status;
420 txc->constant = time_constant;
422 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
423 txc->tick = tick_usec;
426 /* PPS is not implemented, so these are zero */
435 write_sequnlock_irq(&xtime_lock);
437 txc->time.tv_sec = ts.tv_sec;
438 txc->time.tv_usec = ts.tv_nsec;
439 if (!(time_status & STA_NANO))
440 txc->time.tv_usec /= NSEC_PER_USEC;
447 static int __init ntp_tick_adj_setup(char *str)
449 ntp_tick_adj = simple_strtol(str, NULL, 0);
453 __setup("ntp_tick_adj=", ntp_tick_adj_setup);
455 void __init ntp_init(void)
458 hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
459 leap_timer.function = ntp_leap_second;