[PATCH] ntp: convert time_freq to nsec value
[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;
04b617e7 69 tick_length_base += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC);
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;
04b617e7 203 s64 freq_adj;
4c7ee8de 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 }
04b617e7 249 time_freq = ((s64)txc->freq * NSEC_PER_USEC) >> (SHIFT_USEC - SHIFT_NSEC);
4c7ee8de 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) {
04b617e7 282 ltemp = txc->offset * NSEC_PER_USEC;
4c7ee8de 283
284 /*
285 * Scale the phase adjustment and
286 * clamp to the operating range.
287 */
04b617e7
RZ
288 time_offset = min(ltemp, MAXPHASE * NSEC_PER_USEC);
289 time_offset = max(time_offset, -MAXPHASE * NSEC_PER_USEC);
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;
04b617e7 301 freq_adj = 0;
4c7ee8de 302 if (time_status & STA_FLL) {
303 if (mtemp >= MINSEC) {
04b617e7
RZ
304 freq_adj = (s64)time_offset << (SHIFT_NSEC - SHIFT_KH);
305 if (time_offset < 0) {
306 freq_adj = -freq_adj;
307 do_div(freq_adj, mtemp);
308 freq_adj = -freq_adj;
309 } else
310 do_div(freq_adj, mtemp);
4c7ee8de 311 } else /* calibration interval too short (p. 12) */
312 result = TIME_ERROR;
313 } else { /* PLL mode */
314 if (mtemp < MAXSEC) {
04b617e7
RZ
315 freq_adj = (s64)ltemp * mtemp;
316 freq_adj = shift_right(freq_adj,(time_constant +
4c7ee8de 317 time_constant +
04b617e7 318 SHIFT_KF - SHIFT_NSEC));
4c7ee8de 319 } else /* calibration interval too long (p. 12) */
320 result = TIME_ERROR;
321 }
04b617e7
RZ
322 freq_adj += time_freq;
323 freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC);
324 time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC);
325 time_offset = (time_offset / HZ) << SHIFT_UPDATE;
4c7ee8de 326 } /* STA_PLL */
327 } /* txc->modes & ADJ_OFFSET */
b0ee7556 328 if (txc->modes & ADJ_TICK)
4c7ee8de 329 tick_usec = txc->tick;
b0ee7556 330
dc6a43e4 331 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
b0ee7556 332 ntp_update_frequency();
4c7ee8de 333 } /* txc->modes */
334leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
335 result = TIME_ERROR;
336
337 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
338 txc->offset = save_adjust;
3d3675cc
RZ
339 else
340 txc->offset = shift_right(time_offset, SHIFT_UPDATE) * HZ / 1000;
04b617e7 341 txc->freq = (time_freq / NSEC_PER_USEC) << (SHIFT_USEC - SHIFT_NSEC);
4c7ee8de 342 txc->maxerror = time_maxerror;
343 txc->esterror = time_esterror;
344 txc->status = time_status;
345 txc->constant = time_constant;
346 txc->precision = time_precision;
97eebe13 347 txc->tolerance = MAXFREQ;
4c7ee8de 348 txc->tick = tick_usec;
349
350 /* PPS is not implemented, so these are zero */
351 txc->ppsfreq = 0;
352 txc->jitter = 0;
353 txc->shift = 0;
354 txc->stabil = 0;
355 txc->jitcnt = 0;
356 txc->calcnt = 0;
357 txc->errcnt = 0;
358 txc->stbcnt = 0;
359 write_sequnlock_irq(&xtime_lock);
360 do_gettimeofday(&txc->time);
361 notify_arch_cmos_timer();
362 return(result);
363}
This page took 0.056795 seconds and 5 git commands to generate.