qdf_mem.h 23 KB

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