blob: 5fd9b946977038cbdc192dc5a11146696ad53947 [file] [log] [blame]
john stultz4c7ee8d2006-09-30 23:28:22 -07001/*
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>
Thomas Gleixner82644452007-07-21 04:37:37 -070013#include <linux/timer.h>
john stultz4c7ee8d2006-09-30 23:28:22 -070014#include <linux/timex.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040015#include <linux/jiffies.h>
16#include <linux/hrtimer.h>
Alexey Dobriyanaa0ac362007-07-15 23:40:39 -070017#include <linux/capability.h>
john stultz4c7ee8d2006-09-30 23:28:22 -070018#include <asm/div64.h>
19#include <asm/timex.h>
20
Roman Zippelb0ee7552006-09-30 23:28:22 -070021/*
22 * Timekeeping variables
23 */
24unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
25unsigned long tick_nsec; /* ACTHZ period (nsec) */
26static u64 tick_length, tick_length_base;
27
Roman Zippel8f807f82006-09-30 23:28:25 -070028#define MAX_TICKADJ 500 /* microsecs */
29#define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \
john stultzf4304ab2007-02-16 01:27:26 -080030 TICK_LENGTH_SHIFT) / NTP_INTERVAL_FREQ)
john stultz4c7ee8d2006-09-30 23:28:22 -070031
32/*
33 * phase-lock loop variables
34 */
35/* TIME_ERROR prevents overwriting the CMOS clock */
Adrian Bunk70bc42f2006-09-30 23:28:29 -070036static int time_state = TIME_OK; /* clock synchronization status */
john stultz4c7ee8d2006-09-30 23:28:22 -070037int time_status = STA_UNSYNC; /* clock status bits */
john stultzd62ac212007-03-26 21:32:26 -080038static s64 time_offset; /* time adjustment (ns) */
Adrian Bunk70bc42f2006-09-30 23:28:29 -070039static long time_constant = 2; /* pll time constant */
john stultz4c7ee8d2006-09-30 23:28:22 -070040long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
41long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
Roman Zippeldc6a43e2006-09-30 23:28:24 -070042long time_freq; /* frequency offset (scaled ppm)*/
Adrian Bunk70bc42f2006-09-30 23:28:29 -070043static long time_reftime; /* time at last adjustment (s) */
john stultz4c7ee8d2006-09-30 23:28:22 -070044long time_adjust;
Roman Zippel10a398d2008-03-04 15:14:26 -080045static long ntp_tick_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -070046
Adrian Bunk70bc42f2006-09-30 23:28:29 -070047static void ntp_update_frequency(void)
48{
john stultzf4304ab2007-02-16 01:27:26 -080049 u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
50 << TICK_LENGTH_SHIFT;
Roman Zippel10a398d2008-03-04 15:14:26 -080051 second_length += (s64)ntp_tick_adj << TICK_LENGTH_SHIFT;
john stultzf4304ab2007-02-16 01:27:26 -080052 second_length += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC);
Adrian Bunk70bc42f2006-09-30 23:28:29 -070053
john stultzf4304ab2007-02-16 01:27:26 -080054 tick_length_base = second_length;
Adrian Bunk70bc42f2006-09-30 23:28:29 -070055
john stultzf4304ab2007-02-16 01:27:26 -080056 do_div(second_length, HZ);
57 tick_nsec = second_length >> TICK_LENGTH_SHIFT;
58
59 do_div(tick_length_base, NTP_INTERVAL_FREQ);
Adrian Bunk70bc42f2006-09-30 23:28:29 -070060}
61
Roman Zippelb0ee7552006-09-30 23:28:22 -070062/**
63 * ntp_clear - Clears the NTP state variables
64 *
65 * Must be called while holding a write on the xtime_lock
66 */
67void ntp_clear(void)
68{
69 time_adjust = 0; /* stop active adjtime() */
70 time_status |= STA_UNSYNC;
71 time_maxerror = NTP_PHASE_LIMIT;
72 time_esterror = NTP_PHASE_LIMIT;
73
74 ntp_update_frequency();
75
76 tick_length = tick_length_base;
Roman Zippel3d3675c2006-09-30 23:28:25 -070077 time_offset = 0;
Roman Zippelb0ee7552006-09-30 23:28:22 -070078}
79
john stultz4c7ee8d2006-09-30 23:28:22 -070080/*
81 * this routine handles the overflow of the microsecond field
82 *
83 * The tricky bits of code to handle the accurate clock support
84 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
85 * They were originally developed for SUN and DEC kernels.
86 * All the kudos should go to Dave for this stuff.
87 */
88void second_overflow(void)
89{
Roman Zippel3d3675c2006-09-30 23:28:25 -070090 long time_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -070091
92 /* Bump the maxerror field */
Roman Zippel97eebe12006-09-30 23:28:26 -070093 time_maxerror += MAXFREQ >> SHIFT_USEC;
john stultz4c7ee8d2006-09-30 23:28:22 -070094 if (time_maxerror > NTP_PHASE_LIMIT) {
95 time_maxerror = NTP_PHASE_LIMIT;
96 time_status |= STA_UNSYNC;
97 }
98
99 /*
100 * Leap second processing. If in leap-insert state at the end of the
101 * day, the system clock is set back one second; if in leap-delete
102 * state, the system clock is set ahead one second. The microtime()
103 * routine or external clock driver will insure that reported time is
104 * always monotonic. The ugly divides should be replaced.
105 */
106 switch (time_state) {
107 case TIME_OK:
108 if (time_status & STA_INS)
109 time_state = TIME_INS;
110 else if (time_status & STA_DEL)
111 time_state = TIME_DEL;
112 break;
113 case TIME_INS:
114 if (xtime.tv_sec % 86400 == 0) {
115 xtime.tv_sec--;
116 wall_to_monotonic.tv_sec++;
john stultz4c7ee8d2006-09-30 23:28:22 -0700117 time_state = TIME_OOP;
john stultz4c7ee8d2006-09-30 23:28:22 -0700118 printk(KERN_NOTICE "Clock: inserting leap second "
119 "23:59:60 UTC\n");
120 }
121 break;
122 case TIME_DEL:
123 if ((xtime.tv_sec + 1) % 86400 == 0) {
124 xtime.tv_sec++;
125 wall_to_monotonic.tv_sec--;
john stultz4c7ee8d2006-09-30 23:28:22 -0700126 time_state = TIME_WAIT;
john stultz4c7ee8d2006-09-30 23:28:22 -0700127 printk(KERN_NOTICE "Clock: deleting leap second "
128 "23:59:59 UTC\n");
129 }
130 break;
131 case TIME_OOP:
132 time_state = TIME_WAIT;
133 break;
134 case TIME_WAIT:
135 if (!(time_status & (STA_INS | STA_DEL)))
136 time_state = TIME_OK;
137 }
138
139 /*
Roman Zippelf1992392006-09-30 23:28:28 -0700140 * Compute the phase adjustment for the next second. The offset is
141 * reduced by a fixed factor times the time constant.
john stultz4c7ee8d2006-09-30 23:28:22 -0700142 */
Roman Zippelb0ee7552006-09-30 23:28:22 -0700143 tick_length = tick_length_base;
Roman Zippelf1992392006-09-30 23:28:28 -0700144 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
Roman Zippel3d3675c2006-09-30 23:28:25 -0700145 time_offset -= time_adj;
146 tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE);
john stultz4c7ee8d2006-09-30 23:28:22 -0700147
Roman Zippel8f807f82006-09-30 23:28:25 -0700148 if (unlikely(time_adjust)) {
149 if (time_adjust > MAX_TICKADJ) {
150 time_adjust -= MAX_TICKADJ;
151 tick_length += MAX_TICKADJ_SCALED;
152 } else if (time_adjust < -MAX_TICKADJ) {
153 time_adjust += MAX_TICKADJ;
154 tick_length -= MAX_TICKADJ_SCALED;
155 } else {
Roman Zippel8f807f82006-09-30 23:28:25 -0700156 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
john stultzf4304ab2007-02-16 01:27:26 -0800157 NTP_INTERVAL_FREQ) << TICK_LENGTH_SHIFT;
Jim Houstonbb1d8602006-10-28 10:38:56 -0700158 time_adjust = 0;
Roman Zippel8f807f82006-09-30 23:28:25 -0700159 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700160 }
161}
162
163/*
164 * Return how long ticks are at the moment, that is, how much time
165 * update_wall_time_one_tick will add to xtime next time we call it
166 * (assuming no calls to do_adjtimex in the meantime).
167 * The return value is in fixed-point nanoseconds shifted by the
168 * specified number of bits to the right of the binary point.
169 * This function has no side-effects.
170 */
171u64 current_tick_length(void)
172{
Roman Zippel8f807f82006-09-30 23:28:25 -0700173 return tick_length;
john stultz4c7ee8d2006-09-30 23:28:22 -0700174}
175
Thomas Gleixner82644452007-07-21 04:37:37 -0700176#ifdef CONFIG_GENERIC_CMOS_UPDATE
john stultz4c7ee8d2006-09-30 23:28:22 -0700177
Thomas Gleixner82644452007-07-21 04:37:37 -0700178/* Disable the cmos update - used by virtualization and embedded */
179int no_sync_cmos_clock __read_mostly;
180
181static void sync_cmos_clock(unsigned long dummy);
182
183static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0);
184
185static void sync_cmos_clock(unsigned long dummy)
john stultz4c7ee8d2006-09-30 23:28:22 -0700186{
Thomas Gleixner82644452007-07-21 04:37:37 -0700187 struct timespec now, next;
188 int fail = 1;
189
190 /*
191 * If we have an externally synchronized Linux clock, then update
192 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
193 * called as close as possible to 500 ms before the new second starts.
194 * This code is run on a timer. If the clock is set, that timer
195 * may not expire at the correct time. Thus, we adjust...
196 */
197 if (!ntp_synced())
198 /*
199 * Not synced, exit, do not restart a timer (if one is
200 * running, let it run out).
201 */
202 return;
203
204 getnstimeofday(&now);
David P. Reedfa6a1a52007-11-14 17:49:21 -0500205 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
Thomas Gleixner82644452007-07-21 04:37:37 -0700206 fail = update_persistent_clock(now);
207
208 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec;
209 if (next.tv_nsec <= 0)
210 next.tv_nsec += NSEC_PER_SEC;
211
212 if (!fail)
213 next.tv_sec = 659;
214 else
215 next.tv_sec = 0;
216
217 if (next.tv_nsec >= NSEC_PER_SEC) {
218 next.tv_sec++;
219 next.tv_nsec -= NSEC_PER_SEC;
220 }
221 mod_timer(&sync_cmos_timer, jiffies + timespec_to_jiffies(&next));
john stultz4c7ee8d2006-09-30 23:28:22 -0700222}
223
Thomas Gleixner82644452007-07-21 04:37:37 -0700224static void notify_cmos_timer(void)
225{
Tony Breeds298a5df2007-09-11 15:24:03 -0700226 if (!no_sync_cmos_clock)
Thomas Gleixner82644452007-07-21 04:37:37 -0700227 mod_timer(&sync_cmos_timer, jiffies + 1);
228}
229
230#else
231static inline void notify_cmos_timer(void) { }
232#endif
233
john stultz4c7ee8d2006-09-30 23:28:22 -0700234/* adjtimex mainly allows reading (and writing, if superuser) of
235 * kernel time-keeping variables. used by xntpd.
236 */
237int do_adjtimex(struct timex *txc)
238{
john stultzd62ac212007-03-26 21:32:26 -0800239 long mtemp, save_adjust, rem;
Roman Zippelf1992392006-09-30 23:28:28 -0700240 s64 freq_adj, temp64;
john stultz4c7ee8d2006-09-30 23:28:22 -0700241 int result;
242
243 /* In order to modify anything, you gotta be super-user! */
244 if (txc->modes && !capable(CAP_SYS_TIME))
245 return -EPERM;
246
247 /* Now we validate the data before disabling interrupts */
248
John Stultz52bfb362007-11-26 20:42:19 +0100249 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) {
john stultz4c7ee8d2006-09-30 23:28:22 -0700250 /* singleshot must not be used with any other mode bits */
John Stultz52bfb362007-11-26 20:42:19 +0100251 if (txc->modes != ADJ_OFFSET_SINGLESHOT &&
252 txc->modes != ADJ_OFFSET_SS_READ)
john stultz4c7ee8d2006-09-30 23:28:22 -0700253 return -EINVAL;
John Stultz52bfb362007-11-26 20:42:19 +0100254 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700255
256 if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
257 /* adjustment Offset limited to +- .512 seconds */
258 if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
259 return -EINVAL;
260
261 /* if the quartz is off by more than 10% something is VERY wrong ! */
262 if (txc->modes & ADJ_TICK)
263 if (txc->tick < 900000/USER_HZ ||
264 txc->tick > 1100000/USER_HZ)
265 return -EINVAL;
266
267 write_seqlock_irq(&xtime_lock);
268 result = time_state; /* mostly `TIME_OK' */
269
270 /* Save for later - semantics of adjtime is to return old value */
Roman Zippel8f807f82006-09-30 23:28:25 -0700271 save_adjust = time_adjust;
john stultz4c7ee8d2006-09-30 23:28:22 -0700272
273#if 0 /* STA_CLOCKERR is never set yet */
274 time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
275#endif
276 /* If there are input parameters, then process them */
277 if (txc->modes)
278 {
279 if (txc->modes & ADJ_STATUS) /* only set allowed bits */
280 time_status = (txc->status & ~STA_RONLY) |
281 (time_status & STA_RONLY);
282
283 if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
284 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
285 result = -EINVAL;
286 goto leave;
287 }
john stultzf4304ab2007-02-16 01:27:26 -0800288 time_freq = ((s64)txc->freq * NSEC_PER_USEC)
289 >> (SHIFT_USEC - SHIFT_NSEC);
john stultz4c7ee8d2006-09-30 23:28:22 -0700290 }
291
292 if (txc->modes & ADJ_MAXERROR) {
293 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
294 result = -EINVAL;
295 goto leave;
296 }
297 time_maxerror = txc->maxerror;
298 }
299
300 if (txc->modes & ADJ_ESTERROR) {
301 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
302 result = -EINVAL;
303 goto leave;
304 }
305 time_esterror = txc->esterror;
306 }
307
308 if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
309 if (txc->constant < 0) { /* NTP v4 uses values > 6 */
310 result = -EINVAL;
311 goto leave;
312 }
Roman Zippelf1992392006-09-30 23:28:28 -0700313 time_constant = min(txc->constant + 4, (long)MAXTC);
john stultz4c7ee8d2006-09-30 23:28:22 -0700314 }
315
316 if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
317 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
318 /* adjtime() is independent from ntp_adjtime() */
Roman Zippel8f807f82006-09-30 23:28:25 -0700319 time_adjust = txc->offset;
john stultz4c7ee8d2006-09-30 23:28:22 -0700320 }
321 else if (time_status & STA_PLL) {
john stultzd62ac212007-03-26 21:32:26 -0800322 time_offset = txc->offset * NSEC_PER_USEC;
john stultz4c7ee8d2006-09-30 23:28:22 -0700323
324 /*
325 * Scale the phase adjustment and
326 * clamp to the operating range.
327 */
john stultzd62ac212007-03-26 21:32:26 -0800328 time_offset = min(time_offset, (s64)MAXPHASE * NSEC_PER_USEC);
329 time_offset = max(time_offset, (s64)-MAXPHASE * NSEC_PER_USEC);
john stultz4c7ee8d2006-09-30 23:28:22 -0700330
331 /*
332 * Select whether the frequency is to be controlled
333 * and in which mode (PLL or FLL). Clamp to the operating
334 * range. Ugly multiply/divide should be replaced someday.
335 */
336
337 if (time_status & STA_FREQHOLD || time_reftime == 0)
338 time_reftime = xtime.tv_sec;
339 mtemp = xtime.tv_sec - time_reftime;
340 time_reftime = xtime.tv_sec;
Roman Zippelf1992392006-09-30 23:28:28 -0700341
john stultzd62ac212007-03-26 21:32:26 -0800342 freq_adj = time_offset * mtemp;
Roman Zippelf1992392006-09-30 23:28:28 -0700343 freq_adj = shift_right(freq_adj, time_constant * 2 +
344 (SHIFT_PLL + 2) * 2 - SHIFT_NSEC);
345 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
David Howellse48af192008-02-25 18:31:57 +0100346 u64 utemp64;
john stultzd62ac212007-03-26 21:32:26 -0800347 temp64 = time_offset << (SHIFT_NSEC - SHIFT_FLL);
Roman Zippelf1992392006-09-30 23:28:28 -0700348 if (time_offset < 0) {
David Howellse48af192008-02-25 18:31:57 +0100349 utemp64 = -temp64;
350 do_div(utemp64, mtemp);
351 freq_adj -= utemp64;
Roman Zippelf1992392006-09-30 23:28:28 -0700352 } else {
David Howellse48af192008-02-25 18:31:57 +0100353 utemp64 = temp64;
354 do_div(utemp64, mtemp);
355 freq_adj += utemp64;
Roman Zippelf1992392006-09-30 23:28:28 -0700356 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700357 }
Roman Zippel04b617e2006-09-30 23:28:27 -0700358 freq_adj += time_freq;
359 freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC);
360 time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC);
john stultzd62ac212007-03-26 21:32:26 -0800361 time_offset = div_long_long_rem_signed(time_offset,
362 NTP_INTERVAL_FREQ,
363 &rem);
364 time_offset <<= SHIFT_UPDATE;
john stultz4c7ee8d2006-09-30 23:28:22 -0700365 } /* STA_PLL */
366 } /* txc->modes & ADJ_OFFSET */
Roman Zippelb0ee7552006-09-30 23:28:22 -0700367 if (txc->modes & ADJ_TICK)
john stultz4c7ee8d2006-09-30 23:28:22 -0700368 tick_usec = txc->tick;
Roman Zippelb0ee7552006-09-30 23:28:22 -0700369
Roman Zippeldc6a43e2006-09-30 23:28:24 -0700370 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
Roman Zippelb0ee7552006-09-30 23:28:22 -0700371 ntp_update_frequency();
john stultz4c7ee8d2006-09-30 23:28:22 -0700372 } /* txc->modes */
373leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
374 result = TIME_ERROR;
375
John Stultz52bfb362007-11-26 20:42:19 +0100376 if ((txc->modes == ADJ_OFFSET_SINGLESHOT) ||
377 (txc->modes == ADJ_OFFSET_SS_READ))
john stultzd62ac212007-03-26 21:32:26 -0800378 txc->offset = save_adjust;
Roman Zippel3d3675c2006-09-30 23:28:25 -0700379 else
john stultzd62ac212007-03-26 21:32:26 -0800380 txc->offset = ((long)shift_right(time_offset, SHIFT_UPDATE)) *
381 NTP_INTERVAL_FREQ / 1000;
382 txc->freq = (time_freq / NSEC_PER_USEC) <<
383 (SHIFT_USEC - SHIFT_NSEC);
john stultz4c7ee8d2006-09-30 23:28:22 -0700384 txc->maxerror = time_maxerror;
385 txc->esterror = time_esterror;
386 txc->status = time_status;
387 txc->constant = time_constant;
Adrian Bunk70bc42f2006-09-30 23:28:29 -0700388 txc->precision = 1;
Roman Zippel97eebe12006-09-30 23:28:26 -0700389 txc->tolerance = MAXFREQ;
john stultz4c7ee8d2006-09-30 23:28:22 -0700390 txc->tick = tick_usec;
391
392 /* PPS is not implemented, so these are zero */
393 txc->ppsfreq = 0;
394 txc->jitter = 0;
395 txc->shift = 0;
396 txc->stabil = 0;
397 txc->jitcnt = 0;
398 txc->calcnt = 0;
399 txc->errcnt = 0;
400 txc->stbcnt = 0;
401 write_sequnlock_irq(&xtime_lock);
402 do_gettimeofday(&txc->time);
Thomas Gleixner82644452007-07-21 04:37:37 -0700403 notify_cmos_timer();
john stultz4c7ee8d2006-09-30 23:28:22 -0700404 return(result);
405}
Roman Zippel10a398d2008-03-04 15:14:26 -0800406
407static int __init ntp_tick_adj_setup(char *str)
408{
409 ntp_tick_adj = simple_strtol(str, NULL, 0);
410 return 1;
411}
412
413__setup("ntp_tick_adj=", ntp_tick_adj_setup);