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