blob: 3479ec48e604ad65f07a943a6af2fa7b83155123 [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
33#define MAX_TICKADJ 500 /* usecs */
34#define MAX_TICKADJ_SCALED \
35 (((u64)(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
Adrian Bunk70bc42f2006-09-30 23:28:29 -070080static void ntp_update_frequency(void)
81{
john stultzfdcedf72009-02-18 16:02:22 -080082 u64 old_tick_length_base = tick_length_base;
john stultzf4304ab2007-02-16 01:27:26 -080083 u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
Roman Zippel7fc5c782008-05-01 04:34:38 -070084 << NTP_SCALE_SHIFT;
85 second_length += (s64)ntp_tick_adj << NTP_SCALE_SHIFT;
Roman Zippel074b3b82008-05-01 04:34:34 -070086 second_length += time_freq;
Adrian Bunk70bc42f2006-09-30 23:28:29 -070087
john stultzf4304ab2007-02-16 01:27:26 -080088 tick_length_base = second_length;
Adrian Bunk70bc42f2006-09-30 23:28:29 -070089
Roman Zippel7fc5c782008-05-01 04:34:38 -070090 tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
Roman Zippel71abb3a2008-05-01 04:34:26 -070091 tick_length_base = div_u64(tick_length_base, NTP_INTERVAL_FREQ);
john stultzfdcedf72009-02-18 16:02:22 -080092
93 /*
94 * Don't wait for the next second_overflow, apply
95 * the change to the tick length immediately
96 */
97 tick_length += tick_length_base - old_tick_length_base;
Adrian Bunk70bc42f2006-09-30 23:28:29 -070098}
99
Roman Zippelee9851b2008-05-01 04:34:32 -0700100static void ntp_update_offset(long offset)
101{
102 long mtemp;
103 s64 freq_adj;
104
105 if (!(time_status & STA_PLL))
106 return;
107
Roman Zippeleea83d82008-05-01 04:34:33 -0700108 if (!(time_status & STA_NANO))
Roman Zippel9f14f662008-05-01 04:34:36 -0700109 offset *= NSEC_PER_USEC;
Roman Zippelee9851b2008-05-01 04:34:32 -0700110
111 /*
112 * Scale the phase adjustment and
113 * clamp to the operating range.
114 */
Roman Zippel9f14f662008-05-01 04:34:36 -0700115 offset = min(offset, MAXPHASE);
116 offset = max(offset, -MAXPHASE);
Roman Zippelee9851b2008-05-01 04:34:32 -0700117
118 /*
119 * Select how the frequency is to be controlled
120 * and in which mode (PLL or FLL).
121 */
122 if (time_status & STA_FREQHOLD || time_reftime == 0)
123 time_reftime = xtime.tv_sec;
124 mtemp = xtime.tv_sec - time_reftime;
125 time_reftime = xtime.tv_sec;
126
Roman Zippel9f14f662008-05-01 04:34:36 -0700127 freq_adj = (s64)offset * mtemp;
Roman Zippel7fc5c782008-05-01 04:34:38 -0700128 freq_adj <<= NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant);
Roman Zippeleea83d82008-05-01 04:34:33 -0700129 time_status &= ~STA_MODE;
130 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
Roman Zippel7fc5c782008-05-01 04:34:38 -0700131 freq_adj += div_s64((s64)offset << (NTP_SCALE_SHIFT - SHIFT_FLL),
Roman Zippel074b3b82008-05-01 04:34:34 -0700132 mtemp);
Roman Zippeleea83d82008-05-01 04:34:33 -0700133 time_status |= STA_MODE;
134 }
Roman Zippelee9851b2008-05-01 04:34:32 -0700135 freq_adj += time_freq;
Roman Zippel074b3b82008-05-01 04:34:34 -0700136 freq_adj = min(freq_adj, MAXFREQ_SCALED);
137 time_freq = max(freq_adj, -MAXFREQ_SCALED);
Roman Zippel9f14f662008-05-01 04:34:36 -0700138
Roman Zippel7fc5c782008-05-01 04:34:38 -0700139 time_offset = div_s64((s64)offset << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
Roman Zippelee9851b2008-05-01 04:34:32 -0700140}
141
Roman Zippelb0ee7552006-09-30 23:28:22 -0700142/**
143 * ntp_clear - Clears the NTP state variables
144 *
145 * Must be called while holding a write on the xtime_lock
146 */
147void ntp_clear(void)
148{
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100149 time_adjust = 0; /* stop active adjtime() */
150 time_status |= STA_UNSYNC;
151 time_maxerror = NTP_PHASE_LIMIT;
152 time_esterror = NTP_PHASE_LIMIT;
Roman Zippelb0ee7552006-09-30 23:28:22 -0700153
154 ntp_update_frequency();
155
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100156 tick_length = tick_length_base;
157 time_offset = 0;
Roman Zippelb0ee7552006-09-30 23:28:22 -0700158}
159
john stultz4c7ee8d2006-09-30 23:28:22 -0700160/*
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700161 * Leap second processing. If in leap-insert state at the end of the
162 * day, the system clock is set back one second; if in leap-delete
163 * state, the system clock is set ahead one second.
164 */
165static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
166{
167 enum hrtimer_restart res = HRTIMER_NORESTART;
168
Peter Zijlstraca109492008-11-25 12:43:51 +0100169 write_seqlock(&xtime_lock);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700170
171 switch (time_state) {
172 case TIME_OK:
173 break;
174 case TIME_INS:
175 xtime.tv_sec--;
176 wall_to_monotonic.tv_sec++;
177 time_state = TIME_OOP;
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100178 printk(KERN_NOTICE
179 "Clock: inserting leap second 23:59:60 UTC\n");
Arjan van de Vencc584b22008-09-01 15:02:30 -0700180 hrtimer_add_expires_ns(&leap_timer, NSEC_PER_SEC);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700181 res = HRTIMER_RESTART;
182 break;
183 case TIME_DEL:
184 xtime.tv_sec++;
185 time_tai--;
186 wall_to_monotonic.tv_sec--;
187 time_state = TIME_WAIT;
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100188 printk(KERN_NOTICE
189 "Clock: deleting leap second 23:59:59 UTC\n");
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700190 break;
191 case TIME_OOP:
192 time_tai++;
193 time_state = TIME_WAIT;
194 /* fall through */
195 case TIME_WAIT:
196 if (!(time_status & (STA_INS | STA_DEL)))
197 time_state = TIME_OK;
198 break;
199 }
200 update_vsyscall(&xtime, clock);
201
Peter Zijlstraca109492008-11-25 12:43:51 +0100202 write_sequnlock(&xtime_lock);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700203
204 return res;
205}
206
207/*
john stultz4c7ee8d2006-09-30 23:28:22 -0700208 * this routine handles the overflow of the microsecond field
209 *
210 * The tricky bits of code to handle the accurate clock support
211 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
212 * They were originally developed for SUN and DEC kernels.
213 * All the kudos should go to Dave for this stuff.
214 */
215void second_overflow(void)
216{
Roman Zippel9f14f662008-05-01 04:34:36 -0700217 s64 time_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -0700218
219 /* Bump the maxerror field */
Roman Zippel074b3b82008-05-01 04:34:34 -0700220 time_maxerror += MAXFREQ / NSEC_PER_USEC;
john stultz4c7ee8d2006-09-30 23:28:22 -0700221 if (time_maxerror > NTP_PHASE_LIMIT) {
222 time_maxerror = NTP_PHASE_LIMIT;
223 time_status |= STA_UNSYNC;
224 }
225
226 /*
Roman Zippelf1992392006-09-30 23:28:28 -0700227 * Compute the phase adjustment for the next second. The offset is
228 * reduced by a fixed factor times the time constant.
john stultz4c7ee8d2006-09-30 23:28:22 -0700229 */
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100230 tick_length = tick_length_base;
231 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
232 time_offset -= time_adj;
233 tick_length += time_adj;
john stultz4c7ee8d2006-09-30 23:28:22 -0700234
Roman Zippel8f807f82006-09-30 23:28:25 -0700235 if (unlikely(time_adjust)) {
236 if (time_adjust > MAX_TICKADJ) {
237 time_adjust -= MAX_TICKADJ;
238 tick_length += MAX_TICKADJ_SCALED;
239 } else if (time_adjust < -MAX_TICKADJ) {
240 time_adjust += MAX_TICKADJ;
241 tick_length -= MAX_TICKADJ_SCALED;
242 } else {
Roman Zippel8f807f82006-09-30 23:28:25 -0700243 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
Roman Zippel7fc5c782008-05-01 04:34:38 -0700244 NTP_INTERVAL_FREQ) << NTP_SCALE_SHIFT;
Jim Houstonbb1d8602006-10-28 10:38:56 -0700245 time_adjust = 0;
Roman Zippel8f807f82006-09-30 23:28:25 -0700246 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700247 }
248}
249
Thomas Gleixner82644452007-07-21 04:37:37 -0700250#ifdef CONFIG_GENERIC_CMOS_UPDATE
john stultz4c7ee8d2006-09-30 23:28:22 -0700251
Thomas Gleixner82644452007-07-21 04:37:37 -0700252/* Disable the cmos update - used by virtualization and embedded */
253int no_sync_cmos_clock __read_mostly;
254
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700255static void sync_cmos_clock(struct work_struct *work);
Thomas Gleixner82644452007-07-21 04:37:37 -0700256
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700257static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
Thomas Gleixner82644452007-07-21 04:37:37 -0700258
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700259static void sync_cmos_clock(struct work_struct *work)
john stultz4c7ee8d2006-09-30 23:28:22 -0700260{
Thomas Gleixner82644452007-07-21 04:37:37 -0700261 struct timespec now, next;
262 int fail = 1;
263
264 /*
265 * If we have an externally synchronized Linux clock, then update
266 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
267 * called as close as possible to 500 ms before the new second starts.
268 * This code is run on a timer. If the clock is set, that timer
269 * may not expire at the correct time. Thus, we adjust...
270 */
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100271 if (!ntp_synced()) {
Thomas Gleixner82644452007-07-21 04:37:37 -0700272 /*
273 * Not synced, exit, do not restart a timer (if one is
274 * running, let it run out).
275 */
276 return;
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100277 }
Thomas Gleixner82644452007-07-21 04:37:37 -0700278
279 getnstimeofday(&now);
David P. Reedfa6a1a52007-11-14 17:49:21 -0500280 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
Thomas Gleixner82644452007-07-21 04:37:37 -0700281 fail = update_persistent_clock(now);
282
Maciej W. Rozycki4ff4b9e2008-09-05 14:05:31 -0700283 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2);
Thomas Gleixner82644452007-07-21 04:37:37 -0700284 if (next.tv_nsec <= 0)
285 next.tv_nsec += NSEC_PER_SEC;
286
287 if (!fail)
288 next.tv_sec = 659;
289 else
290 next.tv_sec = 0;
291
292 if (next.tv_nsec >= NSEC_PER_SEC) {
293 next.tv_sec++;
294 next.tv_nsec -= NSEC_PER_SEC;
295 }
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700296 schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next));
john stultz4c7ee8d2006-09-30 23:28:22 -0700297}
298
Thomas Gleixner82644452007-07-21 04:37:37 -0700299static void notify_cmos_timer(void)
300{
Tony Breeds298a5df2007-09-11 15:24:03 -0700301 if (!no_sync_cmos_clock)
Maciej W. Rozyckieb3f9382008-09-22 14:42:40 -0700302 schedule_delayed_work(&sync_cmos_work, 0);
Thomas Gleixner82644452007-07-21 04:37:37 -0700303}
304
305#else
306static inline void notify_cmos_timer(void) { }
307#endif
308
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100309/*
310 * adjtimex mainly allows reading (and writing, if superuser) of
john stultz4c7ee8d2006-09-30 23:28:22 -0700311 * kernel time-keeping variables. used by xntpd.
312 */
313int do_adjtimex(struct timex *txc)
314{
Roman Zippeleea83d82008-05-01 04:34:33 -0700315 struct timespec ts;
john stultz4c7ee8d2006-09-30 23:28:22 -0700316 int result;
317
Roman Zippel916c7a82008-08-20 16:46:08 -0700318 /* Validate the data before disabling interrupts */
319 if (txc->modes & ADJ_ADJTIME) {
Roman Zippeleea83d82008-05-01 04:34:33 -0700320 /* singleshot must not be used with any other mode bits */
Roman Zippel916c7a82008-08-20 16:46:08 -0700321 if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
john stultz4c7ee8d2006-09-30 23:28:22 -0700322 return -EINVAL;
Roman Zippel916c7a82008-08-20 16:46:08 -0700323 if (!(txc->modes & ADJ_OFFSET_READONLY) &&
324 !capable(CAP_SYS_TIME))
325 return -EPERM;
326 } else {
327 /* In order to modify anything, you gotta be super-user! */
328 if (txc->modes && !capable(CAP_SYS_TIME))
329 return -EPERM;
330
Ingo Molnar53bbfa92008-02-20 07:58:42 +0100331 /*
332 * if the quartz is off by more than 10% then
333 * something is VERY wrong!
334 */
Roman Zippel916c7a82008-08-20 16:46:08 -0700335 if (txc->modes & ADJ_TICK &&
336 (txc->tick < 900000/USER_HZ ||
337 txc->tick > 1100000/USER_HZ))
338 return -EINVAL;
339
340 if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
341 hrtimer_cancel(&leap_timer);
John Stultz52bfb362007-11-26 20:42:19 +0100342 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700343
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700344 getnstimeofday(&ts);
345
john stultz4c7ee8d2006-09-30 23:28:22 -0700346 write_seqlock_irq(&xtime_lock);
john stultz4c7ee8d2006-09-30 23:28:22 -0700347
john stultz4c7ee8d2006-09-30 23:28:22 -0700348 /* If there are input parameters, then process them */
Roman Zippel916c7a82008-08-20 16:46:08 -0700349 if (txc->modes & ADJ_ADJTIME) {
350 long save_adjust = time_adjust;
351
352 if (!(txc->modes & ADJ_OFFSET_READONLY)) {
353 /* adjtime() is independent from ntp_adjtime() */
354 time_adjust = txc->offset;
355 ntp_update_frequency();
356 }
357 txc->offset = save_adjust;
358 goto adj_done;
359 }
Roman Zippelee9851b2008-05-01 04:34:32 -0700360 if (txc->modes) {
Roman Zippel916c7a82008-08-20 16:46:08 -0700361 long sec;
362
Roman Zippeleea83d82008-05-01 04:34:33 -0700363 if (txc->modes & ADJ_STATUS) {
364 if ((time_status & STA_PLL) &&
365 !(txc->status & STA_PLL)) {
366 time_state = TIME_OK;
367 time_status = STA_UNSYNC;
368 }
369 /* only set allowed bits */
370 time_status &= STA_RONLY;
371 time_status |= txc->status & ~STA_RONLY;
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700372
373 switch (time_state) {
374 case TIME_OK:
375 start_timer:
376 sec = ts.tv_sec;
377 if (time_status & STA_INS) {
378 time_state = TIME_INS;
379 sec += 86400 - sec % 86400;
380 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
381 } else if (time_status & STA_DEL) {
382 time_state = TIME_DEL;
383 sec += 86400 - (sec + 1) % 86400;
384 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
385 }
386 break;
387 case TIME_INS:
388 case TIME_DEL:
389 time_state = TIME_OK;
390 goto start_timer;
391 break;
392 case TIME_WAIT:
393 if (!(time_status & (STA_INS | STA_DEL)))
394 time_state = TIME_OK;
395 break;
396 case TIME_OOP:
397 hrtimer_restart(&leap_timer);
398 break;
399 }
Roman Zippeleea83d82008-05-01 04:34:33 -0700400 }
401
402 if (txc->modes & ADJ_NANO)
403 time_status |= STA_NANO;
404 if (txc->modes & ADJ_MICRO)
405 time_status &= ~STA_NANO;
john stultz4c7ee8d2006-09-30 23:28:22 -0700406
Roman Zippelee9851b2008-05-01 04:34:32 -0700407 if (txc->modes & ADJ_FREQUENCY) {
Roman Zippel074b3b82008-05-01 04:34:34 -0700408 time_freq = (s64)txc->freq * PPM_SCALE;
409 time_freq = min(time_freq, MAXFREQ_SCALED);
410 time_freq = max(time_freq, -MAXFREQ_SCALED);
john stultz4c7ee8d2006-09-30 23:28:22 -0700411 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700412
Roman Zippeleea83d82008-05-01 04:34:33 -0700413 if (txc->modes & ADJ_MAXERROR)
Roman Zippelee9851b2008-05-01 04:34:32 -0700414 time_maxerror = txc->maxerror;
Roman Zippeleea83d82008-05-01 04:34:33 -0700415 if (txc->modes & ADJ_ESTERROR)
Roman Zippelee9851b2008-05-01 04:34:32 -0700416 time_esterror = txc->esterror;
john stultz4c7ee8d2006-09-30 23:28:22 -0700417
Roman Zippelee9851b2008-05-01 04:34:32 -0700418 if (txc->modes & ADJ_TIMECONST) {
Roman Zippeleea83d82008-05-01 04:34:33 -0700419 time_constant = txc->constant;
420 if (!(time_status & STA_NANO))
421 time_constant += 4;
422 time_constant = min(time_constant, (long)MAXTC);
423 time_constant = max(time_constant, 0l);
john stultz4c7ee8d2006-09-30 23:28:22 -0700424 }
john stultz4c7ee8d2006-09-30 23:28:22 -0700425
Roman Zippel153b5d02008-05-01 04:34:37 -0700426 if (txc->modes & ADJ_TAI && txc->constant > 0)
427 time_tai = txc->constant;
428
Roman Zippel916c7a82008-08-20 16:46:08 -0700429 if (txc->modes & ADJ_OFFSET)
430 ntp_update_offset(txc->offset);
Roman Zippelee9851b2008-05-01 04:34:32 -0700431 if (txc->modes & ADJ_TICK)
432 tick_usec = txc->tick;
john stultz4c7ee8d2006-09-30 23:28:22 -0700433
Roman Zippelee9851b2008-05-01 04:34:32 -0700434 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
435 ntp_update_frequency();
436 }
Roman Zippeleea83d82008-05-01 04:34:33 -0700437
Roman Zippel916c7a82008-08-20 16:46:08 -0700438 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
439 NTP_SCALE_SHIFT);
440 if (!(time_status & STA_NANO))
441 txc->offset /= NSEC_PER_USEC;
442
443adj_done:
Roman Zippeleea83d82008-05-01 04:34:33 -0700444 result = time_state; /* mostly `TIME_OK' */
Roman Zippelee9851b2008-05-01 04:34:32 -0700445 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
john stultz4c7ee8d2006-09-30 23:28:22 -0700446 result = TIME_ERROR;
447
Roman Zippeld40e9442008-09-22 14:42:44 -0700448 txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
449 (s64)PPM_SCALE_INV, NTP_SCALE_SHIFT);
john stultz4c7ee8d2006-09-30 23:28:22 -0700450 txc->maxerror = time_maxerror;
451 txc->esterror = time_esterror;
452 txc->status = time_status;
453 txc->constant = time_constant;
Adrian Bunk70bc42f2006-09-30 23:28:29 -0700454 txc->precision = 1;
Roman Zippel074b3b82008-05-01 04:34:34 -0700455 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
john stultz4c7ee8d2006-09-30 23:28:22 -0700456 txc->tick = tick_usec;
Roman Zippel153b5d02008-05-01 04:34:37 -0700457 txc->tai = time_tai;
john stultz4c7ee8d2006-09-30 23:28:22 -0700458
459 /* PPS is not implemented, so these are zero */
460 txc->ppsfreq = 0;
461 txc->jitter = 0;
462 txc->shift = 0;
463 txc->stabil = 0;
464 txc->jitcnt = 0;
465 txc->calcnt = 0;
466 txc->errcnt = 0;
467 txc->stbcnt = 0;
468 write_sequnlock_irq(&xtime_lock);
Roman Zippelee9851b2008-05-01 04:34:32 -0700469
Roman Zippeleea83d82008-05-01 04:34:33 -0700470 txc->time.tv_sec = ts.tv_sec;
471 txc->time.tv_usec = ts.tv_nsec;
472 if (!(time_status & STA_NANO))
473 txc->time.tv_usec /= NSEC_PER_USEC;
Roman Zippelee9851b2008-05-01 04:34:32 -0700474
Thomas Gleixner82644452007-07-21 04:37:37 -0700475 notify_cmos_timer();
Roman Zippelee9851b2008-05-01 04:34:32 -0700476
477 return result;
john stultz4c7ee8d2006-09-30 23:28:22 -0700478}
Roman Zippel10a398d2008-03-04 15:14:26 -0800479
480static int __init ntp_tick_adj_setup(char *str)
481{
482 ntp_tick_adj = simple_strtol(str, NULL, 0);
483 return 1;
484}
485
486__setup("ntp_tick_adj=", ntp_tick_adj_setup);
Roman Zippel7dffa3c2008-05-01 04:34:41 -0700487
488void __init ntp_init(void)
489{
490 ntp_clear();
491 hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
492 leap_timer.function = ntp_leap_second;
493}