code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.34.14-moxart.git] / kernel / time / ntp.c
blob0c2fe9083f1c3f654d9da49d4d07cd9f4be43939
1 /*
2 * NTP state machine interfaces and logic.
4 * This code was mainly moved from kernel/timer.c and kernel/time.c
5 * Please see those files for relevant copyright info and historical
6 * changelogs.
7 */
8 #include <linux/capability.h>
9 #include <linux/clocksource.h>
10 #include <linux/workqueue.h>
11 #include <linux/hrtimer.h>
12 #include <linux/jiffies.h>
13 #include <linux/math64.h>
14 #include <linux/timex.h>
15 #include <linux/time.h>
16 #include <linux/mm.h>
19 * NTP timekeeping variables:
22 /* USER_HZ period (usecs): */
23 unsigned long tick_usec = TICK_USEC;
25 /* ACTHZ period (nsecs): */
26 unsigned long tick_nsec;
28 u64 tick_length;
29 static u64 tick_length_base;
31 #define MAX_TICKADJ 500LL /* usecs */
32 #define MAX_TICKADJ_SCALED \
33 (((MAX_TICKADJ * NSEC_PER_USEC) << NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
36 * phase-lock loop variables
40 * clock synchronization status
42 * (TIME_ERROR prevents overwriting the CMOS clock)
44 static int time_state = TIME_OK;
46 /* clock status bits: */
47 int time_status = STA_UNSYNC;
49 /* TAI offset (secs): */
50 static long time_tai;
52 /* time adjustment (nsecs): */
53 static s64 time_offset;
55 /* pll time constant: */
56 static long time_constant = 2;
58 /* maximum error (usecs): */
59 static long time_maxerror = NTP_PHASE_LIMIT;
61 /* estimated error (usecs): */
62 static long time_esterror = NTP_PHASE_LIMIT;
64 /* frequency offset (scaled nsecs/secs): */
65 static s64 time_freq;
67 /* time at last adjustment (secs): */
68 static long time_reftime;
70 long time_adjust;
72 /* constant (boot-param configurable) NTP tick adjustment (upscaled) */
73 static s64 ntp_tick_adj;
76 * NTP methods:
80 * Update (tick_length, tick_length_base, tick_nsec), based
81 * on (tick_usec, ntp_tick_adj, time_freq):
83 static void ntp_update_frequency(void)
85 u64 second_length;
86 u64 new_base;
88 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
89 << NTP_SCALE_SHIFT;
91 second_length += ntp_tick_adj;
92 second_length += time_freq;
94 tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
95 new_base = div_u64(second_length, NTP_INTERVAL_FREQ);
98 * Don't wait for the next second_overflow, apply
99 * the change to the tick length immediately:
101 tick_length += new_base - tick_length_base;
102 tick_length_base = new_base;
105 static inline s64 ntp_update_offset_fll(s64 offset64, long secs)
107 time_status &= ~STA_MODE;
109 if (secs < MINSEC)
110 return 0;
112 if (!(time_status & STA_FLL) && (secs <= MAXSEC))
113 return 0;
115 time_status |= STA_MODE;
117 return div_s64(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
120 static void ntp_update_offset(long offset)
122 s64 freq_adj;
123 s64 offset64;
124 long secs;
126 if (!(time_status & STA_PLL))
127 return;
129 if (!(time_status & STA_NANO))
130 offset *= NSEC_PER_USEC;
133 * Scale the phase adjustment and
134 * clamp to the operating range.
136 offset = min(offset, MAXPHASE);
137 offset = max(offset, -MAXPHASE);
140 * Select how the frequency is to be controlled
141 * and in which mode (PLL or FLL).
143 secs = get_seconds() - time_reftime;
144 if (unlikely(time_status & STA_FREQHOLD))
145 secs = 0;
147 time_reftime = get_seconds();
149 offset64 = offset;
150 freq_adj = (offset64 * secs) <<
151 (NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant));
153 freq_adj += ntp_update_offset_fll(offset64, secs);
155 freq_adj = min(freq_adj + time_freq, MAXFREQ_SCALED);
157 time_freq = max(freq_adj, -MAXFREQ_SCALED);
159 time_offset = div_s64(offset64 << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
163 * ntp_clear - Clears the NTP state variables
165 * Must be called while holding a write on the xtime_lock
167 void ntp_clear(void)
169 time_adjust = 0; /* stop active adjtime() */
170 time_status |= STA_UNSYNC;
171 time_maxerror = NTP_PHASE_LIMIT;
172 time_esterror = NTP_PHASE_LIMIT;
174 ntp_update_frequency();
176 tick_length = tick_length_base;
177 time_offset = 0;
181 * this routine handles the overflow of the microsecond field
183 * The tricky bits of code to handle the accurate clock support
184 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
185 * They were originally developed for SUN and DEC kernels.
186 * All the kudos should go to Dave for this stuff.
188 * Also handles leap second processing, and returns leap offset
190 int second_overflow(unsigned long secs)
192 int leap = 0;
193 s64 delta;
196 * Leap second processing. If in leap-insert state at the end of the
197 * day, the system clock is set back one second; if in leap-delete
198 * state, the system clock is set ahead one second.
200 switch (time_state) {
201 case TIME_OK:
202 if (time_status & STA_INS)
203 time_state = TIME_INS;
204 else if (time_status & STA_DEL)
205 time_state = TIME_DEL;
206 break;
207 case TIME_INS:
208 if (!(time_status & STA_INS))
209 time_state = TIME_OK;
210 else if (secs % 86400 == 0) {
211 leap = -1;
212 time_state = TIME_OOP;
213 time_tai++;
214 printk(KERN_NOTICE
215 "Clock: inserting leap second 23:59:60 UTC\n");
217 break;
218 case TIME_DEL:
219 if (!(time_status & STA_DEL))
220 time_state = TIME_OK;
221 else if ((secs + 1) % 86400 == 0) {
222 leap = 1;
223 time_tai--;
224 time_state = TIME_WAIT;
225 printk(KERN_NOTICE
226 "Clock: deleting leap second 23:59:59 UTC\n");
228 break;
229 case TIME_OOP:
230 time_state = TIME_WAIT;
231 break;
233 case TIME_WAIT:
234 if (!(time_status & (STA_INS | STA_DEL)))
235 time_state = TIME_OK;
236 break;
240 /* Bump the maxerror field */
241 time_maxerror += MAXFREQ / NSEC_PER_USEC;
242 if (time_maxerror > NTP_PHASE_LIMIT) {
243 time_maxerror = NTP_PHASE_LIMIT;
244 time_status |= STA_UNSYNC;
248 * Compute the phase adjustment for the next second. The offset is
249 * reduced by a fixed factor times the time constant.
251 tick_length = tick_length_base;
253 delta = shift_right(time_offset, SHIFT_PLL + time_constant);
254 time_offset -= delta;
255 tick_length += delta;
257 if (!time_adjust)
258 goto out;
260 if (time_adjust > MAX_TICKADJ) {
261 time_adjust -= MAX_TICKADJ;
262 tick_length += MAX_TICKADJ_SCALED;
263 goto out;
266 if (time_adjust < -MAX_TICKADJ) {
267 time_adjust += MAX_TICKADJ;
268 tick_length -= MAX_TICKADJ_SCALED;
269 goto out;
272 tick_length += (s64)(time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ)
273 << NTP_SCALE_SHIFT;
274 time_adjust = 0;
275 out:
276 return leap;
279 #ifdef CONFIG_GENERIC_CMOS_UPDATE
281 /* Disable the cmos update - used by virtualization and embedded */
282 int no_sync_cmos_clock __read_mostly;
284 static void sync_cmos_clock(struct work_struct *work);
286 static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
288 static void sync_cmos_clock(struct work_struct *work)
290 struct timespec now, next;
291 int fail = 1;
294 * If we have an externally synchronized Linux clock, then update
295 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
296 * called as close as possible to 500 ms before the new second starts.
297 * This code is run on a timer. If the clock is set, that timer
298 * may not expire at the correct time. Thus, we adjust...
300 if (!ntp_synced()) {
302 * Not synced, exit, do not restart a timer (if one is
303 * running, let it run out).
305 return;
308 getnstimeofday(&now);
309 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
310 fail = update_persistent_clock(now);
312 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2);
313 if (next.tv_nsec <= 0)
314 next.tv_nsec += NSEC_PER_SEC;
316 if (!fail)
317 next.tv_sec = 659;
318 else
319 next.tv_sec = 0;
321 if (next.tv_nsec >= NSEC_PER_SEC) {
322 next.tv_sec++;
323 next.tv_nsec -= NSEC_PER_SEC;
325 schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next));
328 static void notify_cmos_timer(void)
330 if (!no_sync_cmos_clock)
331 schedule_delayed_work(&sync_cmos_work, 0);
334 #else
335 static inline void notify_cmos_timer(void) { }
336 #endif
340 * Propagate a new txc->status value into the NTP state:
342 static inline void process_adj_status(struct timex *txc, struct timespec *ts)
344 if ((time_status & STA_PLL) && !(txc->status & STA_PLL)) {
345 time_state = TIME_OK;
346 time_status = STA_UNSYNC;
350 * If we turn on PLL adjustments then reset the
351 * reference time to current time.
353 if (!(time_status & STA_PLL) && (txc->status & STA_PLL))
354 time_reftime = get_seconds();
356 /* only set allowed bits */
357 time_status &= STA_RONLY;
358 time_status |= txc->status & ~STA_RONLY;
362 * Called with the xtime lock held, so we can access and modify
363 * all the global NTP state:
365 static inline void process_adjtimex_modes(struct timex *txc, struct timespec *ts)
367 if (txc->modes & ADJ_STATUS)
368 process_adj_status(txc, ts);
370 if (txc->modes & ADJ_NANO)
371 time_status |= STA_NANO;
373 if (txc->modes & ADJ_MICRO)
374 time_status &= ~STA_NANO;
376 if (txc->modes & ADJ_FREQUENCY) {
377 time_freq = txc->freq * PPM_SCALE;
378 time_freq = min(time_freq, MAXFREQ_SCALED);
379 time_freq = max(time_freq, -MAXFREQ_SCALED);
382 if (txc->modes & ADJ_MAXERROR)
383 time_maxerror = txc->maxerror;
385 if (txc->modes & ADJ_ESTERROR)
386 time_esterror = txc->esterror;
388 if (txc->modes & ADJ_TIMECONST) {
389 time_constant = txc->constant;
390 if (!(time_status & STA_NANO))
391 time_constant += 4;
392 time_constant = min(time_constant, (long)MAXTC);
393 time_constant = max(time_constant, 0l);
396 if (txc->modes & ADJ_TAI && txc->constant > 0)
397 time_tai = txc->constant;
399 if (txc->modes & ADJ_OFFSET)
400 ntp_update_offset(txc->offset);
402 if (txc->modes & ADJ_TICK)
403 tick_usec = txc->tick;
405 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
406 ntp_update_frequency();
410 * adjtimex mainly allows reading (and writing, if superuser) of
411 * kernel time-keeping variables. used by xntpd.
413 int do_adjtimex(struct timex *txc)
415 struct timespec ts;
416 int result;
418 /* Validate the data before disabling interrupts */
419 if (txc->modes & ADJ_ADJTIME) {
420 /* singleshot must not be used with any other mode bits */
421 if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
422 return -EINVAL;
423 if (!(txc->modes & ADJ_OFFSET_READONLY) &&
424 !capable(CAP_SYS_TIME))
425 return -EPERM;
426 } else {
427 /* In order to modify anything, you gotta be super-user! */
428 if (txc->modes && !capable(CAP_SYS_TIME))
429 return -EPERM;
432 * if the quartz is off by more than 10% then
433 * something is VERY wrong!
435 if (txc->modes & ADJ_TICK &&
436 (txc->tick < 900000/USER_HZ ||
437 txc->tick > 1100000/USER_HZ))
438 return -EINVAL;
441 getnstimeofday(&ts);
443 write_seqlock_irq(&xtime_lock);
445 if (txc->modes & ADJ_ADJTIME) {
446 long save_adjust = time_adjust;
448 if (!(txc->modes & ADJ_OFFSET_READONLY)) {
449 /* adjtime() is independent from ntp_adjtime() */
450 time_adjust = txc->offset;
451 ntp_update_frequency();
453 txc->offset = save_adjust;
454 } else {
456 /* If there are input parameters, then process them: */
457 if (txc->modes)
458 process_adjtimex_modes(txc, &ts);
460 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
461 NTP_SCALE_SHIFT);
462 if (!(time_status & STA_NANO))
463 txc->offset /= NSEC_PER_USEC;
466 result = time_state; /* mostly `TIME_OK' */
467 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
468 result = TIME_ERROR;
470 txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
471 PPM_SCALE_INV, NTP_SCALE_SHIFT);
472 txc->maxerror = time_maxerror;
473 txc->esterror = time_esterror;
474 txc->status = time_status;
475 txc->constant = time_constant;
476 txc->precision = 1;
477 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
478 txc->tick = tick_usec;
479 txc->tai = time_tai;
481 /* PPS is not implemented, so these are zero */
482 txc->ppsfreq = 0;
483 txc->jitter = 0;
484 txc->shift = 0;
485 txc->stabil = 0;
486 txc->jitcnt = 0;
487 txc->calcnt = 0;
488 txc->errcnt = 0;
489 txc->stbcnt = 0;
491 write_sequnlock_irq(&xtime_lock);
493 txc->time.tv_sec = ts.tv_sec;
494 txc->time.tv_usec = ts.tv_nsec;
495 if (!(time_status & STA_NANO))
496 txc->time.tv_usec /= NSEC_PER_USEC;
498 notify_cmos_timer();
500 return result;
503 static int __init ntp_tick_adj_setup(char *str)
505 ntp_tick_adj = simple_strtol(str, NULL, 0);
506 ntp_tick_adj <<= NTP_SCALE_SHIFT;
508 return 1;
511 __setup("ntp_tick_adj=", ntp_tick_adj_setup);
513 void __init ntp_init(void)
515 ntp_clear();