timekeeping.h 7.9 KB

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