timekeeping.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 get_monotonic_boottime(struct timespec *ts);
  26. extern void ktime_get_ts64(struct timespec64 *ts);
  27. extern int __getnstimeofday64(struct timespec64 *tv);
  28. extern void getnstimeofday64(struct timespec64 *tv);
  29. #if BITS_PER_LONG == 64
  30. static inline int __getnstimeofday(struct timespec *ts)
  31. {
  32. return __getnstimeofday64(ts);
  33. }
  34. static inline void getnstimeofday(struct timespec *ts)
  35. {
  36. getnstimeofday64(ts);
  37. }
  38. static inline void ktime_get_ts(struct timespec *ts)
  39. {
  40. ktime_get_ts64(ts);
  41. }
  42. static inline void ktime_get_real_ts(struct timespec *ts)
  43. {
  44. getnstimeofday64(ts);
  45. }
  46. #else
  47. static inline int __getnstimeofday(struct timespec *ts)
  48. {
  49. struct timespec64 ts64;
  50. int ret = __getnstimeofday64(&ts64);
  51. *ts = timespec64_to_timespec(ts64);
  52. return ret;
  53. }
  54. static inline void getnstimeofday(struct timespec *ts)
  55. {
  56. struct timespec64 ts64;
  57. getnstimeofday64(&ts64);
  58. *ts = timespec64_to_timespec(ts64);
  59. }
  60. static inline void ktime_get_ts(struct timespec *ts)
  61. {
  62. struct timespec64 ts64;
  63. ktime_get_ts64(&ts64);
  64. *ts = timespec64_to_timespec(ts64);
  65. }
  66. static inline void ktime_get_real_ts(struct timespec *ts)
  67. {
  68. struct timespec64 ts64;
  69. getnstimeofday64(&ts64);
  70. *ts = timespec64_to_timespec(ts64);
  71. }
  72. #endif
  73. extern void getboottime(struct timespec *ts);
  74. #define do_posix_clock_monotonic_gettime(ts) ktime_get_ts(ts)
  75. #define ktime_get_real_ts64(ts) getnstimeofday64(ts)
  76. /*
  77. * ktime_t based interfaces
  78. */
  79. enum tk_offsets {
  80. TK_OFFS_REAL,
  81. TK_OFFS_BOOT,
  82. TK_OFFS_TAI,
  83. TK_OFFS_MAX,
  84. };
  85. extern ktime_t ktime_get(void);
  86. extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
  87. extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
  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. /*
  132. * RTC specific
  133. */
  134. extern void timekeeping_inject_sleeptime(struct timespec *delta);
  135. /*
  136. * PPS accessor
  137. */
  138. extern void getnstime_raw_and_real(struct timespec *ts_raw,
  139. struct timespec *ts_real);
  140. /*
  141. * Persistent clock related interfaces
  142. */
  143. extern bool persistent_clock_exist;
  144. extern int persistent_clock_is_local;
  145. static inline bool has_persistent_clock(void)
  146. {
  147. return persistent_clock_exist;
  148. }
  149. extern void read_persistent_clock(struct timespec *ts);
  150. extern void read_boot_clock(struct timespec *ts);
  151. extern int update_persistent_clock(struct timespec now);
  152. #endif