aboutsummaryrefslogtreecommitdiffstats
path: root/kernel
diff options
context:
space:
mode:
authorjohn stultz <johnstul@us.ibm.com>2006-10-01 02:28:22 -0400
committerLinus Torvalds <torvalds@g5.osdl.org>2006-10-01 03:39:26 -0400
commit4c7ee8de956fc250fe31e2fa91f6da980fabe317 (patch)
treee2d7c207a7ca9f785c256513686f6d7f7234ef93 /kernel
parentc902e0a0102f1095eec4b3511c13c84ca2bc4577 (diff)
[PATCH] NTP: Move all the NTP related code to ntp.c
Move all the NTP related code to ntp.c [akpm@osdl.org: cleanups, build fix] Signed-off-by: John Stultz <johnstul@us.ibm.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Roman Zippel <zippel@linux-m68k.org> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'kernel')
-rw-r--r--kernel/time.c173
-rw-r--r--kernel/time/Makefile2
-rw-r--r--kernel/time/ntp.c389
-rw-r--r--kernel/timer.c211
4 files changed, 391 insertions, 384 deletions
diff --git a/kernel/time.c b/kernel/time.c
index 5bd489747643..0e017bff4c19 100644
--- a/kernel/time.c
+++ b/kernel/time.c
@@ -202,179 +202,6 @@ asmlinkage long sys_settimeofday(struct timeval __user *tv,
202 return do_sys_settimeofday(tv ? &new_ts : NULL, tz ? &new_tz : NULL); 202 return do_sys_settimeofday(tv ? &new_ts : NULL, tz ? &new_tz : NULL);
203} 203}
204 204
205/* we call this to notify the arch when the clock is being
206 * controlled. If no such arch routine, do nothing.
207 */
208void __attribute__ ((weak)) notify_arch_cmos_timer(void)
209{
210 return;
211}
212
213/* adjtimex mainly allows reading (and writing, if superuser) of
214 * kernel time-keeping variables. used by xntpd.
215 */
216int do_adjtimex(struct timex *txc)
217{
218 long ltemp, mtemp, save_adjust;
219 int result;
220
221 /* In order to modify anything, you gotta be super-user! */
222 if (txc->modes && !capable(CAP_SYS_TIME))
223 return -EPERM;
224
225 /* Now we validate the data before disabling interrupts */
226
227 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
228 /* singleshot must not be used with any other mode bits */
229 if (txc->modes != ADJ_OFFSET_SINGLESHOT)
230 return -EINVAL;
231
232 if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
233 /* adjustment Offset limited to +- .512 seconds */
234 if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
235 return -EINVAL;
236
237 /* if the quartz is off by more than 10% something is VERY wrong ! */
238 if (txc->modes & ADJ_TICK)
239 if (txc->tick < 900000/USER_HZ ||
240 txc->tick > 1100000/USER_HZ)
241 return -EINVAL;
242
243 write_seqlock_irq(&xtime_lock);
244 result = time_state; /* mostly `TIME_OK' */
245
246 /* Save for later - semantics of adjtime is to return old value */
247 save_adjust = time_next_adjust ? time_next_adjust : time_adjust;
248
249#if 0 /* STA_CLOCKERR is never set yet */
250 time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
251#endif
252 /* If there are input parameters, then process them */
253 if (txc->modes)
254 {
255 if (txc->modes & ADJ_STATUS) /* only set allowed bits */
256 time_status = (txc->status & ~STA_RONLY) |
257 (time_status & STA_RONLY);
258
259 if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
260 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
261 result = -EINVAL;
262 goto leave;
263 }
264 time_freq = txc->freq;
265 }
266
267 if (txc->modes & ADJ_MAXERROR) {
268 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
269 result = -EINVAL;
270 goto leave;
271 }
272 time_maxerror = txc->maxerror;
273 }
274
275 if (txc->modes & ADJ_ESTERROR) {
276 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
277 result = -EINVAL;
278 goto leave;
279 }
280 time_esterror = txc->esterror;
281 }
282
283 if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
284 if (txc->constant < 0) { /* NTP v4 uses values > 6 */
285 result = -EINVAL;
286 goto leave;
287 }
288 time_constant = txc->constant;
289 }
290
291 if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
292 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
293 /* adjtime() is independent from ntp_adjtime() */
294 if ((time_next_adjust = txc->offset) == 0)
295 time_adjust = 0;
296 }
297 else if (time_status & STA_PLL) {
298 ltemp = txc->offset;
299
300 /*
301 * Scale the phase adjustment and
302 * clamp to the operating range.
303 */
304 if (ltemp > MAXPHASE)
305 time_offset = MAXPHASE << SHIFT_UPDATE;
306 else if (ltemp < -MAXPHASE)
307 time_offset = -(MAXPHASE << SHIFT_UPDATE);
308 else
309 time_offset = ltemp << SHIFT_UPDATE;
310
311 /*
312 * Select whether the frequency is to be controlled
313 * and in which mode (PLL or FLL). Clamp to the operating
314 * range. Ugly multiply/divide should be replaced someday.
315 */
316
317 if (time_status & STA_FREQHOLD || time_reftime == 0)
318 time_reftime = xtime.tv_sec;
319 mtemp = xtime.tv_sec - time_reftime;
320 time_reftime = xtime.tv_sec;
321 if (time_status & STA_FLL) {
322 if (mtemp >= MINSEC) {
323 ltemp = (time_offset / mtemp) << (SHIFT_USEC -
324 SHIFT_UPDATE);
325 time_freq += shift_right(ltemp, SHIFT_KH);
326 } else /* calibration interval too short (p. 12) */
327 result = TIME_ERROR;
328 } else { /* PLL mode */
329 if (mtemp < MAXSEC) {
330 ltemp *= mtemp;
331 time_freq += shift_right(ltemp,(time_constant +
332 time_constant +
333 SHIFT_KF - SHIFT_USEC));
334 } else /* calibration interval too long (p. 12) */
335 result = TIME_ERROR;
336 }
337 time_freq = min(time_freq, time_tolerance);
338 time_freq = max(time_freq, -time_tolerance);
339 } /* STA_PLL */
340 } /* txc->modes & ADJ_OFFSET */
341 if (txc->modes & ADJ_TICK) {
342 tick_usec = txc->tick;
343 tick_nsec = TICK_USEC_TO_NSEC(tick_usec);
344 }
345 } /* txc->modes */
346leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
347 result = TIME_ERROR;
348
349 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
350 txc->offset = save_adjust;
351 else {
352 txc->offset = shift_right(time_offset, SHIFT_UPDATE);
353 }
354 txc->freq = time_freq;
355 txc->maxerror = time_maxerror;
356 txc->esterror = time_esterror;
357 txc->status = time_status;
358 txc->constant = time_constant;
359 txc->precision = time_precision;
360 txc->tolerance = time_tolerance;
361 txc->tick = tick_usec;
362
363 /* PPS is not implemented, so these are zero */
364 txc->ppsfreq = 0;
365 txc->jitter = 0;
366 txc->shift = 0;
367 txc->stabil = 0;
368 txc->jitcnt = 0;
369 txc->calcnt = 0;
370 txc->errcnt = 0;
371 txc->stbcnt = 0;
372 write_sequnlock_irq(&xtime_lock);
373 do_gettimeofday(&txc->time);
374 notify_arch_cmos_timer();
375 return(result);
376}
377
378asmlinkage long sys_adjtimex(struct timex __user *txc_p) 205asmlinkage long sys_adjtimex(struct timex __user *txc_p)
379{ 206{
380 struct timex txc; /* Local copy of parameter */ 207 struct timex txc; /* Local copy of parameter */
diff --git a/kernel/time/Makefile b/kernel/time/Makefile
index e1dfd8e86cce..61a3907d16fb 100644
--- a/kernel/time/Makefile
+++ b/kernel/time/Makefile
@@ -1 +1 @@
obj-y += clocksource.o jiffies.o obj-y += ntp.o clocksource.o jiffies.o
diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
new file mode 100644
index 000000000000..8ccce15b4b23
--- /dev/null
+++ b/kernel/time/ntp.c
@@ -0,0 +1,389 @@
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}
diff --git a/kernel/timer.c b/kernel/timer.c
index 4f55622b0d38..5fccc7cbf3b4 100644
--- a/kernel/timer.c
+++ b/kernel/timer.c
@@ -41,12 +41,6 @@
41#include <asm/timex.h> 41#include <asm/timex.h>
42#include <asm/io.h> 42#include <asm/io.h>
43 43
44#ifdef CONFIG_TIME_INTERPOLATION
45static void time_interpolator_update(long delta_nsec);
46#else
47#define time_interpolator_update(x)
48#endif
49
50u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES; 44u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
51 45
52EXPORT_SYMBOL(jiffies_64); 46EXPORT_SYMBOL(jiffies_64);
@@ -587,209 +581,6 @@ struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
587 581
588EXPORT_SYMBOL(xtime); 582EXPORT_SYMBOL(xtime);
589 583
590/* Don't completely fail for HZ > 500. */
591int tickadj = 500/HZ ? : 1; /* microsecs */
592
593
594/*
595 * phase-lock loop variables
596 */
597/* TIME_ERROR prevents overwriting the CMOS clock */
598int time_state = TIME_OK; /* clock synchronization status */
599int time_status = STA_UNSYNC; /* clock status bits */
600long time_offset; /* time adjustment (us) */
601long time_constant = 2; /* pll time constant */
602long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
603long time_precision = 1; /* clock precision (us) */
604long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
605long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
606long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
607 /* frequency offset (scaled ppm)*/
608static long time_adj; /* tick adjust (scaled 1 / HZ) */
609long time_reftime; /* time at last adjustment (s) */
610long time_adjust;
611long time_next_adjust;
612
613/*
614 * this routine handles the overflow of the microsecond field
615 *
616 * The tricky bits of code to handle the accurate clock support
617 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
618 * They were originally developed for SUN and DEC kernels.
619 * All the kudos should go to Dave for this stuff.
620 *
621 */
622static void second_overflow(void)
623{
624 long ltemp;
625
626 /* Bump the maxerror field */
627 time_maxerror += time_tolerance >> SHIFT_USEC;
628 if (time_maxerror > NTP_PHASE_LIMIT) {
629 time_maxerror = NTP_PHASE_LIMIT;
630 time_status |= STA_UNSYNC;
631 }
632
633 /*
634 * Leap second processing. If in leap-insert state at the end of the
635 * day, the system clock is set back one second; if in leap-delete
636 * state, the system clock is set ahead one second. The microtime()
637 * routine or external clock driver will insure that reported time is
638 * always monotonic. The ugly divides should be replaced.
639 */
640 switch (time_state) {
641 case TIME_OK:
642 if (time_status & STA_INS)
643 time_state = TIME_INS;
644 else if (time_status & STA_DEL)
645 time_state = TIME_DEL;
646 break;
647 case TIME_INS:
648 if (xtime.tv_sec % 86400 == 0) {
649 xtime.tv_sec--;
650 wall_to_monotonic.tv_sec++;
651 /*
652 * The timer interpolator will make time change
653 * gradually instead of an immediate jump by one second
654 */
655 time_interpolator_update(-NSEC_PER_SEC);
656 time_state = TIME_OOP;
657 clock_was_set();
658 printk(KERN_NOTICE "Clock: inserting leap second "
659 "23:59:60 UTC\n");
660 }
661 break;
662 case TIME_DEL:
663 if ((xtime.tv_sec + 1) % 86400 == 0) {
664 xtime.tv_sec++;
665 wall_to_monotonic.tv_sec--;
666 /*
667 * Use of time interpolator for a gradual change of
668 * time
669 */
670 time_interpolator_update(NSEC_PER_SEC);
671 time_state = TIME_WAIT;
672 clock_was_set();
673 printk(KERN_NOTICE "Clock: deleting leap second "
674 "23:59:59 UTC\n");
675 }
676 break;
677 case TIME_OOP:
678 time_state = TIME_WAIT;
679 break;
680 case TIME_WAIT:
681 if (!(time_status & (STA_INS | STA_DEL)))
682 time_state = TIME_OK;
683 }
684
685 /*
686 * Compute the phase adjustment for the next second. In PLL mode, the
687 * offset is reduced by a fixed factor times the time constant. In FLL
688 * mode the offset is used directly. In either mode, the maximum phase
689 * adjustment for each second is clamped so as to spread the adjustment
690 * over not more than the number of seconds between updates.
691 */
692 ltemp = time_offset;
693 if (!(time_status & STA_FLL))
694 ltemp = shift_right(ltemp, SHIFT_KG + time_constant);
695 ltemp = min(ltemp, (MAXPHASE / MINSEC) << SHIFT_UPDATE);
696 ltemp = max(ltemp, -(MAXPHASE / MINSEC) << SHIFT_UPDATE);
697 time_offset -= ltemp;
698 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
699
700 /*
701 * Compute the frequency estimate and additional phase adjustment due
702 * to frequency error for the next second.
703 */
704 ltemp = time_freq;
705 time_adj += shift_right(ltemp,(SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE));
706
707#if HZ == 100
708 /*
709 * Compensate for (HZ==100) != (1 << SHIFT_HZ). Add 25% and 3.125% to
710 * get 128.125; => only 0.125% error (p. 14)
711 */
712 time_adj += shift_right(time_adj, 2) + shift_right(time_adj, 5);
713#endif
714#if HZ == 250
715 /*
716 * Compensate for (HZ==250) != (1 << SHIFT_HZ). Add 1.5625% and
717 * 0.78125% to get 255.85938; => only 0.05% error (p. 14)
718 */
719 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
720#endif
721#if HZ == 1000
722 /*
723 * Compensate for (HZ==1000) != (1 << SHIFT_HZ). Add 1.5625% and
724 * 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
725 */
726 time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
727#endif
728}
729
730/*
731 * Returns how many microseconds we need to add to xtime this tick
732 * in doing an adjustment requested with adjtime.
733 */
734static long adjtime_adjustment(void)
735{
736 long time_adjust_step;
737
738 time_adjust_step = time_adjust;
739 if (time_adjust_step) {
740 /*
741 * We are doing an adjtime thing. Prepare time_adjust_step to
742 * be within bounds. Note that a positive time_adjust means we
743 * want the clock to run faster.
744 *
745 * Limit the amount of the step to be in the range
746 * -tickadj .. +tickadj
747 */
748 time_adjust_step = min(time_adjust_step, (long)tickadj);
749 time_adjust_step = max(time_adjust_step, (long)-tickadj);
750 }
751 return time_adjust_step;
752}
753
754/* in the NTP reference this is called "hardclock()" */
755static void update_ntp_one_tick(void)
756{
757 long time_adjust_step;
758
759 time_adjust_step = adjtime_adjustment();
760 if (time_adjust_step)
761 /* Reduce by this step the amount of time left */
762 time_adjust -= time_adjust_step;
763
764 /* Changes by adjtime() do not take effect till next tick. */
765 if (time_next_adjust != 0) {
766 time_adjust = time_next_adjust;
767 time_next_adjust = 0;
768 }
769}
770
771/*
772 * Return how long ticks are at the moment, that is, how much time
773 * update_wall_time_one_tick will add to xtime next time we call it
774 * (assuming no calls to do_adjtimex in the meantime).
775 * The return value is in fixed-point nanoseconds shifted by the
776 * specified number of bits to the right of the binary point.
777 * This function has no side-effects.
778 */
779u64 current_tick_length(void)
780{
781 long delta_nsec;
782 u64 ret;
783
784 /* calculate the finest interval NTP will allow.
785 * ie: nanosecond value shifted by (SHIFT_SCALE - 10)
786 */
787 delta_nsec = tick_nsec + adjtime_adjustment() * 1000;
788 ret = (u64)delta_nsec << TICK_LENGTH_SHIFT;
789 ret += (s64)time_adj << (TICK_LENGTH_SHIFT - (SHIFT_SCALE - 10));
790
791 return ret;
792}
793 584
794/* XXX - all of this timekeeping code should be later moved to time.c */ 585/* XXX - all of this timekeeping code should be later moved to time.c */
795#include <linux/clocksource.h> 586#include <linux/clocksource.h>
@@ -1775,7 +1566,7 @@ unsigned long time_interpolator_get_offset(void)
1775#define INTERPOLATOR_ADJUST 65536 1566#define INTERPOLATOR_ADJUST 65536
1776#define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST 1567#define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
1777 1568
1778static void time_interpolator_update(long delta_nsec) 1569void time_interpolator_update(long delta_nsec)
1779{ 1570{
1780 u64 counter; 1571 u64 counter;
1781 unsigned long offset; 1572 unsigned long offset;