1 | /*- |
---|
2 | * SPDX-License-Identifier: BSD-3-Clause |
---|
3 | * |
---|
4 | * Copyright (C) 2016 embedded brains GmbH |
---|
5 | * |
---|
6 | * Copyright (c) 1982, 1986, 1993 |
---|
7 | * The Regents of the University of California. All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions |
---|
11 | * are met: |
---|
12 | * 1. Redistributions of source code must retain the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer. |
---|
14 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
15 | * notice, this list of conditions and the following disclaimer in the |
---|
16 | * documentation and/or other materials provided with the distribution. |
---|
17 | * 3. Neither the name of the University nor the names of its contributors |
---|
18 | * may be used to endorse or promote products derived from this software |
---|
19 | * without specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
31 | * SUCH DAMAGE. |
---|
32 | * |
---|
33 | * @(#)time.h 8.5 (Berkeley) 5/4/95 |
---|
34 | * $FreeBSD$ |
---|
35 | */ |
---|
36 | |
---|
37 | /** |
---|
38 | * @file |
---|
39 | * |
---|
40 | * @brief This header file provides time definitions for the kernel space |
---|
41 | * (_KERNEL is defined before including <sys/time.h>). |
---|
42 | */ |
---|
43 | |
---|
44 | #if defined(_SYS_TIME_H_) && defined(_KERNEL) |
---|
45 | |
---|
46 | #include <machine/_timecounter.h> |
---|
47 | |
---|
48 | /* Operations on timespecs */ |
---|
49 | #ifndef timespecclear |
---|
50 | #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0) |
---|
51 | #endif |
---|
52 | #ifndef timespecisset |
---|
53 | #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec) |
---|
54 | #endif |
---|
55 | #ifndef timespeccmp |
---|
56 | #define timespeccmp(tvp, uvp, cmp) \ |
---|
57 | (((tvp)->tv_sec == (uvp)->tv_sec) ? \ |
---|
58 | ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \ |
---|
59 | ((tvp)->tv_sec cmp (uvp)->tv_sec)) |
---|
60 | #endif |
---|
61 | |
---|
62 | #ifndef timespecadd |
---|
63 | #define timespecadd(tsp, usp, vsp) \ |
---|
64 | do { \ |
---|
65 | (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \ |
---|
66 | (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \ |
---|
67 | if ((vsp)->tv_nsec >= 1000000000L) { \ |
---|
68 | (vsp)->tv_sec++; \ |
---|
69 | (vsp)->tv_nsec -= 1000000000L; \ |
---|
70 | } \ |
---|
71 | } while (0) |
---|
72 | #endif |
---|
73 | #ifndef timespecsub |
---|
74 | #define timespecsub(tsp, usp, vsp) \ |
---|
75 | do { \ |
---|
76 | (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \ |
---|
77 | (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \ |
---|
78 | if ((vsp)->tv_nsec < 0) { \ |
---|
79 | (vsp)->tv_sec--; \ |
---|
80 | (vsp)->tv_nsec += 1000000000L; \ |
---|
81 | } \ |
---|
82 | } while (0) |
---|
83 | #endif |
---|
84 | |
---|
85 | /* |
---|
86 | * Simple macros to convert ticks to milliseconds |
---|
87 | * or microseconds and vice-versa. The answer |
---|
88 | * will always be at least 1. Note the return |
---|
89 | * value is a uint32_t however we step up the |
---|
90 | * operations to 64 bit to avoid any overflow/underflow |
---|
91 | * problems. |
---|
92 | */ |
---|
93 | #define TICKS_2_MSEC(t) max(1, (uint32_t)(hz == 1000) ? \ |
---|
94 | (t) : (((uint64_t)(t) * (uint64_t)1000)/(uint64_t)hz)) |
---|
95 | #define TICKS_2_USEC(t) max(1, (uint32_t)(hz == 1000) ? \ |
---|
96 | ((t) * 1000) : (((uint64_t)(t) * (uint64_t)1000000)/(uint64_t)hz)) |
---|
97 | #define MSEC_2_TICKS(m) max(1, (uint32_t)((hz == 1000) ? \ |
---|
98 | (m) : ((uint64_t)(m) * (uint64_t)hz)/(uint64_t)1000)) |
---|
99 | #define USEC_2_TICKS(u) max(1, (uint32_t)((hz == 1000) ? \ |
---|
100 | ((u) / 1000) : ((uint64_t)(u) * (uint64_t)hz)/(uint64_t)1000000)) |
---|
101 | |
---|
102 | /* Operations on timevals. */ |
---|
103 | |
---|
104 | #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) |
---|
105 | #define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) |
---|
106 | #define timevalcmp(tvp, uvp, cmp) \ |
---|
107 | (((tvp)->tv_sec == (uvp)->tv_sec) ? \ |
---|
108 | ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ |
---|
109 | ((tvp)->tv_sec cmp (uvp)->tv_sec)) |
---|
110 | |
---|
111 | /* timevaladd and timevalsub are not inlined */ |
---|
112 | |
---|
113 | /* |
---|
114 | * Kernel to clock driver interface. |
---|
115 | */ |
---|
116 | void inittodr(time_t base); |
---|
117 | void resettodr(void); |
---|
118 | |
---|
119 | #define time_second _Timecounter_Time_second |
---|
120 | #define time_uptime _Timecounter_Time_uptime |
---|
121 | extern struct timeval boottime; |
---|
122 | extern struct bintime tc_tick_bt; |
---|
123 | extern sbintime_t tc_tick_sbt; |
---|
124 | extern struct bintime tick_bt; |
---|
125 | extern sbintime_t tick_sbt; |
---|
126 | extern int tc_precexp; |
---|
127 | extern int tc_timepercentage; |
---|
128 | extern struct bintime bt_timethreshold; |
---|
129 | extern struct bintime bt_tickthreshold; |
---|
130 | extern sbintime_t sbt_timethreshold; |
---|
131 | extern sbintime_t sbt_tickthreshold; |
---|
132 | |
---|
133 | /* |
---|
134 | * Functions for looking at our clock: [get]{bin,nano,micro}[up]time() |
---|
135 | * |
---|
136 | * Functions without the "get" prefix returns the best timestamp |
---|
137 | * we can produce in the given format. |
---|
138 | * |
---|
139 | * "bin" == struct bintime == seconds + 64 bit fraction of seconds. |
---|
140 | * "nano" == struct timespec == seconds + nanoseconds. |
---|
141 | * "micro" == struct timeval == seconds + microseconds. |
---|
142 | * |
---|
143 | * Functions containing "up" returns time relative to boot and |
---|
144 | * should be used for calculating time intervals. |
---|
145 | * |
---|
146 | * Functions without "up" returns UTC time. |
---|
147 | * |
---|
148 | * Functions with the "get" prefix returns a less precise result |
---|
149 | * much faster than the functions without "get" prefix and should |
---|
150 | * be used where a precision of 1/hz seconds is acceptable or where |
---|
151 | * performance is priority. (NB: "precision", _not_ "resolution" !) |
---|
152 | */ |
---|
153 | |
---|
154 | #define binuptime(_bt) _Timecounter_Binuptime(_bt) |
---|
155 | #define nanouptime(_tsp) _Timecounter_Nanouptime(_tsp) |
---|
156 | #define microuptime(_tvp) _Timecounter_Microuptime(_tvp) |
---|
157 | |
---|
158 | static __inline sbintime_t |
---|
159 | sbinuptime(void) |
---|
160 | { |
---|
161 | struct bintime _bt; |
---|
162 | |
---|
163 | binuptime(&_bt); |
---|
164 | return (bttosbt(_bt)); |
---|
165 | } |
---|
166 | |
---|
167 | #define bintime(_bt) _Timecounter_Bintime(_bt) |
---|
168 | #define nanotime(_tsp) _Timecounter_Nanotime(_tsp) |
---|
169 | #define microtime(_tvp) _Timecounter_Microtime(_tvp) |
---|
170 | |
---|
171 | #define getbinuptime(_bt) _Timecounter_Getbinuptime(_bt) |
---|
172 | #define getnanouptime(_tsp) _Timecounter_Getnanouptime(_tsp) |
---|
173 | #define getmicrouptime(_tvp) _Timecounter_Getmicrouptime(_tvp) |
---|
174 | |
---|
175 | static __inline sbintime_t |
---|
176 | getsbinuptime(void) |
---|
177 | { |
---|
178 | struct bintime _bt; |
---|
179 | |
---|
180 | getbinuptime(&_bt); |
---|
181 | return (bttosbt(_bt)); |
---|
182 | } |
---|
183 | |
---|
184 | #define getbintime(_bt) _Timecounter_Getbintime(_bt) |
---|
185 | #define getnanotime(_tsp) _Timecounter_Getnanotime(_tsp) |
---|
186 | #define getmicrotime(_tvp) _Timecounter_Getmicrotime(_tvp) |
---|
187 | |
---|
188 | #define getboottime(_tvp) _Timecounter_Getboottime(_tvp) |
---|
189 | #define getboottimebin(_bt) _Timecounter_Getboottimebin(_bt) |
---|
190 | |
---|
191 | /* Other functions */ |
---|
192 | int itimerdecr(struct itimerval *itp, int usec); |
---|
193 | int itimerfix(struct timeval *tv); |
---|
194 | int ppsratecheck(struct timeval *, int *, int); |
---|
195 | int ratecheck(struct timeval *, const struct timeval *); |
---|
196 | void timevaladd(struct timeval *t1, const struct timeval *t2); |
---|
197 | void timevalsub(struct timeval *t1, const struct timeval *t2); |
---|
198 | int tvtohz(struct timeval *tv); |
---|
199 | |
---|
200 | #define TC_DEFAULTPERC 5 |
---|
201 | |
---|
202 | #define BT2FREQ(bt) \ |
---|
203 | (((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) / \ |
---|
204 | ((bt)->frac >> 1)) |
---|
205 | |
---|
206 | #define SBT2FREQ(sbt) ((SBT_1S + ((sbt) >> 1)) / (sbt)) |
---|
207 | |
---|
208 | #define FREQ2BT(freq, bt) \ |
---|
209 | { \ |
---|
210 | (bt)->sec = 0; \ |
---|
211 | (bt)->frac = ((uint64_t)0x8000000000000000 / (freq)) << 1; \ |
---|
212 | } |
---|
213 | |
---|
214 | #define TIMESEL(sbt, sbt2) \ |
---|
215 | (((sbt2) >= sbt_timethreshold) ? \ |
---|
216 | ((*(sbt) = getsbinuptime()), 1) : ((*(sbt) = sbinuptime()), 0)) |
---|
217 | |
---|
218 | #else /* !_SYS_TIME_H_ || !_KERNEL */ |
---|
219 | #error "must be included via <sys/time.h> in kernel space" |
---|
220 | #endif /* _SYS_TIME_H_ && _KERNEL */ |
---|