xref: /openbsd-src/sys/dev/pci/drm/include/linux/ktime.h (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: ktime.h,v 1.1 2019/04/14 10:14:53 jsg 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 	getmicrouptime(&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 struct timeval
46 ktime_get_monotonic_offset(void)
47 {
48 	struct timeval tv = {0, 0};
49 	return tv;
50 }
51 
52 static inline int64_t
53 ktime_to_ms(struct timeval tv)
54 {
55 	return timeval_to_ms(&tv);
56 }
57 
58 static inline int64_t
59 ktime_to_us(struct timeval tv)
60 {
61 	return timeval_to_us(&tv);
62 }
63 
64 static inline int64_t
65 ktime_to_ns(struct timeval tv)
66 {
67 	return timeval_to_ns(&tv);
68 }
69 
70 static inline int64_t
71 ktime_get_raw_ns(void)
72 {
73 	return ktime_to_ns(ktime_get());
74 }
75 
76 #define ktime_to_timeval(tv) (tv)
77 
78 static inline struct timespec64
79 ktime_to_timespec64(struct timeval tv)
80 {
81 	struct timespec64 ts;
82 	ts.tv_sec = tv.tv_sec;
83 	ts.tv_nsec = tv.tv_usec * NSEC_PER_USEC;
84 	return ts;
85 }
86 
87 static inline struct timeval
88 ktime_sub(struct timeval a, struct timeval b)
89 {
90 	struct timeval res;
91 	timersub(&a, &b, &res);
92 	return res;
93 }
94 
95 static inline struct timeval
96 ktime_add(struct timeval a, struct timeval b)
97 {
98 	struct timeval res;
99 	timeradd(&a, &b, &res);
100 	return res;
101 }
102 
103 static inline struct timeval
104 ktime_add_ns(struct timeval tv, int64_t ns)
105 {
106 	return ns_to_timeval(timeval_to_ns(&tv) + ns);
107 }
108 
109 static inline struct timeval
110 ktime_sub_ns(struct timeval tv, int64_t ns)
111 {
112 	return ns_to_timeval(timeval_to_ns(&tv) - ns);
113 }
114 
115 static inline int64_t
116 ktime_us_delta(struct timeval a, struct timeval b)
117 {
118 	return ktime_to_us(ktime_sub(a, b));
119 }
120 
121 static inline int64_t
122 ktime_ms_delta(struct timeval a, struct timeval b)
123 {
124 	return ktime_to_ms(ktime_sub(a, b));
125 }
126 
127 static inline bool
128 ktime_after(const struct timeval a, const struct timeval b)
129 {
130 	return timercmp(&a, &b, >);
131 }
132 
133 static inline struct timeval
134 ns_to_ktime(uint64_t ns)
135 {
136 	return ns_to_timeval(ns);
137 }
138 
139 #include <linux/timekeeping.h>
140 
141 #endif
142