timekeeping.h 5.1 KB

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