xref: /linux/kernel/time/timekeeping_internal.h (revision 5bb6ba448fe3598a7668838942db1f008beb581b)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _TIMEKEEPING_INTERNAL_H
3 #define _TIMEKEEPING_INTERNAL_H
4 
5 #include <linux/clocksource.h>
6 #include <linux/spinlock.h>
7 #include <linux/time.h>
8 
9 /*
10  * timekeeping debug functions
11  */
12 #ifdef CONFIG_DEBUG_FS
13 
14 DECLARE_PER_CPU(unsigned long, timekeeping_mg_floor_swaps);
15 
16 static inline void timekeeping_inc_mg_floor_swaps(void)
17 {
18 	this_cpu_inc(timekeeping_mg_floor_swaps);
19 }
20 
21 extern void tk_debug_account_sleep_time(const struct timespec64 *t);
22 
23 #else
24 
25 #define tk_debug_account_sleep_time(x)
26 
27 static inline void timekeeping_inc_mg_floor_swaps(void)
28 {
29 }
30 
31 #endif
32 
33 #ifdef CONFIG_CLOCKSOURCE_VALIDATE_LAST_CYCLE
34 static inline u64 clocksource_delta(u64 now, u64 last, u64 mask)
35 {
36 	u64 ret = (now - last) & mask;
37 
38 	/*
39 	 * Prevent time going backwards by checking the MSB of mask in
40 	 * the result. If set, return 0.
41 	 */
42 	return ret & ~(mask >> 1) ? 0 : ret;
43 }
44 #else
45 static inline u64 clocksource_delta(u64 now, u64 last, u64 mask)
46 {
47 	return (now - last) & mask;
48 }
49 #endif
50 
51 /* Semi public for serialization of non timekeeper VDSO updates. */
52 extern raw_spinlock_t timekeeper_lock;
53 
54 #endif /* _TIMEKEEPING_INTERNAL_H */
55