1
2#ifndef _LINUX_TIME64_H
3#define _LINUX_TIME64_H
4
5#include <linux/math64.h>
6
7typedef __s64 time64_t;
8typedef __u64 timeu64_t;
9
10
11
12
13#ifndef CONFIG_64BIT_TIME
14#define __kernel_timespec timespec
15#define __kernel_itimerspec itimerspec
16#endif
17
18#include <uapi/linux/time.h>
19
20struct timespec64 {
21 time64_t tv_sec;
22 long tv_nsec;
23};
24
25struct itimerspec64 {
26 struct timespec64 it_interval;
27 struct timespec64 it_value;
28};
29
30
31#define MSEC_PER_SEC 1000L
32#define USEC_PER_MSEC 1000L
33#define NSEC_PER_USEC 1000L
34#define NSEC_PER_MSEC 1000000L
35#define USEC_PER_SEC 1000000L
36#define NSEC_PER_SEC 1000000000L
37#define FSEC_PER_SEC 1000000000000000LL
38
39
40#define TIME64_MAX ((s64)~((u64)1 << 63))
41#define KTIME_MAX ((s64)~((u64)1 << 63))
42#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
43
44static inline int timespec64_equal(const struct timespec64 *a,
45 const struct timespec64 *b)
46{
47 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
48}
49
50
51
52
53
54
55static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs)
56{
57 if (lhs->tv_sec < rhs->tv_sec)
58 return -1;
59 if (lhs->tv_sec > rhs->tv_sec)
60 return 1;
61 return lhs->tv_nsec - rhs->tv_nsec;
62}
63
64extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec);
65
66static inline struct timespec64 timespec64_add(struct timespec64 lhs,
67 struct timespec64 rhs)
68{
69 struct timespec64 ts_delta;
70 set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec,
71 lhs.tv_nsec + rhs.tv_nsec);
72 return ts_delta;
73}
74
75
76
77
78static inline struct timespec64 timespec64_sub(struct timespec64 lhs,
79 struct timespec64 rhs)
80{
81 struct timespec64 ts_delta;
82 set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec,
83 lhs.tv_nsec - rhs.tv_nsec);
84 return ts_delta;
85}
86
87
88
89
90static inline bool timespec64_valid(const struct timespec64 *ts)
91{
92
93 if (ts->tv_sec < 0)
94 return false;
95
96 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
97 return false;
98 return true;
99}
100
101static inline bool timespec64_valid_strict(const struct timespec64 *ts)
102{
103 if (!timespec64_valid(ts))
104 return false;
105
106 if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
107 return false;
108 return true;
109}
110
111
112
113
114
115
116
117
118static inline s64 timespec64_to_ns(const struct timespec64 *ts)
119{
120 return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
121}
122
123
124
125
126
127
128
129extern struct timespec64 ns_to_timespec64(const s64 nsec);
130
131
132
133
134
135
136
137
138
139static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
140{
141 a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
142 a->tv_nsec = ns;
143}
144
145
146
147
148
149extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
150 const struct timespec64 rhs);
151
152#endif
153