blob: 44491de312a0855d8b9224bc0b9a2504600eaf1f [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>
Roman Zippel71abb3a2008-05-01 04:34:26 -070018#include <linux/math64.h>
john stultz4c7ee8d2006-09-30 23:28:22 -070019#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 */
Roman Zippelee9851b2008-05-01 04:34:32 -070038static 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 Zippel074b3b82008-05-01 04:34:34 -070042static s64 time_freq; /* frequency offset (scaled ns/s)*/
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;
Roman Zippel074b3b82008-05-01 04:34:34 -070052 second_length += time_freq;
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
Roman Zippel71abb3a2008-05-01 04:34:26 -070056 tick_nsec = div_u64(second_length, HZ) >> TICK_LENGTH_SHIFT;
57 tick_length_base = div_u64(tick_length_base, NTP_INTERVAL_FREQ);
Adrian Bunk70bc42f2006-09-30 23:28:29 -070058}
59
Roman Zippelee9851b2008-05-01 04:34:32 -070060static void ntp_update_offset(long offset)
61{
62 long mtemp;
63 s64 freq_adj;
64
65 if (!(time_status & STA_PLL))
66 return;
67
Roman Zippeleea83d82008-05-01 04:34:33 -070068 if (!(time_status & STA_NANO))
Roman Zippel9f14f662008-05-01 04:34:36 -070069 offset *= NSEC_PER_USEC;
Roman Zippelee9851b2008-05-01 04:34:32 -070070
71 /*
72 * Scale the phase adjustment and
73 * clamp to the operating range.
74 */
Roman Zippel9f14f662008-05-01 04:34:36 -070075 offset = min(offset, MAXPHASE);
76 offset = max(offset, -MAXPHASE);
Roman Zippelee9851b2008-05-01 04:34:32 -070077
78 /*
79 * Select how the frequency is to be controlled
80 * and in which mode (PLL or FLL).
81 */
82 if (time_status & STA_FREQHOLD || time_reftime == 0)
83 time_reftime = xtime.tv_sec;
84 mtemp = xtime.tv_sec - time_reftime;
85 time_reftime = xtime.tv_sec;
86
Roman Zippel9f14f662008-05-01 04:34:36 -070087 freq_adj = (s64)offset * mtemp;
Roman Zippel074b3b82008-05-01 04:34:34 -070088 freq_adj <<= TICK_LENGTH_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant);
Roman Zippeleea83d82008-05-01 04:34:33 -070089 time_status &= ~STA_MODE;
90 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
Roman Zippel9f14f662008-05-01 04:34:36 -070091 freq_adj += div_s64((s64)offset << (TICK_LENGTH_SHIFT - SHIFT_FLL),
Roman Zippel074b3b82008-05-01 04:34:34 -070092 mtemp);
Roman Zippeleea83d82008-05-01 04:34:33 -070093 time_status |= STA_MODE;
94 }
Roman Zippelee9851b2008-05-01 04:34:32 -070095 freq_adj += time_freq;
Roman Zippel074b3b82008-05-01 04:34:34 -070096 freq_adj = min(freq_adj, MAXFREQ_SCALED);
97 time_freq = max(freq_adj, -MAXFREQ_SCALED);
Roman Zippel9f14f662008-05-01 04:34:36 -070098
99 time_offset = div_s64((s64)offset << TICK_LENGTH_SHIFT, NTP_INTERVAL_FREQ);
Roman Zippelee9851b2008-05-01 04:34:32 -0700100}
101
Roman Zippelb0ee7552006-09-30 23:28:22 -0700102/**
103 * ntp_clear - Clears the NTP state variables
104 *
105 * Must be called while holding a write on the xtime_lock
106 */
107void ntp_clear(void)
108{
109 time_adjust = 0; /* stop active adjtime() */
110 time_status |= STA_UNSYNC;
111 time_maxerror = NTP_PHASE_LIMIT;
112 time_esterror = NTP_PHASE_LIMIT;
113
114 ntp_update_frequency();
115
116 tick_length = tick_length_base;
Roman Zippel3d3675c2006-09-30 23:28:25 -0700117 time_offset = 0;
Roman Zippelb0ee7552006-09-30 23:28:22 -0700118}
119
john stultz4c7ee8d2006-09-30 23:28:22 -0700120/*
121 * this routine handles the overflow of the microsecond field
122 *
123 * The tricky bits of code to handle the accurate clock support
124 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
125 * They were originally developed for SUN and DEC kernels.
126 * All the kudos should go to Dave for this stuff.
127 */
128void second_overflow(void)
129{
Roman Zippel9f14f662008-05-01 04:34:36 -0700130 s64 time_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -0700131
132 /* Bump the maxerror field */
Roman Zippel074b3b82008-05-01 04:34:34 -0700133 time_maxerror += MAXFREQ / NSEC_PER_USEC;
john stultz4c7ee8d2006-09-30 23:28:22 -0700134 if (time_maxerror > NTP_PHASE_LIMIT) {
135 time_maxerror = NTP_PHASE_LIMIT;
136 time_status |= STA_UNSYNC;
137 }
138
139 /*
140 * Leap second processing. If in leap-insert state at the end of the
141 * day, the system clock is set back one second; if in leap-delete
142 * state, the system clock is set ahead one second. The microtime()
143 * routine or external clock driver will insure that reported time is
144 * always monotonic. The ugly divides should be replaced.
145 */
146 switch (time_state) {
147 case TIME_OK:
148 if (time_status & STA_INS)
149 time_state = TIME_INS;
150 else if (time_status & STA_DEL)
151 time_state = TIME_DEL;
152 break;
153 case TIME_INS:
154 if (xtime.tv_sec % 86400 == 0) {
155 xtime.tv_sec--;
156 wall_to_monotonic.tv_sec++;
john stultz4c7ee8d2006-09-30 23:28:22 -0700157 time_state = TIME_OOP;
john stultz4c7ee8d2006-09-30 23:28:22 -0700158 printk(KERN_NOTICE "Clock: inserting leap second "
159 "23:59:60 UTC\n");
160 }
161 break;
162 case TIME_DEL:
163 if ((xtime.tv_sec + 1) % 86400 == 0) {
164 xtime.tv_sec++;
165 wall_to_monotonic.tv_sec--;
john stultz4c7ee8d2006-09-30 23:28:22 -0700166 time_state = TIME_WAIT;
john stultz4c7ee8d2006-09-30 23:28:22 -0700167 printk(KERN_NOTICE "Clock: deleting leap second "
168 "23:59:59 UTC\n");
169 }
170 break;
171 case TIME_OOP:
172 time_state = TIME_WAIT;
173 break;
174 case TIME_WAIT:
175 if (!(time_status & (STA_INS | STA_DEL)))
Roman Zippelee9851b2008-05-01 04:34:32 -0700176 time_state = TIME_OK;
john stultz4c7ee8d2006-09-30 23:28:22 -0700177 }
178
179 /*
Roman Zippelf1992392006-09-30 23:28:28 -0700180 * Compute the phase adjustment for the next second. The offset is
181 * reduced by a fixed factor times the time constant.
john stultz4c7ee8d2006-09-30 23:28:22 -0700182 */
Roman Zippelb0ee7552006-09-30 23:28:22 -0700183 tick_length = tick_length_base;
Roman Zippelf1992392006-09-30 23:28:28 -0700184 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
Roman Zippel3d3675c2006-09-30 23:28:25 -0700185 time_offset -= time_adj;
Roman Zippel9f14f662008-05-01 04:34:36 -0700186 tick_length += time_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -0700187
Roman Zippel8f807f82006-09-30 23:28:25 -0700188 if (unlikely(time_adjust)) {
189 if (time_adjust > MAX_TICKADJ) {
190 time_adjust -= MAX_TICKADJ;
191 tick_length += MAX_TICKADJ_SCALED;
192 } else if (time_adjust < -MAX_TICKADJ) {
193 time_adjust += MAX_TICKADJ;
194 tick_length -= MAX_TICKADJ_SCALED;
195 } else {
Roman Zippel8f807f82006-09-30 23:28:25 -0700196 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
john stultzf4304ab2007-02-16 01:27:26 -0800197 NTP_INTERVAL_FREQ) << TICK_LENGTH_SHIFT;
Jim Houstonbb1d8602006-10-28 10:38:56 -0700198 time_adjust = 0;
Roman Zippel8f807f82006-09-30 23:28:25 -0700199 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700200 }
201}
202
203/*
204 * Return how long ticks are at the moment, that is, how much time
205 * update_wall_time_one_tick will add to xtime next time we call it
206 * (assuming no calls to do_adjtimex in the meantime).
207 * The return value is in fixed-point nanoseconds shifted by the
208 * specified number of bits to the right of the binary point.
209 * This function has no side-effects.
210 */
211u64 current_tick_length(void)
212{
Roman Zippel8f807f82006-09-30 23:28:25 -0700213 return tick_length;
john stultz4c7ee8d2006-09-30 23:28:22 -0700214}
215
Thomas Gleixner82644452007-07-21 04:37:37 -0700216#ifdef CONFIG_GENERIC_CMOS_UPDATE
john stultz4c7ee8d2006-09-30 23:28:22 -0700217
Thomas Gleixner82644452007-07-21 04:37:37 -0700218/* Disable the cmos update - used by virtualization and embedded */
219int no_sync_cmos_clock __read_mostly;
220
221static void sync_cmos_clock(unsigned long dummy);
222
223static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0);
224
225static void sync_cmos_clock(unsigned long dummy)
john stultz4c7ee8d2006-09-30 23:28:22 -0700226{
Thomas Gleixner82644452007-07-21 04:37:37 -0700227 struct timespec now, next;
228 int fail = 1;
229
230 /*
231 * If we have an externally synchronized Linux clock, then update
232 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
233 * called as close as possible to 500 ms before the new second starts.
234 * This code is run on a timer. If the clock is set, that timer
235 * may not expire at the correct time. Thus, we adjust...
236 */
237 if (!ntp_synced())
238 /*
239 * Not synced, exit, do not restart a timer (if one is
240 * running, let it run out).
241 */
242 return;
243
244 getnstimeofday(&now);
David P. Reedfa6a1a52007-11-14 17:49:21 -0500245 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
Thomas Gleixner82644452007-07-21 04:37:37 -0700246 fail = update_persistent_clock(now);
247
248 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec;
249 if (next.tv_nsec <= 0)
250 next.tv_nsec += NSEC_PER_SEC;
251
252 if (!fail)
253 next.tv_sec = 659;
254 else
255 next.tv_sec = 0;
256
257 if (next.tv_nsec >= NSEC_PER_SEC) {
258 next.tv_sec++;
259 next.tv_nsec -= NSEC_PER_SEC;
260 }
261 mod_timer(&sync_cmos_timer, jiffies + timespec_to_jiffies(&next));
john stultz4c7ee8d2006-09-30 23:28:22 -0700262}
263
Thomas Gleixner82644452007-07-21 04:37:37 -0700264static void notify_cmos_timer(void)
265{
Tony Breeds298a5df2007-09-11 15:24:03 -0700266 if (!no_sync_cmos_clock)
Thomas Gleixner82644452007-07-21 04:37:37 -0700267 mod_timer(&sync_cmos_timer, jiffies + 1);
268}
269
270#else
271static inline void notify_cmos_timer(void) { }
272#endif
273
john stultz4c7ee8d2006-09-30 23:28:22 -0700274/* adjtimex mainly allows reading (and writing, if superuser) of
275 * kernel time-keeping variables. used by xntpd.
276 */
277int do_adjtimex(struct timex *txc)
278{
Roman Zippeleea83d82008-05-01 04:34:33 -0700279 struct timespec ts;
Roman Zippelee9851b2008-05-01 04:34:32 -0700280 long save_adjust;
john stultz4c7ee8d2006-09-30 23:28:22 -0700281 int result;
282
283 /* In order to modify anything, you gotta be super-user! */
284 if (txc->modes && !capable(CAP_SYS_TIME))
285 return -EPERM;
286
287 /* Now we validate the data before disabling interrupts */
288
John Stultz52bfb362007-11-26 20:42:19 +0100289 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) {
Roman Zippeleea83d82008-05-01 04:34:33 -0700290 /* singleshot must not be used with any other mode bits */
291 if (txc->modes & ~ADJ_OFFSET_SS_READ)
john stultz4c7ee8d2006-09-30 23:28:22 -0700292 return -EINVAL;
John Stultz52bfb362007-11-26 20:42:19 +0100293 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700294
john stultz4c7ee8d2006-09-30 23:28:22 -0700295 /* if the quartz is off by more than 10% something is VERY wrong ! */
296 if (txc->modes & ADJ_TICK)
297 if (txc->tick < 900000/USER_HZ ||
298 txc->tick > 1100000/USER_HZ)
299 return -EINVAL;
300
301 write_seqlock_irq(&xtime_lock);
john stultz4c7ee8d2006-09-30 23:28:22 -0700302
303 /* Save for later - semantics of adjtime is to return old value */
Roman Zippel8f807f82006-09-30 23:28:25 -0700304 save_adjust = time_adjust;
john stultz4c7ee8d2006-09-30 23:28:22 -0700305
john stultz4c7ee8d2006-09-30 23:28:22 -0700306 /* If there are input parameters, then process them */
Roman Zippelee9851b2008-05-01 04:34:32 -0700307 if (txc->modes) {
Roman Zippeleea83d82008-05-01 04:34:33 -0700308 if (txc->modes & ADJ_STATUS) {
309 if ((time_status & STA_PLL) &&
310 !(txc->status & STA_PLL)) {
311 time_state = TIME_OK;
312 time_status = STA_UNSYNC;
313 }
314 /* only set allowed bits */
315 time_status &= STA_RONLY;
316 time_status |= txc->status & ~STA_RONLY;
317 }
318
319 if (txc->modes & ADJ_NANO)
320 time_status |= STA_NANO;
321 if (txc->modes & ADJ_MICRO)
322 time_status &= ~STA_NANO;
john stultz4c7ee8d2006-09-30 23:28:22 -0700323
Roman Zippelee9851b2008-05-01 04:34:32 -0700324 if (txc->modes & ADJ_FREQUENCY) {
Roman Zippel074b3b82008-05-01 04:34:34 -0700325 time_freq = (s64)txc->freq * PPM_SCALE;
326 time_freq = min(time_freq, MAXFREQ_SCALED);
327 time_freq = max(time_freq, -MAXFREQ_SCALED);
john stultz4c7ee8d2006-09-30 23:28:22 -0700328 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700329
Roman Zippeleea83d82008-05-01 04:34:33 -0700330 if (txc->modes & ADJ_MAXERROR)
Roman Zippelee9851b2008-05-01 04:34:32 -0700331 time_maxerror = txc->maxerror;
Roman Zippeleea83d82008-05-01 04:34:33 -0700332 if (txc->modes & ADJ_ESTERROR)
Roman Zippelee9851b2008-05-01 04:34:32 -0700333 time_esterror = txc->esterror;
john stultz4c7ee8d2006-09-30 23:28:22 -0700334
Roman Zippelee9851b2008-05-01 04:34:32 -0700335 if (txc->modes & ADJ_TIMECONST) {
Roman Zippeleea83d82008-05-01 04:34:33 -0700336 time_constant = txc->constant;
337 if (!(time_status & STA_NANO))
338 time_constant += 4;
339 time_constant = min(time_constant, (long)MAXTC);
340 time_constant = max(time_constant, 0l);
john stultz4c7ee8d2006-09-30 23:28:22 -0700341 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700342
Roman Zippelee9851b2008-05-01 04:34:32 -0700343 if (txc->modes & ADJ_OFFSET) {
344 if (txc->modes == ADJ_OFFSET_SINGLESHOT)
345 /* adjtime() is independent from ntp_adjtime() */
346 time_adjust = txc->offset;
347 else
348 ntp_update_offset(txc->offset);
john stultz4c7ee8d2006-09-30 23:28:22 -0700349 }
Roman Zippelee9851b2008-05-01 04:34:32 -0700350 if (txc->modes & ADJ_TICK)
351 tick_usec = txc->tick;
john stultz4c7ee8d2006-09-30 23:28:22 -0700352
Roman Zippelee9851b2008-05-01 04:34:32 -0700353 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
354 ntp_update_frequency();
355 }
Roman Zippeleea83d82008-05-01 04:34:33 -0700356
357 result = time_state; /* mostly `TIME_OK' */
Roman Zippelee9851b2008-05-01 04:34:32 -0700358 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
john stultz4c7ee8d2006-09-30 23:28:22 -0700359 result = TIME_ERROR;
360
John Stultz52bfb362007-11-26 20:42:19 +0100361 if ((txc->modes == ADJ_OFFSET_SINGLESHOT) ||
Roman Zippelee9851b2008-05-01 04:34:32 -0700362 (txc->modes == ADJ_OFFSET_SS_READ))
john stultzd62ac212007-03-26 21:32:26 -0800363 txc->offset = save_adjust;
Roman Zippeleea83d82008-05-01 04:34:33 -0700364 else {
Roman Zippel9f14f662008-05-01 04:34:36 -0700365 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
366 TICK_LENGTH_SHIFT);
Roman Zippeleea83d82008-05-01 04:34:33 -0700367 if (!(time_status & STA_NANO))
368 txc->offset /= NSEC_PER_USEC;
369 }
Roman Zippel074b3b82008-05-01 04:34:34 -0700370 txc->freq = shift_right((s32)(time_freq >> PPM_SCALE_INV_SHIFT) *
371 (s64)PPM_SCALE_INV,
372 TICK_LENGTH_SHIFT);
john stultz4c7ee8d2006-09-30 23:28:22 -0700373 txc->maxerror = time_maxerror;
374 txc->esterror = time_esterror;
375 txc->status = time_status;
376 txc->constant = time_constant;
Adrian Bunk70bc42f2006-09-30 23:28:29 -0700377 txc->precision = 1;
Roman Zippel074b3b82008-05-01 04:34:34 -0700378 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
john stultz4c7ee8d2006-09-30 23:28:22 -0700379 txc->tick = tick_usec;
380
381 /* PPS is not implemented, so these are zero */
382 txc->ppsfreq = 0;
383 txc->jitter = 0;
384 txc->shift = 0;
385 txc->stabil = 0;
386 txc->jitcnt = 0;
387 txc->calcnt = 0;
388 txc->errcnt = 0;
389 txc->stbcnt = 0;
390 write_sequnlock_irq(&xtime_lock);
Roman Zippelee9851b2008-05-01 04:34:32 -0700391
Roman Zippeleea83d82008-05-01 04:34:33 -0700392 getnstimeofday(&ts);
393 txc->time.tv_sec = ts.tv_sec;
394 txc->time.tv_usec = ts.tv_nsec;
395 if (!(time_status & STA_NANO))
396 txc->time.tv_usec /= NSEC_PER_USEC;
Roman Zippelee9851b2008-05-01 04:34:32 -0700397
Thomas Gleixner82644452007-07-21 04:37:37 -0700398 notify_cmos_timer();
Roman Zippelee9851b2008-05-01 04:34:32 -0700399
400 return result;
john stultz4c7ee8d2006-09-30 23:28:22 -0700401}
Roman Zippel10a398d2008-03-04 15:14:26 -0800402
403static int __init ntp_tick_adj_setup(char *str)
404{
405 ntp_tick_adj = simple_strtol(str, NULL, 0);
406 return 1;
407}
408
409__setup("ntp_tick_adj=", ntp_tick_adj_setup);