xref: /linux-6.15/include/linux/timekeeping.h (revision c819e2cf)
1 #ifndef _LINUX_TIMEKEEPING_H
2 #define _LINUX_TIMEKEEPING_H
3 
4 /* Included from linux/ktime.h */
5 
6 void timekeeping_init(void);
7 extern int timekeeping_suspended;
8 
9 /*
10  * Get and set timeofday
11  */
12 extern void do_gettimeofday(struct timeval *tv);
13 extern int do_settimeofday64(const struct timespec64 *ts);
14 extern int do_sys_settimeofday(const struct timespec *tv,
15 			       const struct timezone *tz);
16 
17 /*
18  * Kernel time accessors
19  */
20 unsigned long get_seconds(void);
21 struct timespec current_kernel_time(void);
22 /* does not take xtime_lock */
23 struct timespec __current_kernel_time(void);
24 
25 /*
26  * timespec based interfaces
27  */
28 struct timespec64 get_monotonic_coarse64(void);
29 extern void getrawmonotonic64(struct timespec64 *ts);
30 extern void ktime_get_ts64(struct timespec64 *ts);
31 extern time64_t ktime_get_seconds(void);
32 extern time64_t ktime_get_real_seconds(void);
33 
34 extern int __getnstimeofday64(struct timespec64 *tv);
35 extern void getnstimeofday64(struct timespec64 *tv);
36 
37 #if BITS_PER_LONG == 64
38 /**
39  * Deprecated. Use do_settimeofday64().
40  */
41 static inline int do_settimeofday(const struct timespec *ts)
42 {
43 	return do_settimeofday64(ts);
44 }
45 
46 static inline int __getnstimeofday(struct timespec *ts)
47 {
48 	return __getnstimeofday64(ts);
49 }
50 
51 static inline void getnstimeofday(struct timespec *ts)
52 {
53 	getnstimeofday64(ts);
54 }
55 
56 static inline void ktime_get_ts(struct timespec *ts)
57 {
58 	ktime_get_ts64(ts);
59 }
60 
61 static inline void ktime_get_real_ts(struct timespec *ts)
62 {
63 	getnstimeofday64(ts);
64 }
65 
66 static inline void getrawmonotonic(struct timespec *ts)
67 {
68 	getrawmonotonic64(ts);
69 }
70 
71 static inline struct timespec get_monotonic_coarse(void)
72 {
73 	return get_monotonic_coarse64();
74 }
75 #else
76 /**
77  * Deprecated. Use do_settimeofday64().
78  */
79 static inline int do_settimeofday(const struct timespec *ts)
80 {
81 	struct timespec64 ts64;
82 
83 	ts64 = timespec_to_timespec64(*ts);
84 	return do_settimeofday64(&ts64);
85 }
86 
87 static inline int __getnstimeofday(struct timespec *ts)
88 {
89 	struct timespec64 ts64;
90 	int ret = __getnstimeofday64(&ts64);
91 
92 	*ts = timespec64_to_timespec(ts64);
93 	return ret;
94 }
95 
96 static inline void getnstimeofday(struct timespec *ts)
97 {
98 	struct timespec64 ts64;
99 
100 	getnstimeofday64(&ts64);
101 	*ts = timespec64_to_timespec(ts64);
102 }
103 
104 static inline void ktime_get_ts(struct timespec *ts)
105 {
106 	struct timespec64 ts64;
107 
108 	ktime_get_ts64(&ts64);
109 	*ts = timespec64_to_timespec(ts64);
110 }
111 
112 static inline void ktime_get_real_ts(struct timespec *ts)
113 {
114 	struct timespec64 ts64;
115 
116 	getnstimeofday64(&ts64);
117 	*ts = timespec64_to_timespec(ts64);
118 }
119 
120 static inline void getrawmonotonic(struct timespec *ts)
121 {
122 	struct timespec64 ts64;
123 
124 	getrawmonotonic64(&ts64);
125 	*ts = timespec64_to_timespec(ts64);
126 }
127 
128 static inline struct timespec get_monotonic_coarse(void)
129 {
130 	return timespec64_to_timespec(get_monotonic_coarse64());
131 }
132 #endif
133 
134 extern void getboottime(struct timespec *ts);
135 
136 #define do_posix_clock_monotonic_gettime(ts) ktime_get_ts(ts)
137 #define ktime_get_real_ts64(ts)	getnstimeofday64(ts)
138 
139 /*
140  * ktime_t based interfaces
141  */
142 
143 enum tk_offsets {
144 	TK_OFFS_REAL,
145 	TK_OFFS_BOOT,
146 	TK_OFFS_TAI,
147 	TK_OFFS_MAX,
148 };
149 
150 extern ktime_t ktime_get(void);
151 extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
152 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
153 extern ktime_t ktime_get_raw(void);
154 
155 /**
156  * ktime_get_real - get the real (wall-) time in ktime_t format
157  */
158 static inline ktime_t ktime_get_real(void)
159 {
160 	return ktime_get_with_offset(TK_OFFS_REAL);
161 }
162 
163 /**
164  * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
165  *
166  * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
167  * time spent in suspend.
168  */
169 static inline ktime_t ktime_get_boottime(void)
170 {
171 	return ktime_get_with_offset(TK_OFFS_BOOT);
172 }
173 
174 /**
175  * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
176  */
177 static inline ktime_t ktime_get_clocktai(void)
178 {
179 	return ktime_get_with_offset(TK_OFFS_TAI);
180 }
181 
182 /**
183  * ktime_mono_to_real - Convert monotonic time to clock realtime
184  */
185 static inline ktime_t ktime_mono_to_real(ktime_t mono)
186 {
187 	return ktime_mono_to_any(mono, TK_OFFS_REAL);
188 }
189 
190 static inline u64 ktime_get_ns(void)
191 {
192 	return ktime_to_ns(ktime_get());
193 }
194 
195 static inline u64 ktime_get_real_ns(void)
196 {
197 	return ktime_to_ns(ktime_get_real());
198 }
199 
200 static inline u64 ktime_get_boot_ns(void)
201 {
202 	return ktime_to_ns(ktime_get_boottime());
203 }
204 
205 static inline u64 ktime_get_raw_ns(void)
206 {
207 	return ktime_to_ns(ktime_get_raw());
208 }
209 
210 extern u64 ktime_get_mono_fast_ns(void);
211 
212 /*
213  * Timespec interfaces utilizing the ktime based ones
214  */
215 static inline void get_monotonic_boottime(struct timespec *ts)
216 {
217 	*ts = ktime_to_timespec(ktime_get_boottime());
218 }
219 
220 static inline void timekeeping_clocktai(struct timespec *ts)
221 {
222 	*ts = ktime_to_timespec(ktime_get_clocktai());
223 }
224 
225 /*
226  * RTC specific
227  */
228 extern void timekeeping_inject_sleeptime64(struct timespec64 *delta);
229 
230 /*
231  * PPS accessor
232  */
233 extern void getnstime_raw_and_real(struct timespec *ts_raw,
234 				   struct timespec *ts_real);
235 
236 /*
237  * Persistent clock related interfaces
238  */
239 extern bool persistent_clock_exist;
240 extern int persistent_clock_is_local;
241 
242 static inline bool has_persistent_clock(void)
243 {
244 	return persistent_clock_exist;
245 }
246 
247 extern void read_persistent_clock(struct timespec *ts);
248 extern void read_boot_clock(struct timespec *ts);
249 extern int update_persistent_clock(struct timespec now);
250 
251 
252 #endif
253