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