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