timekeeping.h 5.5 KB

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