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>
15 #include <asm/div64.h>
16 #include <asm/timex.h>
19 * Timekeeping variables
21 unsigned long tick_usec
= TICK_USEC
; /* USER_HZ period (usec) */
22 unsigned long tick_nsec
; /* ACTHZ period (nsec) */
23 static u64 tick_length
, tick_length_base
;
25 #define MAX_TICKADJ 500 /* microsecs */
26 #define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \
27 TICK_LENGTH_SHIFT) / HZ)
30 * phase-lock loop variables
32 /* TIME_ERROR prevents overwriting the CMOS clock */
33 int time_state
= TIME_OK
; /* clock synchronization status */
34 int time_status
= STA_UNSYNC
; /* clock status bits */
35 long time_offset
; /* time adjustment (ns) */
36 long time_constant
= 2; /* pll time constant */
37 long time_tolerance
= MAXFREQ
; /* frequency tolerance (ppm) */
38 long time_precision
= 1; /* clock precision (us) */
39 long time_maxerror
= NTP_PHASE_LIMIT
; /* maximum error (us) */
40 long time_esterror
= NTP_PHASE_LIMIT
; /* estimated error (us) */
41 long time_freq
; /* frequency offset (scaled ppm)*/
42 long time_reftime
; /* time at last adjustment (s) */
46 * ntp_clear - Clears the NTP state variables
48 * Must be called while holding a write on the xtime_lock
52 time_adjust
= 0; /* stop active adjtime() */
53 time_status
|= STA_UNSYNC
;
54 time_maxerror
= NTP_PHASE_LIMIT
;
55 time_esterror
= NTP_PHASE_LIMIT
;
57 ntp_update_frequency();
59 tick_length
= tick_length_base
;
63 #define CLOCK_TICK_OVERFLOW (LATCH * HZ - CLOCK_TICK_RATE)
64 #define CLOCK_TICK_ADJUST (((s64)CLOCK_TICK_OVERFLOW * NSEC_PER_SEC) / (s64)CLOCK_TICK_RATE)
66 void ntp_update_frequency(void)
68 tick_length_base
= (u64
)(tick_usec
* NSEC_PER_USEC
* USER_HZ
) << TICK_LENGTH_SHIFT
;
69 tick_length_base
+= (s64
)CLOCK_TICK_ADJUST
<< TICK_LENGTH_SHIFT
;
70 tick_length_base
+= ((s64
)time_freq
* NSEC_PER_USEC
) << (TICK_LENGTH_SHIFT
- SHIFT_USEC
);
72 do_div(tick_length_base
, HZ
);
74 tick_nsec
= tick_length_base
>> TICK_LENGTH_SHIFT
;
78 * this routine handles the overflow of the microsecond field
80 * The tricky bits of code to handle the accurate clock support
81 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
82 * They were originally developed for SUN and DEC kernels.
83 * All the kudos should go to Dave for this stuff.
85 void second_overflow(void)
89 /* Bump the maxerror field */
90 time_maxerror
+= time_tolerance
>> SHIFT_USEC
;
91 if (time_maxerror
> NTP_PHASE_LIMIT
) {
92 time_maxerror
= NTP_PHASE_LIMIT
;
93 time_status
|= STA_UNSYNC
;
97 * Leap second processing. If in leap-insert state at the end of the
98 * day, the system clock is set back one second; if in leap-delete
99 * state, the system clock is set ahead one second. The microtime()
100 * routine or external clock driver will insure that reported time is
101 * always monotonic. The ugly divides should be replaced.
103 switch (time_state
) {
105 if (time_status
& STA_INS
)
106 time_state
= TIME_INS
;
107 else if (time_status
& STA_DEL
)
108 time_state
= TIME_DEL
;
111 if (xtime
.tv_sec
% 86400 == 0) {
113 wall_to_monotonic
.tv_sec
++;
115 * The timer interpolator will make time change
116 * gradually instead of an immediate jump by one second
118 time_interpolator_update(-NSEC_PER_SEC
);
119 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
--;
130 * Use of time interpolator for a gradual change of
133 time_interpolator_update(NSEC_PER_SEC
);
134 time_state
= TIME_WAIT
;
136 printk(KERN_NOTICE
"Clock: deleting leap second "
141 time_state
= TIME_WAIT
;
144 if (!(time_status
& (STA_INS
| STA_DEL
)))
145 time_state
= TIME_OK
;
149 * Compute the phase adjustment for the next second. In PLL mode, the
150 * offset is reduced by a fixed factor times the time constant. In FLL
151 * mode the offset is used directly. In either mode, the maximum phase
152 * adjustment for each second is clamped so as to spread the adjustment
153 * over not more than the number of seconds between updates.
155 tick_length
= tick_length_base
;
156 time_adj
= time_offset
;
157 if (!(time_status
& STA_FLL
))
158 time_adj
= shift_right(time_adj
, SHIFT_KG
+ time_constant
);
159 time_adj
= min(time_adj
, -((MAXPHASE
/ HZ
) << SHIFT_UPDATE
) / MINSEC
);
160 time_adj
= max(time_adj
, ((MAXPHASE
/ HZ
) << SHIFT_UPDATE
) / MINSEC
);
161 time_offset
-= time_adj
;
162 tick_length
+= (s64
)time_adj
<< (TICK_LENGTH_SHIFT
- SHIFT_UPDATE
);
164 if (unlikely(time_adjust
)) {
165 if (time_adjust
> MAX_TICKADJ
) {
166 time_adjust
-= MAX_TICKADJ
;
167 tick_length
+= MAX_TICKADJ_SCALED
;
168 } else if (time_adjust
< -MAX_TICKADJ
) {
169 time_adjust
+= MAX_TICKADJ
;
170 tick_length
-= MAX_TICKADJ_SCALED
;
173 tick_length
+= (s64
)(time_adjust
* NSEC_PER_USEC
/
174 HZ
) << TICK_LENGTH_SHIFT
;
180 * Return how long ticks are at the moment, that is, how much time
181 * update_wall_time_one_tick will add to xtime next time we call it
182 * (assuming no calls to do_adjtimex in the meantime).
183 * The return value is in fixed-point nanoseconds shifted by the
184 * specified number of bits to the right of the binary point.
185 * This function has no side-effects.
187 u64
current_tick_length(void)
193 void __attribute__ ((weak
)) notify_arch_cmos_timer(void)
198 /* adjtimex mainly allows reading (and writing, if superuser) of
199 * kernel time-keeping variables. used by xntpd.
201 int do_adjtimex(struct timex
*txc
)
203 long ltemp
, mtemp
, save_adjust
;
206 /* In order to modify anything, you gotta be super-user! */
207 if (txc
->modes
&& !capable(CAP_SYS_TIME
))
210 /* Now we validate the data before disabling interrupts */
212 if ((txc
->modes
& ADJ_OFFSET_SINGLESHOT
) == ADJ_OFFSET_SINGLESHOT
)
213 /* singleshot must not be used with any other mode bits */
214 if (txc
->modes
!= ADJ_OFFSET_SINGLESHOT
)
217 if (txc
->modes
!= ADJ_OFFSET_SINGLESHOT
&& (txc
->modes
& ADJ_OFFSET
))
218 /* adjustment Offset limited to +- .512 seconds */
219 if (txc
->offset
<= - MAXPHASE
|| txc
->offset
>= MAXPHASE
)
222 /* if the quartz is off by more than 10% something is VERY wrong ! */
223 if (txc
->modes
& ADJ_TICK
)
224 if (txc
->tick
< 900000/USER_HZ
||
225 txc
->tick
> 1100000/USER_HZ
)
228 write_seqlock_irq(&xtime_lock
);
229 result
= time_state
; /* mostly `TIME_OK' */
231 /* Save for later - semantics of adjtime is to return old value */
232 save_adjust
= time_adjust
;
234 #if 0 /* STA_CLOCKERR is never set yet */
235 time_status
&= ~STA_CLOCKERR
; /* reset STA_CLOCKERR */
237 /* If there are input parameters, then process them */
240 if (txc
->modes
& ADJ_STATUS
) /* only set allowed bits */
241 time_status
= (txc
->status
& ~STA_RONLY
) |
242 (time_status
& STA_RONLY
);
244 if (txc
->modes
& ADJ_FREQUENCY
) { /* p. 22 */
245 if (txc
->freq
> MAXFREQ
|| txc
->freq
< -MAXFREQ
) {
249 time_freq
= txc
->freq
;
252 if (txc
->modes
& ADJ_MAXERROR
) {
253 if (txc
->maxerror
< 0 || txc
->maxerror
>= NTP_PHASE_LIMIT
) {
257 time_maxerror
= txc
->maxerror
;
260 if (txc
->modes
& ADJ_ESTERROR
) {
261 if (txc
->esterror
< 0 || txc
->esterror
>= NTP_PHASE_LIMIT
) {
265 time_esterror
= txc
->esterror
;
268 if (txc
->modes
& ADJ_TIMECONST
) { /* p. 24 */
269 if (txc
->constant
< 0) { /* NTP v4 uses values > 6 */
273 time_constant
= txc
->constant
;
276 if (txc
->modes
& ADJ_OFFSET
) { /* values checked earlier */
277 if (txc
->modes
== ADJ_OFFSET_SINGLESHOT
) {
278 /* adjtime() is independent from ntp_adjtime() */
279 time_adjust
= txc
->offset
;
281 else if (time_status
& STA_PLL
) {
285 * Scale the phase adjustment and
286 * clamp to the operating range.
288 time_offset
= min(ltemp
, MAXPHASE
);
289 time_offset
= max(time_offset
, -MAXPHASE
);
292 * Select whether the frequency is to be controlled
293 * and in which mode (PLL or FLL). Clamp to the operating
294 * range. Ugly multiply/divide should be replaced someday.
297 if (time_status
& STA_FREQHOLD
|| time_reftime
== 0)
298 time_reftime
= xtime
.tv_sec
;
299 mtemp
= xtime
.tv_sec
- time_reftime
;
300 time_reftime
= xtime
.tv_sec
;
301 if (time_status
& STA_FLL
) {
302 if (mtemp
>= MINSEC
) {
303 ltemp
= ((time_offset
<< 12) / mtemp
) << (SHIFT_USEC
- 12);
304 time_freq
+= shift_right(ltemp
, SHIFT_KH
);
305 } else /* calibration interval too short (p. 12) */
307 } else { /* PLL mode */
308 if (mtemp
< MAXSEC
) {
310 time_freq
+= shift_right(ltemp
,(time_constant
+
312 SHIFT_KF
- SHIFT_USEC
));
313 } else /* calibration interval too long (p. 12) */
316 time_freq
= min(time_freq
, time_tolerance
);
317 time_freq
= max(time_freq
, -time_tolerance
);
318 time_offset
= (time_offset
* NSEC_PER_USEC
/ HZ
) << SHIFT_UPDATE
;
320 } /* txc->modes & ADJ_OFFSET */
321 if (txc
->modes
& ADJ_TICK
)
322 tick_usec
= txc
->tick
;
324 if (txc
->modes
& (ADJ_TICK
|ADJ_FREQUENCY
|ADJ_OFFSET
))
325 ntp_update_frequency();
327 leave
: if ((time_status
& (STA_UNSYNC
|STA_CLOCKERR
)) != 0)
330 if ((txc
->modes
& ADJ_OFFSET_SINGLESHOT
) == ADJ_OFFSET_SINGLESHOT
)
331 txc
->offset
= save_adjust
;
333 txc
->offset
= shift_right(time_offset
, SHIFT_UPDATE
) * HZ
/ 1000;
334 txc
->freq
= time_freq
;
335 txc
->maxerror
= time_maxerror
;
336 txc
->esterror
= time_esterror
;
337 txc
->status
= time_status
;
338 txc
->constant
= time_constant
;
339 txc
->precision
= time_precision
;
340 txc
->tolerance
= time_tolerance
;
341 txc
->tick
= tick_usec
;
343 /* PPS is not implemented, so these are zero */
352 write_sequnlock_irq(&xtime_lock
);
353 do_gettimeofday(&txc
->time
);
354 notify_arch_cmos_timer();
This page took 0.048635 seconds and 6 git commands to generate.