timekeeping.h 7.9 KB

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