Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 1 | #ifndef _LINUX_TIMEKEEPING_H |
| 2 | #define _LINUX_TIMEKEEPING_H |
| 3 | |
Christoph Hellwig | b536fd5 | 2016-09-22 07:48:17 -0700 | [diff] [blame] | 4 | #include <linux/errno.h> |
Baolin Wang | 86d3473 | 2016-04-08 14:02:12 +0800 | [diff] [blame] | 5 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 6 | /* Included from linux/ktime.h */ |
| 7 | |
| 8 | void timekeeping_init(void); |
| 9 | extern int timekeeping_suspended; |
| 10 | |
Ingo Molnar | 93b5a9a | 2017-02-05 14:53:36 +0100 | [diff] [blame^] | 11 | /* Architecture timer tick functions: */ |
| 12 | extern void update_process_times(int user); |
| 13 | extern void xtime_update(unsigned long ticks); |
| 14 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 15 | /* |
| 16 | * Get and set timeofday |
| 17 | */ |
| 18 | extern void do_gettimeofday(struct timeval *tv); |
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); |
| 22 | static inline int do_sys_settimeofday(const struct timespec *tv, |
| 23 | const struct timezone *tz) |
| 24 | { |
| 25 | struct timespec64 ts64; |
| 26 | |
| 27 | if (!tv) |
John Stultz | dfc2507 | 2016-06-01 11:53:26 -0700 | [diff] [blame] | 28 | return do_sys_settimeofday64(NULL, tz); |
| 29 | |
| 30 | if (!timespec_valid(tv)) |
Baolin Wang | 86d3473 | 2016-04-08 14:02:12 +0800 | [diff] [blame] | 31 | return -EINVAL; |
| 32 | |
| 33 | ts64 = timespec_to_timespec64(*tv); |
| 34 | return do_sys_settimeofday64(&ts64, tz); |
| 35 | } |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * Kernel time accessors |
| 39 | */ |
| 40 | unsigned long get_seconds(void); |
Baolin Wang | 8758a24 | 2015-07-29 20:09:43 +0800 | [diff] [blame] | 41 | struct timespec64 current_kernel_time64(void); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 42 | /* does not take xtime_lock */ |
| 43 | struct timespec __current_kernel_time(void); |
| 44 | |
Baolin Wang | 8758a24 | 2015-07-29 20:09:43 +0800 | [diff] [blame] | 45 | static inline struct timespec current_kernel_time(void) |
| 46 | { |
| 47 | struct timespec64 now = current_kernel_time64(); |
| 48 | |
| 49 | return timespec64_to_timespec(now); |
| 50 | } |
| 51 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 52 | /* |
| 53 | * timespec based interfaces |
| 54 | */ |
John Stultz | 334334b | 2014-11-07 11:20:40 -0800 | [diff] [blame] | 55 | struct timespec64 get_monotonic_coarse64(void); |
John Stultz | cdba2ec | 2014-11-07 11:03:20 -0800 | [diff] [blame] | 56 | extern void getrawmonotonic64(struct timespec64 *ts); |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 57 | extern void ktime_get_ts64(struct timespec64 *ts); |
Heena Sirwani | 9e3680b | 2014-10-29 16:01:16 +0530 | [diff] [blame] | 58 | extern time64_t ktime_get_seconds(void); |
Heena Sirwani | dbe7aa6 | 2014-10-29 16:01:50 +0530 | [diff] [blame] | 59 | extern time64_t ktime_get_real_seconds(void); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 60 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 61 | extern int __getnstimeofday64(struct timespec64 *tv); |
| 62 | extern void getnstimeofday64(struct timespec64 *tv); |
John Stultz | d08c0cd | 2014-12-08 12:00:09 -0800 | [diff] [blame] | 63 | extern void getboottime64(struct timespec64 *ts); |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 64 | |
| 65 | #if BITS_PER_LONG == 64 |
pang.xunlei | 21f7eca | 2014-11-18 19:15:16 +0800 | [diff] [blame] | 66 | /** |
| 67 | * Deprecated. Use do_settimeofday64(). |
| 68 | */ |
| 69 | static inline int do_settimeofday(const struct timespec *ts) |
| 70 | { |
| 71 | return do_settimeofday64(ts); |
| 72 | } |
| 73 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 74 | static inline int __getnstimeofday(struct timespec *ts) |
| 75 | { |
| 76 | return __getnstimeofday64(ts); |
| 77 | } |
| 78 | |
| 79 | static inline void getnstimeofday(struct timespec *ts) |
| 80 | { |
| 81 | getnstimeofday64(ts); |
| 82 | } |
| 83 | |
| 84 | static inline void ktime_get_ts(struct timespec *ts) |
| 85 | { |
| 86 | ktime_get_ts64(ts); |
| 87 | } |
| 88 | |
| 89 | static inline void ktime_get_real_ts(struct timespec *ts) |
| 90 | { |
| 91 | getnstimeofday64(ts); |
| 92 | } |
| 93 | |
John Stultz | cdba2ec | 2014-11-07 11:03:20 -0800 | [diff] [blame] | 94 | static inline void getrawmonotonic(struct timespec *ts) |
| 95 | { |
| 96 | getrawmonotonic64(ts); |
| 97 | } |
| 98 | |
John Stultz | 334334b | 2014-11-07 11:20:40 -0800 | [diff] [blame] | 99 | static inline struct timespec get_monotonic_coarse(void) |
| 100 | { |
| 101 | return get_monotonic_coarse64(); |
| 102 | } |
John Stultz | d08c0cd | 2014-12-08 12:00:09 -0800 | [diff] [blame] | 103 | |
| 104 | static inline void getboottime(struct timespec *ts) |
| 105 | { |
| 106 | return getboottime64(ts); |
| 107 | } |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 108 | #else |
pang.xunlei | 21f7eca | 2014-11-18 19:15:16 +0800 | [diff] [blame] | 109 | /** |
| 110 | * Deprecated. Use do_settimeofday64(). |
| 111 | */ |
| 112 | static inline int do_settimeofday(const struct timespec *ts) |
| 113 | { |
| 114 | struct timespec64 ts64; |
| 115 | |
| 116 | ts64 = timespec_to_timespec64(*ts); |
| 117 | return do_settimeofday64(&ts64); |
| 118 | } |
| 119 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 120 | static inline int __getnstimeofday(struct timespec *ts) |
| 121 | { |
| 122 | struct timespec64 ts64; |
| 123 | int ret = __getnstimeofday64(&ts64); |
| 124 | |
| 125 | *ts = timespec64_to_timespec(ts64); |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | static inline void getnstimeofday(struct timespec *ts) |
| 130 | { |
| 131 | struct timespec64 ts64; |
| 132 | |
| 133 | getnstimeofday64(&ts64); |
| 134 | *ts = timespec64_to_timespec(ts64); |
| 135 | } |
| 136 | |
| 137 | static inline void ktime_get_ts(struct timespec *ts) |
| 138 | { |
| 139 | struct timespec64 ts64; |
| 140 | |
| 141 | ktime_get_ts64(&ts64); |
| 142 | *ts = timespec64_to_timespec(ts64); |
| 143 | } |
| 144 | |
| 145 | static inline void ktime_get_real_ts(struct timespec *ts) |
| 146 | { |
| 147 | struct timespec64 ts64; |
| 148 | |
| 149 | getnstimeofday64(&ts64); |
| 150 | *ts = timespec64_to_timespec(ts64); |
| 151 | } |
John Stultz | cdba2ec | 2014-11-07 11:03:20 -0800 | [diff] [blame] | 152 | |
| 153 | static inline void getrawmonotonic(struct timespec *ts) |
| 154 | { |
| 155 | struct timespec64 ts64; |
| 156 | |
| 157 | getrawmonotonic64(&ts64); |
| 158 | *ts = timespec64_to_timespec(ts64); |
| 159 | } |
John Stultz | 334334b | 2014-11-07 11:20:40 -0800 | [diff] [blame] | 160 | |
| 161 | static inline struct timespec get_monotonic_coarse(void) |
| 162 | { |
| 163 | return timespec64_to_timespec(get_monotonic_coarse64()); |
| 164 | } |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 165 | |
John Stultz | d08c0cd | 2014-12-08 12:00:09 -0800 | [diff] [blame] | 166 | static inline void getboottime(struct timespec *ts) |
| 167 | { |
| 168 | struct timespec64 ts64; |
| 169 | |
| 170 | getboottime64(&ts64); |
| 171 | *ts = timespec64_to_timespec(ts64); |
| 172 | } |
| 173 | #endif |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 174 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 175 | #define ktime_get_real_ts64(ts) getnstimeofday64(ts) |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * ktime_t based interfaces |
| 179 | */ |
Thomas Gleixner | 0077dc6 | 2014-07-16 21:04:13 +0000 | [diff] [blame] | 180 | |
| 181 | enum tk_offsets { |
| 182 | TK_OFFS_REAL, |
| 183 | TK_OFFS_BOOT, |
| 184 | TK_OFFS_TAI, |
| 185 | TK_OFFS_MAX, |
| 186 | }; |
| 187 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 188 | extern ktime_t ktime_get(void); |
Thomas Gleixner | 0077dc6 | 2014-07-16 21:04:13 +0000 | [diff] [blame] | 189 | extern ktime_t ktime_get_with_offset(enum tk_offsets offs); |
Thomas Gleixner | 9a6b519 | 2014-07-16 21:04:22 +0000 | [diff] [blame] | 190 | 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] | 191 | extern ktime_t ktime_get_raw(void); |
Harald Geyer | 6374f91 | 2015-04-07 11:12:35 +0000 | [diff] [blame] | 192 | extern u32 ktime_get_resolution_ns(void); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 193 | |
Thomas Gleixner | f5264d5 | 2014-07-16 21:04:14 +0000 | [diff] [blame] | 194 | /** |
| 195 | * ktime_get_real - get the real (wall-) time in ktime_t format |
| 196 | */ |
| 197 | static inline ktime_t ktime_get_real(void) |
| 198 | { |
| 199 | return ktime_get_with_offset(TK_OFFS_REAL); |
| 200 | } |
| 201 | |
Thomas Gleixner | b82c817 | 2014-07-16 21:04:16 +0000 | [diff] [blame] | 202 | /** |
| 203 | * ktime_get_boottime - Returns monotonic time since boot in ktime_t format |
| 204 | * |
| 205 | * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the |
| 206 | * time spent in suspend. |
| 207 | */ |
| 208 | static inline ktime_t ktime_get_boottime(void) |
| 209 | { |
| 210 | return ktime_get_with_offset(TK_OFFS_BOOT); |
| 211 | } |
| 212 | |
Thomas Gleixner | afab07c | 2014-07-16 21:04:17 +0000 | [diff] [blame] | 213 | /** |
| 214 | * ktime_get_clocktai - Returns the TAI time of day in ktime_t format |
| 215 | */ |
| 216 | static inline ktime_t ktime_get_clocktai(void) |
| 217 | { |
| 218 | return ktime_get_with_offset(TK_OFFS_TAI); |
| 219 | } |
| 220 | |
Thomas Gleixner | 9a6b519 | 2014-07-16 21:04:22 +0000 | [diff] [blame] | 221 | /** |
| 222 | * ktime_mono_to_real - Convert monotonic time to clock realtime |
| 223 | */ |
| 224 | static inline ktime_t ktime_mono_to_real(ktime_t mono) |
| 225 | { |
| 226 | return ktime_mono_to_any(mono, TK_OFFS_REAL); |
| 227 | } |
| 228 | |
Thomas Gleixner | 897994e | 2014-07-16 21:04:29 +0000 | [diff] [blame] | 229 | static inline u64 ktime_get_ns(void) |
| 230 | { |
| 231 | return ktime_to_ns(ktime_get()); |
| 232 | } |
| 233 | |
| 234 | static inline u64 ktime_get_real_ns(void) |
| 235 | { |
| 236 | return ktime_to_ns(ktime_get_real()); |
| 237 | } |
| 238 | |
| 239 | static inline u64 ktime_get_boot_ns(void) |
| 240 | { |
| 241 | return ktime_to_ns(ktime_get_boottime()); |
| 242 | } |
| 243 | |
Peter Zijlstra | fe5fba0 | 2015-03-17 12:39:10 +0100 | [diff] [blame] | 244 | static inline u64 ktime_get_tai_ns(void) |
| 245 | { |
| 246 | return ktime_to_ns(ktime_get_clocktai()); |
| 247 | } |
| 248 | |
Thomas Gleixner | f519b1a | 2014-07-16 21:05:04 +0000 | [diff] [blame] | 249 | static inline u64 ktime_get_raw_ns(void) |
| 250 | { |
| 251 | return ktime_to_ns(ktime_get_raw()); |
| 252 | } |
| 253 | |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 254 | extern u64 ktime_get_mono_fast_ns(void); |
Peter Zijlstra | f09cb9a | 2015-03-19 09:39:08 +0100 | [diff] [blame] | 255 | extern u64 ktime_get_raw_fast_ns(void); |
Joel Fernandes | 948a531 | 2016-11-28 14:35:22 -0800 | [diff] [blame] | 256 | extern u64 ktime_get_boot_fast_ns(void); |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 257 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 258 | /* |
Thomas Gleixner | 48f18fd | 2014-07-16 21:04:57 +0000 | [diff] [blame] | 259 | * Timespec interfaces utilizing the ktime based ones |
| 260 | */ |
| 261 | static inline void get_monotonic_boottime(struct timespec *ts) |
| 262 | { |
| 263 | *ts = ktime_to_timespec(ktime_get_boottime()); |
| 264 | } |
| 265 | |
John Stultz | 2e0c78e | 2014-12-18 18:04:34 -0800 | [diff] [blame] | 266 | static inline void get_monotonic_boottime64(struct timespec64 *ts) |
| 267 | { |
| 268 | *ts = ktime_to_timespec64(ktime_get_boottime()); |
| 269 | } |
| 270 | |
Thomas Gleixner | 61edec8 | 2014-07-16 21:05:01 +0000 | [diff] [blame] | 271 | static inline void timekeeping_clocktai(struct timespec *ts) |
| 272 | { |
| 273 | *ts = ktime_to_timespec(ktime_get_clocktai()); |
| 274 | } |
| 275 | |
Thomas Gleixner | 48f18fd | 2014-07-16 21:04:57 +0000 | [diff] [blame] | 276 | /* |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 277 | * RTC specific |
| 278 | */ |
Xunlei Pang | 0fa88cb | 2015-04-01 20:34:38 -0700 | [diff] [blame] | 279 | extern bool timekeeping_rtc_skipsuspend(void); |
| 280 | extern bool timekeeping_rtc_skipresume(void); |
| 281 | |
pang.xunlei | 04d9089 | 2014-11-18 19:15:17 +0800 | [diff] [blame] | 282 | extern void timekeeping_inject_sleeptime64(struct timespec64 *delta); |
| 283 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 284 | /* |
| 285 | * PPS accessor |
| 286 | */ |
Arnd Bergmann | 071eee4 | 2015-09-28 22:21:29 +0200 | [diff] [blame] | 287 | extern void ktime_get_raw_and_real_ts64(struct timespec64 *ts_raw, |
| 288 | struct timespec64 *ts_real); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 289 | |
| 290 | /* |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 291 | * struct system_time_snapshot - simultaneous raw/real time capture with |
| 292 | * counter value |
| 293 | * @cycles: Clocksource counter value to produce the system times |
| 294 | * @real: Realtime system time |
| 295 | * @raw: Monotonic raw system time |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 296 | * @clock_was_set_seq: The sequence number of clock was set events |
| 297 | * @cs_was_changed_seq: The sequence number of clocksource change events |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 298 | */ |
| 299 | struct system_time_snapshot { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 300 | u64 cycles; |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 301 | ktime_t real; |
| 302 | ktime_t raw; |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 303 | unsigned int clock_was_set_seq; |
| 304 | u8 cs_was_changed_seq; |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | /* |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 308 | * struct system_device_crosststamp - system/device cross-timestamp |
| 309 | * (syncronized capture) |
| 310 | * @device: Device time |
| 311 | * @sys_realtime: Realtime simultaneous with device time |
| 312 | * @sys_monoraw: Monotonic raw simultaneous with device time |
| 313 | */ |
| 314 | struct system_device_crosststamp { |
| 315 | ktime_t device; |
| 316 | ktime_t sys_realtime; |
| 317 | ktime_t sys_monoraw; |
| 318 | }; |
| 319 | |
| 320 | /* |
| 321 | * struct system_counterval_t - system counter value with the pointer to the |
| 322 | * corresponding clocksource |
| 323 | * @cycles: System counter value |
| 324 | * @cs: Clocksource corresponding to system counter value. Used by |
| 325 | * timekeeping code to verify comparibility of two cycle values |
| 326 | */ |
| 327 | struct system_counterval_t { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 328 | u64 cycles; |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 329 | struct clocksource *cs; |
| 330 | }; |
| 331 | |
| 332 | /* |
| 333 | * Get cross timestamp between system clock and device clock |
| 334 | */ |
| 335 | extern int get_device_system_crosststamp( |
| 336 | int (*get_time_fn)(ktime_t *device_time, |
| 337 | struct system_counterval_t *system_counterval, |
| 338 | void *ctx), |
| 339 | void *ctx, |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 340 | struct system_time_snapshot *history, |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 341 | struct system_device_crosststamp *xtstamp); |
| 342 | |
| 343 | /* |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 344 | * Simultaneously snapshot realtime and monotonic raw clocks |
| 345 | */ |
| 346 | extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot); |
| 347 | |
| 348 | /* |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 349 | * Persistent clock related interfaces |
| 350 | */ |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 351 | extern int persistent_clock_is_local; |
| 352 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 353 | extern void read_persistent_clock(struct timespec *ts); |
Xunlei Pang | 2ee9663 | 2015-04-01 20:34:22 -0700 | [diff] [blame] | 354 | extern void read_persistent_clock64(struct timespec64 *ts); |
Xunlei Pang | 9a806dd | 2015-04-01 20:34:21 -0700 | [diff] [blame] | 355 | extern void read_boot_clock64(struct timespec64 *ts); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 356 | extern int update_persistent_clock(struct timespec now); |
Xunlei Pang | 3c00a1f | 2015-04-01 20:34:23 -0700 | [diff] [blame] | 357 | extern int update_persistent_clock64(struct timespec64 now); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 358 | |
| 359 | |
| 360 | #endif |