qdf_mem.h 11 KB

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