timekeeping.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_TIMEKEEPING_H
  3. #define _LINUX_TIMEKEEPING_H
  4. #include <linux/errno.h>
  5. #include <linux/clocksource_ids.h>
  6. /* Included from linux/ktime.h */
  7. void timekeeping_init(void);
  8. extern int timekeeping_suspended;
  9. /* Architecture timer tick functions: */
  10. extern void legacy_timer_tick(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. * ktime_get() family: read the current time in a multitude of ways,
  19. *
  20. * The default time reference is CLOCK_MONOTONIC, starting at
  21. * boot time but not counting the time spent in suspend.
  22. * For other references, use the functions with "real", "clocktai",
  23. * "boottime" and "raw" suffixes.
  24. *
  25. * To get the time in a different format, use the ones wit
  26. * "ns", "ts64" and "seconds" suffix.
  27. *
  28. * See Documentation/core-api/timekeeping.rst for more details.
  29. */
  30. /*
  31. * timespec64 based interfaces
  32. */
  33. extern void ktime_get_raw_ts64(struct timespec64 *ts);
  34. extern void ktime_get_ts64(struct timespec64 *ts);
  35. extern void ktime_get_real_ts64(struct timespec64 *tv);
  36. extern void ktime_get_coarse_ts64(struct timespec64 *ts);
  37. extern void ktime_get_coarse_real_ts64(struct timespec64 *ts);
  38. void getboottime64(struct timespec64 *ts);
  39. /*
  40. * time64_t base interfaces
  41. */
  42. extern time64_t ktime_get_seconds(void);
  43. extern time64_t __ktime_get_real_seconds(void);
  44. extern time64_t ktime_get_real_seconds(void);
  45. /*
  46. * ktime_t based interfaces
  47. */
  48. enum tk_offsets {
  49. TK_OFFS_REAL,
  50. TK_OFFS_BOOT,
  51. TK_OFFS_TAI,
  52. TK_OFFS_MAX,
  53. };
  54. extern ktime_t ktime_get(void);
  55. extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
  56. extern ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs);
  57. extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
  58. extern ktime_t ktime_get_raw(void);
  59. extern u32 ktime_get_resolution_ns(void);
  60. /**
  61. * ktime_get_real - get the real (wall-) time in ktime_t format
  62. */
  63. static inline ktime_t ktime_get_real(void)
  64. {
  65. return ktime_get_with_offset(TK_OFFS_REAL);
  66. }
  67. static inline ktime_t ktime_get_coarse_real(void)
  68. {
  69. return ktime_get_coarse_with_offset(TK_OFFS_REAL);
  70. }
  71. /**
  72. * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
  73. *
  74. * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
  75. * time spent in suspend.
  76. */
  77. static inline ktime_t ktime_get_boottime(void)
  78. {
  79. return ktime_get_with_offset(TK_OFFS_BOOT);
  80. }
  81. static inline ktime_t ktime_get_coarse_boottime(void)
  82. {
  83. return ktime_get_coarse_with_offset(TK_OFFS_BOOT);
  84. }
  85. /**
  86. * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
  87. */
  88. static inline ktime_t ktime_get_clocktai(void)
  89. {
  90. return ktime_get_with_offset(TK_OFFS_TAI);
  91. }
  92. static inline ktime_t ktime_get_coarse_clocktai(void)
  93. {
  94. return ktime_get_coarse_with_offset(TK_OFFS_TAI);
  95. }
  96. static inline ktime_t ktime_get_coarse(void)
  97. {
  98. struct timespec64 ts;
  99. ktime_get_coarse_ts64(&ts);
  100. return timespec64_to_ktime(ts);
  101. }
  102. static inline u64 ktime_get_coarse_ns(void)
  103. {
  104. return ktime_to_ns(ktime_get_coarse());
  105. }
  106. static inline u64 ktime_get_coarse_real_ns(void)
  107. {
  108. return ktime_to_ns(ktime_get_coarse_real());
  109. }
  110. static inline u64 ktime_get_coarse_boottime_ns(void)
  111. {
  112. return ktime_to_ns(ktime_get_coarse_boottime());
  113. }
  114. static inline u64 ktime_get_coarse_clocktai_ns(void)
  115. {
  116. return ktime_to_ns(ktime_get_coarse_clocktai());
  117. }
  118. /**
  119. * ktime_mono_to_real - Convert monotonic time to clock realtime
  120. */
  121. static inline ktime_t ktime_mono_to_real(ktime_t mono)
  122. {
  123. return ktime_mono_to_any(mono, TK_OFFS_REAL);
  124. }
  125. static inline u64 ktime_get_ns(void)
  126. {
  127. return ktime_to_ns(ktime_get());
  128. }
  129. static inline u64 ktime_get_real_ns(void)
  130. {
  131. return ktime_to_ns(ktime_get_real());
  132. }
  133. static inline u64 ktime_get_boottime_ns(void)
  134. {
  135. return ktime_to_ns(ktime_get_boottime());
  136. }
  137. static inline u64 ktime_get_clocktai_ns(void)
  138. {
  139. return ktime_to_ns(ktime_get_clocktai());
  140. }
  141. static inline u64 ktime_get_raw_ns(void)
  142. {
  143. return ktime_to_ns(ktime_get_raw());
  144. }
  145. extern u64 ktime_get_mono_fast_ns(void);
  146. extern u64 ktime_get_raw_fast_ns(void);
  147. extern u64 ktime_get_boot_fast_ns(void);
  148. extern u64 ktime_get_tai_fast_ns(void);
  149. extern u64 ktime_get_real_fast_ns(void);
  150. /*
  151. * timespec64/time64_t interfaces utilizing the ktime based ones
  152. * for API completeness, these could be implemented more efficiently
  153. * if needed.
  154. */
  155. static inline void ktime_get_boottime_ts64(struct timespec64 *ts)
  156. {
  157. *ts = ktime_to_timespec64(ktime_get_boottime());
  158. }
  159. static inline void ktime_get_coarse_boottime_ts64(struct timespec64 *ts)
  160. {
  161. *ts = ktime_to_timespec64(ktime_get_coarse_boottime());
  162. }
  163. static inline time64_t ktime_get_boottime_seconds(void)
  164. {
  165. return ktime_divns(ktime_get_coarse_boottime(), NSEC_PER_SEC);
  166. }
  167. static inline void ktime_get_clocktai_ts64(struct timespec64 *ts)
  168. {
  169. *ts = ktime_to_timespec64(ktime_get_clocktai());
  170. }
  171. static inline void ktime_get_coarse_clocktai_ts64(struct timespec64 *ts)
  172. {
  173. *ts = ktime_to_timespec64(ktime_get_coarse_clocktai());
  174. }
  175. static inline time64_t ktime_get_clocktai_seconds(void)
  176. {
  177. return ktime_divns(ktime_get_coarse_clocktai(), NSEC_PER_SEC);
  178. }
  179. /*
  180. * RTC specific
  181. */
  182. extern bool timekeeping_rtc_skipsuspend(void);
  183. extern bool timekeeping_rtc_skipresume(void);
  184. extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta);
  185. /*
  186. * struct ktime_timestanps - Simultaneous mono/boot/real timestamps
  187. * @mono: Monotonic timestamp
  188. * @boot: Boottime timestamp
  189. * @real: Realtime timestamp
  190. */
  191. struct ktime_timestamps {
  192. u64 mono;
  193. u64 boot;
  194. u64 real;
  195. };
  196. /**
  197. * struct system_time_snapshot - simultaneous raw/real time capture with
  198. * counter value
  199. * @cycles: Clocksource counter value to produce the system times
  200. * @real: Realtime system time
  201. * @boot: Boot time
  202. * @raw: Monotonic raw system time
  203. * @clock_was_set_seq: The sequence number of clock was set events
  204. * @cs_was_changed_seq: The sequence number of clocksource change events
  205. * @mono_shift: The monotonic clock slope shift
  206. * @mono_mult: The monotonic clock slope mult
  207. */
  208. struct system_time_snapshot {
  209. u64 cycles;
  210. ktime_t real;
  211. ktime_t boot;
  212. ktime_t raw;
  213. enum clocksource_ids cs_id;
  214. unsigned int clock_was_set_seq;
  215. u8 cs_was_changed_seq;
  216. u32 mono_shift;
  217. u32 mono_mult;
  218. };
  219. /**
  220. * struct system_device_crosststamp - system/device cross-timestamp
  221. * (synchronized capture)
  222. * @device: Device time
  223. * @sys_realtime: Realtime simultaneous with device time
  224. * @sys_monoraw: Monotonic raw simultaneous with device time
  225. */
  226. struct system_device_crosststamp {
  227. ktime_t device;
  228. ktime_t sys_realtime;
  229. ktime_t sys_monoraw;
  230. };
  231. /**
  232. * struct system_counterval_t - system counter value with the pointer to the
  233. * corresponding clocksource
  234. * @cycles: System counter value
  235. * @cs: Clocksource corresponding to system counter value. Used by
  236. * timekeeping code to verify comparibility of two cycle values
  237. */
  238. struct system_counterval_t {
  239. u64 cycles;
  240. struct clocksource *cs;
  241. };
  242. /*
  243. * Get cross timestamp between system clock and device clock
  244. */
  245. extern int get_device_system_crosststamp(
  246. int (*get_time_fn)(ktime_t *device_time,
  247. struct system_counterval_t *system_counterval,
  248. void *ctx),
  249. void *ctx,
  250. struct system_time_snapshot *history,
  251. struct system_device_crosststamp *xtstamp);
  252. /*
  253. * Simultaneously snapshot realtime and monotonic raw clocks
  254. */
  255. extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
  256. /* NMI safe mono/boot/realtime timestamps */
  257. extern void ktime_get_fast_timestamps(struct ktime_timestamps *snap);
  258. /*
  259. * Persistent clock related interfaces
  260. */
  261. extern int persistent_clock_is_local;
  262. extern void read_persistent_clock64(struct timespec64 *ts);
  263. void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock,
  264. struct timespec64 *boot_offset);
  265. #ifdef CONFIG_GENERIC_CMOS_UPDATE
  266. extern int update_persistent_clock64(struct timespec64 now);
  267. #endif
  268. #endif