john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/time/timekeeping.c |
| 3 | * |
| 4 | * Kernel timekeeping code and accessor functions |
| 5 | * |
| 6 | * This code was moved from linux/kernel/timer.c. |
| 7 | * Please see that file for copyright and history logs. |
| 8 | * |
| 9 | */ |
| 10 | |
John Stultz | d7b4202 | 2012-09-04 15:12:07 -0400 | [diff] [blame] | 11 | #include <linux/timekeeper_internal.h> |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/percpu.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/mm.h> |
Ingo Molnar | 38b8d20 | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 17 | #include <linux/nmi.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 18 | #include <linux/sched.h> |
Ingo Molnar | 4f17722 | 2017-02-08 08:45:17 +0100 | [diff] [blame] | 19 | #include <linux/sched/loadavg.h> |
Rafael J. Wysocki | e1a85b2 | 2011-03-23 22:16:04 +0100 | [diff] [blame] | 20 | #include <linux/syscore_ops.h> |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 21 | #include <linux/clocksource.h> |
| 22 | #include <linux/jiffies.h> |
| 23 | #include <linux/time.h> |
| 24 | #include <linux/tick.h> |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 25 | #include <linux/stop_machine.h> |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 26 | #include <linux/pvclock_gtod.h> |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 27 | #include <linux/compiler.h> |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 28 | |
Thomas Gleixner | eb93e4d | 2013-02-21 22:51:36 +0000 | [diff] [blame] | 29 | #include "tick-internal.h" |
John Stultz | aa6f9c59 | 2013-03-22 11:31:29 -0700 | [diff] [blame] | 30 | #include "ntp_internal.h" |
Colin Cross | 5c83545 | 2013-05-21 22:32:14 -0700 | [diff] [blame] | 31 | #include "timekeeping_internal.h" |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 32 | |
David Vrabel | 04397fe9 | 2013-06-27 11:35:45 +0100 | [diff] [blame] | 33 | #define TK_CLEAR_NTP (1 << 0) |
| 34 | #define TK_MIRROR (1 << 1) |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 35 | #define TK_CLOCK_WAS_SET (1 << 2) |
David Vrabel | 04397fe9 | 2013-06-27 11:35:45 +0100 | [diff] [blame] | 36 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 37 | /* |
| 38 | * The most important data for readout fits into a single 64 byte |
| 39 | * cache line. |
| 40 | */ |
| 41 | static struct { |
| 42 | seqcount_t seq; |
| 43 | struct timekeeper timekeeper; |
| 44 | } tk_core ____cacheline_aligned; |
| 45 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 46 | static DEFINE_RAW_SPINLOCK(timekeeper_lock); |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 47 | static struct timekeeper shadow_timekeeper; |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 48 | |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 49 | /** |
| 50 | * struct tk_fast - NMI safe timekeeper |
| 51 | * @seq: Sequence counter for protecting updates. The lowest bit |
| 52 | * is the index for the tk_read_base array |
| 53 | * @base: tk_read_base array. Access is indexed by the lowest bit of |
| 54 | * @seq. |
| 55 | * |
| 56 | * See @update_fast_timekeeper() below. |
| 57 | */ |
| 58 | struct tk_fast { |
| 59 | seqcount_t seq; |
| 60 | struct tk_read_base base[2]; |
| 61 | }; |
| 62 | |
Prarit Bhargava | 5df3210 | 2017-08-28 08:21:53 -0400 | [diff] [blame^] | 63 | /* Suspend-time cycles value for halted fast timekeeper. */ |
| 64 | static u64 cycles_at_suspend; |
| 65 | |
| 66 | static u64 dummy_clock_read(struct clocksource *cs) |
| 67 | { |
| 68 | return cycles_at_suspend; |
| 69 | } |
| 70 | |
| 71 | static struct clocksource dummy_clock = { |
| 72 | .read = dummy_clock_read, |
| 73 | }; |
| 74 | |
| 75 | static struct tk_fast tk_fast_mono ____cacheline_aligned = { |
| 76 | .base[0] = { .clock = &dummy_clock, }, |
| 77 | .base[1] = { .clock = &dummy_clock, }, |
| 78 | }; |
| 79 | |
| 80 | static struct tk_fast tk_fast_raw ____cacheline_aligned = { |
| 81 | .base[0] = { .clock = &dummy_clock, }, |
| 82 | .base[1] = { .clock = &dummy_clock, }, |
| 83 | }; |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 84 | |
John Stultz | 8fcce54 | 2011-11-14 11:46:39 -0800 | [diff] [blame] | 85 | /* flag for if timekeeping is suspended */ |
| 86 | int __read_mostly timekeeping_suspended; |
| 87 | |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 88 | static inline void tk_normalize_xtime(struct timekeeper *tk) |
| 89 | { |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 90 | while (tk->tkr_mono.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_mono.shift)) { |
| 91 | tk->tkr_mono.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_mono.shift; |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 92 | tk->xtime_sec++; |
| 93 | } |
John Stultz | fc6eead7 | 2017-05-22 17:20:20 -0700 | [diff] [blame] | 94 | while (tk->tkr_raw.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_raw.shift)) { |
| 95 | tk->tkr_raw.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr_raw.shift; |
| 96 | tk->raw_sec++; |
| 97 | } |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 98 | } |
John Stultz | 8fcce54 | 2011-11-14 11:46:39 -0800 | [diff] [blame] | 99 | |
Thomas Gleixner | c905fae | 2014-07-16 21:04:05 +0000 | [diff] [blame] | 100 | static inline struct timespec64 tk_xtime(struct timekeeper *tk) |
| 101 | { |
| 102 | struct timespec64 ts; |
| 103 | |
| 104 | ts.tv_sec = tk->xtime_sec; |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 105 | ts.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift); |
Thomas Gleixner | c905fae | 2014-07-16 21:04:05 +0000 | [diff] [blame] | 106 | return ts; |
| 107 | } |
| 108 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 109 | static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts) |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 110 | { |
| 111 | tk->xtime_sec = ts->tv_sec; |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 112 | tk->tkr_mono.xtime_nsec = (u64)ts->tv_nsec << tk->tkr_mono.shift; |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 113 | } |
| 114 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 115 | static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts) |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 116 | { |
| 117 | tk->xtime_sec += ts->tv_sec; |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 118 | tk->tkr_mono.xtime_nsec += (u64)ts->tv_nsec << tk->tkr_mono.shift; |
John Stultz | 784ffcb | 2012-08-21 20:30:46 -0400 | [diff] [blame] | 119 | tk_normalize_xtime(tk); |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 120 | } |
John Stultz | 8fcce54 | 2011-11-14 11:46:39 -0800 | [diff] [blame] | 121 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 122 | static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm) |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 123 | { |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 124 | struct timespec64 tmp; |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * Verify consistency of: offset_real = -wall_to_monotonic |
| 128 | * before modifying anything |
| 129 | */ |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 130 | set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec, |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 131 | -tk->wall_to_monotonic.tv_nsec); |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 132 | WARN_ON_ONCE(tk->offs_real != timespec64_to_ktime(tmp)); |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 133 | tk->wall_to_monotonic = wtm; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 134 | set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec); |
| 135 | tk->offs_real = timespec64_to_ktime(tmp); |
John Stultz | 04005f6 | 2013-12-10 17:13:35 -0800 | [diff] [blame] | 136 | tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tk->tai_offset, 0)); |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Thomas Gleixner | 47da70d | 2014-07-16 21:05:00 +0000 | [diff] [blame] | 139 | static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta) |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 140 | { |
Thomas Gleixner | 47da70d | 2014-07-16 21:05:00 +0000 | [diff] [blame] | 141 | tk->offs_boot = ktime_add(tk->offs_boot, delta); |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 142 | } |
| 143 | |
John Stultz | ceea5e3 | 2017-06-08 16:44:20 -0700 | [diff] [blame] | 144 | /* |
| 145 | * tk_clock_read - atomic clocksource read() helper |
| 146 | * |
| 147 | * This helper is necessary to use in the read paths because, while the |
| 148 | * seqlock ensures we don't return a bad value while structures are updated, |
| 149 | * it doesn't protect from potential crashes. There is the possibility that |
| 150 | * the tkr's clocksource may change between the read reference, and the |
| 151 | * clock reference passed to the read function. This can cause crashes if |
| 152 | * the wrong clocksource is passed to the wrong read function. |
| 153 | * This isn't necessary to use when holding the timekeeper_lock or doing |
| 154 | * a read of the fast-timekeeper tkrs (which is protected by its own locking |
| 155 | * and update logic). |
| 156 | */ |
| 157 | static inline u64 tk_clock_read(struct tk_read_base *tkr) |
| 158 | { |
| 159 | struct clocksource *clock = READ_ONCE(tkr->clock); |
| 160 | |
| 161 | return clock->read(clock); |
| 162 | } |
| 163 | |
John Stultz | 3c17ad1 | 2015-03-11 21:16:32 -0700 | [diff] [blame] | 164 | #ifdef CONFIG_DEBUG_TIMEKEEPING |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 165 | #define WARNING_FREQ (HZ*300) /* 5 minute rate-limiting */ |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 166 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 167 | static void timekeeping_check_update(struct timekeeper *tk, u64 offset) |
John Stultz | 3c17ad1 | 2015-03-11 21:16:32 -0700 | [diff] [blame] | 168 | { |
| 169 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 170 | u64 max_cycles = tk->tkr_mono.clock->max_cycles; |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 171 | const char *name = tk->tkr_mono.clock->name; |
John Stultz | 3c17ad1 | 2015-03-11 21:16:32 -0700 | [diff] [blame] | 172 | |
| 173 | if (offset > max_cycles) { |
John Stultz | a558cd0 | 2015-03-11 21:16:33 -0700 | [diff] [blame] | 174 | printk_deferred("WARNING: timekeeping: Cycle offset (%lld) is larger than allowed by the '%s' clock's max_cycles value (%lld): time overflow danger\n", |
John Stultz | 3c17ad1 | 2015-03-11 21:16:32 -0700 | [diff] [blame] | 175 | offset, name, max_cycles); |
John Stultz | a558cd0 | 2015-03-11 21:16:33 -0700 | [diff] [blame] | 176 | printk_deferred(" timekeeping: Your kernel is sick, but tries to cope by capping time updates\n"); |
John Stultz | 3c17ad1 | 2015-03-11 21:16:32 -0700 | [diff] [blame] | 177 | } else { |
| 178 | if (offset > (max_cycles >> 1)) { |
Masanari Iida | fc4fa6e | 2015-12-13 15:26:11 +0900 | [diff] [blame] | 179 | printk_deferred("INFO: timekeeping: Cycle offset (%lld) is larger than the '%s' clock's 50%% safety margin (%lld)\n", |
John Stultz | 3c17ad1 | 2015-03-11 21:16:32 -0700 | [diff] [blame] | 180 | offset, name, max_cycles >> 1); |
| 181 | printk_deferred(" timekeeping: Your kernel is still fine, but is feeling a bit nervous\n"); |
| 182 | } |
| 183 | } |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 184 | |
John Stultz | 57d05a9 | 2015-05-13 16:04:47 -0700 | [diff] [blame] | 185 | if (tk->underflow_seen) { |
| 186 | if (jiffies - tk->last_warning > WARNING_FREQ) { |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 187 | printk_deferred("WARNING: Underflow in clocksource '%s' observed, time update ignored.\n", name); |
| 188 | printk_deferred(" Please report this, consider using a different clocksource, if possible.\n"); |
| 189 | printk_deferred(" Your kernel is probably still fine.\n"); |
John Stultz | 57d05a9 | 2015-05-13 16:04:47 -0700 | [diff] [blame] | 190 | tk->last_warning = jiffies; |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 191 | } |
John Stultz | 57d05a9 | 2015-05-13 16:04:47 -0700 | [diff] [blame] | 192 | tk->underflow_seen = 0; |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 193 | } |
| 194 | |
John Stultz | 57d05a9 | 2015-05-13 16:04:47 -0700 | [diff] [blame] | 195 | if (tk->overflow_seen) { |
| 196 | if (jiffies - tk->last_warning > WARNING_FREQ) { |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 197 | printk_deferred("WARNING: Overflow in clocksource '%s' observed, time update capped.\n", name); |
| 198 | printk_deferred(" Please report this, consider using a different clocksource, if possible.\n"); |
| 199 | printk_deferred(" Your kernel is probably still fine.\n"); |
John Stultz | 57d05a9 | 2015-05-13 16:04:47 -0700 | [diff] [blame] | 200 | tk->last_warning = jiffies; |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 201 | } |
John Stultz | 57d05a9 | 2015-05-13 16:04:47 -0700 | [diff] [blame] | 202 | tk->overflow_seen = 0; |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 203 | } |
John Stultz | 3c17ad1 | 2015-03-11 21:16:32 -0700 | [diff] [blame] | 204 | } |
John Stultz | a558cd0 | 2015-03-11 21:16:33 -0700 | [diff] [blame] | 205 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 206 | static inline u64 timekeeping_get_delta(struct tk_read_base *tkr) |
John Stultz | a558cd0 | 2015-03-11 21:16:33 -0700 | [diff] [blame] | 207 | { |
John Stultz | 57d05a9 | 2015-05-13 16:04:47 -0700 | [diff] [blame] | 208 | struct timekeeper *tk = &tk_core.timekeeper; |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 209 | u64 now, last, mask, max, delta; |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 210 | unsigned int seq; |
John Stultz | a558cd0 | 2015-03-11 21:16:33 -0700 | [diff] [blame] | 211 | |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 212 | /* |
| 213 | * Since we're called holding a seqlock, the data may shift |
| 214 | * under us while we're doing the calculation. This can cause |
| 215 | * false positives, since we'd note a problem but throw the |
| 216 | * results away. So nest another seqlock here to atomically |
| 217 | * grab the points we are checking with. |
| 218 | */ |
| 219 | do { |
| 220 | seq = read_seqcount_begin(&tk_core.seq); |
John Stultz | ceea5e3 | 2017-06-08 16:44:20 -0700 | [diff] [blame] | 221 | now = tk_clock_read(tkr); |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 222 | last = tkr->cycle_last; |
| 223 | mask = tkr->mask; |
| 224 | max = tkr->clock->max_cycles; |
| 225 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
John Stultz | a558cd0 | 2015-03-11 21:16:33 -0700 | [diff] [blame] | 226 | |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 227 | delta = clocksource_delta(now, last, mask); |
John Stultz | a558cd0 | 2015-03-11 21:16:33 -0700 | [diff] [blame] | 228 | |
John Stultz | 057b87e | 2015-03-11 21:16:34 -0700 | [diff] [blame] | 229 | /* |
| 230 | * Try to catch underflows by checking if we are seeing small |
| 231 | * mask-relative negative values. |
| 232 | */ |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 233 | if (unlikely((~delta & mask) < (mask >> 3))) { |
John Stultz | 57d05a9 | 2015-05-13 16:04:47 -0700 | [diff] [blame] | 234 | tk->underflow_seen = 1; |
John Stultz | 057b87e | 2015-03-11 21:16:34 -0700 | [diff] [blame] | 235 | delta = 0; |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 236 | } |
John Stultz | 057b87e | 2015-03-11 21:16:34 -0700 | [diff] [blame] | 237 | |
John Stultz | a558cd0 | 2015-03-11 21:16:33 -0700 | [diff] [blame] | 238 | /* Cap delta value to the max_cycles values to avoid mult overflows */ |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 239 | if (unlikely(delta > max)) { |
John Stultz | 57d05a9 | 2015-05-13 16:04:47 -0700 | [diff] [blame] | 240 | tk->overflow_seen = 1; |
John Stultz | a558cd0 | 2015-03-11 21:16:33 -0700 | [diff] [blame] | 241 | delta = tkr->clock->max_cycles; |
John Stultz | 4ca22c2 | 2015-03-11 21:16:35 -0700 | [diff] [blame] | 242 | } |
John Stultz | a558cd0 | 2015-03-11 21:16:33 -0700 | [diff] [blame] | 243 | |
| 244 | return delta; |
| 245 | } |
John Stultz | 3c17ad1 | 2015-03-11 21:16:32 -0700 | [diff] [blame] | 246 | #else |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 247 | static inline void timekeeping_check_update(struct timekeeper *tk, u64 offset) |
John Stultz | 3c17ad1 | 2015-03-11 21:16:32 -0700 | [diff] [blame] | 248 | { |
| 249 | } |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 250 | static inline u64 timekeeping_get_delta(struct tk_read_base *tkr) |
John Stultz | a558cd0 | 2015-03-11 21:16:33 -0700 | [diff] [blame] | 251 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 252 | u64 cycle_now, delta; |
John Stultz | a558cd0 | 2015-03-11 21:16:33 -0700 | [diff] [blame] | 253 | |
| 254 | /* read clocksource */ |
John Stultz | ceea5e3 | 2017-06-08 16:44:20 -0700 | [diff] [blame] | 255 | cycle_now = tk_clock_read(tkr); |
John Stultz | a558cd0 | 2015-03-11 21:16:33 -0700 | [diff] [blame] | 256 | |
| 257 | /* calculate the delta since the last update_wall_time */ |
| 258 | delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask); |
| 259 | |
| 260 | return delta; |
| 261 | } |
John Stultz | 3c17ad1 | 2015-03-11 21:16:32 -0700 | [diff] [blame] | 262 | #endif |
| 263 | |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 264 | /** |
Yijing Wang | d26e4fe | 2013-11-28 16:28:55 +0800 | [diff] [blame] | 265 | * tk_setup_internals - Set up internals to use clocksource clock. |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 266 | * |
Yijing Wang | d26e4fe | 2013-11-28 16:28:55 +0800 | [diff] [blame] | 267 | * @tk: The target timekeeper to setup. |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 268 | * @clock: Pointer to clocksource. |
| 269 | * |
| 270 | * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment |
| 271 | * pair and interval request. |
| 272 | * |
| 273 | * Unless you're the timekeeping code, you should not be using this! |
| 274 | */ |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 275 | static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 276 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 277 | u64 interval; |
Kasper Pedersen | a386b5a | 2010-10-20 15:55:15 -0700 | [diff] [blame] | 278 | u64 tmp, ntpinterval; |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 279 | struct clocksource *old_clock; |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 280 | |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 281 | ++tk->cs_was_changed_seq; |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 282 | old_clock = tk->tkr_mono.clock; |
| 283 | tk->tkr_mono.clock = clock; |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 284 | tk->tkr_mono.mask = clock->mask; |
John Stultz | ceea5e3 | 2017-06-08 16:44:20 -0700 | [diff] [blame] | 285 | tk->tkr_mono.cycle_last = tk_clock_read(&tk->tkr_mono); |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 286 | |
Peter Zijlstra | 4a4ad80 | 2015-03-19 09:28:44 +0100 | [diff] [blame] | 287 | tk->tkr_raw.clock = clock; |
Peter Zijlstra | 4a4ad80 | 2015-03-19 09:28:44 +0100 | [diff] [blame] | 288 | tk->tkr_raw.mask = clock->mask; |
| 289 | tk->tkr_raw.cycle_last = tk->tkr_mono.cycle_last; |
| 290 | |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 291 | /* Do the ns -> cycle conversion first, using original mult */ |
| 292 | tmp = NTP_INTERVAL_LENGTH; |
| 293 | tmp <<= clock->shift; |
Kasper Pedersen | a386b5a | 2010-10-20 15:55:15 -0700 | [diff] [blame] | 294 | ntpinterval = tmp; |
Martin Schwidefsky | 0a54419 | 2009-08-14 15:47:28 +0200 | [diff] [blame] | 295 | tmp += clock->mult/2; |
| 296 | do_div(tmp, clock->mult); |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 297 | if (tmp == 0) |
| 298 | tmp = 1; |
| 299 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 300 | interval = (u64) tmp; |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 301 | tk->cycle_interval = interval; |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 302 | |
| 303 | /* Go back from cycles -> shifted ns */ |
Thomas Gleixner | cbd99e3 | 2016-12-08 20:49:36 +0000 | [diff] [blame] | 304 | tk->xtime_interval = interval * clock->mult; |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 305 | tk->xtime_remainder = ntpinterval - tk->xtime_interval; |
John Stultz | 3d88d56 | 2017-06-08 16:44:21 -0700 | [diff] [blame] | 306 | tk->raw_interval = interval * clock->mult; |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 307 | |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 308 | /* if changing clocks, convert xtime_nsec shift units */ |
| 309 | if (old_clock) { |
| 310 | int shift_change = clock->shift - old_clock->shift; |
John Stultz | fc6eead7 | 2017-05-22 17:20:20 -0700 | [diff] [blame] | 311 | if (shift_change < 0) { |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 312 | tk->tkr_mono.xtime_nsec >>= -shift_change; |
John Stultz | fc6eead7 | 2017-05-22 17:20:20 -0700 | [diff] [blame] | 313 | tk->tkr_raw.xtime_nsec >>= -shift_change; |
| 314 | } else { |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 315 | tk->tkr_mono.xtime_nsec <<= shift_change; |
John Stultz | fc6eead7 | 2017-05-22 17:20:20 -0700 | [diff] [blame] | 316 | tk->tkr_raw.xtime_nsec <<= shift_change; |
| 317 | } |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 318 | } |
Peter Zijlstra | 4a4ad80 | 2015-03-19 09:28:44 +0100 | [diff] [blame] | 319 | |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 320 | tk->tkr_mono.shift = clock->shift; |
Peter Zijlstra | 4a4ad80 | 2015-03-19 09:28:44 +0100 | [diff] [blame] | 321 | tk->tkr_raw.shift = clock->shift; |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 322 | |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 323 | tk->ntp_error = 0; |
| 324 | tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift; |
John Stultz | 375f45b | 2014-04-23 20:53:29 -0700 | [diff] [blame] | 325 | tk->ntp_tick = ntpinterval << tk->ntp_error_shift; |
Martin Schwidefsky | 0a54419 | 2009-08-14 15:47:28 +0200 | [diff] [blame] | 326 | |
| 327 | /* |
| 328 | * The timekeeper keeps its own mult values for the currently |
| 329 | * active clocksource. These value will be adjusted via NTP |
| 330 | * to counteract clock drifting. |
| 331 | */ |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 332 | tk->tkr_mono.mult = clock->mult; |
Peter Zijlstra | 4a4ad80 | 2015-03-19 09:28:44 +0100 | [diff] [blame] | 333 | tk->tkr_raw.mult = clock->mult; |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 334 | tk->ntp_err_mult = 0; |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 335 | } |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 336 | |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 337 | /* Timekeeper helper functions. */ |
Stephen Warren | 7b1f620 | 2012-11-07 17:58:54 -0700 | [diff] [blame] | 338 | |
| 339 | #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET |
Thomas Gleixner | e06fde3 | 2014-07-16 21:03:50 +0000 | [diff] [blame] | 340 | static u32 default_arch_gettimeoffset(void) { return 0; } |
| 341 | u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset; |
Stephen Warren | 7b1f620 | 2012-11-07 17:58:54 -0700 | [diff] [blame] | 342 | #else |
Thomas Gleixner | e06fde3 | 2014-07-16 21:03:50 +0000 | [diff] [blame] | 343 | static inline u32 arch_gettimeoffset(void) { return 0; } |
Stephen Warren | 7b1f620 | 2012-11-07 17:58:54 -0700 | [diff] [blame] | 344 | #endif |
| 345 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 346 | static inline u64 timekeeping_delta_to_ns(struct tk_read_base *tkr, u64 delta) |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 347 | { |
Thomas Gleixner | 9c16457 | 2016-12-08 20:49:32 +0000 | [diff] [blame] | 348 | u64 nsec; |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 349 | |
Christopher S. Hall | 6bd58f0 | 2016-02-22 03:15:19 -0800 | [diff] [blame] | 350 | nsec = delta * tkr->mult + tkr->xtime_nsec; |
| 351 | nsec >>= tkr->shift; |
John Stultz | f2a5a08 | 2012-07-13 01:21:55 -0400 | [diff] [blame] | 352 | |
Stephen Warren | 7b1f620 | 2012-11-07 17:58:54 -0700 | [diff] [blame] | 353 | /* If arch requires, add in get_arch_timeoffset() */ |
Thomas Gleixner | e06fde3 | 2014-07-16 21:03:50 +0000 | [diff] [blame] | 354 | return nsec + arch_gettimeoffset(); |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 355 | } |
| 356 | |
Thomas Gleixner | acc8961 | 2016-12-08 20:49:34 +0000 | [diff] [blame] | 357 | static inline u64 timekeeping_get_ns(struct tk_read_base *tkr) |
Christopher S. Hall | 6bd58f0 | 2016-02-22 03:15:19 -0800 | [diff] [blame] | 358 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 359 | u64 delta; |
Christopher S. Hall | 6bd58f0 | 2016-02-22 03:15:19 -0800 | [diff] [blame] | 360 | |
| 361 | delta = timekeeping_get_delta(tkr); |
| 362 | return timekeeping_delta_to_ns(tkr, delta); |
| 363 | } |
| 364 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 365 | static inline u64 timekeeping_cycles_to_ns(struct tk_read_base *tkr, u64 cycles) |
Christopher S. Hall | 6bd58f0 | 2016-02-22 03:15:19 -0800 | [diff] [blame] | 366 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 367 | u64 delta; |
Christopher S. Hall | 6bd58f0 | 2016-02-22 03:15:19 -0800 | [diff] [blame] | 368 | |
| 369 | /* calculate the delta since the last update_wall_time */ |
| 370 | delta = clocksource_delta(cycles, tkr->cycle_last, tkr->mask); |
| 371 | return timekeeping_delta_to_ns(tkr, delta); |
| 372 | } |
| 373 | |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 374 | /** |
| 375 | * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper. |
Rafael J. Wysocki | affe3e8 | 2015-02-11 05:01:52 +0100 | [diff] [blame] | 376 | * @tkr: Timekeeping readout base from which we take the update |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 377 | * |
| 378 | * We want to use this from any context including NMI and tracing / |
| 379 | * instrumenting the timekeeping code itself. |
| 380 | * |
Peter Zijlstra | 6695b92 | 2015-05-27 11:09:36 +0930 | [diff] [blame] | 381 | * Employ the latch technique; see @raw_write_seqcount_latch. |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 382 | * |
| 383 | * So if a NMI hits the update of base[0] then it will use base[1] |
| 384 | * which is still consistent. In the worst case this can result is a |
| 385 | * slightly wrong timestamp (a few nanoseconds). See |
| 386 | * @ktime_get_mono_fast_ns. |
| 387 | */ |
Peter Zijlstra | 4498e74 | 2015-03-19 09:36:19 +0100 | [diff] [blame] | 388 | static void update_fast_timekeeper(struct tk_read_base *tkr, struct tk_fast *tkf) |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 389 | { |
Peter Zijlstra | 4498e74 | 2015-03-19 09:36:19 +0100 | [diff] [blame] | 390 | struct tk_read_base *base = tkf->base; |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 391 | |
| 392 | /* Force readers off to base[1] */ |
Peter Zijlstra | 4498e74 | 2015-03-19 09:36:19 +0100 | [diff] [blame] | 393 | raw_write_seqcount_latch(&tkf->seq); |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 394 | |
| 395 | /* Update base[0] */ |
Rafael J. Wysocki | affe3e8 | 2015-02-11 05:01:52 +0100 | [diff] [blame] | 396 | memcpy(base, tkr, sizeof(*base)); |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 397 | |
| 398 | /* Force readers back to base[0] */ |
Peter Zijlstra | 4498e74 | 2015-03-19 09:36:19 +0100 | [diff] [blame] | 399 | raw_write_seqcount_latch(&tkf->seq); |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 400 | |
| 401 | /* Update base[1] */ |
| 402 | memcpy(base + 1, base, sizeof(*base)); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic |
| 407 | * |
| 408 | * This timestamp is not guaranteed to be monotonic across an update. |
| 409 | * The timestamp is calculated by: |
| 410 | * |
| 411 | * now = base_mono + clock_delta * slope |
| 412 | * |
| 413 | * So if the update lowers the slope, readers who are forced to the |
| 414 | * not yet updated second array are still using the old steeper slope. |
| 415 | * |
| 416 | * tmono |
| 417 | * ^ |
| 418 | * | o n |
| 419 | * | o n |
| 420 | * | u |
| 421 | * | o |
| 422 | * |o |
| 423 | * |12345678---> reader order |
| 424 | * |
| 425 | * o = old slope |
| 426 | * u = update |
| 427 | * n = new slope |
| 428 | * |
| 429 | * So reader 6 will observe time going backwards versus reader 5. |
| 430 | * |
| 431 | * While other CPUs are likely to be able observe that, the only way |
| 432 | * for a CPU local observation is when an NMI hits in the middle of |
| 433 | * the update. Timestamps taken from that NMI context might be ahead |
| 434 | * of the following timestamps. Callers need to be aware of that and |
| 435 | * deal with it. |
| 436 | */ |
Peter Zijlstra | 4498e74 | 2015-03-19 09:36:19 +0100 | [diff] [blame] | 437 | static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf) |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 438 | { |
| 439 | struct tk_read_base *tkr; |
| 440 | unsigned int seq; |
| 441 | u64 now; |
| 442 | |
| 443 | do { |
Peter Zijlstra | 7fc2632 | 2015-05-27 11:09:36 +0930 | [diff] [blame] | 444 | seq = raw_read_seqcount_latch(&tkf->seq); |
Peter Zijlstra | 4498e74 | 2015-03-19 09:36:19 +0100 | [diff] [blame] | 445 | tkr = tkf->base + (seq & 0x01); |
John Stultz | 27727df | 2016-08-23 16:08:21 -0700 | [diff] [blame] | 446 | now = ktime_to_ns(tkr->base); |
| 447 | |
John Stultz | 58bfea9 | 2016-10-04 19:55:48 -0700 | [diff] [blame] | 448 | now += timekeeping_delta_to_ns(tkr, |
| 449 | clocksource_delta( |
John Stultz | ceea5e3 | 2017-06-08 16:44:20 -0700 | [diff] [blame] | 450 | tk_clock_read(tkr), |
John Stultz | 58bfea9 | 2016-10-04 19:55:48 -0700 | [diff] [blame] | 451 | tkr->cycle_last, |
| 452 | tkr->mask)); |
Peter Zijlstra | 4498e74 | 2015-03-19 09:36:19 +0100 | [diff] [blame] | 453 | } while (read_seqcount_retry(&tkf->seq, seq)); |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 454 | |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 455 | return now; |
| 456 | } |
Peter Zijlstra | 4498e74 | 2015-03-19 09:36:19 +0100 | [diff] [blame] | 457 | |
| 458 | u64 ktime_get_mono_fast_ns(void) |
| 459 | { |
| 460 | return __ktime_get_fast_ns(&tk_fast_mono); |
| 461 | } |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 462 | EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns); |
| 463 | |
Peter Zijlstra | f09cb9a | 2015-03-19 09:39:08 +0100 | [diff] [blame] | 464 | u64 ktime_get_raw_fast_ns(void) |
| 465 | { |
| 466 | return __ktime_get_fast_ns(&tk_fast_raw); |
| 467 | } |
| 468 | EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns); |
| 469 | |
Joel Fernandes | 948a531 | 2016-11-28 14:35:22 -0800 | [diff] [blame] | 470 | /** |
| 471 | * ktime_get_boot_fast_ns - NMI safe and fast access to boot clock. |
| 472 | * |
| 473 | * To keep it NMI safe since we're accessing from tracing, we're not using a |
| 474 | * separate timekeeper with updates to monotonic clock and boot offset |
| 475 | * protected with seqlocks. This has the following minor side effects: |
| 476 | * |
| 477 | * (1) Its possible that a timestamp be taken after the boot offset is updated |
| 478 | * but before the timekeeper is updated. If this happens, the new boot offset |
| 479 | * is added to the old timekeeping making the clock appear to update slightly |
| 480 | * earlier: |
| 481 | * CPU 0 CPU 1 |
| 482 | * timekeeping_inject_sleeptime64() |
| 483 | * __timekeeping_inject_sleeptime(tk, delta); |
| 484 | * timestamp(); |
| 485 | * timekeeping_update(tk, TK_CLEAR_NTP...); |
| 486 | * |
| 487 | * (2) On 32-bit systems, the 64-bit boot offset (tk->offs_boot) may be |
| 488 | * partially updated. Since the tk->offs_boot update is a rare event, this |
| 489 | * should be a rare occurrence which postprocessing should be able to handle. |
| 490 | */ |
| 491 | u64 notrace ktime_get_boot_fast_ns(void) |
| 492 | { |
| 493 | struct timekeeper *tk = &tk_core.timekeeper; |
| 494 | |
| 495 | return (ktime_get_mono_fast_ns() + ktime_to_ns(tk->offs_boot)); |
| 496 | } |
| 497 | EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns); |
| 498 | |
Rafael J. Wysocki | 060407a | 2015-02-13 14:49:02 +0100 | [diff] [blame] | 499 | /** |
| 500 | * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource. |
| 501 | * @tk: Timekeeper to snapshot. |
| 502 | * |
| 503 | * It generally is unsafe to access the clocksource after timekeeping has been |
| 504 | * suspended, so take a snapshot of the readout base of @tk and use it as the |
| 505 | * fast timekeeper's readout base while suspended. It will return the same |
| 506 | * number of cycles every time until timekeeping is resumed at which time the |
| 507 | * proper readout base for the fast timekeeper will be restored automatically. |
| 508 | */ |
| 509 | static void halt_fast_timekeeper(struct timekeeper *tk) |
| 510 | { |
| 511 | static struct tk_read_base tkr_dummy; |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 512 | struct tk_read_base *tkr = &tk->tkr_mono; |
Rafael J. Wysocki | 060407a | 2015-02-13 14:49:02 +0100 | [diff] [blame] | 513 | |
| 514 | memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy)); |
John Stultz | ceea5e3 | 2017-06-08 16:44:20 -0700 | [diff] [blame] | 515 | cycles_at_suspend = tk_clock_read(tkr); |
| 516 | tkr_dummy.clock = &dummy_clock; |
Peter Zijlstra | 4498e74 | 2015-03-19 09:36:19 +0100 | [diff] [blame] | 517 | update_fast_timekeeper(&tkr_dummy, &tk_fast_mono); |
Peter Zijlstra | f09cb9a | 2015-03-19 09:39:08 +0100 | [diff] [blame] | 518 | |
| 519 | tkr = &tk->tkr_raw; |
| 520 | memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy)); |
John Stultz | ceea5e3 | 2017-06-08 16:44:20 -0700 | [diff] [blame] | 521 | tkr_dummy.clock = &dummy_clock; |
Peter Zijlstra | f09cb9a | 2015-03-19 09:39:08 +0100 | [diff] [blame] | 522 | update_fast_timekeeper(&tkr_dummy, &tk_fast_raw); |
Rafael J. Wysocki | 060407a | 2015-02-13 14:49:02 +0100 | [diff] [blame] | 523 | } |
| 524 | |
Thomas Gleixner | c905fae | 2014-07-16 21:04:05 +0000 | [diff] [blame] | 525 | #ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD |
John Stultz | 369adf0 | 2017-05-12 10:58:18 -0700 | [diff] [blame] | 526 | #warning Please contact your maintainers, as GENERIC_TIME_VSYSCALL_OLD compatibity will disappear soon. |
Thomas Gleixner | c905fae | 2014-07-16 21:04:05 +0000 | [diff] [blame] | 527 | |
| 528 | static inline void update_vsyscall(struct timekeeper *tk) |
| 529 | { |
John Stultz | 0680eb1 | 2014-08-13 12:47:14 -0700 | [diff] [blame] | 530 | struct timespec xt, wm; |
Thomas Gleixner | c905fae | 2014-07-16 21:04:05 +0000 | [diff] [blame] | 531 | |
John Stultz | e2dff1e | 2014-07-23 14:35:39 -0700 | [diff] [blame] | 532 | xt = timespec64_to_timespec(tk_xtime(tk)); |
John Stultz | 0680eb1 | 2014-08-13 12:47:14 -0700 | [diff] [blame] | 533 | wm = timespec64_to_timespec(tk->wall_to_monotonic); |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 534 | update_vsyscall_old(&xt, &wm, tk->tkr_mono.clock, tk->tkr_mono.mult, |
| 535 | tk->tkr_mono.cycle_last); |
Thomas Gleixner | c905fae | 2014-07-16 21:04:05 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static inline void old_vsyscall_fixup(struct timekeeper *tk) |
| 539 | { |
| 540 | s64 remainder; |
| 541 | |
| 542 | /* |
| 543 | * Store only full nanoseconds into xtime_nsec after rounding |
| 544 | * it up and add the remainder to the error difference. |
| 545 | * XXX - This is necessary to avoid small 1ns inconsistnecies caused |
| 546 | * by truncating the remainder in vsyscalls. However, it causes |
| 547 | * additional work to be done in timekeeping_adjust(). Once |
| 548 | * the vsyscall implementations are converted to use xtime_nsec |
| 549 | * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD |
| 550 | * users are removed, this can be killed. |
| 551 | */ |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 552 | remainder = tk->tkr_mono.xtime_nsec & ((1ULL << tk->tkr_mono.shift) - 1); |
Thomas Graziadei | 0209b93 | 2016-05-31 15:06:06 +0200 | [diff] [blame] | 553 | if (remainder != 0) { |
| 554 | tk->tkr_mono.xtime_nsec -= remainder; |
| 555 | tk->tkr_mono.xtime_nsec += 1ULL << tk->tkr_mono.shift; |
| 556 | tk->ntp_error += remainder << tk->ntp_error_shift; |
| 557 | tk->ntp_error -= (1ULL << tk->tkr_mono.shift) << tk->ntp_error_shift; |
| 558 | } |
Thomas Gleixner | c905fae | 2014-07-16 21:04:05 +0000 | [diff] [blame] | 559 | } |
| 560 | #else |
| 561 | #define old_vsyscall_fixup(tk) |
| 562 | #endif |
| 563 | |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 564 | static RAW_NOTIFIER_HEAD(pvclock_gtod_chain); |
| 565 | |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 566 | static void update_pvclock_gtod(struct timekeeper *tk, bool was_set) |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 567 | { |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 568 | raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk); |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /** |
| 572 | * pvclock_gtod_register_notifier - register a pvclock timedata update listener |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 573 | */ |
| 574 | int pvclock_gtod_register_notifier(struct notifier_block *nb) |
| 575 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 576 | struct timekeeper *tk = &tk_core.timekeeper; |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 577 | unsigned long flags; |
| 578 | int ret; |
| 579 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 580 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 581 | ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb); |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 582 | update_pvclock_gtod(tk, true); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 583 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 584 | |
| 585 | return ret; |
| 586 | } |
| 587 | EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier); |
| 588 | |
| 589 | /** |
| 590 | * pvclock_gtod_unregister_notifier - unregister a pvclock |
| 591 | * timedata update listener |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 592 | */ |
| 593 | int pvclock_gtod_unregister_notifier(struct notifier_block *nb) |
| 594 | { |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 595 | unsigned long flags; |
| 596 | int ret; |
| 597 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 598 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 599 | ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 600 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 601 | |
| 602 | return ret; |
| 603 | } |
| 604 | EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier); |
| 605 | |
Thomas Gleixner | 7c032df | 2014-07-16 21:04:10 +0000 | [diff] [blame] | 606 | /* |
John Stultz | 833f32d | 2015-06-11 15:54:55 -0700 | [diff] [blame] | 607 | * tk_update_leap_state - helper to update the next_leap_ktime |
| 608 | */ |
| 609 | static inline void tk_update_leap_state(struct timekeeper *tk) |
| 610 | { |
| 611 | tk->next_leap_ktime = ntp_get_next_leap(); |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 612 | if (tk->next_leap_ktime != KTIME_MAX) |
John Stultz | 833f32d | 2015-06-11 15:54:55 -0700 | [diff] [blame] | 613 | /* Convert to monotonic time */ |
| 614 | tk->next_leap_ktime = ktime_sub(tk->next_leap_ktime, tk->offs_real); |
| 615 | } |
| 616 | |
| 617 | /* |
Thomas Gleixner | 7c032df | 2014-07-16 21:04:10 +0000 | [diff] [blame] | 618 | * Update the ktime_t based scalar nsec members of the timekeeper |
| 619 | */ |
| 620 | static inline void tk_update_ktime_data(struct timekeeper *tk) |
| 621 | { |
Heena Sirwani | 9e3680b | 2014-10-29 16:01:16 +0530 | [diff] [blame] | 622 | u64 seconds; |
| 623 | u32 nsec; |
Thomas Gleixner | 7c032df | 2014-07-16 21:04:10 +0000 | [diff] [blame] | 624 | |
| 625 | /* |
| 626 | * The xtime based monotonic readout is: |
| 627 | * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now(); |
| 628 | * The ktime based monotonic readout is: |
| 629 | * nsec = base_mono + now(); |
| 630 | * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec |
| 631 | */ |
Heena Sirwani | 9e3680b | 2014-10-29 16:01:16 +0530 | [diff] [blame] | 632 | seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec); |
| 633 | nsec = (u32) tk->wall_to_monotonic.tv_nsec; |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 634 | tk->tkr_mono.base = ns_to_ktime(seconds * NSEC_PER_SEC + nsec); |
Thomas Gleixner | f519b1a | 2014-07-16 21:05:04 +0000 | [diff] [blame] | 635 | |
Heena Sirwani | 9e3680b | 2014-10-29 16:01:16 +0530 | [diff] [blame] | 636 | /* |
| 637 | * The sum of the nanoseconds portions of xtime and |
| 638 | * wall_to_monotonic can be greater/equal one second. Take |
| 639 | * this into account before updating tk->ktime_sec. |
| 640 | */ |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 641 | nsec += (u32)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift); |
Heena Sirwani | 9e3680b | 2014-10-29 16:01:16 +0530 | [diff] [blame] | 642 | if (nsec >= NSEC_PER_SEC) |
| 643 | seconds++; |
| 644 | tk->ktime_sec = seconds; |
John Stultz | fc6eead7 | 2017-05-22 17:20:20 -0700 | [diff] [blame] | 645 | |
| 646 | /* Update the monotonic raw base */ |
John Stultz | 0bcdc09 | 2017-08-25 15:57:04 -0700 | [diff] [blame] | 647 | tk->tkr_raw.base = ns_to_ktime(tk->raw_sec * NSEC_PER_SEC); |
Thomas Gleixner | 7c032df | 2014-07-16 21:04:10 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 650 | /* must hold timekeeper_lock */ |
David Vrabel | 04397fe9 | 2013-06-27 11:35:45 +0100 | [diff] [blame] | 651 | static void timekeeping_update(struct timekeeper *tk, unsigned int action) |
Thomas Gleixner | cc06268 | 2011-11-13 23:19:49 +0000 | [diff] [blame] | 652 | { |
David Vrabel | 04397fe9 | 2013-06-27 11:35:45 +0100 | [diff] [blame] | 653 | if (action & TK_CLEAR_NTP) { |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 654 | tk->ntp_error = 0; |
Thomas Gleixner | cc06268 | 2011-11-13 23:19:49 +0000 | [diff] [blame] | 655 | ntp_clear(); |
| 656 | } |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 657 | |
John Stultz | 833f32d | 2015-06-11 15:54:55 -0700 | [diff] [blame] | 658 | tk_update_leap_state(tk); |
Thomas Gleixner | 7c032df | 2014-07-16 21:04:10 +0000 | [diff] [blame] | 659 | tk_update_ktime_data(tk); |
| 660 | |
Thomas Gleixner | 9bf2419 | 2014-09-06 12:24:49 +0200 | [diff] [blame] | 661 | update_vsyscall(tk); |
| 662 | update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET); |
| 663 | |
Peter Zijlstra | 4498e74 | 2015-03-19 09:36:19 +0100 | [diff] [blame] | 664 | update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono); |
Peter Zijlstra | f09cb9a | 2015-03-19 09:39:08 +0100 | [diff] [blame] | 665 | update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw); |
Thomas Gleixner | 868a3e9 | 2015-04-14 21:08:37 +0000 | [diff] [blame] | 666 | |
| 667 | if (action & TK_CLOCK_WAS_SET) |
| 668 | tk->clock_was_set_seq++; |
John Stultz | d151832 | 2015-06-11 15:54:53 -0700 | [diff] [blame] | 669 | /* |
| 670 | * The mirroring of the data to the shadow-timekeeper needs |
| 671 | * to happen last here to ensure we don't over-write the |
| 672 | * timekeeper structure on the next update with stale data |
| 673 | */ |
| 674 | if (action & TK_MIRROR) |
| 675 | memcpy(&shadow_timekeeper, &tk_core.timekeeper, |
| 676 | sizeof(tk_core.timekeeper)); |
Thomas Gleixner | cc06268 | 2011-11-13 23:19:49 +0000 | [diff] [blame] | 677 | } |
| 678 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 679 | /** |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 680 | * timekeeping_forward_now - update clock to the current time |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 681 | * |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 682 | * Forward the current clock to update its state since the last call to |
| 683 | * update_wall_time(). This is useful before significant clock changes, |
| 684 | * as it avoids having to deal with this time offset explicitly. |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 685 | */ |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 686 | static void timekeeping_forward_now(struct timekeeper *tk) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 687 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 688 | u64 cycle_now, delta; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 689 | |
John Stultz | ceea5e3 | 2017-06-08 16:44:20 -0700 | [diff] [blame] | 690 | cycle_now = tk_clock_read(&tk->tkr_mono); |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 691 | delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask); |
| 692 | tk->tkr_mono.cycle_last = cycle_now; |
Peter Zijlstra | 4a4ad80 | 2015-03-19 09:28:44 +0100 | [diff] [blame] | 693 | tk->tkr_raw.cycle_last = cycle_now; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 694 | |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 695 | tk->tkr_mono.xtime_nsec += delta * tk->tkr_mono.mult; |
john stultz | 7d27558 | 2009-05-01 13:10:26 -0700 | [diff] [blame] | 696 | |
Stephen Warren | 7b1f620 | 2012-11-07 17:58:54 -0700 | [diff] [blame] | 697 | /* If arch requires, add in get_arch_timeoffset() */ |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 698 | tk->tkr_mono.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_mono.shift; |
john stultz | 7d27558 | 2009-05-01 13:10:26 -0700 | [diff] [blame] | 699 | |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 700 | |
John Stultz | fc6eead7 | 2017-05-22 17:20:20 -0700 | [diff] [blame] | 701 | tk->tkr_raw.xtime_nsec += delta * tk->tkr_raw.mult; |
| 702 | |
| 703 | /* If arch requires, add in get_arch_timeoffset() */ |
| 704 | tk->tkr_raw.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr_raw.shift; |
| 705 | |
| 706 | tk_normalize_xtime(tk); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /** |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 710 | * __getnstimeofday64 - Returns the time of day in a timespec64. |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 711 | * @ts: pointer to the timespec to be set |
| 712 | * |
Kees Cook | 1e817fb | 2012-11-19 10:26:16 -0800 | [diff] [blame] | 713 | * Updates the time of day in the timespec. |
| 714 | * Returns 0 on success, or -ve when suspended (timespec will be undefined). |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 715 | */ |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 716 | int __getnstimeofday64(struct timespec64 *ts) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 717 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 718 | struct timekeeper *tk = &tk_core.timekeeper; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 719 | unsigned long seq; |
Thomas Gleixner | acc8961 | 2016-12-08 20:49:34 +0000 | [diff] [blame] | 720 | u64 nsecs; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 721 | |
| 722 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 723 | seq = read_seqcount_begin(&tk_core.seq); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 724 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 725 | ts->tv_sec = tk->xtime_sec; |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 726 | nsecs = timekeeping_get_ns(&tk->tkr_mono); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 727 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 728 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 729 | |
John Stultz | ec145ba | 2012-09-11 19:26:03 -0400 | [diff] [blame] | 730 | ts->tv_nsec = 0; |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 731 | timespec64_add_ns(ts, nsecs); |
Kees Cook | 1e817fb | 2012-11-19 10:26:16 -0800 | [diff] [blame] | 732 | |
| 733 | /* |
| 734 | * Do not bail out early, in case there were callers still using |
| 735 | * the value, even in the face of the WARN_ON. |
| 736 | */ |
| 737 | if (unlikely(timekeeping_suspended)) |
| 738 | return -EAGAIN; |
| 739 | return 0; |
| 740 | } |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 741 | EXPORT_SYMBOL(__getnstimeofday64); |
Kees Cook | 1e817fb | 2012-11-19 10:26:16 -0800 | [diff] [blame] | 742 | |
| 743 | /** |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 744 | * getnstimeofday64 - Returns the time of day in a timespec64. |
John Stultz | 5322e4c | 2014-11-07 13:13:04 -0800 | [diff] [blame] | 745 | * @ts: pointer to the timespec64 to be set |
Kees Cook | 1e817fb | 2012-11-19 10:26:16 -0800 | [diff] [blame] | 746 | * |
John Stultz | 5322e4c | 2014-11-07 13:13:04 -0800 | [diff] [blame] | 747 | * Returns the time of day in a timespec64 (WARN if suspended). |
Kees Cook | 1e817fb | 2012-11-19 10:26:16 -0800 | [diff] [blame] | 748 | */ |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 749 | void getnstimeofday64(struct timespec64 *ts) |
Kees Cook | 1e817fb | 2012-11-19 10:26:16 -0800 | [diff] [blame] | 750 | { |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 751 | WARN_ON(__getnstimeofday64(ts)); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 752 | } |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 753 | EXPORT_SYMBOL(getnstimeofday64); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 754 | |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 755 | ktime_t ktime_get(void) |
| 756 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 757 | struct timekeeper *tk = &tk_core.timekeeper; |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 758 | unsigned int seq; |
Thomas Gleixner | a016a5b | 2014-07-16 21:04:12 +0000 | [diff] [blame] | 759 | ktime_t base; |
Thomas Gleixner | acc8961 | 2016-12-08 20:49:34 +0000 | [diff] [blame] | 760 | u64 nsecs; |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 761 | |
| 762 | WARN_ON(timekeeping_suspended); |
| 763 | |
| 764 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 765 | seq = read_seqcount_begin(&tk_core.seq); |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 766 | base = tk->tkr_mono.base; |
| 767 | nsecs = timekeeping_get_ns(&tk->tkr_mono); |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 768 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 769 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
John Stultz | 24e4a8c | 2014-07-16 21:03:53 +0000 | [diff] [blame] | 770 | |
Thomas Gleixner | a016a5b | 2014-07-16 21:04:12 +0000 | [diff] [blame] | 771 | return ktime_add_ns(base, nsecs); |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 772 | } |
| 773 | EXPORT_SYMBOL_GPL(ktime_get); |
| 774 | |
Harald Geyer | 6374f91 | 2015-04-07 11:12:35 +0000 | [diff] [blame] | 775 | u32 ktime_get_resolution_ns(void) |
| 776 | { |
| 777 | struct timekeeper *tk = &tk_core.timekeeper; |
| 778 | unsigned int seq; |
| 779 | u32 nsecs; |
| 780 | |
| 781 | WARN_ON(timekeeping_suspended); |
| 782 | |
| 783 | do { |
| 784 | seq = read_seqcount_begin(&tk_core.seq); |
| 785 | nsecs = tk->tkr_mono.mult >> tk->tkr_mono.shift; |
| 786 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
| 787 | |
| 788 | return nsecs; |
| 789 | } |
| 790 | EXPORT_SYMBOL_GPL(ktime_get_resolution_ns); |
| 791 | |
Thomas Gleixner | 0077dc6 | 2014-07-16 21:04:13 +0000 | [diff] [blame] | 792 | static ktime_t *offsets[TK_OFFS_MAX] = { |
| 793 | [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real, |
| 794 | [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot, |
| 795 | [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai, |
| 796 | }; |
| 797 | |
| 798 | ktime_t ktime_get_with_offset(enum tk_offsets offs) |
| 799 | { |
| 800 | struct timekeeper *tk = &tk_core.timekeeper; |
| 801 | unsigned int seq; |
| 802 | ktime_t base, *offset = offsets[offs]; |
Thomas Gleixner | acc8961 | 2016-12-08 20:49:34 +0000 | [diff] [blame] | 803 | u64 nsecs; |
Thomas Gleixner | 0077dc6 | 2014-07-16 21:04:13 +0000 | [diff] [blame] | 804 | |
| 805 | WARN_ON(timekeeping_suspended); |
| 806 | |
| 807 | do { |
| 808 | seq = read_seqcount_begin(&tk_core.seq); |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 809 | base = ktime_add(tk->tkr_mono.base, *offset); |
| 810 | nsecs = timekeeping_get_ns(&tk->tkr_mono); |
Thomas Gleixner | 0077dc6 | 2014-07-16 21:04:13 +0000 | [diff] [blame] | 811 | |
| 812 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
| 813 | |
| 814 | return ktime_add_ns(base, nsecs); |
| 815 | |
| 816 | } |
| 817 | EXPORT_SYMBOL_GPL(ktime_get_with_offset); |
| 818 | |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 819 | /** |
Thomas Gleixner | 9a6b519 | 2014-07-16 21:04:22 +0000 | [diff] [blame] | 820 | * ktime_mono_to_any() - convert mononotic time to any other time |
| 821 | * @tmono: time to convert. |
| 822 | * @offs: which offset to use |
| 823 | */ |
| 824 | ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs) |
| 825 | { |
| 826 | ktime_t *offset = offsets[offs]; |
| 827 | unsigned long seq; |
| 828 | ktime_t tconv; |
| 829 | |
| 830 | do { |
| 831 | seq = read_seqcount_begin(&tk_core.seq); |
| 832 | tconv = ktime_add(tmono, *offset); |
| 833 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
| 834 | |
| 835 | return tconv; |
| 836 | } |
| 837 | EXPORT_SYMBOL_GPL(ktime_mono_to_any); |
| 838 | |
| 839 | /** |
Thomas Gleixner | f519b1a | 2014-07-16 21:05:04 +0000 | [diff] [blame] | 840 | * ktime_get_raw - Returns the raw monotonic time in ktime_t format |
| 841 | */ |
| 842 | ktime_t ktime_get_raw(void) |
| 843 | { |
| 844 | struct timekeeper *tk = &tk_core.timekeeper; |
| 845 | unsigned int seq; |
| 846 | ktime_t base; |
Thomas Gleixner | acc8961 | 2016-12-08 20:49:34 +0000 | [diff] [blame] | 847 | u64 nsecs; |
Thomas Gleixner | f519b1a | 2014-07-16 21:05:04 +0000 | [diff] [blame] | 848 | |
| 849 | do { |
| 850 | seq = read_seqcount_begin(&tk_core.seq); |
Peter Zijlstra | 4a4ad80 | 2015-03-19 09:28:44 +0100 | [diff] [blame] | 851 | base = tk->tkr_raw.base; |
| 852 | nsecs = timekeeping_get_ns(&tk->tkr_raw); |
Thomas Gleixner | f519b1a | 2014-07-16 21:05:04 +0000 | [diff] [blame] | 853 | |
| 854 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
| 855 | |
| 856 | return ktime_add_ns(base, nsecs); |
| 857 | } |
| 858 | EXPORT_SYMBOL_GPL(ktime_get_raw); |
| 859 | |
| 860 | /** |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 861 | * ktime_get_ts64 - get the monotonic clock in timespec64 format |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 862 | * @ts: pointer to timespec variable |
| 863 | * |
| 864 | * The function calculates the monotonic clock from the realtime |
| 865 | * clock and the wall_to_monotonic offset and stores the result |
John Stultz | 5322e4c | 2014-11-07 13:13:04 -0800 | [diff] [blame] | 866 | * in normalized timespec64 format in the variable pointed to by @ts. |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 867 | */ |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 868 | void ktime_get_ts64(struct timespec64 *ts) |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 869 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 870 | struct timekeeper *tk = &tk_core.timekeeper; |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 871 | struct timespec64 tomono; |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 872 | unsigned int seq; |
Thomas Gleixner | acc8961 | 2016-12-08 20:49:34 +0000 | [diff] [blame] | 873 | u64 nsec; |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 874 | |
| 875 | WARN_ON(timekeeping_suspended); |
| 876 | |
| 877 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 878 | seq = read_seqcount_begin(&tk_core.seq); |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 879 | ts->tv_sec = tk->xtime_sec; |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 880 | nsec = timekeeping_get_ns(&tk->tkr_mono); |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 881 | tomono = tk->wall_to_monotonic; |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 882 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 883 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 884 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 885 | ts->tv_sec += tomono.tv_sec; |
| 886 | ts->tv_nsec = 0; |
| 887 | timespec64_add_ns(ts, nsec + tomono.tv_nsec); |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 888 | } |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 889 | EXPORT_SYMBOL_GPL(ktime_get_ts64); |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 890 | |
Heena Sirwani | 9e3680b | 2014-10-29 16:01:16 +0530 | [diff] [blame] | 891 | /** |
| 892 | * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC |
| 893 | * |
| 894 | * Returns the seconds portion of CLOCK_MONOTONIC with a single non |
| 895 | * serialized read. tk->ktime_sec is of type 'unsigned long' so this |
| 896 | * works on both 32 and 64 bit systems. On 32 bit systems the readout |
| 897 | * covers ~136 years of uptime which should be enough to prevent |
| 898 | * premature wrap arounds. |
| 899 | */ |
| 900 | time64_t ktime_get_seconds(void) |
| 901 | { |
| 902 | struct timekeeper *tk = &tk_core.timekeeper; |
| 903 | |
| 904 | WARN_ON(timekeeping_suspended); |
| 905 | return tk->ktime_sec; |
| 906 | } |
| 907 | EXPORT_SYMBOL_GPL(ktime_get_seconds); |
| 908 | |
Heena Sirwani | dbe7aa6 | 2014-10-29 16:01:50 +0530 | [diff] [blame] | 909 | /** |
| 910 | * ktime_get_real_seconds - Get the seconds portion of CLOCK_REALTIME |
| 911 | * |
| 912 | * Returns the wall clock seconds since 1970. This replaces the |
| 913 | * get_seconds() interface which is not y2038 safe on 32bit systems. |
| 914 | * |
| 915 | * For 64bit systems the fast access to tk->xtime_sec is preserved. On |
| 916 | * 32bit systems the access must be protected with the sequence |
| 917 | * counter to provide "atomic" access to the 64bit tk->xtime_sec |
| 918 | * value. |
| 919 | */ |
| 920 | time64_t ktime_get_real_seconds(void) |
| 921 | { |
| 922 | struct timekeeper *tk = &tk_core.timekeeper; |
| 923 | time64_t seconds; |
| 924 | unsigned int seq; |
| 925 | |
| 926 | if (IS_ENABLED(CONFIG_64BIT)) |
| 927 | return tk->xtime_sec; |
| 928 | |
| 929 | do { |
| 930 | seq = read_seqcount_begin(&tk_core.seq); |
| 931 | seconds = tk->xtime_sec; |
| 932 | |
| 933 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
| 934 | |
| 935 | return seconds; |
| 936 | } |
| 937 | EXPORT_SYMBOL_GPL(ktime_get_real_seconds); |
| 938 | |
DengChao | dee3665 | 2015-12-13 12:24:18 +0800 | [diff] [blame] | 939 | /** |
| 940 | * __ktime_get_real_seconds - The same as ktime_get_real_seconds |
| 941 | * but without the sequence counter protect. This internal function |
| 942 | * is called just when timekeeping lock is already held. |
| 943 | */ |
| 944 | time64_t __ktime_get_real_seconds(void) |
| 945 | { |
| 946 | struct timekeeper *tk = &tk_core.timekeeper; |
| 947 | |
| 948 | return tk->xtime_sec; |
| 949 | } |
| 950 | |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 951 | /** |
| 952 | * ktime_get_snapshot - snapshots the realtime/monotonic raw clocks with counter |
| 953 | * @systime_snapshot: pointer to struct receiving the system time snapshot |
| 954 | */ |
| 955 | void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot) |
| 956 | { |
| 957 | struct timekeeper *tk = &tk_core.timekeeper; |
| 958 | unsigned long seq; |
| 959 | ktime_t base_raw; |
| 960 | ktime_t base_real; |
Thomas Gleixner | acc8961 | 2016-12-08 20:49:34 +0000 | [diff] [blame] | 961 | u64 nsec_raw; |
| 962 | u64 nsec_real; |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 963 | u64 now; |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 964 | |
Christopher S. Hall | ba26621 | 2016-02-22 03:15:21 -0800 | [diff] [blame] | 965 | WARN_ON_ONCE(timekeeping_suspended); |
| 966 | |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 967 | do { |
| 968 | seq = read_seqcount_begin(&tk_core.seq); |
John Stultz | ceea5e3 | 2017-06-08 16:44:20 -0700 | [diff] [blame] | 969 | now = tk_clock_read(&tk->tkr_mono); |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 970 | systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq; |
| 971 | systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq; |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 972 | base_real = ktime_add(tk->tkr_mono.base, |
| 973 | tk_core.timekeeper.offs_real); |
| 974 | base_raw = tk->tkr_raw.base; |
| 975 | nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, now); |
| 976 | nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw, now); |
| 977 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
| 978 | |
| 979 | systime_snapshot->cycles = now; |
| 980 | systime_snapshot->real = ktime_add_ns(base_real, nsec_real); |
| 981 | systime_snapshot->raw = ktime_add_ns(base_raw, nsec_raw); |
| 982 | } |
| 983 | EXPORT_SYMBOL_GPL(ktime_get_snapshot); |
DengChao | dee3665 | 2015-12-13 12:24:18 +0800 | [diff] [blame] | 984 | |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 985 | /* Scale base by mult/div checking for overflow */ |
| 986 | static int scale64_check_overflow(u64 mult, u64 div, u64 *base) |
| 987 | { |
| 988 | u64 tmp, rem; |
| 989 | |
| 990 | tmp = div64_u64_rem(*base, div, &rem); |
| 991 | |
| 992 | if (((int)sizeof(u64)*8 - fls64(mult) < fls64(tmp)) || |
| 993 | ((int)sizeof(u64)*8 - fls64(mult) < fls64(rem))) |
| 994 | return -EOVERFLOW; |
| 995 | tmp *= mult; |
| 996 | rem *= mult; |
| 997 | |
| 998 | do_div(rem, div); |
| 999 | *base = tmp + rem; |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | /** |
| 1004 | * adjust_historical_crosststamp - adjust crosstimestamp previous to current interval |
| 1005 | * @history: Snapshot representing start of history |
| 1006 | * @partial_history_cycles: Cycle offset into history (fractional part) |
| 1007 | * @total_history_cycles: Total history length in cycles |
| 1008 | * @discontinuity: True indicates clock was set on history period |
| 1009 | * @ts: Cross timestamp that should be adjusted using |
| 1010 | * partial/total ratio |
| 1011 | * |
| 1012 | * Helper function used by get_device_system_crosststamp() to correct the |
| 1013 | * crosstimestamp corresponding to the start of the current interval to the |
| 1014 | * system counter value (timestamp point) provided by the driver. The |
| 1015 | * total_history_* quantities are the total history starting at the provided |
| 1016 | * reference point and ending at the start of the current interval. The cycle |
| 1017 | * count between the driver timestamp point and the start of the current |
| 1018 | * interval is partial_history_cycles. |
| 1019 | */ |
| 1020 | static int adjust_historical_crosststamp(struct system_time_snapshot *history, |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 1021 | u64 partial_history_cycles, |
| 1022 | u64 total_history_cycles, |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 1023 | bool discontinuity, |
| 1024 | struct system_device_crosststamp *ts) |
| 1025 | { |
| 1026 | struct timekeeper *tk = &tk_core.timekeeper; |
| 1027 | u64 corr_raw, corr_real; |
| 1028 | bool interp_forward; |
| 1029 | int ret; |
| 1030 | |
| 1031 | if (total_history_cycles == 0 || partial_history_cycles == 0) |
| 1032 | return 0; |
| 1033 | |
| 1034 | /* Interpolate shortest distance from beginning or end of history */ |
Nicholas Mc Guire | 5fc63f9 | 2017-03-24 20:03:35 +0100 | [diff] [blame] | 1035 | interp_forward = partial_history_cycles > total_history_cycles / 2; |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 1036 | partial_history_cycles = interp_forward ? |
| 1037 | total_history_cycles - partial_history_cycles : |
| 1038 | partial_history_cycles; |
| 1039 | |
| 1040 | /* |
| 1041 | * Scale the monotonic raw time delta by: |
| 1042 | * partial_history_cycles / total_history_cycles |
| 1043 | */ |
| 1044 | corr_raw = (u64)ktime_to_ns( |
| 1045 | ktime_sub(ts->sys_monoraw, history->raw)); |
| 1046 | ret = scale64_check_overflow(partial_history_cycles, |
| 1047 | total_history_cycles, &corr_raw); |
| 1048 | if (ret) |
| 1049 | return ret; |
| 1050 | |
| 1051 | /* |
| 1052 | * If there is a discontinuity in the history, scale monotonic raw |
| 1053 | * correction by: |
| 1054 | * mult(real)/mult(raw) yielding the realtime correction |
| 1055 | * Otherwise, calculate the realtime correction similar to monotonic |
| 1056 | * raw calculation |
| 1057 | */ |
| 1058 | if (discontinuity) { |
| 1059 | corr_real = mul_u64_u32_div |
| 1060 | (corr_raw, tk->tkr_mono.mult, tk->tkr_raw.mult); |
| 1061 | } else { |
| 1062 | corr_real = (u64)ktime_to_ns( |
| 1063 | ktime_sub(ts->sys_realtime, history->real)); |
| 1064 | ret = scale64_check_overflow(partial_history_cycles, |
| 1065 | total_history_cycles, &corr_real); |
| 1066 | if (ret) |
| 1067 | return ret; |
| 1068 | } |
| 1069 | |
| 1070 | /* Fixup monotonic raw and real time time values */ |
| 1071 | if (interp_forward) { |
| 1072 | ts->sys_monoraw = ktime_add_ns(history->raw, corr_raw); |
| 1073 | ts->sys_realtime = ktime_add_ns(history->real, corr_real); |
| 1074 | } else { |
| 1075 | ts->sys_monoraw = ktime_sub_ns(ts->sys_monoraw, corr_raw); |
| 1076 | ts->sys_realtime = ktime_sub_ns(ts->sys_realtime, corr_real); |
| 1077 | } |
| 1078 | |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | /* |
| 1083 | * cycle_between - true if test occurs chronologically between before and after |
| 1084 | */ |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 1085 | static bool cycle_between(u64 before, u64 test, u64 after) |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 1086 | { |
| 1087 | if (test > before && test < after) |
| 1088 | return true; |
| 1089 | if (test < before && before > after) |
| 1090 | return true; |
| 1091 | return false; |
| 1092 | } |
| 1093 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1094 | /** |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 1095 | * get_device_system_crosststamp - Synchronously capture system/device timestamp |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 1096 | * @get_time_fn: Callback to get simultaneous device time and |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 1097 | * system counter from the device driver |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 1098 | * @ctx: Context passed to get_time_fn() |
| 1099 | * @history_begin: Historical reference point used to interpolate system |
| 1100 | * time when counter provided by the driver is before the current interval |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 1101 | * @xtstamp: Receives simultaneously captured system and device time |
| 1102 | * |
| 1103 | * Reads a timestamp from a device and correlates it to system time |
| 1104 | */ |
| 1105 | int get_device_system_crosststamp(int (*get_time_fn) |
| 1106 | (ktime_t *device_time, |
| 1107 | struct system_counterval_t *sys_counterval, |
| 1108 | void *ctx), |
| 1109 | void *ctx, |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 1110 | struct system_time_snapshot *history_begin, |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 1111 | struct system_device_crosststamp *xtstamp) |
| 1112 | { |
| 1113 | struct system_counterval_t system_counterval; |
| 1114 | struct timekeeper *tk = &tk_core.timekeeper; |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 1115 | u64 cycles, now, interval_start; |
Ingo Molnar | 6436257 | 2016-03-08 11:09:53 +0100 | [diff] [blame] | 1116 | unsigned int clock_was_set_seq = 0; |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 1117 | ktime_t base_real, base_raw; |
Thomas Gleixner | acc8961 | 2016-12-08 20:49:34 +0000 | [diff] [blame] | 1118 | u64 nsec_real, nsec_raw; |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 1119 | u8 cs_was_changed_seq; |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 1120 | unsigned long seq; |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 1121 | bool do_interp; |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 1122 | int ret; |
| 1123 | |
| 1124 | do { |
| 1125 | seq = read_seqcount_begin(&tk_core.seq); |
| 1126 | /* |
| 1127 | * Try to synchronously capture device time and a system |
| 1128 | * counter value calling back into the device driver |
| 1129 | */ |
| 1130 | ret = get_time_fn(&xtstamp->device, &system_counterval, ctx); |
| 1131 | if (ret) |
| 1132 | return ret; |
| 1133 | |
| 1134 | /* |
| 1135 | * Verify that the clocksource associated with the captured |
| 1136 | * system counter value is the same as the currently installed |
| 1137 | * timekeeper clocksource |
| 1138 | */ |
| 1139 | if (tk->tkr_mono.clock != system_counterval.cs) |
| 1140 | return -ENODEV; |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 1141 | cycles = system_counterval.cycles; |
| 1142 | |
| 1143 | /* |
| 1144 | * Check whether the system counter value provided by the |
| 1145 | * device driver is on the current timekeeping interval. |
| 1146 | */ |
John Stultz | ceea5e3 | 2017-06-08 16:44:20 -0700 | [diff] [blame] | 1147 | now = tk_clock_read(&tk->tkr_mono); |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 1148 | interval_start = tk->tkr_mono.cycle_last; |
| 1149 | if (!cycle_between(interval_start, cycles, now)) { |
| 1150 | clock_was_set_seq = tk->clock_was_set_seq; |
| 1151 | cs_was_changed_seq = tk->cs_was_changed_seq; |
| 1152 | cycles = interval_start; |
| 1153 | do_interp = true; |
| 1154 | } else { |
| 1155 | do_interp = false; |
| 1156 | } |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 1157 | |
| 1158 | base_real = ktime_add(tk->tkr_mono.base, |
| 1159 | tk_core.timekeeper.offs_real); |
| 1160 | base_raw = tk->tkr_raw.base; |
| 1161 | |
| 1162 | nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, |
| 1163 | system_counterval.cycles); |
| 1164 | nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw, |
| 1165 | system_counterval.cycles); |
| 1166 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
| 1167 | |
| 1168 | xtstamp->sys_realtime = ktime_add_ns(base_real, nsec_real); |
| 1169 | xtstamp->sys_monoraw = ktime_add_ns(base_raw, nsec_raw); |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 1170 | |
| 1171 | /* |
| 1172 | * Interpolate if necessary, adjusting back from the start of the |
| 1173 | * current interval |
| 1174 | */ |
| 1175 | if (do_interp) { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 1176 | u64 partial_history_cycles, total_history_cycles; |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 1177 | bool discontinuity; |
| 1178 | |
| 1179 | /* |
| 1180 | * Check that the counter value occurs after the provided |
| 1181 | * history reference and that the history doesn't cross a |
| 1182 | * clocksource change |
| 1183 | */ |
| 1184 | if (!history_begin || |
| 1185 | !cycle_between(history_begin->cycles, |
| 1186 | system_counterval.cycles, cycles) || |
| 1187 | history_begin->cs_was_changed_seq != cs_was_changed_seq) |
| 1188 | return -EINVAL; |
| 1189 | partial_history_cycles = cycles - system_counterval.cycles; |
| 1190 | total_history_cycles = cycles - history_begin->cycles; |
| 1191 | discontinuity = |
| 1192 | history_begin->clock_was_set_seq != clock_was_set_seq; |
| 1193 | |
| 1194 | ret = adjust_historical_crosststamp(history_begin, |
| 1195 | partial_history_cycles, |
| 1196 | total_history_cycles, |
| 1197 | discontinuity, xtstamp); |
| 1198 | if (ret) |
| 1199 | return ret; |
| 1200 | } |
| 1201 | |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 1202 | return 0; |
| 1203 | } |
| 1204 | EXPORT_SYMBOL_GPL(get_device_system_crosststamp); |
| 1205 | |
| 1206 | /** |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1207 | * do_gettimeofday - Returns the time of day in a timeval |
| 1208 | * @tv: pointer to the timeval to be set |
| 1209 | * |
Geert Uytterhoeven | efd9ac8 | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 1210 | * NOTE: Users should be converted to using getnstimeofday() |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1211 | */ |
| 1212 | void do_gettimeofday(struct timeval *tv) |
| 1213 | { |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 1214 | struct timespec64 now; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1215 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 1216 | getnstimeofday64(&now); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1217 | tv->tv_sec = now.tv_sec; |
| 1218 | tv->tv_usec = now.tv_nsec/1000; |
| 1219 | } |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1220 | EXPORT_SYMBOL(do_gettimeofday); |
Richard Cochran | d239f49 | 2012-04-27 10:12:42 +0200 | [diff] [blame] | 1221 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1222 | /** |
pang.xunlei | 21f7eca | 2014-11-18 19:15:16 +0800 | [diff] [blame] | 1223 | * do_settimeofday64 - Sets the time of day. |
| 1224 | * @ts: pointer to the timespec64 variable containing the new time |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1225 | * |
| 1226 | * Sets the time of day to the new time and update NTP and notify hrtimers |
| 1227 | */ |
pang.xunlei | 21f7eca | 2014-11-18 19:15:16 +0800 | [diff] [blame] | 1228 | int do_settimeofday64(const struct timespec64 *ts) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1229 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1230 | struct timekeeper *tk = &tk_core.timekeeper; |
pang.xunlei | 21f7eca | 2014-11-18 19:15:16 +0800 | [diff] [blame] | 1231 | struct timespec64 ts_delta, xt; |
John Stultz | 92c1d3e | 2011-11-14 14:05:44 -0800 | [diff] [blame] | 1232 | unsigned long flags; |
Wang YanQing | e1d7ba8 | 2015-06-23 18:38:54 +0800 | [diff] [blame] | 1233 | int ret = 0; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1234 | |
pang.xunlei | 21f7eca | 2014-11-18 19:15:16 +0800 | [diff] [blame] | 1235 | if (!timespec64_valid_strict(ts)) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1236 | return -EINVAL; |
| 1237 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1238 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1239 | write_seqcount_begin(&tk_core.seq); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1240 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1241 | timekeeping_forward_now(tk); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1242 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1243 | xt = tk_xtime(tk); |
pang.xunlei | 21f7eca | 2014-11-18 19:15:16 +0800 | [diff] [blame] | 1244 | ts_delta.tv_sec = ts->tv_sec - xt.tv_sec; |
| 1245 | ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec; |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 1246 | |
Wang YanQing | e1d7ba8 | 2015-06-23 18:38:54 +0800 | [diff] [blame] | 1247 | if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) { |
| 1248 | ret = -EINVAL; |
| 1249 | goto out; |
| 1250 | } |
| 1251 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1252 | tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta)); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1253 | |
pang.xunlei | 21f7eca | 2014-11-18 19:15:16 +0800 | [diff] [blame] | 1254 | tk_set_xtime(tk, ts); |
Wang YanQing | e1d7ba8 | 2015-06-23 18:38:54 +0800 | [diff] [blame] | 1255 | out: |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 1256 | timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1257 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1258 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1259 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1260 | |
| 1261 | /* signal hrtimers about time change */ |
| 1262 | clock_was_set(); |
| 1263 | |
Wang YanQing | e1d7ba8 | 2015-06-23 18:38:54 +0800 | [diff] [blame] | 1264 | return ret; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1265 | } |
pang.xunlei | 21f7eca | 2014-11-18 19:15:16 +0800 | [diff] [blame] | 1266 | EXPORT_SYMBOL(do_settimeofday64); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1267 | |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 1268 | /** |
| 1269 | * timekeeping_inject_offset - Adds or subtracts from the current time. |
| 1270 | * @tv: pointer to the timespec variable containing the offset |
| 1271 | * |
| 1272 | * Adds or subtracts an offset value from the current time. |
| 1273 | */ |
| 1274 | int timekeeping_inject_offset(struct timespec *ts) |
| 1275 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1276 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 92c1d3e | 2011-11-14 14:05:44 -0800 | [diff] [blame] | 1277 | unsigned long flags; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1278 | struct timespec64 ts64, tmp; |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 1279 | int ret = 0; |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 1280 | |
John Stultz | 37cf4dc | 2015-12-03 22:09:31 -0500 | [diff] [blame] | 1281 | if (!timespec_inject_offset_valid(ts)) |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 1282 | return -EINVAL; |
| 1283 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1284 | ts64 = timespec_to_timespec64(*ts); |
| 1285 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1286 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1287 | write_seqcount_begin(&tk_core.seq); |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 1288 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1289 | timekeeping_forward_now(tk); |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 1290 | |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 1291 | /* Make sure the proposed value is valid */ |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1292 | tmp = timespec64_add(tk_xtime(tk), ts64); |
Wang YanQing | e1d7ba8 | 2015-06-23 18:38:54 +0800 | [diff] [blame] | 1293 | if (timespec64_compare(&tk->wall_to_monotonic, &ts64) > 0 || |
| 1294 | !timespec64_valid_strict(&tmp)) { |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 1295 | ret = -EINVAL; |
| 1296 | goto error; |
| 1297 | } |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 1298 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1299 | tk_xtime_add(tk, &ts64); |
| 1300 | tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts64)); |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 1301 | |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 1302 | error: /* even if we error out, we forwarded the time, so call update */ |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 1303 | timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 1304 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1305 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1306 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 1307 | |
| 1308 | /* signal hrtimers about time change */ |
| 1309 | clock_was_set(); |
| 1310 | |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 1311 | return ret; |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 1312 | } |
| 1313 | EXPORT_SYMBOL(timekeeping_inject_offset); |
| 1314 | |
John Stultz | cc244dd | 2012-05-03 12:30:07 -0700 | [diff] [blame] | 1315 | /** |
Stephen Boyd | 40d9f82 | 2016-12-07 14:33:23 -0800 | [diff] [blame] | 1316 | * __timekeeping_set_tai_offset - Sets the TAI offset from UTC and monotonic |
John Stultz | cc244dd | 2012-05-03 12:30:07 -0700 | [diff] [blame] | 1317 | * |
| 1318 | */ |
Fengguang Wu | dd5d70e8 | 2013-03-25 12:24:24 -0700 | [diff] [blame] | 1319 | static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset) |
John Stultz | cc244dd | 2012-05-03 12:30:07 -0700 | [diff] [blame] | 1320 | { |
| 1321 | tk->tai_offset = tai_offset; |
John Stultz | 04005f6 | 2013-12-10 17:13:35 -0800 | [diff] [blame] | 1322 | tk->offs_tai = ktime_add(tk->offs_real, ktime_set(tai_offset, 0)); |
John Stultz | cc244dd | 2012-05-03 12:30:07 -0700 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | /** |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1326 | * change_clocksource - Swaps clocksources if a new one is available |
| 1327 | * |
| 1328 | * Accumulates current time interval and initializes new clocksource |
| 1329 | */ |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 1330 | static int change_clocksource(void *data) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1331 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1332 | struct timekeeper *tk = &tk_core.timekeeper; |
Magnus Damm | 4614e6a | 2009-04-21 12:24:02 -0700 | [diff] [blame] | 1333 | struct clocksource *new, *old; |
John Stultz | f695cf9 | 2012-03-14 16:38:15 -0700 | [diff] [blame] | 1334 | unsigned long flags; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1335 | |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 1336 | new = (struct clocksource *) data; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1337 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1338 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1339 | write_seqcount_begin(&tk_core.seq); |
John Stultz | f695cf9 | 2012-03-14 16:38:15 -0700 | [diff] [blame] | 1340 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1341 | timekeeping_forward_now(tk); |
Thomas Gleixner | 09ac369 | 2013-04-25 20:31:44 +0000 | [diff] [blame] | 1342 | /* |
| 1343 | * If the cs is in module, get a module reference. Succeeds |
| 1344 | * for built-in code (owner == NULL) as well. |
| 1345 | */ |
| 1346 | if (try_module_get(new->owner)) { |
| 1347 | if (!new->enable || new->enable(new) == 0) { |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1348 | old = tk->tkr_mono.clock; |
Thomas Gleixner | 09ac369 | 2013-04-25 20:31:44 +0000 | [diff] [blame] | 1349 | tk_setup_internals(tk, new); |
| 1350 | if (old->disable) |
| 1351 | old->disable(old); |
| 1352 | module_put(old->owner); |
| 1353 | } else { |
| 1354 | module_put(new->owner); |
| 1355 | } |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 1356 | } |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 1357 | timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
John Stultz | f695cf9 | 2012-03-14 16:38:15 -0700 | [diff] [blame] | 1358 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1359 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1360 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
John Stultz | f695cf9 | 2012-03-14 16:38:15 -0700 | [diff] [blame] | 1361 | |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 1362 | return 0; |
| 1363 | } |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1364 | |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 1365 | /** |
| 1366 | * timekeeping_notify - Install a new clock source |
| 1367 | * @clock: pointer to the clock source |
| 1368 | * |
| 1369 | * This function is called from clocksource.c after a new, better clock |
| 1370 | * source has been registered. The caller holds the clocksource_mutex. |
| 1371 | */ |
Thomas Gleixner | ba919d1 | 2013-04-25 20:31:44 +0000 | [diff] [blame] | 1372 | int timekeeping_notify(struct clocksource *clock) |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 1373 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1374 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1375 | |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1376 | if (tk->tkr_mono.clock == clock) |
Thomas Gleixner | ba919d1 | 2013-04-25 20:31:44 +0000 | [diff] [blame] | 1377 | return 0; |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 1378 | stop_machine(change_clocksource, clock, NULL); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1379 | tick_clock_notify(); |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1380 | return tk->tkr_mono.clock == clock ? 0 : -1; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1381 | } |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 1382 | |
Thomas Gleixner | a40f262 | 2009-07-07 13:00:31 +0200 | [diff] [blame] | 1383 | /** |
John Stultz | cdba2ec | 2014-11-07 11:03:20 -0800 | [diff] [blame] | 1384 | * getrawmonotonic64 - Returns the raw monotonic time in a timespec |
| 1385 | * @ts: pointer to the timespec64 to be set |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 1386 | * |
| 1387 | * Returns the raw monotonic time (completely un-modified by ntp) |
| 1388 | */ |
John Stultz | cdba2ec | 2014-11-07 11:03:20 -0800 | [diff] [blame] | 1389 | void getrawmonotonic64(struct timespec64 *ts) |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 1390 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1391 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 1392 | unsigned long seq; |
Thomas Gleixner | acc8961 | 2016-12-08 20:49:34 +0000 | [diff] [blame] | 1393 | u64 nsecs; |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 1394 | |
| 1395 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1396 | seq = read_seqcount_begin(&tk_core.seq); |
John Stultz | fc6eead7 | 2017-05-22 17:20:20 -0700 | [diff] [blame] | 1397 | ts->tv_sec = tk->raw_sec; |
Peter Zijlstra | 4a4ad80 | 2015-03-19 09:28:44 +0100 | [diff] [blame] | 1398 | nsecs = timekeeping_get_ns(&tk->tkr_raw); |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 1399 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1400 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 1401 | |
John Stultz | fc6eead7 | 2017-05-22 17:20:20 -0700 | [diff] [blame] | 1402 | ts->tv_nsec = 0; |
| 1403 | timespec64_add_ns(ts, nsecs); |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 1404 | } |
John Stultz | cdba2ec | 2014-11-07 11:03:20 -0800 | [diff] [blame] | 1405 | EXPORT_SYMBOL(getrawmonotonic64); |
| 1406 | |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 1407 | |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 1408 | /** |
Li Zefan | cf4fc6c | 2008-02-08 04:19:24 -0800 | [diff] [blame] | 1409 | * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1410 | */ |
Li Zefan | cf4fc6c | 2008-02-08 04:19:24 -0800 | [diff] [blame] | 1411 | int timekeeping_valid_for_hres(void) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1412 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1413 | struct timekeeper *tk = &tk_core.timekeeper; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1414 | unsigned long seq; |
| 1415 | int ret; |
| 1416 | |
| 1417 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1418 | seq = read_seqcount_begin(&tk_core.seq); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1419 | |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1420 | ret = tk->tkr_mono.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1421 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1422 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1423 | |
| 1424 | return ret; |
| 1425 | } |
| 1426 | |
| 1427 | /** |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 1428 | * timekeeping_max_deferment - Returns max time the clocksource can be deferred |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 1429 | */ |
| 1430 | u64 timekeeping_max_deferment(void) |
| 1431 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1432 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 1433 | unsigned long seq; |
| 1434 | u64 ret; |
John Stultz | 42e71e8 | 2012-07-13 01:21:51 -0400 | [diff] [blame] | 1435 | |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 1436 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1437 | seq = read_seqcount_begin(&tk_core.seq); |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 1438 | |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1439 | ret = tk->tkr_mono.clock->max_idle_ns; |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 1440 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1441 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 1442 | |
| 1443 | return ret; |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | /** |
Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 1447 | * read_persistent_clock - Return time from the persistent clock. |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1448 | * |
| 1449 | * Weak dummy function for arches that do not yet support it. |
Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 1450 | * Reads the time from the battery backed persistent clock. |
| 1451 | * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported. |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1452 | * |
| 1453 | * XXX - Do be sure to remove it once all arches implement it. |
| 1454 | */ |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 1455 | void __weak read_persistent_clock(struct timespec *ts) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1456 | { |
Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 1457 | ts->tv_sec = 0; |
| 1458 | ts->tv_nsec = 0; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
Xunlei Pang | 2ee9663 | 2015-04-01 20:34:22 -0700 | [diff] [blame] | 1461 | void __weak read_persistent_clock64(struct timespec64 *ts64) |
| 1462 | { |
| 1463 | struct timespec ts; |
| 1464 | |
| 1465 | read_persistent_clock(&ts); |
| 1466 | *ts64 = timespec_to_timespec64(ts); |
| 1467 | } |
| 1468 | |
Martin Schwidefsky | 23970e3 | 2009-08-14 15:47:32 +0200 | [diff] [blame] | 1469 | /** |
Xunlei Pang | e83d0a4 | 2015-04-09 09:04:42 +0800 | [diff] [blame] | 1470 | * read_boot_clock64 - Return time of the system start. |
Martin Schwidefsky | 23970e3 | 2009-08-14 15:47:32 +0200 | [diff] [blame] | 1471 | * |
| 1472 | * Weak dummy function for arches that do not yet support it. |
| 1473 | * Function to read the exact time the system has been started. |
Xunlei Pang | e83d0a4 | 2015-04-09 09:04:42 +0800 | [diff] [blame] | 1474 | * Returns a timespec64 with tv_sec=0 and tv_nsec=0 if unsupported. |
Martin Schwidefsky | 23970e3 | 2009-08-14 15:47:32 +0200 | [diff] [blame] | 1475 | * |
| 1476 | * XXX - Do be sure to remove it once all arches implement it. |
| 1477 | */ |
Xunlei Pang | e83d0a4 | 2015-04-09 09:04:42 +0800 | [diff] [blame] | 1478 | void __weak read_boot_clock64(struct timespec64 *ts) |
Martin Schwidefsky | 23970e3 | 2009-08-14 15:47:32 +0200 | [diff] [blame] | 1479 | { |
| 1480 | ts->tv_sec = 0; |
| 1481 | ts->tv_nsec = 0; |
| 1482 | } |
| 1483 | |
Xunlei Pang | 0fa88cb | 2015-04-01 20:34:38 -0700 | [diff] [blame] | 1484 | /* Flag for if timekeeping_resume() has injected sleeptime */ |
| 1485 | static bool sleeptime_injected; |
| 1486 | |
| 1487 | /* Flag for if there is a persistent clock on this platform */ |
| 1488 | static bool persistent_clock_exists; |
| 1489 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1490 | /* |
| 1491 | * timekeeping_init - Initializes the clocksource and common timekeeping values |
| 1492 | */ |
| 1493 | void __init timekeeping_init(void) |
| 1494 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1495 | struct timekeeper *tk = &tk_core.timekeeper; |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 1496 | struct clocksource *clock; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1497 | unsigned long flags; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1498 | struct timespec64 now, boot, tmp; |
Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 1499 | |
Xunlei Pang | 2ee9663 | 2015-04-01 20:34:22 -0700 | [diff] [blame] | 1500 | read_persistent_clock64(&now); |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1501 | if (!timespec64_valid_strict(&now)) { |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 1502 | pr_warn("WARNING: Persistent clock returned invalid value!\n" |
| 1503 | " Check your CMOS/BIOS settings.\n"); |
| 1504 | now.tv_sec = 0; |
| 1505 | now.tv_nsec = 0; |
Feng Tang | 31ade30 | 2013-01-16 00:09:47 +0800 | [diff] [blame] | 1506 | } else if (now.tv_sec || now.tv_nsec) |
Xunlei Pang | 0fa88cb | 2015-04-01 20:34:38 -0700 | [diff] [blame] | 1507 | persistent_clock_exists = true; |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 1508 | |
Xunlei Pang | 9a806dd | 2015-04-01 20:34:21 -0700 | [diff] [blame] | 1509 | read_boot_clock64(&boot); |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1510 | if (!timespec64_valid_strict(&boot)) { |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 1511 | pr_warn("WARNING: Boot clock returned invalid value!\n" |
| 1512 | " Check your CMOS/BIOS settings.\n"); |
| 1513 | boot.tv_sec = 0; |
| 1514 | boot.tv_nsec = 0; |
| 1515 | } |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1516 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1517 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1518 | write_seqcount_begin(&tk_core.seq); |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 1519 | ntp_init(); |
| 1520 | |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 1521 | clock = clocksource_default_clock(); |
Martin Schwidefsky | a0f7d48 | 2009-08-14 15:47:19 +0200 | [diff] [blame] | 1522 | if (clock->enable) |
| 1523 | clock->enable(clock); |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1524 | tk_setup_internals(tk, clock); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1525 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1526 | tk_set_xtime(tk, &now); |
John Stultz | fc6eead7 | 2017-05-22 17:20:20 -0700 | [diff] [blame] | 1527 | tk->raw_sec = 0; |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 1528 | if (boot.tv_sec == 0 && boot.tv_nsec == 0) |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1529 | boot = tk_xtime(tk); |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 1530 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1531 | set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec); |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1532 | tk_set_wall_to_mono(tk, tmp); |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 1533 | |
Thomas Gleixner | 56fd16c | 2015-10-16 15:50:22 +0200 | [diff] [blame] | 1534 | timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 1535 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1536 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1537 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
Xunlei Pang | 264bb3f | 2015-04-01 20:34:37 -0700 | [diff] [blame] | 1540 | /* time in seconds when suspend began for persistent clock */ |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1541 | static struct timespec64 timekeeping_suspend_time; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1542 | |
| 1543 | /** |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1544 | * __timekeeping_inject_sleeptime - Internal function to add sleep interval |
| 1545 | * @delta: pointer to a timespec delta value |
| 1546 | * |
| 1547 | * Takes a timespec offset measuring a suspend interval and properly |
| 1548 | * adds the sleep offset to the timekeeping variables. |
| 1549 | */ |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1550 | static void __timekeeping_inject_sleeptime(struct timekeeper *tk, |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1551 | struct timespec64 *delta) |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1552 | { |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1553 | if (!timespec64_valid_strict(delta)) { |
John Stultz | 6d9bcb6 | 2014-06-04 16:11:43 -0700 | [diff] [blame] | 1554 | printk_deferred(KERN_WARNING |
| 1555 | "__timekeeping_inject_sleeptime: Invalid " |
| 1556 | "sleep delta value!\n"); |
John Stultz | cb5de2f8d | 2011-06-01 18:18:09 -0700 | [diff] [blame] | 1557 | return; |
| 1558 | } |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1559 | tk_xtime_add(tk, delta); |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1560 | tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta)); |
Thomas Gleixner | 47da70d | 2014-07-16 21:05:00 +0000 | [diff] [blame] | 1561 | tk_update_sleep_time(tk, timespec64_to_ktime(*delta)); |
Colin Cross | 5c83545 | 2013-05-21 22:32:14 -0700 | [diff] [blame] | 1562 | tk_debug_account_sleep_time(delta); |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
Xunlei Pang | 7f29813 | 2015-04-01 20:34:35 -0700 | [diff] [blame] | 1565 | #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE) |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1566 | /** |
Xunlei Pang | 0fa88cb | 2015-04-01 20:34:38 -0700 | [diff] [blame] | 1567 | * We have three kinds of time sources to use for sleep time |
| 1568 | * injection, the preference order is: |
| 1569 | * 1) non-stop clocksource |
| 1570 | * 2) persistent clock (ie: RTC accessible when irqs are off) |
| 1571 | * 3) RTC |
| 1572 | * |
| 1573 | * 1) and 2) are used by timekeeping, 3) by RTC subsystem. |
| 1574 | * If system has neither 1) nor 2), 3) will be used finally. |
| 1575 | * |
| 1576 | * |
| 1577 | * If timekeeping has injected sleeptime via either 1) or 2), |
| 1578 | * 3) becomes needless, so in this case we don't need to call |
| 1579 | * rtc_resume(), and this is what timekeeping_rtc_skipresume() |
| 1580 | * means. |
| 1581 | */ |
| 1582 | bool timekeeping_rtc_skipresume(void) |
| 1583 | { |
| 1584 | return sleeptime_injected; |
| 1585 | } |
| 1586 | |
| 1587 | /** |
| 1588 | * 1) can be determined whether to use or not only when doing |
| 1589 | * timekeeping_resume() which is invoked after rtc_suspend(), |
| 1590 | * so we can't skip rtc_suspend() surely if system has 1). |
| 1591 | * |
| 1592 | * But if system has 2), 2) will definitely be used, so in this |
| 1593 | * case we don't need to call rtc_suspend(), and this is what |
| 1594 | * timekeeping_rtc_skipsuspend() means. |
| 1595 | */ |
| 1596 | bool timekeeping_rtc_skipsuspend(void) |
| 1597 | { |
| 1598 | return persistent_clock_exists; |
| 1599 | } |
| 1600 | |
| 1601 | /** |
pang.xunlei | 04d9089 | 2014-11-18 19:15:17 +0800 | [diff] [blame] | 1602 | * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values |
| 1603 | * @delta: pointer to a timespec64 delta value |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1604 | * |
Xunlei Pang | 2ee9663 | 2015-04-01 20:34:22 -0700 | [diff] [blame] | 1605 | * This hook is for architectures that cannot support read_persistent_clock64 |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1606 | * because their RTC/persistent clock is only accessible when irqs are enabled. |
Xunlei Pang | 0fa88cb | 2015-04-01 20:34:38 -0700 | [diff] [blame] | 1607 | * and also don't have an effective nonstop clocksource. |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1608 | * |
| 1609 | * This function should only be called by rtc_resume(), and allows |
| 1610 | * a suspend offset to be injected into the timekeeping values. |
| 1611 | */ |
pang.xunlei | 04d9089 | 2014-11-18 19:15:17 +0800 | [diff] [blame] | 1612 | void timekeeping_inject_sleeptime64(struct timespec64 *delta) |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1613 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1614 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 92c1d3e | 2011-11-14 14:05:44 -0800 | [diff] [blame] | 1615 | unsigned long flags; |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1616 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1617 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1618 | write_seqcount_begin(&tk_core.seq); |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 1619 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1620 | timekeeping_forward_now(tk); |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1621 | |
pang.xunlei | 04d9089 | 2014-11-18 19:15:17 +0800 | [diff] [blame] | 1622 | __timekeeping_inject_sleeptime(tk, delta); |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1623 | |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 1624 | timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1625 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1626 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1627 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1628 | |
| 1629 | /* signal hrtimers about time change */ |
| 1630 | clock_was_set(); |
| 1631 | } |
Xunlei Pang | 7f29813 | 2015-04-01 20:34:35 -0700 | [diff] [blame] | 1632 | #endif |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1633 | |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1634 | /** |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1635 | * timekeeping_resume - Resumes the generic timekeeping subsystem. |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1636 | */ |
Rafael J. Wysocki | 124cf911 | 2015-02-13 23:50:43 +0100 | [diff] [blame] | 1637 | void timekeeping_resume(void) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1638 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1639 | struct timekeeper *tk = &tk_core.timekeeper; |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1640 | struct clocksource *clock = tk->tkr_mono.clock; |
John Stultz | 92c1d3e | 2011-11-14 14:05:44 -0800 | [diff] [blame] | 1641 | unsigned long flags; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1642 | struct timespec64 ts_new, ts_delta; |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 1643 | u64 cycle_now; |
Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 1644 | |
Xunlei Pang | 0fa88cb | 2015-04-01 20:34:38 -0700 | [diff] [blame] | 1645 | sleeptime_injected = false; |
Xunlei Pang | 2ee9663 | 2015-04-01 20:34:22 -0700 | [diff] [blame] | 1646 | read_persistent_clock64(&ts_new); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1647 | |
Rafael J. Wysocki | adc78e6 | 2012-08-06 01:40:41 +0200 | [diff] [blame] | 1648 | clockevents_resume(); |
Thomas Gleixner | d10ff3f | 2007-05-14 11:10:02 +0200 | [diff] [blame] | 1649 | clocksource_resume(); |
| 1650 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1651 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1652 | write_seqcount_begin(&tk_core.seq); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1653 | |
Feng Tang | e445cf1 | 2013-03-12 11:56:48 +0800 | [diff] [blame] | 1654 | /* |
| 1655 | * After system resumes, we need to calculate the suspended time and |
| 1656 | * compensate it for the OS time. There are 3 sources that could be |
| 1657 | * used: Nonstop clocksource during suspend, persistent clock and rtc |
| 1658 | * device. |
| 1659 | * |
| 1660 | * One specific platform may have 1 or 2 or all of them, and the |
| 1661 | * preference will be: |
| 1662 | * suspend-nonstop clocksource -> persistent clock -> rtc |
| 1663 | * The less preferred source will only be tried if there is no better |
| 1664 | * usable source. The rtc part is handled separately in rtc core code. |
| 1665 | */ |
John Stultz | ceea5e3 | 2017-06-08 16:44:20 -0700 | [diff] [blame] | 1666 | cycle_now = tk_clock_read(&tk->tkr_mono); |
Feng Tang | e445cf1 | 2013-03-12 11:56:48 +0800 | [diff] [blame] | 1667 | if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) && |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1668 | cycle_now > tk->tkr_mono.cycle_last) { |
Thomas Gleixner | c029a2b | 2016-12-08 20:49:38 +0000 | [diff] [blame] | 1669 | u64 nsec, cyc_delta; |
Feng Tang | e445cf1 | 2013-03-12 11:56:48 +0800 | [diff] [blame] | 1670 | |
Thomas Gleixner | c029a2b | 2016-12-08 20:49:38 +0000 | [diff] [blame] | 1671 | cyc_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, |
| 1672 | tk->tkr_mono.mask); |
| 1673 | nsec = mul_u64_u32_shr(cyc_delta, clock->mult, clock->shift); |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1674 | ts_delta = ns_to_timespec64(nsec); |
Xunlei Pang | 0fa88cb | 2015-04-01 20:34:38 -0700 | [diff] [blame] | 1675 | sleeptime_injected = true; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1676 | } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) { |
| 1677 | ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time); |
Xunlei Pang | 0fa88cb | 2015-04-01 20:34:38 -0700 | [diff] [blame] | 1678 | sleeptime_injected = true; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1679 | } |
Feng Tang | e445cf1 | 2013-03-12 11:56:48 +0800 | [diff] [blame] | 1680 | |
Xunlei Pang | 0fa88cb | 2015-04-01 20:34:38 -0700 | [diff] [blame] | 1681 | if (sleeptime_injected) |
Feng Tang | e445cf1 | 2013-03-12 11:56:48 +0800 | [diff] [blame] | 1682 | __timekeeping_inject_sleeptime(tk, &ts_delta); |
| 1683 | |
| 1684 | /* Re-base the last cycle value */ |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1685 | tk->tkr_mono.cycle_last = cycle_now; |
Peter Zijlstra | 4a4ad80 | 2015-03-19 09:28:44 +0100 | [diff] [blame] | 1686 | tk->tkr_raw.cycle_last = cycle_now; |
| 1687 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1688 | tk->ntp_error = 0; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1689 | timekeeping_suspended = 0; |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 1690 | timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1691 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1692 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1693 | |
| 1694 | touch_softlockup_watchdog(); |
| 1695 | |
Thomas Gleixner | 4ffee52 | 2015-03-25 13:09:16 +0100 | [diff] [blame] | 1696 | tick_resume(); |
Thomas Gleixner | b12a03c | 2011-05-02 16:48:57 +0200 | [diff] [blame] | 1697 | hrtimers_resume(); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
Rafael J. Wysocki | 124cf911 | 2015-02-13 23:50:43 +0100 | [diff] [blame] | 1700 | int timekeeping_suspend(void) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1701 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1702 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 92c1d3e | 2011-11-14 14:05:44 -0800 | [diff] [blame] | 1703 | unsigned long flags; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1704 | struct timespec64 delta, delta_delta; |
| 1705 | static struct timespec64 old_delta; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1706 | |
Xunlei Pang | 2ee9663 | 2015-04-01 20:34:22 -0700 | [diff] [blame] | 1707 | read_persistent_clock64(&timekeeping_suspend_time); |
Thomas Gleixner | 3be9095 | 2007-09-16 15:36:43 +0200 | [diff] [blame] | 1708 | |
Zoran Markovic | 0d6bd99 | 2013-05-17 11:24:05 -0700 | [diff] [blame] | 1709 | /* |
| 1710 | * On some systems the persistent_clock can not be detected at |
| 1711 | * timekeeping_init by its return value, so if we see a valid |
| 1712 | * value returned, update the persistent_clock_exists flag. |
| 1713 | */ |
| 1714 | if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec) |
Xunlei Pang | 0fa88cb | 2015-04-01 20:34:38 -0700 | [diff] [blame] | 1715 | persistent_clock_exists = true; |
Zoran Markovic | 0d6bd99 | 2013-05-17 11:24:05 -0700 | [diff] [blame] | 1716 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1717 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1718 | write_seqcount_begin(&tk_core.seq); |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1719 | timekeeping_forward_now(tk); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1720 | timekeeping_suspended = 1; |
John Stultz | cb33217 | 2011-05-31 22:53:23 -0700 | [diff] [blame] | 1721 | |
Xunlei Pang | 0fa88cb | 2015-04-01 20:34:38 -0700 | [diff] [blame] | 1722 | if (persistent_clock_exists) { |
John Stultz | cb33217 | 2011-05-31 22:53:23 -0700 | [diff] [blame] | 1723 | /* |
Xunlei Pang | 264bb3f | 2015-04-01 20:34:37 -0700 | [diff] [blame] | 1724 | * To avoid drift caused by repeated suspend/resumes, |
| 1725 | * which each can add ~1 second drift error, |
| 1726 | * try to compensate so the difference in system time |
| 1727 | * and persistent_clock time stays close to constant. |
John Stultz | cb33217 | 2011-05-31 22:53:23 -0700 | [diff] [blame] | 1728 | */ |
Xunlei Pang | 264bb3f | 2015-04-01 20:34:37 -0700 | [diff] [blame] | 1729 | delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time); |
| 1730 | delta_delta = timespec64_sub(delta, old_delta); |
| 1731 | if (abs(delta_delta.tv_sec) >= 2) { |
| 1732 | /* |
| 1733 | * if delta_delta is too large, assume time correction |
| 1734 | * has occurred and set old_delta to the current delta. |
| 1735 | */ |
| 1736 | old_delta = delta; |
| 1737 | } else { |
| 1738 | /* Otherwise try to adjust old_system to compensate */ |
| 1739 | timekeeping_suspend_time = |
| 1740 | timespec64_add(timekeeping_suspend_time, delta_delta); |
| 1741 | } |
John Stultz | cb33217 | 2011-05-31 22:53:23 -0700 | [diff] [blame] | 1742 | } |
John Stultz | 330a161 | 2013-12-11 19:10:36 -0800 | [diff] [blame] | 1743 | |
| 1744 | timekeeping_update(tk, TK_MIRROR); |
Rafael J. Wysocki | 060407a | 2015-02-13 14:49:02 +0100 | [diff] [blame] | 1745 | halt_fast_timekeeper(tk); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1746 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1747 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1748 | |
Thomas Gleixner | 4ffee52 | 2015-03-25 13:09:16 +0100 | [diff] [blame] | 1749 | tick_suspend(); |
Magnus Damm | c54a42b | 2010-02-02 14:41:41 -0800 | [diff] [blame] | 1750 | clocksource_suspend(); |
Rafael J. Wysocki | adc78e6 | 2012-08-06 01:40:41 +0200 | [diff] [blame] | 1751 | clockevents_suspend(); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1752 | |
| 1753 | return 0; |
| 1754 | } |
| 1755 | |
| 1756 | /* sysfs resume/suspend bits for timekeeping */ |
Rafael J. Wysocki | e1a85b2 | 2011-03-23 22:16:04 +0100 | [diff] [blame] | 1757 | static struct syscore_ops timekeeping_syscore_ops = { |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1758 | .resume = timekeeping_resume, |
| 1759 | .suspend = timekeeping_suspend, |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1760 | }; |
| 1761 | |
Rafael J. Wysocki | e1a85b2 | 2011-03-23 22:16:04 +0100 | [diff] [blame] | 1762 | static int __init timekeeping_init_ops(void) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1763 | { |
Rafael J. Wysocki | e1a85b2 | 2011-03-23 22:16:04 +0100 | [diff] [blame] | 1764 | register_syscore_ops(&timekeeping_syscore_ops); |
| 1765 | return 0; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1766 | } |
Rafael J. Wysocki | e1a85b2 | 2011-03-23 22:16:04 +0100 | [diff] [blame] | 1767 | device_initcall(timekeeping_init_ops); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1768 | |
| 1769 | /* |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1770 | * Apply a multiplier adjustment to the timekeeper |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1771 | */ |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1772 | static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, |
| 1773 | s64 offset, |
| 1774 | bool negative, |
| 1775 | int adj_scale) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1776 | { |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1777 | s64 interval = tk->cycle_interval; |
| 1778 | s32 mult_adj = 1; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1779 | |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1780 | if (negative) { |
| 1781 | mult_adj = -mult_adj; |
| 1782 | interval = -interval; |
| 1783 | offset = -offset; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1784 | } |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1785 | mult_adj <<= adj_scale; |
| 1786 | interval <<= adj_scale; |
| 1787 | offset <<= adj_scale; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1788 | |
John Stultz | c2bc111 | 2011-10-27 18:12:42 -0700 | [diff] [blame] | 1789 | /* |
| 1790 | * So the following can be confusing. |
| 1791 | * |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1792 | * To keep things simple, lets assume mult_adj == 1 for now. |
John Stultz | c2bc111 | 2011-10-27 18:12:42 -0700 | [diff] [blame] | 1793 | * |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1794 | * When mult_adj != 1, remember that the interval and offset values |
John Stultz | c2bc111 | 2011-10-27 18:12:42 -0700 | [diff] [blame] | 1795 | * have been appropriately scaled so the math is the same. |
| 1796 | * |
| 1797 | * The basic idea here is that we're increasing the multiplier |
| 1798 | * by one, this causes the xtime_interval to be incremented by |
| 1799 | * one cycle_interval. This is because: |
| 1800 | * xtime_interval = cycle_interval * mult |
| 1801 | * So if mult is being incremented by one: |
| 1802 | * xtime_interval = cycle_interval * (mult + 1) |
| 1803 | * Its the same as: |
| 1804 | * xtime_interval = (cycle_interval * mult) + cycle_interval |
| 1805 | * Which can be shortened to: |
| 1806 | * xtime_interval += cycle_interval |
| 1807 | * |
| 1808 | * So offset stores the non-accumulated cycles. Thus the current |
| 1809 | * time (in shifted nanoseconds) is: |
| 1810 | * now = (offset * adj) + xtime_nsec |
| 1811 | * Now, even though we're adjusting the clock frequency, we have |
| 1812 | * to keep time consistent. In other words, we can't jump back |
| 1813 | * in time, and we also want to avoid jumping forward in time. |
| 1814 | * |
| 1815 | * So given the same offset value, we need the time to be the same |
| 1816 | * both before and after the freq adjustment. |
| 1817 | * now = (offset * adj_1) + xtime_nsec_1 |
| 1818 | * now = (offset * adj_2) + xtime_nsec_2 |
| 1819 | * So: |
| 1820 | * (offset * adj_1) + xtime_nsec_1 = |
| 1821 | * (offset * adj_2) + xtime_nsec_2 |
| 1822 | * And we know: |
| 1823 | * adj_2 = adj_1 + 1 |
| 1824 | * So: |
| 1825 | * (offset * adj_1) + xtime_nsec_1 = |
| 1826 | * (offset * (adj_1+1)) + xtime_nsec_2 |
| 1827 | * (offset * adj_1) + xtime_nsec_1 = |
| 1828 | * (offset * adj_1) + offset + xtime_nsec_2 |
| 1829 | * Canceling the sides: |
| 1830 | * xtime_nsec_1 = offset + xtime_nsec_2 |
| 1831 | * Which gives us: |
| 1832 | * xtime_nsec_2 = xtime_nsec_1 - offset |
| 1833 | * Which simplfies to: |
| 1834 | * xtime_nsec -= offset |
| 1835 | * |
| 1836 | * XXX - TODO: Doc ntp_error calculation. |
| 1837 | */ |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1838 | if ((mult_adj > 0) && (tk->tkr_mono.mult + mult_adj < mult_adj)) { |
pang.xunlei | 6067dc5 | 2014-10-08 15:03:34 +0800 | [diff] [blame] | 1839 | /* NTP adjustment caused clocksource mult overflow */ |
| 1840 | WARN_ON_ONCE(1); |
| 1841 | return; |
| 1842 | } |
| 1843 | |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1844 | tk->tkr_mono.mult += mult_adj; |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1845 | tk->xtime_interval += interval; |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1846 | tk->tkr_mono.xtime_nsec -= offset; |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1847 | tk->ntp_error -= (interval - offset) << tk->ntp_error_shift; |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1848 | } |
John Stultz | 2a8c088 | 2012-07-13 01:21:56 -0400 | [diff] [blame] | 1849 | |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1850 | /* |
| 1851 | * Calculate the multiplier adjustment needed to match the frequency |
| 1852 | * specified by NTP |
| 1853 | */ |
| 1854 | static __always_inline void timekeeping_freqadjust(struct timekeeper *tk, |
| 1855 | s64 offset) |
| 1856 | { |
| 1857 | s64 interval = tk->cycle_interval; |
| 1858 | s64 xinterval = tk->xtime_interval; |
John Stultz | ec02b07 | 2015-12-03 10:23:30 -0800 | [diff] [blame] | 1859 | u32 base = tk->tkr_mono.clock->mult; |
| 1860 | u32 max = tk->tkr_mono.clock->maxadj; |
| 1861 | u32 cur_adj = tk->tkr_mono.mult; |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1862 | s64 tick_error; |
| 1863 | bool negative; |
John Stultz | ec02b07 | 2015-12-03 10:23:30 -0800 | [diff] [blame] | 1864 | u32 adj_scale; |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1865 | |
| 1866 | /* Remove any current error adj from freq calculation */ |
| 1867 | if (tk->ntp_err_mult) |
| 1868 | xinterval -= tk->cycle_interval; |
| 1869 | |
John Stultz | 375f45b | 2014-04-23 20:53:29 -0700 | [diff] [blame] | 1870 | tk->ntp_tick = ntp_tick_length(); |
| 1871 | |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1872 | /* Calculate current error per tick */ |
| 1873 | tick_error = ntp_tick_length() >> tk->ntp_error_shift; |
| 1874 | tick_error -= (xinterval + tk->xtime_remainder); |
| 1875 | |
| 1876 | /* Don't worry about correcting it if its small */ |
| 1877 | if (likely((tick_error >= 0) && (tick_error <= interval))) |
| 1878 | return; |
| 1879 | |
| 1880 | /* preserve the direction of correction */ |
| 1881 | negative = (tick_error < 0); |
| 1882 | |
John Stultz | ec02b07 | 2015-12-03 10:23:30 -0800 | [diff] [blame] | 1883 | /* If any adjustment would pass the max, just return */ |
| 1884 | if (negative && (cur_adj - 1) <= (base - max)) |
| 1885 | return; |
| 1886 | if (!negative && (cur_adj + 1) >= (base + max)) |
| 1887 | return; |
| 1888 | /* |
| 1889 | * Sort out the magnitude of the correction, but |
| 1890 | * avoid making so large a correction that we go |
| 1891 | * over the max adjustment. |
| 1892 | */ |
| 1893 | adj_scale = 0; |
Andrew Morton | 79211c8 | 2015-11-09 14:58:13 -0800 | [diff] [blame] | 1894 | tick_error = abs(tick_error); |
John Stultz | ec02b07 | 2015-12-03 10:23:30 -0800 | [diff] [blame] | 1895 | while (tick_error > interval) { |
| 1896 | u32 adj = 1 << (adj_scale + 1); |
| 1897 | |
| 1898 | /* Check if adjustment gets us within 1 unit from the max */ |
| 1899 | if (negative && (cur_adj - adj) <= (base - max)) |
| 1900 | break; |
| 1901 | if (!negative && (cur_adj + adj) >= (base + max)) |
| 1902 | break; |
| 1903 | |
| 1904 | adj_scale++; |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1905 | tick_error >>= 1; |
John Stultz | ec02b07 | 2015-12-03 10:23:30 -0800 | [diff] [blame] | 1906 | } |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1907 | |
| 1908 | /* scale the corrections */ |
John Stultz | ec02b07 | 2015-12-03 10:23:30 -0800 | [diff] [blame] | 1909 | timekeeping_apply_adjustment(tk, offset, negative, adj_scale); |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | /* |
| 1913 | * Adjust the timekeeper's multiplier to the correct frequency |
| 1914 | * and also to reduce the accumulated error value. |
| 1915 | */ |
| 1916 | static void timekeeping_adjust(struct timekeeper *tk, s64 offset) |
| 1917 | { |
| 1918 | /* Correct for the current frequency error */ |
| 1919 | timekeeping_freqadjust(tk, offset); |
| 1920 | |
| 1921 | /* Next make a small adjustment to fix any cumulative error */ |
| 1922 | if (!tk->ntp_err_mult && (tk->ntp_error > 0)) { |
| 1923 | tk->ntp_err_mult = 1; |
| 1924 | timekeeping_apply_adjustment(tk, offset, 0, 0); |
| 1925 | } else if (tk->ntp_err_mult && (tk->ntp_error <= 0)) { |
| 1926 | /* Undo any existing error adjustment */ |
| 1927 | timekeeping_apply_adjustment(tk, offset, 1, 0); |
| 1928 | tk->ntp_err_mult = 0; |
| 1929 | } |
| 1930 | |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1931 | if (unlikely(tk->tkr_mono.clock->maxadj && |
| 1932 | (abs(tk->tkr_mono.mult - tk->tkr_mono.clock->mult) |
| 1933 | > tk->tkr_mono.clock->maxadj))) { |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1934 | printk_once(KERN_WARNING |
| 1935 | "Adjusting %s more than 11%% (%ld vs %ld)\n", |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1936 | tk->tkr_mono.clock->name, (long)tk->tkr_mono.mult, |
| 1937 | (long)tk->tkr_mono.clock->mult + tk->tkr_mono.clock->maxadj); |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1938 | } |
| 1939 | |
John Stultz | 2a8c088 | 2012-07-13 01:21:56 -0400 | [diff] [blame] | 1940 | /* |
| 1941 | * It may be possible that when we entered this function, xtime_nsec |
| 1942 | * was very small. Further, if we're slightly speeding the clocksource |
| 1943 | * in the code above, its possible the required corrective factor to |
| 1944 | * xtime_nsec could cause it to underflow. |
| 1945 | * |
| 1946 | * Now, since we already accumulated the second, cannot simply roll |
| 1947 | * the accumulated second back, since the NTP subsystem has been |
| 1948 | * notified via second_overflow. So instead we push xtime_nsec forward |
| 1949 | * by the amount we underflowed, and add that amount into the error. |
| 1950 | * |
| 1951 | * We'll correct this error next time through this function, when |
| 1952 | * xtime_nsec is not as small. |
| 1953 | */ |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1954 | if (unlikely((s64)tk->tkr_mono.xtime_nsec < 0)) { |
| 1955 | s64 neg = -(s64)tk->tkr_mono.xtime_nsec; |
| 1956 | tk->tkr_mono.xtime_nsec = 0; |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1957 | tk->ntp_error += neg << tk->ntp_error_shift; |
John Stultz | 2a8c088 | 2012-07-13 01:21:56 -0400 | [diff] [blame] | 1958 | } |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | /** |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1962 | * accumulate_nsecs_to_secs - Accumulates nsecs into secs |
| 1963 | * |
Zhen Lei | 571af55 | 2015-08-25 14:42:53 +0800 | [diff] [blame] | 1964 | * Helper function that accumulates the nsecs greater than a second |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1965 | * from the xtime_nsec field to the xtime_secs field. |
| 1966 | * It also calls into the NTP code to handle leapsecond processing. |
| 1967 | * |
| 1968 | */ |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 1969 | static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk) |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1970 | { |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1971 | u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr_mono.shift; |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 1972 | unsigned int clock_set = 0; |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1973 | |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1974 | while (tk->tkr_mono.xtime_nsec >= nsecps) { |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1975 | int leap; |
| 1976 | |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 1977 | tk->tkr_mono.xtime_nsec -= nsecps; |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1978 | tk->xtime_sec++; |
| 1979 | |
| 1980 | /* Figure out if its a leap sec and apply if needed */ |
| 1981 | leap = second_overflow(tk->xtime_sec); |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 1982 | if (unlikely(leap)) { |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1983 | struct timespec64 ts; |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1984 | |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 1985 | tk->xtime_sec += leap; |
| 1986 | |
| 1987 | ts.tv_sec = leap; |
| 1988 | ts.tv_nsec = 0; |
| 1989 | tk_set_wall_to_mono(tk, |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1990 | timespec64_sub(tk->wall_to_monotonic, ts)); |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 1991 | |
John Stultz | cc244dd | 2012-05-03 12:30:07 -0700 | [diff] [blame] | 1992 | __timekeeping_set_tai_offset(tk, tk->tai_offset - leap); |
| 1993 | |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 1994 | clock_set = TK_CLOCK_WAS_SET; |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 1995 | } |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1996 | } |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 1997 | return clock_set; |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1998 | } |
| 1999 | |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 2000 | /** |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 2001 | * logarithmic_accumulation - shifted accumulation of cycles |
| 2002 | * |
| 2003 | * This functions accumulates a shifted interval of cycles into |
| 2004 | * into a shifted interval nanoseconds. Allows for O(log) accumulation |
| 2005 | * loop. |
| 2006 | * |
| 2007 | * Returns the unconsumed cycles. |
| 2008 | */ |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 2009 | static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset, |
| 2010 | u32 shift, unsigned int *clock_set) |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 2011 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 2012 | u64 interval = tk->cycle_interval << shift; |
John Stultz | 3d88d56 | 2017-06-08 16:44:21 -0700 | [diff] [blame] | 2013 | u64 snsec_per_sec; |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 2014 | |
Zhen Lei | 571af55 | 2015-08-25 14:42:53 +0800 | [diff] [blame] | 2015 | /* If the offset is smaller than a shifted interval, do nothing */ |
Thomas Gleixner | 23a9537 | 2013-02-21 22:51:36 +0000 | [diff] [blame] | 2016 | if (offset < interval) |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 2017 | return offset; |
| 2018 | |
| 2019 | /* Accumulate one shifted interval */ |
Thomas Gleixner | 23a9537 | 2013-02-21 22:51:36 +0000 | [diff] [blame] | 2020 | offset -= interval; |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 2021 | tk->tkr_mono.cycle_last += interval; |
Peter Zijlstra | 4a4ad80 | 2015-03-19 09:28:44 +0100 | [diff] [blame] | 2022 | tk->tkr_raw.cycle_last += interval; |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 2023 | |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 2024 | tk->tkr_mono.xtime_nsec += tk->xtime_interval << shift; |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 2025 | *clock_set |= accumulate_nsecs_to_secs(tk); |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 2026 | |
Jason Wessel | deda2e8 | 2010-08-09 14:20:09 -0700 | [diff] [blame] | 2027 | /* Accumulate raw time */ |
John Stultz | 3d88d56 | 2017-06-08 16:44:21 -0700 | [diff] [blame] | 2028 | tk->tkr_raw.xtime_nsec += tk->raw_interval << shift; |
| 2029 | snsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_raw.shift; |
| 2030 | while (tk->tkr_raw.xtime_nsec >= snsec_per_sec) { |
| 2031 | tk->tkr_raw.xtime_nsec -= snsec_per_sec; |
John Stultz | fc6eead7 | 2017-05-22 17:20:20 -0700 | [diff] [blame] | 2032 | tk->raw_sec++; |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | /* Accumulate error between NTP and clock interval */ |
John Stultz | 375f45b | 2014-04-23 20:53:29 -0700 | [diff] [blame] | 2036 | tk->ntp_error += tk->ntp_tick << shift; |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 2037 | tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) << |
| 2038 | (tk->ntp_error_shift + shift); |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 2039 | |
| 2040 | return offset; |
| 2041 | } |
| 2042 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 2043 | /** |
| 2044 | * update_wall_time - Uses the current clocksource to increment the wall time |
| 2045 | * |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 2046 | */ |
John Stultz | 47a1b796 | 2013-12-12 13:10:55 -0800 | [diff] [blame] | 2047 | void update_wall_time(void) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 2048 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2049 | struct timekeeper *real_tk = &tk_core.timekeeper; |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 2050 | struct timekeeper *tk = &shadow_timekeeper; |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 2051 | u64 offset; |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 2052 | int shift = 0, maxshift; |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 2053 | unsigned int clock_set = 0; |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 2054 | unsigned long flags; |
| 2055 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 2056 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 2057 | |
| 2058 | /* Make sure we're fully resumed: */ |
| 2059 | if (unlikely(timekeeping_suspended)) |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 2060 | goto out; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 2061 | |
John Stultz | 592913e | 2010-07-13 17:56:20 -0700 | [diff] [blame] | 2062 | #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 2063 | offset = real_tk->cycle_interval; |
John Stultz | 592913e | 2010-07-13 17:56:20 -0700 | [diff] [blame] | 2064 | #else |
John Stultz | ceea5e3 | 2017-06-08 16:44:20 -0700 | [diff] [blame] | 2065 | offset = clocksource_delta(tk_clock_read(&tk->tkr_mono), |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 2066 | tk->tkr_mono.cycle_last, tk->tkr_mono.mask); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 2067 | #endif |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 2068 | |
John Stultz | bf2ac31 | 2012-08-21 20:30:49 -0400 | [diff] [blame] | 2069 | /* Check if there's really nothing to do */ |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 2070 | if (offset < real_tk->cycle_interval) |
John Stultz | bf2ac31 | 2012-08-21 20:30:49 -0400 | [diff] [blame] | 2071 | goto out; |
| 2072 | |
John Stultz | 3c17ad1 | 2015-03-11 21:16:32 -0700 | [diff] [blame] | 2073 | /* Do some additional sanity checking */ |
Stafford Horne | a529bea | 2017-06-28 22:21:35 +0900 | [diff] [blame] | 2074 | timekeeping_check_update(tk, offset); |
John Stultz | 3c17ad1 | 2015-03-11 21:16:32 -0700 | [diff] [blame] | 2075 | |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 2076 | /* |
| 2077 | * With NO_HZ we may have to accumulate many cycle_intervals |
| 2078 | * (think "ticks") worth of time at once. To do this efficiently, |
| 2079 | * we calculate the largest doubling multiple of cycle_intervals |
Jim Cromie | 88b28ad | 2012-03-14 21:28:56 -0600 | [diff] [blame] | 2080 | * that is smaller than the offset. We then accumulate that |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 2081 | * chunk in one go, and then try to consume the next smaller |
| 2082 | * doubled multiple. |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 2083 | */ |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 2084 | shift = ilog2(offset) - ilog2(tk->cycle_interval); |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 2085 | shift = max(0, shift); |
Jim Cromie | 88b28ad | 2012-03-14 21:28:56 -0600 | [diff] [blame] | 2086 | /* Bound shift to one less than what overflows tick_length */ |
John Stultz | ea7cf49 | 2011-11-14 13:18:07 -0800 | [diff] [blame] | 2087 | maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1; |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 2088 | shift = min(shift, maxshift); |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 2089 | while (offset >= tk->cycle_interval) { |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 2090 | offset = logarithmic_accumulation(tk, offset, shift, |
| 2091 | &clock_set); |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 2092 | if (offset < tk->cycle_interval<<shift) |
John Stultz | 830ec04 | 2010-03-18 14:47:30 -0700 | [diff] [blame] | 2093 | shift--; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | /* correct the clock when NTP error is too big */ |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 2097 | timekeeping_adjust(tk, offset); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 2098 | |
John Stultz | 6a867a3 | 2010-04-06 14:30:51 -0700 | [diff] [blame] | 2099 | /* |
John Stultz | 92bb1fc | 2012-09-04 15:38:12 -0400 | [diff] [blame] | 2100 | * XXX This can be killed once everyone converts |
| 2101 | * to the new update_vsyscall. |
| 2102 | */ |
| 2103 | old_vsyscall_fixup(tk); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 2104 | |
John Stultz | 6a867a3 | 2010-04-06 14:30:51 -0700 | [diff] [blame] | 2105 | /* |
| 2106 | * Finally, make sure that after the rounding |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 2107 | * xtime_nsec isn't larger than NSEC_PER_SEC |
John Stultz | 6a867a3 | 2010-04-06 14:30:51 -0700 | [diff] [blame] | 2108 | */ |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 2109 | clock_set |= accumulate_nsecs_to_secs(tk); |
Linus Torvalds | 83f57a1 | 2009-12-22 14:10:37 -0800 | [diff] [blame] | 2110 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2111 | write_seqcount_begin(&tk_core.seq); |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 2112 | /* |
| 2113 | * Update the real timekeeper. |
| 2114 | * |
| 2115 | * We could avoid this memcpy by switching pointers, but that |
| 2116 | * requires changes to all other timekeeper usage sites as |
| 2117 | * well, i.e. move the timekeeper pointer getter into the |
| 2118 | * spinlocked/seqcount protected sections. And we trade this |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2119 | * memcpy under the tk_core.seq against one before we start |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 2120 | * updating. |
| 2121 | */ |
John Stultz | 906c555 | 2015-06-17 10:05:53 -0700 | [diff] [blame] | 2122 | timekeeping_update(tk, clock_set); |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 2123 | memcpy(real_tk, tk, sizeof(*tk)); |
John Stultz | 906c555 | 2015-06-17 10:05:53 -0700 | [diff] [blame] | 2124 | /* The memcpy must come last. Do not put anything here! */ |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2125 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | ca4523c | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 2126 | out: |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 2127 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
John Stultz | 47a1b796 | 2013-12-12 13:10:55 -0800 | [diff] [blame] | 2128 | if (clock_set) |
John Stultz | cab5e12 | 2014-03-27 16:30:49 -0700 | [diff] [blame] | 2129 | /* Have to call _delayed version, since in irq context*/ |
| 2130 | clock_was_set_delayed(); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 2131 | } |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 2132 | |
| 2133 | /** |
John Stultz | d08c0cd | 2014-12-08 12:00:09 -0800 | [diff] [blame] | 2134 | * getboottime64 - Return the real time of system boot. |
| 2135 | * @ts: pointer to the timespec64 to be set |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 2136 | * |
John Stultz | d08c0cd | 2014-12-08 12:00:09 -0800 | [diff] [blame] | 2137 | * Returns the wall-time of boot in a timespec64. |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 2138 | * |
| 2139 | * This is based on the wall_to_monotonic offset and the total suspend |
| 2140 | * time. Calls to settimeofday will affect the value returned (which |
| 2141 | * basically means that however wrong your real time clock is at boot time, |
| 2142 | * you get the right time here). |
| 2143 | */ |
John Stultz | d08c0cd | 2014-12-08 12:00:09 -0800 | [diff] [blame] | 2144 | void getboottime64(struct timespec64 *ts) |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 2145 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2146 | struct timekeeper *tk = &tk_core.timekeeper; |
Thomas Gleixner | 02cba15 | 2014-07-16 21:04:58 +0000 | [diff] [blame] | 2147 | ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot); |
Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 2148 | |
John Stultz | d08c0cd | 2014-12-08 12:00:09 -0800 | [diff] [blame] | 2149 | *ts = ktime_to_timespec64(t); |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 2150 | } |
John Stultz | d08c0cd | 2014-12-08 12:00:09 -0800 | [diff] [blame] | 2151 | EXPORT_SYMBOL_GPL(getboottime64); |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 2152 | |
john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 2153 | unsigned long get_seconds(void) |
| 2154 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2155 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 2156 | |
| 2157 | return tk->xtime_sec; |
john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 2158 | } |
| 2159 | EXPORT_SYMBOL(get_seconds); |
| 2160 | |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 2161 | struct timespec __current_kernel_time(void) |
| 2162 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2163 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 2164 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 2165 | return timespec64_to_timespec(tk_xtime(tk)); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 2166 | } |
john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 2167 | |
Baolin Wang | 8758a24 | 2015-07-29 20:09:43 +0800 | [diff] [blame] | 2168 | struct timespec64 current_kernel_time64(void) |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 2169 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2170 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 2171 | struct timespec64 now; |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 2172 | unsigned long seq; |
| 2173 | |
| 2174 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2175 | seq = read_seqcount_begin(&tk_core.seq); |
Linus Torvalds | 83f57a1 | 2009-12-22 14:10:37 -0800 | [diff] [blame] | 2176 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 2177 | now = tk_xtime(tk); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2178 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 2179 | |
Baolin Wang | 8758a24 | 2015-07-29 20:09:43 +0800 | [diff] [blame] | 2180 | return now; |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 2181 | } |
Baolin Wang | 8758a24 | 2015-07-29 20:09:43 +0800 | [diff] [blame] | 2182 | EXPORT_SYMBOL(current_kernel_time64); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 2183 | |
John Stultz | 334334b | 2014-11-07 11:20:40 -0800 | [diff] [blame] | 2184 | struct timespec64 get_monotonic_coarse64(void) |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 2185 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2186 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 2187 | struct timespec64 now, mono; |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 2188 | unsigned long seq; |
| 2189 | |
| 2190 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2191 | seq = read_seqcount_begin(&tk_core.seq); |
Linus Torvalds | 83f57a1 | 2009-12-22 14:10:37 -0800 | [diff] [blame] | 2192 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 2193 | now = tk_xtime(tk); |
| 2194 | mono = tk->wall_to_monotonic; |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2195 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 2196 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 2197 | set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec, |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 2198 | now.tv_nsec + mono.tv_nsec); |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 2199 | |
John Stultz | 334334b | 2014-11-07 11:20:40 -0800 | [diff] [blame] | 2200 | return now; |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 2201 | } |
Gregor Boirie | eaaa7ec | 2016-03-09 19:05:48 +0100 | [diff] [blame] | 2202 | EXPORT_SYMBOL(get_monotonic_coarse64); |
Torben Hohn | 871cf1e | 2011-01-27 15:58:55 +0100 | [diff] [blame] | 2203 | |
| 2204 | /* |
John Stultz | d6ad418 | 2012-02-28 16:50:11 -0800 | [diff] [blame] | 2205 | * Must hold jiffies_lock |
Torben Hohn | 871cf1e | 2011-01-27 15:58:55 +0100 | [diff] [blame] | 2206 | */ |
| 2207 | void do_timer(unsigned long ticks) |
| 2208 | { |
| 2209 | jiffies_64 += ticks; |
Torben Hohn | 871cf1e | 2011-01-27 15:58:55 +0100 | [diff] [blame] | 2210 | calc_global_load(ticks); |
| 2211 | } |
Torben Hohn | 48cf76f7 | 2011-01-27 15:59:05 +0100 | [diff] [blame] | 2212 | |
| 2213 | /** |
John Stultz | 76f4108 | 2014-07-16 21:03:52 +0000 | [diff] [blame] | 2214 | * ktime_get_update_offsets_now - hrtimer helper |
Thomas Gleixner | 868a3e9 | 2015-04-14 21:08:37 +0000 | [diff] [blame] | 2215 | * @cwsseq: pointer to check and store the clock was set sequence number |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 2216 | * @offs_real: pointer to storage for monotonic -> realtime offset |
| 2217 | * @offs_boot: pointer to storage for monotonic -> boottime offset |
Xie XiuQi | b7bc50e | 2013-10-18 09:13:30 +0800 | [diff] [blame] | 2218 | * @offs_tai: pointer to storage for monotonic -> clock tai offset |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 2219 | * |
Thomas Gleixner | 868a3e9 | 2015-04-14 21:08:37 +0000 | [diff] [blame] | 2220 | * Returns current monotonic time and updates the offsets if the |
| 2221 | * sequence number in @cwsseq and timekeeper.clock_was_set_seq are |
| 2222 | * different. |
| 2223 | * |
Xie XiuQi | b7bc50e | 2013-10-18 09:13:30 +0800 | [diff] [blame] | 2224 | * Called from hrtimer_interrupt() or retrigger_next_event() |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 2225 | */ |
Thomas Gleixner | 868a3e9 | 2015-04-14 21:08:37 +0000 | [diff] [blame] | 2226 | ktime_t ktime_get_update_offsets_now(unsigned int *cwsseq, ktime_t *offs_real, |
| 2227 | ktime_t *offs_boot, ktime_t *offs_tai) |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 2228 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2229 | struct timekeeper *tk = &tk_core.timekeeper; |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 2230 | unsigned int seq; |
Thomas Gleixner | a37c0aa | 2014-07-16 21:04:19 +0000 | [diff] [blame] | 2231 | ktime_t base; |
| 2232 | u64 nsecs; |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 2233 | |
| 2234 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2235 | seq = read_seqcount_begin(&tk_core.seq); |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 2236 | |
Peter Zijlstra | 876e788 | 2015-03-19 10:09:06 +0100 | [diff] [blame] | 2237 | base = tk->tkr_mono.base; |
| 2238 | nsecs = timekeeping_get_ns(&tk->tkr_mono); |
John Stultz | 833f32d | 2015-06-11 15:54:55 -0700 | [diff] [blame] | 2239 | base = ktime_add_ns(base, nsecs); |
| 2240 | |
Thomas Gleixner | 868a3e9 | 2015-04-14 21:08:37 +0000 | [diff] [blame] | 2241 | if (*cwsseq != tk->clock_was_set_seq) { |
| 2242 | *cwsseq = tk->clock_was_set_seq; |
| 2243 | *offs_real = tk->offs_real; |
| 2244 | *offs_boot = tk->offs_boot; |
| 2245 | *offs_tai = tk->offs_tai; |
| 2246 | } |
John Stultz | 833f32d | 2015-06-11 15:54:55 -0700 | [diff] [blame] | 2247 | |
| 2248 | /* Handle leapsecond insertion adjustments */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 2249 | if (unlikely(base >= tk->next_leap_ktime)) |
John Stultz | 833f32d | 2015-06-11 15:54:55 -0700 | [diff] [blame] | 2250 | *offs_real = ktime_sub(tk->offs_real, ktime_set(1, 0)); |
| 2251 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2252 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 2253 | |
John Stultz | 833f32d | 2015-06-11 15:54:55 -0700 | [diff] [blame] | 2254 | return base; |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 2255 | } |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 2256 | |
Torben Hohn | f0af911a9 | 2011-01-27 15:59:10 +0100 | [diff] [blame] | 2257 | /** |
John Stultz | aa6f9c59 | 2013-03-22 11:31:29 -0700 | [diff] [blame] | 2258 | * do_adjtimex() - Accessor function to NTP __do_adjtimex function |
| 2259 | */ |
| 2260 | int do_adjtimex(struct timex *txc) |
| 2261 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2262 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 2263 | unsigned long flags; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 2264 | struct timespec64 ts; |
John Stultz | 4e8f8b3 | 2013-04-10 12:41:49 -0700 | [diff] [blame] | 2265 | s32 orig_tai, tai; |
John Stultz | e408569 | 2013-03-22 12:08:52 -0700 | [diff] [blame] | 2266 | int ret; |
| 2267 | |
| 2268 | /* Validate the data before disabling interrupts */ |
| 2269 | ret = ntp_validate_timex(txc); |
| 2270 | if (ret) |
| 2271 | return ret; |
| 2272 | |
John Stultz | cef9037 | 2013-03-22 15:04:13 -0700 | [diff] [blame] | 2273 | if (txc->modes & ADJ_SETOFFSET) { |
| 2274 | struct timespec delta; |
| 2275 | delta.tv_sec = txc->time.tv_sec; |
| 2276 | delta.tv_nsec = txc->time.tv_usec; |
| 2277 | if (!(txc->modes & ADJ_NANO)) |
| 2278 | delta.tv_nsec *= 1000; |
| 2279 | ret = timekeeping_inject_offset(&delta); |
| 2280 | if (ret) |
| 2281 | return ret; |
| 2282 | } |
| 2283 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 2284 | getnstimeofday64(&ts); |
John Stultz | aa6f9c59 | 2013-03-22 11:31:29 -0700 | [diff] [blame] | 2285 | |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 2286 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2287 | write_seqcount_begin(&tk_core.seq); |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 2288 | |
John Stultz | 4e8f8b3 | 2013-04-10 12:41:49 -0700 | [diff] [blame] | 2289 | orig_tai = tai = tk->tai_offset; |
John Stultz | 87ace39 | 2013-03-22 12:28:15 -0700 | [diff] [blame] | 2290 | ret = __do_adjtimex(txc, &ts, &tai); |
| 2291 | |
John Stultz | 4e8f8b3 | 2013-04-10 12:41:49 -0700 | [diff] [blame] | 2292 | if (tai != orig_tai) { |
| 2293 | __timekeeping_set_tai_offset(tk, tai); |
John Stultz | f55c076 | 2013-12-11 18:50:25 -0800 | [diff] [blame] | 2294 | timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); |
John Stultz | 4e8f8b3 | 2013-04-10 12:41:49 -0700 | [diff] [blame] | 2295 | } |
John Stultz | 833f32d | 2015-06-11 15:54:55 -0700 | [diff] [blame] | 2296 | tk_update_leap_state(tk); |
| 2297 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2298 | write_seqcount_end(&tk_core.seq); |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 2299 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
| 2300 | |
John Stultz | 6fdda9a | 2013-12-10 17:18:18 -0800 | [diff] [blame] | 2301 | if (tai != orig_tai) |
| 2302 | clock_was_set(); |
| 2303 | |
John Stultz | 7bd3601 | 2013-09-11 16:50:56 -0700 | [diff] [blame] | 2304 | ntp_notify_cmos_timer(); |
| 2305 | |
John Stultz | 87ace39 | 2013-03-22 12:28:15 -0700 | [diff] [blame] | 2306 | return ret; |
| 2307 | } |
John Stultz | aa6f9c59 | 2013-03-22 11:31:29 -0700 | [diff] [blame] | 2308 | |
| 2309 | #ifdef CONFIG_NTP_PPS |
| 2310 | /** |
| 2311 | * hardpps() - Accessor function to NTP __hardpps function |
| 2312 | */ |
Arnd Bergmann | 7ec88e4 | 2015-09-28 22:21:28 +0200 | [diff] [blame] | 2313 | void hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts) |
John Stultz | aa6f9c59 | 2013-03-22 11:31:29 -0700 | [diff] [blame] | 2314 | { |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 2315 | unsigned long flags; |
| 2316 | |
| 2317 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2318 | write_seqcount_begin(&tk_core.seq); |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 2319 | |
John Stultz | aa6f9c59 | 2013-03-22 11:31:29 -0700 | [diff] [blame] | 2320 | __hardpps(phase_ts, raw_ts); |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 2321 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 2322 | write_seqcount_end(&tk_core.seq); |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 2323 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
John Stultz | aa6f9c59 | 2013-03-22 11:31:29 -0700 | [diff] [blame] | 2324 | } |
| 2325 | EXPORT_SYMBOL(hardpps); |
Robert P. J. Day | a2d8180 | 2017-09-08 16:17:19 -0700 | [diff] [blame] | 2326 | #endif /* CONFIG_NTP_PPS */ |
John Stultz | aa6f9c59 | 2013-03-22 11:31:29 -0700 | [diff] [blame] | 2327 | |
| 2328 | /** |
Torben Hohn | f0af911a9 | 2011-01-27 15:59:10 +0100 | [diff] [blame] | 2329 | * xtime_update() - advances the timekeeping infrastructure |
| 2330 | * @ticks: number of ticks, that have elapsed since the last call. |
| 2331 | * |
| 2332 | * Must be called with interrupts disabled. |
| 2333 | */ |
| 2334 | void xtime_update(unsigned long ticks) |
| 2335 | { |
John Stultz | d6ad418 | 2012-02-28 16:50:11 -0800 | [diff] [blame] | 2336 | write_seqlock(&jiffies_lock); |
Torben Hohn | f0af911a9 | 2011-01-27 15:59:10 +0100 | [diff] [blame] | 2337 | do_timer(ticks); |
John Stultz | d6ad418 | 2012-02-28 16:50:11 -0800 | [diff] [blame] | 2338 | write_sequnlock(&jiffies_lock); |
John Stultz | 47a1b796 | 2013-12-12 13:10:55 -0800 | [diff] [blame] | 2339 | update_wall_time(); |
Torben Hohn | f0af911a9 | 2011-01-27 15:59:10 +0100 | [diff] [blame] | 2340 | } |