xref: /wlan-dirver/qca-wifi-host-cmn/qdf/inc/qdf_time.h (revision dd4dc88b837a295134aa9869114a2efee0f4894b)
1 /*
2  * Copyright (c) 2014-2019 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 #define qdf_system_ticks_per_sec __qdf_system_ticks_per_sec
119 
120 /**
121  * qdf_system_ticks_to_msecs - convert ticks to milliseconds
122  * @clock_ticks: Number of ticks
123  *
124  * Return: unsigned int Time in milliseconds
125  */
126 static inline uint32_t qdf_system_ticks_to_msecs(unsigned long clock_ticks)
127 {
128 	return __qdf_system_ticks_to_msecs(clock_ticks);
129 }
130 
131 /**
132  * qdf_system_msecs_to_ticks - convert milliseconds to ticks
133  * @msec: Time in milliseconds
134  *
135  * Return: unsigned long number of ticks
136  */
137 static inline qdf_time_t qdf_system_msecs_to_ticks(uint32_t msecs)
138 {
139 	return __qdf_system_msecs_to_ticks(msecs);
140 }
141 
142 /**
143  * qdf_get_system_uptime - Return a monotonically increasing time
144  * This increments once per HZ ticks
145  *
146  * Return: qdf_time_t system up time in ticks
147  */
148 static inline qdf_time_t qdf_get_system_uptime(void)
149 {
150 	return __qdf_get_system_uptime();
151 }
152 
153 /**
154  * qdf_get_bootbased_boottime_ns() - Get the bootbased time in nanoseconds
155  *
156  * qdf_get_bootbased_boottime_ns() function returns the number of nanoseconds
157  * that have elapsed since the system was booted. It also includes the time when
158  * system was suspended.
159  *
160  * Return:
161  * The time since system booted in nanoseconds
162  */
163 
164 static inline uint64_t qdf_get_bootbased_boottime_ns(void)
165 {
166 	return __qdf_get_bootbased_boottime_ns();
167 }
168 
169 /**
170  * qdf_get_system_timestamp - Return current timestamp
171  *
172  * Return: unsigned long timestamp in ms.
173  */
174 static inline unsigned long qdf_get_system_timestamp(void)
175 {
176 	return __qdf_get_system_timestamp();
177 }
178 
179 /**
180  * qdf_udelay - delay in microseconds
181  * @usecs: Number of microseconds to delay
182  *
183  * Return: none
184  */
185 static inline void qdf_udelay(int usecs)
186 {
187 	__qdf_udelay(usecs);
188 }
189 
190 /**
191  * qdf_mdelay - Delay in milliseconds.
192  * @msec: Number of milliseconds to delay
193  *
194  * Return: none
195  */
196 static inline void qdf_mdelay(int msecs)
197 {
198 	__qdf_mdelay(msecs);
199 }
200 
201 /**
202  * qdf_system_time_after() - Check if a is later than b
203  * @a: Time stamp value a
204  * @b: Time stamp value b
205  *
206  * Return:
207  * true if a < b else false
208  */
209 static inline bool qdf_system_time_after(qdf_time_t a, qdf_time_t b)
210 {
211 	return __qdf_system_time_after(a, b);
212 }
213 
214 /**
215  * qdf_system_time_before() - Check if a is before b
216  * @a: Time stamp value a
217  * @b: Time stamp value b
218  *
219  * Return:
220  * true if a is before b else false
221  */
222 static inline bool qdf_system_time_before(qdf_time_t a, qdf_time_t b)
223 {
224 	return __qdf_system_time_before(a, b);
225 }
226 
227 /**
228  * qdf_system_time_after_eq() - Check if a atleast as recent as b, if not
229  * later
230  * @a: Time stamp value a
231  * @b: Time stamp value b
232  *
233  * Return:
234  * true if a >= b else false
235  */
236 static inline bool qdf_system_time_after_eq(qdf_time_t a, qdf_time_t b)
237 {
238 	return __qdf_system_time_after_eq(a, b);
239 }
240 
241 /**
242  * enum qdf_timestamp_unit - what unit the qdf timestamp is in
243  * @KERNEL_LOG: boottime time in uS (micro seconds)
244  * @QTIMER: QTIME in (1/19200)S
245  *
246  * This enum is used to distinguish which timer source is used.
247  */
248 enum qdf_timestamp_unit {
249 	KERNEL_LOG,
250 	QTIMER,
251 };
252 
253 #ifdef MSM_PLATFORM
254 #define QDF_LOG_TIMESTAMP_UNIT QTIMER
255 #define QDF_LOG_TIMESTAMP_CYCLES_PER_10_US 192
256 
257 static inline uint64_t qdf_log_timestamp_to_usecs(uint64_t time)
258 {
259 	/*
260 	 * Try to preserve precision by multiplying by 10 first.
261 	 * If that would cause a wrap around, divide first instead.
262 	 */
263 	if (time * 10 < time) {
264 		do_div(time, QDF_LOG_TIMESTAMP_CYCLES_PER_10_US);
265 		return time * 10;
266 	}
267 
268 	time = time * 10;
269 	do_div(time, QDF_LOG_TIMESTAMP_CYCLES_PER_10_US);
270 
271 	return time;
272 }
273 #else
274 #define QDF_LOG_TIMESTAMP_UNIT KERNEL_LOG
275 #define QDF_LOG_TIMESTAMP_CYCLES_PER_10_US 10
276 
277 static inline uint64_t qdf_log_timestamp_to_usecs(uint64_t time)
278 {
279 	/* timestamps are already in micro seconds */
280 	return time;
281 }
282 #endif /* end of MSM_PLATFORM */
283 
284 static inline void qdf_log_timestamp_to_secs(uint64_t time, uint64_t *secs,
285 					     uint64_t *usecs)
286 {
287 	*secs = qdf_log_timestamp_to_usecs(time);
288 	*usecs = do_div(*secs, 1000000ul);
289 }
290 
291 static inline uint64_t qdf_usecs_to_log_timestamp(uint64_t usecs)
292 {
293 	return (usecs * QDF_LOG_TIMESTAMP_CYCLES_PER_10_US) / 10;
294 }
295 
296 /**
297  * qdf_get_log_timestamp - get time stamp for logging
298  * For adrastea this API returns QTIMER tick which is needed to synchronize
299  * host and fw log timestamps
300  * For ROME and other discrete solution this API returns system boot time stamp
301  *
302  * Return:
303  * QTIMER ticks(19.2MHz) for adrastea
304  * System tick for rome and other future discrete solutions
305  */
306 static inline uint64_t qdf_get_log_timestamp(void)
307 {
308 	return __qdf_get_log_timestamp();
309 }
310 
311 /**
312  * qdf_get_log_timestamp_usecs() - get time stamp for logging in microseconds
313  *
314  * Return: The current logging timestamp normalized to microsecond precision
315  */
316 static inline uint64_t qdf_get_log_timestamp_usecs(void)
317 {
318 	return qdf_log_timestamp_to_usecs(qdf_get_log_timestamp());
319 }
320 
321 /**
322  * qdf_get_monotonic_boottime - get monotonic kernel boot time
323  * This API is similar to qdf_get_system_boottime but it includes
324  * time spent in suspend.
325  *
326  * Return: Time in microseconds
327  */
328 static inline uint64_t qdf_get_monotonic_boottime(void)
329 {
330 	return __qdf_get_monotonic_boottime();
331 }
332 
333 #endif
334