timekeeping.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_TIMEKEEPING_H
  3. #define _LINUX_TIMEKEEPING_H
  4. #include <linux/errno.h>
  5. /* Included from linux/ktime.h */
  6. void timekeeping_init(void);
  7. extern int timekeeping_suspended;
  8. /* Architecture timer tick functions: */
  9. extern void update_process_times(int user);
  10. extern void xtime_update(unsigned long ticks);
  11. /*
  12. * Get and set timeofday
  13. */
  14. extern int do_settimeofday64(const struct timespec64 *ts);
  15. extern int do_sys_settimeofday64(const struct timespec64 *tv,
  16. const struct timezone *tz);
  17. /*
  18. * Kernel time accessors
  19. */
  20. struct timespec64 current_kernel_time64(void);
  21. /*
  22. * timespec64 based interfaces
  23. */
  24. struct timespec64 get_monotonic_coarse64(void);
  25. extern void getrawmonotonic64(struct timespec64 *ts);
  26. extern void ktime_get_ts64(struct timespec64 *ts);
  27. extern time64_t ktime_get_seconds(void);
  28. extern time64_t __ktime_get_real_seconds(void);
  29. extern time64_t ktime_get_real_seconds(void);
  30. extern int __getnstimeofday64(struct timespec64 *tv);
  31. extern void getnstimeofday64(struct timespec64 *tv);
  32. extern void getboottime64(struct timespec64 *ts);
  33. #define ktime_get_real_ts64(ts) getnstimeofday64(ts)
  34. /*
  35. * ktime_t based interfaces
  36. */
  37. enum tk_offsets {
  38. TK_OFFS_REAL,
  39. TK_OFFS_BOOT,
  40. TK_OFFS_TAI,
  41. TK_OFFS_MAX,
  42. };
  43. extern ktime_t ktime_get(void);
  44. extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
  45. extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
  46. extern ktime_t ktime_get_raw(void);
  47. extern u32 ktime_get_resolution_ns(void);
  48. /**
  49. * ktime_get_real - get the real (wall-) time in ktime_t format
  50. */
  51. static inline ktime_t ktime_get_real(void)
  52. {
  53. return ktime_get_with_offset(TK_OFFS_REAL);
  54. }
  55. /**
  56. * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
  57. *
  58. * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
  59. * time spent in suspend.
  60. */
  61. static inline ktime_t ktime_get_boottime(void)
  62. {
  63. return ktime_get_with_offset(TK_OFFS_BOOT);
  64. }
  65. /**
  66. * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
  67. */
  68. static inline ktime_t ktime_get_clocktai(void)
  69. {
  70. return ktime_get_with_offset(TK_OFFS_TAI);
  71. }
  72. /**
  73. * ktime_mono_to_real - Convert monotonic time to clock realtime
  74. */
  75. static inline ktime_t ktime_mono_to_real(ktime_t mono)
  76. {
  77. return ktime_mono_to_any(mono, TK_OFFS_REAL);
  78. }
  79. static inline u64 ktime_get_ns(void)
  80. {
  81. return ktime_to_ns(ktime_get());
  82. }
  83. static inline u64 ktime_get_real_ns(void)
  84. {
  85. return ktime_to_ns(ktime_get_real());
  86. }
  87. static inline u64 ktime_get_boot_ns(void)
  88. {
  89. return ktime_to_ns(ktime_get_boottime());
  90. }
  91. static inline u64 ktime_get_tai_ns(void)
  92. {
  93. return ktime_to_ns(ktime_get_clocktai());
  94. }
  95. static inline u64 ktime_get_raw_ns(void)
  96. {
  97. return ktime_to_ns(ktime_get_raw());
  98. }
  99. extern u64 ktime_get_mono_fast_ns(void);
  100. extern u64 ktime_get_raw_fast_ns(void);
  101. extern u64 ktime_get_boot_fast_ns(void);
  102. extern u64 ktime_get_real_fast_ns(void);
  103. /*
  104. * timespec64 interfaces utilizing the ktime based ones
  105. */
  106. static inline void get_monotonic_boottime64(struct timespec64 *ts)
  107. {
  108. *ts = ktime_to_timespec64(ktime_get_boottime());
  109. }
  110. static inline void timekeeping_clocktai64(struct timespec64 *ts)
  111. {
  112. *ts = ktime_to_timespec64(ktime_get_clocktai());
  113. }
  114. /*
  115. * RTC specific
  116. */
  117. extern bool timekeeping_rtc_skipsuspend(void);
  118. extern bool timekeeping_rtc_skipresume(void);
  119. extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
  120. /*
  121. * struct system_time_snapshot - simultaneous raw/real time capture with
  122. * counter value
  123. * @cycles: Clocksource counter value to produce the system times
  124. * @real: Realtime system time
  125. * @raw: Monotonic raw system time
  126. * @clock_was_set_seq: The sequence number of clock was set events
  127. * @cs_was_changed_seq: The sequence number of clocksource change events
  128. */
  129. struct system_time_snapshot {
  130. u64 cycles;
  131. ktime_t real;
  132. ktime_t raw;
  133. unsigned int clock_was_set_seq;
  134. u8 cs_was_changed_seq;
  135. };
  136. /*
  137. * struct system_device_crosststamp - system/device cross-timestamp
  138. * (syncronized capture)
  139. * @device: Device time
  140. * @sys_realtime: Realtime simultaneous with device time
  141. * @sys_monoraw: Monotonic raw simultaneous with device time
  142. */
  143. struct system_device_crosststamp {
  144. ktime_t device;
  145. ktime_t sys_realtime;
  146. ktime_t sys_monoraw;
  147. };
  148. /*
  149. * struct system_counterval_t - system counter value with the pointer to the
  150. * corresponding clocksource
  151. * @cycles: System counter value
  152. * @cs: Clocksource corresponding to system counter value. Used by
  153. * timekeeping code to verify comparibility of two cycle values
  154. */
  155. struct system_counterval_t {
  156. u64 cycles;
  157. struct clocksource *cs;
  158. };
  159. /*
  160. * Get cross timestamp between system clock and device clock
  161. */
  162. extern int get_device_system_crosststamp(
  163. int (*get_time_fn)(ktime_t *device_time,
  164. struct system_counterval_t *system_counterval,
  165. void *ctx),
  166. void *ctx,
  167. struct system_time_snapshot *history,
  168. struct system_device_crosststamp *xtstamp);
  169. /*
  170. * Simultaneously snapshot realtime and monotonic raw clocks
  171. */
  172. extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
  173. /*
  174. * Persistent clock related interfaces
  175. */
  176. extern int persistent_clock_is_local;
  177. extern void read_persistent_clock64(struct timespec64 *ts);
  178. extern void read_boot_clock64(struct timespec64 *ts);
  179. extern int update_persistent_clock64(struct timespec64 now);
  180. #endif