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 static long time_maxerror
= NTP_PHASE_LIMIT
;
63 /* estimated error (usecs): */
64 static long time_esterror
= NTP_PHASE_LIMIT
;
66 /* frequency offset (scaled nsecs/secs): */
69 /* time at last adjustment (secs): */
70 static long time_reftime
;
72 static long time_adjust
;
74 /* constant (boot-param configurable) NTP tick adjustment (upscaled) */
75 static s64 ntp_tick_adj
;
82 * Update (tick_length, tick_length_base, tick_nsec), based
83 * on (tick_usec, ntp_tick_adj, time_freq):
85 static void ntp_update_frequency(void)
90 second_length
= (u64
)(tick_usec
* NSEC_PER_USEC
* USER_HZ
)
93 second_length
+= ntp_tick_adj
;
94 second_length
+= time_freq
;
96 tick_nsec
= div_u64(second_length
, HZ
) >> NTP_SCALE_SHIFT
;
97 new_base
= div_u64(second_length
, NTP_INTERVAL_FREQ
);
100 * Don't wait for the next second_overflow, apply
101 * the change to the tick length immediately:
103 tick_length
+= new_base
- tick_length_base
;
104 tick_length_base
= new_base
;
107 static inline s64
ntp_update_offset_fll(s64 offset64
, long secs
)
109 time_status
&= ~STA_MODE
;
114 if (!(time_status
& STA_FLL
) && (secs
<= MAXSEC
))
117 time_status
|= STA_MODE
;
119 return div_s64(offset64
<< (NTP_SCALE_SHIFT
- SHIFT_FLL
), secs
);
122 static void ntp_update_offset(long offset
)
128 if (!(time_status
& STA_PLL
))
131 if (!(time_status
& STA_NANO
))
132 offset
*= NSEC_PER_USEC
;
135 * Scale the phase adjustment and
136 * clamp to the operating range.
138 offset
= min(offset
, MAXPHASE
);
139 offset
= max(offset
, -MAXPHASE
);
142 * Select how the frequency is to be controlled
143 * and in which mode (PLL or FLL).
145 secs
= get_seconds() - time_reftime
;
146 if (unlikely(time_status
& STA_FREQHOLD
))
149 time_reftime
= get_seconds();
152 freq_adj
= ntp_update_offset_fll(offset64
, secs
);
155 * Clamp update interval to reduce PLL gain with low
156 * sampling rate (e.g. intermittent network connection)
157 * to avoid instability.
159 if (unlikely(secs
> 1 << (SHIFT_PLL
+ 1 + time_constant
)))
160 secs
= 1 << (SHIFT_PLL
+ 1 + time_constant
);
162 freq_adj
+= (offset64
* secs
) <<
163 (NTP_SCALE_SHIFT
- 2 * (SHIFT_PLL
+ 2 + time_constant
));
165 freq_adj
= min(freq_adj
+ time_freq
, MAXFREQ_SCALED
);
167 time_freq
= max(freq_adj
, -MAXFREQ_SCALED
);
169 time_offset
= div_s64(offset64
<< NTP_SCALE_SHIFT
, NTP_INTERVAL_FREQ
);
173 * ntp_clear - Clears the NTP state variables
175 * Must be called while holding a write on the xtime_lock
179 time_adjust
= 0; /* stop active adjtime() */
180 time_status
|= STA_UNSYNC
;
181 time_maxerror
= NTP_PHASE_LIMIT
;
182 time_esterror
= NTP_PHASE_LIMIT
;
184 ntp_update_frequency();
186 tick_length
= tick_length_base
;
191 * Leap second processing. If in leap-insert state at the end of the
192 * day, the system clock is set back one second; if in leap-delete
193 * state, the system clock is set ahead one second.
195 static enum hrtimer_restart
ntp_leap_second(struct hrtimer
*timer
)
197 enum hrtimer_restart res
= HRTIMER_NORESTART
;
199 write_seqlock(&xtime_lock
);
201 switch (time_state
) {
205 timekeeping_leap_insert(-1);
206 time_state
= TIME_OOP
;
208 "Clock: inserting leap second 23:59:60 UTC\n");
209 hrtimer_add_expires_ns(&leap_timer
, NSEC_PER_SEC
);
210 res
= HRTIMER_RESTART
;
213 timekeeping_leap_insert(1);
215 time_state
= TIME_WAIT
;
217 "Clock: deleting leap second 23:59:59 UTC\n");
221 time_state
= TIME_WAIT
;
224 if (!(time_status
& (STA_INS
| STA_DEL
)))
225 time_state
= TIME_OK
;
229 write_sequnlock(&xtime_lock
);
235 * this routine handles the overflow of the microsecond field
237 * The tricky bits of code to handle the accurate clock support
238 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
239 * They were originally developed for SUN and DEC kernels.
240 * All the kudos should go to Dave for this stuff.
242 void second_overflow(void)
246 /* Bump the maxerror field */
247 time_maxerror
+= MAXFREQ
/ NSEC_PER_USEC
;
248 if (time_maxerror
> NTP_PHASE_LIMIT
) {
249 time_maxerror
= NTP_PHASE_LIMIT
;
250 time_status
|= STA_UNSYNC
;
254 * Compute the phase adjustment for the next second. The offset is
255 * reduced by a fixed factor times the time constant.
257 tick_length
= tick_length_base
;
259 delta
= shift_right(time_offset
, SHIFT_PLL
+ time_constant
);
260 time_offset
-= delta
;
261 tick_length
+= delta
;
266 if (time_adjust
> MAX_TICKADJ
) {
267 time_adjust
-= MAX_TICKADJ
;
268 tick_length
+= MAX_TICKADJ_SCALED
;
272 if (time_adjust
< -MAX_TICKADJ
) {
273 time_adjust
+= MAX_TICKADJ
;
274 tick_length
-= MAX_TICKADJ_SCALED
;
278 tick_length
+= (s64
)(time_adjust
* NSEC_PER_USEC
/ NTP_INTERVAL_FREQ
)
283 #ifdef CONFIG_GENERIC_CMOS_UPDATE
285 /* Disable the cmos update - used by virtualization and embedded */
286 int no_sync_cmos_clock __read_mostly
;
288 static void sync_cmos_clock(struct work_struct
*work
);
290 static DECLARE_DELAYED_WORK(sync_cmos_work
, sync_cmos_clock
);
292 static void sync_cmos_clock(struct work_struct
*work
)
294 struct timespec now
, next
;
298 * If we have an externally synchronized Linux clock, then update
299 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
300 * called as close as possible to 500 ms before the new second starts.
301 * This code is run on a timer. If the clock is set, that timer
302 * may not expire at the correct time. Thus, we adjust...
306 * Not synced, exit, do not restart a timer (if one is
307 * running, let it run out).
312 getnstimeofday(&now
);
313 if (abs(now
.tv_nsec
- (NSEC_PER_SEC
/ 2)) <= tick_nsec
/ 2)
314 fail
= update_persistent_clock(now
);
316 next
.tv_nsec
= (NSEC_PER_SEC
/ 2) - now
.tv_nsec
- (TICK_NSEC
/ 2);
317 if (next
.tv_nsec
<= 0)
318 next
.tv_nsec
+= NSEC_PER_SEC
;
325 if (next
.tv_nsec
>= NSEC_PER_SEC
) {
327 next
.tv_nsec
-= NSEC_PER_SEC
;
329 schedule_delayed_work(&sync_cmos_work
, timespec_to_jiffies(&next
));
332 static void notify_cmos_timer(void)
334 if (!no_sync_cmos_clock
)
335 schedule_delayed_work(&sync_cmos_work
, 0);
339 static inline void notify_cmos_timer(void) { }
343 * Start the leap seconds timer:
345 static inline void ntp_start_leap_timer(struct timespec
*ts
)
347 long now
= ts
->tv_sec
;
349 if (time_status
& STA_INS
) {
350 time_state
= TIME_INS
;
351 now
+= 86400 - now
% 86400;
352 hrtimer_start(&leap_timer
, ktime_set(now
, 0), HRTIMER_MODE_ABS
);
357 if (time_status
& STA_DEL
) {
358 time_state
= TIME_DEL
;
359 now
+= 86400 - (now
+ 1) % 86400;
360 hrtimer_start(&leap_timer
, ktime_set(now
, 0), HRTIMER_MODE_ABS
);
365 * Propagate a new txc->status value into the NTP state:
367 static inline void process_adj_status(struct timex
*txc
, struct timespec
*ts
)
369 if ((time_status
& STA_PLL
) && !(txc
->status
& STA_PLL
)) {
370 time_state
= TIME_OK
;
371 time_status
= STA_UNSYNC
;
375 * If we turn on PLL adjustments then reset the
376 * reference time to current time.
378 if (!(time_status
& STA_PLL
) && (txc
->status
& STA_PLL
))
379 time_reftime
= get_seconds();
381 /* only set allowed bits */
382 time_status
&= STA_RONLY
;
383 time_status
|= txc
->status
& ~STA_RONLY
;
385 switch (time_state
) {
387 ntp_start_leap_timer(ts
);
391 time_state
= TIME_OK
;
392 ntp_start_leap_timer(ts
);
394 if (!(time_status
& (STA_INS
| STA_DEL
)))
395 time_state
= TIME_OK
;
398 hrtimer_restart(&leap_timer
);
403 * Called with the xtime lock held, so we can access and modify
404 * all the global NTP state:
406 static inline void process_adjtimex_modes(struct timex
*txc
, struct timespec
*ts
)
408 if (txc
->modes
& ADJ_STATUS
)
409 process_adj_status(txc
, ts
);
411 if (txc
->modes
& ADJ_NANO
)
412 time_status
|= STA_NANO
;
414 if (txc
->modes
& ADJ_MICRO
)
415 time_status
&= ~STA_NANO
;
417 if (txc
->modes
& ADJ_FREQUENCY
) {
418 time_freq
= txc
->freq
* PPM_SCALE
;
419 time_freq
= min(time_freq
, MAXFREQ_SCALED
);
420 time_freq
= max(time_freq
, -MAXFREQ_SCALED
);
423 if (txc
->modes
& ADJ_MAXERROR
)
424 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
);
443 if (txc
->modes
& ADJ_TICK
)
444 tick_usec
= txc
->tick
;
446 if (txc
->modes
& (ADJ_TICK
|ADJ_FREQUENCY
|ADJ_OFFSET
))
447 ntp_update_frequency();
451 * adjtimex mainly allows reading (and writing, if superuser) of
452 * kernel time-keeping variables. used by xntpd.
454 int do_adjtimex(struct timex
*txc
)
459 /* Validate the data before disabling interrupts */
460 if (txc
->modes
& ADJ_ADJTIME
) {
461 /* singleshot must not be used with any other mode bits */
462 if (!(txc
->modes
& ADJ_OFFSET_SINGLESHOT
))
464 if (!(txc
->modes
& ADJ_OFFSET_READONLY
) &&
465 !capable(CAP_SYS_TIME
))
468 /* In order to modify anything, you gotta be super-user! */
469 if (txc
->modes
&& !capable(CAP_SYS_TIME
))
473 * if the quartz is off by more than 10% then
474 * something is VERY wrong!
476 if (txc
->modes
& ADJ_TICK
&&
477 (txc
->tick
< 900000/USER_HZ
||
478 txc
->tick
> 1100000/USER_HZ
))
481 if (txc
->modes
& ADJ_STATUS
&& time_state
!= TIME_OK
)
482 hrtimer_cancel(&leap_timer
);
487 write_seqlock_irq(&xtime_lock
);
489 if (txc
->modes
& ADJ_ADJTIME
) {
490 long save_adjust
= time_adjust
;
492 if (!(txc
->modes
& ADJ_OFFSET_READONLY
)) {
493 /* adjtime() is independent from ntp_adjtime() */
494 time_adjust
= txc
->offset
;
495 ntp_update_frequency();
497 txc
->offset
= save_adjust
;
500 /* If there are input parameters, then process them: */
502 process_adjtimex_modes(txc
, &ts
);
504 txc
->offset
= shift_right(time_offset
* NTP_INTERVAL_FREQ
,
506 if (!(time_status
& STA_NANO
))
507 txc
->offset
/= NSEC_PER_USEC
;
510 result
= time_state
; /* mostly `TIME_OK' */
511 if (time_status
& (STA_UNSYNC
|STA_CLOCKERR
))
514 txc
->freq
= shift_right((time_freq
>> PPM_SCALE_INV_SHIFT
) *
515 PPM_SCALE_INV
, NTP_SCALE_SHIFT
);
516 txc
->maxerror
= time_maxerror
;
517 txc
->esterror
= time_esterror
;
518 txc
->status
= time_status
;
519 txc
->constant
= time_constant
;
521 txc
->tolerance
= MAXFREQ_SCALED
/ PPM_SCALE
;
522 txc
->tick
= tick_usec
;
525 /* PPS is not implemented, so these are zero */
535 write_sequnlock_irq(&xtime_lock
);
537 txc
->time
.tv_sec
= ts
.tv_sec
;
538 txc
->time
.tv_usec
= ts
.tv_nsec
;
539 if (!(time_status
& STA_NANO
))
540 txc
->time
.tv_usec
/= NSEC_PER_USEC
;
547 static int __init
ntp_tick_adj_setup(char *str
)
549 ntp_tick_adj
= simple_strtol(str
, NULL
, 0);
550 ntp_tick_adj
<<= NTP_SCALE_SHIFT
;
555 __setup("ntp_tick_adj=", ntp_tick_adj_setup
);
557 void __init
ntp_init(void)
560 hrtimer_init(&leap_timer
, CLOCK_REALTIME
, HRTIMER_MODE_ABS
);
561 leap_timer
.function
= ntp_leap_second
;