qdf_mem.h 23 KB

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