qdf_mem.h 23 KB

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