xref: /openbsd-src/sys/dev/pci/drm/include/linux/time.h (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: time.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_TIME_H
19 #define _LINUX_TIME_H
20 
21 #include <sys/time.h>
22 #include <linux/math64.h>
23 
24 #define NSEC_PER_USEC	1000L
25 #define NSEC_PER_MSEC	1000000L
26 #define NSEC_PER_SEC	1000000000L
27 
28 extern struct timespec ns_to_timespec(const int64_t);
29 extern int64_t timeval_to_ms(const struct timeval *);
30 extern int64_t timeval_to_ns(const struct timeval *);
31 extern int64_t timeval_to_us(const struct timeval *);
32 extern struct timeval ns_to_timeval(const int64_t);
33 
34 struct timespec64 {
35 	time_t	tv_sec;
36 	long	tv_nsec;
37 };
38 
39 static inline struct timespec
40 timespec_sub(struct timespec t1, struct timespec t2)
41 {
42 	struct timespec diff;
43 
44 	timespecsub(&t1, &t2, &diff);
45 	return diff;
46 }
47 
48 static inline void
49 set_normalized_timespec(struct timespec *ts, time_t sec, int64_t nsec)
50 {
51 	while (nsec > NSEC_PER_SEC) {
52 		nsec -= NSEC_PER_SEC;
53 		sec++;
54 	}
55 
56 	ts->tv_sec = sec;
57 	ts->tv_nsec = nsec;
58 }
59 
60 static inline int64_t
61 timespec_to_ns(const struct timespec *ts)
62 {
63 	return ((ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec);
64 }
65 
66 static inline int
67 timespec_valid(const struct timespec *ts)
68 {
69 	if (ts->tv_sec < 0 || ts->tv_sec > 100000000 ||
70 	    ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
71 		return (0);
72 	return (1);
73 }
74 
75 #endif
76