xref: /wlan-dirver/qca-wifi-host-cmn/qdf/inc/qdf_time.h (revision 302a1d9701784af5f4797b1a9fe07ae820b51907)
1 /*
2  * Copyright (c) 2014-2018 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /**
20  * DOC: qdf_time
21  * This file abstracts time related functionality.
22  */
23 
24 #ifndef _QDF_OS_TIME_H
25 #define _QDF_OS_TIME_H
26 
27 #include <i_qdf_time.h>
28 
29 typedef __qdf_time_t qdf_time_t;
30 typedef __qdf_ktime_t qdf_ktime_t;
31 
32 /**
33  * qdf_ns_to_ktime - Converts nanoseconds to a qdf_ktime_t object
34  * @ns: time in nanoseconds
35  *
36  * Return: nanoseconds as qdf_ktime_t object
37  */
38 
39 static inline qdf_ktime_t qdf_ns_to_ktime(uint64_t ns)
40 {
41 	return __qdf_ns_to_ktime(ns);
42 }
43 
44 /**
45  * qdf_ktime_add - Adds two qdf_ktime_t objects and returns
46  * a qdf_ktime_t object
47  * @ktime1: time as qdf_ktime_t object
48  * @ktime2: time as qdf_ktime_t object
49  *
50  * Return: sum of both qdf_ktime_t as qdf_ktime_t object
51  */
52 
53 static inline qdf_ktime_t qdf_ktime_add(qdf_ktime_t ktime1, qdf_ktime_t ktime2)
54 {
55 	return __qdf_ktime_add(ktime1, ktime2);
56 }
57 
58 /**
59  * qdf_ktime_get - Gets the current time as qdf_ktime_t object
60  *
61  * Return: current time as qdf_ktime_t object
62  */
63 
64 static inline qdf_ktime_t qdf_ktime_get(void)
65 {
66 	return __qdf_ktime_get();
67 }
68 
69 /**
70  * qdf_ktime_add_ns - Adds qdf_ktime_t object and nanoseconds value and
71  * returns the qdf_ktime_t object
72  * @ktime: time as qdf_ktime_t object
73  * @ns: time in nanoseconds
74  *
75  * Return: qdf_ktime_t object
76  */
77 
78 static inline qdf_ktime_t qdf_ktime_add_ns(qdf_ktime_t ktime, int64_t ns)
79 {
80 	return __qdf_ktime_add_ns(ktime, ns);
81 }
82 
83 /**
84  * qdf_ktime_to_ms - Convert the qdf_ktime_t object into milliseconds
85  * @ktime: time as qdf_ktime_t object
86  *
87  * Return: qdf_ktime_t in milliseconds
88  */
89 
90 static inline int64_t qdf_ktime_to_ms(qdf_ktime_t ktime)
91 {
92 	return __qdf_ktime_to_ms(ktime);
93 }
94 
95 /**
96  * qdf_ktime_to_ns - Convert the qdf_ktime_t object into nanoseconds
97  * @ktime: time as qdf_ktime_t object
98  *
99  * Return: qdf_ktime_t in nanoseconds
100  */
101 
102 static inline int64_t qdf_ktime_to_ns(qdf_ktime_t ktime)
103 {
104 	return __qdf_ktime_to_ns(ktime);
105 }
106 
107 /**
108  * qdf_system_ticks - Count the number of ticks elapsed from the time when
109  * the system booted
110  *
111  * Return: ticks
112  */
113 static inline qdf_time_t qdf_system_ticks(void)
114 {
115 	return __qdf_system_ticks();
116 }
117 
118 /**
119  * qdf_system_ticks_to_msecs - convert ticks to milliseconds
120  * @clock_ticks: Number of ticks
121  *
122  * Return: unsigned int Time in milliseconds
123  */
124 static inline uint32_t qdf_system_ticks_to_msecs(unsigned long clock_ticks)
125 {
126 	return __qdf_system_ticks_to_msecs(clock_ticks);
127 }
128 
129 /**
130  * qdf_system_msecs_to_ticks - convert milliseconds to ticks
131  * @msec: Time in milliseconds
132  *
133  * Return: unsigned long number of ticks
134  */
135 static inline qdf_time_t qdf_system_msecs_to_ticks(uint32_t msecs)
136 {
137 	return __qdf_system_msecs_to_ticks(msecs);
138 }
139 
140 /**
141  * qdf_get_system_uptime - Return a monotonically increasing time
142  * This increments once per HZ ticks
143  *
144  * Return: qdf_time_t system up time in ticks
145  */
146 static inline qdf_time_t qdf_get_system_uptime(void)
147 {
148 	return __qdf_get_system_uptime();
149 }
150 
151 /**
152  * qdf_get_bootbased_boottime_ns() - Get the bootbased time in nanoseconds
153  *
154  * qdf_get_bootbased_boottime_ns() function returns the number of nanoseconds
155  * that have elapsed since the system was booted. It also includes the time when
156  * system was suspended.
157  *
158  * Return:
159  * The time since system booted in nanoseconds
160  */
161 
162 static inline uint64_t qdf_get_bootbased_boottime_ns(void)
163 {
164 	return __qdf_get_bootbased_boottime_ns();
165 }
166 
167 /**
168  * qdf_get_system_timestamp - Return current timestamp
169  *
170  * Return: unsigned long timestamp in ms.
171  */
172 static inline unsigned long qdf_get_system_timestamp(void)
173 {
174 	return __qdf_get_system_timestamp();
175 }
176 
177 /**
178  * qdf_udelay - delay in microseconds
179  * @usecs: Number of microseconds to delay
180  *
181  * Return: none
182  */
183 static inline void qdf_udelay(int usecs)
184 {
185 	__qdf_udelay(usecs);
186 }
187 
188 /**
189  * qdf_mdelay - Delay in milliseconds.
190  * @msec: Number of milliseconds to delay
191  *
192  * Return: none
193  */
194 static inline void qdf_mdelay(int msecs)
195 {
196 	__qdf_mdelay(msecs);
197 }
198 
199 /**
200  * qdf_system_time_after() - Check if a is later than b
201  * @a: Time stamp value a
202  * @b: Time stamp value b
203  *
204  * Return:
205  * true if a < b else false
206  */
207 static inline bool qdf_system_time_after(qdf_time_t a, qdf_time_t b)
208 {
209 	return __qdf_system_time_after(a, b);
210 }
211 
212 /**
213  * qdf_system_time_before() - Check if a is before b
214  * @a: Time stamp value a
215  * @b: Time stamp value b
216  *
217  * Return:
218  * true if a is before b else false
219  */
220 static inline bool qdf_system_time_before(qdf_time_t a, qdf_time_t b)
221 {
222 	return __qdf_system_time_before(a, b);
223 }
224 
225 /**
226  * qdf_system_time_after_eq() - Check if a atleast as recent as b, if not
227  * later
228  * @a: Time stamp value a
229  * @b: Time stamp value b
230  *
231  * Return:
232  * true if a >= b else false
233  */
234 static inline bool qdf_system_time_after_eq(qdf_time_t a, qdf_time_t b)
235 {
236 	return __qdf_system_time_after_eq(a, b);
237 }
238 
239 /**
240  * enum qdf_timestamp_unit - what unit the qdf timestamp is in
241  * @KERNEL_LOG: boottime time in uS (micro seconds)
242  * @QTIMER: QTIME in (1/19200)S
243  *
244  * This enum is used to distinguish which timer source is used.
245  */
246 enum qdf_timestamp_unit {
247 	KERNEL_LOG,
248 	QTIMER,
249 };
250 
251 #ifdef QCA_WIFI_3_0_ADRASTEA
252 #define QDF_LOG_TIMESTAMP_UNIT QTIMER
253 #define QDF_LOG_TIMESTAMP_CYCLES_PER_10_US 192
254 
255 static inline uint64_t qdf_log_timestamp_to_usecs(uint64_t time)
256 {
257 	/*
258 	 * Try to preserve precision by multiplying by 10 first.
259 	 * If that would cause a wrap around, divide first instead.
260 	 */
261 	if (time * 10 < time) {
262 		do_div(time, QDF_LOG_TIMESTAMP_CYCLES_PER_10_US);
263 		return time * 10;
264 	}
265 
266 	time = time * 10;
267 	do_div(time, QDF_LOG_TIMESTAMP_CYCLES_PER_10_US);
268 
269 	return time;
270 }
271 #else
272 #define QDF_LOG_TIMESTAMP_UNIT KERNEL_LOG
273 #define QDF_LOG_TIMESTAMP_CYCLES_PER_10_US 10
274 
275 static inline uint64_t qdf_log_timestamp_to_usecs(uint64_t time)
276 {
277 	/* timestamps are already in micro seconds */
278 	return time;
279 }
280 #endif
281 
282 static inline void qdf_log_timestamp_to_secs(uint64_t time, uint64_t *secs,
283 					     uint64_t *usecs)
284 {
285 	*secs = qdf_log_timestamp_to_usecs(time);
286 	*usecs = do_div(*secs, 1000000ul);
287 }
288 
289 static inline uint64_t qdf_usecs_to_log_timestamp(uint64_t usecs)
290 {
291 	return (usecs * QDF_LOG_TIMESTAMP_CYCLES_PER_10_US) / 10;
292 }
293 
294 /**
295  * qdf_get_log_timestamp - get time stamp for logging
296  * For adrastea this API returns QTIMER tick which is needed to synchronize
297  * host and fw log timestamps
298  * For ROME and other discrete solution this API returns system boot time stamp
299  *
300  * Return:
301  * QTIMER ticks(19.2MHz) for adrastea
302  * System tick for rome and other future discrete solutions
303  */
304 static inline uint64_t qdf_get_log_timestamp(void)
305 {
306 	return __qdf_get_log_timestamp();
307 }
308 
309 /**
310  * qdf_get_log_timestamp_usecs() - get time stamp for logging in microseconds
311  *
312  * Return: The current logging timestamp normalized to microsecond precision
313  */
314 static inline uint64_t qdf_get_log_timestamp_usecs(void)
315 {
316 	return qdf_log_timestamp_to_usecs(qdf_get_log_timestamp());
317 }
318 
319 /**
320  * qdf_get_monotonic_boottime - get monotonic kernel boot time
321  * This API is similar to qdf_get_system_boottime but it includes
322  * time spent in suspend.
323  *
324  * Return: Time in microseconds
325  */
326 static inline uint64_t qdf_get_monotonic_boottime(void)
327 {
328 	return __qdf_get_monotonic_boottime();
329 }
330 
331 #endif
332