blob: a3fe7ef2d83b55b24c9deb3b48384b9a7c5c41bf [file] [log] [blame]
john stultz4c7ee8d2006-09-30 23:28:22 -07001/*
john stultz4c7ee8d2006-09-30 23:28:22 -07002 * NTP state machine interfaces and logic.
3 *
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 */
Alexey Dobriyanaa0ac362007-07-15 23:40:39 -07008#include <linux/capability.h>
Roman Zippel7dffa3c2008-05-01 04:34:41 -07009#include <linux/clocksource.h>
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -070010#include <linux/workqueue.h>
Ingo Molnar53bbfa92008-02-20 07:58:42 +010011#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>
john stultz4c7ee8d2006-09-30 23:28:22 -070017
Roman Zippelb0ee7552006-09-30 23:28:22 -070018/*
Ingo Molnar53bbfa92008-02-20 07:58:42 +010019 * NTP timekeeping variables:
Roman Zippelb0ee7552006-09-30 23:28:22 -070020 */
Roman Zippelb0ee7552006-09-30 23:28:22 -070021
Ingo Molnar53bbfa92008-02-20 07:58:42 +010022/* USER_HZ period (usecs): */
23unsigned long tick_usec = TICK_USEC;
Roman Zippel7dffa3c2008-05-01 04:34:41 -070024
Ingo Molnar53bbfa92008-02-20 07:58:42 +010025/* ACTHZ period (nsecs): */
26unsigned long tick_nsec;
27
28u64 tick_length;
29static u64 tick_length_base;
30
31static struct hrtimer leap_timer;
32
Ingo Molnarbbd12672009-02-22 12:11:11 +010033#define MAX_TICKADJ 500LL /* usecs */
Ingo Molnar53bbfa92008-02-20 07:58:42 +010034#define MAX_TICKADJ_SCALED \
Ingo Molnarbbd12672009-02-22 12:11:11 +010035 (((MAX_TICKADJ * NSEC_PER_USEC) << NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
john stultz4c7ee8d2006-09-30 23:28:22 -070036
37/*
38 * phase-lock loop variables
39 */
Ingo Molnar53bbfa92008-02-20 07:58:42 +010040
41/*
42 * clock synchronization status
43 *
44 * (TIME_ERROR prevents overwriting the CMOS clock)
45 */
46static int time_state = TIME_OK;
47
48/* clock status bits: */
49int time_status = STA_UNSYNC;
50
51/* TAI offset (secs): */
52static long time_tai;
53
54/* time adjustment (nsecs): */
55static s64 time_offset;
56
57/* pll time constant: */
58static long time_constant = 2;
59
60/* maximum error (usecs): */
61long time_maxerror = NTP_PHASE_LIMIT;
62
63/* estimated error (usecs): */
64long time_esterror = NTP_PHASE_LIMIT;
65
66/* frequency offset (scaled nsecs/secs): */
67static s64 time_freq;
68
69/* time at last adjustment (secs): */
70static long time_reftime;
71
72long time_adjust;
73
Ingo Molnar069569e2009-02-22 16:03:37 +010074/* constant (boot-param configurable) NTP tick adjustment (upscaled) */
75static s64 ntp_tick_adj;
Ingo Molnar53bbfa92008-02-20 07:58:42 +010076
77/*
78 * NTP methods:
79 */
john stultz4c7ee8d2006-09-30 23:28:22 -070080
Ingo Molnar9ce616a2009-02-22 12:42:59 +010081/*
82 * Update (tick_length, tick_length_base, tick_nsec), based
83 * on (tick_usec, ntp_tick_adj, time_freq):
84 */
Adrian Bunk70bc42f2006-09-30 23:28:29 -070085static void ntp_update_frequency(void)
86{
Ingo Molnar9ce616a2009-02-22 12:42:59 +010087 u64 second_length;
Ingo Molnarbc26c312009-02-22 12:17:36 +010088 u64 new_base;
Adrian Bunk70bc42f2006-09-30 23:28:29 -070089
Ingo Molnar9ce616a2009-02-22 12:42:59 +010090 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
91 << NTP_SCALE_SHIFT;
92
Ingo Molnar069569e2009-02-22 16:03:37 +010093 second_length += ntp_tick_adj;
Ingo Molnar9ce616a2009-02-22 12:42:59 +010094 second_length += time_freq;
95
Ingo Molnar9ce616a2009-02-22 12:42:59 +010096 tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
Ingo Molnarbc26c312009-02-22 12:17:36 +010097 new_base = div_u64(second_length, NTP_INTERVAL_FREQ);
john stultzfdcedf72009-02-18 16:02:22 -080098
99 /*
100 * Don't wait for the next second_overflow, apply
Ingo Molnarbc26c312009-02-22 12:17:36 +0100101 * the change to the tick length immediately:
john stultzfdcedf72009-02-18 16:02:22 -0800102 */
Ingo Molnarbc26c312009-02-22 12:17:36 +0100103 tick_length += new_base - tick_length_base;
104 tick_length_base = new_base;
Adrian Bunk70bc42f2006-09-30 23:28:29 -0700105}
106
Ingo Molnar478b7aa2009-02-22 13:22:23 +0100107static inline s64 ntp_update_offset_fll(s64 offset64, long secs)
Ingo Molnarf9398902009-02-22 12:57:49 +0100108{
109 time_status &= ~STA_MODE;
110
111 if (secs < MINSEC)
Ingo Molnar478b7aa2009-02-22 13:22:23 +0100112 return 0;
Ingo Molnarf9398902009-02-22 12:57:49 +0100113
114 if (!(time_status & STA_FLL) && (secs <= MAXSEC))
Ingo Molnar478b7aa2009-02-22 13:22:23 +0100115 return 0;
Ingo Molnarf9398902009-02-22 12:57:49 +0100116
Ingo Molnarf9398902009-02-22 12:57:49 +0100117 time_status |= STA_MODE;
118
Ingo Molnar478b7aa2009-02-22 13:22:23 +0100119 return div_s64(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
Ingo Molnarf9398902009-02-22 12:57:49 +0100120}
121
Roman Zippelee9851b2008-05-01 04:34:32 -0700122static void ntp_update_offset(long offset)
123{
Roman Zippelee9851b2008-05-01 04:34:32 -0700124 s64 freq_adj;
Ingo Molnarf9398902009-02-22 12:57:49 +0100125 s64 offset64;
126 long secs;
Roman Zippelee9851b2008-05-01 04:34:32 -0700127
128 if (!(time_status & STA_PLL))
129 return;
130
Roman Zippeleea83d82008-05-01 04:34:33 -0700131 if (!(time_status & STA_NANO))
Roman Zippel9f14f662008-05-01 04:34:36 -0700132 offset *= NSEC_PER_USEC;
Roman Zippelee9851b2008-05-01 04:34:32 -0700133
134 /*
135 * Scale the phase adjustment and
136 * clamp to the operating range.
137 */
Roman Zippel9f14f662008-05-01 04:34:36 -0700138 offset = min(offset, MAXPHASE);
139 offset = max(offset, -MAXPHASE);
Roman Zippelee9851b2008-05-01 04:34:32 -0700140
141 /*
142 * Select how the frequency is to be controlled
143 * and in which mode (PLL or FLL).
144 */
Ingo Molnarf9398902009-02-22 12:57:49 +0100145 secs = xtime.tv_sec - time_reftime;
Ingo Molnar10dd31a2009-02-22 13:38:40 +0100146 if (unlikely(time_status & STA_FREQHOLD))
Ingo Molnarc7986ac2009-02-22 13:29:09 +0100147 secs = 0;
148
Roman Zippelee9851b2008-05-01 04:34:32 -0700149 time_reftime = xtime.tv_sec;
150
Ingo Molnarf9398902009-02-22 12:57:49 +0100151 offset64 = offset;
152 freq_adj = (offset64 * secs) <<
153 (NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant));
Roman Zippel9f14f662008-05-01 04:34:36 -0700154
Ingo Molnar478b7aa2009-02-22 13:22:23 +0100155 freq_adj += ntp_update_offset_fll(offset64, secs);
Ingo Molnarf9398902009-02-22 12:57:49 +0100156
157 freq_adj = min(freq_adj + time_freq, MAXFREQ_SCALED);
158
159 time_freq = max(freq_adj, -MAXFREQ_SCALED);
160
161 time_offset = div_s64(offset64 << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
Roman Zippelee9851b2008-05-01 04:34:32 -0700162}
163
Roman Zippelb0ee7552006-09-30 23:28:22 -0700164/**
165 * ntp_clear - Clears the NTP state variables
166 *
167 * Must be called while holding a write on the xtime_lock
168 */
169void ntp_clear(void)
170{
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100171 time_adjust = 0; /* stop active adjtime() */
172 time_status |= STA_UNSYNC;
173 time_maxerror = NTP_PHASE_LIMIT;
174 time_esterror = NTP_PHASE_LIMIT;
Roman Zippelb0ee7552006-09-30 23:28:22 -0700175
176 ntp_update_frequency();
177
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100178 tick_length = tick_length_base;
179 time_offset = 0;
Roman Zippelb0ee7552006-09-30 23:28:22 -0700180}
181
john stultz4c7ee8d2006-09-30 23:28:22 -0700182/*
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700183 * Leap second processing. If in leap-insert state at the end of the
184 * day, the system clock is set back one second; if in leap-delete
185 * state, the system clock is set ahead one second.
186 */
187static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
188{
189 enum hrtimer_restart res = HRTIMER_NORESTART;
190
Peter Zijlstraca109492008-11-25 12:43:51 +0100191 write_seqlock(&xtime_lock);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700192
193 switch (time_state) {
194 case TIME_OK:
195 break;
196 case TIME_INS:
197 xtime.tv_sec--;
198 wall_to_monotonic.tv_sec++;
199 time_state = TIME_OOP;
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100200 printk(KERN_NOTICE
201 "Clock: inserting leap second 23:59:60 UTC\n");
Arjan van de Vencc584b22008-09-01 15:02:30 -0700202 hrtimer_add_expires_ns(&leap_timer, NSEC_PER_SEC);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700203 res = HRTIMER_RESTART;
204 break;
205 case TIME_DEL:
206 xtime.tv_sec++;
207 time_tai--;
208 wall_to_monotonic.tv_sec--;
209 time_state = TIME_WAIT;
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100210 printk(KERN_NOTICE
211 "Clock: deleting leap second 23:59:59 UTC\n");
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700212 break;
213 case TIME_OOP:
214 time_tai++;
215 time_state = TIME_WAIT;
216 /* fall through */
217 case TIME_WAIT:
218 if (!(time_status & (STA_INS | STA_DEL)))
219 time_state = TIME_OK;
220 break;
221 }
222 update_vsyscall(&xtime, clock);
223
Peter Zijlstraca109492008-11-25 12:43:51 +0100224 write_sequnlock(&xtime_lock);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700225
226 return res;
227}
228
229/*
john stultz4c7ee8d2006-09-30 23:28:22 -0700230 * this routine handles the overflow of the microsecond field
231 *
232 * The tricky bits of code to handle the accurate clock support
233 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
234 * They were originally developed for SUN and DEC kernels.
235 * All the kudos should go to Dave for this stuff.
236 */
237void second_overflow(void)
238{
Roman Zippel9f14f662008-05-01 04:34:36 -0700239 s64 time_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -0700240
241 /* Bump the maxerror field */
Roman Zippel074b3b82008-05-01 04:34:34 -0700242 time_maxerror += MAXFREQ / NSEC_PER_USEC;
john stultz4c7ee8d2006-09-30 23:28:22 -0700243 if (time_maxerror > NTP_PHASE_LIMIT) {
244 time_maxerror = NTP_PHASE_LIMIT;
245 time_status |= STA_UNSYNC;
246 }
247
248 /*
Roman Zippelf1992392006-09-30 23:28:28 -0700249 * Compute the phase adjustment for the next second. The offset is
250 * reduced by a fixed factor times the time constant.
john stultz4c7ee8d2006-09-30 23:28:22 -0700251 */
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100252 tick_length = tick_length_base;
253 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
254 time_offset -= time_adj;
255 tick_length += time_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -0700256
Ingo Molnar3c972c22009-02-22 12:06:57 +0100257 if (!time_adjust)
258 return;
259
260 if (time_adjust > MAX_TICKADJ) {
261 time_adjust -= MAX_TICKADJ;
262 tick_length += MAX_TICKADJ_SCALED;
263 return;
john stultz4c7ee8d2006-09-30 23:28:22 -0700264 }
Ingo Molnar3c972c22009-02-22 12:06:57 +0100265
266 if (time_adjust < -MAX_TICKADJ) {
267 time_adjust += MAX_TICKADJ;
268 tick_length -= MAX_TICKADJ_SCALED;
269 return;
270 }
271
272 tick_length += (s64)(time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ)
273 << NTP_SCALE_SHIFT;
274 time_adjust = 0;
john stultz4c7ee8d2006-09-30 23:28:22 -0700275}
276
Thomas Gleixner82644452007-07-21 04:37:37 -0700277#ifdef CONFIG_GENERIC_CMOS_UPDATE
john stultz4c7ee8d2006-09-30 23:28:22 -0700278
Thomas Gleixner82644452007-07-21 04:37:37 -0700279/* Disable the cmos update - used by virtualization and embedded */
280int no_sync_cmos_clock __read_mostly;
281
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700282static void sync_cmos_clock(struct work_struct *work);
Thomas Gleixner82644452007-07-21 04:37:37 -0700283
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700284static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
Thomas Gleixner82644452007-07-21 04:37:37 -0700285
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700286static void sync_cmos_clock(struct work_struct *work)
john stultz4c7ee8d2006-09-30 23:28:22 -0700287{
Thomas Gleixner82644452007-07-21 04:37:37 -0700288 struct timespec now, next;
289 int fail = 1;
290
291 /*
292 * If we have an externally synchronized Linux clock, then update
293 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
294 * called as close as possible to 500 ms before the new second starts.
295 * This code is run on a timer. If the clock is set, that timer
296 * may not expire at the correct time. Thus, we adjust...
297 */
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100298 if (!ntp_synced()) {
Thomas Gleixner82644452007-07-21 04:37:37 -0700299 /*
300 * Not synced, exit, do not restart a timer (if one is
301 * running, let it run out).
302 */
303 return;
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100304 }
Thomas Gleixner82644452007-07-21 04:37:37 -0700305
306 getnstimeofday(&now);
David P. Reedfa6a1a52007-11-14 17:49:21 -0500307 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
Thomas Gleixner82644452007-07-21 04:37:37 -0700308 fail = update_persistent_clock(now);
309
Maciej W. Rozycki4ff4b9e2008-09-05 14:05:31 -0700310 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2);
Thomas Gleixner82644452007-07-21 04:37:37 -0700311 if (next.tv_nsec <= 0)
312 next.tv_nsec += NSEC_PER_SEC;
313
314 if (!fail)
315 next.tv_sec = 659;
316 else
317 next.tv_sec = 0;
318
319 if (next.tv_nsec >= NSEC_PER_SEC) {
320 next.tv_sec++;
321 next.tv_nsec -= NSEC_PER_SEC;
322 }
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700323 schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next));
john stultz4c7ee8d2006-09-30 23:28:22 -0700324}
325
Thomas Gleixner82644452007-07-21 04:37:37 -0700326static void notify_cmos_timer(void)
327{
Tony Breeds298a5df2007-09-11 15:24:03 -0700328 if (!no_sync_cmos_clock)
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700329 schedule_delayed_work(&sync_cmos_work, 0);
Thomas Gleixner82644452007-07-21 04:37:37 -0700330}
331
332#else
333static inline void notify_cmos_timer(void) { }
334#endif
335
Ingo Molnare9629162009-02-22 15:35:18 +0100336/*
337 * Start the leap seconds timer:
338 */
339static inline void ntp_start_leap_timer(struct timespec *ts)
340{
341 long now = ts->tv_sec;
342
343 if (time_status & STA_INS) {
344 time_state = TIME_INS;
345 now += 86400 - now % 86400;
346 hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS);
347
348 return;
349 }
350
351 if (time_status & STA_DEL) {
352 time_state = TIME_DEL;
353 now += 86400 - (now + 1) % 86400;
354 hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS);
355 }
356}
Ingo Molnar80f225712009-02-22 15:15:32 +0100357
358/*
359 * Propagate a new txc->status value into the NTP state:
360 */
361static inline void process_adj_status(struct timex *txc, struct timespec *ts)
362{
Ingo Molnar80f225712009-02-22 15:15:32 +0100363 if ((time_status & STA_PLL) && !(txc->status & STA_PLL)) {
364 time_state = TIME_OK;
365 time_status = STA_UNSYNC;
366 }
367 /* only set allowed bits */
368 time_status &= STA_RONLY;
369
370 /*
371 * If we turn on PLL adjustments then reset the
372 * reference time to current time.
373 */
374 if (!(time_status & STA_PLL) && (txc->status & STA_PLL))
375 time_reftime = xtime.tv_sec;
376
377 time_status |= txc->status & ~STA_RONLY;
378
379 switch (time_state) {
380 case TIME_OK:
Ingo Molnare9629162009-02-22 15:35:18 +0100381 ntp_start_leap_timer(ts);
Ingo Molnar80f225712009-02-22 15:15:32 +0100382 break;
383 case TIME_INS:
384 case TIME_DEL:
385 time_state = TIME_OK;
Ingo Molnare9629162009-02-22 15:35:18 +0100386 ntp_start_leap_timer(ts);
Ingo Molnar80f225712009-02-22 15:15:32 +0100387 case TIME_WAIT:
388 if (!(time_status & (STA_INS | STA_DEL)))
389 time_state = TIME_OK;
390 break;
391 case TIME_OOP:
392 hrtimer_restart(&leap_timer);
393 break;
394 }
395}
396/*
397 * Called with the xtime lock held, so we can access and modify
398 * all the global NTP state:
399 */
400static inline void process_adjtimex_modes(struct timex *txc, struct timespec *ts)
401{
402 if (txc->modes & ADJ_STATUS)
403 process_adj_status(txc, ts);
404
405 if (txc->modes & ADJ_NANO)
406 time_status |= STA_NANO;
Ingo Molnare9629162009-02-22 15:35:18 +0100407
Ingo Molnar80f225712009-02-22 15:15:32 +0100408 if (txc->modes & ADJ_MICRO)
409 time_status &= ~STA_NANO;
410
411 if (txc->modes & ADJ_FREQUENCY) {
Ingo Molnar2b9d1492009-02-22 15:48:43 +0100412 time_freq = txc->freq * PPM_SCALE;
Ingo Molnar80f225712009-02-22 15:15:32 +0100413 time_freq = min(time_freq, MAXFREQ_SCALED);
414 time_freq = max(time_freq, -MAXFREQ_SCALED);
415 }
416
417 if (txc->modes & ADJ_MAXERROR)
418 time_maxerror = txc->maxerror;
Ingo Molnare9629162009-02-22 15:35:18 +0100419
Ingo Molnar80f225712009-02-22 15:15:32 +0100420 if (txc->modes & ADJ_ESTERROR)
421 time_esterror = txc->esterror;
422
423 if (txc->modes & ADJ_TIMECONST) {
424 time_constant = txc->constant;
425 if (!(time_status & STA_NANO))
426 time_constant += 4;
427 time_constant = min(time_constant, (long)MAXTC);
428 time_constant = max(time_constant, 0l);
429 }
430
431 if (txc->modes & ADJ_TAI && txc->constant > 0)
432 time_tai = txc->constant;
433
434 if (txc->modes & ADJ_OFFSET)
435 ntp_update_offset(txc->offset);
Ingo Molnare9629162009-02-22 15:35:18 +0100436
Ingo Molnar80f225712009-02-22 15:15:32 +0100437 if (txc->modes & ADJ_TICK)
438 tick_usec = txc->tick;
439
440 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
441 ntp_update_frequency();
442}
443
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100444/*
445 * adjtimex mainly allows reading (and writing, if superuser) of
john stultz4c7ee8d2006-09-30 23:28:22 -0700446 * kernel time-keeping variables. used by xntpd.
447 */
448int do_adjtimex(struct timex *txc)
449{
Roman Zippeleea83d82008-05-01 04:34:33 -0700450 struct timespec ts;
john stultz4c7ee8d2006-09-30 23:28:22 -0700451 int result;
452
Roman Zippel916c7a82008-08-20 16:46:08 -0700453 /* Validate the data before disabling interrupts */
454 if (txc->modes & ADJ_ADJTIME) {
Roman Zippeleea83d82008-05-01 04:34:33 -0700455 /* singleshot must not be used with any other mode bits */
Roman Zippel916c7a82008-08-20 16:46:08 -0700456 if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
john stultz4c7ee8d2006-09-30 23:28:22 -0700457 return -EINVAL;
Roman Zippel916c7a82008-08-20 16:46:08 -0700458 if (!(txc->modes & ADJ_OFFSET_READONLY) &&
459 !capable(CAP_SYS_TIME))
460 return -EPERM;
461 } else {
462 /* In order to modify anything, you gotta be super-user! */
463 if (txc->modes && !capable(CAP_SYS_TIME))
464 return -EPERM;
465
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100466 /*
467 * if the quartz is off by more than 10% then
468 * something is VERY wrong!
469 */
Roman Zippel916c7a82008-08-20 16:46:08 -0700470 if (txc->modes & ADJ_TICK &&
471 (txc->tick < 900000/USER_HZ ||
472 txc->tick > 1100000/USER_HZ))
Ingo Molnare9629162009-02-22 15:35:18 +0100473 return -EINVAL;
Roman Zippel916c7a82008-08-20 16:46:08 -0700474
475 if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
476 hrtimer_cancel(&leap_timer);
John Stultz52bfb362007-11-26 20:42:19 +0100477 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700478
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700479 getnstimeofday(&ts);
480
john stultz4c7ee8d2006-09-30 23:28:22 -0700481 write_seqlock_irq(&xtime_lock);
john stultz4c7ee8d2006-09-30 23:28:22 -0700482
Roman Zippel916c7a82008-08-20 16:46:08 -0700483 if (txc->modes & ADJ_ADJTIME) {
484 long save_adjust = time_adjust;
485
486 if (!(txc->modes & ADJ_OFFSET_READONLY)) {
487 /* adjtime() is independent from ntp_adjtime() */
488 time_adjust = txc->offset;
489 ntp_update_frequency();
490 }
491 txc->offset = save_adjust;
Ingo Molnare9629162009-02-22 15:35:18 +0100492 } else {
493
494 /* If there are input parameters, then process them: */
495 if (txc->modes)
496 process_adjtimex_modes(txc, &ts);
497
498 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
499 NTP_SCALE_SHIFT);
500 if (!(time_status & STA_NANO))
501 txc->offset /= NSEC_PER_USEC;
Roman Zippel916c7a82008-08-20 16:46:08 -0700502 }
Roman Zippel916c7a82008-08-20 16:46:08 -0700503
Roman Zippeleea83d82008-05-01 04:34:33 -0700504 result = time_state; /* mostly `TIME_OK' */
Roman Zippelee9851b2008-05-01 04:34:32 -0700505 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
john stultz4c7ee8d2006-09-30 23:28:22 -0700506 result = TIME_ERROR;
507
Roman Zippeld40e9442008-09-22 14:42:44 -0700508 txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
Ingo Molnar2b9d1492009-02-22 15:48:43 +0100509 PPM_SCALE_INV, NTP_SCALE_SHIFT);
john stultz4c7ee8d2006-09-30 23:28:22 -0700510 txc->maxerror = time_maxerror;
511 txc->esterror = time_esterror;
512 txc->status = time_status;
513 txc->constant = time_constant;
Adrian Bunk70bc42f2006-09-30 23:28:29 -0700514 txc->precision = 1;
Roman Zippel074b3b82008-05-01 04:34:34 -0700515 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
john stultz4c7ee8d2006-09-30 23:28:22 -0700516 txc->tick = tick_usec;
Roman Zippel153b5d02008-05-01 04:34:37 -0700517 txc->tai = time_tai;
john stultz4c7ee8d2006-09-30 23:28:22 -0700518
519 /* PPS is not implemented, so these are zero */
520 txc->ppsfreq = 0;
521 txc->jitter = 0;
522 txc->shift = 0;
523 txc->stabil = 0;
524 txc->jitcnt = 0;
525 txc->calcnt = 0;
526 txc->errcnt = 0;
527 txc->stbcnt = 0;
Ingo Molnare9629162009-02-22 15:35:18 +0100528
john stultz4c7ee8d2006-09-30 23:28:22 -0700529 write_sequnlock_irq(&xtime_lock);
Roman Zippelee9851b2008-05-01 04:34:32 -0700530
Roman Zippeleea83d82008-05-01 04:34:33 -0700531 txc->time.tv_sec = ts.tv_sec;
532 txc->time.tv_usec = ts.tv_nsec;
533 if (!(time_status & STA_NANO))
534 txc->time.tv_usec /= NSEC_PER_USEC;
Roman Zippelee9851b2008-05-01 04:34:32 -0700535
Thomas Gleixner82644452007-07-21 04:37:37 -0700536 notify_cmos_timer();
Roman Zippelee9851b2008-05-01 04:34:32 -0700537
538 return result;
john stultz4c7ee8d2006-09-30 23:28:22 -0700539}
Roman Zippel10a398d2008-03-04 15:14:26 -0800540
541static int __init ntp_tick_adj_setup(char *str)
542{
543 ntp_tick_adj = simple_strtol(str, NULL, 0);
Ingo Molnar069569e2009-02-22 16:03:37 +0100544 ntp_tick_adj <<= NTP_SCALE_SHIFT;
545
Roman Zippel10a398d2008-03-04 15:14:26 -0800546 return 1;
547}
548
549__setup("ntp_tick_adj=", ntp_tick_adj_setup);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700550
551void __init ntp_init(void)
552{
553 ntp_clear();
554 hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
555 leap_timer.function = ntp_leap_second;
556}