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