xref: /openbsd-src/sys/dev/pci/drm/include/linux/ktime.h (revision 505ee9ea3b177e2387d907a91ca7da069f3f14d8)
1 /*	$OpenBSD: ktime.h,v 1.4 2020/07/07 04:05:25 cheloha Exp $	*/
2 /*
3  * Copyright (c) 2013, 2014, 2015 Mark Kettenis
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef _LINUX_KTIME_H
19 #define _LINUX_KTIME_H
20 
21 #include <sys/time.h>
22 #include <linux/time.h>
23 #include <linux/jiffies.h>
24 
25 typedef struct timeval ktime_t;
26 
27 static inline struct timeval
28 ktime_get(void)
29 {
30 	struct timeval tv;
31 
32 	microuptime(&tv);
33 	return tv;
34 }
35 
36 static inline struct timeval
37 ktime_get_raw(void)
38 {
39 	struct timeval tv;
40 
41 	microuptime(&tv);
42 	return tv;
43 }
44 
45 static inline int64_t
46 ktime_to_ms(struct timeval tv)
47 {
48 	return timeval_to_ms(&tv);
49 }
50 
51 static inline int64_t
52 ktime_to_us(struct timeval tv)
53 {
54 	return timeval_to_us(&tv);
55 }
56 
57 static inline int64_t
58 ktime_to_ns(struct timeval tv)
59 {
60 	return timeval_to_ns(&tv);
61 }
62 
63 static inline int64_t
64 ktime_get_raw_ns(void)
65 {
66 	return ktime_to_ns(ktime_get_raw());
67 }
68 
69 static inline struct timespec64
70 ktime_to_timespec64(struct timeval tv)
71 {
72 	struct timespec64 ts;
73 	ts.tv_sec = tv.tv_sec;
74 	ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC;
75 	return ts;
76 }
77 
78 static inline struct timeval
79 ktime_sub(struct timeval a, struct timeval b)
80 {
81 	struct timeval res;
82 	timersub(&a, &b, &res);
83 	return res;
84 }
85 
86 static inline struct timeval
87 ktime_add(struct timeval a, struct timeval b)
88 {
89 	struct timeval res;
90 	timeradd(&a, &b, &res);
91 	return res;
92 }
93 
94 static inline struct timeval
95 ktime_add_us(struct timeval tv, int64_t us)
96 {
97 	return ns_to_timeval(timeval_to_ns(&tv) + (us * NSEC_PER_USEC));
98 }
99 
100 static inline struct timeval
101 ktime_add_ns(struct timeval tv, int64_t ns)
102 {
103 	return ns_to_timeval(timeval_to_ns(&tv) + ns);
104 }
105 
106 static inline struct timeval
107 ktime_sub_ns(struct timeval tv, int64_t ns)
108 {
109 	return ns_to_timeval(timeval_to_ns(&tv) - ns);
110 }
111 
112 static inline int64_t
113 ktime_us_delta(struct timeval a, struct timeval b)
114 {
115 	return ktime_to_us(ktime_sub(a, b));
116 }
117 
118 static inline int64_t
119 ktime_ms_delta(struct timeval a, struct timeval b)
120 {
121 	return ktime_to_ms(ktime_sub(a, b));
122 }
123 
124 static inline bool
125 ktime_after(const struct timeval a, const struct timeval b)
126 {
127 	return timercmp(&a, &b, >);
128 }
129 
130 static inline struct timeval
131 ns_to_ktime(uint64_t ns)
132 {
133 	return ns_to_timeval(ns);
134 }
135 
136 #include <linux/timekeeping.h>
137 
138 #endif
139