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 | */ |
pang.xunlei | 21f7eca | 2014-11-18 19:15:16 +0800 | [diff] [blame] | 18 | extern int do_settimeofday64(const struct timespec64 *ts); |
Baolin Wang | 86d3473 | 2016-04-08 14:02:12 +0800 | [diff] [blame] | 19 | extern int do_sys_settimeofday64(const struct timespec64 *tv, |
| 20 | const struct timezone *tz); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 21 | /* |
| 22 | * Kernel time accessors |
| 23 | */ |
Baolin Wang | 8758a24 | 2015-07-29 20:09:43 +0800 | [diff] [blame] | 24 | struct timespec64 current_kernel_time64(void); |
Baolin Wang | 8758a24 | 2015-07-29 20:09:43 +0800 | [diff] [blame] | 25 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 26 | /* |
Arnd Bergmann | 6546911 | 2017-10-19 13:14:49 +0200 | [diff] [blame^] | 27 | * timespec64 based interfaces |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 28 | */ |
John Stultz | 334334b | 2014-11-07 11:20:40 -0800 | [diff] [blame] | 29 | struct timespec64 get_monotonic_coarse64(void); |
John Stultz | cdba2ec | 2014-11-07 11:03:20 -0800 | [diff] [blame] | 30 | extern void getrawmonotonic64(struct timespec64 *ts); |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 31 | extern void ktime_get_ts64(struct timespec64 *ts); |
Heena Sirwani | 9e3680b | 2014-10-29 16:01:16 +0530 | [diff] [blame] | 32 | extern time64_t ktime_get_seconds(void); |
Heena Sirwani | dbe7aa6 | 2014-10-29 16:01:50 +0530 | [diff] [blame] | 33 | extern time64_t ktime_get_real_seconds(void); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 34 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 35 | extern int __getnstimeofday64(struct timespec64 *tv); |
| 36 | extern void getnstimeofday64(struct timespec64 *tv); |
John Stultz | d08c0cd | 2014-12-08 12:00:09 -0800 | [diff] [blame] | 37 | extern void getboottime64(struct timespec64 *ts); |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 38 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 39 | #define ktime_get_real_ts64(ts) getnstimeofday64(ts) |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * ktime_t based interfaces |
| 43 | */ |
Thomas Gleixner | 0077dc6 | 2014-07-16 21:04:13 +0000 | [diff] [blame] | 44 | |
| 45 | enum tk_offsets { |
| 46 | TK_OFFS_REAL, |
| 47 | TK_OFFS_BOOT, |
| 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); |
Thomas Gleixner | 9a6b519 | 2014-07-16 21:04:22 +0000 | [diff] [blame] | 54 | 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] | 55 | extern ktime_t ktime_get_raw(void); |
Harald Geyer | 6374f91 | 2015-04-07 11:12:35 +0000 | [diff] [blame] | 56 | extern u32 ktime_get_resolution_ns(void); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 57 | |
Thomas Gleixner | f5264d5 | 2014-07-16 21:04:14 +0000 | [diff] [blame] | 58 | /** |
| 59 | * ktime_get_real - get the real (wall-) time in ktime_t format |
| 60 | */ |
| 61 | static inline ktime_t ktime_get_real(void) |
| 62 | { |
| 63 | return ktime_get_with_offset(TK_OFFS_REAL); |
| 64 | } |
| 65 | |
Thomas Gleixner | b82c817 | 2014-07-16 21:04:16 +0000 | [diff] [blame] | 66 | /** |
| 67 | * ktime_get_boottime - Returns monotonic time since boot in ktime_t format |
| 68 | * |
| 69 | * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the |
| 70 | * time spent in suspend. |
| 71 | */ |
| 72 | static inline ktime_t ktime_get_boottime(void) |
| 73 | { |
| 74 | return ktime_get_with_offset(TK_OFFS_BOOT); |
| 75 | } |
| 76 | |
Thomas Gleixner | afab07c | 2014-07-16 21:04:17 +0000 | [diff] [blame] | 77 | /** |
| 78 | * ktime_get_clocktai - Returns the TAI time of day in ktime_t format |
| 79 | */ |
| 80 | static inline ktime_t ktime_get_clocktai(void) |
| 81 | { |
| 82 | return ktime_get_with_offset(TK_OFFS_TAI); |
| 83 | } |
| 84 | |
Thomas Gleixner | 9a6b519 | 2014-07-16 21:04:22 +0000 | [diff] [blame] | 85 | /** |
| 86 | * ktime_mono_to_real - Convert monotonic time to clock realtime |
| 87 | */ |
| 88 | static inline ktime_t ktime_mono_to_real(ktime_t mono) |
| 89 | { |
| 90 | return ktime_mono_to_any(mono, TK_OFFS_REAL); |
| 91 | } |
| 92 | |
Thomas Gleixner | 897994e | 2014-07-16 21:04:29 +0000 | [diff] [blame] | 93 | static inline u64 ktime_get_ns(void) |
| 94 | { |
| 95 | return ktime_to_ns(ktime_get()); |
| 96 | } |
| 97 | |
| 98 | static inline u64 ktime_get_real_ns(void) |
| 99 | { |
| 100 | return ktime_to_ns(ktime_get_real()); |
| 101 | } |
| 102 | |
| 103 | static inline u64 ktime_get_boot_ns(void) |
| 104 | { |
| 105 | return ktime_to_ns(ktime_get_boottime()); |
| 106 | } |
| 107 | |
Peter Zijlstra | fe5fba0 | 2015-03-17 12:39:10 +0100 | [diff] [blame] | 108 | static inline u64 ktime_get_tai_ns(void) |
| 109 | { |
| 110 | return ktime_to_ns(ktime_get_clocktai()); |
| 111 | } |
| 112 | |
Thomas Gleixner | f519b1a | 2014-07-16 21:05:04 +0000 | [diff] [blame] | 113 | static inline u64 ktime_get_raw_ns(void) |
| 114 | { |
| 115 | return ktime_to_ns(ktime_get_raw()); |
| 116 | } |
| 117 | |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 118 | extern u64 ktime_get_mono_fast_ns(void); |
Peter Zijlstra | f09cb9a | 2015-03-19 09:39:08 +0100 | [diff] [blame] | 119 | extern u64 ktime_get_raw_fast_ns(void); |
Joel Fernandes | 948a531 | 2016-11-28 14:35:22 -0800 | [diff] [blame] | 120 | extern u64 ktime_get_boot_fast_ns(void); |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 121 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 122 | /* |
Arnd Bergmann | 6546911 | 2017-10-19 13:14:49 +0200 | [diff] [blame^] | 123 | * timespec64 interfaces utilizing the ktime based ones |
Thomas Gleixner | 48f18fd | 2014-07-16 21:04:57 +0000 | [diff] [blame] | 124 | */ |
John Stultz | 2e0c78e | 2014-12-18 18:04:34 -0800 | [diff] [blame] | 125 | static inline void get_monotonic_boottime64(struct timespec64 *ts) |
| 126 | { |
| 127 | *ts = ktime_to_timespec64(ktime_get_boottime()); |
| 128 | } |
| 129 | |
Deepa Dinamani | 3c9c12f | 2017-03-26 12:04:14 -0700 | [diff] [blame] | 130 | static inline void timekeeping_clocktai64(struct timespec64 *ts) |
| 131 | { |
| 132 | *ts = ktime_to_timespec64(ktime_get_clocktai()); |
| 133 | } |
| 134 | |
Thomas Gleixner | 48f18fd | 2014-07-16 21:04:57 +0000 | [diff] [blame] | 135 | /* |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 136 | * RTC specific |
| 137 | */ |
Xunlei Pang | 0fa88cb | 2015-04-01 20:34:38 -0700 | [diff] [blame] | 138 | extern bool timekeeping_rtc_skipsuspend(void); |
| 139 | extern bool timekeeping_rtc_skipresume(void); |
| 140 | |
pang.xunlei | 04d9089 | 2014-11-18 19:15:17 +0800 | [diff] [blame] | 141 | extern void timekeeping_inject_sleeptime64(struct timespec64 *delta); |
| 142 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 143 | /* |
| 144 | * PPS accessor |
| 145 | */ |
Arnd Bergmann | 071eee4 | 2015-09-28 22:21:29 +0200 | [diff] [blame] | 146 | extern void ktime_get_raw_and_real_ts64(struct timespec64 *ts_raw, |
| 147 | struct timespec64 *ts_real); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 148 | |
| 149 | /* |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 150 | * struct system_time_snapshot - simultaneous raw/real time capture with |
| 151 | * counter value |
| 152 | * @cycles: Clocksource counter value to produce the system times |
| 153 | * @real: Realtime system time |
| 154 | * @raw: Monotonic raw system time |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 155 | * @clock_was_set_seq: The sequence number of clock was set events |
| 156 | * @cs_was_changed_seq: The sequence number of clocksource change events |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 157 | */ |
| 158 | struct system_time_snapshot { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 159 | u64 cycles; |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 160 | ktime_t real; |
| 161 | ktime_t raw; |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 162 | unsigned int clock_was_set_seq; |
| 163 | u8 cs_was_changed_seq; |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | /* |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 167 | * struct system_device_crosststamp - system/device cross-timestamp |
| 168 | * (syncronized capture) |
| 169 | * @device: Device time |
| 170 | * @sys_realtime: Realtime simultaneous with device time |
| 171 | * @sys_monoraw: Monotonic raw simultaneous with device time |
| 172 | */ |
| 173 | struct system_device_crosststamp { |
| 174 | ktime_t device; |
| 175 | ktime_t sys_realtime; |
| 176 | ktime_t sys_monoraw; |
| 177 | }; |
| 178 | |
| 179 | /* |
| 180 | * struct system_counterval_t - system counter value with the pointer to the |
| 181 | * corresponding clocksource |
| 182 | * @cycles: System counter value |
| 183 | * @cs: Clocksource corresponding to system counter value. Used by |
| 184 | * timekeeping code to verify comparibility of two cycle values |
| 185 | */ |
| 186 | struct system_counterval_t { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 187 | u64 cycles; |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 188 | struct clocksource *cs; |
| 189 | }; |
| 190 | |
| 191 | /* |
| 192 | * Get cross timestamp between system clock and device clock |
| 193 | */ |
| 194 | extern int get_device_system_crosststamp( |
| 195 | int (*get_time_fn)(ktime_t *device_time, |
| 196 | struct system_counterval_t *system_counterval, |
| 197 | void *ctx), |
| 198 | void *ctx, |
Christopher S. Hall | 2c756fe | 2016-02-22 03:15:23 -0800 | [diff] [blame] | 199 | struct system_time_snapshot *history, |
Christopher S. Hall | 8006c24 | 2016-02-22 03:15:22 -0800 | [diff] [blame] | 200 | struct system_device_crosststamp *xtstamp); |
| 201 | |
| 202 | /* |
Christopher S. Hall | 9da0f49 | 2016-02-22 03:15:20 -0800 | [diff] [blame] | 203 | * Simultaneously snapshot realtime and monotonic raw clocks |
| 204 | */ |
| 205 | extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot); |
| 206 | |
| 207 | /* |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 208 | * Persistent clock related interfaces |
| 209 | */ |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 210 | extern int persistent_clock_is_local; |
| 211 | |
Xunlei Pang | 2ee9663 | 2015-04-01 20:34:22 -0700 | [diff] [blame] | 212 | extern void read_persistent_clock64(struct timespec64 *ts); |
Xunlei Pang | 9a806dd | 2015-04-01 20:34:21 -0700 | [diff] [blame] | 213 | extern void read_boot_clock64(struct timespec64 *ts); |
Xunlei Pang | 3c00a1f | 2015-04-01 20:34:23 -0700 | [diff] [blame] | 214 | extern int update_persistent_clock64(struct timespec64 now); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 215 | |
| 216 | |
| 217 | #endif |