1 /* $NetBSD: ktime.h,v 1.7 2018/08/27 13:57:38 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/kernel.h> 38 #include <sys/time.h> 39 40 #include <linux/time.h> 41 42 union ktime { 43 int64_t kt_nsec; 44 }; 45 46 typedef union ktime ktime_t; 47 48 static inline int64_t 49 ktime_to_ns(ktime_t kt) 50 { 51 return kt.kt_nsec; 52 } 53 54 static inline int64_t 55 ktime_to_us(ktime_t kt) 56 { 57 return ktime_to_ns(kt)/1000; 58 } 59 60 static inline ktime_t 61 ns_to_ktime(int64_t nsec) 62 { 63 return (ktime_t) { .kt_nsec = nsec }; 64 } 65 66 static inline ktime_t 67 ktime_add_ns(ktime_t kt, int64_t nsec) 68 { 69 return ns_to_ktime(ktime_to_ns(kt) + nsec); 70 } 71 72 static inline ktime_t 73 ktime_sub(ktime_t a, ktime_t b) 74 { 75 return ns_to_ktime(ktime_to_ns(a) - ktime_to_ns(b)); 76 } 77 78 static inline ktime_t 79 ktime_sub_ns(ktime_t a, int64_t nsec) 80 { 81 return ns_to_ktime(ktime_to_ns(a) - nsec); 82 } 83 84 static inline struct timespec 85 ktime_to_timespec(ktime_t kt) 86 { 87 return ns_to_timespec(ktime_to_ns(kt)); 88 } 89 90 static inline struct timeval 91 ktime_to_timeval(ktime_t kt) 92 { 93 return ns_to_timeval(ktime_to_ns(kt)); 94 } 95 96 static inline ktime_t 97 timespec_to_ktime(struct timespec ts) 98 { 99 /* XXX Silently truncate? */ 100 return ns_to_ktime(1000000000*(int64_t)ts.tv_sec + ts.tv_nsec); 101 } 102 103 static inline ktime_t 104 ktime_get(void) 105 { 106 struct timespec ts; 107 108 nanouptime(&ts); 109 110 return timespec_to_ktime(ts); 111 } 112 113 static inline ktime_t 114 ktime_get_real(void) 115 { 116 struct timespec ts; 117 118 nanotime(&ts); 119 120 return timespec_to_ktime(ts); 121 } 122 123 static inline uint64_t 124 ktime_get_raw_ns(void) 125 { 126 127 /* XXX */ 128 return ktime_to_ns(ktime_get()); 129 } 130 131 static inline ktime_t 132 ktime_get_monotonic_offset(void) 133 { 134 return timespec_to_ktime(boottime); 135 } 136 137 static inline ktime_t 138 ktime_mono_to_real(ktime_t kt) 139 { 140 struct timespec ts = ktime_to_timespec(kt); 141 142 timespecadd(&ts, &boottime, &ts); 143 144 return timespec_to_ktime(ts); 145 } 146 147 static inline int64_t 148 ktime_us_delta(ktime_t a, ktime_t b) 149 { 150 return ktime_to_us(ktime_sub(a, b)); 151 } 152 153 static inline bool 154 time_in_range(unsigned long x, unsigned long a, unsigned long b) 155 { 156 return ((a <= x) && (x <= b)); 157 } 158 159 #endif /* _LINUX_KTIME_H_ */ 160