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