qdf_mem.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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_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. /**
  86. * qdf_mem_malloc_debug() - debug version of QDF memory allocation API
  87. * @size: Number of bytes of memory to allocate.
  88. * @file: File name of the call site
  89. * @line: Line number of the call site
  90. *
  91. * This function will dynamicallly allocate the specified number of bytes of
  92. * memory and add it to the qdf tracking list to check for memory leaks and
  93. * corruptions
  94. *
  95. * Return: A valid memory location on success, or NULL on failure
  96. */
  97. void *qdf_mem_malloc_debug(size_t size, const char *file, uint32_t line);
  98. #define qdf_mem_malloc(size) \
  99. qdf_mem_malloc_debug(size, __FILE__, __LINE__)
  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, __FILE__, __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. * @file: file name of the call site
  150. * @line: line numbe rof the call site
  151. *
  152. * Return: pointer of allocated memory or null if memory alloc fails
  153. */
  154. void *qdf_mem_alloc_consistent_debug(qdf_device_t osdev, void *dev,
  155. qdf_size_t size, qdf_dma_addr_t *paddr,
  156. const char *file, uint32_t line);
  157. #define qdf_mem_alloc_consistent(osdev, dev, size, paddr) \
  158. qdf_mem_alloc_consistent_debug(osdev, dev, size, paddr, \
  159. __FILE__, __LINE__)
  160. /**
  161. * qdf_mem_free_consistent_debug() - free consistent qdf memory
  162. * @osdev: OS device handle
  163. * @size: Size to be allocated
  164. * @vaddr: virtual address
  165. * @paddr: Physical address
  166. * @memctx: Pointer to DMA context
  167. * @file: file name of the call site
  168. * @line: line numbe rof the call site
  169. *
  170. * Return: none
  171. */
  172. void qdf_mem_free_consistent_debug(qdf_device_t osdev, void *dev,
  173. qdf_size_t size, void *vaddr,
  174. qdf_dma_addr_t paddr,
  175. qdf_dma_context_t memctx,
  176. const char *file, uint32_t line);
  177. #define qdf_mem_free_consistent(osdev, dev, size, vaddr, paddr, memctx) \
  178. qdf_mem_free_consistent_debug(osdev, dev, size, vaddr, paddr, memctx, \
  179. __FILE__, __LINE__)
  180. #else
  181. void *qdf_mem_malloc(qdf_size_t size);
  182. /**
  183. * qdf_mem_free() - free QDF memory
  184. * @ptr: Pointer to the starting address of the memory to be freed.
  185. *
  186. * Return: None
  187. */
  188. void qdf_mem_free(void *ptr);
  189. static inline void qdf_mem_check_for_leaks(void) { }
  190. void *qdf_mem_alloc_consistent(qdf_device_t osdev, void *dev,
  191. qdf_size_t size, qdf_dma_addr_t *paddr);
  192. void qdf_mem_free_consistent(qdf_device_t osdev, void *dev,
  193. qdf_size_t size, void *vaddr,
  194. qdf_dma_addr_t paddr, qdf_dma_context_t memctx);
  195. #endif /* MEMORY_DEBUG */
  196. void *qdf_mem_alloc_outline(qdf_device_t osdev, qdf_size_t size);
  197. void qdf_mem_set(void *ptr, uint32_t num_bytes, uint32_t value);
  198. void qdf_mem_zero(void *ptr, uint32_t num_bytes);
  199. void qdf_mem_copy(void *dst_addr, const void *src_addr, uint32_t num_bytes);
  200. void qdf_mem_move(void *dst_addr, const void *src_addr, uint32_t num_bytes);
  201. void qdf_mem_free_outline(void *buf);
  202. void qdf_mem_zero_outline(void *buf, qdf_size_t size);
  203. void qdf_ether_addr_copy(void *dst_addr, const void *src_addr);
  204. /**
  205. * qdf_mem_cmp() - memory compare
  206. * @memory1: pointer to one location in memory to compare.
  207. * @memory2: pointer to second location in memory to compare.
  208. * @num_bytes: the number of bytes to compare.
  209. *
  210. * Function to compare two pieces of memory, similar to memcmp function
  211. * in standard C.
  212. * Return:
  213. * int32_t - returns an int value that tells if the memory
  214. * locations are equal or not equal.
  215. * 0 -- equal
  216. * < 0 -- *memory1 is less than *memory2
  217. * > 0 -- *memory1 is bigger than *memory2
  218. */
  219. static inline int32_t qdf_mem_cmp(const void *memory1, const void *memory2,
  220. uint32_t num_bytes)
  221. {
  222. return __qdf_mem_cmp(memory1, memory2, num_bytes);
  223. }
  224. /**
  225. * qdf_str_cmp - Compare two strings
  226. * @str1: First string
  227. * @str2: Second string
  228. * Return: =0 equal
  229. * >0 not equal, if str1 sorts lexicographically after str2
  230. * <0 not equal, if str1 sorts lexicographically before str2
  231. */
  232. static inline int32_t qdf_str_cmp(const char *str1, const char *str2)
  233. {
  234. return __qdf_str_cmp(str1, str2);
  235. }
  236. /**
  237. * qdf_str_lcopy - Copy from one string to another
  238. * @dest: destination string
  239. * @src: source string
  240. * @bytes: limit of num bytes to copy
  241. * Return: =0 returns the initial value of dest
  242. */
  243. static inline uint32_t qdf_str_lcopy(char *dest, const char *src, uint32_t bytes)
  244. {
  245. return __qdf_str_lcopy(dest, src, bytes);
  246. }
  247. /**
  248. * qdf_mem_map_nbytes_single - Map memory for DMA
  249. * @osdev: pomter OS device context
  250. * @buf: pointer to memory to be dma mapped
  251. * @dir: DMA map direction
  252. * @nbytes: number of bytes to be mapped.
  253. * @phy_addr: ponter to recive physical address.
  254. *
  255. * Return: success/failure
  256. */
  257. static inline uint32_t qdf_mem_map_nbytes_single(qdf_device_t osdev, void *buf,
  258. qdf_dma_dir_t dir, int nbytes,
  259. qdf_dma_addr_t *phy_addr)
  260. {
  261. #if defined(HIF_PCI)
  262. return __qdf_mem_map_nbytes_single(osdev, buf, dir, nbytes, phy_addr);
  263. #else
  264. return 0;
  265. #endif
  266. }
  267. /**
  268. * qdf_mem_unmap_nbytes_single() - un_map memory for DMA
  269. * @osdev: pomter OS device context
  270. * @phy_addr: physical address of memory to be dma unmapped
  271. * @dir: DMA unmap direction
  272. * @nbytes: number of bytes to be unmapped.
  273. *
  274. * Return: none
  275. */
  276. static inline void qdf_mem_unmap_nbytes_single(qdf_device_t osdev,
  277. qdf_dma_addr_t phy_addr,
  278. qdf_dma_dir_t dir,
  279. int nbytes)
  280. {
  281. #if defined(HIF_PCI)
  282. __qdf_mem_unmap_nbytes_single(osdev, phy_addr, dir, nbytes);
  283. #endif
  284. }
  285. /**
  286. * qdf_mempool_init - Create and initialize memory pool
  287. * @osdev: platform device object
  288. * @pool_addr: address of the pool created
  289. * @elem_cnt: no. of elements in pool
  290. * @elem_size: size of each pool element in bytes
  291. * @flags: flags
  292. * Return: Handle to memory pool or NULL if allocation failed
  293. */
  294. static inline int qdf_mempool_init(qdf_device_t osdev,
  295. qdf_mempool_t *pool_addr, int elem_cnt,
  296. size_t elem_size, uint32_t flags)
  297. {
  298. return __qdf_mempool_init(osdev, pool_addr, elem_cnt, elem_size,
  299. flags);
  300. }
  301. /**
  302. * qdf_mempool_destroy - Destroy memory pool
  303. * @osdev: platform device object
  304. * @Handle: to memory pool
  305. * Return: none
  306. */
  307. static inline void qdf_mempool_destroy(qdf_device_t osdev, qdf_mempool_t pool)
  308. {
  309. __qdf_mempool_destroy(osdev, pool);
  310. }
  311. /**
  312. * qdf_mempool_alloc - Allocate an element memory pool
  313. * @osdev: platform device object
  314. * @Handle: to memory pool
  315. * Return: Pointer to the allocated element or NULL if the pool is empty
  316. */
  317. static inline void *qdf_mempool_alloc(qdf_device_t osdev, qdf_mempool_t pool)
  318. {
  319. return (void *)__qdf_mempool_alloc(osdev, pool);
  320. }
  321. /**
  322. * qdf_mempool_free - Free a memory pool element
  323. * @osdev: Platform device object
  324. * @pool: Handle to memory pool
  325. * @buf: Element to be freed
  326. * Return: none
  327. */
  328. static inline void qdf_mempool_free(qdf_device_t osdev, qdf_mempool_t pool,
  329. void *buf)
  330. {
  331. __qdf_mempool_free(osdev, pool, buf);
  332. }
  333. void qdf_mem_dma_sync_single_for_device(qdf_device_t osdev,
  334. qdf_dma_addr_t bus_addr,
  335. qdf_size_t size,
  336. __dma_data_direction direction);
  337. void qdf_mem_dma_sync_single_for_cpu(qdf_device_t osdev,
  338. qdf_dma_addr_t bus_addr,
  339. qdf_size_t size,
  340. __dma_data_direction direction);
  341. /**
  342. * qdf_str_len() - returns the length of a string
  343. * @str: input string
  344. * Return:
  345. * length of string
  346. */
  347. static inline int32_t qdf_str_len(const char *str)
  348. {
  349. return __qdf_str_len(str);
  350. }
  351. void qdf_mem_multi_pages_alloc(qdf_device_t osdev,
  352. struct qdf_mem_multi_page_t *pages,
  353. size_t element_size, uint16_t element_num,
  354. qdf_dma_context_t memctxt, bool cacheable);
  355. void qdf_mem_multi_pages_free(qdf_device_t osdev,
  356. struct qdf_mem_multi_page_t *pages,
  357. qdf_dma_context_t memctxt, bool cacheable);
  358. int qdf_mem_multi_page_link(qdf_device_t osdev,
  359. struct qdf_mem_multi_page_t *pages,
  360. uint32_t elem_size, uint32_t elem_count, uint8_t cacheable);
  361. /**
  362. * qdf_mem_skb_inc() - increment total skb allocation size
  363. * @size: size to be added
  364. *
  365. * Return: none
  366. */
  367. void qdf_mem_skb_inc(qdf_size_t size);
  368. /**
  369. * qdf_mem_skb_dec() - decrement total skb allocation size
  370. * @size: size to be decremented
  371. *
  372. * Return: none
  373. */
  374. void qdf_mem_skb_dec(qdf_size_t size);
  375. #endif /* __QDF_MEMORY_H */