Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 2 | #ifndef _LINUX_TIMEKEEPING_H |
| 3 | #define _LINUX_TIMEKEEPING_H |
| 4 | |
Christoph Hellwig | b536fd5 | 2016-09-22 07:48:17 -0700 | [diff] [blame] | 5 | #include <linux/errno.h> |
Baolin Wang | 86d3473 | 2016-04-08 14:02:12 +0800 | [diff] [blame] | 6 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 7 | /* Included from linux/ktime.h */ |
| 8 | |
| 9 | void timekeeping_init(void); |
| 10 | extern int timekeeping_suspended; |
| 11 | |
Ingo Molnar | 93b5a9a | 2017-02-05 14:53:36 +0100 | [diff] [blame] | 12 | /* Architecture timer tick functions: */ |
| 13 | extern void update_process_times(int user); |
| 14 | extern void xtime_update(unsigned long ticks); |
| 15 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 16 | /* |
| 17 | * Get and set timeofday |
| 18 | */ |
pang.xunlei | 21f7eca | 2014-11-18 19:15:16 +0800 | [diff] [blame] | 19 | extern int do_settimeofday64(const struct timespec64 *ts); |
Baolin Wang | 86d3473 | 2016-04-08 14:02:12 +0800 | [diff] [blame] | 20 | extern int do_sys_settimeofday64(const struct timespec64 *tv, |
| 21 | const struct timezone *tz); |
Baolin Wang | 8758a24 | 2015-07-29 20:09:43 +0800 | [diff] [blame] | 22 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 23 | /* |
Arnd Bergmann | 6546911 | 2017-10-19 13:14:49 +0200 | [diff] [blame] | 24 | * timespec64 based interfaces |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 25 | */ |
Arnd Bergmann | fb7fcc9 | 2018-04-27 15:40:14 +0200 | [diff] [blame] | 26 | extern void ktime_get_raw_ts64(struct timespec64 *ts); |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 27 | extern void ktime_get_ts64(struct timespec64 *ts); |
Arnd Bergmann | edca71f | 2018-04-27 15:40:13 +0200 | [diff] [blame] | 28 | extern void ktime_get_real_ts64(struct timespec64 *tv); |
Arnd Bergmann | fb7fcc9 | 2018-04-27 15:40:14 +0200 | [diff] [blame] | 29 | extern void ktime_get_coarse_ts64(struct timespec64 *ts); |
| 30 | extern void ktime_get_coarse_real_ts64(struct timespec64 *ts); |
| 31 | |
| 32 | void getboottime64(struct timespec64 *ts); |
| 33 | |
| 34 | /* |
| 35 | * time64_t base interfaces |
| 36 | */ |
Heena Sirwani | 9e3680b | 2014-10-29 16:01:16 +0530 | [diff] [blame] | 37 | extern time64_t ktime_get_seconds(void); |
Arnd Bergmann | 6909e29 | 2017-10-12 16:06:11 +0200 | [diff] [blame] | 38 | extern time64_t __ktime_get_real_seconds(void); |
Heena Sirwani | dbe7aa6 | 2014-10-29 16:01:50 +0530 | [diff] [blame] | 39 | extern time64_t ktime_get_real_seconds(void); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 40 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 41 | /* |
| 42 | * ktime_t based interfaces |
| 43 | */ |
Thomas Gleixner | a3ed0e43 | 2018-04-25 15:33:38 +0200 | [diff] [blame] | 44 | |
Thomas Gleixner | 0077dc6 | 2014-07-16 21:04:13 +0000 | [diff] [blame] | 45 | enum tk_offsets { |
| 46 | TK_OFFS_REAL, |
Thomas Gleixner | a3ed0e43 | 2018-04-25 15:33:38 +0200 | [diff] [blame] | 47 | TK_OFFS_BOOT, |
Thomas Gleixner | 0077dc6 | 2014-07-16 21:04:13 +0000 | [diff] [blame] | 48 | TK_OFFS_TAI, |
| 49 | TK_OFFS_MAX, |
| 50 | }; |
| 51 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 52 | extern ktime_t ktime_get(void); |
Thomas Gleixner | 0077dc6 | 2014-07-16 21:04:13 +0000 | [diff] [blame] | 53 | extern ktime_t ktime_get_with_offset(enum tk_offsets offs); |
Arnd Bergmann | b9ff604 | 2018-04-27 15:40:15 +0200 | [diff] [blame] | 54 | extern ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs); |
Thomas Gleixner | 9a6b519 | 2014-07-16 21:04:22 +0000 | [diff] [blame] | 55 | extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs); |
Thomas Gleixner | f519b1a | 2014-07-16 21:05:04 +0000 | [diff] [blame] | 56 | extern ktime_t ktime_get_raw(void); |
Harald Geyer | 6374f91 | 2015-04-07 11:12:35 +0000 | [diff] [blame] | 57 | extern u32 ktime_get_resolution_ns(void); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 58 | |
Thomas Gleixner | f5264d5 | 2014-07-16 21:04:14 +0000 | [diff] [blame] | 59 | /** |
| 60 | * ktime_get_real - get the real (wall-) time in ktime_t format |
| 61 | */ |
| 62 | static inline ktime_t ktime_get_real(void) |
| 63 | { |
| 64 | return ktime_get_with_offset(TK_OFFS_REAL); |
| 65 | } |
| 66 | |
Arnd Bergmann | b9ff604 | 2018-04-27 15:40:15 +0200 | [diff] [blame] | 67 | static inline ktime_t ktime_get_coarse_real(void) |
| 68 | { |
| 69 | return ktime_get_coarse_with_offset(TK_OFFS_REAL); |
| 70 | } |
| 71 | |
Thomas Gleixner | b82c817 | 2014-07-16 21:04:16 +0000 | [diff] [blame] | 72 | /** |
Thomas Gleixner | a3ed0e43 | 2018-04-25 15:33:38 +0200 | [diff] [blame] | 73 | * ktime_get_boottime - Returns monotonic time since boot in ktime_t format |
| 74 | * |
| 75 | * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the |
| 76 | * time spent in suspend. |
| 77 | */ |
| 78 | static inline ktime_t ktime_get_boottime(void) |
| 79 | { |
| 80 | return ktime_get_with_offset(TK_OFFS_BOOT); |
| 81 | } |
| 82 | |
Arnd Bergmann | b9ff604 | 2018-04-27 15:40:15 +0200 | [diff] [blame] | 83 | static inline ktime_t ktime_get_coarse_boottime(void) |
| 84 | { |
| 85 | return ktime_get_coarse_with_offset(TK_OFFS_BOOT); |
| 86 | } |
| 87 | |
Thomas Gleixner | a3ed0e43 | 2018-04-25 15:33:38 +0200 | [diff] [blame] | 88 | /** |
Thomas Gleixner | afab07c | 2014-07-16 21:04:17 +0000 | [diff] [blame] | 89 | * ktime_get_clocktai - Returns the TAI time of day in ktime_t format |
| 90 | */ |
| 91 | static inline ktime_t ktime_get_clocktai(void) |
| 92 | { |
| 93 | return ktime_get_with_offset(TK_OFFS_TAI); |
| 94 | } |
| 95 | |
Arnd Bergmann | b9ff604 | 2018-04-27 15:40:15 +0200 | [diff] [blame] | 96 | static inline ktime_t ktime_get_coarse_clocktai(void) |
| 97 | { |
| 98 | return ktime_get_coarse_with_offset(TK_OFFS_TAI); |
| 99 | } |
| 100 | |
Thomas Gleixner | 9a6b519 | 2014-07-16 21:04:22 +0000 | [diff] [blame] | 101 | /** |
| 102 | * ktime_mono_to_real - Convert monotonic time to clock realtime |
| 103 | */ |
| 104 | static inline ktime_t ktime_mono_to_real(ktime_t mono) |
| 105 | { |
| 106 | return ktime_mono_to_any(mono, TK_OFFS_REAL); |
| 107 | } |
| 108 | |
Thomas Gleixner | 897994e | 2014-07-16 21:04:29 +0000 | [diff] [blame] | 109 | static inline u64 ktime_get_ns(void) |
| 110 | { |
| 111 | return ktime_to_ns(ktime_get()); |
| 112 | } |
| 113 | |
| 114 | static inline u64 ktime_get_real_ns(void) |
| 115 | { |
| 116 | return ktime_to_ns(ktime_get_real()); |
| 117 | } |
| 118 | |
Thomas Gleixner | a3ed0e43 | 2018-04-25 15:33:38 +0200 | [diff] [blame] | 119 | static inline u64 ktime_get_boot_ns(void) |
| 120 | { |
| 121 | return ktime_to_ns(ktime_get_boottime()); |
| 122 | } |
| 123 | |
Peter Zijlstra | fe5fba0 | 2015-03-17 12:39:10 +0100 | [diff] [blame] | 124 | static inline u64 ktime_get_tai_ns(void) |
| 125 | { |
| 126 | return ktime_to_ns(ktime_get_clocktai()); |
| 127 | } |
| 128 | |
Thomas Gleixner | f519b1a | 2014-07-16 21:05:04 +0000 | [diff] [blame] | 129 | static inline u64 ktime_get_raw_ns(void) |
| 130 | { |
| 131 | return ktime_to_ns(ktime_get_raw()); |
| 132 | } |
| 133 | |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 134 | extern u64 ktime_get_mono_fast_ns(void); |
Peter Zijlstra | f09cb9a | 2015-03-19 09:39:08 +0100 | [diff] [blame] | 135 | extern u64 ktime_get_raw_fast_ns(void); |
Thomas Gleixner | a3ed0e43 | 2018-04-25 15:33:38 +0200 | [diff] [blame] | 136 | extern u64 ktime_get_boot_fast_ns(void); |
Thomas Gleixner | 4c3711d | 2017-08-31 17:12:48 +0200 | [diff] [blame] | 137 | extern u64 ktime_get_real_fast_ns(void); |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 138 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 139 | /* |
Arnd Bergmann | 06aa376 | 2018-04-27 15:40:16 +0200 | [diff] [blame] | 140 | * timespec64/time64_t interfaces utilizing the ktime based ones |
| 141 | * for API completeness, these could be implemented more efficiently |
| 142 | * if needed. |
Thomas Gleixner | 48f18fd | 2014-07-16 21:04:57 +0000 | [diff] [blame] | 143 | */ |
Arnd Bergmann | fb7fcc9 | 2018-04-27 15:40:14 +0200 | [diff] [blame] | 144 | static inline void ktime_get_boottime_ts64(struct timespec64 *ts) |
Thomas Gleixner | a3ed0e43 | 2018-04-25 15:33:38 +0200 | [diff] [blame] | 145 | { |
| 146 | *ts = ktime_to_timespec64(ktime_get_boottime()); |
| 147 | } |
| 148 | |
Arnd Bergmann | 06aa376 | 2018-04-27 15:40:16 +0200 | [diff] [blame] | 149 | static inline void ktime_get_coarse_boottime_ts64(struct timespec64 *ts) |
| 150 | { |
| 151 | *ts = ktime_to_timespec64(ktime_get_coarse_boottime()); |
| 152 | } |
| 153 | |
| 154 | static inline time64_t ktime_get_boottime_seconds(void) |
| 155 | { |
| 156 | return ktime_divns(ktime_get_coarse_boottime(), NSEC_PER_SEC); |
| 157 | } |
| 158 | |
Arnd Bergmann | fb7fcc9 | 2018-04-27 15:40:14 +0200 | [diff] [blame] | 159 | static inline void ktime_get_clocktai_ts64(struct timespec64 *ts) |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 160 | { |
| 161 | *ts = ktime_to_timespec64(ktime_get_clocktai()); |
| 162 | } |
| 163 | |
Arnd Bergmann | 06aa376 | 2018-04-27 15:40:16 +0200 | [diff] [blame] | 164 | static inline void ktime_get_coarse_clocktai_ts64(struct timespec64 *ts) |
| 165 | { |
| 166 | *ts = ktime_to_timespec64(ktime_get_coarse_clocktai()); |
| 167 | } |
| 168 | |
| 169 | static inline time64_t ktime_get_clocktai_seconds(void) |
| 170 | { |
| 171 | return ktime_divns(ktime_get_coarse_clocktai(), NSEC_PER_SEC); |
| 172 | } |
| 173 | |
Thomas Gleixner | 48f18fd | 2014-07-16 21:04:57 +0000 | [diff] [blame] | 174 | /* |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 175 | * RTC specific |
| 176 | */ |
Xunlei Pang | 0fa88cb | 2015-04-01 20:34:38 -0700 | [diff] [blame] | 177 | extern bool timekeeping_rtc_skipsuspend(void); |
| 178 | extern bool timekeeping_rtc_skipresume(void); |
| 179 | |
pang.xunlei | 04d9089 | 2014-11-18 19:15:17 +0800 | [diff] [blame] | 180 | extern void timekeeping_inject_sleeptime64(struct timespec64 *delta); |
| 181 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 182 | /* |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 183 | * struct system_time_snapshot - simultaneous raw/real time capture with |
| 184 | * counter value |
| 185 | * @cycles: Clocksource counter value to produce the system times |
| 186 | * @real: Realtime system time |
| 187 | * @raw: Monotonic raw system time |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 188 | * @clock_was_set_seq: The sequence number of clock was set events |
| 189 | * @cs_was_changed_seq: The sequence number of clocksource change events |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 190 | */ |
| 191 | struct system_time_snapshot { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 192 | u64 cycles; |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 193 | ktime_t real; |
| 194 | ktime_t raw; |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 195 | unsigned int clock_was_set_seq; |
| 196 | u8 cs_was_changed_seq; |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | /* |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 200 | * struct system_device_crosststamp - system/device cross-timestamp |
| 201 | * (syncronized capture) |
| 202 | * @device: Device time |
| 203 | * @sys_realtime: Realtime simultaneous with device time |
| 204 | * @sys_monoraw: Monotonic raw simultaneous with device time |
| 205 | */ |
| 206 | struct system_device_crosststamp { |
| 207 | ktime_t device; |
| 208 | ktime_t sys_realtime; |
| 209 | ktime_t sys_monoraw; |
| 210 | }; |
| 211 | |
| 212 | /* |
| 213 | * struct system_counterval_t - system counter value with the pointer to the |
| 214 | * corresponding clocksource |
| 215 | * @cycles: System counter value |
| 216 | * @cs: Clocksource corresponding to system counter value. Used by |
| 217 | * timekeeping code to verify comparibility of two cycle values |
| 218 | */ |
| 219 | struct system_counterval_t { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 220 | u64 cycles; |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 221 | struct clocksource *cs; |
| 222 | }; |
| 223 | |
| 224 | /* |
| 225 | * Get cross timestamp between system clock and device clock |
| 226 | */ |
| 227 | extern int get_device_system_crosststamp( |
| 228 | int (*get_time_fn)(ktime_t *device_time, |
| 229 | struct system_counterval_t *system_counterval, |
| 230 | void *ctx), |
| 231 | void *ctx, |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 232 | struct system_time_snapshot *history, |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 233 | struct system_device_crosststamp *xtstamp); |
| 234 | |
| 235 | /* |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 236 | * Simultaneously snapshot realtime and monotonic raw clocks |
| 237 | */ |
| 238 | extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot); |
| 239 | |
| 240 | /* |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 241 | * Persistent clock related interfaces |
| 242 | */ |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 243 | extern int persistent_clock_is_local; |
| 244 | |
Xunlei Pang | 2ee9663 | 2015-04-01 20:34:22 -0700 | [diff] [blame] | 245 | extern void read_persistent_clock64(struct timespec64 *ts); |
Xunlei Pang | 9a806dd | 2015-04-01 20:34:21 -0700 | [diff] [blame] | 246 | extern void read_boot_clock64(struct timespec64 *ts); |
Xunlei Pang | 3c00a1f | 2015-04-01 20:34:23 -0700 | [diff] [blame] | 247 | extern int update_persistent_clock64(struct timespec64 now); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 248 | |
Arnd Bergmann | edca71f | 2018-04-27 15:40:13 +0200 | [diff] [blame] | 249 | /* |
| 250 | * deprecated aliases, don't use in new code |
| 251 | */ |
| 252 | #define getnstimeofday64(ts) ktime_get_real_ts64(ts) |
Arnd Bergmann | fb7fcc9 | 2018-04-27 15:40:14 +0200 | [diff] [blame] | 253 | #define get_monotonic_boottime64(ts) ktime_get_boottime_ts64(ts) |
| 254 | #define getrawmonotonic64(ts) ktime_get_raw_ts64(ts) |
| 255 | #define timekeeping_clocktai64(ts) ktime_get_clocktai_ts64(ts) |
| 256 | |
| 257 | static inline struct timespec64 current_kernel_time64(void) |
| 258 | { |
| 259 | struct timespec64 ts; |
| 260 | |
| 261 | ktime_get_coarse_real_ts64(&ts); |
| 262 | |
| 263 | return ts; |
| 264 | } |
| 265 | |
| 266 | static inline struct timespec64 get_monotonic_coarse64(void) |
| 267 | { |
| 268 | struct timespec64 ts; |
| 269 | |
| 270 | ktime_get_coarse_ts64(&ts); |
| 271 | |
| 272 | return ts; |
| 273 | } |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 274 | |
| 275 | #endif |