Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 1 | #ifndef _LINUX_TIMEKEEPING_H |
| 2 | #define _LINUX_TIMEKEEPING_H |
| 3 | |
| 4 | /* Included from linux/ktime.h */ |
| 5 | |
| 6 | void timekeeping_init(void); |
| 7 | extern int timekeeping_suspended; |
| 8 | |
| 9 | /* |
| 10 | * Get and set timeofday |
| 11 | */ |
| 12 | extern void do_gettimeofday(struct timeval *tv); |
pang.xunlei | 21f7eca | 2014-11-18 19:15:16 +0800 | [diff] [blame] | 13 | extern int do_settimeofday64(const struct timespec64 *ts); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 14 | extern int do_sys_settimeofday(const struct timespec *tv, |
| 15 | const struct timezone *tz); |
| 16 | |
| 17 | /* |
| 18 | * Kernel time accessors |
| 19 | */ |
| 20 | unsigned long get_seconds(void); |
Baolin Wang | 8758a24 | 2015-07-29 20:09:43 +0800 | [diff] [blame] | 21 | struct timespec64 current_kernel_time64(void); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 22 | /* does not take xtime_lock */ |
| 23 | struct timespec __current_kernel_time(void); |
| 24 | |
Baolin Wang | 8758a24 | 2015-07-29 20:09:43 +0800 | [diff] [blame] | 25 | static inline struct timespec current_kernel_time(void) |
| 26 | { |
| 27 | struct timespec64 now = current_kernel_time64(); |
| 28 | |
| 29 | return timespec64_to_timespec(now); |
| 30 | } |
| 31 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 32 | /* |
| 33 | * timespec based interfaces |
| 34 | */ |
John Stultz | 334334b | 2014-11-07 11:20:40 -0800 | [diff] [blame] | 35 | struct timespec64 get_monotonic_coarse64(void); |
John Stultz | cdba2ec | 2014-11-07 11:03:20 -0800 | [diff] [blame] | 36 | extern void getrawmonotonic64(struct timespec64 *ts); |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 37 | extern void ktime_get_ts64(struct timespec64 *ts); |
Heena Sirwani | 9e3680b | 2014-10-29 16:01:16 +0530 | [diff] [blame] | 38 | extern time64_t ktime_get_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 | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 41 | extern int __getnstimeofday64(struct timespec64 *tv); |
| 42 | extern void getnstimeofday64(struct timespec64 *tv); |
John Stultz | d08c0cd | 2014-12-08 12:00:09 -0800 | [diff] [blame] | 43 | extern void getboottime64(struct timespec64 *ts); |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 44 | |
| 45 | #if BITS_PER_LONG == 64 |
pang.xunlei | 21f7eca | 2014-11-18 19:15:16 +0800 | [diff] [blame] | 46 | /** |
| 47 | * Deprecated. Use do_settimeofday64(). |
| 48 | */ |
| 49 | static inline int do_settimeofday(const struct timespec *ts) |
| 50 | { |
| 51 | return do_settimeofday64(ts); |
| 52 | } |
| 53 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 54 | static inline int __getnstimeofday(struct timespec *ts) |
| 55 | { |
| 56 | return __getnstimeofday64(ts); |
| 57 | } |
| 58 | |
| 59 | static inline void getnstimeofday(struct timespec *ts) |
| 60 | { |
| 61 | getnstimeofday64(ts); |
| 62 | } |
| 63 | |
| 64 | static inline void ktime_get_ts(struct timespec *ts) |
| 65 | { |
| 66 | ktime_get_ts64(ts); |
| 67 | } |
| 68 | |
| 69 | static inline void ktime_get_real_ts(struct timespec *ts) |
| 70 | { |
| 71 | getnstimeofday64(ts); |
| 72 | } |
| 73 | |
John Stultz | cdba2ec | 2014-11-07 11:03:20 -0800 | [diff] [blame] | 74 | static inline void getrawmonotonic(struct timespec *ts) |
| 75 | { |
| 76 | getrawmonotonic64(ts); |
| 77 | } |
| 78 | |
John Stultz | 334334b | 2014-11-07 11:20:40 -0800 | [diff] [blame] | 79 | static inline struct timespec get_monotonic_coarse(void) |
| 80 | { |
| 81 | return get_monotonic_coarse64(); |
| 82 | } |
John Stultz | d08c0cd | 2014-12-08 12:00:09 -0800 | [diff] [blame] | 83 | |
| 84 | static inline void getboottime(struct timespec *ts) |
| 85 | { |
| 86 | return getboottime64(ts); |
| 87 | } |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 88 | #else |
pang.xunlei | 21f7eca | 2014-11-18 19:15:16 +0800 | [diff] [blame] | 89 | /** |
| 90 | * Deprecated. Use do_settimeofday64(). |
| 91 | */ |
| 92 | static inline int do_settimeofday(const struct timespec *ts) |
| 93 | { |
| 94 | struct timespec64 ts64; |
| 95 | |
| 96 | ts64 = timespec_to_timespec64(*ts); |
| 97 | return do_settimeofday64(&ts64); |
| 98 | } |
| 99 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 100 | static inline int __getnstimeofday(struct timespec *ts) |
| 101 | { |
| 102 | struct timespec64 ts64; |
| 103 | int ret = __getnstimeofday64(&ts64); |
| 104 | |
| 105 | *ts = timespec64_to_timespec(ts64); |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static inline void getnstimeofday(struct timespec *ts) |
| 110 | { |
| 111 | struct timespec64 ts64; |
| 112 | |
| 113 | getnstimeofday64(&ts64); |
| 114 | *ts = timespec64_to_timespec(ts64); |
| 115 | } |
| 116 | |
| 117 | static inline void ktime_get_ts(struct timespec *ts) |
| 118 | { |
| 119 | struct timespec64 ts64; |
| 120 | |
| 121 | ktime_get_ts64(&ts64); |
| 122 | *ts = timespec64_to_timespec(ts64); |
| 123 | } |
| 124 | |
| 125 | static inline void ktime_get_real_ts(struct timespec *ts) |
| 126 | { |
| 127 | struct timespec64 ts64; |
| 128 | |
| 129 | getnstimeofday64(&ts64); |
| 130 | *ts = timespec64_to_timespec(ts64); |
| 131 | } |
John Stultz | cdba2ec | 2014-11-07 11:03:20 -0800 | [diff] [blame] | 132 | |
| 133 | static inline void getrawmonotonic(struct timespec *ts) |
| 134 | { |
| 135 | struct timespec64 ts64; |
| 136 | |
| 137 | getrawmonotonic64(&ts64); |
| 138 | *ts = timespec64_to_timespec(ts64); |
| 139 | } |
John Stultz | 334334b | 2014-11-07 11:20:40 -0800 | [diff] [blame] | 140 | |
| 141 | static inline struct timespec get_monotonic_coarse(void) |
| 142 | { |
| 143 | return timespec64_to_timespec(get_monotonic_coarse64()); |
| 144 | } |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 145 | |
John Stultz | d08c0cd | 2014-12-08 12:00:09 -0800 | [diff] [blame] | 146 | static inline void getboottime(struct timespec *ts) |
| 147 | { |
| 148 | struct timespec64 ts64; |
| 149 | |
| 150 | getboottime64(&ts64); |
| 151 | *ts = timespec64_to_timespec(ts64); |
| 152 | } |
| 153 | #endif |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 154 | |
Thomas Gleixner | d6d2989 | 2014-07-16 21:04:04 +0000 | [diff] [blame] | 155 | #define ktime_get_real_ts64(ts) getnstimeofday64(ts) |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * ktime_t based interfaces |
| 159 | */ |
Thomas Gleixner | 0077dc6 | 2014-07-16 21:04:13 +0000 | [diff] [blame] | 160 | |
| 161 | enum tk_offsets { |
| 162 | TK_OFFS_REAL, |
| 163 | TK_OFFS_BOOT, |
| 164 | TK_OFFS_TAI, |
| 165 | TK_OFFS_MAX, |
| 166 | }; |
| 167 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 168 | extern ktime_t ktime_get(void); |
Thomas Gleixner | 0077dc6 | 2014-07-16 21:04:13 +0000 | [diff] [blame] | 169 | extern ktime_t ktime_get_with_offset(enum tk_offsets offs); |
Thomas Gleixner | 9a6b519 | 2014-07-16 21:04:22 +0000 | [diff] [blame] | 170 | 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] | 171 | extern ktime_t ktime_get_raw(void); |
Harald Geyer | 6374f91 | 2015-04-07 11:12:35 +0000 | [diff] [blame] | 172 | extern u32 ktime_get_resolution_ns(void); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 173 | |
Thomas Gleixner | f5264d5 | 2014-07-16 21:04:14 +0000 | [diff] [blame] | 174 | /** |
| 175 | * ktime_get_real - get the real (wall-) time in ktime_t format |
| 176 | */ |
| 177 | static inline ktime_t ktime_get_real(void) |
| 178 | { |
| 179 | return ktime_get_with_offset(TK_OFFS_REAL); |
| 180 | } |
| 181 | |
Thomas Gleixner | b82c817 | 2014-07-16 21:04:16 +0000 | [diff] [blame] | 182 | /** |
| 183 | * ktime_get_boottime - Returns monotonic time since boot in ktime_t format |
| 184 | * |
| 185 | * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the |
| 186 | * time spent in suspend. |
| 187 | */ |
| 188 | static inline ktime_t ktime_get_boottime(void) |
| 189 | { |
| 190 | return ktime_get_with_offset(TK_OFFS_BOOT); |
| 191 | } |
| 192 | |
Thomas Gleixner | afab07c | 2014-07-16 21:04:17 +0000 | [diff] [blame] | 193 | /** |
| 194 | * ktime_get_clocktai - Returns the TAI time of day in ktime_t format |
| 195 | */ |
| 196 | static inline ktime_t ktime_get_clocktai(void) |
| 197 | { |
| 198 | return ktime_get_with_offset(TK_OFFS_TAI); |
| 199 | } |
| 200 | |
Thomas Gleixner | 9a6b519 | 2014-07-16 21:04:22 +0000 | [diff] [blame] | 201 | /** |
| 202 | * ktime_mono_to_real - Convert monotonic time to clock realtime |
| 203 | */ |
| 204 | static inline ktime_t ktime_mono_to_real(ktime_t mono) |
| 205 | { |
| 206 | return ktime_mono_to_any(mono, TK_OFFS_REAL); |
| 207 | } |
| 208 | |
Thomas Gleixner | 897994e | 2014-07-16 21:04:29 +0000 | [diff] [blame] | 209 | static inline u64 ktime_get_ns(void) |
| 210 | { |
| 211 | return ktime_to_ns(ktime_get()); |
| 212 | } |
| 213 | |
| 214 | static inline u64 ktime_get_real_ns(void) |
| 215 | { |
| 216 | return ktime_to_ns(ktime_get_real()); |
| 217 | } |
| 218 | |
| 219 | static inline u64 ktime_get_boot_ns(void) |
| 220 | { |
| 221 | return ktime_to_ns(ktime_get_boottime()); |
| 222 | } |
| 223 | |
Peter Zijlstra | fe5fba0 | 2015-03-17 12:39:10 +0100 | [diff] [blame] | 224 | static inline u64 ktime_get_tai_ns(void) |
| 225 | { |
| 226 | return ktime_to_ns(ktime_get_clocktai()); |
| 227 | } |
| 228 | |
Thomas Gleixner | f519b1a | 2014-07-16 21:05:04 +0000 | [diff] [blame] | 229 | static inline u64 ktime_get_raw_ns(void) |
| 230 | { |
| 231 | return ktime_to_ns(ktime_get_raw()); |
| 232 | } |
| 233 | |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 234 | extern u64 ktime_get_mono_fast_ns(void); |
Peter Zijlstra | f09cb9a | 2015-03-19 09:39:08 +0100 | [diff] [blame] | 235 | extern u64 ktime_get_raw_fast_ns(void); |
Thomas Gleixner | 4396e05 | 2014-07-16 21:05:23 +0000 | [diff] [blame] | 236 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 237 | /* |
Thomas Gleixner | 48f18fd | 2014-07-16 21:04:57 +0000 | [diff] [blame] | 238 | * Timespec interfaces utilizing the ktime based ones |
| 239 | */ |
| 240 | static inline void get_monotonic_boottime(struct timespec *ts) |
| 241 | { |
| 242 | *ts = ktime_to_timespec(ktime_get_boottime()); |
| 243 | } |
| 244 | |
John Stultz | 2e0c78e | 2014-12-18 18:04:34 -0800 | [diff] [blame] | 245 | static inline void get_monotonic_boottime64(struct timespec64 *ts) |
| 246 | { |
| 247 | *ts = ktime_to_timespec64(ktime_get_boottime()); |
| 248 | } |
| 249 | |
Thomas Gleixner | 61edec8 | 2014-07-16 21:05:01 +0000 | [diff] [blame] | 250 | static inline void timekeeping_clocktai(struct timespec *ts) |
| 251 | { |
| 252 | *ts = ktime_to_timespec(ktime_get_clocktai()); |
| 253 | } |
| 254 | |
Thomas Gleixner | 48f18fd | 2014-07-16 21:04:57 +0000 | [diff] [blame] | 255 | /* |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 256 | * RTC specific |
| 257 | */ |
Xunlei Pang | 0fa88cb | 2015-04-01 20:34:38 -0700 | [diff] [blame] | 258 | extern bool timekeeping_rtc_skipsuspend(void); |
| 259 | extern bool timekeeping_rtc_skipresume(void); |
| 260 | |
pang.xunlei | 04d9089 | 2014-11-18 19:15:17 +0800 | [diff] [blame] | 261 | extern void timekeeping_inject_sleeptime64(struct timespec64 *delta); |
| 262 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 263 | /* |
| 264 | * PPS accessor |
| 265 | */ |
| 266 | extern void getnstime_raw_and_real(struct timespec *ts_raw, |
| 267 | struct timespec *ts_real); |
| 268 | |
| 269 | /* |
| 270 | * Persistent clock related interfaces |
| 271 | */ |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 272 | extern int persistent_clock_is_local; |
| 273 | |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 274 | extern void read_persistent_clock(struct timespec *ts); |
Xunlei Pang | 2ee9663 | 2015-04-01 20:34:22 -0700 | [diff] [blame] | 275 | extern void read_persistent_clock64(struct timespec64 *ts); |
Xunlei Pang | 9a806dd | 2015-04-01 20:34:21 -0700 | [diff] [blame] | 276 | extern void read_boot_clock64(struct timespec64 *ts); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 277 | extern int update_persistent_clock(struct timespec now); |
Xunlei Pang | 3c00a1f | 2015-04-01 20:34:23 -0700 | [diff] [blame] | 278 | extern int update_persistent_clock64(struct timespec64 now); |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 279 | |
| 280 | |
| 281 | #endif |