time: ntp: clean up second_overflow()
[deliverable/linux.git] / kernel / time / ntp.c
CommitLineData
4c7ee8de 1/*
4c7ee8de 2 * 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 */
aa0ac365 8#include <linux/capability.h>
7dffa3c6 9#include <linux/clocksource.h>
eb3f938f 10#include <linux/workqueue.h>
53bbfa9e
IM
11#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>
4c7ee8de 17
b0ee7556 18/*
53bbfa9e 19 * NTP timekeeping variables:
b0ee7556 20 */
b0ee7556 21
53bbfa9e
IM
22/* USER_HZ period (usecs): */
23unsigned long tick_usec = TICK_USEC;
24
25/* ACTHZ period (nsecs): */
26unsigned long tick_nsec;
7dffa3c6 27
53bbfa9e
IM
28u64 tick_length;
29static u64 tick_length_base;
30
31static struct hrtimer leap_timer;
32
bbd12676 33#define MAX_TICKADJ 500LL /* usecs */
53bbfa9e 34#define MAX_TICKADJ_SCALED \
bbd12676 35 (((MAX_TICKADJ * NSEC_PER_USEC) << NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
4c7ee8de 36
37/*
38 * phase-lock loop variables
39 */
53bbfa9e
IM
40
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
069569e0
IM
74/* constant (boot-param configurable) NTP tick adjustment (upscaled) */
75static s64 ntp_tick_adj;
53bbfa9e
IM
76
77/*
78 * NTP methods:
79 */
4c7ee8de 80
9ce616aa
IM
81/*
82 * Update (tick_length, tick_length_base, tick_nsec), based
83 * on (tick_usec, ntp_tick_adj, time_freq):
84 */
70bc42f9
AB
85static void ntp_update_frequency(void)
86{
9ce616aa 87 u64 second_length;
bc26c31d 88 u64 new_base;
9ce616aa
IM
89
90 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
91 << NTP_SCALE_SHIFT;
92
069569e0 93 second_length += ntp_tick_adj;
9ce616aa 94 second_length += time_freq;
70bc42f9 95
9ce616aa 96 tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
bc26c31d 97 new_base = div_u64(second_length, NTP_INTERVAL_FREQ);
fdcedf7b 98
99 /*
100 * Don't wait for the next second_overflow, apply
bc26c31d 101 * the change to the tick length immediately:
fdcedf7b 102 */
bc26c31d
IM
103 tick_length += new_base - tick_length_base;
104 tick_length_base = new_base;
70bc42f9
AB
105}
106
478b7aab 107static inline s64 ntp_update_offset_fll(s64 offset64, long secs)
f939890b
IM
108{
109 time_status &= ~STA_MODE;
110
111 if (secs < MINSEC)
478b7aab 112 return 0;
f939890b
IM
113
114 if (!(time_status & STA_FLL) && (secs <= MAXSEC))
478b7aab 115 return 0;
f939890b 116
f939890b
IM
117 time_status |= STA_MODE;
118
478b7aab 119 return div_s64(offset64 << (NTP_SCALE_SHIFT - SHIFT_FLL), secs);
f939890b
IM
120}
121
ee9851b2
RZ
122static void ntp_update_offset(long offset)
123{
ee9851b2 124 s64 freq_adj;
f939890b
IM
125 s64 offset64;
126 long secs;
ee9851b2
RZ
127
128 if (!(time_status & STA_PLL))
129 return;
130
eea83d89 131 if (!(time_status & STA_NANO))
9f14f669 132 offset *= NSEC_PER_USEC;
ee9851b2
RZ
133
134 /*
135 * Scale the phase adjustment and
136 * clamp to the operating range.
137 */
9f14f669
RZ
138 offset = min(offset, MAXPHASE);
139 offset = max(offset, -MAXPHASE);
ee9851b2
RZ
140
141 /*
142 * Select how the frequency is to be controlled
143 * and in which mode (PLL or FLL).
144 */
f939890b 145 secs = xtime.tv_sec - time_reftime;
10dd31a7 146 if (unlikely(time_status & STA_FREQHOLD))
c7986acb
IM
147 secs = 0;
148
ee9851b2
RZ
149 time_reftime = xtime.tv_sec;
150
f939890b
IM
151 offset64 = offset;
152 freq_adj = (offset64 * secs) <<
153 (NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant));
154
478b7aab 155 freq_adj += ntp_update_offset_fll(offset64, secs);
f939890b
IM
156
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);
ee9851b2
RZ
162}
163
b0ee7556
RZ
164/**
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{
53bbfa9e
IM
171 time_adjust = 0; /* stop active adjtime() */
172 time_status |= STA_UNSYNC;
173 time_maxerror = NTP_PHASE_LIMIT;
174 time_esterror = NTP_PHASE_LIMIT;
b0ee7556
RZ
175
176 ntp_update_frequency();
177
53bbfa9e
IM
178 tick_length = tick_length_base;
179 time_offset = 0;
b0ee7556
RZ
180}
181
4c7ee8de 182/*
7dffa3c6
RZ
183 * 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.
4c7ee8de 186 */
7dffa3c6 187static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
4c7ee8de 188{
7dffa3c6 189 enum hrtimer_restart res = HRTIMER_NORESTART;
4c7ee8de 190
ca109491 191 write_seqlock(&xtime_lock);
4c7ee8de 192
4c7ee8de 193 switch (time_state) {
194 case TIME_OK:
4c7ee8de 195 break;
196 case TIME_INS:
7dffa3c6
RZ
197 xtime.tv_sec--;
198 wall_to_monotonic.tv_sec++;
199 time_state = TIME_OOP;
53bbfa9e
IM
200 printk(KERN_NOTICE
201 "Clock: inserting leap second 23:59:60 UTC\n");
cc584b21 202 hrtimer_add_expires_ns(&leap_timer, NSEC_PER_SEC);
7dffa3c6 203 res = HRTIMER_RESTART;
4c7ee8de 204 break;
205 case TIME_DEL:
7dffa3c6
RZ
206 xtime.tv_sec++;
207 time_tai--;
208 wall_to_monotonic.tv_sec--;
209 time_state = TIME_WAIT;
53bbfa9e
IM
210 printk(KERN_NOTICE
211 "Clock: deleting leap second 23:59:59 UTC\n");
4c7ee8de 212 break;
213 case TIME_OOP:
153b5d05 214 time_tai++;
4c7ee8de 215 time_state = TIME_WAIT;
7dffa3c6 216 /* fall through */
4c7ee8de 217 case TIME_WAIT:
218 if (!(time_status & (STA_INS | STA_DEL)))
ee9851b2 219 time_state = TIME_OK;
7dffa3c6
RZ
220 break;
221 }
222 update_vsyscall(&xtime, clock);
223
ca109491 224 write_sequnlock(&xtime_lock);
7dffa3c6
RZ
225
226 return res;
227}
228
229/*
230 * 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{
39854fe8 239 s64 delta;
7dffa3c6
RZ
240
241 /* Bump the maxerror field */
242 time_maxerror += MAXFREQ / NSEC_PER_USEC;
243 if (time_maxerror > NTP_PHASE_LIMIT) {
244 time_maxerror = NTP_PHASE_LIMIT;
245 time_status |= STA_UNSYNC;
4c7ee8de 246 }
247
248 /*
f1992393
RZ
249 * Compute the phase adjustment for the next second. The offset is
250 * reduced by a fixed factor times the time constant.
4c7ee8de 251 */
39854fe8
IM
252 tick_length = tick_length_base;
253
254 delta = shift_right(time_offset, SHIFT_PLL + time_constant);
255 time_offset -= delta;
256 tick_length += delta;
4c7ee8de 257
3c972c24
IM
258 if (!time_adjust)
259 return;
260
261 if (time_adjust > MAX_TICKADJ) {
262 time_adjust -= MAX_TICKADJ;
263 tick_length += MAX_TICKADJ_SCALED;
264 return;
4c7ee8de 265 }
3c972c24
IM
266
267 if (time_adjust < -MAX_TICKADJ) {
268 time_adjust += MAX_TICKADJ;
269 tick_length -= MAX_TICKADJ_SCALED;
270 return;
271 }
272
273 tick_length += (s64)(time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ)
274 << NTP_SCALE_SHIFT;
275 time_adjust = 0;
4c7ee8de 276}
277
82644459 278#ifdef CONFIG_GENERIC_CMOS_UPDATE
4c7ee8de 279
82644459
TG
280/* Disable the cmos update - used by virtualization and embedded */
281int no_sync_cmos_clock __read_mostly;
282
eb3f938f 283static void sync_cmos_clock(struct work_struct *work);
82644459 284
eb3f938f 285static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
82644459 286
eb3f938f 287static void sync_cmos_clock(struct work_struct *work)
82644459
TG
288{
289 struct timespec now, next;
290 int fail = 1;
291
292 /*
293 * If we have an externally synchronized Linux clock, then update
294 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
295 * called as close as possible to 500 ms before the new second starts.
296 * This code is run on a timer. If the clock is set, that timer
297 * may not expire at the correct time. Thus, we adjust...
298 */
53bbfa9e 299 if (!ntp_synced()) {
82644459
TG
300 /*
301 * Not synced, exit, do not restart a timer (if one is
302 * running, let it run out).
303 */
304 return;
53bbfa9e 305 }
82644459
TG
306
307 getnstimeofday(&now);
fa6a1a55 308 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
82644459
TG
309 fail = update_persistent_clock(now);
310
4ff4b9e1 311 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2);
82644459
TG
312 if (next.tv_nsec <= 0)
313 next.tv_nsec += NSEC_PER_SEC;
314
315 if (!fail)
316 next.tv_sec = 659;
317 else
318 next.tv_sec = 0;
319
320 if (next.tv_nsec >= NSEC_PER_SEC) {
321 next.tv_sec++;
322 next.tv_nsec -= NSEC_PER_SEC;
323 }
eb3f938f 324 schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next));
82644459
TG
325}
326
327static void notify_cmos_timer(void)
4c7ee8de 328{
298a5df4 329 if (!no_sync_cmos_clock)
eb3f938f 330 schedule_delayed_work(&sync_cmos_work, 0);
4c7ee8de 331}
332
82644459
TG
333#else
334static inline void notify_cmos_timer(void) { }
335#endif
336
e9629165
IM
337/*
338 * Start the leap seconds timer:
339 */
340static inline void ntp_start_leap_timer(struct timespec *ts)
341{
342 long now = ts->tv_sec;
343
344 if (time_status & STA_INS) {
345 time_state = TIME_INS;
346 now += 86400 - now % 86400;
347 hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS);
348
349 return;
350 }
351
352 if (time_status & STA_DEL) {
353 time_state = TIME_DEL;
354 now += 86400 - (now + 1) % 86400;
355 hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS);
356 }
357}
80f22571
IM
358
359/*
360 * Propagate a new txc->status value into the NTP state:
361 */
362static inline void process_adj_status(struct timex *txc, struct timespec *ts)
363{
80f22571
IM
364 if ((time_status & STA_PLL) && !(txc->status & STA_PLL)) {
365 time_state = TIME_OK;
366 time_status = STA_UNSYNC;
367 }
368 /* only set allowed bits */
369 time_status &= STA_RONLY;
370
371 /*
372 * If we turn on PLL adjustments then reset the
373 * reference time to current time.
374 */
375 if (!(time_status & STA_PLL) && (txc->status & STA_PLL))
376 time_reftime = xtime.tv_sec;
377
378 time_status |= txc->status & ~STA_RONLY;
379
380 switch (time_state) {
381 case TIME_OK:
e9629165 382 ntp_start_leap_timer(ts);
80f22571
IM
383 break;
384 case TIME_INS:
385 case TIME_DEL:
386 time_state = TIME_OK;
e9629165 387 ntp_start_leap_timer(ts);
80f22571
IM
388 case TIME_WAIT:
389 if (!(time_status & (STA_INS | STA_DEL)))
390 time_state = TIME_OK;
391 break;
392 case TIME_OOP:
393 hrtimer_restart(&leap_timer);
394 break;
395 }
396}
397/*
398 * Called with the xtime lock held, so we can access and modify
399 * all the global NTP state:
400 */
401static inline void process_adjtimex_modes(struct timex *txc, struct timespec *ts)
402{
403 if (txc->modes & ADJ_STATUS)
404 process_adj_status(txc, ts);
405
406 if (txc->modes & ADJ_NANO)
407 time_status |= STA_NANO;
e9629165 408
80f22571
IM
409 if (txc->modes & ADJ_MICRO)
410 time_status &= ~STA_NANO;
411
412 if (txc->modes & ADJ_FREQUENCY) {
2b9d1496 413 time_freq = txc->freq * PPM_SCALE;
80f22571
IM
414 time_freq = min(time_freq, MAXFREQ_SCALED);
415 time_freq = max(time_freq, -MAXFREQ_SCALED);
416 }
417
418 if (txc->modes & ADJ_MAXERROR)
419 time_maxerror = txc->maxerror;
e9629165 420
80f22571
IM
421 if (txc->modes & ADJ_ESTERROR)
422 time_esterror = txc->esterror;
423
424 if (txc->modes & ADJ_TIMECONST) {
425 time_constant = txc->constant;
426 if (!(time_status & STA_NANO))
427 time_constant += 4;
428 time_constant = min(time_constant, (long)MAXTC);
429 time_constant = max(time_constant, 0l);
430 }
431
432 if (txc->modes & ADJ_TAI && txc->constant > 0)
433 time_tai = txc->constant;
434
435 if (txc->modes & ADJ_OFFSET)
436 ntp_update_offset(txc->offset);
e9629165 437
80f22571
IM
438 if (txc->modes & ADJ_TICK)
439 tick_usec = txc->tick;
440
441 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
442 ntp_update_frequency();
443}
444
53bbfa9e
IM
445/*
446 * adjtimex mainly allows reading (and writing, if superuser) of
4c7ee8de 447 * kernel time-keeping variables. used by xntpd.
448 */
449int do_adjtimex(struct timex *txc)
450{
eea83d89 451 struct timespec ts;
4c7ee8de 452 int result;
453
916c7a85
RZ
454 /* Validate the data before disabling interrupts */
455 if (txc->modes & ADJ_ADJTIME) {
eea83d89 456 /* singleshot must not be used with any other mode bits */
916c7a85 457 if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
4c7ee8de 458 return -EINVAL;
916c7a85
RZ
459 if (!(txc->modes & ADJ_OFFSET_READONLY) &&
460 !capable(CAP_SYS_TIME))
461 return -EPERM;
462 } else {
463 /* In order to modify anything, you gotta be super-user! */
464 if (txc->modes && !capable(CAP_SYS_TIME))
465 return -EPERM;
466
53bbfa9e
IM
467 /*
468 * if the quartz is off by more than 10% then
469 * something is VERY wrong!
470 */
916c7a85
RZ
471 if (txc->modes & ADJ_TICK &&
472 (txc->tick < 900000/USER_HZ ||
473 txc->tick > 1100000/USER_HZ))
e9629165 474 return -EINVAL;
916c7a85
RZ
475
476 if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
477 hrtimer_cancel(&leap_timer);
52bfb360 478 }
4c7ee8de 479
7dffa3c6
RZ
480 getnstimeofday(&ts);
481
4c7ee8de 482 write_seqlock_irq(&xtime_lock);
4c7ee8de 483
916c7a85
RZ
484 if (txc->modes & ADJ_ADJTIME) {
485 long save_adjust = time_adjust;
486
487 if (!(txc->modes & ADJ_OFFSET_READONLY)) {
488 /* adjtime() is independent from ntp_adjtime() */
489 time_adjust = txc->offset;
490 ntp_update_frequency();
491 }
492 txc->offset = save_adjust;
e9629165 493 } else {
ee9851b2 494
e9629165
IM
495 /* If there are input parameters, then process them: */
496 if (txc->modes)
497 process_adjtimex_modes(txc, &ts);
eea83d89 498
e9629165 499 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
916c7a85 500 NTP_SCALE_SHIFT);
e9629165
IM
501 if (!(time_status & STA_NANO))
502 txc->offset /= NSEC_PER_USEC;
503 }
916c7a85 504
eea83d89 505 result = time_state; /* mostly `TIME_OK' */
ee9851b2 506 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
4c7ee8de 507 result = TIME_ERROR;
508
d40e944c 509 txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
2b9d1496 510 PPM_SCALE_INV, NTP_SCALE_SHIFT);
4c7ee8de 511 txc->maxerror = time_maxerror;
512 txc->esterror = time_esterror;
513 txc->status = time_status;
514 txc->constant = time_constant;
70bc42f9 515 txc->precision = 1;
074b3b87 516 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
4c7ee8de 517 txc->tick = tick_usec;
153b5d05 518 txc->tai = time_tai;
4c7ee8de 519
520 /* PPS is not implemented, so these are zero */
521 txc->ppsfreq = 0;
522 txc->jitter = 0;
523 txc->shift = 0;
524 txc->stabil = 0;
525 txc->jitcnt = 0;
526 txc->calcnt = 0;
527 txc->errcnt = 0;
528 txc->stbcnt = 0;
e9629165 529
4c7ee8de 530 write_sequnlock_irq(&xtime_lock);
ee9851b2 531
eea83d89
RZ
532 txc->time.tv_sec = ts.tv_sec;
533 txc->time.tv_usec = ts.tv_nsec;
534 if (!(time_status & STA_NANO))
535 txc->time.tv_usec /= NSEC_PER_USEC;
ee9851b2 536
82644459 537 notify_cmos_timer();
ee9851b2
RZ
538
539 return result;
4c7ee8de 540}
10a398d0
RZ
541
542static int __init ntp_tick_adj_setup(char *str)
543{
544 ntp_tick_adj = simple_strtol(str, NULL, 0);
069569e0
IM
545 ntp_tick_adj <<= NTP_SCALE_SHIFT;
546
10a398d0
RZ
547 return 1;
548}
549
550__setup("ntp_tick_adj=", ntp_tick_adj_setup);
7dffa3c6
RZ
551
552void __init ntp_init(void)
553{
554 ntp_clear();
555 hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
556 leap_timer.function = ntp_leap_second;
557}
This page took 0.321379 seconds and 5 git commands to generate.