xref: /dflybsd-src/sys/dev/drm/include/linux/ktime.h (revision 01c621f36c787d40d0dc972eb679ad65f86fe3fa)
1*01c621f3SFrançois Tigeot /*
2*01c621f3SFrançois Tigeot  * Copyright (c) 2015 François Tigeot
3*01c621f3SFrançois Tigeot  * All rights reserved.
4*01c621f3SFrançois Tigeot  *
5*01c621f3SFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
6*01c621f3SFrançois Tigeot  * modification, are permitted provided that the following conditions
7*01c621f3SFrançois Tigeot  * are met:
8*01c621f3SFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
9*01c621f3SFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
10*01c621f3SFrançois Tigeot  *    disclaimer.
11*01c621f3SFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
12*01c621f3SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
13*01c621f3SFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
14*01c621f3SFrançois Tigeot  *
15*01c621f3SFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*01c621f3SFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*01c621f3SFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*01c621f3SFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*01c621f3SFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*01c621f3SFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*01c621f3SFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*01c621f3SFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*01c621f3SFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*01c621f3SFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*01c621f3SFrançois Tigeot  */
26*01c621f3SFrançois Tigeot 
27*01c621f3SFrançois Tigeot #ifndef _LINUX_KTIME_H_
28*01c621f3SFrançois Tigeot #define _LINUX_KTIME_H_
29*01c621f3SFrançois Tigeot 
30*01c621f3SFrançois Tigeot #include <linux/time.h>
31*01c621f3SFrançois Tigeot #include <linux/jiffies.h>
32*01c621f3SFrançois Tigeot 
33*01c621f3SFrançois Tigeot /* time values in nanoseconds */
34*01c621f3SFrançois Tigeot union ktime {
35*01c621f3SFrançois Tigeot 	int64_t tv64;
36*01c621f3SFrançois Tigeot };
37*01c621f3SFrançois Tigeot 
38*01c621f3SFrançois Tigeot typedef union ktime ktime_t;
39*01c621f3SFrançois Tigeot 
40*01c621f3SFrançois Tigeot static inline int64_t ktime_to_ns(ktime_t kt)
41*01c621f3SFrançois Tigeot {
42*01c621f3SFrançois Tigeot 	return kt.tv64;
43*01c621f3SFrançois Tigeot }
44*01c621f3SFrançois Tigeot 
45*01c621f3SFrançois Tigeot static inline struct timeval ktime_to_timeval(ktime_t kt)
46*01c621f3SFrançois Tigeot {
47*01c621f3SFrançois Tigeot 	return ns_to_timeval(kt.tv64);
48*01c621f3SFrançois Tigeot }
49*01c621f3SFrançois Tigeot 
50*01c621f3SFrançois Tigeot static inline ktime_t ktime_add_ns(ktime_t kt, int64_t ns)
51*01c621f3SFrançois Tigeot {
52*01c621f3SFrançois Tigeot 	ktime_t res;
53*01c621f3SFrançois Tigeot 
54*01c621f3SFrançois Tigeot 	res.tv64 = kt.tv64 + ns;
55*01c621f3SFrançois Tigeot 	return kt;
56*01c621f3SFrançois Tigeot }
57*01c621f3SFrançois Tigeot 
58*01c621f3SFrançois Tigeot static inline ktime_t ktime_sub_ns(ktime_t kt, int64_t ns)
59*01c621f3SFrançois Tigeot {
60*01c621f3SFrançois Tigeot 	ktime_t res;
61*01c621f3SFrançois Tigeot 
62*01c621f3SFrançois Tigeot 	res.tv64 = kt.tv64 - ns;
63*01c621f3SFrançois Tigeot 	return kt;
64*01c621f3SFrançois Tigeot }
65*01c621f3SFrançois Tigeot 
66*01c621f3SFrançois Tigeot #define NSEC_PER_SEC	1000000000L
67*01c621f3SFrançois Tigeot 
68*01c621f3SFrançois Tigeot static inline ktime_t ktime_get(void)
69*01c621f3SFrançois Tigeot {
70*01c621f3SFrançois Tigeot 	struct timespec ts;
71*01c621f3SFrançois Tigeot 	ktime_t kt;
72*01c621f3SFrançois Tigeot 
73*01c621f3SFrançois Tigeot 	nanouptime(&ts);
74*01c621f3SFrançois Tigeot 	kt.tv64 = (ts.tv_sec * NSEC_PER_SEC) + ts.tv_nsec;
75*01c621f3SFrançois Tigeot 	return kt;
76*01c621f3SFrançois Tigeot }
77*01c621f3SFrançois Tigeot 
78*01c621f3SFrançois Tigeot #endif	/* _LINUX_KTIME_H_ */
79