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