diff options
Diffstat (limited to 'kernel/time')
| -rw-r--r-- | kernel/time/Makefile | 2 | ||||
| -rw-r--r-- | kernel/time/clockevents.c | 20 | ||||
| -rw-r--r-- | kernel/time/clocksource.c | 76 | ||||
| -rw-r--r-- | kernel/time/ntp.c | 444 | ||||
| -rw-r--r-- | kernel/time/timecompare.c | 191 |
5 files changed, 553 insertions, 180 deletions
diff --git a/kernel/time/Makefile b/kernel/time/Makefile index 905b0b50792..0b0a6366c9d 100644 --- a/kernel/time/Makefile +++ b/kernel/time/Makefile | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o | 1 | obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o |
| 2 | 2 | ||
| 3 | obj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o | 3 | obj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o |
| 4 | obj-$(CONFIG_GENERIC_CLOCKEVENTS) += tick-common.o | 4 | obj-$(CONFIG_GENERIC_CLOCKEVENTS) += tick-common.o |
diff --git a/kernel/time/clockevents.c b/kernel/time/clockevents.c index ea2f48af83c..d13be216a79 100644 --- a/kernel/time/clockevents.c +++ b/kernel/time/clockevents.c | |||
| @@ -68,6 +68,17 @@ void clockevents_set_mode(struct clock_event_device *dev, | |||
| 68 | if (dev->mode != mode) { | 68 | if (dev->mode != mode) { |
| 69 | dev->set_mode(mode, dev); | 69 | dev->set_mode(mode, dev); |
| 70 | dev->mode = mode; | 70 | dev->mode = mode; |
| 71 | |||
| 72 | /* | ||
| 73 | * A nsec2cyc multiplicator of 0 is invalid and we'd crash | ||
| 74 | * on it, so fix it up and emit a warning: | ||
| 75 | */ | ||
| 76 | if (mode == CLOCK_EVT_MODE_ONESHOT) { | ||
| 77 | if (unlikely(!dev->mult)) { | ||
| 78 | dev->mult = 1; | ||
| 79 | WARN_ON(1); | ||
| 80 | } | ||
| 81 | } | ||
| 71 | } | 82 | } |
| 72 | } | 83 | } |
| 73 | 84 | ||
| @@ -168,15 +179,6 @@ void clockevents_register_device(struct clock_event_device *dev) | |||
| 168 | BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED); | 179 | BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED); |
| 169 | BUG_ON(!dev->cpumask); | 180 | BUG_ON(!dev->cpumask); |
| 170 | 181 | ||
| 171 | /* | ||
| 172 | * A nsec2cyc multiplicator of 0 is invalid and we'd crash | ||
| 173 | * on it, so fix it up and emit a warning: | ||
| 174 | */ | ||
| 175 | if (unlikely(!dev->mult)) { | ||
| 176 | dev->mult = 1; | ||
| 177 | WARN_ON(1); | ||
| 178 | } | ||
| 179 | |||
| 180 | spin_lock(&clockevents_lock); | 182 | spin_lock(&clockevents_lock); |
| 181 | 183 | ||
| 182 | list_add(&dev->list, &clockevent_devices); | 184 | list_add(&dev->list, &clockevent_devices); |
diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c index ca89e1593f0..c46c931a7fe 100644 --- a/kernel/time/clocksource.c +++ b/kernel/time/clocksource.c | |||
| @@ -31,6 +31,82 @@ | |||
| 31 | #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */ | 31 | #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */ |
| 32 | #include <linux/tick.h> | 32 | #include <linux/tick.h> |
| 33 | 33 | ||
| 34 | void timecounter_init(struct timecounter *tc, | ||
| 35 | const struct cyclecounter *cc, | ||
| 36 | u64 start_tstamp) | ||
| 37 | { | ||
| 38 | tc->cc = cc; | ||
| 39 | tc->cycle_last = cc->read(cc); | ||
| 40 | tc->nsec = start_tstamp; | ||
| 41 | } | ||
| 42 | EXPORT_SYMBOL(timecounter_init); | ||
| 43 | |||
| 44 | /** | ||
| 45 | * timecounter_read_delta - get nanoseconds since last call of this function | ||
| 46 | * @tc: Pointer to time counter | ||
| 47 | * | ||
| 48 | * When the underlying cycle counter runs over, this will be handled | ||
| 49 | * correctly as long as it does not run over more than once between | ||
| 50 | * calls. | ||
| 51 | * | ||
| 52 | * The first call to this function for a new time counter initializes | ||
| 53 | * the time tracking and returns an undefined result. | ||
| 54 | */ | ||
| 55 | static u64 timecounter_read_delta(struct timecounter *tc) | ||
| 56 | { | ||
| 57 | cycle_t cycle_now, cycle_delta; | ||
| 58 | u64 ns_offset; | ||
| 59 | |||
| 60 | /* read cycle counter: */ | ||
| 61 | cycle_now = tc->cc->read(tc->cc); | ||
| 62 | |||
| 63 | /* calculate the delta since the last timecounter_read_delta(): */ | ||
| 64 | cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask; | ||
| 65 | |||
| 66 | /* convert to nanoseconds: */ | ||
| 67 | ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta); | ||
| 68 | |||
| 69 | /* update time stamp of timecounter_read_delta() call: */ | ||
| 70 | tc->cycle_last = cycle_now; | ||
| 71 | |||
| 72 | return ns_offset; | ||
| 73 | } | ||
| 74 | |||
| 75 | u64 timecounter_read(struct timecounter *tc) | ||
| 76 | { | ||
| 77 | u64 nsec; | ||
| 78 | |||
| 79 | /* increment time by nanoseconds since last call */ | ||
| 80 | nsec = timecounter_read_delta(tc); | ||
| 81 | nsec += tc->nsec; | ||
| 82 | tc->nsec = nsec; | ||
| 83 | |||
| 84 | return nsec; | ||
| 85 | } | ||
| 86 | EXPORT_SYMBOL(timecounter_read); | ||
| 87 | |||
| 88 | u64 timecounter_cyc2time(struct timecounter *tc, | ||
| 89 | cycle_t cycle_tstamp) | ||
| 90 | { | ||
| 91 | u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask; | ||
| 92 | u64 nsec; | ||
| 93 | |||
| 94 | /* | ||
| 95 | * Instead of always treating cycle_tstamp as more recent | ||
| 96 | * than tc->cycle_last, detect when it is too far in the | ||
| 97 | * future and treat it as old time stamp instead. | ||
| 98 | */ | ||
| 99 | if (cycle_delta > tc->cc->mask / 2) { | ||
| 100 | cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask; | ||
| 101 | nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta); | ||
| 102 | } else { | ||
| 103 | nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec; | ||
| 104 | } | ||
| 105 | |||
| 106 | return nsec; | ||
| 107 | } | ||
| 108 | EXPORT_SYMBOL(timecounter_cyc2time); | ||
| 109 | |||
| 34 | /* XXX - Would like a better way for initializing curr_clocksource */ | 110 | /* XXX - Would like a better way for initializing curr_clocksource */ |
| 35 | extern struct clocksource clocksource_jiffies; | 111 | extern struct clocksource clocksource_jiffies; |
| 36 | 112 | ||
diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c index f5f793d9241..7fc64375ff4 100644 --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c | |||
| @@ -1,71 +1,129 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * linux/kernel/time/ntp.c | ||
| 3 | * | ||
| 4 | * NTP state machine interfaces and logic. | 2 | * NTP state machine interfaces and logic. |
| 5 | * | 3 | * |
| 6 | * This code was mainly moved from kernel/timer.c and kernel/time.c | 4 | * This code was mainly moved from kernel/timer.c and kernel/time.c |
| 7 | * Please see those files for relevant copyright info and historical | 5 | * Please see those files for relevant copyright info and historical |
| 8 | * changelogs. | 6 | * changelogs. |
| 9 | */ | 7 | */ |
| 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> | 8 | #include <linux/capability.h> |
| 17 | #include <linux/math64.h> | ||
| 18 | #include <linux/clocksource.h> | 9 | #include <linux/clocksource.h> |
| 19 | #include <linux/workqueue.h> | 10 | #include <linux/workqueue.h> |
| 20 | #include <asm/timex.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> | ||
| 21 | 17 | ||
| 22 | /* | 18 | /* |
| 23 | * Timekeeping variables | 19 | * NTP timekeeping variables: |
| 24 | */ | 20 | */ |
| 25 | unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */ | ||
| 26 | unsigned long tick_nsec; /* ACTHZ period (nsec) */ | ||
| 27 | u64 tick_length; | ||
| 28 | static u64 tick_length_base; | ||
| 29 | 21 | ||
| 30 | static struct hrtimer leap_timer; | 22 | /* USER_HZ period (usecs): */ |
| 23 | unsigned long tick_usec = TICK_USEC; | ||
| 31 | 24 | ||
| 32 | #define MAX_TICKADJ 500 /* microsecs */ | 25 | /* ACTHZ period (nsecs): */ |
| 33 | #define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \ | 26 | unsigned long tick_nsec; |
| 34 | NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ) | 27 | |
| 28 | u64 tick_length; | ||
| 29 | static u64 tick_length_base; | ||
| 30 | |||
| 31 | static struct hrtimer leap_timer; | ||
| 32 | |||
| 33 | #define MAX_TICKADJ 500LL /* usecs */ | ||
| 34 | #define MAX_TICKADJ_SCALED \ | ||
| 35 | (((MAX_TICKADJ * NSEC_PER_USEC) << NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ) | ||
| 35 | 36 | ||
| 36 | /* | 37 | /* |
| 37 | * phase-lock loop variables | 38 | * phase-lock loop variables |
| 38 | */ | 39 | */ |
| 39 | /* TIME_ERROR prevents overwriting the CMOS clock */ | ||
| 40 | static int time_state = TIME_OK; /* clock synchronization status */ | ||
| 41 | int time_status = STA_UNSYNC; /* clock status bits */ | ||
| 42 | static long time_tai; /* TAI offset (s) */ | ||
| 43 | static s64 time_offset; /* time adjustment (ns) */ | ||
| 44 | static long time_constant = 2; /* pll time constant */ | ||
| 45 | long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */ | ||
