1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_TIMEKEEPING_H 3 #define _LINUX_TIMEKEEPING_H 4 5 #include <linux/errno.h> 6 #include <linux/clocksource_ids.h> 7 #include <linux/ktime.h> 8 9 /* Included from linux/ktime.h */ 10 11 void timekeeping_init(void); 12 extern int timekeeping_suspended; 13 14 /* Architecture timer tick functions: */ 15 extern void legacy_timer_tick(unsigned long ticks); 16 17 /* 18 * Get and set timeofday 19 */ 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 /* 25 * ktime_get() family - read the current time in a multitude of ways. 26 * 27 * The default time reference is CLOCK_MONOTONIC, starting at 28 * boot time but not counting the time spent in suspend. 29 * For other references, use the functions with "real", "clocktai", 30 * "boottime" and "raw" suffixes. 31 * 32 * To get the time in a different format, use the ones with 33 * "ns", "ts64" and "seconds" suffix. 34 * 35 * See Documentation/core-api/timekeeping.rst for more details. 36 */ 37 38 39 /* 40 * timespec64 based interfaces 41 */ 42 extern void ktime_get_raw_ts64(struct timespec64 *ts); 43 extern void ktime_get_ts64(struct timespec64 *ts); 44 extern void ktime_get_real_ts64(struct timespec64 *tv); 45 extern void ktime_get_coarse_ts64(struct timespec64 *ts); 46 extern void ktime_get_coarse_real_ts64(struct timespec64 *ts); 47 extern void ktime_get_clock_ts64(clockid_t id, struct timespec64 *ts); 48 49 /* Multigrain timestamp interfaces */ 50 extern void ktime_get_coarse_real_ts64_mg(struct timespec64 *ts); 51 extern void ktime_get_real_ts64_mg(struct timespec64 *ts); 52 extern unsigned long timekeeping_get_mg_floor_swaps(void); 53 54 void getboottime64(struct timespec64 *ts); 55 56 /* 57 * time64_t base interfaces 58 */ 59 extern time64_t ktime_get_seconds(void); 60 extern time64_t __ktime_get_real_seconds(void); 61 extern time64_t ktime_get_real_seconds(void); 62 63 /* 64 * ktime_t based interfaces 65 */ 66 67 enum tk_offsets { 68 TK_OFFS_REAL, 69 TK_OFFS_BOOT, 70 TK_OFFS_TAI, 71 TK_OFFS_MAX, 72 }; 73 74 extern ktime_t ktime_get(void); 75 extern ktime_t ktime_get_with_offset(enum tk_offsets offs); 76 extern ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs); 77 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs); 78 extern ktime_t ktime_get_raw(void); 79 extern u32 ktime_get_resolution_ns(void); 80 81 /** 82 * ktime_get_real - get the real (wall-) time in ktime_t format 83 * 84 * Returns: real (wall) time in ktime_t format 85 */ 86 static inline ktime_t ktime_get_real(void) 87 { 88 return ktime_get_with_offset(TK_OFFS_REAL); 89 } 90 91 static inline ktime_t ktime_get_coarse_real(void) 92 { 93 return ktime_get_coarse_with_offset(TK_OFFS_REAL); 94 } 95 96 /** 97 * ktime_get_boottime - Get monotonic time since boot in ktime_t format 98 * 99 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the 100 * time spent in suspend. 101 * 102 * Returns: monotonic time since boot in ktime_t format 103 */ 104 static inline ktime_t ktime_get_boottime(void) 105 { 106 return ktime_get_with_offset(TK_OFFS_BOOT); 107 } 108 109 static inline ktime_t ktime_get_coarse_boottime(void) 110 { 111 return ktime_get_coarse_with_offset(TK_OFFS_BOOT); 112 } 113 114 /** 115 * ktime_get_clocktai - Get the TAI time of day in ktime_t format 116 * 117 * Returns: the TAI time of day in ktime_t format 118 */ 119 static inline ktime_t ktime_get_clocktai(void) 120 { 121 return ktime_get_with_offset(TK_OFFS_TAI); 122 } 123 124 static inline ktime_t ktime_get_coarse_clocktai(void) 125 { 126 return ktime_get_coarse_with_offset(TK_OFFS_TAI); 127 } 128 129 static inline ktime_t ktime_get_coarse(void) 130 { 131 struct timespec64 ts; 132 133 ktime_get_coarse_ts64(&ts); 134 return timespec64_to_ktime(ts); 135 } 136 137 static inline u64 ktime_get_coarse_ns(void) 138 { 139 return ktime_to_ns(ktime_get_coarse()); 140 } 141 142 static inline u64 ktime_get_coarse_real_ns(void) 143 { 144 return ktime_to_ns(ktime_get_coarse_real()); 145 } 146 147 static inline u64 ktime_get_coarse_boottime_ns(void) 148 { 149 return ktime_to_ns(ktime_get_coarse_boottime()); 150 } 151 152 static inline u64 ktime_get_coarse_clocktai_ns(void) 153 { 154 return ktime_to_ns(ktime_get_coarse_clocktai()); 155 } 156 157 /** 158 * ktime_mono_to_real - Convert monotonic time to clock realtime 159 * @mono: monotonic time to convert 160 * 161 * Returns: time converted to realtime clock 162 */ 163 static inline ktime_t ktime_mono_to_real(ktime_t mono) 164 { 165 return ktime_mono_to_any(mono, TK_OFFS_REAL); 166 } 167 168 /** 169 * ktime_get_ns - Get the current time in nanoseconds 170 * 171 * Returns: current time converted to nanoseconds 172 */ 173 static inline u64 ktime_get_ns(void) 174 { 175 return ktime_to_ns(ktime_get()); 176 } 177 178 /** 179 * ktime_get_real_ns - Get the current real/wall time in nanoseconds 180 * 181 * Returns: current real time converted to nanoseconds 182 */ 183 static inline u64 ktime_get_real_ns(void) 184 { 185 return ktime_to_ns(ktime_get_real()); 186 } 187 188 /** 189 * ktime_get_boottime_ns - Get the monotonic time since boot in nanoseconds 190 * 191 * Returns: current boottime converted to nanoseconds 192 */ 193 static inline u64 ktime_get_boottime_ns(void) 194 { 195 return ktime_to_ns(ktime_get_boottime()); 196 } 197 198 /** 199 * ktime_get_clocktai_ns - Get the current TAI time of day in nanoseconds 200 * 201 * Returns: current TAI time converted to nanoseconds 202 */ 203 static inline u64 ktime_get_clocktai_ns(void) 204 { 205 return ktime_to_ns(ktime_get_clocktai()); 206 } 207 208 /** 209 * ktime_get_raw_ns - Get the raw monotonic time in nanoseconds 210 * 211 * Returns: current raw monotonic time converted to nanoseconds 212 */ 213 static inline u64 ktime_get_raw_ns(void) 214 { 215 return ktime_to_ns(ktime_get_raw()); 216 } 217 218 extern u64 ktime_get_mono_fast_ns(void); 219 extern u64 ktime_get_raw_fast_ns(void); 220 extern u64 ktime_get_boot_fast_ns(void); 221 extern u64 ktime_get_tai_fast_ns(void); 222 extern u64 ktime_get_real_fast_ns(void); 223 224 /* 225 * timespec64/time64_t interfaces utilizing the ktime based ones 226 * for API completeness, these could be implemented more efficiently 227 * if needed. 228 */ 229 static inline void ktime_get_boottime_ts64(struct timespec64 *ts) 230 { 231 *ts = ktime_to_timespec64(ktime_get_boottime()); 232 } 233 234 static inline void ktime_get_coarse_boottime_ts64(struct timespec64 *ts) 235 { 236 *ts = ktime_to_timespec64(ktime_get_coarse_boottime()); 237 } 238 239 static inline time64_t ktime_get_boottime_seconds(void) 240 { 241 return ktime_divns(ktime_get_coarse_boottime(), NSEC_PER_SEC); 242 } 243 244 static inline void ktime_get_clocktai_ts64(struct timespec64 *ts) 245 { 246 *ts = ktime_to_timespec64(ktime_get_clocktai()); 247 } 248 249 static inline void ktime_get_coarse_clocktai_ts64(struct timespec64 *ts) 250 { 251 *ts = ktime_to_timespec64(ktime_get_coarse_clocktai()); 252 } 253 254 static inline time64_t ktime_get_clocktai_seconds(void) 255 { 256 return ktime_divns(ktime_get_coarse_clocktai(), NSEC_PER_SEC); 257 } 258 259 /* 260 * RTC specific 261 */ 262 extern bool timekeeping_rtc_skipsuspend(void); 263 extern bool timekeeping_rtc_skipresume(void); 264 265 extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta); 266 267 /* 268 * Auxiliary clock interfaces 269 */ 270 #ifdef CONFIG_POSIX_AUX_CLOCKS 271 extern bool ktime_get_aux(clockid_t id, ktime_t *kt); 272 extern bool ktime_get_aux_ts64(clockid_t id, struct timespec64 *kt); 273 #else 274 static inline bool ktime_get_aux(clockid_t id, ktime_t *kt) { return false; } 275 static inline bool ktime_get_aux_ts64(clockid_t id, struct timespec64 *kt) { return false; } 276 #endif 277 278 /** 279 * struct system_time_snapshot - Simultaneous time capture of CLOCK_MONOTONIC_RAW, 280 * a selected CLOCK_* and the clocksource counter value 281 * @cycles: Clocksource counter value to produce the system times 282 * @hw_cycles: For derived clocksources, the hardware counter value from 283 * which @cycles was derived 284 * @systime: The system time of the selected CLOCK ID 285 * @monoraw: Monotonic raw system time 286 * @cs_id: Clocksource ID 287 * @hw_csid: Clocksource ID of the underlying hardware counter for derived 288 * clocksources which implement the read_snapshot() callback. 289 * @clock_was_set_seq: The sequence number of clock-was-set events 290 * @cs_was_changed_seq: The sequence number of clocksource change events 291 * @valid: True if the snapshot is valid 292 */ 293 struct system_time_snapshot { 294 u64 cycles; 295 u64 hw_cycles; 296 ktime_t systime; 297 ktime_t monoraw; 298 enum clocksource_ids cs_id; 299 enum clocksource_ids hw_csid; 300 unsigned int clock_was_set_seq; 301 u8 cs_was_changed_seq; 302 u8 valid; 303 }; 304 305 /** 306 * struct system_counterval_t - system counter value with the ID of the 307 * corresponding clocksource 308 * @cycles: System counter value 309 * @cs_id: Clocksource ID corresponding to system counter value. Used by 310 * timekeeping code to verify comparability of two cycle values. 311 * The default ID, CSID_GENERIC, does not identify a specific 312 * clocksource. 313 * @use_nsecs: @cycles is in nanoseconds. 314 */ 315 struct system_counterval_t { 316 u64 cycles; 317 enum clocksource_ids cs_id; 318 bool use_nsecs; 319 }; 320 321 /** 322 * struct system_device_crosststamp - system/device cross-timestamp 323 * (synchronized capture) 324 * @clock_id: System time Clock ID to capture 325 * @device: Device time 326 * @sys_counter: Clocksource counter value simultaneous with device time 327 * @sys_systime: System time for @clock_id 328 * @sys_monoraw: Monotonic raw simultaneous with device time 329 */ 330 struct system_device_crosststamp { 331 clockid_t clock_id; 332 ktime_t device; 333 struct system_counterval_t sys_counter; 334 ktime_t sys_systime; 335 ktime_t sys_monoraw; 336 }; 337 338 extern bool ktime_real_to_base_clock(ktime_t treal, 339 enum clocksource_ids base_id, u64 *cycles); 340 extern bool timekeeping_clocksource_has_base(enum clocksource_ids id); 341 342 /* 343 * Get cross timestamp between system clock and device clock 344 */ 345 extern int get_device_system_crosststamp( 346 int (*get_time_fn)(ktime_t *device_time, 347 struct system_counterval_t *system_counterval, 348 void *ctx), 349 void *ctx, 350 struct system_time_snapshot *history, 351 struct system_device_crosststamp *xtstamp); 352 353 /* 354 * Simultaneously snapshot a given clock with MONOTONIC_RAW and the underlying 355 * clocksource counter value. 356 */ 357 extern void ktime_get_snapshot_id(clockid_t clock_id, struct system_time_snapshot *systime_snapshot); 358 359 /* 360 * Persistent clock related interfaces 361 */ 362 extern int persistent_clock_is_local; 363 364 extern void read_persistent_clock64(struct timespec64 *ts); 365 void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock, 366 struct timespec64 *boot_offset); 367 #ifdef CONFIG_GENERIC_CMOS_UPDATE 368 extern int update_persistent_clock64(struct timespec64 now); 369 #endif 370 371 #endif 372