hrtimer: speedup hrtimer_enqueue
[deliverable/linux.git] / kernel / time / ntp.c
CommitLineData
4c7ee8de 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>
13#include <linux/timex.h>
e8edc6e0
AD
14#include <linux/jiffies.h>
15#include <linux/hrtimer.h>
aa0ac365 16#include <linux/capability.h>
4c7ee8de 17#include <asm/div64.h>
18#include <asm/timex.h>
19
b0ee7556
RZ
20/*
21 * Timekeeping variables
22 */
23unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
24unsigned long tick_nsec; /* ACTHZ period (nsec) */
25static u64 tick_length, tick_length_base;
26
8f807f8d
RZ
27#define MAX_TICKADJ 500 /* microsecs */
28#define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \
f4304ab2 29 TICK_LENGTH_SHIFT) / NTP_INTERVAL_FREQ)
4c7ee8de 30
31/*
32 * phase-lock loop variables
33 */
34/* TIME_ERROR prevents overwriting the CMOS clock */
70bc42f9 35static int time_state = TIME_OK; /* clock synchronization status */
4c7ee8de 36int time_status = STA_UNSYNC; /* clock status bits */
d62ac21a 37static s64 time_offset; /* time adjustment (ns) */
70bc42f9 38static long time_constant = 2; /* pll time constant */
4c7ee8de 39long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
40long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
dc6a43e4 41long time_freq; /* frequency offset (scaled ppm)*/
70bc42f9 42static long time_reftime; /* time at last adjustment (s) */
4c7ee8de 43long time_adjust;
4c7ee8de 44
70bc42f9
AB
45#define CLOCK_TICK_OVERFLOW (LATCH * HZ - CLOCK_TICK_RATE)
46#define CLOCK_TICK_ADJUST (((s64)CLOCK_TICK_OVERFLOW * NSEC_PER_SEC) / \
47 (s64)CLOCK_TICK_RATE)
48
49static void ntp_update_frequency(void)
50{
f4304ab2 51 u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
52 << TICK_LENGTH_SHIFT;
53 second_length += (s64)CLOCK_TICK_ADJUST << TICK_LENGTH_SHIFT;
54 second_length += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC);
70bc42f9 55
f4304ab2 56 tick_length_base = second_length;
70bc42f9 57
f4304ab2 58 do_div(second_length, HZ);
59 tick_nsec = second_length >> TICK_LENGTH_SHIFT;
60
61 do_div(tick_length_base, NTP_INTERVAL_FREQ);
70bc42f9
AB
62}
63
b0ee7556
RZ
64/**
65 * ntp_clear - Clears the NTP state variables
66 *
67 * Must be called while holding a write on the xtime_lock
68 */
69void ntp_clear(void)
70{
71 time_adjust = 0; /* stop active adjtime() */
72 time_status |= STA_UNSYNC;
73 time_maxerror = NTP_PHASE_LIMIT;
74 time_esterror = NTP_PHASE_LIMIT;
75
76 ntp_update_frequency();
77
78 tick_length = tick_length_base;
3d3675cc 79 time_offset = 0;
b0ee7556
RZ
80}
81
4c7ee8de 82/*
83 * this routine handles the overflow of the microsecond field
84 *
85 * The tricky bits of code to handle the accurate clock support
86 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
87 * They were originally developed for SUN and DEC kernels.
88 * All the kudos should go to Dave for this stuff.
89 */
90void second_overflow(void)
91{
3d3675cc 92 long time_adj;
4c7ee8de 93
94 /* Bump the maxerror field */
97eebe13 95 time_maxerror += MAXFREQ >> SHIFT_USEC;
4c7ee8de 96 if (time_maxerror > NTP_PHASE_LIMIT) {
97 time_maxerror = NTP_PHASE_LIMIT;
98 time_status |= STA_UNSYNC;
99 }
100
101 /*
102 * Leap second processing. If in leap-insert state at the end of the
103 * day, the system clock is set back one second; if in leap-delete
104 * state, the system clock is set ahead one second. The microtime()
105 * routine or external clock driver will insure that reported time is
106 * always monotonic. The ugly divides should be replaced.
107 */
108 switch (time_state) {
109 case TIME_OK:
110 if (time_status & STA_INS)
111 time_state = TIME_INS;
112 else if (time_status & STA_DEL)
113 time_state = TIME_DEL;
114 break;
115 case TIME_INS:
116 if (xtime.tv_sec % 86400 == 0) {
117 xtime.tv_sec--;
118 wall_to_monotonic.tv_sec++;
4c7ee8de 119 time_state = TIME_OOP;
4c7ee8de 120 printk(KERN_NOTICE "Clock: inserting leap second "
121 "23:59:60 UTC\n");
122 }
123 break;
124 case TIME_DEL:
125 if ((xtime.tv_sec + 1) % 86400 == 0) {
126 xtime.tv_sec++;
127 wall_to_monotonic.tv_sec--;
4c7ee8de 128 time_state = TIME_WAIT;
4c7ee8de 129 printk(KERN_NOTICE "Clock: deleting leap second "
130 "23:59:59 UTC\n");
131 }
132 break;
133 case TIME_OOP:
134 time_state = TIME_WAIT;
135 break;
136 case TIME_WAIT:
137 if (!(time_status & (STA_INS | STA_DEL)))
138 time_state = TIME_OK;
139 }
140
141 /*
f1992393
RZ
142 * Compute the phase adjustment for the next second. The offset is
143 * reduced by a fixed factor times the time constant.
4c7ee8de 144 */
b0ee7556 145 tick_length = tick_length_base;
f1992393 146 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
3d3675cc
RZ
147 time_offset -= time_adj;
148 tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE);
4c7ee8de 149
8f807f8d
RZ
150 if (unlikely(time_adjust)) {
151 if (time_adjust > MAX_TICKADJ) {
152 time_adjust -= MAX_TICKADJ;
153 tick_length += MAX_TICKADJ_SCALED;
154 } else if (time_adjust < -MAX_TICKADJ) {
155 time_adjust += MAX_TICKADJ;
156 tick_length -= MAX_TICKADJ_SCALED;
157 } else {
8f807f8d 158 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
f4304ab2 159 NTP_INTERVAL_FREQ) << TICK_LENGTH_SHIFT;
bb1d8605 160 time_adjust = 0;
8f807f8d 161 }
4c7ee8de 162 }
163}
164
165/*
166 * Return how long ticks are at the moment, that is, how much time
167 * update_wall_time_one_tick will add to xtime next time we call it
168 * (assuming no calls to do_adjtimex in the meantime).
169 * The return value is in fixed-point nanoseconds shifted by the
170 * specified number of bits to the right of the binary point.
171 * This function has no side-effects.
172 */
173u64 current_tick_length(void)
174{
8f807f8d 175 return tick_length;
4c7ee8de 176}
177
178
179void __attribute__ ((weak)) notify_arch_cmos_timer(void)
180{
181 return;
182}
183
184/* adjtimex mainly allows reading (and writing, if superuser) of
185 * kernel time-keeping variables. used by xntpd.
186 */
187int do_adjtimex(struct timex *txc)
188{
d62ac21a 189 long mtemp, save_adjust, rem;
f1992393 190 s64 freq_adj, temp64;
4c7ee8de 191 int result;
192
193 /* In order to modify anything, you gotta be super-user! */
194 if (txc->modes && !capable(CAP_SYS_TIME))
195 return -EPERM;
196
197 /* Now we validate the data before disabling interrupts */
198
199 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
200 /* singleshot must not be used with any other mode bits */
201 if (txc->modes != ADJ_OFFSET_SINGLESHOT)
202 return -EINVAL;
203
204 if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
205 /* adjustment Offset limited to +- .512 seconds */
206 if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
207 return -EINVAL;
208
209 /* if the quartz is off by more than 10% something is VERY wrong ! */
210 if (txc->modes & ADJ_TICK)
211 if (txc->tick < 900000/USER_HZ ||
212 txc->tick > 1100000/USER_HZ)
213 return -EINVAL;
214
215 write_seqlock_irq(&xtime_lock);
216 result = time_state; /* mostly `TIME_OK' */
217
218 /* Save for later - semantics of adjtime is to return old value */
8f807f8d 219 save_adjust = time_adjust;
4c7ee8de 220
221#if 0 /* STA_CLOCKERR is never set yet */
222 time_status &= ~STA_CLOCKERR; /* reset STA_CLOCKERR */
223#endif
224 /* If there are input parameters, then process them */
225 if (txc->modes)
226 {
227 if (txc->modes & ADJ_STATUS) /* only set allowed bits */
228 time_status = (txc->status & ~STA_RONLY) |
229 (time_status & STA_RONLY);
230
231 if (txc->modes & ADJ_FREQUENCY) { /* p. 22 */
232 if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
233 result = -EINVAL;
234 goto leave;
235 }
f4304ab2 236 time_freq = ((s64)txc->freq * NSEC_PER_USEC)
237 >> (SHIFT_USEC - SHIFT_NSEC);
4c7ee8de 238 }
239
240 if (txc->modes & ADJ_MAXERROR) {
241 if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
242 result = -EINVAL;
243 goto leave;
244 }
245 time_maxerror = txc->maxerror;
246 }
247
248 if (txc->modes & ADJ_ESTERROR) {
249 if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
250 result = -EINVAL;
251 goto leave;
252 }
253 time_esterror = txc->esterror;
254 }
255
256 if (txc->modes & ADJ_TIMECONST) { /* p. 24 */
257 if (txc->constant < 0) { /* NTP v4 uses values > 6 */
258 result = -EINVAL;
259 goto leave;
260 }
f1992393 261 time_constant = min(txc->constant + 4, (long)MAXTC);
4c7ee8de 262 }
263
264 if (txc->modes & ADJ_OFFSET) { /* values checked earlier */
265 if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
266 /* adjtime() is independent from ntp_adjtime() */
8f807f8d 267 time_adjust = txc->offset;
4c7ee8de 268 }
269 else if (time_status & STA_PLL) {
d62ac21a 270 time_offset = txc->offset * NSEC_PER_USEC;
4c7ee8de 271
272 /*
273 * Scale the phase adjustment and
274 * clamp to the operating range.
275 */
d62ac21a 276 time_offset = min(time_offset, (s64)MAXPHASE * NSEC_PER_USEC);
277 time_offset = max(time_offset, (s64)-MAXPHASE * NSEC_PER_USEC);
4c7ee8de 278
279 /*
280 * Select whether the frequency is to be controlled
281 * and in which mode (PLL or FLL). Clamp to the operating
282 * range. Ugly multiply/divide should be replaced someday.
283 */
284
285 if (time_status & STA_FREQHOLD || time_reftime == 0)
286 time_reftime = xtime.tv_sec;
287 mtemp = xtime.tv_sec - time_reftime;
288 time_reftime = xtime.tv_sec;
f1992393 289
d62ac21a 290 freq_adj = time_offset * mtemp;
f1992393
RZ
291 freq_adj = shift_right(freq_adj, time_constant * 2 +
292 (SHIFT_PLL + 2) * 2 - SHIFT_NSEC);
293 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
d62ac21a 294 temp64 = time_offset << (SHIFT_NSEC - SHIFT_FLL);
f1992393
RZ
295 if (time_offset < 0) {
296 temp64 = -temp64;
297 do_div(temp64, mtemp);
298 freq_adj -= temp64;
299 } else {
300 do_div(temp64, mtemp);
301 freq_adj += temp64;
302 }
4c7ee8de 303 }
04b617e7
RZ
304 freq_adj += time_freq;
305 freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC);
306 time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC);
d62ac21a 307 time_offset = div_long_long_rem_signed(time_offset,
308 NTP_INTERVAL_FREQ,
309 &rem);
310 time_offset <<= SHIFT_UPDATE;
4c7ee8de 311 } /* STA_PLL */
312 } /* txc->modes & ADJ_OFFSET */
b0ee7556 313 if (txc->modes & ADJ_TICK)
4c7ee8de 314 tick_usec = txc->tick;
b0ee7556 315
dc6a43e4 316 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
b0ee7556 317 ntp_update_frequency();
4c7ee8de 318 } /* txc->modes */
319leave: if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0)
320 result = TIME_ERROR;
321
322 if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
d62ac21a 323 txc->offset = save_adjust;
3d3675cc 324 else
d62ac21a 325 txc->offset = ((long)shift_right(time_offset, SHIFT_UPDATE)) *
326 NTP_INTERVAL_FREQ / 1000;
327 txc->freq = (time_freq / NSEC_PER_USEC) <<
328 (SHIFT_USEC - SHIFT_NSEC);
4c7ee8de 329 txc->maxerror = time_maxerror;
330 txc->esterror = time_esterror;
331 txc->status = time_status;
332 txc->constant = time_constant;
70bc42f9 333 txc->precision = 1;
97eebe13 334 txc->tolerance = MAXFREQ;
4c7ee8de 335 txc->tick = tick_usec;
336
337 /* PPS is not implemented, so these are zero */
338 txc->ppsfreq = 0;
339 txc->jitter = 0;
340 txc->shift = 0;
341 txc->stabil = 0;
342 txc->jitcnt = 0;
343 txc->calcnt = 0;
344 txc->errcnt = 0;
345 txc->stbcnt = 0;
346 write_sequnlock_irq(&xtime_lock);
347 do_gettimeofday(&txc->time);
348 notify_arch_cmos_timer();
349 return(result);
350}
This page took 0.546939 seconds and 5 git commands to generate.