1
2
3
4
5
6
7
8
9
10
11#include <linux/mm.h>
12#include <linux/time.h>
13#include <linux/timex.h>
14#include <linux/jiffies.h>
15#include <linux/hrtimer.h>
16#include <linux/capability.h>
17#include <linux/math64.h>
18#include <linux/clocksource.h>
19#include <linux/workqueue.h>
20#include <asm/timex.h>
21
22
23
24
25unsigned long tick_usec = TICK_USEC;
26unsigned long tick_nsec;
27u64 tick_length;
28static u64 tick_length_base;
29
30static struct hrtimer leap_timer;
31
32#define MAX_TICKADJ 500
33#define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \
34 NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
35
36
37
38
39
40static int time_state = TIME_OK;
41int time_status = STA_UNSYNC;
42static long time_tai;
43static s64 time_offset;
44static long time_constant = 2;
45long time_maxerror = NTP_PHASE_LIMIT;
46long time_esterror = NTP_PHASE_LIMIT;
47static s64 time_freq;
48static long time_reftime;
49long time_adjust;
50static long ntp_tick_adj;
51
52static void ntp_update_frequency(void)
53{
54 u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
55 << NTP_SCALE_SHIFT;
56 second_length += (s64)ntp_tick_adj << NTP_SCALE_SHIFT;
57 second_length += time_freq;
58
59 tick_length_base = second_length;
60
61 tick_nsec = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
62 tick_length_base = div_u64(tick_length_base, NTP_INTERVAL_FREQ);
63}
64
65static void ntp_update_offset(long offset)
66{
67 long mtemp;
68 s64 freq_adj;
69
70 if (!(time_status & STA_PLL))
71 return;
72
73 if (!(time_status & STA_NANO))
74 offset *= NSEC_PER_USEC;
75
76
77
78
79
80 offset = min(offset, MAXPHASE);
81 offset = max(offset, -MAXPHASE);
82
83
84
85
86
87 if (time_status & STA_FREQHOLD || time_reftime == 0)
88 time_reftime = xtime.tv_sec;
89 mtemp = xtime.tv_sec - time_reftime;
90 time_reftime = xtime.tv_sec;
91
92 freq_adj = (s64)offset * mtemp;
93 freq_adj <<= NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant);
94 time_status &= ~STA_MODE;
95 if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) {
96 freq_adj += div_s64((s64)offset << (NTP_SCALE_SHIFT - SHIFT_FLL),
97 mtemp);
98 time_status |= STA_MODE;
99 }
100 freq_adj += time_freq;
101 freq_adj = min(freq_adj, MAXFREQ_SCALED);
102 time_freq = max(freq_adj, -MAXFREQ_SCALED);
103
104 time_offset = div_s64((s64)offset << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
105}
106
107
108
109
110
111
112void ntp_clear(void)
113{
114 time_adjust = 0;
115 time_status |= STA_UNSYNC;
116 time_maxerror = NTP_PHASE_LIMIT;
117 time_esterror = NTP_PHASE_LIMIT;
118
119 ntp_update_frequency();
120
121 tick_length = tick_length_base;
122 time_offset = 0;
123}
124
125
126
127
128
129
130static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
131{
132 enum hrtimer_restart res = HRTIMER_NORESTART;
133
134 write_seqlock_irq(&xtime_lock);
135
136 switch (time_state) {
137 case TIME_OK:
138 break;
139 case TIME_INS:
140 xtime.tv_sec--;
141 wall_to_monotonic.tv_sec++;
142 time_state = TIME_OOP;
143 printk(KERN_NOTICE "Clock: "
144 "inserting leap second 23:59:60 UTC\n");
145 hrtimer_add_expires_ns(&leap_timer, NSEC_PER_SEC);
146 res = HRTIMER_RESTART;
147 break;
148 case TIME_DEL:
149 xtime.tv_sec++;
150 time_tai--;
151 wall_to_monotonic.tv_sec--;
152 time_state = TIME_WAIT;
153 printk(KERN_NOTICE "Clock: "
154 "deleting leap second 23:59:59 UTC\n");
155 break;
156 case TIME_OOP:
157 time_tai++;
158 time_state = TIME_WAIT;
159
160 case TIME_WAIT:
161 if (!(time_status & (STA_INS | STA_DEL)))
162 time_state = TIME_OK;
163 break;
164 }
165 update_vsyscall(&xtime, clock);
166
167 write_sequnlock_irq(&xtime_lock);
168
169 return res;
170}
171
172
173
174
175
176
177
178
179
180void second_overflow(void)
181{
182 s64 time_adj;
183
184
185 time_maxerror += MAXFREQ / NSEC_PER_USEC;
186 if (time_maxerror > NTP_PHASE_LIMIT) {
187 time_maxerror = NTP_PHASE_LIMIT;
188 time_status |= STA_UNSYNC;
189 }
190
191
192
193
194
195 tick_length = tick_length_base;
196 time_adj = shift_right(time_offset, SHIFT_PLL + time_constant);
197 time_offset -= time_adj;
198 tick_length += time_adj;
199
200 if (unlikely(time_adjust)) {
201 if (time_adjust > MAX_TICKADJ) {
202 time_adjust -= MAX_TICKADJ;
203 tick_length += MAX_TICKADJ_SCALED;
204 } else if (time_adjust < -MAX_TICKADJ) {
205 time_adjust += MAX_TICKADJ;
206 tick_length -= MAX_TICKADJ_SCALED;
207 } else {
208 tick_length += (s64)(time_adjust * NSEC_PER_USEC /
209 NTP_INTERVAL_FREQ) << NTP_SCALE_SHIFT;
210 time_adjust = 0;
211 }
212 }
213}
214
215#ifdef CONFIG_GENERIC_CMOS_UPDATE
216
217
218int no_sync_cmos_clock __read_mostly;
219
220static void sync_cmos_clock(struct work_struct *work);
221
222static DECLARE_DELAYED_WORK(sync_cmos_work, sync_cmos_clock);
223
224static void sync_cmos_clock(struct work_struct *work)
225{
226 struct timespec now, next;
227 int fail = 1;
228
229
230
231
232
233
234
235
236 if (!ntp_synced())
237
238
239
240
241 return;
242
243 getnstimeofday(&now);
244 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2)
245 fail = update_persistent_clock(now);
246
247 next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2);
248 if (next.tv_nsec <= 0)
249 next.tv_nsec += NSEC_PER_SEC;
250
251 if (!fail)
252 next.tv_sec = 659;
253 else
254 next.tv_sec = 0;
255
256 if (next.tv_nsec >= NSEC_PER_SEC) {
257 next.tv_sec++;
258 next.tv_nsec -= NSEC_PER_SEC;
259 }
260 schedule_delayed_work(&sync_cmos_work, timespec_to_jiffies(&next));
261}
262
263static void notify_cmos_timer(void)
264{
265 if (!no_sync_cmos_clock)
266 schedule_delayed_work(&sync_cmos_work, 0);
267}
268
269#else
270static inline void notify_cmos_timer(void) { }
271#endif
272
273
274
275
276int do_adjtimex(struct timex *txc)
277{
278 struct timespec ts;
279 int result;
280
281
282 if (txc->modes & ADJ_ADJTIME) {
283
284 if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
285 return -EINVAL;
286 if (!(txc->modes & ADJ_OFFSET_READONLY) &&
287 !capable(CAP_SYS_TIME))
288 return -EPERM;
289 } else {
290
291 if (txc->modes && !capable(CAP_SYS_TIME))
292 return -EPERM;
293
294
295 if (txc->modes & ADJ_TICK &&
296 (txc->tick < 900000/USER_HZ ||
297 txc->tick > 1100000/USER_HZ))
298 return -EINVAL;
299
300 if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
301 hrtimer_cancel(&leap_timer);
302 }
303
304 getnstimeofday(&ts);
305
306 write_seqlock_irq(&xtime_lock);
307
308
309 if (txc->modes & ADJ_ADJTIME) {
310 long save_adjust = time_adjust;
311
312 if (!(txc->modes & ADJ_OFFSET_READONLY)) {
313
314 time_adjust = txc->offset;
315 ntp_update_frequency();
316 }
317 txc->offset = save_adjust;
318 goto adj_done;
319 }
320 if (txc->modes) {
321 long sec;
322
323 if (txc->modes & ADJ_STATUS) {
324 if ((time_status & STA_PLL) &&
325 !(txc->status & STA_PLL)) {
326 time_state = TIME_OK;
327 time_status = STA_UNSYNC;
328 }
329
330 time_status &= STA_RONLY;
331 time_status |= txc->status & ~STA_RONLY;
332
333 switch (time_state) {
334 case TIME_OK:
335 start_timer:
336 sec = ts.tv_sec;
337 if (time_status & STA_INS) {
338 time_state = TIME_INS;
339 sec += 86400 - sec % 86400;
340 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
341 } else if (time_status & STA_DEL) {
342 time_state = TIME_DEL;
343 sec += 86400 - (sec + 1) % 86400;
344 hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
345 }
346 break;
347 case TIME_INS:
348 case TIME_DEL:
349 time_state = TIME_OK;
350 goto start_timer;
351 break;
352 case TIME_WAIT:
353 if (!(time_status & (STA_INS | STA_DEL)))
354 time_state = TIME_OK;
355 break;
356 case TIME_OOP:
357 hrtimer_restart(&leap_timer);
358 break;
359 }
360 }
361
362 if (txc->modes & ADJ_NANO)
363 time_status |= STA_NANO;
364 if (txc->modes & ADJ_MICRO)
365 time_status &= ~STA_NANO;
366
367 if (txc->modes & ADJ_FREQUENCY) {
368 time_freq = (s64)txc->freq * PPM_SCALE;
369 time_freq = min(time_freq, MAXFREQ_SCALED);
370 time_freq = max(time_freq, -MAXFREQ_SCALED);
371 }
372
373 if (txc->modes & ADJ_MAXERROR)
374 time_maxerror = txc->maxerror;
375 if (txc->modes & ADJ_ESTERROR)
376 time_esterror = txc->esterror;
377
378 if (txc->modes & ADJ_TIMECONST) {
379 time_constant = txc->constant;
380 if (!(time_status & STA_NANO))
381 time_constant += 4;
382 time_constant = min(time_constant, (long)MAXTC);
383 time_constant = max(time_constant, 0l);
384 }
385
386 if (txc->modes & ADJ_TAI && txc->constant > 0)
387 time_tai = txc->constant;
388
389 if (txc->modes & ADJ_OFFSET)
390 ntp_update_offset(txc->offset);
391 if (txc->modes & ADJ_TICK)
392 tick_usec = txc->tick;
393
394 if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
395 ntp_update_frequency();
396 }
397
398 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
399 NTP_SCALE_SHIFT);
400 if (!(time_status & STA_NANO))
401 txc->offset /= NSEC_PER_USEC;
402
403adj_done:
404 result = time_state;
405 if (time_status & (STA_UNSYNC|STA_CLOCKERR))
406 result = TIME_ERROR;
407
408 txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
409 (s64)PPM_SCALE_INV, NTP_SCALE_SHIFT);
410 txc->maxerror = time_maxerror;
411 txc->esterror = time_esterror;
412 txc->status = time_status;
413 txc->constant = time_constant;
414 txc->precision = 1;
415 txc->tolerance = MAXFREQ_SCALED / PPM_SCALE;
416 txc->tick = tick_usec;
417 txc->tai = time_tai;
418
419
420 txc->ppsfreq = 0;
421 txc->jitter = 0;
422 txc->shift = 0;
423 txc->stabil = 0;
424 txc->jitcnt = 0;
425 txc->calcnt = 0;
426 txc->errcnt = 0;
427 txc->stbcnt = 0;
428 write_sequnlock_irq(&xtime_lock);
429
430 txc->time.tv_sec = ts.tv_sec;
431 txc->time.tv_usec = ts.tv_nsec;
432 if (!(time_status & STA_NANO))
433 txc->time.tv_usec /= NSEC_PER_USEC;
434
435 notify_cmos_timer();
436
437 return result;
438}
439
440static int __init ntp_tick_adj_setup(char *str)
441{
442 ntp_tick_adj = simple_strtol(str, NULL, 0);
443 return 1;
444}
445
446__setup("ntp_tick_adj=", ntp_tick_adj_setup);
447
448void __init ntp_init(void)
449{
450 ntp_clear();
451 hrtimer_init(&leap_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
452 leap_timer.function = ntp_leap_second;
453}
454