]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - kernel/time/ntp.c
ntp: NTP4 user space bits update
[mirror_ubuntu-zesty-kernel.git] / kernel / time / ntp.c
CommitLineData
4c7ee8de
JS
1/*
2 * linux/kernel/time/ntp.c
3 *
4 * NTP state machine interfaces and logic.
5 *
6 * This code was mainly moved from kernel/timer.c and kernel/time.c
7 * Please see those files for relevant copyright info and historical
8 * changelogs.
9 */
10
11#include <linux/mm.h>
12#include <linux/time.h>
82644459 13#include <linux/timer.h>
4c7ee8de 14#include <linux/timex.h>
e8edc6e0
AD
15#include <linux/jiffies.h>
16#include <linux/hrtimer.h>
aa0ac365 17#include <linux/capability.h>
71abb3af 18#include <linux/math64.h>
4c7ee8de
JS
19#include <asm/timex.h>
20
b0ee7556
RZ
21/*
22 * Timekeeping variables
23 */
24unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
25unsigned long tick_nsec; /* ACTHZ period (nsec) */
26static u64 tick_length, tick_length_base;
27
8f807f8d
RZ
28#define MAX_TICKADJ 500 /* microsecs */
29#define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \
f4304ab2 30 TICK_LENGTH_SHIFT) / NTP_INTERVAL_FREQ)
4c7ee8de
JS
31
32/*
33 * phase-lock loop variables
34 */
35/* TIME_ERROR prevents overwriting the CMOS clock */
70bc42f9 36static int time_state = TIME_OK; /* clock synchronization status */
4c7ee8de 37int time_status = STA_UNSYNC; /* clock status bits */
ee9851b2 38static s64 time_offset; /* time adjustment (ns) */
70bc42f9 39static long time_constant = 2; /* pll time constant */
4c7ee8de
JS
40long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
41long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
dc6a43e4 42long time_freq; /* frequency offset (scaled ppm)*/
70bc42f9 43static long time_reftime; /* time at last adjustment (s) */
4c7ee8de 44long time_adjust;
10a398d0 45static long ntp_tick_adj;
4c7ee8de 46
70bc42f9
AB
47static void ntp_update_frequency(void)
48{
f4304ab2
JS
49 u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
50 << TICK_LENGTH_SHIFT;
10a398d0 51 second_length += (s64)ntp_tick_adj << TICK_LENGTH_SHIFT;
f4304ab2 52 second_length += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC);
70bc42f9 53
f4304ab2 54 tick_length_base = second_length;
70bc42f9 55
71abb3af
RZ
56 tick_nsec = div_u64(second_length, HZ) >> TICK_LENGTH_SHIFT;
57 tick_length_base = div_u64(tick_length_base, NTP_INTERVAL_FREQ);
70bc42f9
AB
58}
59
ee9851b2
RZ
60static void ntp_update_offset(long offset)
61{
62 long mtemp;
63 s64 freq_adj;
64
65 if (!(time_status & STA_PLL))
66 return;
67
eea83d89
RZ
68 time_offset = offset;
69 if (!(time_status & STA_NANO))
70 time_offset *= NSEC_PER_USEC;
ee9851b2
RZ
71
72 /*
73 * Scale the phase adjustment and
74 * clamp to the operating range.
75 */
76 time_offset = min(time_offset, (s64)MAXPHASE * NSEC_PER_USEC);
77 time_offset = max(time_offset, (s64)-MAXPHASE * NSEC_PER_USEC);
78
79 /*
80 * Select how the frequency is to be controlled
81 * and in which mode (PLL or FLL).
82 */
83 if (time_status & STA_FREQHOLD || time_reftime == 0)
84 time_reftime = xtime.tv_sec;
85 mtemp = xtime.tv_sec - time_reftime;
86 time_reftime = xtime.tv_sec;
87
88 freq_adj = time_offset * mtemp;
89 freq_adj = shift_right(freq_adj, time_constant * 2 +
90 (SHIFT_PLL + 2) * 2 - SHIFT_NSEC);
eea83d89
RZ
91 time_status &= ~STA_MODE;
92 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
ee9851b2 93 freq_adj += div_s64(time_offset << (SHIFT_NSEC - SHIFT_FLL), mtemp);
eea83d89
RZ
94 time_status |= STA_MODE;
95 }
ee9851b2
RZ
96 freq_adj += time_freq;
97 freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC);
98 time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC);
99 time_offset = div_s64(time_offset, NTP_INTERVAL_FREQ);
100 time_offset <<= SHIFT_UPDATE;
101}
102
b0ee7556
RZ
103/**
104 * ntp_clear - Clears the NTP state variables
105 *
106 * Must be called while holding a write on the xtime_lock
107 */
108void ntp_clear(void)
109{
110 time_adjust = 0; /* stop active adjtime() */
111 time_status |= STA_UNSYNC;
112 time_maxerror = NTP_PHASE_LIMIT;
113 time_esterror = NTP_PHASE_LIMIT;
114
115 ntp_update_frequency();
116
117 tick_length = tick_length_base;
3d3675cc 118 time_offset = 0;
b0ee7556
RZ
119}
120
4c7ee8de
JS
121/*
122 * this routine handles the overflow of the microsecond field
123 *
124 * The tricky bits of code to handle the accurate clock support
125 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
126 * They were originally developed for SUN and DEC kernels.
127 * All the kudos should go to Dave for this stuff.
128 */
129void second_overflow(void)
130{
3d3675cc 131 long time_adj;
4c7ee8de
JS
132
133 /* Bump the maxerror field */
97eebe13 134 time_maxerror += MAXFREQ >> SHIFT_USEC;
4c7ee8de
JS
135 if (time_maxerror > NTP_PHASE_LIMIT) {
136 time_maxerror = NTP_PHASE_LIMIT;
137 time_status |= STA_UNSYNC;
138 }
139
140 /*
141 * Leap second processing. If in leap-insert state at the end of the
142 * day, the system clock is set back one second; if in leap-delete
143 * state, the system clock is set ahead one second. The microtime()
144 * routine or external clock driver will insure that reported time is
145 * always monotonic. The ugly divides should be replaced.
146 */
147 switch (time_state) {
148 case TIME_OK:
149 if (time_status & STA_INS)
150 time_state = TIME_INS;
151 else if (time_status & STA_DEL)
152 time_state = TIME_DEL;
153 break;
154 case TIME_INS:
155 if (xtime.tv_sec % 86400 == 0) {
156 xtime.tv_sec--;
157 wall_to_monotonic.tv_sec++;
4c7ee8de 158 time_state = TIME_OOP;
4c7ee8de
JS
159 printk(KERN_NOTICE "Clock: inserting leap second "
160 "23:59:60 UTC\n");
161 }
162 break;
163 case TIME_DEL:
164 if ((xtime.tv_sec + 1) % 86400 == 0) {
165 xtime.tv_sec++;
166 wall_to_monotonic.tv_sec--;
4c7ee8de 167 time_state = TIME_WAIT;
4c7ee8de
JS
168 printk(KERN_NOTICE "Clock: deleting leap second "
169 "23:59:59 UTC\n");
170 }
171 break;
172 case TIME_OOP:
173 time_state = TIME_WAIT;
174 break;
175 case TIME_WAIT:
176 if (!(time_status & (STA_INS | STA_DEL)))
ee9851b2 177 time_state = TIME_OK;
4c7ee8de
JS
178 }
179
180 /*
f1992393
RZ
181 * Compute the phase adjustment for the next second. The offset is
182 * reduced by a fixed factor times the time constant.
4c7ee8de 183 */
b0ee7556 184 tick_length = tick_length_base;
f1992393 185 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
3d3675cc
RZ
186 time_offset -= time_adj;
187 tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE);
4c7ee8de 188
8f807f8d
RZ
189 if (unlikely(time_adjust)) {
190 if (time_adjust > MAX_TICKADJ) {
191 time_adjust -= MAX_TICKADJ;
192 tick_length += MAX_TICKADJ_SCALED;
193 } else if (time_adjust < -MAX_TICKADJ) {
194 time_adjust += MAX_TICKADJ;
195 tick_length -= MAX_TICKADJ_SCALED;
196 } else {
8f807f8d 197 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
f4304ab2 198 NTP_INTERVAL_FREQ) << TICK_LENGTH_SHIFT;
bb1d8605 199 time_adjust = 0;
8f807f8d 200 }
4c7ee8de
JS
201 }
202}
203
204/*
205 * Return how long ticks are at the moment, that is, how much time
206 * update_wall_time_one_tick will add to xtime next time we call it
207 * (assuming no calls to do_adjtimex in the meantime).
208 * The return value is in fixed-point nanoseconds shifted by the
209 * specified number of bits to the right of the binary point.
210 * This function has no side-effects.
211 */
212u64 current_tick_length(void)
213{
8f807f8d 214 return tick_length;
4c7ee8de
JS
215}
216
82644459 217#ifdef CONFIG_GENERIC_CMOS_UPDATE
4c7ee8de 218
82644459
TG
219/* Disable the cmos update - used by virtualization and embedded */
220int no_sync_cmos_clock __read_mostly;
221
222static void sync_cmos_clock(unsigned long dummy);
223
224static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0);
225
226static void sync_cmos_clock(unsigned long dummy)
227{
228 struct timespec now, next;
229 int fail = 1;
230
231 /*
232 * If we have an externally synchronized Linux clock, then update
233 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
234 * called as close as possible to 500 ms before the new second starts.
235 * This code is run on a timer. If the clock is set, that timer
236 * may not expire at the correct time. Thus, we adjust...
237 */
238 if (!ntp_synced())
239 /*
240 * Not synced, exit, do not restart a timer (if one is
241 * running, let it run out).
242 */
243 return;
244
245 getnstimeofday(&now);
fa6a1a55 246 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
82644459
TG
247 fail = update_persistent_clock(now);
248
249 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec;
250 if (next.tv_nsec <= 0)
251 next.tv_nsec += NSEC_PER_SEC;
252
253 if (!fail)
254 next.tv_sec = 659;
255 else
256 next.tv_sec = 0;
257
258 if (next.tv_nsec >= NSEC_PER_SEC) {
259 next.tv_sec++;
260 next.tv_nsec -= NSEC_PER_SEC;
261 }
262 mod_timer(&sync_cmos_timer, jiffies + timespec_to_jiffies(&next));
263}
264
265static void notify_cmos_timer(void)
4c7ee8de 266{
298a5df4 267 if (!no_sync_cmos_clock)
82644459 268 mod_timer(&sync_cmos_timer, jiffies + 1);
4c7ee8de
JS
269}
270
82644459
TG
271#else
272static inline void notify_cmos_timer(void) { }
273#endif
274
4c7ee8de
JS
275/* adjtimex mainly allows reading (and writing, if superuser) of
276 * kernel time-keeping variables. used by xntpd.
277 */
278int do_adjtimex(struct timex *txc)
279{
eea83d89 280 struct timespec ts;
ee9851b2 281 long save_adjust;
4c7ee8de
JS
282 int result;
283
284 /* In order to modify anything, you gotta be super-user! */
285 if (txc->modes && !capable(CAP_SYS_TIME))
286 return -EPERM;
287
288 /* Now we validate the data before disabling interrupts */
289
52bfb360 290 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) {
eea83d89
RZ
291 /* singleshot must not be used with any other mode bits */
292 if (txc->modes & ~ADJ_OFFSET_SS_READ)
4c7ee8de 293 return -EINVAL;
52bfb360 294 }
4c7ee8de 295
4c7ee8de
JS
296 /* if the quartz is off by more than 10% something is VERY wrong ! */
297 if (txc->modes & ADJ_TICK)
298 if (txc->tick < 900000/USER_HZ ||
299 txc->tick > 1100000/USER_HZ)
300 return -EINVAL;
301
302 write_seqlock_irq(&xtime_lock);
4c7ee8de
JS
303
304 /* Save for later - semantics of adjtime is to return old value */
8f807f8d 305 save_adjust = time_adjust;
4c7ee8de 306
4c7ee8de 307 /* If there are input parameters, then process them */
ee9851b2 308 if (txc->modes) {
eea83d89
RZ
309 if (txc->modes & ADJ_STATUS) {
310 if ((time_status & STA_PLL) &&
311 !(txc->status & STA_PLL)) {
312 time_state = TIME_OK;
313 time_status = STA_UNSYNC;
314 }
315 /* only set allowed bits */
316 time_status &= STA_RONLY;
317 time_status |= txc->status & ~STA_RONLY;
318 }
319
320 if (txc->modes & ADJ_NANO)
321 time_status |= STA_NANO;
322 if (txc->modes & ADJ_MICRO)
323 time_status &= ~STA_NANO;
ee9851b2
RZ
324
325 if (txc->modes & ADJ_FREQUENCY) {
eea83d89
RZ
326 time_freq = min(txc->freq, MAXFREQ);
327 time_freq = min(time_freq, -MAXFREQ);
328 time_freq = ((s64)time_freq * NSEC_PER_USEC)
ee9851b2 329 >> (SHIFT_USEC - SHIFT_NSEC);
4c7ee8de 330 }
ee9851b2 331
eea83d89 332 if (txc->modes & ADJ_MAXERROR)
ee9851b2 333 time_maxerror = txc->maxerror;
eea83d89 334 if (txc->modes & ADJ_ESTERROR)
ee9851b2 335 time_esterror = txc->esterror;
4c7ee8de 336
ee9851b2 337 if (txc->modes & ADJ_TIMECONST) {
eea83d89
RZ
338 time_constant = txc->constant;
339 if (!(time_status & STA_NANO))
340 time_constant += 4;
341 time_constant = min(time_constant, (long)MAXTC);
342 time_constant = max(time_constant, 0l);
4c7ee8de 343 }
4c7ee8de 344
ee9851b2
RZ
345 if (txc->modes & ADJ_OFFSET) {
346 if (txc->modes == ADJ_OFFSET_SINGLESHOT)
347 /* adjtime() is independent from ntp_adjtime() */
348 time_adjust = txc->offset;
349 else
350 ntp_update_offset(txc->offset);
4c7ee8de 351 }
ee9851b2
RZ
352 if (txc->modes & ADJ_TICK)
353 tick_usec = txc->tick;
354
355 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
356 ntp_update_frequency();
357 }
eea83d89
RZ
358
359 result = time_state; /* mostly `TIME_OK' */
ee9851b2 360 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
4c7ee8de
JS
361 result = TIME_ERROR;
362
52bfb360 363 if ((txc->modes == ADJ_OFFSET_SINGLESHOT) ||
ee9851b2 364 (txc->modes == ADJ_OFFSET_SS_READ))
d62ac21a 365 txc->offset = save_adjust;
eea83d89 366 else {
d62ac21a 367 txc->offset = ((long)shift_right(time_offset, SHIFT_UPDATE)) *
eea83d89
RZ
368 NTP_INTERVAL_FREQ;
369 if (!(time_status & STA_NANO))
370 txc->offset /= NSEC_PER_USEC;
371 }
d62ac21a
JS
372 txc->freq = (time_freq / NSEC_PER_USEC) <<
373 (SHIFT_USEC - SHIFT_NSEC);
4c7ee8de
JS
374 txc->maxerror = time_maxerror;
375 txc->esterror = time_esterror;
376 txc->status = time_status;
377 txc->constant = time_constant;
70bc42f9 378 txc->precision = 1;
97eebe13 379 txc->tolerance = MAXFREQ;
4c7ee8de
JS
380 txc->tick = tick_usec;
381
382 /* PPS is not implemented, so these are zero */
383 txc->ppsfreq = 0;
384 txc->jitter = 0;
385 txc->shift = 0;
386 txc->stabil = 0;
387 txc->jitcnt = 0;
388 txc->calcnt = 0;
389 txc->errcnt = 0;
390 txc->stbcnt = 0;
391 write_sequnlock_irq(&xtime_lock);
ee9851b2 392
eea83d89
RZ
393 getnstimeofday(&ts);
394 txc->time.tv_sec = ts.tv_sec;
395 txc->time.tv_usec = ts.tv_nsec;
396 if (!(time_status & STA_NANO))
397 txc->time.tv_usec /= NSEC_PER_USEC;
ee9851b2 398
82644459 399 notify_cmos_timer();
ee9851b2
RZ
400
401 return result;
4c7ee8de 402}
10a398d0
RZ
403
404static int __init ntp_tick_adj_setup(char *str)
405{
406 ntp_tick_adj = simple_strtol(str, NULL, 0);
407 return 1;
408}
409
410__setup("ntp_tick_adj=", ntp_tick_adj_setup);