qdf_lock.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * Copyright (c) 2014-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. * @file qdf_lock.h
  21. * This file abstracts locking operations.
  22. */
  23. #ifndef _QDF_LOCK_H
  24. #define _QDF_LOCK_H
  25. #include <qdf_types.h>
  26. #include <qdf_mem.h>
  27. #include <qdf_time.h>
  28. #include <i_qdf_trace.h>
  29. #ifndef QDF_LOCK_STATS
  30. #define QDF_LOCK_STATS 0
  31. #endif
  32. #ifndef QDF_LOCK_STATS_DESTROY_PRINT
  33. #define QDF_LOCK_STATS_DESTROY_PRINT 0
  34. #endif
  35. #ifndef QDF_LOCK_STATS_BUG_ON
  36. #define QDF_LOCK_STATS_BUG_ON 0
  37. #endif
  38. #ifndef QDF_LOCK_STATS_LIST
  39. #define QDF_LOCK_STATS_LIST 0
  40. #endif
  41. /* Max hold time in micro seconds, 0 to disable detection*/
  42. #ifdef VCPU_TIMESTOLEN
  43. #define QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_IRQ 400000
  44. #else
  45. #define QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_IRQ 10000
  46. #endif
  47. #define QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK 0
  48. #if QDF_LOCK_STATS
  49. #define QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_BH 2000000
  50. #else
  51. #define QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_BH 1000000
  52. #endif
  53. #if !QDF_LOCK_STATS
  54. struct lock_stats {};
  55. #define BEFORE_LOCK(x...) do {} while (0)
  56. #define AFTER_LOCK(x...) do {} while (0)
  57. #define BEFORE_TRYLOCK(x...) do {} while (0)
  58. #define AFTER_TRYLOCK(x...) do {} while (0)
  59. #define BEFORE_UNLOCK(x...) do {} while (0)
  60. #define qdf_lock_stats_create(x...) do {} while (0)
  61. #define qdf_lock_stats_destroy(x...) do {} while (0)
  62. #define qdf_lock_stats_init(x...) do {} while (0)
  63. #define qdf_lock_stats_deinit(x...) do {} while (0)
  64. #else
  65. void qdf_lock_stats_init(void);
  66. void qdf_lock_stats_deinit(void);
  67. struct qdf_lock_cookie;
  68. struct lock_stats {
  69. const char *initialization_fn;
  70. const char *acquired_by;
  71. int line;
  72. int acquired;
  73. int contended;
  74. uint64_t contention_time;
  75. uint64_t non_contention_time;
  76. uint64_t held_time;
  77. uint64_t last_acquired;
  78. uint64_t max_contention_wait;
  79. uint64_t max_held_time;
  80. int num_large_contentions;
  81. int num_large_holds;
  82. struct qdf_lock_cookie *cookie;
  83. };
  84. #define LARGE_CONTENTION QDF_LOG_TIMESTAMP_CYCLES_PER_10_US
  85. #define BEFORE_LOCK(lock, was_locked) \
  86. do { \
  87. uint64_t BEFORE_LOCK_time; \
  88. uint64_t AFTER_LOCK_time; \
  89. bool BEFORE_LOCK_is_locked = was_locked; \
  90. BEFORE_LOCK_time = qdf_get_log_timestamp_lightweight(); \
  91. do {} while (0)
  92. #define AFTER_LOCK(lock, func) \
  93. lock->stats.acquired_by = func; \
  94. AFTER_LOCK_time = qdf_get_log_timestamp_lightweight(); \
  95. lock->stats.acquired++; \
  96. lock->stats.last_acquired = AFTER_LOCK_time; \
  97. if (BEFORE_LOCK_is_locked) { \
  98. lock->stats.contended++; \
  99. lock->stats.contention_time += \
  100. (AFTER_LOCK_time - BEFORE_LOCK_time); \
  101. } else { \
  102. lock->stats.non_contention_time += \
  103. (AFTER_LOCK_time - BEFORE_LOCK_time); \
  104. } \
  105. \
  106. if (AFTER_LOCK_time - BEFORE_LOCK_time > LARGE_CONTENTION) \
  107. lock->stats.num_large_contentions++; \
  108. \
  109. if (AFTER_LOCK_time - BEFORE_LOCK_time > \
  110. lock->stats.max_contention_wait) \
  111. lock->stats.max_contention_wait = \
  112. AFTER_LOCK_time - BEFORE_LOCK_time; \
  113. } while (0)
  114. #define BEFORE_TRYLOCK(lock) \
  115. do { \
  116. uint64_t BEFORE_LOCK_time; \
  117. uint64_t AFTER_LOCK_time; \
  118. BEFORE_LOCK_time = qdf_get_log_timestamp_lightweight(); \
  119. do {} while (0)
  120. #define AFTER_TRYLOCK(lock, trylock_return, func) \
  121. AFTER_LOCK_time = qdf_get_log_timestamp_lightweight(); \
  122. if (trylock_return) { \
  123. lock->stats.acquired++; \
  124. lock->stats.last_acquired = AFTER_LOCK_time; \
  125. lock->stats.non_contention_time += \
  126. (AFTER_LOCK_time - BEFORE_LOCK_time); \
  127. lock->stats.acquired_by = func; \
  128. } \
  129. } while (0)
  130. /* max_hold_time in US */
  131. #define BEFORE_UNLOCK(lock, max_hold_time) \
  132. do {\
  133. uint64_t BEFORE_UNLOCK_time; \
  134. uint64_t held_time; \
  135. BEFORE_UNLOCK_time = qdf_get_log_timestamp_lightweight(); \
  136. \
  137. if (unlikely(BEFORE_UNLOCK_time < lock->stats.last_acquired)) \
  138. held_time = 0; \
  139. else \
  140. held_time = BEFORE_UNLOCK_time - lock->stats.last_acquired; \
  141. \
  142. lock->stats.held_time += held_time; \
  143. \
  144. if (held_time > lock->stats.max_held_time) \
  145. lock->stats.max_held_time = held_time; \
  146. \
  147. if (held_time > LARGE_CONTENTION) \
  148. lock->stats.num_large_holds++; \
  149. if (QDF_LOCK_STATS_BUG_ON && max_hold_time && \
  150. held_time > qdf_usecs_to_log_timestamp(max_hold_time)) { \
  151. QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR, \
  152. "BEFORE_UNLOCK: lock held too long (%lluus)", \
  153. qdf_log_timestamp_to_usecs(held_time)); \
  154. QDF_BUG(0); \
  155. } \
  156. lock->stats.acquired_by = NULL; \
  157. } while (0)
  158. void qdf_lock_stats_cookie_destroy(struct lock_stats *stats);
  159. void qdf_lock_stats_cookie_create(struct lock_stats *stats,
  160. const char *func, int line);
  161. static inline void qdf_lock_stats_destroy(struct lock_stats *stats)
  162. {
  163. if (QDF_LOCK_STATS_DESTROY_PRINT) {
  164. QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_DEBUG,
  165. "%s: lock: %s %d \t"
  166. "acquired:\t%d\tcontended:\t%d\t"
  167. "contention_time\t%llu\tmax_contention_wait:\t%llu\t"
  168. "non_contention_time\t%llu\t"
  169. "held_time\t%llu\tmax_held:\t%llu"
  170. , __func__, stats->initialization_fn, stats->line,
  171. stats->acquired, stats->contended,
  172. qdf_log_timestamp_to_usecs(stats->contention_time),
  173. qdf_log_timestamp_to_usecs(stats->max_contention_wait),
  174. qdf_log_timestamp_to_usecs(stats->non_contention_time),
  175. qdf_log_timestamp_to_usecs(stats->held_time),
  176. qdf_log_timestamp_to_usecs(stats->max_held_time));
  177. }
  178. if (QDF_LOCK_STATS_LIST)
  179. qdf_lock_stats_cookie_destroy(stats);
  180. }
  181. #ifndef MEMORY_DEBUG
  182. #define qdf_mem_malloc_debug(x, y, z) qdf_mem_malloc(x)
  183. #endif
  184. /* qdf_lock_stats_create() - initialize the lock stats structure
  185. *
  186. */
  187. static inline void qdf_lock_stats_create(struct lock_stats *stats,
  188. const char *func, int line)
  189. {
  190. qdf_mem_zero(stats, sizeof(*stats));
  191. stats->initialization_fn = func;
  192. stats->line = line;
  193. if (QDF_LOCK_STATS_LIST)
  194. qdf_lock_stats_cookie_create(stats, func, line);
  195. }
  196. #endif
  197. #include <i_qdf_lock.h>
  198. #define WIFI_POWER_EVENT_DEFAULT_WAKELOCK_TIMEOUT 0
  199. #define WIFI_POWER_EVENT_WAKELOCK_TAKEN 0
  200. #define WIFI_POWER_EVENT_WAKELOCK_RELEASED 1
  201. /**
  202. * qdf_semaphore_acquire_timeout() - Take the semaphore before timeout
  203. * @m: semaphore to take
  204. * @timeout: maximum time to try to take the semaphore
  205. * Return: int
  206. */
  207. static inline int qdf_semaphore_acquire_timeout(struct semaphore *m,
  208. unsigned long timeout)
  209. {
  210. return __qdf_semaphore_acquire_timeout(m, timeout);
  211. }
  212. struct qdf_spinlock {
  213. __qdf_spinlock_t lock;
  214. struct lock_stats stats;
  215. };
  216. /**
  217. * @brief Platform spinlock object
  218. */
  219. typedef struct qdf_spinlock qdf_spinlock_t;
  220. /**
  221. * @brief Platform mutex object
  222. */
  223. typedef __qdf_semaphore_t qdf_semaphore_t;
  224. typedef __qdf_mutex_t qdf_mutex_t;
  225. /* function Declaration */
  226. QDF_STATUS qdf_mutex_create(qdf_mutex_t *m, const char *func, int line);
  227. #define qdf_mutex_create(m) qdf_mutex_create(m, __func__, __LINE__)
  228. QDF_STATUS qdf_mutex_acquire(qdf_mutex_t *m);
  229. QDF_STATUS qdf_mutex_release(qdf_mutex_t *m);
  230. QDF_STATUS qdf_mutex_destroy(qdf_mutex_t *lock);
  231. /**
  232. * qdf_spinlock_create - Initialize a spinlock
  233. * @lock: spinlock object pointer
  234. * Return: none
  235. */
  236. static inline void qdf_spinlock_create(qdf_spinlock_t *lock, const char *func,
  237. int line)
  238. {
  239. __qdf_spinlock_create(&lock->lock);
  240. /* spinlock stats create relies on the spinlock working allread */
  241. qdf_lock_stats_create(&lock->stats, func, line);
  242. }
  243. #define qdf_spinlock_create(x) qdf_spinlock_create(x, __func__, __LINE__)
  244. /**
  245. * qdf_spinlock_destroy - Delete a spinlock
  246. * @lock: spinlock object pointer
  247. * Return: none
  248. */
  249. static inline void qdf_spinlock_destroy(qdf_spinlock_t *lock)
  250. {
  251. qdf_lock_stats_destroy(&lock->stats);
  252. __qdf_spinlock_destroy(&lock->lock);
  253. }
  254. /**
  255. * qdf_spin_is_locked() - check if the spinlock is locked
  256. * @lock: spinlock object
  257. *
  258. * Return: nonzero if lock is held.
  259. */
  260. static inline int qdf_spin_is_locked(qdf_spinlock_t *lock)
  261. {
  262. return __qdf_spin_is_locked(&lock->lock);
  263. }
  264. /**
  265. * qdf_spin_trylock_bh() - spin trylock bottomhalf
  266. * @lock: spinlock object
  267. *
  268. * Return: nonzero if lock is acquired
  269. */
  270. static inline int qdf_spin_trylock_bh(qdf_spinlock_t *lock, const char *func)
  271. {
  272. int trylock_return;
  273. BEFORE_TRYLOCK(lock);
  274. trylock_return = __qdf_spin_trylock_bh(&lock->lock);
  275. AFTER_TRYLOCK(lock, trylock_return, func);
  276. return trylock_return;
  277. }
  278. #define qdf_spin_trylock_bh(lock) qdf_spin_trylock_bh(lock, __func__)
  279. /**
  280. * qdf_spin_trylock() - spin trylock
  281. * @lock: spinlock object
  282. * Return: int
  283. */
  284. static inline int qdf_spin_trylock(qdf_spinlock_t *lock, const char *func)
  285. {
  286. int result = 0;
  287. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  288. result = __qdf_spin_trylock(&lock->lock);
  289. AFTER_LOCK(lock, func);
  290. return result;
  291. }
  292. #define qdf_spin_trylock(lock) qdf_spin_trylock(lock, __func__)
  293. /**
  294. * qdf_spin_lock_bh() - locks the spinlock mutex in soft irq context
  295. * @lock: spinlock object pointer
  296. * Return: none
  297. */
  298. static inline void qdf_spin_lock_bh(qdf_spinlock_t *lock, const char *func)
  299. {
  300. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  301. __qdf_spin_lock_bh(&lock->lock);
  302. AFTER_LOCK(lock, func);
  303. }
  304. #define qdf_spin_lock_bh(lock) qdf_spin_lock_bh(lock, __func__)
  305. /**
  306. * qdf_spin_unlock_bh() - unlocks the spinlock mutex in soft irq context
  307. * @lock: spinlock object pointer
  308. * Return: none
  309. */
  310. static inline void qdf_spin_unlock_bh(qdf_spinlock_t *lock)
  311. {
  312. BEFORE_UNLOCK(lock, QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_BH);
  313. __qdf_spin_unlock_bh(&lock->lock);
  314. }
  315. /**
  316. * qdf_spinlock_irq_exec - Execute the input function with spinlock held
  317. * and interrupt disabled.
  318. * @hdl: OS handle
  319. * @lock: spinlock to be held for the critical region
  320. * @func: critical region function that to be executed
  321. * @context: context of the critical region function
  322. * Return: Boolean status returned by the critical region function
  323. */
  324. static inline bool qdf_spinlock_irq_exec(qdf_handle_t hdl,
  325. qdf_spinlock_t *lock,
  326. qdf_irqlocked_func_t func, void *arg)
  327. {
  328. return __qdf_spinlock_irq_exec(hdl, &lock->lock, func, arg);
  329. }
  330. /**
  331. * qdf_spin_lock() - Acquire a Spinlock(SMP) & disable Preemption (Preemptive)
  332. * @lock: Lock object
  333. *
  334. * Return: none
  335. */
  336. static inline void qdf_spin_lock(qdf_spinlock_t *lock, const char *func)
  337. {
  338. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  339. __qdf_spin_lock(&lock->lock);
  340. AFTER_LOCK(lock, func);
  341. }
  342. #define qdf_spin_lock(lock) qdf_spin_lock(lock, __func__)
  343. /**
  344. * qdf_spin_unlock() - Unlock the spinlock and enables the Preemption
  345. * @lock: Lock object
  346. *
  347. * Return: none
  348. */
  349. static inline void qdf_spin_unlock(qdf_spinlock_t *lock)
  350. {
  351. BEFORE_UNLOCK(lock, QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK);
  352. __qdf_spin_unlock(&lock->lock);
  353. }
  354. /**
  355. * qdf_spin_lock_irq() - Acquire a Spinlock(SMP) & save the irq state
  356. * @lock: Lock object
  357. * @flags: flags
  358. *
  359. * Return: none
  360. */
  361. static inline void qdf_spin_lock_irq(qdf_spinlock_t *lock, unsigned long flags,
  362. const char *func)
  363. {
  364. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  365. __qdf_spin_lock_irq(&lock->lock.spinlock, flags);
  366. AFTER_LOCK(lock, func);
  367. }
  368. #define qdf_spin_lock_irq(lock, flags) qdf_spin_lock_irq(lock, flags, __func__)
  369. /**
  370. * qdf_spin_lock_irqsave() - Acquire a Spinlock (SMP) & disable Preemption
  371. * (Preemptive) and disable IRQs
  372. * @lock: Lock object
  373. *
  374. * Return: none
  375. */
  376. static inline void qdf_spin_lock_irqsave(qdf_spinlock_t *lock, const char *func)
  377. {
  378. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  379. __qdf_spin_lock_irqsave(&lock->lock);
  380. AFTER_LOCK(lock, func);
  381. }
  382. #define qdf_spin_lock_irqsave(lock) qdf_spin_lock_irqsave(lock, __func__)
  383. /**
  384. * qdf_spin_unlock_irqrestore() - Unlock the spinlock and enables the
  385. * Preemption and enable IRQ
  386. * @lock: Lock object
  387. *
  388. * Return: none
  389. */
  390. static inline void qdf_spin_unlock_irqrestore(qdf_spinlock_t *lock)
  391. {
  392. BEFORE_UNLOCK(lock, QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_IRQ);
  393. __qdf_spin_unlock_irqrestore(&lock->lock);
  394. }
  395. /**
  396. * qdf_spin_unlock_irq() - Unlock a Spinlock(SMP) & save the restore state
  397. * @lock: Lock object
  398. * @flags: flags
  399. *
  400. * Return: none
  401. */
  402. static inline void qdf_spin_unlock_irq(qdf_spinlock_t *lock,
  403. unsigned long flags)
  404. {
  405. BEFORE_UNLOCK(lock, QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_IRQ);
  406. __qdf_spin_unlock_irq(&lock->lock.spinlock, flags);
  407. }
  408. /**
  409. * qdf_semaphore_init() - initialize a semaphore
  410. * @m: Semaphore to initialize
  411. * Return: None
  412. */
  413. static inline void qdf_semaphore_init(qdf_semaphore_t *m)
  414. {
  415. __qdf_semaphore_init(m);
  416. }
  417. /**
  418. * qdf_semaphore_acquire() - take the semaphore
  419. * @m: Semaphore to take
  420. * Return: int
  421. */
  422. static inline int qdf_semaphore_acquire(qdf_semaphore_t *m)
  423. {
  424. return __qdf_semaphore_acquire(m);
  425. }
  426. /**
  427. * qdf_semaphore_release() - give the semaphore
  428. * @m: Semaphore to give
  429. * Return: None
  430. */
  431. static inline void qdf_semaphore_release(qdf_semaphore_t *m)
  432. {
  433. __qdf_semaphore_release(m);
  434. }
  435. /**
  436. * qdf_semaphore_acquire_intr - Take the semaphore, interruptible version
  437. * @osdev: OS Device
  438. * @m: mutex to take
  439. * Return: int
  440. */
  441. static inline int qdf_semaphore_acquire_intr(qdf_semaphore_t *m)
  442. {
  443. return __qdf_semaphore_acquire_intr(m);
  444. }
  445. #ifdef WLAN_WAKE_LOCK_DEBUG
  446. /**
  447. * qdf_wake_lock_check_for_leaks() - assert no wake lock leaks
  448. *
  449. * Return: None
  450. */
  451. void qdf_wake_lock_check_for_leaks(void);
  452. /**
  453. * qdf_wake_lock_feature_init() - global init logic for wake lock
  454. *
  455. * Return: None
  456. */
  457. void qdf_wake_lock_feature_init(void);
  458. /**
  459. * qdf_wake_lock_feature_deinit() - global de-init logic for wake lock
  460. *
  461. * Return: None
  462. */
  463. void qdf_wake_lock_feature_deinit(void);
  464. #else
  465. static inline void qdf_wake_lock_check_for_leaks(void) { }
  466. static inline void qdf_wake_lock_feature_init(void) { }
  467. static inline void qdf_wake_lock_feature_deinit(void) { }
  468. #endif /* WLAN_WAKE_LOCK_DEBUG */
  469. /**
  470. * __qdf_wake_lock_create() - initialize a wake lock
  471. * @lock: The wake lock to initialize
  472. * @name: Name of wake lock
  473. * @func: caller function
  474. * @line: caller line
  475. * Return:
  476. * QDF status success: if wake lock is initialized
  477. * QDF status failure: if wake lock was not initialized
  478. */
  479. QDF_STATUS __qdf_wake_lock_create(qdf_wake_lock_t *lock, const char *name,
  480. const char *func, uint32_t line);
  481. /**
  482. * qdf_wake_lock_create() - initialized a wakeup source lock
  483. * @lock: the wakeup source lock to initialize
  484. * @name: the name of wakeup source lock
  485. *
  486. * Return: QDF_STATUS
  487. */
  488. #define qdf_wake_lock_create(lock, name) \
  489. __qdf_wake_lock_create(lock, name, __func__, __LINE__)
  490. QDF_STATUS qdf_wake_lock_acquire(qdf_wake_lock_t *lock, uint32_t reason);
  491. const char *qdf_wake_lock_name(qdf_wake_lock_t *lock);
  492. QDF_STATUS qdf_wake_lock_timeout_acquire(qdf_wake_lock_t *lock,
  493. uint32_t msec);
  494. QDF_STATUS qdf_wake_lock_release(qdf_wake_lock_t *lock, uint32_t reason);
  495. /**
  496. * __qdf_wake_lock_destroy() - destroy a wake lock
  497. * @lock: The wake lock to destroy
  498. * @func: caller function
  499. * @line: caller line
  500. *
  501. * Return: None
  502. */
  503. void __qdf_wake_lock_destroy(qdf_wake_lock_t *lock,
  504. const char *func, uint32_t line);
  505. /**
  506. * qdf_wake_lock_destroy() - deinitialize a wakeup source lock
  507. * @lock: the wakeup source lock to de-initialize
  508. *
  509. * Return: None
  510. */
  511. #define qdf_wake_lock_destroy(lock) \
  512. __qdf_wake_lock_destroy(lock, __func__, __LINE__)
  513. void qdf_pm_system_wakeup(void);
  514. QDF_STATUS qdf_spinlock_acquire(qdf_spinlock_t *lock);
  515. QDF_STATUS qdf_spinlock_release(qdf_spinlock_t *lock);
  516. /**
  517. * enum qdf_rtpm_type - Get and Put calls types
  518. * @QDF_RTPM_GET: Increment usage count and when system is suspended
  519. * schedule resume process, return depends on pm state.
  520. * @QDF_RTPM_GET_FORCE: Increment usage count and when system is suspended
  521. * shedule resume process, returns success irrespective of
  522. * pm_state.
  523. * @QDF_RTPM_GET_SYNC: Increment usage count and when system is suspended,
  524. * wait till process is resumed.
  525. * @QDF_RTPM_GET_NORESUME: Only increments usage count.
  526. * @QDF_RTPM_PUT: Decrements usage count and puts system in idle state.
  527. * @QDF_RTPM_PUT_SYNC_SUSPEND: Decrements usage count and puts system in
  528. * suspended state.
  529. * @QDF_RTPM_PUT_NOIDLE: Decrements usage count.
  530. */
  531. enum qdf_rtpm_call_type {
  532. QDF_RTPM_GET,
  533. QDF_RTPM_GET_FORCE,
  534. QDF_RTPM_GET_SYNC,
  535. QDF_RTPM_GET_NORESUME,
  536. QDF_RTPM_PUT,
  537. QDF_RTPM_PUT_SYNC_SUSPEND,
  538. QDF_RTPM_PUT_NOIDLE,
  539. };
  540. /**
  541. * enum qdf_rtpm_client_id - modules registered with runtime pm module
  542. * @QDF_RTPM_ID_RESERVED: Reserved ID
  543. * @QDF_RTPM_ID_PM_QOS_NOTIFY: PM QOS context
  544. * @QDF_RTPM_ID_BUS_SUSPEND: APSS Bus suspend context
  545. * @QDF_RTPM_ID_MAX: Max id
  546. */
  547. enum qdf_rtpm_client_id {
  548. QDF_RTPM_ID_RESERVED,
  549. QDF_RTPM_ID_PM_QOS_NOTIFY,
  550. QDF_RTPM_ID_WIPHY_SUSPEND,
  551. QDF_RTPM_ID_MAX
  552. };
  553. #define qdf_runtime_lock_init(lock) __qdf_runtime_lock_init(lock, #lock)
  554. #ifdef FEATURE_RUNTIME_PM
  555. /**
  556. * qdf_rtpm_register() - QDF wrapper to register a module with runtime PM.
  557. * @id: ID of the module which needs to be registered
  558. * @hif_rpm_cbk: callback to be called when get was called in suspended state.
  559. * @prevent_multiple_get: not allow simultaneous get calls or put calls
  560. *
  561. * Return: success status if registered
  562. */
  563. QDF_STATUS qdf_rtpm_register(uint32_t id, void (*hif_rpm_cbk)(void));
  564. /**
  565. * qdf_rtpm_deregister() - QDF wrapper to deregister the module
  566. * @id: ID of the module which needs to be de-registered
  567. *
  568. * Return: success status if successfully de-registered
  569. */
  570. QDF_STATUS qdf_rtpm_deregister(uint32_t id);
  571. /**
  572. * qdf_runtime_lock_init() - initialize runtime lock
  573. * @name: name of the runtime lock
  574. *
  575. * Initialize a runtime pm lock. This lock can be used
  576. * to prevent the runtime pm system from putting the bus
  577. * to sleep.
  578. *
  579. * Return: Success if lock initialized
  580. */
  581. QDF_STATUS __qdf_runtime_lock_init(qdf_runtime_lock_t *lock, const char *name);
  582. /**
  583. * qdf_runtime_lock_deinit() - deinitialize runtime pm lock
  584. * @lock: the lock to deinitialize
  585. *
  586. * Ensures the lock is released. Frees the runtime lock.
  587. *
  588. * Return: void
  589. */
  590. void qdf_runtime_lock_deinit(qdf_runtime_lock_t *lock);
  591. /**
  592. * qdf_rtpm_get() - Incremeant usage_count on the device to avoid suspend.
  593. * @type: get call types from hif_rpm_type
  594. * @id: ID of the module calling get()
  595. *
  596. * Return: success if a get has been issued, else error code.
  597. */
  598. QDF_STATUS qdf_rtpm_get(uint8_t type, uint32_t id);
  599. /**
  600. * qdf_rtpm_put() - Decremeant usage_count on the device to avoid suspend.
  601. * @type: put call types from hif_rpm_type
  602. * @id: ID of the module calling put()
  603. *
  604. * Return: success if a put has been issued, else error code.
  605. */
  606. QDF_STATUS qdf_rtpm_put(uint8_t type, uint32_t id);
  607. /**
  608. * qdf_runtime_pm_allow_suspend() - Prevent Runtime suspend
  609. * @data: runtime PM lock
  610. *
  611. * This function will prevent runtime suspend, by incrementing
  612. * device's usage count.
  613. *
  614. * Return: status
  615. */
  616. QDF_STATUS qdf_runtime_pm_prevent_suspend(qdf_runtime_lock_t *lock);
  617. /**
  618. * qdf_runtime_pm_allow_suspend() - Allow Runtime suspend
  619. * @data: runtime PM lock
  620. *
  621. * This function will allow runtime suspend, by decrementing
  622. * device's usage count.
  623. *
  624. * Return: status
  625. */
  626. QDF_STATUS qdf_runtime_pm_allow_suspend(qdf_runtime_lock_t *lock);
  627. /**
  628. * qdf_pm_runtime_sync_resume() - Invoke synchronous runtime resume.
  629. *
  630. * This function will invoke synchronous runtime resume.
  631. *
  632. * Return: Success if state is ON
  633. */
  634. QDF_STATUS qdf_rtpm_sync_resume(void);
  635. #else
  636. static inline
  637. QDF_STATUS qdf_rtpm_register(uint32_t id, void (*hif_rpm_cbk)(void))
  638. {
  639. return 0;
  640. }
  641. static inline
  642. QDF_STATUS qdf_rtpm_deregister(uint32_t id)
  643. {
  644. return QDF_STATUS_SUCCESS;
  645. }
  646. static inline
  647. QDF_STATUS __qdf_runtime_lock_init(qdf_runtime_lock_t *lock, const char *name)
  648. {
  649. return QDF_STATUS_SUCCESS;
  650. }
  651. static inline
  652. void qdf_runtime_lock_deinit(qdf_runtime_lock_t *lock)
  653. {
  654. }
  655. static inline
  656. QDF_STATUS qdf_rtpm_get(uint8_t type, uint32_t id)
  657. {
  658. return QDF_STATUS_SUCCESS;
  659. }
  660. static inline
  661. QDF_STATUS qdf_rtpm_put(uint8_t type, uint32_t id)
  662. {
  663. return QDF_STATUS_SUCCESS;
  664. }
  665. static inline
  666. QDF_STATUS qdf_runtime_pm_prevent_suspend(qdf_runtime_lock_t *lock)
  667. {
  668. return QDF_STATUS_SUCCESS;
  669. }
  670. static inline
  671. QDF_STATUS qdf_runtime_pm_allow_suspend(qdf_runtime_lock_t *lock)
  672. {
  673. return QDF_STATUS_SUCCESS;
  674. }
  675. static inline
  676. QDF_STATUS qdf_rtpm_sync_resume(void)
  677. {
  678. return QDF_STATUS_SUCCESS;
  679. }
  680. #endif /* FEATURE_RUNTIME_PM */
  681. #endif /* _QDF_LOCK_H */