qdf_lock.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Copyright (c) 2014-2019 The Linux Foundation. All rights reserved.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for
  5. * any purpose with or without fee is hereby granted, provided that the
  6. * above copyright notice and this permission notice appear in all
  7. * copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  10. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  12. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. * PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /**
  19. * @file qdf_lock.h
  20. * This file abstracts locking operations.
  21. */
  22. #ifndef _QDF_LOCK_H
  23. #define _QDF_LOCK_H
  24. #include <qdf_types.h>
  25. #include <qdf_mem.h>
  26. #include <qdf_time.h>
  27. #include <i_qdf_trace.h>
  28. #ifndef QDF_LOCK_STATS
  29. #define QDF_LOCK_STATS 0
  30. #endif
  31. #ifndef QDF_LOCK_STATS_DESTROY_PRINT
  32. #define QDF_LOCK_STATS_DESTROY_PRINT 0
  33. #endif
  34. #ifndef QDF_LOCK_STATS_BUG_ON
  35. #define QDF_LOCK_STATS_BUG_ON 0
  36. #endif
  37. #ifndef QDF_LOCK_STATS_LIST
  38. #define QDF_LOCK_STATS_LIST 0
  39. #endif
  40. /* Max hold time in micro seconds, 0 to disable detection*/
  41. #define QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_IRQ 10000
  42. #define QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_BH 1000000
  43. #define QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK 0
  44. #if !QDF_LOCK_STATS
  45. struct lock_stats {};
  46. #define BEFORE_LOCK(x...) do {} while (0)
  47. #define AFTER_LOCK(x...) do {} while (0)
  48. #define BEFORE_TRYLOCK(x...) do {} while (0)
  49. #define AFTER_TRYLOCK(x...) do {} while (0)
  50. #define BEFORE_UNLOCK(x...) do {} while (0)
  51. #define qdf_lock_stats_create(x...) do {} while (0)
  52. #define qdf_lock_stats_destroy(x...) do {} while (0)
  53. #define qdf_lock_stats_init(x...) do {} while (0)
  54. #define qdf_lock_stats_deinit(x...) do {} while (0)
  55. #else
  56. void qdf_lock_stats_init(void);
  57. void qdf_lock_stats_deinit(void);
  58. struct qdf_lock_cookie;
  59. struct lock_stats {
  60. const char *initialization_fn;
  61. const char *acquired_by;
  62. int line;
  63. int acquired;
  64. int contended;
  65. uint64_t contention_time;
  66. uint64_t non_contention_time;
  67. uint64_t held_time;
  68. uint64_t last_acquired;
  69. uint64_t max_contention_wait;
  70. uint64_t max_held_time;
  71. int num_large_contentions;
  72. int num_large_holds;
  73. struct qdf_lock_cookie *cookie;
  74. };
  75. #define LARGE_CONTENTION QDF_LOG_TIMESTAMP_CYCLES_PER_10_US
  76. #define BEFORE_LOCK(lock, was_locked) \
  77. do { \
  78. uint64_t BEFORE_LOCK_time; \
  79. uint64_t AFTER_LOCK_time; \
  80. bool BEFORE_LOCK_is_locked = was_locked; \
  81. BEFORE_LOCK_time = qdf_get_log_timestamp(); \
  82. do {} while (0)
  83. #define AFTER_LOCK(lock, func) \
  84. lock->stats.acquired_by = func; \
  85. AFTER_LOCK_time = qdf_get_log_timestamp(); \
  86. lock->stats.acquired++; \
  87. lock->stats.last_acquired = AFTER_LOCK_time; \
  88. if (BEFORE_LOCK_is_locked) { \
  89. lock->stats.contended++; \
  90. lock->stats.contention_time += \
  91. (AFTER_LOCK_time - BEFORE_LOCK_time); \
  92. } else { \
  93. lock->stats.non_contention_time += \
  94. (AFTER_LOCK_time - BEFORE_LOCK_time); \
  95. } \
  96. \
  97. if (AFTER_LOCK_time - BEFORE_LOCK_time > LARGE_CONTENTION) \
  98. lock->stats.num_large_contentions++; \
  99. \
  100. if (AFTER_LOCK_time - BEFORE_LOCK_time > \
  101. lock->stats.max_contention_wait) \
  102. lock->stats.max_contention_wait = \
  103. AFTER_LOCK_time - BEFORE_LOCK_time; \
  104. } while (0)
  105. #define BEFORE_TRYLOCK(lock) \
  106. do { \
  107. uint64_t BEFORE_LOCK_time; \
  108. uint64_t AFTER_LOCK_time; \
  109. BEFORE_LOCK_time = qdf_get_log_timestamp(); \
  110. do {} while (0)
  111. #define AFTER_TRYLOCK(lock, trylock_return, func) \
  112. AFTER_LOCK_time = qdf_get_log_timestamp(); \
  113. if (trylock_return) { \
  114. lock->stats.acquired++; \
  115. lock->stats.last_acquired = AFTER_LOCK_time; \
  116. lock->stats.non_contention_time += \
  117. (AFTER_LOCK_time - BEFORE_LOCK_time); \
  118. lock->stats.acquired_by = func; \
  119. } \
  120. } while (0)
  121. /* max_hold_time in US */
  122. #define BEFORE_UNLOCK(lock, max_hold_time) \
  123. do {\
  124. uint64_t held_time = qdf_get_log_timestamp() - \
  125. lock->stats.last_acquired; \
  126. lock->stats.held_time += held_time; \
  127. \
  128. if (held_time > lock->stats.max_held_time) \
  129. lock->stats.max_held_time = held_time; \
  130. \
  131. if (held_time > LARGE_CONTENTION) \
  132. lock->stats.num_large_holds++; \
  133. if (QDF_LOCK_STATS_BUG_ON && max_hold_time && \
  134. held_time > qdf_usecs_to_log_timestamp(max_hold_time)) { \
  135. QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR, \
  136. "BEFORE_UNLOCK: lock held too long (%lluus)", \
  137. qdf_log_timestamp_to_usecs(held_time)); \
  138. QDF_BUG(0); \
  139. } \
  140. lock->stats.acquired_by = NULL; \
  141. } while (0)
  142. void qdf_lock_stats_cookie_destroy(struct lock_stats *stats);
  143. void qdf_lock_stats_cookie_create(struct lock_stats *stats,
  144. const char *func, int line);
  145. static inline void qdf_lock_stats_destroy(struct lock_stats *stats)
  146. {
  147. if (QDF_LOCK_STATS_DESTROY_PRINT) {
  148. QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_DEBUG,
  149. "%s: lock: %s %d \t"
  150. "acquired:\t%d\tcontended:\t%d\t"
  151. "contention_time\t%llu\tmax_contention_wait:\t%llu\t"
  152. "non_contention_time\t%llu\t"
  153. "held_time\t%llu\tmax_held:\t%llu"
  154. , __func__, stats->initialization_fn, stats->line,
  155. stats->acquired, stats->contended,
  156. qdf_log_timestamp_to_usecs(stats->contention_time),
  157. qdf_log_timestamp_to_usecs(stats->max_contention_wait),
  158. qdf_log_timestamp_to_usecs(stats->non_contention_time),
  159. qdf_log_timestamp_to_usecs(stats->held_time),
  160. qdf_log_timestamp_to_usecs(stats->max_held_time));
  161. }
  162. if (QDF_LOCK_STATS_LIST)
  163. qdf_lock_stats_cookie_destroy(stats);
  164. }
  165. #ifndef MEMORY_DEBUG
  166. #define qdf_mem_malloc_debug(x, y, z) qdf_mem_malloc(x)
  167. #endif
  168. /* qdf_lock_stats_create() - initialize the lock stats structure
  169. *
  170. */
  171. static inline void qdf_lock_stats_create(struct lock_stats *stats,
  172. const char *func, int line)
  173. {
  174. qdf_mem_zero(stats, sizeof(*stats));
  175. stats->initialization_fn = func;
  176. stats->line = line;
  177. if (QDF_LOCK_STATS_LIST)
  178. qdf_lock_stats_cookie_create(stats, func, line);
  179. }
  180. #endif
  181. #include <i_qdf_lock.h>
  182. #define WIFI_POWER_EVENT_DEFAULT_WAKELOCK_TIMEOUT 0
  183. #define WIFI_POWER_EVENT_WAKELOCK_TAKEN 0
  184. #define WIFI_POWER_EVENT_WAKELOCK_RELEASED 1
  185. /**
  186. * qdf_semaphore_acquire_timeout() - Take the semaphore before timeout
  187. * @m: semaphore to take
  188. * @timeout: maximum time to try to take the semaphore
  189. * Return: int
  190. */
  191. static inline int qdf_semaphore_acquire_timeout(struct semaphore *m,
  192. unsigned long timeout)
  193. {
  194. return __qdf_semaphore_acquire_timeout(m, timeout);
  195. }
  196. struct qdf_spinlock {
  197. __qdf_spinlock_t lock;
  198. struct lock_stats stats;
  199. };
  200. /**
  201. * @brief Platform spinlock object
  202. */
  203. typedef struct qdf_spinlock qdf_spinlock_t;
  204. /**
  205. * @brief Platform mutex object
  206. */
  207. typedef __qdf_semaphore_t qdf_semaphore_t;
  208. typedef __qdf_mutex_t qdf_mutex_t;
  209. /* function Declaration */
  210. QDF_STATUS qdf_mutex_create(qdf_mutex_t *m, const char *func, int line);
  211. #define qdf_mutex_create(m) qdf_mutex_create(m, __func__, __LINE__)
  212. QDF_STATUS qdf_mutex_acquire(qdf_mutex_t *m);
  213. QDF_STATUS qdf_mutex_release(qdf_mutex_t *m);
  214. QDF_STATUS qdf_mutex_destroy(qdf_mutex_t *lock);
  215. /**
  216. * qdf_spinlock_create - Initialize a spinlock
  217. * @lock: spinlock object pointer
  218. * Return: none
  219. */
  220. static inline void qdf_spinlock_create(qdf_spinlock_t *lock, const char *func,
  221. int line)
  222. {
  223. __qdf_spinlock_create(&lock->lock);
  224. /* spinlock stats create relies on the spinlock working allread */
  225. qdf_lock_stats_create(&lock->stats, func, line);
  226. }
  227. #define qdf_spinlock_create(x) qdf_spinlock_create(x, __func__, __LINE__)
  228. /**
  229. * qdf_spinlock_destroy - Delete a spinlock
  230. * @lock: spinlock object pointer
  231. * Return: none
  232. */
  233. static inline void qdf_spinlock_destroy(qdf_spinlock_t *lock)
  234. {
  235. qdf_lock_stats_destroy(&lock->stats);
  236. __qdf_spinlock_destroy(&lock->lock);
  237. }
  238. /**
  239. * qdf_spin_is_locked() - check if the spinlock is locked
  240. * @lock: spinlock object
  241. *
  242. * Return: nonzero if lock is held.
  243. */
  244. static inline int qdf_spin_is_locked(qdf_spinlock_t *lock)
  245. {
  246. return __qdf_spin_is_locked(&lock->lock);
  247. }
  248. /**
  249. * qdf_spin_trylock_bh() - spin trylock bottomhalf
  250. * @lock: spinlock object
  251. *
  252. * Return: nonzero if lock is acquired
  253. */
  254. static inline int qdf_spin_trylock_bh(qdf_spinlock_t *lock, const char *func)
  255. {
  256. int trylock_return;
  257. BEFORE_TRYLOCK(lock);
  258. trylock_return = __qdf_spin_trylock_bh(&lock->lock);
  259. AFTER_TRYLOCK(lock, trylock_return, func);
  260. return trylock_return;
  261. }
  262. #define qdf_spin_trylock_bh(lock) qdf_spin_trylock_bh(lock, __func__)
  263. /**
  264. * qdf_spin_trylock() - spin trylock
  265. * @lock: spinlock object
  266. * Return: int
  267. */
  268. static inline int qdf_spin_trylock(qdf_spinlock_t *lock, const char *func)
  269. {
  270. int result = 0;
  271. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  272. result = __qdf_spin_trylock(&lock->lock);
  273. AFTER_LOCK(lock, func);
  274. return result;
  275. }
  276. #define qdf_spin_trylock(lock) qdf_spin_trylock(lock, __func__)
  277. /**
  278. * qdf_spin_lock_bh() - locks the spinlock mutex in soft irq context
  279. * @lock: spinlock object pointer
  280. * Return: none
  281. */
  282. static inline void qdf_spin_lock_bh(qdf_spinlock_t *lock, const char *func)
  283. {
  284. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  285. __qdf_spin_lock_bh(&lock->lock);
  286. AFTER_LOCK(lock, func);
  287. }
  288. #define qdf_spin_lock_bh(lock) qdf_spin_lock_bh(lock, __func__)
  289. /**
  290. * qdf_spin_unlock_bh() - unlocks the spinlock mutex in soft irq context
  291. * @lock: spinlock object pointer
  292. * Return: none
  293. */
  294. static inline void qdf_spin_unlock_bh(qdf_spinlock_t *lock)
  295. {
  296. BEFORE_UNLOCK(lock, QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_BH);
  297. __qdf_spin_unlock_bh(&lock->lock);
  298. }
  299. /**
  300. * qdf_spinlock_irq_exec - Execute the input function with spinlock held
  301. * and interrupt disabled.
  302. * @hdl: OS handle
  303. * @lock: spinlock to be held for the critical region
  304. * @func: critical region function that to be executed
  305. * @context: context of the critical region function
  306. * Return: Boolean status returned by the critical region function
  307. */
  308. static inline bool qdf_spinlock_irq_exec(qdf_handle_t hdl,
  309. qdf_spinlock_t *lock,
  310. qdf_irqlocked_func_t func, void *arg)
  311. {
  312. return __qdf_spinlock_irq_exec(hdl, &lock->lock, func, arg);
  313. }
  314. /**
  315. * qdf_spin_lock() - Acquire a Spinlock(SMP) & disable Preemption (Preemptive)
  316. * @lock: Lock object
  317. *
  318. * Return: none
  319. */
  320. static inline void qdf_spin_lock(qdf_spinlock_t *lock, const char *func)
  321. {
  322. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  323. __qdf_spin_lock(&lock->lock);
  324. AFTER_LOCK(lock, func);
  325. }
  326. #define qdf_spin_lock(lock) qdf_spin_lock(lock, __func__)
  327. /**
  328. * qdf_spin_unlock() - Unlock the spinlock and enables the Preemption
  329. * @lock: Lock object
  330. *
  331. * Return: none
  332. */
  333. static inline void qdf_spin_unlock(qdf_spinlock_t *lock)
  334. {
  335. BEFORE_UNLOCK(lock, QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK);
  336. __qdf_spin_unlock(&lock->lock);
  337. }
  338. /**
  339. * qdf_spin_lock_irq() - Acquire a Spinlock(SMP) & save the irq state
  340. * @lock: Lock object
  341. * @flags: flags
  342. *
  343. * Return: none
  344. */
  345. static inline void qdf_spin_lock_irq(qdf_spinlock_t *lock, unsigned long flags,
  346. const char *func)
  347. {
  348. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  349. __qdf_spin_lock_irq(&lock->lock.spinlock, flags);
  350. AFTER_LOCK(lock, func);
  351. }
  352. #define qdf_spin_lock_irq(lock, flags) qdf_spin_lock_irq(lock, flags, __func__)
  353. /**
  354. * qdf_spin_lock_irqsave() - Acquire a Spinlock (SMP) & disable Preemption
  355. * (Preemptive) and disable IRQs
  356. * @lock: Lock object
  357. *
  358. * Return: none
  359. */
  360. static inline void qdf_spin_lock_irqsave(qdf_spinlock_t *lock, const char *func)
  361. {
  362. BEFORE_LOCK(lock, qdf_spin_is_locked(lock));
  363. __qdf_spin_lock_irqsave(&lock->lock);
  364. AFTER_LOCK(lock, func);
  365. }
  366. #define qdf_spin_lock_irqsave(lock) qdf_spin_lock_irqsave(lock, __func__)
  367. /**
  368. * qdf_spin_unlock_irqrestore() - Unlock the spinlock and enables the
  369. * Preemption and enable IRQ
  370. * @lock: Lock object
  371. *
  372. * Return: none
  373. */
  374. static inline void qdf_spin_unlock_irqrestore(qdf_spinlock_t *lock)
  375. {
  376. BEFORE_UNLOCK(lock, QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_IRQ);
  377. __qdf_spin_unlock_irqrestore(&lock->lock);
  378. }
  379. /**
  380. * qdf_spin_unlock_irq() - Unlock a Spinlock(SMP) & save the restore state
  381. * @lock: Lock object
  382. * @flags: flags
  383. *
  384. * Return: none
  385. */
  386. static inline void qdf_spin_unlock_irq(qdf_spinlock_t *lock,
  387. unsigned long flags)
  388. {
  389. BEFORE_UNLOCK(lock, QDF_MAX_HOLD_TIME_ALOWED_SPINLOCK_IRQ);
  390. __qdf_spin_unlock_irq(&lock->lock.spinlock, flags);
  391. }
  392. /**
  393. * qdf_semaphore_init() - initialize a semaphore
  394. * @m: Semaphore to initialize
  395. * Return: None
  396. */
  397. static inline void qdf_semaphore_init(qdf_semaphore_t *m)
  398. {
  399. __qdf_semaphore_init(m);
  400. }
  401. /**
  402. * qdf_semaphore_acquire() - take the semaphore
  403. * @m: Semaphore to take
  404. * Return: int
  405. */
  406. static inline int qdf_semaphore_acquire(qdf_semaphore_t *m)
  407. {
  408. return __qdf_semaphore_acquire(m);
  409. }
  410. /**
  411. * qdf_semaphore_release() - give the semaphore
  412. * @m: Semaphore to give
  413. * Return: None
  414. */
  415. static inline void qdf_semaphore_release(qdf_semaphore_t *m)
  416. {
  417. __qdf_semaphore_release(m);
  418. }
  419. /**
  420. * qdf_semaphore_acquire_intr - Take the semaphore, interruptible version
  421. * @osdev: OS Device
  422. * @m: mutex to take
  423. * Return: int
  424. */
  425. static inline int qdf_semaphore_acquire_intr(qdf_semaphore_t *m)
  426. {
  427. return __qdf_semaphore_acquire_intr(m);
  428. }
  429. QDF_STATUS qdf_wake_lock_create(qdf_wake_lock_t *lock, const char *name);
  430. QDF_STATUS qdf_wake_lock_acquire(qdf_wake_lock_t *lock, uint32_t reason);
  431. const char *qdf_wake_lock_name(qdf_wake_lock_t *lock);
  432. QDF_STATUS qdf_wake_lock_timeout_acquire(qdf_wake_lock_t *lock,
  433. uint32_t msec);
  434. QDF_STATUS qdf_wake_lock_release(qdf_wake_lock_t *lock, uint32_t reason);
  435. QDF_STATUS qdf_wake_lock_destroy(qdf_wake_lock_t *lock);
  436. void qdf_pm_system_wakeup(void);
  437. QDF_STATUS qdf_runtime_pm_get(void);
  438. QDF_STATUS qdf_runtime_pm_put(void);
  439. QDF_STATUS qdf_runtime_pm_prevent_suspend(qdf_runtime_lock_t *lock);
  440. QDF_STATUS qdf_runtime_pm_allow_suspend(qdf_runtime_lock_t *lock);
  441. QDF_STATUS __qdf_runtime_lock_init(qdf_runtime_lock_t *lock, const char *name);
  442. #define qdf_runtime_lock_init(lock) __qdf_runtime_lock_init(lock, #lock)
  443. void qdf_runtime_lock_deinit(qdf_runtime_lock_t *lock);
  444. QDF_STATUS qdf_spinlock_acquire(qdf_spinlock_t *lock);
  445. QDF_STATUS qdf_spinlock_release(qdf_spinlock_t *lock);
  446. #endif /* _QDF_LOCK_H */