xref: /netbsd-src/sys/external/bsd/drm2/include/linux/ktime.h (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: ktime.h,v 1.2 2014/03/18 18:20:43 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _LINUX_KTIME_H_
33 #define _LINUX_KTIME_H_
34 
35 #include <sys/types.h>
36 #include <sys/endian.h>
37 #include <sys/time.h>
38 
39 union ktime {
40 	int64_t kt_nsec;
41 	struct {
42 #if _BYTE_ORDER == _BIG_ENDIAN
43 		int32_t ktsn_sec;
44 		int32_t ktsn_nsec;
45 #else
46 		int32_t ktsn_nsec;
47 		int32_t ktsn_sec;
48 #endif
49 	} kt_sec_nsec;
50 };
51 
52 typedef union ktime ktime_t;
53 
54 static inline int64_t
55 ktime_to_ns(ktime_t kt)
56 {
57 	return kt.kt_nsec;
58 }
59 
60 static inline ktime_t
61 ns_to_ktime(int64_t nsec)
62 {
63 	ktime_t kt;
64 
65 	kt.kt_nsec = nsec;
66 
67 	return kt;
68 }
69 
70 static inline ktime_t
71 ktime_sub(ktime_t a, ktime_t b)
72 {
73 	return ns_to_ktime(ktime_to_ns(a) - ktime_to_ns(b));
74 }
75 
76 static inline ktime_t
77 ktime_sub_ns(ktime_t a, int64_t nsec)
78 {
79 	return ns_to_ktime(ktime_to_ns(a) - nsec);
80 }
81 
82 static inline int64_t
83 timespec_to_ns(struct timespec *t)
84 {
85 	return (t->tv_sec * 1000000000ul) + t->tv_nsec;
86 }
87 
88 static inline int64_t
89 timeval_to_ns(struct timeval *tv)
90 {
91 	return (tv->tv_sec * 1000000000ul) + (tv->tv_usec * 1000ul);
92 }
93 
94 static inline struct timespec
95 ns_to_timespec(int64_t nsec)
96 {
97 	struct timespec t;
98 
99 	t.tv_sec = (nsec / 1000000000ul);
100 	t.tv_nsec = (nsec % 1000000000ul);
101 
102 	return t;
103 }
104 
105 static inline struct timeval
106 ns_to_timeval(int64_t nsec)
107 {
108 	struct timespec ts;
109 	struct timeval tv;
110 
111 	ts = ns_to_timespec(nsec);
112 	TIMESPEC_TO_TIMEVAL(&tv, &ts);
113 
114 	return tv;
115 }
116 
117 static inline struct timespec
118 ktime_to_timespec(ktime_t kt)
119 {
120 	return ns_to_timespec(ktime_to_ns(kt));
121 }
122 
123 static inline struct timeval
124 ktime_to_timeval(ktime_t kt)
125 {
126 	return ns_to_timeval(ktime_to_ns(kt));
127 }
128 
129 static inline ktime_t
130 ktime_get(void)
131 {
132 	struct timespec ts;
133 	ktime_t kt;
134 
135 	/* XXX nanotime or nanouptime?  */
136 	nanouptime(&ts);
137 
138 	/* XXX Silently truncate?  */
139 	kt.kt_sec_nsec.ktsn_sec = ts.tv_sec & 0xffffffffUL;
140 	kt.kt_sec_nsec.ktsn_nsec = ts.tv_nsec;
141 
142 	return kt;
143 }
144 
145 static inline ktime_t
146 ktime_get_monotonic_offset(void)
147 {
148 	return ns_to_ktime(0);	/* XXX Obviously wrong!  Revisit.  */
149 }
150 
151 #endif  /* _LINUX_KTIME_H_ */
152