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 48 /* Multigrain timestamp interfaces */ 49 extern void ktime_get_coarse_real_ts64_mg(struct timespec64 *ts); 50 extern void ktime_get_real_ts64_mg(struct timespec64 *ts); 51 52 void getboottime64(struct timespec64 *ts); 53 54 /* 55 * time64_t base interfaces 56 */ 57 extern time64_t ktime_get_seconds(void); 58 extern time64_t __ktime_get_real_seconds(void); 59 extern time64_t ktime_get_real_seconds(void); 60 61 /* 62 * ktime_t based interfaces 63 */ 64 65 enum tk_offsets { 66 TK_OFFS_REAL, 67 TK_OFFS_BOOT, 68 TK_OFFS_TAI, 69 TK_OFFS_MAX, 70 }; 71 72 extern ktime_t ktime_get(void); 73 extern ktime_t ktime_get_with_offset(enum tk_offsets offs); 74 extern ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs); 75 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs); 76 extern ktime_t ktime_get_raw(void); 77 extern u32 ktime_get_resolution_ns(void); 78 79 /** 80 * ktime_get_real - get the real (wall-) time in ktime_t format 81 * 82 * Returns: real (wall) time in ktime_t format 83 */ 84 static inline ktime_t ktime_get_real(void) 85 { 86 return ktime_get_with_offset(TK_OFFS_REAL); 87 } 88 89 static inline ktime_t ktime_get_coarse_real(void) 90 { 91 return ktime_get_coarse_with_offset(TK_OFFS_REAL); 92 } 93 94 /** 95 * ktime_get_boottime - Get monotonic time since boot in ktime_t format 96 * 97 * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the 98 * time spent in suspend. 99 * 100 * Returns: monotonic time since boot in ktime_t format 101 */ 102 static inline ktime_t ktime_get_boottime(void) 103 { 104 return ktime_get_with_offset(TK_OFFS_BOOT); 105 } 106 107 static inline ktime_t ktime_get_coarse_boottime(void) 108 { 109 return ktime_get_coarse_with_offset(TK_OFFS_BOOT); 110 } 111 112 /** 113 * ktime_get_clocktai - Get the TAI time of day in ktime_t format 114 * 115 * Returns: the TAI time of day in ktime_t format 116 */ 117 static inline ktime_t ktime_get_clocktai(void) 118 { 119 return ktime_get_with_offset(TK_OFFS_TAI); 120 } 121 122 static inline ktime_t ktime_get_coarse_clocktai(void) 123 { 124 return ktime_get_coarse_with_offset(TK_OFFS_TAI); 125 } 126 127 static inline ktime_t ktime_get_coarse(void) 128 { 129 struct timespec64 ts; 130 131 ktime_get_coarse_ts64(&ts); 132 return timespec64_to_ktime(ts); 133 } 134 135 static inline u64 ktime_get_coarse_ns(void) 136 { 137 return ktime_to_ns(ktime_get_coarse()); 138 } 139 140 static inline u64 ktime_get_coarse_real_ns(void) 141 { 142 return ktime_to_ns(ktime_get_coarse_real()); 143 } 144 145 static inline u64 ktime_get_coarse_boottime_ns(void) 146 { 147 return ktime_to_ns(ktime_get_coarse_boottime()); 148 } 149 150 static inline u64 ktime_get_coarse_clocktai_ns(void) 151 { 152 return ktime_to_ns(ktime_get_coarse_clocktai()); 153 } 154 155 /** 156 * ktime_mono_to_real - Convert monotonic time to clock realtime 157 * @mono: monotonic time to convert 158 * 159 * Returns: time converted to realtime clock 160 */ 161 static inline ktime_t ktime_mono_to_real(ktime_t mono) 162 { 163 return ktime_mono_to_any(mono, TK_OFFS_REAL); 164 } 165 166 /** 167 * ktime_get_ns - Get the current time in nanoseconds 168 * 169 * Returns: current time converted to nanoseconds 170 */ 171 static inline u64 ktime_get_ns(void) 172 { 173 return ktime_to_ns(ktime_get()); 174 } 175 176 /** 177 * ktime_get_real_ns - Get the current real/wall time in nanoseconds 178 * 179 * Returns: current real time converted to nanoseconds 180 */ 181 static inline u64 ktime_get_real_ns(void) 182 { 183 return ktime_to_ns(ktime_get_real()); 184 } 185 186 /** 187 * ktime_get_boottime_ns - Get the monotonic time since boot in nanoseconds 188 * 189 * Returns: current boottime converted to nanoseconds 190 */ 191 static inline u64 ktime_get_boottime_ns(void) 192 { 193 return ktime_to_ns(ktime_get_boottime()); 194 } 195 196 /** 197 * ktime_get_clocktai_ns - Get the current TAI time of day in nanoseconds 198 * 199 * Returns: current TAI time converted to nanoseconds 200 */ 201 static inline u64 ktime_get_clocktai_ns(void) 202 { 203 return ktime_to_ns(ktime_get_clocktai()); 204 } 205 206 /** 207 * ktime_get_raw_ns - Get the raw monotonic time in nanoseconds 208 * 209 * Returns: current raw monotonic time converted to nanoseconds 210 */ 211 static inline u64 ktime_get_raw_ns(void) 212 { 213 return ktime_to_ns(ktime_get_raw()); 214 } 215 216 extern u64 ktime_get_mono_fast_ns(void); 217 extern u64 ktime_get_raw_fast_ns(void); 218 extern u64 ktime_get_boot_fast_ns(void); 219 extern u64 ktime_get_tai_fast_ns(void); 220 extern u64 ktime_get_real_fast_ns(void); 221 222 /* 223 * timespec64/time64_t interfaces utilizing the ktime based ones 224 * for API completeness, these could be implemented more efficiently 225 * if needed. 226 */ 227 static inline void ktime_get_boottime_ts64(struct timespec64 *ts) 228 { 229 *ts = ktime_to_timespec64(ktime_get_boottime()); 230 } 231 232 static inline void ktime_get_coarse_boottime_ts64(struct timespec64 *ts) 233 { 234 *ts = ktime_to_timespec64(ktime_get_coarse_boottime()); 235 } 236 237 static inline time64_t ktime_get_boottime_seconds(void) 238 { 239 return ktime_divns(ktime_get_coarse_boottime(), NSEC_PER_SEC); 240 } 241 242 static inline void ktime_get_clocktai_ts64(struct timespec64 *ts) 243 { 244 *ts = ktime_to_timespec64(ktime_get_clocktai()); 245 } 246 247 static inline void ktime_get_coarse_clocktai_ts64(struct timespec64 *ts) 248 { 249 *ts = ktime_to_timespec64(ktime_get_coarse_clocktai()); 250 } 251 252 static inline time64_t ktime_get_clocktai_seconds(void) 253 { 254 return ktime_divns(ktime_get_coarse_clocktai(), NSEC_PER_SEC); 255 } 256 257 /* 258 * RTC specific 259 */ 260 extern bool timekeeping_rtc_skipsuspend(void); 261 extern bool timekeeping_rtc_skipresume(void); 262 263 extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta); 264 265 /** 266 * struct ktime_timestamps - Simultaneous mono/boot/real timestamps 267 * @mono: Monotonic timestamp 268 * @boot: Boottime timestamp 269 * @real: Realtime timestamp 270 */ 271 struct ktime_timestamps { 272 u64 mono; 273 u64 boot; 274 u64 real; 275 }; 276 277 /** 278 * struct system_time_snapshot - simultaneous raw/real time capture with 279 * counter value 280 * @cycles: Clocksource counter value to produce the system times 281 * @real: Realtime system time 282 * @raw: Monotonic raw system time 283 * @cs_id: Clocksource ID 284 * @clock_was_set_seq: The sequence number of clock-was-set events 285 * @cs_was_changed_seq: The sequence number of clocksource change events 286 */ 287 struct system_time_snapshot { 288 u64 cycles; 289 ktime_t real; 290 ktime_t raw; 291 enum clocksource_ids cs_id; 292 unsigned int clock_was_set_seq; 293 u8 cs_was_changed_seq; 294 }; 295 296 /** 297 * struct system_device_crosststamp - system/device cross-timestamp 298 * (synchronized capture) 299 * @device: Device time 300 * @sys_realtime: Realtime simultaneous with device time 301 * @sys_monoraw: Monotonic raw simultaneous with device time 302 */ 303 struct system_device_crosststamp { 304 ktime_t device; 305 ktime_t sys_realtime; 306 ktime_t sys_monoraw; 307 }; 308 309 /** 310 * struct system_counterval_t - system counter value with the ID of the 311 * corresponding clocksource 312 * @cycles: System counter value 313 * @cs_id: Clocksource ID corresponding to system counter value. Used by 314 * timekeeping code to verify comparability of two cycle values. 315 * The default ID, CSID_GENERIC, does not identify a specific 316 * clocksource. 317 * @use_nsecs: @cycles is in nanoseconds. 318 */ 319 struct system_counterval_t { 320 u64 cycles; 321 enum clocksource_ids cs_id; 322 bool use_nsecs; 323 }; 324 325 extern bool ktime_real_to_base_clock(ktime_t treal, 326 enum clocksource_ids base_id, u64 *cycles); 327 extern bool timekeeping_clocksource_has_base(enum clocksource_ids id); 328 329 /* 330 * Get cross timestamp between system clock and device clock 331 */ 332 extern int get_device_system_crosststamp( 333 int (*get_time_fn)(ktime_t *device_time, 334 struct system_counterval_t *system_counterval, 335 void *ctx), 336 void *ctx, 337 struct system_time_snapshot *history, 338 struct system_device_crosststamp *xtstamp); 339 340 /* 341 * Simultaneously snapshot realtime and monotonic raw clocks 342 */ 343 extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot); 344 345 /* NMI safe mono/boot/realtime timestamps */ 346 extern void ktime_get_fast_timestamps(struct ktime_timestamps *snap); 347 348 /* 349 * Persistent clock related interfaces 350 */ 351 extern int persistent_clock_is_local; 352 353 extern void read_persistent_clock64(struct timespec64 *ts); 354 void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock, 355 struct timespec64 *boot_offset); 356 #ifdef CONFIG_GENERIC_CMOS_UPDATE 357 extern int update_persistent_clock64(struct timespec64 now); 358 #endif 359 360 #endif 361