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