timekeeping.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #ifndef _LINUX_TIMEKEEPING_H
  2. #define _LINUX_TIMEKEEPING_H
  3. /* Included from linux/ktime.h */
  4. void timekeeping_init(void);
  5. extern int timekeeping_suspended;
  6. /*
  7. * Get and set timeofday
  8. */
  9. extern void do_gettimeofday(struct timeval *tv);
  10. extern int do_settimeofday64(const struct timespec64 *ts);
  11. extern int do_sys_settimeofday(const struct timespec *tv,
  12. const struct timezone *tz);
  13. /*
  14. * Kernel time accessors
  15. */
  16. unsigned long get_seconds(void);
  17. struct timespec64 current_kernel_time64(void);
  18. /* does not take xtime_lock */
  19. struct timespec __current_kernel_time(void);
  20. static inline struct timespec current_kernel_time(void)
  21. {
  22. struct timespec64 now = current_kernel_time64();
  23. return timespec64_to_timespec(now);
  24. }
  25. /*
  26. * timespec based interfaces
  27. */
  28. struct timespec64 get_monotonic_coarse64(void);
  29. extern void getrawmonotonic64(struct timespec64 *ts);
  30. extern void ktime_get_ts64(struct timespec64 *ts);
  31. extern time64_t ktime_get_seconds(void);
  32. extern time64_t ktime_get_real_seconds(void);
  33. extern int __getnstimeofday64(struct timespec64 *tv);
  34. extern void getnstimeofday64(struct timespec64 *tv);
  35. extern void getboottime64(struct timespec64 *ts);
  36. #if BITS_PER_LONG == 64
  37. /**
  38. * Deprecated. Use do_settimeofday64().
  39. */
  40. static inline int do_settimeofday(const struct timespec *ts)
  41. {
  42. return do_settimeofday64(ts);
  43. }
  44. static inline int __getnstimeofday(struct timespec *ts)
  45. {
  46. return __getnstimeofday64(ts);
  47. }
  48. static inline void getnstimeofday(struct timespec *ts)
  49. {
  50. getnstimeofday64(ts);
  51. }
  52. static inline void ktime_get_ts(struct timespec *ts)
  53. {
  54. ktime_get_ts64(ts);
  55. }
  56. static inline void ktime_get_real_ts(struct timespec *ts)
  57. {
  58. getnstimeofday64(ts);
  59. }
  60. static inline void getrawmonotonic(struct timespec *ts)
  61. {
  62. getrawmonotonic64(ts);
  63. }
  64. static inline struct timespec get_monotonic_coarse(void)
  65. {
  66. return get_monotonic_coarse64();
  67. }
  68. static inline void getboottime(struct timespec *ts)
  69. {
  70. return getboottime64(ts);
  71. }
  72. #else
  73. /**
  74. * Deprecated. Use do_settimeofday64().
  75. */
  76. static inline int do_settimeofday(const struct timespec *ts)
  77. {
  78. struct timespec64 ts64;
  79. ts64 = timespec_to_timespec64(*ts);
  80. return do_settimeofday64(&ts64);
  81. }
  82. static inline int __getnstimeofday(struct timespec *ts)
  83. {
  84. struct timespec64 ts64;
  85. int ret = __getnstimeofday64(&ts64);
  86. *ts = timespec64_to_timespec(ts64);
  87. return ret;
  88. }
  89. static inline void getnstimeofday(struct timespec *ts)
  90. {
  91. struct timespec64 ts64;
  92. getnstimeofday64(&ts64);
  93. *ts = timespec64_to_timespec(ts64);
  94. }
  95. static inline void ktime_get_ts(struct timespec *ts)
  96. {
  97. struct timespec64 ts64;
  98. ktime_get_ts64(&ts64);
  99. *ts = timespec64_to_timespec(ts64);
  100. }
  101. static inline void ktime_get_real_ts(struct timespec *ts)
  102. {
  103. struct timespec64 ts64;
  104. getnstimeofday64(&ts64);
  105. *ts = timespec64_to_timespec(ts64);
  106. }
  107. static inline void getrawmonotonic(struct timespec *ts)
  108. {
  109. struct timespec64 ts64;
  110. getrawmonotonic64(&ts64);
  111. *ts = timespec64_to_timespec(ts64);
  112. }
  113. static inline struct timespec get_monotonic_coarse(void)
  114. {
  115. return timespec64_to_timespec(get_monotonic_coarse64());
  116. }
  117. static inline void getboottime(struct timespec *ts)
  118. {
  119. struct timespec64 ts64;
  120. getboottime64(&ts64);
  121. *ts = timespec64_to_timespec(ts64);
  122. }
  123. #endif
  124. #define ktime_get_real_ts64(ts) getnstimeofday64(ts)
  125. /*
  126. * ktime_t based interfaces
  127. */
  128. enum tk_offsets {
  129. TK_OFFS_REAL,
  130. TK_OFFS_BOOT,
  131. TK_OFFS_TAI,
  132. TK_OFFS_MAX,
  133. };
  134. extern ktime_t ktime_get(void);
  135. extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
  136. extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
  137. extern ktime_t ktime_get_raw(void);
  138. extern u32 ktime_get_resolution_ns(void);
  139. /**
  140. * ktime_get_real - get the real (wall-) time in ktime_t format
  141. */
  142. static inline ktime_t ktime_get_real(void)
  143. {
  144. return ktime_get_with_offset(TK_OFFS_REAL);
  145. }
  146. /**
  147. * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
  148. *
  149. * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
  150. * time spent in suspend.
  151. */
  152. static inline ktime_t ktime_get_boottime(void)
  153. {
  154. return ktime_get_with_offset(TK_OFFS_BOOT);
  155. }
  156. /**
  157. * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
  158. */
  159. static inline ktime_t ktime_get_clocktai(void)
  160. {
  161. return ktime_get_with_offset(TK_OFFS_TAI);
  162. }
  163. /**
  164. * ktime_mono_to_real - Convert monotonic time to clock realtime
  165. */
  166. static inline ktime_t ktime_mono_to_real(ktime_t mono)
  167. {
  168. return ktime_mono_to_any(mono, TK_OFFS_REAL);
  169. }
  170. static inline u64 ktime_get_ns(void)
  171. {
  172. return ktime_to_ns(ktime_get());
  173. }
  174. static inline u64 ktime_get_real_ns(void)
  175. {
  176. return ktime_to_ns(ktime_get_real());
  177. }
  178. static inline u64 ktime_get_boot_ns(void)
  179. {
  180. return ktime_to_ns(ktime_get_boottime());
  181. }
  182. static inline u64 ktime_get_tai_ns(void)
  183. {
  184. return ktime_to_ns(ktime_get_clocktai());
  185. }
  186. static inline u64 ktime_get_raw_ns(void)
  187. {
  188. return ktime_to_ns(ktime_get_raw());
  189. }
  190. extern u64 ktime_get_mono_fast_ns(void);
  191. extern u64 ktime_get_raw_fast_ns(void);
  192. /*
  193. * Timespec interfaces utilizing the ktime based ones
  194. */
  195. static inline void get_monotonic_boottime(struct timespec *ts)
  196. {
  197. *ts = ktime_to_timespec(ktime_get_boottime());
  198. }
  199. static inline void get_monotonic_boottime64(struct timespec64 *ts)
  200. {
  201. *ts = ktime_to_timespec64(ktime_get_boottime());
  202. }
  203. static inline void timekeeping_clocktai(struct timespec *ts)
  204. {
  205. *ts = ktime_to_timespec(ktime_get_clocktai());
  206. }
  207. /*
  208. * RTC specific
  209. */
  210. extern bool timekeeping_rtc_skipsuspend(void);
  211. extern bool timekeeping_rtc_skipresume(void);
  212. extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
  213. /*
  214. * PPS accessor
  215. */
  216. extern void ktime_get_raw_and_real_ts64(struct timespec64 *ts_raw,
  217. struct timespec64 *ts_real);
  218. /*
  219. * Persistent clock related interfaces
  220. */
  221. extern int persistent_clock_is_local;
  222. extern void read_persistent_clock(struct timespec *ts);
  223. extern void read_persistent_clock64(struct timespec64 *ts);
  224. extern void read_boot_clock64(struct timespec64 *ts);
  225. extern int update_persistent_clock(struct timespec now);
  226. extern int update_persistent_clock64(struct timespec64 now);
  227. #endif