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