[PATCH] ntp: add time_adjust to tick length
[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 */
37long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
38long time_precision = 1; /* clock precision (us) */
39long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
40long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
dc6a43e4 41long time_freq; /* frequency offset (scaled ppm)*/
4c7ee8de 42long time_reftime; /* time at last adjustment (s) */
43long time_adjust;
4c7ee8de 44
b0ee7556
RZ
45/**
46 * ntp_clear - Clears the NTP state variables
47 *
48 * Must be called while holding a write on the xtime_lock
49 */
50void ntp_clear(void)
51{
52 time_adjust = 0; /* stop active adjtime() */
53 time_status |= STA_UNSYNC;
54 time_maxerror = NTP_PHASE_LIMIT;
55 time_esterror = NTP_PHASE_LIMIT;
56
57 ntp_update_frequency();
58
59 tick_length = tick_length_base;
3d3675cc 60 time_offset = 0;
b0ee7556
RZ
61}
62
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)
65
66void ntp_update_frequency(void)
67{
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;
dc6a43e4 70 tick_length_base += ((s64)time_freq * NSEC_PER_USEC) << (TICK_LENGTH_SHIFT - SHIFT_USEC);
b0ee7556
RZ
71
72 do_div(tick_length_base, HZ);
73
74 tick_nsec = tick_length_base >> TICK_LENGTH_SHIFT;
75}
76
4c7ee8de 77/*
78 * this routine handles the overflow of the microsecond field
79 *
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.
84 */
85void second_overflow(void)
86{
3d3675cc 87 long time_adj;
4c7ee8de 88
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;
94 }
95
96 /*
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.
102 */
103 switch (time_state) {
104 case TIME_OK:
105 if (time_status & STA_INS)
106 time_state = TIME_INS;
107 else if (time_status & STA_DEL)
108 time_state = TIME_DEL;
109 break;
110 case TIME_INS:
111 if (xtime.tv_sec % 86400 == 0) {
112 xtime.tv_sec--;
113 wall_to_monotonic.tv_sec++;
114 /*
115 * The timer interpolator will make time change
116 * gradually instead of an immediate jump by one second
117 */
118 time_interpolator_update(-NSEC_PER_SEC);
119 time_state = TIME_OOP;
120 clock_was_set();
121 printk(KERN_NOTICE "Clock: inserting leap second "
122 "23:59:60 UTC\n");
123 }
124 break;
125 case TIME_DEL:
126 if ((xtime.tv_sec + 1) % 86400 == 0) {
127 xtime.tv_sec++;
128 wall_to_monotonic.tv_sec--;
129 /*
130 * Use of time interpolator for a gradual change of
131 * time
132 */
133 time_interpolator_update(NSEC_PER_SEC);
134 time_state = TIME_WAIT;
135 clock_was_set();
136 printk(KERN_NOTICE "Clock: deleting leap second "
137 "23:59:59 UTC\n");
138 }
139 break;
140 case TIME_OOP:
141 time_state = TIME_WAIT;
142 break;
143 case TIME_WAIT:
144 if (!(time_status & (STA_INS | STA_DEL)))
145 time_state = TIME_OK;
146 }
147
148 /*
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.
154 */
b0ee7556 155 tick_length = tick_length_base;
3d3675cc
RZ
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);
4c7ee8de 163
8f807f8d
RZ
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;
171 } else {
172 time_adjust = 0;
173 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
174 HZ) << TICK_LENGTH_SHIFT;
175 }
4c7ee8de 176 }
177}
178
179/*
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.
186 */
187u64 current_tick_length(void)
188{
8f807f8d 189 return tick_length;
4c7ee8de 190}
191
192
193void __attribute__ ((weak)) notify_arch_cmos_timer(void)
194{
195 return;
196}
197
198/* adjtimex mainly allows reading (and writing, if superuser) of
199 * kernel time-keeping variables. used by xntpd.
200 */
201int do_adjtimex(struct timex *txc)
202{
203 long ltemp, mtemp, save_adjust;
204 int result;
205
206 /* In order to modify anything, you gotta be super-user! */
207 if (txc->modes && !capable(CAP_SYS_TIME))
208 return -EPERM;
209
210 /* Now we validate the data before disabling interrupts */
211
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)
215 return -EINVAL;
216
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 )
220 return -EINVAL;
221
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)
226 return -EINVAL;
227
228 write_seqlock_irq(&xtime_lock);
229 result = time_state; /* mostly `TIME_OK' */
230
231 /* Save for later - semantics of adjtime is to return old value */
8f807f8d 232 save_adjust = time_adjust;
4c7ee8de 233
234#if 0 /* STA_CLOCKERR is never set yet */
235 time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
236#endif
237 /* If there are input parameters, then process them */
238 if (txc->modes)
239 {
240 if (txc->modes & ADJ_STATUS) /* only set allowed bits */
241 time_status = (txc->status & ~STA_RONLY) |
242 (time_status & STA_RONLY);
243
244 if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
245 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
246 result = -EINVAL;
247 goto leave;
248 }
249 time_freq = txc->freq;
250 }
251
252 if (txc->modes & ADJ_MAXERROR) {
253 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
254 result = -EINVAL;
255 goto leave;
256 }
257 time_maxerror = txc->maxerror;
258 }
259
260 if (txc->modes & ADJ_ESTERROR) {
261 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
262 result = -EINVAL;
263 goto leave;
264 }
265 time_esterror = txc->esterror;
266 }
267
268 if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
269 if (txc->constant < 0) { /* NTP v4 uses values > 6 */
270 result = -EINVAL;
271 goto leave;
272 }
273 time_constant = txc->constant;
274 }
275
276 if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
277 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
278 /* adjtime() is independent from ntp_adjtime() */
8f807f8d 279 time_adjust = txc->offset;
4c7ee8de 280 }
281 else if (time_status & STA_PLL) {
282 ltemp = txc->offset;
283
284 /*
285 * Scale the phase adjustment and
286 * clamp to the operating range.
287 */
3d3675cc
RZ
288 time_offset = min(ltemp, MAXPHASE);
289 time_offset = max(time_offset, -MAXPHASE);
4c7ee8de 290
291 /*
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.
295 */
296
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) {
3d3675cc 303 ltemp = ((time_offset << 12) / mtemp) << (SHIFT_USEC - 12);
4c7ee8de 304 time_freq += shift_right(ltemp, SHIFT_KH);
305 } else /* calibration interval too short (p. 12) */
306 result = TIME_ERROR;
307 } else { /* PLL mode */
308 if (mtemp < MAXSEC) {
309 ltemp *= mtemp;
310 time_freq += shift_right(ltemp,(time_constant +
311 time_constant +
312 SHIFT_KF - SHIFT_USEC));
313 } else /* calibration interval too long (p. 12) */
314 result = TIME_ERROR;
315 }
316 time_freq = min(time_freq, time_tolerance);
317 time_freq = max(time_freq, -time_tolerance);
3d3675cc 318 time_offset = (time_offset * NSEC_PER_USEC / HZ) << SHIFT_UPDATE;
4c7ee8de 319 } /* STA_PLL */
320 } /* txc->modes & ADJ_OFFSET */
b0ee7556 321 if (txc->modes & ADJ_TICK)
4c7ee8de 322 tick_usec = txc->tick;
b0ee7556 323
dc6a43e4 324 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
b0ee7556 325 ntp_update_frequency();
4c7ee8de 326 } /* txc->modes */
327leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
328 result = TIME_ERROR;
329
330 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
331 txc->offset = save_adjust;
3d3675cc
RZ
332 else
333 txc->offset = shift_right(time_offset, SHIFT_UPDATE) * HZ / 1000;
4c7ee8de 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;
342
343 /* PPS is not implemented, so these are zero */
344 txc->ppsfreq = 0;
345 txc->jitter = 0;
346 txc->shift = 0;
347 txc->stabil = 0;
348 txc->jitcnt = 0;
349 txc->calcnt = 0;
350 txc->errcnt = 0;
351 txc->stbcnt = 0;
352 write_sequnlock_irq(&xtime_lock);
353 do_gettimeofday(&txc->time);
354 notify_arch_cmos_timer();
355 return(result);
356}
This page took 0.05783 seconds and 5 git commands to generate.