1
2
3
4
5
6
7
8
9
10
11#include <linux/module.h>
12#include <linux/interrupt.h>
13#include <linux/percpu.h>
14#include <linux/init.h>
15#include <linux/mm.h>
16#include <linux/sched.h>
17#include <linux/sysdev.h>
18#include <linux/clocksource.h>
19#include <linux/jiffies.h>
20#include <linux/time.h>
21#include <linux/tick.h>
22#include <linux/stop_machine.h>
23
24
25struct timekeeper {
26
27 struct clocksource *clock;
28
29 int shift;
30
31
32 cycle_t cycle_interval;
33
34 u64 xtime_interval;
35
36 u32 raw_interval;
37
38
39 u64 xtime_nsec;
40
41
42 s64 ntp_error;
43
44
45 int ntp_error_shift;
46
47 u32 mult;
48};
49
50struct timekeeper timekeeper;
51
52
53
54
55
56
57
58
59
60
61
62static void timekeeper_setup_internals(struct clocksource *clock)
63{
64 cycle_t interval;
65 u64 tmp;
66
67 timekeeper.clock = clock;
68 clock->cycle_last = clock->read(clock);
69
70
71 tmp = NTP_INTERVAL_LENGTH;
72 tmp <<= clock->shift;
73 tmp += clock->mult/2;
74 do_div(tmp, clock->mult);
75 if (tmp == 0)
76 tmp = 1;
77
78 interval = (cycle_t) tmp;
79 timekeeper.cycle_interval = interval;
80
81
82 timekeeper.xtime_interval = (u64) interval * clock->mult;
83 timekeeper.raw_interval =
84 ((u64) interval * clock->mult) >> clock->shift;
85
86 timekeeper.xtime_nsec = 0;
87 timekeeper.shift = clock->shift;
88
89 timekeeper.ntp_error = 0;
90 timekeeper.ntp_error_shift = NTP_SCALE_SHIFT - clock->shift;
91
92
93
94
95
96
97 timekeeper.mult = clock->mult;
98}
99
100
101static inline s64 timekeeping_get_ns(void)
102{
103 cycle_t cycle_now, cycle_delta;
104 struct clocksource *clock;
105
106
107 clock = timekeeper.clock;
108 cycle_now = clock->read(clock);
109
110
111 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
112
113
114 return clocksource_cyc2ns(cycle_delta, timekeeper.mult,
115 timekeeper.shift);
116}
117
118static inline s64 timekeeping_get_ns_raw(void)
119{
120 cycle_t cycle_now, cycle_delta;
121 struct clocksource *clock;
122
123
124 clock = timekeeper.clock;
125 cycle_now = clock->read(clock);
126
127
128 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
129
130
131 return clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
132}
133
134
135
136
137
138__cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock);
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156struct timespec xtime __attribute__ ((aligned (16)));
157struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
158static struct timespec total_sleep_time;
159
160
161
162
163struct timespec raw_time;
164
165
166int __read_mostly timekeeping_suspended;
167
168static struct timespec xtime_cache __attribute__ ((aligned (16)));
169void update_xtime_cache(u64 nsec)
170{
171 xtime_cache = xtime;
172 timespec_add_ns(&xtime_cache, nsec);
173}
174
175
176void timekeeping_leap_insert(int leapsecond)
177{
178 xtime.tv_sec += leapsecond;
179 wall_to_monotonic.tv_sec -= leapsecond;
180 update_vsyscall(&xtime, timekeeper.clock);
181}
182
183#ifdef CONFIG_GENERIC_TIME
184
185
186
187
188
189
190
191
192static void timekeeping_forward_now(void)
193{
194 cycle_t cycle_now, cycle_delta;
195 struct clocksource *clock;
196 s64 nsec;
197
198 clock = timekeeper.clock;
199 cycle_now = clock->read(clock);
200 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
201 clock->cycle_last = cycle_now;
202
203 nsec = clocksource_cyc2ns(cycle_delta, timekeeper.mult,
204 timekeeper.shift);
205
206
207 nsec += arch_gettimeoffset();
208
209 timespec_add_ns(&xtime, nsec);
210
211 nsec = clocksource_cyc2ns(cycle_delta, clock->mult, clock->shift);
212 timespec_add_ns(&raw_time, nsec);
213}
214
215
216
217
218
219
220
221void getnstimeofday(struct timespec *ts)
222{
223 unsigned long seq;
224 s64 nsecs;
225
226 WARN_ON(timekeeping_suspended);
227
228 do {
229 seq = read_seqbegin(&xtime_lock);
230
231 *ts = xtime;
232 nsecs = timekeeping_get_ns();
233
234
235 nsecs += arch_gettimeoffset();
236
237 } while (read_seqretry(&xtime_lock, seq));
238
239 timespec_add_ns(ts, nsecs);
240}
241
242EXPORT_SYMBOL(getnstimeofday);
243
244ktime_t ktime_get(void)
245{
246 unsigned int seq;
247 s64 secs, nsecs;
248
249 WARN_ON(timekeeping_suspended);
250
251 do {
252 seq = read_seqbegin(&xtime_lock);
253 secs = xtime.tv_sec + wall_to_monotonic.tv_sec;
254 nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
255 nsecs += timekeeping_get_ns();
256
257 } while (read_seqretry(&xtime_lock, seq));
258
259
260
261
262 return ktime_add_ns(ktime_set(secs, 0), nsecs);
263}
264EXPORT_SYMBOL_GPL(ktime_get);
265
266
267
268
269
270
271
272
273
274void ktime_get_ts(struct timespec *ts)
275{
276 struct timespec tomono;
277 unsigned int seq;
278 s64 nsecs;
279
280 WARN_ON(timekeeping_suspended);
281
282 do {
283 seq = read_seqbegin(&xtime_lock);
284 *ts = xtime;
285 tomono = wall_to_monotonic;
286 nsecs = timekeeping_get_ns();
287
288 } while (read_seqretry(&xtime_lock, seq));
289
290 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
291 ts->tv_nsec + tomono.tv_nsec + nsecs);
292}
293EXPORT_SYMBOL_GPL(ktime_get_ts);
294
295
296
297
298
299
300
301void do_gettimeofday(struct timeval *tv)
302{
303 struct timespec now;
304
305 getnstimeofday(&now);
306 tv->tv_sec = now.tv_sec;
307 tv->tv_usec = now.tv_nsec/1000;
308}
309
310EXPORT_SYMBOL(do_gettimeofday);
311
312
313
314
315
316
317int do_settimeofday(struct timespec *tv)
318{
319 struct timespec ts_delta;
320 unsigned long flags;
321
322 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
323 return -EINVAL;
324
325 write_seqlock_irqsave(&xtime_lock, flags);
326
327 timekeeping_forward_now();
328
329 ts_delta.tv_sec = tv->tv_sec - xtime.tv_sec;
330 ts_delta.tv_nsec = tv->tv_nsec - xtime.tv_nsec;
331 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts_delta);
332
333 xtime = *tv;
334
335 update_xtime_cache(0);
336
337 timekeeper.ntp_error = 0;
338 ntp_clear();
339
340 update_vsyscall(&xtime, timekeeper.clock);
341
342 write_sequnlock_irqrestore(&xtime_lock, flags);
343
344
345 clock_was_set();
346
347 return 0;
348}
349
350EXPORT_SYMBOL(do_settimeofday);
351
352
353
354
355
356
357static int change_clocksource(void *data)
358{
359 struct clocksource *new, *old;
360
361 new = (struct clocksource *) data;
362
363 timekeeping_forward_now();
364 if (!new->enable || new->enable(new) == 0) {
365 old = timekeeper.clock;
366 timekeeper_setup_internals(new);
367 if (old->disable)
368 old->disable(old);
369 }
370 return 0;
371}
372
373
374
375
376
377
378
379
380void timekeeping_notify(struct clocksource *clock)
381{
382 if (timekeeper.clock == clock)
383 return;
384 stop_machine(change_clocksource, clock, NULL);
385 tick_clock_notify();
386}
387
388#else
389
390static inline void timekeeping_forward_now(void) { }
391
392
393
394
395
396
397ktime_t ktime_get(void)
398{
399 struct timespec now;
400
401 ktime_get_ts(&now);
402
403 return timespec_to_ktime(now);
404}
405EXPORT_SYMBOL_GPL(ktime_get);
406
407
408
409
410
411
412
413
414
415void ktime_get_ts(struct timespec *ts)
416{
417 struct timespec tomono;
418 unsigned long seq;
419
420 do {
421 seq = read_seqbegin(&xtime_lock);
422 getnstimeofday(ts);
423 tomono = wall_to_monotonic;
424
425 } while (read_seqretry(&xtime_lock, seq));
426
427 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
428 ts->tv_nsec + tomono.tv_nsec);
429}
430EXPORT_SYMBOL_GPL(ktime_get_ts);
431
432#endif
433
434
435
436
437
438
439ktime_t ktime_get_real(void)
440{
441 struct timespec now;
442
443 getnstimeofday(&now);
444
445 return timespec_to_ktime(now);
446}
447EXPORT_SYMBOL_GPL(ktime_get_real);
448
449
450
451
452
453
454
455void getrawmonotonic(struct timespec *ts)
456{
457 unsigned long seq;
458 s64 nsecs;
459
460 do {
461 seq = read_seqbegin(&xtime_lock);
462 nsecs = timekeeping_get_ns_raw();
463 *ts = raw_time;
464
465 } while (read_seqretry(&xtime_lock, seq));
466
467 timespec_add_ns(ts, nsecs);
468}
469EXPORT_SYMBOL(getrawmonotonic);
470
471
472
473
474
475int timekeeping_valid_for_hres(void)
476{
477 unsigned long seq;
478 int ret;
479
480 do {
481 seq = read_seqbegin(&xtime_lock);
482
483 ret = timekeeper.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
484
485 } while (read_seqretry(&xtime_lock, seq));
486
487 return ret;
488}
489
490
491
492
493
494
495
496
497
498
499void __attribute__((weak)) read_persistent_clock(struct timespec *ts)
500{
501 ts->tv_sec = 0;
502 ts->tv_nsec = 0;
503}
504
505
506
507
508
509
510
511
512
513
514void __attribute__((weak)) read_boot_clock(struct timespec *ts)
515{
516 ts->tv_sec = 0;
517 ts->tv_nsec = 0;
518}
519
520
521
522
523void __init timekeeping_init(void)
524{
525 struct clocksource *clock;
526 unsigned long flags;
527 struct timespec now, boot;
528
529 read_persistent_clock(&now);
530 read_boot_clock(&boot);
531
532 write_seqlock_irqsave(&xtime_lock, flags);
533
534 ntp_init();
535
536 clock = clocksource_default_clock();
537 if (clock->enable)
538 clock->enable(clock);
539 timekeeper_setup_internals(clock);
540
541 xtime.tv_sec = now.tv_sec;
542 xtime.tv_nsec = now.tv_nsec;
543 raw_time.tv_sec = 0;
544 raw_time.tv_nsec = 0;
545 if (boot.tv_sec == 0 && boot.tv_nsec == 0) {
546 boot.tv_sec = xtime.tv_sec;
547 boot.tv_nsec = xtime.tv_nsec;
548 }
549 set_normalized_timespec(&wall_to_monotonic,
550 -boot.tv_sec, -boot.tv_nsec);
551 update_xtime_cache(0);
552 total_sleep_time.tv_sec = 0;
553 total_sleep_time.tv_nsec = 0;
554 write_sequnlock_irqrestore(&xtime_lock, flags);
555}
556
557
558static struct timespec timekeeping_suspend_time;
559
560
561
562
563
564
565
566
567
568static int timekeeping_resume(struct sys_device *dev)
569{
570 unsigned long flags;
571 struct timespec ts;
572
573 read_persistent_clock(&ts);
574
575 clocksource_resume();
576
577 write_seqlock_irqsave(&xtime_lock, flags);
578
579 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
580 ts = timespec_sub(ts, timekeeping_suspend_time);
581 xtime = timespec_add_safe(xtime, ts);
582 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts);
583 total_sleep_time = timespec_add_safe(total_sleep_time, ts);
584 }
585 update_xtime_cache(0);
586
587 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
588 timekeeper.ntp_error = 0;
589 timekeeping_suspended = 0;
590 write_sequnlock_irqrestore(&xtime_lock, flags);
591
592 touch_softlockup_watchdog();
593
594 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
595
596
597 hres_timers_resume();
598
599 return 0;
600}
601
602static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
603{
604 unsigned long flags;
605
606 read_persistent_clock(&timekeeping_suspend_time);
607
608 write_seqlock_irqsave(&xtime_lock, flags);
609 timekeeping_forward_now();
610 timekeeping_suspended = 1;
611 write_sequnlock_irqrestore(&xtime_lock, flags);
612
613 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
614
615 return 0;
616}
617
618
619static struct sysdev_class timekeeping_sysclass = {
620 .name = "timekeeping",
621 .resume = timekeeping_resume,
622 .suspend = timekeeping_suspend,
623};
624
625static struct sys_device device_timer = {
626 .id = 0,
627 .cls = &timekeeping_sysclass,
628};
629
630static int __init timekeeping_init_device(void)
631{
632 int error = sysdev_class_register(&timekeeping_sysclass);
633 if (!error)
634 error = sysdev_register(&device_timer);
635 return error;
636}
637
638device_initcall(timekeeping_init_device);
639
640
641
642
643
644static __always_inline int timekeeping_bigadjust(s64 error, s64 *interval,
645 s64 *offset)
646{
647 s64 tick_error, i;
648 u32 look_ahead, adj;
649 s32 error2, mult;
650
651
652
653
654
655
656
657
658
659
660 error2 = timekeeper.ntp_error >> (NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);
661 error2 = abs(error2);
662 for (look_ahead = 0; error2 > 0; look_ahead++)
663 error2 >>= 2;
664
665
666
667
668
669 tick_error = tick_length >> (timekeeper.ntp_error_shift + 1);
670 tick_error -= timekeeper.xtime_interval >> 1;
671 error = ((error - tick_error) >> look_ahead) + tick_error;
672
673
674 i = *interval;
675 mult = 1;
676 if (error < 0) {
677 error = -error;
678 *interval = -*interval;
679 *offset = -*offset;
680 mult = -1;
681 }
682 for (adj = 0; error > i; adj++)
683 error >>= 1;
684
685 *interval <<= adj;
686 *offset <<= adj;
687 return mult << adj;
688}
689
690
691
692
693
694
695static void timekeeping_adjust(s64 offset)
696{
697 s64 error, interval = timekeeper.cycle_interval;
698 int adj;
699
700 error = timekeeper.ntp_error >> (timekeeper.ntp_error_shift - 1);
701 if (error > interval) {
702 error >>= 2;
703 if (likely(error <= interval))
704 adj = 1;
705 else
706 adj = timekeeping_bigadjust(error, &interval, &offset);
707 } else if (error < -interval) {
708 error >>= 2;
709 if (likely(error >= -interval)) {
710 adj = -1;
711 interval = -interval;
712 offset = -offset;
713 } else
714 adj = timekeeping_bigadjust(error, &interval, &offset);
715 } else
716 return;
717
718 timekeeper.mult += adj;
719 timekeeper.xtime_interval += interval;
720 timekeeper.xtime_nsec -= offset;
721 timekeeper.ntp_error -= (interval - offset) <<
722 timekeeper.ntp_error_shift;
723}
724
725
726
727
728
729
730void update_wall_time(void)
731{
732 struct clocksource *clock;
733 cycle_t offset;
734 u64 nsecs;
735
736
737 if (unlikely(timekeeping_suspended))
738 return;
739
740 clock = timekeeper.clock;
741#ifdef CONFIG_GENERIC_TIME
742 offset = (clock->read(clock) - clock->cycle_last) & clock->mask;
743#else
744 offset = timekeeper.cycle_interval;
745#endif
746 timekeeper.xtime_nsec = (s64)xtime.tv_nsec << timekeeper.shift;
747
748
749
750
751 while (offset >= timekeeper.cycle_interval) {
752 u64 nsecps = (u64)NSEC_PER_SEC << timekeeper.shift;
753
754
755 offset -= timekeeper.cycle_interval;
756 clock->cycle_last += timekeeper.cycle_interval;
757
758 timekeeper.xtime_nsec += timekeeper.xtime_interval;
759 if (timekeeper.xtime_nsec >= nsecps) {
760 timekeeper.xtime_nsec -= nsecps;
761 xtime.tv_sec++;
762 second_overflow();
763 }
764
765 raw_time.tv_nsec += timekeeper.raw_interval;
766 if (raw_time.tv_nsec >= NSEC_PER_SEC) {
767 raw_time.tv_nsec -= NSEC_PER_SEC;
768 raw_time.tv_sec++;
769 }
770
771
772 timekeeper.ntp_error += tick_length;
773 timekeeper.ntp_error -= timekeeper.xtime_interval <<
774 timekeeper.ntp_error_shift;
775 }
776
777
778 timekeeping_adjust(offset);
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796 if (unlikely((s64)timekeeper.xtime_nsec < 0)) {
797 s64 neg = -(s64)timekeeper.xtime_nsec;
798 timekeeper.xtime_nsec = 0;
799 timekeeper.ntp_error += neg << timekeeper.ntp_error_shift;
800 }
801
802
803
804
805 xtime.tv_nsec = ((s64) timekeeper.xtime_nsec >> timekeeper.shift) + 1;
806 timekeeper.xtime_nsec -= (s64) xtime.tv_nsec << timekeeper.shift;
807 timekeeper.ntp_error += timekeeper.xtime_nsec <<
808 timekeeper.ntp_error_shift;
809
810 nsecs = clocksource_cyc2ns(offset, timekeeper.mult, timekeeper.shift);
811 update_xtime_cache(nsecs);
812
813
814 update_vsyscall(&xtime, timekeeper.clock);
815}
816
817
818
819
820
821
822
823
824
825
826
827
828void getboottime(struct timespec *ts)
829{
830 struct timespec boottime = {
831 .tv_sec = wall_to_monotonic.tv_sec + total_sleep_time.tv_sec,
832 .tv_nsec = wall_to_monotonic.tv_nsec + total_sleep_time.tv_nsec
833 };
834
835 set_normalized_timespec(ts, -boottime.tv_sec, -boottime.tv_nsec);
836}
837
838
839
840
841
842void monotonic_to_bootbased(struct timespec *ts)
843{
844 *ts = timespec_add_safe(*ts, total_sleep_time);
845}
846
847unsigned long get_seconds(void)
848{
849 return xtime_cache.tv_sec;
850}
851EXPORT_SYMBOL(get_seconds);
852
853struct timespec __current_kernel_time(void)
854{
855 return xtime_cache;
856}
857
858struct timespec current_kernel_time(void)
859{
860 struct timespec now;
861 unsigned long seq;
862
863 do {
864 seq = read_seqbegin(&xtime_lock);
865
866 now = xtime_cache;
867 } while (read_seqretry(&xtime_lock, seq));
868
869 return now;
870}
871EXPORT_SYMBOL(current_kernel_time);
872
873struct timespec get_monotonic_coarse(void)
874{
875 struct timespec now, mono;
876 unsigned long seq;
877
878 do {
879 seq = read_seqbegin(&xtime_lock);
880
881 now = xtime_cache;
882 mono = wall_to_monotonic;
883 } while (read_seqretry(&xtime_lock, seq));
884
885 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
886 now.tv_nsec + mono.tv_nsec);
887 return now;
888}
889