qdf_mem.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. * 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. #define QDF_CACHE_LINE_SZ __qdf_cache_line_sz
  28. /**
  29. * qdf_align() - align to the given size.
  30. * @a: input that needs to be aligned.
  31. * @align_size: boundary on which 'a' has to be alinged.
  32. *
  33. * Return: aligned value.
  34. */
  35. #define qdf_align(a, align_size) __qdf_align(a, align_size)
  36. /**
  37. * struct qdf_mem_dma_page_t - Allocated dmaable page
  38. * @page_v_addr_start: Page start virtual address
  39. * @page_v_addr_end: Page end virtual address
  40. * @page_p_addr: Page start physical address
  41. */
  42. struct qdf_mem_dma_page_t {
  43. char *page_v_addr_start;
  44. char *page_v_addr_end;
  45. qdf_dma_addr_t page_p_addr;
  46. };
  47. /**
  48. * struct qdf_mem_multi_page_t - multiple page allocation information storage
  49. * @num_element_per_page: Number of element in single page
  50. * @num_pages: Number of allocation needed pages
  51. * @dma_pages: page information storage in case of coherent memory
  52. * @cacheable_pages: page information storage in case of cacheable memory
  53. */
  54. struct qdf_mem_multi_page_t {
  55. uint16_t num_element_per_page;
  56. uint16_t num_pages;
  57. struct qdf_mem_dma_page_t *dma_pages;
  58. void **cacheable_pages;
  59. };
  60. /* Preprocessor definitions and constants */
  61. typedef __qdf_mempool_t qdf_mempool_t;
  62. /**
  63. * qdf_mem_init() - Initialize QDF memory module
  64. *
  65. * Return: None
  66. *
  67. */
  68. void qdf_mem_init(void);
  69. /**
  70. * qdf_mem_exit() - Exit QDF memory module
  71. *
  72. * Return: None
  73. *
  74. */
  75. void qdf_mem_exit(void);
  76. #define QDF_MEM_FUNC_NAME_SIZE 48
  77. #ifdef MEMORY_DEBUG
  78. /**
  79. * qdf_mem_malloc_debug() - debug version of QDF memory allocation API
  80. * @size: Number of bytes of memory to allocate.
  81. * @func: Function name of the call site
  82. * @line: Line number of the call site
  83. * @caller: Address of the caller function
  84. * @flag: GFP flag
  85. *
  86. * This function will dynamicallly allocate the specified number of bytes of
  87. * memory and add it to the qdf tracking list to check for memory leaks and
  88. * corruptions
  89. *
  90. * Return: A valid memory location on success, or NULL on failure
  91. */
  92. void *qdf_mem_malloc_debug(size_t size, const char *func, uint32_t line,
  93. void *caller, uint32_t flag);
  94. #define qdf_mem_malloc(size) \
  95. qdf_mem_malloc_debug(size, __func__, __LINE__, QDF_RET_IP, 0)
  96. #define qdf_mem_malloc_fl(size, func, line) \
  97. qdf_mem_malloc_debug(size, func, line, QDF_RET_IP, 0)
  98. #define qdf_mem_malloc_atomic(size) \
  99. qdf_mem_malloc_debug(size, __func__, __LINE__, QDF_RET_IP, GFP_ATOMIC)
  100. /**
  101. * qdf_mem_free_debug() - debug version of qdf_mem_free
  102. * @ptr: Pointer to the starting address of the memory to be freed.
  103. *
  104. * This function will free the memory pointed to by 'ptr'. It also checks for
  105. * memory corruption, underrun, overrun, double free, domain mismatch, etc.
  106. *
  107. * Return: none
  108. */
  109. void qdf_mem_free_debug(void *ptr, const char *file, uint32_t line);
  110. #define qdf_mem_free(ptr) \
  111. qdf_mem_free_debug(ptr, __func__, __LINE__)
  112. void qdf_mem_multi_pages_alloc_debug(qdf_device_t osdev,
  113. struct qdf_mem_multi_page_t *pages,
  114. size_t element_size, uint16_t element_num,
  115. qdf_dma_context_t memctxt, bool cacheable,
  116. const char *func, uint32_t line,
  117. void *caller);
  118. #define qdf_mem_multi_pages_alloc(osdev, pages, element_size, element_num,\
  119. memctxt, cacheable) \
  120. qdf_mem_multi_pages_alloc_debug(osdev, pages, element_size, \
  121. element_num, memctxt, cacheable, \
  122. __func__, __LINE__, QDF_RET_IP)
  123. void qdf_mem_multi_pages_free_debug(qdf_device_t osdev,
  124. struct qdf_mem_multi_page_t *pages,
  125. qdf_dma_context_t memctxt, bool cacheable,
  126. const char *func, uint32_t line);
  127. #define qdf_mem_multi_pages_free(osdev, pages, memctxt, cacheable) \
  128. qdf_mem_multi_pages_free_debug(osdev, pages, memctxt, cacheable, \
  129. __func__, __LINE__)
  130. /**
  131. * qdf_mem_check_for_leaks() - Assert that the current memory domain is empty
  132. *
  133. * Call this to ensure there are no active memory allocations being tracked
  134. * against the current debug domain. For example, one should call this function
  135. * immediately before a call to qdf_debug_domain_set() as a memory leak
  136. * detection mechanism.
  137. *
  138. * e.g.
  139. * qdf_debug_domain_set(QDF_DEBUG_DOMAIN_ACTIVE);
  140. *
  141. * ...
  142. *
  143. * // memory is allocated and freed
  144. *
  145. * ...
  146. *
  147. * // before transitioning back to inactive state,
  148. * // make sure all active memory has been freed
  149. * qdf_mem_check_for_leaks();
  150. * qdf_debug_domain_set(QDF_DEBUG_DOMAIN_INIT);
  151. *
  152. * ...
  153. *
  154. * // also, before program exit, make sure init time memory is freed
  155. * qdf_mem_check_for_leaks();
  156. * exit();
  157. *
  158. * Return: None
  159. */
  160. void qdf_mem_check_for_leaks(void);
  161. /**
  162. * qdf_mem_alloc_consistent_debug() - allocates consistent qdf memory
  163. * @osdev: OS device handle
  164. * @dev: Pointer to device handle
  165. * @size: Size to be allocated
  166. * @paddr: Physical address
  167. * @func: Function name of the call site
  168. * @line: line numbe rof the call site
  169. * @caller: Address of the caller function
  170. *
  171. * Return: pointer of allocated memory or null if memory alloc fails
  172. */
  173. void *qdf_mem_alloc_consistent_debug(qdf_device_t osdev, void *dev,
  174. qdf_size_t size, qdf_dma_addr_t *paddr,
  175. const char *func, uint32_t line,
  176. void *caller);
  177. #define qdf_mem_alloc_consistent(osdev, dev, size, paddr) \
  178. qdf_mem_alloc_consistent_debug(osdev, dev, size, paddr, \
  179. __func__, __LINE__, QDF_RET_IP)
  180. /**
  181. * qdf_mem_free_consistent_debug() - free consistent qdf memory
  182. * @osdev: OS device handle
  183. * @size: Size to be allocated
  184. * @vaddr: virtual address
  185. * @paddr: Physical address
  186. * @memctx: Pointer to DMA context
  187. * @func: Function name of the call site
  188. * @line: line numbe rof the call site
  189. *
  190. * Return: none
  191. */
  192. void qdf_mem_free_consistent_debug(qdf_device_t osdev, void *dev,
  193. qdf_size_t size, void *vaddr,
  194. qdf_dma_addr_t paddr,
  195. qdf_dma_context_t memctx,
  196. const char *func, uint32_t line);
  197. #define qdf_mem_free_consistent(osdev, dev, size, vaddr, paddr, memctx) \
  198. qdf_mem_free_consistent_debug(osdev, dev, size, vaddr, paddr, memctx, \
  199. __func__, __LINE__)
  200. #else
  201. /**
  202. * qdf_mem_malloc() - allocation QDF memory
  203. * @size: Number of bytes of memory to allocate.
  204. *
  205. * This function will dynamicallly allocate the specified number of bytes of
  206. * memory.
  207. *
  208. * Return:
  209. * Upon successful allocate, returns a non-NULL pointer to the allocated
  210. * memory. If this function is unable to allocate the amount of memory
  211. * specified (for any reason) it returns NULL.
  212. */
  213. #define qdf_mem_malloc(size) \
  214. qdf_mem_malloc_fl(size, __func__, __LINE__)
  215. void *qdf_mem_malloc_fl(qdf_size_t size, const char *func, uint32_t line);
  216. /**
  217. * qdf_mem_malloc_atomic() - allocation QDF memory atomically
  218. * @size: Number of bytes of memory to allocate.
  219. *
  220. * This function will dynamicallly allocate the specified number of bytes of
  221. * memory.
  222. *
  223. * Return:
  224. * Upon successful allocate, returns a non-NULL pointer to the allocated
  225. * memory. If this function is unable to allocate the amount of memory
  226. * specified (for any reason) it returns NULL.
  227. */
  228. #define qdf_mem_malloc_atomic(size) \
  229. qdf_mem_malloc_atomic_fl(size, __func__, __LINE__)
  230. void *qdf_mem_malloc_atomic_fl(qdf_size_t size,
  231. const char *func,
  232. uint32_t line);
  233. /**
  234. * qdf_mem_free() - free QDF memory
  235. * @ptr: Pointer to the starting address of the memory to be freed.
  236. *
  237. * Return: None
  238. */
  239. void qdf_mem_free(void *ptr);
  240. static inline void qdf_mem_check_for_leaks(void) { }
  241. void *qdf_mem_alloc_consistent(qdf_device_t osdev, void *dev,
  242. qdf_size_t size, qdf_dma_addr_t *paddr);
  243. void qdf_mem_free_consistent(qdf_device_t osdev, void *dev,
  244. qdf_size_t size, void *vaddr,
  245. qdf_dma_addr_t paddr, qdf_dma_context_t memctx);
  246. void qdf_mem_multi_pages_alloc(qdf_device_t osdev,
  247. struct qdf_mem_multi_page_t *pages,
  248. size_t element_size, uint16_t element_num,
  249. qdf_dma_context_t memctxt, bool cacheable);
  250. void qdf_mem_multi_pages_free(qdf_device_t osdev,
  251. struct qdf_mem_multi_page_t *pages,
  252. qdf_dma_context_t memctxt, bool cacheable);
  253. #endif /* MEMORY_DEBUG */
  254. /**
  255. * qdf_aligned_malloc() - allocates aligned QDF memory.
  256. * @size: Size to be allocated
  257. * @vaddr_unaligned: Unaligned virtual address.
  258. * @paddr_unaligned: Unaligned physical address.
  259. * @paddr_aligned: Aligned physical address.
  260. * @align: Base address alignment.
  261. * @func: Function name of the call site.
  262. * @line: Line number of the call site.
  263. *
  264. * This function will dynamically allocate the specified number of bytes of
  265. * memory. Checks if the allocated base address is aligned with base_align.
  266. * If not, it frees the allocated memory, adds base_align to alloc size and
  267. * re-allocates the memory.
  268. *
  269. * Return:
  270. * Upon successful allocate, returns an aligned base address of the allocated
  271. * memory. If this function is unable to allocate the amount of memory
  272. * specified (for any reason) it returns NULL.
  273. */
  274. #define qdf_aligned_malloc(size, vaddr_unaligned, paddr_unaligned, \
  275. paddr_aligned, align) \
  276. qdf_aligned_malloc_fl(size, vaddr_unaligned, paddr_unaligned, \
  277. paddr_aligned, align, __func__, __LINE__)
  278. void *qdf_aligned_malloc_fl(uint32_t *size, void **vaddr_unaligned,
  279. qdf_dma_addr_t *paddr_unaligned,
  280. qdf_dma_addr_t *paddr_aligned,
  281. uint32_t align,
  282. const char *func, uint32_t line);
  283. /**
  284. * qdf_aligned_mem_alloc_consistent() - allocates consistent qdf memory
  285. * @osdev: OS device handle
  286. * @size: Size to be allocated
  287. * @vaddr_unaligned: Unaligned virtual address.
  288. * @paddr_unaligned: Unaligned physical address.
  289. * @paddr_aligned: Aligned physical address.
  290. * @align: Base address alignment.
  291. * @func: Function name of the call site.
  292. * @line: Line number of the call site.
  293. *
  294. * Return: pointer of allocated memory or null if memory alloc fails.
  295. */
  296. #define qdf_aligned_mem_alloc_consistent(osdev, size, vaddr_unaligned, \
  297. paddr_unaligned, paddr_aligned, \
  298. align) \
  299. qdf_aligned_mem_alloc_consistent_fl(osdev, size, vaddr_unaligned, \
  300. paddr_unaligned, paddr_aligned, \
  301. align, __func__, __LINE__)
  302. void *qdf_aligned_mem_alloc_consistent_fl(qdf_device_t osdev, uint32_t *size,
  303. void **vaddr_unaligned,
  304. qdf_dma_addr_t *paddr_unaligned,
  305. qdf_dma_addr_t *paddr_aligned,
  306. uint32_t align, const char *func,
  307. uint32_t line);
  308. #define qdf_mem_virt_to_phys(vaddr) virt_to_phys(vaddr)
  309. void qdf_mem_set_io(void *ptr, uint32_t num_bytes, uint32_t value);
  310. void qdf_mem_copy_toio(void *dst_addr, const void *src_addr,
  311. uint32_t num_bytes);
  312. /**
  313. * qdf_mem_set() - set (fill) memory with a specified byte value.
  314. * @ptr: Pointer to memory that will be set
  315. * @num_bytes: Number of bytes to be set
  316. * @value: Byte set in memory
  317. *
  318. * WARNING: parameter @num_bytes and @value are swapped comparing with
  319. * standard C function "memset", please ensure correct usage of this function!
  320. *
  321. * Return: None
  322. */
  323. void qdf_mem_set(void *ptr, uint32_t num_bytes, uint32_t value);
  324. /**
  325. * qdf_mem_zero() - zero out memory
  326. * @ptr: pointer to memory that will be set to zero
  327. * @num_bytes: number of bytes zero
  328. *
  329. * This function sets the memory location to all zeros, essentially clearing
  330. * the memory.
  331. *
  332. * Return: None
  333. */
  334. static inline void qdf_mem_zero(void *ptr, uint32_t num_bytes)
  335. {
  336. qdf_mem_set(ptr, num_bytes, 0);
  337. }
  338. /**
  339. * qdf_mem_copy() - copy memory
  340. * @dst_addr: Pointer to destination memory location (to copy to)
  341. * @src_addr: Pointer to source memory location (to copy from)
  342. * @num_bytes: Number of bytes to copy.
  343. *
  344. * Copy host memory from one location to another, similar to memcpy in
  345. * standard C. Note this function does not specifically handle overlapping
  346. * source and destination memory locations. Calling this function with
  347. * overlapping source and destination memory locations will result in
  348. * unpredictable results. Use qdf_mem_move() if the memory locations
  349. * for the source and destination are overlapping (or could be overlapping!)
  350. *
  351. * Return: none
  352. */
  353. void qdf_mem_copy(void *dst_addr, const void *src_addr, uint32_t num_bytes);
  354. /**
  355. * qdf_mem_move() - move memory
  356. * @dst_addr: pointer to destination memory location (to move to)
  357. * @src_addr: pointer to source memory location (to move from)
  358. * @num_bytes: number of bytes to move.
  359. *
  360. * Move host memory from one location to another, similar to memmove in
  361. * standard C. Note this function *does* handle overlapping
  362. * source and destination memory locations.
  363. * Return: None
  364. */
  365. void qdf_mem_move(void *dst_addr, const void *src_addr, uint32_t num_bytes);
  366. /**
  367. * qdf_mem_cmp() - memory compare
  368. * @left: pointer to one location in memory to compare
  369. * @right: pointer to second location in memory to compare
  370. * @size: the number of bytes to compare
  371. *
  372. * Function to compare two pieces of memory, similar to memcmp function
  373. * in standard C.
  374. *
  375. * Return:
  376. * 0 -- equal
  377. * < 0 -- *memory1 is less than *memory2
  378. * > 0 -- *memory1 is bigger than *memory2
  379. */
  380. int qdf_mem_cmp(const void *left, const void *right, size_t size);
  381. void qdf_ether_addr_copy(void *dst_addr, const void *src_addr);
  382. /**
  383. * qdf_mem_map_nbytes_single - Map memory for DMA
  384. * @osdev: pomter OS device context
  385. * @buf: pointer to memory to be dma mapped
  386. * @dir: DMA map direction
  387. * @nbytes: number of bytes to be mapped.
  388. * @phy_addr: ponter to recive physical address.
  389. *
  390. * Return: success/failure
  391. */
  392. static inline uint32_t qdf_mem_map_nbytes_single(qdf_device_t osdev, void *buf,
  393. qdf_dma_dir_t dir, int nbytes,
  394. qdf_dma_addr_t *phy_addr)
  395. {
  396. #if defined(HIF_PCI)
  397. return __qdf_mem_map_nbytes_single(osdev, buf, dir, nbytes, phy_addr);
  398. #else
  399. return 0;
  400. #endif
  401. }
  402. static inline void qdf_mem_dma_cache_sync(qdf_device_t osdev,
  403. qdf_dma_addr_t buf,
  404. qdf_dma_dir_t dir,
  405. int nbytes)
  406. {
  407. __qdf_mem_dma_cache_sync(osdev, buf, dir, nbytes);
  408. }
  409. /**
  410. * qdf_mem_unmap_nbytes_single() - un_map memory for DMA
  411. * @osdev: pomter OS device context
  412. * @phy_addr: physical address of memory to be dma unmapped
  413. * @dir: DMA unmap direction
  414. * @nbytes: number of bytes to be unmapped.
  415. *
  416. * Return: none
  417. */
  418. static inline void qdf_mem_unmap_nbytes_single(qdf_device_t osdev,
  419. qdf_dma_addr_t phy_addr,
  420. qdf_dma_dir_t dir,
  421. int nbytes)
  422. {
  423. #if defined(HIF_PCI)
  424. __qdf_mem_unmap_nbytes_single(osdev, phy_addr, dir, nbytes);
  425. #endif
  426. }
  427. /**
  428. * qdf_mempool_init - Create and initialize memory pool
  429. * @osdev: platform device object
  430. * @pool_addr: address of the pool created
  431. * @elem_cnt: no. of elements in pool
  432. * @elem_size: size of each pool element in bytes
  433. * @flags: flags
  434. * Return: Handle to memory pool or NULL if allocation failed
  435. */
  436. static inline int qdf_mempool_init(qdf_device_t osdev,
  437. qdf_mempool_t *pool_addr, int elem_cnt,
  438. size_t elem_size, uint32_t flags)
  439. {
  440. return __qdf_mempool_init(osdev, pool_addr, elem_cnt, elem_size,
  441. flags);
  442. }
  443. /**
  444. * qdf_mempool_destroy - Destroy memory pool
  445. * @osdev: platform device object
  446. * @Handle: to memory pool
  447. * Return: none
  448. */
  449. static inline void qdf_mempool_destroy(qdf_device_t osdev, qdf_mempool_t pool)
  450. {
  451. __qdf_mempool_destroy(osdev, pool);
  452. }
  453. /**
  454. * qdf_mempool_alloc - Allocate an element memory pool
  455. * @osdev: platform device object
  456. * @Handle: to memory pool
  457. * Return: Pointer to the allocated element or NULL if the pool is empty
  458. */
  459. static inline void *qdf_mempool_alloc(qdf_device_t osdev, qdf_mempool_t pool)
  460. {
  461. return (void *)__qdf_mempool_alloc(osdev, pool);
  462. }
  463. /**
  464. * qdf_mempool_free - Free a memory pool element
  465. * @osdev: Platform device object
  466. * @pool: Handle to memory pool
  467. * @buf: Element to be freed
  468. * Return: none
  469. */
  470. static inline void qdf_mempool_free(qdf_device_t osdev, qdf_mempool_t pool,
  471. void *buf)
  472. {
  473. __qdf_mempool_free(osdev, pool, buf);
  474. }
  475. void qdf_mem_dma_sync_single_for_device(qdf_device_t osdev,
  476. qdf_dma_addr_t bus_addr,
  477. qdf_size_t size,
  478. __dma_data_direction direction);
  479. void qdf_mem_dma_sync_single_for_cpu(qdf_device_t osdev,
  480. qdf_dma_addr_t bus_addr,
  481. qdf_size_t size,
  482. __dma_data_direction direction);
  483. int qdf_mem_multi_page_link(qdf_device_t osdev,
  484. struct qdf_mem_multi_page_t *pages,
  485. uint32_t elem_size, uint32_t elem_count, uint8_t cacheable);
  486. #ifdef WLAN_DEBUGFS
  487. /**
  488. * qdf_mem_kmalloc_inc() - increment kmalloc allocated bytes count
  489. * @size: number of bytes to increment by
  490. *
  491. * Return: None
  492. */
  493. void qdf_mem_kmalloc_inc(qdf_size_t size);
  494. /**
  495. * qdf_mem_kmalloc_dec() - decrement kmalloc allocated bytes count
  496. * @size: number of bytes to decrement by
  497. *
  498. * Return: None
  499. */
  500. void qdf_mem_kmalloc_dec(qdf_size_t size);
  501. #else
  502. static inline void qdf_mem_kmalloc_inc(qdf_size_t size) { }
  503. static inline void qdf_mem_kmalloc_dec(qdf_size_t size) { }
  504. #endif /* WLAN_DEBUGFS */
  505. /**
  506. * qdf_mem_skb_inc() - increment total skb allocation size
  507. * @size: size to be added
  508. *
  509. * Return: none
  510. */
  511. void qdf_mem_skb_inc(qdf_size_t size);
  512. /**
  513. * qdf_mem_skb_dec() - decrement total skb allocation size
  514. * @size: size to be decremented
  515. *
  516. * Return: none
  517. */
  518. void qdf_mem_skb_dec(qdf_size_t size);
  519. /**
  520. * qdf_mem_map_table_alloc() - Allocate shared memory info structure
  521. * @num: number of required storage
  522. *
  523. * Allocate mapping table for DMA memory allocation. This is needed for
  524. * IPA-WLAN buffer sharing when SMMU Stage1 Translation is enabled.
  525. *
  526. * Return: shared memory info storage table pointer
  527. */
  528. static inline qdf_mem_info_t *qdf_mem_map_table_alloc(uint32_t num)
  529. {
  530. qdf_mem_info_t *mem_info_arr;
  531. mem_info_arr = qdf_mem_malloc(num * sizeof(mem_info_arr[0]));
  532. return mem_info_arr;
  533. }
  534. /**
  535. * qdf_update_mem_map_table() - Update DMA memory map info
  536. * @osdev: Parent device instance
  537. * @mem_info: Pointer to shared memory information
  538. * @dma_addr: dma address
  539. * @mem_size: memory size allocated
  540. *
  541. * Store DMA shared memory information
  542. *
  543. * Return: none
  544. */
  545. static inline void qdf_update_mem_map_table(qdf_device_t osdev,
  546. qdf_mem_info_t *mem_info,
  547. qdf_dma_addr_t dma_addr,
  548. uint32_t mem_size)
  549. {
  550. if (!mem_info) {
  551. __qdf_print("%s: NULL mem_info\n", __func__);
  552. return;
  553. }
  554. __qdf_update_mem_map_table(osdev, mem_info, dma_addr, mem_size);
  555. }
  556. /**
  557. * qdf_mem_smmu_s1_enabled() - Return SMMU stage 1 translation enable status
  558. * @osdev parent device instance
  559. *
  560. * Return: true if smmu s1 enabled, false if smmu s1 is bypassed
  561. */
  562. static inline bool qdf_mem_smmu_s1_enabled(qdf_device_t osdev)
  563. {
  564. return __qdf_mem_smmu_s1_enabled(osdev);
  565. }
  566. /**
  567. * qdf_mem_paddr_from_dmaaddr() - get actual physical address from dma address
  568. * @osdev: Parent device instance
  569. * @dma_addr: DMA/IOVA address
  570. *
  571. * Get actual physical address from dma_addr based on SMMU enablement status.
  572. * IF SMMU Stage 1 tranlation is enabled, DMA APIs return IO virtual address
  573. * (IOVA) otherwise returns physical address. So get SMMU physical address
  574. * mapping from IOVA.
  575. *
  576. * Return: dmaable physical address
  577. */
  578. static inline qdf_dma_addr_t qdf_mem_paddr_from_dmaaddr(qdf_device_t osdev,
  579. qdf_dma_addr_t dma_addr)
  580. {
  581. return __qdf_mem_paddr_from_dmaaddr(osdev, dma_addr);
  582. }
  583. /**
  584. * qdf_mem_dma_get_sgtable() - Returns DMA memory scatter gather table
  585. * @dev: device instace
  586. * @sgt: scatter gather table pointer
  587. * @cpu_addr: HLOS virtual address
  588. * @dma_addr: dma address
  589. * @size: allocated memory size
  590. *
  591. * Return: physical address
  592. */
  593. static inline int
  594. qdf_mem_dma_get_sgtable(struct device *dev, void *sgt, void *cpu_addr,
  595. qdf_dma_addr_t dma_addr, size_t size)
  596. {
  597. return __qdf_os_mem_dma_get_sgtable(dev, sgt, cpu_addr, dma_addr, size);
  598. }
  599. /**
  600. * qdf_mem_free_sgtable() - Free a previously allocated sg table
  601. * @sgt: the mapped sg table header
  602. *
  603. * Return: None
  604. */
  605. static inline void
  606. qdf_mem_free_sgtable(struct sg_table *sgt)
  607. {
  608. __qdf_os_mem_free_sgtable(sgt);
  609. }
  610. /**
  611. * qdf_dma_get_sgtable_dma_addr() - Assigns DMA address to scatterlist elements
  612. * @sgt: scatter gather table pointer
  613. *
  614. * Return: None
  615. */
  616. static inline void
  617. qdf_dma_get_sgtable_dma_addr(struct sg_table *sgt)
  618. {
  619. __qdf_dma_get_sgtable_dma_addr(sgt);
  620. }
  621. /**
  622. * qdf_mem_get_dma_addr() - Return dma address based on SMMU translation status.
  623. * @osdev: Parent device instance
  624. * @mem_info: Pointer to allocated memory information
  625. *
  626. * Get dma address based on SMMU enablement status. If SMMU Stage 1
  627. * tranlation is enabled, DMA APIs return IO virtual address otherwise
  628. * returns physical address.
  629. *
  630. * Return: dma address
  631. */
  632. static inline qdf_dma_addr_t qdf_mem_get_dma_addr(qdf_device_t osdev,
  633. qdf_mem_info_t *mem_info)
  634. {
  635. return __qdf_mem_get_dma_addr(osdev, mem_info);
  636. }
  637. /**
  638. * qdf_mem_get_dma_addr_ptr() - Return DMA address pointer from mem info struct
  639. * @osdev: Parent device instance
  640. * @mem_info: Pointer to allocated memory information
  641. *
  642. * Based on smmu stage 1 translation enablement, return corresponding dma
  643. * address storage pointer.
  644. *
  645. * Return: dma address storage pointer
  646. */
  647. static inline qdf_dma_addr_t *qdf_mem_get_dma_addr_ptr(qdf_device_t osdev,
  648. qdf_mem_info_t *mem_info)
  649. {
  650. return __qdf_mem_get_dma_addr_ptr(osdev, mem_info);
  651. }
  652. /**
  653. * qdf_mem_get_dma_size() - Return DMA memory size
  654. * @osdev: parent device instance
  655. * @mem_info: Pointer to allocated memory information
  656. *
  657. * Return: DMA memory size
  658. */
  659. static inline uint32_t
  660. qdf_mem_get_dma_size(qdf_device_t osdev,
  661. qdf_mem_info_t *mem_info)
  662. {
  663. return __qdf_mem_get_dma_size(osdev, mem_info);
  664. }
  665. /**
  666. * qdf_mem_set_dma_size() - Set DMA memory size
  667. * @osdev: parent device instance
  668. * @mem_info: Pointer to allocated memory information
  669. * @mem_size: memory size allocated
  670. *
  671. * Return: none
  672. */
  673. static inline void
  674. qdf_mem_set_dma_size(qdf_device_t osdev,
  675. qdf_mem_info_t *mem_info,
  676. uint32_t mem_size)
  677. {
  678. __qdf_mem_set_dma_size(osdev, mem_info, mem_size);
  679. }
  680. /**
  681. * qdf_mem_get_dma_size() - Return DMA physical address
  682. * @osdev: parent device instance
  683. * @mem_info: Pointer to allocated memory information
  684. *
  685. * Return: DMA physical address
  686. */
  687. static inline qdf_dma_addr_t
  688. qdf_mem_get_dma_pa(qdf_device_t osdev,
  689. qdf_mem_info_t *mem_info)
  690. {
  691. return __qdf_mem_get_dma_pa(osdev, mem_info);
  692. }
  693. /**
  694. * qdf_mem_set_dma_size() - Set DMA physical address
  695. * @osdev: parent device instance
  696. * @mem_info: Pointer to allocated memory information
  697. * @dma_pa: DMA phsical address
  698. *
  699. * Return: none
  700. */
  701. static inline void
  702. qdf_mem_set_dma_pa(qdf_device_t osdev,
  703. qdf_mem_info_t *mem_info,
  704. qdf_dma_addr_t dma_pa)
  705. {
  706. __qdf_mem_set_dma_pa(osdev, mem_info, dma_pa);
  707. }
  708. /**
  709. * qdf_mem_shared_mem_alloc() - Allocate DMA memory for shared resource
  710. * @osdev: parent device instance
  711. * @mem_info: Pointer to allocated memory information
  712. * @size: size to be allocated
  713. *
  714. * Allocate DMA memory which will be shared with external kernel module. This
  715. * information is needed for SMMU mapping.
  716. *
  717. * Return: 0 success
  718. */
  719. qdf_shared_mem_t *qdf_mem_shared_mem_alloc(qdf_device_t osdev, uint32_t size);
  720. /**
  721. * qdf_mem_shared_mem_free() - Free shared memory
  722. * @osdev: parent device instance
  723. * @shared_mem: shared memory information storage
  724. *
  725. * Free DMA shared memory resource
  726. *
  727. * Return: None
  728. */
  729. static inline void qdf_mem_shared_mem_free(qdf_device_t osdev,
  730. qdf_shared_mem_t *shared_mem)
  731. {
  732. if (!shared_mem) {
  733. __qdf_print("%s: NULL shared mem struct passed\n",
  734. __func__);
  735. return;
  736. }
  737. if (shared_mem->vaddr) {
  738. qdf_mem_free_consistent(osdev, osdev->dev,
  739. qdf_mem_get_dma_size(osdev,
  740. &shared_mem->mem_info),
  741. shared_mem->vaddr,
  742. qdf_mem_get_dma_addr(osdev,
  743. &shared_mem->mem_info),
  744. qdf_get_dma_mem_context(shared_mem,
  745. memctx));
  746. }
  747. qdf_mem_free_sgtable(&shared_mem->sgtable);
  748. qdf_mem_free(shared_mem);
  749. }
  750. #endif /* __QDF_MEMORY_H */