xref: /wlan-dirver/qca-wifi-host-cmn/qdf/inc/qdf_time.h (revision 97f44cd39e4ff816eaa1710279d28cf6b9e65ad9)
1 /*
2  * Copyright (c) 2014-2020 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_real_get - Gets the current wall clock as qdf_ktime_t object
71  *
72  * Return: current wall clock as qdf_ktime_t object
73  */
74 
75 static inline qdf_ktime_t qdf_ktime_real_get(void)
76 {
77 	return __qdf_ktime_real_get();
78 }
79 
80 /**
81  * qdf_ktime_add_ns - Adds qdf_ktime_t object and nanoseconds value and
82  * returns the qdf_ktime_t object
83  * @ktime: time as qdf_ktime_t object
84  * @ns: time in nanoseconds
85  *
86  * Return: qdf_ktime_t object
87  */
88 
89 static inline qdf_ktime_t qdf_ktime_add_ns(qdf_ktime_t ktime, int64_t ns)
90 {
91 	return __qdf_ktime_add_ns(ktime, ns);
92 }
93 
94 /**
95  * qdf_ktime_to_ms - Convert the qdf_ktime_t object into milliseconds
96  * @ktime: time as qdf_ktime_t object
97  *
98  * Return: qdf_ktime_t in milliseconds
99  */
100 
101 static inline int64_t qdf_ktime_to_ms(qdf_ktime_t ktime)
102 {
103 	return __qdf_ktime_to_ms(ktime);
104 }
105 
106 /**
107  * qdf_ktime_to_ns - Convert the qdf_ktime_t object into nanoseconds
108  * @ktime: time as qdf_ktime_t object
109  *
110  * Return: qdf_ktime_t in nanoseconds
111  */
112 
113 static inline int64_t qdf_ktime_to_ns(qdf_ktime_t ktime)
114 {
115 	return __qdf_ktime_to_ns(ktime);
116 }
117 
118 /**
119  * qdf_system_ticks - Count the number of ticks elapsed from the time when
120  * the system booted
121  *
122  * Return: ticks
123  */
124 static inline qdf_time_t qdf_system_ticks(void)
125 {
126 	return __qdf_system_ticks();
127 }
128 
129 #define qdf_system_ticks_per_sec __qdf_system_ticks_per_sec
130 
131 /**
132  * qdf_system_ticks_to_msecs - convert ticks to milliseconds
133  * @clock_ticks: Number of ticks
134  *
135  * Return: unsigned int Time in milliseconds
136  */
137 static inline uint32_t qdf_system_ticks_to_msecs(unsigned long clock_ticks)
138 {
139 	return __qdf_system_ticks_to_msecs(clock_ticks);
140 }
141 
142 /**
143  * qdf_system_msecs_to_ticks - convert milliseconds to ticks
144  * @msec: Time in milliseconds
145  *
146  * Return: unsigned long number of ticks
147  */
148 static inline qdf_time_t qdf_system_msecs_to_ticks(uint32_t msecs)
149 {
150 	return __qdf_system_msecs_to_ticks(msecs);
151 }
152 
153 /**
154  * qdf_get_system_uptime - Return a monotonically increasing time
155  * This increments once per HZ ticks
156  *
157  * Return: qdf_time_t system up time in ticks
158  */
159 static inline qdf_time_t qdf_get_system_uptime(void)
160 {
161 	return __qdf_get_system_uptime();
162 }
163 
164 /**
165  * qdf_get_bootbased_boottime_ns() - Get the bootbased time in nanoseconds
166  *
167  * qdf_get_bootbased_boottime_ns() function returns the number of nanoseconds
168  * that have elapsed since the system was booted. It also includes the time when
169  * system was suspended.
170  *
171  * Return:
172  * The time since system booted in nanoseconds
173  */
174 
175 static inline uint64_t qdf_get_bootbased_boottime_ns(void)
176 {
177 	return __qdf_get_bootbased_boottime_ns();
178 }
179 
180 /**
181  * qdf_get_system_timestamp - Return current timestamp
182  *
183  * Return: unsigned long timestamp in ms.
184  */
185 static inline unsigned long qdf_get_system_timestamp(void)
186 {
187 	return __qdf_get_system_timestamp();
188 }
189 
190 /**
191  * qdf_udelay - delay in microseconds
192  * @usecs: Number of microseconds to delay
193  *
194  * Return: none
195  */
196 static inline void qdf_udelay(int usecs)
197 {
198 	__qdf_udelay(usecs);
199 }
200 
201 /**
202  * qdf_mdelay - Delay in milliseconds.
203  * @msec: Number of milliseconds to delay
204  *
205  * Return: none
206  */
207 static inline void qdf_mdelay(int msecs)
208 {
209 	__qdf_mdelay(msecs);
210 }
211 
212 /**
213  * qdf_system_time_after() - Check if a is later than b
214  * @a: Time stamp value a
215  * @b: Time stamp value b
216  *
217  * Return:
218  * true if a < b else false
219  */
220 static inline bool qdf_system_time_after(qdf_time_t a, qdf_time_t b)
221 {
222 	return __qdf_system_time_after(a, b);
223 }
224 
225 /**
226  * qdf_system_time_before() - Check if a is before b
227  * @a: Time stamp value a
228  * @b: Time stamp value b
229  *
230  * Return:
231  * true if a is before b else false
232  */
233 static inline bool qdf_system_time_before(qdf_time_t a, qdf_time_t b)
234 {
235 	return __qdf_system_time_before(a, b);
236 }
237 
238 /**
239  * qdf_system_time_after_eq() - Check if a atleast as recent as b, if not
240  * later
241  * @a: Time stamp value a
242  * @b: Time stamp value b
243  *
244  * Return:
245  * true if a >= b else false
246  */
247 static inline bool qdf_system_time_after_eq(qdf_time_t a, qdf_time_t b)
248 {
249 	return __qdf_system_time_after_eq(a, b);
250 }
251 
252 /**
253  * enum qdf_timestamp_unit - what unit the qdf timestamp is in
254  * @KERNEL_LOG: boottime time in uS (micro seconds)
255  * @QTIMER: QTIME in (1/19200)S
256  *
257  * This enum is used to distinguish which timer source is used.
258  */
259 enum qdf_timestamp_unit {
260 	KERNEL_LOG,
261 	QTIMER,
262 };
263 
264 #ifdef MSM_PLATFORM
265 #define QDF_LOG_TIMESTAMP_UNIT QTIMER
266 #define QDF_LOG_TIMESTAMP_CYCLES_PER_10_US 192
267 
268 static inline uint64_t qdf_log_timestamp_to_usecs(uint64_t time)
269 {
270 	/*
271 	 * Try to preserve precision by multiplying by 10 first.
272 	 * If that would cause a wrap around, divide first instead.
273 	 */
274 	if (time * 10 < time) {
275 		do_div(time, QDF_LOG_TIMESTAMP_CYCLES_PER_10_US);
276 		return time * 10;
277 	}
278 
279 	time = time * 10;
280 	do_div(time, QDF_LOG_TIMESTAMP_CYCLES_PER_10_US);
281 
282 	return time;
283 }
284 
285 /**
286  * qdf_get_log_timestamp_lightweight - get time stamp for logging
287  * For adrastea this API returns QTIMER tick which is needed to synchronize
288  * host and fw log timestamps
289  * For ROME and other discrete solution this API returns system boot time stamp
290  *
291  * Return:
292  * QTIMER ticks(19.2MHz) for adrastea
293  * System tick for rome and other 3rd party platform solutions
294  */
295 static inline uint64_t qdf_get_log_timestamp_lightweight(void)
296 {
297 	return __qdf_get_log_timestamp();
298 }
299 #else
300 #define QDF_LOG_TIMESTAMP_UNIT KERNEL_LOG
301 #define QDF_LOG_TIMESTAMP_CYCLES_PER_10_US 10
302 
303 static inline uint64_t qdf_log_timestamp_to_usecs(uint64_t time)
304 {
305 	/* timestamps are already in micro seconds */
306 	return time;
307 }
308 
309 static inline uint64_t qdf_get_log_timestamp_lightweight(void)
310 {
311 	uint64_t timestamp_us;
312 
313 	timestamp_us = __qdf_system_ticks_to_msecs(qdf_system_ticks()) * 1000;
314 	return timestamp_us;
315 }
316 #endif /* end of MSM_PLATFORM */
317 
318 static inline void qdf_log_timestamp_to_secs(uint64_t time, uint64_t *secs,
319 					     uint64_t *usecs)
320 {
321 	*secs = qdf_log_timestamp_to_usecs(time);
322 	*usecs = do_div(*secs, 1000000ul);
323 }
324 
325 static inline uint64_t qdf_usecs_to_log_timestamp(uint64_t usecs)
326 {
327 	return (usecs * QDF_LOG_TIMESTAMP_CYCLES_PER_10_US) / 10;
328 }
329 
330 /**
331  * qdf_get_log_timestamp - get time stamp for logging
332  * For adrastea this API returns QTIMER tick which is needed to synchronize
333  * host and fw log timestamps
334  * For ROME and other discrete solution this API returns system boot time stamp
335  *
336  * Return:
337  * QTIMER ticks(19.2MHz) for adrastea
338  * System tick for rome and other future discrete solutions
339  */
340 static inline uint64_t qdf_get_log_timestamp(void)
341 {
342 	return __qdf_get_log_timestamp();
343 }
344 
345 /**
346  * qdf_get_log_timestamp_usecs() - get time stamp for logging in microseconds
347  *
348  * Return: The current logging timestamp normalized to microsecond precision
349  */
350 static inline uint64_t qdf_get_log_timestamp_usecs(void)
351 {
352 	return qdf_log_timestamp_to_usecs(qdf_get_log_timestamp());
353 }
354 
355 /**
356  * qdf_get_monotonic_boottime - get monotonic kernel boot time
357  * This API is similar to qdf_get_system_boottime but it includes
358  * time spent in suspend.
359  *
360  * Return: Time in microseconds
361  */
362 static inline uint64_t qdf_get_monotonic_boottime(void)
363 {
364 	return __qdf_get_monotonic_boottime();
365 }
366 
367 #endif
368