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