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> |
Rafael J. Wysocki | e1a85b2 | 2011-03-23 22:16:04 +0100 | [diff] [blame] | 18 | #include <linux/syscore_ops.h> |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 19 | #include <linux/clocksource.h> |
| 20 | #include <linux/jiffies.h> |
| 21 | #include <linux/time.h> |
| 22 | #include <linux/tick.h> |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 23 | #include <linux/stop_machine.h> |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 24 | #include <linux/pvclock_gtod.h> |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 25 | #include <linux/compiler.h> |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 26 | |
Thomas Gleixner | eb93e4d | 2013-02-21 22:51:36 +0000 | [diff] [blame] | 27 | #include "tick-internal.h" |
John Stultz | aa6f9c59 | 2013-03-22 11:31:29 -0700 | [diff] [blame] | 28 | #include "ntp_internal.h" |
Colin Cross | 5c83545 | 2013-05-21 22:32:14 -0700 | [diff] [blame] | 29 | #include "timekeeping_internal.h" |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 30 | |
David Vrabel | 04397fe9 | 2013-06-27 11:35:45 +0100 | [diff] [blame] | 31 | #define TK_CLEAR_NTP (1 << 0) |
| 32 | #define TK_MIRROR (1 << 1) |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 33 | #define TK_CLOCK_WAS_SET (1 << 2) |
David Vrabel | 04397fe9 | 2013-06-27 11:35:45 +0100 | [diff] [blame] | 34 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 35 | /* |
| 36 | * The most important data for readout fits into a single 64 byte |
| 37 | * cache line. |
| 38 | */ |
| 39 | static struct { |
| 40 | seqcount_t seq; |
| 41 | struct timekeeper timekeeper; |
| 42 | } tk_core ____cacheline_aligned; |
| 43 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 44 | static DEFINE_RAW_SPINLOCK(timekeeper_lock); |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 45 | static struct timekeeper shadow_timekeeper; |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 46 | |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 47 | /** |
| 48 | * struct tk_fast - NMI safe timekeeper |
| 49 | * @seq: Sequence counter for protecting updates. The lowest bit |
| 50 | * is the index for the tk_read_base array |
| 51 | * @base: tk_read_base array. Access is indexed by the lowest bit of |
| 52 | * @seq. |
| 53 | * |
| 54 | * See @update_fast_timekeeper() below. |
| 55 | */ |
| 56 | struct tk_fast { |
| 57 | seqcount_t seq; |
| 58 | struct tk_read_base base[2]; |
| 59 | }; |
| 60 | |
| 61 | static struct tk_fast tk_fast_mono ____cacheline_aligned; |
| 62 | |
John Stultz | 8fcce54 | 2011-11-14 11:46:39 -0800 | [diff] [blame] | 63 | /* flag for if timekeeping is suspended */ |
| 64 | int __read_mostly timekeeping_suspended; |
| 65 | |
Feng Tang | 31ade30 | 2013-01-16 00:09:47 +0800 | [diff] [blame] | 66 | /* Flag for if there is a persistent clock on this platform */ |
| 67 | bool __read_mostly persistent_clock_exist = false; |
| 68 | |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 69 | static inline void tk_normalize_xtime(struct timekeeper *tk) |
| 70 | { |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 71 | while (tk->tkr.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr.shift)) { |
| 72 | tk->tkr.xtime_nsec -= (u64)NSEC_PER_SEC << tk->tkr.shift; |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 73 | tk->xtime_sec++; |
| 74 | } |
| 75 | } |
John Stultz | 8fcce54 | 2011-11-14 11:46:39 -0800 | [diff] [blame] | 76 | |
Thomas Gleixner | c905fae | 2014-07-16 21:04:05 +0000 | [diff] [blame] | 77 | static inline struct timespec64 tk_xtime(struct timekeeper *tk) |
| 78 | { |
| 79 | struct timespec64 ts; |
| 80 | |
| 81 | ts.tv_sec = tk->xtime_sec; |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 82 | ts.tv_nsec = (long)(tk->tkr.xtime_nsec >> tk->tkr.shift); |
Thomas Gleixner | c905fae | 2014-07-16 21:04:05 +0000 | [diff] [blame] | 83 | return ts; |
| 84 | } |
| 85 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 86 | static void tk_set_xtime(struct timekeeper *tk, const struct timespec64 *ts) |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 87 | { |
| 88 | tk->xtime_sec = ts->tv_sec; |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 89 | tk->tkr.xtime_nsec = (u64)ts->tv_nsec << tk->tkr.shift; |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 90 | } |
| 91 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 92 | static void tk_xtime_add(struct timekeeper *tk, const struct timespec64 *ts) |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 93 | { |
| 94 | tk->xtime_sec += ts->tv_sec; |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 95 | tk->tkr.xtime_nsec += (u64)ts->tv_nsec << tk->tkr.shift; |
John Stultz | 784ffcb | 2012-08-21 20:30:46 -0400 | [diff] [blame] | 96 | tk_normalize_xtime(tk); |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 97 | } |
John Stultz | 8fcce54 | 2011-11-14 11:46:39 -0800 | [diff] [blame] | 98 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 99 | 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] | 100 | { |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 101 | struct timespec64 tmp; |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * Verify consistency of: offset_real = -wall_to_monotonic |
| 105 | * before modifying anything |
| 106 | */ |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 107 | set_normalized_timespec64(&tmp, -tk->wall_to_monotonic.tv_sec, |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 108 | -tk->wall_to_monotonic.tv_nsec); |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 109 | WARN_ON_ONCE(tk->offs_real.tv64 != timespec64_to_ktime(tmp).tv64); |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 110 | tk->wall_to_monotonic = wtm; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 111 | set_normalized_timespec64(&tmp, -wtm.tv_sec, -wtm.tv_nsec); |
| 112 | tk->offs_real = timespec64_to_ktime(tmp); |
John Stultz | 04005f6 | 2013-12-10 17:13:35 -0800 | [diff] [blame] | 113 | 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] | 114 | } |
| 115 | |
Thomas Gleixner | 47da70d | 2014-07-16 21:05:00 +0000 | [diff] [blame] | 116 | 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] | 117 | { |
Thomas Gleixner | 47da70d | 2014-07-16 21:05:00 +0000 | [diff] [blame] | 118 | tk->offs_boot = ktime_add(tk->offs_boot, delta); |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 121 | /** |
Yijing Wang | d26e4fe | 2013-11-28 16:28:55 +0800 | [diff] [blame] | 122 | * tk_setup_internals - Set up internals to use clocksource clock. |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 123 | * |
Yijing Wang | d26e4fe | 2013-11-28 16:28:55 +0800 | [diff] [blame] | 124 | * @tk: The target timekeeper to setup. |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 125 | * @clock: Pointer to clocksource. |
| 126 | * |
| 127 | * Calculates a fixed cycle/nsec interval for a given clocksource/adjustment |
| 128 | * pair and interval request. |
| 129 | * |
| 130 | * Unless you're the timekeeping code, you should not be using this! |
| 131 | */ |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 132 | static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 133 | { |
| 134 | cycle_t interval; |
Kasper Pedersen | a386b5a | 2010-10-20 15:55:15 -0700 | [diff] [blame] | 135 | u64 tmp, ntpinterval; |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 136 | struct clocksource *old_clock; |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 137 | |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 138 | old_clock = tk->tkr.clock; |
| 139 | tk->tkr.clock = clock; |
| 140 | tk->tkr.read = clock->read; |
| 141 | tk->tkr.mask = clock->mask; |
| 142 | tk->tkr.cycle_last = tk->tkr.read(clock); |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 143 | |
| 144 | /* Do the ns -> cycle conversion first, using original mult */ |
| 145 | tmp = NTP_INTERVAL_LENGTH; |
| 146 | tmp <<= clock->shift; |
Kasper Pedersen | a386b5a | 2010-10-20 15:55:15 -0700 | [diff] [blame] | 147 | ntpinterval = tmp; |
Martin Schwidefsky | 0a54419 | 2009-08-14 15:47:28 +0200 | [diff] [blame] | 148 | tmp += clock->mult/2; |
| 149 | do_div(tmp, clock->mult); |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 150 | if (tmp == 0) |
| 151 | tmp = 1; |
| 152 | |
| 153 | interval = (cycle_t) tmp; |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 154 | tk->cycle_interval = interval; |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 155 | |
| 156 | /* Go back from cycles -> shifted ns */ |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 157 | tk->xtime_interval = (u64) interval * clock->mult; |
| 158 | tk->xtime_remainder = ntpinterval - tk->xtime_interval; |
| 159 | tk->raw_interval = |
Martin Schwidefsky | 0a54419 | 2009-08-14 15:47:28 +0200 | [diff] [blame] | 160 | ((u64) interval * clock->mult) >> clock->shift; |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 161 | |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 162 | /* if changing clocks, convert xtime_nsec shift units */ |
| 163 | if (old_clock) { |
| 164 | int shift_change = clock->shift - old_clock->shift; |
| 165 | if (shift_change < 0) |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 166 | tk->tkr.xtime_nsec >>= -shift_change; |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 167 | else |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 168 | tk->tkr.xtime_nsec <<= shift_change; |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 169 | } |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 170 | tk->tkr.shift = clock->shift; |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 171 | |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 172 | tk->ntp_error = 0; |
| 173 | tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift; |
John Stultz | 375f45b | 2014-04-23 20:53:29 -0700 | [diff] [blame] | 174 | tk->ntp_tick = ntpinterval << tk->ntp_error_shift; |
Martin Schwidefsky | 0a54419 | 2009-08-14 15:47:28 +0200 | [diff] [blame] | 175 | |
| 176 | /* |
| 177 | * The timekeeper keeps its own mult values for the currently |
| 178 | * active clocksource. These value will be adjusted via NTP |
| 179 | * to counteract clock drifting. |
| 180 | */ |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 181 | tk->tkr.mult = clock->mult; |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 182 | tk->ntp_err_mult = 0; |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 183 | } |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 184 | |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 185 | /* Timekeeper helper functions. */ |
Stephen Warren | 7b1f620 | 2012-11-07 17:58:54 -0700 | [diff] [blame] | 186 | |
| 187 | #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET |
Thomas Gleixner | e06fde3 | 2014-07-16 21:03:50 +0000 | [diff] [blame] | 188 | static u32 default_arch_gettimeoffset(void) { return 0; } |
| 189 | u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset; |
Stephen Warren | 7b1f620 | 2012-11-07 17:58:54 -0700 | [diff] [blame] | 190 | #else |
Thomas Gleixner | e06fde3 | 2014-07-16 21:03:50 +0000 | [diff] [blame] | 191 | static inline u32 arch_gettimeoffset(void) { return 0; } |
Stephen Warren | 7b1f620 | 2012-11-07 17:58:54 -0700 | [diff] [blame] | 192 | #endif |
| 193 | |
Thomas Gleixner | 0e5ac3a | 2014-07-16 21:05:18 +0000 | [diff] [blame] | 194 | static inline s64 timekeeping_get_ns(struct tk_read_base *tkr) |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 195 | { |
Thomas Gleixner | 3a97837 | 2014-07-16 21:05:10 +0000 | [diff] [blame] | 196 | cycle_t cycle_now, delta; |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 197 | s64 nsec; |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 198 | |
| 199 | /* read clocksource: */ |
Thomas Gleixner | 0e5ac3a | 2014-07-16 21:05:18 +0000 | [diff] [blame] | 200 | cycle_now = tkr->read(tkr->clock); |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 201 | |
| 202 | /* calculate the delta since the last update_wall_time: */ |
Thomas Gleixner | 0e5ac3a | 2014-07-16 21:05:18 +0000 | [diff] [blame] | 203 | delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask); |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 204 | |
Thomas Gleixner | 0e5ac3a | 2014-07-16 21:05:18 +0000 | [diff] [blame] | 205 | nsec = delta * tkr->mult + tkr->xtime_nsec; |
| 206 | nsec >>= tkr->shift; |
John Stultz | f2a5a08 | 2012-07-13 01:21:55 -0400 | [diff] [blame] | 207 | |
Stephen Warren | 7b1f620 | 2012-11-07 17:58:54 -0700 | [diff] [blame] | 208 | /* If arch requires, add in get_arch_timeoffset() */ |
Thomas Gleixner | e06fde3 | 2014-07-16 21:03:50 +0000 | [diff] [blame] | 209 | return nsec + arch_gettimeoffset(); |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 210 | } |
| 211 | |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 212 | static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk) |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 213 | { |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 214 | struct clocksource *clock = tk->tkr.clock; |
Thomas Gleixner | 3a97837 | 2014-07-16 21:05:10 +0000 | [diff] [blame] | 215 | cycle_t cycle_now, delta; |
John Stultz | f2a5a08 | 2012-07-13 01:21:55 -0400 | [diff] [blame] | 216 | s64 nsec; |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 217 | |
| 218 | /* read clocksource: */ |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 219 | cycle_now = tk->tkr.read(clock); |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 220 | |
| 221 | /* calculate the delta since the last update_wall_time: */ |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 222 | delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask); |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 223 | |
John Stultz | f2a5a08 | 2012-07-13 01:21:55 -0400 | [diff] [blame] | 224 | /* convert delta to nanoseconds. */ |
Thomas Gleixner | 3a97837 | 2014-07-16 21:05:10 +0000 | [diff] [blame] | 225 | nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift); |
John Stultz | f2a5a08 | 2012-07-13 01:21:55 -0400 | [diff] [blame] | 226 | |
Stephen Warren | 7b1f620 | 2012-11-07 17:58:54 -0700 | [diff] [blame] | 227 | /* If arch requires, add in get_arch_timeoffset() */ |
Thomas Gleixner | e06fde3 | 2014-07-16 21:03:50 +0000 | [diff] [blame] | 228 | return nsec + arch_gettimeoffset(); |
Martin Schwidefsky | 2ba2a30 | 2009-08-14 15:47:29 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 231 | /** |
| 232 | * update_fast_timekeeper - Update the fast and NMI safe monotonic timekeeper. |
| 233 | * @tk: The timekeeper from which we take the update |
| 234 | * @tkf: The fast timekeeper to update |
| 235 | * @tbase: The time base for the fast timekeeper (mono/raw) |
| 236 | * |
| 237 | * We want to use this from any context including NMI and tracing / |
| 238 | * instrumenting the timekeeping code itself. |
| 239 | * |
| 240 | * So we handle this differently than the other timekeeping accessor |
| 241 | * functions which retry when the sequence count has changed. The |
| 242 | * update side does: |
| 243 | * |
| 244 | * smp_wmb(); <- Ensure that the last base[1] update is visible |
| 245 | * tkf->seq++; |
| 246 | * smp_wmb(); <- Ensure that the seqcount update is visible |
| 247 | * update(tkf->base[0], tk); |
| 248 | * smp_wmb(); <- Ensure that the base[0] update is visible |
| 249 | * tkf->seq++; |
| 250 | * smp_wmb(); <- Ensure that the seqcount update is visible |
| 251 | * update(tkf->base[1], tk); |
| 252 | * |
| 253 | * The reader side does: |
| 254 | * |
| 255 | * do { |
| 256 | * seq = tkf->seq; |
| 257 | * smp_rmb(); |
| 258 | * idx = seq & 0x01; |
| 259 | * now = now(tkf->base[idx]); |
| 260 | * smp_rmb(); |
| 261 | * } while (seq != tkf->seq) |
| 262 | * |
| 263 | * As long as we update base[0] readers are forced off to |
| 264 | * base[1]. Once base[0] is updated readers are redirected to base[0] |
| 265 | * and the base[1] update takes place. |
| 266 | * |
| 267 | * So if a NMI hits the update of base[0] then it will use base[1] |
| 268 | * which is still consistent. In the worst case this can result is a |
| 269 | * slightly wrong timestamp (a few nanoseconds). See |
| 270 | * @ktime_get_mono_fast_ns. |
| 271 | */ |
| 272 | static void update_fast_timekeeper(struct timekeeper *tk) |
| 273 | { |
| 274 | struct tk_read_base *base = tk_fast_mono.base; |
| 275 | |
| 276 | /* Force readers off to base[1] */ |
| 277 | raw_write_seqcount_latch(&tk_fast_mono.seq); |
| 278 | |
| 279 | /* Update base[0] */ |
| 280 | memcpy(base, &tk->tkr, sizeof(*base)); |
| 281 | |
| 282 | /* Force readers back to base[0] */ |
| 283 | raw_write_seqcount_latch(&tk_fast_mono.seq); |
| 284 | |
| 285 | /* Update base[1] */ |
| 286 | memcpy(base + 1, base, sizeof(*base)); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * ktime_get_mono_fast_ns - Fast NMI safe access to clock monotonic |
| 291 | * |
| 292 | * This timestamp is not guaranteed to be monotonic across an update. |
| 293 | * The timestamp is calculated by: |
| 294 | * |
| 295 | * now = base_mono + clock_delta * slope |
| 296 | * |
| 297 | * So if the update lowers the slope, readers who are forced to the |
| 298 | * not yet updated second array are still using the old steeper slope. |
| 299 | * |
| 300 | * tmono |
| 301 | * ^ |
| 302 | * | o n |
| 303 | * | o n |
| 304 | * | u |
| 305 | * | o |
| 306 | * |o |
| 307 | * |12345678---> reader order |
| 308 | * |
| 309 | * o = old slope |
| 310 | * u = update |
| 311 | * n = new slope |
| 312 | * |
| 313 | * So reader 6 will observe time going backwards versus reader 5. |
| 314 | * |
| 315 | * While other CPUs are likely to be able observe that, the only way |
| 316 | * for a CPU local observation is when an NMI hits in the middle of |
| 317 | * the update. Timestamps taken from that NMI context might be ahead |
| 318 | * of the following timestamps. Callers need to be aware of that and |
| 319 | * deal with it. |
| 320 | */ |
| 321 | u64 notrace ktime_get_mono_fast_ns(void) |
| 322 | { |
| 323 | struct tk_read_base *tkr; |
| 324 | unsigned int seq; |
| 325 | u64 now; |
| 326 | |
| 327 | do { |
| 328 | seq = raw_read_seqcount(&tk_fast_mono.seq); |
| 329 | tkr = tk_fast_mono.base + (seq & 0x01); |
| 330 | now = ktime_to_ns(tkr->base_mono) + timekeeping_get_ns(tkr); |
| 331 | |
| 332 | } while (read_seqcount_retry(&tk_fast_mono.seq, seq)); |
| 333 | return now; |
| 334 | } |
| 335 | EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns); |
| 336 | |
Thomas Gleixner | c905fae | 2014-07-16 21:04:05 +0000 | [diff] [blame] | 337 | #ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD |
| 338 | |
| 339 | static inline void update_vsyscall(struct timekeeper *tk) |
| 340 | { |
John Stultz | 0680eb1 | 2014-08-13 12:47:14 -0700 | [diff] [blame] | 341 | struct timespec xt, wm; |
Thomas Gleixner | c905fae | 2014-07-16 21:04:05 +0000 | [diff] [blame] | 342 | |
John Stultz | e2dff1e | 2014-07-23 14:35:39 -0700 | [diff] [blame] | 343 | xt = timespec64_to_timespec(tk_xtime(tk)); |
John Stultz | 0680eb1 | 2014-08-13 12:47:14 -0700 | [diff] [blame] | 344 | wm = timespec64_to_timespec(tk->wall_to_monotonic); |
| 345 | update_vsyscall_old(&xt, &wm, tk->tkr.clock, tk->tkr.mult, |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 346 | tk->tkr.cycle_last); |
Thomas Gleixner | c905fae | 2014-07-16 21:04:05 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static inline void old_vsyscall_fixup(struct timekeeper *tk) |
| 350 | { |
| 351 | s64 remainder; |
| 352 | |
| 353 | /* |
| 354 | * Store only full nanoseconds into xtime_nsec after rounding |
| 355 | * it up and add the remainder to the error difference. |
| 356 | * XXX - This is necessary to avoid small 1ns inconsistnecies caused |
| 357 | * by truncating the remainder in vsyscalls. However, it causes |
| 358 | * additional work to be done in timekeeping_adjust(). Once |
| 359 | * the vsyscall implementations are converted to use xtime_nsec |
| 360 | * (shifted nanoseconds), and CONFIG_GENERIC_TIME_VSYSCALL_OLD |
| 361 | * users are removed, this can be killed. |
| 362 | */ |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 363 | remainder = tk->tkr.xtime_nsec & ((1ULL << tk->tkr.shift) - 1); |
| 364 | tk->tkr.xtime_nsec -= remainder; |
| 365 | tk->tkr.xtime_nsec += 1ULL << tk->tkr.shift; |
Thomas Gleixner | c905fae | 2014-07-16 21:04:05 +0000 | [diff] [blame] | 366 | tk->ntp_error += remainder << tk->ntp_error_shift; |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 367 | tk->ntp_error -= (1ULL << tk->tkr.shift) << tk->ntp_error_shift; |
Thomas Gleixner | c905fae | 2014-07-16 21:04:05 +0000 | [diff] [blame] | 368 | } |
| 369 | #else |
| 370 | #define old_vsyscall_fixup(tk) |
| 371 | #endif |
| 372 | |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 373 | static RAW_NOTIFIER_HEAD(pvclock_gtod_chain); |
| 374 | |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 375 | static void update_pvclock_gtod(struct timekeeper *tk, bool was_set) |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 376 | { |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 377 | raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk); |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /** |
| 381 | * pvclock_gtod_register_notifier - register a pvclock timedata update listener |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 382 | */ |
| 383 | int pvclock_gtod_register_notifier(struct notifier_block *nb) |
| 384 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 385 | struct timekeeper *tk = &tk_core.timekeeper; |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 386 | unsigned long flags; |
| 387 | int ret; |
| 388 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 389 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 390 | ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb); |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 391 | update_pvclock_gtod(tk, true); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 392 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 393 | |
| 394 | return ret; |
| 395 | } |
| 396 | EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier); |
| 397 | |
| 398 | /** |
| 399 | * pvclock_gtod_unregister_notifier - unregister a pvclock |
| 400 | * timedata update listener |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 401 | */ |
| 402 | int pvclock_gtod_unregister_notifier(struct notifier_block *nb) |
| 403 | { |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 404 | unsigned long flags; |
| 405 | int ret; |
| 406 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 407 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 408 | ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 409 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
Marcelo Tosatti | e0b306f | 2012-11-27 23:28:59 -0200 | [diff] [blame] | 410 | |
| 411 | return ret; |
| 412 | } |
| 413 | EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier); |
| 414 | |
Thomas Gleixner | 7c032df | 2014-07-16 21:04:10 +0000 | [diff] [blame] | 415 | /* |
| 416 | * Update the ktime_t based scalar nsec members of the timekeeper |
| 417 | */ |
| 418 | static inline void tk_update_ktime_data(struct timekeeper *tk) |
| 419 | { |
Heena Sirwani | 9e3680b | 2014-10-29 16:01:16 +0530 | [diff] [blame^] | 420 | u64 seconds; |
| 421 | u32 nsec; |
Thomas Gleixner | 7c032df | 2014-07-16 21:04:10 +0000 | [diff] [blame] | 422 | |
| 423 | /* |
| 424 | * The xtime based monotonic readout is: |
| 425 | * nsec = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec + now(); |
| 426 | * The ktime based monotonic readout is: |
| 427 | * nsec = base_mono + now(); |
| 428 | * ==> base_mono = (xtime_sec + wtm_sec) * 1e9 + wtm_nsec |
| 429 | */ |
Heena Sirwani | 9e3680b | 2014-10-29 16:01:16 +0530 | [diff] [blame^] | 430 | seconds = (u64)(tk->xtime_sec + tk->wall_to_monotonic.tv_sec); |
| 431 | nsec = (u32) tk->wall_to_monotonic.tv_nsec; |
| 432 | tk->tkr.base_mono = ns_to_ktime(seconds * NSEC_PER_SEC + nsec); |
Thomas Gleixner | f519b1a | 2014-07-16 21:05:04 +0000 | [diff] [blame] | 433 | |
| 434 | /* Update the monotonic raw base */ |
| 435 | tk->base_raw = timespec64_to_ktime(tk->raw_time); |
Heena Sirwani | 9e3680b | 2014-10-29 16:01:16 +0530 | [diff] [blame^] | 436 | |
| 437 | /* |
| 438 | * The sum of the nanoseconds portions of xtime and |
| 439 | * wall_to_monotonic can be greater/equal one second. Take |
| 440 | * this into account before updating tk->ktime_sec. |
| 441 | */ |
| 442 | nsec += (u32)(tk->tkr.xtime_nsec >> tk->tkr.shift); |
| 443 | if (nsec >= NSEC_PER_SEC) |
| 444 | seconds++; |
| 445 | tk->ktime_sec = seconds; |
Thomas Gleixner | 7c032df | 2014-07-16 21:04:10 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 448 | /* must hold timekeeper_lock */ |
David Vrabel | 04397fe9 | 2013-06-27 11:35:45 +0100 | [diff] [blame] | 449 | static void timekeeping_update(struct timekeeper *tk, unsigned int action) |
Thomas Gleixner | cc06268 | 2011-11-13 23:19:49 +0000 | [diff] [blame] | 450 | { |
David Vrabel | 04397fe9 | 2013-06-27 11:35:45 +0100 | [diff] [blame] | 451 | if (action & TK_CLEAR_NTP) { |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 452 | tk->ntp_error = 0; |
Thomas Gleixner | cc06268 | 2011-11-13 23:19:49 +0000 | [diff] [blame] | 453 | ntp_clear(); |
| 454 | } |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 455 | |
Thomas Gleixner | 7c032df | 2014-07-16 21:04:10 +0000 | [diff] [blame] | 456 | tk_update_ktime_data(tk); |
| 457 | |
Thomas Gleixner | 9bf2419 | 2014-09-06 12:24:49 +0200 | [diff] [blame] | 458 | update_vsyscall(tk); |
| 459 | update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET); |
| 460 | |
David Vrabel | 04397fe9 | 2013-06-27 11:35:45 +0100 | [diff] [blame] | 461 | if (action & TK_MIRROR) |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 462 | memcpy(&shadow_timekeeper, &tk_core.timekeeper, |
| 463 | sizeof(tk_core.timekeeper)); |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 464 | |
| 465 | update_fast_timekeeper(tk); |
Thomas Gleixner | cc06268 | 2011-11-13 23:19:49 +0000 | [diff] [blame] | 466 | } |
| 467 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 468 | /** |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 469 | * timekeeping_forward_now - update clock to the current time |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 470 | * |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 471 | * Forward the current clock to update its state since the last call to |
| 472 | * update_wall_time(). This is useful before significant clock changes, |
| 473 | * as it avoids having to deal with this time offset explicitly. |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 474 | */ |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 475 | static void timekeeping_forward_now(struct timekeeper *tk) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 476 | { |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 477 | struct clocksource *clock = tk->tkr.clock; |
Thomas Gleixner | 3a97837 | 2014-07-16 21:05:10 +0000 | [diff] [blame] | 478 | cycle_t cycle_now, delta; |
Roman Zippel | 9a05511 | 2008-08-20 16:37:28 -0700 | [diff] [blame] | 479 | s64 nsec; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 480 | |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 481 | cycle_now = tk->tkr.read(clock); |
| 482 | delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask); |
| 483 | tk->tkr.cycle_last = cycle_now; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 484 | |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 485 | tk->tkr.xtime_nsec += delta * tk->tkr.mult; |
john stultz | 7d27558 | 2009-05-01 13:10:26 -0700 | [diff] [blame] | 486 | |
Stephen Warren | 7b1f620 | 2012-11-07 17:58:54 -0700 | [diff] [blame] | 487 | /* If arch requires, add in get_arch_timeoffset() */ |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 488 | tk->tkr.xtime_nsec += (u64)arch_gettimeoffset() << tk->tkr.shift; |
john stultz | 7d27558 | 2009-05-01 13:10:26 -0700 | [diff] [blame] | 489 | |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 490 | tk_normalize_xtime(tk); |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 491 | |
Thomas Gleixner | 3a97837 | 2014-07-16 21:05:10 +0000 | [diff] [blame] | 492 | nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift); |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 493 | timespec64_add_ns(&tk->raw_time, nsec); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /** |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 497 | * __getnstimeofday64 - Returns the time of day in a timespec64. |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 498 | * @ts: pointer to the timespec to be set |
| 499 | * |
Kees Cook | 1e817fb | 2012-11-19 10:26:16 -0800 | [diff] [blame] | 500 | * Updates the time of day in the timespec. |
| 501 | * Returns 0 on success, or -ve when suspended (timespec will be undefined). |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 502 | */ |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 503 | int __getnstimeofday64(struct timespec64 *ts) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 504 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 505 | struct timekeeper *tk = &tk_core.timekeeper; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 506 | unsigned long seq; |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 507 | s64 nsecs = 0; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 508 | |
| 509 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 510 | seq = read_seqcount_begin(&tk_core.seq); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 511 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 512 | ts->tv_sec = tk->xtime_sec; |
Thomas Gleixner | 0e5ac3a | 2014-07-16 21:05:18 +0000 | [diff] [blame] | 513 | nsecs = timekeeping_get_ns(&tk->tkr); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 514 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 515 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 516 | |
John Stultz | ec145ba | 2012-09-11 19:26:03 -0400 | [diff] [blame] | 517 | ts->tv_nsec = 0; |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 518 | timespec64_add_ns(ts, nsecs); |
Kees Cook | 1e817fb | 2012-11-19 10:26:16 -0800 | [diff] [blame] | 519 | |
| 520 | /* |
| 521 | * Do not bail out early, in case there were callers still using |
| 522 | * the value, even in the face of the WARN_ON. |
| 523 | */ |
| 524 | if (unlikely(timekeeping_suspended)) |
| 525 | return -EAGAIN; |
| 526 | return 0; |
| 527 | } |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 528 | EXPORT_SYMBOL(__getnstimeofday64); |
Kees Cook | 1e817fb | 2012-11-19 10:26:16 -0800 | [diff] [blame] | 529 | |
| 530 | /** |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 531 | * getnstimeofday64 - Returns the time of day in a timespec64. |
Kees Cook | 1e817fb | 2012-11-19 10:26:16 -0800 | [diff] [blame] | 532 | * @ts: pointer to the timespec to be set |
| 533 | * |
| 534 | * Returns the time of day in a timespec (WARN if suspended). |
| 535 | */ |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 536 | void getnstimeofday64(struct timespec64 *ts) |
Kees Cook | 1e817fb | 2012-11-19 10:26:16 -0800 | [diff] [blame] | 537 | { |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 538 | WARN_ON(__getnstimeofday64(ts)); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 539 | } |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 540 | EXPORT_SYMBOL(getnstimeofday64); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 541 | |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 542 | ktime_t ktime_get(void) |
| 543 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 544 | struct timekeeper *tk = &tk_core.timekeeper; |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 545 | unsigned int seq; |
Thomas Gleixner | a016a5b | 2014-07-16 21:04:12 +0000 | [diff] [blame] | 546 | ktime_t base; |
| 547 | s64 nsecs; |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 548 | |
| 549 | WARN_ON(timekeeping_suspended); |
| 550 | |
| 551 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 552 | seq = read_seqcount_begin(&tk_core.seq); |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 553 | base = tk->tkr.base_mono; |
Thomas Gleixner | 0e5ac3a | 2014-07-16 21:05:18 +0000 | [diff] [blame] | 554 | nsecs = timekeeping_get_ns(&tk->tkr); |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 555 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 556 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
John Stultz | 24e4a8c | 2014-07-16 21:03:53 +0000 | [diff] [blame] | 557 | |
Thomas Gleixner | a016a5b | 2014-07-16 21:04:12 +0000 | [diff] [blame] | 558 | return ktime_add_ns(base, nsecs); |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 559 | } |
| 560 | EXPORT_SYMBOL_GPL(ktime_get); |
| 561 | |
Thomas Gleixner | 0077dc6 | 2014-07-16 21:04:13 +0000 | [diff] [blame] | 562 | static ktime_t *offsets[TK_OFFS_MAX] = { |
| 563 | [TK_OFFS_REAL] = &tk_core.timekeeper.offs_real, |
| 564 | [TK_OFFS_BOOT] = &tk_core.timekeeper.offs_boot, |
| 565 | [TK_OFFS_TAI] = &tk_core.timekeeper.offs_tai, |
| 566 | }; |
| 567 | |
| 568 | ktime_t ktime_get_with_offset(enum tk_offsets offs) |
| 569 | { |
| 570 | struct timekeeper *tk = &tk_core.timekeeper; |
| 571 | unsigned int seq; |
| 572 | ktime_t base, *offset = offsets[offs]; |
| 573 | s64 nsecs; |
| 574 | |
| 575 | WARN_ON(timekeeping_suspended); |
| 576 | |
| 577 | do { |
| 578 | seq = read_seqcount_begin(&tk_core.seq); |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 579 | base = ktime_add(tk->tkr.base_mono, *offset); |
Thomas Gleixner | 0e5ac3a | 2014-07-16 21:05:18 +0000 | [diff] [blame] | 580 | nsecs = timekeeping_get_ns(&tk->tkr); |
Thomas Gleixner | 0077dc6 | 2014-07-16 21:04:13 +0000 | [diff] [blame] | 581 | |
| 582 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
| 583 | |
| 584 | return ktime_add_ns(base, nsecs); |
| 585 | |
| 586 | } |
| 587 | EXPORT_SYMBOL_GPL(ktime_get_with_offset); |
| 588 | |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 589 | /** |
Thomas Gleixner | 9a6b519 | 2014-07-16 21:04:22 +0000 | [diff] [blame] | 590 | * ktime_mono_to_any() - convert mononotic time to any other time |
| 591 | * @tmono: time to convert. |
| 592 | * @offs: which offset to use |
| 593 | */ |
| 594 | ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs) |
| 595 | { |
| 596 | ktime_t *offset = offsets[offs]; |
| 597 | unsigned long seq; |
| 598 | ktime_t tconv; |
| 599 | |
| 600 | do { |
| 601 | seq = read_seqcount_begin(&tk_core.seq); |
| 602 | tconv = ktime_add(tmono, *offset); |
| 603 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
| 604 | |
| 605 | return tconv; |
| 606 | } |
| 607 | EXPORT_SYMBOL_GPL(ktime_mono_to_any); |
| 608 | |
| 609 | /** |
Thomas Gleixner | f519b1a | 2014-07-16 21:05:04 +0000 | [diff] [blame] | 610 | * ktime_get_raw - Returns the raw monotonic time in ktime_t format |
| 611 | */ |
| 612 | ktime_t ktime_get_raw(void) |
| 613 | { |
| 614 | struct timekeeper *tk = &tk_core.timekeeper; |
| 615 | unsigned int seq; |
| 616 | ktime_t base; |
| 617 | s64 nsecs; |
| 618 | |
| 619 | do { |
| 620 | seq = read_seqcount_begin(&tk_core.seq); |
| 621 | base = tk->base_raw; |
| 622 | nsecs = timekeeping_get_ns_raw(tk); |
| 623 | |
| 624 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
| 625 | |
| 626 | return ktime_add_ns(base, nsecs); |
| 627 | } |
| 628 | EXPORT_SYMBOL_GPL(ktime_get_raw); |
| 629 | |
| 630 | /** |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 631 | * ktime_get_ts64 - get the monotonic clock in timespec64 format |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 632 | * @ts: pointer to timespec variable |
| 633 | * |
| 634 | * The function calculates the monotonic clock from the realtime |
| 635 | * clock and the wall_to_monotonic offset and stores the result |
| 636 | * in normalized timespec format in the variable pointed to by @ts. |
| 637 | */ |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 638 | void ktime_get_ts64(struct timespec64 *ts) |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 639 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 640 | struct timekeeper *tk = &tk_core.timekeeper; |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 641 | struct timespec64 tomono; |
John Stultz | ec145ba | 2012-09-11 19:26:03 -0400 | [diff] [blame] | 642 | s64 nsec; |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 643 | unsigned int seq; |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 644 | |
| 645 | WARN_ON(timekeeping_suspended); |
| 646 | |
| 647 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 648 | seq = read_seqcount_begin(&tk_core.seq); |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 649 | ts->tv_sec = tk->xtime_sec; |
Thomas Gleixner | 0e5ac3a | 2014-07-16 21:05:18 +0000 | [diff] [blame] | 650 | nsec = timekeeping_get_ns(&tk->tkr); |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 651 | tomono = tk->wall_to_monotonic; |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 652 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 653 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 654 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 655 | ts->tv_sec += tomono.tv_sec; |
| 656 | ts->tv_nsec = 0; |
| 657 | timespec64_add_ns(ts, nsec + tomono.tv_nsec); |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 658 | } |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 659 | EXPORT_SYMBOL_GPL(ktime_get_ts64); |
Martin Schwidefsky | 951ed4d | 2009-07-07 11:27:28 +0200 | [diff] [blame] | 660 | |
Heena Sirwani | 9e3680b | 2014-10-29 16:01:16 +0530 | [diff] [blame^] | 661 | /** |
| 662 | * ktime_get_seconds - Get the seconds portion of CLOCK_MONOTONIC |
| 663 | * |
| 664 | * Returns the seconds portion of CLOCK_MONOTONIC with a single non |
| 665 | * serialized read. tk->ktime_sec is of type 'unsigned long' so this |
| 666 | * works on both 32 and 64 bit systems. On 32 bit systems the readout |
| 667 | * covers ~136 years of uptime which should be enough to prevent |
| 668 | * premature wrap arounds. |
| 669 | */ |
| 670 | time64_t ktime_get_seconds(void) |
| 671 | { |
| 672 | struct timekeeper *tk = &tk_core.timekeeper; |
| 673 | |
| 674 | WARN_ON(timekeeping_suspended); |
| 675 | return tk->ktime_sec; |
| 676 | } |
| 677 | EXPORT_SYMBOL_GPL(ktime_get_seconds); |
| 678 | |
Alexander Gordeev | e2c18e4 | 2011-01-12 17:00:57 -0800 | [diff] [blame] | 679 | #ifdef CONFIG_NTP_PPS |
| 680 | |
| 681 | /** |
| 682 | * getnstime_raw_and_real - get day and raw monotonic time in timespec format |
| 683 | * @ts_raw: pointer to the timespec to be set to raw monotonic time |
| 684 | * @ts_real: pointer to the timespec to be set to the time of day |
| 685 | * |
| 686 | * This function reads both the time of day and raw monotonic time at the |
| 687 | * same time atomically and stores the resulting timestamps in timespec |
| 688 | * format. |
| 689 | */ |
| 690 | void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real) |
| 691 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 692 | struct timekeeper *tk = &tk_core.timekeeper; |
Alexander Gordeev | e2c18e4 | 2011-01-12 17:00:57 -0800 | [diff] [blame] | 693 | unsigned long seq; |
| 694 | s64 nsecs_raw, nsecs_real; |
| 695 | |
| 696 | WARN_ON_ONCE(timekeeping_suspended); |
| 697 | |
| 698 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 699 | seq = read_seqcount_begin(&tk_core.seq); |
Alexander Gordeev | e2c18e4 | 2011-01-12 17:00:57 -0800 | [diff] [blame] | 700 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 701 | *ts_raw = timespec64_to_timespec(tk->raw_time); |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 702 | ts_real->tv_sec = tk->xtime_sec; |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 703 | ts_real->tv_nsec = 0; |
Alexander Gordeev | e2c18e4 | 2011-01-12 17:00:57 -0800 | [diff] [blame] | 704 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 705 | nsecs_raw = timekeeping_get_ns_raw(tk); |
Thomas Gleixner | 0e5ac3a | 2014-07-16 21:05:18 +0000 | [diff] [blame] | 706 | nsecs_real = timekeeping_get_ns(&tk->tkr); |
Alexander Gordeev | e2c18e4 | 2011-01-12 17:00:57 -0800 | [diff] [blame] | 707 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 708 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
Alexander Gordeev | e2c18e4 | 2011-01-12 17:00:57 -0800 | [diff] [blame] | 709 | |
| 710 | timespec_add_ns(ts_raw, nsecs_raw); |
| 711 | timespec_add_ns(ts_real, nsecs_real); |
| 712 | } |
| 713 | EXPORT_SYMBOL(getnstime_raw_and_real); |
| 714 | |
| 715 | #endif /* CONFIG_NTP_PPS */ |
| 716 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 717 | /** |
| 718 | * do_gettimeofday - Returns the time of day in a timeval |
| 719 | * @tv: pointer to the timeval to be set |
| 720 | * |
Geert Uytterhoeven | efd9ac8 | 2008-01-30 13:30:01 +0100 | [diff] [blame] | 721 | * NOTE: Users should be converted to using getnstimeofday() |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 722 | */ |
| 723 | void do_gettimeofday(struct timeval *tv) |
| 724 | { |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 725 | struct timespec64 now; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 726 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 727 | getnstimeofday64(&now); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 728 | tv->tv_sec = now.tv_sec; |
| 729 | tv->tv_usec = now.tv_nsec/1000; |
| 730 | } |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 731 | EXPORT_SYMBOL(do_gettimeofday); |
Richard Cochran | d239f49 | 2012-04-27 10:12:42 +0200 | [diff] [blame] | 732 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 733 | /** |
| 734 | * do_settimeofday - Sets the time of day |
| 735 | * @tv: pointer to the timespec variable containing the new time |
| 736 | * |
| 737 | * Sets the time of day to the new time and update NTP and notify hrtimers |
| 738 | */ |
Richard Cochran | 1e6d767 | 2011-02-01 13:50:58 +0000 | [diff] [blame] | 739 | int do_settimeofday(const struct timespec *tv) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 740 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 741 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 742 | struct timespec64 ts_delta, xt, tmp; |
John Stultz | 92c1d3e | 2011-11-14 14:05:44 -0800 | [diff] [blame] | 743 | unsigned long flags; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 744 | |
John Stultz | cee5848 | 2012-08-31 13:30:06 -0400 | [diff] [blame] | 745 | if (!timespec_valid_strict(tv)) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 746 | return -EINVAL; |
| 747 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 748 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 749 | write_seqcount_begin(&tk_core.seq); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 750 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 751 | timekeeping_forward_now(tk); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 752 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 753 | xt = tk_xtime(tk); |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 754 | ts_delta.tv_sec = tv->tv_sec - xt.tv_sec; |
| 755 | ts_delta.tv_nsec = tv->tv_nsec - xt.tv_nsec; |
| 756 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 757 | 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] | 758 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 759 | tmp = timespec_to_timespec64(*tv); |
| 760 | tk_set_xtime(tk, &tmp); |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 761 | |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 762 | timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 763 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 764 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 765 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 766 | |
| 767 | /* signal hrtimers about time change */ |
| 768 | clock_was_set(); |
| 769 | |
| 770 | return 0; |
| 771 | } |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 772 | EXPORT_SYMBOL(do_settimeofday); |
| 773 | |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 774 | /** |
| 775 | * timekeeping_inject_offset - Adds or subtracts from the current time. |
| 776 | * @tv: pointer to the timespec variable containing the offset |
| 777 | * |
| 778 | * Adds or subtracts an offset value from the current time. |
| 779 | */ |
| 780 | int timekeeping_inject_offset(struct timespec *ts) |
| 781 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 782 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 92c1d3e | 2011-11-14 14:05:44 -0800 | [diff] [blame] | 783 | unsigned long flags; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 784 | struct timespec64 ts64, tmp; |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 785 | int ret = 0; |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 786 | |
| 787 | if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) |
| 788 | return -EINVAL; |
| 789 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 790 | ts64 = timespec_to_timespec64(*ts); |
| 791 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 792 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 793 | write_seqcount_begin(&tk_core.seq); |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 794 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 795 | timekeeping_forward_now(tk); |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 796 | |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 797 | /* Make sure the proposed value is valid */ |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 798 | tmp = timespec64_add(tk_xtime(tk), ts64); |
| 799 | if (!timespec64_valid_strict(&tmp)) { |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 800 | ret = -EINVAL; |
| 801 | goto error; |
| 802 | } |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 803 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 804 | tk_xtime_add(tk, &ts64); |
| 805 | 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] | 806 | |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 807 | 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] | 808 | timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 809 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 810 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 811 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 812 | |
| 813 | /* signal hrtimers about time change */ |
| 814 | clock_was_set(); |
| 815 | |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 816 | return ret; |
John Stultz | c528f7c | 2011-02-01 13:52:17 +0000 | [diff] [blame] | 817 | } |
| 818 | EXPORT_SYMBOL(timekeeping_inject_offset); |
| 819 | |
John Stultz | cc244dd | 2012-05-03 12:30:07 -0700 | [diff] [blame] | 820 | |
| 821 | /** |
| 822 | * timekeeping_get_tai_offset - Returns current TAI offset from UTC |
| 823 | * |
| 824 | */ |
| 825 | s32 timekeeping_get_tai_offset(void) |
| 826 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 827 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | cc244dd | 2012-05-03 12:30:07 -0700 | [diff] [blame] | 828 | unsigned int seq; |
| 829 | s32 ret; |
| 830 | |
| 831 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 832 | seq = read_seqcount_begin(&tk_core.seq); |
John Stultz | cc244dd | 2012-05-03 12:30:07 -0700 | [diff] [blame] | 833 | ret = tk->tai_offset; |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 834 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
John Stultz | cc244dd | 2012-05-03 12:30:07 -0700 | [diff] [blame] | 835 | |
| 836 | return ret; |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * __timekeeping_set_tai_offset - Lock free worker function |
| 841 | * |
| 842 | */ |
Fengguang Wu | dd5d70e8 | 2013-03-25 12:24:24 -0700 | [diff] [blame] | 843 | static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset) |
John Stultz | cc244dd | 2012-05-03 12:30:07 -0700 | [diff] [blame] | 844 | { |
| 845 | tk->tai_offset = tai_offset; |
John Stultz | 04005f6 | 2013-12-10 17:13:35 -0800 | [diff] [blame] | 846 | 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] | 847 | } |
| 848 | |
| 849 | /** |
| 850 | * timekeeping_set_tai_offset - Sets the current TAI offset from UTC |
| 851 | * |
| 852 | */ |
| 853 | void timekeeping_set_tai_offset(s32 tai_offset) |
| 854 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 855 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | cc244dd | 2012-05-03 12:30:07 -0700 | [diff] [blame] | 856 | unsigned long flags; |
| 857 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 858 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 859 | write_seqcount_begin(&tk_core.seq); |
John Stultz | cc244dd | 2012-05-03 12:30:07 -0700 | [diff] [blame] | 860 | __timekeeping_set_tai_offset(tk, tai_offset); |
John Stultz | f55c076 | 2013-12-11 18:50:25 -0800 | [diff] [blame] | 861 | timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 862 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 863 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
John Stultz | 4e8f8b3 | 2013-04-10 12:41:49 -0700 | [diff] [blame] | 864 | clock_was_set(); |
John Stultz | cc244dd | 2012-05-03 12:30:07 -0700 | [diff] [blame] | 865 | } |
| 866 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 867 | /** |
| 868 | * change_clocksource - Swaps clocksources if a new one is available |
| 869 | * |
| 870 | * Accumulates current time interval and initializes new clocksource |
| 871 | */ |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 872 | static int change_clocksource(void *data) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 873 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 874 | struct timekeeper *tk = &tk_core.timekeeper; |
Magnus Damm | 4614e6a | 2009-04-21 12:24:02 -0700 | [diff] [blame] | 875 | struct clocksource *new, *old; |
John Stultz | f695cf9 | 2012-03-14 16:38:15 -0700 | [diff] [blame] | 876 | unsigned long flags; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 877 | |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 878 | new = (struct clocksource *) data; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 879 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 880 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 881 | write_seqcount_begin(&tk_core.seq); |
John Stultz | f695cf9 | 2012-03-14 16:38:15 -0700 | [diff] [blame] | 882 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 883 | timekeeping_forward_now(tk); |
Thomas Gleixner | 09ac369 | 2013-04-25 20:31:44 +0000 | [diff] [blame] | 884 | /* |
| 885 | * If the cs is in module, get a module reference. Succeeds |
| 886 | * for built-in code (owner == NULL) as well. |
| 887 | */ |
| 888 | if (try_module_get(new->owner)) { |
| 889 | if (!new->enable || new->enable(new) == 0) { |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 890 | old = tk->tkr.clock; |
Thomas Gleixner | 09ac369 | 2013-04-25 20:31:44 +0000 | [diff] [blame] | 891 | tk_setup_internals(tk, new); |
| 892 | if (old->disable) |
| 893 | old->disable(old); |
| 894 | module_put(old->owner); |
| 895 | } else { |
| 896 | module_put(new->owner); |
| 897 | } |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 898 | } |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 899 | timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
John Stultz | f695cf9 | 2012-03-14 16:38:15 -0700 | [diff] [blame] | 900 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 901 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 902 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
John Stultz | f695cf9 | 2012-03-14 16:38:15 -0700 | [diff] [blame] | 903 | |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 904 | return 0; |
| 905 | } |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 906 | |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 907 | /** |
| 908 | * timekeeping_notify - Install a new clock source |
| 909 | * @clock: pointer to the clock source |
| 910 | * |
| 911 | * This function is called from clocksource.c after a new, better clock |
| 912 | * source has been registered. The caller holds the clocksource_mutex. |
| 913 | */ |
Thomas Gleixner | ba919d1 | 2013-04-25 20:31:44 +0000 | [diff] [blame] | 914 | int timekeeping_notify(struct clocksource *clock) |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 915 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 916 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 917 | |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 918 | if (tk->tkr.clock == clock) |
Thomas Gleixner | ba919d1 | 2013-04-25 20:31:44 +0000 | [diff] [blame] | 919 | return 0; |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 920 | stop_machine(change_clocksource, clock, NULL); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 921 | tick_clock_notify(); |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 922 | return tk->tkr.clock == clock ? 0 : -1; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 923 | } |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 924 | |
Thomas Gleixner | a40f262 | 2009-07-07 13:00:31 +0200 | [diff] [blame] | 925 | /** |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 926 | * getrawmonotonic - Returns the raw monotonic time in a timespec |
| 927 | * @ts: pointer to the timespec to be set |
| 928 | * |
| 929 | * Returns the raw monotonic time (completely un-modified by ntp) |
| 930 | */ |
| 931 | void getrawmonotonic(struct timespec *ts) |
| 932 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 933 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 934 | struct timespec64 ts64; |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 935 | unsigned long seq; |
| 936 | s64 nsecs; |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 937 | |
| 938 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 939 | seq = read_seqcount_begin(&tk_core.seq); |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 940 | nsecs = timekeeping_get_ns_raw(tk); |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 941 | ts64 = tk->raw_time; |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 942 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 943 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 944 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 945 | timespec64_add_ns(&ts64, nsecs); |
| 946 | *ts = timespec64_to_timespec(ts64); |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 947 | } |
| 948 | EXPORT_SYMBOL(getrawmonotonic); |
| 949 | |
John Stultz | 2d42244 | 2008-08-20 16:37:30 -0700 | [diff] [blame] | 950 | /** |
Li Zefan | cf4fc6c | 2008-02-08 04:19:24 -0800 | [diff] [blame] | 951 | * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 952 | */ |
Li Zefan | cf4fc6c | 2008-02-08 04:19:24 -0800 | [diff] [blame] | 953 | int timekeeping_valid_for_hres(void) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 954 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 955 | struct timekeeper *tk = &tk_core.timekeeper; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 956 | unsigned long seq; |
| 957 | int ret; |
| 958 | |
| 959 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 960 | seq = read_seqcount_begin(&tk_core.seq); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 961 | |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 962 | ret = tk->tkr.clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 963 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 964 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 965 | |
| 966 | return ret; |
| 967 | } |
| 968 | |
| 969 | /** |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 970 | * timekeeping_max_deferment - Returns max time the clocksource can be deferred |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 971 | */ |
| 972 | u64 timekeeping_max_deferment(void) |
| 973 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 974 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 975 | unsigned long seq; |
| 976 | u64 ret; |
John Stultz | 42e71e8 | 2012-07-13 01:21:51 -0400 | [diff] [blame] | 977 | |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 978 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 979 | seq = read_seqcount_begin(&tk_core.seq); |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 980 | |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 981 | ret = tk->tkr.clock->max_idle_ns; |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 982 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 983 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 984 | |
| 985 | return ret; |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | /** |
Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 989 | * read_persistent_clock - Return time from the persistent clock. |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 990 | * |
| 991 | * Weak dummy function for arches that do not yet support it. |
Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 992 | * Reads the time from the battery backed persistent clock. |
| 993 | * 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] | 994 | * |
| 995 | * XXX - Do be sure to remove it once all arches implement it. |
| 996 | */ |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 997 | void __weak read_persistent_clock(struct timespec *ts) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 998 | { |
Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 999 | ts->tv_sec = 0; |
| 1000 | ts->tv_nsec = 0; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Martin Schwidefsky | 23970e3 | 2009-08-14 15:47:32 +0200 | [diff] [blame] | 1003 | /** |
| 1004 | * read_boot_clock - Return time of the system start. |
| 1005 | * |
| 1006 | * Weak dummy function for arches that do not yet support it. |
| 1007 | * Function to read the exact time the system has been started. |
| 1008 | * Returns a timespec with tv_sec=0 and tv_nsec=0 if unsupported. |
| 1009 | * |
| 1010 | * XXX - Do be sure to remove it once all arches implement it. |
| 1011 | */ |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 1012 | void __weak read_boot_clock(struct timespec *ts) |
Martin Schwidefsky | 23970e3 | 2009-08-14 15:47:32 +0200 | [diff] [blame] | 1013 | { |
| 1014 | ts->tv_sec = 0; |
| 1015 | ts->tv_nsec = 0; |
| 1016 | } |
| 1017 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1018 | /* |
| 1019 | * timekeeping_init - Initializes the clocksource and common timekeeping values |
| 1020 | */ |
| 1021 | void __init timekeeping_init(void) |
| 1022 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1023 | struct timekeeper *tk = &tk_core.timekeeper; |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 1024 | struct clocksource *clock; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1025 | unsigned long flags; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1026 | struct timespec64 now, boot, tmp; |
| 1027 | struct timespec ts; |
Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 1028 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1029 | read_persistent_clock(&ts); |
| 1030 | now = timespec_to_timespec64(ts); |
| 1031 | if (!timespec64_valid_strict(&now)) { |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 1032 | pr_warn("WARNING: Persistent clock returned invalid value!\n" |
| 1033 | " Check your CMOS/BIOS settings.\n"); |
| 1034 | now.tv_sec = 0; |
| 1035 | now.tv_nsec = 0; |
Feng Tang | 31ade30 | 2013-01-16 00:09:47 +0800 | [diff] [blame] | 1036 | } else if (now.tv_sec || now.tv_nsec) |
| 1037 | persistent_clock_exist = true; |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 1038 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1039 | read_boot_clock(&ts); |
| 1040 | boot = timespec_to_timespec64(ts); |
| 1041 | if (!timespec64_valid_strict(&boot)) { |
John Stultz | 4e8b145 | 2012-08-08 15:36:20 -0400 | [diff] [blame] | 1042 | pr_warn("WARNING: Boot clock returned invalid value!\n" |
| 1043 | " Check your CMOS/BIOS settings.\n"); |
| 1044 | boot.tv_sec = 0; |
| 1045 | boot.tv_nsec = 0; |
| 1046 | } |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1047 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1048 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1049 | write_seqcount_begin(&tk_core.seq); |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 1050 | ntp_init(); |
| 1051 | |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 1052 | clock = clocksource_default_clock(); |
Martin Schwidefsky | a0f7d48 | 2009-08-14 15:47:19 +0200 | [diff] [blame] | 1053 | if (clock->enable) |
| 1054 | clock->enable(clock); |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1055 | tk_setup_internals(tk, clock); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1056 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1057 | tk_set_xtime(tk, &now); |
| 1058 | tk->raw_time.tv_sec = 0; |
| 1059 | tk->raw_time.tv_nsec = 0; |
Thomas Gleixner | f519b1a | 2014-07-16 21:05:04 +0000 | [diff] [blame] | 1060 | tk->base_raw.tv64 = 0; |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 1061 | if (boot.tv_sec == 0 && boot.tv_nsec == 0) |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1062 | boot = tk_xtime(tk); |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 1063 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1064 | set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec); |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1065 | tk_set_wall_to_mono(tk, tmp); |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 1066 | |
Thomas Gleixner | f111adf | 2014-07-16 21:04:09 +0000 | [diff] [blame] | 1067 | timekeeping_update(tk, TK_MIRROR); |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 1068 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1069 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1070 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1073 | /* time in seconds when suspend began */ |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1074 | static struct timespec64 timekeeping_suspend_time; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1075 | |
| 1076 | /** |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1077 | * __timekeeping_inject_sleeptime - Internal function to add sleep interval |
| 1078 | * @delta: pointer to a timespec delta value |
| 1079 | * |
| 1080 | * Takes a timespec offset measuring a suspend interval and properly |
| 1081 | * adds the sleep offset to the timekeeping variables. |
| 1082 | */ |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1083 | static void __timekeeping_inject_sleeptime(struct timekeeper *tk, |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1084 | struct timespec64 *delta) |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1085 | { |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1086 | if (!timespec64_valid_strict(delta)) { |
John Stultz | 6d9bcb6 | 2014-06-04 16:11:43 -0700 | [diff] [blame] | 1087 | printk_deferred(KERN_WARNING |
| 1088 | "__timekeeping_inject_sleeptime: Invalid " |
| 1089 | "sleep delta value!\n"); |
John Stultz | cb5de2f8d | 2011-06-01 18:18:09 -0700 | [diff] [blame] | 1090 | return; |
| 1091 | } |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1092 | tk_xtime_add(tk, delta); |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1093 | 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] | 1094 | tk_update_sleep_time(tk, timespec64_to_ktime(*delta)); |
Colin Cross | 5c83545 | 2013-05-21 22:32:14 -0700 | [diff] [blame] | 1095 | tk_debug_account_sleep_time(delta); |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1098 | /** |
| 1099 | * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values |
| 1100 | * @delta: pointer to a timespec delta value |
| 1101 | * |
| 1102 | * This hook is for architectures that cannot support read_persistent_clock |
| 1103 | * because their RTC/persistent clock is only accessible when irqs are enabled. |
| 1104 | * |
| 1105 | * This function should only be called by rtc_resume(), and allows |
| 1106 | * a suspend offset to be injected into the timekeeping values. |
| 1107 | */ |
| 1108 | void timekeeping_inject_sleeptime(struct timespec *delta) |
| 1109 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1110 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1111 | struct timespec64 tmp; |
John Stultz | 92c1d3e | 2011-11-14 14:05:44 -0800 | [diff] [blame] | 1112 | unsigned long flags; |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1113 | |
Feng Tang | 31ade30 | 2013-01-16 00:09:47 +0800 | [diff] [blame] | 1114 | /* |
| 1115 | * Make sure we don't set the clock twice, as timekeeping_resume() |
| 1116 | * already did it |
| 1117 | */ |
| 1118 | if (has_persistent_clock()) |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1119 | return; |
| 1120 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1121 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1122 | write_seqcount_begin(&tk_core.seq); |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 1123 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1124 | timekeeping_forward_now(tk); |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1125 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1126 | tmp = timespec_to_timespec64(*delta); |
| 1127 | __timekeeping_inject_sleeptime(tk, &tmp); |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1128 | |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 1129 | timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1130 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1131 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1132 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1133 | |
| 1134 | /* signal hrtimers about time change */ |
| 1135 | clock_was_set(); |
| 1136 | } |
| 1137 | |
John Stultz | 304529b | 2011-04-01 14:32:09 -0700 | [diff] [blame] | 1138 | /** |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1139 | * timekeeping_resume - Resumes the generic timekeeping subsystem. |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1140 | * |
| 1141 | * This is for the generic clocksource timekeeping. |
| 1142 | * xtime/wall_to_monotonic/jiffies/etc are |
| 1143 | * still managed by arch specific suspend/resume code. |
| 1144 | */ |
Rafael J. Wysocki | e1a85b2 | 2011-03-23 22:16:04 +0100 | [diff] [blame] | 1145 | static void timekeeping_resume(void) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1146 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1147 | struct timekeeper *tk = &tk_core.timekeeper; |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1148 | struct clocksource *clock = tk->tkr.clock; |
John Stultz | 92c1d3e | 2011-11-14 14:05:44 -0800 | [diff] [blame] | 1149 | unsigned long flags; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1150 | struct timespec64 ts_new, ts_delta; |
| 1151 | struct timespec tmp; |
Feng Tang | e445cf1 | 2013-03-12 11:56:48 +0800 | [diff] [blame] | 1152 | cycle_t cycle_now, cycle_delta; |
| 1153 | bool suspendtime_found = false; |
Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 1154 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1155 | read_persistent_clock(&tmp); |
| 1156 | ts_new = timespec_to_timespec64(tmp); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1157 | |
Rafael J. Wysocki | adc78e6 | 2012-08-06 01:40:41 +0200 | [diff] [blame] | 1158 | clockevents_resume(); |
Thomas Gleixner | d10ff3f | 2007-05-14 11:10:02 +0200 | [diff] [blame] | 1159 | clocksource_resume(); |
| 1160 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1161 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1162 | write_seqcount_begin(&tk_core.seq); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1163 | |
Feng Tang | e445cf1 | 2013-03-12 11:56:48 +0800 | [diff] [blame] | 1164 | /* |
| 1165 | * After system resumes, we need to calculate the suspended time and |
| 1166 | * compensate it for the OS time. There are 3 sources that could be |
| 1167 | * used: Nonstop clocksource during suspend, persistent clock and rtc |
| 1168 | * device. |
| 1169 | * |
| 1170 | * One specific platform may have 1 or 2 or all of them, and the |
| 1171 | * preference will be: |
| 1172 | * suspend-nonstop clocksource -> persistent clock -> rtc |
| 1173 | * The less preferred source will only be tried if there is no better |
| 1174 | * usable source. The rtc part is handled separately in rtc core code. |
| 1175 | */ |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1176 | cycle_now = tk->tkr.read(clock); |
Feng Tang | e445cf1 | 2013-03-12 11:56:48 +0800 | [diff] [blame] | 1177 | if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) && |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1178 | cycle_now > tk->tkr.cycle_last) { |
Feng Tang | e445cf1 | 2013-03-12 11:56:48 +0800 | [diff] [blame] | 1179 | u64 num, max = ULLONG_MAX; |
| 1180 | u32 mult = clock->mult; |
| 1181 | u32 shift = clock->shift; |
| 1182 | s64 nsec = 0; |
| 1183 | |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1184 | cycle_delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, |
| 1185 | tk->tkr.mask); |
Feng Tang | e445cf1 | 2013-03-12 11:56:48 +0800 | [diff] [blame] | 1186 | |
| 1187 | /* |
| 1188 | * "cycle_delta * mutl" may cause 64 bits overflow, if the |
| 1189 | * suspended time is too long. In that case we need do the |
| 1190 | * 64 bits math carefully |
| 1191 | */ |
| 1192 | do_div(max, mult); |
| 1193 | if (cycle_delta > max) { |
| 1194 | num = div64_u64(cycle_delta, max); |
| 1195 | nsec = (((u64) max * mult) >> shift) * num; |
| 1196 | cycle_delta -= num * max; |
| 1197 | } |
| 1198 | nsec += ((u64) cycle_delta * mult) >> shift; |
| 1199 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1200 | ts_delta = ns_to_timespec64(nsec); |
Feng Tang | e445cf1 | 2013-03-12 11:56:48 +0800 | [diff] [blame] | 1201 | suspendtime_found = true; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1202 | } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) { |
| 1203 | ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time); |
Feng Tang | e445cf1 | 2013-03-12 11:56:48 +0800 | [diff] [blame] | 1204 | suspendtime_found = true; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1205 | } |
Feng Tang | e445cf1 | 2013-03-12 11:56:48 +0800 | [diff] [blame] | 1206 | |
| 1207 | if (suspendtime_found) |
| 1208 | __timekeeping_inject_sleeptime(tk, &ts_delta); |
| 1209 | |
| 1210 | /* Re-base the last cycle value */ |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1211 | tk->tkr.cycle_last = cycle_now; |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1212 | tk->ntp_error = 0; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1213 | timekeeping_suspended = 0; |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 1214 | timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1215 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1216 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1217 | |
| 1218 | touch_softlockup_watchdog(); |
| 1219 | |
| 1220 | clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL); |
| 1221 | |
| 1222 | /* Resume hrtimers */ |
Thomas Gleixner | b12a03c | 2011-05-02 16:48:57 +0200 | [diff] [blame] | 1223 | hrtimers_resume(); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Rafael J. Wysocki | e1a85b2 | 2011-03-23 22:16:04 +0100 | [diff] [blame] | 1226 | static int timekeeping_suspend(void) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1227 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1228 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 92c1d3e | 2011-11-14 14:05:44 -0800 | [diff] [blame] | 1229 | unsigned long flags; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1230 | struct timespec64 delta, delta_delta; |
| 1231 | static struct timespec64 old_delta; |
| 1232 | struct timespec tmp; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1233 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1234 | read_persistent_clock(&tmp); |
| 1235 | timekeeping_suspend_time = timespec_to_timespec64(tmp); |
Thomas Gleixner | 3be9095 | 2007-09-16 15:36:43 +0200 | [diff] [blame] | 1236 | |
Zoran Markovic | 0d6bd99 | 2013-05-17 11:24:05 -0700 | [diff] [blame] | 1237 | /* |
| 1238 | * On some systems the persistent_clock can not be detected at |
| 1239 | * timekeeping_init by its return value, so if we see a valid |
| 1240 | * value returned, update the persistent_clock_exists flag. |
| 1241 | */ |
| 1242 | if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec) |
| 1243 | persistent_clock_exist = true; |
| 1244 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1245 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1246 | write_seqcount_begin(&tk_core.seq); |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1247 | timekeeping_forward_now(tk); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1248 | timekeeping_suspended = 1; |
John Stultz | cb33217 | 2011-05-31 22:53:23 -0700 | [diff] [blame] | 1249 | |
| 1250 | /* |
| 1251 | * To avoid drift caused by repeated suspend/resumes, |
| 1252 | * which each can add ~1 second drift error, |
| 1253 | * try to compensate so the difference in system time |
| 1254 | * and persistent_clock time stays close to constant. |
| 1255 | */ |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1256 | delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time); |
| 1257 | delta_delta = timespec64_sub(delta, old_delta); |
John Stultz | cb33217 | 2011-05-31 22:53:23 -0700 | [diff] [blame] | 1258 | if (abs(delta_delta.tv_sec) >= 2) { |
| 1259 | /* |
| 1260 | * if delta_delta is too large, assume time correction |
| 1261 | * has occured and set old_delta to the current delta. |
| 1262 | */ |
| 1263 | old_delta = delta; |
| 1264 | } else { |
| 1265 | /* Otherwise try to adjust old_system to compensate */ |
| 1266 | timekeeping_suspend_time = |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1267 | timespec64_add(timekeeping_suspend_time, delta_delta); |
John Stultz | cb33217 | 2011-05-31 22:53:23 -0700 | [diff] [blame] | 1268 | } |
John Stultz | 330a161 | 2013-12-11 19:10:36 -0800 | [diff] [blame] | 1269 | |
| 1270 | timekeeping_update(tk, TK_MIRROR); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1271 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1272 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1273 | |
| 1274 | clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL); |
Magnus Damm | c54a42b | 2010-02-02 14:41:41 -0800 | [diff] [blame] | 1275 | clocksource_suspend(); |
Rafael J. Wysocki | adc78e6 | 2012-08-06 01:40:41 +0200 | [diff] [blame] | 1276 | clockevents_suspend(); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1277 | |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | /* sysfs resume/suspend bits for timekeeping */ |
Rafael J. Wysocki | e1a85b2 | 2011-03-23 22:16:04 +0100 | [diff] [blame] | 1282 | static struct syscore_ops timekeeping_syscore_ops = { |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1283 | .resume = timekeeping_resume, |
| 1284 | .suspend = timekeeping_suspend, |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1285 | }; |
| 1286 | |
Rafael J. Wysocki | e1a85b2 | 2011-03-23 22:16:04 +0100 | [diff] [blame] | 1287 | static int __init timekeeping_init_ops(void) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1288 | { |
Rafael J. Wysocki | e1a85b2 | 2011-03-23 22:16:04 +0100 | [diff] [blame] | 1289 | register_syscore_ops(&timekeeping_syscore_ops); |
| 1290 | return 0; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1291 | } |
Rafael J. Wysocki | e1a85b2 | 2011-03-23 22:16:04 +0100 | [diff] [blame] | 1292 | device_initcall(timekeeping_init_ops); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1293 | |
| 1294 | /* |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1295 | * Apply a multiplier adjustment to the timekeeper |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1296 | */ |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1297 | static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, |
| 1298 | s64 offset, |
| 1299 | bool negative, |
| 1300 | int adj_scale) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1301 | { |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1302 | s64 interval = tk->cycle_interval; |
| 1303 | s32 mult_adj = 1; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1304 | |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1305 | if (negative) { |
| 1306 | mult_adj = -mult_adj; |
| 1307 | interval = -interval; |
| 1308 | offset = -offset; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1309 | } |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1310 | mult_adj <<= adj_scale; |
| 1311 | interval <<= adj_scale; |
| 1312 | offset <<= adj_scale; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1313 | |
John Stultz | c2bc111 | 2011-10-27 18:12:42 -0700 | [diff] [blame] | 1314 | /* |
| 1315 | * So the following can be confusing. |
| 1316 | * |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1317 | * To keep things simple, lets assume mult_adj == 1 for now. |
John Stultz | c2bc111 | 2011-10-27 18:12:42 -0700 | [diff] [blame] | 1318 | * |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1319 | * When mult_adj != 1, remember that the interval and offset values |
John Stultz | c2bc111 | 2011-10-27 18:12:42 -0700 | [diff] [blame] | 1320 | * have been appropriately scaled so the math is the same. |
| 1321 | * |
| 1322 | * The basic idea here is that we're increasing the multiplier |
| 1323 | * by one, this causes the xtime_interval to be incremented by |
| 1324 | * one cycle_interval. This is because: |
| 1325 | * xtime_interval = cycle_interval * mult |
| 1326 | * So if mult is being incremented by one: |
| 1327 | * xtime_interval = cycle_interval * (mult + 1) |
| 1328 | * Its the same as: |
| 1329 | * xtime_interval = (cycle_interval * mult) + cycle_interval |
| 1330 | * Which can be shortened to: |
| 1331 | * xtime_interval += cycle_interval |
| 1332 | * |
| 1333 | * So offset stores the non-accumulated cycles. Thus the current |
| 1334 | * time (in shifted nanoseconds) is: |
| 1335 | * now = (offset * adj) + xtime_nsec |
| 1336 | * Now, even though we're adjusting the clock frequency, we have |
| 1337 | * to keep time consistent. In other words, we can't jump back |
| 1338 | * in time, and we also want to avoid jumping forward in time. |
| 1339 | * |
| 1340 | * So given the same offset value, we need the time to be the same |
| 1341 | * both before and after the freq adjustment. |
| 1342 | * now = (offset * adj_1) + xtime_nsec_1 |
| 1343 | * now = (offset * adj_2) + xtime_nsec_2 |
| 1344 | * So: |
| 1345 | * (offset * adj_1) + xtime_nsec_1 = |
| 1346 | * (offset * adj_2) + xtime_nsec_2 |
| 1347 | * And we know: |
| 1348 | * adj_2 = adj_1 + 1 |
| 1349 | * So: |
| 1350 | * (offset * adj_1) + xtime_nsec_1 = |
| 1351 | * (offset * (adj_1+1)) + xtime_nsec_2 |
| 1352 | * (offset * adj_1) + xtime_nsec_1 = |
| 1353 | * (offset * adj_1) + offset + xtime_nsec_2 |
| 1354 | * Canceling the sides: |
| 1355 | * xtime_nsec_1 = offset + xtime_nsec_2 |
| 1356 | * Which gives us: |
| 1357 | * xtime_nsec_2 = xtime_nsec_1 - offset |
| 1358 | * Which simplfies to: |
| 1359 | * xtime_nsec -= offset |
| 1360 | * |
| 1361 | * XXX - TODO: Doc ntp_error calculation. |
| 1362 | */ |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1363 | tk->tkr.mult += mult_adj; |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1364 | tk->xtime_interval += interval; |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1365 | tk->tkr.xtime_nsec -= offset; |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1366 | tk->ntp_error -= (interval - offset) << tk->ntp_error_shift; |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1367 | } |
John Stultz | 2a8c088 | 2012-07-13 01:21:56 -0400 | [diff] [blame] | 1368 | |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1369 | /* |
| 1370 | * Calculate the multiplier adjustment needed to match the frequency |
| 1371 | * specified by NTP |
| 1372 | */ |
| 1373 | static __always_inline void timekeeping_freqadjust(struct timekeeper *tk, |
| 1374 | s64 offset) |
| 1375 | { |
| 1376 | s64 interval = tk->cycle_interval; |
| 1377 | s64 xinterval = tk->xtime_interval; |
| 1378 | s64 tick_error; |
| 1379 | bool negative; |
| 1380 | u32 adj; |
| 1381 | |
| 1382 | /* Remove any current error adj from freq calculation */ |
| 1383 | if (tk->ntp_err_mult) |
| 1384 | xinterval -= tk->cycle_interval; |
| 1385 | |
John Stultz | 375f45b | 2014-04-23 20:53:29 -0700 | [diff] [blame] | 1386 | tk->ntp_tick = ntp_tick_length(); |
| 1387 | |
John Stultz | dc49159 | 2013-12-06 17:25:21 -0800 | [diff] [blame] | 1388 | /* Calculate current error per tick */ |
| 1389 | tick_error = ntp_tick_length() >> tk->ntp_error_shift; |
| 1390 | tick_error -= (xinterval + tk->xtime_remainder); |
| 1391 | |
| 1392 | /* Don't worry about correcting it if its small */ |
| 1393 | if (likely((tick_error >= 0) && (tick_error <= interval))) |
| 1394 | return; |
| 1395 | |
| 1396 | /* preserve the direction of correction */ |
| 1397 | negative = (tick_error < 0); |
| 1398 | |
| 1399 | /* Sort out the magnitude of the correction */ |
| 1400 | tick_error = abs(tick_error); |
| 1401 | for (adj = 0; tick_error > interval; adj++) |
| 1402 | tick_error >>= 1; |
| 1403 | |
| 1404 | /* scale the corrections */ |
| 1405 | timekeeping_apply_adjustment(tk, offset, negative, adj); |
| 1406 | } |
| 1407 | |
| 1408 | /* |
| 1409 | * Adjust the timekeeper's multiplier to the correct frequency |
| 1410 | * and also to reduce the accumulated error value. |
| 1411 | */ |
| 1412 | static void timekeeping_adjust(struct timekeeper *tk, s64 offset) |
| 1413 | { |
| 1414 | /* Correct for the current frequency error */ |
| 1415 | timekeeping_freqadjust(tk, offset); |
| 1416 | |
| 1417 | /* Next make a small adjustment to fix any cumulative error */ |
| 1418 | if (!tk->ntp_err_mult && (tk->ntp_error > 0)) { |
| 1419 | tk->ntp_err_mult = 1; |
| 1420 | timekeeping_apply_adjustment(tk, offset, 0, 0); |
| 1421 | } else if (tk->ntp_err_mult && (tk->ntp_error <= 0)) { |
| 1422 | /* Undo any existing error adjustment */ |
| 1423 | timekeeping_apply_adjustment(tk, offset, 1, 0); |
| 1424 | tk->ntp_err_mult = 0; |
| 1425 | } |
| 1426 | |
| 1427 | if (unlikely(tk->tkr.clock->maxadj && |
| 1428 | (tk->tkr.mult > tk->tkr.clock->mult + tk->tkr.clock->maxadj))) { |
| 1429 | printk_once(KERN_WARNING |
| 1430 | "Adjusting %s more than 11%% (%ld vs %ld)\n", |
| 1431 | tk->tkr.clock->name, (long)tk->tkr.mult, |
| 1432 | (long)tk->tkr.clock->mult + tk->tkr.clock->maxadj); |
| 1433 | } |
| 1434 | |
John Stultz | 2a8c088 | 2012-07-13 01:21:56 -0400 | [diff] [blame] | 1435 | /* |
| 1436 | * It may be possible that when we entered this function, xtime_nsec |
| 1437 | * was very small. Further, if we're slightly speeding the clocksource |
| 1438 | * in the code above, its possible the required corrective factor to |
| 1439 | * xtime_nsec could cause it to underflow. |
| 1440 | * |
| 1441 | * Now, since we already accumulated the second, cannot simply roll |
| 1442 | * the accumulated second back, since the NTP subsystem has been |
| 1443 | * notified via second_overflow. So instead we push xtime_nsec forward |
| 1444 | * by the amount we underflowed, and add that amount into the error. |
| 1445 | * |
| 1446 | * We'll correct this error next time through this function, when |
| 1447 | * xtime_nsec is not as small. |
| 1448 | */ |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1449 | if (unlikely((s64)tk->tkr.xtime_nsec < 0)) { |
| 1450 | s64 neg = -(s64)tk->tkr.xtime_nsec; |
| 1451 | tk->tkr.xtime_nsec = 0; |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1452 | tk->ntp_error += neg << tk->ntp_error_shift; |
John Stultz | 2a8c088 | 2012-07-13 01:21:56 -0400 | [diff] [blame] | 1453 | } |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | /** |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1457 | * accumulate_nsecs_to_secs - Accumulates nsecs into secs |
| 1458 | * |
| 1459 | * Helper function that accumulates a the nsecs greater then a second |
| 1460 | * from the xtime_nsec field to the xtime_secs field. |
| 1461 | * It also calls into the NTP code to handle leapsecond processing. |
| 1462 | * |
| 1463 | */ |
David Vrabel | 780427f | 2013-06-27 11:35:46 +0100 | [diff] [blame] | 1464 | static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk) |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1465 | { |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1466 | u64 nsecps = (u64)NSEC_PER_SEC << tk->tkr.shift; |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 1467 | unsigned int clock_set = 0; |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1468 | |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1469 | while (tk->tkr.xtime_nsec >= nsecps) { |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1470 | int leap; |
| 1471 | |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1472 | tk->tkr.xtime_nsec -= nsecps; |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1473 | tk->xtime_sec++; |
| 1474 | |
| 1475 | /* Figure out if its a leap sec and apply if needed */ |
| 1476 | leap = second_overflow(tk->xtime_sec); |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 1477 | if (unlikely(leap)) { |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1478 | struct timespec64 ts; |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1479 | |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 1480 | tk->xtime_sec += leap; |
| 1481 | |
| 1482 | ts.tv_sec = leap; |
| 1483 | ts.tv_nsec = 0; |
| 1484 | tk_set_wall_to_mono(tk, |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1485 | timespec64_sub(tk->wall_to_monotonic, ts)); |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 1486 | |
John Stultz | cc244dd | 2012-05-03 12:30:07 -0700 | [diff] [blame] | 1487 | __timekeeping_set_tai_offset(tk, tk->tai_offset - leap); |
| 1488 | |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 1489 | clock_set = TK_CLOCK_WAS_SET; |
John Stultz | 6d0ef90 | 2012-07-27 14:48:12 -0400 | [diff] [blame] | 1490 | } |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1491 | } |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 1492 | return clock_set; |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1493 | } |
| 1494 | |
John Stultz | 1f4f948 | 2012-07-13 01:21:54 -0400 | [diff] [blame] | 1495 | /** |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 1496 | * logarithmic_accumulation - shifted accumulation of cycles |
| 1497 | * |
| 1498 | * This functions accumulates a shifted interval of cycles into |
| 1499 | * into a shifted interval nanoseconds. Allows for O(log) accumulation |
| 1500 | * loop. |
| 1501 | * |
| 1502 | * Returns the unconsumed cycles. |
| 1503 | */ |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1504 | static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset, |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 1505 | u32 shift, |
| 1506 | unsigned int *clock_set) |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 1507 | { |
Thomas Gleixner | 23a9537 | 2013-02-21 22:51:36 +0000 | [diff] [blame] | 1508 | cycle_t interval = tk->cycle_interval << shift; |
Jason Wessel | deda2e8 | 2010-08-09 14:20:09 -0700 | [diff] [blame] | 1509 | u64 raw_nsecs; |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 1510 | |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1511 | /* If the offset is smaller then a shifted interval, do nothing */ |
Thomas Gleixner | 23a9537 | 2013-02-21 22:51:36 +0000 | [diff] [blame] | 1512 | if (offset < interval) |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 1513 | return offset; |
| 1514 | |
| 1515 | /* Accumulate one shifted interval */ |
Thomas Gleixner | 23a9537 | 2013-02-21 22:51:36 +0000 | [diff] [blame] | 1516 | offset -= interval; |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1517 | tk->tkr.cycle_last += interval; |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 1518 | |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1519 | tk->tkr.xtime_nsec += tk->xtime_interval << shift; |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 1520 | *clock_set |= accumulate_nsecs_to_secs(tk); |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 1521 | |
Jason Wessel | deda2e8 | 2010-08-09 14:20:09 -0700 | [diff] [blame] | 1522 | /* Accumulate raw time */ |
Dan Carpenter | 5b3900c | 2012-10-09 10:18:23 +0300 | [diff] [blame] | 1523 | raw_nsecs = (u64)tk->raw_interval << shift; |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1524 | raw_nsecs += tk->raw_time.tv_nsec; |
John Stultz | c7dcf87 | 2010-08-13 11:30:58 -0700 | [diff] [blame] | 1525 | if (raw_nsecs >= NSEC_PER_SEC) { |
| 1526 | u64 raw_secs = raw_nsecs; |
| 1527 | raw_nsecs = do_div(raw_secs, NSEC_PER_SEC); |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1528 | tk->raw_time.tv_sec += raw_secs; |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 1529 | } |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1530 | tk->raw_time.tv_nsec = raw_nsecs; |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 1531 | |
| 1532 | /* Accumulate error between NTP and clock interval */ |
John Stultz | 375f45b | 2014-04-23 20:53:29 -0700 | [diff] [blame] | 1533 | tk->ntp_error += tk->ntp_tick << shift; |
John Stultz | f726a69 | 2012-07-13 01:21:57 -0400 | [diff] [blame] | 1534 | tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) << |
| 1535 | (tk->ntp_error_shift + shift); |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 1536 | |
| 1537 | return offset; |
| 1538 | } |
| 1539 | |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1540 | /** |
| 1541 | * update_wall_time - Uses the current clocksource to increment the wall time |
| 1542 | * |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1543 | */ |
John Stultz | 47a1b796 | 2013-12-12 13:10:55 -0800 | [diff] [blame] | 1544 | void update_wall_time(void) |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1545 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1546 | struct timekeeper *real_tk = &tk_core.timekeeper; |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 1547 | struct timekeeper *tk = &shadow_timekeeper; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1548 | cycle_t offset; |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 1549 | int shift = 0, maxshift; |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 1550 | unsigned int clock_set = 0; |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 1551 | unsigned long flags; |
| 1552 | |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1553 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1554 | |
| 1555 | /* Make sure we're fully resumed: */ |
| 1556 | if (unlikely(timekeeping_suspended)) |
John Stultz | 70471f2 | 2011-11-14 12:48:10 -0800 | [diff] [blame] | 1557 | goto out; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1558 | |
John Stultz | 592913e | 2010-07-13 17:56:20 -0700 | [diff] [blame] | 1559 | #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 1560 | offset = real_tk->cycle_interval; |
John Stultz | 592913e | 2010-07-13 17:56:20 -0700 | [diff] [blame] | 1561 | #else |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1562 | offset = clocksource_delta(tk->tkr.read(tk->tkr.clock), |
| 1563 | tk->tkr.cycle_last, tk->tkr.mask); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1564 | #endif |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1565 | |
John Stultz | bf2ac31 | 2012-08-21 20:30:49 -0400 | [diff] [blame] | 1566 | /* Check if there's really nothing to do */ |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 1567 | if (offset < real_tk->cycle_interval) |
John Stultz | bf2ac31 | 2012-08-21 20:30:49 -0400 | [diff] [blame] | 1568 | goto out; |
| 1569 | |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 1570 | /* |
| 1571 | * With NO_HZ we may have to accumulate many cycle_intervals |
| 1572 | * (think "ticks") worth of time at once. To do this efficiently, |
| 1573 | * we calculate the largest doubling multiple of cycle_intervals |
Jim Cromie | 88b28ad | 2012-03-14 21:28:56 -0600 | [diff] [blame] | 1574 | * that is smaller than the offset. We then accumulate that |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 1575 | * chunk in one go, and then try to consume the next smaller |
| 1576 | * doubled multiple. |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1577 | */ |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1578 | shift = ilog2(offset) - ilog2(tk->cycle_interval); |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 1579 | shift = max(0, shift); |
Jim Cromie | 88b28ad | 2012-03-14 21:28:56 -0600 | [diff] [blame] | 1580 | /* Bound shift to one less than what overflows tick_length */ |
John Stultz | ea7cf49 | 2011-11-14 13:18:07 -0800 | [diff] [blame] | 1581 | maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1; |
john stultz | a092ff0 | 2009-10-02 16:17:53 -0700 | [diff] [blame] | 1582 | shift = min(shift, maxshift); |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1583 | while (offset >= tk->cycle_interval) { |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 1584 | offset = logarithmic_accumulation(tk, offset, shift, |
| 1585 | &clock_set); |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1586 | if (offset < tk->cycle_interval<<shift) |
John Stultz | 830ec04 | 2010-03-18 14:47:30 -0700 | [diff] [blame] | 1587 | shift--; |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | /* correct the clock when NTP error is too big */ |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1591 | timekeeping_adjust(tk, offset); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1592 | |
John Stultz | 6a867a3 | 2010-04-06 14:30:51 -0700 | [diff] [blame] | 1593 | /* |
John Stultz | 92bb1fc | 2012-09-04 15:38:12 -0400 | [diff] [blame] | 1594 | * XXX This can be killed once everyone converts |
| 1595 | * to the new update_vsyscall. |
| 1596 | */ |
| 1597 | old_vsyscall_fixup(tk); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1598 | |
John Stultz | 6a867a3 | 2010-04-06 14:30:51 -0700 | [diff] [blame] | 1599 | /* |
| 1600 | * Finally, make sure that after the rounding |
John Stultz | 1e75fa8 | 2012-07-13 01:21:53 -0400 | [diff] [blame] | 1601 | * xtime_nsec isn't larger than NSEC_PER_SEC |
John Stultz | 6a867a3 | 2010-04-06 14:30:51 -0700 | [diff] [blame] | 1602 | */ |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 1603 | clock_set |= accumulate_nsecs_to_secs(tk); |
Linus Torvalds | 83f57a1 | 2009-12-22 14:10:37 -0800 | [diff] [blame] | 1604 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1605 | write_seqcount_begin(&tk_core.seq); |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 1606 | /* |
| 1607 | * Update the real timekeeper. |
| 1608 | * |
| 1609 | * We could avoid this memcpy by switching pointers, but that |
| 1610 | * requires changes to all other timekeeper usage sites as |
| 1611 | * well, i.e. move the timekeeper pointer getter into the |
| 1612 | * spinlocked/seqcount protected sections. And we trade this |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1613 | * memcpy under the tk_core.seq against one before we start |
Thomas Gleixner | 48cdc13 | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 1614 | * updating. |
| 1615 | */ |
| 1616 | memcpy(real_tk, tk, sizeof(*tk)); |
John Stultz | 5258d3f | 2013-12-11 20:07:49 -0800 | [diff] [blame] | 1617 | timekeeping_update(real_tk, clock_set); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1618 | write_seqcount_end(&tk_core.seq); |
Thomas Gleixner | ca4523c | 2013-02-21 22:51:40 +0000 | [diff] [blame] | 1619 | out: |
Thomas Gleixner | 9a7a71b | 2013-02-21 22:51:38 +0000 | [diff] [blame] | 1620 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
John Stultz | 47a1b796 | 2013-12-12 13:10:55 -0800 | [diff] [blame] | 1621 | if (clock_set) |
John Stultz | cab5e12 | 2014-03-27 16:30:49 -0700 | [diff] [blame] | 1622 | /* Have to call _delayed version, since in irq context*/ |
| 1623 | clock_was_set_delayed(); |
john stultz | 8524070 | 2007-05-08 00:27:59 -0700 | [diff] [blame] | 1624 | } |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 1625 | |
| 1626 | /** |
| 1627 | * getboottime - Return the real time of system boot. |
| 1628 | * @ts: pointer to the timespec to be set |
| 1629 | * |
John Stultz | abb3a4e | 2011-02-14 17:52:09 -0800 | [diff] [blame] | 1630 | * Returns the wall-time of boot in a timespec. |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 1631 | * |
| 1632 | * This is based on the wall_to_monotonic offset and the total suspend |
| 1633 | * time. Calls to settimeofday will affect the value returned (which |
| 1634 | * basically means that however wrong your real time clock is at boot time, |
| 1635 | * you get the right time here). |
| 1636 | */ |
| 1637 | void getboottime(struct timespec *ts) |
| 1638 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1639 | struct timekeeper *tk = &tk_core.timekeeper; |
Thomas Gleixner | 02cba15 | 2014-07-16 21:04:58 +0000 | [diff] [blame] | 1640 | ktime_t t = ktime_sub(tk->offs_real, tk->offs_boot); |
Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 1641 | |
Thomas Gleixner | 02cba15 | 2014-07-16 21:04:58 +0000 | [diff] [blame] | 1642 | *ts = ktime_to_timespec(t); |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 1643 | } |
Jason Wang | c93d89f | 2010-01-27 19:13:40 +0800 | [diff] [blame] | 1644 | EXPORT_SYMBOL_GPL(getboottime); |
Tomas Janousek | 7c3f1a5 | 2007-07-15 23:39:41 -0700 | [diff] [blame] | 1645 | |
john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 1646 | unsigned long get_seconds(void) |
| 1647 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1648 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1649 | |
| 1650 | return tk->xtime_sec; |
john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 1651 | } |
| 1652 | EXPORT_SYMBOL(get_seconds); |
| 1653 | |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 1654 | struct timespec __current_kernel_time(void) |
| 1655 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1656 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1657 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1658 | return timespec64_to_timespec(tk_xtime(tk)); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 1659 | } |
john stultz | 17c38b7 | 2007-07-24 18:38:34 -0700 | [diff] [blame] | 1660 | |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 1661 | struct timespec current_kernel_time(void) |
| 1662 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1663 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1664 | struct timespec64 now; |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 1665 | unsigned long seq; |
| 1666 | |
| 1667 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1668 | seq = read_seqcount_begin(&tk_core.seq); |
Linus Torvalds | 83f57a1 | 2009-12-22 14:10:37 -0800 | [diff] [blame] | 1669 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1670 | now = tk_xtime(tk); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1671 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 1672 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1673 | return timespec64_to_timespec(now); |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 1674 | } |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 1675 | EXPORT_SYMBOL(current_kernel_time); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 1676 | |
| 1677 | struct timespec get_monotonic_coarse(void) |
| 1678 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1679 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1680 | struct timespec64 now, mono; |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 1681 | unsigned long seq; |
| 1682 | |
| 1683 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1684 | seq = read_seqcount_begin(&tk_core.seq); |
Linus Torvalds | 83f57a1 | 2009-12-22 14:10:37 -0800 | [diff] [blame] | 1685 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1686 | now = tk_xtime(tk); |
| 1687 | mono = tk->wall_to_monotonic; |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1688 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 1689 | |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1690 | set_normalized_timespec64(&now, now.tv_sec + mono.tv_sec, |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 1691 | now.tv_nsec + mono.tv_nsec); |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1692 | |
| 1693 | return timespec64_to_timespec(now); |
john stultz | da15cfd | 2009-08-19 19:13:34 -0700 | [diff] [blame] | 1694 | } |
Torben Hohn | 871cf1e | 2011-01-27 15:58:55 +0100 | [diff] [blame] | 1695 | |
| 1696 | /* |
John Stultz | d6ad418 | 2012-02-28 16:50:11 -0800 | [diff] [blame] | 1697 | * Must hold jiffies_lock |
Torben Hohn | 871cf1e | 2011-01-27 15:58:55 +0100 | [diff] [blame] | 1698 | */ |
| 1699 | void do_timer(unsigned long ticks) |
| 1700 | { |
| 1701 | jiffies_64 += ticks; |
Torben Hohn | 871cf1e | 2011-01-27 15:58:55 +0100 | [diff] [blame] | 1702 | calc_global_load(ticks); |
| 1703 | } |
Torben Hohn | 48cf76f7 | 2011-01-27 15:59:05 +0100 | [diff] [blame] | 1704 | |
| 1705 | /** |
John Stultz | 76f4108 | 2014-07-16 21:03:52 +0000 | [diff] [blame] | 1706 | * ktime_get_update_offsets_tick - hrtimer helper |
| 1707 | * @offs_real: pointer to storage for monotonic -> realtime offset |
| 1708 | * @offs_boot: pointer to storage for monotonic -> boottime offset |
| 1709 | * @offs_tai: pointer to storage for monotonic -> clock tai offset |
| 1710 | * |
| 1711 | * Returns monotonic time at last tick and various offsets |
Torben Hohn | 48cf76f7 | 2011-01-27 15:59:05 +0100 | [diff] [blame] | 1712 | */ |
John Stultz | 76f4108 | 2014-07-16 21:03:52 +0000 | [diff] [blame] | 1713 | ktime_t ktime_get_update_offsets_tick(ktime_t *offs_real, ktime_t *offs_boot, |
| 1714 | ktime_t *offs_tai) |
Torben Hohn | 48cf76f7 | 2011-01-27 15:59:05 +0100 | [diff] [blame] | 1715 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1716 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 76f4108 | 2014-07-16 21:03:52 +0000 | [diff] [blame] | 1717 | unsigned int seq; |
Thomas Gleixner | 48064f5 | 2014-07-16 21:04:20 +0000 | [diff] [blame] | 1718 | ktime_t base; |
| 1719 | u64 nsecs; |
Torben Hohn | 48cf76f7 | 2011-01-27 15:59:05 +0100 | [diff] [blame] | 1720 | |
| 1721 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1722 | seq = read_seqcount_begin(&tk_core.seq); |
John Stultz | 76f4108 | 2014-07-16 21:03:52 +0000 | [diff] [blame] | 1723 | |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1724 | base = tk->tkr.base_mono; |
| 1725 | nsecs = tk->tkr.xtime_nsec >> tk->tkr.shift; |
Thomas Gleixner | 48064f5 | 2014-07-16 21:04:20 +0000 | [diff] [blame] | 1726 | |
John Stultz | 76f4108 | 2014-07-16 21:03:52 +0000 | [diff] [blame] | 1727 | *offs_real = tk->offs_real; |
| 1728 | *offs_boot = tk->offs_boot; |
| 1729 | *offs_tai = tk->offs_tai; |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1730 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
John Stultz | 76f4108 | 2014-07-16 21:03:52 +0000 | [diff] [blame] | 1731 | |
Thomas Gleixner | 48064f5 | 2014-07-16 21:04:20 +0000 | [diff] [blame] | 1732 | return ktime_add_ns(base, nsecs); |
Torben Hohn | 48cf76f7 | 2011-01-27 15:59:05 +0100 | [diff] [blame] | 1733 | } |
Torben Hohn | f0af911a9 | 2011-01-27 15:59:10 +0100 | [diff] [blame] | 1734 | |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 1735 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 1736 | /** |
John Stultz | 76f4108 | 2014-07-16 21:03:52 +0000 | [diff] [blame] | 1737 | * ktime_get_update_offsets_now - hrtimer helper |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 1738 | * @offs_real: pointer to storage for monotonic -> realtime offset |
| 1739 | * @offs_boot: pointer to storage for monotonic -> boottime offset |
Xie XiuQi | b7bc50e | 2013-10-18 09:13:30 +0800 | [diff] [blame] | 1740 | * @offs_tai: pointer to storage for monotonic -> clock tai offset |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 1741 | * |
| 1742 | * Returns current monotonic time and updates the offsets |
Xie XiuQi | b7bc50e | 2013-10-18 09:13:30 +0800 | [diff] [blame] | 1743 | * Called from hrtimer_interrupt() or retrigger_next_event() |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 1744 | */ |
John Stultz | 76f4108 | 2014-07-16 21:03:52 +0000 | [diff] [blame] | 1745 | ktime_t ktime_get_update_offsets_now(ktime_t *offs_real, ktime_t *offs_boot, |
John Stultz | 90adda9 | 2013-01-21 17:00:11 -0800 | [diff] [blame] | 1746 | ktime_t *offs_tai) |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 1747 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1748 | struct timekeeper *tk = &tk_core.timekeeper; |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 1749 | unsigned int seq; |
Thomas Gleixner | a37c0aa | 2014-07-16 21:04:19 +0000 | [diff] [blame] | 1750 | ktime_t base; |
| 1751 | u64 nsecs; |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 1752 | |
| 1753 | do { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1754 | seq = read_seqcount_begin(&tk_core.seq); |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 1755 | |
Thomas Gleixner | d28ede8 | 2014-07-16 21:05:16 +0000 | [diff] [blame] | 1756 | base = tk->tkr.base_mono; |
Thomas Gleixner | 0e5ac3a | 2014-07-16 21:05:18 +0000 | [diff] [blame] | 1757 | nsecs = timekeeping_get_ns(&tk->tkr); |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 1758 | |
John Stultz | 4e250fd | 2012-07-27 14:48:13 -0400 | [diff] [blame] | 1759 | *offs_real = tk->offs_real; |
| 1760 | *offs_boot = tk->offs_boot; |
John Stultz | 90adda9 | 2013-01-21 17:00:11 -0800 | [diff] [blame] | 1761 | *offs_tai = tk->offs_tai; |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1762 | } while (read_seqcount_retry(&tk_core.seq, seq)); |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 1763 | |
Thomas Gleixner | a37c0aa | 2014-07-16 21:04:19 +0000 | [diff] [blame] | 1764 | return ktime_add_ns(base, nsecs); |
Thomas Gleixner | f6c06ab | 2012-07-10 18:43:24 -0400 | [diff] [blame] | 1765 | } |
| 1766 | #endif |
| 1767 | |
Torben Hohn | f0af911a9 | 2011-01-27 15:59:10 +0100 | [diff] [blame] | 1768 | /** |
John Stultz | aa6f9c59 | 2013-03-22 11:31:29 -0700 | [diff] [blame] | 1769 | * do_adjtimex() - Accessor function to NTP __do_adjtimex function |
| 1770 | */ |
| 1771 | int do_adjtimex(struct timex *txc) |
| 1772 | { |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1773 | struct timekeeper *tk = &tk_core.timekeeper; |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 1774 | unsigned long flags; |
John Stultz | 7d489d1 | 2014-07-16 21:04:01 +0000 | [diff] [blame] | 1775 | struct timespec64 ts; |
John Stultz | 4e8f8b3 | 2013-04-10 12:41:49 -0700 | [diff] [blame] | 1776 | s32 orig_tai, tai; |
John Stultz | e408569 | 2013-03-22 12:08:52 -0700 | [diff] [blame] | 1777 | int ret; |
| 1778 | |
| 1779 | /* Validate the data before disabling interrupts */ |
| 1780 | ret = ntp_validate_timex(txc); |
| 1781 | if (ret) |
| 1782 | return ret; |
| 1783 | |
John Stultz | cef9037 | 2013-03-22 15:04:13 -0700 | [diff] [blame] | 1784 | if (txc->modes & ADJ_SETOFFSET) { |
| 1785 | struct timespec delta; |
| 1786 | delta.tv_sec = txc->time.tv_sec; |
| 1787 | delta.tv_nsec = txc->time.tv_usec; |
| 1788 | if (!(txc->modes & ADJ_NANO)) |
| 1789 | delta.tv_nsec *= 1000; |
| 1790 | ret = timekeeping_inject_offset(&delta); |
| 1791 | if (ret) |
| 1792 | return ret; |
| 1793 | } |
| 1794 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 1795 | getnstimeofday64(&ts); |
John Stultz | aa6f9c59 | 2013-03-22 11:31:29 -0700 | [diff] [blame] | 1796 | |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 1797 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1798 | write_seqcount_begin(&tk_core.seq); |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 1799 | |
John Stultz | 4e8f8b3 | 2013-04-10 12:41:49 -0700 | [diff] [blame] | 1800 | orig_tai = tai = tk->tai_offset; |
John Stultz | 87ace39 | 2013-03-22 12:28:15 -0700 | [diff] [blame] | 1801 | ret = __do_adjtimex(txc, &ts, &tai); |
| 1802 | |
John Stultz | 4e8f8b3 | 2013-04-10 12:41:49 -0700 | [diff] [blame] | 1803 | if (tai != orig_tai) { |
| 1804 | __timekeeping_set_tai_offset(tk, tai); |
John Stultz | f55c076 | 2013-12-11 18:50:25 -0800 | [diff] [blame] | 1805 | timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); |
John Stultz | 4e8f8b3 | 2013-04-10 12:41:49 -0700 | [diff] [blame] | 1806 | } |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1807 | write_seqcount_end(&tk_core.seq); |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 1808 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
| 1809 | |
John Stultz | 6fdda9a | 2013-12-10 17:18:18 -0800 | [diff] [blame] | 1810 | if (tai != orig_tai) |
| 1811 | clock_was_set(); |
| 1812 | |
John Stultz | 7bd3601 | 2013-09-11 16:50:56 -0700 | [diff] [blame] | 1813 | ntp_notify_cmos_timer(); |
| 1814 | |
John Stultz | 87ace39 | 2013-03-22 12:28:15 -0700 | [diff] [blame] | 1815 | return ret; |
| 1816 | } |
John Stultz | aa6f9c59 | 2013-03-22 11:31:29 -0700 | [diff] [blame] | 1817 | |
| 1818 | #ifdef CONFIG_NTP_PPS |
| 1819 | /** |
| 1820 | * hardpps() - Accessor function to NTP __hardpps function |
| 1821 | */ |
| 1822 | void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts) |
| 1823 | { |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 1824 | unsigned long flags; |
| 1825 | |
| 1826 | raw_spin_lock_irqsave(&timekeeper_lock, flags); |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1827 | write_seqcount_begin(&tk_core.seq); |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 1828 | |
John Stultz | aa6f9c59 | 2013-03-22 11:31:29 -0700 | [diff] [blame] | 1829 | __hardpps(phase_ts, raw_ts); |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 1830 | |
Thomas Gleixner | 3fdb14f | 2014-07-16 21:04:07 +0000 | [diff] [blame] | 1831 | write_seqcount_end(&tk_core.seq); |
John Stultz | 06c017f | 2013-03-22 11:37:28 -0700 | [diff] [blame] | 1832 | raw_spin_unlock_irqrestore(&timekeeper_lock, flags); |
John Stultz | aa6f9c59 | 2013-03-22 11:31:29 -0700 | [diff] [blame] | 1833 | } |
| 1834 | EXPORT_SYMBOL(hardpps); |
| 1835 | #endif |
| 1836 | |
| 1837 | /** |
Torben Hohn | f0af911a9 | 2011-01-27 15:59:10 +0100 | [diff] [blame] | 1838 | * xtime_update() - advances the timekeeping infrastructure |
| 1839 | * @ticks: number of ticks, that have elapsed since the last call. |
| 1840 | * |
| 1841 | * Must be called with interrupts disabled. |
| 1842 | */ |
| 1843 | void xtime_update(unsigned long ticks) |
| 1844 | { |
John Stultz | d6ad418 | 2012-02-28 16:50:11 -0800 | [diff] [blame] | 1845 | write_seqlock(&jiffies_lock); |
Torben Hohn | f0af911a9 | 2011-01-27 15:59:10 +0100 | [diff] [blame] | 1846 | do_timer(ticks); |
John Stultz | d6ad418 | 2012-02-28 16:50:11 -0800 | [diff] [blame] | 1847 | write_sequnlock(&jiffies_lock); |
John Stultz | 47a1b796 | 2013-12-12 13:10:55 -0800 | [diff] [blame] | 1848 | update_wall_time(); |
Torben Hohn | f0af911a9 | 2011-01-27 15:59:10 +0100 | [diff] [blame] | 1849 | } |