xref: /wlan-driver/qca-wifi-host-cmn/qdf/inc/qdf_mc_timer.h (revision 5113495b16420b49004c444715d2daae2066e7dc)
1*5113495bSYour Name /*
2*5113495bSYour Name  * Copyright (c) 2014-2019, 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_mc_timer
22*5113495bSYour Name  * QCA driver framework timer APIs serialized to MC thread
23*5113495bSYour Name  */
24*5113495bSYour Name 
25*5113495bSYour Name #if !defined(__QDF_MC_TIMER_H)
26*5113495bSYour Name #define __QDF_MC_TIMER_H
27*5113495bSYour Name 
28*5113495bSYour Name /* Include Files */
29*5113495bSYour Name #include <qdf_types.h>
30*5113495bSYour Name #include <qdf_status.h>
31*5113495bSYour Name #include <qdf_lock.h>
32*5113495bSYour Name #include <i_qdf_mc_timer.h>
33*5113495bSYour Name 
34*5113495bSYour Name #ifdef TIMER_MANAGER
35*5113495bSYour Name #include <qdf_list.h>
36*5113495bSYour Name #endif
37*5113495bSYour Name 
38*5113495bSYour Name /* Preprocessor definitions and constants */
39*5113495bSYour Name #define QDF_TIMER_STATE_COOKIE (0x12)
40*5113495bSYour Name #define QDF_MC_TIMER_TO_MS_UNIT (1000)
41*5113495bSYour Name #define QDF_MC_TIMER_TO_SEC_UNIT (1000000)
42*5113495bSYour Name 
43*5113495bSYour Name /* Type declarations */
44*5113495bSYour Name /* qdf Timer callback function prototype (well, actually a prototype for
45*5113495bSYour Name  * a pointer to this callback function)
46*5113495bSYour Name  */
47*5113495bSYour Name typedef void (*qdf_mc_timer_callback_t)(void *user_data);
48*5113495bSYour Name 
49*5113495bSYour Name typedef enum {
50*5113495bSYour Name 	QDF_TIMER_STATE_UNUSED = QDF_TIMER_STATE_COOKIE,
51*5113495bSYour Name 	QDF_TIMER_STATE_STOPPED,
52*5113495bSYour Name 	QDF_TIMER_STATE_STARTING,
53*5113495bSYour Name 	QDF_TIMER_STATE_RUNNING,
54*5113495bSYour Name } QDF_TIMER_STATE;
55*5113495bSYour Name 
56*5113495bSYour Name #ifdef TIMER_MANAGER
57*5113495bSYour Name struct qdf_mc_timer_s;
58*5113495bSYour Name typedef struct qdf_mc_timer_node_s {
59*5113495bSYour Name 	qdf_list_node_t node;
60*5113495bSYour Name 	char *file_name;
61*5113495bSYour Name 	uint32_t line_num;
62*5113495bSYour Name 	struct qdf_mc_timer_s *qdf_timer;
63*5113495bSYour Name } qdf_mc_timer_node_t;
64*5113495bSYour Name #endif
65*5113495bSYour Name 
66*5113495bSYour Name typedef struct qdf_mc_timer_s {
67*5113495bSYour Name #ifdef TIMER_MANAGER
68*5113495bSYour Name 	qdf_mc_timer_node_t *timer_node;
69*5113495bSYour Name #endif
70*5113495bSYour Name 	qdf_mc_timer_platform_t platform_info;
71*5113495bSYour Name 	qdf_mc_timer_callback_t callback;
72*5113495bSYour Name 	void *user_data;
73*5113495bSYour Name 	qdf_mutex_t lock;
74*5113495bSYour Name 	QDF_TIMER_TYPE type;
75*5113495bSYour Name 	QDF_TIMER_STATE state;
76*5113495bSYour Name 	qdf_time_t timer_start_jiffies;
77*5113495bSYour Name 	qdf_time_t timer_end_jiffies;
78*5113495bSYour Name } qdf_mc_timer_t;
79*5113495bSYour Name 
80*5113495bSYour Name 
81*5113495bSYour Name /**
82*5113495bSYour Name  * qdf_try_allowing_sleep() - clean up timer states after it has been deactivated
83*5113495bSYour Name  * @type: timer type
84*5113495bSYour Name  *
85*5113495bSYour Name  * Clean up timer states after it has been deactivated check and try to allow
86*5113495bSYour Name  * sleep after a timer has been stopped or expired.
87*5113495bSYour Name  *
88*5113495bSYour Name  * Return: none
89*5113495bSYour Name  */
90*5113495bSYour Name void qdf_try_allowing_sleep(QDF_TIMER_TYPE type);
91*5113495bSYour Name 
92*5113495bSYour Name #ifdef TIMER_MANAGER
93*5113495bSYour Name /**
94*5113495bSYour Name  * qdf_mc_timer_manager_init() - initialize QDF debug timer manager
95*5113495bSYour Name  * This API initializes QDF timer debug functionality.
96*5113495bSYour Name  *
97*5113495bSYour Name  * Return: none
98*5113495bSYour Name  */
99*5113495bSYour Name void qdf_mc_timer_manager_init(void);
100*5113495bSYour Name 
101*5113495bSYour Name /**
102*5113495bSYour Name  * qdf_mc_timer_manager_exit() - exit QDF timer debug functionality
103*5113495bSYour Name  * This API exists QDF timer debug functionality
104*5113495bSYour Name  *
105*5113495bSYour Name  * Return: none
106*5113495bSYour Name  */
107*5113495bSYour Name void qdf_mc_timer_manager_exit(void);
108*5113495bSYour Name 
109*5113495bSYour Name /**
110*5113495bSYour Name  * qdf_mc_timer_check_for_leaks() - Assert there are no active mc timers
111*5113495bSYour Name  *
112*5113495bSYour Name  * If there are active timers, this API prints them and panics.
113*5113495bSYour Name  *
114*5113495bSYour Name  * Return: None
115*5113495bSYour Name  */
116*5113495bSYour Name void qdf_mc_timer_check_for_leaks(void);
117*5113495bSYour Name #else
qdf_mc_timer_manager_init(void)118*5113495bSYour Name static inline void qdf_mc_timer_manager_init(void)
119*5113495bSYour Name {
120*5113495bSYour Name }
121*5113495bSYour Name 
qdf_mc_timer_manager_exit(void)122*5113495bSYour Name static inline void qdf_mc_timer_manager_exit(void)
123*5113495bSYour Name {
124*5113495bSYour Name }
125*5113495bSYour Name 
qdf_mc_timer_check_for_leaks(void)126*5113495bSYour Name static inline void qdf_mc_timer_check_for_leaks(void)
127*5113495bSYour Name {
128*5113495bSYour Name }
129*5113495bSYour Name #endif
130*5113495bSYour Name /**
131*5113495bSYour Name  * qdf_mc_timer_get_current_state() - get the current state of the timer
132*5113495bSYour Name  * @timer:  Pointer to timer object
133*5113495bSYour Name  *
134*5113495bSYour Name  * Return:
135*5113495bSYour Name  * QDF_TIMER_STATE - qdf timer state
136*5113495bSYour Name  */
137*5113495bSYour Name QDF_TIMER_STATE qdf_mc_timer_get_current_state(qdf_mc_timer_t *timer);
138*5113495bSYour Name 
139*5113495bSYour Name /**
140*5113495bSYour Name  * qdf_mc_timer_init() - initialize a QDF timer
141*5113495bSYour Name  * @timer: Pointer to timer object
142*5113495bSYour Name  * @timer_type: Type of timer
143*5113495bSYour Name  * @callback: Callback to be called after timer expiry
144*5113495bSYour Name  * @user_data: User data which will be passed to callback function
145*5113495bSYour Name  *
146*5113495bSYour Name  * This API initializes a QDF Timer object.
147*5113495bSYour Name  *
148*5113495bSYour Name  * qdf_mc_timer_init() initializes a QDF Timer object.  A timer must be
149*5113495bSYour Name  * initialized by calling qdf_mc_timer_initialize() before it may be used in
150*5113495bSYour Name  * any other timer functions.
151*5113495bSYour Name  *
152*5113495bSYour Name  * Attempting to initialize timer that is already initialized results in
153*5113495bSYour Name  * a failure. A destroyed timer object can be re-initialized with a call to
154*5113495bSYour Name  * qdf_mc_timer_init().  The results of otherwise referencing the object
155*5113495bSYour Name  * after it has been destroyed are undefined.
156*5113495bSYour Name  *
157*5113495bSYour Name  *  Calls to QDF timer functions to manipulate the timer such
158*5113495bSYour Name  *  as qdf_mc_timer_set() will fail if the timer is not initialized or has
159*5113495bSYour Name  *  been destroyed.  Therefore, don't use the timer after it has been
160*5113495bSYour Name  *  destroyed until it has been re-initialized.
161*5113495bSYour Name  *
162*5113495bSYour Name  *  All callback will be executed within the CDS main thread unless it is
163*5113495bSYour Name  *  initialized from the Tx thread flow, in which case it will be executed
164*5113495bSYour Name  *  within the tx thread flow.
165*5113495bSYour Name  *
166*5113495bSYour Name  * Return:
167*5113495bSYour Name  * QDF_STATUS_SUCCESS - Timer is initialized successfully
168*5113495bSYour Name  * QDF failure status - Timer initialization failed
169*5113495bSYour Name  */
170*5113495bSYour Name #ifdef TIMER_MANAGER
171*5113495bSYour Name #define qdf_mc_timer_init(timer, timer_type, callback, user_data) \
172*5113495bSYour Name 	qdf_mc_timer_init_debug(timer, timer_type, callback, user_data, \
173*5113495bSYour Name 				__FILE__, __LINE__)
174*5113495bSYour Name 
175*5113495bSYour Name QDF_STATUS qdf_mc_timer_init_debug(qdf_mc_timer_t *timer,
176*5113495bSYour Name 				   QDF_TIMER_TYPE timer_type,
177*5113495bSYour Name 				   qdf_mc_timer_callback_t callback,
178*5113495bSYour Name 				   void *user_data, char *file_name,
179*5113495bSYour Name 				   uint32_t line_num);
180*5113495bSYour Name #else
181*5113495bSYour Name QDF_STATUS qdf_mc_timer_init(qdf_mc_timer_t *timer, QDF_TIMER_TYPE timer_type,
182*5113495bSYour Name 			     qdf_mc_timer_callback_t callback,
183*5113495bSYour Name 			     void *user_data);
184*5113495bSYour Name #endif
185*5113495bSYour Name 
186*5113495bSYour Name /**
187*5113495bSYour Name  * qdf_mc_timer_destroy() - destroy QDF timer
188*5113495bSYour Name  * @timer: Pointer to timer object
189*5113495bSYour Name  *
190*5113495bSYour Name  * qdf_mc_timer_destroy() function shall destroy the timer object.
191*5113495bSYour Name  * After a successful return from \a qdf_mc_timer_destroy() the timer
192*5113495bSYour Name  * object becomes, in effect, uninitialized.
193*5113495bSYour Name  *
194*5113495bSYour Name  * A destroyed timer object can be re-initialized by calling
195*5113495bSYour Name  * qdf_mc_timer_init().  The results of otherwise referencing the object
196*5113495bSYour Name  * after it has been destroyed are undefined.
197*5113495bSYour Name  *
198*5113495bSYour Name  * Calls to QDF timer functions to manipulate the timer, such
199*5113495bSYour Name  * as qdf_mc_timer_set() will fail if the lock is destroyed.  Therefore,
200*5113495bSYour Name  * don't use the timer after it has been destroyed until it has
201*5113495bSYour Name  * been re-initialized.
202*5113495bSYour Name  *
203*5113495bSYour Name  * Return:
204*5113495bSYour Name  * QDF_STATUS_SUCCESS - Timer is initialized successfully
205*5113495bSYour Name  * QDF failure status - Timer initialization failed
206*5113495bSYour Name  */
207*5113495bSYour Name QDF_STATUS qdf_mc_timer_destroy(qdf_mc_timer_t *timer);
208*5113495bSYour Name 
209*5113495bSYour Name /**
210*5113495bSYour Name  * qdf_mc_timer_start() - start a QDF Timer object
211*5113495bSYour Name  * @timer: Pointer to timer object
212*5113495bSYour Name  * @expiration_time: Time to expire
213*5113495bSYour Name  *
214*5113495bSYour Name  * qdf_mc_timer_start() function starts a timer to expire after the
215*5113495bSYour Name  * specified interval, thus running the timer callback function when
216*5113495bSYour Name  * the interval expires.
217*5113495bSYour Name  *
218*5113495bSYour Name  * A timer only runs once (a one-shot timer).  To re-start the
219*5113495bSYour Name  * timer, qdf_mc_timer_start() has to be called after the timer runs
220*5113495bSYour Name  * or has been cancelled.
221*5113495bSYour Name  *
222*5113495bSYour Name  * Return:
223*5113495bSYour Name  * QDF_STATUS_SUCCESS - Timer is initialized successfully
224*5113495bSYour Name  * QDF failure status - Timer initialization failed
225*5113495bSYour Name  */
226*5113495bSYour Name QDF_STATUS qdf_mc_timer_start(qdf_mc_timer_t *timer, uint32_t expiration_time);
227*5113495bSYour Name 
228*5113495bSYour Name /**
229*5113495bSYour Name  * qdf_mc_timer_stop() - stop a QDF Timer
230*5113495bSYour Name  * @timer: Pointer to timer object
231*5113495bSYour Name  * qdf_mc_timer_stop() function stops a timer that has been started but
232*5113495bSYour Name  * has not expired, essentially cancelling the 'start' request.
233*5113495bSYour Name  *
234*5113495bSYour Name  * After a timer is stopped, it goes back to the state it was in after it
235*5113495bSYour Name  * was created and can be started again via a call to qdf_mc_timer_start().
236*5113495bSYour Name  *
237*5113495bSYour Name  * Return:
238*5113495bSYour Name  * QDF_STATUS_SUCCESS - Timer is initialized successfully
239*5113495bSYour Name  * QDF failure status - Timer initialization failed
240*5113495bSYour Name  */
241*5113495bSYour Name QDF_STATUS qdf_mc_timer_stop(qdf_mc_timer_t *timer);
242*5113495bSYour Name 
243*5113495bSYour Name /**
244*5113495bSYour Name  * qdf_mc_timer_stop_sync() - stop a QDF Timer
245*5113495bSYour Name  * @timer: Pointer to timer object
246*5113495bSYour Name  * qdf_mc_timer_stop_sync() function stops a timer synchronously
247*5113495bSYour Name  * that has been started but has not expired, essentially
248*5113495bSYour Name  * cancelling the 'start' request.
249*5113495bSYour Name  *
250*5113495bSYour Name  * After a timer is stopped, it goes back to the state it was in after it
251*5113495bSYour Name  * was created and can be started again via a call to qdf_mc_timer_start().
252*5113495bSYour Name  *
253*5113495bSYour Name  * Return:
254*5113495bSYour Name  * QDF_STATUS_SUCCESS - Timer is initialized successfully
255*5113495bSYour Name  * QDF failure status - Timer initialization failed
256*5113495bSYour Name  */
257*5113495bSYour Name QDF_STATUS qdf_mc_timer_stop_sync(qdf_mc_timer_t *timer);
258*5113495bSYour Name 
259*5113495bSYour Name /**
260*5113495bSYour Name  * qdf_mc_timer_get_system_ticks() - get the system time in 10ms ticks
261*5113495bSYour Name  *
262*5113495bSYour Name  * qdf_mc_timer_get_system_ticks() function returns the current number
263*5113495bSYour Name  * of timer ticks in 10msec intervals.  This function is suitable timestamping
264*5113495bSYour Name  * and calculating time intervals by calculating the difference between two
265*5113495bSYour Name  * timestamps.
266*5113495bSYour Name  *
267*5113495bSYour Name  * Return:
268*5113495bSYour Name  * The current system tick count (in 10msec intervals).  This
269*5113495bSYour Name  * function cannot fail.
270*5113495bSYour Name  */
271*5113495bSYour Name unsigned long qdf_mc_timer_get_system_ticks(void);
272*5113495bSYour Name 
273*5113495bSYour Name /**
274*5113495bSYour Name  * qdf_mc_timer_get_system_time() - Get the system time in milliseconds
275*5113495bSYour Name  *
276*5113495bSYour Name  * qdf_mc_timer_get_system_time() function returns the number of milliseconds
277*5113495bSYour Name  * that have elapsed since the system was started
278*5113495bSYour Name  *
279*5113495bSYour Name  * Return:
280*5113495bSYour Name  * The current system time in milliseconds
281*5113495bSYour Name  */
282*5113495bSYour Name unsigned long qdf_mc_timer_get_system_time(void);
283*5113495bSYour Name 
284*5113495bSYour Name /**
285*5113495bSYour Name  * qdf_get_monotonic_boottime_ns() - Get kernel boottime in ns
286*5113495bSYour Name  *
287*5113495bSYour Name  * Return: kernel boottime in nano sec (includes time spent in suspend)
288*5113495bSYour Name  */
289*5113495bSYour Name s64 qdf_get_monotonic_boottime_ns(void);
290*5113495bSYour Name 
291*5113495bSYour Name /**
292*5113495bSYour Name  * qdf_timer_module_init() - initializes a QDF timer module.
293*5113495bSYour Name  *
294*5113495bSYour Name  * This API initializes the QDF timer module. This needs to be called
295*5113495bSYour Name  * exactly once prior to using any QDF timers.
296*5113495bSYour Name  *
297*5113495bSYour Name  * Return: none
298*5113495bSYour Name  */
299*5113495bSYour Name void qdf_timer_module_init(void);
300*5113495bSYour Name 
301*5113495bSYour Name /**
302*5113495bSYour Name  * qdf_get_time_of_the_day_us() - Get time of the day in microseconds
303*5113495bSYour Name  *
304*5113495bSYour Name  * Return: None
305*5113495bSYour Name  */
306*5113495bSYour Name uint64_t qdf_get_time_of_the_day_us(void);
307*5113495bSYour Name 
308*5113495bSYour Name /**
309*5113495bSYour Name  * qdf_get_time_of_the_day_ms() - get time of the day in millisec
310*5113495bSYour Name  *
311*5113495bSYour Name  * Return: time of the day in ms
312*5113495bSYour Name  */
313*5113495bSYour Name qdf_time_t qdf_get_time_of_the_day_ms(void);
314*5113495bSYour Name 
315*5113495bSYour Name /**
316*5113495bSYour Name  * qdf_timer_module_deinit() - Deinitializes the QDF timer module.
317*5113495bSYour Name  *
318*5113495bSYour Name  * This API deinitializes the QDF timer module.
319*5113495bSYour Name  * Return: none
320*5113495bSYour Name  */
321*5113495bSYour Name void qdf_timer_module_deinit(void);
322*5113495bSYour Name 
323*5113495bSYour Name /**
324*5113495bSYour Name  * qdf_get_time_of_the_day_in_hr_min_sec_usec() - Get system time
325*5113495bSYour Name  * @tbuf: Pointer to time stamp buffer
326*5113495bSYour Name  * @len: Time buffer size
327*5113495bSYour Name  *
328*5113495bSYour Name  * This function updates the 'tbuf' with system time in hr:min:sec:msec format
329*5113495bSYour Name  *
330*5113495bSYour Name  * Return: None
331*5113495bSYour Name  */
332*5113495bSYour Name void qdf_get_time_of_the_day_in_hr_min_sec_usec(char *tbuf, int len);
333*5113495bSYour Name 
334*5113495bSYour Name void qdf_register_mc_timer_callback(void (*callback) (qdf_mc_timer_t *data));
335*5113495bSYour Name 
336*5113495bSYour Name /**
337*5113495bSYour Name  * qdf_timer_set_multiplier() - set the global QDF timer scalar value
338*5113495bSYour Name  * @multiplier: the scalar value to apply
339*5113495bSYour Name  *
340*5113495bSYour Name  * Return: None
341*5113495bSYour Name  */
342*5113495bSYour Name void qdf_timer_set_multiplier(uint32_t multiplier);
343*5113495bSYour Name 
344*5113495bSYour Name /**
345*5113495bSYour Name  * qdf_timer_get_multiplier() - get the global QDF timer scalar value
346*5113495bSYour Name  *
347*5113495bSYour Name  * Return: the global QDF timer scalar value
348*5113495bSYour Name  */
349*5113495bSYour Name uint32_t qdf_timer_get_multiplier(void);
350*5113495bSYour Name 
351*5113495bSYour Name #endif /* __QDF_MC_TIMER_H */
352