xref: /freebsd/sys/compat/linuxkpi/common/include/linux/ktime.h (revision aafe4126f7942daef5b7d522bd4fafc5deddb0bf)
1 /*-
2  * Copyright (c) 2018 Limelight Networks, Inc.
3  * Copyright (c) 2014-2018 Mellanox Technologies, Ltd.
4  * Copyright (c) 2015 François Tigeot
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef _LINUXKPI_LINUX_KTIME_H
30 #define	_LINUXKPI_LINUX_KTIME_H
31 
32 #include <linux/types.h>
33 #include <linux/time.h>
34 #include <linux/jiffies.h>
35 
36 /* time values in nanoseconds */
37 typedef s64 ktime_t;
38 
39 #define	KTIME_MAX			((s64)~((u64)1 << 63))
40 #define	KTIME_SEC_MAX			(KTIME_MAX / NSEC_PER_SEC)
41 
42 static inline int64_t
ktime_to_ns(ktime_t kt)43 ktime_to_ns(ktime_t kt)
44 {
45 	return (kt);
46 }
47 
48 static inline ktime_t
ns_to_ktime(uint64_t nsec)49 ns_to_ktime(uint64_t nsec)
50 {
51 	return (nsec);
52 }
53 
54 static inline int64_t
ktime_divns(const ktime_t kt,int64_t div)55 ktime_divns(const ktime_t kt, int64_t div)
56 {
57 	return (kt / div);
58 }
59 
60 static inline int64_t
ktime_to_us(ktime_t kt)61 ktime_to_us(ktime_t kt)
62 {
63 	return (ktime_divns(kt, NSEC_PER_USEC));
64 }
65 
66 static inline int64_t
ktime_to_ms(ktime_t kt)67 ktime_to_ms(ktime_t kt)
68 {
69 	return (ktime_divns(kt, NSEC_PER_MSEC));
70 }
71 
72 static inline ktime_t
ms_to_ktime(uint64_t ms)73 ms_to_ktime(uint64_t ms)
74 {
75 	return (ms * NSEC_PER_MSEC);
76 }
77 
78 static inline struct timeval
ktime_to_timeval(ktime_t kt)79 ktime_to_timeval(ktime_t kt)
80 {
81 	return (ns_to_timeval(kt));
82 }
83 
84 static inline ktime_t
ktime_add_ns(ktime_t kt,int64_t ns)85 ktime_add_ns(ktime_t kt, int64_t ns)
86 {
87 	return (kt + ns);
88 }
89 
90 static inline ktime_t
ktime_add_ms(ktime_t kt,int64_t ms)91 ktime_add_ms(ktime_t kt, int64_t ms)
92 {
93 
94 	return (ktime_add_ns(kt, ms * NSEC_PER_MSEC));
95 }
96 
97 static inline ktime_t
ktime_add_us(ktime_t kt,int64_t us)98 ktime_add_us(ktime_t kt, int64_t us)
99 {
100 
101 	return (ktime_add_ns(kt, us * NSEC_PER_USEC));
102 }
103 
104 static inline ktime_t
ktime_sub_ns(ktime_t kt,int64_t ns)105 ktime_sub_ns(ktime_t kt, int64_t ns)
106 {
107 	return (kt - ns);
108 }
109 
110 static inline ktime_t
ktime_set(const long secs,const unsigned long nsecs)111 ktime_set(const long secs, const unsigned long nsecs)
112 {
113 	ktime_t retval = {(s64) secs * NSEC_PER_SEC + (s64) nsecs};
114 
115 	return (retval);
116 }
117 
118 static inline ktime_t
ktime_sub(ktime_t lhs,ktime_t rhs)119 ktime_sub(ktime_t lhs, ktime_t rhs)
120 {
121 	return (lhs - rhs);
122 }
123 
124 static inline int64_t
ktime_us_delta(ktime_t later,ktime_t earlier)125 ktime_us_delta(ktime_t later, ktime_t earlier)
126 {
127 	ktime_t diff = ktime_sub(later, earlier);
128 
129 	return (ktime_to_us(diff));
130 }
131 
132 static inline int64_t
ktime_ms_delta(ktime_t later,ktime_t earlier)133 ktime_ms_delta(ktime_t later, ktime_t earlier)
134 {
135 	ktime_t diff = ktime_sub(later, earlier);
136 
137 	return (ktime_to_ms(diff));
138 }
139 
140 static inline ktime_t
ktime_add(ktime_t lhs,ktime_t rhs)141 ktime_add(ktime_t lhs, ktime_t rhs)
142 {
143 	return (lhs + rhs);
144 }
145 
146 static inline int
ktime_compare(const ktime_t cmp1,const ktime_t cmp2)147 ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
148 {
149 
150 	if (cmp1 > cmp2)
151 		return (1);
152 	else if (cmp1 < cmp2)
153 		return (-1);
154 	else
155 		return (0);
156 }
157 
158 static inline bool
ktime_after(const ktime_t cmp1,const ktime_t cmp2)159 ktime_after(const ktime_t cmp1, const ktime_t cmp2)
160 {
161 
162 	return (ktime_compare(cmp1, cmp2) > 0);
163 }
164 
165 static inline bool
ktime_before(const ktime_t cmp1,const ktime_t cmp2)166 ktime_before(const ktime_t cmp1, const ktime_t cmp2)
167 {
168 
169 	return (ktime_compare(cmp1, cmp2) < 0);
170 }
171 
172 static inline ktime_t
timespec_to_ktime(struct timespec ts)173 timespec_to_ktime(struct timespec ts)
174 {
175 	return (ktime_set(ts.tv_sec, ts.tv_nsec));
176 }
177 
178 static inline ktime_t
timeval_to_ktime(struct timeval tv)179 timeval_to_ktime(struct timeval tv)
180 {
181 	return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC));
182 }
183 
184 static inline int64_t
timespec64_to_ns(struct timespec64 * ts)185 timespec64_to_ns(struct timespec64 *ts)
186 {
187 	return (timespec_to_ns(ts));
188 }
189 
190 #define	ktime_to_timespec(kt)		ns_to_timespec(kt)
191 #define	ktime_to_timespec64(kt)		ns_to_timespec(kt)
192 #define	ktime_to_timeval(kt)		ns_to_timeval(kt)
193 #define	ktime_to_ns(kt)			(kt)
194 #define	ktime_get_ts(ts)		getnanouptime(ts)
195 #define	ktime_get_ts64(ts)		getnanouptime(ts)
196 #define	ktime_get_raw_ts64(ts)		getnanouptime(ts)
197 #define	ktime_get_real_ts64(ts)		getnanotime(ts)
198 #define	getrawmonotonic64(ts)		getnanouptime(ts)
199 
200 static inline int64_t
ktime_get_ns(void)201 ktime_get_ns(void)
202 {
203 	struct timespec ts;
204 
205 	ktime_get_ts(&ts);
206 
207 	return (ktime_to_ns(timespec_to_ktime(ts)));
208 }
209 
210 static inline ktime_t
ktime_get(void)211 ktime_get(void)
212 {
213 	struct timespec ts;
214 
215 	ktime_get_ts(&ts);
216 	return (timespec_to_ktime(ts));
217 }
218 
219 static inline ktime_t
ktime_get_boottime(void)220 ktime_get_boottime(void)
221 {
222 	struct timespec ts;
223 
224 	nanouptime(&ts);
225 	return (timespec_to_ktime(ts));
226 }
227 
228 static inline uint64_t
ktime_get_boottime_ns(void)229 ktime_get_boottime_ns(void)
230 {
231 
232 	return (ktime_to_ns(ktime_get_boottime()));
233 }
234 
235 static inline ktime_t
ktime_get_real(void)236 ktime_get_real(void)
237 {
238 	struct timespec ts;
239 
240 	nanotime(&ts);
241 	return (timespec_to_ktime(ts));
242 }
243 
244 static inline ktime_t
ktime_get_real_seconds(void)245 ktime_get_real_seconds(void)
246 {
247 	struct timespec ts;
248 
249 	nanotime(&ts);
250 	return (ts.tv_sec);
251 }
252 
253 static inline ktime_t
ktime_get_raw(void)254 ktime_get_raw(void)
255 {
256 	struct timespec ts;
257 
258 	nanouptime(&ts);
259 	return (timespec_to_ktime(ts));
260 }
261 
262 static inline u64
ktime_get_raw_ns(void)263 ktime_get_raw_ns(void)
264 {
265 	struct timespec ts;
266 
267 	nanouptime(&ts);
268 	return (ktime_to_ns(timespec_to_ktime(ts)));
269 }
270 
271 static inline uint64_t
ktime_get_raw_fast_ns(void)272 ktime_get_raw_fast_ns(void)
273 {
274 	struct timespec ts;
275 
276 	getnanouptime(&ts);
277 	return (ktime_to_ns(timespec_to_ktime(ts)));
278 }
279 
280 #endif /* _LINUXKPI_LINUX_KTIME_H */
281