aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/time
diff options
context:
space:
mode:
authorThomas Gleixner <tglx@linutronix.de>2013-04-03 06:27:29 -0400
committerThomas Gleixner <tglx@linutronix.de>2013-04-03 06:27:29 -0400
commit0ed2aef9b3bffe598045b62a31a50d912eee92d8 (patch)
treed7dda12955c838f531727d2775d09c4e04bdf066 /kernel/time
parentcfea7d7e452f57682a0bb55a55e9f79c569558c2 (diff)
parent8011657b9e63cb2e914b9a0f75233b910c1854cb (diff)
Merge branch 'fortglx/3.10/time' of git://git.linaro.org/people/jstultz/linux into timers/core
Diffstat (limited to 'kernel/time')
-rw-r--r--kernel/time/ntp.c18
-rw-r--r--kernel/time/tick-internal.h2
-rw-r--r--kernel/time/timekeeping.c282
3 files changed, 228 insertions, 74 deletions
diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index 072bb066bb7d..59e2749be0fa 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -53,9 +53,6 @@ static int time_state = TIME_OK;
53/* clock status bits: */ 53/* clock status bits: */
54static int time_status = STA_UNSYNC; 54static int time_status = STA_UNSYNC;
55 55
56/* TAI offset (secs): */
57static long time_tai;
58
59/* time adjustment (nsecs): */ 56/* time adjustment (nsecs): */
60static s64 time_offset; 57static s64 time_offset;
61 58
@@ -415,7 +412,6 @@ int second_overflow(unsigned long secs)
415 else if (secs % 86400 == 0) { 412 else if (secs % 86400 == 0) {
416 leap = -1; 413 leap = -1;
417 time_state = TIME_OOP; 414 time_state = TIME_OOP;
418 time_tai++;
419 printk(KERN_NOTICE 415 printk(KERN_NOTICE
420 "Clock: inserting leap second 23:59:60 UTC\n"); 416 "Clock: inserting leap second 23:59:60 UTC\n");
421 } 417 }
@@ -425,7 +421,6 @@ int second_overflow(unsigned long secs)
425 time_state = TIME_OK; 421 time_state = TIME_OK;
426 else if ((secs + 1) % 86400 == 0) { 422 else if ((secs + 1) % 86400 == 0) {
427 leap = 1; 423 leap = 1;
428 time_tai--;
429 time_state = TIME_WAIT; 424 time_state = TIME_WAIT;
430 printk(KERN_NOTICE 425 printk(KERN_NOTICE
431 "Clock: deleting leap second 23:59:59 UTC\n"); 426 "Clock: deleting leap second 23:59:59 UTC\n");
@@ -579,7 +574,9 @@ static inline void process_adj_status(struct timex *txc, struct timespec *ts)
579 * Called with ntp_lock held, so we can access and modify 574 * Called with ntp_lock held, so we can access and modify
580 * all the global NTP state: 575 * all the global NTP state:
581 */ 576 */
582static inline void process_adjtimex_modes(struct timex *txc, struct timespec *ts) 577static inline void process_adjtimex_modes(struct timex *txc,
578 struct timespec *ts,
579 s32 *time_tai)
583{ 580{
584 if (txc->modes & ADJ_STATUS) 581 if (txc->modes & ADJ_STATUS)
585 process_adj_status(txc, ts); 582 process_adj_status(txc, ts);
@@ -613,7 +610,7 @@ static inline void process_adjtimex_modes(struct timex *txc, struct timespec *ts
613 } 610 }
614 611
615 if (txc->modes & ADJ_TAI && txc->constant > 0) 612 if (txc->modes & ADJ_TAI && txc->constant > 0)
616 time_tai = txc->constant; 613 *time_tai = txc->constant;
617 614
618 if (txc->modes & ADJ_OFFSET) 615 if (txc->modes & ADJ_OFFSET)
619 ntp_update_offset(txc->offset); 616 ntp_update_offset(txc->offset);
@@ -632,6 +629,7 @@ static inline void process_adjtimex_modes(struct timex *txc, struct timespec *ts
632int do_adjtimex(struct timex *txc) 629int do_adjtimex(struct timex *txc)
633{ 630{
634 struct timespec ts; 631 struct timespec ts;
632 u32 time_tai, orig_tai;
635 int result; 633 int result;
636 634
637 /* Validate the data before disabling interrupts */ 635 /* Validate the data before disabling interrupts */
@@ -671,6 +669,7 @@ int do_adjtimex(struct timex *txc)
671 } 669 }
672 670
673 getnstimeofday(&ts); 671 getnstimeofday(&ts);
672 orig_tai = time_tai = timekeeping_get_tai_offset();
674 673
675 raw_spin_lock_irq(&ntp_lock); 674 raw_spin_lock_irq(&ntp_lock);
676 675
@@ -687,7 +686,7 @@ int do_adjtimex(struct timex *txc)
687 686
688 /* If there are input parameters, then process them: */ 687 /* If there are input parameters, then process them: */
689 if (txc->modes) 688 if (txc->modes)
690 process_adjtimex_modes(txc, &ts); 689 process_adjtimex_modes(txc, &ts, &time_tai);
691 690
692 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ, 691 txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
693 NTP_SCALE_SHIFT); 692 NTP_SCALE_SHIFT);
@@ -716,6 +715,9 @@ int do_adjtimex(struct timex *txc)
716 715
717 raw_spin_unlock_irq(&ntp_lock); 716 raw_spin_unlock_irq(&ntp_lock);
718 717
718 if (time_tai != orig_tai)
719 timekeeping_set_tai_offset(time_tai);
720
719 txc->time.tv_sec = ts.tv_sec; 721 txc->time.tv_sec = ts.tv_sec;
720 txc->time.tv_usec = ts.tv_nsec; 722 txc->time.tv_usec = ts.tv_nsec;
721 if (!(time_status & STA_NANO)) 723 if (!(time_status & STA_NANO))
diff --git a/kernel/time/tick-internal.h b/kernel/time/tick-internal.h
index 46d9bd02844c..f0299eae4602 100644
--- a/kernel/time/tick-internal.h
+++ b/kernel/time/tick-internal.h
@@ -4,6 +4,8 @@
4#include <linux/hrtimer.h> 4#include <linux/hrtimer.h>
5#include <linux/tick.h> 5#include <linux/tick.h>
6 6
7extern seqlock_t jiffies_lock;
8
7#ifdef CONFIG_GENERIC_CLOCKEVENTS_BUILD 9#ifdef CONFIG_GENERIC_CLOCKEVENTS_BUILD
8 10
9#define TICK_DO_TIMER_NONE -1 11#define TICK_DO_TIMER_NONE -1
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 9a0bc98fbe1d..c5feb7aa3acb 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -23,8 +23,11 @@
23#include <linux/stop_machine.h> 23#include <linux/stop_machine.h>
24#include <linux/pvclock_gtod.h> 24#include <linux/pvclock_gtod.h>
25 25
26#include "tick-internal.h"
26 27
27static struct timekeeper timekeeper; 28static struct timekeeper timekeeper;
29static DEFINE_RAW_SPINLOCK(timekeeper_lock);
30static seqcount_t timekeeper_seq;
28 31
29/* flag for if timekeeping is suspended */ 32/* flag for if timekeeping is suspended */
30int __read_mostly timekeeping_suspended; 33int __read_mostly timekeeping_suspended;
@@ -67,6 +70,7 @@ static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec wtm)
67 tk->wall_to_monotonic = wtm; 70 tk->wall_to_monotonic = wtm;
68 set_normalized_timespec(&tmp, -wtm.tv_sec, -wtm.tv_nsec); 71 set_normalized_timespec(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
69 tk->offs_real = timespec_to_ktime(tmp); 72 tk->offs_real = timespec_to_ktime(tmp);
73 tk->offs_tai = ktime_sub(tk->offs_real, ktime_set(tk->tai_offset, 0));
70} 74}
71 75
72static void tk_set_sleep_time(struct timekeeper *tk, struct timespec t) 76static void tk_set_sleep_time(struct timekeeper *tk, struct timespec t)
@@ -201,8 +205,6 @@ static void update_pvclock_gtod(struct timekeeper *tk)
201 205
202/** 206/**
203 * pvclock_gtod_register_notifier - register a pvclock timedata update listener 207 * pvclock_gtod_register_notifier - register a pvclock timedata update listener
204 *
205 * Must hold write on timekeeper.lock
206 */ 208 */
207int pvclock_gtod_register_notifier(struct notifier_block *nb) 209int pvclock_gtod_register_notifier(struct notifier_block *nb)
208{ 210{
@@ -210,11 +212,10 @@ int pvclock_gtod_register_notifier(struct notifier_block *nb)
210 unsigned long flags; 212 unsigned long flags;
211 int ret; 213 int ret;
212 214
213 write_seqlock_irqsave(&tk->lock, flags); 215 raw_spin_lock_irqsave(&timekeeper_lock, flags);
214 ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb); 216 ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb);
215 /* update timekeeping data */
216 update_pvclock_gtod(tk); 217 update_pvclock_gtod(tk);
217 write_sequnlock_irqrestore(&tk->lock, flags); 218 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
218 219
219 return ret; 220 return ret;
220} 221}
@@ -223,24 +224,21 @@ EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier);
223/** 224/**
224 * pvclock_gtod_unregister_notifier - unregister a pvclock 225 * pvclock_gtod_unregister_notifier - unregister a pvclock
225 * timedata update listener 226 * timedata update listener
226 *
227 * Must hold write on timekeeper.lock
228 */ 227 */
229int pvclock_gtod_unregister_notifier(struct notifier_block *nb) 228int pvclock_gtod_unregister_notifier(struct notifier_block *nb)
230{ 229{
231 struct timekeeper *tk = &timekeeper;
232 unsigned long flags; 230 unsigned long flags;
233 int ret; 231 int ret;
234 232
235 write_seqlock_irqsave(&tk->lock, flags); 233 raw_spin_lock_irqsave(&timekeeper_lock, flags);
236 ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb); 234 ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb);
237 write_sequnlock_irqrestore(&tk->lock, flags); 235 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
238 236
239 return ret; 237 return ret;
240} 238}
241EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier); 239EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier);
242 240
243/* must hold write on timekeeper.lock */ 241/* must hold timekeeper_lock */
244static void timekeeping_update(struct timekeeper *tk, bool clearntp) 242static void timekeeping_update(struct timekeeper *tk, bool clearntp)
245{ 243{
246 if (clearntp) { 244 if (clearntp) {
@@ -294,12 +292,12 @@ int __getnstimeofday(struct timespec *ts)
294 s64 nsecs = 0; 292 s64 nsecs = 0;
295 293
296 do { 294 do {
297 seq = read_seqbegin(&tk->lock); 295 seq = read_seqcount_begin(&timekeeper_seq);
298 296
299 ts->tv_sec = tk->xtime_sec; 297 ts->tv_sec = tk->xtime_sec;
300 nsecs = timekeeping_get_ns(tk); 298 nsecs = timekeeping_get_ns(tk);
301 299
302 } while (read_seqretry(&tk->lock, seq)); 300 } while (read_seqcount_retry(&timekeeper_seq, seq));
303 301
304 ts->tv_nsec = 0; 302 ts->tv_nsec = 0;
305 timespec_add_ns(ts, nsecs); 303 timespec_add_ns(ts, nsecs);
@@ -335,11 +333,11 @@ ktime_t ktime_get(void)
335 WARN_ON(timekeeping_suspended); 333 WARN_ON(timekeeping_suspended);
336 334
337 do { 335 do {
338 seq = read_seqbegin(&tk->lock); 336 seq = read_seqcount_begin(&timekeeper_seq);
339 secs = tk->xtime_sec + tk->wall_to_monotonic.tv_sec; 337 secs = tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
340 nsecs = timekeeping_get_ns(tk) + tk->wall_to_monotonic.tv_nsec; 338 nsecs = timekeeping_get_ns(tk) + tk->wall_to_monotonic.tv_nsec;
341 339
342 } while (read_seqretry(&tk->lock, seq)); 340 } while (read_seqcount_retry(&timekeeper_seq, seq));
343 /* 341 /*
344 * Use ktime_set/ktime_add_ns to create a proper ktime on 342 * Use ktime_set/ktime_add_ns to create a proper ktime on
345 * 32-bit architectures without CONFIG_KTIME_SCALAR. 343 * 32-bit architectures without CONFIG_KTIME_SCALAR.
@@ -366,12 +364,12 @@ void ktime_get_ts(struct timespec *ts)
366 WARN_ON(timekeeping_suspended); 364 WARN_ON(timekeeping_suspended);
367 365
368 do { 366 do {
369 seq = read_seqbegin(&tk->lock); 367 seq = read_seqcount_begin(&timekeeper_seq);
370 ts->tv_sec = tk->xtime_sec; 368 ts->tv_sec = tk->xtime_sec;
371 nsec = timekeeping_get_ns(tk); 369 nsec = timekeeping_get_ns(tk);
372 tomono = tk->wall_to_monotonic; 370 tomono = tk->wall_to_monotonic;
373 371
374 } while (read_seqretry(&tk->lock, seq)); 372 } while (read_seqcount_retry(&timekeeper_seq, seq));
375 373
376 ts->tv_sec += tomono.tv_sec; 374 ts->tv_sec += tomono.tv_sec;
377 ts->tv_nsec = 0; 375 ts->tv_nsec = 0;
@@ -379,6 +377,50 @@ void ktime_get_ts(struct timespec *ts)
379} 377}
380EXPORT_SYMBOL_GPL(ktime_get_ts); 378EXPORT_SYMBOL_GPL(ktime_get_ts);
381 379
380
381/**
382 * timekeeping_clocktai - Returns the TAI time of day in a timespec
383 * @ts: pointer to the timespec to be set
384 *
385 * Returns the time of day in a timespec.
386 */
387void timekeeping_clocktai(struct timespec *ts)
388{
389 struct timekeeper *tk = &timekeeper;
390 unsigned long seq;
391 u64 nsecs;
392
393 WARN_ON(timekeeping_suspended);
394
395 do {
396 seq = read_seqcount_begin(&timekeeper_seq);
397
398 ts->tv_sec = tk->xtime_sec + tk->tai_offset;
399 nsecs = timekeeping_get_ns(tk);
400
401 } while (read_seqcount_retry(&timekeeper_seq, seq));
402
403 ts->tv_nsec = 0;
404 timespec_add_ns(ts, nsecs);
405
406}
407EXPORT_SYMBOL(timekeeping_clocktai);
408
409
410/**
411 * ktime_get_clocktai - Returns the TAI time of day in a ktime
412 *
413 * Returns the time of day in a ktime.
414 */
415ktime_t ktime_get_clocktai(void)
416{
417 struct timespec ts;
418
419 timekeeping_clocktai(&ts);
420 return timespec_to_ktime(ts);
421}
422EXPORT_SYMBOL(ktime_get_clocktai);
423
382#ifdef CONFIG_NTP_PPS 424#ifdef CONFIG_NTP_PPS
383 425
384/** 426/**
@@ -399,7 +441,7 @@ void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
399 WARN_ON_ONCE(timekeeping_suspended); 441 WARN_ON_ONCE(timekeeping_suspended);
400 442
401 do { 443 do {
402 seq = read_seqbegin(&tk->lock); 444 seq = read_seqcount_begin(&timekeeper_seq);
403 445
404 *ts_raw = tk->raw_time; 446 *ts_raw = tk->raw_time;
405 ts_real->tv_sec = tk->xtime_sec; 447 ts_real->tv_sec = tk->xtime_sec;
@@ -408,7 +450,7 @@ void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
408 nsecs_raw = timekeeping_get_ns_raw(tk); 450 nsecs_raw = timekeeping_get_ns_raw(tk);
409 nsecs_real = timekeeping_get_ns(tk); 451 nsecs_real = timekeeping_get_ns(tk);
410 452
411 } while (read_seqretry(&tk->lock, seq)); 453 } while (read_seqcount_retry(&timekeeper_seq, seq));
412 454
413 timespec_add_ns(ts_raw, nsecs_raw); 455 timespec_add_ns(ts_raw, nsecs_raw);
414 timespec_add_ns(ts_real, nsecs_real); 456 timespec_add_ns(ts_real, nsecs_real);
@@ -448,7 +490,8 @@ int do_settimeofday(const struct timespec *tv)
448 if (!timespec_valid_strict(tv)) 490 if (!timespec_valid_strict(tv))
449 return -EINVAL; 491 return -EINVAL;
450 492
451 write_seqlock_irqsave(&tk->lock, flags); 493 raw_spin_lock_irqsave(&timekeeper_lock, flags);
494 write_seqcount_begin(&timekeeper_seq);
452 495
453 timekeeping_forward_now(tk); 496 timekeeping_forward_now(tk);
454 497
@@ -462,7 +505,8 @@ int do_settimeofday(const struct timespec *tv)
462 505
463 timekeeping_update(tk, true); 506 timekeeping_update(tk, true);
464 507
465 write_sequnlock_irqrestore(&tk->lock, flags); 508 write_seqcount_end(&timekeeper_seq);
509 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
466 510
467 /* signal hrtimers about time change */ 511 /* signal hrtimers about time change */
468 clock_was_set(); 512 clock_was_set();
@@ -487,7 +531,8 @@ int timekeeping_inject_offset(struct timespec *ts)
487 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) 531 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
488 return -EINVAL; 532 return -EINVAL;
489 533
490 write_seqlock_irqsave(&tk->lock, flags); 534 raw_spin_lock_irqsave(&timekeeper_lock, flags);
535 write_seqcount_begin(&timekeeper_seq);
491 536
492 timekeeping_forward_now(tk); 537 timekeeping_forward_now(tk);
493 538
@@ -504,7 +549,8 @@ int timekeeping_inject_offset(struct timespec *ts)
504error: /* even if we error out, we forwarded the time, so call update */ 549error: /* even if we error out, we forwarded the time, so call update */
505 timekeeping_update(tk, true); 550 timekeeping_update(tk, true);
506 551
507 write_sequnlock_irqrestore(&tk->lock, flags); 552 write_seqcount_end(&timekeeper_seq);
553 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
508 554
509 /* signal hrtimers about time change */ 555 /* signal hrtimers about time change */
510 clock_was_set(); 556 clock_was_set();
@@ -513,6 +559,51 @@ error: /* even if we error out, we forwarded the time, so call update */
513} 559}
514EXPORT_SYMBOL(timekeeping_inject_offset); 560EXPORT_SYMBOL(timekeeping_inject_offset);
515 561
562
563/**
564 * timekeeping_get_tai_offset - Returns current TAI offset from UTC
565 *
566 */
567s32 timekeeping_get_tai_offset(void)
568{
569 struct timekeeper *tk = &timekeeper;
570 unsigned int seq;
571 s32 ret;
572
573 do {
574 seq = read_seqcount_begin(&timekeeper_seq);
575 ret = tk->tai_offset;
576 } while (read_seqcount_retry(&timekeeper_seq, seq));
577
578 return ret;
579}
580
581/**
582 * __timekeeping_set_tai_offset - Lock free worker function
583 *
584 */
585static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
586{
587 tk->tai_offset = tai_offset;
588 tk->offs_tai = ktime_sub(tk->offs_real, ktime_set(tai_offset, 0));
589}
590
591/**
592 * timekeeping_set_tai_offset - Sets the current TAI offset from UTC
593 *
594 */
595void timekeeping_set_tai_offset(s32 tai_offset)
596{
597 struct timekeeper *tk = &timekeeper;
598 unsigned long flags;
599
600 raw_spin_lock_irqsave(&timekeeper_lock, flags);
601 write_seqcount_begin(&timekeeper_seq);
602 __timekeeping_set_tai_offset(tk, tai_offset);
603 write_seqcount_end(&timekeeper_seq);
604 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
605}
606
516/** 607/**
517 * change_clocksource - Swaps clocksources if a new one is available 608 * change_clocksource - Swaps clocksources if a new one is available
518 * 609 *
@@ -526,7 +617,8 @@ static int change_clocksource(void *data)
526 617
527 new = (struct clocksource *) data; 618 new = (struct clocksource *) data;
528 619
529 write_seqlock_irqsave(&tk->lock, flags); 620 raw_spin_lock_irqsave(&timekeeper_lock, flags);
621 write_seqcount_begin(&timekeeper_seq);
530 622
531 timekeeping_forward_now(tk); 623 timekeeping_forward_now(tk);
532 if (!new->enable || new->enable(new) == 0) { 624 if (!new->enable || new->enable(new) == 0) {
@@ -537,7 +629,8 @@ static int change_clocksource(void *data)
537 } 629 }
538 timekeeping_update(tk, true); 630 timekeeping_update(tk, true);
539 631
540 write_sequnlock_irqrestore(&tk->lock, flags); 632 write_seqcount_end(&timekeeper_seq);
633 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
541 634
542 return 0; 635 return 0;
543} 636}
@@ -587,11 +680,11 @@ void getrawmonotonic(struct timespec *ts)
587 s64 nsecs; 680 s64 nsecs;
588 681
589 do { 682 do {
590 seq = read_seqbegin(&tk->lock); 683 seq = read_seqcount_begin(&timekeeper_seq);
591 nsecs = timekeeping_get_ns_raw(tk); 684 nsecs = timekeeping_get_ns_raw(tk);
592 *ts = tk->raw_time; 685 *ts = tk->raw_time;
593 686
594 } while (read_seqretry(&tk->lock, seq)); 687 } while (read_seqcount_retry(&timekeeper_seq, seq));
595 688
596 timespec_add_ns(ts, nsecs); 689 timespec_add_ns(ts, nsecs);
597} 690}
@@ -607,11 +700,11 @@ int timekeeping_valid_for_hres(void)
607 int ret; 700 int ret;
608 701
609 do { 702 do {
610 seq = read_seqbegin(&tk->lock); 703 seq = read_seqcount_begin(&timekeeper_seq);
611 704
612 ret = tk->clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; 705 ret = tk->clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
613 706
614 } while (read_seqretry(&tk->lock, seq)); 707 } while (read_seqcount_retry(&timekeeper_seq, seq));
615 708
616 return ret; 709 return ret;
617} 710}
@@ -626,11 +719,11 @@ u64 timekeeping_max_deferment(void)
626 u64 ret; 719 u64 ret;
627 720
628 do { 721 do {
629 seq = read_seqbegin(&tk->lock); 722 seq = read_seqcount_begin(&timekeeper_seq);
630 723
631 ret = tk->clock->max_idle_ns; 724 ret = tk->clock->max_idle_ns;
632 725
633 } while (read_seqretry(&tk->lock, seq)); 726 } while (read_seqcount_retry(&timekeeper_seq, seq));
634 727
635 return ret; 728 return ret;
636} 729}
@@ -693,11 +786,10 @@ void __init timekeeping_init(void)
693 boot.tv_nsec = 0; 786 boot.tv_nsec = 0;
694 } 787 }
695 788
696 seqlock_init(&tk->lock);
697
698 ntp_init(); 789 ntp_init();
699 790
700 write_seqlock_irqsave(&tk->lock, flags); 791 raw_spin_lock_irqsave(&timekeeper_lock, flags);
792 write_seqcount_begin(&timekeeper_seq);
701 clock = clocksource_default_clock(); 793 clock = clocksource_default_clock();
702 if (clock->enable) 794 if (clock->enable)
703 clock->enable(clock); 795 clock->enable(clock);
@@ -716,7 +808,8 @@ void __init timekeeping_init(void)
716 tmp.tv_nsec = 0; 808 tmp.tv_nsec = 0;
717 tk_set_sleep_time(tk, tmp); 809 tk_set_sleep_time(tk, tmp);
718 810
719 write_sequnlock_irqrestore(&tk->lock, flags); 811 write_seqcount_end(&timekeeper_seq);
812 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
720} 813}
721 814
722/* time in seconds when suspend began */ 815/* time in seconds when suspend began */
@@ -764,7 +857,8 @@ void timekeeping_inject_sleeptime(struct timespec *delta)
764 if (has_persistent_clock()) 857 if (has_persistent_clock())
765 return; 858 return;
766 859
767 write_seqlock_irqsave(&tk->lock, flags); 860 raw_spin_lock_irqsave(&timekeeper_lock, flags);
861 write_seqcount_begin(&timekeeper_seq);
768 862
769 timekeeping_forward_now(tk); 863 timekeeping_forward_now(tk);
770 864
@@ -772,7 +866,8 @@ void timekeeping_inject_sleeptime(struct timespec *delta)
772 866
773 timekeeping_update(tk, true); 867 timekeeping_update(tk, true);
774 868
775 write_sequnlock_irqrestore(&tk->lock, flags); 869 write_seqcount_end(&timekeeper_seq);
870 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
776 871
777 /* signal hrtimers about time change */ 872 /* signal hrtimers about time change */
778 clock_was_set(); 873 clock_was_set();
@@ -788,26 +883,72 @@ void timekeeping_inject_sleeptime(struct timespec *delta)
788static void timekeeping_resume(void) 883static void timekeeping_resume(void)
789{ 884{
790 struct timekeeper *tk = &timekeeper; 885 struct timekeeper *tk = &timekeeper;
886 struct clocksource *clock = tk->clock;
791 unsigned long flags; 887 unsigned long flags;
792 struct timespec ts; 888 struct timespec ts_new, ts_delta;
889 cycle_t cycle_now, cycle_delta;
890 bool suspendtime_found = false;
793 891
794 read_persistent_clock(&ts); 892 read_persistent_clock(&ts_new);
795 893
796 clockevents_resume(); 894 clockevents_resume();
797 clocksource_resume(); 895 clocksource_resume();
798 896
799 write_seqlock_irqsave(&tk->lock, flags); 897 raw_spin_lock_irqsave(&timekeeper_lock, flags);
898 write_seqcount_begin(&timekeeper_seq);
899
900 /*
901 * After system resumes, we need to calculate the suspended time and
902 * compensate it for the OS time. There are 3 sources that could be
903 * used: Nonstop clocksource during suspend, persistent clock and rtc
904 * device.
905 *
906 * One specific platform may have 1 or 2 or all of them, and the
907 * preference will be:
908 * suspend-nonstop clocksource -> persistent clock -> rtc
909 * The less preferred source will only be tried if there is no better
910 * usable source. The rtc part is handled separately in rtc core code.
911 */
912 cycle_now = clock->read(clock);
913 if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
914 cycle_now > clock->cycle_last) {
915 u64 num, max = ULLONG_MAX;
916 u32 mult = clock->mult;
917 u32 shift = clock->shift;
918 s64 nsec = 0;
800 919
801 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) { 920 cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
802 ts = timespec_sub(ts, timekeeping_suspend_time); 921
803 __timekeeping_inject_sleeptime(tk, &ts); 922 /*
923 * "cycle_delta * mutl" may cause 64 bits overflow, if the
924 * suspended time is too long. In that case we need do the
925 * 64 bits math carefully
926 */
927 do_div(max, mult);
928 if (cycle_delta > max) {
929 num = div64_u64(cycle_delta, max);
930 nsec = (((u64) max * mult) >> shift) * num;
931 cycle_delta -= num * max;
932 }
933 nsec += ((u64) cycle_delta * mult) >> shift;
934
935 ts_delta = ns_to_timespec(nsec);
936 suspendtime_found = true;
937 } else if (timespec_compare(&ts_new, &timekeeping_suspend_time) > 0) {
938 ts_delta = timespec_sub(ts_new, timekeeping_suspend_time);
939 suspendtime_found = true;
804 } 940 }
805 /* re-base the last cycle value */ 941
806 tk->clock->cycle_last = tk->clock->read(tk->clock); 942 if (suspendtime_found)
943 __timekeeping_inject_sleeptime(tk, &ts_delta);
944
945 /* Re-base the last cycle value */
946 clock->cycle_last = cycle_now;
807 tk->ntp_error = 0; 947 tk->ntp_error = 0;
808 timekeeping_suspended = 0; 948 timekeeping_suspended = 0;
809 timekeeping_update(tk, false); 949 timekeeping_update(tk, false);
810 write_sequnlock_irqrestore(&tk->lock, flags); 950 write_seqcount_end(&timekeeper_seq);
951 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
811 952
812 touch_softlockup_watchdog(); 953 touch_softlockup_watchdog();
813 954
@@ -826,7 +967,8 @@ static int timekeeping_suspend(void)
826 967
827 read_persistent_clock(&timekeeping_suspend_time); 968 read_persistent_clock(&timekeeping_suspend_time);
828 969
829 write_seqlock_irqsave(&tk->lock, flags); 970 raw_spin_lock_irqsave(&timekeeper_lock, flags);
971 write_seqcount_begin(&timekeeper_seq);
830 timekeeping_forward_now(tk); 972 timekeeping_forward_now(tk);
831 timekeeping_suspended = 1; 973 timekeeping_suspended = 1;
832 974
@@ -849,7 +991,8 @@ static int timekeeping_suspend(void)
849 timekeeping_suspend_time = 991 timekeeping_suspend_time =
850 timespec_add(timekeeping_suspend_time, delta_delta); 992 timespec_add(timekeeping_suspend_time, delta_delta);
851 } 993 }
852 write_sequnlock_irqrestore(&tk->lock, flags); 994 write_seqcount_end(&timekeeper_seq);
995 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
853 996
854 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL); 997 clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
855 clocksource_suspend(); 998 clocksource_suspend();
@@ -1099,6 +1242,8 @@ static inline void accumulate_nsecs_to_secs(struct timekeeper *tk)
1099 tk_set_wall_to_mono(tk, 1242 tk_set_wall_to_mono(tk,
1100 timespec_sub(tk->wall_to_monotonic, ts)); 1243 timespec_sub(tk->wall_to_monotonic, ts));
1101 1244
1245 __timekeeping_set_tai_offset(tk, tk->tai_offset - leap);
1246
1102 clock_was_set_delayed(); 1247 clock_was_set_delayed();
1103 } 1248 }
1104 } 1249 }
@@ -1116,15 +1261,16 @@ static inline void accumulate_nsecs_to_secs(struct timekeeper *tk)
1116static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset, 1261static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset,
1117 u32 shift) 1262 u32 shift)
1118{ 1263{
1264 cycle_t interval = tk->cycle_interval << shift;
1119 u64 raw_nsecs; 1265 u64 raw_nsecs;
1120 1266
1121 /* If the offset is smaller then a shifted interval, do nothing */ 1267 /* If the offset is smaller then a shifted interval, do nothing */
1122 if (offset < tk->cycle_interval<<shift) 1268 if (offset < interval)
1123 return offset; 1269 return offset;
1124 1270
1125 /* Accumulate one shifted interval */ 1271 /* Accumulate one shifted interval */
1126 offset -= tk->cycle_interval << shift; 1272 offset -= interval;
1127 tk->clock->cycle_last += tk->cycle_interval << shift; 1273 tk->clock->cycle_last += interval;
1128 1274
1129 tk->xtime_nsec += tk->xtime_interval << shift; 1275 tk->xtime_nsec += tk->xtime_interval << shift;
1130 accumulate_nsecs_to_secs(tk); 1276 accumulate_nsecs_to_secs(tk);
@@ -1186,7 +1332,8 @@ static void update_wall_time(void)
1186 int shift = 0, maxshift; 1332 int shift = 0, maxshift;
1187 unsigned long flags; 1333 unsigned long flags;
1188 1334
1189 write_seqlock_irqsave(&tk->lock, flags); 1335 raw_spin_lock_irqsave(&timekeeper_lock, flags);
1336 write_seqcount_begin(&timekeeper_seq);
1190 1337
1191 /* Make sure we're fully resumed: */ 1338 /* Make sure we're fully resumed: */
1192 if (unlikely(timekeeping_suspended)) 1339 if (unlikely(timekeeping_suspended))
@@ -1241,7 +1388,8 @@ static void update_wall_time(void)
1241 timekeeping_update(tk, false); 1388 timekeeping_update(tk, false);
1242 1389
1243out: 1390out:
1244 write_sequnlock_irqrestore(&tk->lock, flags); 1391 write_seqcount_end(&timekeeper_seq);
1392 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
1245 1393
1246} 1394}
1247 1395
@@ -1289,13 +1437,13 @@ void get_monotonic_boottime(struct timespec *ts)
1289 WARN_ON(timekeeping_suspended); 1437 WARN_ON(timekeeping_suspended);
1290 1438
1291 do { 1439 do {
1292 seq = read_seqbegin(&tk->lock); 1440 seq = read_seqcount_begin(&timekeeper_seq);
1293 ts->tv_sec = tk->xtime_sec; 1441 ts->tv_sec = tk->xtime_sec;
1294 nsec = timekeeping_get_ns(tk); 1442 nsec = timekeeping_get_ns(tk);
1295 tomono = tk->wall_to_monotonic; 1443 tomono = tk->wall_to_monotonic;
1296 sleep = tk->total_sleep_time; 1444 sleep = tk->total_sleep_time;
1297 1445
1298 } while (read_seqretry(&tk->lock, seq)); 1446 } while (read_seqcount_retry(&timekeeper_seq, seq));
1299 1447
1300 ts->tv_sec += tomono.tv_sec + sleep.tv_sec; 1448 ts->tv_sec += tomono.tv_sec + sleep.tv_sec;
1301 ts->tv_nsec = 0; 1449 ts->tv_nsec = 0;
@@ -1354,10 +1502,10 @@ struct timespec current_kernel_time(void)
1354 unsigned long seq; 1502 unsigned long seq;
1355 1503
1356 do { 1504 do {
1357 seq = read_seqbegin(&tk->lock); 1505 seq = read_seqcount_begin(&timekeeper_seq);
1358 1506
1359 now = tk_xtime(tk); 1507 now = tk_xtime(tk);
1360 } while (read_seqretry(&tk->lock, seq)); 1508 } while (read_seqcount_retry(&timekeeper_seq, seq));
1361 1509
1362 return now; 1510 return now;
1363} 1511}
@@ -1370,11 +1518,11 @@ struct timespec get_monotonic_coarse(void)
1370 unsigned long seq; 1518 unsigned long seq;
1371 1519
1372 do { 1520 do {
1373 seq = read_seqbegin(&tk->lock); 1521 seq = read_seqcount_begin(&timekeeper_seq);
1374 1522
1375 now = tk_xtime(tk); 1523 now = tk_xtime(tk);
1376 mono = tk->wall_to_monotonic; 1524 mono = tk->wall_to_monotonic;
1377 } while (read_seqretry(&tk->lock, seq)); 1525 } while (read_seqcount_retry(&timekeeper_seq, seq));
1378 1526
1379 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec, 1527 set_normalized_timespec(&now, now.tv_sec + mono.tv_sec,
1380 now.tv_nsec + mono.tv_nsec); 1528 now.tv_nsec + mono.tv_nsec);
@@ -1405,11 +1553,11 @@ void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
1405 unsigned long seq; 1553 unsigned long seq;
1406 1554
1407 do { 1555 do {
1408 seq = read_seqbegin(&tk->lock); 1556 seq = read_seqcount_begin(&timekeeper_seq);
1409 *xtim = tk_xtime(tk); 1557 *xtim = tk_xtime(tk);
1410 *wtom = tk->wall_to_monotonic; 1558 *wtom = tk->wall_to_monotonic;
1411 *sleep = tk->total_sleep_time; 1559 *sleep = tk->total_sleep_time;
1412 } while (read_seqretry(&tk->lock, seq)); 1560 } while (read_seqcount_retry(&timekeeper_seq, seq));
1413} 1561}
1414 1562
1415#ifdef CONFIG_HIGH_RES_TIMERS 1563#ifdef CONFIG_HIGH_RES_TIMERS
@@ -1421,7 +1569,8 @@ void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
1421 * Returns current monotonic time and updates the offsets 1569 * Returns current monotonic time and updates the offsets
1422 * Called from hrtimer_interupt() or retrigger_next_event() 1570 * Called from hrtimer_interupt() or retrigger_next_event()
1423 */ 1571 */
1424ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot) 1572ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot,
1573 ktime_t *offs_tai)
1425{ 1574{
1426 struct timekeeper *tk = &timekeeper; 1575 struct timekeeper *tk = &timekeeper;
1427 ktime_t now; 1576 ktime_t now;
@@ -1429,14 +1578,15 @@ ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot)
1429 u64 secs, nsecs; 1578 u64 secs, nsecs;
1430 1579
1431 do { 1580 do {
1432 seq = read_seqbegin(&tk->lock); 1581 seq = read_seqcount_begin(&timekeeper_seq);
1433 1582
1434 secs = tk->xtime_sec; 1583 secs = tk->xtime_sec;
1435 nsecs = timekeeping_get_ns(tk); 1584 nsecs = timekeeping_get_ns(tk);
1436 1585
1437 *offs_real = tk->offs_real; 1586 *offs_real = tk->offs_real;
1438 *offs_boot = tk->offs_boot; 1587 *offs_boot = tk->offs_boot;
1439 } while (read_seqretry(&tk->lock, seq)); 1588 *offs_tai = tk->offs_tai;
1589 } while (read_seqcount_retry(&timekeeper_seq, seq));
1440 1590
1441 now = ktime_add_ns(ktime_set(secs, 0), nsecs); 1591 now = ktime_add_ns(ktime_set(secs, 0), nsecs);
1442 now = ktime_sub(now, *offs_real); 1592 now = ktime_sub(now, *offs_real);
@@ -1454,9 +1604,9 @@ ktime_t ktime_get_monotonic_offset(void)
1454 struct timespec wtom; 1604 struct timespec wtom;
1455 1605
1456 do { 1606 do {
1457 seq = read_seqbegin(&tk->lock); 1607 seq = read_seqcount_begin(&timekeeper_seq);
1458 wtom = tk->wall_to_monotonic; 1608 wtom = tk->wall_to_monotonic;
1459 } while (read_seqretry(&tk->lock, seq)); 1609 } while (read_seqcount_retry(&timekeeper_seq, seq));
1460 1610
1461 return timespec_to_ktime(wtom); 1611 return timespec_to_ktime(wtom);
1462} 1612}