blob: f1abad738579998cd87dea6fc21b52e92979bf64 [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
Roman Zippelee9851b2008-05-01 04:34:32 -0700106static void ntp_update_offset(long offset)
107{
108 long mtemp;
109 s64 freq_adj;
110
111 if (!(time_status & STA_PLL))
112 return;
113
Roman Zippeleea83d82008-05-01 04:34:33 -0700114 if (!(time_status & STA_NANO))
Roman Zippel9f14f662008-05-01 04:34:36 -0700115 offset *= NSEC_PER_USEC;
Roman Zippelee9851b2008-05-01 04:34:32 -0700116
117 /*
118 * Scale the phase adjustment and
119 * clamp to the operating range.
120 */
Roman Zippel9f14f662008-05-01 04:34:36 -0700121 offset = min(offset, MAXPHASE);
122 offset = max(offset, -MAXPHASE);
Roman Zippelee9851b2008-05-01 04:34:32 -0700123
124 /*
125 * Select how the frequency is to be controlled
126 * and in which mode (PLL or FLL).
127 */
128 if (time_status & STA_FREQHOLD || time_reftime == 0)
129 time_reftime = xtime.tv_sec;
130 mtemp = xtime.tv_sec - time_reftime;
131 time_reftime = xtime.tv_sec;
132
Roman Zippel9f14f662008-05-01 04:34:36 -0700133 freq_adj = (s64)offset * mtemp;
Roman Zippel7fc5c782008-05-01 04:34:38 -0700134 freq_adj <<= NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant);
Roman Zippeleea83d82008-05-01 04:34:33 -0700135 time_status &= ~STA_MODE;
136 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
Roman Zippel7fc5c782008-05-01 04:34:38 -0700137 freq_adj += div_s64((s64)offset << (NTP_SCALE_SHIFT - SHIFT_FLL),
Roman Zippel074b3b82008-05-01 04:34:34 -0700138 mtemp);
Roman Zippeleea83d82008-05-01 04:34:33 -0700139 time_status |= STA_MODE;
140 }
Roman Zippelee9851b2008-05-01 04:34:32 -0700141 freq_adj += time_freq;
Roman Zippel074b3b82008-05-01 04:34:34 -0700142 freq_adj = min(freq_adj, MAXFREQ_SCALED);
143 time_freq = max(freq_adj, -MAXFREQ_SCALED);
Roman Zippel9f14f662008-05-01 04:34:36 -0700144
Roman Zippel7fc5c782008-05-01 04:34:38 -0700145 time_offset = div_s64((s64)offset << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
Roman Zippelee9851b2008-05-01 04:34:32 -0700146}
147
Roman Zippelb0ee7552006-09-30 23:28:22 -0700148/**
149 * ntp_clear - Clears the NTP state variables
150 *
151 * Must be called while holding a write on the xtime_lock
152 */
153void ntp_clear(void)
154{
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100155 time_adjust = 0; /* stop active adjtime() */
156 time_status |= STA_UNSYNC;
157 time_maxerror = NTP_PHASE_LIMIT;
158 time_esterror = NTP_PHASE_LIMIT;
Roman Zippelb0ee7552006-09-30 23:28:22 -0700159
160 ntp_update_frequency();
161
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100162 tick_length = tick_length_base;
163 time_offset = 0;
Roman Zippelb0ee7552006-09-30 23:28:22 -0700164}
165
john stultz4c7ee8d2006-09-30 23:28:22 -0700166/*
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700167 * Leap second processing. If in leap-insert state at the end of the
168 * day, the system clock is set back one second; if in leap-delete
169 * state, the system clock is set ahead one second.
170 */
171static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
172{
173 enum hrtimer_restart res = HRTIMER_NORESTART;
174
Peter Zijlstraca109492008-11-25 12:43:51 +0100175 write_seqlock(&xtime_lock);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700176
177 switch (time_state) {
178 case TIME_OK:
179 break;
180 case TIME_INS:
181 xtime.tv_sec--;
182 wall_to_monotonic.tv_sec++;
183 time_state = TIME_OOP;
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100184 printk(KERN_NOTICE
185 "Clock: inserting leap second 23:59:60 UTC\n");
Arjan van de Vencc584b22008-09-01 15:02:30 -0700186 hrtimer_add_expires_ns(&leap_timer, NSEC_PER_SEC);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700187 res = HRTIMER_RESTART;
188 break;
189 case TIME_DEL:
190 xtime.tv_sec++;
191 time_tai--;
192 wall_to_monotonic.tv_sec--;
193 time_state = TIME_WAIT;
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100194 printk(KERN_NOTICE
195 "Clock: deleting leap second 23:59:59 UTC\n");
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700196 break;
197 case TIME_OOP:
198 time_tai++;
199 time_state = TIME_WAIT;
200 /* fall through */
201 case TIME_WAIT:
202 if (!(time_status & (STA_INS | STA_DEL)))
203 time_state = TIME_OK;
204 break;
205 }
206 update_vsyscall(&xtime, clock);
207
Peter Zijlstraca109492008-11-25 12:43:51 +0100208 write_sequnlock(&xtime_lock);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700209
210 return res;
211}
212
213/*
john stultz4c7ee8d2006-09-30 23:28:22 -0700214 * this routine handles the overflow of the microsecond field
215 *
216 * The tricky bits of code to handle the accurate clock support
217 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
218 * They were originally developed for SUN and DEC kernels.
219 * All the kudos should go to Dave for this stuff.
220 */
221void second_overflow(void)
222{
Roman Zippel9f14f662008-05-01 04:34:36 -0700223 s64 time_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -0700224
225 /* Bump the maxerror field */
Roman Zippel074b3b82008-05-01 04:34:34 -0700226 time_maxerror += MAXFREQ / NSEC_PER_USEC;
john stultz4c7ee8d2006-09-30 23:28:22 -0700227 if (time_maxerror > NTP_PHASE_LIMIT) {
228 time_maxerror = NTP_PHASE_LIMIT;
229 time_status |= STA_UNSYNC;
230 }
231
232 /*
Roman Zippelf1992392006-09-30 23:28:28 -0700233 * Compute the phase adjustment for the next second. The offset is
234 * reduced by a fixed factor times the time constant.
john stultz4c7ee8d2006-09-30 23:28:22 -0700235 */
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100236 tick_length = tick_length_base;
237 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
238 time_offset -= time_adj;
239 tick_length += time_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -0700240
Ingo Molnar3c972c22009-02-22 12:06:57 +0100241 if (!time_adjust)
242 return;
243
244 if (time_adjust > MAX_TICKADJ) {
245 time_adjust -= MAX_TICKADJ;
246 tick_length += MAX_TICKADJ_SCALED;
247 return;
john stultz4c7ee8d2006-09-30 23:28:22 -0700248 }
Ingo Molnar3c972c22009-02-22 12:06:57 +0100249
250 if (time_adjust < -MAX_TICKADJ) {
251 time_adjust += MAX_TICKADJ;
252 tick_length -= MAX_TICKADJ_SCALED;
253 return;
254 }
255
256 tick_length += (s64)(time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ)
257 << NTP_SCALE_SHIFT;
258 time_adjust = 0;
john stultz4c7ee8d2006-09-30 23:28:22 -0700259}
260
Thomas Gleixner82644452007-07-21 04:37:37 -0700261#ifdef CONFIG_GENERIC_CMOS_UPDATE
john stultz4c7ee8d2006-09-30 23:28:22 -0700262
Thomas Gleixner82644452007-07-21 04:37:37 -0700263/* Disable the cmos update - used by virtualization and embedded */
264int no_sync_cmos_clock __read_mostly;
265
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700266static void sync_cmos_clock(struct work_struct *work);
Thomas Gleixner82644452007-07-21 04:37:37 -0700267
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700268static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
Thomas Gleixner82644452007-07-21 04:37:37 -0700269
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700270static void sync_cmos_clock(struct work_struct *work)
john stultz4c7ee8d2006-09-30 23:28:22 -0700271{
Thomas Gleixner82644452007-07-21 04:37:37 -0700272 struct timespec now, next;
273 int fail = 1;
274
275 /*
276 * If we have an externally synchronized Linux clock, then update
277 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
278 * called as close as possible to 500 ms before the new second starts.
279 * This code is run on a timer. If the clock is set, that timer
280 * may not expire at the correct time. Thus, we adjust...
281 */
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100282 if (!ntp_synced()) {
Thomas Gleixner82644452007-07-21 04:37:37 -0700283 /*
284 * Not synced, exit, do not restart a timer (if one is
285 * running, let it run out).
286 */
287 return;
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100288 }
Thomas Gleixner82644452007-07-21 04:37:37 -0700289
290 getnstimeofday(&now);
David P. Reedfa6a1a52007-11-14 17:49:21 -0500291 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
Thomas Gleixner82644452007-07-21 04:37:37 -0700292 fail = update_persistent_clock(now);
293
Maciej W. Rozycki4ff4b9e2008-09-05 14:05:31 -0700294 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2);
Thomas Gleixner82644452007-07-21 04:37:37 -0700295 if (next.tv_nsec <= 0)
296 next.tv_nsec += NSEC_PER_SEC;
297
298 if (!fail)
299 next.tv_sec = 659;
300 else
301 next.tv_sec = 0;
302
303 if (next.tv_nsec >= NSEC_PER_SEC) {
304 next.tv_sec++;
305 next.tv_nsec -= NSEC_PER_SEC;
306 }
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700307 schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next));
john stultz4c7ee8d2006-09-30 23:28:22 -0700308}
309
Thomas Gleixner82644452007-07-21 04:37:37 -0700310static void notify_cmos_timer(void)
311{
Tony Breeds298a5df2007-09-11 15:24:03 -0700312 if (!no_sync_cmos_clock)
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700313 schedule_delayed_work(&sync_cmos_work, 0);
Thomas Gleixner82644452007-07-21 04:37:37 -0700314}
315
316#else
317static inline void notify_cmos_timer(void) { }
318#endif
319
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100320/*
321 * adjtimex mainly allows reading (and writing, if superuser) of
john stultz4c7ee8d2006-09-30 23:28:22 -0700322 * kernel time-keeping variables. used by xntpd.
323 */
324int do_adjtimex(struct timex *txc)
325{
Roman Zippeleea83d82008-05-01 04:34:33 -0700326 struct timespec ts;
john stultz4c7ee8d2006-09-30 23:28:22 -0700327 int result;
328
Roman Zippel916c7a82008-08-20 16:46:08 -0700329 /* Validate the data before disabling interrupts */
330 if (txc->modes & ADJ_ADJTIME) {
Roman Zippeleea83d82008-05-01 04:34:33 -0700331 /* singleshot must not be used with any other mode bits */
Roman Zippel916c7a82008-08-20 16:46:08 -0700332 if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
john stultz4c7ee8d2006-09-30 23:28:22 -0700333 return -EINVAL;
Roman Zippel916c7a82008-08-20 16:46:08 -0700334 if (!(txc->modes & ADJ_OFFSET_READONLY) &&
335 !capable(CAP_SYS_TIME))
336 return -EPERM;
337 } else {
338 /* In order to modify anything, you gotta be super-user! */
339 if (txc->modes && !capable(CAP_SYS_TIME))
340 return -EPERM;
341
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100342 /*
343 * if the quartz is off by more than 10% then
344 * something is VERY wrong!
345 */
Roman Zippel916c7a82008-08-20 16:46:08 -0700346 if (txc->modes & ADJ_TICK &&
347 (txc->tick < 900000/USER_HZ ||
348 txc->tick > 1100000/USER_HZ))
349 return -EINVAL;
350
351 if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
352 hrtimer_cancel(&leap_timer);
John Stultz52bfb362007-11-26 20:42:19 +0100353 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700354
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700355 getnstimeofday(&ts);
356
john stultz4c7ee8d2006-09-30 23:28:22 -0700357 write_seqlock_irq(&xtime_lock);
john stultz4c7ee8d2006-09-30 23:28:22 -0700358
john stultz4c7ee8d2006-09-30 23:28:22 -0700359 /* If there are input parameters, then process them */
Roman Zippel916c7a82008-08-20 16:46:08 -0700360 if (txc->modes & ADJ_ADJTIME) {
361 long save_adjust = time_adjust;
362
363 if (!(txc->modes & ADJ_OFFSET_READONLY)) {
364 /* adjtime() is independent from ntp_adjtime() */
365 time_adjust = txc->offset;
366 ntp_update_frequency();
367 }
368 txc->offset = save_adjust;
369 goto adj_done;
370 }
Roman Zippelee9851b2008-05-01 04:34:32 -0700371 if (txc->modes) {
Roman Zippel916c7a82008-08-20 16:46:08 -0700372 long sec;
373
Roman Zippeleea83d82008-05-01 04:34:33 -0700374 if (txc->modes & ADJ_STATUS) {
375 if ((time_status & STA_PLL) &&
376 !(txc->status & STA_PLL)) {
377 time_state = TIME_OK;
378 time_status = STA_UNSYNC;
379 }
380 /* only set allowed bits */
381 time_status &= STA_RONLY;
382 time_status |= txc->status & ~STA_RONLY;
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700383
384 switch (time_state) {
385 case TIME_OK:
386 start_timer:
387 sec = ts.tv_sec;
388 if (time_status & STA_INS) {
389 time_state = TIME_INS;
390 sec += 86400 - sec % 86400;
391 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
392 } else if (time_status & STA_DEL) {
393 time_state = TIME_DEL;
394 sec += 86400 - (sec + 1) % 86400;
395 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
396 }
397 break;
398 case TIME_INS:
399 case TIME_DEL:
400 time_state = TIME_OK;
401 goto start_timer;
402 break;
403 case TIME_WAIT:
404 if (!(time_status & (STA_INS | STA_DEL)))
405 time_state = TIME_OK;
406 break;
407 case TIME_OOP:
408 hrtimer_restart(&leap_timer);
409 break;
410 }
Roman Zippeleea83d82008-05-01 04:34:33 -0700411 }
412
413 if (txc->modes & ADJ_NANO)
414 time_status |= STA_NANO;
415 if (txc->modes & ADJ_MICRO)
416 time_status &= ~STA_NANO;
john stultz4c7ee8d2006-09-30 23:28:22 -0700417
Roman Zippelee9851b2008-05-01 04:34:32 -0700418 if (txc->modes & ADJ_FREQUENCY) {
Roman Zippel074b3b82008-05-01 04:34:34 -0700419 time_freq = (s64)txc->freq * PPM_SCALE;
420 time_freq = min(time_freq, MAXFREQ_SCALED);
421 time_freq = max(time_freq, -MAXFREQ_SCALED);
john stultz4c7ee8d2006-09-30 23:28:22 -0700422 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700423
Roman Zippeleea83d82008-05-01 04:34:33 -0700424 if (txc->modes & ADJ_MAXERROR)
Roman Zippelee9851b2008-05-01 04:34:32 -0700425 time_maxerror = txc->maxerror;
Roman Zippeleea83d82008-05-01 04:34:33 -0700426 if (txc->modes & ADJ_ESTERROR)
Roman Zippelee9851b2008-05-01 04:34:32 -0700427 time_esterror = txc->esterror;
john stultz4c7ee8d2006-09-30 23:28:22 -0700428
Roman Zippelee9851b2008-05-01 04:34:32 -0700429 if (txc->modes & ADJ_TIMECONST) {
Roman Zippeleea83d82008-05-01 04:34:33 -0700430 time_constant = txc->constant;
431 if (!(time_status & STA_NANO))
432 time_constant += 4;
433 time_constant = min(time_constant, (long)MAXTC);
434 time_constant = max(time_constant, 0l);
john stultz4c7ee8d2006-09-30 23:28:22 -0700435 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700436
Roman Zippel153b5d02008-05-01 04:34:37 -0700437 if (txc->modes & ADJ_TAI && txc->constant > 0)
438 time_tai = txc->constant;
439
Roman Zippel916c7a82008-08-20 16:46:08 -0700440 if (txc->modes & ADJ_OFFSET)
441 ntp_update_offset(txc->offset);
Roman Zippelee9851b2008-05-01 04:34:32 -0700442 if (txc->modes & ADJ_TICK)
443 tick_usec = txc->tick;
john stultz4c7ee8d2006-09-30 23:28:22 -0700444
Roman Zippelee9851b2008-05-01 04:34:32 -0700445 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
446 ntp_update_frequency();
447 }
Roman Zippeleea83d82008-05-01 04:34:33 -0700448
Roman Zippel916c7a82008-08-20 16:46:08 -0700449 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
450 NTP_SCALE_SHIFT);
451 if (!(time_status & STA_NANO))
452 txc->offset /= NSEC_PER_USEC;
453
454adj_done:
Roman Zippeleea83d82008-05-01 04:34:33 -0700455 result = time_state; /* mostly `TIME_OK' */
Roman Zippelee9851b2008-05-01 04:34:32 -0700456 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
john stultz4c7ee8d2006-09-30 23:28:22 -0700457 result = TIME_ERROR;
458
Roman Zippeld40e9442008-09-22 14:42:44 -0700459 txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
460 (s64)PPM_SCALE_INV, NTP_SCALE_SHIFT);
john stultz4c7ee8d2006-09-30 23:28:22 -0700461 txc->maxerror = time_maxerror;
462 txc->esterror = time_esterror;
463 txc->status = time_status;
464 txc->constant = time_constant;
Adrian Bunk70bc42f2006-09-30 23:28:29 -0700465 txc->precision = 1;
Roman Zippel074b3b82008-05-01 04:34:34 -0700466 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
john stultz4c7ee8d2006-09-30 23:28:22 -0700467 txc->tick = tick_usec;
Roman Zippel153b5d02008-05-01 04:34:37 -0700468 txc->tai = time_tai;
john stultz4c7ee8d2006-09-30 23:28:22 -0700469
470 /* PPS is not implemented, so these are zero */
471 txc->ppsfreq = 0;
472 txc->jitter = 0;
473 txc->shift = 0;
474 txc->stabil = 0;
475 txc->jitcnt = 0;
476 txc->calcnt = 0;
477 txc->errcnt = 0;
478 txc->stbcnt = 0;
479 write_sequnlock_irq(&xtime_lock);
Roman Zippelee9851b2008-05-01 04:34:32 -0700480
Roman Zippeleea83d82008-05-01 04:34:33 -0700481 txc->time.tv_sec = ts.tv_sec;
482 txc->time.tv_usec = ts.tv_nsec;
483 if (!(time_status & STA_NANO))
484 txc->time.tv_usec /= NSEC_PER_USEC;
Roman Zippelee9851b2008-05-01 04:34:32 -0700485
Thomas Gleixner82644452007-07-21 04:37:37 -0700486 notify_cmos_timer();
Roman Zippelee9851b2008-05-01 04:34:32 -0700487
488 return result;
john stultz4c7ee8d2006-09-30 23:28:22 -0700489}
Roman Zippel10a398d2008-03-04 15:14:26 -0800490
491static int __init ntp_tick_adj_setup(char *str)
492{
493 ntp_tick_adj = simple_strtol(str, NULL, 0);
494 return 1;
495}
496
497__setup("ntp_tick_adj=", ntp_tick_adj_setup);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700498
499void __init ntp_init(void)
500{
501 ntp_clear();
502 hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
503 leap_timer.function = ntp_leap_second;
504}