]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - kernel/time/ntp.c
[PATCH] NTP: Move all the NTP related code to ntp.c
[mirror_ubuntu-zesty-kernel.git] / kernel / time / ntp.c
CommitLineData
4c7ee8de
JS
1/*
2 * linux/kernel/time/ntp.c
3 *
4 * NTP state machine interfaces and logic.
5 *
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
8 * changelogs.
9 */
10
11#include <linux/mm.h>
12#include <linux/time.h>
13#include <linux/timex.h>
14
15#include <asm/div64.h>
16#include <asm/timex.h>
17
18/* Don't completely fail for HZ > 500. */
19int tickadj = 500/HZ ? : 1; /* microsecs */
20
21/*
22 * phase-lock loop variables
23 */
24/* TIME_ERROR prevents overwriting the CMOS clock */
25int time_state = TIME_OK; /* clock synchronization status */
26int time_status = STA_UNSYNC; /* clock status bits */
27long time_offset; /* time adjustment (us) */
28long time_constant = 2; /* pll time constant */
29long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
30long time_precision = 1; /* clock precision (us) */
31long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
32long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
33long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
34 /* frequency offset (scaled ppm)*/
35static long time_adj; /* tick adjust (scaled 1 / HZ) */
36long time_reftime; /* time at last adjustment (s) */
37long time_adjust;
38long time_next_adjust;
39
40/*
41 * this routine handles the overflow of the microsecond field
42 *
43 * The tricky bits of code to handle the accurate clock support
44 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
45 * They were originally developed for SUN and DEC kernels.
46 * All the kudos should go to Dave for this stuff.
47 */
48void second_overflow(void)
49{
50 long ltemp;
51
52 /* Bump the maxerror field */
53 time_maxerror += time_tolerance >> SHIFT_USEC;
54 if (time_maxerror > NTP_PHASE_LIMIT) {
55 time_maxerror = NTP_PHASE_LIMIT;
56 time_status |= STA_UNSYNC;
57 }
58
59 /*
60 * Leap second processing. If in leap-insert state at the end of the
61 * day, the system clock is set back one second; if in leap-delete
62 * state, the system clock is set ahead one second. The microtime()
63 * routine or external clock driver will insure that reported time is
64 * always monotonic. The ugly divides should be replaced.
65 */
66 switch (time_state) {
67 case TIME_OK:
68 if (time_status & STA_INS)
69 time_state = TIME_INS;
70 else if (time_status & STA_DEL)
71 time_state = TIME_DEL;
72 break;
73 case TIME_INS:
74 if (xtime.tv_sec % 86400 == 0) {
75 xtime.tv_sec--;
76 wall_to_monotonic.tv_sec++;
77 /*
78 * The timer interpolator will make time change
79 * gradually instead of an immediate jump by one second
80 */
81 time_interpolator_update(-NSEC_PER_SEC);
82 time_state = TIME_OOP;
83 clock_was_set();
84 printk(KERN_NOTICE "Clock: inserting leap second "
85 "23:59:60 UTC\n");
86 }
87 break;
88 case TIME_DEL:
89 if ((xtime.tv_sec + 1) % 86400 == 0) {
90 xtime.tv_sec++;
91 wall_to_monotonic.tv_sec--;
92 /*
93 * Use of time interpolator for a gradual change of
94 * time
95 */
96 time_interpolator_update(NSEC_PER_SEC);
97 time_state = TIME_WAIT;
98 clock_was_set();
99 printk(KERN_NOTICE "Clock: deleting leap second "
100 "23:59:59 UTC\n");
101 }
102 break;
103 case TIME_OOP:
104 time_state = TIME_WAIT;
105 break;
106 case TIME_WAIT:
107 if (!(time_status & (STA_INS | STA_DEL)))
108 time_state = TIME_OK;
109 }
110
111 /*
112 * Compute the phase adjustment for the next second. In PLL mode, the
113 * offset is reduced by a fixed factor times the time constant. In FLL
114 * mode the offset is used directly. In either mode, the maximum phase
115 * adjustment for each second is clamped so as to spread the adjustment
116 * over not more than the number of seconds between updates.
117 */
118 ltemp = time_offset;
119 if (!(time_status & STA_FLL))
120 ltemp = shift_right(ltemp, SHIFT_KG + time_constant);
121 ltemp = min(ltemp, (MAXPHASE / MINSEC) << SHIFT_UPDATE);
122 ltemp = max(ltemp, -(MAXPHASE / MINSEC) << SHIFT_UPDATE);
123 time_offset -= ltemp;
124 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
125
126 /*
127 * Compute the frequency estimate and additional phase adjustment due
128 * to frequency error for the next second.
129 */
130 ltemp = time_freq;
131 time_adj += shift_right(ltemp,(SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE));
132
133#if HZ == 100
134 /*
135 * Compensate for (HZ==100) != (1 << SHIFT_HZ). Add 25% and 3.125% to
136 * get 128.125; => only 0.125% error (p. 14)
137 */
138 time_adj += shift_right(time_adj, 2) + shift_right(time_adj, 5);
139#endif
140#if HZ == 250
141 /*
142 * Compensate for (HZ==250) != (1 << SHIFT_HZ). Add 1.5625% and
143 * 0.78125% to get 255.85938; => only 0.05% error (p. 14)
144 */
145 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
146#endif
147#if HZ == 1000
148 /*
149 * Compensate for (HZ==1000) != (1 << SHIFT_HZ). Add 1.5625% and
150 * 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
151 */
152 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
153#endif
154}
155
156/*
157 * Returns how many microseconds we need to add to xtime this tick
158 * in doing an adjustment requested with adjtime.
159 */
160static long adjtime_adjustment(void)
161{
162 long time_adjust_step;
163
164 time_adjust_step = time_adjust;
165 if (time_adjust_step) {
166 /*
167 * We are doing an adjtime thing. Prepare time_adjust_step to
168 * be within bounds. Note that a positive time_adjust means we
169 * want the clock to run faster.
170 *
171 * Limit the amount of the step to be in the range
172 * -tickadj .. +tickadj
173 */
174 time_adjust_step = min(time_adjust_step, (long)tickadj);
175 time_adjust_step = max(time_adjust_step, (long)-tickadj);
176 }
177 return time_adjust_step;
178}
179
180/* in the NTP reference this is called "hardclock()" */
181void update_ntp_one_tick(void)
182{
183 long time_adjust_step;
184
185 time_adjust_step = adjtime_adjustment();
186 if (time_adjust_step)
187 /* Reduce by this step the amount of time left */
188 time_adjust -= time_adjust_step;
189
190 /* Changes by adjtime() do not take effect till next tick. */
191 if (time_next_adjust != 0) {
192 time_adjust = time_next_adjust;
193 time_next_adjust = 0;
194 }
195}
196
197/*
198 * Return how long ticks are at the moment, that is, how much time
199 * update_wall_time_one_tick will add to xtime next time we call it
200 * (assuming no calls to do_adjtimex in the meantime).
201 * The return value is in fixed-point nanoseconds shifted by the
202 * specified number of bits to the right of the binary point.
203 * This function has no side-effects.
204 */
205u64 current_tick_length(void)
206{
207 long delta_nsec;
208 u64 ret;
209
210 /* calculate the finest interval NTP will allow.
211 * ie: nanosecond value shifted by (SHIFT_SCALE - 10)
212 */
213 delta_nsec = tick_nsec + adjtime_adjustment() * 1000;
214 ret = (u64)delta_nsec << TICK_LENGTH_SHIFT;
215 ret += (s64)time_adj << (TICK_LENGTH_SHIFT - (SHIFT_SCALE - 10));
216
217 return ret;
218}
219
220
221void __attribute__ ((weak)) notify_arch_cmos_timer(void)
222{
223 return;
224}
225
226/* adjtimex mainly allows reading (and writing, if superuser) of
227 * kernel time-keeping variables. used by xntpd.
228 */
229int do_adjtimex(struct timex *txc)
230{
231 long ltemp, mtemp, save_adjust;
232 int result;
233
234 /* In order to modify anything, you gotta be super-user! */
235 if (txc->modes && !capable(CAP_SYS_TIME))
236 return -EPERM;
237
238 /* Now we validate the data before disabling interrupts */
239
240 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
241 /* singleshot must not be used with any other mode bits */
242 if (txc->modes != ADJ_OFFSET_SINGLESHOT)
243 return -EINVAL;
244
245 if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
246 /* adjustment Offset limited to +- .512 seconds */
247 if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
248 return -EINVAL;
249
250 /* if the quartz is off by more than 10% something is VERY wrong ! */
251 if (txc->modes & ADJ_TICK)
252 if (txc->tick < 900000/USER_HZ ||
253 txc->tick > 1100000/USER_HZ)
254 return -EINVAL;
255
256 write_seqlock_irq(&xtime_lock);
257 result = time_state; /* mostly `TIME_OK' */
258
259 /* Save for later - semantics of adjtime is to return old value */
260 save_adjust = time_next_adjust ? time_next_adjust : time_adjust;
261
262#if 0 /* STA_CLOCKERR is never set yet */
263 time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
264#endif
265 /* If there are input parameters, then process them */
266 if (txc->modes)
267 {
268 if (txc->modes & ADJ_STATUS) /* only set allowed bits */
269 time_status = (txc->status & ~STA_RONLY) |
270 (time_status & STA_RONLY);
271
272 if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
273 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
274 result = -EINVAL;
275 goto leave;
276 }
277 time_freq = txc->freq;
278 }
279
280 if (txc->modes & ADJ_MAXERROR) {
281 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
282 result = -EINVAL;
283 goto leave;
284 }
285 time_maxerror = txc->maxerror;
286 }
287
288 if (txc->modes & ADJ_ESTERROR) {
289 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
290 result = -EINVAL;
291 goto leave;
292 }
293 time_esterror = txc->esterror;
294 }
295
296 if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
297 if (txc->constant < 0) { /* NTP v4 uses values > 6 */
298 result = -EINVAL;
299 goto leave;
300 }
301 time_constant = txc->constant;
302 }
303
304 if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
305 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
306 /* adjtime() is independent from ntp_adjtime() */
307 if ((time_next_adjust = txc->offset) == 0)
308 time_adjust = 0;
309 }
310 else if (time_status & STA_PLL) {
311 ltemp = txc->offset;
312
313 /*
314 * Scale the phase adjustment and
315 * clamp to the operating range.
316 */
317 if (ltemp > MAXPHASE)
318 time_offset = MAXPHASE << SHIFT_UPDATE;
319 else if (ltemp < -MAXPHASE)
320 time_offset = -(MAXPHASE << SHIFT_UPDATE);
321 else
322 time_offset = ltemp << SHIFT_UPDATE;
323
324 /*
325 * Select whether the frequency is to be controlled
326 * and in which mode (PLL or FLL). Clamp to the operating
327 * range. Ugly multiply/divide should be replaced someday.
328 */
329
330 if (time_status & STA_FREQHOLD || time_reftime == 0)
331 time_reftime = xtime.tv_sec;
332 mtemp = xtime.tv_sec - time_reftime;
333 time_reftime = xtime.tv_sec;
334 if (time_status & STA_FLL) {
335 if (mtemp >= MINSEC) {
336 ltemp = (time_offset / mtemp) << (SHIFT_USEC -
337 SHIFT_UPDATE);
338 time_freq += shift_right(ltemp, SHIFT_KH);
339 } else /* calibration interval too short (p. 12) */
340 result = TIME_ERROR;
341 } else { /* PLL mode */
342 if (mtemp < MAXSEC) {
343 ltemp *= mtemp;
344 time_freq += shift_right(ltemp,(time_constant +
345 time_constant +
346 SHIFT_KF - SHIFT_USEC));
347 } else /* calibration interval too long (p. 12) */
348 result = TIME_ERROR;
349 }
350 time_freq = min(time_freq, time_tolerance);
351 time_freq = max(time_freq, -time_tolerance);
352 } /* STA_PLL */
353 } /* txc->modes & ADJ_OFFSET */
354 if (txc->modes & ADJ_TICK) {
355 tick_usec = txc->tick;
356 tick_nsec = TICK_USEC_TO_NSEC(tick_usec);
357 }
358 } /* txc->modes */
359leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
360 result = TIME_ERROR;
361
362 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
363 txc->offset = save_adjust;
364 else {
365 txc->offset = shift_right(time_offset, SHIFT_UPDATE);
366 }
367 txc->freq = time_freq;
368 txc->maxerror = time_maxerror;
369 txc->esterror = time_esterror;
370 txc->status = time_status;
371 txc->constant = time_constant;
372 txc->precision = time_precision;
373 txc->tolerance = time_tolerance;
374 txc->tick = tick_usec;
375
376 /* PPS is not implemented, so these are zero */
377 txc->ppsfreq = 0;
378 txc->jitter = 0;
379 txc->shift = 0;
380 txc->stabil = 0;
381 txc->jitcnt = 0;
382 txc->calcnt = 0;
383 txc->errcnt = 0;
384 txc->stbcnt = 0;
385 write_sequnlock_irq(&xtime_lock);
386 do_gettimeofday(&txc->time);
387 notify_arch_cmos_timer();
388 return(result);
389}