qdf_mem.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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: Number of bytes of memory to allocate.
  257. * @ring_base_align: Base address alignment.
  258. * @vaddr_unaligned: Unaligned virtual address.
  259. * @func: Function name of the call site.
  260. * @line: Line number of the call site.
  261. *
  262. * This function will dynamically allocate the specified number of bytes of
  263. * memory. Checks if the allocated base address is aligned with base_align.
  264. * If not, it frees the allocated memory, adds base_align to alloc size and
  265. * re-allocates the memory.
  266. *
  267. * Return:
  268. * Upon successful allocate, returns an aligned base address of the allocated
  269. * memory. If this function is unable to allocate the amount of memory
  270. * specified (for any reason) it returns NULL.
  271. */
  272. #define qdf_aligned_malloc(size, ring_base_align, vaddr_unaligned) \
  273. qdf_aligned_malloc_fl(size, ring_base_align, vaddr_unaligned, \
  274. __func__, __LINE__)
  275. void *qdf_aligned_malloc_fl(qdf_size_t size, uint32_t ring_base_align,
  276. void **vaddr_unaligned,
  277. const char *func, uint32_t line);
  278. /**
  279. * qdf_aligned_mem_alloc_consistent() - allocates consistent qdf memory
  280. * @osdev: OS device handle
  281. * @dev: Pointer to device handle
  282. * @size: Size to be allocated
  283. * @vaddr_unaligned: Unaligned virtual address.
  284. * @paddr_unaligned: Unaligned physical address.
  285. * @paddr_aligned: Aligned physical address.
  286. * @ring_base_align: Base address alignment.
  287. * @func: Function name of the call site.
  288. * @line: Line number of the call site.
  289. *
  290. * Return: pointer of allocated memory or null if memory alloc fails.
  291. */
  292. #define qdf_aligned_mem_alloc_consistent(osdev, dev, size, vaddr_unaligned, \
  293. paddr_unaligned, paddr_aligned, ring_base_align) \
  294. qdf_aligned_mem_alloc_consistent_fl(osdev, dev, size, vaddr_unaligned, \
  295. paddr_unaligned, paddr_aligned, \
  296. ring_base_align, __func__, __LINE__)
  297. void *qdf_aligned_mem_alloc_consistent_fl(
  298. qdf_device_t osdev, void *dev, qdf_size_t size,
  299. void **vaddr_unaligned, qdf_dma_addr_t *paddr_unaligned,
  300. qdf_dma_addr_t *paddr_aligned, uint32_t ring_base_align,
  301. const char *func, uint32_t line);
  302. #define qdf_mem_virt_to_phys(vaddr) virt_to_phys(vaddr)
  303. void qdf_mem_set_io(void *ptr, uint32_t num_bytes, uint32_t value);
  304. void qdf_mem_copy_toio(void *dst_addr, const void *src_addr,
  305. uint32_t num_bytes);
  306. /**
  307. * qdf_mem_set() - set (fill) memory with a specified byte value.
  308. * @ptr: Pointer to memory that will be set
  309. * @num_bytes: Number of bytes to be set
  310. * @value: Byte set in memory
  311. *
  312. * WARNING: parameter @num_bytes and @value are swapped comparing with
  313. * standard C function "memset", please ensure correct usage of this function!
  314. *
  315. * Return: None
  316. */
  317. void qdf_mem_set(void *ptr, uint32_t num_bytes, uint32_t value);
  318. /**
  319. * qdf_mem_zero() - zero out memory
  320. * @ptr: pointer to memory that will be set to zero
  321. * @num_bytes: number of bytes zero
  322. *
  323. * This function sets the memory location to all zeros, essentially clearing
  324. * the memory.
  325. *
  326. * Return: None
  327. */
  328. static inline void qdf_mem_zero(void *ptr, uint32_t num_bytes)
  329. {
  330. qdf_mem_set(ptr, num_bytes, 0);
  331. }
  332. /**
  333. * qdf_mem_copy() - copy memory
  334. * @dst_addr: Pointer to destination memory location (to copy to)
  335. * @src_addr: Pointer to source memory location (to copy from)
  336. * @num_bytes: Number of bytes to copy.
  337. *
  338. * Copy host memory from one location to another, similar to memcpy in
  339. * standard C. Note this function does not specifically handle overlapping
  340. * source and destination memory locations. Calling this function with
  341. * overlapping source and destination memory locations will result in
  342. * unpredictable results. Use qdf_mem_move() if the memory locations
  343. * for the source and destination are overlapping (or could be overlapping!)
  344. *
  345. * Return: none
  346. */
  347. void qdf_mem_copy(void *dst_addr, const void *src_addr, uint32_t num_bytes);
  348. /**
  349. * qdf_mem_move() - move memory
  350. * @dst_addr: pointer to destination memory location (to move to)
  351. * @src_addr: pointer to source memory location (to move from)
  352. * @num_bytes: number of bytes to move.
  353. *
  354. * Move host memory from one location to another, similar to memmove in
  355. * standard C. Note this function *does* handle overlapping
  356. * source and destination memory locations.
  357. * Return: None
  358. */
  359. void qdf_mem_move(void *dst_addr, const void *src_addr, uint32_t num_bytes);
  360. /**
  361. * qdf_mem_cmp() - memory compare
  362. * @left: pointer to one location in memory to compare
  363. * @right: pointer to second location in memory to compare
  364. * @size: the number of bytes to compare
  365. *
  366. * Function to compare two pieces of memory, similar to memcmp function
  367. * in standard C.
  368. *
  369. * Return:
  370. * 0 -- equal
  371. * < 0 -- *memory1 is less than *memory2
  372. * > 0 -- *memory1 is bigger than *memory2
  373. */
  374. int qdf_mem_cmp(const void *left, const void *right, size_t size);
  375. void qdf_ether_addr_copy(void *dst_addr, const void *src_addr);
  376. /**
  377. * qdf_mem_map_nbytes_single - Map memory for DMA
  378. * @osdev: pomter OS device context
  379. * @buf: pointer to memory to be dma mapped
  380. * @dir: DMA map direction
  381. * @nbytes: number of bytes to be mapped.
  382. * @phy_addr: ponter to recive physical address.
  383. *
  384. * Return: success/failure
  385. */
  386. static inline uint32_t qdf_mem_map_nbytes_single(qdf_device_t osdev, void *buf,
  387. qdf_dma_dir_t dir, int nbytes,
  388. qdf_dma_addr_t *phy_addr)
  389. {
  390. #if defined(HIF_PCI)
  391. return __qdf_mem_map_nbytes_single(osdev, buf, dir, nbytes, phy_addr);
  392. #else
  393. return 0;
  394. #endif
  395. }
  396. static inline void qdf_mem_dma_cache_sync(qdf_device_t osdev,
  397. qdf_dma_addr_t buf,
  398. qdf_dma_dir_t dir,
  399. int nbytes)
  400. {
  401. __qdf_mem_dma_cache_sync(osdev, buf, dir, nbytes);
  402. }
  403. /**
  404. * qdf_mem_unmap_nbytes_single() - un_map memory for DMA
  405. * @osdev: pomter OS device context
  406. * @phy_addr: physical address of memory to be dma unmapped
  407. * @dir: DMA unmap direction
  408. * @nbytes: number of bytes to be unmapped.
  409. *
  410. * Return: none
  411. */
  412. static inline void qdf_mem_unmap_nbytes_single(qdf_device_t osdev,
  413. qdf_dma_addr_t phy_addr,
  414. qdf_dma_dir_t dir,
  415. int nbytes)
  416. {
  417. #if defined(HIF_PCI)
  418. __qdf_mem_unmap_nbytes_single(osdev, phy_addr, dir, nbytes);
  419. #endif
  420. }
  421. /**
  422. * qdf_mempool_init - Create and initialize memory pool
  423. * @osdev: platform device object
  424. * @pool_addr: address of the pool created
  425. * @elem_cnt: no. of elements in pool
  426. * @elem_size: size of each pool element in bytes
  427. * @flags: flags
  428. * Return: Handle to memory pool or NULL if allocation failed
  429. */
  430. static inline int qdf_mempool_init(qdf_device_t osdev,
  431. qdf_mempool_t *pool_addr, int elem_cnt,
  432. size_t elem_size, uint32_t flags)
  433. {
  434. return __qdf_mempool_init(osdev, pool_addr, elem_cnt, elem_size,
  435. flags);
  436. }
  437. /**
  438. * qdf_mempool_destroy - Destroy memory pool
  439. * @osdev: platform device object
  440. * @Handle: to memory pool
  441. * Return: none
  442. */
  443. static inline void qdf_mempool_destroy(qdf_device_t osdev, qdf_mempool_t pool)
  444. {
  445. __qdf_mempool_destroy(osdev, pool);
  446. }
  447. /**
  448. * qdf_mempool_alloc - Allocate an element memory pool
  449. * @osdev: platform device object
  450. * @Handle: to memory pool
  451. * Return: Pointer to the allocated element or NULL if the pool is empty
  452. */
  453. static inline void *qdf_mempool_alloc(qdf_device_t osdev, qdf_mempool_t pool)
  454. {
  455. return (void *)__qdf_mempool_alloc(osdev, pool);
  456. }
  457. /**
  458. * qdf_mempool_free - Free a memory pool element
  459. * @osdev: Platform device object
  460. * @pool: Handle to memory pool
  461. * @buf: Element to be freed
  462. * Return: none
  463. */
  464. static inline void qdf_mempool_free(qdf_device_t osdev, qdf_mempool_t pool,
  465. void *buf)
  466. {
  467. __qdf_mempool_free(osdev, pool, buf);
  468. }
  469. void qdf_mem_dma_sync_single_for_device(qdf_device_t osdev,
  470. qdf_dma_addr_t bus_addr,
  471. qdf_size_t size,
  472. __dma_data_direction direction);
  473. void qdf_mem_dma_sync_single_for_cpu(qdf_device_t osdev,
  474. qdf_dma_addr_t bus_addr,
  475. qdf_size_t size,
  476. __dma_data_direction direction);
  477. int qdf_mem_multi_page_link(qdf_device_t osdev,
  478. struct qdf_mem_multi_page_t *pages,
  479. uint32_t elem_size, uint32_t elem_count, uint8_t cacheable);
  480. #ifdef WLAN_DEBUGFS
  481. /**
  482. * qdf_mem_kmalloc_inc() - increment kmalloc allocated bytes count
  483. * @size: number of bytes to increment by
  484. *
  485. * Return: None
  486. */
  487. void qdf_mem_kmalloc_inc(qdf_size_t size);
  488. /**
  489. * qdf_mem_kmalloc_dec() - decrement kmalloc allocated bytes count
  490. * @size: number of bytes to decrement by
  491. *
  492. * Return: None
  493. */
  494. void qdf_mem_kmalloc_dec(qdf_size_t size);
  495. #else
  496. static inline void qdf_mem_kmalloc_inc(qdf_size_t size) { }
  497. static inline void qdf_mem_kmalloc_dec(qdf_size_t size) { }
  498. #endif /* WLAN_DEBUGFS */
  499. /**
  500. * qdf_mem_skb_inc() - increment total skb allocation size
  501. * @size: size to be added
  502. *
  503. * Return: none
  504. */
  505. void qdf_mem_skb_inc(qdf_size_t size);
  506. /**
  507. * qdf_mem_skb_dec() - decrement total skb allocation size
  508. * @size: size to be decremented
  509. *
  510. * Return: none
  511. */
  512. void qdf_mem_skb_dec(qdf_size_t size);
  513. /**
  514. * qdf_mem_map_table_alloc() - Allocate shared memory info structure
  515. * @num: number of required storage
  516. *
  517. * Allocate mapping table for DMA memory allocation. This is needed for
  518. * IPA-WLAN buffer sharing when SMMU Stage1 Translation is enabled.
  519. *
  520. * Return: shared memory info storage table pointer
  521. */
  522. static inline qdf_mem_info_t *qdf_mem_map_table_alloc(uint32_t num)
  523. {
  524. qdf_mem_info_t *mem_info_arr;
  525. mem_info_arr = qdf_mem_malloc(num * sizeof(mem_info_arr[0]));
  526. return mem_info_arr;
  527. }
  528. /**
  529. * qdf_update_mem_map_table() - Update DMA memory map info
  530. * @osdev: Parent device instance
  531. * @mem_info: Pointer to shared memory information
  532. * @dma_addr: dma address
  533. * @mem_size: memory size allocated
  534. *
  535. * Store DMA shared memory information
  536. *
  537. * Return: none
  538. */
  539. static inline void qdf_update_mem_map_table(qdf_device_t osdev,
  540. qdf_mem_info_t *mem_info,
  541. qdf_dma_addr_t dma_addr,
  542. uint32_t mem_size)
  543. {
  544. if (!mem_info) {
  545. __qdf_print("%s: NULL mem_info\n", __func__);
  546. return;
  547. }
  548. __qdf_update_mem_map_table(osdev, mem_info, dma_addr, mem_size);
  549. }
  550. /**
  551. * qdf_mem_smmu_s1_enabled() - Return SMMU stage 1 translation enable status
  552. * @osdev parent device instance
  553. *
  554. * Return: true if smmu s1 enabled, false if smmu s1 is bypassed
  555. */
  556. static inline bool qdf_mem_smmu_s1_enabled(qdf_device_t osdev)
  557. {
  558. return __qdf_mem_smmu_s1_enabled(osdev);
  559. }
  560. /**
  561. * qdf_mem_paddr_from_dmaaddr() - get actual physical address from dma address
  562. * @osdev: Parent device instance
  563. * @dma_addr: DMA/IOVA address
  564. *
  565. * Get actual physical address from dma_addr based on SMMU enablement status.
  566. * IF SMMU Stage 1 tranlation is enabled, DMA APIs return IO virtual address
  567. * (IOVA) otherwise returns physical address. So get SMMU physical address
  568. * mapping from IOVA.
  569. *
  570. * Return: dmaable physical address
  571. */
  572. static inline qdf_dma_addr_t qdf_mem_paddr_from_dmaaddr(qdf_device_t osdev,
  573. qdf_dma_addr_t dma_addr)
  574. {
  575. return __qdf_mem_paddr_from_dmaaddr(osdev, dma_addr);
  576. }
  577. /**
  578. * qdf_mem_dma_get_sgtable() - Returns DMA memory scatter gather table
  579. * @dev: device instace
  580. * @sgt: scatter gather table pointer
  581. * @cpu_addr: HLOS virtual address
  582. * @dma_addr: dma address
  583. * @size: allocated memory size
  584. *
  585. * Return: physical address
  586. */
  587. static inline int
  588. qdf_mem_dma_get_sgtable(struct device *dev, void *sgt, void *cpu_addr,
  589. qdf_dma_addr_t dma_addr, size_t size)
  590. {
  591. return __qdf_os_mem_dma_get_sgtable(dev, sgt, cpu_addr, dma_addr, size);
  592. }
  593. /**
  594. * qdf_mem_free_sgtable() - Free a previously allocated sg table
  595. * @sgt: the mapped sg table header
  596. *
  597. * Return: None
  598. */
  599. static inline void
  600. qdf_mem_free_sgtable(struct sg_table *sgt)
  601. {
  602. __qdf_os_mem_free_sgtable(sgt);
  603. }
  604. /**
  605. * qdf_dma_get_sgtable_dma_addr() - Assigns DMA address to scatterlist elements
  606. * @sgt: scatter gather table pointer
  607. *
  608. * Return: None
  609. */
  610. static inline void
  611. qdf_dma_get_sgtable_dma_addr(struct sg_table *sgt)
  612. {
  613. __qdf_dma_get_sgtable_dma_addr(sgt);
  614. }
  615. /**
  616. * qdf_mem_get_dma_addr() - Return dma address based on SMMU translation status.
  617. * @osdev: Parent device instance
  618. * @mem_info: Pointer to allocated memory information
  619. *
  620. * Get dma address based on SMMU enablement status. If SMMU Stage 1
  621. * tranlation is enabled, DMA APIs return IO virtual address otherwise
  622. * returns physical address.
  623. *
  624. * Return: dma address
  625. */
  626. static inline qdf_dma_addr_t qdf_mem_get_dma_addr(qdf_device_t osdev,
  627. qdf_mem_info_t *mem_info)
  628. {
  629. return __qdf_mem_get_dma_addr(osdev, mem_info);
  630. }
  631. /**
  632. * qdf_mem_get_dma_addr_ptr() - Return DMA address pointer from mem info struct
  633. * @osdev: Parent device instance
  634. * @mem_info: Pointer to allocated memory information
  635. *
  636. * Based on smmu stage 1 translation enablement, return corresponding dma
  637. * address storage pointer.
  638. *
  639. * Return: dma address storage pointer
  640. */
  641. static inline qdf_dma_addr_t *qdf_mem_get_dma_addr_ptr(qdf_device_t osdev,
  642. qdf_mem_info_t *mem_info)
  643. {
  644. return __qdf_mem_get_dma_addr_ptr(osdev, mem_info);
  645. }
  646. /**
  647. * qdf_mem_get_dma_size() - Return DMA memory size
  648. * @osdev: parent device instance
  649. * @mem_info: Pointer to allocated memory information
  650. *
  651. * Return: DMA memory size
  652. */
  653. static inline uint32_t
  654. qdf_mem_get_dma_size(qdf_device_t osdev,
  655. qdf_mem_info_t *mem_info)
  656. {
  657. return __qdf_mem_get_dma_size(osdev, mem_info);
  658. }
  659. /**
  660. * qdf_mem_set_dma_size() - Set DMA memory size
  661. * @osdev: parent device instance
  662. * @mem_info: Pointer to allocated memory information
  663. * @mem_size: memory size allocated
  664. *
  665. * Return: none
  666. */
  667. static inline void
  668. qdf_mem_set_dma_size(qdf_device_t osdev,
  669. qdf_mem_info_t *mem_info,
  670. uint32_t mem_size)
  671. {
  672. __qdf_mem_set_dma_size(osdev, mem_info, mem_size);
  673. }
  674. /**
  675. * qdf_mem_get_dma_size() - Return DMA physical address
  676. * @osdev: parent device instance
  677. * @mem_info: Pointer to allocated memory information
  678. *
  679. * Return: DMA physical address
  680. */
  681. static inline qdf_dma_addr_t
  682. qdf_mem_get_dma_pa(qdf_device_t osdev,
  683. qdf_mem_info_t *mem_info)
  684. {
  685. return __qdf_mem_get_dma_pa(osdev, mem_info);
  686. }
  687. /**
  688. * qdf_mem_set_dma_size() - Set DMA physical address
  689. * @osdev: parent device instance
  690. * @mem_info: Pointer to allocated memory information
  691. * @dma_pa: DMA phsical address
  692. *
  693. * Return: none
  694. */
  695. static inline void
  696. qdf_mem_set_dma_pa(qdf_device_t osdev,
  697. qdf_mem_info_t *mem_info,
  698. qdf_dma_addr_t dma_pa)
  699. {
  700. __qdf_mem_set_dma_pa(osdev, mem_info, dma_pa);
  701. }
  702. /**
  703. * qdf_mem_shared_mem_alloc() - Allocate DMA memory for shared resource
  704. * @osdev: parent device instance
  705. * @mem_info: Pointer to allocated memory information
  706. * @size: size to be allocated
  707. *
  708. * Allocate DMA memory which will be shared with external kernel module. This
  709. * information is needed for SMMU mapping.
  710. *
  711. * Return: 0 success
  712. */
  713. qdf_shared_mem_t *qdf_mem_shared_mem_alloc(qdf_device_t osdev, uint32_t size);
  714. /**
  715. * qdf_mem_shared_mem_free() - Free shared memory
  716. * @osdev: parent device instance
  717. * @shared_mem: shared memory information storage
  718. *
  719. * Free DMA shared memory resource
  720. *
  721. * Return: None
  722. */
  723. static inline void qdf_mem_shared_mem_free(qdf_device_t osdev,
  724. qdf_shared_mem_t *shared_mem)
  725. {
  726. if (!shared_mem) {
  727. __qdf_print("%s: NULL shared mem struct passed\n",
  728. __func__);
  729. return;
  730. }
  731. if (shared_mem->vaddr) {
  732. qdf_mem_free_consistent(osdev, osdev->dev,
  733. qdf_mem_get_dma_size(osdev,
  734. &shared_mem->mem_info),
  735. shared_mem->vaddr,
  736. qdf_mem_get_dma_addr(osdev,
  737. &shared_mem->mem_info),
  738. qdf_get_dma_mem_context(shared_mem,
  739. memctx));
  740. }
  741. qdf_mem_free_sgtable(&shared_mem->sgtable);
  742. qdf_mem_free(shared_mem);
  743. }
  744. #endif /* __QDF_MEMORY_H */