qdf_mem.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /*
  2. * Copyright (c) 2014-2021 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. * DOC: qdf_mem
  20. * QCA driver framework (QDF) memory management APIs
  21. */
  22. #if !defined(__QDF_MEMORY_H)
  23. #define __QDF_MEMORY_H
  24. /* Include Files */
  25. #include <qdf_types.h>
  26. #include <i_qdf_mem.h>
  27. #include <i_qdf_trace.h>
  28. #include <qdf_atomic.h>
  29. #define QDF_CACHE_LINE_SZ __qdf_cache_line_sz
  30. /**
  31. * qdf_align() - align to the given size.
  32. * @a: input that needs to be aligned.
  33. * @align_size: boundary on which 'a' has to be alinged.
  34. *
  35. * Return: aligned value.
  36. */
  37. #define qdf_align(a, align_size) __qdf_align(a, align_size)
  38. #define qdf_page_size __page_size
  39. /**
  40. * struct qdf_mem_dma_page_t - Allocated dmaable page
  41. * @page_v_addr_start: Page start virtual address
  42. * @page_v_addr_end: Page end virtual address
  43. * @page_p_addr: Page start physical address
  44. */
  45. struct qdf_mem_dma_page_t {
  46. char *page_v_addr_start;
  47. char *page_v_addr_end;
  48. qdf_dma_addr_t page_p_addr;
  49. };
  50. /**
  51. * struct qdf_mem_multi_page_t - multiple page allocation information storage
  52. * @num_element_per_page: Number of element in single page
  53. * @num_pages: Number of allocation needed pages
  54. * @dma_pages: page information storage in case of coherent memory
  55. * @cacheable_pages: page information storage in case of cacheable memory
  56. * @is_mem_prealloc: flag for multiple pages pre-alloc or not
  57. */
  58. struct qdf_mem_multi_page_t {
  59. uint16_t num_element_per_page;
  60. uint16_t num_pages;
  61. struct qdf_mem_dma_page_t *dma_pages;
  62. void **cacheable_pages;
  63. qdf_size_t page_size;
  64. #ifdef DP_MEM_PRE_ALLOC
  65. uint8_t is_mem_prealloc;
  66. #endif
  67. };
  68. /* Preprocessor definitions and constants */
  69. typedef __qdf_mempool_t qdf_mempool_t;
  70. /**
  71. * qdf_mem_init() - Initialize QDF memory module
  72. *
  73. * Return: None
  74. *
  75. */
  76. void qdf_mem_init(void);
  77. /**
  78. * qdf_mem_exit() - Exit QDF memory module
  79. *
  80. * Return: None
  81. *
  82. */
  83. void qdf_mem_exit(void);
  84. #ifdef QCA_WIFI_MODULE_PARAMS_FROM_INI
  85. #define qdf_untracked_mem_malloc(size) \
  86. __qdf_untracked_mem_malloc(size, __func__, __LINE__)
  87. #define qdf_untracked_mem_free(ptr) \
  88. __qdf_untracked_mem_free(ptr)
  89. #endif
  90. #define QDF_MEM_FUNC_NAME_SIZE 48
  91. #ifdef MEMORY_DEBUG
  92. /**
  93. * qdf_mem_debug_config_get() - Get the user configuration of mem_debug_disabled
  94. *
  95. * Return: value of mem_debug_disabled qdf module argument
  96. */
  97. bool qdf_mem_debug_config_get(void);
  98. #ifdef QCA_WIFI_MODULE_PARAMS_FROM_INI
  99. /**
  100. * qdf_mem_debug_disabled_set() - Set mem_debug_disabled
  101. * @str_value: value of the module param
  102. *
  103. * This function will set qdf module param mem_debug_disabled
  104. *
  105. * Return: QDF_STATUS_SUCCESS on Success
  106. */
  107. QDF_STATUS qdf_mem_debug_disabled_config_set(const char *str_value);
  108. #endif
  109. /**
  110. * qdf_mem_malloc_debug() - debug version of QDF memory allocation API
  111. * @size: Number of bytes of memory to allocate.
  112. * @func: Function name of the call site
  113. * @line: Line number of the call site
  114. * @caller: Address of the caller function
  115. * @flag: GFP flag
  116. *
  117. * This function will dynamicallly allocate the specified number of bytes of
  118. * memory and add it to the qdf tracking list to check for memory leaks and
  119. * corruptions
  120. *
  121. * Return: A valid memory location on success, or NULL on failure
  122. */
  123. void *qdf_mem_malloc_debug(size_t size, const char *func, uint32_t line,
  124. void *caller, uint32_t flag);
  125. #define qdf_mem_malloc(size) \
  126. qdf_mem_malloc_debug(size, __func__, __LINE__, QDF_RET_IP, 0)
  127. #define qdf_mem_malloc_fl(size, func, line) \
  128. qdf_mem_malloc_debug(size, func, line, QDF_RET_IP, 0)
  129. #define qdf_mem_malloc_atomic(size) \
  130. qdf_mem_malloc_debug(size, __func__, __LINE__, QDF_RET_IP, GFP_ATOMIC)
  131. /**
  132. * qdf_mem_free_debug() - debug version of qdf_mem_free
  133. * @ptr: Pointer to the starting address of the memory to be freed.
  134. *
  135. * This function will free the memory pointed to by 'ptr'. It also checks for
  136. * memory corruption, underrun, overrun, double free, domain mismatch, etc.
  137. *
  138. * Return: none
  139. */
  140. void qdf_mem_free_debug(void *ptr, const char *file, uint32_t line);
  141. #define qdf_mem_free(ptr) \
  142. qdf_mem_free_debug(ptr, __func__, __LINE__)
  143. void qdf_mem_multi_pages_alloc_debug(qdf_device_t osdev,
  144. struct qdf_mem_multi_page_t *pages,
  145. size_t element_size, uint16_t element_num,
  146. qdf_dma_context_t memctxt, bool cacheable,
  147. const char *func, uint32_t line,
  148. void *caller);
  149. #define qdf_mem_multi_pages_alloc(osdev, pages, element_size, element_num,\
  150. memctxt, cacheable) \
  151. qdf_mem_multi_pages_alloc_debug(osdev, pages, element_size, \
  152. element_num, memctxt, cacheable, \
  153. __func__, __LINE__, QDF_RET_IP)
  154. void qdf_mem_multi_pages_free_debug(qdf_device_t osdev,
  155. struct qdf_mem_multi_page_t *pages,
  156. qdf_dma_context_t memctxt, bool cacheable,
  157. const char *func, uint32_t line);
  158. #define qdf_mem_multi_pages_free(osdev, pages, memctxt, cacheable) \
  159. qdf_mem_multi_pages_free_debug(osdev, pages, memctxt, cacheable, \
  160. __func__, __LINE__)
  161. /**
  162. * qdf_mem_check_for_leaks() - Assert that the current memory domain is empty
  163. *
  164. * Call this to ensure there are no active memory allocations being tracked
  165. * against the current debug domain. For example, one should call this function
  166. * immediately before a call to qdf_debug_domain_set() as a memory leak
  167. * detection mechanism.
  168. *
  169. * e.g.
  170. * qdf_debug_domain_set(QDF_DEBUG_DOMAIN_ACTIVE);
  171. *
  172. * ...
  173. *
  174. * // memory is allocated and freed
  175. *
  176. * ...
  177. *
  178. * // before transitioning back to inactive state,
  179. * // make sure all active memory has been freed
  180. * qdf_mem_check_for_leaks();
  181. * qdf_debug_domain_set(QDF_DEBUG_DOMAIN_INIT);
  182. *
  183. * ...
  184. *
  185. * // also, before program exit, make sure init time memory is freed
  186. * qdf_mem_check_for_leaks();
  187. * exit();
  188. *
  189. * Return: None
  190. */
  191. void qdf_mem_check_for_leaks(void);
  192. /**
  193. * qdf_mem_alloc_consistent_debug() - allocates consistent qdf memory
  194. * @osdev: OS device handle
  195. * @dev: Pointer to device handle
  196. * @size: Size to be allocated
  197. * @paddr: Physical address
  198. * @func: Function name of the call site
  199. * @line: line numbe rof the call site
  200. * @caller: Address of the caller function
  201. *
  202. * Return: pointer of allocated memory or null if memory alloc fails
  203. */
  204. void *qdf_mem_alloc_consistent_debug(qdf_device_t osdev, void *dev,
  205. qdf_size_t size, qdf_dma_addr_t *paddr,
  206. const char *func, uint32_t line,
  207. void *caller);
  208. #define qdf_mem_alloc_consistent(osdev, dev, size, paddr) \
  209. qdf_mem_alloc_consistent_debug(osdev, dev, size, paddr, \
  210. __func__, __LINE__, QDF_RET_IP)
  211. /**
  212. * qdf_mem_free_consistent_debug() - free consistent qdf memory
  213. * @osdev: OS device handle
  214. * @size: Size to be allocated
  215. * @vaddr: virtual address
  216. * @paddr: Physical address
  217. * @memctx: Pointer to DMA context
  218. * @func: Function name of the call site
  219. * @line: line numbe rof the call site
  220. *
  221. * Return: none
  222. */
  223. void qdf_mem_free_consistent_debug(qdf_device_t osdev, void *dev,
  224. qdf_size_t size, void *vaddr,
  225. qdf_dma_addr_t paddr,
  226. qdf_dma_context_t memctx,
  227. const char *func, uint32_t line);
  228. #define qdf_mem_free_consistent(osdev, dev, size, vaddr, paddr, memctx) \
  229. qdf_mem_free_consistent_debug(osdev, dev, size, vaddr, paddr, memctx, \
  230. __func__, __LINE__)
  231. #else
  232. static inline bool qdf_mem_debug_config_get(void)
  233. {
  234. return false;
  235. }
  236. static inline
  237. QDF_STATUS qdf_mem_debug_disabled_config_set(const char *str_value)
  238. {
  239. return QDF_STATUS_SUCCESS;
  240. }
  241. /**
  242. * qdf_mem_malloc() - allocation QDF memory
  243. * @size: Number of bytes of memory to allocate.
  244. *
  245. * This function will dynamicallly allocate the specified number of bytes of
  246. * memory.
  247. *
  248. * Return:
  249. * Upon successful allocate, returns a non-NULL pointer to the allocated
  250. * memory. If this function is unable to allocate the amount of memory
  251. * specified (for any reason) it returns NULL.
  252. */
  253. #define qdf_mem_malloc(size) \
  254. __qdf_mem_malloc(size, __func__, __LINE__)
  255. #define qdf_mem_malloc_fl(size, func, line) \
  256. __qdf_mem_malloc(size, func, line)
  257. /**
  258. * qdf_mem_malloc_atomic() - allocation QDF memory atomically
  259. * @size: Number of bytes of memory to allocate.
  260. *
  261. * This function will dynamicallly allocate the specified number of bytes of
  262. * memory.
  263. *
  264. * Return:
  265. * Upon successful allocate, returns a non-NULL pointer to the allocated
  266. * memory. If this function is unable to allocate the amount of memory
  267. * specified (for any reason) it returns NULL.
  268. */
  269. #define qdf_mem_malloc_atomic(size) \
  270. qdf_mem_malloc_atomic_fl(size, __func__, __LINE__)
  271. void *qdf_mem_malloc_atomic_fl(qdf_size_t size,
  272. const char *func,
  273. uint32_t line);
  274. #define qdf_mem_free(ptr) \
  275. __qdf_mem_free(ptr)
  276. static inline void qdf_mem_check_for_leaks(void) { }
  277. #define qdf_mem_alloc_consistent(osdev, dev, size, paddr) \
  278. __qdf_mem_alloc_consistent(osdev, dev, size, paddr, __func__, __LINE__)
  279. #define qdf_mem_free_consistent(osdev, dev, size, vaddr, paddr, memctx) \
  280. __qdf_mem_free_consistent(osdev, dev, size, vaddr, paddr, memctx)
  281. void qdf_mem_multi_pages_alloc(qdf_device_t osdev,
  282. struct qdf_mem_multi_page_t *pages,
  283. size_t element_size, uint16_t element_num,
  284. qdf_dma_context_t memctxt, bool cacheable);
  285. void qdf_mem_multi_pages_free(qdf_device_t osdev,
  286. struct qdf_mem_multi_page_t *pages,
  287. qdf_dma_context_t memctxt, bool cacheable);
  288. #endif /* MEMORY_DEBUG */
  289. /**
  290. * qdf_mem_malloc_flags: Get mem allocation flags
  291. *
  292. * Return the flag to be use for memory allocation
  293. * based on the context
  294. *
  295. * Returns: Based on the context, returns the GFP flag
  296. * for memory alloaction
  297. */
  298. int qdf_mem_malloc_flags(void);
  299. /**
  300. * qdf_prealloc_disabled_config_get() - Get the user configuration of
  301. * prealloc_disabled
  302. *
  303. * Return: value of prealloc_disabled qdf module argument
  304. */
  305. bool qdf_prealloc_disabled_config_get(void);
  306. #ifdef QCA_WIFI_MODULE_PARAMS_FROM_INI
  307. /**
  308. * qdf_prealloc_disabled_config_set() - Set prealloc_disabled
  309. * @str_value: value of the module param
  310. *
  311. * This function will set qdf module param prealloc_disabled
  312. *
  313. * Return: QDF_STATUS_SUCCESS on Success
  314. */
  315. QDF_STATUS qdf_prealloc_disabled_config_set(const char *str_value);
  316. #endif
  317. /**
  318. * qdf_mem_multi_pages_zero() - zero out each page memory
  319. * @pages: Multi page information storage
  320. * @cacheable: Coherent memory or cacheable memory
  321. *
  322. * This function will zero out each page memory
  323. *
  324. * Return: None
  325. */
  326. void qdf_mem_multi_pages_zero(struct qdf_mem_multi_page_t *pages,
  327. bool cacheable);
  328. /**
  329. * qdf_aligned_malloc() - allocates aligned QDF memory.
  330. * @size: Size to be allocated
  331. * @vaddr_unaligned: Unaligned virtual address.
  332. * @paddr_unaligned: Unaligned physical address.
  333. * @paddr_aligned: Aligned physical address.
  334. * @align: Base address alignment.
  335. * @func: Function name of the call site.
  336. * @line: Line number of the call site.
  337. *
  338. * This function will dynamically allocate the specified number of bytes of
  339. * memory. Checks if the allocated base address is aligned with base_align.
  340. * If not, it frees the allocated memory, adds base_align to alloc size and
  341. * re-allocates the memory.
  342. *
  343. * Return:
  344. * Upon successful allocate, returns an aligned base address of the allocated
  345. * memory. If this function is unable to allocate the amount of memory
  346. * specified (for any reason) it returns NULL.
  347. */
  348. #define qdf_aligned_malloc(size, vaddr_unaligned, paddr_unaligned, \
  349. paddr_aligned, align) \
  350. qdf_aligned_malloc_fl(size, vaddr_unaligned, paddr_unaligned, \
  351. paddr_aligned, align, __func__, __LINE__)
  352. void *qdf_aligned_malloc_fl(uint32_t *size, void **vaddr_unaligned,
  353. qdf_dma_addr_t *paddr_unaligned,
  354. qdf_dma_addr_t *paddr_aligned,
  355. uint32_t align,
  356. const char *func, uint32_t line);
  357. /**
  358. * qdf_aligned_mem_alloc_consistent() - allocates consistent qdf memory
  359. * @osdev: OS device handle
  360. * @size: Size to be allocated
  361. * @vaddr_unaligned: Unaligned virtual address.
  362. * @paddr_unaligned: Unaligned physical address.
  363. * @paddr_aligned: Aligned physical address.
  364. * @align: Base address alignment.
  365. * @func: Function name of the call site.
  366. * @line: Line number of the call site.
  367. *
  368. * Return: pointer of allocated memory or null if memory alloc fails.
  369. */
  370. #define qdf_aligned_mem_alloc_consistent(osdev, size, vaddr_unaligned, \
  371. paddr_unaligned, paddr_aligned, \
  372. align) \
  373. qdf_aligned_mem_alloc_consistent_fl(osdev, size, vaddr_unaligned, \
  374. paddr_unaligned, paddr_aligned, \
  375. align, __func__, __LINE__)
  376. void *qdf_aligned_mem_alloc_consistent_fl(qdf_device_t osdev, uint32_t *size,
  377. void **vaddr_unaligned,
  378. qdf_dma_addr_t *paddr_unaligned,
  379. qdf_dma_addr_t *paddr_aligned,
  380. uint32_t align, const char *func,
  381. uint32_t line);
  382. #define qdf_mem_virt_to_phys(vaddr) virt_to_phys(vaddr)
  383. void qdf_mem_set_io(void *ptr, uint32_t num_bytes, uint32_t value);
  384. void qdf_mem_copy_toio(void *dst_addr, const void *src_addr,
  385. uint32_t num_bytes);
  386. /**
  387. * qdf_mem_set() - set (fill) memory with a specified byte value.
  388. * @ptr: Pointer to memory that will be set
  389. * @num_bytes: Number of bytes to be set
  390. * @value: Byte set in memory
  391. *
  392. * WARNING: parameter @num_bytes and @value are swapped comparing with
  393. * standard C function "memset", please ensure correct usage of this function!
  394. *
  395. * Return: None
  396. */
  397. void qdf_mem_set(void *ptr, uint32_t num_bytes, uint32_t value);
  398. /**
  399. * qdf_mem_zero() - zero out memory
  400. * @ptr: pointer to memory that will be set to zero
  401. * @num_bytes: number of bytes zero
  402. *
  403. * This function sets the memory location to all zeros, essentially clearing
  404. * the memory.
  405. *
  406. * Return: None
  407. */
  408. static inline void qdf_mem_zero(void *ptr, uint32_t num_bytes)
  409. {
  410. qdf_mem_set(ptr, num_bytes, 0);
  411. }
  412. /**
  413. * qdf_mem_copy() - copy memory
  414. * @dst_addr: Pointer to destination memory location (to copy to)
  415. * @src_addr: Pointer to source memory location (to copy from)
  416. * @num_bytes: Number of bytes to copy.
  417. *
  418. * Copy host memory from one location to another, similar to memcpy in
  419. * standard C. Note this function does not specifically handle overlapping
  420. * source and destination memory locations. Calling this function with
  421. * overlapping source and destination memory locations will result in
  422. * unpredictable results. Use qdf_mem_move() if the memory locations
  423. * for the source and destination are overlapping (or could be overlapping!)
  424. *
  425. * Return: none
  426. */
  427. void qdf_mem_copy(void *dst_addr, const void *src_addr, uint32_t num_bytes);
  428. /**
  429. * qdf_mem_move() - move memory
  430. * @dst_addr: pointer to destination memory location (to move to)
  431. * @src_addr: pointer to source memory location (to move from)
  432. * @num_bytes: number of bytes to move.
  433. *
  434. * Move host memory from one location to another, similar to memmove in
  435. * standard C. Note this function *does* handle overlapping
  436. * source and destination memory locations.
  437. * Return: None
  438. */
  439. void qdf_mem_move(void *dst_addr, const void *src_addr, uint32_t num_bytes);
  440. /**
  441. * qdf_mem_cmp() - memory compare
  442. * @left: pointer to one location in memory to compare
  443. * @right: pointer to second location in memory to compare
  444. * @size: the number of bytes to compare
  445. *
  446. * Function to compare two pieces of memory, similar to memcmp function
  447. * in standard C.
  448. *
  449. * Return:
  450. * 0 -- equal
  451. * < 0 -- *memory1 is less than *memory2
  452. * > 0 -- *memory1 is bigger than *memory2
  453. */
  454. int qdf_mem_cmp(const void *left, const void *right, size_t size);
  455. void qdf_ether_addr_copy(void *dst_addr, const void *src_addr);
  456. /**
  457. * qdf_mem_map_nbytes_single - Map memory for DMA
  458. * @osdev: pomter OS device context
  459. * @buf: pointer to memory to be dma mapped
  460. * @dir: DMA map direction
  461. * @nbytes: number of bytes to be mapped.
  462. * @phy_addr: ponter to recive physical address.
  463. *
  464. * Return: success/failure
  465. */
  466. static inline uint32_t qdf_mem_map_nbytes_single(qdf_device_t osdev, void *buf,
  467. qdf_dma_dir_t dir, int nbytes,
  468. qdf_dma_addr_t *phy_addr)
  469. {
  470. #if defined(HIF_PCI) || defined(HIF_IPCI)
  471. return __qdf_mem_map_nbytes_single(osdev, buf, dir, nbytes, phy_addr);
  472. #else
  473. return 0;
  474. #endif
  475. }
  476. static inline void qdf_mem_dma_cache_sync(qdf_device_t osdev,
  477. qdf_dma_addr_t buf,
  478. qdf_dma_dir_t dir,
  479. int nbytes)
  480. {
  481. __qdf_mem_dma_cache_sync(osdev, buf, dir, nbytes);
  482. }
  483. /**
  484. * qdf_mem_unmap_nbytes_single() - un_map memory for DMA
  485. * @osdev: pomter OS device context
  486. * @phy_addr: physical address of memory to be dma unmapped
  487. * @dir: DMA unmap direction
  488. * @nbytes: number of bytes to be unmapped.
  489. *
  490. * Return: none
  491. */
  492. static inline void qdf_mem_unmap_nbytes_single(qdf_device_t osdev,
  493. qdf_dma_addr_t phy_addr,
  494. qdf_dma_dir_t dir,
  495. int nbytes)
  496. {
  497. #if defined(HIF_PCI) || defined(HIF_IPCI)
  498. __qdf_mem_unmap_nbytes_single(osdev, phy_addr, dir, nbytes);
  499. #endif
  500. }
  501. /**
  502. * qdf_mempool_init - Create and initialize memory pool
  503. * @osdev: platform device object
  504. * @pool_addr: address of the pool created
  505. * @elem_cnt: no. of elements in pool
  506. * @elem_size: size of each pool element in bytes
  507. * @flags: flags
  508. * Return: Handle to memory pool or NULL if allocation failed
  509. */
  510. static inline int qdf_mempool_init(qdf_device_t osdev,
  511. qdf_mempool_t *pool_addr, int elem_cnt,
  512. size_t elem_size, uint32_t flags)
  513. {
  514. return __qdf_mempool_init(osdev, pool_addr, elem_cnt, elem_size,
  515. flags);
  516. }
  517. /**
  518. * qdf_mempool_destroy - Destroy memory pool
  519. * @osdev: platform device object
  520. * @Handle: to memory pool
  521. * Return: none
  522. */
  523. static inline void qdf_mempool_destroy(qdf_device_t osdev, qdf_mempool_t pool)
  524. {
  525. __qdf_mempool_destroy(osdev, pool);
  526. }
  527. /**
  528. * qdf_mempool_alloc - Allocate an element memory pool
  529. * @osdev: platform device object
  530. * @Handle: to memory pool
  531. * Return: Pointer to the allocated element or NULL if the pool is empty
  532. */
  533. static inline void *qdf_mempool_alloc(qdf_device_t osdev, qdf_mempool_t pool)
  534. {
  535. return (void *)__qdf_mempool_alloc(osdev, pool);
  536. }
  537. /**
  538. * qdf_mempool_free - Free a memory pool element
  539. * @osdev: Platform device object
  540. * @pool: Handle to memory pool
  541. * @buf: Element to be freed
  542. * Return: none
  543. */
  544. static inline void qdf_mempool_free(qdf_device_t osdev, qdf_mempool_t pool,
  545. void *buf)
  546. {
  547. __qdf_mempool_free(osdev, pool, buf);
  548. }
  549. void qdf_mem_dma_sync_single_for_device(qdf_device_t osdev,
  550. qdf_dma_addr_t bus_addr,
  551. qdf_size_t size,
  552. __dma_data_direction direction);
  553. void qdf_mem_dma_sync_single_for_cpu(qdf_device_t osdev,
  554. qdf_dma_addr_t bus_addr,
  555. qdf_size_t size,
  556. __dma_data_direction direction);
  557. int qdf_mem_multi_page_link(qdf_device_t osdev,
  558. struct qdf_mem_multi_page_t *pages,
  559. uint32_t elem_size, uint32_t elem_count, uint8_t cacheable);
  560. /**
  561. * qdf_mem_kmalloc_inc() - increment kmalloc allocated bytes count
  562. * @size: number of bytes to increment by
  563. *
  564. * Return: None
  565. */
  566. void qdf_mem_kmalloc_inc(qdf_size_t size);
  567. /**
  568. * qdf_mem_kmalloc_dec() - decrement kmalloc allocated bytes count
  569. * @size: number of bytes to decrement by
  570. *
  571. * Return: None
  572. */
  573. void qdf_mem_kmalloc_dec(qdf_size_t size);
  574. #ifdef CONFIG_WLAN_SYSFS_MEM_STATS
  575. /**
  576. * qdf_mem_skb_inc() - increment total skb allocation size
  577. * @size: size to be added
  578. *
  579. * Return: none
  580. */
  581. void qdf_mem_skb_inc(qdf_size_t size);
  582. /**
  583. * qdf_mem_skb_dec() - decrement total skb allocation size
  584. * @size: size to be decremented
  585. *
  586. * Return: none
  587. */
  588. void qdf_mem_skb_dec(qdf_size_t size);
  589. /**
  590. * qdf_mem_skb_total_inc() - increment total skb allocation size
  591. * in host driver in both debug and perf builds
  592. * @size: size to be added
  593. *
  594. * Return: none
  595. */
  596. void qdf_mem_skb_total_inc(qdf_size_t size);
  597. /**
  598. * qdf_mem_skb_total_dec() - decrement total skb allocation size
  599. * in the host driver in debug and perf flavors
  600. * @size: size to be decremented
  601. *
  602. * Return: none
  603. */
  604. void qdf_mem_skb_total_dec(qdf_size_t size);
  605. /**
  606. * qdf_mem_dp_tx_skb_inc() - Increment Tx skb allocation size
  607. * @size: size to be added
  608. *
  609. * Return: none
  610. */
  611. void qdf_mem_dp_tx_skb_inc(qdf_size_t size);
  612. /**
  613. * qdf_mem_dp_tx_skb_dec() - Decrement Tx skb allocation size
  614. * @size: size to be decreased
  615. *
  616. * Return: none
  617. */
  618. void qdf_mem_dp_tx_skb_dec(qdf_size_t size);
  619. /**
  620. * qdf_mem_dp_rx_skb_inc() - Increment Rx skb allocation size
  621. * @size: size to be added
  622. *
  623. * Return: none
  624. */
  625. void qdf_mem_dp_rx_skb_inc(qdf_size_t size);
  626. /**
  627. * qdf_mem_dp_rx_skb_dec() - Decrement Rx skb allocation size
  628. * @size: size to be decreased
  629. *
  630. * Return: none
  631. */
  632. void qdf_mem_dp_rx_skb_dec(qdf_size_t size);
  633. /**
  634. * qdf_mem_dp_tx_skb_cnt_inc() - Increment Tx buffer count
  635. *
  636. * Return: none
  637. */
  638. void qdf_mem_dp_tx_skb_cnt_inc(void);
  639. /**
  640. * qdf_mem_dp_tx_skb_cnt_dec() - Decrement Tx buffer count
  641. *
  642. * Return: none
  643. */
  644. void qdf_mem_dp_tx_skb_cnt_dec(void);
  645. /**
  646. * qdf_mem_dp_rx_skb_cnt_inc() - Increment Rx buffer count
  647. *
  648. * Return: none
  649. */
  650. void qdf_mem_dp_rx_skb_cnt_inc(void);
  651. /**
  652. * qdf_mem_dp_rx_skb_cnt_dec() - Decrement Rx buffer count
  653. *
  654. * Return: none
  655. */
  656. void qdf_mem_dp_rx_skb_cnt_dec(void);
  657. #else
  658. static inline void qdf_mem_skb_inc(qdf_size_t size)
  659. {
  660. }
  661. static inline void qdf_mem_skb_dec(qdf_size_t size)
  662. {
  663. }
  664. static inline void qdf_mem_skb_total_inc(qdf_size_t size)
  665. {
  666. }
  667. static inline void qdf_mem_skb_total_dec(qdf_size_t size)
  668. {
  669. }
  670. static inline void qdf_mem_dp_tx_skb_inc(qdf_size_t size)
  671. {
  672. }
  673. static inline void qdf_mem_dp_tx_skb_dec(qdf_size_t size)
  674. {
  675. }
  676. static inline void qdf_mem_dp_rx_skb_inc(qdf_size_t size)
  677. {
  678. }
  679. static inline void qdf_mem_dp_rx_skb_dec(qdf_size_t size)
  680. {
  681. }
  682. static inline void qdf_mem_dp_tx_skb_cnt_inc(void)
  683. {
  684. }
  685. static inline void qdf_mem_dp_tx_skb_cnt_dec(void)
  686. {
  687. }
  688. static inline void qdf_mem_dp_rx_skb_cnt_inc(void)
  689. {
  690. }
  691. static inline void qdf_mem_dp_rx_skb_cnt_dec(void)
  692. {
  693. }
  694. #endif /* CONFIG_WLAN_SYSFS_MEM_STATS */
  695. /**
  696. * qdf_mem_map_table_alloc() - Allocate shared memory info structure
  697. * @num: number of required storage
  698. *
  699. * Allocate mapping table for DMA memory allocation. This is needed for
  700. * IPA-WLAN buffer sharing when SMMU Stage1 Translation is enabled.
  701. *
  702. * Return: shared memory info storage table pointer
  703. */
  704. static inline qdf_mem_info_t *qdf_mem_map_table_alloc(uint32_t num)
  705. {
  706. qdf_mem_info_t *mem_info_arr;
  707. mem_info_arr = qdf_mem_malloc(num * sizeof(mem_info_arr[0]));
  708. return mem_info_arr;
  709. }
  710. /**
  711. * qdf_update_mem_map_table() - Update DMA memory map info
  712. * @osdev: Parent device instance
  713. * @mem_info: Pointer to shared memory information
  714. * @dma_addr: dma address
  715. * @mem_size: memory size allocated
  716. *
  717. * Store DMA shared memory information
  718. *
  719. * Return: none
  720. */
  721. static inline void qdf_update_mem_map_table(qdf_device_t osdev,
  722. qdf_mem_info_t *mem_info,
  723. qdf_dma_addr_t dma_addr,
  724. uint32_t mem_size)
  725. {
  726. if (!mem_info) {
  727. qdf_nofl_err("%s: NULL mem_info", __func__);
  728. return;
  729. }
  730. __qdf_update_mem_map_table(osdev, mem_info, dma_addr, mem_size);
  731. }
  732. /**
  733. * qdf_mem_smmu_s1_enabled() - Return SMMU stage 1 translation enable status
  734. * @osdev parent device instance
  735. *
  736. * Return: true if smmu s1 enabled, false if smmu s1 is bypassed
  737. */
  738. static inline bool qdf_mem_smmu_s1_enabled(qdf_device_t osdev)
  739. {
  740. return __qdf_mem_smmu_s1_enabled(osdev);
  741. }
  742. /**
  743. * qdf_mem_paddr_from_dmaaddr() - get actual physical address from dma address
  744. * @osdev: Parent device instance
  745. * @dma_addr: DMA/IOVA address
  746. *
  747. * Get actual physical address from dma_addr based on SMMU enablement status.
  748. * IF SMMU Stage 1 tranlation is enabled, DMA APIs return IO virtual address
  749. * (IOVA) otherwise returns physical address. So get SMMU physical address
  750. * mapping from IOVA.
  751. *
  752. * Return: dmaable physical address
  753. */
  754. static inline qdf_dma_addr_t qdf_mem_paddr_from_dmaaddr(qdf_device_t osdev,
  755. qdf_dma_addr_t dma_addr)
  756. {
  757. return __qdf_mem_paddr_from_dmaaddr(osdev, dma_addr);
  758. }
  759. /**
  760. * qdf_mem_dma_get_sgtable() - Returns DMA memory scatter gather table
  761. * @dev: device instace
  762. * @sgt: scatter gather table pointer
  763. * @cpu_addr: HLOS virtual address
  764. * @dma_addr: dma address
  765. * @size: allocated memory size
  766. *
  767. * Return: physical address
  768. */
  769. static inline int
  770. qdf_mem_dma_get_sgtable(struct device *dev, void *sgt, void *cpu_addr,
  771. qdf_dma_addr_t dma_addr, size_t size)
  772. {
  773. return __qdf_os_mem_dma_get_sgtable(dev, sgt, cpu_addr, dma_addr, size);
  774. }
  775. /**
  776. * qdf_mem_free_sgtable() - Free a previously allocated sg table
  777. * @sgt: the mapped sg table header
  778. *
  779. * Return: None
  780. */
  781. static inline void
  782. qdf_mem_free_sgtable(struct sg_table *sgt)
  783. {
  784. __qdf_os_mem_free_sgtable(sgt);
  785. }
  786. /**
  787. * qdf_dma_get_sgtable_dma_addr() - Assigns DMA address to scatterlist elements
  788. * @sgt: scatter gather table pointer
  789. *
  790. * Return: None
  791. */
  792. static inline void
  793. qdf_dma_get_sgtable_dma_addr(struct sg_table *sgt)
  794. {
  795. __qdf_dma_get_sgtable_dma_addr(sgt);
  796. }
  797. /**
  798. * qdf_mem_get_dma_addr() - Return dma address based on SMMU translation status.
  799. * @osdev: Parent device instance
  800. * @mem_info: Pointer to allocated memory information
  801. *
  802. * Get dma address based on SMMU enablement status. If SMMU Stage 1
  803. * tranlation is enabled, DMA APIs return IO virtual address otherwise
  804. * returns physical address.
  805. *
  806. * Return: dma address
  807. */
  808. static inline qdf_dma_addr_t qdf_mem_get_dma_addr(qdf_device_t osdev,
  809. qdf_mem_info_t *mem_info)
  810. {
  811. return __qdf_mem_get_dma_addr(osdev, mem_info);
  812. }
  813. /**
  814. * qdf_mem_get_dma_addr_ptr() - Return DMA address pointer from mem info struct
  815. * @osdev: Parent device instance
  816. * @mem_info: Pointer to allocated memory information
  817. *
  818. * Based on smmu stage 1 translation enablement, return corresponding dma
  819. * address storage pointer.
  820. *
  821. * Return: dma address storage pointer
  822. */
  823. static inline qdf_dma_addr_t *qdf_mem_get_dma_addr_ptr(qdf_device_t osdev,
  824. qdf_mem_info_t *mem_info)
  825. {
  826. return __qdf_mem_get_dma_addr_ptr(osdev, mem_info);
  827. }
  828. /**
  829. * qdf_mem_get_dma_size() - Return DMA memory size
  830. * @osdev: parent device instance
  831. * @mem_info: Pointer to allocated memory information
  832. *
  833. * Return: DMA memory size
  834. */
  835. static inline uint32_t
  836. qdf_mem_get_dma_size(qdf_device_t osdev,
  837. qdf_mem_info_t *mem_info)
  838. {
  839. return __qdf_mem_get_dma_size(osdev, mem_info);
  840. }
  841. /**
  842. * qdf_mem_set_dma_size() - Set DMA memory size
  843. * @osdev: parent device instance
  844. * @mem_info: Pointer to allocated memory information
  845. * @mem_size: memory size allocated
  846. *
  847. * Return: none
  848. */
  849. static inline void
  850. qdf_mem_set_dma_size(qdf_device_t osdev,
  851. qdf_mem_info_t *mem_info,
  852. uint32_t mem_size)
  853. {
  854. __qdf_mem_set_dma_size(osdev, mem_info, mem_size);
  855. }
  856. /**
  857. * qdf_mem_get_dma_size() - Return DMA physical address
  858. * @osdev: parent device instance
  859. * @mem_info: Pointer to allocated memory information
  860. *
  861. * Return: DMA physical address
  862. */
  863. static inline qdf_dma_addr_t
  864. qdf_mem_get_dma_pa(qdf_device_t osdev,
  865. qdf_mem_info_t *mem_info)
  866. {
  867. return __qdf_mem_get_dma_pa(osdev, mem_info);
  868. }
  869. /**
  870. * qdf_mem_set_dma_size() - Set DMA physical address
  871. * @osdev: parent device instance
  872. * @mem_info: Pointer to allocated memory information
  873. * @dma_pa: DMA phsical address
  874. *
  875. * Return: none
  876. */
  877. static inline void
  878. qdf_mem_set_dma_pa(qdf_device_t osdev,
  879. qdf_mem_info_t *mem_info,
  880. qdf_dma_addr_t dma_pa)
  881. {
  882. __qdf_mem_set_dma_pa(osdev, mem_info, dma_pa);
  883. }
  884. /**
  885. * qdf_mem_shared_mem_alloc() - Allocate DMA memory for shared resource
  886. * @osdev: parent device instance
  887. * @mem_info: Pointer to allocated memory information
  888. * @size: size to be allocated
  889. *
  890. * Allocate DMA memory which will be shared with external kernel module. This
  891. * information is needed for SMMU mapping.
  892. *
  893. * Return: 0 success
  894. */
  895. qdf_shared_mem_t *qdf_mem_shared_mem_alloc(qdf_device_t osdev, uint32_t size);
  896. /**
  897. * qdf_mem_shared_mem_free() - Free shared memory
  898. * @osdev: parent device instance
  899. * @shared_mem: shared memory information storage
  900. *
  901. * Free DMA shared memory resource
  902. *
  903. * Return: None
  904. */
  905. static inline void qdf_mem_shared_mem_free(qdf_device_t osdev,
  906. qdf_shared_mem_t *shared_mem)
  907. {
  908. if (!shared_mem) {
  909. qdf_nofl_err("%s: NULL shared mem struct passed",
  910. __func__);
  911. return;
  912. }
  913. if (shared_mem->vaddr) {
  914. qdf_mem_free_consistent(osdev, osdev->dev,
  915. qdf_mem_get_dma_size(osdev,
  916. &shared_mem->mem_info),
  917. shared_mem->vaddr,
  918. qdf_mem_get_dma_addr(osdev,
  919. &shared_mem->mem_info),
  920. qdf_get_dma_mem_context(shared_mem,
  921. memctx));
  922. }
  923. qdf_mem_free_sgtable(&shared_mem->sgtable);
  924. qdf_mem_free(shared_mem);
  925. }
  926. /**
  927. * qdf_dma_mem_stats_read() - Return the DMA memory allocated in
  928. * host driver
  929. *
  930. * Return: Total DMA memory allocated
  931. */
  932. int32_t qdf_dma_mem_stats_read(void);
  933. /**
  934. * qdf_heap_mem_stats_read() - Return the heap memory allocated
  935. * in host driver
  936. *
  937. * Return: Total heap memory allocated
  938. */
  939. int32_t qdf_heap_mem_stats_read(void);
  940. /**
  941. * qdf_skb_mem_stats_read() - Return the SKB memory allocated in
  942. * host driver
  943. *
  944. * Return: Total SKB memory allocated
  945. */
  946. int32_t qdf_skb_mem_stats_read(void);
  947. /**
  948. * qdf_skb_total_mem_stats_read() - Return the SKB memory allocated
  949. * in the host driver tracked in both debug and perf builds
  950. *
  951. * Return: Total SKB memory allocated
  952. */
  953. int32_t qdf_skb_total_mem_stats_read(void);
  954. /**
  955. * qdf_skb_max_mem_stats_read() - Return the max SKB memory
  956. * allocated in host driver. This is the high watermark for the
  957. * total SKB allocated in the host driver
  958. *
  959. * Return: None
  960. */
  961. int32_t qdf_skb_max_mem_stats_read(void);
  962. /**
  963. * qdf_mem_tx_desc_cnt_read() - Return the outstanding Tx descs
  964. * which are waiting on Tx completions
  965. *
  966. * Return: Outstanding Tx desc count
  967. */
  968. int32_t qdf_mem_tx_desc_cnt_read(void);
  969. /**
  970. * qdf_mem_tx_desc_max_read() - Return the max outstanding Tx
  971. * descs which are waiting on Tx completions. This is the high
  972. * watermark for the pending desc count
  973. *
  974. * Return: Max outstanding Tx desc count
  975. */
  976. int32_t qdf_mem_tx_desc_max_read(void);
  977. /**
  978. * qdf_mem_stats_init() - Initialize the qdf memstats fields on
  979. * creating the sysfs node
  980. *
  981. * Return: None
  982. */
  983. void qdf_mem_stats_init(void);
  984. /**
  985. * qdf_dp_tx_skb_mem_stats_read() - Return the SKB memory
  986. * allocated for Tx data path
  987. *
  988. * Return: Tx SKB memory allocated
  989. */
  990. int32_t qdf_dp_tx_skb_mem_stats_read(void);
  991. /**
  992. * qdf_dp_rx_skb_mem_stats_read() - Return the SKB memory
  993. * allocated for Rx data path
  994. *
  995. * Return: Rx SKB memory allocated
  996. */
  997. int32_t qdf_dp_rx_skb_mem_stats_read(void);
  998. /**
  999. * qdf_dp_tx_skb_max_mem_stats_read() - Return the high
  1000. * watermark for the SKB memory allocated for Tx data path
  1001. *
  1002. * Return: Max Tx SKB memory allocated
  1003. */
  1004. int32_t qdf_dp_tx_skb_max_mem_stats_read(void);
  1005. /**
  1006. * qdf_dp_rx_skb_max_mem_stats_read() - Return the high
  1007. * watermark for the SKB memory allocated for Rx data path
  1008. *
  1009. * Return: Max Rx SKB memory allocated
  1010. */
  1011. int32_t qdf_dp_rx_skb_max_mem_stats_read(void);
  1012. /**
  1013. * qdf_mem_dp_tx_skb_cnt_read() - Return number of buffers
  1014. * allocated in the Tx data path by the host driver or
  1015. * buffers coming from the n/w stack
  1016. *
  1017. * Return: Number of DP Tx buffers allocated
  1018. */
  1019. int32_t qdf_mem_dp_tx_skb_cnt_read(void);
  1020. /**
  1021. * qdf_mem_dp_tx_skb_max_cnt_read() - Return max number of
  1022. * buffers allocated in the Tx data path
  1023. *
  1024. * Return: Max number of DP Tx buffers allocated
  1025. */
  1026. int32_t qdf_mem_dp_tx_skb_max_cnt_read(void);
  1027. /**
  1028. * qdf_mem_dp_rx_skb_cnt_read() - Return number of buffers
  1029. * allocated in the Rx data path
  1030. *
  1031. * Return: Number of DP Rx buffers allocated
  1032. */
  1033. int32_t qdf_mem_dp_rx_skb_cnt_read(void);
  1034. /**
  1035. * qdf_mem_dp_rx_skb_max_cnt_read() - Return max number of
  1036. * buffers allocated in the Rx data path
  1037. *
  1038. * Return: Max number of DP Rx buffers allocated
  1039. */
  1040. int32_t qdf_mem_dp_rx_skb_max_cnt_read(void);
  1041. /**
  1042. * qdf_mem_tx_desc_cnt_update() - Update the pending tx desc
  1043. * count and the high watermark for pending tx desc count
  1044. *
  1045. * @pending_tx_descs: outstanding Tx desc count
  1046. * @tx_descs_max: high watermark for outstanding Tx desc count
  1047. *
  1048. * Return: None
  1049. */
  1050. void qdf_mem_tx_desc_cnt_update(qdf_atomic_t pending_tx_descs,
  1051. int32_t tx_descs_max);
  1052. #endif /* __QDF_MEMORY_H */