timekeeping.h 4.3 KB

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