qdf_mem.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 2014-2016 The Linux Foundation. All rights reserved.
  3. *
  4. * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
  5. *
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for
  8. * any purpose with or without fee is hereby granted, provided that the
  9. * above copyright notice and this permission notice appear in all
  10. * copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  13. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  15. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  16. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  17. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  18. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  19. * PERFORMANCE OF THIS SOFTWARE.
  20. */
  21. /*
  22. * This file was originally distributed by Qualcomm Atheros, Inc.
  23. * under proprietary terms before Copyright ownership was assigned
  24. * to the Linux Foundation.
  25. */
  26. /**
  27. * DOC: qdf_mem
  28. * QCA driver framework (QDF) memory management APIs
  29. */
  30. #if !defined(__QDF_MEMORY_H)
  31. #define __QDF_MEMORY_H
  32. /* Include Files */
  33. #include <qdf_types.h>
  34. #include <i_qdf_mem.h>
  35. /**
  36. * struct qdf_mem_dma_page_t - Allocated dmaable page
  37. * @page_v_addr_start: Page start virtual address
  38. * @page_v_addr_end: Page end virtual address
  39. * @page_p_addr: Page start physical address
  40. */
  41. struct qdf_mem_dma_page_t {
  42. char *page_v_addr_start;
  43. char *page_v_addr_end;
  44. qdf_dma_addr_t page_p_addr;
  45. };
  46. /**
  47. * struct qdf_mem_multi_page_t - multiple page allocation information storage
  48. * @num_element_per_page: Number of element in single page
  49. * @num_pages: Number of allocation needed pages
  50. * @dma_pages: page information storage in case of coherent memory
  51. * @cacheable_pages: page information storage in case of cacheable memory
  52. */
  53. struct qdf_mem_multi_page_t {
  54. uint16_t num_element_per_page;
  55. uint16_t num_pages;
  56. struct qdf_mem_dma_page_t *dma_pages;
  57. void **cacheable_pages;
  58. };
  59. /* Preprocessor definitions and constants */
  60. typedef __qdf_mempool_t qdf_mempool_t;
  61. #ifdef MEMORY_DEBUG
  62. void qdf_mem_clean(void);
  63. void qdf_mem_init(void);
  64. void qdf_mem_exit(void);
  65. #else
  66. /**
  67. * qdf_mem_init() - initialize qdf memory debug functionality
  68. *
  69. * Return: none
  70. */
  71. static inline void qdf_mem_init(void)
  72. {
  73. }
  74. /**
  75. * qdf_mem_exit() - exit qdf memory debug functionality
  76. *
  77. * Return: none
  78. */
  79. static inline void qdf_mem_exit(void)
  80. {
  81. }
  82. #endif
  83. #ifdef MEMORY_DEBUG
  84. #define qdf_mem_malloc(size) \
  85. qdf_mem_malloc_debug(size, __FILE__, __LINE__)
  86. void *qdf_mem_malloc_debug(size_t size, char *file_name, uint32_t line_num);
  87. #else
  88. void *
  89. qdf_mem_malloc(qdf_size_t size);
  90. #endif
  91. void *qdf_mem_alloc_outline(qdf_device_t osdev, qdf_size_t size);
  92. /**
  93. * qdf_mem_free() - free QDF memory
  94. * @ptr: Pointer to the starting address of the memory to be free'd.
  95. * This function will free the memory pointed to by 'ptr'.
  96. * Return:
  97. * None
  98. */
  99. void qdf_mem_free(void *ptr);
  100. void qdf_mem_set(void *ptr, uint32_t num_bytes, uint32_t value);
  101. void qdf_mem_zero(void *ptr, uint32_t num_bytes);
  102. void qdf_mem_copy(void *dst_addr, const void *src_addr, uint32_t num_bytes);
  103. void qdf_mem_move(void *dst_addr, const void *src_addr, uint32_t num_bytes);
  104. void qdf_mem_free_outline(void *buf);
  105. void *qdf_mem_alloc_consistent(qdf_device_t osdev, void *dev, qdf_size_t size,
  106. qdf_dma_addr_t *paddr);
  107. void qdf_mem_free_consistent(qdf_device_t osdev, void *dev, qdf_size_t size,
  108. void *vaddr, qdf_dma_addr_t paddr,
  109. qdf_dma_context_t memctx);
  110. void qdf_mem_zero_outline(void *buf, qdf_size_t size);
  111. /**
  112. * qdf_mem_cmp() - memory compare
  113. * @memory1: pointer to one location in memory to compare.
  114. * @memory2: pointer to second location in memory to compare.
  115. * @num_bytes: the number of bytes to compare.
  116. *
  117. * Function to compare two pieces of memory, similar to memcmp function
  118. * in standard C.
  119. * Return:
  120. * int32_t - returns an int value that tells if the memory
  121. * locations are equal or not equal.
  122. * 0 -- equal
  123. * < 0 -- *memory1 is less than *memory2
  124. * > 0 -- *memory1 is bigger than *memory2
  125. */
  126. static inline int32_t qdf_mem_cmp(const void *memory1, const void *memory2,
  127. uint32_t num_bytes)
  128. {
  129. return __qdf_mem_cmp(memory1, memory2, num_bytes);
  130. }
  131. /**
  132. * qdf_str_cmp - Compare two strings
  133. * @str1: First string
  134. * @str2: Second string
  135. * Return: =0 equal
  136. * >0 not equal, if str1 sorts lexicographically after str2
  137. * <0 not equal, if str1 sorts lexicographically before str2
  138. */
  139. static inline int32_t qdf_str_cmp(const char *str1, const char *str2)
  140. {
  141. return __qdf_str_cmp(str1, str2);
  142. }
  143. /**
  144. * qdf_str_lcopy - Copy from one string to another
  145. * @dest: destination string
  146. * @src: source string
  147. * @bytes: limit of num bytes to copy
  148. * Return: =0 returns the initial value of dest
  149. */
  150. static inline uint32_t qdf_str_lcopy(char *dest, const char *src, uint32_t bytes)
  151. {
  152. return __qdf_str_lcopy(dest, src, bytes);
  153. }
  154. /**
  155. * qdf_mem_map_nbytes_single - Map memory for DMA
  156. * @osdev: pomter OS device context
  157. * @buf: pointer to memory to be dma mapped
  158. * @dir: DMA map direction
  159. * @nbytes: number of bytes to be mapped.
  160. * @phy_addr: ponter to recive physical address.
  161. *
  162. * Return: success/failure
  163. */
  164. static inline uint32_t qdf_mem_map_nbytes_single(qdf_device_t osdev, void *buf,
  165. qdf_dma_dir_t dir, int nbytes,
  166. uint32_t *phy_addr)
  167. {
  168. #if defined(HIF_PCI)
  169. return __qdf_mem_map_nbytes_single(osdev, buf, dir, nbytes, phy_addr);
  170. #else
  171. return 0;
  172. #endif
  173. }
  174. /**
  175. * qdf_mem_unmap_nbytes_single() - un_map memory for DMA
  176. * @osdev: pomter OS device context
  177. * @phy_addr: physical address of memory to be dma unmapped
  178. * @dir: DMA unmap direction
  179. * @nbytes: number of bytes to be unmapped.
  180. *
  181. * Return: none
  182. */
  183. static inline void qdf_mem_unmap_nbytes_single(qdf_device_t osdev,
  184. uint32_t phy_addr,
  185. qdf_dma_dir_t dir,
  186. int nbytes)
  187. {
  188. #if defined(HIF_PCI)
  189. __qdf_mem_unmap_nbytes_single(osdev, phy_addr, dir, nbytes);
  190. #endif
  191. }
  192. /**
  193. * qdf_mempool_init - Create and initialize memory pool
  194. * @osdev: platform device object
  195. * @pool_addr: address of the pool created
  196. * @elem_cnt: no. of elements in pool
  197. * @elem_size: size of each pool element in bytes
  198. * @flags: flags
  199. * Return: Handle to memory pool or NULL if allocation failed
  200. */
  201. static inline int qdf_mempool_init(qdf_device_t osdev,
  202. qdf_mempool_t *pool_addr, int elem_cnt,
  203. size_t elem_size, uint32_t flags)
  204. {
  205. return __qdf_mempool_init(osdev, pool_addr, elem_cnt, elem_size,
  206. flags);
  207. }
  208. /**
  209. * qdf_mempool_destroy - Destroy memory pool
  210. * @osdev: platform device object
  211. * @Handle: to memory pool
  212. * Return: none
  213. */
  214. static inline void qdf_mempool_destroy(qdf_device_t osdev, qdf_mempool_t pool)
  215. {
  216. __qdf_mempool_destroy(osdev, pool);
  217. }
  218. /**
  219. * qdf_mempool_alloc - Allocate an element memory pool
  220. * @osdev: platform device object
  221. * @Handle: to memory pool
  222. * Return: Pointer to the allocated element or NULL if the pool is empty
  223. */
  224. static inline void *qdf_mempool_alloc(qdf_device_t osdev, qdf_mempool_t pool)
  225. {
  226. return (void *)__qdf_mempool_alloc(osdev, pool);
  227. }
  228. /**
  229. * qdf_mempool_free - Free a memory pool element
  230. * @osdev: Platform device object
  231. * @pool: Handle to memory pool
  232. * @buf: Element to be freed
  233. * Return: none
  234. */
  235. static inline void qdf_mempool_free(qdf_device_t osdev, qdf_mempool_t pool,
  236. void *buf)
  237. {
  238. __qdf_mempool_free(osdev, pool, buf);
  239. }
  240. void qdf_mem_dma_sync_single_for_device(qdf_device_t osdev,
  241. qdf_dma_addr_t bus_addr,
  242. qdf_size_t size,
  243. __dma_data_direction direction);
  244. void qdf_mem_dma_sync_single_for_cpu(qdf_device_t osdev,
  245. qdf_dma_addr_t bus_addr,
  246. qdf_size_t size,
  247. __dma_data_direction direction);
  248. /**
  249. * qdf_str_len() - returns the length of a string
  250. * @str: input string
  251. * Return:
  252. * length of string
  253. */
  254. static inline int32_t qdf_str_len(const char *str)
  255. {
  256. return __qdf_str_len(str);
  257. }
  258. void qdf_mem_multi_pages_alloc(qdf_device_t osdev,
  259. struct qdf_mem_multi_page_t *pages,
  260. size_t element_size, uint16_t element_num,
  261. qdf_dma_context_t memctxt, bool cacheable);
  262. void qdf_mem_multi_pages_free(qdf_device_t osdev,
  263. struct qdf_mem_multi_page_t *pages,
  264. qdf_dma_context_t memctxt, bool cacheable);
  265. #endif /* __QDF_MEMORY_H */