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