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