qdf_mem.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (c) 2014-2017 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. #define QDF_CACHE_LINE_SZ __qdf_cache_line_sz
  36. /**
  37. * qdf_align() - align to the given size.
  38. * @a: input that needs to be aligned.
  39. * @align_size: boundary on which 'a' has to be alinged.
  40. *
  41. * Return: aligned value.
  42. */
  43. #define qdf_align(a, align_size) __qdf_align(a, align_size)
  44. /**
  45. * struct qdf_mem_dma_page_t - Allocated dmaable page
  46. * @page_v_addr_start: Page start virtual address
  47. * @page_v_addr_end: Page end virtual address
  48. * @page_p_addr: Page start physical address
  49. */
  50. struct qdf_mem_dma_page_t {
  51. char *page_v_addr_start;
  52. char *page_v_addr_end;
  53. qdf_dma_addr_t page_p_addr;
  54. };
  55. /**
  56. * struct qdf_mem_multi_page_t - multiple page allocation information storage
  57. * @num_element_per_page: Number of element in single page
  58. * @num_pages: Number of allocation needed pages
  59. * @dma_pages: page information storage in case of coherent memory
  60. * @cacheable_pages: page information storage in case of cacheable memory
  61. */
  62. struct qdf_mem_multi_page_t {
  63. uint16_t num_element_per_page;
  64. uint16_t num_pages;
  65. struct qdf_mem_dma_page_t *dma_pages;
  66. void **cacheable_pages;
  67. };
  68. /* Preprocessor definitions and constants */
  69. typedef __qdf_mempool_t qdf_mempool_t;
  70. /**
  71. * qdf_mem_init() - Initialize QDF memory module
  72. *
  73. * Return: None
  74. *
  75. */
  76. void qdf_mem_init(void);
  77. /**
  78. * qdf_mem_exit() - Exit QDF memory module
  79. *
  80. * Return: None
  81. *
  82. */
  83. void qdf_mem_exit(void);
  84. #ifdef MEMORY_DEBUG
  85. #define qdf_mem_malloc(size) \
  86. qdf_mem_malloc_debug(size, __FILE__, __LINE__)
  87. void *qdf_mem_malloc_debug(size_t size, char *file_name, uint32_t line_num);
  88. /**
  89. * qdf_mem_check_for_leaks() - Assert that the current memory domain is empty
  90. *
  91. * Call this to ensure there are no active memory allocations being tracked
  92. * against the current debug domain. For example, one should call this function
  93. * immediately before a call to qdf_debug_domain_set() as a memory leak
  94. * detection mechanism.
  95. *
  96. * e.g.
  97. * qdf_debug_domain_set(QDF_DEBUG_DOMAIN_ACTIVE);
  98. *
  99. * ...
  100. *
  101. * // memory is allocated and freed
  102. *
  103. * ...
  104. *
  105. * // before transitioning back to inactive state,
  106. * // make sure all active memory has been freed
  107. * qdf_mem_check_for_leaks();
  108. * qdf_debug_domain_set(QDF_DEBUG_DOMAIN_INIT);
  109. *
  110. * ...
  111. *
  112. * // also, before program exit, make sure init time memory is freed
  113. * qdf_mem_check_for_leaks();
  114. * exit();
  115. *
  116. * Return: None
  117. */
  118. void qdf_mem_check_for_leaks(void);
  119. #else
  120. void *qdf_mem_malloc(qdf_size_t size);
  121. static inline void qdf_mem_check_for_leaks(void) { }
  122. #endif /* MEMORY_DEBUG */
  123. void *qdf_mem_alloc_outline(qdf_device_t osdev, qdf_size_t size);
  124. /**
  125. * qdf_mem_free() - free QDF memory
  126. * @ptr: Pointer to the starting address of the memory to be free'd.
  127. * This function will free the memory pointed to by 'ptr'.
  128. * Return:
  129. * None
  130. */
  131. void qdf_mem_free(void *ptr);
  132. void qdf_mem_set(void *ptr, uint32_t num_bytes, uint32_t value);
  133. void qdf_mem_zero(void *ptr, uint32_t num_bytes);
  134. void qdf_mem_copy(void *dst_addr, const void *src_addr, uint32_t num_bytes);
  135. void qdf_mem_move(void *dst_addr, const void *src_addr, uint32_t num_bytes);
  136. void qdf_mem_free_outline(void *buf);
  137. void *qdf_mem_alloc_consistent(qdf_device_t osdev, void *dev, qdf_size_t size,
  138. qdf_dma_addr_t *paddr);
  139. void qdf_mem_free_consistent(qdf_device_t osdev, void *dev, qdf_size_t size,
  140. void *vaddr, qdf_dma_addr_t paddr,
  141. qdf_dma_context_t memctx);
  142. void qdf_mem_zero_outline(void *buf, qdf_size_t size);
  143. void qdf_ether_addr_copy(void *dst_addr, const void *src_addr);
  144. /**
  145. * qdf_mem_cmp() - memory compare
  146. * @memory1: pointer to one location in memory to compare.
  147. * @memory2: pointer to second location in memory to compare.
  148. * @num_bytes: the number of bytes to compare.
  149. *
  150. * Function to compare two pieces of memory, similar to memcmp function
  151. * in standard C.
  152. * Return:
  153. * int32_t - returns an int value that tells if the memory
  154. * locations are equal or not equal.
  155. * 0 -- equal
  156. * < 0 -- *memory1 is less than *memory2
  157. * > 0 -- *memory1 is bigger than *memory2
  158. */
  159. static inline int32_t qdf_mem_cmp(const void *memory1, const void *memory2,
  160. uint32_t num_bytes)
  161. {
  162. return __qdf_mem_cmp(memory1, memory2, num_bytes);
  163. }
  164. /**
  165. * qdf_str_cmp - Compare two strings
  166. * @str1: First string
  167. * @str2: Second string
  168. * Return: =0 equal
  169. * >0 not equal, if str1 sorts lexicographically after str2
  170. * <0 not equal, if str1 sorts lexicographically before str2
  171. */
  172. static inline int32_t qdf_str_cmp(const char *str1, const char *str2)
  173. {
  174. return __qdf_str_cmp(str1, str2);
  175. }
  176. /**
  177. * qdf_str_lcopy - Copy from one string to another
  178. * @dest: destination string
  179. * @src: source string
  180. * @bytes: limit of num bytes to copy
  181. * Return: =0 returns the initial value of dest
  182. */
  183. static inline uint32_t qdf_str_lcopy(char *dest, const char *src, uint32_t bytes)
  184. {
  185. return __qdf_str_lcopy(dest, src, bytes);
  186. }
  187. /**
  188. * qdf_mem_map_nbytes_single - Map memory for DMA
  189. * @osdev: pomter OS device context
  190. * @buf: pointer to memory to be dma mapped
  191. * @dir: DMA map direction
  192. * @nbytes: number of bytes to be mapped.
  193. * @phy_addr: ponter to recive physical address.
  194. *
  195. * Return: success/failure
  196. */
  197. static inline uint32_t qdf_mem_map_nbytes_single(qdf_device_t osdev, void *buf,
  198. qdf_dma_dir_t dir, int nbytes,
  199. qdf_dma_addr_t *phy_addr)
  200. {
  201. #if defined(HIF_PCI)
  202. return __qdf_mem_map_nbytes_single(osdev, buf, dir, nbytes, phy_addr);
  203. #else
  204. return 0;
  205. #endif
  206. }
  207. /**
  208. * qdf_mem_unmap_nbytes_single() - un_map memory for DMA
  209. * @osdev: pomter OS device context
  210. * @phy_addr: physical address of memory to be dma unmapped
  211. * @dir: DMA unmap direction
  212. * @nbytes: number of bytes to be unmapped.
  213. *
  214. * Return: none
  215. */
  216. static inline void qdf_mem_unmap_nbytes_single(qdf_device_t osdev,
  217. qdf_dma_addr_t phy_addr,
  218. qdf_dma_dir_t dir,
  219. int nbytes)
  220. {
  221. #if defined(HIF_PCI)
  222. __qdf_mem_unmap_nbytes_single(osdev, phy_addr, dir, nbytes);
  223. #endif
  224. }
  225. /**
  226. * qdf_mempool_init - Create and initialize memory pool
  227. * @osdev: platform device object
  228. * @pool_addr: address of the pool created
  229. * @elem_cnt: no. of elements in pool
  230. * @elem_size: size of each pool element in bytes
  231. * @flags: flags
  232. * Return: Handle to memory pool or NULL if allocation failed
  233. */
  234. static inline int qdf_mempool_init(qdf_device_t osdev,
  235. qdf_mempool_t *pool_addr, int elem_cnt,
  236. size_t elem_size, uint32_t flags)
  237. {
  238. return __qdf_mempool_init(osdev, pool_addr, elem_cnt, elem_size,
  239. flags);
  240. }
  241. /**
  242. * qdf_mempool_destroy - Destroy memory pool
  243. * @osdev: platform device object
  244. * @Handle: to memory pool
  245. * Return: none
  246. */
  247. static inline void qdf_mempool_destroy(qdf_device_t osdev, qdf_mempool_t pool)
  248. {
  249. __qdf_mempool_destroy(osdev, pool);
  250. }
  251. /**
  252. * qdf_mempool_alloc - Allocate an element memory pool
  253. * @osdev: platform device object
  254. * @Handle: to memory pool
  255. * Return: Pointer to the allocated element or NULL if the pool is empty
  256. */
  257. static inline void *qdf_mempool_alloc(qdf_device_t osdev, qdf_mempool_t pool)
  258. {
  259. return (void *)__qdf_mempool_alloc(osdev, pool);
  260. }
  261. /**
  262. * qdf_mempool_free - Free a memory pool element
  263. * @osdev: Platform device object
  264. * @pool: Handle to memory pool
  265. * @buf: Element to be freed
  266. * Return: none
  267. */
  268. static inline void qdf_mempool_free(qdf_device_t osdev, qdf_mempool_t pool,
  269. void *buf)
  270. {
  271. __qdf_mempool_free(osdev, pool, buf);
  272. }
  273. void qdf_mem_dma_sync_single_for_device(qdf_device_t osdev,
  274. qdf_dma_addr_t bus_addr,
  275. qdf_size_t size,
  276. __dma_data_direction direction);
  277. void qdf_mem_dma_sync_single_for_cpu(qdf_device_t osdev,
  278. qdf_dma_addr_t bus_addr,
  279. qdf_size_t size,
  280. __dma_data_direction direction);
  281. /**
  282. * qdf_str_len() - returns the length of a string
  283. * @str: input string
  284. * Return:
  285. * length of string
  286. */
  287. static inline int32_t qdf_str_len(const char *str)
  288. {
  289. return __qdf_str_len(str);
  290. }
  291. void qdf_mem_multi_pages_alloc(qdf_device_t osdev,
  292. struct qdf_mem_multi_page_t *pages,
  293. size_t element_size, uint16_t element_num,
  294. qdf_dma_context_t memctxt, bool cacheable);
  295. void qdf_mem_multi_pages_free(qdf_device_t osdev,
  296. struct qdf_mem_multi_page_t *pages,
  297. qdf_dma_context_t memctxt, bool cacheable);
  298. int qdf_mem_multi_page_link(qdf_device_t osdev,
  299. struct qdf_mem_multi_page_t *pages,
  300. uint32_t elem_size, uint32_t elem_count, uint8_t cacheable);
  301. /**
  302. * qdf_mem_skb_inc() - increment total skb allocation size
  303. * @size: size to be added
  304. *
  305. * Return: none
  306. */
  307. void qdf_mem_skb_inc(qdf_size_t size);
  308. /**
  309. * qdf_mem_skb_dec() - decrement total skb allocation size
  310. * @size: size to be decremented
  311. *
  312. * Return: none
  313. */
  314. void qdf_mem_skb_dec(qdf_size_t size);
  315. #endif /* __QDF_MEMORY_H */