qdf_lock.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * Copyright (c) 2014-2021 The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2022-2023 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. * DOC: 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. /**
  185. * qdf_lock_stats_create() - initialize the lock stats structure
  186. * @stats: stats to initialize
  187. * @func: calling function
  188. * @line: calling line number
  189. */
  190. static inline void qdf_lock_stats_create(struct lock_stats *stats,
  191. const char *func, int line)
  192. {
  193. qdf_mem_zero(stats, sizeof(*stats));
  194. stats->initialization_fn = func;
  195. stats->line = line;
  196. if (QDF_LOCK_STATS_LIST)
  197. qdf_lock_stats_cookie_create(stats, func, line);
  198. }
  199. #endif
  200. #include <i_qdf_lock.h>
  201. #define WIFI_POWER_EVENT_DEFAULT_WAKELOCK_TIMEOUT 0
  202. #define WIFI_POWER_EVENT_WAKELOCK_TAKEN 0
  203. #define WIFI_POWER_EVENT_WAKELOCK_RELEASED 1
  204. /**
  205. * qdf_semaphore_acquire_timeout() - Take the semaphore before timeout
  206. * @m: semaphore to take
  207. * @timeout: maximum time to try to take the semaphore
  208. *
  209. * Return: int
  210. */
  211. static inline int qdf_semaphore_acquire_timeout(struct semaphore *m,
  212. unsigned long timeout)
  213. {
  214. return __qdf_semaphore_acquire_timeout(m, timeout);
  215. }
  216. struct qdf_spinlock {
  217. __qdf_spinlock_t lock;
  218. struct lock_stats stats;
  219. };
  220. /**
  221. * typedef qdf_spinlock_t - Abstracted spinlock object
  222. *
  223. * Abstracted object. Clients must not make any assumptions about the
  224. * composition of this object
  225. */
  226. typedef struct qdf_spinlock qdf_spinlock_t;
  227. /**
  228. * typedef qdf_semaphore_t - Abstracted semaphore object
  229. *
  230. * Abstracted object. Clients must not make any assumptions about the
  231. * composition of this object
  232. */
  233. typedef __qdf_semaphore_t qdf_semaphore_t;
  234. /**
  235. * typedef qdf_mutex_t - Abstracted mutex object
  236. *
  237. * Abstracted object. Clients must not make any assumptions about the
  238. * composition of this object
  239. */
  240. typedef __qdf_mutex_t qdf_mutex_t;
  241. QDF_STATUS qdf_mutex_create(qdf_mutex_t *lock, const char *func, int line);
  242. /**
  243. * qdf_mutex_create() - Initialize a mutex
  244. * @lock: pointer to the qdf_mutex_t mutex to initialize
  245. *
  246. * Return: QDF_STATUS_SUCCESS on success, else QDF_STATUS failure
  247. */
  248. #define qdf_mutex_create(lock) qdf_mutex_create(lock, __func__, __LINE__)
  249. /**
  250. * qdf_mutex_acquire() - acquire a QDF lock
  251. * @lock: Pointer to the opaque lock object to acquire
  252. *
  253. * A lock object is acquired by calling qdf_mutex_acquire(). If the lock
  254. * is already locked, the calling thread shall block until the lock becomes
  255. * available. This operation shall return with the lock object referenced by
  256. * lock in the locked state with the calling thread as its owner.
  257. *
  258. * Return:
  259. * QDF_STATUS_SUCCESS if lock was successfully initialized
  260. * QDF failure reason codes if lock is not initialized and can't be used
  261. */
  262. QDF_STATUS qdf_mutex_acquire(qdf_mutex_t *lock);
  263. /**
  264. * qdf_mutex_release() - release a QDF lock
  265. * @lock: Pointer to the opaque lock object to be released
  266. *
  267. * qdf_mutex_release() function shall release the lock object
  268. * referenced by 'lock'.
  269. *
  270. * If a thread attempts to release a lock that it unlocked or is not
  271. * initialized, an error is returned.
  272. *
  273. * Return:
  274. * QDF_STATUS_SUCCESS if lock was successfully initialized
  275. * QDF failure reason codes if lock is not initialized and can't be used
  276. */
  277. QDF_STATUS qdf_mutex_release(qdf_mutex_t *lock);
  278. /**
  279. * qdf_mutex_destroy() - destroy a QDF lock
  280. * @lock: Pointer to the opaque lock object to be destroyed
  281. *
  282. * function shall destroy the lock object referenced by lock. After a
  283. * successful return from qdf_mutex_destroy()
  284. * the lock object becomes, in effect, uninitialized.
  285. *
  286. * A destroyed lock object can be reinitialized using qdf_mutex_create();
  287. * the results of otherwise referencing the object after it has been destroyed
  288. * are undefined. Calls to QDF lock functions to manipulate the lock such
  289. * as qdf_mutex_acquire() will fail if the lock is destroyed. Therefore,
  290. * don't use the lock after it has been destroyed until it has
  291. * been re-initialized.
  292. *
  293. * Return:
  294. * QDF_STATUS_SUCCESS if lock was successfully initialized
  295. * QDF failure reason codes if lock is not initialized and can't be used
  296. */
  297. QDF_STATUS qdf_mutex_destroy(qdf_mutex_t *lock);
  298. static inline void qdf_spinlock_create(qdf_spinlock_t *lock, const char *func,
  299. int line)
  300. {
  301. __qdf_spinlock_create(&lock->lock);
  302. /* spinlock stats create relies on the spinlock working allread */
  303. qdf_lock_stats_create(&lock->stats, func, line);
  304. }
  305. /**
  306. * qdf_spinlock_create() - Initialize a spinlock
  307. * @lock: spinlock object pointer
  308. *
  309. * Return: none
  310. */
  311. #define qdf_spinlock_create(lock) qdf_spinlock_create(lock, __func__, __LINE__)
  312. /**
  313. * qdf_spinlock_destroy() - Delete a spinlock
  314. * @lock: spinlock object pointer
  315. *
  316. * Return: none
  317. */
  318. static inline void qdf_spinlock_destroy(qdf_spinlock_t *lock)
  319. {
  320. qdf_lock_stats_destroy(&lock->stats);
  321. __qdf_spinlock_destroy(&lock->lock);
  322. }
  323. /**
  324. * qdf_spin_is_locked() - check if the spinlock is locked
  325. * @lock: spinlock object
  326. *
  327. * Return: nonzero if lock is held.
  328. */
  329. static inline int qdf_spin_is_locked(qdf_spinlock_t *lock)
  330. {
  331. return __qdf_spin_is_locked(&lock->lock);
  332. }
  333. static inline int qdf_spin_trylock_bh(qdf_spinlock_t *lock, const char *func)
  334. {
  335. int trylock_return;
  336. BEFORE_TRYLOCK(lock);
  337. trylock_return = __qdf_spin_trylock_bh(&lock->lock);
  338. AFTER_TRYLOCK(lock, trylock_return, func);
  339. return trylock_return;
  340. }
  341. /**
  342. * qdf_spin_trylock_bh() - spin trylock bottomhalf
  343. * @lock: spinlock object
  344. *
  345. * Return: nonzero if lock is acquired
  346. */
  347. #define qdf_spin_trylock_bh(lock) qdf_spin_trylock_bh(lock, __func__)
  348. static inline int qdf_spin_trylock(qdf_spinlock_t *lock, const char *func)
  349. {
  350. int result = 0;
  351. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  352. result = __qdf_spin_trylock(&lock->lock);
  353. AFTER_LOCK(lock, func);
  354. return result;
  355. }
  356. /**
  357. * qdf_spin_trylock() - spin trylock
  358. * @lock: spinlock object
  359. *
  360. * Return: nonzero if lock is acquired
  361. */
  362. #define qdf_spin_trylock(lock) qdf_spin_trylock(lock, __func__)
  363. static inline void qdf_spin_lock_bh(qdf_spinlock_t *lock, const char *func)
  364. {
  365. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  366. __qdf_spin_lock_bh(&lock->lock);
  367. AFTER_LOCK(lock, func);
  368. }
  369. /**
  370. * qdf_spin_lock_bh() - locks the spinlock mutex in soft irq context
  371. * @lock: spinlock object pointer
  372. *
  373. * Return: none
  374. */
  375. #define qdf_spin_lock_bh(lock) qdf_spin_lock_bh(lock, __func__)
  376. /**
  377. * qdf_spin_unlock_bh() - unlocks the spinlock mutex in soft irq context
  378. * @lock: spinlock object pointer
  379. *
  380. * Return: none
  381. */
  382. static inline void qdf_spin_unlock_bh(qdf_spinlock_t *lock)
  383. {
  384. BEFORE_UNLOCK(lock, QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_BH);
  385. __qdf_spin_unlock_bh(&lock->lock);
  386. }
  387. /**
  388. * qdf_spinlock_irq_exec() - Execute the input function with spinlock held
  389. * and interrupt disabled.
  390. * @hdl: OS handle
  391. * @lock: spinlock to be held for the critical region
  392. * @func: critical region function that to be executed
  393. * @arg: argument of the critical region function
  394. *
  395. * Return: Boolean status returned by the critical region function
  396. */
  397. static inline bool qdf_spinlock_irq_exec(qdf_handle_t hdl,
  398. qdf_spinlock_t *lock,
  399. qdf_irqlocked_func_t func, void *arg)
  400. {
  401. return __qdf_spinlock_irq_exec(hdl, &lock->lock, func, arg);
  402. }
  403. static inline void qdf_spin_lock(qdf_spinlock_t *lock, const char *func)
  404. {
  405. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  406. __qdf_spin_lock(&lock->lock);
  407. AFTER_LOCK(lock, func);
  408. }
  409. /**
  410. * qdf_spin_lock() - Acquire a Spinlock(SMP) & disable Preemption (Preemptive)
  411. * @lock: Lock object
  412. *
  413. * Return: none
  414. */
  415. #define qdf_spin_lock(lock) qdf_spin_lock(lock, __func__)
  416. /**
  417. * qdf_spin_unlock() - Unlock the spinlock and enables the Preemption
  418. * @lock: Lock object
  419. *
  420. * Return: none
  421. */
  422. static inline void qdf_spin_unlock(qdf_spinlock_t *lock)
  423. {
  424. BEFORE_UNLOCK(lock, QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK);
  425. __qdf_spin_unlock(&lock->lock);
  426. }
  427. static inline void qdf_spin_lock_irq(qdf_spinlock_t *lock, unsigned long flags,
  428. const char *func)
  429. {
  430. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  431. __qdf_spin_lock_irq(&lock->lock.spinlock, flags);
  432. AFTER_LOCK(lock, func);
  433. }
  434. /**
  435. * qdf_spin_lock_irq() - Acquire a Spinlock(SMP) & save the irq state
  436. * @lock: Lock object
  437. * @flags: flags
  438. *
  439. * Return: none
  440. */
  441. #define qdf_spin_lock_irq(lock, flags) qdf_spin_lock_irq(lock, flags, __func__)
  442. static inline void qdf_spin_lock_irqsave(qdf_spinlock_t *lock, const char *func)
  443. {
  444. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  445. __qdf_spin_lock_irqsave(&lock->lock);
  446. AFTER_LOCK(lock, func);
  447. }
  448. /**
  449. * qdf_spin_lock_irqsave() - Acquire a Spinlock (SMP) & disable Preemption
  450. * (Preemptive) and disable IRQs
  451. * @lock: Lock object
  452. *
  453. * Return: none
  454. */
  455. #define qdf_spin_lock_irqsave(lock) qdf_spin_lock_irqsave(lock, __func__)
  456. /**
  457. * qdf_spin_unlock_irqrestore() - Unlock the spinlock and enables the
  458. * Preemption and enable IRQ
  459. * @lock: Lock object
  460. *
  461. * Return: none
  462. */
  463. static inline void qdf_spin_unlock_irqrestore(qdf_spinlock_t *lock)
  464. {
  465. BEFORE_UNLOCK(lock, QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_IRQ);
  466. __qdf_spin_unlock_irqrestore(&lock->lock);
  467. }
  468. /**
  469. * qdf_spin_unlock_irq() - Unlock a Spinlock(SMP) & save the restore state
  470. * @lock: Lock object
  471. * @flags: flags
  472. *
  473. * Return: none
  474. */
  475. static inline void qdf_spin_unlock_irq(qdf_spinlock_t *lock,
  476. unsigned long flags)
  477. {
  478. BEFORE_UNLOCK(lock, QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_IRQ);
  479. __qdf_spin_unlock_irq(&lock->lock.spinlock, flags);
  480. }
  481. /**
  482. * qdf_semaphore_init() - initialize a semaphore
  483. * @m: Semaphore to initialize
  484. * Return: None
  485. */
  486. static inline void qdf_semaphore_init(qdf_semaphore_t *m)
  487. {
  488. __qdf_semaphore_init(m);
  489. }
  490. /**
  491. * qdf_semaphore_acquire() - take the semaphore
  492. * @m: Semaphore to take
  493. *
  494. * Return: int
  495. */
  496. static inline int qdf_semaphore_acquire(qdf_semaphore_t *m)
  497. {
  498. return __qdf_semaphore_acquire(m);
  499. }
  500. /**
  501. * qdf_semaphore_release() - give the semaphore
  502. * @m: Semaphore to give
  503. *
  504. * Return: None
  505. */
  506. static inline void qdf_semaphore_release(qdf_semaphore_t *m)
  507. {
  508. __qdf_semaphore_release(m);
  509. }
  510. /**
  511. * qdf_semaphore_acquire_intr() - Take the semaphore, interruptible
  512. * @m: mutex to take
  513. *
  514. * This function allows a user-space process that is waiting on a
  515. * semaphore to be interrupted by the user. If the operation is
  516. * interrupted, the function returns a nonzero value, and the caller
  517. * does not hold the semaphore. Always check the return value and
  518. * responding accordingly.
  519. *
  520. * Return: 0 if the semaphore was acquired, non-zero if not acquired
  521. */
  522. static inline int qdf_semaphore_acquire_intr(qdf_semaphore_t *m)
  523. {
  524. return __qdf_semaphore_acquire_intr(m);
  525. }
  526. #ifdef WLAN_WAKE_LOCK_DEBUG
  527. /**
  528. * qdf_wake_lock_check_for_leaks() - assert no wake lock leaks
  529. *
  530. * Return: None
  531. */
  532. void qdf_wake_lock_check_for_leaks(void);
  533. /**
  534. * qdf_wake_lock_feature_init() - global init logic for wake lock
  535. *
  536. * Return: None
  537. */
  538. void qdf_wake_lock_feature_init(void);
  539. /**
  540. * qdf_wake_lock_feature_deinit() - global de-init logic for wake lock
  541. *
  542. * Return: None
  543. */
  544. void qdf_wake_lock_feature_deinit(void);
  545. #else
  546. static inline void qdf_wake_lock_check_for_leaks(void) { }
  547. static inline void qdf_wake_lock_feature_init(void) { }
  548. static inline void qdf_wake_lock_feature_deinit(void) { }
  549. #endif /* WLAN_WAKE_LOCK_DEBUG */
  550. /**
  551. * __qdf_wake_lock_create() - initialize a wake lock
  552. * @lock: The wake lock to initialize
  553. * @name: Name of wake lock
  554. * @func: caller function
  555. * @line: caller line
  556. * Return:
  557. * QDF status success if wake lock is initialized
  558. * QDF status failure if wake lock was not initialized
  559. */
  560. QDF_STATUS __qdf_wake_lock_create(qdf_wake_lock_t *lock, const char *name,
  561. const char *func, uint32_t line);
  562. /**
  563. * qdf_wake_lock_create() - initialized a wakeup source lock
  564. * @lock: the wakeup source lock to initialize
  565. * @name: the name of wakeup source lock
  566. *
  567. * Return: QDF_STATUS
  568. */
  569. #define qdf_wake_lock_create(lock, name) \
  570. __qdf_wake_lock_create(lock, name, __func__, __LINE__)
  571. /**
  572. * qdf_wake_lock_acquire() - acquires a wake lock
  573. * @lock: The wake lock to acquire
  574. * @reason: Reason for wakelock
  575. *
  576. * Return:
  577. * QDF status success if wake lock is acquired
  578. * QDF status failure if wake lock was not acquired
  579. */
  580. QDF_STATUS qdf_wake_lock_acquire(qdf_wake_lock_t *lock, uint32_t reason);
  581. /**
  582. * qdf_wake_lock_name() - This function returns the name of the wakelock
  583. * @lock: Pointer to the wakelock
  584. *
  585. * This function returns the name of the wakelock
  586. *
  587. * Return: Pointer to the name if it is valid or a default string
  588. */
  589. const char *qdf_wake_lock_name(qdf_wake_lock_t *lock);
  590. /**
  591. * qdf_wake_lock_timeout_acquire() - acquires a wake lock with a timeout
  592. * @lock: The wake lock to acquire
  593. * @msec: timeout in ms (0 for no timeout)
  594. *
  595. * Return:
  596. * QDF status success if wake lock is acquired
  597. * QDF status failure if wake lock was not acquired
  598. */
  599. QDF_STATUS qdf_wake_lock_timeout_acquire(qdf_wake_lock_t *lock,
  600. uint32_t msec);
  601. /**
  602. * qdf_wake_lock_release() - releases a wake lock
  603. * @lock: the wake lock to release
  604. * @reason: Reason for wakelock
  605. *
  606. * Return:
  607. * QDF status success if wake lock is acquired
  608. * QDF status failure if wake lock was not acquired
  609. */
  610. QDF_STATUS qdf_wake_lock_release(qdf_wake_lock_t *lock, uint32_t reason);
  611. /**
  612. * __qdf_wake_lock_destroy() - destroy a wake lock
  613. * @lock: The wake lock to destroy
  614. * @func: caller function
  615. * @line: caller line
  616. *
  617. * Return: None
  618. */
  619. void __qdf_wake_lock_destroy(qdf_wake_lock_t *lock,
  620. const char *func, uint32_t line);
  621. /**
  622. * qdf_wake_lock_destroy() - deinitialize a wakeup source lock
  623. * @lock: the wakeup source lock to de-initialize
  624. *
  625. * Return: None
  626. */
  627. #define qdf_wake_lock_destroy(lock) \
  628. __qdf_wake_lock_destroy(lock, __func__, __LINE__)
  629. /**
  630. * qdf_pm_system_wakeup() - wakeup system
  631. *
  632. * Return: None
  633. */
  634. void qdf_pm_system_wakeup(void);
  635. /**
  636. * qdf_spinlock_acquire() - acquires a spin lock
  637. * @lock: Spin lock to acquire
  638. *
  639. * Return: QDF status success if wake lock is acquired
  640. */
  641. QDF_STATUS qdf_spinlock_acquire(qdf_spinlock_t *lock);
  642. /**
  643. * qdf_spinlock_release() - release a spin lock
  644. * @lock: Spin lock to release
  645. *
  646. * Return: QDF status success if wake lock is acquired
  647. */
  648. QDF_STATUS qdf_spinlock_release(qdf_spinlock_t *lock);
  649. /**
  650. * enum qdf_rtpm_call_type - Get and Put calls types
  651. * @QDF_RTPM_GET: Increment usage count and when system is suspended
  652. * schedule resume process, return depends on pm state.
  653. * @QDF_RTPM_GET_FORCE: Increment usage count and when system is suspended
  654. * schedule resume process, returns success irrespective of
  655. * pm_state.
  656. * @QDF_RTPM_GET_SYNC: Increment usage count and when system is suspended,
  657. * wait till process is resumed.
  658. * @QDF_RTPM_GET_NORESUME: Only increments usage count.
  659. * @QDF_RTPM_PUT: Decrements usage count and puts system in idle state.
  660. * @QDF_RTPM_PUT_SYNC_SUSPEND: Decrements usage count and puts system in
  661. * suspended state.
  662. * @QDF_RTPM_PUT_NOIDLE: Decrements usage count.
  663. */
  664. enum qdf_rtpm_call_type {
  665. QDF_RTPM_GET,
  666. QDF_RTPM_GET_FORCE,
  667. QDF_RTPM_GET_SYNC,
  668. QDF_RTPM_GET_NORESUME,
  669. QDF_RTPM_PUT,
  670. QDF_RTPM_PUT_SYNC_SUSPEND,
  671. QDF_RTPM_PUT_NOIDLE,
  672. };
  673. /**
  674. * enum qdf_rtpm_client_id - modules registered with runtime pm module
  675. * @QDF_RTPM_ID_RESERVED: Reserved ID
  676. * @QDF_RTPM_ID_PM_QOS_NOTIFY: PM QOS context
  677. * @QDF_RTPM_ID_WIPHY_SUSPEND: APSS Bus suspend context
  678. * @QDF_RTPM_ID_MAX: Max id
  679. */
  680. enum qdf_rtpm_client_id {
  681. QDF_RTPM_ID_RESERVED,
  682. QDF_RTPM_ID_PM_QOS_NOTIFY,
  683. QDF_RTPM_ID_WIPHY_SUSPEND,
  684. QDF_RTPM_ID_MAX
  685. };
  686. /**
  687. * qdf_runtime_lock_init() - initialize runtime lock
  688. * @lock: the lock to initialize
  689. *
  690. * Initialize a runtime pm lock. This lock can be used
  691. * to prevent the runtime pm system from putting the bus
  692. * to sleep.
  693. *
  694. * Return: Success if lock initialized
  695. */
  696. #define qdf_runtime_lock_init(lock) __qdf_runtime_lock_init(lock, #lock)
  697. #ifdef FEATURE_RUNTIME_PM
  698. /**
  699. * qdf_rtpm_register() - QDF wrapper to register a module with runtime PM.
  700. * @id: ID of the module which needs to be registered
  701. * @hif_rpm_cbk: callback to be called when get was called in suspended state.
  702. *
  703. * Return: success status if registered
  704. */
  705. QDF_STATUS qdf_rtpm_register(uint32_t id, void (*hif_rpm_cbk)(void));
  706. /**
  707. * qdf_rtpm_deregister() - QDF wrapper to deregister the module
  708. * @id: ID of the module which needs to be de-registered
  709. *
  710. * Return: success status if successfully de-registered
  711. */
  712. QDF_STATUS qdf_rtpm_deregister(uint32_t id);
  713. /**
  714. * __qdf_runtime_lock_init() - initialize runtime lock
  715. * @lock: the lock to initialize
  716. * @name: name of the runtime lock
  717. *
  718. * Initialize a runtime pm lock. This lock can be used
  719. * to prevent the runtime pm system from putting the bus
  720. * to sleep.
  721. *
  722. * Return: Success if lock initialized
  723. */
  724. QDF_STATUS __qdf_runtime_lock_init(qdf_runtime_lock_t *lock, const char *name);
  725. /**
  726. * qdf_runtime_lock_deinit() - deinitialize runtime pm lock
  727. * @lock: the lock to deinitialize
  728. *
  729. * Ensures the lock is released. Frees the runtime lock.
  730. *
  731. * Return: void
  732. */
  733. void qdf_runtime_lock_deinit(qdf_runtime_lock_t *lock);
  734. /**
  735. * qdf_rtpm_get() - Increment usage_count on the device to avoid suspend.
  736. * @type: get call types from hif_rpm_type
  737. * @id: ID of the module calling qdf_rtpm_get()
  738. *
  739. * Return: success if a get has been issued, else error code.
  740. */
  741. QDF_STATUS qdf_rtpm_get(uint8_t type, uint32_t id);
  742. /**
  743. * qdf_rtpm_put() - Decrement usage_count on the device to avoid suspend.
  744. * @type: put call types from hif_rpm_type
  745. * @id: ID of the module calling qdf_rtpm_put()
  746. *
  747. * Return: success if a put has been issued, else error code.
  748. */
  749. QDF_STATUS qdf_rtpm_put(uint8_t type, uint32_t id);
  750. /**
  751. * qdf_runtime_pm_prevent_suspend() - Prevent Runtime suspend
  752. * @lock: runtime PM lock
  753. *
  754. * This function will prevent runtime suspend, by incrementing
  755. * device's usage count.
  756. *
  757. * Return: status
  758. */
  759. QDF_STATUS qdf_runtime_pm_prevent_suspend(qdf_runtime_lock_t *lock);
  760. /**
  761. * qdf_runtime_pm_prevent_suspend_sync() - Synchronized Prevent Runtime suspend
  762. * @lock: runtime PM lock
  763. *
  764. * This function will prevent runtime suspend, by incrementing
  765. * device's usage count and waits till system is in resumed state.
  766. *
  767. * Return: status
  768. */
  769. QDF_STATUS qdf_runtime_pm_prevent_suspend_sync(qdf_runtime_lock_t *lock);
  770. /**
  771. * qdf_runtime_pm_allow_suspend() - Allow Runtime suspend
  772. * @lock: runtime PM lock
  773. *
  774. * This function will allow runtime suspend, by decrementing
  775. * device's usage count.
  776. *
  777. * Return: status
  778. */
  779. QDF_STATUS qdf_runtime_pm_allow_suspend(qdf_runtime_lock_t *lock);
  780. /**
  781. * qdf_rtpm_sync_resume() - Invoke synchronous runtime resume.
  782. *
  783. * This function will invoke synchronous runtime resume.
  784. *
  785. * Return: Success if state is ON
  786. */
  787. QDF_STATUS qdf_rtpm_sync_resume(void);
  788. #else
  789. static inline
  790. QDF_STATUS qdf_rtpm_register(uint32_t id, void (*hif_rpm_cbk)(void))
  791. {
  792. return 0;
  793. }
  794. static inline
  795. QDF_STATUS qdf_rtpm_deregister(uint32_t id)
  796. {
  797. return QDF_STATUS_SUCCESS;
  798. }
  799. static inline
  800. QDF_STATUS __qdf_runtime_lock_init(qdf_runtime_lock_t *lock, const char *name)
  801. {
  802. return QDF_STATUS_SUCCESS;
  803. }
  804. static inline
  805. void qdf_runtime_lock_deinit(qdf_runtime_lock_t *lock)
  806. {
  807. }
  808. static inline
  809. QDF_STATUS qdf_rtpm_get(uint8_t type, uint32_t id)
  810. {
  811. return QDF_STATUS_SUCCESS;
  812. }
  813. static inline
  814. QDF_STATUS qdf_rtpm_put(uint8_t type, uint32_t id)
  815. {
  816. return QDF_STATUS_SUCCESS;
  817. }
  818. static inline
  819. QDF_STATUS qdf_runtime_pm_prevent_suspend(qdf_runtime_lock_t *lock)
  820. {
  821. return QDF_STATUS_SUCCESS;
  822. }
  823. static inline
  824. QDF_STATUS qdf_runtime_pm_prevent_suspend_sync(qdf_runtime_lock_t *lock)
  825. {
  826. return QDF_STATUS_SUCCESS;
  827. }
  828. static inline
  829. QDF_STATUS qdf_runtime_pm_allow_suspend(qdf_runtime_lock_t *lock)
  830. {
  831. return QDF_STATUS_SUCCESS;
  832. }
  833. static inline
  834. QDF_STATUS qdf_rtpm_sync_resume(void)
  835. {
  836. return QDF_STATUS_SUCCESS;
  837. }
  838. #endif /* FEATURE_RUNTIME_PM */
  839. #endif /* _QDF_LOCK_H */