xref: /wlan-driver/qca-wifi-host-cmn/qdf/inc/qdf_hrtimer.h (revision 5113495b16420b49004c444715d2daae2066e7dc)
1*5113495bSYour Name /*
2*5113495bSYour Name  * Copyright (c) 2014-2018, 2021 The Linux Foundation. All rights reserved.
3*5113495bSYour Name  * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
4*5113495bSYour Name  *
5*5113495bSYour Name  * Permission to use, copy, modify, and/or distribute this software for
6*5113495bSYour Name  * any purpose with or without fee is hereby granted, provided that the
7*5113495bSYour Name  * above copyright notice and this permission notice appear in all
8*5113495bSYour Name  * copies.
9*5113495bSYour Name  *
10*5113495bSYour Name  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11*5113495bSYour Name  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12*5113495bSYour Name  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13*5113495bSYour Name  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14*5113495bSYour Name  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15*5113495bSYour Name  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16*5113495bSYour Name  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17*5113495bSYour Name  * PERFORMANCE OF THIS SOFTWARE.
18*5113495bSYour Name  */
19*5113495bSYour Name 
20*5113495bSYour Name /**
21*5113495bSYour Name  * DOC: qdf_hrtimer
22*5113495bSYour Name  * This file abstracts high resolution timers running in hardware context.
23*5113495bSYour Name  */
24*5113495bSYour Name 
25*5113495bSYour Name #ifndef _QDF_HRTIMER_H
26*5113495bSYour Name #define _QDF_HRTIMER_H
27*5113495bSYour Name 
28*5113495bSYour Name #include <qdf_types.h>
29*5113495bSYour Name #include <i_qdf_hrtimer.h>
30*5113495bSYour Name #include <qdf_time.h>
31*5113495bSYour Name 
32*5113495bSYour Name /* Context independent hrtimer object */
33*5113495bSYour Name typedef __qdf_hrtimer_data_t qdf_hrtimer_data_t;
34*5113495bSYour Name 
35*5113495bSYour Name /* Platform independent timer callback function */
36*5113495bSYour Name typedef enum qdf_hrtimer_restart_status(*qdf_hrtimer_func_t)
37*5113495bSYour Name 					(qdf_hrtimer_data_t *timer);
38*5113495bSYour Name 
39*5113495bSYour Name #ifdef ENHANCED_OS_ABSTRACTION
40*5113495bSYour Name /**
41*5113495bSYour Name  * qdf_hrtimer_start() - Starts hrtimer in given context
42*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
43*5113495bSYour Name  * @interval: interval to forward as qdf_ktime_t object
44*5113495bSYour Name  * @mode: mode of qdf_hrtimer_data_t
45*5113495bSYour Name  *
46*5113495bSYour Name  * Starts hrtimer in given context
47*5113495bSYour Name  *
48*5113495bSYour Name  * Return: void
49*5113495bSYour Name  */
50*5113495bSYour Name void qdf_hrtimer_start(qdf_hrtimer_data_t *timer, qdf_ktime_t interval,
51*5113495bSYour Name 		       enum qdf_hrtimer_mode mode);
52*5113495bSYour Name 
53*5113495bSYour Name /**
54*5113495bSYour Name  * qdf_hrtimer_cancel() - Cancels hrtimer in given context
55*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
56*5113495bSYour Name  *
57*5113495bSYour Name  * Cancels hrtimer in given context
58*5113495bSYour Name  *
59*5113495bSYour Name  * Return: int
60*5113495bSYour Name  */
61*5113495bSYour Name int qdf_hrtimer_cancel(qdf_hrtimer_data_t *timer);
62*5113495bSYour Name 
63*5113495bSYour Name /**
64*5113495bSYour Name  * qdf_hrtimer_init() - init hrtimer based on context
65*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
66*5113495bSYour Name  * @callback: callback function to be fired
67*5113495bSYour Name  * @clock: clock type
68*5113495bSYour Name  * @mode: mode of qdf_hrtimer_data_t
69*5113495bSYour Name  * @ctx: interrupt context mode
70*5113495bSYour Name  *
71*5113495bSYour Name  * starts hrtimer in a context passed as per qdf_context_mode
72*5113495bSYour Name  *
73*5113495bSYour Name  * Return: void
74*5113495bSYour Name  */
75*5113495bSYour Name void qdf_hrtimer_init(qdf_hrtimer_data_t *timer,
76*5113495bSYour Name 		      qdf_hrtimer_func_t callback,
77*5113495bSYour Name 		      enum qdf_clock_id clock,
78*5113495bSYour Name 		      enum qdf_hrtimer_mode mode,
79*5113495bSYour Name 		      enum qdf_context_mode ctx);
80*5113495bSYour Name 
81*5113495bSYour Name /**
82*5113495bSYour Name  * qdf_time_ms_to_ktime() - Converts milliseconds to a qdf_ktime_t object
83*5113495bSYour Name  * @ms: time in milliseconds
84*5113495bSYour Name  *
85*5113495bSYour Name  * Return: milliseconds as ktime object
86*5113495bSYour Name  */
87*5113495bSYour Name qdf_ktime_t qdf_time_ms_to_ktime(uint64_t ms);
88*5113495bSYour Name 
89*5113495bSYour Name /**
90*5113495bSYour Name  * qdf_hrtimer_kill() - kills hrtimer in given context
91*5113495bSYour Name  * @timer: pointer to the hrtimer object
92*5113495bSYour Name  *
93*5113495bSYour Name  * kills hrtimer in given context
94*5113495bSYour Name  *
95*5113495bSYour Name  * Return: void
96*5113495bSYour Name  */
97*5113495bSYour Name void qdf_hrtimer_kill(__qdf_hrtimer_data_t *timer);
98*5113495bSYour Name 
99*5113495bSYour Name /**
100*5113495bSYour Name  * qdf_hrtimer_get_remaining() - check remaining time in the timer
101*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
102*5113495bSYour Name  *
103*5113495bSYour Name  * check whether the timer is on one of the queues
104*5113495bSYour Name  *
105*5113495bSYour Name  * Return: remaining time as qdf_ktime_t object
106*5113495bSYour Name  */
107*5113495bSYour Name qdf_ktime_t qdf_hrtimer_get_remaining(qdf_hrtimer_data_t *timer);
108*5113495bSYour Name 
109*5113495bSYour Name /**
110*5113495bSYour Name  * qdf_hrtimer_is_queued() - check whether the timer is on one of the queues
111*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
112*5113495bSYour Name  *
113*5113495bSYour Name  * check whether the timer is on one of the queues
114*5113495bSYour Name  *
115*5113495bSYour Name  * Return: false when the timer was not in queue
116*5113495bSYour Name  *         true when the timer was in queue
117*5113495bSYour Name  */
118*5113495bSYour Name bool qdf_hrtimer_is_queued(qdf_hrtimer_data_t *timer);
119*5113495bSYour Name 
120*5113495bSYour Name /**
121*5113495bSYour Name  * qdf_hrtimer_callback_running() - check if callback is running
122*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
123*5113495bSYour Name  *
124*5113495bSYour Name  * check whether the timer is running the callback function
125*5113495bSYour Name  *
126*5113495bSYour Name  * Return: false when callback is not running
127*5113495bSYour Name  *         true when callback is running
128*5113495bSYour Name  */
129*5113495bSYour Name bool qdf_hrtimer_callback_running(qdf_hrtimer_data_t *timer);
130*5113495bSYour Name 
131*5113495bSYour Name /**
132*5113495bSYour Name  * qdf_hrtimer_active() - check if timer is active
133*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
134*5113495bSYour Name  *
135*5113495bSYour Name  * Check if timer is active. A timer is active, when it is enqueued into
136*5113495bSYour Name  * the rbtree or the callback function is running.
137*5113495bSYour Name  *
138*5113495bSYour Name  * Return: false if timer is not active
139*5113495bSYour Name  *         true if timer is active
140*5113495bSYour Name  */
141*5113495bSYour Name bool qdf_hrtimer_active(qdf_hrtimer_data_t *timer);
142*5113495bSYour Name 
143*5113495bSYour Name /**
144*5113495bSYour Name  * qdf_hrtimer_cb_get_time() - get remaining time in callback
145*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
146*5113495bSYour Name  *
147*5113495bSYour Name  * Get remaining time in the hrtimer callback
148*5113495bSYour Name  *
149*5113495bSYour Name  * Return: time remaining as qdf_ktime_t object
150*5113495bSYour Name  */
151*5113495bSYour Name qdf_ktime_t qdf_hrtimer_cb_get_time(qdf_hrtimer_data_t *timer);
152*5113495bSYour Name 
153*5113495bSYour Name /**
154*5113495bSYour Name  * qdf_hrtimer_forward() - forward the hrtimer
155*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
156*5113495bSYour Name  * @now: current time as qdf_ktime_t object
157*5113495bSYour Name  * @interval: interval to forward as qdf_ktime_t object
158*5113495bSYour Name  *
159*5113495bSYour Name  * Forward the timer expiry so it will expire in the future
160*5113495bSYour Name  *
161*5113495bSYour Name  * Return: the number of overruns
162*5113495bSYour Name  */
163*5113495bSYour Name uint64_t qdf_hrtimer_forward(qdf_hrtimer_data_t *timer,
164*5113495bSYour Name 			     qdf_ktime_t now,
165*5113495bSYour Name 			     qdf_ktime_t interval);
166*5113495bSYour Name 
167*5113495bSYour Name void qdf_hrtimer_add_expires(qdf_hrtimer_data_t *timer, qdf_ktime_t interval);
168*5113495bSYour Name #else
169*5113495bSYour Name /**
170*5113495bSYour Name  * qdf_hrtimer_start() - Starts hrtimer in given context
171*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
172*5113495bSYour Name  * @interval: interval to forward as qdf_ktime_t object
173*5113495bSYour Name  * @mode: mode of qdf_hrtimer_data_t
174*5113495bSYour Name  *
175*5113495bSYour Name  * Starts hrtimer in given context
176*5113495bSYour Name  *
177*5113495bSYour Name  * Return: void
178*5113495bSYour Name  */
179*5113495bSYour Name static inline
qdf_hrtimer_start(qdf_hrtimer_data_t * timer,qdf_ktime_t interval,enum qdf_hrtimer_mode mode)180*5113495bSYour Name void qdf_hrtimer_start(qdf_hrtimer_data_t *timer, qdf_ktime_t interval,
181*5113495bSYour Name 		       enum qdf_hrtimer_mode mode)
182*5113495bSYour Name {
183*5113495bSYour Name 	__qdf_hrtimer_start(timer, interval, mode);
184*5113495bSYour Name }
185*5113495bSYour Name 
186*5113495bSYour Name /**
187*5113495bSYour Name  * qdf_hrtimer_cancel() - Cancels hrtimer in given context
188*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
189*5113495bSYour Name  *
190*5113495bSYour Name  * Cancels hrtimer in given context
191*5113495bSYour Name  *
192*5113495bSYour Name  * Return: int
193*5113495bSYour Name  */
194*5113495bSYour Name static inline
qdf_hrtimer_cancel(qdf_hrtimer_data_t * timer)195*5113495bSYour Name int qdf_hrtimer_cancel(qdf_hrtimer_data_t *timer)
196*5113495bSYour Name {
197*5113495bSYour Name 	return __qdf_hrtimer_cancel(timer);
198*5113495bSYour Name }
199*5113495bSYour Name 
200*5113495bSYour Name /**
201*5113495bSYour Name  * qdf_hrtimer_init() - init hrtimer based on context
202*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
203*5113495bSYour Name  * @callback: callback function to be fired
204*5113495bSYour Name  * @clock: clock type
205*5113495bSYour Name  * @mode: mode of qdf_hrtimer_data_t
206*5113495bSYour Name  * @ctx: interrupt context mode
207*5113495bSYour Name  *
208*5113495bSYour Name  * starts hrtimer in a context passed as per qdf_context_mode
209*5113495bSYour Name  *
210*5113495bSYour Name  * Return: void
211*5113495bSYour Name  */
qdf_hrtimer_init(qdf_hrtimer_data_t * timer,qdf_hrtimer_func_t callback,enum qdf_clock_id clock,enum qdf_hrtimer_mode mode,enum qdf_context_mode ctx)212*5113495bSYour Name static inline void qdf_hrtimer_init(qdf_hrtimer_data_t *timer,
213*5113495bSYour Name 				    qdf_hrtimer_func_t callback,
214*5113495bSYour Name 				    enum qdf_clock_id clock,
215*5113495bSYour Name 				    enum qdf_hrtimer_mode mode,
216*5113495bSYour Name 				    enum qdf_context_mode ctx)
217*5113495bSYour Name {
218*5113495bSYour Name 	__qdf_hrtimer_init(timer, callback, clock, mode, ctx);
219*5113495bSYour Name }
220*5113495bSYour Name 
221*5113495bSYour Name /**
222*5113495bSYour Name  * qdf_time_ms_to_ktime() - Converts milliseconds to a qdf_ktime_t object
223*5113495bSYour Name  * @ms: time in milliseconds
224*5113495bSYour Name  *
225*5113495bSYour Name  * Return: milliseconds as qdf_ktime_t object
226*5113495bSYour Name  */
qdf_time_ms_to_ktime(uint64_t ms)227*5113495bSYour Name static inline qdf_ktime_t qdf_time_ms_to_ktime(uint64_t ms)
228*5113495bSYour Name {
229*5113495bSYour Name 	return __qdf_time_ms_to_ktime(ms);
230*5113495bSYour Name }
231*5113495bSYour Name 
232*5113495bSYour Name /**
233*5113495bSYour Name  * qdf_hrtimer_kill() - kills hrtimer in given context
234*5113495bSYour Name  * @timer: pointer to the hrtimer object
235*5113495bSYour Name  *
236*5113495bSYour Name  * kills hrtimer in given context
237*5113495bSYour Name  *
238*5113495bSYour Name  * Return: void
239*5113495bSYour Name  */
240*5113495bSYour Name static inline
qdf_hrtimer_kill(__qdf_hrtimer_data_t * timer)241*5113495bSYour Name void qdf_hrtimer_kill(__qdf_hrtimer_data_t *timer)
242*5113495bSYour Name {
243*5113495bSYour Name 	 __qdf_hrtimer_kill(timer);
244*5113495bSYour Name }
245*5113495bSYour Name 
246*5113495bSYour Name /**
247*5113495bSYour Name  * qdf_hrtimer_get_remaining() - check remaining time in the timer
248*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
249*5113495bSYour Name  *
250*5113495bSYour Name  * check whether the timer is on one of the queues
251*5113495bSYour Name  *
252*5113495bSYour Name  * Return: remaining time as qdf_ktime_t object
253*5113495bSYour Name  */
qdf_hrtimer_get_remaining(qdf_hrtimer_data_t * timer)254*5113495bSYour Name static inline qdf_ktime_t qdf_hrtimer_get_remaining(qdf_hrtimer_data_t *timer)
255*5113495bSYour Name {
256*5113495bSYour Name 	return __qdf_hrtimer_get_remaining(timer);
257*5113495bSYour Name }
258*5113495bSYour Name 
259*5113495bSYour Name /**
260*5113495bSYour Name  * qdf_hrtimer_is_queued() - check whether the timer is on one of the queues
261*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
262*5113495bSYour Name  *
263*5113495bSYour Name  * check whether the timer is on one of the queues
264*5113495bSYour Name  *
265*5113495bSYour Name  * Return: false when the timer was not in queue
266*5113495bSYour Name  *         true when the timer was in queue
267*5113495bSYour Name  */
qdf_hrtimer_is_queued(qdf_hrtimer_data_t * timer)268*5113495bSYour Name static inline bool qdf_hrtimer_is_queued(qdf_hrtimer_data_t *timer)
269*5113495bSYour Name {
270*5113495bSYour Name 	return __qdf_hrtimer_is_queued(timer);
271*5113495bSYour Name }
272*5113495bSYour Name 
273*5113495bSYour Name /**
274*5113495bSYour Name  * qdf_hrtimer_callback_running() - check if callback is running
275*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
276*5113495bSYour Name  *
277*5113495bSYour Name  * check whether the timer is running the callback function
278*5113495bSYour Name  *
279*5113495bSYour Name  * Return: false when callback is not running
280*5113495bSYour Name  *         true when callback is running
281*5113495bSYour Name  */
qdf_hrtimer_callback_running(qdf_hrtimer_data_t * timer)282*5113495bSYour Name static inline bool qdf_hrtimer_callback_running(qdf_hrtimer_data_t *timer)
283*5113495bSYour Name {
284*5113495bSYour Name 	return __qdf_hrtimer_callback_running(timer);
285*5113495bSYour Name }
286*5113495bSYour Name 
287*5113495bSYour Name /**
288*5113495bSYour Name  * qdf_hrtimer_active() - check if timer is active
289*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
290*5113495bSYour Name  *
291*5113495bSYour Name  * Check if timer is active. A timer is active, when it is enqueued into
292*5113495bSYour Name  * the rbtree or the callback function is running.
293*5113495bSYour Name  *
294*5113495bSYour Name  * Return: false if timer is not active
295*5113495bSYour Name  *         true if timer is active
296*5113495bSYour Name  */
qdf_hrtimer_active(qdf_hrtimer_data_t * timer)297*5113495bSYour Name static inline bool qdf_hrtimer_active(qdf_hrtimer_data_t *timer)
298*5113495bSYour Name {
299*5113495bSYour Name 	return __qdf_hrtimer_active(timer);
300*5113495bSYour Name }
301*5113495bSYour Name 
302*5113495bSYour Name /**
303*5113495bSYour Name  * qdf_hrtimer_cb_get_time() - get remaining time in callback
304*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
305*5113495bSYour Name  *
306*5113495bSYour Name  * Get remaining time in the hrtimer callback
307*5113495bSYour Name  *
308*5113495bSYour Name  * Return: time remaining as qdf_ktime_t object
309*5113495bSYour Name  */
qdf_hrtimer_cb_get_time(qdf_hrtimer_data_t * timer)310*5113495bSYour Name static inline qdf_ktime_t qdf_hrtimer_cb_get_time(qdf_hrtimer_data_t *timer)
311*5113495bSYour Name {
312*5113495bSYour Name 	return __qdf_hrtimer_cb_get_time(timer);
313*5113495bSYour Name }
314*5113495bSYour Name 
315*5113495bSYour Name /**
316*5113495bSYour Name  * qdf_hrtimer_forward() - forward the hrtimer
317*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
318*5113495bSYour Name  * @now: current time as qdf_ktime_t object
319*5113495bSYour Name  * @interval: interval to forward as qdf_ktime_t object
320*5113495bSYour Name  *
321*5113495bSYour Name  * Forward the timer expiry so it will expire in the future
322*5113495bSYour Name  *
323*5113495bSYour Name  * Return: the number of overruns
324*5113495bSYour Name  */
qdf_hrtimer_forward(qdf_hrtimer_data_t * timer,qdf_ktime_t now,qdf_ktime_t interval)325*5113495bSYour Name static inline uint64_t qdf_hrtimer_forward(qdf_hrtimer_data_t *timer,
326*5113495bSYour Name 					   qdf_ktime_t now,
327*5113495bSYour Name 					   qdf_ktime_t interval)
328*5113495bSYour Name {
329*5113495bSYour Name 	return __qdf_hrtimer_forward(timer, now, interval);
330*5113495bSYour Name }
331*5113495bSYour Name 
332*5113495bSYour Name /**
333*5113495bSYour Name  * qdf_hrtimer_add_expires() - Add expiry to hrtimer with given interval
334*5113495bSYour Name  * @timer: pointer to the qdf_hrtimer_data_t object
335*5113495bSYour Name  * @interval: interval to add as qdf_ktime_t object
336*5113495bSYour Name  *
337*5113495bSYour Name  * Add the timer expiry so it will expire in the future
338*5113495bSYour Name  *
339*5113495bSYour Name  * Return: None
340*5113495bSYour Name  */
341*5113495bSYour Name static inline
qdf_hrtimer_add_expires(qdf_hrtimer_data_t * timer,qdf_ktime_t interval)342*5113495bSYour Name void qdf_hrtimer_add_expires(qdf_hrtimer_data_t *timer, qdf_ktime_t interval)
343*5113495bSYour Name {
344*5113495bSYour Name 	return __qdf_hrtimer_add_expires(timer, interval);
345*5113495bSYour Name }
346*5113495bSYour Name 
347*5113495bSYour Name #endif
348*5113495bSYour Name 
349*5113495bSYour Name #endif /* _QDF_HRTIMER_H */
350