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