Linux Audio

Check our new training course

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