timekeeping.h 4.4 KB

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