xref: /dflybsd-src/sys/dev/drm/include/linux/ktime.h (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
101c621f3SFrançois Tigeot /*
2*3f2dd94aSFrançois Tigeot  * Copyright (c) 2015-2020 François Tigeot <ftigeot@wolfpond.org>
301c621f3SFrançois Tigeot  * All rights reserved.
401c621f3SFrançois Tigeot  *
501c621f3SFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
601c621f3SFrançois Tigeot  * modification, are permitted provided that the following conditions
701c621f3SFrançois Tigeot  * are met:
801c621f3SFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
901c621f3SFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
1001c621f3SFrançois Tigeot  *    disclaimer.
1101c621f3SFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
1201c621f3SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
1301c621f3SFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
1401c621f3SFrançois Tigeot  *
1501c621f3SFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1601c621f3SFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1701c621f3SFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1801c621f3SFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1901c621f3SFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2001c621f3SFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2101c621f3SFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2201c621f3SFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2301c621f3SFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2401c621f3SFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2501c621f3SFrançois Tigeot  */
2601c621f3SFrançois Tigeot 
2701c621f3SFrançois Tigeot #ifndef _LINUX_KTIME_H_
2801c621f3SFrançois Tigeot #define _LINUX_KTIME_H_
2901c621f3SFrançois Tigeot 
3001c621f3SFrançois Tigeot #include <linux/time.h>
3101c621f3SFrançois Tigeot #include <linux/jiffies.h>
3201c621f3SFrançois Tigeot 
3301c621f3SFrançois Tigeot /* time values in nanoseconds */
340856d15dSFrançois Tigeot typedef s64	ktime_t;
3501c621f3SFrançois Tigeot 
365915b712SFrançois Tigeot static inline ktime_t
ktime_set(const s64 secs,const unsigned long nsecs)375915b712SFrançois Tigeot ktime_set(const s64 secs, const unsigned long nsecs)
385915b712SFrançois Tigeot {
390856d15dSFrançois Tigeot 	return (secs * NSEC_PER_SEC) + (s64)nsecs;
405915b712SFrançois Tigeot }
415915b712SFrançois Tigeot 
42d4c6ae4eSFrançois Tigeot static inline s64
ktime_to_us(const ktime_t kt)43d4c6ae4eSFrançois Tigeot ktime_to_us(const ktime_t kt)
44d4c6ae4eSFrançois Tigeot {
450856d15dSFrançois Tigeot 	return kt / NSEC_PER_USEC;
46d4c6ae4eSFrançois Tigeot }
47d4c6ae4eSFrançois Tigeot 
48d4c6ae4eSFrançois Tigeot static inline s64
ktime_us_delta(const ktime_t later,const ktime_t earlier)49d4c6ae4eSFrançois Tigeot ktime_us_delta(const ktime_t later, const ktime_t earlier)
50d4c6ae4eSFrançois Tigeot {
510856d15dSFrançois Tigeot 	return later - earlier;
52d4c6ae4eSFrançois Tigeot }
53d4c6ae4eSFrançois Tigeot 
ktime_to_ns(ktime_t kt)5401c621f3SFrançois Tigeot static inline int64_t ktime_to_ns(ktime_t kt)
5501c621f3SFrançois Tigeot {
560856d15dSFrançois Tigeot 	return kt;
5701c621f3SFrançois Tigeot }
5801c621f3SFrançois Tigeot 
ktime_to_timeval(ktime_t kt)5901c621f3SFrançois Tigeot static inline struct timeval ktime_to_timeval(ktime_t kt)
6001c621f3SFrançois Tigeot {
610856d15dSFrançois Tigeot 	return ns_to_timeval(kt);
6201c621f3SFrançois Tigeot }
6301c621f3SFrançois Tigeot 
ktime_add_ns(ktime_t kt,int64_t ns)6401c621f3SFrançois Tigeot static inline ktime_t ktime_add_ns(ktime_t kt, int64_t ns)
6501c621f3SFrançois Tigeot {
660856d15dSFrançois Tigeot 	return kt + ns;
6701c621f3SFrançois Tigeot }
6801c621f3SFrançois Tigeot 
69*3f2dd94aSFrançois Tigeot static inline ktime_t
ktime_add_ms(const ktime_t kt,const u64 ms)70*3f2dd94aSFrançois Tigeot ktime_add_ms(const ktime_t kt, const u64 ms)
71*3f2dd94aSFrançois Tigeot {
72*3f2dd94aSFrançois Tigeot 	return ktime_add_ns(kt, NSEC_PER_MSEC * ms);
73*3f2dd94aSFrançois Tigeot }
74*3f2dd94aSFrançois Tigeot 
ktime_sub_ns(ktime_t kt,int64_t ns)7501c621f3SFrançois Tigeot static inline ktime_t ktime_sub_ns(ktime_t kt, int64_t ns)
7601c621f3SFrançois Tigeot {
770856d15dSFrançois Tigeot 	return kt - ns;
7801c621f3SFrançois Tigeot }
7901c621f3SFrançois Tigeot 
ktime_get(void)8001c621f3SFrançois Tigeot static inline ktime_t ktime_get(void)
8101c621f3SFrançois Tigeot {
8201c621f3SFrançois Tigeot 	struct timespec ts;
8301c621f3SFrançois Tigeot 
8401c621f3SFrançois Tigeot 	nanouptime(&ts);
850856d15dSFrançois Tigeot 	return (ts.tv_sec * NSEC_PER_SEC) + ts.tv_nsec;
860856d15dSFrançois Tigeot }
870856d15dSFrançois Tigeot 
880856d15dSFrançois Tigeot static inline ktime_t
ktime_sub(ktime_t lhs,ktime_t rhs)890856d15dSFrançois Tigeot ktime_sub(ktime_t lhs, ktime_t rhs)
900856d15dSFrançois Tigeot {
910856d15dSFrançois Tigeot 	return lhs - rhs;
920856d15dSFrançois Tigeot }
930856d15dSFrançois Tigeot 
940856d15dSFrançois Tigeot static inline ktime_t
ns_to_ktime(u64 ns)950856d15dSFrançois Tigeot ns_to_ktime(u64 ns)
960856d15dSFrançois Tigeot {
970856d15dSFrançois Tigeot 	return ns;
9801c621f3SFrançois Tigeot }
9901c621f3SFrançois Tigeot 
100*3f2dd94aSFrançois Tigeot static inline bool
ktime_before(const ktime_t cmp1,const ktime_t cmp2)101*3f2dd94aSFrançois Tigeot ktime_before(const ktime_t cmp1, const ktime_t cmp2)
102*3f2dd94aSFrançois Tigeot {
103*3f2dd94aSFrançois Tigeot 	return (cmp1 < cmp2);
104*3f2dd94aSFrançois Tigeot }
105*3f2dd94aSFrançois Tigeot 
106*3f2dd94aSFrançois Tigeot static inline bool
ktime_after(const ktime_t cmp1,const ktime_t cmp2)107*3f2dd94aSFrançois Tigeot ktime_after(const ktime_t cmp1, const ktime_t cmp2)
108*3f2dd94aSFrançois Tigeot {
109*3f2dd94aSFrançois Tigeot 	return (cmp1 > cmp2);
110*3f2dd94aSFrançois Tigeot }
111*3f2dd94aSFrançois Tigeot 
112*3f2dd94aSFrançois Tigeot #define ktime_to_timespec64(kt)		ns_to_timespec((kt))
113*3f2dd94aSFrançois Tigeot 
11481d31d56SFrançois Tigeot #include <linux/timekeeping.h>
11581d31d56SFrançois Tigeot 
11601c621f3SFrançois Tigeot #endif	/* _LINUX_KTIME_H_ */
117