qdf_mem.h 24 KB

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