blob: aded09be98cce9e2d2aaefc2e9d9674c300df094 [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
74static long ntp_tick_adj;
75
76/*
77 * NTP methods:
78 */
john stultz4c7ee8d2006-09-30 23:28:22 -070079
Ingo Molnar9ce616a2009-02-22 12:42:59 +010080/*
81 * Update (tick_length, tick_length_base, tick_nsec), based
82 * on (tick_usec, ntp_tick_adj, time_freq):
83 */
Adrian Bunk70bc42f2006-09-30 23:28:29 -070084static void ntp_update_frequency(void)
85{
Ingo Molnar9ce616a2009-02-22 12:42:59 +010086 u64 second_length;
Ingo Molnarbc26c312009-02-22 12:17:36 +010087 u64 new_base;
Adrian Bunk70bc42f2006-09-30 23:28:29 -070088
Ingo Molnar9ce616a2009-02-22 12:42:59 +010089 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
90 << NTP_SCALE_SHIFT;
91
92 second_length += (s64)ntp_tick_adj << NTP_SCALE_SHIFT;
93 second_length += time_freq;
94
Ingo Molnar9ce616a2009-02-22 12:42:59 +010095 tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
Ingo Molnarbc26c312009-02-22 12:17:36 +010096 new_base = div_u64(second_length, NTP_INTERVAL_FREQ);
john stultzfdcedf72009-02-18 16:02:22 -080097
98 /*
99 * Don't wait for the next second_overflow, apply
Ingo Molnarbc26c312009-02-22 12:17:36 +0100100 * the change to the tick length immediately:
john stultzfdcedf72009-02-18 16:02:22 -0800101 */
Ingo Molnarbc26c312009-02-22 12:17:36 +0100102 tick_length += new_base - tick_length_base;
103 tick_length_base = new_base;
Adrian Bunk70bc42f2006-09-30 23:28:29 -0700104}
105
Ingo Molnar478b7aa2009-02-22 13:22:23 +0100106static inline s64 ntp_update_offset_fll(s64 offset64, long secs)
Ingo Molnarf9398902009-02-22 12:57:49 +0100107{
108 time_status &= ~STA_MODE;
109
110 if (secs < MINSEC)
Ingo Molnar478b7aa2009-02-22 13:22:23 +0100111 return 0;
Ingo Molnarf9398902009-02-22 12:57:49 +0100112
113 if (!(time_status & STA_FLL) && (secs <= MAXSEC))
Ingo Molnar478b7aa2009-02-22 13:22:23 +0100114 return 0;
Ingo Molnarf9398902009-02-22 12:57:49 +0100115
Ingo Molnarf9398902009-02-22 12:57:49 +0100116 time_status |= STA_MODE;
117
Ingo Molnar478b7aa2009-02-22 13:22:23 +0100118 return div_s64(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
Ingo Molnarf9398902009-02-22 12:57:49 +0100119}
120
Roman Zippelee9851b2008-05-01 04:34:32 -0700121static void ntp_update_offset(long offset)
122{
Roman Zippelee9851b2008-05-01 04:34:32 -0700123 s64 freq_adj;
Ingo Molnarf9398902009-02-22 12:57:49 +0100124 s64 offset64;
125 long secs;
Roman Zippelee9851b2008-05-01 04:34:32 -0700126
127 if (!(time_status & STA_PLL))
128 return;
129
Roman Zippeleea83d82008-05-01 04:34:33 -0700130 if (!(time_status & STA_NANO))
Roman Zippel9f14f662008-05-01 04:34:36 -0700131 offset *= NSEC_PER_USEC;
Roman Zippelee9851b2008-05-01 04:34:32 -0700132
133 /*
134 * Scale the phase adjustment and
135 * clamp to the operating range.
136 */
Roman Zippel9f14f662008-05-01 04:34:36 -0700137 offset = min(offset, MAXPHASE);
138 offset = max(offset, -MAXPHASE);
Roman Zippelee9851b2008-05-01 04:34:32 -0700139
140 /*
141 * Select how the frequency is to be controlled
142 * and in which mode (PLL or FLL).
143 */
Ingo Molnarf9398902009-02-22 12:57:49 +0100144 secs = xtime.tv_sec - time_reftime;
Ingo Molnar10dd31a2009-02-22 13:38:40 +0100145 if (unlikely(time_status & STA_FREQHOLD))
Ingo Molnarc7986ac2009-02-22 13:29:09 +0100146 secs = 0;
147
Roman Zippelee9851b2008-05-01 04:34:32 -0700148 time_reftime = xtime.tv_sec;
149
Ingo Molnarf9398902009-02-22 12:57:49 +0100150 offset64 = offset;
151 freq_adj = (offset64 * secs) <<
152 (NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant));
Roman Zippel9f14f662008-05-01 04:34:36 -0700153
Ingo Molnar478b7aa2009-02-22 13:22:23 +0100154 freq_adj += ntp_update_offset_fll(offset64, secs);
Ingo Molnarf9398902009-02-22 12:57:49 +0100155
156 freq_adj = min(freq_adj + time_freq, MAXFREQ_SCALED);
157
158 time_freq = max(freq_adj, -MAXFREQ_SCALED);
159
160 time_offset = div_s64(offset64 << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
Roman Zippelee9851b2008-05-01 04:34:32 -0700161}
162
Roman Zippelb0ee7552006-09-30 23:28:22 -0700163/**
164 * ntp_clear - Clears the NTP state variables
165 *
166 * Must be called while holding a write on the xtime_lock
167 */
168void ntp_clear(void)
169{
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100170 time_adjust = 0; /* stop active adjtime() */
171 time_status |= STA_UNSYNC;
172 time_maxerror = NTP_PHASE_LIMIT;
173 time_esterror = NTP_PHASE_LIMIT;
Roman Zippelb0ee7552006-09-30 23:28:22 -0700174
175 ntp_update_frequency();
176
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100177 tick_length = tick_length_base;
178 time_offset = 0;
Roman Zippelb0ee7552006-09-30 23:28:22 -0700179}
180
john stultz4c7ee8d2006-09-30 23:28:22 -0700181/*
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700182 * Leap second processing. If in leap-insert state at the end of the
183 * day, the system clock is set back one second; if in leap-delete
184 * state, the system clock is set ahead one second.
185 */
186static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
187{
188 enum hrtimer_restart res = HRTIMER_NORESTART;
189
Peter Zijlstraca109492008-11-25 12:43:51 +0100190 write_seqlock(&xtime_lock);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700191
192 switch (time_state) {
193 case TIME_OK:
194 break;
195 case TIME_INS:
196 xtime.tv_sec--;
197 wall_to_monotonic.tv_sec++;
198 time_state = TIME_OOP;
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100199 printk(KERN_NOTICE
200 "Clock: inserting leap second 23:59:60 UTC\n");
Arjan van de Vencc584b22008-09-01 15:02:30 -0700201 hrtimer_add_expires_ns(&leap_timer, NSEC_PER_SEC);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700202 res = HRTIMER_RESTART;
203 break;
204 case TIME_DEL:
205 xtime.tv_sec++;
206 time_tai--;
207 wall_to_monotonic.tv_sec--;
208 time_state = TIME_WAIT;
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100209 printk(KERN_NOTICE
210 "Clock: deleting leap second 23:59:59 UTC\n");
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700211 break;
212 case TIME_OOP:
213 time_tai++;
214 time_state = TIME_WAIT;
215 /* fall through */
216 case TIME_WAIT:
217 if (!(time_status & (STA_INS | STA_DEL)))
218 time_state = TIME_OK;
219 break;
220 }
221 update_vsyscall(&xtime, clock);
222
Peter Zijlstraca109492008-11-25 12:43:51 +0100223 write_sequnlock(&xtime_lock);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700224
225 return res;
226}
227
228/*
john stultz4c7ee8d2006-09-30 23:28:22 -0700229 * this routine handles the overflow of the microsecond field
230 *
231 * The tricky bits of code to handle the accurate clock support
232 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
233 * They were originally developed for SUN and DEC kernels.
234 * All the kudos should go to Dave for this stuff.
235 */
236void second_overflow(void)
237{
Roman Zippel9f14f662008-05-01 04:34:36 -0700238 s64 time_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -0700239
240 /* Bump the maxerror field */
Roman Zippel074b3b82008-05-01 04:34:34 -0700241 time_maxerror += MAXFREQ / NSEC_PER_USEC;
john stultz4c7ee8d2006-09-30 23:28:22 -0700242 if (time_maxerror > NTP_PHASE_LIMIT) {
243 time_maxerror = NTP_PHASE_LIMIT;
244 time_status |= STA_UNSYNC;
245 }
246
247 /*
Roman Zippelf1992392006-09-30 23:28:28 -0700248 * Compute the phase adjustment for the next second. The offset is
249 * reduced by a fixed factor times the time constant.
john stultz4c7ee8d2006-09-30 23:28:22 -0700250 */
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100251 tick_length = tick_length_base;
252 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
253 time_offset -= time_adj;
254 tick_length += time_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -0700255
Ingo Molnar3c972c22009-02-22 12:06:57 +0100256 if (!time_adjust)
257 return;
258
259 if (time_adjust > MAX_TICKADJ) {
260 time_adjust -= MAX_TICKADJ;
261 tick_length += MAX_TICKADJ_SCALED;
262 return;
john stultz4c7ee8d2006-09-30 23:28:22 -0700263 }
Ingo Molnar3c972c22009-02-22 12:06:57 +0100264
265 if (time_adjust < -MAX_TICKADJ) {
266 time_adjust += MAX_TICKADJ;
267 tick_length -= MAX_TICKADJ_SCALED;
268 return;
269 }
270
271 tick_length += (s64)(time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ)
272 << NTP_SCALE_SHIFT;
273 time_adjust = 0;
john stultz4c7ee8d2006-09-30 23:28:22 -0700274}
275
Thomas Gleixner82644452007-07-21 04:37:37 -0700276#ifdef CONFIG_GENERIC_CMOS_UPDATE
john stultz4c7ee8d2006-09-30 23:28:22 -0700277
Thomas Gleixner82644452007-07-21 04:37:37 -0700278/* Disable the cmos update - used by virtualization and embedded */
279int no_sync_cmos_clock __read_mostly;
280
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700281static void sync_cmos_clock(struct work_struct *work);
Thomas Gleixner82644452007-07-21 04:37:37 -0700282
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700283static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
Thomas Gleixner82644452007-07-21 04:37:37 -0700284
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700285static void sync_cmos_clock(struct work_struct *work)
john stultz4c7ee8d2006-09-30 23:28:22 -0700286{
Thomas Gleixner82644452007-07-21 04:37:37 -0700287 struct timespec now, next;
288 int fail = 1;
289
290 /*
291 * If we have an externally synchronized Linux clock, then update
292 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
293 * called as close as possible to 500 ms before the new second starts.
294 * This code is run on a timer. If the clock is set, that timer
295 * may not expire at the correct time. Thus, we adjust...
296 */
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100297 if (!ntp_synced()) {
Thomas Gleixner82644452007-07-21 04:37:37 -0700298 /*
299 * Not synced, exit, do not restart a timer (if one is
300 * running, let it run out).
301 */
302 return;
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100303 }
Thomas Gleixner82644452007-07-21 04:37:37 -0700304
305 getnstimeofday(&now);
David P. Reedfa6a1a52007-11-14 17:49:21 -0500306 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
Thomas Gleixner82644452007-07-21 04:37:37 -0700307 fail = update_persistent_clock(now);
308
Maciej W. Rozycki4ff4b9e2008-09-05 14:05:31 -0700309 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2);
Thomas Gleixner82644452007-07-21 04:37:37 -0700310 if (next.tv_nsec <= 0)
311 next.tv_nsec += NSEC_PER_SEC;
312
313 if (!fail)
314 next.tv_sec = 659;
315 else
316 next.tv_sec = 0;
317
318 if (next.tv_nsec >= NSEC_PER_SEC) {
319 next.tv_sec++;
320 next.tv_nsec -= NSEC_PER_SEC;
321 }
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700322 schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next));
john stultz4c7ee8d2006-09-30 23:28:22 -0700323}
324
Thomas Gleixner82644452007-07-21 04:37:37 -0700325static void notify_cmos_timer(void)
326{
Tony Breeds298a5df2007-09-11 15:24:03 -0700327 if (!no_sync_cmos_clock)
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700328 schedule_delayed_work(&sync_cmos_work, 0);
Thomas Gleixner82644452007-07-21 04:37:37 -0700329}
330
331#else
332static inline void notify_cmos_timer(void) { }
333#endif
334
Ingo Molnar80f22572009-02-22 15:15:32 +0100335
336/*
337 * Propagate a new txc->status value into the NTP state:
338 */
339static inline void process_adj_status(struct timex *txc, struct timespec *ts)
340{
341 long now;
342
343 if ((time_status & STA_PLL) && !(txc->status & STA_PLL)) {
344 time_state = TIME_OK;
345 time_status = STA_UNSYNC;
346 }
347 /* only set allowed bits */
348 time_status &= STA_RONLY;
349
350 /*
351 * If we turn on PLL adjustments then reset the
352 * reference time to current time.
353 */
354 if (!(time_status & STA_PLL) && (txc->status & STA_PLL))
355 time_reftime = xtime.tv_sec;
356
357 time_status |= txc->status & ~STA_RONLY;
358
359 switch (time_state) {
360 case TIME_OK:
361 start_timer:
362 now = ts->tv_sec;
363 if (time_status & STA_INS) {
364 time_state = TIME_INS;
365 now += 86400 - now % 86400;
366 hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS);
367 } else if (time_status & STA_DEL) {
368 time_state = TIME_DEL;
369 now += 86400 - (now + 1) % 86400;
370 hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS);
371 }
372 break;
373 case TIME_INS:
374 case TIME_DEL:
375 time_state = TIME_OK;
376 goto start_timer;
377 case TIME_WAIT:
378 if (!(time_status & (STA_INS | STA_DEL)))
379 time_state = TIME_OK;
380 break;
381 case TIME_OOP:
382 hrtimer_restart(&leap_timer);
383 break;
384 }
385}
386/*
387 * Called with the xtime lock held, so we can access and modify
388 * all the global NTP state:
389 */
390static inline void process_adjtimex_modes(struct timex *txc, struct timespec *ts)
391{
392 if (txc->modes & ADJ_STATUS)
393 process_adj_status(txc, ts);
394
395 if (txc->modes & ADJ_NANO)
396 time_status |= STA_NANO;
397 if (txc->modes & ADJ_MICRO)
398 time_status &= ~STA_NANO;
399
400 if (txc->modes & ADJ_FREQUENCY) {
401 time_freq = (s64)txc->freq * PPM_SCALE;
402 time_freq = min(time_freq, MAXFREQ_SCALED);
403 time_freq = max(time_freq, -MAXFREQ_SCALED);
404 }
405
406 if (txc->modes & ADJ_MAXERROR)
407 time_maxerror = txc->maxerror;
408 if (txc->modes & ADJ_ESTERROR)
409 time_esterror = txc->esterror;
410
411 if (txc->modes & ADJ_TIMECONST) {
412 time_constant = txc->constant;
413 if (!(time_status & STA_NANO))
414 time_constant += 4;
415 time_constant = min(time_constant, (long)MAXTC);
416 time_constant = max(time_constant, 0l);
417 }
418
419 if (txc->modes & ADJ_TAI && txc->constant > 0)
420 time_tai = txc->constant;
421
422 if (txc->modes & ADJ_OFFSET)
423 ntp_update_offset(txc->offset);
424 if (txc->modes & ADJ_TICK)
425 tick_usec = txc->tick;
426
427 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
428 ntp_update_frequency();
429}
430
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100431/*
432 * adjtimex mainly allows reading (and writing, if superuser) of
john stultz4c7ee8d2006-09-30 23:28:22 -0700433 * kernel time-keeping variables. used by xntpd.
434 */
435int do_adjtimex(struct timex *txc)
436{
Roman Zippeleea83d82008-05-01 04:34:33 -0700437 struct timespec ts;
john stultz4c7ee8d2006-09-30 23:28:22 -0700438 int result;
439
Roman Zippel916c7a82008-08-20 16:46:08 -0700440 /* Validate the data before disabling interrupts */
441 if (txc->modes & ADJ_ADJTIME) {
Roman Zippeleea83d82008-05-01 04:34:33 -0700442 /* singleshot must not be used with any other mode bits */
Roman Zippel916c7a82008-08-20 16:46:08 -0700443 if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
john stultz4c7ee8d2006-09-30 23:28:22 -0700444 return -EINVAL;
Roman Zippel916c7a82008-08-20 16:46:08 -0700445 if (!(txc->modes & ADJ_OFFSET_READONLY) &&
446 !capable(CAP_SYS_TIME))
447 return -EPERM;
448 } else {
449 /* In order to modify anything, you gotta be super-user! */
450 if (txc->modes && !capable(CAP_SYS_TIME))
451 return -EPERM;
452
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100453 /*
454 * if the quartz is off by more than 10% then
455 * something is VERY wrong!
456 */
Roman Zippel916c7a82008-08-20 16:46:08 -0700457 if (txc->modes & ADJ_TICK &&
458 (txc->tick < 900000/USER_HZ ||
459 txc->tick > 1100000/USER_HZ))
460 return -EINVAL;
461
462 if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
463 hrtimer_cancel(&leap_timer);
John Stultz52bfb362007-11-26 20:42:19 +0100464 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700465
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700466 getnstimeofday(&ts);
467
john stultz4c7ee8d2006-09-30 23:28:22 -0700468 write_seqlock_irq(&xtime_lock);
john stultz4c7ee8d2006-09-30 23:28:22 -0700469
john stultz4c7ee8d2006-09-30 23:28:22 -0700470 /* If there are input parameters, then process them */
Roman Zippel916c7a82008-08-20 16:46:08 -0700471 if (txc->modes & ADJ_ADJTIME) {
472 long save_adjust = time_adjust;
473
474 if (!(txc->modes & ADJ_OFFSET_READONLY)) {
475 /* adjtime() is independent from ntp_adjtime() */
476 time_adjust = txc->offset;
477 ntp_update_frequency();
478 }
479 txc->offset = save_adjust;
480 goto adj_done;
481 }
Roman Zippel916c7a82008-08-20 16:46:08 -0700482
Ingo Molnar80f22572009-02-22 15:15:32 +0100483 /* If there are input parameters, then process them: */
484 if (txc->modes)
485 process_adjtimex_modes(txc, &ts);
Roman Zippeleea83d82008-05-01 04:34:33 -0700486
Roman Zippel916c7a82008-08-20 16:46:08 -0700487 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
488 NTP_SCALE_SHIFT);
489 if (!(time_status & STA_NANO))
490 txc->offset /= NSEC_PER_USEC;
491
492adj_done:
Roman Zippeleea83d82008-05-01 04:34:33 -0700493 result = time_state; /* mostly `TIME_OK' */
Roman Zippelee9851b2008-05-01 04:34:32 -0700494 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
john stultz4c7ee8d2006-09-30 23:28:22 -0700495 result = TIME_ERROR;
496
Roman Zippeld40e9442008-09-22 14:42:44 -0700497 txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
498 (s64)PPM_SCALE_INV, NTP_SCALE_SHIFT);
john stultz4c7ee8d2006-09-30 23:28:22 -0700499 txc->maxerror = time_maxerror;
500 txc->esterror = time_esterror;
501 txc->status = time_status;
502 txc->constant = time_constant;
Adrian Bunk70bc42f2006-09-30 23:28:29 -0700503 txc->precision = 1;
Roman Zippel074b3b82008-05-01 04:34:34 -0700504 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
john stultz4c7ee8d2006-09-30 23:28:22 -0700505 txc->tick = tick_usec;
Roman Zippel153b5d02008-05-01 04:34:37 -0700506 txc->tai = time_tai;
john stultz4c7ee8d2006-09-30 23:28:22 -0700507
508 /* PPS is not implemented, so these are zero */
509 txc->ppsfreq = 0;
510 txc->jitter = 0;
511 txc->shift = 0;
512 txc->stabil = 0;
513 txc->jitcnt = 0;
514 txc->calcnt = 0;
515 txc->errcnt = 0;
516 txc->stbcnt = 0;
517 write_sequnlock_irq(&xtime_lock);
Roman Zippelee9851b2008-05-01 04:34:32 -0700518
Roman Zippeleea83d82008-05-01 04:34:33 -0700519 txc->time.tv_sec = ts.tv_sec;
520 txc->time.tv_usec = ts.tv_nsec;
521 if (!(time_status & STA_NANO))
522 txc->time.tv_usec /= NSEC_PER_USEC;
Roman Zippelee9851b2008-05-01 04:34:32 -0700523
Thomas Gleixner82644452007-07-21 04:37:37 -0700524 notify_cmos_timer();
Roman Zippelee9851b2008-05-01 04:34:32 -0700525
526 return result;
john stultz4c7ee8d2006-09-30 23:28:22 -0700527}
Roman Zippel10a398d2008-03-04 15:14:26 -0800528
529static int __init ntp_tick_adj_setup(char *str)
530{
531 ntp_tick_adj = simple_strtol(str, NULL, 0);
532 return 1;
533}
534
535__setup("ntp_tick_adj=", ntp_tick_adj_setup);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700536
537void __init ntp_init(void)
538{
539 ntp_clear();
540 hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
541 leap_timer.function = ntp_leap_second;
542}