qdf_mem.h 25 KB

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